Mercurial > hg-old > index.cgi
annotate lwasm/lwasm.c @ 388:8991eb507d2d
Added missing arg-nonnull.h ??? file to repo
author | lost@l-w.ca |
---|---|
date | Wed, 14 Jul 2010 20:17:57 -0600 |
parents | a741d2e4869f |
children | c1d83336e1d1 |
rev | line source |
---|---|
337 | 1 /* |
2 lwasm.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 | |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
22 #define ___lwasm_c_seen___ |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
23 |
337 | 24 #include <config.h> |
25 | |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
26 #include <stdio.h> |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
27 #include <stdarg.h> |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
28 #include <string.h> |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
29 |
337 | 30 #include <lw_expr.h> |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
31 #include <lw_alloc.h> |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
32 #include <lw_string.h> |
337 | 33 |
34 #include "lwasm.h" | |
35 | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
36 void lwasm_register_error(asmstate_t *as, line_t *l, const char *msg, ...); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
37 |
375 | 38 int lwasm_expr_exportable(asmstate_t *as, lw_expr_t expr) |
39 { | |
40 return 0; | |
41 } | |
42 | |
43 int lwasm_expr_exportval(asmstate_t *as, lw_expr_t expr) | |
44 { | |
45 return 0; | |
46 } | |
47 | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
48 lw_expr_t lwasm_evaluate_var(char *var, void *priv) |
337 | 49 { |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
50 asmstate_t *as = (asmstate_t *)priv; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
51 lw_expr_t e; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
52 importlist_t *im; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
53 struct symtabe *s; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
54 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
55 s = lookup_symbol(as, as -> cl, var); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
56 if (s) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
57 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
58 e = lw_expr_build(lw_expr_type_special, lwasm_expr_syment, s); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
59 return e; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
60 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
61 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
62 // undefined here is undefied unless output is object |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
63 if (as -> output_format != OUTPUT_OBJ) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
64 goto nomatch; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
65 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
66 // check for import |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
67 for (im = as -> importlist; im; im = im -> next) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
68 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
69 if (!strcmp(im -> symbol, var)) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
70 break; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
71 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
72 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
73 // check for "undefined" to import automatically |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
74 if (!im && CURPRAGMA(as -> cl, PRAGMA_UNDEFEXTERN)) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
75 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
76 im = lw_alloc(sizeof(importlist_t)); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
77 im -> symbol = lw_strdup(var); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
78 im -> next = as -> importlist; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
79 as -> importlist = im; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
80 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
81 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
82 if (!im) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
83 goto nomatch; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
84 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
85 e = lw_expr_build(lw_expr_type_special, lwasm_expr_import, im); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
86 return e; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
87 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
88 nomatch: |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
89 if (as -> badsymerr) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
90 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
91 lwasm_register_error(as, as -> cl, "Undefined symbol %s", var); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
92 } |
337 | 93 return NULL; |
94 } | |
95 | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
96 lw_expr_t lwasm_evaluate_special(int t, void *ptr, void *priv) |
337 | 97 { |
98 switch (t) | |
99 { | |
375 | 100 case lwasm_expr_secbase: |
101 { | |
385 | 102 // sectiontab_t *s = priv; |
103 asmstate_t *as = priv; | |
104 if (as -> exportcheck && ptr == as -> csect) | |
375 | 105 return lw_expr_build(lw_expr_type_int, 0); |
106 return NULL; | |
107 } | |
108 | |
337 | 109 case lwasm_expr_linelen: |
110 { | |
111 line_t *cl = ptr; | |
112 if (cl -> len == -1) | |
113 return NULL; | |
114 return lw_expr_build(lw_expr_type_int, cl -> len); | |
115 } | |
116 break; | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
117 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
118 case lwasm_expr_lineaddr: |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
119 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
120 line_t *cl = ptr; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
121 if (cl -> addr) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
122 return lw_expr_copy(cl -> addr); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
123 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
124 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
125 } |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
126 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
127 case lwasm_expr_syment: |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
128 { |
371 | 129 struct symtabe *sym = ptr; |
130 return lw_expr_copy(sym -> value); | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
131 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
132 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
133 case lwasm_expr_import: |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
134 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
135 return NULL; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
136 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
137 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
138 case lwasm_expr_nextbp: |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
139 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
140 return NULL; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
141 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
142 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
143 case lwasm_expr_prevbp: |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
144 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
145 return NULL; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
146 } |
337 | 147 } |
148 return NULL; | |
149 } | |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
150 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
151 void lwasm_register_error(asmstate_t *as, line_t *l, const char *msg, ...) |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
152 { |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
153 lwasm_error_t *e; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
154 va_list args; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
155 char errbuff[1024]; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
156 int r; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
157 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
158 if (!l) |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
159 return; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
160 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
161 va_start(args, msg); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
162 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
163 e = lw_alloc(sizeof(lwasm_error_t)); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
164 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
165 e -> next = l -> err; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
166 l -> err = e; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
167 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
168 as -> errorcount++; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
169 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
170 r = vsnprintf(errbuff, 1024, msg, args); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
171 e -> mess = lw_strdup(errbuff); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
172 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
173 va_end(args); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
174 } |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
175 |
351 | 176 void lwasm_register_warning(asmstate_t *as, line_t *l, const char *msg, ...) |
177 { | |
178 lwasm_error_t *e; | |
179 va_list args; | |
180 char errbuff[1024]; | |
181 int r; | |
182 | |
183 if (!l) | |
184 return; | |
185 | |
186 va_start(args, msg); | |
187 | |
188 e = lw_alloc(sizeof(lwasm_error_t)); | |
189 | |
190 e -> next = l -> err; | |
191 l -> err = e; | |
192 | |
193 as -> errorcount++; | |
194 | |
195 r = vsnprintf(errbuff, 1024, msg, args); | |
196 e -> mess = lw_strdup(errbuff); | |
197 | |
198 va_end(args); | |
199 } | |
200 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
201 int lwasm_next_context(asmstate_t *as) |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
202 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
203 int r; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
204 r = as -> nextcontext; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
205 as -> nextcontext++; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
206 return r; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
207 } |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
208 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
209 void lwasm_emit(line_t *cl, int byte) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
210 { |
374 | 211 if (cl -> outputl < 0) |
212 cl -> outputl = 0; | |
213 | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
214 if (cl -> outputl == cl -> outputbl) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
215 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
216 cl -> output = lw_realloc(cl -> output, cl -> outputbl + 8); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
217 cl -> outputbl += 8; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
218 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
219 cl -> output[cl -> outputl++] = byte & 0xff; |
354 | 220 |
221 if (cl -> inmod) | |
222 { | |
223 asmstate_t *as = cl -> as; | |
224 // update module CRC | |
225 // this is a direct transliteration from the nitros9 asm source | |
226 // to C; it can, no doubt, be optimized for 32 bit processing | |
227 byte &= 0xff; | |
228 | |
229 byte ^= (as -> crc)[0]; | |
230 (as -> crc)[0] = (as -> crc)[1]; | |
231 (as -> crc)[1] = (as -> crc)[2]; | |
232 (as -> crc)[1] ^= (byte >> 7); | |
233 (as -> crc)[2] = (byte << 1); | |
234 (as -> crc)[1] ^= (byte >> 2); | |
235 (as -> crc)[2] ^= (byte << 6); | |
236 byte ^= (byte << 1); | |
237 byte ^= (byte << 2); | |
238 byte ^= (byte << 4); | |
239 if (byte & 0x80) | |
240 { | |
241 (as -> crc)[0] ^= 0x80; | |
242 (as -> crc)[2] ^= 0x21; | |
243 } | |
244 } | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
245 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
246 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
247 void lwasm_emitop(line_t *cl, int opc) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
248 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
249 if (opc > 0x100) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
250 lwasm_emit(cl, opc >> 8); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
251 lwasm_emit(cl, opc); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
252 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
253 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
254 lw_expr_t lwasm_parse_term(char **p, void *priv) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
255 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
256 asmstate_t *as = priv; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
257 int val; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
258 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
259 if (!**p) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
260 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
261 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
262 if (**p == '*' || ( |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
263 **p == '.' |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
264 && !((*p)[1] >= 'A' && (*p)[1] <= 'Z') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
265 && !((*p)[1] >= 'a' && (*p)[1] <= 'z') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
266 && !((*p)[1] >= '0' && (*p)[1] <= '9') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
267 )) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
268 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
269 // special "symbol" for current line addr (*, .) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
270 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
271 return lw_expr_build(lw_expr_type_special, lwasm_expr_lineaddr, as -> cl); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
272 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
273 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
274 // branch points |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
275 if (**p == '<') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
276 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
277 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
278 return lw_expr_build(lw_expr_type_special, lwasm_expr_prevbp, as -> cl); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
279 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
280 if (**p == '>') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
281 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
282 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
283 return lw_expr_build(lw_expr_type_special, lwasm_expr_nextbp, as -> cl); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
284 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
285 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
286 // double ascii constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
287 if (**p == '"') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
288 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
289 int v; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
290 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
291 if (!**p) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
292 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
293 if (!*((*p)+1)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
294 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
295 v = (unsigned char)**p << 8 | (unsigned char)*((*p)+1); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
296 (*p) += 2; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
297 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
298 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
299 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
300 if (**p == '\'') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
301 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
302 int v; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
303 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
304 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
305 if (!**p) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
306 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
307 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
308 v = (unsigned char)**p; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
309 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
310 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
311 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
312 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
313 if (**p == '&') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
314 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
315 // decimal constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
316 int v = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
317 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
318 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
319 if (!strchr("0123456789", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
320 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
321 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
322 while (**p && strchr("0123456789", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
323 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
324 val = val * 10 + (**p - '0'); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
325 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
326 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
327 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
328 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
329 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
330 if (**p == '%') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
331 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
332 // binary constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
333 int v = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
334 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
335 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
336 if (**p != '0' && **p != '1') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
337 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
338 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
339 while (**p && (**p == '0' || **p == '1')) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
340 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
341 val = val * 2 + (**p - '0'); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
342 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
343 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
344 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
345 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
346 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
347 if (**p == '$') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
348 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
349 // hexadecimal constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
350 int v = 0, v2; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
351 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
352 if (!strchr("0123456789abcdefABCDEF", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
353 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
354 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
355 while (**p && strchr("0123456789abcdefABCDEF", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
356 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
357 v2 = toupper(**p) - '0'; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
358 if (v2 > 9) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
359 v2 -= 7; |
382 | 360 v = v * 16 + v2; |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
361 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
362 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
363 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
364 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
365 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
366 if (**p == '0' && (*((*p)+1) == 'x' || *((*p)+1) == 'X')) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
367 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
368 // hexadecimal constant, C style |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
369 int v = 0, v2; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
370 (*p)+=2; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
371 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
372 if (!strchr("0123456789abcdefABCDEF", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
373 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
374 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
375 while (**p && strchr("0123456789abcdefABCDEF", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
376 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
377 v2 = toupper(**p) - '0'; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
378 if (v2 > 9) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
379 v2 -= 7; |
382 | 380 v = v * 16 + v2; |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
381 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
382 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
383 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
384 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
385 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
386 if (**p == '@' && (*((*p)+1) >= '0' && *((*p)+1) <= '7')) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
387 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
388 // octal constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
389 int v = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
390 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
391 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
392 if (!strchr("01234567", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
393 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
394 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
395 while (**p && strchr("01234567", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
396 { |
382 | 397 v = v * 8 + (**p - '0'); |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
398 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
399 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
400 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
401 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
402 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
403 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
404 // symbol or bare decimal or suffix constant here |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
405 do |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
406 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
407 int havedol = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
408 int l = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
409 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
410 while ((*p)[l] && strchr(SYMCHARS, (*p)[l])) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
411 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
412 if ((*p)[l] == '$') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
413 havedol = 1; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
414 l++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
415 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
416 if (l == 0) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
417 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
418 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
419 if (havedol || **p < '0' || **p > '9') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
420 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
421 // have a symbol here |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
422 char *sym; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
423 lw_expr_t term; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
424 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
425 sym = lw_strndup(*p, l); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
426 (*p) += l; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
427 term = lw_expr_build(lw_expr_type_var, sym); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
428 lw_free(sym); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
429 return term; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
430 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
431 } while (0); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
432 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
433 if (!**p) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
434 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
435 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
436 // we have a numeric constant here, either decimal or postfix base notation |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
437 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
438 int decval = 0, binval = 0, hexval = 0, octval = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
439 int valtype = 15; // 1 = bin, 2 = oct, 4 = dec, 8 = hex |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
440 int bindone = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
441 int val; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
442 int dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
443 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
444 while (1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
445 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
446 if (!**p || !strchr("0123456789ABCDEFabcdefqhoQHO", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
447 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
448 // we can legally be bin or decimal here |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
449 if (bindone) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
450 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
451 // just finished a binary value |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
452 val = binval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
453 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
454 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
455 else if (valtype & 4) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
456 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
457 val = decval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
458 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
459 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
460 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
461 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
462 // bad value |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
463 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
464 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
465 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
466 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
467 dval = toupper(**p); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
468 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
469 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
470 if (bindone) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
471 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
472 // any characters past "B" means it is not binary |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
473 bindone = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
474 valtype &= 14; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
475 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
476 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
477 switch (dval) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
478 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
479 case 'Q': |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
480 case 'O': |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
481 if (valtype & 2) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
482 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
483 val = octval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
484 valtype = -1; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
485 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
486 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
487 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
488 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
489 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
490 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
491 /* can't get here */ |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
492 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
493 case 'H': |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
494 if (valtype & 8) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
495 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
496 val = hexval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
497 valtype = -1; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
498 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
499 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
500 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
501 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
502 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
503 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
504 /* can't get here */ |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
505 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
506 case 'B': |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
507 // this is a bit of a sticky one since B may be a |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
508 // hex number instead of the end of a binary number |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
509 // so it falls through to the digit case |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
510 if (valtype & 1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
511 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
512 // could still be binary of hex |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
513 bindone = 1; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
514 valtype = 9; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
515 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
516 /* fall through intented */ |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
517 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
518 default: |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
519 // digit |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
520 dval -= '0'; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
521 if (dval > 9) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
522 dval -= 7; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
523 if (valtype & 8) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
524 hexval = hexval * 16 + dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
525 if (valtype & 4) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
526 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
527 if (dval > 9) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
528 valtype &= 11; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
529 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
530 decval = decval * 10 + dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
531 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
532 if (valtype & 2) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
533 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
534 if (dval > 7) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
535 valtype &= 13; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
536 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
537 octval = octval * 8 + dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
538 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
539 if (valtype & 1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
540 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
541 if (dval > 1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
542 valtype &= 14; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
543 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
544 binval = binval * 2 + dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
545 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
546 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
547 if (valtype == -1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
548 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
549 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
550 // return if no more valid types |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
551 if (valtype == 0) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
552 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
553 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
554 val = decval; // in case we fall through |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
555 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
556 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
557 // get here if we have a value |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
558 return lw_expr_build(lw_expr_type_int, val); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
559 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
560 // can't get here |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
561 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
562 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
563 lw_expr_t lwasm_parse_expr(asmstate_t *as, char **p) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
564 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
565 lw_expr_t e; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
566 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
567 e = lw_expr_parse(p, as); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
568 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
569 return e; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
570 } |
347 | 571 |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
572 int lwasm_reduce_expr(asmstate_t *as, lw_expr_t expr) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
573 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
574 lw_expr_simplify(expr, as); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
575 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
576 |
347 | 577 void lwasm_save_expr(line_t *cl, int id, lw_expr_t expr) |
578 { | |
579 struct line_expr_s *e; | |
580 | |
581 for (e = cl -> exprs; e; e = e -> next) | |
582 { | |
583 if (e -> id == id) | |
584 { | |
585 lw_expr_destroy(e -> expr); | |
586 e -> expr = expr; | |
587 return; | |
588 } | |
589 } | |
590 | |
591 e = lw_alloc(sizeof(struct line_expr_s)); | |
592 e -> expr = expr; | |
593 e -> id = id; | |
594 e -> next = cl -> exprs; | |
595 cl -> exprs = e; | |
596 } | |
597 | |
598 lw_expr_t lwasm_fetch_expr(line_t *cl, int id) | |
599 { | |
600 struct line_expr_s *e; | |
601 | |
602 for (e = cl -> exprs; e; e = e -> next) | |
603 { | |
604 if (e -> id == id) | |
605 { | |
606 return e -> expr; | |
607 } | |
608 } | |
609 return NULL; | |
610 } | |
611 | |
612 void skip_operand(char **p) | |
613 { | |
614 for (; **p && !isspace(**p); (*p)++) | |
615 /* do nothing */ ; | |
616 } | |
617 | |
618 int lwasm_emitexpr(line_t *l, lw_expr_t expr, int size) | |
619 { | |
620 int v; | |
621 | |
622 if (lw_expr_istype(expr, lw_expr_type_int)) | |
623 { | |
624 v = lw_expr_intval(expr); | |
625 } | |
348
11a95c6414b4
Added third func to instab to split resolve and emit logic
lost@starbug
parents:
347
diff
changeset
|
626 // handle external/cross-section/incomplete references here |
347 | 627 else |
628 { | |
374 | 629 if (l -> as -> output_format == OUTPUT_OBJ) |
630 { | |
631 reloctab_t *re; | |
376 | 632 lw_expr_t te; |
374 | 633 |
381 | 634 if (size == 4) |
635 { | |
636 // create a two part reference because lwlink doesn't | |
637 // support 32 bit references | |
638 lw_expr_t te2; | |
639 te = lw_expr_build(lw_expr_type_int, 0x10000); | |
640 te2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_divide, expr, te); | |
641 lw_expr_destroy(te); | |
642 | |
643 re = lw_alloc(sizeof(reloctab_t)); | |
644 re -> next = l -> csect -> reloctab; | |
645 l -> csect -> reloctab = re; | |
646 te = lw_expr_build(lw_expr_type_int, l -> outputl); | |
647 re -> offset = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, l -> addr, te); | |
648 lw_expr_destroy(te); | |
649 lwasm_reduce_expr(l -> as, re -> offset); | |
650 re -> expr = te2; | |
651 re -> size = 2; | |
652 | |
653 te = lw_expr_build(lw_expr_type_int, 0xFFFF); | |
654 te2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_bwand, expr, te); | |
655 lw_expr_destroy(te); | |
656 | |
657 re = lw_alloc(sizeof(reloctab_t)); | |
658 re -> next = l -> csect -> reloctab; | |
659 l -> csect -> reloctab = re; | |
660 te = lw_expr_build(lw_expr_type_int, l -> outputl + 2); | |
661 re -> offset = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, l -> addr, te); | |
662 lw_expr_destroy(te); | |
663 lwasm_reduce_expr(l -> as, re -> offset); | |
664 re -> expr = te2; | |
665 re -> size = 2; | |
666 } | |
667 else | |
668 { | |
669 // add "expression" record to section table | |
670 re = lw_alloc(sizeof(reloctab_t)); | |
671 re -> next = l -> csect -> reloctab; | |
672 l -> csect -> reloctab = re; | |
673 te = lw_expr_build(lw_expr_type_int, l -> outputl); | |
674 re -> offset = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, l -> addr, te); | |
675 lw_expr_destroy(te); | |
676 lwasm_reduce_expr(l -> as, re -> offset); | |
677 re -> size = size; | |
678 re -> expr = lw_expr_copy(expr); | |
679 } | |
374 | 680 for (v = 0; v < size; v++) |
681 lwasm_emit(l, 0); | |
682 return 0; | |
683 } | |
347 | 684 lwasm_register_error(l -> as, l, "Expression not fully resolved"); |
685 return -1; | |
686 } | |
687 | |
688 switch (size) | |
689 { | |
690 case 4: | |
691 lwasm_emit(l, v >> 24); | |
692 lwasm_emit(l, v >> 16); | |
693 /* fallthrough intended */ | |
694 | |
695 case 2: | |
696 lwasm_emit(l, v >> 8); | |
697 /* fallthrough intended */ | |
698 | |
699 case 1: | |
700 lwasm_emit(l, v); | |
701 } | |
702 | |
703 return 0; | |
704 } | |
357 | 705 |
706 int lwasm_lookupreg2(const char *regs, char **p) | |
707 { | |
708 int rval = 0; | |
709 | |
710 while (*regs) | |
711 { | |
712 if (toupper(**p) == *regs) | |
713 { | |
714 if (regs[1] == ' ' && !isalpha(*(*p + 1))) | |
715 break; | |
716 if (toupper(*(*p + 1)) == regs[1]) | |
717 break; | |
718 } | |
719 regs += 2; | |
720 rval++; | |
721 } | |
722 if (!*regs) | |
723 return -1; | |
724 if (regs[1] == ' ') | |
725 (*p)++; | |
726 else | |
727 (*p) += 2; | |
728 return rval; | |
729 } | |
359 | 730 |
731 int lwasm_lookupreg3(const char *regs, char **p) | |
732 { | |
733 int rval = 0; | |
734 | |
735 while (*regs) | |
736 { | |
737 if (toupper(**p) == *regs) | |
738 { | |
739 if (regs[1] == ' ' && !isalpha(*(*p + 1))) | |
740 break; | |
741 if (toupper(*(*p + 1)) == regs[1]) | |
742 { | |
743 if (regs[2] == ' ' && !isalpha(*(*p + 2))) | |
744 break; | |
745 if (toupper(*(*p + 2)) == regs[2]) | |
746 break; | |
747 } | |
748 } | |
749 regs += 3; | |
750 rval++; | |
751 } | |
752 if (!*regs) | |
753 return -1; | |
754 if (regs[1] == ' ') | |
755 (*p)++; | |
756 else if (regs[2] == ' ') | |
757 (*p) += 2; | |
758 else | |
759 (*p) += 3; | |
760 return rval; | |
761 } | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
762 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
763 void lwasm_show_errors(asmstate_t *as) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
764 { |
382 | 765 line_t *cl; |
766 lwasm_error_t *e; | |
767 | |
768 for (cl = as -> line_head; cl; cl = cl -> next) | |
769 { | |
770 if (!(cl -> err) && !(cl -> warn)) | |
771 continue; | |
772 for (e = cl -> err; e; e = e -> next) | |
773 { | |
774 fprintf(stderr, "ERROR: %s\n", e -> mess); | |
775 } | |
776 for (e = cl -> warn; e; e = e -> next) | |
777 { | |
778 fprintf(stderr, "WARNING: %s\n", e -> mess); | |
779 } | |
780 | |
781 fprintf(stderr, " LINE: %s\n", cl -> ltext); | |
782 } | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
783 } |
387
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
784 |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
785 /* |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
786 this does any passes and other gymnastics that might be useful |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
787 to see if an expression reduces early |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
788 */ |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
789 extern void do_pass3(asmstate_t *as); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
790 extern void do_pass4_aux(asmstate_t *as, int force); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
791 |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
792 void lwasm_interim_reduce(asmstate_t *as) |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
793 { |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
794 do_pass3(as); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
795 // do_pass4_aux(as, 0); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
796 } |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
797 |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
798 lw_expr_t lwasm_parse_cond(asmstate_t *as, char **p) |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
799 { |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
800 lw_expr_t e; |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
801 |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
802 debug_message(as, 250, "Parsing condition"); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
803 e = lwasm_parse_expr(as, p); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
804 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e)); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
805 |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
806 if (!e) |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
807 { |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
808 lwasm_register_error(as, as -> cl, "Bad expression"); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
809 return NULL; |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
810 } |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
811 |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
812 /* we need to simplify the expression here */ |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
813 debug_message(as, 250, "Doing interim reductions"); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
814 lwasm_interim_reduce(as); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
815 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e)); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
816 debug_message(as, 250, "Reducing expression"); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
817 lwasm_reduce_expr(as, e); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
818 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e)); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
819 /* lwasm_reduce_expr(as, e); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
820 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e)); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
821 lwasm_reduce_expr(as, e); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
822 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e)); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
823 lwasm_reduce_expr(as, e); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
824 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e)); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
825 */ |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
826 |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
827 lwasm_save_expr(as -> cl, 4242, e); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
828 |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
829 if (!lw_expr_istype(e, lw_expr_type_int)) |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
830 { |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
831 debug_message(as, 250, "Non-constant expression"); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
832 lwasm_register_error(as, as -> cl, "Conditions must be constant on pass 1"); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
833 return NULL; |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
834 } |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
835 debug_message(as, 250, "Returning expression"); |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
836 return e; |
a741d2e4869f
Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents:
385
diff
changeset
|
837 } |