]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
2 | ** constraintExpr.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" | |
616915dd | 14 | # include "exprNodeSList.h" |
15 | ||
616915dd | 16 | /*@-czechfcns@*/ |
17 | ||
18 | //#include "constraintExpr.h" | |
19 | ||
28bf4b0b | 20 | /*@access exprNode @*/ |
a8e557d3 | 21 | |
28bf4b0b | 22 | /*@unused@*/ static bool constraintTerm_same (constraintTerm p_term1, constraintTerm p_term2) ; |
2934b455 | 23 | |
d46ce6a4 | 24 | void constraintTerm_free (/*@only@*/ constraintTerm term) |
25 | { | |
26 | llassert(constraintTerm_isDefined(term) ); | |
27 | fileloc_free (term->loc); | |
28 | ||
29 | switch (term->kind) | |
30 | { | |
31 | case EXPRNODE: | |
32 | /* we don't free an exprNode*/ | |
33 | break; | |
34 | case SREF: | |
35 | /* sref */ | |
795e7f34 | 36 | sRef_free (term->value.sref); |
d46ce6a4 | 37 | break; |
38 | case INTLITERAL: | |
39 | /* don't free an int */ | |
40 | break; | |
41 | case ERRORBADCONSTRAINTTERMTYPE: | |
42 | default: | |
43 | /* type was set incorrectly */ | |
44 | llcontbug (message("constraintTerm_free type was set incorrectly")); | |
45 | } | |
795e7f34 | 46 | // term->value.intlit = 0; |
47 | term->kind = ERRORBADCONSTRAINTTERMTYPE; | |
d46ce6a4 | 48 | free (term); |
49 | } | |
616915dd | 50 | |
4ab867d6 | 51 | /*@only@*/ static/*@out@*/ constraintTerm new_constraintTermExpr (void) |
616915dd | 52 | { |
53 | constraintTerm ret; | |
54 | ret = dmalloc (sizeof (* ret ) ); | |
bb25bea6 | 55 | ret->value.intlit = 0; |
616915dd | 56 | return ret; |
57 | } | |
58 | ||
59 | ||
60 | bool constraintTerm_isIntLiteral (constraintTerm term) | |
61 | { | |
dc92450f | 62 | llassert(term != NULL); |
616915dd | 63 | |
64 | if (term->kind == INTLITERAL) | |
65 | return TRUE; | |
66 | ||
67 | return FALSE; | |
68 | } | |
69 | ||
dc92450f | 70 | bool constraintTerm_isStringLiteral (constraintTerm c) /*@*/ |
616915dd | 71 | { |
dc92450f | 72 | llassert (c != NULL); |
616915dd | 73 | if (c->kind == EXPRNODE) |
74 | { | |
75 | if (exprNode_knownStringValue(c->value.expr) ) | |
76 | { | |
77 | return TRUE; | |
78 | } | |
79 | } | |
80 | return FALSE; | |
81 | } | |
82 | ||
83 | cstring constraintTerm_getStringLiteral (constraintTerm c) | |
84 | { | |
dc92450f | 85 | llassert (c != NULL); |
616915dd | 86 | llassert (constraintTerm_isStringLiteral (c) ); |
87 | llassert (c->kind == EXPRNODE); | |
88 | ||
dc92450f | 89 | return (cstring_copy ( multiVal_forceString (exprNode_getValue (c->value.expr) ) ) ); |
616915dd | 90 | } |
91 | ||
4ab867d6 | 92 | constraintTerm constraintTerm_simplify (/*@returned@*/ constraintTerm term) /*@modifies term@*/ |
616915dd | 93 | { |
94 | if (term->kind == EXPRNODE) | |
95 | { | |
96 | if ( exprNode_knownIntValue (term->value.expr ) ) | |
97 | { | |
98 | long int temp; | |
28bf4b0b | 99 | |
616915dd | 100 | temp = exprNode_getLongValue (term->value.expr); |
dc92450f | 101 | term->value.intlit = (int)temp; |
616915dd | 102 | term->kind = INTLITERAL; |
103 | } | |
104 | } | |
105 | return term; | |
106 | } | |
107 | ||
108 | fileloc constraintTerm_getFileloc (constraintTerm t) | |
109 | { | |
110 | return (fileloc_copy (t->loc) ); | |
111 | } | |
112 | ||
a8e557d3 | 113 | constraintTermType constraintTerm_getKind (constraintTerm t) |
114 | { | |
115 | llassert (constraintTerm_isDefined(t) ); | |
116 | ||
117 | return (t->kind); | |
118 | } | |
119 | ||
120 | /*@exposed@*/ sRef constraintTerm_getSRef (constraintTerm t) | |
121 | { | |
122 | llassert (constraintTerm_isDefined(t) ); | |
123 | llassert (t->kind == SREF); | |
124 | ||
125 | return (t->value.sref); | |
126 | } | |
127 | ||
28bf4b0b | 128 | /*@only@*/ constraintTerm constraintTerm_makeExprNode (/*@depenedent@*/ exprNode e) |
616915dd | 129 | { |
130 | constraintTerm ret = new_constraintTermExpr(); | |
d46ce6a4 | 131 | ret->loc = fileloc_copy(exprNode_getfileloc(e)); |
616915dd | 132 | ret->value.expr = e; |
133 | ret->kind = EXPRNODE; | |
134 | ret = constraintTerm_simplify(ret); | |
135 | return ret; | |
136 | } | |
137 | ||
28bf4b0b | 138 | /*@only@*/ constraintTerm constraintTerm_makesRef (/*@temp@*/ /*@observer@*/ sRef s) |
616915dd | 139 | { |
140 | constraintTerm ret = new_constraintTermExpr(); | |
141 | ret->loc = fileloc_undefined; | |
4ab867d6 | 142 | ret->value.sref = sRef_saveCopy(s); |
616915dd | 143 | ret->kind = SREF; |
144 | ret = constraintTerm_simplify(ret); | |
145 | return ret; | |
146 | } | |
147 | ||
795e7f34 | 148 | |
149 | ||
616915dd | 150 | constraintTerm constraintTerm_copy (constraintTerm term) |
151 | { | |
152 | constraintTerm ret; | |
153 | ret = new_constraintTermExpr(); | |
154 | ret->loc = fileloc_copy (term->loc); | |
795e7f34 | 155 | |
156 | switch (term->kind) | |
157 | { | |
158 | case EXPRNODE: | |
159 | ret->value.expr = term->value.expr; | |
160 | break; | |
161 | case INTLITERAL: | |
162 | ret->value.intlit = term->value.intlit; | |
163 | break; | |
164 | ||
165 | case SREF: | |
166 | ret->value.sref = sRef_saveCopy(term->value.sref); | |
167 | break; | |
168 | default: | |
169 | BADEXIT; | |
170 | } | |
616915dd | 171 | ret->kind = term->kind; |
172 | return ret; | |
173 | } | |
174 | ||
d46ce6a4 | 175 | constraintTerm constraintTerm_setFileloc (/*@returned@*/ constraintTerm term, fileloc loc) |
616915dd | 176 | { |
dc92450f | 177 | llassert(term != NULL); |
d46ce6a4 | 178 | |
28bf4b0b | 179 | if ( fileloc_isDefined( term->loc ) ) |
d46ce6a4 | 180 | fileloc_free(term->loc); |
181 | ||
616915dd | 182 | term->loc = fileloc_copy(loc); |
183 | return term; | |
184 | } | |
185 | ||
186 | ||
d46ce6a4 | 187 | static cstring constraintTerm_getName (constraintTerm term) |
616915dd | 188 | { |
189 | cstring s; | |
190 | s = cstring_undefined; | |
191 | ||
192 | llassert (term != NULL); | |
193 | ||
194 | switch (term->kind) | |
195 | { | |
196 | case EXPRNODE: | |
197 | /*@i334*/ //wtf | |
198 | s = message ("%s", exprNode_unparse (term->value.expr) ); | |
199 | break; | |
200 | case INTLITERAL: | |
201 | s = message (" %d ", term->value.intlit); | |
202 | break; | |
203 | ||
204 | case SREF: | |
d46ce6a4 | 205 | s = message ("%q", sRef_unparse (term->value.sref) ); |
616915dd | 206 | |
207 | break; | |
c3e695ff | 208 | default: |
209 | BADEXIT; | |
210 | /*@notreached@*/ | |
616915dd | 211 | break; |
212 | } | |
616915dd | 213 | |
c3e695ff | 214 | return s; |
616915dd | 215 | } |
216 | ||
217 | constraintTerm | |
d46ce6a4 | 218 | constraintTerm_doSRefFixBaseParam (/*@returned@*/constraintTerm term, exprNodeList arglist) /*@modifies term@*/ |
616915dd | 219 | { |
220 | llassert (term != NULL); | |
221 | ||
222 | switch (term->kind) | |
223 | { | |
224 | case EXPRNODE: | |
225 | /*@i334*/ //wtf | |
226 | // s = message ("%s @ %s ", exprNode_unparse (term->value.expr), | |
227 | // fileloc_unparse (term->loc) ); | |
228 | break; | |
229 | case INTLITERAL: | |
230 | // s = message (" %d ", term->value.intlit); | |
231 | break; | |
232 | ||
233 | case SREF: | |
234 | term->value.sref = sRef_fixBaseParam (term->value.sref, arglist); | |
235 | // s = message ("%s ", sRef_unparse (term->value.sref) ); | |
236 | ||
237 | break; | |
c3e695ff | 238 | default: |
239 | BADEXIT; | |
616915dd | 240 | } |
241 | return term; | |
242 | ||
243 | } | |
244 | ||
616915dd | 245 | cstring constraintTerm_print (constraintTerm term) /*@*/ |
246 | { | |
247 | cstring s; | |
248 | s = cstring_undefined; | |
249 | ||
250 | llassert (term != NULL); | |
251 | ||
252 | switch (term->kind) | |
253 | { | |
254 | case EXPRNODE: | |
255 | /*@i334*/ //wtf | |
d46ce6a4 | 256 | s = message ("%s @ %q ", exprNode_unparse (term->value.expr), |
616915dd | 257 | fileloc_unparse (term->loc) ); |
258 | break; | |
259 | case INTLITERAL: | |
260 | s = message (" %d ", term->value.intlit); | |
261 | break; | |
262 | ||
263 | case SREF: | |
d46ce6a4 | 264 | s = message ("%q ", sRef_unparseDebug (term->value.sref) ); |
616915dd | 265 | |
266 | break; | |
c3e695ff | 267 | default: |
268 | BADEXIT; | |
616915dd | 269 | } |
270 | ||
271 | return s; | |
272 | } | |
273 | ||
274 | ||
275 | constraintTerm constraintTerm_makeIntLiteral (int i) | |
276 | { | |
277 | constraintTerm ret = new_constraintTermExpr(); | |
278 | ret->value.intlit = i; | |
279 | ret->kind = INTLITERAL; | |
280 | ret->loc = fileloc_undefined; | |
281 | return ret; | |
282 | } | |
283 | ||
284 | bool constraintTerm_canGetValue (constraintTerm term) | |
285 | { | |
286 | if (term->kind == INTLITERAL) | |
287 | return TRUE; | |
288 | else | |
289 | return FALSE; | |
290 | } | |
291 | ||
292 | int constraintTerm_getValue (constraintTerm term) | |
293 | { | |
294 | llassert (term->kind == INTLITERAL); | |
295 | return term->value.intlit; | |
296 | } | |
297 | ||
616915dd | 298 | /* same and similar are similar but not the same*/ |
bb25bea6 | 299 | static bool constraintTerm_same (constraintTerm term1, constraintTerm term2) |
616915dd | 300 | { |
301 | llassert (term1 !=NULL && term2 !=NULL); | |
302 | ||
303 | if ( (term1->kind != term2->kind) || (term1->kind != EXPRNODE) ) | |
304 | { | |
305 | return FALSE; | |
306 | } | |
307 | ||
308 | DPRINTF ( (message | |
309 | ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2) | |
310 | ) | |
311 | ) | |
312 | ); | |
313 | ||
314 | if (sRef_same (term1->value.expr->sref, term2->value.expr->sref) ) | |
315 | { | |
316 | DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2) ) )); | |
317 | return TRUE; | |
318 | } | |
319 | else | |
320 | { | |
321 | DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2) ) )); | |
322 | return FALSE; | |
323 | } | |
324 | ||
325 | } | |
326 | ||
bb25bea6 | 327 | static /*@exposed@*/ sRef constraintTerm_getsRef (constraintTerm t) |
616915dd | 328 | { |
dc92450f | 329 | llassert (t != NULL); |
616915dd | 330 | if (t->kind == EXPRNODE) |
331 | { | |
c3e695ff | 332 | return exprNode_getSref(t->value.expr); |
616915dd | 333 | } |
334 | ||
335 | if (t->kind == SREF) | |
336 | { | |
c3e695ff | 337 | return t->value.sref; |
616915dd | 338 | } |
339 | ||
340 | return sRef_undefined; | |
341 | } | |
342 | ||
343 | bool constraintTerm_probSame (constraintTerm term1, constraintTerm term2) | |
344 | { | |
345 | cstring s1, s2; | |
346 | ||
347 | llassert (term1 !=NULL && term2 !=NULL); | |
348 | ||
349 | DPRINTF ( (message | |
350 | ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2) | |
351 | ) | |
352 | ) | |
353 | ); | |
354 | ||
355 | s1 = constraintTerm_getName (term1); | |
356 | s2 = constraintTerm_getName (term2); | |
357 | ||
358 | if (cstring_equal (s1, s2) ) | |
359 | { | |
d46ce6a4 | 360 | DPRINTF ((message (" %q and %q are same", s1, s2 ) ) ); |
616915dd | 361 | return TRUE; |
362 | } | |
363 | else | |
364 | { | |
d46ce6a4 | 365 | DPRINTF ((message (" %q and %q are not same", s1, s2 ) ) ); |
616915dd | 366 | return FALSE; |
367 | } | |
368 | } | |
369 | ||
370 | bool constraintTerm_similar (constraintTerm term1, constraintTerm term2) | |
371 | { | |
372 | sRef s1, s2; | |
373 | ||
374 | llassert (term1 !=NULL && term2 !=NULL); | |
90bc41f7 | 375 | |
376 | if ( (term1->kind == INTLITERAL) && (term2->kind == INTLITERAL) ) | |
377 | { | |
378 | int t1, t2; | |
379 | llassert (constraintTerm_canGetValue(term1) ); | |
380 | t1 = constraintTerm_getValue (term1); | |
381 | ||
382 | llassert (constraintTerm_canGetValue(term2) ); | |
383 | t2 = constraintTerm_getValue (term2); | |
384 | if (t1 == t2) | |
385 | return TRUE; | |
386 | ||
387 | return FALSE; | |
388 | } | |
389 | ||
616915dd | 390 | s1 = constraintTerm_getsRef (term1); |
391 | s2 = constraintTerm_getsRef (term2); | |
392 | ||
28bf4b0b | 393 | if ( ! (sRef_isValid(s1) && sRef_isValid(s2) ) ) |
616915dd | 394 | { |
395 | return FALSE; | |
396 | } | |
397 | ||
28bf4b0b | 398 | DPRINTF( (message |
616915dd | 399 | ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2) |
400 | ) | |
401 | ) | |
402 | ); | |
403 | ||
404 | if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) ) | |
405 | { | |
406 | DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2) ) )); | |
407 | return TRUE; | |
408 | } | |
409 | else | |
410 | { | |
411 | DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2) ) )); | |
412 | return FALSE; | |
413 | } | |
414 | ||
415 | } | |
920a3797 | 416 | |
417 | void constraintTerm_dump ( /*@observer@*/ constraintTerm t, FILE *f) | |
418 | { | |
419 | fileloc loc; | |
420 | constraintTermValue value; | |
421 | constraintTermType kind; | |
422 | uentry u; | |
423 | ||
424 | loc = t->loc; | |
425 | ||
426 | value = t->value; | |
427 | ||
428 | kind = t->kind; | |
429 | ||
430 | fprintf(f, "%d\n", (int) kind); | |
431 | ||
432 | switch (kind) | |
433 | { | |
434 | ||
435 | case EXPRNODE: | |
436 | u = exprNode_getUentry(t->value.expr); | |
28bf4b0b | 437 | fprintf(f, "%s\n", cstring_toCharsSafe( uentry_rawName (u) ) |
438 | ); | |
920a3797 | 439 | break; |
440 | ||
441 | case SREF: | |
442 | { | |
443 | sRef s; | |
444 | ||
445 | s = t->value.sref; | |
446 | ||
447 | if (sRef_isResult (s ) ) | |
448 | { | |
449 | fprintf(f, "Result\n"); | |
450 | } | |
451 | else if (sRef_isParam (s ) ) | |
452 | { | |
453 | int param; | |
454 | ctype ct; | |
455 | cstring ctString; | |
456 | ||
457 | ||
458 | ct = sRef_getType (s); | |
459 | param = sRef_getParam(s); | |
460 | ||
461 | ctString = ctype_dump(ct); | |
462 | ||
28bf4b0b | 463 | fprintf(f, "Param %s %d\n", cstring_toCharsSafe(ctString), (int) param ); |
920a3797 | 464 | cstring_free(ctString); |
465 | } | |
466 | else | |
467 | { | |
468 | u = sRef_getUentry(s); | |
28bf4b0b | 469 | fprintf(f, "%s\n", cstring_toCharsSafe(uentry_rawName (u) ) ); |
920a3797 | 470 | } |
471 | ||
472 | } | |
473 | break; | |
474 | ||
475 | case INTLITERAL: | |
476 | fprintf (f, "%d\n", t->value.intlit); | |
477 | break; | |
478 | ||
479 | default: | |
480 | BADEXIT; | |
481 | } | |
482 | ||
483 | } | |
484 | ||
485 | ||
486 | /*@only@*/ constraintTerm constraintTerm_undump ( FILE *f) | |
487 | { | |
920a3797 | 488 | constraintTermType kind; |
489 | constraintTerm ret; | |
490 | ||
491 | uentry ue; | |
492 | ||
493 | char * str; | |
494 | char * os; | |
495 | ||
496 | str = mstring_create (MAX_DUMP_LINE_LENGTH); | |
497 | os = str; | |
498 | str = fgets(os, MAX_DUMP_LINE_LENGTH, f); | |
499 | ||
28bf4b0b | 500 | kind = (constraintTermType) reader_getInt(&str); |
920a3797 | 501 | str = fgets(os, MAX_DUMP_LINE_LENGTH, f); |
502 | ||
503 | switch (kind) | |
504 | { | |
505 | ||
506 | case SREF: | |
507 | { | |
508 | sRef s; | |
509 | char * term; | |
28bf4b0b | 510 | term = reader_getWord(&str); |
920a3797 | 511 | |
512 | if (strcmp (term, "Result") == 0 ) | |
513 | { | |
514 | s = sRef_makeResult(); | |
515 | } | |
516 | else if (strcmp (term, "Param" ) == 0 ) | |
517 | { | |
518 | int param; | |
519 | char *str2, *ostr2; | |
520 | ||
521 | ctype t; | |
522 | ||
28bf4b0b | 523 | reader_checkChar(&str, ' '); |
524 | str2 = reader_getWord(&str); | |
525 | param = reader_getInt(&str); | |
920a3797 | 526 | |
527 | ostr2 = str2; | |
528 | t = ctype_undump(&str2) ; | |
529 | s = sRef_makeParam (param, t ); | |
530 | free (ostr2); | |
531 | } | |
532 | else //This must be an identified that we can search for | |
533 | // in usymTab | |
534 | { | |
28bf4b0b | 535 | cstring termStr = cstring_makeLiteralTemp(term); |
536 | ||
537 | ue = usymtab_lookup (termStr); | |
920a3797 | 538 | s = uentry_getSref(ue); |
539 | } | |
540 | ||
541 | ret = constraintTerm_makesRef(s); | |
542 | ||
543 | free(term); | |
544 | } | |
545 | break; | |
546 | ||
547 | case EXPRNODE: | |
548 | { | |
549 | sRef s; | |
550 | char * term; | |
28bf4b0b | 551 | cstring termStr; |
920a3797 | 552 | |
28bf4b0b | 553 | term = reader_getWord(&str); |
920a3797 | 554 | //This must be an identifier that we can search for |
555 | // in usymTab | |
28bf4b0b | 556 | termStr = cstring_makeLiteralTemp(term); |
920a3797 | 557 | |
28bf4b0b | 558 | ue = usymtab_lookup (termStr); |
920a3797 | 559 | s = uentry_getSref(ue); |
560 | ret = constraintTerm_makesRef(s); | |
561 | ||
562 | free (term); | |
563 | } | |
564 | break; | |
565 | ||
566 | ||
567 | case INTLITERAL: | |
568 | { | |
569 | int i; | |
570 | ||
28bf4b0b | 571 | i = reader_getInt(&str); |
920a3797 | 572 | ret = constraintTerm_makeIntLiteral (i); |
573 | } | |
574 | break; | |
575 | ||
576 | default: | |
577 | BADEXIT; | |
578 | } | |
579 | free (os); | |
580 | ||
581 | return ret; | |
582 | } | |
583 | ||
584 | ||
585 | ||
586 |