1 /*****************************************************************************/
5 /* Expression descriptor structure */
9 /* (C) 2002-2004 Ullrich von Bassewitz */
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 /* Defines for the flags field of the expression descriptor */
59 /* Location: Where is the value we're talking about? */
61 E_LOC_ABS = 0x0001, /* Absolute: numeric address or const */
62 E_LOC_GLOBAL = 0x0002, /* Global variable */
63 E_LOC_STATIC = 0x0004, /* Static variable */
64 E_LOC_REGISTER = 0x0008, /* Register variable */
65 E_LOC_STACK = 0x0010, /* Value on the stack */
66 E_LOC_PRIMARY = 0x0020, /* The primary register */
67 E_LOC_EXPR = 0x0040, /* An expression in the primary register */
68 E_LOC_LITERAL = 0x0080, /* Literal in the literal pool */
70 /* Constant location of some sort (only if rval) */
71 E_LOC_CONST = E_LOC_ABS | E_LOC_GLOBAL | E_LOC_STATIC |
72 E_LOC_REGISTER | E_LOC_LITERAL,
75 E_MASK_RTYPE = 0x8000,
76 E_RTYPE_RVAL = 0x0000,
80 /* Defines for the test field of the expression descriptor */
81 #define E_CC 0x0001U /* Condition codes are set */
82 #define E_FORCETEST 0x0002U /* Force test to set condition codes */
84 /* Describe the result of an expression */
85 typedef struct ExprDesc ExprDesc;
87 struct SymEntry* Sym; /* Symbol table entry if known */
88 type* Type; /* Type array of expression */
89 long Val; /* Value if expression constant */
91 unsigned short Test; /* */
92 unsigned long Name; /* Name or label number */
97 /*****************************************************************************/
99 /*****************************************************************************/
103 ExprDesc* ED_Init (ExprDesc* Expr);
104 /* Initialize an ExprDesc */
106 #if defined(HAVE_INLINE)
107 INLINE int ED_GetLoc (const ExprDesc* Expr)
108 /* Return the location flags from the expression */
110 return (Expr->Flags & E_MASK_LOC);
113 # define ED_GetLoc(Expr) ((Expr)->Flags & E_MASK_LOC)
116 #if defined(HAVE_INLINE)
117 INLINE int ED_IsLocAbs (const ExprDesc* Expr)
118 /* Return true if the expression is an absolute value */
120 return (Expr->Flags & E_MASK_LOC) == E_LOC_ABS;
123 # define ED_IsLocAbs(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_ABS)
126 #if defined(HAVE_INLINE)
127 INLINE int ED_IsLocRegister (const ExprDesc* Expr)
128 /* Return true if the expression is located in a register */
130 return (Expr->Flags & E_MASK_LOC) == E_LOC_REGISTER;
133 # define ED_IsLocRegister(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_REGISTER)
136 #if defined(HAVE_INLINE)
137 INLINE int ED_IsLocStack (const ExprDesc* Expr)
138 /* Return true if the expression is located on the stack */
140 return (Expr->Flags & E_MASK_LOC) == E_LOC_STACK;
143 # define ED_IsLocStack(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_STACK)
146 #if defined(HAVE_INLINE)
147 INLINE int ED_IsLocPrimary (const ExprDesc* Expr)
148 /* Return true if the expression is an expression in the register pseudo variable */
150 return (Expr->Flags & E_MASK_LOC) == E_LOC_PRIMARY;
153 # define ED_IsLocExpr(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_PRIMARY)
156 #if defined(HAVE_INLINE)
157 INLINE int ED_IsLocExpr (const ExprDesc* Expr)
158 /* Return true if the expression is an expression in the primary */
160 return (Expr->Flags & E_MASK_LOC) == E_LOC_EXPR;
163 # define ED_IsLocExpr(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_EXPR)
166 #if defined(HAVE_INLINE)
167 INLINE int ED_IsLocLiteral (const ExprDesc* Expr)
168 /* Return true if the expression is a string from the literal pool */
170 return (Expr->Flags & E_MASK_LOC) == E_LOC_LITERAL;
173 # define ED_IsLocLiteral(Expr) (((Expr)->Flags & E_MASK_LOC) == E_LOC_LITERAL)
176 #if defined(HAVE_INLINE)
177 INLINE int ED_IsLocConst (const ExprDesc* Expr)
178 /* Return true if the expression is a constant location of some sort */
180 return (Expr->Flags & E_LOC_CONST) != 0;
183 # define ED_IsLocConst(Expr) (((Expr)->Flags & E_LOC_CONST) != 0)
186 #if defined(HAVE_INLINE)
187 INLINE int ED_IsLVal (const ExprDesc* Expr)
188 /* Return true if the expression is a reference */
190 return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL;
193 # define ED_IsLVal(Expr) (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL)
196 #if defined(HAVE_INLINE)
197 INLINE int ED_IsRVal (const ExprDesc* Expr)
198 /* Return true if the expression is a rvalue */
200 return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL;
203 # define ED_IsRVal(Expr) (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL)
206 #if defined(HAVE_INLINE)
207 INLINE void ED_MakeLVal (ExprDesc* Expr)
208 /* Make the expression a lvalue. */
210 Expr->Flags |= E_RTYPE_LVAL;
213 # define ED_MakeLVal(Expr) do { (Expr)->Flags |= R_RTYPE_LVAL; } while (0)
216 #if defined(HAVE_INLINE)
217 INLINE void ED_MakeRVal (ExprDesc* Expr)
218 /* Make the expression a rvalue. */
220 Expr->Flags &= ~E_RTYPE_LVAL;
223 # define ED_MakeRVal(Expr) do { (Expr)->Flags &= ~E_RTYPE_LVAL; } while (0)
226 const char* ED_GetLabelName (const ExprDesc* Expr, long Offs);
227 /* Return the assembler label name of the given expression. Beware: This
228 * function may use a static buffer, so the name may get "lost" on the second
229 * call to the function.
232 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, type* Type);
233 /* Make Expr an absolute const with the given value and type. */
235 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value);
236 /* Make Expr a constant integer expression with the given value */
238 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr);
239 /* Convert Expr into a rvalue which is in the primary register without an
243 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr);
244 /* Convert Expr into a lvalue which is in the primary register without an
248 int ED_IsConst (const ExprDesc* Expr);
249 /* Return true if the expression denotes a constant of some sort. This can be a
250 * numeric constant, the address of a global variable (maybe with offset) or
254 int ED_IsConstAbs (const ExprDesc* Expr);
255 /* Return true if the expression denotes a constant absolute value. This can be
256 * a numeric constant, cast to any type.
259 int ED_IsConstAbsInt (const ExprDesc* Expr);
260 /* Return true if the expression is a constant (numeric) integer. */
262 int ED_IsNullPtr (const ExprDesc* Expr);
263 /* Return true if the given expression is a NULL pointer constant */
265 int ED_IsBool (const ExprDesc* Expr);
266 /* Return true of the expression can be treated as a boolean, that is, it can
267 * be an operand to a compare operation.
270 void PrintExprDesc (FILE* F, ExprDesc* Expr);
271 /* Print an ExprDesc */
273 type* ReplaceType (ExprDesc* Expr, const type* NewType);
274 /* Replace the type of Expr by a copy of Newtype and return the old type string */
278 /* End of exprdesc.h */