]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | ** constraint.c | |
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" | |
14 | # include "exprNodeSList.h" | |
15 | ||
16 | /*@i33*/ | |
17 | ||
18 | /*@access exprNode @*/ | |
19 | ||
20 | ||
21 | static /*@only@*/ cstring constraint_printDetailedPostCondition (/*@observer@*/ /*@temp@*/ constraint p_c); | |
22 | ||
23 | ||
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 @*/; | |
27 | ||
28 | /* constraint makeConstraintParse (sRef x, lltok relOp, exprNode cconstant) */ | |
29 | ||
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; */ | |
38 | ||
39 | ||
40 | /* ret->lexpr = constraintExpr_makeTermsRef (x); */ | |
41 | /* #warning fix abstraction */ | |
42 | ||
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") ); */ | |
51 | ||
52 | ||
53 | /* t = cstring_toCharsSafe (exprNode_unparse(cconstant)); */ | |
54 | /* c = atoi( t ); */ | |
55 | /* ret->expr = constraintExpr_makeIntLiteral (c); */ | |
56 | ||
57 | /* ret->post = TRUE; */ | |
58 | /* // ret->orig = ret; */ | |
59 | /* DPRINTF(("GENERATED CONSTRAINT:")); */ | |
60 | /* DPRINTF( (message ("%s", constraint_print(ret) ) ) ); */ | |
61 | /* return ret; */ | |
62 | /* } */ | |
63 | ||
64 | ||
65 | static void | |
66 | advanceField (char **s) | |
67 | { | |
68 | reader_checkChar (s, '@'); | |
69 | } | |
70 | ||
71 | ||
72 | ||
73 | static constraint makeConstraintParse2 (constraintExpr l, lltok relOp, exprNode cconstant) | |
74 | { | |
75 | char *t; | |
76 | int c; | |
77 | constraint ret; | |
78 | ret = constraint_makeNew (); | |
79 | llassert (constraintExpr_isDefined(l) ); | |
80 | ||
81 | ret->lexpr = constraintExpr_copy (l); | |
82 | ||
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 | |
91 | llfatalbug(message("Unsupported relational operator") ); | |
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 | ||
105 | bool constraint_same (constraint c1, constraint c2) | |
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 | } | |
119 | ||
120 | constraint makeConstraintParse3 (constraintExpr l, lltok relOp, constraintExpr r) | |
121 | { | |
122 | constraint ret; | |
123 | ret = constraint_makeNew(); | |
124 | llassert (constraintExpr_isDefined(l) ); | |
125 | ||
126 | ret->lexpr = constraintExpr_copy (l); | |
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 | |
135 | llfatalbug( message("Unsupported relational operator") ); | |
136 | ||
137 | ret->expr = constraintExpr_copy (r); | |
138 | ||
139 | ret->post = TRUE; | |
140 | ||
141 | ret->orig = constraint_copy(ret); | |
142 | ||
143 | ret = constraint_simplify (ret); | |
144 | ||
145 | // ret->orig = ret; | |
146 | DPRINTF(("GENERATED CONSTRAINT:")); | |
147 | DPRINTF( (message ("%s", constraint_print(ret) ) ) ); | |
148 | return ret; | |
149 | } | |
150 | ||
151 | constraint constraint_copy (/*@temp@*/ /*@observer@*/ constraint c) | |
152 | { | |
153 | constraint ret; | |
154 | ||
155 | llassert (constraint_isDefined(c) ); | |
156 | // DPRINTF((message("Copying constraint %q", constraint_print) )); | |
157 | ||
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; | |
163 | /*@-assignexpose@*/ | |
164 | ret->generatingExpr = c->generatingExpr; | |
165 | /*@=assignexpose@*/ | |
166 | ||
167 | /*@i33 fix this*/ | |
168 | if (c->orig != NULL) | |
169 | ret->orig = constraint_copy (c->orig); | |
170 | else | |
171 | ret->orig = NULL; | |
172 | ||
173 | if (c->or != NULL) | |
174 | ret->or = constraint_copy (c->or); | |
175 | else | |
176 | ret->or = NULL; | |
177 | ||
178 | ret->fcnPre = c->fcnPre; | |
179 | ||
180 | return ret; | |
181 | } | |
182 | ||
183 | /*like copy expect it doesn't allocate memory for the constraint*/ | |
184 | ||
185 | void constraint_overWrite (constraint c1, constraint c2) | |
186 | { | |
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 | ||
197 | c1->lexpr = constraintExpr_copy (c2->lexpr); | |
198 | c1->ar = c2->ar; | |
199 | c1->expr = constraintExpr_copy (c2->expr); | |
200 | c1->post = c2->post; | |
201 | ||
202 | if (c1->orig != NULL) | |
203 | constraint_free (c1->orig); | |
204 | ||
205 | if (c2->orig != NULL) | |
206 | c1->orig = constraint_copy (c2->orig); | |
207 | else | |
208 | c1->orig = NULL; | |
209 | ||
210 | /*@i33 make sure that the or is freed correctly*/ | |
211 | if (c1->or != NULL) | |
212 | constraint_free (c1->or); | |
213 | ||
214 | if (c2->or != NULL) | |
215 | c1->or = constraint_copy (c2->or); | |
216 | else | |
217 | c1->or = NULL; | |
218 | ||
219 | c1->fcnPre = c2->fcnPre; | |
220 | ||
221 | /*@-assignexpose@*/ | |
222 | c1->generatingExpr = c2->generatingExpr; | |
223 | /*@=assignexpose@*/ | |
224 | } | |
225 | ||
226 | ||
227 | ||
228 | static /*@notnull@*/ /*@special@*/ constraint constraint_makeNew (void) | |
229 | /*@post:isnull result->or, result->orig, result->generatingExpr, result->fcnPre @*/ | |
230 | /*@defines result->or, result->generatingExpr, result->orig, result->fcnPre @*/ | |
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; | |
239 | ret->or = NULL; | |
240 | ret->generatingExpr = NULL; | |
241 | ret->fcnPre = NULL; | |
242 | return ret; | |
243 | } | |
244 | ||
245 | constraint constraint_addGeneratingExpr (/*@returned@*/ constraint c, /*@exposed@*/ exprNode e) | |
246 | { | |
247 | ||
248 | if (c->generatingExpr == NULL) | |
249 | { | |
250 | c->generatingExpr = e; | |
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 | ||
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 | ||
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; | |
284 | // DPRINTF(( message("Warning Setting fcnPre directly") )); | |
285 | } | |
286 | return c; | |
287 | } | |
288 | ||
289 | ||
290 | ||
291 | ||
292 | fileloc constraint_getFileloc (constraint c) | |
293 | { | |
294 | if (exprNode_isDefined(c->generatingExpr) ) | |
295 | return (fileloc_copy (exprNode_getfileloc (c->generatingExpr) ) ); | |
296 | ||
297 | return (constraintExpr_getFileloc (c->lexpr) ); | |
298 | ||
299 | ||
300 | } | |
301 | ||
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 | { | |
312 | if (c->orig != NULL) | |
313 | { | |
314 | if (checkForMaxSet(c->orig) ) | |
315 | return TRUE; | |
316 | } | |
317 | ||
318 | return (checkForMaxSet(c) ); | |
319 | } | |
320 | ||
321 | constraint constraint_makeReadSafeExprNode ( exprNode po, exprNode ind) | |
322 | { | |
323 | constraint ret = constraint_makeNew(); | |
324 | // constraintTerm term; | |
325 | po = po; | |
326 | ind = ind; | |
327 | ret->lexpr = constraintExpr_makeMaxReadExpr(po); | |
328 | ret->ar = GTE; | |
329 | ret->expr = constraintExpr_makeValueExpr (ind); | |
330 | ret->post = FALSE; | |
331 | return ret; | |
332 | } | |
333 | ||
334 | constraint constraint_makeWriteSafeInt ( exprNode po, int ind) | |
335 | { | |
336 | constraint ret = constraint_makeNew(); | |
337 | ||
338 | ||
339 | ret->lexpr =constraintExpr_makeMaxSetExpr(po); | |
340 | ret->ar = GTE; | |
341 | ret->expr = constraintExpr_makeIntLiteral (ind); | |
342 | /*@i1*/return ret; | |
343 | } | |
344 | ||
345 | constraint constraint_makeSRefSetBufferSize (sRef s, long int size) | |
346 | { | |
347 | constraint ret = constraint_makeNew(); | |
348 | ret->lexpr = constraintExpr_makeSRefMaxset (s); | |
349 | ret->ar = EQ; | |
350 | ret->expr = constraintExpr_makeIntLiteral ((int)size); | |
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 | ||
360 | ret->lexpr = constraintExpr_makeSRefMaxset ( s ); | |
361 | ret->ar = GTE; | |
362 | ret->expr = constraintExpr_makeIntLiteral (ind); | |
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 | ||
394 | constraint constraint_makeReadSafeInt ( exprNode t1, int index) | |
395 | { | |
396 | constraint ret = constraint_makeNew(); | |
397 | ||
398 | ret->lexpr = constraintExpr_makeMaxReadExpr(t1); | |
399 | ret->ar = GTE; | |
400 | ret->expr = constraintExpr_makeIntLiteral (index); | |
401 | ret->post = FALSE; | |
402 | return ret; | |
403 | } | |
404 | ||
405 | constraint constraint_makeSRefReadSafeInt (sRef s, int ind) | |
406 | { | |
407 | constraint ret = constraint_makeNew(); | |
408 | ||
409 | ||
410 | ret->lexpr = constraintExpr_makeSRefMaxRead (s ); | |
411 | ret->ar = GTE; | |
412 | ret->expr = constraintExpr_makeIntLiteral (ind); | |
413 | ret->post = TRUE; | |
414 | /*@i1*/return ret; | |
415 | } | |
416 | ||
417 | constraint constraint_makeEnsureMaxReadAtLeast (exprNode t1, exprNode t2, fileloc sequencePoint) | |
418 | { | |
419 | constraint ret; | |
420 | ||
421 | ret = constraint_makeReadSafeExprNode(t1, t2); | |
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 | ||
431 | static constraint constraint_makeEnsuresOpConstraintExpr (/*@only@*/ constraintExpr c1, /*@only@*/ constraintExpr c2, fileloc sequencePoint, arithType ar) | |
432 | { | |
433 | ||
434 | constraint ret; | |
435 | ||
436 | llassert(constraintExpr_isDefined(c1) && constraintExpr_isDefined(c2) ); | |
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 | } | |
448 | ||
449 | static constraint constraint_makeEnsuresOp (/*@dependent@*/ exprNode e1, /*@dependent@*/ exprNode e2, fileloc sequencePoint, arithType ar) | |
450 | { | |
451 | constraintExpr c1, c2; | |
452 | constraint ret; | |
453 | exprNode e; | |
454 | ||
455 | if (! (exprNode_isDefined(e1) && exprNode_isDefined(e2) ) ) | |
456 | { | |
457 | llcontbug((message("null exprNode, Exprnodes are %s and %s", | |
458 | exprNode_unparse(e1), exprNode_unparse(e2) ) | |
459 | )); | |
460 | } | |
461 | ||
462 | // llassert (sequencePoint); | |
463 | ||
464 | e = e1; | |
465 | c1 = constraintExpr_makeValueExpr (e); | |
466 | ||
467 | e = e2; | |
468 | c2 = constraintExpr_makeValueExpr (e); | |
469 | ||
470 | ret = constraint_makeEnsuresOpConstraintExpr (c1, c2, sequencePoint, ar); | |
471 | ||
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 | { | |
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) ); | |
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 | { | |
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) ); | |
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 | { | |
526 | constraintList_free(dst->ensuresConstraints); | |
527 | constraintList_free(dst->requiresConstraints); | |
528 | constraintList_free(dst->trueEnsuresConstraints); | |
529 | constraintList_free(dst->falseEnsuresConstraints); | |
530 | ||
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 | ||
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 | ||
583 | constraint constraint_makeMaxSetSideEffectPostDecrement (exprNode e, fileloc sequencePoint) | |
584 | { | |
585 | constraint ret = constraint_makeNew(); | |
586 | //constraintTerm term; | |
587 | ||
588 | // e = exprNode_fakeCopy(e); | |
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 | ||
605 | // e = exprNode_fakeCopy(e); | |
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 | ||
619 | void constraint_free (/*@only@*/ constraint c) | |
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 | ||
629 | ||
630 | constraintExpr_free(c->lexpr); | |
631 | constraintExpr_free(c->expr); | |
632 | ||
633 | c->orig = NULL; | |
634 | c->or = NULL; | |
635 | c->lexpr = NULL; | |
636 | c->expr = NULL; | |
637 | ||
638 | free (c); | |
639 | ||
640 | } | |
641 | ||
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 | ||
658 | cstring arithType_print (arithType ar) /*@*/ | |
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 | ||
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 | ||
719 | void constraint_printError (constraint c, fileloc loc) | |
720 | { | |
721 | cstring string; | |
722 | fileloc errorLoc, temp; | |
723 | ||
724 | string = constraint_printDetailed (c); | |
725 | ||
726 | errorLoc = loc; | |
727 | ||
728 | loc = NULL; | |
729 | ||
730 | temp = constraint_getFileloc(c); | |
731 | ||
732 | if (fileloc_isDefined(temp) ) | |
733 | { | |
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); | |
745 | errorLoc = NULL; | |
746 | } | |
747 | else | |
748 | { | |
749 | if (c->post) | |
750 | { | |
751 | voptgenerror (FLG_FUNCTIONPOST, string, errorLoc); | |
752 | } | |
753 | else | |
754 | { | |
755 | voptgenerror (FLG_FUNCTIONCONSTRAINT, string, errorLoc); | |
756 | } | |
757 | errorLoc = NULL; | |
758 | } | |
759 | } | |
760 | ||
761 | ||
762 | static cstring constraint_printDeep (constraint c) | |
763 | { | |
764 | cstring genExpr; | |
765 | cstring st = cstring_undefined; | |
766 | ||
767 | st = constraint_print(c); | |
768 | ||
769 | ||
770 | if (c->orig != constraint_undefined) | |
771 | { | |
772 | genExpr = exprNode_unparse(c->orig->generatingExpr); | |
773 | if (!c->post) | |
774 | { | |
775 | if (c->orig->fcnPre) | |
776 | st = cstring_concatFree(st, (message(" derived from %s precondition: %q", genExpr, constraint_printDeep(c->orig) ) | |
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 | ||
796 | ||
797 | static /*@only@*/ cstring constraint_printDetailedPostCondition (/*@observer@*/ /*@temp@*/ constraint c) | |
798 | { | |
799 | cstring st = cstring_undefined; | |
800 | cstring genExpr; | |
801 | ||
802 | st = message ("Unsatisfied ensures constraint condition:\nLCLint is unable to verify the constraint %q", constraint_printDeep (c) ); | |
803 | ||
804 | genExpr = exprNode_unparse (c->generatingExpr); | |
805 | ||
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) ), | |
811 | genExpr ); | |
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 | ||
823 | cstring constraint_printDetailed (constraint c) | |
824 | { | |
825 | cstring st = cstring_undefined; | |
826 | cstring genExpr; | |
827 | ||
828 | if (!c->post) | |
829 | { | |
830 | st = message ("Unresolved constraint:\nLclint is unable to resolve %q", constraint_printDeep (c) ); | |
831 | } | |
832 | else | |
833 | { | |
834 | st = message ("Block Post condition:\nThis function block has the post condition %q", constraint_printDeep (c) ); | |
835 | } | |
836 | ||
837 | genExpr = exprNode_unparse (c->generatingExpr); | |
838 | ||
839 | if (context_getFlag (FLG_CONSTRAINTLOCATION) ) | |
840 | { | |
841 | cstring temp; | |
842 | // llassert (c->generatingExpr); | |
843 | temp = message ("\nOriginal Generating expression %q: %s\n", fileloc_unparse( exprNode_getfileloc (c->generatingExpr) ), | |
844 | genExpr ); | |
845 | st = cstring_concatFree (st, temp); | |
846 | ||
847 | if (constraint_hasMaxSet(c) ) | |
848 | { | |
849 | temp = message ("Has MaxSet\n"); | |
850 | st = cstring_concatFree (st, temp); | |
851 | } | |
852 | } | |
853 | return st; | |
854 | } | |
855 | ||
856 | /*@only@*/ cstring constraint_print (constraint c) /*@*/ | |
857 | { | |
858 | cstring st = cstring_undefined; | |
859 | cstring type = cstring_undefined; | |
860 | llassert (c !=NULL); | |
861 | if (c->post) | |
862 | { | |
863 | type = cstring_makeLiteral ("Ensures: "); | |
864 | } | |
865 | else | |
866 | { | |
867 | type = cstring_makeLiteral ("Requires: "); | |
868 | } | |
869 | st = message ("%q: %q %q %q", | |
870 | type, | |
871 | constraintExpr_print (c->lexpr), | |
872 | arithType_print(c->ar), | |
873 | constraintExpr_print(c->expr) | |
874 | ); | |
875 | return st; | |
876 | } | |
877 | ||
878 | cstring constraint_printOr (constraint c) /*@*/ | |
879 | { | |
880 | cstring ret; | |
881 | constraint temp; | |
882 | ||
883 | ret = cstring_undefined; | |
884 | temp = c; | |
885 | ||
886 | ret = cstring_concatFree (ret, constraint_print(temp) ); | |
887 | ||
888 | temp = temp->or; | |
889 | ||
890 | while ( constraint_isDefined(temp) ) | |
891 | { | |
892 | ret = cstring_concatFree (ret, cstring_makeLiteral (" OR ") ); | |
893 | ret = cstring_concatFree (ret, constraint_print(temp) ); | |
894 | temp = temp->or; | |
895 | } | |
896 | ||
897 | return ret; | |
898 | ||
899 | } | |
900 | ||
901 | /*@only@*/ constraint constraint_doSRefFixBaseParam (/*@returned@*/ /*@only@*/ constraint precondition, | |
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 | ||
913 | constraint constraint_doFixResult (constraint postcondition, /*@dependent@*/ exprNode fcnCall) | |
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 | ||
922 | /*@only@*/ constraint constraint_doSRefFixConstraintParam (constraint precondition, | |
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 | ||
930 | precondition->fcnPre = FALSE; | |
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 | ||
947 | constraint constraint_preserveOrig (/*@returned@*/ constraint c) /*@modifies c @*/ | |
948 | { | |
949 | ||
950 | DPRINTF( (message("Doing constraint_preserverOrig for %q ", constraint_printDetailed(c) ) )); | |
951 | ||
952 | if (c->orig == constraint_undefined) | |
953 | c->orig = constraint_copy (c); | |
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) | |
965 | { | |
966 | c->orig->orig = temp; | |
967 | temp = NULL; | |
968 | } | |
969 | else | |
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 | } | |
976 | } | |
977 | else | |
978 | { | |
979 | DPRINTF( (message("Not changing constraint") )); | |
980 | } | |
981 | ||
982 | DPRINTF( (message("After Doing constraint_preserverOrig for %q ", constraint_printDetailed(c) ) )); | |
983 | ||
984 | return c; | |
985 | } | |
986 | /*@=fcnuse*/ | |
987 | /*@=assignexpose*/ | |
988 | /*@=czechfcns@*/ | |
989 | ||
990 | ||
991 | constraint constraint_togglePost (/*@returned@*/ constraint c) | |
992 | { | |
993 | c->post = !c->post; | |
994 | return c; | |
995 | } | |
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 | } | |
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; | |
1022 | ||
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 | ||
1036 | fcnPre = (bool) reader_getInt (&s); | |
1037 | advanceField(&s); | |
1038 | post = (bool) reader_getInt (&s); | |
1039 | advanceField(&s); | |
1040 | ar = (arithType) reader_getInt (&s); | |
1041 | ||
1042 | s = fgets(os, MAX_DUMP_LINE_LENGTH, f); | |
1043 | ||
1044 | reader_checkChar (&s, 'l'); | |
1045 | ||
1046 | lexpr = constraintExpr_undump (f); | |
1047 | ||
1048 | s = fgets(os, MAX_DUMP_LINE_LENGTH, f); | |
1049 | ||
1050 | reader_checkChar (&s, 'r'); | |
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; | |
1075 | ||
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 |