prefix bytecodeproc with xasm_
[xorcyst.git] / astnode.h
blob09e5d38a65f6eb7d7830698b658b7ae3532e73c6
1 /*
2 * $Id: astnode.h,v 1.14 2007/08/12 18:59:10 khansen Exp $
3 * $Log: astnode.h,v $
4 * Revision 1.14 2007/08/12 18:59:10 khansen
5 * ability to generate pure 6502 binary
7 * Revision 1.13 2007/08/09 22:06:00 khansen
8 * general-purpose flags
10 * Revision 1.12 2007/08/07 21:12:16 khansen
11 * const
13 * Revision 1.11 2007/07/22 13:35:20 khansen
14 * convert tabs to whitespaces
16 * Revision 1.10 2004/12/29 21:44:23 kenth
17 * xorcyst 1.4.2
18 * added create_index()
20 * Revision 1.9 2004/12/19 20:46:49 kenth
21 * xorcyst 1.4.0
23 * Revision 1.8 2004/12/19 09:53:56 kenth
24 * added create_align()
26 * Revision 1.7 2004/12/18 16:56:55 kenth
27 * create_extrn() takes unit id
29 * Revision 1.6 2004/12/16 13:19:28 kenth
30 * changed astnode_create_label() signature
32 * Revision 1.5 2004/12/14 01:51:22 kenth
33 * xorcyst 1.3.0
35 * Revision 1.4 2004/12/11 02:01:44 kenth
36 * added forward/backward branching
38 * Revision 1.3 2004/12/09 11:17:41 kenth
39 * added: warning, error nodes
41 * Revision 1.2 2004/12/06 04:54:00 kenth
42 * xorcyst 1.1.0
44 * Revision 1.1 2004/06/30 07:56:09 kenth
45 * Initial revision
49 /**
50 * (C) 2004 Kent Hansen
52 * The XORcyst is free software; you can redistribute it and/or modify
53 * it under the terms of the GNU General Public License as published by
54 * the Free Software Foundation; either version 2 of the License, or
55 * (at your option) any later version.
57 * The XORcyst is distributed in the hope that it will be useful,
58 * but WITHOUT ANY WARRANTY; without even the implied warranty of
59 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 * GNU General Public License for more details.
62 * You should have received a copy of the GNU General Public License
63 * along with The XORcyst; if not, write to the Free Software
64 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
67 /**
68 * Header file with definitions for Abstract Syntax Tree (AST) nodes.
70 #ifndef ASTNODE_H
71 #define ASTNODE_H
73 #include "loc.h"
75 #define LHS(e) astnode_get_child(e, 0)
76 #define RHS(e) astnode_get_child(e, 1)
78 /**
79 * The possible addressing modes for a parsed instruction.
80 * Note that there is not a 1:1 correspondence between these and
81 * the "real" 6502 addressing modes. Specifically, the parser doesn't
82 * distinguish between absolute and relative mode, or between
83 * 8-bit (zeropage) absolute addressing and 16-bit addressing.
84 * That's for the code generator to figure out.
86 enum tag_addressing_mode {
87 IMPLIED_MODE=0,
88 ACCUMULATOR_MODE,
89 IMMEDIATE_MODE,
90 ZEROPAGE_MODE,
91 ZEROPAGE_X_MODE,
92 ZEROPAGE_Y_MODE,
93 ABSOLUTE_MODE,
94 ABSOLUTE_X_MODE,
95 ABSOLUTE_Y_MODE,
96 PREINDEXED_INDIRECT_MODE,
97 POSTINDEXED_INDIRECT_MODE,
98 INDIRECT_MODE,
99 RELATIVE_MODE,
100 INVALID_MODE
103 typedef enum tag_addressing_mode addressing_mode;
106 * The possible instruction mnemonics.
108 enum tag_instr_mnemonic {
109 ADC_MNEMONIC=0,
110 AND_MNEMONIC,
111 ASL_MNEMONIC,
112 BCC_MNEMONIC,
113 BCS_MNEMONIC,
114 BEQ_MNEMONIC,
115 BIT_MNEMONIC,
116 BMI_MNEMONIC,
117 BNE_MNEMONIC,
118 BPL_MNEMONIC,
119 BRK_MNEMONIC,
120 BVC_MNEMONIC,
121 BVS_MNEMONIC,
122 CLC_MNEMONIC,
123 CLD_MNEMONIC,
124 CLI_MNEMONIC,
125 CLV_MNEMONIC,
126 CMP_MNEMONIC,
127 CPX_MNEMONIC,
128 CPY_MNEMONIC,
129 DEC_MNEMONIC,
130 DEX_MNEMONIC,
131 DEY_MNEMONIC,
132 EOR_MNEMONIC,
133 INC_MNEMONIC,
134 INX_MNEMONIC,
135 INY_MNEMONIC,
136 JMP_MNEMONIC,
137 JSR_MNEMONIC,
138 LDA_MNEMONIC,
139 LDX_MNEMONIC,
140 LDY_MNEMONIC,
141 LSR_MNEMONIC,
142 NOP_MNEMONIC,
143 ORA_MNEMONIC,
144 PHA_MNEMONIC,
145 PHP_MNEMONIC,
146 PLA_MNEMONIC,
147 PLP_MNEMONIC,
148 ROL_MNEMONIC,
149 ROR_MNEMONIC,
150 RTI_MNEMONIC,
151 RTS_MNEMONIC,
152 SBC_MNEMONIC,
153 SEC_MNEMONIC,
154 SED_MNEMONIC,
155 SEI_MNEMONIC,
156 STA_MNEMONIC,
157 STX_MNEMONIC,
158 STY_MNEMONIC,
159 TAX_MNEMONIC,
160 TAY_MNEMONIC,
161 TSX_MNEMONIC,
162 TXA_MNEMONIC,
163 TXS_MNEMONIC,
164 TYA_MNEMONIC
167 typedef enum tag_instr_mnemonic instr_mnemonic;
170 * The possible types of a node in the abstract syntax tree.
172 enum tag_astnode_type {
173 NULL_NODE=0,
174 INTEGER_NODE,
175 STRING_NODE,
176 IDENTIFIER_NODE,
177 DATA_NODE,
178 STORAGE_NODE,
179 MACRO_DECL_NODE,
180 MACRO_NODE,
181 ARITHMETIC_NODE,
182 IF_NODE,
183 CASE_NODE,
184 DEFAULT_NODE,
185 IFDEF_NODE,
186 IFNDEF_NODE,
187 INCSRC_NODE,
188 INCBIN_NODE,
189 EQU_NODE,
190 ASSIGN_NODE,
191 ALIGN_NODE,
192 INSTRUCTION_NODE,
193 FILE_PATH_NODE,
194 CURRENT_PC_NODE,
195 LIST_NODE,
196 LABEL_NODE,
197 LOCAL_LABEL_NODE,
198 LOCAL_ID_NODE,
199 BINARY_NODE,
200 PUBLIC_NODE,
201 EXTRN_NODE,
202 DATASEG_NODE,
203 CODESEG_NODE,
204 CHARMAP_NODE,
205 STRUC_NODE,
206 STRUC_DECL_NODE,
207 UNION_DECL_NODE,
208 ENUM_DECL_NODE,
209 RECORD_DECL_NODE,
210 BITFIELD_DECL_NODE,
211 DOT_NODE,
212 SIZEOF_NODE,
213 DATATYPE_NODE,
214 VAR_DECL_NODE,
215 SCOPE_NODE,
216 PROC_NODE,
217 REPT_NODE,
218 WHILE_NODE,
219 MESSAGE_NODE,
220 WARNING_NODE,
221 ERROR_NODE,
222 FORWARD_BRANCH_DECL_NODE,
223 BACKWARD_BRANCH_DECL_NODE,
224 FORWARD_BRANCH_NODE,
225 BACKWARD_BRANCH_NODE,
226 MASK_NODE,
227 INDEX_NODE,
228 ORG_NODE,
229 TOMBSTONE_NODE
232 typedef enum tag_astnode_type astnode_type;
235 * The possible data types.
237 enum tag_datatype {
238 BYTE_DATATYPE = 0,
239 CHAR_DATATYPE,
240 WORD_DATATYPE,
241 DWORD_DATATYPE,
242 USER_DATATYPE /* i.e. structure, union, enumeration, ... */
245 typedef enum tag_datatype datatype;
248 * The possible types of operators for an ARITHMETIC_NODE.
250 enum tag_arithmetic_operator {
251 PLUS_OPERATOR=0,
252 MINUS_OPERATOR,
253 MUL_OPERATOR,
254 DIV_OPERATOR,
255 MOD_OPERATOR,
256 AND_OPERATOR,
257 OR_OPERATOR,
258 XOR_OPERATOR,
259 SHL_OPERATOR,
260 SHR_OPERATOR,
261 LT_OPERATOR,
262 GT_OPERATOR,
263 EQ_OPERATOR,
264 NE_OPERATOR,
265 LE_OPERATOR,
266 GE_OPERATOR,
267 NEG_OPERATOR,
268 NOT_OPERATOR,
269 LO_OPERATOR,
270 HI_OPERATOR,
271 UMINUS_OPERATOR,
272 BANK_OPERATOR
275 typedef enum tag_arithmetic_operator arithmetic_operator;
278 * Instruction attributes.
280 struct tag_instruction_attribs {
281 int mnemonic;
282 addressing_mode mode;
283 unsigned char opcode;
286 typedef struct tag_instruction_attribs instruction_attribs;
289 * Binary (byte buffer) attributes.
291 struct tag_binary_attribs {
292 unsigned char *data;
293 int size;
296 typedef struct tag_binary_attribs binary_attribs;
299 * Structure that defines content of a node in the abstract syntax tree.
301 struct tag_astnode {
302 astnode_type type;
303 union {
304 int integer; /* type == INTEGER_NODE */
305 char *string; /* type == STRING_NODE */
306 char *ident; /* type == IDENTIFIER_NODE */
307 char *label; /* type == LABEL_NODE */
308 char *file_path; /* type == FILE_PATH_NODE */
309 binary_attribs binary; /* type == BINARY_NODE */
310 instruction_attribs instr; /* type == INSTRUCTION_NODE */
311 arithmetic_operator oper; /* type == ARITHMETIC_NODE */
312 datatype datatype; /* type == DATATYPE_NODE */
313 int modifiers; /* type == DATASEG_NODE, VAR_DECL_NODE */
314 /* The other node types have attributes stored as children,
315 or can use this general-purpose field: */
316 long param;
318 int flags;
319 location loc; /* File location where node was parsed */
320 struct tag_astnode *prev_sibling;
321 struct tag_astnode *next_sibling;
322 struct tag_astnode *first_child;
323 struct tag_astnode *parent;
326 typedef struct tag_astnode astnode;
329 * Function prototypes.
331 astnode *astnode_create(astnode_type, location);
332 void astnode_finalize(astnode *);
333 void astnode_replace(astnode *, astnode *);
334 void astnode_remove(astnode *);
335 void astnode_insert_child(astnode *, astnode *, int);
336 int astnode_remove_child(astnode *, astnode *);
337 astnode *astnode_remove_child_at(astnode *, int);
338 astnode *astnode_remove_children(astnode *);
339 void astnode_add_sibling(astnode *, astnode *);
340 void astnode_add_child(astnode *, astnode *);
341 void astnode_add_children(astnode *, int, ...);
342 astnode *astnode_get_child(const astnode *, int);
343 astnode *astnode_get_first_child(const astnode *);
344 int astnode_get_child_count(const astnode *);
345 astnode *astnode_get_parent(const astnode *);
346 astnode *astnode_get_ancestor(const astnode *, int);
347 int astnode_has_ancestor_of_type(const astnode *, astnode_type);
348 int astnode_get_child_index(const astnode *, const astnode *);
349 astnode *astnode_get_last_sibling(const astnode *);
350 astnode_type astnode_get_type(const astnode *);
351 astnode *astnode_get_next_sibling(const astnode *);
352 astnode *astnode_get_prev_sibling(const astnode *);
353 void astnode_print(const astnode *, int);
354 astnode *astnode_clone(const astnode *, location);
355 int astnode_equal(const astnode *, const astnode *);
356 int astnode_is_literal(const astnode *);
357 const char *astnode_type_to_string(astnode_type);
358 #define astnode_is_type(n, t) (astnode_get_type(n) == (t))
360 astnode *astnode_create_null(location);
361 astnode *astnode_create_instruction(int, addressing_mode, astnode *, location);
362 astnode *astnode_create_identifier(const char *, location);
363 astnode *astnode_create_integer(int, location);
364 astnode *astnode_create_string(const char *, location);
365 astnode *astnode_create_file_path(const char *, location);
366 astnode *astnode_create_arithmetic(arithmetic_operator, astnode *, astnode *, location);
367 astnode *astnode_create_if(astnode *, astnode *, astnode *, astnode *, location);
368 astnode *astnode_create_case(astnode *, astnode *, location);
369 astnode *astnode_create_default(astnode *, location);
370 astnode *astnode_create_ifdef(astnode *, astnode *, astnode *, location);
371 astnode *astnode_create_ifndef(astnode *, astnode *, astnode *, location);
372 astnode *astnode_create_macro_decl(astnode *, astnode *, astnode *, location);
373 astnode *astnode_create_macro(astnode *, astnode *, location);
374 astnode *astnode_create_equ(astnode *, astnode *, location);
375 astnode *astnode_create_assign(astnode *, astnode *, location);
376 astnode *astnode_create_incsrc(astnode *, location);
377 astnode *astnode_create_incbin(astnode *, location);
378 astnode *astnode_create_public(astnode *, location);
379 astnode *astnode_create_extrn(astnode *, astnode *, astnode *, location);
380 astnode *astnode_create_charmap(astnode *, location);
381 astnode *astnode_create_struc(astnode *, location);
382 astnode *astnode_create_struc_decl(astnode *, astnode *, location);
383 astnode *astnode_create_union_decl(astnode *, astnode *, location);
384 astnode *astnode_create_enum_decl(astnode *, astnode *, location);
385 astnode *astnode_create_record_decl(astnode *, astnode *, location);
386 astnode *astnode_create_bitfield_decl(astnode *, astnode *, location);
387 astnode *astnode_create_dot(astnode *, astnode *, location);
388 astnode *astnode_create_data(astnode *, astnode *, location);
389 astnode *astnode_create_storage(astnode *, astnode *, location);
390 astnode *astnode_create_dataseg(int, location);
391 astnode *astnode_create_codeseg(location);
392 astnode *astnode_create_pc(location);
393 astnode *astnode_create_label(const char *, astnode *, astnode *, location);
394 astnode *astnode_create_local_label(const char *, location);
395 astnode *astnode_create_local_id(const char *, location);
396 astnode *astnode_create_binary(unsigned char *, int, location);
397 astnode *astnode_create_list(astnode *);
398 astnode *astnode_create_sizeof(astnode *, location);
399 astnode *astnode_create_datatype(datatype, astnode *, location);
400 astnode *astnode_create_var_decl(int, astnode *, astnode *, location);
401 astnode *astnode_create_scope(astnode *, astnode *, location);
402 astnode *astnode_create_proc(astnode *, astnode *, location);
403 astnode *astnode_create_rept(astnode *, astnode *, location);
404 astnode *astnode_create_while(astnode *, astnode *, location);
405 astnode *astnode_create_message(astnode *, location);
406 astnode *astnode_create_warning(astnode *, location);
407 astnode *astnode_create_error(astnode *, location);
408 astnode *astnode_create_forward_branch_decl(const char *, location);
409 astnode *astnode_create_backward_branch_decl(const char *, location);
410 astnode *astnode_create_forward_branch(const char *, location);
411 astnode *astnode_create_backward_branch(const char *, location);
412 astnode *astnode_create_mask(astnode *, location);
413 astnode *astnode_create_align(astnode *, astnode *, location);
414 astnode *astnode_create_index(astnode *, astnode *, location);
415 astnode *astnode_create_org(astnode *, location);
416 astnode *astnode_create_tombstone(astnode_type type, location);
418 #endif /* !ASTNODE_H */