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
93 return error(".org permitted only in gpu/dsp section");
97 if (abs_expr(&address) == ERROR)
99 error("cannot determine org'd address");
115 char prntstr[LNSIZ]; // String for PRINT directive
116 char format[LNSIZ]; // Format for PRINT directive
117 int formatting = 0; // Formatting on/off
118 int wordlong = 0; // WORD = 0, LONG = 1
119 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
121 VALUE eval; // Expression value
122 WORD eattr; // Expression attributes
123 SYM * esym; // External symbol involved in expr.
124 TOKEN r_expr[EXPRSIZE];
131 // sprintf(prntstr, "%s", (char *)tok[1]);
132 sprintf(prntstr, "%s", string[tok[1]]);
133 printf("%s", prntstr);
136 write(list_fd, prntstr, (LONG)strlen(prntstr));
143 if (tok[1] != SYMBOL)
146 // strcpy(prntstr, (char *)tok[2]);
147 strcpy(prntstr, string[tok[2]]);
151 case 'l': case 'L': wordlong = 1; break;
152 case 'w': case 'W': wordlong = 0; break;
153 case 'x': case 'X': outtype = 0; break;
154 case 'd': case 'D': outtype = 1; break;
155 case 'u': case 'U': outtype = 2; break;
157 error("unknown print format flag");
167 if (expr(r_expr, &eval, &eattr, &esym) != OK)
173 case 0: strcpy(format, "%X"); break;
174 case 1: strcpy(format, "%d" ); break;
175 case 2: strcpy(format, "%u" ); break;
179 sprintf(prntstr, format, eval);
181 sprintf(prntstr, format, eval & 0xFFFF);
183 printf("%s", prntstr);
186 write(list_fd, prntstr, (LONG)strlen(prntstr));
203 error("illegal print token");
209 // Undefine an Equated Condition Code
215 // Check that we are in a RISC section
218 error(".ccundef must be defined in .gpu/.dsp section");
228 // ccname = lookup((char *)tok[1], LABEL, 0);
229 ccname = lookup(string[tok[1]], LABEL, 0);
231 // Make sure symbol is a valid ccdef
232 if (!ccname || !(ccname->sattre & EQUATEDCC))
234 error("invalid equated condition name specified");
238 ccname->sattre |= UNDEF_CC;
245 // Undefine an Equated Register
247 int d_equrundef(void)
251 // Check that we are in a RISC section
254 error(".equrundef/.regundef must be defined in .gpu/.dsp section");
260 // Skip preceeding or seperating commas
264 // Check we are dealing with a symbol
271 // Lookup and undef if equated register
272 // regname = lookup((char *)tok[1], LABEL, 0);
273 regname = lookup(string[tok[1]], LABEL, 0);
275 if (regname && (regname->sattre & EQUATEDREG))
276 regname->sattre |= UNDEF_EQUR;
278 // Skip over symbol token and address
287 // Do Not Allow the Use of the CLR.L Opcode
296 // Include Binary File
305 // Check to see if we're in BSS, and, if so, throw an error
308 errors("Cannot include binary file \"%s\" in BSS section", string[tok[1]]);
318 // if ((j = open((char *)tok[1], _OPEN_INC)) >= 0)
319 if ((j = open(string[tok[1]], _OPEN_INC)) >= 0)
321 size = lseek(j, 0L, SEEK_END);
323 pos = lseek(j, 0L, SEEK_SET);
327 printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size);
330 for(i=0; i<size; i++)
333 bytes = read(j, &buf, 1);
339 // errors("cannot open include binary file (%s)", (char *)tok[1]);
340 errors("cannot open include binary file (%s)", string[tok[1]]);
350 // Set RISC Register Banks
354 // Set active register bank zero
362 // Set active register bank one
369 // Adjust Location to an EVEN Value
375 if ((scattr & SBSS) == 0)
391 // Adjust Location to an LONG Value
398 i = sloc & ~(val - 1);
401 val = val - (sloc - i);
407 if ((scattr & SBSS) == 0)
425 // Adjust Location to an PHRASE Value
432 i = sloc & ~(val - 1);
435 val = val - (sloc - i);
441 if ((scattr & SBSS) == 0)
459 // Adjust Location to an DPHRASE Value
466 i = sloc & ~(val - 1);
469 val = val - (sloc - i);
475 if ((scattr & SBSS) == 0)
493 // Adjust Location to an QPHRASE Value
500 i = sloc & ~(val - 1);
503 val = val - (sloc - i);
509 if ((scattr & SBSS) == 0)
528 // Do auto-even. This must be called ONLY if 'sloc' is odd.
530 // This is made hairy because, if there was a label on the line, we also have
531 // to adjust its value. This won't work with more than one label on the line,
532 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
533 // the C compiler is VERY paranoid and uses ".even" whenever it can
538 ++sloc; // Bump BSS section
540 D_byte(0) // Deposit 0.b in non-BSS
542 if (lab_sym != NULL) // Bump label if we have to
548 // Unimplemened Directive Error
552 return error("unimplemented directive");
557 // Return absolute (not TDB) and defined expression or return an error
559 int abs_expr(VALUE * a_eval)
563 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
566 if (!(eattr & DEFINED))
567 return error(undef_error);
570 return error(rel_error);
577 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
579 int symlist(int(* func)())
581 char * em = "symbol list syntax";
589 if ((*func)(tok[1]) != OK)
591 if ((*func)(string[tok[1]]) != OK)
611 // .include "filename"
621 if (*tok == STRING) // Leave strings ALONE
627 else if (*tok == SYMBOL) // Try to append ".s" to symbols
630 strcpy(buf, (char *)*++tok);
632 strcpy(buf, string[*++tok]);
637 else // Punt if no STRING or SYMBOL
638 return error("missing filename");
640 // Make sure the user didn't try anything like:
641 // .include equates.s
643 return error("extra stuff after filename -- enclose it in quotes");
645 // Attempt to open the include file in the current directory, then (if that
646 // failed) try list of include files passed in the enviroment string or by
648 if ((j = open(fn, 0)) < 0)
650 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
654 if (j > 0 && buf1[j - 1] != SLASHCHAR) // Append path char if necessary
655 strcat(buf1, SLASHSTRING);
659 if ((j = open(buf1, 0)) >= 0)
663 return errors("cannot open: \"%s\"", fn);
673 // .assert expression [, expression...]
680 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
682 if (!(eattr & DEFINED))
683 return error("forward or undefined .assert");
686 return error("assert failure");
698 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
705 return error("cannot .globl local symbol");
707 if ((sy = lookup(p, LABEL, 0)) == NULL)
709 sy = NewSymbol(p, LABEL, 0);
738 else if (abs_expr(&eval) != OK)
754 return error("directive forbidden in gpu/dsp mode");
769 return error("directive forbidden in gpu/dsp mode");
784 return error("directive forbidden in gpu/dsp mode");
797 // .ds[.size] expression
803 // This gets kind of stupid. This directive is disallowed in normal 68000
804 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
805 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
807 if (as68_flag == 0 && (scattr & SBSS) == 0)
808 return error(".ds permitted only in BSS");
810 if (siz != SIZB && (sloc & 1)) // Automatic .even
813 if (abs_expr(&eval) != OK)
816 // In non-TDB section (BSS, ABS and M6502) just advance the location
817 // counter appropriately. In TDB sections, deposit (possibly large) chunks
818 //of zeroed memory....
824 just_bss = 1; // No data deposited (8-bit CPU mode)
828 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED|ABS), NULL);
837 // dc.b, dc.w / dc, dc.l
847 int movei = 0; // movei flag for dc.i
849 if ((scattr & SBSS) != 0)
850 return error("illegal initialization of section");
852 if ((siz != SIZB) && (sloc & 1))
857 // dc.b 'string' [,] ...
858 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
860 // i = strlen((const char*)tok[1]);
861 i = strlen(string[tok[1]]);
863 if ((challoc - ch_size) < i)
866 for(p=string[tok[1]]; *p!=EOS; ++p)
881 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
884 tdb = (WORD)(eattr & TDB);
885 defined = (WORD)(eattr & DEFINED);
887 if ((challoc - ch_size) < 4)
895 fixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
901 return error("non-absolute byte value");
903 if (eval + 0x100 >= 0x200)
904 return error(range_error);
914 fixup(FU_WORD | FU_SEXT, sloc, exprbuf);
920 rmark(cursect, sloc, tdb, MWORD, NULL);
922 if (eval + 0x10000 >= 0x20000)
923 return error(range_error);
925 // Deposit 68000 or 6502 (byte-reversed) word
934 fixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
936 fixup(FU_LONG, sloc, exprbuf);
943 rmark(cursect, sloc, tdb, MLONG, NULL);
946 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
964 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
971 if ((scattr & SBSS) != 0)
972 return error("illegal initialization of section");
974 if (abs_expr(&evalc) != OK)
978 return error("missing comma");
980 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
983 if ((siz != SIZB) && (sloc & 1))
986 dep_block(evalc, siz, eval, eattr, exprbuf);
992 // Generalized initialization directive
994 // .init[.siz] [#count,] expression [.size] , ...
996 // The size suffix on the ".init" directive becomes the default size of the
997 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
998 // and an expression, it specifies a repeat count. The value to be deposited
999 // may be followed by a size suffix, which overrides the default size.
1001 int d_init(WORD def_siz)
1008 if ((scattr & SBSS) != 0)
1009 return error(".init not permitted in BSS or ABS");
1012 return error("directive forbidden in gpu/dsp mode");
1016 // Get repeat count (defaults to 1)
1021 if (abs_expr(&count) != OK)
1025 return error(comma_error);
1030 // Evaluate expression to deposit
1031 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1034 switch ((int)*tok++)
1035 { // Determine size of object to deposit
1036 case DOTB: siz = SIZB; break;
1037 case DOTW: siz = SIZB; break;
1038 case DOTL: siz = SIZL; break;
1045 dep_block(count, siz, eval, eattr, exprbuf);
1055 return error(comma_error);
1062 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1064 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1069 tdb = (WORD)(eattr & TDB);
1070 defined = (WORD)(eattr & DEFINED);
1074 if ((challoc - ch_size) < 4)
1082 fixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1088 return error("non-absolute byte value");
1090 if (eval + 0x100 >= 0x200)
1091 return error(range_error);
1101 fixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1107 rmark(cursect, sloc, tdb, MWORD, NULL);
1109 if (eval + 0x10000 >= 0x20000)
1110 return error(range_error);
1112 // Deposit 68000 or 6502 (byte-reversed) word
1120 fixup(FU_LONG, sloc, exprbuf);
1126 rmark(cursect, sloc, tdb, MLONG, NULL);
1140 // .comm symbol, size
1149 return error("missing symbol");
1151 // p = (char *)tok[1];
1155 if (*p == '.') // Cannot .comm a local symbol
1156 return error(locgl_error);
1158 if ((sym = lookup(p, LABEL, 0)) == NULL)
1159 sym = NewSymbol(p, LABEL, 0);
1162 if (sym->sattr & DEFINED)
1163 return error(".comm symbol already defined");
1166 sym->sattr = GLOBAL | COMMON | BSS;
1169 return error(comma_error);
1171 if (abs_expr(&eval) != OK) // Parse size of common region
1174 sym->svalue = eval; // Install common symbol's size
1181 // .list - Turn listing on
1193 // .nlist - Turn listing off
1205 // .68000 - Back to 68000 TEXT segment
1210 // Switching from gpu/dsp sections should reset any ORG'd Address
1220 // .gpu - Switch to GPU Assembler
1224 if ((cursect != TEXT) && (cursect != DATA))
1226 error(".gpu can only be used in the TEXT or DATA segments");
1230 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1237 rgpu = 1; // Set GPU assembly
1238 rdsp = 0; // Unset DSP assembly
1239 regbank = BANK_N; // Set no default register bank
1245 // .dsp - Switch to DSP Assembler
1249 if ((cursect != TEXT) && (cursect != DATA))
1251 error(".dsp can only be used in the TEXT or DATA segments");
1255 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1262 rdsp = 1; // Set DSP assembly
1263 rgpu = 0; // Unset GPU assembly
1264 regbank = BANK_N; // Set no default register bank
1270 // .cargs [#offset], symbol[.size], ...
1272 // Lists of registers may also be mentioned; they just take up space. Good for
1273 // "documentation" purposes:
1275 // .cargs a6, .arg1, .arg2, .arg3...
1277 // Symbols thus created are ABS and EQUATED.
1289 return error("directive forbidden in gpu/dsp mode");
1295 if (abs_expr(&eval) != OK)
1298 // Eat the comma, if it's there
1303 // Default to 4 if not specified (because PC is on stack according to GroovyBee)
1310 // p = (char *)tok[1];
1315 env = curenv; // Label is local
1317 env = 0; // Label is global
1319 // Set env to either local (dot prefixed) or global scope
1320 env = (*p == '.' ? curenv : 0);
1322 symbol = lookup(p, LABEL, env);
1326 symbol = NewSymbol(p, LABEL, env);
1329 else if (symbol->sattr & DEFINED)
1330 return errors("multiply-defined label '%s'", p);
1332 // Put symbol in "order of definition" list
1333 if (!(symbol->sattr & SDECLLIST))
1336 symbol->sattr |= (ABS | DEFINED | EQUATED);
1337 symbol->svalue = eval;
1340 // What this does is eat any dot suffixes attached to a symbol. If
1341 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1342 // there is no dot suffix, it assumes a size of 2.
1354 else if (*tok >= KW_D0 && *tok <= KW_A7)
1356 if (reglist(&rlist) < 0)
1359 // for(i=0; i++<16; rlist>>=1)
1360 for(i=0; i<16; i++, rlist>>=1)
1383 return error(".cargs syntax");
1387 // Eat commas in between each argument, if they exist
1395 // Undefine a macro - .undefmac macname [, macname...]
1397 int undmac1(char * p)
1399 SYM * symbol = lookup(p, MACRO, 0);
1401 // If the macro symbol exists, cause it to disappear
1402 // if ((sy = lookup(p, MACRO, 0)) != NULL)
1404 symbol->stype = (BYTE)SY_UNDEF;