2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // DIRECT.C - Directive Handling
4 // Copyright (C) 199x Landon Dyer, 2011-2020 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
73 int d_equrundef(void);
85 void SetLargestAlignment(int);
87 // Directive handler table
98 (void *)d_init, // 9 init
100 (void *)d_goto, // 11 goto
101 (void *)d_dc, // 12 dc
102 (void *)d_ds, // 13 ds
103 d_undmac, // 14 undefmac
106 (void *)d_dcb, // 17 dcb
110 d_incbin, // 21 .incbin //load
111 d_unimpl, // 22 disable
112 d_unimpl, // 23 enable
114 d_regbank0, // 25 .regbank0
115 d_regbank1, // 26 .regbank1
117 d_assert, // 28 assert
119 d_unimpl, // 30* endif
120 d_unimpl, // 31* endc
122 d_include, // 33 include
124 d_unimpl, // 35* macro
125 ExitMacro, // 36* exitm
126 d_unimpl, // 37* endm
130 d_phrase, // 41* endr
131 d_dphrase, // 42 struct
132 d_qphrase, // 43 ends
134 d_subttl, // 45 subttl
138 d_noclear, // 49 .noclear
139 d_equrundef, // 50 .equrundef/.regundef
140 d_ccundef, // 51 .ccundef
141 d_print, // 52 .print
142 d_cstruct, // 53 .cstruct
143 d_jpad, // 54 .jpad (deprecated)
144 d_nojpad, // 55 .nojpad (deprecated)
145 d_gpumain, // 56 .gpumain (deprecated)
146 d_prgflags, // 57 .prgflags
147 d_68020, // 58 .68020
148 d_68030, // 59 .68030
149 d_68040, // 60 .68040
150 d_68060, // 61 .68060
151 d_68881, // 62 .68881
152 d_68882, // 63 .68882
153 d_56001, // 64 .56001
156 d_objproc, // 67 .objproc
157 (void *)d_dsm, // 68 .dsm
162 // Set the largest alignment seen in the current section
164 void SetLargestAlignment(int size)
166 if ((scattr & TEXT) && (largestAlign[0] < size))
167 largestAlign[0] = size;
168 else if ((scattr & DATA) && (largestAlign[1] < size))
169 largestAlign[1] = size;
170 else if ((scattr & BSS) && (largestAlign[2] < size))
171 largestAlign[2] = size;
176 // .error - Abort compilation, printing an error message
178 int d_error(char *str)
181 return error("error directive encountered - aborting assembling");
187 return error(string[tok[1]]);
190 return error("error directive encountered--aborting assembly");
197 // .warn - Just display a warning on screen
199 int d_warn(char *str)
202 return warn("WARNING WARNING WARNING");
208 return warn(string[tok[1]]);
211 return warn("WARNING WARNING WARNING");
224 if (!rgpu && !rdsp && !robjproc && !m6502 && !dsp56001 && !(obj_format == RAW))
225 return error(".org permitted only in GPU/DSP/OP, 56001, 6502 and 68k (with -fr switch) sections");
227 // M56K can leave the expression off the org for some reason :-/
228 // (It's because the expression is non-standard, and so we have to look at
230 if (!dsp56001 && (abs_expr(&address) == ERROR))
232 error("cannot determine org'd address");
236 if (rgpu | rdsp | robjproc)
243 // 6502. We also kludge 'lsloc' so the listing generator doesn't try
244 // to spew out megabytes.
245 if (address > 0xFFFF)
246 return error(range_error);
248 if (sloc != currentorg[0])
250 currentorg[1] = sloc;
254 currentorg[0] = address;
256 lsloc = sloc = address;
257 chptr = scode->chptr + address;
263 // Only mark segments we actually wrote something
264 if (chptr != dsp_currentorg->start && dsp_written_data_in_current_org)
266 dsp_currentorg->end = chptr;
270 // Maybe we switched from a non-DSP section (TEXT, DATA, etc) and
271 // scode isn't initialised yet. Not that it's going to be a valid
272 // scenario, but if we try it anyhow it's going to lead to a crash. So
273 // let's fudge a value of 0 and get on with it.
274 orgaddr = (scode != NULL ? sloc : 0);
278 return error(syntax_error);
280 int sectionToSwitch = 0;
285 dsp_currentorg->memtype = ORG_X;
286 sectionToSwitch = M56001X;
290 dsp_currentorg->memtype = ORG_Y;
291 sectionToSwitch = M56001Y;
295 dsp_currentorg->memtype = ORG_P;
296 sectionToSwitch = M56001P;
300 dsp_currentorg->memtype = ORG_L;
301 sectionToSwitch = M56001L;
305 return error("unknown type in ORG");
308 if ((obj_format == LOD) || (obj_format == P56))
309 SwitchSection(sectionToSwitch);
312 chcheck(3); // Ensure we got a valid address to write
313 dsp_currentorg->chunk = scode; // Mark down which chunk this org starts from (will be needed when outputting)
317 // Well, the user didn't specify an address at all so we'll have to
318 // use the last used address of that section (or 0 if there wasn't one)
320 dsp_currentorg->start = chptr;
321 dsp_currentorg->orgadr = orgaddr;
325 if (abs_expr(&address) == ERROR)
327 error("cannot determine org'd address");
331 dsp_currentorg->start = chptr;
332 dsp_currentorg->orgadr = (uint32_t)address;
333 sect[cursect].orgaddr = (uint32_t)address;
336 if (address > DSP_MAX_RAM)
338 return error(range_error);
341 dsp_written_data_in_current_org = 0;
343 // Copied from 6502 above: kludge `lsloc' so the listing generator
344 // doesn't try to spew out megabytes.
345 lsloc = sloc = (int32_t)address;
346 // 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.
351 // If we get here we assume it's 68k with RAW output, so this is allowed
354 return error("In 68k mode only one .org statement is allowed");
357 org68k_address = address;
371 char prntstr[LNSIZ]; // String for PRINT directive
372 char format[LNSIZ]; // Format for PRINT directive
373 int formatting = 0; // Formatting on/off
374 int wordlong = 0; // WORD = 0, LONG = 1
375 int outtype = 0; // 0:hex, 1:decimal, 2:unsigned
377 uint64_t eval; // Expression value
378 WORD eattr; // Expression attributes
379 SYM * esym; // External symbol involved in expr.
380 TOKEN r_expr[EXPRSIZE];
387 sprintf(prntstr, "%s", string[tok[1]]);
388 printf("%s", prntstr);
391 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
398 // "X" & "L" get tokenized now... :-/ Probably should look into preventing this kind of thing from happening (was added with DSP56K code)
399 if ((tok[1] != SYMBOL) && (tok[1] != KW_L) && (tok[1] != KW_X))
407 else if (tok[1] == KW_X)
414 strcpy(prntstr, string[tok[2]]);
418 case 'l': case 'L': wordlong = 1; break;
419 case 'w': case 'W': wordlong = 0; break;
420 case 'x': case 'X': outtype = 0; break;
421 case 'd': case 'D': outtype = 1; break;
422 case 'u': case 'U': outtype = 2; break;
424 error("unknown print format flag");
436 if (expr(r_expr, &eval, &eattr, &esym) != OK)
442 case 0: strcpy(format, "%X"); break;
443 case 1: strcpy(format, "%d" ); break;
444 case 2: strcpy(format, "%u" ); break;
448 sprintf(prntstr, format, eval);
450 sprintf(prntstr, format, eval & 0xFFFF);
452 printf("%s", prntstr);
455 unused = write(list_fd, prntstr, (LONG)strlen(prntstr));
471 error("illegal print token [@ '%s']", prntstr);
477 // Undefine an equated condition code
483 // Check that we are in a RISC section
486 error(".ccundef must be defined in .gpu/.dsp section");
492 error("syntax error; expected symbol");
496 ccname = lookup(string[tok[1]], LABEL, 0);
498 // Make sure symbol is a valid ccdef
499 if (!ccname || !(ccname->sattre & EQUATEDCC))
501 error("invalid equated condition name specified");
505 ccname->sattre |= UNDEF_CC;
512 // Undefine an equated register
514 int d_equrundef(void)
518 // Check that we are in a RISC section
520 return error(".equrundef/.regundef must be defined in .gpu/.dsp section");
524 // Skip preceeding or seperating commas (if any)
528 // Check we are dealing with a symbol
530 return error("syntax error; expected symbol");
532 // Lookup and undef if equated register
533 regname = lookup(string[tok[1]], LABEL, 0);
535 if (regname && (regname->sattre & EQUATEDREG))
537 // Reset the attributes of this symbol...
539 regname->sattre &= ~(EQUATEDREG | BANK_0 | BANK_1);
540 regname->sattre |= UNDEF_EQUR;
543 // Skip over symbol token and address
552 // Do not allow use of the CLR.L opcode
556 warn("CLR.L opcode ignored...");
562 // Include binary file (can add addition size & position params, comma separated)
568 uint64_t pos, size, bytesRead;
572 // Check to see if we're in BSS, and, if so, throw an error
575 error("cannot include binary file \"%s\" in BSS section", string[tok[1]]);
581 error("syntax error; file to include missing");
585 // Attempt to open the include file in the current directory, then (if that
586 // failed) try list of include files passed in the enviroment string or by
588 TOKEN filename = tok[1];
590 if ((fd = open(string[filename], _OPEN_INC)) < 0)
592 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
596 // Append path char if necessary
597 if (fd > 0 && buf1[fd - 1] != SLASHCHAR)
598 strcat(buf1, SLASHSTRING);
600 strcat(buf1, string[filename]);
602 if ((fd = open(buf1, _OPEN_INC)) >= 0)
606 return error("cannot open: \"%s\"", string[filename]);
615 // Check size parameter (can be omitted)
620 if (abs_expr(&size) != OK)
627 size = lseek(fd, 0L, SEEK_END);
630 // Check offset parameter (can be omitted)
637 if (abs_expr(&pos) != OK)
643 lseek(fd, pos, SEEK_SET);
648 // offset parameter omitted, so it's 0
649 pos = lseek(fd, 0L, SEEK_SET);
653 return error(comma_error);
656 pos = lseek(fd, 0L, SEEK_SET);
660 // size & pos not given, so assume offset of 0 and all of the binary
661 size = lseek(fd, 0L, SEEK_END);
662 pos = lseek(fd, 0L, SEEK_SET);
667 DEBUG { printf("INCBIN: File '%s' is %li bytes.\n", string[filename], size); }
669 char * fileBuffer = (char *)malloc(size);
670 bytesRead = read(fd, fileBuffer, size);
672 if (bytesRead != size)
674 error("was only able to read %li bytes from binary file (%s, %li bytes)", bytesRead, string[filename], size);
678 memcpy(chptr, fileBuffer, size);
693 // Set RISC register banks
697 // Set active register bank zero
705 // Set active register bank one
712 // Helper function, to cut down on mistakes & typing
714 static inline void SkipBytes(unsigned bytesToSkip)
719 if ((scattr & SBSS) == 0)
721 chcheck(bytesToSkip);
722 D_ZEROFILL(bytesToSkip);
729 orgaddr += bytesToSkip;
735 // Adjust location to an EVEN value
740 return error(in_6502mode);
742 unsigned skip = (rgpu || rdsp ? orgaddr : sloc) & 0x01;
746 if ((scattr & SBSS) == 0)
765 // Adjust location to a LONG value
769 unsigned lower2Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x03;
770 unsigned bytesToSkip = (0x04 - lower2Bits) & 0x03;
771 SkipBytes(bytesToSkip);
772 SetLargestAlignment(4);
779 // Adjust location to a PHRASE value
781 // N.B.: We have to handle the GPU/DSP cases separately because you can embed
782 // RISC code in the middle of a regular 68K section. Also note that all
783 // of the alignment pseudo-ops will have to be fixed this way.
785 // This *must* behave differently when in a RISC section, as following sloc
786 // (instead of orgaddr) will fuck things up royally. Note that we do it this
787 // way because you can embed RISC code in a 68K section, and have the origin
788 // pointing to a different alignment in the RISC section than the 68K section.
792 unsigned lower3Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x07;
793 unsigned bytesToSkip = (0x08 - lower3Bits) & 0x07;
794 SkipBytes(bytesToSkip);
795 SetLargestAlignment(8);
802 // Adjust location to a DPHRASE value
806 unsigned lower4Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x0F;
807 unsigned bytesToSkip = (0x10 - lower4Bits) & 0x0F;
808 SkipBytes(bytesToSkip);
809 SetLargestAlignment(16);
816 // Adjust location to a QPHRASE value
820 unsigned lower5Bits = (rgpu || rdsp ? orgaddr : sloc) & 0x1F;
821 unsigned bytesToSkip = (0x20 - lower5Bits) & 0x1F;
822 SkipBytes(bytesToSkip);
823 SetLargestAlignment(32);
830 // Do auto-even. This must be called ONLY if 'sloc' is odd.
832 // This is made hairy because, if there was a label on the line, we also have
833 // to adjust its value. This won't work with more than one label on the line,
834 // which is OK since multiple labels are only allowed in AS68 kludge mode, and
835 // the C compiler is VERY paranoid and uses ".even" whenever it can
837 // N.B.: This probably needs the same fixes as above...
841 if (cursect != M6502)
844 sloc++; // Bump BSS section
846 D_byte(0); // Deposit 0.b in non-BSS
848 if (lab_sym != NULL) // Bump label if we have to
855 // Unimplemened directive error
859 return error("unimplemented directive");
864 // Return absolute (not TDB) and defined expression or return an error
866 int abs_expr(uint64_t * a_eval)
870 if (expr(exprbuf, a_eval, &eattr, NULL) < 0)
873 if (!(eattr & DEFINED))
874 return error(undef_error);
877 return error(rel_error);
884 // Hand symbols in a symbol-list to a function (kind of like mapcar...)
886 int symlist(int(* func)())
888 const char * em = "symbol list syntax";
895 if ((*func)(string[tok[1]]) != OK)
914 // .include "filename"
924 if (*tok == STRING) // Leave strings ALONE
926 else if (*tok == SYMBOL) // Try to append ".s" to symbols
928 strcpy(buf, string[*++tok]);
932 else // Punt if no STRING or SYMBOL
933 return error("missing filename");
935 // Make sure the user didn't try anything like:
936 // .include equates.s
938 return error("extra stuff after filename--enclose it in quotes");
940 // Attempt to open the include file in the current directory, then (if that
941 // failed) try list of include files passed in the enviroment string or by
943 if ((j = open(fn, 0)) < 0)
945 for(i=0; nthpath("RMACPATH", i, buf1)!=0; i++)
949 // Append path char if necessary
950 if (j > 0 && buf1[j - 1] != SLASHCHAR)
951 strcat(buf1, SLASHSTRING);
955 if ((j = open(buf1, 0)) >= 0)
959 return error("cannot open: \"%s\"", fn);
969 // .assert expression [, expression...]
976 for(; expr(exprbuf, &eval, &eattr, NULL)==OK; ++tok)
978 if (!(eattr & DEFINED))
979 return error("forward or undefined .assert");
982 return error("assert failure");
994 // .globl symbol [, symbol] <<<cannot make local symbols global>>>
1001 return error("cannot .globl local symbol");
1003 if ((sy = lookup(p, LABEL, 0)) == NULL)
1005 sy = NewSymbol(p, LABEL, 0);
1008 //printf("glob1: Making global symbol: attr=%04X, eattr=%08X, %s\n", sy->sattr, sy->sattre, sy->sname);
1011 sy->sattr |= GLOBAL;
1020 return error(in_6502mode);
1028 // .prgflags expression
1030 int d_prgflags(void)
1035 return error("PRGFLAGS requires value");
1036 else if (abs_expr(&eval) == OK)
1038 PRGFLAGS = (uint32_t)eval;
1043 return error("PRGFLAGS requires value");
1049 // .abs [expression]
1056 return error(in_6502mode);
1062 else if (abs_expr(&eval) != OK)
1066 sloc = (uint32_t)eval;
1077 return error("directive forbidden in gpu/dsp mode");
1079 return error(in_6502mode);
1081 if (cursect != TEXT)
1084 SwitchSection(TEXT);
1094 return error("directive forbidden in gpu/dsp mode");
1096 return error(in_6502mode);
1098 if (cursect != DATA)
1101 SwitchSection(DATA);
1111 return error("directive forbidden in gpu/dsp mode");
1113 return error(in_6502mode);
1126 // .ds[.size] expression
1130 DEBUG { printf("Directive: .ds.[size] = %u, sloc = $%X\n", siz, sloc); }
1134 if ((cursect & (M6502 | M56KPXYL)) == 0)
1136 if ((siz != SIZB) && (sloc & 1)) // Automatic .even
1140 if (abs_expr(&eval) != OK)
1143 // Check to see if the value being passed in is negative (who the hell does
1144 // that?--nobody does; it's the code gremlins, or rum, what does it)
1145 // N.B.: Since 'eval' is of type uint64_t, if it goes negative, it will
1146 // have its high bit set.
1147 if (eval & 0x8000000000000000)
1148 return error("negative sizes not allowed in DS");
1150 // In non-TDB section (BSS, ABS and M6502) just advance the location
1151 // counter appropriately. In TDB sections, deposit (possibly large) chunks
1152 // of zeroed memory....
1153 if ((scattr & SBSS) || cursect == M6502)
1155 listvalue((uint32_t)eval);
1157 sloc += (uint32_t)eval;
1159 if (cursect == M6502)
1162 just_bss = 1; // No data deposited (8-bit CPU mode)
1164 else if (cursect & M56KPXYL)
1166 // Change segment instead of marking blanks.
1167 // Only mark segments we actually wrote something
1168 if (chptr != dsp_currentorg->start && dsp_written_data_in_current_org)
1170 dsp_currentorg->end = chptr;
1172 dsp_currentorg->memtype = dsp_currentorg[-1].memtype;
1175 listvalue((uint32_t)eval);
1176 sloc += (uint32_t)eval;
1178 // And now let's create a new segment
1179 dsp_currentorg->start = chptr;
1180 dsp_currentorg->chunk = scode; // Mark down which chunk this org starts from (will be needed when outputting)
1181 sect[cursect].orgaddr = sloc;
1182 dsp_currentorg->orgadr = sloc;
1183 dsp_written_data_in_current_org = 0;
1185 just_bss = 1; // No data deposited
1189 dep_block(eval, siz, 0, (DEFINED | ABS), NULL);
1198 // dsm[.siz] expression
1199 // Define modulo storage
1200 // Quoting the Motorola assembler manual:
1201 // "The DSM directive reserves a block of memory the length of which in words is equal to
1202 // the value of <expression>.If the runtime location counter is not zero, this directive first
1203 // advances the runtime location counter to a base address that is a multiple of 2k, where
1204 // 2k >= <expression>."
1205 // The kicker of course is written a few sentences after:
1206 // "<label>, if present, will be assigned the value of the runtime location counter after a valid
1207 // base address has been established."
1211 TOKEN * tok_current = tok; // Keep track of where tok was when we entered this procedure
1214 if (abs_expr(&eval) != OK)
1217 // Round up to the next highest power of 2
1218 // Nicked from https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
1227 units_to_skip = eval + 1 - sloc;
1228 sloc += units_to_skip; // Bump up sloc - TODO: check if this goes over the RAM limits?
1230 // If a label has been defined in the same line as dsm, its value also needs to be adjusted
1233 SYM * label = lookup(label_defined, LABEL, 0);
1234 label->svalue += units_to_skip;
1237 tok = tok_current; // Rewind tok back to where it was
1238 return d_ds(siz); // And let d_ds take over from here
1243 // dc.b, dc.w / dc, dc.l, dc.i, dc.q, dc.d, dc.s, dc.x
1251 if ((scattr & SBSS) != 0)
1252 return error("illegal initialization of section");
1254 // Do an auto_even if it's not BYTE sized (hmm, should we be doing this???)
1255 if ((cursect != M6502) && (cursect != M56001P) && (cursect != M56001X)
1256 && (cursect != M56001Y) && (cursect != M56001L)
1257 && (siz != SIZB) && (sloc & 1))
1260 // Check to see if we're trying to set LONGS on a non 32-bit aligned
1261 // address in a GPU or DSP section, in their local RAM
1262 if ((siz == SIZL) && (orgaddr & 0x03)
1263 && ((rgpu && (orgaddr >= 0xF03000) && (orgaddr <= 0xF03FFFF))
1264 || (rdsp && (orgaddr >= 0xF1B000) && (orgaddr <= 0xF1CFFFF))))
1265 warn("depositing LONGs on a non-long address in local RAM");
1269 // dc.b 'string' [,] ...
1270 if (siz == SIZB && (*tok == STRING || *tok == STRINGA8) && (tok[2] == ',' || tok[2] == EOL))
1272 uint32_t i = strlen(string[tok[1]]);
1274 if ((challoc - ch_size) < i)
1279 for(p=string[tok[1]]; *p!=EOS; p++)
1282 else if (*tok == STRINGA8)
1284 for(p=string[tok[1]]; *p!=EOS; p++)
1285 D_byte(strtoa8[*p]);
1289 error("String format not supported... yet");
1296 int movei = 0; // MOVEI flag for dc.i
1305 // dc.x <expression>
1308 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
1311 uint16_t tdb = eattr & TDB;
1312 uint16_t defined = eattr & DEFINED;
1314 // 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 !!!
1317 if (cursect != M56001L)
1321 AddFixup(FU_DSPIMM24 | FU_SEXT, sloc, exprbuf);
1328 double fval = *(double *)&eval;
1329 eval = DoubleToDSPFloat(fval);
1333 if ((uint32_t)eval + 0x1000000 >= 0x2000000)
1334 return error(range_error);
1337 // Deposit DSP word (24-bit)
1343 // In L: we deposit stuff to both X: and Y: instead
1344 // We will be a bit lazy and require that there is a 2nd value
1345 // in the same source line. (Motorola's assembler can parse
1346 // 12-digit hex values, which we can't do at the moment) This
1347 // of course requires to parse 2 values in one pass. If there
1348 // isn't another value in this line, assume X: value is 0.
1355 AddFixup(FU_DSPIMM24 | FU_SEXT, sloc, exprbuf);
1362 float fval = *(float *)&eval;
1363 eval = DoubleToDSPFloat(fval);
1367 if (eval + 0x1000000 >= 0x2000000)
1368 return error(range_error);
1371 // Parse 2nd value if we didn't do this yet
1372 if (secondword == 0)
1374 evaly = (uint32_t)eval;
1379 // If we don't have a : then we're probably at EOL,
1380 // which means the X: value will be 0
1386 tok++; // Eat the comma;
1388 if (expr(exprbuf, &eval, &eattr, NULL) != OK)
1391 defined = (WORD)(eattr & DEFINED);
1396 // Deposit DSP words (24-bit)
1399 sloc--; // We do write 2 DSP words but as far as L: space is concerned we actually advance our counter by one
1412 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1418 return error("non-absolute byte value");
1420 if (eval + 0x100 >= 0x200)
1421 return error("%s (value = $%X)", range_error, eval);
1432 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1437 if (eval + 0x10000 >= 0x20000)
1438 return error(range_error);
1441 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1443 // Deposit 68000 or 6502 (byte-reversed) word
1444 if (cursect != M6502)
1453 // Shamus: Why can't we do longs in 6502 mode?
1455 return error(in_6502mode);
1459 AddFixup(FU_LONG | (movei ? FU_MOVEI : 0), sloc, exprbuf);
1465 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1468 eval = WORDSWAP32(eval);
1478 return error(in_6502mode);
1480 // DEFINITELY NEED FIXUPS HERE!
1483 AddFixup(FU_QUAD, sloc, exprbuf);
1491 // 32-bit float size
1493 return error(in_6502mode);
1495 /* Seems to me that if something is undefined here, then that should be an error. Likewise for the D & X variants. */
1498 // AddFixup(FU_FLOATSING, sloc, exprbuf);
1500 return error("labels not allowed in floating point expressions");
1504 //Would this *ever* happen?
1506 // MarkRelocatable(cursect, sloc, tdb, MSINGLE, NULL);
1510 uint32_t ieee754 = FloatToIEEE754((float)*ptr.dp);
1517 // 64-bit double size
1519 return error(in_6502mode);
1523 // AddFixup(FU_FLOATDOUB, sloc, exprbuf);
1525 return error("labels not allowed in floating point expressions");
1529 //Would this *ever* happen?
1531 // MarkRelocatable(cursect, sloc, tdb, MDOUBLE, NULL);
1535 uint64_t ieee754 = DoubleToIEEE754(*ptr.dp);
1543 return error(in_6502mode);
1546 memset(extDbl, 0, 12);
1550 // AddFixup(FU_FLOATEXT, sloc, exprbuf);
1551 // D_extend(extDbl);
1552 return error("labels not allowed in floating point expressions");
1556 //Would this *ever* happen?
1558 // MarkRelocatable(cursect, sloc, tdb, MEXTEND, NULL);
1562 DoubleToExtended(*ptr.dp, extDbl);
1580 // dcb[.siz] expr1,expr2 - Make 'expr1' copies of 'expr2'
1584 uint64_t evalc, eval;
1587 DEBUG { printf("dcb: section is %s%s%s (scattr=$%X)\n", (cursect & TEXT ? "TEXT" : ""), (cursect & DATA ? " DATA" : ""), (cursect & BSS ? "BSS" : ""), scattr); }
1589 if ((scattr & SBSS) != 0)
1590 return error("illegal initialization of section");
1592 if (abs_expr(&evalc) != OK)
1596 return error("missing comma");
1598 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1601 if (cursect != M6502 && (siz != SIZB) && (sloc & 1))
1604 dep_block((uint32_t)evalc, siz, (uint32_t)eval, eattr, exprbuf);
1610 // Generalized initialization directive
1612 // .init[.siz] [#count,] expression [.size] , ...
1614 // The size suffix on the ".init" directive becomes the default size of the
1615 // objects to deposit. If an item is preceeded with a sharp (immediate) sign
1616 // and an expression, it specifies a repeat count. The value to be deposited
1617 // may be followed by a size suffix, which overrides the default size.
1619 int d_init(WORD def_siz)
1626 if ((scattr & SBSS) != 0)
1627 return error(".init not permitted in BSS or ABS");
1630 return error("directive forbidden in gpu/dsp mode");
1634 // Get repeat count (defaults to 1)
1639 if (abs_expr(&count) != OK)
1643 return error(comma_error);
1648 // Evaluate expression to deposit
1649 if (expr(exprbuf, &eval, &eattr, NULL) < 0)
1653 { // Determine size of object to deposit
1654 case DOTB: siz = SIZB; break;
1655 case DOTW: siz = SIZB; break;
1656 case DOTL: siz = SIZL; break;
1663 dep_block((uint32_t)count, siz, (uint32_t)eval, eattr, exprbuf);
1673 return error(comma_error);
1680 // Deposit 'count' values of size 'siz' in the current (non-BSS) segment
1682 int dep_block(uint32_t count, WORD siz, uint32_t eval, WORD eattr, TOKEN * exprbuf)
1684 WORD tdb = eattr & TDB;
1685 WORD defined = eattr & DEFINED;
1689 if ((challoc - ch_size) < 4)
1697 AddFixup(FU_BYTE | FU_SEXT, sloc, exprbuf);
1703 return error("non-absolute byte value");
1705 if (eval + 0x100 >= 0x200)
1706 return error(range_error);
1716 AddFixup(FU_WORD | FU_SEXT, sloc, exprbuf);
1722 MarkRelocatable(cursect, sloc, tdb, MWORD, NULL);
1724 if (eval + 0x10000 >= 0x20000)
1725 return error(range_error);
1727 // Deposit 68000 or 6502 (byte-reversed) word
1728 if (cursect != M6502)
1738 return error(in_6502mode);
1742 AddFixup(FU_LONG, sloc, exprbuf);
1748 MarkRelocatable(cursect, sloc, tdb, MLONG, NULL);
1762 // .comm symbol, size
1771 return error(in_6502mode);
1774 return error("missing symbol");
1779 if (*p == '.') // Cannot .comm a local symbol
1780 return error(locgl_error);
1782 if ((sym = lookup(p, LABEL, 0)) == NULL)
1783 sym = NewSymbol(p, LABEL, 0);
1786 if (sym->sattr & DEFINED)
1787 return error(".comm symbol already defined");
1790 sym->sattr = GLOBAL | COMMON | BSS;
1793 return error(comma_error);
1795 if (abs_expr(&eval) != OK) // Parse size of common region
1798 sym->svalue = eval; // Install common symbol's size
1805 // .list - Turn listing on
1817 // .nlist - Turn listing off
1829 // .68000 - Back to 68000 TEXT segment
1833 rgpu = rdsp = robjproc = dsp56001 = 0;
1834 // Switching from gpu/dsp sections should reset any ORG'd Address
1838 SwitchSection(TEXT);
1839 activecpu = CPU_68000;
1845 // .68020 - Back to 68000 TEXT segment and select 68020
1850 activecpu = CPU_68020;
1856 // .68030 - Back to 68000 TEXT segment and select 68030
1861 activecpu = CPU_68030;
1867 // .68040 - Back to 68000 TEXT segment and select 68040
1872 activecpu = CPU_68040;
1873 activefpu = FPU_68040;
1879 // .68060 - Back to 68000 TEXT segment and select 68060
1884 activecpu = CPU_68060;
1885 activefpu = FPU_68060;
1891 // .68881 - Back to 680x0 TEXT segment and select 68881 FPU
1896 activefpu = FPU_68881;
1902 // .68882 - Back to 680x0 TEXT segment and select 68882 FPU
1907 activefpu = FPU_68882;
1913 // nofpu - Deselect FPUs.
1917 activefpu = FPU_NONE;
1923 // .56001 - Switch to DSP56001 assembler
1928 rgpu = rdsp = robjproc = 0;
1931 if ((obj_format == LOD) || (obj_format == P56))
1932 SwitchSection(M56001P);
1939 // .gpu - Switch to GPU assembler
1943 if ((cursect != TEXT) && (cursect != DATA))
1945 error(".gpu can only be used in the TEXT or DATA segments");
1949 // If previous section was DSP or 68000 then we need to reset ORG'd Addresses
1956 rgpu = 1; // Set GPU assembly
1957 rdsp = 0; // Unset DSP assembly
1958 robjproc = 0; // Unset OP assembly
1959 dsp56001 = 0; // Unset 56001 assembly
1960 regbank = BANK_N; // Set no default register bank
1966 // .dsp - Switch to DSP assembler
1970 if ((cursect != TEXT) && (cursect != DATA))
1972 error(".dsp can only be used in the TEXT or DATA segments");
1976 // If previous section was gpu or 68000 then we need to reset ORG'd Addresses
1983 rdsp = 1; // Set DSP assembly
1984 rgpu = 0; // Unset GPU assembly
1985 robjproc = 0; // Unset OP assembly
1986 dsp56001 = 0; // Unset 56001 assembly
1987 regbank = BANK_N; // Set no default register bank
1993 // .cargs [#offset], symbol[.size], ...
1995 // Lists of registers may also be mentioned; they just take up space. Good for
1996 // "documentation" purposes:
1998 // .cargs a6, .arg1, .arg2, .arg3...
2000 // Symbols thus created are ABS and EQUATED.
2004 uint64_t eval = 4; // Default to 4 if no offset specified (to account for
2013 return error("directive forbidden in gpu/dsp mode");
2019 if (abs_expr(&eval) != OK)
2022 // Eat the comma, if it's there
2033 // Set env to either local (dot prefixed) or global scope
2034 env = (*p == '.' ? curenv : 0);
2035 symbol = lookup(p, LABEL, env);
2039 symbol = NewSymbol(p, LABEL, env);
2042 else if (symbol->sattr & DEFINED)
2043 return error("multiply-defined label '%s'", p);
2045 // Put symbol in "order of definition" list
2046 AddToSymbolDeclarationList(symbol);
2048 symbol->sattr |= (ABS | DEFINED | EQUATED);
2049 symbol->svalue = eval;
2052 // What this does is eat any dot suffixes attached to a symbol. If
2053 // it's a .L, it adds 4 to eval; if it's .W or .B, it adds 2. If
2054 // there is no dot suffix, it assumes a size of 2.
2066 else if (*tok >= KW_D0 && *tok <= KW_A7)
2068 if (reglist(&rlist) < 0)
2071 for(i=0; i<16; i++, rlist>>=1)
2094 return error(".cargs syntax");
2098 // Eat commas in between each argument, if they exist
2106 // .cstruct [#offset], symbol[.size], ...
2108 // Lists of registers may also be mentioned; they just take up space. Good for
2109 // "documentation" purposes:
2111 // .cstruct a6, .arg1, .arg2, .arg3...
2113 // Symbols thus created are ABS and EQUATED. Note that this is for
2114 // compatibility with VBCC and the Remover's library. Thanks to GroovyBee for
2119 uint64_t eval = 0; // Default, if no offset specified, is zero
2127 return error("directive forbidden in gpu/dsp mode");
2133 if (abs_expr(&eval) != OK)
2136 // Eat the comma, if it's there
2145 symbolName = string[tok[1]];
2147 // Set env to either local (dot prefixed) or global scope
2148 env = (symbolName[0] == '.' ? curenv : 0);
2149 symbol = lookup(symbolName, LABEL, env);
2151 // If the symbol wasn't found, then define it. Otherwise, throw an
2155 symbol = NewSymbol(symbolName, LABEL, env);
2158 else if (symbol->sattr & DEFINED)
2159 return error("multiply-defined label '%s'", symbolName);
2161 // Put symbol in "order of definition" list
2162 AddToSymbolDeclarationList(symbol);
2166 // Adjust label start address if it's a word or a long, as a byte
2167 // label might have left us on an odd address.
2172 eval += eval & 0x01;
2175 symbol->sattr |= (ABS | DEFINED | EQUATED);
2176 symbol->svalue = eval;
2178 // Check for dot suffixes and adjust space accordingly (longs and
2179 // words on an odd boundary get bumped to the next word aligned
2180 // address). If no suffix, then throw an error.
2193 return error("Symbol missing dot suffix in .cstruct construct");
2198 else if (*tok >= KW_D0 && *tok <= KW_A7)
2200 if (reglist(&rlist) < 0)
2203 for(i=0; i<16; i++, rlist>>=1)
2226 return error(".cstruct syntax");
2230 // Eat commas in between each argument, if they exist
2238 // Define start of OP object list (allows the use of ORG)
2242 if ((cursect != TEXT) && (cursect != DATA))
2244 error(".objproc can only be used in the TEXT or DATA segments");
2248 // If previous section was DSP or 68000 then we need to reset ORG'd
2256 robjproc = 1; // Set OP assembly
2257 rgpu = 0; // Unset GPU assembly
2258 rdsp = 0; // Unset DSP assembly
2259 dsp56001 = 0; // Unset 56001 assembly
2265 // Undefine a macro - .undefmac macname [, macname...]
2267 int undmac1(char * p)
2269 SYM * symbol = lookup(p, MACRO, 0);
2271 // If the macro symbol exists, cause it to disappear
2273 symbol->stype = (BYTE)SY_UNDEF;
2288 warn("JPAD directive is deprecated/non-functional");
2295 warn("NOJPAD directive is deprecated/non-functional");
2302 return error("What the hell? Do you think we adhere to the Goof standard?");
2307 // .opt - turn a specific (or all) optimisation on or off
2316 char * tmpstr = string[*tok++];
2318 if (ParseOptimization(tmpstr) != OK)
2319 return error("unknown optimization flag '%s'", tmpstr);
2322 return error(".opt directive needs every switch enclosed inside quotation marks");
2330 // .if, Start conditional assembly
2337 IFENT * rif = f_ifent;
2341 rif = (IFENT *)malloc(sizeof(IFENT));
2343 f_ifent = rif->if_prev;
2345 rif->if_prev = ifent;
2350 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
2353 if ((eattr & DEFINED) == 0)
2354 return error(undef_error);
2359 rif->if_state = (WORD)disabled;
2365 // .else, Do alternate case for .if
2369 IFENT * rif = ifent;
2371 if (rif->if_prev == NULL)
2372 return error("mismatched .else");
2375 disabled = rif->if_prev->if_state;
2379 rif->if_state = (WORD)disabled;
2385 // .endif, End of conditional assembly block
2386 // This is also called by fpop() to pop levels of IFENTs in case a macro or
2387 // include file exits early with `exitm' or `end'.
2391 IFENT * rif = ifent;
2393 if (rif->if_prev == NULL)
2394 return error("mismatched .endif");
2396 ifent = rif->if_prev;
2397 disabled = rif->if_prev->if_state;
2398 rif->if_prev = f_ifent;