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
25 SYM * symbolPtr[1000000]; // Symbol pointers table
27 // Directive handler table
43 d_undmac, // 14 undefmac
50 d_incbin, // 21 .incbin //load
51 d_unimpl, // 22 disable
52 d_unimpl, // 23 enable
54 d_regbank0, // 25 .regbank0
55 d_regbank1, // 26 .regbank1
57 d_assert, // 28 assert
59 d_unimpl, // 30* endif
62 d_include, // 33 include
64 d_unimpl, // 35* macro
65 ExitMacro, // 36* exitm
71 d_dphrase, // 42 struct
74 d_subttl, // 45 subttl
78 d_noclear, // 49 .noclear
79 d_equrundef, // 50 .equrundef/.regundef
80 d_ccundef, // 51 .ccundef
82 d_gpumain, // 53 .gpumain
84 d_nojpad, // 55 .nojpad
107 return error(".org permitted only in gpu/dsp section");
111 if (abs_expr(&address) == ERROR)
113 error("cannot determine org'd address");
125 // NOP Padding Directive
146 char prntstr[LNSIZ]; // String for PRINT directive
147 char format[LNSIZ]; // Format for PRINT directive
148 int formatting = 0; // Formatting on/off
149 int wordlong = 0; // WORD = 0, LONG = 1
150 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
152 VALUE eval; // Expression value
153 WORD eattr; // Expression attributes
154 SYM * esym; // External symbol involved in expr.
155 TOKEN r_expr[EXPRSIZE];
162 // sprintf(prntstr, "%s", (char *)tok[1]);
163 sprintf(prntstr, "%s", string[tok[1]]);
164 printf("%s", prntstr);
167 write(list_fd, prntstr, (LONG)strlen(prntstr));
174 if (tok[1] != SYMBOL)
177 // strcpy(prntstr, (char *)tok[2]);
178 strcpy(prntstr, string[tok[2]]);
182 case 'l': case 'L': wordlong = 1; break;
183 case 'w': case 'W': wordlong = 0; break;
184 case 'x': case 'X': outtype = 0; break;
185 case 'd': case 'D': outtype = 1; break;
186 case 'u': case 'U': outtype = 2; break;
188 error("unknown print format flag");
198 if (expr(r_expr, &eval, &eattr, &esym) != OK)
204 case 0: strcpy(format, "%X"); break;
205 case 1: strcpy(format, "%d" ); break;
206 case 2: strcpy(format, "%u" ); break;
210 sprintf(prntstr, format, eval);
212 sprintf(prntstr, format, eval & 0xFFFF);
214 printf("%s", prntstr);
217 write(list_fd, prntstr, (LONG)strlen(prntstr));
234 error("illegal print token");
240 // Undefine an Equated Condition Code
246 // Check that we are in a RISC section
249 error(".ccundef must be defined in .gpu/.dsp section");
259 // ccname = lookup((char *)tok[1], LABEL, 0);
260 ccname = lookup(string[tok[1]], LABEL, 0);
262 // Make sure symbol is a valid ccdef
263 if (!ccname || !(ccname->sattre & EQUATEDCC))
265 error("invalid equated condition name specified");
269 ccname->sattre |= UNDEF_CC;
276 // Undefine an Equated Register
278 int d_equrundef(void)
282 // Check that we are in a RISC section
285 error(".equrundef/.regundef must be defined in .gpu/.dsp section");
291 // Skip preceeding or seperating commas
295 // Check we are dealing with a symbol
302 // Lookup and undef if equated register
303 // regname = lookup((char *)tok[1], LABEL, 0);
304 regname = lookup(string[tok[1]], LABEL, 0);
306 if (regname && (regname->sattre & EQUATEDREG))
307 regname->sattre |= UNDEF_EQUR;
309 // Skip over symbol token and address
318 // Do Not Allow the Use of the CLR.L Opcode
327 // Include Binary File
336 // Check to see if we're in BSS, and, if so, throw an error
339 errors("Cannot include binary file \"%s\" in BSS section", string[tok[1]]);
349 // if ((j = open((char *)tok[1], _OPEN_INC)) >= 0)
350 if ((j = open(string[tok[1]], _OPEN_INC)) >= 0)
352 size = lseek(j, 0L, SEEK_END);
354 pos = lseek(j, 0L, SEEK_SET);
358 printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size);
361 for(i=0; i<size; i++)
364 bytes = read(j, &buf, 1);
370 // errors("cannot open include binary file (%s)", (char *)tok[1]);
371 errors("cannot open include binary file (%s)", string[tok[1]]);
381 // Set RISC Register Banks
385 regbank = BANK_0; // Set active register bank zero
392 regbank = BANK_1; // Set active register bank one
398 // Adjust Location to an EVEN Value
404 if ((scattr & SBSS) == 0)
420 // Adjust Location to an LONG Value
427 i = sloc & ~(val - 1);
430 val = val - (sloc - i);
436 if ((scattr & SBSS) == 0)
454 // Adjust Location to an PHRASE Value
461 i = sloc & ~(val - 1);
464 val = val - (sloc - i);
470 if ((scattr & SBSS) == 0)
488 // Adjust Location to an DPHRASE Value
495 i = sloc & ~(val - 1);
498 val = val - (sloc - i);
504 if ((scattr & SBSS) == 0)
522 // Adjust Location to an QPHRASE Value
529 i = sloc & ~(val - 1);
532 val = val - (sloc - i);
538 if ((scattr & SBSS) == 0)
557 // Do auto-even. This must be called ONLY if 'sloc' is odd.
559 // This is made hairy because, if there was a label on the line, we also have
560 // to adjust its value. This won't work with more than one label on the line,
561 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
562 // the C compiler is VERY paranoid and uses ".even" whenever it can
567 ++sloc; // Bump BSS section
569 D_byte(0) // Deposit 0.b in non-BSS
571 if (lab_sym != NULL) // Bump label if we have to
577 // Unimplemened Directive Error
581 return error("unimplemented directive");
586 // Return absolute (not TDB) and defined expression or return an error
588 int abs_expr(VALUE * a_eval)
592 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
595 if (!(eattr & DEFINED))
596 return error(undef_error);
599 return error(rel_error);
606 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
608 int symlist(int(* func)())
610 char * em = "symbol list syntax";
618 if ((*func)(tok[1]) != OK)
620 if ((*func)(string[tok[1]]) != OK)
640 // .include "filename"
650 if (*tok == STRING) // Leave strings ALONE
656 else if (*tok == SYMBOL) // Try to append ".s" to symbols
659 strcpy(buf, (char *)*++tok);
661 strcpy(buf, string[*++tok]);
666 else // Punt if no STRING or SYMBOL
667 return error("missing filename");
669 // Make sure the user didn't try anything like:
670 // .include equates.s
672 return error("extra stuff after filename -- enclose it in quotes");
674 // Attempt to open the include file in the current directory, then (if that
675 // failed) try list of include files passed in the enviroment string or by
677 if ((j = open(fn, 0)) < 0)
679 for(i=0; nthpath("RMACPATH", i, buf1)!=0; ++i)
683 if (j > 0 && buf1[j-1] != SLASHCHAR) // Append path char if necessary
684 strcat(buf1, SLASHSTRING);
688 if ((j = open(buf1, 0)) >= 0)
692 return errors("cannot open: \"%s\"", fn);
702 // .assert expression [, expression...]
709 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
711 if (!(eattr & DEFINED))
712 return error("forward or undefined .assert");
715 return error("assert failure");
727 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
734 return error("cannot .globl local symbol");
736 if ((sy = lookup(p, LABEL, 0)) == NULL)
738 sy = NewSymbol(p, LABEL, 0);
767 else if (abs_expr(&eval) != OK)
783 return error("directive forbidden in gpu/dsp mode");
798 return error("directive forbidden in gpu/dsp mode");
813 return error("directive forbidden in gpu/dsp mode");
826 // .ds[.size] expression
832 // This gets kind of stupid. This directive is disallowed in normal 68000
833 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
834 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
836 if (as68_flag == 0 && (scattr & SBSS) == 0)
837 return error(".ds permitted only in BSS");
839 if (siz != SIZB && (sloc & 1)) // Automatic .even
842 if (abs_expr(&eval) != OK)
845 // In non-TDB section (BSS, ABS and M6502) just advance the location
846 // counter appropriately. In TDB sections, deposit (possibly large) chunks
847 //of zeroed memory....
853 just_bss = 1; // No data deposited (8-bit CPU mode)
857 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED|ABS), NULL);
866 // dc.b, dc.w / dc, dc.l
876 int movei = 0; // movei flag for dc.i
878 if ((scattr & SBSS) != 0)
879 return error("illegal initialization of section");
881 if ((siz != SIZB) && (sloc & 1))
886 // dc.b 'string' [,] ...
887 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
889 // i = strlen((const char*)tok[1]);
890 i = strlen(string[tok[1]]);
892 if ((challoc - ch_size) < i)
895 for(p=string[tok[1]]; *p!=EOS; ++p)
910 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
913 tdb = (WORD)(eattr & TDB);
914 defined = (WORD)(eattr & DEFINED);
916 if ((challoc - ch_size) < 4)
924 fixup(FU_BYTE|FU_SEXT, sloc, exprbuf);
930 return error("non-absolute byte value");
932 if (eval + 0x100 >= 0x200)
933 return error(range_error);
943 fixup(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 fixup(FU_LONG|FU_MOVEI, sloc, exprbuf);
965 fixup(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 if ((scattr & SBSS) != 0)
1001 return error("illegal initialization of section");
1003 if (abs_expr(&evalc) != OK)
1007 return error("missing comma");
1009 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1012 if ((siz != SIZB) && (sloc & 1))
1015 dep_block(evalc, siz, eval, eattr, exprbuf);
1021 // Generalized initialization directive
1023 // .init[.siz] [#count,] expression [.size] , ...
1025 // The size suffix on the ".init" directive becomes the default size of the
1026 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1027 // and an expression, it specifies a repeat count. The value to be deposited
1028 // may be followed by a size suffix, which overrides the default size.
1030 int d_init(WORD def_siz)
1037 if ((scattr & SBSS) != 0)
1038 return error(".init not permitted in BSS or ABS");
1041 return error("directive forbidden in gpu/dsp mode");
1045 // Get repeat count (defaults to 1)
1050 if (abs_expr(&count) != OK)
1054 return error(comma_error);
1059 // Evaluate expression to deposit
1060 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1063 switch ((int)*tok++)
1064 { // Determine size of object to deposit
1065 case DOTB: siz = SIZB; break;
1066 case DOTW: siz = SIZB; break;
1067 case DOTL: siz = SIZL; break;
1074 dep_block(count, siz, eval, eattr, exprbuf);
1084 return error(comma_error);
1091 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1093 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1098 tdb = (WORD)(eattr & TDB);
1099 defined = (WORD)(eattr & DEFINED);
1103 if ((challoc - ch_size) < 4)
1111 fixup(FU_BYTE|FU_SEXT, sloc, exprbuf);
1117 return error("non-absolute byte value");
1119 if (eval + 0x100 >= 0x200)
1120 return error(range_error);
1130 fixup(FU_WORD|FU_SEXT, sloc, exprbuf);
1136 rmark(cursect, sloc, tdb, MWORD, NULL);
1138 if (eval + 0x10000 >= 0x20000)
1139 return error(range_error);
1141 // Deposit 68000 or 6502 (byte-reversed) word
1149 fixup(FU_LONG, sloc, exprbuf);
1155 rmark(cursect, sloc, tdb, MLONG, NULL);
1169 // .comm symbol, size
1178 return error("missing symbol");
1180 // p = (char *)tok[1];
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
1240 // Switching from gpu/dsp sections should reset any ORG'd Address
1250 // .gpu - Switch to GPU Assembler
1254 if ((cursect != TEXT) && (cursect != DATA))
1256 error(".gpu can only be used in the TEXT or DATA segments");
1260 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1267 rgpu = 1; // Set GPU assembly
1268 rdsp = 0; // Unset DSP assembly
1269 regbank = BANK_N; // Set no default register bank
1277 // GPU Main Code Directive
1282 if ((cursect != TEXT) && (cursect != DATA))
1284 error(".gpumain can only be used in the TEXT or DATA segments");
1288 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1295 rgpu = 1; // Set GPU assembly
1296 rdsp = 0; // Unset DSP assembly
1297 regbank = BANK_N; // Set no default register bank
1298 in_main = 1; // Enable main code execution rules
1305 // .dsp - Switch to DSP Assembler
1309 if ((cursect != TEXT) && (cursect != DATA))
1311 error(".dsp can only be used in the TEXT or DATA segments");
1315 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1322 rdsp = 1; // Set DSP assembly
1323 rgpu = 0; // Unset GPU assembly
1324 regbank = BANK_N; // Set no default register bank
1332 // .cargs [#offset], symbol[.size], ...
1334 // Lists of registers may also be mentioned; they just take up space. Good for
1335 // "documentation" purposes.
1337 // .cargs a6,.arg1, .arg2, .arg3...
1339 // The symbols are ABS and EQUATED.
1351 return error("directive forbidden in gpu/dsp mode");
1357 if (abs_expr(&eval) != OK)
1360 if (*tok == ',') // Eat comma if it's there
1370 // p = (char *)tok[1];
1378 sy = lookup(p, LABEL, env);
1382 sy = NewSymbol(p, LABEL, env);
1385 else if (sy->sattr & DEFINED)
1386 return errors("multiply-defined label '%s'", p);
1388 // Put symbol in "order of definition" list
1389 if (!(sy->sattr & SDECLLIST))
1392 sy->sattr |= ABS|DEFINED|EQUATED;
1409 if (*tok >= KW_D0 && *tok <= KW_A7)
1411 if (reglist(&rlist) < 0)
1414 for(i=0; i++<16; rlist>>=1)
1435 return error(".cargs syntax");
1447 // Undefine a macro - .undefmac macname [, macname...]
1449 int undmac1(char * p)
1453 // If the macro symbol exists, cause it to dissappear
1454 if ((sy = lookup(p, MACRO, 0)) != NULL)
1455 sy->stype = (BYTE)SY_UNDEF;