1 /*****************************************************************************/
5 /* Expression node structure for the cc65 C compiler */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
60 /*****************************************************************************/
62 /*****************************************************************************/
69 /* Bits encoding the type of the objects stored in List for this
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,
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,
83 /* Special node type */
84 NT_NONE = 0x0000, /* None (invalid) op */
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 */
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 */
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 (*) */
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, /* ! */
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, /* ^ */
120 NT_TERNARY = 0x0024 | NT_BRANCH | NT_LIST_EXPR, /* ?: */
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, /* ^= */
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, /* ++ */
138 NT_BOOL_OR = 0x0033 | NT_BRANCH | NT_LIST_EXPR, /* || */
139 NT_BOOL_AND = 0x0034 | NT_BRANCH | NT_LIST_EXPR, /* && */
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, /* >= */
148 NT_MASK_TYPE = 0x00FF
154 /* Struct describing one node in an expression tree */
155 typedef struct ExprNode ExprNode;
158 /* Management data */
160 struct ExprHeap* Owner; /* Heap, this node is in */
161 struct ExprNode* Next; /* Next in free list */
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 */
170 long IVal; /* Constant int value if any */
171 double FVal; /* Constant float value if any */
176 /* Predefined indices for node items in List */
183 /* Some other constants for better readability */
191 /*****************************************************************************/
193 /*****************************************************************************/
197 ExprNode* InitExprNode (ExprNode* E, nodetype_t NT, type* Type,
198 int LValue, struct ExprHeap* Owner);
199 /* Initialize a new expression node */
201 void* GetItem (ExprNode* N, unsigned Index);
202 /* Return one of the items from the nodes item list */
204 void AppendItem (ExprNode* N, void* Item);
205 /* Append an item to the nodes item list */
207 void SetItem (ExprNode* N, void* Item, unsigned Index);
208 /* Set a specific node item. The item list is filled with null pointers as
212 ExprNode* GetLeftNode (ExprNode* N);
213 /* Get the left sub-node from the list */
215 void SetLeftNode (ExprNode* Root, ExprNode* Left);
216 /* Set the left node in Root */
218 ExprNode* GetRightNode (ExprNode* N);
219 /* Get the right sub-node from the list */
221 void SetRightNode (ExprNode* Root, ExprNode* Right);
222 /* Set the right node in Root */
224 struct SymEntry* GetNodeSym (ExprNode* N);
225 /* Get the symbol entry for a NT_SYM node */
227 void SetNodeSym (ExprNode* N, struct SymEntry* Sym);
228 /* Set the symbol entry in a NT_SYM node */
230 int IsLeafNode (const ExprNode* E);
231 /* Return true if this is a leaf node */
233 int IsBranchNode (const ExprNode* E);
234 /* Return true if this is a branch node */
238 /* End of exprnode.h */