]> git.sur5r.net Git - cc65/blob - src/cc65/exprdesc.h
Some more floating point support.
[cc65] / src / cc65 / exprdesc.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                exprdesc.h                                 */
4 /*                                                                           */
5 /*                      Expression descriptor structure                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002-2008 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 "datatype.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Data                                    */
54 /*****************************************************************************/
55
56
57
58 /* Defines for the flags field of the expression descriptor */
59 enum {
60     /* Location: Where is the value we're talking about? */
61     E_MASK_LOC          = 0x00FF,
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 */
70
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,
74
75     /* Reference? */
76     E_MASK_RTYPE        = 0x0100,
77     E_RTYPE_RVAL        = 0x0000,
78     E_RTYPE_LVAL        = 0x0100,
79
80     /* Test */
81     E_NEED_TEST         = 0x0200,       /* Expression needs a test to set cc */
82     E_CC_SET            = 0x0400        /* Condition codes are set */
83 };
84
85 /* Describe the result of an expression */
86 typedef struct ExprDesc ExprDesc;
87 struct ExprDesc {
88     struct SymEntry*    Sym;    /* Symbol table entry if known */
89     Type*               Type;   /* Type array of expression */
90     unsigned            Flags;
91     unsigned long       Name;   /* Name or label number */
92     long                IVal;   /* Integer value if expression constant */
93     Double              FVal;   /* Floating point value */
94 };
95
96
97
98 /*****************************************************************************/
99 /*                                   Code                                    */
100 /*****************************************************************************/
101
102
103
104 ExprDesc* ED_Init (ExprDesc* Expr);
105 /* Initialize an ExprDesc */
106
107 #if defined(HAVE_INLINE)
108 INLINE int ED_GetLoc (const ExprDesc* Expr)
109 /* Return the location flags from the expression */
110 {
111     return (Expr->Flags & E_MASK_LOC);
112 }
113 #else
114 #  define ED_GetLoc(Expr)       ((Expr)->Flags & E_MASK_LOC)
115 #endif
116
117 #if defined(HAVE_INLINE)
118 INLINE int ED_IsLocAbs (const ExprDesc* Expr)
119 /* Return true if the expression is an absolute value */
120 {
121     return (Expr->Flags & E_MASK_LOC) == E_LOC_ABS;
122 }
123 #else
124 #  define ED_IsLocAbs(Expr)     (((Expr)->Flags & E_MASK_LOC) == E_LOC_ABS)
125 #endif
126
127 #if defined(HAVE_INLINE)
128 INLINE int ED_IsLocRegister (const ExprDesc* Expr)
129 /* Return true if the expression is located in a register */
130 {
131     return (Expr->Flags & E_MASK_LOC) == E_LOC_REGISTER;
132 }
133 #else
134 #  define ED_IsLocRegister(Expr)    (((Expr)->Flags & E_MASK_LOC) == E_LOC_REGISTER)
135 #endif
136
137 #if defined(HAVE_INLINE)
138 INLINE int ED_IsLocStack (const ExprDesc* Expr)
139 /* Return true if the expression is located on the stack */
140 {
141     return (Expr->Flags & E_MASK_LOC) == E_LOC_STACK;
142 }
143 #else
144 #  define ED_IsLocStack(Expr)     (((Expr)->Flags & E_MASK_LOC) == E_LOC_STACK)
145 #endif
146
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 */
150 {
151     return (Expr->Flags & E_MASK_LOC) == E_LOC_PRIMARY;
152 }
153 #else
154 #  define ED_IsLocPrimary(Expr)  (((Expr)->Flags & E_MASK_LOC) == E_LOC_PRIMARY)
155 #endif
156
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 */
160 {
161     return (Expr->Flags & E_MASK_LOC) == E_LOC_EXPR;
162 }
163 #else
164 #  define ED_IsLocExpr(Expr)     (((Expr)->Flags & E_MASK_LOC) == E_LOC_EXPR)
165 #endif
166
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 */
170 {
171     return (Expr->Flags & E_MASK_LOC) == E_LOC_LITERAL;
172 }
173 #else
174 #  define ED_IsLocLiteral(Expr)   (((Expr)->Flags & E_MASK_LOC) == E_LOC_LITERAL)
175 #endif
176
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 */
180 {
181     return (Expr->Flags & E_LOC_CONST) != 0;
182 }
183 #else
184 #  define ED_IsLocConst(Expr)  (((Expr)->Flags & E_LOC_CONST) != 0)
185 #endif
186
187 #if defined(HAVE_INLINE)
188 INLINE int ED_IsLVal (const ExprDesc* Expr)
189 /* Return true if the expression is a reference */
190 {
191     return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL;
192 }
193 #else
194 #  define ED_IsLVal(Expr)       (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_LVAL)
195 #endif
196
197 #if defined(HAVE_INLINE)
198 INLINE int ED_IsRVal (const ExprDesc* Expr)
199 /* Return true if the expression is a rvalue */
200 {
201     return (Expr->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL;
202 }
203 #else
204 #  define ED_IsRVal(Expr)       (((Expr)->Flags & E_MASK_RTYPE) == E_RTYPE_RVAL)
205 #endif
206
207 #if defined(HAVE_INLINE)
208 INLINE void ED_MakeLVal (ExprDesc* Expr)
209 /* Make the expression a lvalue. */
210 {
211     Expr->Flags |= E_RTYPE_LVAL;
212 }
213 #else
214 #  define ED_MakeLVal(Expr)     do { (Expr)->Flags |= E_RTYPE_LVAL; } while (0)
215 #endif
216
217 #if defined(HAVE_INLINE)
218 INLINE void ED_MakeRVal (ExprDesc* Expr)
219 /* Make the expression a rvalue. */
220 {
221     Expr->Flags &= ~E_RTYPE_LVAL;
222 }
223 #else
224 #  define ED_MakeRVal(Expr)     do { (Expr)->Flags &= ~E_RTYPE_LVAL; } while (0)
225 #endif
226
227 #if defined(HAVE_INLINE)
228 INLINE void ED_MarkForTest (ExprDesc* Expr)
229 /* Mark the expression for a test. */
230 {
231     Expr->Flags |= E_NEED_TEST;
232 }
233 #else
234 #  define ED_MarkForTest(Expr)  do { (Expr)->Flags |= E_NEED_TEST; } while (0)
235 #endif
236
237 #if defined(HAVE_INLINE)
238 INLINE int ED_NeedsTest (const ExprDesc* Expr)
239 /* Check if the expression needs a test. */
240 {
241     return (Expr->Flags & E_NEED_TEST) != 0;
242 }
243 #else
244 #  define ED_NeedsTest(Expr)    (((Expr)->Flags & E_NEED_TEST) != 0)
245 #endif
246
247 #if defined(HAVE_INLINE)
248 INLINE void ED_TestDone (ExprDesc* Expr)
249 /* Mark the expression as tested and condition codes set. */
250 {
251     Expr->Flags = (Expr->Flags & ~E_NEED_TEST) | E_CC_SET;
252 }
253 #else
254 #  define ED_TestDone(Expr)     \
255     do { (Expr)->Flags = ((Expr)->Flags & ~E_NEED_TEST) | E_CC_SET; } while (0)
256 #endif
257
258 #if defined(HAVE_INLINE)
259 INLINE int ED_IsTested (const ExprDesc* Expr)
260 /* Check if the expression has set the condition codes. */
261 {
262     return (Expr->Flags & E_CC_SET) != 0;
263 }
264 #else
265 #  define ED_IsTested(Expr)   (((Expr)->Flags & E_CC_SET) != 0)
266 #endif
267
268 #if defined(HAVE_INLINE)
269 INLINE void ED_MarkAsUntested (ExprDesc* Expr)
270 /* Mark the expression as not tested (condition codes not set). */
271 {
272     Expr->Flags &= ~E_CC_SET;
273 }
274 #else
275 #  define ED_MarkAsUntested(Expr)   do { (Expr)->Flags &= ~E_CC_SET; } while (0)
276 #endif
277
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.
282  */
283
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.
287  */
288
289 ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type);
290 /* Make Expr an absolute const with the given value and type. */
291
292 ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value);
293 /* Make Expr a constant integer expression with the given value */
294
295 ExprDesc* ED_MakeRValExpr (ExprDesc* Expr);
296 /* Convert Expr into a rvalue which is in the primary register without an
297  * offset.
298  */
299
300 ExprDesc* ED_MakeLValExpr (ExprDesc* Expr);
301 /* Convert Expr into a lvalue which is in the primary register without an
302  * offset.
303  */
304
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
308  * similar.
309  */
310
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.
315  */
316 {
317     return (Expr->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL);
318 }
319 #else
320 #  define ED_IsConstAbs(E)      \
321         (((E)->Flags & (E_MASK_LOC|E_MASK_RTYPE)) == (E_LOC_ABS|E_RTYPE_RVAL))
322 #endif
323
324 int ED_IsConstAbsInt (const ExprDesc* Expr);
325 /* Return true if the expression is a constant (numeric) integer. */
326
327 int ED_IsNullPtr (const ExprDesc* Expr);
328 /* Return true if the given expression is a NULL pointer constant */
329
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.
333  */
334
335 void PrintExprDesc (FILE* F, ExprDesc* Expr);
336 /* Print an ExprDesc */
337
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 */
340
341
342
343 /* End of exprdesc.h */
344 #endif
345
346
347