]> git.sur5r.net Git - cc65/blob - src/cc65/exprdesc.h
Merge remote-tracking branch 'upstream/master' into a5200
[cc65] / src / cc65 / exprdesc.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                exprdesc.h                                 */
4 /*                                                                           */
5 /*                      Expression descriptor structure                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002-2010, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #ifndef EXPRDESC_H
37 #define EXPRDESC_H
38
39
40
41 #include <string.h>
42
43 /* common */
44 #include "fp.h"
45 #include "inline.h"
46
47 /* cc65 */
48 #include "asmcode.h"
49 #include "datatype.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 /* Defines for the flags field of the expression descriptor */
60 enum {
61     /* Location: Where is the value we're talking about? */
62     E_MASK_LOC          = 0x00FF,
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 */
71
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,
75
76     /* Reference? */
77     E_MASK_RTYPE        = 0x0100,
78     E_RTYPE_RVAL        = 0x0000,
79     E_RTYPE_LVAL        = 0x0100,
80
81     /* Bit-field? */
82     E_BITFIELD          = 0x0200,
83
84     /* Test */
85     E_NEED_TEST         = 0x0400,       /* Expression needs a test to set cc */
86     E_CC_SET            = 0x0800,       /* Condition codes are set */
87
88     E_HAVE_MARKS        = 0x1000,       /* Code marks are valid */
89
90 };
91
92 /* Forward */
93 struct Literal;
94
95 /* Describe the result of an expression */
96 typedef struct ExprDesc ExprDesc;
97 struct ExprDesc {
98     struct SymEntry*    Sym;            /* Symbol table entry if known */
99     Type*               Type;           /* Type array of expression */
100     unsigned            Flags;
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 */
105
106     /* Bit field stuff */
107     unsigned            BitOffs;        /* Bit offset for bit fields */
108     unsigned            BitWidth;       /* Bit width for bit fields */
109
110     /* Start and end of generated code */
111     CodeMark            Start;
112     CodeMark            End;
113 };
114
115
116
117 /*****************************************************************************/
118 /*                                   Code                                    */
119 /*****************************************************************************/
120
121
122
123 ExprDesc* ED_Init (ExprDesc* Expr);
124 /* Initialize an ExprDesc */
125
126 #if defined(HAVE_INLINE)
127 INLINE int ED_GetLoc (const ExprDesc* Expr)
128 /* Return the location flags from the expression */
129 {
130     return (Expr->Flags & E_MASK_LOC);
131 }
132 #else
133 #  define ED_GetLoc(Expr)       ((Expr)->Flags & E_MASK_LOC)
134 #endif
135
136 #if defined(HAVE_INLINE)
137 INLINE int ED_IsLocAbs (const ExprDesc* Expr)
138 /* Return true if the expression is an absolute value */
139 {
140     return (Expr->Flags & E_MASK_LOC) == E_LOC_ABS;
141 }
142 #else
143 #  define ED_IsLocAbs(Expr)     (((Expr)->Flags & E_MASK_LOC) == E_LOC_ABS)
144 #endif
145
146 #if defined(HAVE_INLINE)
147 INLINE int ED_IsLocRegister (const ExprDesc* Expr)
148 /* Return true if the expression is located in a register */
149 {
150     return (Expr->Flags & E_MASK_LOC) == E_LOC_REGISTER;
151 }
152 #else
153 #  define ED_IsLocRegister(Expr)    (((Expr)->Flags & E_MASK_LOC) == E_LOC_REGISTER)
154 #endif
155
156 #if defined(HAVE_INLINE)
157 INLINE int ED_IsLocStack (const ExprDesc* Expr)
158 /* Return true if the expression is located on the stack */
159 {
160     return (Expr->Flags & E_MASK_LOC) == E_LOC_STACK;
161 }
162 #else
163 #  define ED_IsLocStack(Expr)     (((Expr)->Flags & E_MASK_LOC) == E_LOC_STACK)
164 #endif
165
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 */
169 {
170     return (Expr->Flags & E_MASK_LOC) == E_LOC_PRIMARY;
171 }
172 #else
173 #  define ED_IsLocPrimary(Expr)  (((Expr)->Flags & E_MASK_LOC) == E_LOC_PRIMARY)
174 #endif
175
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 */
179 {
180     return (Expr->Flags & E_MASK_LOC) == E_LOC_EXPR;
181 }
182 #else
183 #  define ED_IsLocExpr(Expr)     (((Expr)->Flags & E_MASK_LOC) == E_LOC_EXPR)
184 #endif
185
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 */
189 {               
190     return (Expr->Flags & E_MASK_LOC) == E_LOC_LITERAL;
191 }
192 #else
193 #  define ED_IsLocLiteral(Expr)   (((Expr)->Flags & E_MASK_LOC) == E_LOC_LITERAL)
194 #endif
195
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 */
199 {
200     return (Expr->Flags & E_LOC_CONST) != 0;
201 }
202 #else
203 #  define ED_IsLocConst(Expr)  (((Expr)->Flags & E_LOC_CONST) != 0)
204 #endif
205
206 #if defined(HAVE_INLINE)
207 INLINE int ED_IsLVal (const ExprDesc* Expr)
208 /* Return true if the expression is a reference */
209 {
210     return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL;
211 }
212 #else
213 #  define ED_IsLVal(Expr)       (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL)
214 #endif
215
216 #if defined(HAVE_INLINE)
217 INLINE int ED_IsRVal (const ExprDesc* Expr)
218 /* Return true if the expression is a rvalue */
219 {
220     return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL;
221 }
222 #else
223 #  define ED_IsRVal(Expr)       (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL)
224 #endif
225
226 #if defined(HAVE_INLINE)
227 INLINE void ED_MakeLVal (ExprDesc* Expr)
228 /* Make the expression a lvalue. */
229 {
230     Expr->Flags |= E_RTYPE_LVAL;
231 }
232 #else
233 #  define ED_MakeLVal(Expr)     do { (Expr)->Flags |= E_RTYPE_LVAL; } while (0)
234 #endif
235
236 #if defined(HAVE_INLINE)
237 INLINE void ED_MakeRVal (ExprDesc* Expr)
238 /* Make the expression a rvalue. */
239 {
240     Expr->Flags &= ~E_RTYPE_LVAL;
241 }
242 #else
243 #  define ED_MakeRVal(Expr)     do { (Expr)->Flags &= ~E_RTYPE_LVAL; } while (0)
244 #endif
245
246 #if defined(HAVE_INLINE)
247 INLINE int ED_IsBitField (const ExprDesc* Expr)
248 /* Return true if the expression is a bit field */
249 {
250     return (Expr->Flags & E_BITFIELD) != 0;
251 }
252 #else
253 #  define ED_IsBitField(Expr)   (((Expr)->Flags & E_BITFIELD) != 0)
254 #endif
255
256 void ED_MakeBitField (ExprDesc* Expr, unsigned BitOffs, unsigned BitWidth);
257 /* Make this expression a bit field expression */
258
259 #if defined(HAVE_INLINE)
260 INLINE void ED_MarkForTest (ExprDesc* Expr)
261 /* Mark the expression for a test. */
262 {
263     Expr->Flags |= E_NEED_TEST;
264 }
265 #else
266 #  define ED_MarkForTest(Expr)  do { (Expr)->Flags |= E_NEED_TEST; } while (0)
267 #endif
268
269 #if defined(HAVE_INLINE)
270 INLINE int ED_NeedsTest (const ExprDesc* Expr)
271 /* Check if the expression needs a test. */
272 {
273     return (Expr->Flags & E_NEED_TEST) != 0;
274 }
275 #else
276 #  define ED_NeedsTest(Expr)    (((Expr)->Flags & E_NEED_TEST) != 0)
277 #endif
278
279 #if defined(HAVE_INLINE)
280 INLINE void ED_TestDone (ExprDesc* Expr)
281 /* Mark the expression as tested and condition codes set. */
282 {
283     Expr->Flags = (Expr->Flags & ~E_NEED_TEST) | E_CC_SET;
284 }
285 #else
286 #  define ED_TestDone(Expr)     \
287     do { (Expr)->Flags = ((Expr)->Flags & ~E_NEED_TEST) | E_CC_SET; } while (0)
288 #endif
289
290 #if defined(HAVE_INLINE)
291 INLINE int ED_IsTested (const ExprDesc* Expr)
292 /* Check if the expression has set the condition codes. */
293 {
294     return (Expr->Flags & E_CC_SET) != 0;
295 }
296 #else
297 #  define ED_IsTested(Expr)   (((Expr)->Flags & E_CC_SET) != 0)
298 #endif
299
300 #if defined(HAVE_INLINE)
301 INLINE void ED_MarkAsUntested (ExprDesc* Expr)
302 /* Mark the expression as not tested (condition codes not set). */
303 {
304     Expr->Flags &= ~E_CC_SET;
305 }
306 #else
307 #  define ED_MarkAsUntested(Expr)   do { (Expr)->Flags &= ~E_CC_SET; } while (0)
308 #endif
309
310 void ED_SetCodeRange (ExprDesc* Expr, const CodeMark* Start, const CodeMark* End);
311 /* Set the code range for this expression */
312
313 int ED_CodeRangeIsEmpty (const ExprDesc* Expr);
314 /* Return true if no code was output for this expression */
315
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.
320  */
321
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.
325  */
326
327 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type);
328 /* Make Expr an absolute const with the given value and type. */
329
330 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value);
331 /* Make Expr a constant integer expression with the given value */
332
333 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr);
334 /* Convert Expr into a rvalue which is in the primary register without an
335  * offset.
336  */
337
338 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr);
339 /* Convert Expr into a lvalue which is in the primary register without an
340  * offset.
341  */
342
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
346  * similar.
347  */
348
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.
353  */
354 {
355     return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL);
356 }
357 #else
358 #  define ED_IsConstAbs(E)      \
359         (((E)->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL))
360 #endif
361
362 int ED_IsConstAbsInt (const ExprDesc* Expr);
363 /* Return true if the expression is a constant (numeric) integer. */
364
365 int ED_IsNullPtr (const ExprDesc* Expr);
366 /* Return true if the given expression is a NULL pointer constant */
367
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.
371  */
372
373 void PrintExprDesc (FILE* F, ExprDesc* Expr);
374 /* Print an ExprDesc */
375
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 */
378
379
380
381 /* End of exprdesc.h */
382
383 #endif