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
21 #define DEF_KW // Declare keyword values
22 #include "kwtab.h" // Incl generated keyword tables & defs
24 #define DEF_MN // Incl 68k keyword definitions
25 #define DECL_MN // Incl 68k keyword state machine tables
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)
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";
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,
52 M_AREG, M_AREG, M_AREG, M_AREG,
53 M_AREG, M_AREG, M_AREG, M_AREG,
55 M_AIND, M_AIND, M_AIND, M_AIND,
56 M_AIND, M_AIND, M_AIND, M_AIND,
58 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
59 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
61 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
62 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
64 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
65 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
67 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
68 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
92 // Initialize Line Processor
94 void init_procln(void)
98 f_ifent = ifent0.if_prev = NULL;
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
130 listflag = 0; // Initialise listing flag
132 loop: // Line processing loop label
134 // Get another line of tokens
135 if (tokln() == TKEOF)
137 if (list_flag && listflag) // Flush last line of source
140 if (ifent->if_prev != NULL) // Check conditional token
141 error("hit EOF without finding matching .endif");
146 DEBUG DumpTokenBuffer();
150 if (listflag && listing > 0)
151 listeol(); // Tell listing generator about EOL
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
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
165 loop1: // Internal line processing loop
167 if (*tok == EOL) // Restart loop if end-of-line
170 // First token MUST be a symbol
177 j = (int)tok[2]; // Skip equates (normal statements)
179 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
181 equate = (char *)tok[1];
187 // Skip past label (but record it)
188 if (j == ':' || j == DCOLON)
191 label = (char *)tok[1]; // Get label name
192 labtyp = tok[2]; // Get label type
193 tok += 3; // Go to next line token
195 // Handle multiple labels; if there's another label, go process it,
196 // and come back at `as68label' above.
201 if (*tok == SYMBOL && tok[2] == ':')
209 if (*tok == EOL) // EOL is legal here...
212 // Next token MUST be a symbol
213 if (*tok++ != SYMBOL)
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
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;)
233 j = mnbase[state] + (int)tolowertab[*p];
235 // Reject, character doesn't match
236 if (mncheck[j] != state)
238 state = -1; // No match
242 // Must accept or reject at EOS
245 state = mnaccept[j]; // (-1 on no terminal match)
252 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
257 else if (*tok == DOTL)
259 else if (*tok == DOTB)
262 // Do special directives (500..999) (These must be handled in "real time")
263 if (state >= 500 && state < 1000)
276 case MN_IIF: // .iif --- immediate if
277 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
280 if (!(eattr & DEFINED))
296 case MN_MACRO: // .macro --- macro definition
306 case MN_EXITM: // .exitm --- exit macro
307 case MN_ENDM: // .endm --- same as .exitm
329 error("mis-nested .endr");
336 if (disabled) // Conditionally disabled code
342 j = 0; // Pick global or local sym enviroment
347 sy = lookup(equate, LABEL, j);
351 sy = newsym(equate, LABEL, j);
354 if (equtyp == DEQUALS)
356 // Can't GLOBAL a local symbol
366 else if ((sy->sattr & DEFINED) && equtyp != SET)
368 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
370 sy->sattre |= ~UNDEF_EQUR;
373 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
375 sy->sattre |= ~UNDEF_CC;
380 errors("multiple equate to '%s'", sy->sname);
385 // Put symbol in "order of definition" list
386 if (!(sy->sattr & SDECLLIST))
389 // Parse value to equate symbol to;
393 if (equtyp == EQUREG)
395 // Check that we are in a RISC section
398 error(".equr/.regequ must be defined in .gpu/.dsp section");
402 // Check for register to equate to
403 if ((*tok >= KW_R0) && (*tok <= KW_R31))
405 sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
406 riscreg = (*tok - KW_R0);
407 sy->sattre |= (riscreg << 8); // Store register number
409 if ((tok[1] == ',') && (tok[2] == CONST))
414 registerbank = BANK_0;
416 registerbank = BANK_1;
418 registerbank = BANK_N;
422 registerbank = BANK_N;
425 sy->sattre |= regbank; // Store register bank
426 eattr = ABS | DEFINED | GLOBAL;
427 eval = 0x80000080 + (riscreg) + (registerbank << 8);
430 // Checking for a register symbol
431 else if (tok[0] == SYMBOL)
433 sy2 = lookup((char *)tok[1], LABEL, j);
435 // Make sure symbol is a valid equreg
436 if (!sy2 || !(sy2->sattre & EQUATEDREG))
438 error("invalid GPU/DSP .equr/.regequ definition");
443 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
444 sy->sattre = sy2->sattre;
445 eval = (sy2->svalue & 0xFFFFF0FF);
451 error("invalid GPU/DSP .equr/.regequ definition");
455 else if (equtyp == REG)
457 if (reglist(&rmask) < 0)
461 eattr = ABS | DEFINED;
463 else if (equtyp == CCDEF)
465 sy->sattre |= EQUATEDCC;
466 eattr = ABS | DEFINED | GLOBAL;
468 if (tok[0] == SYMBOL)
470 sy2 = lookup((char *)tok[1], LABEL, j);
472 if (!sy2 || !(sy2->sattre & EQUATEDCC))
474 error("invalid gpu/dsp .ccdef definition");
479 eattr = ABS | DEFINED | GLOBAL;
480 sy->sattre = sy2->sattre;
485 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
489 else if (*tok == SYMBOL)
491 sy2 = lookup((char *)tok[1], LABEL, j);
493 if (sy2 && (sy2->sattre & EQUATEDREG))
495 sy->stype = sy2->stype;
496 sy->sattr = sy2->sattr;
497 sy->sattre = sy2->sattre;
498 sy->svalue = (sy2->svalue & 0xFFFFF0FF);
501 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
504 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
507 if (!(eattr & DEFINED))
513 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
516 if (list_flag) // Put value in listing
519 at_eol(); // Must be at EOL now
532 sy = lookup(label, LABEL, j);
536 sy = newsym(label, LABEL, j);
538 sy->sattre = RISCSYM;
540 else if (sy->sattr & DEFINED)
542 errors("multiply-defined label '%s'", label);
546 // Put symbol in "order of definition" list
547 if (!(sy->sattr & SDECLLIST))
552 sy->svalue = orgaddr;
553 sy->sattr |= ABS | DEFINED | EQUATED;
558 sy->sattr |= DEFINED | cursect;
567 if (labtyp == DCOLON)
578 // If we're in as68 mode, and there's another label, go back and handle it
579 if (as68_flag && as68mode)
587 // If we are in GPU or DSP mode and still in need of a mnemonic then search
589 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
591 for(state=0, p=opname; state>=0;)
593 j = mrbase[state] + (int)tolowertab[*p];
595 // Reject, character doesn't match
596 if (mrcheck[j] != state)
598 state = -1; // No match
602 // Must accept or reject at EOS
605 state = mraccept[j]; // (-1 on no terminal match)
612 // Call RISC code generator if we found a mnemonic
620 // Invoke macro or complain about bad mnemonic
623 if ((sy = lookup(opname, MACRO, 0)) != NULL)
626 errors("unknown op '%s'", opname);
631 // Call directive handlers
634 (*dirtab[state])(siz);
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
645 error("cannot initialize non-storage (BSS) section");
649 if (sloc & 1) // Automatic .even
652 if (challoc - ch_size < 18) // Make sure have space in current chunk
655 m = &machtab[state - 1000];
657 // Call special-mode handler
658 if (m->mnattr & CGSPECIAL)
660 (*m->mnfunc)(m->mninst, siz);
664 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
670 amsk0 = amsktab[am0];
671 amsk1 = amsktab[am1];
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))
677 error("cannot use '.b' with an address register");
683 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
685 (*m->mnfunc)(m->mninst, siz);
689 m = &machtab[m->mncont];
695 // .if, Start Conditional Assembly
705 if ((rif = f_ifent) == NULL)
706 // rif = (IFENT *)amem((LONG)sizeof(IFENT));
707 rif = (IFENT *)malloc(sizeof(IFENT));
709 f_ifent = rif->if_prev;
711 rif->if_prev = ifent;
716 if (expr(exprbuf, &eval, &eattr, &esym) != OK) return 0;
718 if ((eattr & DEFINED) == 0)
719 return error(undef_error);
724 rif->if_state = (WORD)disabled;
730 // .else, Do Alternate Case For .if
736 if (rif->if_prev == NULL)
737 return error("mismatched .else");
740 disabled = rif->if_prev->if_state;
744 rif->if_state = (WORD)disabled;
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'.
758 if (rif->if_prev == NULL)
759 return error("mismatched .endif");
761 ifent = rif->if_prev;
762 disabled = rif->if_prev->if_state;
763 rif->if_prev = f_ifent;