Mercurial > hg-old > index.cgi
comparison old-trunk/lwasm/old/pseudo.c @ 339:eb230fa7d28e
Prepare for migration to hg
author | lost |
---|---|
date | Fri, 19 Mar 2010 02:54:14 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
338:e7885b3ee266 | 339:eb230fa7d28e |
---|---|
1 /* | |
2 pseudo.c | |
3 Copyright © 2009 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 This file implements the various pseudo operations. | |
22 */ | |
23 #include <config.h> | |
24 #include <errno.h> | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 #include "lwasm.h" | |
29 #include "instab.h" | |
30 #include "expr.h" | |
31 #include "util.h" | |
32 | |
33 extern int lwasm_read_file(asmstate_t *as, const char *filename); | |
34 | |
35 OPFUNC(pseudo_noop) | |
36 { | |
37 skip_operand(p); | |
38 } | |
39 | |
40 OPFUNC(pseudo_org) | |
41 { | |
42 int v, r; | |
43 | |
44 if (as -> csect) | |
45 { | |
46 register_error(as, l, 1, "ORG not allowed within sections"); | |
47 return; | |
48 } | |
49 if (as -> inmod) | |
50 { | |
51 register_error(as, l, 1, "ORG not allowed within modules"); | |
52 return; | |
53 } | |
54 | |
55 if (as -> passnum != 1) | |
56 { | |
57 // org is not needed to be processed on pass 2 | |
58 // this will prevent phasing errors for forward references that | |
59 // resolve on the second pass | |
60 // we saved the org address in l -> codeaddr on pass 1 | |
61 as -> addr = l -> codeaddr; | |
62 skip_operand(p); | |
63 return; | |
64 } | |
65 | |
66 if (l -> sym) | |
67 { | |
68 register_error(as, l, 1, "No symbol allowed with ORG"); | |
69 } | |
70 | |
71 r = lwasm_expr_result2(as, l, p, EXPR_PASS1CONST, &v, 0); | |
72 if (r != 0) | |
73 return; | |
74 l -> codeaddr = v; | |
75 l -> addrset = 1; | |
76 as -> addr = v; | |
77 } | |
78 | |
79 /* | |
80 The operand for include is a string optionally enclosed in " | |
81 */ | |
82 OPFUNC(pseudo_include) | |
83 { | |
84 int v1; | |
85 char *fn; | |
86 | |
87 // only include files on pass 1 | |
88 // but make sure local include context is right | |
89 // for the next line... | |
90 if (as -> passnum != 1) | |
91 { | |
92 as -> context = lwasm_next_context(as); | |
93 skip_operand(p); | |
94 return; | |
95 } | |
96 | |
97 while (**p && isspace(**p)) | |
98 (*p)++; | |
99 | |
100 if (!**p) | |
101 { | |
102 register_error(as, l, 1, "Bad file name"); | |
103 return; | |
104 } | |
105 | |
106 if (**p == '"') | |
107 { | |
108 // search for ending " | |
109 (*p)++; | |
110 for (v1 = 0; *((*p)+v1) && *((*p)+v1) != '"'; v1++) | |
111 /* do nothing */ ; | |
112 if (*((*p)+v1) != '"') | |
113 { | |
114 register_error(as, l, 1, "Bad file name"); | |
115 return; | |
116 } | |
117 } | |
118 else | |
119 { | |
120 // search for a space type character | |
121 for (v1 = 0; *((*p)+v1) && !isspace(*((*p)+v1)); v1++) | |
122 ; | |
123 } | |
124 | |
125 fn = lwasm_alloc(v1 + 1); | |
126 memcpy(fn, *p, v1); | |
127 fn[v1] = '\0'; | |
128 | |
129 (*p) += v1; | |
130 if (**p == '"') | |
131 (*p)++; | |
132 | |
133 // end local label context on include | |
134 as -> context = lwasm_next_context(as); | |
135 if (lwasm_read_file(as, fn) < 0) | |
136 { | |
137 register_error(as, l, 1, "File include error (%s)", fn); | |
138 } | |
139 lwasm_free(fn); | |
140 } | |
141 | |
142 /* | |
143 The operand for includebin is a string optionally enclosed in " | |
144 */ | |
145 OPFUNC(pseudo_includebin) | |
146 { | |
147 int v1; | |
148 char *fn; | |
149 FILE *f; | |
150 | |
151 // only include files on pass 1 | |
152 while (**p && isspace(**p)) | |
153 (*p)++; | |
154 | |
155 if (!**p) | |
156 { | |
157 register_error(as, l, 1, "Bad file name"); | |
158 return; | |
159 } | |
160 | |
161 if (**p == '"') | |
162 { | |
163 // search for ending " | |
164 (*p)++; | |
165 for (v1 = 0; *((*p)+v1) && *((*p)+v1) != '"'; v1++) | |
166 /* do nothing */ ; | |
167 if (*((*p)+v1) != '"') | |
168 { | |
169 register_error(as, l, 1, "Bad file name"); | |
170 return; | |
171 } | |
172 } | |
173 else | |
174 { | |
175 // search for a space type character | |
176 for (v1 = 0; *((*p)+v1) && !isspace(*((*p)+v1)); v1++) | |
177 ; | |
178 } | |
179 | |
180 fn = lwasm_alloc(v1 + 1); | |
181 memcpy(fn, *p, v1); | |
182 fn[v1] = '\0'; | |
183 | |
184 (*p) += v1; | |
185 if (**p == '"') | |
186 (*p)++; | |
187 | |
188 // open the file | |
189 f = fopen(fn, "rb"); | |
190 if (!f) | |
191 { | |
192 register_error(as, l, 1, "Cannot open file: %s", strerror(errno)); | |
193 register_error(as, l, 2, "Cannot open file: %s", strerror(errno)); | |
194 } | |
195 | |
196 // don't need fn any more | |
197 lwasm_free(fn); | |
198 | |
199 // read the contents of the file and "emit()" it | |
200 while (!feof(f) && !ferror(f)) | |
201 { | |
202 v1 = fgetc(f); | |
203 if (v1 == EOF) | |
204 break; | |
205 lwasm_emit(as, l, v1); | |
206 } | |
207 // close file | |
208 fclose(f); | |
209 } | |
210 | |
211 OPFUNC(pseudo_rmb) | |
212 { | |
213 int r, v; | |
214 | |
215 if (as -> passnum == 2) | |
216 { | |
217 as -> addr += l -> nocodelen; | |
218 skip_operand(p); | |
219 return; | |
220 } | |
221 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, -1); | |
222 if (r != 0) | |
223 return; | |
224 l -> nocodelen = v; | |
225 as -> addr += v; | |
226 } | |
227 | |
228 OPFUNC(pseudo_rmd) | |
229 { | |
230 int r, v; | |
231 | |
232 if (as -> passnum == 2) | |
233 { | |
234 as -> addr += l -> nocodelen; | |
235 skip_operand(p); | |
236 return; | |
237 } | |
238 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); | |
239 if (r != 0) | |
240 return; | |
241 v *= 2; | |
242 l -> nocodelen = v; | |
243 as -> addr += v; | |
244 } | |
245 | |
246 OPFUNC(pseudo_rmq) | |
247 { | |
248 int r, v; | |
249 | |
250 if (as -> passnum == 2) | |
251 { | |
252 as -> addr += l -> nocodelen; | |
253 skip_operand(p); | |
254 return; | |
255 } | |
256 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); | |
257 if (r != 0) | |
258 return; | |
259 v *= 4; | |
260 l -> nocodelen = v; | |
261 as -> addr += v; | |
262 } | |
263 | |
264 OPFUNC(pseudo_zmb) | |
265 { | |
266 int r, v; | |
267 | |
268 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); | |
269 if (r != 0) | |
270 return; | |
271 while (v--) | |
272 lwasm_emit(as, l, 0); | |
273 } | |
274 | |
275 OPFUNC(pseudo_zmd) | |
276 { | |
277 int r, v; | |
278 | |
279 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); | |
280 if (r != 0) | |
281 return; | |
282 v *= 2; | |
283 while (v--) | |
284 lwasm_emit(as, l, 0); | |
285 } | |
286 | |
287 OPFUNC(pseudo_zmq) | |
288 { | |
289 int r, v; | |
290 | |
291 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); | |
292 if (r != 0) | |
293 return; | |
294 v *= 4; | |
295 while (v--) | |
296 lwasm_emit(as, l, 0); | |
297 } | |
298 | |
299 OPFUNC(pseudo_end) | |
300 { | |
301 int r, v; | |
302 lwasm_expr_stack_t *s; | |
303 | |
304 | |
305 as -> endseen = 1; | |
306 | |
307 // address only matters for DECB output | |
308 if (as -> outformat != OUTPUT_DECB) | |
309 { | |
310 skip_operand(p); | |
311 return; | |
312 } | |
313 | |
314 r = lwasm_expr_result2(as, l, p, 0, &v, 0); | |
315 if (r != 0) | |
316 { | |
317 register_error(as, l, 2, "Bad operand"); | |
318 } | |
319 | |
320 v = v & 0xffff; | |
321 if (as -> passnum == 2) | |
322 { | |
323 as -> execaddr = v; | |
324 l -> symaddr = v; | |
325 l -> addrset = 2; | |
326 } | |
327 } | |
328 | |
329 | |
330 OPFUNC(pseudo_align) | |
331 { | |
332 int cn; | |
333 int r, v; | |
334 int pad = 0; | |
335 // we have to parse this on pass 2 so that we get the pad value | |
336 // if (as -> passnum == 2) | |
337 // { | |
338 // skip_operand(p); | |
339 // while (as -> addr < l -> symaddr) | |
340 // lwasm_emit(as, l, 0); | |
341 // return; | |
342 // } | |
343 | |
344 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); | |
345 if (r != 0) | |
346 { | |
347 l -> symaddr = as -> addr; | |
348 return; | |
349 } | |
350 | |
351 if (v < 1) | |
352 { | |
353 register_error(as, l, 1, "Illegal alignment %d", v); | |
354 return; | |
355 } | |
356 | |
357 cn = l -> codeaddr % v; | |
358 if (cn) | |
359 cn = v - cn; | |
360 | |
361 if (**p == ',') | |
362 { | |
363 // we have a padding value specified | |
364 (*p)++; | |
365 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST, &pad, 1); | |
366 if (r != 0 && as -> passnum == 2) | |
367 { | |
368 register_error(as, l, 2, "Illegal padding value - must be constant on pass 2"); | |
369 return; | |
370 } | |
371 } | |
372 | |
373 while (cn--) | |
374 { | |
375 lwasm_emit(as, l, pad); | |
376 } | |
377 l -> symaddr = as -> addr; | |
378 } | |
379 | |
380 OPFUNC(pseudo_equ) | |
381 { | |
382 int r, v; | |
383 | |
384 if (l -> sym == NULL) | |
385 { | |
386 register_error(as, l, 1, "No symbol specified"); | |
387 return; | |
388 } | |
389 | |
390 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); | |
391 if (r < 0) | |
392 v = 0; | |
393 | |
394 l -> symaddr = v & 0xFFFF; | |
395 l -> addrset = 2; | |
396 | |
397 // note: we need to do this because the symbol might have resolved | |
398 // to a constant! | |
399 lwasm_register_symbol(as, l, l -> sym, v, (r > 0 ? SYMBOL_COMPLEX: SYMBOL_NORM) | SYMBOL_FORCE | (l -> forceglobal ? SYMBOL_GLOBAL : SYMBOL_NORM)); | |
400 } | |
401 | |
402 OPFUNC(pseudo_set) | |
403 { | |
404 int r, v; | |
405 | |
406 // set MUST run on both passes as the symbol value changes! | |
407 | |
408 if (l -> sym == NULL) | |
409 { | |
410 register_error(as, l, 1, "No symbol specified"); | |
411 return; | |
412 } | |
413 | |
414 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); | |
415 if (r < 0) | |
416 v = 0; | |
417 | |
418 l -> symaddr = v & 0xFFFF; | |
419 l -> addrset = 2; | |
420 | |
421 lwasm_register_symbol(as, l, l -> sym, v, (r > 0 ? SYMBOL_COMPLEX: SYMBOL_NORM) | SYMBOL_SET); | |
422 } | |
423 | |
424 OPFUNC(pseudo_setdp) | |
425 { | |
426 int r, v; | |
427 | |
428 if (as -> outformat == OUTPUT_OBJ) | |
429 { | |
430 register_error(as, l, 1, "SETDP not permitted with OBJ target"); | |
431 return; | |
432 } | |
433 | |
434 // setdp is needed on both passes; must resolve to a constant on pass 1 | |
435 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); | |
436 if (r != 0) | |
437 return; | |
438 | |
439 if (v < -127 || v > 255) | |
440 { | |
441 register_error(as, l, 1, "Byte overflow"); | |
442 return; | |
443 } | |
444 | |
445 l -> symaddr = v & 0xFF; | |
446 l -> addrset = 2; | |
447 | |
448 as -> dpval = v & 0xFF; | |
449 } | |
450 | |
451 // used to get a byte from a string | |
452 // -1 is end of line | |
453 int pseudo_fcc_fetchchar(asmstate_t *as, char **p) | |
454 { | |
455 int c; | |
456 | |
457 // - | |
458 if (!**p) | |
459 return -1; | |
460 | |
461 c = (unsigned char)(**p); | |
462 (*p)++; | |
463 | |
464 if (as -> pragmas & PRAGMA_CESCAPES && c == '\\') | |
465 { | |
466 // decode escapes if needed | |
467 if (!**p) | |
468 return c; | |
469 | |
470 c = **p; | |
471 (*p)++; | |
472 | |
473 switch (c) | |
474 { | |
475 // octal value | |
476 // 1, 2, or 3 digits | |
477 // NOTE: \0 for NUL is included in this... | |
478 case '0': | |
479 case '1': | |
480 case '2': | |
481 case '3': | |
482 case '4': | |
483 case '5': | |
484 case '6': | |
485 case '7': | |
486 c -= '0'; | |
487 if (**p < '0' || **p > '9') | |
488 return c; | |
489 c = c << 3; | |
490 c |= **p - '0'; | |
491 (*p)++; | |
492 if (**p < '0' || **p > '9') | |
493 return c; | |
494 c = c << 3; | |
495 c |= **p - '0'; | |
496 (*p)++; | |
497 return c; | |
498 | |
499 // LF | |
500 case 'n': | |
501 return 10; | |
502 | |
503 // CR | |
504 case 'r': | |
505 return 13; | |
506 | |
507 // TAB | |
508 case 't': | |
509 return 9; | |
510 | |
511 // VT | |
512 case 'v': | |
513 return 11; | |
514 | |
515 // BS | |
516 case 'b': | |
517 return 8; | |
518 | |
519 // FF | |
520 case 'f': | |
521 return 12; | |
522 | |
523 // BEL | |
524 case 'a': | |
525 return 7; | |
526 | |
527 // hex char code (2 chars) | |
528 case 'x': | |
529 { | |
530 int c2; | |
531 if (!**p) | |
532 return 'x'; | |
533 c = toupper(**p); | |
534 (*p)++; | |
535 if (c < '0' || (c > '9' && c < 'A') || c > 'F') | |
536 return 0; | |
537 c -= '0'; | |
538 if (c > 9) | |
539 c -= 7; | |
540 c2 = c << 4; | |
541 if (!**p) | |
542 return 0; | |
543 c = toupper(**p); | |
544 (*p)++; | |
545 if (c < '0' || (c > '9' && c < 'A') || c > 'F') | |
546 return 0; | |
547 c -= '0'; | |
548 if (c > 9) | |
549 c -= 7; | |
550 c2 |= c; | |
551 return c2; | |
552 } | |
553 // everything else stands for itself as a fall back or legit | |
554 default: | |
555 return c; | |
556 } | |
557 } | |
558 return c; | |
559 } | |
560 | |
561 OPFUNC(pseudo_fcc) | |
562 { | |
563 int delim = 0; | |
564 int c; | |
565 | |
566 delim = **p; | |
567 if (!delim) | |
568 { | |
569 register_error(as, l, 1, "Bad operand"); | |
570 return; | |
571 } | |
572 *p += 1; | |
573 for (;;) | |
574 { | |
575 c = pseudo_fcc_fetchchar(as, p); | |
576 if (c == delim || c < 0) | |
577 break; | |
578 | |
579 lwasm_emit(as, l, c); | |
580 } | |
581 } | |
582 | |
583 | |
584 OPFUNC(pseudo_fcs) | |
585 { | |
586 int delim = 0; | |
587 int c, lc = -1; | |
588 | |
589 delim = **p; | |
590 if (!delim) | |
591 { | |
592 register_error(as, l, 1, "Bad operand"); | |
593 return; | |
594 } | |
595 *p += 1; | |
596 for (;;) | |
597 { | |
598 c = pseudo_fcc_fetchchar(as, p); | |
599 if (c == delim || c < 0) | |
600 { | |
601 if (lc >= 0) | |
602 lwasm_emit(as, l, lc | 0x80); | |
603 break; | |
604 } | |
605 if (lc >= 0) | |
606 lwasm_emit(as, l, lc); | |
607 lc = c; | |
608 } | |
609 } | |
610 | |
611 OPFUNC(pseudo_fcn) | |
612 { | |
613 int delim = 0; | |
614 int c; | |
615 | |
616 delim = **p; | |
617 if (!delim) | |
618 { | |
619 register_error(as, l, 1, "Bad operand"); | |
620 return; | |
621 } | |
622 *p += 1; | |
623 for (;;) | |
624 { | |
625 c = pseudo_fcc_fetchchar(as, p); | |
626 if (c == delim || c < 0) | |
627 break; | |
628 | |
629 lwasm_emit(as, l, c); | |
630 } | |
631 lwasm_emit(as, l, 0); | |
632 } | |
633 | |
634 OPFUNC(pseudo_fcb) | |
635 { | |
636 int r, v; | |
637 | |
638 fcb_again: | |
639 r = lwasm_expr_result2(as, l, p, 0, &v, -1); | |
640 if (r < 0) | |
641 return; | |
642 | |
643 if (r > 0) | |
644 { | |
645 register_error(as, l, 2, "Illegal external or inter-segment reference"); | |
646 v = 0; | |
647 } | |
648 | |
649 if (v < -127 || v > 255) | |
650 { | |
651 register_error(as, l, 1, "Byte overflow"); | |
652 } | |
653 | |
654 lwasm_emit(as, l, v); | |
655 if (**p == ',') | |
656 { | |
657 (*p)++; | |
658 goto fcb_again; | |
659 } | |
660 } | |
661 | |
662 // FIXME: handle external references in an intelligent way | |
663 OPFUNC(pseudo_fdb) | |
664 { | |
665 int r, v; | |
666 int extseen = 0; | |
667 char *p1; | |
668 | |
669 fdb_again: | |
670 p1 = *p; | |
671 r = lwasm_expr_result2(as, l, p, 0, &v, -1); | |
672 if (r < 0) | |
673 return; | |
674 | |
675 if (r > 0 && extseen == 1) | |
676 { | |
677 register_error(as, l, 2, "Illegal external or inter-segment reference (only 1 per FDB line)"); | |
678 v = 0; | |
679 } | |
680 else if (r > 0) | |
681 { | |
682 l -> relocoff = as -> addr - l -> codeaddr; | |
683 *p = p1; | |
684 r = lwasm_expr_result2(as, l, p, 0, &v, 0); | |
685 } | |
686 | |
687 lwasm_emit(as, l, v >> 8); | |
688 lwasm_emit(as, l, v & 0xff); | |
689 if (**p == ',') | |
690 { | |
691 (*p)++; | |
692 goto fdb_again; | |
693 } | |
694 } | |
695 | |
696 // FIXME: handle external references in a sensible way | |
697 OPFUNC(pseudo_fqb) | |
698 { | |
699 int r, v; | |
700 | |
701 fqb_again: | |
702 r = lwasm_expr_result2(as, l, p, 0, &v, -1); | |
703 if (r < 0) | |
704 return; | |
705 | |
706 if (r > 0) | |
707 { | |
708 register_error(as, l, 2, "Illegal external or inter-segment reference"); | |
709 v = 0; | |
710 } | |
711 | |
712 lwasm_emit(as, l, v >> 24); | |
713 lwasm_emit(as, l, v >> 16); | |
714 lwasm_emit(as, l, v >> 8); | |
715 lwasm_emit(as, l, v & 0xff); | |
716 if (**p == ',') | |
717 { | |
718 (*p)++; | |
719 goto fqb_again; | |
720 } | |
721 } | |
722 | |
723 // don't need to do anything if we are executing one of these | |
724 OPFUNC(pseudo_endc) | |
725 { | |
726 if (as -> skipcond && !(as -> skipmacro)) | |
727 { | |
728 as -> skipcount -= 1; | |
729 if (as -> skipcount <= 0) | |
730 { | |
731 as -> skipcond = 0; | |
732 } | |
733 } | |
734 return; | |
735 } | |
736 | |
737 // if "else" executes, we must be going into an "ignore" state | |
738 OPFUNC(pseudo_else) | |
739 { | |
740 if (as -> skipmacro) | |
741 return; | |
742 | |
743 if (as -> skipcond) | |
744 { | |
745 if (as -> skipcount == 1) | |
746 { | |
747 as -> skipcount = 0; | |
748 as -> skipcond = 0; | |
749 } | |
750 return; | |
751 } | |
752 | |
753 as -> skipcond = 1; | |
754 as -> skipcount = 1; | |
755 } | |
756 | |
757 OPFUNC(pseudo_ifne) | |
758 { | |
759 int v1; | |
760 int rval; | |
761 | |
762 if (as -> skipcond && !(as -> skipmacro)) | |
763 { | |
764 as -> skipcount++; | |
765 skip_operand(p); | |
766 return; | |
767 } | |
768 | |
769 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); | |
770 if (rval != 0) | |
771 return; | |
772 if (!v1) | |
773 { | |
774 as -> skipcond = 1; | |
775 as -> skipcount = 1; | |
776 } | |
777 } | |
778 | |
779 OPFUNC(pseudo_ifdef) | |
780 { | |
781 lwasm_symbol_ent_t *se; | |
782 char *sym; | |
783 char *p2; | |
784 | |
785 if (as -> skipcond && !(as -> skipmacro)) | |
786 { | |
787 as -> skipcount++; | |
788 skip_operand(p); | |
789 return; | |
790 } | |
791 | |
792 if (as -> passnum != 1) | |
793 { | |
794 skip_operand(p); | |
795 if (!(l -> fsize)) | |
796 { | |
797 as -> skipcond = 1; | |
798 as -> skipcount = 1; | |
799 } | |
800 return; | |
801 } | |
802 | |
803 if (!**p) | |
804 { | |
805 register_error(as, l, 1, "Need symbol name"); | |
806 return; | |
807 } | |
808 | |
809 for (p2 = *p; **p && !isspace(**p); (*p)++) | |
810 /* do nothing */ ; | |
811 | |
812 sym = lwasm_alloc(*p - p2 + 1); | |
813 memcpy(sym, p2, *p - p2); | |
814 sym[*p - p2] = '\0'; | |
815 | |
816 // fprintf(stderr, "STUFF: %s; '%s'; '%s' (%d)\n", p2, *p, sym, as -> passnum); | |
817 se = lwasm_find_symbol(as, sym, l -> context); | |
818 if (!se) | |
819 se = lwasm_find_symbol(as, sym, -1); | |
820 | |
821 lwasm_free(sym); | |
822 | |
823 if (!se) | |
824 { | |
825 as -> skipcond = 1; | |
826 as -> skipcount = 1; | |
827 l -> fsize = 0; | |
828 } | |
829 else | |
830 { | |
831 l -> fsize = 1; | |
832 } | |
833 } | |
834 | |
835 OPFUNC(pseudo_ifndef) | |
836 { | |
837 lwasm_symbol_ent_t *se; | |
838 char *sym; | |
839 char *p2; | |
840 | |
841 if (as -> skipcond && !(as -> skipmacro)) | |
842 { | |
843 as -> skipcount++; | |
844 skip_operand(p); | |
845 return; | |
846 } | |
847 | |
848 if (as -> passnum != 1) | |
849 { | |
850 skip_operand(p); | |
851 if (l -> fsize) | |
852 { | |
853 as -> skipcond = 1; | |
854 as -> skipcount = 1; | |
855 } | |
856 return; | |
857 } | |
858 | |
859 if (!**p) | |
860 { | |
861 register_error(as, l, 1, "Need symbol name"); | |
862 return; | |
863 } | |
864 | |
865 for (p2 = *p; *p2 && !isspace(*p2); p2++) | |
866 /* do nothing */ ; | |
867 | |
868 sym = lwasm_alloc(p2 - *p + 1); | |
869 memcpy(sym, *p, p2 - *p); | |
870 sym[p2 - *p] = '\0'; | |
871 | |
872 *p = p2; | |
873 | |
874 // fprintf(stderr, "STUFF2: %s; '%s'; '%s' (%d)\n", *p, p2, sym, as -> passnum); | |
875 se = lwasm_find_symbol(as, sym, l -> context); | |
876 if (!se) | |
877 se = lwasm_find_symbol(as, sym, -1); | |
878 | |
879 lwasm_free(sym); | |
880 | |
881 if (se) | |
882 { | |
883 as -> skipcond = 1; | |
884 as -> skipcount = 1; | |
885 l -> fsize = 0; | |
886 } | |
887 else | |
888 { | |
889 l -> fsize = 1; | |
890 } | |
891 } | |
892 | |
893 OPFUNC(pseudo_ifp1) | |
894 { | |
895 if (as -> skipcond && !(as -> skipmacro)) | |
896 { | |
897 as -> skipcount++; | |
898 skip_operand(p); | |
899 return; | |
900 } | |
901 | |
902 if (as -> passnum != 1) | |
903 { | |
904 as -> skipcond = 1; | |
905 as -> skipcount = 1; | |
906 } | |
907 } | |
908 | |
909 OPFUNC(pseudo_ifp2) | |
910 { | |
911 if (as -> skipcond && !(as -> skipmacro)) | |
912 { | |
913 as -> skipcount++; | |
914 skip_operand(p); | |
915 return; | |
916 } | |
917 | |
918 if (as -> passnum != 2) | |
919 { | |
920 as -> skipcond = 1; | |
921 as -> skipcount = 1; | |
922 } | |
923 } | |
924 | |
925 OPFUNC(pseudo_ifeq) | |
926 { | |
927 int v1; | |
928 int rval; | |
929 | |
930 if (as -> skipcond && !(as -> skipmacro)) | |
931 { | |
932 as -> skipcount++; | |
933 skip_operand(p); | |
934 return; | |
935 } | |
936 | |
937 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); | |
938 if (rval != 0) | |
939 return; | |
940 if (v1) | |
941 { | |
942 as -> skipcond = 1; | |
943 as -> skipcount = 1; | |
944 } | |
945 } | |
946 | |
947 OPFUNC(pseudo_iflt) | |
948 { | |
949 int v1; | |
950 int rval; | |
951 | |
952 if (as -> skipcond && !(as -> skipmacro)) | |
953 { | |
954 as -> skipcount++; | |
955 skip_operand(p); | |
956 return; | |
957 } | |
958 | |
959 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); | |
960 if (rval != 0) | |
961 return; | |
962 if (v1 >= 0) | |
963 { | |
964 as -> skipcond = 1; | |
965 as -> skipcount = 1; | |
966 } | |
967 } | |
968 | |
969 OPFUNC(pseudo_ifle) | |
970 { | |
971 int v1; | |
972 int rval; | |
973 | |
974 if (as -> skipcond && !(as -> skipmacro)) | |
975 { | |
976 as -> skipcount++; | |
977 skip_operand(p); | |
978 return; | |
979 } | |
980 | |
981 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); | |
982 if (rval != 0) | |
983 return; | |
984 if (v1 > 0) | |
985 { | |
986 as -> skipcond = 1; | |
987 as -> skipcount = 1; | |
988 } | |
989 } | |
990 | |
991 OPFUNC(pseudo_ifgt) | |
992 { | |
993 int v1; | |
994 int rval; | |
995 | |
996 if (as -> skipcond && !(as -> skipmacro)) | |
997 { | |
998 as -> skipcount++; | |
999 skip_operand(p); | |
1000 return; | |
1001 } | |
1002 | |
1003 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); | |
1004 if (rval != 0) | |
1005 return; | |
1006 if (v1 <= 0) | |
1007 { | |
1008 as -> skipcond = 1; | |
1009 as -> skipcount = 1; | |
1010 } | |
1011 } | |
1012 | |
1013 OPFUNC(pseudo_ifge) | |
1014 { | |
1015 int v1; | |
1016 int rval; | |
1017 | |
1018 if (as -> skipcond && !(as -> skipmacro)) | |
1019 { | |
1020 as -> skipcount++; | |
1021 skip_operand(p); | |
1022 return; | |
1023 } | |
1024 | |
1025 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); | |
1026 if (rval != 0) | |
1027 return; | |
1028 if (v1 < 0) | |
1029 { | |
1030 as -> skipcond = 1; | |
1031 as -> skipcount = 1; | |
1032 } | |
1033 } | |
1034 | |
1035 OPFUNC(pseudo_error) | |
1036 { | |
1037 register_error(as, l, 1, "User error: %s", *p); | |
1038 } | |
1039 | |
1040 | |
1041 OPFUNC(pseudo_section) | |
1042 { | |
1043 sectiontab_t *s; | |
1044 char *p2; | |
1045 char *sn; | |
1046 char *opts; | |
1047 | |
1048 | |
1049 if (as -> outformat != OUTPUT_OBJ) | |
1050 { | |
1051 register_error(as, l, 1, "Sections only supported for obj target"); | |
1052 return; | |
1053 } | |
1054 | |
1055 if (as -> csect) | |
1056 { | |
1057 as -> csect -> offset = as -> addr; | |
1058 as -> csect = NULL; | |
1059 } | |
1060 | |
1061 if (!**p) | |
1062 { | |
1063 register_error(as, l, 1, "Need section name"); | |
1064 return; | |
1065 } | |
1066 | |
1067 for (p2 = *p; *p2 && !isspace(*p2); p2++) | |
1068 /* do nothing */ ; | |
1069 | |
1070 sn = lwasm_alloc(p2 - *p + 1); | |
1071 memcpy(sn, *p, p2 - *p); | |
1072 sn[p2 - *p] = '\0'; | |
1073 | |
1074 *p = p2; | |
1075 | |
1076 opts = strchr(sn, ','); | |
1077 if (opts) | |
1078 { | |
1079 *opts++ = '\0'; | |
1080 } | |
1081 | |
1082 // have we seen the section name already? | |
1083 for (s = as -> sections; s; s = s -> next) | |
1084 { | |
1085 if (!strcmp(s -> name, sn)) | |
1086 break; | |
1087 } | |
1088 | |
1089 if (s && as -> passnum == 1) | |
1090 { | |
1091 lwasm_free(sn); | |
1092 if (opts) | |
1093 { | |
1094 register_error(as, l, 1, "Section options can only be specified the first time"); | |
1095 return; | |
1096 } | |
1097 } | |
1098 else if (!s) | |
1099 { | |
1100 s = lwasm_alloc(sizeof(sectiontab_t)); | |
1101 s -> name = sn; | |
1102 s -> offset = 0; | |
1103 s -> flags = 0; | |
1104 s -> obytes = NULL; | |
1105 s -> oblen = 0; | |
1106 s -> obsize = 0; | |
1107 s -> rl = NULL; | |
1108 s -> exports = NULL; | |
1109 // if name of section is "bss" or ".bss", assume bss flag | |
1110 // which can be overridden with !bss flag | |
1111 if (!strcasecmp(sn, "bss") || !strcasecmp(sn, ".bss")) | |
1112 s -> flags = SECTION_BSS; | |
1113 // parse options; only one "bss" | |
1114 if (opts && as -> passnum == 1) | |
1115 { | |
1116 if (!strcasecmp(opts, "bss")) | |
1117 { | |
1118 s -> flags |= SECTION_BSS; | |
1119 } | |
1120 else if (!strcasecmp(opts, "!bss")) | |
1121 { | |
1122 s -> flags &= ~SECTION_BSS; | |
1123 } | |
1124 else | |
1125 { | |
1126 register_error(as, l, 1, "Unrecognized section option '%s'", opts); | |
1127 lwasm_free(s -> name); | |
1128 lwasm_free(s); | |
1129 return; | |
1130 } | |
1131 } | |
1132 | |
1133 s -> next = as -> sections; | |
1134 as -> sections = s; | |
1135 } | |
1136 as -> addr = s -> offset; | |
1137 as -> csect = s; | |
1138 as -> context = lwasm_next_context(as); | |
1139 } | |
1140 | |
1141 OPFUNC(pseudo_endsection) | |
1142 { | |
1143 if (as -> outformat != OUTPUT_OBJ) | |
1144 { | |
1145 register_error(as, l, 1, "Sections only supported for obj target"); | |
1146 return; | |
1147 } | |
1148 | |
1149 if (!(as -> csect)) | |
1150 { | |
1151 register_error(as, l, 1, "ENDSECTION when not in a section"); | |
1152 return; | |
1153 } | |
1154 | |
1155 as -> csect -> offset = as -> addr; | |
1156 as -> addr = 0; | |
1157 as -> csect = 0; | |
1158 as -> context = lwasm_next_context(as); | |
1159 skip_operand(p); | |
1160 } | |
1161 | |
1162 OPFUNC(pseudo_extern) | |
1163 { | |
1164 if (as -> passnum != 1) | |
1165 return; | |
1166 | |
1167 if (as -> outformat != OUTPUT_OBJ) | |
1168 { | |
1169 register_error(as, l, 1, "External references only supported for obj target"); | |
1170 return; | |
1171 } | |
1172 | |
1173 if (as -> csect) | |
1174 { | |
1175 register_error(as, l, 1, "Cannot declare external symbols within a section"); | |
1176 return; | |
1177 } | |
1178 | |
1179 if (l -> sym) | |
1180 { | |
1181 lwasm_register_symbol(as, l, l -> sym, 0, SYMBOL_EXTERN); | |
1182 for ( ; **p; (*p)++) ; | |
1183 return; | |
1184 } | |
1185 | |
1186 while (**p) | |
1187 { | |
1188 char *sym2, *sym3; | |
1189 for (sym2 = *p; **p && !isspace(**p) && **p != ','; (*p)++) | |
1190 /* do nothing */ ; | |
1191 | |
1192 if (l -> sym) | |
1193 lwasm_free(l -> sym); | |
1194 | |
1195 sym3 = lwasm_alloc(*p - sym2 + 1); | |
1196 memcpy(sym3, sym2, *p - sym2); | |
1197 sym3[*p - sym2] = '\0'; | |
1198 | |
1199 l -> sym = sym3; | |
1200 debug_message(2, "import symbol: '%s'", sym3); | |
1201 lwasm_register_symbol(as, l, l -> sym, 0, SYMBOL_EXTERN); | |
1202 if (**p && (**p != ',')) | |
1203 { | |
1204 register_error(as, l, 1, "Bad import list"); | |
1205 return; | |
1206 } | |
1207 if (**p == ',') | |
1208 (*p)++; | |
1209 } | |
1210 if (!(l -> sym)) | |
1211 register_error(as, l, 1, "Bad import list"); | |
1212 } | |
1213 | |
1214 OPFUNC(pseudo_export) | |
1215 { | |
1216 lwasm_symbol_ent_t *se; | |
1217 export_list_t *ex; | |
1218 char *sym2, *sym3; | |
1219 int after = 0; | |
1220 | |
1221 if (as -> outformat != OUTPUT_OBJ) | |
1222 { | |
1223 register_error(as, l, 1, "Symbol exports only supported for obj target"); | |
1224 return; | |
1225 } | |
1226 | |
1227 if (as -> passnum == 1) | |
1228 { | |
1229 skip_operand(p); | |
1230 return; | |
1231 } | |
1232 | |
1233 if (l -> sym) | |
1234 { | |
1235 for ( ; **p; (*p)++) ; | |
1236 } | |
1237 | |
1238 again: | |
1239 if (!(l -> sym) || after == 1) | |
1240 { | |
1241 | |
1242 after = 1; | |
1243 // look for symbol after op | |
1244 if (**p) | |
1245 { | |
1246 for (sym2 = *p; **p && !isspace(**p) && **p != ','; (*p)++) | |
1247 /* do nothing */ ; | |
1248 | |
1249 debug_message(2, "export expression: %s", sym2); | |
1250 if (l -> sym) | |
1251 lwasm_free(l -> sym); | |
1252 sym3 = lwasm_alloc(*p - sym2 + 1); | |
1253 memcpy(sym3, sym2, *p - sym2); | |
1254 sym3[*p - sym2] = '\0'; | |
1255 | |
1256 l -> sym = sym3; | |
1257 debug_message(2, "export symbol: '%s'", sym3); | |
1258 } | |
1259 } | |
1260 if (!(l -> sym)) | |
1261 { | |
1262 register_error(as, l, 2, "No symbol"); | |
1263 return; | |
1264 } | |
1265 | |
1266 | |
1267 // the symbol better be defined at this point (pass 2) | |
1268 // local symbols cannot be exported nor can "global" symbols | |
1269 se = lwasm_find_symbol(as, l -> sym, -1); | |
1270 if ((!se || (se -> flags & SYMBOL_EXTERN)) && (as -> pragmas & PRAGMA_IMPORTUNDEFEXPORT)) | |
1271 { | |
1272 void *p; | |
1273 p = as -> csect; | |
1274 as -> csect = NULL; | |
1275 lwasm_register_symbol(as, l, l -> sym, 0, SYMBOL_EXTERN); | |
1276 as -> csect = p; | |
1277 } | |
1278 else | |
1279 { | |
1280 if (!se) | |
1281 { | |
1282 register_error(as, l, 2, "Exported symbols must be fully defined within a section"); | |
1283 return; | |
1284 } | |
1285 if (se -> sect == NULL) | |
1286 { | |
1287 register_error(as, l, 2, "Only non-local symbols within a section can be exported"); | |
1288 return; | |
1289 } | |
1290 | |
1291 if (se -> flags & SYMBOL_SET) | |
1292 { | |
1293 register_error(as, l, 2, "You cannot export symbols defined with SET"); | |
1294 return; | |
1295 } | |
1296 | |
1297 // if the symbol is not already a simple value, re-evaluate it | |
1298 // and see if it becomes simple | |
1299 | |
1300 | |
1301 if (se -> flags & SYMBOL_COMPLEX) | |
1302 { | |
1303 register_error(as, l, 2, "Exported symbols must be fully resolved on pass 2"); | |
1304 return; | |
1305 } | |
1306 | |
1307 // search for existing export | |
1308 for (ex = se -> sect -> exports; ex; ex = ex -> next) | |
1309 if (!strcmp(l -> sym, ex -> sym)) | |
1310 break; | |
1311 if (ex) | |
1312 { | |
1313 register_error(as, l, 2, "Symbol %s already exported", l -> sym); | |
1314 return; | |
1315 } | |
1316 | |
1317 // add an external reference | |
1318 ex = lwasm_alloc(sizeof(export_list_t)); | |
1319 ex -> next = se -> sect -> exports; | |
1320 se -> sect -> exports = ex; | |
1321 ex -> offset = se -> value; | |
1322 ex -> sym = lwasm_strdup(se -> sym); | |
1323 } | |
1324 next: | |
1325 if (after == 1) | |
1326 { | |
1327 if (**p == ',') | |
1328 { | |
1329 debug_message(2, "Export another symbol: %s", *p); | |
1330 (*p)++; | |
1331 for ( ; **p && isspace(**p); (*p)++) | |
1332 /* do nothing */ ; | |
1333 goto again; | |
1334 } | |
1335 } | |
1336 } |