Version bump (1.8.4) for last commit.
[rmac] / parmode.h
1 //
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
7 //
8
9 // This file is included (twice) to parse two addressing modes, into slightly
10 // different var names
11 {
12         // Dn
13         // An
14         // # expression
15         if ((*tok >= KW_D0) && (*tok <= KW_D7))
16         {
17                 AMn = DREG;
18                 AnREG = *tok++ & 7;
19         }
20         else if ((*tok >= KW_A0) && (*tok <= KW_A7))
21         {
22                 AMn = AREG;
23                 AnREG = *tok++ & 7;
24         }
25         else if (*tok == '#')
26         {
27                 tok++;
28
29                 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
30                         return ERROR;
31
32                 AMn = IMMED;
33         }
34
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
37         // that as an error.
38
39         // (An)
40         // (An)+
41         // (An,Xn[.siz][*scale])
42         // (PC,Xn[.siz][*scale])
43         // (d16,An)
44         // (d8,An,Xn[.siz][*scale])
45         // (d16,PC)
46         // (d8,PC,Xn[.siz][*scale])
47         // ([bd,An],Xn,od)
48         // ([bd,An,Xn],od)
49         // ([bd,PC],Xn,od)
50         // ([bd,PC,Xn],od)
51         else if (*tok == '(')
52         {
53                 tok++;
54
55                 if ((*tok >= KW_A0) && (*tok <= KW_A7))
56                 {
57                         AnREG = *tok++ & 7;
58
59                         if (*tok == ')')
60                         {
61                                 tok++;
62
63                                 if (*tok == '+')
64                                 {
65                                         tok++;
66                                         AMn = APOSTINC;
67                                 }
68                                 else
69                                         AMn = AIND;
70
71                                 goto AnOK;
72                         }
73
74                         AMn = AINDEXED;
75                         goto AMn_IX0;            // Handle ",Xn[.siz][*scale])"
76                 }
77                 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
78                 {
79                         //Since index register isn't used here, store register number in this field
80                         AnIXREG = *tok++ & 7;                                // (Dn)
81                         if (*tok == ')')
82                         {
83                                 tok++;
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
88                                 AMn= MEMPOST;
89                                 AnREG = 6 << 3;         // stuff 110 to mode field
90                                 goto AnOK;
91                         }
92                         else if (*tok == 'L')
93                         {
94                                 // TODO: does DINDL gets used at all?
95                                 AMn=DINDL;                                     // (Dn.l)
96                                 AnEXTEN = 1 << 1;   // Long index size
97                                 tok++;
98                         }
99                         else if (*tok == 'W')                                // (Dn.w)
100                         {
101                                 // TODO: does DINDW gets used at all?
102                                 AMn=DINDW;
103                                 AnEXTEN = 1 << 1;   // Word index size
104                                 tok++;
105                         }
106                         else if (*tok == ',')
107                         {
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
114                                 tok++;
115                                 goto CHECKODn;
116                         }
117                         else
118                         {
119                                 return error("(Dn) error");
120                         }
121
122                         if (*tok == '*')
123                         {                        // scale: *1, *2, *4, *8
124                                 tok++;
125
126                                 if (*tok == SYMBOL)
127                                 {
128                                         if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
129                                                 return error("scale factor expression must evaluate");
130
131                                         switch (AnEXVAL)
132                                         {
133                                         case 1:
134                                                 break;
135                                         case 2:
136                                                 AnIXSIZ |= TIMES2;
137                                                 break;
138                                         case 4:
139                                                 AnIXSIZ |= TIMES4;
140                                                 break;
141                                         case 8:
142                                                 AnIXSIZ |= TIMES8;
143                                                 break;
144                                         default:
145                                                 goto badmode;
146                                         }
147                                 }
148                                 else if (*tok++ != CONST || *tok > 8)
149                                         goto badmode;
150                                 else
151                                 {
152                                         switch ((int)*tok++)
153                                         {
154                                         case 1:
155                                                 break;
156                                         case 2:
157                                                 AnIXSIZ |= TIMES2;
158                                                 break;
159                                         case 4:
160                                                 AnIXSIZ |= TIMES4;
161                                                 break;
162                                         case 8:
163                                                 AnIXSIZ |= TIMES8;
164                                                 break;
165                                         default:
166                                                 goto badmode;
167                                         }
168                                 }
169                         }
170
171                         if (*tok == ')')
172                         {
173                                 tok++;
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
179                                 AMn = MEMPOST;
180                                 goto AnOK;
181                         }
182                         else if (*tok==',')
183                         {
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...
188                                 goto CHECKODn;
189                         }
190                         else
191                                 return error("unhandled so far");
192                 }
193                 else if (*tok == KW_PC)
194                 {                            // (PC,Xn[.siz][*scale])
195                         tok++;
196                         AMn = PCINDEXED;
197
198                         // Common index handler; enter here with 'tok' pointing at the
199                         // comma.
200
201                         AMn_IX0:                 // Handle indexed with missing expr
202
203                         AnEXVAL = 0;
204                         AnEXATTR = ABS | DEFINED;
205
206                         AMn_IXN:                 // Handle any indexed (tok -> a comma)
207
208                         if (*tok++ != ',')
209                                 goto badmode;
210
211                         if (*tok < KW_D0 || *tok > KW_A7)
212                                 goto badmode;
213
214                         AnIXREG = *tok++ & 15;
215
216                         switch ((int)*tok)
217                         {                        // Index reg size: <empty> | .W | .L
218                         case DOTW:
219                                 tok++;
220                         default:
221                                 AnIXSIZ = 0;
222                                 break;
223                         case DOTL:
224                                 AnIXSIZ = 0x0800;
225                                 tok++;
226                                 break;
227                         case DOTB:               // .B not allowed here...
228                                 goto badmode;
229                         }
230
231                         if (*tok == '*')
232                         {                        // scale: *1, *2, *4, *8
233                                 tok++;
234
235                                 if (*tok == SYMBOL)
236                                 {
237                                         if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
238                                                 return error("scale factor expression must evaluate");
239                                         switch (AnEXVAL)
240                                         {
241                                         case 1:
242                                                 break;
243                                         case 2:
244                                                 AnIXSIZ |= TIMES2;
245                                                 break;
246                                         case 4:
247                                                 AnIXSIZ |= TIMES4;
248                                                 break;
249                                         case 8:
250                                                 AnIXSIZ |= TIMES8;
251                                                 break;
252                                         default:
253                                                 goto badmode;
254                                         }
255                                 }
256                                 else if (*tok++ != CONST || *tok > 8)
257                                         goto badmode;
258                                 else
259                                 {
260                                         switch ((int)*tok++)
261                                         {
262                                         case 1:
263                                                 break;
264                                         case 2:
265                                                 AnIXSIZ |= TIMES2;
266                                                 break;
267                                         case 4:
268                                                 AnIXSIZ |= TIMES4;
269                                                 break;
270                                         case 8:
271                                                 AnIXSIZ |= TIMES8;
272                                                 break;
273                                         default:
274                                                 goto badmode;
275                                         }
276                                 }
277                         }
278
279                         if (*tok == ',')
280                         {
281                                 tok++;
282                                 goto CHECKODn;
283                         }
284                         if (*tok++ != ')')         // final ")"
285                                 goto badmode;
286
287                         goto AnOK;
288                 }
289                 else if (*tok == '[')
290                 {                              // ([...
291                         tok++;
292                         AnEXTEN|=EXT_FULLWORD;     //Definitely using full extension format, so set bit 8
293                         // Check to see if base displacement is present
294                         if (*tok!=CONST && *tok !=SYMBOL)
295                         {
296                                 AnEXTEN|=EXT_BDSIZE0;
297                         }
298                         else
299                         {
300                                 expr(AnBEXPR, &AnBEXVAL, &AnBEXATTR, &AnESYM);
301                                 if (CHECK_OPTS(OPT_BASE_DISP) && AnBEXVAL==0 && AnEXATTR!=0)
302                                 {
303                                         // bd=0 so let's optimise it out
304                                         AnEXTEN|=EXT_BDSIZE0;
305                                 }
306                                 else if (*tok==DOTL)
307                                 {                                               // ([bd.l,...
308                                                 AnEXTEN|=EXT_BDSIZEL;
309                                                 tok++;
310                                 }
311                                 else
312                                 {                                               // ([bd[.w],... or ([bd,...
313                                         // Is .W forced here?
314                                         if (*tok == DOTW)
315                                         {
316                                         AnEXTEN|=EXT_BDSIZEW;
317                                                 tok++;
318                                         }
319                                         else
320                                         {
321                                                 // Defined, absolute values from $FFFF8000..$00007FFF get optimized
322                                                 // to absolute short
323                                                 if (CHECK_OPTS(OPT_ABS_SHORT)
324                                                         && ((AnBEXATTR & (TDB | DEFINED)) == DEFINED)
325                                                         && ((AnBEXVAL + 0x8000) < 0x10000))
326                                                 {
327                                                         AnEXTEN |= EXT_BDSIZEW;
328                                                         warn("absolute value in base displacement ranging $FFFF8000..$00007FFF optimised to absolute short");
329                                                 }
330                                                 else
331                                                 {
332                                                         AnEXTEN |= EXT_BDSIZEL;
333                                                 }
334                                         }
335                                 }
336
337                                 if (*tok == ',')
338                                         tok++;
339                                 //else
340                                 //      return error("Comma expected after base displacement");
341                         }
342
343                         // Check for address register or PC, suppress base register
344                         // otherwise
345
346                         if (*tok == KW_PC)
347                         {                                       // ([bd,PC,...
348                                 AnREG = (7 << 3) | 3;   // PC is special case - stuff 011 to register field and 111 to the mode field
349                                 tok++;
350                         }
351                         else if ((*tok >= KW_A0) && (*tok <= KW_A7))
352                         {                                       // ([bd,An,...
353                                 AnREG = (6<<3)|*tok & 7;
354                                 tok++;
355                         }
356                         else if ((*tok >= KW_D0) && (*tok <= KW_D7))
357                         {
358                                 // ([bd,Dn,...
359                                 AnREG = (6 << 3);
360                                 AnEXTEN |= ((*tok & 7) << 12);
361                                 AnEXTEN |= EXT_D;
362                                 AnEXTEN |= EXT_BS; // Oh look, a data register! Which means that base register is suppressed
363                                 tok++;
364
365                                 // Check for size
366                                 {
367                                         // ([bd,An/PC],Xn.W/L...)
368                                         switch ((int)*tok)
369                                         {
370                                         // Index reg size: <empty> | .W | .L
371                                         case DOTW:
372                                                 tok++;
373                                                 break;
374                                         default:
375                                                 break;
376                                         case DOTL:
377                                                 AnEXTEN |= EXT_L;
378                                                 tok++;
379                                                 break;
380                                         case DOTB:
381                                                 // .B not allowed here...
382                                                 goto badmode;
383                                         }
384                                 }
385
386                                 // Check for scale
387                                 if (*tok == '*')                        // ([bd,An/PC],Xn*...)
388                                 {                           // scale: *1, *2, *4, *8
389                                         tok++;
390
391                                         if (*tok == SYMBOL)
392                                         {
393                                                 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
394                                                         return error("scale factor expression must evaluate");
395                                                 switch (AnEXVAL)
396                                                 {
397                                                 case 1:
398                                                         break;
399                                                 case 2:
400                                                         AnIXSIZ |= TIMES2;
401                                                         break;
402                                                 case 4:
403                                                         AnIXSIZ |= TIMES4;
404                                                         break;
405                                                 case 8:
406                                                         AnIXSIZ |= TIMES8;
407                                                         break;
408                                                 default:
409                                                         goto badmode;
410                                 }
411                                         }
412                                         else if (*tok++ != CONST || *tok > 8)
413                                                 goto badmode;
414                                         else
415                                         {
416                                                 switch ((int)*tok++)
417                                                 {
418                                                 case 1:
419                                                         break;
420                                                 case 2:
421                                                         AnIXSIZ |= TIMES2;
422                                                         break;
423                                                 case 4:
424                                                         AnIXSIZ |= TIMES4;
425                                                         break;
426                                                 case 8:
427                                                         AnIXSIZ |= TIMES8;
428                                                         break;
429                                                 default:
430                                                         goto badmode;
431                                                 }
432                                         }
433                                 }
434                                 if (*tok == ']')  // ([bd,Dn]...
435                                 {
436                                         tok++;
437                                         goto IS_SUPPRESSEDn;
438                                 }
439                         }
440                         else if (*tok == ']')
441                         {
442                                 // PC and Xn is suppressed
443                                 AnREG = 6 << 3;         // stuff 110 to mode field
444                                 //AnEXTEN|=EXT_BS|EXT_IS;
445                                 AnEXTEN |= EXT_BS;
446                         }
447                         else
448                         {
449                                 goto badmode;
450                         }
451
452                         // At a crossroads here. We can accept either ([bd,An/PC],... or ([bd,An/PC,Xn*scale],...
453                         if (*tok == ']')
454                         {
455                                 //([bd,An/PC],Xn,od)
456                                 // Check for Xn
457                                 tok++;
458
459                                 if (*tok == ')')
460                                 {
461                                         //Xn and od are non existent, get out of jail free card
462                                         tok++;
463                                         AMn = MEMPRE;                   // ([bc,An,Xn],od) with no Xn and od
464                                         AnEXTEN |= EXT_IS | EXT_IISPREN;        //Suppress Xn and od
465                                         goto AnOK;
466                                 }
467                                 else if (*tok != ',')
468                                         return error("comma expected after ]");
469                                 else
470                                         tok++;                          // eat the comma
471
472                                 if ((*tok >= KW_A0) && (*tok <= KW_A7))
473                                 {
474                                         AnIXREG = ((*tok & 7) << 12);
475                                         AnEXTEN |= EXT_A;
476                                         tok++;
477                                 }
478                                 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
479                                 {
480                                         AnEXTEN |= ((*tok & 7) << 12);
481                                         AnEXTEN |= EXT_D;
482                                         tok++;
483                                 }
484                                 else
485                                 {
486                                         //No index found, suppress it
487                                         AnEXTEN |= EXT_IS;
488                                         tok--;                                  // Rewind tok to point to the comma
489                                         goto IS_SUPPRESSEDn;    // https://xkcd.com/292/ - what does he know anyway?
490                                 }
491
492                                 // Check for size
493                                 {
494                                         // ([bd,An/PC],Xn.W/L...)
495                                         switch ((int)*tok)
496                                         {
497                                         // Index reg size: <empty> | .W | .L
498                                         case DOTW:
499                                                 tok++;
500                                                 break;
501                                         default:
502                                                 break;
503                                         case DOTL:
504                                                 AnEXTEN |= EXT_L;
505                                                 tok++;
506                                                 break;
507                                         case DOTB:
508                                                 // .B not allowed here...
509                                                 goto badmode;
510                                         }
511                                 }
512
513                                 // Check for scale
514                                 if (*tok == '*')                   // ([bd,An/PC],Xn*...)
515                                 {                                  // scale: *1, *2, *4, *8
516                                         tok++;
517
518                                         if (*tok == SYMBOL)
519                                         {
520                                                 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
521                                                         return error("scale factor expression must evaluate");
522
523                                                 switch (AnEXVAL)
524                                                 {
525                                                 case 1:
526                                                         break;
527                                                 case 2:
528                                                         AnIXSIZ |= TIMES2;
529                                                         break;
530                                                 case 4:
531                                                         AnIXSIZ |= TIMES4;
532                                                         break;
533                                                 case 8:
534                                                         AnIXSIZ |= TIMES8;
535                                                         break;
536                                                 default:
537                                                         goto badmode;
538                                                 }
539                                         }
540                                         else if (*tok++ != CONST || *tok > 8)
541                                                 goto badmode;
542                                         else
543                                         {
544                                                 switch ((int)*tok++)
545                                                 {
546                                                 case 1:
547                                                         break;
548                                                 case 2:
549                                                         AnIXSIZ |= TIMES2;
550                                                         break;
551                                                 case 4:
552                                                         AnIXSIZ |= TIMES4;
553                                                         break;
554                                                 case 8:
555                                                         AnIXSIZ |= TIMES8;
556                                                         break;
557                                                 default:
558                                                         goto badmode;
559                                                 }
560                                         }
561                                 }
562
563                                 // Check for od
564                                 if (*tok == ')')        // ([bd,An/PC],Xn)
565                                 {
566                                         //od is non existant, get out of jail free card
567                                         AMn = MEMPOST;          // let's say it's ([bd,An],Xn,od) with od=0 then
568                                         AnEXTEN |= EXT_IISPOSN; // No outer displacement
569                                         tok++;
570                                         goto AnOK;
571                                 }
572                                 else if (*tok!=',')
573                                         return error("comma expected");
574                                 else
575                                         tok++;  // eat the comma
576
577                                 CHECKODn:
578                                 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
579                                         goto badmode;
580
581                                 if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0))
582                                 {
583                                         // od=0 so optimise it out
584                                         AMn = MEMPOST;           // let's say it's ([bd,An],Xn,od) with od=0 then
585                                         AnEXTEN |= EXT_IISPOSN; // No outer displacement
586                                         tok++;
587                                         goto AnOK;
588                                 }
589                                 // ([bd,An/PC],Xn,od)
590                                 if (*tok == DOTL)
591                                 {
592                                         // expr.L
593                                         AnEXTEN |= EXT_IISPOSL; // Long outer displacement
594                                         AMn = MEMPOST;
595                                         tok++;
596
597                                         // Defined, absolute values from $FFFF8000..$00007FFF get
598                                         // optimized to absolute short
599                                         if (CHECK_OPTS(OPT_ABS_SHORT)
600                                                 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
601                                                 && ((AnEXVAL + 0x8000) < 0x10000))
602                                         {
603                                                 AnEXTEN |= EXT_IISPOSW; // Word outer displacement
604                                                 AMn = MEMPOST;
605                                                 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
606                                         }
607
608                                 }
609                                 else
610                                 {
611                                         // expr[.W]
612                                         AnEXTEN |= EXT_IISPOSW; // Word outer displacement
613                                         AMn = MEMPOST;
614
615                                         // Is .W forced here?
616                                         if (*tok == DOTW)
617                                         {
618                                                 tok++;
619                                         }
620                                 }
621
622                                 // Check for final closing parenthesis
623                                 if (*tok == ')')
624                                 {
625                                         tok++;
626                                         goto AnOK;
627                                 }
628                                 else
629                                         return error("Closing parenthesis missing on addressing mode");
630
631                                 IS_SUPPRESSEDn:
632
633                                 // Check for od
634                                 if (*tok == ')')        // ([bd,An/PC],Xn)
635                                 {
636                                         //od is non existant, get out of jail free card
637                                         AMn = MEMPOST;          // let's say it's ([bd,An],Xn,od) with od=0 then
638                                         AnEXTEN |= EXT_IISNOIN; // No outer displacement
639                                         tok++;
640                                         goto AnOK;
641                                 }
642                                 else if (*tok!=',')
643                                         return error("comma expected");
644                                 else
645                                         tok++;  // eat the comma
646
647                 if ((*tok != CONST) && (*tok != SYMBOL))
648                                         goto badmode;
649
650                                 expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM);
651
652                                 if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0))
653                                 {
654                                         // od=0 so optimise it out
655                                         AMn = MEMPOST;           // let's say it's ([bd,An],Xn,od) with od=0 then
656                                         AnEXTEN |= EXT_IISNOIN; // No outer displacement
657                                         tok++;
658                                         goto AnOK;
659                                 }
660
661                                 // ([bd,An/PC],Xn,od)
662                                 if (*tok == DOTL)
663                                 {
664                                         // expr.L
665                                         tok++;
666                                         AMn = MEMPOST;
667                                         AnEXTEN |= EXT_IISNOIL; // Long outer displacement with IS suppressed
668                                 }
669                                 else
670                                 {
671                                         // expr[.W][]
672                                         AnEXTEN |= EXT_IISNOIW; // Word outer displacement with IS suppressed
673                                         AMn = MEMPRE;
674                                         if (*tok == DOTW)
675                                         {
676                                                 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement
677                                                 AMn = MEMPOST;
678                                                 tok++;
679                                         }
680
681                                         // Defined, absolute values from $FFFF8000..$00007FFF get
682                                         // optimized to absolute short
683                                         else if (CHECK_OPTS(OPT_BASE_DISP)
684                                                 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
685                                                 && ((AnEXVAL + 0x8000) < 0x10000))
686                                         {
687                                                 //AnEXTEN|=EXT_IISNOIW; // Word outer displacement with IS suppressed
688                                                 warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
689                                         }
690                                 }
691
692                                 // Check for final closing parenthesis
693                                 if (*tok == ')')
694                                 {
695                                         tok++;
696                                         goto AnOK;
697                                 }
698                                 else
699                                         return error("Closing parenthesis missing on addressing mode");
700                         }
701                         else if (*tok == ',')
702                         {
703                                 *tok++;                 // ([bd,An,Xn.size*scale],od)
704
705                                 //Check for Xn
706                                 if ((*tok >= KW_A0) && (*tok <= KW_A7))
707                                 {
708                                         AnEXTEN |= ((*tok & 7) << 12);
709                                         AnEXTEN |= EXT_A;
710                                         tok++;
711                                 }
712                                 else if ((*tok >= KW_D0) && (*tok <= KW_D7))
713                                 {
714                                         AnEXTEN |= ((*tok & 7) << 12);
715                                         AnEXTEN |= EXT_D;
716                                         tok++;
717                                 }
718
719                                 // Check for size
720                                 {
721                                         // ([bd,An/PC],Xn.W/L...)
722                                         switch ((int)*tok)
723                                         {
724                                         // Index reg size: <empty> | .W | .L
725                                         case DOTW:
726                                                 tok++;
727                                                 break;
728                                         default:
729                                                 break;
730                                         case DOTL:
731                                                 tok++;
732                                                 AnEXTEN |= EXT_L;
733                                                 break;
734                                         case DOTB:
735                                                 // .B not allowed here...
736                                                 goto badmode;
737                                         }
738                                 }
739
740                                 // Check for scale
741                                 if (*tok == '*')                        // ([bd,An/PC],Xn*...)
742                                 {                           // scale: *1, *2, *4, *8
743                                         tok++;
744
745                                         if (*tok == SYMBOL)
746                                         {
747                                                 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
748                                                         return error("scale factor expression must evaluate");
749                                                 switch (AnEXVAL)
750                                                 {
751                                                 case 1:
752                                                         break;
753                                                 case 2:
754                                                         AnIXSIZ |= TIMES2;
755                                                         break;
756                                                 case 4:
757                                                         AnIXSIZ |= TIMES4;
758                                                         break;
759                                                 case 8:
760                                                         AnIXSIZ |= TIMES8;
761                                                         break;
762                                                 default:
763                                                         goto badmode;
764                                                 }
765                                         }
766                                         else if (*tok++ != CONST || *tok > 8)
767                                                 goto badmode;
768                                         else
769                                         {
770                                                 switch ((int)*tok++)
771                                                 {
772                                                 case 1:
773                                                         break;
774                                                 case 2:
775                                                         AnIXSIZ |= TIMES2;
776                                                         break;
777                                                 case 4:
778                                                         AnIXSIZ |= TIMES4;
779                                                         break;
780                                                 case 8:
781                                                         AnIXSIZ |= TIMES8;
782                                                         break;
783                                                 default:
784                                                         goto badmode;
785                                                 }
786                                         }
787                                 }
788
789                                 //Check for ]
790                                 if (*tok != ']')
791                                         return error("Expected closing bracket ]");
792                                 tok++;                  // Eat the bracket
793
794                                 //Check for od
795                                 if (*tok == ')')        // ([bd,An/PC,Xn]...
796                                 {
797                                         //od is non existant, get out of jail free card
798                                         //AnEXVAL=0;            // zero outer displacement
799                                         AMn = MEMPRE;                   // let's say it's ([bd,An,Xn],od) with od suppressed then
800                                         AnEXTEN |= EXT_IISPREN; // No outer displacement
801                                         tok++;
802                                         goto AnOK;
803                                 }
804                 else if (*tok++ != ',')
805                                         return error("comma expected after ]");
806
807                                 if (*tok == SYMBOL || *tok == CONST)
808                                 {
809                                         if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
810                                                 goto badmode;
811
812                                         if (CHECK_OPTS(OPT_BASE_DISP) && (AnEXVAL == 0) && (AnEXATTR & DEFINED))
813                                         {
814                                                 // od=0 so optimise it out
815                                                 AMn = MEMPRE;            // let's say it's ([bd,An],Xn,od) with od=0 then
816                                                 AnEXTEN |= EXT_IISPRE0; // No outer displacement
817                                                 tok++;
818                                                 goto AnOK;
819                                         }
820                                 }
821
822                                 // ([bd,An/PC,Xn],od)
823                                 if (*tok == DOTL)
824                                 {
825                                         // expr.L
826                                         AMn = MEMPRE;
827                                         tok++;
828                                         AnEXTEN |= EXT_IISPREL;
829                                 }
830                                 else
831                                 {
832                                         // expr.[W]
833                                         AMn = MEMPRE;
834                                         int expr_size = EXT_IISPREW; // Assume we have a .w value
835
836                                         if ((AnEXVAL + 0x8000) > 0x10000)
837                                         {
838                                                 // Long value, so mark it as such for now
839                                                 expr_size = EXT_IISPREL;
840
841                                                 // Defined, absolute values from $FFFF8000..$00007FFF
842                                                 // get optimized to absolute short
843                         if (CHECK_OPTS(OPT_BASE_DISP)
844                                                         && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
845                                                         && ((AnEXVAL + 0x8000) < 0x10000))
846                                                 {
847                                                         expr_size = EXT_IISPREW;
848                                                         warn("outer displacement absolute value from $FFFF8000..$00007FFF optimised to absolute short");
849                                                 }
850                                         }
851
852                                         AnEXTEN |= expr_size; // Assume we have a .w value
853
854                                         // Is .W forced here?
855                                         if (*tok == DOTW)
856                                         {
857                                                 tok++;
858
859                                                 if (expr_size == EXT_IISPREL)
860                                                         return error("outer displacement value does not fit in .w size");
861                                         }
862                                 }
863
864                                 // Check for final closing parenthesis
865                                 if (*tok == ')')
866                                 {
867                                         tok++;
868                                         goto AnOK;
869                                 }
870                                 else
871                                         return error("Closing parenthesis missing on addressing mode");
872                         }
873                         else
874                                 goto badmode;
875                 }
876                 else
877                 {
878                         // (expr...
879                         if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
880                                 return ERROR;
881
882                         // It could be that this is really just an expression prefixing a
883                         // register as a displacement...
884                         if (*tok == ')')
885                         {
886                                 tok++;
887                                 goto CHK_FOR_DISPn;
888                         }
889
890                         // Otherwise, check for PC & etc displacements...
891                         if (*tok++ != ',')
892                                 goto badmode;
893
894                         if ((*tok >= KW_A0) && (*tok <= KW_A7))
895                         {
896                                 AnREG = *tok & 7;
897                                 tok++;
898
899                                 if (*tok == ',')
900                                 {
901                                         AMn = AINDEXED;
902                                         goto AMn_IXN;
903                                 }
904                                 else if (*tok == ')')
905                                 {
906                                         AMn = ADISP;
907                                         tok++;
908                                         goto AnOK;
909                                 }
910                                 else
911                                         goto badmode;
912                         }
913                         else if (*tok == KW_PC)
914                         {
915                                 if (*++tok == ',')
916                                 {                             // expr(PC,Xn...)
917                                         AMn = PCINDEXED;
918                                         goto AMn_IXN;
919                                 }
920                                 else if (*tok == ')')
921                                 {
922                                         AMn = PCDISP;             // expr(PC)
923                                         tok++;
924                                         goto AnOK;
925                                 }
926                                 else
927                                         goto badmode;
928                         }
929                         else
930                                 goto badmode;
931                 }
932         }
933         else if (*tok == '-' && tok[1] == '(' && ((tok[2] >= KW_A0) && (tok[2] <= KW_A7)) && tok[3] == ')')
934         {
935                 AMn = APREDEC;
936                 AnREG = tok[2] & 7;
937                 tok += 4;
938         }
939         else if (*tok == KW_CCR)
940         {
941                 AMn = AM_CCR;
942                 tok++;
943                 goto AnOK;
944         }
945         else if (*tok == KW_SR)
946         {
947                 AMn = AM_SR;
948                 tok++;
949                 goto AnOK;
950         }
951         else if (*tok == KW_USP)
952         {
953                 AMn = AM_USP;
954                 tok++;
955                 AnREG = 2;      //Added this for the case of USP used in movec (see CREGlut in mach.c). Hopefully nothing gets broken!
956                 goto AnOK;
957         }
958         else if ((*tok >= KW_IC40) && (*tok <= KW_BC40))
959         {
960                 AMn = CACHES;
961                 AnREG = *tok++ - KW_IC40;
962
963                 // After a cache keyword only a comma or EOL is allowed
964                 if ((*tok != ',') && (*tok != EOL))
965                         return ERROR;
966                 goto AnOK;
967         }
968         else if ((*tok >= KW_SFC) && (*tok <= KW_CRP))
969         {
970                 AMn = CREG;
971                 AnREG = (*tok++) - KW_SFC;
972                 goto AnOK;
973         }
974         else if ((*tok >= KW_FP0) && (*tok <= KW_FP7))
975         {
976                 AMn = FREG;
977                 AnREG = (*tok++ & 7);
978         }
979         else if ((*tok >= KW_FPIAR) && (*tok <= KW_FPCR))
980         {
981                 AMn = FPSCR;
982                 AnREG = (1 << ((*tok++) - KW_FPIAR + 10));
983         }
984         // expr
985         // expr.w
986         // expr.l
987         // d16(An)
988         // d8(An,Xn[.siz])
989         // d16(PC)
990         // d8(PC,Xn[.siz])
991         else
992         {
993                 if (expr(AnEXPR, &AnEXVAL, &AnEXATTR, &AnESYM) != OK)
994                         return ERROR;
995
996 CHK_FOR_DISPn:
997                 if (*tok == DOTW)
998                 {
999                         // expr.W
1000                         tok++;
1001                         AMn = ABSW;
1002
1003                         if (((AnEXATTR & (TDB | DEFINED)) == DEFINED) && (AnEXVAL < 0x10000))
1004                                 AnEXVAL = (int32_t)(int16_t)AnEXVAL;  // Sign extend value
1005
1006                         goto AnOK;
1007                 }
1008                 else if (*tok != '(')
1009                 {
1010                         // expr[.L]
1011                         AMn = ABSL;
1012
1013                         // Defined, absolute values from $FFFF8000..$00007FFF get optimized
1014                         // to absolute short
1015                         if (CHECK_OPTS(OPT_ABS_SHORT)
1016                                 && ((AnEXATTR & (TDB | DEFINED)) == DEFINED)
1017                                 && ((AnEXVAL + 0x8000) < 0x10000))
1018                         {
1019                                 AMn = ABSW;
1020
1021                                 if (sbra_flag)
1022                                         warn("absolute value from $FFFF8000..$00007FFF optimised to absolute short");
1023                         }
1024
1025                         // Is .L forced here?
1026                         if (*tok == DOTL)
1027                         {
1028                                 tok++;
1029                                 AMn = ABSL;
1030                         }
1031
1032                         goto AnOK;
1033                 }
1034
1035                 tok++;
1036
1037                 if ((*tok >= KW_A0) && (*tok <= KW_A7))
1038                 {
1039                         AnREG = *tok++ & 7;
1040
1041                         if (*tok == ')')
1042                         {
1043                                 AMn = ADISP;
1044                                 tok++;
1045                                 goto AnOK;
1046                         }
1047
1048                         AMn = AINDEXED;
1049                         goto AMn_IXN;
1050                 }
1051                 else if (*tok == KW_PC)
1052                 {
1053                         if (*++tok == ')')
1054                         {
1055                                 AMn = PCDISP;
1056                                 tok++;
1057                                 goto AnOK;
1058                         }
1059
1060                         AMn = PCINDEXED;
1061                         goto AMn_IXN;
1062                 }
1063
1064                 goto badmode;
1065         }
1066
1067         // Addressing mode OK
1068 AnOK:
1069         ;
1070 }
1071
1072 // Clean up dirty little macros
1073 #undef AnOK
1074 #undef AMn
1075 #undef AnREG
1076 #undef AnIXREG
1077 #undef AnIXSIZ
1078 #undef AnEXPR
1079 #undef AnEXVAL
1080 #undef AnEXATTR
1081 #undef AnOEXPR
1082 #undef AnOEXVAL
1083 #undef AnOEXATTR
1084 #undef AnESYM
1085 #undef AMn_IX0
1086 #undef AMn_IXN
1087 #undef CHK_FOR_DISPn
1088 #undef AnBEXPR
1089 #undef AnBEXVAL
1090 #undef AnBEXATTR
1091 #undef AnBZISE
1092 #undef AnEXTEN
1093 #undef AMn_030
1094 #undef IS_SUPPRESSEDn
1095 #undef CHECKODn