]> Shamusworld >> Repos - rmac/blob - procln.c
Fixed compiler not checking for INCBIN in BSS sections.
[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                                 ExitMacro();
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                 // Check for dot in front of label; means this is a local label if present
537 #if 0
538                 j = 0;
539
540                 if (*label == '.')
541                         j = curenv;
542 #else
543                 j = (*label == '.' ? curenv : 0);
544 #endif
545
546                 sy = lookup(label, LABEL, j);
547
548                 if (sy == NULL)
549                 {
550                         sy = NewSymbol(label, LABEL, j);
551                         sy->sattr = 0;
552                         sy->sattre = RISCSYM;
553                 }
554                 else if (sy->sattr & DEFINED)
555                 {
556                         errors("multiply-defined label '%s'", label);
557                         goto loop;
558                 }
559
560                 // Put symbol in "order of definition" list
561                 if (!(sy->sattr & SDECLLIST))
562                         sym_decl(sy);
563
564                 if (orgactive)
565                 {
566                         sy->svalue = orgaddr;
567                         sy->sattr |= ABS | DEFINED | EQUATED;
568                 }
569                 else
570                 {
571                         sy->svalue = sloc;
572                         sy->sattr |= DEFINED | cursect;
573                 }
574
575                 lab_sym = sy;
576
577                 if (!j)
578                         curenv++;
579
580                 // Make label global
581                 if (labtyp == DCOLON)
582                 {
583                         if (j)
584                         {
585                                 error(locgl_error);
586                                 goto loop;
587                         }
588
589                         sy->sattr |= GLOBAL;
590                 }
591
592                 // If we're in as68 mode, and there's another label, go back and handle it
593                 if (as68_flag && as68mode)
594                         goto as68label;
595         }
596
597         // Punt on EOL
598         if (state == -3)
599                 goto loop;
600
601         // If we are in GPU or DSP mode and still in need of a mnemonic then search
602         // for one
603         if ((rgpu || rdsp) && (state < 0 || state >= 1000))
604         {
605                 for(state=0, p=opname; state>=0;)
606                 {
607                         j = mrbase[state] + (int)tolowertab[*p];
608
609                         // Reject, character doesn't match
610                         if (mrcheck[j] != state)
611                         {
612                                 state = -1;                                     // No match
613                                 break;
614                         }
615
616                         // Must accept or reject at EOS
617                         if (!*++p)
618                         {
619                                 state = mraccept[j];            // (-1 on no terminal match)
620                                 break;
621                         }
622
623                         state = mrtab[j];
624                 }
625
626                 // Call RISC code generator if we found a mnemonic
627                 if (state >= 3000)
628                 {
629                         risccg(state);
630                         goto loop;
631                 }
632         }
633
634         // Invoke macro or complain about bad mnemonic
635         if (state < 0)
636         {
637                 if ((sy = lookup(opname, MACRO, 0)) != NULL) 
638                         InvokeMacro(sy, siz);
639                 else
640                         errors("unknown op '%s'", opname);
641
642                 goto loop;
643         }
644
645         // Call directive handlers
646         if (state < 500)
647         {
648                 (*dirtab[state])(siz);
649                 goto loop;
650         }
651
652         // Do mnemonics
653         // o  can't deposit instrs in BSS or ABS
654         // o  do automatic .EVEN for instrs
655         // o  allocate space for largest possible instr
656         // o  can't do ".b" operations with an address register
657         if (scattr & SBSS)
658         {
659                 error("cannot initialize non-storage (BSS) section");
660                 goto loop;
661         }
662
663         if (sloc & 1)                                                   // Automatic .even
664                 auto_even();
665
666         if (challoc - ch_size < 18)                             // Make sure have space in current chunk
667                 chcheck(0);
668
669         m = &machtab[state - 1000];
670
671         // Call special-mode handler
672         if (m->mnattr & CGSPECIAL)
673         {
674                 (*m->mnfunc)(m->mninst, siz);
675                 goto loop;
676         }
677
678         if (amode(1) < 0)                                               // Parse 0, 1 or 2 addr modes
679                 goto loop;
680
681         if (*tok != EOL)
682                 error(extra_stuff);
683
684         amsk0 = amsktab[am0];
685         amsk1 = amsktab[am1];
686
687         // Catch attempts to use ".B" with an address register (yes, this check
688         // does work at this level)
689         if (siz == SIZB && (am0 == AREG || am1 == AREG))
690         {
691                 error("cannot use '.b' with an address register");
692                 goto loop;
693         }
694
695         for(;;)
696         {
697                 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
698                 {
699                         (*m->mnfunc)(m->mninst, siz);
700                         goto loop;
701                 }
702
703                 m = &machtab[m->mncont];
704         }
705 }
706
707
708 // 
709 // .if, Start Conditional Assembly
710 //
711 int d_if(void)
712 {
713         IFENT * rif;
714         WORD eattr;
715         VALUE eval;
716         SYM * esym;
717
718         // Alloc an IFENTRY
719         if ((rif = f_ifent) == NULL)
720                 rif = (IFENT *)malloc(sizeof(IFENT));
721         else
722                 f_ifent = rif->if_prev;
723
724         rif->if_prev = ifent;
725         ifent = rif;
726
727         if (!disabled)
728         {
729                 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
730                         return 0;
731
732                 if ((eattr & DEFINED) == 0)
733                         return error(undef_error);
734
735                 disabled = !eval;
736         }
737
738         rif->if_state = (WORD)disabled;
739         return 0;
740 }
741
742
743 // 
744 // .else, Do Alternate Case For .if
745 //
746 int d_else(void)
747 {
748         IFENT * rif = ifent;
749
750         if (rif->if_prev == NULL)
751                 return error("mismatched .else");
752
753         if (disabled)
754                 disabled = rif->if_prev->if_state;
755         else
756                 disabled = 1;
757
758         rif->if_state = (WORD)disabled;
759         return 0;
760 }
761
762
763 //
764 // .endif, End of conditional assembly block
765 // This is also called by fpop() to pop levels of IFENTs in case a macro or
766 // include file exits early with `exitm' or `end'.
767 //
768 int d_endif (void)
769 {
770         IFENT * rif = ifent;
771
772         if (rif->if_prev == NULL)
773                 return error("mismatched .endif");
774
775         ifent = rif->if_prev;
776         disabled = rif->if_prev->if_state;
777         rif->if_prev = f_ifent;
778         f_ifent = rif;
779         return 0;
780 }