Fix silly mask bugs, added check for use of undefined register equates.
[rmac] / procln.c
1 //
2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // PROCLN.C - Line Processing
4 // Copyright (C) 199x Landon Dyer, 2011 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 "listing.h"
11 #include "amode.h"
12 #include "error.h"
13 #include "sect.h"
14 #include "expr.h"
15 #include "mach.h"
16 #include "direct.h"
17 #include "macro.h"
18 #include "symbol.h"
19 #include "riscasm.h"
20
21 #define DEF_KW                                  // Declare keyword values 
22 #include "kwtab.h"                              // Incl generated keyword tables & defs
23
24 #define DEF_MN                                  // Incl 68k keyword definitions
25 #define DECL_MN                                 // Incl 68k keyword state machine tables
26 #include "mntab.h"
27
28 #define DEF_MR
29 #define DECL_MR
30 #include "risckw.h"
31
32
33 IFENT * ifent;                                  // Current ifent
34 static IFENT ifent0;                    // Root ifent
35 static IFENT * f_ifent;                 // Freelist of ifents
36 static int disabled;                    // Assembly conditionally disabled
37 int just_bss;                                   // 1, ds.b in microprocessor mode 
38 VALUE pcloc;                                    // Value of "PC" at beginning of line 
39 IFENT * ifent;                                  // Current ifent
40 SYM * lab_sym;                                  // Label on line (or NULL)
41
42 char extra_stuff[] = "extra (unexpected) text found after addressing mode";
43 char * comma_error = "missing comma";
44 char * syntax_error = "syntax error";
45 char * locgl_error = "cannot GLOBL local symbol";
46 char * lab_ignored = "label ignored";
47
48 // Table to convert an addressing-mode number to a bitmask.
49 LONG amsktab[0112] = {
50         M_DREG, M_DREG, M_DREG, M_DREG,
51         M_DREG, M_DREG, M_DREG, M_DREG,
52
53         M_AREG, M_AREG, M_AREG, M_AREG,
54         M_AREG, M_AREG, M_AREG, M_AREG,
55
56         M_AIND, M_AIND, M_AIND, M_AIND,
57         M_AIND, M_AIND, M_AIND, M_AIND,
58
59         M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
60         M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
61
62         M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
63         M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
64
65         M_ADISP, M_ADISP, M_ADISP, M_ADISP,
66         M_ADISP, M_ADISP, M_ADISP, M_ADISP,
67
68         M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
69         M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
70
71         M_ABSW,                                                                                 // 070
72         M_ABSL,                                                                                 // 071
73         M_PCDISP,                                                                               // 072
74         M_PCINDEXED,                                                                    // 073
75         M_IMMED,                                                                                // 074
76         0L,                                                                                             // 075
77         0L,                                                                                             // 076
78         0L,                                                                                             // 077
79         M_ABASE,                                                                                // 0100
80         M_MEMPOST,                                                                              // 0101 
81         M_MEMPRE,                                                                               // 0102 
82         M_PCBASE,                                                                               // 0103
83         M_PCMPOST,                                                                              // 0104
84         M_PCMPRE,                                                                               // 0105
85         M_AM_USP,                                                                               // 0106
86         M_AM_SR,                                                                                // 0107 
87         M_AM_CCR,                                                                               // 0110
88         M_AM_NONE                                                                               // 0111 
89 };                                                                                                      // 0112 length
90
91
92 //
93 // Initialize Line Processor
94 //
95 void init_procln(void)
96 {
97         disabled = 0;
98         ifent = &ifent0;
99         f_ifent = ifent0.if_prev = NULL;
100         ifent0.if_state = 0;
101 }
102
103
104 //
105 // Line Processor
106 //
107 void Assemble(void)
108 {
109         int state;                                      // Keyword machine state (output)
110         int j;                                          // Random int, must be fast
111         char * p;                                       // Random char ptr, must be fast
112         TOKEN * tk;                                     // First token in line
113         char * label;                           // Symbol (or NULL)
114         char * equate;                          // Symbol (or NULL)
115         int labtyp = 0;                         // Label type (':', DCOLON)
116         int equtyp = 0;                         // Equ type ('=', DEQUALS)
117         VALUE eval;                                     // Expression value
118         WORD eattr;                                     // Expression attributes
119         SYM * esym;                                     // External symbol involved in expr.
120         WORD siz = 0;                           // Size suffix to mnem/diretve/macro
121         LONG amsk0, amsk1;                      // Address-type masks for ea0, ea1
122         MNTAB * m;                                      // Code generation table pointer
123         SYM * sy, * sy2;                        // Symbol (temp usage)
124         char * opname = NULL;           // Name of dirctve/mnemonic/macro
125         int listflag;                           // 0: Don't call listeol()
126         int as68mode = 0;                       // 1: Handle multiple labels
127         WORD rmask;                                     // Register list, for REG
128         int registerbank;                       // RISC register bank
129         int riscreg;                            // RISC register
130
131         listflag = 0;                           // Initialise listing flag
132
133 loop:                                                   // Line processing loop label
134
135         // Get another line of tokens
136         if (tokln() == TKEOF)
137         {
138 if (verb_flag) printf("Assemble: Found TKEOF flag...\n");
139                 if (list_flag && listflag)                      // Flush last line of source
140                         listeol();
141
142                 if (ifent->if_prev != NULL)                     // Check conditional token
143                         error("hit EOF without finding matching .endif");
144
145                 return;
146         }
147
148         DEBUG DumpTokenBuffer();
149
150         if (list_flag)
151         {
152                 if (listflag && listing > 0)
153                         listeol();                                              // Tell listing generator about EOL
154
155                 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
156                 listflag = 1;                                           // OK to call `listeol' now
157                 just_bss = 0;                                           // Reset just_bss mode
158         }
159
160         state = -3;                                                             // No keyword (just EOL)
161         label = NULL;                                                   // No label
162         lab_sym = NULL;                                                 // No (exported) label
163         equate = NULL;                                                  // No equate
164         tk = tok;                                                               // Save first token in line
165         pcloc = (VALUE)sloc;                                    // Set beginning-of-line PC
166
167 loop1:                                                                          // Internal line processing loop
168
169         if (*tok == EOL)                                                // Restart loop if end-of-line
170                 goto loop;
171
172         // First token MUST be a symbol
173         if (*tok != SYMBOL)
174         {
175                 error(syntax_error);
176                 goto loop;
177         }
178
179         j = (int)tok[2];                                                // Skip equates (normal statements)
180
181         if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
182         {
183 //              equate = (char *)tok[1];
184                 equate = string[tok[1]];
185                 equtyp = j;
186                 tok += 3;
187                 goto normal;
188         }
189
190         // Skip past label (but record it)
191         if (j == ':' || j == DCOLON)
192         {
193 as68label:
194 //              label = (char *)tok[1];                         // Get label name
195                 label = string[tok[1]];                         // Get label name
196                 labtyp = tok[2];                                        // Get label type
197                 tok += 3;                                                       // Go to next line token
198
199                 // Handle multiple labels; if there's another label, go process it, 
200                 // and come back at `as68label' above.
201                 if (as68_flag)
202                 {
203                         as68mode = 0;
204
205                         if (*tok == SYMBOL && tok[2] == ':')
206                         {
207                                 as68mode = 1;
208                                 goto do_label;
209                         }
210                 }
211         }
212
213         if (*tok == EOL)                                                // EOL is legal here...
214                 goto normal;
215
216         // Next token MUST be a symbol
217         if (*tok++ != SYMBOL)
218         {
219                 error(syntax_error);
220                 goto loop;
221         }
222
223 // This is the problem here: On 64-bit platforms, this cuts the native pointer
224 // in half. We need to figure out how to fix this.
225 //#warning "!!! Bad pointer !!!"
226 #if 0
227         opname = p = (char *)*tok++;                    // Store opcode name here
228 #else
229         opname = p = string[*tok++];
230 #endif
231
232         // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
233         // On output, `state' will have one of the values:
234         //    -3          there was no symbol (EOL)
235         //    -2..-1      the symbol didn't match any keyword
236         //    0..499      vanilla directives (dc, ds, etc.)
237         //    500..999    electric directives (macro, rept, etc.)
238         //    1000..+     mnemonics (move, lsr, etc.)
239         for(state=0; state>=0;)
240         {
241                 j = mnbase[state] + (int)tolowertab[*p];
242
243                 // Reject, character doesn't match
244                 if (mncheck[j] != state)
245                 {
246                         state = -1;                                             // No match
247                         break;
248                 }
249
250                 // Must accept or reject at EOS
251                 if (!*++p)
252                 {
253                         state = mnaccept[j];                    // (-1 on no terminal match)
254                         break;
255                 }
256
257                 state = mntab[j];
258         }
259
260         // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
261         siz = SIZN;
262
263         if (*tok == DOTW) 
264                 siz = SIZW, ++tok;
265         else if (*tok == DOTL)
266                 siz = SIZL, ++tok;
267         else if (*tok == DOTB)
268                 siz = SIZB, ++tok;
269
270         // Do special directives (500..999) (These must be handled in "real time")
271         if (state >= 500 && state < 1000)
272         {
273                 switch (state)
274                 {
275                 case MN_IF:
276                         d_if ();
277                 goto loop;
278                 case MN_ELSE:
279                         d_else();
280                         goto loop;
281                 case MN_ENDIF:
282                         d_endif ();
283                         goto loop;
284                 case MN_IIF:                                            // .iif --- immediate if
285                         if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
286                                 goto loop;
287
288                         if (!(eattr & DEFINED))
289                         {
290                                 error(undef_error);
291                                 goto loop;
292                         }
293
294                         if (*tok++ != ',')
295                         {
296                                 error(comma_error);
297                                 goto loop;
298                         }
299
300                         if (eval == 0)
301                                 goto loop;
302
303                         goto loop1;
304                 case MN_MACRO:                                          // .macro --- macro definition
305                         if (!disabled)
306                         {
307                                 if (label != NULL)
308                                         warn(lab_ignored);
309
310                                 DefineMacro();
311                         }
312
313                         goto loop;
314                 case MN_EXITM:                                          // .exitm --- exit macro
315                 case MN_ENDM:                                           // .endm --- same as .exitm
316                         if (!disabled)
317                         {
318                                 if (label != NULL)
319                                         warn(lab_ignored);
320
321                                 ExitMacro();
322                         }
323
324                         goto loop;
325                 case MN_REPT:
326                         if (!disabled)
327                         {
328                                 if (label != NULL)
329                                         warn(lab_ignored);
330
331                                 defrept();
332                         }
333
334                         goto loop;
335                 case MN_ENDR:
336                         if (!disabled)
337                                 error("mis-nested .endr");
338
339                         goto loop;
340                 }
341         }
342
343 normal:
344         if (disabled)                                                   // Conditionally disabled code
345                 goto loop;
346
347         // Do equates
348         if (equate != NULL)
349         {
350                 j = 0;                                                          // Pick global or local sym enviroment
351
352                 if (*equate == '.')
353                         j = curenv;
354
355                 sy = lookup(equate, LABEL, j);
356
357                 if (sy == NULL)
358                 {
359                         sy = NewSymbol(equate, LABEL, j);
360                         sy->sattr = 0;
361
362                         if (equtyp == DEQUALS)
363                         {
364                                 // Can't GLOBAL a local symbol
365                                 if (j)
366                                 {
367                                         error(locgl_error);
368                                         goto loop;
369                                 }
370
371                                 sy->sattr = GLOBAL;
372                         }
373                 }
374                 else if ((sy->sattr & DEFINED) && equtyp != SET)
375                 {
376                         if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
377                         {
378 //REALLY?                               sy->sattre |= ~UNDEF_EQUR; 
379                                 sy->sattre &= ~UNDEF_EQUR; 
380                                 sy->svalue  = 0;
381                         }
382                         else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
383                         {
384 //REALLY?                               sy->sattre |= ~UNDEF_CC;
385                                 sy->sattre &= ~UNDEF_CC;
386                                 sy->svalue = 0;
387                         }
388                         else
389                         {
390                                 errors("multiple equate to '%s'", sy->sname);
391                                 goto loop;
392                         }
393                 }
394
395                 // Put symbol in "order of definition" list
396                 if (!(sy->sattr & SDECLLIST))
397                         sym_decl(sy);
398
399                 // Parse value to equate symbol to;
400                 // o  .equr
401                 // o  .reg
402                 // o  everything else
403                 if (equtyp == EQUREG)
404                 {
405                         // Check that we are in a RISC section
406                         if (!rgpu && !rdsp)
407                         {
408                                 error(".equr/.regequ must be defined in .gpu/.dsp section");
409                                 goto loop;
410                         }
411
412                         // Check for register to equate to
413                         if ((*tok >= KW_R0) && (*tok <= KW_R31))
414                         {
415                                 sy->sattre  = EQUATEDREG | RISCSYM;     // Mark as equated register
416                                 riscreg = (*tok - KW_R0);
417                                 sy->sattre |= (riscreg << 8);           // Store register number
418
419                                 if ((tok[1] == ',') && (tok[2] == CONST))
420                                 {
421                                         tok += 3;
422
423                                         if (*tok == 0)
424                                                 registerbank = BANK_0;
425                                         else if (*tok == 1)
426                                                 registerbank = BANK_1;
427                                         else
428                                                 registerbank = BANK_N;
429                                 }
430                                 else
431                                 {
432                                         registerbank = BANK_N;
433                                 }
434
435                                 sy->sattre |= regbank;          // Store register bank
436                                 eattr = ABS | DEFINED | GLOBAL;
437                                 eval = 0x80000080 + (riscreg) + (registerbank << 8);
438                                 tok++;
439                         }
440                         // Checking for a register symbol
441                         else if (tok[0] == SYMBOL)
442                         {
443 //                              sy2 = lookup((char *)tok[1], LABEL, j);
444                                 sy2 = lookup(string[tok[1]], LABEL, j);
445
446                                 // Make sure symbol is a valid equreg
447                                 if (!sy2 || !(sy2->sattre & EQUATEDREG))
448                                 {
449                                         error("invalid GPU/DSP .equr/.regequ definition");
450                                         goto loop;
451                                 }
452                                 else
453                                 {
454                                         eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
455                                         sy->sattre = sy2->sattre;
456                                         eval = (sy2->svalue & 0xFFFFF0FF);
457                                         tok += 2;
458                                 }
459                         }
460                         else
461                         {
462                                 error("invalid GPU/DSP .equr/.regequ definition");
463                                 goto loop;
464                         }
465                 }
466                 else if (equtyp == REG)
467                 {
468                         if (reglist(&rmask) < 0)
469                                 goto loop;
470
471                         eval = (VALUE)rmask;
472                         eattr = ABS | DEFINED;
473                 }
474                 else if (equtyp == CCDEF)
475                 {
476                         sy->sattre |= EQUATEDCC;
477                         eattr = ABS | DEFINED | GLOBAL;
478
479                         if (tok[0] == SYMBOL)
480                         {
481 //                              sy2 = lookup((char *)tok[1], LABEL, j);
482                                 sy2 = lookup(string[tok[1]], LABEL, j);
483
484                                 if (!sy2 || !(sy2->sattre & EQUATEDCC))
485                                 {
486                                         error("invalid gpu/dsp .ccdef definition");
487                                         goto loop;
488                                 }
489                                 else
490                                 {
491                                         eattr = ABS | DEFINED | GLOBAL;
492                                         sy->sattre = sy2->sattre;
493                                         eval = sy2->svalue;
494                                         tok += 2;
495                                 }
496                         }
497                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
498                                 goto loop;
499                 }
500                 //equ a equr
501                 else if (*tok == SYMBOL)
502                 {
503 //                      sy2 = lookup((char *)tok[1], LABEL, j);
504                         sy2 = lookup(string[tok[1]], LABEL, j);
505
506                         if (sy2 && (sy2->sattre & EQUATEDREG))
507                         {
508                                 sy->stype = sy2->stype;
509                                 sy->sattr = sy2->sattr;
510                                 sy->sattre = sy2->sattre;
511                                 sy->svalue = (sy2->svalue & 0xFFFFF0FF);
512                                 goto loop;
513                         }
514                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
515                                 goto loop;
516                 }
517                 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
518                         goto loop;
519
520                 if (!(eattr & DEFINED))
521                 {
522                         error(undef_error);
523                         goto loop;
524                 }
525
526                 sy->sattr |= eattr | EQUATED;           // Symbol inherits value and attributes
527                 sy->svalue = eval;
528
529                 if (list_flag)                                          // Put value in listing
530                         listvalue(eval);
531
532                 at_eol();                                                       // Must be at EOL now
533                 goto loop;
534         }
535
536         // Do labels
537         if (label != NULL)
538         {
539 do_label:
540                 // Check for dot in front of label; means this is a local label if present
541 #if 0
542                 j = 0;
543
544                 if (*label == '.')
545                         j = curenv;
546 #else
547                 j = (*label == '.' ? curenv : 0);
548 #endif
549
550                 sy = lookup(label, LABEL, j);
551
552                 if (sy == NULL)
553                 {
554                         sy = NewSymbol(label, LABEL, j);
555                         sy->sattr = 0;
556                         sy->sattre = RISCSYM;
557                 }
558                 else if (sy->sattr & DEFINED)
559                 {
560                         errors("multiply-defined label '%s'", label);
561                         goto loop;
562                 }
563
564                 // Put symbol in "order of definition" list
565                 if (!(sy->sattr & SDECLLIST))
566                         sym_decl(sy);
567
568                 if (orgactive)
569                 {
570                         sy->svalue = orgaddr;
571                         sy->sattr |= ABS | DEFINED | EQUATED;
572                 }
573                 else
574                 {
575                         sy->svalue = sloc;
576                         sy->sattr |= DEFINED | cursect;
577                 }
578
579                 lab_sym = sy;
580
581                 if (!j)
582                         curenv++;
583
584                 // Make label global
585                 if (labtyp == DCOLON)
586                 {
587                         if (j)
588                         {
589                                 error(locgl_error);
590                                 goto loop;
591                         }
592
593                         sy->sattr |= GLOBAL;
594                 }
595
596                 // If we're in as68 mode, and there's another label, go back and handle it
597                 if (as68_flag && as68mode)
598                         goto as68label;
599         }
600
601         // Punt on EOL
602         if (state == -3)
603                 goto loop;
604
605         // If we are in GPU or DSP mode and still in need of a mnemonic then search
606         // for one
607         if ((rgpu || rdsp) && (state < 0 || state >= 1000))
608         {
609                 for(state=0, p=opname; state>=0;)
610                 {
611                         j = mrbase[state] + (int)tolowertab[*p];
612
613                         // Reject, character doesn't match
614                         if (mrcheck[j] != state)
615                         {
616                                 state = -1;                                     // No match
617                                 break;
618                         }
619
620                         // Must accept or reject at EOS
621                         if (!*++p)
622                         {
623                                 state = mraccept[j];            // (-1 on no terminal match)
624                                 break;
625                         }
626
627                         state = mrtab[j];
628                 }
629
630                 // Call RISC code generator if we found a mnemonic
631                 if (state >= 3000)
632                 {
633                         GenerateRISCCode(state);
634                         goto loop;
635                 }
636         }
637
638         // Invoke macro or complain about bad mnemonic
639         if (state < 0)
640         {
641                 if ((sy = lookup(opname, MACRO, 0)) != NULL) 
642                         InvokeMacro(sy, siz);
643                 else
644                         errors("unknown op '%s'", opname);
645
646                 goto loop;
647         }
648
649         // Call directive handlers
650         if (state < 500)
651         {
652                 (*dirtab[state])(siz);
653                 goto loop;
654         }
655
656         // Do mnemonics
657         // o  can't deposit instrs in BSS or ABS
658         // o  do automatic .EVEN for instrs
659         // o  allocate space for largest possible instr
660         // o  can't do ".b" operations with an address register
661         if (scattr & SBSS)
662         {
663                 error("cannot initialize non-storage (BSS) section");
664                 goto loop;
665         }
666
667         if (sloc & 1)                                                   // Automatic .even
668                 auto_even();
669
670         if (challoc - ch_size < 18)                             // Make sure have space in current chunk
671                 chcheck(0);
672
673         m = &machtab[state - 1000];
674
675         // Call special-mode handler
676         if (m->mnattr & CGSPECIAL)
677         {
678                 (*m->mnfunc)(m->mninst, siz);
679                 goto loop;
680         }
681
682         if (amode(1) < 0)                                               // Parse 0, 1 or 2 addr modes
683                 goto loop;
684
685         if (*tok != EOL)
686                 error(extra_stuff);
687
688         amsk0 = amsktab[am0];
689         amsk1 = amsktab[am1];
690
691         // Catch attempts to use ".B" with an address register (yes, this check
692         // does work at this level)
693         if (siz == SIZB && (am0 == AREG || am1 == AREG))
694         {
695                 error("cannot use '.b' with an address register");
696                 goto loop;
697         }
698
699         for(;;)
700         {
701                 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
702                 {
703                         (*m->mnfunc)(m->mninst, siz);
704                         goto loop;
705                 }
706
707                 m = &machtab[m->mncont];
708         }
709 }
710
711
712 // 
713 // .if, Start Conditional Assembly
714 //
715 int d_if(void)
716 {
717         IFENT * rif;
718         WORD eattr;
719         VALUE eval;
720         SYM * esym;
721
722         // Alloc an IFENTRY
723         if ((rif = f_ifent) == NULL)
724                 rif = (IFENT *)malloc(sizeof(IFENT));
725         else
726                 f_ifent = rif->if_prev;
727
728         rif->if_prev = ifent;
729         ifent = rif;
730
731         if (!disabled)
732         {
733                 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
734                         return 0;
735
736                 if ((eattr & DEFINED) == 0)
737                         return error(undef_error);
738
739                 disabled = !eval;
740         }
741
742         rif->if_state = (WORD)disabled;
743         return 0;
744 }
745
746
747 // 
748 // .else, Do Alternate Case For .if
749 //
750 int d_else(void)
751 {
752         IFENT * rif = ifent;
753
754         if (rif->if_prev == NULL)
755                 return error("mismatched .else");
756
757         if (disabled)
758                 disabled = rif->if_prev->if_state;
759         else
760                 disabled = 1;
761
762         rif->if_state = (WORD)disabled;
763         return 0;
764 }
765
766
767 //
768 // .endif, End of conditional assembly block
769 // This is also called by fpop() to pop levels of IFENTs in case a macro or
770 // include file exits early with `exitm' or `end'.
771 //
772 int d_endif (void)
773 {
774         IFENT * rif = ifent;
775
776         if (rif->if_prev == NULL)
777                 return error("mismatched .endif");
778
779         ifent = rif->if_prev;
780         disabled = rif->if_prev->if_state;
781         rif->if_prev = f_ifent;
782         f_ifent = rif;
783         return 0;
784 }