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
25 TOKEN exprbuf[128]; // Expression buffer
26 SYM * symbolPtr[1000000]; // Symbol pointers table
27 static long unused; // For supressing 'write' warnings
30 // Directive handler table
46 d_undmac, // 14 undefmac
53 d_incbin, // 21 .incbin //load
54 d_unimpl, // 22 disable
55 d_unimpl, // 23 enable
57 d_regbank0, // 25 .regbank0
58 d_regbank1, // 26 .regbank1
60 d_assert, // 28 assert
62 d_unimpl, // 30* endif
65 d_include, // 33 include
67 d_unimpl, // 35* macro
68 ExitMacro, // 36* exitm
74 d_dphrase, // 42 struct
77 d_subttl, // 45 subttl
81 d_noclear, // 49 .noclear
82 d_equrundef, // 50 .equrundef/.regundef
83 d_ccundef, // 51 .ccundef
85 d_cstruct, // 53 .cstruct
86 d_jpad, // 54 .jpad (deprecated)
87 d_nojpad, // 55 .nojpad (deprecated)
88 d_gpumain, // 56 .gpumain (deprecated)
100 return error(".org permitted only in gpu/dsp section");
104 if (abs_expr(&address) == ERROR)
106 error("cannot determine org'd address");
122 char prntstr[LNSIZ]; // String for PRINT directive
123 char format[LNSIZ]; // Format for PRINT directive
124 int formatting = 0; // Formatting on/off
125 int wordlong = 0; // WORD = 0, LONG = 1
126 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
128 VALUE eval; // Expression value
129 WORD eattr; // Expression attributes
130 SYM * esym; // External symbol involved in expr.
131 TOKEN r_expr[EXPRSIZE];
138 // sprintf(prntstr, "%s", (char *)tok[1]);
139 sprintf(prntstr, "%s", string[tok[1]]);
140 printf("%s", prntstr);
143 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
150 if (tok[1] != SYMBOL)
153 // strcpy(prntstr, (char *)tok[2]);
154 strcpy(prntstr, string[tok[2]]);
158 case 'l': case 'L': wordlong = 1; break;
159 case 'w': case 'W': wordlong = 0; break;
160 case 'x': case 'X': outtype = 0; break;
161 case 'd': case 'D': outtype = 1; break;
162 case 'u': case 'U': outtype = 2; break;
164 error("unknown print format flag");
174 if (expr(r_expr, &eval, &eattr, &esym) != OK)
180 case 0: strcpy(format, "%X"); break;
181 case 1: strcpy(format, "%d" ); break;
182 case 2: strcpy(format, "%u" ); break;
186 sprintf(prntstr, format, eval);
188 sprintf(prntstr, format, eval & 0xFFFF);
190 printf("%s", prntstr);
193 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
210 error("illegal print token");
216 // Undefine an Equated Condition Code
222 // Check that we are in a RISC section
225 error(".ccundef must be defined in .gpu/.dsp section");
231 // error(syntax_error);
232 error("syntax error; expected symbol");
236 // ccname = lookup((char *)tok[1], LABEL, 0);
237 ccname = lookup(string[tok[1]], LABEL, 0);
239 // Make sure symbol is a valid ccdef
240 if (!ccname || !(ccname->sattre & EQUATEDCC))
242 error("invalid equated condition name specified");
246 ccname->sattre |= UNDEF_CC;
253 // Undefine an Equated Register
255 int d_equrundef(void)
259 // Check that we are in a RISC section
261 return error(".equrundef/.regundef must be defined in .gpu/.dsp section");
265 // Skip preceeding or seperating commas (if any)
269 // Check we are dealing with a symbol
271 return error("syntax error; expected symbol");
273 // Lookup and undef if equated register
274 regname = lookup(string[tok[1]], LABEL, 0);
276 if (regname && (regname->sattre & EQUATEDREG))
277 regname->sattre |= UNDEF_EQUR;
279 // Skip over symbol token and address
288 // Do Not Allow the Use of the CLR.L Opcode
297 // Include Binary File
303 long pos, size, bytesRead;
306 // Check to see if we're in BSS, and, if so, throw an error
309 errors("cannot include binary file \"%s\" in BSS section", string[tok[1]]);
315 // error(syntax_error);
316 error("syntax error; string missing");
320 if ((fd = open(string[tok[1]], _OPEN_INC)) < 0)
322 errors("cannot open include binary file (%s)", string[tok[1]]);
326 size = lseek(fd, 0L, SEEK_END);
327 pos = lseek(fd, 0L, SEEK_SET);
330 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size); }
332 char * fileBuffer = (char *)malloc(size);
333 bytesRead = read(fd, fileBuffer, size);
335 if (bytesRead != size)
337 sprintf(msg, "was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[tok[1]], size);
342 //#define D_byte(b) {*chptr++=(char)b; ++sloc; ++ch_size; if(orgactive) ++orgaddr;}
343 memcpy(chptr, fileBuffer, size);
358 // Set RISC Register Banks
362 // Set active register bank zero
370 // Set active register bank one
377 // Adjust Location to an EVEN Value
383 if ((scattr & SBSS) == 0)
399 // Adjust Location to an LONG Value
406 i = sloc & ~(val - 1);
409 val = val - (sloc - i);
415 if ((scattr & SBSS) == 0)
433 // Adjust Location to an PHRASE Value
440 i = sloc & ~(val - 1);
443 val = val - (sloc - i);
449 if ((scattr & SBSS) == 0)
467 // Adjust Location to an DPHRASE Value
474 i = sloc & ~(val - 1);
477 val = val - (sloc - i);
483 if ((scattr & SBSS) == 0)
501 // Adjust Location to an QPHRASE Value
508 i = sloc & ~(val - 1);
511 val = val - (sloc - i);
517 if ((scattr & SBSS) == 0)
536 // Do auto-even. This must be called ONLY if 'sloc' is odd.
538 // This is made hairy because, if there was a label on the line, we also have
539 // to adjust its value. This won't work with more than one label on the line,
540 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
541 // the C compiler is VERY paranoid and uses ".even" whenever it can
546 ++sloc; // Bump BSS section
548 D_byte(0) // Deposit 0.b in non-BSS
550 if (lab_sym != NULL) // Bump label if we have to
556 // Unimplemened Directive Error
560 return error("unimplemented directive");
565 // Return absolute (not TDB) and defined expression or return an error
567 int abs_expr(VALUE * a_eval)
571 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
574 if (!(eattr & DEFINED))
575 return error(undef_error);
578 return error(rel_error);
585 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
587 int symlist(int(* func)())
589 const char * em = "symbol list syntax";
596 if ((*func)(string[tok[1]]) != OK)
615 // .include "filename"
625 if (*tok == STRING) // Leave strings ALONE
631 else if (*tok == SYMBOL) // Try to append ".s" to symbols
634 strcpy(buf, (char *)*++tok);
636 strcpy(buf, string[*++tok]);
641 else // Punt if no STRING or SYMBOL
642 return error("missing filename");
644 // Make sure the user didn't try anything like:
645 // .include equates.s
647 return error("extra stuff after filename -- enclose it in quotes");
649 // Attempt to open the include file in the current directory, then (if that
650 // failed) try list of include files passed in the enviroment string or by
652 if ((j = open(fn, 0)) < 0)
654 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
658 if (j > 0 && buf1[j - 1] != SLASHCHAR) // Append path char if necessary
659 strcat(buf1, SLASHSTRING);
663 if ((j = open(buf1, 0)) >= 0)
667 return errors("cannot open: \"%s\"", fn);
677 // .assert expression [, expression...]
684 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
686 if (!(eattr & DEFINED))
687 return error("forward or undefined .assert");
690 return error("assert failure");
702 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
709 return error("cannot .globl local symbol");
711 if ((sy = lookup(p, LABEL, 0)) == NULL)
713 sy = NewSymbol(p, LABEL, 0);
716 //printf("glob1: Making global symbol: attr=%04X, eattr=%08X, %s\n", sy->sattr, sy->sattre, sy->sname);
743 else if (abs_expr(&eval) != OK)
759 return error("directive forbidden in gpu/dsp mode");
774 return error("directive forbidden in gpu/dsp mode");
789 return error("directive forbidden in gpu/dsp mode");
802 // .ds[.size] expression
807 printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc);
811 // This gets kind of stupid. This directive is disallowed in normal 68000
812 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
813 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
815 if (as68_flag == 0 && (scattr & SBSS) == 0)
816 return error(".ds permitted only in BSS");
818 if (siz != SIZB && (sloc & 1)) // Automatic .even
821 if (abs_expr(&eval) != OK)
824 // In non-TDB section (BSS, ABS and M6502) just advance the location
825 // counter appropriately. In TDB sections, deposit (possibly large) chunks
826 //of zeroed memory....
832 just_bss = 1; // No data deposited (8-bit CPU mode)
836 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED | ABS), NULL);
845 // dc.b, dc.w / dc, dc.l
855 int movei = 0; // movei flag for dc.i
857 if ((scattr & SBSS) != 0)
858 return error("illegal initialization of section");
860 if ((siz != SIZB) && (sloc & 1))
865 // dc.b 'string' [,] ...
866 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
868 // i = strlen((const char*)tok[1]);
869 i = strlen(string[tok[1]]);
871 if ((challoc - ch_size) < i)
874 for(p=string[tok[1]]; *p!=EOS; ++p)
889 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
892 tdb = (WORD)(eattr & TDB);
893 defined = (WORD)(eattr & DEFINED);
895 if ((challoc - ch_size) < 4)
903 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
909 return error("non-absolute byte value");
911 if (eval + 0x100 >= 0x200)
912 return error(range_error);
922 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
928 rmark(cursect, sloc, tdb, MWORD, NULL);
930 if (eval + 0x10000 >= 0x20000)
931 return error(range_error);
933 // Deposit 68000 or 6502 (byte-reversed) word
942 AddFixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
944 AddFixup(FU_LONG, sloc, exprbuf);
951 rmark(cursect, sloc, tdb, MLONG, NULL);
954 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
972 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
979 if ((scattr & SBSS) != 0)
980 return error("illegal initialization of section");
982 if (abs_expr(&evalc) != OK)
986 return error("missing comma");
988 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
991 if ((siz != SIZB) && (sloc & 1))
994 dep_block(evalc, siz, eval, eattr, exprbuf);
1000 // Generalized initialization directive
1002 // .init[.siz] [#count,] expression [.size] , ...
1004 // The size suffix on the ".init" directive becomes the default size of the
1005 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1006 // and an expression, it specifies a repeat count. The value to be deposited
1007 // may be followed by a size suffix, which overrides the default size.
1009 int d_init(WORD def_siz)
1016 if ((scattr & SBSS) != 0)
1017 return error(".init not permitted in BSS or ABS");
1020 return error("directive forbidden in gpu/dsp mode");
1024 // Get repeat count (defaults to 1)
1029 if (abs_expr(&count) != OK)
1033 return error(comma_error);
1038 // Evaluate expression to deposit
1039 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1042 switch ((int)*tok++)
1043 { // Determine size of object to deposit
1044 case DOTB: siz = SIZB; break;
1045 case DOTW: siz = SIZB; break;
1046 case DOTL: siz = SIZL; break;
1053 dep_block(count, siz, eval, eattr, exprbuf);
1063 return error(comma_error);
1070 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1072 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1077 tdb = (WORD)(eattr & TDB);
1078 defined = (WORD)(eattr & DEFINED);
1082 if ((challoc - ch_size) < 4)
1090 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1096 return error("non-absolute byte value");
1098 if (eval + 0x100 >= 0x200)
1099 return error(range_error);
1109 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1115 rmark(cursect, sloc, tdb, MWORD, NULL);
1117 if (eval + 0x10000 >= 0x20000)
1118 return error(range_error);
1120 // Deposit 68000 or 6502 (byte-reversed) word
1128 AddFixup(FU_LONG, sloc, exprbuf);
1134 rmark(cursect, sloc, tdb, MLONG, NULL);
1148 // .comm symbol, size
1157 return error("missing symbol");
1159 // p = (char *)tok[1];
1163 if (*p == '.') // Cannot .comm a local symbol
1164 return error(locgl_error);
1166 if ((sym = lookup(p, LABEL, 0)) == NULL)
1167 sym = NewSymbol(p, LABEL, 0);
1170 if (sym->sattr & DEFINED)
1171 return error(".comm symbol already defined");
1174 sym->sattr = GLOBAL | COMMON | BSS;
1177 return error(comma_error);
1179 if (abs_expr(&eval) != OK) // Parse size of common region
1182 sym->svalue = eval; // Install common symbol's size
1189 // .list - Turn listing on
1201 // .nlist - Turn listing off
1213 // .68000 - Back to 68000 TEXT segment
1218 // Switching from gpu/dsp sections should reset any ORG'd Address
1222 SwitchSection(TEXT);
1228 // .gpu - Switch to GPU Assembler
1232 if ((cursect != TEXT) && (cursect != DATA))
1234 error(".gpu can only be used in the TEXT or DATA segments");
1238 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1245 rgpu = 1; // Set GPU assembly
1246 rdsp = 0; // Unset DSP assembly
1247 regbank = BANK_N; // Set no default register bank
1253 // .dsp - Switch to DSP Assembler
1257 if ((cursect != TEXT) && (cursect != DATA))
1259 error(".dsp can only be used in the TEXT or DATA segments");
1263 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1270 rdsp = 1; // Set DSP assembly
1271 rgpu = 0; // Unset GPU assembly
1272 regbank = BANK_N; // Set no default register bank
1278 // .cargs [#offset], symbol[.size], ...
1280 // Lists of registers may also be mentioned; they just take up space. Good for
1281 // "documentation" purposes:
1283 // .cargs a6, .arg1, .arg2, .arg3...
1285 // Symbols thus created are ABS and EQUATED.
1289 VALUE eval = 4; // Default to 4 if no offset specified (to account for
1298 return error("directive forbidden in gpu/dsp mode");
1304 if (abs_expr(&eval) != OK)
1307 // Eat the comma, if it's there
1316 // p = (char *)tok[1];
1321 env = curenv; // Label is local
1323 env = 0; // Label is global
1325 // Set env to either local (dot prefixed) or global scope
1326 env = (*p == '.' ? curenv : 0);
1328 symbol = lookup(p, LABEL, env);
1332 symbol = NewSymbol(p, LABEL, env);
1335 else if (symbol->sattr & DEFINED)
1336 return errors("multiply-defined label '%s'", p);
1338 // Put symbol in "order of definition" list
1339 if (!(symbol->sattr & SDECLLIST))
1340 AddToSymbolOrderList(symbol);
1342 symbol->sattr |= (ABS | DEFINED | EQUATED);
1343 symbol->svalue = eval;
1346 // What this does is eat any dot suffixes attached to a symbol. If
1347 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1348 // there is no dot suffix, it assumes a size of 2.
1360 else if (*tok >= KW_D0 && *tok <= KW_A7)
1362 if (reglist(&rlist) < 0)
1365 // for(i=0; i++<16; rlist>>=1)
1366 for(i=0; i<16; i++, rlist>>=1)
1389 return error(".cargs syntax");
1393 // Eat commas in between each argument, if they exist
1401 // .cstruct [#offset], symbol[.size], ...
1403 // Lists of registers may also be mentioned; they just take up space. Good for
1404 // "documentation" purposes:
1406 // .cstruct a6, .arg1, .arg2, .arg3...
1408 // Symbols thus created are ABS and EQUATED. Note that this is for
1409 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1414 VALUE eval = 0; // Default, if no offset specified, is zero
1422 return error("directive forbidden in gpu/dsp mode");
1428 if (abs_expr(&eval) != OK)
1431 // Eat the comma, if it's there
1440 symbolName = string[tok[1]];
1442 // Set env to either local (dot prefixed) or global scope
1443 env = (symbolName[0] == '.' ? curenv : 0);
1444 symbol = lookup(symbolName, LABEL, env);
1446 // If the symbol wasn't found, then define it. Otherwise, throw an
1450 symbol = NewSymbol(symbolName, LABEL, env);
1453 else if (symbol->sattr & DEFINED)
1454 return errors("multiply-defined label '%s'", symbolName);
1456 // Put symbol in "order of definition" list
1457 if (!(symbol->sattr & SDECLLIST))
1458 AddToSymbolOrderList(symbol);
1462 // Adjust label start address if it's a word or a long, as a byte
1463 // label might have left us on an odd address.
1468 eval += eval & 0x01;
1471 symbol->sattr |= (ABS | DEFINED | EQUATED);
1472 symbol->svalue = eval;
1474 // Check for dot suffixes and adjust space accordingly (longs and
1475 // words on an odd boundary get bumped to the next word aligned
1476 // address). If no suffix, then throw an error.
1489 return error("Symbol missing dot suffix in .cstruct construct");
1494 else if (*tok >= KW_D0 && *tok <= KW_A7)
1496 if (reglist(&rlist) < 0)
1499 for(i=0; i<16; i++, rlist>>=1)
1522 return error(".cstruct syntax");
1526 // Eat commas in between each argument, if they exist
1534 // Undefine a macro - .undefmac macname [, macname...]
1536 int undmac1(char * p)
1538 SYM * symbol = lookup(p, MACRO, 0);
1540 // If the macro symbol exists, cause it to disappear
1541 // if ((sy = lookup(p, MACRO, 0)) != NULL)
1543 symbol->stype = (BYTE)SY_UNDEF;
1558 warn("JPAD directive is deprecated/non-functional");
1565 warn("NOJPAD directive is deprecated/non-functional");
1572 return error("What the hell? Do you think we adhere to the Goof standard?");