2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // PARMODE.C - Addressing Modes Parser Include
4 // Copyright (C) 199x Landon Dyer, 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?
95 //AMn=DINDL; // (Dn.l)
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
118 return error("(Dn) error");
123 AnEXTEN |= EXT_FULLWORD; // Definitely using full extension format, so set bit 8
124 AnEXTEN |= EXT_BS; // Base register suppressed
125 AnEXTEN |= EXT_BDSIZE0; // Base displacement null
126 AnEXTEN |= EXT_IISPOSN; // Indirect Postindexed with Null Outer Displacement
127 AnREG = 6 << 3; // stuff 110 to mode field
132 return error("unhandled so far");
134 else if (*tok == KW_PC)
135 { // (PC,Xn[.siz][*scale])
139 // Common index handler; enter here with 'tok' pointing at the
142 AMn_IX0: // Handle indexed with missing expr
145 AnEXATTR = ABS | DEFINED;
147 AMn_IXN: // Handle any indexed (tok -> a comma)
152 if (*tok < KW_D0 || *tok > KW_A7)
155 AnIXREG = *tok++ & 15;
158 { // Index reg size: <empty> | .W | .L
168 case DOTB: // .B not allowed here...
173 { // scale: *1, *2, *4, *8
176 if (*tok++ != CONST || *tok > 8)
197 if (*tok++ != ')') // final ")"
202 else if (*tok == '[')
205 AnEXTEN|=EXT_FULLWORD; //Definitely using full extension format, so set bit 8
206 // Check to see if base displacement is present
207 //WARNING("expr will return a bad expression error here but this is expected, it needs to be silenced!");
208 if (*tok!=CONST && *tok !=SYMBOL)
209 //if (expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM) != OK)
211 AnEXTEN|=EXT_BDSIZE0;
213 //tok--; //Rewind tok since expr advances it forward
217 expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
218 if (optim_flags[OPT_BASE_DISP] && AnBEXVAL==0 && AnEXATTR!=0)
220 // bd=0 so let's optimise it out
221 AnEXTEN|=EXT_BDSIZE0;
225 AnEXTEN|=EXT_BDSIZEL;
229 { // ([bd[.w],... or ([bd,...
230 // Is .W forced here?
233 AnEXTEN|=EXT_BDSIZEW;
238 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
240 if (optim_flags[OPT_ABS_SHORT] && (AnBEXATTR & (TDB|DEFINED)) == DEFINED && (AnBEXVAL + 0x8000) < 0x10000)
242 AnEXTEN|=EXT_BDSIZEW;
243 warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
247 AnEXTEN|=EXT_BDSIZEL;
254 // return error("Comma expected after base displacement");
258 // Check for address register or PC, suppress base register otherwise
262 AnREG=(7<<3)|3; // PC is special case - stuff 011 to register field and 111 to the mode field
265 else if ((*tok >= KW_A0) && (*tok <= KW_A7))
267 AnREG = (6<<3)|*tok & 7;
270 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
273 AnEXTEN|=((*tok&7)<<12);
275 AnEXTEN|=EXT_BS; // Oh look, a data register! Which means that base register is suppressed
279 { // ([bd,An/PC],Xn.W/L...)
281 { // Index reg size: <empty> | .W | .L
291 case DOTB: // .B not allowed here...
297 if (*tok=='*') // ([bd,An/PC],Xn*...)
300 if (*tok==CONST) // TODO: I suppose the scale is stored as a CONST and nothing else? So prolly the if is not needed?
307 AnEXTEN |= EXT_TIMES2;
310 AnEXTEN |= EXT_TIMES4;
313 AnEXTEN |= EXT_TIMES8;
319 if (*tok==']') // ([bd,Dn]...
327 // PC and Xn is suppressed
328 AnREG=6<<3; // stuff 110 to mode field
329 //AnEXTEN|=EXT_BS|EXT_IS;
337 // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
339 { //([bd,An/PC],Xn,od)
343 { //Xn and od are non existent, get out of jail free card
344 AMn=MEMPRE; // ([bc,An,Xn],od) with no Xn and od
345 AnEXTEN|=EXT_IS|EXT_IISPREN; //Suppress Xn and od
350 return error("comma expected after ]");
352 tok++; // eat the comma
354 if ((*tok >= KW_A0) && (*tok <= KW_A7))
356 AnIXREG=((*tok&7)<<12);
360 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
362 AnEXTEN|=((*tok&7)<<12);
368 //No index found, suppress it
370 tok--; // Rewind tok to point to the comma
371 goto IS_SUPPRESSEDn; // https://xkcd.com/292/ - what does he know anyway?
375 { // ([bd,An/PC],Xn.W/L...)
377 { // Index reg size: <empty> | .W | .L
387 case DOTB: // .B not allowed here...
393 if (*tok=='*') // ([bd,An/PC],Xn*...)
396 if (*tok==CONST) // TODO: I suppose the scale is stored as a CONST and nothing else? So prolly the if is not needed?
403 AnEXTEN |= EXT_TIMES2;
406 AnEXTEN |= EXT_TIMES4;
409 AnEXTEN |= EXT_TIMES8;
417 if (*tok==')') // ([bd,An/PC],Xn)
418 { //od is non existant, get out of jail free card
419 AMn=MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
420 AnEXTEN|=EXT_IISPOSN; // No outer displacement
425 return error("comma expected");
427 tok++; // eat the comma
430 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
432 if (optim_flags[OPT_BASE_DISP] && AnEXVAL==0)
434 // od=0 so optimise it out
435 AMn=MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
436 AnEXTEN|=EXT_IISPOSN; // No outer displacement
440 // ([bd,An/PC],Xn,od)
444 AnEXTEN|=EXT_IISPOSL; // Long outer displacement
447 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
449 if (optim_flags[OPT_ABS_SHORT] && (AnEXATTR & (TDB|DEFINED)) == DEFINED && (AnEXVAL + 0x8000) < 0x10000)
451 AnEXTEN|=EXT_IISPOSW; // Word outer displacement
453 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
460 AnEXTEN|=EXT_IISPOSW; // Word outer displacement
463 // Is .W forced here?
471 // Check for final closing parenthesis
478 return error("Closing parenthesis missing on addressing mode");
483 if (*tok==')') // ([bd,An/PC],Xn)
484 { //od is non existant, get out of jail free card
485 AMn=MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
486 AnEXTEN|=EXT_IISNOIN; // No outer displacement
491 return error("comma expected");
493 tok++; // eat the comma
495 if (*tok != CONST && *tok != SYMBOL)
496 //if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
498 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
499 if (optim_flags[OPT_BASE_DISP] && AnEXVAL==0)
501 // od=0 so optimise it out
502 AMn=MEMPOST; // let's say it's ([bd,An],Xn,od) with od=0 then
503 AnEXTEN|=EXT_IISNOIN; // No outer displacement
507 // ([bd,An/PC],Xn,od)
513 AnEXTEN|=EXT_IISNOIL; // Long outer displacement with IS suppressed
520 AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
524 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
529 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
531 else if (optim_flags[OPT_BASE_DISP] && (AnEXATTR & (TDB|DEFINED)) == DEFINED && (AnEXVAL + 0x8000) < 0x10000)
533 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
534 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
540 // Check for final closing parenthesis
547 return error("Closing parenthesis missing on addressing mode");
552 *tok++; // ([bd,An,Xn.size*scale],od)
554 if ((*tok >= KW_A0) && (*tok <= KW_A7))
556 AnEXTEN|=((*tok&7)<<12);
560 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
562 AnEXTEN|=((*tok&7)<<12);
568 { // ([bd,An/PC],Xn.W/L...)
570 { // Index reg size: <empty> | .W | .L
580 case DOTB: // .B not allowed here...
587 if (*tok=='*') // ([bd,An/PC],Xn*...)
590 if (*tok==CONST) // TODO: I suppose the scale is stored as a CONST and nothing else? So prolly the if is not needed?
597 AnEXTEN |= EXT_TIMES2;
600 AnEXTEN |= EXT_TIMES4;
603 AnEXTEN |= EXT_TIMES8;
612 return error("Expected closing bracket ]");
613 tok++; // Eat the bracket
616 if (*tok==')') // ([bd,An/PC,Xn]...
617 { //od is non existant, get out of jail free card
618 //AnEXVAL=0; // zero outer displacement
619 AMn=MEMPRE; // let's say it's ([bd,An,Xn],od) with od suppressed then
620 AnEXTEN|=EXT_IISPREN; // No outer displacement
624 else if (*tok++!=',')
625 return error("comma expected after ]");
626 WARNING(Put symbol and constant checks here!)
627 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
629 if (optim_flags[OPT_BASE_DISP] && AnEXVAL==0)
631 // od=0 so optimise it out
632 AMn=MEMPRE; // let's say it's ([bd,An],Xn,od) with od=0 then
633 AnEXTEN|=EXT_IISPRE0; // No outer displacement
637 // ([bd,An/PC,Xn],od)
644 AnEXTEN|=EXT_IISPREL;
652 AnEXTEN|=EXT_IISPREW;
655 // Is .W forced here?
661 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
663 else if (optim_flags[OPT_BASE_DISP] && (AnEXATTR & (TDB|DEFINED)) == DEFINED && (AnEXVAL + 0x8000) < 0x10000)
665 AnEXTEN|=EXT_IISPREW;
666 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
670 // Check for final closing parenthesis
677 return error("Closing parenthesis missing on addressing mode");
686 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
689 // It could be that this is really just an expression prefixing a
690 // register as a displacement...
697 // Otherwise, check for PC & etc displacements...
701 if ((*tok >= KW_A0) && (*tok <= KW_A7))
711 else if (*tok == ')')
720 else if (*tok == KW_PC)
727 else if (*tok == ')')
729 AMn = PCDISP; // expr(PC)
740 else if (*tok=='-' && tok[1]=='(' && ((tok[2]>=KW_A0) && (tok[2]<=KW_A7)) && tok[3]==')')
746 else if (*tok == KW_CCR)
752 else if (*tok == KW_SR)
758 else if (*tok == KW_USP)
762 AnREG=2; //Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
765 else if ((*tok>=KW_IC40) && (*tok<=KW_BC40))
768 AnREG=*tok++-KW_IC40;
770 // After a cache keyword only a comma or EOL is allowed
771 if ((*tok!=',') && (*tok!=EOL))
775 else if ((*tok>=KW_SFC) && (*tok<=KW_CRP))
778 AnREG=(*tok++)-KW_SFC;
781 else if ((*tok>=KW_FP0) && (*tok<=KW_FP7))
786 else if ((*tok>=KW_FPIAR) && (*tok<=KW_FPCR))
789 AnREG=(1<<((*tok++)-KW_FPIAR+10));
800 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
810 if ((AnEXATTR & (TDB|DEFINED)) == DEFINED && (AnEXVAL < 0x10000))
811 AnEXVAL = (int32_t)(int16_t)AnEXVAL; // Sign extend value
815 else if (*tok != '(')
820 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
822 if (optim_flags[OPT_ABS_SHORT] && (AnEXATTR & (TDB|DEFINED)) == DEFINED && (AnEXVAL + 0x8000) < 0x10000)
827 warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
830 // Is .L forced here?
842 if ((*tok >= KW_A0) && (*tok <= KW_A7))
856 else if (*tok == KW_PC)
871 // Addressing mode OK
877 // Clean up dirty little macros
899 #undef IS_SUPPRESSEDn