More readability cleanups.
[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 "risca.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 IFENT * ifent;                                               // Current ifent
33 static IFENT ifent0;                                        // Root ifent
34 static IFENT * f_ifent;                                      // Freelist of ifents
35 static int disabled;                                        // Assembly conditionally disabled
36 int just_bss;                                               // 1, ds.b in microprocessor mode 
37 VALUE pcloc;                                                // Value of "PC" at beginning of line 
38 IFENT * ifent;                                               // Current ifent
39 SYM * lab_sym;                                               // Label on line (or NULL)
40
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         if (tokln() == TKEOF)
136         {                                   // Get another line of tokens
137                 if (list_flag && listflag)                             // Flush last line of source
138                         listeol();
139
140                 if (ifent->if_prev != NULL)                            // Check conditional token
141                         error("hit EOF without finding matching .endif");
142
143                 return;
144         }
145
146         if (list_flag)
147         {
148                 if (listflag && listing > 0)
149                         listeol();                // Tell listing generator about EOL
150
151                 lstout((char)(disabled ? '-' : lntag));             // Prepare new line for listing
152                 listflag = 1;                                         // OK to call `listeol' now
153                 just_bss = 0;                                         // Reset just_bss mode
154         }
155
156         state = -3;                                              // No keyword (just EOL)
157         label = NULL;                                            // No label
158         lab_sym = NULL;                                          // No (exported) label
159         equate = NULL;                                           // No equate
160         tk = tok;                                                // Save first token in line
161         pcloc = (VALUE)sloc;                                     // Set beginning-of-line PC
162
163         loop1:                                                   // Internal line processing loop
164
165         if (*tok == EOL)                                          // Restart loop if end-of-line
166                 goto loop;
167
168         if (*tok != SYMBOL)
169         {                                     // First token MUST be a symbol
170                 error(syntax_error);
171                 goto loop;
172         }
173
174         j = (int)tok[2];                                         // Skip equates (normal statements)
175
176         if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
177         {
178                 equate = (char *)tok[1];
179                 equtyp = j;
180                 tok += 3;
181                 goto normal;
182         }
183
184         if (j == ':' || j == DCOLON)
185         {                            // Skip past label (but record it)
186 as68label:
187                 label = (char *)tok[1];                               // Get label name
188                 labtyp = tok[2];                                      // Get label type
189                 tok += 3;                                             // Go to next line token
190
191                 // Handle multiple labels; if there's another label, go process it, 
192                 // and come back at `as68label' above.
193                 if (as68_flag)
194                 {
195                         as68mode = 0;
196
197                         if (*tok == SYMBOL && tok[2] == ':')
198                         {
199                                 as68mode = 1;
200                                 goto do_label;
201                         }
202                 }
203         }
204
205         if (*tok == EOL)                                          // EOL is legal here...
206                 goto normal;
207
208         if (*tok++ != SYMBOL)
209         {                                   // Next token MUST be a symbol
210                 error(syntax_error);
211                 goto loop;
212         }
213
214         opname = p = (char *)*tok++;                             // Store opcode name here
215
216         // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
217         // On output, `state' will have one of the values:
218         //    -3          there was no symbol (EOL)
219         //    -2..-1      the symbol didn't match any keyword
220         //    0..499      vanilla directives (dc, ds, etc.)
221         //    500..999    electric directives (macro, rept, etc.)
222         //    1000..+     mnemonics (move, lsr, etc.)
223         for(state=0; state>=0;)
224         {
225                 j = mnbase[state] + (int)tolowertab[*p];
226
227                 if (mncheck[j] != state)
228                 {                             // Reject, character doesn't match
229                         state = -1;                                        // No match
230                         break;
231                 }
232
233                 if (!*++p)
234                 {                                           // Must accept or reject at EOS
235                         state = mnaccept[j];                               // (-1 on no terminal match)
236                         break;
237                 }
238
239                 state = mntab[j];
240         }
241
242         // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
243         siz = SIZN;
244
245         if (*tok == DOTW) 
246                 siz = SIZW, ++tok;
247         else if (*tok == DOTL)
248                 siz = SIZL, ++tok;
249         else if (*tok == DOTB)
250                 siz = SIZB, ++tok;
251
252         // Do special directives (500..999) (These must be handled in "real time")
253         if (state >= 500 && state < 1000)
254         {
255                 switch (state)
256                 {
257                 case MN_IF:
258                         d_if ();
259                 goto loop;
260                 case MN_ELSE:
261                         d_else();
262                         goto loop;
263                 case MN_ENDIF:
264                         d_endif ();
265                         goto loop;
266                 case MN_IIF:                                       // .iif --- immediate if
267                         if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
268                                 goto loop;
269
270                         if (!(eattr & DEFINED))
271                         {
272                                 error(undef_error);
273                                 goto loop;
274                         }
275
276                         if (*tok++ != ',')
277                         {
278                                 error(comma_error);
279                                 goto loop;
280                         }
281
282                         if (eval == 0)
283                                 goto loop;
284
285                         goto loop1;
286                 case MN_MACRO:                                     // .macro --- macro definition
287                         if (!disabled)
288                         {
289                                 if (label != NULL)
290                                         warn(lab_ignored);
291
292                                 defmac();
293                         }
294
295                         goto loop;
296                 case MN_EXITM:                                     // .exitm --- exit macro
297                 case MN_ENDM:                                      // .endm --- same as .exitm
298                         if (!disabled)
299                         {
300                                 if (label != NULL)
301                                         warn(lab_ignored);
302
303                                 exitmac();
304                         }
305
306                         goto loop;
307                 case MN_REPT:
308                         if (!disabled)
309                         {
310                                 if (label != NULL)
311                                         warn(lab_ignored);
312
313                                 defrept();
314                         }
315
316                         goto loop;
317                 case MN_ENDR:
318                         if (!disabled)
319                                 error("mis-nested .endr");
320
321                         goto loop;
322                 }
323         }
324
325 normal:
326         if (disabled)                                             // Conditionally disabled code
327                 goto loop;
328
329         // Do equates
330         if (equate != NULL)
331         {
332                 j = 0;                                                // Pick global or local sym enviroment
333
334                 if (*equate == '.')
335                         j = curenv;
336
337                 sy = lookup(equate, LABEL, j);
338
339                 if (sy == NULL)
340                 {
341                         sy = newsym(equate, LABEL, j);
342                         sy->sattr = 0;
343
344                         if (equtyp == DEQUALS)
345                         {
346                                 if (j)
347                                 {                                         // Can't GLOBAL a local symbol
348                                         error(locgl_error);
349                                         goto loop;
350                                 }
351
352                                 sy->sattr = GLOBAL;
353                         }
354                 }
355                 else if ((sy->sattr & DEFINED) && equtyp != SET)
356                 {
357                         if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
358                         {
359                                 sy->sattre |= ~UNDEF_EQUR; 
360                                 sy->svalue  = 0;
361                         }
362                         else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
363                         {
364                                 sy->sattre |= ~UNDEF_CC;
365                                 sy->svalue = 0;
366                         }
367                         else
368                         {
369                                 errors("multiple equate to '%s'", sy->sname);
370                                 goto loop;
371                         }
372                 }
373
374                 // Put symbol in "order of definition" list
375                 if (!(sy->sattr & SDECLLIST))
376                         sym_decl(sy);
377
378                 // Parse value to equate symbol to;
379                 // o  .equr
380                 // o  .reg
381                 // o  everything else
382                 if (equtyp == EQUREG)
383                 {
384                         if (!rgpu && !rdsp)
385                         {                               // Check that we are in a RISC section
386                                 error(".equr/.regequ must be defined in .gpu/.dsp section");
387                                 goto loop;
388                         }
389
390                         if ((*tok >= KW_R0) && (*tok <= KW_R31))
391                         {          // Check for register to equate to
392                                 sy->sattre  = EQUATEDREG | RISCSYM;             // Mark as equated register
393                                 riscreg = (*tok - KW_R0);
394                                 sy->sattre |= (riscreg << 8);                   // Store register number
395
396                                 if ((tok[1] == ',') && (tok[2] == CONST))
397                                 {
398                                         tok += 3;
399
400                                         if (*tok == 0)
401                                                 registerbank = BANK_0;
402                                         else if (*tok == 1)
403                                                 registerbank = BANK_1;
404                                         else
405                                                 registerbank = BANK_N;
406                                 }
407                                 else
408                                 {
409                                         registerbank = BANK_N;
410                                 }
411
412                                 sy->sattre |= regbank;                          // Store register bank
413                                 eattr = ABS | DEFINED | GLOBAL;
414                                 eval = 0x80000080 + (riscreg) + (registerbank << 8);
415                                 tok++;
416                         }
417                         else if (tok[0] == SYMBOL)
418                         {                      // Checking for a register symbol
419                                 sy2 = lookup((char *)tok[1], LABEL, j);
420
421                                 if (!sy2 || !(sy2->sattre & EQUATEDREG))
422                                 {       // Make sure symbol is a valid equreg
423                                         error("invalid GPU/DSP .equr/.regequ definition");
424                                         goto loop;
425                                 }
426                                 else
427                                 {
428                                         eattr = ABS | DEFINED | GLOBAL;              // Copy symbols attributes
429                                         sy->sattre = sy2->sattre;
430                                         eval = (sy2->svalue & 0xFFFFF0FF);
431                                         tok += 2;
432                                 }
433                         }
434                         else
435                         {
436                                 error("invalid GPU/DSP .equr/.regequ definition");
437                                 goto loop;
438                         }
439                 }
440                 else if (equtyp == REG)
441                 {
442                         if (reglist(&rmask) < 0)
443                                 goto loop;
444
445                         eval = (VALUE)rmask;
446                         eattr = ABS | DEFINED;
447                 }
448                 else if (equtyp == CCDEF)
449                 {
450                         sy->sattre |= EQUATEDCC;
451                         eattr = ABS | DEFINED | GLOBAL;
452
453                         if (tok[0] == SYMBOL)
454                         {
455                                 sy2 = lookup((char *)tok[1], LABEL, j);
456
457                                 if (!sy2 || !(sy2->sattre & EQUATEDCC))
458                                 {
459                                         error("invalid gpu/dsp .ccdef definition");
460                                         goto loop;
461                                 }
462                                 else
463                                 {
464                                         eattr = ABS | DEFINED | GLOBAL;
465                                         sy->sattre = sy2->sattre;
466                                         eval = sy2->svalue;
467                                         tok += 2;
468                                 }
469                         }
470                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
471                                 goto loop;
472                 }
473                 else if (*tok == SYMBOL)
474                 {  //equ a equr
475                         sy2 = lookup((char *)tok[1], LABEL, j);
476
477                         if (sy2 && (sy2->sattre & EQUATEDREG))
478                         {
479                                 sy->stype = sy2->stype;
480                                 sy->sattr = sy2->sattr;
481                                 sy->sattre = sy2->sattre;
482                                 sy->svalue = (sy2->svalue & 0xFFFFF0FF);
483                                 goto loop;
484                         }
485                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
486                                 goto loop;
487                 }
488                 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
489                         goto loop;
490
491                 if (!(eattr & DEFINED))
492                 {
493                         error(undef_error);
494                         goto loop;
495                 }
496
497                 
498                 sy->sattr |= eattr | EQUATED;                         // Symbol inherits value and attributes
499                 sy->svalue = eval;
500                 if (list_flag)                                         // Put value in listing
501                         listvalue(eval);
502
503                 at_eol();                                             // Must be at EOL now
504                 goto loop;
505         }
506
507         // Do labels
508         if (label != NULL)
509         {
510 do_label:
511                 j = 0;
512
513                 if (*label == '.')
514                         j = curenv;
515
516                 sy = lookup(label, LABEL, j);
517
518                 if (sy == NULL)
519                 {
520                         sy = newsym(label, LABEL, j);
521                         sy->sattr = 0;
522                         sy->sattre = RISCSYM;
523                 }
524                 else if (sy->sattr & DEFINED)
525                 {
526                         errors("multiply-defined label '%s'", label);
527                         goto loop;
528                 }
529
530                 // Put symbol in "order of definition" list
531                 if (!(sy->sattr & SDECLLIST))
532                         sym_decl(sy);
533
534                 if (orgactive)
535                 {
536                         sy->svalue = orgaddr;
537                         sy->sattr |= ABS | DEFINED | EQUATED;
538                 }
539                 else
540                 {
541                         sy->svalue = sloc;
542                         sy->sattr |= DEFINED | cursect;
543                 }
544
545                 lab_sym = sy;
546
547                 if (!j)
548                         ++curenv;
549
550                 if (labtyp == DCOLON)
551                 {                                // Make label global
552                         if (j)
553                         {
554                                 error(locgl_error);
555                                 goto loop;
556                         }
557
558                         sy->sattr |= GLOBAL;
559                 }
560
561                 // If we're in as68 mode, and there's another label, go back and handle it
562                 if (as68_flag && as68mode)
563                         goto as68label;
564         }
565
566         // Punt on EOL
567         if (state == -3)
568                 goto loop;
569
570         // If we are in GPU or DSP mode and still in need of a mnemonic then search for one
571         if ((rgpu || rdsp) && (state < 0 || state >= 1000))
572         {
573                 for(state=0, p=opname; state>=0;)
574                 {
575                         j = mrbase[state] + (int)tolowertab[*p];
576
577                         if (mrcheck[j] != state)
578                         {                          // Reject, character doesn't match
579                                 state = -1;                                     // No match
580                                 break;
581                         }
582
583                         if (!*++p)
584                         {                                        // Must accept or reject at EOS
585                                 state = mraccept[j];                            // (-1 on no terminal match)
586                                 break;
587                         }
588
589                         state = mrtab[j];
590                 }
591
592                 // Call RISC code generator if we found a mnemonic
593                 if (state >= 3000)
594                 {
595                         risccg(state);
596                         goto loop;
597                 }
598         }
599
600         // Invoke macro or complain about bad mnemonic
601         if (state < 0)
602         {
603                 if ((sy = lookup(opname, MACRO, 0)) != NULL) 
604                         invokemac(sy, siz);
605                 else
606                         errors("unknown op '%s'", opname);
607
608                 goto loop;
609         }
610
611         // Call directive handlers
612         if (state < 500)
613         {
614                 (*dirtab[state])(siz);
615                 goto loop;
616         }
617
618         // Do mnemonics
619         // o  can't deposit instrs in BSS or ABS
620         // o  do automatic .EVEN for instrs
621         // o  allocate space for largest possible instr
622         // o  can't do ".b" operations with an address register
623         if (scattr & SBSS)
624         {
625                 error("cannot initialize non-storage (BSS) section");
626                 goto loop;
627         }
628
629         if (sloc & 1)                                             // Automatic .even
630                 auto_even();
631
632         if (challoc - ch_size < 18)                               // Make sure have space in current chunk
633                 chcheck(0L);
634
635         m = &machtab[state - 1000];
636
637         if (m->mnattr & CGSPECIAL)
638         {                              // Call special-mode handler
639                 (*m->mnfunc)(m->mninst, siz);
640                 goto loop;
641         }
642
643         if (amode(1) < 0)                                         // Parse 0, 1 or 2 addr modes
644                 goto loop;
645
646         if (*tok != EOL)
647                 error(extra_stuff);
648
649         amsk0 = amsktab[am0];
650         amsk1 = amsktab[am1];
651
652         // Catch attempts to use ".B" with an address register (yes, this check does work at this level)
653         if (siz == SIZB && (am0 == AREG || am1 == AREG))
654         {
655                 error("cannot use '.b' with an address register");
656                 goto loop;
657         }
658
659         for(;;)
660         {
661                 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
662                 {
663                         (*m->mnfunc)(m->mninst, siz);
664                         goto loop;
665                 }
666
667                 m = &machtab[m->mncont];
668         }
669 }
670
671
672 // 
673 // .if, Start Conditional Assembly
674 //
675 int d_if (void)
676 {
677         IFENT * rif;
678         WORD eattr;
679         VALUE eval;
680         SYM * esym;
681
682         // Alloc an IFENTRY
683         if ((rif = f_ifent) == NULL)
684                 rif = (IFENT *)amem((LONG)sizeof(IFENT));
685         else
686                 f_ifent = rif->if_prev;
687
688         rif->if_prev = ifent;
689         ifent = rif;
690
691         if (!disabled)
692         {
693                 if (expr(exprbuf, &eval, &eattr, &esym) != OK) return 0;
694
695                 if ((eattr & DEFINED) == 0)
696                         return error(undef_error);
697
698                 disabled = !eval;
699         }
700
701         rif->if_state = (WORD)disabled;
702         return 0;
703 }
704
705
706 // 
707 // .else, Do Alternate Case For .if
708 //
709 int d_else(void)
710 {
711         IFENT * rif = ifent;
712
713         if (rif->if_prev == NULL)
714                 return error("mismatched .else");
715
716         if (disabled)
717                 disabled = rif->if_prev->if_state;
718         else
719                 disabled = 1;
720
721         rif->if_state = (WORD)disabled;
722         return 0;
723 }
724
725
726 //
727 // .endif, End of conditional assembly block
728 // This is also called by fpop() to pop levels of IFENTs in case a macro or
729 // include file exits early with `exitm' or `end'.
730 //
731 int d_endif (void)
732 {
733         IFENT * rif = ifent;
734
735         if (rif->if_prev == NULL)
736                 return error("mismatched .endif");
737
738         ifent = rif->if_prev;
739         disabled = rif->if_prev->if_state;
740         rif->if_prev = f_ifent;
741         f_ifent = rif;
742         return 0;
743 }