]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
4ab867d6 | 2 | ** constraint.c |
616915dd | 3 | */ |
4 | ||
5 | //#define DEBUGPRINT 1 | |
6 | ||
7 | # include <ctype.h> /* for isdigit */ | |
8 | # include "lclintMacros.nf" | |
9 | # include "basic.h" | |
10 | # include "cgrammar.h" | |
11 | # include "cgrammar_tokens.h" | |
f0171cff | 12 | |
616915dd | 13 | # include "exprChecks.h" |
616915dd | 14 | # include "exprNodeSList.h" |
616915dd | 15 | |
16 | /*@i33*/ | |
616915dd | 17 | |
c3e695ff | 18 | /*@access exprNode @*/ |
616915dd | 19 | |
d46ce6a4 | 20 | |
28bf4b0b | 21 | static /*@only@*/ cstring constraint_printDetailedPostCondition (/*@observer@*/ /*@temp@*/ constraint p_c); |
2934b455 | 22 | |
23 | ||
ccf0a4a8 | 24 | static /*@notnull@*/ /*@special@*/ constraint constraint_makeNew (void) |
25 | /*@post:isnull result->or, result->orig, result->generatingExpr, result->fcnPre @*/ | |
26 | /*@defines result->or, result->generatingExpr, result->orig, result->fcnPre @*/; | |
4ab867d6 | 27 | |
d46ce6a4 | 28 | /* constraint makeConstraintParse (sRef x, lltok relOp, exprNode cconstant) */ |
616915dd | 29 | |
d46ce6a4 | 30 | /* { */ |
31 | /* char *t; */ | |
32 | /* int c; */ | |
33 | /* constraint ret; */ | |
34 | /* ret = constraint_makeNew(); */ | |
35 | /* llassert (sRef_isValid(x) ); */ | |
36 | /* if (!sRef_isValid(x)) */ | |
37 | /* return ret; */ | |
616915dd | 38 | |
39 | ||
d46ce6a4 | 40 | /* ret->lexpr = constraintExpr_makeTermsRef (x); */ |
41 | /* #warning fix abstraction */ | |
616915dd | 42 | |
d46ce6a4 | 43 | /* if (relOp.tok == GE_OP) */ |
44 | /* ret->ar = GTE; */ | |
45 | /* else if (relOp.tok == LE_OP) */ | |
46 | /* ret->ar = LTE; */ | |
47 | /* else if (relOp.tok == EQ_OP) */ | |
48 | /* ret->ar = EQ; */ | |
49 | /* else */ | |
50 | /* llfatalbug(message ("Unsupported relational operator") ); */ | |
616915dd | 51 | |
52 | ||
d46ce6a4 | 53 | /* t = cstring_toCharsSafe (exprNode_unparse(cconstant)); */ |
54 | /* c = atoi( t ); */ | |
55 | /* ret->expr = constraintExpr_makeIntLiteral (c); */ | |
616915dd | 56 | |
d46ce6a4 | 57 | /* ret->post = TRUE; */ |
58 | /* // ret->orig = ret; */ | |
59 | /* DPRINTF(("GENERATED CONSTRAINT:")); */ | |
60 | /* DPRINTF( (message ("%s", constraint_print(ret) ) ) ); */ | |
61 | /* return ret; */ | |
62 | /* } */ | |
616915dd | 63 | |
920a3797 | 64 | |
65 | static void | |
66 | advanceField (char **s) | |
67 | { | |
28bf4b0b | 68 | reader_checkChar (s, '@'); |
920a3797 | 69 | } |
70 | ||
71 | ||
72 | ||
28bf4b0b | 73 | static constraint makeConstraintParse2 (constraintExpr l, lltok relOp, exprNode cconstant) |
616915dd | 74 | { |
75 | char *t; | |
76 | int c; | |
77 | constraint ret; | |
ccf0a4a8 | 78 | ret = constraint_makeNew (); |
28bf4b0b | 79 | llassert (constraintExpr_isDefined(l) ); |
d46ce6a4 | 80 | |
616915dd | 81 | ret->lexpr = constraintExpr_copy (l); |
2934b455 | 82 | |
616915dd | 83 | |
84 | if (relOp.tok == GE_OP) | |
85 | ret->ar = GTE; | |
86 | else if (relOp.tok == LE_OP) | |
87 | ret->ar = LTE; | |
88 | else if (relOp.tok == EQ_OP) | |
89 | ret->ar = EQ; | |
90 | else | |
dc92450f | 91 | llfatalbug(message("Unsupported relational operator") ); |
616915dd | 92 | |
93 | ||
94 | t = cstring_toCharsSafe (exprNode_unparse(cconstant)); | |
95 | c = atoi( t ); | |
96 | ret->expr = constraintExpr_makeIntLiteral (c); | |
97 | ||
98 | ret->post = TRUE; | |
99 | // ret->orig = ret; | |
100 | DPRINTF(("GENERATED CONSTRAINT:")); | |
101 | DPRINTF( (message ("%s", constraint_print(ret) ) ) ); | |
102 | return ret; | |
103 | } | |
104 | ||
c3e695ff | 105 | bool constraint_same (constraint c1, constraint c2) |
90bc41f7 | 106 | { |
107 | ||
108 | if (c1->ar != c2->ar) | |
109 | return FALSE; | |
110 | ||
111 | if (!constraintExpr_similar (c1->lexpr, c2->lexpr) ) | |
112 | return FALSE; | |
113 | ||
114 | if (!constraintExpr_similar (c1->expr, c2->expr) ) | |
115 | return FALSE; | |
116 | ||
117 | return TRUE; | |
118 | } | |
616915dd | 119 | |
120 | constraint makeConstraintParse3 (constraintExpr l, lltok relOp, constraintExpr r) | |
121 | { | |
122 | constraint ret; | |
123 | ret = constraint_makeNew(); | |
28bf4b0b | 124 | llassert (constraintExpr_isDefined(l) ); |
616915dd | 125 | |
126 | ret->lexpr = constraintExpr_copy (l); | |
616915dd | 127 | |
128 | if (relOp.tok == GE_OP) | |
129 | ret->ar = GTE; | |
130 | else if (relOp.tok == LE_OP) | |
131 | ret->ar = LTE; | |
132 | else if (relOp.tok == EQ_OP) | |
133 | ret->ar = EQ; | |
134 | else | |
dc92450f | 135 | llfatalbug( message("Unsupported relational operator") ); |
616915dd | 136 | |
137 | ret->expr = constraintExpr_copy (r); | |
138 | ||
139 | ret->post = TRUE; | |
90bc41f7 | 140 | |
141 | ret->orig = constraint_copy(ret); | |
142 | ||
143 | ret = constraint_simplify (ret); | |
144 | ||
616915dd | 145 | // ret->orig = ret; |
146 | DPRINTF(("GENERATED CONSTRAINT:")); | |
147 | DPRINTF( (message ("%s", constraint_print(ret) ) ) ); | |
148 | return ret; | |
149 | } | |
150 | ||
28bf4b0b | 151 | constraint constraint_copy (/*@temp@*/ /*@observer@*/ constraint c) |
616915dd | 152 | { |
153 | constraint ret; | |
90bc41f7 | 154 | |
c3e695ff | 155 | llassert (constraint_isDefined(c) ); |
28bf4b0b | 156 | // DPRINTF((message("Copying constraint %q", constraint_print) )); |
90bc41f7 | 157 | |
616915dd | 158 | ret = constraint_makeNew(); |
159 | ret->lexpr = constraintExpr_copy (c->lexpr); | |
160 | ret->ar = c->ar; | |
161 | ret->expr = constraintExpr_copy (c->expr); | |
162 | ret->post = c->post; | |
795e7f34 | 163 | /*@-assignexpose@*/ |
28bf4b0b | 164 | ret->generatingExpr = c->generatingExpr; |
795e7f34 | 165 | /*@=assignexpose@*/ |
9280addf | 166 | |
616915dd | 167 | /*@i33 fix this*/ |
168 | if (c->orig != NULL) | |
169 | ret->orig = constraint_copy (c->orig); | |
170 | else | |
171 | ret->orig = NULL; | |
90bc41f7 | 172 | |
173 | if (c->or != NULL) | |
174 | ret->or = constraint_copy (c->or); | |
175 | else | |
176 | ret->or = NULL; | |
4ab867d6 | 177 | |
178 | ret->fcnPre = c->fcnPre; | |
90bc41f7 | 179 | |
616915dd | 180 | return ret; |
181 | } | |
182 | ||
183 | /*like copy expect it doesn't allocate memory for the constraint*/ | |
184 | ||
d46ce6a4 | 185 | void constraint_overWrite (constraint c1, constraint c2) |
616915dd | 186 | { |
d46ce6a4 | 187 | llassert (constraint_isDefined(c1) ); |
188 | ||
189 | llassert (c1 != c2); | |
190 | ||
191 | DPRINTF((message("OverWriteing constraint %q with %q", constraint_print(c1), | |
192 | constraint_print(c2) ) )); | |
193 | ||
194 | constraintExpr_free(c1->lexpr); | |
195 | constraintExpr_free(c1->expr); | |
196 | ||
616915dd | 197 | c1->lexpr = constraintExpr_copy (c2->lexpr); |
198 | c1->ar = c2->ar; | |
199 | c1->expr = constraintExpr_copy (c2->expr); | |
200 | c1->post = c2->post; | |
d46ce6a4 | 201 | |
202 | if (c1->orig != NULL) | |
203 | constraint_free (c1->orig); | |
204 | ||
616915dd | 205 | if (c2->orig != NULL) |
206 | c1->orig = constraint_copy (c2->orig); | |
207 | else | |
208 | c1->orig = NULL; | |
90bc41f7 | 209 | |
d46ce6a4 | 210 | /*@i33 make sure that the or is freed correctly*/ |
211 | if (c1->or != NULL) | |
212 | constraint_free (c1->or); | |
213 | ||
90bc41f7 | 214 | if (c2->or != NULL) |
215 | c1->or = constraint_copy (c2->or); | |
216 | else | |
217 | c1->or = NULL; | |
4ab867d6 | 218 | |
219 | c1->fcnPre = c2->fcnPre; | |
795e7f34 | 220 | |
221 | /*@-assignexpose@*/ | |
4ab867d6 | 222 | c1->generatingExpr = c2->generatingExpr; |
795e7f34 | 223 | /*@=assignexpose@*/ |
616915dd | 224 | } |
225 | ||
616915dd | 226 | |
227 | ||
d46ce6a4 | 228 | static /*@notnull@*/ /*@special@*/ constraint constraint_makeNew (void) |
ccf0a4a8 | 229 | /*@post:isnull result->or, result->orig, result->generatingExpr, result->fcnPre @*/ |
230 | /*@defines result->or, result->generatingExpr, result->orig, result->fcnPre @*/ | |
616915dd | 231 | { |
232 | constraint ret; | |
233 | ret = dmalloc(sizeof (*ret) ); | |
234 | ret->lexpr = NULL; | |
235 | ret->expr = NULL; | |
236 | ret->ar = LT; | |
237 | ret->post = FALSE; | |
238 | ret->orig = NULL; | |
90bc41f7 | 239 | ret->or = NULL; |
9280addf | 240 | ret->generatingExpr = NULL; |
4ab867d6 | 241 | ret->fcnPre = NULL; |
dc92450f | 242 | return ret; |
616915dd | 243 | } |
244 | ||
b37cf05e | 245 | constraint constraint_addGeneratingExpr (/*@returned@*/ constraint c, /*@exposed@*/ exprNode e) |
9280addf | 246 | { |
247 | ||
248 | if (c->generatingExpr == NULL) | |
249 | { | |
28bf4b0b | 250 | c->generatingExpr = e; |
9280addf | 251 | DPRINTF ((message ("setting generatingExpr for %s to %s", constraint_print(c), exprNode_unparse(e) ) )); |
252 | } | |
253 | else | |
254 | { | |
255 | DPRINTF ((message ("Not setting generatingExpr for %s to %s", constraint_print(c), exprNode_unparse(e) ) )); | |
256 | } | |
257 | return c; | |
258 | } | |
259 | ||
4ab867d6 | 260 | constraint constraint_origAddGeneratingExpr (/*@returned@*/ constraint c, exprNode e) |
261 | { | |
262 | ||
263 | if (c->orig != constraint_undefined) | |
264 | { | |
265 | c->orig = constraint_addGeneratingExpr(c->orig, e); | |
266 | } | |
267 | else | |
268 | { | |
269 | DPRINTF ((message ("constraint_origAddGeneratingExpr: Not setting generatingExpr for %s to %s", constraint_print(c), exprNode_unparse(e) ) )); | |
270 | } | |
271 | return c; | |
272 | } | |
273 | ||
4ab867d6 | 274 | constraint constraint_setFcnPre (/*@returned@*/ constraint c ) |
275 | { | |
276 | ||
277 | if (c->orig != constraint_undefined) | |
278 | { | |
279 | c->orig->fcnPre = TRUE; | |
280 | } | |
281 | else | |
282 | { | |
283 | c->fcnPre = TRUE; | |
28bf4b0b | 284 | // DPRINTF(( message("Warning Setting fcnPre directly") )); |
4ab867d6 | 285 | } |
286 | return c; | |
287 | } | |
288 | ||
289 | ||
290 | ||
291 | ||
616915dd | 292 | fileloc constraint_getFileloc (constraint c) |
293 | { | |
c3e695ff | 294 | if (exprNode_isDefined(c->generatingExpr) ) |
d46ce6a4 | 295 | return (fileloc_copy (exprNode_getfileloc (c->generatingExpr) ) ); |
9280addf | 296 | |
616915dd | 297 | return (constraintExpr_getFileloc (c->lexpr) ); |
298 | ||
299 | ||
300 | } | |
301 | ||
9280addf | 302 | static bool checkForMaxSet (constraint c) |
303 | { | |
304 | if (constraintExpr_hasMaxSet(c->lexpr) || constraintExpr_hasMaxSet(c->expr) ) | |
305 | return TRUE; | |
306 | ||
307 | return FALSE; | |
308 | } | |
309 | ||
310 | bool constraint_hasMaxSet(constraint c) | |
311 | { | |
dc92450f | 312 | if (c->orig != NULL) |
9280addf | 313 | { |
314 | if (checkForMaxSet(c->orig) ) | |
315 | return TRUE; | |
316 | } | |
317 | ||
318 | return (checkForMaxSet(c) ); | |
319 | } | |
320 | ||
28bf4b0b | 321 | constraint constraint_makeReadSafeExprNode ( exprNode po, exprNode ind) |
616915dd | 322 | { |
323 | constraint ret = constraint_makeNew(); | |
324 | // constraintTerm term; | |
28bf4b0b | 325 | po = po; |
326 | ind = ind; | |
616915dd | 327 | ret->lexpr = constraintExpr_makeMaxReadExpr(po); |
328 | ret->ar = GTE; | |
329 | ret->expr = constraintExpr_makeValueExpr (ind); | |
d46ce6a4 | 330 | ret->post = FALSE; |
616915dd | 331 | return ret; |
332 | } | |
333 | ||
28bf4b0b | 334 | constraint constraint_makeWriteSafeInt ( exprNode po, int ind) |
616915dd | 335 | { |
336 | constraint ret = constraint_makeNew(); | |
337 | ||
338 | ||
339 | ret->lexpr =constraintExpr_makeMaxSetExpr(po); | |
340 | ret->ar = GTE; | |
c3e695ff | 341 | ret->expr = constraintExpr_makeIntLiteral (ind); |
616915dd | 342 | /*@i1*/return ret; |
343 | } | |
344 | ||
84c9ffbf | 345 | constraint constraint_makeSRefSetBufferSize (sRef s, long int size) |
616915dd | 346 | { |
347 | constraint ret = constraint_makeNew(); | |
4ab867d6 | 348 | ret->lexpr = constraintExpr_makeSRefMaxset (s); |
616915dd | 349 | ret->ar = EQ; |
84c9ffbf | 350 | ret->expr = constraintExpr_makeIntLiteral ((int)size); |
616915dd | 351 | ret->post = TRUE; |
352 | /*@i1*/return ret; | |
353 | } | |
354 | ||
355 | constraint constraint_makeSRefWriteSafeInt (sRef s, int ind) | |
356 | { | |
357 | constraint ret = constraint_makeNew(); | |
358 | ||
359 | ||
4ab867d6 | 360 | ret->lexpr = constraintExpr_makeSRefMaxset ( s ); |
616915dd | 361 | ret->ar = GTE; |
c3e695ff | 362 | ret->expr = constraintExpr_makeIntLiteral (ind); |
616915dd | 363 | ret->post = TRUE; |
364 | /*@i1*/return ret; | |
365 | } | |
366 | ||
367 | /* drl added 01/12/2000 | |
368 | ||
369 | makes the constraint: Ensures index <= MaxRead(buffer) */ | |
370 | ||
371 | constraint constraint_makeEnsureLteMaxRead(exprNode index, exprNode buffer) | |
372 | { | |
373 | constraint ret = constraint_makeNew(); | |
374 | ||
375 | ret->lexpr = constraintExpr_makeValueExpr (index); | |
376 | ret->ar = LTE; | |
377 | ret->expr = constraintExpr_makeMaxReadExpr(buffer); | |
378 | ret->post = TRUE; | |
379 | return ret; | |
380 | } | |
381 | ||
382 | constraint constraint_makeWriteSafeExprNode (exprNode po, exprNode ind) | |
383 | { | |
384 | constraint ret = constraint_makeNew(); | |
385 | ||
386 | ||
387 | ret->lexpr =constraintExpr_makeMaxSetExpr(po); | |
388 | ret->ar = GTE; | |
389 | ret->expr = constraintExpr_makeValueExpr (ind); | |
390 | /*@i1*/return ret; | |
391 | } | |
392 | ||
393 | ||
28bf4b0b | 394 | constraint constraint_makeReadSafeInt ( exprNode t1, int index) |
616915dd | 395 | { |
396 | constraint ret = constraint_makeNew(); | |
397 | ||
28bf4b0b | 398 | ret->lexpr = constraintExpr_makeMaxReadExpr(t1); |
616915dd | 399 | ret->ar = GTE; |
28bf4b0b | 400 | ret->expr = constraintExpr_makeIntLiteral (index); |
d46ce6a4 | 401 | ret->post = FALSE; |
616915dd | 402 | return ret; |
403 | } | |
404 | ||
470b7798 | 405 | constraint constraint_makeSRefReadSafeInt (sRef s, int ind) |
406 | { | |
407 | constraint ret = constraint_makeNew(); | |
408 | ||
409 | ||
4ab867d6 | 410 | ret->lexpr = constraintExpr_makeSRefMaxRead (s ); |
470b7798 | 411 | ret->ar = GTE; |
c3e695ff | 412 | ret->expr = constraintExpr_makeIntLiteral (ind); |
470b7798 | 413 | ret->post = TRUE; |
414 | /*@i1*/return ret; | |
415 | } | |
416 | ||
28bf4b0b | 417 | constraint constraint_makeEnsureMaxReadAtLeast (exprNode t1, exprNode t2, fileloc sequencePoint) |
616915dd | 418 | { |
d46ce6a4 | 419 | constraint ret; |
616915dd | 420 | |
28bf4b0b | 421 | ret = constraint_makeReadSafeExprNode(t1, t2); |
616915dd | 422 | |
423 | ret->lexpr = constraintExpr_setFileloc (ret->lexpr, sequencePoint); | |
424 | ||
425 | ret->post = TRUE; | |
426 | ||
427 | // fileloc_incColumn (ret->lexpr->term->loc); | |
428 | return ret; | |
429 | } | |
430 | ||
d46ce6a4 | 431 | static constraint constraint_makeEnsuresOpConstraintExpr (/*@only@*/ constraintExpr c1, /*@only@*/ constraintExpr c2, fileloc sequencePoint, arithType ar) |
470b7798 | 432 | { |
433 | ||
434 | constraint ret; | |
435 | ||
28bf4b0b | 436 | llassert(constraintExpr_isDefined(c1) && constraintExpr_isDefined(c2) ); |
470b7798 | 437 | // llassert(sequencePoint); |
438 | ||
439 | ret = constraint_makeNew(); | |
440 | ||
441 | ret->lexpr = c1; | |
442 | ret->ar = ar; | |
443 | ret->post = TRUE; | |
444 | ret->expr = c2; | |
445 | ret->lexpr = constraintExpr_setFileloc (ret->lexpr, sequencePoint); | |
446 | return ret; | |
447 | } | |
616915dd | 448 | |
28bf4b0b | 449 | static constraint constraint_makeEnsuresOp (/*@dependent@*/ exprNode e1, /*@dependent@*/ exprNode e2, fileloc sequencePoint, arithType ar) |
616915dd | 450 | { |
470b7798 | 451 | constraintExpr c1, c2; |
452 | constraint ret; | |
616915dd | 453 | exprNode e; |
470b7798 | 454 | |
28bf4b0b | 455 | if (! (exprNode_isDefined(e1) && exprNode_isDefined(e2) ) ) |
616915dd | 456 | { |
457 | llcontbug((message("null exprNode, Exprnodes are %s and %s", | |
458 | exprNode_unparse(e1), exprNode_unparse(e2) ) | |
459 | )); | |
460 | } | |
470b7798 | 461 | |
462 | // llassert (sequencePoint); | |
463 | ||
28bf4b0b | 464 | e = e1; |
470b7798 | 465 | c1 = constraintExpr_makeValueExpr (e); |
466 | ||
28bf4b0b | 467 | e = e2; |
470b7798 | 468 | c2 = constraintExpr_makeValueExpr (e); |
469 | ||
470 | ret = constraint_makeEnsuresOpConstraintExpr (c1, c2, sequencePoint, ar); | |
616915dd | 471 | |
616915dd | 472 | return ret; |
473 | } | |
474 | ||
475 | ||
476 | /* make constraint ensures e1 == e2 */ | |
477 | ||
478 | constraint constraint_makeEnsureEqual (exprNode e1, exprNode e2, fileloc sequencePoint) | |
479 | { | |
480 | return ( constraint_makeEnsuresOp (e1, e2, sequencePoint, EQ) ); | |
481 | } | |
482 | ||
483 | /*make constraint ensures e1 < e2 */ | |
484 | constraint constraint_makeEnsureLessThan (exprNode e1, exprNode e2, fileloc sequencePoint) | |
485 | { | |
470b7798 | 486 | constraintExpr t1, t2; |
487 | ||
488 | t1 = constraintExpr_makeValueExpr (e1); | |
489 | t2 = constraintExpr_makeValueExpr (e2); | |
490 | ||
491 | /*change this to e1 <= (e2 -1) */ | |
492 | ||
493 | t2 = constraintExpr_makeDecConstraintExpr (t2); | |
494 | ||
495 | return ( constraint_makeEnsuresOpConstraintExpr (t1, t2, sequencePoint, LTE) ); | |
616915dd | 496 | } |
497 | ||
498 | constraint constraint_makeEnsureLessThanEqual (exprNode e1, exprNode e2, fileloc sequencePoint) | |
499 | { | |
500 | return ( constraint_makeEnsuresOp (e1, e2, sequencePoint, LTE) ); | |
501 | } | |
502 | ||
503 | constraint constraint_makeEnsureGreaterThan (exprNode e1, exprNode e2, fileloc sequencePoint) | |
504 | { | |
470b7798 | 505 | constraintExpr t1, t2; |
506 | ||
507 | t1 = constraintExpr_makeValueExpr (e1); | |
508 | t2 = constraintExpr_makeValueExpr (e2); | |
509 | ||
510 | ||
511 | /* change this to e1 >= (e2 + 1) */ | |
512 | t2 = constraintExpr_makeIncConstraintExpr (t2); | |
513 | ||
514 | ||
515 | return ( constraint_makeEnsuresOpConstraintExpr (t1, t2, sequencePoint, GTE) ); | |
616915dd | 516 | } |
517 | ||
518 | constraint constraint_makeEnsureGreaterThanEqual (exprNode e1, exprNode e2, fileloc sequencePoint) | |
519 | { | |
520 | return ( constraint_makeEnsuresOp (e1, e2, sequencePoint, GTE) ); | |
521 | } | |
522 | ||
523 | ||
524 | exprNode exprNode_copyConstraints (/*@returned@*/ exprNode dst, exprNode src) | |
525 | { | |
d46ce6a4 | 526 | constraintList_free(dst->ensuresConstraints); |
527 | constraintList_free(dst->requiresConstraints); | |
528 | constraintList_free(dst->trueEnsuresConstraints); | |
529 | constraintList_free(dst->falseEnsuresConstraints); | |
530 | ||
616915dd | 531 | dst->ensuresConstraints = constraintList_copy (src->ensuresConstraints ); |
532 | dst->requiresConstraints = constraintList_copy (src->requiresConstraints ); | |
533 | dst->trueEnsuresConstraints = constraintList_copy (src->trueEnsuresConstraints ); | |
534 | dst->falseEnsuresConstraints = constraintList_copy (src->falseEnsuresConstraints ); | |
535 | return dst; | |
536 | } | |
537 | ||
2934b455 | 538 | /* Makes the constraint e = e + f */ |
539 | constraint constraint_makeAddAssign (exprNode e, exprNode f, fileloc sequencePoint) | |
540 | { | |
541 | constraintExpr x1, x2, y; | |
542 | constraint ret; | |
543 | ||
544 | ret = constraint_makeNew(); | |
545 | ||
546 | x1 = constraintExpr_makeValueExpr (e); | |
547 | x2 = constraintExpr_copy(x1); | |
548 | y = constraintExpr_makeValueExpr (f); | |
549 | ||
550 | ret->lexpr = x1; | |
551 | ret->ar = EQ; | |
552 | ret->post = TRUE; | |
553 | ret->expr = constraintExpr_makeAddExpr (x2, y); | |
554 | ||
555 | ret->lexpr = constraintExpr_setFileloc (ret->lexpr, sequencePoint); | |
556 | ||
557 | return ret; | |
558 | } | |
559 | ||
560 | ||
561 | /* Makes the constraint e = e - f */ | |
562 | constraint constraint_makeSubtractAssign (exprNode e, exprNode f, fileloc sequencePoint) | |
563 | { | |
564 | constraintExpr x1, x2, y; | |
565 | constraint ret; | |
566 | ||
567 | ret = constraint_makeNew(); | |
568 | ||
569 | x1 = constraintExpr_makeValueExpr (e); | |
570 | x2 = constraintExpr_copy(x1); | |
571 | y = constraintExpr_makeValueExpr (f); | |
572 | ||
573 | ret->lexpr = x1; | |
574 | ret->ar = EQ; | |
575 | ret->post = TRUE; | |
576 | ret->expr = constraintExpr_makeSubtractExpr (x2, y); | |
577 | ||
578 | ret->lexpr = constraintExpr_setFileloc (ret->lexpr, sequencePoint); | |
579 | ||
580 | return ret; | |
581 | } | |
582 | ||
616915dd | 583 | constraint constraint_makeMaxSetSideEffectPostDecrement (exprNode e, fileloc sequencePoint) |
584 | { | |
585 | constraint ret = constraint_makeNew(); | |
586 | //constraintTerm term; | |
587 | ||
28bf4b0b | 588 | // e = exprNode_fakeCopy(e); |
616915dd | 589 | ret->lexpr = constraintExpr_makeValueExpr (e); |
590 | ret->ar = EQ; | |
591 | ret->post = TRUE; | |
592 | ret->expr = constraintExpr_makeValueExpr (e); | |
593 | ret->expr = constraintExpr_makeDecConstraintExpr (ret->expr); | |
594 | ||
595 | ret->lexpr = constraintExpr_setFileloc (ret->lexpr, sequencePoint); | |
596 | // fileloc_incColumn ( ret->lexpr->term->loc); | |
597 | // fileloc_incColumn ( ret->lexpr->term->loc); | |
598 | return ret; | |
599 | } | |
600 | constraint constraint_makeMaxSetSideEffectPostIncrement (exprNode e, fileloc sequencePoint) | |
601 | { | |
602 | constraint ret = constraint_makeNew(); | |
603 | //constraintTerm term; | |
604 | ||
28bf4b0b | 605 | // e = exprNode_fakeCopy(e); |
616915dd | 606 | ret->lexpr = constraintExpr_makeValueExpr (e); |
607 | ret->ar = EQ; | |
608 | ret->post = TRUE; | |
609 | ret->expr = constraintExpr_makeValueExpr (e); | |
610 | ret->expr = constraintExpr_makeIncConstraintExpr (ret->expr); | |
611 | ||
612 | ret->lexpr = constraintExpr_setFileloc (ret->lexpr, sequencePoint); | |
613 | // fileloc_incColumn ( ret->lexpr->term->loc); | |
614 | // fileloc_incColumn ( ret->lexpr->term->loc); | |
615 | return ret; | |
616 | } | |
617 | ||
618 | ||
920a3797 | 619 | void constraint_free (/*@only@*/ constraint c) |
d46ce6a4 | 620 | { |
621 | llassert(constraint_isDefined (c) ); | |
622 | ||
623 | ||
624 | if (constraint_isDefined(c->orig) ) | |
625 | constraint_free (c->orig); | |
626 | if ( constraint_isDefined(c->or) ) | |
627 | constraint_free (c->or); | |
628 | ||
bb25bea6 | 629 | |
d46ce6a4 | 630 | constraintExpr_free(c->lexpr); |
631 | constraintExpr_free(c->expr); | |
bb25bea6 | 632 | |
633 | c->orig = NULL; | |
634 | c->or = NULL; | |
635 | c->lexpr = NULL; | |
636 | c->expr = NULL; | |
637 | ||
d46ce6a4 | 638 | free (c); |
639 | ||
640 | } | |
641 | ||
616915dd | 642 | |
643 | // constraint constraint_makeMaxReadSideEffectPostIncrement (exprNode e, fileloc sequencePoint) | |
644 | // { | |
645 | // constraint ret = constraint_makeNew(); | |
646 | // //constraintTerm term; | |
647 | ||
648 | // e = exprNode_fakeCopy(e); | |
649 | // ret->lexpr = constraintExpr_makeMaxReadExpr(e); | |
650 | // ret->ar = EQ; | |
651 | // ret->post = TRUE; | |
652 | // ret->expr = constraintExpr_makeIncConstraintExpr (e); | |
653 | // ret->lexpr = constraintExpr_setFileLoc (ret->lexpr, sequencePoint); | |
654 | // return ret; | |
655 | // } | |
656 | ||
657 | ||
dc92450f | 658 | cstring arithType_print (arithType ar) /*@*/ |
616915dd | 659 | { |
660 | cstring st = cstring_undefined; | |
661 | switch (ar) | |
662 | { | |
663 | case LT: | |
664 | st = cstring_makeLiteral (" < "); | |
665 | break; | |
666 | case LTE: | |
667 | st = cstring_makeLiteral (" <= "); | |
668 | break; | |
669 | case GT: | |
670 | st = cstring_makeLiteral (" > "); | |
671 | break; | |
672 | case GTE: | |
673 | st = cstring_makeLiteral (" >= "); | |
674 | break; | |
675 | case EQ: | |
676 | st = cstring_makeLiteral (" == "); | |
677 | break; | |
678 | case NONNEGATIVE: | |
679 | st = cstring_makeLiteral (" NONNEGATIVE "); | |
680 | break; | |
681 | case POSITIVE: | |
682 | st = cstring_makeLiteral (" POSITIVE "); | |
683 | break; | |
684 | default: | |
685 | llassert(FALSE); | |
686 | break; | |
687 | } | |
688 | return st; | |
689 | } | |
690 | ||
8f299805 | 691 | void constraint_printErrorPostCondition (constraint c, fileloc loc) |
692 | { | |
693 | cstring string; | |
694 | fileloc errorLoc, temp; | |
695 | ||
696 | string = constraint_printDetailedPostCondition (c); | |
697 | ||
698 | errorLoc = loc; | |
699 | ||
700 | loc = NULL; | |
701 | ||
702 | temp = constraint_getFileloc(c); | |
703 | ||
704 | if (fileloc_isDefined(temp) ) | |
705 | { | |
706 | errorLoc = temp; | |
707 | voptgenerror ( FLG_CHECKPOST, string, errorLoc); | |
708 | fileloc_free(temp); | |
709 | } | |
710 | else | |
711 | { | |
712 | voptgenerror ( FLG_CHECKPOST, string, errorLoc); | |
713 | } | |
714 | } | |
715 | ||
716 | ||
717 | ||
718 | ||
616915dd | 719 | void constraint_printError (constraint c, fileloc loc) |
720 | { | |
721 | cstring string; | |
4ab867d6 | 722 | fileloc errorLoc, temp; |
9280addf | 723 | |
616915dd | 724 | string = constraint_printDetailed (c); |
9280addf | 725 | |
726 | errorLoc = loc; | |
727 | ||
4ab867d6 | 728 | loc = NULL; |
729 | ||
730 | temp = constraint_getFileloc(c); | |
731 | ||
732 | if (fileloc_isDefined(temp) ) | |
616915dd | 733 | { |
4ab867d6 | 734 | errorLoc = temp; |
735 | ||
736 | if (c->post) | |
737 | { | |
738 | voptgenerror (FLG_FUNCTIONPOST, string, errorLoc); | |
739 | } | |
740 | else | |
741 | { | |
742 | voptgenerror (FLG_FUNCTIONCONSTRAINT, string, errorLoc); | |
743 | } | |
744 | fileloc_free(temp); | |
28bf4b0b | 745 | errorLoc = NULL; |
616915dd | 746 | } |
747 | else | |
748 | { | |
4ab867d6 | 749 | if (c->post) |
750 | { | |
751 | voptgenerror (FLG_FUNCTIONPOST, string, errorLoc); | |
752 | } | |
753 | else | |
754 | { | |
755 | voptgenerror (FLG_FUNCTIONCONSTRAINT, string, errorLoc); | |
756 | } | |
28bf4b0b | 757 | errorLoc = NULL; |
616915dd | 758 | } |
616915dd | 759 | } |
760 | ||
4ab867d6 | 761 | |
28bf4b0b | 762 | static cstring constraint_printDeep (constraint c) |
616915dd | 763 | { |
28bf4b0b | 764 | cstring genExpr; |
616915dd | 765 | cstring st = cstring_undefined; |
766 | ||
4ab867d6 | 767 | st = constraint_print(c); |
768 | ||
28bf4b0b | 769 | |
4ab867d6 | 770 | if (c->orig != constraint_undefined) |
771 | { | |
28bf4b0b | 772 | genExpr = exprNode_unparse(c->orig->generatingExpr); |
4ab867d6 | 773 | if (!c->post) |
774 | { | |
775 | if (c->orig->fcnPre) | |
28bf4b0b | 776 | st = cstring_concatFree(st, (message(" derived from %s precondition: %q", genExpr, constraint_printDeep(c->orig) ) |
4ab867d6 | 777 | ) ); |
778 | else | |
779 | st = cstring_concatFree(st,(message(" needed to satisfy %q", | |
780 | constraint_printDeep(c->orig) ) | |
781 | ) ); | |
782 | ||
783 | } | |
784 | else | |
785 | { | |
786 | st = cstring_concatFree(st,(message("derived from: %q", | |
787 | constraint_printDeep(c->orig) ) | |
788 | ) ); | |
789 | } | |
790 | } | |
791 | ||
792 | return st; | |
793 | ||
794 | } | |
795 | ||
2934b455 | 796 | |
797 | static /*@only@*/ cstring constraint_printDetailedPostCondition (/*@observer@*/ /*@temp@*/ constraint c) | |
8f299805 | 798 | { |
799 | cstring st = cstring_undefined; | |
28bf4b0b | 800 | cstring genExpr; |
801 | ||
8f299805 | 802 | st = message ("Unsatisfied ensures constraint condition:\nLCLint is unable to verify the constraint %q", constraint_printDeep (c) ); |
28bf4b0b | 803 | |
804 | genExpr = exprNode_unparse (c->generatingExpr); | |
805 | ||
8f299805 | 806 | if (context_getFlag (FLG_CONSTRAINTLOCATION) ) |
807 | { | |
808 | cstring temp; | |
809 | // llassert (c->generatingExpr); | |
810 | temp = message ("\nOriginal Generating expression %q: %s\n", fileloc_unparse( exprNode_getfileloc (c->generatingExpr) ), | |
28bf4b0b | 811 | genExpr ); |
8f299805 | 812 | st = cstring_concatFree (st, temp); |
813 | ||
814 | if (constraint_hasMaxSet(c) ) | |
815 | { | |
816 | temp = message ("Has MaxSet\n"); | |
817 | st = cstring_concatFree (st, temp); | |
818 | } | |
819 | } | |
820 | return st; | |
821 | } | |
822 | ||
4ab867d6 | 823 | cstring constraint_printDetailed (constraint c) |
824 | { | |
825 | cstring st = cstring_undefined; | |
28bf4b0b | 826 | cstring genExpr; |
827 | ||
616915dd | 828 | if (!c->post) |
829 | { | |
4ab867d6 | 830 | st = message ("Unresolved constraint:\nLclint is unable to resolve %q", constraint_printDeep (c) ); |
616915dd | 831 | } |
832 | else | |
833 | { | |
4ab867d6 | 834 | st = message ("Block Post condition:\nThis function block has the post condition %q", constraint_printDeep (c) ); |
616915dd | 835 | } |
9280addf | 836 | |
28bf4b0b | 837 | genExpr = exprNode_unparse (c->generatingExpr); |
838 | ||
9280addf | 839 | if (context_getFlag (FLG_CONSTRAINTLOCATION) ) |
840 | { | |
841 | cstring temp; | |
842 | // llassert (c->generatingExpr); | |
d46ce6a4 | 843 | temp = message ("\nOriginal Generating expression %q: %s\n", fileloc_unparse( exprNode_getfileloc (c->generatingExpr) ), |
28bf4b0b | 844 | genExpr ); |
d46ce6a4 | 845 | st = cstring_concatFree (st, temp); |
9280addf | 846 | |
847 | if (constraint_hasMaxSet(c) ) | |
848 | { | |
8f299805 | 849 | temp = message ("Has MaxSet\n"); |
d46ce6a4 | 850 | st = cstring_concatFree (st, temp); |
9280addf | 851 | } |
852 | } | |
616915dd | 853 | return st; |
854 | } | |
855 | ||
d46ce6a4 | 856 | /*@only@*/ cstring constraint_print (constraint c) /*@*/ |
616915dd | 857 | { |
858 | cstring st = cstring_undefined; | |
859 | cstring type = cstring_undefined; | |
dc92450f | 860 | llassert (c !=NULL); |
616915dd | 861 | if (c->post) |
862 | { | |
863 | type = cstring_makeLiteral ("Ensures: "); | |
864 | } | |
865 | else | |
866 | { | |
867 | type = cstring_makeLiteral ("Requires: "); | |
868 | } | |
d46ce6a4 | 869 | st = message ("%q: %q %q %q", |
616915dd | 870 | type, |
871 | constraintExpr_print (c->lexpr), | |
872 | arithType_print(c->ar), | |
873 | constraintExpr_print(c->expr) | |
874 | ); | |
875 | return st; | |
876 | } | |
877 | ||
90bc41f7 | 878 | cstring constraint_printOr (constraint c) /*@*/ |
879 | { | |
880 | cstring ret; | |
881 | constraint temp; | |
882 | ||
883 | ret = cstring_undefined; | |
884 | temp = c; | |
885 | ||
d46ce6a4 | 886 | ret = cstring_concatFree (ret, constraint_print(temp) ); |
90bc41f7 | 887 | |
888 | temp = temp->or; | |
889 | ||
c3e695ff | 890 | while ( constraint_isDefined(temp) ) |
90bc41f7 | 891 | { |
d46ce6a4 | 892 | ret = cstring_concatFree (ret, cstring_makeLiteral (" OR ") ); |
893 | ret = cstring_concatFree (ret, constraint_print(temp) ); | |
90bc41f7 | 894 | temp = temp->or; |
895 | } | |
896 | ||
897 | return ret; | |
898 | ||
899 | } | |
900 | ||
dc92450f | 901 | /*@only@*/ constraint constraint_doSRefFixBaseParam (/*@returned@*/ /*@only@*/ constraint precondition, |
616915dd | 902 | exprNodeList arglist) |
903 | { | |
904 | precondition->lexpr = constraintExpr_doSRefFixBaseParam (precondition->lexpr, | |
905 | arglist); | |
906 | precondition->expr = constraintExpr_doSRefFixBaseParam (precondition->expr, | |
907 | arglist); | |
908 | ||
909 | return precondition; | |
910 | } | |
911 | ||
912 | ||
28bf4b0b | 913 | constraint constraint_doFixResult (constraint postcondition, /*@dependent@*/ exprNode fcnCall) |
616915dd | 914 | { |
915 | postcondition = constraint_copy (postcondition); | |
916 | postcondition->lexpr = constraintExpr_doFixResult (postcondition->lexpr, fcnCall); | |
917 | postcondition->expr = constraintExpr_doFixResult (postcondition->expr, fcnCall); | |
918 | ||
919 | return postcondition; | |
920 | } | |
921 | ||
d46ce6a4 | 922 | /*@only@*/ constraint constraint_doSRefFixConstraintParam (constraint precondition, |
616915dd | 923 | exprNodeList arglist) |
924 | { | |
925 | ||
926 | precondition = constraint_copy (precondition); | |
927 | precondition->lexpr = constraintExpr_doSRefFixConstraintParam (precondition->lexpr, arglist); | |
928 | precondition->expr = constraintExpr_doSRefFixConstraintParam (precondition->expr, arglist); | |
929 | ||
4ab867d6 | 930 | precondition->fcnPre = FALSE; |
616915dd | 931 | return precondition; |
932 | } | |
933 | ||
934 | // bool constraint_hasTerm (constraint c, constraintTerm term) | |
935 | // { | |
936 | // DPRINTF((message ("Constraint %s", constraint_print (c) ) ) ); | |
937 | ||
938 | // if (constraintExpr_includesTerm (c->lexpr, term) ) | |
939 | // return TRUE; | |
940 | ||
941 | // if (constraintExpr_includesTerm (c->expr, term) ) | |
942 | // return TRUE; | |
943 | ||
944 | // return FALSE; | |
945 | // } | |
946 | ||
d46ce6a4 | 947 | constraint constraint_preserveOrig (/*@returned@*/ constraint c) /*@modifies c @*/ |
616915dd | 948 | { |
4ab867d6 | 949 | |
950 | DPRINTF( (message("Doing constraint_preserverOrig for %q ", constraint_printDetailed(c) ) )); | |
951 | ||
d46ce6a4 | 952 | if (c->orig == constraint_undefined) |
953 | c->orig = constraint_copy (c); | |
4ab867d6 | 954 | |
955 | else if (c->orig->fcnPre) | |
956 | { | |
957 | constraint temp; | |
958 | ||
959 | temp = c->orig; | |
960 | ||
961 | /* avoid infinite loop */ | |
962 | c->orig = NULL; | |
963 | c->orig = constraint_copy (c); | |
964 | if (c->orig->orig == NULL) | |
920a3797 | 965 | { |
966 | c->orig->orig = temp; | |
967 | temp = NULL; | |
968 | } | |
4ab867d6 | 969 | else |
920a3797 | 970 | { |
971 | llcontbug((message("Expected c->orig->orig to be null" ) )); | |
972 | constraint_free(c->orig->orig); | |
973 | c->orig->orig = temp; | |
974 | temp = NULL; | |
975 | } | |
4ab867d6 | 976 | } |
977 | else | |
978 | { | |
979 | DPRINTF( (message("Not changing constraint") )); | |
980 | } | |
d46ce6a4 | 981 | |
4ab867d6 | 982 | DPRINTF( (message("After Doing constraint_preserverOrig for %q ", constraint_printDetailed(c) ) )); |
983 | ||
616915dd | 984 | return c; |
985 | } | |
986 | /*@=fcnuse*/ | |
987 | /*@=assignexpose*/ | |
988 | /*@=czechfcns@*/ | |
84c9ffbf | 989 | |
2934b455 | 990 | |
84c9ffbf | 991 | constraint constraint_togglePost (/*@returned@*/ constraint c) |
992 | { | |
993 | c->post = !c->post; | |
994 | return c; | |
995 | } | |
2934b455 | 996 | |
997 | constraint constraint_togglePostOrig (/*@returned@*/ constraint c) | |
998 | { | |
999 | if (c->orig != NULL) | |
1000 | c->orig = constraint_togglePost(c->orig); | |
1001 | return c; | |
1002 | } | |
1003 | ||
1004 | bool constraint_hasOrig( /*@observer@*/ /*@temp@*/ constraint c) | |
1005 | { | |
1006 | if (c->orig == NULL) | |
1007 | return FALSE; | |
1008 | else | |
1009 | return TRUE; | |
1010 | } | |
920a3797 | 1011 | |
1012 | ||
1013 | constraint constraint_undump (FILE *f) | |
1014 | { | |
1015 | constraint c; | |
1016 | bool fcnPre; | |
1017 | bool post; | |
1018 | arithType ar; | |
1019 | ||
1020 | constraintExpr lexpr; | |
1021 | constraintExpr expr; | |
28bf4b0b | 1022 | |
920a3797 | 1023 | |
1024 | char * s; | |
1025 | ||
1026 | char *os; | |
1027 | ||
1028 | s = mstring_create (MAX_DUMP_LINE_LENGTH); | |
1029 | ||
1030 | os = s; | |
1031 | ||
1032 | s = fgets(os, MAX_DUMP_LINE_LENGTH, f); | |
1033 | ||
1034 | /*@i33*/ /*this should probably be wrappered...*/ | |
1035 | ||
28bf4b0b | 1036 | fcnPre = (bool) reader_getInt (&s); |
920a3797 | 1037 | advanceField(&s); |
28bf4b0b | 1038 | post = (bool) reader_getInt (&s); |
920a3797 | 1039 | advanceField(&s); |
28bf4b0b | 1040 | ar = (arithType) reader_getInt (&s); |
920a3797 | 1041 | |
1042 | s = fgets(os, MAX_DUMP_LINE_LENGTH, f); | |
1043 | ||
28bf4b0b | 1044 | reader_checkChar (&s, 'l'); |
920a3797 | 1045 | |
1046 | lexpr = constraintExpr_undump (f); | |
1047 | ||
1048 | s = fgets(os, MAX_DUMP_LINE_LENGTH, f); | |
1049 | ||
28bf4b0b | 1050 | reader_checkChar (&s, 'r'); |
920a3797 | 1051 | expr = constraintExpr_undump (f); |
1052 | ||
1053 | c = constraint_makeNew(); | |
1054 | ||
1055 | c->fcnPre = fcnPre; | |
1056 | c->post = post; | |
1057 | c->ar = ar; | |
1058 | ||
1059 | c->lexpr = lexpr; | |
1060 | c->expr = expr; | |
1061 | ||
1062 | free(os); | |
1063 | return c; | |
1064 | } | |
1065 | ||
1066 | ||
1067 | void constraint_dump (/*@observer@*/ constraint c, FILE *f) | |
1068 | { | |
1069 | bool fcnPre; | |
1070 | bool post; | |
1071 | arithType ar; | |
1072 | ||
1073 | constraintExpr lexpr; | |
1074 | constraintExpr expr; | |
28bf4b0b | 1075 | |
920a3797 | 1076 | |
1077 | fcnPre = c->fcnPre; | |
1078 | post = c->post; | |
1079 | ar = c->ar; | |
1080 | lexpr = c->lexpr; | |
1081 | expr = c->expr; | |
1082 | ||
1083 | fprintf(f, "%d@%d@%d\n", (int) fcnPre, (int) post, (int) ar); | |
1084 | fprintf(f,"l\n"); | |
1085 | constraintExpr_dump (lexpr, f); | |
1086 | fprintf(f,"r\n"); | |
1087 | constraintExpr_dump (expr, f); | |
1088 | } | |
1089 | ||
1090 |