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