Version bump for last commit; now at v2.0.23.
[rmac] / parmode.h
index 690b6168d0b5752d00e8accfcb39b82a7a57025c..dd3e1a2b03ee87b6bd58b0a6df3da462ab7bfe2f 100644 (file)
--- a/parmode.h
+++ b/parmode.h
@@ -1,7 +1,7 @@
 //
 // RMAC - Reboot's Macro Assembler for all Atari computers
 // PARMODE.C - Addressing Modes Parser Include
-// Copyright (C) 199x Landon Dyer, 2011-2018 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
 //
@@ -55,6 +55,7 @@
        // ([bd,PC,Xn[.siz][*scale]],od)
        else if (*tok == '(')
        {
+               int ea_PC = 0;                          // Flag that let us know if we have PC or An relative ea
                tok++;
 
                if ((*tok >= KW_A0) && (*tok <= KW_A7))
@@ -81,7 +82,7 @@
                }
                else if ((*tok >= KW_D0) && (*tok <= KW_D7))
                {
-                       //Since index register isn't used here, store register number in this field
+                       // Since index register isn't used here, store register number in this field
                        AnIXREG = *tok++ & 7;                                // (Dn)
 
                        if (*tok == ')')
@@ -322,10 +323,10 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
                        {
                                expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
 
-                               if (CHECK_OPTS(OPT_BASE_DISP) && AnBEXVAL == 0 && AnEXATTR != 0)
+                               if (CHECK_OPTS(OPT_020_DISP) && (AnBEXVAL == 0) && (AnEXATTR != 0))
                                {
-                                       // bd=0 so let's optimise it out
-                                       AnEXTEN|=EXT_BDSIZE0;
+                                       // bd = 0 so let's optimise it out
+                                       AnEXTEN |= EXT_BDSIZE0;
                                }
                                else if (*tok == DOTL)
                                {
@@ -346,12 +347,14 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
                                        {
                                                // Defined, absolute values from $FFFF8000..$00007FFF
                                                // get optimized to absolute short
-                                               if (CHECK_OPTS(OPT_ABS_SHORT)
+                                               if (CHECK_OPTS(OPT_020_DISP)
                                                        && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
                                                        && (((uint32_t)AnBEXVAL + 0x8000) < 0x10000))
                                                {
                                                        AnEXTEN |= EXT_BDSIZEW;
-                                                       warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
+
+                                                       if (optim_warn_flag)
+                                                               warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
                                                }
                                                else
                                                {
@@ -362,8 +365,6 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
 
                                if (*tok == ',')
                                        tok++;
-                               //else
-                               //      return error("Comma expected after base displacement");
                        }
 
                        // Check for address register or PC, suppress base register
@@ -371,6 +372,8 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
 
                        if (*tok == KW_PC)
                        {                                       // ([bd,PC,...
+                               ea_PC = 3;                              // Set flag in order to set proper value to AMn below when we can make a decision on ea
+                                                                               // (why "3"? Well, MEMPOST is 3 away from PCMPOST, etc. Have a look at amode.h)
                                AnREG = (7 << 3) | 3;   // PC is special case - stuff 011 to register field and 111 to the mode field
                                tok++;
                        }
@@ -467,8 +470,7 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
                        else if (*tok == ']')
                        {
                                // PC and Xn is suppressed
-                               AnREG = 6 << 3;         // stuff 110 to mode field
-                               //AnEXTEN|=EXT_BS|EXT_IS;
+                               AnREG = 6 << 3;         // stuff 110b to mode field
                                AnEXTEN |= EXT_BS;
                        }
                        else
@@ -479,16 +481,16 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
                        // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
                        if (*tok == ']')
                        {
-                               //([bd,An/PC],Xn,od)
+                               // ([bd,An/PC],Xn,od)
                                // Check for Xn
                                tok++;
 
                                if (*tok == ')')
                                {
-                                       //Xn and od are non existent, get out of jail free card
+                                       // Xn and od are non existent, get out of jail free card
                                        tok++;
-                                       AMn = MEMPRE;                   // ([bc,An,Xn],od) with no Xn and od
-                                       AnEXTEN |= EXT_IS | EXT_IISPREN;        //Suppress Xn and od
+                                       AMn = MEMPRE + ea_PC;                           // ([bc,An,Xn],od) with no Xn and od
+                                       AnEXTEN |= EXT_IS | EXT_IISPREN;        // Suppress Xn and od
                                        goto AnOK;
                                }
                                else if (*tok != ',')
@@ -510,13 +512,13 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
                                }
                                else
                                {
-                                       //No index found, suppress it
+                                       // No index found, suppress it
                                        AnEXTEN |= EXT_IS;
                                        tok--;                                  // Rewind tok to point to the comma
                                        goto IS_SUPPRESSEDn;    // https://xkcd.com/292/ - what does he know anyway?
                                }
 
-                               // Check for size
+                               // Check for Xn size
                                // ([bd,An/PC],Xn.W/L...)
                                switch ((int)*tok)
                                {
@@ -524,8 +526,6 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
                                case DOTW:
                                        tok++;
                                        break;
-                               default:
-                                       break;
                                case DOTL:
                                        AnEXTEN |= EXT_L;
                                        tok++;
@@ -533,9 +533,11 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
                                case DOTB:
                                        // .B not allowed here...
                                        goto badmode;
+                               default:
+                                       break;
                                }
 
-                               // Check for scale
+                               // Check for Xn scale
                                if (*tok == '*')                   // ([bd,An/PC],Xn*...)
                                {                                  // scale: *1, *2, *4, *8
                                        tok++;
@@ -544,54 +546,39 @@ AMn_IXN:                 // Handle any indexed (tok -> a comma)
                                        {
                                                if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
                                                        return error("scale factor expression must evaluate");
-
-                                               switch (scaleval)
-                                               {
-                                               case 1:
-                                                       break;
-                                               case 2:
-                                                       AnIXSIZ |= TIMES2;
-                                                       break;
-                                               case 4:
-                                                       AnIXSIZ |= TIMES4;
-                                                       break;
-                                               case 8:
-                                                       AnIXSIZ |= TIMES8;
-                                                       break;
-                                               default:
-                                                       goto badmode;
-                                               }
                                        }
-                                       else if (*tok++ != CONST)
-                                               goto badmode;
-                                       else
+                                       else if (*tok == CONST)
                                        {
-                                               switch ((int)*tok++)
-                                               {
-                                               case 1:
-                                                       break;
-                                               case 2:
-                                                       AnIXSIZ |= TIMES2;
-                                                       break;
-                                               case 4:
-                                                       AnIXSIZ |= TIMES4;
-                                                       break;
-                                               case 8:
-                                                       AnIXSIZ |= TIMES8;
-                                                       break;
-                                               default:
-                                                       goto badmode;
-                                               }
-
+                                               scaleval = (int)*tok++;
                                                tok++;  // Take into account that constants are 64-bit
                                        }
+                                       else
+                                               goto badmode;
+
+                                       switch (scaleval)
+                                       {
+                                       case 1:
+                                               break;
+                                       case 2:
+                                               AnIXSIZ |= TIMES2;
+                                               break;
+                                       case 4:
+                                               AnIXSIZ |= TIMES4;
+                                               break;
+                                       case 8:
+                                               AnIXSIZ |= TIMES8;
+                                               break;
+                                       default:
+                                               goto badmode;
+                                       }
+
                                }
 
                                // Check for od
                                if (*tok == ')')        // ([bd,An/PC],Xn)
                                {
                                        // od is non existent, get out of jail free card
-                                       AMn = MEMPOST;          // let's say it's ([bd,An],Xn,od) with od=0 then
+                                       AMn = MEMPOST + ea_PC;  // let's say it's ([bd,An],Xn,od) with od=0 then
                                        AnEXTEN |= EXT_IISPOSN; // No outer displacement
                                        tok++;
                                        goto AnOK;
@@ -605,25 +592,39 @@ CHECKODn:
                                if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
                                        goto badmode;
 
-                               if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXATTR & DEFINED) && (AnEXVAL == 0))
+                               if (CHECK_OPTS(OPT_020_DISP) && (AnEXATTR & DEFINED) && (AnEXVAL == 0))
                                {
-                                       // od=0 so optimise it out
-                                       AMn = MEMPOST          // let's say it's ([bd,An],Xn,od) with od=0 then
-                                       AnEXTEN |= EXT_IISPOSN; // No outer displacement
+                                       // od = 0 so optimise it out
+                                       AMn = MEMPOST + ea_PC;          // let's say it's ([bd,An],Xn,od) with od=0 then
+                                       AnEXTEN |= EXT_IISPOSN;         // No outer displacement
                                        tok++;
                                        goto AnOK;
                                }
 
                                // ([bd,An/PC],Xn,od)
-                               if (*tok == DOTL)
+                               // Is .W forced here?
+                               if (*tok == DOTW)
                                {
-                                       // expr.L
+                                       tok++;
+                                       // od[.W]
+                                       AnEXTEN |= EXT_IISPOSW; // Word outer displacement
+                                       AMn = MEMPOST + ea_PC;
+                               }
+                               else
+                               {
+                                       // Is .L forced here?
+                                       if (*tok == DOTL)
+                                               tok++;                          // Doesn't matter, we're going for .L anyway
+
+                                       WORD od_ea = 0;
+
+                                       // od.L
                                        if (!(AnEXTEN & EXT_BS))
-                                               AnEXTEN |= EXT_IISPOSL; // Long outer displacement
+                                               od_ea = EXT_IISPOSL; // Long outer displacement
                                        else
                                        {
                                                // bd is suppressed, so sticking the od size in bd
-                                               AnEXTEN |= EXT_BDSIZEL;
+                                               od_ea = EXT_BDSIZEL;
                                                // And of course the expression has to be copied to
                                                // AnBEXPR instead of AnEXPR. Yay. :-/
                                                int i = 0;
@@ -638,30 +639,21 @@ CHECKODn:
                                                AnBEXPR[i] = 'E';
                                        }
 
-                                       AMn = MEMPOST;
-                                       tok++;
+                                       AMn = MEMPOST + ea_PC;
 
                                        // Defined, absolute values from $FFFF8000..$00007FFF get
                                        // optimized to absolute short
-                                       if (CHECK_OPTS(OPT_ABS_SHORT)
+                                       if (CHECK_OPTS(OPT_020_DISP)
                                                && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
                                                && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
                                        {
-                                               AnEXTEN |= EXT_IISPOSW; // Word outer displacement
-                                               AMn = MEMPOST;
-                                               warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
-                                       }
+                                               od_ea = EXT_IISPOSW; // Word outer displacement
+                                               AMn = MEMPOST + ea_PC;
 
-                               }
-                               else
-                               {
-                                       // expr[.W]
-                                       AnEXTEN |= EXT_IISPOSW; // Word outer displacement
-                                       AMn = MEMPOST;
-
-                                       // Is .W forced here?
-                                       if (*tok == DOTW)
-                                               tok++;
+                                               if (optim_warn_flag)
+                                                       warn("absolute value in outer displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
+                                       }
+                                       AnEXTEN |= od_ea;
                                }
 
                                // Check for final closing parenthesis
@@ -679,7 +671,7 @@ IS_SUPPRESSEDn:
                                if (*tok == ')')        // ([bd,An/PC],Xn)
                                {
                                        // od is non existent, get out of jail free card
-                                       AMn = MEMPOST;          // let's say it's ([bd,An],Xn,od) with od=0 then
+                                       AMn = MEMPOST + ea_PC;          // let's say it's ([bd,An],Xn,od) with od=0 then
                                        AnEXTEN |= EXT_IISNOIN; // No outer displacement
                                        tok++;
                                        goto AnOK;
@@ -694,10 +686,10 @@ IS_SUPPRESSEDn:
 
                                expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
 
-                               if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0))
+                               if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0))
                                {
                                        // od=0 so optimise it out
-                                       AMn = MEMPOST;           // let's say it's ([bd,An],Xn,od) with od=0 then
+                                       AMn = MEMPOST + ea_PC;           // let's say it's ([bd,An],Xn,od) with od=0 then
                                        AnEXTEN |= EXT_IISNOIN; // No outer displacement
                                        tok++;
                                        goto AnOK;
@@ -708,29 +700,30 @@ IS_SUPPRESSEDn:
                                {
                                        // expr.L
                                        tok++;
-                                       AMn = MEMPOST;
+                                       AMn = MEMPOST + ea_PC;
                                        AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
                                }
                                else
                                {
                                        // expr[.W][]
                                        AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
-                                       AMn = MEMPRE;
+                                       AMn = MEMPRE + ea_PC;;
 
                                        if (*tok == DOTW)
                                        {
                                                //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
-                                               AMn = MEMPOST;
+                                               AMn = MEMPOST + ea_PC;
                                                tok++;
                                        }
                                        // Defined, absolute values from $FFFF8000..$00007FFF get
                                        // optimized to absolute short
-                                       else if (CHECK_OPTS(OPT_BASE_DISP)
+                                       else if (CHECK_OPTS(OPT_020_DISP)
                                                && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
                                                && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
                                        {
                                                //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
-                                               warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
+                                               if (optim_warn_flag)
+                                                       warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
                                        }
                                }
 
@@ -844,8 +837,7 @@ IS_SUPPRESSEDn:
                                if (*tok == ')')        // ([bd,An/PC,Xn]...
                                {
                                        // od is non existent, get out of jail free card
-                                       //AnEXVAL=0;            // zero outer displacement
-                                       AMn = MEMPRE;                   // let's say it's ([bd,An,Xn],od) with od suppressed then
+                                       AMn = MEMPRE + ea_PC;                   // let's say it's ([bd,An,Xn],od) with od suppressed then
                                        AnEXTEN |= EXT_IISPREN; // No outer displacement
                                        tok++;
                                        goto AnOK;
@@ -858,10 +850,10 @@ IS_SUPPRESSEDn:
                                        if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
                                                goto badmode;
 
-                                       if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
+                                       if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
                                        {
                                                // od=0 so optimise it out
-                                               AMn = MEMPRE;            // let's say it's ([bd,An],Xn,od) with od=0 then
+                                               AMn = MEMPRE + ea_PC;            // let's say it's ([bd,An],Xn,od) with od=0 then
                                                AnEXTEN |= EXT_IISPRE0; // No outer displacement
                                                tok++;
                                                goto AnOK;
@@ -872,14 +864,14 @@ IS_SUPPRESSEDn:
                                if (*tok == DOTL)
                                {
                                        // expr.L
-                                       AMn = MEMPRE;
+                                       AMn = MEMPRE + ea_PC;
                                        tok++;
                                        AnEXTEN |= EXT_IISPREL;
                                }
                                else
                                {
                                        // expr.[W]
-                                       AMn = MEMPRE;
+                                       AMn = MEMPRE + ea_PC;
                                        int expr_size = EXT_IISPREW; // Assume we have a .w value
 
                                        if ((AnEXVAL + 0x8000) > 0x10000)
@@ -889,12 +881,14 @@ IS_SUPPRESSEDn:
 
                                                // Defined, absolute values from $FFFF8000..$00007FFF
                                                // get optimized to absolute short
-                                               if (CHECK_OPTS(OPT_BASE_DISP)
+                                               if (CHECK_OPTS(OPT_020_DISP)
                                                        && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
                                                        && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
                                                {
                                                        expr_size = EXT_IISPREW;
-                                                       warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
+
+                                                       if (optim_warn_flag)
+                                                               warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
                                                }
                                        }
 
@@ -968,7 +962,6 @@ IS_SUPPRESSEDn:
                                                // Our expression is techically a base displacement,
                                                // so let's copy it to the relevant variables so
                                                // eagen0.c can pick it up properly
-                                               //AnBEXPR = AnEXPR;
                                                AnBEXVAL = AnEXVAL;
                                                AnBEXATTR = AnEXATTR;
 
@@ -995,7 +988,7 @@ IS_SUPPRESSEDn:
                                                                }
                                                        }
                                                        // Check for scale
-                                                       if (*tok == '*')                // ([bd,An/PC],Xn*...)
+                                                       if (*tok == '*')                // (d16,An,Dn[.size][*scale])
                                                        {                                               // scale: *1, *2, *4, *8
                                                                tok++;
 
@@ -1169,24 +1162,31 @@ CHK_FOR_DISPn:
                        // expr[.L]
                        AMn = ABSL;
 
-                       // Defined, absolute values from $FFFF8000..$00007FFF get optimized
-                       // to absolute short
-                       if (CHECK_OPTS(OPT_ABS_SHORT)
-                               && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
-                               && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
-                       {
-                               AMn = ABSW;
-
-                               if (sbra_flag)
-                                       warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
-                       }
+                       // When PC relative is enforced, check for any symbols that aren't
+                       // EQU'd, in this case it's an illegal mode
+                       if ((CHECK_OPTS(OPT_PC_RELATIVE)) && (AnEXATTR & REFERENCED) && (AnEXATTR & DEFINED) && (!(AnEXATTR & EQUATED)))
+                               return error("relocation not allowed");
 
-                       // Is .L forced here?
+                       // .L is forced here
                        if (*tok == DOTL)
                        {
                                tok++;
                                AMn = ABSL;
                        }
+                       else
+                       {
+                               // Defined, absolute values from $FFFF8000..$00007FFF get
+                               // optimized to absolute short
+                               if (CHECK_OPTS(OPT_ABS_SHORT)
+                                       && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
+                                       && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
+                               {
+                                       AMn = ABSW;
+
+                                       if (optim_warn_flag)
+                                               warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
+                               }
+                       }
 
                        goto AnOK;
                }