]> andersk Git - splint.git/blame - src/constraintResolve.c
Fixed some /@*i@*/ comments in the code.
[splint.git] / src / constraintResolve.c
CommitLineData
65f973be 1/*
11db3170 2** Splint - annotation-assisted static program checker
c59f5181 3** Copyright (C) 1994-2003 University of Virginia,
65f973be 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**
155af98d 20** For information on splint: info@splint.org
21** To report a bug: splint-bug@splint.org
11db3170 22** For more information: http://www.splint.org
65f973be 23*/
24
616915dd 25/*
26*
27** constraintResolve.c
28*/
29
b7b694d6 30/* #define DEBUGPRINT 1 */
616915dd 31
32# include <ctype.h> /* for isdigit */
1b8ae690 33# include "splintMacros.nf"
616915dd 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
393e573f 42/*@access constraint, exprNode @*/ /*!!! NO! Don't do this so recklessly - design your code more carefully so you don't need to! */
43
28bf4b0b 44static constraint inequalitySubstitute (/*@returned@*/ constraint p_c, constraintList p_p);
616915dd 45
616915dd 46
28bf4b0b 47static bool rangeCheck (arithType p_ar1, /*@observer@*/ constraintExpr p_expr1, arithType p_ar2, /*@observer@*/ constraintExpr p_expr2);
616915dd 48
28bf4b0b 49static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint p_c, constraintList p_p);
616915dd 50
dc7f6a51 51static constraint inequalitySubstituteStrong (/*@returned@*/ constraint p_c, constraintList p_p);
52
28bf4b0b 53static constraint constraint_searchandreplace (/*@returned@*/ constraint p_c, constraintExpr p_old, constraintExpr p_newExpr);
bb25bea6 54
28bf4b0b 55
56static constraint constraint_addOr (/*@returned@*/ constraint p_orig, /*@observer@*/ constraint p_orConstr);
57
35b9a1d9 58static bool resolveOr (/*@temp@*/constraint p_c, /*@observer@*/ /*@temp@*/ constraintList p_list);
28bf4b0b 59
60static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList p_pre2, constraintList p_post1);
616915dd 61
35b9a1d9 62
63/*@only@*/ constraintList constraintList_mergeEnsuresFreeFirst (constraintList list1, constraintList list2)
bb25bea6 64{
35b9a1d9 65 constraintList ret;
66
67 ret = constraintList_mergeEnsures (list1, list2);
68
69 constraintList_free(list1);
bb25bea6 70 return ret;
71}
616915dd 72
35b9a1d9 73/*@only@*/ constraintList constraintList_mergeEnsures (constraintList list1, constraintList list2)
616915dd 74{
75 constraintList ret;
76 constraintList temp;
470b7798 77
35b9a1d9 78 llassert(constraintList_isDefined(list1) );
79 llassert(constraintList_isDefined(list2) );
90bc41f7 80
35b9a1d9 81 DPRINTF(( message ("constraintList_mergeEnsures: list1 %s list2 %s",
82 constraintList_print(list1), constraintList_print(list2)
83 )));
616915dd 84
90bc41f7 85 ret = constraintList_fixConflicts (list1, list2);
bb25bea6 86 ret = reflectChangesEnsuresFree1 (ret, list2);
d46ce6a4 87 temp = constraintList_subsumeEnsures (ret, list2);
35b9a1d9 88 constraintList_free(ret);
9a48d98c 89 ret = temp;
35b9a1d9 90
d46ce6a4 91 temp = constraintList_subsumeEnsures (list2, ret);
35b9a1d9 92
616915dd 93 temp = constraintList_addList (temp, ret);
35b9a1d9 94 constraintList_free(ret);
4ab867d6 95
35b9a1d9 96 DPRINTF(( message ("constraintList_mergeEnsures: returning %s ",
97 constraintList_print(temp) )
98 ));
99
100
616915dd 101 return temp;
616915dd 102}
103
35b9a1d9 104
105/*@only@*/ constraintList constraintList_mergeRequiresFreeFirst (/*@only@*/ constraintList list1, constraintList list2)
bb25bea6 106{
35b9a1d9 107 constraintList ret;
108
109 ret = constraintList_mergeRequires(list1, list2);
110
111 constraintList_free(list1);
112
bb25bea6 113 return ret;
114}
115
35b9a1d9 116/*@only@*/ constraintList constraintList_mergeRequires (constraintList list1, constraintList list2)
470b7798 117{
118 constraintList ret;
119 constraintList temp;
120
35b9a1d9 121 DPRINTF((message ("constraintList_mergeRequires: merging %s and %s ", constraintList_print (list1), constraintList_print(list2) ) ) );
470b7798 122
35b9a1d9 123 if (context_getFlag (FLG_REDUNDANTCONSTRAINTS) )
4a689c7f 124 {
35b9a1d9 125 ret = constraintList_copy(list1);
126 ret = constraintList_addList(ret, list2);
4a689c7f 127 return ret;
128 }
35b9a1d9 129
470b7798 130 /* get constraints in list1 not satified by list2 */
35b9a1d9 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) ) ) );
470b7798 138
4ab867d6 139 ret = constraintList_addListFree (ret, temp);
470b7798 140
35b9a1d9 141 DPRINTF((message ("constraintList_mergeRequires: returning %s", constraintList_print(ret) ) ) );
142
470b7798 143 return ret;
144}
616915dd 145
28bf4b0b 146/* old name mergeResolve renamed for czech naming convention */
147void exprNode_mergeResolve (exprNode parent, exprNode child1, exprNode child2)
616915dd 148{
d46ce6a4 149 constraintList temp, temp2;
616915dd 150
35b9a1d9 151 DPRINTF((message ("magically merging constraint into parent:%s for", exprNode_unparse (parent) )) );
616915dd 152
35b9a1d9 153 DPRINTF((message (" children: %s and %s", exprNode_unparse (child1), exprNode_unparse(child2) ) ) );
616915dd 154
35b9a1d9 155
156 if (exprNode_isUndefined(parent) )
a64ebe74 157 {
35b9a1d9 158 llassert (exprNode_isDefined(parent) );
a64ebe74 159 return;
160 }
161
162
35b9a1d9 163 if (exprNode_isError (child1) || exprNode_isError(child2) )
d46ce6a4 164 {
35b9a1d9 165 if (exprNode_isError (child1) && !exprNode_isError(child2) )
616915dd 166 {
35b9a1d9 167 constraintList_free(parent->requiresConstraints);
168
616915dd 169 parent->requiresConstraints = constraintList_copy (child2->requiresConstraints);
35b9a1d9 170 constraintList_free(parent->ensuresConstraints);
d46ce6a4 171
616915dd 172 parent->ensuresConstraints = constraintList_copy (child2->ensuresConstraints);
35b9a1d9 173 DPRINTF((message ("Copied child constraints: pre: %s and post: %s",
174 constraintList_print( child2->requiresConstraints),
175 constraintList_print (child2->ensuresConstraints)
176 )
177 ));
616915dd 178 return;
179 }
180 else
181 {
35b9a1d9 182 llassert(exprNode_isError(child2) );
616915dd 183 return;
184 }
185 }
186
35b9a1d9 187 llassert(!exprNode_isError (child1) && ! exprNode_isError(child2) );
616915dd 188
35b9a1d9 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 ) ) );
616915dd 226
227}
35b9a1d9 228
229
230
616915dd 231
35b9a1d9 232/*@only@*/ constraintList constraintList_subsumeEnsures (constraintList list1, constraintList list2)
616915dd 233{
35b9a1d9 234 constraintList ret;
235 ret = constraintList_makeNew();
616915dd 236 constraintList_elements (list1, el)
237 {
35b9a1d9 238
239 DPRINTF ((message ("Examining %s", constraint_print (el) ) ) );
240 if (!constraintList_resolve (el, list2) )
616915dd 241 {
35b9a1d9 242 constraint temp;
243 temp = constraint_copy(el);
d46ce6a4 244 ret = constraintList_add (ret, temp);
616915dd 245 }
246 else
247 {
35b9a1d9 248 DPRINTF ((message ("Subsuming %s", constraint_print (el) ) ) );
616915dd 249 }
250 } end_constraintList_elements;
251
252 return ret;
253}
254
35b9a1d9 255
256
257/*used to be reflectChangesFreePre renamed for Czech naming conventino*/
bb25bea6 258/* tries to resolve constraints in list pre2 using post1 */
35b9a1d9 259/*@only@*/ constraintList constraintList_reflectChangesFreePre (/*@only@*/ constraintList pre2, /*@observer@*/ constraintList post1)
bb25bea6 260{
35b9a1d9 261 constraintList ret;
262
263 ret = constraintList_reflectChanges(pre2, post1);
264
bb25bea6 265 constraintList_free (pre2);
35b9a1d9 266
bb25bea6 267 return ret;
268}
269
270
dc7f6a51 271
90bc41f7 272/* tries to resolve constraints in list pre2 using post1 */
35b9a1d9 273
274static /*@only@*/ constraintList reflectChangesNoOr (/*@observer@*/ /*@temp@*/ constraintList pre2, /*@observer@*/ /*@temp@*/ constraintList post1)
616915dd 275{
9a48d98c 276
35b9a1d9 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) )));
616915dd 285
286 constraintList_elements (pre2, el)
287 {
35b9a1d9 288 if (!constraintList_resolve (el, post1) )
616915dd 289 {
35b9a1d9 290 temp = constraint_substitute (el, post1);
291 if (!constraintList_resolve (temp, post1) )
616915dd 292 {
b7b694d6 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 */
35b9a1d9 297 temp2 = constraint_copy (temp);
920a3797 298 temp2 = inequalitySubstitute (temp2, post1);
35b9a1d9 299 if (!constraintList_resolve (temp2, post1) )
920a3797 300 {
301 temp2 = inequalitySubstituteUnsound (temp2, post1);
35b9a1d9 302 if (!constraintList_resolve (temp2, post1) )
303 ret = constraintList_add (ret, temp2);
920a3797 304 else
35b9a1d9 305 constraint_free(temp2);
920a3797 306 }
307 else
308 {
35b9a1d9 309 constraint_free(temp2);
920a3797 310 }
616915dd 311 }
35b9a1d9 312 constraint_free(temp);
616915dd 313 }
314 } end_constraintList_elements;
315
35b9a1d9 316 DPRINTF((message ("reflectChanges: returning %s", constraintList_print(ret) ) ) );
317 return ret;
616915dd 318}
319
dc7f6a51 320/* tries to resolve constraints in list pre2 using post1 */
35b9a1d9 321/*@only@*/ constraintList constraintList_reflectChanges(/*@observer@*/ constraintList pre2, /*@observer@*/ constraintList post1)
dc7f6a51 322{
35b9a1d9 323 constraintList temp;
324
325 if ( context_getFlag (FLG_ORCONSTRAINT) )
326
327 temp = constraintList_reflectChangesOr (pre2, post1);
dc7f6a51 328 else
35b9a1d9 329 temp = reflectChangesNoOr(pre2, post1);
330
331 return temp;
dc7f6a51 332}
616915dd 333
35b9a1d9 334static constraint constraint_addOr (/*@returned@*/ constraint orig, /*@observer@*/ constraint orConstr)
90bc41f7 335{
35b9a1d9 336 constraint c;
a64ebe74 337
35b9a1d9 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) ) ));
90bc41f7 343
344 while (c->or != NULL)
345 {
346 c = c->or;
347 }
a779b61e 348
35b9a1d9 349 c->or = constraint_copy(orConstr);
350
351 DPRINTF((message("constraint_addor: returning %s",constraint_printOr(orig) ) ));
90bc41f7 352
353 return orig;
354}
355
35b9a1d9 356
357static bool resolveOr ( /*@temp@*/ constraint c, /*@observer@*/ /*@temp@*/ constraintList list)
90bc41f7 358{
35b9a1d9 359 constraint temp;
360
361 int numberOr;
362
363 numberOr = 0;
364
365 llassert(constraint_isDefined(c) );
a64ebe74 366
35b9a1d9 367 DPRINTF(( message("resolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(list) ) ));
a64ebe74 368
35b9a1d9 369 temp = c;
370
90bc41f7 371 do
372 {
35b9a1d9 373 if (constraintList_resolve (temp, list) )
374 return TRUE;
90bc41f7 375 temp = temp->or;
a779b61e 376 numberOr++;
377 llassert(numberOr <= 10);
90bc41f7 378 }
35b9a1d9 379 while (constraint_isDefined(temp));
90bc41f7 380
381 return FALSE;
382}
383
35b9a1d9 384/*This is a "helper" function for doResolveOr */
90bc41f7 385
bb25bea6 386static /*@only@*/ constraint doResolve (/*@only@*/ constraint c, constraintList post1, bool * resolved)
90bc41f7 387{
35b9a1d9 388 constraint temp;
a779b61e 389
35b9a1d9 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 ));
90bc41f7 396
35b9a1d9 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 ));
d9daf826 406
35b9a1d9 407 if (!resolveOr (temp, post1) )
90bc41f7 408 {
b7b694d6 409 /* try inequality substitution */
35b9a1d9 410 constraint temp2;
bb25bea6 411
35b9a1d9 412 /* the inequality substitution may cause us to lose information
413 so we don't want to store the result but we do anyway
b7b694d6 414 */
35b9a1d9 415 temp2 = constraint_copy (c);
b7b694d6 416 temp2 = inequalitySubstitute (temp2, post1);
417
35b9a1d9 418 if (!resolveOr (temp2, post1) )
b7b694d6 419 {
35b9a1d9 420 constraint temp3;
421 temp3 = constraint_copy(temp2);
422
b7b694d6 423 temp3 = inequalitySubstituteStrong (temp3, post1);
35b9a1d9 424 if (!resolveOr (temp3, post1) )
bb25bea6 425 {
b7b694d6 426 temp2 = inequalitySubstituteUnsound (temp2, post1);
35b9a1d9 427 if (!resolveOr (temp2, post1) )
dc7f6a51 428 {
35b9a1d9 429 if (!constraint_same (temp, temp2) )
d9daf826 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
35b9a1d9 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
d9daf826 450 temp = constraint_addOr (temp, tempSub);
35b9a1d9 451 constraint_free(tempSub);
452
d9daf826 453 }
35b9a1d9 454 if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) )
d9daf826 455 {
35b9a1d9 456 /* drl added 8/28/2002*/
457 /*make sure that the information from
458 a post condition like i = i + 1 is transfered
d9daf826 459 */
460 constraint tempSub;
461
462 tempSub = constraint_substitute (temp3, post1);
463
35b9a1d9 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 ));
d9daf826 476
477 temp = constraint_addOr (temp, tempSub);
35b9a1d9 478
d9daf826 479 constraint_free(tempSub);
480 }
b7b694d6 481 *resolved = FALSE;
482
35b9a1d9 483 constraint_free(temp2);
484 constraint_free(temp3);
485 constraint_free(c);
b7b694d6 486
487 return temp;
bb25bea6 488 }
35b9a1d9 489 constraint_free(temp2);
490 constraint_free(temp3);
bb25bea6 491 }
dc7f6a51 492 else
493 {
35b9a1d9 494 constraint_free(temp2);
495 constraint_free(temp3);
b7b694d6 496 }
90bc41f7 497 }
b7b694d6 498 else
499 {
35b9a1d9 500 constraint_free(temp2);
b7b694d6 501 }
502
90bc41f7 503 }
35b9a1d9 504 constraint_free(temp);
b7b694d6 505 }
35b9a1d9 506 constraint_free(c);
507
8fd556fb 508 *resolved = TRUE;
b7b694d6 509 return NULL;
90bc41f7 510}
511
35b9a1d9 512static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved)
90bc41f7 513{
514 constraint ret;
515 constraint next;
516 constraint curr;
35b9a1d9 517
a779b61e 518
35b9a1d9 519 DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) ));
520
521
9a48d98c 522
8fd556fb 523 *resolved = FALSE;
35b9a1d9 524
525 llassert(constraint_isDefined(c) );
526
527 ret = constraint_copy(c);
a779b61e 528
35b9a1d9 529 llassert(constraint_isDefined(ret) );
a64ebe74 530
35b9a1d9 531 if (constraintList_isEmpty(post1) )
a779b61e 532 {
533 return ret;
534 }
535
90bc41f7 536 next = ret->or;
537 ret->or = NULL;
538
539 ret = doResolve (ret, post1, resolved);
35b9a1d9 540
bb25bea6 541 if (*resolved)
542 {
2934b455 543 if (next != NULL)
35b9a1d9 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);
920a3797 548
a779b61e 549 return NULL;
bb25bea6 550 }
a779b61e 551
84c9ffbf 552 while (next != NULL)
90bc41f7 553 {
554 curr = next;
555 next = curr->or;
556 curr->or = NULL;
35b9a1d9 557
90bc41f7 558 curr = doResolve (curr, post1, resolved);
a779b61e 559
8fd556fb 560 if (*resolved)
bb25bea6 561 {
4ab867d6 562 /* curr is null so we don't try to free it*/
35b9a1d9 563 llassert(curr == NULL);
4ab867d6 564
2934b455 565 if (next != NULL)
35b9a1d9 566 constraint_free(next);
920a3797 567
35b9a1d9 568 constraint_free(ret);
920a3797 569 return NULL;
bb25bea6 570 }
90bc41f7 571 ret = constraint_addOr (ret, curr);
4ab867d6 572 constraint_free(curr);
90bc41f7 573 }
90bc41f7 574 return ret;
575}
576
90bc41f7 577/* tries to resolve constraints in list pr2 using post1 */
35b9a1d9 578/*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1)
90bc41f7 579{
580 bool resolved;
581 constraintList ret;
582 constraint temp;
c3e695ff 583 ret = constraintList_makeNew();
35b9a1d9 584 DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) )));
90bc41f7 585
586 constraintList_elements (pre2, el)
587 {
588 temp = doResolveOr (el, post1, &resolved);
35b9a1d9 589
90bc41f7 590 if (!resolved)
591 {
592 ret = constraintList_add(ret, temp);
593 }
920a3797 594 else
595 {
35b9a1d9 596 /* we don't need to free temp when
597 resolved is false because temp is null */
598 llassert(temp == NULL);
920a3797 599 }
35b9a1d9 600
90bc41f7 601 } end_constraintList_elements;
35b9a1d9 602
603 DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) );
604 return ret;
90bc41f7 605}
a779b61e 606
35b9a1d9 607static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1)
616915dd 608{
35b9a1d9 609 constraintList ret;
610 constraint temp;
611 ret = constraintList_makeNew();
616915dd 612 constraintList_elements (pre2, el)
613 {
35b9a1d9 614 if (!constraintList_resolve (el, post1) )
616915dd 615 {
35b9a1d9 616 temp = constraint_substitute (el, post1);
616915dd 617 llassert (temp != NULL);
35b9a1d9 618
619 if (!constraintList_resolve (temp, post1) )
620 ret = constraintList_add (ret, temp);
920a3797 621 else
35b9a1d9 622 constraint_free(temp);
616915dd 623 }
624 else
625 {
35b9a1d9 626 DPRINTF ((message ("Resolved away %s ", constraint_print(el) ) ) );
616915dd 627 }
628 } end_constraintList_elements;
35b9a1d9 629
630 return ret;
616915dd 631}
632
633
35b9a1d9 634static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1)
bb25bea6 635{
35b9a1d9 636 constraintList ret;
637
638 ret = reflectChangesEnsures (pre2, post1);
639
bb25bea6 640 constraintList_free(pre2);
35b9a1d9 641
bb25bea6 642 return ret;
643}
644
645
646static bool constraint_conflict (constraint c1, constraint c2)
616915dd 647{
a64ebe74 648
35b9a1d9 649 llassert(constraint_isDefined(c1) );
650 llassert(constraint_isDefined(c2) );
651
652 if (constraintExpr_similar(c1->lexpr, c2->lexpr) )
616915dd 653 {
35b9a1d9 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 }
616915dd 660 }
35b9a1d9 661
662 /* This is a slight kludg to prevent circular constraints like
663 strlen(str) == maxRead(s) + strlen(str);
b7b694d6 664 */
35b9a1d9 665
b941db6b 666 /*this code is functional but it may be worth cleaning up at some point. */
a779b61e 667
35b9a1d9 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) )
a779b61e 673 {
35b9a1d9 674 constraintTerm term;
a779b61e 675
35b9a1d9 676 term = constraintExpr_getTerm(c1->lexpr);
677
678 if (constraintTerm_isExprNode(term) )
a779b61e 679 {
35b9a1d9 680 DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) );
a779b61e 681 return TRUE;
682 }
683 }
35b9a1d9 684 }
616915dd 685
35b9a1d9 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 }
9a48d98c 691
35b9a1d9 692 DPRINTF ((message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) );
693
616915dd 694 return FALSE;
35b9a1d9 695
616915dd 696}
697
35b9a1d9 698static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/
616915dd 699{
35b9a1d9 700 llassert(constraint_isDefined(conflicting) );
a64ebe74 701
35b9a1d9 702 if (conflicting->ar ==EQ )
616915dd 703 {
35b9a1d9 704 llassert(constraint_isDefined(good) );
616915dd 705 good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr);
d46ce6a4 706 good = constraint_simplify (good);
616915dd 707 }
35b9a1d9 708
709
616915dd 710}
711
bb25bea6 712static bool conflict (constraint c, constraintList list)
616915dd 713{
35b9a1d9 714
90bc41f7 715 constraintList_elements (list, el)
616915dd 716 {
35b9a1d9 717 if ( constraint_conflict(el, c) )
616915dd 718 {
719 constraint_fixConflict (el, c);
720 return TRUE;
721 }
722 } end_constraintList_elements;
35b9a1d9 723
724 return FALSE;
725
616915dd 726}
727
b7b694d6 728/*
35b9a1d9 729 check if constraint in list1 conflicts with constraints in List2. If so we
730 remove form list1 and change list2.
b7b694d6 731*/
732
84c9ffbf 733constraintList constraintList_fixConflicts (constraintList list1, constraintList list2)
616915dd 734{
35b9a1d9 735 constraintList ret;
736 ret = constraintList_makeNew();
737 llassert(constraintList_isDefined(list1) );
616915dd 738 constraintList_elements (list1, el)
739 {
35b9a1d9 740 if (! conflict (el, list2) )
616915dd 741 {
35b9a1d9 742 constraint temp;
743 temp = constraint_copy(el);
d46ce6a4 744 ret = constraintList_add (ret, temp);
616915dd 745 }
746 } end_constraintList_elements;
35b9a1d9 747
748 return ret;
616915dd 749}
750
35b9a1d9 751/*returns true if constraint post satifies cosntriant pre */
bb25bea6 752static bool satifies (constraint pre, constraint post)
616915dd 753{
35b9a1d9 754 llassert(constraint_isDefined(pre) );
755 llassert(constraint_isDefined(post) );
a64ebe74 756
35b9a1d9 757 if (constraint_isAlwaysTrue (pre) )
758 return TRUE;
616915dd 759
35b9a1d9 760 if (!constraintExpr_similar (pre->lexpr, post->lexpr) )
616915dd 761 {
35b9a1d9 762 return FALSE;
616915dd 763 }
35b9a1d9 764 if (constraintExpr_isUndefined(post->expr))
616915dd 765 {
35b9a1d9 766 llassert(FALSE);
616915dd 767 return FALSE;
768 }
35b9a1d9 769
616915dd 770 return rangeCheck (pre->ar, pre->expr, post->ar, post->expr);
771}
772
35b9a1d9 773
774bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p)
bb25bea6 775{
776 constraintList_elements (p, el)
777 {
35b9a1d9 778 if ( satifies (c, el) )
bb25bea6 779 {
35b9a1d9 780 DPRINTF ((message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) );
bb25bea6 781 return TRUE;
782 }
35b9a1d9 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) ) ));
bb25bea6 787 return FALSE;
788}
789
790static bool arithType_canResolve (arithType ar1, arithType ar2)
616915dd 791{
792 switch (ar1)
793 {
794 case GTE:
795 case GT:
35b9a1d9 796 if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ) )
616915dd 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:
35b9a1d9 809 if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ) )
616915dd 810 return TRUE;
84c9ffbf 811 break;
616915dd 812 default:
813 return FALSE;
814 }
815 return FALSE;
816}
817
35b9a1d9 818/*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/
e5f31c00 819static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2)
820{
821 constraintTerm ct;
822 exprNode e, t;
823 sRef s1, s2;
a64ebe74 824
35b9a1d9 825 llassert(constraintExpr_isDefined(buf1) && constraintExpr_isDefined(expr2) );
a64ebe74 826
e5f31c00 827 /*@access constraintExpr@*/
828
35b9a1d9 829 if ((expr2->kind != term) && (buf1->kind != term) )
830 return FALSE;
831
e5f31c00 832
833 ct = constraintExprData_termGetTerm(expr2->data);
834
35b9a1d9 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;
a64ebe74 844
e5f31c00 845 if (e->kind != XPR_SIZEOF)
35b9a1d9 846 return FALSE;
e5f31c00 847
848 t = exprData_getSingle (e->edata);
9a48d98c 849 s1 = exprNode_getSref (t);
35b9a1d9 850
851 s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) );
e5f31c00 852
b941db6b 853 /*drl this may be the wronge thing to test for but this
854 seems to work correctly*/
35b9a1d9 855 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
e5f31c00 856 {
b941db6b 857 /* origly checked that ctype_isFixedArray(sRef_getType(s2)) but
858 removed that test */
e5f31c00 859 return TRUE;
860 }
861 return FALSE;
862}
863
864/* look for the special case of
865 maxSet(buf) >= sizeof(buf) - 1
866*/
867
b941db6b 868/*drl eventually it would be good to check that
869 buf is of type char.*/
332e22fa 870
e5f31c00 871static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c)
872{
873 constraintExpr l, r, buf1, buf2, con;
874
35b9a1d9 875 DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) )
876 ));
877
878 llassert (constraint_isDefined(c) );
879
e5f31c00 880 l = c->lexpr;
881 r = c->expr;
882
35b9a1d9 883 if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) )
884 return FALSE;
e5f31c00 885
35b9a1d9 886 llassert (constraintExpr_isDefined(l) );
887 llassert (constraintExpr_isDefined(r) );
a64ebe74 888
35b9a1d9 889 /*check if the constraintExpr is MaxSet(buf) */
e5f31c00 890 if (l->kind == unaryExpr)
891 {
892 if (constraintExprData_unaryExprGetOp(l->data) == MAXSET)
893 {
894 buf1 = constraintExprData_unaryExprGetExpr(l->data);
895 }
896 else
35b9a1d9 897 return FALSE;
e5f31c00 898 }
899 else
35b9a1d9 900 return FALSE;
901
e5f31c00 902
903 if (r->kind != binaryexpr)
35b9a1d9 904 return FALSE;
e5f31c00 905
35b9a1d9 906 buf2 = constraintExprData_binaryExprGetExpr1(r->data);
907 con = constraintExprData_binaryExprGetExpr2(r->data);
e5f31c00 908
35b9a1d9 909 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS)
e5f31c00 910 {
35b9a1d9 911 if (constraintExpr_canGetValue(con) )
e5f31c00 912 {
35b9a1d9 913 long i;
914
915 i = constraintExpr_getValue(con);
e5f31c00 916 if (i != 1)
917 {
918 return FALSE;
919 }
920 }
921 else
35b9a1d9 922 return FALSE;
e5f31c00 923 }
924
35b9a1d9 925 if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS)
e5f31c00 926 {
35b9a1d9 927 if (constraintExpr_canGetValue(con) )
e5f31c00 928 {
35b9a1d9 929 long i;
e5f31c00 930
35b9a1d9 931 i = constraintExpr_getValue(con);
e5f31c00 932 if (i != -1)
933 {
934 return FALSE;
935 }
936 }
937 else
35b9a1d9 938 return FALSE;
e5f31c00 939 }
35b9a1d9 940
941 if (sizeofBufComp(buf1, buf2))
942 {
943 return TRUE;
944 }
945 else
946 {
947 return FALSE;
948 }
e5f31c00 949}
950/*@noaccess constraintExpr@*/
951
90bc41f7 952/* We look for constraint which are tautologies */
953
28bf4b0b 954bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c)
616915dd 955{
956 constraintExpr l, r;
15b3d2b2 957 bool rHasConstant;
958 int rConstant;
a64ebe74 959
35b9a1d9 960
961 llassert (constraint_isDefined(c) );
9280addf 962
616915dd 963 l = c->lexpr;
964 r = c->expr;
90bc41f7 965
35b9a1d9 966 DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) ));
e5f31c00 967
35b9a1d9 968 if (sizeOfMaxSet(c) )
e5f31c00 969 return TRUE;
970
35b9a1d9 971 if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) )
616915dd 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:
9280addf 989 BADEXIT;
84c9ffbf 990 /*@notreached@*/
9280addf 991 break;
992 }
993 }
994
35b9a1d9 995 if (constraintExpr_similar (l,r) )
9280addf 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;
84c9ffbf 1009 /*@notreached@*/
616915dd 1010 break;
1011 }
1012 }
9280addf 1013
1014 l = constraintExpr_copy (c->lexpr);
1015 r = constraintExpr_copy (c->expr);
1016
9280addf 1017 r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant);
1018
35b9a1d9 1019 if (constraintExpr_similar (l,r) && (rHasConstant ) )
9280addf 1020 {
35b9a1d9 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 ) ));
bb25bea6 1023
1024 constraintExpr_free(l);
1025 constraintExpr_free(r);
1026
90bc41f7 1027 switch (c->ar)
9280addf 1028 {
90bc41f7 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;
84c9ffbf 1042 /*@notreached@*/
90bc41f7 1043 break;
9280addf 1044 }
9280addf 1045 }
90bc41f7 1046 else
1047 {
bb25bea6 1048 constraintExpr_free(l);
1049 constraintExpr_free(r);
35b9a1d9 1050 DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) ));
90bc41f7 1051 return FALSE;
1052 }
9280addf 1053
1054 BADEXIT;
616915dd 1055}
1056
bb25bea6 1057static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2)
616915dd 1058
1059{
35b9a1d9 1060 DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) ));
616915dd 1061
35b9a1d9 1062 if (! arithType_canResolve (ar1, ar2) )
616915dd 1063 return FALSE;
1064
1065 switch (ar1)
1066 {
1067 case GTE:
35b9a1d9 1068 if (constraintExpr_similar (expr1, expr2) )
2681ee39 1069 return TRUE;
1070 /*@fallthrough@*/
1071 case GT:
616915dd 1072 if (! (constraintExpr_canGetValue (expr1) &&
35b9a1d9 1073 constraintExpr_canGetValue (expr2) ) )
2681ee39 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)
35b9a1d9 1095 if (constraintExpr_similar (e1, e2) )
2681ee39 1096 {
1097 constraintExpr_free(e1);
1098 constraintExpr_free(e2);
1099 return TRUE;
1100 }
1101 }
bb7c2085 1102 DPRINTF(("Can't Get value"));
2681ee39 1103
1104 constraintExpr_free(e1);
1105 constraintExpr_free(e2);
1106 return FALSE;
1107 }
1108
1109 if (constraintExpr_compare (expr2, expr1) >= 0)
1110 return TRUE;
616915dd 1111
1112 return FALSE;
2681ee39 1113 case EQ:
35b9a1d9 1114 if (constraintExpr_similar (expr1, expr2) )
2681ee39 1115 return TRUE;
1116
1117 return FALSE;
1118 case LTE:
35b9a1d9 1119 if (constraintExpr_similar (expr1, expr2) )
2681ee39 1120 return TRUE;
1121 /*@fallthrough@*/
1122 case LT:
1123 if (! (constraintExpr_canGetValue (expr1) &&
35b9a1d9 1124 constraintExpr_canGetValue (expr2) ) )
2681ee39 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)
35b9a1d9 1146 if (constraintExpr_similar (e1, e2) )
2681ee39 1147 {
1148 constraintExpr_free(e1);
1149 constraintExpr_free(e2);
1150 return TRUE;
1151 }
1152 }
1153 constraintExpr_free(e1);
1154 constraintExpr_free(e2);
1155
bb7c2085 1156 DPRINTF(("Can't Get value"));
2681ee39 1157 return FALSE;
1158 }
1159
1160 if (constraintExpr_compare (expr2, expr1) <= 0)
1161 return TRUE;
1162
1163 return FALSE;
1164
1165 default:
35b9a1d9 1166 llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) );
2681ee39 1167 }
616915dd 1168 BADEXIT;
616915dd 1169}
1170
28bf4b0b 1171static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr)
616915dd 1172{
35b9a1d9 1173 llassert (constraint_isDefined(c) );
a64ebe74 1174
35b9a1d9 1175 DPRINTF (("Doing replace for lexpr") );
1176
28bf4b0b 1177 c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr);
35b9a1d9 1178 DPRINTF (("Doing replace for expr") );
28bf4b0b 1179 c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr);
616915dd 1180 return c;
1181}
1182
84c9ffbf 1183bool constraint_search (constraint c, constraintExpr old) /*@*/
616915dd 1184{
35b9a1d9 1185 bool ret;
1186 ret = FALSE;
a64ebe74 1187
35b9a1d9 1188 llassert (constraint_isDefined(c) );
a64ebe74 1189
616915dd 1190 ret = constraintExpr_search (c->lexpr, old);
1191 ret = ret || constraintExpr_search (c->expr, old);
1192 return ret;
1193}
1194
b7b694d6 1195/* adjust file locs and stuff */
bb25bea6 1196static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old)
616915dd 1197{
1198 fileloc loc1, loc2, loc3;
1199
35b9a1d9 1200 DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute),
1201 constraint_print(old))
1202 ));
616915dd 1203
35b9a1d9 1204 llassert(constraint_isDefined(substitute));
1205 llassert(constraint_isDefined(old));
a64ebe74 1206
616915dd 1207 loc1 = constraint_getFileloc (old);
616915dd 1208 loc2 = constraintExpr_getFileloc (substitute->lexpr);
616915dd 1209 loc3 = constraintExpr_getFileloc (substitute->expr);
616915dd 1210
b7b694d6 1211 /* special case of an equality that "contains itself" */
35b9a1d9 1212 if (constraintExpr_search (substitute->expr, substitute->lexpr) )
616915dd 1213 if (fileloc_closer (loc1, loc3, loc2))
35b9a1d9 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
bb25bea6 1224 fileloc_free (loc1);
1225 fileloc_free (loc2);
1226 fileloc_free (loc3);
35b9a1d9 1227
616915dd 1228 return substitute;
35b9a1d9 1229
616915dd 1230}
1231
35b9a1d9 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 */
dc7f6a51 1238
35b9a1d9 1239constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p)
616915dd 1240{
35b9a1d9 1241 llassert(constraint_isDefined(c) );
1242
616915dd 1243 if (c->ar != GTE)
35b9a1d9 1244 return c;
616915dd 1245
1246 constraintList_elements (p, el)
1247 {
a64ebe74 1248
35b9a1d9 1249 llassert(constraint_isDefined(el) );
1250
1251 if ((el->ar == LT ) )
35b9a1d9 1252 {
1253 constraintExpr temp2;
1254
35b9a1d9 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);
616915dd 1273 return c;
1274}
1275
35b9a1d9 1276
dc7f6a51 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
35b9a1d9 1287static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p)
dc7f6a51 1288{
35b9a1d9 1289 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) ));
dc7f6a51 1290
35b9a1d9 1291 llassert(constraint_isDefined(c) );
1292
1293 if (! (constraint_isDefined(c) ) )
1294 {
1295 return c;
1296 }
a64ebe74 1297
dc7f6a51 1298 if (c->ar != GTE)
35b9a1d9 1299 return c;
dc7f6a51 1300
35b9a1d9 1301 DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q",
1302 constraint_print(c), constraintList_print(p) ) ));
dc7f6a51 1303 constraintList_elements (p, el)
1304 {
9a48d98c 1305
35b9a1d9 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 ) )
35b9a1d9 1310 {
1311 constraintExpr temp2;
1312
35b9a1d9 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;
dc7f6a51 1335
1336 c = constraint_simplify(c);
1337 return c;
1338}
1339
1340
90bc41f7 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
35b9a1d9 1348static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p)
90bc41f7 1349{
35b9a1d9 1350 DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) ));
1351
1352 llassert(constraint_isDefined(c) );
90bc41f7 1353
1354 if (c->ar != GTE)
35b9a1d9 1355 return c;
90bc41f7 1356
1357 constraintList_elements (p, el)
1358 {
35b9a1d9 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) )
35b9a1d9 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);
90bc41f7 1384 return c;
1385}
1386
28bf4b0b 1387/*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p)
616915dd 1388{
35b9a1d9 1389 constraint ret;
1390
1391 ret = constraint_copy(c);
616915dd 1392 constraintList_elements (p, el)
1393 {
35b9a1d9 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
90bc41f7 1424 return ret;
616915dd 1425}
1426
35b9a1d9 1427
1428/*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList)
bb25bea6 1429{
35b9a1d9 1430constraintList ret;
1431
1432ret = constraintList_substitute (target, subList);
1433
1434constraintList_free(target);
1435
1436return ret;
bb25bea6 1437}
1438
c3e695ff 1439/* we try to do substitutions on each constraint in target using the constraint in sublist*/
1440
35b9a1d9 1441/*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList)
c3e695ff 1442{
35b9a1d9 1443
1444 constraintList ret;
1445
1446 ret = constraintList_makeNew();
c3e695ff 1447
1448 constraintList_elements(target, el)
35b9a1d9 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;
84c9ffbf 1457
bb25bea6 1458 return ret;
c3e695ff 1459}
616915dd 1460
28bf4b0b 1461static constraint constraint_solve (/*@returned@*/ constraint c)
616915dd 1462{
35b9a1d9 1463
1464 llassert(constraint_isDefined(c) );
1465
1466 DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) );
616915dd 1467 c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr);
35b9a1d9 1468 DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) );
1469
616915dd 1470 return c;
1471}
1472
1473static arithType flipAr (arithType ar)
1474{
1475 switch (ar)
1476 {
35b9a1d9 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"));
616915dd 1489 }
1490 BADEXIT;
1491}
1492
35b9a1d9 1493static constraint constraint_swapLeftRight (/*@returned@*/ constraint c)
616915dd 1494{
1495 constraintExpr temp;
a64ebe74 1496
35b9a1d9 1497 llassert(constraint_isDefined(c) );
a64ebe74 1498
616915dd 1499 c->ar = flipAr (c->ar);
1500 temp = c->lexpr;
1501 c->lexpr = c->expr;
1502 c->expr = temp;
35b9a1d9 1503 DPRINTF(("Swaped left and right sides of constraint"));
616915dd 1504 return c;
1505}
1506
35b9a1d9 1507
1508
1509constraint constraint_simplify ( /*@returned@*/ constraint c)
616915dd 1510{
35b9a1d9 1511
1512 llassert(constraint_isDefined(c) );
1513
1514 DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) ));
a779b61e 1515
1516 if (constraint_tooDeep(c))
1517 {
35b9a1d9 1518 DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) ));
a779b61e 1519 return c;
35b9a1d9 1520
a779b61e 1521 }
1522
616915dd 1523 c->lexpr = constraintExpr_simplify (c->lexpr);
1524 c->expr = constraintExpr_simplify (c->expr);
616915dd 1525
35b9a1d9 1526 if (constraintExpr_isBinaryExpr (c->lexpr) )
c3e695ff 1527 {
1528 c = constraint_solve (c);
35b9a1d9 1529
c3e695ff 1530 c->lexpr = constraintExpr_simplify (c->lexpr);
1531 c->expr = constraintExpr_simplify (c->expr);
1532 }
1533
35b9a1d9 1534 if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) )
616915dd 1535 {
35b9a1d9 1536 c = constraint_swapLeftRight(c);
1537 /*I don't think this will be an infinate loop*/
1538 c = constraint_simplify(c);
616915dd 1539 }
a779b61e 1540
35b9a1d9 1541 DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) ));
1542
616915dd 1543 return c;
1544}
1545
1546
35b9a1d9 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)
616915dd 1552{
35b9a1d9 1553
1554 if (!fileloc_isDefined (loc1) )
c3e695ff 1555 return FALSE;
35b9a1d9 1556
1557 if (!fileloc_isDefined (loc2) )
c3e695ff 1558 return FALSE;
1559
35b9a1d9 1560 if (!fileloc_isDefined (loc3) )
c3e695ff 1561 return TRUE;
35b9a1d9 1562
c3e695ff 1563
9a48d98c 1564
9a48d98c 1565
35b9a1d9 1566 if (fileloc_equal (loc2, loc3) )
c3e695ff 1567 return FALSE;
35b9a1d9 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;
616915dd 1603}
1604
1605
This page took 0.328976 seconds and 5 git commands to generate.