]> git.sur5r.net Git - cc65/blob - src/cc65/exprnode.h
More optimizations
[cc65] / src / cc65 / exprnode.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                exprnode.h                                 */
4 /*                                                                           */
5 /*             Expression node structure for the cc65 C compiler             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 EXPRNODE_H
37 #define EXPRNODE_H
38
39
40
41 /* common */
42 #include "coll.h"
43
44 /* cc65 */
45 #include "datatype.h"
46
47
48
49 /*****************************************************************************/
50 /*                                 Forwards                                  */
51 /*****************************************************************************/
52
53
54
55 struct ExprHeap;
56 struct SymEntry;
57
58
59
60 /*****************************************************************************/
61 /*                                   Data                                    */
62 /*****************************************************************************/
63
64
65
66 /* Node types */
67 typedef enum {
68
69     /* Bits encoding the type of the objects stored in List for this
70      * particular node.
71      */
72     NT_LIST_NONE        = 0x0000,       /* No items */
73     NT_LIST_EXPR        = 0x0100,       /* Items are expression nodes */
74     NT_LIST_SYM         = 0x0200,       /* Items are symbol table entries */
75     NT_LIST_STRING      = 0x0300,       /* List item are character strings */
76     NT_MASK_LIST        = 0x0300,
77
78     /* Two bits telling if this is a leaf or a branch */
79     NT_LEAF             = 0x0000,       /* Leaf */
80     NT_BRANCH           = 0x8000,       /* Branch */
81     NT_MASK_LEAF        = 0x8000,
82
83     /* Special node type */
84     NT_NONE             = 0x0000,       /* None (invalid) op */
85
86     /* Leaves */
87     NT_SYM              = 0x0001 | NT_LEAF   | NT_LIST_SYM,     /* Symbol */
88     NT_CONST            = 0x0002 | NT_LEAF   | NT_LIST_NONE,    /* A constant of some sort */
89     NT_ASM              = 0x0003 | NT_LEAF   | NT_LIST_STRING,  /* Inline assembler */
90
91     NT_REG_A            = 0x0005 | NT_LEAF   | NT_LIST_NONE,    /* A register */
92     NT_REG_X            = 0x0006 | NT_LEAF   | NT_LIST_NONE,    /* X register */
93     NT_REG_Y            = 0x0007 | NT_LEAF   | NT_LIST_NONE,    /* Y register */
94     NT_REG_AX           = 0x0008 | NT_LEAF   | NT_LIST_NONE,    /* AX register */
95     NT_REG_EAX          = 0x0009 | NT_LEAF   | NT_LIST_NONE,    /* EAX register */
96
97     /* Branches */
98     NT_ARRAY_SUBSCRIPT  = 0x0010 | NT_BRANCH | NT_LIST_EXPR,    /* Array subscript */
99     NT_STRUCT_ACCESS    = 0x0011 | NT_BRANCH | NT_LIST_EXPR,    /* Access of a struct field */
100     NT_STRUCTPTR_ACCESS = 0x0012 | NT_BRANCH | NT_LIST_EXPR,    /* Access via struct ptr */
101     NT_FUNCTION_CALL    = 0x0013 | NT_BRANCH | NT_LIST_EXPR,    /* Call a function */
102     NT_TYPECAST         = 0x0014 | NT_BRANCH | NT_LIST_EXPR,    /* A cast */
103     NT_ADDRESS          = 0x0015 | NT_BRANCH | NT_LIST_EXPR,    /* Address operator (&) */
104     NT_INDIRECT         = 0x0016 | NT_BRANCH | NT_LIST_EXPR,    /* Indirection operator (*) */
105
106     NT_UNARY_MINUS      = 0x0018 | NT_BRANCH | NT_LIST_EXPR,
107     NT_COMPLEMENT       = 0x0019 | NT_BRANCH | NT_LIST_EXPR,    /* ~ */
108     NT_BOOL_NOT         = 0x001A | NT_BRANCH | NT_LIST_EXPR,    /* ! */
109
110     NT_PLUS             = 0x001B | NT_BRANCH | NT_LIST_EXPR,    /* + */
111     NT_MINUS            = 0x001C | NT_BRANCH | NT_LIST_EXPR,    /* - */
112     NT_MUL              = 0x001D | NT_BRANCH | NT_LIST_EXPR,    /* * */
113     NT_DIV              = 0x001E | NT_BRANCH | NT_LIST_EXPR,    /* / */
114     NT_SHL              = 0x001F | NT_BRANCH | NT_LIST_EXPR,    /* << */
115     NT_SHR              = 0x0020 | NT_BRANCH | NT_LIST_EXPR,    /* >> */
116     NT_AND              = 0x0021 | NT_BRANCH | NT_LIST_EXPR,    /* & */
117     NT_OR               = 0x0022 | NT_BRANCH | NT_LIST_EXPR,    /* | */
118     NT_XOR              = 0x0023 | NT_BRANCH | NT_LIST_EXPR,    /* ^ */
119
120     NT_TERNARY          = 0x0024 | NT_BRANCH | NT_LIST_EXPR,    /* ?: */
121
122     NT_ASSIGN           = 0x0025 | NT_BRANCH | NT_LIST_EXPR,    /* = */
123     NT_PLUS_ASSIGN      = 0x0026 | NT_BRANCH | NT_LIST_EXPR,    /* += */
124     NT_MINUS_ASSIGN     = 0x0027 | NT_BRANCH | NT_LIST_EXPR,    /* -= */
125     NT_MUL_ASSIGN       = 0x0028 | NT_BRANCH | NT_LIST_EXPR,    /* *= */
126     NT_DIV_ASSIGN       = 0x0029 | NT_BRANCH | NT_LIST_EXPR,    /* /= */
127     NT_SHL_ASSIGN       = 0x002A | NT_BRANCH | NT_LIST_EXPR,    /* <<= */
128     NT_SHR_ASSIGN       = 0x002B | NT_BRANCH | NT_LIST_EXPR,    /* >>= */
129     NT_AND_ASSIGN       = 0x002C | NT_BRANCH | NT_LIST_EXPR,    /* &= */
130     NT_OR_ASSIGN        = 0x002D | NT_BRANCH | NT_LIST_EXPR,    /* |= */
131     NT_XOR_ASSIGN       = 0x002E | NT_BRANCH | NT_LIST_EXPR,    /* ^= */
132
133     NT_PRE_DEC          = 0x002F | NT_BRANCH | NT_LIST_EXPR,    /* -- */
134     NT_POST_DEC         = 0x0030 | NT_BRANCH | NT_LIST_EXPR,    /* -- */
135     NT_PRE_INC          = 0x0031 | NT_BRANCH | NT_LIST_EXPR,    /* ++ */
136     NT_POST_INC         = 0x0032 | NT_BRANCH | NT_LIST_EXPR,    /* ++ */
137
138     NT_BOOL_OR          = 0x0033 | NT_BRANCH | NT_LIST_EXPR,    /* || */
139     NT_BOOL_AND         = 0x0034 | NT_BRANCH | NT_LIST_EXPR,    /* && */
140
141     NT_EQ               = 0x0035 | NT_BRANCH | NT_LIST_EXPR,    /* == */
142     NT_NE               = 0x0036 | NT_BRANCH | NT_LIST_EXPR,    /* != */
143     NT_LT               = 0x0037 | NT_BRANCH | NT_LIST_EXPR,    /* < */
144     NT_LE               = 0x0038 | NT_BRANCH | NT_LIST_EXPR,    /* <= */
145     NT_GT               = 0x0039 | NT_BRANCH | NT_LIST_EXPR,    /* > */
146     NT_GE               = 0x003A | NT_BRANCH | NT_LIST_EXPR,    /* >= */
147
148     NT_MASK_TYPE        = 0x00FF
149
150 } nodetype_t;
151
152
153
154 /* Struct describing one node in an expression tree */
155 typedef struct ExprNode ExprNode;
156 struct ExprNode {
157
158     /* Management data */
159     union {
160         struct ExprHeap*        Owner;  /* Heap, this node is in */
161         struct ExprNode*        Next;   /* Next in free list */
162     } MData;
163
164     Collection                  List;   /* List of subexpressions */
165     nodetype_t                  NT;     /* Node type */
166     type*                       Type;   /* Resulting type */
167     int                         LValue; /* True if this is an lvalue */
168
169     /* Attributes */
170     long                        IVal;   /* Constant int value if any */
171     double                      FVal;   /* Constant float value if any */
172 };
173
174
175
176 /* Predefined indices for node items in List */
177 enum {
178     IDX_LEFT    = 0,
179     IDX_RIGHT   = 1,
180     IDX_SYM     = 0
181 };
182
183 /* Some other constants for better readability */
184 enum {
185     RVALUE      = 0,
186     LVALUE      = 1
187 };
188
189
190
191 /*****************************************************************************/
192 /*                                   Code                                    */
193 /*****************************************************************************/
194
195
196
197 ExprNode* InitExprNode (ExprNode* E, nodetype_t NT, type* Type,
198                         int LValue, struct ExprHeap* Owner);
199 /* Initialize a new expression node */
200
201 void* GetItem (ExprNode* N, unsigned Index);
202 /* Return one of the items from the nodes item list */
203
204 void AppendItem (ExprNode* N, void* Item);
205 /* Append an item to the nodes item list */
206
207 void SetItem (ExprNode* N, void* Item, unsigned Index);
208 /* Set a specific node item. The item list is filled with null pointers as
209  * needed.
210  */
211
212 ExprNode* GetLeftNode (ExprNode* N);
213 /* Get the left sub-node from the list */
214
215 void SetLeftNode (ExprNode* Root, ExprNode* Left);
216 /* Set the left node in Root */
217
218 ExprNode* GetRightNode (ExprNode* N);
219 /* Get the right sub-node from the list */
220
221 void SetRightNode (ExprNode* Root, ExprNode* Right);
222 /* Set the right node in Root */
223
224 struct SymEntry* GetNodeSym (ExprNode* N);
225 /* Get the symbol entry for a NT_SYM node */
226
227 void SetNodeSym (ExprNode* N, struct SymEntry* Sym);
228 /* Set the symbol entry in a NT_SYM node */
229
230 int IsLeafNode (const ExprNode* E);
231 /* Return true if this is a leaf node */
232
233 int IsBranchNode (const ExprNode* E);
234 /* Return true if this is a branch node */
235
236
237
238 /* End of exprnode.h */
239
240 #endif
241
242
243