]> Shamusworld >> Repos - rmac/blob - procln.c
Removed some dead code, as well as all gpu/dsp regbank check code (not only it was...
[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 riscreg;                            // RISC register
158         listflag = 0;                           // Initialise listing flag
159
160 loop:                                                   // Line processing loop label
161
162         // Get another line of tokens
163         if (TokenizeLine() == TKEOF)
164         {
165 DEBUG { printf("Assemble: Found TKEOF flag...\n"); }
166                 if (list_flag && listflag)                      // Flush last line of source
167                         listeol();
168
169                 if (ifent->if_prev != NULL)                     // Check conditional token
170                         error("hit EOF without finding matching .endif");
171
172                 return;
173         }
174
175         DEBUG { DumpTokenBuffer(); }
176
177         if (list_flag)
178         {
179                 if (listflag && listing > 0)
180                         listeol();                                              // Tell listing generator about EOL
181
182                 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
183                 listflag = 1;                                           // OK to call `listeol' now
184                 just_bss = 0;                                           // Reset just_bss mode
185         }
186
187         state = -3;                                                             // No keyword (just EOL)
188         label = NULL;                                                   // No label
189         label_defined = NULL;                                   // No label defined yet
190         lab_sym = NULL;                                                 // No (exported) label
191         equate = NULL;                                                  // No equate
192         tk = tok;                                                               // Save first token in line
193         pcloc = (uint32_t)sloc;                                 // Set beginning-of-line PC
194
195 loop1:                                                                          // Internal line processing loop
196
197         if (*tok == EOL)                                                // Restart loop if end-of-line
198                 goto loop;
199
200         // First token MUST be a symbol (Shamus: not sure why :-/)
201         if (*tok != SYMBOL)
202         {
203                 if ((*tok >= KW_D0) && (*tok <= KW_R31))
204                         error("cannot use reserved keyword as label name or .equ");
205                 else
206                         error("syntax error; expected symbol");
207
208                 goto loop;
209         }
210
211         j = (int)tok[2];                                                // Skip equates (normal statements)
212
213         if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
214         {
215                 equate = string[tok[1]];
216                 equtyp = j;
217                 tok += 3;
218                 goto normal;
219         }
220
221         // Skip past label (but record it)
222         if (j == ':' || j == DCOLON)
223         {
224                 label = string[tok[1]];                         // Get label name
225                 labtyp = tok[2];                                        // Get label type
226                 tok += 3;                                                       // Go to next line token
227         }
228
229         // EOL is legal here...
230         if (*tok == EOL)
231                 goto normal;
232
233         // First token MUST be a symbol (if we get here, tok didn't advance)
234         if (*tok++ != SYMBOL)
235         {
236                 error("syntax error; expected symbol");
237                 goto loop;
238         }
239
240         opname = p = string[*tok++];
241
242         // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
243         // On output, `state' will have one of the values:
244         //    -3          there was no symbol (EOL)
245         //    -2..-1      the symbol didn't match any keyword
246         //    0..499      vanilla directives (dc, ds, etc.)
247         //    500..999    electric directives (macro, rept, etc.)
248         //    1000..+     mnemonics (move, lsr, etc.)
249         for(state=0; state>=0;)
250         {
251                 j = mnbase[state] + (int)tolowertab[*p];
252
253                 // Reject, character doesn't match
254                 if (mncheck[j] != state)
255                 {
256                         state = -1;                                             // No match
257                         break;
258                 }
259
260                 // Must accept or reject at EOS
261                 if (!*++p)
262                 {
263                         state = mnaccept[j];                    // (-1 on no terminal match)
264                         break;
265                 }
266
267                 state = mntab[j];
268         }
269
270         // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
271         siz = SIZN;
272
273         switch (*tok)
274         {
275         case DOTW: siz = SIZW, tok++; break;
276         case DOTL: siz = SIZL, tok++; break;
277         case DOTB: siz = SIZB, tok++; break;
278         case DOTD: siz = SIZD, tok++; break;
279         case DOTP: siz = SIZP, tok++; break;
280         case DOTQ: siz = SIZQ, tok++; break;
281         case DOTS: siz = SIZS, tok++; break;
282         case DOTX: siz = SIZX, tok++; break;
283         }
284
285         // Do special directives (500..999) (These must be handled in "real time")
286         if (state >= 500 && state < 1000)
287         {
288                 switch (state)
289                 {
290                 case MN_IF:
291                         d_if();
292                         goto loop;
293
294                 case MN_ELSE:
295                         d_else();
296                         goto loop;
297
298                 case MN_ENDIF:
299                         d_endif();
300                         goto loop;
301
302                 case MN_IIF:                                            // .iif --- immediate if
303                         if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
304                                 goto loop;
305
306                         if (!(eattr & DEFINED))
307                         {
308                                 error(undef_error);
309                                 goto loop;
310                         }
311
312                         if (*tok++ != ',')
313                         {
314                                 error(comma_error);
315                                 goto loop;
316                         }
317
318                         if (eval == 0)
319                                 goto loop;
320
321                         goto loop1;
322
323                 case MN_MACRO:                                          // .macro --- macro definition
324                         if (!disabled)
325                         {
326                                 // Label on a macro definition is bad mojo... Warn the user
327                                 if (label != NULL)
328                                         warn(lab_ignored);
329
330                                 DefineMacro();
331                         }
332
333                         goto loop;
334
335                 case MN_EXITM:                                          // .exitm --- exit macro
336                 case MN_ENDM:                                           // .endm --- same as .exitm
337                         if (!disabled)
338                         {
339                                 if (label != NULL)
340                                         warn(lab_ignored);
341
342                                 ExitMacro();
343                         }
344
345                         goto loop;
346
347                 case MN_REPT:
348                         if (!disabled)
349                         {
350                                 // Handle labels on REPT directive lines...
351                                 if (label)
352                                 {
353                                         if (HandleLabel(label, labtyp) != 0)
354                                                 goto loop;
355
356                                         label_defined = label;
357                                 }
358
359                                 HandleRept();
360                         }
361
362                         goto loop;
363
364                 case MN_ENDR:
365                         if (!disabled)
366                                 error("mis-nested .endr");
367
368                         goto loop;
369                 }
370         }
371
372 normal:
373         if (disabled)                                                   // Conditionally disabled code
374                 goto loop;
375
376         // Do equates
377         if (equate != NULL)
378         {
379                 // Pick global or local symbol enviroment
380                 j = (*equate == '.' ? curenv : 0);
381                 sy = lookup(equate, LABEL, j);
382
383                 if (sy == NULL)
384                 {
385                         sy = NewSymbol(equate, LABEL, j);
386                         sy->sattr = 0;
387
388                         if (equtyp == DEQUALS)
389                         {
390                                 // Can't GLOBAL a local symbol
391                                 if (j)
392                                 {
393                                         error(locgl_error);
394                                         goto loop;
395                                 }
396
397                                 sy->sattr = GLOBAL;
398                         }
399                 }
400                 else if ((sy->sattr & DEFINED) && equtyp != SET)
401                 {
402                         if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
403                         {
404                                 sy->sattre &= ~UNDEF_EQUR;
405                                 sy->svalue = 0;
406                         }
407                         else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
408                         {
409                                 sy->sattre &= ~UNDEF_CC;
410                                 sy->svalue = 0;
411                         }
412                         else
413                         {
414                                 error("multiple equate to '%s'", sy->sname);
415                                 goto loop;
416                         }
417                 }
418
419                 // Put symbol in "order of definition" list if it's not already there
420                 AddToSymbolDeclarationList(sy);
421
422                 // Parse value to equate symbol to;
423                 // o  .equr
424                 // o  .reg
425                 // o  everything else
426                 if (equtyp == EQUREG)
427                 {
428 //Linko's request to issue a warning on labels that equated to the same
429 //register would go here. Not sure how to implement it though. :-/
430 /*
431 Maybe like this way:
432 have an array of bools with 64 entries. Whenever a register is equated, set the
433 corresponding register bool to true. Whenever it's undef'ed, set it to false.
434 When checking to see if it's already been equated, issue a warning.
435 */
436
437                         if (rgpu || rdsp)
438                         {
439                                 // GPU/DSP architectures need some special TLC for now
440                                 // Check for register to equate to
441                                 if ((*tok >= KW_R0) && (*tok <= KW_R31))
442                                 {
443                                         sy->sattre = EQUATEDREG;        // Mark as equated register
444                                         riscreg = *tok;
445                                         // Check for ",<bank #>" override notation and skip past it.
446                                         // It is ignored now. Was that ever useful anyway?
447                                         if ((tok[1] == ',') && (tok[2] == CONST))
448                                         {
449                                                 // Advance token pointer and skip everything
450                                                 tok += 4;
451                                         }
452
453                                         eattr = ABS | DEFINED | GLOBAL;
454                                         eval = riscreg;
455                                         tok++;
456                                 }
457                                 // Checking for a register symbol
458                                 else if (tok[0] == SYMBOL)
459                                 {
460                                         sy2 = lookup(string[tok[1]], LABEL, j);
461
462                                         // Make sure symbol is a valid equreg
463                                         if (!sy2 || !(sy2->sattre & EQUATEDREG))
464                                         {
465                                                 error("invalid GPU/DSP .equr/.regequ definition");
466                                                 goto loop;
467                                         }
468                                         else
469                                         {
470                                                 eattr = ABS | DEFINED | GLOBAL; // Copy symbol's attributes
471                                                 sy->sattre = sy2->sattre;
472                                                 eval = (sy2->svalue & 0xFFFFF0FF);
473                                                 tok += 2;
474                                         }
475                                 }
476                                 else
477                                 {
478                                         error("invalid GPU/DSP .equr/.regequ definition");
479                                         goto loop;
480                                 }
481                         }
482                         else
483                         {
484                                 sy->sattre = EQUATEDREG;        // Mark as equated register
485                                 eattr = ABS | DEFINED | GLOBAL;
486                                 eval = *tok;
487                                 tok++;
488                         }
489                 }
490                 else if (equtyp == REG)
491                 {
492                         if (reglist(&rmask) < 0)
493                                 goto loop;
494
495                         eval = (uint32_t)rmask;
496                         eattr = ABS | DEFINED;
497                 }
498                 else if (equtyp == CCDEF)
499                 {
500                         sy->sattre |= EQUATEDCC;
501                         eattr = ABS | DEFINED | GLOBAL;
502
503                         if (tok[0] == SYMBOL)
504                         {
505                                 sy2 = lookup(string[tok[1]], LABEL, j);
506
507                                 if (!sy2 || !(sy2->sattre & EQUATEDCC))
508                                 {
509                                         error("invalid gpu/dsp .ccdef definition");
510                                         goto loop;
511                                 }
512                                 else
513                                 {
514                                         eattr = ABS | DEFINED | GLOBAL;
515                                         sy->sattre = sy2->sattre;
516                                         eval = sy2->svalue;
517                                         tok += 2;
518                                 }
519                         }
520                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
521                                 goto loop;
522                 }
523                 // equ an equr
524                 else if (*tok == SYMBOL)
525                 {
526                         sy2 = lookup(string[tok[1]], LABEL, j);
527
528                         if (sy2 && (sy2->sattre & EQUATEDREG))
529                         {
530                                 sy->stype = sy2->stype;
531                                 sy->sattr = sy2->sattr;
532                                 sy->sattre = sy2->sattre;
533                                 sy->svalue = sy2->svalue;
534                                 goto loop;
535                         }
536                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
537                                 goto loop;
538                 }
539                 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
540                         goto loop;
541
542                 if (!(eattr & DEFINED))
543                 {
544                         error(undef_error);
545                         goto loop;
546                 }
547
548                 sy->sattr |= eattr | EQUATED;   // Symbol inherits value and attributes
549                 sy->svalue = eval;
550
551                 if (list_flag)                                  // Put value in listing
552                         listvalue((uint32_t)eval);
553
554                 ErrorIfNotAtEOL();                              // Must be at EOL now
555                 goto loop;
556         }
557
558         // Do labels
559         if (label != NULL)
560         {
561                 // Non-zero == error occurred
562                 if (HandleLabel(label, labtyp) != 0)
563                         goto loop;
564
565                 label_defined = label;
566         }
567
568         // Punt on EOL
569         if (state == -3)
570                 goto loop;
571
572         // If we're in 6502 mode and are still in need of a mnemonic, then search
573         // for valid 6502 mnemonic.
574         if (m6502 && (state < 0 || state >= 1000))
575         {
576 #ifdef ST
577                 state = kmatch(opname, mpbase, mpcheck, mptab, mpaccept);
578 #else
579                 for(state=0, p=opname; state>= 0; )
580                 {
581                         j = mpbase[state] + tolowertab[*p];
582
583                         if (mpcheck[j] != state)        // Reject, character doesn't match
584                         {
585                                 state = -1;             // No match
586                                 break;
587                         }
588
589                         if (!*++p)
590                         {                       // Must accept or reject at EOS
591                                 state = mpaccept[j];    // (-1 on no terminal match)
592                                 break;
593                         }
594
595                         state = mptab[j];
596                 }
597 #endif
598
599                 // Call 6502 code generator if we found a mnemonic
600                 if (state >= 2000)
601                 {
602                         m6502cg(state - 2000);
603                         goto loop;
604                 }
605         }
606
607         // If we are in GPU or DSP mode and still in need of a mnemonic then search
608         // for one
609         if ((rgpu || rdsp) && (state < 0 || state >= 1000))
610         {
611                 for(state=0, p=opname; state>=0;)
612                 {
613                         j = mrbase[state] + (int)tolowertab[*p];
614
615                         // Reject, character doesn't match
616                         if (mrcheck[j] != state)
617                         {
618                                 state = -1;                                     // No match
619                                 break;
620                         }
621
622                         // Must accept or reject at EOS
623                         if (!*++p)
624                         {
625                                 state = mraccept[j];            // (-1 on no terminal match)
626                                 break;
627                         }
628
629                         state = mrtab[j];
630                 }
631
632                 // Call RISC code generator if we found a mnemonic
633                 if (state >= 3000)
634                 {
635                         GenerateRISCCode(state);
636                         goto loop;
637                 }
638         }
639
640         // If we are in OP mode and still in need of a mnemonic then search for one
641         if (robjproc && ((state < 0) || (state >= 1000)))
642         {
643                 for(state=0, p=opname; state>=0;)
644                 {
645                         j = mobase[state] + (int)tolowertab[*p];
646
647                         // Reject, character doesn't match
648                         if (mocheck[j] != state)
649                         {
650                                 state = -1;                                     // No match
651                                 break;
652                         }
653
654                         // Must accept or reject at EOS
655                         if (!*++p)
656                         {
657                                 state = moaccept[j];            // (-1 on no terminal match)
658                                 break;
659                         }
660
661                         state = motab[j];
662                 }
663
664                 // Call OP code generator if we found a mnemonic
665                 if (state >= 3100)
666                 {
667                         GenerateOPCode(state);
668                         goto loop;
669                 }
670         }
671
672         // If we are in 56K mode and still in need of a mnemonic then search for one
673         if (dsp56001 && ((state < 0) || (state >= 1000)))
674         {
675                 for(state=0, p=opname; state>=0;)
676                 {
677                         j = dspbase[state] + (int)tolowertab[*p];
678
679                         // Reject, character doesn't match
680                         if (dspcheck[j] != state)
681                         {
682                                 state = -1;                                     // No match
683                                 break;
684                         }
685
686                         // Must accept or reject at EOS
687                         if (!*++p)
688                         {
689                                 state = dspaccept[j];           // (-1 on no terminal match)
690                                 break;
691                         }
692
693                         state = dsptab[j];
694                 }
695
696                 // Call DSP code generator if we found a mnemonic
697                 if (state >= 2000)
698                 {
699                         LONG parcode;
700                         int operands;
701                         MNTABDSP * md = &dsp56k_machtab[state - 2000];
702                         deposit_extra_ea = 0;   // Assume no extra word needed
703
704                         if (md->mnfunc == dsp_mult)
705                         {
706                                 // Special case for multiplication instructions: they require
707                                 // 3 operands
708                                 if ((operands = dsp_amode(3)) == ERROR)
709                                         goto loop;
710                         }
711                         else if ((md->mnattr & PARMOVE) && md->mn0 != M_AM_NONE)
712                         {
713                                 if (dsp_amode(2) == ERROR)
714                                         goto loop;
715                         }
716                         else if ((md->mnattr & PARMOVE) && md->mn0 == M_AM_NONE)
717                         {
718                                 // Instructions that have parallel moves but use no operands
719                                 // (probably only move). In this case, don't parse addressing
720                                 // modes--just go straight to parallel parse
721                                 dsp_am0 = dsp_am1 = M_AM_NONE;
722                         }
723                         else
724                         {
725                                 // Non parallel move instructions can have up to 4 parameters
726                                 // (well, only tcc instructions really)
727                                 if ((operands = dsp_amode(4)) == ERROR)
728                                         goto loop;
729
730                                 if (operands == 4)
731                                 {
732                                         dsp_tcc4(md->mninst);
733                                         goto loop;
734                                 }
735                         }
736
737                         if (md->mnattr & PARMOVE)
738                         {
739                                 // Check for parallel moves
740                                 if ((parcode = parmoves(dsp_a1reg)) == ERROR)
741                                         goto loop;
742                         }
743                         else
744                         {
745                                 if (*tok != EOL)
746                                         error("parallel moves not allowed with this instruction");
747
748                                 parcode = 0;
749                         }
750
751                         while ((dsp_am0 & md->mn0) == 0 || (dsp_am1 & md->mn1) == 0)
752                                 md = &dsp56k_machtab[md->mncont];
753
754                         (*md->mnfunc)(md->mninst | (parcode << 8));
755                         goto loop;
756                 }
757         }
758
759         // Invoke macro or complain about bad mnemonic
760         if (state < 0)
761         {
762                 if ((sy = lookup(opname, MACRO, 0)) != NULL)
763                         InvokeMacro(sy, siz);
764                 else
765                         error("unknown op '%s'", opname);
766
767                 goto loop;
768         }
769
770         // Call directive handlers
771         if (state < 500)
772         {
773                 (*dirtab[state])(siz);
774                 goto loop;
775         }
776
777         // Do mnemonics
778         //  o  can't deposit instrs in BSS or ABS
779         //  o  do automatic .EVEN for instrs
780         //  o  allocate space for largest possible instr
781         //  o  can't do ".b" operations with an address register
782         if (scattr & SBSS)
783         {
784                 error("cannot initialize non-storage (BSS) section");
785                 goto loop;
786         }
787
788         if (sloc & 1)                                   // Automatic .even
789                 auto_even();
790
791         if (challoc - ch_size < 18)             // Make sure have space in current chunk
792                 chcheck(0);
793
794         m = &machtab[state - 1000];
795
796         // Call special-mode handler
797         if (m->mnattr & CGSPECIAL)
798         {
799                 (*m->mnfunc)(m->mninst, siz);
800                 goto loop;
801         }
802
803         if (amode(1) < 0)                               // Parse 0, 1 or 2 addr modes
804                 goto loop;
805
806         // Check that we're at EOL
807         // The only exception is ptestr/ptestw instructions
808         // that have 3 or 4 operands and are not handled by
809         // amode(). (yes, we're taking a performance hit here sadly)
810         if (m->mnfunc != m_ptestr && m->mnfunc != m_ptestw)
811                 if (*tok != EOL)
812                         error(extra_stuff);
813
814         amsk0 = amsktab[am0];
815         amsk1 = amsktab[am1];
816
817         // Catch attempts to use ".B" with an address register (yes, this check
818         // does work at this level)
819         if (siz == SIZB && (am0 == AREG || am1 == AREG))
820         {
821                 error("cannot use '.b' with an address register");
822                 goto loop;
823         }
824
825         // Keep a backup of chptr (used for optimisations during codegen)
826         chptr_opcode = chptr;
827
828         while (!(m->mnattr & siz) || (amsk0 & m->mn0) == 0 || (amsk1 & m->mn1) == 0)
829                 m = &machtab[m->mncont];
830
831         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); }
832
833         (*m->mnfunc)(m->mninst, siz);
834         goto loop;
835 }
836
837
838 //
839 // Handle the creation of labels
840 //
841 int HandleLabel(char * label, int labelType)
842 {
843         // Check for dot in front of label; means this is a local label if present
844         int environment = (*label == '.' ? curenv : 0);
845         SYM * symbol = lookup(label, LABEL, environment);
846
847         if (symbol == NULL)
848         {
849                 symbol = NewSymbol(label, LABEL, environment);
850                 symbol->sattr = 0;
851                 symbol->sattre = 0;
852         }
853         else if (symbol->sattr & DEFINED)
854                 return error("multiply-defined label '%s'", label);
855
856         // Put symbol in "order of definition" list if it's not already in it
857         AddToSymbolDeclarationList(symbol);
858
859         if (orgactive)
860         {
861                 symbol->svalue = orgaddr;
862                 symbol->sattr |= ABS | DEFINED | EQUATED;
863         }
864         else
865         {
866                 symbol->svalue = sloc;
867                 symbol->sattr |= DEFINED | cursect;
868         }
869
870         lab_sym = symbol;
871
872         // Yes, our CS professors told us to write checks for equality this way,
873         // but damn, it hurts my brain every time I look at it. :-/
874         if (0 == environment)
875                 curenv++;
876
877         // Make label global if it has a double colon
878         if (labelType == DCOLON)
879         {
880                 if (environment != 0)
881                         return error(locgl_error);
882
883                 symbol->sattr |= GLOBAL;
884         }
885
886         return 0;
887 }
888