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 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 /*@-nullderef@*/ /* !!! DRL needs to fix this code! */
45 /*@-nullstate@*/ /* !!! DRL needs to fix this code! */
46 /*@-nullpass@*/ /* !!! DRL needs to fix this code! */
47 /*@-temptrans@*/ /* !!! DRL needs to fix this code! */
51 static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
54 static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
56 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
58 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
60 static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
63 static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
65 static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
67 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
69 /*********************************************/
74 /*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
78 ret = constraintList_mergeEnsures (list1, list2);
80 constraintList_free(list1);
84 /*@only@*/ constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2)
89 llassert(constraintList_isDefined(list1) );
90 llassert(constraintList_isDefined(list2) );
92 DPRINTF(( message ("constraintList_mergeEnsures: list1 %s list2 %s",
93 constraintList_print(list1), constraintList_print(list2)
96 ret = constraintList_fixConflicts (list1, list2);
97 ret = reflectChangesEnsuresFree1 (ret, list2);
98 temp = constraintList_subsumeEnsures (ret, list2);
99 constraintList_free(ret);
102 temp = constraintList_subsumeEnsures (list2, ret);
104 temp = constraintList_addList (temp, ret);
105 constraintList_free(ret);
107 DPRINTF(( message ("constraintList_mergeEnsures: returning %s ",
108 constraintList_print(temp) )
116 /*@only@*/ constraintList constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1, constraintList list2)
120 ret = constraintList_mergeRequires(list1, list2);
122 constraintList_free(list1);
127 /*@only@*/ constraintList constraintList_mergeRequires (constraintList list1, constraintList list2)
132 DPRINTF((message ("constraintList_mergeRequires: merging %s and %s ", constraintList_print (list1), constraintList_print(list2) ) ) );
134 if (context_getFlag (FLG_REDUNDANTCONSTRAINTS) )
136 ret = constraintList_copy(list1);
137 ret = constraintList_addList(ret, list2);
141 /* get constraints in list1 not satified by list2 */
142 temp = constraintList_reflectChanges(list1, list2);
143 DPRINTF((message ("constraintList_mergeRequires: temp = %s", constraintList_print(temp) ) ) );
145 /*get constraints in list2 not satified by temp*/
146 ret = constraintList_reflectChanges(list2, temp);
148 DPRINTF((message ("constraintList_mergeRequires: ret = %s", constraintList_print(ret) ) ) );
150 ret = constraintList_addListFree (ret, temp);
152 DPRINTF((message ("constraintList_mergeRequires: returning %s", constraintList_print(ret) ) ) );
157 /* old name mergeResolve renamed for czech naming convention */
158 void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
160 constraintList temp, temp2;
162 DPRINTF((message ("magically merging constraint into parent:%s for", exprNode_unparse (parent) )) );
164 DPRINTF((message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2) ) ) );
166 if (exprNode_isError (child1) || exprNode_isError(child2) )
168 if (exprNode_isError (child1) && !exprNode_isError(child2) )
170 constraintList_free(parent->requiresConstraints);
172 parent->requiresConstraints = constraintList_copy (child2->requiresConstraints);
173 constraintList_free(parent->ensuresConstraints);
175 parent->ensuresConstraints = constraintList_copy (child2->ensuresConstraints);
176 DPRINTF((message ("Copied child constraints: pre: %s and post: %s",
177 constraintList_print( child2->requiresConstraints),
178 constraintList_print (child2->ensuresConstraints)
185 llassert(exprNode_isError(child2) );
190 llassert(!exprNode_isError (child1) && ! exprNode_isError(child2) );
192 DPRINTF((message ("Child constraints are %s %s and %s %s",
193 constraintList_print (child1->requiresConstraints),
194 constraintList_print (child1->ensuresConstraints),
195 constraintList_print (child2->requiresConstraints),
196 constraintList_print (child2->ensuresConstraints)
200 constraintList_free(parent->requiresConstraints);
202 parent->requiresConstraints = constraintList_copy (child1->requiresConstraints);
204 if ( context_getFlag (FLG_ORCONSTRAINT) )
205 temp = constraintList_reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
207 temp = constraintList_reflectChanges(child2->requiresConstraints, child1->ensuresConstraints);
209 temp2 = constraintList_mergeRequires (parent->requiresConstraints, temp);
210 constraintList_free(parent->requiresConstraints);
211 constraintList_free(temp);
213 parent->requiresConstraints = temp2;
215 DPRINTF((message ("Parent requires constraints are %s ",
216 constraintList_print (parent->requiresConstraints)
219 constraintList_free(parent->ensuresConstraints);
221 parent->ensuresConstraints = constraintList_mergeEnsures(child1->ensuresConstraints,
222 child2->ensuresConstraints);
225 DPRINTF((message ("Parent constraints are %s and %s ",
226 constraintList_print (parent->requiresConstraints),
227 constraintList_print (parent->ensuresConstraints)
235 /*@only@*/ constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2)
238 ret = constraintList_makeNew();
239 constraintList_elements (list1, el)
242 DPRINTF ((message ("Examining %s", constraint_print (el) ) ) );
243 if (!constraintList_resolve (el, list2) )
246 temp = constraint_copy(el);
247 ret = constraintList_add (ret, temp);
251 DPRINTF ((message ("Subsuming %s", constraint_print (el) ) ) );
253 } end_constraintList_elements;
260 /*used to be reflectChangesFreePre renamed for Czech naming conventino*/
261 /* tries to resolve constraints in list pre2 using post1 */
262 /*@only@*/ constraintList constraintList_reflectChangesFreePre (/*@only@*/ constraintList pre2, /*@observer@*/ constraintList post1)
266 ret = constraintList_reflectChanges(pre2, post1);
268 constraintList_free (pre2);
275 /* tries to resolve constraints in list pre2 using post1 */
277 static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
284 llassert (! context_getFlag (FLG_ORCONSTRAINT) );
286 ret = constraintList_makeNew();
287 DPRINTF((message ("reflectChanges: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
289 constraintList_elements (pre2, el)
291 if (!constraintList_resolve (el, post1) )
293 temp = constraint_substitute (el, post1);
294 if (!constraintList_resolve (temp, post1) )
296 /* try inequality substitution
297 the inequality substitution may cause us to lose information
298 so we don't want to store the result but we do it anyway
300 temp2 = constraint_copy (temp);
301 temp2 = inequalitySubstitute (temp2, post1);
302 if (!constraintList_resolve (temp2, post1) )
304 temp2 = inequalitySubstituteUnsound (temp2, post1);
305 if (!constraintList_resolve (temp2, post1) )
306 ret = constraintList_add (ret, temp2);
308 constraint_free(temp2);
312 constraint_free(temp2);
315 constraint_free(temp);
317 } end_constraintList_elements;
319 DPRINTF((message ("reflectChanges: returning %s", constraintList_print(ret) ) ) );
323 /* tries to resolve constraints in list pre2 using post1 */
324 /*@only@*/ constraintList constraintList_reflectChanges(/*@observer@*/ constraintList pre2, /*@observer@*/ constraintList post1)
328 if ( context_getFlag (FLG_ORCONSTRAINT) )
330 temp = constraintList_reflectChangesOr (pre2, post1);
332 temp = reflectChangesNoOr(pre2, post1);
337 static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
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)
364 DPRINTF(( message("resolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(list) ) ));
369 if (constraintList_resolve (temp, list) )
373 llassert(numberOr <= 10);
375 while (constraint_isDefined(temp));
380 /*This is a "helper" function for doResolveOr */
382 static /*@only@*/ constraint doResolve (/*@only@*/ constraint c, constraintList post1, bool * resolved)
386 llassert(constraint_isUndefined (c->or ) );
388 DPRINTF((message("doResolve:: call on constraint c = : %q and constraintList %q",
389 constraint_printOr(c), constraintList_print(post1)
393 if (!resolveOr (c, post1) )
396 temp = constraint_substitute (c, post1);
398 DPRINTF((message("doResolve:: after substitute temp is %q",
399 constraint_printOr(temp)
403 if (!resolveOr (temp, post1) )
405 /* try inequality substitution */
408 /* the inequality substitution may cause us to lose information
409 so we don't want to store the result but we do anyway
411 temp2 = constraint_copy (c);
412 temp2 = inequalitySubstitute (temp2, post1);
414 if (!resolveOr (temp2, post1) )
417 temp3 = constraint_copy(temp2);
419 temp3 = inequalitySubstituteStrong (temp3, post1);
420 if (!resolveOr (temp3, post1) )
422 temp2 = inequalitySubstituteUnsound (temp2, post1);
423 if (!resolveOr (temp2, post1) )
425 if (!constraint_same (temp, temp2) )
427 /* drl added 8/28/2002*/
428 /*make sure that the information from
429 a post condition like i = i + 1 is transfered
432 tempSub = constraint_substitute (temp2, post1);
435 message("doResolve: adding %s ",
436 constraint_printOr(tempSub)
441 message("doResolve: not adding %s ",
442 constraint_printOr(temp2)
446 temp = constraint_addOr (temp, tempSub);
447 constraint_free(tempSub);
450 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
452 /* drl added 8/28/2002*/
453 /*make sure that the information from
454 a post condition like i = i + 1 is transfered
458 tempSub = constraint_substitute (temp3, post1);
461 message("doResolve: adding %s ",
462 constraint_printOr(tempSub)
468 message("doResolve: not adding %s ",
469 constraint_printOr(temp3)
473 temp = constraint_addOr (temp, tempSub);
475 constraint_free(tempSub);
479 constraint_free(temp2);
480 constraint_free(temp3);
485 constraint_free(temp2);
486 constraint_free(temp3);
490 constraint_free(temp2);
491 constraint_free(temp3);
496 constraint_free(temp2);
500 constraint_free(temp);
504 /*drl bee: pbr*/ *resolved = TRUE;
508 static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
515 DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
519 /*drl bee: pbr*/ *resolved = FALSE;
522 ret = constraint_copy(c);
524 if (constraintList_isEmpty(post1) )
532 ret = doResolve (ret, post1, resolved);
537 constraint_free(next);
539 /*we don't need to free ret when resolved is false because ret is null*/
540 llassert(ret == NULL);
551 curr = doResolve (curr, post1, resolved);
553 /*drl bee: pbr*/ if (*resolved)
555 /* curr is null so we don't try to free it*/
556 llassert(curr == NULL);
559 constraint_free(next);
561 constraint_free(ret);
564 ret = constraint_addOr (ret, curr);
565 constraint_free(curr);
570 /* tries to resolve constraints in list pr2 using post1 */
571 /*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
576 ret = constraintList_makeNew();
577 DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
579 constraintList_elements (pre2, el)
581 temp = doResolveOr (el, post1, &resolved);
585 ret = constraintList_add(ret, temp);
589 /* we don't need to free temp when
590 resolved is false because temp is null */
591 llassert(temp == NULL);
594 } end_constraintList_elements;
596 DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
600 static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
604 ret = constraintList_makeNew();
605 constraintList_elements (pre2, el)
607 if (!constraintList_resolve (el, post1) )
609 temp = constraint_substitute (el, post1);
610 llassert (temp != NULL);
612 if (!constraintList_resolve (temp, post1) )
613 ret = constraintList_add (ret, temp);
615 constraint_free(temp);
619 DPRINTF ((message ("Resolved away %s ", constraint_print(el) ) ) );
621 } end_constraintList_elements;
627 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
631 ret = reflectChangesEnsures (pre2, post1);
633 constraintList_free(pre2);
639 static bool constraint_conflict (constraint c1, constraint c2)
642 if (constraintExpr_similar(c1->lexpr, c2->lexpr) )
645 if (c1->ar == c2->ar)
647 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
652 /* This is a slight kludg to prevent circular constraints like
653 strlen(str) == maxRead(s) + strlen(str);
656 /*@i324234*/ /* clean this up */
659 if (c1->ar == c2->ar)
661 if (constraintExpr_search (c1->lexpr, c2->expr) )
662 if (constraintExpr_isTerm(c1->lexpr) )
666 term = constraintExpr_getTerm(c1->lexpr);
668 if (constraintTerm_isExprNode(term) )
670 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
676 if (constraint_tooDeep(c1) || constraint_tooDeep(c2) )
678 DPRINTF ((message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) );
682 DPRINTF ((message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
688 static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
690 if (conflicting->ar ==EQ )
692 good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
693 good = constraint_simplify (good);
699 static bool conflict (constraint c, constraintList list)
702 constraintList_elements (list, el)
704 if ( constraint_conflict(el, c) )
706 constraint_fixConflict (el, c);
709 } end_constraintList_elements;
716 check if constraint in list1 conflicts with constraints in List2. If so we
717 remove form list1 and change list2.
720 constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
723 ret = constraintList_makeNew();
724 llassert(constraintList_isDefined(list1) );
725 constraintList_elements (list1, el)
727 if (! conflict (el, list2) )
730 temp = constraint_copy(el);
731 ret = constraintList_add (ret, temp);
733 } end_constraintList_elements;
738 /*returns true if constraint post satifies cosntriant pre */
739 static bool satifies (constraint pre, constraint post)
741 if (constraint_isAlwaysTrue (pre) )
744 if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
748 if (constraintExpr_isUndefined(post->expr))
754 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
758 bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p)
760 constraintList_elements (p, el)
762 if ( satifies (c, el) )
764 DPRINTF ((message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
767 DPRINTF ((message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) );
769 end_constraintList_elements;
770 DPRINTF ((message ("no constraints satify %s", constraint_print(c) ) ));
774 static bool arithType_canResolve (arithType ar1, arithType ar2)
780 if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ) )
793 if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ) )
802 /*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
803 static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
808 /*@access constraintExpr@*/
810 if ((expr2->kind != term) && (buf1->kind != term) )
814 ct = constraintExprData_termGetTerm(expr2->data);
816 if (!constraintTerm_isExprNode(ct) )
819 e = constraintTerm_getExprNode(ct);
821 if (e->kind != XPR_SIZEOF)
824 t = exprData_getSingle (e->edata);
825 s1 = exprNode_getSref (t);
827 s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) );
829 /*@i223@*/ /*this may be the wronge thing to test for */
830 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
832 /*@i22*/ /* get rid of this test of now */
833 /* if (ctype_isFixedArray (sRef_getType (s2) ) ) */
839 /* look for the special case of
840 maxSet(buf) >= sizeof(buf) - 1
843 /*@i223@*/ /*need to add some type checking */
844 static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
846 constraintExpr l, r, buf1, buf2, con;
848 DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
854 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
857 /*check if the constraintExpr is MaxSet(buf) */
858 if (l->kind == unaryExpr)
860 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
862 buf1 = constraintExprData_unaryExprGetExpr(l->data);
871 if (r->kind != binaryexpr)
874 buf2 = constraintExprData_binaryExprGetExpr1(r->data);
875 con = constraintExprData_binaryExprGetExpr2(r->data);
877 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS)
879 if (constraintExpr_canGetValue(con) )
883 i = constraintExpr_getValue(con);
893 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS)
895 if (constraintExpr_canGetValue(con) )
899 i = constraintExpr_getValue(con);
909 if (sizeofBufComp(buf1, buf2))
920 /*@noaccess constraintExpr@*/
922 /* We look for constraint which are tautologies */
924 bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
933 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
935 if (sizeOfMaxSet(c) )
938 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
941 cmp = constraintExpr_compare (l, r);
962 if (constraintExpr_similar (l,r) )
981 l = constraintExpr_copy (c->lexpr);
982 r = constraintExpr_copy (c->expr);
984 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
986 if (constraintExpr_similar (l,r) && (rHasConstant ) )
988 DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
989 DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
991 constraintExpr_free(l);
992 constraintExpr_free(r);
997 return (rConstant == 0);
999 return (rConstant > 0);
1001 return (rConstant >= 0);
1003 return (rConstant <= 0);
1005 return (rConstant < 0);
1015 constraintExpr_free(l);
1016 constraintExpr_free(r);
1017 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
1024 static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
1027 DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
1029 if (! arithType_canResolve (ar1, ar2) )
1035 if (constraintExpr_similar (expr1, expr2) )
1039 if (! (constraintExpr_canGetValue (expr1) &&
1040 constraintExpr_canGetValue (expr2) ) )
1042 constraintExpr e1, e2;
1046 e1 = constraintExpr_copy(expr1);
1047 e2 = constraintExpr_copy(expr2);
1049 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1051 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1061 if (const1 <= const2)
1062 if (constraintExpr_similar (e1, e2) )
1064 constraintExpr_free(e1);
1065 constraintExpr_free(e2);
1069 DPRINTF(("Can't Get value"));
1071 constraintExpr_free(e1);
1072 constraintExpr_free(e2);
1076 if (constraintExpr_compare (expr2, expr1) >= 0)
1081 if (constraintExpr_similar (expr1, expr2) )
1086 if (constraintExpr_similar (expr1, expr2) )
1090 if (! (constraintExpr_canGetValue (expr1) &&
1091 constraintExpr_canGetValue (expr2) ) )
1093 constraintExpr e1, e2;
1097 e1 = constraintExpr_copy(expr1);
1098 e2 = constraintExpr_copy(expr2);
1100 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1102 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1112 if (const1 >= const2)
1113 if (constraintExpr_similar (e1, e2) )
1115 constraintExpr_free(e1);
1116 constraintExpr_free(e2);
1120 constraintExpr_free(e1);
1121 constraintExpr_free(e2);
1123 DPRINTF(("Can't Get value"));
1127 if (constraintExpr_compare (expr2, expr1) <= 0)
1133 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
1138 static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
1140 DPRINTF (("Doing replace for lexpr") );
1141 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
1142 DPRINTF (("Doing replace for expr") );
1143 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
1147 bool constraint_search (constraint c, constraintExpr old) /*@*/
1152 ret = constraintExpr_search (c->lexpr, old);
1153 ret = ret || constraintExpr_search (c->expr, old);
1157 /* adjust file locs and stuff */
1158 static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
1160 fileloc loc1, loc2, loc3;
1162 DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute),
1163 constraint_print(old))
1166 loc1 = constraint_getFileloc (old);
1167 loc2 = constraintExpr_getFileloc (substitute->lexpr);
1168 loc3 = constraintExpr_getFileloc (substitute->expr);
1170 /* special case of an equality that "contains itself" */
1171 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
1172 if (fileloc_closer (loc1, loc3, loc2))
1174 constraintExpr temp;
1175 DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) )
1177 temp = substitute->lexpr;
1178 substitute->lexpr = substitute->expr;
1179 substitute->expr = temp;
1180 substitute = constraint_simplify(substitute);
1183 fileloc_free (loc1);
1184 fileloc_free (loc2);
1185 fileloc_free (loc3);
1191 /* If function preforms substitutes based on inequality
1193 It uses the rule x >= y && b < y ===> x >= b + 1
1195 Warning this is sound but throws out information
1198 constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
1203 constraintList_elements (p, el)
1205 if ((el->ar == LT ) )
1206 /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */
1208 constraintExpr temp2;
1212 if (constraintExpr_same (el->expr, c->expr) )
1214 DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
1215 constraintExpr_print (c->expr),
1216 constraint_print (c),
1217 constraintExpr_print (el->expr) )
1219 temp2 = constraintExpr_copy (el->lexpr);
1220 constraintExpr_free(c->expr);
1221 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1227 end_constraintList_elements;
1229 c = constraint_simplify(c);
1236 THis function is like inequalitySubstitute but it adds the rule
1237 added the rules x >= y && y <= b ===> x >= b
1238 x >= y && y < b ===> x >= b + 1
1240 This is sound but sonce it throws out additional information it should only one used
1241 if we're oring constraints.
1244 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1246 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
1251 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1252 constraint_print(c), constraintList_print(p) ) ));
1253 constraintList_elements (p, el)
1255 DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1257 if ((el->ar == LT ) || (el->ar == LTE ) )
1258 /* if (!constraint_conflict (c, el) ) */ /*@i523@*/
1260 constraintExpr temp2;
1264 if (constraintExpr_same (el->lexpr, c->expr) )
1266 DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
1267 constraintExpr_print (c->expr),
1268 constraint_print (c),
1269 constraintExpr_print (el->expr) )
1271 temp2 = constraintExpr_copy (el->expr);
1272 constraintExpr_free(c->expr);
1273 if ((el->ar == LTE ) )
1279 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1285 end_constraintList_elements;
1287 c = constraint_simplify(c);
1292 /* This function performs substitutions based on the rule:
1293 for a constraint of the form expr1 >= expr2; a < b =>
1294 a = b -1 for all a in expr1. This will work in most cases.
1296 Like inequalitySubstitute we're throwing away some information
1299 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
1301 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1306 constraintList_elements (p, el)
1308 DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1309 if (( el->ar == LTE) || (el->ar == LT) )
1310 /* if (!constraint_conflict (c, el) ) */ /*@i532@*/
1312 constraintExpr temp2;
1314 temp2 = constraintExpr_copy (el->expr);
1317 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1319 DPRINTF((message ("Replacing %s in %s with %s",
1320 constraintExpr_print (el->lexpr),
1321 constraintExpr_print (c->lexpr),
1322 constraintExpr_print (temp2) ) ));
1324 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
1325 constraintExpr_free(temp2);
1328 end_constraintList_elements;
1330 c = constraint_simplify(c);
1334 /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
1338 ret = constraint_copy(c);
1339 constraintList_elements (p, el)
1342 if (!constraint_conflict (ret, el) )
1347 temp = constraint_copy(el);
1349 temp = constraint_adjust(temp, ret);
1351 DPRINTF((message ("constraint_substitute :: Substituting in %s using %s",
1352 constraint_print (ret), constraint_print (temp)
1356 ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
1357 DPRINTF(( message (" constraint_substitute :: The new constraint is %s", constraint_print (ret) ) ));
1358 constraint_free(temp);
1361 end_constraintList_elements;
1363 ret = constraint_simplify(ret);
1365 DPRINTF(( message (" constraint_substitute :: The final new constraint is %s", constraint_print (ret) ) ));
1371 /*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
1375 ret = constraintList_substitute (target, subList);
1377 constraintList_free(target);
1382 /* we try to do substitutions on each constraint in target using the constraint in sublist*/
1384 /*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
1389 ret = constraintList_makeNew();
1391 constraintList_elements(target, el)
1394 /* drl possible problem : warning make sure that a side effect is not expected */
1396 temp = constraint_substitute(el, subList);
1397 ret = constraintList_add (ret, temp);
1399 end_constraintList_elements;
1404 static constraint constraint_solve (/*@returned@*/ constraint c)
1406 DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) );
1407 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
1408 DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) );
1413 static arithType flipAr (arithType ar)
1428 llcontbug (message("unexpected value: case not handled"));
1433 static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
1435 constraintExpr temp;
1436 c->ar = flipAr (c->ar);
1440 DPRINTF(("Swaped left and right sides of constraint"));
1446 constraint constraint_simplify ( /*@returned@*/ constraint c)
1449 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
1451 if (constraint_tooDeep(c))
1453 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
1458 c->lexpr = constraintExpr_simplify (c->lexpr);
1459 c->expr = constraintExpr_simplify (c->expr);
1461 if (constraintExpr_isBinaryExpr (c->lexpr) )
1463 c = constraint_solve (c);
1465 c->lexpr = constraintExpr_simplify (c->lexpr);
1466 c->expr = constraintExpr_simplify (c->expr);
1469 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
1471 c = constraint_swapLeftRight(c);
1472 /*I don't think this will be an infinate loop*/
1473 c = constraint_simplify(c);
1476 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1484 /* returns true if fileloc for term1 is closer to file for term2 than term3*/
1486 bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1489 if (!fileloc_isDefined (loc1) )
1492 if (!fileloc_isDefined (loc2) )
1495 if (!fileloc_isDefined (loc3) )
1501 if (fileloc_equal (loc2, loc3) )
1504 if (fileloc_equal (loc1, loc2) )
1507 if (fileloc_equal (loc1, loc3) )
1510 if ( fileloc_lessthan (loc1, loc2) )
1512 if (fileloc_lessthan (loc2, loc3) )
1514 llassert (fileloc_lessthan (loc1, loc3) );
1523 if ( !fileloc_lessthan (loc1, loc2) )
1525 if (!fileloc_lessthan (loc2, loc3) )
1527 llassert (!fileloc_lessthan (loc1, loc3) );