Mercurial > hg-old > index.cgi
annotate lwasm/pass1.c @ 374:d99322ef6f21
Stage 1: actually do output
author | lost@starbug |
---|---|
date | Sat, 24 Apr 2010 14:15:18 -0600 |
parents | 90de73ba0cac |
children | eacdae8a1575 |
rev | line source |
---|---|
332 | 1 /* |
2 pass1.c | |
3 | |
4 Copyright © 2010 William Astle | |
5 | |
6 This file is part of LWTOOLS. | |
7 | |
8 LWTOOLS is free software: you can redistribute it and/or modify it under the | |
9 terms of the GNU General Public License as published by the Free Software | |
10 Foundation, either version 3 of the License, or (at your option) any later | |
11 version. | |
12 | |
13 This program is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 more details. | |
17 | |
18 You should have received a copy of the GNU General Public License along with | |
19 this program. If not, see <http://www.gnu.org/licenses/>. | |
20 */ | |
21 | |
22 #include <config.h> | |
23 | |
24 #include <stdio.h> | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
25 #include <string.h> |
332 | 26 |
27 #include <lw_alloc.h> | |
342 | 28 #include <lw_string.h> |
332 | 29 |
30 #include "lwasm.h" | |
342 | 31 #include "instab.h" |
332 | 32 #include "input.h" |
33 | |
337 | 34 /* |
35 pass 1: parse the lines | |
340 | 36 |
37 line format: | |
38 | |
39 [<symbol>] <opcode> <operand>[ <comment>] | |
40 | |
41 If <symbol> is followed by a :, whitespace may precede the symbol | |
42 | |
43 A line may optionally start with a number which must not be preceded by | |
44 white space and must be followed by a single whitespace character. After | |
45 that whitespace character, the line is parsed as if it had no line number. | |
46 | |
337 | 47 */ |
332 | 48 void do_pass1(asmstate_t *as) |
49 { | |
50 char *line; | |
337 | 51 line_t *cl; |
340 | 52 char *p1; |
342 | 53 int stspace; |
340 | 54 char *tok, *sym; |
55 int opnum; | |
56 | |
332 | 57 for (;;) |
58 { | |
340 | 59 sym = NULL; |
332 | 60 line = input_readline(as); |
61 if (!line) | |
62 break; | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
63 if (line[0] == 1 && line[1] == 1) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
64 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
65 // special internal directive |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
66 // these DO NOT appear in the output anywhere |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
67 // they are generated by the parser to pass information |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
68 // forward |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
69 for (p1 = line + 2; *p1 && !isspace(*p1); p1++) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
70 /* do nothing */ ; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
71 *p1++ = '\0'; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
72 if (!strcmp(line + 2, "SETCONTEXT")) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
73 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
74 as -> context = strtol(p1, NULL, 10); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
75 } |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
76 lw_free(line); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
77 continue; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
78 } |
372
90de73ba0cac
Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents:
370
diff
changeset
|
79 debug_message(as, 75, "Read line: %s", line); |
337 | 80 |
81 cl = lw_alloc(sizeof(line_t)); | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
82 memset(cl, 0, sizeof(line_t)); |
374 | 83 cl -> outputl = -1; |
337 | 84 cl -> prev = as -> line_tail; |
85 cl -> insn = -1; | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
86 cl -> as = as; |
354 | 87 cl -> inmod = as -> inmod; |
88 cl -> csect = as -> csect; | |
360
7d91ab7ac7d6
Indexed stage 2; set line structure to track pragmas in effect for that line
lost@starbug
parents:
354
diff
changeset
|
89 cl -> pragmas = as -> pragmas; |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
90 cl -> context = as -> context; |
337 | 91 if (!as -> line_tail) |
92 { | |
93 as -> line_head = cl; | |
94 cl -> addr = lw_expr_build(lw_expr_type_int, 0); | |
95 } | |
96 else | |
97 { | |
98 lw_expr_t te; | |
351 | 99 |
337 | 100 as -> line_tail -> next = cl; |
351 | 101 |
102 // set the line address | |
337 | 103 te = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, cl -> prev); |
104 cl -> addr = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, cl -> prev -> addr, te); | |
105 lw_expr_destroy(te); | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
106 lw_expr_simplify(cl -> addr, as); |
351 | 107 |
108 // carry DP value forward | |
109 cl -> dpval = cl -> prev -> dpval; | |
337 | 110 } |
111 as -> line_tail = cl; | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
112 as -> cl = cl; |
340 | 113 // blank lines don't count for anything |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
114 // except a local symbol context break |
340 | 115 if (!*line) |
116 { | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
117 as -> context = lwasm_next_context(as); |
340 | 118 goto nextline; |
119 } | |
120 | |
121 // skip comments | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
122 // commends do not create a context break |
340 | 123 if (*line == '*' || *line == ';' || *line == '#') |
124 goto nextline; | |
125 | |
126 p1 = line; | |
127 if (isdigit(*p1)) | |
128 { | |
129 // skip line number | |
130 while (*p1 && isdigit(*p1)) | |
131 p1++; | |
132 if (!*p1 && !isspace(*p1)) | |
133 p1 = line; | |
134 else if (*p1 && isspace(*p1)) | |
135 p1++; | |
136 } | |
137 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
138 // blank line - context break |
340 | 139 if (!*p1) |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
140 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
141 as -> context = lwasm_next_context(as); |
340 | 142 goto nextline; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
143 } |
340 | 144 |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
145 // comment - no context break |
340 | 146 if (*p1 == '*' || *p1 == ';' || *p1 == '#') |
147 goto nextline; | |
148 | |
149 if (isspace(*p1)) | |
150 { | |
151 for (; *p1 && isspace(*p1); p1++) | |
152 /* do nothing */ ; | |
153 stspace = 1; | |
154 } | |
155 else | |
156 stspace = 0; | |
157 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
158 if (*p1 == '*' || *p1 == ';' || *p1 == '#') |
340 | 159 goto nextline; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
160 if (!*p1) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
161 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
162 // nothing but whitespace - context break |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
163 as -> context = lwasm_next_context(as); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
164 goto nextline; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
165 } |
340 | 166 |
342 | 167 // find the end of the first token |
340 | 168 for (tok = p1; *p1 && !isspace(*p1) && *p1 != ':'; p1++) |
169 /* do nothing */ ; | |
170 | |
171 if (*p1 == ':' || stspace == 0) | |
172 { | |
173 // have a symbol here | |
174 sym = lw_strndup(tok, p1 - tok); | |
175 if (*p1 == ':') | |
176 p1++; | |
177 for (; *p1 && isspace(*p1); p1++) | |
178 /* do nothing */ ; | |
179 | |
180 for (tok = p1; *p1 && !isspace(*p1); p1++) | |
181 /* do nothing */ ; | |
182 } | |
370 | 183 if (sym) |
184 cl -> sym = lw_strdup(sym); | |
340 | 185 cl -> symset = 0; |
186 | |
187 // tok points to the opcode for the line or NUL if none | |
188 if (*tok) | |
189 { | |
190 // look up operation code | |
191 sym = lw_strndup(tok, p1 - tok); | |
370 | 192 for (; *p1 && isspace(*p1); p1++) |
340 | 193 /* do nothing */ ; |
194 | |
195 for (opnum = 0; instab[opnum].opcode; opnum++) | |
196 { | |
197 if (!strcasecmp(instab[opnum].opcode, sym)) | |
198 break; | |
199 } | |
200 | |
201 // p1 points to the start of the operand | |
202 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
203 // if we're inside a macro definition and not at ENDM, |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
204 // add the line to the macro definition and continue |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
205 if (as -> inmacro && !(instab[opnum].flags & lwasm_insn_endm)) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
206 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
207 add_macro_line(as, line); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
208 goto linedone; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
209 } |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
210 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
211 // if skipping a condition and the operation code doesn't |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
212 // operate within a condition (not a conditional) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
213 // do nothing |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
214 if (as -> skipcond && !(instab[opnum].flags & lwasm_insn_cond)) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
215 goto linedone; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
216 |
340 | 217 if (instab[opnum].opcode == NULL) |
218 { | |
219 cl -> insn = -1; | |
220 if (*tok != ';' && *tok != '*') | |
221 { | |
222 // bad opcode; check for macro here | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
223 if (expand_macro(as, cl, &p1, sym) != 0) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
224 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
225 // macro expansion failed |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
226 lwasm_register_error(as, cl, "Bad opcode"); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
227 } |
340 | 228 } |
229 } | |
230 else | |
231 { | |
232 cl -> insn = opnum; | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
233 // no parse func means operand doesn't matter |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
234 if (instab[opnum].parse) |
340 | 235 { |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
236 cl -> len = -1; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
237 // call parse function |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
238 (instab[opnum].parse)(as, cl, &p1); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
239 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
240 if (*p1 && !isspace(*p1)) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
241 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
242 // flag bad operand error |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
243 lwasm_register_error(as, cl, "Bad operand (%s)", p1); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
244 } |
340 | 245 } |
246 } | |
247 } | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
248 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
249 linedone: |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
250 lw_free(sym); |
340 | 251 |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
252 if (!as -> skipcond && !as -> inmacro) |
340 | 253 { |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
254 if (cl -> sym && cl -> symset == 0) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
255 { |
372
90de73ba0cac
Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents:
370
diff
changeset
|
256 debug_message(as, 50, "Register symbol %s: %s", cl -> sym, lw_expr_print(cl -> addr)); |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
257 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
258 // register symbol at line address |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
259 if (!register_symbol(as, cl, cl -> sym, cl -> addr, symbol_flag_none)) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
260 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
261 // symbol error |
370 | 262 // lwasm_register_error(as, cl, "Bad symbol '%s'", cl -> sym); |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
263 } |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
264 } |
372
90de73ba0cac
Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents:
370
diff
changeset
|
265 debug_message(as, 40, "Line address: %s", lw_expr_print(cl -> addr)); |
340 | 266 } |
267 | |
268 nextline: | |
332 | 269 lw_free(line); |
347 | 270 |
271 // if we've hit the "end" bit, finish out | |
272 if (as -> endseen) | |
273 return; | |
332 | 274 } |
275 } |