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 /*****************************************************************************/
45 /*****************************************************************************/
47 /*****************************************************************************/
51 ExprNode* InitExprNode (ExprNode* E, nodetype_t NT, type* Type,
52 int LValue, struct ExprHeap* Owner)
53 /* Initialize a new expression node */
55 /* Intialize basic data */
56 E->MData.Owner = Owner;
63 /* Initialize the expression list in the node */
64 InitCollection (&E->List);
66 /* Return the node just initialized */
72 void* GetItem (ExprNode* N, unsigned Index)
73 /* Return one of the items from the nodes item list */
75 return CollAt (&N->List, Index);
80 void AppendItem (ExprNode* N, void* Item)
81 /* Append an item to the nodes item list */
83 CollAppend (&N->List, Item);
88 void SetItem (ExprNode* N, void* Item, unsigned Index)
89 /* Set a specific node item. The item list is filled with null pointers as
93 if (Index >= CollCount (&N->List)) {
94 /* Fill up with NULL pointers */
95 while (Index >= CollCount (&N->List)) {
96 CollAppend (&N->List, 0);
98 /* Append the new item */
99 CollAppend (&N->List, Item);
101 /* There is an item with this index, replace it */
102 CollReplace (&N->List, Item, Index);
108 ExprNode* GetNode (ExprNode* N, unsigned Index)
109 /* Get one of the sub-nodes from the list */
111 return GetNode (N, Index);
116 ExprNode* GetLeftNode (ExprNode* N)
117 /* Get the left sub-node from the list */
119 return GetNode (N, IDX_LEFT);
124 void SetLeftNode (ExprNode* Root, ExprNode* Left)
125 /* Set the left node in Root */
127 SetItem (Root, Left, IDX_LEFT);
132 ExprNode* GetRightNode (ExprNode* N)
133 /* Get the right sub-node from the list */
135 return GetNode (N, IDX_RIGHT);
140 void SetRightNode (ExprNode* Root, ExprNode* Right)
141 /* Set the right node in Root */
143 SetItem (Root, Right, IDX_RIGHT);
148 struct SymEntry* GetNodeSym (ExprNode* N)
149 /* Get the symbol entry for a NT_SYM node */
151 return (struct SymEntry*) GetItem (N, IDX_SYM);
156 void SetNodeSym (ExprNode* N, struct SymEntry* Sym)
157 /* Set the symbol entry in a NT_SYM node */
159 SetItem (N, Sym, IDX_SYM);
164 int IsLeafNode (const ExprNode* E)
165 /* Return true if this is a leaf node */
167 return (E->NT & NT_MASK_LEAF) == NT_LEAF;
172 int IsBranchNode (const ExprNode* E)
173 /* Return true if this is a branch node */
175 return (E->NT & NT_MASK_LEAF) == NT_BRANCH;
180 void DumpExpr (FILE* F, const ExprNode* E)
181 /* Dump an expression in UPN notation to the given file */
183 if (IsLeafNode (E)) {
194 /* A constant of some sort */
195 if (IsClassInt (E->Type)) {
196 fprintf (F, "%0*lX ", SizeOf (E->Type), E->IVal);
197 } else if (IsClassFloat (E->Type)) {
198 fprintf (F, "%f ", E->FVal);
200 Internal ("Unknown type for NT_CONST");
205 /* Inline assembler */
211 fprintf (F, "REG_A ");
216 fprintf (F, "REG_X ");
221 fprintf (F, "REG_Y ");
226 fprintf (F, "REG_AX ");
231 fprintf (F, "REG_EAX ");
235 Internal ("Unknown node type: %04X", E->NT);
244 /* Dump the operands */
245 switch (E->NT & NT_MASK_LIST) {
248 Count = CollCount (&E->List);
249 for (I = 0; I < Count; ++I) {
250 DumpExpr (F, (const ExprNode*) CollConstAt (&E->List, I));
255 Internal ("Operator with LIST != NT_LIST_EXPR");
259 /* Dump the operator */
262 case NT_ARRAY_SUBSCRIPT:
263 /* Array subscript */
267 case NT_STRUCT_ACCESS:
268 /* Access of a struct field */
272 case NT_STRUCTPTR_ACCESS:
273 /* Access via struct ptr */
277 case NT_FUNCTION_CALL:
278 /* Call a function */
279 fprintf (F, "CALL ");
284 fprintf (F, "CAST ");
288 /* Address operator (&) */
289 fprintf (F, "ADDR ");
293 /* Indirection operator (*) */
294 fprintf (F, "FETCH ");
372 case NT_MINUS_ASSIGN:
473 Internal ("Unknown node type: %04X", E->NT);