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
20 // Section descriptors
21 SECT sect[NSECTS]; // All sections...
22 int cursect; // Current section number
24 // These are copied from the section descriptor, the current code chunk
25 // descriptor and the current fixup chunk descriptor when a switch is made into
26 // a section. They are copied back to the descriptors when the section is left.
27 WORD scattr; // Section attributes
28 LONG sloc; // Current loc in section
30 CHUNK * scode; // Current (last) code chunk
31 LONG challoc; // # bytes alloc'd to code chunk
32 LONG ch_size; // # bytes used in code chunk
33 char * chptr; // Deposit point in code chunk buffer
35 CHUNK * sfix; // Current (last) fixup chunk
36 LONG fchalloc; // # bytes alloc'd to fixup chunk
37 LONG fchsize; // # bytes used in fixup chunk
38 PTR fchptr; // Deposit point in fixup chunk buffer
40 unsigned fwdjump[MAXFWDJUMPS]; // forward jump check table
41 unsigned fwindex = 0; // forward jump index
43 // Return a size (SIZB, SIZW, SIZL) or 0, depending on what kind of fixup is
44 // associated with a location.
45 static char fusiztab[] = {
56 // Offset to REAL fixup location
57 static char fusizoffs[] = {
70 // Make a New (Clean) Section
72 void mksect(int sno, WORD attr)
74 SECT * p = §[sno];
77 p->scode = p->sfcode = NULL;
78 p->sfix = p->sffix = NULL;
83 // Switch to Another Section (Copy Section & Chunk Descriptors to Global Vars
86 void switchsect(int sno)
88 CHUNK * cp; // Chunk pointer
90 SECT * p = §[sno];
92 scattr = p->scattr; // Copy section vars
97 // Copy code chunk vars
98 if ((cp = scode) != NULL)
100 challoc = cp->challoc;
101 ch_size = cp->ch_size;
102 chptr = cp->chptr + ch_size;
105 challoc = ch_size = 0;
107 // Copy fixup chunk vars
108 if ((cp = sfix) != NULL)
110 fchalloc = cp->challoc;
111 fchsize = cp->ch_size;
112 fchptr.cp = cp->chptr + fchsize;
115 fchalloc = fchsize = 0;
120 // Save Current Section
124 SECT * p = §[cursect];
126 p->scattr = scattr; // Bailout section vars
129 if (scode != NULL) // Bailout code chunk
130 scode->ch_size = ch_size;
132 if (sfix != NULL) // Bailout fixup chunk
133 sfix->ch_size = fchsize;
138 // Initialize Sections; Setup initial ABS, TEXT, DATA and BSS sections
144 // Cleanup all sections
145 for(i=0; i<NSECTS; i++)
148 // Construct default sections, make TEXT the current section
149 mksect(ABS, SUSED | SABS | SBSS); // ABS
150 mksect(TEXT, SUSED | TEXT ); // TEXT
151 mksect(DATA, SUSED | DATA ); // DATA
152 mksect(BSS, SUSED | BSS | SBSS ); // BSS
153 // mksect(M6502, SUSED | TEXT ); // 6502 code section
155 switchsect(TEXT); // Switch to TEXT for starters
160 // Test to see if a location has a fixup sic'd on it. This is used by the
161 // listing generator to print 'xx's instead of '00's for forward references
163 int fixtest(int sno, LONG loc)
171 stopmark(); // Force update to sect[] variables
173 // Hairy, ugly linear search for a mark on our location;
174 // the speed doesn't matter, since this is only done when generating a
175 // listing, which is SLOW.
176 for(ch=sect[sno].sffix; ch!=NULL; ch=ch->chnext)
178 fup.cp = (char *)ch->chptr;
179 fuend = fup.cp + ch->ch_size;
181 while (fup.cp < fuend)
184 xloc = *fup.lp++ + (int)fusizoffs[w & FUMASK];
188 return (int)fusiztab[w & FUMASK];
205 // Check that there are at least `amt' bytes left in the current chunk. If
206 // there are not, allocate another chunk of at least `amt' bytes (and probably
209 // If `amt' is zero, ensure there are at least CH_THRESHOLD bytes, likewise.
211 int chcheck(LONG amt)
213 DEBUG { printf("chcheck(%u)\n", amt); }
214 // If in BSS section, no allocation required
221 DEBUG { printf(" challoc=%i, ch_size=%i, diff=%i\n", challoc, ch_size, challoc-ch_size); }
222 if ((int)(challoc - ch_size) >= (int)amt)
225 if (amt < CH_CODE_SIZE)
228 DEBUG { printf(" amt (adjusted)=%u\n", amt); }
229 SECT * p = §[cursect];
230 CHUNK * cp = malloc(sizeof(CHUNK) + amt);
232 // First chunk in section
238 // Add chunk to other chunks
243 scode->ch_size = ch_size; // Save old chunk's globals
246 // Setup chunk and global vars
249 challoc = cp->challoc = amt;
250 ch_size = cp->ch_size = 0;
251 chptr = cp->chptr = ((char *)cp) + sizeof(CHUNK);
252 scode = p->scode = cp;
258 // This is really wrong. We need to make some proper structures here so we don't
259 // have to count sizes of objects, that's what the compiler's for! :-P
260 #define FIXUP_BASE_SIZE (sizeof(WORD) + sizeof(LONG) + sizeof(WORD) + sizeof(WORD))
262 // Arrange for a fixup on a location
264 int fixup(WORD attr, LONG loc, TOKEN * fexpr)
270 // Shamus: Expression lengths are voodoo ATM (varibale "i"). Need to fix this.
271 DEBUG printf("FIXUP@$%X: $%X\n", loc, attr);
273 // Compute length of expression (could be faster); determine if it's the
274 // single-symbol case; no expression if it's just a mark. This code assumes
275 // 16 bit WORDs and 32 bit LONGs
276 if (*fexpr == SYMBOL && fexpr[2] == ENDEXPR)
278 // Just a single symbol
279 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
280 if ((attr & 0x0F00) == FU_JR)
283 // i = FIXUP_BASE_SIZE + (sizeof(LONG) * 2);
284 i = FIXUP_BASE_SIZE + sizeof(SYM *) + sizeof(LONG);
289 i = FIXUP_BASE_SIZE + sizeof(SYM *);
296 for(len=0; fexpr[len]!=ENDEXPR; len++)
298 if (fexpr[len] == CONST || fexpr[len] == SYMBOL)
302 len++; // Add 1 for ENDEXPR
303 // i = (len << 2) + 12;
304 i = FIXUP_BASE_SIZE + sizeof(WORD) + (len * sizeof(TOKEN));
307 // Maybe alloc another fixup chunk for this one to fit in
308 if ((fchalloc - fchsize) < i)
311 cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
313 // First fixup chunk in section
319 // Add to other chunks
324 sfix->ch_size = fchsize;
327 // Setup fixup chunk and its global vars
329 fchalloc = cp->challoc = CH_FIXUP_SIZE;
330 fchsize = cp->ch_size = 0;
331 fchptr.cp = cp->chptr = ((char *)cp) + sizeof(CHUNK);
335 // Record fixup type, fixup location, and the file number and line number
336 // the fixup is located at.
339 *fchptr.wp++ = cfileno;
340 *fchptr.wp++ = (WORD)curlineno;
342 // Store postfix expression or pointer to a single symbol, or nothing for a
346 *fchptr.wp++ = (WORD)len;
349 *fchptr.lp++ = (LONG)*fexpr++;
353 // *fchptr.lp++ = (LONG)fexpr[1];
354 *fchptr.sy++ = symbolPtr[fexpr[1]];
357 // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
358 if ((attr & 0x0F00) == FU_JR)
361 *fchptr.lp++ = orgaddr;
363 *fchptr.lp++ = 0x00000000;
372 // Resolve all Fixups
374 int ResolveAllFixups(void)
379 // Make undefined symbols GLOBL
383 DEBUG printf("Resolving TEXT sections...\n");
385 DEBUG printf("Resolving DATA sections...\n");
388 // We need to do a final check of forward 'jump' destination addresses that
390 for(i=0; i<MAXFWDJUMPS; i++)
395 sprintf(buf, "* \'jump\' at $%08X - destination address is external to this source file and cannot have its aligment validated", fwdjump[i]);
401 write(err_fd, buf, (LONG)strlen(buf));
412 // Resolve Fixups in a Section
414 int ResolveFixups(int sno)
416 PTR fup; // Current fixup
417 WORD * fuend; // End of last fixup (in this chunk)
418 WORD w; // Fixup word (type+modes+flags)
419 char * locp; // Location to fix (in cached chunk)
420 LONG loc; // Location to fixup
421 VALUE eval; // Expression value
422 WORD eattr; // Expression attrib
423 SYM * esym; // External symbol involved in expr
424 SYM * sy; // (Temp) pointer to a symbol
425 WORD i; // (Temp) word
426 WORD tdb; // eattr & TDB
430 unsigned page_jump = 0;
431 unsigned address = 0;
435 SECT * sc = §[sno];
436 CHUNK * ch = sc->sffix;
441 CHUNK * cch = sc->sfcode; // "cache" first chunk
443 if (cch == NULL) // Can't fixup a sect with nothing in it
448 fup.cp = ch->chptr; // fup -> start of chunk
449 fuend = (WORD *)(fup.cp + ch->ch_size); // fuend -> end of chunk
451 while (fup.wp < fuend)
456 curlineno = (int)*fup.wp++;
457 DEBUG { printf("ResolveFixups: cfileno=%u\n", cfileno); }
461 // Search for chunk containing location to fix up; compute a
462 // pointer to the location (in the chunk). Often we will find the
463 // fixup is in the "cached" chunk, so the linear-search is seldom
465 if (loc < cch->chloc || loc >= (cch->chloc + cch->ch_size))
467 for(cch=sc->sfcode; cch!=NULL; cch=cch->chnext)
469 if (loc >= cch->chloc && loc < (cch->chloc + cch->ch_size))
475 interror(7); // Fixup (loc) out of range
480 locp = cch->chptr + (loc - cch->chloc);
483 // Compute expression/symbol value and attribs
484 // Complex expression
489 if (evexpr(fup.tk, &eval, &eattr, &esym) != OK)
508 if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
512 tdb = (WORD)(eattr & TDB);
514 // If the expression is undefined and no external symbol is
515 // involved, then it's an error.
516 if (!(eattr & DEFINED) && esym == NULL)
522 if (((w & 0x0F00) == FU_MOVEI) && esym)
523 esym->sattre |= RISCSYM;
527 // If a PC-relative fixup is undefined, its value is *not*
528 // subtracted from the location (that will happen in the linker
529 // when the external reference is resolved).
531 // MWC expects PC-relative things to have the LOC subtracted from
532 // the value, if the value is external (that is, undefined at this
535 // PC-relative fixups must be DEFINED and either in the same
536 // section (whereupon the subtraction takes place) or ABS (with no
546 error("PC-relative expr across sections");
550 if (sbra_flag && (w & FU_LBRA) && (eval + 0x80 < 0x100))
551 warn("unoptimized short branch");
553 else if (obj_format == MWC)
561 switch ((int)(w & FUMASK))
563 // FU_BBRA fixes up a one-byte branch offset.
565 if (!(eattr & DEFINED))
567 error("external short branch");
573 if (eval + 0x80 >= 0x100)
578 error("illegal bra.s with zero offset");
582 *++locp = (char)eval;
584 // Fixup one-byte value at locp + 1.
588 // Fixup one-byte forward references
590 if (!(eattr & DEFINED))
592 error("external byte reference");
598 error("non-absolute byte reference");
602 if ((w & FU_PCREL) && eval + 0x80 >= 0x100)
607 if (eval + 0x100 >= 0x200)
610 else if (eval >= 0x100)
615 // Fixup WORD forward references;
616 // the word could be unaligned in the section buffer, so we have to
619 if (((w & 0x0F00) == FU_JR) || ((w & 0x0F00) == FU_MJR))
625 reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
629 reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
632 if ((w & 0x0F00) == FU_MJR)
634 // Main code destination alignment checking here for
635 // forward declared labels
636 address = (oaddr) ? oaddr : loc;
638 if (((address >= 0xF03000) && (address < 0xF04000)
639 && (eval < 0xF03000)) || ((eval >= 0xF03000)
640 && (eval < 0xF04000) && (address < 0xF03000)))
642 warni("* \'jr\' at $%08X - cannot jump relative between "
643 "main memory and local gpu ram", address);
647 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
651 // This jump is to a page outside of the
652 // current 256 byte page
655 warni("* \'jr\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
660 // This jump is in the current 256 byte page
663 warni("* \'jr\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
669 if ((reg2 < -16) || (reg2 > 15))
671 error("relative jump out of range");
675 *locp = (char)(*locp | ((reg2 >> 3) & 0x03));
677 *locp = (char)(*locp | ((reg2 & 0x07) << 5));
681 if ((w & 0x0F00) == FU_NUM15)
683 if (eval < -16 || eval > 15)
685 error("constant out of range");
689 *locp = (char)(*locp | ((eval >> 3) & 0x03));
691 *locp = (char)(*locp | ((eval & 0x07) << 5));
695 if ((w & 0x0F00) == FU_NUM31)
697 if (eval < 0 || eval > 31)
699 error("constant out of range");
703 *locp = (char)(*locp | ((eval >> 3) & 0x03));
705 *locp = (char)(*locp | ((eval & 0x07) << 5));
709 if ((w & 0x0F00) == FU_NUM32)
711 if (eval < 1 || eval > 32)
713 error("constant out of range");
720 eval = (eval == 32) ? 0 : eval;
721 *locp = (char)(*locp | ((eval >> 3) & 0x03));
723 *locp = (char)(*locp | ((eval & 0x07) << 5));
727 if ((w & 0x0F00) == FU_REGONE)
729 if (eval < 0 || eval > 31)
731 error("register value out of range");
735 *locp = (char)(*locp | ((eval >> 3) & 0x03));
737 *locp = (char)(*locp | ((eval & 0x07) << 5));
741 if ((w & 0x0F00) == FU_REGTWO)
743 if (eval < 0 || eval > 31)
745 error("register value out of range");
750 *locp = (char)(*locp | (eval & 0x1F));
754 if (!(eattr & DEFINED))
761 rmark(sno, loc, 0, w, esym);
766 rmark(sno, loc, tdb, MWORD, NULL);
770 if (eval + 0x10000 >= 0x20000)
775 // Range-check BRA and DBRA
778 if (eval + 0x8000 >= 0x10000)
781 else if (eval >= 0x10000)
786 *locp++ = (char)(eval >> 8);
789 // Fixup LONG forward references;
790 // the long could be unaligned in the section buffer, so be careful
793 if ((w & 0x0F00) == FU_MOVEI)
799 for(j=0; j<fwindex; j++)
801 if (fwdjump[j] == address)
803 page_jump = (address & 0xFFFFFF00) - (eval & 0xFFFFFF00);
810 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for long page jump, insert a \'nop\' before the destination address", address);
816 write(err_fd, buf, (LONG)strlen(buf));
823 if (!(eval & 0x0000000F) || ((eval - 2) % 4))
826 sprintf(buf, "* \'jump\' at $%08X - destination address not aligned for short page jump, insert a \'nop\' before the destination address", address);
832 write(err_fd, buf, (LONG)strlen(buf));
838 // Clear this jump as it has been checked
845 eval = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
846 flags = (MLONG|MMOVEI);
851 if (!(eattr & DEFINED))
853 rmark(sno, loc, 0, flags, esym);
857 rmark(sno, loc, tdb, flags, NULL);
860 *locp++ = (char)(eval >> 24);
861 *locp++ = (char)(eval >> 16);
862 *locp++ = (char)(eval >> 8);
865 // Fixup a 3-bit "QUICK" reference in bits 9..1
866 // (range of 1..8) in a word. Really bits 1..3 in a byte.
868 if (!(eattr & DEFINED))
870 error("External quick reference");
874 if (eval < 1 || eval > 8)
877 *locp |= (eval & 7) << 1;
879 // Fix up 6502 funny branch
883 if (eval + 0x80 >= 0x100)
889 interror(4); // Bad fixup type
894 error("expression out of range");