Mercurial > hg-old > index.cgi
annotate src/pseudo.c @ 49:21ae0fab469b
Added needed infra for useful listing of EQU and ORG type statements
author | lost |
---|---|
date | Sun, 04 Jan 2009 07:37:19 +0000 |
parents | 804d7465e0f9 |
children | e672232caffe |
rev | line source |
---|---|
0 | 1 /* |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
2 pseudo.c |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
3 Copyright © 2009 William Astle |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
20 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
21 This file implements the various pseudo operations. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
22 */ |
0 | 23 |
24 #include <stdlib.h> | |
25 #include "lwasm.h" | |
26 #include "instab.h" | |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
27 #include "expr.h" |
0 | 28 |
29 | |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
30 OPFUNC(pseudo_org) |
0 | 31 { |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
32 int rval; |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
33 lwasm_expr_stack_t *s; |
0 | 34 |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
35 if (l -> sym) |
0 | 36 { |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
37 register_error(as, l, 1, "No symbol allowed with ORG"); |
0 | 38 } |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
39 s = lwasm_evaluate_expr(as, l, *p, NULL); |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
40 if (!s) |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
41 { |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
42 register_error(as, l, 1, "Bad expression"); |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
43 return; |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
44 } |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
45 if (!lwasm_expr_is_constant(s)) |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
46 { |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
47 register_error(as, l, 1, "Illegal incomplete reference (pass 1)"); |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
48 return; |
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
49 } |
49
21ae0fab469b
Added needed infra for useful listing of EQU and ORG type statements
lost
parents:
47
diff
changeset
|
50 rval = lwasm_expr_get_value(s) & 0xffff; |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
51 l -> codeaddr = rval; |
49
21ae0fab469b
Added needed infra for useful listing of EQU and ORG type statements
lost
parents:
47
diff
changeset
|
52 l -> addrset = 1; |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
53 as -> addr = rval; |
0 | 54 } |
55 | |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
56 /* |
0 | 57 void pseudo_include(asmstate_t *as, sourceline_t *cl, char **optr) |
58 { | |
59 int v1; | |
60 | |
61 if (as -> passnum != 1) | |
62 return; | |
63 while (**optr && isspace(**optr)) | |
64 (*optr)++; | |
65 if (!**optr) | |
66 { | |
67 register_error(as, cl, ERR_BADFN); | |
68 return; | |
69 } | |
70 for (v1 = 0; *((*optr)+v1) && !isspace(*((*optr)+v1)); v1++) | |
71 ; | |
72 { | |
73 char *fn = malloc(v1 + 1); | |
74 strncpy(fn, *optr, v1); | |
75 fn[v1] = '\0'; | |
76 lwasm_read_file(as, fn); | |
77 } | |
78 } | |
79 | |
80 | |
81 void pseudo_rmb(asmstate_t *as, sourceline_t *cl, char **optr) | |
82 { | |
83 int rval, v1; | |
84 | |
85 rval = eval_expr(as, cl, optr, &v1); | |
86 if (rval < 0) | |
87 { | |
88 errorp1(ERR_FORWARD); | |
89 return; | |
90 } | |
91 if (v1 < 0) | |
92 { | |
93 errorp1(ERR_BADOPER); | |
94 return; | |
95 } | |
96 cl -> len = v1; | |
97 cl -> nocode = 1; | |
98 } | |
99 | |
100 void pseudo_rmd(asmstate_t *as, sourceline_t *cl, char **optr) | |
101 { | |
102 int rval, v1; | |
103 | |
104 rval = eval_expr(as, cl, optr, &v1); | |
105 if (rval < 0) | |
106 { | |
107 errorp1(ERR_FORWARD); | |
108 return; | |
109 } | |
110 if (v1 < 0) | |
111 { | |
112 errorp1(ERR_BADOPER); | |
113 return; | |
114 } | |
115 cl -> len = v1 * 2; | |
116 cl -> nocode = 1; | |
117 } | |
118 | |
119 void pseudo_rmq(asmstate_t *as, sourceline_t *cl, char **optr) | |
120 { | |
121 int rval, v1; | |
122 | |
123 rval = eval_expr(as, cl, optr, &v1); | |
124 if (rval < 0) | |
125 { | |
126 errorp1(ERR_FORWARD); | |
127 return; | |
128 } | |
129 if (v1 < 0) | |
130 { | |
131 errorp1(ERR_BADOPER); | |
132 return; | |
133 } | |
134 cl -> len = v1 * 4; | |
135 cl -> nocode = 1; | |
136 } | |
137 | |
138 void pseudo_zmb(asmstate_t *as, sourceline_t *cl, char **optr) | |
139 { | |
140 int rval, v1; | |
141 | |
142 rval = eval_expr(as, cl, optr, &v1); | |
143 if (rval < 0) | |
144 { | |
145 errorp1(ERR_FORWARD); | |
146 return; | |
147 } | |
148 if (v1 < 0) | |
149 { | |
150 errorp1(ERR_BADOPER); | |
151 return; | |
152 } | |
153 while (v1--) | |
154 emit(0); | |
155 } | |
156 | |
157 void pseudo_zmd(asmstate_t *as, sourceline_t *cl, char **optr) | |
158 { | |
159 int rval, v1; | |
160 | |
161 rval = eval_expr(as, cl, optr, &v1); | |
162 if (rval < 0) | |
163 { | |
164 errorp1(ERR_FORWARD); | |
165 return; | |
166 } | |
167 if (v1 < 0) | |
168 { | |
169 errorp1(ERR_BADOPER); | |
170 return; | |
171 } | |
172 while (v1--) | |
173 { | |
174 emit(0); | |
175 emit(0); | |
176 } | |
177 } | |
178 | |
179 void pseudo_zmq(asmstate_t *as, sourceline_t *cl, char **optr) | |
180 { | |
181 int rval, v1; | |
182 | |
183 rval = eval_expr(as, cl, optr, &v1); | |
184 if (rval < 0) | |
185 { | |
186 errorp1(ERR_FORWARD); | |
187 return; | |
188 } | |
189 if (v1 < 0) | |
190 { | |
191 errorp1(ERR_BADOPER); | |
192 return; | |
193 } | |
194 while (v1--) | |
195 { | |
196 emit(0); | |
197 emit(0); | |
198 emit(0); | |
199 emit(0); | |
200 } | |
201 } | |
202 | |
203 void pseudo_end(asmstate_t *as, sourceline_t *cl, char **optr) | |
204 { | |
205 int rval, v1; | |
206 | |
207 while (**optr && isspace(**optr)) | |
208 ; | |
209 if (**optr && **optr != '*' && **optr != ';') | |
210 { | |
211 rval = eval_expr(as, cl, optr, &v1); | |
212 if (rval < 0) | |
213 { | |
214 errorp1(ERR_FORWARD); | |
215 return; | |
216 } | |
217 } | |
218 else | |
219 { | |
220 v1 = 0; | |
221 } | |
222 if (as -> passnum == 2) | |
223 as -> execaddr = v1; | |
224 } | |
225 | |
226 void pseudo_align(asmstate_t *as, sourceline_t *cl, char **optr) | |
227 { | |
228 int rval, v1; | |
229 int cn; | |
230 | |
231 rval = eval_expr(as, cl, optr, &v1); | |
232 if (rval < 0) | |
233 { | |
234 errorp1(ERR_FORWARD); | |
235 return; | |
236 } | |
237 cn = cl -> addr % v1; | |
238 if (cn) | |
239 cn = v1 - cn; | |
240 | |
241 while (cn) | |
242 { | |
243 emit(0); | |
244 cn--; | |
245 } | |
246 } | |
247 | |
248 void pseudo_equ(asmstate_t *as, sourceline_t *cl, char **optr) | |
249 { | |
250 int rval, v1; | |
251 | |
252 if (cl -> hassym == 0) | |
253 { | |
254 errorp1(ERR_NOSYM); | |
255 return; | |
256 } | |
257 rval = eval_expr(as, cl, optr, &v1); | |
258 // eval_expr returns -1 if there was a forward ref | |
259 // or -2 if the expr was invalid | |
260 if (rval == -2) | |
261 { | |
262 // carp | |
263 errorp1(ERR_FORWARD); | |
264 } | |
265 if (rval < 0) | |
266 { | |
267 // remove symbol ref | |
268 cl -> hassym = 0; | |
269 // mark as a "comment" so it isn't processed next time | |
270 cl -> opcode = -1; | |
271 return; | |
272 } | |
273 cl -> code_symloc = v1; | |
274 cl -> isequ = 1; | |
275 cl -> symaddr = v1 & 0xFFFF; | |
276 } | |
277 | |
278 void pseudo_set(asmstate_t *as, sourceline_t *cl, char **optr) | |
279 { | |
280 int rval, v1; | |
281 | |
282 if (cl -> hassym == 0) | |
283 { | |
284 errorp1(ERR_NOSYM); | |
285 return; | |
286 } | |
287 rval = eval_expr(as, cl, optr, &v1); | |
288 // eval_expr returns -1 if there was a forward ref | |
289 // or -2 if the expr was invalid | |
290 if (rval == -2) | |
291 { | |
292 // carp | |
293 errorp1(ERR_FORWARD); | |
294 } | |
295 if (rval < 0) | |
296 { | |
297 // remove symbol ref | |
298 cl -> hassym = 0; | |
299 // mark as a "comment" so it isn't processed next time | |
300 cl -> opcode = -1; | |
301 return; | |
302 } | |
303 cl -> code_symloc = v1; | |
304 cl -> isset = 1; | |
305 cl -> isequ = 1; | |
306 cl -> symaddr = v1 & 0xFFFF; | |
307 } | |
308 | |
309 void pseudo_setdp(asmstate_t *as, sourceline_t *cl, char **optr) | |
310 { | |
311 int rval, v1; | |
312 | |
313 if (cl -> hassym) | |
314 { | |
315 register_error(as, cl, ERR_SYM); | |
316 cl -> hassym = 0; | |
317 return; | |
318 } | |
319 else | |
320 { | |
321 rval = eval_expr(as, cl, optr, &v1); | |
322 if (rval == -1) | |
323 { | |
324 errorp1(ERR_FORWARD); | |
325 } | |
326 if (rval < 0) | |
327 { | |
328 cl -> opcode = -1; | |
329 return; | |
330 } | |
331 } | |
332 // setdp needs to resolve properly on pass 2 | |
333 if (v1 > 0xff || v1 < 0) | |
334 { | |
335 errorp1(ERR_OVERFLOW); | |
336 } | |
337 as -> dpval = v1 & 0xff; | |
338 cl -> dpval = v1 & 0xff; | |
339 cl -> issetdp = 1; | |
340 cl -> numcodebytes = 0; | |
341 //printf("%s\n", "SETDP2"); | |
342 } | |
343 | |
344 void pseudo_fcc(asmstate_t *as, sourceline_t *cl, char **optr) | |
345 { | |
346 int cn = 0; | |
347 int delim = 0; | |
348 | |
349 delim = *(*optr)++; | |
350 if (!delim) | |
351 { | |
352 errorp1(ERR_BADOPER); | |
353 } | |
354 else | |
355 { | |
356 while (**optr && **optr != delim) | |
357 { | |
358 emit(**optr); | |
359 (*optr)++; | |
360 cn += 1; | |
361 } | |
362 } | |
363 cl -> len = cn; | |
364 } | |
365 | |
366 void pseudo_fcs(asmstate_t *as, sourceline_t *cl, char **optr) | |
367 { | |
368 int cn = 0; | |
369 int delim = 0; | |
370 | |
371 delim = *(*optr)++; | |
372 if (!delim) | |
373 { | |
374 errorp1(ERR_BADOPER); | |
375 } | |
376 else | |
377 { | |
378 while (**optr && **optr != delim) | |
379 { | |
380 if (!*((*optr) + 1) || *((*optr) + 1) == delim) | |
381 emit((**optr) | 0x80); | |
382 else | |
383 emit(**optr); | |
384 (*optr)++; | |
385 cn += 1; | |
386 } | |
387 } | |
388 cl -> len = cn; | |
389 } | |
390 | |
391 void pseudo_fcn(asmstate_t *as, sourceline_t *cl, char **optr) | |
392 { | |
393 int cn = 0; | |
394 int delim = 0; | |
395 | |
396 delim = *(*optr)++; | |
397 if (!delim) | |
398 { | |
399 errorp1(ERR_BADOPER); | |
400 } | |
401 else | |
402 { | |
403 while (**optr && **optr != delim) | |
404 { | |
405 emit(**optr); | |
406 (*optr)++; | |
407 cn += 1; | |
408 } | |
409 } | |
410 emit(0); | |
411 cl -> len = cn + 1; | |
412 } | |
413 | |
414 void pseudo_fcb(asmstate_t *as, sourceline_t *cl, char **optr) | |
415 { | |
416 int rval, v1; | |
417 | |
418 fcb_again: | |
419 rval = eval_expr(as, cl, optr, &v1); | |
420 if (v1 < -127 || v1 > 0xff) | |
421 errorp2(ERR_OVERFLOW); | |
422 emit(v1 & 0xff); | |
423 if (**optr == ',') | |
424 { | |
425 (*optr)++; | |
426 goto fcb_again; | |
427 } | |
428 } | |
429 | |
430 void pseudo_fdb(asmstate_t *as, sourceline_t *cl, char **optr) | |
431 { | |
432 int rval, v1; | |
433 | |
434 fdb_again: | |
435 rval = eval_expr(as, cl, optr, &v1); | |
436 emit((v1 >> 8) & 0xff); | |
437 emit(v1 & 0xff); | |
438 if (**optr == ',') | |
439 { | |
440 (*optr)++; | |
441 goto fdb_again; | |
442 } | |
443 } | |
444 | |
445 void pseudo_fqb(asmstate_t *as, sourceline_t *cl, char **optr) | |
446 { | |
447 int rval, v1; | |
448 | |
449 fqb_again: | |
450 rval = eval_expr(as, cl, optr, &v1); | |
451 emit((v1 >> 24) & 0xff); | |
452 emit((v1 >> 16) & 0xff); | |
453 emit((v1 >> 8) & 0xff); | |
454 emit(v1 & 0xff); | |
455 if (**optr == ',') | |
456 { | |
457 (*optr)++; | |
458 goto fqb_again; | |
459 } | |
460 } | |
461 | |
462 // don't need to do anything if we are executing one of these | |
463 void pseudo_endc(asmstate_t *as, sourceline_t *cl, char **optr) | |
464 { | |
465 return; | |
466 } | |
467 | |
468 // if "else" executes, we must be going into an "ignore" state | |
469 void pseudo_else(asmstate_t *as, sourceline_t *cl, char **optr) | |
470 { | |
471 as -> skipcond = 1; | |
472 as -> skipcount = 1; | |
473 } | |
474 | |
475 void pseudo_ifne(asmstate_t *as, sourceline_t *cl, char **optr) | |
476 { | |
477 int v1; | |
478 int rval; | |
479 // printf("ifne %s\n", *optr); | |
480 rval = eval_expr(as, cl, optr, &v1); | |
481 if (rval < 0) | |
482 { | |
483 errorp1(ERR_BADCOND); | |
484 } | |
485 else | |
486 { | |
487 // printf("Condition value: %d\n", v1); | |
488 if (!v1) | |
489 { | |
490 // printf("condition no match\n"); | |
491 as -> skipcond = 1; | |
492 as -> skipcount = 1; | |
493 } | |
494 } | |
495 } | |
496 void pseudo_ifeq(asmstate_t *as, sourceline_t *cl, char **optr) | |
497 { | |
498 int v1; | |
499 int rval; | |
500 | |
501 rval = eval_expr(as, cl, optr, &v1); | |
502 if (rval < 0) | |
503 { | |
504 errorp1(ERR_BADCOND); | |
505 } | |
506 else | |
507 { | |
508 if (v1) | |
509 { | |
510 as -> skipcond = 1; | |
511 as -> skipcount = 1; | |
512 } | |
513 } | |
514 } | |
515 void pseudo_iflt(asmstate_t *as, sourceline_t *cl, char **optr) | |
516 { | |
517 int v1; | |
518 int rval; | |
519 | |
520 rval = eval_expr(as, cl, optr, &v1); | |
521 if (rval < 0) | |
522 { | |
523 errorp1(ERR_BADCOND); | |
524 } | |
525 else | |
526 { | |
527 if (v1 >= 0) | |
528 { | |
529 as -> skipcond = 1; | |
530 as -> skipcount = 1; | |
531 } | |
532 } | |
533 } | |
534 void pseudo_ifle(asmstate_t *as, sourceline_t *cl, char **optr) | |
535 { | |
536 int v1; | |
537 int rval; | |
538 | |
539 rval = eval_expr(as, cl, optr, &v1); | |
540 if (rval < 0) | |
541 { | |
542 errorp1(ERR_BADCOND); | |
543 } | |
544 else | |
545 { | |
546 if (v1 > 0) | |
547 { | |
548 as -> skipcond = 1; | |
549 as -> skipcount = 1; | |
550 } | |
551 } | |
552 } | |
553 void pseudo_ifgt(asmstate_t *as, sourceline_t *cl, char **optr) | |
554 { | |
555 int v1; | |
556 int rval; | |
557 | |
558 rval = eval_expr(as, cl, optr, &v1); | |
559 if (rval < 0) | |
560 { | |
561 errorp1(ERR_BADCOND); | |
562 } | |
563 else | |
564 { | |
565 if (v1 <= 0) | |
566 { | |
567 as -> skipcond = 1; | |
568 as -> skipcount = 1; | |
569 } | |
570 } | |
571 } | |
572 void pseudo_ifge(asmstate_t *as, sourceline_t *cl, char **optr) | |
573 { | |
574 int v1; | |
575 int rval; | |
576 | |
577 rval = eval_expr(as, cl, optr, &v1); | |
578 if (rval < 0) | |
579 { | |
580 errorp1(ERR_BADCOND); | |
581 } | |
582 else | |
583 { | |
584 if (v1 < 0) | |
585 { | |
586 as -> skipcond = 1; | |
587 as -> skipcount = 1; | |
588 } | |
589 } | |
590 } | |
591 | |
592 void pseudo_error(asmstate_t *as, sourceline_t *cl, char **optr) | |
593 { | |
594 cl -> user_error = strdup(*optr); | |
595 errorp1(ERR_USER); | |
596 } | |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
597 */ |