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
97 return error(".org permitted only in gpu/dsp section");
101 if (abs_expr(&address) == ERROR)
103 error("cannot determine org'd address");
119 char prntstr[LNSIZ]; // String for PRINT directive
120 char format[LNSIZ]; // Format for PRINT directive
121 int formatting = 0; // Formatting on/off
122 int wordlong = 0; // WORD = 0, LONG = 1
123 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
125 VALUE eval; // Expression value
126 WORD eattr; // Expression attributes
127 SYM * esym; // External symbol involved in expr.
128 TOKEN r_expr[EXPRSIZE];
135 // sprintf(prntstr, "%s", (char *)tok[1]);
136 sprintf(prntstr, "%s", string[tok[1]]);
137 printf("%s", prntstr);
140 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
147 if (tok[1] != SYMBOL)
150 // strcpy(prntstr, (char *)tok[2]);
151 strcpy(prntstr, string[tok[2]]);
155 case 'l': case 'L': wordlong = 1; break;
156 case 'w': case 'W': wordlong = 0; break;
157 case 'x': case 'X': outtype = 0; break;
158 case 'd': case 'D': outtype = 1; break;
159 case 'u': case 'U': outtype = 2; break;
161 error("unknown print format flag");
171 if (expr(r_expr, &eval, &eattr, &esym) != OK)
177 case 0: strcpy(format, "%X"); break;
178 case 1: strcpy(format, "%d" ); break;
179 case 2: strcpy(format, "%u" ); break;
183 sprintf(prntstr, format, eval);
185 sprintf(prntstr, format, eval & 0xFFFF);
187 printf("%s", prntstr);
190 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
207 error("illegal print token");
213 // Undefine an Equated Condition Code
219 // Check that we are in a RISC section
222 error(".ccundef must be defined in .gpu/.dsp section");
232 // ccname = lookup((char *)tok[1], LABEL, 0);
233 ccname = lookup(string[tok[1]], LABEL, 0);
235 // Make sure symbol is a valid ccdef
236 if (!ccname || !(ccname->sattre & EQUATEDCC))
238 error("invalid equated condition name specified");
242 ccname->sattre |= UNDEF_CC;
249 // Undefine an Equated Register
251 int d_equrundef(void)
255 // Check that we are in a RISC section
258 error(".equrundef/.regundef must be defined in .gpu/.dsp section");
264 // Skip preceeding or seperating commas (if any)
268 // Check we are dealing with a symbol
275 // Lookup and undef if equated register
276 // regname = lookup((char *)tok[1], LABEL, 0);
277 regname = lookup(string[tok[1]], LABEL, 0);
279 if (regname && (regname->sattre & EQUATEDREG))
280 regname->sattre |= UNDEF_EQUR;
282 // Skip over symbol token and address
291 // Do Not Allow the Use of the CLR.L Opcode
300 // Include Binary File
306 long pos, size, bytesRead;
309 // Check to see if we're in BSS, and, if so, throw an error
312 errors("cannot include binary file \"%s\" in BSS section", string[tok[1]]);
322 if ((fd = open(string[tok[1]], _OPEN_INC)) < 0)
324 errors("cannot open include binary file (%s)", string[tok[1]]);
328 size = lseek(fd, 0L, SEEK_END);
329 pos = lseek(fd, 0L, SEEK_SET);
332 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size); }
334 char * fileBuffer = (char *)malloc(size);
335 bytesRead = read(fd, fileBuffer, size);
337 if (bytesRead != size)
339 sprintf(msg, "was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[tok[1]], size);
344 //#define D_byte(b) {*chptr++=(char)b; ++sloc; ++ch_size; if(orgactive) ++orgaddr;}
345 memcpy(chptr, fileBuffer, size);
360 // Set RISC Register Banks
364 // Set active register bank zero
372 // Set active register bank one
379 // Adjust Location to an EVEN Value
385 if ((scattr & SBSS) == 0)
401 // Adjust Location to an LONG Value
408 i = sloc & ~(val - 1);
411 val = val - (sloc - i);
417 if ((scattr & SBSS) == 0)
435 // Adjust Location to an PHRASE Value
442 i = sloc & ~(val - 1);
445 val = val - (sloc - i);
451 if ((scattr & SBSS) == 0)
469 // Adjust Location to an DPHRASE Value
476 i = sloc & ~(val - 1);
479 val = val - (sloc - i);
485 if ((scattr & SBSS) == 0)
503 // Adjust Location to an QPHRASE Value
510 i = sloc & ~(val - 1);
513 val = val - (sloc - i);
519 if ((scattr & SBSS) == 0)
538 // Do auto-even. This must be called ONLY if 'sloc' is odd.
540 // This is made hairy because, if there was a label on the line, we also have
541 // to adjust its value. This won't work with more than one label on the line,
542 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
543 // the C compiler is VERY paranoid and uses ".even" whenever it can
548 ++sloc; // Bump BSS section
550 D_byte(0) // Deposit 0.b in non-BSS
552 if (lab_sym != NULL) // Bump label if we have to
558 // Unimplemened Directive Error
562 return error("unimplemented directive");
567 // Return absolute (not TDB) and defined expression or return an error
569 int abs_expr(VALUE * a_eval)
573 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
576 if (!(eattr & DEFINED))
577 return error(undef_error);
580 return error(rel_error);
587 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
589 int symlist(int(* func)())
591 char * em = "symbol list syntax";
599 if ((*func)(tok[1]) != OK)
601 if ((*func)(string[tok[1]]) != OK)
621 // .include "filename"
631 if (*tok == STRING) // Leave strings ALONE
637 else if (*tok == SYMBOL) // Try to append ".s" to symbols
640 strcpy(buf, (char *)*++tok);
642 strcpy(buf, string[*++tok]);
647 else // Punt if no STRING or SYMBOL
648 return error("missing filename");
650 // Make sure the user didn't try anything like:
651 // .include equates.s
653 return error("extra stuff after filename -- enclose it in quotes");
655 // Attempt to open the include file in the current directory, then (if that
656 // failed) try list of include files passed in the enviroment string or by
658 if ((j = open(fn, 0)) < 0)
660 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
664 if (j > 0 && buf1[j - 1] != SLASHCHAR) // Append path char if necessary
665 strcat(buf1, SLASHSTRING);
669 if ((j = open(buf1, 0)) >= 0)
673 return errors("cannot open: \"%s\"", fn);
683 // .assert expression [, expression...]
690 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
692 if (!(eattr & DEFINED))
693 return error("forward or undefined .assert");
696 return error("assert failure");
708 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
715 return error("cannot .globl local symbol");
717 if ((sy = lookup(p, LABEL, 0)) == NULL)
719 sy = NewSymbol(p, LABEL, 0);
748 else if (abs_expr(&eval) != OK)
764 return error("directive forbidden in gpu/dsp mode");
779 return error("directive forbidden in gpu/dsp mode");
794 return error("directive forbidden in gpu/dsp mode");
807 // .ds[.size] expression
812 printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc);
816 // This gets kind of stupid. This directive is disallowed in normal 68000
817 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
818 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
820 if (as68_flag == 0 && (scattr & SBSS) == 0)
821 return error(".ds permitted only in BSS");
823 if (siz != SIZB && (sloc & 1)) // Automatic .even
826 if (abs_expr(&eval) != OK)
829 // In non-TDB section (BSS, ABS and M6502) just advance the location
830 // counter appropriately. In TDB sections, deposit (possibly large) chunks
831 //of zeroed memory....
837 just_bss = 1; // No data deposited (8-bit CPU mode)
841 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED | ABS), NULL);
850 // dc.b, dc.w / dc, dc.l
860 int movei = 0; // movei flag for dc.i
862 if ((scattr & SBSS) != 0)
863 return error("illegal initialization of section");
865 if ((siz != SIZB) && (sloc & 1))
870 // dc.b 'string' [,] ...
871 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
873 // i = strlen((const char*)tok[1]);
874 i = strlen(string[tok[1]]);
876 if ((challoc - ch_size) < i)
879 for(p=string[tok[1]]; *p!=EOS; ++p)
894 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
897 tdb = (WORD)(eattr & TDB);
898 defined = (WORD)(eattr & DEFINED);
900 if ((challoc - ch_size) < 4)
908 fixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
914 return error("non-absolute byte value");
916 if (eval + 0x100 >= 0x200)
917 return error(range_error);
927 fixup(FU_WORD | FU_SEXT, sloc, exprbuf);
933 rmark(cursect, sloc, tdb, MWORD, NULL);
935 if (eval + 0x10000 >= 0x20000)
936 return error(range_error);
938 // Deposit 68000 or 6502 (byte-reversed) word
947 fixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
949 fixup(FU_LONG, sloc, exprbuf);
956 rmark(cursect, sloc, tdb, MLONG, NULL);
959 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
977 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
984 if ((scattr & SBSS) != 0)
985 return error("illegal initialization of section");
987 if (abs_expr(&evalc) != OK)
991 return error("missing comma");
993 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
996 if ((siz != SIZB) && (sloc & 1))
999 dep_block(evalc, siz, eval, eattr, exprbuf);
1005 // Generalized initialization directive
1007 // .init[.siz] [#count,] expression [.size] , ...
1009 // The size suffix on the ".init" directive becomes the default size of the
1010 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1011 // and an expression, it specifies a repeat count. The value to be deposited
1012 // may be followed by a size suffix, which overrides the default size.
1014 int d_init(WORD def_siz)
1021 if ((scattr & SBSS) != 0)
1022 return error(".init not permitted in BSS or ABS");
1025 return error("directive forbidden in gpu/dsp mode");
1029 // Get repeat count (defaults to 1)
1034 if (abs_expr(&count) != OK)
1038 return error(comma_error);
1043 // Evaluate expression to deposit
1044 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1047 switch ((int)*tok++)
1048 { // Determine size of object to deposit
1049 case DOTB: siz = SIZB; break;
1050 case DOTW: siz = SIZB; break;
1051 case DOTL: siz = SIZL; break;
1058 dep_block(count, siz, eval, eattr, exprbuf);
1068 return error(comma_error);
1075 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1077 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1082 tdb = (WORD)(eattr & TDB);
1083 defined = (WORD)(eattr & DEFINED);
1087 if ((challoc - ch_size) < 4)
1095 fixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1101 return error("non-absolute byte value");
1103 if (eval + 0x100 >= 0x200)
1104 return error(range_error);
1114 fixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1120 rmark(cursect, sloc, tdb, MWORD, NULL);
1122 if (eval + 0x10000 >= 0x20000)
1123 return error(range_error);
1125 // Deposit 68000 or 6502 (byte-reversed) word
1133 fixup(FU_LONG, sloc, exprbuf);
1139 rmark(cursect, sloc, tdb, MLONG, NULL);
1153 // .comm symbol, size
1162 return error("missing symbol");
1164 // p = (char *)tok[1];
1168 if (*p == '.') // Cannot .comm a local symbol
1169 return error(locgl_error);
1171 if ((sym = lookup(p, LABEL, 0)) == NULL)
1172 sym = NewSymbol(p, LABEL, 0);
1175 if (sym->sattr & DEFINED)
1176 return error(".comm symbol already defined");
1179 sym->sattr = GLOBAL | COMMON | BSS;
1182 return error(comma_error);
1184 if (abs_expr(&eval) != OK) // Parse size of common region
1187 sym->svalue = eval; // Install common symbol's size
1194 // .list - Turn listing on
1206 // .nlist - Turn listing off
1218 // .68000 - Back to 68000 TEXT segment
1223 // Switching from gpu/dsp sections should reset any ORG'd Address
1233 // .gpu - Switch to GPU Assembler
1237 if ((cursect != TEXT) && (cursect != DATA))
1239 error(".gpu can only be used in the TEXT or DATA segments");
1243 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1250 rgpu = 1; // Set GPU assembly
1251 rdsp = 0; // Unset DSP assembly
1252 regbank = BANK_N; // Set no default register bank
1258 // .dsp - Switch to DSP Assembler
1262 if ((cursect != TEXT) && (cursect != DATA))
1264 error(".dsp can only be used in the TEXT or DATA segments");
1268 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1275 rdsp = 1; // Set DSP assembly
1276 rgpu = 0; // Unset GPU assembly
1277 regbank = BANK_N; // Set no default register bank
1283 // .cargs [#offset], symbol[.size], ...
1285 // Lists of registers may also be mentioned; they just take up space. Good for
1286 // "documentation" purposes:
1288 // .cargs a6, .arg1, .arg2, .arg3...
1290 // Symbols thus created are ABS and EQUATED.
1294 VALUE eval = 4; // Default to 4 if no offset specified (to account for
1303 return error("directive forbidden in gpu/dsp mode");
1309 if (abs_expr(&eval) != OK)
1312 // Eat the comma, if it's there
1321 // p = (char *)tok[1];
1326 env = curenv; // Label is local
1328 env = 0; // Label is global
1330 // Set env to either local (dot prefixed) or global scope
1331 env = (*p == '.' ? curenv : 0);
1333 symbol = lookup(p, LABEL, env);
1337 symbol = NewSymbol(p, LABEL, env);
1340 else if (symbol->sattr & DEFINED)
1341 return errors("multiply-defined label '%s'", p);
1343 // Put symbol in "order of definition" list
1344 if (!(symbol->sattr & SDECLLIST))
1347 symbol->sattr |= (ABS | DEFINED | EQUATED);
1348 symbol->svalue = eval;
1351 // What this does is eat any dot suffixes attached to a symbol. If
1352 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1353 // there is no dot suffix, it assumes a size of 2.
1365 else if (*tok >= KW_D0 && *tok <= KW_A7)
1367 if (reglist(&rlist) < 0)
1370 // for(i=0; i++<16; rlist>>=1)
1371 for(i=0; i<16; i++, rlist>>=1)
1394 return error(".cargs syntax");
1398 // Eat commas in between each argument, if they exist
1406 // .cstruct [#offset], symbol[.size], ...
1408 // Lists of registers may also be mentioned; they just take up space. Good for
1409 // "documentation" purposes:
1411 // .cstruct a6, .arg1, .arg2, .arg3...
1413 // Symbols thus created are ABS and EQUATED. Note that this is for
1414 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1419 VALUE eval = 0; // Default, if no offset specified, is zero
1427 return error("directive forbidden in gpu/dsp mode");
1433 if (abs_expr(&eval) != OK)
1436 // Eat the comma, if it's there
1445 symbolName = string[tok[1]];
1447 // Set env to either local (dot prefixed) or global scope
1448 env = (symbolName[0] == '.' ? curenv : 0);
1449 symbol = lookup(symbolName, LABEL, env);
1451 // If the symbol wasn't found, then define it. Otherwise, throw an
1455 symbol = NewSymbol(symbolName, LABEL, env);
1458 else if (symbol->sattr & DEFINED)
1459 return errors("multiply-defined label '%s'", symbolName);
1461 // Put symbol in "order of definition" list
1462 if (!(symbol->sattr & SDECLLIST))
1467 // Adjust label start address if it's a word or a long, as a byte
1468 // label might have left us on an odd address.
1473 eval += eval & 0x01;
1476 symbol->sattr |= (ABS | DEFINED | EQUATED);
1477 symbol->svalue = eval;
1479 // Check for dot suffixes and adjust space accordingly (longs and
1480 // words on an odd boundary get bumped to the next word aligned
1481 // address). If no suffix, then throw an error.
1494 return error("Symbol missing dot suffix in .cstruct construct");
1499 else if (*tok >= KW_D0 && *tok <= KW_A7)
1501 if (reglist(&rlist) < 0)
1504 for(i=0; i<16; i++, rlist>>=1)
1527 return error(".cstruct syntax");
1531 // Eat commas in between each argument, if they exist
1539 // Undefine a macro - .undefmac macname [, macname...]
1541 int undmac1(char * p)
1543 SYM * symbol = lookup(p, MACRO, 0);
1545 // If the macro symbol exists, cause it to disappear
1546 // if ((sy = lookup(p, MACRO, 0)) != NULL)
1548 symbol->stype = (BYTE)SY_UNDEF;