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