]> Shamusworld >> Repos - rmac/blob - direct.c
900c629a3b686d392b207cf4e36bad04adab8991
[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
1008                         tok += 2;
1009                         goto comma;
1010                 }
1011
1012                 int movei = 0; // MOVEI flag for dc.i
1013
1014                 if (*tok == DOTI)
1015                 {
1016                         movei = 1;
1017                         tok++;
1018                         siz = SIZL;
1019                 }
1020
1021                 // dc.x <expression>
1022                 SYM * esym = 0;
1023
1024                 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
1025                         return 0;
1026
1027                 uint16_t tdb = eattr & TDB;
1028                 uint16_t defined = eattr & DEFINED;
1029
1030                 if ((challoc - ch_size) < 4)
1031                         chcheck(4);
1032
1033                 switch (siz)
1034                 {
1035                 case SIZB:
1036                         if (!defined)
1037                         {
1038                                 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1039                                 D_byte(0);
1040                         }
1041                         else
1042                         {
1043                                 if (eval + 0x100 >= 0x200)
1044                                 {
1045                                         sprintf(buffer, "%s (value = $%X)", range_error, eval);
1046                                         return error(buffer);
1047                                 }
1048
1049                                 if (tdb)
1050                                         return error("non-absolute byte value");
1051
1052                                 D_byte(eval);
1053                         }
1054
1055                         break;
1056                 case SIZW:
1057                 case SIZN:
1058                         if (!defined)
1059                         {
1060                                 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1061                                 D_word(0);
1062                         }
1063                         else
1064                         {
1065                                 if (eval + 0x10000 >= 0x20000)
1066                                         return error(range_error);
1067
1068                                 if (tdb)
1069                                         MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1070
1071                                 // Deposit 68000 or 6502 (byte-reversed) word
1072                                 if (cursect != M6502)
1073                                         D_word(eval)
1074                                 else
1075                                         D_rword(eval)
1076                         }
1077
1078                         break;
1079                 case SIZL:
1080                         if (m6502)
1081                                 return error(in_6502mode);
1082
1083                         if (!defined)
1084                         {
1085                                 if (movei)
1086                                         AddFixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
1087                                 else
1088                                         AddFixup(FU_LONG, sloc, exprbuf);
1089
1090                                 D_long(0);
1091                         }
1092                         else
1093                         {
1094                                 if (tdb)
1095                                         MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1096
1097                                 if (movei)
1098                                         eval = WORDSWAP32(eval);
1099
1100                                 D_long(eval);
1101                         }
1102                         break;
1103                 }
1104
1105 comma:
1106                 if (*tok != ',')
1107                         break;
1108         }
1109
1110         at_eol();
1111         return 0;
1112 }
1113
1114
1115 //
1116 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1117 //
1118 int d_dcb(WORD siz)
1119 {
1120         VALUE evalc, eval;
1121         WORD eattr;
1122
1123         DEBUG { printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr); }
1124
1125         if ((scattr & SBSS) != 0)
1126                 return error("illegal initialization of section");
1127
1128         if (abs_expr(&evalc) != OK)
1129                 return 0;
1130
1131         if (*tok++ != ',')
1132                 return error("missing comma");
1133
1134         if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1135                 return 0;
1136
1137         if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1138                 auto_even();
1139
1140         dep_block(evalc, siz, eval, eattr, exprbuf);
1141         return 0;
1142 }
1143
1144
1145 //
1146 // Generalized initialization directive
1147 //
1148 // .init[.siz] [#count,] expression [.size] , ...
1149 //
1150 // The size suffix on the ".init" directive becomes the default size of the
1151 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1152 // and an expression, it specifies a repeat count. The value to be deposited
1153 // may be followed by a size suffix, which overrides the default size.
1154 //
1155 int d_init(WORD def_siz)
1156 {
1157         VALUE count;
1158         VALUE eval;
1159         WORD eattr;
1160         WORD siz;
1161
1162         if ((scattr & SBSS) != 0)
1163                 return error(".init not permitted in BSS or ABS");
1164
1165         if (rgpu || rdsp)
1166                 return error("directive forbidden in gpu/dsp mode");
1167
1168         for(;;)
1169         {
1170                 // Get repeat count (defaults to 1)
1171                 if (*tok == '#')
1172                 {
1173                         ++tok;
1174
1175                         if (abs_expr(&count) != OK)
1176                                 return 0;
1177
1178                         if (*tok++ != ',')
1179                                 return error(comma_error);
1180                 }
1181                 else
1182                         count = 1;
1183
1184                 // Evaluate expression to deposit
1185                 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1186                         return 0;
1187
1188                 switch ((int)*tok++)
1189                 {                                 // Determine size of object to deposit
1190                 case DOTB: siz = SIZB; break;
1191                 case DOTW: siz = SIZB; break;
1192                 case DOTL: siz = SIZL; break;
1193                 default:
1194                         siz = def_siz;
1195                         tok--;
1196                         break;
1197                 }
1198
1199                 dep_block(count, siz, eval, eattr, exprbuf);
1200
1201                 switch ((int)*tok)
1202                 {
1203                 case EOL:
1204                         return 0;
1205                 case ',':
1206                         tok++;
1207                         continue;
1208                 default:
1209                         return error(comma_error);
1210                 }
1211         }
1212 }
1213
1214
1215 //
1216 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1217 //
1218 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1219 {
1220         WORD tdb;
1221         WORD defined;
1222
1223         tdb = (WORD)(eattr & TDB);
1224         defined = (WORD)(eattr & DEFINED);
1225
1226         while (count--)
1227         {
1228                 if ((challoc - ch_size) < 4)
1229                         chcheck(4L);
1230
1231                 switch(siz)
1232                 {
1233                 case SIZB:
1234                         if (!defined)
1235                         {
1236                                 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1237                                 D_byte(0);
1238                         }
1239                         else
1240                         {
1241                                 if (tdb)
1242                                         return error("non-absolute byte value");
1243
1244                                 if (eval + 0x100 >= 0x200)
1245                                         return error(range_error);
1246
1247                                 D_byte(eval);
1248                         }
1249
1250                         break;
1251                 case SIZW:
1252                 case SIZN:
1253                         if (!defined)
1254                         {
1255                                 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1256                                 D_word(0);
1257                         }
1258                         else
1259                         {
1260                                 if (tdb)
1261                                         MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1262
1263                                 if (eval + 0x10000 >= 0x20000)
1264                                         return error(range_error);
1265
1266                                 // Deposit 68000 or 6502 (byte-reversed) word
1267                                 if (cursect != M6502)
1268                                         D_word(eval)
1269                                 else
1270                                         D_rword(eval)
1271
1272                         }
1273
1274                         break;
1275                 case SIZL:
1276                         if (m6502)
1277                                 return error(in_6502mode);
1278
1279                         if (!defined)
1280                         {
1281                                 AddFixup(FU_LONG, sloc, exprbuf);
1282                                 D_long(0);
1283                         }
1284                         else
1285                         {
1286                                 if (tdb)
1287                                         MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1288
1289                                 D_long(eval);
1290                         }
1291
1292                         break;
1293                 }
1294         }
1295
1296         return 0;
1297 }
1298
1299
1300 //
1301 // .comm symbol, size
1302 //
1303 int d_comm(void)
1304 {
1305         SYM * sym;
1306         char * p;
1307         VALUE eval;
1308
1309         if (m6502)
1310                 return error(in_6502mode);
1311
1312         if (*tok != SYMBOL)
1313                 return error("missing symbol");
1314
1315         p = string[tok[1]];
1316         tok += 2;
1317
1318         if (*p == '.')                                                  // Cannot .comm a local symbol
1319                 return error(locgl_error);
1320
1321         if ((sym = lookup(p, LABEL, 0)) == NULL)
1322                 sym = NewSymbol(p, LABEL, 0);
1323         else
1324         {
1325                 if (sym->sattr & DEFINED)
1326                         return error(".comm symbol already defined");
1327         }
1328
1329         sym->sattr = GLOBAL | COMMON | BSS;
1330
1331         if (*tok++ != ',')
1332                 return error(comma_error);
1333
1334         if (abs_expr(&eval) != OK)                              // Parse size of common region
1335                 return 0;
1336
1337         sym->svalue = eval;                                             // Install common symbol's size
1338         at_eol();
1339         return 0;
1340 }
1341
1342
1343 //
1344 // .list - Turn listing on
1345 //
1346 int d_list(void)
1347 {
1348         if (list_flag)
1349                 listing++;
1350
1351         return 0;
1352 }
1353
1354
1355 //
1356 // .nlist - Turn listing off
1357 //
1358 int d_nlist(void)
1359 {
1360         if (list_flag)
1361                 listing--;
1362
1363         return 0;
1364 }
1365
1366
1367 //
1368 // .68000 - Back to 68000 TEXT segment
1369 //
1370 int d_68000(void)
1371 {
1372         rgpu = rdsp = 0;
1373         // Switching from gpu/dsp sections should reset any ORG'd Address
1374         orgactive = 0;
1375         orgwarning = 0;
1376         SaveSection();
1377         SwitchSection(TEXT);
1378         return 0;
1379 }
1380
1381
1382 //
1383 // .gpu - Switch to GPU assembler
1384 //
1385 int d_gpu(void)
1386 {
1387         if ((cursect != TEXT) && (cursect != DATA))
1388         {
1389                 error(".gpu can only be used in the TEXT or DATA segments");
1390                 return ERROR;
1391         }
1392
1393         // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1394         if (!rgpu)
1395         {
1396 //printf("Resetting ORG...\n");
1397                 orgactive = 0;
1398                 orgwarning = 0;
1399         }
1400 //else printf("NOT resetting ORG!\n");
1401
1402         rgpu = 1;                       // Set GPU assembly
1403         rdsp = 0;                       // Unset DSP assembly
1404         regbank = BANK_N;       // Set no default register bank
1405         return 0;
1406 }
1407
1408
1409 //
1410 // .dsp - Switch to DSP assembler
1411 //
1412 int d_dsp(void)
1413 {
1414         if ((cursect != TEXT) && (cursect != DATA))
1415         {
1416                 error(".dsp can only be used in the TEXT or DATA segments");
1417                 return ERROR;
1418         }
1419
1420         // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1421         if (!rdsp)
1422         {
1423                 orgactive = 0;
1424                 orgwarning = 0;
1425         }
1426
1427         rdsp = 1;                       // Set DSP assembly
1428         rgpu = 0;                       // Unset GPU assembly
1429         regbank = BANK_N;       // Set no default register bank
1430         return 0;
1431 }
1432
1433
1434 //
1435 // .cargs [#offset], symbol[.size], ...
1436 //
1437 // Lists of registers may also be mentioned; they just take up space. Good for
1438 // "documentation" purposes:
1439 //
1440 // .cargs a6, .arg1, .arg2, .arg3...
1441 //
1442 // Symbols thus created are ABS and EQUATED.
1443 //
1444 int d_cargs(void)
1445 {
1446         VALUE eval = 4;         // Default to 4 if no offset specified (to account for
1447                                                 // return address)
1448         WORD rlist;
1449         SYM * symbol;
1450         char * p;
1451         int env;
1452         int i;
1453
1454         if (rgpu || rdsp)
1455                 return error("directive forbidden in gpu/dsp mode");
1456
1457         if (*tok == '#')
1458         {
1459                 tok++;
1460
1461                 if (abs_expr(&eval) != OK)
1462                         return 0;
1463
1464                 // Eat the comma, if it's there
1465                 if (*tok == ',')
1466                         tok++;
1467         }
1468
1469         for(;;)
1470         {
1471                 if (*tok == SYMBOL)
1472                 {
1473                         p = string[tok[1]];
1474
1475                         // Set env to either local (dot prefixed) or global scope
1476                         env = (*p == '.' ? curenv : 0);
1477                         symbol = lookup(p, LABEL, env);
1478
1479                         if (symbol == NULL)
1480                         {
1481                                 symbol = NewSymbol(p, LABEL, env);
1482                                 symbol->sattr = 0;
1483                         }
1484                         else if (symbol->sattr & DEFINED)
1485                                 return errors("multiply-defined label '%s'", p);
1486
1487                         // Put symbol in "order of definition" list
1488                         AddToSymbolDeclarationList(symbol);
1489
1490                         symbol->sattr |= (ABS | DEFINED | EQUATED);
1491                         symbol->svalue = eval;
1492                         tok += 2;
1493
1494                         // What this does is eat any dot suffixes attached to a symbol. If
1495                         // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1496                         // there is no dot suffix, it assumes a size of 2.
1497                         switch ((int)*tok)
1498                         {
1499                         case DOTL:
1500                                 eval += 2;
1501                         case DOTB:
1502                         case DOTW:
1503                                 tok++;
1504                         }
1505
1506                         eval += 2;
1507                 }
1508                 else if (*tok >= KW_D0 && *tok <= KW_A7)
1509                 {
1510                         if (reglist(&rlist) < 0)
1511                                 return 0;
1512
1513                         for(i=0; i<16; i++, rlist>>=1)
1514                         {
1515                                 if (rlist & 1)
1516                                         eval += 4;
1517                         }
1518                 }
1519                 else
1520                 {
1521                         switch ((int)*tok)
1522                         {
1523                         case KW_USP:
1524                         case KW_SSP:
1525                         case KW_PC:
1526                                 eval += 2;
1527                                 // FALLTHROUGH
1528                         case KW_SR:
1529                         case KW_CCR:
1530                                 eval += 2;
1531                                 tok++;
1532                                 break;
1533                         case EOL:
1534                                 return 0;
1535                         default:
1536                                 return error(".cargs syntax");
1537                         }
1538                 }
1539
1540                 // Eat commas in between each argument, if they exist
1541                 if (*tok == ',')
1542                         tok++;
1543         }
1544 }
1545
1546
1547 //
1548 // .cstruct [#offset], symbol[.size], ...
1549 //
1550 // Lists of registers may also be mentioned; they just take up space. Good for
1551 // "documentation" purposes:
1552 //
1553 // .cstruct a6, .arg1, .arg2, .arg3...
1554 //
1555 // Symbols thus created are ABS and EQUATED. Note that this is for
1556 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1557 // the suggestion.
1558 //
1559 int d_cstruct(void)
1560 {
1561         VALUE eval = 0;         // Default, if no offset specified, is zero
1562         WORD rlist;
1563         SYM * symbol;
1564         char * symbolName;
1565         int env;
1566         int i;
1567
1568         if (rgpu || rdsp)
1569                 return error("directive forbidden in gpu/dsp mode");
1570
1571         if (*tok == '#')
1572         {
1573                 tok++;
1574
1575                 if (abs_expr(&eval) != OK)
1576                         return 0;
1577
1578                 // Eat the comma, if it's there
1579                 if (*tok == ',')
1580                         tok++;
1581         }
1582
1583         for(;;)
1584         {
1585                 if (*tok == SYMBOL)
1586                 {
1587                         symbolName = string[tok[1]];
1588
1589                         // Set env to either local (dot prefixed) or global scope
1590                         env = (symbolName[0] == '.' ? curenv : 0);
1591                         symbol = lookup(symbolName, LABEL, env);
1592
1593                         // If the symbol wasn't found, then define it. Otherwise, throw an
1594                         // error.
1595                         if (symbol == NULL)
1596                         {
1597                                 symbol = NewSymbol(symbolName, LABEL, env);
1598                                 symbol->sattr = 0;
1599                         }
1600                         else if (symbol->sattr & DEFINED)
1601                                 return errors("multiply-defined label '%s'", symbolName);
1602
1603                         // Put symbol in "order of definition" list
1604                         AddToSymbolDeclarationList(symbol);
1605
1606                         tok += 2;
1607
1608                         // Adjust label start address if it's a word or a long, as a byte
1609                         // label might have left us on an odd address.
1610                         switch ((int)*tok)
1611                         {
1612                         case DOTW:
1613                         case DOTL:
1614                                 eval += eval & 0x01;
1615                         }
1616
1617                         symbol->sattr |= (ABS | DEFINED | EQUATED);
1618                         symbol->svalue = eval;
1619
1620                         // Check for dot suffixes and adjust space accordingly (longs and
1621                         // words on an odd boundary get bumped to the next word aligned
1622                         // address). If no suffix, then throw an error.
1623                         switch ((int)*tok)
1624                         {
1625                         case DOTL:
1626                                 eval += 4;
1627                                 break;
1628                         case DOTW:
1629                                 eval += 2;
1630                                 break;
1631                         case DOTB:
1632                                 eval += 1;
1633                                 break;
1634                         default:
1635                                 return error("Symbol missing dot suffix in .cstruct construct");
1636                         }
1637
1638                         tok++;
1639                 }
1640                 else if (*tok >= KW_D0 && *tok <= KW_A7)
1641                 {
1642                         if (reglist(&rlist) < 0)
1643                                 return 0;
1644
1645                         for(i=0; i<16; i++, rlist>>=1)
1646                         {
1647                                 if (rlist & 1)
1648                                         eval += 4;
1649                         }
1650                 }
1651                 else
1652                 {
1653                         switch ((int)*tok)
1654                         {
1655                         case KW_USP:
1656                         case KW_SSP:
1657                         case KW_PC:
1658                                 eval += 2;
1659                                 // FALLTHROUGH
1660                         case KW_SR:
1661                         case KW_CCR:
1662                                 eval += 2;
1663                                 tok++;
1664                                 break;
1665                         case EOL:
1666                                 return 0;
1667                         default:
1668                                 return error(".cstruct syntax");
1669                         }
1670                 }
1671
1672                 // Eat commas in between each argument, if they exist
1673                 if (*tok == ',')
1674                         tok++;
1675         }
1676 }
1677
1678
1679 //
1680 // Undefine a macro - .undefmac macname [, macname...]
1681 //
1682 int undmac1(char * p)
1683 {
1684         SYM * symbol = lookup(p, MACRO, 0);
1685
1686         // If the macro symbol exists, cause it to disappear
1687         if (symbol != NULL)
1688                 symbol->stype = (BYTE)SY_UNDEF;
1689
1690         return OK;
1691 }
1692
1693
1694 int d_undmac(void)
1695 {
1696         symlist(undmac1);
1697         return 0;
1698 }
1699
1700
1701 int d_jpad(void)
1702 {
1703         warn("JPAD directive is deprecated/non-functional");
1704         return OK;
1705 }
1706
1707
1708 int d_nojpad(void)
1709 {
1710         warn("NOJPAD directive is deprecated/non-functional");
1711         return OK;
1712 }
1713
1714
1715 int d_gpumain(void)
1716 {
1717         return error("What the hell? Do you think we adhere to the Goof standard?");
1718 }
1719
1720
1721 //
1722 // .opt - turn a specific (or all) optimisation on or off
1723 //
1724 int d_opt(void)
1725 {
1726         while (*tok != EOL)
1727         {
1728                 if (*tok == STRING)
1729                 {
1730                         tok++;
1731                         char * tmpstr = string[*tok++];
1732
1733                         if (ParseOptimization(tmpstr) != OK)
1734                         {
1735                                 char temperr[256];
1736                                 sprintf(temperr, "unknown optimization flag '%s'", tmpstr);
1737                                 return error(temperr);
1738                         }
1739                 }
1740                 else
1741                         return error(".opt directive needs every switch enclosed inside quotation marks");
1742         }
1743
1744         return OK;
1745 }
1746