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