Mercurial > hg-old > index.cgi
annotate lwasm/pass1.c @ 386:af5f2c51db76
Bugfixing on includes
author | lost@starbug |
---|---|
date | Sun, 16 May 2010 13:03:17 -0600 |
parents | cf8c92d70eb1 |
children | fbb7bfed8076 |
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; | |
383
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
56 int lc = 1; |
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); |
383
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
77 lc = 1; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
78 continue; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
79 } |
372
90de73ba0cac
Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents:
370
diff
changeset
|
80 debug_message(as, 75, "Read line: %s", line); |
337 | 81 |
82 cl = lw_alloc(sizeof(line_t)); | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
83 memset(cl, 0, sizeof(line_t)); |
374 | 84 cl -> outputl = -1; |
383
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
85 cl -> linespec = lw_strdup(input_curspec(as)); |
337 | 86 cl -> prev = as -> line_tail; |
87 cl -> insn = -1; | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
88 cl -> as = as; |
354 | 89 cl -> inmod = as -> inmod; |
90 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
|
91 cl -> pragmas = as -> pragmas; |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
92 cl -> context = as -> context; |
382 | 93 cl -> ltext = lw_strdup(line); |
385 | 94 as -> cl = cl; |
337 | 95 if (!as -> line_tail) |
96 { | |
97 as -> line_head = cl; | |
98 cl -> addr = lw_expr_build(lw_expr_type_int, 0); | |
99 } | |
100 else | |
101 { | |
102 lw_expr_t te; | |
351 | 103 |
383
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
104 cl -> lineno = as -> line_tail -> lineno + 1; |
337 | 105 as -> line_tail -> next = cl; |
351 | 106 |
107 // set the line address | |
337 | 108 te = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, cl -> prev); |
109 cl -> addr = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, cl -> prev -> addr, te); | |
110 lw_expr_destroy(te); | |
385 | 111 lwasm_reduce_expr(as, cl -> addr); |
112 // lw_expr_simplify(cl -> addr, as); | |
351 | 113 |
114 // carry DP value forward | |
115 cl -> dpval = cl -> prev -> dpval; | |
383
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
116 |
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
117 } |
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
118 if (!lc && strcmp(cl -> linespec, cl -> prev -> linespec)) |
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
119 lc = 1; |
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
120 if (lc) |
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
121 { |
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
122 cl -> lineno = 1; |
848d3cca8078
Fixed imm8 to actually use expression and also fixed gen mode to respect > and <
lost@starbug
parents:
382
diff
changeset
|
123 lc = 0; |
337 | 124 } |
125 as -> line_tail = cl; | |
340 | 126 // 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
|
127 // except a local symbol context break |
340 | 128 if (!*line) |
129 { | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
130 as -> context = lwasm_next_context(as); |
340 | 131 goto nextline; |
132 } | |
133 | |
134 // skip comments | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
135 // commends do not create a context break |
340 | 136 if (*line == '*' || *line == ';' || *line == '#') |
137 goto nextline; | |
138 | |
139 p1 = line; | |
140 if (isdigit(*p1)) | |
141 { | |
142 // skip line number | |
143 while (*p1 && isdigit(*p1)) | |
144 p1++; | |
145 if (!*p1 && !isspace(*p1)) | |
146 p1 = line; | |
147 else if (*p1 && isspace(*p1)) | |
148 p1++; | |
149 } | |
150 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
151 // blank line - context break |
340 | 152 if (!*p1) |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
153 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
154 as -> context = lwasm_next_context(as); |
340 | 155 goto nextline; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
156 } |
340 | 157 |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
158 // comment - no context break |
340 | 159 if (*p1 == '*' || *p1 == ';' || *p1 == '#') |
160 goto nextline; | |
161 | |
162 if (isspace(*p1)) | |
163 { | |
164 for (; *p1 && isspace(*p1); p1++) | |
165 /* do nothing */ ; | |
166 stspace = 1; | |
167 } | |
168 else | |
169 stspace = 0; | |
170 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
171 if (*p1 == '*' || *p1 == ';' || *p1 == '#') |
340 | 172 goto nextline; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
173 if (!*p1) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
174 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
175 // nothing but whitespace - context break |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
176 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
|
177 goto nextline; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
178 } |
340 | 179 |
342 | 180 // find the end of the first token |
340 | 181 for (tok = p1; *p1 && !isspace(*p1) && *p1 != ':'; p1++) |
182 /* do nothing */ ; | |
183 | |
184 if (*p1 == ':' || stspace == 0) | |
185 { | |
186 // have a symbol here | |
187 sym = lw_strndup(tok, p1 - tok); | |
188 if (*p1 == ':') | |
189 p1++; | |
190 for (; *p1 && isspace(*p1); p1++) | |
191 /* do nothing */ ; | |
192 | |
193 for (tok = p1; *p1 && !isspace(*p1); p1++) | |
194 /* do nothing */ ; | |
195 } | |
370 | 196 if (sym) |
197 cl -> sym = lw_strdup(sym); | |
340 | 198 cl -> symset = 0; |
199 | |
200 // tok points to the opcode for the line or NUL if none | |
201 if (*tok) | |
202 { | |
203 // look up operation code | |
204 sym = lw_strndup(tok, p1 - tok); | |
370 | 205 for (; *p1 && isspace(*p1); p1++) |
340 | 206 /* do nothing */ ; |
207 | |
208 for (opnum = 0; instab[opnum].opcode; opnum++) | |
209 { | |
210 if (!strcasecmp(instab[opnum].opcode, sym)) | |
211 break; | |
212 } | |
213 | |
214 // p1 points to the start of the operand | |
215 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
216 // 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
|
217 // 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
|
218 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
|
219 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
220 add_macro_line(as, line); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
221 goto linedone; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
222 } |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
223 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
224 // 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
|
225 // 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
|
226 // do nothing |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
227 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
|
228 goto linedone; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
229 |
340 | 230 if (instab[opnum].opcode == NULL) |
231 { | |
232 cl -> insn = -1; | |
233 if (*tok != ';' && *tok != '*') | |
234 { | |
235 // 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
|
236 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
|
237 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
238 // macro expansion failed |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
239 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
|
240 } |
340 | 241 } |
242 } | |
243 else | |
244 { | |
245 cl -> insn = opnum; | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
246 // 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
|
247 if (instab[opnum].parse) |
340 | 248 { |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
249 cl -> len = -1; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
250 // call parse function |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
251 (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
|
252 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
253 if (*p1 && !isspace(*p1)) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
254 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
255 // flag bad operand error |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
256 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
|
257 } |
340 | 258 } |
259 } | |
260 } | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
261 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
262 linedone: |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
263 lw_free(sym); |
340 | 264 |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
265 if (!as -> skipcond && !as -> inmacro) |
340 | 266 { |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
267 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
|
268 { |
372
90de73ba0cac
Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents:
370
diff
changeset
|
269 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
|
270 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
271 // register symbol at line address |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
272 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
|
273 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
274 // symbol error |
370 | 275 // 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
|
276 } |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
277 } |
372
90de73ba0cac
Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
lost@starbug
parents:
370
diff
changeset
|
278 debug_message(as, 40, "Line address: %s", lw_expr_print(cl -> addr)); |
340 | 279 } |
280 | |
281 nextline: | |
332 | 282 lw_free(line); |
347 | 283 |
284 // if we've hit the "end" bit, finish out | |
285 if (as -> endseen) | |
286 return; | |
332 | 287 } |
288 } |