2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
27 ** constraintResolve.c
30 /* #define DEBUGPRINT 1 */
32 # include <ctype.h> /* for isdigit */
33 # include "splintMacros.nf"
35 # include "cgrammar.h"
36 # include "cgrammar_tokens.h"
38 # include "exprChecks.h"
39 # include "exprNodeSList.h"
42 /*@access constraint, exprNode @*/ /*!!! NO! Don't do this so recklessly - design your code more carefully so you don't need to! */
44 static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
47 static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
49 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
51 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
53 static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
56 static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
58 static bool constraint_resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
60 static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
62 /*@only@*/ constraintList
63 constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
65 constraintList ret = constraintList_mergeEnsures (list1, list2);
66 constraintList_free (list1);
70 /*@only@*/ constraintList
71 constraintList_mergeEnsures (constraintList list1, constraintList list2)
76 llassert (constraintList_isDefined (list1));
77 llassert (constraintList_isDefined (list2));
79 DPRINTF (("constraintList_mergeEnsures: list1 %s list2 %s",
80 constraintList_unparse (list1), constraintList_unparse (list2)));
82 ret = constraintList_fixConflicts (list1, list2);
83 ret = reflectChangesEnsuresFree1 (ret, list2);
84 temp = constraintList_subsumeEnsures (ret, list2);
85 constraintList_free (ret);
88 temp = constraintList_subsumeEnsures (list2, ret);
89 temp = constraintList_addList (temp, ret);
90 constraintList_free (ret);
92 DPRINTF (("constraintList_mergeEnsures: returning %s", constraintList_unparse (temp)));
96 /*@only@*/ constraintList
97 constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1,
100 constraintList ret = constraintList_mergeRequires (list1, list2);
101 constraintList_free (list1);
105 /*@only@*/ constraintList
106 constraintList_mergeRequires (constraintList list1, constraintList list2)
111 DPRINTF (("constraintList_mergeRequires: merging %s and %s",
112 constraintList_unparse (list1), constraintList_unparse (list2)));
114 if (context_getFlag (FLG_REDUNDANTCONSTRAINTS))
116 ret = constraintList_copy (list1);
117 ret = constraintList_addList (ret, list2);
121 /* get constraints in list1 not satified by list2 */
122 temp = constraintList_reflectChanges (list1, list2);
123 DPRINTF (("constraintList_mergeRequires: temp = %s", constraintList_unparse (temp)));
125 /* get constraints in list2 not satified by temp*/
126 ret = constraintList_reflectChanges (list2, temp);
127 DPRINTF (("constraintList_mergeRequires: ret = %s", constraintList_unparse(ret)));
129 ret = constraintList_addListFree (ret, temp);
130 DPRINTF (("constraintList_mergeRequires: returning %s", constraintList_unparse(ret)));
135 /* old name mergeResolve renamed for czech naming convention */
136 void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
138 constraintList temp, temp2;
140 DPRINTF((message ("magically merging constraint into parent:%s for", exprNode_unparse (parent))));
142 DPRINTF((message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2))));
144 if (exprNode_isUndefined (parent))
146 llassert (exprNode_isDefined (parent));
151 if (exprNode_isError (child1) || exprNode_isError (child2))
153 if (exprNode_isError (child1) && !exprNode_isError (child2))
155 constraintList_free (parent->requiresConstraints);
156 parent->requiresConstraints = constraintList_copy (child2->requiresConstraints);
157 constraintList_free (parent->ensuresConstraints);
159 parent->ensuresConstraints = constraintList_copy (child2->ensuresConstraints);
160 DPRINTF (("Copied child constraints: pre: %s and post: %s",
161 constraintList_unparse(child2->requiresConstraints),
162 constraintList_unparse (child2->ensuresConstraints)));
167 llassert (exprNode_isError (child2));
172 llassert (!exprNode_isError (child1) && !exprNode_isError (child2));
174 DPRINTF (("Child constraints are %s %s and %s %s",
175 constraintList_unparse (child1->requiresConstraints),
176 constraintList_unparse (child1->ensuresConstraints),
177 constraintList_unparse (child2->requiresConstraints),
178 constraintList_unparse (child2->ensuresConstraints)));
180 constraintList_free (parent->requiresConstraints);
181 parent->requiresConstraints = constraintList_copy (child1->requiresConstraints);
183 if (context_getFlag (FLG_ORCONSTRAINT))
185 temp = constraintList_reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
189 temp = constraintList_reflectChanges(child2->requiresConstraints, child1->ensuresConstraints);
192 temp2 = constraintList_mergeRequires (parent->requiresConstraints, temp);
193 constraintList_free (parent->requiresConstraints);
194 constraintList_free (temp);
196 parent->requiresConstraints = temp2;
198 DPRINTF (("Parent requires constraints are %s ",
199 constraintList_unparse (parent->requiresConstraints)));
201 constraintList_free (parent->ensuresConstraints);
203 parent->ensuresConstraints = constraintList_mergeEnsures (child1->ensuresConstraints,
204 child2->ensuresConstraints);
207 DPRINTF (("Parent constraints are %s and %s ",
208 constraintList_unparse (parent->requiresConstraints),
209 constraintList_unparse (parent->ensuresConstraints)));
212 /*@only@*/ constraintList
213 constraintList_subsumeEnsures (constraintList list1, constraintList list2)
215 constraintList ret = constraintList_makeNew ();
217 constraintList_elements (list1, el)
219 DPRINTF (("Examining %s", constraint_unparse (el)));
220 if (!constraintList_resolve (el, list2))
222 constraint temp = constraint_copy (el);
223 ret = constraintList_add (ret, temp);
227 DPRINTF (("Subsuming %s", constraint_unparse (el)));
229 } end_constraintList_elements;
234 /* tries to resolve constraints in list pre2 using post1 */
235 /*@only@*/ constraintList
236 constraintList_reflectChangesFreePre (/*@only@*/ constraintList pre2, /*@observer@*/ constraintList post1)
238 constraintList ret = constraintList_reflectChanges(pre2, post1);
239 constraintList_free (pre2);
245 /* tries to resolve constraints in list pre2 using post1 */
246 static /*@only@*/ constraintList
247 constraintList_reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2,
248 /*@observer@*/ /*@temp@*/ constraintList post1)
250 constraintList ret = constraintList_makeNew ();
252 llassert (!context_getFlag (FLG_ORCONSTRAINT));
254 DPRINTF (("reflectChanges: lists %s and %s",
255 constraintList_unparse (pre2), constraintList_unparse (post1)));
257 constraintList_elements (pre2, el)
259 if (!constraintList_resolve (el, post1))
261 constraint temp = constraint_substitute (el, post1);
263 if (!constraintList_resolve (temp, post1))
265 /* try inequality substitution
266 the inequality substitution may cause us to lose information
267 so we don't want to store the result but we do it anyway
269 constraint temp2 = constraint_copy (temp);
270 temp2 = inequalitySubstitute (temp2, post1);
272 if (!constraintList_resolve (temp2, post1))
274 temp2 = inequalitySubstituteUnsound (temp2, post1);
275 if (!constraintList_resolve (temp2, post1))
277 ret = constraintList_add (ret, temp2);
281 constraint_free (temp2);
286 constraint_free (temp2);
289 constraint_free (temp);
291 } end_constraintList_elements;
293 DPRINTF (("reflectChanges: returning %s", constraintList_unparse(ret)));
297 /* tries to resolve constraints in list pre2 using post1 */
298 /*@only@*/ constraintList
299 constraintList_reflectChanges (/*@observer@*/ constraintList pre2, /*@observer@*/ constraintList post1)
301 if (context_getFlag (FLG_ORCONSTRAINT))
303 return constraintList_reflectChangesOr (pre2, post1);
307 return constraintList_reflectChangesNoOr (pre2, post1);
312 constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
315 llassert (constraint_isDefined (c));
317 DPRINTF (("constraint_addor: oring %s onto %s",
318 constraint_unparseOr (orConstr), constraint_unparseOr (orig)));
320 while (c->or != NULL)
325 c->or = constraint_copy (orConstr);
327 DPRINTF (("constraint_addor: returning %s",constraint_unparseOr (orig)));
331 static bool constraint_resolveOr (/*@temp@*/ constraint c, /*@observer@*/ /*@temp@*/ constraintList list)
336 llassert (constraint_isDefined(c));
337 DPRINTF (("constraint_resolveOr: constraint %s and list %s",
338 constraint_printOr (c), constraintList_print (list)));
342 if (constraintList_resolve (temp, list))
349 llassert(numberOr <= 10);
351 while (constraint_isDefined (temp));
356 /* This is a "helper" function for doResolveOr */
358 static /*@only@*/ constraint doResolve (/*@only@*/ constraint c, constraintList post1, bool * resolved)
360 llassert (constraint_isDefined (c));
362 DPRINTF (("doResolve:: call on constraint c = : %q and constraintList %q",
363 constraint_unparseOr (c), constraintList_unparse (post1)));
365 if (!constraint_resolveOr (c, post1))
367 constraint temp = constraint_substitute (c, post1);
368 DPRINTF (("doResolve:: after substitute temp is %q", constraint_unparseOr (temp)));
370 if (!constraint_resolveOr (temp, post1))
372 /* try inequality substitution */
373 constraint temp2 = constraint_copy (c);
376 ** the inequality substitution may cause us to lose information
377 ** so we don't want to store the result but we do anyway
380 temp2 = inequalitySubstitute (temp2, post1);
382 if (!constraint_resolveOr (temp2, post1))
384 constraint temp3 = constraint_copy(temp2);
385 temp3 = inequalitySubstituteStrong (temp3, post1);
387 if (!constraint_resolveOr (temp3, post1))
389 temp2 = inequalitySubstituteUnsound (temp2, post1);
391 if (!constraint_resolveOr (temp2, post1))
393 if (!constraint_same (temp, temp2))
395 /* drl added 8/28/2002*/
396 /*make sure that the information from
397 a post condition like i = i + 1 is transfered
400 tempSub = constraint_substitute (temp2, post1);
402 DPRINTF (("doResolve: adding %s", constraint_unparseOr (tempSub)));
403 DPRINTF (("doResolve: not adding %s", constraint_unparseOr (temp2)));
405 temp = constraint_addOr (temp, tempSub);
406 constraint_free (tempSub);
409 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2))
411 /* drl added 8/28/2002*/
412 /* make sure that the information from
413 a post condition like i = i + 1 is transfered
417 tempSub = constraint_substitute (temp3, post1);
419 DPRINTF (("doResolve: adding %s", constraint_unparseOr (tempSub)));
420 DPRINTF (("doResolve: not adding %s", constraint_unparseOr(temp3)));
422 temp = constraint_addOr (temp, tempSub);
423 constraint_free(tempSub);
428 constraint_free (temp2);
429 constraint_free (temp3);
435 constraint_free (temp2);
436 constraint_free (temp3);
440 constraint_free (temp2);
441 constraint_free (temp3);
446 constraint_free (temp2);
450 constraint_free (temp);
454 /*@i523@*/ /*drl bee: pbr*/ *resolved = TRUE;
458 static /*@only@*/ constraint
459 doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/ bool *resolved)
465 DPRINTF (("doResolveOr: constraint %s and list %s",
466 constraint_unparseOr (c), constraintList_unparse (post1)));
468 /*@i523@*/ /*drl bee: pbr*/ *resolved = FALSE;
470 llassert (constraint_isDefined (c));
471 ret = constraint_copy (c);
473 llassert (constraint_isDefined (ret));
475 if (constraintList_isEmpty (post1))
483 ret = doResolve (ret, post1, resolved);
489 constraint_free (next);
492 /* we don't need to free ret when resolved is false because ret is null*/
493 llassert (ret == NULL);
503 curr = doResolve (curr, post1, resolved);
505 /*@i423@*/ /*drl bee: pbr*/ if (*resolved)
507 /* curr is null so we don't try to free it*/
508 llassert (curr == NULL);
512 constraint_free (next);
515 constraint_free (ret);
518 ret = constraint_addOr (ret, curr);
519 constraint_free(curr);
524 /* tries to resolve constraints in list pr2 using post1 */
525 /*@only@*/ constraintList
526 constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
531 ret = constraintList_makeNew();
533 DPRINTF (("constraintList_reflectChangesOr: lists %s and %s",
534 constraintList_unparse (pre2), constraintList_unparse (post1)));
536 constraintList_elements (pre2, el)
538 temp = doResolveOr (el, post1, &resolved);
542 ret = constraintList_add(ret, temp);
546 /* we don't need to free temp when
547 resolved is false because temp is null */
548 llassert (temp == NULL);
550 } end_constraintList_elements;
552 DPRINTF (("constraintList_reflectChangesOr: returning %s", constraintList_unparse (ret)));
556 static /*@only@*/ constraintList
557 reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
559 constraintList ret = constraintList_makeNew ();
561 constraintList_elements (pre2, el)
563 if (!constraintList_resolve (el, post1))
565 constraint temp = constraint_substitute (el, post1);
566 llassert (temp != NULL);
568 if (!constraintList_resolve (temp, post1))
570 ret = constraintList_add (ret, temp);
574 constraint_free (temp);
579 DPRINTF (("Resolved away %s ", constraint_unparse(el)));
581 } end_constraintList_elements;
587 static /*@only@*/ constraintList
588 reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
590 constraintList ret = reflectChangesEnsures (pre2, post1);
591 constraintList_free(pre2);
596 static bool constraint_conflict (constraint c1, constraint c2)
598 llassert (constraint_isDefined (c1));
599 llassert (constraint_isDefined (c2));
601 if (constraintExpr_similar (c1->lexpr, c2->lexpr))
603 if (c1->ar == EQ && (c1->ar == c2->ar))
605 DPRINTF (("%s conflicts with %s ", constraint_unparse (c1), constraint_unparse(c2)));
611 ** This is a slight kludge to prevent circular constraints like
612 ** strlen(str) == maxRead(s) + strlen(str);
615 if (c1->ar == EQ && (c1->ar == c2->ar))
617 if (constraintExpr_search (c1->lexpr, c2->expr))
619 if (constraintExpr_isTerm (c1->lexpr))
621 constraintTerm term = constraintExpr_getTerm (c1->lexpr);
623 if (constraintTerm_isExprNode (term))
625 DPRINTF (("%s conflicts with %s ", constraint_unparse (c1),
626 constraint_unparse (c2)));
633 if (constraint_tooDeep (c1) || constraint_tooDeep (c2))
635 DPRINTF (("%s conflicts with %s (constraint is too deep)",
636 constraint_unparse (c1), constraint_unparse (c2)));
640 DPRINTF (("%s doesn't conflict with %s",
641 constraint_unparse (c1), constraint_unparse (c2)));
646 constraint_fixConflict (/*@temp@*/ constraint good,
647 /*@temp@*/ /*@observer@*/ constraint conflicting)
650 llassert (constraint_isDefined (conflicting));
652 if (conflicting->ar ==EQ)
654 llassert (constraint_isDefined (good));
655 good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
656 good = constraint_simplify (good);
660 static bool conflict (constraint c, constraintList list)
662 constraintList_elements (list, el)
664 if (constraint_conflict (el, c))
666 constraint_fixConflict (el, c);
669 } end_constraintList_elements;
675 ** Check if constraint in list1 conflicts with constraints in List2. If so we
676 ** remove form list1 and change list2.
679 constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
681 constraintList ret = constraintList_makeNew ();
682 llassert (constraintList_isDefined (list1));
684 constraintList_elements (list1, el)
686 if (!conflict (el, list2))
688 constraint temp = constraint_copy (el);
689 ret = constraintList_add (ret, temp);
691 } end_constraintList_elements;
696 /* Returns true if constraint post satifies constraint pre */
697 static bool satifies (constraint pre, constraint post)
699 llassert (constraint_isDefined (pre));
700 llassert (constraint_isDefined (post));
702 if (constraint_isAlwaysTrue (pre))
707 if (!constraintExpr_similar (pre->lexpr, post->lexpr))
712 if (!constraintExpr_similar (pre->lexpr, post->lexpr))
717 llassertretval (!constraintExpr_isUndefined (post->expr), FALSE);
718 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
722 constraintList_resolve (/*@observer@*/ constraint c,
723 /*@observer@*/ constraintList p)
725 constraintList_elements (p, el)
727 if (satifies (c, el))
729 DPRINTF (("%s satifies %s", constraint_unparse (el), constraint_unparse(c)));
732 DPRINTF (("%s does not satify %s", constraint_unparse (el), constraint_unparse (c)));
733 } end_constraintList_elements;
735 DPRINTF (("no constraints satisfy %s", constraint_unparse (c)));
739 static bool arithType_canResolve (arithType ar1, arithType ar2)
745 if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ))
758 if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ))
767 /* Checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
768 static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
774 llassert (constraintExpr_isDefined (buf1) && constraintExpr_isDefined (expr2));
776 /*@i6343 rewrite this to not need access, or move to constraintExpr module */
777 /*@access constraintExpr@*/
779 if ((expr2->kind != term) && (buf1->kind != term))
784 ct = constraintExprData_termGetTerm(expr2->data);
786 if (!constraintTerm_isExprNode(ct))
791 e = constraintTerm_getExprNode (ct);
793 llassert (exprNode_isDefined(e));
795 if (!exprNode_isDefined (e))
800 if (e->kind != XPR_SIZEOF)
805 t = exprData_getSingle (e->edata);
807 s1 = exprNode_getSref (t);
808 s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data));
810 /*@i223@*/ /*this may be the wronge thing to test for */
811 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2))
813 /*@i22*/ /* get rid of this test of now */
814 /* if (ctype_isFixedArray (sRef_getType (s2))) */
820 /* look for the special case of
821 maxSet(buf) >= sizeof(buf) - 1
824 /*@i223@*/ /*need to add some type checking */
826 static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
828 constraintExpr l, r, buf1, buf2, con;
830 DPRINTF (("sizeOfMaxSet: checking %s ", constraint_unparse (c)));
831 llassert (constraint_isDefined (c));
836 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE)))
841 llassert (constraintExpr_isDefined (l));
842 llassert (constraintExpr_isDefined (r));
844 /* Check if the constraintExpr is MaxSet(buf) */
846 if (l->kind == unaryExpr)
848 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
850 buf1 = constraintExprData_unaryExprGetExpr(l->data);
862 if (r->kind != binaryexpr)
867 buf2 = constraintExprData_binaryExprGetExpr1 (r->data);
868 con = constraintExprData_binaryExprGetExpr2 (r->data);
870 if (constraintExprData_binaryExprGetOp (r->data) == BINARYOP_MINUS)
872 if (constraintExpr_canGetValue (con))
874 long i = constraintExpr_getValue (con);
887 if (constraintExprData_binaryExprGetOp (r->data) == BINARYOP_PLUS)
889 if (constraintExpr_canGetValue (con))
891 long i = constraintExpr_getValue (con);
904 return (sizeofBufComp (buf1, buf2));
907 /*@noaccess constraintExpr@*/
909 /* We look for constraint which are tautologies */
911 bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
917 llassert (constraint_isDefined (c));
922 DPRINTF((message("constraint_IsAlwaysTrue:examining %s", constraint_unparse(c))));
927 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r))
930 cmp = constraintExpr_compare (l, r);
951 if (constraintExpr_similar (l,r))
970 l = constraintExpr_copy (c->lexpr);
971 r = constraintExpr_copy (c->expr);
973 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
975 if (constraintExpr_similar (l,r) && (rHasConstant))
977 DPRINTF((message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r))));
978 DPRINTF((message("constraint_IsAlwaysTrue: rconstant is %d", rConstant)));
980 constraintExpr_free(l);
981 constraintExpr_free(r);
986 return (rConstant == 0);
988 return (rConstant > 0);
990 return (rConstant >= 0);
992 return (rConstant <= 0);
994 return (rConstant < 0);
1004 constraintExpr_free(l);
1005 constraintExpr_free(r);
1006 DPRINTF((message("Constraint %s is not always true", constraint_unparse(c))));
1013 static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
1016 DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2))));
1018 if (! arithType_canResolve (ar1, ar2))
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 DPRINTF(("Can't Get value"));
1060 constraintExpr_free(e1);
1061 constraintExpr_free(e2);
1065 if (constraintExpr_compare (expr2, expr1) >= 0)
1070 if (constraintExpr_similar (expr1, expr2))
1075 if (constraintExpr_similar (expr1, expr2))
1079 if (! (constraintExpr_canGetValue (expr1) &&
1080 constraintExpr_canGetValue (expr2)))
1082 constraintExpr e1, e2;
1086 e1 = constraintExpr_copy(expr1);
1087 e2 = constraintExpr_copy(expr2);
1089 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 constraintExpr_free(e1);
1110 constraintExpr_free(e2);
1112 DPRINTF(("Can't Get value"));
1116 if (constraintExpr_compare (expr2, expr1) <= 0)
1122 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1))));
1127 static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
1129 llassert (constraint_isDefined (c));
1130 DPRINTF (("Doing replace for lexpr"));
1132 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
1133 DPRINTF (("Doing replace for expr"));
1134 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
1138 bool constraint_search (constraint c, constraintExpr old) /*@*/
1142 llassert (constraint_isDefined (c));
1144 ret = constraintExpr_search (c->lexpr, old);
1145 ret = ret || constraintExpr_search (c->expr, old);
1149 /* adjust file locs and stuff */
1150 static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
1152 fileloc loc1, loc2, loc3;
1154 DPRINTF (("Start adjust on %s and %s", constraint_unparse (substitute),
1155 constraint_unparse (old)));
1157 llassert (constraint_isDefined (substitute));
1158 llassert (constraint_isDefined (old));
1160 loc1 = constraint_getFileloc (old);
1161 loc2 = constraintExpr_getFileloc (substitute->lexpr);
1162 loc3 = constraintExpr_getFileloc (substitute->expr);
1164 /* special case of an equality that "contains itself" */
1165 if (constraintExpr_search (substitute->expr, substitute->lexpr))
1167 if (fileloc_closer (loc1, loc3, loc2))
1169 constraintExpr temp;
1170 DPRINTF (("Doing adjust on %s", constraint_unparse(substitute)));
1171 temp = substitute->lexpr;
1172 substitute->lexpr = substitute->expr;
1173 substitute->expr = temp;
1174 substitute = constraint_simplify(substitute);
1178 fileloc_free (loc1);
1179 fileloc_free (loc2);
1180 fileloc_free (loc3);
1185 ** If function preforms substitutes based on inequality
1186 ** It uses the rule x >= y && b < y ===> x >= b + 1
1187 ** warning this is sound but throws out information.
1190 constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
1192 llassert (constraint_isDefined (c));
1199 constraintList_elements (p, el)
1201 llassert (constraint_isDefined (el));
1203 if ((el->ar == LT ))
1204 /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */
1206 constraintExpr temp2;
1210 if (constraintExpr_same (el->expr, c->expr))
1212 DPRINTF (("inequalitySubstitute Replacing %q in %q with %q",
1213 constraintExpr_print (c->expr),
1214 constraint_unparse (c),
1215 constraintExpr_print (el->expr)));
1216 temp2 = constraintExpr_copy (el->lexpr);
1217 constraintExpr_free(c->expr);
1218 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1221 } end_constraintList_elements;
1223 c = constraint_simplify (c);
1229 THis function is like inequalitySubstitute but it adds the rule
1230 added the rules x >= y && y <= b ===> x >= b
1231 x >= y && y < b ===> x >= b + 1
1233 This is sound but sonce it throws out additional information it should only one used
1234 if we're oring constraints.
1237 static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1239 DPRINTF (("inequalitySubstituteStrong examining substituting for %q", constraint_unparse(c)));
1240 llassert (constraint_isDefined (c));
1242 if (!constraint_isDefined (c))
1252 DPRINTF (("inequalitySubstituteStrong examining substituting for %q with %q",
1253 constraint_unparse (c), constraintList_unparse (p)));
1255 constraintList_elements (p, el)
1257 DPRINTF (("inequalitySubstituteStrong examining substituting %s on %s",
1258 constraint_unparse (el), constraint_unparse (c)));
1260 llassert (constraint_isDefined (el));
1262 if ((el->ar == LT ) || (el->ar == LTE))
1263 /* if (!constraint_conflict (c, el) ) */ /*@i523@*/
1265 constraintExpr temp2;
1268 if (constraintExpr_same (el->lexpr, c->expr))
1270 DPRINTF (("inequalitySubstitute Replacing %s in %s with %s",
1271 constraintExpr_print (c->expr),
1272 constraint_unparse (c),
1273 constraintExpr_print (el->expr)));
1275 temp2 = constraintExpr_copy (el->expr);
1276 constraintExpr_free (c->expr);
1278 if ((el->ar == LTE))
1284 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1289 } end_constraintList_elements;
1291 c = constraint_simplify(c);
1296 /* This function performs substitutions based on the rule:
1297 for a constraint of the form expr1 >= expr2; a < b =>
1298 a = b -1 for all a in expr1. This will work in most cases.
1300 Like inequalitySubstitute we're throwing away some information
1303 static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
1305 DPRINTF (("Doing inequalitySubstituteUnsound"));
1306 llassert (constraint_isDefined (c));
1313 constraintList_elements (p, el)
1315 llassert (constraint_isDefined (el));
1317 DPRINTF (("inequalitySubstituteUnsound examining substituting %s on %s",
1318 constraint_unparse (el), constraint_unparse (c)));
1319 if (( el->ar == LTE) || (el->ar == LT))
1320 /* if (!constraint_conflict (c, el) ) */ /*@i532@*/
1322 constraintExpr temp2 = constraintExpr_copy (el->expr);
1326 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1329 DPRINTF (("Replacing %s in %s with %s",
1330 constraintExpr_print (el->lexpr),
1331 constraintExpr_print (c->lexpr),
1332 constraintExpr_print (temp2)));
1334 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
1335 constraintExpr_free(temp2);
1337 } end_constraintList_elements;
1339 c = constraint_simplify (c);
1343 /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
1345 constraint ret = constraint_copy (c);
1347 constraintList_elements (p, el)
1349 llassert (constraint_isDefined (el));
1352 if (!constraint_conflict (ret, el))
1354 constraint temp = constraint_copy (el);
1355 temp = constraint_adjust (temp, ret);
1357 llassert (constraint_isDefined (temp));
1359 DPRINTF (("constraint_substitute :: Substituting in %s using %s",
1360 constraint_unparse (ret), constraint_unparse (temp)));
1362 ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
1364 DPRINTF (("constraint_substitute :: The new constraint is %s", constraint_unparse (ret)));
1365 constraint_free (temp);
1368 } end_constraintList_elements;
1370 ret = constraint_simplify (ret);
1371 DPRINTF (("constraint_substitute :: The final new constraint is %s", constraint_unparse (ret)));
1375 /*@only@*/ constraintList
1376 constraintList_substituteFreeTarget (/*@only@*/ constraintList target,
1377 /*@observer@*/ constraintList subList)
1379 constraintList ret = constraintList_substitute (target, subList);
1380 constraintList_free(target);
1384 /* we try to do substitutions on each constraint in target using the constraint in sublist*/
1386 /*@only@*/ constraintList
1387 constraintList_substitute (constraintList target, /*@observer@*/ constraintList subList)
1389 constraintList ret = constraintList_makeNew();
1391 constraintList_elements(target, el)
1393 constraint temp = constraint_substitute (el, subList);
1394 /*@i3232@*/ /* drl possible problem : warning make sure that a side effect is not expected */
1395 ret = constraintList_add (ret, temp);
1396 } end_constraintList_elements;
1401 static constraint constraint_solve (/*@returned@*/ constraint c)
1403 llassert (constraint_isDefined (c));
1404 DPRINTF (("Solving %s", constraint_unparse (c)));
1405 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
1406 DPRINTF (("Solved and got %s", constraint_unparse (c)));
1410 static arithType flipAr (arithType ar)
1415 case LTE: return GTE;
1418 case GTE: return LTE;
1419 default: llcontbuglit ("unexpected value: case not handled");
1424 static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
1426 constraintExpr temp;
1428 llassert (constraint_isDefined (c));
1430 c->ar = flipAr (c->ar);
1434 DPRINTF (("Swaped left and right sides of constraint"));
1438 constraint constraint_simplify (/*@returned@*/ constraint c)
1440 llassert (constraint_isDefined (c));
1441 DPRINTF (("constraint_simplify on %q ", constraint_unparse (c)));
1443 if (constraint_tooDeep(c))
1445 DPRINTF (("constraint_simplify: constraint to complex aborting %q", constraint_unparse (c)));
1449 c->lexpr = constraintExpr_simplify (c->lexpr);
1450 c->expr = constraintExpr_simplify (c->expr);
1452 if (constraintExpr_isBinaryExpr (c->lexpr))
1454 c = constraint_solve (c);
1455 c->lexpr = constraintExpr_simplify (c->lexpr);
1456 c->expr = constraintExpr_simplify (c->expr);
1459 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr)))
1461 c = constraint_swapLeftRight (c);
1462 /* I don't think this will be an infinite loop */
1463 c = constraint_simplify (c);
1466 DPRINTF (("constraint_simplify returning %q", constraint_unparse (c)));
1470 /* returns true if fileloc for term1 is closer to file for term2 than term3*/
1472 bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1474 if (!fileloc_isDefined (loc1))
1477 if (!fileloc_isDefined (loc2))
1480 if (!fileloc_isDefined (loc3))
1483 if (fileloc_equal (loc2, loc3))
1486 if (fileloc_equal (loc1, loc2))
1489 if (fileloc_equal (loc1, loc3))
1492 if (fileloc_lessthan (loc1, loc2))
1494 if (fileloc_lessthan (loc2, loc3))
1496 llassert (fileloc_lessthan (loc1, loc3));
1505 if (!fileloc_lessthan (loc1, loc2))
1507 if (!fileloc_lessthan (loc2, loc3))
1509 llassert (!fileloc_lessthan (loc1, loc3));