]> Shamusworld >> Repos - rmac/blob - direct.c
Fix for section alignment values in ELF objects.
[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         uint32_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         uint32_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(uint32_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         uint32_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         uint32_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         uint32_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         uint32_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         uint32_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                         if (m6502)
1116                                 return error(in_6502mode);
1117
1118                         if (!defined)
1119                         {
1120                                 if (movei)
1121                                         AddFixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
1122                                 else
1123                                         AddFixup(FU_LONG, sloc, exprbuf);
1124
1125                                 D_long(0);
1126                         }
1127                         else
1128                         {
1129                                 if (tdb)
1130                                         MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1131
1132                                 if (movei)
1133                                         eval = WORDSWAP32(eval);
1134
1135                                 D_long(eval);
1136                         }
1137                         break;
1138                 }
1139
1140 comma:
1141                 if (*tok != ',')
1142                         break;
1143         }
1144
1145         at_eol();
1146         return 0;
1147 }
1148
1149
1150 //
1151 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1152 //
1153 int d_dcb(WORD siz)
1154 {
1155         uint32_t evalc, eval;
1156         WORD eattr;
1157
1158         DEBUG { printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr); }
1159
1160         if ((scattr & SBSS) != 0)
1161                 return error("illegal initialization of section");
1162
1163         if (abs_expr(&evalc) != OK)
1164                 return 0;
1165
1166         if (*tok++ != ',')
1167                 return error("missing comma");
1168
1169         if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1170                 return 0;
1171
1172         if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1173                 auto_even();
1174
1175         dep_block(evalc, siz, eval, eattr, exprbuf);
1176         return 0;
1177 }
1178
1179
1180 //
1181 // Generalized initialization directive
1182 //
1183 // .init[.siz] [#count,] expression [.size] , ...
1184 //
1185 // The size suffix on the ".init" directive becomes the default size of the
1186 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1187 // and an expression, it specifies a repeat count. The value to be deposited
1188 // may be followed by a size suffix, which overrides the default size.
1189 //
1190 int d_init(WORD def_siz)
1191 {
1192         uint32_t count;
1193         uint32_t eval;
1194         WORD eattr;
1195         WORD siz;
1196
1197         if ((scattr & SBSS) != 0)
1198                 return error(".init not permitted in BSS or ABS");
1199
1200         if (rgpu || rdsp)
1201                 return error("directive forbidden in gpu/dsp mode");
1202
1203         for(;;)
1204         {
1205                 // Get repeat count (defaults to 1)
1206                 if (*tok == '#')
1207                 {
1208                         ++tok;
1209
1210                         if (abs_expr(&count) != OK)
1211                                 return 0;
1212
1213                         if (*tok++ != ',')
1214                                 return error(comma_error);
1215                 }
1216                 else
1217                         count = 1;
1218
1219                 // Evaluate expression to deposit
1220                 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1221                         return 0;
1222
1223                 switch ((int)*tok++)
1224                 {                                 // Determine size of object to deposit
1225                 case DOTB: siz = SIZB; break;
1226                 case DOTW: siz = SIZB; break;
1227                 case DOTL: siz = SIZL; break;
1228                 default:
1229                         siz = def_siz;
1230                         tok--;
1231                         break;
1232                 }
1233
1234                 dep_block(count, siz, eval, eattr, exprbuf);
1235
1236                 switch ((int)*tok)
1237                 {
1238                 case EOL:
1239                         return 0;
1240                 case ',':
1241                         tok++;
1242                         continue;
1243                 default:
1244                         return error(comma_error);
1245                 }
1246         }
1247 }
1248
1249
1250 //
1251 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1252 //
1253 int dep_block(uint32_t count, WORD siz, uint32_t eval, WORD eattr, TOKEN * exprbuf)
1254 {
1255         WORD tdb;
1256         WORD defined;
1257
1258         tdb = (WORD)(eattr & TDB);
1259         defined = (WORD)(eattr & DEFINED);
1260
1261         while (count--)
1262         {
1263                 if ((challoc - ch_size) < 4)
1264                         chcheck(4L);
1265
1266                 switch(siz)
1267                 {
1268                 case SIZB:
1269                         if (!defined)
1270                         {
1271                                 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1272                                 D_byte(0);
1273                         }
1274                         else
1275                         {
1276                                 if (tdb)
1277                                         return error("non-absolute byte value");
1278
1279                                 if (eval + 0x100 >= 0x200)
1280                                         return error(range_error);
1281
1282                                 D_byte(eval);
1283                         }
1284
1285                         break;
1286                 case SIZW:
1287                 case SIZN:
1288                         if (!defined)
1289                         {
1290                                 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1291                                 D_word(0);
1292                         }
1293                         else
1294                         {
1295                                 if (tdb)
1296                                         MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1297
1298                                 if (eval + 0x10000 >= 0x20000)
1299                                         return error(range_error);
1300
1301                                 // Deposit 68000 or 6502 (byte-reversed) word
1302                                 if (cursect != M6502)
1303                                         D_word(eval)
1304                                 else
1305                                         D_rword(eval)
1306
1307                         }
1308
1309                         break;
1310                 case SIZL:
1311                         if (m6502)
1312                                 return error(in_6502mode);
1313
1314                         if (!defined)
1315                         {
1316                                 AddFixup(FU_LONG, sloc, exprbuf);
1317                                 D_long(0);
1318                         }
1319                         else
1320                         {
1321                                 if (tdb)
1322                                         MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1323
1324                                 D_long(eval);
1325                         }
1326
1327                         break;
1328                 }
1329         }
1330
1331         return 0;
1332 }
1333
1334
1335 //
1336 // .comm symbol, size
1337 //
1338 int d_comm(void)
1339 {
1340         SYM * sym;
1341         char * p;
1342         uint32_t eval;
1343
1344         if (m6502)
1345                 return error(in_6502mode);
1346
1347         if (*tok != SYMBOL)
1348                 return error("missing symbol");
1349
1350         p = string[tok[1]];
1351         tok += 2;
1352
1353         if (*p == '.')                                                  // Cannot .comm a local symbol
1354                 return error(locgl_error);
1355
1356         if ((sym = lookup(p, LABEL, 0)) == NULL)
1357                 sym = NewSymbol(p, LABEL, 0);
1358         else
1359         {
1360                 if (sym->sattr & DEFINED)
1361                         return error(".comm symbol already defined");
1362         }
1363
1364         sym->sattr = GLOBAL | COMMON | BSS;
1365
1366         if (*tok++ != ',')
1367                 return error(comma_error);
1368
1369         if (abs_expr(&eval) != OK)                              // Parse size of common region
1370                 return 0;
1371
1372         sym->svalue = eval;                                             // Install common symbol's size
1373         at_eol();
1374         return 0;
1375 }
1376
1377
1378 //
1379 // .list - Turn listing on
1380 //
1381 int d_list(void)
1382 {
1383         if (list_flag)
1384                 listing++;
1385
1386         return 0;
1387 }
1388
1389
1390 //
1391 // .nlist - Turn listing off
1392 //
1393 int d_nlist(void)
1394 {
1395         if (list_flag)
1396                 listing--;
1397
1398         return 0;
1399 }
1400
1401
1402 //
1403 // .68000 - Back to 68000 TEXT segment
1404 //
1405 int d_68000(void)
1406 {
1407         rgpu = rdsp = 0;
1408         // Switching from gpu/dsp sections should reset any ORG'd Address
1409         orgactive = 0;
1410         orgwarning = 0;
1411         SaveSection();
1412         SwitchSection(TEXT);
1413         activecpu = CPU_68000;
1414         return 0;
1415 }
1416
1417
1418 //
1419 // .68020 - Back to 68000 TEXT segment and select 68020
1420 //
1421 int d_68020(void)
1422 {
1423         d_68000();
1424         activecpu = CPU_68020;
1425         return 0;
1426 }
1427
1428
1429 //
1430 // .68030 - Back to 68000 TEXT segment and select 68030
1431 //
1432 int d_68030(void)
1433 {
1434         d_68000();
1435         activecpu = CPU_68030;
1436         return 0;
1437 }
1438
1439
1440 //
1441 // .68040 - Back to 68000 TEXT segment and select 68040
1442 //
1443 int d_68040(void)
1444 {
1445         d_68000();
1446         activecpu = CPU_68040;
1447         activefpu = FPU_68040;
1448         return 0;
1449 }
1450
1451
1452 //
1453 // .68060 - Back to 68000 TEXT segment and select 68060
1454 //
1455 int d_68060(void)
1456 {
1457         d_68000();
1458         activecpu = CPU_68060;
1459         activefpu = FPU_68040;
1460         return 0;
1461 }
1462
1463
1464 //
1465 // .68881 - Back to 68000 TEXT segment and select 68881 FPU
1466 //
1467 int d_68881(void)
1468 {
1469         d_68000();
1470         activefpu = FPU_68881;
1471         return 0;
1472 }
1473
1474
1475 //
1476 // .68882 - Back to 68000 TEXT segment and select 68882 FPU
1477 //
1478 int d_68882(void)
1479 {
1480         d_68000();
1481         activefpu = FPU_68881;
1482         return 0;
1483 }
1484
1485
1486 //
1487 // nofpu - Deselect FPUs.
1488 //
1489 int d_nofpu(void)
1490 {
1491         activefpu = FPU_NONE;
1492         return 0;
1493 }
1494
1495
1496 //
1497 // DSP56001
1498 //
1499 int d_56001(void)
1500 {
1501         return error("Not yet, child. Be patient.");
1502 }
1503
1504
1505 //
1506 // .gpu - Switch to GPU assembler
1507 //
1508 int d_gpu(void)
1509 {
1510         if ((cursect != TEXT) && (cursect != DATA))
1511         {
1512                 error(".gpu can only be used in the TEXT or DATA segments");
1513                 return ERROR;
1514         }
1515
1516         // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1517         if (!rgpu)
1518         {
1519 //printf("Resetting ORG...\n");
1520                 orgactive = 0;
1521                 orgwarning = 0;
1522         }
1523 //else printf("NOT resetting ORG!\n");
1524
1525         rgpu = 1;                       // Set GPU assembly
1526         rdsp = 0;                       // Unset DSP assembly
1527         regbank = BANK_N;       // Set no default register bank
1528         return 0;
1529 }
1530
1531
1532 //
1533 // .dsp - Switch to DSP assembler
1534 //
1535 int d_dsp(void)
1536 {
1537         if ((cursect != TEXT) && (cursect != DATA))
1538         {
1539                 error(".dsp can only be used in the TEXT or DATA segments");
1540                 return ERROR;
1541         }
1542
1543         // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1544         if (!rdsp)
1545         {
1546                 orgactive = 0;
1547                 orgwarning = 0;
1548         }
1549
1550         rdsp = 1;                       // Set DSP assembly
1551         rgpu = 0;                       // Unset GPU assembly
1552         regbank = BANK_N;       // Set no default register bank
1553         return 0;
1554 }
1555
1556
1557 //
1558 // .cargs [#offset], symbol[.size], ...
1559 //
1560 // Lists of registers may also be mentioned; they just take up space. Good for
1561 // "documentation" purposes:
1562 //
1563 // .cargs a6, .arg1, .arg2, .arg3...
1564 //
1565 // Symbols thus created are ABS and EQUATED.
1566 //
1567 int d_cargs(void)
1568 {
1569         uint32_t eval = 4;              // Default to 4 if no offset specified (to account for
1570                                                 // return address)
1571         WORD rlist;
1572         SYM * symbol;
1573         char * p;
1574         int env;
1575         int i;
1576
1577         if (rgpu || rdsp)
1578                 return error("directive forbidden in gpu/dsp mode");
1579
1580         if (*tok == '#')
1581         {
1582                 tok++;
1583
1584                 if (abs_expr(&eval) != OK)
1585                         return 0;
1586
1587                 // Eat the comma, if it's there
1588                 if (*tok == ',')
1589                         tok++;
1590         }
1591
1592         for(;;)
1593         {
1594                 if (*tok == SYMBOL)
1595                 {
1596                         p = string[tok[1]];
1597
1598                         // Set env to either local (dot prefixed) or global scope
1599                         env = (*p == '.' ? curenv : 0);
1600                         symbol = lookup(p, LABEL, env);
1601
1602                         if (symbol == NULL)
1603                         {
1604                                 symbol = NewSymbol(p, LABEL, env);
1605                                 symbol->sattr = 0;
1606                         }
1607                         else if (symbol->sattr & DEFINED)
1608                                 return error("multiply-defined label '%s'", p);
1609
1610                         // Put symbol in "order of definition" list
1611                         AddToSymbolDeclarationList(symbol);
1612
1613                         symbol->sattr |= (ABS | DEFINED | EQUATED);
1614                         symbol->svalue = eval;
1615                         tok += 2;
1616
1617                         // What this does is eat any dot suffixes attached to a symbol. If
1618                         // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1619                         // there is no dot suffix, it assumes a size of 2.
1620                         switch ((int)*tok)
1621                         {
1622                         case DOTL:
1623                                 eval += 2;
1624                         case DOTB:
1625                         case DOTW:
1626                                 tok++;
1627                         }
1628
1629                         eval += 2;
1630                 }
1631                 else if (*tok >= KW_D0 && *tok <= KW_A7)
1632                 {
1633                         if (reglist(&rlist) < 0)
1634                                 return 0;
1635
1636                         for(i=0; i<16; i++, rlist>>=1)
1637                         {
1638                                 if (rlist & 1)
1639                                         eval += 4;
1640                         }
1641                 }
1642                 else
1643                 {
1644                         switch ((int)*tok)
1645                         {
1646                         case KW_USP:
1647                         case KW_SSP:
1648                         case KW_PC:
1649                                 eval += 2;
1650                                 // FALLTHROUGH
1651                         case KW_SR:
1652                         case KW_CCR:
1653                                 eval += 2;
1654                                 tok++;
1655                                 break;
1656                         case EOL:
1657                                 return 0;
1658                         default:
1659                                 return error(".cargs syntax");
1660                         }
1661                 }
1662
1663                 // Eat commas in between each argument, if they exist
1664                 if (*tok == ',')
1665                         tok++;
1666         }
1667 }
1668
1669
1670 //
1671 // .cstruct [#offset], symbol[.size], ...
1672 //
1673 // Lists of registers may also be mentioned; they just take up space. Good for
1674 // "documentation" purposes:
1675 //
1676 // .cstruct a6, .arg1, .arg2, .arg3...
1677 //
1678 // Symbols thus created are ABS and EQUATED. Note that this is for
1679 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1680 // the suggestion.
1681 //
1682 int d_cstruct(void)
1683 {
1684         uint32_t eval = 0;              // Default, if no offset specified, is zero
1685         WORD rlist;
1686         SYM * symbol;
1687         char * symbolName;
1688         int env;
1689         int i;
1690
1691         if (rgpu || rdsp)
1692                 return error("directive forbidden in gpu/dsp mode");
1693
1694         if (*tok == '#')
1695         {
1696                 tok++;
1697
1698                 if (abs_expr(&eval) != OK)
1699                         return 0;
1700
1701                 // Eat the comma, if it's there
1702                 if (*tok == ',')
1703                         tok++;
1704         }
1705
1706         for(;;)
1707         {
1708                 if (*tok == SYMBOL)
1709                 {
1710                         symbolName = string[tok[1]];
1711
1712                         // Set env to either local (dot prefixed) or global scope
1713                         env = (symbolName[0] == '.' ? curenv : 0);
1714                         symbol = lookup(symbolName, LABEL, env);
1715
1716                         // If the symbol wasn't found, then define it. Otherwise, throw an
1717                         // error.
1718                         if (symbol == NULL)
1719                         {
1720                                 symbol = NewSymbol(symbolName, LABEL, env);
1721                                 symbol->sattr = 0;
1722                         }
1723                         else if (symbol->sattr & DEFINED)
1724                                 return error("multiply-defined label '%s'", symbolName);
1725
1726                         // Put symbol in "order of definition" list
1727                         AddToSymbolDeclarationList(symbol);
1728
1729                         tok += 2;
1730
1731                         // Adjust label start address if it's a word or a long, as a byte
1732                         // label might have left us on an odd address.
1733                         switch ((int)*tok)
1734                         {
1735                         case DOTW:
1736                         case DOTL:
1737                                 eval += eval & 0x01;
1738                         }
1739
1740                         symbol->sattr |= (ABS | DEFINED | EQUATED);
1741                         symbol->svalue = eval;
1742
1743                         // Check for dot suffixes and adjust space accordingly (longs and
1744                         // words on an odd boundary get bumped to the next word aligned
1745                         // address). If no suffix, then throw an error.
1746                         switch ((int)*tok)
1747                         {
1748                         case DOTL:
1749                                 eval += 4;
1750                                 break;
1751                         case DOTW:
1752                                 eval += 2;
1753                                 break;
1754                         case DOTB:
1755                                 eval += 1;
1756                                 break;
1757                         default:
1758                                 return error("Symbol missing dot suffix in .cstruct construct");
1759                         }
1760
1761                         tok++;
1762                 }
1763                 else if (*tok >= KW_D0 && *tok <= KW_A7)
1764                 {
1765                         if (reglist(&rlist) < 0)
1766                                 return 0;
1767
1768                         for(i=0; i<16; i++, rlist>>=1)
1769                         {
1770                                 if (rlist & 1)
1771                                         eval += 4;
1772                         }
1773                 }
1774                 else
1775                 {
1776                         switch ((int)*tok)
1777                         {
1778                         case KW_USP:
1779                         case KW_SSP:
1780                         case KW_PC:
1781                                 eval += 2;
1782                                 // FALLTHROUGH
1783                         case KW_SR:
1784                         case KW_CCR:
1785                                 eval += 2;
1786                                 tok++;
1787                                 break;
1788                         case EOL:
1789                                 return 0;
1790                         default:
1791                                 return error(".cstruct syntax");
1792                         }
1793                 }
1794
1795                 // Eat commas in between each argument, if they exist
1796                 if (*tok == ',')
1797                         tok++;
1798         }
1799 }
1800
1801
1802 //
1803 // Undefine a macro - .undefmac macname [, macname...]
1804 //
1805 int undmac1(char * p)
1806 {
1807         SYM * symbol = lookup(p, MACRO, 0);
1808
1809         // If the macro symbol exists, cause it to disappear
1810         if (symbol != NULL)
1811                 symbol->stype = (BYTE)SY_UNDEF;
1812
1813         return OK;
1814 }
1815
1816
1817 int d_undmac(void)
1818 {
1819         symlist(undmac1);
1820         return 0;
1821 }
1822
1823
1824 int d_jpad(void)
1825 {
1826         warn("JPAD directive is deprecated/non-functional");
1827         return OK;
1828 }
1829
1830
1831 int d_nojpad(void)
1832 {
1833         warn("NOJPAD directive is deprecated/non-functional");
1834         return OK;
1835 }
1836
1837
1838 int d_gpumain(void)
1839 {
1840         return error("What the hell? Do you think we adhere to the Goof standard?");
1841 }
1842
1843
1844 //
1845 // .opt - turn a specific (or all) optimisation on or off
1846 //
1847 int d_opt(void)
1848 {
1849         while (*tok != EOL)
1850         {
1851                 if (*tok == STRING)
1852                 {
1853                         tok++;
1854                         char * tmpstr = string[*tok++];
1855
1856                         if (ParseOptimization(tmpstr) != OK)
1857                                 return error("unknown optimization flag '%s'", tmpstr);
1858                 }
1859                 else
1860                         return error(".opt directive needs every switch enclosed inside quotation marks");
1861         }
1862
1863         return OK;
1864 }
1865
1866
1867 //
1868 // .if, Start conditional assembly
1869 //
1870 int d_if(void)
1871 {
1872         WORD eattr;
1873         uint32_t eval;
1874         SYM * esym;
1875         IFENT * rif = f_ifent;
1876
1877         // Alloc an IFENTRY
1878         if (rif == NULL)
1879                 rif = (IFENT *)malloc(sizeof(IFENT));
1880         else
1881                 f_ifent = rif->if_prev;
1882
1883         rif->if_prev = ifent;
1884         ifent = rif;
1885
1886         if (!disabled)
1887         {
1888                 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
1889                         return 0;
1890
1891                 if ((eattr & DEFINED) == 0)
1892                         return error(undef_error);
1893
1894                 disabled = !eval;
1895         }
1896
1897         rif->if_state = (WORD)disabled;
1898         return 0;
1899 }
1900
1901
1902 //
1903 // .else, Do alternate case for .if
1904 //
1905 int d_else(void)
1906 {
1907         IFENT * rif = ifent;
1908
1909         if (rif->if_prev == NULL)
1910                 return error("mismatched .else");
1911
1912         if (disabled)
1913                 disabled = rif->if_prev->if_state;
1914         else
1915                 disabled = 1;
1916
1917         rif->if_state = (WORD)disabled;
1918         return 0;
1919 }
1920
1921
1922 //
1923 // .endif, End of conditional assembly block
1924 // This is also called by fpop() to pop levels of IFENTs in case a macro or
1925 // include file exits early with `exitm' or `end'.
1926 //
1927 int d_endif(void)
1928 {
1929         IFENT * rif = ifent;
1930
1931         if (rif->if_prev == NULL)
1932                 return error("mismatched .endif");
1933
1934         ifent = rif->if_prev;
1935         disabled = rif->if_prev->if_state;
1936         rif->if_prev = f_ifent;
1937         f_ifent = rif;
1938         return 0;
1939 }
1940