]> andersk Git - splint.git/blame - src/constraintResolve.c
Removed .out files from the repository because they are automaticaly gnerated durrin...
[splint.git] / src / constraintResolve.c
CommitLineData
65f973be 1/*
2** LCLint - annotation-assisted static program checker
3** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu
21** To report a bug: lclint-bug@cs.virginia.edu
22** For more information: http://lclint.cs.virginia.edu
23*/
24
616915dd 25/*
26*
27** constraintResolve.c
28*/
29
b7b694d6 30/* #define DEBUGPRINT 1 */
616915dd 31
32# include <ctype.h> /* for isdigit */
33# include "lclintMacros.nf"
34# include "basic.h"
35# include "cgrammar.h"
36# include "cgrammar_tokens.h"
37
38# include "exprChecks.h"
616915dd 39# include "exprNodeSList.h"
616915dd 40
616915dd 41
84c9ffbf 42/*@access constraint, exprNode @*/
616915dd 43
44
28bf4b0b 45static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
616915dd 46
616915dd 47
28bf4b0b 48static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
616915dd 49
28bf4b0b 50static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
616915dd 51
dc7f6a51 52static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
53
28bf4b0b 54static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
bb25bea6 55
28bf4b0b 56
57static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
58
59static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
60
61static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
616915dd 62
63/*********************************************/
64
bb25bea6 65
66
67
68/*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
69{
70 constraintList ret;
71
72 ret = constraintList_mergeEnsures (list1, list2);
73
74 constraintList_free(list1);
75 return ret;
76}
616915dd 77
d46ce6a4 78/*@only@*/ constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2)
616915dd 79{
80 constraintList ret;
81 constraintList temp;
470b7798 82
84c9ffbf 83 llassert(constraintList_isDefined(list1) );
d46ce6a4 84 llassert(constraintList_isDefined(list2) );
90bc41f7 85
86 DPRINTF(( message ("constraintList_mergeEnsures: list1 %s list2 %s",
87 constraintList_print(list1), constraintList_print(list2)
88 )));
616915dd 89
90bc41f7 90 ret = constraintList_fixConflicts (list1, list2);
bb25bea6 91 ret = reflectChangesEnsuresFree1 (ret, list2);
d46ce6a4 92 temp = constraintList_subsumeEnsures (ret, list2);
93 constraintList_free(ret);
94 ret = temp;
95
96 temp = constraintList_subsumeEnsures (list2, ret);
616915dd 97
98 temp = constraintList_addList (temp, ret);
4ab867d6 99 constraintList_free(ret);
100
90bc41f7 101 DPRINTF(( message ("constraintList_mergeEnsures: returning %s ",
102 constraintList_print(temp) )
103 ));
104
105
616915dd 106 return temp;
616915dd 107}
108
bb25bea6 109
110/*@only@*/ constraintList constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1, constraintList list2)
111{
112 constraintList ret;
113
114 ret = constraintList_mergeRequires(list1, list2);
115
116 constraintList_free(list1);
117
118 return ret;
119}
120
d46ce6a4 121/*@only@*/ constraintList constraintList_mergeRequires (constraintList list1, constraintList list2)
470b7798 122{
123 constraintList ret;
124 constraintList temp;
125
126 DPRINTF((message ("constraintList_mergeRequires: merging %s and %s ", constraintList_print (list1), constraintList_print(list2) ) ) );
127
128 /* get constraints in list1 not satified by list2 */
28bf4b0b 129 temp = constraintList_reflectChanges(list1, list2);
470b7798 130 DPRINTF((message ("constraintList_mergeRequires: temp = %s", constraintList_print(temp) ) ) );
131
132/*get constraints in list2 not satified by temp*/
28bf4b0b 133 ret = constraintList_reflectChanges(list2, temp);
470b7798 134
135 DPRINTF((message ("constraintList_mergeRequires: ret = %s", constraintList_print(ret) ) ) );
136
4ab867d6 137 ret = constraintList_addListFree (ret, temp);
470b7798 138
139 DPRINTF((message ("constraintList_mergeRequires: returning %s", constraintList_print(ret) ) ) );
140
141 return ret;
142}
616915dd 143
28bf4b0b 144/* old name mergeResolve renamed for czech naming convention */
145void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
616915dd 146{
d46ce6a4 147 constraintList temp, temp2;
616915dd 148
d46ce6a4 149 DPRINTF( (message ("magically merging constraint into parent:%s for", exprNode_unparse (parent) )) );
616915dd 150
151 DPRINTF( (message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2) ) ) );
152
153 if (exprNode_isError (child1) || exprNode_isError(child2) )
d46ce6a4 154 {
155 if (exprNode_isError (child1) && !exprNode_isError(child2) )
616915dd 156 {
d46ce6a4 157 constraintList_free(parent->requiresConstraints);
158
616915dd 159 parent->requiresConstraints = constraintList_copy (child2->requiresConstraints);
d46ce6a4 160 constraintList_free(parent->ensuresConstraints);
161
616915dd 162 parent->ensuresConstraints = constraintList_copy (child2->ensuresConstraints);
163 DPRINTF((message ("Copied child constraints: pre: %s and post: %s",
164 constraintList_print( child2->requiresConstraints),
165 constraintList_print (child2->ensuresConstraints)
166 )
167 ));
168 return;
169 }
170 else
171 {
172 llassert(exprNode_isError(child2) );
616915dd 173 return;
174 }
175 }
176
177 llassert(!exprNode_isError (child1) && ! exprNode_isError(child2) );
178
9280addf 179 DPRINTF( (message ("Child constraints are %s %s and %s %s",
616915dd 180 constraintList_print (child1->requiresConstraints),
181 constraintList_print (child1->ensuresConstraints),
182 constraintList_print (child2->requiresConstraints),
183 constraintList_print (child2->ensuresConstraints)
184 ) ) );
185
d46ce6a4 186
187 constraintList_free(parent->requiresConstraints);
616915dd 188
189 parent->requiresConstraints = constraintList_copy (child1->requiresConstraints);
470b7798 190
90bc41f7 191 if ( context_getFlag (FLG_ORCONSTRAINT) )
28bf4b0b 192 temp = constraintList_reflectChangesOr (child2->requiresConstraints, child1->ensuresConstraints);
90bc41f7 193 else
28bf4b0b 194 temp = constraintList_reflectChanges(child2->requiresConstraints, child1->ensuresConstraints);
d46ce6a4 195
196 temp2 = constraintList_mergeRequires (parent->requiresConstraints, temp);
197 constraintList_free(parent->requiresConstraints);
198 constraintList_free(temp);
90bc41f7 199
d46ce6a4 200 parent->requiresConstraints = temp2;
201
202 DPRINTF( (message ("Parent requires constraints are %s ",
203 constraintList_print (parent->requiresConstraints)
204 ) ) );
205
206 constraintList_free(parent->ensuresConstraints);
616915dd 207
208 parent->ensuresConstraints = constraintList_mergeEnsures(child1->ensuresConstraints,
209 child2->ensuresConstraints);
d46ce6a4 210
616915dd 211
212 DPRINTF( (message ("Parent constraints are %s and %s ",
213 constraintList_print (parent->requiresConstraints),
214 constraintList_print (parent->ensuresConstraints)
215 ) ) );
216
217}
218
219
220
221
d46ce6a4 222/*@only@*/ constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2)
616915dd 223{
224 constraintList ret;
c3e695ff 225 ret = constraintList_makeNew();
616915dd 226 constraintList_elements (list1, el)
227 {
228
229 DPRINTF ((message ("Examining %s", constraint_print (el) ) ) );
28bf4b0b 230 if (!constraintList_resolve (el, list2) )
616915dd 231 {
d46ce6a4 232 constraint temp;
233 temp = constraint_copy(el);
234 ret = constraintList_add (ret, temp);
616915dd 235 }
236 else
237 {
238 DPRINTF ( (message ("Subsuming %s", constraint_print (el) ) ) );
239 }
240 } end_constraintList_elements;
241
242 return ret;
243}
244
90bc41f7 245
bb25bea6 246
28bf4b0b 247/*used to be reflectChangesFreePre renamed for Czech naming conventino*/
bb25bea6 248/* tries to resolve constraints in list pre2 using post1 */
28bf4b0b 249/*@only@*/ constraintList constraintList_reflectChangesFreePre (/*@only@*/ constraintList pre2, /*@observer@*/ constraintList post1)
bb25bea6 250{
251 constraintList ret;
252
28bf4b0b 253 ret = constraintList_reflectChanges(pre2, post1);
bb25bea6 254
255 constraintList_free (pre2);
256
257 return ret;
258}
259
260
dc7f6a51 261
90bc41f7 262/* tries to resolve constraints in list pre2 using post1 */
dc7f6a51 263
264static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
616915dd 265{
266
267 constraintList ret;
268 constraint temp;
bb25bea6 269 constraint temp2;
270
dc7f6a51 271 llassert (! context_getFlag (FLG_ORCONSTRAINT) );
272
c3e695ff 273 ret = constraintList_makeNew();
616915dd 274 DPRINTF((message ("reflectChanges: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
275
276 constraintList_elements (pre2, el)
277 {
28bf4b0b 278 if (!constraintList_resolve (el, post1) )
616915dd 279 {
28bf4b0b 280 temp = constraint_substitute (el, post1);
281 if (!constraintList_resolve (temp, post1) )
616915dd 282 {
b7b694d6 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 */
616915dd 287 temp2 = constraint_copy (temp);
920a3797 288 temp2 = inequalitySubstitute (temp2, post1);
28bf4b0b 289 if (!constraintList_resolve (temp2, post1) )
920a3797 290 {
291 temp2 = inequalitySubstituteUnsound (temp2, post1);
28bf4b0b 292 if (!constraintList_resolve (temp2, post1) )
920a3797 293 ret = constraintList_add (ret, temp2);
294 else
295 constraint_free(temp2);
296 }
297 else
298 {
299 constraint_free(temp2);
300 }
616915dd 301 }
bb25bea6 302 constraint_free(temp);
616915dd 303 }
304 } end_constraintList_elements;
305
306 DPRINTF((message ("reflectChanges: returning %s", constraintList_print(ret) ) ) );
307 return ret;
308}
309
dc7f6a51 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}
616915dd 323
28bf4b0b 324static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
90bc41f7 325{
326 constraint c;
327 c = orig;
328
28bf4b0b 329 DPRINTF((message("constraint_addor: oring %s onto %s", constraint_printOr(orConstr), constraint_printOr(orig) ) ));
90bc41f7 330
331 while (c->or != NULL)
332 {
333 c = c->or;
334 }
a779b61e 335
28bf4b0b 336 c->or = constraint_copy(orConstr);
90bc41f7 337
338 DPRINTF((message("constraint_addor: returning %s",constraint_printOr(orig) ) ));
339
340 return orig;
341}
342
343
28bf4b0b 344static bool resolveOr ( /*@temp@*/ constraint c, /*@observer@*/ /*@temp@*/ constraintList list)
90bc41f7 345{
346 constraint temp;
347
a779b61e 348 int numberOr;
349
350 numberOr = 0;
90bc41f7 351 DPRINTF(( message("resolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(list) ) ));
352 temp = c;
353
354 do
355 {
28bf4b0b 356 if (constraintList_resolve (temp, list) )
90bc41f7 357 return TRUE;
358 temp = temp->or;
a779b61e 359 numberOr++;
360 llassert(numberOr <= 10);
90bc41f7 361 }
84c9ffbf 362 while (constraint_isDefined(temp));
90bc41f7 363
364 return FALSE;
365}
366
bb25bea6 367/*This is a "helper" function for doResolveOr */
90bc41f7 368
bb25bea6 369static /*@only@*/ constraint doResolve (/*@only@*/ constraint c, constraintList post1, bool * resolved)
90bc41f7 370{
371 constraint temp;
a779b61e 372
373 llassert(constraint_isUndefined (c->or ) );
90bc41f7 374
a779b61e 375 if (!resolveOr (c, post1) )
376 {
377
378 temp = constraint_substitute (c, post1);
379
380 if (!resolveOr (temp, post1) )
90bc41f7 381 {
b7b694d6 382 /* try inequality substitution */
a779b61e 383 constraint temp2;
bb25bea6 384
b7b694d6 385 /* the inequality substitution may cause us to lose information
386 so we don't want to store the result but we do anyway
387 */
388 temp2 = constraint_copy (c);
389 temp2 = inequalitySubstitute (temp2, post1);
390
391 if (!resolveOr (temp2, post1) )
392 {
393 constraint temp3;
394 temp3 = constraint_copy(temp2);
395
396 temp3 = inequalitySubstituteStrong (temp3, post1);
397 if (!resolveOr (temp3, post1) )
bb25bea6 398 {
b7b694d6 399 temp2 = inequalitySubstituteUnsound (temp2, post1);
400 if (!resolveOr (temp2, post1) )
dc7f6a51 401 {
b7b694d6 402 if (!constraint_same (temp, temp2) )
403 temp = constraint_addOr (temp, temp2);
404
405 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
406 temp = constraint_addOr (temp, temp3);
407
408 *resolved = FALSE;
409
dc7f6a51 410 constraint_free(temp2);
411 constraint_free(temp3);
b7b694d6 412 constraint_free(c);
413
414 return temp;
bb25bea6 415 }
b7b694d6 416 constraint_free(temp2);
417 constraint_free(temp3);
bb25bea6 418 }
dc7f6a51 419 else
420 {
421 constraint_free(temp2);
b7b694d6 422 constraint_free(temp3);
423 }
90bc41f7 424 }
b7b694d6 425 else
426 {
427 constraint_free(temp2);
428 }
429
90bc41f7 430 }
b7b694d6 431 constraint_free(temp);
432 }
433 constraint_free(c);
434
435 *resolved = TRUE;
436 return NULL;
90bc41f7 437}
438
a779b61e 439static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
90bc41f7 440{
441 constraint ret;
442 constraint next;
443 constraint curr;
a779b61e 444
445
446 DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
447
448
90bc41f7 449
450 *resolved = FALSE;
a779b61e 451
452
90bc41f7 453 ret = constraint_copy(c);
a779b61e 454
455 if (constraintList_isEmpty(post1) )
456 {
457 return ret;
458 }
459
90bc41f7 460 next = ret->or;
461 ret->or = NULL;
462
463 ret = doResolve (ret, post1, resolved);
bb25bea6 464
465 if (*resolved)
466 {
2934b455 467 if (next != NULL)
4ab867d6 468 constraint_free(next);
a779b61e 469
920a3797 470 /*we don't need to free ret when resolved is false because ret is null*/
471 llassert(ret == NULL);
472
a779b61e 473 return NULL;
bb25bea6 474 }
a779b61e 475
84c9ffbf 476 while (next != NULL)
90bc41f7 477 {
478 curr = next;
479 next = curr->or;
480 curr->or = NULL;
481
482 curr = doResolve (curr, post1, resolved);
a779b61e 483
90bc41f7 484 if (*resolved)
bb25bea6 485 {
4ab867d6 486 /* curr is null so we don't try to free it*/
920a3797 487 llassert(curr == NULL);
4ab867d6 488
2934b455 489 if (next != NULL)
4ab867d6 490 constraint_free(next);
920a3797 491
bb25bea6 492 constraint_free(ret);
920a3797 493 return NULL;
bb25bea6 494 }
90bc41f7 495 ret = constraint_addOr (ret, curr);
4ab867d6 496 constraint_free(curr);
90bc41f7 497 }
90bc41f7 498 return ret;
499}
500
90bc41f7 501/* tries to resolve constraints in list pr2 using post1 */
28bf4b0b 502/*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
90bc41f7 503{
504 bool resolved;
505 constraintList ret;
506 constraint temp;
c3e695ff 507 ret = constraintList_makeNew();
28bf4b0b 508 DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
90bc41f7 509
510 constraintList_elements (pre2, el)
511 {
512 temp = doResolveOr (el, post1, &resolved);
513
514 if (!resolved)
515 {
516 ret = constraintList_add(ret, temp);
517 }
920a3797 518 else
519 {
a779b61e 520 /* we don't need to free temp when
521 resolved is false because temp is null */
920a3797 522 llassert(temp == NULL);
523 }
524
90bc41f7 525 } end_constraintList_elements;
526
a779b61e 527 DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
90bc41f7 528 return ret;
529}
a779b61e 530
bb25bea6 531static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
616915dd 532{
533 constraintList ret;
534 constraint temp;
c3e695ff 535 ret = constraintList_makeNew();
616915dd 536 constraintList_elements (pre2, el)
537 {
28bf4b0b 538 if (!constraintList_resolve (el, post1) )
616915dd 539 {
28bf4b0b 540 temp = constraint_substitute (el, post1);
616915dd 541 llassert (temp != NULL);
542
28bf4b0b 543 if (!constraintList_resolve (temp, post1) )
616915dd 544 ret = constraintList_add (ret, temp);
920a3797 545 else
546 constraint_free(temp);
616915dd 547 }
548 else
549 {
550 DPRINTF ( (message ("Resolved away %s ", constraint_print(el) ) ) );
551 }
552 } end_constraintList_elements;
553
554 return ret;
555}
556
557
bb25bea6 558static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
559{
560 constraintList ret;
561
562 ret = reflectChangesEnsures (pre2, post1);
563
564 constraintList_free(pre2);
565
566 return ret;
567}
568
569
570static bool constraint_conflict (constraint c1, constraint c2)
616915dd 571{
572
573 if (constraintExpr_similar(c1->lexpr, c2->lexpr) )
574 {
575 if (c1->ar == EQ)
576 if (c1->ar == c2->ar)
577 {
578 DPRINTF ( (message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
579 return TRUE;
580 }
581 }
582
b7b694d6 583 /* This is a slight kludg to prevent circular constraints like
584 strlen(str) == maxRead(s) + strlen(str);
585 */
a779b61e 586
b7b694d6 587 /*@i324234*/ /* clean this up */
a779b61e 588
589 if (c1->ar == EQ)
590 if (c1->ar == c2->ar)
591 {
592 if (constraintExpr_search (c1->lexpr, c2->expr) )
593 if (constraintExpr_isTerm(c1->lexpr) )
594 {
595 constraintTerm term;
596
597 term = constraintExpr_getTerm(c1->lexpr);
598
599 if (constraintTerm_isExprNode(term) )
600 {
601 DPRINTF ( (message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
602 return TRUE;
603 }
604 }
605 }
606
607 if (constraint_tooDeep(c1) || constraint_tooDeep(c2) )
608 {
609 DPRINTF ( (message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) );
610 return TRUE;
611 }
612
616915dd 613 DPRINTF ( (message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
614
615 return FALSE;
616
617}
618
28bf4b0b 619static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
616915dd 620{
616915dd 621 if (conflicting->ar ==EQ )
622 {
623 good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
d46ce6a4 624 good = constraint_simplify (good);
616915dd 625 }
626
627
628}
629
bb25bea6 630static bool conflict (constraint c, constraintList list)
616915dd 631{
90bc41f7 632
633 constraintList_elements (list, el)
616915dd 634 {
635 if ( constraint_conflict(el, c) )
636 {
637 constraint_fixConflict (el, c);
638 return TRUE;
639 }
640 } end_constraintList_elements;
641
642 return FALSE;
616915dd 643
644}
645
b7b694d6 646/*
647 check if constraint in list1 conflicts with constraints in List2. If so we
648 remove form list1 and change list2.
649*/
650
84c9ffbf 651constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
616915dd 652{
653 constraintList ret;
c3e695ff 654 ret = constraintList_makeNew();
84c9ffbf 655 llassert(constraintList_isDefined(list1) );
616915dd 656 constraintList_elements (list1, el)
657 {
658 if (! conflict (el, list2) )
659 {
d46ce6a4 660 constraint temp;
661 temp = constraint_copy(el);
662 ret = constraintList_add (ret, temp);
616915dd 663 }
664 } end_constraintList_elements;
665
666 return ret;
616915dd 667}
668
90bc41f7 669/*returns true if constraint post satifies cosntriant pre */
bb25bea6 670static bool satifies (constraint pre, constraint post)
616915dd 671{
672 if (constraint_isAlwaysTrue (pre) )
673 return TRUE;
674
675 if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
676 {
677 return FALSE;
678 }
28bf4b0b 679 if (constraintExpr_isUndefined(post->expr))
616915dd 680 {
681 llassert(FALSE);
682 return FALSE;
683 }
684
685 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
686}
687
bb25bea6 688
28bf4b0b 689bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p)
bb25bea6 690{
691 constraintList_elements (p, el)
692 {
693 if ( satifies (c, el) )
694 {
695 DPRINTF ( (message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
696 return TRUE;
697 }
698 DPRINTF ( (message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) );
699 }
700 end_constraintList_elements;
701 DPRINTF ( (message ("no constraints satify %s", constraint_print(c) ) ));
702 return FALSE;
703}
704
705static bool arithType_canResolve (arithType ar1, arithType ar2)
616915dd 706{
707 switch (ar1)
708 {
709 case GTE:
710 case GT:
711 if ( (ar2 == GT) || (ar2 == GTE) || (ar2 == EQ) )
712 {
713 return TRUE;
714 }
715 break;
716
717 case EQ:
718 if (ar2 == EQ)
719 return TRUE;
720 break;
721
722 case LT:
723 case LTE:
616915dd 724 if ( (ar2 == LT) || (ar2 == LTE) || (ar2 == EQ) )
725 return TRUE;
84c9ffbf 726 break;
616915dd 727 default:
728 return FALSE;
729 }
730 return FALSE;
731}
732
90bc41f7 733/* We look for constraint which are tautologies */
734
28bf4b0b 735bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
616915dd 736{
737 constraintExpr l, r;
15b3d2b2 738 bool rHasConstant;
739 int rConstant;
9280addf 740
616915dd 741 l = c->lexpr;
742 r = c->expr;
90bc41f7 743
744 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
9280addf 745
616915dd 746 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
747 {
748 int cmp;
749 cmp = constraintExpr_compare (l, r);
750 switch (c->ar)
751 {
752 case EQ:
753 return (cmp == 0);
754 case GT:
755 return (cmp > 0);
756 case GTE:
757 return (cmp >= 0);
758 case LTE:
759 return (cmp <= 0);
760 case LT:
761 return (cmp < 0);
762
763 default:
9280addf 764 BADEXIT;
84c9ffbf 765 /*@notreached@*/
9280addf 766 break;
767 }
768 }
769
770 if (constraintExpr_similar (l,r) )
771 {
772 switch (c->ar)
773 {
774 case EQ:
775 case GTE:
776 case LTE:
777 return TRUE;
778
779 case GT:
780 case LT:
781 break;
782 default:
783 BADEXIT;
84c9ffbf 784 /*@notreached@*/
616915dd 785 break;
786 }
787 }
9280addf 788
789 l = constraintExpr_copy (c->lexpr);
790 r = constraintExpr_copy (c->expr);
791
9280addf 792 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
793
90bc41f7 794 if (constraintExpr_similar (l,r) && (rHasConstant ) )
9280addf 795 {
796 DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) ));
90bc41f7 797 DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) ));
bb25bea6 798
799 constraintExpr_free(l);
800 constraintExpr_free(r);
801
90bc41f7 802 switch (c->ar)
9280addf 803 {
90bc41f7 804 case EQ:
805 return (rConstant == 0);
806 case LT:
807 return (rConstant > 0);
808 case LTE:
809 return (rConstant >= 0);
810 case GTE:
811 return (rConstant <= 0);
812 case GT:
813 return (rConstant < 0);
814
815 default:
816 BADEXIT;
84c9ffbf 817 /*@notreached@*/
90bc41f7 818 break;
9280addf 819 }
9280addf 820 }
90bc41f7 821 else
822 {
bb25bea6 823 constraintExpr_free(l);
824 constraintExpr_free(r);
90bc41f7 825 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
826 return FALSE;
827 }
9280addf 828
829 BADEXIT;
616915dd 830}
831
bb25bea6 832static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
616915dd 833
834{
bb25bea6 835 DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
616915dd 836
837 if (! arithType_canResolve (ar1, ar2) )
838 return FALSE;
839
840 switch (ar1)
841 {
842 case GTE:
2681ee39 843 if (constraintExpr_similar (expr1, expr2) )
844 return TRUE;
845 /*@fallthrough@*/
846 case GT:
616915dd 847 if (! (constraintExpr_canGetValue (expr1) &&
2681ee39 848 constraintExpr_canGetValue (expr2) ) )
849 {
850 constraintExpr e1, e2;
851 bool p1, p2;
852 int const1, const2;
853
854 e1 = constraintExpr_copy(expr1);
855 e2 = constraintExpr_copy(expr2);
856
857 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
858
859 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
860
861 if (p1 || p2)
862 {
863 if (!p1)
864 const1 = 0;
865
866 if (!p2)
867 const2 = 0;
868
869 if (const1 <= const2)
870 if (constraintExpr_similar (e1, e2) )
871 {
872 constraintExpr_free(e1);
873 constraintExpr_free(e2);
874 return TRUE;
875 }
876 }
877 DPRINTF( ("Can't Get value"));
878
879 constraintExpr_free(e1);
880 constraintExpr_free(e2);
881 return FALSE;
882 }
883
884 if (constraintExpr_compare (expr2, expr1) >= 0)
885 return TRUE;
616915dd 886
887 return FALSE;
2681ee39 888 case EQ:
889 if (constraintExpr_similar (expr1, expr2) )
890 return TRUE;
891
892 return FALSE;
893 case LTE:
894 if (constraintExpr_similar (expr1, expr2) )
895 return TRUE;
896 /*@fallthrough@*/
897 case LT:
898 if (! (constraintExpr_canGetValue (expr1) &&
899 constraintExpr_canGetValue (expr2) ) )
900 {
901 constraintExpr e1, e2;
902 bool p1, p2;
903 int const1, const2;
904
905 e1 = constraintExpr_copy(expr1);
906 e2 = constraintExpr_copy(expr2);
907
908 e1 = constraintExpr_propagateConstants (e1, &p1, &const1);
909
910 e2 = constraintExpr_propagateConstants (e2, &p2, &const2);
911
912 if (p1 || p2)
913 {
914 if (!p1)
915 const1 = 0;
916
917 if (!p2)
918 const2 = 0;
919
920 if (const1 >= const2)
921 if (constraintExpr_similar (e1, e2) )
922 {
923 constraintExpr_free(e1);
924 constraintExpr_free(e2);
925 return TRUE;
926 }
927 }
928 constraintExpr_free(e1);
929 constraintExpr_free(e2);
930
931 DPRINTF( ("Can't Get value"));
932 return FALSE;
933 }
934
935 if (constraintExpr_compare (expr2, expr1) <= 0)
936 return TRUE;
937
938 return FALSE;
939
940 default:
941 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
942 }
616915dd 943 BADEXIT;
616915dd 944}
945
28bf4b0b 946static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
616915dd 947{
948 DPRINTF (("Doing replace for lexpr") );
28bf4b0b 949 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
616915dd 950 DPRINTF (("Doing replace for expr") );
28bf4b0b 951 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
616915dd 952 return c;
953}
954
84c9ffbf 955bool constraint_search (constraint c, constraintExpr old) /*@*/
616915dd 956{
957 bool ret;
958 ret = FALSE;
959
960 ret = constraintExpr_search (c->lexpr, old);
961 ret = ret || constraintExpr_search (c->expr, old);
962 return ret;
963}
964
b7b694d6 965/* adjust file locs and stuff */
bb25bea6 966static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
616915dd 967{
968 fileloc loc1, loc2, loc3;
969
970 DPRINTF ( (message("Start adjust on %s and %s", constraint_print(substitute),
971 constraint_print(old))
972 ));
973
974 loc1 = constraint_getFileloc (old);
616915dd 975 loc2 = constraintExpr_getFileloc (substitute->lexpr);
616915dd 976 loc3 = constraintExpr_getFileloc (substitute->expr);
616915dd 977
b7b694d6 978 /* special case of an equality that "contains itself" */
616915dd 979 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
980 if (fileloc_closer (loc1, loc3, loc2))
981 {
982 constraintExpr temp;
983 DPRINTF ( (message("Doing adjust on %s", constraint_print(substitute) )
984 ));
985 temp = substitute->lexpr;
986 substitute->lexpr = substitute->expr;
987 substitute->expr = temp;
988 substitute = constraint_simplify(substitute);
989 }
990
bb25bea6 991 fileloc_free (loc1);
992 fileloc_free (loc2);
993 fileloc_free (loc3);
994
616915dd 995 return substitute;
996
997}
998
90bc41f7 999/* If function preforms substitutes based on inequality
1000
1001 It uses the rule x >= y && b < y ===> x >= b + 1
1002
1003 Warning this is sound but throws out information
1004 */
dc7f6a51 1005
bb25bea6 1006constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
616915dd 1007{
1008 if (c->ar != GTE)
1009 return c;
1010
1011 constraintList_elements (p, el)
1012 {
dc7f6a51 1013 if ( (el->ar == LT ) )
b7b694d6 1014 /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */
616915dd 1015 {
90bc41f7 1016 constraintExpr temp2;
1017
bb25bea6 1018 /*@i22*/
1019
90bc41f7 1020 if (constraintExpr_same (el->expr, c->expr) )
616915dd 1021 {
dc7f6a51 1022 DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q",
616915dd 1023 constraintExpr_print (c->expr),
1024 constraint_print (c),
1025 constraintExpr_print (el->expr) )
1026 ));
90bc41f7 1027 temp2 = constraintExpr_copy (el->lexpr);
bb25bea6 1028 constraintExpr_free(c->expr);
90bc41f7 1029 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
dc7f6a51 1030
616915dd 1031 }
1032
1033 }
1034 }
1035 end_constraintList_elements;
1036
1037 c = constraint_simplify(c);
1038 return c;
1039}
1040
dc7f6a51 1041
1042/* drl7x 7/26/001
1043
1044 THis function is like inequalitySubstitute but it adds the rule
1045 added the rules x >= y && y <= b ===> x >= b
1046 x >= y && y < b ===> x >= b + 1
1047
1048 This is sound but sonce it throws out additional information it should only one used
1049 if we're oring constraints.
1050 */
1051
1052static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
1053{
1054 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
1055
1056 if (c->ar != GTE)
1057 return c;
1058
1059 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1060 constraint_print(c), constraintList_print(p) ) ));
1061 constraintList_elements (p, el)
1062 {
1063 DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1064
1065 if ( (el->ar == LT ) || (el->ar == LTE ) )
b7b694d6 1066 /* if (!constraint_conflict (c, el) ) */ /*@i523@*/
dc7f6a51 1067 {
dc7f6a51 1068 constraintExpr temp2;
1069
1070 /*@i22*/
1071
dc7f6a51 1072 if (constraintExpr_same (el->lexpr, c->expr) )
1073 {
1074 DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s",
1075 constraintExpr_print (c->expr),
1076 constraint_print (c),
1077 constraintExpr_print (el->expr) )
1078 ));
1079 temp2 = constraintExpr_copy (el->expr);
1080 constraintExpr_free(c->expr);
1081 if ( (el->ar == LTE ) )
1082 {
1083 c->expr = temp2;
1084 }
1085 else
1086 {
1087 c->expr = constraintExpr_makeIncConstraintExpr (temp2);
1088 }
1089 }
1090
1091 }
1092 }
1093 end_constraintList_elements;
1094
1095 c = constraint_simplify(c);
1096 return c;
1097}
1098
1099
90bc41f7 1100/* This function performs substitutions based on the rule:
1101 for a constraint of the form expr1 >= expr2; a < b =>
1102 a = b -1 for all a in expr1. This will work in most cases.
1103
1104 Like inequalitySubstitute we're throwing away some information
1105*/
1106
bb25bea6 1107static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
90bc41f7 1108{
1109 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1110
1111 if (c->ar != GTE)
1112 return c;
1113
1114 constraintList_elements (p, el)
1115 {
1116 DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) ));
1117 if ( ( el->ar == LTE) || (el->ar == LT) )
b7b694d6 1118 /* if (!constraint_conflict (c, el) ) */ /*@i532@*/
90bc41f7 1119 {
90bc41f7 1120 constraintExpr temp2;
b7b694d6 1121
90bc41f7 1122 temp2 = constraintExpr_copy (el->expr);
1123
1124 if (el->ar == LT)
1125 temp2 = constraintExpr_makeDecConstraintExpr (temp2);
1126
1127 DPRINTF((message ("Replacing %s in %s with %s",
1128 constraintExpr_print (el->lexpr),
1129 constraintExpr_print (c->lexpr),
1130 constraintExpr_print (temp2) ) ));
1131
1132 c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2);
bb25bea6 1133 constraintExpr_free(temp2);
90bc41f7 1134 }
1135 }
1136 end_constraintList_elements;
1137
1138 c = constraint_simplify(c);
1139 return c;
1140}
1141
28bf4b0b 1142/*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
616915dd 1143{
90bc41f7 1144 constraint ret;
1145
1146 ret = constraint_copy(c);
616915dd 1147 constraintList_elements (p, el)
1148 {
1149 if ( el->ar == EQ)
90bc41f7 1150 if (!constraint_conflict (ret, el) )
616915dd 1151
1152 {
1153 constraint temp;
bb25bea6 1154
616915dd 1155 temp = constraint_copy(el);
1156
90bc41f7 1157 temp = constraint_adjust(temp, ret);
616915dd 1158
470b7798 1159 DPRINTF((message ("Substituting %s in the constraint %s",
90bc41f7 1160 constraint_print (temp), constraint_print (ret)
616915dd 1161 ) ) );
1162
1163
90bc41f7 1164 ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr);
1165 DPRINTF(( message ("The new constraint is %s", constraint_print (ret) ) ));
bb25bea6 1166 constraint_free(temp);
616915dd 1167 }
1168 }
1169 end_constraintList_elements;
bb25bea6 1170 DPRINTF(( message ("The finial new constraint is %s", constraint_print (ret) ) ));
616915dd 1171
90bc41f7 1172 ret = constraint_simplify(ret);
1173 return ret;
616915dd 1174}
1175
bb25bea6 1176
1177/*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
1178{
1179constraintList ret;
1180
1181ret = constraintList_substitute (target, subList);
1182
1183constraintList_free(target);
1184
1185return ret;
1186}
1187
c3e695ff 1188/* we try to do substitutions on each constraint in target using the constraint in sublist*/
1189
bb25bea6 1190/*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
c3e695ff 1191{
1192
1193 constraintList ret;
1194
1195 ret = constraintList_makeNew();
1196
1197 constraintList_elements(target, el)
1198 {
d46ce6a4 1199 constraint temp;
b7b694d6 1200 /* drl possible problem : warning make sure that a side effect is not expected */
bb25bea6 1201
28bf4b0b 1202 temp = constraint_substitute(el, subList);
d46ce6a4 1203 ret = constraintList_add (ret, temp);
c3e695ff 1204 }
1205 end_constraintList_elements;
84c9ffbf 1206
bb25bea6 1207 return ret;
c3e695ff 1208}
616915dd 1209
28bf4b0b 1210static constraint constraint_solve (/*@returned@*/ constraint c)
616915dd 1211{
1212 DPRINTF( (message ("Solving %s\n", constraint_print(c) ) ) );
1213 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
1214 DPRINTF( (message ("Solved and got %s\n", constraint_print(c) ) ) );
1215
1216 return c;
1217}
1218
1219static arithType flipAr (arithType ar)
1220{
1221 switch (ar)
1222 {
1223 case LT:
1224 return GT;
1225 case LTE:
1226 return GTE;
1227 case EQ:
1228 return EQ;
1229 case GT:
1230 return LT;
1231 case GTE:
1232 return LTE;
1233 default:
84c9ffbf 1234 llcontbug (message("unexpected value: case not handled"));
616915dd 1235 }
1236 BADEXIT;
1237}
1238
d46ce6a4 1239static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
616915dd 1240{
1241 constraintExpr temp;
1242 c->ar = flipAr (c->ar);
1243 temp = c->lexpr;
1244 c->lexpr = c->expr;
1245 c->expr = temp;
1246 DPRINTF(("Swaped left and right sides of constraint"));
1247 return c;
1248}
1249
c3e695ff 1250
1251
d46ce6a4 1252constraint constraint_simplify ( /*@returned@*/ constraint c)
616915dd 1253{
a779b61e 1254
1255 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
1256
1257 if (constraint_tooDeep(c))
1258 {
1259 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
1260 return c;
1261
1262 }
1263
616915dd 1264 c->lexpr = constraintExpr_simplify (c->lexpr);
1265 c->expr = constraintExpr_simplify (c->expr);
616915dd 1266
bb25bea6 1267 if (constraintExpr_isBinaryExpr (c->lexpr) )
c3e695ff 1268 {
1269 c = constraint_solve (c);
1270
1271 c->lexpr = constraintExpr_simplify (c->lexpr);
1272 c->expr = constraintExpr_simplify (c->expr);
1273 }
1274
616915dd 1275 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
1276 {
1277 c = constraint_swapLeftRight(c);
1278 /*I don't think this will be an infinate loop*/
84c9ffbf 1279 c = constraint_simplify(c);
616915dd 1280 }
a779b61e 1281
1282 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1283
616915dd 1284 return c;
1285}
1286
1287
1288
1289
1290/* returns true if fileloc for term1 is closer to file for term2 than term3*/
1291
1292bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3)
1293{
c3e695ff 1294
1295 if (!fileloc_isDefined (loc1) )
1296 return FALSE;
1297
1298 if (!fileloc_isDefined (loc2) )
1299 return FALSE;
1300
1301 if (!fileloc_isDefined (loc3) )
1302 return TRUE;
1303
1304
1305
1306
1307 if (fileloc_equal (loc2, loc3) )
1308 return FALSE;
1309
1310 if (fileloc_equal (loc1, loc2) )
1311 return TRUE;
1312
1313 if (fileloc_equal (loc1, loc3) )
1314 return FALSE;
1315
616915dd 1316 if ( fileloc_lessthan (loc1, loc2) )
1317 {
1318 if (fileloc_lessthan (loc2, loc3) )
1319 {
1320 llassert (fileloc_lessthan (loc1, loc3) );
1321 return TRUE;
1322 }
1323 else
1324 {
1325 return FALSE;
1326 }
1327 }
1328
1329 if ( !fileloc_lessthan (loc1, loc2) )
1330 {
1331 if (!fileloc_lessthan (loc2, loc3) )
1332 {
1333 llassert (!fileloc_lessthan (loc1, loc3) );
1334 return TRUE;
1335 }
1336 else
1337 {
1338 return FALSE;
1339 }
1340 }
1341
1342 llassert(FALSE);
1343 return FALSE;
1344}
1345
1346
This page took 0.276605 seconds and 5 git commands to generate.