2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // PROCLN.C - Line Processing
4 // Copyright (C) 199x Landon Dyer, 2011-2019 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
13 #include "dsp56k_amode.h"
14 #include "dsp56k_mach.h"
25 #define DEF_KW // Declare keyword values
26 #include "kwtab.h" // Incl generated keyword tables & defs
28 #define DEF_MN // Incl 68k keyword definitions
29 #define DECL_MN // Incl 68k keyword state machine tables
36 #define DEF_MP // Include 6502 keyword definitions
37 #define DECL_MP // Include 6502 keyword state machine tables
40 #define DEF_MO // Include OP keyword definitions
41 #define DECL_MO // Include OP keyword state machine tables
44 #define DEF_DSP // Include DSP56K keywords definitions
45 #define DECL_DSP // Include DSP56K keyword state machine tables
49 IFENT * ifent; // Current ifent
50 static IFENT ifent0; // Root ifent
51 IFENT * f_ifent; // Freelist of ifents
52 int disabled; // Assembly conditionally disabled
53 int just_bss; // 1, ds.b in microprocessor mode
54 uint32_t pcloc; // Value of "PC" at beginning of line
55 SYM * lab_sym; // Label on line (or NULL)
56 char * label_defined; // The name of the last label defined in current line (if any)
58 const char extra_stuff[] = "extra (unexpected) text found after addressing mode";
59 const char comma_error[] = "missing comma";
60 const char syntax_error[] = "syntax error";
61 const char locgl_error[] = "cannot GLOBL local symbol";
62 const char lab_ignored[] = "label ignored";
64 // Table to convert an addressing-mode number to a bitmask.
65 LONG amsktab[0124] = {
66 M_DREG, M_DREG, M_DREG, M_DREG,
67 M_DREG, M_DREG, M_DREG, M_DREG,
69 M_AREG, M_AREG, M_AREG, M_AREG,
70 M_AREG, M_AREG, M_AREG, M_AREG,
72 M_AIND, M_AIND, M_AIND, M_AIND,
73 M_AIND, M_AIND, M_AIND, M_AIND,
75 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
76 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
78 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
79 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
81 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
82 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
84 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
85 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
118 // Function prototypes
119 int HandleLabel(char *, int);
123 // Initialize line processor
125 void InitLineProcessor(void)
129 f_ifent = ifent0.if_prev = NULL;
139 int state; // Keyword machine state (output)
140 int j; // Random int, must be fast
141 char * p; // Random char ptr, must be fast
142 TOKEN * tk; // First token in line
143 char * label; // Symbol (or NULL)
144 char * equate; // Symbol (or NULL)
145 int labtyp = 0; // Label type (':', DCOLON)
146 int equtyp = 0; // Equ type ('=', DEQUALS)
147 uint64_t eval; // Expression value
148 WORD eattr; // Expression attributes
149 SYM * esym; // External symbol involved in expr.
150 WORD siz = 0; // Size suffix to mnem/diretve/macro
151 LONG amsk0, amsk1; // Address-type masks for ea0, ea1
152 MNTAB * m; // Code generation table pointer
153 SYM * sy, * sy2; // Symbol (temp usage)
154 char * opname = NULL; // Name of dirctve/mnemonic/macro
155 int listflag; // 0: Don't call listeol()
156 WORD rmask; // Register list, for REG
157 int registerbank; // RISC register bank
158 int riscreg; // RISC register
159 listflag = 0; // Initialise listing flag
161 loop: // Line processing loop label
163 // Get another line of tokens
164 if (TokenizeLine() == TKEOF)
166 DEBUG { printf("Assemble: Found TKEOF flag...\n"); }
167 if (list_flag && listflag) // Flush last line of source
170 if (ifent->if_prev != NULL) // Check conditional token
171 error("hit EOF without finding matching .endif");
176 DEBUG { DumpTokenBuffer(); }
180 if (listflag && listing > 0)
181 listeol(); // Tell listing generator about EOL
183 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
184 listflag = 1; // OK to call `listeol' now
185 just_bss = 0; // Reset just_bss mode
188 state = -3; // No keyword (just EOL)
189 label = NULL; // No label
190 label_defined = NULL; // No label defined yet
191 lab_sym = NULL; // No (exported) label
192 equate = NULL; // No equate
193 tk = tok; // Save first token in line
194 pcloc = (uint32_t)sloc; // Set beginning-of-line PC
196 loop1: // Internal line processing loop
198 if (*tok == EOL) // Restart loop if end-of-line
201 // First token MUST be a symbol (Shamus: not sure why :-/)
204 if ((*tok >= KW_D0) && (*tok <= KW_R31))
205 error("cannot use reserved keyword as label name or .equ");
207 error("syntax error; expected symbol");
212 j = (int)tok[2]; // Skip equates (normal statements)
214 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
216 equate = string[tok[1]];
222 // Skip past label (but record it)
223 if (j == ':' || j == DCOLON)
226 label = string[tok[1]]; // Get label name
227 labtyp = tok[2]; // Get label type
228 tok += 3; // Go to next line token
231 // Looks like another label follows the previous one, so handle
232 // the previous one until there aren't any more
233 if (as68_flag && (*tok == SYMBOL && tok[2] == ':'))
235 if (HandleLabel(label, labtyp) != 0)
238 label_defined = label;
244 // EOL is legal here...
248 // First token MUST be a symbol (if we get here, tok didn't advance)
249 if (*tok++ != SYMBOL)
251 error("syntax error; expected symbol");
255 opname = p = string[*tok++];
257 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
258 // On output, `state' will have one of the values:
259 // -3 there was no symbol (EOL)
260 // -2..-1 the symbol didn't match any keyword
261 // 0..499 vanilla directives (dc, ds, etc.)
262 // 500..999 electric directives (macro, rept, etc.)
263 // 1000..+ mnemonics (move, lsr, etc.)
264 for(state=0; state>=0;)
266 j = mnbase[state] + (int)tolowertab[*p];
268 // Reject, character doesn't match
269 if (mncheck[j] != state)
271 state = -1; // No match
275 // Must accept or reject at EOS
278 state = mnaccept[j]; // (-1 on no terminal match)
285 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
290 case DOTW: siz = SIZW, tok++; break;
291 case DOTL: siz = SIZL, tok++; break;
292 case DOTB: siz = SIZB, tok++; break;
293 case DOTD: siz = SIZD, tok++; break;
294 case DOTP: siz = SIZP, tok++; break;
295 case DOTQ: siz = SIZQ, tok++; break;
296 case DOTS: siz = SIZS, tok++; break;
297 case DOTX: siz = SIZX, tok++; break;
300 // Do special directives (500..999) (These must be handled in "real time")
301 if (state >= 500 && state < 1000)
317 case MN_IIF: // .iif --- immediate if
318 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
321 if (!(eattr & DEFINED))
338 case MN_MACRO: // .macro --- macro definition
341 // Label on a macro definition is bad mojo... Warn the user
350 case MN_EXITM: // .exitm --- exit macro
351 case MN_ENDM: // .endm --- same as .exitm
365 // Handle labels on REPT directive lines...
368 if (HandleLabel(label, labtyp) != 0)
371 label_defined = label;
381 error("mis-nested .endr");
388 if (disabled) // Conditionally disabled code
394 // Pick global or local symbol enviroment
395 j = (*equate == '.' ? curenv : 0);
396 sy = lookup(equate, LABEL, j);
400 sy = NewSymbol(equate, LABEL, j);
403 if (equtyp == DEQUALS)
405 // Can't GLOBAL a local symbol
415 else if ((sy->sattr & DEFINED) && equtyp != SET)
417 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
419 sy->sattre &= ~UNDEF_EQUR;
422 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
424 sy->sattre &= ~UNDEF_CC;
429 error("multiple equate to '%s'", sy->sname);
434 // Put symbol in "order of definition" list if it's not already there
435 AddToSymbolDeclarationList(sy);
437 // Parse value to equate symbol to;
441 if (equtyp == EQUREG)
443 //Linko's request to issue a warning on labels that equated to the same
444 //register would go here. Not sure how to implement it though. :-/
447 have an array of bools with 64 entries. Whenever a register is equated, set the
448 corresponding register bool to true. Whenever it's undef'ed, set it to false.
449 When checking to see if it's already been equated, issue a warning.
451 // Check that we are in a RISC section
454 error(".equr/.regequ must be defined in .gpu/.dsp section");
458 // Check for register to equate to
459 if ((*tok >= KW_R0) && (*tok <= KW_R31))
461 // sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
462 sy->sattre = EQUATEDREG; // Mark as equated register
463 riscreg = (*tok - KW_R0);
464 //is there any reason to do this, since we're putting this in svalue?
465 //i'm thinking, no. Let's test that out! :-D
466 // sy->sattre |= (riscreg << 8); // Store register number
467 //everything seems to build fine without it... We'll leave it here Just In Case(tm)
469 #define DEBODGE_REGBANK
470 #ifdef DEBODGE_REGBANK
471 // Default is current state of "regbank"
472 registerbank = regbank;
474 // Default is no register bank specified
475 registerbank = BANK_N;
478 // Check for ",<bank #>" override notation
479 if ((tok[1] == ',') && (tok[2] == CONST))
481 // Advance token pointer to the constant
484 // Anything other than a 0 or a 1 will result in "No Bank"
485 if (*(uint64_t *)tok == 0)
486 registerbank = BANK_0;
487 else if (*(uint64_t *)tok == 1)
488 registerbank = BANK_1;
491 #ifdef DEBODGE_REGBANK
492 sy->sattre |= registerbank; // Store register bank
494 // What needs to happen here is to prime registerbank with regbank, then use
495 // registerbank down below for the bank marking.
496 #warning "!!! regbank <-> registerbank confusion here !!!"
497 // The question here is why, if we're allowed to override the ".regbankN" rules
498 // above, then why is it using the one set by the directive in the extended
499 // attributes and not in what ends up in symbol->svalue?
500 // ".regbankN" is not an original Madmac directive, so it's suspect
501 sy->sattre |= regbank; // Store register bank
503 eattr = ABS | DEFINED | GLOBAL;
507 // Checking for a register symbol
508 else if (tok[0] == SYMBOL)
510 sy2 = lookup(string[tok[1]], LABEL, j);
512 // Make sure symbol is a valid equreg
513 if (!sy2 || !(sy2->sattre & EQUATEDREG))
515 error("invalid GPU/DSP .equr/.regequ definition");
520 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
521 sy->sattre = sy2->sattre;
522 eval = (sy2->svalue & 0xFFFFF0FF);
528 error("invalid GPU/DSP .equr/.regequ definition");
532 else if (equtyp == REG)
534 if (reglist(&rmask) < 0)
537 eval = (uint32_t)rmask;
538 eattr = ABS | DEFINED;
540 else if (equtyp == CCDEF)
542 sy->sattre |= EQUATEDCC;
543 eattr = ABS | DEFINED | GLOBAL;
545 if (tok[0] == SYMBOL)
547 sy2 = lookup(string[tok[1]], LABEL, j);
549 if (!sy2 || !(sy2->sattre & EQUATEDCC))
551 error("invalid gpu/dsp .ccdef definition");
556 eattr = ABS | DEFINED | GLOBAL;
557 sy->sattre = sy2->sattre;
562 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
566 else if (*tok == SYMBOL)
568 sy2 = lookup(string[tok[1]], LABEL, j);
570 if (sy2 && (sy2->sattre & EQUATEDREG))
572 sy->stype = sy2->stype;
573 sy->sattr = sy2->sattr;
574 sy->sattre = sy2->sattre;
575 sy->svalue = sy2->svalue;
578 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
581 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
584 if (!(eattr & DEFINED))
590 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
593 if (list_flag) // Put value in listing
594 listvalue((uint32_t)eval);
596 ErrorIfNotAtEOL(); // Must be at EOL now
603 // Non-zero == error occurred
604 if (HandleLabel(label, labtyp) != 0)
607 label_defined = label;
614 // If we're in 6502 mode and are still in need of a mnemonic, then search
615 // for valid 6502 mnemonic.
616 if (m6502 && (state < 0 || state >= 1000))
619 state = kmatch(opname, mpbase, mpcheck, mptab, mpaccept);
621 for(state=0, p=opname; state>= 0; )
623 j = mpbase[state] + tolowertab[*p];
625 if (mpcheck[j] != state) // Reject, character doesn't match
627 state = -1; // No match
632 { // Must accept or reject at EOS
633 state = mpaccept[j]; // (-1 on no terminal match)
641 // Call 6502 code generator if we found a mnemonic
644 m6502cg(state - 2000);
649 // If we are in GPU or DSP mode and still in need of a mnemonic then search
651 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
653 for(state=0, p=opname; state>=0;)
655 j = mrbase[state] + (int)tolowertab[*p];
657 // Reject, character doesn't match
658 if (mrcheck[j] != state)
660 state = -1; // No match
664 // Must accept or reject at EOS
667 state = mraccept[j]; // (-1 on no terminal match)
674 // Call RISC code generator if we found a mnemonic
677 GenerateRISCCode(state);
682 // If we are in OP mode and still in need of a mnemonic then search for one
683 if (robjproc && ((state < 0) || (state >= 1000)))
685 for(state=0, p=opname; state>=0;)
687 j = mobase[state] + (int)tolowertab[*p];
689 // Reject, character doesn't match
690 if (mocheck[j] != state)
692 state = -1; // No match
696 // Must accept or reject at EOS
699 state = moaccept[j]; // (-1 on no terminal match)
706 // Call OP code generator if we found a mnemonic
709 GenerateOPCode(state);
714 // If we are in 56K mode and still in need of a mnemonic then search for one
715 if (dsp56001 && ((state < 0) || (state >= 1000)))
717 for(state=0, p=opname; state>=0;)
719 j = dspbase[state] + (int)tolowertab[*p];
721 // Reject, character doesn't match
722 if (dspcheck[j] != state)
724 state = -1; // No match
728 // Must accept or reject at EOS
731 state = dspaccept[j]; // (-1 on no terminal match)
738 // Call DSP code generator if we found a mnemonic
743 MNTABDSP * md = &dsp56k_machtab[state - 2000];
744 deposit_extra_ea = 0; // Assume no extra word needed
746 if (md->mnfunc == dsp_mult)
748 // Special case for multiplication instructions: they require
750 if ((operands = dsp_amode(3)) == ERROR)
753 else if ((md->mnattr & PARMOVE) && md->mn0 != M_AM_NONE)
755 if (dsp_amode(2) == ERROR)
758 else if ((md->mnattr & PARMOVE) && md->mn0 == M_AM_NONE)
760 // Instructions that have parallel moves but use no operands
761 // (probably only move). In this case, don't parse addressing
762 // modes--just go straight to parallel parse
763 dsp_am0 = dsp_am1 = M_AM_NONE;
767 // Non parallel move instructions can have up to 4 parameters
768 // (well, only tcc instructions really)
769 if ((operands = dsp_amode(4)) == ERROR)
774 dsp_tcc4(md->mninst);
779 if (md->mnattr & PARMOVE)
781 // Check for parallel moves
782 if ((parcode = parmoves(dsp_a1reg)) == ERROR)
788 error("parallel moves not allowed with this instruction");
793 while ((dsp_am0 & md->mn0) == 0 || (dsp_am1 & md->mn1) == 0)
794 md = &dsp56k_machtab[md->mncont];
796 (*md->mnfunc)(md->mninst | (parcode << 8));
801 // Invoke macro or complain about bad mnemonic
804 if ((sy = lookup(opname, MACRO, 0)) != NULL)
805 InvokeMacro(sy, siz);
807 error("unknown op '%s'", opname);
812 // Call directive handlers
815 (*dirtab[state])(siz);
820 // o can't deposit instrs in BSS or ABS
821 // o do automatic .EVEN for instrs
822 // o allocate space for largest possible instr
823 // o can't do ".b" operations with an address register
826 error("cannot initialize non-storage (BSS) section");
830 if (sloc & 1) // Automatic .even
833 if (challoc - ch_size < 18) // Make sure have space in current chunk
836 m = &machtab[state - 1000];
838 // Call special-mode handler
839 if (m->mnattr & CGSPECIAL)
841 (*m->mnfunc)(m->mninst, siz);
845 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
851 amsk0 = amsktab[am0];
852 amsk1 = amsktab[am1];
854 // Catch attempts to use ".B" with an address register (yes, this check
855 // does work at this level)
856 if (siz == SIZB && (am0 == AREG || am1 == AREG))
858 error("cannot use '.b' with an address register");
862 // Keep a backup of chptr (used for optimisations during codegen)
863 chptr_opcode = chptr;
865 while (!(m->mnattr & siz) || (amsk0 & m->mn0) == 0 || (amsk1 & m->mn1) == 0)
866 m = &machtab[m->mncont];
868 DEBUG { printf(" 68K: mninst=$%X, siz=$%X, mnattr=$%X, amsk0=$%X, mn0=$%X, amsk1=$%X, mn1=$%X\n", m->mninst, siz, m->mnattr, amsk0, m->mn0, amsk1, m->mn1); }
870 (*m->mnfunc)(m->mninst, siz);
876 // Handle the creation of labels
878 int HandleLabel(char * label, int labelType)
880 // Check for dot in front of label; means this is a local label if present
881 int environment = (*label == '.' ? curenv : 0);
882 SYM * symbol = lookup(label, LABEL, environment);
886 symbol = NewSymbol(label, LABEL, environment);
890 else if (symbol->sattr & DEFINED)
891 return error("multiply-defined label '%s'", label);
893 // Put symbol in "order of definition" list if it's not already in it
894 AddToSymbolDeclarationList(symbol);
898 symbol->svalue = orgaddr;
899 symbol->sattr |= ABS | DEFINED | EQUATED;
903 symbol->svalue = sloc;
904 symbol->sattr |= DEFINED | cursect;
909 // Yes, our CS professors told us to write checks for equality this way,
910 // but damn, it hurts my brain every time I look at it. :-/
911 if (0 == environment)
914 // Make label global if it has a double colon
915 if (labelType == DCOLON)
917 if (environment != 0)
918 return error(locgl_error);
920 symbol->sattr |= GLOBAL;