339
|
1 /*
|
|
2 parse.c
|
|
3 Copyright © 2008 William Astle
|
|
4
|
|
5 This file is part of LWASM.
|
|
6
|
|
7 LWASM is free software: you can redistribute it and/or modify it under the
|
|
8 terms of the GNU General Public License as published by the Free Software
|
|
9 Foundation, either version 3 of the License, or (at your option) any later
|
|
10 version.
|
|
11
|
|
12 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
15 more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License along with
|
|
18 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 */
|
|
20
|
|
21 /*
|
|
22 Contains the general parser
|
|
23 */
|
|
24
|
|
25 #define __parse_c_seen__
|
|
26 #include <config.h>
|
|
27
|
|
28 #include <ctype.h>
|
|
29 #include <string.h>
|
|
30
|
|
31 #include "lwasm.h"
|
|
32 #include "instab.h"
|
|
33 #include "util.h"
|
|
34
|
|
35 // parse a line and despatch to the appropriate handlers for opcodes
|
|
36 int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l)
|
|
37 {
|
|
38 char *p, *p2;
|
|
39 char *opc;
|
|
40 int opnum;
|
|
41 char *sym = NULL;
|
|
42
|
|
43 // if this was a bad op first pass (or otherwise a no-process line)
|
|
44 // ignore it
|
|
45 if (l -> badop)
|
|
46 return;
|
|
47
|
|
48 p = l -> text;
|
|
49 l -> sect = as -> csect;
|
|
50 l -> inmod = as -> inmod;
|
|
51
|
|
52 // blank lines are a no brainer
|
|
53 if (!*p)
|
|
54 {
|
|
55 as -> context = lwasm_next_context(as);
|
|
56 return 0;
|
|
57 }
|
|
58
|
|
59 // for output generation later but only on pass 1
|
|
60 // also used by some pseudo ops on pass 2
|
|
61 if (as -> passnum == 1)
|
|
62 l -> codeaddr = as -> addr;
|
|
63
|
|
64 // if it's a comment, return (this doesn't cause a context change)
|
|
65 if (*p == '*' || *p == ';')
|
|
66 return;
|
|
67
|
|
68 // if we have C pre-processor directives/output, ignore it
|
|
69 if (*p == '#')
|
|
70 return;
|
|
71
|
|
72 // if we start with a non-space character, it's a symbol
|
|
73 if (!isspace(*p))
|
|
74 {
|
|
75 // we have a symbol specified here
|
|
76 // parse it out and record it for later use
|
|
77 for (p2 = p; *p2 && !isspace(*p2) && *p2 != ':'; p2++)
|
|
78 /* do nothing */ ;
|
|
79
|
|
80 sym = lwasm_alloc((p2 - p) + 1);
|
|
81 sym[p2 - p] = '\0';
|
|
82 memcpy(sym, p, p2 - p);
|
|
83
|
|
84 p = p2;
|
|
85 if (!*sym)
|
|
86 {
|
|
87 register_error(as, l, 1, "Invalid symbol");
|
|
88 lwasm_free(sym);
|
|
89 sym = NULL;
|
|
90 }
|
|
91 if (*p == ':')
|
|
92 p++;
|
|
93 }
|
|
94 l -> sym = sym;
|
|
95
|
|
96 // now skip any whitespace to find the opcode
|
|
97 while (*p && isspace(*p))
|
|
98 p++;
|
|
99
|
|
100 // is the line blank?
|
|
101 if (!*p && !sym)
|
|
102 {
|
|
103 // nothing but white space *is* a context break
|
|
104 as -> context = lwasm_next_context(as);
|
|
105 return;
|
|
106 }
|
|
107
|
|
108 // parse the opcode
|
|
109 for (p2 = p; *p2 && !isspace(*p2); p2++)
|
|
110 /* do nothing */ ;
|
|
111
|
|
112 opc = lwasm_alloc((p2 - p) + 1);
|
|
113 memcpy(opc, p, p2 - p);
|
|
114 opc[p2 - p] = '\0';
|
|
115
|
|
116 l -> forceglobal = 0;
|
|
117 // if the opcode contains an =, treat it as "symbol = expr"
|
|
118 if (!sym && strchr(opc, '='))
|
|
119 {
|
|
120 for (p2 = opc; *p2 && *p2 != '='; p2++)
|
|
121 /* do nothing */ ;
|
|
122 sym = lwasm_alloc((p2 - opc) + 1);
|
|
123 memcpy(sym, opc, p2 - opc);
|
|
124 sym[p2 - opc] = '\0';
|
|
125 l -> sym = sym;
|
|
126
|
|
127 p2 = p + (p2 - opc) + 1;
|
|
128 // p2++;
|
|
129 opc[0] = '=';
|
|
130 opc[1] = '\0';
|
|
131 debug_message(2, "Found opcode = with symbol %s and operand %s", sym, p2);
|
|
132 l -> forceglobal = 1;
|
|
133 }
|
|
134
|
|
135 debug_message(2, "Found operation code: '%s'", opc);
|
|
136
|
|
137 // skip intervening whitespace if present
|
|
138 while (*p2 && isspace(*p2))
|
|
139 p2++;
|
|
140
|
|
141 // look up instruction in insn table
|
|
142 for (opnum = 0; instab[opnum].opcode; opnum++)
|
|
143 {
|
|
144 if (!strcasecmp(instab[opnum].opcode, opc))
|
|
145 break;
|
|
146 }
|
|
147
|
|
148 // if we found no operation, check if we had a comment
|
|
149 // the reason this check is here is to allow for "private"
|
|
150 // operation codes like "*pragma" which will be ignored by
|
|
151 // other assemblers
|
|
152 // also skip empty ops
|
|
153 if (!(instab[opnum].opcode))
|
|
154 {
|
|
155 if (*opc == '*' || *opc == ';' || !*opc)
|
|
156 goto done_line;
|
|
157 }
|
|
158
|
|
159 // now we have the opcode and the symbol, we can decide if we're
|
|
160 // actually going to do anything with this line
|
|
161
|
|
162 // we will NOT call the function if any of the following are true:
|
|
163
|
|
164 // - we are skipping a condition and the operation code is not a conditional
|
|
165 // - we are defining a macro and the operation code is not ENDM
|
|
166
|
|
167 // we will call the function in any other circumstance
|
|
168
|
|
169 // first condition above
|
|
170 if (as -> inmacro && instab[opnum].endm == 0)
|
|
171 {
|
|
172 add_macro_line(as, l -> text);
|
|
173 goto done_line;
|
|
174 }
|
|
175
|
|
176 // second condition above
|
|
177 if (as -> skipcond && instab[opnum].iscond == 0)
|
|
178 goto done_line;
|
|
179
|
|
180 // we've registered the symbol as needed
|
|
181 // now we need to check for a macro call IFF we don't collide with
|
|
182 // an operation code; otherwise, call the operation function
|
|
183 if (instab[opnum].opcode)
|
|
184 {
|
|
185 if (instab[opnum].fn && !(as -> no6309 && instab[opnum].is6309))
|
|
186 {
|
|
187 (instab[opnum].fn)(as, l, &p2, opnum);
|
|
188
|
|
189 // if we didn't end on a "space" character or EOL, throw error
|
|
190 if (*p2 && !isspace(*p2) && !(l -> err) && as -> passnum == 1)
|
|
191 register_error(as, l, 1, "Bad operand: %s (%d)", p2, as -> passnum);
|
|
192 }
|
|
193 else
|
|
194 {
|
|
195 // carp about unimplemented operation
|
|
196 if (instab[opnum].is6309)
|
|
197 register_error(as, l, 1, "Use of 6309 operation code: %s", opc);
|
|
198 else
|
|
199 register_error(as, l, 1, "Unimplemented operation code: %s", opc);
|
|
200 }
|
|
201 }
|
|
202 else
|
|
203 {
|
|
204 if (expand_macro(as, l, &p2, opc) == 0)
|
|
205 goto done_line;
|
|
206
|
|
207 // carp about an unknown operation code and note that fact for
|
|
208 // pass 2 in case a macro appears later with the same name!
|
|
209 register_error(as, l, 1, "Uknown operation code: %s", opc);
|
|
210 l -> badop = 1;
|
|
211 }
|
|
212
|
|
213 done_line:
|
|
214 if (!(as -> skipcond || as -> inmacro))
|
|
215 {
|
|
216 // register symbol if the operation didn't
|
|
217 if (sym && instab[opnum].setsym == 0)
|
|
218 {
|
|
219 if (as -> passnum == 1)
|
|
220 {
|
|
221 debug_message(1, "Registering symbol '%s' at %04X", sym, l -> codeaddr);
|
|
222 if (lwasm_register_symbol(as, l, sym, l -> codeaddr, SYMBOL_NORM) < 0)
|
|
223 l -> sym = NULL;
|
|
224 else
|
|
225 l -> addrset = 1;
|
|
226 }
|
|
227 }
|
|
228 }
|
|
229
|
|
230 l -> sect = as -> csect;
|
|
231 l -> context = as -> context;
|
|
232
|
|
233 lwasm_free(opc);
|
|
234 if (sym)
|
|
235 lwasm_free(sym);
|
|
236 }
|