Mercurial > hg-old > index.cgi
annotate lwasm/pseudo.c @ 349:dcd2978a7d18
More pseudo ops
author | lost@starbug |
---|---|
date | Sat, 27 Mar 2010 22:44:46 -0600 |
parents | 11a95c6414b4 |
children | f5666775d76f |
rev | line source |
---|---|
347 | 1 /* |
2 pseudo.c | |
3 Copyright © 2010 William Astle | |
4 | |
5 This file is part of LWASM. | |
6 | |
7 LWASM is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 */ | |
21 | |
22 #include <config.h> | |
23 | |
24 #include "lwasm.h" | |
25 #include "instab.h" | |
26 | |
349 | 27 #include "lw_string.h" |
28 | |
347 | 29 // for "end" |
30 PARSEFUNC(pseudo_parse_end) | |
31 { | |
32 lw_expr_t addr; | |
33 | |
34 as -> endseen = 1; | |
35 | |
36 if (as -> output_format != OUTPUT_DECB) | |
37 { | |
38 skip_operand(p); | |
39 return; | |
40 } | |
41 | |
42 addr = lwasm_parse_expr(as, p); | |
43 if (!addr) | |
44 { | |
45 lwasm_register_error(as, l, "Bad expression"); | |
46 addr = lw_expr_build(lw_expr_type_int, 0); | |
47 } | |
48 lwasm_save_expr(l, 0, addr); | |
49 } | |
50 | |
348
11a95c6414b4
Added third func to instab to split resolve and emit logic
lost@starbug
parents:
347
diff
changeset
|
51 EMITFUNC(pseudo_emit_end) |
347 | 52 { |
53 lw_expr_t addr; | |
54 | |
55 addr = lwasm_fetch_expr(l, 0); | |
56 | |
57 if (addr) | |
58 { | |
59 if (!lw_expr_istype(addr, lw_expr_type_int)) | |
60 lwasm_register_error(as, l, "Exec address not constant!"); | |
61 else | |
62 as -> execaddr = lw_expr_intval(addr); | |
63 } | |
64 as -> endseen = 1; | |
65 } | |
66 | |
67 PARSEFUNC(pseudo_parse_fcb) | |
68 { | |
69 int i = 0; | |
70 lw_expr_t e; | |
71 | |
72 for (;;) | |
73 { | |
74 e = lwasm_parse_expr(as, p); | |
75 if (!e) | |
76 { | |
77 lwasm_register_error(as, l, "Bad expression (#%s)", i); | |
78 break; | |
79 } | |
80 lwasm_save_expr(l, i, e); | |
81 i++; | |
82 } | |
83 | |
84 l -> len = i; | |
85 } | |
86 | |
348
11a95c6414b4
Added third func to instab to split resolve and emit logic
lost@starbug
parents:
347
diff
changeset
|
87 EMITFUNC(pseudo_emit_fcb) |
347 | 88 { |
89 int i; | |
90 lw_expr_t e; | |
91 int v; | |
92 | |
93 for (i = 0; i < l -> len; i++) | |
94 { | |
95 e = lwasm_fetch_expr(l, i); | |
96 lwasm_emitexpr(l, e, 1); | |
97 } | |
98 } | |
99 | |
100 PARSEFUNC(pseudo_parse_fdb) | |
101 { | |
102 int i = 0; | |
103 lw_expr_t e; | |
104 | |
105 for (;;) | |
106 { | |
107 e = lwasm_parse_expr(as, p); | |
108 if (!e) | |
109 { | |
110 lwasm_register_error(as, l, "Bad expression (#%s)", i); | |
111 break; | |
112 } | |
113 lwasm_save_expr(l, i, e); | |
114 i++; | |
115 } | |
116 | |
117 l -> len = i * 2; | |
118 } | |
119 | |
348
11a95c6414b4
Added third func to instab to split resolve and emit logic
lost@starbug
parents:
347
diff
changeset
|
120 EMITFUNC(pseudo_emit_fdb) |
347 | 121 { |
122 int i; | |
123 lw_expr_t e; | |
124 int v; | |
125 | |
126 for (i = 0; i < (l -> len)/2; i++) | |
127 { | |
128 e = lwasm_fetch_expr(l, i); | |
129 lwasm_emitexpr(l, e, 2); | |
130 } | |
131 } | |
132 | |
133 PARSEFUNC(pseudo_parse_fqb) | |
134 { | |
135 int i = 0; | |
136 lw_expr_t e; | |
137 | |
138 for (;;) | |
139 { | |
140 e = lwasm_parse_expr(as, p); | |
141 if (!e) | |
142 { | |
143 lwasm_register_error(as, l, "Bad expression (#%s)", i); | |
144 break; | |
145 } | |
146 lwasm_save_expr(l, i, e); | |
147 i++; | |
148 } | |
149 | |
150 l -> len = i * 4; | |
151 } | |
152 | |
348
11a95c6414b4
Added third func to instab to split resolve and emit logic
lost@starbug
parents:
347
diff
changeset
|
153 EMITFUNC(pseudo_emit_fqb) |
347 | 154 { |
155 int i; | |
156 lw_expr_t e; | |
157 int v; | |
158 | |
159 for (i = 0; i < (l -> len)/4; i++) | |
160 { | |
161 e = lwasm_fetch_expr(l, i); | |
162 lwasm_emitexpr(l, e, 4); | |
163 } | |
164 } | |
349 | 165 |
166 PARSEFUNC(pseudo_parse_fcc) | |
167 { | |
168 char delim; | |
169 int i; | |
170 | |
171 if (!**p) | |
172 { | |
173 lwasm_register_error(as, l, "Bad operand"); | |
174 return; | |
175 } | |
176 | |
177 delim = **p; | |
178 (*p)++; | |
179 | |
180 for (i = 0; (*p)[i] && (*p)[i] != delim; i++) | |
181 /* do nothing */ ; | |
182 | |
183 if ((*p)[i] != delim) | |
184 { | |
185 lwasm_register_error(as, l, "Bad operand"); | |
186 return; | |
187 } | |
188 | |
189 l -> lstr = lw_strndup(*p, i); | |
190 (*p) += i + 1; | |
191 | |
192 l -> len = i; | |
193 } | |
194 | |
195 EMITFUNC(pseudo_emit_fcc) | |
196 { | |
197 int i; | |
198 | |
199 for (i = 0; i < l -> len; i++) | |
200 lwasm_emit(l, l -> lstr[i]); | |
201 } | |
202 | |
203 PARSEFUNC(pseudo_parse_fcs) | |
204 { | |
205 char delim; | |
206 int i; | |
207 | |
208 if (!**p) | |
209 { | |
210 lwasm_register_error(as, l, "Bad operand"); | |
211 return; | |
212 } | |
213 | |
214 delim = **p; | |
215 (*p)++; | |
216 | |
217 for (i = 0; (*p)[i] && (*p)[i] != delim; i++) | |
218 /* do nothing */ ; | |
219 | |
220 if ((*p)[i] != delim) | |
221 { | |
222 lwasm_register_error(as, l, "Bad operand"); | |
223 return; | |
224 } | |
225 | |
226 l -> lstr = lw_strndup(*p, i); | |
227 (*p) += i + 1; | |
228 | |
229 l -> len = i; | |
230 } | |
231 | |
232 EMITFUNC(pseudo_emit_fcs) | |
233 { | |
234 int i; | |
235 | |
236 for (i = 0; i < l -> len - 1; i++) | |
237 lwasm_emit(l, l -> lstr[i]); | |
238 lwasm_emit(l, l -> lstr[i] | 0x80); | |
239 } | |
240 | |
241 PARSEFUNC(pseudo_parse_fcn) | |
242 { | |
243 char delim; | |
244 int i; | |
245 | |
246 if (!**p) | |
247 { | |
248 lwasm_register_error(as, l, "Bad operand"); | |
249 return; | |
250 } | |
251 | |
252 delim = **p; | |
253 (*p)++; | |
254 | |
255 for (i = 0; (*p)[i] && (*p)[i] != delim; i++) | |
256 /* do nothing */ ; | |
257 | |
258 if ((*p)[i] != delim) | |
259 { | |
260 lwasm_register_error(as, l, "Bad operand"); | |
261 return; | |
262 } | |
263 | |
264 l -> lstr = lw_strndup(*p, i); | |
265 (*p) += i + 1; | |
266 | |
267 l -> len = i; | |
268 } | |
269 | |
270 EMITFUNC(pseudo_emit_fcn) | |
271 { | |
272 int i; | |
273 | |
274 for (i = 0; i < l -> len; i++) | |
275 lwasm_emit(l, l -> lstr[i]); | |
276 lwasm_emit(l, 0); | |
277 } | |
278 | |
279 PARSEFUNC(pseudo_parse_rmb) | |
280 { | |
281 lw_expr_t expr; | |
282 | |
283 expr = lwasm_parse_expr(as, p); | |
284 if (!expr) | |
285 { | |
286 lwasm_register_error(as, l, "Bad expression"); | |
287 } | |
288 | |
289 lwasm_save_expr(l, 0, expr); | |
290 } | |
291 | |
292 RESOLVEFUNC(pseudo_resolve_rmb) | |
293 { | |
294 lw_expr_t expr; | |
295 | |
296 if (l -> len >= 0) | |
297 return; | |
298 | |
299 expr = lwasm_fetch_expr(l, 0); | |
300 | |
301 if (lw_expr_istype(expr, lw_expr_type_int)) | |
302 { | |
303 l -> len = lw_expr_intval(expr); | |
304 } | |
305 } | |
306 | |
307 EMITFUNC(pseudo_emit_rmb) | |
308 { | |
309 if (l -> len < 0) | |
310 lwasm_register_error(as, l, "Expression not constant"); | |
311 } | |
312 | |
313 PARSEFUNC(pseudo_parse_rmd) | |
314 { | |
315 lw_expr_t expr; | |
316 | |
317 expr = lwasm_parse_expr(as, p); | |
318 if (!expr) | |
319 { | |
320 lwasm_register_error(as, l, "Bad expression"); | |
321 } | |
322 | |
323 lwasm_save_expr(l, 0, expr); | |
324 } | |
325 | |
326 RESOLVEFUNC(pseudo_resolve_rmd) | |
327 { | |
328 lw_expr_t expr; | |
329 | |
330 if (l -> len >= 0) | |
331 return; | |
332 | |
333 expr = lwasm_fetch_expr(l, 0); | |
334 | |
335 if (lw_expr_istype(expr, lw_expr_type_int)) | |
336 { | |
337 l -> len = lw_expr_intval(expr) * 2; | |
338 } | |
339 } | |
340 | |
341 EMITFUNC(pseudo_emit_rmd) | |
342 { | |
343 if (l -> len < 0) | |
344 lwasm_register_error(as, l, "Expression not constant"); | |
345 } | |
346 | |
347 | |
348 PARSEFUNC(pseudo_parse_rmq) | |
349 { | |
350 lw_expr_t expr; | |
351 | |
352 expr = lwasm_parse_expr(as, p); | |
353 if (!expr) | |
354 { | |
355 lwasm_register_error(as, l, "Bad expression"); | |
356 } | |
357 | |
358 lwasm_save_expr(l, 0, expr); | |
359 } | |
360 | |
361 RESOLVEFUNC(pseudo_resolve_rmq) | |
362 { | |
363 lw_expr_t expr; | |
364 | |
365 if (l -> len >= 0) | |
366 return; | |
367 | |
368 expr = lwasm_fetch_expr(l, 0); | |
369 | |
370 if (lw_expr_istype(expr, lw_expr_type_int)) | |
371 { | |
372 l -> len = lw_expr_intval(expr) * 4; | |
373 } | |
374 } | |
375 | |
376 EMITFUNC(pseudo_emit_rmq) | |
377 { | |
378 if (l -> len < 0) | |
379 lwasm_register_error(as, l, "Expression not constant"); | |
380 } | |
381 | |
382 | |
383 PARSEFUNC(pseudo_parse_zmq) | |
384 { | |
385 lw_expr_t expr; | |
386 | |
387 expr = lwasm_parse_expr(as, p); | |
388 if (!expr) | |
389 { | |
390 lwasm_register_error(as, l, "Bad expression"); | |
391 } | |
392 | |
393 lwasm_save_expr(l, 0, expr); | |
394 } | |
395 | |
396 RESOLVEFUNC(pseudo_resolve_zmq) | |
397 { | |
398 lw_expr_t expr; | |
399 | |
400 if (l -> len >= 0) | |
401 return; | |
402 | |
403 expr = lwasm_fetch_expr(l, 0); | |
404 | |
405 if (lw_expr_istype(expr, lw_expr_type_int)) | |
406 { | |
407 l -> len = lw_expr_intval(expr) * 4; | |
408 } | |
409 } | |
410 | |
411 EMITFUNC(pseudo_emit_zmq) | |
412 { | |
413 int i; | |
414 | |
415 if (l -> len < 0) | |
416 { | |
417 lwasm_register_error(as, l, "Expression not constant"); | |
418 return; | |
419 } | |
420 | |
421 for (i = 0; i < l -> len; i++) | |
422 lwasm_emit(l, 0); | |
423 } | |
424 | |
425 | |
426 PARSEFUNC(pseudo_parse_zmd) | |
427 { | |
428 lw_expr_t expr; | |
429 | |
430 expr = lwasm_parse_expr(as, p); | |
431 if (!expr) | |
432 { | |
433 lwasm_register_error(as, l, "Bad expression"); | |
434 } | |
435 | |
436 lwasm_save_expr(l, 0, expr); | |
437 } | |
438 | |
439 RESOLVEFUNC(pseudo_resolve_zmd) | |
440 { | |
441 lw_expr_t expr; | |
442 | |
443 if (l -> len >= 0) | |
444 return; | |
445 | |
446 expr = lwasm_fetch_expr(l, 0); | |
447 | |
448 if (lw_expr_istype(expr, lw_expr_type_int)) | |
449 { | |
450 l -> len = lw_expr_intval(expr) * 2; | |
451 } | |
452 } | |
453 | |
454 EMITFUNC(pseudo_emit_zmd) | |
455 { | |
456 int i; | |
457 | |
458 if (l -> len < 0) | |
459 { | |
460 lwasm_register_error(as, l, "Expression not constant"); | |
461 return; | |
462 } | |
463 | |
464 for (i = 0; i < l -> len; i++) | |
465 lwasm_emit(l, 0); | |
466 } | |
467 | |
468 PARSEFUNC(pseudo_parse_zmb) | |
469 { | |
470 lw_expr_t expr; | |
471 | |
472 expr = lwasm_parse_expr(as, p); | |
473 if (!expr) | |
474 { | |
475 lwasm_register_error(as, l, "Bad expression"); | |
476 } | |
477 | |
478 lwasm_save_expr(l, 0, expr); | |
479 } | |
480 | |
481 RESOLVEFUNC(pseudo_resolve_zmb) | |
482 { | |
483 lw_expr_t expr; | |
484 | |
485 if (l -> len >= 0) | |
486 return; | |
487 | |
488 expr = lwasm_fetch_expr(l, 0); | |
489 | |
490 if (lw_expr_istype(expr, lw_expr_type_int)) | |
491 { | |
492 l -> len = lw_expr_intval(expr); | |
493 } | |
494 } | |
495 | |
496 EMITFUNC(pseudo_emit_zmb) | |
497 { | |
498 int i; | |
499 | |
500 if (l -> len < 0) | |
501 { | |
502 lwasm_register_error(as, l, "Expression not constant"); | |
503 return; | |
504 } | |
505 | |
506 for (i = 0; i < l -> len; i++) | |
507 lwasm_emit(l, 0); | |
508 } |