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