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_cstruct, // 53 .cstruct
94 return error(".org permitted only in gpu/dsp section");
98 if (abs_expr(&address) == ERROR)
100 error("cannot determine org'd address");
116 char prntstr[LNSIZ]; // String for PRINT directive
117 char format[LNSIZ]; // Format for PRINT directive
118 int formatting = 0; // Formatting on/off
119 int wordlong = 0; // WORD = 0, LONG = 1
120 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
122 VALUE eval; // Expression value
123 WORD eattr; // Expression attributes
124 SYM * esym; // External symbol involved in expr.
125 TOKEN r_expr[EXPRSIZE];
132 // sprintf(prntstr, "%s", (char *)tok[1]);
133 sprintf(prntstr, "%s", string[tok[1]]);
134 printf("%s", prntstr);
137 write(list_fd, prntstr, (LONG)strlen(prntstr));
144 if (tok[1] != SYMBOL)
147 // strcpy(prntstr, (char *)tok[2]);
148 strcpy(prntstr, string[tok[2]]);
152 case 'l': case 'L': wordlong = 1; break;
153 case 'w': case 'W': wordlong = 0; break;
154 case 'x': case 'X': outtype = 0; break;
155 case 'd': case 'D': outtype = 1; break;
156 case 'u': case 'U': outtype = 2; break;
158 error("unknown print format flag");
168 if (expr(r_expr, &eval, &eattr, &esym) != OK)
174 case 0: strcpy(format, "%X"); break;
175 case 1: strcpy(format, "%d" ); break;
176 case 2: strcpy(format, "%u" ); break;
180 sprintf(prntstr, format, eval);
182 sprintf(prntstr, format, eval & 0xFFFF);
184 printf("%s", prntstr);
187 write(list_fd, prntstr, (LONG)strlen(prntstr));
204 error("illegal print token");
210 // Undefine an Equated Condition Code
216 // Check that we are in a RISC section
219 error(".ccundef must be defined in .gpu/.dsp section");
229 // ccname = lookup((char *)tok[1], LABEL, 0);
230 ccname = lookup(string[tok[1]], LABEL, 0);
232 // Make sure symbol is a valid ccdef
233 if (!ccname || !(ccname->sattre & EQUATEDCC))
235 error("invalid equated condition name specified");
239 ccname->sattre |= UNDEF_CC;
246 // Undefine an Equated Register
248 int d_equrundef(void)
252 // Check that we are in a RISC section
255 error(".equrundef/.regundef must be defined in .gpu/.dsp section");
261 // Skip preceeding or seperating commas
265 // Check we are dealing with a symbol
272 // Lookup and undef if equated register
273 // regname = lookup((char *)tok[1], LABEL, 0);
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]]);
320 if ((j = open(string[tok[1]], _OPEN_INC)) >= 0)
322 size = lseek(j, 0L, SEEK_END);
324 pos = lseek(j, 0L, SEEK_SET);
328 printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size);
331 for(i=0; i<size; i++)
334 bytes = read(j, &buf, 1);
340 errors("cannot open include binary file (%s)", string[tok[1]]);
344 if ((fd = open(string[tok[1]], _OPEN_INC)) < 0)
346 errors("cannot open include binary file (%s)", string[tok[1]]);
350 size = lseek(fd, 0L, SEEK_END);
351 pos = lseek(fd, 0L, SEEK_SET);
356 printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size);
360 for(i=0; i<size; i++)
363 bytes = read(j, &buf, 1);
367 char * fileBuffer = (char *)malloc(size);
368 bytesRead = read(fd, fileBuffer, size);
370 if (bytesRead != size)
372 sprintf(msg, "was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[tok[1]], size);
377 //#define D_byte(b) {*chptr++=(char)b; ++sloc; ++ch_size; if(orgactive) ++orgaddr;}
378 memcpy(chptr, fileBuffer, size);
396 // Set RISC Register Banks
400 // Set active register bank zero
408 // Set active register bank one
415 // Adjust Location to an EVEN Value
421 if ((scattr & SBSS) == 0)
437 // Adjust Location to an LONG Value
444 i = sloc & ~(val - 1);
447 val = val - (sloc - i);
453 if ((scattr & SBSS) == 0)
471 // Adjust Location to an PHRASE Value
478 i = sloc & ~(val - 1);
481 val = val - (sloc - i);
487 if ((scattr & SBSS) == 0)
505 // Adjust Location to an DPHRASE Value
512 i = sloc & ~(val - 1);
515 val = val - (sloc - i);
521 if ((scattr & SBSS) == 0)
539 // Adjust Location to an QPHRASE Value
546 i = sloc & ~(val - 1);
549 val = val - (sloc - i);
555 if ((scattr & SBSS) == 0)
574 // Do auto-even. This must be called ONLY if 'sloc' is odd.
576 // This is made hairy because, if there was a label on the line, we also have
577 // to adjust its value. This won't work with more than one label on the line,
578 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
579 // the C compiler is VERY paranoid and uses ".even" whenever it can
584 ++sloc; // Bump BSS section
586 D_byte(0) // Deposit 0.b in non-BSS
588 if (lab_sym != NULL) // Bump label if we have to
594 // Unimplemened Directive Error
598 return error("unimplemented directive");
603 // Return absolute (not TDB) and defined expression or return an error
605 int abs_expr(VALUE * a_eval)
609 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
612 if (!(eattr & DEFINED))
613 return error(undef_error);
616 return error(rel_error);
623 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
625 int symlist(int(* func)())
627 char * em = "symbol list syntax";
635 if ((*func)(tok[1]) != OK)
637 if ((*func)(string[tok[1]]) != OK)
657 // .include "filename"
667 if (*tok == STRING) // Leave strings ALONE
673 else if (*tok == SYMBOL) // Try to append ".s" to symbols
676 strcpy(buf, (char *)*++tok);
678 strcpy(buf, string[*++tok]);
683 else // Punt if no STRING or SYMBOL
684 return error("missing filename");
686 // Make sure the user didn't try anything like:
687 // .include equates.s
689 return error("extra stuff after filename -- enclose it in quotes");
691 // Attempt to open the include file in the current directory, then (if that
692 // failed) try list of include files passed in the enviroment string or by
694 if ((j = open(fn, 0)) < 0)
696 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
700 if (j > 0 && buf1[j - 1] != SLASHCHAR) // Append path char if necessary
701 strcat(buf1, SLASHSTRING);
705 if ((j = open(buf1, 0)) >= 0)
709 return errors("cannot open: \"%s\"", fn);
719 // .assert expression [, expression...]
726 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
728 if (!(eattr & DEFINED))
729 return error("forward or undefined .assert");
732 return error("assert failure");
744 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
751 return error("cannot .globl local symbol");
753 if ((sy = lookup(p, LABEL, 0)) == NULL)
755 sy = NewSymbol(p, LABEL, 0);
784 else if (abs_expr(&eval) != OK)
800 return error("directive forbidden in gpu/dsp mode");
815 return error("directive forbidden in gpu/dsp mode");
830 return error("directive forbidden in gpu/dsp mode");
843 // .ds[.size] expression
848 printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc);
852 // This gets kind of stupid. This directive is disallowed in normal 68000
853 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
854 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
856 if (as68_flag == 0 && (scattr & SBSS) == 0)
857 return error(".ds permitted only in BSS");
859 if (siz != SIZB && (sloc & 1)) // Automatic .even
862 if (abs_expr(&eval) != OK)
865 // In non-TDB section (BSS, ABS and M6502) just advance the location
866 // counter appropriately. In TDB sections, deposit (possibly large) chunks
867 //of zeroed memory....
873 just_bss = 1; // No data deposited (8-bit CPU mode)
877 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED | ABS), NULL);
886 // dc.b, dc.w / dc, dc.l
896 int movei = 0; // movei flag for dc.i
898 if ((scattr & SBSS) != 0)
899 return error("illegal initialization of section");
901 if ((siz != SIZB) && (sloc & 1))
906 // dc.b 'string' [,] ...
907 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
909 // i = strlen((const char*)tok[1]);
910 i = strlen(string[tok[1]]);
912 if ((challoc - ch_size) < i)
915 for(p=string[tok[1]]; *p!=EOS; ++p)
930 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
933 tdb = (WORD)(eattr & TDB);
934 defined = (WORD)(eattr & DEFINED);
936 if ((challoc - ch_size) < 4)
944 fixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
950 return error("non-absolute byte value");
952 if (eval + 0x100 >= 0x200)
953 return error(range_error);
963 fixup(FU_WORD | FU_SEXT, sloc, exprbuf);
969 rmark(cursect, sloc, tdb, MWORD, NULL);
971 if (eval + 0x10000 >= 0x20000)
972 return error(range_error);
974 // Deposit 68000 or 6502 (byte-reversed) word
983 fixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
985 fixup(FU_LONG, sloc, exprbuf);
992 rmark(cursect, sloc, tdb, MLONG, NULL);
995 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
1013 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1020 if ((scattr & SBSS) != 0)
1021 return error("illegal initialization of section");
1023 if (abs_expr(&evalc) != OK)
1027 return error("missing comma");
1029 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1032 if ((siz != SIZB) && (sloc & 1))
1035 dep_block(evalc, siz, eval, eattr, exprbuf);
1041 // Generalized initialization directive
1043 // .init[.siz] [#count,] expression [.size] , ...
1045 // The size suffix on the ".init" directive becomes the default size of the
1046 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1047 // and an expression, it specifies a repeat count. The value to be deposited
1048 // may be followed by a size suffix, which overrides the default size.
1050 int d_init(WORD def_siz)
1057 if ((scattr & SBSS) != 0)
1058 return error(".init not permitted in BSS or ABS");
1061 return error("directive forbidden in gpu/dsp mode");
1065 // Get repeat count (defaults to 1)
1070 if (abs_expr(&count) != OK)
1074 return error(comma_error);
1079 // Evaluate expression to deposit
1080 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1083 switch ((int)*tok++)
1084 { // Determine size of object to deposit
1085 case DOTB: siz = SIZB; break;
1086 case DOTW: siz = SIZB; break;
1087 case DOTL: siz = SIZL; break;
1094 dep_block(count, siz, eval, eattr, exprbuf);
1104 return error(comma_error);
1111 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1113 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1118 tdb = (WORD)(eattr & TDB);
1119 defined = (WORD)(eattr & DEFINED);
1123 if ((challoc - ch_size) < 4)
1131 fixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1137 return error("non-absolute byte value");
1139 if (eval + 0x100 >= 0x200)
1140 return error(range_error);
1150 fixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1156 rmark(cursect, sloc, tdb, MWORD, NULL);
1158 if (eval + 0x10000 >= 0x20000)
1159 return error(range_error);
1161 // Deposit 68000 or 6502 (byte-reversed) word
1169 fixup(FU_LONG, sloc, exprbuf);
1175 rmark(cursect, sloc, tdb, MLONG, NULL);
1189 // .comm symbol, size
1198 return error("missing symbol");
1200 // p = (char *)tok[1];
1204 if (*p == '.') // Cannot .comm a local symbol
1205 return error(locgl_error);
1207 if ((sym = lookup(p, LABEL, 0)) == NULL)
1208 sym = NewSymbol(p, LABEL, 0);
1211 if (sym->sattr & DEFINED)
1212 return error(".comm symbol already defined");
1215 sym->sattr = GLOBAL | COMMON | BSS;
1218 return error(comma_error);
1220 if (abs_expr(&eval) != OK) // Parse size of common region
1223 sym->svalue = eval; // Install common symbol's size
1230 // .list - Turn listing on
1242 // .nlist - Turn listing off
1254 // .68000 - Back to 68000 TEXT segment
1259 // Switching from gpu/dsp sections should reset any ORG'd Address
1269 // .gpu - Switch to GPU Assembler
1273 if ((cursect != TEXT) && (cursect != DATA))
1275 error(".gpu can only be used in the TEXT or DATA segments");
1279 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1286 rgpu = 1; // Set GPU assembly
1287 rdsp = 0; // Unset DSP assembly
1288 regbank = BANK_N; // Set no default register bank
1294 // .dsp - Switch to DSP Assembler
1298 if ((cursect != TEXT) && (cursect != DATA))
1300 error(".dsp can only be used in the TEXT or DATA segments");
1304 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1311 rdsp = 1; // Set DSP assembly
1312 rgpu = 0; // Unset GPU assembly
1313 regbank = BANK_N; // Set no default register bank
1319 // .cargs [#offset], symbol[.size], ...
1321 // Lists of registers may also be mentioned; they just take up space. Good for
1322 // "documentation" purposes:
1324 // .cargs a6, .arg1, .arg2, .arg3...
1326 // Symbols thus created are ABS and EQUATED.
1330 VALUE eval = 4; // Default to 4 if no offset specified (to account for
1339 return error("directive forbidden in gpu/dsp mode");
1345 if (abs_expr(&eval) != OK)
1348 // Eat the comma, if it's there
1357 // p = (char *)tok[1];
1362 env = curenv; // Label is local
1364 env = 0; // Label is global
1366 // Set env to either local (dot prefixed) or global scope
1367 env = (*p == '.' ? curenv : 0);
1369 symbol = lookup(p, LABEL, env);
1373 symbol = NewSymbol(p, LABEL, env);
1376 else if (symbol->sattr & DEFINED)
1377 return errors("multiply-defined label '%s'", p);
1379 // Put symbol in "order of definition" list
1380 if (!(symbol->sattr & SDECLLIST))
1383 symbol->sattr |= (ABS | DEFINED | EQUATED);
1384 symbol->svalue = eval;
1387 // What this does is eat any dot suffixes attached to a symbol. If
1388 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1389 // there is no dot suffix, it assumes a size of 2.
1401 else if (*tok >= KW_D0 && *tok <= KW_A7)
1403 if (reglist(&rlist) < 0)
1406 // for(i=0; i++<16; rlist>>=1)
1407 for(i=0; i<16; i++, rlist>>=1)
1430 return error(".cargs syntax");
1434 // Eat commas in between each argument, if they exist
1442 // .cstruct [#offset], symbol[.size], ...
1444 // Lists of registers may also be mentioned; they just take up space. Good for
1445 // "documentation" purposes:
1447 // .cstruct a6, .arg1, .arg2, .arg3...
1449 // Symbols thus created are ABS and EQUATED. Note that this is for
1450 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1455 VALUE eval = 0; // Default, if no offset specified, is zero
1463 return error("directive forbidden in gpu/dsp mode");
1469 if (abs_expr(&eval) != OK)
1472 // Eat the comma, if it's there
1481 symbolName = string[tok[1]];
1483 // Set env to either local (dot prefixed) or global scope
1484 env = (symbolName[0] == '.' ? curenv : 0);
1485 symbol = lookup(symbolName, LABEL, env);
1487 // If the symbol wasn't found, then define it. Otherwise, throw an
1491 symbol = NewSymbol(symbolName, LABEL, env);
1494 else if (symbol->sattr & DEFINED)
1495 return errors("multiply-defined label '%s'", symbolName);
1497 // Put symbol in "order of definition" list
1498 if (!(symbol->sattr & SDECLLIST))
1503 // Adjust label start address if it's a word or a long, as a byte
1504 // label might have left us on an odd address.
1509 eval += eval & 0x01;
1512 symbol->sattr |= (ABS | DEFINED | EQUATED);
1513 symbol->svalue = eval;
1515 // Check for dot suffixes and adjust space accordingly (longs and
1516 // words on an odd boundary get bumped to the next word aligned
1517 // address). If no suffix, then throw an error.
1530 return error("Symbol missing dot suffix in .cstruct construct");
1535 else if (*tok >= KW_D0 && *tok <= KW_A7)
1537 if (reglist(&rlist) < 0)
1540 for(i=0; i<16; i++, rlist>>=1)
1563 return error(".cstruct syntax");
1567 // Eat commas in between each argument, if they exist
1575 // Undefine a macro - .undefmac macname [, macname...]
1577 int undmac1(char * p)
1579 SYM * symbol = lookup(p, MACRO, 0);
1581 // If the macro symbol exists, cause it to disappear
1582 // if ((sy = lookup(p, MACRO, 0)) != NULL)
1584 symbol->stype = (BYTE)SY_UNDEF;