2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // DIRECT.C - Directive Handling
4 // Copyright (C) 199x Landon Dyer, 2011 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
25 TOKEN exprbuf[128]; // Expression buffer
26 SYM * symbolPtr[1000000]; // Symbol pointers table
27 static long unused; // For supressing 'write' warnings
28 char buffer[256]; // Scratch buffer for messages
31 // Directive handler table
42 (void *)d_init, // 9 init
44 (void *)d_goto, // 11 goto
45 (void *)d_dc, // 12 dc
46 (void *)d_ds, // 13 ds
47 d_undmac, // 14 undefmac
50 (void *)d_dcb, // 17 dcb
54 d_incbin, // 21 .incbin //load
55 d_unimpl, // 22 disable
56 d_unimpl, // 23 enable
58 d_regbank0, // 25 .regbank0
59 d_regbank1, // 26 .regbank1
61 d_assert, // 28 assert
63 d_unimpl, // 30* endif
66 d_include, // 33 include
68 d_unimpl, // 35* macro
69 ExitMacro, // 36* exitm
75 d_dphrase, // 42 struct
78 d_subttl, // 45 subttl
82 d_noclear, // 49 .noclear
83 d_equrundef, // 50 .equrundef/.regundef
84 d_ccundef, // 51 .ccundef
86 d_cstruct, // 53 .cstruct
87 d_jpad, // 54 .jpad (deprecated)
88 d_nojpad, // 55 .nojpad (deprecated)
89 d_gpumain, // 56 .gpumain (deprecated)
90 d_prgflags, // 57 .prgflags
95 // .error - Abort compilation, printing an error message
97 int d_error(char *str)
100 return error("error directive encountered - aborting assembling");
106 return error(string[tok[1]]);
109 return error("error directive encountered - aborting assembling");
116 // .warn - Just display a warning on screen
118 int d_warn(char *str)
121 return warn("WARNING WARNING WARNING");
127 return warn(string[tok[1]]);
130 return warn("WARNING WARNING WARNING");
144 return error(".org permitted only in gpu/dsp section");
148 if (abs_expr(&address) == ERROR)
150 error("cannot determine org'd address");
156 //printf("ORG: address = $%08X...\n", orgaddr);
167 char prntstr[LNSIZ]; // String for PRINT directive
168 char format[LNSIZ]; // Format for PRINT directive
169 int formatting = 0; // Formatting on/off
170 int wordlong = 0; // WORD = 0, LONG = 1
171 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
173 VALUE eval; // Expression value
174 WORD eattr; // Expression attributes
175 SYM * esym; // External symbol involved in expr.
176 TOKEN r_expr[EXPRSIZE];
183 sprintf(prntstr, "%s", string[tok[1]]);
184 printf("%s", prntstr);
187 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
194 if (tok[1] != SYMBOL)
197 // strcpy(prntstr, (char *)tok[2]);
198 strcpy(prntstr, string[tok[2]]);
202 case 'l': case 'L': wordlong = 1; break;
203 case 'w': case 'W': wordlong = 0; break;
204 case 'x': case 'X': outtype = 0; break;
205 case 'd': case 'D': outtype = 1; break;
206 case 'u': case 'U': outtype = 2; break;
208 error("unknown print format flag");
218 if (expr(r_expr, &eval, &eattr, &esym) != OK)
224 case 0: strcpy(format, "%X"); break;
225 case 1: strcpy(format, "%d" ); break;
226 case 2: strcpy(format, "%u" ); break;
230 sprintf(prntstr, format, eval);
232 sprintf(prntstr, format, eval & 0xFFFF);
234 printf("%s", prntstr);
237 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
253 error("illegal print token");
259 // Undefine an equated condition code
265 // Check that we are in a RISC section
268 error(".ccundef must be defined in .gpu/.dsp section");
274 error("syntax error; expected symbol");
278 ccname = lookup(string[tok[1]], LABEL, 0);
280 // Make sure symbol is a valid ccdef
281 if (!ccname || !(ccname->sattre & EQUATEDCC))
283 error("invalid equated condition name specified");
287 ccname->sattre |= UNDEF_CC;
294 // Undefine an equated register
296 int d_equrundef(void)
300 // Check that we are in a RISC section
302 return error(".equrundef/.regundef must be defined in .gpu/.dsp section");
306 // Skip preceeding or seperating commas (if any)
310 // Check we are dealing with a symbol
312 return error("syntax error; expected symbol");
314 // Lookup and undef if equated register
315 regname = lookup(string[tok[1]], LABEL, 0);
317 if (regname && (regname->sattre & EQUATEDREG))
319 // Reset the attributes of this symbol...
321 regname->sattre &= ~(EQUATEDREG | BANK_0 | BANK_1);
322 regname->sattre |= UNDEF_EQUR;
325 // Skip over symbol token and address
334 // Do not allow use of the CLR.L opcode
338 warn("CLR.L opcode ignored...");
344 // Include binary file
350 long pos, size, bytesRead;
353 // Check to see if we're in BSS, and, if so, throw an error
356 errors("cannot include binary file \"%s\" in BSS section", string[tok[1]]);
362 error("syntax error; string missing");
366 if ((fd = open(string[tok[1]], _OPEN_INC)) < 0)
368 errors("cannot open include binary file (%s)", string[tok[1]]);
372 size = lseek(fd, 0L, SEEK_END);
373 pos = lseek(fd, 0L, SEEK_SET);
376 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size); }
378 char * fileBuffer = (char *)malloc(size);
379 bytesRead = read(fd, fileBuffer, size);
381 if (bytesRead != size)
383 sprintf(msg, "was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[tok[1]], size);
388 memcpy(chptr, fileBuffer, size);
403 // Set RISC register banks
407 // Set active register bank zero
415 // Set active register bank one
422 // Helper function, to cut down on mistakes & typing
424 static inline void SkipBytes(unsigned bytesToSkip)
429 if ((scattr & SBSS) == 0)
431 chcheck(bytesToSkip);
432 D_ZEROFILL(bytesToSkip);
439 orgaddr += bytesToSkip;
445 // Adjust location to an EVEN value
449 unsigned skip = (rgpu || rdsp ? orgaddr : sloc) & 0x01;
453 if ((scattr & SBSS) == 0)
472 // Adjust location to a LONG value
476 unsigned lower2Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x03;
477 unsigned bytesToSkip = (0x04 - lower2Bits) & 0x03;
478 SkipBytes(bytesToSkip);
485 // Adjust location to a PHRASE value
487 // N.B.: We have to handle the GPU/DSP cases separately because you can embed
488 // RISC code in the middle of a regular 68K section. Also note that all
489 // of the alignment pseudo-ops will have to be fixed this way.
491 // This *must* behave differently when in a RISC section, as following sloc
492 // (instead of orgaddr) will fuck things up royally. Note that we do it this
493 // way because you can embed RISC code in a 68K section, and have the origin
494 // pointing to a different alignment in the RISC section than the 68K section.
498 unsigned lower3Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x07;
499 unsigned bytesToSkip = (0x08 - lower3Bits) & 0x07;
500 SkipBytes(bytesToSkip);
507 // Adjust location to a DPHRASE value
511 unsigned lower4Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x0F;
512 unsigned bytesToSkip = (0x10 - lower4Bits) & 0x0F;
513 SkipBytes(bytesToSkip);
520 // Adjust location to a QPHRASE value
524 unsigned lower5Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x1F;
525 unsigned bytesToSkip = (0x20 - lower5Bits) & 0x1F;
526 SkipBytes(bytesToSkip);
533 // Do auto-even. This must be called ONLY if 'sloc' is odd.
535 // This is made hairy because, if there was a label on the line, we also have
536 // to adjust its value. This won't work with more than one label on the line,
537 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
538 // the C compiler is VERY paranoid and uses ".even" whenever it can
540 // N.B.: This probably needs the same fixes as above...
545 sloc++; // Bump BSS section
547 D_byte(0); // Deposit 0.b in non-BSS
549 if (lab_sym != NULL) // Bump label if we have to
555 // Unimplemened directive error
559 return error("unimplemented directive");
564 // Return absolute (not TDB) and defined expression or return an error
566 int abs_expr(VALUE * a_eval)
570 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
573 if (!(eattr & DEFINED))
574 return error(undef_error);
577 return error(rel_error);
584 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
586 int symlist(int(* func)())
588 const char * em = "symbol list syntax";
595 if ((*func)(string[tok[1]]) != OK)
614 // .include "filename"
624 if (*tok == STRING) // Leave strings ALONE
626 else if (*tok == SYMBOL) // Try to append ".s" to symbols
628 strcpy(buf, string[*++tok]);
632 else // Punt if no STRING or SYMBOL
633 return error("missing filename");
635 // Make sure the user didn't try anything like:
636 // .include equates.s
638 return error("extra stuff after filename -- enclose it in quotes");
640 // Attempt to open the include file in the current directory, then (if that
641 // failed) try list of include files passed in the enviroment string or by
643 if ((j = open(fn, 0)) < 0)
645 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
649 if (j > 0 && buf1[j - 1] != SLASHCHAR) // Append path char if necessary
650 strcat(buf1, SLASHSTRING);
654 if ((j = open(buf1, 0)) >= 0)
658 return errors("cannot open: \"%s\"", fn);
668 // .assert expression [, expression...]
675 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
677 if (!(eattr & DEFINED))
678 return error("forward or undefined .assert");
681 return error("assert failure");
693 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
700 return error("cannot .globl local symbol");
702 if ((sy = lookup(p, LABEL, 0)) == NULL)
704 sy = NewSymbol(p, LABEL, 0);
707 //printf("glob1: Making global symbol: attr=%04X, eattr=%08X, %s\n", sy->sattr, sy->sattre, sy->sname);
724 // .prgflags expression
731 return error("PRGFLAGS requires value");
732 else if (abs_expr(&eval) == OK)
739 return error("PRGFLAGS requires value");
755 else if (abs_expr(&eval) != OK)
770 return error("directive forbidden in gpu/dsp mode");
785 return error("directive forbidden in gpu/dsp mode");
800 return error("directive forbidden in gpu/dsp mode");
813 // .ds[.size] expression
818 printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc);
822 // This gets kind of stupid. This directive is disallowed in normal 68000
823 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
824 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
826 if (as68_flag == 0 && (scattr & SBSS) == 0)
827 return error(".ds permitted only in BSS");
829 if (siz != SIZB && (sloc & 1)) // Automatic .even
832 if (abs_expr(&eval) != OK)
835 // In non-TDB section (BSS, ABS and M6502) just advance the location
836 // counter appropriately. In TDB sections, deposit (possibly large) chunks
837 //of zeroed memory....
843 just_bss = 1; // No data deposited (8-bit CPU mode)
847 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED | ABS), NULL);
856 // dc.b, dc.w / dc, dc.l
866 int movei = 0; // movei flag for dc.i
868 if ((scattr & SBSS) != 0)
869 return error("illegal initialization of section");
871 // Do an auto_even if it's not BYTE sized (hmm, should we be doing this???)
872 if ((siz != SIZB) && (sloc & 1))
875 // Check to see if we're trying to set LONGS on a non 32-bit aligned
876 // address in a GPU or DSP section, in their local RAM
877 if ((siz == SIZL) && (orgaddr & 0x03)
878 && ((rgpu && (orgaddr >= 0xF03000) && (orgaddr <= 0xF03FFFF))
879 || (rdsp && (orgaddr >= 0xF1B000) && (orgaddr <= 0xF1CFFFF))))
880 warn("depositing LONGs on a non-long address in local RAM");
884 // dc.b 'string' [,] ...
885 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
887 i = strlen(string[tok[1]]);
889 if ((challoc - ch_size) < i)
892 for(p=string[tok[1]]; *p!=EOS; ++p)
907 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
910 tdb = (WORD)(eattr & TDB);
911 defined = (WORD)(eattr & DEFINED);
913 if ((challoc - ch_size) < 4)
921 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
927 return error("non-absolute byte value");
929 if (eval + 0x100 >= 0x200)
931 sprintf(buffer, "%s (value = $%X)", range_error, eval);
932 return error(buffer);
943 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
949 rmark(cursect, sloc, tdb, MWORD, NULL);
951 if (eval + 0x10000 >= 0x20000)
952 return error(range_error);
954 // Deposit 68000 or 6502 (byte-reversed) word
963 AddFixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
965 AddFixup(FU_LONG, sloc, exprbuf);
972 rmark(cursect, sloc, tdb, MLONG, NULL);
975 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
993 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1000 printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr);
1001 if ((scattr & SBSS) != 0)
1002 return error("illegal initialization of section");
1004 if (abs_expr(&evalc) != OK)
1008 return error("missing comma");
1010 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1013 if ((siz != SIZB) && (sloc & 1))
1016 dep_block(evalc, siz, eval, eattr, exprbuf);
1022 // Generalized initialization directive
1024 // .init[.siz] [#count,] expression [.size] , ...
1026 // The size suffix on the ".init" directive becomes the default size of the
1027 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1028 // and an expression, it specifies a repeat count. The value to be deposited
1029 // may be followed by a size suffix, which overrides the default size.
1031 int d_init(WORD def_siz)
1038 if ((scattr & SBSS) != 0)
1039 return error(".init not permitted in BSS or ABS");
1042 return error("directive forbidden in gpu/dsp mode");
1046 // Get repeat count (defaults to 1)
1051 if (abs_expr(&count) != OK)
1055 return error(comma_error);
1060 // Evaluate expression to deposit
1061 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1064 switch ((int)*tok++)
1065 { // Determine size of object to deposit
1066 case DOTB: siz = SIZB; break;
1067 case DOTW: siz = SIZB; break;
1068 case DOTL: siz = SIZL; break;
1075 dep_block(count, siz, eval, eattr, exprbuf);
1085 return error(comma_error);
1092 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1094 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1099 tdb = (WORD)(eattr & TDB);
1100 defined = (WORD)(eattr & DEFINED);
1104 if ((challoc - ch_size) < 4)
1112 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1118 return error("non-absolute byte value");
1120 if (eval + 0x100 >= 0x200)
1121 return error(range_error);
1131 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1137 rmark(cursect, sloc, tdb, MWORD, NULL);
1139 if (eval + 0x10000 >= 0x20000)
1140 return error(range_error);
1142 // Deposit 68000 or 6502 (byte-reversed) word
1150 AddFixup(FU_LONG, sloc, exprbuf);
1156 rmark(cursect, sloc, tdb, MLONG, NULL);
1170 // .comm symbol, size
1179 return error("missing symbol");
1184 if (*p == '.') // Cannot .comm a local symbol
1185 return error(locgl_error);
1187 if ((sym = lookup(p, LABEL, 0)) == NULL)
1188 sym = NewSymbol(p, LABEL, 0);
1191 if (sym->sattr & DEFINED)
1192 return error(".comm symbol already defined");
1195 sym->sattr = GLOBAL | COMMON | BSS;
1198 return error(comma_error);
1200 if (abs_expr(&eval) != OK) // Parse size of common region
1203 sym->svalue = eval; // Install common symbol's size
1210 // .list - Turn listing on
1222 // .nlist - Turn listing off
1234 // .68000 - Back to 68000 TEXT segment
1239 // Switching from gpu/dsp sections should reset any ORG'd Address
1243 SwitchSection(TEXT);
1249 // .gpu - Switch to GPU assembler
1253 if ((cursect != TEXT) && (cursect != DATA))
1255 error(".gpu can only be used in the TEXT or DATA segments");
1259 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1262 //printf("Resetting ORG...\n");
1266 //else printf("NOT resetting ORG!\n");
1268 rgpu = 1; // Set GPU assembly
1269 rdsp = 0; // Unset DSP assembly
1270 regbank = BANK_N; // Set no default register bank
1276 // .dsp - Switch to DSP assembler
1280 if ((cursect != TEXT) && (cursect != DATA))
1282 error(".dsp can only be used in the TEXT or DATA segments");
1286 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1293 rdsp = 1; // Set DSP assembly
1294 rgpu = 0; // Unset GPU assembly
1295 regbank = BANK_N; // Set no default register bank
1301 // .cargs [#offset], symbol[.size], ...
1303 // Lists of registers may also be mentioned; they just take up space. Good for
1304 // "documentation" purposes:
1306 // .cargs a6, .arg1, .arg2, .arg3...
1308 // Symbols thus created are ABS and EQUATED.
1312 VALUE eval = 4; // Default to 4 if no offset specified (to account for
1321 return error("directive forbidden in gpu/dsp mode");
1327 if (abs_expr(&eval) != OK)
1330 // Eat the comma, if it's there
1341 // Set env to either local (dot prefixed) or global scope
1342 env = (*p == '.' ? curenv : 0);
1343 symbol = lookup(p, LABEL, env);
1347 symbol = NewSymbol(p, LABEL, env);
1350 else if (symbol->sattr & DEFINED)
1351 return errors("multiply-defined label '%s'", p);
1353 // Put symbol in "order of definition" list
1354 AddToSymbolDeclarationList(symbol);
1356 symbol->sattr |= (ABS | DEFINED | EQUATED);
1357 symbol->svalue = eval;
1360 // What this does is eat any dot suffixes attached to a symbol. If
1361 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1362 // there is no dot suffix, it assumes a size of 2.
1374 else if (*tok >= KW_D0 && *tok <= KW_A7)
1376 if (reglist(&rlist) < 0)
1379 for(i=0; i<16; i++, rlist>>=1)
1402 return error(".cargs syntax");
1406 // Eat commas in between each argument, if they exist
1414 // .cstruct [#offset], symbol[.size], ...
1416 // Lists of registers may also be mentioned; they just take up space. Good for
1417 // "documentation" purposes:
1419 // .cstruct a6, .arg1, .arg2, .arg3...
1421 // Symbols thus created are ABS and EQUATED. Note that this is for
1422 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1427 VALUE eval = 0; // Default, if no offset specified, is zero
1435 return error("directive forbidden in gpu/dsp mode");
1441 if (abs_expr(&eval) != OK)
1444 // Eat the comma, if it's there
1453 symbolName = string[tok[1]];
1455 // Set env to either local (dot prefixed) or global scope
1456 env = (symbolName[0] == '.' ? curenv : 0);
1457 symbol = lookup(symbolName, LABEL, env);
1459 // If the symbol wasn't found, then define it. Otherwise, throw an
1463 symbol = NewSymbol(symbolName, LABEL, env);
1466 else if (symbol->sattr & DEFINED)
1467 return errors("multiply-defined label '%s'", symbolName);
1469 // Put symbol in "order of definition" list
1470 AddToSymbolDeclarationList(symbol);
1474 // Adjust label start address if it's a word or a long, as a byte
1475 // label might have left us on an odd address.
1480 eval += eval & 0x01;
1483 symbol->sattr |= (ABS | DEFINED | EQUATED);
1484 symbol->svalue = eval;
1486 // Check for dot suffixes and adjust space accordingly (longs and
1487 // words on an odd boundary get bumped to the next word aligned
1488 // address). If no suffix, then throw an error.
1501 return error("Symbol missing dot suffix in .cstruct construct");
1506 else if (*tok >= KW_D0 && *tok <= KW_A7)
1508 if (reglist(&rlist) < 0)
1511 for(i=0; i<16; i++, rlist>>=1)
1534 return error(".cstruct syntax");
1538 // Eat commas in between each argument, if they exist
1546 // Undefine a macro - .undefmac macname [, macname...]
1548 int undmac1(char * p)
1550 SYM * symbol = lookup(p, MACRO, 0);
1552 // If the macro symbol exists, cause it to disappear
1553 // if ((sy = lookup(p, MACRO, 0)) != NULL)
1555 symbol->stype = (BYTE)SY_UNDEF;
1570 warn("JPAD directive is deprecated/non-functional");
1577 warn("NOJPAD directive is deprecated/non-functional");
1584 return error("What the hell? Do you think we adhere to the Goof standard?");