2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // SECT.C - Code Generation, Fixups and Section Management
4 // Copyright (C) 199x Landon Dyer, 2011 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source Utilised with the Kind Permission of Landon Dyer
19 // Section descriptors
20 SECT sect[NSECTS]; // All sections...
21 int cursect; // Current section number
23 // These are copied from the section descriptor, the current code chunk descriptor and the current
24 // fixup chunk descriptor when a switch is made into a section. They are copied back to the
25 // descriptors when the section is left.
26 WORD scattr; // Section attributes
27 LONG sloc; // Current loc in section
29 CHUNK *scode; // Current (last) code chunk
30 LONG challoc; // #bytes alloc'd to code chunk
31 LONG ch_size; // #bytes used in code chunk
32 char *chptr; // Deposit point in code chunk buffer
34 CHUNK *sfix; // Current (last) fixup chunk
35 LONG fchalloc; // #bytes alloc'd to fixup chunk
36 LONG fchsize; // #bytes used in fixup chunk
37 PTR fchptr; // Deposit point in fixup chunk buffer
39 unsigned fwdjump[MAXFWDJUMPS]; // forward jump check table
40 unsigned fwindex = 0; // forward jump index
42 // Return a size (SIZB, SIZW, SIZL) or 0, depending on what kind of fixup is associated
44 static char fusiztab[] = {
55 // Offset to REAL fixup location
56 static char fusizoffs[] = {
68 // --- Make a New (Clean) Section ------------------------------------------------------------------
71 void mksect(int sno, WORD attr) {
72 SECT *p; // Section pointer
77 p->scode = p->sfcode = NULL;
78 p->sfix = p->sffix = NULL;
82 // --- Switch to Another Section (Copy Section & Chunk Descriptors to Global Vars for Fast Access) -
85 void switchsect(int sno) {
86 SECT *p; // Section pointer
87 CHUNK *cp; // Chunk pointer
92 scattr = p->scattr; // Copy section vars
97 if((cp = scode) != NULL) { // Copy code chunk vars
98 challoc = cp->challoc;
99 ch_size = cp->ch_size;
100 chptr = cp->chptr + ch_size;
101 } else challoc = ch_size = 0;
103 if((cp = sfix) != NULL) { // Copy fixup chunk vars
104 fchalloc = cp->challoc;
105 fchsize = cp->ch_size;
106 fchptr.cp = cp->chptr + fchsize;
107 } else fchalloc = fchsize = 0;
111 // --- Save Current Section ------------------------------------------------------------------------
119 p->scattr = scattr; // Bailout section vars
122 if(scode != NULL) // Bailout code chunk
123 scode->ch_size = ch_size;
125 if(sfix != NULL) // Bailout fixup chunk
126 sfix->ch_size = fchsize;
130 // --- Initialize Sections; Setup initial ABS, TEXT, DATA and BSS sections -------------------------
133 void init_sect(void) {
136 // Cleanup all sections
137 for(i = 0; i < NSECTS; ++i)
140 // Construct default sections, make TEXT the current section
141 mksect(ABS, SUSED|SABS|SBSS); // ABS
142 mksect(TEXT, SUSED|TEXT ); // TEXT
143 mksect(DATA, SUSED|DATA ); // DATA
144 mksect(BSS, SUSED|BSS |SBSS); // BSS
145 // mksect(M6502, SUSED|TEXT ); // 6502 code section
147 switchsect(TEXT); // Switch to TEXT for starters
151 // -------------------------------------------------------------------------------------------------
152 // Test to see if a location has a fixup sic'd on it. This is used by the listing
153 // generator to print 'xx's instead of '00's for forward references
154 // -------------------------------------------------------------------------------------------------
157 int fixtest(int sno, LONG loc) {
164 stopmark(); // Force update to sect[] variables
166 // Hairy, ugly linear search for a mark on our location;
167 // the speed doesn't matter, since this is only done when generating a listing, which is SLOW.
168 for(ch = sect[sno].sffix; ch != NULL; ch = ch->chnext) {
169 fup.cp = (char *)ch->chptr;
170 fuend = fup.cp + ch->ch_size;
172 while(fup.cp < fuend) {
174 xloc = *fup.lp++ + (int)fusizoffs[w & FUMASK];
178 return((int)fusiztab[w & FUMASK]);
191 // -------------------------------------------------------------------------------------------------
192 // Check that there are at least `amt' bytes left in the current chunk. If there are not,
193 // allocate another chunk of at least `amt' bytes (and probably more).
195 // If `amt' is zero, ensure there are at least CH_THRESHOLD bytes, likewise.
196 // -------------------------------------------------------------------------------------------------
199 int chcheck(LONG amt) {
203 if(scattr & SBSS) return(0); // If in BSS section, forget it
205 if(!amt) amt = CH_THRESHOLD;
207 if((int)(challoc - ch_size) >= (int)amt)
210 if(amt < CH_CODE_SIZE) amt = CH_CODE_SIZE;
212 cp = (CHUNK *)amem((long)(sizeof(CHUNK) + amt));
213 if(scode == NULL) { // First chunk in section
216 } else { // Add chunk to other chunks
219 scode->ch_size = ch_size; // Save old chunk's globals
222 // Setup chunk and global vars
225 challoc = cp->challoc = amt;
226 ch_size = cp->ch_size = 0;
227 chptr = cp->chptr = ((char *)cp) + sizeof(CHUNK);
228 scode = p->scode = cp;
234 // --- Arrange for a fixup on a location -----------------------------------------------------------
237 int fixup(WORD attr, LONG loc, TOKEN *fexpr) {
243 // Compute length of expression (could be faster); determine if it's the single-symbol case;
244 // no expression if it's just a mark. This code assumes 16 bit WORDs and 32 bit LONGs
245 if(*fexpr == SYMBOL && fexpr[2] == ENDEXPR)
246 //if((attr & 0x0F00) == FU_JR) {
247 if((attr & 0x0200) == FU_JR) {
248 i = 18; // Just a single symbol
254 for(len = 0; fexpr[len] != ENDEXPR; ++len)
255 if(fexpr[len] == CONST || fexpr[len] == SYMBOL)
257 ++len; // Add 1 for ENDEXPR
261 // Maybe alloc another fixup chunk for this one to fit in
262 if((fchalloc - fchsize) < i) {
264 cp = (CHUNK *)amem((long)(sizeof(CHUNK) + CH_FIXUP_SIZE));
265 if(sfix == NULL) { // First fixup chunk in section
268 } else { // Add to other chunks
271 sfix->ch_size = fchsize;
274 // Setup fixup chunk and its global vars
276 fchalloc = cp->challoc = CH_FIXUP_SIZE;
277 fchsize = cp->ch_size = 0;
278 fchptr.cp = cp->chptr = ((char *)cp) + sizeof(CHUNK);
282 // Record fixup type, fixup location, and the file number and line number the fixup is
286 *fchptr.wp++ = cfileno;
287 *fchptr.wp++ = (WORD)curlineno;
288 // Store postfix expression or pointer to a single symbol, or nothing for a mark.
290 *fchptr.wp++ = (WORD)len;
292 *fchptr.lp++ = (LONG)*fexpr++;
294 *fchptr.lp++ = (LONG)fexpr[1];
297 //if((attr & 0x0F00) == FU_JR) {
298 if((attr & 0x0200) == FU_JR) {
299 if(orgactive) *fchptr.lp++ = orgaddr;
300 else *fchptr.lp++ = 0x00000000;
309 // --- Resolve all Fixups --------------------------------------------------------------------------
313 unsigned i; // Iterator
316 if(glob_flag) // Make undefined symbols GLOBL
321 // We need to do a final check of forward 'jump' destination addresses that are external
322 for(i = 0; i < MAXFWDJUMPS; i++) {
325 sprintf(buf, "* \'jump\' at $%08X - destination address is external to this source file and "
326 "cannot have its aligment validated", fwdjump[i]);
327 if(listing > 0) ship_ln(buf);
328 if(err_flag) write(err_fd, buf, (LONG)strlen(buf));
329 else printf("%s\n", buf);
337 // --- Resolve Fixups in a Section -----------------------------------------------------------------
340 int resfix(int sno) {
343 PTR fup; // Current fixup
344 WORD *fuend; // End of last fixup (in this chunk)
345 CHUNK *cch; // Cached chunk for target
346 WORD w; // Fixup word (type+modes+flags)
347 char *locp; // Location to fix (in cached chunk)
348 LONG loc; // Location to fixup
349 VALUE eval; // Expression value
350 WORD eattr; // Expression attrib
351 SYM *esym; // External symbol involved in expr
352 SYM *sy; // (Temp) pointer to a symbol
353 WORD i; // (Temp) word
354 WORD tdb; // eattr & TDB
358 unsigned page_jump = 0;
359 unsigned address = 0;
360 unsigned j; // iterator
369 cch = sc->sfcode; // "cache" first chunk
370 if(cch == NULL) // Can't fixup a sect with nothing in it
374 fup.cp = ch->chptr; // fup -> start of chunk
375 fuend = (WORD *)(fup.cp + ch->ch_size); // fuend -> end of chunk
376 while(fup.wp < fuend) {
380 curlineno = (int)*fup.wp++;
383 // Search for chunk containing location to fix up; compute a pointer to the location
384 // (in the chunk). Often we will find the fixup is in the "cached" chunk, so the
385 // linear-search is seldom executed.
386 if(loc < cch->chloc || loc >= (cch->chloc + cch->ch_size)) {
387 for(cch = sc->sfcode; cch != NULL; cch = cch->chnext)
388 if(loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
391 interror(7); // Fixup (loc) out of range
395 locp = cch->chptr + (loc - cch->chloc);
399 // Compute expression/symbol value and attribs
400 if(w & FU_EXPR) { // Complex expression
402 if(evexpr(fup.tk, &eval, &eattr, &esym) != OK) {
407 } else { // Simple symbol
414 if((eattr & (GLOBAL|DEFINED)) == GLOBAL)
417 tdb = (WORD)(eattr & TDB);
418 // If the expression is undefined and no external symbol is involved, then it's an error.
419 if(!(eattr & DEFINED) && esym == NULL) {
425 if(((w & 0x0F00) == FU_MOVEI) && esym)
426 esym->sattre |= RISCSYM;
430 // If a PC-relative fixup is undefined, its value is *not* subtracted from the location
431 // (that will happen in the linker when the external reference is resolved).
433 // MWC expects PC-relative things to have the LOC subtracted from the value, if the
434 // value is external (that is, undefined at this point).
436 // PC-relative fixups must be DEFINED and either in the same section (whereupon the
437 // subtraction takes place) or ABS (with no subtract).
439 if(eattr & DEFINED) {
440 if(tdb == sno) eval -= (VALUE)loc;
442 error("PC-relative expr across sections");
446 if(sbra_flag && (w & FU_LBRA) && (eval + 0x80 < 0x100))
447 warn("unoptimized short branch");
449 if(obj_format == MWC) eval -= (VALUE)loc;
456 switch((int)(w & FUMASK)) {
457 // FU_BBRA fixes up a one-byte branch offset.
459 if(!(eattr & DEFINED)) {
460 error("external short branch");
464 if(eval + 0x80 >= 0x100)
467 error("illegal bra.s with zero offset");
470 *++locp = (char)eval;
472 // Fixup one-byte value at locp + 1.
476 // Fixup one-byte forward references
478 if(!(eattr & DEFINED)) {
479 error("external byte reference");
483 error("non-absolute byte reference");
486 if((w & FU_PCREL) && eval + 0x80 >= 0x100) goto range;
488 if(eval + 0x100 >= 0x200) goto range;
490 if(eval >= 0x100) goto range;
494 // Fixup WORD forward references;
495 // the word could be unaligned in the section buffer, so we have to be careful.
497 if(((w & 0x0F00) == FU_JR) || ((w & 0x0F00) == FU_MJR)) {
500 reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
502 reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
504 if((w & 0x0F00) == FU_MJR) {
505 // Main code destination alignment checking here for forward declared labels
506 address = (oaddr) ? oaddr : loc;
507 if(((address >= 0xF03000) && (address < 0xF04000) && (eval < 0xF03000)) ||
508 ((eval >= 0xF03000) && (eval < 0xF04000) && (address < 0xF03000)) ) {
509 warni("* \'jr\' at $%08X - cannot jump relative between "
510 "main memory and local gpu ram", address);
512 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
514 // This jump is to a page outside of the current 256 byte page
516 warni("* \'jr\' at $%08X - destination address not aligned for long page jump, "
517 "insert a \'nop\' before the destination address", address);
520 // This jump is in the current 256 byte page
522 warni("* \'jr\' at $%08X - destination address not aligned for short page jump, "
523 "insert a \'nop\' before the destination address", address);
528 if((reg2 < -16) || (reg2 > 15)) {
529 error("relative jump out of range");
532 *locp = (char)(*locp | ((reg2 >> 3) & 0x03));
534 *locp = (char)(*locp | ((reg2 & 0x07) << 5));
537 if((w & 0x0F00) == FU_NUM15) {
538 if(eval < -16 || eval > 15) {
539 error("constant out of range");
542 *locp = (char)(*locp | ((eval >> 3) & 0x03));
544 *locp = (char)(*locp | ((eval & 0x07) << 5));
547 if((w & 0x0F00) == FU_NUM31) {
548 if(eval < 0 || eval > 31) {
549 error("constant out of range");
552 *locp = (char)(*locp | ((eval >> 3) & 0x03));
554 *locp = (char)(*locp | ((eval & 0x07) << 5));
557 if((w & 0x0F00) == FU_NUM32) {
558 if(eval < 1 || eval > 32) {
559 error("constant out of range");
564 eval = (eval == 32) ? 0 : eval;
565 *locp = (char)(*locp | ((eval >> 3) & 0x03));
567 *locp = (char)(*locp | ((eval & 0x07) << 5));
570 if((w & 0x0F00) == FU_REGONE) {
571 if(eval < 0 || eval > 31) {
572 error("register value out of range");
575 *locp = (char)(*locp | ((eval >> 3) & 0x03));
577 *locp = (char)(*locp | ((eval & 0x07) << 5));
580 if((w & 0x0F00) == FU_REGTWO) {
581 if(eval < 0 || eval > 31) {
582 error("register value out of range");
586 *locp = (char)(*locp | (eval & 0x1F));
590 if(!(eattr & DEFINED)) {
594 rmark(sno, loc, 0, w, esym);
597 rmark(sno, loc, tdb, MWORD, NULL);
599 if(eval + 0x10000 >= 0x20000)
602 if(w & FU_ISBRA) { // Range-check BRA and DBRA
603 if(eval + 0x8000 >= 0x10000)
610 *locp++ = (char)(eval >> 8);
613 // Fixup LONG forward references;
614 // the long could be unaligned in the section buffer, so be careful (again).
616 if((w & 0x0F00) == FU_MOVEI) {
618 if(eattr & DEFINED) {
619 for(j = 0; j < fwindex; j++) {
620 if(fwdjump[j] == address) {
621 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
625 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for long page jump, "
626 "insert a \'nop\' before the destination address", address);
627 if(listing > 0) ship_ln(buf);
628 if(err_flag) write(err_fd, buf, (LONG)strlen(buf));
629 else printf("%s\n", buf);
632 if(!(eval & 0x0000000F) || ((eval - 2) % 4)) {
634 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for short page jump, "
635 "insert a \'nop\' before the destination address", address);
636 if(listing > 0) ship_ln(buf);
637 if(err_flag) write(err_fd, buf, (LONG)strlen(buf));
638 else printf("%s\n", buf);
641 // Clear this jump as it has been checked
647 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
648 flags = (MLONG|MMOVEI);
649 } else flags = MLONG;
650 if(!(eattr & DEFINED)) {
651 rmark(sno, loc, 0, flags, esym);
654 rmark(sno, loc, tdb, flags, NULL);
656 *locp++ = (char)(eval >> 24);
657 *locp++ = (char)(eval >> 16);
658 *locp++ = (char)(eval >> 8);
661 // Fixup a 3-bit "QUICK" reference in bits 9..1
662 // (range of 1..8) in a word. Really bits 1..3 in a byte.
664 if(!(eattr & DEFINED)) {
665 error("External quick reference");
669 if(eval < 1 || eval > 8)
671 *locp |= (eval & 7) << 1;
673 // Fix up 6502 funny branch
676 if(eval + 0x80 >= 0x100)
681 interror(4); // Bad fixup type
688 error("expression out of range");