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)
58 int ea_PC = 0; // Flag that let us know if we have PC or An relative ea
61 if ((*tok >= KW_A0) && (*tok <= KW_A7))
81 goto AMn_IX0; // Handle ",Xn[.siz][*scale])"
83 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
85 // Since index register isn't used here, store register number in this field
86 AnIXREG = *tok++ & 7; // (Dn)
91 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
92 AnEXTEN |= EXT_BS; // Base register suppressed
93 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
94 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
96 AnREG = 6 << 3; // stuff 110 to mode field
101 AMn = DINDL; // (Dn.l)
102 AnEXTEN = 1 << 11; // Long index size
105 else if (*tok == 'W') // (Dn.w)
108 AnEXTEN = 0 << 11; // Word index size
111 else if (*tok == ',')
113 // ([bd,An],Xn..) without bd, An
114 // Base displacement is suppressed
115 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
116 AnEXTEN |= EXT_BS; // Base register suppressed
117 AnEXTEN |= EXT_BDSIZE0;
118 AnREG = 6 << 3; // stuff 110 to mode field
124 return error("(Dn) error");
128 { // scale: *1, *2, *4, *8
133 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
134 return error("scale factor expression must evaluate");
156 else if (*tok++ != CONST)
180 tok++; // Take into account that constants are 64-bit
187 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
188 AnEXTEN |= EXT_BS; // Base register suppressed
189 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
190 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
191 AnREG = 6 << 3; // stuff 110 to mode field
195 else if (*tok == ',')
197 tok++; // eat the comma
198 // It might be (Dn[.wl][*scale],od)
199 // Maybe this is wrong and we have to write some code here
200 // instead of reusing that path...
201 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
202 AnEXTEN |= EXT_BS; // Base displacement null - suppressed
203 AnEXTEN |= AnIXREG << 12;
207 return error("unhandled so far");
209 else if (*tok == KW_PC)
210 { // (PC,Xn[.siz][*scale])
214 // Common index handler; enter here with 'tok' pointing at the
217 AMn_IX0: // Handle indexed with missing expr
220 AnEXATTR = ABS | DEFINED;
222 AMn_IXN: // Handle any indexed (tok -> a comma)
227 if (*tok < KW_D0 || *tok > KW_A7)
230 AnIXREG = *tok++ & 15;
233 { // Index reg size: <empty> | .W | .L
243 case DOTB: // .B not allowed here...
248 { // scale: *1, *2, *4, *8
253 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
254 return error("scale factor expression must evaluate");
273 else if (*tok++ != CONST)
294 tok++; // Take into account that constants are 64-bit
300 // If we got here we didn't get any [] stuff
301 // so let's suppress base displacement before
304 AnEXTEN |= EXT_BDSIZE0; // Base displacement null - suppressed
307 if (*tok++ != ')') // final ")"
312 else if (*tok == '[')
315 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
317 // Check to see if base displacement is present
318 if (*tok != CONST && *tok != SYMBOL)
320 AnEXTEN |= EXT_BDSIZE0;
324 expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
326 if (CHECK_OPTS(OPT_020_DISP) && (AnBEXVAL == 0) && (AnEXATTR != 0))
328 // bd = 0 so let's optimise it out
329 AnEXTEN |= EXT_BDSIZE0;
331 else if (*tok == DOTL)
334 AnEXTEN |= EXT_BDSIZEL;
339 // ([bd[.w],... or ([bd,...
340 // Is .W forced here?
343 AnEXTEN |= EXT_BDSIZEW;
348 // Defined, absolute values from $FFFF8000..$00007FFF
349 // get optimized to absolute short
350 if (CHECK_OPTS(OPT_020_DISP)
351 && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
352 && (((uint32_t)AnBEXVAL + 0x8000) < 0x10000))
354 AnEXTEN |= EXT_BDSIZEW;
357 warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
361 AnEXTEN |= EXT_BDSIZEL;
370 // Check for address register or PC,
371 // suppress base register otherwise
375 ea_PC = 3; // Set flag in order to set proper value to AMn below when we can make a decision on ea
376 // (why "3"? Well, MEMPOST is 3 away from PCMPOST, etc. Have a look at amode.h)
377 AnREG = (7 << 3) | 3; // PC is special case - stuff 011 to register field and 111 to the mode field
380 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
382 AnREG = (6 << 3) | (*tok & 7);
385 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
389 AnEXTEN |= ((*tok & 7) << 12);
391 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
395 // ([bd,An/PC],Xn.W/L...)
398 // Index reg size: <empty> | .W | .L
409 // .B not allowed here...
414 if (*tok == '*') // ([bd,An/PC],Xn*...)
415 { // scale: *1, *2, *4, *8
420 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
421 return error("scale factor expression must evaluate");
440 else if (*tok++ != CONST)
461 tok++; // Take into account that constants are 64-bit
464 if (*tok == ']') // ([bd,Dn]...
470 else if (*tok == ']')
472 // PC and Xn is suppressed
473 AnREG = 6 << 3; // stuff 110b to mode field
481 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
484 // ([bd,An/PC],Xn,od)
490 // Xn and od are non existent, get out of jail free card
492 AMn = MEMPRE + ea_PC; // ([bc,An,Xn],od) with no Xn and od
493 AnEXTEN |= EXT_IS | EXT_IISPREN; // Suppress Xn and od
496 else if (*tok != ',')
497 return error("comma expected after ]");
499 tok++; // eat the comma
501 if ((*tok >= KW_A0) && (*tok <= KW_A7))
503 AnIXREG = ((*tok & 7) << 12);
507 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
509 AnEXTEN |= ((*tok & 7) << 12);
515 // No index found, suppress it
517 tok--; // Rewind tok to point to the comma
518 goto IS_SUPPRESSEDn; // https://xkcd.com/292/ - what does he know anyway?
522 // ([bd,An/PC],Xn.W/L...)
525 // Index reg size: <empty> | .W | .L
534 // .B not allowed here...
540 // Check for Xn scale
541 if (*tok == '*') // ([bd,An/PC],Xn*...)
542 { // scale: *1, *2, *4, *8
547 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
548 return error("scale factor expression must evaluate");
550 else if (*tok == CONST)
552 scaleval = (int)*tok++;
553 tok++; // Take into account that constants are 64-bit
578 if (*tok == ')') // ([bd,An/PC],Xn)
580 // od is non existent, get out of jail free card
581 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
582 AnEXTEN |= EXT_IISPOSN; // No outer displacement
586 else if (*tok != ',')
587 return error("comma expected");
589 tok++; // eat the comma
592 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
595 if (CHECK_OPTS(OPT_020_DISP) && (AnEXATTR & DEFINED) && (AnEXVAL == 0))
597 // od = 0 so optimise it out
598 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
599 AnEXTEN |= EXT_IISPOSN; // No outer displacement
604 // ([bd,An/PC],Xn,od)
605 // Is .W forced here?
610 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
611 AMn = MEMPOST + ea_PC;
615 // Is .L forced here?
617 tok++; // Doesn't matter, we're going for .L anyway
620 if (!(AnEXTEN & EXT_BS))
621 AnEXTEN |= EXT_IISPOSL; // Long outer displacement
624 // bd is suppressed, so sticking the od size in bd
625 AnEXTEN |= EXT_BDSIZEL;
626 // And of course the expression has to be copied to
627 // AnBEXPR instead of AnEXPR. Yay. :-/
632 AnBEXPR[i] = AnEXPR[i];
634 } while (AnEXPR[i] != 'E');
639 AMn = MEMPOST + ea_PC;
641 // Defined, absolute values from $FFFF8000..$00007FFF get
642 // optimized to absolute short
643 if (CHECK_OPTS(OPT_020_DISP)
644 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
645 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
647 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
648 AMn = MEMPOST + ea_PC;
650 warn("absolute value in outer displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
654 // Check for final closing parenthesis
661 return error("Closing parenthesis missing on addressing mode");
666 if (*tok == ')') // ([bd,An/PC],Xn)
668 // od is non existent, get out of jail free card
669 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
670 AnEXTEN |= EXT_IISNOIN; // No outer displacement
675 return error("comma expected");
677 tok++; // eat the comma
679 if ((*tok != CONST) && (*tok != SYMBOL))
682 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
684 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0))
686 // od=0 so optimise it out
687 AMn = MEMPOST + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
688 AnEXTEN |= EXT_IISNOIN; // No outer displacement
693 // ([bd,An/PC],Xn,od)
698 AMn = MEMPOST + ea_PC;
699 AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
704 AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
705 AMn = MEMPRE + ea_PC;;
709 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
710 AMn = MEMPOST + ea_PC;
713 // Defined, absolute values from $FFFF8000..$00007FFF get
714 // optimized to absolute short
715 else if (CHECK_OPTS(OPT_020_DISP)
716 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
717 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
719 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
721 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
725 // Check for final closing parenthesis
732 return error("Closing parenthesis missing on addressing mode");
734 else if (*tok == ',')
736 tok++; // ([bd,An,Xn.size*scale],od)
739 if ((*tok >= KW_A0) && (*tok <= KW_A7))
741 AnEXTEN |= ((*tok & 7) << 12);
745 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
747 AnEXTEN |= ((*tok & 7) << 12);
753 // ([bd,An/PC],Xn.W/L...)
756 // Index reg size: <empty> | .W | .L
767 // .B not allowed here...
772 if (*tok == '*') // ([bd,An/PC],Xn*...)
773 { // scale: *1, *2, *4, *8
778 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
779 return error("scale factor expression must evaluate");
798 else if (*tok++ != CONST)
822 tok++; // Take into account that constants are 64-bit
828 return error("Expected closing bracket ]");
829 tok++; // Eat the bracket
832 if (*tok == ')') // ([bd,An/PC,Xn]...
834 // od is non existent, get out of jail free card
835 AMn = MEMPRE + ea_PC; // let's say it's ([bd,An,Xn],od) with od suppressed then
836 AnEXTEN |= EXT_IISPREN; // No outer displacement
840 else if (*tok++ != ',')
841 return error("comma expected after ]");
843 if (*tok == SYMBOL || *tok == CONST)
845 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
848 if (CHECK_OPTS(OPT_020_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
850 // od=0 so optimise it out
851 AMn = MEMPRE + ea_PC; // let's say it's ([bd,An],Xn,od) with od=0 then
852 AnEXTEN |= EXT_IISPRE0; // No outer displacement
858 // ([bd,An/PC,Xn],od)
862 AMn = MEMPRE + ea_PC;
864 AnEXTEN |= EXT_IISPREL;
869 AMn = MEMPRE + ea_PC;
870 int expr_size = EXT_IISPREW; // Assume we have a .w value
872 if ((AnEXVAL + 0x8000) > 0x10000)
874 // Long value, so mark it as such for now
875 expr_size = EXT_IISPREL;
877 // Defined, absolute values from $FFFF8000..$00007FFF
878 // get optimized to absolute short
879 if (CHECK_OPTS(OPT_020_DISP)
880 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
881 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
883 expr_size = EXT_IISPREW;
886 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
890 AnEXTEN |= expr_size; // Assume we have a .w value
892 // Is .W forced here?
897 if (expr_size == EXT_IISPREL)
898 return error("outer displacement value does not fit in .w size");
902 // Check for final closing parenthesis
909 return error("Closing parenthesis missing on addressing mode");
917 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
920 // It could be that this is really just an expression prefixing a
921 // register as a displacement...
928 // Otherwise, check for PC & etc displacements...
932 if ((*tok >= KW_A0) && (*tok <= KW_A7))
939 // Check if we're actually doing d8(An,Dn) or
940 // (d16,An,Dn[.size][*scale])
941 // TODO: not a very clear cut case from what I can think.
942 // The only way to distinguish between the two is to check
943 // AnEXVAL and see if it's >127 or <-128. But this doesn't
944 // work if AnEXVAL isn't defined yet. For now we fall
945 // through to d8(An,Dn) but this might bite us in the arse
947 if ((AnEXATTR & DEFINED) && (AnEXVAL + 0x80 > 0x100))
949 // We're going to treat it as a full extension format
950 // with no indirect access and no base displacement/
951 // index register suppression
952 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
953 AnEXTEN |= EXT_IISPRE0; // No Memory Indirect Action
954 AnEXTEN |= EXT_BDSIZEL; // Base Displacement Size Long
955 tok++; // Get past the comma
957 // Our expression is techically a base displacement,
958 // so let's copy it to the relevant variables so
959 // eagen0.c can pick it up properly
961 AnBEXATTR = AnEXATTR;
963 if ((*tok >= KW_D0) && (*tok <= KW_D7))
965 AnEXTEN |= ((*tok++) & 7) << 12;
970 // Index reg size: <empty> | .W | .L
981 // .B not allowed here...
986 if (*tok == '*') // (d16,An,Dn[.size][*scale])
987 { // scale: *1, *2, *4, *8
992 if (expr(scaleexpr, &scaleval, &scaleattr, &scaleesym) != OK)
993 return error("scale factor expression must evaluate");
1012 else if (*tok++ != CONST)
1016 switch ((int)*tok++)
1033 tok++; // Take into account that constants are 64-bit
1038 return error("Closing parenthesis missing on addressing mode");
1040 // Let's say that this is the closest to our case
1051 else if (*tok == ')')
1060 else if (*tok == KW_PC)
1067 else if (*tok == ')')
1069 AMn = PCDISP; // expr(PC)
1080 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
1086 else if (*tok == KW_CCR)
1092 else if (*tok == KW_SR)
1098 else if (*tok == KW_USP)
1102 AnREG = 2; // Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
1105 else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
1108 AnREG = *tok++ - KW_IC40;
1110 // After a cache keyword only a comma or EOL is allowed
1111 if ((*tok != ',') && (*tok != EOL))
1115 else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
1118 AnREG = (*tok++) - KW_SFC;
1121 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
1124 AnREG = (*tok++ & 7);
1126 else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
1129 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
1140 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
1150 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1151 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1155 else if (*tok != '(')
1160 // When PC relative is enforced, check for any symbols that aren't
1161 // EQU'd, in this case it's an illegal mode
1162 if ((CHECK_OPTS(OPT_PC_RELATIVE)) && (AnEXATTR & REFERENCED) && (AnEXATTR & DEFINED) && (!(AnEXATTR & EQUATED)))
1163 return error("relocation not allowed");
1165 // .L is forced here
1173 // Defined, absolute values from $FFFF8000..$00007FFF get
1174 // optimized to absolute short
1175 if (CHECK_OPTS(OPT_ABS_SHORT)
1176 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1177 && (((uint32_t)AnEXVAL + 0x8000) < 0x10000))
1181 if (optim_warn_flag)
1182 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1191 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1205 else if (*tok == KW_PC)
1221 // Addressing mode OK
1226 // Clean up dirty little macros
1241 #undef CHK_FOR_DISPn
1248 #undef IS_SUPPRESSEDn