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