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 if (!resolveOr (c, post1) )
391 temp = constraint_substitute (c, post1);
393 if (!resolveOr (temp, post1) )
395 /* try inequality substitution */
398 /* the inequality substitution may cause us to lose information
399 so we don't want to store the result but we do anyway
401 temp2 = constraint_copy (c);
402 temp2 = inequalitySubstitute (temp2, post1);
404 if (!resolveOr (temp2, post1) )
407 temp3 = constraint_copy(temp2);
409 temp3 = inequalitySubstituteStrong (temp3, post1);
410 if (!resolveOr (temp3, post1) )
412 temp2 = inequalitySubstituteUnsound (temp2, post1);
413 if (!resolveOr (temp2, post1) )
415 if (!constraint_same (temp, temp2) )
416 temp = constraint_addOr (temp, temp2);
418 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
419 temp = constraint_addOr (temp, temp3);
423 constraint_free(temp2);
424 constraint_free(temp3);
429 constraint_free(temp2);
430 constraint_free(temp3);
434 constraint_free(temp2);
435 constraint_free(temp3);
440 constraint_free(temp2);
444 constraint_free(temp);
448 /*drl bee: pbr*/ *resolved = TRUE;
452 static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
459 DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
463 /*drl bee: pbr*/ *resolved = FALSE;
466 ret = constraint_copy(c);
468 if (constraintList_isEmpty(post1) )
476 ret = doResolve (ret, post1, resolved);
481 constraint_free(next);
483 /*we don't need to free ret when resolved is false because ret is null*/
484 llassert(ret == NULL);
495 curr = doResolve (curr, post1, resolved);
497 /*drl bee: pbr*/ if (*resolved)
499 /* curr is null so we don't try to free it*/
500 llassert(curr == NULL);
503 constraint_free(next);
505 constraint_free(ret);
508 ret = constraint_addOr (ret, curr);
509 constraint_free(curr);
514 /* tries to resolve constraints in list pr2 using post1 */
515 /*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
520 ret = constraintList_makeNew();
521 DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
523 constraintList_elements (pre2, el)
525 temp = doResolveOr (el, post1, &resolved);
529 ret = constraintList_add(ret, temp);
533 /* we don't need to free temp when
534 resolved is false because temp is null */
535 llassert(temp == NULL);
538 } end_constraintList_elements;
540 DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
544 static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
548 ret = constraintList_makeNew();
549 constraintList_elements (pre2, el)
551 if (!constraintList_resolve (el, post1) )
553 temp = constraint_substitute (el, post1);
554 llassert (temp != NULL);
556 if (!constraintList_resolve (temp, post1) )
557 ret = constraintList_add (ret, temp);
559 constraint_free(temp);
563 DPRINTF ((message ("Resolved away %s ", constraint_print(el) ) ) );
565 } end_constraintList_elements;
571 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
575 ret = reflectChangesEnsures (pre2, post1);
577 constraintList_free(pre2);
583 static bool constraint_conflict (constraint c1, constraint c2)
586 if (constraintExpr_similar(c1->lexpr, c2->lexpr) )
589 if (c1->ar == c2->ar)
591 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
596 /* This is a slight kludg to prevent circular constraints like
597 strlen(str) == maxRead(s) + strlen(str);
600 /*@i324234*/ /* clean this up */
603 if (c1->ar == c2->ar)
605 if (constraintExpr_search (c1->lexpr, c2->expr) )
606 if (constraintExpr_isTerm(c1->lexpr) )
610 term = constraintExpr_getTerm(c1->lexpr);
612 if (constraintTerm_isExprNode(term) )
614 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
620 if (constraint_tooDeep(c1) || constraint_tooDeep(c2) )
622 DPRINTF ((message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) );
626 DPRINTF ((message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
632 static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
634 if (conflicting->ar ==EQ )
636 good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
637 good = constraint_simplify (good);
643 static bool conflict (constraint c, constraintList list)
646 constraintList_elements (list, el)
648 if ( constraint_conflict(el, c) )
650 constraint_fixConflict (el, c);
653 } end_constraintList_elements;
660 check if constraint in list1 conflicts with constraints in List2. If so we
661 remove form list1 and change list2.
664 constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
667 ret = constraintList_makeNew();
668 llassert(constraintList_isDefined(list1) );
669 constraintList_elements (list1, el)
671 if (! conflict (el, list2) )
674 temp = constraint_copy(el);
675 ret = constraintList_add (ret, temp);
677 } end_constraintList_elements;
682 /*returns true if constraint post satifies cosntriant pre */
683 static bool satifies (constraint pre, constraint post)
685 if (constraint_isAlwaysTrue (pre) )
688 if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
692 if (constraintExpr_isUndefined(post->expr))
698 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
702 bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p)
704 constraintList_elements (p, el)
706 if ( satifies (c, el) )
708 DPRINTF ((message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
711 DPRINTF ((message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) );
713 end_constraintList_elements;
714 DPRINTF ((message ("no constraints satify %s", constraint_print(c) ) ));
718 static bool arithType_canResolve (arithType ar1, arithType ar2)
724 if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ) )
737 if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ) )
746 /*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
747 static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
752 /*@access constraintExpr@*/
754 if ((expr2->kind != term) && (buf1->kind != term) )
758 ct = constraintExprData_termGetTerm(expr2->data);
760 if (!constraintTerm_isExprNode(ct) )
763 e = constraintTerm_getExprNode(ct);
765 if (e->kind != XPR_SIZEOF)
768 t = exprData_getSingle (e->edata);
769 s1 = exprNode_getSref (t);
771 s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) );
773 /*@i223@*/ /*this may be the wronge thing to test for */
774 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
776 /*@i22*/ /* get rid of this test of now */
777 /* if (ctype_isFixedArray (sRef_getType (s2) ) ) */
783 /* look for the special case of
784 maxSet(buf) >= sizeof(buf) - 1
787 /*@i223@*/ /*need to add some type checking */
788 static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
790 constraintExpr l, r, buf1, buf2, con;
792 DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
798 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
801 /*check if the constraintExpr is MaxSet(buf) */
802 if (l->kind == unaryExpr)
804 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
806 buf1 = constraintExprData_unaryExprGetExpr(l->data);
815 if (r->kind != binaryexpr)
818 buf2 = constraintExprData_binaryExprGetExpr1(r->data);
819 con = constraintExprData_binaryExprGetExpr2(r->data);
821 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS)
823 if (constraintExpr_canGetValue(con) )
827 i = constraintExpr_getValue(con);
837 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS)
839 if (constraintExpr_canGetValue(con) )
843 i = constraintExpr_getValue(con);
853 if (sizeofBufComp(buf1, buf2))
864 /*@noaccess constraintExpr@*/
866 /* We look for constraint which are tautologies */
868 bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
877 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
879 if (sizeOfMaxSet(c) )
882 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
885 cmp = constraintExpr_compare (l, r);
906 if (constraintExpr_similar (l,r) )
925 l = constraintExpr_copy (c->lexpr);
926 r = constraintExpr_copy (c->expr);
928 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
930 if (constraintExpr_similar (l,r) && (rHasConstant ) )
932 DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
933 DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
935 constraintExpr_free(l);
936 constraintExpr_free(r);
941 return (rConstant == 0);
943 return (rConstant > 0);
945 return (rConstant >= 0);
947 return (rConstant <= 0);
949 return (rConstant < 0);
959 constraintExpr_free(l);
960 constraintExpr_free(r);
961 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
968 static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
971 DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
973 if (! arithType_canResolve (ar1, ar2) )
979 if (constraintExpr_similar (expr1, expr2) )
983 if (! (constraintExpr_canGetValue (expr1) &&
984 constraintExpr_canGetValue (expr2) ) )
986 constraintExpr e1, e2;
990 e1 = constraintExpr_copy(expr1);
991 e2 = constraintExpr_copy(expr2);
993 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
995 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1005 if (const1 <= const2)
1006 if (constraintExpr_similar (e1, e2) )
1008 constraintExpr_free(e1);
1009 constraintExpr_free(e2);
1013 DPRINTF(("Can't Get value"));
1015 constraintExpr_free(e1);
1016 constraintExpr_free(e2);
1020 if (constraintExpr_compare (expr2, expr1) >= 0)
1025 if (constraintExpr_similar (expr1, expr2) )
1030 if (constraintExpr_similar (expr1, expr2) )
1034 if (! (constraintExpr_canGetValue (expr1) &&
1035 constraintExpr_canGetValue (expr2) ) )
1037 constraintExpr e1, e2;
1041 e1 = constraintExpr_copy(expr1);
1042 e2 = constraintExpr_copy(expr2);
1044 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1046 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1056 if (const1 >= const2)
1057 if (constraintExpr_similar (e1, e2) )
1059 constraintExpr_free(e1);
1060 constraintExpr_free(e2);
1064 constraintExpr_free(e1);
1065 constraintExpr_free(e2);
1067 DPRINTF(("Can't Get value"));
1071 if (constraintExpr_compare (expr2, expr1) <= 0)
1077 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
1082 static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
1084 DPRINTF (("Doing replace for lexpr") );
1085 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
1086 DPRINTF (("Doing replace for expr") );
1087 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
1091 bool constraint_search (constraint c, constraintExpr old) /*@*/
1096 ret = constraintExpr_search (c->lexpr, old);
1097 ret = ret || constraintExpr_search (c->expr, old);
1101 /* adjust file locs and stuff */
1102 static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
1104 fileloc loc1, loc2, loc3;
1106 DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute),
1107 constraint_print(old))
1110 loc1 = constraint_getFileloc (old);
1111 loc2 = constraintExpr_getFileloc (substitute->lexpr);
1112 loc3 = constraintExpr_getFileloc (substitute->expr);
1114 /* special case of an equality that "contains itself" */
1115 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
1116 if (fileloc_closer (loc1, loc3, loc2))
1118 constraintExpr temp;
1119 DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) )
1121 temp = substitute->lexpr;
1122 substitute->lexpr = substitute->expr;
1123 substitute->expr = temp;
1124 substitute = constraint_simplify(substitute);
1127 fileloc_free (loc1);
1128 fileloc_free (loc2);
1129 fileloc_free (loc3);
1135 /* If function preforms substitutes based on inequality
1137 It uses the rule x >= y && b < y ===> x >= b + 1
1139 Warning this is sound but throws out information
1142 constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
1147 constraintList_elements (p, el)
1149 if ((el->ar == LT ) )
1150 /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */
1152 constraintExpr temp2;
1156 if (constraintExpr_same (el->expr, c->expr) )
1158 DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
1159 constraintExpr_print (c->expr),
1160 constraint_print (c),
1161 constraintExpr_print (el->expr) )
1163 temp2 = constraintExpr_copy (el->lexpr);
1164 constraintExpr_free(c->expr);
1165 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1171 end_constraintList_elements;
1173 c = constraint_simplify(c);
1180 THis function is like inequalitySubstitute but it adds the rule
1181 added the rules x >= y && y <= b ===> x >= b
1182 x >= y && y < b ===> x >= b + 1
1184 This is sound but sonce it throws out additional information it should only one used
1185 if we're oring constraints.
1188 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1190 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
1195 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1196 constraint_print(c), constraintList_print(p) ) ));
1197 constraintList_elements (p, el)
1199 DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1201 if ((el->ar == LT ) || (el->ar == LTE ) )
1202 /* if (!constraint_conflict (c, el) ) */ /*@i523@*/
1204 constraintExpr temp2;
1208 if (constraintExpr_same (el->lexpr, c->expr) )
1210 DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
1211 constraintExpr_print (c->expr),
1212 constraint_print (c),
1213 constraintExpr_print (el->expr) )
1215 temp2 = constraintExpr_copy (el->expr);
1216 constraintExpr_free(c->expr);
1217 if ((el->ar == LTE ) )
1223 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1229 end_constraintList_elements;
1231 c = constraint_simplify(c);
1236 /* This function performs substitutions based on the rule:
1237 for a constraint of the form expr1 >= expr2; a < b =>
1238 a = b -1 for all a in expr1. This will work in most cases.
1240 Like inequalitySubstitute we're throwing away some information
1243 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
1245 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1250 constraintList_elements (p, el)
1252 DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1253 if (( el->ar == LTE) || (el->ar == LT) )
1254 /* if (!constraint_conflict (c, el) ) */ /*@i532@*/
1256 constraintExpr temp2;
1258 temp2 = constraintExpr_copy (el->expr);
1261 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1263 DPRINTF((message ("Replacing %s in %s with %s",
1264 constraintExpr_print (el->lexpr),
1265 constraintExpr_print (c->lexpr),
1266 constraintExpr_print (temp2) ) ));
1268 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
1269 constraintExpr_free(temp2);
1272 end_constraintList_elements;
1274 c = constraint_simplify(c);
1278 /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
1282 ret = constraint_copy(c);
1283 constraintList_elements (p, el)
1286 if (!constraint_conflict (ret, el) )
1291 temp = constraint_copy(el);
1293 temp = constraint_adjust(temp, ret);
1295 DPRINTF((message ("Substituting %s in the constraint %s",
1296 constraint_print (temp), constraint_print (ret)
1300 ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
1301 DPRINTF(( message ("The new constraint is %s", constraint_print (ret) ) ));
1302 constraint_free(temp);
1305 end_constraintList_elements;
1306 DPRINTF(( message ("The finial new constraint is %s", constraint_print (ret) ) ));
1308 ret = constraint_simplify(ret);
1313 /*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
1317 ret = constraintList_substitute (target, subList);
1319 constraintList_free(target);
1324 /* we try to do substitutions on each constraint in target using the constraint in sublist*/
1326 /*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
1331 ret = constraintList_makeNew();
1333 constraintList_elements(target, el)
1336 /* drl possible problem : warning make sure that a side effect is not expected */
1338 temp = constraint_substitute(el, subList);
1339 ret = constraintList_add (ret, temp);
1341 end_constraintList_elements;
1346 static constraint constraint_solve (/*@returned@*/ constraint c)
1348 DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) );
1349 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
1350 DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) );
1355 static arithType flipAr (arithType ar)
1370 llcontbug (message("unexpected value: case not handled"));
1375 static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
1377 constraintExpr temp;
1378 c->ar = flipAr (c->ar);
1382 DPRINTF(("Swaped left and right sides of constraint"));
1388 constraint constraint_simplify ( /*@returned@*/ constraint c)
1391 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
1393 if (constraint_tooDeep(c))
1395 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
1400 c->lexpr = constraintExpr_simplify (c->lexpr);
1401 c->expr = constraintExpr_simplify (c->expr);
1403 if (constraintExpr_isBinaryExpr (c->lexpr) )
1405 c = constraint_solve (c);
1407 c->lexpr = constraintExpr_simplify (c->lexpr);
1408 c->expr = constraintExpr_simplify (c->expr);
1411 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
1413 c = constraint_swapLeftRight(c);
1414 /*I don't think this will be an infinate loop*/
1415 c = constraint_simplify(c);
1418 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1426 /* returns true if fileloc for term1 is closer to file for term2 than term3*/
1428 bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1431 if (!fileloc_isDefined (loc1) )
1434 if (!fileloc_isDefined (loc2) )
1437 if (!fileloc_isDefined (loc3) )
1443 if (fileloc_equal (loc2, loc3) )
1446 if (fileloc_equal (loc1, loc2) )
1449 if (fileloc_equal (loc1, loc3) )
1452 if ( fileloc_lessthan (loc1, loc2) )
1454 if (fileloc_lessthan (loc2, loc3) )
1456 llassert (fileloc_lessthan (loc1, loc3) );
1465 if ( !fileloc_lessthan (loc1, loc2) )
1467 if (!fileloc_lessthan (loc2, loc3) )
1469 llassert (!fileloc_lessthan (loc1, loc3) );