]> Shamusworld >> Repos - rmac/blob - direct.c
aebe56648760f3ee3add972e7ef1b68eb56c44de
[rmac] / direct.c
1 //
2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // DIRECT.C - Directive Handling
4 // Copyright (C) 199x Landon Dyer, 2017 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
7 //
8
9 #include "direct.h"
10 #include "6502.h"
11 #include "error.h"
12 #include "expr.h"
13 #include "listing.h"
14 #include "mach.h"
15 #include "macro.h"
16 #include "mark.h"
17 #include "procln.h"
18 #include "riscasm.h"
19 #include "sect.h"
20 #include "symbol.h"
21 #include "token.h"
22
23 #define DEF_KW
24 #include "kwtab.h"
25
26 TOKEN exprbuf[128];                     // Expression buffer
27 SYM * symbolPtr[1000000];       // Symbol pointers table
28 static long unused;                     // For supressing 'write' warnings
29 char buffer[256];                       // Scratch buffer for messages
30 int stringtype;             // Non-zero if we need any special string conversions
31
32 // Function prototypes
33 int d_unimpl(void);
34 int d_68000(void);
35 int d_bss(void);
36 int d_data(void);
37 int d_text(void);
38 int d_abs(void);
39 int d_comm(void);
40 int d_dc(WORD);
41 int d_ds(WORD);
42 int d_dcb(WORD);
43 int d_globl(void);
44 int d_gpu(void);
45 int d_dsp(void);
46 int d_assert(void);
47 int d_include(void);
48 int d_list(void);
49 int d_nlist(void);
50 int d_error(char *);
51 int d_warn(char *);
52 int d_org(void);
53 int d_init(WORD);
54 int d_cargs(void);
55 int d_undmac(void);
56 int d_regbank0(void);
57 int d_regbank1(void);
58 int d_incbin(void);
59 int d_noclear(void);
60 int d_equrundef(void);
61 int d_ccundef(void);
62 int d_print(void);
63 int d_gpumain(void);
64 int d_jpad(void);
65 int d_nojpad(void);
66 int d_fail(void);
67 int d_cstruct(void);
68 int d_prgflags(void);
69 int d_opt(void);
70
71 // Directive handler table
72 int (*dirtab[])() = {
73         d_org,                          // 0 org
74         d_even,                         // 1 even
75         d_6502,                         // 2 .6502
76         d_68000,                        // 3 .68000
77         d_bss,                          // 4 bss
78         d_data,                         // 5 data
79         d_text,                         // 6 text
80         d_abs,                          // 7 abs
81         d_comm,                         // 8 comm
82         (void *)d_init,         // 9 init
83         d_cargs,                        // 10 cargs
84         (void *)d_goto,         // 11 goto
85         (void *)d_dc,           // 12 dc
86         (void *)d_ds,           // 13 ds
87         d_undmac,                       // 14 undefmac
88         d_gpu,                          // 15 .gpu
89         d_dsp,                          // 16 .dsp
90         (void *)d_dcb,          // 17 dcb
91         d_unimpl,                       // 18* set
92         d_unimpl,                       // 19* reg
93         d_unimpl,                       // 20 dump
94         d_incbin,                       // 21 .incbin //load
95         d_unimpl,                       // 22 disable
96         d_unimpl,                       // 23 enable
97         d_globl,                        // 24 globl
98         d_regbank0,                     // 25 .regbank0
99         d_regbank1,                     // 26 .regbank1
100         d_unimpl,                       // 27 xdef
101         d_assert,                       // 28 assert
102         d_unimpl,                       // 29* if
103         d_unimpl,                       // 30* endif
104         d_unimpl,                       // 31* endc
105         d_unimpl,                       // 32* iif
106         d_include,                      // 33 include
107         fpop,                           // 34 end
108         d_unimpl,                       // 35* macro
109         ExitMacro,                      // 36* exitm
110         d_unimpl,                       // 37* endm
111         d_list,                         // 38 list
112         d_nlist,                        // 39 nlist
113         d_long,                         // 40* rept
114         d_phrase,                       // 41* endr
115         d_dphrase,                      // 42 struct
116         d_qphrase,                      // 43 ends
117         d_title,                        // 44 title
118         d_subttl,                       // 45 subttl
119         eject,                          // 46 eject
120         d_error,                        // 47 error
121         d_warn,                         // 48 warn
122         d_noclear,                      // 49 .noclear
123         d_equrundef,            // 50 .equrundef/.regundef
124         d_ccundef,                      // 51 .ccundef
125         d_print,                        // 52 .print
126         d_cstruct,                      // 53 .cstruct
127         d_jpad,                         // 54 .jpad (deprecated)
128         d_nojpad,                       // 55 .nojpad (deprecated)
129         d_gpumain,                      // 56 .gpumain (deprecated)
130         d_prgflags,                     // 57 .prgflags
131         d_opt,                          // 58 .opt
132 };
133
134
135 //
136 // .error - Abort compilation, printing an error message
137 //
138 int d_error(char *str)
139 {
140         if (*tok == EOL)
141                 return error("error directive encountered - aborting assembling");
142         else
143         {
144                 switch(*tok)
145                 {
146                 case STRING:
147                         return error(string[tok[1]]);
148                         break;
149                 default:
150                         return error("error directive encountered - aborting assembling");
151                 }
152         }
153 }
154
155
156 //
157 // .warn - Just display a warning on screen
158 //
159 int d_warn(char *str)
160 {
161         if (*tok == EOL)
162                 return warn("WARNING WARNING WARNING");
163         else
164         {
165                 switch(*tok)
166                 {
167                 case STRING:
168                         return warn(string[tok[1]]);
169                         break;
170                 default:
171                         return warn("WARNING WARNING WARNING");
172                 }
173         }
174 }
175
176
177 //
178 // .org - Set origin
179 //
180 int d_org(void)
181 {
182         VALUE address;
183
184         if (!rgpu && !rdsp && !m6502)
185                 return error(".org permitted only in gpu/dsp and 6502 sections");
186
187         if (abs_expr(&address) == ERROR)
188         {
189                 error("cannot determine org'd address");
190                 return ERROR;
191         }
192
193         if (rgpu | rdsp)
194         {
195                 orgaddr = address;
196                 orgactive = 1;
197         }
198         else
199         {
200                 // 6502.  We also kludge `lsloc' so the listing generator doesn't try
201                 // to spew out megabytes.
202                 if (address > 0xFFFF)
203                         return error(range_error);
204
205                 if (sloc != currentorg[0])
206                 {
207                         currentorg[1] = sloc;
208                         currentorg += 2;
209                 }
210
211                 currentorg[0] = address;
212                 ch_size = 0;
213                 lsloc = sloc = address;
214                 chptr = scode->chptr + address;
215                 orgaddr = address;
216                 orgactive = 1;
217                 at_eol();
218         }
219
220         return 0;
221 }
222
223
224 //
225 // Print directive
226 //
227 int d_print(void)
228 {
229         char prntstr[LNSIZ];            // String for PRINT directive
230         char format[LNSIZ];                     // Format for PRINT directive
231         int formatting = 0;                     // Formatting on/off
232         int wordlong = 0;                       // WORD = 0, LONG = 1
233         int outtype = 0;                        // 0:hex, 1:decimal, 2:unsigned
234
235         VALUE eval;                                     // Expression value
236         WORD eattr;                                     // Expression attributes
237         SYM * esym;                                     // External symbol involved in expr.
238         TOKEN r_expr[EXPRSIZE];
239
240         while (*tok != EOL)
241         {
242                 switch(*tok)
243                 {
244                 case STRING:
245                         sprintf(prntstr, "%s", string[tok[1]]);
246                         printf("%s", prntstr);
247
248                         if (list_fd)
249                                 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
250
251                         tok += 2;
252                         break;
253                 case '/':
254                         formatting = 1;
255
256                         if (tok[1] != SYMBOL)
257                                 goto token_err;
258
259 //                      strcpy(prntstr, (char *)tok[2]);
260                         strcpy(prntstr, string[tok[2]]);
261
262                         switch(prntstr[0])
263                         {
264                         case 'l': case 'L': wordlong = 1; break;
265                         case 'w': case 'W': wordlong = 0; break;
266                         case 'x': case 'X': outtype  = 0; break;
267                         case 'd': case 'D': outtype  = 1; break;
268                         case 'u': case 'U': outtype  = 2; break;
269                         default:
270                                 error("unknown print format flag");
271                                 return ERROR;
272                         }
273
274                         tok += 3;
275                         break;
276                 case ',':
277                         tok++;
278                         break;
279                 default:
280                         if (expr(r_expr, &eval, &eattr, &esym) != OK)
281                                 goto token_err;
282                         else
283                         {
284                                 switch(outtype)
285                                 {
286                                 case 0: strcpy(format, "%X"); break;
287                                 case 1: strcpy(format, "%d" ); break;
288                                 case 2: strcpy(format, "%u" ); break;
289                                 }
290
291                                 if (wordlong)
292                                         sprintf(prntstr, format, eval);
293                                 else
294                                         sprintf(prntstr, format, eval & 0xFFFF);
295
296                                 printf("%s", prntstr);
297
298                                 if (list_fd)
299                                         unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
300
301                                 formatting = 0;
302                                 wordlong = 0;
303                                 outtype = 0;
304                         }
305
306                         break;
307                 }
308         }
309
310         printf("\n");
311
312         return 0;
313
314 token_err:
315         error("illegal print token");
316         return ERROR;
317 }
318
319
320 //
321 // Undefine an equated condition code
322 //
323 int d_ccundef(void)
324 {
325         SYM * ccname;
326
327         // Check that we are in a RISC section
328         if (!rgpu && !rdsp)
329         {
330                 error(".ccundef must be defined in .gpu/.dsp section");
331                 return ERROR;
332         }
333
334         if (*tok != SYMBOL)
335         {
336                 error("syntax error; expected symbol");
337                 return ERROR;
338         }
339
340         ccname = lookup(string[tok[1]], LABEL, 0);
341
342         // Make sure symbol is a valid ccdef
343         if (!ccname || !(ccname->sattre & EQUATEDCC))
344         {
345                 error("invalid equated condition name specified");
346                 return ERROR;
347         }
348
349         ccname->sattre |= UNDEF_CC;
350
351         return 0;
352 }
353
354
355 //
356 // Undefine an equated register
357 //
358 int d_equrundef(void)
359 {
360         SYM * regname;
361
362         // Check that we are in a RISC section
363         if (!rgpu && !rdsp)
364                 return error(".equrundef/.regundef must be defined in .gpu/.dsp section");
365
366         while (*tok != EOL)
367         {
368                 // Skip preceeding or seperating commas (if any)
369                 if (*tok == ',')
370                         tok++;
371
372                 // Check we are dealing with a symbol
373                 if (*tok != SYMBOL)
374                         return error("syntax error; expected symbol");
375
376                 // Lookup and undef if equated register
377                 regname = lookup(string[tok[1]], LABEL, 0);
378
379                 if (regname && (regname->sattre & EQUATEDREG))
380                 {
381                         // Reset the attributes of this symbol...
382                         regname->sattr = 0;
383                         regname->sattre &= ~(EQUATEDREG | BANK_0 | BANK_1);
384                         regname->sattre |= UNDEF_EQUR;
385                 }
386
387                 // Skip over symbol token and address
388                 tok += 2;
389         }
390
391         return 0;
392 }
393
394
395 //
396 // Do not allow use of the CLR.L opcode
397 //
398 int d_noclear(void)
399 {
400         warn("CLR.L opcode ignored...");
401         return 0;
402 }
403
404
405 //
406 // Include binary file
407 //
408 int d_incbin(void)
409 {
410         int fd;
411         int bytes = 0;
412         long pos, size, bytesRead;
413         char msg[256];
414         char buf1[256];
415         int i;
416
417         // Check to see if we're in BSS, and, if so, throw an error
418         if (scattr & SBSS)
419         {
420                 errors("cannot include binary file \"%s\" in BSS section", string[tok[1]]);
421                 return ERROR;
422         }
423
424         if (*tok != STRING)
425         {
426                 error("syntax error; string missing");
427                 return ERROR;
428         }
429
430         // Attempt to open the include file in the current directory, then (if that
431         // failed) try list of include files passed in the enviroment string or by
432         // the "-d" option.
433         if ((fd = open(string[tok[1]], _OPEN_INC)) < 0)
434         {
435                 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
436                 {
437                         fd = strlen(buf1);
438
439                         // Append path char if necessary
440                         if (fd > 0 && buf1[fd - 1] != SLASHCHAR)
441                                 strcat(buf1, SLASHSTRING);
442
443                         strcat(buf1, string[tok[1]]);
444
445                         if ((fd = open(buf1, _OPEN_INC)) >= 0)
446                                 goto allright;
447                 }
448
449                 return errors("cannot open: \"%s\"", string[tok[1]]);
450         }
451
452 allright:
453
454         size = lseek(fd, 0L, SEEK_END);
455         pos = lseek(fd, 0L, SEEK_SET);
456         chcheck(size);
457
458         DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size); }
459
460         char * fileBuffer = (char *)malloc(size);
461         bytesRead = read(fd, fileBuffer, size);
462
463         if (bytesRead != size)
464         {
465                 sprintf(msg, "was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[tok[1]], size);
466                 error(msg);
467                 return ERROR;
468         }
469
470         memcpy(chptr, fileBuffer, size);
471         chptr += size;
472         sloc += size;
473         ch_size += size;
474
475         if (orgactive)
476                 orgaddr += size;
477
478         free(fileBuffer);
479         close(fd);
480         return 0;
481 }
482
483
484 //
485 // Set RISC register banks
486 //
487 int d_regbank0(void)
488 {
489         // Set active register bank zero
490         regbank = BANK_0;
491         return 0;
492 }
493
494
495 int d_regbank1(void)
496 {
497         // Set active register bank one
498         regbank = BANK_1;
499         return 0;
500 }
501
502
503 //
504 // Helper function, to cut down on mistakes & typing
505 //
506 static inline void SkipBytes(unsigned bytesToSkip)
507 {
508         if (!bytesToSkip)
509                 return;
510
511         if ((scattr & SBSS) == 0)
512         {
513                 chcheck(bytesToSkip);
514                 D_ZEROFILL(bytesToSkip);
515         }
516         else
517         {
518                 sloc += bytesToSkip;
519
520                 if (orgactive)
521                         orgaddr += bytesToSkip;
522         }
523 }
524
525
526 //
527 // Adjust location to an EVEN value
528 //
529 int d_even(void)
530 {
531         if (m6502)
532                 return error(in_6502mode);
533
534         unsigned skip = (rgpu || rdsp ? orgaddr : sloc) & 0x01;
535
536         if (skip)
537         {
538                 if ((scattr & SBSS) == 0)
539                 {
540                         chcheck(1);
541                         D_byte(0);
542                 }
543                 else
544                 {
545                         sloc++;
546
547                         if (orgactive)
548                                 orgaddr++;
549                 }
550         }
551
552         return 0;
553 }
554
555
556 //
557 // Adjust location to a LONG value
558 //
559 int d_long(void)
560 {
561         unsigned lower2Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x03;
562         unsigned bytesToSkip = (0x04 - lower2Bits) & 0x03;
563         SkipBytes(bytesToSkip);
564
565         return 0;
566 }
567
568
569 //
570 // Adjust location to a PHRASE value
571 //
572 // N.B.: We have to handle the GPU/DSP cases separately because you can embed
573 //       RISC code in the middle of a regular 68K section. Also note that all
574 //       of the alignment pseudo-ops will have to be fixed this way.
575 //
576 // This *must* behave differently when in a RISC section, as following sloc
577 // (instead of orgaddr) will fuck things up royally. Note that we do it this
578 // way because you can embed RISC code in a 68K section, and have the origin
579 // pointing to a different alignment in the RISC section than the 68K section.
580 //
581 int d_phrase(void)
582 {
583         unsigned lower3Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x07;
584         unsigned bytesToSkip = (0x08 - lower3Bits) & 0x07;
585         SkipBytes(bytesToSkip);
586
587         return 0;
588 }
589
590
591 //
592 // Adjust location to a DPHRASE value
593 //
594 int d_dphrase(void)
595 {
596         unsigned lower4Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x0F;
597         unsigned bytesToSkip = (0x10 - lower4Bits) & 0x0F;
598         SkipBytes(bytesToSkip);
599
600         return 0;
601 }
602
603
604 //
605 // Adjust location to a QPHRASE value
606 //
607 int d_qphrase(void)
608 {
609         unsigned lower5Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x1F;
610         unsigned bytesToSkip = (0x20 - lower5Bits) & 0x1F;
611         SkipBytes(bytesToSkip);
612
613         return 0;
614 }
615
616
617 //
618 // Do auto-even.  This must be called ONLY if 'sloc' is odd.
619 //
620 // This is made hairy because, if there was a label on the line, we also have
621 // to adjust its value. This won't work with more than one label on the line,
622 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
623 // the C compiler is VERY paranoid and uses ".even" whenever it can
624 //
625 // N.B.: This probably needs the same fixes as above...
626 //
627 void auto_even(void)
628 {
629         if (cursect != M6502)
630         {
631                 if (scattr & SBSS)
632                         sloc++;                         // Bump BSS section
633                 else
634                         D_byte(0);                      // Deposit 0.b in non-BSS
635
636                 if (lab_sym != NULL)    // Bump label if we have to
637                         lab_sym->svalue++;
638         }
639 }
640
641
642 //
643 // Unimplemened directive error
644 //
645 int d_unimpl(void)
646 {
647         return error("unimplemented directive");
648 }
649
650
651 //
652 // Return absolute (not TDB) and defined expression or return an error
653 //
654 int abs_expr(VALUE * a_eval)
655 {
656         WORD eattr;
657
658         if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
659                 return ERROR;
660
661         if (!(eattr & DEFINED))
662                 return error(undef_error);
663
664         if (eattr & TDB)
665                 return error(rel_error);
666
667         return OK;
668 }
669
670
671 //
672 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
673 //
674 int symlist(int(* func)())
675 {
676         const char * em = "symbol list syntax";
677
678         for(;;)
679         {
680                 if (*tok != SYMBOL)
681                         return error(em);
682
683                 if ((*func)(string[tok[1]]) != OK)
684                         break;
685
686                 tok += 2;
687
688                 if (*tok == EOL)
689                         break;
690
691                 if (*tok != ',')
692                         return error(em);
693
694                 tok++;
695         }
696
697         return 0;
698 }
699
700
701 //
702 // .include "filename"
703 //
704 int d_include(void)
705 {
706         int j;
707         int i;
708         char * fn;
709         char buf[128];
710         char buf1[128];
711
712         if (*tok == STRING)                     // Leave strings ALONE
713                 fn = string[*++tok];
714         else if (*tok == SYMBOL)        // Try to append ".s" to symbols
715         {
716                 strcpy(buf, string[*++tok]);
717                 fext(buf, ".s", 0);
718                 fn = &buf[0];
719         }
720         else                                            // Punt if no STRING or SYMBOL
721                 return error("missing filename");
722
723         // Make sure the user didn't try anything like:
724         // .include equates.s
725         if (*++tok != EOL)
726                 return error("extra stuff after filename -- enclose it in quotes");
727
728         // Attempt to open the include file in the current directory, then (if that
729         // failed) try list of include files passed in the enviroment string or by
730         // the "-i" option.
731         if ((j = open(fn, 0)) < 0)
732         {
733                 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
734                 {
735                         j = strlen(buf1);
736
737                         // Append path char if necessary
738                         if (j > 0 && buf1[j - 1] != SLASHCHAR)
739                                 strcat(buf1, SLASHSTRING);
740
741                         strcat(buf1, fn);
742
743                         if ((j = open(buf1, 0)) >= 0)
744                                 goto allright;
745                 }
746
747                 return errors("cannot open: \"%s\"", fn);
748         }
749
750 allright:
751         include(j, fn);
752         return 0;
753 }
754
755
756 //
757 // .assert expression [, expression...]
758 //
759 int d_assert(void)
760 {
761         WORD eattr;
762         VALUE eval;
763
764         for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
765         {
766                 if (!(eattr & DEFINED))
767                         return error("forward or undefined .assert");
768
769                 if (!eval)
770                         return error("assert failure");
771
772                 if (*tok != ',')
773                         break;
774         }
775
776         at_eol();
777         return 0;
778 }
779
780
781 //
782 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
783 //
784 int globl1(char * p)
785 {
786         SYM * sy;
787
788         if (*p == '.')
789                 return error("cannot .globl local symbol");
790
791         if ((sy = lookup(p, LABEL, 0)) == NULL)
792         {
793                 sy = NewSymbol(p, LABEL, 0);
794                 sy->svalue = 0;
795                 sy->sattr = GLOBAL;
796 //printf("glob1: Making global symbol: attr=%04X, eattr=%08X, %s\n", sy->sattr, sy->sattre, sy->sname);
797         }
798         else
799                 sy->sattr |= GLOBAL;
800
801         return OK;
802 }
803
804
805 int d_globl(void)
806 {
807         if (m6502)
808                 return error(in_6502mode);
809
810         symlist(globl1);
811         return 0;
812 }
813
814
815 //
816 // .prgflags expression
817 //
818 int d_prgflags(void)
819 {
820         VALUE eval;
821
822         if (*tok == EOL)
823                 return error("PRGFLAGS requires value");
824         else if (abs_expr(&eval) == OK)
825         {
826                 PRGFLAGS=eval;
827                 return 0;
828         }
829         else
830         {
831                 return error("PRGFLAGS requires value");
832         }
833 }
834
835
836 //
837 // .abs [expression]
838 //
839 int d_abs(void)
840 {
841         VALUE eval;
842
843         if (m6502)
844                 return error(in_6502mode);
845
846         SaveSection();
847
848         if (*tok == EOL)
849                 eval = 0;
850         else if (abs_expr(&eval) != OK)
851                 return 0;
852
853         SwitchSection(ABS);
854         sloc = eval;
855         return 0;
856 }
857
858
859 //
860 // Switch segments
861 //
862 int d_text(void)
863 {
864         if (rgpu || rdsp)
865                 return error("directive forbidden in gpu/dsp mode");
866         else if (m6502)
867                 return error(in_6502mode);
868
869         if (cursect != TEXT)
870         {
871                 SaveSection();
872                 SwitchSection(TEXT);
873         }
874
875         return 0;
876 }
877
878
879 int d_data(void)
880 {
881         if (rgpu || rdsp)
882                 return error("directive forbidden in gpu/dsp mode");
883         else if (m6502)
884                 return error(in_6502mode);
885
886         if (cursect != DATA)
887         {
888                 SaveSection();
889                 SwitchSection(DATA);
890         }
891
892         return 0;
893 }
894
895
896 int d_bss(void)
897 {
898         if (rgpu || rdsp)
899                 return error("directive forbidden in gpu/dsp mode");
900         else if (m6502)
901                 return error(in_6502mode);
902
903         if (cursect != BSS)
904         {
905                 SaveSection();
906                 SwitchSection(BSS);
907         }
908
909         return 0;
910 }
911
912
913 //
914 // .ds[.size] expression
915 //
916 int d_ds(WORD siz)
917 {
918         DEBUG { printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc); }
919
920         VALUE eval;
921
922         if (cursect != M6502)
923         {
924                 if ((siz != SIZB) && (sloc & 1))        // Automatic .even
925                         auto_even();
926         }
927
928         if (abs_expr(&eval) != OK)
929                 return 0;
930
931         // Check to see if the value being passed in is negative (who the hell does
932         // that?--nobody does; it's the code gremlins, or rum, that does it)
933         if (eval < 0)
934                 return error("negative sizes not allowed");
935
936         // In non-TDB section (BSS, ABS and M6502) just advance the location
937         // counter appropriately. In TDB sections, deposit (possibly large) chunks
938         // of zeroed memory....
939         if ((scattr & SBSS) || cursect == M6502)
940         {
941                 listvalue(eval);
942                 eval *= siz;
943                 sloc += eval;
944
945                 if (cursect == M6502)
946                         chptr += eval;
947
948                 just_bss = 1;                                   // No data deposited (8-bit CPU mode)
949         }
950         else
951         {
952                 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED | ABS), NULL);
953         }
954
955         at_eol();
956         return 0;
957 }
958
959
960 //
961 // dc.b, dc.w / dc, dc.l, dc.i
962 //
963 int d_dc(WORD siz)
964 {
965         WORD eattr;
966         VALUE eval;
967         uint8_t * p;
968
969         if ((scattr & SBSS) != 0)
970                 return error("illegal initialization of section");
971
972         // Do an auto_even if it's not BYTE sized (hmm, should we be doing this???)
973         if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
974                 auto_even();
975
976         // Check to see if we're trying to set LONGS on a non 32-bit aligned
977         // address in a GPU or DSP section, in their local RAM
978         if ((siz == SIZL) && (orgaddr & 0x03)
979                 && ((rgpu && (orgaddr >= 0xF03000) && (orgaddr <= 0xF03FFFF))
980                 || (rdsp && (orgaddr >= 0xF1B000) && (orgaddr <= 0xF1CFFFF))))
981                 warn("depositing LONGs on a non-long address in local RAM");
982
983         for(;; tok++)
984         {
985                 // dc.b 'string' [,] ...
986                 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
987                 {
988                         uint32_t i = strlen(string[tok[1]]);
989
990                         if ((challoc - ch_size) < i)
991                                 chcheck(i);
992
993                         if (stringtype == NORMAL)
994                         {
995                                 for(p=string[tok[1]]; *p!=EOS; p++)
996                                         D_byte(*p);
997                         }
998                         else if(stringtype == A8INT)
999                         {
1000                                 for(p=string[tok[1]]; *p!=EOS; p++)
1001                                         D_byte(strtoa8[*p]);
1002                         }
1003                         else
1004                         {
1005                                 error("String format not supported... yet");
1006                         }
1007
1008
1009                         tok += 2;
1010                         goto comma;
1011                 }
1012
1013                 int movei = 0; // MOVEI flag for dc.i
1014
1015                 if (*tok == DOTI)
1016                 {
1017                         movei = 1;
1018                         tok++;
1019                         siz = SIZL;
1020                 }
1021
1022                 // dc.x <expression>
1023                 SYM * esym = 0;
1024
1025                 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
1026                         return 0;
1027
1028                 uint16_t tdb = eattr & TDB;
1029                 uint16_t defined = eattr & DEFINED;
1030
1031                 if ((challoc - ch_size) < 4)
1032                         chcheck(4);
1033
1034                 switch (siz)
1035                 {
1036                 case SIZB:
1037                         if (!defined)
1038                         {
1039                                 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1040                                 D_byte(0);
1041                         }
1042                         else
1043                         {
1044                                 if (eval + 0x100 >= 0x200)
1045                                 {
1046                                         sprintf(buffer, "%s (value = $%X)", range_error, eval);
1047                                         return error(buffer);
1048                                 }
1049
1050                                 if (tdb)
1051                                         return error("non-absolute byte value");
1052
1053                                 D_byte(eval);
1054                         }
1055
1056                         break;
1057                 case SIZW:
1058                 case SIZN:
1059                         if (!defined)
1060                         {
1061                                 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1062                                 D_word(0);
1063                         }
1064                         else
1065                         {
1066                                 if (eval + 0x10000 >= 0x20000)
1067                                         return error(range_error);
1068
1069                                 if (tdb)
1070                                         MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1071
1072                                 // Deposit 68000 or 6502 (byte-reversed) word
1073                                 if (cursect != M6502)
1074                                         D_word(eval)
1075                                 else
1076                                         D_rword(eval)
1077                         }
1078
1079                         break;
1080                 case SIZL:
1081                         if (m6502)
1082                                 return error(in_6502mode);
1083
1084                         if (!defined)
1085                         {
1086                                 if (movei)
1087                                         AddFixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
1088                                 else
1089                                         AddFixup(FU_LONG, sloc, exprbuf);
1090
1091                                 D_long(0);
1092                         }
1093                         else
1094                         {
1095                                 if (tdb)
1096                                         MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1097
1098                                 if (movei)
1099                                         eval = WORDSWAP32(eval);
1100
1101                                 D_long(eval);
1102                         }
1103                         break;
1104                 }
1105
1106 comma:
1107                 if (*tok != ',')
1108                         break;
1109         }
1110
1111         at_eol();
1112         return 0;
1113 }
1114
1115
1116 //
1117 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1118 //
1119 int d_dcb(WORD siz)
1120 {
1121         VALUE evalc, eval;
1122         WORD eattr;
1123
1124         DEBUG { printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr); }
1125
1126         if ((scattr & SBSS) != 0)
1127                 return error("illegal initialization of section");
1128
1129         if (abs_expr(&evalc) != OK)
1130                 return 0;
1131
1132         if (*tok++ != ',')
1133                 return error("missing comma");
1134
1135         if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1136                 return 0;
1137
1138         if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1139                 auto_even();
1140
1141         dep_block(evalc, siz, eval, eattr, exprbuf);
1142         return 0;
1143 }
1144
1145
1146 //
1147 // Generalized initialization directive
1148 //
1149 // .init[.siz] [#count,] expression [.size] , ...
1150 //
1151 // The size suffix on the ".init" directive becomes the default size of the
1152 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1153 // and an expression, it specifies a repeat count. The value to be deposited
1154 // may be followed by a size suffix, which overrides the default size.
1155 //
1156 int d_init(WORD def_siz)
1157 {
1158         VALUE count;
1159         VALUE eval;
1160         WORD eattr;
1161         WORD siz;
1162
1163         if ((scattr & SBSS) != 0)
1164                 return error(".init not permitted in BSS or ABS");
1165
1166         if (rgpu || rdsp)
1167                 return error("directive forbidden in gpu/dsp mode");
1168
1169         for(;;)
1170         {
1171                 // Get repeat count (defaults to 1)
1172                 if (*tok == '#')
1173                 {
1174                         ++tok;
1175
1176                         if (abs_expr(&count) != OK)
1177                                 return 0;
1178
1179                         if (*tok++ != ',')
1180                                 return error(comma_error);
1181                 }
1182                 else
1183                         count = 1;
1184
1185                 // Evaluate expression to deposit
1186                 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1187                         return 0;
1188
1189                 switch ((int)*tok++)
1190                 {                                 // Determine size of object to deposit
1191                 case DOTB: siz = SIZB; break;
1192                 case DOTW: siz = SIZB; break;
1193                 case DOTL: siz = SIZL; break;
1194                 default:
1195                         siz = def_siz;
1196                         tok--;
1197                         break;
1198                 }
1199
1200                 dep_block(count, siz, eval, eattr, exprbuf);
1201
1202                 switch ((int)*tok)
1203                 {
1204                 case EOL:
1205                         return 0;
1206                 case ',':
1207                         tok++;
1208                         continue;
1209                 default:
1210                         return error(comma_error);
1211                 }
1212         }
1213 }
1214
1215
1216 //
1217 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1218 //
1219 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1220 {
1221         WORD tdb;
1222         WORD defined;
1223
1224         tdb = (WORD)(eattr & TDB);
1225         defined = (WORD)(eattr & DEFINED);
1226
1227         while (count--)
1228         {
1229                 if ((challoc - ch_size) < 4)
1230                         chcheck(4L);
1231
1232                 switch(siz)
1233                 {
1234                 case SIZB:
1235                         if (!defined)
1236                         {
1237                                 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1238                                 D_byte(0);
1239                         }
1240                         else
1241                         {
1242                                 if (tdb)
1243                                         return error("non-absolute byte value");
1244
1245                                 if (eval + 0x100 >= 0x200)
1246                                         return error(range_error);
1247
1248                                 D_byte(eval);
1249                         }
1250
1251                         break;
1252                 case SIZW:
1253                 case SIZN:
1254                         if (!defined)
1255                         {
1256                                 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1257                                 D_word(0);
1258                         }
1259                         else
1260                         {
1261                                 if (tdb)
1262                                         MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1263
1264                                 if (eval + 0x10000 >= 0x20000)
1265                                         return error(range_error);
1266
1267                                 // Deposit 68000 or 6502 (byte-reversed) word
1268                                 if (cursect != M6502)
1269                                         D_word(eval)
1270                                 else
1271                                         D_rword(eval)
1272
1273                         }
1274
1275                         break;
1276                 case SIZL:
1277                         if (m6502)
1278                                 return error(in_6502mode);
1279
1280                         if (!defined)
1281                         {
1282                                 AddFixup(FU_LONG, sloc, exprbuf);
1283                                 D_long(0);
1284                         }
1285                         else
1286                         {
1287                                 if (tdb)
1288                                         MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1289
1290                                 D_long(eval);
1291                         }
1292
1293                         break;
1294                 }
1295         }
1296
1297         return 0;
1298 }
1299
1300
1301 //
1302 // .comm symbol, size
1303 //
1304 int d_comm(void)
1305 {
1306         SYM * sym;
1307         char * p;
1308         VALUE eval;
1309
1310         if (m6502)
1311                 return error(in_6502mode);
1312
1313         if (*tok != SYMBOL)
1314                 return error("missing symbol");
1315
1316         p = string[tok[1]];
1317         tok += 2;
1318
1319         if (*p == '.')                                                  // Cannot .comm a local symbol
1320                 return error(locgl_error);
1321
1322         if ((sym = lookup(p, LABEL, 0)) == NULL)
1323                 sym = NewSymbol(p, LABEL, 0);
1324         else
1325         {
1326                 if (sym->sattr & DEFINED)
1327                         return error(".comm symbol already defined");
1328         }
1329
1330         sym->sattr = GLOBAL | COMMON | BSS;
1331
1332         if (*tok++ != ',')
1333                 return error(comma_error);
1334
1335         if (abs_expr(&eval) != OK)                              // Parse size of common region
1336                 return 0;
1337
1338         sym->svalue = eval;                                             // Install common symbol's size
1339         at_eol();
1340         return 0;
1341 }
1342
1343
1344 //
1345 // .list - Turn listing on
1346 //
1347 int d_list(void)
1348 {
1349         if (list_flag)
1350                 listing++;
1351
1352         return 0;
1353 }
1354
1355
1356 //
1357 // .nlist - Turn listing off
1358 //
1359 int d_nlist(void)
1360 {
1361         if (list_flag)
1362                 listing--;
1363
1364         return 0;
1365 }
1366
1367
1368 //
1369 // .68000 - Back to 68000 TEXT segment
1370 //
1371 int d_68000(void)
1372 {
1373         rgpu = rdsp = 0;
1374         // Switching from gpu/dsp sections should reset any ORG'd Address
1375         orgactive = 0;
1376         orgwarning = 0;
1377         SaveSection();
1378         SwitchSection(TEXT);
1379         return 0;
1380 }
1381
1382
1383 //
1384 // .gpu - Switch to GPU assembler
1385 //
1386 int d_gpu(void)
1387 {
1388         if ((cursect != TEXT) && (cursect != DATA))
1389         {
1390                 error(".gpu can only be used in the TEXT or DATA segments");
1391                 return ERROR;
1392         }
1393
1394         // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1395         if (!rgpu)
1396         {
1397 //printf("Resetting ORG...\n");
1398                 orgactive = 0;
1399                 orgwarning = 0;
1400         }
1401 //else printf("NOT resetting ORG!\n");
1402
1403         rgpu = 1;                       // Set GPU assembly
1404         rdsp = 0;                       // Unset DSP assembly
1405         regbank = BANK_N;       // Set no default register bank
1406         return 0;
1407 }
1408
1409
1410 //
1411 // .dsp - Switch to DSP assembler
1412 //
1413 int d_dsp(void)
1414 {
1415         if ((cursect != TEXT) && (cursect != DATA))
1416         {
1417                 error(".dsp can only be used in the TEXT or DATA segments");
1418                 return ERROR;
1419         }
1420
1421         // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1422         if (!rdsp)
1423         {
1424                 orgactive = 0;
1425                 orgwarning = 0;
1426         }
1427
1428         rdsp = 1;                       // Set DSP assembly
1429         rgpu = 0;                       // Unset GPU assembly
1430         regbank = BANK_N;       // Set no default register bank
1431         return 0;
1432 }
1433
1434
1435 //
1436 // .cargs [#offset], symbol[.size], ...
1437 //
1438 // Lists of registers may also be mentioned; they just take up space. Good for
1439 // "documentation" purposes:
1440 //
1441 // .cargs a6, .arg1, .arg2, .arg3...
1442 //
1443 // Symbols thus created are ABS and EQUATED.
1444 //
1445 int d_cargs(void)
1446 {
1447         VALUE eval = 4;         // Default to 4 if no offset specified (to account for
1448                                                 // return address)
1449         WORD rlist;
1450         SYM * symbol;
1451         char * p;
1452         int env;
1453         int i;
1454
1455         if (rgpu || rdsp)
1456                 return error("directive forbidden in gpu/dsp mode");
1457
1458         if (*tok == '#')
1459         {
1460                 tok++;
1461
1462                 if (abs_expr(&eval) != OK)
1463                         return 0;
1464
1465                 // Eat the comma, if it's there
1466                 if (*tok == ',')
1467                         tok++;
1468         }
1469
1470         for(;;)
1471         {
1472                 if (*tok == SYMBOL)
1473                 {
1474                         p = string[tok[1]];
1475
1476                         // Set env to either local (dot prefixed) or global scope
1477                         env = (*p == '.' ? curenv : 0);
1478                         symbol = lookup(p, LABEL, env);
1479
1480                         if (symbol == NULL)
1481                         {
1482                                 symbol = NewSymbol(p, LABEL, env);
1483                                 symbol->sattr = 0;
1484                         }
1485                         else if (symbol->sattr & DEFINED)
1486                                 return errors("multiply-defined label '%s'", p);
1487
1488                         // Put symbol in "order of definition" list
1489                         AddToSymbolDeclarationList(symbol);
1490
1491                         symbol->sattr |= (ABS | DEFINED | EQUATED);
1492                         symbol->svalue = eval;
1493                         tok += 2;
1494
1495                         // What this does is eat any dot suffixes attached to a symbol. If
1496                         // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1497                         // there is no dot suffix, it assumes a size of 2.
1498                         switch ((int)*tok)
1499                         {
1500                         case DOTL:
1501                                 eval += 2;
1502                         case DOTB:
1503                         case DOTW:
1504                                 tok++;
1505                         }
1506
1507                         eval += 2;
1508                 }
1509                 else if (*tok >= KW_D0 && *tok <= KW_A7)
1510                 {
1511                         if (reglist(&rlist) < 0)
1512                                 return 0;
1513
1514                         for(i=0; i<16; i++, rlist>>=1)
1515                         {
1516                                 if (rlist & 1)
1517                                         eval += 4;
1518                         }
1519                 }
1520                 else
1521                 {
1522                         switch ((int)*tok)
1523                         {
1524                         case KW_USP:
1525                         case KW_SSP:
1526                         case KW_PC:
1527                                 eval += 2;
1528                                 // FALLTHROUGH
1529                         case KW_SR:
1530                         case KW_CCR:
1531                                 eval += 2;
1532                                 tok++;
1533                                 break;
1534                         case EOL:
1535                                 return 0;
1536                         default:
1537                                 return error(".cargs syntax");
1538                         }
1539                 }
1540
1541                 // Eat commas in between each argument, if they exist
1542                 if (*tok == ',')
1543                         tok++;
1544         }
1545 }
1546
1547
1548 //
1549 // .cstruct [#offset], symbol[.size], ...
1550 //
1551 // Lists of registers may also be mentioned; they just take up space. Good for
1552 // "documentation" purposes:
1553 //
1554 // .cstruct a6, .arg1, .arg2, .arg3...
1555 //
1556 // Symbols thus created are ABS and EQUATED. Note that this is for
1557 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1558 // the suggestion.
1559 //
1560 int d_cstruct(void)
1561 {
1562         VALUE eval = 0;         // Default, if no offset specified, is zero
1563         WORD rlist;
1564         SYM * symbol;
1565         char * symbolName;
1566         int env;
1567         int i;
1568
1569         if (rgpu || rdsp)
1570                 return error("directive forbidden in gpu/dsp mode");
1571
1572         if (*tok == '#')
1573         {
1574                 tok++;
1575
1576                 if (abs_expr(&eval) != OK)
1577                         return 0;
1578
1579                 // Eat the comma, if it's there
1580                 if (*tok == ',')
1581                         tok++;
1582         }
1583
1584         for(;;)
1585         {
1586                 if (*tok == SYMBOL)
1587                 {
1588                         symbolName = string[tok[1]];
1589
1590                         // Set env to either local (dot prefixed) or global scope
1591                         env = (symbolName[0] == '.' ? curenv : 0);
1592                         symbol = lookup(symbolName, LABEL, env);
1593
1594                         // If the symbol wasn't found, then define it. Otherwise, throw an
1595                         // error.
1596                         if (symbol == NULL)
1597                         {
1598                                 symbol = NewSymbol(symbolName, LABEL, env);
1599                                 symbol->sattr = 0;
1600                         }
1601                         else if (symbol->sattr & DEFINED)
1602                                 return errors("multiply-defined label '%s'", symbolName);
1603
1604                         // Put symbol in "order of definition" list
1605                         AddToSymbolDeclarationList(symbol);
1606
1607                         tok += 2;
1608
1609                         // Adjust label start address if it's a word or a long, as a byte
1610                         // label might have left us on an odd address.
1611                         switch ((int)*tok)
1612                         {
1613                         case DOTW:
1614                         case DOTL:
1615                                 eval += eval & 0x01;
1616                         }
1617
1618                         symbol->sattr |= (ABS | DEFINED | EQUATED);
1619                         symbol->svalue = eval;
1620
1621                         // Check for dot suffixes and adjust space accordingly (longs and
1622                         // words on an odd boundary get bumped to the next word aligned
1623                         // address). If no suffix, then throw an error.
1624                         switch ((int)*tok)
1625                         {
1626                         case DOTL:
1627                                 eval += 4;
1628                                 break;
1629                         case DOTW:
1630                                 eval += 2;
1631                                 break;
1632                         case DOTB:
1633                                 eval += 1;
1634                                 break;
1635                         default:
1636                                 return error("Symbol missing dot suffix in .cstruct construct");
1637                         }
1638
1639                         tok++;
1640                 }
1641                 else if (*tok >= KW_D0 && *tok <= KW_A7)
1642                 {
1643                         if (reglist(&rlist) < 0)
1644                                 return 0;
1645
1646                         for(i=0; i<16; i++, rlist>>=1)
1647                         {
1648                                 if (rlist & 1)
1649                                         eval += 4;
1650                         }
1651                 }
1652                 else
1653                 {
1654                         switch ((int)*tok)
1655                         {
1656                         case KW_USP:
1657                         case KW_SSP:
1658                         case KW_PC:
1659                                 eval += 2;
1660                                 // FALLTHROUGH
1661                         case KW_SR:
1662                         case KW_CCR:
1663                                 eval += 2;
1664                                 tok++;
1665                                 break;
1666                         case EOL:
1667                                 return 0;
1668                         default:
1669                                 return error(".cstruct syntax");
1670                         }
1671                 }
1672
1673                 // Eat commas in between each argument, if they exist
1674                 if (*tok == ',')
1675                         tok++;
1676         }
1677 }
1678
1679
1680 //
1681 // Undefine a macro - .undefmac macname [, macname...]
1682 //
1683 int undmac1(char * p)
1684 {
1685         SYM * symbol = lookup(p, MACRO, 0);
1686
1687         // If the macro symbol exists, cause it to disappear
1688         if (symbol != NULL)
1689                 symbol->stype = (BYTE)SY_UNDEF;
1690
1691         return OK;
1692 }
1693
1694
1695 int d_undmac(void)
1696 {
1697         symlist(undmac1);
1698         return 0;
1699 }
1700
1701
1702 int d_jpad(void)
1703 {
1704         warn("JPAD directive is deprecated/non-functional");
1705         return OK;
1706 }
1707
1708
1709 int d_nojpad(void)
1710 {
1711         warn("NOJPAD directive is deprecated/non-functional");
1712         return OK;
1713 }
1714
1715
1716 int d_gpumain(void)
1717 {
1718         return error("What the hell? Do you think we adhere to the Goof standard?");
1719 }
1720
1721
1722 //
1723 // .opt - turn a specific (or all) optimisation on or off
1724 //
1725 int d_opt(void)
1726 {
1727         while (*tok != EOL)
1728         {
1729                 if (*tok == STRING)
1730                 {
1731                         tok++;
1732                         char * tmpstr = string[*tok++];
1733
1734                         if (ParseOptimization(tmpstr) != OK)
1735                         {
1736                                 char temperr[256];
1737                                 sprintf(temperr, "unknown optimization flag '%s'", tmpstr);
1738                                 return error(temperr);
1739                         }
1740                 }
1741                 else
1742                         return error(".opt directive needs every switch enclosed inside quotation marks");
1743         }
1744
1745         return OK;
1746 }
1747