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)
619 // Is .W forced here?
624 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
629 // Is .L forced here?
631 tok++; // Doesn't matter, we're going for .L anyway
634 if (!(AnEXTEN & EXT_BS))
635 AnEXTEN |= EXT_IISPOSL; // Long outer displacement
638 // bd is suppressed, so sticking the od size in bd
639 AnEXTEN |= EXT_BDSIZEL;
640 // And of course the expression has to be copied to
641 // AnBEXPR instead of AnEXPR. Yay. :-/
646 AnBEXPR[i] = AnEXPR[i];
648 } while (AnEXPR[i] != 'E');
655 // Defined, absolute values from $FFFF8000..$00007FFF get
656 // optimized to absolute short
657 if (CHECK_OPTS(OPT_020_DISP)
658 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
659 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
661 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
664 warn("absolute value in outer displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
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;
901 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
905 AnEXTEN |= expr_size; // Assume we have a .w value
907 // Is .W forced here?
912 if (expr_size == EXT_IISPREL)
913 return error("outer displacement value does not fit in .w size");
917 // Check for final closing parenthesis
924 return error("Closing parenthesis missing on addressing mode");
932 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
935 // It could be that this is really just an expression prefixing a
936 // register as a displacement...
943 // Otherwise, check for PC & etc displacements...
947 if ((*tok >= KW_A0) && (*tok <= KW_A7))
954 // Check if we're actually doing d8(An,Dn) or
955 // (d16,An,Dn[.size][*scale])
956 // TODO: not a very clear cut case from what I can think.
957 // The only way to distinguish between the two is to check
958 // AnEXVAL and see if it's >127 or <-128. But this doesn't
959 // work if AnEXVAL isn't defined yet. For now we fall
960 // through to d8(An,Dn) but this might bite us in the arse
962 if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
964 // We're going to treat it as a full extension format
965 // with no indirect access and no base displacement/
966 // index register suppression
967 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
968 AnEXTEN |= EXT_IISPRE0; // No Memory Indirect Action
969 AnEXTEN |= EXT_BDSIZEL; // Base Displacement Size Long
970 tok++; // Get past the comma
972 // Our expression is techically a base displacement,
973 // so let's copy it to the relevant variables so
974 // eagen0.c can pick it up properly
977 AnBEXATTR = AnEXATTR;
979 if ((*tok >= KW_D0) && (*tok <= KW_D7))
981 AnEXTEN |= ((*tok++) & 7) << 12;
986 // Index reg size: <empty> | .W | .L
997 // .B not allowed here...
1002 if (*tok == '*') // ([bd,An/PC],Xn*...)
1003 { // scale: *1, *2, *4, *8
1008 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
1009 return error("scale factor expression must evaluate");
1028 else if (*tok++ != CONST)
1032 switch ((int)*tok++)
1049 tok++; // Take into account that constants are 64-bit
1054 return error("Closing parenthesis missing on addressing mode");
1056 // Let's say that this is the closest to our case
1067 else if (*tok == ')')
1076 else if (*tok == KW_PC)
1083 else if (*tok == ')')
1085 AMn = PCDISP; // expr(PC)
1096 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
1102 else if (*tok == KW_CCR)
1108 else if (*tok == KW_SR)
1114 else if (*tok == KW_USP)
1118 AnREG = 2; // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
1121 else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
1124 AnREG = *tok++ - KW_IC40;
1126 // After a cache keyword only a comma or EOL is allowed
1127 if ((*tok != ',') && (*tok != EOL))
1131 else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
1134 AnREG = (*tok++) - KW_SFC;
1137 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
1140 AnREG = (*tok++ & 7);
1142 else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
1145 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
1156 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
1166 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1167 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1171 else if (*tok != '(')
1176 // When PC relative is enforced, check for any symbols that aren't
1177 // EQU'd, in this case it's an illegal mode
1178 if ((CHECK_OPTS(OPT_PC_RELATIVE)) && (AnEXATTR & REFERENCED) && (AnEXATTR & DEFINED) && (!(AnEXATTR & EQUATED)))
1179 return error("relocation not allowed");
1181 // .L is forced here
1189 // Defined, absolute values from $FFFF8000..$00007FFF get
1190 // optimized to absolute short
1191 if (CHECK_OPTS(OPT_ABS_SHORT)
1192 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1193 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
1197 if (optim_warn_flag)
1198 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1207 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1221 else if (*tok == KW_PC)
1237 // Addressing mode OK
1242 // Clean up dirty little macros
1257 #undef CHK_FOR_DISPn
1264 #undef IS_SUPPRESSEDn