]>
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 ) ); | |
d9daf826 | 387 | |
388 | DPRINTF((message("doResolve:: call on constraint c = : %q and constraintList %q", | |
389 | constraint_printOr(c), constraintList_print(post1) | |
390 | ) | |
391 | )); | |
90bc41f7 | 392 | |
a779b61e | 393 | if (!resolveOr (c, post1) ) |
394 | { | |
395 | ||
396 | temp = constraint_substitute (c, post1); | |
397 | ||
d9daf826 | 398 | DPRINTF((message("doResolve:: after substitute temp is %q", |
399 | constraint_printOr(temp) | |
400 | ) | |
401 | )); | |
402 | ||
a779b61e | 403 | if (!resolveOr (temp, post1) ) |
90bc41f7 | 404 | { |
b7b694d6 | 405 | /* try inequality substitution */ |
a779b61e | 406 | constraint temp2; |
bb25bea6 | 407 | |
b7b694d6 | 408 | /* the inequality substitution may cause us to lose information |
409 | so we don't want to store the result but we do anyway | |
410 | */ | |
411 | temp2 = constraint_copy (c); | |
412 | temp2 = inequalitySubstitute (temp2, post1); | |
413 | ||
414 | if (!resolveOr (temp2, post1) ) | |
415 | { | |
416 | constraint temp3; | |
417 | temp3 = constraint_copy(temp2); | |
418 | ||
419 | temp3 = inequalitySubstituteStrong (temp3, post1); | |
420 | if (!resolveOr (temp3, post1) ) | |
bb25bea6 | 421 | { |
b7b694d6 | 422 | temp2 = inequalitySubstituteUnsound (temp2, post1); |
423 | if (!resolveOr (temp2, post1) ) | |
dc7f6a51 | 424 | { |
b7b694d6 | 425 | if (!constraint_same (temp, temp2) ) |
d9daf826 | 426 | { |
427 | /* drl added 8/28/2002*/ | |
428 | /*make sure that the information from | |
429 | a post condition like i = i + 1 is transfered | |
430 | */ | |
431 | constraint tempSub; | |
432 | tempSub = constraint_substitute (temp2, post1); | |
433 | ||
434 | DPRINTF(( | |
435 | message("doResolve: adding %s ", | |
436 | constraint_printOr(tempSub) | |
437 | ) | |
438 | )); | |
439 | ||
440 | DPRINTF(( | |
441 | message("doResolve: not adding %s ", | |
442 | constraint_printOr(temp2) | |
443 | ) | |
444 | )); | |
445 | ||
446 | temp = constraint_addOr (temp, tempSub); | |
447 | constraint_free(tempSub); | |
448 | ||
449 | } | |
b7b694d6 | 450 | if (!constraint_same (temp, temp3) && !constraint_same (temp3, temp2) ) |
d9daf826 | 451 | { |
452 | /* drl added 8/28/2002*/ | |
453 | /*make sure that the information from | |
454 | a post condition like i = i + 1 is transfered | |
455 | */ | |
456 | constraint tempSub; | |
457 | ||
458 | tempSub = constraint_substitute (temp3, post1); | |
459 | ||
460 | DPRINTF(( | |
461 | message("doResolve: adding %s ", | |
462 | constraint_printOr(tempSub) | |
463 | ) | |
464 | )); | |
465 | ||
466 | ||
467 | DPRINTF(( | |
468 | message("doResolve: not adding %s ", | |
469 | constraint_printOr(temp3) | |
470 | ) | |
471 | )); | |
472 | ||
473 | temp = constraint_addOr (temp, tempSub); | |
474 | ||
475 | constraint_free(tempSub); | |
476 | } | |
b7b694d6 | 477 | *resolved = FALSE; |
478 | ||
dc7f6a51 | 479 | constraint_free(temp2); |
480 | constraint_free(temp3); | |
b7b694d6 | 481 | constraint_free(c); |
482 | ||
483 | return temp; | |
bb25bea6 | 484 | } |
b7b694d6 | 485 | constraint_free(temp2); |
486 | constraint_free(temp3); | |
bb25bea6 | 487 | } |
dc7f6a51 | 488 | else |
489 | { | |
490 | constraint_free(temp2); | |
b7b694d6 | 491 | constraint_free(temp3); |
492 | } | |
90bc41f7 | 493 | } |
b7b694d6 | 494 | else |
495 | { | |
496 | constraint_free(temp2); | |
497 | } | |
498 | ||
90bc41f7 | 499 | } |
b7b694d6 | 500 | constraint_free(temp); |
501 | } | |
502 | constraint_free(c); | |
503 | ||
86d93ed3 | 504 | /*drl bee: pbr*/ *resolved = TRUE; |
b7b694d6 | 505 | return NULL; |
90bc41f7 | 506 | } |
507 | ||
a779b61e | 508 | static /*@only@*/ constraint doResolveOr (/*@observer@*/ /*@temp@*/ constraint c, constraintList post1, /*@out@*/bool * resolved) |
90bc41f7 | 509 | { |
510 | constraint ret; | |
511 | constraint next; | |
512 | constraint curr; | |
a779b61e | 513 | |
514 | ||
515 | DPRINTF(( message("doResolveOr: constraint %s and list %s", constraint_printOr(c), constraintList_print(post1) ) )); | |
516 | ||
517 | ||
90bc41f7 | 518 | |
86d93ed3 | 519 | /*drl bee: pbr*/ *resolved = FALSE; |
a779b61e | 520 | |
521 | ||
90bc41f7 | 522 | ret = constraint_copy(c); |
a779b61e | 523 | |
524 | if (constraintList_isEmpty(post1) ) | |
525 | { | |
526 | return ret; | |
527 | } | |
528 | ||
90bc41f7 | 529 | next = ret->or; |
530 | ret->or = NULL; | |
531 | ||
532 | ret = doResolve (ret, post1, resolved); | |
bb25bea6 | 533 | |
534 | if (*resolved) | |
535 | { | |
2934b455 | 536 | if (next != NULL) |
4ab867d6 | 537 | constraint_free(next); |
a779b61e | 538 | |
920a3797 | 539 | /*we don't need to free ret when resolved is false because ret is null*/ |
540 | llassert(ret == NULL); | |
541 | ||
a779b61e | 542 | return NULL; |
bb25bea6 | 543 | } |
a779b61e | 544 | |
84c9ffbf | 545 | while (next != NULL) |
90bc41f7 | 546 | { |
547 | curr = next; | |
548 | next = curr->or; | |
549 | curr->or = NULL; | |
550 | ||
551 | curr = doResolve (curr, post1, resolved); | |
a779b61e | 552 | |
86d93ed3 | 553 | /*drl bee: pbr*/ if (*resolved) |
bb25bea6 | 554 | { |
4ab867d6 | 555 | /* curr is null so we don't try to free it*/ |
920a3797 | 556 | llassert(curr == NULL); |
4ab867d6 | 557 | |
2934b455 | 558 | if (next != NULL) |
4ab867d6 | 559 | constraint_free(next); |
920a3797 | 560 | |
bb25bea6 | 561 | constraint_free(ret); |
920a3797 | 562 | return NULL; |
bb25bea6 | 563 | } |
90bc41f7 | 564 | ret = constraint_addOr (ret, curr); |
4ab867d6 | 565 | constraint_free(curr); |
90bc41f7 | 566 | } |
90bc41f7 | 567 | return ret; |
568 | } | |
569 | ||
90bc41f7 | 570 | /* tries to resolve constraints in list pr2 using post1 */ |
28bf4b0b | 571 | /*@only@*/ constraintList constraintList_reflectChangesOr (constraintList pre2, constraintList post1) |
90bc41f7 | 572 | { |
573 | bool resolved; | |
574 | constraintList ret; | |
575 | constraint temp; | |
c3e695ff | 576 | ret = constraintList_makeNew(); |
28bf4b0b | 577 | DPRINTF((message ("constraintList_reflectChangesOr: lists %s and %s", constraintList_print(pre2), constraintList_print(post1) ))); |
90bc41f7 | 578 | |
579 | constraintList_elements (pre2, el) | |
580 | { | |
581 | temp = doResolveOr (el, post1, &resolved); | |
582 | ||
583 | if (!resolved) | |
584 | { | |
585 | ret = constraintList_add(ret, temp); | |
586 | } | |
920a3797 | 587 | else |
588 | { | |
a779b61e | 589 | /* we don't need to free temp when |
590 | resolved is false because temp is null */ | |
920a3797 | 591 | llassert(temp == NULL); |
592 | } | |
593 | ||
90bc41f7 | 594 | } end_constraintList_elements; |
595 | ||
a779b61e | 596 | DPRINTF((message ("constraintList_reflectChangesOr: returning %s", constraintList_print(ret) ) ) ); |
90bc41f7 | 597 | return ret; |
598 | } | |
a779b61e | 599 | |
bb25bea6 | 600 | static /*@only@*/ constraintList reflectChangesEnsures (/*@observer@*/ constraintList pre2, constraintList post1) |
616915dd | 601 | { |
602 | constraintList ret; | |
603 | constraint temp; | |
c3e695ff | 604 | ret = constraintList_makeNew(); |
616915dd | 605 | constraintList_elements (pre2, el) |
606 | { | |
28bf4b0b | 607 | if (!constraintList_resolve (el, post1) ) |
616915dd | 608 | { |
28bf4b0b | 609 | temp = constraint_substitute (el, post1); |
616915dd | 610 | llassert (temp != NULL); |
611 | ||
28bf4b0b | 612 | if (!constraintList_resolve (temp, post1) ) |
616915dd | 613 | ret = constraintList_add (ret, temp); |
920a3797 | 614 | else |
615 | constraint_free(temp); | |
616915dd | 616 | } |
617 | else | |
618 | { | |
bb7c2085 | 619 | DPRINTF ((message ("Resolved away %s ", constraint_print(el) ) ) ); |
616915dd | 620 | } |
621 | } end_constraintList_elements; | |
622 | ||
623 | return ret; | |
624 | } | |
625 | ||
626 | ||
bb25bea6 | 627 | static /*@only@*/ constraintList reflectChangesEnsuresFree1 (/*@only@*/ constraintList pre2, constraintList post1) |
628 | { | |
629 | constraintList ret; | |
630 | ||
631 | ret = reflectChangesEnsures (pre2, post1); | |
632 | ||
633 | constraintList_free(pre2); | |
634 | ||
635 | return ret; | |
636 | } | |
637 | ||
638 | ||
639 | static bool constraint_conflict (constraint c1, constraint c2) | |
616915dd | 640 | { |
641 | ||
642 | if (constraintExpr_similar(c1->lexpr, c2->lexpr) ) | |
643 | { | |
644 | if (c1->ar == EQ) | |
645 | if (c1->ar == c2->ar) | |
646 | { | |
bb7c2085 | 647 | DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) ); |
616915dd | 648 | return TRUE; |
649 | } | |
650 | } | |
651 | ||
b7b694d6 | 652 | /* This is a slight kludg to prevent circular constraints like |
653 | strlen(str) == maxRead(s) + strlen(str); | |
654 | */ | |
a779b61e | 655 | |
b7b694d6 | 656 | /*@i324234*/ /* clean this up */ |
a779b61e | 657 | |
658 | if (c1->ar == EQ) | |
659 | if (c1->ar == c2->ar) | |
660 | { | |
661 | if (constraintExpr_search (c1->lexpr, c2->expr) ) | |
662 | if (constraintExpr_isTerm(c1->lexpr) ) | |
663 | { | |
664 | constraintTerm term; | |
665 | ||
666 | term = constraintExpr_getTerm(c1->lexpr); | |
667 | ||
668 | if (constraintTerm_isExprNode(term) ) | |
669 | { | |
bb7c2085 | 670 | DPRINTF ((message ("%s conflicts with %s ", constraint_print (c1), constraint_print(c2) ) ) ); |
a779b61e | 671 | return TRUE; |
672 | } | |
673 | } | |
674 | } | |
675 | ||
676 | if (constraint_tooDeep(c1) || constraint_tooDeep(c2) ) | |
677 | { | |
bb7c2085 | 678 | DPRINTF ((message ("%s conflicts with %s (constraint is too deep", constraint_print (c1), constraint_print(c2) ) ) ); |
a779b61e | 679 | return TRUE; |
680 | } | |
681 | ||
bb7c2085 | 682 | DPRINTF ((message ("%s doesn't conflict with %s ", constraint_print (c1), constraint_print(c2) ) ) ); |
616915dd | 683 | |
684 | return FALSE; | |
685 | ||
686 | } | |
687 | ||
28bf4b0b | 688 | static void constraint_fixConflict (/*@temp@*/ constraint good, /*@temp@*/ /*@observer@*/ constraint conflicting) /*@modifies good@*/ |
616915dd | 689 | { |
616915dd | 690 | if (conflicting->ar ==EQ ) |
691 | { | |
692 | good->expr = constraintExpr_searchandreplace (good->expr, conflicting->lexpr, conflicting->expr); | |
d46ce6a4 | 693 | good = constraint_simplify (good); |
616915dd | 694 | } |
695 | ||
696 | ||
697 | } | |
698 | ||
bb25bea6 | 699 | static bool conflict (constraint c, constraintList list) |
616915dd | 700 | { |
90bc41f7 | 701 | |
702 | constraintList_elements (list, el) | |
616915dd | 703 | { |
704 | if ( constraint_conflict(el, c) ) | |
705 | { | |
706 | constraint_fixConflict (el, c); | |
707 | return TRUE; | |
708 | } | |
709 | } end_constraintList_elements; | |
710 | ||
711 | return FALSE; | |
616915dd | 712 | |
713 | } | |
714 | ||
b7b694d6 | 715 | /* |
716 | check if constraint in list1 conflicts with constraints in List2. If so we | |
717 | remove form list1 and change list2. | |
718 | */ | |
719 | ||
84c9ffbf | 720 | constraintList constraintList_fixConflicts (constraintList list1, constraintList list2) |
616915dd | 721 | { |
722 | constraintList ret; | |
c3e695ff | 723 | ret = constraintList_makeNew(); |
84c9ffbf | 724 | llassert(constraintList_isDefined(list1) ); |
616915dd | 725 | constraintList_elements (list1, el) |
726 | { | |
727 | if (! conflict (el, list2) ) | |
728 | { | |
d46ce6a4 | 729 | constraint temp; |
730 | temp = constraint_copy(el); | |
731 | ret = constraintList_add (ret, temp); | |
616915dd | 732 | } |
733 | } end_constraintList_elements; | |
734 | ||
735 | return ret; | |
616915dd | 736 | } |
737 | ||
90bc41f7 | 738 | /*returns true if constraint post satifies cosntriant pre */ |
bb25bea6 | 739 | static bool satifies (constraint pre, constraint post) |
616915dd | 740 | { |
741 | if (constraint_isAlwaysTrue (pre) ) | |
742 | return TRUE; | |
743 | ||
744 | if (!constraintExpr_similar (pre->lexpr, post->lexpr) ) | |
745 | { | |
746 | return FALSE; | |
747 | } | |
28bf4b0b | 748 | if (constraintExpr_isUndefined(post->expr)) |
616915dd | 749 | { |
750 | llassert(FALSE); | |
751 | return FALSE; | |
752 | } | |
753 | ||
754 | return rangeCheck (pre->ar, pre->expr, post->ar, post->expr); | |
755 | } | |
756 | ||
bb25bea6 | 757 | |
28bf4b0b | 758 | bool constraintList_resolve (/*@temp@*/ /*@observer@*/ constraint c, /*@temp@*/ /*@observer@*/ constraintList p) |
bb25bea6 | 759 | { |
760 | constraintList_elements (p, el) | |
761 | { | |
762 | if ( satifies (c, el) ) | |
763 | { | |
bb7c2085 | 764 | DPRINTF ((message ("\n%s Satifies %s\n ", constraint_print(el), constraint_print(c) ) ) ); |
bb25bea6 | 765 | return TRUE; |
766 | } | |
bb7c2085 | 767 | DPRINTF ((message ("\n%s does not satify %s\n ", constraint_print(el), constraint_print(c) ) ) ); |
bb25bea6 | 768 | } |
769 | end_constraintList_elements; | |
bb7c2085 | 770 | DPRINTF ((message ("no constraints satify %s", constraint_print(c) ) )); |
bb25bea6 | 771 | return FALSE; |
772 | } | |
773 | ||
774 | static bool arithType_canResolve (arithType ar1, arithType ar2) | |
616915dd | 775 | { |
776 | switch (ar1) | |
777 | { | |
778 | case GTE: | |
779 | case GT: | |
bb7c2085 | 780 | if ((ar2 == GT) || (ar2 == GTE) || (ar2 == EQ) ) |
616915dd | 781 | { |
782 | return TRUE; | |
783 | } | |
784 | break; | |
785 | ||
786 | case EQ: | |
787 | if (ar2 == EQ) | |
788 | return TRUE; | |
789 | break; | |
790 | ||
791 | case LT: | |
792 | case LTE: | |
bb7c2085 | 793 | if ((ar2 == LT) || (ar2 == LTE) || (ar2 == EQ) ) |
616915dd | 794 | return TRUE; |
84c9ffbf | 795 | break; |
616915dd | 796 | default: |
797 | return FALSE; | |
798 | } | |
799 | return FALSE; | |
800 | } | |
801 | ||
e5f31c00 | 802 | /*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/ |
803 | static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2) | |
804 | { | |
805 | constraintTerm ct; | |
806 | exprNode e, t; | |
807 | sRef s1, s2; | |
808 | /*@access constraintExpr@*/ | |
809 | ||
bb7c2085 | 810 | if ((expr2->kind != term) && (buf1->kind != term) ) |
e5f31c00 | 811 | return FALSE; |
812 | ||
813 | ||
814 | ct = constraintExprData_termGetTerm(expr2->data); | |
815 | ||
816 | if (!constraintTerm_isExprNode(ct) ) | |
817 | return FALSE; | |
818 | ||
819 | e = constraintTerm_getExprNode(ct); | |
820 | ||
821 | if (e->kind != XPR_SIZEOF) | |
822 | return FALSE; | |
823 | ||
824 | t = exprData_getSingle (e->edata); | |
825 | s1 = exprNode_getSref (t); | |
826 | ||
827 | s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) ); | |
828 | ||
829 | /*@i223@*/ /*this may be the wronge thing to test for */ | |
830 | if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) ) | |
831 | { | |
e5508969 | 832 | /*@i22*/ /* get rid of this test of now */ |
833 | /* if (ctype_isFixedArray (sRef_getType (s2) ) ) */ | |
e5f31c00 | 834 | return TRUE; |
835 | } | |
836 | return FALSE; | |
837 | } | |
838 | ||
839 | /* look for the special case of | |
840 | maxSet(buf) >= sizeof(buf) - 1 | |
841 | */ | |
842 | ||
843 | /*@i223@*/ /*need to add some type checking */ | |
844 | static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c) | |
845 | { | |
846 | constraintExpr l, r, buf1, buf2, con; | |
847 | ||
848 | DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) ) | |
849 | )); | |
850 | ||
851 | l = c->lexpr; | |
852 | r = c->expr; | |
853 | ||
bb7c2085 | 854 | if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) ) |
e5f31c00 | 855 | return FALSE; |
856 | ||
857 | /*check if the constraintExpr is MaxSet(buf) */ | |
858 | if (l->kind == unaryExpr) | |
859 | { | |
860 | if (constraintExprData_unaryExprGetOp(l->data) == MAXSET) | |
861 | { | |
862 | buf1 = constraintExprData_unaryExprGetExpr(l->data); | |
863 | } | |
864 | else | |
865 | return FALSE; | |
866 | } | |
867 | else | |
868 | return FALSE; | |
869 | ||
870 | ||
871 | if (r->kind != binaryexpr) | |
872 | return FALSE; | |
873 | ||
874 | buf2 = constraintExprData_binaryExprGetExpr1(r->data); | |
875 | con = constraintExprData_binaryExprGetExpr2(r->data); | |
876 | ||
877 | if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS) | |
878 | { | |
879 | if (constraintExpr_canGetValue(con) ) | |
880 | { | |
881 | long i; | |
882 | ||
883 | i = constraintExpr_getValue(con); | |
884 | if (i != 1) | |
885 | { | |
886 | return FALSE; | |
887 | } | |
888 | } | |
889 | else | |
890 | return FALSE; | |
891 | } | |
892 | ||
893 | if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS) | |
894 | { | |
895 | if (constraintExpr_canGetValue(con) ) | |
896 | { | |
897 | long i; | |
898 | ||
899 | i = constraintExpr_getValue(con); | |
900 | if (i != -1) | |
901 | { | |
902 | return FALSE; | |
903 | } | |
904 | } | |
905 | else | |
906 | return FALSE; | |
907 | } | |
908 | ||
909 | if (sizeofBufComp(buf1, buf2)) | |
910 | { | |
911 | return TRUE; | |
912 | } | |
913 | else | |
914 | { | |
915 | return FALSE; | |
916 | } | |
917 | ||
918 | ||
919 | } | |
920 | /*@noaccess constraintExpr@*/ | |
921 | ||
90bc41f7 | 922 | /* We look for constraint which are tautologies */ |
923 | ||
28bf4b0b | 924 | bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c) |
616915dd | 925 | { |
926 | constraintExpr l, r; | |
15b3d2b2 | 927 | bool rHasConstant; |
928 | int rConstant; | |
9280addf | 929 | |
616915dd | 930 | l = c->lexpr; |
931 | r = c->expr; | |
90bc41f7 | 932 | |
933 | DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) )); | |
e5f31c00 | 934 | |
935 | if (sizeOfMaxSet(c) ) | |
936 | return TRUE; | |
937 | ||
616915dd | 938 | if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) ) |
939 | { | |
940 | int cmp; | |
941 | cmp = constraintExpr_compare (l, r); | |
942 | switch (c->ar) | |
943 | { | |
944 | case EQ: | |
945 | return (cmp == 0); | |
946 | case GT: | |
947 | return (cmp > 0); | |
948 | case GTE: | |
949 | return (cmp >= 0); | |
950 | case LTE: | |
951 | return (cmp <= 0); | |
952 | case LT: | |
953 | return (cmp < 0); | |
954 | ||
955 | default: | |
9280addf | 956 | BADEXIT; |
84c9ffbf | 957 | /*@notreached@*/ |
9280addf | 958 | break; |
959 | } | |
960 | } | |
961 | ||
962 | if (constraintExpr_similar (l,r) ) | |
963 | { | |
964 | switch (c->ar) | |
965 | { | |
966 | case EQ: | |
967 | case GTE: | |
968 | case LTE: | |
969 | return TRUE; | |
970 | ||
971 | case GT: | |
972 | case LT: | |
973 | break; | |
974 | default: | |
975 | BADEXIT; | |
84c9ffbf | 976 | /*@notreached@*/ |
616915dd | 977 | break; |
978 | } | |
979 | } | |
9280addf | 980 | |
981 | l = constraintExpr_copy (c->lexpr); | |
982 | r = constraintExpr_copy (c->expr); | |
983 | ||
9280addf | 984 | r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant); |
985 | ||
90bc41f7 | 986 | if (constraintExpr_similar (l,r) && (rHasConstant ) ) |
9280addf | 987 | { |
988 | DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) )); | |
90bc41f7 | 989 | DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) )); |
bb25bea6 | 990 | |
991 | constraintExpr_free(l); | |
992 | constraintExpr_free(r); | |
993 | ||
90bc41f7 | 994 | switch (c->ar) |
9280addf | 995 | { |
90bc41f7 | 996 | case EQ: |
997 | return (rConstant == 0); | |
998 | case LT: | |
999 | return (rConstant > 0); | |
1000 | case LTE: | |
1001 | return (rConstant >= 0); | |
1002 | case GTE: | |
1003 | return (rConstant <= 0); | |
1004 | case GT: | |
1005 | return (rConstant < 0); | |
1006 | ||
1007 | default: | |
1008 | BADEXIT; | |
84c9ffbf | 1009 | /*@notreached@*/ |
90bc41f7 | 1010 | break; |
9280addf | 1011 | } |
9280addf | 1012 | } |
90bc41f7 | 1013 | else |
1014 | { | |
bb25bea6 | 1015 | constraintExpr_free(l); |
1016 | constraintExpr_free(r); | |
90bc41f7 | 1017 | DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) )); |
1018 | return FALSE; | |
1019 | } | |
9280addf | 1020 | |
1021 | BADEXIT; | |
616915dd | 1022 | } |
1023 | ||
bb25bea6 | 1024 | static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2) |
616915dd | 1025 | |
1026 | { | |
bb25bea6 | 1027 | DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) )); |
616915dd | 1028 | |
1029 | if (! arithType_canResolve (ar1, ar2) ) | |
1030 | return FALSE; | |
1031 | ||
1032 | switch (ar1) | |
1033 | { | |
1034 | case GTE: | |
2681ee39 | 1035 | if (constraintExpr_similar (expr1, expr2) ) |
1036 | return TRUE; | |
1037 | /*@fallthrough@*/ | |
1038 | case GT: | |
616915dd | 1039 | if (! (constraintExpr_canGetValue (expr1) && |
2681ee39 | 1040 | constraintExpr_canGetValue (expr2) ) ) |
1041 | { | |
1042 | constraintExpr e1, e2; | |
1043 | bool p1, p2; | |
1044 | int const1, const2; | |
1045 | ||
1046 | e1 = constraintExpr_copy(expr1); | |
1047 | e2 = constraintExpr_copy(expr2); | |
1048 | ||
1049 | e1 = constraintExpr_propagateConstants (e1, &p1, &const1); | |
1050 | ||
1051 | e2 = constraintExpr_propagateConstants (e2, &p2, &const2); | |
1052 | ||
1053 | if (p1 || p2) | |
1054 | { | |
1055 | if (!p1) | |
1056 | const1 = 0; | |
1057 | ||
1058 | if (!p2) | |
1059 | const2 = 0; | |
1060 | ||
1061 | if (const1 <= const2) | |
1062 | if (constraintExpr_similar (e1, e2) ) | |
1063 | { | |
1064 | constraintExpr_free(e1); | |
1065 | constraintExpr_free(e2); | |
1066 | return TRUE; | |
1067 | } | |
1068 | } | |
bb7c2085 | 1069 | DPRINTF(("Can't Get value")); |
2681ee39 | 1070 | |
1071 | constraintExpr_free(e1); | |
1072 | constraintExpr_free(e2); | |
1073 | return FALSE; | |
1074 | } | |
1075 | ||
1076 | if (constraintExpr_compare (expr2, expr1) >= 0) | |
1077 | return TRUE; | |
616915dd | 1078 | |
1079 | return FALSE; | |
2681ee39 | 1080 | case EQ: |
1081 | if (constraintExpr_similar (expr1, expr2) ) | |
1082 | return TRUE; | |
1083 | ||
1084 | return FALSE; | |
1085 | case LTE: | |
1086 | if (constraintExpr_similar (expr1, expr2) ) | |
1087 | return TRUE; | |
1088 | /*@fallthrough@*/ | |
1089 | case LT: | |
1090 | if (! (constraintExpr_canGetValue (expr1) && | |
1091 | constraintExpr_canGetValue (expr2) ) ) | |
1092 | { | |
1093 | constraintExpr e1, e2; | |
1094 | bool p1, p2; | |
1095 | int const1, const2; | |
1096 | ||
1097 | e1 = constraintExpr_copy(expr1); | |
1098 | e2 = constraintExpr_copy(expr2); | |
1099 | ||
1100 | e1 = constraintExpr_propagateConstants (e1, &p1, &const1); | |
1101 | ||
1102 | e2 = constraintExpr_propagateConstants (e2, &p2, &const2); | |
1103 | ||
1104 | if (p1 || p2) | |
1105 | { | |
1106 | if (!p1) | |
1107 | const1 = 0; | |
1108 | ||
1109 | if (!p2) | |
1110 | const2 = 0; | |
1111 | ||
1112 | if (const1 >= const2) | |
1113 | if (constraintExpr_similar (e1, e2) ) | |
1114 | { | |
1115 | constraintExpr_free(e1); | |
1116 | constraintExpr_free(e2); | |
1117 | return TRUE; | |
1118 | } | |
1119 | } | |
1120 | constraintExpr_free(e1); | |
1121 | constraintExpr_free(e2); | |
1122 | ||
bb7c2085 | 1123 | DPRINTF(("Can't Get value")); |
2681ee39 | 1124 | return FALSE; |
1125 | } | |
1126 | ||
1127 | if (constraintExpr_compare (expr2, expr1) <= 0) | |
1128 | return TRUE; | |
1129 | ||
1130 | return FALSE; | |
1131 | ||
1132 | default: | |
1133 | llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) ); | |
1134 | } | |
616915dd | 1135 | BADEXIT; |
616915dd | 1136 | } |
1137 | ||
28bf4b0b | 1138 | static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr) |
616915dd | 1139 | { |
1140 | DPRINTF (("Doing replace for lexpr") ); | |
28bf4b0b | 1141 | c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr); |
616915dd | 1142 | DPRINTF (("Doing replace for expr") ); |
28bf4b0b | 1143 | c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr); |
616915dd | 1144 | return c; |
1145 | } | |
1146 | ||
84c9ffbf | 1147 | bool constraint_search (constraint c, constraintExpr old) /*@*/ |
616915dd | 1148 | { |
1149 | bool ret; | |
1150 | ret = FALSE; | |
1151 | ||
1152 | ret = constraintExpr_search (c->lexpr, old); | |
1153 | ret = ret || constraintExpr_search (c->expr, old); | |
1154 | return ret; | |
1155 | } | |
1156 | ||
b7b694d6 | 1157 | /* adjust file locs and stuff */ |
bb25bea6 | 1158 | static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old) |
616915dd | 1159 | { |
1160 | fileloc loc1, loc2, loc3; | |
1161 | ||
bb7c2085 | 1162 | DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute), |
616915dd | 1163 | constraint_print(old)) |
1164 | )); | |
1165 | ||
1166 | loc1 = constraint_getFileloc (old); | |
616915dd | 1167 | loc2 = constraintExpr_getFileloc (substitute->lexpr); |
616915dd | 1168 | loc3 = constraintExpr_getFileloc (substitute->expr); |
616915dd | 1169 | |
b7b694d6 | 1170 | /* special case of an equality that "contains itself" */ |
616915dd | 1171 | if (constraintExpr_search (substitute->expr, substitute->lexpr) ) |
1172 | if (fileloc_closer (loc1, loc3, loc2)) | |
1173 | { | |
1174 | constraintExpr temp; | |
bb7c2085 | 1175 | DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) ) |
616915dd | 1176 | )); |
1177 | temp = substitute->lexpr; | |
1178 | substitute->lexpr = substitute->expr; | |
1179 | substitute->expr = temp; | |
1180 | substitute = constraint_simplify(substitute); | |
1181 | } | |
1182 | ||
bb25bea6 | 1183 | fileloc_free (loc1); |
1184 | fileloc_free (loc2); | |
1185 | fileloc_free (loc3); | |
1186 | ||
616915dd | 1187 | return substitute; |
1188 | ||
1189 | } | |
1190 | ||
90bc41f7 | 1191 | /* If function preforms substitutes based on inequality |
1192 | ||
1193 | It uses the rule x >= y && b < y ===> x >= b + 1 | |
1194 | ||
1195 | Warning this is sound but throws out information | |
1196 | */ | |
dc7f6a51 | 1197 | |
bb25bea6 | 1198 | constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p) |
616915dd | 1199 | { |
1200 | if (c->ar != GTE) | |
1201 | return c; | |
1202 | ||
1203 | constraintList_elements (p, el) | |
1204 | { | |
bb7c2085 | 1205 | if ((el->ar == LT ) ) |
b7b694d6 | 1206 | /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */ |
616915dd | 1207 | { |
90bc41f7 | 1208 | constraintExpr temp2; |
1209 | ||
bb25bea6 | 1210 | /*@i22*/ |
1211 | ||
90bc41f7 | 1212 | if (constraintExpr_same (el->expr, c->expr) ) |
616915dd | 1213 | { |
dc7f6a51 | 1214 | DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q", |
616915dd | 1215 | constraintExpr_print (c->expr), |
1216 | constraint_print (c), | |
1217 | constraintExpr_print (el->expr) ) | |
1218 | )); | |
90bc41f7 | 1219 | temp2 = constraintExpr_copy (el->lexpr); |
bb25bea6 | 1220 | constraintExpr_free(c->expr); |
90bc41f7 | 1221 | c->expr = constraintExpr_makeIncConstraintExpr (temp2); |
dc7f6a51 | 1222 | |
616915dd | 1223 | } |
1224 | ||
1225 | } | |
1226 | } | |
1227 | end_constraintList_elements; | |
1228 | ||
1229 | c = constraint_simplify(c); | |
1230 | return c; | |
1231 | } | |
1232 | ||
dc7f6a51 | 1233 | |
1234 | /* drl7x 7/26/001 | |
1235 | ||
1236 | THis function is like inequalitySubstitute but it adds the rule | |
1237 | added the rules x >= y && y <= b ===> x >= b | |
1238 | x >= y && y < b ===> x >= b + 1 | |
1239 | ||
1240 | This is sound but sonce it throws out additional information it should only one used | |
1241 | if we're oring constraints. | |
1242 | */ | |
1243 | ||
1244 | static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p) | |
1245 | { | |
1246 | DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) )); | |
1247 | ||
1248 | if (c->ar != GTE) | |
1249 | return c; | |
1250 | ||
1251 | DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q", | |
1252 | constraint_print(c), constraintList_print(p) ) )); | |
1253 | constraintList_elements (p, el) | |
1254 | { | |
1255 | DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) )); | |
1256 | ||
bb7c2085 | 1257 | if ((el->ar == LT ) || (el->ar == LTE ) ) |
b7b694d6 | 1258 | /* if (!constraint_conflict (c, el) ) */ /*@i523@*/ |
dc7f6a51 | 1259 | { |
dc7f6a51 | 1260 | constraintExpr temp2; |
1261 | ||
1262 | /*@i22*/ | |
1263 | ||
dc7f6a51 | 1264 | if (constraintExpr_same (el->lexpr, c->expr) ) |
1265 | { | |
1266 | DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s", | |
1267 | constraintExpr_print (c->expr), | |
1268 | constraint_print (c), | |
1269 | constraintExpr_print (el->expr) ) | |
1270 | )); | |
1271 | temp2 = constraintExpr_copy (el->expr); | |
1272 | constraintExpr_free(c->expr); | |
bb7c2085 | 1273 | if ((el->ar == LTE ) ) |
dc7f6a51 | 1274 | { |
1275 | c->expr = temp2; | |
1276 | } | |
1277 | else | |
1278 | { | |
1279 | c->expr = constraintExpr_makeIncConstraintExpr (temp2); | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | } | |
1284 | } | |
1285 | end_constraintList_elements; | |
1286 | ||
1287 | c = constraint_simplify(c); | |
1288 | return c; | |
1289 | } | |
1290 | ||
1291 | ||
90bc41f7 | 1292 | /* This function performs substitutions based on the rule: |
1293 | for a constraint of the form expr1 >= expr2; a < b => | |
1294 | a = b -1 for all a in expr1. This will work in most cases. | |
1295 | ||
1296 | Like inequalitySubstitute we're throwing away some information | |
1297 | */ | |
1298 | ||
bb25bea6 | 1299 | static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p) |
90bc41f7 | 1300 | { |
1301 | DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) )); | |
1302 | ||
1303 | if (c->ar != GTE) | |
1304 | return c; | |
1305 | ||
1306 | constraintList_elements (p, el) | |
1307 | { | |
1308 | DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) )); | |
bb7c2085 | 1309 | if (( el->ar == LTE) || (el->ar == LT) ) |
b7b694d6 | 1310 | /* if (!constraint_conflict (c, el) ) */ /*@i532@*/ |
90bc41f7 | 1311 | { |
90bc41f7 | 1312 | constraintExpr temp2; |
b7b694d6 | 1313 | |
90bc41f7 | 1314 | temp2 = constraintExpr_copy (el->expr); |
1315 | ||
1316 | if (el->ar == LT) | |
1317 | temp2 = constraintExpr_makeDecConstraintExpr (temp2); | |
1318 | ||
1319 | DPRINTF((message ("Replacing %s in %s with %s", | |
1320 | constraintExpr_print (el->lexpr), | |
1321 | constraintExpr_print (c->lexpr), | |
1322 | constraintExpr_print (temp2) ) )); | |
1323 | ||
1324 | c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2); | |
bb25bea6 | 1325 | constraintExpr_free(temp2); |
90bc41f7 | 1326 | } |
1327 | } | |
1328 | end_constraintList_elements; | |
1329 | ||
1330 | c = constraint_simplify(c); | |
1331 | return c; | |
1332 | } | |
1333 | ||
28bf4b0b | 1334 | /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p) |
616915dd | 1335 | { |
90bc41f7 | 1336 | constraint ret; |
1337 | ||
1338 | ret = constraint_copy(c); | |
616915dd | 1339 | constraintList_elements (p, el) |
1340 | { | |
1341 | if ( el->ar == EQ) | |
90bc41f7 | 1342 | if (!constraint_conflict (ret, el) ) |
616915dd | 1343 | |
1344 | { | |
1345 | constraint temp; | |
bb25bea6 | 1346 | |
616915dd | 1347 | temp = constraint_copy(el); |
1348 | ||
90bc41f7 | 1349 | temp = constraint_adjust(temp, ret); |
616915dd | 1350 | |
d9daf826 | 1351 | DPRINTF((message ("constraint_substitute :: Substituting in %s using %s", |
1352 | constraint_print (ret), constraint_print (temp) | |
616915dd | 1353 | ) ) ); |
1354 | ||
1355 | ||
90bc41f7 | 1356 | ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr); |
d9daf826 | 1357 | DPRINTF(( message (" constraint_substitute :: The new constraint is %s", constraint_print (ret) ) )); |
bb25bea6 | 1358 | constraint_free(temp); |
616915dd | 1359 | } |
1360 | } | |
1361 | end_constraintList_elements; | |
1362 | ||
90bc41f7 | 1363 | ret = constraint_simplify(ret); |
d9daf826 | 1364 | |
1365 | DPRINTF(( message (" constraint_substitute :: The final new constraint is %s", constraint_print (ret) ) )); | |
1366 | ||
90bc41f7 | 1367 | return ret; |
616915dd | 1368 | } |
1369 | ||
bb25bea6 | 1370 | |
1371 | /*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList) | |
1372 | { | |
1373 | constraintList ret; | |
1374 | ||
1375 | ret = constraintList_substitute (target, subList); | |
1376 | ||
1377 | constraintList_free(target); | |
1378 | ||
1379 | return ret; | |
1380 | } | |
1381 | ||
c3e695ff | 1382 | /* we try to do substitutions on each constraint in target using the constraint in sublist*/ |
1383 | ||
bb25bea6 | 1384 | /*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList) |
c3e695ff | 1385 | { |
1386 | ||
1387 | constraintList ret; | |
1388 | ||
1389 | ret = constraintList_makeNew(); | |
1390 | ||
1391 | constraintList_elements(target, el) | |
1392 | { | |
d46ce6a4 | 1393 | constraint temp; |
b7b694d6 | 1394 | /* drl possible problem : warning make sure that a side effect is not expected */ |
bb25bea6 | 1395 | |
28bf4b0b | 1396 | temp = constraint_substitute(el, subList); |
d46ce6a4 | 1397 | ret = constraintList_add (ret, temp); |
c3e695ff | 1398 | } |
1399 | end_constraintList_elements; | |
84c9ffbf | 1400 | |
bb25bea6 | 1401 | return ret; |
c3e695ff | 1402 | } |
616915dd | 1403 | |
28bf4b0b | 1404 | static constraint constraint_solve (/*@returned@*/ constraint c) |
616915dd | 1405 | { |
bb7c2085 | 1406 | DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) ); |
616915dd | 1407 | c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr); |
bb7c2085 | 1408 | DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) ); |
616915dd | 1409 | |
1410 | return c; | |
1411 | } | |
1412 | ||
1413 | static arithType flipAr (arithType ar) | |
1414 | { | |
1415 | switch (ar) | |
1416 | { | |
1417 | case LT: | |
1418 | return GT; | |
1419 | case LTE: | |
1420 | return GTE; | |
1421 | case EQ: | |
1422 | return EQ; | |
1423 | case GT: | |
1424 | return LT; | |
1425 | case GTE: | |
1426 | return LTE; | |
1427 | default: | |
84c9ffbf | 1428 | llcontbug (message("unexpected value: case not handled")); |
616915dd | 1429 | } |
1430 | BADEXIT; | |
1431 | } | |
1432 | ||
d46ce6a4 | 1433 | static constraint constraint_swapLeftRight (/*@returned@*/ constraint c) |
616915dd | 1434 | { |
1435 | constraintExpr temp; | |
1436 | c->ar = flipAr (c->ar); | |
1437 | temp = c->lexpr; | |
1438 | c->lexpr = c->expr; | |
1439 | c->expr = temp; | |
1440 | DPRINTF(("Swaped left and right sides of constraint")); | |
1441 | return c; | |
1442 | } | |
1443 | ||
c3e695ff | 1444 | |
1445 | ||
d46ce6a4 | 1446 | constraint constraint_simplify ( /*@returned@*/ constraint c) |
616915dd | 1447 | { |
a779b61e | 1448 | |
1449 | DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) )); | |
1450 | ||
1451 | if (constraint_tooDeep(c)) | |
1452 | { | |
1453 | DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) )); | |
1454 | return c; | |
1455 | ||
1456 | } | |
1457 | ||
616915dd | 1458 | c->lexpr = constraintExpr_simplify (c->lexpr); |
1459 | c->expr = constraintExpr_simplify (c->expr); | |
616915dd | 1460 | |
bb25bea6 | 1461 | if (constraintExpr_isBinaryExpr (c->lexpr) ) |
c3e695ff | 1462 | { |
1463 | c = constraint_solve (c); | |
1464 | ||
1465 | c->lexpr = constraintExpr_simplify (c->lexpr); | |
1466 | c->expr = constraintExpr_simplify (c->expr); | |
1467 | } | |
1468 | ||
616915dd | 1469 | if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) ) |
1470 | { | |
1471 | c = constraint_swapLeftRight(c); | |
1472 | /*I don't think this will be an infinate loop*/ | |
84c9ffbf | 1473 | c = constraint_simplify(c); |
616915dd | 1474 | } |
a779b61e | 1475 | |
1476 | DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) )); | |
1477 | ||
616915dd | 1478 | return c; |
1479 | } | |
1480 | ||
1481 | ||
1482 | ||
1483 | ||
1484 | /* returns true if fileloc for term1 is closer to file for term2 than term3*/ | |
1485 | ||
1486 | bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3) | |
1487 | { | |
c3e695ff | 1488 | |
1489 | if (!fileloc_isDefined (loc1) ) | |
1490 | return FALSE; | |
1491 | ||
1492 | if (!fileloc_isDefined (loc2) ) | |
1493 | return FALSE; | |
1494 | ||
1495 | if (!fileloc_isDefined (loc3) ) | |
1496 | return TRUE; | |
1497 | ||
1498 | ||
1499 | ||
1500 | ||
1501 | if (fileloc_equal (loc2, loc3) ) | |
1502 | return FALSE; | |
1503 | ||
1504 | if (fileloc_equal (loc1, loc2) ) | |
1505 | return TRUE; | |
1506 | ||
1507 | if (fileloc_equal (loc1, loc3) ) | |
1508 | return FALSE; | |
1509 | ||
616915dd | 1510 | if ( fileloc_lessthan (loc1, loc2) ) |
1511 | { | |
1512 | if (fileloc_lessthan (loc2, loc3) ) | |
1513 | { | |
1514 | llassert (fileloc_lessthan (loc1, loc3) ); | |
1515 | return TRUE; | |
1516 | } | |
1517 | else | |
1518 | { | |
1519 | return FALSE; | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | if ( !fileloc_lessthan (loc1, loc2) ) | |
1524 | { | |
1525 | if (!fileloc_lessthan (loc2, loc3) ) | |
1526 | { | |
1527 | llassert (!fileloc_lessthan (loc1, loc3) ); | |
1528 | return TRUE; | |
1529 | } | |
1530 | else | |
1531 | { | |
1532 | return FALSE; | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | llassert(FALSE); | |
1537 | return FALSE; | |
1538 | } | |
1539 | ||
1540 |