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