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
39 char * chptr_opcode; // Backup of chptr, updated before entering code generators
41 CHUNK * sfix; // Current (last) fixup chunk
42 LONG fchalloc; // # bytes alloc'd to fixup chunk
43 LONG 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 char fusiztab[] = {
59 // Offset to REAL fixup location
60 static char 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, WORD 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, LONG loc)
176 // Force update to sect[] variables
179 // Hairy, ugly linear search for a mark on our location; the speed doesn't
180 // matter, since this is only done when generating a listing, which is
182 for(ch=sect[sno].sffix; ch!=NULL; ch=ch->chnext)
184 fup.cp = (char *)ch->chptr;
185 fuend = fup.cp + ch->ch_size;
187 while (fup.cp < fuend)
190 xloc = *fup.lp++ + (int)fusizoffs[w & FUMASK];
194 return (int)fusiztab[w & FUMASK];
211 // Check that there are at least 'amt' bytes left in the current chunk. If
212 // there are not, allocate another chunk of at least 'amt' bytes (and probably
215 // If 'amt' is zero, ensure there are at least CH_THRESHOLD bytes, likewise.
217 int chcheck(LONG amt)
219 DEBUG { printf("chcheck(%u)\n", amt); }
220 // If in BSS section, no allocation required
227 DEBUG { printf(" challoc=%i, ch_size=%i, diff=%i\n", challoc, ch_size, challoc-ch_size); }
228 if ((int)(challoc - ch_size) >= (int)amt)
231 if (amt < CH_CODE_SIZE)
234 DEBUG { printf(" amt (adjusted)=%u\n", amt); }
235 SECT * p = §[cursect];
236 CHUNK * cp = malloc(sizeof(CHUNK) + amt);
238 // First chunk in section
244 // Add chunk to other chunks
249 scode->ch_size = ch_size; // Save old chunk's globals
252 // Setup chunk and global vars
255 challoc = cp->challoc = amt;
256 ch_size = cp->ch_size = 0;
257 chptr = cp->chptr = ((char *)cp) + sizeof(CHUNK);
258 scode = p->scode = cp;
264 // This is really wrong. We need to make some proper structures here so we
265 // don't have to count sizes of objects, that's what the compiler's for! :-P
266 #define FIXUP_BASE_SIZE (sizeof(WORD) + sizeof(LONG) + sizeof(WORD) + sizeof(WORD))
268 // Arrange for a fixup on a location
270 int AddFixup(WORD attr, LONG loc, TOKEN * fexpr)
276 // Shamus: Expression lengths are voodoo ATM (variable "i"). Need to fix
278 WARNING(!!! AddFixup() is filled with VOODOO !!!)
279 DEBUG printf("FIXUP@$%X: $%X\n", loc, attr);
281 // Compute length of expression (could be faster); determine if it's the
282 // single-symbol case; no expression if it's just a mark. This code assumes
283 // 16 bit WORDs and 32 bit LONGs
284 if (*fexpr == SYMBOL && fexpr[2] == ENDEXPR)
286 // Just a single symbol
287 // SCPCD : correct bit mask for attr (else other FU_xxx will match)
289 if ((attr & FUMASKRISC) == FU_JR)
291 //printf("AddFixup: ((attr & FUMASKRISC) == FU_JR)\n");
293 // i = FIXUP_BASE_SIZE + (sizeof(LONG) * 2);
294 i = FIXUP_BASE_SIZE + sizeof(SYM *) + sizeof(LONG);
298 //printf("AddFixup: ((attr & FUMASKRISC) == FU_JR) ELSE\n");
300 i = FIXUP_BASE_SIZE + sizeof(SYM *);
305 //printf("AddFixup: !SYMBOL\n");
308 for(len=0; fexpr[len]!=ENDEXPR; len++)
310 if (fexpr[len] == CONST || fexpr[len] == SYMBOL)
314 len++; // Add 1 for ENDEXPR
315 // i = (len << 2) + 12;
316 i = FIXUP_BASE_SIZE + sizeof(WORD) + (len * sizeof(TOKEN));
319 // Alloc another fixup chunk for this one to fit in if necessary
320 if ((fchalloc - fchsize) < i)
323 cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
325 // First fixup chunk in section
331 // Add to other chunks
336 sfix->ch_size = fchsize;
339 // Setup fixup chunk and its global vars
341 fchalloc = cp->challoc = CH_FIXUP_SIZE;
342 fchsize = cp->ch_size = 0;
343 fchptr.cp = cp->chptr = ((char *)cp) + sizeof(CHUNK);
347 // Record fixup type, fixup location, and the file number and line number
348 // the fixup is located at.
351 *fchptr.wp++ = cfileno;
352 *fchptr.wp++ = (WORD)curlineno;
354 // Store postfix expression or pointer to a single symbol, or nothing for a
358 *fchptr.wp++ = (WORD)len;
361 *fchptr.lp++ = (LONG)*fexpr++;
365 // *fchptr.lp++ = (LONG)fexpr[1];
366 *fchptr.sy++ = symbolPtr[fexpr[1]];
367 //printf("AddFixup: adding symbol (%s) [%08X]\n", symbolPtr[fexpr[1]]->sname, symbolPtr[fexpr[1]]->sattr);
370 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
371 if ((attr & FUMASKRISC) == FU_JR)
374 *fchptr.lp++ = orgaddr;
376 *fchptr.lp++ = 0x00000000;
385 // Resolve fixups in a section
387 int ResolveFixups(int sno)
389 PTR fup; // Current fixup
390 WORD * fuend; // End of last fixup (in this chunk)
391 WORD w; // Fixup word (type+modes+flags)
392 char * locp; // Location to fix (in cached chunk)
393 LONG loc; // Location to fixup
394 VALUE eval; // Expression value
395 WORD eattr; // Expression attrib
396 SYM * esym; // External symbol involved in expr
397 SYM * sy; // (Temp) pointer to a symbol
398 WORD i; // (Temp) word
399 WORD tdb; // eattr & TDB
403 unsigned page_jump = 0;
404 unsigned address = 0;
408 SECT * sc = §[sno];
409 CHUNK * ch = sc->sffix;
414 // "Cache" first chunk
415 CHUNK * cch = sc->sfcode;
417 // Can't fixup a section with nothing in it
423 fup.cp = ch->chptr; // fup -> start of chunk
424 fuend = (WORD *)(fup.cp + ch->ch_size); // fuend -> end of chunk
426 while (fup.wp < fuend)
431 curlineno = (int)*fup.wp++;
432 DEBUG { printf("ResolveFixups: cfileno=%u\n", cfileno); }
433 // This is based on global vars cfileno, curfname :-P
434 // This approach is kinda meh as well. I think we can do better than this.
435 SetFilenameForErrorReporting();
439 // Search for chunk containing location to fix up; compute a
440 // pointer to the location (in the chunk). Often we will find the
441 // fixup is in the "cached" chunk, so the linear-search is seldom
443 if (loc < cch->chloc || loc >= (cch->chloc + cch->ch_size))
445 for(cch=sc->sfcode; cch!=NULL; cch=cch->chnext)
447 if (loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
453 // Fixup (loc) out of range
459 locp = cch->chptr + (loc - cch->chloc);
462 // Compute expression/symbol value and attribs
463 // Complex expression
468 if (evexpr(fup.tk, &eval, &eattr, &esym) != OK)
487 if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
491 tdb = (WORD)(eattr & TDB);
493 // If the expression is undefined and no external symbol is
494 // involved, then it's an error.
495 if (!(eattr & DEFINED) && (esym == NULL))
501 // It seems that this is completely unnecessary!
503 if (((w & FUMASKRISC) == FU_MOVEI) && esym)
505 //printf("DoFixups: Setting symbol attre to RISCSYM...\n");
506 esym->sattre |= RISCSYM;
512 // If a PC-relative fixup is undefined, its value is *not*
513 // subtracted from the location (that will happen in the linker
514 // when the external reference is resolved).
516 // MWC expects PC-relative things to have the LOC subtracted from
517 // the value, if the value is external (that is, undefined at this
520 // PC-relative fixups must be DEFINED and either in the same
521 // section (whereupon the subtraction takes place) or ABS (with no
531 error("PC-relative expr across sections");
535 if (sbra_flag && (w & FU_LBRA) && (eval + 0x80 < 0x100))
536 warn("unoptimized short branch");
538 else if (obj_format == MWC)
546 switch ((int)(w & FUMASK))
548 // FU_BBRA fixes up a one-byte branch offset.
550 if (!(eattr & DEFINED))
552 error("external short branch");
558 if (eval + 0x80 >= 0x100)
563 error("illegal bra.s with zero offset");
567 *++locp = (char)eval;
569 // Fixup one-byte value at locp + 1.
573 // Fixup one-byte forward references
575 if (!(eattr & DEFINED))
577 error("external byte reference");
583 error("non-absolute byte reference");
587 if ((w & FU_PCREL) && eval + 0x80 >= 0x100)
592 if (eval + 0x100 >= 0x200)
595 else if (eval >= 0x100)
600 // Fixup WORD forward references;
601 // the word could be unaligned in the section buffer, so we have to
604 if ((w & FUMASKRISC) == FU_JR)// || ((w & 0x0F00) == FU_MJR))
609 reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
611 reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
613 if ((reg2 < -16) || (reg2 > 15))
615 error("relative jump out of range");
619 *locp = (char)(*locp | ((reg2 >> 3) & 0x03));
621 *locp = (char)(*locp | ((reg2 & 0x07) << 5));
625 if ((w & FUMASKRISC) == FU_NUM15)
627 if (eval < -16 || eval > 15)
629 error("constant out of range");
633 *locp = (char)(*locp | ((eval >> 3) & 0x03));
635 *locp = (char)(*locp | ((eval & 0x07) << 5));
639 if ((w & FUMASKRISC) == FU_NUM31)
641 if (eval < 0 || eval > 31)
643 error("constant out of range");
647 *locp = (char)(*locp | ((eval >> 3) & 0x03));
649 *locp = (char)(*locp | ((eval & 0x07) << 5));
653 if ((w & FUMASKRISC) == FU_NUM32)
655 if (eval < 1 || eval > 32)
657 error("constant out of range");
664 eval = (eval == 32) ? 0 : eval;
665 *locp = (char)(*locp | ((eval >> 3) & 0x03));
667 *locp = (char)(*locp | ((eval & 0x07) << 5));
671 if ((w & FUMASKRISC) == FU_REGONE)
673 if (eval < 0 || eval > 31)
675 error("register value out of range");
679 *locp = (char)(*locp | ((eval >> 3) & 0x03));
681 *locp = (char)(*locp | ((eval & 0x07) << 5));
685 if ((w & FUMASKRISC) == FU_REGTWO)
687 if (eval < 0 || eval > 31)
689 error("register value out of range");
694 *locp = (char)(*locp | (eval & 0x1F));
698 if (!(eattr & DEFINED))
705 rmark(sno, loc, 0, w, esym);
710 rmark(sno, loc, tdb, MWORD, NULL);
714 if (eval + 0x10000 >= 0x20000)
719 // Range-check BRA and DBRA
722 if (eval + 0x8000 >= 0x10000)
725 else if (eval >= 0x10000)
730 *locp++ = (char)(eval >> 8);
733 // Fixup LONG forward references;
734 // the long could be unaligned in the section buffer, so be careful
737 if ((w & FUMASKRISC) == FU_MOVEI)
739 // Long constant in MOVEI # is word-swapped, so fix it here
740 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
741 flags = (MLONG | MMOVEI);
746 if (!(eattr & DEFINED))
748 //printf("Fixup (long): Symbol undefined. loc = $%X, long = $%X, flags = $%x\n", loc, eval, flags);
749 rmark(sno, loc, 0, flags, esym);
753 //printf("Fixup (long): TDB = $%X. loc =$%X, long = $%X, flags = $%x\n", tdb, loc, eval, flags);
754 rmark(sno, loc, tdb, flags, NULL);
757 //printf("Fixup (long): TDB = $%X. loc =$%X, long = $%X, flags = $%x\n", tdb, loc, eval, flags);
759 *locp++ = (char)(eval >> 24);
760 *locp++ = (char)(eval >> 16);
761 *locp++ = (char)(eval >> 8);
765 // Fixup a 3-bit "QUICK" reference in bits 9..1
766 // (range of 1..8) in a word. Really bits 1..3 in a byte.
768 if (!(eattr & DEFINED))
770 error("External quick reference");
774 if (eval < 1 || eval > 8)
777 *locp |= (eval & 7) << 1;
780 // Fix up 6502 funny branch
784 if (eval + 0x80 >= 0x100)
791 // Bad fixup type--this should *never* happen!
797 error("expression out of range");
808 // Resolve all fixups
810 int ResolveAllFixups(void)
815 // Make undefined symbols GLOBL
817 ForceUndefinedSymbolsGlobal();
819 DEBUG printf("Resolving TEXT sections...\n");
821 DEBUG printf("Resolving DATA sections...\n");