Mercurial > hg-old > index.cgi
annotate lwasm/lwasm.c @ 384:38b50ce6967a
Made --list and --depend work
author | lost@starbug |
---|---|
date | Sat, 15 May 2010 20:46:04 -0600 |
parents | eacdae8a1575 |
children | cf8c92d70eb1 |
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 { | |
102 line_t *cl = ptr; | |
103 if (cl -> as -> exportcheck && ptr == cl -> as -> csect) | |
104 return lw_expr_build(lw_expr_type_int, 0); | |
105 return NULL; | |
106 } | |
107 | |
337 | 108 case lwasm_expr_linelen: |
109 { | |
110 line_t *cl = ptr; | |
111 if (cl -> len == -1) | |
112 return NULL; | |
113 return lw_expr_build(lw_expr_type_int, cl -> len); | |
114 } | |
115 break; | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
116 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
117 case lwasm_expr_lineaddr: |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
118 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
119 line_t *cl = ptr; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
120 if (cl -> addr) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
121 return lw_expr_copy(cl -> addr); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
122 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
123 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
124 } |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
125 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
126 case lwasm_expr_syment: |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
127 { |
371 | 128 struct symtabe *sym = ptr; |
129 return lw_expr_copy(sym -> value); | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
130 } |
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 case lwasm_expr_import: |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
133 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
134 return NULL; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
135 } |
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 case lwasm_expr_nextbp: |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
138 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
139 return NULL; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
140 } |
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 case lwasm_expr_prevbp: |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
143 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
144 return NULL; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
145 } |
337 | 146 } |
147 return NULL; | |
148 } | |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
149 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
150 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
|
151 { |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
152 lwasm_error_t *e; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
153 va_list args; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
154 char errbuff[1024]; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
155 int r; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
156 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
157 if (!l) |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
158 return; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
159 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
160 va_start(args, msg); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
161 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
162 e = lw_alloc(sizeof(lwasm_error_t)); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
163 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
164 e -> next = l -> err; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
165 l -> err = e; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
166 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
167 as -> errorcount++; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
168 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
169 r = vsnprintf(errbuff, 1024, msg, args); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
170 e -> mess = lw_strdup(errbuff); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
171 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
172 va_end(args); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
337
diff
changeset
|
173 } |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
174 |
351 | 175 void lwasm_register_warning(asmstate_t *as, line_t *l, const char *msg, ...) |
176 { | |
177 lwasm_error_t *e; | |
178 va_list args; | |
179 char errbuff[1024]; | |
180 int r; | |
181 | |
182 if (!l) | |
183 return; | |
184 | |
185 va_start(args, msg); | |
186 | |
187 e = lw_alloc(sizeof(lwasm_error_t)); | |
188 | |
189 e -> next = l -> err; | |
190 l -> err = e; | |
191 | |
192 as -> errorcount++; | |
193 | |
194 r = vsnprintf(errbuff, 1024, msg, args); | |
195 e -> mess = lw_strdup(errbuff); | |
196 | |
197 va_end(args); | |
198 } | |
199 | |
345
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
200 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
|
201 { |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
202 int r; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
203 r = as -> nextcontext; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
204 as -> nextcontext++; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
205 return r; |
7416c3f9c321
Basic macro processor ported forward; added context break handling for local symbols
lost@starbug
parents:
344
diff
changeset
|
206 } |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
207 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
208 void lwasm_emit(line_t *cl, int byte) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
209 { |
374 | 210 if (cl -> outputl < 0) |
211 cl -> outputl = 0; | |
212 | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
213 if (cl -> outputl == cl -> outputbl) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
214 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
215 cl -> output = lw_realloc(cl -> output, cl -> outputbl + 8); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
216 cl -> outputbl += 8; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
217 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
218 cl -> output[cl -> outputl++] = byte & 0xff; |
354 | 219 |
220 if (cl -> inmod) | |
221 { | |
222 asmstate_t *as = cl -> as; | |
223 // update module CRC | |
224 // this is a direct transliteration from the nitros9 asm source | |
225 // to C; it can, no doubt, be optimized for 32 bit processing | |
226 byte &= 0xff; | |
227 | |
228 byte ^= (as -> crc)[0]; | |
229 (as -> crc)[0] = (as -> crc)[1]; | |
230 (as -> crc)[1] = (as -> crc)[2]; | |
231 (as -> crc)[1] ^= (byte >> 7); | |
232 (as -> crc)[2] = (byte << 1); | |
233 (as -> crc)[1] ^= (byte >> 2); | |
234 (as -> crc)[2] ^= (byte << 6); | |
235 byte ^= (byte << 1); | |
236 byte ^= (byte << 2); | |
237 byte ^= (byte << 4); | |
238 if (byte & 0x80) | |
239 { | |
240 (as -> crc)[0] ^= 0x80; | |
241 (as -> crc)[2] ^= 0x21; | |
242 } | |
243 } | |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
244 } |
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 void lwasm_emitop(line_t *cl, int opc) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
247 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
248 if (opc > 0x100) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
249 lwasm_emit(cl, opc >> 8); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
250 lwasm_emit(cl, opc); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
251 } |
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 lw_expr_t lwasm_parse_term(char **p, void *priv) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
254 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
255 asmstate_t *as = priv; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
256 int val; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
257 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
258 if (!**p) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
259 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
260 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
261 if (**p == '*' || ( |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
262 **p == '.' |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
263 && !((*p)[1] >= 'A' && (*p)[1] <= 'Z') |
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] >= '0' && (*p)[1] <= '9') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
266 )) |
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 // special "symbol" for current line addr (*, .) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
269 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
270 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
|
271 } |
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 // branch points |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
274 if (**p == '<') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
275 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
276 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
277 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
|
278 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
279 if (**p == '>') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
280 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
281 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
282 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
|
283 } |
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 // double ascii constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
286 if (**p == '"') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
287 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
288 int v; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
289 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
290 if (!**p) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
291 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
292 if (!*((*p)+1)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
293 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
294 v = (unsigned char)**p << 8 | (unsigned char)*((*p)+1); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
295 (*p) += 2; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
296 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
297 } |
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 if (**p == '\'') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
300 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
301 int v; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
302 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
303 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
304 if (!**p) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
305 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
306 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
307 v = (unsigned char)**p; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
308 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
309 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
310 } |
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 if (**p == '&') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
313 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
314 // decimal constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
315 int v = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
316 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
317 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
318 if (!strchr("0123456789", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
319 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
320 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
321 while (**p && strchr("0123456789", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
322 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
323 val = val * 10 + (**p - '0'); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
324 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
325 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
326 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
327 } |
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 if (**p == '%') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
330 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
331 // binary constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
332 int v = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
333 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
334 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
335 if (**p != '0' && **p != '1') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
336 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
337 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
338 while (**p && (**p == '0' || **p == '1')) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
339 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
340 val = val * 2 + (**p - '0'); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
341 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
342 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
343 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
344 } |
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 if (**p == '$') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
347 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
348 // hexadecimal constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
349 int v = 0, v2; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
350 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
351 if (!strchr("0123456789abcdefABCDEF", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
352 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
353 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
354 while (**p && strchr("0123456789abcdefABCDEF", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
355 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
356 v2 = toupper(**p) - '0'; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
357 if (v2 > 9) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
358 v2 -= 7; |
382 | 359 v = v * 16 + v2; |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
360 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
361 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
362 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
363 } |
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 if (**p == '0' && (*((*p)+1) == 'x' || *((*p)+1) == 'X')) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
366 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
367 // hexadecimal constant, C style |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
368 int v = 0, v2; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
369 (*p)+=2; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
370 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
371 if (!strchr("0123456789abcdefABCDEF", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
372 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
373 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
374 while (**p && strchr("0123456789abcdefABCDEF", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
375 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
376 v2 = toupper(**p) - '0'; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
377 if (v2 > 9) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
378 v2 -= 7; |
382 | 379 v = v * 16 + v2; |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
380 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
381 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
382 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
383 } |
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 if (**p == '@' && (*((*p)+1) >= '0' && *((*p)+1) <= '7')) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
386 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
387 // octal constant |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
388 int v = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
389 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
390 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
391 if (!strchr("01234567", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
392 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
393 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
394 while (**p && strchr("01234567", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
395 { |
382 | 396 v = v * 8 + (**p - '0'); |
346
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
397 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
398 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
399 return lw_expr_build(lw_expr_type_int, v); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
400 } |
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 // symbol or bare decimal or suffix constant here |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
404 do |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
405 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
406 int havedol = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
407 int l = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
408 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
409 while ((*p)[l] && strchr(SYMCHARS, (*p)[l])) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
410 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
411 if ((*p)[l] == '$') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
412 havedol = 1; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
413 l++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
414 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
415 if (l == 0) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
416 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
417 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
418 if (havedol || **p < '0' || **p > '9') |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
419 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
420 // have a symbol here |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
421 char *sym; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
422 lw_expr_t term; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
423 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
424 sym = lw_strndup(*p, l); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
425 (*p) += l; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
426 term = lw_expr_build(lw_expr_type_var, sym); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
427 lw_free(sym); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
428 return term; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
429 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
430 } while (0); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
431 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
432 if (!**p) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
433 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
434 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
435 // 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
|
436 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
437 int decval = 0, binval = 0, hexval = 0, octval = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
438 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
|
439 int bindone = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
440 int val; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
441 int dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
442 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
443 while (1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
444 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
445 if (!**p || !strchr("0123456789ABCDEFabcdefqhoQHO", **p)) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
446 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
447 // we can legally be bin or decimal here |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
448 if (bindone) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
449 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
450 // just finished a binary value |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
451 val = binval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
452 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
453 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
454 else if (valtype & 4) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
455 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
456 val = decval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
457 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
458 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
459 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
460 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
461 // bad value |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
462 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
463 } |
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 dval = toupper(**p); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
467 (*p)++; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
468 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
469 if (bindone) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
470 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
471 // any characters past "B" means it is not binary |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
472 bindone = 0; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
473 valtype &= 14; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
474 } |
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 switch (dval) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
477 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
478 case 'Q': |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
479 case 'O': |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
480 if (valtype & 2) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
481 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
482 val = octval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
483 valtype = -1; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
484 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
485 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
486 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
487 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
488 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
489 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
490 /* can't get here */ |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
491 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
492 case 'H': |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
493 if (valtype & 8) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
494 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
495 val = hexval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
496 valtype = -1; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
497 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
498 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
499 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
500 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
501 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
502 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
503 /* can't get here */ |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
504 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
505 case 'B': |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
506 // 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
|
507 // hex number instead of the end of a binary number |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
508 // so it falls through to the digit case |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
509 if (valtype & 1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
510 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
511 // could still be binary of hex |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
512 bindone = 1; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
513 valtype = 9; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
514 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
515 /* fall through intented */ |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
516 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
517 default: |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
518 // digit |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
519 dval -= '0'; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
520 if (dval > 9) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
521 dval -= 7; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
522 if (valtype & 8) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
523 hexval = hexval * 16 + dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
524 if (valtype & 4) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
525 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
526 if (dval > 9) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
527 valtype &= 11; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
528 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
529 decval = decval * 10 + dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
530 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
531 if (valtype & 2) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
532 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
533 if (dval > 7) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
534 valtype &= 13; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
535 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
536 octval = octval * 8 + dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
537 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
538 if (valtype & 1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
539 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
540 if (dval > 1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
541 valtype &= 14; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
542 else |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
543 binval = binval * 2 + dval; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
544 } |
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 if (valtype == -1) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
547 break; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
548 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
549 // return if no more valid types |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
550 if (valtype == 0) |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
551 return NULL; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
552 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
553 val = decval; // in case we fall through |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
554 } |
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 // get here if we have a value |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
557 return lw_expr_build(lw_expr_type_int, val); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
558 } |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
559 // can't get here |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
560 } |
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 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
|
563 { |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
564 lw_expr_t e; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
565 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
566 e = lw_expr_parse(p, as); |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
567 |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
568 return e; |
a82c55070624
Added expression parsing infrastructure and misc fixes
lost@starbug
parents:
345
diff
changeset
|
569 } |
347 | 570 |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
571 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
|
572 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
573 lw_expr_simplify(expr, as); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
574 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
575 |
347 | 576 void lwasm_save_expr(line_t *cl, int id, lw_expr_t expr) |
577 { | |
578 struct line_expr_s *e; | |
579 | |
580 for (e = cl -> exprs; e; e = e -> next) | |
581 { | |
582 if (e -> id == id) | |
583 { | |
584 lw_expr_destroy(e -> expr); | |
585 e -> expr = expr; | |
586 return; | |
587 } | |
588 } | |
589 | |
590 e = lw_alloc(sizeof(struct line_expr_s)); | |
591 e -> expr = expr; | |
592 e -> id = id; | |
593 e -> next = cl -> exprs; | |
594 cl -> exprs = e; | |
595 } | |
596 | |
597 lw_expr_t lwasm_fetch_expr(line_t *cl, int id) | |
598 { | |
599 struct line_expr_s *e; | |
600 | |
601 for (e = cl -> exprs; e; e = e -> next) | |
602 { | |
603 if (e -> id == id) | |
604 { | |
605 return e -> expr; | |
606 } | |
607 } | |
608 return NULL; | |
609 } | |
610 | |
611 void skip_operand(char **p) | |
612 { | |
613 for (; **p && !isspace(**p); (*p)++) | |
614 /* do nothing */ ; | |
615 } | |
616 | |
617 int lwasm_emitexpr(line_t *l, lw_expr_t expr, int size) | |
618 { | |
619 int v; | |
620 | |
621 if (lw_expr_istype(expr, lw_expr_type_int)) | |
622 { | |
623 v = lw_expr_intval(expr); | |
624 } | |
348
11a95c6414b4
Added third func to instab to split resolve and emit logic
lost@starbug
parents:
347
diff
changeset
|
625 // handle external/cross-section/incomplete references here |
347 | 626 else |
627 { | |
374 | 628 if (l -> as -> output_format == OUTPUT_OBJ) |
629 { | |
630 reloctab_t *re; | |
376 | 631 lw_expr_t te; |
374 | 632 |
381 | 633 if (size == 4) |
634 { | |
635 // create a two part reference because lwlink doesn't | |
636 // support 32 bit references | |
637 lw_expr_t te2; | |
638 te = lw_expr_build(lw_expr_type_int, 0x10000); | |
639 te2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_divide, expr, te); | |
640 lw_expr_destroy(te); | |
641 | |
642 re = lw_alloc(sizeof(reloctab_t)); | |
643 re -> next = l -> csect -> reloctab; | |
644 l -> csect -> reloctab = re; | |
645 te = lw_expr_build(lw_expr_type_int, l -> outputl); | |
646 re -> offset = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, l -> addr, te); | |
647 lw_expr_destroy(te); | |
648 lwasm_reduce_expr(l -> as, re -> offset); | |
649 re -> expr = te2; | |
650 re -> size = 2; | |
651 | |
652 te = lw_expr_build(lw_expr_type_int, 0xFFFF); | |
653 te2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_bwand, expr, te); | |
654 lw_expr_destroy(te); | |
655 | |
656 re = lw_alloc(sizeof(reloctab_t)); | |
657 re -> next = l -> csect -> reloctab; | |
658 l -> csect -> reloctab = re; | |
659 te = lw_expr_build(lw_expr_type_int, l -> outputl + 2); | |
660 re -> offset = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, l -> addr, te); | |
661 lw_expr_destroy(te); | |
662 lwasm_reduce_expr(l -> as, re -> offset); | |
663 re -> expr = te2; | |
664 re -> size = 2; | |
665 } | |
666 else | |
667 { | |
668 // add "expression" record to section table | |
669 re = lw_alloc(sizeof(reloctab_t)); | |
670 re -> next = l -> csect -> reloctab; | |
671 l -> csect -> reloctab = re; | |
672 te = lw_expr_build(lw_expr_type_int, l -> outputl); | |
673 re -> offset = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, l -> addr, te); | |
674 lw_expr_destroy(te); | |
675 lwasm_reduce_expr(l -> as, re -> offset); | |
676 re -> size = size; | |
677 re -> expr = lw_expr_copy(expr); | |
678 } | |
374 | 679 for (v = 0; v < size; v++) |
680 lwasm_emit(l, 0); | |
681 return 0; | |
682 } | |
347 | 683 lwasm_register_error(l -> as, l, "Expression not fully resolved"); |
684 return -1; | |
685 } | |
686 | |
687 switch (size) | |
688 { | |
689 case 4: | |
690 lwasm_emit(l, v >> 24); | |
691 lwasm_emit(l, v >> 16); | |
692 /* fallthrough intended */ | |
693 | |
694 case 2: | |
695 lwasm_emit(l, v >> 8); | |
696 /* fallthrough intended */ | |
697 | |
698 case 1: | |
699 lwasm_emit(l, v); | |
700 } | |
701 | |
702 return 0; | |
703 } | |
357 | 704 |
705 int lwasm_lookupreg2(const char *regs, char **p) | |
706 { | |
707 int rval = 0; | |
708 | |
709 while (*regs) | |
710 { | |
711 if (toupper(**p) == *regs) | |
712 { | |
713 if (regs[1] == ' ' && !isalpha(*(*p + 1))) | |
714 break; | |
715 if (toupper(*(*p + 1)) == regs[1]) | |
716 break; | |
717 } | |
718 regs += 2; | |
719 rval++; | |
720 } | |
721 if (!*regs) | |
722 return -1; | |
723 if (regs[1] == ' ') | |
724 (*p)++; | |
725 else | |
726 (*p) += 2; | |
727 return rval; | |
728 } | |
359 | 729 |
730 int lwasm_lookupreg3(const char *regs, char **p) | |
731 { | |
732 int rval = 0; | |
733 | |
734 while (*regs) | |
735 { | |
736 if (toupper(**p) == *regs) | |
737 { | |
738 if (regs[1] == ' ' && !isalpha(*(*p + 1))) | |
739 break; | |
740 if (toupper(*(*p + 1)) == regs[1]) | |
741 { | |
742 if (regs[2] == ' ' && !isalpha(*(*p + 2))) | |
743 break; | |
744 if (toupper(*(*p + 2)) == regs[2]) | |
745 break; | |
746 } | |
747 } | |
748 regs += 3; | |
749 rval++; | |
750 } | |
751 if (!*regs) | |
752 return -1; | |
753 if (regs[1] == ' ') | |
754 (*p)++; | |
755 else if (regs[2] == ' ') | |
756 (*p) += 2; | |
757 else | |
758 (*p) += 3; | |
759 return rval; | |
760 } | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
761 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
762 void lwasm_show_errors(asmstate_t *as) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
763 { |
382 | 764 line_t *cl; |
765 lwasm_error_t *e; | |
766 | |
767 for (cl = as -> line_head; cl; cl = cl -> next) | |
768 { | |
769 if (!(cl -> err) && !(cl -> warn)) | |
770 continue; | |
771 for (e = cl -> err; e; e = e -> next) | |
772 { | |
773 fprintf(stderr, "ERROR: %s\n", e -> mess); | |
774 } | |
775 for (e = cl -> warn; e; e = e -> next) | |
776 { | |
777 fprintf(stderr, "WARNING: %s\n", e -> mess); | |
778 } | |
779 | |
780 fprintf(stderr, " LINE: %s\n", cl -> ltext); | |
781 } | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
359
diff
changeset
|
782 } |