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