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
24 TOKEN exprbuf[128]; // Expression buffer
26 // Directive handler table
42 d_undmac, // 14 undefmac
49 d_incbin, // 21 .incbin //load
50 d_unimpl, // 22 disable
51 d_unimpl, // 23 enable
53 d_regbank0, // 25 .regbank0
54 d_regbank1, // 26 .regbank1
56 d_assert, // 28 assert
58 d_unimpl, // 30* endif
61 d_include, // 33 include
63 d_unimpl, // 35* macro
70 d_dphrase, // 42 struct
73 d_subttl, // 45 subttl
77 d_noclear, // 49 .noclear
78 d_equrundef, // 50 .equrundef/.regundef
79 d_ccundef, // 51 .ccundef
81 d_gpumain, // 53 .gpumain
83 d_nojpad, // 55 .nojpad
88 // --- .org - Set origin ---------------------------------------------------------------------------
98 // --- .org - Set origin ---------------------------------------------------------------------------
106 return(error(".org permitted only in gpu/dsp section"));
110 if (abs_expr(&address) == ERROR)
112 error("cannot determine org'd address");
123 // --- NOP Padding Directive -----------------------------------------------------------------------
139 // --- Print Directive -----------------------------------------------------------------------------
144 char prntstr[LNSIZ]; // String for PRINT directive
145 char format[LNSIZ]; // Format for PRINT directive
146 int formatting = 0; // Formatting on/off
147 int wordlong = 0; // WORD = 0, LONG = 1
148 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
150 VALUE eval; // Expression value
151 WORD eattr; // Expression attributes
152 SYM *esym; // External symbol involved in expr.
153 TOKEN r_expr[EXPRSIZE];
158 sprintf(prntstr, "%s", (char*)tok[1]);
159 printf("%s", prntstr);
161 write(list_fd, prntstr, (LONG)strlen(prntstr));
166 if(tok[1] != SYMBOL) goto token_err;
167 strcpy(prntstr, (char*)tok[2]);
169 case 'l': case 'L': wordlong = 1; break;
170 case 'w': case 'W': wordlong = 0; break;
171 case 'x': case 'X': outtype = 0; break;
172 case 'd': case 'D': outtype = 1; break;
173 case 'u': case 'U': outtype = 2; break;
175 error("unknown print format flag");
184 if(expr(r_expr, &eval, &eattr, &esym) != OK)
188 case 0: strcpy(format, "%X"); break;
189 case 1: strcpy(format, "%d" ); break;
190 case 2: strcpy(format, "%u" ); break;
192 if(wordlong) sprintf(prntstr, format, eval);
193 else sprintf(prntstr, format, eval & 0xFFFF);
194 printf("%s", prntstr);
196 write(list_fd, prntstr, (LONG)strlen(prntstr));
211 error("illegal print token");
216 // --- Undefine an Equated Condition Code ----------------------------------------------------------
219 int d_ccundef(void) {
222 if(!rgpu && !rdsp) { // Check that we are in a RISC section
223 error(".ccundef must be defined in .gpu/.dsp section");
230 ccname = lookup((char *)tok[1], LABEL, 0);
231 if(!ccname || !(ccname->sattre & EQUATEDCC)) { // Make sure symbol is a valid ccdef
232 error("invalid equated condition name specified");
236 ccname->sattre |= UNDEF_CC;
242 // --- Undefine an Equated Register ----------------------------------------------------------------
245 int d_equrundef(void) {
248 if(!rgpu && !rdsp) { // Check that we are in a RISC section
249 error(".equrundef/.regundef must be defined in .gpu/.dsp section");
254 if(*tok == ',') tok++; // Skip preceeding or seperating commas
256 if(*tok != SYMBOL) { // Check we are dealing with a symbol
261 regname = lookup((char *)tok[1], LABEL, 0); // Lookup and undef if equated register
262 if(regname && (regname->sattre & EQUATEDREG))
263 regname->sattre |= UNDEF_EQUR;
265 tok += 2; // Skip over symbol token and address
272 // --- Do Not Allow the Use of the CLR.L Opcode ----------------------------------------------------
275 int d_noclear(void) {
280 // --- Include Binary File -------------------------------------------------------------------------
294 if((j = open((char *)tok[1], _OPEN_INC)) >= 0) {
295 size = lseek(j, 0L, SEEK_END);
297 pos = lseek(j, 0L, SEEK_SET);
299 for(i = 0; i < size; i++) {
301 bytes = read(j, &buf, 1);
306 errors("cannot open include binary file (%s)", (char*)tok[1]);
315 // --- Set RISC Register Banks ---------------------------------------------------------------------
318 int d_regbank0(void) {
319 regbank = BANK_0; // Set active register bank zero
323 int d_regbank1(void) {
324 regbank = BANK_1; // Set active register bank one
329 // --- Adjust Location to an EVEN Value ------------------------------------------------------------
334 if((scattr & SBSS) == 0) {
346 // --- Adjust Location to an LONG Value ------------------------------------------------------------
353 i = sloc & ~(val - 1);
354 if(i != sloc) val = val - (sloc - i);
358 if((scattr & SBSS) == 0) {
360 for(i = 0; i < val; i++)
371 // --- Adjust Location to an PHRASE Value ----------------------------------------------------------
378 i = sloc & ~(val - 1);
379 if(i != sloc) val = val - (sloc - i);
383 if((scattr & SBSS) == 0) {
385 for(i = 0; i < val; i++)
396 // --- Adjust Location to an DPHRASE Value ---------------------------------------------------------
399 int d_dphrase(void) {
403 i = sloc & ~(val - 1);
404 if(i != sloc) val = val - (sloc - i);
408 if((scattr & SBSS) == 0) {
410 for(i = 0; i < val; i++)
421 // --- Adjust Location to an QPHRASE Value ---------------------------------------------------------
424 int d_qphrase(void) {
428 i = sloc & ~(val - 1);
429 if(i != sloc) val = val - (sloc - i);
433 if((scattr & SBSS) == 0) {
436 for(i = 0; i < val; i++)
447 // -------------------------------------------------------------------------------------------------
448 // Do auto-even. This must be called ONLY if 'sloc' is odd.
450 // This is made hairy because, if there was a label on the line, we also have to adjust its value.
451 // This won't work with more than one label on the line, which is OK since multiple labels are only
452 // allowed in AS68 kludge mode, and the C compiler is VERY paranoid and uses ".even" whenever it can
453 // -------------------------------------------------------------------------------------------------
456 void auto_even(void) {
458 ++sloc; // Bump BSS section
460 D_byte(0) // Deposit 0.b in non-BSS
463 if(lab_sym != NULL) // Bump label if we have to
468 // --- Unimplemened Directive Error ----------------------------------------------------------------
472 return(error("unimplemented directive"));
476 // --- Return absolute (not TDB) and defined expression or return an error -------------------------
479 int abs_expr(VALUE *a_eval) {
482 if(expr(exprbuf, a_eval, &eattr, NULL) < 0)
484 if(!(eattr & DEFINED))
485 return(error(undef_error));
487 return(error(rel_error));
493 // --- Hand symbols in a symbol-list to a function (kind of like mapcar...) ------------------------
496 int symlist(int(*func)()) {
497 char *em = "symbol list syntax";
500 if(*tok != SYMBOL) return(error(em));
501 if((*func)(tok[1]) != OK) break;
503 if(*tok == EOL) break;
504 if(*tok != ',') return(error(em));
512 // --- .include "filename" -------------------------------------------------------------------------
515 int d_include(void) {
522 if(*tok == STRING) // Leave strings ALONE
524 else if(*tok == SYMBOL) { // Try to append ".s" to symbols
525 strcpy(buf, (char *)*++tok);
528 } else // Punt if no STRING or SYMBOL
529 return(error("missing filename"));
531 // Make sure the user didn't try anything like:
532 // .include equates.s
533 if(*++tok != EOL) return(error("extra stuff after filename -- enclose it in quotes"));
535 // Attempt to open the include file in the current directory, then (if that failed) try list
536 // of include files passed in the enviroment string or by the "-d" option.
537 if((j = open(fn, 0)) < 0) {
538 for(i = 0; nthpath("RMACPATH", i, buf1) != 0; ++i) {
540 if(j > 0 && buf1[j-1] != SLASHCHAR) // Append path char if necessary
541 strcat(buf1, SLASHSTRING);
543 if((j = open(buf1, 0)) >= 0)
547 return(errors("cannot open: \"%s\"", fn));
557 // --- .assert expression [, expression...] --------------------------------------------------------
564 for(; expr(exprbuf, &eval, &eattr, NULL) == OK; ++tok) {
565 if(!(eattr & DEFINED))
566 return(error("forward or undefined .assert"));
568 return(error("assert failure"));
577 // --- .globl symbol [, symbol] <<<cannot make local symbols global>>> -----------------------------
580 int globl1(char *p) {
584 return(error("cannot .globl local symbol"));
585 if((sy = lookup(p, LABEL, 0)) == NULL) {
586 sy = newsym(p, LABEL, 0);
601 // --- .abs [expression] ---------------------------------------------------------------------------
611 if(abs_expr(&eval) != OK)
620 // --- Switch Segments -----------------------------------------------------------------------------
625 return(error("directive forbidden in gpu/dsp mode"));
627 if(cursect != TEXT) {
636 return(error("directive forbidden in gpu/dsp mode"));
638 if(cursect != DATA) {
647 return(error("directive forbidden in gpu/dsp mode"));
657 // --- .ds[.size] expression -----------------------------------------------------------------------
664 // This gets kind of stupid. This directive is disallowed in normal 68000 mode ("for your own
665 // good!"), but is permitted for 6502 and Alcyon-compatibility modes.
666 // For obvious reasons, no auto-even is done in 8-bit processor modes.
667 if (as68_flag == 0 && (scattr & SBSS) == 0)
668 return(error(".ds permitted only in BSS"));
670 if (siz != SIZB && (sloc & 1)) // Automatic .even
673 if (abs_expr(&eval) != OK)
676 // In non-TDB section (BSS, ABS and M6502) just advance the location counter appropriately.
677 // In TDB sections, deposit (possibly large) chunks of zeroed memory....
683 just_bss = 1; // No data deposited (8-bit CPU mode)
687 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED|ABS), NULL);
695 // --- dc.b, dc.w / dc, dc.l -----------------------------------------------------------------------
706 int movei = 0; // movei flag for dc.i
708 if ((scattr & SBSS) != 0)
709 return(error("illegal initialization of section"));
711 if ((siz != SIZB) && (sloc & 1))
716 // dc.b 'string' [,] ...
717 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
719 i = strlen((const char*)tok[1]);
721 if ((challoc - ch_size) < i)
724 for(p=(char *)tok[1]; *p!=EOS; ++p)
739 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
742 tdb = (WORD)(eattr & TDB);
743 defined = (WORD)(eattr & DEFINED);
745 if ((challoc - ch_size) < 4)
753 fixup(FU_BYTE|FU_SEXT, sloc, exprbuf);
759 return(error("non-absolute byte value"));
761 if (eval + 0x100 >= 0x200)
762 return(error(range_error));
771 fixup(FU_WORD|FU_SEXT, sloc, exprbuf);
777 rmark(cursect, sloc, tdb, MWORD, NULL);
779 if (eval + 0x10000 >= 0x20000)
780 return(error(range_error));
782 // Deposit 68000 or 6502 (byte-reversed) word
790 fixup(FU_LONG|FU_MOVEI, sloc, exprbuf);
792 fixup(FU_LONG, sloc, exprbuf);
799 rmark(cursect, sloc, tdb, MLONG, NULL);
802 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
820 // --- dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2' --------------------------------------
828 if ((scattr & SBSS) != 0)
829 return(error("illegal initialization of section"));
831 if (abs_expr(&evalc) != OK)
835 return(error("missing comma"));
837 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
840 if ((siz != SIZB) && (sloc & 1))
843 dep_block(evalc, siz, eval, eattr, exprbuf);
848 // -------------------------------------------------------------------------------------------------
849 // Generalized initialization directive
851 // .init[.siz] [#count,] expression [.size] , ...
853 // The size suffix on the ".init" directive becomes the default size of the objects to deposit.
854 // If an item is preceeded with a sharp (immediate) sign and an expression, it specifies a repeat
855 // count. The value to be deposited may be followed by a size suffix, which overrides the
857 // -------------------------------------------------------------------------------------------------
860 int d_init(WORD def_siz)
867 if ((scattr & SBSS) != 0)
868 return(error(".init not permitted in BSS or ABS"));
871 return(error("directive forbidden in gpu/dsp mode"));
875 // Get repeat count (defaults to 1)
880 if (abs_expr(&count) != OK)
884 return(error(comma_error));
889 // Evaluate expression to deposit
890 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
894 { // Determine size of object to deposit
895 case DOTB: siz = SIZB; break;
896 case DOTW: siz = SIZB; break;
897 case DOTL: siz = SIZL; break;
904 dep_block(count, siz, eval, eattr, exprbuf);
914 return(error(comma_error));
920 // --- Deposit 'count' values of size 'siz' in the current (non-BSS) segment -----------------------
923 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN *exprbuf) {
927 tdb = (WORD)(eattr & TDB);
928 defined = (WORD)(eattr & DEFINED);
931 if((challoc - ch_size) < 4)
937 fixup(FU_BYTE|FU_SEXT, sloc, exprbuf);
941 return(error("non-absolute byte value"));
942 if(eval + 0x100 >= 0x200)
943 return(error(range_error));
950 fixup(FU_WORD|FU_SEXT, sloc, exprbuf);
954 rmark(cursect, sloc, tdb, MWORD, NULL);
955 if(eval + 0x10000 >= 0x20000)
956 return(error(range_error));
958 // Deposit 68000 or 6502 (byte-reversed) word
964 fixup(FU_LONG, sloc, exprbuf);
968 rmark(cursect, sloc, tdb, MLONG, NULL);
978 // --- .comm symbol, size --------------------------------------------------------------------------
987 return(error("missing symbol"));
991 if(*p == '.') // Cannot .comm a local symbol
992 return(error(locgl_error));
994 if((sym = lookup(p, LABEL, 0)) == NULL)
995 sym = newsym(p, LABEL, 0);
997 if(sym->sattr & DEFINED)
998 return(error(".comm symbol already defined"));
1001 sym->sattr = GLOBAL|COMMON|BSS;
1003 return(error(comma_error));
1005 if(abs_expr(&eval) != OK) // Parse size of common region
1007 sym->svalue = eval; // Install common symbol's size
1013 // --- .list - Turn listing on --------------------------------------------------------------------
1023 // --- .nlist - Turn listing off -------------------------------------------------------------------
1033 // --- .68000 - Back to 68000 TEXT segment ---------------------------------------------------------
1039 // Switching from gpu/dsp sections should reset any ORG'd Address
1048 // --- .gpu - Switch to GPU Assembler --------------------------------------------------------------
1052 if((cursect != TEXT) && (cursect != DATA)) {
1053 error(".gpu can only be used in the TEXT or DATA segments");
1056 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1061 rgpu = 1; // Set GPU assembly
1062 rdsp = 0; // Unset DSP assembly
1063 regbank = BANK_N; // Set no default register bank
1070 // --- GPU Main Code Directive ---------------------------------------------------------------------
1073 int d_gpumain(void) {
1074 if((cursect != TEXT) && (cursect != DATA)) {
1075 error(".gpumain can only be used in the TEXT or DATA segments");
1078 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1083 rgpu = 1; // Set GPU assembly
1084 rdsp = 0; // Unset DSP assembly
1085 regbank = BANK_N; // Set no default register bank
1086 in_main = 1; // Enable main code execution rules
1092 // --- .dsp - Switch to DSP Assembler --------------------------------------------------------------
1096 if((cursect != TEXT) && (cursect != DATA)) {
1097 error(".dsp can only be used in the TEXT or DATA segments");
1100 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1105 rdsp = 1; // Set DSP assembly
1106 rgpu = 0; // Unset GPU assembly
1107 regbank = BANK_N; // Set no default register bank
1114 // -------------------------------------------------------------------------------------------------
1115 // .cargs [#offset], symbol[.size], ...
1117 // Lists of registers may also be mentioned; they just take up space. Good for "documentation"
1120 // .cargs a6,.arg1, .arg2, .arg3...
1122 // The symbols are ABS and EQUATED.
1123 // -------------------------------------------------------------------------------------------------
1135 return(error("directive forbidden in gpu/dsp mode"));
1139 if(abs_expr(&eval) != OK)
1141 if(*tok == ',') // Eat comma if it's there
1147 if(*tok == SYMBOL) {
1154 sy = lookup(p, LABEL, env);
1156 sy = newsym(p, LABEL, env);
1159 if(sy->sattr & DEFINED)
1160 return(errors("multiply-defined label '%s'", p));
1162 // Put symbol in "order of definition" list
1163 if(!(sy->sattr & SDECLLIST)) sym_decl(sy);
1165 sy->sattr |= ABS|DEFINED|EQUATED;
1178 if(*tok >= KW_D0 && *tok <= KW_A7) {
1179 if(reglist(&rlist) < 0)
1181 for(i = 0; i++ < 16; rlist >>= 1)
1199 return(error(".cargs syntax"));
1208 // --- Undefine a macro - .undefmac macname [, macname...] -----------------------------------------
1211 int undmac1(char *p) {
1214 // If the macro symbol exists, cause it to dissappear
1215 if((sy = lookup(p, MACRO, 0)) != NULL) sy->stype = (BYTE)SY_UNDEF;
1219 int d_undmac(void) {