2 * $Id: astnode.c,v 1.15 2007/08/12 18:58:12 khansen Exp $
4 * Revision 1.15 2007/08/12 18:58:12 khansen
5 * ability to generate pure 6502 binary (--pure-binary switch)
7 * Revision 1.14 2007/08/10 20:21:02 khansen
8 * *** empty log message ***
10 * Revision 1.13 2007/08/09 22:05:49 khansen
11 * general-purpose flags
13 * Revision 1.12 2007/08/07 21:12:16 khansen
16 * Revision 1.11 2007/07/22 13:33:26 khansen
17 * convert tabs to whitespaces
19 * Revision 1.10 2004/12/29 21:44:04 kenth
21 * added create_index()
23 * Revision 1.9 2004/12/19 19:58:23 kenth
26 * Revision 1.8 2004/12/19 09:53:46 kenth
27 * added create_align()
29 * Revision 1.7 2004/12/18 16:56:12 kenth
30 * create_extrn() takes unit id
32 * Revision 1.6 2004/12/16 13:19:07 kenth
33 * astnode_create_label() takes datatype argument
35 * Revision 1.5 2004/12/14 01:48:57 kenth
38 * Revision 1.4 2004/12/11 02:01:10 kenth
39 * added forward/backward branching
41 * Revision 1.3 2004/12/09 11:17:59 kenth
42 * added: warning, error nodes
44 * Revision 1.2 2004/12/06 04:52:05 kenth
45 * Major updates (xorcyst 1.1.0)
47 * Revision 1.1 2004/06/30 07:55:28 kenth
53 * (C) 2004 Kent Hansen
55 * The XORcyst is free software; you can redistribute it and/or modify
56 * it under the terms of the GNU General Public License as published by
57 * the Free Software Foundation; either version 2 of the License, or
58 * (at your option) any later version.
60 * The XORcyst is distributed in the hope that it will be useful,
61 * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 * GNU General Public License for more details.
65 * You should have received a copy of the GNU General Public License
66 * along with The XORcyst; if not, write to the Free Software
67 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
71 * The result of parsing an assembly file is an Abstract Syntax Tree (AST).
72 * Such a tree consists of AST nodes.
73 * This file contains the code to manipulate such nodes.
83 #define SAFE_FREE(a) if (a) { free(a); a = NULL; }
85 /*---------------------------------------------------------------------------*/
86 /* Functions to convert and print astnodes as string.
87 These are useful when debugging syntax trees.
91 * Gets string representation of an addressing mode.
92 * @param am Addressing mode (enumerated type)
93 * @return String representation of am
95 const char *addressing_mode_to_string(addressing_mode am
)
98 case IMPLIED_MODE
: return "IMPLIED_MODE";
99 case ACCUMULATOR_MODE
: return "ACCUMULATOR_MODE";
100 case IMMEDIATE_MODE
: return "IMMEDIATE_MODE";
101 case ZEROPAGE_MODE
: return "ZEROPAGE_MODE";
102 case ZEROPAGE_X_MODE
: return "ZEROPAGE_X_MODE";
103 case ZEROPAGE_Y_MODE
: return "ZEROPAGE_Y_MODE";
104 case ABSOLUTE_MODE
: return "ABSOLUTE_MODE";
105 case ABSOLUTE_X_MODE
: return "ABSOLUTE_X_MODE";
106 case ABSOLUTE_Y_MODE
: return "ABSOLUTE_Y_MODE";
107 case PREINDEXED_INDIRECT_MODE
: return "PREINDEXED_INDIRECT_MODE";
108 case POSTINDEXED_INDIRECT_MODE
: return "POSTINDEXED_INDIRECT_MODE";
109 case INDIRECT_MODE
: return "INDIRECT_MODE";
110 case RELATIVE_MODE
: return "RELATIVE_MODE";
111 case INVALID_MODE
: assert(0); break;
113 return "addressing_mode_to_string: invalid addressing mode";
117 * Gets string representation of an instruction mnemonic.
118 * @param im Instruction mnemonic (enumerated type)
119 * @return String representation of im
121 const char *instr_mnemonic_to_string(instr_mnemonic im
)
124 case ADC_MNEMONIC
: return "ADC_MNEMONIC";
125 case AND_MNEMONIC
: return "AND_MNEMONIC";
126 case ASL_MNEMONIC
: return "ASL_MNEMONIC";
127 case BCC_MNEMONIC
: return "BCC_MNEMONIC";
128 case BCS_MNEMONIC
: return "BCS_MNEMONIC";
129 case BEQ_MNEMONIC
: return "BEQ_MNEMONIC";
130 case BIT_MNEMONIC
: return "BIT_MNEMONIC";
131 case BMI_MNEMONIC
: return "BMI_MNEMONIC";
132 case BNE_MNEMONIC
: return "BNE_MNEMONIC";
133 case BPL_MNEMONIC
: return "BPL_MNEMONIC";
134 case BRK_MNEMONIC
: return "BRK_MNEMONIC";
135 case BVC_MNEMONIC
: return "BVC_MNEMONIC";
136 case BVS_MNEMONIC
: return "BVS_MNEMONIC";
137 case CLC_MNEMONIC
: return "CLC_MNEMONIC";
138 case CLD_MNEMONIC
: return "CLD_MNEMONIC";
139 case CLI_MNEMONIC
: return "CLI_MNEMONIC";
140 case CLV_MNEMONIC
: return "CLV_MNEMONIC";
141 case CMP_MNEMONIC
: return "CMP_MNEMONIC";
142 case CPX_MNEMONIC
: return "CPX_MNEMONIC";
143 case CPY_MNEMONIC
: return "CPY_MNEMONIC";
144 case DEC_MNEMONIC
: return "DEC_MNEMONIC";
145 case DEX_MNEMONIC
: return "DEX_MNEMONIC";
146 case DEY_MNEMONIC
: return "DEY_MNEMONIC";
147 case EOR_MNEMONIC
: return "EOR_MNEMONIC";
148 case INC_MNEMONIC
: return "INC_MNEMONIC";
149 case INX_MNEMONIC
: return "INX_MNEMONIC";
150 case INY_MNEMONIC
: return "INY_MNEMONIC";
151 case JMP_MNEMONIC
: return "JMP_MNEMONIC";
152 case JSR_MNEMONIC
: return "JSR_MNEMONIC";
153 case LDA_MNEMONIC
: return "LDA_MNEMONIC";
154 case LDX_MNEMONIC
: return "LDX_MNEMONIC";
155 case LDY_MNEMONIC
: return "LDY_MNEMONIC";
156 case LSR_MNEMONIC
: return "LSR_MNEMONIC";
157 case NOP_MNEMONIC
: return "NOP_MNEMONIC";
158 case ORA_MNEMONIC
: return "ORA_MNEMONIC";
159 case PHA_MNEMONIC
: return "PHA_MNEMONIC";
160 case PHP_MNEMONIC
: return "PHP_MNEMONIC";
161 case PLA_MNEMONIC
: return "PLA_MNEMONIC";
162 case PLP_MNEMONIC
: return "PLP_MNEMONIC";
163 case ROL_MNEMONIC
: return "ROL_MNEMONIC";
164 case ROR_MNEMONIC
: return "ROR_MNEMONIC";
165 case RTI_MNEMONIC
: return "RTI_MNEMONIC";
166 case RTS_MNEMONIC
: return "RTS_MNEMONIC";
167 case SBC_MNEMONIC
: return "SBC_MNEMONIC";
168 case SEC_MNEMONIC
: return "SEC_MNEMONIC";
169 case SED_MNEMONIC
: return "SED_MNEMONIC";
170 case SEI_MNEMONIC
: return "SEI_MNEMONIC";
171 case STA_MNEMONIC
: return "STA_MNEMONIC";
172 case STX_MNEMONIC
: return "STX_MNEMONIC";
173 case STY_MNEMONIC
: return "STY_MNEMONIC";
174 case TAX_MNEMONIC
: return "TAX_MNEMONIC";
175 case TAY_MNEMONIC
: return "TAY_MNEMONIC";
176 case TSX_MNEMONIC
: return "TSX_MNEMONIC";
177 case TXA_MNEMONIC
: return "TXA_MNEMONIC";
178 case TXS_MNEMONIC
: return "TXS_MNEMONIC";
179 case TYA_MNEMONIC
: return "TYA_MNEMONIC";
181 return "instr_mnemonic_to_string: invalid mnemonic";
185 * Gets string representation of an astnode type.
186 * @param at Node type
187 * @return String representation of at
189 const char *astnode_type_to_string(astnode_type at
) {
191 case NULL_NODE
: return "NULL_NODE";
192 case INTEGER_NODE
: return "INTEGER_NODE";
193 case STRING_NODE
: return "STRING_NODE";
194 case IDENTIFIER_NODE
: return "IDENTIFIER_NODE";
195 case DATA_NODE
: return "DATA_NODE";
196 case STORAGE_NODE
: return "STORAGE_NODE";
197 case MACRO_DECL_NODE
: return "MACRO_DECL_NODE";
198 case MACRO_NODE
: return "MACRO_NODE";
199 case ARITHMETIC_NODE
: return "ARITHMETIC_NODE";
200 case IF_NODE
: return "IF_NODE";
201 case CASE_NODE
: return "CASE_NODE";
202 case DEFAULT_NODE
: return "DEFAULT_NODE";
203 case IFDEF_NODE
: return "IFDEF_NODE";
204 case IFNDEF_NODE
: return "IFNDEF_NODE";
205 case INCSRC_NODE
: return "INCSRC_NODE";
206 case INCBIN_NODE
: return "INCBIN_NODE";
207 case EQU_NODE
: return "EQU_NODE";
208 case ASSIGN_NODE
: return "ASSIGN_NODE";
209 case ALIGN_NODE
: return "ALIGN_NODE";
210 case INSTRUCTION_NODE
: return "INSTRUCTION_NODE";
211 case FILE_PATH_NODE
: return "FILE_PATH_NODE";
212 case CURRENT_PC_NODE
: return "CURRENT_PC_NODE";
213 case LIST_NODE
: return "LIST_NODE";
214 case LABEL_NODE
: return "LABEL_NODE";
215 case LOCAL_LABEL_NODE
: return "LOCAL_LABEL_NODE";
216 case LOCAL_ID_NODE
: return "LOCAL_ID_NODE";
217 case BINARY_NODE
: return "BINARY_NODE";
218 case PUBLIC_NODE
: return "PUBLIC_NODE";
219 case EXTRN_NODE
: return "EXTRN_NODE";
220 case DATASEG_NODE
: return "DATASEG_NODE";
221 case CODESEG_NODE
: return "CODESEG_NODE";
222 case CHARMAP_NODE
: return "CHARMAP_NODE";
223 case STRUC_NODE
: return "STRUC_NODE";
224 case STRUC_DECL_NODE
: return "STRUC_DECL_NODE";
225 case UNION_DECL_NODE
: return "UNION_DECL_NODE";
226 case ENUM_DECL_NODE
: return "ENUM_DECL_NODE";
227 case RECORD_DECL_NODE
: return "RECORD_DECL_NODE";
228 case BITFIELD_DECL_NODE
:return "BITFIELD_DECL_NODE";
229 case DOT_NODE
: return "DOT_NODE";
230 case SIZEOF_NODE
: return "SIZEOF_NODE";
231 case DATATYPE_NODE
: return "DATATYPE_NODE";
232 case VAR_DECL_NODE
: return "VAR_DECL_NODE";
233 case SCOPE_NODE
: return "SCOPE_NODE";
234 case PROC_NODE
: return "PROC_NODE";
235 case REPT_NODE
: return "REPT_NODE";
236 case WHILE_NODE
: return "WHILE_NODE";
237 case MESSAGE_NODE
: return "MESSAGE_NODE";
238 case WARNING_NODE
: return "WARNING_NODE";
239 case ERROR_NODE
: return "ERROR_NODE";
240 case FORWARD_BRANCH_DECL_NODE
: return "FORWARD_BRANCH_DECL_NODE";
241 case BACKWARD_BRANCH_DECL_NODE
: return "BACKWARD_BRANCH_DECL_NODE";
242 case FORWARD_BRANCH_NODE
: return "FORWARD_BRANCH_NODE";
243 case BACKWARD_BRANCH_NODE
: return "BACKWARD_BRANCH_NODE";
244 case MASK_NODE
: return "MASK_NODE";
245 case INDEX_NODE
: return "INDEX_NODE";
246 case ORG_NODE
: return "ORG_NODE";
247 case TOMBSTONE_NODE
: return "TOMBSTONE_NODE";
249 return "astnode_type_to_string: invalid type";
253 * Gets string representation of a datatype.
255 * @return String representation of dt
257 const char *datatype_to_string(const astnode
*dt
)
259 switch (dt
->datatype
) {
260 case BYTE_DATATYPE
: return "BYTE_DATATYPE";
261 case CHAR_DATATYPE
: return "CHAR_DATATYPE";
262 case WORD_DATATYPE
: return "WORD_DATATYPE";
263 case DWORD_DATATYPE
: return "DWORD_DATATYPE";
264 case USER_DATATYPE
: return "USER_DATATYPE"; // astnode_get_child(dt, 0)->ident;
266 return "datatype_to_string: invalid datatype";
270 * Gets string representation of an operator.
272 * @return String representation of op
274 const char *operator_to_string(int op
)
277 case PLUS_OPERATOR
: return "PLUS_OPERATOR";
278 case MINUS_OPERATOR
: return "MINUS_OPERATOR";
279 case MUL_OPERATOR
: return "MUL_OPERATOR";
280 case DIV_OPERATOR
: return "DIV_OPERATOR";
281 case MOD_OPERATOR
: return "MOD_OPERATOR";
282 case AND_OPERATOR
: return "AND_OPERATOR";
283 case OR_OPERATOR
: return "OR_OPERATOR";
284 case XOR_OPERATOR
: return "XOR_OPERATOR";
285 case SHL_OPERATOR
: return "SHL_OPERATOR";
286 case SHR_OPERATOR
: return "SHR_OPERATOR";
287 case LT_OPERATOR
: return "LT_OPERATOR";
288 case GT_OPERATOR
: return "GT_OPERATOR";
289 case EQ_OPERATOR
: return "EQ_OPERATOR";
290 case NE_OPERATOR
: return "NE_OPERATOR";
291 case LE_OPERATOR
: return "LE_OPERATOR";
292 case GE_OPERATOR
: return "GE_OPERATOR";
293 case NEG_OPERATOR
: return "NEG_OPERATOR";
294 case NOT_OPERATOR
: return "NOT_OPERATOR";
295 case LO_OPERATOR
: return "LO_OPERATOR";
296 case HI_OPERATOR
: return "HI_OPERATOR";
297 case UMINUS_OPERATOR
: return "UMINUS_OPERATOR";
298 case BANK_OPERATOR
: return "BANK_OPERATOR";
300 return "operator_to_string: invalid operator";
305 * @param nlevels Levels
307 void indent(int nlevels
)
310 for (i
=0; i
<nlevels
; i
++) {
316 * Prints a node recursively.
317 * @param n Node to print
318 * @param level Level (depth)
320 void astnode_print(const astnode
*n
, int level
)
323 /* Indent so it looks pretty */
325 /* Print the node type */
326 printf(astnode_type_to_string(astnode_get_type(n
)));
327 /* Print attributes for those that have */
328 switch (astnode_get_type(n
)) {
329 case INTEGER_NODE
: printf("(%d)", n
->integer
); break;
330 case STRING_NODE
: printf("(\"%s\")", n
->string
); break;
331 case IDENTIFIER_NODE
: printf("(%s)", n
->ident
); break;
332 case LOCAL_ID_NODE
: printf("(%s)", n
->ident
); break;
333 case FILE_PATH_NODE
: printf("(%s)", n
->file_path
); break;
334 case LABEL_NODE
: printf("(%s)", n
->label
); break;
335 case LOCAL_LABEL_NODE
: printf("(%s)", n
->label
); break;
336 case BINARY_NODE
: printf("(%d)", n
->binary
.size
); break;
338 case ARITHMETIC_NODE
:
341 operator_to_string(n
->oper
)
345 case INSTRUCTION_NODE
:
348 instr_mnemonic_to_string(n
->instr
.mnemonic
),
349 addressing_mode_to_string(n
->instr
.mode
),
357 datatype_to_string(n
)
361 case FORWARD_BRANCH_DECL_NODE
:
362 case BACKWARD_BRANCH_DECL_NODE
:
363 case FORWARD_BRANCH_NODE
:
364 case BACKWARD_BRANCH_NODE
:
365 printf("(%s)", n
->ident
);
371 astnode_type_to_string(n
->param
)
376 /* Has no internal attributes */
380 /* Print the children */
381 for (i
=0; i
<astnode_get_child_count(n
); i
++) {
382 astnode_print(astnode_get_child(n
, i
), level
+1);
386 /*---------------------------------------------------------------------------*/
387 /* Functions for general-purpose node management:
388 Creation, destruction, children etc.
392 * Creates a new node of the given type.
393 * @param type The node's type
394 * @param loc File location
395 * @return The newly created node
397 astnode
*astnode_create(astnode_type type
, location loc
)
399 extern const char *yy_current_filename();
400 if (loc
.file
== NULL
) {
401 loc
.file
= yy_current_filename();
403 astnode
*n
= (astnode
*)malloc(sizeof(astnode
));
410 n
->parent
= n
->first_child
= n
->prev_sibling
= n
->next_sibling
= NULL
;
417 * Any children of the node are also finalized, recursively.
418 * @param n The node to finalize.
420 void astnode_finalize(astnode
*n
)
423 /* Remove the node from the tree it's in. */
425 /* Finalize all its children recursively. */
426 while ((c
= astnode_get_first_child(n
)) != NULL
) {
427 astnode_remove_child(n
, c
);
430 /* Free up memory. */
431 switch (astnode_get_type(n
)) {
432 case LABEL_NODE
: SAFE_FREE(n
->label
); break;
433 case LOCAL_LABEL_NODE
: SAFE_FREE(n
->label
); break;
434 case STRING_NODE
: SAFE_FREE(n
->string
); break;
435 case IDENTIFIER_NODE
: SAFE_FREE(n
->ident
); break;
436 case LOCAL_ID_NODE
: SAFE_FREE(n
->ident
); break;
437 case FILE_PATH_NODE
: SAFE_FREE(n
->file_path
);break;
438 case BINARY_NODE
: SAFE_FREE(n
->binary
.data
); break;
439 case FORWARD_BRANCH_DECL_NODE
:
440 case BACKWARD_BRANCH_DECL_NODE
:
441 case FORWARD_BRANCH_NODE
:
442 case BACKWARD_BRANCH_NODE
:
446 /* Has no internal attributes that are dynamically allocated */
453 * Gets the node's type.
454 * @param n The node whose type to get
455 * @return The node's type (astnode_type)
457 astnode_type
astnode_get_type(const astnode
*n
)
459 return (n
!= NULL
) ? n
->type
: NULL_NODE
;
463 * Sets the parent field of all nodes in c to p.
465 void astnode_set_parent(astnode
*c
, astnode
*p
)
468 for (n
= c
; n
!= NULL
; n
= n
->next_sibling
) {
474 * Replaces a node with another.
476 void astnode_replace(astnode
*old_node
, astnode
*new_node
)
480 p
= astnode_get_parent(old_node
);
482 i
= astnode_remove_child(p
, old_node
);
483 /* Insert new child at old child's position */
484 astnode_insert_child(p
, new_node
, i
);
489 * Removes a node from a tree.
490 * @param n The node to remove (can't be the root of the tree)
492 void astnode_remove(astnode
*n
)
494 astnode
*p
= astnode_get_parent(n
);
496 astnode_remove_child(p
, n
);
501 * Removes a child node.
502 * @param p Parent node
503 * @param c Child node
504 * @return Index of the removed node
506 int astnode_remove_child(astnode
*p
, astnode
*c
)
509 i
= astnode_get_child_index(p
, c
);
511 /* Remove head of list. */
512 p
->first_child
= c
->next_sibling
;
513 if (p
->first_child
) {
514 p
->first_child
->prev_sibling
= NULL
;
516 c
->parent
= c
->next_sibling
= c
->prev_sibling
= NULL
;
519 c
->prev_sibling
->next_sibling
= c
->next_sibling
;
520 if (c
->next_sibling
) {
521 c
->next_sibling
->prev_sibling
= c
->prev_sibling
;
523 c
->parent
= c
->next_sibling
= c
->prev_sibling
= NULL
;
529 * Removes child node at specified index.
530 * @param p Parent node
531 * @param i Index >= 0
533 astnode
*astnode_remove_child_at(astnode
*p
, int i
)
535 astnode
*c
= astnode_get_child(p
, i
);
536 astnode_remove_child(p
, c
);
541 * Removes all children from a node and returns them as a list.
542 * @param p Parent node whose children to remove
544 astnode
*astnode_remove_children(astnode
*p
)
547 if (p
== NULL
) { return NULL
; }
548 if (p
->first_child
!= NULL
) {
550 p
->first_child
= NULL
;
551 /* Set parent of all siblings to NULL. */
552 astnode_set_parent(c
, NULL
);
553 /* Return the list of children */
557 /* Has no children. */
563 * Inserts a list of nodes as children.
566 void astnode_insert_child(astnode
*p
, astnode
*c
, int i
)
571 x
= astnode_get_child(p
, i
); /* Current child at that position */
573 /* There isn't a node here. Just add to end. */
574 astnode_add_child(p
, c
);
577 n
= astnode_get_last_sibling(c
);
578 /* Make c..n precede x */
579 c
->prev_sibling
= x
->prev_sibling
;
580 if (x
->prev_sibling
) {
581 x
->prev_sibling
->next_sibling
= c
;
586 astnode_set_parent(c
, p
);
595 * Gets the last node in a list.
597 astnode
*astnode_get_last_sibling(const astnode
*n
)
601 for (s
= (astnode
*)n
; s
->next_sibling
!= NULL
; s
= s
->next_sibling
) ;
607 * Gets the parent of a node.
608 * @param n The node whose parent to get
609 * @return The node's parent, or <code>NULL</code> if it has none
611 astnode
*astnode_get_parent(const astnode
*n
)
613 return n
? n
->parent
: NULL
;
617 * Adds child(ren) to a node.
618 * @param n The parent-to-be
619 * @param new_child List of children-to-be
621 void astnode_add_child(astnode
*n
, astnode
*new_child
)
623 if (n
&& new_child
) {
624 if (n
->first_child
== NULL
) {
625 /* This node has no children, add this as the first one */
626 n
->first_child
= new_child
;
627 astnode_set_parent(new_child
, n
);
630 astnode_add_sibling(n
->first_child
, new_child
);
636 * Adds any number of children to a node.
637 * @param n The parent-to-be
639 void astnode_add_children(astnode
*n
, int count
, ...)
646 for (i
=0; i
<count
; i
++) {
647 c
= va_arg(ap
, astnode
*);
648 astnode_add_child(n
, c
);
654 * Adds sibling(s) to a node.
655 * @param brother List of existing siblings
656 * @param sister List of new siblings
658 void astnode_add_sibling(astnode
*brother
, astnode
*sister
)
662 if (brother
&& sister
) {
663 /* Add to end of list */
664 n
= astnode_get_last_sibling(brother
);
665 n
->next_sibling
= sister
;
666 sister
->prev_sibling
= n
;
667 p
= astnode_get_parent(brother
);
668 astnode_set_parent(sister
, p
);
673 * Gets the child node at the specified index.
674 * @param n The parent node
675 * @param index The index of the desired child node
677 astnode
*astnode_get_child(const astnode
*n
, int index
)
683 for (i
= 0; i
!= index
; i
++) {
685 /* No child at that index. */
696 * Gets a node's first child.
699 astnode
*astnode_get_first_child(const astnode
*n
)
701 return (n
== NULL
) ? NULL
: n
->first_child
;
705 * Gets the index of a child node.
706 * @param p Parent node
707 * @param c Child node
708 * @return Index of c >= 0, or -1 if invalid input
710 int astnode_get_child_index(const astnode
*p
, const astnode
*c
)
715 for (i
=0, n
=p
->first_child
; (n
!= c
) && (n
!= NULL
); i
++, n
=n
->next_sibling
);
722 * Gets the number of children a node has.
723 * @param p Node whose children count to get
725 int astnode_get_child_count(const astnode
*p
)
730 for (c
= p
->first_child
; c
!= NULL
; count
++, c
= c
->next_sibling
);
736 * Clones a node and all its children.
737 * @param n The node to clone
738 * @param loc File location
740 astnode
*astnode_clone(const astnode
*n
, location loc
)
744 if (n
== NULL
) { return NULL
; }
746 c
= astnode_create(astnode_get_type(n
), loc
);
747 /* Copy attributes */
748 switch (astnode_get_type(n
)) {
750 c
->integer
= n
->integer
;
754 case IDENTIFIER_NODE
:
757 case LOCAL_LABEL_NODE
:
759 case FORWARD_BRANCH_DECL_NODE
:
760 case BACKWARD_BRANCH_DECL_NODE
:
761 case FORWARD_BRANCH_NODE
:
762 case BACKWARD_BRANCH_NODE
:
763 c
->string
= (char *)malloc(strlen(n
->string
)+1);
764 if (c
->string
!= NULL
) {
765 strcpy(c
->string
, n
->string
);
769 case ARITHMETIC_NODE
:
773 case INSTRUCTION_NODE
:
778 c
->binary
= n
->binary
;
782 c
->datatype
= n
->datatype
;
788 /* Clone children (TODO: OPTIMIZE THIS) */
789 for (n_c
=n
->first_child
; n_c
!= NULL
; n_c
=n_c
->next_sibling
) {
790 astnode_add_child(c
, astnode_clone(n_c
, loc
));
792 /* Return the clone */
797 * Tests if two nodes are equal.
799 int astnode_equal(const astnode
*n1
, const astnode
*n2
)
802 /* Verify that types are the same */
803 if (astnode_get_type(n1
) != astnode_get_type(n2
)) {
804 return 0; /* Types don't match -- not equal */
806 /* Verify that internal data is the same */
807 switch (astnode_get_type(n1
)) {
808 case ARITHMETIC_NODE
: if (n1
->oper
!= n2
->oper
) return 0; break;
809 case INTEGER_NODE
: if (n1
->integer
!= n2
->integer
) return 0; break;
810 case STRING_NODE
: if (strcmp(n1
->string
, n2
->string
)) return 0; break;
811 case IDENTIFIER_NODE
: if (strcmp(n1
->ident
, n2
->ident
)) return 0; break;
812 case LOCAL_ID_NODE
: if (strcmp(n1
->ident
, n2
->ident
)) return 0; break;
813 case FILE_PATH_NODE
: if (strcmp(n1
->file_path
, n2
->file_path
)) return 0; break;
814 case LABEL_NODE
: if (strcmp(n1
->label
, n2
->label
)) return 0; break;
815 case LOCAL_LABEL_NODE
: if (strcmp(n1
->label
, n2
->label
)) return 0; break;
816 case BINARY_NODE
: if (n1
->binary
.size
!= n2
->binary
.size
) return 0; break;
817 case DATATYPE_NODE
: if (n1
->datatype
!= n2
->datatype
) return 0; break;
818 case TOMBSTONE_NODE
: if (n1
->param
!= n2
->param
) return 0; break;
820 case INSTRUCTION_NODE
:
821 if ( (n1
->instr
.mnemonic
!= n2
->instr
.mnemonic
)
822 || (n1
->instr
.mode
!= n2
->instr
.mode
) ) {
827 case FORWARD_BRANCH_DECL_NODE
:
828 case BACKWARD_BRANCH_DECL_NODE
:
829 case FORWARD_BRANCH_NODE
:
830 case BACKWARD_BRANCH_NODE
:
831 if (strcmp(n1
->ident
, n2
->ident
))
836 /* Has no internal attributes */
839 /* Verify that they have the same number of children */
840 if (astnode_get_child_count(n1
) != astnode_get_child_count(n2
)) {
843 /* Verify that children are equal */
844 for (i
=0; i
<astnode_get_child_count(n1
); i
++) {
845 if (!astnode_equal(astnode_get_child(n1
, i
), astnode_get_child(n2
, i
))) {
854 * Gets the ancestor of a node.
855 * @param n Node whose ancestor to get
856 * @param back How many generations to go back (0=father, 1=grandfather etc.)
858 astnode
*astnode_get_ancestor(const astnode
*n
, int back
)
861 astnode
*a
= astnode_get_parent(n
);
862 for (i
=0; i
<back
; i
++) {
863 a
= astnode_get_parent(a
);
869 * Tests if a node is a descendant of a node of a particular type.
871 * @param type Ancestor's type
872 * @return 0 if no such ancestor, 1 otherwise
874 int astnode_has_ancestor_of_type(const astnode
*n
, astnode_type type
)
877 for (a
= astnode_get_parent(n
); a
!= NULL
; a
= astnode_get_parent(a
) ) {
878 if (astnode_is_type(a
, type
)) {
886 * Gets the next sibling of a node.
889 astnode
*astnode_get_next_sibling(const astnode
*n
)
891 if (n
== NULL
) { return NULL
; }
892 return n
->next_sibling
;
896 * Gets the previous sibling of a node.
899 astnode
*astnode_get_prev_sibling(const astnode
*n
)
901 if (n
== NULL
) { return NULL
; }
902 return n
->prev_sibling
;
906 * Tests if a node is a literal.
907 * @param n Node to test
909 int astnode_is_literal(const astnode
*n
)
911 switch (astnode_get_type(n
)) {
925 /*---------------------------------------------------------------------------*/
926 /* Functions for creating AST nodes of specific type.
927 1:1 correspondence between astnode_create_* and *_INSTRUCTION.
928 Each takes the operands required for that node type,
929 calls astnode_create() and then fills in fields and adds children (if any).
932 astnode
*astnode_create_null(location loc
)
934 astnode
*n
= astnode_create(NULL_NODE
, loc
);
939 * Creates a CPU instruction node.
940 * @param mnemonic The instruction mnemonic
941 * @param mode The addressing mode used
942 * @param operand The instruction operand (an expression) (can be <code>NULL</code>)
943 * @param loc File location
945 astnode
*astnode_create_instruction(int mnemonic
, addressing_mode mode
, astnode
*operand
, location loc
)
947 astnode
*n
= astnode_create(INSTRUCTION_NODE
, loc
);
948 /* Store the mnemonic and addressing mode */
949 n
->instr
.mnemonic
= mnemonic
;
950 n
->instr
.mode
= mode
;
951 /* This node has one child: The operand, which is an expression */
952 astnode_add_child(n
, operand
);
957 * Creates an identifier node.
958 * @param ident The identifier (a string)
959 * @param loc File location
961 astnode
*astnode_create_identifier(const char *ident
, location loc
)
963 astnode
*n
= astnode_create(IDENTIFIER_NODE
, loc
);
964 /* Allocate and store text */
965 n
->ident
= (char *)malloc(strlen(ident
)+1);
966 if (n
->ident
!= NULL
) {
967 strcpy(n
->ident
, ident
);
973 * Creates an integer literal node.
974 * @param value The integer literal
975 * @param loc File location
977 astnode
*astnode_create_integer(int value
, location loc
)
979 astnode
*n
= astnode_create(INTEGER_NODE
, loc
);
985 * Creates a string literal node.
986 * @param value The string literal
987 * @param loc File location
989 astnode
*astnode_create_string(const char *value
, location loc
)
991 astnode
*n
= astnode_create(STRING_NODE
, loc
);
992 /* Allocate and store text */
993 n
->string
= (char *)malloc(strlen(value
)+1);
994 if (n
->string
!= NULL
) {
995 strcpy(n
->string
, value
);
1001 * Creates an expression node (unary or binary).
1002 * @param oper The operator
1003 * @param left Left operand
1004 * @param right Right operand (can be <code>NULL</code>)
1005 * @param loc File location
1007 astnode
*astnode_create_arithmetic(arithmetic_operator oper
, astnode
*left
, astnode
*right
, location loc
)
1009 astnode
*n
= astnode_create(ARITHMETIC_NODE
, loc
);
1011 /* This node has two children: left-hand side and right-hand side expression */
1012 /* For unary operators right-hand side should be <code>NULL</code> */
1013 astnode_add_children(n
, 2, left
, right
);
1018 * Creates an if node.
1019 * @param expr The expression involved in the if
1020 * @param then The statement(s) to assemble when expr is non-zero
1021 * @param elif List of CASE nodes (may be <code>NULL</code>)
1022 * @param els The final else-part (DEFAULT node) (may be <code>NULL</code>)
1023 * @param loc File location
1025 astnode
*astnode_create_if(astnode
*expr
, astnode
*then
, astnode
*elif
, astnode
*els
, location loc
)
1027 astnode
*n
= astnode_create(IF_NODE
, loc
);
1028 /* This node has several children: List of CASE nodes, possibly ended by DEFAULT node */
1029 astnode_add_child(n
, astnode_create_case(expr
, then
, loc
) );
1030 astnode_add_child(n
, elif
);
1032 astnode_add_child(n
, astnode_create_default(els
, loc
));
1038 * Creates a CASE node.
1039 * @param expr Expression
1040 * @param then List of statement to assemble when expr is non-zero (true)
1041 * @param loc File location
1043 astnode
*astnode_create_case(astnode
*expr
, astnode
*then
, location loc
)
1045 astnode
*n
= astnode_create(CASE_NODE
, loc
);
1046 /* This node has two children: expression to test and list of statements. */
1047 astnode_add_children(
1051 astnode_create_list(then
)
1057 * Creates a DEFAULT node.
1058 * @param stmts List of statements
1059 * @param loc File location
1061 astnode
*astnode_create_default(astnode
*stmts
, location loc
)
1063 astnode
*n
= astnode_create(DEFAULT_NODE
, loc
);
1064 /* This node has list of statements as children. */
1073 * Creates an ifdef node.
1074 * @param ident The identifier to check
1075 * @param then The statement(s) to assemble when ident is defined
1076 * @param els The statement(s) to assemble when ident is not defined (can be <code>NULL</code>)
1077 * @param loc File location
1079 astnode
*astnode_create_ifdef(astnode
*ident
, astnode
*then
, astnode
*els
, location loc
)
1081 astnode
*n
= astnode_create(IFDEF_NODE
, loc
);
1082 /* This node has three children: identifier to test, then-part, else-part */
1083 astnode_add_children(
1087 astnode_create_list(then
),
1088 astnode_create_list(els
)
1094 * Creates an ifndef node.
1095 * @param ident The identifier to check
1096 * @param then The statement(s) to assemble when ident is not defined
1097 * @param els The statement(s) to assemble when ident is defined (can be <code>NULL</code>)
1098 * @param loc File location
1100 astnode
*astnode_create_ifndef(astnode
*ident
, astnode
*then
, astnode
*els
, location loc
)
1102 astnode
*n
= astnode_create(IFNDEF_NODE
, loc
);
1103 /* This node has three children: identifier to test, then-part, else-part */
1104 astnode_add_children(
1108 astnode_create_list(then
),
1109 astnode_create_list(els
)
1115 * Creates a macro declaration node.
1116 * @param ident Name of macro
1117 * @param params List of parameters (can be <code>NULL</code>)
1118 * @param body Macro body
1119 * @param loc File location
1121 astnode
*astnode_create_macro_decl(astnode
*ident
, astnode
*params
, astnode
*body
, location loc
)
1123 astnode
*n
= astnode_create(MACRO_DECL_NODE
, loc
);
1124 /* This node has three children:
1125 1) An identifier, which is the name of the macro
1126 2) List of parameters
1127 3) List of statements, which is the macro body */
1128 astnode_add_children(
1132 astnode_create_list(params
),
1133 astnode_create_list(body
)
1139 * Creates a macro node.
1140 * @param ident Name of macro
1141 * @param args List of arguments (can be <code>NULL</code>)
1142 * @param loc File location
1144 astnode
*astnode_create_macro(astnode
*ident
, astnode
*args
, location loc
)
1146 astnode
*n
= astnode_create(MACRO_NODE
, loc
);
1147 astnode_add_children(
1151 astnode_create_list(args
)
1157 * Creates an equ node.
1158 * @param ident Identifier
1159 * @param expr Expression
1160 * @param loc File location
1162 astnode
*astnode_create_equ(astnode
*ident
, astnode
*expr
, location loc
)
1164 astnode
*n
= astnode_create(EQU_NODE
, loc
);
1165 astnode_add_children(n
, 2, ident
, expr
);
1170 * Creates an assign node.
1171 * @param ident Identifier
1172 * @param expr Expression
1173 * @param loc File location
1175 astnode
*astnode_create_assign(astnode
*ident
, astnode
*expr
, location loc
)
1177 astnode
*n
= astnode_create(ASSIGN_NODE
, loc
);
1178 astnode_add_children(n
, 2, ident
, expr
);
1183 * Creates a storage node.
1184 * @param type Type of storage
1185 * @param count Expression with contains count
1186 * @param loc File location
1188 astnode
*astnode_create_storage(astnode
*type
, astnode
*count
, location loc
)
1190 astnode
*n
= astnode_create(STORAGE_NODE
, loc
);
1191 /* Add the type of data (enumerated or identifier) */
1192 astnode_add_child(n
, type
);
1193 /* Second child: Count */
1194 if (count
== NULL
) {
1195 /* No count given, default=1 */
1196 count
= astnode_create_integer(1, loc
);
1198 astnode_add_child(n
, count
);
1203 * Creates an incsrc node.
1204 * @param file File specifier
1205 * @param loc File location
1207 astnode
*astnode_create_incsrc(astnode
*file
, location loc
)
1209 astnode
*n
= astnode_create(INCSRC_NODE
, loc
);
1210 /* One child: Path to file */
1211 astnode_add_child(n
, file
);
1216 * Creates an incbin node.
1217 * @param file File specifier
1218 * @param loc File location
1220 astnode
*astnode_create_incbin(astnode
*file
, location loc
)
1222 astnode
*n
= astnode_create(INCBIN_NODE
, loc
);
1223 /* One child: Path to file */
1224 astnode_add_child(n
, file
);
1229 * Creates a charmap node.
1230 * @param file File specifier
1231 * @param loc File location
1233 astnode
*astnode_create_charmap(astnode
*file
, location loc
)
1235 astnode
*n
= astnode_create(CHARMAP_NODE
, loc
);
1236 /* One child: Path to file */
1237 astnode_add_child(n
, file
);
1242 * Creates a structure (STRUC) instance node.
1243 * @param vals Values for the structure fields
1244 * @param loc File location
1246 astnode
*astnode_create_struc(astnode
*vals
, location loc
)
1248 astnode
*n
= astnode_create(STRUC_NODE
, loc
);
1249 astnode_add_child(n
, vals
);
1253 * Creates a structure (STRUC) declaration node.
1254 * @param id Structure identifier
1255 * @param stmts Statements of the structure declaration
1256 * @param loc File location
1258 astnode
*astnode_create_struc_decl(astnode
*id
, astnode
*stmts
, location loc
)
1260 astnode
*n
= astnode_create(STRUC_DECL_NODE
, loc
);
1261 astnode_add_child(n
, id
);
1262 astnode_add_child(n
, stmts
);
1267 * Creates a union declaration node.
1268 * @param id Union identifier
1269 * @param stmts Statements of the union declaration
1270 * @param loc File location
1272 astnode
*astnode_create_union_decl(astnode
*id
, astnode
*stmts
, location loc
)
1274 astnode
*n
= astnode_create(UNION_DECL_NODE
, loc
);
1275 astnode_add_child(n
, id
);
1276 astnode_add_child(n
, stmts
);
1281 * Creates an enum declaration node.
1282 * @param id Enum identifier
1283 * @param stmts Statements of the enum declaration
1284 * @param loc File location
1286 astnode
*astnode_create_enum_decl(astnode
*id
, astnode
*stmts
, location loc
)
1288 astnode
*n
= astnode_create(ENUM_DECL_NODE
, loc
);
1289 astnode_add_child(n
, id
);
1290 astnode_add_child(n
, stmts
);
1295 * Creates a record declaration node.
1296 * @param id Record identifier
1297 * @param fields Fields of the record
1298 * @param loc File location
1300 astnode
*astnode_create_record_decl(astnode
*id
, astnode
*fields
, location loc
)
1302 astnode
*n
= astnode_create(RECORD_DECL_NODE
, loc
);
1303 astnode_add_child(n
, id
);
1304 astnode_add_child(n
, fields
);
1309 * Creates a bitfield declaration node.
1310 * @param id Identifier
1311 * @param width Width of field
1312 * @param loc Location
1314 astnode
*astnode_create_bitfield_decl(astnode
*id
, astnode
*width
, location loc
)
1316 astnode
*n
= astnode_create(BITFIELD_DECL_NODE
, loc
);
1317 astnode_add_child(n
, id
);
1318 astnode_add_child(n
, width
);
1323 * Creates a public node.
1325 astnode
*astnode_create_public(astnode
*l
, location loc
)
1327 astnode
*n
= astnode_create(PUBLIC_NODE
, loc
);
1328 astnode_add_child(n
, l
);
1333 * Creates an extrn node.
1334 * @param l List of identifiers
1335 * @param t Symbol type specifier
1336 * @param f From unit (identifier, may be <code>NULL</code>)
1338 astnode
*astnode_create_extrn(astnode
*l
, astnode
*t
, astnode
*f
, location loc
)
1340 astnode
*n
= astnode_create(EXTRN_NODE
, loc
);
1341 astnode_add_child(n
, t
);
1342 astnode_add_child(n
, astnode_create_list(l
));
1343 astnode_add_child(n
, f
);
1348 * Creates a dataseg node.
1350 astnode
*astnode_create_dataseg(int modifiers
, location loc
)
1352 astnode
*n
= astnode_create(DATASEG_NODE
, loc
);
1353 n
->modifiers
= modifiers
;
1358 * Creates a codeseg node.
1360 astnode
*astnode_create_codeseg(location loc
)
1362 astnode
*n
= astnode_create(CODESEG_NODE
, loc
);
1367 * Creates a data node.
1368 * @param type Type specifier
1369 * @param data List of values
1370 * @param loc File location
1372 astnode
*astnode_create_data(astnode
*type
, astnode
*data
, location loc
)
1374 astnode
*n
= astnode_create(DATA_NODE
, loc
);
1375 astnode_add_child(n
, type
);
1376 astnode_add_child(n
, data
);
1381 * Creates a file path node.
1382 * This is similar to a string literal node, the only difference is semantics.
1383 * A file path node implies that the path can be relative to both current
1384 * directory and any of the directories in the search path.
1385 * @param path The path this node represents
1386 * @param loc File location
1388 astnode
*astnode_create_file_path(const char *path
, location loc
)
1390 astnode
*n
= astnode_create(FILE_PATH_NODE
, loc
);
1391 /* Allocate and store text */
1392 n
->file_path
= (char *)malloc(strlen(path
)+1);
1393 if (n
->file_path
!= NULL
) {
1394 strcpy(n
->file_path
, path
);
1400 * Creates a (global) label node.
1401 * @param s Name of label
1402 * @param addr Address
1403 * @param type Datatype (may be <code>NULL</code>)
1404 * @param loc Location
1406 astnode
*astnode_create_label(const char *s
, astnode
*addr
, astnode
*type
, location loc
)
1408 astnode
*n
= astnode_create(LABEL_NODE
, loc
);
1409 /* Allocate and store text */
1410 n
->label
= (char *)malloc(strlen(s
)+1);
1411 if (n
->label
!= NULL
) {
1412 strcpy(n
->label
, s
);
1414 /* Two children: Datatype and address */
1416 addr
= astnode_create_pc(loc
);
1419 type
= astnode_create_datatype(BYTE_DATATYPE
, NULL
, loc
);
1421 astnode_add_child(n
, type
);
1422 astnode_add_child(n
, addr
);
1427 * Creates a local label node.
1428 * @param s Name of label
1429 * @param loc Location
1431 astnode
*astnode_create_local_label(const char *s
, location loc
)
1433 astnode
*n
= astnode_create(LOCAL_LABEL_NODE
, loc
);
1434 /* Allocate and store text */
1435 n
->label
= (char *)malloc(strlen(s
)+1);
1436 if (n
->label
!= NULL
) {
1437 strcpy(n
->label
, s
);
1443 * Creates a local identifier node.
1444 * @param s Identifier
1445 * @param loc Location
1447 astnode
*astnode_create_local_id(const char *s
, location loc
)
1449 astnode
*n
= astnode_create(LOCAL_ID_NODE
, loc
);
1450 /* Allocate and store text */
1451 n
->ident
= (char *)malloc(strlen(s
)+1);
1452 if (n
->ident
!= NULL
) {
1453 strcpy(n
->ident
, s
);
1459 * Creates a list node.
1460 * This is a way to group a list of nodes in a parent node.
1461 * @param l List of nodes to group in list node
1463 astnode
*astnode_create_list(astnode
*l
)
1466 /* Create the node */
1468 n
= astnode_create(LIST_NODE
, l
->loc
);
1469 /* Add list of values */
1470 astnode_add_child(n
, l
);
1472 /* Make a node with zero children */
1475 n
= astnode_create(LIST_NODE
, dummyloc
);
1481 * Creates a PC node.
1482 * @param loc File location
1484 astnode
*astnode_create_pc(location loc
)
1486 return astnode_create(CURRENT_PC_NODE
, loc
);
1490 * Creates a binary node.
1491 * @param bin Dynamically allocated (malloc() ) data that this node wraps. Will be freed automatically by astnode_finalize()
1492 * @param size Size of bin
1493 * @param loc File location
1495 astnode
*astnode_create_binary(unsigned char *bin
, int size
, location loc
)
1497 astnode
*n
= astnode_create(BINARY_NODE
, loc
);
1498 n
->binary
.data
= bin
;
1499 n
->binary
.size
= size
;
1504 * Creates a tombstone node, which is a marker node that says that another node
1506 * @param type The type of node that used to live here
1507 * @param loc File location
1509 astnode
*astnode_create_tombstone(astnode_type type
, location loc
)
1511 astnode
*n
= astnode_create(TOMBSTONE_NODE
, loc
);
1512 n
->param
= (long)type
;
1517 * Creates a dot operator node.
1518 * Represents a structure field access of the form 'before.after'.
1519 * @param before Structure identifier
1520 * @param after Field identifier (can be another dot op, or an identifier)
1522 astnode
*astnode_create_dot(astnode
*before
, astnode
*after
, location loc
)
1524 astnode
*n
= astnode_create(DOT_NODE
, loc
);
1525 astnode_add_child(n
, before
);
1526 astnode_add_child(n
, after
);
1531 * Creates a sizeof operator node.
1532 * @param expr Expression (datatype?)
1533 * @param loc Location
1535 astnode
*astnode_create_sizeof(astnode
*expr
, location loc
)
1537 astnode
*n
= astnode_create(SIZEOF_NODE
, loc
);
1538 astnode_add_child(n
, expr
);
1543 * Creates a datatype node.
1544 * @param t The datatype this node represents
1545 * @param id If the datatype is a custom one, this is its name
1546 * @param loc Location
1548 astnode
*astnode_create_datatype(datatype t
, astnode
*id
, location loc
)
1550 astnode
*n
= astnode_create(DATATYPE_NODE
, loc
);
1553 astnode_add_child(n
, id
);
1559 * Creates a variable declaration node.
1560 * @param modifiers PUBLIC_FLAG | ZEROPAGE_FLAG
1561 * @param id Identifier
1562 * @param data Datatype+initializer
1563 * @param loc Location
1565 astnode
*astnode_create_var_decl(int modifiers
, astnode
*id
, astnode
*data
, location loc
)
1567 astnode
*n
= astnode_create(VAR_DECL_NODE
, loc
);
1568 n
->modifiers
= modifiers
;
1569 astnode_add_child(n
, id
);
1570 astnode_add_child(n
, data
);
1577 astnode
*astnode_create_scope(astnode
*left
, astnode
*right
, location loc
)
1579 astnode
*n
= astnode_create(SCOPE_NODE
, loc
);
1580 astnode_add_child(n
, left
);
1581 astnode_add_child(n
, right
);
1586 * Creates a procedure (PROC) node.
1587 * @param ident Name of procedure
1588 * @param stmts Procedure statements
1589 * @param loc File location
1591 astnode
*astnode_create_proc(astnode
*ident
, astnode
*stmts
, location loc
)
1593 astnode
*n
= astnode_create(PROC_NODE
, loc
);
1594 /* This node has two children:
1595 1) An identifier, which is the name of the procedure
1596 2) List of statements, which is the procedure body */
1597 astnode_add_children(
1601 astnode_create_list(stmts
)
1607 * Creates a REPT node.
1608 * @param expr Number of times to repeat statements
1609 * @param stmts Statement list
1610 * @param loc File location
1612 astnode
*astnode_create_rept(astnode
*expr
, astnode
*stmts
, location loc
)
1614 astnode
*n
= astnode_create(REPT_NODE
, loc
);
1615 /* This node has two children:
1616 1) An expression, which is the repeat count
1617 2) List of statements, which is the (anonymous) macro body */
1618 astnode_add_children(
1622 astnode_create_list(stmts
)
1628 * Creates a WHILE node.
1629 * @param expr Boolean expression
1630 * @param stmts Statement list
1631 * @param loc File location
1633 astnode
*astnode_create_while(astnode
*expr
, astnode
*stmts
, location loc
)
1635 astnode
*n
= astnode_create(WHILE_NODE
, loc
);
1636 /* This node has two children:
1637 1) A boolean expression
1638 2) List of statements, which is the (anonymous) macro body */
1639 astnode_add_children(
1643 astnode_create_list(stmts
)
1649 * Creates a MESSAGE node.
1650 * @param expr Message to print.
1651 * @param loc File location
1653 astnode
*astnode_create_message(astnode
*expr
, location loc
)
1655 astnode
*n
= astnode_create(MESSAGE_NODE
, loc
);
1656 astnode_add_child(n
, expr
);
1661 * Creates a WARNING node.
1662 * @param str Warning to print.
1663 * @param loc File location
1665 astnode
*astnode_create_warning(astnode
*str
, location loc
)
1667 astnode
*n
= astnode_create(WARNING_NODE
, loc
);
1668 astnode_add_child(n
, str
);
1673 * Creates an ERROR node.
1674 * @param str Error to print.
1675 * @param loc File location
1677 astnode
*astnode_create_error(astnode
*str
, location loc
)
1679 astnode
*n
= astnode_create(ERROR_NODE
, loc
);
1680 astnode_add_child(n
, str
);
1685 * Creates a forward branch declaration node.
1686 * @param ident Branch name
1687 * @param loc File location
1689 astnode
*astnode_create_forward_branch_decl(const char *ident
, location loc
)
1691 astnode
*n
= astnode_create(FORWARD_BRANCH_DECL_NODE
, loc
);
1692 /* Allocate and store text */
1693 n
->ident
= (char *)malloc(strlen(ident
)+1);
1694 if (n
->ident
!= NULL
) {
1695 strcpy(n
->ident
, ident
);
1701 * Creates a backward branch declaration node.
1702 * @param ident Branch name
1703 * @param loc File location
1705 astnode
*astnode_create_backward_branch_decl(const char *ident
, location loc
)
1707 astnode
*n
= astnode_create(BACKWARD_BRANCH_DECL_NODE
, loc
);
1708 /* Allocate and store text */
1709 n
->ident
= (char *)malloc(strlen(ident
)+1);
1710 if (n
->ident
!= NULL
) {
1711 strcpy(n
->ident
, ident
);
1717 * Creates a forward branch reference node.
1718 * @param ident Branch name
1719 * @param loc File location
1721 astnode
*astnode_create_forward_branch(const char *ident
, location loc
)
1723 astnode
*n
= astnode_create(FORWARD_BRANCH_NODE
, loc
);
1724 /* Allocate and store text */
1725 n
->ident
= (char *)malloc(strlen(ident
)+1);
1726 if (n
->ident
!= NULL
) {
1727 strcpy(n
->ident
, ident
);
1733 * Creates a backward branch reference node.
1734 * @param ident Branch name
1735 * @param loc File location
1737 astnode
*astnode_create_backward_branch(const char *ident
, location loc
)
1739 astnode
*n
= astnode_create(BACKWARD_BRANCH_NODE
, loc
);
1740 /* Allocate and store text */
1741 n
->ident
= (char *)malloc(strlen(ident
)+1);
1742 if (n
->ident
!= NULL
) {
1743 strcpy(n
->ident
, ident
);
1749 * Creates a mask operator node.
1750 * @param expr Expression
1751 * @param loc Location
1753 astnode
*astnode_create_mask(astnode
*expr
, location loc
)
1755 astnode
*n
= astnode_create(MASK_NODE
, loc
);
1756 astnode_add_child(n
, expr
);
1761 * Creates an ALIGN node.
1762 * @param idents List of identifiers
1763 * @param expr Expression
1764 * @param loc File location
1766 astnode
*astnode_create_align(astnode
*idents
, astnode
*expr
, location loc
)
1768 astnode
*n
= astnode_create(ALIGN_NODE
, loc
);
1769 astnode_add_child(n
, astnode_create_list(idents
) );
1770 astnode_add_child(n
, expr
);
1775 * Creates an INDEX node.
1776 * @param ident Identifier being indexed
1777 * @param expr Index expression
1778 * @param loc File location
1780 astnode
*astnode_create_index(astnode
*ident
, astnode
*expr
, location loc
)
1782 astnode
*n
= astnode_create(INDEX_NODE
, loc
);
1783 astnode_add_child(n
, ident
);
1784 astnode_add_child(n
, expr
);
1789 * Creates an ORG node.
1790 * @param addr Address
1791 * @param loc File location
1793 astnode
*astnode_create_org(astnode
*addr
, location loc
)
1795 astnode
*n
= astnode_create(ORG_NODE
, loc
);
1796 astnode_add_child(n
, addr
);