2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
27 ** constraintResolve.c
30 /* #define DEBUGPRINT 1 */
32 # include <ctype.h> /* for isdigit */
33 # include "lclintMacros.nf"
35 # include "cgrammar.h"
36 # include "cgrammar_tokens.h"
38 # include "exprChecks.h"
39 # include "exprNodeSList.h"
42 /*@access constraint, exprNode @*/
45 static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
48 static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
50 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
52 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
54 static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
57 static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
59 static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
61 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
63 /*********************************************/
68 /*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
72 ret = constraintList_mergeEnsures (list1, list2);
74 constraintList_free(list1);
78 /*@only@*/ constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2)
83 llassert(constraintList_isDefined(list1) );
84 llassert(constraintList_isDefined(list2) );
86 DPRINTF(( message ("constraintList_mergeEnsures: list1 %s list2 %s",
87 constraintList_print(list1), constraintList_print(list2)
90 ret = constraintList_fixConflicts (list1, list2);
91 ret = reflectChangesEnsuresFree1 (ret, list2);
92 temp = constraintList_subsumeEnsures (ret, list2);
93 constraintList_free(ret);
96 temp = constraintList_subsumeEnsures (list2, ret);
98 temp = constraintList_addList (temp, ret);
99 constraintList_free(ret);
101 DPRINTF(( message ("constraintList_mergeEnsures: returning %s ",
102 constraintList_print(temp) )
110 /*@only@*/ constraintList constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1, constraintList list2)
114 ret = constraintList_mergeRequires(list1, list2);
116 constraintList_free(list1);
121 /*@only@*/ constraintList constraintList_mergeRequires (constraintList list1, constraintList list2)
126 DPRINTF((message ("constraintList_mergeRequires: merging %s and %s ", constraintList_print (list1), constraintList_print(list2) ) ) );
128 /* get constraints in list1 not satified by list2 */
129 temp = constraintList_reflectChanges(list1, list2);
130 DPRINTF((message ("constraintList_mergeRequires: temp = %s", constraintList_print(temp) ) ) );
132 /*get constraints in list2 not satified by temp*/
133 ret = constraintList_reflectChanges(list2, temp);
135 DPRINTF((message ("constraintList_mergeRequires: ret = %s", constraintList_print(ret) ) ) );
137 ret = constraintList_addListFree (ret, temp);
139 DPRINTF((message ("constraintList_mergeRequires: returning %s", constraintList_print(ret) ) ) );
144 /* old name mergeResolve renamed for czech naming convention */
145 void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
147 constraintList temp, temp2;
149 DPRINTF((message ("magically merging constraint into parent:%s for", exprNode_unparse (parent) )) );
151 DPRINTF((message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2) ) ) );
153 if (exprNode_isError (child1) || exprNode_isError(child2) )
155 if (exprNode_isError (child1) && !exprNode_isError(child2) )
157 constraintList_free(parent->requiresConstraints);
159 parent->requiresConstraints = constraintList_copy (child2->requiresConstraints);
160 constraintList_free(parent->ensuresConstraints);
162 parent->ensuresConstraints = constraintList_copy (child2->ensuresConstraints);
163 DPRINTF((message ("Copied child constraints: pre: %s and post: %s",
164 constraintList_print( child2->requiresConstraints),
165 constraintList_print (child2->ensuresConstraints)
172 llassert(exprNode_isError(child2) );
177 llassert(!exprNode_isError (child1) && ! exprNode_isError(child2) );
179 DPRINTF((message ("Child constraints are %s %s and %s %s",
180 constraintList_print (child1->requiresConstraints),
181 constraintList_print (child1->ensuresConstraints),
182 constraintList_print (child2->requiresConstraints),
183 constraintList_print (child2->ensuresConstraints)
187 constraintList_free(parent->requiresConstraints);
189 parent->requiresConstraints = constraintList_copy (child1->requiresConstraints);
191 if ( context_getFlag (FLG_ORCONSTRAINT) )
192 temp = constraintList_reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
194 temp = constraintList_reflectChanges(child2->requiresConstraints, child1->ensuresConstraints);
196 temp2 = constraintList_mergeRequires (parent->requiresConstraints, temp);
197 constraintList_free(parent->requiresConstraints);
198 constraintList_free(temp);
200 parent->requiresConstraints = temp2;
202 DPRINTF((message ("Parent requires constraints are %s ",
203 constraintList_print (parent->requiresConstraints)
206 constraintList_free(parent->ensuresConstraints);
208 parent->ensuresConstraints = constraintList_mergeEnsures(child1->ensuresConstraints,
209 child2->ensuresConstraints);
212 DPRINTF((message ("Parent constraints are %s and %s ",
213 constraintList_print (parent->requiresConstraints),
214 constraintList_print (parent->ensuresConstraints)
222 /*@only@*/ constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2)
225 ret = constraintList_makeNew();
226 constraintList_elements (list1, el)
229 DPRINTF ((message ("Examining %s", constraint_print (el) ) ) );
230 if (!constraintList_resolve (el, list2) )
233 temp = constraint_copy(el);
234 ret = constraintList_add (ret, temp);
238 DPRINTF ((message ("Subsuming %s", constraint_print (el) ) ) );
240 } end_constraintList_elements;
247 /*used to be reflectChangesFreePre renamed for Czech naming conventino*/
248 /* tries to resolve constraints in list pre2 using post1 */
249 /*@only@*/ constraintList constraintList_reflectChangesFreePre (/*@only@*/ constraintList pre2, /*@observer@*/ constraintList post1)
253 ret = constraintList_reflectChanges(pre2, post1);
255 constraintList_free (pre2);
262 /* tries to resolve constraints in list pre2 using post1 */
264 static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
271 llassert (! context_getFlag (FLG_ORCONSTRAINT) );
273 ret = constraintList_makeNew();
274 DPRINTF((message ("reflectChanges: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
276 constraintList_elements (pre2, el)
278 if (!constraintList_resolve (el, post1) )
280 temp = constraint_substitute (el, post1);
281 if (!constraintList_resolve (temp, post1) )
283 /* try inequality substitution
284 the inequality substitution may cause us to lose information
285 so we don't want to store the result but we do it anyway
287 temp2 = constraint_copy (temp);
288 temp2 = inequalitySubstitute (temp2, post1);
289 if (!constraintList_resolve (temp2, post1) )
291 temp2 = inequalitySubstituteUnsound (temp2, post1);
292 if (!constraintList_resolve (temp2, post1) )
293 ret = constraintList_add (ret, temp2);
295 constraint_free(temp2);
299 constraint_free(temp2);
302 constraint_free(temp);
304 } end_constraintList_elements;
306 DPRINTF((message ("reflectChanges: returning %s", constraintList_print(ret) ) ) );
310 /* tries to resolve constraints in list pre2 using post1 */
311 /*@only@*/ constraintList constraintList_reflectChanges(/*@observer@*/ constraintList pre2, /*@observer@*/ constraintList post1)
315 if ( context_getFlag (FLG_ORCONSTRAINT) )
317 temp = constraintList_reflectChangesOr (pre2, post1);
319 temp = reflectChangesNoOr(pre2, post1);
324 static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
329 DPRINTF((message("constraint_addor: oring %s onto %s", constraint_printOr(orConstr), constraint_printOr(orig) ) ));
331 while (c->or != NULL)
336 c->or = constraint_copy(orConstr);
338 DPRINTF((message("constraint_addor: returning %s",constraint_printOr(orig) ) ));
344 static bool resolveOr ( /*@temp@*/ constraint c, /*@observer@*/ /*@temp@*/ constraintList list)
351 DPRINTF(( message("resolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(list) ) ));
356 if (constraintList_resolve (temp, list) )
360 llassert(numberOr <= 10);
362 while (constraint_isDefined(temp));
367 /*This is a "helper" function for doResolveOr */
369 static /*@only@*/ constraint doResolve (/*@only@*/ constraint c, constraintList post1, bool * resolved)
373 llassert(constraint_isUndefined (c->or ) );
375 if (!resolveOr (c, post1) )
378 temp = constraint_substitute (c, post1);
380 if (!resolveOr (temp, post1) )
382 /* try inequality substitution */
385 /* the inequality substitution may cause us to lose information
386 so we don't want to store the result but we do anyway
388 temp2 = constraint_copy (c);
389 temp2 = inequalitySubstitute (temp2, post1);
391 if (!resolveOr (temp2, post1) )
394 temp3 = constraint_copy(temp2);
396 temp3 = inequalitySubstituteStrong (temp3, post1);
397 if (!resolveOr (temp3, post1) )
399 temp2 = inequalitySubstituteUnsound (temp2, post1);
400 if (!resolveOr (temp2, post1) )
402 if (!constraint_same (temp, temp2) )
403 temp = constraint_addOr (temp, temp2);
405 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
406 temp = constraint_addOr (temp, temp3);
410 constraint_free(temp2);
411 constraint_free(temp3);
416 constraint_free(temp2);
417 constraint_free(temp3);
421 constraint_free(temp2);
422 constraint_free(temp3);
427 constraint_free(temp2);
431 constraint_free(temp);
439 static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
446 DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
453 ret = constraint_copy(c);
455 if (constraintList_isEmpty(post1) )
463 ret = doResolve (ret, post1, resolved);
468 constraint_free(next);
470 /*we don't need to free ret when resolved is false because ret is null*/
471 llassert(ret == NULL);
482 curr = doResolve (curr, post1, resolved);
486 /* curr is null so we don't try to free it*/
487 llassert(curr == NULL);
490 constraint_free(next);
492 constraint_free(ret);
495 ret = constraint_addOr (ret, curr);
496 constraint_free(curr);
501 /* tries to resolve constraints in list pr2 using post1 */
502 /*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
507 ret = constraintList_makeNew();
508 DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
510 constraintList_elements (pre2, el)
512 temp = doResolveOr (el, post1, &resolved);
516 ret = constraintList_add(ret, temp);
520 /* we don't need to free temp when
521 resolved is false because temp is null */
522 llassert(temp == NULL);
525 } end_constraintList_elements;
527 DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
531 static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
535 ret = constraintList_makeNew();
536 constraintList_elements (pre2, el)
538 if (!constraintList_resolve (el, post1) )
540 temp = constraint_substitute (el, post1);
541 llassert (temp != NULL);
543 if (!constraintList_resolve (temp, post1) )
544 ret = constraintList_add (ret, temp);
546 constraint_free(temp);
550 DPRINTF ((message ("Resolved away %s ", constraint_print(el) ) ) );
552 } end_constraintList_elements;
558 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
562 ret = reflectChangesEnsures (pre2, post1);
564 constraintList_free(pre2);
570 static bool constraint_conflict (constraint c1, constraint c2)
573 if (constraintExpr_similar(c1->lexpr, c2->lexpr) )
576 if (c1->ar == c2->ar)
578 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
583 /* This is a slight kludg to prevent circular constraints like
584 strlen(str) == maxRead(s) + strlen(str);
587 /*@i324234*/ /* clean this up */
590 if (c1->ar == c2->ar)
592 if (constraintExpr_search (c1->lexpr, c2->expr) )
593 if (constraintExpr_isTerm(c1->lexpr) )
597 term = constraintExpr_getTerm(c1->lexpr);
599 if (constraintTerm_isExprNode(term) )
601 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
607 if (constraint_tooDeep(c1) || constraint_tooDeep(c2) )
609 DPRINTF ((message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) );
613 DPRINTF ((message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
619 static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
621 if (conflicting->ar ==EQ )
623 good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
624 good = constraint_simplify (good);
630 static bool conflict (constraint c, constraintList list)
633 constraintList_elements (list, el)
635 if ( constraint_conflict(el, c) )
637 constraint_fixConflict (el, c);
640 } end_constraintList_elements;
647 check if constraint in list1 conflicts with constraints in List2. If so we
648 remove form list1 and change list2.
651 constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
654 ret = constraintList_makeNew();
655 llassert(constraintList_isDefined(list1) );
656 constraintList_elements (list1, el)
658 if (! conflict (el, list2) )
661 temp = constraint_copy(el);
662 ret = constraintList_add (ret, temp);
664 } end_constraintList_elements;
669 /*returns true if constraint post satifies cosntriant pre */
670 static bool satifies (constraint pre, constraint post)
672 if (constraint_isAlwaysTrue (pre) )
675 if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
679 if (constraintExpr_isUndefined(post->expr))
685 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
689 bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p)
691 constraintList_elements (p, el)
693 if ( satifies (c, el) )
695 DPRINTF ((message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
698 DPRINTF ((message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) );
700 end_constraintList_elements;
701 DPRINTF ((message ("no constraints satify %s", constraint_print(c) ) ));
705 static bool arithType_canResolve (arithType ar1, arithType ar2)
711 if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ) )
724 if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ) )
733 /*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
734 static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
739 /*@access constraintExpr@*/
741 if ((expr2->kind != term) && (buf1->kind != term) )
745 ct = constraintExprData_termGetTerm(expr2->data);
747 if (!constraintTerm_isExprNode(ct) )
750 e = constraintTerm_getExprNode(ct);
752 if (e->kind != XPR_SIZEOF)
755 t = exprData_getSingle (e->edata);
756 s1 = exprNode_getSref (t);
758 s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) );
760 /*@i223@*/ /*this may be the wronge thing to test for */
761 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
763 /*@i22*/ /* get rid of this test of now */
764 /* if (ctype_isFixedArray (sRef_getType (s2) ) ) */
770 /* look for the special case of
771 maxSet(buf) >= sizeof(buf) - 1
774 /*@i223@*/ /*need to add some type checking */
775 static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
777 constraintExpr l, r, buf1, buf2, con;
779 DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
785 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
788 /*check if the constraintExpr is MaxSet(buf) */
789 if (l->kind == unaryExpr)
791 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
793 buf1 = constraintExprData_unaryExprGetExpr(l->data);
802 if (r->kind != binaryexpr)
805 buf2 = constraintExprData_binaryExprGetExpr1(r->data);
806 con = constraintExprData_binaryExprGetExpr2(r->data);
808 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS)
810 if (constraintExpr_canGetValue(con) )
814 i = constraintExpr_getValue(con);
824 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS)
826 if (constraintExpr_canGetValue(con) )
830 i = constraintExpr_getValue(con);
840 if (sizeofBufComp(buf1, buf2))
851 /*@noaccess constraintExpr@*/
853 /* We look for constraint which are tautologies */
855 bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
864 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
866 if (sizeOfMaxSet(c) )
869 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
872 cmp = constraintExpr_compare (l, r);
893 if (constraintExpr_similar (l,r) )
912 l = constraintExpr_copy (c->lexpr);
913 r = constraintExpr_copy (c->expr);
915 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
917 if (constraintExpr_similar (l,r) && (rHasConstant ) )
919 DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
920 DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
922 constraintExpr_free(l);
923 constraintExpr_free(r);
928 return (rConstant == 0);
930 return (rConstant > 0);
932 return (rConstant >= 0);
934 return (rConstant <= 0);
936 return (rConstant < 0);
946 constraintExpr_free(l);
947 constraintExpr_free(r);
948 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
955 static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
958 DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
960 if (! arithType_canResolve (ar1, ar2) )
966 if (constraintExpr_similar (expr1, expr2) )
970 if (! (constraintExpr_canGetValue (expr1) &&
971 constraintExpr_canGetValue (expr2) ) )
973 constraintExpr e1, e2;
977 e1 = constraintExpr_copy(expr1);
978 e2 = constraintExpr_copy(expr2);
980 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
982 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
992 if (const1 <= const2)
993 if (constraintExpr_similar (e1, e2) )
995 constraintExpr_free(e1);
996 constraintExpr_free(e2);
1000 DPRINTF(("Can't Get value"));
1002 constraintExpr_free(e1);
1003 constraintExpr_free(e2);
1007 if (constraintExpr_compare (expr2, expr1) >= 0)
1012 if (constraintExpr_similar (expr1, expr2) )
1017 if (constraintExpr_similar (expr1, expr2) )
1021 if (! (constraintExpr_canGetValue (expr1) &&
1022 constraintExpr_canGetValue (expr2) ) )
1024 constraintExpr e1, e2;
1028 e1 = constraintExpr_copy(expr1);
1029 e2 = constraintExpr_copy(expr2);
1031 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1033 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1043 if (const1 >= const2)
1044 if (constraintExpr_similar (e1, e2) )
1046 constraintExpr_free(e1);
1047 constraintExpr_free(e2);
1051 constraintExpr_free(e1);
1052 constraintExpr_free(e2);
1054 DPRINTF(("Can't Get value"));
1058 if (constraintExpr_compare (expr2, expr1) <= 0)
1064 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
1069 static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
1071 DPRINTF (("Doing replace for lexpr") );
1072 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
1073 DPRINTF (("Doing replace for expr") );
1074 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
1078 bool constraint_search (constraint c, constraintExpr old) /*@*/
1083 ret = constraintExpr_search (c->lexpr, old);
1084 ret = ret || constraintExpr_search (c->expr, old);
1088 /* adjust file locs and stuff */
1089 static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
1091 fileloc loc1, loc2, loc3;
1093 DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute),
1094 constraint_print(old))
1097 loc1 = constraint_getFileloc (old);
1098 loc2 = constraintExpr_getFileloc (substitute->lexpr);
1099 loc3 = constraintExpr_getFileloc (substitute->expr);
1101 /* special case of an equality that "contains itself" */
1102 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
1103 if (fileloc_closer (loc1, loc3, loc2))
1105 constraintExpr temp;
1106 DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) )
1108 temp = substitute->lexpr;
1109 substitute->lexpr = substitute->expr;
1110 substitute->expr = temp;
1111 substitute = constraint_simplify(substitute);
1114 fileloc_free (loc1);
1115 fileloc_free (loc2);
1116 fileloc_free (loc3);
1122 /* If function preforms substitutes based on inequality
1124 It uses the rule x >= y && b < y ===> x >= b + 1
1126 Warning this is sound but throws out information
1129 constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
1134 constraintList_elements (p, el)
1136 if ((el->ar == LT ) )
1137 /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */
1139 constraintExpr temp2;
1143 if (constraintExpr_same (el->expr, c->expr) )
1145 DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
1146 constraintExpr_print (c->expr),
1147 constraint_print (c),
1148 constraintExpr_print (el->expr) )
1150 temp2 = constraintExpr_copy (el->lexpr);
1151 constraintExpr_free(c->expr);
1152 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1158 end_constraintList_elements;
1160 c = constraint_simplify(c);
1167 THis function is like inequalitySubstitute but it adds the rule
1168 added the rules x >= y && y <= b ===> x >= b
1169 x >= y && y < b ===> x >= b + 1
1171 This is sound but sonce it throws out additional information it should only one used
1172 if we're oring constraints.
1175 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1177 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
1182 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1183 constraint_print(c), constraintList_print(p) ) ));
1184 constraintList_elements (p, el)
1186 DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1188 if ((el->ar == LT ) || (el->ar == LTE ) )
1189 /* if (!constraint_conflict (c, el) ) */ /*@i523@*/
1191 constraintExpr temp2;
1195 if (constraintExpr_same (el->lexpr, c->expr) )
1197 DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
1198 constraintExpr_print (c->expr),
1199 constraint_print (c),
1200 constraintExpr_print (el->expr) )
1202 temp2 = constraintExpr_copy (el->expr);
1203 constraintExpr_free(c->expr);
1204 if ((el->ar == LTE ) )
1210 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1216 end_constraintList_elements;
1218 c = constraint_simplify(c);
1223 /* This function performs substitutions based on the rule:
1224 for a constraint of the form expr1 >= expr2; a < b =>
1225 a = b -1 for all a in expr1. This will work in most cases.
1227 Like inequalitySubstitute we're throwing away some information
1230 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
1232 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1237 constraintList_elements (p, el)
1239 DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1240 if (( el->ar == LTE) || (el->ar == LT) )
1241 /* if (!constraint_conflict (c, el) ) */ /*@i532@*/
1243 constraintExpr temp2;
1245 temp2 = constraintExpr_copy (el->expr);
1248 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1250 DPRINTF((message ("Replacing %s in %s with %s",
1251 constraintExpr_print (el->lexpr),
1252 constraintExpr_print (c->lexpr),
1253 constraintExpr_print (temp2) ) ));
1255 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
1256 constraintExpr_free(temp2);
1259 end_constraintList_elements;
1261 c = constraint_simplify(c);
1265 /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
1269 ret = constraint_copy(c);
1270 constraintList_elements (p, el)
1273 if (!constraint_conflict (ret, el) )
1278 temp = constraint_copy(el);
1280 temp = constraint_adjust(temp, ret);
1282 DPRINTF((message ("Substituting %s in the constraint %s",
1283 constraint_print (temp), constraint_print (ret)
1287 ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
1288 DPRINTF(( message ("The new constraint is %s", constraint_print (ret) ) ));
1289 constraint_free(temp);
1292 end_constraintList_elements;
1293 DPRINTF(( message ("The finial new constraint is %s", constraint_print (ret) ) ));
1295 ret = constraint_simplify(ret);
1300 /*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
1304 ret = constraintList_substitute (target, subList);
1306 constraintList_free(target);
1311 /* we try to do substitutions on each constraint in target using the constraint in sublist*/
1313 /*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
1318 ret = constraintList_makeNew();
1320 constraintList_elements(target, el)
1323 /* drl possible problem : warning make sure that a side effect is not expected */
1325 temp = constraint_substitute(el, subList);
1326 ret = constraintList_add (ret, temp);
1328 end_constraintList_elements;
1333 static constraint constraint_solve (/*@returned@*/ constraint c)
1335 DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) );
1336 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
1337 DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) );
1342 static arithType flipAr (arithType ar)
1357 llcontbug (message("unexpected value: case not handled"));
1362 static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
1364 constraintExpr temp;
1365 c->ar = flipAr (c->ar);
1369 DPRINTF(("Swaped left and right sides of constraint"));
1375 constraint constraint_simplify ( /*@returned@*/ constraint c)
1378 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
1380 if (constraint_tooDeep(c))
1382 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
1387 c->lexpr = constraintExpr_simplify (c->lexpr);
1388 c->expr = constraintExpr_simplify (c->expr);
1390 if (constraintExpr_isBinaryExpr (c->lexpr) )
1392 c = constraint_solve (c);
1394 c->lexpr = constraintExpr_simplify (c->lexpr);
1395 c->expr = constraintExpr_simplify (c->expr);
1398 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
1400 c = constraint_swapLeftRight(c);
1401 /*I don't think this will be an infinate loop*/
1402 c = constraint_simplify(c);
1405 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1413 /* returns true if fileloc for term1 is closer to file for term2 than term3*/
1415 bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1418 if (!fileloc_isDefined (loc1) )
1421 if (!fileloc_isDefined (loc2) )
1424 if (!fileloc_isDefined (loc3) )
1430 if (fileloc_equal (loc2, loc3) )
1433 if (fileloc_equal (loc1, loc2) )
1436 if (fileloc_equal (loc1, loc3) )
1439 if ( fileloc_lessthan (loc1, loc2) )
1441 if (fileloc_lessthan (loc2, loc3) )
1443 llassert (fileloc_lessthan (loc1, loc3) );
1452 if ( !fileloc_lessthan (loc1, loc2) )
1454 if (!fileloc_lessthan (loc2, loc3) )
1456 llassert (!fileloc_lessthan (loc1, loc3) );