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
83 // d_jpad, // 54 .jpad
84 // d_nojpad, // 55 .nojpad
85 // d_fail, // 56 .fail
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 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 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
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
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 ((j = open((char *)tok[1], _OPEN_INC)) >= 0)
323 if ((j = open(string[tok[1]], _OPEN_INC)) >= 0)
325 size = lseek(j, 0L, SEEK_END);
327 pos = lseek(j, 0L, SEEK_SET);
331 printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size);
334 for(i=0; i<size; i++)
337 bytes = read(j, &buf, 1);
343 // errors("cannot open include binary file (%s)", (char *)tok[1]);
344 errors("cannot open include binary file (%s)", string[tok[1]]);
354 // Set RISC Register Banks
358 regbank = BANK_0; // Set active register bank zero
365 regbank = BANK_1; // Set active register bank one
371 // Adjust Location to an EVEN Value
377 if ((scattr & SBSS) == 0)
393 // Adjust Location to an LONG Value
400 i = sloc & ~(val - 1);
403 val = val - (sloc - i);
409 if ((scattr & SBSS) == 0)
427 // Adjust Location to an PHRASE Value
434 i = sloc & ~(val - 1);
437 val = val - (sloc - i);
443 if ((scattr & SBSS) == 0)
461 // Adjust Location to an DPHRASE Value
468 i = sloc & ~(val - 1);
471 val = val - (sloc - i);
477 if ((scattr & SBSS) == 0)
495 // Adjust Location to an QPHRASE Value
502 i = sloc & ~(val - 1);
505 val = val - (sloc - i);
511 if ((scattr & SBSS) == 0)
530 // Do auto-even. This must be called ONLY if 'sloc' is odd.
532 // This is made hairy because, if there was a label on the line, we also have
533 // to adjust its value. This won't work with more than one label on the line,
534 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
535 // the C compiler is VERY paranoid and uses ".even" whenever it can
540 ++sloc; // Bump BSS section
542 D_byte(0) // Deposit 0.b in non-BSS
544 if (lab_sym != NULL) // Bump label if we have to
550 // Unimplemened Directive Error
554 return error("unimplemented directive");
559 // Return absolute (not TDB) and defined expression or return an error
561 int abs_expr(VALUE * a_eval)
565 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
568 if (!(eattr & DEFINED))
569 return error(undef_error);
572 return error(rel_error);
579 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
581 int symlist(int(* func)())
583 char * em = "symbol list syntax";
591 if ((*func)(tok[1]) != OK)
593 if ((*func)(string[tok[1]]) != OK)
613 // .include "filename"
623 if (*tok == STRING) // Leave strings ALONE
629 else if (*tok == SYMBOL) // Try to append ".s" to symbols
632 strcpy(buf, (char *)*++tok);
634 strcpy(buf, string[*++tok]);
639 else // Punt if no STRING or SYMBOL
640 return error("missing filename");
642 // Make sure the user didn't try anything like:
643 // .include equates.s
645 return error("extra stuff after filename -- enclose it in quotes");
647 // Attempt to open the include file in the current directory, then (if that
648 // failed) try list of include files passed in the enviroment string or by
650 if ((j = open(fn, 0)) < 0)
652 for(i=0; nthpath("RMACPATH", i, buf1)!=0; ++i)
656 if (j > 0 && buf1[j-1] != SLASHCHAR) // Append path char if necessary
657 strcat(buf1, SLASHSTRING);
661 if ((j = open(buf1, 0)) >= 0)
665 return errors("cannot open: \"%s\"", fn);
675 // .assert expression [, expression...]
682 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
684 if (!(eattr & DEFINED))
685 return error("forward or undefined .assert");
688 return error("assert failure");
700 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
707 return error("cannot .globl local symbol");
709 if ((sy = lookup(p, LABEL, 0)) == NULL)
711 sy = NewSymbol(p, LABEL, 0);
740 else if (abs_expr(&eval) != OK)
756 return error("directive forbidden in gpu/dsp mode");
771 return error("directive forbidden in gpu/dsp mode");
786 return error("directive forbidden in gpu/dsp mode");
799 // .ds[.size] expression
805 // This gets kind of stupid. This directive is disallowed in normal 68000
806 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
807 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
809 if (as68_flag == 0 && (scattr & SBSS) == 0)
810 return error(".ds permitted only in BSS");
812 if (siz != SIZB && (sloc & 1)) // Automatic .even
815 if (abs_expr(&eval) != OK)
818 // In non-TDB section (BSS, ABS and M6502) just advance the location
819 // counter appropriately. In TDB sections, deposit (possibly large) chunks
820 //of zeroed memory....
826 just_bss = 1; // No data deposited (8-bit CPU mode)
830 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED|ABS), NULL);
839 // dc.b, dc.w / dc, dc.l
849 int movei = 0; // movei flag for dc.i
851 if ((scattr & SBSS) != 0)
852 return error("illegal initialization of section");
854 if ((siz != SIZB) && (sloc & 1))
859 // dc.b 'string' [,] ...
860 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
862 // i = strlen((const char*)tok[1]);
863 i = strlen(string[tok[1]]);
865 if ((challoc - ch_size) < i)
868 for(p=string[tok[1]]; *p!=EOS; ++p)
883 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
886 tdb = (WORD)(eattr & TDB);
887 defined = (WORD)(eattr & DEFINED);
889 if ((challoc - ch_size) < 4)
897 fixup(FU_BYTE|FU_SEXT, sloc, exprbuf);
903 return error("non-absolute byte value");
905 if (eval + 0x100 >= 0x200)
906 return error(range_error);
916 fixup(FU_WORD|FU_SEXT, sloc, exprbuf);
922 rmark(cursect, sloc, tdb, MWORD, NULL);
924 if (eval + 0x10000 >= 0x20000)
925 return error(range_error);
927 // Deposit 68000 or 6502 (byte-reversed) word
936 fixup(FU_LONG|FU_MOVEI, sloc, exprbuf);
938 fixup(FU_LONG, sloc, exprbuf);
945 rmark(cursect, sloc, tdb, MLONG, NULL);
948 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
966 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
973 if ((scattr & SBSS) != 0)
974 return error("illegal initialization of section");
976 if (abs_expr(&evalc) != OK)
980 return error("missing comma");
982 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
985 if ((siz != SIZB) && (sloc & 1))
988 dep_block(evalc, siz, eval, eattr, exprbuf);
994 // Generalized initialization directive
996 // .init[.siz] [#count,] expression [.size] , ...
998 // The size suffix on the ".init" directive becomes the default size of the
999 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1000 // and an expression, it specifies a repeat count. The value to be deposited
1001 // may be followed by a size suffix, which overrides the default size.
1003 int d_init(WORD def_siz)
1010 if ((scattr & SBSS) != 0)
1011 return error(".init not permitted in BSS or ABS");
1014 return error("directive forbidden in gpu/dsp mode");
1018 // Get repeat count (defaults to 1)
1023 if (abs_expr(&count) != OK)
1027 return error(comma_error);
1032 // Evaluate expression to deposit
1033 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1036 switch ((int)*tok++)
1037 { // Determine size of object to deposit
1038 case DOTB: siz = SIZB; break;
1039 case DOTW: siz = SIZB; break;
1040 case DOTL: siz = SIZL; break;
1047 dep_block(count, siz, eval, eattr, exprbuf);
1057 return error(comma_error);
1064 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1066 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1071 tdb = (WORD)(eattr & TDB);
1072 defined = (WORD)(eattr & DEFINED);
1076 if ((challoc - ch_size) < 4)
1084 fixup(FU_BYTE|FU_SEXT, sloc, exprbuf);
1090 return error("non-absolute byte value");
1092 if (eval + 0x100 >= 0x200)
1093 return error(range_error);
1103 fixup(FU_WORD|FU_SEXT, sloc, exprbuf);
1109 rmark(cursect, sloc, tdb, MWORD, NULL);
1111 if (eval + 0x10000 >= 0x20000)
1112 return error(range_error);
1114 // Deposit 68000 or 6502 (byte-reversed) word
1122 fixup(FU_LONG, sloc, exprbuf);
1128 rmark(cursect, sloc, tdb, MLONG, NULL);
1142 // .comm symbol, size
1151 return error("missing symbol");
1153 // p = (char *)tok[1];
1157 if (*p == '.') // Cannot .comm a local symbol
1158 return error(locgl_error);
1160 if ((sym = lookup(p, LABEL, 0)) == NULL)
1161 sym = NewSymbol(p, LABEL, 0);
1164 if (sym->sattr & DEFINED)
1165 return error(".comm symbol already defined");
1168 sym->sattr = GLOBAL | COMMON | BSS;
1171 return error(comma_error);
1173 if (abs_expr(&eval) != OK) // Parse size of common region
1176 sym->svalue = eval; // Install common symbol's size
1183 // .list - Turn listing on
1195 // .nlist - Turn listing off
1207 // .68000 - Back to 68000 TEXT segment
1213 // Switching from gpu/dsp sections should reset any ORG'd Address
1223 // .gpu - Switch to GPU Assembler
1227 if ((cursect != TEXT) && (cursect != DATA))
1229 error(".gpu can only be used in the TEXT or DATA segments");
1233 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1240 rgpu = 1; // Set GPU assembly
1241 rdsp = 0; // Unset DSP assembly
1242 regbank = BANK_N; // Set no default register bank
1250 // .dsp - Switch to DSP Assembler
1254 if ((cursect != TEXT) && (cursect != DATA))
1256 error(".dsp can only be used in the TEXT or DATA segments");
1260 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1267 rdsp = 1; // Set DSP assembly
1268 rgpu = 0; // Unset GPU assembly
1269 regbank = BANK_N; // Set no default register bank
1277 // .cargs [#offset], symbol[.size], ...
1279 // Lists of registers may also be mentioned; they just take up space. Good for
1280 // "documentation" purposes.
1282 // .cargs a6,.arg1, .arg2, .arg3...
1284 // The symbols are ABS and EQUATED.
1296 return error("directive forbidden in gpu/dsp mode");
1302 if (abs_expr(&eval) != OK)
1305 if (*tok == ',') // Eat comma if it's there
1315 // p = (char *)tok[1];
1323 sy = lookup(p, LABEL, env);
1327 sy = NewSymbol(p, LABEL, env);
1330 else if (sy->sattr & DEFINED)
1331 return errors("multiply-defined label '%s'", p);
1333 // Put symbol in "order of definition" list
1334 if (!(sy->sattr & SDECLLIST))
1337 sy->sattr |= ABS|DEFINED|EQUATED;
1354 if (*tok >= KW_D0 && *tok <= KW_A7)
1356 if (reglist(&rlist) < 0)
1359 for(i=0; i++<16; rlist>>=1)
1380 return error(".cargs syntax");
1392 // Undefine a macro - .undefmac macname [, macname...]
1394 int undmac1(char * p)
1398 // If the macro symbol exists, cause it to dissappear
1399 if ((sy = lookup(p, MACRO, 0)) != NULL)
1400 sy->stype = (BYTE)SY_UNDEF;