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"
41 /*@access constraint, exprNode @*/ /*!!! NO! Don't do this so recklessly - design your code more carefully so you don't need to! */
43 static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
44 static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
46 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
47 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
49 static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
51 static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
53 static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
55 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
58 /*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
62 ret = constraintList_mergeEnsures (list1, list2);
64 constraintList_free(list1);
68 /*@only@*/ constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2)
73 llassert(constraintList_isDefined(list1) );
74 llassert(constraintList_isDefined(list2) );
76 DPRINTF(( message ("constraintList_mergeEnsures: list1 %s list2 %s",
77 constraintList_print(list1), constraintList_print(list2)
80 ret = constraintList_fixConflicts (list1, list2);
81 ret = reflectChangesEnsuresFree1 (ret, list2);
82 temp = constraintList_subsumeEnsures (ret, list2);
83 constraintList_free(ret);
86 temp = constraintList_subsumeEnsures (list2, ret);
88 temp = constraintList_addList (temp, ret);
89 constraintList_free(ret);
91 DPRINTF(( message ("constraintList_mergeEnsures: returning %s ",
92 constraintList_print(temp) )
100 /*@only@*/ constraintList constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1, constraintList list2)
104 ret = constraintList_mergeRequires(list1, list2);
106 constraintList_free(list1);
111 /*@only@*/ constraintList constraintList_mergeRequires (constraintList list1, constraintList list2)
116 DPRINTF((message ("constraintList_mergeRequires: merging %s and %s ", constraintList_print (list1), constraintList_print(list2) ) ) );
118 if (context_getFlag (FLG_REDUNDANTCONSTRAINTS) )
120 ret = constraintList_copy(list1);
121 ret = constraintList_addList(ret, list2);
125 /* get constraints in list1 not satified by list2 */
126 temp = constraintList_reflectChanges(list1, list2);
127 DPRINTF((message ("constraintList_mergeRequires: temp = %s", constraintList_print(temp) ) ) );
129 /*get constraints in list2 not satified by temp*/
130 ret = constraintList_reflectChanges(list2, temp);
132 DPRINTF((message ("constraintList_mergeRequires: ret = %s", constraintList_print(ret) ) ) );
134 ret = constraintList_addListFree (ret, temp);
136 DPRINTF((message ("constraintList_mergeRequires: returning %s", constraintList_print(ret) ) ) );
141 /* old name mergeResolve renamed for czech naming convention */
142 void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
144 constraintList temp, temp2;
146 DPRINTF((message ("magically merging constraint into parent:%s for", exprNode_unparse (parent) )) );
148 DPRINTF((message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2) ) ) );
151 if (exprNode_isUndefined(parent) )
153 llassert (exprNode_isDefined(parent) );
158 if (exprNode_isError (child1) || exprNode_isError(child2) )
160 if (exprNode_isError (child1) && !exprNode_isError(child2) )
162 constraintList_free(parent->requiresConstraints);
164 parent->requiresConstraints = constraintList_copy (child2->requiresConstraints);
165 constraintList_free(parent->ensuresConstraints);
167 parent->ensuresConstraints = constraintList_copy (child2->ensuresConstraints);
168 DPRINTF((message ("Copied child constraints: pre: %s and post: %s",
169 constraintList_print( child2->requiresConstraints),
170 constraintList_print (child2->ensuresConstraints)
177 llassert(exprNode_isError(child2) );
182 llassert(!exprNode_isError (child1) && ! exprNode_isError(child2) );
184 DPRINTF((message ("Child constraints are %s %s and %s %s",
185 constraintList_print (child1->requiresConstraints),
186 constraintList_print (child1->ensuresConstraints),
187 constraintList_print (child2->requiresConstraints),
188 constraintList_print (child2->ensuresConstraints)
192 constraintList_free(parent->requiresConstraints);
194 parent->requiresConstraints = constraintList_copy (child1->requiresConstraints);
196 if ( context_getFlag (FLG_ORCONSTRAINT) )
197 temp = constraintList_reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
199 temp = constraintList_reflectChanges(child2->requiresConstraints, child1->ensuresConstraints);
201 temp2 = constraintList_mergeRequires (parent->requiresConstraints, temp);
202 constraintList_free(parent->requiresConstraints);
203 constraintList_free(temp);
205 parent->requiresConstraints = temp2;
207 DPRINTF((message ("Parent requires constraints are %s ",
208 constraintList_print (parent->requiresConstraints)
211 constraintList_free(parent->ensuresConstraints);
213 parent->ensuresConstraints = constraintList_mergeEnsures(child1->ensuresConstraints,
214 child2->ensuresConstraints);
217 DPRINTF((message ("Parent constraints are %s and %s ",
218 constraintList_print (parent->requiresConstraints),
219 constraintList_print (parent->ensuresConstraints)
224 /*@only@*/ constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2)
227 ret = constraintList_makeNew();
228 constraintList_elements (list1, el)
231 DPRINTF ((message ("Examining %s", constraint_print (el) ) ) );
232 if (!constraintList_resolve (el, list2) )
235 temp = constraint_copy(el);
236 ret = constraintList_add (ret, temp);
240 DPRINTF ((message ("Subsuming %s", constraint_print (el) ) ) );
242 } end_constraintList_elements;
249 /*used to be reflectChangesFreePre renamed for Czech naming conventino*/
250 /* tries to resolve constraints in list pre2 using post1 */
251 /*@only@*/ constraintList constraintList_reflectChangesFreePre (/*@only@*/ constraintList pre2, /*@observer@*/ constraintList post1)
255 ret = constraintList_reflectChanges(pre2, post1);
257 constraintList_free (pre2);
262 /* tries to resolve constraints in list pre2 using post1 */
264 static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
271 llassert (! context_getFlag (FLG_ORCONSTRAINT) );
273 ret = constraintList_makeNew();
274 DPRINTF((message ("reflectChanges: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
276 constraintList_elements (pre2, el)
278 if (!constraintList_resolve (el, post1) )
280 temp = constraint_substitute (el, post1);
281 if (!constraintList_resolve (temp, post1) )
283 /* try inequality substitution
284 the inequality substitution may cause us to lose information
285 so we don't want to store the result but we do it anyway
287 temp2 = constraint_copy (temp);
288 temp2 = inequalitySubstitute (temp2, post1);
289 if (!constraintList_resolve (temp2, post1) )
291 temp2 = inequalitySubstituteUnsound (temp2, post1);
292 if (!constraintList_resolve (temp2, post1) )
293 ret = constraintList_add (ret, temp2);
295 constraint_free(temp2);
299 constraint_free(temp2);
302 constraint_free(temp);
304 } end_constraintList_elements;
306 DPRINTF((message ("reflectChanges: returning %s", constraintList_print(ret) ) ) );
310 /* tries to resolve constraints in list pre2 using post1 */
311 /*@only@*/ constraintList constraintList_reflectChanges(/*@observer@*/ constraintList pre2, /*@observer@*/ constraintList post1)
315 if ( context_getFlag (FLG_ORCONSTRAINT) )
317 temp = constraintList_reflectChangesOr (pre2, post1);
319 temp = reflectChangesNoOr(pre2, post1);
324 static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
328 llassert(constraint_isDefined(orig) );
332 DPRINTF((message("constraint_addor: oring %s onto %s", constraint_printOr(orConstr), constraint_printOr(orig) ) ));
334 while (c->or != NULL)
339 c->or = constraint_copy(orConstr);
341 DPRINTF((message("constraint_addor: returning %s",constraint_printOr(orig) ) ));
347 static bool resolveOr ( /*@temp@*/ constraint c, /*@observer@*/ /*@temp@*/ constraintList list)
355 llassert(constraint_isDefined(c) );
357 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_isDefined (c ) );
382 DPRINTF((message("doResolve:: call on constraint c = : %q and constraintList %q",
383 constraint_printOr(c), constraintList_print(post1)
387 if (!resolveOr (c, post1) )
390 temp = constraint_substitute (c, post1);
392 DPRINTF((message("doResolve:: after substitute temp is %q",
393 constraint_printOr(temp)
397 if (!resolveOr (temp, post1) )
399 /* try inequality substitution */
402 /* the inequality substitution may cause us to lose information
403 so we don't want to store the result but we do anyway
405 temp2 = constraint_copy (c);
406 temp2 = inequalitySubstitute (temp2, post1);
408 if (!resolveOr (temp2, post1) )
411 temp3 = constraint_copy(temp2);
413 temp3 = inequalitySubstituteStrong (temp3, post1);
414 if (!resolveOr (temp3, post1) )
416 temp2 = inequalitySubstituteUnsound (temp2, post1);
417 if (!resolveOr (temp2, post1) )
419 if (!constraint_same (temp, temp2) )
421 /* drl added 8/28/2002*/
422 /*make sure that the information from
423 a post condition like i = i + 1 is transfered
426 tempSub = constraint_substitute (temp2, post1);
429 message("doResolve: adding %s ",
430 constraint_printOr(tempSub)
435 message("doResolve: not adding %s ",
436 constraint_printOr(temp2)
440 temp = constraint_addOr (temp, tempSub);
441 constraint_free(tempSub);
444 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
446 /* drl added 8/28/2002*/
447 /*make sure that the information from
448 a post condition like i = i + 1 is transfered
452 tempSub = constraint_substitute (temp3, post1);
455 message("doResolve: adding %s ",
456 constraint_printOr(tempSub)
462 message("doResolve: not adding %s ",
463 constraint_printOr(temp3)
467 temp = constraint_addOr (temp, tempSub);
469 constraint_free(tempSub);
473 constraint_free(temp2);
474 constraint_free(temp3);
479 constraint_free(temp2);
480 constraint_free(temp3);
484 constraint_free(temp2);
485 constraint_free(temp3);
490 constraint_free(temp2);
494 constraint_free(temp);
502 static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
508 DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
512 llassert(constraint_isDefined(c) );
514 ret = constraint_copy(c);
516 llassert(constraint_isDefined(ret) );
518 if (constraintList_isEmpty(post1) )
526 ret = doResolve (ret, post1, resolved);
531 constraint_free(next);
533 /*we don't need to free ret when resolved is false because ret is null*/
534 llassert(ret == NULL);
545 curr = doResolve (curr, post1, resolved);
549 /* curr is null so we don't try to free it*/
550 llassert(curr == NULL);
553 constraint_free(next);
555 constraint_free(ret);
558 ret = constraint_addOr (ret, curr);
559 constraint_free(curr);
564 /* tries to resolve constraints in list pr2 using post1 */
565 /*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
570 ret = constraintList_makeNew();
571 DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
573 constraintList_elements (pre2, el)
575 temp = doResolveOr (el, post1, &resolved);
579 ret = constraintList_add(ret, temp);
583 /* we don't need to free temp when
584 resolved is false because temp is null */
585 llassert(temp == NULL);
588 } end_constraintList_elements;
590 DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
594 static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
598 ret = constraintList_makeNew();
599 constraintList_elements (pre2, el)
601 if (!constraintList_resolve (el, post1) )
603 temp = constraint_substitute (el, post1);
604 llassert (temp != NULL);
606 if (!constraintList_resolve (temp, post1) )
607 ret = constraintList_add (ret, temp);
609 constraint_free(temp);
613 DPRINTF ((message ("Resolved away %s ", constraint_print(el) ) ) );
615 } end_constraintList_elements;
621 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
625 ret = reflectChangesEnsures (pre2, post1);
627 constraintList_free(pre2);
633 static bool constraint_conflict (constraint c1, constraint c2)
635 if (!constraint_isDefined(c1) || !constraint_isDefined(c2))
640 if (constraintExpr_similar (c1->lexpr, c2->lexpr))
643 if (c1->ar == c2->ar)
645 DPRINTF (("%s conflicts with %s", constraint_unparse (c1), constraint_unparse (c2)));
650 /* This is a slight kludge to prevent circular constraints like
651 strlen(str) == maxRead(s) + strlen(str);
654 /*this code is functional but it may be worth cleaning up at some point. */
657 if (c1->ar == c2->ar)
659 if (constraintExpr_search (c1->lexpr, c2->expr) )
660 if (constraintExpr_isTerm(c1->lexpr) )
664 term = constraintExpr_getTerm(c1->lexpr);
666 if (constraintTerm_isExprNode(term) )
668 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
674 if (constraint_tooDeep(c1) || constraint_tooDeep(c2) )
676 DPRINTF ((message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) );
680 DPRINTF ((message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
686 static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
688 llassert(constraint_isDefined(conflicting) );
690 if (conflicting->ar == EQ)
692 llassert (constraint_isDefined(good));
693 DPRINTF (("Replacing here!"));
694 good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
695 good = constraint_simplify (good);
701 static bool conflict (constraint c, constraintList list)
704 constraintList_elements (list, el)
706 if ( constraint_conflict(el, c) )
708 constraint_fixConflict (el, c);
711 } end_constraintList_elements;
718 check if constraint in list1 conflicts with constraints in List2. If so we
719 remove form list1 and change list2.
722 constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
725 ret = constraintList_makeNew();
726 llassert(constraintList_isDefined(list1) );
727 constraintList_elements (list1, el)
729 if (! conflict (el, list2) )
732 temp = constraint_copy(el);
733 ret = constraintList_add (ret, temp);
735 } end_constraintList_elements;
740 /*returns true if constraint post satisfies cosntriant pre */
742 static bool constraintResolve_satisfies (constraint pre, constraint post)
744 if (!constraint_isDefined (pre))
749 if (!constraint_isDefined(post))
754 if (constraint_isAlwaysTrue (pre))
757 if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
762 if (constraintExpr_isUndefined(post->expr))
768 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
772 bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c,
773 /*@temp@*/ /*@observer@*/ constraintList p)
775 DPRINTF (("[resolve] Trying to resolve constraint: %s using %s",
776 constraint_unparse (c),
777 constraintList_unparse (p)));
779 constraintList_elements (p, el)
781 if (constraintResolve_satisfies (c, el))
783 DPRINTF (("constraintList_resolve: %s satifies %s",
784 constraint_unparse (el), constraint_unparse (c)));
788 DPRINTF (("constraintList_resolve: %s does not satify %s\n ",
789 constraint_unparse (el), constraint_unparse (c)));
791 end_constraintList_elements;
793 DPRINTF (("No constraints satify: %s", constraint_unparse (c)));
797 static bool arithType_canResolve (arithType ar1, arithType ar2)
803 if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ))
816 if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ))
825 /*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
826 static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
832 llassert(constraintExpr_isDefined(buf1) && constraintExpr_isDefined(expr2) );
834 /*@access constraintExpr@*/
836 if ((expr2->kind != term) && (buf1->kind != term) )
840 ct = constraintExprData_termGetTerm(expr2->data);
842 if (!constraintTerm_isExprNode(ct) )
845 e = constraintTerm_getExprNode(ct);
847 llassert (exprNode_isDefined(e));
849 if (! (exprNode_isDefined(e)))
852 if (e->kind != XPR_SIZEOF)
855 t = exprData_getSingle (e->edata);
856 s1 = exprNode_getSref (t);
858 s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) );
860 /*drl this may be the wronge thing to test for but this
861 seems to work correctly*/
862 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
864 /* origly checked that ctype_isFixedArray(sRef_getType(s2)) but
871 /* look for the special case of
872 maxSet(buf) >= sizeof(buf) - 1
875 /*drl eventually it would be good to check that
876 buf is of type char.*/
878 static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
880 constraintExpr l, r, buf1, buf2, con;
882 DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
885 llassert (constraint_isDefined(c) );
890 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
893 llassert (constraintExpr_isDefined(l) );
894 llassert (constraintExpr_isDefined(r) );
896 /*check if the constraintExpr is MaxSet(buf) */
897 if (l->kind == unaryExpr)
899 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
901 buf1 = constraintExprData_unaryExprGetExpr(l->data);
910 if (r->kind != binaryexpr)
913 buf2 = constraintExprData_binaryExprGetExpr1(r->data);
914 con = constraintExprData_binaryExprGetExpr2(r->data);
916 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS)
918 if (constraintExpr_canGetValue(con) )
922 i = constraintExpr_getValue(con);
932 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS)
934 if (constraintExpr_canGetValue(con) )
938 i = constraintExpr_getValue(con);
948 if (sizeofBufComp(buf1, buf2))
957 /*@noaccess constraintExpr@*/
959 /* We look for constraint which are tautologies */
961 bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
968 llassert (constraint_isDefined(c) );
973 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
975 if (sizeOfMaxSet(c) )
978 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
981 cmp = constraintExpr_compare (l, r);
1002 if (constraintExpr_similar (l,r))
1021 l = constraintExpr_copy (c->lexpr);
1022 r = constraintExpr_copy (c->expr);
1024 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
1026 if (constraintExpr_similar (l,r) && (rHasConstant ) )
1028 DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
1029 DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
1031 constraintExpr_free(l);
1032 constraintExpr_free(r);
1037 return (rConstant == 0);
1039 return (rConstant > 0);
1041 return (rConstant >= 0);
1043 return (rConstant <= 0);
1045 return (rConstant < 0);
1055 constraintExpr_free(l);
1056 constraintExpr_free(r);
1057 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
1064 static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
1067 DPRINTF (("Doing range check %s and %s",
1068 constraintExpr_unparse (expr1), constraintExpr_unparse (expr2)));
1070 if (!arithType_canResolve (ar1, ar2))
1076 if (constraintExpr_similar (expr1, expr2) )
1080 if (! (constraintExpr_canGetValue (expr1) &&
1081 constraintExpr_canGetValue (expr2) ) )
1083 constraintExpr e1, e2;
1087 e1 = constraintExpr_copy(expr1);
1088 e2 = constraintExpr_copy(expr2);
1090 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1091 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1101 if (const1 <= const2)
1102 if (constraintExpr_similar (e1, e2) )
1104 constraintExpr_free(e1);
1105 constraintExpr_free(e2);
1109 DPRINTF(("Can't Get value"));
1111 constraintExpr_free(e1);
1112 constraintExpr_free(e2);
1116 if (constraintExpr_compare (expr2, expr1) >= 0)
1121 if (constraintExpr_similar (expr1, expr2) )
1126 if (constraintExpr_similar (expr1, expr2) )
1130 if (! (constraintExpr_canGetValue (expr1) &&
1131 constraintExpr_canGetValue (expr2) ) )
1133 constraintExpr e1, e2;
1137 e1 = constraintExpr_copy(expr1);
1138 e2 = constraintExpr_copy(expr2);
1140 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1142 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1152 if (const1 >= const2)
1153 if (constraintExpr_similar (e1, e2) )
1155 constraintExpr_free(e1);
1156 constraintExpr_free(e2);
1160 constraintExpr_free(e1);
1161 constraintExpr_free(e2);
1163 DPRINTF(("Can't Get value"));
1167 if (constraintExpr_compare (expr2, expr1) <= 0)
1173 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
1178 static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
1180 llassert (constraint_isDefined(c));
1182 DPRINTF (("Starting replace lexpr [%p]: %s < %s ==> %s > in %s", c,
1183 constraintExpr_unparse (c->lexpr),
1184 constraintExpr_unparse (old), constraintExpr_unparse (newExpr),
1185 constraint_unparse (c)));
1186 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
1187 DPRINTF (("Finished replace lexpr [%p]: %s", c, constraintExpr_unparse (c->lexpr)));
1188 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
1192 bool constraint_search (constraint c, constraintExpr old) /*@*/
1197 llassert (constraint_isDefined (c));
1199 ret = constraintExpr_search (c->lexpr, old);
1200 ret = ret || constraintExpr_search (c->expr, old);
1204 /* adjust file locs and stuff */
1205 static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
1207 fileloc loc1, loc2, loc3;
1209 DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute),
1210 constraint_print(old))
1213 llassert(constraint_isDefined(substitute));
1214 llassert(constraint_isDefined(old));
1216 loc1 = constraint_getFileloc (old);
1217 loc2 = constraintExpr_loc (substitute->lexpr);
1218 loc3 = constraintExpr_loc (substitute->expr);
1220 /* special case of an equality that "contains itself" */
1221 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
1222 if (fileloc_closer (loc1, loc3, loc2))
1224 constraintExpr temp;
1225 DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) )
1227 temp = substitute->lexpr;
1228 substitute->lexpr = substitute->expr;
1229 substitute->expr = temp;
1230 substitute = constraint_simplify(substitute);
1233 fileloc_free (loc1);
1234 fileloc_free (loc2);
1235 fileloc_free (loc3);
1241 /* If function preforms substitutes based on inequality
1243 It uses the rule x >= y && b < y ===> x >= b + 1
1245 Warning this is sound but throws out information
1248 constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
1250 llassert(constraint_isDefined(c) );
1255 constraintList_elements (p, el)
1258 llassert(constraint_isDefined(el) );
1260 if ((el->ar == LT ) )
1262 constraintExpr temp2;
1264 if (constraintExpr_same (el->expr, c->expr) )
1266 DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
1267 constraintExpr_print (c->expr),
1268 constraint_print (c),
1269 constraintExpr_print (el->expr) )
1271 temp2 = constraintExpr_copy (el->lexpr);
1272 constraintExpr_free(c->expr);
1273 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1279 end_constraintList_elements;
1281 c = constraint_simplify(c);
1288 THis function is like inequalitySubstitute but it adds the rule
1289 added the rules x >= y && y <= b ===> x >= b
1290 x >= y && y < b ===> x >= b + 1
1292 This is sound but sonce it throws out additional information it should only one used
1293 if we're oring constraints.
1296 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1298 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
1300 llassert(constraint_isDefined(c) );
1302 if (! (constraint_isDefined(c) ) )
1310 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1311 constraint_print(c), constraintList_print(p) ) ));
1312 constraintList_elements (p, el)
1315 DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1317 llassert(constraint_isDefined(el) );
1318 if ((el->ar == LT ) || (el->ar == LTE ) )
1320 constraintExpr temp2;
1322 if (constraintExpr_same (el->lexpr, c->expr) )
1324 DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
1325 constraintExpr_print (c->expr),
1326 constraint_print (c),
1327 constraintExpr_print (el->expr) )
1329 temp2 = constraintExpr_copy (el->expr);
1330 constraintExpr_free(c->expr);
1331 if ((el->ar == LTE ) )
1337 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1343 end_constraintList_elements;
1345 c = constraint_simplify(c);
1350 /* This function performs substitutions based on the rule:
1351 for a constraint of the form expr1 >= expr2; a < b =>
1352 a = b -1 for all a in expr1. This will work in most cases.
1354 Like inequalitySubstitute we're throwing away some information
1357 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
1359 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1361 llassert(constraint_isDefined(c) );
1366 constraintList_elements (p, el)
1369 llassert(constraint_isDefined(el) );
1371 DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1372 if (( el->ar == LTE) || (el->ar == LT) )
1374 constraintExpr temp2;
1376 temp2 = constraintExpr_copy (el->expr);
1379 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1381 DPRINTF((message ("Replacing %s in %s with %s",
1382 constraintExpr_print (el->lexpr),
1383 constraintExpr_print (c->lexpr),
1384 constraintExpr_print (temp2) ) ));
1386 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
1387 constraintExpr_free(temp2);
1390 end_constraintList_elements;
1392 c = constraint_simplify(c);
1396 /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
1398 constraint ret = constraint_copy (c);
1400 constraintList_elements (p, el)
1402 if (constraint_isDefined (el))
1405 if (!constraint_conflict (ret, el))
1407 constraint temp = constraint_copy(el);
1408 temp = constraint_adjust(temp, ret);
1410 llassert(constraint_isDefined(temp) );
1413 DPRINTF (("constraint_substitute :: Substituting in %s using %s",
1414 constraint_print (ret), constraint_print (temp)));
1416 ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
1417 DPRINTF (("constraint_substitute :: The new constraint is %s", constraint_print (ret)));;
1418 constraint_free(temp);
1422 end_constraintList_elements;
1424 ret = constraint_simplify (ret);
1425 DPRINTF(( message (" constraint_substitute :: The final new constraint is %s", constraint_print (ret) ) ));
1430 /*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
1434 ret = constraintList_substitute (target, subList);
1436 constraintList_free(target);
1441 /* we try to do substitutions on each constraint in target using the constraint in sublist*/
1443 /*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
1448 ret = constraintList_makeNew();
1450 constraintList_elements(target, el)
1453 /* drl possible problem : warning make sure that a side effect is not expected */
1455 temp = constraint_substitute(el, subList);
1456 ret = constraintList_add (ret, temp);
1458 end_constraintList_elements;
1463 static constraint constraint_solve (/*@returned@*/ constraint c)
1466 llassert(constraint_isDefined(c) );
1468 DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) );
1469 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
1470 DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) );
1475 static arithType flipAr (arithType ar)
1490 llcontbug (message("unexpected value: case not handled"));
1495 static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
1497 constraintExpr temp;
1499 llassert(constraint_isDefined(c) );
1501 c->ar = flipAr (c->ar);
1505 DPRINTF(("Swaped left and right sides of constraint"));
1511 constraint constraint_simplify ( /*@returned@*/ constraint c)
1514 llassert(constraint_isDefined(c) );
1516 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
1518 if (constraint_tooDeep(c))
1520 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
1525 c->lexpr = constraintExpr_simplify (c->lexpr);
1526 c->expr = constraintExpr_simplify (c->expr);
1528 if (constraintExpr_isBinaryExpr (c->lexpr) )
1530 c = constraint_solve (c);
1532 c->lexpr = constraintExpr_simplify (c->lexpr);
1533 c->expr = constraintExpr_simplify (c->expr);
1536 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
1538 c = constraint_swapLeftRight(c);
1539 /*I don't think this will be an infinate loop*/
1540 c = constraint_simplify(c);
1543 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1551 /* returns true if fileloc for term1 is closer to file for term2 than term3*/
1553 bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1556 if (!fileloc_isDefined (loc1) )
1559 if (!fileloc_isDefined (loc2) )
1562 if (!fileloc_isDefined (loc3) )
1568 if (fileloc_equal (loc2, loc3) )
1571 if (fileloc_equal (loc1, loc2) )
1574 if (fileloc_equal (loc1, loc3) )
1577 if ( fileloc_lessthan (loc1, loc2) )
1579 if (fileloc_lessthan (loc2, loc3) )
1581 llassert (fileloc_lessthan (loc1, loc3) );
1590 if ( !fileloc_lessthan (loc1, loc2) )
1592 if (!fileloc_lessthan (loc2, loc3) )
1594 llassert (!fileloc_lessthan (loc1, loc3) );