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 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 /* Defines for the flags field of the expression descriptor */
61 /* Location: Where is the value we're talking about? */
63 E_LOC_ABS = 0x0001, /* Absolute: numeric address or const */
64 E_LOC_GLOBAL = 0x0002, /* Global variable */
65 E_LOC_STATIC = 0x0004, /* Static variable */
66 E_LOC_REGISTER = 0x0008, /* Register variable */
67 E_LOC_STACK = 0x0010, /* Value on the stack */
68 E_LOC_PRIMARY = 0x0020, /* The primary register */
69 E_LOC_EXPR = 0x0040, /* An expression in the primary register */
70 E_LOC_LITERAL = 0x0080, /* Literal in the literal pool */
72 /* Constant location of some sort (only if rval) */
73 E_LOC_CONST = E_LOC_ABS | E_LOC_GLOBAL | E_LOC_STATIC |
74 E_LOC_REGISTER | E_LOC_LITERAL,
77 E_MASK_RTYPE = 0x0100,
78 E_RTYPE_RVAL = 0x0000,
79 E_RTYPE_LVAL = 0x0100,
85 E_NEED_TEST = 0x0400, /* Expression needs a test to set cc */
86 E_CC_SET = 0x0800, /* Condition codes are set */
88 E_HAVE_MARKS = 0x1000, /* Code marks are valid */
95 /* Describe the result of an expression */
96 typedef struct ExprDesc ExprDesc;
98 struct SymEntry* Sym; /* Symbol table entry if known */
99 Type* Type; /* Type array of expression */
101 unsigned long Name; /* Name or label number */
102 long IVal; /* Integer value if expression constant */
103 Double FVal; /* Floating point value */
104 struct Literal* LVal; /* Literal value */
106 /* Bit field stuff */
107 unsigned BitOffs; /* Bit offset for bit fields */
108 unsigned BitWidth; /* Bit width for bit fields */
110 /* Start and end of generated code */
117 /*****************************************************************************/
119 /*****************************************************************************/
123 ExprDesc* ED_Init (ExprDesc* Expr);
124 /* Initialize an ExprDesc */
126 #if defined(HAVE_INLINE)
127 INLINE int ED_GetLoc (const ExprDesc* Expr)
128 /* Return the location flags from the expression */
130 return (Expr->Flags & E_MASK_LOC);
133 # define ED_GetLoc(Expr) ((Expr)->Flags & E_MASK_LOC)
136 #if defined(HAVE_INLINE)
137 INLINE int ED_IsLocAbs (const ExprDesc* Expr)
138 /* Return true if the expression is an absolute value */
140 return (Expr->Flags & E_MASK_LOC) == E_LOC_ABS;
143 # define ED_IsLocAbs(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_ABS)
146 #if defined(HAVE_INLINE)
147 INLINE int ED_IsLocRegister (const ExprDesc* Expr)
148 /* Return true if the expression is located in a register */
150 return (Expr->Flags & E_MASK_LOC) == E_LOC_REGISTER;
153 # define ED_IsLocRegister(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_REGISTER)
156 #if defined(HAVE_INLINE)
157 INLINE int ED_IsLocStack (const ExprDesc* Expr)
158 /* Return true if the expression is located on the stack */
160 return (Expr->Flags & E_MASK_LOC) == E_LOC_STACK;
163 # define ED_IsLocStack(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_STACK)
166 #if defined(HAVE_INLINE)
167 INLINE int ED_IsLocPrimary (const ExprDesc* Expr)
168 /* Return true if the expression is an expression in the register pseudo variable */
170 return (Expr->Flags & E_MASK_LOC) == E_LOC_PRIMARY;
173 # define ED_IsLocPrimary(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_PRIMARY)
176 #if defined(HAVE_INLINE)
177 INLINE int ED_IsLocExpr (const ExprDesc* Expr)
178 /* Return true if the expression is an expression in the primary */
180 return (Expr->Flags & E_MASK_LOC) == E_LOC_EXPR;
183 # define ED_IsLocExpr(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_EXPR)
186 #if defined(HAVE_INLINE)
187 INLINE int ED_IsLocLiteral (const ExprDesc* Expr)
188 /* Return true if the expression is a string from the literal pool */
190 return (Expr->Flags & E_MASK_LOC) == E_LOC_LITERAL;
193 # define ED_IsLocLiteral(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_LITERAL)
196 #if defined(HAVE_INLINE)
197 INLINE int ED_IsLocConst (const ExprDesc* Expr)
198 /* Return true if the expression is a constant location of some sort */
200 return (Expr->Flags & E_LOC_CONST) != 0;
203 # define ED_IsLocConst(Expr) (((Expr)->Flags & E_LOC_CONST) != 0)
206 #if defined(HAVE_INLINE)
207 INLINE int ED_IsLVal (const ExprDesc* Expr)
208 /* Return true if the expression is a reference */
210 return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL;
213 # define ED_IsLVal(Expr) (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL)
216 #if defined(HAVE_INLINE)
217 INLINE int ED_IsRVal (const ExprDesc* Expr)
218 /* Return true if the expression is a rvalue */
220 return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL;
223 # define ED_IsRVal(Expr) (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL)
226 #if defined(HAVE_INLINE)
227 INLINE void ED_MakeLVal (ExprDesc* Expr)
228 /* Make the expression a lvalue. */
230 Expr->Flags |= E_RTYPE_LVAL;
233 # define ED_MakeLVal(Expr) do { (Expr)->Flags |= E_RTYPE_LVAL; } while (0)
236 #if defined(HAVE_INLINE)
237 INLINE void ED_MakeRVal (ExprDesc* Expr)
238 /* Make the expression a rvalue. */
240 Expr->Flags &= ~E_RTYPE_LVAL;
243 # define ED_MakeRVal(Expr) do { (Expr)->Flags &= ~E_RTYPE_LVAL; } while (0)
246 #if defined(HAVE_INLINE)
247 INLINE int ED_IsBitField (const ExprDesc* Expr)
248 /* Return true if the expression is a bit field */
250 return (Expr->Flags & E_BITFIELD) != 0;
253 # define ED_IsBitField(Expr) (((Expr)->Flags & E_BITFIELD) != 0)
256 void ED_MakeBitField (ExprDesc* Expr, unsigned BitOffs, unsigned BitWidth);
257 /* Make this expression a bit field expression */
259 #if defined(HAVE_INLINE)
260 INLINE void ED_MarkForTest (ExprDesc* Expr)
261 /* Mark the expression for a test. */
263 Expr->Flags |= E_NEED_TEST;
266 # define ED_MarkForTest(Expr) do { (Expr)->Flags |= E_NEED_TEST; } while (0)
269 #if defined(HAVE_INLINE)
270 INLINE int ED_NeedsTest (const ExprDesc* Expr)
271 /* Check if the expression needs a test. */
273 return (Expr->Flags & E_NEED_TEST) != 0;
276 # define ED_NeedsTest(Expr) (((Expr)->Flags & E_NEED_TEST) != 0)
279 #if defined(HAVE_INLINE)
280 INLINE void ED_TestDone (ExprDesc* Expr)
281 /* Mark the expression as tested and condition codes set. */
283 Expr->Flags = (Expr->Flags & ~E_NEED_TEST) | E_CC_SET;
286 # define ED_TestDone(Expr) \
287 do { (Expr)->Flags = ((Expr)->Flags & ~E_NEED_TEST) | E_CC_SET; } while (0)
290 #if defined(HAVE_INLINE)
291 INLINE int ED_IsTested (const ExprDesc* Expr)
292 /* Check if the expression has set the condition codes. */
294 return (Expr->Flags & E_CC_SET) != 0;
297 # define ED_IsTested(Expr) (((Expr)->Flags & E_CC_SET) != 0)
300 #if defined(HAVE_INLINE)
301 INLINE void ED_MarkAsUntested (ExprDesc* Expr)
302 /* Mark the expression as not tested (condition codes not set). */
304 Expr->Flags &= ~E_CC_SET;
307 # define ED_MarkAsUntested(Expr) do { (Expr)->Flags &= ~E_CC_SET; } while (0)
310 void ED_SetCodeRange (ExprDesc* Expr, const CodeMark* Start, const CodeMark* End);
311 /* Set the code range for this expression */
313 int ED_CodeRangeIsEmpty (const ExprDesc* Expr);
314 /* Return true if no code was output for this expression */
316 const char* ED_GetLabelName (const ExprDesc* Expr, long Offs);
317 /* Return the assembler label name of the given expression. Beware: This
318 * function may use a static buffer, so the name may get "lost" on the second
319 * call to the function.
322 int ED_GetStackOffs (const ExprDesc* Expr, int Offs);
323 /* Get the stack offset of an address on the stack in Expr taking into account
324 * an additional offset in Offs.
327 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type);
328 /* Make Expr an absolute const with the given value and type. */
330 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value);
331 /* Make Expr a constant integer expression with the given value */
333 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr);
334 /* Convert Expr into a rvalue which is in the primary register without an
338 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr);
339 /* Convert Expr into a lvalue which is in the primary register without an
343 int ED_IsConst (const ExprDesc* Expr);
344 /* Return true if the expression denotes a constant of some sort. This can be a
345 * numeric constant, the address of a global variable (maybe with offset) or
349 #if defined(HAVE_INLINE)
350 INLINE int ED_IsConstAbs (const ExprDesc* Expr)
351 /* Return true if the expression denotes a constant absolute value. This can be
352 * a numeric constant, cast to any type.
355 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL);
358 # define ED_IsConstAbs(E) \
359 (((E)->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL))
362 int ED_IsConstAbsInt (const ExprDesc* Expr);
363 /* Return true if the expression is a constant (numeric) integer. */
365 int ED_IsNullPtr (const ExprDesc* Expr);
366 /* Return true if the given expression is a NULL pointer constant */
368 int ED_IsBool (const ExprDesc* Expr);
369 /* Return true of the expression can be treated as a boolean, that is, it can
370 * be an operand to a compare operation.
373 void PrintExprDesc (FILE* F, ExprDesc* Expr);
374 /* Print an ExprDesc */
376 Type* ReplaceType (ExprDesc* Expr, const Type* NewType);
377 /* Replace the type of Expr by a copy of Newtype and return the old type string */
381 /* End of exprdesc.h */