Mercurial > hg-old > index.cgi
annotate lwasm/pass1.c @ 360:7d91ab7ac7d6
Indexed stage 2; set line structure to track pragmas in effect for that line
author | lost@starbug |
---|---|
date | Thu, 01 Apr 2010 18:39:40 -0600 |
parents | 60568b123281 |
children | d96c30e60ddf |
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 } |
332 | 79 printf("%s\n", 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)); |
337 | 83 cl -> prev = as -> line_tail; |
84 cl -> insn = -1; | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
85 cl -> as = as; |
354 | 86 cl -> inmod = as -> inmod; |
87 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
|
88 cl -> pragmas = as -> pragmas; |
337 | 89 if (!as -> line_tail) |
90 { | |
91 as -> line_head = cl; | |
92 cl -> addr = lw_expr_build(lw_expr_type_int, 0); | |
93 } | |
94 else | |
95 { | |
96 lw_expr_t te; | |
351 | 97 |
337 | 98 as -> line_tail -> next = cl; |
351 | 99 |
100 // set the line address | |
337 | 101 te = lw_expr_build(lw_expr_type_special, lwasm_expr_linelen, cl -> prev); |
102 cl -> addr = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, cl -> prev -> addr, te); | |
103 lw_expr_destroy(te); | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
104 lw_expr_simplify(cl -> addr, as); |
351 | 105 |
106 // carry DP value forward | |
107 cl -> dpval = cl -> prev -> dpval; | |
337 | 108 } |
109 as -> line_tail = cl; | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
110 as -> cl = cl; |
340 | 111 // 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
|
112 // except a local symbol context break |
340 | 113 if (!*line) |
114 { | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
115 as -> context = lwasm_next_context(as); |
340 | 116 goto nextline; |
117 } | |
118 | |
119 // skip comments | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
120 // commends do not create a context break |
340 | 121 if (*line == '*' || *line == ';' || *line == '#') |
122 goto nextline; | |
123 | |
124 p1 = line; | |
125 if (isdigit(*p1)) | |
126 { | |
127 // skip line number | |
128 while (*p1 && isdigit(*p1)) | |
129 p1++; | |
130 if (!*p1 && !isspace(*p1)) | |
131 p1 = line; | |
132 else if (*p1 && isspace(*p1)) | |
133 p1++; | |
134 } | |
135 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
136 // blank line - context break |
340 | 137 if (!*p1) |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
138 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
139 as -> context = lwasm_next_context(as); |
340 | 140 goto nextline; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
141 } |
340 | 142 |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
143 // comment - no context break |
340 | 144 if (*p1 == '*' || *p1 == ';' || *p1 == '#') |
145 goto nextline; | |
146 | |
147 if (isspace(*p1)) | |
148 { | |
149 for (; *p1 && isspace(*p1); p1++) | |
150 /* do nothing */ ; | |
151 stspace = 1; | |
152 } | |
153 else | |
154 stspace = 0; | |
155 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
156 if (*p1 == '*' || *p1 == ';' || *p1 == '#') |
340 | 157 goto nextline; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
158 if (!*p1) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
159 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
160 // nothing but whitespace - context break |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
161 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
|
162 goto nextline; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
163 } |
340 | 164 |
342 | 165 // find the end of the first token |
340 | 166 for (tok = p1; *p1 && !isspace(*p1) && *p1 != ':'; p1++) |
167 /* do nothing */ ; | |
168 | |
169 if (*p1 == ':' || stspace == 0) | |
170 { | |
171 // have a symbol here | |
172 sym = lw_strndup(tok, p1 - tok); | |
173 if (*p1 == ':') | |
174 p1++; | |
175 for (; *p1 && isspace(*p1); p1++) | |
176 /* do nothing */ ; | |
177 | |
178 for (tok = p1; *p1 && !isspace(*p1); p1++) | |
179 /* do nothing */ ; | |
180 } | |
181 | |
182 cl -> sym = sym; | |
183 cl -> symset = 0; | |
184 | |
185 // tok points to the opcode for the line or NUL if none | |
186 if (*tok) | |
187 { | |
188 // look up operation code | |
189 sym = lw_strndup(tok, p1 - tok); | |
190 for (; *p1 && isspace(p1); p1++) | |
191 /* do nothing */ ; | |
192 | |
193 for (opnum = 0; instab[opnum].opcode; opnum++) | |
194 { | |
195 if (!strcasecmp(instab[opnum].opcode, sym)) | |
196 break; | |
197 } | |
198 | |
199 // p1 points to the start of the operand | |
200 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
201 // 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
|
202 // 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
|
203 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
|
204 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
205 add_macro_line(as, line); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
206 goto linedone; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
207 } |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
208 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
209 // 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
|
210 // 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
|
211 // do nothing |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
212 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
|
213 goto linedone; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
214 |
340 | 215 if (instab[opnum].opcode == NULL) |
216 { | |
217 cl -> insn = -1; | |
218 if (*tok != ';' && *tok != '*') | |
219 { | |
220 // 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
|
221 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
|
222 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
223 // macro expansion failed |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
224 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
|
225 } |
340 | 226 } |
227 } | |
228 else | |
229 { | |
230 cl -> insn = opnum; | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
231 // 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
|
232 if (instab[opnum].parse) |
340 | 233 { |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
234 cl -> len = -1; |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
235 // call parse function |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
236 (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
|
237 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
238 if (*p1 && !isspace(*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 // flag bad operand error |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
241 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
|
242 } |
340 | 243 } |
244 } | |
245 } | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
246 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
247 linedone: |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
248 lw_free(sym); |
340 | 249 |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
250 if (!as -> skipcond && !as -> inmacro) |
340 | 251 { |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
252 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
|
253 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
254 printf("Register symbol %s:", sym); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
255 lw_expr_print(cl -> addr); |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
256 printf("\n"); |
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 |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
262 lwasm_register_error(as, cl, "Bad symbol '%s'", cl -> sym); |
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 } |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
265 |
342 | 266 lw_expr_print(cl -> addr); |
267 printf("\n"); | |
340 | 268 } |
269 | |
270 nextline: | |
332 | 271 lw_free(line); |
347 | 272 |
273 // if we've hit the "end" bit, finish out | |
274 if (as -> endseen) | |
275 return; | |
332 | 276 } |
277 } |