1 #ifndef INCLUDED_PARSE
2 #define INCLUDED_PARSE
3 
4 /*
5  * Copyright (c) 2005 Magnus Lind.
6  *
7  * This software is provided 'as-is', without any express or implied warranty.
8  * In no event will the authors be held liable for any damages arising from
9  * the use of this software.
10  *
11  * Permission is granted to anyone to use this software, alter it and re-
12  * distribute it freely for any non-commercial, non-profit purpose subject to
13  * the following restrictions:
14  *
15  *   1. The origin of this software must not be misrepresented; you must not
16  *   claim that you wrote the original software. If you use this software in a
17  *   product, an acknowledgment in the product documentation would be
18  *   appreciated but is not required.
19  *
20  *   2. Altered source versions must be plainly marked as such, and must not
21  *   be misrepresented as being the original software.
22  *
23  *   3. This notice may not be removed or altered from any distribution.
24  *
25  *   4. The names of this software and/or it's copyright holders may not be
26  *   used to endorse or promote products derived from this software without
27  *   specific prior written permission.
28  *
29  */
30 
31 #include "int.h"
32 #include "vec.h"
33 #include "membuf.h"
34 #include "expr.h"
35 
36 #define ATOM_TYPE_OP_ARG_NONE    0	/* uses u.op */
37 #define ATOM_TYPE_OP_ARG_U8      1	/* uses u.op */
38 #define ATOM_TYPE_OP_ARG_U16     2	/* uses u.op */
39 #define ATOM_TYPE_OP_ARG_I8      3	/* uses u.op */
40 #define ATOM_TYPE_OP_ARG_UI8     4	/* uses u.op */
41 #define ATOM_TYPE_EXPRS		12	/* uses u.exprs */
42 #define ATOM_TYPE_WORD_EXPRS	10	/* uses u.exprs */
43 #define ATOM_TYPE_BYTE_EXPRS	11	/* uses u.exprs */
44 #define ATOM_TYPE_RES		13	/* uses u.res */
45 #define ATOM_TYPE_BUFFER	14	/* uses u.buffer */
46 
47 struct op
48 {
49     struct expr *arg;
50     u8 code;
51 };
52 
53 struct res
54 {
55     struct expr *length;
56     struct expr *value;
57 };
58 
59 struct buffer
60 {
61     const char *name;
62     i32 length;
63     i32 skip;
64 };
65 
66 struct atom
67 {
68     u8 type;
69     union
70     {
71         struct op op;
72         struct vec *exprs;
73         struct buffer buffer;
74         struct res res;
75     } u;
76 };
77 
78 extern int push_state_skip;
79 extern int push_state_macro;
80 extern int push_state_init;
81 extern int num_lines;
82 
83 void parse_init();
84 void parse_free();
85 
86 struct atom *new_op(u8 op_code, u8 op_size, struct expr *arg);
87 struct atom *new_op0(u8 op_code);
88 
89 struct atom *new_exprs(struct expr *arg);
90 struct atom *exprs_add(struct atom *atom, struct expr *arg);
91 struct atom *exprs_to_byte_exprs(struct atom *atom);
92 struct atom *exprs_to_word_exprs(struct atom *atom);
93 
94 struct atom *new_res(struct expr *len, struct expr *value);
95 struct atom *new_incbin(const char *name, struct expr *skip, struct expr *len);
96 
97 struct expr *new_is_defined(const char *symbol);
98 struct expr *new_expr_incword(const char *name, struct expr *skip);
99 
100 void new_symbol(const char *symbol, i32 value);
101 void new_symbol_expr(const char *symbol, struct expr *arg);
102 
103 /* returns NULL if found, not otherwise, expp may be NULL. */
104 const char *find_symref(const char *symbol, struct expr **expp);
105 
106 void symbol_dump_resolved(int level, const char *symbol);
107 
108 void new_label(const char *label);
109 void set_org(struct expr *arg);
110 void push_if_state(struct expr *arg);
111 void push_macro_state(const char *name);
112 void macro_append(const char *text);
113 void asm_error(const char *msg);
114 void asm_echo(const char *msg);
115 void asm_include(const char *msg);
116 
117 int assemble(struct membuf *source, struct membuf *dest);
118 void output_atoms(struct membuf *out, struct vec *mem);
119 void asm_src_buffer_push(struct membuf *buf);
120 
121 #endif
122