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 MakeSection(int, WORD);
23 void SwitchSection(int);
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 MakeSection(ABS, SUSED | SABS | SBSS); // ABS
84 MakeSection(TEXT, SUSED | TEXT ); // TEXT
85 MakeSection(DATA, SUSED | DATA ); // DATA
86 MakeSection(BSS, SUSED | BSS | SBSS); // BSS
87 // MakeSection(M6502, SUSED | TEXT ); // 6502 code section
89 SwitchSection(TEXT); // Switch to TEXT for starters
94 // Make a New (Clean) Section
96 void MakeSection(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 SwitchSection(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
146 void SaveSection(void)
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 AddFixup(WORD attr, LONG loc, TOKEN * fexpr)
272 // Shamus: Expression lengths are voodoo ATM (variable "i"). Need to fix this.
273 #warning "!!! AddFixup() 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 & FUMASKRISC) == 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 & FUMASKRISC) == 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");
396 // Resolve Fixups in a Section
398 int ResolveFixups(int sno)
400 PTR fup; // Current fixup
401 WORD * fuend; // End of last fixup (in this chunk)
402 WORD w; // Fixup word (type+modes+flags)
403 char * locp; // Location to fix (in cached chunk)
404 LONG loc; // Location to fixup
405 VALUE eval; // Expression value
406 WORD eattr; // Expression attrib
407 SYM * esym; // External symbol involved in expr
408 SYM * sy; // (Temp) pointer to a symbol
409 WORD i; // (Temp) word
410 WORD tdb; // eattr & TDB
414 unsigned page_jump = 0;
415 unsigned address = 0;
419 SECT * sc = §[sno];
420 CHUNK * ch = sc->sffix;
425 // "Cache" first chunk
426 CHUNK * cch = sc->sfcode;
428 // Can't fixup a sect with nothing in it
434 fup.cp = ch->chptr; // fup -> start of chunk
435 fuend = (WORD *)(fup.cp + ch->ch_size); // fuend -> end of chunk
437 while (fup.wp < fuend)
442 curlineno = (int)*fup.wp++;
443 DEBUG { printf("ResolveFixups: cfileno=%u\n", cfileno); }
444 // This is based on global vars cfileno, curfname :-P
445 // This approach is kinda meh as well. I think we can do better than this.
446 SetFilenameForErrorReporting();
450 // Search for chunk containing location to fix up; compute a
451 // pointer to the location (in the chunk). Often we will find the
452 // fixup is in the "cached" chunk, so the linear-search is seldom
454 if (loc < cch->chloc || loc >= (cch->chloc + cch->ch_size))
456 for(cch=sc->sfcode; cch!=NULL; cch=cch->chnext)
458 if (loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
464 // Fixup (loc) out of range
470 locp = cch->chptr + (loc - cch->chloc);
473 // Compute expression/symbol value and attribs
474 // Complex expression
479 if (evexpr(fup.tk, &eval, &eattr, &esym) != OK)
498 if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
502 tdb = (WORD)(eattr & TDB);
504 // If the expression is undefined and no external symbol is
505 // involved, then it's an error.
506 if (!(eattr & DEFINED) && (esym == NULL))
512 // It seems that this is completely unnecessary!
514 if (((w & FUMASKRISC) == FU_MOVEI) && esym)
516 //printf("DoFixups: Setting symbol attre to RISCSYM...\n");
517 esym->sattre |= RISCSYM;
523 // If a PC-relative fixup is undefined, its value is *not*
524 // subtracted from the location (that will happen in the linker
525 // when the external reference is resolved).
527 // MWC expects PC-relative things to have the LOC subtracted from
528 // the value, if the value is external (that is, undefined at this
531 // PC-relative fixups must be DEFINED and either in the same
532 // section (whereupon the subtraction takes place) or ABS (with no
542 error("PC-relative expr across sections");
546 if (sbra_flag && (w & FU_LBRA) && (eval + 0x80 < 0x100))
547 warn("unoptimized short branch");
549 else if (obj_format == MWC)
557 switch ((int)(w & FUMASK))
559 // FU_BBRA fixes up a one-byte branch offset.
561 if (!(eattr & DEFINED))
563 error("external short branch");
569 if (eval + 0x80 >= 0x100)
574 error("illegal bra.s with zero offset");
578 *++locp = (char)eval;
580 // Fixup one-byte value at locp + 1.
584 // Fixup one-byte forward references
586 if (!(eattr & DEFINED))
588 error("external byte reference");
594 error("non-absolute byte reference");
598 if ((w & FU_PCREL) && eval + 0x80 >= 0x100)
603 if (eval + 0x100 >= 0x200)
606 else if (eval >= 0x100)
611 // Fixup WORD forward references;
612 // the word could be unaligned in the section buffer, so we have to
615 if ((w & FUMASKRISC) == FU_JR)// || ((w & 0x0F00) == FU_MJR))
620 reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
622 reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
625 if ((w & 0x0F00) == FU_MJR)
627 // Main code destination alignment checking here for
628 // forward declared labels
629 address = (oaddr) ? oaddr : loc;
631 if (((address >= 0xF03000) && (address < 0xF04000)
632 && (eval < 0xF03000)) || ((eval >= 0xF03000)
633 && (eval < 0xF04000) && (address < 0xF03000)))
635 warni("* \'jr\' at $%08X - cannot jump relative between "
636 "main memory and local gpu ram", address);
640 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
644 // This jump is to a page outside of the
645 // current 256 byte page
648 warni("* \'jr\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
653 // This jump is in the current 256 byte page
656 warni("* \'jr\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
663 if ((reg2 < -16) || (reg2 > 15))
665 error("relative jump out of range");
669 *locp = (char)(*locp | ((reg2 >> 3) & 0x03));
671 *locp = (char)(*locp | ((reg2 & 0x07) << 5));
675 if ((w & FUMASKRISC) == FU_NUM15)
677 if (eval < -16 || eval > 15)
679 error("constant out of range");
683 *locp = (char)(*locp | ((eval >> 3) & 0x03));
685 *locp = (char)(*locp | ((eval & 0x07) << 5));
689 if ((w & FUMASKRISC) == FU_NUM31)
691 if (eval < 0 || eval > 31)
693 error("constant out of range");
697 *locp = (char)(*locp | ((eval >> 3) & 0x03));
699 *locp = (char)(*locp | ((eval & 0x07) << 5));
703 if ((w & FUMASKRISC) == FU_NUM32)
705 if (eval < 1 || eval > 32)
707 error("constant out of range");
714 eval = (eval == 32) ? 0 : eval;
715 *locp = (char)(*locp | ((eval >> 3) & 0x03));
717 *locp = (char)(*locp | ((eval & 0x07) << 5));
721 if ((w & FUMASKRISC) == FU_REGONE)
723 if (eval < 0 || eval > 31)
725 error("register value out of range");
729 *locp = (char)(*locp | ((eval >> 3) & 0x03));
731 *locp = (char)(*locp | ((eval & 0x07) << 5));
735 if ((w & FUMASKRISC) == FU_REGTWO)
737 if (eval < 0 || eval > 31)
739 error("register value out of range");
744 *locp = (char)(*locp | (eval & 0x1F));
748 if (!(eattr & DEFINED))
755 rmark(sno, loc, 0, w, esym);
760 rmark(sno, loc, tdb, MWORD, NULL);
764 if (eval + 0x10000 >= 0x20000)
769 // Range-check BRA and DBRA
772 if (eval + 0x8000 >= 0x10000)
775 else if (eval >= 0x10000)
780 *locp++ = (char)(eval >> 8);
783 // Fixup LONG forward references;
784 // the long could be unaligned in the section buffer, so be careful
787 if ((w & FUMASKRISC) == FU_MOVEI)
794 for(j=0; j<fwindex; j++)
796 if (fwdjump[j] == address)
798 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
805 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
811 write(err_fd, buf, (LONG)strlen(buf));
818 if (!(eval & 0x0000000F) || ((eval - 2) % 4))
821 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
827 write(err_fd, buf, (LONG)strlen(buf));
833 // Clear this jump as it has been checked
841 // Long constant in MOVEI # is word-swapped, so fix it here
842 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
843 flags = (MLONG | MMOVEI);
848 if (!(eattr & DEFINED))
850 //printf("Fixup (long): Symbol undefined. loc = $%X, long = $%X, flags = $%x\n", loc, eval, flags);
851 rmark(sno, loc, 0, flags, esym);
855 //printf("Fixup (long): TDB = $%X. loc =$%X, long = $%X, flags = $%x\n", tdb, loc, eval, flags);
856 rmark(sno, loc, tdb, flags, NULL);
859 //printf("Fixup (long): TDB = $%X. loc =$%X, long = $%X, flags = $%x\n", tdb, loc, eval, flags);
861 *locp++ = (char)(eval >> 24);
862 *locp++ = (char)(eval >> 16);
863 *locp++ = (char)(eval >> 8);
867 // Fixup a 3-bit "QUICK" reference in bits 9..1
868 // (range of 1..8) in a word. Really bits 1..3 in a byte.
870 if (!(eattr & DEFINED))
872 error("External quick reference");
876 if (eval < 1 || eval > 8)
879 *locp |= (eval & 7) << 1;
882 // Fix up 6502 funny branch
886 if (eval + 0x80 >= 0x100)
893 interror(4); // Bad fixup type
898 error("expression out of range");