]> Shamusworld >> Repos - rmac/blob - procln.c
Remove all remaining traces of "as68_flag" from the codebase (issue #186)
[rmac] / procln.c
1 //
2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // PROCLN.C - Line Processing
4 // Copyright (C) 199x Landon Dyer, 2011-2021 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 #include "procln.h"
10 #include "6502.h"
11 #include "amode.h"
12 #include "direct.h"
13 #include "dsp56k_amode.h"
14 #include "dsp56k_mach.h"
15 #include "error.h"
16 #include "expr.h"
17 #include "listing.h"
18 #include "mach.h"
19 #include "macro.h"
20 #include "op.h"
21 #include "riscasm.h"
22 #include "sect.h"
23 #include "symbol.h"
24
25 #define DEF_KW                                  // Declare keyword values
26 #include "kwtab.h"                              // Incl generated keyword tables & defs
27
28 #define DEF_MN                                  // Incl 68k keyword definitions
29 #define DECL_MN                                 // Incl 68k keyword state machine tables
30 #include "mntab.h"
31
32 #define DEF_MR
33 #define DECL_MR
34 #include "risckw.h"
35
36 #define DEF_MP                                  // Include 6502 keyword definitions
37 #define DECL_MP                                 // Include 6502 keyword state machine tables
38 #include "6502kw.h"
39
40 #define DEF_MO                                  // Include OP keyword definitions
41 #define DECL_MO                                 // Include OP keyword state machine tables
42 #include "opkw.h"
43
44 #define DEF_DSP                                 // Include DSP56K keywords definitions
45 #define DECL_DSP                                // Include DSP56K keyword state machine tables
46 #include "dsp56kkw.h"
47
48
49 IFENT * ifent;                                  // Current ifent
50 static IFENT ifent0;                    // Root ifent
51 IFENT * f_ifent;                                // Freelist of ifents
52 int disabled;                                   // Assembly conditionally disabled
53 int just_bss;                                   // 1, ds.b in microprocessor mode
54 uint32_t pcloc;                                 // Value of "PC" at beginning of line
55 SYM * lab_sym;                                  // Label on line (or NULL)
56 char * label_defined;                   // The name of the last label defined in current line (if any)
57
58 const char extra_stuff[] = "extra (unexpected) text found after addressing mode";
59 const char comma_error[] = "missing comma";
60 const char syntax_error[] = "syntax error";
61 const char locgl_error[] = "cannot GLOBL local symbol";
62 const char lab_ignored[] = "label ignored";
63
64 // Table to convert an addressing-mode number to a bitmask.
65 LONG amsktab[0124] = {
66         M_DREG, M_DREG, M_DREG, M_DREG,
67         M_DREG, M_DREG, M_DREG, M_DREG,
68
69         M_AREG, M_AREG, M_AREG, M_AREG,
70         M_AREG, M_AREG, M_AREG, M_AREG,
71
72         M_AIND, M_AIND, M_AIND, M_AIND,
73         M_AIND, M_AIND, M_AIND, M_AIND,
74
75         M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
76         M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
77
78         M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
79         M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
80
81         M_ADISP, M_ADISP, M_ADISP, M_ADISP,
82         M_ADISP, M_ADISP, M_ADISP, M_ADISP,
83
84         M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
85         M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
86
87         M_ABSW,                 // 070
88         M_ABSL,                 // 071
89         M_PCDISP,               // 072
90         M_PCINDEXED,    // 073
91         M_IMMED,                // 074
92         0L,                             // 075
93         0L,                             // 076
94         0L,                             // 077
95         M_ABASE,                // 0100
96         M_MEMPOST,              // 0101
97         M_MEMPRE,               // 0102
98         M_PCBASE,               // 0103
99         M_PCMPOST,              // 0104
100         M_PCMPRE,               // 0105
101         M_AM_USP,               // 0106
102         M_AM_SR,                // 0107
103         M_AM_CCR,               // 0110
104         M_AM_NONE,              // 0111
105         0x30,                   // 0112
106         0x30,                   // 0113
107         0L,                             // 0114
108         0L,                             // 0115
109         0L,                             // 0116
110         0L,                             // 0117
111         M_CACHE40,              // 0120
112         M_CREG,                 // 0121
113         M_FREG,                 // 0122
114         M_FPSCR                 // 0123
115 };                                      // 0123 length
116
117
118 // Function prototypes
119 int HandleLabel(char *, int);
120
121
122 //
123 // Initialize line processor
124 //
125 void InitLineProcessor(void)
126 {
127         disabled = 0;
128         ifent = &ifent0;
129         f_ifent = ifent0.if_prev = NULL;
130         ifent0.if_state = 0;
131 }
132
133
134 //
135 // Line processor
136 //
137 void Assemble(void)
138 {
139         int state;                                      // Keyword machine state (output)
140         int j;                                          // Random int, must be fast
141         char * p;                                       // Random char ptr, must be fast
142         TOKEN * tk;                                     // First token in line
143         char * label;                           // Symbol (or NULL)
144         char * equate;                          // Symbol (or NULL)
145         int labtyp = 0;                         // Label type (':', DCOLON)
146         int equtyp = 0;                         // Equ type ('=', DEQUALS)
147         uint64_t eval;                          // Expression value
148         WORD eattr;                                     // Expression attributes
149         SYM * esym;                                     // External symbol involved in expr.
150         WORD siz = 0;                           // Size suffix to mnem/diretve/macro
151         LONG amsk0, amsk1;                      // Address-type masks for ea0, ea1
152         MNTAB * m;                                      // Code generation table pointer
153         SYM * sy, * sy2;                        // Symbol (temp usage)
154         char * opname = NULL;           // Name of dirctve/mnemonic/macro
155         int listflag;                           // 0: Don't call listeol()
156         WORD rmask;                                     // Register list, for REG
157         int registerbank;                       // RISC register bank
158         int riscreg;                            // RISC register
159         listflag = 0;                           // Initialise listing flag
160
161 loop:                                                   // Line processing loop label
162
163         // Get another line of tokens
164         if (TokenizeLine() == TKEOF)
165         {
166 DEBUG { printf("Assemble: Found TKEOF flag...\n"); }
167                 if (list_flag && listflag)                      // Flush last line of source
168                         listeol();
169
170                 if (ifent->if_prev != NULL)                     // Check conditional token
171                         error("hit EOF without finding matching .endif");
172
173                 return;
174         }
175
176         DEBUG { DumpTokenBuffer(); }
177
178         if (list_flag)
179         {
180                 if (listflag && listing > 0)
181                         listeol();                                              // Tell listing generator about EOL
182
183                 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
184                 listflag = 1;                                           // OK to call `listeol' now
185                 just_bss = 0;                                           // Reset just_bss mode
186         }
187
188         state = -3;                                                             // No keyword (just EOL)
189         label = NULL;                                                   // No label
190         label_defined = NULL;                                   // No label defined yet
191         lab_sym = NULL;                                                 // No (exported) label
192         equate = NULL;                                                  // No equate
193         tk = tok;                                                               // Save first token in line
194         pcloc = (uint32_t)sloc;                                 // Set beginning-of-line PC
195
196 loop1:                                                                          // Internal line processing loop
197
198         if (*tok == EOL)                                                // Restart loop if end-of-line
199                 goto loop;
200
201         // First token MUST be a symbol (Shamus: not sure why :-/)
202         if (*tok != SYMBOL)
203         {
204                 if ((*tok >= KW_D0) && (*tok <= KW_R31))
205                         error("cannot use reserved keyword as label name or .equ");
206                 else
207                         error("syntax error; expected symbol");
208
209                 goto loop;
210         }
211
212         j = (int)tok[2];                                                // Skip equates (normal statements)
213
214         if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
215         {
216                 equate = string[tok[1]];
217                 equtyp = j;
218                 tok += 3;
219                 goto normal;
220         }
221
222         // Skip past label (but record it)
223         if (j == ':' || j == DCOLON)
224         {
225                 label = string[tok[1]];                         // Get label name
226                 labtyp = tok[2];                                        // Get label type
227                 tok += 3;                                                       // Go to next line token
228         }
229
230         // EOL is legal here...
231         if (*tok == EOL)
232                 goto normal;
233
234         // First token MUST be a symbol (if we get here, tok didn't advance)
235         if (*tok++ != SYMBOL)
236         {
237                 error("syntax error; expected symbol");
238                 goto loop;
239         }
240
241         opname = p = string[*tok++];
242
243         // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
244         // On output, `state' will have one of the values:
245         //    -3          there was no symbol (EOL)
246         //    -2..-1      the symbol didn't match any keyword
247         //    0..499      vanilla directives (dc, ds, etc.)
248         //    500..999    electric directives (macro, rept, etc.)
249         //    1000..+     mnemonics (move, lsr, etc.)
250         for(state=0; state>=0;)
251         {
252                 j = mnbase[state] + (int)tolowertab[*p];
253
254                 // Reject, character doesn't match
255                 if (mncheck[j] != state)
256                 {
257                         state = -1;                                             // No match
258                         break;
259                 }
260
261                 // Must accept or reject at EOS
262                 if (!*++p)
263                 {
264                         state = mnaccept[j];                    // (-1 on no terminal match)
265                         break;
266                 }
267
268                 state = mntab[j];
269         }
270
271         // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
272         siz = SIZN;
273
274         switch (*tok)
275         {
276         case DOTW: siz = SIZW, tok++; break;
277         case DOTL: siz = SIZL, tok++; break;
278         case DOTB: siz = SIZB, tok++; break;
279         case DOTD: siz = SIZD, tok++; break;
280         case DOTP: siz = SIZP, tok++; break;
281         case DOTQ: siz = SIZQ, tok++; break;
282         case DOTS: siz = SIZS, tok++; break;
283         case DOTX: siz = SIZX, tok++; break;
284         }
285
286         // Do special directives (500..999) (These must be handled in "real time")
287         if (state >= 500 && state < 1000)
288         {
289                 switch (state)
290                 {
291                 case MN_IF:
292                         d_if();
293                         goto loop;
294
295                 case MN_ELSE:
296                         d_else();
297                         goto loop;
298
299                 case MN_ENDIF:
300                         d_endif();
301                         goto loop;
302
303                 case MN_IIF:                                            // .iif --- immediate if
304                         if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
305                                 goto loop;
306
307                         if (!(eattr & DEFINED))
308                         {
309                                 error(undef_error);
310                                 goto loop;
311                         }
312
313                         if (*tok++ != ',')
314                         {
315                                 error(comma_error);
316                                 goto loop;
317                         }
318
319                         if (eval == 0)
320                                 goto loop;
321
322                         goto loop1;
323
324                 case MN_MACRO:                                          // .macro --- macro definition
325                         if (!disabled)
326                         {
327                                 // Label on a macro definition is bad mojo... Warn the user
328                                 if (label != NULL)
329                                         warn(lab_ignored);
330
331                                 DefineMacro();
332                         }
333
334                         goto loop;
335
336                 case MN_EXITM:                                          // .exitm --- exit macro
337                 case MN_ENDM:                                           // .endm --- same as .exitm
338                         if (!disabled)
339                         {
340                                 if (label != NULL)
341                                         warn(lab_ignored);
342
343                                 ExitMacro();
344                         }
345
346                         goto loop;
347
348                 case MN_REPT:
349                         if (!disabled)
350                         {
351                                 // Handle labels on REPT directive lines...
352                                 if (label)
353                                 {
354                                         if (HandleLabel(label, labtyp) != 0)
355                                                 goto loop;
356
357                                         label_defined = label;
358                                 }
359
360                                 HandleRept();
361                         }
362
363                         goto loop;
364
365                 case MN_ENDR:
366                         if (!disabled)
367                                 error("mis-nested .endr");
368
369                         goto loop;
370                 }
371         }
372
373 normal:
374         if (disabled)                                                   // Conditionally disabled code
375                 goto loop;
376
377         // Do equates
378         if (equate != NULL)
379         {
380                 // Pick global or local symbol enviroment
381                 j = (*equate == '.' ? curenv : 0);
382                 sy = lookup(equate, LABEL, j);
383
384                 if (sy == NULL)
385                 {
386                         sy = NewSymbol(equate, LABEL, j);
387                         sy->sattr = 0;
388
389                         if (equtyp == DEQUALS)
390                         {
391                                 // Can't GLOBAL a local symbol
392                                 if (j)
393                                 {
394                                         error(locgl_error);
395                                         goto loop;
396                                 }
397
398                                 sy->sattr = GLOBAL;
399                         }
400                 }
401                 else if ((sy->sattr & DEFINED) && equtyp != SET)
402                 {
403                         if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
404                         {
405                                 sy->sattre &= ~UNDEF_EQUR;
406                                 sy->svalue = 0;
407                         }
408                         else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
409                         {
410                                 sy->sattre &= ~UNDEF_CC;
411                                 sy->svalue = 0;
412                         }
413                         else
414                         {
415                                 error("multiple equate to '%s'", sy->sname);
416                                 goto loop;
417                         }
418                 }
419
420                 // Put symbol in "order of definition" list if it's not already there
421                 AddToSymbolDeclarationList(sy);
422
423                 // Parse value to equate symbol to;
424                 // o  .equr
425                 // o  .reg
426                 // o  everything else
427                 if (equtyp == EQUREG)
428                 {
429 //Linko's request to issue a warning on labels that equated to the same
430 //register would go here. Not sure how to implement it though. :-/
431 /*
432 Maybe like this way:
433 have an array of bools with 64 entries. Whenever a register is equated, set the
434 corresponding register bool to true. Whenever it's undef'ed, set it to false.
435 When checking to see if it's already been equated, issue a warning.
436 */
437                         // Check that we are in a RISC section
438                         if (!rgpu && !rdsp)
439                         {
440                                 error(".equr/.regequ must be defined in .gpu/.dsp section");
441                                 goto loop;
442                         }
443
444                         // Check for register to equate to
445                         if ((*tok >= KW_R0) && (*tok <= KW_R31))
446                         {
447 //                              sy->sattre  = EQUATEDREG | RISCSYM;     // Mark as equated register
448                                 sy->sattre  = EQUATEDREG;       // Mark as equated register
449                                 riscreg = (*tok - KW_R0);
450 //is there any reason to do this, since we're putting this in svalue?
451 //i'm thinking, no. Let's test that out! :-D
452 //                              sy->sattre |= (riscreg << 8);           // Store register number
453 //everything seems to build fine without it... We'll leave it here Just In Case(tm)
454
455 #define DEBODGE_REGBANK
456 #ifdef DEBODGE_REGBANK
457                                 // Default is current state of "regbank"
458                                 registerbank = regbank;
459 #else
460                                 // Default is no register bank specified
461                                 registerbank = BANK_N;
462 #endif
463
464                                 // Check for ",<bank #>" override notation
465                                 if ((tok[1] == ',') && (tok[2] == CONST))
466                                 {
467                                         // Advance token pointer to the constant
468                                         tok += 3;
469
470                                         // Anything other than a 0 or a 1 will result in "No Bank"
471                                         if (*(uint64_t *)tok == 0)
472                                                 registerbank = BANK_0;
473                                         else if (*(uint64_t *)tok == 1)
474                                                 registerbank = BANK_1;
475
476                                         // Advance half-way through the 64-bit const.
477                                         // The code below, expecting a regular token,
478                                         // will advance past the second half.
479                                         tok++;
480                                 }
481
482 #ifdef DEBODGE_REGBANK
483                                 sy->sattre |= registerbank;     // Store register bank
484 #else
485 // What needs to happen here is to prime registerbank with regbank, then use
486 // registerbank down below for the bank marking.
487 #warning "!!! regbank <-> registerbank confusion here !!!"
488 // The question here is why, if we're allowed to override the ".regbankN" rules
489 // above, then why is it using the one set by the directive in the extended
490 // attributes and not in what ends up in symbol->svalue?
491 // ".regbankN" is not an original Madmac directive, so it's suspect
492                                 sy->sattre |= regbank;          // Store register bank
493 #endif
494                                 eattr = ABS | DEFINED | GLOBAL;
495                                 eval = riscreg;
496                                 tok++;
497                         }
498                         // Checking for a register symbol
499                         else if (tok[0] == SYMBOL)
500                         {
501                                 sy2 = lookup(string[tok[1]], LABEL, j);
502
503                                 // Make sure symbol is a valid equreg
504                                 if (!sy2 || !(sy2->sattre & EQUATEDREG))
505                                 {
506                                         error("invalid GPU/DSP .equr/.regequ definition");
507                                         goto loop;
508                                 }
509                                 else
510                                 {
511                                         eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
512                                         sy->sattre = sy2->sattre;
513                                         eval = (sy2->svalue & 0xFFFFF0FF);
514                                         tok += 2;
515                                 }
516                         }
517                         else
518                         {
519                                 error("invalid GPU/DSP .equr/.regequ definition");
520                                 goto loop;
521                         }
522                 }
523                 else if (equtyp == REG)
524                 {
525                         if (reglist(&rmask) < 0)
526                                 goto loop;
527
528                         eval = (uint32_t)rmask;
529                         eattr = ABS | DEFINED;
530                 }
531                 else if (equtyp == CCDEF)
532                 {
533                         sy->sattre |= EQUATEDCC;
534                         eattr = ABS | DEFINED | GLOBAL;
535
536                         if (tok[0] == SYMBOL)
537                         {
538                                 sy2 = lookup(string[tok[1]], LABEL, j);
539
540                                 if (!sy2 || !(sy2->sattre & EQUATEDCC))
541                                 {
542                                         error("invalid gpu/dsp .ccdef definition");
543                                         goto loop;
544                                 }
545                                 else
546                                 {
547                                         eattr = ABS | DEFINED | GLOBAL;
548                                         sy->sattre = sy2->sattre;
549                                         eval = sy2->svalue;
550                                         tok += 2;
551                                 }
552                         }
553                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
554                                 goto loop;
555                 }
556                 // equ an equr
557                 else if (*tok == SYMBOL)
558                 {
559                         sy2 = lookup(string[tok[1]], LABEL, j);
560
561                         if (sy2 && (sy2->sattre & EQUATEDREG))
562                         {
563                                 sy->stype = sy2->stype;
564                                 sy->sattr = sy2->sattr;
565                                 sy->sattre = sy2->sattre;
566                                 sy->svalue = sy2->svalue;
567                                 goto loop;
568                         }
569                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
570                                 goto loop;
571                 }
572                 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
573                         goto loop;
574
575                 if (!(eattr & DEFINED))
576                 {
577                         error(undef_error);
578                         goto loop;
579                 }
580
581                 sy->sattr |= eattr | EQUATED;   // Symbol inherits value and attributes
582                 sy->svalue = eval;
583
584                 if (list_flag)                                  // Put value in listing
585                         listvalue((uint32_t)eval);
586
587                 ErrorIfNotAtEOL();                              // Must be at EOL now
588                 goto loop;
589         }
590
591         // Do labels
592         if (label != NULL)
593         {
594                 // Non-zero == error occurred
595                 if (HandleLabel(label, labtyp) != 0)
596                         goto loop;
597
598                 label_defined = label;
599         }
600
601         // Punt on EOL
602         if (state == -3)
603                 goto loop;
604
605         // If we're in 6502 mode and are still in need of a mnemonic, then search
606         // for valid 6502 mnemonic.
607         if (m6502 && (state < 0 || state >= 1000))
608         {
609 #ifdef ST
610                 state = kmatch(opname, mpbase, mpcheck, mptab, mpaccept);
611 #else
612                 for(state=0, p=opname; state>= 0; )
613                 {
614                         j = mpbase[state] + tolowertab[*p];
615
616                         if (mpcheck[j] != state)        // Reject, character doesn't match
617                         {
618                                 state = -1;             // No match
619                                 break;
620                         }
621
622                         if (!*++p)
623                         {                       // Must accept or reject at EOS
624                                 state = mpaccept[j];    // (-1 on no terminal match)
625                                 break;
626                         }
627
628                         state = mptab[j];
629                 }
630 #endif
631
632                 // Call 6502 code generator if we found a mnemonic
633                 if (state >= 2000)
634                 {
635                         m6502cg(state - 2000);
636                         goto loop;
637                 }
638         }
639
640         // If we are in GPU or DSP mode and still in need of a mnemonic then search
641         // for one
642         if ((rgpu || rdsp) && (state < 0 || state >= 1000))
643         {
644                 for(state=0, p=opname; state>=0;)
645                 {
646                         j = mrbase[state] + (int)tolowertab[*p];
647
648                         // Reject, character doesn't match
649                         if (mrcheck[j] != state)
650                         {
651                                 state = -1;                                     // No match
652                                 break;
653                         }
654
655                         // Must accept or reject at EOS
656                         if (!*++p)
657                         {
658                                 state = mraccept[j];            // (-1 on no terminal match)
659                                 break;
660                         }
661
662                         state = mrtab[j];
663                 }
664
665                 // Call RISC code generator if we found a mnemonic
666                 if (state >= 3000)
667                 {
668                         GenerateRISCCode(state);
669                         goto loop;
670                 }
671         }
672
673         // If we are in OP mode and still in need of a mnemonic then search for one
674         if (robjproc && ((state < 0) || (state >= 1000)))
675         {
676                 for(state=0, p=opname; state>=0;)
677                 {
678                         j = mobase[state] + (int)tolowertab[*p];
679
680                         // Reject, character doesn't match
681                         if (mocheck[j] != state)
682                         {
683                                 state = -1;                                     // No match
684                                 break;
685                         }
686
687                         // Must accept or reject at EOS
688                         if (!*++p)
689                         {
690                                 state = moaccept[j];            // (-1 on no terminal match)
691                                 break;
692                         }
693
694                         state = motab[j];
695                 }
696
697                 // Call OP code generator if we found a mnemonic
698                 if (state >= 3100)
699                 {
700                         GenerateOPCode(state);
701                         goto loop;
702                 }
703         }
704
705         // If we are in 56K mode and still in need of a mnemonic then search for one
706         if (dsp56001 && ((state < 0) || (state >= 1000)))
707         {
708                 for(state=0, p=opname; state>=0;)
709                 {
710                         j = dspbase[state] + (int)tolowertab[*p];
711
712                         // Reject, character doesn't match
713                         if (dspcheck[j] != state)
714                         {
715                                 state = -1;                                     // No match
716                                 break;
717                         }
718
719                         // Must accept or reject at EOS
720                         if (!*++p)
721                         {
722                                 state = dspaccept[j];           // (-1 on no terminal match)
723                                 break;
724                         }
725
726                         state = dsptab[j];
727                 }
728
729                 // Call DSP code generator if we found a mnemonic
730                 if (state >= 2000)
731                 {
732                         LONG parcode;
733                         int operands;
734                         MNTABDSP * md = &dsp56k_machtab[state - 2000];
735                         deposit_extra_ea = 0;   // Assume no extra word needed
736
737                         if (md->mnfunc == dsp_mult)
738                         {
739                                 // Special case for multiplication instructions: they require
740                                 // 3 operands
741                                 if ((operands = dsp_amode(3)) == ERROR)
742                                         goto loop;
743                         }
744                         else if ((md->mnattr & PARMOVE) && md->mn0 != M_AM_NONE)
745                         {
746                                 if (dsp_amode(2) == ERROR)
747                                         goto loop;
748                         }
749                         else if ((md->mnattr & PARMOVE) && md->mn0 == M_AM_NONE)
750                         {
751                                 // Instructions that have parallel moves but use no operands
752                                 // (probably only move). In this case, don't parse addressing
753                                 // modes--just go straight to parallel parse
754                                 dsp_am0 = dsp_am1 = M_AM_NONE;
755                         }
756                         else
757                         {
758                                 // Non parallel move instructions can have up to 4 parameters
759                                 // (well, only tcc instructions really)
760                                 if ((operands = dsp_amode(4)) == ERROR)
761                                         goto loop;
762
763                                 if (operands == 4)
764                                 {
765                                         dsp_tcc4(md->mninst);
766                                         goto loop;
767                                 }
768                         }
769
770                         if (md->mnattr & PARMOVE)
771                         {
772                                 // Check for parallel moves
773                                 if ((parcode = parmoves(dsp_a1reg)) == ERROR)
774                                         goto loop;
775                         }
776                         else
777                         {
778                                 if (*tok != EOL)
779                                         error("parallel moves not allowed with this instruction");
780
781                                 parcode = 0;
782                         }
783
784                         while ((dsp_am0 & md->mn0) == 0 || (dsp_am1 & md->mn1) == 0)
785                                 md = &dsp56k_machtab[md->mncont];
786
787                         (*md->mnfunc)(md->mninst | (parcode << 8));
788                         goto loop;
789                 }
790         }
791
792         // Invoke macro or complain about bad mnemonic
793         if (state < 0)
794         {
795                 if ((sy = lookup(opname, MACRO, 0)) != NULL)
796                         InvokeMacro(sy, siz);
797                 else
798                         error("unknown op '%s'", opname);
799
800                 goto loop;
801         }
802
803         // Call directive handlers
804         if (state < 500)
805         {
806                 (*dirtab[state])(siz);
807                 goto loop;
808         }
809
810         // Do mnemonics
811         //  o  can't deposit instrs in BSS or ABS
812         //  o  do automatic .EVEN for instrs
813         //  o  allocate space for largest possible instr
814         //  o  can't do ".b" operations with an address register
815         if (scattr & SBSS)
816         {
817                 error("cannot initialize non-storage (BSS) section");
818                 goto loop;
819         }
820
821         if (sloc & 1)                                   // Automatic .even
822                 auto_even();
823
824         if (challoc - ch_size < 18)             // Make sure have space in current chunk
825                 chcheck(0);
826
827         m = &machtab[state - 1000];
828
829         // Call special-mode handler
830         if (m->mnattr & CGSPECIAL)
831         {
832                 (*m->mnfunc)(m->mninst, siz);
833                 goto loop;
834         }
835
836         if (amode(1) < 0)                               // Parse 0, 1 or 2 addr modes
837                 goto loop;
838
839         // Check that we're at EOL
840         // The only exception is ptestr/ptestw instructions
841         // that have 3 or 4 operands and are not handled by
842         // amode(). (yes, we're taking a performance hit here sadly)
843         if (m->mnfunc != m_ptestr && m->mnfunc != m_ptestw)
844                 if (*tok != EOL)
845                         error(extra_stuff);
846
847         amsk0 = amsktab[am0];
848         amsk1 = amsktab[am1];
849
850         // Catch attempts to use ".B" with an address register (yes, this check
851         // does work at this level)
852         if (siz == SIZB && (am0 == AREG || am1 == AREG))
853         {
854                 error("cannot use '.b' with an address register");
855                 goto loop;
856         }
857
858         // Keep a backup of chptr (used for optimisations during codegen)
859         chptr_opcode = chptr;
860
861         while (!(m->mnattr & siz) || (amsk0 & m->mn0) == 0 || (amsk1 & m->mn1) == 0)
862                 m = &machtab[m->mncont];
863
864         DEBUG { printf("    68K: mninst=$%X, siz=$%X, mnattr=$%X, amsk0=$%X, mn0=$%X, amsk1=$%X, mn1=$%X\n", m->mninst, siz, m->mnattr, amsk0, m->mn0, amsk1, m->mn1); }
865
866         (*m->mnfunc)(m->mninst, siz);
867         goto loop;
868 }
869
870
871 //
872 // Handle the creation of labels
873 //
874 int HandleLabel(char * label, int labelType)
875 {
876         // Check for dot in front of label; means this is a local label if present
877         int environment = (*label == '.' ? curenv : 0);
878         SYM * symbol = lookup(label, LABEL, environment);
879
880         if (symbol == NULL)
881         {
882                 symbol = NewSymbol(label, LABEL, environment);
883                 symbol->sattr = 0;
884                 symbol->sattre = 0;
885         }
886         else if (symbol->sattr & DEFINED)
887                 return error("multiply-defined label '%s'", label);
888
889         // Put symbol in "order of definition" list if it's not already in it
890         AddToSymbolDeclarationList(symbol);
891
892         if (orgactive)
893         {
894                 symbol->svalue = orgaddr;
895                 symbol->sattr |= ABS | DEFINED | EQUATED;
896         }
897         else
898         {
899                 symbol->svalue = sloc;
900                 symbol->sattr |= DEFINED | cursect;
901         }
902
903         lab_sym = symbol;
904
905         // Yes, our CS professors told us to write checks for equality this way,
906         // but damn, it hurts my brain every time I look at it. :-/
907         if (0 == environment)
908                 curenv++;
909
910         // Make label global if it has a double colon
911         if (labelType == DCOLON)
912         {
913                 if (environment != 0)
914                         return error(locgl_error);
915
916                 symbol->sattr |= GLOBAL;
917         }
918
919         return 0;
920 }
921