7 # include <ctype.h> /* for isdigit */
8 # include "lclintMacros.nf"
10 # include "cgrammar.h"
11 # include "cgrammar_tokens.h"
13 # include "exprChecks.h"
14 # include "exprNodeSList.h"
18 //#include "constraintExpr.h"
20 /*@access exprNode @*/
22 bool constraintTerm_isDefined (constraintTerm t)
27 /*@unused@*/ static bool constraintTerm_same (constraintTerm p_term1, constraintTerm p_term2) ;
29 void constraintTerm_free (/*@only@*/ constraintTerm term)
31 llassert (constraintTerm_isDefined (term));
33 fileloc_free (term->loc);
38 /* we don't free an exprNode*/
42 sRef_free (term->value.sref);
45 /* don't free an int */
47 case ERRORBADCONSTRAINTTERMTYPE:
49 /* type was set incorrectly */
50 llcontbug (message("constraintTerm_free type was set incorrectly"));
52 // term->value.intlit = 0;
53 term->kind = ERRORBADCONSTRAINTTERMTYPE;
57 /*@only@*/ static/*@out@*/ constraintTerm new_constraintTermExpr (void)
60 ret = dmalloc (sizeof (* ret ) );
61 ret->value.intlit = 0;
66 bool constraintTerm_isIntLiteral (constraintTerm term)
68 llassert(term != NULL);
70 if (term->kind == INTLITERAL)
77 bool constraintTerm_isInitBlock (/*@observer@*/ /*@temp@*/ constraintTerm c) /*@*/
80 if (c->kind == EXPRNODE)
83 if (exprNode_isInitBlock(c->value.expr) )
92 int constraintTerm_getInitBlockLength (/*@observer@*/ /*@temp@*/ constraintTerm c) /*@*/
98 llassert (constraintTerm_isInitBlock (c) );
99 llassert (c->kind == EXPRNODE);
101 llassert(exprNode_isDefined(c->value.expr) );
103 if (exprNode_isUndefined(c->value.expr) )
108 if (c->value.expr->edata == exprData_undefined)
112 list = exprData_getArgs(c->value.expr->edata);
114 ret = exprNodeList_size(list);
121 bool constraintTerm_isStringLiteral (constraintTerm c) /*@*/
123 llassert (c != NULL);
124 if (c->kind == EXPRNODE)
126 if (exprNode_knownStringValue(c->value.expr) )
136 cstring constraintTerm_getStringLiteral (constraintTerm c)
138 llassert (c != NULL);
139 llassert (constraintTerm_isStringLiteral (c) );
140 llassert (c->kind == EXPRNODE);
142 return (cstring_copy ( multiVal_forceString (exprNode_getValue (c->value.expr) ) ) );
145 constraintTerm constraintTerm_simplify (/*@returned@*/ constraintTerm term) /*@modifies term@*/
147 if (term->kind == EXPRNODE)
149 if ( exprNode_knownIntValue (term->value.expr ) )
153 temp = exprNode_getLongValue (term->value.expr);
154 term->value.intlit = (int)temp;
155 term->kind = INTLITERAL;
161 fileloc constraintTerm_getFileloc (constraintTerm t)
163 llassert (constraintTerm_isDefined (t));
164 return (fileloc_copy (t->loc) );
167 constraintTermType constraintTerm_getKind (constraintTerm t)
169 llassert (constraintTerm_isDefined(t) );
174 /*@exposed@*/ sRef constraintTerm_getSRef (constraintTerm t)
176 llassert (constraintTerm_isDefined(t) );
177 llassert (t->kind == SREF);
179 return (t->value.sref);
182 /*@only@*/ constraintTerm constraintTerm_makeExprNode (/*@dependent@*/ exprNode e)
184 constraintTerm ret = new_constraintTermExpr();
185 ret->loc = fileloc_copy(exprNode_getfileloc(e));
187 ret->kind = EXPRNODE;
188 ret = constraintTerm_simplify(ret);
192 /*@only@*/ constraintTerm constraintTerm_makesRef (/*@temp@*/ /*@observer@*/ sRef s)
194 constraintTerm ret = new_constraintTermExpr();
195 ret->loc = fileloc_undefined;
196 ret->value.sref = sRef_saveCopy(s);
198 ret = constraintTerm_simplify(ret);
204 constraintTerm constraintTerm_copy (constraintTerm term)
207 ret = new_constraintTermExpr();
208 ret->loc = fileloc_copy (term->loc);
213 ret->value.expr = term->value.expr;
216 ret->value.intlit = term->value.intlit;
220 ret->value.sref = sRef_saveCopy(term->value.sref);
225 ret->kind = term->kind;
229 constraintTerm constraintTerm_setFileloc (/*@returned@*/ constraintTerm term, fileloc loc)
231 llassert(term != NULL);
233 if ( fileloc_isDefined( term->loc ) )
234 fileloc_free(term->loc);
236 term->loc = fileloc_copy(loc);
241 static cstring constraintTerm_getName (constraintTerm term)
244 s = cstring_undefined;
246 llassert (term != NULL);
252 s = message ("%s", exprNode_unparse (term->value.expr) );
255 s = message (" %d ", (int) term->value.intlit);
259 s = message ("%q", sRef_unparse (term->value.sref) );
272 constraintTerm_doSRefFixBaseParam (/*@returned@*/constraintTerm term, exprNodeList arglist) /*@modifies term@*/
274 llassert (term != NULL);
280 // s = message ("%s @ %s ", exprNode_unparse (term->value.expr),
281 // fileloc_unparse (term->loc) );
284 // s = message (" %d ", term->value.intlit);
288 term->value.sref = sRef_fixBaseParam (term->value.sref, arglist);
289 // s = message ("%s ", sRef_unparse (term->value.sref) );
299 cstring constraintTerm_print (constraintTerm term) /*@*/
302 s = cstring_undefined;
304 llassert (term != NULL);
310 s = message ("%s @ %q ", exprNode_unparse (term->value.expr),
311 fileloc_unparse (term->loc) );
314 s = message (" %d ", (int)term->value.intlit);
318 s = message ("%q ", sRef_unparseDebug (term->value.sref) );
329 constraintTerm constraintTerm_makeIntLiteral (long i)
331 constraintTerm ret = new_constraintTermExpr();
332 ret->value.intlit = i;
333 ret->kind = INTLITERAL;
334 ret->loc = fileloc_undefined;
338 bool constraintTerm_canGetValue (constraintTerm term)
340 if (term->kind == INTLITERAL)
344 else if (term->kind == SREF)
346 if (sRef_hasValue (term->value.sref))
348 multiVal mval = sRef_getValue (term->value.sref);
350 return multiVal_isInt (mval); /* for now, only try to deal with int values */
357 else if (term->kind == EXPRNODE)
367 long constraintTerm_getValue (constraintTerm term)
369 llassert (constraintTerm_canGetValue (term));
371 if (term->kind == INTLITERAL)
373 return term->value.intlit;
375 else if (term->kind == SREF)
377 if (sRef_hasValue (term->value.sref))
379 multiVal mval = sRef_getValue (term->value.sref);
381 return multiVal_forceInt (mval); /* for now, only try to deal with int values */
388 else if (term->kind == EXPRNODE)
400 /* same and similar are similar but not the same*/
401 static bool constraintTerm_same (constraintTerm term1, constraintTerm term2)
403 llassert (term1 !=NULL && term2 !=NULL);
405 if ( (term1->kind != term2->kind) || (term1->kind != EXPRNODE) )
411 ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2)
416 if (sRef_same (term1->value.expr->sref, term2->value.expr->sref) )
418 DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2) ) ));
423 DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2) ) ));
429 static /*@exposed@*/ sRef constraintTerm_getsRef (constraintTerm t)
431 llassert (t != NULL);
432 if (t->kind == EXPRNODE)
434 return exprNode_getSref(t->value.expr);
439 return t->value.sref;
442 return sRef_undefined;
445 bool constraintTerm_probSame (constraintTerm term1, constraintTerm term2)
449 llassert (term1 !=NULL && term2 !=NULL);
452 ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2)
457 s1 = constraintTerm_getName (term1);
458 s2 = constraintTerm_getName (term2);
460 if (cstring_equal (s1, s2) )
462 DPRINTF ((message (" %q and %q are same", s1, s2 ) ) );
467 DPRINTF ((message (" %q and %q are not same", s1, s2 ) ) );
472 bool constraintTerm_similar (constraintTerm term1, constraintTerm term2)
476 llassert (term1 !=NULL && term2 !=NULL);
478 if (constraintTerm_canGetValue (term1) && constraintTerm_canGetValue (term2))
479 /* evans 2001-07-24: was (term1->kind == INTLITERAL) && (term2->kind == INTLITERAL) ) */
483 t1 = constraintTerm_getValue (term1);
484 t2 = constraintTerm_getValue (term2);
489 if (constraintTerm_canGetValue (term1) || constraintTerm_canGetValue (term2))
491 /* evans 2001-07-24: is this right? */ /*@i534@*/
495 s1 = constraintTerm_getsRef (term1);
496 s2 = constraintTerm_getsRef (term2);
498 if (!(sRef_isValid(s1) && sRef_isValid(s2)))
504 ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2)
509 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
511 DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2) ) ));
516 DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2) ) ));
521 void constraintTerm_dump ( /*@observer@*/ constraintTerm t, FILE *f)
524 constraintTermValue value;
525 constraintTermType kind;
534 fprintf(f, "%d\n", (int) kind);
540 u = exprNode_getUentry(t->value.expr);
541 fprintf(f, "%s\n", cstring_toCharsSafe( uentry_rawName (u) )
551 if (sRef_isResult (s ) )
553 fprintf(f, "Result\n");
555 else if (sRef_isParam (s ) )
562 ct = sRef_getType (s);
563 param = sRef_getParam(s);
565 ctString = ctype_dump(ct);
567 fprintf(f, "Param %s %d\n", cstring_toCharsSafe(ctString), (int) param );
568 cstring_free(ctString);
572 u = sRef_getUentry(s);
573 fprintf(f, "%s\n", cstring_toCharsSafe(uentry_rawName (u) ) );
580 fprintf (f, "%ld\n", t->value.intlit);
590 /*@only@*/ constraintTerm constraintTerm_undump ( FILE *f)
592 constraintTermType kind;
600 str = mstring_create (MAX_DUMP_LINE_LENGTH);
602 str = fgets (os, MAX_DUMP_LINE_LENGTH, f);
604 kind = (constraintTermType) reader_getInt(&str);
605 str = fgets(os, MAX_DUMP_LINE_LENGTH, f);
614 term = reader_getWord(&str);
616 if (strcmp (term, "Result") == 0 )
618 s = sRef_makeResult (ctype_unknown);
620 else if (strcmp (term, "Param" ) == 0 )
627 reader_checkChar(&str, ' ');
628 str2 = reader_getWord(&str);
629 param = reader_getInt(&str);
632 t = ctype_undump(&str2) ;
633 s = sRef_makeParam (param, t, stateInfo_makeLoc (g_currentloc));
636 else //This must be an identified that we can search for
639 cstring termStr = cstring_makeLiteralTemp(term);
641 ue = usymtab_lookup (termStr);
642 s = uentry_getSref(ue);
645 ret = constraintTerm_makesRef(s);
657 term = reader_getWord(&str);
658 //This must be an identifier that we can search for
660 termStr = cstring_makeLiteralTemp(term);
662 ue = usymtab_lookup (termStr);
663 s = uentry_getSref(ue);
664 ret = constraintTerm_makesRef(s);
675 i = reader_getInt(&str);
676 ret = constraintTerm_makeIntLiteral (i);