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, 2017 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, uint16_t);
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 uint16_t scattr; // Section attributes
33 uint32_t sloc; // Current loc in section
35 CHUNK * scode; // Current (last) code chunk
36 uint32_t challoc; // # bytes alloc'd to code chunk
37 uint32_t ch_size; // # bytes used in code chunk
38 uint8_t * chptr; // Deposit point in code chunk buffer
39 uint8_t * chptr_opcode; // Backup of chptr, updated before entering code generators
41 CHUNK * sfix; // Current (last) fixup chunk
42 uint32_t fchalloc; // # bytes alloc'd to fixup chunk
43 uint32_t fchsize; // # bytes used in fixup chunk
44 PTR fchptr; // Deposit point in fixup chunk buffer
46 // Return a size (SIZB, SIZW, SIZL) or 0, depending on what kind of fixup is
47 // associated with a location.
48 static uint8_t fusiztab[] = {
59 // Offset to REAL fixup location
60 static uint8_t fusizoffs[] = {
73 // Initialize sections; setup initial ABS, TEXT, DATA and BSS sections
75 void InitSection(void)
79 // Cleanup all sections
80 for(i=0; i<NSECTS; i++)
83 // Construct default sections, make TEXT the current section
84 MakeSection(ABS, SUSED | SABS | SBSS); // ABS
85 MakeSection(TEXT, SUSED | TEXT ); // TEXT
86 MakeSection(DATA, SUSED | DATA ); // DATA
87 MakeSection(BSS, SUSED | BSS | SBSS); // BSS
88 // MakeSection(M6502, SUSED | TEXT ); // 6502 code section
90 // Switch to TEXT for starters
96 // Make a new (clean) section
98 void MakeSection(int sno, uint16_t attr)
100 SECT * p = §[sno];
103 p->scode = p->sfcode = NULL;
104 p->sfix = p->sffix = NULL;
109 // Switch to another section (copy section & chunk descriptors to global vars
112 void SwitchSection(int sno)
116 SECT * p = §[sno];
124 // Copy code chunk vars
125 if ((cp = scode) != NULL)
127 challoc = cp->challoc;
128 ch_size = cp->ch_size;
129 chptr = cp->chptr + ch_size;
132 challoc = ch_size = 0;
134 // Copy fixup chunk vars
135 if ((cp = sfix) != NULL)
137 fchalloc = cp->challoc;
138 fchsize = cp->ch_size;
139 fchptr.cp = cp->chptr + fchsize;
142 fchalloc = fchsize = 0;
147 // Save current section
149 void SaveSection(void)
151 SECT * p = §[cursect];
153 p->scattr = scattr; // Bailout section vars
156 if (scode != NULL) // Bailout code chunk
157 scode->ch_size = ch_size;
159 if (sfix != NULL) // Bailout fixup chunk
160 sfix->ch_size = fchsize;
165 // Test to see if a location has a fixup sic'd on it. This is used by the
166 // listing generator to print 'xx's instead of '00's for forward references
168 int fixtest(int sno, uint32_t loc)
172 // Force update to sect[] variables
175 // Hairy, ugly linear search for a mark on our location; the speed doesn't
176 // matter, since this is only done when generating a listing, which is
178 for(CHUNK * ch=sect[sno].sffix; ch!=NULL; ch=ch->chnext)
180 fup.cp = (uint8_t *)ch->chptr;
181 uint8_t * fuend = fup.cp + ch->ch_size;
183 while (fup.cp < fuend)
185 uint16_t w = *fup.wp++;
186 uint32_t 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(uint32_t 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 = ((uint8_t *)cp) + sizeof(CHUNK);
254 scode = p->scode = cp;
260 // This is really wrong. We need to make some proper structures here so we
261 // don't have to count sizes of objects, that's what the compiler's for! :-P
262 #define FIXUP_BASE_SIZE (sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t))
264 // Arrange for a fixup on a location
266 int AddFixup(uint16_t attr, uint32_t loc, TOKEN * fexpr)
272 // Shamus: Expression lengths are voodoo ATM (variable "i"). Need to fix
274 WARNING(!!! AddFixup() is filled with VOODOO !!!)
275 DEBUG printf("FIXUP@$%X: $%X\n", loc, attr);
277 // Compute length of expression (could be faster); determine if it's the
278 // single-symbol case; no expression if it's just a mark. This code assumes
279 // 16 bit WORDs and 32 bit LONGs
280 if (*fexpr == SYMBOL && fexpr[2] == ENDEXPR)
282 // Just a single symbol
283 // SCPCD : correct bit mask for attr (else other FU_xxx will match)
285 if ((attr & FUMASKRISC) == FU_JR)
287 //printf("AddFixup: ((attr & FUMASKRISC) == FU_JR)\n");
289 // i = FIXUP_BASE_SIZE + (sizeof(uint32_t) * 2);
290 i = FIXUP_BASE_SIZE + sizeof(SYM *) + sizeof(uint32_t);
294 //printf("AddFixup: ((attr & FUMASKRISC) == FU_JR) ELSE\n");
296 i = FIXUP_BASE_SIZE + sizeof(SYM *);
301 //printf("AddFixup: !SYMBOL\n");
304 for(len=0; fexpr[len]!=ENDEXPR; len++)
306 if (fexpr[len] == CONST || fexpr[len] == SYMBOL)
310 len++; // Add 1 for ENDEXPR
311 // i = (len << 2) + 12;
312 i = FIXUP_BASE_SIZE + sizeof(uint16_t) + (len * sizeof(TOKEN));
315 // Alloc another fixup chunk for this one to fit in if necessary
316 if ((fchalloc - fchsize) < i)
319 cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
321 // First fixup chunk in section
327 // Add to other chunks
332 sfix->ch_size = fchsize;
335 // Setup fixup chunk and its global vars
337 fchalloc = cp->challoc = CH_FIXUP_SIZE;
338 fchsize = cp->ch_size = 0;
339 fchptr.cp = cp->chptr = ((uint8_t *)cp) + sizeof(CHUNK);
343 // Record fixup type, fixup location, and the file number and line number
344 // the fixup is located at.
347 *fchptr.wp++ = cfileno;
348 *fchptr.wp++ = (uint16_t)curlineno;
350 // Store postfix expression or pointer to a single symbol, or nothing for a
354 *fchptr.wp++ = (uint16_t)len;
357 *fchptr.lp++ = (uint32_t)*fexpr++;
361 *fchptr.sy++ = symbolPtr[fexpr[1]];
362 //printf("AddFixup: adding symbol (%s) [%08X]\n", symbolPtr[fexpr[1]]->sname, symbolPtr[fexpr[1]]->sattr);
365 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
366 if ((attr & FUMASKRISC) == FU_JR)
369 *fchptr.lp++ = orgaddr;
371 *fchptr.lp++ = 0x00000000;
380 // Resolve fixups in a section
382 int ResolveFixups(int sno)
384 PTR fup; // Current fixup
385 uint16_t * fuend; // End of last fixup (in this chunk)
386 uint16_t w; // Fixup word (type+modes+flags)
387 uint8_t * locp; // Location to fix (in cached chunk)
388 uint32_t loc; // Location to fixup
389 VALUE eval; // Expression value
390 uint16_t eattr; // Expression attrib
391 SYM * esym; // External symbol involved in expr
392 SYM * sy; // (Temp) pointer to a symbol
393 uint16_t i; // (Temp) word
394 uint16_t tdb; // eattr & TDB
399 SECT * sc = §[sno];
400 CHUNK * ch = sc->sffix;
405 // "Cache" first chunk
406 CHUNK * cch = sc->sfcode;
408 // Can't fixup a section with nothing in it
414 fup.cp = ch->chptr; // fup -> start of chunk
415 fuend = (uint16_t *)(fup.cp + ch->ch_size); // fuend -> end of chunk
417 while (fup.wp < fuend)
422 curlineno = (int)*fup.wp++;
423 DEBUG { printf("ResolveFixups: cfileno=%u\n", cfileno); }
425 // This is based on global vars cfileno, curfname :-P
426 // This approach is kinda meh as well. I think we can do better
428 SetFilenameForErrorReporting();
432 // Search for chunk containing location to fix up; compute a
433 // pointer to the location (in the chunk). Often we will find the
434 // Fixup is in the "cached" chunk, so the linear-search is seldom
436 if (loc < cch->chloc || loc >= (cch->chloc + cch->ch_size))
438 for(cch=sc->sfcode; cch!=NULL; cch=cch->chnext)
440 if (loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
446 // Fixup (loc) out of range
452 locp = cch->chptr + (loc - cch->chloc);
455 // Compute expression/symbol value and attribs
457 // Complex expression
462 if (evexpr(fup.tk, &eval, &eattr, &esym) != OK)
481 // If the symbol is not defined, but global, set esym to sy
482 if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
486 tdb = (uint16_t)(eattr & TDB);
488 // If the expression is undefined and no external symbol is
489 // involved, then that's an error.
490 if (!(eattr & DEFINED) && (esym == NULL))
498 // If a PC-relative fixup is undefined, its value is *not*
499 // subtracted from the location (that will happen in the linker
500 // when the external reference is resolved).
502 // MWC expects PC-relative things to have the LOC subtracted from
503 // the value, if the value is external (that is, undefined at this
506 // PC-relative fixups must be DEFINED and either in the same
507 // section (whereupon the subtraction takes place) or ABS (with no
517 error("PC-relative expr across sections");
521 if (sbra_flag && (w & FU_LBRA) && (eval + 0x80 < 0x100))
522 warn("unoptimized short branch");
524 else if (obj_format == MWC)
532 switch ((int)(w & FUMASK))
534 // FU_BBRA fixes up a one-byte branch offset.
536 if (!(eattr & DEFINED))
538 error("external short branch");
544 if (eval + 0x80 >= 0x100)
549 error("illegal bra.s with zero offset");
553 *++locp = (uint8_t)eval;
555 // Fixup one-byte value at locp + 1.
559 // Fixup one-byte forward references
561 if (!(eattr & DEFINED))
563 error("external byte reference");
569 error("non-absolute byte reference");
573 if ((w & FU_PCREL) && eval + 0x80 >= 0x100)
578 if (eval + 0x100 >= 0x200)
581 else if (eval >= 0x100)
584 *locp = (uint8_t)eval;
586 // Fixup WORD forward references;
587 // the word could be unaligned in the section buffer, so we have to
590 if ((w & FUMASKRISC) == FU_JR)
595 reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
597 reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
599 if ((reg2 < -16) || (reg2 > 15))
601 error("relative jump out of range");
605 *locp = (uint8_t)(*locp | ((reg2 >> 3) & 0x03));
607 *locp = (uint8_t)(*locp | ((reg2 & 0x07) << 5));
611 if ((w & FUMASKRISC) == FU_NUM15)
613 if (eval < -16 || eval > 15)
615 error("constant out of range");
619 *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
621 *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
625 if ((w & FUMASKRISC) == FU_NUM31)
627 if (eval < 0 || eval > 31)
629 error("constant out of range");
633 *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
635 *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
639 if ((w & FUMASKRISC) == FU_NUM32)
641 if (eval < 1 || eval > 32)
643 error("constant out of range");
650 eval = (eval == 32) ? 0 : eval;
651 *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
653 *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
657 if ((w & FUMASKRISC) == FU_REGONE)
659 if (eval < 0 || eval > 31)
661 error("register value out of range");
665 *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
667 *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
671 if ((w & FUMASKRISC) == FU_REGTWO)
673 if (eval < 0 || eval > 31)
675 error("register value out of range");
680 *locp = (uint8_t)(*locp | (eval & 0x1F));
684 if (!(eattr & DEFINED))
691 MarkRelocatable(sno, loc, 0, flags, esym);
696 MarkRelocatable(sno, loc, tdb, MWORD, NULL);
700 if (eval + 0x10000 >= 0x20000)
705 // Range-check BRA and DBRA
708 if (eval + 0x8000 >= 0x10000)
711 else if (eval >= 0x10000)
716 SETBE16(locp, 0, eval);
718 // Fixup LONG forward references;
719 // the long could be unaligned in the section buffer, so be careful
724 if ((w & FUMASKRISC) == FU_MOVEI)
726 // Long constant in MOVEI # is word-swapped, so fix it here
727 eval = WORDSWAP32(eval);
731 // If the symbol is undefined, make sure to pass the symbol in
732 // to the MarkRelocatable() function.
733 if (!(eattr & DEFINED))
734 MarkRelocatable(sno, loc, 0, flags, esym);
736 MarkRelocatable(sno, loc, tdb, flags, NULL);
738 SETBE32(locp, 0, eval);
741 // Fixup a 3-bit "QUICK" reference in bits 9..1
742 // (range of 1..8) in a word. Really bits 1..3 in a byte.
744 if (!(eattr & DEFINED))
746 error("External quick reference");
750 if (eval < 1 || eval > 8)
753 *locp |= (eval & 7) << 1;
756 // Fix up 6502 funny branch
760 if (eval + 0x80 >= 0x100)
763 *locp = (uint8_t)eval;
767 // Bad fixup type--this should *never* happen!
773 error("expression out of range");
784 // Resolve all fixups
786 int ResolveAllFixups(void)
788 // Make undefined symbols GLOBL
790 ForceUndefinedSymbolsGlobal();
792 DEBUG printf("Resolving TEXT sections...\n");
794 DEBUG printf("Resolving DATA sections...\n");