]> andersk Git - splint.git/blame_incremental - src/constraintResolve.c
Made allocations involving sizeof work correctly (test/malloc.c).
[splint.git] / src / constraintResolve.c
... / ...
CommitLineData
1/*
2** Splint - annotation-assisted static program checker
3** Copyright (C) 1994-2003 University of Virginia,
4** Massachusetts Institute of Technology
5**
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.
10**
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.
15**
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.
19**
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
23*/
24
25/*
26*
27** constraintResolve.c
28*/
29
30/* #define DEBUGPRINT 1 */
31
32# include <ctype.h> /* for isdigit */
33# include "splintMacros.nf"
34# include "basic.h"
35# include "cgrammar.h"
36# include "cgrammar_tokens.h"
37
38# include "exprChecks.h"
39# include "exprNodeSList.h"
40
41/*@access constraint, exprNode @*/ /*!!! NO! Don't do this so recklessly - design your code more carefully so you don't need to! */
42
43static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
44static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
45
46static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
47static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
48
49static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
50
51static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
52
53static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
54
55static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
56
57
58/*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
59{
60 constraintList ret;
61
62 ret = constraintList_mergeEnsures (list1, list2);
63
64 constraintList_free(list1);
65 return ret;
66}
67
68/*@only@*/ constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2)
69{
70 constraintList ret;
71 constraintList temp;
72
73 llassert(constraintList_isDefined(list1) );
74 llassert(constraintList_isDefined(list2) );
75
76 DPRINTF(( message ("constraintList_mergeEnsures: list1 %s list2 %s",
77 constraintList_print(list1), constraintList_print(list2)
78 )));
79
80 ret = constraintList_fixConflicts (list1, list2);
81 ret = reflectChangesEnsuresFree1 (ret, list2);
82 temp = constraintList_subsumeEnsures (ret, list2);
83 constraintList_free(ret);
84 ret = temp;
85
86 temp = constraintList_subsumeEnsures (list2, ret);
87
88 temp = constraintList_addList (temp, ret);
89 constraintList_free(ret);
90
91 DPRINTF(( message ("constraintList_mergeEnsures: returning %s ",
92 constraintList_print(temp) )
93 ));
94
95
96 return temp;
97}
98
99
100/*@only@*/ constraintList constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1, constraintList list2)
101{
102 constraintList ret;
103
104 ret = constraintList_mergeRequires(list1, list2);
105
106 constraintList_free(list1);
107
108 return ret;
109}
110
111/*@only@*/ constraintList constraintList_mergeRequires (constraintList list1, constraintList list2)
112{
113 constraintList ret;
114 constraintList temp;
115
116 DPRINTF((message ("constraintList_mergeRequires: merging %s and %s ", constraintList_print (list1), constraintList_print(list2) ) ) );
117
118 if (context_getFlag (FLG_REDUNDANTCONSTRAINTS) )
119 {
120 ret = constraintList_copy(list1);
121 ret = constraintList_addList(ret, list2);
122 return ret;
123 }
124
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) ) ) );
128
129/*get constraints in list2 not satified by temp*/
130 ret = constraintList_reflectChanges(list2, temp);
131
132 DPRINTF((message ("constraintList_mergeRequires: ret = %s", constraintList_print(ret) ) ) );
133
134 ret = constraintList_addListFree (ret, temp);
135
136 DPRINTF((message ("constraintList_mergeRequires: returning %s", constraintList_print(ret) ) ) );
137
138 return ret;
139}
140
141/* old name mergeResolve renamed for czech naming convention */
142void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
143{
144 constraintList temp, temp2;
145
146 DPRINTF((message ("magically merging constraint into parent:%s for", exprNode_unparse (parent) )) );
147
148 DPRINTF((message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2) ) ) );
149
150
151 if (exprNode_isUndefined(parent) )
152 {
153 llassert (exprNode_isDefined(parent) );
154 return;
155 }
156
157
158 if (exprNode_isError (child1) || exprNode_isError(child2) )
159 {
160 if (exprNode_isError (child1) && !exprNode_isError(child2) )
161 {
162 constraintList_free(parent->requiresConstraints);
163
164 parent->requiresConstraints = constraintList_copy (child2->requiresConstraints);
165 constraintList_free(parent->ensuresConstraints);
166
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)
171 )
172 ));
173 return;
174 }
175 else
176 {
177 llassert(exprNode_isError(child2) );
178 return;
179 }
180 }
181
182 llassert(!exprNode_isError (child1) && ! exprNode_isError(child2) );
183
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)
189 ) ) );
190
191
192 constraintList_free(parent->requiresConstraints);
193
194 parent->requiresConstraints = constraintList_copy (child1->requiresConstraints);
195
196 if ( context_getFlag (FLG_ORCONSTRAINT) )
197 temp = constraintList_reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
198 else
199 temp = constraintList_reflectChanges(child2->requiresConstraints, child1->ensuresConstraints);
200
201 temp2 = constraintList_mergeRequires (parent->requiresConstraints, temp);
202 constraintList_free(parent->requiresConstraints);
203 constraintList_free(temp);
204
205 parent->requiresConstraints = temp2;
206
207 DPRINTF((message ("Parent requires constraints are %s ",
208 constraintList_print (parent->requiresConstraints)
209 ) ) );
210
211 constraintList_free(parent->ensuresConstraints);
212
213 parent->ensuresConstraints = constraintList_mergeEnsures(child1->ensuresConstraints,
214 child2->ensuresConstraints);
215
216
217 DPRINTF((message ("Parent constraints are %s and %s ",
218 constraintList_print (parent->requiresConstraints),
219 constraintList_print (parent->ensuresConstraints)
220 ) ) );
221
222}
223
224/*@only@*/ constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2)
225{
226 constraintList ret;
227 ret = constraintList_makeNew();
228 constraintList_elements (list1, el)
229 {
230
231 DPRINTF ((message ("Examining %s", constraint_print (el) ) ) );
232 if (!constraintList_resolve (el, list2) )
233 {
234 constraint temp;
235 temp = constraint_copy(el);
236 ret = constraintList_add (ret, temp);
237 }
238 else
239 {
240 DPRINTF ((message ("Subsuming %s", constraint_print (el) ) ) );
241 }
242 } end_constraintList_elements;
243
244 return ret;
245}
246
247
248
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)
252{
253 constraintList ret;
254
255 ret = constraintList_reflectChanges(pre2, post1);
256
257 constraintList_free (pre2);
258
259 return ret;
260}
261
262/* tries to resolve constraints in list pre2 using post1 */
263
264static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
265{
266
267 constraintList ret;
268 constraint temp;
269 constraint temp2;
270
271 llassert (! context_getFlag (FLG_ORCONSTRAINT) );
272
273 ret = constraintList_makeNew();
274 DPRINTF((message ("reflectChanges: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
275
276 constraintList_elements (pre2, el)
277 {
278 if (!constraintList_resolve (el, post1) )
279 {
280 temp = constraint_substitute (el, post1);
281 if (!constraintList_resolve (temp, post1) )
282 {
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
286 */
287 temp2 = constraint_copy (temp);
288 temp2 = inequalitySubstitute (temp2, post1);
289 if (!constraintList_resolve (temp2, post1) )
290 {
291 temp2 = inequalitySubstituteUnsound (temp2, post1);
292 if (!constraintList_resolve (temp2, post1) )
293 ret = constraintList_add (ret, temp2);
294 else
295 constraint_free(temp2);
296 }
297 else
298 {
299 constraint_free(temp2);
300 }
301 }
302 constraint_free(temp);
303 }
304 } end_constraintList_elements;
305
306 DPRINTF((message ("reflectChanges: returning %s", constraintList_print(ret) ) ) );
307 return ret;
308}
309
310/* tries to resolve constraints in list pre2 using post1 */
311/*@only@*/ constraintList constraintList_reflectChanges(/*@observer@*/ constraintList pre2, /*@observer@*/ constraintList post1)
312{
313 constraintList temp;
314
315 if ( context_getFlag (FLG_ORCONSTRAINT) )
316
317 temp = constraintList_reflectChangesOr (pre2, post1);
318 else
319 temp = reflectChangesNoOr(pre2, post1);
320
321 return temp;
322}
323
324static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
325{
326 constraint c;
327
328 llassert(constraint_isDefined(orig) );
329
330 c = orig;
331
332 DPRINTF((message("constraint_addor: oring %s onto %s", constraint_printOr(orConstr), constraint_printOr(orig) ) ));
333
334 while (c->or != NULL)
335 {
336 c = c->or;
337 }
338
339 c->or = constraint_copy(orConstr);
340
341 DPRINTF((message("constraint_addor: returning %s",constraint_printOr(orig) ) ));
342
343 return orig;
344}
345
346
347static bool resolveOr ( /*@temp@*/ constraint c, /*@observer@*/ /*@temp@*/ constraintList list)
348{
349 constraint temp;
350
351 int numberOr;
352
353 numberOr = 0;
354
355 llassert(constraint_isDefined(c) );
356
357 DPRINTF(( message("resolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(list) ) ));
358
359 temp = c;
360
361 do
362 {
363 if (constraintList_resolve (temp, list) )
364 return TRUE;
365 temp = temp->or;
366 numberOr++;
367 llassert(numberOr <= 10);
368 }
369 while (constraint_isDefined(temp));
370
371 return FALSE;
372}
373
374/*This is a "helper" function for doResolveOr */
375
376static /*@only@*/ constraint doResolve (/*@only@*/ constraint c, constraintList post1, bool * resolved)
377{
378 constraint temp;
379
380 llassert(constraint_isDefined (c ) );
381
382 DPRINTF((message("doResolve:: call on constraint c = : %q and constraintList %q",
383 constraint_printOr(c), constraintList_print(post1)
384 )
385 ));
386
387 if (!resolveOr (c, post1) )
388 {
389
390 temp = constraint_substitute (c, post1);
391
392 DPRINTF((message("doResolve:: after substitute temp is %q",
393 constraint_printOr(temp)
394 )
395 ));
396
397 if (!resolveOr (temp, post1) )
398 {
399 /* try inequality substitution */
400 constraint temp2;
401
402 /* the inequality substitution may cause us to lose information
403 so we don't want to store the result but we do anyway
404 */
405 temp2 = constraint_copy (c);
406 temp2 = inequalitySubstitute (temp2, post1);
407
408 if (!resolveOr (temp2, post1) )
409 {
410 constraint temp3;
411 temp3 = constraint_copy(temp2);
412
413 temp3 = inequalitySubstituteStrong (temp3, post1);
414 if (!resolveOr (temp3, post1) )
415 {
416 temp2 = inequalitySubstituteUnsound (temp2, post1);
417 if (!resolveOr (temp2, post1) )
418 {
419 if (!constraint_same (temp, temp2) )
420 {
421 /* drl added 8/28/2002*/
422 /*make sure that the information from
423 a post condition like i = i + 1 is transfered
424 */
425 constraint tempSub;
426 tempSub = constraint_substitute (temp2, post1);
427
428 DPRINTF((
429 message("doResolve: adding %s ",
430 constraint_printOr(tempSub)
431 )
432 ));
433
434 DPRINTF((
435 message("doResolve: not adding %s ",
436 constraint_printOr(temp2)
437 )
438 ));
439
440 temp = constraint_addOr (temp, tempSub);
441 constraint_free(tempSub);
442
443 }
444 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
445 {
446 /* drl added 8/28/2002*/
447 /*make sure that the information from
448 a post condition like i = i + 1 is transfered
449 */
450 constraint tempSub;
451
452 tempSub = constraint_substitute (temp3, post1);
453
454 DPRINTF((
455 message("doResolve: adding %s ",
456 constraint_printOr(tempSub)
457 )
458 ));
459
460
461 DPRINTF((
462 message("doResolve: not adding %s ",
463 constraint_printOr(temp3)
464 )
465 ));
466
467 temp = constraint_addOr (temp, tempSub);
468
469 constraint_free(tempSub);
470 }
471 *resolved = FALSE;
472
473 constraint_free(temp2);
474 constraint_free(temp3);
475 constraint_free(c);
476
477 return temp;
478 }
479 constraint_free(temp2);
480 constraint_free(temp3);
481 }
482 else
483 {
484 constraint_free(temp2);
485 constraint_free(temp3);
486 }
487 }
488 else
489 {
490 constraint_free(temp2);
491 }
492
493 }
494 constraint_free(temp);
495 }
496 constraint_free(c);
497
498 *resolved = TRUE;
499 return NULL;
500}
501
502static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
503{
504 constraint ret;
505 constraint next;
506 constraint curr;
507
508 DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
509
510 *resolved = FALSE;
511
512 llassert(constraint_isDefined(c) );
513
514 ret = constraint_copy(c);
515
516 llassert(constraint_isDefined(ret) );
517
518 if (constraintList_isEmpty(post1) )
519 {
520 return ret;
521 }
522
523 next = ret->or;
524 ret->or = NULL;
525
526 ret = doResolve (ret, post1, resolved);
527
528 if (*resolved)
529 {
530 if (next != NULL)
531 constraint_free(next);
532
533 /*we don't need to free ret when resolved is false because ret is null*/
534 llassert(ret == NULL);
535
536 return NULL;
537 }
538
539 while (next != NULL)
540 {
541 curr = next;
542 next = curr->or;
543 curr->or = NULL;
544
545 curr = doResolve (curr, post1, resolved);
546
547 if (*resolved)
548 {
549 /* curr is null so we don't try to free it*/
550 llassert(curr == NULL);
551
552 if (next != NULL)
553 constraint_free(next);
554
555 constraint_free(ret);
556 return NULL;
557 }
558 ret = constraint_addOr (ret, curr);
559 constraint_free(curr);
560 }
561 return ret;
562}
563
564/* tries to resolve constraints in list pr2 using post1 */
565/*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
566{
567 bool resolved;
568 constraintList ret;
569 constraint temp;
570 ret = constraintList_makeNew();
571 DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
572
573 constraintList_elements (pre2, el)
574 {
575 temp = doResolveOr (el, post1, &resolved);
576
577 if (!resolved)
578 {
579 ret = constraintList_add(ret, temp);
580 }
581 else
582 {
583 /* we don't need to free temp when
584 resolved is false because temp is null */
585 llassert(temp == NULL);
586 }
587
588 } end_constraintList_elements;
589
590 DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
591 return ret;
592}
593
594static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
595{
596 constraintList ret;
597 constraint temp;
598 ret = constraintList_makeNew();
599 constraintList_elements (pre2, el)
600 {
601 if (!constraintList_resolve (el, post1) )
602 {
603 temp = constraint_substitute (el, post1);
604 llassert (temp != NULL);
605
606 if (!constraintList_resolve (temp, post1) )
607 ret = constraintList_add (ret, temp);
608 else
609 constraint_free(temp);
610 }
611 else
612 {
613 DPRINTF ((message ("Resolved away %s ", constraint_print(el) ) ) );
614 }
615 } end_constraintList_elements;
616
617 return ret;
618}
619
620
621static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
622{
623 constraintList ret;
624
625 ret = reflectChangesEnsures (pre2, post1);
626
627 constraintList_free(pre2);
628
629 return ret;
630}
631
632
633static bool constraint_conflict (constraint c1, constraint c2)
634{
635 if (!constraint_isDefined(c1) || !constraint_isDefined(c2))
636 {
637 return FALSE;
638 }
639
640 if (constraintExpr_similar (c1->lexpr, c2->lexpr))
641 {
642 if (c1->ar == EQ)
643 if (c1->ar == c2->ar)
644 {
645 DPRINTF (("%s conflicts with %s", constraint_unparse (c1), constraint_unparse (c2)));
646 return TRUE;
647 }
648 }
649
650 /* This is a slight kludge to prevent circular constraints like
651 strlen(str) == maxRead(s) + strlen(str);
652 */
653
654 /*this code is functional but it may be worth cleaning up at some point. */
655
656 if (c1->ar == EQ)
657 if (c1->ar == c2->ar)
658 {
659 if (constraintExpr_search (c1->lexpr, c2->expr) )
660 if (constraintExpr_isTerm(c1->lexpr) )
661 {
662 constraintTerm term;
663
664 term = constraintExpr_getTerm(c1->lexpr);
665
666 if (constraintTerm_isExprNode(term) )
667 {
668 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
669 return TRUE;
670 }
671 }
672 }
673
674 if (constraint_tooDeep(c1) || constraint_tooDeep(c2) )
675 {
676 DPRINTF ((message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) );
677 return TRUE;
678 }
679
680 DPRINTF ((message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
681
682 return FALSE;
683
684}
685
686static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
687{
688 llassert(constraint_isDefined(conflicting) );
689
690 if (conflicting->ar == EQ)
691 {
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);
696 }
697
698
699}
700
701static bool conflict (constraint c, constraintList list)
702{
703
704 constraintList_elements (list, el)
705 {
706 if ( constraint_conflict(el, c) )
707 {
708 constraint_fixConflict (el, c);
709 return TRUE;
710 }
711 } end_constraintList_elements;
712
713 return FALSE;
714
715}
716
717/*
718 check if constraint in list1 conflicts with constraints in List2. If so we
719 remove form list1 and change list2.
720*/
721
722constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
723{
724 constraintList ret;
725 ret = constraintList_makeNew();
726 llassert(constraintList_isDefined(list1) );
727 constraintList_elements (list1, el)
728 {
729 if (! conflict (el, list2) )
730 {
731 constraint temp;
732 temp = constraint_copy(el);
733 ret = constraintList_add (ret, temp);
734 }
735 } end_constraintList_elements;
736
737 return ret;
738}
739
740/*returns true if constraint post satisfies cosntriant pre */
741
742static bool constraintResolve_satisfies (constraint pre, constraint post)
743{
744 if (!constraint_isDefined (pre))
745 {
746 return TRUE;
747 }
748
749 if (!constraint_isDefined(post))
750 {
751 return FALSE;
752 }
753
754 if (constraint_isAlwaysTrue (pre))
755 return TRUE;
756
757 if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
758 {
759 return FALSE;
760 }
761
762 if (constraintExpr_isUndefined(post->expr))
763 {
764 llassert(FALSE);
765 return FALSE;
766 }
767
768 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
769}
770
771
772bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c,
773 /*@temp@*/ /*@observer@*/ constraintList p)
774{
775 DPRINTF (("[resolve] Trying to resolve constraint: %s using %s",
776 constraint_unparse (c),
777 constraintList_unparse (p)));
778
779 constraintList_elements (p, el)
780 {
781 if (constraintResolve_satisfies (c, el))
782 {
783 DPRINTF (("constraintList_resolve: %s satifies %s",
784 constraint_unparse (el), constraint_unparse (c)));
785 return TRUE;
786 }
787
788 DPRINTF (("constraintList_resolve: %s does not satify %s\n ",
789 constraint_unparse (el), constraint_unparse (c)));
790 }
791 end_constraintList_elements;
792
793 DPRINTF (("No constraints satify: %s", constraint_unparse (c)));
794 return FALSE;
795}
796
797static bool arithType_canResolve (arithType ar1, arithType ar2)
798{
799 switch (ar1)
800 {
801 case GTE:
802 case GT:
803 if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ))
804 {
805 return TRUE;
806 }
807 break;
808
809 case EQ:
810 if (ar2 == EQ)
811 return TRUE;
812 break;
813
814 case LT:
815 case LTE:
816 if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ))
817 return TRUE;
818 break;
819 default:
820 return FALSE;
821 }
822 return FALSE;
823}
824
825/*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
826static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
827{
828 constraintTerm ct;
829 exprNode e, t;
830 sRef s1, s2;
831
832 llassert(constraintExpr_isDefined(buf1) && constraintExpr_isDefined(expr2) );
833
834 /*@access constraintExpr@*/
835
836 if ((expr2->kind != term) && (buf1->kind != term) )
837 return FALSE;
838
839
840 ct = constraintExprData_termGetTerm(expr2->data);
841
842 if (!constraintTerm_isExprNode(ct) )
843 return FALSE;
844
845 e = constraintTerm_getExprNode(ct);
846
847 llassert (exprNode_isDefined(e));
848
849 if (! (exprNode_isDefined(e)))
850 return FALSE;
851
852 if (e->kind != XPR_SIZEOF)
853 return FALSE;
854
855 t = exprData_getSingle (e->edata);
856 s1 = exprNode_getSref (t);
857
858 s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) );
859
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) )
863 {
864 /* origly checked that ctype_isFixedArray(sRef_getType(s2)) but
865 removed that test */
866 return TRUE;
867 }
868 return FALSE;
869}
870
871/* look for the special case of
872 maxSet(buf) >= sizeof(buf) - 1
873*/
874
875/*drl eventually it would be good to check that
876 buf is of type char.*/
877
878static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
879{
880 constraintExpr l, r, buf1, buf2, con;
881
882 DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
883 ));
884
885 llassert (constraint_isDefined(c) );
886
887 l = c->lexpr;
888 r = c->expr;
889
890 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
891 return FALSE;
892
893 llassert (constraintExpr_isDefined(l) );
894 llassert (constraintExpr_isDefined(r) );
895
896 /*check if the constraintExpr is MaxSet(buf) */
897 if (l->kind == unaryExpr)
898 {
899 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
900 {
901 buf1 = constraintExprData_unaryExprGetExpr(l->data);
902 }
903 else
904 return FALSE;
905 }
906 else
907 return FALSE;
908
909
910 if (r->kind != binaryexpr)
911 return FALSE;
912
913 buf2 = constraintExprData_binaryExprGetExpr1(r->data);
914 con = constraintExprData_binaryExprGetExpr2(r->data);
915
916 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS)
917 {
918 if (constraintExpr_canGetValue(con) )
919 {
920 long i;
921
922 i = constraintExpr_getValue(con);
923 if (i != 1)
924 {
925 return FALSE;
926 }
927 }
928 else
929 return FALSE;
930 }
931
932 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS)
933 {
934 if (constraintExpr_canGetValue(con) )
935 {
936 long i;
937
938 i = constraintExpr_getValue(con);
939 if (i != -1)
940 {
941 return FALSE;
942 }
943 }
944 else
945 return FALSE;
946 }
947
948 if (sizeofBufComp(buf1, buf2))
949 {
950 return TRUE;
951 }
952 else
953 {
954 return FALSE;
955 }
956}
957/*@noaccess constraintExpr@*/
958
959/* We look for constraint which are tautologies */
960
961bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
962{
963 constraintExpr l, r;
964 bool rHasConstant;
965 int rConstant;
966
967
968 llassert (constraint_isDefined(c) );
969
970 l = c->lexpr;
971 r = c->expr;
972
973 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
974
975 if (sizeOfMaxSet(c) )
976 return TRUE;
977
978 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
979 {
980 int cmp;
981 cmp = constraintExpr_compare (l, r);
982 switch (c->ar)
983 {
984 case EQ:
985 return (cmp == 0);
986 case GT:
987 return (cmp > 0);
988 case GTE:
989 return (cmp >= 0);
990 case LTE:
991 return (cmp <= 0);
992 case LT:
993 return (cmp < 0);
994
995 default:
996 BADEXIT;
997 /*@notreached@*/
998 break;
999 }
1000 }
1001
1002 if (constraintExpr_similar (l,r))
1003 {
1004 switch (c->ar)
1005 {
1006 case EQ:
1007 case GTE:
1008 case LTE:
1009 return TRUE;
1010
1011 case GT:
1012 case LT:
1013 break;
1014 default:
1015 BADEXIT;
1016 /*@notreached@*/
1017 break;
1018 }
1019 }
1020
1021 l = constraintExpr_copy (c->lexpr);
1022 r = constraintExpr_copy (c->expr);
1023
1024 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
1025
1026 if (constraintExpr_similar (l,r) && (rHasConstant ) )
1027 {
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 ) ));
1030
1031 constraintExpr_free(l);
1032 constraintExpr_free(r);
1033
1034 switch (c->ar)
1035 {
1036 case EQ:
1037 return (rConstant == 0);
1038 case LT:
1039 return (rConstant > 0);
1040 case LTE:
1041 return (rConstant >= 0);
1042 case GTE:
1043 return (rConstant <= 0);
1044 case GT:
1045 return (rConstant < 0);
1046
1047 default:
1048 BADEXIT;
1049 /*@notreached@*/
1050 break;
1051 }
1052 }
1053 else
1054 {
1055 constraintExpr_free(l);
1056 constraintExpr_free(r);
1057 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
1058 return FALSE;
1059 }
1060
1061 BADEXIT;
1062}
1063
1064static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
1065
1066{
1067 DPRINTF (("Doing range check %s and %s",
1068 constraintExpr_unparse (expr1), constraintExpr_unparse (expr2)));
1069
1070 if (!arithType_canResolve (ar1, ar2))
1071 return FALSE;
1072
1073 switch (ar1)
1074 {
1075 case GTE:
1076 if (constraintExpr_similar (expr1, expr2) )
1077 return TRUE;
1078 /*@fallthrough@*/
1079 case GT:
1080 if (! (constraintExpr_canGetValue (expr1) &&
1081 constraintExpr_canGetValue (expr2) ) )
1082 {
1083 constraintExpr e1, e2;
1084 bool p1, p2;
1085 int const1, const2;
1086
1087 e1 = constraintExpr_copy(expr1);
1088 e2 = constraintExpr_copy(expr2);
1089
1090 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1091 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1092
1093 if (p1 || p2)
1094 {
1095 if (!p1)
1096 const1 = 0;
1097
1098 if (!p2)
1099 const2 = 0;
1100
1101 if (const1 <= const2)
1102 if (constraintExpr_similar (e1, e2) )
1103 {
1104 constraintExpr_free(e1);
1105 constraintExpr_free(e2);
1106 return TRUE;
1107 }
1108 }
1109 DPRINTF(("Can't Get value"));
1110
1111 constraintExpr_free(e1);
1112 constraintExpr_free(e2);
1113 return FALSE;
1114 }
1115
1116 if (constraintExpr_compare (expr2, expr1) >= 0)
1117 return TRUE;
1118
1119 return FALSE;
1120 case EQ:
1121 if (constraintExpr_similar (expr1, expr2) )
1122 return TRUE;
1123
1124 return FALSE;
1125 case LTE:
1126 if (constraintExpr_similar (expr1, expr2) )
1127 return TRUE;
1128 /*@fallthrough@*/
1129 case LT:
1130 if (! (constraintExpr_canGetValue (expr1) &&
1131 constraintExpr_canGetValue (expr2) ) )
1132 {
1133 constraintExpr e1, e2;
1134 bool p1, p2;
1135 int const1, const2;
1136
1137 e1 = constraintExpr_copy(expr1);
1138 e2 = constraintExpr_copy(expr2);
1139
1140 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
1141
1142 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
1143
1144 if (p1 || p2)
1145 {
1146 if (!p1)
1147 const1 = 0;
1148
1149 if (!p2)
1150 const2 = 0;
1151
1152 if (const1 >= const2)
1153 if (constraintExpr_similar (e1, e2) )
1154 {
1155 constraintExpr_free(e1);
1156 constraintExpr_free(e2);
1157 return TRUE;
1158 }
1159 }
1160 constraintExpr_free(e1);
1161 constraintExpr_free(e2);
1162
1163 DPRINTF(("Can't Get value"));
1164 return FALSE;
1165 }
1166
1167 if (constraintExpr_compare (expr2, expr1) <= 0)
1168 return TRUE;
1169
1170 return FALSE;
1171
1172 default:
1173 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
1174 }
1175 BADEXIT;
1176}
1177
1178static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
1179{
1180 llassert (constraint_isDefined(c));
1181
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);
1189 return c;
1190}
1191
1192bool constraint_search (constraint c, constraintExpr old) /*@*/
1193{
1194 bool ret;
1195 ret = FALSE;
1196
1197 llassert (constraint_isDefined (c));
1198
1199 ret = constraintExpr_search (c->lexpr, old);
1200 ret = ret || constraintExpr_search (c->expr, old);
1201 return ret;
1202}
1203
1204/* adjust file locs and stuff */
1205static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
1206{
1207 fileloc loc1, loc2, loc3;
1208
1209 DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute),
1210 constraint_print(old))
1211 ));
1212
1213 llassert(constraint_isDefined(substitute));
1214 llassert(constraint_isDefined(old));
1215
1216 loc1 = constraint_getFileloc (old);
1217 loc2 = constraintExpr_loc (substitute->lexpr);
1218 loc3 = constraintExpr_loc (substitute->expr);
1219
1220 /* special case of an equality that "contains itself" */
1221 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
1222 if (fileloc_closer (loc1, loc3, loc2))
1223 {
1224 constraintExpr temp;
1225 DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) )
1226 ));
1227 temp = substitute->lexpr;
1228 substitute->lexpr = substitute->expr;
1229 substitute->expr = temp;
1230 substitute = constraint_simplify(substitute);
1231 }
1232
1233 fileloc_free (loc1);
1234 fileloc_free (loc2);
1235 fileloc_free (loc3);
1236
1237 return substitute;
1238
1239}
1240
1241/* If function preforms substitutes based on inequality
1242
1243 It uses the rule x >= y && b < y ===> x >= b + 1
1244
1245 Warning this is sound but throws out information
1246 */
1247
1248constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
1249{
1250 llassert(constraint_isDefined(c) );
1251
1252 if (c->ar != GTE)
1253 return c;
1254
1255 constraintList_elements (p, el)
1256 {
1257
1258 llassert(constraint_isDefined(el) );
1259
1260 if ((el->ar == LT ) )
1261 {
1262 constraintExpr temp2;
1263
1264 if (constraintExpr_same (el->expr, c->expr) )
1265 {
1266 DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
1267 constraintExpr_print (c->expr),
1268 constraint_print (c),
1269 constraintExpr_print (el->expr) )
1270 ));
1271 temp2 = constraintExpr_copy (el->lexpr);
1272 constraintExpr_free(c->expr);
1273 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1274
1275 }
1276
1277 }
1278 }
1279 end_constraintList_elements;
1280
1281 c = constraint_simplify(c);
1282 return c;
1283}
1284
1285
1286/* drl7x 7/26/001
1287
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
1291
1292 This is sound but sonce it throws out additional information it should only one used
1293 if we're oring constraints.
1294 */
1295
1296static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1297{
1298 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
1299
1300 llassert(constraint_isDefined(c) );
1301
1302 if (! (constraint_isDefined(c) ) )
1303 {
1304 return c;
1305 }
1306
1307 if (c->ar != GTE)
1308 return c;
1309
1310 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1311 constraint_print(c), constraintList_print(p) ) ));
1312 constraintList_elements (p, el)
1313 {
1314
1315 DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1316
1317 llassert(constraint_isDefined(el) );
1318 if ((el->ar == LT ) || (el->ar == LTE ) )
1319 {
1320 constraintExpr temp2;
1321
1322 if (constraintExpr_same (el->lexpr, c->expr) )
1323 {
1324 DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
1325 constraintExpr_print (c->expr),
1326 constraint_print (c),
1327 constraintExpr_print (el->expr) )
1328 ));
1329 temp2 = constraintExpr_copy (el->expr);
1330 constraintExpr_free(c->expr);
1331 if ((el->ar == LTE ) )
1332 {
1333 c->expr = temp2;
1334 }
1335 else
1336 {
1337 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1338 }
1339 }
1340
1341 }
1342 }
1343 end_constraintList_elements;
1344
1345 c = constraint_simplify(c);
1346 return c;
1347}
1348
1349
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.
1353
1354 Like inequalitySubstitute we're throwing away some information
1355*/
1356
1357static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
1358{
1359 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1360
1361 llassert(constraint_isDefined(c) );
1362
1363 if (c->ar != GTE)
1364 return c;
1365
1366 constraintList_elements (p, el)
1367 {
1368
1369 llassert(constraint_isDefined(el) );
1370
1371 DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1372 if (( el->ar == LTE) || (el->ar == LT) )
1373 {
1374 constraintExpr temp2;
1375
1376 temp2 = constraintExpr_copy (el->expr);
1377
1378 if (el->ar == LT)
1379 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1380
1381 DPRINTF((message ("Replacing %s in %s with %s",
1382 constraintExpr_print (el->lexpr),
1383 constraintExpr_print (c->lexpr),
1384 constraintExpr_print (temp2) ) ));
1385
1386 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
1387 constraintExpr_free(temp2);
1388 }
1389 }
1390 end_constraintList_elements;
1391
1392 c = constraint_simplify(c);
1393 return c;
1394}
1395
1396/*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
1397{
1398 constraint ret = constraint_copy (c);
1399
1400 constraintList_elements (p, el)
1401 {
1402 if (constraint_isDefined (el))
1403 {
1404 if ( el->ar == EQ)
1405 if (!constraint_conflict (ret, el))
1406 {
1407 constraint temp = constraint_copy(el);
1408 temp = constraint_adjust(temp, ret);
1409
1410 llassert(constraint_isDefined(temp) );
1411
1412
1413 DPRINTF (("constraint_substitute :: Substituting in %s using %s",
1414 constraint_print (ret), constraint_print (temp)));
1415
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);
1419 }
1420 }
1421 }
1422 end_constraintList_elements;
1423
1424 ret = constraint_simplify (ret);
1425 DPRINTF(( message (" constraint_substitute :: The final new constraint is %s", constraint_print (ret) ) ));
1426 return ret;
1427}
1428
1429
1430/*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
1431{
1432constraintList ret;
1433
1434ret = constraintList_substitute (target, subList);
1435
1436constraintList_free(target);
1437
1438return ret;
1439}
1440
1441/* we try to do substitutions on each constraint in target using the constraint in sublist*/
1442
1443/*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
1444{
1445
1446 constraintList ret;
1447
1448 ret = constraintList_makeNew();
1449
1450 constraintList_elements(target, el)
1451 {
1452 constraint temp;
1453 /* drl possible problem : warning make sure that a side effect is not expected */
1454
1455 temp = constraint_substitute(el, subList);
1456 ret = constraintList_add (ret, temp);
1457 }
1458 end_constraintList_elements;
1459
1460 return ret;
1461}
1462
1463static constraint constraint_solve (/*@returned@*/ constraint c)
1464{
1465
1466 llassert(constraint_isDefined(c) );
1467
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) ) ) );
1471
1472 return c;
1473}
1474
1475static arithType flipAr (arithType ar)
1476{
1477 switch (ar)
1478 {
1479 case LT:
1480 return GT;
1481 case LTE:
1482 return GTE;
1483 case EQ:
1484 return EQ;
1485 case GT:
1486 return LT;
1487 case GTE:
1488 return LTE;
1489 default:
1490 llcontbug (message("unexpected value: case not handled"));
1491 }
1492 BADEXIT;
1493}
1494
1495static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
1496{
1497 constraintExpr temp;
1498
1499 llassert(constraint_isDefined(c) );
1500
1501 c->ar = flipAr (c->ar);
1502 temp = c->lexpr;
1503 c->lexpr = c->expr;
1504 c->expr = temp;
1505 DPRINTF(("Swaped left and right sides of constraint"));
1506 return c;
1507}
1508
1509
1510
1511constraint constraint_simplify ( /*@returned@*/ constraint c)
1512{
1513
1514 llassert(constraint_isDefined(c) );
1515
1516 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
1517
1518 if (constraint_tooDeep(c))
1519 {
1520 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
1521 return c;
1522
1523 }
1524
1525 c->lexpr = constraintExpr_simplify (c->lexpr);
1526 c->expr = constraintExpr_simplify (c->expr);
1527
1528 if (constraintExpr_isBinaryExpr (c->lexpr) )
1529 {
1530 c = constraint_solve (c);
1531
1532 c->lexpr = constraintExpr_simplify (c->lexpr);
1533 c->expr = constraintExpr_simplify (c->expr);
1534 }
1535
1536 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
1537 {
1538 c = constraint_swapLeftRight(c);
1539 /*I don't think this will be an infinate loop*/
1540 c = constraint_simplify(c);
1541 }
1542
1543 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1544
1545 return c;
1546}
1547
1548
1549
1550
1551/* returns true if fileloc for term1 is closer to file for term2 than term3*/
1552
1553bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1554{
1555
1556 if (!fileloc_isDefined (loc1) )
1557 return FALSE;
1558
1559 if (!fileloc_isDefined (loc2) )
1560 return FALSE;
1561
1562 if (!fileloc_isDefined (loc3) )
1563 return TRUE;
1564
1565
1566
1567
1568 if (fileloc_equal (loc2, loc3) )
1569 return FALSE;
1570
1571 if (fileloc_equal (loc1, loc2) )
1572 return TRUE;
1573
1574 if (fileloc_equal (loc1, loc3) )
1575 return FALSE;
1576
1577 if ( fileloc_lessthan (loc1, loc2) )
1578 {
1579 if (fileloc_lessthan (loc2, loc3) )
1580 {
1581 llassert (fileloc_lessthan (loc1, loc3) );
1582 return TRUE;
1583 }
1584 else
1585 {
1586 return FALSE;
1587 }
1588 }
1589
1590 if ( !fileloc_lessthan (loc1, loc2) )
1591 {
1592 if (!fileloc_lessthan (loc2, loc3) )
1593 {
1594 llassert (!fileloc_lessthan (loc1, loc3) );
1595 return TRUE;
1596 }
1597 else
1598 {
1599 return FALSE;
1600 }
1601 }
1602
1603 llassert(FALSE);
1604 return FALSE;
1605}
1606
1607
This page took 0.056956 seconds and 5 git commands to generate.