2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // PARMODE.C - Addressing Modes Parser Include
4 // Copyright (C) 199x Landon Dyer, 2011-2017 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");
271 else if (*tok++ != CONST)
292 tok++; // Take into account that constants are 64-bit
298 // If we got here we didn't get any [] stuff
299 // so let's suppress base displacement before
302 AnEXTEN |= EXT_BDSIZE0; // Base displacement null - suppressed
305 if (*tok++ != ')') // final ")"
310 else if (*tok == '[')
313 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
315 // Check to see if base displacement is present
316 if (*tok != CONST && *tok != SYMBOL)
318 AnEXTEN |= EXT_BDSIZE0;
322 expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
323 if (CHECK_OPTS(OPT_BASE_DISP) && AnBEXVAL == 0 && AnEXATTR != 0)
325 // bd=0 so let's optimise it out
326 AnEXTEN|=EXT_BDSIZE0;
328 else if (*tok == DOTL)
330 AnEXTEN |= EXT_BDSIZEL;
334 { // ([bd[.w],... or ([bd,...
335 // Is .W forced here?
338 AnEXTEN |= EXT_BDSIZEW;
343 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
345 if (CHECK_OPTS(OPT_ABS_SHORT)
346 && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
347 && (((uint32_t)AnBEXVAL + 0x8000) < 0x10000))
349 AnEXTEN |= EXT_BDSIZEW;
350 warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
354 AnEXTEN |= EXT_BDSIZEL;
362 // return error("Comma expected after base displacement");
365 // Check for address register or PC, suppress base register
370 AnREG = (7 << 3) | 3; // PC is special case - stuff 011 to register field and 111 to the mode field
373 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
375 AnREG = (6 << 3) | (*tok & 7);
378 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
382 AnEXTEN |= ((*tok & 7) << 12);
384 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
389 // ([bd,An/PC],Xn.W/L...)
392 // Index reg size: <empty> | .W | .L
403 // .B not allowed here...
409 if (*tok == '*') // ([bd,An/PC],Xn*...)
410 { // scale: *1, *2, *4, *8
415 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
416 return error("scale factor expression must evaluate");
435 else if (*tok++ != CONST)
456 tok++; // Take into account that constants are 64-bit
459 if (*tok == ']') // ([bd,Dn]...
465 else if (*tok == ']')
467 // PC and Xn is suppressed
468 AnREG = 6 << 3; // stuff 110 to mode field
469 //AnEXTEN|=EXT_BS|EXT_IS;
477 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
486 //Xn and od are non existent, get out of jail free card
488 AMn = MEMPRE; // ([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 >= KW_A0) && (*tok <= KW_A7))
499 AnIXREG = ((*tok & 7) << 12);
503 else if ((*tok >= KW_D0) && (*tok <= KW_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?
519 // ([bd,An/PC],Xn.W/L...)
522 // Index reg size: <empty> | .W | .L
533 // .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_BASE_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_ABS_SHORT)
647 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
648 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
650 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
652 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
659 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
662 // 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_BASE_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_BASE_DISP)
731 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
732 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
734 //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);
768 // ([bd,An/PC],Xn.W/L...)
771 // Index reg size: <empty> | .W | .L
782 // .B not allowed here...
788 if (*tok == '*') // ([bd,An/PC],Xn*...)
789 { // scale: *1, *2, *4, *8
794 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
795 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_BASE_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_BASE_DISP)
896 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
897 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
899 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 (d16,An,Dn[.size][*scale])
954 // TODO: not a very clear cut case from what I can think. The only way
955 // to distinguish between the two is to check AnEXVAL and see if it's
956 // >127 or <-128. But this doesn't work if AnEXVAL isn't defined yet.
957 // For now we fall through to d8(An,Dn) but this might bite us in the
958 // arse during fixups...
959 if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
961 // We're going to treat it as a full extension format with no
962 // indirect access and no base displacement/index register suppression
963 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
964 AnEXTEN |= EXT_IISPRE0; // No Memory Indirect Action
965 AnEXTEN |= EXT_BDSIZEL; // Base Displacement Size Long
966 tok++; // Get past the comma
968 // Our expression is techically a base displacement
969 // so let's copy it to the relevant variables so
970 // eagen0.c can pick it up properly
973 AnBEXATTR = AnEXATTR;
975 if ((*tok >= KW_D0) && (*tok <= KW_D7))
977 AnEXTEN |= ((*tok++) & 7) << 12;
982 // Index reg size: <empty> | .W | .L
993 // .B not allowed here...
998 if (*tok == '*') // ([bd,An/PC],Xn*...)
999 { // scale: *1, *2, *4, *8
1004 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
1005 return error("scale factor expression must evaluate");
1023 else if (*tok++ != CONST)
1027 switch ((int)*tok++)
1044 tok++; // Take into account that constants are 64-bit
1049 return error("Closing parenthesis missing on addressing mode");
1051 // Let's say that this is the closest to our case
1062 else if (*tok == ')')
1071 else if (*tok == KW_PC)
1078 else if (*tok == ')')
1080 AMn = PCDISP; // expr(PC)
1091 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
1097 else if (*tok == KW_CCR)
1103 else if (*tok == KW_SR)
1109 else if (*tok == KW_USP)
1113 AnREG = 2; // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
1116 else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
1119 AnREG = *tok++ - KW_IC40;
1121 // After a cache keyword only a comma or EOL is allowed
1122 if ((*tok != ',') && (*tok != EOL))
1126 else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
1129 AnREG = (*tok++) - KW_SFC;
1132 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
1135 AnREG = (*tok++ & 7);
1137 else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
1140 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
1151 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
1161 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1162 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1166 else if (*tok != '(')
1171 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
1172 // to absolute short
1173 if (CHECK_OPTS(OPT_ABS_SHORT)
1174 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1175 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
1180 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1183 // Is .L forced here?
1195 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1209 else if (*tok == KW_PC)
1225 // Addressing mode OK
1230 // Clean up dirty little macros
1245 #undef CHK_FOR_DISPn
1252 #undef IS_SUPPRESSEDn