2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 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 splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
27 ** constraintResolve.c
30 /* #define DEBUGPRINT 1 */
32 # include <ctype.h> /* for isdigit */
33 # include "splintMacros.nf"
35 # include "cgrammar.h"
36 # include "cgrammar_tokens.h"
38 # include "exprChecks.h"
39 # include "exprNodeSList.h"
42 /*@access constraint, exprNode @*/ /*!!! NO! Don't do this so recklessly - design your code more carefully so you don't need to! */
44 static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
47 static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
49 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
51 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
53 static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
56 static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
58 static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
60 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
63 /*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
67 ret = constraintList_mergeEnsures (list1, list2);
69 constraintList_free(list1);
73 /*@only@*/ constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2)
78 llassert(constraintList_isDefined(list1) );
79 llassert(constraintList_isDefined(list2) );
81 DPRINTF(( message ("constraintList_mergeEnsures: list1 %s list2 %s",
82 constraintList_print(list1), constraintList_print(list2)
85 ret = constraintList_fixConflicts (list1, list2);
86 ret = reflectChangesEnsuresFree1 (ret, list2);
87 temp = constraintList_subsumeEnsures (ret, list2);
88 constraintList_free(ret);
91 temp = constraintList_subsumeEnsures (list2, ret);
93 temp = constraintList_addList (temp, ret);
94 constraintList_free(ret);
96 DPRINTF(( message ("constraintList_mergeEnsures: returning %s ",
97 constraintList_print(temp) )
105 /*@only@*/ constraintList constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1, constraintList list2)
109 ret = constraintList_mergeRequires(list1, list2);
111 constraintList_free(list1);
116 /*@only@*/ constraintList constraintList_mergeRequires (constraintList list1, constraintList list2)
121 DPRINTF((message ("constraintList_mergeRequires: merging %s and %s ", constraintList_print (list1), constraintList_print(list2) ) ) );
123 if (context_getFlag (FLG_REDUNDANTCONSTRAINTS) )
125 ret = constraintList_copy(list1);
126 ret = constraintList_addList(ret, list2);
130 /* get constraints in list1 not satified by list2 */
131 temp = constraintList_reflectChanges(list1, list2);
132 DPRINTF((message ("constraintList_mergeRequires: temp = %s", constraintList_print(temp) ) ) );
134 /*get constraints in list2 not satified by temp*/
135 ret = constraintList_reflectChanges(list2, temp);
137 DPRINTF((message ("constraintList_mergeRequires: ret = %s", constraintList_print(ret) ) ) );
139 ret = constraintList_addListFree (ret, temp);
141 DPRINTF((message ("constraintList_mergeRequires: returning %s", constraintList_print(ret) ) ) );
146 /* old name mergeResolve renamed for czech naming convention */
147 void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
149 constraintList temp, temp2;
151 DPRINTF((message ("magically merging constraint into parent:%s for", exprNode_unparse (parent) )) );
153 DPRINTF((message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2) ) ) );
156 if (exprNode_isUndefined(parent) )
158 llassert (exprNode_isDefined(parent) );
163 if (exprNode_isError (child1) || exprNode_isError(child2) )
165 if (exprNode_isError (child1) && !exprNode_isError(child2) )
167 constraintList_free(parent->requiresConstraints);
169 parent->requiresConstraints = constraintList_copy (child2->requiresConstraints);
170 constraintList_free(parent->ensuresConstraints);
172 parent->ensuresConstraints = constraintList_copy (child2->ensuresConstraints);
173 DPRINTF((message ("Copied child constraints: pre: %s and post: %s",
174 constraintList_print( child2->requiresConstraints),
175 constraintList_print (child2->ensuresConstraints)
182 llassert(exprNode_isError(child2) );
187 llassert(!exprNode_isError (child1) && ! exprNode_isError(child2) );
189 DPRINTF((message ("Child constraints are %s %s and %s %s",
190 constraintList_print (child1->requiresConstraints),
191 constraintList_print (child1->ensuresConstraints),
192 constraintList_print (child2->requiresConstraints),
193 constraintList_print (child2->ensuresConstraints)
197 constraintList_free(parent->requiresConstraints);
199 parent->requiresConstraints = constraintList_copy (child1->requiresConstraints);
201 if ( context_getFlag (FLG_ORCONSTRAINT) )
202 temp = constraintList_reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
204 temp = constraintList_reflectChanges(child2->requiresConstraints, child1->ensuresConstraints);
206 temp2 = constraintList_mergeRequires (parent->requiresConstraints, temp);
207 constraintList_free(parent->requiresConstraints);
208 constraintList_free(temp);
210 parent->requiresConstraints = temp2;
212 DPRINTF((message ("Parent requires constraints are %s ",
213 constraintList_print (parent->requiresConstraints)
216 constraintList_free(parent->ensuresConstraints);
218 parent->ensuresConstraints = constraintList_mergeEnsures(child1->ensuresConstraints,
219 child2->ensuresConstraints);
222 DPRINTF((message ("Parent constraints are %s and %s ",
223 constraintList_print (parent->requiresConstraints),
224 constraintList_print (parent->ensuresConstraints)
232 /*@only@*/ constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2)
235 ret = constraintList_makeNew();
236 constraintList_elements (list1, el)
239 DPRINTF ((message ("Examining %s", constraint_print (el) ) ) );
240 if (!constraintList_resolve (el, list2) )
243 temp = constraint_copy(el);
244 ret = constraintList_add (ret, temp);
248 DPRINTF ((message ("Subsuming %s", constraint_print (el) ) ) );
250 } end_constraintList_elements;
257 /*used to be reflectChangesFreePre renamed for Czech naming conventino*/
258 /* tries to resolve constraints in list pre2 using post1 */
259 /*@only@*/ constraintList constraintList_reflectChangesFreePre (/*@only@*/ constraintList pre2, /*@observer@*/ constraintList post1)
263 ret = constraintList_reflectChanges(pre2, post1);
265 constraintList_free (pre2);
272 /* tries to resolve constraints in list pre2 using post1 */
274 static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
281 llassert (! context_getFlag (FLG_ORCONSTRAINT) );
283 ret = constraintList_makeNew();
284 DPRINTF((message ("reflectChanges: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
286 constraintList_elements (pre2, el)
288 if (!constraintList_resolve (el, post1) )
290 temp = constraint_substitute (el, post1);
291 if (!constraintList_resolve (temp, post1) )
293 /* try inequality substitution
294 the inequality substitution may cause us to lose information
295 so we don't want to store the result but we do it anyway
297 temp2 = constraint_copy (temp);
298 temp2 = inequalitySubstitute (temp2, post1);
299 if (!constraintList_resolve (temp2, post1) )
301 temp2 = inequalitySubstituteUnsound (temp2, post1);
302 if (!constraintList_resolve (temp2, post1) )
303 ret = constraintList_add (ret, temp2);
305 constraint_free(temp2);
309 constraint_free(temp2);
312 constraint_free(temp);
314 } end_constraintList_elements;
316 DPRINTF((message ("reflectChanges: returning %s", constraintList_print(ret) ) ) );
320 /* tries to resolve constraints in list pre2 using post1 */
321 /*@only@*/ constraintList constraintList_reflectChanges(/*@observer@*/ constraintList pre2, /*@observer@*/ constraintList post1)
325 if ( context_getFlag (FLG_ORCONSTRAINT) )
327 temp = constraintList_reflectChangesOr (pre2, post1);
329 temp = reflectChangesNoOr(pre2, post1);
334 static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
338 llassert(constraint_isDefined(orig) );
342 DPRINTF((message("constraint_addor: oring %s onto %s", constraint_printOr(orConstr), constraint_printOr(orig) ) ));
344 while (c->or != NULL)
349 c->or = constraint_copy(orConstr);
351 DPRINTF((message("constraint_addor: returning %s",constraint_printOr(orig) ) ));
357 static bool resolveOr ( /*@temp@*/ constraint c, /*@observer@*/ /*@temp@*/ constraintList list)
365 llassert(constraint_isDefined(c) );
367 DPRINTF(( message("resolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(list) ) ));
373 if (constraintList_resolve (temp, list) )
377 llassert(numberOr <= 10);
379 while (constraint_isDefined(temp));
384 /*This is a "helper" function for doResolveOr */
386 static /*@only@*/ constraint doResolve (/*@only@*/ constraint c, constraintList post1, bool * resolved)
390 llassert(constraint_isDefined (c ) );
392 DPRINTF((message("doResolve:: call on constraint c = : %q and constraintList %q",
393 constraint_printOr(c), constraintList_print(post1)
397 if (!resolveOr (c, post1) )
400 temp = constraint_substitute (c, post1);
402 DPRINTF((message("doResolve:: after substitute temp is %q",
403 constraint_printOr(temp)
407 if (!resolveOr (temp, post1) )
409 /* try inequality substitution */
412 /* the inequality substitution may cause us to lose information
413 so we don't want to store the result but we do anyway
415 temp2 = constraint_copy (c);
416 temp2 = inequalitySubstitute (temp2, post1);
418 if (!resolveOr (temp2, post1) )
421 temp3 = constraint_copy(temp2);
423 temp3 = inequalitySubstituteStrong (temp3, post1);
424 if (!resolveOr (temp3, post1) )
426 temp2 = inequalitySubstituteUnsound (temp2, post1);
427 if (!resolveOr (temp2, post1) )
429 if (!constraint_same (temp, temp2) )
431 /* drl added 8/28/2002*/
432 /*make sure that the information from
433 a post condition like i = i + 1 is transfered
436 tempSub = constraint_substitute (temp2, post1);
439 message("doResolve: adding %s ",
440 constraint_printOr(tempSub)
445 message("doResolve: not adding %s ",
446 constraint_printOr(temp2)
450 temp = constraint_addOr (temp, tempSub);
451 constraint_free(tempSub);
454 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
456 /* drl added 8/28/2002*/
457 /*make sure that the information from
458 a post condition like i = i + 1 is transfered
462 tempSub = constraint_substitute (temp3, post1);
465 message("doResolve: adding %s ",
466 constraint_printOr(tempSub)
472 message("doResolve: not adding %s ",
473 constraint_printOr(temp3)
477 temp = constraint_addOr (temp, tempSub);
479 constraint_free(tempSub);
483 constraint_free(temp2);
484 constraint_free(temp3);
489 constraint_free(temp2);
490 constraint_free(temp3);
494 constraint_free(temp2);
495 constraint_free(temp3);
500 constraint_free(temp2);
504 constraint_free(temp);
512 static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
519 DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
525 llassert(constraint_isDefined(c) );
527 ret = constraint_copy(c);
529 llassert(constraint_isDefined(ret) );
531 if (constraintList_isEmpty(post1) )
539 ret = doResolve (ret, post1, resolved);
544 constraint_free(next);
546 /*we don't need to free ret when resolved is false because ret is null*/
547 llassert(ret == NULL);
558 curr = doResolve (curr, post1, resolved);
562 /* curr is null so we don't try to free it*/
563 llassert(curr == NULL);
566 constraint_free(next);
568 constraint_free(ret);
571 ret = constraint_addOr (ret, curr);
572 constraint_free(curr);
577 /* tries to resolve constraints in list pr2 using post1 */
578 /*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
583 ret = constraintList_makeNew();
584 DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
586 constraintList_elements (pre2, el)
588 temp = doResolveOr (el, post1, &resolved);
592 ret = constraintList_add(ret, temp);
596 /* we don't need to free temp when
597 resolved is false because temp is null */
598 llassert(temp == NULL);
601 } end_constraintList_elements;
603 DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
607 static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
611 ret = constraintList_makeNew();
612 constraintList_elements (pre2, el)
614 if (!constraintList_resolve (el, post1) )
616 temp = constraint_substitute (el, post1);
617 llassert (temp != NULL);
619 if (!constraintList_resolve (temp, post1) )
620 ret = constraintList_add (ret, temp);
622 constraint_free(temp);
626 DPRINTF ((message ("Resolved away %s ", constraint_print(el) ) ) );
628 } end_constraintList_elements;
634 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
638 ret = reflectChangesEnsures (pre2, post1);
640 constraintList_free(pre2);
646 static bool constraint_conflict (constraint c1, constraint c2)
649 llassert(constraint_isDefined(c1) );
650 llassert(constraint_isDefined(c2) );
652 if (constraintExpr_similar(c1->lexpr, c2->lexpr) )
655 if (c1->ar == c2->ar)
657 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
662 /* This is a slight kludg to prevent circular constraints like
663 strlen(str) == maxRead(s) + strlen(str);
666 /*this code is functional but it may be worth cleaning up at some point. */
669 if (c1->ar == c2->ar)
671 if (constraintExpr_search (c1->lexpr, c2->expr) )
672 if (constraintExpr_isTerm(c1->lexpr) )
676 term = constraintExpr_getTerm(c1->lexpr);
678 if (constraintTerm_isExprNode(term) )
680 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
686 if (constraint_tooDeep(c1) || constraint_tooDeep(c2) )
688 DPRINTF ((message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) );
692 DPRINTF ((message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
698 static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
700 llassert(constraint_isDefined(conflicting) );
702 if (conflicting->ar ==EQ )
704 llassert(constraint_isDefined(good) );
705 good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
706 good = constraint_simplify (good);
712 static bool conflict (constraint c, constraintList list)
715 constraintList_elements (list, el)
717 if ( constraint_conflict(el, c) )
719 constraint_fixConflict (el, c);
722 } end_constraintList_elements;
729 check if constraint in list1 conflicts with constraints in List2. If so we
730 remove form list1 and change list2.
733 constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
736 ret = constraintList_makeNew();
737 llassert(constraintList_isDefined(list1) );
738 constraintList_elements (list1, el)
740 if (! conflict (el, list2) )
743 temp = constraint_copy(el);
744 ret = constraintList_add (ret, temp);
746 } end_constraintList_elements;
751 /*returns true if constraint post satifies cosntriant pre */
752 static bool satifies (constraint pre, constraint post)
754 llassert(constraint_isDefined(pre) );
755 llassert(constraint_isDefined(post) );
757 if (constraint_isAlwaysTrue (pre) )
760 if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
764 if (constraintExpr_isUndefined(post->expr))
770 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
774 bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p)
776 constraintList_elements (p, el)
778 if ( satifies (c, el) )
780 DPRINTF ((message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
783 DPRINTF ((message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) );
785 end_constraintList_elements;
786 DPRINTF ((message ("no constraints satify %s", constraint_print(c) ) ));
790 static bool arithType_canResolve (arithType ar1, arithType ar2)
796 if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ) )
809 if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ) )
818 /*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
819 static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
825 llassert(constraintExpr_isDefined(buf1) && constraintExpr_isDefined(expr2) );
827 /*@access constraintExpr@*/
829 if ((expr2->kind != term) && (buf1->kind != term) )
833 ct = constraintExprData_termGetTerm(expr2->data);
835 if (!constraintTerm_isExprNode(ct) )
838 e = constraintTerm_getExprNode(ct);
840 llassert(exprNode_isDefined(e) );
842 if (! (exprNode_isDefined(e) ) )
845 if (e->kind != XPR_SIZEOF)
848 t = exprData_getSingle (e->edata);
849 s1 = exprNode_getSref (t);
851 s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) );
853 /*drl this may be the wronge thing to test for but this
854 seems to work correctly*/
855 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
857 /* origly checked that ctype_isFixedArray(sRef_getType(s2)) but
864 /* look for the special case of
865 maxSet(buf) >= sizeof(buf) - 1
868 /*drl eventually it would be good to check that
869 buf is of type char.*/
871 static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
873 constraintExpr l, r, buf1, buf2, con;
875 DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
878 llassert (constraint_isDefined(c) );
883 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
886 llassert (constraintExpr_isDefined(l) );
887 llassert (constraintExpr_isDefined(r) );
889 /*check if the constraintExpr is MaxSet(buf) */
890 if (l->kind == unaryExpr)
892 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
894 buf1 = constraintExprData_unaryExprGetExpr(l->data);
903 if (r->kind != binaryexpr)
906 buf2 = constraintExprData_binaryExprGetExpr1(r->data);
907 con = constraintExprData_binaryExprGetExpr2(r->data);
909 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS)
911 if (constraintExpr_canGetValue(con) )
915 i = constraintExpr_getValue(con);
925 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS)
927 if (constraintExpr_canGetValue(con) )
931 i = constraintExpr_getValue(con);
941 if (sizeofBufComp(buf1, buf2))
950 /*@noaccess constraintExpr@*/
952 /* We look for constraint which are tautologies */
954 bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
961 llassert (constraint_isDefined(c) );
966 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
968 if (sizeOfMaxSet(c) )
971 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
974 cmp = constraintExpr_compare (l, r);
995 if (constraintExpr_similar (l,r) )
1014 l = constraintExpr_copy (c->lexpr);
1015 r = constraintExpr_copy (c->expr);
1017 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
1019 if (constraintExpr_similar (l,r) && (rHasConstant ) )
1021 DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
1022 DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
1024 constraintExpr_free(l);
1025 constraintExpr_free(r);
1030 return (rConstant == 0);
1032 return (rConstant > 0);
1034 return (rConstant >= 0);
1036 return (rConstant <= 0);
1038 return (rConstant < 0);
1048 constraintExpr_free(l);
1049 constraintExpr_free(r);
1050 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
1057 static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
1060 DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
1062 if (! arithType_canResolve (ar1, ar2) )
1068 if (constraintExpr_similar (expr1, expr2) )
1072 if (! (constraintExpr_canGetValue (expr1) &&
1073 constraintExpr_canGetValue (expr2) ) )
1075 constraintExpr e1, e2;
1079 e1 = constraintExpr_copy(expr1);
1080 e2 = constraintExpr_copy(expr2);
1082 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1084 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1094 if (const1 <= const2)
1095 if (constraintExpr_similar (e1, e2) )
1097 constraintExpr_free(e1);
1098 constraintExpr_free(e2);
1102 DPRINTF(("Can't Get value"));
1104 constraintExpr_free(e1);
1105 constraintExpr_free(e2);
1109 if (constraintExpr_compare (expr2, expr1) >= 0)
1114 if (constraintExpr_similar (expr1, expr2) )
1119 if (constraintExpr_similar (expr1, expr2) )
1123 if (! (constraintExpr_canGetValue (expr1) &&
1124 constraintExpr_canGetValue (expr2) ) )
1126 constraintExpr e1, e2;
1130 e1 = constraintExpr_copy(expr1);
1131 e2 = constraintExpr_copy(expr2);
1133 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1135 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1145 if (const1 >= const2)
1146 if (constraintExpr_similar (e1, e2) )
1148 constraintExpr_free(e1);
1149 constraintExpr_free(e2);
1153 constraintExpr_free(e1);
1154 constraintExpr_free(e2);
1156 DPRINTF(("Can't Get value"));
1160 if (constraintExpr_compare (expr2, expr1) <= 0)
1166 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
1171 static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
1173 llassert (constraint_isDefined(c) );
1175 DPRINTF (("Doing replace for lexpr") );
1177 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
1178 DPRINTF (("Doing replace for expr") );
1179 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
1183 bool constraint_search (constraint c, constraintExpr old) /*@*/
1188 llassert (constraint_isDefined(c) );
1190 ret = constraintExpr_search (c->lexpr, old);
1191 ret = ret || constraintExpr_search (c->expr, old);
1195 /* adjust file locs and stuff */
1196 static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
1198 fileloc loc1, loc2, loc3;
1200 DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute),
1201 constraint_print(old))
1204 llassert(constraint_isDefined(substitute));
1205 llassert(constraint_isDefined(old));
1207 loc1 = constraint_getFileloc (old);
1208 loc2 = constraintExpr_getFileloc (substitute->lexpr);
1209 loc3 = constraintExpr_getFileloc (substitute->expr);
1211 /* special case of an equality that "contains itself" */
1212 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
1213 if (fileloc_closer (loc1, loc3, loc2))
1215 constraintExpr temp;
1216 DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) )
1218 temp = substitute->lexpr;
1219 substitute->lexpr = substitute->expr;
1220 substitute->expr = temp;
1221 substitute = constraint_simplify(substitute);
1224 fileloc_free (loc1);
1225 fileloc_free (loc2);
1226 fileloc_free (loc3);
1232 /* If function preforms substitutes based on inequality
1234 It uses the rule x >= y && b < y ===> x >= b + 1
1236 Warning this is sound but throws out information
1239 constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
1241 llassert(constraint_isDefined(c) );
1246 constraintList_elements (p, el)
1249 llassert(constraint_isDefined(el) );
1251 if ((el->ar == LT ) )
1253 constraintExpr temp2;
1255 if (constraintExpr_same (el->expr, c->expr) )
1257 DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
1258 constraintExpr_print (c->expr),
1259 constraint_print (c),
1260 constraintExpr_print (el->expr) )
1262 temp2 = constraintExpr_copy (el->lexpr);
1263 constraintExpr_free(c->expr);
1264 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1270 end_constraintList_elements;
1272 c = constraint_simplify(c);
1279 THis function is like inequalitySubstitute but it adds the rule
1280 added the rules x >= y && y <= b ===> x >= b
1281 x >= y && y < b ===> x >= b + 1
1283 This is sound but sonce it throws out additional information it should only one used
1284 if we're oring constraints.
1287 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1289 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
1291 llassert(constraint_isDefined(c) );
1293 if (! (constraint_isDefined(c) ) )
1301 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1302 constraint_print(c), constraintList_print(p) ) ));
1303 constraintList_elements (p, el)
1306 DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1308 llassert(constraint_isDefined(el) );
1309 if ((el->ar == LT ) || (el->ar == LTE ) )
1311 constraintExpr temp2;
1313 if (constraintExpr_same (el->lexpr, c->expr) )
1315 DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
1316 constraintExpr_print (c->expr),
1317 constraint_print (c),
1318 constraintExpr_print (el->expr) )
1320 temp2 = constraintExpr_copy (el->expr);
1321 constraintExpr_free(c->expr);
1322 if ((el->ar == LTE ) )
1328 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1334 end_constraintList_elements;
1336 c = constraint_simplify(c);
1341 /* This function performs substitutions based on the rule:
1342 for a constraint of the form expr1 >= expr2; a < b =>
1343 a = b -1 for all a in expr1. This will work in most cases.
1345 Like inequalitySubstitute we're throwing away some information
1348 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
1350 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1352 llassert(constraint_isDefined(c) );
1357 constraintList_elements (p, el)
1360 llassert(constraint_isDefined(el) );
1362 DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1363 if (( el->ar == LTE) || (el->ar == LT) )
1365 constraintExpr temp2;
1367 temp2 = constraintExpr_copy (el->expr);
1370 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1372 DPRINTF((message ("Replacing %s in %s with %s",
1373 constraintExpr_print (el->lexpr),
1374 constraintExpr_print (c->lexpr),
1375 constraintExpr_print (temp2) ) ));
1377 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
1378 constraintExpr_free(temp2);
1381 end_constraintList_elements;
1383 c = constraint_simplify(c);
1387 /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
1391 ret = constraint_copy(c);
1392 constraintList_elements (p, el)
1394 llassert(constraint_isDefined(el) );
1396 if (!constraint_conflict (ret, el) )
1401 temp = constraint_copy(el);
1403 temp = constraint_adjust(temp, ret);
1405 llassert(constraint_isDefined(temp) );
1408 DPRINTF((message ("constraint_substitute :: Substituting in %s using %s",
1409 constraint_print (ret), constraint_print (temp)
1413 ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
1414 DPRINTF(( message (" constraint_substitute :: The new constraint is %s", constraint_print (ret) ) ));
1415 constraint_free(temp);
1418 end_constraintList_elements;
1420 ret = constraint_simplify(ret);
1422 DPRINTF(( message (" constraint_substitute :: The final new constraint is %s", constraint_print (ret) ) ));
1428 /*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
1432 ret = constraintList_substitute (target, subList);
1434 constraintList_free(target);
1439 /* we try to do substitutions on each constraint in target using the constraint in sublist*/
1441 /*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
1446 ret = constraintList_makeNew();
1448 constraintList_elements(target, el)
1451 /* drl possible problem : warning make sure that a side effect is not expected */
1453 temp = constraint_substitute(el, subList);
1454 ret = constraintList_add (ret, temp);
1456 end_constraintList_elements;
1461 static constraint constraint_solve (/*@returned@*/ constraint c)
1464 llassert(constraint_isDefined(c) );
1466 DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) );
1467 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
1468 DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) );
1473 static arithType flipAr (arithType ar)
1488 llcontbug (message("unexpected value: case not handled"));
1493 static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
1495 constraintExpr temp;
1497 llassert(constraint_isDefined(c) );
1499 c->ar = flipAr (c->ar);
1503 DPRINTF(("Swaped left and right sides of constraint"));
1509 constraint constraint_simplify ( /*@returned@*/ constraint c)
1512 llassert(constraint_isDefined(c) );
1514 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
1516 if (constraint_tooDeep(c))
1518 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
1523 c->lexpr = constraintExpr_simplify (c->lexpr);
1524 c->expr = constraintExpr_simplify (c->expr);
1526 if (constraintExpr_isBinaryExpr (c->lexpr) )
1528 c = constraint_solve (c);
1530 c->lexpr = constraintExpr_simplify (c->lexpr);
1531 c->expr = constraintExpr_simplify (c->expr);
1534 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
1536 c = constraint_swapLeftRight(c);
1537 /*I don't think this will be an infinate loop*/
1538 c = constraint_simplify(c);
1541 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1549 /* returns true if fileloc for term1 is closer to file for term2 than term3*/
1551 bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1554 if (!fileloc_isDefined (loc1) )
1557 if (!fileloc_isDefined (loc2) )
1560 if (!fileloc_isDefined (loc3) )
1566 if (fileloc_equal (loc2, loc3) )
1569 if (fileloc_equal (loc1, loc2) )
1572 if (fileloc_equal (loc1, loc3) )
1575 if ( fileloc_lessthan (loc1, loc2) )
1577 if (fileloc_lessthan (loc2, loc3) )
1579 llassert (fileloc_lessthan (loc1, loc3) );
1588 if ( !fileloc_lessthan (loc1, loc2) )
1590 if (!fileloc_lessthan (loc2, loc3) )
1592 llassert (!fileloc_lessthan (loc1, loc3) );