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
22 // Function prototypes
23 void MakeSection(int, uint16_t);
24 void SwitchSection(int);
26 // Section descriptors
27 SECT sect[NSECTS]; // All sections...
28 int cursect; // Current section number
30 // These are copied from the section descriptor, the current code chunk
31 // descriptor and the current fixup chunk descriptor when a switch is made into
32 // a section. They are copied back to the descriptors when the section is left.
33 uint16_t scattr; // Section attributes
34 uint32_t sloc; // Current loc in section
36 CHUNK * scode; // Current (last) code chunk
37 uint32_t challoc; // # bytes alloc'd to code chunk
38 uint32_t ch_size; // # bytes used in code chunk
39 uint8_t * chptr; // Deposit point in code chunk buffer
40 uint8_t * chptr_opcode; // Backup of chptr, updated before entering code generators
42 CHUNK * sfix; // Current (last) fixup chunk
43 uint32_t fchalloc; // # bytes alloc'd to fixup chunk
44 uint32_t fchsize; // # bytes used in fixup chunk
45 PTR fchptr; // Deposit point in fixup chunk buffer
47 // Return a size (SIZB, SIZW, SIZL) or 0, depending on what kind of fixup is
48 // associated with a location.
49 static uint8_t fusiztab[] = {
60 // Offset to REAL fixup location
61 static uint8_t fusizoffs[] = {
74 // Initialize sections; setup initial ABS, TEXT, DATA and BSS sections
76 void InitSection(void)
80 // Cleanup all sections
81 for(i=0; i<NSECTS; i++)
84 // Construct default sections, make TEXT the current section
85 MakeSection(ABS, SUSED | SABS | SBSS); // ABS
86 MakeSection(TEXT, SUSED | TEXT ); // TEXT
87 MakeSection(DATA, SUSED | DATA ); // DATA
88 MakeSection(BSS, SUSED | BSS | SBSS); // BSS
89 MakeSection(M6502, SUSED | TEXT ); // 6502 code section
91 // Switch to TEXT for starters
97 // Make a new (clean) section
99 void MakeSection(int sno, uint16_t attr)
101 SECT * p = §[sno];
104 p->scode = p->sfcode = NULL;
105 p->sfix = p->sffix = NULL;
110 // Switch to another section (copy section & chunk descriptors to global vars
113 void SwitchSection(int sno)
117 SECT * p = §[sno];
119 m6502 = (sno == M6502); // Set 6502-mode flag
127 // Copy code chunk vars
128 if ((cp = scode) != NULL)
130 challoc = cp->challoc;
131 ch_size = cp->ch_size;
132 chptr = cp->chptr + ch_size;
134 // For 6502 mode, add the last org'd address
136 chptr = cp->chptr + orgaddr;
139 challoc = ch_size = 0;
141 // Copy fixup chunk vars
142 if ((cp = sfix) != NULL)
144 fchalloc = cp->challoc;
145 fchsize = cp->ch_size;
146 fchptr.cp = cp->chptr + fchsize;
149 fchalloc = fchsize = 0;
154 // Save current section
156 void SaveSection(void)
158 SECT * p = §[cursect];
160 p->scattr = scattr; // Bailout section vars
163 if (scode != NULL) // Bailout code chunk
164 scode->ch_size = ch_size;
166 if (sfix != NULL) // Bailout fixup chunk
167 sfix->ch_size = fchsize;
172 // Test to see if a location has a fixup sic'd on it. This is used by the
173 // listing generator to print 'xx's instead of '00's for forward references
175 int fixtest(int sno, uint32_t loc)
179 // Force update to sect[] variables
182 // Hairy, ugly linear search for a mark on our location; the speed doesn't
183 // matter, since this is only done when generating a listing, which is
185 for(CHUNK * ch=sect[sno].sffix; ch!=NULL; ch=ch->chnext)
187 fup.cp = (uint8_t *)ch->chptr;
188 uint8_t * fuend = fup.cp + ch->ch_size;
190 while (fup.cp < fuend)
192 uint16_t w = *fup.wp++;
193 uint32_t xloc = *fup.lp++ + (int)fusizoffs[w & FUMASK];
197 return (int)fusiztab[w & FUMASK];
214 // Check that there are at least 'amt' bytes left in the current chunk. If
215 // there are not, allocate another chunk of at least 'amt' bytes (and probably
218 // If 'amt' is zero, ensure there are at least CH_THRESHOLD bytes, likewise.
220 int chcheck(uint32_t amt)
222 DEBUG { printf("chcheck(%u)\n", amt); }
223 // If in BSS section, no allocation required
230 DEBUG { printf(" challoc=%i, ch_size=%i, diff=%i\n", challoc, ch_size, challoc-ch_size); }
231 if ((int)(challoc - ch_size) >= (int)amt)
234 if (amt < CH_CODE_SIZE)
237 DEBUG { printf(" amt (adjusted)=%u\n", amt); }
238 SECT * p = §[cursect];
239 CHUNK * cp = malloc(sizeof(CHUNK) + amt);
241 // First chunk in section
247 // Add chunk to other chunks
252 scode->ch_size = ch_size; // Save old chunk's globals
255 // Setup chunk and global vars
258 challoc = cp->challoc = amt;
259 ch_size = cp->ch_size = 0;
260 chptr = cp->chptr = ((uint8_t *)cp) + sizeof(CHUNK);
261 scode = p->scode = cp;
267 // This is really wrong. We need to make some proper structures here so we
268 // don't have to count sizes of objects, that's what the compiler's for! :-P
269 #define FIXUP_BASE_SIZE (sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t))
271 // Arrange for a fixup on a location
273 int AddFixup(uint16_t attr, uint32_t loc, TOKEN * fexpr)
279 // Shamus: Expression lengths are voodoo ATM (variable "i"). Need to fix
281 WARNING(!!! AddFixup() is filled with VOODOO !!!)
282 DEBUG printf("FIXUP@$%X: $%X\n", loc, attr);
284 // Compute length of expression (could be faster); determine if it's the
285 // single-symbol case; no expression if it's just a mark. This code assumes
286 // 16 bit WORDs and 32 bit LONGs
287 if (*fexpr == SYMBOL && fexpr[2] == ENDEXPR)
289 // Just a single symbol
290 // SCPCD : correct bit mask for attr (else other FU_xxx will match)
292 if ((attr & FUMASKRISC) == FU_JR)
294 //printf("AddFixup: ((attr & FUMASKRISC) == FU_JR)\n");
296 // i = FIXUP_BASE_SIZE + (sizeof(uint32_t) * 2);
297 i = FIXUP_BASE_SIZE + sizeof(SYM *) + sizeof(uint32_t);
301 //printf("AddFixup: ((attr & FUMASKRISC) == FU_JR) ELSE\n");
303 i = FIXUP_BASE_SIZE + sizeof(SYM *);
308 //printf("AddFixup: !SYMBOL\n");
311 for(len=0; fexpr[len]!=ENDEXPR; len++)
313 if (fexpr[len] == CONST || fexpr[len] == SYMBOL)
317 len++; // Add 1 for ENDEXPR
318 // i = (len << 2) + 12;
319 i = FIXUP_BASE_SIZE + sizeof(uint16_t) + (len * sizeof(TOKEN));
322 // Alloc another fixup chunk for this one to fit in if necessary
323 if ((fchalloc - fchsize) < i)
326 cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
328 // First fixup chunk in section
334 // Add to other chunks
339 sfix->ch_size = fchsize;
342 // Setup fixup chunk and its global vars
344 fchalloc = cp->challoc = CH_FIXUP_SIZE;
345 fchsize = cp->ch_size = 0;
346 fchptr.cp = cp->chptr = ((uint8_t *)cp) + sizeof(CHUNK);
350 // Record fixup type, fixup location, and the file number and line number
351 // the fixup is located at.
354 *fchptr.wp++ = cfileno;
355 *fchptr.wp++ = (uint16_t)curlineno;
357 // Store postfix expression or pointer to a single symbol, or nothing for a
361 *fchptr.wp++ = (uint16_t)len;
364 *fchptr.lp++ = (uint32_t)*fexpr++;
368 *fchptr.sy++ = symbolPtr[fexpr[1]];
369 //printf("AddFixup: adding symbol (%s) [%08X]\n", symbolPtr[fexpr[1]]->sname, symbolPtr[fexpr[1]]->sattr);
372 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
373 if ((attr & FUMASKRISC) == FU_JR)
376 *fchptr.lp++ = orgaddr;
378 *fchptr.lp++ = 0x00000000;
387 // Resolve fixups in a section
389 int ResolveFixups(int sno)
391 PTR fup; // Current fixup
392 uint16_t * fuend; // End of last fixup (in this chunk)
393 uint16_t w; // Fixup word (type+modes+flags)
394 uint8_t * locp; // Location to fix (in cached chunk)
395 uint32_t loc; // Location to fixup
396 VALUE eval; // Expression value
397 uint16_t eattr; // Expression attrib
398 SYM * esym; // External symbol involved in expr
399 SYM * sy; // (Temp) pointer to a symbol
400 uint16_t i; // (Temp) word
401 uint16_t tdb; // eattr & TDB
406 SECT * sc = §[sno];
407 CHUNK * ch = sc->sffix;
412 // "Cache" first chunk
413 CHUNK * cch = sc->sfcode;
415 // Can't fixup a section with nothing in it
420 * Wire the 6502 segment's size to its allocated size (64K)
423 cch->ch_size = cch->challoc;
427 fup.cp = ch->chptr; // fup -> start of chunk
428 fuend = (uint16_t *)(fup.cp + ch->ch_size); // fuend -> end of chunk
430 while (fup.wp < fuend)
435 curlineno = (int)*fup.wp++;
436 DEBUG { printf("ResolveFixups: cfileno=%u\n", cfileno); }
438 // This is based on global vars cfileno, curfname :-P
439 // This approach is kinda meh as well. I think we can do better
441 SetFilenameForErrorReporting();
445 // Search for chunk containing location to fix up; compute a
446 // pointer to the location (in the chunk). Often we will find the
447 // Fixup is in the "cached" chunk, so the linear-search is seldom
449 if (loc < cch->chloc || loc >= (cch->chloc + cch->ch_size))
451 for(cch=sc->sfcode; cch!=NULL; cch=cch->chnext)
453 if (loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
459 // Fixup (loc) out of range
465 locp = cch->chptr + (loc - cch->chloc);
468 // Compute expression/symbol value and attribs
470 // Complex expression
475 if (evexpr(fup.tk, &eval, &eattr, &esym) != OK)
494 // If the symbol is not defined, but global, set esym to sy
495 if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
499 tdb = (uint16_t)(eattr & TDB);
501 // If the expression is undefined and no external symbol is
502 // involved, then that's an error.
503 if (!(eattr & DEFINED) && (esym == NULL))
511 // If a PC-relative fixup is undefined, its value is *not*
512 // subtracted from the location (that will happen in the linker
513 // when the external reference is resolved).
515 // MWC expects PC-relative things to have the LOC subtracted from
516 // the value, if the value is external (that is, undefined at this
519 // PC-relative fixups must be DEFINED and either in the same
520 // section (whereupon the subtraction takes place) or ABS (with no
530 error("PC-relative expr across sections");
534 if (sbra_flag && (w & FU_LBRA) && (eval + 0x80 < 0x100))
535 warn("unoptimized short branch");
537 else if (obj_format == MWC)
545 switch ((int)(w & FUMASK))
547 // FU_BBRA fixes up a one-byte branch offset.
549 if (!(eattr & DEFINED))
551 error("external short branch");
557 if (eval + 0x80 >= 0x100)
562 error("illegal bra.s with zero offset");
566 *++locp = (uint8_t)eval;
568 // Fixup one-byte value at locp + 1.
572 // Fixup one-byte forward references
574 if (!(eattr & DEFINED))
576 error("external byte reference");
582 error("non-absolute byte reference");
586 if ((w & FU_PCREL) && eval + 0x80 >= 0x100)
591 if (eval + 0x100 >= 0x200)
594 else if (eval >= 0x100)
597 *locp = (uint8_t)eval;
599 // Fixup high/low byte off word for 6502
601 if (!(eattr & DEFINED))
603 error("external byte reference");
607 *locp = (uint8_t)((eval >> 8) & 0xFF);
610 if (!(eattr & DEFINED))
612 error("external byte reference");
616 *locp = (uint8_t)(eval & 0xFF);
618 // Fixup WORD forward references;
619 // the word could be unaligned in the section buffer, so we have to
622 if ((w & FUMASKRISC) == FU_JR)
627 reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
629 reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
631 if ((reg2 < -16) || (reg2 > 15))
633 error("relative jump out of range");
637 *locp = (uint8_t)(*locp | ((reg2 >> 3) & 0x03));
639 *locp = (uint8_t)(*locp | ((reg2 & 0x07) << 5));
643 if ((w & FUMASKRISC) == FU_NUM15)
645 if (eval < -16 || eval > 15)
647 error("constant out of range");
651 *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
653 *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
657 if ((w & FUMASKRISC) == FU_NUM31)
659 if (eval < 0 || eval > 31)
661 error("constant out of range");
665 *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
667 *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
671 if ((w & FUMASKRISC) == FU_NUM32)
673 if (eval < 1 || eval > 32)
675 error("constant out of range");
682 eval = (eval == 32) ? 0 : eval;
683 *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
685 *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
689 if ((w & FUMASKRISC) == FU_REGONE)
691 if (eval < 0 || eval > 31)
693 error("register value out of range");
697 *locp = (uint8_t)(*locp | ((eval >> 3) & 0x03));
699 *locp = (uint8_t)(*locp | ((eval & 0x07) << 5));
703 if ((w & FUMASKRISC) == FU_REGTWO)
705 if (eval < 0 || eval > 31)
707 error("register value out of range");
712 *locp = (uint8_t)(*locp | (eval & 0x1F));
716 if (!(eattr & DEFINED))
723 MarkRelocatable(sno, loc, 0, flags, esym);
728 MarkRelocatable(sno, loc, tdb, MWORD, NULL);
732 if (eval + 0x10000 >= 0x20000)
737 // Range-check BRA and DBRA
740 if (eval + 0x8000 >= 0x10000)
743 else if (eval >= 0x10000)
748 // 6502 words are little endian, so handle that here
750 SETLE16(locp, 0, eval)
752 SETBE16(locp, 0, eval)
755 // Fixup LONG forward references;
756 // the long could be unaligned in the section buffer, so be careful
761 if ((w & FUMASKRISC) == FU_MOVEI)
763 // Long constant in MOVEI # is word-swapped, so fix it here
764 eval = WORDSWAP32(eval);
768 // If the symbol is undefined, make sure to pass the symbol in
769 // to the MarkRelocatable() function.
770 if (!(eattr & DEFINED))
771 MarkRelocatable(sno, loc, 0, flags, esym);
773 MarkRelocatable(sno, loc, tdb, flags, NULL);
775 SETBE32(locp, 0, eval);
778 // Fixup a 3-bit "QUICK" reference in bits 9..1
779 // (range of 1..8) in a word. Really bits 1..3 in a byte.
781 if (!(eattr & DEFINED))
783 error("External quick reference");
787 if (eval < 1 || eval > 8)
790 *locp |= (eval & 7) << 1;
793 // Fix up 6502 funny branch
797 if (eval + 0x80 >= 0x100)
800 *locp = (uint8_t)eval;
804 // Bad fixup type--this should *never* happen!
810 error("expression out of range");
821 // Resolve all fixups
823 int ResolveAllFixups(void)
825 // Make undefined symbols GLOBL
827 ForceUndefinedSymbolsGlobal();
829 DEBUG printf("Resolving TEXT sections...\n");
831 DEBUG printf("Resolving DATA sections...\n");
833 DEBUG printf("Resolving 6502 sections...\n");
834 ResolveFixups(M6502); /* fixup 6502 section (if any) */