9 date 2007.08.12.18.58.12; author khansen; state Exp;
14 date 2007.08.10.20.21.02; author khansen; state Exp;
19 date 2007.08.09.22.05.49; author khansen; state Exp;
24 date 2007.08.07.21.12.16; author khansen; state Exp;
29 date 2007.07.22.13.33.26; author khansen; state Exp;
34 date 2004.12.29.21.44.04; author kenth; state Exp;
39 date 2004.12.19.19.58.23; author kenth; state Exp;
44 date 2004.12.19.09.53.46; author kenth; state Exp;
49 date 2004.12.18.16.56.12; author kenth; state Exp;
54 date 2004.12.16.13.19.07; author kenth; state Exp;
59 date 2004.12.14.01.48.57; author kenth; state Exp;
64 date 2004.12.11.02.01.10; author kenth; state Exp;
69 date 2004.12.09.11.17.59; author kenth; state Exp;
74 date 2004.12.06.04.52.05; author kenth; state Exp;
79 date 2004.06.30.07.55.28; author kenth; state Exp;
90 @ability to generate pure 6502 binary (--pure-binary switch)
94 * $Id: astnode.c,v 1.14 2007/08/10 20:21:02 khansen Exp khansen $
96 * Revision 1.14 2007/08/10 20:21:02 khansen
97 * *** empty log message ***
99 * Revision 1.13 2007/08/09 22:05:49 khansen
100 * general-purpose flags
102 * Revision 1.12 2007/08/07 21:12:16 khansen
105 * Revision 1.11 2007/07/22 13:33:26 khansen
106 * convert tabs to whitespaces
108 * Revision 1.10 2004/12/29 21:44:04 kenth
110 * added create_index()
112 * Revision 1.9 2004/12/19 19:58:23 kenth
115 * Revision 1.8 2004/12/19 09:53:46 kenth
116 * added create_align()
118 * Revision 1.7 2004/12/18 16:56:12 kenth
119 * create_extrn() takes unit id
121 * Revision 1.6 2004/12/16 13:19:07 kenth
122 * astnode_create_label() takes datatype argument
124 * Revision 1.5 2004/12/14 01:48:57 kenth
127 * Revision 1.4 2004/12/11 02:01:10 kenth
128 * added forward/backward branching
130 * Revision 1.3 2004/12/09 11:17:59 kenth
131 * added: warning, error nodes
133 * Revision 1.2 2004/12/06 04:52:05 kenth
134 * Major updates (xorcyst 1.1.0)
136 * Revision 1.1 2004/06/30 07:55:28 kenth
142 * (C) 2004 Kent Hansen
144 * The XORcyst is free software; you can redistribute it and/or modify
145 * it under the terms of the GNU General Public License as published by
146 * the Free Software Foundation; either version 2 of the License, or
147 * (at your option) any later version.
149 * The XORcyst is distributed in the hope that it will be useful,
150 * but WITHOUT ANY WARRANTY; without even the implied warranty of
151 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
152 * GNU General Public License for more details.
154 * You should have received a copy of the GNU General Public License
155 * along with The XORcyst; if not, write to the Free Software
156 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
160 * The result of parsing an assembly file is an Abstract Syntax Tree (AST).
161 * Such a tree consists of AST nodes.
162 * This file contains the code to manipulate such nodes.
171 #define SAFE_FREE(a) if (a) { free(a); a = NULL; }
173 /*---------------------------------------------------------------------------*/
174 /* Functions to convert and print astnodes as string.
175 These are useful when debugging syntax trees.
179 * Gets string representation of an addressing mode.
180 * @@param am Addressing mode (enumerated type)
181 * @@return String representation of am
183 const char *addressing_mode_to_string(addressing_mode am)
186 case IMPLIED_MODE: return "IMPLIED_MODE";
187 case ACCUMULATOR_MODE: return "ACCUMULATOR_MODE";
188 case IMMEDIATE_MODE: return "IMMEDIATE_MODE";
189 case ZEROPAGE_MODE: return "ZEROPAGE_MODE";
190 case ZEROPAGE_X_MODE: return "ZEROPAGE_X_MODE";
191 case ZEROPAGE_Y_MODE: return "ZEROPAGE_Y_MODE";
192 case ABSOLUTE_MODE: return "ABSOLUTE_MODE";
193 case ABSOLUTE_X_MODE: return "ABSOLUTE_X_MODE";
194 case ABSOLUTE_Y_MODE: return "ABSOLUTE_Y_MODE";
195 case PREINDEXED_INDIRECT_MODE: return "PREINDEXED_INDIRECT_MODE";
196 case POSTINDEXED_INDIRECT_MODE: return "POSTINDEXED_INDIRECT_MODE";
197 case INDIRECT_MODE: return "INDIRECT_MODE";
198 case RELATIVE_MODE: return "RELATIVE_MODE";
200 return "addressing_mode_to_string: invalid addressing mode";
204 * Gets string representation of an instruction mnemonic.
205 * @@param im Instruction mnemonic (enumerated type)
206 * @@return String representation of im
208 const char *instr_mnemonic_to_string(instr_mnemonic im)
211 case ADC_MNEMONIC: return "ADC_MNEMONIC";
212 case AND_MNEMONIC: return "AND_MNEMONIC";
213 case ASL_MNEMONIC: return "ASL_MNEMONIC";
214 case BCC_MNEMONIC: return "BCC_MNEMONIC";
215 case BCS_MNEMONIC: return "BCS_MNEMONIC";
216 case BEQ_MNEMONIC: return "BEQ_MNEMONIC";
217 case BIT_MNEMONIC: return "BIT_MNEMONIC";
218 case BMI_MNEMONIC: return "BMI_MNEMONIC";
219 case BNE_MNEMONIC: return "BNE_MNEMONIC";
220 case BPL_MNEMONIC: return "BPL_MNEMONIC";
221 case BRK_MNEMONIC: return "BRK_MNEMONIC";
222 case BVC_MNEMONIC: return "BVC_MNEMONIC";
223 case BVS_MNEMONIC: return "BVS_MNEMONIC";
224 case CLC_MNEMONIC: return "CLC_MNEMONIC";
225 case CLD_MNEMONIC: return "CLD_MNEMONIC";
226 case CLI_MNEMONIC: return "CLI_MNEMONIC";
227 case CLV_MNEMONIC: return "CLV_MNEMONIC";
228 case CMP_MNEMONIC: return "CMP_MNEMONIC";
229 case CPX_MNEMONIC: return "CPX_MNEMONIC";
230 case CPY_MNEMONIC: return "CPY_MNEMONIC";
231 case DEC_MNEMONIC: return "DEC_MNEMONIC";
232 case DEX_MNEMONIC: return "DEX_MNEMONIC";
233 case DEY_MNEMONIC: return "DEY_MNEMONIC";
234 case EOR_MNEMONIC: return "EOR_MNEMONIC";
235 case INC_MNEMONIC: return "INC_MNEMONIC";
236 case INX_MNEMONIC: return "INX_MNEMONIC";
237 case INY_MNEMONIC: return "INY_MNEMONIC";
238 case JMP_MNEMONIC: return "JMP_MNEMONIC";
239 case JSR_MNEMONIC: return "JSR_MNEMONIC";
240 case LDA_MNEMONIC: return "LDA_MNEMONIC";
241 case LDX_MNEMONIC: return "LDX_MNEMONIC";
242 case LDY_MNEMONIC: return "LDY_MNEMONIC";
243 case LSR_MNEMONIC: return "LSR_MNEMONIC";
244 case NOP_MNEMONIC: return "NOP_MNEMONIC";
245 case ORA_MNEMONIC: return "ORA_MNEMONIC";
246 case PHA_MNEMONIC: return "PHA_MNEMONIC";
247 case PHP_MNEMONIC: return "PHP_MNEMONIC";
248 case PLA_MNEMONIC: return "PLA_MNEMONIC";
249 case PLP_MNEMONIC: return "PLP_MNEMONIC";
250 case ROL_MNEMONIC: return "ROL_MNEMONIC";
251 case ROR_MNEMONIC: return "ROR_MNEMONIC";
252 case RTI_MNEMONIC: return "RTI_MNEMONIC";
253 case RTS_MNEMONIC: return "RTS_MNEMONIC";
254 case SBC_MNEMONIC: return "SBC_MNEMONIC";
255 case SEC_MNEMONIC: return "SEC_MNEMONIC";
256 case SED_MNEMONIC: return "SED_MNEMONIC";
257 case SEI_MNEMONIC: return "SEI_MNEMONIC";
258 case STA_MNEMONIC: return "STA_MNEMONIC";
259 case STX_MNEMONIC: return "STX_MNEMONIC";
260 case STY_MNEMONIC: return "STY_MNEMONIC";
261 case TAX_MNEMONIC: return "TAX_MNEMONIC";
262 case TAY_MNEMONIC: return "TAY_MNEMONIC";
263 case TSX_MNEMONIC: return "TSX_MNEMONIC";
264 case TXA_MNEMONIC: return "TXA_MNEMONIC";
265 case TXS_MNEMONIC: return "TXS_MNEMONIC";
266 case TYA_MNEMONIC: return "TYA_MNEMONIC";
268 return "instr_mnemonic_to_string: invalid mnemonic";
272 * Gets string representation of an astnode type.
273 * @@param at Node type
274 * @@return String representation of at
276 const char *astnode_type_to_string(astnode_type at) {
278 case NULL_NODE: return "NULL_NODE";
279 case INTEGER_NODE: return "INTEGER_NODE";
280 case STRING_NODE: return "STRING_NODE";
281 case IDENTIFIER_NODE: return "IDENTIFIER_NODE";
282 case DATA_NODE: return "DATA_NODE";
283 case STORAGE_NODE: return "STORAGE_NODE";
284 case MACRO_DECL_NODE: return "MACRO_DECL_NODE";
285 case MACRO_NODE: return "MACRO_NODE";
286 case ARITHMETIC_NODE: return "ARITHMETIC_NODE";
287 case IF_NODE: return "IF_NODE";
288 case CASE_NODE: return "CASE_NODE";
289 case DEFAULT_NODE: return "DEFAULT_NODE";
290 case IFDEF_NODE: return "IFDEF_NODE";
291 case IFNDEF_NODE: return "IFNDEF_NODE";
292 case INCSRC_NODE: return "INCSRC_NODE";
293 case INCBIN_NODE: return "INCBIN_NODE";
294 case EQU_NODE: return "EQU_NODE";
295 case ASSIGN_NODE: return "ASSIGN_NODE";
296 case ALIGN_NODE: return "ALIGN_NODE";
297 case INSTRUCTION_NODE: return "INSTRUCTION_NODE";
298 case FILE_PATH_NODE: return "FILE_PATH_NODE";
299 case CURRENT_PC_NODE: return "CURRENT_PC_NODE";
300 case LIST_NODE: return "LIST_NODE";
301 case LABEL_NODE: return "LABEL_NODE";
302 case LOCAL_LABEL_NODE: return "LOCAL_LABEL_NODE";
303 case LOCAL_ID_NODE: return "LOCAL_ID_NODE";
304 case BINARY_NODE: return "BINARY_NODE";
305 case PUBLIC_NODE: return "PUBLIC_NODE";
306 case EXTRN_NODE: return "EXTRN_NODE";
307 case DATASEG_NODE: return "DATASEG_NODE";
308 case CODESEG_NODE: return "CODESEG_NODE";
309 case CHARMAP_NODE: return "CHARMAP_NODE";
310 case STRUC_NODE: return "STRUC_NODE";
311 case STRUC_DECL_NODE: return "STRUC_DECL_NODE";
312 case UNION_DECL_NODE: return "UNION_DECL_NODE";
313 case ENUM_DECL_NODE: return "ENUM_DECL_NODE";
314 case RECORD_DECL_NODE: return "RECORD_DECL_NODE";
315 case BITFIELD_DECL_NODE:return "BITFIELD_DECL_NODE";
316 case DOT_NODE: return "DOT_NODE";
317 case SIZEOF_NODE: return "SIZEOF_NODE";
318 case DATATYPE_NODE: return "DATATYPE_NODE";
319 case VAR_DECL_NODE: return "VAR_DECL_NODE";
320 case SCOPE_NODE: return "SCOPE_NODE";
321 case PROC_NODE: return "PROC_NODE";
322 case REPT_NODE: return "REPT_NODE";
323 case WHILE_NODE: return "WHILE_NODE";
324 case MESSAGE_NODE: return "MESSAGE_NODE";
325 case WARNING_NODE: return "WARNING_NODE";
326 case ERROR_NODE: return "ERROR_NODE";
327 case FORWARD_BRANCH_DECL_NODE: return "FORWARD_BRANCH_DECL_NODE";
328 case BACKWARD_BRANCH_DECL_NODE: return "BACKWARD_BRANCH_DECL_NODE";
329 case FORWARD_BRANCH_NODE: return "FORWARD_BRANCH_NODE";
330 case BACKWARD_BRANCH_NODE: return "BACKWARD_BRANCH_NODE";
331 case MASK_NODE: return "MASK_NODE";
332 case INDEX_NODE: return "INDEX_NODE";
333 case ORG_NODE: return "ORG_NODE";
334 case TOMBSTONE_NODE: return "TOMBSTONE_NODE";
336 return "astnode_type_to_string: invalid type";
340 * Gets string representation of a datatype.
341 * @@param dt Datatype
342 * @@return String representation of dt
344 const char *datatype_to_string(const astnode *dt)
346 switch (dt->datatype) {
347 case BYTE_DATATYPE: return "BYTE_DATATYPE";
348 case CHAR_DATATYPE: return "CHAR_DATATYPE";
349 case WORD_DATATYPE: return "WORD_DATATYPE";
350 case DWORD_DATATYPE: return "DWORD_DATATYPE";
351 case USER_DATATYPE: return "USER_DATATYPE"; // astnode_get_child(dt, 0)->ident;
353 return "datatype_to_string: invalid datatype";
357 * Gets string representation of an operator.
358 * @@param op Operator
359 * @@return String representation of op
361 const char *operator_to_string(int op)
364 case PLUS_OPERATOR: return "PLUS_OPERATOR";
365 case MINUS_OPERATOR: return "MINUS_OPERATOR";
366 case MUL_OPERATOR: return "MUL_OPERATOR";
367 case DIV_OPERATOR: return "DIV_OPERATOR";
368 case MOD_OPERATOR: return "MOD_OPERATOR";
369 case AND_OPERATOR: return "AND_OPERATOR";
370 case OR_OPERATOR: return "OR_OPERATOR";
371 case XOR_OPERATOR: return "XOR_OPERATOR";
372 case SHL_OPERATOR: return "SHL_OPERATOR";
373 case SHR_OPERATOR: return "SHR_OPERATOR";
374 case LT_OPERATOR: return "LT_OPERATOR";
375 case GT_OPERATOR: return "GT_OPERATOR";
376 case EQ_OPERATOR: return "EQ_OPERATOR";
377 case NE_OPERATOR: return "NE_OPERATOR";
378 case LE_OPERATOR: return "LE_OPERATOR";
379 case GE_OPERATOR: return "GE_OPERATOR";
380 case NEG_OPERATOR: return "NEG_OPERATOR";
381 case NOT_OPERATOR: return "NOT_OPERATOR";
382 case LO_OPERATOR: return "LO_OPERATOR";
383 case HI_OPERATOR: return "HI_OPERATOR";
384 case UMINUS_OPERATOR: return "UMINUS_OPERATOR";
385 case BANK_OPERATOR: return "BANK_OPERATOR";
387 return "operator_to_string: invalid operator";
392 * @@param nlevels Levels
394 void indent(int nlevels)
397 for (i=0; i<nlevels; i++) {
403 * Prints a node recursively.
404 * @@param n Node to print
405 * @@param level Level (depth)
407 void astnode_print(const astnode *n, int level)
410 /* Indent so it looks pretty */
412 /* Print the node type */
413 printf(astnode_type_to_string(astnode_get_type(n)));
414 /* Print attributes for those that have */
415 switch (astnode_get_type(n)) {
416 case INTEGER_NODE: printf("(%d)", n->integer); break;
417 case STRING_NODE: printf("(\"%s\")", n->string); break;
418 case IDENTIFIER_NODE: printf("(%s)", n->ident); break;
419 case LOCAL_ID_NODE: printf("(%s)", n->ident); break;
420 case FILE_PATH_NODE: printf("(%s)", n->file_path); break;
421 case LABEL_NODE: printf("(%s)", n->label); break;
422 case LOCAL_LABEL_NODE: printf("(%s)", n->label); break;
423 case BINARY_NODE: printf("(%d)", n->binary.size); break;
425 case ARITHMETIC_NODE:
428 operator_to_string(n->oper)
432 case INSTRUCTION_NODE:
435 instr_mnemonic_to_string(n->instr.mnemonic),
436 addressing_mode_to_string(n->instr.mode),
444 datatype_to_string(n)
448 case FORWARD_BRANCH_DECL_NODE:
449 case BACKWARD_BRANCH_DECL_NODE:
450 case FORWARD_BRANCH_NODE:
451 case BACKWARD_BRANCH_NODE:
452 printf("(%s)", n->ident);
458 astnode_type_to_string(n->param)
463 /* Has no internal attributes */
467 /* Print the children */
468 for (i=0; i<astnode_get_child_count(n); i++) {
469 astnode_print(astnode_get_child(n, i), level+1);
473 /*---------------------------------------------------------------------------*/
474 /* Functions for general-purpose node management:
475 Creation, destruction, children etc.
479 * Creates a new node of the given type.
480 * @@param type The node's type
481 * @@param loc File location
482 * @@return The newly created node
484 astnode *astnode_create(astnode_type type, location loc)
486 /* Fix: Sometimes loc.file is NULL for some reason */
487 extern const char *yy_current_filename();
488 if (loc.file == NULL) {
489 loc.file = yy_current_filename();
491 /* Allocate memory for node struct */
492 astnode *n = (astnode *)malloc(sizeof(astnode));
493 /* Fill in struct only if alloc succeeded */
500 n->parent = n->first_child = n->prev_sibling = n->next_sibling = NULL;
507 * Any children of the node are also finalized, recursively.
508 * @@param n The node to finalize.
510 void astnode_finalize(astnode *n)
512 /* Remove the node from the tree it's in. */
514 /* Finalize all its children recursively. */
515 while (astnode_get_first_child(n) != NULL) {
516 astnode_finalize(astnode_remove_child_at(n, 0));
518 /* Free up memory. */
519 switch (astnode_get_type(n)) {
520 case LABEL_NODE: SAFE_FREE(n->label); break;
521 case LOCAL_LABEL_NODE: SAFE_FREE(n->label); break;
522 case STRING_NODE: SAFE_FREE(n->string); break;
523 case IDENTIFIER_NODE: SAFE_FREE(n->ident); break;
524 case LOCAL_ID_NODE: SAFE_FREE(n->ident); break;
525 case FILE_PATH_NODE: SAFE_FREE(n->file_path);break;
526 case BINARY_NODE: SAFE_FREE(n->binary.data); break;
527 case FORWARD_BRANCH_DECL_NODE:
528 case BACKWARD_BRANCH_DECL_NODE:
529 case FORWARD_BRANCH_NODE:
530 case BACKWARD_BRANCH_NODE:
534 /* Has no internal attributes that are dynamically allocated */
541 * Gets the node's type.
542 * @@param n The node whose type to get
543 * @@return The node's type (astnode_type)
545 astnode_type astnode_get_type(const astnode *n)
547 return (n != NULL) ? n->type : NULL_NODE;
551 * Sets the parent field of all nodes in c to p.
553 void astnode_set_parent(astnode *c, astnode *p)
556 for (n = c; n != NULL; n = n->next_sibling) {
562 * Replaces a node with another.
564 void astnode_replace(astnode *old_node, astnode *new_node)
568 /* Get the parent of the node to be replaced */
569 p = astnode_get_parent(old_node);
571 /* Call remove_child on parent */
572 i = astnode_remove_child(p, old_node);
573 /* Insert new child at old child's position */
574 astnode_insert_child(p, new_node, i);
579 * Removes a node from a tree.
580 * @@param n The node to remove (can't be the root of the tree)
582 void astnode_remove(astnode *n)
584 astnode *p = astnode_get_parent(n);
586 astnode_remove_child(p, n);
591 * Removes a child node.
592 * @@param p Parent node
593 * @@param c Child node
594 * @@return Index of the removed node
596 int astnode_remove_child(astnode *p, astnode *c)
599 i = astnode_get_child_index(p, c);
601 /* Remove head of list. */
602 p->first_child = c->next_sibling;
603 if (p->first_child) {
604 p->first_child->prev_sibling = NULL;
606 c->parent = c->next_sibling = c->prev_sibling = NULL;
609 c->prev_sibling->next_sibling = c->next_sibling;
610 if (c->next_sibling) {
611 c->next_sibling->prev_sibling = c->prev_sibling;
613 c->parent = c->next_sibling = c->prev_sibling = NULL;
619 * Removes child node at specified index.
620 * @@param p Parent node
621 * @@param i Index >= 0
623 astnode *astnode_remove_child_at(astnode *p, int i)
625 astnode *c = astnode_get_child(p, i);
626 astnode_remove_child(p, c);
631 * Removes all children from a node and returns them as a list.
632 * @@param p Parent node whose children to remove
634 astnode *astnode_remove_children(astnode *p)
637 if (p == NULL) { return NULL; }
638 if (p->first_child != NULL) {
640 p->first_child = NULL;
641 /* Set parent of all siblings to NULL. */
642 astnode_set_parent(c, NULL);
643 /* Return the list of children */
647 /* Has no children. */
653 * Inserts a list of nodes as children.
656 void astnode_insert_child(astnode *p, astnode *c, int i)
661 x = astnode_get_child(p, i); /* Current child at that position */
663 /* There isn't a node here. Just add to end. */
664 astnode_add_child(p, c);
667 n = astnode_get_last_sibling(c);
668 /* Make c..n precede x */
669 c->prev_sibling = x->prev_sibling;
670 if (x->prev_sibling) {
671 x->prev_sibling->next_sibling = c;
677 astnode_set_parent(c, p);
686 * Gets the last node in a list.
688 astnode *astnode_get_last_sibling(const astnode *n)
692 for (s = (astnode *)n; s->next_sibling != NULL; s = s->next_sibling) ;
698 * Gets the parent of a node.
699 * @@param n The node whose parent to get
700 * @@return The node's parent, or <code>NULL</code> if it has none
702 astnode *astnode_get_parent(const astnode *n)
704 return n ? n->parent : NULL;
708 * Adds child(ren) to a node.
709 * @@param n The parent-to-be
710 * @@param new_child List of children-to-be
712 void astnode_add_child(astnode *n, astnode *new_child)
714 if (n && new_child) {
715 if (n->first_child == NULL) {
716 /* This node has no children, add this as the first one */
717 n->first_child = new_child;
718 astnode_set_parent(new_child, n);
721 astnode_add_sibling(n->first_child, new_child);
727 * Adds any number of children to a node.
728 * @@param n The parent-to-be
730 void astnode_add_children(astnode *n, int count, ...)
737 for (i=0; i<count; i++) {
738 c = va_arg(ap, astnode*);
739 astnode_add_child(n, c);
745 * Adds sibling(s) to a node.
746 * @@param brother List of existing siblings
747 * @@param sister List of new siblings
749 void astnode_add_sibling(astnode *brother, astnode *sister)
753 if (brother && sister) {
754 /* Add to end of list */
755 n = astnode_get_last_sibling(brother);
756 n->next_sibling = sister;
757 sister->prev_sibling = n;
758 p = astnode_get_parent(brother);
759 astnode_set_parent(sister, p);
764 * Gets the child node at the specified index.
765 * @@param n The parent node
766 * @@param index The index of the desired child node
768 astnode *astnode_get_child(const astnode *n, int index)
774 for (i = 0; i != index; i++) {
776 /* No child at that index. */
783 /* Node is NULL, so return NULL */
788 * Gets a node's first child.
791 astnode *astnode_get_first_child(const astnode *n)
793 return (n == NULL) ? NULL : n->first_child;
797 * Gets the index of a child node.
798 * @@param p Parent node
799 * @@param c Child node
800 * @@return Index of c >= 0, or -1 if invalid input
802 int astnode_get_child_index(const astnode *p, const astnode *c)
807 for (i=0, n=p->first_child; (n != c) && (n != NULL); i++, n=n->next_sibling);
814 * Gets the number of children a node has.
815 * @@param p Node whose children count to get
817 int astnode_get_child_count(const astnode *p)
822 for (c = p->first_child; c != NULL; count++, c = c->next_sibling);
828 * Clones a node and all its children.
829 * @@param n The node to clone
830 * @@param loc File location
832 astnode *astnode_clone(const astnode *n, location loc)
836 if (n == NULL) { return NULL; }
838 c = astnode_create(astnode_get_type(n), loc);
839 /* Copy attributes */
840 switch (astnode_get_type(n)) {
842 c->integer = n->integer;
846 case IDENTIFIER_NODE:
849 case LOCAL_LABEL_NODE:
851 c->string = (char *)malloc(strlen(n->string)+1);
852 if (c->string != NULL) {
853 strcpy(c->string, n->string);
857 case ARITHMETIC_NODE:
861 case INSTRUCTION_NODE:
866 c->binary = n->binary;
870 c->datatype = n->datatype;
876 /* Clone children (TODO: OPTIMIZE THIS) */
877 for (n_c=n->first_child; n_c != NULL; n_c=n_c->next_sibling) {
878 astnode_add_child(c, astnode_clone(n_c, loc));
880 /* Return the clone */
885 * Tests if two nodes are equal.
887 int astnode_equal(const astnode *n1, const astnode *n2)
890 /* Verify that types are the same */
891 if (astnode_get_type(n1) != astnode_get_type(n2)) {
892 return 0; /* Types don't match -- not equal */
894 /* Verify that internal data is the same */
895 switch (astnode_get_type(n1)) {
896 case ARITHMETIC_NODE: if (n1->oper != n2->oper) return 0; break;
897 case INTEGER_NODE: if (n1->integer != n2->integer) return 0; break;
898 case STRING_NODE: if (strcmp(n1->string, n2->string)) return 0; break;
899 case IDENTIFIER_NODE: if (strcmp(n1->ident, n2->ident)) return 0; break;
900 case LOCAL_ID_NODE: if (strcmp(n1->ident, n2->ident)) return 0; break;
901 case FILE_PATH_NODE: if (strcmp(n1->file_path, n2->file_path)) return 0; break;
902 case LABEL_NODE: if (strcmp(n1->label, n2->label)) return 0; break;
903 case LOCAL_LABEL_NODE: if (strcmp(n1->label, n2->label)) return 0; break;
904 case BINARY_NODE: if (n1->binary.size != n2->binary.size) return 0; break;
905 case DATATYPE_NODE: if (n1->datatype != n2->datatype) return 0; break;
906 case TOMBSTONE_NODE: if (n1->param != n2->param) return 0; break;
907 case INSTRUCTION_NODE: if ( (n1->instr.mnemonic != n2->instr.mnemonic) || (n1->instr.mode != n2->instr.mode) ) return 0; break;
909 /* Has no internal attributes */
912 /* Verify that they have the same number of children */
913 if (astnode_get_child_count(n1) != astnode_get_child_count(n2)) {
916 /* Verify that children are equal */
917 for (i=0; i<astnode_get_child_count(n1); i++) {
918 if (!astnode_equal(astnode_get_child(n1, i), astnode_get_child(n2, i))) {
927 * Gets the ancestor of a node.
928 * @@param n Node whose ancestor to get
929 * @@param back How many generations to go back (0=father, 1=grandfather etc.)
931 astnode *astnode_get_ancestor(const astnode *n, int back)
934 astnode *a = astnode_get_parent(n);
935 for (i=0; i<back; i++) {
936 a = astnode_get_parent(a);
942 * Tests if a node is a descendant of a node of a particular type.
944 * @@param type Ancestor's type
945 * @@return 0 if no such ancestor, 1 otherwise
947 int astnode_has_ancestor_of_type(const astnode *n, astnode_type type)
950 for (a = astnode_get_parent(n); a != NULL; a = astnode_get_parent(a) ) {
951 if (astnode_is_type(a, type)) {
959 * Gets the next sibling of a node.
962 astnode *astnode_get_next_sibling(const astnode *n)
964 if (n == NULL) { return NULL; }
965 return n->next_sibling;
969 * Gets the previous sibling of a node.
972 astnode *astnode_get_prev_sibling(const astnode *n)
974 if (n == NULL) { return NULL; }
975 return n->prev_sibling;
979 * Tests if a node is a literal.
980 * @@param n Node to test
982 int astnode_is_literal(const astnode *n)
984 switch (astnode_get_type(n)) {
998 /*---------------------------------------------------------------------------*/
999 /* Functions for creating AST nodes of specific type.
1000 1:1 correspondence between astnode_create_* and *_INSTRUCTION.
1001 Each takes the operands required for that node type,
1002 calls astnode_create() and then fills in fields and adds children (if any).
1005 astnode *astnode_create_null(location loc)
1007 /* Create the node */
1008 astnode *n = astnode_create(NULL_NODE, loc);
1009 /* Return the newly created node */
1014 * Creates a CPU instruction node.
1015 * @@param mnemonic The instruction mnemonic
1016 * @@param mode The addressing mode used
1017 * @@param operand The instruction operand (an expression) (can be <code>NULL</code>)
1018 * @@param loc File location
1020 astnode *astnode_create_instruction(int mnemonic, addressing_mode mode, astnode *operand, location loc)
1022 /* Create the node */
1023 astnode *n = astnode_create(INSTRUCTION_NODE, loc);
1024 /* Store the mnemonic and addressing mode */
1025 n->instr.mnemonic = mnemonic;
1026 n->instr.mode = mode;
1027 /* This node has one child: The operand, which is an expression */
1028 astnode_add_child(n, operand);
1029 /* Return the newly created node */
1034 * Creates an identifier node.
1035 * @@param ident The identifier (a string)
1036 * @@param loc File location
1038 astnode *astnode_create_identifier(const char *ident, location loc)
1040 /* Create the node */
1041 astnode *n = astnode_create(IDENTIFIER_NODE, loc);
1042 /* Allocate and store text */
1043 n->ident = (char *)malloc(strlen(ident)+1);
1044 if (n->ident != NULL) {
1045 strcpy(n->ident, ident);
1047 /* Return the newly created node */
1052 * Creates an integer literal node.
1053 * @@param value The integer literal
1054 * @@param loc File location
1056 astnode *astnode_create_integer(int value, location loc)
1058 /* Create the node */
1059 astnode *n = astnode_create(INTEGER_NODE, loc);
1060 /* Store the integer which this node represents */
1062 /* Return the newly created node */
1067 * Creates a string literal node.
1068 * @@param value The string literal
1069 * @@param loc File location
1071 astnode *astnode_create_string(const char *value, location loc)
1073 /* Create the node */
1074 astnode *n = astnode_create(STRING_NODE, loc);
1075 /* Allocate and store text */
1076 n->string = (char *)malloc(strlen(value)+1);
1077 if (n->string != NULL) {
1078 strcpy(n->string, value);
1080 /* Return the newly created node */
1085 * Creates an expression node (unary or binary).
1086 * @@param oper The operator
1087 * @@param left Left operand
1088 * @@param right Right operand (can be <code>NULL</code>)
1089 * @@param loc File location
1091 astnode *astnode_create_arithmetic(arithmetic_operator oper, astnode *left, astnode *right, location loc)
1093 /* Create the node */
1094 astnode *n = astnode_create(ARITHMETIC_NODE, loc);
1095 /* Store the operator, which describes the type of expression */
1097 /* This node has two children: left-hand side and right-hand side expression */
1098 /* For unary operators right-hand side should be <code>NULL</code> */
1099 astnode_add_children(n, 2, left, right);
1100 /* Return the newly created node */
1105 * Creates an if node.
1106 * @@param expr The expression involved in the if
1107 * @@param then The statement(s) to assemble when expr is non-zero
1108 * @@param elif List of CASE nodes (may be <code>NULL</code>)
1109 * @@param els The final else-part (DEFAULT node) (may be <code>NULL</code>)
1110 * @@param loc File location
1112 astnode *astnode_create_if(astnode *expr, astnode *then, astnode *elif, astnode *els, location loc)
1114 /* Create the node */
1115 astnode *n = astnode_create(IF_NODE, loc);
1116 /* This node has several children: List of CASE nodes, possibly ended by DEFAULT node */
1117 astnode_add_child(n, astnode_create_case(expr, then, loc) );
1118 astnode_add_child(n, elif);
1120 astnode_add_child(n, astnode_create_default(els, loc));
1122 /* Return the newly created node */
1127 * Creates a CASE node.
1128 * @@param expr Expression
1129 * @@param then List of statement to assemble when expr is non-zero (true)
1130 * @@param loc File location
1132 astnode *astnode_create_case(astnode *expr, astnode *then, location loc)
1134 /* Create the node */
1135 astnode *n = astnode_create(CASE_NODE, loc);
1136 /* This node has two children: expression to test and list of statements. */
1137 astnode_add_children(
1141 astnode_create_list(then)
1143 /* Return the newly created node */
1148 * Creates a DEFAULT node.
1149 * @@param stmts List of statements
1150 * @@param loc File location
1152 astnode *astnode_create_default(astnode *stmts, location loc)
1154 /* Create the node */
1155 astnode *n = astnode_create(DEFAULT_NODE, loc);
1156 /* This node has list of statements as children. */
1161 /* Return the newly created node */
1166 * Creates an ifdef node.
1167 * @@param ident The identifier to check
1168 * @@param then The statement(s) to assemble when ident is defined
1169 * @@param els The statement(s) to assemble when ident is not defined (can be <code>NULL</code>)
1170 * @@param loc File location
1172 astnode *astnode_create_ifdef(astnode *ident, astnode *then, astnode *els, location loc)
1174 /* Create the node */
1175 astnode *n = astnode_create(IFDEF_NODE, loc);
1176 /* This node has three children: identifier to test, then-part, else-part */
1177 astnode_add_children(
1181 astnode_create_list(then),
1182 astnode_create_list(els)
1184 /* Return the newly created node */
1189 * Creates an ifndef node.
1190 * @@param ident The identifier to check
1191 * @@param then The statement(s) to assemble when ident is not defined
1192 * @@param els The statement(s) to assemble when ident is defined (can be <code>NULL</code>)
1193 * @@param loc File location
1195 astnode *astnode_create_ifndef(astnode *ident, astnode *then, astnode *els, location loc)
1197 /* Create the node */
1198 astnode *n = astnode_create(IFNDEF_NODE, loc);
1199 /* This node has three children: identifier to test, then-part, else-part */
1200 astnode_add_children(
1204 astnode_create_list(then),
1205 astnode_create_list(els)
1207 /* Return the newly created node */
1212 * Creates a macro declaration node.
1213 * @@param ident Name of macro
1214 * @@param params List of parameters (can be <code>NULL</code>)
1215 * @@param body Macro body
1216 * @@param loc File location
1218 astnode *astnode_create_macro_decl(astnode *ident, astnode *params, astnode *body, location loc)
1220 /* Create the node */
1221 astnode *n = astnode_create(MACRO_DECL_NODE, loc);
1222 /* This node has three children:
1223 1) An identifier, which is the name of the macro
1224 2) List of parameters
1225 3) List of statements, which is the macro body */
1226 astnode_add_children(
1230 astnode_create_list(params),
1231 astnode_create_list(body)
1233 /* Return the newly created node */
1238 * Creates a macro node.
1239 * @@param ident Name of macro
1240 * @@param args List of arguments (can be <code>NULL</code>)
1241 * @@param loc File location
1243 astnode *astnode_create_macro(astnode *ident, astnode *args, location loc)
1245 /* Create the node */
1246 astnode *n = astnode_create(MACRO_NODE, loc);
1247 /* Add the children */
1248 astnode_add_children(
1252 astnode_create_list(args)
1254 /* Return the newly created node */
1259 * Creates an equ node.
1260 * @@param ident Identifier
1261 * @@param expr Expression
1262 * @@param loc File location
1264 astnode *astnode_create_equ(astnode *ident, astnode *expr, location loc)
1266 /* Create the node */
1267 astnode *n = astnode_create(EQU_NODE, loc);
1268 /* Add the children */
1269 astnode_add_children(n, 2, ident, expr);
1270 /* Return the newly created node */
1275 * Creates an assign node.
1276 * @@param ident Identifier
1277 * @@param expr Expression
1278 * @@param loc File location
1280 astnode *astnode_create_assign(astnode *ident, astnode *expr, location loc)
1282 /* Create the node */
1283 astnode *n = astnode_create(ASSIGN_NODE, loc);
1284 /* Add the children */
1285 astnode_add_children(n, 2, ident, expr);
1286 /* Return the newly created node */
1291 * Creates a storage node.
1292 * @@param type Type of storage
1293 * @@param count Expression with contains count
1294 * @@param loc File location
1296 astnode *astnode_create_storage(astnode *type, astnode *count, location loc)
1298 /* Create the node */
1299 astnode *n = astnode_create(STORAGE_NODE, loc);
1300 /* Add the type of data (enumerated or identifier) */
1301 astnode_add_child(n, type);
1302 /* Second child: Count */
1303 if (count == NULL) {
1304 /* No count given, default=1 */
1305 count = astnode_create_integer(1, loc);
1307 astnode_add_child(n, count);
1308 /* Return the newly created node */
1313 * Creates an incsrc node.
1314 * @@param file File specifier
1315 * @@param loc File location
1317 astnode *astnode_create_incsrc(astnode *file, location loc)
1319 /* Create the node */
1320 astnode *n = astnode_create(INCSRC_NODE, loc);
1321 /* One child: Path to file */
1322 astnode_add_child(n, file);
1323 /* Return the newly created node */
1328 * Creates an incbin node.
1329 * @@param file File specifier
1330 * @@param loc File location
1332 astnode *astnode_create_incbin(astnode *file, location loc)
1334 /* Create the node */
1335 astnode *n = astnode_create(INCBIN_NODE, loc);
1336 /* One child: Path to file */
1337 astnode_add_child(n, file);
1338 /* Return the newly created node */
1343 * Creates a charmap node.
1344 * @@param file File specifier
1345 * @@param loc File location
1347 astnode *astnode_create_charmap(astnode *file, location loc)
1349 /* Create the node */
1350 astnode *n = astnode_create(CHARMAP_NODE, loc);
1351 /* One child: Path to file */
1352 astnode_add_child(n, file);
1353 /* Return the newly created node */
1358 * Creates a structure (STRUC) instance node.
1359 * @@param vals Values for the structure fields
1360 * @@param loc File location
1362 astnode *astnode_create_struc(astnode *vals, location loc)
1364 /* Create the node */
1365 astnode *n = astnode_create(STRUC_NODE, loc);
1366 /* Children: value list */
1367 astnode_add_child(n, vals);
1368 /* Return the newly created node */
1372 * Creates a structure (STRUC) declaration node.
1373 * @@param id Structure identifier
1374 * @@param stmts Statements of the structure declaration
1375 * @@param loc File location
1377 astnode *astnode_create_struc_decl(astnode *id, astnode *stmts, location loc)
1379 /* Create the node */
1380 astnode *n = astnode_create(STRUC_DECL_NODE, loc);
1381 /* Two children: Identifier, statement list */
1382 astnode_add_child(n, id);
1383 astnode_add_child(n, stmts);
1384 /* Return the newly created node */
1389 * Creates a union declaration node.
1390 * @@param id Union identifier
1391 * @@param stmts Statements of the union declaration
1392 * @@param loc File location
1394 astnode *astnode_create_union_decl(astnode *id, astnode *stmts, location loc)
1396 /* Create the node */
1397 astnode *n = astnode_create(UNION_DECL_NODE, loc);
1398 /* Two children: Identifier, statement list */
1399 astnode_add_child(n, id);
1400 astnode_add_child(n, stmts);
1401 /* Return the newly created node */
1406 * Creates an enum declaration node.
1407 * @@param id Enum identifier
1408 * @@param stmts Statements of the enum declaration
1409 * @@param loc File location
1411 astnode *astnode_create_enum_decl(astnode *id, astnode *stmts, location loc)
1413 /* Create the node */
1414 astnode *n = astnode_create(ENUM_DECL_NODE, loc);
1415 /* Two children: Identifier, statement list */
1416 astnode_add_child(n, id);
1417 astnode_add_child(n, stmts);
1418 /* Return the newly created node */
1423 * Creates a record declaration node.
1424 * @@param id Record identifier
1425 * @@param fields Fields of the record
1426 * @@param loc File location
1428 astnode *astnode_create_record_decl(astnode *id, astnode *fields, location loc)
1430 /* Create the node */
1431 astnode *n = astnode_create(RECORD_DECL_NODE, loc);
1432 /* Two children: Identifier, field list */
1433 astnode_add_child(n, id);
1434 astnode_add_child(n, fields);
1435 /* Return the newly created node */
1440 * Creates a bitfield declaration node.
1441 * @@param id Identifier
1442 * @@param width Width of field
1443 * @@param loc Location
1445 astnode *astnode_create_bitfield_decl(astnode *id, astnode *width, location loc)
1447 /* Create the node */
1448 astnode *n = astnode_create(BITFIELD_DECL_NODE, loc);
1449 /* Two children: Identifier and width */
1450 astnode_add_child(n, id);
1451 astnode_add_child(n, width);
1452 /* Return the newly created node */
1457 * Creates a public node.
1459 astnode *astnode_create_public(astnode *l, location loc)
1461 /* Create the node */
1462 astnode *n = astnode_create(PUBLIC_NODE, loc);
1463 /* Add list of identifiers as child */
1464 astnode_add_child(n, l);
1465 /* Return the newly created node */
1470 * Creates an extrn node.
1471 * @@param l List of identifiers
1472 * @@param t Symbol type specifier
1473 * @@param f From unit (identifier, may be <code>NULL</code>)
1475 astnode *astnode_create_extrn(astnode *l, astnode *t, astnode *f, location loc)
1477 /* Create the node */
1478 astnode *n = astnode_create(EXTRN_NODE, loc);
1479 /* Add type specifier as child */
1480 astnode_add_child(n, t);
1481 /* Add list of identifiers as child */
1482 astnode_add_child(n, astnode_create_list(l));
1483 /* Add from unit identifier */
1484 astnode_add_child(n, f);
1485 /* Return the newly created node */
1490 * Creates a dataseg node.
1492 astnode *astnode_create_dataseg(int modifiers, location loc)
1494 /* Create the node */
1495 astnode *n = astnode_create(DATASEG_NODE, loc);
1497 n->modifiers = modifiers;
1498 /* Return the newly created node */
1503 * Creates a codeseg node.
1505 astnode *astnode_create_codeseg(location loc)
1507 /* Create the node */
1508 astnode *n = astnode_create(CODESEG_NODE, loc);
1509 /* Return the newly created node */
1514 * Creates a data node.
1515 * @@param type Type specifier
1516 * @@param data List of values
1517 * @@param loc File location
1519 astnode *astnode_create_data(astnode *type, astnode *data, location loc)
1521 /* Create the node */
1522 astnode *n = astnode_create(DATA_NODE, loc);
1523 /* Add the type of data (enumerated or identifier) */
1524 astnode_add_child(n, type);
1525 /* Add list of values */
1526 astnode_add_child(n, data);
1527 /* Return the newly created node */
1532 * Creates a file path node.
1533 * This is similar to a string literal node, the only difference is semantics.
1534 * A file path node implies that the path can be relative to both current
1535 * directory and any of the directories in the search path.
1536 * @@param path The path this node represents
1537 * @@param loc File location
1539 astnode *astnode_create_file_path(const char *path, location loc)
1541 /* Create the node */
1542 astnode *n = astnode_create(FILE_PATH_NODE, loc);
1543 /* Allocate and store text */
1544 n->file_path = (char *)malloc(strlen(path)+1);
1545 if (n->file_path != NULL) {
1546 strcpy(n->file_path, path);
1548 /* Return the newly created node */
1553 * Creates a (global) label node.
1554 * @@param s Name of label
1555 * @@param addr Address
1556 * @@param type Datatype (may be <code>NULL</code>)
1557 * @@param loc Location
1559 astnode *astnode_create_label(const char *s, astnode *addr, astnode *type, location loc)
1561 /* Create the node */
1562 astnode *n = astnode_create(LABEL_NODE, loc);
1563 /* Allocate and store text */
1564 n->label = (char *)malloc(strlen(s)+1);
1565 if (n->label != NULL) {
1566 strcpy(n->label, s);
1568 /* Two children: Datatype and address */
1570 addr = astnode_create_pc(loc);
1573 type = astnode_create_datatype(BYTE_DATATYPE, NULL, loc);
1575 astnode_add_child(n, type);
1576 astnode_add_child(n, addr);
1577 /* Return the newly created node */
1582 * Creates a local label node.
1583 * @@param s Name of label
1584 * @@param loc Location
1586 astnode *astnode_create_local_label(const char *s, location loc)
1588 /* Create the node */
1589 astnode *n = astnode_create(LOCAL_LABEL_NODE, loc);
1590 /* Allocate and store text */
1591 n->label = (char *)malloc(strlen(s)+1);
1592 if (n->label != NULL) {
1593 strcpy(n->label, s);
1595 /* Return the newly created node */
1600 * Creates a local identifier node.
1601 * @@param s Identifier
1602 * @@param loc Location
1604 astnode *astnode_create_local_id(const char *s, location loc)
1606 /* Create the node */
1607 astnode *n = astnode_create(LOCAL_ID_NODE, loc);
1608 /* Allocate and store text */
1609 n->ident = (char *)malloc(strlen(s)+1);
1610 if (n->ident != NULL) {
1611 strcpy(n->ident, s);
1613 /* Return the newly created node */
1618 * Creates a list node.
1619 * This is a way to group a list of nodes in a parent node.
1620 * @@param l List of nodes to group in list node
1622 astnode *astnode_create_list(astnode *l)
1626 /* Create the node */
1628 n = astnode_create(LIST_NODE, l->loc);
1629 /* Add list of values */
1630 astnode_add_child(n, l);
1633 /* Make a node with zero children */
1634 n = astnode_create(LIST_NODE, dummyloc);
1636 /* Return the newly created node (or NULL) */
1641 * Creates a PC node.
1642 * @@param loc File location
1644 astnode *astnode_create_pc(location loc)
1646 return astnode_create(CURRENT_PC_NODE, loc);
1650 * Creates a binary node.
1651 * @@param bin Dynamically allocated (malloc() ) data that this node wraps. Will be freed automatically by astnode_finalize()
1652 * @@param size Size of bin
1653 * @@param loc File location
1655 astnode *astnode_create_binary(unsigned char *bin, int size, location loc)
1657 /* Create the node */
1658 astnode *n = astnode_create(BINARY_NODE, loc);
1660 n->binary.data = bin;
1661 n->binary.size = size;
1662 /* Return the newly created node */
1667 * Creates a tombstone node, which is a marker node that says that another node
1669 * @@param type The type of node that used to live here
1670 * @@param loc File location
1672 astnode *astnode_create_tombstone(astnode_type type, location loc)
1674 /* Create the node */
1675 astnode *n = astnode_create(TOMBSTONE_NODE, loc);
1676 /* Store the type of the old node */
1677 n->param = (long)type;
1678 /* Return the newly created node */
1683 * Creates a dot operator node.
1684 * Represents a structure field access of the form 'before.after'.
1685 * @@param before Structure identifier
1686 * @@param after Field identifier (can be another dot op, or an identifier)
1688 astnode *astnode_create_dot(astnode *before, astnode *after, location loc)
1690 /* Create the node */
1691 astnode *n = astnode_create(DOT_NODE, loc);
1692 /* Two children: 'before' . 'after' */
1693 astnode_add_child(n, before);
1694 astnode_add_child(n, after);
1695 /* Return the newly created node */
1700 * Creates a sizeof operator node.
1701 * @@param expr Expression (datatype?)
1702 * @@param loc Location
1704 astnode *astnode_create_sizeof(astnode *expr, location loc)
1706 /* Create the node */
1707 astnode *n = astnode_create(SIZEOF_NODE, loc);
1708 /* One child: expression */
1709 astnode_add_child(n, expr);
1710 /* Return the newly created node */
1715 * Creates a datatype node.
1716 * @@param t The datatype this node represents
1717 * @@param id If the datatype is a custom one, this is its name
1718 * @@param loc Location
1720 astnode *astnode_create_datatype(datatype t, astnode *id, location loc)
1722 /* Create the node */
1723 astnode *n = astnode_create(DATATYPE_NODE, loc);
1724 /* Set the datatype */
1726 /* Possibly one child: identifier */
1728 astnode_add_child(n, id);
1730 /* Return the newly created node */
1735 * Creates a variable declaration node.
1736 * @@param modifiers PUBLIC_FLAG | ZEROPAGE_FLAG
1737 * @@param id Identifier
1738 * @@param data Datatype+initializer
1739 * @@param loc Location
1741 astnode *astnode_create_var_decl(int modifiers, astnode *id, astnode *data, location loc)
1743 /* Create the node */
1744 astnode *n = astnode_create(VAR_DECL_NODE, loc);
1746 n->modifiers = modifiers;
1747 /* Two children: Identifier and datatype+initializer */
1748 astnode_add_child(n, id);
1749 astnode_add_child(n, data);
1750 /* Return the newly created node */
1757 astnode *astnode_create_scope(astnode *left, astnode *right, location loc)
1759 /* Create the node */
1760 astnode *n = astnode_create(SCOPE_NODE, loc);
1761 /* Two children: left and right */
1762 astnode_add_child(n, left);
1763 astnode_add_child(n, right);
1764 /* Return the newly created node */
1769 * Creates a procedure (PROC) node.
1770 * @@param ident Name of procedure
1771 * @@param stmts Procedure statements
1772 * @@param loc File location
1774 astnode *astnode_create_proc(astnode *ident, astnode *stmts, location loc)
1776 /* Create the node */
1777 astnode *n = astnode_create(PROC_NODE, loc);
1778 /* This node has two children:
1779 1) An identifier, which is the name of the procedure
1780 2) List of statements, which is the procedure body */
1781 astnode_add_children(
1785 astnode_create_list(stmts)
1787 /* Return the newly created node */
1792 * Creates a REPT node.
1793 * @@param expr Number of times to repeat statements
1794 * @@param stmts Statement list
1795 * @@param loc File location
1797 astnode *astnode_create_rept(astnode *expr, astnode *stmts, location loc)
1799 /* Create the node */
1800 astnode *n = astnode_create(REPT_NODE, loc);
1801 /* This node has two children:
1802 1) An expression, which is the repeat count
1803 2) List of statements, which is the (anonymous) macro body */
1804 astnode_add_children(
1808 astnode_create_list(stmts)
1810 /* Return the newly created node */
1815 * Creates a WHILE node.
1816 * @@param expr Boolean expression
1817 * @@param stmts Statement list
1818 * @@param loc File location
1820 astnode *astnode_create_while(astnode *expr, astnode *stmts, location loc)
1822 /* Create the node */
1823 astnode *n = astnode_create(WHILE_NODE, loc);
1824 /* This node has two children:
1825 1) A boolean expression
1826 2) List of statements, which is the (anonymous) macro body */
1827 astnode_add_children(
1831 astnode_create_list(stmts)
1833 /* Return the newly created node */
1838 * Creates a MESSAGE node.
1839 * @@param expr Message to print.
1840 * @@param loc File location
1842 astnode *astnode_create_message(astnode *expr, location loc)
1844 /* Create the node */
1845 astnode *n = astnode_create(MESSAGE_NODE, loc);
1846 /* This node has one children: An expression, which is the message to print */
1847 astnode_add_child(n, expr);
1848 /* Return the newly created node */
1853 * Creates a WARNING node.
1854 * @@param str Warning to print.
1855 * @@param loc File location
1857 astnode *astnode_create_warning(astnode *str, location loc)
1859 /* Create the node */
1860 astnode *n = astnode_create(WARNING_NODE, loc);
1861 /* This node has one child: A string, which is the warning to print */
1862 astnode_add_child(n, str);
1863 /* Return the newly created node */
1868 * Creates an ERROR node.
1869 * @@param str Error to print.
1870 * @@param loc File location
1872 astnode *astnode_create_error(astnode *str, location loc)
1874 /* Create the node */
1875 astnode *n = astnode_create(ERROR_NODE, loc);
1876 /* This node has one child: A string, which is the error to print */
1877 astnode_add_child(n, str);
1878 /* Return the newly created node */
1883 * Creates a forward branch declaration node.
1884 * @@param ident Branch name
1885 * @@param loc File location
1887 astnode *astnode_create_forward_branch_decl(const char *ident, location loc)
1889 /* Create the node */
1890 astnode *n = astnode_create(FORWARD_BRANCH_DECL_NODE, loc);
1891 /* Allocate and store text */
1892 n->ident = (char *)malloc(strlen(ident)+1);
1893 if (n->ident != NULL) {
1894 strcpy(n->ident, ident);
1896 /* Return the newly created node */
1901 * Creates a backward branch declaration node.
1902 * @@param ident Branch name
1903 * @@param loc File location
1905 astnode *astnode_create_backward_branch_decl(const char *ident, location loc)
1907 /* Create the node */
1908 astnode *n = astnode_create(BACKWARD_BRANCH_DECL_NODE, loc);
1909 /* Allocate and store text */
1910 n->ident = (char *)malloc(strlen(ident)+1);
1911 if (n->ident != NULL) {
1912 strcpy(n->ident, ident);
1914 /* Return the newly created node */
1919 * Creates a forward branch reference node.
1920 * @@param ident Branch name
1921 * @@param loc File location
1923 astnode *astnode_create_forward_branch(const char *ident, location loc)
1925 /* Create the node */
1926 astnode *n = astnode_create(FORWARD_BRANCH_NODE, loc);
1927 /* Allocate and store text */
1928 n->ident = (char *)malloc(strlen(ident)+1);
1929 if (n->ident != NULL) {
1930 strcpy(n->ident, ident);
1932 /* Return the newly created node */
1937 * Creates a backward branch reference node.
1938 * @@param ident Branch name
1939 * @@param loc File location
1941 astnode *astnode_create_backward_branch(const char *ident, location loc)
1943 /* Create the node */
1944 astnode *n = astnode_create(BACKWARD_BRANCH_NODE, loc);
1945 /* Allocate and store text */
1946 n->ident = (char *)malloc(strlen(ident)+1);
1947 if (n->ident != NULL) {
1948 strcpy(n->ident, ident);
1950 /* Return the newly created node */
1955 * Creates a mask operator node.
1956 * @@param expr Expression
1957 * @@param loc Location
1959 astnode *astnode_create_mask(astnode *expr, location loc)
1961 /* Create the node */
1962 astnode *n = astnode_create(MASK_NODE, loc);
1963 /* One child: expression */
1964 astnode_add_child(n, expr);
1965 /* Return the newly created node */
1970 * Creates an ALIGN node.
1971 * @@param idents List of identifiers
1972 * @@param expr Expression
1973 * @@param loc File location
1975 astnode *astnode_create_align(astnode *idents, astnode *expr, location loc)
1977 /* Create the node */
1978 astnode *n = astnode_create(ALIGN_NODE, loc);
1979 /* This node has two children: List of identifiers and alignment constraint */
1980 astnode_add_child(n, astnode_create_list(idents) );
1981 astnode_add_child(n, expr);
1982 /* Return the newly created node */
1987 * Creates an INDEX node.
1988 * @@param ident Identifier being indexed
1989 * @@param expr Index expression
1990 * @@param loc File location
1992 astnode *astnode_create_index(astnode *ident, astnode *expr, location loc)
1994 /* Create the node */
1995 astnode *n = astnode_create(INDEX_NODE, loc);
1996 /* This node has two children: Identifier and expression */
1997 astnode_add_child(n, ident);
1998 astnode_add_child(n, expr);
1999 /* Return the newly created node */
2004 * Creates an ORG node.
2005 * @@param addr Address
2006 * @@param loc File location
2008 astnode *astnode_create_org(astnode *addr, location loc)
2010 /* Create the node */
2011 astnode *n = astnode_create(ORG_NODE, loc);
2012 /* This node has one child: The address */
2013 astnode_add_child(n, addr);
2014 /* Return the newly created node */
2018 /*---------------------------------------------------------------------------*/
2019 /* Functions for (de)serializing a node; probably not complete since it didn't
2020 end up being used by any program.
2023 #define put_byte(f, b) { fputc((unsigned char)(b), f); }
2024 #define put_short(f, w) { put_byte(f, w >> 8); put_byte(f, w); }
2025 #define put_int(f, q) { put_short(f, q >> 16); put_short(f, q); }
2028 * Serializes a node.
2029 * @@param n The node to serialize
2030 * @@param f The file to write to
2032 void astnode_serialize(const astnode *n, FILE *f)
2035 /* Node type: 1 byte */
2036 put_byte(f, astnode_get_type(n));
2037 /* Internal node data */
2038 switch (astnode_get_type(n)) {
2040 /* Assumes that sizeof(long) is same as for int */
2041 put_int(f, n->integer);
2045 case IDENTIFIER_NODE:
2047 case FILE_PATH_NODE:
2049 case LOCAL_LABEL_NODE:
2050 /* Put length first */
2051 put_int(f, strlen(n->string));
2052 /* Put characters */
2053 for (i=0; i<strlen(n->string); i++) {
2054 put_byte(f, n->string[i]);
2059 /* Datatype: 1 byte */
2060 put_byte(f, n->datatype);
2065 put_int(f, n->binary.size);
2066 /* Put data bytes */
2067 for (i=0; i<n->binary.size; i++) {
2068 put_byte(f, n->binary.data[i]);
2072 case ARITHMETIC_NODE:
2076 case INSTRUCTION_NODE:
2081 /* No internal attributes */
2085 put_int(f, astnode_get_child_count(n));
2086 /* Serialize children */
2087 for (i=0; i<astnode_get_child_count(n); i++) {
2088 astnode_serialize(astnode_get_child(n, i), f);
2092 #define get_byte(f) 0
2093 #define get_short(f) 0
2094 #define get_int(f) 0
2097 * Deserializes a node.
2098 * @@param f The file to read from
2100 astnode *astnode_deserialize(FILE *f)
2107 /* Node type: 1 byte */
2108 t = (astnode_type)get_byte(f);
2109 /* Create the node */
2110 n = astnode_create(t, loc);
2111 /* Internal node data */
2112 switch (astnode_get_type(n)) {
2114 /* Assumes that sizeof(long) is same as for int */
2115 n->integer = get_int(f);
2119 case IDENTIFIER_NODE:
2121 case FILE_PATH_NODE:
2123 case LOCAL_LABEL_NODE:
2126 /* Create the character array */
2127 n->string = malloc(len+1);
2128 if (n->string != NULL) {
2129 /* Get characters */
2130 for (i=0; i<len; i++) {
2131 n->string[i] = get_byte(f);
2137 /* Datatype: 1 byte */
2138 n->datatype = get_byte(f);
2143 n->binary.size = get_int(f);
2144 /* Allocate storage */
2145 n->binary.data = (unsigned char *)malloc(n->binary.size);
2146 if (n->binary.data != NULL) {
2147 /* Get data bytes */
2148 for (i=0; i<n->param; i++) {
2149 n->binary.data[i] = get_byte(f);
2154 case ARITHMETIC_NODE:
2158 case INSTRUCTION_NODE:
2163 /* No internal attributes */
2168 /* Deserialize children */
2169 for (i=0; i<len; i++) {
2170 astnode_add_child(n, astnode_deserialize(f));
2173 /* Return the deserialized node */
2181 @*** empty log message ***
2186 * $Id: astnode.c,v 1.13 2007/08/09 22:05:49 khansen Exp khansen $
2195 @general-purpose flags
2200 * $Id: astnode.c,v 1.12 2007/08/07 21:12:16 khansen Exp khansen $
2204 extern char *yy_current_filename();
2215 * $Id: astnode.c,v 1.11 2007/07/22 13:33:26 khansen Exp khansen $
2223 @convert tabs to whitespaces
2228 * $Id: astnode.c,v 1.10 2004/12/29 21:44:04 kenth Exp $
2232 const char *datatype_to_string(astnode *dt)
2235 void astnode_print(astnode *n, int level)
2241 astnode_type astnode_get_type(astnode *n)
2244 astnode *astnode_get_last_sibling(astnode *n)
2247 for (s = n; s->next_sibling != NULL; s = s->next_sibling);
2250 astnode *astnode_get_parent(astnode *n)
2253 astnode *astnode_get_child(astnode *n, int index)
2256 astnode *astnode_get_first_child(astnode *n)
2259 int astnode_get_child_index(astnode *p, astnode *c)
2262 int astnode_get_child_count(astnode *p)
2265 astnode *astnode_clone(astnode *n, location loc)
2268 int astnode_equal(astnode *n1, astnode *n2)
2271 astnode *astnode_get_ancestor(astnode *n, int back)
2274 int astnode_has_ancestor_of_type(astnode *n, astnode_type type)
2277 astnode *astnode_get_next_sibling(astnode *n)
2280 astnode *astnode_get_prev_sibling(astnode *n)
2283 int astnode_is_literal(astnode *n)
2286 astnode *astnode_create_identifier(char *ident, location loc)
2289 astnode *astnode_create_string(char *value, location loc)
2292 astnode *astnode_create_file_path(char *path, location loc)
2295 astnode *astnode_create_label(char *s, astnode *addr, astnode *type, location loc)
2298 astnode *astnode_create_local_label(char *s, location loc)
2301 astnode *astnode_create_local_id(char *s, location loc)
2304 astnode *astnode_create_forward_branch_decl(char *ident, location loc)
2307 astnode *astnode_create_backward_branch_decl(char *ident, location loc)
2310 astnode *astnode_create_forward_branch(char *ident, location loc)
2313 astnode *astnode_create_backward_branch(char *ident, location loc)
2316 void astnode_serialize(astnode *n, FILE *f)
2323 added create_index()
2328 * $Id: astnode.c,v 1.9 2004/12/19 19:58:23 kenth Exp kenth $
2333 case IMPLIED_MODE: return "IMPLIED_MODE";
2334 case ACCUMULATOR_MODE: return "ACCUMULATOR_MODE";
2335 case IMMEDIATE_MODE: return "IMMEDIATE_MODE";
2336 case ZEROPAGE_MODE: return "ZEROPAGE_MODE";
2337 case ZEROPAGE_X_MODE: return "ZEROPAGE_X_MODE";
2338 case ZEROPAGE_Y_MODE: return "ZEROPAGE_Y_MODE";
2339 case ABSOLUTE_MODE: return "ABSOLUTE_MODE";
2340 case ABSOLUTE_X_MODE: return "ABSOLUTE_X_MODE";
2341 case ABSOLUTE_Y_MODE: return "ABSOLUTE_Y_MODE";
2342 case PREINDEXED_INDIRECT_MODE: return "PREINDEXED_INDIRECT_MODE";
2343 case POSTINDEXED_INDIRECT_MODE: return "POSTINDEXED_INDIRECT_MODE";
2344 case INDIRECT_MODE: return "INDIRECT_MODE";
2345 case RELATIVE_MODE: return "RELATIVE_MODE";
2347 return "addressing_mode_to_string: invalid addressing mode";
2351 case ADC_MNEMONIC: return "ADC_MNEMONIC";
2352 case AND_MNEMONIC: return "AND_MNEMONIC";
2353 case ASL_MNEMONIC: return "ASL_MNEMONIC";
2354 case BCC_MNEMONIC: return "BCC_MNEMONIC";
2355 case BCS_MNEMONIC: return "BCS_MNEMONIC";
2356 case BEQ_MNEMONIC: return "BEQ_MNEMONIC";
2357 case BIT_MNEMONIC: return "BIT_MNEMONIC";
2358 case BMI_MNEMONIC: return "BMI_MNEMONIC";
2359 case BNE_MNEMONIC: return "BNE_MNEMONIC";
2360 case BPL_MNEMONIC: return "BPL_MNEMONIC";
2361 case BRK_MNEMONIC: return "BRK_MNEMONIC";
2362 case BVC_MNEMONIC: return "BVC_MNEMONIC";
2363 case BVS_MNEMONIC: return "BVS_MNEMONIC";
2364 case CLC_MNEMONIC: return "CLC_MNEMONIC";
2365 case CLD_MNEMONIC: return "CLD_MNEMONIC";
2366 case CLI_MNEMONIC: return "CLI_MNEMONIC";
2367 case CLV_MNEMONIC: return "CLV_MNEMONIC";
2368 case CMP_MNEMONIC: return "CMP_MNEMONIC";
2369 case CPX_MNEMONIC: return "CPX_MNEMONIC";
2370 case CPY_MNEMONIC: return "CPY_MNEMONIC";
2371 case DEC_MNEMONIC: return "DEC_MNEMONIC";
2372 case DEX_MNEMONIC: return "DEX_MNEMONIC";
2373 case DEY_MNEMONIC: return "DEY_MNEMONIC";
2374 case EOR_MNEMONIC: return "EOR_MNEMONIC";
2375 case INC_MNEMONIC: return "INC_MNEMONIC";
2376 case INX_MNEMONIC: return "INX_MNEMONIC";
2377 case INY_MNEMONIC: return "INY_MNEMONIC";
2378 case JMP_MNEMONIC: return "JMP_MNEMONIC";
2379 case JSR_MNEMONIC: return "JSR_MNEMONIC";
2380 case LDA_MNEMONIC: return "LDA_MNEMONIC";
2381 case LDX_MNEMONIC: return "LDX_MNEMONIC";
2382 case LDY_MNEMONIC: return "LDY_MNEMONIC";
2383 case LSR_MNEMONIC: return "LSR_MNEMONIC";
2384 case NOP_MNEMONIC: return "NOP_MNEMONIC";
2385 case ORA_MNEMONIC: return "ORA_MNEMONIC";
2386 case PHA_MNEMONIC: return "PHA_MNEMONIC";
2387 case PHP_MNEMONIC: return "PHP_MNEMONIC";
2388 case PLA_MNEMONIC: return "PLA_MNEMONIC";
2389 case PLP_MNEMONIC: return "PLP_MNEMONIC";
2390 case ROL_MNEMONIC: return "ROL_MNEMONIC";
2391 case ROR_MNEMONIC: return "ROR_MNEMONIC";
2392 case RTI_MNEMONIC: return "RTI_MNEMONIC";
2393 case RTS_MNEMONIC: return "RTS_MNEMONIC";
2394 case SBC_MNEMONIC: return "SBC_MNEMONIC";
2395 case SEC_MNEMONIC: return "SEC_MNEMONIC";
2396 case SED_MNEMONIC: return "SED_MNEMONIC";
2397 case SEI_MNEMONIC: return "SEI_MNEMONIC";
2398 case STA_MNEMONIC: return "STA_MNEMONIC";
2399 case STX_MNEMONIC: return "STX_MNEMONIC";
2400 case STY_MNEMONIC: return "STY_MNEMONIC";
2401 case TAX_MNEMONIC: return "TAX_MNEMONIC";
2402 case TAY_MNEMONIC: return "TAY_MNEMONIC";
2403 case TSX_MNEMONIC: return "TSX_MNEMONIC";
2404 case TXA_MNEMONIC: return "TXA_MNEMONIC";
2405 case TXS_MNEMONIC: return "TXS_MNEMONIC";
2406 case TYA_MNEMONIC: return "TYA_MNEMONIC";
2408 return "instr_mnemonic_to_string: invalid mnemonic";
2412 case NULL_NODE: return "NULL_NODE";
2413 case INTEGER_NODE: return "INTEGER_NODE";
2414 case STRING_NODE: return "STRING_NODE";
2415 case IDENTIFIER_NODE: return "IDENTIFIER_NODE";
2416 case DATA_NODE: return "DATA_NODE";
2417 case STORAGE_NODE: return "STORAGE_NODE";
2418 case MACRO_DECL_NODE: return "MACRO_DECL_NODE";
2419 case MACRO_NODE: return "MACRO_NODE";
2420 case ARITHMETIC_NODE: return "ARITHMETIC_NODE";
2421 case IF_NODE: return "IF_NODE";
2422 case CASE_NODE: return "CASE_NODE";
2423 case DEFAULT_NODE: return "DEFAULT_NODE";
2424 case IFDEF_NODE: return "IFDEF_NODE";
2425 case IFNDEF_NODE: return "IFNDEF_NODE";
2426 case INCSRC_NODE: return "INCSRC_NODE";
2427 case INCBIN_NODE: return "INCBIN_NODE";
2428 case EQU_NODE: return "EQU_NODE";
2429 case ASSIGN_NODE: return "ASSIGN_NODE";
2430 case ALIGN_NODE: return "ALIGN_NODE";
2431 case INSTRUCTION_NODE: return "INSTRUCTION_NODE";
2432 case FILE_PATH_NODE: return "FILE_PATH_NODE";
2433 case CURRENT_PC_NODE: return "CURRENT_PC_NODE";
2434 case LIST_NODE: return "LIST_NODE";
2435 case LABEL_NODE: return "LABEL_NODE";
2436 case LOCAL_LABEL_NODE: return "LOCAL_LABEL_NODE";
2437 case LOCAL_ID_NODE: return "LOCAL_ID_NODE";
2438 case BINARY_NODE: return "BINARY_NODE";
2439 case PUBLIC_NODE: return "PUBLIC_NODE";
2440 case EXTRN_NODE: return "EXTRN_NODE";
2441 case DATASEG_NODE: return "DATASEG_NODE";
2442 case CODESEG_NODE: return "CODESEG_NODE";
2443 case CHARMAP_NODE: return "CHARMAP_NODE";
2444 case STRUC_NODE: return "STRUC_NODE";
2445 case STRUC_DECL_NODE: return "STRUC_DECL_NODE";
2446 case UNION_DECL_NODE: return "UNION_DECL_NODE";
2447 case ENUM_DECL_NODE: return "ENUM_DECL_NODE";
2448 case RECORD_DECL_NODE: return "RECORD_DECL_NODE";
2449 case BITFIELD_DECL_NODE:return "BITFIELD_DECL_NODE";
2450 case DOT_NODE: return "DOT_NODE";
2451 case SIZEOF_NODE: return "SIZEOF_NODE";
2452 case DATATYPE_NODE: return "DATATYPE_NODE";
2453 case VAR_DECL_NODE: return "VAR_DECL_NODE";
2454 case SCOPE_NODE: return "SCOPE_NODE";
2455 case PROC_NODE: return "PROC_NODE";
2456 case REPT_NODE: return "REPT_NODE";
2457 case WHILE_NODE: return "WHILE_NODE";
2458 case MESSAGE_NODE: return "MESSAGE_NODE";
2459 case WARNING_NODE: return "WARNING_NODE";
2460 case ERROR_NODE: return "ERROR_NODE";
2461 case FORWARD_BRANCH_DECL_NODE: return "FORWARD_BRANCH_DECL_NODE";
2462 case BACKWARD_BRANCH_DECL_NODE: return "BACKWARD_BRANCH_DECL_NODE";
2463 case FORWARD_BRANCH_NODE: return "FORWARD_BRANCH_NODE";
2464 case BACKWARD_BRANCH_NODE: return "BACKWARD_BRANCH_NODE";
2465 case MASK_NODE: return "MASK_NODE";
2466 case INDEX_NODE: return "INDEX_NODE";
2467 case TOMBSTONE_NODE: return "TOMBSTONE_NODE";
2469 return "astnode_type_to_string: invalid type";
2472 switch (dt->datatype) {
2473 case BYTE_DATATYPE: return "BYTE_DATATYPE";
2474 case CHAR_DATATYPE: return "CHAR_DATATYPE";
2475 case WORD_DATATYPE: return "WORD_DATATYPE";
2476 case DWORD_DATATYPE: return "DWORD_DATATYPE";
2477 case USER_DATATYPE: return "USER_DATATYPE"; // astnode_get_child(dt, 0)->ident;
2479 return "datatype_to_string: invalid datatype";
2483 case PLUS_OPERATOR: return "PLUS_OPERATOR";
2484 case MINUS_OPERATOR: return "MINUS_OPERATOR";
2485 case MUL_OPERATOR: return "MUL_OPERATOR";
2486 case DIV_OPERATOR: return "DIV_OPERATOR";
2487 case MOD_OPERATOR: return "MOD_OPERATOR";
2488 case AND_OPERATOR: return "AND_OPERATOR";
2489 case OR_OPERATOR: return "OR_OPERATOR";
2490 case XOR_OPERATOR: return "XOR_OPERATOR";
2491 case SHL_OPERATOR: return "SHL_OPERATOR";
2492 case SHR_OPERATOR: return "SHR_OPERATOR";
2493 case LT_OPERATOR: return "LT_OPERATOR";
2494 case GT_OPERATOR: return "GT_OPERATOR";
2495 case EQ_OPERATOR: return "EQ_OPERATOR";
2496 case NE_OPERATOR: return "NE_OPERATOR";
2497 case LE_OPERATOR: return "LE_OPERATOR";
2498 case GE_OPERATOR: return "GE_OPERATOR";
2499 case NEG_OPERATOR: return "NEG_OPERATOR";
2500 case NOT_OPERATOR: return "NOT_OPERATOR";
2501 case LO_OPERATOR: return "LO_OPERATOR";
2502 case HI_OPERATOR: return "HI_OPERATOR";
2503 case UMINUS_OPERATOR: return "UMINUS_OPERATOR";
2504 case BANK_OPERATOR: return "BANK_OPERATOR";
2506 return "operator_to_string: invalid operator";
2510 for (i=0; i<nlevels; i++) {
2516 /* Indent so it looks pretty */
2518 /* Print the node type */
2519 printf(astnode_type_to_string(astnode_get_type(n)));
2520 /* Print attributes for those that have */
2521 switch (astnode_get_type(n)) {
2522 case INTEGER_NODE: printf("(%d)", n->integer); break;
2523 case STRING_NODE: printf("(\"%s\")", n->string); break;
2524 case IDENTIFIER_NODE: printf("(%s)", n->ident); break;
2525 case LOCAL_ID_NODE: printf("(%s)", n->ident); break;
2526 case FILE_PATH_NODE: printf("(%s)", n->file_path); break;
2527 case LABEL_NODE: printf("(%s)", n->label); break;
2528 case LOCAL_LABEL_NODE: printf("(%s)", n->label); break;
2529 case BINARY_NODE: printf("(%d)", n->binary.size); break;
2531 case ARITHMETIC_NODE:
2534 operator_to_string(n->oper)
2538 case INSTRUCTION_NODE:
2541 instr_mnemonic_to_string(n->instr.mnemonic),
2542 addressing_mode_to_string(n->instr.mode),
2550 datatype_to_string(n)
2554 case FORWARD_BRANCH_DECL_NODE:
2555 case BACKWARD_BRANCH_DECL_NODE:
2556 case FORWARD_BRANCH_NODE:
2557 case BACKWARD_BRANCH_NODE:
2558 printf("(%s)", n->ident);
2561 case TOMBSTONE_NODE:
2564 astnode_type_to_string(n->param)
2569 /* Has no internal attributes */
2573 /* Print the children */
2574 for (i=0; i<astnode_get_child_count(n); i++) {
2575 astnode_print(astnode_get_child(n, i), level+1);
2579 /* Fix: Sometimes loc.file is NULL for some reason */
2580 extern char *yy_current_filename();
2581 if (loc.file == NULL) {
2582 loc.file = yy_current_filename();
2584 /* Allocate memory for node struct */
2585 astnode *n = (astnode *)malloc(sizeof(astnode));
2586 /* Fill in struct only if alloc succeeded */
2592 n->parent = n->first_child = n->prev_sibling = n->next_sibling = NULL;
2597 /* Remove the node from the tree it's in. */
2599 /* Finalize all its children recursively. */
2600 while (astnode_get_first_child(n) != NULL) {
2601 astnode_finalize(astnode_remove_child_at(n, 0));
2603 /* Free up memory. */
2604 switch (astnode_get_type(n)) {
2605 case LABEL_NODE: SAFE_FREE(n->label); break;
2606 case LOCAL_LABEL_NODE: SAFE_FREE(n->label); break;
2607 case STRING_NODE: SAFE_FREE(n->string); break;
2608 case IDENTIFIER_NODE: SAFE_FREE(n->ident); break;
2609 case LOCAL_ID_NODE: SAFE_FREE(n->ident); break;
2610 case FILE_PATH_NODE: SAFE_FREE(n->file_path);break;
2611 case BINARY_NODE: SAFE_FREE(n->binary.data); break;
2612 case FORWARD_BRANCH_DECL_NODE:
2613 case BACKWARD_BRANCH_DECL_NODE:
2614 case FORWARD_BRANCH_NODE:
2615 case BACKWARD_BRANCH_NODE:
2616 SAFE_FREE(n->ident);
2619 /* Has no internal attributes that are dynamically allocated */
2625 return (n != NULL) ? n->type : NULL_NODE;
2629 for (n = c; n != NULL; n = n->next_sibling) {
2636 /* Get the parent of the node to be replaced */
2637 p = astnode_get_parent(old_node);
2639 /* Call remove_child on parent */
2640 i = astnode_remove_child(p, old_node);
2641 /* Insert new child at old child's position */
2642 astnode_insert_child(p, new_node, i);
2646 astnode *p = astnode_get_parent(n);
2648 astnode_remove_child(p, n);
2653 i = astnode_get_child_index(p, c);
2655 /* Remove head of list. */
2656 p->first_child = c->next_sibling;
2657 if (p->first_child) {
2658 p->first_child->prev_sibling = NULL;
2660 c->parent = c->next_sibling = c->prev_sibling = NULL;
2663 c->prev_sibling->next_sibling = c->next_sibling;
2664 if (c->next_sibling) {
2665 c->next_sibling->prev_sibling = c->prev_sibling;
2667 c->parent = c->next_sibling = c->prev_sibling = NULL;
2672 astnode *c = astnode_get_child(p, i);
2673 astnode_remove_child(p, c);
2678 if (p == NULL) { return NULL; }
2679 if (p->first_child != NULL) {
2681 p->first_child = NULL;
2682 /* Set parent of all siblings to NULL. */
2683 astnode_set_parent(c, NULL);
2684 /* Return the list of children */
2688 /* Has no children. */
2696 x = astnode_get_child(p, i); /* Current child at that position */
2698 /* There isn't a node here. Just add to end. */
2699 astnode_add_child(p, c);
2702 n = astnode_get_last_sibling(c);
2703 /* Make c..n precede x */
2704 c->prev_sibling = x->prev_sibling;
2705 if (x->prev_sibling) {
2706 x->prev_sibling->next_sibling = c;
2708 n->next_sibling = x;
2709 x->prev_sibling = n;
2712 astnode_set_parent(c, p);
2722 for (s = n; s->next_sibling != NULL; s = s->next_sibling);
2727 return n ? n->parent : NULL;
2730 if (n && new_child) {
2731 if (n->first_child == NULL) {
2732 /* This node has no children, add this as the first one */
2733 n->first_child = new_child;
2734 astnode_set_parent(new_child, n);
2737 astnode_add_sibling(n->first_child, new_child);
2746 va_start(ap, count);
2747 for (i=0; i<count; i++) {
2748 c = va_arg(ap, astnode*);
2749 astnode_add_child(n, c);
2756 if (brother && sister) {
2757 /* Add to end of list */
2758 n = astnode_get_last_sibling(brother);
2759 n->next_sibling = sister;
2760 sister->prev_sibling = n;
2761 p = astnode_get_parent(brother);
2762 astnode_set_parent(sister, p);
2770 for (i = 0; i != index; i++) {
2772 /* No child at that index. */
2775 c = c->next_sibling;
2779 /* Node is NULL, so return NULL */
2783 return (n == NULL) ? NULL : n->first_child;
2789 for (i=0, n=p->first_child; (n != c) && (n != NULL); i++, n=n->next_sibling);
2798 for (c = p->first_child; c != NULL; count++, c = c->next_sibling);
2805 if (n == NULL) { return NULL; }
2807 c = astnode_create(astnode_get_type(n), loc);
2808 /* Copy attributes */
2809 switch (astnode_get_type(n)) {
2811 c->integer = n->integer;
2815 case IDENTIFIER_NODE:
2816 case FILE_PATH_NODE:
2818 case LOCAL_LABEL_NODE:
2820 c->string = (char *)malloc(strlen(n->string)+1);
2821 if (c->string != NULL) {
2822 strcpy(c->string, n->string);
2826 case ARITHMETIC_NODE:
2830 case INSTRUCTION_NODE:
2831 c->instr = n->instr;
2835 c->binary = n->binary;
2839 c->datatype = n->datatype;
2843 c->param = n->param;
2845 /* Clone children (TODO: OPTIMIZE THIS) */
2846 for (n_c=n->first_child; n_c != NULL; n_c=n_c->next_sibling) {
2847 astnode_add_child(c, astnode_clone(n_c, loc));
2849 /* Return the clone */
2854 /* Verify that types are the same */
2855 if (astnode_get_type(n1) != astnode_get_type(n2)) {
2856 return 0; /* Types don't match -- not equal */
2858 /* Verify that internal data is the same */
2859 switch (astnode_get_type(n1)) {
2860 case ARITHMETIC_NODE: if (n1->oper != n2->oper) return 0; break;
2861 case INTEGER_NODE: if (n1->integer != n2->integer) return 0; break;
2862 case STRING_NODE: if (strcmp(n1->string, n2->string)) return 0; break;
2863 case IDENTIFIER_NODE: if (strcmp(n1->ident, n2->ident)) return 0; break;
2864 case LOCAL_ID_NODE: if (strcmp(n1->ident, n2->ident)) return 0; break;
2865 case FILE_PATH_NODE: if (strcmp(n1->file_path, n2->file_path)) return 0; break;
2866 case LABEL_NODE: if (strcmp(n1->label, n2->label)) return 0; break;
2867 case LOCAL_LABEL_NODE: if (strcmp(n1->label, n2->label)) return 0; break;
2868 case BINARY_NODE: if (n1->binary.size != n2->binary.size) return 0; break;
2869 case DATATYPE_NODE: if (n1->datatype != n2->datatype) return 0; break;
2870 case TOMBSTONE_NODE: if (n1->param != n2->param) return 0; break;
2871 case INSTRUCTION_NODE: if ( (n1->instr.mnemonic != n2->instr.mnemonic) || (n1->instr.mode != n2->instr.mode) ) return 0; break;
2873 /* Has no internal attributes */
2876 /* Verify that they have the same number of children */
2877 if (astnode_get_child_count(n1) != astnode_get_child_count(n2)) {
2880 /* Verify that children are equal */
2881 for (i=0; i<astnode_get_child_count(n1); i++) {
2882 if (!astnode_equal(astnode_get_child(n1, i), astnode_get_child(n2, i))) {
2891 astnode *a = astnode_get_parent(n);
2892 for (i=0; i<back; i++) {
2893 a = astnode_get_parent(a);
2899 for (a = astnode_get_parent(n); a != NULL; a = astnode_get_parent(a) ) {
2900 if (astnode_is_type(a, type)) {
2907 if (n == NULL) { return NULL; }
2908 return n->next_sibling;
2911 if (n == NULL) { return NULL; }
2912 return n->prev_sibling;
2915 switch (astnode_get_type(n)) {
2929 /* Create the node */
2930 astnode *n = astnode_create(NULL_NODE, loc);
2931 /* Return the newly created node */
2935 /* Create the node */
2936 astnode *n = astnode_create(INSTRUCTION_NODE, loc);
2937 /* Store the mnemonic and addressing mode */
2938 n->instr.mnemonic = mnemonic;
2939 n->instr.mode = mode;
2940 /* This node has one child: The operand, which is an expression */
2941 astnode_add_child(n, operand);
2942 /* Return the newly created node */
2946 /* Create the node */
2947 astnode *n = astnode_create(IDENTIFIER_NODE, loc);
2948 /* Allocate and store text */
2949 n->ident = (char *)malloc(strlen(ident)+1);
2950 if (n->ident != NULL) {
2951 strcpy(n->ident, ident);
2953 /* Return the newly created node */
2957 /* Create the node */
2958 astnode *n = astnode_create(INTEGER_NODE, loc);
2959 /* Store the integer which this node represents */
2961 /* Return the newly created node */
2965 /* Create the node */
2966 astnode *n = astnode_create(STRING_NODE, loc);
2967 /* Allocate and store text */
2968 n->string = (char *)malloc(strlen(value)+1);
2969 if (n->string != NULL) {
2970 strcpy(n->string, value);
2972 /* Return the newly created node */
2976 /* Create the node */
2977 astnode *n = astnode_create(ARITHMETIC_NODE, loc);
2978 /* Store the operator, which describes the type of expression */
2980 /* This node has two children: left-hand side and right-hand side expression */
2981 /* For unary operators right-hand side should be <code>NULL</code> */
2982 astnode_add_children(n, 2, left, right);
2983 /* Return the newly created node */
2987 /* Create the node */
2988 astnode *n = astnode_create(IF_NODE, loc);
2989 /* This node has several children: List of CASE nodes, possibly ended by DEFAULT node */
2990 astnode_add_child(n, astnode_create_case(expr, then, loc) );
2991 astnode_add_child(n, elif);
2993 astnode_add_child(n, astnode_create_default(els, loc));
2995 /* Return the newly created node */
2999 /* Create the node */
3000 astnode *n = astnode_create(CASE_NODE, loc);
3001 /* This node has two children: expression to test and list of statements. */
3002 astnode_add_children(
3006 astnode_create_list(then)
3008 /* Return the newly created node */
3012 /* Create the node */
3013 astnode *n = astnode_create(DEFAULT_NODE, loc);
3014 /* This node has list of statements as children. */
3019 /* Return the newly created node */
3023 /* Create the node */
3024 astnode *n = astnode_create(IFDEF_NODE, loc);
3025 /* This node has three children: identifier to test, then-part, else-part */
3026 astnode_add_children(
3030 astnode_create_list(then),
3031 astnode_create_list(els)
3033 /* Return the newly created node */
3037 /* Create the node */
3038 astnode *n = astnode_create(IFNDEF_NODE, loc);
3039 /* This node has three children: identifier to test, then-part, else-part */
3040 astnode_add_children(
3044 astnode_create_list(then),
3045 astnode_create_list(els)
3047 /* Return the newly created node */
3051 /* Create the node */
3052 astnode *n = astnode_create(MACRO_DECL_NODE, loc);
3053 /* This node has three children:
3054 1) An identifier, which is the name of the macro
3055 2) List of parameters
3056 3) List of statements, which is the macro body */
3057 astnode_add_children(
3061 astnode_create_list(params),
3062 astnode_create_list(body)
3064 /* Return the newly created node */
3068 /* Create the node */
3069 astnode *n = astnode_create(MACRO_NODE, loc);
3070 /* Add the children */
3071 astnode_add_children(
3075 astnode_create_list(args)
3077 /* Return the newly created node */
3081 /* Create the node */
3082 astnode *n = astnode_create(EQU_NODE, loc);
3083 /* Add the children */
3084 astnode_add_children(n, 2, ident, expr);
3085 /* Return the newly created node */
3089 /* Create the node */
3090 astnode *n = astnode_create(ASSIGN_NODE, loc);
3091 /* Add the children */
3092 astnode_add_children(n, 2, ident, expr);
3093 /* Return the newly created node */
3097 /* Create the node */
3098 astnode *n = astnode_create(STORAGE_NODE, loc);
3099 /* Add the type of data (enumerated or identifier) */
3100 astnode_add_child(n, type);
3101 /* Second child: Count */
3102 if (count == NULL) {
3103 /* No count given, default=1 */
3104 count = astnode_create_integer(1, loc);
3106 astnode_add_child(n, count);
3107 /* Return the newly created node */
3111 /* Create the node */
3112 astnode *n = astnode_create(INCSRC_NODE, loc);
3113 /* One child: Path to file */
3114 astnode_add_child(n, file);
3115 /* Return the newly created node */
3119 /* Create the node */
3120 astnode *n = astnode_create(INCBIN_NODE, loc);
3121 /* One child: Path to file */
3122 astnode_add_child(n, file);
3123 /* Return the newly created node */
3127 /* Create the node */
3128 astnode *n = astnode_create(CHARMAP_NODE, loc);
3129 /* One child: Path to file */
3130 astnode_add_child(n, file);
3131 /* Return the newly created node */
3135 /* Create the node */
3136 astnode *n = astnode_create(STRUC_NODE, loc);
3137 /* Children: value list */
3138 astnode_add_child(n, vals);
3139 /* Return the newly created node */
3143 /* Create the node */
3144 astnode *n = astnode_create(STRUC_DECL_NODE, loc);
3145 /* Two children: Identifier, statement list */
3146 astnode_add_child(n, id);
3147 astnode_add_child(n, stmts);
3148 /* Return the newly created node */
3152 /* Create the node */
3153 astnode *n = astnode_create(UNION_DECL_NODE, loc);
3154 /* Two children: Identifier, statement list */
3155 astnode_add_child(n, id);
3156 astnode_add_child(n, stmts);
3157 /* Return the newly created node */
3161 /* Create the node */
3162 astnode *n = astnode_create(ENUM_DECL_NODE, loc);
3163 /* Two children: Identifier, statement list */
3164 astnode_add_child(n, id);
3165 astnode_add_child(n, stmts);
3166 /* Return the newly created node */
3170 /* Create the node */
3171 astnode *n = astnode_create(RECORD_DECL_NODE, loc);
3172 /* Two children: Identifier, field list */
3173 astnode_add_child(n, id);
3174 astnode_add_child(n, fields);
3175 /* Return the newly created node */
3179 /* Create the node */
3180 astnode *n = astnode_create(BITFIELD_DECL_NODE, loc);
3181 /* Two children: Identifier and width */
3182 astnode_add_child(n, id);
3183 astnode_add_child(n, width);
3184 /* Return the newly created node */
3188 /* Create the node */
3189 astnode *n = astnode_create(PUBLIC_NODE, loc);
3190 /* Add list of identifiers as child */
3191 astnode_add_child(n, l);
3192 /* Return the newly created node */
3196 /* Create the node */
3197 astnode *n = astnode_create(EXTRN_NODE, loc);
3198 /* Add type specifier as child */
3199 astnode_add_child(n, t);
3200 /* Add list of identifiers as child */
3201 astnode_add_child(n, astnode_create_list(l));
3202 /* Add from unit identifier */
3203 astnode_add_child(n, f);
3204 /* Return the newly created node */
3208 /* Create the node */
3209 astnode *n = astnode_create(DATASEG_NODE, loc);
3211 n->modifiers = modifiers;
3212 /* Return the newly created node */
3216 /* Create the node */
3217 astnode *n = astnode_create(CODESEG_NODE, loc);
3218 /* Return the newly created node */
3222 /* Create the node */
3223 astnode *n = astnode_create(DATA_NODE, loc);
3224 /* Add the type of data (enumerated or identifier) */
3225 astnode_add_child(n, type);
3226 /* Add list of values */
3227 astnode_add_child(n, data);
3228 /* Return the newly created node */
3232 /* Create the node */
3233 astnode *n = astnode_create(FILE_PATH_NODE, loc);
3234 /* Allocate and store text */
3235 n->file_path = (char *)malloc(strlen(path)+1);
3236 if (n->file_path != NULL) {
3237 strcpy(n->file_path, path);
3239 /* Return the newly created node */
3243 /* Create the node */
3244 astnode *n = astnode_create(LABEL_NODE, loc);
3245 /* Allocate and store text */
3246 n->label = (char *)malloc(strlen(s)+1);
3247 if (n->label != NULL) {
3248 strcpy(n->label, s);
3250 /* Two children: Datatype and address */
3252 addr = astnode_create_pc(loc);
3255 type = astnode_create_datatype(BYTE_DATATYPE, NULL, loc);
3257 astnode_add_child(n, type);
3258 astnode_add_child(n, addr);
3259 /* Return the newly created node */
3263 /* Create the node */
3264 astnode *n = astnode_create(LOCAL_LABEL_NODE, loc);
3265 /* Allocate and store text */
3266 n->label = (char *)malloc(strlen(s)+1);
3267 if (n->label != NULL) {
3268 strcpy(n->label, s);
3270 /* Return the newly created node */
3274 /* Create the node */
3275 astnode *n = astnode_create(LOCAL_ID_NODE, loc);
3276 /* Allocate and store text */
3277 n->ident = (char *)malloc(strlen(s)+1);
3278 if (n->ident != NULL) {
3279 strcpy(n->ident, s);
3281 /* Return the newly created node */
3287 /* Create the node */
3289 n = astnode_create(LIST_NODE, l->loc);
3290 /* Add list of values */
3291 astnode_add_child(n, l);
3294 /* Make a node with zero children */
3295 n = astnode_create(LIST_NODE, dummyloc);
3297 /* Return the newly created node (or NULL) */
3301 return astnode_create(CURRENT_PC_NODE, loc);
3304 /* Create the node */
3305 astnode *n = astnode_create(BINARY_NODE, loc);
3307 n->binary.data = bin;
3308 n->binary.size = size;
3309 /* Return the newly created node */
3313 /* Create the node */
3314 astnode *n = astnode_create(TOMBSTONE_NODE, loc);
3315 /* Store the type of the old node */
3316 n->param = (long)type;
3317 /* Return the newly created node */
3321 /* Create the node */
3322 astnode *n = astnode_create(DOT_NODE, loc);
3323 /* Two children: 'before' . 'after' */
3324 astnode_add_child(n, before);
3325 astnode_add_child(n, after);
3326 /* Return the newly created node */
3330 /* Create the node */
3331 astnode *n = astnode_create(SIZEOF_NODE, loc);
3332 /* One child: expression */
3333 astnode_add_child(n, expr);
3334 /* Return the newly created node */
3338 /* Create the node */
3339 astnode *n = astnode_create(DATATYPE_NODE, loc);
3340 /* Set the datatype */
3342 /* Possibly one child: identifier */
3344 astnode_add_child(n, id);
3346 /* Return the newly created node */
3350 /* Create the node */
3351 astnode *n = astnode_create(VAR_DECL_NODE, loc);
3353 n->modifiers = modifiers;
3354 /* Two children: Identifier and datatype+initializer */
3355 astnode_add_child(n, id);
3356 astnode_add_child(n, data);
3357 /* Return the newly created node */
3361 /* Create the node */
3362 astnode *n = astnode_create(SCOPE_NODE, loc);
3363 /* Two children: left and right */
3364 astnode_add_child(n, left);
3365 astnode_add_child(n, right);
3366 /* Return the newly created node */
3370 /* Create the node */
3371 astnode *n = astnode_create(PROC_NODE, loc);
3372 /* This node has two children:
3373 1) An identifier, which is the name of the procedure
3374 2) List of statements, which is the procedure body */
3375 astnode_add_children(
3379 astnode_create_list(stmts)
3381 /* Return the newly created node */
3385 /* Create the node */
3386 astnode *n = astnode_create(REPT_NODE, loc);
3387 /* This node has two children:
3388 1) An expression, which is the repeat count
3389 2) List of statements, which is the (anonymous) macro body */
3390 astnode_add_children(
3394 astnode_create_list(stmts)
3396 /* Return the newly created node */
3400 /* Create the node */
3401 astnode *n = astnode_create(WHILE_NODE, loc);
3402 /* This node has two children:
3403 1) A boolean expression
3404 2) List of statements, which is the (anonymous) macro body */
3405 astnode_add_children(
3409 astnode_create_list(stmts)
3411 /* Return the newly created node */
3415 /* Create the node */
3416 astnode *n = astnode_create(MESSAGE_NODE, loc);
3417 /* This node has one children: An expression, which is the message to print */
3418 astnode_add_child(n, expr);
3419 /* Return the newly created node */
3423 /* Create the node */
3424 astnode *n = astnode_create(WARNING_NODE, loc);
3425 /* This node has one child: A string, which is the warning to print */
3426 astnode_add_child(n, str);
3427 /* Return the newly created node */
3431 /* Create the node */
3432 astnode *n = astnode_create(ERROR_NODE, loc);
3433 /* This node has one child: A string, which is the error to print */
3434 astnode_add_child(n, str);
3435 /* Return the newly created node */
3439 /* Create the node */
3440 astnode *n = astnode_create(FORWARD_BRANCH_DECL_NODE, loc);
3441 /* Allocate and store text */
3442 n->ident = (char *)malloc(strlen(ident)+1);
3443 if (n->ident != NULL) {
3444 strcpy(n->ident, ident);
3446 /* Return the newly created node */
3450 /* Create the node */
3451 astnode *n = astnode_create(BACKWARD_BRANCH_DECL_NODE, loc);
3452 /* Allocate and store text */
3453 n->ident = (char *)malloc(strlen(ident)+1);
3454 if (n->ident != NULL) {
3455 strcpy(n->ident, ident);
3457 /* Return the newly created node */
3461 /* Create the node */
3462 astnode *n = astnode_create(FORWARD_BRANCH_NODE, loc);
3463 /* Allocate and store text */
3464 n->ident = (char *)malloc(strlen(ident)+1);
3465 if (n->ident != NULL) {
3466 strcpy(n->ident, ident);
3468 /* Return the newly created node */
3472 /* Create the node */
3473 astnode *n = astnode_create(BACKWARD_BRANCH_NODE, loc);
3474 /* Allocate and store text */
3475 n->ident = (char *)malloc(strlen(ident)+1);
3476 if (n->ident != NULL) {
3477 strcpy(n->ident, ident);
3479 /* Return the newly created node */
3483 /* Create the node */
3484 astnode *n = astnode_create(MASK_NODE, loc);
3485 /* One child: expression */
3486 astnode_add_child(n, expr);
3487 /* Return the newly created node */
3491 /* Create the node */
3492 astnode *n = astnode_create(ALIGN_NODE, loc);
3493 /* This node has two children: List of identifiers and alignment constraint */
3494 astnode_add_child(n, astnode_create_list(idents) );
3495 astnode_add_child(n, expr);
3496 /* Return the newly created node */
3500 /* Create the node */
3501 astnode *n = astnode_create(INDEX_NODE, loc);
3502 /* This node has two children: Identifier and expression */
3503 astnode_add_child(n, ident);
3504 astnode_add_child(n, expr);
3505 /* Return the newly created node */
3510 /* Node type: 1 byte */
3511 put_byte(f, astnode_get_type(n));
3512 /* Internal node data */
3513 switch (astnode_get_type(n)) {
3515 /* Assumes that sizeof(long) is same as for int */
3516 put_int(f, n->integer);
3520 case IDENTIFIER_NODE:
3522 case FILE_PATH_NODE:
3524 case LOCAL_LABEL_NODE:
3525 /* Put length first */
3526 put_int(f, strlen(n->string));
3527 /* Put characters */
3528 for (i=0; i<strlen(n->string); i++) {
3529 put_byte(f, n->string[i]);
3534 /* Datatype: 1 byte */
3535 put_byte(f, n->datatype);
3540 put_int(f, n->binary.size);
3541 /* Put data bytes */
3542 for (i=0; i<n->binary.size; i++) {
3543 put_byte(f, n->binary.data[i]);
3547 case ARITHMETIC_NODE:
3551 case INSTRUCTION_NODE:
3556 /* No internal attributes */
3560 put_int(f, astnode_get_child_count(n));
3561 /* Serialize children */
3562 for (i=0; i<astnode_get_child_count(n); i++) {
3563 astnode_serialize(astnode_get_child(n, i), f);
3572 /* Node type: 1 byte */
3573 t = (astnode_type)get_byte(f);
3574 /* Create the node */
3575 n = astnode_create(t, loc);
3576 /* Internal node data */
3577 switch (astnode_get_type(n)) {
3579 /* Assumes that sizeof(long) is same as for int */
3580 n->integer = get_int(f);
3584 case IDENTIFIER_NODE:
3586 case FILE_PATH_NODE:
3588 case LOCAL_LABEL_NODE:
3591 /* Create the character array */
3592 n->string = malloc(len+1);
3593 if (n->string != NULL) {
3594 /* Get characters */
3595 for (i=0; i<len; i++) {
3596 n->string[i] = get_byte(f);
3602 /* Datatype: 1 byte */
3603 n->datatype = get_byte(f);
3608 n->binary.size = get_int(f);
3609 /* Allocate storage */
3610 n->binary.data = (unsigned char *)malloc(n->binary.size);
3611 if (n->binary.data != NULL) {
3612 /* Get data bytes */
3613 for (i=0; i<n->param; i++) {
3614 n->binary.data[i] = get_byte(f);
3619 case ARITHMETIC_NODE:
3623 case INSTRUCTION_NODE:
3628 /* No internal attributes */
3633 /* Deserialize children */
3634 for (i=0; i<len; i++) {
3635 astnode_add_child(n, astnode_deserialize(f));
3639 /* Return the deserialized node */
3651 * $Id: astnode.c,v 1.8 2004/12/19 09:53:46 kenth Exp kenth $
3660 @added create_align()
3665 * $Id: astnode.c,v 1.7 2004/12/18 16:56:12 kenth Exp kenth $
3669 astnode *astnode_create_dataseg(location loc)
3674 astnode *astnode_create_label(char *s, astnode *type, location loc)
3677 /* One child: Datatype */
3682 astnode *astnode_create_var_decl(astnode *id, astnode *data, location loc)
3687 astnode *astnode_create_align(astnode *expr, location loc)
3690 /* This node has one child: An expression, which describes the alignment */
3696 @create_extrn() takes unit id
3701 * $Id: astnode.c,v 1.6 2004/12/16 13:19:07 kenth Exp kenth $
3709 @astnode_create_label() takes datatype argument
3714 * $Id: astnode.c,v 1.5 2004/12/14 01:48:57 kenth Exp kenth $
3719 astnode *astnode_create_extrn(astnode *l, astnode *t, location loc)
3722 astnode_add_child(n, l);
3733 * $Id: astnode.c,v 1.4 2004/12/11 02:01:10 kenth Exp kenth $
3738 astnode *astnode_create_label(char *s, location loc)
3745 @added forward/backward branching
3750 * $Id: astnode.c,v 1.3 2004/12/09 11:17:59 kenth Exp kenth $
3754 case ELIF_NODE: return "ELIF_NODE";
3760 * @@param elif List of elif nodes (may be <code>NULL</code>)
3761 * @@param els The final else-part (may be <code>NULL</code>)
3764 /* This node has four children:
3765 1) The expression that is being tested
3766 2) The list of statements to execute when true
3767 3) The list of lists of elif nodes
3768 4) The list of statements to execute when false
3769 2, 3, and 4 are wrapped in NODE_LISTs first so that this node has at
3772 astnode_add_children(
3776 astnode_create_list(then),
3777 astnode_create_list(elif),
3778 astnode_create_list(els)
3782 * Creates an elif node.
3783 * @@param expr Expression involved in elif
3784 * @@param then List of statement to assemble when expr is non-zero
3787 astnode *astnode_create_elif(astnode *expr, astnode *then, location loc)
3790 astnode *n = astnode_create(ELIF_NODE, loc);
3800 @added: warning, error nodes
3805 * $Id: astnode.c,v 1.2 2004/12/06 04:52:05 kenth Exp kenth $
3812 /* This node has one children: A string, which is the warning to print */
3815 /* This node has one children: A string, which is the error to print */
3822 @Major updates (xorcyst 1.1.0)
3827 * $Id: astnode.c,v 1.1 2004/06/30 07:55:28 kenth Exp kenth $
3844 #define LHS(e) astnode_get_child(e, 0)
3845 #define RHS(e) astnode_get_child(e, 1)
3849 const char *datatype_to_string(datatype dt)
3855 case QWORD_DATATYPE: return "QWORD_DATATYPE";
3861 datatype_to_string(n->datatype)
3868 /* Call remove_child on parent */
3869 i = astnode_remove_child(p, old_node);
3870 /* Insert new child at old child's position */
3871 astnode_insert_child(p, new_node, i);
3877 c->instr.mnemonic = n->instr.mnemonic;
3878 c->instr.mode = n->instr.mode;
3881 c->binary.size = n->binary.size;
3882 c->binary.data = n->binary.data;
3888 /* Clone children */
3889 for (i=0; i<astnode_get_child_count(n); i++) {
3890 astnode_add_child(c, astnode_clone(astnode_get_child(n, i), loc));
3892 case DATA_NODE: if (n1->datatype != n2->datatype) return 0; break;
3898 * @@param type Type of storage (*_DATATYPE)
3901 astnode *astnode_create_storage(int type, astnode *count, location loc)
3904 /* Store the type of data */
3906 /* One child: Count */
3911 astnode *astnode_create_extrn(astnode *l, location loc)
3915 astnode *astnode_create_data(int type, astnode *data, location loc)
3918 /* Store the type of data */