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)
60 if ((*tok >= KW_A0) && (*tok <= KW_A7))
80 goto AMn_IX0; // Handle ",Xn[.siz][*scale])"
82 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
84 // Since index register isn't used here, store register number in this field
85 AnIXREG = *tok++ & 7; // (Dn)
90 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
91 AnEXTEN |= EXT_BS; // Base register suppressed
92 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
93 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
95 AnREG = 6 << 3; // stuff 110 to mode field
100 AMn = DINDL; // (Dn.l)
101 AnEXTEN = 1 << 11; // Long index size
104 else if (*tok == 'W') // (Dn.w)
107 AnEXTEN = 0 << 11; // Word index size
110 else if (*tok == ',')
112 // ([bd,An],Xn..) without bd, An
113 // Base displacement is suppressed
114 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
115 AnEXTEN |= EXT_BS; // Base register suppressed
116 AnEXTEN |= EXT_BDSIZE0;
117 AnREG = 6 << 3; // stuff 110 to mode field
123 return error("(Dn) error");
127 { // scale: *1, *2, *4, *8
132 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
133 return error("scale factor expression must evaluate");
155 else if (*tok++ != CONST)
179 tok++; // Take into account that constants are 64-bit
186 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
187 AnEXTEN |= EXT_BS; // Base register suppressed
188 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
189 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
190 AnREG = 6 << 3; // stuff 110 to mode field
194 else if (*tok == ',')
196 tok++; // eat the comma
197 // It might be (Dn[.wl][*scale],od)
198 // Maybe this is wrong and we have to write some code here
199 // instead of reusing that path...
200 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
201 AnEXTEN |= EXT_BS; // Base displacement null - suppressed
202 AnEXTEN |= AnIXREG << 12;
206 return error("unhandled so far");
208 else if (*tok == KW_PC)
209 { // (PC,Xn[.siz][*scale])
213 // Common index handler; enter here with 'tok' pointing at the
216 AMn_IX0: // Handle indexed with missing expr
219 AnEXATTR = ABS | DEFINED;
221 AMn_IXN: // Handle any indexed (tok -> a comma)
226 if (*tok < KW_D0 || *tok > KW_A7)
229 AnIXREG = *tok++ & 15;
232 { // Index reg size: <empty> | .W | .L
242 case DOTB: // .B not allowed here...
247 { // scale: *1, *2, *4, *8
252 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
253 return error("scale factor expression must evaluate");
272 else if (*tok++ != CONST)
293 tok++; // Take into account that constants are 64-bit
299 // If we got here we didn't get any [] stuff
300 // so let's suppress base displacement before
303 AnEXTEN |= EXT_BDSIZE0; // Base displacement null - suppressed
306 if (*tok++ != ')') // final ")"
311 else if (*tok == '[')
314 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
316 // Check to see if base displacement is present
317 if (*tok != CONST && *tok != SYMBOL)
319 AnEXTEN |= EXT_BDSIZE0;
323 expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
325 if (CHECK_OPTS(OPT_020_DISP) && (AnBEXVAL == 0) && (AnEXATTR != 0))
327 // bd = 0 so let's optimise it out
328 AnEXTEN |= EXT_BDSIZE0;
330 else if (*tok == DOTL)
333 AnEXTEN |= EXT_BDSIZEL;
338 // ([bd[.w],... or ([bd,...
339 // Is .W forced here?
342 AnEXTEN |= EXT_BDSIZEW;
347 // Defined, absolute values from $FFFF8000..$00007FFF
348 // get optimized to absolute short
349 if (CHECK_OPTS(OPT_020_DISP)
350 && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
351 && (((uint32_t)AnBEXVAL + 0x8000) < 0x10000))
353 AnEXTEN |= EXT_BDSIZEW;
356 warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
360 AnEXTEN |= EXT_BDSIZEL;
368 // return error("Comma expected after base displacement");
371 // Check for address register or PC, suppress base register
376 AnREG = (7 << 3) | 3; // PC is special case - stuff 011 to register field and 111 to the mode field
379 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
381 AnREG = (6 << 3) | (*tok & 7);
384 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
388 AnEXTEN |= ((*tok & 7) << 12);
390 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
394 // ([bd,An/PC],Xn.W/L...)
397 // Index reg size: <empty> | .W | .L
408 // .B not allowed here...
413 if (*tok == '*') // ([bd,An/PC],Xn*...)
414 { // scale: *1, *2, *4, *8
419 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
420 return error("scale factor expression must evaluate");
439 else if (*tok++ != CONST)
460 tok++; // Take into account that constants are 64-bit
463 if (*tok == ']') // ([bd,Dn]...
469 else if (*tok == ']')
471 // PC and Xn is suppressed
472 AnREG = 6 << 3; // stuff 110b to mode field
480 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
483 // ([bd,An/PC],Xn,od)
489 // Xn and od are non existent, get out of jail free card
491 AMn = MEMPRE; // ([bc,An,Xn],od) with no Xn and od
492 AnEXTEN |= EXT_IS | EXT_IISPREN; // Suppress Xn and od
495 else if (*tok != ',')
496 return error("comma expected after ]");
498 tok++; // eat the comma
500 if ((*tok >= KW_A0) && (*tok <= KW_A7))
502 AnIXREG = ((*tok & 7) << 12);
506 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
508 AnEXTEN |= ((*tok & 7) << 12);
514 // No index found, suppress it
516 tok--; // Rewind tok to point to the comma
517 goto IS_SUPPRESSEDn; // https://xkcd.com/292/ - what does he know anyway?
521 // ([bd,An/PC],Xn.W/L...)
524 // Index reg size: <empty> | .W | .L
535 // .B not allowed here...
540 if (*tok == '*') // ([bd,An/PC],Xn*...)
541 { // scale: *1, *2, *4, *8
546 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
547 return error("scale factor expression must evaluate");
566 else if (*tok++ != CONST)
587 tok++; // Take into account that constants are 64-bit
592 if (*tok == ')') // ([bd,An/PC],Xn)
594 // od is non existent, get out of jail free card
595 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
596 AnEXTEN |= EXT_IISPOSN; // No outer displacement
600 else if (*tok != ',')
601 return error("comma expected");
603 tok++; // eat the comma
606 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
609 if (CHECK_OPTS(OPT_020_DISP) && (AnEXATTR & DEFINED) && (AnEXVAL == 0))
611 // od = 0 so optimise it out
612 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
613 AnEXTEN |= EXT_IISPOSN; // No outer displacement
618 // ([bd,An/PC],Xn,od)
622 if (!(AnEXTEN & EXT_BS))
623 AnEXTEN |= EXT_IISPOSL; // Long outer displacement
626 // bd is suppressed, so sticking the od size in bd
627 AnEXTEN |= EXT_BDSIZEL;
628 // And of course the expression has to be copied to
629 // AnBEXPR instead of AnEXPR. Yay. :-/
634 AnBEXPR[i] = AnEXPR[i];
637 while (AnEXPR[i] != 'E');
645 // Defined, absolute values from $FFFF8000..$00007FFF get
646 // optimized to absolute short
647 if (CHECK_OPTS(OPT_020_DISP)
648 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
649 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
651 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
654 warn("absolute value in outer displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
661 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
664 // Is .W forced here?
669 // Check for final closing parenthesis
676 return error("Closing parenthesis missing on addressing mode");
681 if (*tok == ')') // ([bd,An/PC],Xn)
683 // od is non existent, get out of jail free card
684 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
685 AnEXTEN |= EXT_IISNOIN; // No outer displacement
690 return error("comma expected");
692 tok++; // eat the comma
694 if ((*tok != CONST) && (*tok != SYMBOL))
697 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
699 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0))
701 // od=0 so optimise it out
702 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
703 AnEXTEN |= EXT_IISNOIN; // No outer displacement
708 // ([bd,An/PC],Xn,od)
714 AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
719 AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
724 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
728 // Defined, absolute values from $FFFF8000..$00007FFF get
729 // optimized to absolute short
730 else if (CHECK_OPTS(OPT_020_DISP)
731 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
732 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
734 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
736 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
740 // Check for final closing parenthesis
747 return error("Closing parenthesis missing on addressing mode");
749 else if (*tok == ',')
751 tok++; // ([bd,An,Xn.size*scale],od)
754 if ((*tok >= KW_A0) && (*tok <= KW_A7))
756 AnEXTEN |= ((*tok & 7) << 12);
760 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
762 AnEXTEN |= ((*tok & 7) << 12);
768 // ([bd,An/PC],Xn.W/L...)
771 // Index reg size: <empty> | .W | .L
782 // .B not allowed here...
787 if (*tok == '*') // ([bd,An/PC],Xn*...)
788 { // scale: *1, *2, *4, *8
793 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
794 return error("scale factor expression must evaluate");
813 else if (*tok++ != CONST)
837 tok++; // Take into account that constants are 64-bit
843 return error("Expected closing bracket ]");
844 tok++; // Eat the bracket
847 if (*tok == ')') // ([bd,An/PC,Xn]...
849 // od is non existent, get out of jail free card
850 //AnEXVAL=0; // zero outer displacement
851 AMn = MEMPRE; // let's say it's ([bd,An,Xn],od) with od suppressed then
852 AnEXTEN |= EXT_IISPREN; // No outer displacement
856 else if (*tok++ != ',')
857 return error("comma expected after ]");
859 if (*tok == SYMBOL || *tok == CONST)
861 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
864 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
866 // od=0 so optimise it out
867 AMn = MEMPRE; // let's say it's ([bd,An],Xn,od) with od=0 then
868 AnEXTEN |= EXT_IISPRE0; // No outer displacement
874 // ([bd,An/PC,Xn],od)
880 AnEXTEN |= EXT_IISPREL;
886 int expr_size = EXT_IISPREW; // Assume we have a .w value
888 if ((AnEXVAL + 0x8000) > 0x10000)
890 // Long value, so mark it as such for now
891 expr_size = EXT_IISPREL;
893 // Defined, absolute values from $FFFF8000..$00007FFF
894 // get optimized to absolute short
895 if (CHECK_OPTS(OPT_020_DISP)
896 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
897 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
899 expr_size = EXT_IISPREW;
902 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
906 AnEXTEN |= expr_size; // Assume we have a .w value
908 // Is .W forced here?
913 if (expr_size == EXT_IISPREL)
914 return error("outer displacement value does not fit in .w size");
918 // Check for final closing parenthesis
925 return error("Closing parenthesis missing on addressing mode");
933 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
936 // It could be that this is really just an expression prefixing a
937 // register as a displacement...
944 // Otherwise, check for PC & etc displacements...
948 if ((*tok >= KW_A0) && (*tok <= KW_A7))
955 // Check if we're actually doing d8(An,Dn) or
956 // (d16,An,Dn[.size][*scale])
957 // TODO: not a very clear cut case from what I can think.
958 // The only way to distinguish between the two is to check
959 // AnEXVAL and see if it's >127 or <-128. But this doesn't
960 // work if AnEXVAL isn't defined yet. For now we fall
961 // through to d8(An,Dn) but this might bite us in the arse
963 if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
965 // We're going to treat it as a full extension format
966 // with no indirect access and no base displacement/
967 // index register suppression
968 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
969 AnEXTEN |= EXT_IISPRE0; // No Memory Indirect Action
970 AnEXTEN |= EXT_BDSIZEL; // Base Displacement Size Long
971 tok++; // Get past the comma
973 // Our expression is techically a base displacement,
974 // so let's copy it to the relevant variables so
975 // eagen0.c can pick it up properly
978 AnBEXATTR = AnEXATTR;
980 if ((*tok >= KW_D0) && (*tok <= KW_D7))
982 AnEXTEN |= ((*tok++) & 7) << 12;
987 // Index reg size: <empty> | .W | .L
998 // .B not allowed here...
1003 if (*tok == '*') // ([bd,An/PC],Xn*...)
1004 { // scale: *1, *2, *4, *8
1009 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
1010 return error("scale factor expression must evaluate");
1029 else if (*tok++ != CONST)
1033 switch ((int)*tok++)
1050 tok++; // Take into account that constants are 64-bit
1055 return error("Closing parenthesis missing on addressing mode");
1057 // Let's say that this is the closest to our case
1068 else if (*tok == ')')
1077 else if (*tok == KW_PC)
1084 else if (*tok == ')')
1086 AMn = PCDISP; // expr(PC)
1097 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
1103 else if (*tok == KW_CCR)
1109 else if (*tok == KW_SR)
1115 else if (*tok == KW_USP)
1119 AnREG = 2; // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
1122 else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
1125 AnREG = *tok++ - KW_IC40;
1127 // After a cache keyword only a comma or EOL is allowed
1128 if ((*tok != ',') && (*tok != EOL))
1132 else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
1135 AnREG = (*tok++) - KW_SFC;
1138 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
1141 AnREG = (*tok++ & 7);
1143 else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
1146 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
1157 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
1167 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1168 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1172 else if (*tok != '(')
1177 // When PC relative is enforced, check for any symbols that aren't
1178 // EQU'd, in this case it's an illegal mode
1179 if ((CHECK_OPTS(OPT_PC_RELATIVE)) && (AnEXATTR & REFERENCED) && (AnEXATTR & DEFINED) && (!(AnEXATTR & EQUATED)))
1180 return error("relocation not allowed");
1182 // .L is forced here
1190 // Defined, absolute values from $FFFF8000..$00007FFF get
1191 // optimized to absolute short
1192 if (CHECK_OPTS(OPT_ABS_SHORT)
1193 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1194 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
1198 if (optim_warn_flag)
1199 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1208 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1222 else if (*tok == KW_PC)
1238 // Addressing mode OK
1243 // Clean up dirty little macros
1258 #undef CHK_FOR_DISPn
1265 #undef IS_SUPPRESSEDn