X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=rmac;a=blobdiff_plain;f=sect.c;h=3e127656916367fa1f7573336582f7a951f9501e;hp=b58d441709681244e395a0bb2683d607cf4e00f1;hb=eace4e1b294ccec54a5c476619f616f5da0bf8a9;hpb=60f204cb9e3905100da0d89f14bb40db764acd9e diff --git a/sect.c b/sect.c index b58d441..3e12765 100644 --- a/sect.c +++ b/sect.c @@ -1,12 +1,13 @@ // -// 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" @@ -18,6 +19,9 @@ #include "token.h" +// 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); @@ -46,26 +50,26 @@ PTR fchptr; // Deposit point in fixup chunk buffer // 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 }; @@ -74,18 +78,16 @@ static uint8_t fusizoffs[] = { // void InitSection(void) { - int i; - // Cleanup all sections - for(i=0; iscattr; sloc = p->sloc; @@ -127,6 +131,10 @@ void SwitchSection(int sno) 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; @@ -257,66 +265,66 @@ int chcheck(uint32_t amt) } -// 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++) { - if (fexpr[len] == CONST || fexpr[len] == SYMBOL) + // Add one to len for 2X tokens, two for 3X tokens + if (fexpr[len] == SYMBOL) len++; + else if (fexpr[len] == CONST) + len += 2; } - 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) @@ -345,30 +353,30 @@ WARNING(!!! AddFixup() is filled with VOODOO !!!) *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; @@ -382,17 +390,9 @@ WARNING(!!! AddFixup() is filled with VOODOO !!!) 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 + uint64_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; @@ -409,17 +409,21 @@ int ResolveFixups(int sno) if (cch == NULL) return 0; + // 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 @@ -427,7 +431,7 @@ int ResolveFixups(int sno) // 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 @@ -449,8 +453,9 @@ int ResolveFixups(int sno) } } - 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 @@ -483,7 +488,7 @@ int ResolveFixups(int sno) 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. @@ -511,7 +516,7 @@ int ResolveFixups(int sno) if (eattr & DEFINED) { if (tdb == sno) - eval -= (VALUE)loc; + eval -= (uint32_t)loc; else if (tdb) { error("PC-relative expr across sections"); @@ -522,14 +527,14 @@ int ResolveFixups(int sno) 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: @@ -583,18 +588,37 @@ int ResolveFixups(int sno) *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 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)) { @@ -713,7 +737,12 @@ int ResolveFixups(int sno) } } - SETBE16(locp, 0, eval); + // 6502 words are little endian, so handle that here + if (sno == M6502) + SETLE16(locp, 0, eval) + else + SETBE16(locp, 0, eval) + break; // Fixup LONG forward references; // the long could be unaligned in the section buffer, so be careful @@ -793,6 +822,8 @@ int ResolveAllFixups(void) ResolveFixups(TEXT); DEBUG printf("Resolving DATA sections...\n"); ResolveFixups(DATA); + DEBUG printf("Resolving 6502 sections...\n"); + ResolveFixups(M6502); /* fixup 6502 section (if any) */ return 0; }