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