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
29 // N.B.: It's perfectly fine to keep exprbuf as an array of TOKENS and to cast
30 // to a TOKENPTR where needed. But this works too. :-P
31 TOKEN _exprbuf[128]; // Expression buffer
32 TOKENPTR exprbuf = (TOKENPTR)_exprbuf; // Expression buffer point
33 SYM * symbolPtr[1000000]; // Symbol pointers table
34 static long unused; // For supressing 'write' warnings
35 char buffer[256]; // Scratch buffer for messages
36 int largestAlign[3] = { 2, 2, 2 }; // Largest alignment value seen per section
38 // Function prototypes
75 int d_equrundef(void);
86 void SetLargestAlignment(int);
88 // Directive handler table
99 (void *)d_init, // 9 init
101 (void *)d_goto, // 11 goto
102 (void *)d_dc, // 12 dc
103 (void *)d_ds, // 13 ds
104 d_undmac, // 14 undefmac
107 (void *)d_dcb, // 17 dcb
111 d_incbin, // 21 .incbin //load
112 d_unimpl, // 22 disable
113 d_unimpl, // 23 enable
115 d_regbank0, // 25 .regbank0
116 d_regbank1, // 26 .regbank1
118 d_assert, // 28 assert
120 d_unimpl, // 30* endif
121 d_unimpl, // 31* endc
123 d_include, // 33 include
125 d_unimpl, // 35* macro
126 ExitMacro, // 36* exitm
127 d_unimpl, // 37* endm
131 d_phrase, // 41* endr
132 d_dphrase, // 42 struct
133 d_qphrase, // 43 ends
135 d_subttl, // 45 subttl
139 d_noclear, // 49 .noclear
140 d_equrundef, // 50 .equrundef/.regundef
141 d_ccundef, // 51 .ccundef
142 d_print, // 52 .print
143 d_cstruct, // 53 .cstruct
144 d_jpad, // 54 .jpad (deprecated)
145 d_nojpad, // 55 .nojpad (deprecated)
146 d_gpumain, // 56 .gpumain (deprecated)
147 d_prgflags, // 57 .prgflags
148 d_68020, // 58 .68020
149 d_68030, // 59 .68030
150 d_68040, // 60 .68040
151 d_68060, // 61 .68060
152 d_68881, // 62 .68881
153 d_68882, // 63 .68882
154 d_56001, // 64 .56001
161 // Set the largest alignment seen in the current section
163 void SetLargestAlignment(int size)
165 if ((scattr & TEXT) && (largestAlign[0] < size))
166 largestAlign[0] = size;
167 else if ((scattr & DATA) && (largestAlign[1] < size))
168 largestAlign[1] = size;
169 else if ((scattr & BSS) && (largestAlign[2] < size))
170 largestAlign[2] = size;
175 // .error - Abort compilation, printing an error message
177 int d_error(char *str)
180 return error("error directive encountered - aborting assembling");
186 return error(string[tok.u32[1]]);
189 return error("error directive encountered--aborting assembly");
196 // .warn - Just display a warning on screen
198 int d_warn(char *str)
201 return warn("WARNING WARNING WARNING");
207 return warn(string[tok.u32[1]]);
210 return warn("WARNING WARNING WARNING");
223 if (!rgpu && !rdsp && !m6502)
224 return error(".org permitted only in gpu/dsp and 6502 sections");
226 if (abs_expr(&address) == ERROR)
228 error("cannot determine org'd address");
239 // 6502. We also kludge `lsloc' so the listing generator doesn't try
240 // to spew out megabytes.
241 if (address > 0xFFFF)
242 return error(range_error);
244 if (sloc != currentorg[0])
246 currentorg[1] = sloc;
250 currentorg[0] = address;
252 lsloc = sloc = address;
253 chptr = scode->chptr + address;
268 char prntstr[LNSIZ]; // String for PRINT directive
269 char format[LNSIZ]; // Format for PRINT directive
270 int formatting = 0; // Formatting on/off
271 int wordlong = 0; // WORD = 0, LONG = 1
272 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
274 uint64_t eval; // Expression value
275 WORD eattr; // Expression attributes
276 SYM * esym; // External symbol involved in expr.
277 TOKEN r_expr[EXPRSIZE];
279 while (*tok.u32 != EOL)
284 sprintf(prntstr, "%s", string[tok.u32[1]]);
285 printf("%s", prntstr);
288 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
295 if (tok.u32[1] != SYMBOL)
298 // strcpy(prntstr, (char *)tok.u32[2]);
299 strcpy(prntstr, string[tok.u32[2]]);
303 case 'l': case 'L': wordlong = 1; break;
304 case 'w': case 'W': wordlong = 0; break;
305 case 'x': case 'X': outtype = 0; break;
306 case 'd': case 'D': outtype = 1; break;
307 case 'u': case 'U': outtype = 2; break;
309 error("unknown print format flag");
319 if (expr((TOKENPTR)r_expr, &eval, &eattr, &esym) != OK)
325 case 0: strcpy(format, "%X"); break;
326 case 1: strcpy(format, "%d" ); break;
327 case 2: strcpy(format, "%u" ); break;
331 sprintf(prntstr, format, eval);
333 sprintf(prntstr, format, eval & 0xFFFF);
335 printf("%s", prntstr);
338 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
354 error("illegal print token");
360 // Undefine an equated condition code
366 // Check that we are in a RISC section
369 error(".ccundef must be defined in .gpu/.dsp section");
373 if (*tok.u32 != SYMBOL)
375 error("syntax error; expected symbol");
379 ccname = lookup(string[tok.u32[1]], LABEL, 0);
381 // Make sure symbol is a valid ccdef
382 if (!ccname || !(ccname->sattre & EQUATEDCC))
384 error("invalid equated condition name specified");
388 ccname->sattre |= UNDEF_CC;
395 // Undefine an equated register
397 int d_equrundef(void)
401 // Check that we are in a RISC section
403 return error(".equrundef/.regundef must be defined in .gpu/.dsp section");
405 while (*tok.u32 != EOL)
407 // Skip preceeding or seperating commas (if any)
411 // Check we are dealing with a symbol
412 if (*tok.u32 != SYMBOL)
413 return error("syntax error; expected symbol");
415 // Lookup and undef if equated register
416 regname = lookup(string[tok.u32[1]], LABEL, 0);
418 if (regname && (regname->sattre & EQUATEDREG))
420 // Reset the attributes of this symbol...
422 regname->sattre &= ~(EQUATEDREG | BANK_0 | BANK_1);
423 regname->sattre |= UNDEF_EQUR;
426 // Skip over symbol token and address
435 // Do not allow use of the CLR.L opcode
439 warn("CLR.L opcode ignored...");
445 // Include binary file
451 long pos, size, bytesRead;
455 // Check to see if we're in BSS, and, if so, throw an error
458 error("cannot include binary file \"%s\" in BSS section", string[tok.u32[1]]);
462 if (*tok.u32 != STRING)
464 error("syntax error; string missing");
468 // Attempt to open the include file in the current directory, then (if that
469 // failed) try list of include files passed in the enviroment string or by
471 if ((fd = open(string[tok.u32[1]], _OPEN_INC)) < 0)
473 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
477 // Append path char if necessary
478 if (fd > 0 && buf1[fd - 1] != SLASHCHAR)
479 strcat(buf1, SLASHSTRING);
481 strcat(buf1, string[tok.u32[1]]);
483 if ((fd = open(buf1, _OPEN_INC)) >= 0)
487 return error("cannot open: \"%s\"", string[tok.u32[1]]);
492 size = lseek(fd, 0L, SEEK_END);
493 pos = lseek(fd, 0L, SEEK_SET);
496 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[tok.u32[1]], size); }
498 char * fileBuffer = (char *)malloc(size);
499 bytesRead = read(fd, fileBuffer, size);
501 if (bytesRead != size)
503 error("was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[tok.u32[1]], size);
507 memcpy(chptr, fileBuffer, size);
522 // Set RISC register banks
526 // Set active register bank zero
534 // Set active register bank one
541 // Helper function, to cut down on mistakes & typing
543 static inline void SkipBytes(unsigned bytesToSkip)
548 if ((scattr & SBSS) == 0)
550 chcheck(bytesToSkip);
551 D_ZEROFILL(bytesToSkip);
558 orgaddr += bytesToSkip;
564 // Adjust location to an EVEN value
569 return error(in_6502mode);
571 unsigned skip = (rgpu || rdsp ? orgaddr : sloc) & 0x01;
575 if ((scattr & SBSS) == 0)
594 // Adjust location to a LONG value
598 unsigned lower2Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x03;
599 unsigned bytesToSkip = (0x04 - lower2Bits) & 0x03;
600 SkipBytes(bytesToSkip);
601 SetLargestAlignment(4);
608 // Adjust location to a PHRASE value
610 // N.B.: We have to handle the GPU/DSP cases separately because you can embed
611 // RISC code in the middle of a regular 68K section. Also note that all
612 // of the alignment pseudo-ops will have to be fixed this way.
614 // This *must* behave differently when in a RISC section, as following sloc
615 // (instead of orgaddr) will fuck things up royally. Note that we do it this
616 // way because you can embed RISC code in a 68K section, and have the origin
617 // pointing to a different alignment in the RISC section than the 68K section.
621 unsigned lower3Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x07;
622 unsigned bytesToSkip = (0x08 - lower3Bits) & 0x07;
623 SkipBytes(bytesToSkip);
624 SetLargestAlignment(8);
631 // Adjust location to a DPHRASE value
635 unsigned lower4Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x0F;
636 unsigned bytesToSkip = (0x10 - lower4Bits) & 0x0F;
637 SkipBytes(bytesToSkip);
638 SetLargestAlignment(16);
645 // Adjust location to a QPHRASE value
649 unsigned lower5Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x1F;
650 unsigned bytesToSkip = (0x20 - lower5Bits) & 0x1F;
651 SkipBytes(bytesToSkip);
652 SetLargestAlignment(32);
659 // Do auto-even. This must be called ONLY if 'sloc' is odd.
661 // This is made hairy because, if there was a label on the line, we also have
662 // to adjust its value. This won't work with more than one label on the line,
663 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
664 // the C compiler is VERY paranoid and uses ".even" whenever it can
666 // N.B.: This probably needs the same fixes as above...
670 if (cursect != M6502)
673 sloc++; // Bump BSS section
675 D_byte(0); // Deposit 0.b in non-BSS
677 if (lab_sym != NULL) // Bump label if we have to
684 // Unimplemened directive error
688 return error("unimplemented directive");
693 // Return absolute (not TDB) and defined expression or return an error
695 int abs_expr(uint64_t * a_eval)
699 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
702 if (!(eattr & DEFINED))
703 return error(undef_error);
706 return error(rel_error);
713 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
715 int symlist(int(* func)())
717 const char * em = "symbol list syntax";
721 if (*tok.u32 != SYMBOL)
724 if ((*func)(string[tok.u32[1]]) != OK)
743 // .include "filename"
753 if (*tok.u32 == STRING) // Leave strings ALONE
754 fn = string[*++tok.u32];
755 else if (*tok.u32 == SYMBOL) // Try to append ".s" to symbols
757 strcpy(buf, string[*++tok.u32]);
761 else // Punt if no STRING or SYMBOL
762 return error("missing filename");
764 // Make sure the user didn't try anything like:
765 // .include equates.s
766 if (*++tok.u32 != EOL)
767 return error("extra stuff after filename--enclose it in quotes");
769 // Attempt to open the include file in the current directory, then (if that
770 // failed) try list of include files passed in the enviroment string or by
772 if ((j = open(fn, 0)) < 0)
774 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
778 // Append path char if necessary
779 if (j > 0 && buf1[j - 1] != SLASHCHAR)
780 strcat(buf1, SLASHSTRING);
784 if ((j = open(buf1, 0)) >= 0)
788 return error("cannot open: \"%s\"", fn);
798 // .assert expression [, expression...]
805 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok.u32)
807 if (!(eattr & DEFINED))
808 return error("forward or undefined .assert");
811 return error("assert failure");
823 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
830 return error("cannot .globl local symbol");
832 if ((sy = lookup(p, LABEL, 0)) == NULL)
834 sy = NewSymbol(p, LABEL, 0);
837 //printf("glob1: Making global symbol: attr=%04X, eattr=%08X, %s\n", sy->sattr, sy->sattre, sy->sname);
849 return error(in_6502mode);
857 // .prgflags expression
864 return error("PRGFLAGS requires value");
865 else if (abs_expr(&eval) == OK)
867 PRGFLAGS = (uint32_t)eval;
872 return error("PRGFLAGS requires value");
885 return error(in_6502mode);
891 else if (abs_expr(&eval) != OK)
895 sloc = (uint32_t)eval;
906 return error("directive forbidden in gpu/dsp mode");
908 return error(in_6502mode);
923 return error("directive forbidden in gpu/dsp mode");
925 return error(in_6502mode);
940 return error("directive forbidden in gpu/dsp mode");
942 return error(in_6502mode);
955 // .ds[.size] expression
959 DEBUG { printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc); }
963 if (cursect != M6502)
965 if ((siz != SIZB) && (sloc & 1)) // Automatic .even
969 if (abs_expr(&eval) != OK)
972 // Check to see if the value being passed in is negative (who the hell does
973 // that?--nobody does; it's the code gremlins, or rum, that does it)
974 // N.B.: Since 'eval' is of type uint32_t, if it goes negative, it will have
976 if (eval & 0x80000000)
977 return error("negative sizes not allowed");
979 // In non-TDB section (BSS, ABS and M6502) just advance the location
980 // counter appropriately. In TDB sections, deposit (possibly large) chunks
981 // of zeroed memory....
982 if ((scattr & SBSS) || cursect == M6502)
984 listvalue((uint32_t)eval);
986 sloc += (uint32_t)eval;
988 if (cursect == M6502)
991 just_bss = 1; // No data deposited (8-bit CPU mode)
995 dep_block(eval, siz, 0, (WORD)(DEFINED | ABS), (TOKENPTR){NULL});
1004 // dc.b, dc.w / dc, dc.l, dc.i, dc.q, dc.d
1012 if ((scattr & SBSS) != 0)
1013 return error("illegal initialization of section");
1015 // Do an auto_even if it's not BYTE sized (hmm, should we be doing this???)
1016 if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1019 // Check to see if we're trying to set LONGS on a non 32-bit aligned
1020 // address in a GPU or DSP section, in their local RAM
1021 if ((siz == SIZL) && (orgaddr & 0x03)
1022 && ((rgpu && (orgaddr >= 0xF03000) && (orgaddr <= 0xF03FFFF))
1023 || (rdsp && (orgaddr >= 0xF1B000) && (orgaddr <= 0xF1CFFFF))))
1024 warn("depositing LONGs on a non-long address in local RAM");
1028 // dc.b 'string' [,] ...
1029 if (siz == SIZB && (*tok.u32 == STRING || *tok.u32 == STRINGA8) && (tok.u32[2] == ',' || tok.u32[2] == EOL))
1031 uint32_t i = strlen(string[tok.u32[1]]);
1033 if ((challoc - ch_size) < i)
1036 if (*tok.u32 == STRING)
1038 for(p=string[tok.u32[1]]; *p!=EOS; p++)
1041 else if(*tok.u32 == STRINGA8)
1043 for(p=string[tok.u32[1]]; *p!=EOS; p++)
1044 D_byte(strtoa8[*p]);
1048 error("String format not supported... yet");
1055 int movei = 0; // MOVEI flag for dc.i
1057 if (*tok.u32 == DOTI)
1064 // dc.x <expression>
1067 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
1070 uint16_t tdb = eattr & TDB;
1071 uint16_t defined = eattr & DEFINED;
1073 if ((challoc - ch_size) < 4)
1081 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1087 return error("non-absolute byte value");
1089 if (eval + 0x100 >= 0x200)
1090 return error("%s (value = $%X)", range_error, eval);
1100 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1105 if (eval + 0x10000 >= 0x20000)
1106 return error(range_error);
1109 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1111 // Deposit 68000 or 6502 (byte-reversed) word
1112 if (cursect != M6502)
1120 // Shamus: Why can't we do longs in 6502 mode?
1122 return error(in_6502mode);
1127 AddFixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
1129 AddFixup(FU_LONG, sloc, exprbuf);
1136 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1139 eval = WORDSWAP32(eval);
1147 return error(in_6502mode);
1149 // Shamus: We only handle DC.Q type stuff, will have to add fixups
1150 // and stuff later (maybe... might not be needed...)
1155 return error(in_6502mode);
1160 AddFixup(FU_FLOATSING, sloc, exprbuf);
1167 MarkRelocatable(cursect, sloc, tdb, MSINGLE, NULL);
1175 return error(in_6502mode);
1180 AddFixup(FU_FLOATDOUB, sloc, exprbuf);
1187 MarkRelocatable(cursect, sloc, tdb, MDOUBLE, NULL);
1189 double vv = *(double *)&eval;
1196 return error(in_6502mode);
1201 AddFixup(FU_FLOATEXT, sloc, exprbuf);
1208 MarkRelocatable(cursect, sloc, tdb, MEXTEND, NULL);
1210 float vv = *(double *)&eval;
1218 if (*tok.u32 != ',')
1228 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1232 uint64_t evalc, eval;
1235 DEBUG { printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr); }
1237 if ((scattr & SBSS) != 0)
1238 return error("illegal initialization of section");
1240 if (abs_expr(&evalc) != OK)
1243 if (*tok.u32++ != ',')
1244 return error("missing comma");
1246 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1249 if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1252 dep_block((uint32_t)evalc, siz, (uint32_t)eval, eattr, exprbuf);
1258 // Generalized initialization directive
1260 // .init[.siz] [#count,] expression [.size] , ...
1262 // The size suffix on the ".init" directive becomes the default size of the
1263 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1264 // and an expression, it specifies a repeat count. The value to be deposited
1265 // may be followed by a size suffix, which overrides the default size.
1267 int d_init(WORD def_siz)
1274 if ((scattr & SBSS) != 0)
1275 return error(".init not permitted in BSS or ABS");
1278 return error("directive forbidden in gpu/dsp mode");
1282 // Get repeat count (defaults to 1)
1283 if (*tok.u32 == '#')
1287 if (abs_expr(&count) != OK)
1290 if (*tok.u32++ != ',')
1291 return error(comma_error);
1296 // Evaluate expression to deposit
1297 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1301 { // Determine size of object to deposit
1302 case DOTB: siz = SIZB; break;
1303 case DOTW: siz = SIZB; break;
1304 case DOTL: siz = SIZL; break;
1311 dep_block((uint32_t)count, siz, (uint32_t)eval, eattr, exprbuf);
1321 return error(comma_error);
1328 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1330 int dep_block(uint32_t count, WORD siz, uint32_t eval, WORD eattr, TOKENPTR exprbuf)
1335 tdb = (WORD)(eattr & TDB);
1336 defined = (WORD)(eattr & DEFINED);
1340 if ((challoc - ch_size) < 4)
1348 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1354 return error("non-absolute byte value");
1356 if (eval + 0x100 >= 0x200)
1357 return error(range_error);
1367 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1373 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1375 if (eval + 0x10000 >= 0x20000)
1376 return error(range_error);
1378 // Deposit 68000 or 6502 (byte-reversed) word
1379 if (cursect != M6502)
1389 return error(in_6502mode);
1393 AddFixup(FU_LONG, sloc, exprbuf);
1399 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1413 // .comm symbol, size
1422 return error(in_6502mode);
1424 if (*tok.u32 != SYMBOL)
1425 return error("missing symbol");
1427 p = string[tok.u32[1]];
1430 if (*p == '.') // Cannot .comm a local symbol
1431 return error(locgl_error);
1433 if ((sym = lookup(p, LABEL, 0)) == NULL)
1434 sym = NewSymbol(p, LABEL, 0);
1437 if (sym->sattr & DEFINED)
1438 return error(".comm symbol already defined");
1441 sym->sattr = GLOBAL | COMMON | BSS;
1443 if (*tok.u32++ != ',')
1444 return error(comma_error);
1446 if (abs_expr(&eval) != OK) // Parse size of common region
1449 sym->svalue = (uint32_t)eval; // Install common symbol's size
1456 // .list - Turn listing on
1468 // .nlist - Turn listing off
1480 // .68000 - Back to 68000 TEXT segment
1485 // Switching from gpu/dsp sections should reset any ORG'd Address
1489 SwitchSection(TEXT);
1490 activecpu = CPU_68000;
1496 // .68020 - Back to 68000 TEXT segment and select 68020
1501 activecpu = CPU_68020;
1507 // .68030 - Back to 68000 TEXT segment and select 68030
1512 activecpu = CPU_68030;
1518 // .68040 - Back to 68000 TEXT segment and select 68040
1523 activecpu = CPU_68040;
1524 activefpu = FPU_68040;
1530 // .68060 - Back to 68000 TEXT segment and select 68060
1535 activecpu = CPU_68060;
1536 activefpu = FPU_68040;
1542 // .68881 - Back to 68000 TEXT segment and select 68881 FPU
1547 activefpu = FPU_68881;
1553 // .68882 - Back to 68000 TEXT segment and select 68882 FPU
1558 activefpu = FPU_68881;
1564 // nofpu - Deselect FPUs.
1568 activefpu = FPU_NONE;
1578 return error("Not yet, child. Be patient.");
1583 // .gpu - Switch to GPU assembler
1587 if ((cursect != TEXT) && (cursect != DATA))
1589 error(".gpu can only be used in the TEXT or DATA segments");
1593 // If previous section was DSP or 68000 then we need to reset ORG'd Addresses
1600 rgpu = 1; // Set GPU assembly
1601 rdsp = 0; // Unset DSP assembly
1602 regbank = BANK_N; // Set no default register bank
1608 // .dsp - Switch to DSP assembler
1612 if ((cursect != TEXT) && (cursect != DATA))
1614 error(".dsp can only be used in the TEXT or DATA segments");
1618 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1625 rdsp = 1; // Set DSP assembly
1626 rgpu = 0; // Unset GPU assembly
1627 regbank = BANK_N; // Set no default register bank
1633 // .cargs [#offset], symbol[.size], ...
1635 // Lists of registers may also be mentioned; they just take up space. Good for
1636 // "documentation" purposes:
1638 // .cargs a6, .arg1, .arg2, .arg3...
1640 // Symbols thus created are ABS and EQUATED.
1644 uint64_t eval = 4; // Default to 4 if no offset specified (to account for
1653 return error("directive forbidden in gpu/dsp mode");
1655 if (*tok.u32 == '#')
1659 if (abs_expr(&eval) != OK)
1662 // Eat the comma, if it's there
1663 if (*tok.u32 == ',')
1669 if (*tok.u32 == SYMBOL)
1671 p = string[tok.u32[1]];
1673 // Set env to either local (dot prefixed) or global scope
1674 env = (*p == '.' ? curenv : 0);
1675 symbol = lookup(p, LABEL, env);
1679 symbol = NewSymbol(p, LABEL, env);
1682 else if (symbol->sattr & DEFINED)
1683 return error("multiply-defined label '%s'", p);
1685 // Put symbol in "order of definition" list
1686 AddToSymbolDeclarationList(symbol);
1688 symbol->sattr |= (ABS | DEFINED | EQUATED);
1689 symbol->svalue = (uint32_t)eval;
1692 // What this does is eat any dot suffixes attached to a symbol. If
1693 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1694 // there is no dot suffix, it assumes a size of 2.
1695 switch ((int)*tok.u32)
1706 else if (*tok.u32 >= KW_D0 && *tok.u32 <= KW_A7)
1708 if (reglist(&rlist) < 0)
1711 for(i=0; i<16; i++, rlist>>=1)
1719 switch ((int)*tok.u32)
1734 return error(".cargs syntax");
1738 // Eat commas in between each argument, if they exist
1739 if (*tok.u32 == ',')
1746 // .cstruct [#offset], symbol[.size], ...
1748 // Lists of registers may also be mentioned; they just take up space. Good for
1749 // "documentation" purposes:
1751 // .cstruct a6, .arg1, .arg2, .arg3...
1753 // Symbols thus created are ABS and EQUATED. Note that this is for
1754 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1759 uint64_t eval = 0; // Default, if no offset specified, is zero
1767 return error("directive forbidden in gpu/dsp mode");
1769 if (*tok.u32 == '#')
1773 if (abs_expr(&eval) != OK)
1776 // Eat the comma, if it's there
1777 if (*tok.u32 == ',')
1783 if (*tok.u32 == SYMBOL)
1785 symbolName = string[tok.u32[1]];
1787 // Set env to either local (dot prefixed) or global scope
1788 env = (symbolName[0] == '.' ? curenv : 0);
1789 symbol = lookup(symbolName, LABEL, env);
1791 // If the symbol wasn't found, then define it. Otherwise, throw an
1795 symbol = NewSymbol(symbolName, LABEL, env);
1798 else if (symbol->sattr & DEFINED)
1799 return error("multiply-defined label '%s'", symbolName);
1801 // Put symbol in "order of definition" list
1802 AddToSymbolDeclarationList(symbol);
1806 // Adjust label start address if it's a word or a long, as a byte
1807 // label might have left us on an odd address.
1808 switch ((int)*tok.u32)
1812 eval += eval & 0x01;
1815 symbol->sattr |= (ABS | DEFINED | EQUATED);
1816 symbol->svalue = (uint32_t)eval;
1818 // Check for dot suffixes and adjust space accordingly (longs and
1819 // words on an odd boundary get bumped to the next word aligned
1820 // address). If no suffix, then throw an error.
1821 switch ((int)*tok.u32)
1833 return error("Symbol missing dot suffix in .cstruct construct");
1838 else if (*tok.u32 >= KW_D0 && *tok.u32 <= KW_A7)
1840 if (reglist(&rlist) < 0)
1843 for(i=0; i<16; i++, rlist>>=1)
1851 switch ((int)*tok.u32)
1866 return error(".cstruct syntax");
1870 // Eat commas in between each argument, if they exist
1871 if (*tok.u32 == ',')
1878 // Undefine a macro - .undefmac macname [, macname...]
1880 int undmac1(char * p)
1882 SYM * symbol = lookup(p, MACRO, 0);
1884 // If the macro symbol exists, cause it to disappear
1886 symbol->stype = (BYTE)SY_UNDEF;
1901 warn("JPAD directive is deprecated/non-functional");
1908 warn("NOJPAD directive is deprecated/non-functional");
1915 return error("What the hell? Do you think we adhere to the Goof standard?");
1920 // .opt - turn a specific (or all) optimisation on or off
1924 while (*tok.u32 != EOL)
1926 if (*tok.u32 == STRING)
1929 char * tmpstr = string[*tok.u32++];
1931 if (ParseOptimization(tmpstr) != OK)
1932 return error("unknown optimization flag '%s'", tmpstr);
1935 return error(".opt directive needs every switch enclosed inside quotation marks");
1943 // .if, Start conditional assembly
1950 IFENT * rif = f_ifent;
1954 rif = (IFENT *)malloc(sizeof(IFENT));
1956 f_ifent = rif->if_prev;
1958 rif->if_prev = ifent;
1963 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
1966 if ((eattr & DEFINED) == 0)
1967 return error(undef_error);
1972 rif->if_state = (WORD)disabled;
1978 // .else, Do alternate case for .if
1982 IFENT * rif = ifent;
1984 if (rif->if_prev == NULL)
1985 return error("mismatched .else");
1988 disabled = rif->if_prev->if_state;
1992 rif->if_state = (WORD)disabled;
1998 // .endif, End of conditional assembly block
1999 // This is also called by fpop() to pop levels of IFENTs in case a macro or
2000 // include file exits early with `exitm' or `end'.
2004 IFENT * rif = ifent;
2006 if (rif->if_prev == NULL)
2007 return error("mismatched .endif");
2009 ifent = rif->if_prev;
2010 disabled = rif->if_prev->if_state;
2011 rif->if_prev = f_ifent;