2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // PARMODE.C - Addressing Modes Parser Include
4 // Copyright (C) 199x Landon Dyer, 2011-2021 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
9 // This file is included (twice) to parse two addressing modes, into slightly
10 // different var names
12 uint64_t scaleval; // Expression's value
13 TOKEN scaleexpr[EXPRSIZE]; // Expression
14 WORD scaleattr; // Expression's attribute
15 SYM * scaleesym; // External symbol involved in expr
20 if ((*tok >= REG68_D0) && (*tok <= REG68_D7))
25 else if ((*tok >= REG68_A0) && (*tok <= REG68_A7))
34 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
42 // (An,Xn[.siz][*scale])
43 // (PC,Xn[.siz][*scale])
45 // (d8,An,Xn[.siz][*scale])
47 // (d8,PC,Xn[.siz][*scale])
48 // ([bd,An],Xn[.siz][*scale],od)
49 // ([bd,An,Xn[.siz][*scale]],od)
50 // ([bd,PC],Xn[.siz][*scale],od)
51 // ([bd,PC,Xn[.siz][*scale]],od)
54 int ea_PC = 0; // Flag that let us know if we have PC or An relative ea
57 if ((*tok >= REG68_A0) && (*tok <= REG68_A7))
77 goto AMn_IX0; // Handle ",Xn[.siz][*scale])"
79 else if ((*tok >= REG68_D0) && (*tok <= REG68_D7))
81 // Since index register isn't used here, store register number in this field
82 AnIXREG = *tok++ & 7; // (Dn)
87 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
88 AnEXTEN |= EXT_BS; // Base register suppressed
89 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
90 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
92 AnREG = 6 << 3; // stuff 110 to mode field
97 AMn = DINDL; // (Dn.l)
98 AnEXTEN = 1 << 11; // Long index size
101 else if (*tok == 'W') // (Dn.w)
104 AnEXTEN = 0 << 11; // Word index size
107 else if (*tok == ',')
109 // ([bd,An],Xn..) without bd, An
110 // Base displacement is suppressed
111 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
112 AnEXTEN |= EXT_BS; // Base register suppressed
113 AnEXTEN |= EXT_BDSIZE0;
114 AnREG = 6 << 3; // stuff 110 to mode field
120 return error("(Dn) error");
124 { // scale: *1, *2, *4, *8
129 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
130 return error("scale factor expression must evaluate");
152 else if (*tok++ != CONST)
176 tok++; // Take into account that constants are 64-bit
183 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
184 AnEXTEN |= EXT_BS; // Base register suppressed
185 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
186 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
187 AnREG = 6 << 3; // stuff 110 to mode field
191 else if (*tok == ',')
193 tok++; // eat the comma
194 // It might be (Dn[.wl][*scale],od)
195 // Maybe this is wrong and we have to write some code here
196 // instead of reusing that path...
197 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
198 AnEXTEN |= EXT_BS; // Base displacement null - suppressed
199 AnEXTEN |= AnIXREG << 12;
203 return error("unhandled so far");
205 else if (*tok == REG68_PC)
206 { // (PC,Xn[.siz][*scale])
210 // Common index handler; enter here with 'tok' pointing at the
213 AMn_IX0: // Handle indexed with missing expr
216 AnEXATTR = ABS | DEFINED;
218 AMn_IXN: // Handle any indexed (tok -> a comma)
223 if (*tok < REG68_D0 || *tok > REG68_A7)
226 AnIXREG = *tok++ & 15;
229 { // Index reg size: <empty> | .W | .L
239 case DOTB: // .B not allowed here...
244 { // scale: *1, *2, *4, *8
249 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
250 return error("scale factor expression must evaluate");
269 else if (*tok++ != CONST)
290 tok++; // Take into account that constants are 64-bit
296 // If we got here we didn't get any [] stuff
297 // so let's suppress base displacement before
300 AnEXTEN |= EXT_BDSIZE0; // Base displacement null - suppressed
303 if (*tok++ != ')') // final ")"
308 else if (*tok == '[')
311 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
313 // Check to see if base displacement is present
314 if (*tok != CONST && *tok != SYMBOL)
316 AnEXTEN |= EXT_BDSIZE0;
320 expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
322 if (CHECK_OPTS(OPT_020_DISP) && (AnBEXVAL == 0) && (AnEXATTR != 0))
324 // bd = 0 so let's optimise it out
325 AnEXTEN |= EXT_BDSIZE0;
327 else if (*tok == DOTL)
330 AnEXTEN |= EXT_BDSIZEL;
335 // ([bd[.w],... or ([bd,...
336 // Is .W forced here?
339 AnEXTEN |= EXT_BDSIZEW;
344 // Defined, absolute values from $FFFF8000..$00007FFF
345 // get optimized to absolute short
346 if (CHECK_OPTS(OPT_020_DISP)
347 && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
348 && (((uint32_t)AnBEXVAL + 0x8000) < 0x10000))
350 AnEXTEN |= EXT_BDSIZEW;
353 warn("o5: absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
357 AnEXTEN |= EXT_BDSIZEL;
366 // Check for address register or PC, suppress base register
369 if (*tok == REG68_PC)
371 ea_PC = 3; // Set flag in order to set proper value to AMn below when we can make a decision on ea
372 // (why "3"? Well, MEMPOST is 3 away from PCMPOST, etc. Have a look at amode.h)
373 AnREG = (7 << 3) | 3; // PC is special case - stuff 011 to register field and 111 to the mode field
376 else if ((*tok >= REG68_A0) && (*tok <= REG68_A7))
378 AnREG = (6 << 3) | (*tok & 7);
381 else if ((*tok >= REG68_D0) && (*tok <= REG68_D7))
385 AnEXTEN |= ((*tok & 7) << 12);
387 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
391 // ([bd,An/PC],Xn.W/L...)
394 // Index reg size: <empty> | .W | .L
405 // .B not allowed here...
410 if (*tok == '*') // ([bd,An/PC],Xn*...)
411 { // scale: *1, *2, *4, *8
416 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
417 return error("scale factor expression must evaluate");
436 else if (*tok++ != CONST)
457 tok++; // Take into account that constants are 64-bit
460 if (*tok == ']') // ([bd,Dn]...
466 else if (*tok == ']')
468 // PC and Xn is suppressed
469 AnREG = 6 << 3; // stuff 110b to mode field
477 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
480 // ([bd,An/PC],Xn,od)
486 // Xn and od are non existent, get out of jail free card
488 AMn = MEMPRE + ea_PC; // ([bc,An,Xn],od) with no Xn and od
489 AnEXTEN |= EXT_IS | EXT_IISPREN; // Suppress Xn and od
492 else if (*tok != ',')
493 return error("comma expected after ]");
495 tok++; // eat the comma
497 if ((*tok >= REG68_A0) && (*tok <= REG68_A7))
499 AnIXREG = ((*tok & 7) << 12);
503 else if ((*tok >= REG68_D0) && (*tok <= REG68_D7))
505 AnEXTEN |= ((*tok & 7) << 12);
511 // No index found, suppress it
513 tok--; // Rewind tok to point to the comma
514 goto IS_SUPPRESSEDn; // https://xkcd.com/292/ - what does he know anyway?
518 // ([bd,An/PC],Xn.W/L...)
521 // Index reg size: <empty> | .W | .L
530 // .B not allowed here...
536 // Check for Xn scale
537 if (*tok == '*') // ([bd,An/PC],Xn*...)
538 { // scale: *1, *2, *4, *8
543 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
544 return error("scale factor expression must evaluate");
546 else if (*tok == CONST)
548 scaleval = (int)*tok++;
549 tok++; // Take into account that constants are 64-bit
574 if (*tok == ')') // ([bd,An/PC],Xn)
576 // od is non existent, get out of jail free card
577 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
578 AnEXTEN |= EXT_IISPOSN; // No outer displacement
582 else if (*tok != ',')
583 return error("comma expected");
585 tok++; // eat the comma
588 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
591 if (CHECK_OPTS(OPT_020_DISP) && (AnEXATTR & DEFINED) && (AnEXVAL == 0))
593 // od = 0 so optimise it out
594 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
595 AnEXTEN |= EXT_IISPOSN; // No outer displacement
600 // ([bd,An/PC],Xn,od)
601 // Is .W forced here?
606 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
607 AMn = MEMPOST + ea_PC;
611 // Is .L forced here?
613 tok++; // Doesn't matter, we're going for .L anyway
618 if (!(AnEXTEN & EXT_BS))
619 od_ea = EXT_IISPOSL; // Long outer displacement
622 // bd is suppressed, so sticking the od size in bd
624 // And of course the expression has to be copied to
625 // AnBEXPR instead of AnEXPR. Yay. :-/
630 AnBEXPR[i] = AnEXPR[i];
633 while (AnEXPR[i] != 'E');
638 AMn = MEMPOST + ea_PC;
640 // Defined, absolute values from $FFFF8000..$00007FFF get
641 // optimized to absolute short
642 if (CHECK_OPTS(OPT_020_DISP)
643 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
644 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
646 od_ea = EXT_IISPOSW; // Word outer displacement
647 AMn = MEMPOST + ea_PC;
650 warn("o5: absolute value in outer displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
655 // Check for final closing parenthesis
662 return error("Closing parenthesis missing on addressing mode");
667 if (*tok == ')') // ([bd,An/PC],Xn)
669 // od is non existent, get out of jail free card
670 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
671 AnEXTEN |= EXT_IISNOIN; // No outer displacement
676 return error("comma expected");
678 tok++; // eat the comma
680 if ((*tok != CONST) && (*tok != SYMBOL))
683 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
685 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0))
687 // od=0 so optimise it out
688 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
689 AnEXTEN |= EXT_IISNOIN; // No outer displacement
694 // ([bd,An/PC],Xn,od)
699 AMn = MEMPOST + ea_PC;
700 AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
705 AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
706 AMn = MEMPRE + ea_PC;;
710 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
711 AMn = MEMPOST + ea_PC;
714 // Defined, absolute values from $FFFF8000..$00007FFF get
715 // optimized to absolute short
716 else if (CHECK_OPTS(OPT_020_DISP)
717 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
718 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
720 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
722 warn("o5: outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
726 // Check for final closing parenthesis
733 return error("Closing parenthesis missing on addressing mode");
735 else if (*tok == ',')
737 tok++; // ([bd,An,Xn.size*scale],od)
740 if ((*tok >= REG68_A0) && (*tok <= REG68_A7))
742 AnEXTEN |= ((*tok & 7) << 12);
746 else if ((*tok >= REG68_D0) && (*tok <= REG68_D7))
748 AnEXTEN |= ((*tok & 7) << 12);
754 // ([bd,An/PC],Xn.W/L...)
757 // Index reg size: <empty> | .W | .L
768 // .B not allowed here...
773 if (*tok == '*') // ([bd,An/PC],Xn*...)
774 { // scale: *1, *2, *4, *8
779 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
780 return error("scale factor expression must evaluate");
799 else if (*tok++ != CONST)
823 tok++; // Take into account that constants are 64-bit
829 return error("Expected closing bracket ]");
830 tok++; // Eat the bracket
833 if (*tok == ')') // ([bd,An/PC,Xn]...
835 // od is non existent, get out of jail free card
836 AMn = MEMPRE + ea_PC; // let's say it's ([bd,An,Xn],od) with od suppressed then
837 AnEXTEN |= EXT_IISPREN; // No outer displacement
841 else if (*tok++ != ',')
842 return error("comma expected after ]");
844 if (*tok == SYMBOL || *tok == CONST)
846 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
849 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
851 // od=0 so optimise it out
852 AMn = MEMPRE + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
853 AnEXTEN |= EXT_IISPRE0; // No outer displacement
859 // ([bd,An/PC,Xn],od)
863 AMn = MEMPRE + ea_PC;
865 AnEXTEN |= EXT_IISPREL;
870 AMn = MEMPRE + ea_PC;
871 int expr_size = EXT_IISPREW; // Assume we have a .w value
873 if ((AnEXVAL + 0x8000) > 0x10000)
875 // Long value, so mark it as such for now
876 expr_size = EXT_IISPREL;
878 // Defined, absolute values from $FFFF8000..$00007FFF
879 // get optimized to absolute short
880 if (CHECK_OPTS(OPT_020_DISP)
881 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
882 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
884 expr_size = EXT_IISPREW;
887 warn("o5: outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
891 AnEXTEN |= expr_size; // Assume we have a .w value
893 // Is .W forced here?
898 if (expr_size == EXT_IISPREL)
899 return error("outer displacement value does not fit in .w size");
903 // Check for final closing parenthesis
910 return error("Closing parenthesis missing on addressing mode");
918 // We have an aliasing problem here, because a couple of differenct cases end up here:
919 // (a0), 0(a0,d0), (-288,a0,d0.l) can be easily detected and parsed.
920 // But what about (160*150)+4(A1)? With the old scheme, i.e. skip past the left parenthesis and try to parse the inside
921 // tokens will only parse (160*150) and everything else is assumed that it's part of the ea, i.e. +4(a1). This would produce
922 // an error since the parser would expect (a1). The way to work around this used to be to wrap all the displacement in
923 // parenthesis, ((160*150)+4)(a1). But that's something the user really doesn't want to think about.
924 // What we can do is to peek ahead in the token stream and see if we have something that reminds of an expression
925 // (i.e. no register tokens or commas) until we hit an open parenthesis plus a register (parenthesis balance during the scan
926 // has to be maintained of course, otherwise we might be led into false conclusions).
927 TOKEN *look_ahead = tok;
928 int parenthesis_level = 1; // We count the opening parenthesis so we're not at level 0
929 int this_is_an_expression = 0;
932 if (*look_ahead == EOL)
934 // Something really bad happened, abort
935 return error("reached end of line while parsing expression");
937 if (*look_ahead == '(')
939 if (parenthesis_level == 0)
941 if (look_ahead[1] == EOL)
943 return error("reached end of line while parsing expression");
945 if ((look_ahead[1] >= REG68_A0 && look_ahead[1] <= REG68_A7) || look_ahead[1] == REG68_PC)
947 tok--; // Rewind token pointer to start of parenthesis
948 this_is_an_expression = 1;
956 if (*look_ahead == ',' || (*look_ahead >= REG68_A0 && *look_ahead <= REG68_A7))
958 // Nope, this is a different case, abort
961 if (*look_ahead == ')')
964 if (parenthesis_level < 0) return error("unbalanced parenthesis in expression");
968 if (*look_ahead == ACONST||*look_ahead==FCONST)
970 look_ahead += 3; // Skip all the data associated with ACONST
977 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
980 // It could be that this is really just an expression prefixing a
981 // register as a displacement...
987 // Otherwise, check for PC & etc displacements...
991 if ((*tok >= REG68_A0) && (*tok <= REG68_A7))
998 // Check if we're actually doing d8(An,Dn) or
999 // (d16,An,Dn[.size][*scale])
1000 // TODO: not a very clear cut case from what I can think.
1001 // The only way to distinguish between the two is to check
1002 // AnEXVAL and see if it's >127 or <-128. But this doesn't
1003 // work if AnEXVAL isn't defined yet. For now we fall
1004 // through to d8(An,Dn) but this might bite us in the arse
1006 if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
1008 // We're going to treat it as a full extension format
1009 // with no indirect access and no base displacement/
1010 // index register suppression
1011 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
1012 AnEXTEN |= EXT_IISPRE0; // No Memory Indirect Action
1013 AnEXTEN |= EXT_BDSIZEL; // Base Displacement Size Long
1014 tok++; // Get past the comma
1016 // Our expression is techically a base displacement,
1017 // so let's copy it to the relevant variables so
1018 // eagen0.c can pick it up properly
1020 AnBEXATTR = AnEXATTR;
1022 if ((*tok >= REG68_D0) && (*tok <= REG68_D7))
1024 AnEXTEN |= ((*tok++) & 7) << 12;
1029 // Index reg size: <empty> | .W | .L
1040 // .B not allowed here...
1045 if (*tok == '*') // (d16,An,Dn[.size][*scale])
1046 { // scale: *1, *2, *4, *8
1051 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
1052 return error("scale factor expression must evaluate");
1071 else if (*tok++ != CONST)
1075 switch ((int)*tok++)
1092 tok++; // Take into account that constants are 64-bit
1097 return error("Closing parenthesis missing on addressing mode");
1099 // Let's say that this is the closest to our case
1110 else if (*tok == ')')
1119 else if (*tok == REG68_PC)
1126 else if (*tok == ')')
1128 AMn = PCDISP; // expr(PC)
1139 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= REG68_A0) && (tok[2] <= REG68_A7)) && tok[3] == ')')
1145 else if (*tok == REG68_CCR)
1151 else if (*tok == REG68_SR)
1157 else if (*tok == REG68_USP)
1161 AnREG = 2; // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
1164 else if ((*tok >= REG68_IC40) && (*tok <= REG68_BC40))
1167 AnREG = *tok++ - REG68_IC40;
1169 // After a cache keyword only a comma or EOL is allowed
1170 if ((*tok != ',') && (*tok != EOL))
1174 else if ((*tok >= REG68_SFC) && (*tok <= REG68_CRP))
1177 AnREG = (*tok++) - REG68_SFC;
1180 else if ((*tok >= REG68_FP0) && (*tok <= REG68_FP7))
1183 AnREG = (*tok++ & 7);
1185 else if ((*tok >= REG68_FPIAR) && (*tok <= REG68_FPCR))
1188 AnREG = (1 << ((*tok++) - REG68_FPIAR + 10));
1199 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
1209 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1210 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1214 else if (*tok != '(')
1219 // .L is forced here
1222 // When PC relative is enforced, check for any symbols that aren't
1223 // EQU'd, in this case it's an illegal mode
1224 if ((CHECK_OPTS(OPT_PC_RELATIVE)) && (AnEXATTR & (DEFINED | REFERENCED | EQUATED) == (DEFINED | REFERENCED)))
1225 return error("relocation not allowed when o30 is enabled");
1231 // Defined, absolute values from $FFFF8000..$00007FFF get
1232 // optimized to absolute short
1233 if (CHECK_OPTS(OPT_ABS_SHORT)
1234 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1235 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
1239 if (optim_warn_flag)
1240 warn("o0: absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1249 if ((*tok >= REG68_A0) && (*tok <= REG68_A7))
1263 else if (*tok == REG68_PC)
1279 // Addressing mode OK
1284 // Clean up dirty little macros
1299 #undef CHK_FOR_DISPn
1306 #undef IS_SUPPRESSEDn