2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // PARMODE.C - Addressing Modes Parser Include
4 // Copyright (C) 199x Landon Dyer, 2011-2020 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 >= KW_D0) && (*tok <= KW_D7))
25 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
34 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
40 // Small problem with this is that the opening parentheses might be an
41 // expression that's part of a displacement; this code will falsely flag
46 // (An,Xn[.siz][*scale])
47 // (PC,Xn[.siz][*scale])
49 // (d8,An,Xn[.siz][*scale])
51 // (d8,PC,Xn[.siz][*scale])
52 // ([bd,An],Xn[.siz][*scale],od)
53 // ([bd,An,Xn[.siz][*scale]],od)
54 // ([bd,PC],Xn[.siz][*scale],od)
55 // ([bd,PC,Xn[.siz][*scale]],od)
58 int ea_PC = 0; // Flag that let us know if we have PC or An relative ea
61 if ((*tok >= KW_A0) && (*tok <= KW_A7))
81 goto AMn_IX0; // Handle ",Xn[.siz][*scale])"
83 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
85 // Since index register isn't used here, store register number in this field
86 AnIXREG = *tok++ & 7; // (Dn)
91 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
92 AnEXTEN |= EXT_BS; // Base register suppressed
93 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
94 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
96 AnREG = 6 << 3; // stuff 110 to mode field
101 AMn = DINDL; // (Dn.l)
102 AnEXTEN = 1 << 11; // Long index size
105 else if (*tok == 'W') // (Dn.w)
108 AnEXTEN = 0 << 11; // Word index size
111 else if (*tok == ',')
113 // ([bd,An],Xn..) without bd, An
114 // Base displacement is suppressed
115 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
116 AnEXTEN |= EXT_BS; // Base register suppressed
117 AnEXTEN |= EXT_BDSIZE0;
118 AnREG = 6 << 3; // stuff 110 to mode field
124 return error("(Dn) error");
128 { // scale: *1, *2, *4, *8
133 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
134 return error("scale factor expression must evaluate");
156 else if (*tok++ != CONST)
180 tok++; // Take into account that constants are 64-bit
187 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
188 AnEXTEN |= EXT_BS; // Base register suppressed
189 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
190 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
191 AnREG = 6 << 3; // stuff 110 to mode field
195 else if (*tok == ',')
197 tok++; // eat the comma
198 // It might be (Dn[.wl][*scale],od)
199 // Maybe this is wrong and we have to write some code here
200 // instead of reusing that path...
201 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
202 AnEXTEN |= EXT_BS; // Base displacement null - suppressed
203 AnEXTEN |= AnIXREG << 12;
207 return error("unhandled so far");
209 else if (*tok == KW_PC)
210 { // (PC,Xn[.siz][*scale])
214 // Common index handler; enter here with 'tok' pointing at the
217 AMn_IX0: // Handle indexed with missing expr
220 AnEXATTR = ABS | DEFINED;
222 AMn_IXN: // Handle any indexed (tok -> a comma)
227 if (*tok < KW_D0 || *tok > KW_A7)
230 AnIXREG = *tok++ & 15;
233 { // Index reg size: <empty> | .W | .L
243 case DOTB: // .B not allowed here...
248 { // scale: *1, *2, *4, *8
253 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
254 return error("scale factor expression must evaluate");
273 else if (*tok++ != CONST)
294 tok++; // Take into account that constants are 64-bit
300 // If we got here we didn't get any [] stuff
301 // so let's suppress base displacement before
304 AnEXTEN |= EXT_BDSIZE0; // Base displacement null - suppressed
307 if (*tok++ != ')') // final ")"
312 else if (*tok == '[')
315 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
317 // Check to see if base displacement is present
318 if (*tok != CONST && *tok != SYMBOL)
320 AnEXTEN |= EXT_BDSIZE0;
324 expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
326 if (CHECK_OPTS(OPT_020_DISP) && (AnBEXVAL == 0) && (AnEXATTR != 0))
328 // bd = 0 so let's optimise it out
329 AnEXTEN |= EXT_BDSIZE0;
331 else if (*tok == DOTL)
334 AnEXTEN |= EXT_BDSIZEL;
339 // ([bd[.w],... or ([bd,...
340 // Is .W forced here?
343 AnEXTEN |= EXT_BDSIZEW;
348 // Defined, absolute values from $FFFF8000..$00007FFF
349 // get optimized to absolute short
350 if (CHECK_OPTS(OPT_020_DISP)
351 && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
352 && (((uint32_t)AnBEXVAL + 0x8000) < 0x10000))
354 AnEXTEN |= EXT_BDSIZEW;
357 warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
361 AnEXTEN |= EXT_BDSIZEL;
370 // Check for address register or PC,
371 // suppress base register otherwise
375 ea_PC = 3; // Set flag in order to set proper value to AMn below when we can make a decision on ea
376 // (why "3"? Well, MEMPOST is 3 away from PCMPOST, etc. Have a look at amode.h)
377 AnREG = (7 << 3) | 3; // PC is special case - stuff 011 to register field and 111 to the mode field
380 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
382 AnREG = (6 << 3) | (*tok & 7);
385 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
389 AnEXTEN |= ((*tok & 7) << 12);
391 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
395 // ([bd,An/PC],Xn.W/L...)
398 // Index reg size: <empty> | .W | .L
409 // .B not allowed here...
414 if (*tok == '*') // ([bd,An/PC],Xn*...)
415 { // scale: *1, *2, *4, *8
420 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
421 return error("scale factor expression must evaluate");
440 else if (*tok++ != CONST)
461 tok++; // Take into account that constants are 64-bit
464 if (*tok == ']') // ([bd,Dn]...
470 else if (*tok == ']')
472 // PC and Xn is suppressed
473 AnREG = 6 << 3; // stuff 110b to mode field
481 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
484 // ([bd,An/PC],Xn,od)
490 // Xn and od are non existent, get out of jail free card
492 AMn = MEMPRE + ea_PC; // ([bc,An,Xn],od) with no Xn and od
493 AnEXTEN |= EXT_IS | EXT_IISPREN; // Suppress Xn and od
496 else if (*tok != ',')
497 return error("comma expected after ]");
499 tok++; // eat the comma
501 if ((*tok >= KW_A0) && (*tok <= KW_A7))
503 AnIXREG = ((*tok & 7) << 12);
507 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
509 AnEXTEN |= ((*tok & 7) << 12);
515 // No index found, suppress it
517 tok--; // Rewind tok to point to the comma
518 goto IS_SUPPRESSEDn; // https://xkcd.com/292/ - what does he know anyway?
522 // ([bd,An/PC],Xn.W/L...)
525 // Index reg size: <empty> | .W | .L
534 // .B not allowed here...
540 // Check for Xn scale
541 if (*tok == '*') // ([bd,An/PC],Xn*...)
542 { // scale: *1, *2, *4, *8
547 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
548 return error("scale factor expression must evaluate");
550 else if (*tok == CONST)
552 scaleval = (int)*tok++;
553 tok++; // Take into account that constants are 64-bit
578 if (*tok == ')') // ([bd,An/PC],Xn)
580 // od is non existent, get out of jail free card
581 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
582 AnEXTEN |= EXT_IISPOSN; // No outer displacement
586 else if (*tok != ',')
587 return error("comma expected");
589 tok++; // eat the comma
592 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
595 if (CHECK_OPTS(OPT_020_DISP) && (AnEXATTR & DEFINED) && (AnEXVAL == 0))
597 // od = 0 so optimise it out
598 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
599 AnEXTEN |= EXT_IISPOSN; // No outer displacement
604 // ([bd,An/PC],Xn,od)
605 // Is .W forced here?
610 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
611 AMn = MEMPOST + ea_PC;
615 // Is .L forced here?
617 tok++; // Doesn't matter, we're going for .L anyway
622 if (!(AnEXTEN & EXT_BS))
623 od_ea = EXT_IISPOSL; // Long outer displacement
626 // bd is suppressed, so sticking the od size in bd
628 // And of course the expression has to be copied to
629 // AnBEXPR instead of AnEXPR. Yay. :-/
634 AnBEXPR[i] = AnEXPR[i];
636 } while (AnEXPR[i] != 'E');
641 AMn = MEMPOST + ea_PC;
643 // Defined, absolute values from $FFFF8000..$00007FFF get
644 // optimized to absolute short
645 if (CHECK_OPTS(OPT_020_DISP)
646 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
647 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
649 od_ea = EXT_IISPOSW; // Word outer displacement
650 AMn = MEMPOST + ea_PC;
652 warn("absolute value in outer displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
657 // Check for final closing parenthesis
664 return error("Closing parenthesis missing on addressing mode");
669 if (*tok == ')') // ([bd,An/PC],Xn)
671 // od is non existent, get out of jail free card
672 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
673 AnEXTEN |= EXT_IISNOIN; // No outer displacement
678 return error("comma expected");
680 tok++; // eat the comma
682 if ((*tok != CONST) && (*tok != SYMBOL))
685 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
687 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0))
689 // od=0 so optimise it out
690 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
691 AnEXTEN |= EXT_IISNOIN; // No outer displacement
696 // ([bd,An/PC],Xn,od)
701 AMn = MEMPOST + ea_PC;
702 AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
707 AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
708 AMn = MEMPRE + ea_PC;;
712 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
713 AMn = MEMPOST + ea_PC;
716 // Defined, absolute values from $FFFF8000..$00007FFF get
717 // optimized to absolute short
718 else if (CHECK_OPTS(OPT_020_DISP)
719 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
720 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
722 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
724 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
728 // Check for final closing parenthesis
735 return error("Closing parenthesis missing on addressing mode");
737 else if (*tok == ',')
739 tok++; // ([bd,An,Xn.size*scale],od)
742 if ((*tok >= KW_A0) && (*tok <= KW_A7))
744 AnEXTEN |= ((*tok & 7) << 12);
748 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
750 AnEXTEN |= ((*tok & 7) << 12);
756 // ([bd,An/PC],Xn.W/L...)
759 // Index reg size: <empty> | .W | .L
770 // .B not allowed here...
775 if (*tok == '*') // ([bd,An/PC],Xn*...)
776 { // scale: *1, *2, *4, *8
781 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
782 return error("scale factor expression must evaluate");
801 else if (*tok++ != CONST)
825 tok++; // Take into account that constants are 64-bit
831 return error("Expected closing bracket ]");
832 tok++; // Eat the bracket
835 if (*tok == ')') // ([bd,An/PC,Xn]...
837 // od is non existent, get out of jail free card
838 AMn = MEMPRE + ea_PC; // let's say it's ([bd,An,Xn],od) with od suppressed then
839 AnEXTEN |= EXT_IISPREN; // No outer displacement
843 else if (*tok++ != ',')
844 return error("comma expected after ]");
846 if (*tok == SYMBOL || *tok == CONST)
848 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
851 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
853 // od=0 so optimise it out
854 AMn = MEMPRE + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
855 AnEXTEN |= EXT_IISPRE0; // No outer displacement
861 // ([bd,An/PC,Xn],od)
865 AMn = MEMPRE + ea_PC;
867 AnEXTEN |= EXT_IISPREL;
872 AMn = MEMPRE + ea_PC;
873 int expr_size = EXT_IISPREW; // Assume we have a .w value
875 if ((AnEXVAL + 0x8000) > 0x10000)
877 // Long value, so mark it as such for now
878 expr_size = EXT_IISPREL;
880 // Defined, absolute values from $FFFF8000..$00007FFF
881 // get optimized to absolute short
882 if (CHECK_OPTS(OPT_020_DISP)
883 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
884 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
886 expr_size = EXT_IISPREW;
889 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
893 AnEXTEN |= expr_size; // Assume we have a .w value
895 // Is .W forced here?
900 if (expr_size == EXT_IISPREL)
901 return error("outer displacement value does not fit in .w size");
905 // Check for final closing parenthesis
912 return error("Closing parenthesis missing on addressing mode");
920 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
923 // It could be that this is really just an expression prefixing a
924 // register as a displacement...
931 // Otherwise, check for PC & etc displacements...
935 if ((*tok >= KW_A0) && (*tok <= KW_A7))
942 // Check if we're actually doing d8(An,Dn) or
943 // (d16,An,Dn[.size][*scale])
944 // TODO: not a very clear cut case from what I can think.
945 // The only way to distinguish between the two is to check
946 // AnEXVAL and see if it's >127 or <-128. But this doesn't
947 // work if AnEXVAL isn't defined yet. For now we fall
948 // through to d8(An,Dn) but this might bite us in the arse
950 if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
952 // We're going to treat it as a full extension format
953 // with no indirect access and no base displacement/
954 // index register suppression
955 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
956 AnEXTEN |= EXT_IISPRE0; // No Memory Indirect Action
957 AnEXTEN |= EXT_BDSIZEL; // Base Displacement Size Long
958 tok++; // Get past the comma
960 // Our expression is techically a base displacement,
961 // so let's copy it to the relevant variables so
962 // eagen0.c can pick it up properly
964 AnBEXATTR = AnEXATTR;
966 if ((*tok >= KW_D0) && (*tok <= KW_D7))
968 AnEXTEN |= ((*tok++) & 7) << 12;
973 // Index reg size: <empty> | .W | .L
984 // .B not allowed here...
989 if (*tok == '*') // (d16,An,Dn[.size][*scale])
990 { // scale: *1, *2, *4, *8
995 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
996 return error("scale factor expression must evaluate");
1015 else if (*tok++ != CONST)
1019 switch ((int)*tok++)
1036 tok++; // Take into account that constants are 64-bit
1041 return error("Closing parenthesis missing on addressing mode");
1043 // Let's say that this is the closest to our case
1054 else if (*tok == ')')
1063 else if (*tok == KW_PC)
1070 else if (*tok == ')')
1072 AMn = PCDISP; // expr(PC)
1083 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
1089 else if (*tok == KW_CCR)
1095 else if (*tok == KW_SR)
1101 else if (*tok == KW_USP)
1105 AnREG = 2; // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
1108 else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
1111 AnREG = *tok++ - KW_IC40;
1113 // After a cache keyword only a comma or EOL is allowed
1114 if ((*tok != ',') && (*tok != EOL))
1118 else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
1121 AnREG = (*tok++) - KW_SFC;
1124 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
1127 AnREG = (*tok++ & 7);
1129 else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
1132 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
1143 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
1153 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1154 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1158 else if (*tok != '(')
1163 // When PC relative is enforced, check for any symbols that aren't
1164 // EQU'd, in this case it's an illegal mode
1165 if ((CHECK_OPTS(OPT_PC_RELATIVE)) && (AnEXATTR & REFERENCED) && (AnEXATTR & DEFINED) && (!(AnEXATTR & EQUATED)))
1166 return error("relocation not allowed");
1168 // .L is forced here
1176 // Defined, absolute values from $FFFF8000..$00007FFF get
1177 // optimized to absolute short
1178 if (CHECK_OPTS(OPT_ABS_SHORT)
1179 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1180 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
1184 if (optim_warn_flag)
1185 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1194 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1208 else if (*tok == KW_PC)
1224 // Addressing mode OK
1229 // Clean up dirty little macros
1244 #undef CHK_FOR_DISPn
1251 #undef IS_SUPPRESSEDn