]> git.sur5r.net Git - cc65/blob - src/cc65/exprdesc.h
bdae371b80184003d13049dcf5d91ec577d5535a
[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 #define E_MREG          0x0110U /* Special: Expression is primary register */
59 #define E_MGLOBAL       0x0080U /* Reference to static variable */
60 #define E_MLOCAL        0x0040U /* Reference to local variable (stack offset) */
61 #define E_MCONST        0x0020U /* Constant value */
62 #define E_MEXPR         0x0010U /* Result is in primary register */
63 #define E_MEOFFS        0x0011U /* Base is in primary register, const offset */
64
65 #define E_MCTYPE        0x0007U /* Type of a constant */
66 #define E_TCONST        0x0000U /* Constant */
67 #define E_TGLAB         0x0001U /* Global label */
68 #define E_TLIT          0x0002U /* Literal of some kind */
69 #define E_TLOFFS        0x0003U /* Constant stack offset */
70 #define E_TLLAB         0x0004U /* Local label */
71 #define E_TREGISTER     0x0005U /* Register variable */
72
73 #define E_RVAL          0x0000U /* Expression node is a value */
74 #define E_LVAL          0x1000U /* Expression node is a reference */
75
76 /* Defines for the test field of the expression descriptor */
77 #define E_CC            0x0001U /* expr has set cond codes apropos result value */
78 #define E_FORCETEST     0x0002U /* if expr has NOT set CC, force a test */
79
80 /* Describe the result of an expression */
81 typedef struct ExprDesc ExprDesc;
82 struct ExprDesc {
83     struct SymEntry*    Sym;     /* Symbol table entry if known */
84     type*               Type;    /* Type array of expression */
85     long                ConstVal;/* Value if expression constant */
86     unsigned short      Flags;
87     unsigned short      Test;    /* */
88     unsigned long       Name;    /* Name or label number */
89 };
90
91
92
93 /*****************************************************************************/
94 /*                                   Code                                    */
95 /*****************************************************************************/
96
97
98
99 #if defined(HAVE_INLINE)
100 INLINE ExprDesc* InitExprDesc (ExprDesc* Expr)
101 /* Initialize an ExprDesc */
102 {
103     return memset (Expr, 0, sizeof (*Expr));
104 }
105 #else
106 #  define InitExprDesc(E)       memset ((E), 0, sizeof (*(E)))
107 #endif
108
109 #if defined(HAVE_INLINE)
110 INLINE int ED_IsLVal (const ExprDesc* Expr)
111 /* Return true if the expression is a reference */
112 {
113     return (Expr->Flags & E_LVAL) != 0;
114 }
115 #else
116 #  define ED_IsLVal(Expr)       (((Expr)->Flags & E_LVAL) != 0)
117 #endif
118
119 #if defined(HAVE_INLINE)
120 INLINE int ED_IsRVal (const ExprDesc* Expr)
121 /* Return true if the expression is a rvalue */
122 {
123     return (Expr->Flags & E_LVAL) == 0;
124 }
125 #else
126 #  define ED_IsRVal(Expr)       (((Expr)->Flags & E_LVAL) == 0)
127 #endif
128
129 #if defined(HAVE_INLINE)
130 INLINE int ED_SetValType (ExprDesc* Expr, int Ref)
131 /* Set the reference flag for an expression and return it (the flag) */
132 {
133     Expr->Flags = Ref? (Expr->Flags | E_LVAL) : (Expr->Flags & ~E_LVAL);
134     return Ref;
135 }
136 #else
137 /* Beware: Just one occurance of R below, since it may have side effects! */
138 #  define ED_SetValType(E, R)                                                   \
139         (((E)->Flags = (R)? ((E)->Flags | E_LVAL) : ((E)->Flags & ~E_LVAL)),    \
140         ED_IsLVal (E))
141 #endif
142
143 #if defined(HAVE_INLINE)
144 INLINE int ED_MakeLVal (ExprDesc* Expr)
145 /* Make the expression a lvalue and return true */
146 {
147     return ED_SetValType (Expr, 1);
148 }
149 #else
150 #  define ED_MakeLVal(Expr)       ED_SetValType (Expr, 1)
151 #endif
152
153 #if defined(HAVE_INLINE)
154 INLINE int ED_MakeRVal (ExprDesc* Expr)
155 /* Make the expression a rvalue and return false */
156 {
157     return ED_SetValType (Expr, 0);
158 }
159 #else
160 #  define ED_MakeRVal(Expr)       ED_SetValType (Expr, 0)
161 #endif
162
163 ExprDesc* ED_MakeConstInt (ExprDesc* Expr, long Value);
164 /* Make Expr a constant integer expression with the given value */
165
166 void PrintExprDesc (FILE* F, ExprDesc* Expr);
167 /* Print an ExprDesc */
168
169 type* ReplaceType (ExprDesc* Expr, const type* NewType);
170 /* Replace the type of Expr by a copy of Newtype and return the old type string */
171
172
173
174 /* End of exprdesc.h */
175 #endif
176
177
178