2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // PROCLN.C - Line Processing
4 // Copyright (C) 199x Landon Dyer, 2011-2018 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
22 #define DEF_KW // Declare keyword values
23 #include "kwtab.h" // Incl generated keyword tables & defs
25 #define DEF_MN // Incl 68k keyword definitions
26 #define DECL_MN // Incl 68k keyword state machine tables
33 #define DEF_MP // Include 6502 keyword definitions
34 #define DECL_MP // Include 6502 keyword state machine tables
37 IFENT * ifent; // Current ifent
38 static IFENT ifent0; // Root ifent
39 IFENT * f_ifent; // Freelist of ifents
40 int disabled; // Assembly conditionally disabled
41 int just_bss; // 1, ds.b in microprocessor mode
42 uint32_t pcloc; // Value of "PC" at beginning of line
43 SYM * lab_sym; // Label on line (or NULL)
45 const char extra_stuff[] = "extra (unexpected) text found after addressing mode";
46 const char comma_error[] = "missing comma";
47 const char syntax_error[] = "syntax error";
48 const char locgl_error[] = "cannot GLOBL local symbol";
49 const char lab_ignored[] = "label ignored";
51 // Table to convert an addressing-mode number to a bitmask.
52 LONG amsktab[0124] = {
53 M_DREG, M_DREG, M_DREG, M_DREG,
54 M_DREG, M_DREG, M_DREG, M_DREG,
56 M_AREG, M_AREG, M_AREG, M_AREG,
57 M_AREG, M_AREG, M_AREG, M_AREG,
59 M_AIND, M_AIND, M_AIND, M_AIND,
60 M_AIND, M_AIND, M_AIND, M_AIND,
62 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
63 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
65 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
66 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
68 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
69 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
71 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
72 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
105 // Function prototypes
106 int HandleLabel(char *, int);
110 // Initialize line processor
112 void InitLineProcessor(void)
116 f_ifent = ifent0.if_prev = NULL;
126 int state; // Keyword machine state (output)
127 int j; // Random int, must be fast
128 char * p; // Random char ptr, must be fast
129 TOKEN * tk; // First token in line
130 char * label; // Symbol (or NULL)
131 char * equate; // Symbol (or NULL)
132 int labtyp = 0; // Label type (':', DCOLON)
133 int equtyp = 0; // Equ type ('=', DEQUALS)
134 uint64_t eval; // Expression value
135 WORD eattr; // Expression attributes
136 SYM * esym; // External symbol involved in expr.
137 WORD siz = 0; // Size suffix to mnem/diretve/macro
138 LONG amsk0, amsk1; // Address-type masks for ea0, ea1
139 MNTAB * m; // Code generation table pointer
140 SYM * sy, * sy2; // Symbol (temp usage)
141 char * opname = NULL; // Name of dirctve/mnemonic/macro
142 int listflag; // 0: Don't call listeol()
143 WORD rmask; // Register list, for REG
144 int registerbank; // RISC register bank
145 int riscreg; // RISC register
146 listflag = 0; // Initialise listing flag
148 loop: // Line processing loop label
150 // Get another line of tokens
151 if (TokenizeLine() == TKEOF)
153 DEBUG { printf("Assemble: Found TKEOF flag...\n"); }
154 if (list_flag && listflag) // Flush last line of source
157 if (ifent->if_prev != NULL) // Check conditional token
158 error("hit EOF without finding matching .endif");
163 DEBUG { DumpTokenBuffer(); }
167 if (listflag && listing > 0)
168 listeol(); // Tell listing generator about EOL
170 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
171 listflag = 1; // OK to call `listeol' now
172 just_bss = 0; // Reset just_bss mode
175 state = -3; // No keyword (just EOL)
176 label = NULL; // No label
177 lab_sym = NULL; // No (exported) label
178 equate = NULL; // No equate
179 tk = tok; // Save first token in line
180 pcloc = (uint32_t)sloc; // Set beginning-of-line PC
182 loop1: // Internal line processing loop
184 if (*tok == EOL) // Restart loop if end-of-line
187 // First token MUST be a symbol (Shamus: not sure why :-/)
190 if ((*tok >= KW_D0) && (*tok <= KW_R31))
191 error("cannot use reserved keyword as label name or .equ");
193 error("syntax error; expected symbol");
198 j = (int)tok[2]; // Skip equates (normal statements)
200 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
202 equate = string[tok[1]];
208 // Skip past label (but record it)
209 if (j == ':' || j == DCOLON)
212 label = string[tok[1]]; // Get label name
213 labtyp = tok[2]; // Get label type
214 tok += 3; // Go to next line token
217 // Looks like another label follows the previous one, so handle
218 // the previous one until there aren't any more
219 if (as68_flag && (*tok == SYMBOL && tok[2] == ':'))
221 if (HandleLabel(label, labtyp) != 0)
228 // EOL is legal here...
232 // First token MUST be a symbol (if we get here, tok didn't advance)
233 if (*tok++ != SYMBOL)
235 error("syntax error; expected symbol");
239 opname = p = string[*tok++];
241 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
242 // On output, `state' will have one of the values:
243 // -3 there was no symbol (EOL)
244 // -2..-1 the symbol didn't match any keyword
245 // 0..499 vanilla directives (dc, ds, etc.)
246 // 500..999 electric directives (macro, rept, etc.)
247 // 1000..+ mnemonics (move, lsr, etc.)
248 for(state=0; state>=0;)
250 j = mnbase[state] + (int)tolowertab[*p];
252 // Reject, character doesn't match
253 if (mncheck[j] != state)
255 state = -1; // No match
259 // Must accept or reject at EOS
262 state = mnaccept[j]; // (-1 on no terminal match)
269 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
274 case DOTW: siz = SIZW, tok++; break;
275 case DOTL: siz = SIZL, tok++; break;
276 case DOTB: siz = SIZB, tok++; break;
277 case DOTD: siz = SIZD, tok++; break;
278 case DOTP: siz = SIZP, tok++; break;
279 case DOTQ: siz = SIZQ, tok++; break;
280 case DOTS: siz = SIZS, tok++; break;
281 case DOTX: siz = SIZX, tok++; break;
284 // Do special directives (500..999) (These must be handled in "real time")
285 if (state >= 500 && state < 1000)
301 case MN_IIF: // .iif --- immediate if
302 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
305 if (!(eattr & DEFINED))
322 case MN_MACRO: // .macro --- macro definition
325 // Label on a macro definition is bad mojo... Warn the user
334 case MN_EXITM: // .exitm --- exit macro
335 case MN_ENDM: // .endm --- same as .exitm
349 // Handle labels on REPT directive lines...
352 if (HandleLabel(label, labtyp) != 0)
363 error("mis-nested .endr");
370 if (disabled) // Conditionally disabled code
376 // Pick global or local symbol enviroment
377 j = (*equate == '.' ? curenv : 0);
378 sy = lookup(equate, LABEL, j);
382 sy = NewSymbol(equate, LABEL, j);
385 if (equtyp == DEQUALS)
387 // Can't GLOBAL a local symbol
397 else if ((sy->sattr & DEFINED) && equtyp != SET)
399 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
401 sy->sattre &= ~UNDEF_EQUR;
404 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
406 sy->sattre &= ~UNDEF_CC;
411 error("multiple equate to '%s'", sy->sname);
416 // Put symbol in "order of definition" list if it's not already there
417 AddToSymbolDeclarationList(sy);
419 // Parse value to equate symbol to;
423 if (equtyp == EQUREG)
425 //Linko's request to issue a warning on labels that equated to the same
426 //register would go here. Not sure how to implement it though. :-/
429 have an array of bools with 64 entries. Whenever a register is equated, set the
430 corresponding register bool to true. Whenever it's undef'ed, set it to false.
431 When checking to see if it's already been equated, issue a warning.
433 // Check that we are in a RISC section
436 error(".equr/.regequ must be defined in .gpu/.dsp section");
440 // Check for register to equate to
441 if ((*tok >= KW_R0) && (*tok <= KW_R31))
443 // sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
444 sy->sattre = EQUATEDREG; // Mark as equated register
445 riscreg = (*tok - KW_R0);
446 //is there any reason to do this, since we're putting this in svalue?
447 //i'm thinking, no. Let's test that out! :-D
448 // sy->sattre |= (riscreg << 8); // Store register number
449 //everything seems to build fine without it... We'll leave it here Just In Case(tm)
451 #define DEBODGE_REGBANK
452 #ifdef DEBODGE_REGBANK
453 // Default is current state of "regbank"
454 registerbank = regbank;
456 // Default is no register bank specified
457 registerbank = BANK_N;
460 // Check for ",<bank #>" override notation
461 if ((tok[1] == ',') && (tok[2] == CONST))
463 // Advance token pointer to the constant
466 // Anything other than a 0 or a 1 will result in "No Bank"
467 if (*(uint64_t *)tok == 0)
468 registerbank = BANK_0;
469 else if (*(uint64_t *)tok == 1)
470 registerbank = BANK_1;
473 #ifdef DEBODGE_REGBANK
474 sy->sattre |= registerbank; // Store register bank
476 // What needs to happen here is to prime registerbank with regbank, then use
477 // registerbank down below for the bank marking.
478 #warning "!!! regbank <-> registerbank confusion here !!!"
479 // The question here is why, if we're allowed to override the ".regbankN" rules
480 // above, then why is it using the one set by the directive in the extended
481 // attributes and not in what ends up in symbol->svalue?
482 // ".regbankN" is not an original Madmac directive, so it's suspect
483 sy->sattre |= regbank; // Store register bank
485 eattr = ABS | DEFINED | GLOBAL;
489 // Checking for a register symbol
490 else if (tok[0] == SYMBOL)
492 sy2 = lookup(string[tok[1]], LABEL, j);
494 // Make sure symbol is a valid equreg
495 if (!sy2 || !(sy2->sattre & EQUATEDREG))
497 error("invalid GPU/DSP .equr/.regequ definition");
502 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
503 sy->sattre = sy2->sattre;
504 eval = (sy2->svalue & 0xFFFFF0FF);
510 error("invalid GPU/DSP .equr/.regequ definition");
514 else if (equtyp == REG)
516 if (reglist(&rmask) < 0)
519 eval = (uint32_t)rmask;
520 eattr = ABS | DEFINED;
522 else if (equtyp == CCDEF)
524 sy->sattre |= EQUATEDCC;
525 eattr = ABS | DEFINED | GLOBAL;
527 if (tok[0] == SYMBOL)
529 sy2 = lookup(string[tok[1]], LABEL, j);
531 if (!sy2 || !(sy2->sattre & EQUATEDCC))
533 error("invalid gpu/dsp .ccdef definition");
538 eattr = ABS | DEFINED | GLOBAL;
539 sy->sattre = sy2->sattre;
544 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
548 else if (*tok == SYMBOL)
550 sy2 = lookup(string[tok[1]], LABEL, j);
552 if (sy2 && (sy2->sattre & EQUATEDREG))
554 sy->stype = sy2->stype;
555 sy->sattr = sy2->sattr;
556 sy->sattre = sy2->sattre;
557 sy->svalue = sy2->svalue;
560 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
563 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
566 if (!(eattr & DEFINED))
572 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
575 if (list_flag) // Put value in listing
576 listvalue((uint32_t)eval);
578 at_eol(); // Must be at EOL now
585 // Non-zero == error occurred
586 if (HandleLabel(label, labtyp) != 0)
594 // If we're in 6502 mode and are still in need of a mnemonic, then search
595 // for valid 6502 mnemonic.
596 if (m6502 && (state < 0 || state >= 1000))
599 state = kmatch(opname, mpbase, mpcheck, mptab, mpaccept);
601 for(state=0, p=opname; state>= 0; )
603 j = mpbase[state] + tolowertab[*p];
605 if (mpcheck[j] != state) // Reject, character doesn't match
607 state = -1; // No match
612 { // Must accept or reject at EOS
613 state = mpaccept[j]; // (-1 on no terminal match)
621 // Call 6502 code generator if we found a mnemonic
624 m6502cg(state - 2000);
629 // If we are in GPU or DSP mode and still in need of a mnemonic then search
631 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
633 for(state=0, p=opname; state>=0;)
635 j = mrbase[state] + (int)tolowertab[*p];
637 // Reject, character doesn't match
638 if (mrcheck[j] != state)
640 state = -1; // No match
644 // Must accept or reject at EOS
647 state = mraccept[j]; // (-1 on no terminal match)
654 // Call RISC code generator if we found a mnemonic
657 GenerateRISCCode(state);
662 // Invoke macro or complain about bad mnemonic
665 if ((sy = lookup(opname, MACRO, 0)) != NULL)
666 InvokeMacro(sy, siz);
668 error("unknown op '%s'", opname);
673 // Call directive handlers
676 (*dirtab[state])(siz);
681 // o can't deposit instrs in BSS or ABS
682 // o do automatic .EVEN for instrs
683 // o allocate space for largest possible instr
684 // o can't do ".b" operations with an address register
687 error("cannot initialize non-storage (BSS) section");
691 if (sloc & 1) // Automatic .even
694 if (challoc - ch_size < 18) // Make sure have space in current chunk
697 m = &machtab[state - 1000];
699 // Call special-mode handler
700 if (m->mnattr & CGSPECIAL)
702 (*m->mnfunc)(m->mninst, siz);
706 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
712 amsk0 = amsktab[am0];
713 amsk1 = amsktab[am1];
715 // Catch attempts to use ".B" with an address register (yes, this check
716 // does work at this level)
717 if (siz == SIZB && (am0 == AREG || am1 == AREG))
719 error("cannot use '.b' with an address register");
723 // Keep a backup of chptr (used for optimisations during codegen)
724 chptr_opcode = chptr;
728 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
730 (*m->mnfunc)(m->mninst, siz);
734 m = &machtab[m->mncont];
740 // Handle the creation of labels
742 int HandleLabel(char * label, int labelType)
744 // Check for dot in front of label; means this is a local label if present
745 int environment = (*label == '.' ? curenv : 0);
746 SYM * symbol = lookup(label, LABEL, environment);
750 symbol = NewSymbol(label, LABEL, environment);
754 else if (symbol->sattr & DEFINED)
755 return error("multiply-defined label '%s'", label);
757 // Put symbol in "order of definition" list if it's not already in it
758 AddToSymbolDeclarationList(symbol);
762 symbol->svalue = orgaddr;
763 symbol->sattr |= ABS | DEFINED | EQUATED;
767 symbol->svalue = sloc;
768 symbol->sattr |= DEFINED | cursect;
773 // Yes, our CS professors told us to write checks for equality this way,
774 // but damn, it hurts my brain every time I look at it. :-/
775 if (0 == environment)
778 // Make label global if it has a double colon
779 if (labelType == DCOLON)
781 if (environment != 0)
782 return error(locgl_error);
784 symbol->sattr |= GLOBAL;