2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // DIRECT.C - Directive Handling
4 // Copyright (C) 199x Landon Dyer, 2011-2021 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
38 TOKEN exprbuf[128]; // Expression buffer
39 SYM * symbolPtr[1000000]; // Symbol pointers table
40 static long unused; // For supressing 'write' warnings
41 char buffer[256]; // Scratch buffer for messages
42 int largestAlign[3] = { 2, 2, 2 }; // Largest alignment value seen per section
44 // Function prototypes
81 int d_equrundef(void);
94 void SetLargestAlignment(int);
96 // Directive handler table
107 (void *)d_init, // 9 init
109 (void *)d_goto, // 11 goto
110 (void *)d_dc, // 12 dc
111 (void *)d_ds, // 13 ds
112 d_undmac, // 14 undefmac
115 (void *)d_dcb, // 17 dcb
119 d_incbin, // 21 .incbin //load
120 d_unimpl, // 22 disable
121 d_unimpl, // 23 enable
123 d_regbank0, // 25 .regbank0
124 d_regbank1, // 26 .regbank1
126 d_assert, // 28 assert
128 d_unimpl, // 30* endif
129 d_unimpl, // 31* endc
131 d_include, // 33 include
133 d_unimpl, // 35* macro
134 ExitMacro, // 36* exitm
135 d_unimpl, // 37* endm
139 d_phrase, // 41* endr
140 d_dphrase, // 42 struct
141 d_qphrase, // 43 ends
143 d_subttl, // 45 subttl
147 d_noclear, // 49 .noclear
148 d_equrundef, // 50 .equrundef/.regundef
149 d_ccundef, // 51 .ccundef
150 d_print, // 52 .print
151 d_cstruct, // 53 .cstruct
152 d_jpad, // 54 .jpad (deprecated)
153 d_nojpad, // 55 .nojpad (deprecated)
154 d_gpumain, // 56 .gpumain (deprecated)
155 d_prgflags, // 57 .prgflags
156 d_68020, // 58 .68020
157 d_68030, // 59 .68030
158 d_68040, // 60 .68040
159 d_68060, // 61 .68060
160 d_68881, // 62 .68881
161 d_68882, // 63 .68882
162 d_56001, // 64 .56001
165 d_objproc, // 67 .objproc
166 (void *)d_dsm, // 68 .dsm
172 // Set the largest alignment seen in the current section
174 void SetLargestAlignment(int size)
176 if ((scattr & TEXT) && (largestAlign[0] < size))
177 largestAlign[0] = size;
178 else if ((scattr & DATA) && (largestAlign[1] < size))
179 largestAlign[1] = size;
180 else if ((scattr & BSS) && (largestAlign[2] < size))
181 largestAlign[2] = size;
186 // .error - Abort compilation, printing an error message
188 int d_error(char *str)
191 return error("error directive encountered - aborting assembling");
197 return error(string[tok[1]]);
200 return error("error directive encountered--aborting assembly");
207 // .warn - Just display a warning on screen
209 int d_warn(char *str)
212 return warn("WARNING WARNING WARNING");
218 return warn(string[tok[1]]);
221 return warn("WARNING WARNING WARNING");
234 if (!rgpu && !rdsp && !robjproc && !m6502 && !dsp56001 && !(obj_format == RAW))
235 return error(".org permitted only in GPU/DSP/OP, 56001, 6502 and 68k (with -fr switch) sections");
237 // M56K can leave the expression off the org for some reason :-/
238 // (It's because the expression is non-standard, and so we have to look at
240 if (!dsp56001 && (abs_expr(&address) == ERROR))
242 error("cannot determine org'd address");
246 if (rgpu | rdsp | robjproc)
253 // 6502. We also kludge 'lsloc' so the listing generator doesn't try
254 // to spew out megabytes.
255 if (address > 0xFFFF)
256 return error(range_error);
258 if (sloc != currentorg[0])
260 currentorg[1] = sloc;
264 currentorg[0] = address;
266 lsloc = sloc = address;
267 chptr = scode->chptr + address;
273 // Only mark segments we actually wrote something
274 if (chptr != dsp_currentorg->start && dsp_written_data_in_current_org)
276 dsp_currentorg->end = chptr;
280 // Maybe we switched from a non-DSP section (TEXT, DATA, etc) and
281 // scode isn't initialised yet. Not that it's going to be a valid
282 // scenario, but if we try it anyhow it's going to lead to a crash. So
283 // let's fudge a value of 0 and get on with it.
284 orgaddr = (scode != NULL ? sloc : 0);
288 return error(syntax_error);
290 int sectionToSwitch = 0;
295 dsp_currentorg->memtype = ORG_X;
296 sectionToSwitch = M56001X;
300 dsp_currentorg->memtype = ORG_Y;
301 sectionToSwitch = M56001Y;
305 dsp_currentorg->memtype = ORG_P;
306 sectionToSwitch = M56001P;
310 dsp_currentorg->memtype = ORG_L;
311 sectionToSwitch = M56001L;
315 return error("unknown type in ORG");
318 if ((obj_format == LOD) || (obj_format == P56))
319 SwitchSection(sectionToSwitch);
322 chcheck(3); // Ensure we got a valid address to write
323 dsp_currentorg->chunk = scode; // Mark down which chunk this org starts from (will be needed when outputting)
327 // Well, the user didn't specify an address at all so we'll have to
328 // use the last used address of that section (or 0 if there wasn't one)
330 dsp_currentorg->start = chptr;
331 dsp_currentorg->orgadr = orgaddr;
335 if (abs_expr(&address) == ERROR)
337 error("cannot determine org'd address");
341 dsp_currentorg->start = chptr;
342 dsp_currentorg->orgadr = (uint32_t)address;
343 sect[cursect].orgaddr = (uint32_t)address;
346 if (address > DSP_MAX_RAM)
348 return error(range_error);
351 dsp_written_data_in_current_org = 0;
353 // Copied from 6502 above: kludge `lsloc' so the listing generator
354 // doesn't try to spew out megabytes.
355 lsloc = sloc = (int32_t)address;
356 // 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.
361 // If we get here we assume it's 68k with RAW output, so this is allowed
364 return error("In 68k mode only one .org statement is allowed");
367 org68k_address = address;
381 char prntstr[LNSIZ]; // String for PRINT directive
382 char format[LNSIZ]; // Format for PRINT directive
383 int formatting = 0; // Formatting on/off
384 int wordlong = 0; // WORD = 0, LONG = 1
385 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
387 uint64_t eval; // Expression value
388 WORD eattr; // Expression attributes
389 SYM * esym; // External symbol involved in expr.
390 TOKEN r_expr[EXPRSIZE];
397 sprintf(prntstr, "%s", string[tok[1]]);
398 printf("%s", prntstr);
401 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
408 // "X" & "L" get tokenized now... :-/ Probably should look into preventing this kind of thing from happening (was added with DSP56K code)
409 // Note (ggn): This is now much less severe as it's localised for 56k only
410 if ((tok[1] != SYMBOL) && (tok[1] != REG56_L) && (tok[1] != REG56_X))
413 if (tok[1] == REG56_L)
418 else if (tok[1] == REG56_X)
425 strcpy(prntstr, string[tok[2]]);
429 case 'l': case 'L': wordlong = 1; break;
430 case 'w': case 'W': wordlong = 0; break;
431 case 'x': case 'X': outtype = 0; break;
432 case 'd': case 'D': outtype = 1; break;
433 case 'u': case 'U': outtype = 2; break;
435 error("unknown print format flag");
447 if (expr(r_expr, &eval, &eattr, &esym) != OK)
453 case 0: strcpy(format, "%X"); break;
454 case 1: strcpy(format, "%d" ); break;
455 case 2: strcpy(format, "%u" ); break;
459 sprintf(prntstr, format, eval);
461 sprintf(prntstr, format, eval & 0xFFFF);
463 printf("%s", prntstr);
466 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
482 error("illegal print token [@ '%s']", prntstr);
488 // Undefine an equated condition code
494 // Check that we are in a RISC section
497 error(".ccundef must be defined in .gpu/.dsp section");
503 error("syntax error; expected symbol");
507 ccname = lookup(string[tok[1]], LABEL, 0);
509 // Make sure symbol is a valid ccdef
510 if (!ccname || !(ccname->sattre & EQUATEDCC))
512 error("invalid equated condition name specified");
516 ccname->sattre |= UNDEF_CC;
523 // Undefine an equated register
525 int d_equrundef(void)
529 // Check that we are in a RISC section
531 return error(".equrundef/.regundef must be defined in .gpu/.dsp section");
535 // Skip preceeding or seperating commas (if any)
539 // Check we are dealing with a symbol
541 return error("syntax error; expected symbol");
543 // Lookup and undef if equated register
544 regname = lookup(string[tok[1]], LABEL, 0);
546 if (regname && (regname->sattre & EQUATEDREG))
548 // Reset the attributes of this symbol...
550 regname->sattre &= ~EQUATEDREG;
551 regname->sattre |= UNDEF_EQUR;
554 // Skip over symbol token and address
563 // Do not allow use of the CLR.L opcode
567 warn("CLR.L opcode ignored...");
573 // Include binary file (can add addition size & position params, comma separated)
579 uint64_t pos, size, bytesRead;
583 // Check to see if we're in BSS, and, if so, throw an error
586 error("cannot include binary file \"%s\" in BSS section", string[tok[1]]);
592 error("syntax error; file to include missing");
596 // Attempt to open the include file in the current directory, then (if that
597 // failed) try list of include files passed in the enviroment string or by
599 TOKEN filename = tok[1];
601 if ((fd = open(string[filename], _OPEN_INC)) < 0)
603 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
607 // Append path char if necessary
608 if (fd > 0 && buf1[fd - 1] != SLASHCHAR)
609 strcat(buf1, SLASHSTRING);
611 strcat(buf1, string[filename]);
613 if ((fd = open(buf1, _OPEN_INC)) >= 0)
617 return error("cannot open: \"%s\"", string[filename]);
624 size = lseek(fd, 0L, SEEK_END);
625 pos = lseek(fd, 0L, SEEK_SET);
629 // Parse size and position parameters
630 uint64_t requested_size = -1; // -1 means "not set" for these two
634 return error("expected comma after incbin filename");
640 if (abs_expr(&requested_size) != OK)
645 if ((int64_t)requested_size <= 0 || requested_size > size)
648 return error("invalid incbin size requested");
656 return error("expected comma after size parameter");
660 if (abs_expr(&pos) != OK)
665 if ((int64_t)pos <= 0 || pos > size)
668 return error("invalid incbin position requested");
676 return error("extra characters following incbin");
680 // Adjust size if the user didn't specify it via the parameter
681 if (requested_size == -1)
683 requested_size = size - pos;
686 // Are we going to read past the end of the file?
687 if (pos + requested_size > size)
690 return error("invalid combination of incbin position and size");
692 size = requested_size;
694 // All checks passed, let's seek to where the user requested, otherwise at file start
695 lseek(fd, pos, SEEK_SET);
700 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[filename], size); }
702 char * fileBuffer = (char *)malloc(size);
703 bytesRead = read(fd, fileBuffer, size);
705 if (bytesRead != size)
707 error("was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[filename], size);
711 memcpy(chptr, fileBuffer, size);
726 // Set RISC register banks
730 // Deprecated, it's not as if this did anything useful, ever
731 warn("regbank0 ignored");
738 // Deprecated, it's not as if this did anything useful, ever
739 warn("regbank1 ignored");
745 // Helper function, to cut down on mistakes & typing
747 static inline void SkipBytes(unsigned bytesToSkip)
752 if ((scattr & SBSS) == 0)
754 chcheck(bytesToSkip);
755 D_ZEROFILL(bytesToSkip);
762 orgaddr += bytesToSkip;
768 // Adjust location to an EVEN value
773 return error(in_6502mode);
775 unsigned skip = (rgpu || rdsp ? orgaddr : sloc) & 0x01;
779 if ((scattr & SBSS) == 0)
798 // Adjust location to a LONG value
802 unsigned lower2Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x03;
803 unsigned bytesToSkip = (0x04 - lower2Bits) & 0x03;
804 SkipBytes(bytesToSkip);
805 SetLargestAlignment(4);
812 // Adjust location to a PHRASE value
814 // N.B.: We have to handle the GPU/DSP cases separately because you can embed
815 // RISC code in the middle of a regular 68K section. Also note that all
816 // of the alignment pseudo-ops will have to be fixed this way.
818 // This *must* behave differently when in a RISC section, as following sloc
819 // (instead of orgaddr) will fuck things up royally. Note that we do it this
820 // way because you can embed RISC code in a 68K section, and have the origin
821 // pointing to a different alignment in the RISC section than the 68K section.
825 unsigned lower3Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x07;
826 unsigned bytesToSkip = (0x08 - lower3Bits) & 0x07;
827 SkipBytes(bytesToSkip);
828 SetLargestAlignment(8);
835 // Adjust location to a DPHRASE value
839 unsigned lower4Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x0F;
840 unsigned bytesToSkip = (0x10 - lower4Bits) & 0x0F;
841 SkipBytes(bytesToSkip);
842 SetLargestAlignment(16);
849 // Adjust location to a QPHRASE value
853 unsigned lower5Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x1F;
854 unsigned bytesToSkip = (0x20 - lower5Bits) & 0x1F;
855 SkipBytes(bytesToSkip);
856 SetLargestAlignment(32);
863 // Adjust location to <alignment> bytes
867 unsigned bytesToSkip;
870 if (abs_expr(&eval) != OK)
875 return error("Invalid .align value specified");
880 bytesToSkip = eval - sloc % eval;
881 D_ZEROFILL(bytesToSkip*3);
885 bytesToSkip = eval - (rgpu || rdsp ? orgaddr : sloc) % eval;
886 if ( bytesToSkip != eval )
888 if ((scattr & SBSS) == 0)
890 D_ZEROFILL(bytesToSkip);
897 orgaddr += bytesToSkip;
905 // Do auto-even. This must be called ONLY if 'sloc' is odd.
907 // This is made hairy because, if there was a label on the line, we also have
908 // to adjust its value. This won't work with more than one label on the line,
909 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
910 // the C compiler is VERY paranoid and uses ".even" whenever it can
912 // N.B.: This probably needs the same fixes as above...
916 if (cursect != M6502)
919 sloc++; // Bump BSS section
921 D_byte(0); // Deposit 0.b in non-BSS
923 if (lab_sym != NULL) // Bump label if we have to
930 // Unimplemened directive error
934 return error("unimplemented directive");
939 // Return absolute (not TDB) and defined expression or return an error
941 int abs_expr(uint64_t * a_eval)
945 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
948 if (!(eattr & DEFINED))
949 return error(undef_error);
952 return error(rel_error);
959 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
961 int symlist(int(* func)())
963 const char * em = "symbol list syntax";
970 if ((*func)(string[tok[1]]) != OK)
989 // .include "filename"
999 if (*tok == STRING) // Leave strings ALONE
1000 fn = string[*++tok];
1001 else if (*tok == SYMBOL) // Try to append ".s" to symbols
1003 strcpy(buf, string[*++tok]);
1007 else // Punt if no STRING or SYMBOL
1008 return error("missing filename");
1010 // Make sure the user didn't try anything like:
1011 // .include equates.s
1013 return error("extra stuff after filename--enclose it in quotes");
1015 // Attempt to open the include file in the current directory, then (if that
1016 // failed) try list of include files passed in the enviroment string or by
1018 if ((j = open(fn, 0)) < 0)
1020 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
1024 // Append path char if necessary
1025 if (j > 0 && buf1[j - 1] != SLASHCHAR)
1026 strcat(buf1, SLASHSTRING);
1030 if ((j = open(buf1, 0)) >= 0)
1034 return error("cannot open: \"%s\"", fn);
1044 // .assert expression [, expression...]
1051 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
1053 if (!(eattr & DEFINED))
1054 return error("forward or undefined .assert");
1057 return error("assert failure");
1069 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
1071 int globl1(char * p)
1076 return error("cannot .globl local symbol");
1078 if ((sy = lookup(p, LABEL, 0)) == NULL)
1080 sy = NewSymbol(p, LABEL, 0);
1083 //printf("glob1: Making global symbol: attr=%04X, eattr=%08X, %s\n", sy->sattr, sy->sattre, sy->sname);
1086 sy->sattr |= GLOBAL;
1095 return error(in_6502mode);
1103 // .prgflags expression
1105 int d_prgflags(void)
1110 return error("PRGFLAGS requires value");
1111 else if (abs_expr(&eval) == OK)
1113 PRGFLAGS = (uint32_t)eval;
1118 return error("PRGFLAGS requires value");
1124 // .abs [expression]
1131 return error(in_6502mode);
1137 else if (abs_expr(&eval) != OK)
1141 sloc = (uint32_t)eval;
1152 return error("directive forbidden in gpu/dsp mode");
1154 return error(in_6502mode);
1156 if (cursect != TEXT)
1159 SwitchSection(TEXT);
1169 return error("directive forbidden in gpu/dsp mode");
1171 return error(in_6502mode);
1173 if (cursect != DATA)
1176 SwitchSection(DATA);
1186 return error("directive forbidden in gpu/dsp mode");
1188 return error(in_6502mode);
1201 // .ds[.size] expression
1205 DEBUG { printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc); }
1210 if ((cursect & (M6502 | M56KPXYL)) == 0)
1212 if ((siz != SIZB) && (sloc & 1)) // Automatic .even
1216 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1219 // Check to see if the value being passed in is negative (who the hell does
1220 // that?--nobody does; it's the code gremlins, or rum, what does it)
1221 // N.B.: Since 'eval' is of type uint64_t, if it goes negative, it will
1222 // have its high bit set.
1223 if (eval & 0x8000000000000000)
1224 return error("negative sizes not allowed in DS");
1226 // In non-TDB section (BSS, ABS and M6502) just advance the location
1227 // counter appropriately. In TDB sections, deposit (possibly large) chunks
1228 // of zeroed memory....
1229 if ((scattr & SBSS) || cursect == M6502)
1231 listvalue((uint32_t)eval);
1233 sloc += (uint32_t)eval;
1235 if (cursect == M6502)
1238 just_bss = 1; // No data deposited (8-bit CPU mode)
1240 else if (cursect & M56KPXYL)
1242 // Change segment instead of marking blanks.
1243 // Only mark segments we actually wrote something
1244 if (chptr != dsp_currentorg->start && dsp_written_data_in_current_org)
1246 dsp_currentorg->end = chptr;
1248 dsp_currentorg->memtype = dsp_currentorg[-1].memtype;
1251 listvalue((uint32_t)eval);
1252 sloc += (uint32_t)eval;
1254 // And now let's create a new segment
1255 dsp_currentorg->start = chptr;
1256 dsp_currentorg->chunk = scode; // Mark down which chunk this org starts from (will be needed when outputting)
1257 sect[cursect].orgaddr = sloc;
1258 dsp_currentorg->orgadr = sloc;
1259 dsp_written_data_in_current_org = 0;
1261 just_bss = 1; // No data deposited
1265 dep_block(eval, siz, 0, (DEFINED | ABS), NULL);
1274 // dsm[.siz] expression
1275 // Define modulo storage
1276 // Quoting the Motorola assembler manual:
1277 // "The DSM directive reserves a block of memory the length of which in words is equal to
1278 // the value of <expression>.If the runtime location counter is not zero, this directive first
1279 // advances the runtime location counter to a base address that is a multiple of 2k, where
1280 // 2k >= <expression>."
1281 // The kicker of course is written a few sentences after:
1282 // "<label>, if present, will be assigned the value of the runtime location counter after a valid
1283 // base address has been established."
1287 TOKEN * tok_current = tok; // Keep track of where tok was when we entered this procedure
1290 if (abs_expr(&eval) != OK)
1293 // Round up to the next highest power of 2
1294 // Nicked from https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
1303 units_to_skip = eval + 1 - sloc;
1304 sloc += units_to_skip; // Bump up sloc - TODO: check if this goes over the RAM limits?
1306 // If a label has been defined in the same line as dsm, its value also needs to be adjusted
1309 SYM * label = lookup(label_defined, LABEL, 0);
1310 label->svalue += units_to_skip;
1313 tok = tok_current; // Rewind tok back to where it was
1314 return d_ds(siz); // And let d_ds take over from here
1319 // dc.b, dc.w / dc, dc.l, dc.i, dc.q, dc.d, dc.s, dc.x
1327 if ((scattr & SBSS) != 0)
1328 return error("illegal initialization of section");
1330 // Do an auto_even if it's not BYTE sized (hmm, should we be doing this???)
1331 if ((cursect != M6502) && (cursect != M56001P) && (cursect != M56001X)
1332 && (cursect != M56001Y) && (cursect != M56001L)
1333 && (siz != SIZB) && (sloc & 1))
1336 // Check to see if we're trying to set LONGS on a non 32-bit aligned
1337 // address in a GPU or DSP section, in their local RAM
1338 if ((siz == SIZL) && (orgaddr & 0x03)
1339 && ((rgpu && (orgaddr >= 0xF03000) && (orgaddr <= 0xF03FFFF))
1340 || (rdsp && (orgaddr >= 0xF1B000) && (orgaddr <= 0xF1CFFFF))))
1341 warn("depositing LONGs on a non-long address in local RAM");
1345 // dc.b 'string' [,] ...
1346 if (siz == SIZB && (*tok == STRING || *tok == STRINGA8) && (tok[2] == ',' || tok[2] == EOL))
1348 uint32_t i = strlen(string[tok[1]]);
1350 if ((challoc - ch_size) < i)
1355 for(p=string[tok[1]]; *p!=EOS; p++)
1358 else if (*tok == STRINGA8)
1360 for(p=string[tok[1]]; *p!=EOS; p++)
1361 D_byte(strtoa8[*p]);
1365 error("String format not supported... yet");
1372 int movei = 0; // MOVEI flag for dc.i
1381 // dc.x <expression>
1384 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
1387 uint16_t tdb = eattr & TDB;
1388 uint16_t defined = eattr & DEFINED;
1390 // 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 !!!
1393 if (cursect != M56001L)
1397 AddFixup(FU_DSPIMM24 | FU_SEXT, sloc, exprbuf);
1404 double fval = *(double *)&eval;
1405 eval = DoubleToDSPFloat(fval);
1409 if ((uint32_t)eval + 0x1000000 >= 0x2000000)
1410 return error(range_error);
1413 // Deposit DSP word (24-bit)
1419 // In L: we deposit stuff to both X: and Y: instead
1420 // We will be a bit lazy and require that there is a 2nd value
1421 // in the same source line. (Motorola's assembler can parse
1422 // 12-digit hex values, which we can't do at the moment) This
1423 // of course requires to parse 2 values in one pass. If there
1424 // isn't another value in this line, assume X: value is 0.
1431 AddFixup(FU_DSPIMM24 | FU_SEXT, sloc, exprbuf);
1438 float fval = *(float *)&eval;
1439 eval = DoubleToDSPFloat(fval);
1443 if (eval + 0x1000000 >= 0x2000000)
1444 return error(range_error);
1447 // Parse 2nd value if we didn't do this yet
1448 if (secondword == 0)
1450 evaly = (uint32_t)eval;
1455 // If we don't have a : then we're probably at EOL,
1456 // which means the X: value will be 0
1462 tok++; // Eat the comma;
1464 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
1467 defined = (WORD)(eattr & DEFINED);
1472 // Deposit DSP words (24-bit)
1475 sloc--; // We do write 2 DSP words but as far as L: space is concerned we actually advance our counter by one
1488 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1494 return error("non-absolute byte value");
1496 if (eval + 0x100 >= 0x200)
1497 return error("%s (value = $%X)", range_error, eval);
1508 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1513 if (eval + 0x10000 >= 0x20000)
1514 return error(range_error);
1517 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1519 // Deposit 68000 or 6502 (byte-reversed) word
1520 if (cursect != M6502)
1529 // Shamus: Why can't we do longs in 6502 mode?
1531 return error(in_6502mode);
1535 AddFixup(FU_LONG | (movei ? FU_MOVEI : 0), sloc, exprbuf);
1541 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1544 eval = WORDSWAP32(eval);
1554 return error(in_6502mode);
1556 // DEFINITELY NEED FIXUPS HERE!
1559 AddFixup(FU_QUAD, sloc, exprbuf);
1567 // 32-bit float size
1569 return error(in_6502mode);
1571 /* Seems to me that if something is undefined here, then that should be an error. Likewise for the D & X variants. */
1574 // AddFixup(FU_FLOATSING, sloc, exprbuf);
1576 return error("labels not allowed in floating point expressions");
1580 //Would this *ever* happen?
1582 // MarkRelocatable(cursect, sloc, tdb, MSINGLE, NULL);
1586 uint32_t ieee754 = FloatToIEEE754((float)*ptr.dp);
1593 // 64-bit double size
1595 return error(in_6502mode);
1599 // AddFixup(FU_FLOATDOUB, sloc, exprbuf);
1601 return error("labels not allowed in floating point expressions");
1605 //Would this *ever* happen?
1607 // MarkRelocatable(cursect, sloc, tdb, MDOUBLE, NULL);
1611 uint64_t ieee754 = DoubleToIEEE754(*ptr.dp);
1619 return error(in_6502mode);
1622 memset(extDbl, 0, 12);
1626 // AddFixup(FU_FLOATEXT, sloc, exprbuf);
1627 // D_extend(extDbl);
1628 return error("labels not allowed in floating point expressions");
1632 //Would this *ever* happen?
1634 // MarkRelocatable(cursect, sloc, tdb, MEXTEND, NULL);
1638 DoubleToExtended(*ptr.dp, extDbl);
1656 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1660 uint64_t evalc, eval;
1663 DEBUG { printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr); }
1665 if ((scattr & SBSS) != 0)
1666 return error("illegal initialization of section");
1668 if (abs_expr(&evalc) != OK)
1672 return error("missing comma");
1674 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1677 if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1680 dep_block((uint32_t)evalc, siz, (uint32_t)eval, eattr, exprbuf);
1686 // Generalized initialization directive
1688 // .init[.siz] [#count,] expression [.size] , ...
1690 // The size suffix on the ".init" directive becomes the default size of the
1691 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1692 // and an expression, it specifies a repeat count. The value to be deposited
1693 // may be followed by a size suffix, which overrides the default size.
1695 int d_init(WORD def_siz)
1702 if ((scattr & SBSS) != 0)
1703 return error(".init not permitted in BSS or ABS");
1706 return error("directive forbidden in gpu/dsp mode");
1710 // Get repeat count (defaults to 1)
1715 if (abs_expr(&count) != OK)
1719 return error(comma_error);
1724 // Evaluate expression to deposit
1725 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1729 { // Determine size of object to deposit
1730 case DOTB: siz = SIZB; break;
1731 case DOTW: siz = SIZB; break;
1732 case DOTL: siz = SIZL; break;
1739 dep_block((uint32_t)count, siz, (uint32_t)eval, eattr, exprbuf);
1749 return error(comma_error);
1756 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1758 int dep_block(uint32_t count, WORD siz, uint32_t eval, WORD eattr, TOKEN * exprbuf)
1760 WORD tdb = eattr & TDB;
1761 WORD defined = eattr & DEFINED;
1765 if ((challoc - ch_size) < 4)
1773 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1779 return error("non-absolute byte value");
1781 if (eval + 0x100 >= 0x200)
1782 return error(range_error);
1792 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1798 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1800 if (eval + 0x10000 >= 0x20000)
1801 return error(range_error);
1803 // Deposit 68000 or 6502 (byte-reversed) word
1804 if (cursect != M6502)
1814 return error(in_6502mode);
1818 AddFixup(FU_LONG, sloc, exprbuf);
1824 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1838 // .comm symbol, size
1847 return error(in_6502mode);
1850 return error("missing symbol");
1855 if (*p == '.') // Cannot .comm a local symbol
1856 return error(locgl_error);
1858 if ((sym = lookup(p, LABEL, 0)) == NULL)
1859 sym = NewSymbol(p, LABEL, 0);
1862 if (sym->sattr & DEFINED)
1863 return error(".comm symbol already defined");
1866 sym->sattr = GLOBAL | COMMON | BSS;
1869 return error(comma_error);
1871 if (abs_expr(&eval) != OK) // Parse size of common region
1874 sym->svalue = eval; // Install common symbol's size
1881 // .list - Turn listing on
1893 // .nlist - Turn listing off
1905 // .68000 - Back to 68000 TEXT segment
1909 rgpu = rdsp = robjproc = dsp56001 = 0;
1910 // Switching from gpu/dsp sections should reset any ORG'd Address
1914 SwitchSection(TEXT);
1915 activecpu = CPU_68000;
1916 regbase = reg68base; // Update register DFA tables
1918 regcheck = reg68check;
1919 regaccept = reg68accept;
1925 // .68020 - Back to 68000 TEXT segment and select 68020
1930 activecpu = CPU_68020;
1936 // .68030 - Back to 68000 TEXT segment and select 68030
1941 activecpu = CPU_68030;
1947 // .68040 - Back to 68000 TEXT segment and select 68040
1952 activecpu = CPU_68040;
1953 activefpu = FPU_68040;
1959 // .68060 - Back to 68000 TEXT segment and select 68060
1964 activecpu = CPU_68060;
1965 activefpu = FPU_68060;
1971 // .68881 - Back to 680x0 TEXT segment and select 68881 FPU
1975 activefpu = FPU_68881;
1976 regbase = reg68base; // Update register DFA tables
1978 regcheck = reg68check;
1979 regaccept = reg68accept;
1985 // .68882 - Back to 680x0 TEXT segment and select 68882 FPU
1989 activefpu = FPU_68882;
1990 regbase = reg68base; // Update register DFA tables
1992 regcheck = reg68check;
1993 regaccept = reg68accept;
1999 // nofpu - Deselect FPUs.
2003 activefpu = FPU_NONE;
2009 // .56001 - Switch to DSP56001 assembler
2014 rgpu = rdsp = robjproc = 0;
2017 if ((obj_format == LOD) || (obj_format == P56))
2018 SwitchSection(M56001P);
2020 regbase = reg56base; // Update register DFA tables
2022 regcheck = reg56check;
2023 regaccept = reg56accept;
2029 // .gpu - Switch to GPU assembler
2033 if ((cursect != TEXT) && (cursect != DATA))
2035 error(".gpu can only be used in the TEXT or DATA segments");
2039 // If previous section was DSP or 68000 then we need to reset ORG'd Addresses
2046 rgpu = 1; // Set GPU assembly
2047 rdsp = 0; // Unset DSP assembly
2048 robjproc = 0; // Unset OP assembly
2049 dsp56001 = 0; // Unset 56001 assembly
2051 regbase = regriscbase; // Update register DFA tables
2052 regtab = regrisctab;
2053 regcheck = regrisccheck;
2054 regaccept = regriscaccept;
2060 // .dsp - Switch to DSP assembler
2064 if ((cursect != TEXT) && (cursect != DATA))
2066 error(".dsp can only be used in the TEXT or DATA segments");
2070 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
2077 rdsp = 1; // Set DSP assembly
2078 rgpu = 0; // Unset GPU assembly
2079 robjproc = 0; // Unset OP assembly
2080 dsp56001 = 0; // Unset 56001 assembly
2082 regbase = regriscbase; // Update register DFA tables
2083 regtab = regrisctab;
2084 regcheck = regrisccheck;
2085 regaccept = regriscaccept;
2091 // .cargs [#offset], symbol[.size], ...
2093 // Lists of registers may also be mentioned; they just take up space. Good for
2094 // "documentation" purposes:
2096 // .cargs a6, .arg1, .arg2, .arg3...
2098 // Symbols thus created are ABS and EQUATED.
2102 uint64_t eval = 4; // Default to 4 if no offset specified (to account for
2111 return error("directive forbidden in gpu/dsp mode");
2117 if (abs_expr(&eval) != OK)
2120 // Eat the comma, if it's there
2131 // Set env to either local (dot prefixed) or global scope
2132 env = (*p == '.' ? curenv : 0);
2133 symbol = lookup(p, LABEL, env);
2137 symbol = NewSymbol(p, LABEL, env);
2140 else if (symbol->sattr & DEFINED)
2141 return error("multiply-defined label '%s'", p);
2143 // Put symbol in "order of definition" list
2144 AddToSymbolDeclarationList(symbol);
2146 symbol->sattr |= (ABS | DEFINED | EQUATED);
2147 symbol->svalue = eval;
2150 // What this does is eat any dot suffixes attached to a symbol. If
2151 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
2152 // there is no dot suffix, it assumes a size of 2.
2164 else if (*tok >= REG68_D0 && *tok <= REG68_A7)
2166 if (reglist(&rlist) < 0)
2169 for(i=0; i<16; i++, rlist>>=1)
2192 return error(".cargs syntax");
2196 // Eat commas in between each argument, if they exist
2204 // .cstruct [#offset], symbol[.size], ...
2206 // Lists of registers may also be mentioned; they just take up space. Good for
2207 // "documentation" purposes:
2209 // .cstruct a6, .arg1, .arg2, .arg3...
2211 // Symbols thus created are ABS and EQUATED. Note that this is for
2212 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
2217 uint64_t eval = 0; // Default, if no offset specified, is zero
2225 return error("directive forbidden in gpu/dsp mode");
2231 if (abs_expr(&eval) != OK)
2234 // Eat the comma, if it's there
2243 symbolName = string[tok[1]];
2245 // Set env to either local (dot prefixed) or global scope
2246 env = (symbolName[0] == '.' ? curenv : 0);
2247 symbol = lookup(symbolName, LABEL, env);
2249 // If the symbol wasn't found, then define it. Otherwise, throw an
2253 symbol = NewSymbol(symbolName, LABEL, env);
2256 else if (symbol->sattr & DEFINED)
2257 return error("multiply-defined label '%s'", symbolName);
2259 // Put symbol in "order of definition" list
2260 AddToSymbolDeclarationList(symbol);
2264 // Adjust label start address if it's a word or a long, as a byte
2265 // label might have left us on an odd address.
2270 eval += eval & 0x01;
2273 symbol->sattr |= (ABS | DEFINED | EQUATED);
2274 symbol->svalue = eval;
2276 // Check for dot suffixes and adjust space accordingly (longs and
2277 // words on an odd boundary get bumped to the next word aligned
2278 // address). If no suffix, then throw an error.
2291 return error("Symbol missing dot suffix in .cstruct construct");
2296 else if (*tok >= REG68_D0 && *tok <= REG68_A7)
2298 if (reglist(&rlist) < 0)
2301 for(i=0; i<16; i++, rlist>>=1)
2324 return error(".cstruct syntax");
2328 // Eat commas in between each argument, if they exist
2336 // Define start of OP object list (allows the use of ORG)
2340 if ((cursect != TEXT) && (cursect != DATA))
2342 error(".objproc can only be used in the TEXT or DATA segments");
2346 // If previous section was DSP or 68000 then we need to reset ORG'd
2354 robjproc = 1; // Set OP assembly
2355 rgpu = 0; // Unset GPU assembly
2356 rdsp = 0; // Unset DSP assembly
2357 dsp56001 = 0; // Unset 56001 assembly
2363 // Undefine a macro - .undefmac macname [, macname...]
2365 int undmac1(char * p)
2367 SYM * symbol = lookup(p, MACRO, 0);
2369 // If the macro symbol exists, cause it to disappear
2371 symbol->stype = (BYTE)SY_UNDEF;
2386 warn("JPAD directive is deprecated/non-functional");
2393 warn("NOJPAD directive is deprecated/non-functional");
2400 return error("What the hell? Do you think we adhere to the Goof standard?");
2405 // .opt - turn a specific (or all) optimisation on or off
2414 char * tmpstr = string[*tok++];
2416 if (ParseOptimization(tmpstr) != OK)
2417 return error("unknown optimization flag '%s'", tmpstr);
2420 return error(".opt directive needs every switch enclosed inside quotation marks");
2428 // .if, Start conditional assembly
2435 IFENT * rif = f_ifent;
2439 rif = (IFENT *)malloc(sizeof(IFENT));
2441 f_ifent = rif->if_prev;
2443 rif->if_prev = ifent;
2448 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
2451 if ((eattr & DEFINED) == 0)
2452 return error(undef_error);
2457 rif->if_state = (WORD)disabled;
2463 // .else, Do alternate case for .if
2467 IFENT * rif = ifent;
2469 if (rif->if_prev == NULL)
2470 return error("mismatched .else");
2473 disabled = rif->if_prev->if_state;
2477 rif->if_state = (WORD)disabled;
2483 // .endif, End of conditional assembly block
2484 // This is also called by fpop() to pop levels of IFENTs in case a macro or
2485 // include file exits early with `exitm' or `end'.
2489 IFENT * rif = ifent;
2491 if (rif->if_prev == NULL)
2492 return error("mismatched .endif");
2494 ifent = rif->if_prev;
2495 disabled = rif->if_prev->if_state;
2496 rif->if_prev = f_ifent;