EOY minor update.
[rmac] / parmode.h
index 5ad5da90e1240e21bcd37da69a889228af048082..ca313efb1bd8dbcff02169303831d0c8bc4f6bc8 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-2017 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
 //
@@ -9,6 +9,11 @@
 // This file is included (twice) to parse two addressing modes, into slightly
 // different var names
 {
+       uint64_t scaleval;                      // Expression's value
+       TOKEN scaleexpr[EXPRSIZE];      // Expression
+       WORD scaleattr;                         // Expression's attribute
+       SYM * scaleesym;                        // External symbol involved in expr
+
        // Dn
        // An
        // # expression
        // (d8,An,Xn[.siz][*scale])
        // (d16,PC)
        // (d8,PC,Xn[.siz][*scale])
-       // ([bd,An],Xn,od)
-       // ([bd,An,Xn],od)
-       // ([bd,PC],Xn,od)
-       // ([bd,PC,Xn],od)
+       // ([bd,An],Xn[.siz][*scale],od)
+       // ([bd,An,Xn[.siz][*scale]],od)
+       // ([bd,PC],Xn[.siz][*scale],od)
+       // ([bd,PC,Xn[.siz][*scale]],od)
        else if (*tok == '(')
        {
                tok++;
@@ -78,6 +83,7 @@
                {
                        //Since index register isn't used here, store register number in this field
                        AnIXREG = *tok++ & 7;                                // (Dn)
+
                        if (*tok == ')')
                        {
                                tok++;
                                AnEXTEN |= EXT_BS;          // Base register suppressed
                                AnEXTEN |= EXT_BDSIZE0;     // Base displacement null
                                AnEXTEN |= EXT_IISPOSN;     // Indirect Postindexed with Null Outer Displacement
-                               AMn= MEMPOST;
+                               AMn = MEMPOST;
                                AnREG = 6 << 3;         // stuff 110 to mode field
                                goto AnOK;
                        }
                        else if (*tok == 'L')
                        {
-                               // TODO: does DINDL gets used at all?
-                               //AMn=DINDL;                                     // (Dn.l)
-                               //AnEXTEN = 1 << 1;   // Long index size
-                               //tok++;
+                               AMn = DINDL;                                                                     // (Dn.l)
+                               AnEXTEN = 1 << 11;   // Long index size
+                               tok++;
                        }
                        else if (*tok == 'W')                                // (Dn.w)
                        {
-                               // TODO: does DINDW gets used at all?
-                               //AMn=DINDW;
-                               //AnEXTEN = 1 << 1;   // Word index size
-                               //tok++;
+                               AMn = DINDW;
+                               AnEXTEN = 0 << 11;   // Word index size
+                               tok++;
                        }
                        else if (*tok == ',')
                        {
                                goto CHECKODn;
                        }
                        else
+                       {
                                return error("(Dn) error");
+                       }
+
+                       if (*tok == '*')
+                       {                        // scale: *1, *2, *4, *8
+                               tok++;
+
+                               if (*tok == SYMBOL)
+                               {
+                                       if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
+                                               return error("scale factor expression must evaluate");
+
+                                       switch (scaleval)
+                                       {
+                                       case 1:
+                                               break;
+                                       case 2:
+                                               AnIXSIZ |= TIMES2;
+                                               AnEXTEN |= 1 << 9;
+                                               break;
+                                       case 4:
+                                               AnIXSIZ |= TIMES4;
+                                               AnEXTEN |= 2 << 9;
+                                               break;
+                                       case 8:
+                                               AnIXSIZ |= TIMES8;
+                                               AnEXTEN |= 3 << 9;
+                                               break;
+                                       default:
+                                               goto badmode;
+                                       }
+                               }
+                               else if (*tok++ != CONST)
+                                       goto badmode;
+                               else
+                               {
+                                       switch ((int)*tok++)
+                                       {
+                                       case 1:
+                                               break;
+                                       case 2:
+                                               AnIXSIZ |= TIMES2;
+                                               AnEXTEN |= 1 << 9;
+                                               break;
+                                       case 4:
+                                               AnIXSIZ |= TIMES4;
+                                               AnEXTEN |= 2 << 9;
+                                               break;
+                                       case 8:
+                                               AnIXSIZ |= TIMES8;
+                                               AnEXTEN |= 3 << 9;
+                                               break;
+                                       default:
+                                               goto badmode;
+                                       }
+
+                                       tok++;  // Take into account that constants are 64-bit
+                               }
+                       }
 
                        if (*tok == ')')
                        {
                                AMn = MEMPOST;
                                goto AnOK;
                        }
+                       else if (*tok == ',')
+                       {
+                               tok++;  // eat the comma
+                               // It might be (Dn[.wl][*scale],od)
+                               // Maybe this is wrong and we have to write some code here
+                               // instead of reusing that path...
+                               AnEXTEN |= EXT_FULLWORD;        // Definitely using full extension format, so set bit 8
+                               AnEXTEN |= EXT_BS;       // Base displacement null - suppressed
+                               AnEXTEN |= AnIXREG << 12;
+                               goto CHECKODn;
+                       }
                        else
                                return error("unhandled so far");
                }
                        // Common index handler; enter here with 'tok' pointing at the
                        // comma.
 
-                       AMn_IX0:                 // Handle indexed with missing expr
+AMn_IX0:                 // Handle indexed with missing expr
 
                        AnEXVAL = 0;
                        AnEXATTR = ABS | DEFINED;
 
-                       AMn_IXN:                 // Handle any indexed (tok -> a comma)
+AMn_IXN:                 // Handle any indexed (tok -> a comma)
 
                        if (*tok++ != ',')
                                goto badmode;
                        {                        // scale: *1, *2, *4, *8
                                tok++;
 
-                               if (*tok++ != CONST || *tok > 8)
-                                       goto badmode;
-
-                               switch ((int)*tok++)
+                               if (*tok == SYMBOL)
                                {
-                               case 1:
-                                       break;
-                               case 2:
-                                       AnIXSIZ |= TIMES2;
-                                       break;
-                               case 4:
-                                       AnIXSIZ |= TIMES4;
-                                       break;
-                               case 8:
-                                       AnIXSIZ |= TIMES8;
-                                       break;
-                               default:
+                                       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
+                               {
+                                       switch ((int)*tok++)
+                                       {
+                                       case 1:
+                                               break;
+                                       case 2:
+                                               AnIXSIZ |= TIMES2;
+                                               break;
+                                       case 4:
+                                               AnIXSIZ |= TIMES4;
+                                               break;
+                                       case 8:
+                                               AnIXSIZ |= TIMES8;
+                                               break;
+                                       default:
+                                               goto badmode;
+                                       }
+
+                                       tok++;  // Take into account that constants are 64-bit
                                }
                        }
 
+                       if (*tok == ',')
+                       {
+                               // If we got here we didn't get any [] stuff
+                               // so let's suppress base displacement before
+                               // branching off
+                               tok++;
+                               AnEXTEN |= EXT_BDSIZE0;     // Base displacement null - suppressed
+                               goto CHECKODn;
+                       }
                        if (*tok++ != ')')         // final ")"
                                goto badmode;
 
                else if (*tok == '[')
                {                              // ([...
                        tok++;
-                       AnEXTEN|=EXT_FULLWORD;     //Definitely using full extension format, so set bit 8
+                       AnEXTEN |= EXT_FULLWORD;     // Definitely using full extension format, so set bit 8
+
                        // Check to see if base displacement is present
-                       //WARNING("expr will return a bad expression error here but this is expected, it needs to be silenced!");
-                       if (*tok!=CONST && *tok !=SYMBOL)
-                       //if (expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM) != OK)
+                       if (*tok != CONST && *tok != SYMBOL)
                        {
-                               AnEXTEN|=EXT_BDSIZE0;
-                               //tok++;
-                               //tok--;                 //Rewind tok since expr advances it forward
+                               AnEXTEN |= EXT_BDSIZE0;
                        }
                        else
                        {
                                expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
-                               if (optim_flags[OPT_BASE_DISP] && AnBEXVAL==0 && AnEXATTR!=0)
+
+                               if (CHECK_OPTS(OPT_BASE_DISP) && AnBEXVAL == 0 && AnEXATTR != 0)
                                {
                                        // bd=0 so let's optimise it out
                                        AnEXTEN|=EXT_BDSIZE0;
                                }
-                               else if (*tok==DOTL)
-                               {                                                  // ([bd.l,...
-                                               AnEXTEN|=EXT_BDSIZEL;
-                                               tok++;
+                               else if (*tok == DOTL)
+                               {
+                                       // ([bd.l,...
+                                       AnEXTEN |= EXT_BDSIZEL;
+                                       tok++;
                                }
                                else
-                               {                                                  // ([bd[.w],... or ([bd,...
+                               {
+                                       // ([bd[.w],... or ([bd,...
                                        // Is .W forced here?
                                        if (*tok == DOTW)
                                        {
-                                       AnEXTEN|=EXT_BDSIZEW;
+                                               AnEXTEN |= EXT_BDSIZEW;
                                                tok++;
                                        }
                                        else
                                        {
-                                               // Defined, absolute values from $FFFF8000..$00007FFF get optimized
-                                               // to absolute short
-                                               if (optim_flags[OPT_ABS_SHORT]
+                                               // Defined, absolute values from $FFFF8000..$00007FFF
+                                               // get optimized to absolute short
+                                               if (CHECK_OPTS(OPT_ABS_SHORT)
                                                        && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
-                                                       && ((AnBEXVAL + 0x8000) < 0x10000))
+                                                       && (((uint32_t)AnBEXVAL + 0x8000) < 0x10000))
                                                {
                                                        AnEXTEN |= EXT_BDSIZEW;
                                                        warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
                        }
                        else if ((*tok >= KW_A0) && (*tok <= KW_A7))
                        {                                       // ([bd,An,...
-                               AnREG = (6<<3)|*tok & 7;
+                               AnREG = (6 << 3) | (*tok & 7);
                                tok++;
                        }
                        else if ((*tok >= KW_D0) && (*tok <= KW_D7))
                                tok++;
 
                                // Check for size
+                               // ([bd,An/PC],Xn.W/L...)
+                               switch ((int)*tok)
                                {
-                                       // ([bd,An/PC],Xn.W/L...)
-                                       switch ((int)*tok)
-                                       {
-                                       // Index reg size: <empty> | .W | .L
-                                       case DOTW:
-                                               tok++;
-                                               break;
-                                       default:
-                                               break;
-                                       case DOTL:
-                                               AnEXTEN |= EXT_L;
-                                               tok++;
-                                               break;
-                                       case DOTB:
-                                               // .B not allowed here...
-                                               goto badmode;
-                                       }
+                               // Index reg size: <empty> | .W | .L
+                               case DOTW:
+                                       tok++;
+                                       break;
+                               default:
+                                       break;
+                               case DOTL:
+                                       AnEXTEN |= EXT_L;
+                                       tok++;
+                                       break;
+                               case DOTB:
+                                       // .B not allowed here...
+                                       goto badmode;
                                }
 
                                // Check for scale
                                if (*tok == '*')                        // ([bd,An/PC],Xn*...)
-                               {
+                               {                           // scale: *1, *2, *4, *8
                                        tok++;
 
-                                       if (*tok == CONST)      // TODO: I suppose the scale is stored as a CONST and nothing else? So prolly the if is not needed?
-                                               tok++;
-
-                                       switch ((int)*tok++)
+                                       if (*tok == SYMBOL)
                                        {
-                                       case 1:
-                                               break;
-                                       case 2:
-                                               AnEXTEN |= EXT_TIMES2;
-                                               break;
-                                       case 4:
-                                               AnEXTEN |= EXT_TIMES4;
-                                               break;
-                                       case 8:
-                                               AnEXTEN |= EXT_TIMES8;
-                                               break;
-                                       default:
+                                               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
+                                       {
+                                               switch ((int)*tok++)
+                                               {
+                                               case 1:
+                                                       break;
+                                               case 2:
+                                                       AnIXSIZ |= TIMES2;
+                                                       break;
+                                               case 4:
+                                                       AnIXSIZ |= TIMES4;
+                                                       break;
+                                               case 8:
+                                                       AnIXSIZ |= TIMES8;
+                                                       break;
+                                               default:
+                                                       goto badmode;
+                                               }
+
+                                               tok++;  // Take into account that constants are 64-bit
                                        }
                                }
-
                                if (*tok == ']')  // ([bd,Dn]...
                                {
                                        tok++;
                                if (*tok == ')')
                                {
                                        //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
-                                       tok++;
                                        goto AnOK;
                                }
                                else if (*tok != ',')
                                {
                                        //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?
+                                       tok--;                                  // Rewind tok to point to the comma
+                                       goto IS_SUPPRESSEDn;    // https://xkcd.com/292/ - what does he know anyway?
                                }
 
                                // Check for size
+                               // ([bd,An/PC],Xn.W/L...)
+                               switch ((int)*tok)
                                {
-                                       // ([bd,An/PC],Xn.W/L...)
-                                       switch ((int)*tok)
-                                       {
-                                       // Index reg size: <empty> | .W | .L
-                                       case DOTW:
-                                               tok++;
-                                               break;
-                                       default:
-                                               break;
-                                       case DOTL:
-                                               AnEXTEN |= EXT_L;
-                                               tok++;
-                                               break;
-                                       case DOTB:
-                                               // .B not allowed here...
-                                               goto badmode;
-                                       }
+                               // Index reg size: <empty> | .W | .L
+                               case DOTW:
+                                       tok++;
+                                       break;
+                               default:
+                                       break;
+                               case DOTL:
+                                       AnEXTEN |= EXT_L;
+                                       tok++;
+                                       break;
+                               case DOTB:
+                                       // .B not allowed here...
+                                       goto badmode;
                                }
 
                                // Check for scale
-                               if (*tok == '*')
-                               {
-                                       // ([bd,An/PC],Xn*...)
+                               if (*tok == '*')                   // ([bd,An/PC],Xn*...)
+                               {                                  // scale: *1, *2, *4, *8
                                        tok++;
 
-                                       if (*tok == CONST)      // TODO: I suppose the scale is stored as a CONST and nothing else? So prolly the if is not needed?
-                                               tok++;
-
-                                       switch ((int)*tok++)
+                                       if (*tok == SYMBOL)
                                        {
-                                       case 1:
-                                               break;
-                                       case 2:
-                                               AnEXTEN |= EXT_TIMES2;
-                                               break;
-                                       case 4:
-                                               AnEXTEN |= EXT_TIMES4;
-                                               break;
-                                       case 8:
-                                               AnEXTEN |= EXT_TIMES8;
-                                               break;
-                                       default:
+                                               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
+                                       {
+                                               switch ((int)*tok++)
+                                               {
+                                               case 1:
+                                                       break;
+                                               case 2:
+                                                       AnIXSIZ |= TIMES2;
+                                                       break;
+                                               case 4:
+                                                       AnIXSIZ |= TIMES4;
+                                                       break;
+                                               case 8:
+                                                       AnIXSIZ |= TIMES8;
+                                                       break;
+                                               default:
+                                                       goto badmode;
+                                               }
+
+                                               tok++;  // Take into account that constants are 64-bit
                                        }
                                }
 
                                // Check for od
                                if (*tok == ')')        // ([bd,An/PC],Xn)
                                {
-                                       //od is non existant, get out of jail free card
+                                       // 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
                                        AnEXTEN |= EXT_IISPOSN; // No outer displacement
                                        tok++;
                                        goto AnOK;
                                }
-                               else if (*tok!=',')
+                               else if (*tok != ',')
                                        return error("comma expected");
                                else
                                        tok++;  // eat the comma
 
-                               CHECKODn:
-
+CHECKODn:
                                if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
                                        goto badmode;
 
-                               if (optim_flags[OPT_BASE_DISP] && (AnEXVAL == 0))
+                               if (CHECK_OPTS(OPT_BASE_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
                                if (*tok == DOTL)
                                {
                                        // expr.L
-                                       AnEXTEN |= EXT_IISPOSL; // Long outer displacement
+                                       if (!(AnEXTEN & EXT_BS))
+                                               AnEXTEN |= EXT_IISPOSL; // Long outer displacement
+                                       else
+                                       {
+                                               // bd is suppressed, so sticking the od size in bd
+                                               AnEXTEN |= EXT_BDSIZEL;
+                                               // And of course the expression has to be copied to
+                                               // AnBEXPR instead of AnEXPR. Yay. :-/
+                                               int i = 0;
+
+                                               do
+                                               {
+                                                       AnBEXPR[i] = AnEXPR[i];
+                                                       i++;
+                                               }
+                                               while (AnEXPR[i] != 'E');
+
+                                               AnBEXPR[i] = 'E';
+                                       }
+
                                        AMn = MEMPOST;
+                                       tok++;
 
                                        // Defined, absolute values from $FFFF8000..$00007FFF get
                                        // optimized to absolute short
-                                       if (optim_flags[OPT_ABS_SHORT]
+                                       if (CHECK_OPTS(OPT_ABS_SHORT)
                                                && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
-                                               && ((AnEXVAL + 0x8000) < 0x10000))
+                                               && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
                                        {
                                                AnEXTEN |= EXT_IISPOSW; // Word outer displacement
                                                AMn = MEMPOST;
 
                                        // Is .W forced here?
                                        if (*tok == DOTW)
-                                       {
                                                tok++;
-                                       }
                                }
 
                                // Check for final closing parenthesis
                                else
                                        return error("Closing parenthesis missing on addressing mode");
 
-                               IS_SUPPRESSEDn:
+IS_SUPPRESSEDn:
 
                                // Check for od
                                if (*tok == ')')        // ([bd,An/PC],Xn)
                                {
-                                       //od is non existant, get out of jail free card
+                                       // 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
                                        AnEXTEN |= EXT_IISNOIN; // No outer displacement
                                        tok++;
                                else
                                        tok++;  // eat the comma
 
-                if ((*tok != CONST) && (*tok != SYMBOL))
+                               if ((*tok != CONST) && (*tok != SYMBOL))
                                        goto badmode;
 
                                expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
 
-                               if (optim_flags[OPT_BASE_DISP] && (AnEXVAL == 0))
+                               if (CHECK_OPTS(OPT_BASE_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;
                                                tok++;
                                        }
-
                                        // Defined, absolute values from $FFFF8000..$00007FFF get
                                        // optimized to absolute short
-                                       else if (optim_flags[OPT_BASE_DISP]
+                                       else if (CHECK_OPTS(OPT_BASE_DISP)
                                                && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
-                                               && ((AnEXVAL + 0x8000) < 0x10000))
+                                               && (((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");
                        }
                        else if (*tok == ',')
                        {
-                               *tok++;                 // ([bd,An,Xn.size*scale],od)
+                               tok++;                  // ([bd,An,Xn.size*scale],od)
 
-                               //Check for Xn
+                               // Check for Xn
                                if ((*tok >= KW_A0) && (*tok <= KW_A7))
                                {
                                        AnEXTEN |= ((*tok & 7) << 12);
                                }
 
                                // Check for size
+                               // ([bd,An/PC],Xn.W/L...)
+                               switch ((int)*tok)
                                {
-                                       // ([bd,An/PC],Xn.W/L...)
-                                       switch ((int)*tok)
-                                       {
-                                       // Index reg size: <empty> | .W | .L
-                                       case DOTW:
-                                               tok++;
-                                               break;
-                                       default:
-                                               break;
-                                       case DOTL:
-                                               tok++;
-                                               AnEXTEN |= EXT_L;
-                                               break;
-                                       case DOTB:
-                                               // .B not allowed here...
-                                               goto badmode;
-                                       }
+                               // Index reg size: <empty> | .W | .L
+                               case DOTW:
+                                       tok++;
+                                       break;
+                               default:
+                                       break;
+                               case DOTL:
+                                       tok++;
+                                       AnEXTEN |= EXT_L;
+                                       break;
+                               case DOTB:
+                               // .B not allowed here...
+                               goto badmode;
                                }
 
                                // Check for scale
-                               if (*tok == '*')                        // ([bd,An/PC],Xn*...)
-                               {
+                               if (*tok == '*')                                // ([bd,An/PC],Xn*...)
+                               {                                                               // scale: *1, *2, *4, *8
                                        tok++;
 
-                                       if (*tok == CONST)      // TODO: I suppose the scale is stored as a CONST and nothing else? So prolly the if is not needed?
-                                               tok++;
-
-                                       switch ((int)*tok++)
+                                       if (*tok == SYMBOL)
                                        {
-                                       case 1:
-                                               break;
-                                       case 2:
-                                               AnEXTEN |= EXT_TIMES2;
-                                               break;
-                                       case 4:
-                                               AnEXTEN |= EXT_TIMES4;
-                                               break;
-                                       case 8:
-                                               AnEXTEN |= EXT_TIMES8;
-                                               break;
-                                       default:
+                                               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
+                                       {
+                                               switch ((int)*tok++)
+                                               {
+                                               case 1:
+                                                       break;
+                                               case 2:
+                                                       AnIXSIZ |= TIMES2;
+                                                       AnEXTEN |= 1 << 9;
+                                                       break;
+                                               case 4:
+                                                       AnIXSIZ |= TIMES4;
+                                                       AnEXTEN |= 2 << 9;
+                                                       break;
+                                               case 8:
+                                                       AnIXSIZ |= TIMES8;
+                                                       AnEXTEN |= 3 << 9;
+                                                       break;
+                                               default:
+                                                       goto badmode;
+                                               }
+
+                                               tok++;  // Take into account that constants are 64-bit
                                        }
                                }
 
-                               //Check for ]
+                               // Check for ]
                                if (*tok != ']')
                                        return error("Expected closing bracket ]");
-
                                tok++;                  // Eat the bracket
 
-                               //Check for od
+                               // Check for od
                                if (*tok == ')')        // ([bd,An/PC,Xn]...
                                {
-                                       //od is non existant, get out of jail free card
+                                       // 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
                                        AnEXTEN |= EXT_IISPREN; // No outer displacement
                                        tok++;
                                        goto AnOK;
                                }
-                               else if (*tok++!=',')
+                               else if (*tok++ != ',')
                                        return error("comma expected after ]");
 
-                               WARNING(Put symbol and constant checks here!)
-
-                               if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
-                                       goto badmode;
-
-                               if (optim_flags[OPT_BASE_DISP] && (AnEXVAL == 0))
+                               if (*tok == SYMBOL || *tok == CONST)
                                {
-                                       // od=0 so optimise it out
-                                       AMn = MEMPRE;            // let's say it's ([bd,An],Xn,od) with od=0 then
-                                       AnEXTEN |= EXT_IISPRE0; // No outer displacement
-                                       tok++;
-                                       goto AnOK;
+                                       if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
+                                               goto badmode;
+
+                                       if (CHECK_OPTS(OPT_BASE_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
+                                               AnEXTEN |= EXT_IISPRE0; // No outer displacement
+                                               tok++;
+                                               goto AnOK;
+                                       }
                                }
 
                                // ([bd,An/PC,Xn],od)
                                else
                                {
                                        // expr.[W]
-                                       //tok++;
-
-                                       AnEXTEN |= EXT_IISPREW;
                                        AMn = MEMPRE;
+                                       int expr_size = EXT_IISPREW; // Assume we have a .w value
 
-                    // Is .W forced here?
-                                       if (*tok == DOTW)
+                                       if ((AnEXVAL + 0x8000) > 0x10000)
                                        {
-                                               tok++;
+                                               // Long value, so mark it as such for now
+                                               expr_size = EXT_IISPREL;
+
+                                               // Defined, absolute values from $FFFF8000..$00007FFF
+                                               // get optimized to absolute short
+                                               if (CHECK_OPTS(OPT_BASE_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");
+                                               }
                                        }
 
-                                       // Defined, absolute values from $FFFF8000..$00007FFF get optimized
-                                       // to absolute short
-                                       else if (optim_flags[OPT_BASE_DISP]
-                                               && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
-                                               && ((AnEXVAL + 0x8000) < 0x10000))
+                                       AnEXTEN |= expr_size; // Assume we have a .w value
+
+                                       // Is .W forced here?
+                                       if (*tok == DOTW)
                                        {
-                                               AnEXTEN |= EXT_IISPREW;
-                                               warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
+                                               tok++;
+
+                                               if (expr_size == EXT_IISPREL)
+                                                       return error("outer displacement value does not fit in .w size");
                                        }
                                }
 
 
                                if (*tok == ',')
                                {
+                                       // Check if we're actually doing d8(An,Dn) or
+                                       // (d16,An,Dn[.size][*scale])
+                                       // TODO: not a very clear cut case from what I can think.
+                                       // The only way to distinguish between the two is to check
+                                       // AnEXVAL and see if it's >127 or <-128. But this doesn't
+                                       // work if AnEXVAL isn't defined yet. For now we fall
+                                       // through to d8(An,Dn) but this might bite us in the arse
+                                       // during fixups...
+                                       if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
+                                       {
+                                               // We're going to treat it as a full extension format
+                                               // with no indirect access and no base displacement/
+                                               // index register suppression
+                                               AnEXTEN |= EXT_FULLWORD;        // Definitely using full extension format, so set bit 8
+                                               AnEXTEN |= EXT_IISPRE0;         // No Memory Indirect Action
+                                               AnEXTEN |= EXT_BDSIZEL;         // Base Displacement Size Long
+                                               tok++;                                          // Get past the comma
+
+                                               // 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;
+
+                                               if ((*tok >= KW_D0) && (*tok <= KW_D7))
+                                               {
+                                                       AnEXTEN |= ((*tok++) & 7) << 12;
+                                                       // Check for size
+                                                       {
+                                                               switch ((int)*tok)
+                                                               {
+                                                               // Index reg size: <empty> | .W | .L
+                                                               case DOTW:
+                                                                       tok++;
+                                                                       break;
+                                                               default:
+                                                                       break;
+                                                               case DOTL:
+                                                                       tok++;
+                                                                       AnEXTEN |= EXT_L;
+                                                                       break;
+                                                               case DOTB:
+                                                                       // .B not allowed here...
+                                                                       goto badmode;
+                                                               }
+                                                       }
+                                                       // Check for scale
+                                                       if (*tok == '*')                // ([bd,An/PC],Xn*...)
+                                                       {                                               // scale: *1, *2, *4, *8
+                                                               tok++;
+
+                                                               if (*tok == SYMBOL)
+                                                               {
+                                                                       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
+                                                               {
+                                                                       switch ((int)*tok++)
+                                                                       {
+                                                                       case 1:
+                                                                               break;
+                                                                       case 2:
+                                                                               AnIXSIZ |= TIMES2;
+                                                                               break;
+                                                                       case 4:
+                                                                               AnIXSIZ |= TIMES4;
+                                                                               break;
+                                                                       case 8:
+                                                                               AnIXSIZ |= TIMES8;
+                                                                               break;
+                                                                       default:
+                                                                               goto badmode;
+                                                                       }
+
+                                                                       tok++;  // Take into account that constants are 64-bit
+                                                               }
+                                                       }
+
+                                                       if (*tok++ != ')')
+                                                               return error("Closing parenthesis missing on addressing mode");
+
+                                                       // Let's say that this is the closest to our case
+                                                       AMn = MEMPOST;
+                                                       goto AnOK;
+                                               }
+                                               else
+                                                       goto badmode;
+                                       }
+
                                        AMn = AINDEXED;
                                        goto AMn_IXN;
                                }
                                }
                                else if (*tok == ')')
                                {
-                                       AMn = PCDISP;                                // expr(PC)
+                                       AMn = PCDISP;             // expr(PC)
                                        tok++;
                                        goto AnOK;
                                }
        {
                AMn = AM_USP;
                tok++;
-               AnREG = 2;      //Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
+               AnREG = 2;      // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
                goto AnOK;
        }
        else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
                // After a cache keyword only a comma or EOL is allowed
                if ((*tok != ',') && (*tok != EOL))
                        return ERROR;
-
                goto AnOK;
        }
        else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
@@ -853,24 +1169,26 @@ CHK_FOR_DISPn:
                        // expr[.L]
                        AMn = ABSL;
 
-                       // Defined, absolute values from $FFFF8000..$00007FFF get optimized
-                       // to absolute short
-                       if (optim_flags[OPT_ABS_SHORT]
-                               && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
-                               && ((AnEXVAL + 0x8000) < 0x10000))
-                       {
-                               AMn = ABSW;
-
-                               if (sbra_flag)
-                                       warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
-                       }
-
-                       // 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 (sbra_flag)
+                                               warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
+                               }
+                       }
 
                        goto AnOK;
                }
@@ -903,6 +1221,7 @@ CHK_FOR_DISPn:
                        AMn = PCINDEXED;
                        goto AMn_IXN;
                }
+
                goto badmode;
        }