2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // DIRECT.C - Directive Handling
4 // Copyright (C) 199x Landon Dyer, 2011-2019 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
30 TOKEN exprbuf[128]; // Expression buffer
31 SYM * symbolPtr[1000000]; // Symbol pointers table
32 static long unused; // For supressing 'write' warnings
33 char buffer[256]; // Scratch buffer for messages
34 int largestAlign[3] = { 2, 2, 2 }; // Largest alignment value seen per section
36 // Function prototypes
72 int d_equrundef(void);
84 void SetLargestAlignment(int);
86 // Directive handler table
97 (void *)d_init, // 9 init
99 (void *)d_goto, // 11 goto
100 (void *)d_dc, // 12 dc
101 (void *)d_ds, // 13 ds
102 d_undmac, // 14 undefmac
105 (void *)d_dcb, // 17 dcb
109 d_incbin, // 21 .incbin //load
110 d_unimpl, // 22 disable
111 d_unimpl, // 23 enable
113 d_regbank0, // 25 .regbank0
114 d_regbank1, // 26 .regbank1
116 d_assert, // 28 assert
118 d_unimpl, // 30* endif
119 d_unimpl, // 31* endc
121 d_include, // 33 include
123 d_unimpl, // 35* macro
124 ExitMacro, // 36* exitm
125 d_unimpl, // 37* endm
129 d_phrase, // 41* endr
130 d_dphrase, // 42 struct
131 d_qphrase, // 43 ends
133 d_subttl, // 45 subttl
137 d_noclear, // 49 .noclear
138 d_equrundef, // 50 .equrundef/.regundef
139 d_ccundef, // 51 .ccundef
140 d_print, // 52 .print
141 d_cstruct, // 53 .cstruct
142 d_jpad, // 54 .jpad (deprecated)
143 d_nojpad, // 55 .nojpad (deprecated)
144 d_gpumain, // 56 .gpumain (deprecated)
145 d_prgflags, // 57 .prgflags
146 d_68020, // 58 .68020
147 d_68030, // 59 .68030
148 d_68040, // 60 .68040
149 d_68060, // 61 .68060
150 d_68881, // 62 .68881
151 d_68882, // 63 .68882
152 d_56001, // 64 .56001
155 d_objproc, // 67 .objproc
160 // Set the largest alignment seen in the current section
162 void SetLargestAlignment(int size)
164 if ((scattr & TEXT) && (largestAlign[0] < size))
165 largestAlign[0] = size;
166 else if ((scattr & DATA) && (largestAlign[1] < size))
167 largestAlign[1] = size;
168 else if ((scattr & BSS) && (largestAlign[2] < size))
169 largestAlign[2] = size;
174 // .error - Abort compilation, printing an error message
176 int d_error(char *str)
179 return error("error directive encountered - aborting assembling");
185 return error(string[tok[1]]);
188 return error("error directive encountered--aborting assembly");
195 // .warn - Just display a warning on screen
197 int d_warn(char *str)
200 return warn("WARNING WARNING WARNING");
206 return warn(string[tok[1]]);
209 return warn("WARNING WARNING WARNING");
222 if (!rgpu && !rdsp && !robjproc && !m6502 && !dsp56001)
223 return error(".org permitted only in GPU/DSP/OP, 56001 and 6502 sections");
225 // M56K can leave the expression off the org for some reason :-/
226 // (It's because the expression is non-standard, and so we have to look at
228 if (!dsp56001 && (abs_expr(&address) == ERROR))
230 error("cannot determine org'd address");
234 if (rgpu | rdsp | robjproc)
241 // 6502. We also kludge 'lsloc' so the listing generator doesn't try
242 // to spew out megabytes.
243 if (address > 0xFFFF)
244 return error(range_error);
246 if (sloc != currentorg[0])
248 currentorg[1] = sloc;
252 currentorg[0] = address;
254 lsloc = sloc = address;
255 chptr = scode->chptr + address;
261 // Only mark segments we actually wrote something
262 if (chptr != dsp_currentorg->start && dsp_written_data_in_current_org)
264 dsp_currentorg->end = chptr;
268 // Maybe we switched from a non-DSP section (TEXT, DATA, etc) and
269 // scode isn't initialised yet. Not that it's going to be a valid
270 // scenario, but if we try it anyhow it's going to lead to a crash. So
271 // let's fudge a value of 0 and get on with it.
272 orgaddr = (scode != NULL ? sloc : 0);
276 return error(syntax_error);
278 int sectionToSwitch = 0;
283 dsp_currentorg->memtype = ORG_X;
284 sectionToSwitch = M56001X;
288 dsp_currentorg->memtype = ORG_Y;
289 sectionToSwitch = M56001Y;
293 dsp_currentorg->memtype = ORG_P;
294 sectionToSwitch = M56001P;
298 dsp_currentorg->memtype = ORG_L;
299 sectionToSwitch = M56001L;
303 return error("unknown type in ORG");
306 if ((obj_format == LOD) || (obj_format == P56))
307 SwitchSection(sectionToSwitch);
310 chcheck(3); // Ensure we got a valid address to write
311 dsp_currentorg->chunk = scode; // Mark down which chunk this org starts from (will be needed when outputting)
315 // Well, the user didn't specify an address at all so we'll have to
316 // use the last used address of that section (or 0 if there wasn't one)
318 dsp_currentorg->start = chptr;
319 dsp_currentorg->orgadr = orgaddr;
323 if (abs_expr(&address) == ERROR)
325 error("cannot determine org'd address");
329 dsp_currentorg->start = chptr;
330 dsp_currentorg->orgadr = (uint32_t)address;
331 sect[cursect].orgaddr = (uint32_t)address;
334 if (address > DSP_MAX_RAM)
336 return error(range_error);
339 dsp_written_data_in_current_org = 0;
341 // Copied from 6502 above: kludge `lsloc' so the listing generator
342 // doesn't try to spew out megabytes.
343 lsloc = sloc = (int32_t)address;
344 // N.B.: It seems that by enabling this, even though it works elsewhere, will cause symbols to royally fuck up. Will have to do some digging to figure out why.
358 char prntstr[LNSIZ]; // String for PRINT directive
359 char format[LNSIZ]; // Format for PRINT directive
360 int formatting = 0; // Formatting on/off
361 int wordlong = 0; // WORD = 0, LONG = 1
362 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
364 uint64_t eval; // Expression value
365 WORD eattr; // Expression attributes
366 SYM * esym; // External symbol involved in expr.
367 TOKEN r_expr[EXPRSIZE];
374 sprintf(prntstr, "%s", string[tok[1]]);
375 printf("%s", prntstr);
378 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
385 // "X" & "L" get tokenized now... :-/ Probably should look into preventing this kind of thing from happening (was added with DSP56K code)
386 if ((tok[1] != SYMBOL) && (tok[1] != KW_L) && (tok[1] != KW_X))
394 else if (tok[1] == KW_X)
401 strcpy(prntstr, string[tok[2]]);
405 case 'l': case 'L': wordlong = 1; break;
406 case 'w': case 'W': wordlong = 0; break;
407 case 'x': case 'X': outtype = 0; break;
408 case 'd': case 'D': outtype = 1; break;
409 case 'u': case 'U': outtype = 2; break;
411 error("unknown print format flag");
423 if (expr(r_expr, &eval, &eattr, &esym) != OK)
429 case 0: strcpy(format, "%X"); break;
430 case 1: strcpy(format, "%d" ); break;
431 case 2: strcpy(format, "%u" ); break;
435 sprintf(prntstr, format, eval);
437 sprintf(prntstr, format, eval & 0xFFFF);
439 printf("%s", prntstr);
442 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
458 error("illegal print token [@ '%s']", prntstr);
464 // Undefine an equated condition code
470 // Check that we are in a RISC section
473 error(".ccundef must be defined in .gpu/.dsp section");
479 error("syntax error; expected symbol");
483 ccname = lookup(string[tok[1]], LABEL, 0);
485 // Make sure symbol is a valid ccdef
486 if (!ccname || !(ccname->sattre & EQUATEDCC))
488 error("invalid equated condition name specified");
492 ccname->sattre |= UNDEF_CC;
499 // Undefine an equated register
501 int d_equrundef(void)
505 // Check that we are in a RISC section
507 return error(".equrundef/.regundef must be defined in .gpu/.dsp section");
511 // Skip preceeding or seperating commas (if any)
515 // Check we are dealing with a symbol
517 return error("syntax error; expected symbol");
519 // Lookup and undef if equated register
520 regname = lookup(string[tok[1]], LABEL, 0);
522 if (regname && (regname->sattre & EQUATEDREG))
524 // Reset the attributes of this symbol...
526 regname->sattre &= ~(EQUATEDREG | BANK_0 | BANK_1);
527 regname->sattre |= UNDEF_EQUR;
530 // Skip over symbol token and address
539 // Do not allow use of the CLR.L opcode
543 warn("CLR.L opcode ignored...");
549 // Include binary file
555 long pos, size, bytesRead;
559 // Check to see if we're in BSS, and, if so, throw an error
562 error("cannot include binary file \"%s\" in BSS section", string[tok[1]]);
568 error("syntax error; file to include missing");
572 // Attempt to open the include file in the current directory, then (if that
573 // failed) try list of include files passed in the enviroment string or by
575 if ((fd = open(string[tok[1]], _OPEN_INC)) < 0)
577 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
581 // Append path char if necessary
582 if (fd > 0 && buf1[fd - 1] != SLASHCHAR)
583 strcat(buf1, SLASHSTRING);
585 strcat(buf1, string[tok[1]]);
587 if ((fd = open(buf1, _OPEN_INC)) >= 0)
591 return error("cannot open: \"%s\"", string[tok[1]]);
596 size = lseek(fd, 0L, SEEK_END);
597 pos = lseek(fd, 0L, SEEK_SET);
600 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[tok[1]], size); }
602 char * fileBuffer = (char *)malloc(size);
603 bytesRead = read(fd, fileBuffer, size);
605 if (bytesRead != size)
607 error("was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[tok[1]], size);
611 memcpy(chptr, fileBuffer, size);
626 // Set RISC register banks
630 // Set active register bank zero
638 // Set active register bank one
645 // Helper function, to cut down on mistakes & typing
647 static inline void SkipBytes(unsigned bytesToSkip)
652 if ((scattr & SBSS) == 0)
654 chcheck(bytesToSkip);
655 D_ZEROFILL(bytesToSkip);
662 orgaddr += bytesToSkip;
668 // Adjust location to an EVEN value
673 return error(in_6502mode);
675 unsigned skip = (rgpu || rdsp ? orgaddr : sloc) & 0x01;
679 if ((scattr & SBSS) == 0)
698 // Adjust location to a LONG value
702 unsigned lower2Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x03;
703 unsigned bytesToSkip = (0x04 - lower2Bits) & 0x03;
704 SkipBytes(bytesToSkip);
705 SetLargestAlignment(4);
712 // Adjust location to a PHRASE value
714 // N.B.: We have to handle the GPU/DSP cases separately because you can embed
715 // RISC code in the middle of a regular 68K section. Also note that all
716 // of the alignment pseudo-ops will have to be fixed this way.
718 // This *must* behave differently when in a RISC section, as following sloc
719 // (instead of orgaddr) will fuck things up royally. Note that we do it this
720 // way because you can embed RISC code in a 68K section, and have the origin
721 // pointing to a different alignment in the RISC section than the 68K section.
725 unsigned lower3Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x07;
726 unsigned bytesToSkip = (0x08 - lower3Bits) & 0x07;
727 SkipBytes(bytesToSkip);
728 SetLargestAlignment(8);
735 // Adjust location to a DPHRASE value
739 unsigned lower4Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x0F;
740 unsigned bytesToSkip = (0x10 - lower4Bits) & 0x0F;
741 SkipBytes(bytesToSkip);
742 SetLargestAlignment(16);
749 // Adjust location to a QPHRASE value
753 unsigned lower5Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x1F;
754 unsigned bytesToSkip = (0x20 - lower5Bits) & 0x1F;
755 SkipBytes(bytesToSkip);
756 SetLargestAlignment(32);
763 // Do auto-even. This must be called ONLY if 'sloc' is odd.
765 // This is made hairy because, if there was a label on the line, we also have
766 // to adjust its value. This won't work with more than one label on the line,
767 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
768 // the C compiler is VERY paranoid and uses ".even" whenever it can
770 // N.B.: This probably needs the same fixes as above...
774 if (cursect != M6502)
777 sloc++; // Bump BSS section
779 D_byte(0); // Deposit 0.b in non-BSS
781 if (lab_sym != NULL) // Bump label if we have to
788 // Unimplemened directive error
792 return error("unimplemented directive");
797 // Return absolute (not TDB) and defined expression or return an error
799 int abs_expr(uint64_t * a_eval)
803 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
806 if (!(eattr & DEFINED))
807 return error(undef_error);
810 return error(rel_error);
817 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
819 int symlist(int(* func)())
821 const char * em = "symbol list syntax";
828 if ((*func)(string[tok[1]]) != OK)
847 // .include "filename"
857 if (*tok == STRING) // Leave strings ALONE
859 else if (*tok == SYMBOL) // Try to append ".s" to symbols
861 strcpy(buf, string[*++tok]);
865 else // Punt if no STRING or SYMBOL
866 return error("missing filename");
868 // Make sure the user didn't try anything like:
869 // .include equates.s
871 return error("extra stuff after filename--enclose it in quotes");
873 // Attempt to open the include file in the current directory, then (if that
874 // failed) try list of include files passed in the enviroment string or by
876 if ((j = open(fn, 0)) < 0)
878 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
882 // Append path char if necessary
883 if (j > 0 && buf1[j - 1] != SLASHCHAR)
884 strcat(buf1, SLASHSTRING);
888 if ((j = open(buf1, 0)) >= 0)
892 return error("cannot open: \"%s\"", fn);
902 // .assert expression [, expression...]
909 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
911 if (!(eattr & DEFINED))
912 return error("forward or undefined .assert");
915 return error("assert failure");
927 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
934 return error("cannot .globl local symbol");
936 if ((sy = lookup(p, LABEL, 0)) == NULL)
938 sy = NewSymbol(p, LABEL, 0);
941 //printf("glob1: Making global symbol: attr=%04X, eattr=%08X, %s\n", sy->sattr, sy->sattre, sy->sname);
953 return error(in_6502mode);
961 // .prgflags expression
968 return error("PRGFLAGS requires value");
969 else if (abs_expr(&eval) == OK)
971 PRGFLAGS = (uint32_t)eval;
976 return error("PRGFLAGS requires value");
989 return error(in_6502mode);
995 else if (abs_expr(&eval) != OK)
999 sloc = (uint32_t)eval;
1010 return error("directive forbidden in gpu/dsp mode");
1012 return error(in_6502mode);
1014 if (cursect != TEXT)
1017 SwitchSection(TEXT);
1027 return error("directive forbidden in gpu/dsp mode");
1029 return error(in_6502mode);
1031 if (cursect != DATA)
1034 SwitchSection(DATA);
1044 return error("directive forbidden in gpu/dsp mode");
1046 return error(in_6502mode);
1059 // .ds[.size] expression
1063 DEBUG { printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc); }
1067 if ((cursect & (M6502 | M56KPXYL)) == 0)
1069 if ((siz != SIZB) && (sloc & 1)) // Automatic .even
1073 if (abs_expr(&eval) != OK)
1076 // Check to see if the value being passed in is negative (who the hell does
1077 // that?--nobody does; it's the code gremlins, or rum, that does it)
1078 // N.B.: Since 'eval' is of type uint32_t, if it goes negative, it will have
1079 // its high bit set.
1080 if (eval & 0x80000000)
1081 return error("negative sizes not allowed");
1083 // In non-TDB section (BSS, ABS and M6502) just advance the location
1084 // counter appropriately. In TDB sections, deposit (possibly large) chunks
1085 // of zeroed memory....
1086 if ((scattr & SBSS) || cursect == M6502)
1088 listvalue((uint32_t)eval);
1090 sloc += (uint32_t)eval;
1092 if (cursect == M6502)
1095 just_bss = 1; // No data deposited (8-bit CPU mode)
1099 dep_block(eval, siz, 0, (WORD)(DEFINED | ABS), NULL);
1108 // dc.b, dc.w / dc, dc.l, dc.i, dc.q, dc.d
1116 if ((scattr & SBSS) != 0)
1117 return error("illegal initialization of section");
1119 // Do an auto_even if it's not BYTE sized (hmm, should we be doing this???)
1120 if ((cursect != M6502) && (cursect != M56001P) && (cursect != M56001X)
1121 && (cursect != M56001Y) && (cursect != M56001L)
1122 && (siz != SIZB) && (sloc & 1))
1125 // Check to see if we're trying to set LONGS on a non 32-bit aligned
1126 // address in a GPU or DSP section, in their local RAM
1127 if ((siz == SIZL) && (orgaddr & 0x03)
1128 && ((rgpu && (orgaddr >= 0xF03000) && (orgaddr <= 0xF03FFFF))
1129 || (rdsp && (orgaddr >= 0xF1B000) && (orgaddr <= 0xF1CFFFF))))
1130 warn("depositing LONGs on a non-long address in local RAM");
1134 // dc.b 'string' [,] ...
1135 if (siz == SIZB && (*tok == STRING || *tok == STRINGA8) && (tok[2] == ',' || tok[2] == EOL))
1137 uint32_t i = strlen(string[tok[1]]);
1139 if ((challoc - ch_size) < i)
1144 for(p=string[tok[1]]; *p!=EOS; p++)
1147 else if (*tok == STRINGA8)
1149 for(p=string[tok[1]]; *p!=EOS; p++)
1150 D_byte(strtoa8[*p]);
1154 error("String format not supported... yet");
1161 int movei = 0; // MOVEI flag for dc.i
1170 // dc.x <expression>
1173 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
1176 uint16_t tdb = eattr & TDB;
1177 uint16_t defined = eattr & DEFINED;
1179 // N.B.: This is awful. This needs better handling, rather than just bodging something in that, while works, is basically an ugly wart on the assembler. !!! FIX !!!
1182 if (cursect != M56001L)
1186 AddFixup(FU_DSPIMM24 | FU_SEXT, sloc, exprbuf);
1193 double fval = *(double *)&eval;
1197 warn("value clamped to +1.");
1200 else if (fval <= -1)
1202 warn("value clamped to -1.");
1207 // Convert fraction to 24 bits fixed point with sign and rounding
1208 // Yeah, that cast to int32_t has to be there because casting
1209 // a float to unsigned int is "undefined" according to the C
1210 // standard. Which most compilers seem to do the sensible thing
1211 // and just cast the f**king value properly, except gcc 4.x.x
1212 // for arm (tested on raspbian).
1213 // Thanks, C and gcc! Thanks for making me waste a few hours \o/
1214 eval = 0;//!!! FIX !!! (uint32_t)(int32_t)round(fval*(1 << 23));
1219 if ((uint32_t)eval + 0x1000000 >= 0x2000000)
1220 return error(range_error);
1223 // Deposit DSP word (24-bit)
1229 // In L: we deposit stuff to both X: and Y: instead
1230 // We will be a bit lazy and require that there is a 2nd value in the same source line.
1231 // (Motorola's assembler can parse 12-digit hex values, which we can't do at the moment)
1232 // This of course requires to parse 2 values in one pass.
1233 // If there isn't another value in this line, assume X: value is 0.
1240 AddFixup(FU_DSPIMM24 | FU_SEXT, sloc, exprbuf);
1247 float fval = *(float *)&eval;
1250 warn("value clamped to +1.");
1253 else if (fval <= -1)
1255 warn("value clamped to -1.");
1260 // Convert fraction to 24 bits fixed point with sign and rounding
1261 // Yeah, that cast to int32_t has to be there because casting
1262 // a float to unsigned int is "undefined" according to the C
1263 // standard. Which most compilers seem to do the sensible thing
1264 // and just cast the f**king value properly, except gcc 4.x.x
1265 // for arm (tested on raspbian).
1266 // Thanks, C and gcc! Thanks for making me waste a few hours \o/
1267 eval = 0;//!!! FIX !!! (uint32_t)(int32_t)round(fval*(1 << 23));
1272 if (eval + 0x1000000 >= 0x2000000)
1273 return error(range_error);
1276 // Parse 2nd value if we didn't do this yet
1277 if (secondword == 0)
1279 evaly = (uint32_t)eval;
1284 // If we don't have a : then we're probably at EOL,
1285 // which means the X: value will be 0
1291 tok++; // Eat the comma;
1293 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
1296 defined = (WORD)(eattr & DEFINED);
1301 // Deposit DSP words (24-bit)
1304 sloc--; // We do write 2 DSP words but as far as L: space is concerned we actually advance our counter by one
1316 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1322 return error("non-absolute byte value");
1324 if (eval + 0x100 >= 0x200)
1325 return error("%s (value = $%X)", range_error, eval);
1335 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1340 if (eval + 0x10000 >= 0x20000)
1341 return error(range_error);
1344 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1346 // Deposit 68000 or 6502 (byte-reversed) word
1347 if (cursect != M6502)
1355 // Shamus: Why can't we do longs in 6502 mode?
1357 return error(in_6502mode);
1362 AddFixup(FU_LONG | FU_MOVEI, sloc, exprbuf);
1364 AddFixup(FU_LONG, sloc, exprbuf);
1371 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1374 eval = WORDSWAP32(eval);
1383 return error(in_6502mode);
1385 // Shamus: We only handle DC.Q type stuff, will have to add fixups
1386 // and stuff later (maybe... might not be needed...)
1387 // DEFINITELY NEED FIXUPS HERE!
1390 AddFixup(FU_QUAD, sloc, exprbuf);
1400 // 32-bit float size
1402 return error(in_6502mode);
1406 AddFixup(FU_FLOATSING, sloc, exprbuf);
1411 //Would this *ever* happen?
1413 // MarkRelocatable(cursect, sloc, tdb, MSINGLE, NULL);
1417 uint32_t ieee754 = FloatToIEEE754((float)*ptr.dp);
1423 // 64-bit double size
1425 return error(in_6502mode);
1429 AddFixup(FU_FLOATDOUB, sloc, exprbuf);
1434 //Would this *ever* happen?
1436 // MarkRelocatable(cursect, sloc, tdb, MDOUBLE, NULL);
1440 uint64_t ieee754 = DoubleToIEEE754(*ptr.dp);
1447 return error(in_6502mode);
1450 memset(extDbl, 0, 12);
1454 AddFixup(FU_FLOATEXT, sloc, exprbuf);
1459 //Would this *ever* happen?
1461 // MarkRelocatable(cursect, sloc, tdb, MEXTEND, NULL);
1465 DoubleToExtended(*ptr.dp, extDbl);
1483 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1487 uint64_t evalc, eval;
1490 DEBUG { printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr); }
1492 if ((scattr & SBSS) != 0)
1493 return error("illegal initialization of section");
1495 if (abs_expr(&evalc) != OK)
1499 return error("missing comma");
1501 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1504 if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1507 dep_block((uint32_t)evalc, siz, (uint32_t)eval, eattr, exprbuf);
1513 // Generalized initialization directive
1515 // .init[.siz] [#count,] expression [.size] , ...
1517 // The size suffix on the ".init" directive becomes the default size of the
1518 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1519 // and an expression, it specifies a repeat count. The value to be deposited
1520 // may be followed by a size suffix, which overrides the default size.
1522 int d_init(WORD def_siz)
1529 if ((scattr & SBSS) != 0)
1530 return error(".init not permitted in BSS or ABS");
1533 return error("directive forbidden in gpu/dsp mode");
1537 // Get repeat count (defaults to 1)
1542 if (abs_expr(&count) != OK)
1546 return error(comma_error);
1551 // Evaluate expression to deposit
1552 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1556 { // Determine size of object to deposit
1557 case DOTB: siz = SIZB; break;
1558 case DOTW: siz = SIZB; break;
1559 case DOTL: siz = SIZL; break;
1566 dep_block((uint32_t)count, siz, (uint32_t)eval, eattr, exprbuf);
1576 return error(comma_error);
1583 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1585 int dep_block(uint32_t count, WORD siz, uint32_t eval, WORD eattr, TOKEN * exprbuf)
1590 tdb = (WORD)(eattr & TDB);
1591 defined = (WORD)(eattr & DEFINED);
1595 if ((challoc - ch_size) < 4)
1603 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1609 return error("non-absolute byte value");
1611 if (eval + 0x100 >= 0x200)
1612 return error(range_error);
1622 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1628 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1630 if (eval + 0x10000 >= 0x20000)
1631 return error(range_error);
1633 // Deposit 68000 or 6502 (byte-reversed) word
1634 if (cursect != M6502)
1644 return error(in_6502mode);
1648 AddFixup(FU_LONG, sloc, exprbuf);
1654 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1668 // .comm symbol, size
1677 return error(in_6502mode);
1680 return error("missing symbol");
1685 if (*p == '.') // Cannot .comm a local symbol
1686 return error(locgl_error);
1688 if ((sym = lookup(p, LABEL, 0)) == NULL)
1689 sym = NewSymbol(p, LABEL, 0);
1692 if (sym->sattr & DEFINED)
1693 return error(".comm symbol already defined");
1696 sym->sattr = GLOBAL | COMMON | BSS;
1699 return error(comma_error);
1701 if (abs_expr(&eval) != OK) // Parse size of common region
1704 sym->svalue = eval; // Install common symbol's size
1711 // .list - Turn listing on
1723 // .nlist - Turn listing off
1735 // .68000 - Back to 68000 TEXT segment
1739 rgpu = rdsp = robjproc = dsp56001 = 0;
1740 // Switching from gpu/dsp sections should reset any ORG'd Address
1744 SwitchSection(TEXT);
1745 activecpu = CPU_68000;
1751 // .68020 - Back to 68000 TEXT segment and select 68020
1756 activecpu = CPU_68020;
1762 // .68030 - Back to 68000 TEXT segment and select 68030
1767 activecpu = CPU_68030;
1773 // .68040 - Back to 68000 TEXT segment and select 68040
1778 activecpu = CPU_68040;
1779 activefpu = FPU_68040;
1785 // .68060 - Back to 68000 TEXT segment and select 68060
1790 activecpu = CPU_68060;
1791 activefpu = FPU_68060;
1797 // .68881 - Back to 680x0 TEXT segment and select 68881 FPU
1802 activefpu = FPU_68881;
1808 // .68882 - Back to 680x0 TEXT segment and select 68882 FPU
1813 activefpu = FPU_68882;
1819 // nofpu - Deselect FPUs.
1823 activefpu = FPU_NONE;
1829 // .56001 - Switch to DSP56001 assembler
1834 rgpu = rdsp = robjproc = 0;
1837 if ((obj_format == LOD) || (obj_format == P56))
1838 SwitchSection(M56001P);
1845 // .gpu - Switch to GPU assembler
1849 if ((cursect != TEXT) && (cursect != DATA))
1851 error(".gpu can only be used in the TEXT or DATA segments");
1855 // If previous section was DSP or 68000 then we need to reset ORG'd Addresses
1862 rgpu = 1; // Set GPU assembly
1863 rdsp = 0; // Unset DSP assembly
1864 robjproc = 0; // Unset OP assembly
1865 dsp56001 = 0; // Unset 56001 assembly
1866 regbank = BANK_N; // Set no default register bank
1872 // .dsp - Switch to DSP assembler
1876 if ((cursect != TEXT) && (cursect != DATA))
1878 error(".dsp can only be used in the TEXT or DATA segments");
1882 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1889 rdsp = 1; // Set DSP assembly
1890 rgpu = 0; // Unset GPU assembly
1891 robjproc = 0; // Unset OP assembly
1892 dsp56001 = 0; // Unset 56001 assembly
1893 regbank = BANK_N; // Set no default register bank
1899 // .cargs [#offset], symbol[.size], ...
1901 // Lists of registers may also be mentioned; they just take up space. Good for
1902 // "documentation" purposes:
1904 // .cargs a6, .arg1, .arg2, .arg3...
1906 // Symbols thus created are ABS and EQUATED.
1910 uint64_t eval = 4; // Default to 4 if no offset specified (to account for
1919 return error("directive forbidden in gpu/dsp mode");
1925 if (abs_expr(&eval) != OK)
1928 // Eat the comma, if it's there
1939 // Set env to either local (dot prefixed) or global scope
1940 env = (*p == '.' ? curenv : 0);
1941 symbol = lookup(p, LABEL, env);
1945 symbol = NewSymbol(p, LABEL, env);
1948 else if (symbol->sattr & DEFINED)
1949 return error("multiply-defined label '%s'", p);
1951 // Put symbol in "order of definition" list
1952 AddToSymbolDeclarationList(symbol);
1954 symbol->sattr |= (ABS | DEFINED | EQUATED);
1955 symbol->svalue = eval;
1958 // What this does is eat any dot suffixes attached to a symbol. If
1959 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
1960 // there is no dot suffix, it assumes a size of 2.
1972 else if (*tok >= KW_D0 && *tok <= KW_A7)
1974 if (reglist(&rlist) < 0)
1977 for(i=0; i<16; i++, rlist>>=1)
2000 return error(".cargs syntax");
2004 // Eat commas in between each argument, if they exist
2012 // .cstruct [#offset], symbol[.size], ...
2014 // Lists of registers may also be mentioned; they just take up space. Good for
2015 // "documentation" purposes:
2017 // .cstruct a6, .arg1, .arg2, .arg3...
2019 // Symbols thus created are ABS and EQUATED. Note that this is for
2020 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
2025 uint64_t eval = 0; // Default, if no offset specified, is zero
2033 return error("directive forbidden in gpu/dsp mode");
2039 if (abs_expr(&eval) != OK)
2042 // Eat the comma, if it's there
2051 symbolName = string[tok[1]];
2053 // Set env to either local (dot prefixed) or global scope
2054 env = (symbolName[0] == '.' ? curenv : 0);
2055 symbol = lookup(symbolName, LABEL, env);
2057 // If the symbol wasn't found, then define it. Otherwise, throw an
2061 symbol = NewSymbol(symbolName, LABEL, env);
2064 else if (symbol->sattr & DEFINED)
2065 return error("multiply-defined label '%s'", symbolName);
2067 // Put symbol in "order of definition" list
2068 AddToSymbolDeclarationList(symbol);
2072 // Adjust label start address if it's a word or a long, as a byte
2073 // label might have left us on an odd address.
2078 eval += eval & 0x01;
2081 symbol->sattr |= (ABS | DEFINED | EQUATED);
2082 symbol->svalue = eval;
2084 // Check for dot suffixes and adjust space accordingly (longs and
2085 // words on an odd boundary get bumped to the next word aligned
2086 // address). If no suffix, then throw an error.
2099 return error("Symbol missing dot suffix in .cstruct construct");
2104 else if (*tok >= KW_D0 && *tok <= KW_A7)
2106 if (reglist(&rlist) < 0)
2109 for(i=0; i<16; i++, rlist>>=1)
2132 return error(".cstruct syntax");
2136 // Eat commas in between each argument, if they exist
2144 // Define start of OP object list (allows the use of ORG)
2148 if ((cursect != TEXT) && (cursect != DATA))
2150 error(".objproc can only be used in the TEXT or DATA segments");
2154 // If previous section was DSP or 68000 then we need to reset ORG'd
2162 robjproc = 1; // Set OP assembly
2163 rgpu = 0; // Unset GPU assembly
2164 rdsp = 0; // Unset DSP assembly
2165 dsp56001 = 0; // Unset 56001 assembly
2171 // Undefine a macro - .undefmac macname [, macname...]
2173 int undmac1(char * p)
2175 SYM * symbol = lookup(p, MACRO, 0);
2177 // If the macro symbol exists, cause it to disappear
2179 symbol->stype = (BYTE)SY_UNDEF;
2194 warn("JPAD directive is deprecated/non-functional");
2201 warn("NOJPAD directive is deprecated/non-functional");
2208 return error("What the hell? Do you think we adhere to the Goof standard?");
2213 // .opt - turn a specific (or all) optimisation on or off
2222 char * tmpstr = string[*tok++];
2224 if (ParseOptimization(tmpstr) != OK)
2225 return error("unknown optimization flag '%s'", tmpstr);
2228 return error(".opt directive needs every switch enclosed inside quotation marks");
2236 // .if, Start conditional assembly
2243 IFENT * rif = f_ifent;
2247 rif = (IFENT *)malloc(sizeof(IFENT));
2249 f_ifent = rif->if_prev;
2251 rif->if_prev = ifent;
2256 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
2259 if ((eattr & DEFINED) == 0)
2260 return error(undef_error);
2265 rif->if_state = (WORD)disabled;
2271 // .else, Do alternate case for .if
2275 IFENT * rif = ifent;
2277 if (rif->if_prev == NULL)
2278 return error("mismatched .else");
2281 disabled = rif->if_prev->if_state;
2285 rif->if_state = (WORD)disabled;
2291 // .endif, End of conditional assembly block
2292 // This is also called by fpop() to pop levels of IFENTs in case a macro or
2293 // include file exits early with `exitm' or `end'.
2297 IFENT * rif = ifent;
2299 if (rif->if_prev == NULL)
2300 return error("mismatched .endif");
2302 ifent = rif->if_prev;
2303 disabled = rif->if_prev->if_state;
2304 rif->if_prev = f_ifent;