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
30 // Directive handler table
46 d_undmac, // 14 undefmac
53 d_incbin, // 21 .incbin //load
54 d_unimpl, // 22 disable
55 d_unimpl, // 23 enable
57 d_regbank0, // 25 .regbank0
58 d_regbank1, // 26 .regbank1
60 d_assert, // 28 assert
62 d_unimpl, // 30* endif
65 d_include, // 33 include
67 d_unimpl, // 35* macro
68 ExitMacro, // 36* exitm
74 d_dphrase, // 42 struct
77 d_subttl, // 45 subttl
81 d_noclear, // 49 .noclear
82 d_equrundef, // 50 .equrundef/.regundef
83 d_ccundef, // 51 .ccundef
85 d_cstruct, // 53 .cstruct
86 d_jpad, // 54 .jpad (deprecated)
87 d_nojpad, // 55 .nojpad (deprecated)
88 d_gpumain, // 56 .gpumain (deprecated)
100 return error(".org permitted only in gpu/dsp section");
104 if (abs_expr(&address) == ERROR)
106 error("cannot determine org'd address");
112 //printf("ORG: address = $%08X...\n", orgaddr);
123 char prntstr[LNSIZ]; // String for PRINT directive
124 char format[LNSIZ]; // Format for PRINT directive
125 int formatting = 0; // Formatting on/off
126 int wordlong = 0; // WORD = 0, LONG = 1
127 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
129 VALUE eval; // Expression value
130 WORD eattr; // Expression attributes
131 SYM * esym; // External symbol involved in expr.
132 TOKEN r_expr[EXPRSIZE];
139 // sprintf(prntstr, "%s", (char *)tok[1]);
140 sprintf(prntstr, "%s", string[tok[1]]);
141 printf("%s", prntstr);
144 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
151 if (tok[1] != SYMBOL)
154 // strcpy(prntstr, (char *)tok[2]);
155 strcpy(prntstr, string[tok[2]]);
159 case 'l': case 'L': wordlong = 1; break;
160 case 'w': case 'W': wordlong = 0; break;
161 case 'x': case 'X': outtype = 0; break;
162 case 'd': case 'D': outtype = 1; break;
163 case 'u': case 'U': outtype = 2; break;
165 error("unknown print format flag");
175 if (expr(r_expr, &eval, &eattr, &esym) != OK)
181 case 0: strcpy(format, "%X"); break;
182 case 1: strcpy(format, "%d" ); break;
183 case 2: strcpy(format, "%u" ); break;
187 sprintf(prntstr, format, eval);
189 sprintf(prntstr, format, eval & 0xFFFF);
191 printf("%s", prntstr);
194 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
211 error("illegal print token");
217 // Undefine an equated condition code
223 // Check that we are in a RISC section
226 error(".ccundef must be defined in .gpu/.dsp section");
232 // error(syntax_error);
233 error("syntax error; expected symbol");
237 // ccname = lookup((char *)tok[1], LABEL, 0);
238 ccname = lookup(string[tok[1]], LABEL, 0);
240 // Make sure symbol is a valid ccdef
241 if (!ccname || !(ccname->sattre & EQUATEDCC))
243 error("invalid equated condition name specified");
247 ccname->sattre |= UNDEF_CC;
254 // Undefine an equated register
256 int d_equrundef(void)
260 // Check that we are in a RISC section
262 return error(".equrundef/.regundef must be defined in .gpu/.dsp section");
266 // Skip preceeding or seperating commas (if any)
270 // Check we are dealing with a symbol
272 return error("syntax error; expected symbol");
274 // Lookup and undef if equated register
275 regname = lookup(string[tok[1]], LABEL, 0);
277 if (regname && (regname->sattre & EQUATEDREG))
279 // Reset the attributes of this symbol...
281 regname->sattre &= ~(EQUATEDREG | BANK_0 | BANK_1);
282 regname->sattre |= UNDEF_EQUR;
285 // Skip over symbol token and address
294 // Do not allow use of the CLR.L opcode
303 // Include binary file
309 long pos, size, bytesRead;
312 // Check to see if we're in BSS, and, if so, throw an error
315 errors("cannot include binary file \"%s\" in BSS section", string[tok[1]]);
321 error("syntax error; string missing");
325 if ((fd = open(string[tok[1]], _OPEN_INC)) < 0)
327 errors("cannot open include binary file (%s)", string[tok[1]]);
331 size = lseek(fd, 0L, SEEK_END);
332 pos = lseek(fd, 0L, SEEK_SET);
335 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size); }
337 char * fileBuffer = (char *)malloc(size);
338 bytesRead = read(fd, fileBuffer, size);
340 if (bytesRead != size)
342 sprintf(msg, "was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[tok[1]], size);
347 memcpy(chptr, fileBuffer, size);
362 // Set RISC register banks
366 // Set active register bank zero
374 // Set active register bank one
381 // Helper function, to cut down on mistakes & typing
383 static inline void SkipBytes(unsigned bytesToSkip)
388 if ((scattr & SBSS) == 0)
390 chcheck(bytesToSkip);
391 D_ZEROFILL(bytesToSkip);
398 orgaddr += bytesToSkip;
404 // Adjust location to an EVEN value
408 unsigned skip = (rgpu || rdsp ? orgaddr : sloc) & 0x01;
413 if ((scattr & SBSS) == 0)
432 // Adjust location to a LONG value
436 unsigned lower2Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x03;
437 unsigned bytesToSkip = (0x04 - lower2Bits) & 0x03;
438 SkipBytes(bytesToSkip);
445 // Adjust location to a PHRASE value
447 // N.B.: We have to handle the GPU/DSP cases separately because you can embed
448 // RISC code in the middle of a regular 68K section. Also note that all
449 // of the alignment pseudo-ops will have to be fixed this way.
451 // This *must* behave differently when in a RISC section, as following sloc
452 // (instead of orgaddr) will fuck things up royally. Note that we do it this
453 // way because you can embed RISC code in a 68K section, and have the origin
454 // pointing to a different alignment in the RISC section than the 68K section.
458 unsigned lower3Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x07;
459 unsigned bytesToSkip = (0x08 - lower3Bits) & 0x07;
460 SkipBytes(bytesToSkip);
467 // Adjust location to a DPHRASE value
471 unsigned lower4Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x0F;
472 unsigned bytesToSkip = (0x10 - lower4Bits) & 0x0F;
473 SkipBytes(bytesToSkip);
480 // Adjust location to a QPHRASE value
484 unsigned lower5Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x1F;
485 unsigned bytesToSkip = (0x20 - lower5Bits) & 0x1F;
486 SkipBytes(bytesToSkip);
493 // Do auto-even. This must be called ONLY if 'sloc' is odd.
495 // This is made hairy because, if there was a label on the line, we also have
496 // to adjust its value. This won't work with more than one label on the line,
497 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
498 // the C compiler is VERY paranoid and uses ".even" whenever it can
500 // N.B.: This probably needs the same fixes as above...
505 sloc++; // Bump BSS section
507 D_byte(0); // Deposit 0.b in non-BSS
509 if (lab_sym != NULL) // Bump label if we have to
515 // Unimplemened directive error
519 return error("unimplemented directive");
524 // Return absolute (not TDB) and defined expression or return an error
526 int abs_expr(VALUE * a_eval)
530 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
533 if (!(eattr & DEFINED))
534 return error(undef_error);
537 return error(rel_error);
544 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
546 int symlist(int(* func)())
548 const char * em = "symbol list syntax";
555 if ((*func)(string[tok[1]]) != OK)
574 // .include "filename"
584 if (*tok == STRING) // Leave strings ALONE
590 else if (*tok == SYMBOL) // Try to append ".s" to symbols
593 strcpy(buf, (char *)*++tok);
595 strcpy(buf, string[*++tok]);
600 else // Punt if no STRING or SYMBOL
601 return error("missing filename");
603 // Make sure the user didn't try anything like:
604 // .include equates.s
606 return error("extra stuff after filename -- enclose it in quotes");
608 // Attempt to open the include file in the current directory, then (if that
609 // failed) try list of include files passed in the enviroment string or by
611 if ((j = open(fn, 0)) < 0)
613 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
617 if (j > 0 && buf1[j - 1] != SLASHCHAR) // Append path char if necessary
618 strcat(buf1, SLASHSTRING);
622 if ((j = open(buf1, 0)) >= 0)
626 return errors("cannot open: \"%s\"", fn);
636 // .assert expression [, expression...]
643 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
645 if (!(eattr & DEFINED))
646 return error("forward or undefined .assert");
649 return error("assert failure");
661 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
668 return error("cannot .globl local symbol");
670 if ((sy = lookup(p, LABEL, 0)) == NULL)
672 sy = NewSymbol(p, LABEL, 0);
675 //printf("glob1: Making global symbol: attr=%04X, eattr=%08X, %s\n", sy->sattr, sy->sattre, sy->sname);
702 else if (abs_expr(&eval) != OK)
717 return error("directive forbidden in gpu/dsp mode");
732 return error("directive forbidden in gpu/dsp mode");
747 return error("directive forbidden in gpu/dsp mode");
760 // .ds[.size] expression
765 printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc);
769 // This gets kind of stupid. This directive is disallowed in normal 68000
770 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
771 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
773 if (as68_flag == 0 && (scattr & SBSS) == 0)
774 return error(".ds permitted only in BSS");
776 if (siz != SIZB && (sloc & 1)) // Automatic .even
779 if (abs_expr(&eval) != OK)
782 // In non-TDB section (BSS, ABS and M6502) just advance the location
783 // counter appropriately. In TDB sections, deposit (possibly large) chunks
784 //of zeroed memory....
790 just_bss = 1; // No data deposited (8-bit CPU mode)
794 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED | ABS), NULL);
803 // dc.b, dc.w / dc, dc.l
813 int movei = 0; // movei flag for dc.i
815 if ((scattr & SBSS) != 0)
816 return error("illegal initialization of section");
818 if ((siz != SIZB) && (sloc & 1))
823 // dc.b 'string' [,] ...
824 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
826 // i = strlen((const char*)tok[1]);
827 i = strlen(string[tok[1]]);
829 if ((challoc - ch_size) < i)
832 for(p=string[tok[1]]; *p!=EOS; ++p)
847 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
850 tdb = (WORD)(eattr & TDB);
851 defined = (WORD)(eattr & DEFINED);
853 if ((challoc - ch_size) < 4)
861 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
867 return error("non-absolute byte value");
869 if (eval + 0x100 >= 0x200)
870 return error(range_error);
880 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
886 rmark(cursect, sloc, tdb, MWORD, NULL);
888 if (eval + 0x10000 >= 0x20000)
889 return error(range_error);
891 // Deposit 68000 or 6502 (byte-reversed) word
900 AddFixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
902 AddFixup(FU_LONG, sloc, exprbuf);
909 rmark(cursect, sloc, tdb, MLONG, NULL);
912 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
930 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
937 if ((scattr & SBSS) != 0)
938 return error("illegal initialization of section");
940 if (abs_expr(&evalc) != OK)
944 return error("missing comma");
946 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
949 if ((siz != SIZB) && (sloc & 1))
952 dep_block(evalc, siz, eval, eattr, exprbuf);
958 // Generalized initialization directive
960 // .init[.siz] [#count,] expression [.size] , ...
962 // The size suffix on the ".init" directive becomes the default size of the
963 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
964 // and an expression, it specifies a repeat count. The value to be deposited
965 // may be followed by a size suffix, which overrides the default size.
967 int d_init(WORD def_siz)
974 if ((scattr & SBSS) != 0)
975 return error(".init not permitted in BSS or ABS");
978 return error("directive forbidden in gpu/dsp mode");
982 // Get repeat count (defaults to 1)
987 if (abs_expr(&count) != OK)
991 return error(comma_error);
996 // Evaluate expression to deposit
997 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1000 switch ((int)*tok++)
1001 { // Determine size of object to deposit
1002 case DOTB: siz = SIZB; break;
1003 case DOTW: siz = SIZB; break;
1004 case DOTL: siz = SIZL; break;
1011 dep_block(count, siz, eval, eattr, exprbuf);
1021 return error(comma_error);
1028 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1030 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1035 tdb = (WORD)(eattr & TDB);
1036 defined = (WORD)(eattr & DEFINED);
1040 if ((challoc - ch_size) < 4)
1048 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1054 return error("non-absolute byte value");
1056 if (eval + 0x100 >= 0x200)
1057 return error(range_error);
1067 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1073 rmark(cursect, sloc, tdb, MWORD, NULL);
1075 if (eval + 0x10000 >= 0x20000)
1076 return error(range_error);
1078 // Deposit 68000 or 6502 (byte-reversed) word
1086 AddFixup(FU_LONG, sloc, exprbuf);
1092 rmark(cursect, sloc, tdb, MLONG, NULL);
1106 // .comm symbol, size
1115 return error("missing symbol");
1117 // p = (char *)tok[1];
1121 if (*p == '.') // Cannot .comm a local symbol
1122 return error(locgl_error);
1124 if ((sym = lookup(p, LABEL, 0)) == NULL)
1125 sym = NewSymbol(p, LABEL, 0);
1128 if (sym->sattr & DEFINED)
1129 return error(".comm symbol already defined");
1132 sym->sattr = GLOBAL | COMMON | BSS;
1135 return error(comma_error);
1137 if (abs_expr(&eval) != OK) // Parse size of common region
1140 sym->svalue = eval; // Install common symbol's size
1147 // .list - Turn listing on
1159 // .nlist - Turn listing off
1171 // .68000 - Back to 68000 TEXT segment
1176 // Switching from gpu/dsp sections should reset any ORG'd Address
1180 SwitchSection(TEXT);
1186 // .gpu - Switch to GPU assembler
1190 if ((cursect != TEXT) && (cursect != DATA))
1192 error(".gpu can only be used in the TEXT or DATA segments");
1196 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1199 //printf("Resetting ORG...\n");
1203 //else printf("NOT resetting ORG!\n");
1205 rgpu = 1; // Set GPU assembly
1206 rdsp = 0; // Unset DSP assembly
1207 regbank = BANK_N; // Set no default register bank
1213 // .dsp - Switch to DSP assembler
1217 if ((cursect != TEXT) && (cursect != DATA))
1219 error(".dsp can only be used in the TEXT or DATA segments");
1223 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1230 rdsp = 1; // Set DSP assembly
1231 rgpu = 0; // Unset GPU assembly
1232 regbank = BANK_N; // Set no default register bank
1238 // .cargs [#offset], symbol[.size], ...
1240 // Lists of registers may also be mentioned; they just take up space. Good for
1241 // "documentation" purposes:
1243 // .cargs a6, .arg1, .arg2, .arg3...
1245 // Symbols thus created are ABS and EQUATED.
1249 VALUE eval = 4; // Default to 4 if no offset specified (to account for
1258 return error("directive forbidden in gpu/dsp mode");
1264 if (abs_expr(&eval) != OK)
1267 // Eat the comma, if it's there
1276 // p = (char *)tok[1];
1281 env = curenv; // Label is local
1283 env = 0; // Label is global
1285 // Set env to either local (dot prefixed) or global scope
1286 env = (*p == '.' ? curenv : 0);
1288 symbol = lookup(p, LABEL, env);
1292 symbol = NewSymbol(p, LABEL, env);
1295 else if (symbol->sattr & DEFINED)
1296 return errors("multiply-defined label '%s'", p);
1298 // Put symbol in "order of definition" list
1299 if (!(symbol->sattr & SDECLLIST))
1300 AddToSymbolOrderList(symbol);
1302 symbol->sattr |= (ABS | DEFINED | EQUATED);
1303 symbol->svalue = eval;
1306 // What this does is eat any dot suffixes attached to a symbol. If
1307 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1308 // there is no dot suffix, it assumes a size of 2.
1320 else if (*tok >= KW_D0 && *tok <= KW_A7)
1322 if (reglist(&rlist) < 0)
1325 // for(i=0; i++<16; rlist>>=1)
1326 for(i=0; i<16; i++, rlist>>=1)
1349 return error(".cargs syntax");
1353 // Eat commas in between each argument, if they exist
1361 // .cstruct [#offset], symbol[.size], ...
1363 // Lists of registers may also be mentioned; they just take up space. Good for
1364 // "documentation" purposes:
1366 // .cstruct a6, .arg1, .arg2, .arg3...
1368 // Symbols thus created are ABS and EQUATED. Note that this is for
1369 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1374 VALUE eval = 0; // Default, if no offset specified, is zero
1382 return error("directive forbidden in gpu/dsp mode");
1388 if (abs_expr(&eval) != OK)
1391 // Eat the comma, if it's there
1400 symbolName = string[tok[1]];
1402 // Set env to either local (dot prefixed) or global scope
1403 env = (symbolName[0] == '.' ? curenv : 0);
1404 symbol = lookup(symbolName, LABEL, env);
1406 // If the symbol wasn't found, then define it. Otherwise, throw an
1410 symbol = NewSymbol(symbolName, LABEL, env);
1413 else if (symbol->sattr & DEFINED)
1414 return errors("multiply-defined label '%s'", symbolName);
1416 // Put symbol in "order of definition" list
1417 if (!(symbol->sattr & SDECLLIST))
1418 AddToSymbolOrderList(symbol);
1422 // Adjust label start address if it's a word or a long, as a byte
1423 // label might have left us on an odd address.
1428 eval += eval & 0x01;
1431 symbol->sattr |= (ABS | DEFINED | EQUATED);
1432 symbol->svalue = eval;
1434 // Check for dot suffixes and adjust space accordingly (longs and
1435 // words on an odd boundary get bumped to the next word aligned
1436 // address). If no suffix, then throw an error.
1449 return error("Symbol missing dot suffix in .cstruct construct");
1454 else if (*tok >= KW_D0 && *tok <= KW_A7)
1456 if (reglist(&rlist) < 0)
1459 for(i=0; i<16; i++, rlist>>=1)
1482 return error(".cstruct syntax");
1486 // Eat commas in between each argument, if they exist
1494 // Undefine a macro - .undefmac macname [, macname...]
1496 int undmac1(char * p)
1498 SYM * symbol = lookup(p, MACRO, 0);
1500 // If the macro symbol exists, cause it to disappear
1501 // if ((sy = lookup(p, MACRO, 0)) != NULL)
1503 symbol->stype = (BYTE)SY_UNDEF;
1518 warn("JPAD directive is deprecated/non-functional");
1525 warn("NOJPAD directive is deprecated/non-functional");
1532 return error("What the hell? Do you think we adhere to the Goof standard?");