1 /*****************************************************************************/
5 /* Expression descriptor structure */
9 /* (C) 2002-2010, 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 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 ExprDesc* ED_Init (ExprDesc* Expr)
57 /* Initialize an ExprDesc */
64 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 void ED_SetCodeRange (ExprDesc* Expr, const CodeMark* Start, const CodeMark* End)
84 /* Set the code range for this expression */
86 Expr->Flags |= E_HAVE_MARKS;
93 int ED_CodeRangeIsEmpty (const ExprDesc* Expr)
94 /* Return true if no code was output for this expression */
96 /* We must have code marks */
97 PRECONDITION (Expr->Flags & E_HAVE_MARKS);
99 return CodeRangeIsEmpty (&Expr->Start, &Expr->End);
104 const char* ED_GetLabelName (const ExprDesc* Expr, long Offs)
105 /* Return the assembler label name of the given expression. Beware: This
106 ** function may use a static buffer, so the name may get "lost" on the second
107 ** call to the function.
110 static StrBuf Buf = STATIC_STRBUF_INITIALIZER;
112 /* Expr may have it's own offset, adjust Offs accordingly */
115 /* Generate a label depending on the location */
116 switch (ED_GetLoc (Expr)) {
119 /* Absolute: numeric address or const */
120 SB_Printf (&Buf, "$%04X", (int)(Offs & 0xFFFF));
125 /* Global or static variable */
127 SB_Printf (&Buf, "%s%+ld", SymGetAsmName (Expr->Sym), Offs);
129 SB_Printf (&Buf, "%s", SymGetAsmName (Expr->Sym));
134 /* Register variable */
135 SB_Printf (&Buf, "regbank+%u", (unsigned)((Offs + Expr->Name) & 0xFFFFU));
139 /* Literal in the literal pool */
141 SB_Printf (&Buf, "%s%+ld", LocalLabelName (Expr->Name), Offs);
143 SB_Printf (&Buf, "%s", LocalLabelName (Expr->Name));
148 Internal ("Invalid location in ED_GetLabelName: 0x%04X", ED_GetLoc (Expr));
151 /* Return a pointer to the static buffer */
152 return SB_GetConstBuf (&Buf);
157 int ED_GetStackOffs (const ExprDesc* Expr, int Offs)
158 /* Get the stack offset of an address on the stack in Expr taking into account
159 ** an additional offset in Offs.
162 PRECONDITION (ED_IsLocStack (Expr));
163 Offs += ((int) Expr->IVal) - StackPtr;
164 CHECK (Offs >= 0); /* Cannot handle negative stack offsets */
170 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type)
171 /* Make Expr an absolute const with the given value and type. */
175 Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL | (Expr->Flags & E_HAVE_MARKS);
178 Expr->FVal = FP_D_Make (0.0);
184 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value)
185 /* Make Expr a constant integer expression with the given value */
188 Expr->Type = type_int;
189 Expr->Flags = E_LOC_ABS | E_RTYPE_RVAL | (Expr->Flags & E_HAVE_MARKS);
192 Expr->FVal = FP_D_Make (0.0);
198 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr)
199 /* Convert Expr into a rvalue which is in the primary register without an
204 Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_BITFIELD | E_NEED_TEST | E_CC_SET);
205 Expr->Flags |= (E_LOC_EXPR | E_RTYPE_RVAL);
207 Expr->IVal = 0; /* No offset */
208 Expr->FVal = FP_D_Make (0.0);
214 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr)
215 /* Convert Expr into a lvalue which is in the primary register without an
220 Expr->Flags &= ~(E_MASK_LOC | E_MASK_RTYPE | E_BITFIELD | E_NEED_TEST | E_CC_SET);
221 Expr->Flags |= (E_LOC_EXPR | E_RTYPE_LVAL);
223 Expr->IVal = 0; /* No offset */
224 Expr->FVal = FP_D_Make (0.0);
230 int ED_IsConst (const ExprDesc* Expr)
231 /* Return true if the expression denotes a constant of some sort. This can be a
232 ** numeric constant, the address of a global variable (maybe with offset) or
236 return ED_IsRVal (Expr) && (Expr->Flags & E_LOC_CONST) != 0;
241 int ED_IsConstAbsInt (const ExprDesc* Expr)
242 /* Return true if the expression is a constant (numeric) integer. */
244 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL) &&
245 IsClassInt (Expr->Type);
250 int ED_IsNullPtr (const ExprDesc* Expr)
251 /* Return true if the given expression is a NULL pointer constant */
253 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE|E_BITFIELD)) ==
254 (E_LOC_ABS|E_RTYPE_RVAL) &&
256 IsClassInt (Expr->Type);
261 int ED_IsBool (const ExprDesc* Expr)
262 /* Return true of the expression can be treated as a boolean, that is, it can
263 ** be an operand to a compare operation.
266 /* Either ints, floats, or pointers can be used in a boolean context */
267 return IsClassInt (Expr->Type) ||
268 IsClassFloat (Expr->Type) ||
269 IsClassPtr (Expr->Type);
274 void PrintExprDesc (FILE* F, ExprDesc* E)
275 /* Print an ExprDesc */
280 fprintf (F, "Symbol: %s\n", E->Sym? E->Sym->Name : "(none)");
282 fprintf (F, "Type: ");
283 PrintType (F, E->Type);
284 fprintf (F, "\nRaw type: ");
285 PrintRawType (F, E->Type);
287 fprintf (F, "Type: (unknown)\n"
288 "Raw type: (unknown)\n");
290 fprintf (F, "IVal: 0x%08lX\n", E->IVal);
291 fprintf (F, "FVal: %f\n", FP_D_ToFloat (E->FVal));
295 fprintf (F, "Flags: 0x%04X ", Flags);
296 if (Flags & E_LOC_ABS) {
297 fprintf (F, "%cE_LOC_ABS", Sep);
301 if (Flags & E_LOC_GLOBAL) {
302 fprintf (F, "%cE_LOC_GLOBAL", Sep);
303 Flags &= ~E_LOC_GLOBAL;
306 if (Flags & E_LOC_STATIC) {
307 fprintf (F, "%cE_LOC_STATIC", Sep);
308 Flags &= ~E_LOC_STATIC;
311 if (Flags & E_LOC_REGISTER) {
312 fprintf (F, "%cE_LOC_REGISTER", Sep);
313 Flags &= ~E_LOC_REGISTER;
316 if (Flags & E_LOC_STACK) {
317 fprintf (F, "%cE_LOC_STACK", Sep);
318 Flags &= ~E_LOC_STACK;
321 if (Flags & E_LOC_PRIMARY) {
322 fprintf (F, "%cE_LOC_PRIMARY", Sep);
323 Flags &= ~E_LOC_PRIMARY;
326 if (Flags & E_LOC_EXPR) {
327 fprintf (F, "%cE_LOC_EXPR", Sep);
328 Flags &= ~E_LOC_EXPR;
331 if (Flags & E_LOC_LITERAL) {
332 fprintf (F, "%cE_LOC_LITERAL", Sep);
333 Flags &= ~E_LOC_LITERAL;
336 if (Flags & E_RTYPE_LVAL) {
337 fprintf (F, "%cE_RTYPE_LVAL", Sep);
338 Flags &= ~E_RTYPE_LVAL;
341 if (Flags & E_BITFIELD) {
342 fprintf (F, "%cE_BITFIELD", Sep);
343 Flags &= ~E_BITFIELD;
346 if (Flags & E_NEED_TEST) {
347 fprintf (F, "%cE_NEED_TEST", Sep);
348 Flags &= ~E_NEED_TEST;
351 if (Flags & E_CC_SET) {
352 fprintf (F, "%cE_CC_SET", Sep);
357 fprintf (F, "%c,0x%04X", Sep, Flags);
363 fprintf (F, "\nName: 0x%08lX\n", (unsigned long)E->Name);
368 Type* ReplaceType (ExprDesc* Expr, const Type* NewType)
369 /* Replace the type of Expr by a copy of Newtype and return the old type string */
371 Type* OldType = Expr->Type;
372 Expr->Type = TypeDup (NewType);