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
21 // Function prototypes
22 void mksect(int, WORD);
25 // Section descriptors
26 SECT sect[NSECTS]; // All sections...
27 int cursect; // Current section number
29 // These are copied from the section descriptor, the current code chunk
30 // descriptor and the current fixup chunk descriptor when a switch is made into
31 // a section. They are copied back to the descriptors when the section is left.
32 WORD scattr; // Section attributes
33 LONG sloc; // Current loc in section
35 CHUNK * scode; // Current (last) code chunk
36 LONG challoc; // # bytes alloc'd to code chunk
37 LONG ch_size; // # bytes used in code chunk
38 char * chptr; // Deposit point in code chunk buffer
40 CHUNK * sfix; // Current (last) fixup chunk
41 LONG fchalloc; // # bytes alloc'd to fixup chunk
42 LONG fchsize; // # bytes used in fixup chunk
43 PTR fchptr; // Deposit point in fixup chunk buffer
45 // Return a size (SIZB, SIZW, SIZL) or 0, depending on what kind of fixup is
46 // associated with a location.
47 static char fusiztab[] = {
58 // Offset to REAL fixup location
59 static char fusizoffs[] = {
72 // Initialize Sections; Setup initial ABS, TEXT, DATA and BSS sections
74 void InitSection(void)
78 // Cleanup all sections
79 for(i=0; i<NSECTS; i++)
82 // Construct default sections, make TEXT the current section
83 mksect(ABS, SUSED | SABS | SBSS); // ABS
84 mksect(TEXT, SUSED | TEXT ); // TEXT
85 mksect(DATA, SUSED | DATA ); // DATA
86 mksect(BSS, SUSED | BSS | SBSS ); // BSS
87 // mksect(M6502, SUSED | TEXT ); // 6502 code section
89 switchsect(TEXT); // Switch to TEXT for starters
94 // Make a New (Clean) Section
96 void mksect(int sno, WORD attr)
98 SECT * p = §[sno];
101 p->scode = p->sfcode = NULL;
102 p->sfix = p->sffix = NULL;
107 // Switch to Another Section (Copy Section & Chunk Descriptors to Global Vars
110 void switchsect(int sno)
112 CHUNK * cp; // Chunk pointer
114 SECT * p = §[sno];
116 scattr = p->scattr; // Copy section vars
121 // Copy code chunk vars
122 if ((cp = scode) != NULL)
124 challoc = cp->challoc;
125 ch_size = cp->ch_size;
126 chptr = cp->chptr + ch_size;
129 challoc = ch_size = 0;
131 // Copy fixup chunk vars
132 if ((cp = sfix) != NULL)
134 fchalloc = cp->challoc;
135 fchsize = cp->ch_size;
136 fchptr.cp = cp->chptr + fchsize;
139 fchalloc = fchsize = 0;
144 // Save Current Section
148 SECT * p = §[cursect];
150 p->scattr = scattr; // Bailout section vars
153 if (scode != NULL) // Bailout code chunk
154 scode->ch_size = ch_size;
156 if (sfix != NULL) // Bailout fixup chunk
157 sfix->ch_size = fchsize;
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 DEBUG { printf("chcheck(%u)\n", amt); }
216 // If in BSS section, no allocation required
223 DEBUG { printf(" challoc=%i, ch_size=%i, diff=%i\n", challoc, ch_size, challoc-ch_size); }
224 if ((int)(challoc - ch_size) >= (int)amt)
227 if (amt < CH_CODE_SIZE)
230 DEBUG { printf(" amt (adjusted)=%u\n", amt); }
231 SECT * p = §[cursect];
232 CHUNK * cp = malloc(sizeof(CHUNK) + amt);
234 // First chunk in section
240 // Add chunk to other chunks
245 scode->ch_size = ch_size; // Save old chunk's globals
248 // Setup chunk and global vars
251 challoc = cp->challoc = amt;
252 ch_size = cp->ch_size = 0;
253 chptr = cp->chptr = ((char *)cp) + sizeof(CHUNK);
254 scode = p->scode = cp;
260 // This is really wrong. We need to make some proper structures here so we don't
261 // have to count sizes of objects, that's what the compiler's for! :-P
262 #define FIXUP_BASE_SIZE (sizeof(WORD) + sizeof(LONG) + sizeof(WORD) + sizeof(WORD))
264 // Arrange for a fixup on a location
266 int fixup(WORD attr, LONG loc, TOKEN * fexpr)
272 // Shamus: Expression lengths are voodoo ATM (variable "i"). Need to fix this.
273 #warning "!!! fixup() is filled with VOODOO !!!"
274 DEBUG printf("FIXUP@$%X: $%X\n", loc, attr);
276 // Compute length of expression (could be faster); determine if it's the
277 // single-symbol case; no expression if it's just a mark. This code assumes
278 // 16 bit WORDs and 32 bit LONGs
279 if (*fexpr == SYMBOL && fexpr[2] == ENDEXPR)
281 // Just a single symbol
282 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
283 if ((attr & 0x0F00) == FU_JR)
286 // i = FIXUP_BASE_SIZE + (sizeof(LONG) * 2);
287 i = FIXUP_BASE_SIZE + sizeof(SYM *) + sizeof(LONG);
292 i = FIXUP_BASE_SIZE + sizeof(SYM *);
299 for(len=0; fexpr[len]!=ENDEXPR; len++)
301 if (fexpr[len] == CONST || fexpr[len] == SYMBOL)
305 len++; // Add 1 for ENDEXPR
306 // i = (len << 2) + 12;
307 i = FIXUP_BASE_SIZE + sizeof(WORD) + (len * sizeof(TOKEN));
310 // Maybe alloc another fixup chunk for this one to fit in
311 if ((fchalloc - fchsize) < i)
314 cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
316 // First fixup chunk in section
322 // Add to other chunks
327 sfix->ch_size = fchsize;
330 // Setup fixup chunk and its global vars
332 fchalloc = cp->challoc = CH_FIXUP_SIZE;
333 fchsize = cp->ch_size = 0;
334 fchptr.cp = cp->chptr = ((char *)cp) + sizeof(CHUNK);
338 // Record fixup type, fixup location, and the file number and line number
339 // the fixup is located at.
342 *fchptr.wp++ = cfileno;
343 *fchptr.wp++ = (WORD)curlineno;
345 // Store postfix expression or pointer to a single symbol, or nothing for a
349 *fchptr.wp++ = (WORD)len;
352 *fchptr.lp++ = (LONG)*fexpr++;
356 // *fchptr.lp++ = (LONG)fexpr[1];
357 *fchptr.sy++ = symbolPtr[fexpr[1]];
360 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
361 if ((attr & 0x0F00) == FU_JR)
364 *fchptr.lp++ = orgaddr;
366 *fchptr.lp++ = 0x00000000;
375 // Resolve all Fixups
377 int ResolveAllFixups(void)
382 // Make undefined symbols GLOBL
386 DEBUG printf("Resolving TEXT sections...\n");
388 DEBUG printf("Resolving DATA sections...\n");
393 // We need to do a final check of forward 'jump' destination addresses that
395 for(i=0; i<MAXFWDJUMPS; i++)
400 sprintf(buf, "* \'jump\' at $%08X - destination address is external to this source file and cannot have its aligment validated", fwdjump[i]);
406 write(err_fd, buf, (LONG)strlen(buf));
418 // Resolve Fixups in a Section
420 int ResolveFixups(int sno)
422 PTR fup; // Current fixup
423 WORD * fuend; // End of last fixup (in this chunk)
424 WORD w; // Fixup word (type+modes+flags)
425 char * locp; // Location to fix (in cached chunk)
426 LONG loc; // Location to fixup
427 VALUE eval; // Expression value
428 WORD eattr; // Expression attrib
429 SYM * esym; // External symbol involved in expr
430 SYM * sy; // (Temp) pointer to a symbol
431 WORD i; // (Temp) word
432 WORD tdb; // eattr & TDB
436 unsigned page_jump = 0;
437 unsigned address = 0;
441 SECT * sc = §[sno];
442 CHUNK * ch = sc->sffix;
447 CHUNK * cch = sc->sfcode; // "cache" first chunk
449 if (cch == NULL) // Can't fixup a sect with nothing in it
454 fup.cp = ch->chptr; // fup -> start of chunk
455 fuend = (WORD *)(fup.cp + ch->ch_size); // fuend -> end of chunk
457 while (fup.wp < fuend)
462 curlineno = (int)*fup.wp++;
463 DEBUG { printf("ResolveFixups: cfileno=%u\n", cfileno); }
464 // This is based on global vars cfileno, curfname :-P
465 // This approach is kinda meh as well. I think we can do better than this.
466 SetFilenameForErrorReporting();
470 // Search for chunk containing location to fix up; compute a
471 // pointer to the location (in the chunk). Often we will find the
472 // fixup is in the "cached" chunk, so the linear-search is seldom
474 if (loc < cch->chloc || loc >= (cch->chloc + cch->ch_size))
476 for(cch=sc->sfcode; cch!=NULL; cch=cch->chnext)
478 if (loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
484 interror(7); // Fixup (loc) out of range
489 locp = cch->chptr + (loc - cch->chloc);
492 // Compute expression/symbol value and attribs
493 // Complex expression
498 if (evexpr(fup.tk, &eval, &eattr, &esym) != OK)
517 if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
521 tdb = (WORD)(eattr & TDB);
523 // If the expression is undefined and no external symbol is
524 // involved, then it's an error.
525 if (!(eattr & DEFINED) && esym == NULL)
531 if (((w & 0x0F00) == FU_MOVEI) && esym)
532 esym->sattre |= RISCSYM;
536 // If a PC-relative fixup is undefined, its value is *not*
537 // subtracted from the location (that will happen in the linker
538 // when the external reference is resolved).
540 // MWC expects PC-relative things to have the LOC subtracted from
541 // the value, if the value is external (that is, undefined at this
544 // PC-relative fixups must be DEFINED and either in the same
545 // section (whereupon the subtraction takes place) or ABS (with no
555 error("PC-relative expr across sections");
559 if (sbra_flag && (w & FU_LBRA) && (eval + 0x80 < 0x100))
560 warn("unoptimized short branch");
562 else if (obj_format == MWC)
570 switch ((int)(w & FUMASK))
572 // FU_BBRA fixes up a one-byte branch offset.
574 if (!(eattr & DEFINED))
576 error("external short branch");
582 if (eval + 0x80 >= 0x100)
587 error("illegal bra.s with zero offset");
591 *++locp = (char)eval;
593 // Fixup one-byte value at locp + 1.
597 // Fixup one-byte forward references
599 if (!(eattr & DEFINED))
601 error("external byte reference");
607 error("non-absolute byte reference");
611 if ((w & FU_PCREL) && eval + 0x80 >= 0x100)
616 if (eval + 0x100 >= 0x200)
619 else if (eval >= 0x100)
624 // Fixup WORD forward references;
625 // the word could be unaligned in the section buffer, so we have to
628 if ((w & 0x0F00) == FU_JR)// || ((w & 0x0F00) == FU_MJR))
633 reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
635 reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
638 if ((w & 0x0F00) == FU_MJR)
640 // Main code destination alignment checking here for
641 // forward declared labels
642 address = (oaddr) ? oaddr : loc;
644 if (((address >= 0xF03000) && (address < 0xF04000)
645 && (eval < 0xF03000)) || ((eval >= 0xF03000)
646 && (eval < 0xF04000) && (address < 0xF03000)))
648 warni("* \'jr\' at $%08X - cannot jump relative between "
649 "main memory and local gpu ram", address);
653 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
657 // This jump is to a page outside of the
658 // current 256 byte page
661 warni("* \'jr\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
666 // This jump is in the current 256 byte page
669 warni("* \'jr\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
676 if ((reg2 < -16) || (reg2 > 15))
678 error("relative jump out of range");
682 *locp = (char)(*locp | ((reg2 >> 3) & 0x03));
684 *locp = (char)(*locp | ((reg2 & 0x07) << 5));
688 if ((w & 0x0F00) == FU_NUM15)
690 if (eval < -16 || eval > 15)
692 error("constant out of range");
696 *locp = (char)(*locp | ((eval >> 3) & 0x03));
698 *locp = (char)(*locp | ((eval & 0x07) << 5));
702 if ((w & 0x0F00) == FU_NUM31)
704 if (eval < 0 || eval > 31)
706 error("constant out of range");
710 *locp = (char)(*locp | ((eval >> 3) & 0x03));
712 *locp = (char)(*locp | ((eval & 0x07) << 5));
716 if ((w & 0x0F00) == FU_NUM32)
718 if (eval < 1 || eval > 32)
720 error("constant out of range");
727 eval = (eval == 32) ? 0 : eval;
728 *locp = (char)(*locp | ((eval >> 3) & 0x03));
730 *locp = (char)(*locp | ((eval & 0x07) << 5));
734 if ((w & 0x0F00) == FU_REGONE)
736 if (eval < 0 || eval > 31)
738 error("register value out of range");
742 *locp = (char)(*locp | ((eval >> 3) & 0x03));
744 *locp = (char)(*locp | ((eval & 0x07) << 5));
748 if ((w & 0x0F00) == FU_REGTWO)
750 if (eval < 0 || eval > 31)
752 error("register value out of range");
757 *locp = (char)(*locp | (eval & 0x1F));
761 if (!(eattr & DEFINED))
768 rmark(sno, loc, 0, w, esym);
773 rmark(sno, loc, tdb, MWORD, NULL);
777 if (eval + 0x10000 >= 0x20000)
782 // Range-check BRA and DBRA
785 if (eval + 0x8000 >= 0x10000)
788 else if (eval >= 0x10000)
793 *locp++ = (char)(eval >> 8);
796 // Fixup LONG forward references;
797 // the long could be unaligned in the section buffer, so be careful
800 if ((w & 0x0F00) == FU_MOVEI)
807 for(j=0; j<fwindex; j++)
809 if (fwdjump[j] == address)
811 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
818 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
824 write(err_fd, buf, (LONG)strlen(buf));
831 if (!(eval & 0x0000000F) || ((eval - 2) % 4))
834 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
840 write(err_fd, buf, (LONG)strlen(buf));
846 // Clear this jump as it has been checked
854 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
855 flags = (MLONG | MMOVEI);
860 if (!(eattr & DEFINED))
862 //printf("Fixup (long): Symbol undefined. loc = $%X, long = $%X, flags = $%x\n", loc, eval, flags);
863 rmark(sno, loc, 0, flags, esym);
867 //printf("Fixup (long): TDB = $%X. loc =$%X, long = $%X, flags = $%x\n", tdb, loc, eval, flags);
868 rmark(sno, loc, tdb, flags, NULL);
871 //printf("Fixup (long): TDB = $%X. loc =$%X, long = $%X, flags = $%x\n", tdb, loc, eval, flags);
873 *locp++ = (char)(eval >> 24);
874 *locp++ = (char)(eval >> 16);
875 *locp++ = (char)(eval >> 8);
879 // Fixup a 3-bit "QUICK" reference in bits 9..1
880 // (range of 1..8) in a word. Really bits 1..3 in a byte.
882 if (!(eattr & DEFINED))
884 error("External quick reference");
888 if (eval < 1 || eval > 8)
891 *locp |= (eval & 7) << 1;
894 // Fix up 6502 funny branch
898 if (eval + 0x80 >= 0x100)
905 interror(4); // Bad fixup type
910 error("expression out of range");