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;
355 warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
359 AnEXTEN |= EXT_BDSIZEL;
367 // return error("Comma expected after base displacement");
370 // Check for address register or PC, suppress base register
375 AnREG = (7 << 3) | 3; // PC is special case - stuff 011 to register field and 111 to the mode field
378 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
380 AnREG = (6 << 3) | (*tok & 7);
383 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
387 AnEXTEN |= ((*tok & 7) << 12);
389 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
393 // ([bd,An/PC],Xn.W/L...)
396 // Index reg size: <empty> | .W | .L
407 // .B not allowed here...
412 if (*tok == '*') // ([bd,An/PC],Xn*...)
413 { // scale: *1, *2, *4, *8
418 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
419 return error("scale factor expression must evaluate");
438 else if (*tok++ != CONST)
459 tok++; // Take into account that constants are 64-bit
462 if (*tok == ']') // ([bd,Dn]...
468 else if (*tok == ']')
470 // PC and Xn is suppressed
471 AnREG = 6 << 3; // stuff 110b to mode field
479 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
482 // ([bd,An/PC],Xn,od)
488 // Xn and od are non existent, get out of jail free card
490 AMn = MEMPRE; // ([bc,An,Xn],od) with no Xn and od
491 AnEXTEN |= EXT_IS | EXT_IISPREN; // Suppress Xn and od
494 else if (*tok != ',')
495 return error("comma expected after ]");
497 tok++; // eat the comma
499 if ((*tok >= KW_A0) && (*tok <= KW_A7))
501 AnIXREG = ((*tok & 7) << 12);
505 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
507 AnEXTEN |= ((*tok & 7) << 12);
513 // No index found, suppress it
515 tok--; // Rewind tok to point to the comma
516 goto IS_SUPPRESSEDn; // https://xkcd.com/292/ - what does he know anyway?
520 // ([bd,An/PC],Xn.W/L...)
523 // Index reg size: <empty> | .W | .L
534 // .B not allowed here...
539 if (*tok == '*') // ([bd,An/PC],Xn*...)
540 { // scale: *1, *2, *4, *8
545 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
546 return error("scale factor expression must evaluate");
565 else if (*tok++ != CONST)
586 tok++; // Take into account that constants are 64-bit
591 if (*tok == ')') // ([bd,An/PC],Xn)
593 // od is non existent, get out of jail free card
594 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
595 AnEXTEN |= EXT_IISPOSN; // No outer displacement
599 else if (*tok != ',')
600 return error("comma expected");
602 tok++; // eat the comma
605 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
608 if (CHECK_OPTS(OPT_020_DISP) && (AnEXATTR & DEFINED) && (AnEXVAL == 0))
610 // od=0 so optimise it out
611 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
612 AnEXTEN |= EXT_IISPOSN; // No outer displacement
617 // ([bd,An/PC],Xn,od)
621 if (!(AnEXTEN & EXT_BS))
622 AnEXTEN |= EXT_IISPOSL; // Long outer displacement
625 // bd is suppressed, so sticking the od size in bd
626 AnEXTEN |= EXT_BDSIZEL;
627 // And of course the expression has to be copied to
628 // AnBEXPR instead of AnEXPR. Yay. :-/
633 AnBEXPR[i] = AnEXPR[i];
636 while (AnEXPR[i] != 'E');
644 // Defined, absolute values from $FFFF8000..$00007FFF get
645 // optimized to absolute short
646 if (CHECK_OPTS(OPT_020_DISP)
647 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
648 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
650 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
653 warn("absolute value in outer displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
660 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
663 // Is .W forced here?
668 // Check for final closing parenthesis
675 return error("Closing parenthesis missing on addressing mode");
680 if (*tok == ')') // ([bd,An/PC],Xn)
682 // od is non existent, get out of jail free card
683 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
684 AnEXTEN |= EXT_IISNOIN; // No outer displacement
689 return error("comma expected");
691 tok++; // eat the comma
693 if ((*tok != CONST) && (*tok != SYMBOL))
696 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
698 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0))
700 // od=0 so optimise it out
701 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
702 AnEXTEN |= EXT_IISNOIN; // No outer displacement
707 // ([bd,An/PC],Xn,od)
713 AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
718 AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
723 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
727 // Defined, absolute values from $FFFF8000..$00007FFF get
728 // optimized to absolute short
729 else if (CHECK_OPTS(OPT_020_DISP)
730 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
731 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
733 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
735 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
739 // Check for final closing parenthesis
746 return error("Closing parenthesis missing on addressing mode");
748 else if (*tok == ',')
750 tok++; // ([bd,An,Xn.size*scale],od)
753 if ((*tok >= KW_A0) && (*tok <= KW_A7))
755 AnEXTEN |= ((*tok & 7) << 12);
759 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
761 AnEXTEN |= ((*tok & 7) << 12);
767 // ([bd,An/PC],Xn.W/L...)
770 // Index reg size: <empty> | .W | .L
781 // .B not allowed here...
786 if (*tok == '*') // ([bd,An/PC],Xn*...)
787 { // scale: *1, *2, *4, *8
792 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
793 return error("scale factor expression must evaluate");
812 else if (*tok++ != CONST)
836 tok++; // Take into account that constants are 64-bit
842 return error("Expected closing bracket ]");
843 tok++; // Eat the bracket
846 if (*tok == ')') // ([bd,An/PC,Xn]...
848 // od is non existent, get out of jail free card
849 //AnEXVAL=0; // zero outer displacement
850 AMn = MEMPRE; // let's say it's ([bd,An,Xn],od) with od suppressed then
851 AnEXTEN |= EXT_IISPREN; // No outer displacement
855 else if (*tok++ != ',')
856 return error("comma expected after ]");
858 if (*tok == SYMBOL || *tok == CONST)
860 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
863 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
865 // od=0 so optimise it out
866 AMn = MEMPRE; // let's say it's ([bd,An],Xn,od) with od=0 then
867 AnEXTEN |= EXT_IISPRE0; // No outer displacement
873 // ([bd,An/PC,Xn],od)
879 AnEXTEN |= EXT_IISPREL;
885 int expr_size = EXT_IISPREW; // Assume we have a .w value
887 if ((AnEXVAL + 0x8000) > 0x10000)
889 // Long value, so mark it as such for now
890 expr_size = EXT_IISPREL;
892 // Defined, absolute values from $FFFF8000..$00007FFF
893 // get optimized to absolute short
894 if (CHECK_OPTS(OPT_020_DISP)
895 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
896 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
898 expr_size = EXT_IISPREW;
900 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
904 AnEXTEN |= expr_size; // Assume we have a .w value
906 // Is .W forced here?
911 if (expr_size == EXT_IISPREL)
912 return error("outer displacement value does not fit in .w size");
916 // Check for final closing parenthesis
923 return error("Closing parenthesis missing on addressing mode");
931 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
934 // It could be that this is really just an expression prefixing a
935 // register as a displacement...
942 // Otherwise, check for PC & etc displacements...
946 if ((*tok >= KW_A0) && (*tok <= KW_A7))
953 // Check if we're actually doing d8(An,Dn) or
954 // (d16,An,Dn[.size][*scale])
955 // TODO: not a very clear cut case from what I can think.
956 // The only way to distinguish between the two is to check
957 // AnEXVAL and see if it's >127 or <-128. But this doesn't
958 // work if AnEXVAL isn't defined yet. For now we fall
959 // through to d8(An,Dn) but this might bite us in the arse
961 if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
963 // We're going to treat it as a full extension format
964 // with no indirect access and no base displacement/
965 // index register suppression
966 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
967 AnEXTEN |= EXT_IISPRE0; // No Memory Indirect Action
968 AnEXTEN |= EXT_BDSIZEL; // Base Displacement Size Long
969 tok++; // Get past the comma
971 // Our expression is techically a base displacement,
972 // so let's copy it to the relevant variables so
973 // eagen0.c can pick it up properly
976 AnBEXATTR = AnEXATTR;
978 if ((*tok >= KW_D0) && (*tok <= KW_D7))
980 AnEXTEN |= ((*tok++) & 7) << 12;
985 // Index reg size: <empty> | .W | .L
996 // .B not allowed here...
1001 if (*tok == '*') // ([bd,An/PC],Xn*...)
1002 { // scale: *1, *2, *4, *8
1007 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
1008 return error("scale factor expression must evaluate");
1027 else if (*tok++ != CONST)
1031 switch ((int)*tok++)
1048 tok++; // Take into account that constants are 64-bit
1053 return error("Closing parenthesis missing on addressing mode");
1055 // Let's say that this is the closest to our case
1066 else if (*tok == ')')
1075 else if (*tok == KW_PC)
1082 else if (*tok == ')')
1084 AMn = PCDISP; // expr(PC)
1095 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
1101 else if (*tok == KW_CCR)
1107 else if (*tok == KW_SR)
1113 else if (*tok == KW_USP)
1117 AnREG = 2; // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
1120 else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
1123 AnREG = *tok++ - KW_IC40;
1125 // After a cache keyword only a comma or EOL is allowed
1126 if ((*tok != ',') && (*tok != EOL))
1130 else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
1133 AnREG = (*tok++) - KW_SFC;
1136 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
1139 AnREG = (*tok++ & 7);
1141 else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
1144 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
1155 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
1165 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1166 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1170 else if (*tok != '(')
1175 // When PC relative is enforced, check for any symbols that aren't
1176 // EQU'd, in this case it's an illegal mode
1177 if (CHECK_OPTS(OPT_PC_RELATIVE))
1178 if (AnEXATTR & REFERENCED)
1179 if (AnEXATTR & DEFINED)
1180 if (!(AnEXATTR & EQUATED))
1181 return error("relocation not allowed");
1183 // .L is forced here
1191 // Defined, absolute values from $FFFF8000..$00007FFF get
1192 // optimized to absolute short
1193 if (CHECK_OPTS(OPT_ABS_SHORT)
1194 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1195 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
1199 if (optim_warn_flag)
1200 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1209 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1223 else if (*tok == KW_PC)
1239 // Addressing mode OK
1244 // Clean up dirty little macros
1259 #undef CHK_FOR_DISPn
1266 #undef IS_SUPPRESSEDn