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