1 /*****************************************************************************/
5 /* Expression descriptor structure */
9 /* (C) 2002-2004 Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 ExprDesc* ED_Init (ExprDesc* Expr)
57 /* Initialize an ExprDesc */
70 const char* ED_GetLabelName (const ExprDesc* Expr, long Offs)
71 /* Return the assembler label name of the given expression. Beware: This
72 * function may use a static buffer, so the name may get "lost" on the second
73 * call to the function.
78 /* Expr may have it's own offset, adjust Offs accordingly */
81 /* Generate a label depending on the location */
82 switch (ED_GetLoc (Expr)) {
85 /* Absolute: numeric address or const */
86 xsprintf (Buf, sizeof (Buf), "$%04X", (int)(Offs & 0xFFFF));
91 /* Global or static variable */
93 xsprintf (Buf, sizeof (Buf), "%s%+ld",
94 SymGetAsmName (Expr->Sym), Offs);
96 xsprintf (Buf, sizeof (Buf), "%s",
97 SymGetAsmName (Expr->Sym));
102 /* Register variable */
103 xsprintf (Buf, sizeof (Buf), "regbank+%u",
104 (unsigned)((Offs + Expr->Name) & 0xFFFFU));
108 /* Literal in the literal pool */
110 xsprintf (Buf, sizeof (Buf), "%s%+ld",
111 LocalLabelName (Expr->Name), Offs);
113 xsprintf (Buf, sizeof (Buf), "%s",
114 LocalLabelName (Expr->Name));
119 Internal ("Invalid location in ED_GetLabelName: 0x%04X", ED_GetLoc (Expr));
122 /* Return a pointer to the static buffer */
128 int ED_GetStackOffs (const ExprDesc* Expr, int Offs)
129 /* Get the stack offset of an address on the stack in Expr taking into account
130 * an additional offset in Offs.
133 PRECONDITION (ED_IsLocStack (Expr));
134 Offs += ((int) Expr->IVal) - StackPtr;
135 CHECK (Offs >= 0); /* Cannot handle negative stack offsets */
141 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, type* Type)
142 /* Make Expr an absolute const with the given value and type. */
146 Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL;
155 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value)
156 /* Make Expr a constant integer expression with the given value */
159 Expr->Type = type_int;
160 Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL;
169 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr)
170 /* Convert Expr into a rvalue which is in the primary register without an
175 Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_NEED_TEST | E_CC_SET);
176 Expr->Flags |= (E_LOC_EXPR | E_RTYPE_RVAL);
178 Expr->IVal = 0; /* No offset */
185 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr)
186 /* Convert Expr into a lvalue which is in the primary register without an
191 Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_NEED_TEST | E_CC_SET);
192 Expr->Flags |= (E_LOC_EXPR | E_RTYPE_LVAL);
194 Expr->IVal = 0; /* No offset */
201 int ED_IsConst (const ExprDesc* Expr)
202 /* Return true if the expression denotes a constant of some sort. This can be a
203 * numeric constant, the address of a global variable (maybe with offset) or
207 return ED_IsRVal (Expr) && (Expr->Flags & E_LOC_CONST) != 0;
212 int ED_IsConstAbsInt (const ExprDesc* Expr)
213 /* Return true if the expression is a constant (numeric) integer. */
215 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL) &&
216 IsClassInt (Expr->Type);
221 int ED_IsNullPtr (const ExprDesc* Expr)
222 /* Return true if the given expression is a NULL pointer constant */
224 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL) &&
226 IsClassInt (Expr->Type);
231 int ED_IsBool (const ExprDesc* Expr)
232 /* Return true of the expression can be treated as a boolean, that is, it can
233 * be an operand to a compare operation.
236 /* Either ints, floats, or pointers can be used in a boolean context */
237 return IsClassInt (Expr->Type) ||
238 IsClassFloat (Expr->Type) ||
239 IsClassPtr (Expr->Type);
244 void PrintExprDesc (FILE* F, ExprDesc* E)
245 /* Print an ExprDesc */
250 fprintf (F, "Symbol: %s\n", E->Sym? E->Sym->Name : "(none)");
252 fprintf (F, "Type: ");
253 PrintType (F, E->Type);
254 fprintf (F, "\nRaw type: ");
255 PrintRawType (F, E->Type);
257 fprintf (F, "Type: (unknown)\n"
258 "Raw type: (unknown)\n");
260 fprintf (F, "IVal: 0x%08lX\n", E->IVal);
261 fprintf (F, "FVal: %f\n", E->FVal);
265 fprintf (F, "Flags: 0x%04X ", Flags);
266 if (Flags & E_LOC_ABS) {
267 fprintf (F, "%cE_LOC_ABS", Sep);
271 if (Flags & E_LOC_GLOBAL) {
272 fprintf (F, "%cE_LOC_GLOBAL", Sep);
273 Flags &= ~E_LOC_GLOBAL;
276 if (Flags & E_LOC_STATIC) {
277 fprintf (F, "%cE_LOC_STATIC", Sep);
278 Flags &= ~E_LOC_STATIC;
281 if (Flags & E_LOC_REGISTER) {
282 fprintf (F, "%cE_LOC_REGISTER", Sep);
283 Flags &= ~E_LOC_REGISTER;
286 if (Flags & E_LOC_STACK) {
287 fprintf (F, "%cE_LOC_STACK", Sep);
288 Flags &= ~E_LOC_STACK;
291 if (Flags & E_LOC_PRIMARY) {
292 fprintf (F, "%cE_LOC_PRIMARY", Sep);
293 Flags &= ~E_LOC_PRIMARY;
296 if (Flags & E_LOC_EXPR) {
297 fprintf (F, "%cE_LOC_EXPR", Sep);
298 Flags &= ~E_LOC_EXPR;
301 if (Flags & E_LOC_LITERAL) {
302 fprintf (F, "%cE_LOC_LITERAL", Sep);
303 Flags &= ~E_LOC_LITERAL;
306 if (Flags & E_RTYPE_LVAL) {
307 fprintf (F, "%cE_RTYPE_LVAL", Sep);
308 Flags &= ~E_RTYPE_LVAL;
311 if (Flags & E_NEED_TEST) {
312 fprintf (F, "%cE_NEED_TEST", Sep);
313 Flags &= ~E_NEED_TEST;
316 if (Flags & E_CC_SET) {
317 fprintf (F, "%cE_CC_SET", Sep);
322 fprintf (F, "%c,0x%04X", Sep, Flags);
328 fprintf (F, "\nName: 0x%08lX\n", E->Name);
333 type* ReplaceType (ExprDesc* Expr, const type* NewType)
334 /* Replace the type of Expr by a copy of Newtype and return the old type string */
336 type* OldType = Expr->Type;
337 Expr->Type = TypeDup (NewType);