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
20 // Section descriptors
21 SECT sect[NSECTS]; // All sections...
22 int cursect; // Current section number
24 // These are copied from the section descriptor, the current code chunk
25 // descriptor and the current fixup chunk descriptor when a switch is made into
26 // a section. They are copied back to the descriptors when the section is left.
27 WORD scattr; // Section attributes
28 LONG sloc; // Current loc in section
30 CHUNK * scode; // Current (last) code chunk
31 LONG challoc; // # bytes alloc'd to code chunk
32 LONG ch_size; // # bytes used in code chunk
33 char * chptr; // Deposit point in code chunk buffer
35 CHUNK * sfix; // Current (last) fixup chunk
36 LONG fchalloc; // # bytes alloc'd to fixup chunk
37 LONG fchsize; // # bytes used in fixup chunk
38 PTR fchptr; // Deposit point in fixup chunk buffer
40 unsigned fwdjump[MAXFWDJUMPS]; // forward jump check table
41 unsigned fwindex = 0; // forward jump index
43 // Return a size (SIZB, SIZW, SIZL) or 0, depending on what kind of fixup is
44 // associated with a location.
45 static char fusiztab[] = {
56 // Offset to REAL fixup location
57 static char fusizoffs[] = {
70 // Make a New (Clean) Section
72 void mksect(int sno, WORD attr)
74 SECT * p; // Section pointer
79 p->scode = p->sfcode = NULL;
80 p->sfix = p->sffix = NULL;
85 // Switch to Another Section (Copy Section & Chunk Descriptors to Global Vars
88 void switchsect(int sno)
90 CHUNK * cp; // Chunk pointer
92 SECT * p = §[sno];
94 scattr = p->scattr; // Copy section vars
99 // Copy code chunk vars
100 if ((cp = scode) != NULL)
102 challoc = cp->challoc;
103 ch_size = cp->ch_size;
104 chptr = cp->chptr + ch_size;
107 challoc = ch_size = 0;
109 // Copy fixup chunk vars
110 if ((cp = sfix) != NULL)
112 fchalloc = cp->challoc;
113 fchsize = cp->ch_size;
114 fchptr.cp = cp->chptr + fchsize;
117 fchalloc = fchsize = 0;
122 // Save Current Section
126 SECT * p = §[cursect];
128 p->scattr = scattr; // Bailout section vars
131 if (scode != NULL) // Bailout code chunk
132 scode->ch_size = ch_size;
134 if (sfix != NULL) // Bailout fixup chunk
135 sfix->ch_size = fchsize;
140 // Initialize Sections; Setup initial ABS, TEXT, DATA and BSS sections
146 // Cleanup all sections
147 for(i=0; i<NSECTS; i++)
150 // Construct default sections, make TEXT the current section
151 mksect(ABS, SUSED | SABS | SBSS); // ABS
152 mksect(TEXT, SUSED | TEXT ); // TEXT
153 mksect(DATA, SUSED | DATA ); // DATA
154 mksect(BSS, SUSED | BSS | SBSS ); // BSS
155 // mksect(M6502, SUSED | TEXT ); // 6502 code section
157 switchsect(TEXT); // Switch to TEXT for starters
162 // Test to see if a location has a fixup sic'd on it. This is used by the
163 // listing generator to print 'xx's instead of '00's for forward references
165 int fixtest(int sno, LONG loc)
173 stopmark(); // Force update to sect[] variables
175 // Hairy, ugly linear search for a mark on our location;
176 // the speed doesn't matter, since this is only done when generating a
177 // listing, which is SLOW.
178 for(ch=sect[sno].sffix; ch!=NULL; ch=ch->chnext)
180 fup.cp = (char *)ch->chptr;
181 fuend = fup.cp + ch->ch_size;
183 while (fup.cp < fuend)
186 xloc = *fup.lp++ + (int)fusizoffs[w & FUMASK];
190 return (int)fusiztab[w & FUMASK];
207 // Check that there are at least `amt' bytes left in the current chunk. If
208 // there are not, allocate another chunk of at least `amt' bytes (and probably
211 // If `amt' is zero, ensure there are at least CH_THRESHOLD bytes, likewise.
213 int chcheck(LONG amt)
215 // If in BSS section, no allocation required
222 if ((int)(challoc - ch_size) >= (int)amt)
225 if (amt < CH_CODE_SIZE)
228 SECT * p = §[cursect];
229 CHUNK * cp = malloc(sizeof(CHUNK) + amt);
231 // First chunk in section
237 // Add chunk to other chunks
242 scode->ch_size = ch_size; // Save old chunk's globals
245 // Setup chunk and global vars
248 challoc = cp->challoc = amt;
249 ch_size = cp->ch_size = 0;
250 chptr = cp->chptr = ((char *)cp) + sizeof(CHUNK);
251 scode = p->scode = cp;
257 // This is really wrong. We need to make some proper structures here so we don't
258 // have to count sizes of objects, that's what the compiler's for! :-P
259 #define FIXUP_BASE_SIZE (sizeof(WORD) + sizeof(LONG) + sizeof(WORD) + sizeof(WORD))
261 // Arrange for a fixup on a location
263 int fixup(WORD attr, LONG loc, TOKEN * fexpr)
269 // Shamus: Expression lengths are voodoo ATM (varibale "i"). Need to fix this.
270 DEBUG printf("FIXUP@$%X: $%X\n", loc, attr);
272 // Compute length of expression (could be faster); determine if it's the
273 // single-symbol case; no expression if it's just a mark. This code assumes
274 // 16 bit WORDs and 32 bit LONGs
275 if (*fexpr == SYMBOL && fexpr[2] == ENDEXPR)
277 // Just a single symbol
278 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
279 if ((attr & 0x0F00) == FU_JR)
282 i = FIXUP_BASE_SIZE + (sizeof(LONG) * 2);
287 i = FIXUP_BASE_SIZE + sizeof(SYM *);
294 for(len=0; fexpr[len]!=ENDEXPR; len++)
296 if (fexpr[len] == CONST || fexpr[len] == SYMBOL)
300 len++; // Add 1 for ENDEXPR
301 // i = (len << 2) + 12;
302 i = FIXUP_BASE_SIZE + sizeof(WORD) + (len * sizeof(TOKEN));
305 // Maybe alloc another fixup chunk for this one to fit in
306 if ((fchalloc - fchsize) < i)
309 cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
311 // First fixup chunk in section
317 // Add to other chunks
322 sfix->ch_size = fchsize;
325 // Setup fixup chunk and its global vars
327 fchalloc = cp->challoc = CH_FIXUP_SIZE;
328 fchsize = cp->ch_size = 0;
329 fchptr.cp = cp->chptr = ((char *)cp) + sizeof(CHUNK);
333 // Record fixup type, fixup location, and the file number and line number
334 // the fixup is located at.
337 *fchptr.wp++ = cfileno;
338 *fchptr.wp++ = (WORD)curlineno;
340 // Store postfix expression or pointer to a single symbol, or nothing for a
344 *fchptr.wp++ = (WORD)len;
347 *fchptr.lp++ = (LONG)*fexpr++;
351 // *fchptr.lp++ = (LONG)fexpr[1];
352 *fchptr.sy++ = symbolPtr[fexpr[1]];
355 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
356 if ((attr & 0x0F00) == FU_JR)
359 *fchptr.lp++ = orgaddr;
361 *fchptr.lp++ = 0x00000000;
370 // Resolve all Fixups
372 int ResolveAllFixups(void)
377 // Make undefined symbols GLOBL
381 DEBUG printf("Resolving TEXT sections...\n");
383 DEBUG printf("Resolving DATA sections...\n");
386 // We need to do a final check of forward 'jump' destination addresses that
388 for(i=0; i<MAXFWDJUMPS; i++)
393 sprintf(buf, "* \'jump\' at $%08X - destination address is external to this source file and cannot have its aligment validated", fwdjump[i]);
399 write(err_fd, buf, (LONG)strlen(buf));
410 // Resolve Fixups in a Section
412 int ResolveFixups(int sno)
414 PTR fup; // Current fixup
415 WORD * fuend; // End of last fixup (in this chunk)
416 WORD w; // Fixup word (type+modes+flags)
417 char * locp; // Location to fix (in cached chunk)
418 LONG loc; // Location to fixup
419 VALUE eval; // Expression value
420 WORD eattr; // Expression attrib
421 SYM * esym; // External symbol involved in expr
422 SYM * sy; // (Temp) pointer to a symbol
423 WORD i; // (Temp) word
424 WORD tdb; // eattr & TDB
428 unsigned page_jump = 0;
429 unsigned address = 0;
433 SECT * sc = §[sno];
434 CHUNK * ch = sc->sffix;
439 CHUNK * cch = sc->sfcode; // "cache" first chunk
441 if (cch == NULL) // Can't fixup a sect with nothing in it
446 fup.cp = ch->chptr; // fup -> start of chunk
447 fuend = (WORD *)(fup.cp + ch->ch_size); // fuend -> end of chunk
449 while (fup.wp < fuend)
454 curlineno = (int)*fup.wp++;
458 // Search for chunk containing location to fix up; compute a
459 // pointer to the location (in the chunk). Often we will find the
460 // fixup is in the "cached" chunk, so the linear-search is seldom
462 if (loc < cch->chloc || loc >= (cch->chloc + cch->ch_size))
464 for(cch=sc->sfcode; cch!=NULL; cch=cch->chnext)
466 if (loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
472 interror(7); // Fixup (loc) out of range
477 locp = cch->chptr + (loc - cch->chloc);
480 // Compute expression/symbol value and attribs
481 // Complex expression
486 if (evexpr(fup.tk, &eval, &eattr, &esym) != OK)
505 if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
509 tdb = (WORD)(eattr & TDB);
511 // If the expression is undefined and no external symbol is
512 // involved, then it's an error.
513 if (!(eattr & DEFINED) && esym == NULL)
519 if (((w & 0x0F00) == FU_MOVEI) && esym)
520 esym->sattre |= RISCSYM;
524 // If a PC-relative fixup is undefined, its value is *not*
525 // subtracted from the location (that will happen in the linker
526 // when the external reference is resolved).
528 // MWC expects PC-relative things to have the LOC subtracted from
529 // the value, if the value is external (that is, undefined at this
532 // PC-relative fixups must be DEFINED and either in the same
533 // section (whereupon the subtraction takes place) or ABS (with no
543 error("PC-relative expr across sections");
547 if (sbra_flag && (w & FU_LBRA) && (eval + 0x80 < 0x100))
548 warn("unoptimized short branch");
550 else if (obj_format == MWC)
558 switch ((int)(w & FUMASK))
560 // FU_BBRA fixes up a one-byte branch offset.
562 if (!(eattr & DEFINED))
564 error("external short branch");
570 if (eval + 0x80 >= 0x100)
575 error("illegal bra.s with zero offset");
579 *++locp = (char)eval;
581 // Fixup one-byte value at locp + 1.
585 // Fixup one-byte forward references
587 if (!(eattr & DEFINED))
589 error("external byte reference");
595 error("non-absolute byte reference");
599 if ((w & FU_PCREL) && eval + 0x80 >= 0x100)
604 if (eval + 0x100 >= 0x200)
607 else if (eval >= 0x100)
612 // Fixup WORD forward references;
613 // the word could be unaligned in the section buffer, so we have to
616 if (((w & 0x0F00) == FU_JR) || ((w & 0x0F00) == FU_MJR))
622 reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
626 reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
629 if ((w & 0x0F00) == FU_MJR)
631 // Main code destination alignment checking here for
632 // forward declared labels
633 address = (oaddr) ? oaddr : loc;
635 if (((address >= 0xF03000) && (address < 0xF04000)
636 && (eval < 0xF03000)) || ((eval >= 0xF03000)
637 && (eval < 0xF04000) && (address < 0xF03000)))
639 warni("* \'jr\' at $%08X - cannot jump relative between "
640 "main memory and local gpu ram", address);
644 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
648 // This jump is to a page outside of the
649 // current 256 byte page
652 warni("* \'jr\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
657 // This jump is in the current 256 byte page
660 warni("* \'jr\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
666 if ((reg2 < -16) || (reg2 > 15))
668 error("relative jump out of range");
672 *locp = (char)(*locp | ((reg2 >> 3) & 0x03));
674 *locp = (char)(*locp | ((reg2 & 0x07) << 5));
678 if ((w & 0x0F00) == FU_NUM15)
680 if (eval < -16 || eval > 15)
682 error("constant out of range");
686 *locp = (char)(*locp | ((eval >> 3) & 0x03));
688 *locp = (char)(*locp | ((eval & 0x07) << 5));
692 if ((w & 0x0F00) == FU_NUM31)
694 if (eval < 0 || eval > 31)
696 error("constant out of range");
700 *locp = (char)(*locp | ((eval >> 3) & 0x03));
702 *locp = (char)(*locp | ((eval & 0x07) << 5));
706 if ((w & 0x0F00) == FU_NUM32)
708 if (eval < 1 || eval > 32)
710 error("constant out of range");
717 eval = (eval == 32) ? 0 : eval;
718 *locp = (char)(*locp | ((eval >> 3) & 0x03));
720 *locp = (char)(*locp | ((eval & 0x07) << 5));
724 if ((w & 0x0F00) == FU_REGONE)
726 if (eval < 0 || eval > 31)
728 error("register value out of range");
732 *locp = (char)(*locp | ((eval >> 3) & 0x03));
734 *locp = (char)(*locp | ((eval & 0x07) << 5));
738 if ((w & 0x0F00) == FU_REGTWO)
740 if (eval < 0 || eval > 31)
742 error("register value out of range");
747 *locp = (char)(*locp | (eval & 0x1F));
751 if (!(eattr & DEFINED))
758 rmark(sno, loc, 0, w, esym);
763 rmark(sno, loc, tdb, MWORD, NULL);
767 if (eval + 0x10000 >= 0x20000)
772 // Range-check BRA and DBRA
775 if (eval + 0x8000 >= 0x10000)
778 else if (eval >= 0x10000)
783 *locp++ = (char)(eval >> 8);
786 // Fixup LONG forward references;
787 // the long could be unaligned in the section buffer, so be careful
790 if ((w & 0x0F00) == FU_MOVEI)
796 for(j=0; j<fwindex; j++)
798 if (fwdjump[j] == address)
800 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
807 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
813 write(err_fd, buf, (LONG)strlen(buf));
820 if (!(eval & 0x0000000F) || ((eval - 2) % 4))
823 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
829 write(err_fd, buf, (LONG)strlen(buf));
835 // Clear this jump as it has been checked
842 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
843 flags = (MLONG|MMOVEI);
848 if (!(eattr & DEFINED))
850 rmark(sno, loc, 0, flags, esym);
854 rmark(sno, loc, tdb, flags, NULL);
857 *locp++ = (char)(eval >> 24);
858 *locp++ = (char)(eval >> 16);
859 *locp++ = (char)(eval >> 8);
862 // Fixup a 3-bit "QUICK" reference in bits 9..1
863 // (range of 1..8) in a word. Really bits 1..3 in a byte.
865 if (!(eattr & DEFINED))
867 error("External quick reference");
871 if (eval < 1 || eval > 8)
874 *locp |= (eval & 7) << 1;
876 // Fix up 6502 funny branch
880 if (eval + 0x80 >= 0x100)
886 interror(4); // Bad fixup type
891 error("expression out of range");