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);
508 /*drl bee: pbr*/ *resolved = TRUE;
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) ) ));
523 /*drl bee: pbr*/ *resolved = FALSE;
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);
560 /*drl bee: pbr*/ if (*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 /*@i324234*/ /* clean this up */
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 /*@i223@*/ /*this may be the wronge thing to test for */
854 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
856 /*@i22*/ /* get rid of this test of now */
857 /* if (ctype_isFixedArray (sRef_getType (s2) ) ) */
863 /* look for the special case of
864 maxSet(buf) >= sizeof(buf) - 1
867 /*@i223@*/ /*need to add some type checking */
868 static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
870 constraintExpr l, r, buf1, buf2, con;
872 DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
875 llassert (constraint_isDefined(c) );
880 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
883 llassert (constraintExpr_isDefined(l) );
884 llassert (constraintExpr_isDefined(r) );
886 /*check if the constraintExpr is MaxSet(buf) */
887 if (l->kind == unaryExpr)
889 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
891 buf1 = constraintExprData_unaryExprGetExpr(l->data);
900 if (r->kind != binaryexpr)
903 buf2 = constraintExprData_binaryExprGetExpr1(r->data);
904 con = constraintExprData_binaryExprGetExpr2(r->data);
906 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS)
908 if (constraintExpr_canGetValue(con) )
912 i = constraintExpr_getValue(con);
922 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS)
924 if (constraintExpr_canGetValue(con) )
928 i = constraintExpr_getValue(con);
938 if (sizeofBufComp(buf1, buf2))
949 /*@noaccess constraintExpr@*/
951 /* We look for constraint which are tautologies */
953 bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
960 llassert (constraint_isDefined(c) );
965 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
967 if (sizeOfMaxSet(c) )
970 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
973 cmp = constraintExpr_compare (l, r);
994 if (constraintExpr_similar (l,r) )
1013 l = constraintExpr_copy (c->lexpr);
1014 r = constraintExpr_copy (c->expr);
1016 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
1018 if (constraintExpr_similar (l,r) && (rHasConstant ) )
1020 DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
1021 DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
1023 constraintExpr_free(l);
1024 constraintExpr_free(r);
1029 return (rConstant == 0);
1031 return (rConstant > 0);
1033 return (rConstant >= 0);
1035 return (rConstant <= 0);
1037 return (rConstant < 0);
1047 constraintExpr_free(l);
1048 constraintExpr_free(r);
1049 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
1056 static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
1059 DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
1061 if (! arithType_canResolve (ar1, ar2) )
1067 if (constraintExpr_similar (expr1, expr2) )
1071 if (! (constraintExpr_canGetValue (expr1) &&
1072 constraintExpr_canGetValue (expr2) ) )
1074 constraintExpr e1, e2;
1078 e1 = constraintExpr_copy(expr1);
1079 e2 = constraintExpr_copy(expr2);
1081 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1083 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1093 if (const1 <= const2)
1094 if (constraintExpr_similar (e1, e2) )
1096 constraintExpr_free(e1);
1097 constraintExpr_free(e2);
1101 DPRINTF(("Can't Get value"));
1103 constraintExpr_free(e1);
1104 constraintExpr_free(e2);
1108 if (constraintExpr_compare (expr2, expr1) >= 0)
1113 if (constraintExpr_similar (expr1, expr2) )
1118 if (constraintExpr_similar (expr1, expr2) )
1122 if (! (constraintExpr_canGetValue (expr1) &&
1123 constraintExpr_canGetValue (expr2) ) )
1125 constraintExpr e1, e2;
1129 e1 = constraintExpr_copy(expr1);
1130 e2 = constraintExpr_copy(expr2);
1132 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1134 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1144 if (const1 >= const2)
1145 if (constraintExpr_similar (e1, e2) )
1147 constraintExpr_free(e1);
1148 constraintExpr_free(e2);
1152 constraintExpr_free(e1);
1153 constraintExpr_free(e2);
1155 DPRINTF(("Can't Get value"));
1159 if (constraintExpr_compare (expr2, expr1) <= 0)
1165 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
1170 static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
1172 llassert (constraint_isDefined(c) );
1174 DPRINTF (("Doing replace for lexpr") );
1176 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
1177 DPRINTF (("Doing replace for expr") );
1178 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
1182 bool constraint_search (constraint c, constraintExpr old) /*@*/
1187 llassert (constraint_isDefined(c) );
1189 ret = constraintExpr_search (c->lexpr, old);
1190 ret = ret || constraintExpr_search (c->expr, old);
1194 /* adjust file locs and stuff */
1195 static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
1197 fileloc loc1, loc2, loc3;
1199 DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute),
1200 constraint_print(old))
1203 llassert(constraint_isDefined(substitute));
1204 llassert(constraint_isDefined(old));
1206 loc1 = constraint_getFileloc (old);
1207 loc2 = constraintExpr_getFileloc (substitute->lexpr);
1208 loc3 = constraintExpr_getFileloc (substitute->expr);
1210 /* special case of an equality that "contains itself" */
1211 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
1212 if (fileloc_closer (loc1, loc3, loc2))
1214 constraintExpr temp;
1215 DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) )
1217 temp = substitute->lexpr;
1218 substitute->lexpr = substitute->expr;
1219 substitute->expr = temp;
1220 substitute = constraint_simplify(substitute);
1223 fileloc_free (loc1);
1224 fileloc_free (loc2);
1225 fileloc_free (loc3);
1231 /* If function preforms substitutes based on inequality
1233 It uses the rule x >= y && b < y ===> x >= b + 1
1235 Warning this is sound but throws out information
1238 constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
1240 llassert(constraint_isDefined(c) );
1245 constraintList_elements (p, el)
1248 llassert(constraint_isDefined(el) );
1250 if ((el->ar == LT ) )
1251 /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */
1253 constraintExpr temp2;
1257 if (constraintExpr_same (el->expr, c->expr) )
1259 DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
1260 constraintExpr_print (c->expr),
1261 constraint_print (c),
1262 constraintExpr_print (el->expr) )
1264 temp2 = constraintExpr_copy (el->lexpr);
1265 constraintExpr_free(c->expr);
1266 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1272 end_constraintList_elements;
1274 c = constraint_simplify(c);
1281 THis function is like inequalitySubstitute but it adds the rule
1282 added the rules x >= y && y <= b ===> x >= b
1283 x >= y && y < b ===> x >= b + 1
1285 This is sound but sonce it throws out additional information it should only one used
1286 if we're oring constraints.
1289 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1291 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
1293 llassert(constraint_isDefined(c) );
1295 if (! (constraint_isDefined(c) ) )
1303 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1304 constraint_print(c), constraintList_print(p) ) ));
1305 constraintList_elements (p, el)
1308 DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1310 llassert(constraint_isDefined(el) );
1311 if ((el->ar == LT ) || (el->ar == LTE ) )
1312 /* if (!constraint_conflict (c, el) ) */ /*@i523@*/
1314 constraintExpr temp2;
1318 if (constraintExpr_same (el->lexpr, c->expr) )
1320 DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
1321 constraintExpr_print (c->expr),
1322 constraint_print (c),
1323 constraintExpr_print (el->expr) )
1325 temp2 = constraintExpr_copy (el->expr);
1326 constraintExpr_free(c->expr);
1327 if ((el->ar == LTE ) )
1333 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1339 end_constraintList_elements;
1341 c = constraint_simplify(c);
1346 /* This function performs substitutions based on the rule:
1347 for a constraint of the form expr1 >= expr2; a < b =>
1348 a = b -1 for all a in expr1. This will work in most cases.
1350 Like inequalitySubstitute we're throwing away some information
1353 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
1355 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1357 llassert(constraint_isDefined(c) );
1362 constraintList_elements (p, el)
1365 llassert(constraint_isDefined(el) );
1367 DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1368 if (( el->ar == LTE) || (el->ar == LT) )
1369 /* if (!constraint_conflict (c, el) ) */ /*@i532@*/
1371 constraintExpr temp2;
1373 temp2 = constraintExpr_copy (el->expr);
1376 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1378 DPRINTF((message ("Replacing %s in %s with %s",
1379 constraintExpr_print (el->lexpr),
1380 constraintExpr_print (c->lexpr),
1381 constraintExpr_print (temp2) ) ));
1383 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
1384 constraintExpr_free(temp2);
1387 end_constraintList_elements;
1389 c = constraint_simplify(c);
1393 /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
1397 ret = constraint_copy(c);
1398 constraintList_elements (p, el)
1400 llassert(constraint_isDefined(el) );
1402 if (!constraint_conflict (ret, el) )
1407 temp = constraint_copy(el);
1409 temp = constraint_adjust(temp, ret);
1411 llassert(constraint_isDefined(temp) );
1414 DPRINTF((message ("constraint_substitute :: Substituting in %s using %s",
1415 constraint_print (ret), constraint_print (temp)
1419 ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
1420 DPRINTF(( message (" constraint_substitute :: The new constraint is %s", constraint_print (ret) ) ));
1421 constraint_free(temp);
1424 end_constraintList_elements;
1426 ret = constraint_simplify(ret);
1428 DPRINTF(( message (" constraint_substitute :: The final new constraint is %s", constraint_print (ret) ) ));
1434 /*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
1438 ret = constraintList_substitute (target, subList);
1440 constraintList_free(target);
1445 /* we try to do substitutions on each constraint in target using the constraint in sublist*/
1447 /*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
1452 ret = constraintList_makeNew();
1454 constraintList_elements(target, el)
1457 /* drl possible problem : warning make sure that a side effect is not expected */
1459 temp = constraint_substitute(el, subList);
1460 ret = constraintList_add (ret, temp);
1462 end_constraintList_elements;
1467 static constraint constraint_solve (/*@returned@*/ constraint c)
1470 llassert(constraint_isDefined(c) );
1472 DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) );
1473 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
1474 DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) );
1479 static arithType flipAr (arithType ar)
1494 llcontbug (message("unexpected value: case not handled"));
1499 static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
1501 constraintExpr temp;
1503 llassert(constraint_isDefined(c) );
1505 c->ar = flipAr (c->ar);
1509 DPRINTF(("Swaped left and right sides of constraint"));
1515 constraint constraint_simplify ( /*@returned@*/ constraint c)
1518 llassert(constraint_isDefined(c) );
1520 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
1522 if (constraint_tooDeep(c))
1524 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
1529 c->lexpr = constraintExpr_simplify (c->lexpr);
1530 c->expr = constraintExpr_simplify (c->expr);
1532 if (constraintExpr_isBinaryExpr (c->lexpr) )
1534 c = constraint_solve (c);
1536 c->lexpr = constraintExpr_simplify (c->lexpr);
1537 c->expr = constraintExpr_simplify (c->expr);
1540 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
1542 c = constraint_swapLeftRight(c);
1543 /*I don't think this will be an infinate loop*/
1544 c = constraint_simplify(c);
1547 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1555 /* returns true if fileloc for term1 is closer to file for term2 than term3*/
1557 bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1560 if (!fileloc_isDefined (loc1) )
1563 if (!fileloc_isDefined (loc2) )
1566 if (!fileloc_isDefined (loc3) )
1572 if (fileloc_equal (loc2, loc3) )
1575 if (fileloc_equal (loc1, loc2) )
1578 if (fileloc_equal (loc1, loc3) )
1581 if ( fileloc_lessthan (loc1, loc2) )
1583 if (fileloc_lessthan (loc2, loc3) )
1585 llassert (fileloc_lessthan (loc1, loc3) );
1594 if ( !fileloc_lessthan (loc1, loc2) )
1596 if (!fileloc_lessthan (loc2, loc3) )
1598 llassert (!fileloc_lessthan (loc1, loc3) );