]>
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 | |
495af944 | 42 | /*@access constraint@*/ |
393e573f | 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! */ | |
495af944 | 47 | |
393e573f | 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 | ||
495af944 | 746 | |
747 | ||
e5f31c00 | 748 | /*checks for the case expr2 == sizeof buf1 and buf1 is a fixed array*/ |
749 | static bool sizeofBufComp(constraintExpr buf1, constraintExpr expr2) | |
750 | { | |
751 | constraintTerm ct; | |
752 | exprNode e, t; | |
753 | sRef s1, s2; | |
754 | /*@access constraintExpr@*/ | |
755 | ||
bb7c2085 | 756 | if ((expr2->kind != term) && (buf1->kind != term) ) |
e5f31c00 | 757 | return FALSE; |
758 | ||
759 | ||
760 | ct = constraintExprData_termGetTerm(expr2->data); | |
761 | ||
762 | if (!constraintTerm_isExprNode(ct) ) | |
763 | return FALSE; | |
764 | ||
765 | e = constraintTerm_getExprNode(ct); | |
766 | ||
495af944 | 767 | /*@access exprNode@*/ /*!!! NO! Don't do this so recklessly - design your code more carefully so you don't need to! */ |
768 | ||
e5f31c00 | 769 | if (e->kind != XPR_SIZEOF) |
770 | return FALSE; | |
771 | ||
772 | t = exprData_getSingle (e->edata); | |
495af944 | 773 | |
774 | /*@noaccess exprNode@*/ | |
775 | ||
e5f31c00 | 776 | s1 = exprNode_getSref (t); |
777 | ||
778 | s2 = constraintTerm_getsRef(constraintExprData_termGetTerm(buf1->data) ); | |
779 | ||
780 | /*@i223@*/ /*this may be the wronge thing to test for */ | |
781 | if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) ) | |
782 | { | |
e5508969 | 783 | /*@i22*/ /* get rid of this test of now */ |
784 | /* if (ctype_isFixedArray (sRef_getType (s2) ) ) */ | |
e5f31c00 | 785 | return TRUE; |
786 | } | |
787 | return FALSE; | |
788 | } | |
789 | ||
790 | /* look for the special case of | |
791 | maxSet(buf) >= sizeof(buf) - 1 | |
792 | */ | |
793 | ||
794 | /*@i223@*/ /*need to add some type checking */ | |
795 | static bool sizeOfMaxSet( /*@observer@*/ /*@temp@*/ constraint c) | |
796 | { | |
797 | constraintExpr l, r, buf1, buf2, con; | |
798 | ||
799 | DPRINTF(( message("sizeOfMaxSet: checking %s ", constraint_print(c) ) | |
800 | )); | |
801 | ||
802 | l = c->lexpr; | |
803 | r = c->expr; | |
804 | ||
bb7c2085 | 805 | if (!((c->ar == EQ) || (c->ar == GTE) || (c->ar == LTE) ) ) |
e5f31c00 | 806 | return FALSE; |
807 | ||
808 | /*check if the constraintExpr is MaxSet(buf) */ | |
809 | if (l->kind == unaryExpr) | |
810 | { | |
811 | if (constraintExprData_unaryExprGetOp(l->data) == MAXSET) | |
812 | { | |
813 | buf1 = constraintExprData_unaryExprGetExpr(l->data); | |
814 | } | |
815 | else | |
816 | return FALSE; | |
817 | } | |
818 | else | |
819 | return FALSE; | |
820 | ||
821 | ||
822 | if (r->kind != binaryexpr) | |
823 | return FALSE; | |
824 | ||
825 | buf2 = constraintExprData_binaryExprGetExpr1(r->data); | |
826 | con = constraintExprData_binaryExprGetExpr2(r->data); | |
827 | ||
828 | if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_MINUS) | |
829 | { | |
830 | if (constraintExpr_canGetValue(con) ) | |
831 | { | |
832 | long i; | |
833 | ||
834 | i = constraintExpr_getValue(con); | |
835 | if (i != 1) | |
836 | { | |
837 | return FALSE; | |
838 | } | |
839 | } | |
840 | else | |
841 | return FALSE; | |
842 | } | |
843 | ||
844 | if (constraintExprData_binaryExprGetOp(r->data) == BINARYOP_PLUS) | |
845 | { | |
846 | if (constraintExpr_canGetValue(con) ) | |
847 | { | |
848 | long i; | |
849 | ||
850 | i = constraintExpr_getValue(con); | |
851 | if (i != -1) | |
852 | { | |
853 | return FALSE; | |
854 | } | |
855 | } | |
856 | else | |
857 | return FALSE; | |
858 | } | |
859 | ||
860 | if (sizeofBufComp(buf1, buf2)) | |
861 | { | |
862 | return TRUE; | |
863 | } | |
864 | else | |
865 | { | |
866 | return FALSE; | |
867 | } | |
868 | ||
869 | ||
870 | } | |
871 | /*@noaccess constraintExpr@*/ | |
872 | ||
90bc41f7 | 873 | /* We look for constraint which are tautologies */ |
874 | ||
28bf4b0b | 875 | bool constraint_isAlwaysTrue (/*@observer@*/ /*@temp@*/ constraint c) |
616915dd | 876 | { |
877 | constraintExpr l, r; | |
15b3d2b2 | 878 | bool rHasConstant; |
879 | int rConstant; | |
9280addf | 880 | |
616915dd | 881 | l = c->lexpr; |
882 | r = c->expr; | |
90bc41f7 | 883 | |
884 | DPRINTF(( message("constraint_IsAlwaysTrue:examining %s", constraint_print(c) ) )); | |
e5f31c00 | 885 | |
886 | if (sizeOfMaxSet(c) ) | |
887 | return TRUE; | |
888 | ||
616915dd | 889 | if (constraintExpr_canGetValue(l) && constraintExpr_canGetValue(r) ) |
890 | { | |
891 | int cmp; | |
892 | cmp = constraintExpr_compare (l, r); | |
893 | switch (c->ar) | |
894 | { | |
895 | case EQ: | |
896 | return (cmp == 0); | |
897 | case GT: | |
898 | return (cmp > 0); | |
899 | case GTE: | |
900 | return (cmp >= 0); | |
901 | case LTE: | |
902 | return (cmp <= 0); | |
903 | case LT: | |
904 | return (cmp < 0); | |
905 | ||
906 | default: | |
9280addf | 907 | BADEXIT; |
84c9ffbf | 908 | /*@notreached@*/ |
9280addf | 909 | break; |
910 | } | |
911 | } | |
912 | ||
913 | if (constraintExpr_similar (l,r) ) | |
914 | { | |
915 | switch (c->ar) | |
916 | { | |
917 | case EQ: | |
918 | case GTE: | |
919 | case LTE: | |
920 | return TRUE; | |
921 | ||
922 | case GT: | |
923 | case LT: | |
924 | break; | |
925 | default: | |
926 | BADEXIT; | |
84c9ffbf | 927 | /*@notreached@*/ |
616915dd | 928 | break; |
929 | } | |
930 | } | |
9280addf | 931 | |
932 | l = constraintExpr_copy (c->lexpr); | |
933 | r = constraintExpr_copy (c->expr); | |
934 | ||
9280addf | 935 | r = constraintExpr_propagateConstants (r, &rHasConstant, &rConstant); |
936 | ||
90bc41f7 | 937 | if (constraintExpr_similar (l,r) && (rHasConstant ) ) |
9280addf | 938 | { |
939 | DPRINTF(( message("constraint_IsAlwaysTrue: after removing constants %s and %s are similar", constraintExpr_unparse(l), constraintExpr_unparse(r) ) )); | |
90bc41f7 | 940 | DPRINTF(( message("constraint_IsAlwaysTrue: rconstant is %d", rConstant ) )); |
bb25bea6 | 941 | |
942 | constraintExpr_free(l); | |
943 | constraintExpr_free(r); | |
944 | ||
90bc41f7 | 945 | switch (c->ar) |
9280addf | 946 | { |
90bc41f7 | 947 | case EQ: |
948 | return (rConstant == 0); | |
949 | case LT: | |
950 | return (rConstant > 0); | |
951 | case LTE: | |
952 | return (rConstant >= 0); | |
953 | case GTE: | |
954 | return (rConstant <= 0); | |
955 | case GT: | |
956 | return (rConstant < 0); | |
957 | ||
958 | default: | |
959 | BADEXIT; | |
84c9ffbf | 960 | /*@notreached@*/ |
90bc41f7 | 961 | break; |
9280addf | 962 | } |
9280addf | 963 | } |
90bc41f7 | 964 | else |
965 | { | |
bb25bea6 | 966 | constraintExpr_free(l); |
967 | constraintExpr_free(r); | |
90bc41f7 | 968 | DPRINTF(( message("Constraint %s is not always true", constraint_print(c) ) )); |
969 | return FALSE; | |
970 | } | |
9280addf | 971 | |
972 | BADEXIT; | |
616915dd | 973 | } |
974 | ||
bb25bea6 | 975 | static bool rangeCheck (arithType ar1, /*@observer@*/ constraintExpr expr1, arithType ar2, /*@observer@*/ constraintExpr expr2) |
616915dd | 976 | |
977 | { | |
bb25bea6 | 978 | DPRINTF ((message ("Doing Range CHECK %s and %s", constraintExpr_unparse(expr1), constraintExpr_unparse(expr2) ) )); |
616915dd | 979 | |
980 | if (! arithType_canResolve (ar1, ar2) ) | |
981 | return FALSE; | |
982 | ||
983 | switch (ar1) | |
984 | { | |
985 | case GTE: | |
2681ee39 | 986 | if (constraintExpr_similar (expr1, expr2) ) |
987 | return TRUE; | |
988 | /*@fallthrough@*/ | |
989 | case GT: | |
616915dd | 990 | if (! (constraintExpr_canGetValue (expr1) && |
2681ee39 | 991 | constraintExpr_canGetValue (expr2) ) ) |
992 | { | |
993 | constraintExpr e1, e2; | |
994 | bool p1, p2; | |
995 | int const1, const2; | |
996 | ||
997 | e1 = constraintExpr_copy(expr1); | |
998 | e2 = constraintExpr_copy(expr2); | |
999 | ||
1000 | e1 = constraintExpr_propagateConstants (e1, &p1, &const1); | |
1001 | ||
1002 | e2 = constraintExpr_propagateConstants (e2, &p2, &const2); | |
1003 | ||
1004 | if (p1 || p2) | |
1005 | { | |
1006 | if (!p1) | |
1007 | const1 = 0; | |
1008 | ||
1009 | if (!p2) | |
1010 | const2 = 0; | |
1011 | ||
1012 | if (const1 <= const2) | |
1013 | if (constraintExpr_similar (e1, e2) ) | |
1014 | { | |
1015 | constraintExpr_free(e1); | |
1016 | constraintExpr_free(e2); | |
1017 | return TRUE; | |
1018 | } | |
1019 | } | |
bb7c2085 | 1020 | DPRINTF(("Can't Get value")); |
2681ee39 | 1021 | |
1022 | constraintExpr_free(e1); | |
1023 | constraintExpr_free(e2); | |
1024 | return FALSE; | |
1025 | } | |
1026 | ||
1027 | if (constraintExpr_compare (expr2, expr1) >= 0) | |
1028 | return TRUE; | |
616915dd | 1029 | |
1030 | return FALSE; | |
2681ee39 | 1031 | case EQ: |
1032 | if (constraintExpr_similar (expr1, expr2) ) | |
1033 | return TRUE; | |
1034 | ||
1035 | return FALSE; | |
1036 | case LTE: | |
1037 | if (constraintExpr_similar (expr1, expr2) ) | |
1038 | return TRUE; | |
1039 | /*@fallthrough@*/ | |
1040 | case LT: | |
1041 | if (! (constraintExpr_canGetValue (expr1) && | |
1042 | constraintExpr_canGetValue (expr2) ) ) | |
1043 | { | |
1044 | constraintExpr e1, e2; | |
1045 | bool p1, p2; | |
1046 | int const1, const2; | |
1047 | ||
1048 | e1 = constraintExpr_copy(expr1); | |
1049 | e2 = constraintExpr_copy(expr2); | |
1050 | ||
1051 | e1 = constraintExpr_propagateConstants (e1, &p1, &const1); | |
1052 | ||
1053 | e2 = constraintExpr_propagateConstants (e2, &p2, &const2); | |
1054 | ||
1055 | if (p1 || p2) | |
1056 | { | |
1057 | if (!p1) | |
1058 | const1 = 0; | |
1059 | ||
1060 | if (!p2) | |
1061 | const2 = 0; | |
1062 | ||
1063 | if (const1 >= const2) | |
1064 | if (constraintExpr_similar (e1, e2) ) | |
1065 | { | |
1066 | constraintExpr_free(e1); | |
1067 | constraintExpr_free(e2); | |
1068 | return TRUE; | |
1069 | } | |
1070 | } | |
1071 | constraintExpr_free(e1); | |
1072 | constraintExpr_free(e2); | |
1073 | ||
bb7c2085 | 1074 | DPRINTF(("Can't Get value")); |
2681ee39 | 1075 | return FALSE; |
1076 | } | |
1077 | ||
1078 | if (constraintExpr_compare (expr2, expr1) <= 0) | |
1079 | return TRUE; | |
1080 | ||
1081 | return FALSE; | |
1082 | ||
1083 | default: | |
1084 | llcontbug((message("Unhandled case in switch: %q", arithType_print(ar1) ) ) ); | |
1085 | } | |
616915dd | 1086 | BADEXIT; |
616915dd | 1087 | } |
1088 | ||
28bf4b0b | 1089 | static constraint constraint_searchandreplace (/*@returned@*/ constraint c, constraintExpr old, constraintExpr newExpr) |
616915dd | 1090 | { |
1091 | DPRINTF (("Doing replace for lexpr") ); | |
28bf4b0b | 1092 | c->lexpr = constraintExpr_searchandreplace (c->lexpr, old, newExpr); |
616915dd | 1093 | DPRINTF (("Doing replace for expr") ); |
28bf4b0b | 1094 | c->expr = constraintExpr_searchandreplace (c->expr, old, newExpr); |
616915dd | 1095 | return c; |
1096 | } | |
1097 | ||
84c9ffbf | 1098 | bool constraint_search (constraint c, constraintExpr old) /*@*/ |
616915dd | 1099 | { |
1100 | bool ret; | |
1101 | ret = FALSE; | |
1102 | ||
1103 | ret = constraintExpr_search (c->lexpr, old); | |
1104 | ret = ret || constraintExpr_search (c->expr, old); | |
1105 | return ret; | |
1106 | } | |
1107 | ||
b7b694d6 | 1108 | /* adjust file locs and stuff */ |
bb25bea6 | 1109 | static constraint constraint_adjust (/*@returned@*/ constraint substitute, /*@observer@*/ constraint old) |
616915dd | 1110 | { |
1111 | fileloc loc1, loc2, loc3; | |
1112 | ||
bb7c2085 | 1113 | DPRINTF ((message("Start adjust on %s and %s", constraint_print(substitute), |
616915dd | 1114 | constraint_print(old)) |
1115 | )); | |
1116 | ||
1117 | loc1 = constraint_getFileloc (old); | |
616915dd | 1118 | loc2 = constraintExpr_getFileloc (substitute->lexpr); |
616915dd | 1119 | loc3 = constraintExpr_getFileloc (substitute->expr); |
616915dd | 1120 | |
b7b694d6 | 1121 | /* special case of an equality that "contains itself" */ |
616915dd | 1122 | if (constraintExpr_search (substitute->expr, substitute->lexpr) ) |
1123 | if (fileloc_closer (loc1, loc3, loc2)) | |
1124 | { | |
1125 | constraintExpr temp; | |
bb7c2085 | 1126 | DPRINTF ((message("Doing adjust on %s", constraint_print(substitute) ) |
616915dd | 1127 | )); |
1128 | temp = substitute->lexpr; | |
1129 | substitute->lexpr = substitute->expr; | |
1130 | substitute->expr = temp; | |
1131 | substitute = constraint_simplify(substitute); | |
1132 | } | |
1133 | ||
bb25bea6 | 1134 | fileloc_free (loc1); |
1135 | fileloc_free (loc2); | |
1136 | fileloc_free (loc3); | |
1137 | ||
616915dd | 1138 | return substitute; |
1139 | ||
1140 | } | |
1141 | ||
90bc41f7 | 1142 | /* If function preforms substitutes based on inequality |
1143 | ||
1144 | It uses the rule x >= y && b < y ===> x >= b + 1 | |
1145 | ||
1146 | Warning this is sound but throws out information | |
1147 | */ | |
dc7f6a51 | 1148 | |
bb25bea6 | 1149 | constraint inequalitySubstitute (/*@returned@*/ constraint c, constraintList p) |
616915dd | 1150 | { |
1151 | if (c->ar != GTE) | |
1152 | return c; | |
1153 | ||
1154 | constraintList_elements (p, el) | |
1155 | { | |
bb7c2085 | 1156 | if ((el->ar == LT ) ) |
b7b694d6 | 1157 | /* if (!constraint_conflict (c, el) ) */ /*@i523 explain this! */ |
616915dd | 1158 | { |
90bc41f7 | 1159 | constraintExpr temp2; |
1160 | ||
bb25bea6 | 1161 | /*@i22*/ |
1162 | ||
90bc41f7 | 1163 | if (constraintExpr_same (el->expr, c->expr) ) |
616915dd | 1164 | { |
dc7f6a51 | 1165 | DPRINTF((message ("inequalitySubstitute Replacing %q in %q with %q", |
616915dd | 1166 | constraintExpr_print (c->expr), |
1167 | constraint_print (c), | |
1168 | constraintExpr_print (el->expr) ) | |
1169 | )); | |
90bc41f7 | 1170 | temp2 = constraintExpr_copy (el->lexpr); |
bb25bea6 | 1171 | constraintExpr_free(c->expr); |
90bc41f7 | 1172 | c->expr = constraintExpr_makeIncConstraintExpr (temp2); |
dc7f6a51 | 1173 | |
616915dd | 1174 | } |
1175 | ||
1176 | } | |
1177 | } | |
1178 | end_constraintList_elements; | |
1179 | ||
1180 | c = constraint_simplify(c); | |
1181 | return c; | |
1182 | } | |
1183 | ||
dc7f6a51 | 1184 | |
1185 | /* drl7x 7/26/001 | |
1186 | ||
1187 | THis function is like inequalitySubstitute but it adds the rule | |
1188 | added the rules x >= y && y <= b ===> x >= b | |
1189 | x >= y && y < b ===> x >= b + 1 | |
1190 | ||
1191 | This is sound but sonce it throws out additional information it should only one used | |
1192 | if we're oring constraints. | |
1193 | */ | |
1194 | ||
1195 | static constraint inequalitySubstituteStrong (/*@returned@*/ constraint c, constraintList p) | |
1196 | { | |
1197 | DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q", constraint_print(c) ) )); | |
1198 | ||
1199 | if (c->ar != GTE) | |
1200 | return c; | |
1201 | ||
1202 | DPRINTF (( message ("inequalitySubstituteStrong examining substituting for %q with %q", | |
1203 | constraint_print(c), constraintList_print(p) ) )); | |
1204 | constraintList_elements (p, el) | |
1205 | { | |
1206 | DPRINTF (( message ("inequalitySubstituteStrong examining substituting %s on %s", constraint_print(el), constraint_print(c) ) )); | |
1207 | ||
bb7c2085 | 1208 | if ((el->ar == LT ) || (el->ar == LTE ) ) |
b7b694d6 | 1209 | /* if (!constraint_conflict (c, el) ) */ /*@i523@*/ |
dc7f6a51 | 1210 | { |
dc7f6a51 | 1211 | constraintExpr temp2; |
1212 | ||
1213 | /*@i22*/ | |
1214 | ||
dc7f6a51 | 1215 | if (constraintExpr_same (el->lexpr, c->expr) ) |
1216 | { | |
1217 | DPRINTF((message ("inequalitySubstitute Replacing %s in %s with %s", | |
1218 | constraintExpr_print (c->expr), | |
1219 | constraint_print (c), | |
1220 | constraintExpr_print (el->expr) ) | |
1221 | )); | |
1222 | temp2 = constraintExpr_copy (el->expr); | |
1223 | constraintExpr_free(c->expr); | |
bb7c2085 | 1224 | if ((el->ar == LTE ) ) |
dc7f6a51 | 1225 | { |
1226 | c->expr = temp2; | |
1227 | } | |
1228 | else | |
1229 | { | |
1230 | c->expr = constraintExpr_makeIncConstraintExpr (temp2); | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | } | |
1235 | } | |
1236 | end_constraintList_elements; | |
1237 | ||
1238 | c = constraint_simplify(c); | |
1239 | return c; | |
1240 | } | |
1241 | ||
1242 | ||
90bc41f7 | 1243 | /* This function performs substitutions based on the rule: |
1244 | for a constraint of the form expr1 >= expr2; a < b => | |
1245 | a = b -1 for all a in expr1. This will work in most cases. | |
1246 | ||
1247 | Like inequalitySubstitute we're throwing away some information | |
1248 | */ | |
1249 | ||
bb25bea6 | 1250 | static constraint inequalitySubstituteUnsound (/*@returned@*/ constraint c, constraintList p) |
90bc41f7 | 1251 | { |
1252 | DPRINTF (( message ("Doing inequalitySubstituteUnsound " ) )); | |
1253 | ||
1254 | if (c->ar != GTE) | |
1255 | return c; | |
1256 | ||
1257 | constraintList_elements (p, el) | |
1258 | { | |
1259 | DPRINTF (( message ("inequalitySubstituteUnsound examining substituting %s on %s", constraint_print(el), constraint_print(c) ) )); | |
bb7c2085 | 1260 | if (( el->ar == LTE) || (el->ar == LT) ) |
b7b694d6 | 1261 | /* if (!constraint_conflict (c, el) ) */ /*@i532@*/ |
90bc41f7 | 1262 | { |
90bc41f7 | 1263 | constraintExpr temp2; |
b7b694d6 | 1264 | |
90bc41f7 | 1265 | temp2 = constraintExpr_copy (el->expr); |
1266 | ||
1267 | if (el->ar == LT) | |
1268 | temp2 = constraintExpr_makeDecConstraintExpr (temp2); | |
1269 | ||
1270 | DPRINTF((message ("Replacing %s in %s with %s", | |
1271 | constraintExpr_print (el->lexpr), | |
1272 | constraintExpr_print (c->lexpr), | |
1273 | constraintExpr_print (temp2) ) )); | |
1274 | ||
1275 | c->lexpr = constraintExpr_searchandreplace (c->lexpr, el->lexpr, temp2); | |
bb25bea6 | 1276 | constraintExpr_free(temp2); |
90bc41f7 | 1277 | } |
1278 | } | |
1279 | end_constraintList_elements; | |
1280 | ||
1281 | c = constraint_simplify(c); | |
1282 | return c; | |
1283 | } | |
1284 | ||
28bf4b0b | 1285 | /*@only@*/ constraint constraint_substitute (/*@observer@*/ /*@temp@*/ constraint c, constraintList p) |
616915dd | 1286 | { |
90bc41f7 | 1287 | constraint ret; |
1288 | ||
1289 | ret = constraint_copy(c); | |
616915dd | 1290 | constraintList_elements (p, el) |
1291 | { | |
1292 | if ( el->ar == EQ) | |
90bc41f7 | 1293 | if (!constraint_conflict (ret, el) ) |
616915dd | 1294 | |
1295 | { | |
1296 | constraint temp; | |
bb25bea6 | 1297 | |
616915dd | 1298 | temp = constraint_copy(el); |
1299 | ||
90bc41f7 | 1300 | temp = constraint_adjust(temp, ret); |
616915dd | 1301 | |
470b7798 | 1302 | DPRINTF((message ("Substituting %s in the constraint %s", |
90bc41f7 | 1303 | constraint_print (temp), constraint_print (ret) |
616915dd | 1304 | ) ) ); |
1305 | ||
1306 | ||
90bc41f7 | 1307 | ret = constraint_searchandreplace (ret, temp->lexpr, temp->expr); |
1308 | DPRINTF(( message ("The new constraint is %s", constraint_print (ret) ) )); | |
bb25bea6 | 1309 | constraint_free(temp); |
616915dd | 1310 | } |
1311 | } | |
1312 | end_constraintList_elements; | |
bb25bea6 | 1313 | DPRINTF(( message ("The finial new constraint is %s", constraint_print (ret) ) )); |
616915dd | 1314 | |
90bc41f7 | 1315 | ret = constraint_simplify(ret); |
1316 | return ret; | |
616915dd | 1317 | } |
1318 | ||
bb25bea6 | 1319 | |
1320 | /*@only@*/ constraintList constraintList_substituteFreeTarget (/*@only@*/ constraintList target, /*@observer@*/ constraintList subList) | |
1321 | { | |
1322 | constraintList ret; | |
1323 | ||
1324 | ret = constraintList_substitute (target, subList); | |
1325 | ||
1326 | constraintList_free(target); | |
1327 | ||
1328 | return ret; | |
1329 | } | |
1330 | ||
c3e695ff | 1331 | /* we try to do substitutions on each constraint in target using the constraint in sublist*/ |
1332 | ||
bb25bea6 | 1333 | /*@only@*/ constraintList constraintList_substitute (constraintList target,/*2observer@*/ constraintList subList) |
c3e695ff | 1334 | { |
1335 | ||
1336 | constraintList ret; | |
1337 | ||
1338 | ret = constraintList_makeNew(); | |
1339 | ||
1340 | constraintList_elements(target, el) | |
1341 | { | |
d46ce6a4 | 1342 | constraint temp; |
b7b694d6 | 1343 | /* drl possible problem : warning make sure that a side effect is not expected */ |
bb25bea6 | 1344 | |
28bf4b0b | 1345 | temp = constraint_substitute(el, subList); |
d46ce6a4 | 1346 | ret = constraintList_add (ret, temp); |
c3e695ff | 1347 | } |
1348 | end_constraintList_elements; | |
84c9ffbf | 1349 | |
bb25bea6 | 1350 | return ret; |
c3e695ff | 1351 | } |
616915dd | 1352 | |
28bf4b0b | 1353 | static constraint constraint_solve (/*@returned@*/ constraint c) |
616915dd | 1354 | { |
bb7c2085 | 1355 | DPRINTF((message ("Solving %s\n", constraint_print(c) ) ) ); |
616915dd | 1356 | c->expr = constraintExpr_solveBinaryExpr (c->lexpr, c->expr); |
bb7c2085 | 1357 | DPRINTF((message ("Solved and got %s\n", constraint_print(c) ) ) ); |
616915dd | 1358 | |
1359 | return c; | |
1360 | } | |
1361 | ||
1362 | static arithType flipAr (arithType ar) | |
1363 | { | |
1364 | switch (ar) | |
1365 | { | |
1366 | case LT: | |
1367 | return GT; | |
1368 | case LTE: | |
1369 | return GTE; | |
1370 | case EQ: | |
1371 | return EQ; | |
1372 | case GT: | |
1373 | return LT; | |
1374 | case GTE: | |
1375 | return LTE; | |
1376 | default: | |
84c9ffbf | 1377 | llcontbug (message("unexpected value: case not handled")); |
616915dd | 1378 | } |
1379 | BADEXIT; | |
1380 | } | |
1381 | ||
d46ce6a4 | 1382 | static constraint constraint_swapLeftRight (/*@returned@*/ constraint c) |
616915dd | 1383 | { |
1384 | constraintExpr temp; | |
1385 | c->ar = flipAr (c->ar); | |
1386 | temp = c->lexpr; | |
1387 | c->lexpr = c->expr; | |
1388 | c->expr = temp; | |
1389 | DPRINTF(("Swaped left and right sides of constraint")); | |
1390 | return c; | |
1391 | } | |
1392 | ||
c3e695ff | 1393 | |
1394 | ||
d46ce6a4 | 1395 | constraint constraint_simplify ( /*@returned@*/ constraint c) |
616915dd | 1396 | { |
a779b61e | 1397 | |
1398 | DPRINTF(( message("constraint_simplify on %q ", constraint_print(c) ) )); | |
1399 | ||
1400 | if (constraint_tooDeep(c)) | |
1401 | { | |
1402 | DPRINTF(( message("constraint_simplify: constraint to complex aborting %q ", constraint_print(c) ) )); | |
1403 | return c; | |
1404 | ||
1405 | } | |
1406 | ||
616915dd | 1407 | c->lexpr = constraintExpr_simplify (c->lexpr); |
1408 | c->expr = constraintExpr_simplify (c->expr); | |
616915dd | 1409 | |
bb25bea6 | 1410 | if (constraintExpr_isBinaryExpr (c->lexpr) ) |
c3e695ff | 1411 | { |
1412 | c = constraint_solve (c); | |
1413 | ||
1414 | c->lexpr = constraintExpr_simplify (c->lexpr); | |
1415 | c->expr = constraintExpr_simplify (c->expr); | |
1416 | } | |
1417 | ||
616915dd | 1418 | if (constraintExpr_isLit(c->lexpr) && (!constraintExpr_isLit(c->expr) ) ) |
1419 | { | |
1420 | c = constraint_swapLeftRight(c); | |
1421 | /*I don't think this will be an infinate loop*/ | |
84c9ffbf | 1422 | c = constraint_simplify(c); |
616915dd | 1423 | } |
a779b61e | 1424 | |
1425 | DPRINTF(( message("constraint_simplify returning %q ", constraint_print(c) ) )); | |
1426 | ||
616915dd | 1427 | return c; |
1428 | } | |
1429 | ||
1430 | ||
1431 | ||
1432 | ||
1433 | /* returns true if fileloc for term1 is closer to file for term2 than term3*/ | |
1434 | ||
1435 | bool fileloc_closer (fileloc loc1, fileloc loc2, fileloc loc3) | |
1436 | { | |
c3e695ff | 1437 | |
1438 | if (!fileloc_isDefined (loc1) ) | |
1439 | return FALSE; | |
1440 | ||
1441 | if (!fileloc_isDefined (loc2) ) | |
1442 | return FALSE; | |
1443 | ||
1444 | if (!fileloc_isDefined (loc3) ) | |
1445 | return TRUE; | |
1446 | ||
1447 | ||
1448 | ||
1449 | ||
1450 | if (fileloc_equal (loc2, loc3) ) | |
1451 | return FALSE; | |
1452 | ||
1453 | if (fileloc_equal (loc1, loc2) ) | |
1454 | return TRUE; | |
1455 | ||
1456 | if (fileloc_equal (loc1, loc3) ) | |
1457 | return FALSE; | |
1458 | ||
616915dd | 1459 | if ( fileloc_lessthan (loc1, loc2) ) |
1460 | { | |
1461 | if (fileloc_lessthan (loc2, loc3) ) | |
1462 | { | |
1463 | llassert (fileloc_lessthan (loc1, loc3) ); | |
1464 | return TRUE; | |
1465 | } | |
1466 | else | |
1467 | { | |
1468 | return FALSE; | |
1469 | } | |
1470 | } | |
1471 | ||
1472 | if ( !fileloc_lessthan (loc1, loc2) ) | |
1473 | { | |
1474 | if (!fileloc_lessthan (loc2, loc3) ) | |
1475 | { | |
1476 | llassert (!fileloc_lessthan (loc1, loc3) ); | |
1477 | return TRUE; | |
1478 | } | |
1479 | else | |
1480 | { | |
1481 | return FALSE; | |
1482 | } | |
1483 | } | |
1484 | ||
1485 | llassert(FALSE); | |
1486 | return FALSE; | |
1487 | } | |
1488 | ||
1489 |