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