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