2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // PARMODE.C - Addressing Modes Parser Include
4 // Copyright (C) 199x Landon Dyer, 2011-2019 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_BASE_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_ABS_SHORT)
350 && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
351 && (((uint32_t)AnBEXVAL + 0x8000) < 0x10000))
353 AnEXTEN |= EXT_BDSIZEW;
354 warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
358 AnEXTEN |= EXT_BDSIZEL;
366 // return error("Comma expected after base displacement");
369 // Check for address register or PC, suppress base register
374 AnREG = (7 << 3) | 3; // PC is special case - stuff 011 to register field and 111 to the mode field
377 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
379 AnREG = (6 << 3) | (*tok & 7);
382 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
386 AnEXTEN |= ((*tok & 7) << 12);
388 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
392 // ([bd,An/PC],Xn.W/L...)
395 // Index reg size: <empty> | .W | .L
406 // .B not allowed here...
411 if (*tok == '*') // ([bd,An/PC],Xn*...)
412 { // scale: *1, *2, *4, *8
417 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
418 return error("scale factor expression must evaluate");
437 else if (*tok++ != CONST)
458 tok++; // Take into account that constants are 64-bit
461 if (*tok == ']') // ([bd,Dn]...
467 else if (*tok == ']')
469 // PC and Xn is suppressed
470 AnREG = 6 << 3; // stuff 110 to mode field
471 //AnEXTEN|=EXT_BS|EXT_IS;
479 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
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_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?
667 // Check for final closing parenthesis
674 return error("Closing parenthesis missing on addressing mode");
679 if (*tok == ')') // ([bd,An/PC],Xn)
681 // od is non existent, get out of jail free card
682 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
683 AnEXTEN |= EXT_IISNOIN; // No outer displacement
688 return error("comma expected");
690 tok++; // eat the comma
692 if ((*tok != CONST) && (*tok != SYMBOL))
695 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
697 if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0))
699 // od=0 so optimise it out
700 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
701 AnEXTEN |= EXT_IISNOIN; // No outer displacement
706 // ([bd,An/PC],Xn,od)
712 AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
717 AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
722 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
726 // Defined, absolute values from $FFFF8000..$00007FFF get
727 // optimized to absolute short
728 else if (CHECK_OPTS(OPT_BASE_DISP)
729 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
730 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
732 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
733 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
737 // Check for final closing parenthesis
744 return error("Closing parenthesis missing on addressing mode");
746 else if (*tok == ',')
748 tok++; // ([bd,An,Xn.size*scale],od)
751 if ((*tok >= KW_A0) && (*tok <= KW_A7))
753 AnEXTEN |= ((*tok & 7) << 12);
757 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
759 AnEXTEN |= ((*tok & 7) << 12);
765 // ([bd,An/PC],Xn.W/L...)
768 // Index reg size: <empty> | .W | .L
779 // .B not allowed here...
784 if (*tok == '*') // ([bd,An/PC],Xn*...)
785 { // scale: *1, *2, *4, *8
790 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
791 return error("scale factor expression must evaluate");
810 else if (*tok++ != CONST)
834 tok++; // Take into account that constants are 64-bit
840 return error("Expected closing bracket ]");
841 tok++; // Eat the bracket
844 if (*tok == ')') // ([bd,An/PC,Xn]...
846 // od is non existent, get out of jail free card
847 //AnEXVAL=0; // zero outer displacement
848 AMn = MEMPRE; // let's say it's ([bd,An,Xn],od) with od suppressed then
849 AnEXTEN |= EXT_IISPREN; // No outer displacement
853 else if (*tok++ != ',')
854 return error("comma expected after ]");
856 if (*tok == SYMBOL || *tok == CONST)
858 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
861 if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
863 // od=0 so optimise it out
864 AMn = MEMPRE; // let's say it's ([bd,An],Xn,od) with od=0 then
865 AnEXTEN |= EXT_IISPRE0; // No outer displacement
871 // ([bd,An/PC,Xn],od)
877 AnEXTEN |= EXT_IISPREL;
883 int expr_size = EXT_IISPREW; // Assume we have a .w value
885 if ((AnEXVAL + 0x8000) > 0x10000)
887 // Long value, so mark it as such for now
888 expr_size = EXT_IISPREL;
890 // Defined, absolute values from $FFFF8000..$00007FFF
891 // get optimized to absolute short
892 if (CHECK_OPTS(OPT_BASE_DISP)
893 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
894 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
896 expr_size = EXT_IISPREW;
897 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
901 AnEXTEN |= expr_size; // Assume we have a .w value
903 // Is .W forced here?
908 if (expr_size == EXT_IISPREL)
909 return error("outer displacement value does not fit in .w size");
913 // Check for final closing parenthesis
920 return error("Closing parenthesis missing on addressing mode");
928 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
931 // It could be that this is really just an expression prefixing a
932 // register as a displacement...
939 // Otherwise, check for PC & etc displacements...
943 if ((*tok >= KW_A0) && (*tok <= KW_A7))
950 // Check if we're actually doing d8(An,Dn) or
951 // (d16,An,Dn[.size][*scale])
952 // TODO: not a very clear cut case from what I can think.
953 // The only way to distinguish between the two is to check
954 // AnEXVAL and see if it's >127 or <-128. But this doesn't
955 // work if AnEXVAL isn't defined yet. For now we fall
956 // through to d8(An,Dn) but this might bite us in the arse
958 if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
960 // We're going to treat it as a full extension format
961 // with no indirect access and no base displacement/
962 // 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");
1024 else if (*tok++ != CONST)
1028 switch ((int)*tok++)
1045 tok++; // Take into account that constants are 64-bit
1050 return error("Closing parenthesis missing on addressing mode");
1052 // Let's say that this is the closest to our case
1063 else if (*tok == ')')
1072 else if (*tok == KW_PC)
1079 else if (*tok == ')')
1081 AMn = PCDISP; // expr(PC)
1092 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
1098 else if (*tok == KW_CCR)
1104 else if (*tok == KW_SR)
1110 else if (*tok == KW_USP)
1114 AnREG = 2; // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
1117 else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
1120 AnREG = *tok++ - KW_IC40;
1122 // After a cache keyword only a comma or EOL is allowed
1123 if ((*tok != ',') && (*tok != EOL))
1127 else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
1130 AnREG = (*tok++) - KW_SFC;
1133 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
1136 AnREG = (*tok++ & 7);
1138 else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
1141 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
1152 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
1162 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1163 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1167 else if (*tok != '(')
1172 // .L is forced here
1180 // Defined, absolute values from $FFFF8000..$00007FFF get
1181 // optimized to absolute short
1182 if (CHECK_OPTS(OPT_ABS_SHORT)
1183 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1184 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
1189 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1198 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1212 else if (*tok == KW_PC)
1228 // Addressing mode OK
1233 // Clean up dirty little macros
1248 #undef CHK_FOR_DISPn
1255 #undef IS_SUPPRESSEDn