show PC of labels
[xorcyst.git] / RCS / astnode.c,v
blobd4ec57fa8eb43a215ed360270e8e7d83b5f2ecd5
1 head    1.15;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
8 1.15
9 date    2007.08.12.18.58.12;    author khansen; state Exp;
10 branches;
11 next    1.14;
13 1.14
14 date    2007.08.10.20.21.02;    author khansen; state Exp;
15 branches;
16 next    1.13;
18 1.13
19 date    2007.08.09.22.05.49;    author khansen; state Exp;
20 branches;
21 next    1.12;
23 1.12
24 date    2007.08.07.21.12.16;    author khansen; state Exp;
25 branches;
26 next    1.11;
28 1.11
29 date    2007.07.22.13.33.26;    author khansen; state Exp;
30 branches;
31 next    1.10;
33 1.10
34 date    2004.12.29.21.44.04;    author kenth;   state Exp;
35 branches;
36 next    1.9;
38 1.9
39 date    2004.12.19.19.58.23;    author kenth;   state Exp;
40 branches;
41 next    1.8;
43 1.8
44 date    2004.12.19.09.53.46;    author kenth;   state Exp;
45 branches;
46 next    1.7;
48 1.7
49 date    2004.12.18.16.56.12;    author kenth;   state Exp;
50 branches;
51 next    1.6;
53 1.6
54 date    2004.12.16.13.19.07;    author kenth;   state Exp;
55 branches;
56 next    1.5;
58 1.5
59 date    2004.12.14.01.48.57;    author kenth;   state Exp;
60 branches;
61 next    1.4;
63 1.4
64 date    2004.12.11.02.01.10;    author kenth;   state Exp;
65 branches;
66 next    1.3;
68 1.3
69 date    2004.12.09.11.17.59;    author kenth;   state Exp;
70 branches;
71 next    1.2;
73 1.2
74 date    2004.12.06.04.52.05;    author kenth;   state Exp;
75 branches;
76 next    1.1;
78 1.1
79 date    2004.06.30.07.55.28;    author kenth;   state Exp;
80 branches;
81 next    ;
84 desc
88 1.15
89 log
90 @ability to generate pure 6502 binary (--pure-binary switch)
92 text
93 @/*
94  * $Id: astnode.c,v 1.14 2007/08/10 20:21:02 khansen Exp khansen $
95  * $Log: astnode.c,v $
96  * Revision 1.14  2007/08/10 20:21:02  khansen
97  * *** empty log message ***
98  *
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
103  * const
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
109  * xorcyst 1.4.2
110  * added create_index()
112  * Revision 1.9  2004/12/19 19:58:23  kenth
113  * xorcyst 1.4.0
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
125  * xorcyst 1.3.0
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
137  * Initial revision
139  */
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
157  */
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.
163  */
165 #include <stdio.h>
166 #include <stdlib.h>
167 #include <stdarg.h>
168 #include <string.h>
169 #include "astnode.h"
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
182  */
183 const char *addressing_mode_to_string(addressing_mode am)
185     switch (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";
199     }
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
207  */
208 const char *instr_mnemonic_to_string(instr_mnemonic im)
210     switch (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";
267     }
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
275  */
276 const char *astnode_type_to_string(astnode_type at) {
277     switch (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";
335     }
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
343  */
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;
352     }
353     return "datatype_to_string: invalid datatype";
357  * Gets string representation of an operator.
358  * @@param op Operator
359  * @@return String representation of op
360  */
361 const char *operator_to_string(int op)
363     switch (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";
386     }
387     return "operator_to_string: invalid operator";
391  * Indents.
392  * @@param nlevels Levels
393  */
394 void indent(int nlevels)
396     int i;
397     for (i=0; i<nlevels; i++) {
398         printf("  ");
399     }
403  * Prints a node recursively.
404  * @@param n Node to print
405  * @@param level Level (depth)
406  */
407 void astnode_print(const astnode *n, int level)
409     int i;
410     /* Indent so it looks pretty */
411     indent(level);
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:
426         printf(
427             "(%s)",
428             operator_to_string(n->oper)
429         );
430         break;
432         case INSTRUCTION_NODE:
433         printf(
434             "(%s,%s,%.2X)",
435             instr_mnemonic_to_string(n->instr.mnemonic),
436             addressing_mode_to_string(n->instr.mode),
437             n->instr.opcode
438         );
439         break;
441         case DATATYPE_NODE:
442         printf(
443             "(%s)",
444             datatype_to_string(n)
445         );
446         break;
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);
453         break;
455         case TOMBSTONE_NODE:
456         printf(
457             "(%s)",
458             astnode_type_to_string(n->param)
459         );
460         break;
462         default:
463         /* Has no internal attributes */
464         break;
465     }
466     printf("\n");
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);
470     }
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
483  */
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();
490     }
491     /* Allocate memory for node struct */
492     astnode *n = (astnode *)malloc(sizeof(astnode));
493     /* Fill in struct only if alloc succeeded */
494     if (n != NULL) {
495         n->type = type;
496         n->loc = loc;
497         n->flags = 0;
498         n->label = NULL;
499         n->string = NULL;
500         n->parent = n->first_child = n->prev_sibling = n->next_sibling = NULL;
501     }
502     return n;
506  * Finalizes a node.
507  * Any children of the node are also finalized, recursively.
508  * @@param n The node to finalize.
509  */
510 void astnode_finalize(astnode *n)
512     /* Remove the node from the tree it's in. */
513     astnode_remove(n);
514     /* Finalize all its children recursively. */
515     while (astnode_get_first_child(n) != NULL) {
516         astnode_finalize(astnode_remove_child_at(n, 0));
517     }
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:
531         SAFE_FREE(n->ident);
532         break;
533         default:
534         /* Has no internal attributes that are dynamically allocated */
535         break;
536     }
537     SAFE_FREE(n);
541  * Gets the node's type.
542  * @@param n The node whose type to get
543  * @@return The node's type (astnode_type)
544  */
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.
552  */
553 void astnode_set_parent(astnode *c, astnode *p)
555     astnode *n;
556     for (n = c; n != NULL; n = n->next_sibling) {
557         n->parent = p;
558     }
562  * Replaces a node with another.
563  */
564 void astnode_replace(astnode *old_node, astnode *new_node)
566     astnode *p;
567     int i;
568     /* Get the parent of the node to be replaced */
569     p = astnode_get_parent(old_node);
570     if (p != NULL) {
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);
575     }
579  * Removes a node from a tree.
580  * @@param n The node to remove (can't be the root of the tree)
581  */
582 void astnode_remove(astnode *n)
584     astnode *p = astnode_get_parent(n);
585     if (n && p) {
586         astnode_remove_child(p, n);
587     }
591  * Removes a child node.
592  * @@param p Parent node
593  * @@param c Child node
594  * @@return Index of the removed node
595  */
596 int astnode_remove_child(astnode *p, astnode *c)
598     int i;
599     i = astnode_get_child_index(p, c);
600     if (i == 0) {
601         /* Remove head of list. */
602         p->first_child = c->next_sibling;
603         if (p->first_child) {
604             p->first_child->prev_sibling = NULL;
605         }
606         c->parent = c->next_sibling = c->prev_sibling = NULL;
607     }
608     else if (i > 0) {
609         c->prev_sibling->next_sibling = c->next_sibling;
610         if (c->next_sibling) {
611             c->next_sibling->prev_sibling = c->prev_sibling;
612         }
613         c->parent = c->next_sibling = c->prev_sibling = NULL;
614     }
615     return i;
619  * Removes child node at specified index.
620  * @@param p Parent node
621  * @@param i Index >= 0
622  */
623 astnode *astnode_remove_child_at(astnode *p, int i)
625     astnode *c = astnode_get_child(p, i);
626     astnode_remove_child(p, c);
627     return c;
631  * Removes all children from a node and returns them as a list.
632  * @@param p Parent node whose children to remove
633  */
634 astnode *astnode_remove_children(astnode *p)
636     astnode *c;
637     if (p == NULL) { return NULL; }
638     if (p->first_child != NULL) {
639         c = p->first_child;
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 */
644         return c;
645     }
646     else {
647         /* Has no children. */
648         return NULL;
649     }
653  * Inserts a list of nodes as children.
655  */
656 void astnode_insert_child(astnode *p, astnode *c, int i)
658     astnode *n;
659     astnode *x;
660     if (p && c) {
661         x = astnode_get_child(p, i);    /* Current child at that position */
662         if (x == NULL) {
663             /* There isn't a node here. Just add to end. */
664             astnode_add_child(p, c);
665         }
666         else {
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;
672             }
673             n->next_sibling = x;
674             x->prev_sibling = n;
675         }
676         /* Set parent */
677         astnode_set_parent(c, p);
678         /* Check if head */
679         if (i == 0) {
680             p->first_child = c;
681         }
682     }
686  * Gets the last node in a list.
687  */
688 astnode *astnode_get_last_sibling(const astnode *n)
690     astnode *s = NULL;
691     if (n) {
692         for (s = (astnode *)n; s->next_sibling != NULL; s = s->next_sibling) ;
693     }
694     return s;
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
701  */
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
711  */
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);
719         }
720         else {
721             astnode_add_sibling(n->first_child, new_child);
722         }
723     }
727  * Adds any number of children to a node.
728  * @@param n The parent-to-be
729  */
730 void astnode_add_children(astnode *n, int count, ...)
732     int i;
733     va_list ap;
734     astnode *c;
736     va_start(ap, count);
737     for (i=0; i<count; i++) {
738         c = va_arg(ap, astnode*);
739         astnode_add_child(n, c);
740     }
741     va_end(ap);
745  * Adds sibling(s) to a node.
746  * @@param brother List of existing siblings
747  * @@param sister List of new siblings
748  */
749 void astnode_add_sibling(astnode *brother, astnode *sister)
751     astnode *n;
752     astnode *p;
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);
760     }
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
767  */
768 astnode *astnode_get_child(const astnode *n, int index)
770     int i;
771     astnode *c;
772     if (n) {
773         c = n->first_child;
774         for (i = 0; i != index; i++) {
775             if (c == NULL) {
776                 /* No child at that index. */
777                 break;
778             }
779             c = c->next_sibling;
780         }
781         return c;
782     }
783     /* Node is NULL, so return NULL */
784     return NULL;
788  * Gets a node's first child.
789  * @@param n The node
790  */
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
801  */
802 int astnode_get_child_index(const astnode *p, const astnode *c)
804     int i;
805     astnode *n;
806     if (p && c) {
807         for (i=0, n=p->first_child; (n != c) && (n != NULL); i++, n=n->next_sibling);
808         return n ? i : -1;
809     }
810     return -1;
814  * Gets the number of children a node has.
815  * @@param p Node whose children count to get
816  */
817 int astnode_get_child_count(const astnode *p)
819     astnode *c;
820     int count = 0;
821     if (p != NULL) {
822         for (c = p->first_child; c != NULL; count++, c = c->next_sibling);
823     }
824     return count;
828  * Clones a node and all its children.
829  * @@param n The node to clone
830  * @@param loc File location
831  */
832 astnode *astnode_clone(const astnode *n, location loc)
834     astnode *c;
835     astnode *n_c;
836     if (n == NULL) { return NULL; }
837     /* Create node */
838     c = astnode_create(astnode_get_type(n), loc);
839     /* Copy attributes */
840     switch (astnode_get_type(n)) {
841         case INTEGER_NODE:
842         c->integer = n->integer;
843         break;
845         case STRING_NODE:
846         case IDENTIFIER_NODE:
847         case FILE_PATH_NODE:
848         case LABEL_NODE:
849         case LOCAL_LABEL_NODE:
850         case LOCAL_ID_NODE:
851         c->string = (char *)malloc(strlen(n->string)+1);
852         if (c->string != NULL) {
853             strcpy(c->string, n->string);
854         }
855         break;
857         case ARITHMETIC_NODE:
858         c->oper = n->oper;
859         break;
861         case INSTRUCTION_NODE:
862         c->instr = n->instr;
863         break;
865         case BINARY_NODE:
866         c->binary = n->binary;
867         break;
869         case DATATYPE_NODE:
870         c->datatype = n->datatype;
871         break;
873         default:
874         c->param = n->param;
875     }
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));
879     }
880     /* Return the clone */
881     return c;
885  * Tests if two nodes are equal.
886  */
887 int astnode_equal(const astnode *n1, const astnode *n2)
889     int i;
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 */
893     }
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;
908         default:
909         /* Has no internal attributes */
910         break;
911     }
912     /* Verify that they have the same number of children */
913     if (astnode_get_child_count(n1) != astnode_get_child_count(n2)) {
914         return 0;
915     }
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))) {
919             return 0;
920         }
921     }
922     /* Equal. */
923     return 1;
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.)
930  */
931 astnode *astnode_get_ancestor(const astnode *n, int back)
933     int i;
934     astnode *a = astnode_get_parent(n);
935     for (i=0; i<back; i++) {
936         a = astnode_get_parent(a);
937     }
938     return a;
942  * Tests if a node is a descendant of a node of a particular type.
943  * @@param n Node
944  * @@param type Ancestor's type
945  * @@return 0 if no such ancestor, 1 otherwise
946  */
947 int astnode_has_ancestor_of_type(const astnode *n, astnode_type type)
949     astnode *a;
950     for (a = astnode_get_parent(n); a != NULL; a = astnode_get_parent(a) ) {
951         if (astnode_is_type(a, type)) {
952             return 1;
953         }
954     }
955     return 0;
959  * Gets the next sibling of a node.
960  * @@param n Node
961  */
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.
970  * @@param n Node
971  */
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
981  */
982 int astnode_is_literal(const astnode *n)
984     switch (astnode_get_type(n)) {
985         case INTEGER_NODE:
986         case STRING_NODE:
987         /* A literal */
988         return 1;
990         default:
991         /* Not a literal */
992         break;
993     }
994     /* Not a literal */
995     return 0;
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 */
1010     return n;
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
1019  */
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 */
1030     return n;
1034  * Creates an identifier node.
1035  * @@param ident The identifier (a string)
1036  * @@param loc File location
1037  */
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);
1046     }
1047     /* Return the newly created node */
1048     return n;
1052  * Creates an integer literal node.
1053  * @@param value The integer literal
1054  * @@param loc File location
1055  */
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 */
1061     n->integer = value;
1062     /* Return the newly created node */
1063     return n;
1067  * Creates a string literal node.
1068  * @@param value The string literal
1069  * @@param loc File location
1070  */
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);
1079     }
1080     /* Return the newly created node */
1081     return n;
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
1090  */
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 */
1096     n->oper = oper;
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 */
1101     return n;
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
1111  */
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);
1119     if (els != NULL) {
1120         astnode_add_child(n, astnode_create_default(els, loc));
1121     }
1122     /* Return the newly created node */
1123     return n;
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
1131  */
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(
1138         n,
1139         2,
1140         expr,
1141         astnode_create_list(then)
1142     );
1143     /* Return the newly created node */
1144     return n;
1148  * Creates a DEFAULT node.
1149  * @@param stmts List of statements
1150  * @@param loc File location
1151  */
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. */
1157     astnode_add_child(
1158         n,
1159         stmts
1160     );
1161     /* Return the newly created node */
1162     return n;
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
1171  */
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(
1178         n,
1179         3,
1180         ident,
1181         astnode_create_list(then),
1182         astnode_create_list(els)
1183     );
1184     /* Return the newly created node */
1185     return n;
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
1194  */
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(
1201         n,
1202         3,
1203         ident,
1204         astnode_create_list(then),
1205         astnode_create_list(els)
1206     );
1207     /* Return the newly created node */
1208     return n;
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
1217  */
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(
1227         n,
1228         3,
1229         ident,
1230         astnode_create_list(params),
1231         astnode_create_list(body)
1232     );
1233     /* Return the newly created node */
1234     return n;
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
1242  */
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(
1249         n,
1250         2,
1251         ident,
1252         astnode_create_list(args)
1253     );
1254     /* Return the newly created node */
1255     return n;
1259  * Creates an equ node.
1260  * @@param ident Identifier
1261  * @@param expr Expression
1262  * @@param loc File location
1263  */
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 */
1271     return n;
1275  * Creates an assign node.
1276  * @@param ident Identifier
1277  * @@param expr Expression
1278  * @@param loc File location
1279  */
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 */
1287     return n;
1291  * Creates a storage node.
1292  * @@param type Type of storage
1293  * @@param count Expression with contains count
1294  * @@param loc File location
1295  */
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);
1306     }
1307     astnode_add_child(n, count);
1308     /* Return the newly created node */
1309     return n;
1313  * Creates an incsrc node.
1314  * @@param file File specifier
1315  * @@param loc File location
1316  */
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 */
1324     return n;
1328  * Creates an incbin node.
1329  * @@param file File specifier
1330  * @@param loc File location
1331  */
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 */
1339     return n;
1343  * Creates a charmap node.
1344  * @@param file File specifier
1345  * @@param loc File location
1346  */
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 */
1354     return n;
1358  * Creates a structure (STRUC) instance node.
1359  * @@param vals Values for the structure fields
1360  * @@param loc File location
1361  */
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 */
1369     return n;
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
1376  */
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 */
1385     return n;
1389  * Creates a union declaration node.
1390  * @@param id Union identifier
1391  * @@param stmts Statements of the union declaration
1392  * @@param loc File location
1393  */
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 */
1402     return n;
1406  * Creates an enum declaration node.
1407  * @@param id Enum identifier
1408  * @@param stmts Statements of the enum declaration
1409  * @@param loc File location
1410  */
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 */
1419     return n;
1423  * Creates a record declaration node.
1424  * @@param id Record identifier
1425  * @@param fields Fields of the record
1426  * @@param loc File location
1427  */
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 */
1436     return n;
1440  * Creates a bitfield declaration node.
1441  * @@param id Identifier
1442  * @@param width Width of field
1443  * @@param loc Location
1444  */
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 */
1453     return n;
1457  * Creates a public node.
1458  */
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 */
1466     return n;
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>)
1474  */
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 */
1486     return n;
1490  * Creates a dataseg node.
1491  */
1492 astnode *astnode_create_dataseg(int modifiers, location loc)
1494     /* Create the node */
1495     astnode *n = astnode_create(DATASEG_NODE, loc);
1496     /* Set modifiers */
1497     n->modifiers = modifiers;
1498     /* Return the newly created node */
1499     return n;
1503  * Creates a codeseg node.
1504  */
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 */
1510     return n;
1514  * Creates a data node.
1515  * @@param type Type specifier
1516  * @@param data List of values
1517  * @@param loc File location
1518  */
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 */
1528     return n;
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
1538  */
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);
1547     }
1548     /* Return the newly created node */
1549     return n;
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
1558  */
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);
1567     }
1568     /* Two children: Datatype and address */
1569     if (addr == NULL) {
1570         addr = astnode_create_pc(loc);
1571     }
1572     if (type == NULL) {
1573         type = astnode_create_datatype(BYTE_DATATYPE, NULL, loc);
1574     }
1575     astnode_add_child(n, type);
1576     astnode_add_child(n, addr);
1577     /* Return the newly created node */
1578     return n;
1582  * Creates a local label node.
1583  * @@param s Name of label
1584  * @@param loc Location
1585  */
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);
1594     }
1595     /* Return the newly created node */
1596     return n;
1600  * Creates a local identifier node.
1601  * @@param s Identifier
1602  * @@param loc Location
1603  */
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);
1612     }
1613     /* Return the newly created node */
1614     return n;
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
1621  */
1622 astnode *astnode_create_list(astnode *l)
1624     astnode *n;
1625     location dummyloc;
1626     /* Create the node */
1627     if (l != NULL) {
1628         n = astnode_create(LIST_NODE, l->loc);
1629         /* Add list of values */
1630         astnode_add_child(n, l);
1631     }
1632     else {
1633         /* Make a node with zero children */
1634         n = astnode_create(LIST_NODE, dummyloc);
1635     }
1636     /* Return the newly created node (or NULL) */
1637     return n;
1641  * Creates a PC node.
1642  * @@param loc File location
1643  */
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
1654  */
1655 astnode *astnode_create_binary(unsigned char *bin, int size, location loc)
1657     /* Create the node */
1658     astnode *n = astnode_create(BINARY_NODE, loc);
1659     /* Set data */
1660     n->binary.data = bin;
1661     n->binary.size = size;
1662     /* Return the newly created node */
1663     return n;
1667  * Creates a tombstone node, which is a marker node that says that another node
1668  * once lived here.
1669  * @@param type The type of node that used to live here
1670  * @@param loc File location
1671  */
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 */
1679     return n;
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)
1687  */
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 */
1696     return n;
1700  * Creates a sizeof operator node.
1701  * @@param expr Expression (datatype?)
1702  * @@param loc Location
1703  */
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 */
1711     return n;
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
1719  */
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 */
1725     n->datatype = t;
1726     /* Possibly one child: identifier */
1727     if (id != NULL) {
1728         astnode_add_child(n, id);
1729     }
1730     /* Return the newly created node */
1731     return n;
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
1740  */
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);
1745     /* Set modifiers */
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 */
1751     return n;
1756  */
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 */
1765     return n;
1769  * Creates a procedure (PROC) node.
1770  * @@param ident Name of procedure
1771  * @@param stmts Procedure statements
1772  * @@param loc File location
1773  */
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(
1782         n,
1783         2,
1784         ident,
1785         astnode_create_list(stmts)
1786     );
1787     /* Return the newly created node */
1788     return n;
1792  * Creates a REPT node.
1793  * @@param expr Number of times to repeat statements
1794  * @@param stmts Statement list
1795  * @@param loc File location
1796  */
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(
1805         n,
1806         2,
1807         expr,
1808         astnode_create_list(stmts)
1809     );
1810     /* Return the newly created node */
1811     return n;
1815  * Creates a WHILE node.
1816  * @@param expr Boolean expression
1817  * @@param stmts Statement list
1818  * @@param loc File location
1819  */
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(
1828         n,
1829         2,
1830         expr,
1831         astnode_create_list(stmts)
1832     );
1833     /* Return the newly created node */
1834     return n;
1838  * Creates a MESSAGE node.
1839  * @@param expr Message to print.
1840  * @@param loc File location
1841  */
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 */
1849     return n;
1853  * Creates a WARNING node.
1854  * @@param str Warning to print.
1855  * @@param loc File location
1856  */
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 */
1864     return n;
1868  * Creates an ERROR node.
1869  * @@param str Error to print.
1870  * @@param loc File location
1871  */
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 */
1879     return n;
1883  * Creates a forward branch declaration node.
1884  * @@param ident Branch name
1885  * @@param loc File location
1886  */
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);
1895     }
1896     /* Return the newly created node */
1897     return n;
1901  * Creates a backward branch declaration node.
1902  * @@param ident Branch name
1903  * @@param loc File location
1904  */
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);
1913     }
1914     /* Return the newly created node */
1915     return n;
1919  * Creates a forward branch reference node.
1920  * @@param ident Branch name
1921  * @@param loc File location
1922  */
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);
1931     }
1932     /* Return the newly created node */
1933     return n;
1937  * Creates a backward branch reference node.
1938  * @@param ident Branch name
1939  * @@param loc File location
1940  */
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);
1949     }
1950     /* Return the newly created node */
1951     return n;
1955  * Creates a mask operator node.
1956  * @@param expr Expression
1957  * @@param loc Location
1958  */
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 */
1966     return n;
1970  * Creates an ALIGN node.
1971  * @@param idents List of identifiers
1972  * @@param expr Expression
1973  * @@param loc File location
1974  */
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 */
1983     return n;
1987  * Creates an INDEX node.
1988  * @@param ident Identifier being indexed
1989  * @@param expr Index expression
1990  * @@param loc File location
1991  */
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 */
2000     return n;
2004  * Creates an ORG node.
2005  * @@param addr Address
2006  * @@param loc File location
2007  */
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 */
2015     return n;
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
2031  */
2032 void astnode_serialize(const astnode *n, FILE *f)
2034     int i;
2035     /* Node type: 1 byte */
2036     put_byte(f, astnode_get_type(n));
2037     /* Internal node data */
2038     switch (astnode_get_type(n)) {
2039         case INTEGER_NODE:
2040         /* Assumes that sizeof(long) is same as for int */
2041         put_int(f, n->integer);
2042         break;
2044         case STRING_NODE:
2045         case IDENTIFIER_NODE:
2046         case LOCAL_ID_NODE:
2047         case FILE_PATH_NODE:
2048         case LABEL_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]);
2055         }
2056         break;
2058         case DATA_NODE:
2059         /* Datatype: 1 byte */
2060         put_byte(f, n->datatype);
2061         break;
2063         case BINARY_NODE:
2064         /* Size: 4 bytes */
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]);
2069         }
2070         break;
2072         case ARITHMETIC_NODE:
2073         // TODO
2074         break;
2076         case INSTRUCTION_NODE:
2077         // TODO
2078         break;
2080         default:
2081         /* No internal attributes */
2082         break;
2083     }
2084     /* Child count */
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);
2089     }
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
2099  */
2100 astnode *astnode_deserialize(FILE *f)
2102     int i;
2103     int len;
2104     astnode *n;
2105     astnode_type t;
2106     location loc;
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)) {
2113         case INTEGER_NODE:
2114         /* Assumes that sizeof(long) is same as for int */
2115         n->integer = get_int(f);
2116         break;
2118         case STRING_NODE:
2119         case IDENTIFIER_NODE:
2120         case LOCAL_ID_NODE:
2121         case FILE_PATH_NODE:
2122         case LABEL_NODE:
2123         case LOCAL_LABEL_NODE:
2124         /* Get length */
2125         len = get_int(f);
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);
2132             }
2133         }
2134         break;
2136         case DATA_NODE:
2137         /* Datatype: 1 byte */
2138         n->datatype = get_byte(f);
2139         break;
2141         case BINARY_NODE:
2142         /* Size: 4 bytes */
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);
2150             }
2151         }
2152         break;
2154         case ARITHMETIC_NODE:
2155         // TODO
2156         break;
2158         case INSTRUCTION_NODE:
2159         // TODO
2160         break;
2162         default:
2163         /* No internal attributes */
2164         break;
2165     }
2166     /* Child count */
2167     len = get_int(f);
2168     /* Deserialize children */
2169     for (i=0; i<len; i++) {
2170         astnode_add_child(n, astnode_deserialize(f));
2171     }
2173     /* Return the deserialized node */
2174     return n;
2179 1.14
2181 @*** empty log message ***
2183 text
2184 @d2 1
2185 a2 1
2186  * $Id: astnode.c,v 1.13 2007/08/09 22:05:49 khansen Exp khansen $
2187 d4 3
2188 d241 1
2189 d1911 15
2193 1.13
2195 @general-purpose flags
2197 text
2198 @d2 1
2199 a2 1
2200  * $Id: astnode.c,v 1.12 2007/08/07 21:12:16 khansen Exp khansen $
2201 d4 3
2202 d391 1
2203 a391 1
2204     extern char *yy_current_filename();
2208 1.12
2210 @const
2212 text
2213 @d2 1
2214 a2 1
2215  * $Id: astnode.c,v 1.11 2007/07/22 13:33:26 khansen Exp khansen $
2216 d4 3
2217 d398 1
2221 1.11
2223 @convert tabs to whitespaces
2225 text
2226 @d2 1
2227 a2 1
2228  * $Id: astnode.c,v 1.10 2004/12/29 21:44:04 kenth Exp $
2229 d4 3
2230 d242 1
2231 a242 1
2232 const char *datatype_to_string(astnode *dt)
2233 d305 1
2234 a305 1
2235 void astnode_print(astnode *n, int level)
2236 d352 1
2237 a352 1
2238         
2239 d442 1
2240 a442 1
2241 astnode_type astnode_get_type(astnode *n)
2242 d585 1
2243 a585 1
2244 astnode *astnode_get_last_sibling(astnode *n)
2245 d589 1
2246 a589 1
2247         for (s = n; s->next_sibling != NULL; s = s->next_sibling);
2248 d599 1
2249 a599 1
2250 astnode *astnode_get_parent(astnode *n)
2251 d665 1
2252 a665 1
2253 astnode *astnode_get_child(astnode *n, int index)
2254 d688 1
2255 a688 1
2256 astnode *astnode_get_first_child(astnode *n)
2257 d699 1
2258 a699 1
2259 int astnode_get_child_index(astnode *p, astnode *c)
2260 d714 1
2261 a714 1
2262 int astnode_get_child_count(astnode *p)
2263 d729 1
2264 a729 1
2265 astnode *astnode_clone(astnode *n, location loc)
2266 d784 1
2267 a784 1
2268 int astnode_equal(astnode *n1, astnode *n2)
2269 d828 1
2270 a828 1
2271 astnode *astnode_get_ancestor(astnode *n, int back)
2272 d844 1
2273 a844 1
2274 int astnode_has_ancestor_of_type(astnode *n, astnode_type type)
2275 d859 1
2276 a859 1
2277 astnode *astnode_get_next_sibling(astnode *n)
2278 d869 1
2279 a869 1
2280 astnode *astnode_get_prev_sibling(astnode *n)
2281 d879 1
2282 a879 1
2283 int astnode_is_literal(astnode *n)
2284 d935 1
2285 a935 1
2286 astnode *astnode_create_identifier(char *ident, location loc)
2287 d968 1
2288 a968 1
2289 astnode *astnode_create_string(char *value, location loc)
2290 d1436 1
2291 a1436 1
2292 astnode *astnode_create_file_path(char *path, location loc)
2293 d1456 1
2294 a1456 1
2295 astnode *astnode_create_label(char *s, astnode *addr, astnode *type, location loc)
2296 d1483 1
2297 a1483 1
2298 astnode *astnode_create_local_label(char *s, location loc)
2299 d1501 1
2300 a1501 1
2301 astnode *astnode_create_local_id(char *s, location loc)
2302 d1784 1
2303 a1784 1
2304 astnode *astnode_create_forward_branch_decl(char *ident, location loc)
2305 d1802 1
2306 a1802 1
2307 astnode *astnode_create_backward_branch_decl(char *ident, location loc)
2308 d1820 1
2309 a1820 1
2310 astnode *astnode_create_forward_branch(char *ident, location loc)
2311 d1838 1
2312 a1838 1
2313 astnode *astnode_create_backward_branch(char *ident, location loc)
2314 d1914 1
2315 a1914 1
2316 void astnode_serialize(astnode *n, FILE *f)
2320 1.10
2322 @xorcyst 1.4.2
2323 added create_index()
2325 text
2326 @d2 1
2327 a2 1
2328  * $Id: astnode.c,v 1.9 2004/12/19 19:58:23 kenth Exp kenth $
2329 d4 4
2330 d81 16
2331 a96 16
2332         switch (am) {
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";
2346         }
2347         return "addressing_mode_to_string: invalid addressing mode";
2348 d106 59
2349 a164 59
2350         switch (im) {
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";
2407         }
2408         return "instr_mnemonic_to_string: invalid mnemonic";
2409 d173 59
2410 a231 59
2411         switch (at) {
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";
2468         }
2469         return "astnode_type_to_string: invalid type";
2470 d241 8
2471 a248 8
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;
2478         }
2479         return "datatype_to_string: invalid datatype";
2480 d258 25
2481 a282 25
2482         switch (op) {
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";
2505         }
2506         return "operator_to_string: invalid operator";
2507 d291 4
2508 a294 4
2509         int i;
2510         for (i=0; i<nlevels; i++) {
2511                 printf("  ");
2512         }
2513 d304 62
2514 a365 62
2515         int i;
2516         /* Indent so it looks pretty */
2517         indent(level);
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:
2532                 printf(
2533                         "(%s)",
2534                         operator_to_string(n->oper)
2535                 );
2536                 break;
2538                 case INSTRUCTION_NODE:
2539                 printf(
2540                         "(%s,%s,%.2X)",
2541                         instr_mnemonic_to_string(n->instr.mnemonic),
2542                         addressing_mode_to_string(n->instr.mode),
2543                         n->instr.opcode
2544                 );
2545                 break;
2547                 case DATATYPE_NODE:
2548                 printf(
2549                         "(%s)",
2550                         datatype_to_string(n)
2551                 );
2552                 break;
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);
2559                 break;
2560                 
2561                 case TOMBSTONE_NODE:
2562                 printf(
2563                         "(%s)",
2564                         astnode_type_to_string(n->param)
2565                 );
2566                 break;
2568                 default:
2569                 /* Has no internal attributes */
2570                 break;
2571         }
2572         printf("\n");
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);
2576         }
2577 d381 16
2578 a396 16
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();
2583         }
2584         /* Allocate memory for node struct */
2585         astnode *n = (astnode *)malloc(sizeof(astnode));
2586         /* Fill in struct only if alloc succeeded */
2587         if (n != NULL) {
2588                 n->type = type;
2589                 n->loc = loc;
2590                 n->label = NULL;
2591                 n->string = NULL;
2592                 n->parent = n->first_child = n->prev_sibling = n->next_sibling = NULL;
2593         }
2594         return n;
2595 d406 26
2596 a431 26
2597         /* Remove the node from the tree it's in. */
2598         astnode_remove(n);
2599         /* Finalize all its children recursively. */
2600         while (astnode_get_first_child(n) != NULL) {
2601                 astnode_finalize(astnode_remove_child_at(n, 0));
2602         }
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);
2617                 break;
2618                 default:
2619                 /* Has no internal attributes that are dynamically allocated */
2620                 break;
2621         }
2622         SAFE_FREE(n);
2623 d441 1
2624 a441 1
2625         return (n != NULL) ? n->type : NULL_NODE;
2626 d449 4
2627 a452 4
2628         astnode *n;
2629         for (n = c; n != NULL; n = n->next_sibling) {
2630                 n->parent = p;
2631         }
2632 d460 10
2633 a469 10
2634         astnode *p;
2635         int i;
2636         /* Get the parent of the node to be replaced */
2637         p = astnode_get_parent(old_node);
2638         if (p != NULL) {
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);
2643         }
2644 d478 4
2645 a481 4
2646         astnode *p = astnode_get_parent(n);
2647         if (n && p) {
2648                 astnode_remove_child(p, n);
2649         }
2650 d492 18
2651 a509 18
2652         int i;
2653         i = astnode_get_child_index(p, c);
2654         if (i == 0) {
2655                 /* Remove head of list. */
2656                 p->first_child = c->next_sibling;
2657                 if (p->first_child) {
2658                         p->first_child->prev_sibling = NULL;
2659                 }
2660                 c->parent = c->next_sibling = c->prev_sibling = NULL;
2661         }
2662         else if (i > 0) {
2663                 c->prev_sibling->next_sibling = c->next_sibling;
2664                 if (c->next_sibling) {
2665                         c->next_sibling->prev_sibling = c->prev_sibling;
2666                 }
2667                 c->parent = c->next_sibling = c->prev_sibling = NULL;
2668         }
2669         return i;
2670 d519 3
2671 a521 3
2672         astnode *c = astnode_get_child(p, i);
2673         astnode_remove_child(p, c);
2674         return c;
2675 d530 14
2676 a543 14
2677         astnode *c;
2678         if (p == NULL) { return NULL; }
2679         if (p->first_child != NULL) {
2680                 c = p->first_child;
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 */
2685                 return c;
2686         }
2687         else {
2688                 /* Has no children. */
2689                 return NULL;
2690         }
2691 d552 25
2692 a576 25
2693         astnode *n;
2694         astnode *x;
2695         if (p && c) {
2696                 x = astnode_get_child(p, i);    /* Current child at that position */
2697                 if (x == NULL) {
2698                         /* There isn't a node here. Just add to end. */
2699                         astnode_add_child(p, c);
2700                 }
2701                 else {
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;
2707                         }
2708                         n->next_sibling = x;
2709                         x->prev_sibling = n;
2710                 }
2711                 /* Set parent */
2712                 astnode_set_parent(c, p);
2713                 /* Check if head */
2714                 if (i == 0) {
2715                         p->first_child = c;
2716                 }
2717         }
2718 d584 5
2719 a588 5
2720         astnode *s = NULL;
2721         if (n) {
2722                 for (s = n; s->next_sibling != NULL; s = s->next_sibling);
2723         }
2724         return s;
2725 d598 1
2726 a598 1
2727         return n ? n->parent : NULL;
2728 d608 10
2729 a617 10
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);
2735                 }
2736                 else {
2737                         astnode_add_sibling(n->first_child, new_child);
2738                 }
2739         }
2740 d626 10
2741 a635 10
2742         int i;
2743         va_list ap;
2744         astnode *c;
2746         va_start(ap, count);
2747         for (i=0; i<count; i++) {
2748                 c = va_arg(ap, astnode*);
2749                 astnode_add_child(n, c);
2750         }
2751         va_end(ap);
2752 d645 10
2753 a654 10
2754         astnode *n;
2755         astnode *p;
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);
2763         }
2764 d664 15
2765 a678 15
2766         int i;
2767         astnode *c;
2768         if (n) {
2769                 c = n->first_child;
2770                 for (i = 0; i != index; i++) {
2771                         if (c == NULL) {
2772                                 /* No child at that index. */
2773                                 break;
2774                         }
2775                         c = c->next_sibling;
2776                 }
2777                 return c;
2778         }
2779         /* Node is NULL, so return NULL */
2780         return NULL;
2781 d687 1
2782 a687 1
2783         return (n == NULL) ? NULL : n->first_child;
2784 d698 7
2785 a704 7
2786         int i;
2787         astnode *n;
2788         if (p && c) {
2789                 for (i=0, n=p->first_child; (n != c) && (n != NULL); i++, n=n->next_sibling);
2790                 return n ? i : -1;
2791         }
2792         return -1;
2793 d713 6
2794 a718 6
2795         astnode *c;
2796         int count = 0;
2797         if (p != NULL) {
2798                 for (c = p->first_child; c != NULL; count++, c = c->next_sibling);
2799         }
2800         return count;
2801 d728 48
2802 a775 48
2803         astnode *c;
2804         astnode *n_c;
2805         if (n == NULL) { return NULL; }
2806         /* Create node */
2807         c = astnode_create(astnode_get_type(n), loc);
2808         /* Copy attributes */
2809         switch (astnode_get_type(n)) {
2810                 case INTEGER_NODE:
2811                 c->integer = n->integer;
2812                 break;
2814                 case STRING_NODE:
2815                 case IDENTIFIER_NODE:
2816                 case FILE_PATH_NODE:
2817                 case LABEL_NODE:
2818                 case LOCAL_LABEL_NODE:
2819                 case LOCAL_ID_NODE:
2820                 c->string = (char *)malloc(strlen(n->string)+1);
2821                 if (c->string != NULL) {
2822                         strcpy(c->string, n->string);
2823                 }
2824                 break;
2826                 case ARITHMETIC_NODE:
2827                 c->oper = n->oper;
2828                 break;
2830                 case INSTRUCTION_NODE:
2831                 c->instr = n->instr;
2832                 break;
2834                 case BINARY_NODE:
2835                 c->binary = n->binary;
2836                 break;
2838                 case DATATYPE_NODE:
2839                 c->datatype = n->datatype;
2840                 break;
2842                 default:
2843                 c->param = n->param;
2844         }
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));
2848         }
2849         /* Return the clone */
2850         return c;
2851 d783 35
2852 a817 35
2853         int i;
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 */
2857         }
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;
2872                 default:
2873                 /* Has no internal attributes */
2874                 break;
2875         }
2876         /* Verify that they have the same number of children */
2877         if (astnode_get_child_count(n1) != astnode_get_child_count(n2)) {
2878                 return 0;
2879         }
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))) {
2883                         return 0;
2884                 }
2885         }
2886         /* Equal. */
2887         return 1;
2888 d827 6
2889 a832 6
2890         int i;
2891         astnode *a = astnode_get_parent(n);
2892         for (i=0; i<back; i++) {
2893                 a = astnode_get_parent(a);
2894         }
2895         return a;
2896 d843 7
2897 a849 7
2898         astnode *a;
2899         for (a = astnode_get_parent(n); a != NULL; a = astnode_get_parent(a) ) {
2900                 if (astnode_is_type(a, type)) {
2901                         return 1;
2902                 }
2903         }
2904         return 0;
2905 d858 2
2906 a859 2
2907         if (n == NULL) { return NULL; }
2908         return n->next_sibling;
2909 d868 2
2910 a869 2
2911         if (n == NULL) { return NULL; }
2912         return n->prev_sibling;
2913 d878 12
2914 a889 12
2915         switch (astnode_get_type(n)) {
2916                 case INTEGER_NODE:
2917                 case STRING_NODE:
2918                 /* A literal */
2919                 return 1;
2921                 default:
2922                 /* Not a literal */
2923                 break;
2924         }
2925         /* Not a literal */
2926         return 0;
2927 d901 4
2928 a904 4
2929         /* Create the node */
2930         astnode *n = astnode_create(NULL_NODE, loc);
2931         /* Return the newly created node */
2932         return n;
2933 d916 9
2934 a924 9
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 */
2943         return n;
2944 d934 9
2945 a942 9
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);
2952         }
2953         /* Return the newly created node */
2954         return n;
2955 d952 6
2956 a957 6
2957         /* Create the node */
2958         astnode *n = astnode_create(INTEGER_NODE, loc);
2959         /* Store the integer which this node represents */
2960         n->integer = value;
2961         /* Return the newly created node */
2962         return n;
2963 d967 9
2964 a975 9
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);
2971         }
2972         /* Return the newly created node */
2973         return n;
2974 d987 9
2975 a995 9
2976         /* Create the node */
2977         astnode *n = astnode_create(ARITHMETIC_NODE, loc);
2978         /* Store the operator, which describes the type of expression */
2979         n->oper = oper;
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 */
2984         return n;
2985 d1008 10
2986 a1017 10
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);
2992         if (els != NULL) {
2993                 astnode_add_child(n, astnode_create_default(els, loc));
2994         }
2995         /* Return the newly created node */
2996         return n;
2997 d1028 11
2998 a1038 11
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(
3003                 n,
3004                 2,
3005                 expr,
3006                 astnode_create_list(then)
3007         );
3008         /* Return the newly created node */
3009         return n;
3010 d1048 9
3011 a1056 9
3012         /* Create the node */
3013         astnode *n = astnode_create(DEFAULT_NODE, loc);
3014         /* This node has list of statements as children. */
3015         astnode_add_child(
3016                 n,
3017                 stmts
3018         );
3019         /* Return the newly created node */
3020         return n;
3021 d1068 12
3022 a1079 12
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(
3027                 n,
3028                 3,
3029                 ident,
3030                 astnode_create_list(then),
3031                 astnode_create_list(els)
3032         );
3033         /* Return the newly created node */
3034         return n;
3035 d1091 12
3036 a1102 12
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(
3041                 n,
3042                 3,
3043                 ident,
3044                 astnode_create_list(then),
3045                 astnode_create_list(els)
3046         );
3047         /* Return the newly created node */
3048         return n;
3049 d1114 15
3050 a1128 15
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(
3058                 n,
3059                 3,
3060                 ident,
3061                 astnode_create_list(params),
3062                 astnode_create_list(body)
3063         );
3064         /* Return the newly created node */
3065         return n;
3066 d1139 11
3067 a1149 11
3068         /* Create the node */
3069         astnode *n = astnode_create(MACRO_NODE, loc);
3070         /* Add the children */
3071         astnode_add_children(
3072                 n,
3073                 2,
3074                 ident,
3075                 astnode_create_list(args)
3076         );
3077         /* Return the newly created node */
3078         return n;
3079 d1160 6
3080 a1165 6
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 */
3086         return n;
3087 d1176 6
3088 a1181 6
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 */
3094         return n;
3095 d1192 12
3096 a1203 12
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);
3105         }
3106         astnode_add_child(n, count);
3107         /* Return the newly created node */
3108         return n;
3109 d1213 6
3110 a1218 6
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 */
3116         return n;
3117 d1228 6
3118 a1233 6
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 */
3124         return n;
3125 d1243 6
3126 a1248 6
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 */
3132         return n;
3133 d1258 6
3134 a1263 6
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 */
3140         return n;
3141 d1273 7
3142 a1279 7
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 */
3149         return n;
3150 d1290 7
3151 a1296 7
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 */
3158         return n;
3159 d1307 7
3160 a1313 7
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 */
3167         return n;
3168 d1324 7
3169 a1330 7
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 */
3176         return n;
3177 d1341 7
3178 a1347 7
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 */
3185         return n;
3186 d1355 6
3187 a1360 6
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 */
3193         return n;
3194 d1371 10
3195 a1380 10
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 */
3205         return n;
3206 d1388 6
3207 a1393 6
3208         /* Create the node */
3209         astnode *n = astnode_create(DATASEG_NODE, loc);
3210         /* Set modifiers */
3211         n->modifiers = modifiers;
3212         /* Return the newly created node */
3213         return n;
3214 d1401 4
3215 a1404 4
3216         /* Create the node */
3217         astnode *n = astnode_create(CODESEG_NODE, loc);
3218         /* Return the newly created node */
3219         return n;
3220 d1415 8
3221 a1422 8
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 */
3229         return n;
3230 d1435 9
3231 a1443 9
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);
3238         }
3239         /* Return the newly created node */
3240         return n;
3241 d1455 18
3242 a1472 18
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);
3249         }
3250         /* Two children: Datatype and address */
3251         if (addr == NULL) {
3252                 addr = astnode_create_pc(loc);
3253         }
3254         if (type == NULL) {
3255                 type = astnode_create_datatype(BYTE_DATATYPE, NULL, loc);
3256         }
3257         astnode_add_child(n, type);
3258         astnode_add_child(n, addr);
3259         /* Return the newly created node */
3260         return n;
3261 d1482 9
3262 a1490 9
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);
3269         }
3270         /* Return the newly created node */
3271         return n;
3272 d1500 9
3273 a1508 9
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);
3280         }
3281         /* Return the newly created node */
3282         return n;
3283 d1518 14
3284 a1531 14
3285         astnode *n;
3286         location dummyloc;
3287         /* Create the node */
3288         if (l != NULL) {
3289                 n = astnode_create(LIST_NODE, l->loc);
3290                 /* Add list of values */
3291                 astnode_add_child(n, l);
3292         }
3293         else {
3294                 /* Make a node with zero children */
3295                 n = astnode_create(LIST_NODE, dummyloc);
3296         }
3297         /* Return the newly created node (or NULL) */
3298         return n;
3299 d1540 1
3300 a1540 1
3301         return astnode_create(CURRENT_PC_NODE, loc);
3302 d1551 7
3303 a1557 7
3304         /* Create the node */
3305         astnode *n = astnode_create(BINARY_NODE, loc);
3306         /* Set data */
3307         n->binary.data = bin;
3308         n->binary.size = size;
3309         /* Return the newly created node */
3310         return n;
3311 d1568 6
3312 a1573 6
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 */
3318         return n;
3319 d1584 7
3320 a1590 7
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 */
3327         return n;
3328 d1600 6
3329 a1605 6
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 */
3335         return n;
3336 d1616 10
3337 a1625 10
3338         /* Create the node */
3339         astnode *n = astnode_create(DATATYPE_NODE, loc);
3340         /* Set the datatype */
3341         n->datatype = t;
3342         /* Possibly one child: identifier */
3343         if (id != NULL) {
3344                 astnode_add_child(n, id);
3345         }
3346         /* Return the newly created node */
3347         return n;
3348 d1637 9
3349 a1645 9
3350         /* Create the node */
3351         astnode *n = astnode_create(VAR_DECL_NODE, loc);
3352         /* Set modifiers */
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 */
3358         return n;
3359 d1653 7
3360 a1659 7
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 */
3367         return n;
3368 d1670 13
3369 a1682 13
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(
3376                 n,
3377                 2,
3378                 ident,
3379                 astnode_create_list(stmts)
3380         );
3381         /* Return the newly created node */
3382         return n;
3383 d1693 13
3384 a1705 13
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(
3391                 n,
3392                 2,
3393                 expr,
3394                 astnode_create_list(stmts)
3395         );
3396         /* Return the newly created node */
3397         return n;
3398 d1716 13
3399 a1728 13
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(
3406                 n,
3407                 2,
3408                 expr,
3409                 astnode_create_list(stmts)
3410         );
3411         /* Return the newly created node */
3412         return n;
3413 d1738 6
3414 a1743 6
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 */
3420         return n;
3421 d1753 6
3422 a1758 6
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 */
3428         return n;
3429 d1768 6
3430 a1773 6
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 */
3436         return n;
3437 d1783 9
3438 a1791 9
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);
3445         }
3446         /* Return the newly created node */
3447         return n;
3448 d1801 9
3449 a1809 9
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);
3456         }
3457         /* Return the newly created node */
3458         return n;
3459 d1819 9
3460 a1827 9
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);
3467         }
3468         /* Return the newly created node */
3469         return n;
3470 d1837 9
3471 a1845 9
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);
3478         }
3479         /* Return the newly created node */
3480         return n;
3481 d1855 6
3482 a1860 6
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 */
3488         return n;
3489 d1871 7
3490 a1877 7
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 */
3497         return n;
3498 d1888 7
3499 a1894 7
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 */
3506         return n;
3507 d1913 56
3508 a1968 56
3509         int i;
3510         /* Node type: 1 byte */
3511         put_byte(f, astnode_get_type(n));
3512         /* Internal node data */
3513         switch (astnode_get_type(n)) {
3514                 case INTEGER_NODE:
3515                 /* Assumes that sizeof(long) is same as for int */
3516                 put_int(f, n->integer);
3517                 break;
3519                 case STRING_NODE:
3520                 case IDENTIFIER_NODE:
3521                 case LOCAL_ID_NODE:
3522                 case FILE_PATH_NODE:
3523                 case LABEL_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]);
3530                 }
3531                 break;
3533                 case DATA_NODE:
3534                 /* Datatype: 1 byte */
3535                 put_byte(f, n->datatype);
3536                 break;
3538                 case BINARY_NODE:
3539                 /* Size: 4 bytes */
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]);
3544                 }
3545                 break;
3547                 case ARITHMETIC_NODE:
3548                 // TODO
3549                 break;
3551                 case INSTRUCTION_NODE:
3552                 // TODO
3553                 break;
3555                 default:
3556                 /* No internal attributes */
3557                 break;
3558         }
3559         /* Child count */
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);
3564         }
3565 d1981 70
3566 a2050 70
3567         int i;
3568         int len;
3569         astnode *n;
3570         astnode_type t;
3571         location loc;
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)) {
3578                 case INTEGER_NODE:
3579                 /* Assumes that sizeof(long) is same as for int */
3580                 n->integer = get_int(f);
3581                 break;
3583                 case STRING_NODE:
3584                 case IDENTIFIER_NODE:
3585                 case LOCAL_ID_NODE:
3586                 case FILE_PATH_NODE:
3587                 case LABEL_NODE:
3588                 case LOCAL_LABEL_NODE:
3589                 /* Get length */
3590                 len = get_int(f);
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);
3597                         }
3598                 }
3599                 break;
3601                 case DATA_NODE:
3602                 /* Datatype: 1 byte */
3603                 n->datatype = get_byte(f);
3604                 break;
3606                 case BINARY_NODE:
3607                 /* Size: 4 bytes */
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);
3615                         }
3616                 }
3617                 break;
3619                 case ARITHMETIC_NODE:
3620                 // TODO
3621                 break;
3623                 case INSTRUCTION_NODE:
3624                 // TODO
3625                 break;
3627                 default:
3628                 /* No internal attributes */
3629                 break;
3630         }
3631         /* Child count */
3632         len = get_int(f);
3633         /* Deserialize children */
3634         for (i=0; i<len; i++) {
3635                 astnode_add_child(n, astnode_deserialize(f));
3636         }
3637 d2052 2
3638 a2053 2
3639         /* Return the deserialized node */
3640         return n;
3646 @xorcyst 1.4.0
3648 text
3649 @d2 1
3650 a2 1
3651  * $Id: astnode.c,v 1.8 2004/12/19 09:53:46 kenth Exp kenth $
3652 d4 3
3653 d224 1
3654 d1876 17
3660 @added create_align()
3662 text
3663 @d2 1
3664 a2 1
3665  * $Id: astnode.c,v 1.7 2004/12/18 16:56:12 kenth Exp kenth $
3666 d4 3
3667 d1378 1
3668 a1378 1
3669 astnode *astnode_create_dataseg(location loc)
3670 d1382 2
3671 d1441 1
3672 d1445 1
3673 a1445 1
3674 astnode *astnode_create_label(char *s, astnode *type, location loc)
3675 d1454 4
3676 a1457 1
3677         /* One child: Datatype */
3678 d1462 1
3679 d1622 1
3680 d1627 1
3681 a1627 1
3682 astnode *astnode_create_var_decl(astnode *id, astnode *data, location loc)
3683 d1631 2
3684 d1857 1
3685 d1861 1
3686 a1861 1
3687 astnode *astnode_create_align(astnode *expr, location loc)
3688 d1865 2
3689 a1866 1
3690         /* This node has one child: An expression, which describes the alignment */
3696 @create_extrn() takes unit id
3698 text
3699 @d2 1
3700 a2 1
3701  * $Id: astnode.c,v 1.6 2004/12/16 13:19:07 kenth Exp kenth $
3702 d4 3
3703 d1842 15
3709 @astnode_create_label() takes datatype argument
3711 text
3712 @d2 1
3713 a2 1
3714  * $Id: astnode.c,v 1.5 2004/12/14 01:48:57 kenth Exp kenth $
3715 d4 3
3716 d1353 1
3717 d1355 1
3718 a1355 1
3719 astnode *astnode_create_extrn(astnode *l, astnode *t, location loc)
3720 d1362 3
3721 a1364 1
3722         astnode_add_child(n, l);
3728 @xorcyst 1.3.0
3730 text
3731 @d2 1
3732 a2 1
3733  * $Id: astnode.c,v 1.4 2004/12/11 02:01:10 kenth Exp kenth $
3734 d4 3
3735 d1427 1
3736 d1430 1
3737 a1430 1
3738 astnode *astnode_create_label(char *s, location loc)
3739 d1439 5
3745 @added forward/backward branching
3747 text
3748 @d2 1
3749 a2 1
3750  * $Id: astnode.c,v 1.3 2004/12/09 11:17:59 kenth Exp kenth $
3751 d4 3
3752 d165 2
3753 a166 1
3754                 case ELIF_NODE:         return "ELIF_NODE";
3755 d191 2
3756 d200 1
3757 d208 1
3758 d982 2
3759 a983 2
3760  * @@param elif List of elif nodes (may be <code>NULL</code>)
3761  * @@param els The final else-part (may be <code>NULL</code>)
3762 d990 6
3763 a995 16
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
3770         most four children.
3771         */
3772         astnode_add_children(
3773                 n,
3774                 4,
3775                 expr,
3776                 astnode_create_list(then),
3777                 astnode_create_list(elif),
3778                 astnode_create_list(els)
3779         );
3780 d1001 3
3781 a1003 3
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
3785 d1006 1
3786 a1006 1
3787 astnode *astnode_create_elif(astnode *expr, astnode *then, location loc)
3788 d1009 1
3789 a1009 1
3790         astnode *n = astnode_create(ELIF_NODE, loc);
3791 d1022 18
3792 d1297 34
3793 d1670 23
3794 d1809 15
3800 @added: warning, error nodes
3802 text
3803 @d2 1
3804 a2 1
3805  * $Id: astnode.c,v 1.2 2004/12/06 04:52:05 kenth Exp kenth $
3806 d4 3
3807 d197 4
3808 d315 7
3809 d393 6
3810 d1643 1
3811 a1643 1
3812         /* This node has one children: A string, which is the warning to print */
3813 d1658 1
3814 a1658 1
3815         /* This node has one children: A string, which is the error to print */
3816 d1664 72
3822 @Major updates (xorcyst 1.1.0)
3824 text
3825 @d2 1
3826 a2 1
3827  * $Id: astnode.c,v 1.1 2004/06/30 07:55:28 kenth Exp kenth $
3828 d4 3
3829 d192 2
3830 d1614 30
3836 @Initial revision
3838 text
3839 @d2 5
3840 a6 2
3841  * $Id$
3842  * $Log$
3843 a39 2
3844 #define LHS(e) astnode_get_child(e, 0)
3845 #define RHS(e) astnode_get_child(e, 1)
3846 d177 12
3847 d199 1
3848 a199 1
3849 const char *datatype_to_string(datatype dt)
3850 d201 1
3851 a201 1
3852         switch (dt) {
3853 d206 1
3854 a206 1
3855                 case QWORD_DATATYPE:    return "QWORD_DATATYPE";
3856 a286 8
3857                 case DATA_NODE:
3858                 case STORAGE_NODE:
3859                 printf(
3860                         "(%s)",
3861                         datatype_to_string(n->datatype)
3862                 );
3863                 break;
3865 d296 7
3866 d411 6
3867 a416 4
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);
3872 a674 1
3873         int i;
3874 d676 1
3875 d703 1
3876 a703 2
3877                 c->instr.mnemonic = n->instr.mnemonic;
3878                 c->instr.mode = n->instr.mode;
3879 d707 1
3880 a707 2
3881                 c->binary.size = n->binary.size;
3882                 c->binary.data = n->binary.data;
3883 d710 1
3884 a710 1
3885                 case DATA_NODE:
3886 d717 3
3887 a719 3
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));
3891 a744 1
3892                 case DATA_NODE:         if (n1->datatype != n2->datatype) return 0;     break;
3893 d746 1
3894 d783 17
3895 d846 8
3896 d1125 1
3897 a1125 1
3898  * @@param type Type of storage (*_DATATYPE)
3899 d1129 1
3900 a1129 1
3901 astnode *astnode_create_storage(int type, astnode *count, location loc)
3902 d1133 7
3903 a1139 3
3904         /* Store the type of data */
3905         n->datatype = type;
3906         /* One child: Count */
3907 d1191 65
3908 d1270 2
3909 d1273 1
3910 a1273 1
3911 astnode *astnode_create_extrn(astnode *l, location loc)
3912 d1277 2
3913 d1313 1
3914 a1313 1
3915 astnode *astnode_create_data(int type, astnode *data, location loc)
3916 d1317 2
3917 a1318 2
3918         /* Store the type of data */
3919         n->datatype = type;
3920 d1465 144
3921 d1620 2
3922 d1689 1