]> Shamusworld >> Repos - rmac/blob - direct.c
Remove hacky code that truncates absolute addresses. (Bug 38)
[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-2018 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 "fltpoint.h"
15 #include "listing.h"
16 #include "mach.h"
17 #include "macro.h"
18 #include "mark.h"
19 #include "procln.h"
20 #include "riscasm.h"
21 #include "sect.h"
22 #include "symbol.h"
23 #include "token.h"
24
25 #define DEF_KW
26 #include "kwtab.h"
27
28
29 TOKEN exprbuf[128];                     // Expression buffer
30 SYM * symbolPtr[1000000];       // Symbol pointers table
31 static long unused;                     // For supressing 'write' warnings
32 char buffer[256];                       // Scratch buffer for messages
33 int largestAlign[3] = { 2, 2, 2 };      // Largest alignment value seen per section
34
35 // Function prototypes
36 int d_unimpl(void);
37 int d_68000(void);
38 int d_68020(void);
39 int d_68030(void);
40 int d_68040(void);
41 int d_68060(void);
42 int d_68881(void);
43 int d_68882(void);
44 int d_56001(void);
45 int d_nofpu(void);
46 int d_bss(void);
47 int d_data(void);
48 int d_text(void);
49 int d_abs(void);
50 int d_comm(void);
51 int d_dc(WORD);
52 int d_ds(WORD);
53 int d_dcb(WORD);
54 int d_globl(void);
55 int d_gpu(void);
56 int d_dsp(void);
57 int d_assert(void);
58 int d_include(void);
59 int d_list(void);
60 int d_nlist(void);
61 int d_error(char *);
62 int d_warn(char *);
63 int d_org(void);
64 int d_init(WORD);
65 int d_cargs(void);
66 int d_undmac(void);
67 int d_regbank0(void);
68 int d_regbank1(void);
69 int d_incbin(void);
70 int d_noclear(void);
71 int d_equrundef(void);
72 int d_ccundef(void);
73 int d_print(void);
74 int d_gpumain(void);
75 int d_jpad(void);
76 int d_nojpad(void);
77 int d_fail(void);
78 int d_cstruct(void);
79 int d_prgflags(void);
80 int d_opt(void);
81 int d_dsp(void);
82 int d_objproc(void);
83 void SetLargestAlignment(int);
84
85 // Directive handler table
86 int (*dirtab[])() = {
87         d_org,                          // 0 org
88         d_even,                         // 1 even
89         d_6502,                         // 2 .6502
90         d_68000,                        // 3 .68000
91         d_bss,                          // 4 bss
92         d_data,                         // 5 data
93         d_text,                         // 6 text
94         d_abs,                          // 7 abs
95         d_comm,                         // 8 comm
96         (void *)d_init,         // 9 init
97         d_cargs,                        // 10 cargs
98         (void *)d_goto,         // 11 goto
99         (void *)d_dc,           // 12 dc
100         (void *)d_ds,           // 13 ds
101         d_undmac,                       // 14 undefmac
102         d_gpu,                          // 15 .gpu
103         d_dsp,                          // 16 .dsp
104         (void *)d_dcb,          // 17 dcb
105         d_unimpl,                       // 18* set
106         d_unimpl,                       // 19* reg
107         d_unimpl,                       // 20 dump
108         d_incbin,                       // 21 .incbin //load
109         d_unimpl,                       // 22 disable
110         d_unimpl,                       // 23 enable
111         d_globl,                        // 24 globl
112         d_regbank0,                     // 25 .regbank0
113         d_regbank1,                     // 26 .regbank1
114         d_unimpl,                       // 27 xdef
115         d_assert,                       // 28 assert
116         d_unimpl,                       // 29* if
117         d_unimpl,                       // 30* endif
118         d_unimpl,                       // 31* endc
119         d_unimpl,                       // 32* iif
120         d_include,                      // 33 include
121         fpop,                           // 34 end
122         d_unimpl,                       // 35* macro
123         ExitMacro,                      // 36* exitm
124         d_unimpl,                       // 37* endm
125         d_list,                         // 38 list
126         d_nlist,                        // 39 nlist
127         d_long,                         // 40* rept
128         d_phrase,                       // 41* endr
129         d_dphrase,                      // 42 struct
130         d_qphrase,                      // 43 ends
131         d_title,                        // 44 title
132         d_subttl,                       // 45 subttl
133         eject,                          // 46 eject
134         d_error,                        // 47 error
135         d_warn,                         // 48 warn
136         d_noclear,                      // 49 .noclear
137         d_equrundef,            // 50 .equrundef/.regundef
138         d_ccundef,                      // 51 .ccundef
139         d_print,                        // 52 .print
140         d_cstruct,                      // 53 .cstruct
141         d_jpad,                         // 54 .jpad (deprecated)
142         d_nojpad,                       // 55 .nojpad (deprecated)
143         d_gpumain,                      // 56 .gpumain (deprecated)
144         d_prgflags,                     // 57 .prgflags
145         d_68020,                        // 58 .68020
146         d_68030,                        // 59 .68030
147         d_68040,                        // 60 .68040
148         d_68060,                        // 61 .68060
149         d_68881,                        // 62 .68881
150         d_68882,                        // 63 .68882
151         d_56001,                        // 64 .56001
152         d_nofpu,                        // 65 nofpu
153         d_opt,                          // 66 .opt
154         d_objproc,                      // 67 .objproc
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 && !robjproc && !m6502)
222                 return error(".org permitted only in GPU/DSP/OP 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 | robjproc)
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                 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
1139                         break;
1140                 case SIZQ:
1141                         // 64-bit size
1142                         if (m6502)
1143                                 return error(in_6502mode);
1144
1145                         // Shamus: We only handle DC.Q type stuff, will have to add fixups
1146                         //         and stuff later (maybe... might not be needed...)
1147                         // DEFINITELY NEED FIXUPS HERE!
1148                         if (!defined)
1149                         {
1150                                 AddFixup(FU_QUAD, sloc, exprbuf);
1151                                 D_quad(0LL);
1152                         }
1153                         else
1154                         {
1155                                 D_quad(eval);
1156                         }
1157
1158                         break;
1159                 case SIZS:
1160                         // 32-bit float size
1161                         if (m6502)
1162                                 return error(in_6502mode);
1163
1164                         if (!defined)
1165                         {
1166                                 AddFixup(FU_FLOATSING, sloc, exprbuf);
1167                                 D_long(0);
1168                         }
1169                         else
1170                         {
1171 //Would this *ever* happen?
1172 //                              if (tdb)
1173 //                                      MarkRelocatable(cursect, sloc, tdb, MSINGLE, NULL);
1174
1175                                 PTR ptr;
1176                                 ptr.u64 = &eval;
1177                                 uint32_t ieee754 = FloatToIEEE754((float)*ptr.dp);
1178                                 D_long(ieee754);
1179                         }
1180
1181                         break;
1182                 case SIZD:
1183                         // 64-bit double size
1184                         if (m6502)
1185                                 return error(in_6502mode);
1186
1187                         if (!defined)
1188                         {
1189                                 AddFixup(FU_FLOATDOUB, sloc, exprbuf);
1190                                 D_quad(0LL);
1191                         }
1192                         else
1193                         {
1194 //Would this *ever* happen?
1195 //                              if (tdb)
1196 //                                      MarkRelocatable(cursect, sloc, tdb, MDOUBLE, NULL);
1197
1198                                 PTR ptr;
1199                                 ptr.u64 = &eval;
1200                                 uint64_t ieee754 = DoubleToIEEE754(*ptr.dp);
1201                                 D_quad(ieee754);
1202                         }
1203
1204                         break;
1205                 case SIZX:
1206                         if (m6502)
1207                                 return error(in_6502mode);
1208
1209                         uint8_t extDbl[12];
1210                         memset(extDbl, 0, 12);
1211
1212                         if (!defined)
1213                         {
1214                                 AddFixup(FU_FLOATEXT, sloc, exprbuf);
1215                                 D_extend(extDbl);
1216                         }
1217                         else
1218                         {
1219 //Would this *ever* happen?
1220 //                              if (tdb)
1221 //                                      MarkRelocatable(cursect, sloc, tdb, MEXTEND, NULL);
1222
1223                                 PTR ptr;
1224                                 ptr.u64 = &eval;
1225                                 DoubleToExtended(*ptr.dp, extDbl);
1226                                 D_extend(extDbl);
1227                         }
1228
1229                         break;
1230                 }
1231
1232 comma:
1233                 if (*tok != ',')
1234                         break;
1235         }
1236
1237         at_eol();
1238         return 0;
1239 }
1240
1241
1242 //
1243 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1244 //
1245 int d_dcb(WORD siz)
1246 {
1247         uint64_t evalc, eval;
1248         WORD eattr;
1249
1250         DEBUG { printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr); }
1251
1252         if ((scattr & SBSS) != 0)
1253                 return error("illegal initialization of section");
1254
1255         if (abs_expr(&evalc) != OK)
1256                 return 0;
1257
1258         if (*tok++ != ',')
1259                 return error("missing comma");
1260
1261         if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1262                 return 0;
1263
1264         if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1265                 auto_even();
1266
1267         dep_block((uint32_t)evalc, siz, (uint32_t)eval, eattr, exprbuf);
1268         return 0;
1269 }
1270
1271
1272 //
1273 // Generalized initialization directive
1274 //
1275 // .init[.siz] [#count,] expression [.size] , ...
1276 //
1277 // The size suffix on the ".init" directive becomes the default size of the
1278 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1279 // and an expression, it specifies a repeat count. The value to be deposited
1280 // may be followed by a size suffix, which overrides the default size.
1281 //
1282 int d_init(WORD def_siz)
1283 {
1284         uint64_t count;
1285         uint64_t eval;
1286         WORD eattr;
1287         WORD siz;
1288
1289         if ((scattr & SBSS) != 0)
1290                 return error(".init not permitted in BSS or ABS");
1291
1292         if (rgpu || rdsp)
1293                 return error("directive forbidden in gpu/dsp mode");
1294
1295         for(;;)
1296         {
1297                 // Get repeat count (defaults to 1)
1298                 if (*tok == '#')
1299                 {
1300                         tok++;
1301
1302                         if (abs_expr(&count) != OK)
1303                                 return 0;
1304
1305                         if (*tok++ != ',')
1306                                 return error(comma_error);
1307                 }
1308                 else
1309                         count = 1;
1310
1311                 // Evaluate expression to deposit
1312                 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1313                         return 0;
1314
1315                 switch (*tok++)
1316                 {                                 // Determine size of object to deposit
1317                 case DOTB: siz = SIZB; break;
1318                 case DOTW: siz = SIZB; break;
1319                 case DOTL: siz = SIZL; break;
1320                 default:
1321                         siz = def_siz;
1322                         tok--;
1323                         break;
1324                 }
1325
1326                 dep_block((uint32_t)count, siz, (uint32_t)eval, eattr, exprbuf);
1327
1328                 switch (*tok)
1329                 {
1330                 case EOL:
1331                         return 0;
1332                 case ',':
1333                         tok++;
1334                         continue;
1335                 default:
1336                         return error(comma_error);
1337                 }
1338         }
1339 }
1340
1341
1342 //
1343 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1344 //
1345 int dep_block(uint32_t count, WORD siz, uint32_t eval, WORD eattr, TOKEN * exprbuf)
1346 {
1347         WORD tdb;
1348         WORD defined;
1349
1350         tdb = (WORD)(eattr & TDB);
1351         defined = (WORD)(eattr & DEFINED);
1352
1353         while (count--)
1354         {
1355                 if ((challoc - ch_size) < 4)
1356                         chcheck(4L);
1357
1358                 switch(siz)
1359                 {
1360                 case SIZB:
1361                         if (!defined)
1362                         {
1363                                 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1364                                 D_byte(0);
1365                         }
1366                         else
1367                         {
1368                                 if (tdb)
1369                                         return error("non-absolute byte value");
1370
1371                                 if (eval + 0x100 >= 0x200)
1372                                         return error(range_error);
1373
1374                                 D_byte(eval);
1375                         }
1376
1377                         break;
1378                 case SIZW:
1379                 case SIZN:
1380                         if (!defined)
1381                         {
1382                                 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1383                                 D_word(0);
1384                         }
1385                         else
1386                         {
1387                                 if (tdb)
1388                                         MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1389
1390                                 if (eval + 0x10000 >= 0x20000)
1391                                         return error(range_error);
1392
1393                                 // Deposit 68000 or 6502 (byte-reversed) word
1394                                 if (cursect != M6502)
1395                                         D_word(eval)
1396                                 else
1397                                         D_rword(eval)
1398
1399                         }
1400
1401                         break;
1402                 case SIZL:
1403                         if (m6502)
1404                                 return error(in_6502mode);
1405
1406                         if (!defined)
1407                         {
1408                                 AddFixup(FU_LONG, sloc, exprbuf);
1409                                 D_long(0);
1410                         }
1411                         else
1412                         {
1413                                 if (tdb)
1414                                         MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1415
1416                                 D_long(eval);
1417                         }
1418
1419                         break;
1420                 }
1421         }
1422
1423         return 0;
1424 }
1425
1426
1427 //
1428 // .comm symbol, size
1429 //
1430 int d_comm(void)
1431 {
1432         SYM * sym;
1433         char * p;
1434         uint64_t eval;
1435
1436         if (m6502)
1437                 return error(in_6502mode);
1438
1439         if (*tok != SYMBOL)
1440                 return error("missing symbol");
1441
1442         p = string[tok[1]];
1443         tok += 2;
1444
1445         if (*p == '.')                                          // Cannot .comm a local symbol
1446                 return error(locgl_error);
1447
1448         if ((sym = lookup(p, LABEL, 0)) == NULL)
1449                 sym = NewSymbol(p, LABEL, 0);
1450         else
1451         {
1452                 if (sym->sattr & DEFINED)
1453                         return error(".comm symbol already defined");
1454         }
1455
1456         sym->sattr = GLOBAL | COMMON | BSS;
1457
1458         if (*tok++ != ',')
1459                 return error(comma_error);
1460
1461         if (abs_expr(&eval) != OK)                      // Parse size of common region
1462                 return 0;
1463
1464         sym->svalue = eval;                                     // Install common symbol's size
1465         at_eol();
1466         return 0;
1467 }
1468
1469
1470 //
1471 // .list - Turn listing on
1472 //
1473 int d_list(void)
1474 {
1475         if (list_flag)
1476                 listing++;
1477
1478         return 0;
1479 }
1480
1481
1482 //
1483 // .nlist - Turn listing off
1484 //
1485 int d_nlist(void)
1486 {
1487         if (list_flag)
1488                 listing--;
1489
1490         return 0;
1491 }
1492
1493
1494 //
1495 // .68000 - Back to 68000 TEXT segment
1496 //
1497 int d_68000(void)
1498 {
1499         rgpu = rdsp = robjproc = 0;
1500         // Switching from gpu/dsp sections should reset any ORG'd Address
1501         orgactive = 0;
1502         orgwarning = 0;
1503         SaveSection();
1504         SwitchSection(TEXT);
1505         activecpu = CPU_68000;
1506         return 0;
1507 }
1508
1509
1510 //
1511 // .68020 - Back to 68000 TEXT segment and select 68020
1512 //
1513 int d_68020(void)
1514 {
1515         d_68000();
1516         activecpu = CPU_68020;
1517         return 0;
1518 }
1519
1520
1521 //
1522 // .68030 - Back to 68000 TEXT segment and select 68030
1523 //
1524 int d_68030(void)
1525 {
1526         d_68000();
1527         activecpu = CPU_68030;
1528         return 0;
1529 }
1530
1531
1532 //
1533 // .68040 - Back to 68000 TEXT segment and select 68040
1534 //
1535 int d_68040(void)
1536 {
1537         d_68000();
1538         activecpu = CPU_68040;
1539         activefpu = FPU_68040;
1540         return 0;
1541 }
1542
1543
1544 //
1545 // .68060 - Back to 68000 TEXT segment and select 68060
1546 //
1547 int d_68060(void)
1548 {
1549         d_68000();
1550         activecpu = CPU_68060;
1551         activefpu = FPU_68060;
1552         return 0;
1553 }
1554
1555
1556 //
1557 // .68881 - Back to 680x0 TEXT segment and select 68881 FPU
1558 //
1559 int d_68881(void)
1560 {
1561         //d_68000();
1562         activefpu = FPU_68881;
1563         return 0;
1564 }
1565
1566
1567 //
1568 // .68882 - Back to 680x0 TEXT segment and select 68882 FPU
1569 //
1570 int d_68882(void)
1571 {
1572         //d_68000();
1573         activefpu = FPU_68882;
1574         return 0;
1575 }
1576
1577
1578 //
1579 // nofpu - Deselect FPUs.
1580 //
1581 int d_nofpu(void)
1582 {
1583         activefpu = FPU_NONE;
1584         return 0;
1585 }
1586
1587
1588 //
1589 // DSP56001
1590 //
1591 int d_56001(void)
1592 {
1593         return error("Not yet, child. Be patient.");
1594 }
1595
1596
1597 //
1598 // .gpu - Switch to GPU assembler
1599 //
1600 int d_gpu(void)
1601 {
1602         if ((cursect != TEXT) && (cursect != DATA))
1603         {
1604                 error(".gpu can only be used in the TEXT or DATA segments");
1605                 return ERROR;
1606         }
1607
1608         // If previous section was DSP or 68000 then we need to reset ORG'd Addresses
1609         if (!rgpu)
1610         {
1611                 orgactive = 0;
1612                 orgwarning = 0;
1613         }
1614
1615         rgpu = 1;                       // Set GPU assembly
1616         rdsp = 0;                       // Unset DSP assembly
1617         robjproc = 0;           // Unset OP assembly
1618         regbank = BANK_N;       // Set no default register bank
1619         return 0;
1620 }
1621
1622
1623 //
1624 // .dsp - Switch to DSP assembler
1625 //
1626 int d_dsp(void)
1627 {
1628         if ((cursect != TEXT) && (cursect != DATA))
1629         {
1630                 error(".dsp can only be used in the TEXT or DATA segments");
1631                 return ERROR;
1632         }
1633
1634         // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1635         if (!rdsp)
1636         {
1637                 orgactive = 0;
1638                 orgwarning = 0;
1639         }
1640
1641         rdsp = 1;                       // Set DSP assembly
1642         rgpu = 0;                       // Unset GPU assembly
1643         robjproc = 0;           // Unset OP assembly
1644         regbank = BANK_N;       // Set no default register bank
1645         return 0;
1646 }
1647
1648
1649 //
1650 // .cargs [#offset], symbol[.size], ...
1651 //
1652 // Lists of registers may also be mentioned; they just take up space. Good for
1653 // "documentation" purposes:
1654 //
1655 // .cargs a6, .arg1, .arg2, .arg3...
1656 //
1657 // Symbols thus created are ABS and EQUATED.
1658 //
1659 int d_cargs(void)
1660 {
1661         uint64_t eval = 4;      // Default to 4 if no offset specified (to account for
1662                                                 // return address)
1663         WORD rlist;
1664         SYM * symbol;
1665         char * p;
1666         int env;
1667         int i;
1668
1669         if (rgpu || rdsp)
1670                 return error("directive forbidden in gpu/dsp mode");
1671
1672         if (*tok == '#')
1673         {
1674                 tok++;
1675
1676                 if (abs_expr(&eval) != OK)
1677                         return 0;
1678
1679                 // Eat the comma, if it's there
1680                 if (*tok == ',')
1681                         tok++;
1682         }
1683
1684         for(;;)
1685         {
1686                 if (*tok == SYMBOL)
1687                 {
1688                         p = string[tok[1]];
1689
1690                         // Set env to either local (dot prefixed) or global scope
1691                         env = (*p == '.' ? curenv : 0);
1692                         symbol = lookup(p, LABEL, env);
1693
1694                         if (symbol == NULL)
1695                         {
1696                                 symbol = NewSymbol(p, LABEL, env);
1697                                 symbol->sattr = 0;
1698                         }
1699                         else if (symbol->sattr & DEFINED)
1700                                 return error("multiply-defined label '%s'", p);
1701
1702                         // Put symbol in "order of definition" list
1703                         AddToSymbolDeclarationList(symbol);
1704
1705                         symbol->sattr |= (ABS | DEFINED | EQUATED);
1706                         symbol->svalue = eval;
1707                         tok += 2;
1708
1709                         // What this does is eat any dot suffixes attached to a symbol. If
1710                         // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1711                         // there is no dot suffix, it assumes a size of 2.
1712                         switch ((int)*tok)
1713                         {
1714                         case DOTL:
1715                                 eval += 2;
1716                         case DOTB:
1717                         case DOTW:
1718                                 tok++;
1719                         }
1720
1721                         eval += 2;
1722                 }
1723                 else if (*tok >= KW_D0 && *tok <= KW_A7)
1724                 {
1725                         if (reglist(&rlist) < 0)
1726                                 return 0;
1727
1728                         for(i=0; i<16; i++, rlist>>=1)
1729                         {
1730                                 if (rlist & 1)
1731                                         eval += 4;
1732                         }
1733                 }
1734                 else
1735                 {
1736                         switch ((int)*tok)
1737                         {
1738                         case KW_USP:
1739                         case KW_SSP:
1740                         case KW_PC:
1741                                 eval += 2;
1742                                 // FALLTHROUGH
1743                         case KW_SR:
1744                         case KW_CCR:
1745                                 eval += 2;
1746                                 tok++;
1747                                 break;
1748                         case EOL:
1749                                 return 0;
1750                         default:
1751                                 return error(".cargs syntax");
1752                         }
1753                 }
1754
1755                 // Eat commas in between each argument, if they exist
1756                 if (*tok == ',')
1757                         tok++;
1758         }
1759 }
1760
1761
1762 //
1763 // .cstruct [#offset], symbol[.size], ...
1764 //
1765 // Lists of registers may also be mentioned; they just take up space. Good for
1766 // "documentation" purposes:
1767 //
1768 // .cstruct a6, .arg1, .arg2, .arg3...
1769 //
1770 // Symbols thus created are ABS and EQUATED. Note that this is for
1771 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1772 // the suggestion.
1773 //
1774 int d_cstruct(void)
1775 {
1776         uint64_t eval = 0;      // Default, if no offset specified, is zero
1777         WORD rlist;
1778         SYM * symbol;
1779         char * symbolName;
1780         int env;
1781         int i;
1782
1783         if (rgpu || rdsp)
1784                 return error("directive forbidden in gpu/dsp mode");
1785
1786         if (*tok == '#')
1787         {
1788                 tok++;
1789
1790                 if (abs_expr(&eval) != OK)
1791                         return 0;
1792
1793                 // Eat the comma, if it's there
1794                 if (*tok == ',')
1795                         tok++;
1796         }
1797
1798         for(;;)
1799         {
1800                 if (*tok == SYMBOL)
1801                 {
1802                         symbolName = string[tok[1]];
1803
1804                         // Set env to either local (dot prefixed) or global scope
1805                         env = (symbolName[0] == '.' ? curenv : 0);
1806                         symbol = lookup(symbolName, LABEL, env);
1807
1808                         // If the symbol wasn't found, then define it. Otherwise, throw an
1809                         // error.
1810                         if (symbol == NULL)
1811                         {
1812                                 symbol = NewSymbol(symbolName, LABEL, env);
1813                                 symbol->sattr = 0;
1814                         }
1815                         else if (symbol->sattr & DEFINED)
1816                                 return error("multiply-defined label '%s'", symbolName);
1817
1818                         // Put symbol in "order of definition" list
1819                         AddToSymbolDeclarationList(symbol);
1820
1821                         tok += 2;
1822
1823                         // Adjust label start address if it's a word or a long, as a byte
1824                         // label might have left us on an odd address.
1825                         switch ((int)*tok)
1826                         {
1827                         case DOTW:
1828                         case DOTL:
1829                                 eval += eval & 0x01;
1830                         }
1831
1832                         symbol->sattr |= (ABS | DEFINED | EQUATED);
1833                         symbol->svalue = eval;
1834
1835                         // Check for dot suffixes and adjust space accordingly (longs and
1836                         // words on an odd boundary get bumped to the next word aligned
1837                         // address). If no suffix, then throw an error.
1838                         switch ((int)*tok)
1839                         {
1840                         case DOTL:
1841                                 eval += 4;
1842                                 break;
1843                         case DOTW:
1844                                 eval += 2;
1845                                 break;
1846                         case DOTB:
1847                                 eval += 1;
1848                                 break;
1849                         default:
1850                                 return error("Symbol missing dot suffix in .cstruct construct");
1851                         }
1852
1853                         tok++;
1854                 }
1855                 else if (*tok >= KW_D0 && *tok <= KW_A7)
1856                 {
1857                         if (reglist(&rlist) < 0)
1858                                 return 0;
1859
1860                         for(i=0; i<16; i++, rlist>>=1)
1861                         {
1862                                 if (rlist & 1)
1863                                         eval += 4;
1864                         }
1865                 }
1866                 else
1867                 {
1868                         switch ((int)*tok)
1869                         {
1870                         case KW_USP:
1871                         case KW_SSP:
1872                         case KW_PC:
1873                                 eval += 2;
1874                                 // FALLTHROUGH
1875                         case KW_SR:
1876                         case KW_CCR:
1877                                 eval += 2;
1878                                 tok++;
1879                                 break;
1880                         case EOL:
1881                                 return 0;
1882                         default:
1883                                 return error(".cstruct syntax");
1884                         }
1885                 }
1886
1887                 // Eat commas in between each argument, if they exist
1888                 if (*tok == ',')
1889                         tok++;
1890         }
1891 }
1892
1893
1894 //
1895 // Define start of OP object list (allows the use of ORG)
1896 //
1897 int d_objproc(void)
1898 {
1899         if ((cursect != TEXT) && (cursect != DATA))
1900         {
1901                 error(".objproc can only be used in the TEXT or DATA segments");
1902                 return ERROR;
1903         }
1904
1905         // If previous section was DSP or 68000 then we need to reset ORG'd
1906         // Addresses
1907         if (!robjproc)
1908         {
1909                 orgactive = 0;
1910                 orgwarning = 0;
1911         }
1912
1913         robjproc = 1;           // Set OP assembly
1914         rgpu = 0;                       // Unset GPU assembly
1915         rdsp = 0;                       // Unset DSP assembly
1916         return OK;
1917 }
1918
1919
1920 //
1921 // Undefine a macro - .undefmac macname [, macname...]
1922 //
1923 int undmac1(char * p)
1924 {
1925         SYM * symbol = lookup(p, MACRO, 0);
1926
1927         // If the macro symbol exists, cause it to disappear
1928         if (symbol != NULL)
1929                 symbol->stype = (BYTE)SY_UNDEF;
1930
1931         return OK;
1932 }
1933
1934
1935 int d_undmac(void)
1936 {
1937         symlist(undmac1);
1938         return 0;
1939 }
1940
1941
1942 int d_jpad(void)
1943 {
1944         warn("JPAD directive is deprecated/non-functional");
1945         return OK;
1946 }
1947
1948
1949 int d_nojpad(void)
1950 {
1951         warn("NOJPAD directive is deprecated/non-functional");
1952         return OK;
1953 }
1954
1955
1956 int d_gpumain(void)
1957 {
1958         return error("What the hell? Do you think we adhere to the Goof standard?");
1959 }
1960
1961
1962 //
1963 // .opt - turn a specific (or all) optimisation on or off
1964 //
1965 int d_opt(void)
1966 {
1967         while (*tok != EOL)
1968         {
1969                 if (*tok == STRING)
1970                 {
1971                         tok++;
1972                         char * tmpstr = string[*tok++];
1973
1974                         if (ParseOptimization(tmpstr) != OK)
1975                                 return error("unknown optimization flag '%s'", tmpstr);
1976                 }
1977                 else
1978                         return error(".opt directive needs every switch enclosed inside quotation marks");
1979         }
1980
1981         return OK;
1982 }
1983
1984
1985 //
1986 // .if, Start conditional assembly
1987 //
1988 int d_if(void)
1989 {
1990         WORD eattr;
1991         uint64_t eval;
1992         SYM * esym;
1993         IFENT * rif = f_ifent;
1994
1995         // Alloc an IFENTRY
1996         if (rif == NULL)
1997                 rif = (IFENT *)malloc(sizeof(IFENT));
1998         else
1999                 f_ifent = rif->if_prev;
2000
2001         rif->if_prev = ifent;
2002         ifent = rif;
2003
2004         if (!disabled)
2005         {
2006                 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
2007                         return 0;
2008
2009                 if ((eattr & DEFINED) == 0)
2010                         return error(undef_error);
2011
2012                 disabled = !eval;
2013         }
2014
2015         rif->if_state = (WORD)disabled;
2016         return 0;
2017 }
2018
2019
2020 //
2021 // .else, Do alternate case for .if
2022 //
2023 int d_else(void)
2024 {
2025         IFENT * rif = ifent;
2026
2027         if (rif->if_prev == NULL)
2028                 return error("mismatched .else");
2029
2030         if (disabled)
2031                 disabled = rif->if_prev->if_state;
2032         else
2033                 disabled = 1;
2034
2035         rif->if_state = (WORD)disabled;
2036         return 0;
2037 }
2038
2039
2040 //
2041 // .endif, End of conditional assembly block
2042 // This is also called by fpop() to pop levels of IFENTs in case a macro or
2043 // include file exits early with `exitm' or `end'.
2044 //
2045 int d_endif(void)
2046 {
2047         IFENT * rif = ifent;
2048
2049         if (rif->if_prev == NULL)
2050                 return error("mismatched .endif");
2051
2052         ifent = rif->if_prev;
2053         disabled = rif->if_prev->if_state;
2054         rif->if_prev = f_ifent;
2055         f_ifent = rif;
2056         return 0;
2057 }
2058