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
15 if ((*tok >= KW_D0) && (*tok <= KW_D7))
20 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
29 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
35 // Small problem with this is that the opening parentheses might be an
36 // expression that's part of a displacement; this code will falsely flag
41 // (An,Xn[.siz][*scale])
42 // (PC,Xn[.siz][*scale])
44 // (d8,An,Xn[.siz][*scale])
46 // (d8,PC,Xn[.siz][*scale])
55 if ((*tok >= KW_A0) && (*tok <= KW_A7))
75 goto AMn_IX0; // Handle ",Xn[.siz][*scale])"
77 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
79 //Since index register isn't used here, store register number in this field
80 AnIXREG = *tok++ & 7; // (Dn)
84 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
85 AnEXTEN |= EXT_BS; // Base register suppressed
86 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
87 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
89 AnREG = 6 << 3; // stuff 110 to mode field
94 // TODO: does DINDL gets used at all?
96 AnEXTEN = 1 << 1; // Long index size
99 else if (*tok == 'W') // (Dn.w)
101 // TODO: does DINDW gets used at all?
103 AnEXTEN = 1 << 1; // Word index size
106 else if (*tok == ',')
108 // ([bd,An],Xn..) without bd, An
109 // Base displacement is suppressed
110 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
111 AnEXTEN |= EXT_BS; // Base register suppressed
112 AnEXTEN |= EXT_BDSIZE0;
113 AnREG = 6 << 3; // stuff 110 to mode field
119 return error("(Dn) error");
123 { // scale: *1, *2, *4, *8
128 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
129 return error("scale factor expression must evaluate");
148 else if (*tok++ != CONST || *tok > 8)
174 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
175 AnEXTEN |= EXT_BS; // Base register suppressed
176 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
177 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
178 AnREG = 6 << 3; // stuff 110 to mode field
184 tok++; // eat the comma
185 // It might be (Dn[.wl][*scale],od)
186 // Maybe this is wrong and we have to write some code here
187 // instead of reusing that path...
191 return error("unhandled so far");
193 else if (*tok == KW_PC)
194 { // (PC,Xn[.siz][*scale])
198 // Common index handler; enter here with 'tok' pointing at the
201 AMn_IX0: // Handle indexed with missing expr
204 AnEXATTR = ABS | DEFINED;
206 AMn_IXN: // Handle any indexed (tok -> a comma)
211 if (*tok < KW_D0 || *tok > KW_A7)
214 AnIXREG = *tok++ & 15;
217 { // Index reg size: <empty> | .W | .L
227 case DOTB: // .B not allowed here...
232 { // scale: *1, *2, *4, *8
237 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
238 return error("scale factor expression must evaluate");
257 else if (*tok++ != CONST || *tok > 8)
285 if (*tok++ != ')') // final ")"
290 else if (*tok == '[')
293 AnEXTEN|=EXT_FULLWORD; //Definitely using full extension format, so set bit 8
294 // Check to see if base displacement is present
295 //WARNING("expr will return a bad expression error here but this is expected, it needs to be silenced!");
296 if (*tok!=CONST && *tok !=SYMBOL)
297 //if (expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM) != OK)
299 AnEXTEN|=EXT_BDSIZE0;
301 //tok--; //Rewind tok since expr advances it forward
305 expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
306 if (optim_flags[OPT_BASE_DISP] && AnBEXVAL==0 && AnEXATTR!=0)
308 // bd=0 so let's optimise it out
309 AnEXTEN|=EXT_BDSIZE0;
313 AnEXTEN|=EXT_BDSIZEL;
317 { // ([bd[.w],... or ([bd,...
318 // Is .W forced here?
321 AnEXTEN|=EXT_BDSIZEW;
326 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
328 if (optim_flags[OPT_ABS_SHORT]
329 && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
330 && ((AnBEXVAL + 0x8000) < 0x10000))
332 AnEXTEN |= EXT_BDSIZEW;
333 warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
337 AnEXTEN |= EXT_BDSIZEL;
345 // return error("Comma expected after base displacement");
348 // Check for address register or PC, suppress base register
353 AnREG = (7 << 3) | 3; // PC is special case - stuff 011 to register field and 111 to the mode field
356 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
358 AnREG = (6<<3)|*tok & 7;
361 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
365 AnEXTEN |= ((*tok & 7) << 12);
367 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
372 // ([bd,An/PC],Xn.W/L...)
375 // Index reg size: <empty> | .W | .L
386 // .B not allowed here...
392 if (*tok == '*') // ([bd,An/PC],Xn*...)
393 { // scale: *1, *2, *4, *8
398 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
399 return error("scale factor expression must evaluate");
417 else if (*tok++ != CONST || *tok > 8)
439 if (*tok == ']') // ([bd,Dn]...
445 else if (*tok == ']')
447 // PC and Xn is suppressed
448 AnREG = 6 << 3; // stuff 110 to mode field
449 //AnEXTEN|=EXT_BS|EXT_IS;
457 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
466 //Xn and od are non existent, get out of jail free card
468 AMn = MEMPRE; // ([bc,An,Xn],od) with no Xn and od
469 AnEXTEN |= EXT_IS | EXT_IISPREN; //Suppress Xn and od
472 else if (*tok != ',')
473 return error("comma expected after ]");
475 tok++; // eat the comma
477 if ((*tok >= KW_A0) && (*tok <= KW_A7))
479 AnIXREG = ((*tok & 7) << 12);
483 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
485 AnEXTEN |= ((*tok & 7) << 12);
491 //No index found, suppress it
493 tok--; // Rewind tok to point to the comma
494 goto IS_SUPPRESSEDn; // https://xkcd.com/292/ - what does he know anyway?
499 // ([bd,An/PC],Xn.W/L...)
502 // Index reg size: <empty> | .W | .L
513 // .B not allowed here...
519 if (*tok == '*') // ([bd,An/PC],Xn*...)
520 { // scale: *1, *2, *4, *8
525 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
526 return error("scale factor expression must evaluate");
545 else if (*tok++ != CONST || *tok > 8)
569 if (*tok == ')') // ([bd,An/PC],Xn)
571 //od is non existant, get out of jail free card
572 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
573 AnEXTEN |= EXT_IISPOSN; // No outer displacement
578 return error("comma expected");
580 tok++; // eat the comma
583 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
586 if (optim_flags[OPT_BASE_DISP] && (AnEXVAL == 0))
588 // od=0 so optimise it out
589 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
590 AnEXTEN |= EXT_IISPOSN; // No outer displacement
594 // ([bd,An/PC],Xn,od)
598 AnEXTEN |= EXT_IISPOSL; // Long outer displacement
602 // Defined, absolute values from $FFFF8000..$00007FFF get
603 // optimized to absolute short
604 if (optim_flags[OPT_ABS_SHORT]
605 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
606 && ((AnEXVAL + 0x8000) < 0x10000))
608 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
610 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
617 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
620 // Is .W forced here?
627 // Check for final closing parenthesis
634 return error("Closing parenthesis missing on addressing mode");
639 if (*tok == ')') // ([bd,An/PC],Xn)
641 //od is non existant, get out of jail free card
642 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
643 AnEXTEN |= EXT_IISNOIN; // No outer displacement
648 return error("comma expected");
650 tok++; // eat the comma
652 if ((*tok != CONST) && (*tok != SYMBOL))
655 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
657 if (optim_flags[OPT_BASE_DISP] && (AnEXVAL == 0))
659 // od=0 so optimise it out
660 AMn = MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
661 AnEXTEN |= EXT_IISNOIN; // No outer displacement
666 // ([bd,An/PC],Xn,od)
672 AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
677 AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
681 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
686 // Defined, absolute values from $FFFF8000..$00007FFF get
687 // optimized to absolute short
688 else if (optim_flags[OPT_BASE_DISP]
689 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
690 && ((AnEXVAL + 0x8000) < 0x10000))
692 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
693 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
697 // Check for final closing parenthesis
704 return error("Closing parenthesis missing on addressing mode");
706 else if (*tok == ',')
708 *tok++; // ([bd,An,Xn.size*scale],od)
711 if ((*tok >= KW_A0) && (*tok <= KW_A7))
713 AnEXTEN |= ((*tok & 7) << 12);
717 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
719 AnEXTEN |= ((*tok & 7) << 12);
726 // ([bd,An/PC],Xn.W/L...)
729 // Index reg size: <empty> | .W | .L
740 // .B not allowed here...
746 if (*tok == '*') // ([bd,An/PC],Xn*...)
747 { // scale: *1, *2, *4, *8
752 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
753 return error("scale factor expression must evaluate");
771 else if (*tok++ != CONST || *tok > 8)
796 return error("Expected closing bracket ]");
797 tok++; // Eat the bracket
800 if (*tok == ')') // ([bd,An/PC,Xn]...
802 //od is non existant, get out of jail free card
803 //AnEXVAL=0; // zero outer displacement
804 AMn = MEMPRE; // let's say it's ([bd,An,Xn],od) with od suppressed then
805 AnEXTEN |= EXT_IISPREN; // No outer displacement
809 else if (*tok++!=',')
810 return error("comma expected after ]");
812 WARNING(Put symbol and constant checks here!)
814 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
817 if (optim_flags[OPT_BASE_DISP] && (AnEXVAL == 0))
819 // od=0 so optimise it out
820 AMn = MEMPRE; // let's say it's ([bd,An],Xn,od) with od=0 then
821 AnEXTEN |= EXT_IISPRE0; // No outer displacement
826 // ([bd,An/PC,Xn],od)
832 AnEXTEN |= EXT_IISPREL;
839 AnEXTEN |= EXT_IISPREW;
842 // Is .W forced here?
848 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
850 else if (optim_flags[OPT_BASE_DISP]
851 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
852 && ((AnEXVAL + 0x8000) < 0x10000))
854 AnEXTEN |= EXT_IISPREW;
855 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
859 // Check for final closing parenthesis
866 return error("Closing parenthesis missing on addressing mode");
874 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
877 // It could be that this is really just an expression prefixing a
878 // register as a displacement...
885 // Otherwise, check for PC & etc displacements...
889 if ((*tok >= KW_A0) && (*tok <= KW_A7))
899 else if (*tok == ')')
908 else if (*tok == KW_PC)
915 else if (*tok == ')')
917 AMn = PCDISP; // expr(PC)
928 else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
934 else if (*tok == KW_CCR)
940 else if (*tok == KW_SR)
946 else if (*tok == KW_USP)
950 AnREG = 2; //Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
953 else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
956 AnREG = *tok++ - KW_IC40;
958 // After a cache keyword only a comma or EOL is allowed
959 if ((*tok != ',') && (*tok != EOL))
963 else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
966 AnREG = (*tok++) - KW_SFC;
969 else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
972 AnREG = (*tok++ & 7);
974 else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
977 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
988 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
998 if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
999 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
1003 else if (*tok != '(')
1008 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
1009 // to absolute short
1010 if (optim_flags[OPT_ABS_SHORT]
1011 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1012 && ((AnEXVAL + 0x8000) < 0x10000))
1017 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1020 // Is .L forced here?
1032 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1046 else if (*tok == KW_PC)
1062 // Addressing mode OK
1067 // Clean up dirty little macros
1082 #undef CHK_FOR_DISPn
1089 #undef IS_SUPPRESSEDn