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)
99 return error(".org permitted only in gpu/dsp section");
103 if (abs_expr(&address) == ERROR)
105 error("cannot determine org'd address");
121 char prntstr[LNSIZ]; // String for PRINT directive
122 char format[LNSIZ]; // Format for PRINT directive
123 int formatting = 0; // Formatting on/off
124 int wordlong = 0; // WORD = 0, LONG = 1
125 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
127 VALUE eval; // Expression value
128 WORD eattr; // Expression attributes
129 SYM * esym; // External symbol involved in expr.
130 TOKEN r_expr[EXPRSIZE];
137 // sprintf(prntstr, "%s", (char *)tok[1]);
138 sprintf(prntstr, "%s", string[tok[1]]);
139 printf("%s", prntstr);
142 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
149 if (tok[1] != SYMBOL)
152 // strcpy(prntstr, (char *)tok[2]);
153 strcpy(prntstr, string[tok[2]]);
157 case 'l': case 'L': wordlong = 1; break;
158 case 'w': case 'W': wordlong = 0; break;
159 case 'x': case 'X': outtype = 0; break;
160 case 'd': case 'D': outtype = 1; break;
161 case 'u': case 'U': outtype = 2; break;
163 error("unknown print format flag");
173 if (expr(r_expr, &eval, &eattr, &esym) != OK)
179 case 0: strcpy(format, "%X"); break;
180 case 1: strcpy(format, "%d" ); break;
181 case 2: strcpy(format, "%u" ); break;
185 sprintf(prntstr, format, eval);
187 sprintf(prntstr, format, eval & 0xFFFF);
189 printf("%s", prntstr);
192 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
209 error("illegal print token");
215 // Undefine an Equated Condition Code
221 // Check that we are in a RISC section
224 error(".ccundef must be defined in .gpu/.dsp section");
230 // error(syntax_error);
231 error("syntax error; expected symbol");
235 // ccname = lookup((char *)tok[1], LABEL, 0);
236 ccname = lookup(string[tok[1]], LABEL, 0);
238 // Make sure symbol is a valid ccdef
239 if (!ccname || !(ccname->sattre & EQUATEDCC))
241 error("invalid equated condition name specified");
245 ccname->sattre |= UNDEF_CC;
252 // Undefine an Equated Register
254 int d_equrundef(void)
258 // Check that we are in a RISC section
261 error(".equrundef/.regundef must be defined in .gpu/.dsp section");
267 // Skip preceeding or seperating commas (if any)
271 // Check we are dealing with a symbol
274 // error(syntax_error);
275 error("syntax error; expected symbol");
279 // Lookup and undef if equated register
280 // regname = lookup((char *)tok[1], LABEL, 0);
281 regname = lookup(string[tok[1]], LABEL, 0);
283 if (regname && (regname->sattre & EQUATEDREG))
284 regname->sattre |= UNDEF_EQUR;
286 // Skip over symbol token and address
295 // Do Not Allow the Use of the CLR.L Opcode
304 // Include Binary File
310 long pos, size, bytesRead;
313 // Check to see if we're in BSS, and, if so, throw an error
316 errors("cannot include binary file \"%s\" in BSS section", string[tok[1]]);
322 // error(syntax_error);
323 error("syntax error; string missing");
327 if ((fd = open(string[tok[1]], _OPEN_INC)) < 0)
329 errors("cannot open include binary file (%s)", string[tok[1]]);
333 size = lseek(fd, 0L, SEEK_END);
334 pos = lseek(fd, 0L, SEEK_SET);
337 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size); }
339 char * fileBuffer = (char *)malloc(size);
340 bytesRead = read(fd, fileBuffer, size);
342 if (bytesRead != size)
344 sprintf(msg, "was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[tok[1]], size);
349 //#define D_byte(b) {*chptr++=(char)b; ++sloc; ++ch_size; if(orgactive) ++orgaddr;}
350 memcpy(chptr, fileBuffer, size);
365 // Set RISC Register Banks
369 // Set active register bank zero
377 // Set active register bank one
384 // Adjust Location to an EVEN Value
390 if ((scattr & SBSS) == 0)
406 // Adjust Location to an LONG Value
413 i = sloc & ~(val - 1);
416 val = val - (sloc - i);
422 if ((scattr & SBSS) == 0)
440 // Adjust Location to an PHRASE Value
447 i = sloc & ~(val - 1);
450 val = val - (sloc - i);
456 if ((scattr & SBSS) == 0)
474 // Adjust Location to an DPHRASE Value
481 i = sloc & ~(val - 1);
484 val = val - (sloc - i);
490 if ((scattr & SBSS) == 0)
508 // Adjust Location to an QPHRASE Value
515 i = sloc & ~(val - 1);
518 val = val - (sloc - i);
524 if ((scattr & SBSS) == 0)
543 // Do auto-even. This must be called ONLY if 'sloc' is odd.
545 // This is made hairy because, if there was a label on the line, we also have
546 // to adjust its value. This won't work with more than one label on the line,
547 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
548 // the C compiler is VERY paranoid and uses ".even" whenever it can
553 ++sloc; // Bump BSS section
555 D_byte(0) // Deposit 0.b in non-BSS
557 if (lab_sym != NULL) // Bump label if we have to
563 // Unimplemened Directive Error
567 return error("unimplemented directive");
572 // Return absolute (not TDB) and defined expression or return an error
574 int abs_expr(VALUE * a_eval)
578 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
581 if (!(eattr & DEFINED))
582 return error(undef_error);
585 return error(rel_error);
592 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
594 int symlist(int(* func)())
596 char * em = "symbol list syntax";
604 if ((*func)(tok[1]) != OK)
606 if ((*func)(string[tok[1]]) != OK)
626 // .include "filename"
636 if (*tok == STRING) // Leave strings ALONE
642 else if (*tok == SYMBOL) // Try to append ".s" to symbols
645 strcpy(buf, (char *)*++tok);
647 strcpy(buf, string[*++tok]);
652 else // Punt if no STRING or SYMBOL
653 return error("missing filename");
655 // Make sure the user didn't try anything like:
656 // .include equates.s
658 return error("extra stuff after filename -- enclose it in quotes");
660 // Attempt to open the include file in the current directory, then (if that
661 // failed) try list of include files passed in the enviroment string or by
663 if ((j = open(fn, 0)) < 0)
665 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
669 if (j > 0 && buf1[j - 1] != SLASHCHAR) // Append path char if necessary
670 strcat(buf1, SLASHSTRING);
674 if ((j = open(buf1, 0)) >= 0)
678 return errors("cannot open: \"%s\"", fn);
688 // .assert expression [, expression...]
695 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
697 if (!(eattr & DEFINED))
698 return error("forward or undefined .assert");
701 return error("assert failure");
713 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
720 return error("cannot .globl local symbol");
722 if ((sy = lookup(p, LABEL, 0)) == NULL)
724 sy = NewSymbol(p, LABEL, 0);
753 else if (abs_expr(&eval) != OK)
769 return error("directive forbidden in gpu/dsp mode");
784 return error("directive forbidden in gpu/dsp mode");
799 return error("directive forbidden in gpu/dsp mode");
812 // .ds[.size] expression
817 printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc);
821 // This gets kind of stupid. This directive is disallowed in normal 68000
822 // mode ("for your own good!"), but is permitted for 6502 and Alcyon-
823 // compatibility modes. For obvious reasons, no auto-even is done in 8-bit
825 if (as68_flag == 0 && (scattr & SBSS) == 0)
826 return error(".ds permitted only in BSS");
828 if (siz != SIZB && (sloc & 1)) // Automatic .even
831 if (abs_expr(&eval) != OK)
834 // In non-TDB section (BSS, ABS and M6502) just advance the location
835 // counter appropriately. In TDB sections, deposit (possibly large) chunks
836 //of zeroed memory....
842 just_bss = 1; // No data deposited (8-bit CPU mode)
846 dep_block(eval, siz, (VALUE)0, (WORD)(DEFINED | ABS), NULL);
855 // dc.b, dc.w / dc, dc.l
865 int movei = 0; // movei flag for dc.i
867 if ((scattr & SBSS) != 0)
868 return error("illegal initialization of section");
870 if ((siz != SIZB) && (sloc & 1))
875 // dc.b 'string' [,] ...
876 if (siz == SIZB && *tok == STRING && (tok[2] == ',' || tok[2] == EOL))
878 // i = strlen((const char*)tok[1]);
879 i = strlen(string[tok[1]]);
881 if ((challoc - ch_size) < i)
884 for(p=string[tok[1]]; *p!=EOS; ++p)
899 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
902 tdb = (WORD)(eattr & TDB);
903 defined = (WORD)(eattr & DEFINED);
905 if ((challoc - ch_size) < 4)
913 fixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
919 return error("non-absolute byte value");
921 if (eval + 0x100 >= 0x200)
922 return error(range_error);
932 fixup(FU_WORD | FU_SEXT, sloc, exprbuf);
938 rmark(cursect, sloc, tdb, MWORD, NULL);
940 if (eval + 0x10000 >= 0x20000)
941 return error(range_error);
943 // Deposit 68000 or 6502 (byte-reversed) word
952 fixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
954 fixup(FU_LONG, sloc, exprbuf);
961 rmark(cursect, sloc, tdb, MLONG, NULL);
964 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
982 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
989 if ((scattr & SBSS) != 0)
990 return error("illegal initialization of section");
992 if (abs_expr(&evalc) != OK)
996 return error("missing comma");
998 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1001 if ((siz != SIZB) && (sloc & 1))
1004 dep_block(evalc, siz, eval, eattr, exprbuf);
1010 // Generalized initialization directive
1012 // .init[.siz] [#count,] expression [.size] , ...
1014 // The size suffix on the ".init" directive becomes the default size of the
1015 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1016 // and an expression, it specifies a repeat count. The value to be deposited
1017 // may be followed by a size suffix, which overrides the default size.
1019 int d_init(WORD def_siz)
1026 if ((scattr & SBSS) != 0)
1027 return error(".init not permitted in BSS or ABS");
1030 return error("directive forbidden in gpu/dsp mode");
1034 // Get repeat count (defaults to 1)
1039 if (abs_expr(&count) != OK)
1043 return error(comma_error);
1048 // Evaluate expression to deposit
1049 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1052 switch ((int)*tok++)
1053 { // Determine size of object to deposit
1054 case DOTB: siz = SIZB; break;
1055 case DOTW: siz = SIZB; break;
1056 case DOTL: siz = SIZL; break;
1063 dep_block(count, siz, eval, eattr, exprbuf);
1073 return error(comma_error);
1080 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1082 int dep_block(VALUE count, WORD siz, VALUE eval, WORD eattr, TOKEN * exprbuf)
1087 tdb = (WORD)(eattr & TDB);
1088 defined = (WORD)(eattr & DEFINED);
1092 if ((challoc - ch_size) < 4)
1100 fixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1106 return error("non-absolute byte value");
1108 if (eval + 0x100 >= 0x200)
1109 return error(range_error);
1119 fixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1125 rmark(cursect, sloc, tdb, MWORD, NULL);
1127 if (eval + 0x10000 >= 0x20000)
1128 return error(range_error);
1130 // Deposit 68000 or 6502 (byte-reversed) word
1138 fixup(FU_LONG, sloc, exprbuf);
1144 rmark(cursect, sloc, tdb, MLONG, NULL);
1158 // .comm symbol, size
1167 return error("missing symbol");
1169 // p = (char *)tok[1];
1173 if (*p == '.') // Cannot .comm a local symbol
1174 return error(locgl_error);
1176 if ((sym = lookup(p, LABEL, 0)) == NULL)
1177 sym = NewSymbol(p, LABEL, 0);
1180 if (sym->sattr & DEFINED)
1181 return error(".comm symbol already defined");
1184 sym->sattr = GLOBAL | COMMON | BSS;
1187 return error(comma_error);
1189 if (abs_expr(&eval) != OK) // Parse size of common region
1192 sym->svalue = eval; // Install common symbol's size
1199 // .list - Turn listing on
1211 // .nlist - Turn listing off
1223 // .68000 - Back to 68000 TEXT segment
1228 // Switching from gpu/dsp sections should reset any ORG'd Address
1238 // .gpu - Switch to GPU Assembler
1242 if ((cursect != TEXT) && (cursect != DATA))
1244 error(".gpu can only be used in the TEXT or DATA segments");
1248 // If previous section was dsp or 68000 then we need to reset ORG'd Addresses
1255 rgpu = 1; // Set GPU assembly
1256 rdsp = 0; // Unset DSP assembly
1257 regbank = BANK_N; // Set no default register bank
1263 // .dsp - Switch to DSP Assembler
1267 if ((cursect != TEXT) && (cursect != DATA))
1269 error(".dsp can only be used in the TEXT or DATA segments");
1273 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1280 rdsp = 1; // Set DSP assembly
1281 rgpu = 0; // Unset GPU assembly
1282 regbank = BANK_N; // Set no default register bank
1288 // .cargs [#offset], symbol[.size], ...
1290 // Lists of registers may also be mentioned; they just take up space. Good for
1291 // "documentation" purposes:
1293 // .cargs a6, .arg1, .arg2, .arg3...
1295 // Symbols thus created are ABS and EQUATED.
1299 VALUE eval = 4; // Default to 4 if no offset specified (to account for
1308 return error("directive forbidden in gpu/dsp mode");
1314 if (abs_expr(&eval) != OK)
1317 // Eat the comma, if it's there
1326 // p = (char *)tok[1];
1331 env = curenv; // Label is local
1333 env = 0; // Label is global
1335 // Set env to either local (dot prefixed) or global scope
1336 env = (*p == '.' ? curenv : 0);
1338 symbol = lookup(p, LABEL, env);
1342 symbol = NewSymbol(p, LABEL, env);
1345 else if (symbol->sattr & DEFINED)
1346 return errors("multiply-defined label '%s'", p);
1348 // Put symbol in "order of definition" list
1349 if (!(symbol->sattr & SDECLLIST))
1352 symbol->sattr |= (ABS | DEFINED | EQUATED);
1353 symbol->svalue = eval;
1356 // What this does is eat any dot suffixes attached to a symbol. If
1357 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1358 // there is no dot suffix, it assumes a size of 2.
1370 else if (*tok >= KW_D0 && *tok <= KW_A7)
1372 if (reglist(&rlist) < 0)
1375 // for(i=0; i++<16; rlist>>=1)
1376 for(i=0; i<16; i++, rlist>>=1)
1399 return error(".cargs syntax");
1403 // Eat commas in between each argument, if they exist
1411 // .cstruct [#offset], symbol[.size], ...
1413 // Lists of registers may also be mentioned; they just take up space. Good for
1414 // "documentation" purposes:
1416 // .cstruct a6, .arg1, .arg2, .arg3...
1418 // Symbols thus created are ABS and EQUATED. Note that this is for
1419 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
1424 VALUE eval = 0; // Default, if no offset specified, is zero
1432 return error("directive forbidden in gpu/dsp mode");
1438 if (abs_expr(&eval) != OK)
1441 // Eat the comma, if it's there
1450 symbolName = string[tok[1]];
1452 // Set env to either local (dot prefixed) or global scope
1453 env = (symbolName[0] == '.' ? curenv : 0);
1454 symbol = lookup(symbolName, LABEL, env);
1456 // If the symbol wasn't found, then define it. Otherwise, throw an
1460 symbol = NewSymbol(symbolName, LABEL, env);
1463 else if (symbol->sattr & DEFINED)
1464 return errors("multiply-defined label '%s'", symbolName);
1466 // Put symbol in "order of definition" list
1467 if (!(symbol->sattr & SDECLLIST))
1472 // Adjust label start address if it's a word or a long, as a byte
1473 // label might have left us on an odd address.
1478 eval += eval & 0x01;
1481 symbol->sattr |= (ABS | DEFINED | EQUATED);
1482 symbol->svalue = eval;
1484 // Check for dot suffixes and adjust space accordingly (longs and
1485 // words on an odd boundary get bumped to the next word aligned
1486 // address). If no suffix, then throw an error.
1499 return error("Symbol missing dot suffix in .cstruct construct");
1504 else if (*tok >= KW_D0 && *tok <= KW_A7)
1506 if (reglist(&rlist) < 0)
1509 for(i=0; i<16; i++, rlist>>=1)
1532 return error(".cstruct syntax");
1536 // Eat commas in between each argument, if they exist
1544 // Undefine a macro - .undefmac macname [, macname...]
1546 int undmac1(char * p)
1548 SYM * symbol = lookup(p, MACRO, 0);
1550 // If the macro symbol exists, cause it to disappear
1551 // if ((sy = lookup(p, MACRO, 0)) != NULL)
1553 symbol->stype = (BYTE)SY_UNDEF;
1568 warn("JPAD directive is deprecated/non-functional");
1575 warn("NOJPAD directive is deprecated/non-functional");