1 /*****************************************************************************/
5 /* Expression descriptor structure */
9 /* (C) 2002-2008 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);
71 const char* ED_GetLabelName (const ExprDesc* Expr, long Offs)
72 /* Return the assembler label name of the given expression. Beware: This
73 * function may use a static buffer, so the name may get "lost" on the second
74 * call to the function.
77 static StrBuf Buf = STATIC_STRBUF_INITIALIZER;
79 /* Expr may have it's own offset, adjust Offs accordingly */
82 /* Generate a label depending on the location */
83 switch (ED_GetLoc (Expr)) {
86 /* Absolute: numeric address or const */
87 SB_Printf (&Buf, "$%04X", (int)(Offs & 0xFFFF));
92 /* Global or static variable */
94 SB_Printf (&Buf, "%s%+ld", SymGetAsmName (Expr->Sym), Offs);
96 SB_Printf (&Buf, "%s", SymGetAsmName (Expr->Sym));
101 /* Register variable */
102 SB_Printf (&Buf, "regbank+%u", (unsigned)((Offs + Expr->Name) & 0xFFFFU));
106 /* Literal in the literal pool */
108 SB_Printf (&Buf, "%s%+ld", LocalLabelName (Expr->Name), Offs);
110 SB_Printf (&Buf, "%s", LocalLabelName (Expr->Name));
115 Internal ("Invalid location in ED_GetLabelName: 0x%04X", ED_GetLoc (Expr));
118 /* Return a pointer to the static buffer */
119 return SB_GetConstBuf (&Buf);
124 int ED_GetStackOffs (const ExprDesc* Expr, int Offs)
125 /* Get the stack offset of an address on the stack in Expr taking into account
126 * an additional offset in Offs.
129 PRECONDITION (ED_IsLocStack (Expr));
130 Offs += ((int) Expr->IVal) - StackPtr;
131 CHECK (Offs >= 0); /* Cannot handle negative stack offsets */
137 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type)
138 /* Make Expr an absolute const with the given value and type. */
142 Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL;
145 Expr->FVal = FP_D_Make (0.0);
151 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value)
152 /* Make Expr a constant integer expression with the given value */
155 Expr->Type = type_int;
156 Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL;
159 Expr->FVal = FP_D_Make (0.0);
165 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr)
166 /* Convert Expr into a rvalue which is in the primary register without an
171 Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_NEED_TEST | E_CC_SET);
172 Expr->Flags |= (E_LOC_EXPR | E_RTYPE_RVAL);
174 Expr->IVal = 0; /* No offset */
175 Expr->FVal = FP_D_Make (0.0);
181 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr)
182 /* Convert Expr into a lvalue which is in the primary register without an
187 Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_NEED_TEST | E_CC_SET);
188 Expr->Flags |= (E_LOC_EXPR | E_RTYPE_LVAL);
190 Expr->IVal = 0; /* No offset */
191 Expr->FVal = FP_D_Make (0.0);
197 int ED_IsConst (const ExprDesc* Expr)
198 /* Return true if the expression denotes a constant of some sort. This can be a
199 * numeric constant, the address of a global variable (maybe with offset) or
203 return ED_IsRVal (Expr) && (Expr->Flags & E_LOC_CONST) != 0;
208 int ED_IsConstAbsInt (const ExprDesc* Expr)
209 /* Return true if the expression is a constant (numeric) integer. */
211 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL) &&
212 IsClassInt (Expr->Type);
217 int ED_IsNullPtr (const ExprDesc* Expr)
218 /* Return true if the given expression is a NULL pointer constant */
220 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL) &&
222 IsClassInt (Expr->Type);
227 int ED_IsBool (const ExprDesc* Expr)
228 /* Return true of the expression can be treated as a boolean, that is, it can
229 * be an operand to a compare operation.
232 /* Either ints, floats, or pointers can be used in a boolean context */
233 return IsClassInt (Expr->Type) ||
234 IsClassFloat (Expr->Type) ||
235 IsClassPtr (Expr->Type);
240 void PrintExprDesc (FILE* F, ExprDesc* E)
241 /* Print an ExprDesc */
246 fprintf (F, "Symbol: %s\n", E->Sym? E->Sym->Name : "(none)");
248 fprintf (F, "Type: ");
249 PrintType (F, E->Type);
250 fprintf (F, "\nRaw type: ");
251 PrintRawType (F, E->Type);
253 fprintf (F, "Type: (unknown)\n"
254 "Raw type: (unknown)\n");
256 fprintf (F, "IVal: 0x%08lX\n", E->IVal);
257 fprintf (F, "FVal: %f\n", FP_D_ToFloat (E->FVal));
261 fprintf (F, "Flags: 0x%04X ", Flags);
262 if (Flags & E_LOC_ABS) {
263 fprintf (F, "%cE_LOC_ABS", Sep);
267 if (Flags & E_LOC_GLOBAL) {
268 fprintf (F, "%cE_LOC_GLOBAL", Sep);
269 Flags &= ~E_LOC_GLOBAL;
272 if (Flags & E_LOC_STATIC) {
273 fprintf (F, "%cE_LOC_STATIC", Sep);
274 Flags &= ~E_LOC_STATIC;
277 if (Flags & E_LOC_REGISTER) {
278 fprintf (F, "%cE_LOC_REGISTER", Sep);
279 Flags &= ~E_LOC_REGISTER;
282 if (Flags & E_LOC_STACK) {
283 fprintf (F, "%cE_LOC_STACK", Sep);
284 Flags &= ~E_LOC_STACK;
287 if (Flags & E_LOC_PRIMARY) {
288 fprintf (F, "%cE_LOC_PRIMARY", Sep);
289 Flags &= ~E_LOC_PRIMARY;
292 if (Flags & E_LOC_EXPR) {
293 fprintf (F, "%cE_LOC_EXPR", Sep);
294 Flags &= ~E_LOC_EXPR;
297 if (Flags & E_LOC_LITERAL) {
298 fprintf (F, "%cE_LOC_LITERAL", Sep);
299 Flags &= ~E_LOC_LITERAL;
302 if (Flags & E_RTYPE_LVAL) {
303 fprintf (F, "%cE_RTYPE_LVAL", Sep);
304 Flags &= ~E_RTYPE_LVAL;
307 if (Flags & E_NEED_TEST) {
308 fprintf (F, "%cE_NEED_TEST", Sep);
309 Flags &= ~E_NEED_TEST;
312 if (Flags & E_CC_SET) {
313 fprintf (F, "%cE_CC_SET", Sep);
318 fprintf (F, "%c,0x%04X", Sep, Flags);
324 fprintf (F, "\nName: 0x%08lX\n", E->Name);
329 Type* ReplaceType (ExprDesc* Expr, const Type* NewType)
330 /* Replace the type of Expr by a copy of Newtype and return the old type string */
332 Type* OldType = Expr->Type;
333 Expr->Type = TypeDup (NewType);