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 /*****************************************************************************/
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,
81 E_NEED_TEST = 0x0200, /* Expression needs a test to set cc */
82 E_CC_SET = 0x0400 /* Condition codes are set */
85 /* Describe the result of an expression */
86 typedef struct ExprDesc ExprDesc;
88 struct SymEntry* Sym; /* Symbol table entry if known */
89 Type* Type; /* Type array of expression */
91 unsigned long Name; /* Name or label number */
92 long IVal; /* Integer value if expression constant */
93 Double FVal; /* Floating point value */
98 /*****************************************************************************/
100 /*****************************************************************************/
104 ExprDesc* ED_Init (ExprDesc* Expr);
105 /* Initialize an ExprDesc */
107 #if defined(HAVE_INLINE)
108 INLINE int ED_GetLoc (const ExprDesc* Expr)
109 /* Return the location flags from the expression */
111 return (Expr->Flags & E_MASK_LOC);
114 # define ED_GetLoc(Expr) ((Expr)->Flags & E_MASK_LOC)
117 #if defined(HAVE_INLINE)
118 INLINE int ED_IsLocAbs (const ExprDesc* Expr)
119 /* Return true if the expression is an absolute value */
121 return (Expr->Flags & E_MASK_LOC) == E_LOC_ABS;
124 # define ED_IsLocAbs(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_ABS)
127 #if defined(HAVE_INLINE)
128 INLINE int ED_IsLocRegister (const ExprDesc* Expr)
129 /* Return true if the expression is located in a register */
131 return (Expr->Flags & E_MASK_LOC) == E_LOC_REGISTER;
134 # define ED_IsLocRegister(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_REGISTER)
137 #if defined(HAVE_INLINE)
138 INLINE int ED_IsLocStack (const ExprDesc* Expr)
139 /* Return true if the expression is located on the stack */
141 return (Expr->Flags & E_MASK_LOC) == E_LOC_STACK;
144 # define ED_IsLocStack(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_STACK)
147 #if defined(HAVE_INLINE)
148 INLINE int ED_IsLocPrimary (const ExprDesc* Expr)
149 /* Return true if the expression is an expression in the register pseudo variable */
151 return (Expr->Flags & E_MASK_LOC) == E_LOC_PRIMARY;
154 # define ED_IsLocPrimary(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_PRIMARY)
157 #if defined(HAVE_INLINE)
158 INLINE int ED_IsLocExpr (const ExprDesc* Expr)
159 /* Return true if the expression is an expression in the primary */
161 return (Expr->Flags & E_MASK_LOC) == E_LOC_EXPR;
164 # define ED_IsLocExpr(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_EXPR)
167 #if defined(HAVE_INLINE)
168 INLINE int ED_IsLocLiteral (const ExprDesc* Expr)
169 /* Return true if the expression is a string from the literal pool */
171 return (Expr->Flags & E_MASK_LOC) == E_LOC_LITERAL;
174 # define ED_IsLocLiteral(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_LITERAL)
177 #if defined(HAVE_INLINE)
178 INLINE int ED_IsLocConst (const ExprDesc* Expr)
179 /* Return true if the expression is a constant location of some sort */
181 return (Expr->Flags & E_LOC_CONST) != 0;
184 # define ED_IsLocConst(Expr) (((Expr)->Flags & E_LOC_CONST) != 0)
187 #if defined(HAVE_INLINE)
188 INLINE int ED_IsLVal (const ExprDesc* Expr)
189 /* Return true if the expression is a reference */
191 return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL;
194 # define ED_IsLVal(Expr) (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL)
197 #if defined(HAVE_INLINE)
198 INLINE int ED_IsRVal (const ExprDesc* Expr)
199 /* Return true if the expression is a rvalue */
201 return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL;
204 # define ED_IsRVal(Expr) (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL)
207 #if defined(HAVE_INLINE)
208 INLINE void ED_MakeLVal (ExprDesc* Expr)
209 /* Make the expression a lvalue. */
211 Expr->Flags |= E_RTYPE_LVAL;
214 # define ED_MakeLVal(Expr) do { (Expr)->Flags |= E_RTYPE_LVAL; } while (0)
217 #if defined(HAVE_INLINE)
218 INLINE void ED_MakeRVal (ExprDesc* Expr)
219 /* Make the expression a rvalue. */
221 Expr->Flags &= ~E_RTYPE_LVAL;
224 # define ED_MakeRVal(Expr) do { (Expr)->Flags &= ~E_RTYPE_LVAL; } while (0)
227 #if defined(HAVE_INLINE)
228 INLINE void ED_MarkForTest (ExprDesc* Expr)
229 /* Mark the expression for a test. */
231 Expr->Flags |= E_NEED_TEST;
234 # define ED_MarkForTest(Expr) do { (Expr)->Flags |= E_NEED_TEST; } while (0)
237 #if defined(HAVE_INLINE)
238 INLINE int ED_NeedsTest (const ExprDesc* Expr)
239 /* Check if the expression needs a test. */
241 return (Expr->Flags & E_NEED_TEST) != 0;
244 # define ED_NeedsTest(Expr) (((Expr)->Flags & E_NEED_TEST) != 0)
247 #if defined(HAVE_INLINE)
248 INLINE void ED_TestDone (ExprDesc* Expr)
249 /* Mark the expression as tested and condition codes set. */
251 Expr->Flags = (Expr->Flags & ~E_NEED_TEST) | E_CC_SET;
254 # define ED_TestDone(Expr) \
255 do { (Expr)->Flags = ((Expr)->Flags & ~E_NEED_TEST) | E_CC_SET; } while (0)
258 #if defined(HAVE_INLINE)
259 INLINE int ED_IsTested (const ExprDesc* Expr)
260 /* Check if the expression has set the condition codes. */
262 return (Expr->Flags & E_CC_SET) != 0;
265 # define ED_IsTested(Expr) (((Expr)->Flags & E_CC_SET) != 0)
268 #if defined(HAVE_INLINE)
269 INLINE void ED_MarkAsUntested (ExprDesc* Expr)
270 /* Mark the expression as not tested (condition codes not set). */
272 Expr->Flags &= ~E_CC_SET;
275 # define ED_MarkAsUntested(Expr) do { (Expr)->Flags &= ~E_CC_SET; } while (0)
278 const char* ED_GetLabelName (const ExprDesc* Expr, long Offs);
279 /* Return the assembler label name of the given expression. Beware: This
280 * function may use a static buffer, so the name may get "lost" on the second
281 * call to the function.
284 int ED_GetStackOffs (const ExprDesc* Expr, int Offs);
285 /* Get the stack offset of an address on the stack in Expr taking into account
286 * an additional offset in Offs.
289 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type);
290 /* Make Expr an absolute const with the given value and type. */
292 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value);
293 /* Make Expr a constant integer expression with the given value */
295 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr);
296 /* Convert Expr into a rvalue which is in the primary register without an
300 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr);
301 /* Convert Expr into a lvalue which is in the primary register without an
305 int ED_IsConst (const ExprDesc* Expr);
306 /* Return true if the expression denotes a constant of some sort. This can be a
307 * numeric constant, the address of a global variable (maybe with offset) or
311 #if defined(HAVE_INLINE)
312 INLINE int ED_IsConstAbs (const ExprDesc* Expr)
313 /* Return true if the expression denotes a constant absolute value. This can be
314 * a numeric constant, cast to any type.
317 return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL);
320 # define ED_IsConstAbs(E) \
321 (((E)->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL))
324 int ED_IsConstAbsInt (const ExprDesc* Expr);
325 /* Return true if the expression is a constant (numeric) integer. */
327 int ED_IsNullPtr (const ExprDesc* Expr);
328 /* Return true if the given expression is a NULL pointer constant */
330 int ED_IsBool (const ExprDesc* Expr);
331 /* Return true of the expression can be treated as a boolean, that is, it can
332 * be an operand to a compare operation.
335 void PrintExprDesc (FILE* F, ExprDesc* Expr);
336 /* Print an ExprDesc */
338 Type* ReplaceType (ExprDesc* Expr, const Type* NewType);
339 /* Replace the type of Expr by a copy of Newtype and return the old type string */
343 /* End of exprdesc.h */