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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Defines for the flags field of the expression descriptor */
60 /* Location: Where is the value we're talking about? */
62 E_LOC_ABS = 0x0001, /* Absolute: numeric address or const */
63 E_LOC_GLOBAL = 0x0002, /* Global variable */
64 E_LOC_STATIC = 0x0004, /* Static variable */
65 E_LOC_REGISTER = 0x0008, /* Register variable */
66 E_LOC_STACK = 0x0010, /* Value on the stack */
67 E_LOC_PRIMARY = 0x0020, /* The primary register */
68 E_LOC_EXPR = 0x0040, /* An expression in the primary register */
69 E_LOC_LITERAL = 0x0080, /* Literal in the literal pool */
71 /* Constant location of some sort (only if rval) */
72 E_LOC_CONST = E_LOC_ABS | E_LOC_GLOBAL | E_LOC_STATIC |
73 E_LOC_REGISTER | E_LOC_LITERAL,
76 E_MASK_RTYPE = 0x0100,
77 E_RTYPE_RVAL = 0x0000,
78 E_RTYPE_LVAL = 0x0100,
84 E_NEED_TEST = 0x0400, /* Expression needs a test to set cc */
85 E_CC_SET = 0x0800 /* Condition codes are set */
88 /* Describe the result of an expression */
89 typedef struct ExprDesc ExprDesc;
91 struct SymEntry* Sym; /* Symbol table entry if known */
92 Type* Type; /* Type array of expression */
94 unsigned long Name; /* Name or label number */
95 long IVal; /* Integer value if expression constant */
96 Double FVal; /* Floating point value */
98 unsigned BitOffs; /* Bit offset for bit fields */
99 unsigned BitWidth; /* Bit width for bit fields */
104 /*****************************************************************************/
106 /*****************************************************************************/
110 ExprDesc* ED_Init (ExprDesc* Expr);
111 /* Initialize an ExprDesc */
113 #if defined(HAVE_INLINE)
114 INLINE int ED_GetLoc (const ExprDesc* Expr)
115 /* Return the location flags from the expression */
117 return (Expr->Flags & E_MASK_LOC);
120 # define ED_GetLoc(Expr) ((Expr)->Flags & E_MASK_LOC)
123 #if defined(HAVE_INLINE)
124 INLINE int ED_IsLocAbs (const ExprDesc* Expr)
125 /* Return true if the expression is an absolute value */
127 return (Expr->Flags & E_MASK_LOC) == E_LOC_ABS;
130 # define ED_IsLocAbs(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_ABS)
133 #if defined(HAVE_INLINE)
134 INLINE int ED_IsLocRegister (const ExprDesc* Expr)
135 /* Return true if the expression is located in a register */
137 return (Expr->Flags & E_MASK_LOC) == E_LOC_REGISTER;
140 # define ED_IsLocRegister(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_REGISTER)
143 #if defined(HAVE_INLINE)
144 INLINE int ED_IsLocStack (const ExprDesc* Expr)
145 /* Return true if the expression is located on the stack */
147 return (Expr->Flags & E_MASK_LOC) == E_LOC_STACK;
150 # define ED_IsLocStack(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_STACK)
153 #if defined(HAVE_INLINE)
154 INLINE int ED_IsLocPrimary (const ExprDesc* Expr)
155 /* Return true if the expression is an expression in the register pseudo variable */
157 return (Expr->Flags & E_MASK_LOC) == E_LOC_PRIMARY;
160 # define ED_IsLocPrimary(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_PRIMARY)
163 #if defined(HAVE_INLINE)
164 INLINE int ED_IsLocExpr (const ExprDesc* Expr)
165 /* Return true if the expression is an expression in the primary */
167 return (Expr->Flags & E_MASK_LOC) == E_LOC_EXPR;
170 # define ED_IsLocExpr(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_EXPR)
173 #if defined(HAVE_INLINE)
174 INLINE int ED_IsLocLiteral (const ExprDesc* Expr)
175 /* Return true if the expression is a string from the literal pool */
177 return (Expr->Flags & E_MASK_LOC) == E_LOC_LITERAL;
180 # define ED_IsLocLiteral(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_LITERAL)
183 #if defined(HAVE_INLINE)
184 INLINE int ED_IsLocConst (const ExprDesc* Expr)
185 /* Return true if the expression is a constant location of some sort */
187 return (Expr->Flags & E_LOC_CONST) != 0;
190 # define ED_IsLocConst(Expr) (((Expr)->Flags & E_LOC_CONST) != 0)
193 #if defined(HAVE_INLINE)
194 INLINE int ED_IsLVal (const ExprDesc* Expr)
195 /* Return true if the expression is a reference */
197 return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL;
200 # define ED_IsLVal(Expr) (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL)
203 #if defined(HAVE_INLINE)
204 INLINE int ED_IsRVal (const ExprDesc* Expr)
205 /* Return true if the expression is a rvalue */
207 return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL;
210 # define ED_IsRVal(Expr) (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL)
213 #if defined(HAVE_INLINE)
214 INLINE void ED_MakeLVal (ExprDesc* Expr)
215 /* Make the expression a lvalue. */
217 Expr->Flags |= E_RTYPE_LVAL;
220 # define ED_MakeLVal(Expr) do { (Expr)->Flags |= E_RTYPE_LVAL; } while (0)
223 #if defined(HAVE_INLINE)
224 INLINE void ED_MakeRVal (ExprDesc* Expr)
225 /* Make the expression a rvalue. */
227 Expr->Flags &= ~E_RTYPE_LVAL;
230 # define ED_MakeRVal(Expr) do { (Expr)->Flags &= ~E_RTYPE_LVAL; } while (0)
233 #if defined(HAVE_INLINE)
234 INLINE int ED_IsBitField (const ExprDesc* Expr)
235 /* Return true if the expression is a bit field */
237 return (Expr->Flags & E_BITFIELD) != 0;
240 # define ED_IsBitField(Expr) (((Expr)->Flags & E_BITFIELD) != 0)
243 void ED_MakeBitField (ExprDesc* Expr, unsigned BitOffs, unsigned BitWidth);
244 /* Make this expression a bit field expression */
246 #if defined(HAVE_INLINE)
247 INLINE void ED_MarkForTest (ExprDesc* Expr)
248 /* Mark the expression for a test. */
250 Expr->Flags |= E_NEED_TEST;
253 # define ED_MarkForTest(Expr) do { (Expr)->Flags |= E_NEED_TEST; } while (0)
256 #if defined(HAVE_INLINE)
257 INLINE int ED_NeedsTest (const ExprDesc* Expr)
258 /* Check if the expression needs a test. */
260 return (Expr->Flags & E_NEED_TEST) != 0;
263 # define ED_NeedsTest(Expr) (((Expr)->Flags & E_NEED_TEST) != 0)
266 #if defined(HAVE_INLINE)
267 INLINE void ED_TestDone (ExprDesc* Expr)
268 /* Mark the expression as tested and condition codes set. */
270 Expr->Flags = (Expr->Flags & ~E_NEED_TEST) | E_CC_SET;
273 # define ED_TestDone(Expr) \
274 do { (Expr)->Flags = ((Expr)->Flags & ~E_NEED_TEST) | E_CC_SET; } while (0)
277 #if defined(HAVE_INLINE)
278 INLINE int ED_IsTested (const ExprDesc* Expr)
279 /* Check if the expression has set the condition codes. */
281 return (Expr->Flags & E_CC_SET) != 0;
284 # define ED_IsTested(Expr) (((Expr)->Flags & E_CC_SET) != 0)
287 #if defined(HAVE_INLINE)
288 INLINE void ED_MarkAsUntested (ExprDesc* Expr)
289 /* Mark the expression as not tested (condition codes not set). */
291 Expr->Flags &= ~E_CC_SET;
294 # define ED_MarkAsUntested(Expr) do { (Expr)->Flags &= ~E_CC_SET; } while (0)
297 const char* ED_GetLabelName (const ExprDesc* Expr, long Offs);
298 /* Return the assembler label name of the given expression. Beware: This
299 * function may use a static buffer, so the name may get "lost" on the second
300 * call to the function.
303 int ED_GetStackOffs (const ExprDesc* Expr, int Offs);
304 /* Get the stack offset of an address on the stack in Expr taking into account
305 * an additional offset in Offs.
308 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type);
309 /* Make Expr an absolute const with the given value and type. */
311 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value);
312 /* Make Expr a constant integer expression with the given value */
314 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr);
315 /* Convert Expr into a rvalue which is in the primary register without an
319 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr);
320 /* Convert Expr into a lvalue which is in the primary register without an
324 int ED_IsConst (const ExprDesc* Expr);
325 /* Return true if the expression denotes a constant of some sort. This can be a
326 * numeric constant, the address of a global variable (maybe with offset) or
330 #if defined(HAVE_INLINE)
331 INLINE int ED_IsConstAbs (const ExprDesc* Expr)
332 /* Return true if the expression denotes a constant absolute value. This can be
333 * a numeric constant, cast to any type.
336 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL);
339 # define ED_IsConstAbs(E) \
340 (((E)->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL))
343 int ED_IsConstAbsInt (const ExprDesc* Expr);
344 /* Return true if the expression is a constant (numeric) integer. */
346 int ED_IsNullPtr (const ExprDesc* Expr);
347 /* Return true if the given expression is a NULL pointer constant */
349 int ED_IsBool (const ExprDesc* Expr);
350 /* Return true of the expression can be treated as a boolean, that is, it can
351 * be an operand to a compare operation.
354 void PrintExprDesc (FILE* F, ExprDesc* Expr);
355 /* Print an ExprDesc */
357 Type* ReplaceType (ExprDesc* Expr, const Type* NewType);
358 /* Replace the type of Expr by a copy of Newtype and return the old type string */
362 /* End of exprdesc.h */