//
-// RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
+// RMAC - Reboot's Macro Assembler for all Atari computers
// SECT.C - Code Generation, Fixups and Section Management
-// Copyright (C) 199x Landon Dyer, 2017 Reboot and Friends
+// Copyright (C) 199x Landon Dyer, 2011-2017 Reboot and Friends
// RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
// Source utilised with the kind permission of Landon Dyer
//
#include "sect.h"
+#include "6502.h"
#include "direct.h"
#include "error.h"
#include "expr.h"
#include "riscasm.h"
#include "symbol.h"
#include "token.h"
-extern int m6502; /* 1, assembler in .6502 mode */
+// Minimum size of a fixup record
+#define MIN_FIXUP_MEM (3 * sizeof(uint16_t) + 1 * sizeof(uint32_t))
+
// Function prototypes
void MakeSection(int, uint16_t);
void SwitchSection(int);
// Return a size (SIZB, SIZW, SIZL) or 0, depending on what kind of fixup is
// associated with a location.
static uint8_t fusiztab[] = {
- 0, // FU_QUICK
- 1, // FU_BYTE
- 2, // FU_WORD
- 2, // FU_WBYTE
- 4, // FU_LONG
- 1, // FU_BBRA
- 0, // (unused)
- 1, // FU_6BRA
+ 0, // FU_QUICK
+ 1, // FU_BYTE
+ 2, // FU_WORD
+ 2, // FU_WBYTE
+ 4, // FU_LONG
+ 1, // FU_BBRA
+ 0, // (unused)
+ 1, // FU_6BRA
};
// Offset to REAL fixup location
static uint8_t fusizoffs[] = {
- 0, // FU_QUICK
- 0, // FU_BYTE
- 0, // FU_WORD
- 1, // FU_WBYTE
- 0, // FU_LONG
- 1, // FU_BBRA
- 0, // (unused)
- 0, // FU_6BRA
+ 0, // FU_QUICK
+ 0, // FU_BYTE
+ 0, // FU_WORD
+ 1, // FU_WBYTE
+ 0, // FU_LONG
+ 1, // FU_BBRA
+ 0, // (unused)
+ 0, // FU_6BRA
};
//
void InitSection(void)
{
- int i;
-
// Cleanup all sections
- for(i=0; i<NSECTS; i++)
+ for(int i=0; i<NSECTS; i++)
MakeSection(i, 0);
// Construct default sections, make TEXT the current section
- MakeSection(ABS, SUSED | SABS | SBSS); // ABS
- MakeSection(TEXT, SUSED | TEXT ); // TEXT
- MakeSection(DATA, SUSED | DATA ); // DATA
- MakeSection(BSS, SUSED | BSS | SBSS); // BSS
- MakeSection(M6502, SUSED | TEXT ); // 6502 code section
+ MakeSection(ABS, SUSED | SABS | SBSS); // ABS
+ MakeSection(TEXT, SUSED | TEXT ); // TEXT
+ MakeSection(DATA, SUSED | DATA ); // DATA
+ MakeSection(BSS, SUSED | BSS | SBSS); // BSS
+ MakeSection(M6502, SUSED | TEXT ); // 6502 code section
// Switch to TEXT for starters
SwitchSection(TEXT);
cursect = sno;
SECT * p = §[sno];
- m6502 = (sno == M6502); /* set 6502-mode */
+ m6502 = (sno == M6502); // Set 6502-mode flag
// Copy section vars
scattr = p->scattr;
challoc = cp->challoc;
ch_size = cp->ch_size;
chptr = cp->chptr + ch_size;
+
+ // For 6502 mode, add the last org'd address
+ if (m6502)
+ chptr = cp->chptr + orgaddr;
}
else
challoc = ch_size = 0;
}
-// This is really wrong. We need to make some proper structures here so we
-// don't have to count sizes of objects, that's what the compiler's for! :-P
-#define FIXUP_BASE_SIZE (sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t))
+//
+// A fixup record is at least 4 pieces of data long, with some optional data at
+// the end. Is of the form:
+//
+// SYMBOL EXPRESSION
+// ------ ----------
+// FU_EXPR.W FU_EXPR.W fixup type
+// loc.L loc.L location in section
+// fileno.W fileno.W file number fixup occurred in
+// lineno.W lineno.W line number fixup occurred in
+// symbol.* size.W &symbol (32 or 64 bits) / size of expression
+// token.L size (zero or more) TOKENS (32-bits each)
+// ENDEXPR.L End of expression (with size > zero)
+// JR.L Possible ORG address of RISC JR instruction
//
// Arrange for a fixup on a location
//
int AddFixup(uint16_t attr, uint32_t loc, TOKEN * fexpr)
{
- uint32_t i;
- uint32_t len = 0;
- CHUNK * cp;
- SECT * p;
- // Shamus: Expression lengths are voodoo ATM (variable "i"). Need to fix
- // this.
-WARNING(!!! AddFixup() is filled with VOODOO !!!)
+ uint32_t i = MIN_FIXUP_MEM;
+ uint16_t len = 0;
+
DEBUG printf("FIXUP@$%X: $%X\n", loc, attr);
// Compute length of expression (could be faster); determine if it's the
- // single-symbol case; no expression if it's just a mark. This code assumes
- // 16 bit WORDs and 32 bit LONGs
- if (*fexpr == SYMBOL && fexpr[2] == ENDEXPR)
+ // single-symbol case; no expression if it's just a mark. (? is this true?)
+ if ((*fexpr == SYMBOL) && (fexpr[2] == ENDEXPR))
{
- // Just a single symbol
- // SCPCD : correct bit mask for attr (else other FU_xxx will match)
+ // Just a single symbol, possibly followed by a DWORD
+ i += sizeof(SYM *);
+
+ // SCPCD: Correct bit mask for attr (else other FU_xxx will match)
// NYAN !
if ((attr & FUMASKRISC) == FU_JR)
- {
-//printf("AddFixup: ((attr & FUMASKRISC) == FU_JR)\n");
-// i = 18;
-// i = FIXUP_BASE_SIZE + (sizeof(uint32_t) * 2);
- i = FIXUP_BASE_SIZE + sizeof(SYM *) + sizeof(uint32_t);
- }
- else
- {
-//printf("AddFixup: ((attr & FUMASKRISC) == FU_JR) ELSE\n");
-// i = 14;
- i = FIXUP_BASE_SIZE + sizeof(SYM *);
- }
+ i += sizeof(uint32_t);
}
else
{
-//printf("AddFixup: !SYMBOL\n");
attr |= FU_EXPR;
+ // Count the # of tokens in the expression
for(len=0; fexpr[len]!=ENDEXPR; len++)
{
+ // Add one to len for 2X tokens
if (fexpr[len] == CONST || fexpr[len] == SYMBOL)
len++;
}
- len++; // Add 1 for ENDEXPR
-// i = (len << 2) + 12;
- i = FIXUP_BASE_SIZE + sizeof(uint16_t) + (len * sizeof(TOKEN));
+ // Add 1 for ENDEXPR
+ len++;
+ i += sizeof(uint16_t) + (len * sizeof(TOKEN));
}
// Alloc another fixup chunk for this one to fit in if necessary
if ((fchalloc - fchsize) < i)
{
- p = §[cursect];
- cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
+ SECT * p = §[cursect];
+ CHUNK * cp = (CHUNK *)malloc(sizeof(CHUNK) + CH_FIXUP_SIZE);
// First fixup chunk in section
if (sfix == NULL)
*fchptr.wp++ = attr;
*fchptr.lp++ = loc;
*fchptr.wp++ = cfileno;
- *fchptr.wp++ = (uint16_t)curlineno;
+ *fchptr.wp++ = curlineno;
// Store postfix expression or pointer to a single symbol, or nothing for a
- // mark.
+ // mark (a zero word is stored in this case--[? is it?]).
if (attr & FU_EXPR)
{
- *fchptr.wp++ = (uint16_t)len;
+ *fchptr.wp++ = len;
while (len--)
- *fchptr.lp++ = (uint32_t)*fexpr++;
+ *fchptr.lp++ = *fexpr++;
}
else
{
*fchptr.sy++ = symbolPtr[fexpr[1]];
-//printf("AddFixup: adding symbol (%s) [%08X]\n", symbolPtr[fexpr[1]]->sname, symbolPtr[fexpr[1]]->sattr);
- }
- // SCPCD : correct bit mask for attr (else other FU_xxx will match) NYAN !
- if ((attr & FUMASKRISC) == FU_JR)
- {
- if (orgactive)
- *fchptr.lp++ = orgaddr;
- else
- *fchptr.lp++ = 0x00000000;
+ // SCPCD: Correct bit mask for attr (else other FU_xxx will match)
+ // NYAN !
+ if ((attr & FUMASKRISC) == FU_JR)
+ {
+ if (orgactive)
+ *fchptr.lp++ = orgaddr;
+ else
+ *fchptr.lp++ = 0x00000000;
+ }
}
fchsize += i;
int ResolveFixups(int sno)
{
PTR fup; // Current fixup
- uint16_t * fuend; // End of last fixup (in this chunk)
- uint16_t w; // Fixup word (type+modes+flags)
- uint8_t * locp; // Location to fix (in cached chunk)
- uint32_t loc; // Location to fixup
- VALUE eval; // Expression value
- uint16_t eattr; // Expression attrib
- SYM * esym; // External symbol involved in expr
+ uint32_t eval; // Expression value
SYM * sy; // (Temp) pointer to a symbol
uint16_t i; // (Temp) word
- uint16_t tdb; // eattr & TDB
- uint32_t oaddr;
int reg2;
uint16_t flags;
if (cch == NULL)
return 0;
- /*
- * Wire the 6502 segment's size to its allocated size (64K)
- */
+ // Wire the 6502 segment's size to its allocated size (64K)
if (sno == M6502)
cch->ch_size = cch->challoc;
-
+
do
{
fup.cp = ch->chptr; // fup -> start of chunk
- fuend = (uint16_t *)(fup.cp + ch->ch_size); // fuend -> end of chunk
+ uint16_t * fuend = (uint16_t *)(fup.cp + ch->ch_size); // fuend -> end of chunk
while (fup.wp < fuend)
{
- w = *fup.wp++;
- loc = *fup.lp++;
+ uint16_t w = *fup.wp++; // Fixup word (type+modes+flags)
+ uint32_t loc = *fup.lp++; // Location to fixup
cfileno = *fup.wp++;
- curlineno = (int)*fup.wp++;
+ curlineno = *fup.wp++;
DEBUG { printf("ResolveFixups: cfileno=%u\n", cfileno); }
// This is based on global vars cfileno, curfname :-P
// than this.
SetFilenameForErrorReporting();
- esym = NULL;
+ SYM * esym = NULL; // External symbol involved in expr
// Search for chunk containing location to fix up; compute a
// pointer to the location (in the chunk). Often we will find the
}
}
- locp = cch->chptr + (loc - cch->chloc);
- eattr = 0;
+ // Location to fix (in cached chunk)
+ uint8_t * locp = cch->chptr + (loc - cch->chloc);
+ uint16_t eattr = 0; // Expression attrib
// Compute expression/symbol value and attribs
esym = sy;
}
- tdb = (uint16_t)(eattr & TDB);
+ uint16_t tdb = eattr & TDB;
// If the expression is undefined and no external symbol is
// involved, then that's an error.
if (eattr & DEFINED)
{
if (tdb == sno)
- eval -= (VALUE)loc;
+ eval -= (uint32_t)loc;
else if (tdb)
{
error("PC-relative expr across sections");
warn("unoptimized short branch");
}
else if (obj_format == MWC)
- eval -= (VALUE)loc;
+ eval -= (uint32_t)loc;
tdb = 0;
eattr &= ~TDB;
}
// Do fixup classes
- switch ((int)(w & FUMASK))
+ switch (w & FUMASK)
{
// FU_BBRA fixes up a one-byte branch offset.
case FU_BBRA:
*locp = (uint8_t)eval;
break;
- // Fixup high/low byte off word for 6502
- case FU_BYTEH:
- if (!(eattr & DEFINED))
- {
- error("external byte reference");
- continue;
- }
-
- *locp = (uint8_t)((eval >> 8) & 0xff);
- break;
- case FU_BYTEL:
- if (!(eattr & DEFINED))
- {
- error("external byte reference");
- continue;
- }
-
- *locp = (uint8_t)(eval & 0xff);
- break;
+ // Fixup high/low byte off word for 6502
+ case FU_BYTEH:
+ if (!(eattr & DEFINED))
+ {
+ error("external byte reference");
+ continue;
+ }
+
+ *locp = (uint8_t)((eval >> 8) & 0xFF);
+ break;
+ case FU_BYTEL:
+ if (!(eattr & DEFINED))
+ {
+ error("external byte reference");
+ continue;
+ }
+
+ *locp = (uint8_t)(eval & 0xFF);
+ break;
// Fixup WORD forward references;
// the word could be unaligned in the section buffer, so we have to
// be careful.
case FU_WORD:
if ((w & FUMASKRISC) == FU_JR)
{
- oaddr = *fup.lp++;
+ uint32_t orgaddr = *fup.lp++;
- if (oaddr)
- reg2 = (signed)((eval - (oaddr + 2)) / 2);// & 0x1F;
+ if (orgaddr)
+ reg2 = (signed)((eval - (orgaddr + 2)) / 2);
else
- reg2 = (signed)((eval - (loc + 2)) / 2);// & 0x1F;
+ reg2 = (signed)((eval - (loc + 2)) / 2);
if ((reg2 < -16) || (reg2 > 15))
{
}
}
- if (sno != M6502)
- {
- *locp++ = (char)(eval >> 8);
- *locp = (char)eval;
- }
+ // 6502 words are little endian, so handle that here
+ if (sno == M6502)
+ SETLE16(locp, 0, eval)
else
- {
- SETBE16(locp, 0, eval);
- }
+ SETBE16(locp, 0, eval)
+
break;
// Fixup LONG forward references;
// the long could be unaligned in the section buffer, so be careful