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