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]);
626 // Check size parameter (can be omitted)
631 if (abs_expr(&size) != OK)
636 if ((int64_t)size <= 0)
638 return error("invalid incbin size requested");
642 size = lseek(fd, 0L, SEEK_END);
645 // Check offset parameter (can be omitted)
652 if (abs_expr(&pos) != OK)
658 lseek(fd, pos, SEEK_SET);
659 if ((int64_t)(size - pos) < 0)
661 return error("requested incbin size out of range");
666 // offset parameter omitted, so it's 0
667 pos = lseek(fd, 0L, SEEK_SET);
671 return error(comma_error);
674 pos = lseek(fd, 0L, SEEK_SET);
678 // size & pos not given, so assume offset of 0 and all of the binary
679 size = lseek(fd, 0L, SEEK_END);
680 pos = lseek(fd, 0L, SEEK_SET);
685 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[filename], size); }
687 char * fileBuffer = (char *)malloc(size);
688 bytesRead = read(fd, fileBuffer, size);
690 if (bytesRead != size)
692 error("was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[filename], size);
696 memcpy(chptr, fileBuffer, size);
711 // Set RISC register banks
715 // Deprecated, it's not as if this did anything useful, ever
716 warn("regbank0 ignored");
723 // Deprecated, it's not as if this did anything useful, ever
724 warn("regbank1 ignored");
730 // Helper function, to cut down on mistakes & typing
732 static inline void SkipBytes(unsigned bytesToSkip)
737 if ((scattr & SBSS) == 0)
739 chcheck(bytesToSkip);
740 D_ZEROFILL(bytesToSkip);
747 orgaddr += bytesToSkip;
753 // Adjust location to an EVEN value
758 return error(in_6502mode);
760 unsigned skip = (rgpu || rdsp ? orgaddr : sloc) & 0x01;
764 if ((scattr & SBSS) == 0)
783 // Adjust location to a LONG value
787 unsigned lower2Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x03;
788 unsigned bytesToSkip = (0x04 - lower2Bits) & 0x03;
789 SkipBytes(bytesToSkip);
790 SetLargestAlignment(4);
797 // Adjust location to a PHRASE value
799 // N.B.: We have to handle the GPU/DSP cases separately because you can embed
800 // RISC code in the middle of a regular 68K section. Also note that all
801 // of the alignment pseudo-ops will have to be fixed this way.
803 // This *must* behave differently when in a RISC section, as following sloc
804 // (instead of orgaddr) will fuck things up royally. Note that we do it this
805 // way because you can embed RISC code in a 68K section, and have the origin
806 // pointing to a different alignment in the RISC section than the 68K section.
810 unsigned lower3Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x07;
811 unsigned bytesToSkip = (0x08 - lower3Bits) & 0x07;
812 SkipBytes(bytesToSkip);
813 SetLargestAlignment(8);
820 // Adjust location to a DPHRASE value
824 unsigned lower4Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x0F;
825 unsigned bytesToSkip = (0x10 - lower4Bits) & 0x0F;
826 SkipBytes(bytesToSkip);
827 SetLargestAlignment(16);
834 // Adjust location to a QPHRASE value
838 unsigned lower5Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x1F;
839 unsigned bytesToSkip = (0x20 - lower5Bits) & 0x1F;
840 SkipBytes(bytesToSkip);
841 SetLargestAlignment(32);
848 // Adjust location to <alignment> bytes
852 unsigned bytesToSkip;
855 if (abs_expr(&eval) != OK)
860 return error("Invalid .align value specified");
865 bytesToSkip = eval - sloc % eval;
866 D_ZEROFILL(bytesToSkip*3);
870 bytesToSkip = eval - (rgpu || rdsp ? orgaddr : sloc) % eval;
871 if ( bytesToSkip != eval )
873 if ((scattr & SBSS) == 0)
875 D_ZEROFILL(bytesToSkip);
882 orgaddr += bytesToSkip;
890 // Do auto-even. This must be called ONLY if 'sloc' is odd.
892 // This is made hairy because, if there was a label on the line, we also have
893 // to adjust its value. This won't work with more than one label on the line,
894 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
895 // the C compiler is VERY paranoid and uses ".even" whenever it can
897 // N.B.: This probably needs the same fixes as above...
901 if (cursect != M6502)
904 sloc++; // Bump BSS section
906 D_byte(0); // Deposit 0.b in non-BSS
908 if (lab_sym != NULL) // Bump label if we have to
915 // Unimplemened directive error
919 return error("unimplemented directive");
924 // Return absolute (not TDB) and defined expression or return an error
926 int abs_expr(uint64_t * a_eval)
930 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
933 if (!(eattr & DEFINED))
934 return error(undef_error);
937 return error(rel_error);
944 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
946 int symlist(int(* func)())
948 const char * em = "symbol list syntax";
955 if ((*func)(string[tok[1]]) != OK)
974 // .include "filename"
984 if (*tok == STRING) // Leave strings ALONE
986 else if (*tok == SYMBOL) // Try to append ".s" to symbols
988 strcpy(buf, string[*++tok]);
992 else // Punt if no STRING or SYMBOL
993 return error("missing filename");
995 // Make sure the user didn't try anything like:
996 // .include equates.s
998 return error("extra stuff after filename--enclose it in quotes");
1000 // Attempt to open the include file in the current directory, then (if that
1001 // failed) try list of include files passed in the enviroment string or by
1003 if ((j = open(fn, 0)) < 0)
1005 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
1009 // Append path char if necessary
1010 if (j > 0 && buf1[j - 1] != SLASHCHAR)
1011 strcat(buf1, SLASHSTRING);
1015 if ((j = open(buf1, 0)) >= 0)
1019 return error("cannot open: \"%s\"", fn);
1029 // .assert expression [, expression...]
1036 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
1038 if (!(eattr & DEFINED))
1039 return error("forward or undefined .assert");
1042 return error("assert failure");
1054 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
1056 int globl1(char * p)
1061 return error("cannot .globl local symbol");
1063 if ((sy = lookup(p, LABEL, 0)) == NULL)
1065 sy = NewSymbol(p, LABEL, 0);
1068 //printf("glob1: Making global symbol: attr=%04X, eattr=%08X, %s\n", sy->sattr, sy->sattre, sy->sname);
1071 sy->sattr |= GLOBAL;
1080 return error(in_6502mode);
1088 // .prgflags expression
1090 int d_prgflags(void)
1095 return error("PRGFLAGS requires value");
1096 else if (abs_expr(&eval) == OK)
1098 PRGFLAGS = (uint32_t)eval;
1103 return error("PRGFLAGS requires value");
1109 // .abs [expression]
1116 return error(in_6502mode);
1122 else if (abs_expr(&eval) != OK)
1126 sloc = (uint32_t)eval;
1137 return error("directive forbidden in gpu/dsp mode");
1139 return error(in_6502mode);
1141 if (cursect != TEXT)
1144 SwitchSection(TEXT);
1154 return error("directive forbidden in gpu/dsp mode");
1156 return error(in_6502mode);
1158 if (cursect != DATA)
1161 SwitchSection(DATA);
1171 return error("directive forbidden in gpu/dsp mode");
1173 return error(in_6502mode);
1186 // .ds[.size] expression
1190 DEBUG { printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc); }
1195 if ((cursect & (M6502 | M56KPXYL)) == 0)
1197 if ((siz != SIZB) && (sloc & 1)) // Automatic .even
1201 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1204 // Check to see if the value being passed in is negative (who the hell does
1205 // that?--nobody does; it's the code gremlins, or rum, what does it)
1206 // N.B.: Since 'eval' is of type uint64_t, if it goes negative, it will
1207 // have its high bit set.
1208 if (eval & 0x8000000000000000)
1209 return error("negative sizes not allowed in DS");
1211 // In non-TDB section (BSS, ABS and M6502) just advance the location
1212 // counter appropriately. In TDB sections, deposit (possibly large) chunks
1213 // of zeroed memory....
1214 if ((scattr & SBSS) || cursect == M6502)
1216 listvalue((uint32_t)eval);
1218 sloc += (uint32_t)eval;
1220 if (cursect == M6502)
1223 just_bss = 1; // No data deposited (8-bit CPU mode)
1225 else if (cursect & M56KPXYL)
1227 // Change segment instead of marking blanks.
1228 // Only mark segments we actually wrote something
1229 if (chptr != dsp_currentorg->start && dsp_written_data_in_current_org)
1231 dsp_currentorg->end = chptr;
1233 dsp_currentorg->memtype = dsp_currentorg[-1].memtype;
1236 listvalue((uint32_t)eval);
1237 sloc += (uint32_t)eval;
1239 // And now let's create a new segment
1240 dsp_currentorg->start = chptr;
1241 dsp_currentorg->chunk = scode; // Mark down which chunk this org starts from (will be needed when outputting)
1242 sect[cursect].orgaddr = sloc;
1243 dsp_currentorg->orgadr = sloc;
1244 dsp_written_data_in_current_org = 0;
1246 just_bss = 1; // No data deposited
1250 dep_block(eval, siz, 0, (DEFINED | ABS), NULL);
1259 // dsm[.siz] expression
1260 // Define modulo storage
1261 // Quoting the Motorola assembler manual:
1262 // "The DSM directive reserves a block of memory the length of which in words is equal to
1263 // the value of <expression>.If the runtime location counter is not zero, this directive first
1264 // advances the runtime location counter to a base address that is a multiple of 2k, where
1265 // 2k >= <expression>."
1266 // The kicker of course is written a few sentences after:
1267 // "<label>, if present, will be assigned the value of the runtime location counter after a valid
1268 // base address has been established."
1272 TOKEN * tok_current = tok; // Keep track of where tok was when we entered this procedure
1275 if (abs_expr(&eval) != OK)
1278 // Round up to the next highest power of 2
1279 // Nicked from https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
1288 units_to_skip = eval + 1 - sloc;
1289 sloc += units_to_skip; // Bump up sloc - TODO: check if this goes over the RAM limits?
1291 // If a label has been defined in the same line as dsm, its value also needs to be adjusted
1294 SYM * label = lookup(label_defined, LABEL, 0);
1295 label->svalue += units_to_skip;
1298 tok = tok_current; // Rewind tok back to where it was
1299 return d_ds(siz); // And let d_ds take over from here
1304 // dc.b, dc.w / dc, dc.l, dc.i, dc.q, dc.d, dc.s, dc.x
1312 if ((scattr & SBSS) != 0)
1313 return error("illegal initialization of section");
1315 // Do an auto_even if it's not BYTE sized (hmm, should we be doing this???)
1316 if ((cursect != M6502) && (cursect != M56001P) && (cursect != M56001X)
1317 && (cursect != M56001Y) && (cursect != M56001L)
1318 && (siz != SIZB) && (sloc & 1))
1321 // Check to see if we're trying to set LONGS on a non 32-bit aligned
1322 // address in a GPU or DSP section, in their local RAM
1323 if ((siz == SIZL) && (orgaddr & 0x03)
1324 && ((rgpu && (orgaddr >= 0xF03000) && (orgaddr <= 0xF03FFFF))
1325 || (rdsp && (orgaddr >= 0xF1B000) && (orgaddr <= 0xF1CFFFF))))
1326 warn("depositing LONGs on a non-long address in local RAM");
1330 // dc.b 'string' [,] ...
1331 if (siz == SIZB && (*tok == STRING || *tok == STRINGA8) && (tok[2] == ',' || tok[2] == EOL))
1333 uint32_t i = strlen(string[tok[1]]);
1335 if ((challoc - ch_size) < i)
1340 for(p=string[tok[1]]; *p!=EOS; p++)
1343 else if (*tok == STRINGA8)
1345 for(p=string[tok[1]]; *p!=EOS; p++)
1346 D_byte(strtoa8[*p]);
1350 error("String format not supported... yet");
1357 int movei = 0; // MOVEI flag for dc.i
1366 // dc.x <expression>
1369 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
1372 uint16_t tdb = eattr & TDB;
1373 uint16_t defined = eattr & DEFINED;
1375 // 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 !!!
1378 if (cursect != M56001L)
1382 AddFixup(FU_DSPIMM24 | FU_SEXT, sloc, exprbuf);
1389 double fval = *(double *)&eval;
1390 eval = DoubleToDSPFloat(fval);
1394 if ((uint32_t)eval + 0x1000000 >= 0x2000000)
1395 return error(range_error);
1398 // Deposit DSP word (24-bit)
1404 // In L: we deposit stuff to both X: and Y: instead
1405 // We will be a bit lazy and require that there is a 2nd value
1406 // in the same source line. (Motorola's assembler can parse
1407 // 12-digit hex values, which we can't do at the moment) This
1408 // of course requires to parse 2 values in one pass. If there
1409 // isn't another value in this line, assume X: value is 0.
1416 AddFixup(FU_DSPIMM24 | FU_SEXT, sloc, exprbuf);
1423 float fval = *(float *)&eval;
1424 eval = DoubleToDSPFloat(fval);
1428 if (eval + 0x1000000 >= 0x2000000)
1429 return error(range_error);
1432 // Parse 2nd value if we didn't do this yet
1433 if (secondword == 0)
1435 evaly = (uint32_t)eval;
1440 // If we don't have a : then we're probably at EOL,
1441 // which means the X: value will be 0
1447 tok++; // Eat the comma;
1449 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
1452 defined = (WORD)(eattr & DEFINED);
1457 // Deposit DSP words (24-bit)
1460 sloc--; // We do write 2 DSP words but as far as L: space is concerned we actually advance our counter by one
1473 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1479 return error("non-absolute byte value");
1481 if (eval + 0x100 >= 0x200)
1482 return error("%s (value = $%X)", range_error, eval);
1493 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1498 if (eval + 0x10000 >= 0x20000)
1499 return error(range_error);
1502 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1504 // Deposit 68000 or 6502 (byte-reversed) word
1505 if (cursect != M6502)
1514 // Shamus: Why can't we do longs in 6502 mode?
1516 return error(in_6502mode);
1520 AddFixup(FU_LONG | (movei ? FU_MOVEI : 0), sloc, exprbuf);
1526 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1529 eval = WORDSWAP32(eval);
1539 return error(in_6502mode);
1541 // DEFINITELY NEED FIXUPS HERE!
1544 AddFixup(FU_QUAD, sloc, exprbuf);
1552 // 32-bit float size
1554 return error(in_6502mode);
1556 /* Seems to me that if something is undefined here, then that should be an error. Likewise for the D & X variants. */
1559 // AddFixup(FU_FLOATSING, sloc, exprbuf);
1561 return error("labels not allowed in floating point expressions");
1565 //Would this *ever* happen?
1567 // MarkRelocatable(cursect, sloc, tdb, MSINGLE, NULL);
1571 uint32_t ieee754 = FloatToIEEE754((float)*ptr.dp);
1578 // 64-bit double size
1580 return error(in_6502mode);
1584 // AddFixup(FU_FLOATDOUB, sloc, exprbuf);
1586 return error("labels not allowed in floating point expressions");
1590 //Would this *ever* happen?
1592 // MarkRelocatable(cursect, sloc, tdb, MDOUBLE, NULL);
1596 uint64_t ieee754 = DoubleToIEEE754(*ptr.dp);
1604 return error(in_6502mode);
1607 memset(extDbl, 0, 12);
1611 // AddFixup(FU_FLOATEXT, sloc, exprbuf);
1612 // D_extend(extDbl);
1613 return error("labels not allowed in floating point expressions");
1617 //Would this *ever* happen?
1619 // MarkRelocatable(cursect, sloc, tdb, MEXTEND, NULL);
1623 DoubleToExtended(*ptr.dp, extDbl);
1641 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1645 uint64_t evalc, eval;
1648 DEBUG { printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr); }
1650 if ((scattr & SBSS) != 0)
1651 return error("illegal initialization of section");
1653 if (abs_expr(&evalc) != OK)
1657 return error("missing comma");
1659 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1662 if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1665 dep_block((uint32_t)evalc, siz, (uint32_t)eval, eattr, exprbuf);
1671 // Generalized initialization directive
1673 // .init[.siz] [#count,] expression [.size] , ...
1675 // The size suffix on the ".init" directive becomes the default size of the
1676 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1677 // and an expression, it specifies a repeat count. The value to be deposited
1678 // may be followed by a size suffix, which overrides the default size.
1680 int d_init(WORD def_siz)
1687 if ((scattr & SBSS) != 0)
1688 return error(".init not permitted in BSS or ABS");
1691 return error("directive forbidden in gpu/dsp mode");
1695 // Get repeat count (defaults to 1)
1700 if (abs_expr(&count) != OK)
1704 return error(comma_error);
1709 // Evaluate expression to deposit
1710 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1714 { // Determine size of object to deposit
1715 case DOTB: siz = SIZB; break;
1716 case DOTW: siz = SIZB; break;
1717 case DOTL: siz = SIZL; break;
1724 dep_block((uint32_t)count, siz, (uint32_t)eval, eattr, exprbuf);
1734 return error(comma_error);
1741 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1743 int dep_block(uint32_t count, WORD siz, uint32_t eval, WORD eattr, TOKEN * exprbuf)
1745 WORD tdb = eattr & TDB;
1746 WORD defined = eattr & DEFINED;
1750 if ((challoc - ch_size) < 4)
1758 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1764 return error("non-absolute byte value");
1766 if (eval + 0x100 >= 0x200)
1767 return error(range_error);
1777 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1783 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1785 if (eval + 0x10000 >= 0x20000)
1786 return error(range_error);
1788 // Deposit 68000 or 6502 (byte-reversed) word
1789 if (cursect != M6502)
1799 return error(in_6502mode);
1803 AddFixup(FU_LONG, sloc, exprbuf);
1809 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1823 // .comm symbol, size
1832 return error(in_6502mode);
1835 return error("missing symbol");
1840 if (*p == '.') // Cannot .comm a local symbol
1841 return error(locgl_error);
1843 if ((sym = lookup(p, LABEL, 0)) == NULL)
1844 sym = NewSymbol(p, LABEL, 0);
1847 if (sym->sattr & DEFINED)
1848 return error(".comm symbol already defined");
1851 sym->sattr = GLOBAL | COMMON | BSS;
1854 return error(comma_error);
1856 if (abs_expr(&eval) != OK) // Parse size of common region
1859 sym->svalue = eval; // Install common symbol's size
1866 // .list - Turn listing on
1878 // .nlist - Turn listing off
1890 // .68000 - Back to 68000 TEXT segment
1894 rgpu = rdsp = robjproc = dsp56001 = 0;
1895 // Switching from gpu/dsp sections should reset any ORG'd Address
1899 SwitchSection(TEXT);
1900 activecpu = CPU_68000;
1901 regbase = reg68base; // Update register DFA tables
1903 regcheck = reg68check;
1904 regaccept = reg68accept;
1910 // .68020 - Back to 68000 TEXT segment and select 68020
1915 activecpu = CPU_68020;
1921 // .68030 - Back to 68000 TEXT segment and select 68030
1926 activecpu = CPU_68030;
1932 // .68040 - Back to 68000 TEXT segment and select 68040
1937 activecpu = CPU_68040;
1938 activefpu = FPU_68040;
1944 // .68060 - Back to 68000 TEXT segment and select 68060
1949 activecpu = CPU_68060;
1950 activefpu = FPU_68060;
1956 // .68881 - Back to 680x0 TEXT segment and select 68881 FPU
1960 activefpu = FPU_68881;
1961 regbase = reg68base; // Update register DFA tables
1963 regcheck = reg68check;
1964 regaccept = reg68accept;
1970 // .68882 - Back to 680x0 TEXT segment and select 68882 FPU
1974 activefpu = FPU_68882;
1975 regbase = reg68base; // Update register DFA tables
1977 regcheck = reg68check;
1978 regaccept = reg68accept;
1984 // nofpu - Deselect FPUs.
1988 activefpu = FPU_NONE;
1994 // .56001 - Switch to DSP56001 assembler
1999 rgpu = rdsp = robjproc = 0;
2002 if ((obj_format == LOD) || (obj_format == P56))
2003 SwitchSection(M56001P);
2005 regbase = reg56base; // Update register DFA tables
2007 regcheck = reg56check;
2008 regaccept = reg56accept;
2014 // .gpu - Switch to GPU assembler
2018 if ((cursect != TEXT) && (cursect != DATA))
2020 error(".gpu can only be used in the TEXT or DATA segments");
2024 // If previous section was DSP or 68000 then we need to reset ORG'd Addresses
2031 rgpu = 1; // Set GPU assembly
2032 rdsp = 0; // Unset DSP assembly
2033 robjproc = 0; // Unset OP assembly
2034 dsp56001 = 0; // Unset 56001 assembly
2036 regbase = regriscbase; // Update register DFA tables
2037 regtab = regrisctab;
2038 regcheck = regrisccheck;
2039 regaccept = regriscaccept;
2045 // .dsp - Switch to DSP assembler
2049 if ((cursect != TEXT) && (cursect != DATA))
2051 error(".dsp can only be used in the TEXT or DATA segments");
2055 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
2062 rdsp = 1; // Set DSP assembly
2063 rgpu = 0; // Unset GPU assembly
2064 robjproc = 0; // Unset OP assembly
2065 dsp56001 = 0; // Unset 56001 assembly
2067 regbase = regriscbase; // Update register DFA tables
2068 regtab = regrisctab;
2069 regcheck = regrisccheck;
2070 regaccept = regriscaccept;
2076 // .cargs [#offset], symbol[.size], ...
2078 // Lists of registers may also be mentioned; they just take up space. Good for
2079 // "documentation" purposes:
2081 // .cargs a6, .arg1, .arg2, .arg3...
2083 // Symbols thus created are ABS and EQUATED.
2087 uint64_t eval = 4; // Default to 4 if no offset specified (to account for
2096 return error("directive forbidden in gpu/dsp mode");
2102 if (abs_expr(&eval) != OK)
2105 // Eat the comma, if it's there
2116 // Set env to either local (dot prefixed) or global scope
2117 env = (*p == '.' ? curenv : 0);
2118 symbol = lookup(p, LABEL, env);
2122 symbol = NewSymbol(p, LABEL, env);
2125 else if (symbol->sattr & DEFINED)
2126 return error("multiply-defined label '%s'", p);
2128 // Put symbol in "order of definition" list
2129 AddToSymbolDeclarationList(symbol);
2131 symbol->sattr |= (ABS | DEFINED | EQUATED);
2132 symbol->svalue = eval;
2135 // What this does is eat any dot suffixes attached to a symbol. If
2136 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
2137 // there is no dot suffix, it assumes a size of 2.
2149 else if (*tok >= REG68_D0 && *tok <= REG68_A7)
2151 if (reglist(&rlist) < 0)
2154 for(i=0; i<16; i++, rlist>>=1)
2177 return error(".cargs syntax");
2181 // Eat commas in between each argument, if they exist
2189 // .cstruct [#offset], symbol[.size], ...
2191 // Lists of registers may also be mentioned; they just take up space. Good for
2192 // "documentation" purposes:
2194 // .cstruct a6, .arg1, .arg2, .arg3...
2196 // Symbols thus created are ABS and EQUATED. Note that this is for
2197 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
2202 uint64_t eval = 0; // Default, if no offset specified, is zero
2210 return error("directive forbidden in gpu/dsp mode");
2216 if (abs_expr(&eval) != OK)
2219 // Eat the comma, if it's there
2228 symbolName = string[tok[1]];
2230 // Set env to either local (dot prefixed) or global scope
2231 env = (symbolName[0] == '.' ? curenv : 0);
2232 symbol = lookup(symbolName, LABEL, env);
2234 // If the symbol wasn't found, then define it. Otherwise, throw an
2238 symbol = NewSymbol(symbolName, LABEL, env);
2241 else if (symbol->sattr & DEFINED)
2242 return error("multiply-defined label '%s'", symbolName);
2244 // Put symbol in "order of definition" list
2245 AddToSymbolDeclarationList(symbol);
2249 // Adjust label start address if it's a word or a long, as a byte
2250 // label might have left us on an odd address.
2255 eval += eval & 0x01;
2258 symbol->sattr |= (ABS | DEFINED | EQUATED);
2259 symbol->svalue = eval;
2261 // Check for dot suffixes and adjust space accordingly (longs and
2262 // words on an odd boundary get bumped to the next word aligned
2263 // address). If no suffix, then throw an error.
2276 return error("Symbol missing dot suffix in .cstruct construct");
2281 else if (*tok >= REG68_D0 && *tok <= REG68_A7)
2283 if (reglist(&rlist) < 0)
2286 for(i=0; i<16; i++, rlist>>=1)
2309 return error(".cstruct syntax");
2313 // Eat commas in between each argument, if they exist
2321 // Define start of OP object list (allows the use of ORG)
2325 if ((cursect != TEXT) && (cursect != DATA))
2327 error(".objproc can only be used in the TEXT or DATA segments");
2331 // If previous section was DSP or 68000 then we need to reset ORG'd
2339 robjproc = 1; // Set OP assembly
2340 rgpu = 0; // Unset GPU assembly
2341 rdsp = 0; // Unset DSP assembly
2342 dsp56001 = 0; // Unset 56001 assembly
2348 // Undefine a macro - .undefmac macname [, macname...]
2350 int undmac1(char * p)
2352 SYM * symbol = lookup(p, MACRO, 0);
2354 // If the macro symbol exists, cause it to disappear
2356 symbol->stype = (BYTE)SY_UNDEF;
2371 warn("JPAD directive is deprecated/non-functional");
2378 warn("NOJPAD directive is deprecated/non-functional");
2385 return error("What the hell? Do you think we adhere to the Goof standard?");
2390 // .opt - turn a specific (or all) optimisation on or off
2399 char * tmpstr = string[*tok++];
2401 if (ParseOptimization(tmpstr) != OK)
2402 return error("unknown optimization flag '%s'", tmpstr);
2405 return error(".opt directive needs every switch enclosed inside quotation marks");
2413 // .if, Start conditional assembly
2420 IFENT * rif = f_ifent;
2424 rif = (IFENT *)malloc(sizeof(IFENT));
2426 f_ifent = rif->if_prev;
2428 rif->if_prev = ifent;
2433 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
2436 if ((eattr & DEFINED) == 0)
2437 return error(undef_error);
2442 rif->if_state = (WORD)disabled;
2448 // .else, Do alternate case for .if
2452 IFENT * rif = ifent;
2454 if (rif->if_prev == NULL)
2455 return error("mismatched .else");
2458 disabled = rif->if_prev->if_state;
2462 rif->if_state = (WORD)disabled;
2468 // .endif, End of conditional assembly block
2469 // This is also called by fpop() to pop levels of IFENTs in case a macro or
2470 // include file exits early with `exitm' or `end'.
2474 IFENT * rif = ifent;
2476 if (rif->if_prev == NULL)
2477 return error("mismatched .endif");
2479 ifent = rif->if_prev;
2480 disabled = rif->if_prev->if_state;
2481 rif->if_prev = f_ifent;