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