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_BDSIZE0; // Base displacement null - suppressed
204 return error("unhandled so far");
206 else if (*tok == KW_PC)
207 { // (PC,Xn[.siz][*scale])
211 // Common index handler; enter here with 'tok' pointing at the
214 AMn_IX0: // Handle indexed with missing expr
217 AnEXATTR = ABS | DEFINED;
219 AMn_IXN: // Handle any indexed (tok -> a comma)
224 if (*tok < KW_D0 || *tok > KW_A7)
227 AnIXREG = *tok++ & 15;
230 { // Index reg size: <empty> | .W | .L
240 case DOTB: // .B not allowed here...
245 { // scale: *1, *2, *4, *8
250 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
251 return error("scale factor expression must evaluate");
269 else if (*tok++ != CONST)
290 tok++; // Take into account that constants are 64-bit
296 // If we got here we didn't get any [] stuff
297 // so let's suppress base displacement before
300 AnEXTEN |= EXT_BDSIZE0; // Base displacement null - suppressed
303 if (*tok++ != ')') // final ")"
308 else if (*tok == '[')
311 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
313 // Check to see if base displacement is present
314 if (*tok != CONST && *tok != SYMBOL)
316 AnEXTEN |= EXT_BDSIZE0;
320 expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
321 if (CHECK_OPTS(OPT_BASE_DISP) && AnBEXVAL == 0 && AnEXATTR != 0)
323 // bd=0 so let's optimise it out
324 AnEXTEN|=EXT_BDSIZE0;
326 else if (*tok == DOTL)
328 AnEXTEN |= EXT_BDSIZEL;
332 { // ([bd[.w],... or ([bd,...
333 // Is .W forced here?
336 AnEXTEN |= EXT_BDSIZEW;
341 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
343 if (CHECK_OPTS(OPT_ABS_SHORT)
344 && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
345 && (((uint32_t)AnBEXVAL + 0x8000) < 0x10000))
347 AnEXTEN |= EXT_BDSIZEW;
348 warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
352 AnEXTEN |= EXT_BDSIZEL;
360 // return error("Comma expected after base displacement");
363 // Check for address register or PC, suppress base register
368 AnREG = (7 << 3) | 3; // PC is special case - stuff 011 to register field and 111 to the mode field
371 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
373 AnREG = (6 << 3) | *tok & 7;
376 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
380 AnEXTEN |= ((*tok & 7) << 12);
382 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
387 // ([bd,An/PC],Xn.W/L...)
390 // Index reg size: <empty> | .W | .L
401 // .B not allowed here...
407 if (*tok == '*') // ([bd,An/PC],Xn*...)
408 { // scale: *1, *2, *4, *8
413 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
414 return error("scale factor expression must evaluate");
433 else if (*tok++ != CONST)
454 tok++; // Take into account that constants are 64-bit
457 if (*tok == ']') // ([bd,Dn]...
463 else if (*tok == ']')
465 // PC and Xn is suppressed
466 AnREG = 6 << 3; // stuff 110 to mode field
467 //AnEXTEN|=EXT_BS|EXT_IS;
475 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
484 //Xn and od are non existent, get out of jail free card
486 AMn = MEMPRE; // ([bc,An,Xn],od) with no Xn and od
487 AnEXTEN |= EXT_IS | EXT_IISPREN; //Suppress Xn and od
490 else if (*tok != ',')
491 return error("comma expected after ]");
493 tok++; // eat the comma
495 if ((*tok >= KW_A0) && (*tok <= KW_A7))
497 AnIXREG = ((*tok & 7) << 12);
501 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
503 AnEXTEN |= ((*tok & 7) << 12);
509 //No index found, suppress it
511 tok--; // Rewind tok to point to the comma
512 goto IS_SUPPRESSEDn; // https://xkcd.com/292/ - what does he know anyway?
517 // ([bd,An/PC],Xn.W/L...)
520 // Index reg size: <empty> | .W | .L
531 // .B not allowed here...
537 if (*tok == '*') // ([bd,An/PC],Xn*...)
538 { // scale: *1, *2, *4, *8
543 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
544 return error("scale factor expression must evaluate");
563 else if (*tok++ != CONST)
584 tok++; // Take into account that constants are 64-bit
589 if (*tok == ')') // ([bd,An/PC],Xn)
591 // od is non existent, get out of jail free card
592 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
593 AnEXTEN |= EXT_IISPOSN; // No outer displacement
597 else if (*tok != ',')
598 return error("comma expected");
600 tok++; // eat the comma
603 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
606 if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0))
608 // od=0 so optimise it out
609 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
610 AnEXTEN |= EXT_IISPOSN; // No outer displacement
615 // ([bd,An/PC],Xn,od)
619 AnEXTEN |= EXT_IISPOSL; // Long outer displacement
623 // Defined, absolute values from $FFFF8000..$00007FFF get
624 // optimized to absolute short
625 if (CHECK_OPTS(OPT_ABS_SHORT)
626 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
627 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
629 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
631 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
638 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
641 // Is .W forced here?
648 // Check for final closing parenthesis
655 return error("Closing parenthesis missing on addressing mode");
660 if (*tok == ')') // ([bd,An/PC],Xn)
662 // od is non existent, get out of jail free card
663 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
664 AnEXTEN |= EXT_IISNOIN; // No outer displacement
669 return error("comma expected");
671 tok++; // eat the comma
673 if ((*tok != CONST) && (*tok != SYMBOL))
676 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
678 if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0))
680 // od=0 so optimise it out
681 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
682 AnEXTEN |= EXT_IISNOIN; // No outer displacement
687 // ([bd,An/PC],Xn,od)
693 AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
698 AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
703 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
707 // Defined, absolute values from $FFFF8000..$00007FFF get
708 // optimized to absolute short
709 else if (CHECK_OPTS(OPT_BASE_DISP)
710 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
711 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
713 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
714 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
718 // Check for final closing parenthesis
725 return error("Closing parenthesis missing on addressing mode");
727 else if (*tok == ',')
729 *tok++; // ([bd,An,Xn.size*scale],od)
732 if ((*tok >= KW_A0) && (*tok <= KW_A7))
734 AnEXTEN |= ((*tok & 7) << 12);
738 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
740 AnEXTEN |= ((*tok & 7) << 12);
747 // ([bd,An/PC],Xn.W/L...)
750 // Index reg size: <empty> | .W | .L
761 // .B not allowed here...
767 if (*tok == '*') // ([bd,An/PC],Xn*...)
768 { // scale: *1, *2, *4, *8
773 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
774 return error("scale factor expression must evaluate");
792 else if (*tok++ != CONST)
816 tok++; // Take into account that constants are 64-bit
822 return error("Expected closing bracket ]");
823 tok++; // Eat the bracket
826 if (*tok == ')') // ([bd,An/PC,Xn]...
828 // od is non existent, get out of jail free card
829 //AnEXVAL=0; // zero outer displacement
830 AMn = MEMPRE; // let's say it's ([bd,An,Xn],od) with od suppressed then
831 AnEXTEN |= EXT_IISPREN; // No outer displacement
835 else if (*tok++ != ',')
836 return error("comma expected after ]");
838 if (*tok == SYMBOL || *tok == CONST)
840 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
843 if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
845 // od=0 so optimise it out
846 AMn = MEMPRE; // let's say it's ([bd,An],Xn,od) with od=0 then
847 AnEXTEN |= EXT_IISPRE0; // No outer displacement
853 // ([bd,An/PC,Xn],od)
859 AnEXTEN |= EXT_IISPREL;
865 int expr_size = EXT_IISPREW; // Assume we have a .w value
867 if ((AnEXVAL + 0x8000) > 0x10000)
869 // Long value, so mark it as such for now
870 expr_size = EXT_IISPREL;
872 // Defined, absolute values from $FFFF8000..$00007FFF
873 // get optimized to absolute short
874 if (CHECK_OPTS(OPT_BASE_DISP)
875 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
876 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
878 expr_size = EXT_IISPREW;
879 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
883 AnEXTEN |= expr_size; // Assume we have a .w value
885 // Is .W forced here?
890 if (expr_size == EXT_IISPREL)
891 return error("outer displacement value does not fit in .w size");
895 // Check for final closing parenthesis
902 return error("Closing parenthesis missing on addressing mode");
910 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
913 // It could be that this is really just an expression prefixing a
914 // register as a displacement...
921 // Otherwise, check for PC & etc displacements...
925 if ((*tok >= KW_A0) && (*tok <= KW_A7))
932 // Check if we're actually doing d8(An,Dn) or (d16,An,Dn[.size][*scale])
933 // TODO: not a very clear cut case from what I can think. The only way
934 // to distinguish between the two is to check AnEXVAL and see if it's
935 // >127 or <-128. But this doesn't work if AnEXVAL isn't defined yet.
936 // For now we fall through to d8(An,Dn) but this might bite us in the
937 // arse during fixups...
938 if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
940 // We're going to treat it as a full extension format with no
941 // indirect access and no base displacement/index register suppression
942 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
943 AnEXTEN |= EXT_IISPRE0; // No Memory Indirect Action
944 AnEXTEN |= EXT_BDSIZEL; // Base Displacement Size Long
945 tok++; // Get past the comma
947 // Our expression is techically a base displacement
948 // so let's copy it to the relevant variables so
949 // eagen0.c can pick it up properly
952 AnBEXATTR = AnEXATTR;
954 if ((*tok >= KW_D0) && (*tok <= KW_D7))
956 AnEXTEN |= ((*tok++) & 7) << 12;
961 // Index reg size: <empty> | .W | .L
972 // .B not allowed here...
977 if (*tok == '*') // ([bd,An/PC],Xn*...)
978 { // scale: *1, *2, *4, *8
983 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
984 return error("scale factor expression must evaluate");
1002 else if (*tok++ != CONST)
1006 switch ((int)*tok++)
1023 tok++; // Take into account that constants are 64-bit
1028 return error("Closing parenthesis missing on addressing mode");
1030 // Let's say that this is the closest to our case
1041 else if (*tok == ')')
1050 else if (*tok == KW_PC)
1057 else if (*tok == ')')
1059 AMn = PCDISP; // expr(PC)
1070 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
1076 else if (*tok == KW_CCR)
1082 else if (*tok == KW_SR)
1088 else if (*tok == KW_USP)
1092 AnREG = 2; // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
1095 else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
1098 AnREG = *tok++ - KW_IC40;
1100 // After a cache keyword only a comma or EOL is allowed
1101 if ((*tok != ',') && (*tok != EOL))
1105 else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
1108 AnREG = (*tok++) - KW_SFC;
1111 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
1114 AnREG = (*tok++ & 7);
1116 else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
1119 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
1130 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
1140 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1141 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1145 else if (*tok != '(')
1150 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
1151 // to absolute short
1152 if (CHECK_OPTS(OPT_ABS_SHORT)
1153 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1154 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
1159 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1162 // Is .L forced here?
1174 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1188 else if (*tok == KW_PC)
1204 // Addressing mode OK
1209 // Clean up dirty little macros
1224 #undef CHK_FOR_DISPn
1231 #undef IS_SUPPRESSEDn