]> Shamusworld >> Repos - rmac/blobdiff - sect.c
Slight revamp of the optimisation system, featuring: strengthened flag parser, slight...
[rmac] / sect.c
diff --git a/sect.c b/sect.c
index 49806a02b9d4e8d7570b19b5d27f35ca89b6dc86..cdf53ffc1ab5e650848211a030931580eb349a11 100644 (file)
--- a/sect.c
+++ b/sect.c
@@ -1,7 +1,7 @@
 //
 // RMAC - Reboot's Macro Assembler for all Atari computers
 // SECT.C - Code Generation, Fixups and Section Management
-// Copyright (C) 199x Landon Dyer, 2011-2019 Reboot and Friends
+// Copyright (C) 199x Landon Dyer, 2011-2020 Reboot and Friends
 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
 // Source utilised with the kind permission of Landon Dyer
 //
@@ -159,7 +159,7 @@ void SaveSection(void)
        sp->sloc = sloc;
        sp->orgaddr = orgaddr;
 
-       if (scode != NULL)                              // Bailout code chunk
+       if (scode != NULL)                              // Bailout code chunk (if any)
                scode->ch_size = ch_size;
 }
 
@@ -218,29 +218,43 @@ void chcheck(uint32_t amt)
        DEBUG { printf("    amt (adjusted)=%u\n", amt); }
        SECT * p = &sect[cursect];
        CHUNK * cp = malloc(sizeof(CHUNK) + amt);
+       int first = 0;
 
-       // First chunk in section
        if (scode == NULL)
        {
+               // First chunk in section
                cp->chprev = NULL;
                p->sfcode = cp;
+               first = 1;
        }
-       // Add chunk to other chunks
        else
        {
+               // Add second and on to previous chunk
                cp->chprev = scode;
                scode->chnext = cp;
                scode->ch_size = ch_size;       // Save old chunk's globals
        }
 
        // Setup chunk and global vars
-       cp->chloc = sloc;
+/*
+So, whenever there's an ORG in a 56K section, it sets sloc TO THE ADDRESS IN THE ORG.  Also, the loc/sloc are incremented by 1s, which means to alias correctly to the byte-oriented memory model we have here, we have to fix that kind of crap.
+*/
+       cp->chloc = sloc; // <-- HERE'S THE PROBLEM FOR 56K  :-/
        cp->chnext = NULL;
        challoc = cp->challoc = amt;
        ch_size = cp->ch_size = 0;
        chptr = cp->chptr = ((uint8_t *)cp) + sizeof(CHUNK);
        scode = p->scode = cp;
 
+       // A quick kludge
+/*
+OK, so this is a bit shite, but at least it gets things working the way they should.  The right way to do this is not rely on sloc & friends for the right fixup address but to have an accurate model of the thing.  That will probably come with v2.0.1  :-P
+
+So the problem is, d_org sets sloc to the address of the ORG statement, and that gives an incorrect base for the fixup.  And so when a second (or later) chunk is allocated, it gets set wrong.  Further complicating things is that the orgaddress *does not* get used in a typical way with the DSP56001 code, and, as such, causes incorrect addresses to be generated.  All that has to be dealt with in order to get this right and do away with this kludge.
+*/
+       if (((cursect == M56001P) || (cursect == M56001X) || (cursect == M56001Y)) && !first)
+               cp->chloc = cp->chprev->chloc + cp->chprev->ch_size;
+
        return;
 }
 
@@ -276,7 +290,7 @@ int AddFixup(uint32_t attr, uint32_t loc, TOKEN * fexpr)
        {
                attr |= FU_56001;
                // Save the exact spot in this chunk where the fixup should go
-               _orgaddr = chptr - scode->chptr;
+               _orgaddr = chptr - scode->chptr + scode->chloc;
        }
 
        // Allocate space for the fixup + any expression
@@ -398,8 +412,18 @@ int ResolveFixups(int sno)
                // Complex expression
                if (dw & FU_EXPR)
                {
+                       // evexpr presumably issues the errors/warnings here
                        if (evexpr(fup->expr, &eval, &eattr, &esym) != OK)
                                continue;
+
+                       if (CHECK_OPTS(OPT_PC_RELATIVE))
+                               if (eattr & REFERENCED)
+                                       if (eattr & DEFINED)
+                                               if (!(eattr & EQUATED))
+                                               {
+                                                       error("relocation not allowed");
+                                                       continue;
+                                               }
                }
                // Simple symbol
                else
@@ -407,6 +431,15 @@ int ResolveFixups(int sno)
                        SYM * sy = fup->symbol;
                        eattr = sy->sattr;
 
+                       if (CHECK_OPTS(OPT_PC_RELATIVE))
+                               if (eattr & REFERENCED)
+                                       if (eattr & DEFINED)
+                                               if (!(eattr & EQUATED))
+                                               {
+                                                       error("relocation not allowed");
+                                                       continue;
+                                               }
+
                        if (eattr & DEFINED)
                                eval = sy->svalue;
                        else
@@ -415,6 +448,8 @@ int ResolveFixups(int sno)
                        // If the symbol is not defined, but global, set esym to sy
                        if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
                                esym = sy;
+
+                       DEBUG { printf("               name: %s, value: $%" PRIX64 "\n", sy->sname, sy->svalue); }
                }
 
                uint16_t tdb = eattr & TDB;
@@ -433,21 +468,24 @@ int ResolveFixups(int sno)
                // from the location (that will happen in the linker when the external
                // reference is resolved).
                //
-               // MWC expects PC-relative things to have the LOC subtracted from the
-               // value, if the value is external (that is, undefined at this point).
-               //
                // PC-relative fixups must be DEFINED and either in the same section
                // (whereupon the subtraction takes place) or ABS (with no subtract).
-               if (dw & FU_PCREL)
+               if ((dw & FU_PCREL) || (dw & FU_PCRELX))
                {
                        if (eattr & DEFINED)
                        {
                                if (tdb == sno)
+                               {
                                        eval -= loc;
+
+                                       // In this instruction the PC is located a DWORD away
+                                       if (dw & FU_PCRELX)
+                                               eval += 2;
+                               }
                                else if (tdb)
                                {
                                        // Allow cross-section PCREL fixups in Alcyon mode
-                                       if (prg_flag)
+                                       if (prg_flag || (obj_format == RAW))
                                        {
                                                switch (tdb)
                                                {
@@ -466,6 +504,10 @@ int ResolveFixups(int sno)
                                                }
 
                                                eval -= loc;
+
+                                               // In this instruction the PC is located a DWORD away
+                                               if (dw & FU_PCRELX)
+                                                       eval += 2;
                                        }
                                        else
                                        {
@@ -474,12 +516,11 @@ int ResolveFixups(int sno)
                                        }
                                }
 
-                               if (sbra_flag && (dw & FU_LBRA) && (eval + 0x80 < 0x100))
+                               if (optim_warn_flag && (dw & FU_LBRA) && (eval + 0x80 < 0x100))
                                        warn("unoptimized short branch");
                        }
-                       else if (obj_format == MWC)
-                               eval -= loc;
 
+                       // Be sure to clear any TDB flags, since we handled it just now
                        tdb = 0;
                        eattr &= ~TDB;
                }
@@ -502,11 +543,13 @@ int ResolveFixups(int sno)
 
                        if (eval == 0)
                        {
-                               if (CHECK_OPTS(OPT_NULL_BRA))
+                               if (*locp) // optim_flags[OPT_NULL_BRA] is stored there, check the comment in mach.s under m_br
                                {
                                        // Just output a NOP
                                        *locp++ = 0x4E;
                                        *locp = 0x71;
+                                       if (optim_warn_flag)
+                                               warn("bra.s with zero offset converted to NOP");
                                        continue;
                                }
                                else