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: splint@cs.virginia.edu
21 ** To report a bug: splint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
27 ** constraintResolve.c
30 /* #define DEBUGPRINT 1 */
32 # include <ctype.h> /* for isdigit */
33 # include "splintMacros.nf"
35 # include "cgrammar.h"
36 # include "cgrammar_tokens.h"
38 # include "exprChecks.h"
39 # include "exprNodeSList.h"
42 /*@access constraint, exprNode @*/
45 static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
48 static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
50 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
52 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
54 static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
57 static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
59 static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
61 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
63 /*********************************************/
68 /*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
72 ret = constraintList_mergeEnsures (list1, list2);
74 constraintList_free(list1);
78 /*@only@*/ constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2)
83 llassert(constraintList_isDefined(list1) );
84 llassert(constraintList_isDefined(list2) );
86 DPRINTF(( message ("constraintList_mergeEnsures: list1 %s list2 %s",
87 constraintList_print(list1), constraintList_print(list2)
90 ret = constraintList_fixConflicts (list1, list2);
91 ret = reflectChangesEnsuresFree1 (ret, list2);
92 temp = constraintList_subsumeEnsures (ret, list2);
93 constraintList_free(ret);
96 temp = constraintList_subsumeEnsures (list2, ret);
98 temp = constraintList_addList (temp, ret);
99 constraintList_free(ret);
101 DPRINTF(( message ("constraintList_mergeEnsures: returning %s ",
102 constraintList_print(temp) )
110 /*@only@*/ constraintList constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1, constraintList list2)
114 ret = constraintList_mergeRequires(list1, list2);
116 constraintList_free(list1);
121 /*@only@*/ constraintList constraintList_mergeRequires (constraintList list1, constraintList list2)
126 DPRINTF((message ("constraintList_mergeRequires: merging %s and %s ", constraintList_print (list1), constraintList_print(list2) ) ) );
128 if (context_getFlag (FLG_REDUNDANTCONSTRAINTS) )
130 ret = constraintList_copy(list1);
131 ret = constraintList_addList(ret, list2);
135 /* get constraints in list1 not satified by list2 */
136 temp = constraintList_reflectChanges(list1, list2);
137 DPRINTF((message ("constraintList_mergeRequires: temp = %s", constraintList_print(temp) ) ) );
139 /*get constraints in list2 not satified by temp*/
140 ret = constraintList_reflectChanges(list2, temp);
142 DPRINTF((message ("constraintList_mergeRequires: ret = %s", constraintList_print(ret) ) ) );
144 ret = constraintList_addListFree (ret, temp);
146 DPRINTF((message ("constraintList_mergeRequires: returning %s", constraintList_print(ret) ) ) );
151 /* old name mergeResolve renamed for czech naming convention */
152 void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
154 constraintList temp, temp2;
156 DPRINTF((message ("magically merging constraint into parent:%s for", exprNode_unparse (parent) )) );
158 DPRINTF((message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2) ) ) );
160 if (exprNode_isError (child1) || exprNode_isError(child2) )
162 if (exprNode_isError (child1) && !exprNode_isError(child2) )
164 constraintList_free(parent->requiresConstraints);
166 parent->requiresConstraints = constraintList_copy (child2->requiresConstraints);
167 constraintList_free(parent->ensuresConstraints);
169 parent->ensuresConstraints = constraintList_copy (child2->ensuresConstraints);
170 DPRINTF((message ("Copied child constraints: pre: %s and post: %s",
171 constraintList_print( child2->requiresConstraints),
172 constraintList_print (child2->ensuresConstraints)
179 llassert(exprNode_isError(child2) );
184 llassert(!exprNode_isError (child1) && ! exprNode_isError(child2) );
186 DPRINTF((message ("Child constraints are %s %s and %s %s",
187 constraintList_print (child1->requiresConstraints),
188 constraintList_print (child1->ensuresConstraints),
189 constraintList_print (child2->requiresConstraints),
190 constraintList_print (child2->ensuresConstraints)
194 constraintList_free(parent->requiresConstraints);
196 parent->requiresConstraints = constraintList_copy (child1->requiresConstraints);
198 if ( context_getFlag (FLG_ORCONSTRAINT) )
199 temp = constraintList_reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
201 temp = constraintList_reflectChanges(child2->requiresConstraints, child1->ensuresConstraints);
203 temp2 = constraintList_mergeRequires (parent->requiresConstraints, temp);
204 constraintList_free(parent->requiresConstraints);
205 constraintList_free(temp);
207 parent->requiresConstraints = temp2;
209 DPRINTF((message ("Parent requires constraints are %s ",
210 constraintList_print (parent->requiresConstraints)
213 constraintList_free(parent->ensuresConstraints);
215 parent->ensuresConstraints = constraintList_mergeEnsures(child1->ensuresConstraints,
216 child2->ensuresConstraints);
219 DPRINTF((message ("Parent constraints are %s and %s ",
220 constraintList_print (parent->requiresConstraints),
221 constraintList_print (parent->ensuresConstraints)
229 /*@only@*/ constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2)
232 ret = constraintList_makeNew();
233 constraintList_elements (list1, el)
236 DPRINTF ((message ("Examining %s", constraint_print (el) ) ) );
237 if (!constraintList_resolve (el, list2) )
240 temp = constraint_copy(el);
241 ret = constraintList_add (ret, temp);
245 DPRINTF ((message ("Subsuming %s", constraint_print (el) ) ) );
247 } end_constraintList_elements;
254 /*used to be reflectChangesFreePre renamed for Czech naming conventino*/
255 /* tries to resolve constraints in list pre2 using post1 */
256 /*@only@*/ constraintList constraintList_reflectChangesFreePre (/*@only@*/ constraintList pre2, /*@observer@*/ constraintList post1)
260 ret = constraintList_reflectChanges(pre2, post1);
262 constraintList_free (pre2);
269 /* tries to resolve constraints in list pre2 using post1 */
271 static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
278 llassert (! context_getFlag (FLG_ORCONSTRAINT) );
280 ret = constraintList_makeNew();
281 DPRINTF((message ("reflectChanges: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
283 constraintList_elements (pre2, el)
285 if (!constraintList_resolve (el, post1) )
287 temp = constraint_substitute (el, post1);
288 if (!constraintList_resolve (temp, post1) )
290 /* try inequality substitution
291 the inequality substitution may cause us to lose information
292 so we don't want to store the result but we do it anyway
294 temp2 = constraint_copy (temp);
295 temp2 = inequalitySubstitute (temp2, post1);
296 if (!constraintList_resolve (temp2, post1) )
298 temp2 = inequalitySubstituteUnsound (temp2, post1);
299 if (!constraintList_resolve (temp2, post1) )
300 ret = constraintList_add (ret, temp2);
302 constraint_free(temp2);
306 constraint_free(temp2);
309 constraint_free(temp);
311 } end_constraintList_elements;
313 DPRINTF((message ("reflectChanges: returning %s", constraintList_print(ret) ) ) );
317 /* tries to resolve constraints in list pre2 using post1 */
318 /*@only@*/ constraintList constraintList_reflectChanges(/*@observer@*/ constraintList pre2, /*@observer@*/ constraintList post1)
322 if ( context_getFlag (FLG_ORCONSTRAINT) )
324 temp = constraintList_reflectChangesOr (pre2, post1);
326 temp = reflectChangesNoOr(pre2, post1);
331 static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
336 DPRINTF((message("constraint_addor: oring %s onto %s", constraint_printOr(orConstr), constraint_printOr(orig) ) ));
338 while (c->or != NULL)
343 c->or = constraint_copy(orConstr);
345 DPRINTF((message("constraint_addor: returning %s",constraint_printOr(orig) ) ));
351 static bool resolveOr ( /*@temp@*/ constraint c, /*@observer@*/ /*@temp@*/ constraintList list)
358 DPRINTF(( message("resolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(list) ) ));
363 if (constraintList_resolve (temp, list) )
367 llassert(numberOr <= 10);
369 while (constraint_isDefined(temp));
374 /*This is a "helper" function for doResolveOr */
376 static /*@only@*/ constraint doResolve (/*@only@*/ constraint c, constraintList post1, bool * resolved)
380 llassert(constraint_isUndefined (c->or ) );
382 if (!resolveOr (c, post1) )
385 temp = constraint_substitute (c, post1);
387 if (!resolveOr (temp, post1) )
389 /* try inequality substitution */
392 /* the inequality substitution may cause us to lose information
393 so we don't want to store the result but we do anyway
395 temp2 = constraint_copy (c);
396 temp2 = inequalitySubstitute (temp2, post1);
398 if (!resolveOr (temp2, post1) )
401 temp3 = constraint_copy(temp2);
403 temp3 = inequalitySubstituteStrong (temp3, post1);
404 if (!resolveOr (temp3, post1) )
406 temp2 = inequalitySubstituteUnsound (temp2, post1);
407 if (!resolveOr (temp2, post1) )
409 if (!constraint_same (temp, temp2) )
410 temp = constraint_addOr (temp, temp2);
412 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
413 temp = constraint_addOr (temp, temp3);
417 constraint_free(temp2);
418 constraint_free(temp3);
423 constraint_free(temp2);
424 constraint_free(temp3);
428 constraint_free(temp2);
429 constraint_free(temp3);
434 constraint_free(temp2);
438 constraint_free(temp);
442 /*drl bee: pbr*/ *resolved = TRUE;
446 static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
453 DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
457 /*drl bee: pbr*/ *resolved = FALSE;
460 ret = constraint_copy(c);
462 if (constraintList_isEmpty(post1) )
470 ret = doResolve (ret, post1, resolved);
475 constraint_free(next);
477 /*we don't need to free ret when resolved is false because ret is null*/
478 llassert(ret == NULL);
489 curr = doResolve (curr, post1, resolved);
491 /*drl bee: pbr*/ if (*resolved)
493 /* curr is null so we don't try to free it*/
494 llassert(curr == NULL);
497 constraint_free(next);
499 constraint_free(ret);
502 ret = constraint_addOr (ret, curr);
503 constraint_free(curr);
508 /* tries to resolve constraints in list pr2 using post1 */
509 /*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
514 ret = constraintList_makeNew();
515 DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
517 constraintList_elements (pre2, el)
519 temp = doResolveOr (el, post1, &resolved);
523 ret = constraintList_add(ret, temp);
527 /* we don't need to free temp when
528 resolved is false because temp is null */
529 llassert(temp == NULL);
532 } end_constraintList_elements;
534 DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
538 static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
542 ret = constraintList_makeNew();
543 constraintList_elements (pre2, el)
545 if (!constraintList_resolve (el, post1) )
547 temp = constraint_substitute (el, post1);
548 llassert (temp != NULL);
550 if (!constraintList_resolve (temp, post1) )
551 ret = constraintList_add (ret, temp);
553 constraint_free(temp);
557 DPRINTF ((message ("Resolved away %s ", constraint_print(el) ) ) );
559 } end_constraintList_elements;
565 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
569 ret = reflectChangesEnsures (pre2, post1);
571 constraintList_free(pre2);
577 static bool constraint_conflict (constraint c1, constraint c2)
580 if (constraintExpr_similar(c1->lexpr, c2->lexpr) )
583 if (c1->ar == c2->ar)
585 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
590 /* This is a slight kludg to prevent circular constraints like
591 strlen(str) == maxRead(s) + strlen(str);
594 /*@i324234*/ /* clean this up */
597 if (c1->ar == c2->ar)
599 if (constraintExpr_search (c1->lexpr, c2->expr) )
600 if (constraintExpr_isTerm(c1->lexpr) )
604 term = constraintExpr_getTerm(c1->lexpr);
606 if (constraintTerm_isExprNode(term) )
608 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
614 if (constraint_tooDeep(c1) || constraint_tooDeep(c2) )
616 DPRINTF ((message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) );
620 DPRINTF ((message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
626 static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
628 if (conflicting->ar ==EQ )
630 good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
631 good = constraint_simplify (good);
637 static bool conflict (constraint c, constraintList list)
640 constraintList_elements (list, el)
642 if ( constraint_conflict(el, c) )
644 constraint_fixConflict (el, c);
647 } end_constraintList_elements;
654 check if constraint in list1 conflicts with constraints in List2. If so we
655 remove form list1 and change list2.
658 constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
661 ret = constraintList_makeNew();
662 llassert(constraintList_isDefined(list1) );
663 constraintList_elements (list1, el)
665 if (! conflict (el, list2) )
668 temp = constraint_copy(el);
669 ret = constraintList_add (ret, temp);
671 } end_constraintList_elements;
676 /*returns true if constraint post satifies cosntriant pre */
677 static bool satifies (constraint pre, constraint post)
679 if (constraint_isAlwaysTrue (pre) )
682 if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
686 if (constraintExpr_isUndefined(post->expr))
692 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
696 bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p)
698 constraintList_elements (p, el)
700 if ( satifies (c, el) )
702 DPRINTF ((message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
705 DPRINTF ((message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) );
707 end_constraintList_elements;
708 DPRINTF ((message ("no constraints satify %s", constraint_print(c) ) ));
712 static bool arithType_canResolve (arithType ar1, arithType ar2)
718 if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ) )
731 if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ) )
740 /*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
741 static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
746 /*@access constraintExpr@*/
748 if ((expr2->kind != term) && (buf1->kind != term) )
752 ct = constraintExprData_termGetTerm(expr2->data);
754 if (!constraintTerm_isExprNode(ct) )
757 e = constraintTerm_getExprNode(ct);
759 if (e->kind != XPR_SIZEOF)
762 t = exprData_getSingle (e->edata);
763 s1 = exprNode_getSref (t);
765 s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) );
767 /*@i223@*/ /*this may be the wronge thing to test for */
768 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
770 /*@i22*/ /* get rid of this test of now */
771 /* if (ctype_isFixedArray (sRef_getType (s2) ) ) */
777 /* look for the special case of
778 maxSet(buf) >= sizeof(buf) - 1
781 /*@i223@*/ /*need to add some type checking */
782 static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
784 constraintExpr l, r, buf1, buf2, con;
786 DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
792 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
795 /*check if the constraintExpr is MaxSet(buf) */
796 if (l->kind == unaryExpr)
798 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
800 buf1 = constraintExprData_unaryExprGetExpr(l->data);
809 if (r->kind != binaryexpr)
812 buf2 = constraintExprData_binaryExprGetExpr1(r->data);
813 con = constraintExprData_binaryExprGetExpr2(r->data);
815 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS)
817 if (constraintExpr_canGetValue(con) )
821 i = constraintExpr_getValue(con);
831 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS)
833 if (constraintExpr_canGetValue(con) )
837 i = constraintExpr_getValue(con);
847 if (sizeofBufComp(buf1, buf2))
858 /*@noaccess constraintExpr@*/
860 /* We look for constraint which are tautologies */
862 bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
871 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
873 if (sizeOfMaxSet(c) )
876 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
879 cmp = constraintExpr_compare (l, r);
900 if (constraintExpr_similar (l,r) )
919 l = constraintExpr_copy (c->lexpr);
920 r = constraintExpr_copy (c->expr);
922 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
924 if (constraintExpr_similar (l,r) && (rHasConstant ) )
926 DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
927 DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
929 constraintExpr_free(l);
930 constraintExpr_free(r);
935 return (rConstant == 0);
937 return (rConstant > 0);
939 return (rConstant >= 0);
941 return (rConstant <= 0);
943 return (rConstant < 0);
953 constraintExpr_free(l);
954 constraintExpr_free(r);
955 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
962 static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
965 DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
967 if (! arithType_canResolve (ar1, ar2) )
973 if (constraintExpr_similar (expr1, expr2) )
977 if (! (constraintExpr_canGetValue (expr1) &&
978 constraintExpr_canGetValue (expr2) ) )
980 constraintExpr e1, e2;
984 e1 = constraintExpr_copy(expr1);
985 e2 = constraintExpr_copy(expr2);
987 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
989 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
999 if (const1 <= const2)
1000 if (constraintExpr_similar (e1, e2) )
1002 constraintExpr_free(e1);
1003 constraintExpr_free(e2);
1007 DPRINTF(("Can't Get value"));
1009 constraintExpr_free(e1);
1010 constraintExpr_free(e2);
1014 if (constraintExpr_compare (expr2, expr1) >= 0)
1019 if (constraintExpr_similar (expr1, expr2) )
1024 if (constraintExpr_similar (expr1, expr2) )
1028 if (! (constraintExpr_canGetValue (expr1) &&
1029 constraintExpr_canGetValue (expr2) ) )
1031 constraintExpr e1, e2;
1035 e1 = constraintExpr_copy(expr1);
1036 e2 = constraintExpr_copy(expr2);
1038 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1040 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1050 if (const1 >= const2)
1051 if (constraintExpr_similar (e1, e2) )
1053 constraintExpr_free(e1);
1054 constraintExpr_free(e2);
1058 constraintExpr_free(e1);
1059 constraintExpr_free(e2);
1061 DPRINTF(("Can't Get value"));
1065 if (constraintExpr_compare (expr2, expr1) <= 0)
1071 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
1076 static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
1078 DPRINTF (("Doing replace for lexpr") );
1079 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
1080 DPRINTF (("Doing replace for expr") );
1081 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
1085 bool constraint_search (constraint c, constraintExpr old) /*@*/
1090 ret = constraintExpr_search (c->lexpr, old);
1091 ret = ret || constraintExpr_search (c->expr, old);
1095 /* adjust file locs and stuff */
1096 static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
1098 fileloc loc1, loc2, loc3;
1100 DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute),
1101 constraint_print(old))
1104 loc1 = constraint_getFileloc (old);
1105 loc2 = constraintExpr_getFileloc (substitute->lexpr);
1106 loc3 = constraintExpr_getFileloc (substitute->expr);
1108 /* special case of an equality that "contains itself" */
1109 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
1110 if (fileloc_closer (loc1, loc3, loc2))
1112 constraintExpr temp;
1113 DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) )
1115 temp = substitute->lexpr;
1116 substitute->lexpr = substitute->expr;
1117 substitute->expr = temp;
1118 substitute = constraint_simplify(substitute);
1121 fileloc_free (loc1);
1122 fileloc_free (loc2);
1123 fileloc_free (loc3);
1129 /* If function preforms substitutes based on inequality
1131 It uses the rule x >= y && b < y ===> x >= b + 1
1133 Warning this is sound but throws out information
1136 constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
1141 constraintList_elements (p, el)
1143 if ((el->ar == LT ) )
1144 /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */
1146 constraintExpr temp2;
1150 if (constraintExpr_same (el->expr, c->expr) )
1152 DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
1153 constraintExpr_print (c->expr),
1154 constraint_print (c),
1155 constraintExpr_print (el->expr) )
1157 temp2 = constraintExpr_copy (el->lexpr);
1158 constraintExpr_free(c->expr);
1159 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1165 end_constraintList_elements;
1167 c = constraint_simplify(c);
1174 THis function is like inequalitySubstitute but it adds the rule
1175 added the rules x >= y && y <= b ===> x >= b
1176 x >= y && y < b ===> x >= b + 1
1178 This is sound but sonce it throws out additional information it should only one used
1179 if we're oring constraints.
1182 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1184 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
1189 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1190 constraint_print(c), constraintList_print(p) ) ));
1191 constraintList_elements (p, el)
1193 DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1195 if ((el->ar == LT ) || (el->ar == LTE ) )
1196 /* if (!constraint_conflict (c, el) ) */ /*@i523@*/
1198 constraintExpr temp2;
1202 if (constraintExpr_same (el->lexpr, c->expr) )
1204 DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
1205 constraintExpr_print (c->expr),
1206 constraint_print (c),
1207 constraintExpr_print (el->expr) )
1209 temp2 = constraintExpr_copy (el->expr);
1210 constraintExpr_free(c->expr);
1211 if ((el->ar == LTE ) )
1217 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1223 end_constraintList_elements;
1225 c = constraint_simplify(c);
1230 /* This function performs substitutions based on the rule:
1231 for a constraint of the form expr1 >= expr2; a < b =>
1232 a = b -1 for all a in expr1. This will work in most cases.
1234 Like inequalitySubstitute we're throwing away some information
1237 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
1239 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1244 constraintList_elements (p, el)
1246 DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1247 if (( el->ar == LTE) || (el->ar == LT) )
1248 /* if (!constraint_conflict (c, el) ) */ /*@i532@*/
1250 constraintExpr temp2;
1252 temp2 = constraintExpr_copy (el->expr);
1255 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1257 DPRINTF((message ("Replacing %s in %s with %s",
1258 constraintExpr_print (el->lexpr),
1259 constraintExpr_print (c->lexpr),
1260 constraintExpr_print (temp2) ) ));
1262 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
1263 constraintExpr_free(temp2);
1266 end_constraintList_elements;
1268 c = constraint_simplify(c);
1272 /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
1276 ret = constraint_copy(c);
1277 constraintList_elements (p, el)
1280 if (!constraint_conflict (ret, el) )
1285 temp = constraint_copy(el);
1287 temp = constraint_adjust(temp, ret);
1289 DPRINTF((message ("Substituting %s in the constraint %s",
1290 constraint_print (temp), constraint_print (ret)
1294 ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
1295 DPRINTF(( message ("The new constraint is %s", constraint_print (ret) ) ));
1296 constraint_free(temp);
1299 end_constraintList_elements;
1300 DPRINTF(( message ("The finial new constraint is %s", constraint_print (ret) ) ));
1302 ret = constraint_simplify(ret);
1307 /*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
1311 ret = constraintList_substitute (target, subList);
1313 constraintList_free(target);
1318 /* we try to do substitutions on each constraint in target using the constraint in sublist*/
1320 /*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
1325 ret = constraintList_makeNew();
1327 constraintList_elements(target, el)
1330 /* drl possible problem : warning make sure that a side effect is not expected */
1332 temp = constraint_substitute(el, subList);
1333 ret = constraintList_add (ret, temp);
1335 end_constraintList_elements;
1340 static constraint constraint_solve (/*@returned@*/ constraint c)
1342 DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) );
1343 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
1344 DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) );
1349 static arithType flipAr (arithType ar)
1364 llcontbug (message("unexpected value: case not handled"));
1369 static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
1371 constraintExpr temp;
1372 c->ar = flipAr (c->ar);
1376 DPRINTF(("Swaped left and right sides of constraint"));
1382 constraint constraint_simplify ( /*@returned@*/ constraint c)
1385 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
1387 if (constraint_tooDeep(c))
1389 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
1394 c->lexpr = constraintExpr_simplify (c->lexpr);
1395 c->expr = constraintExpr_simplify (c->expr);
1397 if (constraintExpr_isBinaryExpr (c->lexpr) )
1399 c = constraint_solve (c);
1401 c->lexpr = constraintExpr_simplify (c->lexpr);
1402 c->expr = constraintExpr_simplify (c->expr);
1405 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
1407 c = constraint_swapLeftRight(c);
1408 /*I don't think this will be an infinate loop*/
1409 c = constraint_simplify(c);
1412 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1420 /* returns true if fileloc for term1 is closer to file for term2 than term3*/
1422 bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1425 if (!fileloc_isDefined (loc1) )
1428 if (!fileloc_isDefined (loc2) )
1431 if (!fileloc_isDefined (loc3) )
1437 if (fileloc_equal (loc2, loc3) )
1440 if (fileloc_equal (loc1, loc2) )
1443 if (fileloc_equal (loc1, loc3) )
1446 if ( fileloc_lessthan (loc1, loc2) )
1448 if (fileloc_lessthan (loc2, loc3) )
1450 llassert (fileloc_lessthan (loc1, loc3) );
1459 if ( !fileloc_lessthan (loc1, loc2) )
1461 if (!fileloc_lessthan (loc2, loc3) )
1463 llassert (!fileloc_lessthan (loc1, loc3) );