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
106 return error(".org permitted only in gpu/dsp section");
110 if (abs_expr(&address) == ERROR)
112 error("cannot determine org'd address");
124 // NOP Padding Directive
145 char prntstr[LNSIZ]; // String for PRINT directive
146 char format[LNSIZ]; // Format for PRINT directive
147 int formatting = 0; // Formatting on/off
148 int wordlong = 0; // WORD = 0, LONG = 1
149 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
151 VALUE eval; // Expression value
152 WORD eattr; // Expression attributes
153 SYM * esym; // External symbol involved in expr.
154 TOKEN r_expr[EXPRSIZE];
161 sprintf(prntstr, "%s", (char *)tok[1]);
162 printf("%s", prntstr);
165 write(list_fd, prntstr, (LONG)strlen(prntstr));
172 if (tok[1] != SYMBOL)
175 strcpy(prntstr, (char *)tok[2]);
179 case 'l': case 'L': wordlong = 1; break;
180 case 'w': case 'W': wordlong = 0; break;
181 case 'x': case 'X': outtype = 0; break;
182 case 'd': case 'D': outtype = 1; break;
183 case 'u': case 'U': outtype = 2; break;
185 error("unknown print format flag");
195 if (expr(r_expr, &eval, &eattr, &esym) != OK)
201 case 0: strcpy(format, "%X"); break;
202 case 1: strcpy(format, "%d" ); break;
203 case 2: strcpy(format, "%u" ); break;
207 sprintf(prntstr, format, eval);
209 sprintf(prntstr, format, eval & 0xFFFF);
211 printf("%s", prntstr);
214 write(list_fd, prntstr, (LONG)strlen(prntstr));
231 error("illegal print token");
237 // Undefine an Equated Condition Code
243 // Check that we are in a RISC section
246 error(".ccundef must be defined in .gpu/.dsp section");
256 ccname = lookup((char *)tok[1], LABEL, 0);
258 // Make sure symbol is a valid ccdef
259 if (!ccname || !(ccname->sattre & EQUATEDCC))
261 error("invalid equated condition name specified");
265 ccname->sattre |= UNDEF_CC;
272 // Undefine an Equated Register
274 int d_equrundef(void)
278 // Check that we are in a RISC section
281 error(".equrundef/.regundef must be defined in .gpu/.dsp section");
287 // Skip preceeding or seperating commas
291 // Check we are dealing with a symbol
298 // Lookup and undef if equated register
299 regname = lookup((char *)tok[1], LABEL, 0);
301 if (regname && (regname->sattre & EQUATEDREG))
302 regname->sattre |= UNDEF_EQUR;
304 // Skip over symbol token and address
313 // Do Not Allow the Use of the CLR.L Opcode
322 // Include Binary File
337 if ((j = open((char *)tok[1], _OPEN_INC)) >= 0)
339 size = lseek(j, 0L, SEEK_END);
341 pos = lseek(j, 0L, SEEK_SET);
343 for(i=0; i<size; i++)
346 bytes = read(j, &buf, 1);
352 errors("cannot open include binary file (%s)", (char *)tok[1]);
362 // Set RISC Register Banks
366 regbank = BANK_0; // Set active register bank zero
373 regbank = BANK_1; // 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";
598 if ((*func)(tok[1]) != OK)
617 // .include "filename"
627 if (*tok == STRING) // Leave strings ALONE
629 else if (*tok == SYMBOL) // Try to append ".s" to symbols
631 strcpy(buf, (char *)*++tok);
635 else // Punt if no STRING or SYMBOL
636 return error("missing filename");
638 // Make sure the user didn't try anything like:
639 // .include equates.s
641 return error("extra stuff after filename -- enclose it in quotes");
643 // Attempt to open the include file in the current directory, then (if that failed) try list
644 // of include files passed in the enviroment string or by the "-d" option.
645 if ((j = open(fn, 0)) < 0)
647 for(i=0; nthpath("RMACPATH", i, buf1)!=0; ++i)
651 if (j > 0 && buf1[j-1] != SLASHCHAR) // Append path char if necessary
652 strcat(buf1, SLASHSTRING);
656 if ((j = open(buf1, 0)) >= 0)
660 return errors("cannot open: \"%s\"", fn);
670 // .assert expression [, expression...]
677 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
679 if (!(eattr & DEFINED))
680 return error("forward or undefined .assert");
683 return error("assert failure");
695 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
702 return error("cannot .globl local symbol");
704 if ((sy = lookup(p, LABEL, 0)) == NULL)
706 sy = newsym(p, LABEL, 0);
735 else if (abs_expr(&eval) != OK)
751 return error("directive forbidden in gpu/dsp mode");
766 return error("directive forbidden in gpu/dsp mode");
781 return error("directive forbidden in gpu/dsp mode");
794 // .ds[.size] expression
800 // This gets kind of stupid. This directive is disallowed in normal 68000
801 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
802 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
804 if (as68_flag == 0 && (scattr & SBSS) == 0)
805 return error(".ds permitted only in BSS");
807 if (siz != SIZB && (sloc & 1)) // Automatic .even
810 if (abs_expr(&eval) != OK)
813 // In non-TDB section (BSS, ABS and M6502) just advance the location
814 // counter appropriately. In TDB sections, deposit (possibly large) chunks
815 //of zeroed memory....
821 just_bss = 1; // No data deposited (8-bit CPU mode)
825 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED|ABS), NULL);
834 // dc.b, dc.w / dc, dc.l
844 int movei = 0; // movei flag for dc.i
846 if ((scattr & SBSS) != 0)
847 return error("illegal initialization of section");
849 if ((siz != SIZB) && (sloc & 1))
854 // dc.b 'string' [,] ...
855 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
857 i = strlen((const char*)tok[1]);
859 if ((challoc - ch_size) < i)
862 for(p=(char *)tok[1]; *p!=EOS; ++p)
877 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
880 tdb = (WORD)(eattr & TDB);
881 defined = (WORD)(eattr & DEFINED);
883 if ((challoc - ch_size) < 4)
891 fixup(FU_BYTE|FU_SEXT, sloc, exprbuf);
897 return error("non-absolute byte value");
899 if (eval + 0x100 >= 0x200)
900 return error(range_error);
910 fixup(FU_WORD|FU_SEXT, sloc, exprbuf);
916 rmark(cursect, sloc, tdb, MWORD, NULL);
918 if (eval + 0x10000 >= 0x20000)
919 return error(range_error);
921 // Deposit 68000 or 6502 (byte-reversed) word
930 fixup(FU_LONG|FU_MOVEI, sloc, exprbuf);
932 fixup(FU_LONG, sloc, exprbuf);
939 rmark(cursect, sloc, tdb, MLONG, NULL);
942 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
960 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
967 if ((scattr & SBSS) != 0)
968 return error("illegal initialization of section");
970 if (abs_expr(&evalc) != OK)
974 return error("missing comma");
976 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
979 if ((siz != SIZB) && (sloc & 1))
982 dep_block(evalc, siz, eval, eattr, exprbuf);
988 // Generalized initialization directive
990 // .init[.siz] [#count,] expression [.size] , ...
992 // The size suffix on the ".init" directive becomes the default size of the
993 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
994 // and an expression, it specifies a repeat count. The value to be deposited
995 // may be followed by a size suffix, which overrides the default size.
997 int d_init(WORD def_siz)
1004 if ((scattr & SBSS) != 0)
1005 return error(".init not permitted in BSS or ABS");
1008 return error("directive forbidden in gpu/dsp mode");
1012 // Get repeat count (defaults to 1)
1017 if (abs_expr(&count) != OK)
1021 return error(comma_error);
1026 // Evaluate expression to deposit
1027 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1030 switch ((int)*tok++)
1031 { // Determine size of object to deposit
1032 case DOTB: siz = SIZB; break;
1033 case DOTW: siz = SIZB; break;
1034 case DOTL: siz = SIZL; break;
1041 dep_block(count, siz, eval, eattr, exprbuf);
1051 return error(comma_error);
1058 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1060 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1065 tdb = (WORD)(eattr & TDB);
1066 defined = (WORD)(eattr & DEFINED);
1070 if ((challoc - ch_size) < 4)
1078 fixup(FU_BYTE|FU_SEXT, sloc, exprbuf);
1084 return error("non-absolute byte value");
1086 if (eval + 0x100 >= 0x200)
1087 return error(range_error);
1097 fixup(FU_WORD|FU_SEXT, sloc, exprbuf);
1103 rmark(cursect, sloc, tdb, MWORD, NULL);
1105 if (eval + 0x10000 >= 0x20000)
1106 return error(range_error);
1108 // Deposit 68000 or 6502 (byte-reversed) word
1116 fixup(FU_LONG, sloc, exprbuf);
1122 rmark(cursect, sloc, tdb, MLONG, NULL);
1136 // .comm symbol, size
1145 return error("missing symbol");
1150 if (*p == '.') // Cannot .comm a local symbol
1151 return error(locgl_error);
1153 if ((sym = lookup(p, LABEL, 0)) == NULL)
1154 sym = newsym(p, LABEL, 0);
1157 if (sym->sattr & DEFINED)
1158 return error(".comm symbol already defined");
1161 sym->sattr = GLOBAL|COMMON|BSS;
1164 return error(comma_error);
1166 if (abs_expr(&eval) != OK) // Parse size of common region
1169 sym->svalue = eval; // Install common symbol's size
1176 // .list - Turn listing on
1188 // .nlist - Turn listing off
1200 // .68000 - Back to 68000 TEXT segment
1206 // Switching from gpu/dsp sections should reset any ORG'd Address
1216 // .gpu - Switch to GPU Assembler
1220 if ((cursect != TEXT) && (cursect != DATA))
1222 error(".gpu can only be used in the TEXT or DATA segments");
1226 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1233 rgpu = 1; // Set GPU assembly
1234 rdsp = 0; // Unset DSP assembly
1235 regbank = BANK_N; // Set no default register bank
1243 // GPU Main Code Directive
1248 if ((cursect != TEXT) && (cursect != DATA))
1250 error(".gpumain can only be used in the TEXT or DATA segments");
1254 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1261 rgpu = 1; // Set GPU assembly
1262 rdsp = 0; // Unset DSP assembly
1263 regbank = BANK_N; // Set no default register bank
1264 in_main = 1; // Enable main code execution rules
1271 // .dsp - Switch to DSP Assembler
1275 if ((cursect != TEXT) && (cursect != DATA))
1277 error(".dsp can only be used in the TEXT or DATA segments");
1281 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1288 rdsp = 1; // Set DSP assembly
1289 rgpu = 0; // Unset GPU assembly
1290 regbank = BANK_N; // Set no default register bank
1298 // .cargs [#offset], symbol[.size], ...
1300 // Lists of registers may also be mentioned; they just take up space. Good for
1301 // "documentation" purposes.
1303 // .cargs a6,.arg1, .arg2, .arg3...
1305 // The symbols are ABS and EQUATED.
1317 return error("directive forbidden in gpu/dsp mode");
1323 if (abs_expr(&eval) != OK)
1326 if (*tok == ',') // Eat comma if it's there
1343 sy = lookup(p, LABEL, env);
1347 sy = newsym(p, LABEL, env);
1350 else if (sy->sattr & DEFINED)
1351 return errors("multiply-defined label '%s'", p);
1353 // Put symbol in "order of definition" list
1354 if (!(sy->sattr & SDECLLIST))
1357 sy->sattr |= ABS|DEFINED|EQUATED;
1374 if (*tok >= KW_D0 && *tok <= KW_A7)
1376 if (reglist(&rlist) < 0)
1379 for(i=0; i++<16; rlist>>=1)
1400 return error(".cargs syntax");
1412 // Undefine a macro - .undefmac macname [, macname...]
1414 int undmac1(char * p)
1418 // If the macro symbol exists, cause it to dissappear
1419 if ((sy = lookup(p, MACRO, 0)) != NULL)
1420 sy->stype = (BYTE)SY_UNDEF;