1 /*****************************************************************************/
5 /* Expression descriptor structure */
9 /* (C) 2002-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 ExprDesc* ED_Init (ExprDesc* Expr)
58 /* Initialize an ExprDesc */
65 Expr->FVal = FP_D_Make (0.0);
73 void ED_MakeBitField (ExprDesc* Expr, unsigned BitOffs, unsigned BitWidth)
74 /* Make this expression a bit field expression */
76 Expr->Flags |= E_BITFIELD;
77 Expr->BitOffs = BitOffs;
78 Expr->BitWidth = BitWidth;
83 const char* ED_GetLabelName (const ExprDesc* Expr, long Offs)
84 /* Return the assembler label name of the given expression. Beware: This
85 * function may use a static buffer, so the name may get "lost" on the second
86 * call to the function.
89 static StrBuf Buf = STATIC_STRBUF_INITIALIZER;
91 /* Expr may have it's own offset, adjust Offs accordingly */
94 /* Generate a label depending on the location */
95 switch (ED_GetLoc (Expr)) {
98 /* Absolute: numeric address or const */
99 SB_Printf (&Buf, "$%04X", (int)(Offs & 0xFFFF));
104 /* Global or static variable */
106 SB_Printf (&Buf, "%s%+ld", SymGetAsmName (Expr->Sym), Offs);
108 SB_Printf (&Buf, "%s", SymGetAsmName (Expr->Sym));
113 /* Register variable */
114 SB_Printf (&Buf, "regbank+%u", (unsigned)((Offs + Expr->Name) & 0xFFFFU));
118 /* Literal in the literal pool */
120 SB_Printf (&Buf, "%s%+ld", LocalLabelName (Expr->Name), Offs);
122 SB_Printf (&Buf, "%s", LocalLabelName (Expr->Name));
127 Internal ("Invalid location in ED_GetLabelName: 0x%04X", ED_GetLoc (Expr));
130 /* Return a pointer to the static buffer */
131 return SB_GetConstBuf (&Buf);
136 int ED_GetStackOffs (const ExprDesc* Expr, int Offs)
137 /* Get the stack offset of an address on the stack in Expr taking into account
138 * an additional offset in Offs.
141 PRECONDITION (ED_IsLocStack (Expr));
142 Offs += ((int) Expr->IVal) - StackPtr;
143 CHECK (Offs >= 0); /* Cannot handle negative stack offsets */
149 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type)
150 /* Make Expr an absolute const with the given value and type. */
154 Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL;
157 Expr->FVal = FP_D_Make (0.0);
163 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value)
164 /* Make Expr a constant integer expression with the given value */
167 Expr->Type = type_int;
168 Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL;
171 Expr->FVal = FP_D_Make (0.0);
177 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr)
178 /* Convert Expr into a rvalue which is in the primary register without an
183 Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_BITFIELD | E_NEED_TEST | E_CC_SET);
184 Expr->Flags |= (E_LOC_EXPR | E_RTYPE_RVAL);
186 Expr->IVal = 0; /* No offset */
187 Expr->FVal = FP_D_Make (0.0);
193 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr)
194 /* Convert Expr into a lvalue which is in the primary register without an
199 Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_BITFIELD | E_NEED_TEST | E_CC_SET);
200 Expr->Flags |= (E_LOC_EXPR | E_RTYPE_LVAL);
202 Expr->IVal = 0; /* No offset */
203 Expr->FVal = FP_D_Make (0.0);
209 int ED_IsConst (const ExprDesc* Expr)
210 /* Return true if the expression denotes a constant of some sort. This can be a
211 * numeric constant, the address of a global variable (maybe with offset) or
215 return ED_IsRVal (Expr) && (Expr->Flags & E_LOC_CONST) != 0;
220 int ED_IsConstAbsInt (const ExprDesc* Expr)
221 /* Return true if the expression is a constant (numeric) integer. */
223 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL) &&
224 IsClassInt (Expr->Type);
229 int ED_IsNullPtr (const ExprDesc* Expr)
230 /* Return true if the given expression is a NULL pointer constant */
232 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE|E_BITFIELD)) ==
233 (E_LOC_ABS|E_RTYPE_RVAL) &&
235 IsClassInt (Expr->Type);
240 int ED_IsBool (const ExprDesc* Expr)
241 /* Return true of the expression can be treated as a boolean, that is, it can
242 * be an operand to a compare operation.
245 /* Either ints, floats, or pointers can be used in a boolean context */
246 return IsClassInt (Expr->Type) ||
247 IsClassFloat (Expr->Type) ||
248 IsClassPtr (Expr->Type);
253 void PrintExprDesc (FILE* F, ExprDesc* E)
254 /* Print an ExprDesc */
259 fprintf (F, "Symbol: %s\n", E->Sym? E->Sym->Name : "(none)");
261 fprintf (F, "Type: ");
262 PrintType (F, E->Type);
263 fprintf (F, "\nRaw type: ");
264 PrintRawType (F, E->Type);
266 fprintf (F, "Type: (unknown)\n"
267 "Raw type: (unknown)\n");
269 fprintf (F, "IVal: 0x%08lX\n", E->IVal);
270 fprintf (F, "FVal: %f\n", FP_D_ToFloat (E->FVal));
274 fprintf (F, "Flags: 0x%04X ", Flags);
275 if (Flags & E_LOC_ABS) {
276 fprintf (F, "%cE_LOC_ABS", Sep);
280 if (Flags & E_LOC_GLOBAL) {
281 fprintf (F, "%cE_LOC_GLOBAL", Sep);
282 Flags &= ~E_LOC_GLOBAL;
285 if (Flags & E_LOC_STATIC) {
286 fprintf (F, "%cE_LOC_STATIC", Sep);
287 Flags &= ~E_LOC_STATIC;
290 if (Flags & E_LOC_REGISTER) {
291 fprintf (F, "%cE_LOC_REGISTER", Sep);
292 Flags &= ~E_LOC_REGISTER;
295 if (Flags & E_LOC_STACK) {
296 fprintf (F, "%cE_LOC_STACK", Sep);
297 Flags &= ~E_LOC_STACK;
300 if (Flags & E_LOC_PRIMARY) {
301 fprintf (F, "%cE_LOC_PRIMARY", Sep);
302 Flags &= ~E_LOC_PRIMARY;
305 if (Flags & E_LOC_EXPR) {
306 fprintf (F, "%cE_LOC_EXPR", Sep);
307 Flags &= ~E_LOC_EXPR;
310 if (Flags & E_LOC_LITERAL) {
311 fprintf (F, "%cE_LOC_LITERAL", Sep);
312 Flags &= ~E_LOC_LITERAL;
315 if (Flags & E_RTYPE_LVAL) {
316 fprintf (F, "%cE_RTYPE_LVAL", Sep);
317 Flags &= ~E_RTYPE_LVAL;
320 if (Flags & E_BITFIELD) {
321 fprintf (F, "%cE_BITFIELD", Sep);
322 Flags &= ~E_BITFIELD;
325 if (Flags & E_NEED_TEST) {
326 fprintf (F, "%cE_NEED_TEST", Sep);
327 Flags &= ~E_NEED_TEST;
330 if (Flags & E_CC_SET) {
331 fprintf (F, "%cE_CC_SET", Sep);
336 fprintf (F, "%c,0x%04X", Sep, Flags);
342 fprintf (F, "\nName: 0x%08lX\n", E->Name);
347 Type* ReplaceType (ExprDesc* Expr, const Type* NewType)
348 /* Replace the type of Expr by a copy of Newtype and return the old type string */
350 Type* OldType = Expr->Type;
351 Expr->Type = TypeDup (NewType);