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("%s", 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
);
484 /* Insert new child at old child's position */
485 astnode_insert_child(p
, new_node
, i
);
491 * Removes a node from a tree.
492 * @param n The node to remove (can't be the root of the tree)
494 void astnode_remove(astnode
*n
)
496 astnode
*p
= astnode_get_parent(n
);
498 astnode_remove_child(p
, n
);
503 * Removes a child node.
504 * @param p Parent node
505 * @param c Child node
506 * @return Index of the removed node
508 int astnode_remove_child(astnode
*p
, astnode
*c
)
511 i
= astnode_get_child_index(p
, c
);
513 /* Remove head of list. */
514 p
->first_child
= c
->next_sibling
;
515 if (p
->first_child
) {
516 p
->first_child
->prev_sibling
= NULL
;
518 c
->parent
= c
->next_sibling
= c
->prev_sibling
= NULL
;
521 c
->prev_sibling
->next_sibling
= c
->next_sibling
;
522 if (c
->next_sibling
) {
523 c
->next_sibling
->prev_sibling
= c
->prev_sibling
;
525 c
->parent
= c
->next_sibling
= c
->prev_sibling
= NULL
;
531 * Removes child node at specified index.
532 * @param p Parent node
533 * @param i Index >= 0
535 astnode
*astnode_remove_child_at(astnode
*p
, int i
)
537 astnode
*c
= astnode_get_child(p
, i
);
538 astnode_remove_child(p
, c
);
543 * Removes all children from a node and returns them as a list.
544 * @param p Parent node whose children to remove
546 astnode
*astnode_remove_children(astnode
*p
)
549 if (p
== NULL
) { return NULL
; }
550 if (p
->first_child
!= NULL
) {
552 p
->first_child
= NULL
;
553 /* Set parent of all siblings to NULL. */
554 astnode_set_parent(c
, NULL
);
555 /* Return the list of children */
559 /* Has no children. */
565 * Inserts a list of nodes as children.
568 void astnode_insert_child(astnode
*p
, astnode
*c
, int i
)
573 x
= astnode_get_child(p
, i
); /* Current child at that position */
575 /* There isn't a node here. Just add to end. */
576 astnode_add_child(p
, c
);
579 n
= astnode_get_last_sibling(c
);
580 /* Make c..n precede x */
581 c
->prev_sibling
= x
->prev_sibling
;
582 if (x
->prev_sibling
) {
583 x
->prev_sibling
->next_sibling
= c
;
588 astnode_set_parent(c
, p
);
597 * Gets the last node in a list.
599 astnode
*astnode_get_last_sibling(const astnode
*n
)
603 for (s
= (astnode
*)n
; s
->next_sibling
!= NULL
; s
= s
->next_sibling
) ;
609 * Gets the parent of a node.
610 * @param n The node whose parent to get
611 * @return The node's parent, or <code>NULL</code> if it has none
613 astnode
*astnode_get_parent(const astnode
*n
)
615 return n
? n
->parent
: NULL
;
619 * Adds child(ren) to a node.
620 * @param n The parent-to-be
621 * @param new_child List of children-to-be
623 void astnode_add_child(astnode
*n
, astnode
*new_child
)
625 if (n
&& new_child
) {
626 if (n
->first_child
== NULL
) {
627 /* This node has no children, add this as the first one */
628 n
->first_child
= new_child
;
629 astnode_set_parent(new_child
, n
);
632 astnode_add_sibling(n
->first_child
, new_child
);
638 * Adds any number of children to a node.
639 * @param n The parent-to-be
641 void astnode_add_children(astnode
*n
, int count
, ...)
648 for (i
=0; i
<count
; i
++) {
649 c
= va_arg(ap
, astnode
*);
650 astnode_add_child(n
, c
);
656 * Adds sibling(s) to a node.
657 * @param brother List of existing siblings
658 * @param sister List of new siblings
660 void astnode_add_sibling(astnode
*brother
, astnode
*sister
)
664 if (brother
&& sister
) {
665 /* Add to end of list */
666 n
= astnode_get_last_sibling(brother
);
667 n
->next_sibling
= sister
;
668 sister
->prev_sibling
= n
;
669 p
= astnode_get_parent(brother
);
670 astnode_set_parent(sister
, p
);
675 * Gets the child node at the specified index.
676 * @param n The parent node
677 * @param index The index of the desired child node
679 astnode
*astnode_get_child(const astnode
*n
, int index
)
685 for (i
= 0; i
!= index
; i
++) {
687 /* No child at that index. */
698 * Gets a node's first child.
701 astnode
*astnode_get_first_child(const astnode
*n
)
703 return (n
== NULL
) ? NULL
: n
->first_child
;
707 * Gets the index of a child node.
708 * @param p Parent node
709 * @param c Child node
710 * @return Index of c >= 0, or -1 if invalid input
712 int astnode_get_child_index(const astnode
*p
, const astnode
*c
)
717 for (i
=0, n
=p
->first_child
; (n
!= c
) && (n
!= NULL
); i
++, n
=n
->next_sibling
);
724 * Gets the number of children a node has.
725 * @param p Node whose children count to get
727 int astnode_get_child_count(const astnode
*p
)
732 for (c
= p
->first_child
; c
!= NULL
; count
++, c
= c
->next_sibling
);
738 * Clones a node and all its children.
739 * @param n The node to clone
740 * @param loc File location
742 astnode
*astnode_clone(const astnode
*n
, location loc
)
746 if (n
== NULL
) { return NULL
; }
748 c
= astnode_create(astnode_get_type(n
), loc
);
749 /* Copy attributes */
750 switch (astnode_get_type(n
)) {
752 c
->integer
= n
->integer
;
756 case IDENTIFIER_NODE
:
759 case LOCAL_LABEL_NODE
:
761 case FORWARD_BRANCH_DECL_NODE
:
762 case BACKWARD_BRANCH_DECL_NODE
:
763 case FORWARD_BRANCH_NODE
:
764 case BACKWARD_BRANCH_NODE
:
765 c
->string
= (char *)malloc(strlen(n
->string
)+1);
766 if (c
->string
!= NULL
) {
767 strcpy(c
->string
, n
->string
);
771 case ARITHMETIC_NODE
:
775 case INSTRUCTION_NODE
:
780 c
->binary
= n
->binary
;
784 c
->datatype
= n
->datatype
;
790 /* Clone children (TODO: OPTIMIZE THIS) */
791 for (n_c
=n
->first_child
; n_c
!= NULL
; n_c
=n_c
->next_sibling
) {
792 astnode_add_child(c
, astnode_clone(n_c
, loc
));
794 /* Return the clone */
799 * Tests if two nodes are equal.
801 int astnode_equal(const astnode
*n1
, const astnode
*n2
)
804 /* Verify that types are the same */
805 if (astnode_get_type(n1
) != astnode_get_type(n2
)) {
806 return 0; /* Types don't match -- not equal */
808 /* Verify that internal data is the same */
809 switch (astnode_get_type(n1
)) {
810 case ARITHMETIC_NODE
: if (n1
->oper
!= n2
->oper
) return 0; break;
811 case INTEGER_NODE
: if (n1
->integer
!= n2
->integer
) return 0; break;
812 case STRING_NODE
: if (strcmp(n1
->string
, n2
->string
)) return 0; break;
813 case IDENTIFIER_NODE
: if (strcmp(n1
->ident
, n2
->ident
)) return 0; break;
814 case LOCAL_ID_NODE
: if (strcmp(n1
->ident
, n2
->ident
)) return 0; break;
815 case FILE_PATH_NODE
: if (strcmp(n1
->file_path
, n2
->file_path
)) return 0; break;
816 case LABEL_NODE
: if (strcmp(n1
->label
, n2
->label
)) return 0; break;
817 case LOCAL_LABEL_NODE
: if (strcmp(n1
->label
, n2
->label
)) return 0; break;
818 case BINARY_NODE
: if (n1
->binary
.size
!= n2
->binary
.size
) return 0; break;
819 case DATATYPE_NODE
: if (n1
->datatype
!= n2
->datatype
) return 0; break;
820 case TOMBSTONE_NODE
: if (n1
->param
!= n2
->param
) return 0; break;
822 case INSTRUCTION_NODE
:
823 if ( (n1
->instr
.mnemonic
!= n2
->instr
.mnemonic
)
824 || (n1
->instr
.mode
!= n2
->instr
.mode
) ) {
829 case FORWARD_BRANCH_DECL_NODE
:
830 case BACKWARD_BRANCH_DECL_NODE
:
831 case FORWARD_BRANCH_NODE
:
832 case BACKWARD_BRANCH_NODE
:
833 if (strcmp(n1
->ident
, n2
->ident
))
838 /* Has no internal attributes */
841 /* Verify that they have the same number of children */
842 if (astnode_get_child_count(n1
) != astnode_get_child_count(n2
)) {
845 /* Verify that children are equal */
846 for (i
=0; i
<astnode_get_child_count(n1
); i
++) {
847 if (!astnode_equal(astnode_get_child(n1
, i
), astnode_get_child(n2
, i
))) {
856 * Gets the ancestor of a node.
857 * @param n Node whose ancestor to get
858 * @param back How many generations to go back (0=father, 1=grandfather etc.)
860 astnode
*astnode_get_ancestor(const astnode
*n
, int back
)
863 astnode
*a
= astnode_get_parent(n
);
864 for (i
=0; i
<back
; i
++) {
865 a
= astnode_get_parent(a
);
871 * Tests if a node is a descendant of a node of a particular type.
873 * @param type Ancestor's type
874 * @return 0 if no such ancestor, 1 otherwise
876 int astnode_has_ancestor_of_type(const astnode
*n
, astnode_type type
)
879 for (a
= astnode_get_parent(n
); a
!= NULL
; a
= astnode_get_parent(a
) ) {
880 if (astnode_is_type(a
, type
)) {
888 * Gets the next sibling of a node.
891 astnode
*astnode_get_next_sibling(const astnode
*n
)
893 if (n
== NULL
) { return NULL
; }
894 return n
->next_sibling
;
898 * Gets the previous sibling of a node.
901 astnode
*astnode_get_prev_sibling(const astnode
*n
)
903 if (n
== NULL
) { return NULL
; }
904 return n
->prev_sibling
;
908 * Tests if a node is a literal.
909 * @param n Node to test
911 int astnode_is_literal(const astnode
*n
)
913 switch (astnode_get_type(n
)) {
927 /*---------------------------------------------------------------------------*/
928 /* Functions for creating AST nodes of specific type.
929 1:1 correspondence between astnode_create_* and *_INSTRUCTION.
930 Each takes the operands required for that node type,
931 calls astnode_create() and then fills in fields and adds children (if any).
934 astnode
*astnode_create_null(location loc
)
936 astnode
*n
= astnode_create(NULL_NODE
, loc
);
941 * Creates a CPU instruction node.
942 * @param mnemonic The instruction mnemonic
943 * @param mode The addressing mode used
944 * @param operand The instruction operand (an expression) (can be <code>NULL</code>)
945 * @param loc File location
947 astnode
*astnode_create_instruction(int mnemonic
, addressing_mode mode
, astnode
*operand
, location loc
)
949 astnode
*n
= astnode_create(INSTRUCTION_NODE
, loc
);
950 /* Store the mnemonic and addressing mode */
951 n
->instr
.mnemonic
= mnemonic
;
952 n
->instr
.mode
= mode
;
953 /* This node has one child: The operand, which is an expression */
954 astnode_add_child(n
, operand
);
959 * Creates an identifier node.
960 * @param ident The identifier (a string)
961 * @param loc File location
963 astnode
*astnode_create_identifier(const char *ident
, location loc
)
965 astnode
*n
= astnode_create(IDENTIFIER_NODE
, loc
);
966 /* Allocate and store text */
967 n
->ident
= (char *)malloc(strlen(ident
)+1);
968 if (n
->ident
!= NULL
) {
969 strcpy(n
->ident
, ident
);
975 * Creates an integer literal node.
976 * @param value The integer literal
977 * @param loc File location
979 astnode
*astnode_create_integer(int value
, location loc
)
981 astnode
*n
= astnode_create(INTEGER_NODE
, loc
);
987 * Creates a string literal node.
988 * @param value The string literal
989 * @param loc File location
991 astnode
*astnode_create_string(const char *value
, location loc
)
993 astnode
*n
= astnode_create(STRING_NODE
, loc
);
994 /* Allocate and store text */
995 n
->string
= (char *)malloc(strlen(value
)+1);
996 if (n
->string
!= NULL
) {
997 strcpy(n
->string
, value
);
1003 * Creates an expression node (unary or binary).
1004 * @param oper The operator
1005 * @param left Left operand
1006 * @param right Right operand (can be <code>NULL</code>)
1007 * @param loc File location
1009 astnode
*astnode_create_arithmetic(arithmetic_operator oper
, astnode
*left
, astnode
*right
, location loc
)
1011 astnode
*n
= astnode_create(ARITHMETIC_NODE
, loc
);
1013 /* This node has two children: left-hand side and right-hand side expression */
1014 /* For unary operators right-hand side should be <code>NULL</code> */
1015 astnode_add_children(n
, 2, left
, right
);
1020 * Creates an if node.
1021 * @param expr The expression involved in the if
1022 * @param then The statement(s) to assemble when expr is non-zero
1023 * @param elif List of CASE nodes (may be <code>NULL</code>)
1024 * @param els The final else-part (DEFAULT node) (may be <code>NULL</code>)
1025 * @param loc File location
1027 astnode
*astnode_create_if(astnode
*expr
, astnode
*then
, astnode
*elif
, astnode
*els
, location loc
)
1029 astnode
*n
= astnode_create(IF_NODE
, loc
);
1030 /* This node has several children: List of CASE nodes, possibly ended by DEFAULT node */
1031 astnode_add_child(n
, astnode_create_case(expr
, then
, loc
) );
1032 astnode_add_child(n
, elif
);
1034 astnode_add_child(n
, astnode_create_default(els
, loc
));
1040 * Creates a CASE node.
1041 * @param expr Expression
1042 * @param then List of statement to assemble when expr is non-zero (true)
1043 * @param loc File location
1045 astnode
*astnode_create_case(astnode
*expr
, astnode
*then
, location loc
)
1047 astnode
*n
= astnode_create(CASE_NODE
, loc
);
1048 /* This node has two children: expression to test and list of statements. */
1049 astnode_add_children(
1053 astnode_create_list(then
)
1059 * Creates a DEFAULT node.
1060 * @param stmts List of statements
1061 * @param loc File location
1063 astnode
*astnode_create_default(astnode
*stmts
, location loc
)
1065 astnode
*n
= astnode_create(DEFAULT_NODE
, loc
);
1066 /* This node has list of statements as children. */
1075 * Creates an ifdef node.
1076 * @param ident The identifier to check
1077 * @param then The statement(s) to assemble when ident is defined
1078 * @param els The statement(s) to assemble when ident is not defined (can be <code>NULL</code>)
1079 * @param loc File location
1081 astnode
*astnode_create_ifdef(astnode
*ident
, astnode
*then
, astnode
*els
, location loc
)
1083 astnode
*n
= astnode_create(IFDEF_NODE
, loc
);
1084 /* This node has three children: identifier to test, then-part, else-part */
1085 astnode_add_children(
1089 astnode_create_list(then
),
1090 astnode_create_list(els
)
1096 * Creates an ifndef node.
1097 * @param ident The identifier to check
1098 * @param then The statement(s) to assemble when ident is not defined
1099 * @param els The statement(s) to assemble when ident is defined (can be <code>NULL</code>)
1100 * @param loc File location
1102 astnode
*astnode_create_ifndef(astnode
*ident
, astnode
*then
, astnode
*els
, location loc
)
1104 astnode
*n
= astnode_create(IFNDEF_NODE
, loc
);
1105 /* This node has three children: identifier to test, then-part, else-part */
1106 astnode_add_children(
1110 astnode_create_list(then
),
1111 astnode_create_list(els
)
1117 * Creates a macro declaration node.
1118 * @param ident Name of macro
1119 * @param params List of parameters (can be <code>NULL</code>)
1120 * @param body Macro body
1121 * @param loc File location
1123 astnode
*astnode_create_macro_decl(astnode
*ident
, astnode
*params
, astnode
*body
, location loc
)
1125 astnode
*n
= astnode_create(MACRO_DECL_NODE
, loc
);
1126 /* This node has three children:
1127 1) An identifier, which is the name of the macro
1128 2) List of parameters
1129 3) List of statements, which is the macro body */
1130 astnode_add_children(
1134 astnode_create_list(params
),
1135 astnode_create_list(body
)
1141 * Creates a macro node.
1142 * @param ident Name of macro
1143 * @param args List of arguments (can be <code>NULL</code>)
1144 * @param loc File location
1146 astnode
*astnode_create_macro(astnode
*ident
, astnode
*args
, location loc
)
1148 astnode
*n
= astnode_create(MACRO_NODE
, loc
);
1149 astnode_add_children(
1153 astnode_create_list(args
)
1159 * Creates an equ node.
1160 * @param ident Identifier
1161 * @param expr Expression
1162 * @param loc File location
1164 astnode
*astnode_create_equ(astnode
*ident
, astnode
*expr
, location loc
)
1166 astnode
*n
= astnode_create(EQU_NODE
, loc
);
1167 astnode_add_children(n
, 2, ident
, expr
);
1172 * Creates an assign node.
1173 * @param ident Identifier
1174 * @param expr Expression
1175 * @param loc File location
1177 astnode
*astnode_create_assign(astnode
*ident
, astnode
*expr
, location loc
)
1179 astnode
*n
= astnode_create(ASSIGN_NODE
, loc
);
1180 astnode_add_children(n
, 2, ident
, expr
);
1185 * Creates a storage node.
1186 * @param type Type of storage
1187 * @param count Expression with contains count
1188 * @param loc File location
1190 astnode
*astnode_create_storage(astnode
*type
, astnode
*count
, location loc
)
1192 astnode
*n
= astnode_create(STORAGE_NODE
, loc
);
1193 /* Add the type of data (enumerated or identifier) */
1194 astnode_add_child(n
, type
);
1195 /* Second child: Count */
1196 if (count
== NULL
) {
1197 /* No count given, default=1 */
1198 count
= astnode_create_integer(1, loc
);
1200 astnode_add_child(n
, count
);
1205 * Creates an incsrc node.
1206 * @param file File specifier
1207 * @param loc File location
1209 astnode
*astnode_create_incsrc(astnode
*file
, location loc
)
1211 astnode
*n
= astnode_create(INCSRC_NODE
, loc
);
1212 /* One child: Path to file */
1213 astnode_add_child(n
, file
);
1218 * Creates an incbin node.
1219 * @param file File specifier
1220 * @param loc File location
1222 astnode
*astnode_create_incbin(astnode
*file
, location loc
)
1224 astnode
*n
= astnode_create(INCBIN_NODE
, loc
);
1225 /* One child: Path to file */
1226 astnode_add_child(n
, file
);
1231 * Creates a charmap node.
1232 * @param file File specifier
1233 * @param loc File location
1235 astnode
*astnode_create_charmap(astnode
*file
, location loc
)
1237 astnode
*n
= astnode_create(CHARMAP_NODE
, loc
);
1238 /* One child: Path to file */
1239 astnode_add_child(n
, file
);
1244 * Creates a structure (STRUC) instance node.
1245 * @param vals Values for the structure fields
1246 * @param loc File location
1248 astnode
*astnode_create_struc(astnode
*vals
, location loc
)
1250 astnode
*n
= astnode_create(STRUC_NODE
, loc
);
1251 astnode_add_child(n
, vals
);
1255 * Creates a structure (STRUC) declaration node.
1256 * @param id Structure identifier
1257 * @param stmts Statements of the structure declaration
1258 * @param loc File location
1260 astnode
*astnode_create_struc_decl(astnode
*id
, astnode
*stmts
, location loc
)
1262 astnode
*n
= astnode_create(STRUC_DECL_NODE
, loc
);
1263 astnode_add_child(n
, id
);
1264 astnode_add_child(n
, stmts
);
1269 * Creates a union declaration node.
1270 * @param id Union identifier
1271 * @param stmts Statements of the union declaration
1272 * @param loc File location
1274 astnode
*astnode_create_union_decl(astnode
*id
, astnode
*stmts
, location loc
)
1276 astnode
*n
= astnode_create(UNION_DECL_NODE
, loc
);
1277 astnode_add_child(n
, id
);
1278 astnode_add_child(n
, stmts
);
1283 * Creates an enum declaration node.
1284 * @param id Enum identifier
1285 * @param stmts Statements of the enum declaration
1286 * @param loc File location
1288 astnode
*astnode_create_enum_decl(astnode
*id
, astnode
*stmts
, location loc
)
1290 astnode
*n
= astnode_create(ENUM_DECL_NODE
, loc
);
1291 astnode_add_child(n
, id
);
1292 astnode_add_child(n
, stmts
);
1297 * Creates a record declaration node.
1298 * @param id Record identifier
1299 * @param fields Fields of the record
1300 * @param loc File location
1302 astnode
*astnode_create_record_decl(astnode
*id
, astnode
*fields
, location loc
)
1304 astnode
*n
= astnode_create(RECORD_DECL_NODE
, loc
);
1305 astnode_add_child(n
, id
);
1306 astnode_add_child(n
, fields
);
1311 * Creates a bitfield declaration node.
1312 * @param id Identifier
1313 * @param width Width of field
1314 * @param loc Location
1316 astnode
*astnode_create_bitfield_decl(astnode
*id
, astnode
*width
, location loc
)
1318 astnode
*n
= astnode_create(BITFIELD_DECL_NODE
, loc
);
1319 astnode_add_child(n
, id
);
1320 astnode_add_child(n
, width
);
1325 * Creates a public node.
1327 astnode
*astnode_create_public(astnode
*l
, location loc
)
1329 astnode
*n
= astnode_create(PUBLIC_NODE
, loc
);
1330 astnode_add_child(n
, l
);
1335 * Creates an extrn node.
1336 * @param l List of identifiers
1337 * @param t Symbol type specifier
1338 * @param f From unit (identifier, may be <code>NULL</code>)
1340 astnode
*astnode_create_extrn(astnode
*l
, astnode
*t
, astnode
*f
, location loc
)
1342 astnode
*n
= astnode_create(EXTRN_NODE
, loc
);
1343 astnode_add_child(n
, t
);
1344 astnode_add_child(n
, astnode_create_list(l
));
1345 astnode_add_child(n
, f
);
1350 * Creates a dataseg node.
1352 astnode
*astnode_create_dataseg(int modifiers
, location loc
)
1354 astnode
*n
= astnode_create(DATASEG_NODE
, loc
);
1355 n
->modifiers
= modifiers
;
1360 * Creates a codeseg node.
1362 astnode
*astnode_create_codeseg(location loc
)
1364 astnode
*n
= astnode_create(CODESEG_NODE
, loc
);
1369 * Creates a data node.
1370 * @param type Type specifier
1371 * @param data List of values
1372 * @param loc File location
1374 astnode
*astnode_create_data(astnode
*type
, astnode
*data
, location loc
)
1376 astnode
*n
= astnode_create(DATA_NODE
, loc
);
1377 astnode_add_child(n
, type
);
1378 astnode_add_child(n
, data
);
1383 * Creates a file path node.
1384 * This is similar to a string literal node, the only difference is semantics.
1385 * A file path node implies that the path can be relative to both current
1386 * directory and any of the directories in the search path.
1387 * @param path The path this node represents
1388 * @param loc File location
1390 astnode
*astnode_create_file_path(const char *path
, location loc
)
1392 astnode
*n
= astnode_create(FILE_PATH_NODE
, loc
);
1393 /* Allocate and store text */
1394 n
->file_path
= (char *)malloc(strlen(path
)+1);
1395 if (n
->file_path
!= NULL
) {
1396 strcpy(n
->file_path
, path
);
1402 * Creates a (global) label node.
1403 * @param s Name of label
1404 * @param addr Address
1405 * @param type Datatype (may be <code>NULL</code>)
1406 * @param loc Location
1408 astnode
*astnode_create_label(const char *s
, astnode
*addr
, astnode
*type
, location loc
)
1410 astnode
*n
= astnode_create(LABEL_NODE
, loc
);
1411 /* Allocate and store text */
1412 n
->label
= (char *)malloc(strlen(s
)+1);
1413 if (n
->label
!= NULL
) {
1414 strcpy(n
->label
, s
);
1416 /* Two children: Datatype and address */
1418 addr
= astnode_create_pc(loc
);
1421 type
= astnode_create_datatype(BYTE_DATATYPE
, NULL
, loc
);
1423 astnode_add_child(n
, type
);
1424 astnode_add_child(n
, addr
);
1429 * Creates a local label node.
1430 * @param s Name of label
1431 * @param loc Location
1433 astnode
*astnode_create_local_label(const char *s
, location loc
)
1435 astnode
*n
= astnode_create(LOCAL_LABEL_NODE
, loc
);
1436 /* Allocate and store text */
1437 n
->label
= (char *)malloc(strlen(s
)+1);
1438 if (n
->label
!= NULL
) {
1439 strcpy(n
->label
, s
);
1445 * Creates a local identifier node.
1446 * @param s Identifier
1447 * @param loc Location
1449 astnode
*astnode_create_local_id(const char *s
, location loc
)
1451 astnode
*n
= astnode_create(LOCAL_ID_NODE
, loc
);
1452 /* Allocate and store text */
1453 n
->ident
= (char *)malloc(strlen(s
)+1);
1454 if (n
->ident
!= NULL
) {
1455 strcpy(n
->ident
, s
);
1461 * Creates a list node.
1462 * This is a way to group a list of nodes in a parent node.
1463 * @param l List of nodes to group in list node
1465 astnode
*astnode_create_list(astnode
*l
)
1468 /* Create the node */
1470 n
= astnode_create(LIST_NODE
, l
->loc
);
1471 /* Add list of values */
1472 astnode_add_child(n
, l
);
1474 /* Make a node with zero children */
1477 n
= astnode_create(LIST_NODE
, dummyloc
);
1483 * Creates a PC node.
1484 * @param loc File location
1486 astnode
*astnode_create_pc(location loc
)
1488 return astnode_create(CURRENT_PC_NODE
, loc
);
1492 * Creates a binary node.
1493 * @param bin Dynamically allocated (malloc() ) data that this node wraps. Will be freed automatically by astnode_finalize()
1494 * @param size Size of bin
1495 * @param loc File location
1497 astnode
*astnode_create_binary(unsigned char *bin
, int size
, location loc
)
1499 astnode
*n
= astnode_create(BINARY_NODE
, loc
);
1500 n
->binary
.data
= bin
;
1501 n
->binary
.size
= size
;
1506 * Creates a tombstone node, which is a marker node that says that another node
1508 * @param type The type of node that used to live here
1509 * @param loc File location
1511 astnode
*astnode_create_tombstone(astnode_type type
, location loc
)
1513 astnode
*n
= astnode_create(TOMBSTONE_NODE
, loc
);
1514 n
->param
= (long)type
;
1519 * Creates a dot operator node.
1520 * Represents a structure field access of the form 'before.after'.
1521 * @param before Structure identifier
1522 * @param after Field identifier (can be another dot op, or an identifier)
1524 astnode
*astnode_create_dot(astnode
*before
, astnode
*after
, location loc
)
1526 astnode
*n
= astnode_create(DOT_NODE
, loc
);
1527 astnode_add_child(n
, before
);
1528 astnode_add_child(n
, after
);
1533 * Creates a sizeof operator node.
1534 * @param expr Expression (datatype?)
1535 * @param loc Location
1537 astnode
*astnode_create_sizeof(astnode
*expr
, location loc
)
1539 astnode
*n
= astnode_create(SIZEOF_NODE
, loc
);
1540 astnode_add_child(n
, expr
);
1545 * Creates a datatype node.
1546 * @param t The datatype this node represents
1547 * @param id If the datatype is a custom one, this is its name
1548 * @param loc Location
1550 astnode
*astnode_create_datatype(datatype t
, astnode
*id
, location loc
)
1552 astnode
*n
= astnode_create(DATATYPE_NODE
, loc
);
1555 astnode_add_child(n
, id
);
1561 * Creates a variable declaration node.
1562 * @param modifiers PUBLIC_FLAG | ZEROPAGE_FLAG
1563 * @param id Identifier
1564 * @param data Datatype+initializer
1565 * @param loc Location
1567 astnode
*astnode_create_var_decl(int modifiers
, astnode
*id
, astnode
*data
, location loc
)
1569 astnode
*n
= astnode_create(VAR_DECL_NODE
, loc
);
1570 n
->modifiers
= modifiers
;
1571 astnode_add_child(n
, id
);
1572 astnode_add_child(n
, data
);
1579 astnode
*astnode_create_scope(astnode
*left
, astnode
*right
, location loc
)
1581 astnode
*n
= astnode_create(SCOPE_NODE
, loc
);
1582 astnode_add_child(n
, left
);
1583 astnode_add_child(n
, right
);
1588 * Creates a procedure (PROC) node.
1589 * @param ident Name of procedure
1590 * @param stmts Procedure statements
1591 * @param loc File location
1593 astnode
*astnode_create_proc(astnode
*ident
, astnode
*stmts
, location loc
)
1595 astnode
*n
= astnode_create(PROC_NODE
, loc
);
1596 /* This node has two children:
1597 1) An identifier, which is the name of the procedure
1598 2) List of statements, which is the procedure body */
1599 astnode_add_children(
1603 astnode_create_list(stmts
)
1609 * Creates a REPT node.
1610 * @param expr Number of times to repeat statements
1611 * @param stmts Statement list
1612 * @param loc File location
1614 astnode
*astnode_create_rept(astnode
*expr
, astnode
*stmts
, location loc
)
1616 astnode
*n
= astnode_create(REPT_NODE
, loc
);
1617 /* This node has two children:
1618 1) An expression, which is the repeat count
1619 2) List of statements, which is the (anonymous) macro body */
1620 astnode_add_children(
1624 astnode_create_list(stmts
)
1630 * Creates a WHILE node.
1631 * @param expr Boolean expression
1632 * @param stmts Statement list
1633 * @param loc File location
1635 astnode
*astnode_create_while(astnode
*expr
, astnode
*stmts
, location loc
)
1637 astnode
*n
= astnode_create(WHILE_NODE
, loc
);
1638 /* This node has two children:
1639 1) A boolean expression
1640 2) List of statements, which is the (anonymous) macro body */
1641 astnode_add_children(
1645 astnode_create_list(stmts
)
1651 * Creates a MESSAGE node.
1652 * @param expr Message to print.
1653 * @param loc File location
1655 astnode
*astnode_create_message(astnode
*expr
, location loc
)
1657 astnode
*n
= astnode_create(MESSAGE_NODE
, loc
);
1658 astnode_add_child(n
, expr
);
1663 * Creates a WARNING node.
1664 * @param str Warning to print.
1665 * @param loc File location
1667 astnode
*astnode_create_warning(astnode
*str
, location loc
)
1669 astnode
*n
= astnode_create(WARNING_NODE
, loc
);
1670 astnode_add_child(n
, str
);
1675 * Creates an ERROR node.
1676 * @param str Error to print.
1677 * @param loc File location
1679 astnode
*astnode_create_error(astnode
*str
, location loc
)
1681 astnode
*n
= astnode_create(ERROR_NODE
, loc
);
1682 astnode_add_child(n
, str
);
1687 * Creates a forward branch declaration node.
1688 * @param ident Branch name
1689 * @param loc File location
1691 astnode
*astnode_create_forward_branch_decl(const char *ident
, location loc
)
1693 astnode
*n
= astnode_create(FORWARD_BRANCH_DECL_NODE
, loc
);
1694 /* Allocate and store text */
1695 n
->ident
= (char *)malloc(strlen(ident
)+1);
1696 if (n
->ident
!= NULL
) {
1697 strcpy(n
->ident
, ident
);
1703 * Creates a backward branch declaration node.
1704 * @param ident Branch name
1705 * @param loc File location
1707 astnode
*astnode_create_backward_branch_decl(const char *ident
, location loc
)
1709 astnode
*n
= astnode_create(BACKWARD_BRANCH_DECL_NODE
, loc
);
1710 /* Allocate and store text */
1711 n
->ident
= (char *)malloc(strlen(ident
)+1);
1712 if (n
->ident
!= NULL
) {
1713 strcpy(n
->ident
, ident
);
1719 * Creates a forward branch reference node.
1720 * @param ident Branch name
1721 * @param loc File location
1723 astnode
*astnode_create_forward_branch(const char *ident
, location loc
)
1725 astnode
*n
= astnode_create(FORWARD_BRANCH_NODE
, loc
);
1726 /* Allocate and store text */
1727 n
->ident
= (char *)malloc(strlen(ident
)+1);
1728 if (n
->ident
!= NULL
) {
1729 strcpy(n
->ident
, ident
);
1735 * Creates a backward branch reference node.
1736 * @param ident Branch name
1737 * @param loc File location
1739 astnode
*astnode_create_backward_branch(const char *ident
, location loc
)
1741 astnode
*n
= astnode_create(BACKWARD_BRANCH_NODE
, loc
);
1742 /* Allocate and store text */
1743 n
->ident
= (char *)malloc(strlen(ident
)+1);
1744 if (n
->ident
!= NULL
) {
1745 strcpy(n
->ident
, ident
);
1751 * Creates a mask operator node.
1752 * @param expr Expression
1753 * @param loc Location
1755 astnode
*astnode_create_mask(astnode
*expr
, location loc
)
1757 astnode
*n
= astnode_create(MASK_NODE
, loc
);
1758 astnode_add_child(n
, expr
);
1763 * Creates an ALIGN node.
1764 * @param idents List of identifiers
1765 * @param expr Expression
1766 * @param loc File location
1768 astnode
*astnode_create_align(astnode
*idents
, astnode
*expr
, location loc
)
1770 astnode
*n
= astnode_create(ALIGN_NODE
, loc
);
1771 astnode_add_child(n
, astnode_create_list(idents
) );
1772 astnode_add_child(n
, expr
);
1777 * Creates an INDEX node.
1778 * @param ident Identifier being indexed
1779 * @param expr Index expression
1780 * @param loc File location
1782 astnode
*astnode_create_index(astnode
*ident
, astnode
*expr
, location loc
)
1784 astnode
*n
= astnode_create(INDEX_NODE
, loc
);
1785 astnode_add_child(n
, ident
);
1786 astnode_add_child(n
, expr
);
1791 * Creates an ORG node.
1792 * @param addr Address
1793 * @param loc File location
1795 astnode
*astnode_create_org(astnode
*addr
, location loc
)
1797 astnode
*n
= astnode_create(ORG_NODE
, loc
);
1798 astnode_add_child(n
, addr
);