compile with newer version of bison
[xorcyst.git] / astnode.c
blob1957d37e03870971b8a6f2dfed854b574262d8a1
1 /*
2 * $Id: astnode.c,v 1.15 2007/08/12 18:58:12 khansen Exp $
3 * $Log: astnode.c,v $
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
14 * const
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
20 * xorcyst 1.4.2
21 * added create_index()
23 * Revision 1.9 2004/12/19 19:58:23 kenth
24 * xorcyst 1.4.0
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
36 * xorcyst 1.3.0
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
48 * Initial revision
52 /**
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
70 /**
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.
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <stdarg.h>
79 #include <string.h>
80 #include <assert.h>
81 #include "astnode.h"
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.
90 /**
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)
97 switch (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)
123 switch (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) {
190 switch (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.
254 * @param dt 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.
271 * @param op Operator
272 * @return String representation of op
274 const char *operator_to_string(int op)
276 switch (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";
304 * Indents.
305 * @param nlevels Levels
307 void indent(int nlevels)
309 int i;
310 for (i=0; i<nlevels; i++) {
311 printf(" ");
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)
322 int i;
323 /* Indent so it looks pretty */
324 indent(level);
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:
339 printf(
340 "(%s)",
341 operator_to_string(n->oper)
343 break;
345 case INSTRUCTION_NODE:
346 printf(
347 "(%s,%s,%.2X)",
348 instr_mnemonic_to_string(n->instr.mnemonic),
349 addressing_mode_to_string(n->instr.mode),
350 n->instr.opcode
352 break;
354 case DATATYPE_NODE:
355 printf(
356 "(%s)",
357 datatype_to_string(n)
359 break;
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);
366 break;
368 case TOMBSTONE_NODE:
369 printf(
370 "(%s)",
371 astnode_type_to_string(n->param)
373 break;
375 default:
376 /* Has no internal attributes */
377 break;
379 printf("\n");
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 /* Allocate memory for node struct */
404 astnode *n = (astnode *)malloc(sizeof(astnode));
405 /* Fill in struct only if alloc succeeded */
406 if (n != NULL) {
407 n->type = type;
408 n->loc = loc;
409 n->flags = 0;
410 n->label = NULL;
411 n->string = NULL;
412 n->parent = n->first_child = n->prev_sibling = n->next_sibling = NULL;
414 return n;
418 * Finalizes a node.
419 * Any children of the node are also finalized, recursively.
420 * @param n The node to finalize.
422 void astnode_finalize(astnode *n)
424 astnode *c;
425 /* Remove the node from the tree it's in. */
426 astnode_remove(n);
427 /* Finalize all its children recursively. */
428 while ((c = astnode_get_first_child(n)) != NULL) {
429 astnode_remove_child(n, c);
430 astnode_finalize(c);
432 /* Free up memory. */
433 switch (astnode_get_type(n)) {
434 case LABEL_NODE: SAFE_FREE(n->label); break;
435 case LOCAL_LABEL_NODE: SAFE_FREE(n->label); break;
436 case STRING_NODE: SAFE_FREE(n->string); break;
437 case IDENTIFIER_NODE: SAFE_FREE(n->ident); break;
438 case LOCAL_ID_NODE: SAFE_FREE(n->ident); break;
439 case FILE_PATH_NODE: SAFE_FREE(n->file_path);break;
440 case BINARY_NODE: SAFE_FREE(n->binary.data); break;
441 case FORWARD_BRANCH_DECL_NODE:
442 case BACKWARD_BRANCH_DECL_NODE:
443 case FORWARD_BRANCH_NODE:
444 case BACKWARD_BRANCH_NODE:
445 SAFE_FREE(n->ident);
446 break;
447 default:
448 /* Has no internal attributes that are dynamically allocated */
449 break;
451 SAFE_FREE(n);
455 * Gets the node's type.
456 * @param n The node whose type to get
457 * @return The node's type (astnode_type)
459 astnode_type astnode_get_type(const astnode *n)
461 return (n != NULL) ? n->type : NULL_NODE;
465 * Sets the parent field of all nodes in c to p.
467 void astnode_set_parent(astnode *c, astnode *p)
469 astnode *n;
470 for (n = c; n != NULL; n = n->next_sibling) {
471 n->parent = p;
476 * Replaces a node with another.
478 void astnode_replace(astnode *old_node, astnode *new_node)
480 astnode *p;
481 int i;
482 /* Get the parent of the node to be replaced */
483 p = astnode_get_parent(old_node);
484 if (p != NULL) {
485 /* Call remove_child on parent */
486 i = astnode_remove_child(p, old_node);
487 /* Insert new child at old child's position */
488 astnode_insert_child(p, new_node, i);
493 * Removes a node from a tree.
494 * @param n The node to remove (can't be the root of the tree)
496 void astnode_remove(astnode *n)
498 astnode *p = astnode_get_parent(n);
499 if (n && p) {
500 astnode_remove_child(p, n);
505 * Removes a child node.
506 * @param p Parent node
507 * @param c Child node
508 * @return Index of the removed node
510 int astnode_remove_child(astnode *p, astnode *c)
512 int i;
513 i = astnode_get_child_index(p, c);
514 if (i == 0) {
515 /* Remove head of list. */
516 p->first_child = c->next_sibling;
517 if (p->first_child) {
518 p->first_child->prev_sibling = NULL;
520 c->parent = c->next_sibling = c->prev_sibling = NULL;
522 else if (i > 0) {
523 c->prev_sibling->next_sibling = c->next_sibling;
524 if (c->next_sibling) {
525 c->next_sibling->prev_sibling = c->prev_sibling;
527 c->parent = c->next_sibling = c->prev_sibling = NULL;
529 return i;
533 * Removes child node at specified index.
534 * @param p Parent node
535 * @param i Index >= 0
537 astnode *astnode_remove_child_at(astnode *p, int i)
539 astnode *c = astnode_get_child(p, i);
540 astnode_remove_child(p, c);
541 return c;
545 * Removes all children from a node and returns them as a list.
546 * @param p Parent node whose children to remove
548 astnode *astnode_remove_children(astnode *p)
550 astnode *c;
551 if (p == NULL) { return NULL; }
552 if (p->first_child != NULL) {
553 c = p->first_child;
554 p->first_child = NULL;
555 /* Set parent of all siblings to NULL. */
556 astnode_set_parent(c, NULL);
557 /* Return the list of children */
558 return c;
560 else {
561 /* Has no children. */
562 return NULL;
567 * Inserts a list of nodes as children.
570 void astnode_insert_child(astnode *p, astnode *c, int i)
572 astnode *n;
573 astnode *x;
574 if (p && c) {
575 x = astnode_get_child(p, i); /* Current child at that position */
576 if (x == NULL) {
577 /* There isn't a node here. Just add to end. */
578 astnode_add_child(p, c);
580 else {
581 n = astnode_get_last_sibling(c);
582 /* Make c..n precede x */
583 c->prev_sibling = x->prev_sibling;
584 if (x->prev_sibling) {
585 x->prev_sibling->next_sibling = c;
587 n->next_sibling = x;
588 x->prev_sibling = n;
590 /* Set parent */
591 astnode_set_parent(c, p);
592 /* Check if head */
593 if (i == 0) {
594 p->first_child = c;
600 * Gets the last node in a list.
602 astnode *astnode_get_last_sibling(const astnode *n)
604 astnode *s = NULL;
605 if (n) {
606 for (s = (astnode *)n; s->next_sibling != NULL; s = s->next_sibling) ;
608 return s;
612 * Gets the parent of a node.
613 * @param n The node whose parent to get
614 * @return The node's parent, or <code>NULL</code> if it has none
616 astnode *astnode_get_parent(const astnode *n)
618 return n ? n->parent : NULL;
622 * Adds child(ren) to a node.
623 * @param n The parent-to-be
624 * @param new_child List of children-to-be
626 void astnode_add_child(astnode *n, astnode *new_child)
628 if (n && new_child) {
629 if (n->first_child == NULL) {
630 /* This node has no children, add this as the first one */
631 n->first_child = new_child;
632 astnode_set_parent(new_child, n);
634 else {
635 astnode_add_sibling(n->first_child, new_child);
641 * Adds any number of children to a node.
642 * @param n The parent-to-be
644 void astnode_add_children(astnode *n, int count, ...)
646 int i;
647 va_list ap;
648 astnode *c;
650 va_start(ap, count);
651 for (i=0; i<count; i++) {
652 c = va_arg(ap, astnode*);
653 astnode_add_child(n, c);
655 va_end(ap);
659 * Adds sibling(s) to a node.
660 * @param brother List of existing siblings
661 * @param sister List of new siblings
663 void astnode_add_sibling(astnode *brother, astnode *sister)
665 astnode *n;
666 astnode *p;
667 if (brother && sister) {
668 /* Add to end of list */
669 n = astnode_get_last_sibling(brother);
670 n->next_sibling = sister;
671 sister->prev_sibling = n;
672 p = astnode_get_parent(brother);
673 astnode_set_parent(sister, p);
678 * Gets the child node at the specified index.
679 * @param n The parent node
680 * @param index The index of the desired child node
682 astnode *astnode_get_child(const astnode *n, int index)
684 int i;
685 astnode *c;
686 if (n) {
687 c = n->first_child;
688 for (i = 0; i != index; i++) {
689 if (c == NULL) {
690 /* No child at that index. */
691 break;
693 c = c->next_sibling;
695 return c;
697 /* Node is NULL, so return NULL */
698 return NULL;
702 * Gets a node's first child.
703 * @param n The node
705 astnode *astnode_get_first_child(const astnode *n)
707 return (n == NULL) ? NULL : n->first_child;
711 * Gets the index of a child node.
712 * @param p Parent node
713 * @param c Child node
714 * @return Index of c >= 0, or -1 if invalid input
716 int astnode_get_child_index(const astnode *p, const astnode *c)
718 int i;
719 astnode *n;
720 if (p && c) {
721 for (i=0, n=p->first_child; (n != c) && (n != NULL); i++, n=n->next_sibling);
722 return n ? i : -1;
724 return -1;
728 * Gets the number of children a node has.
729 * @param p Node whose children count to get
731 int astnode_get_child_count(const astnode *p)
733 astnode *c;
734 int count = 0;
735 if (p != NULL) {
736 for (c = p->first_child; c != NULL; count++, c = c->next_sibling);
738 return count;
742 * Clones a node and all its children.
743 * @param n The node to clone
744 * @param loc File location
746 astnode *astnode_clone(const astnode *n, location loc)
748 astnode *c;
749 astnode *n_c;
750 if (n == NULL) { return NULL; }
751 /* Create node */
752 c = astnode_create(astnode_get_type(n), loc);
753 /* Copy attributes */
754 switch (astnode_get_type(n)) {
755 case INTEGER_NODE:
756 c->integer = n->integer;
757 break;
759 case STRING_NODE:
760 case IDENTIFIER_NODE:
761 case FILE_PATH_NODE:
762 case LABEL_NODE:
763 case LOCAL_LABEL_NODE:
764 case LOCAL_ID_NODE:
765 case FORWARD_BRANCH_DECL_NODE:
766 case BACKWARD_BRANCH_DECL_NODE:
767 case FORWARD_BRANCH_NODE:
768 case BACKWARD_BRANCH_NODE:
769 c->string = (char *)malloc(strlen(n->string)+1);
770 if (c->string != NULL) {
771 strcpy(c->string, n->string);
773 break;
775 case ARITHMETIC_NODE:
776 c->oper = n->oper;
777 break;
779 case INSTRUCTION_NODE:
780 c->instr = n->instr;
781 break;
783 case BINARY_NODE:
784 c->binary = n->binary;
785 break;
787 case DATATYPE_NODE:
788 c->datatype = n->datatype;
789 break;
791 default:
792 c->param = n->param;
794 /* Clone children (TODO: OPTIMIZE THIS) */
795 for (n_c=n->first_child; n_c != NULL; n_c=n_c->next_sibling) {
796 astnode_add_child(c, astnode_clone(n_c, loc));
798 /* Return the clone */
799 return c;
803 * Tests if two nodes are equal.
805 int astnode_equal(const astnode *n1, const astnode *n2)
807 int i;
808 /* Verify that types are the same */
809 if (astnode_get_type(n1) != astnode_get_type(n2)) {
810 return 0; /* Types don't match -- not equal */
812 /* Verify that internal data is the same */
813 switch (astnode_get_type(n1)) {
814 case ARITHMETIC_NODE: if (n1->oper != n2->oper) return 0; break;
815 case INTEGER_NODE: if (n1->integer != n2->integer) return 0; break;
816 case STRING_NODE: if (strcmp(n1->string, n2->string)) return 0; break;
817 case IDENTIFIER_NODE: if (strcmp(n1->ident, n2->ident)) return 0; break;
818 case LOCAL_ID_NODE: if (strcmp(n1->ident, n2->ident)) return 0; break;
819 case FILE_PATH_NODE: if (strcmp(n1->file_path, n2->file_path)) return 0; break;
820 case LABEL_NODE: if (strcmp(n1->label, n2->label)) return 0; break;
821 case LOCAL_LABEL_NODE: if (strcmp(n1->label, n2->label)) return 0; break;
822 case BINARY_NODE: if (n1->binary.size != n2->binary.size) return 0; break;
823 case DATATYPE_NODE: if (n1->datatype != n2->datatype) return 0; break;
824 case TOMBSTONE_NODE: if (n1->param != n2->param) return 0; break;
826 case INSTRUCTION_NODE:
827 if ( (n1->instr.mnemonic != n2->instr.mnemonic)
828 || (n1->instr.mode != n2->instr.mode) ) {
829 return 0;
831 break;
833 case FORWARD_BRANCH_DECL_NODE:
834 case BACKWARD_BRANCH_DECL_NODE:
835 case FORWARD_BRANCH_NODE:
836 case BACKWARD_BRANCH_NODE:
837 if (strcmp(n1->ident, n2->ident))
838 return 0;
839 break;
841 default:
842 /* Has no internal attributes */
843 break;
845 /* Verify that they have the same number of children */
846 if (astnode_get_child_count(n1) != astnode_get_child_count(n2)) {
847 return 0;
849 /* Verify that children are equal */
850 for (i=0; i<astnode_get_child_count(n1); i++) {
851 if (!astnode_equal(astnode_get_child(n1, i), astnode_get_child(n2, i))) {
852 return 0;
855 /* Equal. */
856 return 1;
860 * Gets the ancestor of a node.
861 * @param n Node whose ancestor to get
862 * @param back How many generations to go back (0=father, 1=grandfather etc.)
864 astnode *astnode_get_ancestor(const astnode *n, int back)
866 int i;
867 astnode *a = astnode_get_parent(n);
868 for (i=0; i<back; i++) {
869 a = astnode_get_parent(a);
871 return a;
875 * Tests if a node is a descendant of a node of a particular type.
876 * @param n Node
877 * @param type Ancestor's type
878 * @return 0 if no such ancestor, 1 otherwise
880 int astnode_has_ancestor_of_type(const astnode *n, astnode_type type)
882 astnode *a;
883 for (a = astnode_get_parent(n); a != NULL; a = astnode_get_parent(a) ) {
884 if (astnode_is_type(a, type)) {
885 return 1;
888 return 0;
892 * Gets the next sibling of a node.
893 * @param n Node
895 astnode *astnode_get_next_sibling(const astnode *n)
897 if (n == NULL) { return NULL; }
898 return n->next_sibling;
902 * Gets the previous sibling of a node.
903 * @param n Node
905 astnode *astnode_get_prev_sibling(const astnode *n)
907 if (n == NULL) { return NULL; }
908 return n->prev_sibling;
912 * Tests if a node is a literal.
913 * @param n Node to test
915 int astnode_is_literal(const astnode *n)
917 switch (astnode_get_type(n)) {
918 case INTEGER_NODE:
919 case STRING_NODE:
920 /* A literal */
921 return 1;
923 default:
924 /* Not a literal */
925 break;
927 /* Not a literal */
928 return 0;
931 /*---------------------------------------------------------------------------*/
932 /* Functions for creating AST nodes of specific type.
933 1:1 correspondence between astnode_create_* and *_INSTRUCTION.
934 Each takes the operands required for that node type,
935 calls astnode_create() and then fills in fields and adds children (if any).
938 astnode *astnode_create_null(location loc)
940 /* Create the node */
941 astnode *n = astnode_create(NULL_NODE, loc);
942 /* Return the newly created node */
943 return n;
947 * Creates a CPU instruction node.
948 * @param mnemonic The instruction mnemonic
949 * @param mode The addressing mode used
950 * @param operand The instruction operand (an expression) (can be <code>NULL</code>)
951 * @param loc File location
953 astnode *astnode_create_instruction(int mnemonic, addressing_mode mode, astnode *operand, location loc)
955 /* Create the node */
956 astnode *n = astnode_create(INSTRUCTION_NODE, loc);
957 /* Store the mnemonic and addressing mode */
958 n->instr.mnemonic = mnemonic;
959 n->instr.mode = mode;
960 /* This node has one child: The operand, which is an expression */
961 astnode_add_child(n, operand);
962 /* Return the newly created node */
963 return n;
967 * Creates an identifier node.
968 * @param ident The identifier (a string)
969 * @param loc File location
971 astnode *astnode_create_identifier(const char *ident, location loc)
973 /* Create the node */
974 astnode *n = astnode_create(IDENTIFIER_NODE, loc);
975 /* Allocate and store text */
976 n->ident = (char *)malloc(strlen(ident)+1);
977 if (n->ident != NULL) {
978 strcpy(n->ident, ident);
980 /* Return the newly created node */
981 return n;
985 * Creates an integer literal node.
986 * @param value The integer literal
987 * @param loc File location
989 astnode *astnode_create_integer(int value, location loc)
991 /* Create the node */
992 astnode *n = astnode_create(INTEGER_NODE, loc);
993 /* Store the integer which this node represents */
994 n->integer = value;
995 /* Return the newly created node */
996 return n;
1000 * Creates a string literal node.
1001 * @param value The string literal
1002 * @param loc File location
1004 astnode *astnode_create_string(const char *value, location loc)
1006 /* Create the node */
1007 astnode *n = astnode_create(STRING_NODE, loc);
1008 /* Allocate and store text */
1009 n->string = (char *)malloc(strlen(value)+1);
1010 if (n->string != NULL) {
1011 strcpy(n->string, value);
1013 /* Return the newly created node */
1014 return n;
1018 * Creates an expression node (unary or binary).
1019 * @param oper The operator
1020 * @param left Left operand
1021 * @param right Right operand (can be <code>NULL</code>)
1022 * @param loc File location
1024 astnode *astnode_create_arithmetic(arithmetic_operator oper, astnode *left, astnode *right, location loc)
1026 /* Create the node */
1027 astnode *n = astnode_create(ARITHMETIC_NODE, loc);
1028 /* Store the operator, which describes the type of expression */
1029 n->oper = oper;
1030 /* This node has two children: left-hand side and right-hand side expression */
1031 /* For unary operators right-hand side should be <code>NULL</code> */
1032 astnode_add_children(n, 2, left, right);
1033 /* Return the newly created node */
1034 return n;
1038 * Creates an if node.
1039 * @param expr The expression involved in the if
1040 * @param then The statement(s) to assemble when expr is non-zero
1041 * @param elif List of CASE nodes (may be <code>NULL</code>)
1042 * @param els The final else-part (DEFAULT node) (may be <code>NULL</code>)
1043 * @param loc File location
1045 astnode *astnode_create_if(astnode *expr, astnode *then, astnode *elif, astnode *els, location loc)
1047 /* Create the node */
1048 astnode *n = astnode_create(IF_NODE, loc);
1049 /* This node has several children: List of CASE nodes, possibly ended by DEFAULT node */
1050 astnode_add_child(n, astnode_create_case(expr, then, loc) );
1051 astnode_add_child(n, elif);
1052 if (els != NULL) {
1053 astnode_add_child(n, astnode_create_default(els, loc));
1055 /* Return the newly created node */
1056 return n;
1060 * Creates a CASE node.
1061 * @param expr Expression
1062 * @param then List of statement to assemble when expr is non-zero (true)
1063 * @param loc File location
1065 astnode *astnode_create_case(astnode *expr, astnode *then, location loc)
1067 /* Create the node */
1068 astnode *n = astnode_create(CASE_NODE, loc);
1069 /* This node has two children: expression to test and list of statements. */
1070 astnode_add_children(
1073 expr,
1074 astnode_create_list(then)
1076 /* Return the newly created node */
1077 return n;
1081 * Creates a DEFAULT node.
1082 * @param stmts List of statements
1083 * @param loc File location
1085 astnode *astnode_create_default(astnode *stmts, location loc)
1087 /* Create the node */
1088 astnode *n = astnode_create(DEFAULT_NODE, loc);
1089 /* This node has list of statements as children. */
1090 astnode_add_child(
1092 stmts
1094 /* Return the newly created node */
1095 return n;
1099 * Creates an ifdef node.
1100 * @param ident The identifier to check
1101 * @param then The statement(s) to assemble when ident is defined
1102 * @param els The statement(s) to assemble when ident is not defined (can be <code>NULL</code>)
1103 * @param loc File location
1105 astnode *astnode_create_ifdef(astnode *ident, astnode *then, astnode *els, location loc)
1107 /* Create the node */
1108 astnode *n = astnode_create(IFDEF_NODE, loc);
1109 /* This node has three children: identifier to test, then-part, else-part */
1110 astnode_add_children(
1113 ident,
1114 astnode_create_list(then),
1115 astnode_create_list(els)
1117 /* Return the newly created node */
1118 return n;
1122 * Creates an ifndef node.
1123 * @param ident The identifier to check
1124 * @param then The statement(s) to assemble when ident is not defined
1125 * @param els The statement(s) to assemble when ident is defined (can be <code>NULL</code>)
1126 * @param loc File location
1128 astnode *astnode_create_ifndef(astnode *ident, astnode *then, astnode *els, location loc)
1130 /* Create the node */
1131 astnode *n = astnode_create(IFNDEF_NODE, loc);
1132 /* This node has three children: identifier to test, then-part, else-part */
1133 astnode_add_children(
1136 ident,
1137 astnode_create_list(then),
1138 astnode_create_list(els)
1140 /* Return the newly created node */
1141 return n;
1145 * Creates a macro declaration node.
1146 * @param ident Name of macro
1147 * @param params List of parameters (can be <code>NULL</code>)
1148 * @param body Macro body
1149 * @param loc File location
1151 astnode *astnode_create_macro_decl(astnode *ident, astnode *params, astnode *body, location loc)
1153 /* Create the node */
1154 astnode *n = astnode_create(MACRO_DECL_NODE, loc);
1155 /* This node has three children:
1156 1) An identifier, which is the name of the macro
1157 2) List of parameters
1158 3) List of statements, which is the macro body */
1159 astnode_add_children(
1162 ident,
1163 astnode_create_list(params),
1164 astnode_create_list(body)
1166 /* Return the newly created node */
1167 return n;
1171 * Creates a macro node.
1172 * @param ident Name of macro
1173 * @param args List of arguments (can be <code>NULL</code>)
1174 * @param loc File location
1176 astnode *astnode_create_macro(astnode *ident, astnode *args, location loc)
1178 /* Create the node */
1179 astnode *n = astnode_create(MACRO_NODE, loc);
1180 /* Add the children */
1181 astnode_add_children(
1184 ident,
1185 astnode_create_list(args)
1187 /* Return the newly created node */
1188 return n;
1192 * Creates an equ node.
1193 * @param ident Identifier
1194 * @param expr Expression
1195 * @param loc File location
1197 astnode *astnode_create_equ(astnode *ident, astnode *expr, location loc)
1199 /* Create the node */
1200 astnode *n = astnode_create(EQU_NODE, loc);
1201 /* Add the children */
1202 astnode_add_children(n, 2, ident, expr);
1203 /* Return the newly created node */
1204 return n;
1208 * Creates an assign node.
1209 * @param ident Identifier
1210 * @param expr Expression
1211 * @param loc File location
1213 astnode *astnode_create_assign(astnode *ident, astnode *expr, location loc)
1215 /* Create the node */
1216 astnode *n = astnode_create(ASSIGN_NODE, loc);
1217 /* Add the children */
1218 astnode_add_children(n, 2, ident, expr);
1219 /* Return the newly created node */
1220 return n;
1224 * Creates a storage node.
1225 * @param type Type of storage
1226 * @param count Expression with contains count
1227 * @param loc File location
1229 astnode *astnode_create_storage(astnode *type, astnode *count, location loc)
1231 /* Create the node */
1232 astnode *n = astnode_create(STORAGE_NODE, loc);
1233 /* Add the type of data (enumerated or identifier) */
1234 astnode_add_child(n, type);
1235 /* Second child: Count */
1236 if (count == NULL) {
1237 /* No count given, default=1 */
1238 count = astnode_create_integer(1, loc);
1240 astnode_add_child(n, count);
1241 /* Return the newly created node */
1242 return n;
1246 * Creates an incsrc node.
1247 * @param file File specifier
1248 * @param loc File location
1250 astnode *astnode_create_incsrc(astnode *file, location loc)
1252 /* Create the node */
1253 astnode *n = astnode_create(INCSRC_NODE, loc);
1254 /* One child: Path to file */
1255 astnode_add_child(n, file);
1256 /* Return the newly created node */
1257 return n;
1261 * Creates an incbin node.
1262 * @param file File specifier
1263 * @param loc File location
1265 astnode *astnode_create_incbin(astnode *file, location loc)
1267 /* Create the node */
1268 astnode *n = astnode_create(INCBIN_NODE, loc);
1269 /* One child: Path to file */
1270 astnode_add_child(n, file);
1271 /* Return the newly created node */
1272 return n;
1276 * Creates a charmap node.
1277 * @param file File specifier
1278 * @param loc File location
1280 astnode *astnode_create_charmap(astnode *file, location loc)
1282 /* Create the node */
1283 astnode *n = astnode_create(CHARMAP_NODE, loc);
1284 /* One child: Path to file */
1285 astnode_add_child(n, file);
1286 /* Return the newly created node */
1287 return n;
1291 * Creates a structure (STRUC) instance node.
1292 * @param vals Values for the structure fields
1293 * @param loc File location
1295 astnode *astnode_create_struc(astnode *vals, location loc)
1297 /* Create the node */
1298 astnode *n = astnode_create(STRUC_NODE, loc);
1299 /* Children: value list */
1300 astnode_add_child(n, vals);
1301 /* Return the newly created node */
1302 return n;
1305 * Creates a structure (STRUC) declaration node.
1306 * @param id Structure identifier
1307 * @param stmts Statements of the structure declaration
1308 * @param loc File location
1310 astnode *astnode_create_struc_decl(astnode *id, astnode *stmts, location loc)
1312 /* Create the node */
1313 astnode *n = astnode_create(STRUC_DECL_NODE, loc);
1314 /* Two children: Identifier, statement list */
1315 astnode_add_child(n, id);
1316 astnode_add_child(n, stmts);
1317 /* Return the newly created node */
1318 return n;
1322 * Creates a union declaration node.
1323 * @param id Union identifier
1324 * @param stmts Statements of the union declaration
1325 * @param loc File location
1327 astnode *astnode_create_union_decl(astnode *id, astnode *stmts, location loc)
1329 /* Create the node */
1330 astnode *n = astnode_create(UNION_DECL_NODE, loc);
1331 /* Two children: Identifier, statement list */
1332 astnode_add_child(n, id);
1333 astnode_add_child(n, stmts);
1334 /* Return the newly created node */
1335 return n;
1339 * Creates an enum declaration node.
1340 * @param id Enum identifier
1341 * @param stmts Statements of the enum declaration
1342 * @param loc File location
1344 astnode *astnode_create_enum_decl(astnode *id, astnode *stmts, location loc)
1346 /* Create the node */
1347 astnode *n = astnode_create(ENUM_DECL_NODE, loc);
1348 /* Two children: Identifier, statement list */
1349 astnode_add_child(n, id);
1350 astnode_add_child(n, stmts);
1351 /* Return the newly created node */
1352 return n;
1356 * Creates a record declaration node.
1357 * @param id Record identifier
1358 * @param fields Fields of the record
1359 * @param loc File location
1361 astnode *astnode_create_record_decl(astnode *id, astnode *fields, location loc)
1363 /* Create the node */
1364 astnode *n = astnode_create(RECORD_DECL_NODE, loc);
1365 /* Two children: Identifier, field list */
1366 astnode_add_child(n, id);
1367 astnode_add_child(n, fields);
1368 /* Return the newly created node */
1369 return n;
1373 * Creates a bitfield declaration node.
1374 * @param id Identifier
1375 * @param width Width of field
1376 * @param loc Location
1378 astnode *astnode_create_bitfield_decl(astnode *id, astnode *width, location loc)
1380 /* Create the node */
1381 astnode *n = astnode_create(BITFIELD_DECL_NODE, loc);
1382 /* Two children: Identifier and width */
1383 astnode_add_child(n, id);
1384 astnode_add_child(n, width);
1385 /* Return the newly created node */
1386 return n;
1390 * Creates a public node.
1392 astnode *astnode_create_public(astnode *l, location loc)
1394 /* Create the node */
1395 astnode *n = astnode_create(PUBLIC_NODE, loc);
1396 /* Add list of identifiers as child */
1397 astnode_add_child(n, l);
1398 /* Return the newly created node */
1399 return n;
1403 * Creates an extrn node.
1404 * @param l List of identifiers
1405 * @param t Symbol type specifier
1406 * @param f From unit (identifier, may be <code>NULL</code>)
1408 astnode *astnode_create_extrn(astnode *l, astnode *t, astnode *f, location loc)
1410 /* Create the node */
1411 astnode *n = astnode_create(EXTRN_NODE, loc);
1412 /* Add type specifier as child */
1413 astnode_add_child(n, t);
1414 /* Add list of identifiers as child */
1415 astnode_add_child(n, astnode_create_list(l));
1416 /* Add from unit identifier */
1417 astnode_add_child(n, f);
1418 /* Return the newly created node */
1419 return n;
1423 * Creates a dataseg node.
1425 astnode *astnode_create_dataseg(int modifiers, location loc)
1427 /* Create the node */
1428 astnode *n = astnode_create(DATASEG_NODE, loc);
1429 /* Set modifiers */
1430 n->modifiers = modifiers;
1431 /* Return the newly created node */
1432 return n;
1436 * Creates a codeseg node.
1438 astnode *astnode_create_codeseg(location loc)
1440 /* Create the node */
1441 astnode *n = astnode_create(CODESEG_NODE, loc);
1442 /* Return the newly created node */
1443 return n;
1447 * Creates a data node.
1448 * @param type Type specifier
1449 * @param data List of values
1450 * @param loc File location
1452 astnode *astnode_create_data(astnode *type, astnode *data, location loc)
1454 /* Create the node */
1455 astnode *n = astnode_create(DATA_NODE, loc);
1456 /* Add the type of data (enumerated or identifier) */
1457 astnode_add_child(n, type);
1458 /* Add list of values */
1459 astnode_add_child(n, data);
1460 /* Return the newly created node */
1461 return n;
1465 * Creates a file path node.
1466 * This is similar to a string literal node, the only difference is semantics.
1467 * A file path node implies that the path can be relative to both current
1468 * directory and any of the directories in the search path.
1469 * @param path The path this node represents
1470 * @param loc File location
1472 astnode *astnode_create_file_path(const char *path, location loc)
1474 /* Create the node */
1475 astnode *n = astnode_create(FILE_PATH_NODE, loc);
1476 /* Allocate and store text */
1477 n->file_path = (char *)malloc(strlen(path)+1);
1478 if (n->file_path != NULL) {
1479 strcpy(n->file_path, path);
1481 /* Return the newly created node */
1482 return n;
1486 * Creates a (global) label node.
1487 * @param s Name of label
1488 * @param addr Address
1489 * @param type Datatype (may be <code>NULL</code>)
1490 * @param loc Location
1492 astnode *astnode_create_label(const char *s, astnode *addr, astnode *type, location loc)
1494 /* Create the node */
1495 astnode *n = astnode_create(LABEL_NODE, loc);
1496 /* Allocate and store text */
1497 n->label = (char *)malloc(strlen(s)+1);
1498 if (n->label != NULL) {
1499 strcpy(n->label, s);
1501 /* Two children: Datatype and address */
1502 if (addr == NULL) {
1503 addr = astnode_create_pc(loc);
1505 if (type == NULL) {
1506 type = astnode_create_datatype(BYTE_DATATYPE, NULL, loc);
1508 astnode_add_child(n, type);
1509 astnode_add_child(n, addr);
1510 /* Return the newly created node */
1511 return n;
1515 * Creates a local label node.
1516 * @param s Name of label
1517 * @param loc Location
1519 astnode *astnode_create_local_label(const char *s, location loc)
1521 /* Create the node */
1522 astnode *n = astnode_create(LOCAL_LABEL_NODE, loc);
1523 /* Allocate and store text */
1524 n->label = (char *)malloc(strlen(s)+1);
1525 if (n->label != NULL) {
1526 strcpy(n->label, s);
1528 /* Return the newly created node */
1529 return n;
1533 * Creates a local identifier node.
1534 * @param s Identifier
1535 * @param loc Location
1537 astnode *astnode_create_local_id(const char *s, location loc)
1539 /* Create the node */
1540 astnode *n = astnode_create(LOCAL_ID_NODE, loc);
1541 /* Allocate and store text */
1542 n->ident = (char *)malloc(strlen(s)+1);
1543 if (n->ident != NULL) {
1544 strcpy(n->ident, s);
1546 /* Return the newly created node */
1547 return n;
1551 * Creates a list node.
1552 * This is a way to group a list of nodes in a parent node.
1553 * @param l List of nodes to group in list node
1555 astnode *astnode_create_list(astnode *l)
1557 astnode *n;
1558 /* Create the node */
1559 if (l != NULL) {
1560 n = astnode_create(LIST_NODE, l->loc);
1561 /* Add list of values */
1562 astnode_add_child(n, l);
1564 else {
1565 /* Make a node with zero children */
1566 location dummyloc;
1567 dummyloc.file = 0;
1568 n = astnode_create(LIST_NODE, dummyloc);
1570 /* Return the newly created node (or NULL) */
1571 return n;
1575 * Creates a PC node.
1576 * @param loc File location
1578 astnode *astnode_create_pc(location loc)
1580 return astnode_create(CURRENT_PC_NODE, loc);
1584 * Creates a binary node.
1585 * @param bin Dynamically allocated (malloc() ) data that this node wraps. Will be freed automatically by astnode_finalize()
1586 * @param size Size of bin
1587 * @param loc File location
1589 astnode *astnode_create_binary(unsigned char *bin, int size, location loc)
1591 /* Create the node */
1592 astnode *n = astnode_create(BINARY_NODE, loc);
1593 /* Set data */
1594 n->binary.data = bin;
1595 n->binary.size = size;
1596 /* Return the newly created node */
1597 return n;
1601 * Creates a tombstone node, which is a marker node that says that another node
1602 * once lived here.
1603 * @param type The type of node that used to live here
1604 * @param loc File location
1606 astnode *astnode_create_tombstone(astnode_type type, location loc)
1608 /* Create the node */
1609 astnode *n = astnode_create(TOMBSTONE_NODE, loc);
1610 /* Store the type of the old node */
1611 n->param = (long)type;
1612 /* Return the newly created node */
1613 return n;
1617 * Creates a dot operator node.
1618 * Represents a structure field access of the form 'before.after'.
1619 * @param before Structure identifier
1620 * @param after Field identifier (can be another dot op, or an identifier)
1622 astnode *astnode_create_dot(astnode *before, astnode *after, location loc)
1624 /* Create the node */
1625 astnode *n = astnode_create(DOT_NODE, loc);
1626 /* Two children: 'before' . 'after' */
1627 astnode_add_child(n, before);
1628 astnode_add_child(n, after);
1629 /* Return the newly created node */
1630 return n;
1634 * Creates a sizeof operator node.
1635 * @param expr Expression (datatype?)
1636 * @param loc Location
1638 astnode *astnode_create_sizeof(astnode *expr, location loc)
1640 /* Create the node */
1641 astnode *n = astnode_create(SIZEOF_NODE, loc);
1642 /* One child: expression */
1643 astnode_add_child(n, expr);
1644 /* Return the newly created node */
1645 return n;
1649 * Creates a datatype node.
1650 * @param t The datatype this node represents
1651 * @param id If the datatype is a custom one, this is its name
1652 * @param loc Location
1654 astnode *astnode_create_datatype(datatype t, astnode *id, location loc)
1656 /* Create the node */
1657 astnode *n = astnode_create(DATATYPE_NODE, loc);
1658 /* Set the datatype */
1659 n->datatype = t;
1660 /* Possibly one child: identifier */
1661 if (id != NULL) {
1662 astnode_add_child(n, id);
1664 /* Return the newly created node */
1665 return n;
1669 * Creates a variable declaration node.
1670 * @param modifiers PUBLIC_FLAG | ZEROPAGE_FLAG
1671 * @param id Identifier
1672 * @param data Datatype+initializer
1673 * @param loc Location
1675 astnode *astnode_create_var_decl(int modifiers, astnode *id, astnode *data, location loc)
1677 /* Create the node */
1678 astnode *n = astnode_create(VAR_DECL_NODE, loc);
1679 /* Set modifiers */
1680 n->modifiers = modifiers;
1681 /* Two children: Identifier and datatype+initializer */
1682 astnode_add_child(n, id);
1683 astnode_add_child(n, data);
1684 /* Return the newly created node */
1685 return n;
1691 astnode *astnode_create_scope(astnode *left, astnode *right, location loc)
1693 /* Create the node */
1694 astnode *n = astnode_create(SCOPE_NODE, loc);
1695 /* Two children: left and right */
1696 astnode_add_child(n, left);
1697 astnode_add_child(n, right);
1698 /* Return the newly created node */
1699 return n;
1703 * Creates a procedure (PROC) node.
1704 * @param ident Name of procedure
1705 * @param stmts Procedure statements
1706 * @param loc File location
1708 astnode *astnode_create_proc(astnode *ident, astnode *stmts, location loc)
1710 /* Create the node */
1711 astnode *n = astnode_create(PROC_NODE, loc);
1712 /* This node has two children:
1713 1) An identifier, which is the name of the procedure
1714 2) List of statements, which is the procedure body */
1715 astnode_add_children(
1718 ident,
1719 astnode_create_list(stmts)
1721 /* Return the newly created node */
1722 return n;
1726 * Creates a REPT node.
1727 * @param expr Number of times to repeat statements
1728 * @param stmts Statement list
1729 * @param loc File location
1731 astnode *astnode_create_rept(astnode *expr, astnode *stmts, location loc)
1733 /* Create the node */
1734 astnode *n = astnode_create(REPT_NODE, loc);
1735 /* This node has two children:
1736 1) An expression, which is the repeat count
1737 2) List of statements, which is the (anonymous) macro body */
1738 astnode_add_children(
1741 expr,
1742 astnode_create_list(stmts)
1744 /* Return the newly created node */
1745 return n;
1749 * Creates a WHILE node.
1750 * @param expr Boolean expression
1751 * @param stmts Statement list
1752 * @param loc File location
1754 astnode *astnode_create_while(astnode *expr, astnode *stmts, location loc)
1756 /* Create the node */
1757 astnode *n = astnode_create(WHILE_NODE, loc);
1758 /* This node has two children:
1759 1) A boolean expression
1760 2) List of statements, which is the (anonymous) macro body */
1761 astnode_add_children(
1764 expr,
1765 astnode_create_list(stmts)
1767 /* Return the newly created node */
1768 return n;
1772 * Creates a MESSAGE node.
1773 * @param expr Message to print.
1774 * @param loc File location
1776 astnode *astnode_create_message(astnode *expr, location loc)
1778 /* Create the node */
1779 astnode *n = astnode_create(MESSAGE_NODE, loc);
1780 /* This node has one children: An expression, which is the message to print */
1781 astnode_add_child(n, expr);
1782 /* Return the newly created node */
1783 return n;
1787 * Creates a WARNING node.
1788 * @param str Warning to print.
1789 * @param loc File location
1791 astnode *astnode_create_warning(astnode *str, location loc)
1793 /* Create the node */
1794 astnode *n = astnode_create(WARNING_NODE, loc);
1795 /* This node has one child: A string, which is the warning to print */
1796 astnode_add_child(n, str);
1797 /* Return the newly created node */
1798 return n;
1802 * Creates an ERROR node.
1803 * @param str Error to print.
1804 * @param loc File location
1806 astnode *astnode_create_error(astnode *str, location loc)
1808 /* Create the node */
1809 astnode *n = astnode_create(ERROR_NODE, loc);
1810 /* This node has one child: A string, which is the error to print */
1811 astnode_add_child(n, str);
1812 /* Return the newly created node */
1813 return n;
1817 * Creates a forward branch declaration node.
1818 * @param ident Branch name
1819 * @param loc File location
1821 astnode *astnode_create_forward_branch_decl(const char *ident, location loc)
1823 /* Create the node */
1824 astnode *n = astnode_create(FORWARD_BRANCH_DECL_NODE, loc);
1825 /* Allocate and store text */
1826 n->ident = (char *)malloc(strlen(ident)+1);
1827 if (n->ident != NULL) {
1828 strcpy(n->ident, ident);
1830 /* Return the newly created node */
1831 return n;
1835 * Creates a backward branch declaration node.
1836 * @param ident Branch name
1837 * @param loc File location
1839 astnode *astnode_create_backward_branch_decl(const char *ident, location loc)
1841 /* Create the node */
1842 astnode *n = astnode_create(BACKWARD_BRANCH_DECL_NODE, loc);
1843 /* Allocate and store text */
1844 n->ident = (char *)malloc(strlen(ident)+1);
1845 if (n->ident != NULL) {
1846 strcpy(n->ident, ident);
1848 /* Return the newly created node */
1849 return n;
1853 * Creates a forward branch reference node.
1854 * @param ident Branch name
1855 * @param loc File location
1857 astnode *astnode_create_forward_branch(const char *ident, location loc)
1859 /* Create the node */
1860 astnode *n = astnode_create(FORWARD_BRANCH_NODE, loc);
1861 /* Allocate and store text */
1862 n->ident = (char *)malloc(strlen(ident)+1);
1863 if (n->ident != NULL) {
1864 strcpy(n->ident, ident);
1866 /* Return the newly created node */
1867 return n;
1871 * Creates a backward branch reference node.
1872 * @param ident Branch name
1873 * @param loc File location
1875 astnode *astnode_create_backward_branch(const char *ident, location loc)
1877 /* Create the node */
1878 astnode *n = astnode_create(BACKWARD_BRANCH_NODE, loc);
1879 /* Allocate and store text */
1880 n->ident = (char *)malloc(strlen(ident)+1);
1881 if (n->ident != NULL) {
1882 strcpy(n->ident, ident);
1884 /* Return the newly created node */
1885 return n;
1889 * Creates a mask operator node.
1890 * @param expr Expression
1891 * @param loc Location
1893 astnode *astnode_create_mask(astnode *expr, location loc)
1895 /* Create the node */
1896 astnode *n = astnode_create(MASK_NODE, loc);
1897 /* One child: expression */
1898 astnode_add_child(n, expr);
1899 /* Return the newly created node */
1900 return n;
1904 * Creates an ALIGN node.
1905 * @param idents List of identifiers
1906 * @param expr Expression
1907 * @param loc File location
1909 astnode *astnode_create_align(astnode *idents, astnode *expr, location loc)
1911 /* Create the node */
1912 astnode *n = astnode_create(ALIGN_NODE, loc);
1913 /* This node has two children: List of identifiers and alignment constraint */
1914 astnode_add_child(n, astnode_create_list(idents) );
1915 astnode_add_child(n, expr);
1916 /* Return the newly created node */
1917 return n;
1921 * Creates an INDEX node.
1922 * @param ident Identifier being indexed
1923 * @param expr Index expression
1924 * @param loc File location
1926 astnode *astnode_create_index(astnode *ident, astnode *expr, location loc)
1928 /* Create the node */
1929 astnode *n = astnode_create(INDEX_NODE, loc);
1930 /* This node has two children: Identifier and expression */
1931 astnode_add_child(n, ident);
1932 astnode_add_child(n, expr);
1933 /* Return the newly created node */
1934 return n;
1938 * Creates an ORG node.
1939 * @param addr Address
1940 * @param loc File location
1942 astnode *astnode_create_org(astnode *addr, location loc)
1944 /* Create the node */
1945 astnode *n = astnode_create(ORG_NODE, loc);
1946 /* This node has one child: The address */
1947 astnode_add_child(n, addr);
1948 /* Return the newly created node */
1949 return n;