2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // PROCLN.C - Line Processing
4 // Copyright (C) 199x Landon Dyer, 2011-2021 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 equreg; // RISC register
158 listflag = 0; // Initialise listing flag
160 loop: // Line processing loop label
162 // Get another line of tokens
163 if (TokenizeLine() == TKEOF)
165 DEBUG { printf("Assemble: Found TKEOF flag...\n"); }
166 if (list_flag && listflag) // Flush last line of source
169 if (ifent->if_prev != NULL) // Check conditional token
170 error("hit EOF without finding matching .endif");
175 DEBUG { DumpTokenBuffer(); }
179 if (listflag && listing > 0)
180 listeol(); // Tell listing generator about EOL
182 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
183 listflag = 1; // OK to call `listeol' now
184 just_bss = 0; // Reset just_bss mode
187 state = -3; // No keyword (just EOL)
188 label = NULL; // No label
189 label_defined = NULL; // No label defined yet
190 lab_sym = NULL; // No (exported) label
191 equate = NULL; // No equate
192 tk = tok; // Save first token in line
193 pcloc = (uint32_t)sloc; // Set beginning-of-line PC
195 loop1: // Internal line processing loop
197 if (*tok == EOL) // Restart loop if end-of-line
200 // First token MUST be a symbol (Shamus: not sure why :-/)
203 if ((*tok >= KW_D0) && (*tok <= KW_R31))
204 error("cannot use reserved keyword as label name or .equ");
206 error("syntax error; expected symbol");
211 j = (int)tok[2]; // Skip equates (normal statements)
213 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
215 equate = string[tok[1]];
221 // Skip past label (but record it)
222 if (j == ':' || j == DCOLON)
224 label = string[tok[1]]; // Get label name
225 labtyp = tok[2]; // Get label type
226 tok += 3; // Go to next line token
229 // EOL is legal here...
233 // First token MUST be a symbol (if we get here, tok didn't advance)
234 if (*tok++ != SYMBOL)
236 error("syntax error; expected symbol");
240 opname = p = string[*tok++];
242 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
243 // On output, `state' will have one of the values:
244 // -3 there was no symbol (EOL)
245 // -2..-1 the symbol didn't match any keyword
246 // 0..499 vanilla directives (dc, ds, etc.)
247 // 500..999 electric directives (macro, rept, etc.)
248 // 1000..+ mnemonics (move, lsr, etc.)
249 for(state=0; state>=0;)
251 j = mnbase[state] + (int)tolowertab[*p];
253 // Reject, character doesn't match
254 if (mncheck[j] != state)
256 state = -1; // No match
260 // Must accept or reject at EOS
263 state = mnaccept[j]; // (-1 on no terminal match)
270 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
275 case DOTW: siz = SIZW, tok++; break;
276 case DOTL: siz = SIZL, tok++; break;
277 case DOTB: siz = SIZB, tok++; break;
278 case DOTD: siz = SIZD, tok++; break;
279 case DOTP: siz = SIZP, tok++; break;
280 case DOTQ: siz = SIZQ, tok++; break;
281 case DOTS: siz = SIZS, tok++; break;
282 case DOTX: siz = SIZX, tok++; break;
285 // Do special directives (500..999) (These must be handled in "real time")
286 if (state >= 500 && state < 1000)
302 case MN_IIF: // .iif --- immediate if
303 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
306 if (!(eattr & DEFINED))
323 case MN_MACRO: // .macro --- macro definition
326 // Label on a macro definition is bad mojo... Warn the user
335 case MN_EXITM: // .exitm --- exit macro
336 case MN_ENDM: // .endm --- same as .exitm
350 // Handle labels on REPT directive lines...
353 if (HandleLabel(label, labtyp) != 0)
356 label_defined = label;
366 error("mis-nested .endr");
373 if (disabled) // Conditionally disabled code
379 // Pick global or local symbol enviroment
380 j = (*equate == '.' ? curenv : 0);
381 sy = lookup(equate, LABEL, j);
385 sy = NewSymbol(equate, LABEL, j);
388 if (equtyp == DEQUALS)
390 // Can't GLOBAL a local symbol
400 else if ((sy->sattr & DEFINED) && equtyp != SET)
402 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
404 sy->sattre &= ~UNDEF_EQUR;
407 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
409 sy->sattre &= ~UNDEF_CC;
414 error("multiple equate to '%s'", sy->sname);
419 // Put symbol in "order of definition" list if it's not already there
420 AddToSymbolDeclarationList(sy);
422 // Parse value to equate symbol to;
426 if (equtyp == EQUREG)
428 //Linko's request to issue a warning on labels that equated to the same
429 //register would go here. Not sure how to implement it though. :-/
432 have an array of bools with 64 entries. Whenever a register is equated, set the
433 corresponding register bool to true. Whenever it's undef'ed, set it to false.
434 When checking to see if it's already been equated, issue a warning.
437 // Check for register to equate to
438 // This check will change once we split the registers per architecture into their own tables
439 // and out of kw.tab. But for now it'll do...
440 if ((*tok >= KW_D0) && (*tok <= KW_BA))
442 sy->sattre = EQUATEDREG; // Mark as equated register
444 // Check for ",<bank #>" override notation and skip past it.
445 // It is ignored now. Was that ever useful anyway?
446 if ((rgpu ||rdsp) && (tok[1] == ',') && (tok[2] == CONST))
448 // Advance token pointer and skip everything
452 eattr = ABS | DEFINED | GLOBAL;
456 // Checking for a register symbol
457 else if (tok[0] == SYMBOL)
459 sy2 = lookup(string[tok[1]], LABEL, j);
461 // Make sure symbol is a valid equreg
462 if (!sy2 || !(sy2->sattre & EQUATEDREG))
464 error("invalid .equr/.regequ definition");
469 eattr = ABS | DEFINED | GLOBAL; // Copy symbol's attributes
470 sy->sattre = sy2->sattre;
471 eval = (sy2->svalue & 0xFFFFF0FF);
477 error("invalid .equr/.regequ definition");
481 else if (equtyp == REG)
483 if (reglist(&rmask) < 0)
486 eval = (uint32_t)rmask;
487 eattr = ABS | DEFINED;
489 else if (equtyp == CCDEF)
491 sy->sattre |= EQUATEDCC;
492 eattr = ABS | DEFINED | GLOBAL;
494 if (tok[0] == SYMBOL)
496 sy2 = lookup(string[tok[1]], LABEL, j);
498 if (!sy2 || !(sy2->sattre & EQUATEDCC))
500 error("invalid gpu/dsp .ccdef definition");
505 eattr = ABS | DEFINED | GLOBAL;
506 sy->sattre = sy2->sattre;
511 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
515 else if (*tok == SYMBOL)
517 sy2 = lookup(string[tok[1]], LABEL, j);
519 if (sy2 && (sy2->sattre & EQUATEDREG))
521 sy->stype = sy2->stype;
522 sy->sattr = sy2->sattr;
523 sy->sattre = sy2->sattre;
524 sy->svalue = sy2->svalue;
527 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
530 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
533 if (!(eattr & DEFINED))
539 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
542 if (list_flag) // Put value in listing
543 listvalue((uint32_t)eval);
545 ErrorIfNotAtEOL(); // Must be at EOL now
552 // Non-zero == error occurred
553 if (HandleLabel(label, labtyp) != 0)
556 label_defined = label;
563 // If we're in 6502 mode and are still in need of a mnemonic, then search
564 // for valid 6502 mnemonic.
565 if (m6502 && (state < 0 || state >= 1000))
568 state = kmatch(opname, mpbase, mpcheck, mptab, mpaccept);
570 for(state=0, p=opname; state>= 0; )
572 j = mpbase[state] + tolowertab[*p];
574 if (mpcheck[j] != state) // Reject, character doesn't match
576 state = -1; // No match
581 { // Must accept or reject at EOS
582 state = mpaccept[j]; // (-1 on no terminal match)
590 // Call 6502 code generator if we found a mnemonic
593 m6502cg(state - 2000);
598 // If we are in GPU or DSP mode and still in need of a mnemonic then search
600 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
602 for(state=0, p=opname; state>=0;)
604 j = mrbase[state] + (int)tolowertab[*p];
606 // Reject, character doesn't match
607 if (mrcheck[j] != state)
609 state = -1; // No match
613 // Must accept or reject at EOS
616 state = mraccept[j]; // (-1 on no terminal match)
623 // Call RISC code generator if we found a mnemonic
626 GenerateRISCCode(state);
631 // If we are in OP mode and still in need of a mnemonic then search for one
632 if (robjproc && ((state < 0) || (state >= 1000)))
634 for(state=0, p=opname; state>=0;)
636 j = mobase[state] + (int)tolowertab[*p];
638 // Reject, character doesn't match
639 if (mocheck[j] != state)
641 state = -1; // No match
645 // Must accept or reject at EOS
648 state = moaccept[j]; // (-1 on no terminal match)
655 // Call OP code generator if we found a mnemonic
658 GenerateOPCode(state);
663 // If we are in 56K mode and still in need of a mnemonic then search for one
664 if (dsp56001 && ((state < 0) || (state >= 1000)))
666 for(state=0, p=opname; state>=0;)
668 j = dspbase[state] + (int)tolowertab[*p];
670 // Reject, character doesn't match
671 if (dspcheck[j] != state)
673 state = -1; // No match
677 // Must accept or reject at EOS
680 state = dspaccept[j]; // (-1 on no terminal match)
687 // Call DSP code generator if we found a mnemonic
692 MNTABDSP * md = &dsp56k_machtab[state - 2000];
693 deposit_extra_ea = 0; // Assume no extra word needed
695 if (md->mnfunc == dsp_mult)
697 // Special case for multiplication instructions: they require
699 if ((operands = dsp_amode(3)) == ERROR)
702 else if ((md->mnattr & PARMOVE) && md->mn0 != M_AM_NONE)
704 if (dsp_amode(2) == ERROR)
707 else if ((md->mnattr & PARMOVE) && md->mn0 == M_AM_NONE)
709 // Instructions that have parallel moves but use no operands
710 // (probably only move). In this case, don't parse addressing
711 // modes--just go straight to parallel parse
712 dsp_am0 = dsp_am1 = M_AM_NONE;
716 // Non parallel move instructions can have up to 4 parameters
717 // (well, only tcc instructions really)
718 if ((operands = dsp_amode(4)) == ERROR)
723 dsp_tcc4(md->mninst);
728 if (md->mnattr & PARMOVE)
730 // Check for parallel moves
731 if ((parcode = parmoves(dsp_a1reg)) == ERROR)
737 error("parallel moves not allowed with this instruction");
742 while ((dsp_am0 & md->mn0) == 0 || (dsp_am1 & md->mn1) == 0)
743 md = &dsp56k_machtab[md->mncont];
745 (*md->mnfunc)(md->mninst | (parcode << 8));
750 // Invoke macro or complain about bad mnemonic
753 if ((sy = lookup(opname, MACRO, 0)) != NULL)
754 InvokeMacro(sy, siz);
756 error("unknown op '%s'", opname);
761 // Call directive handlers
764 (*dirtab[state])(siz);
769 // o can't deposit instrs in BSS or ABS
770 // o do automatic .EVEN for instrs
771 // o allocate space for largest possible instr
772 // o can't do ".b" operations with an address register
775 error("cannot initialize non-storage (BSS) section");
779 if (sloc & 1) // Automatic .even
782 if (challoc - ch_size < 18) // Make sure have space in current chunk
785 m = &machtab[state - 1000];
787 // Call special-mode handler
788 if (m->mnattr & CGSPECIAL)
790 (*m->mnfunc)(m->mninst, siz);
794 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
797 // Check that we're at EOL
798 // The only exception is ptestr/ptestw instructions
799 // that have 3 or 4 operands and are not handled by
800 // amode(). (yes, we're taking a performance hit here sadly)
801 if (m->mnfunc != m_ptestr && m->mnfunc != m_ptestw)
805 amsk0 = amsktab[am0];
806 amsk1 = amsktab[am1];
808 // Catch attempts to use ".B" with an address register (yes, this check
809 // does work at this level)
810 if (siz == SIZB && (am0 == AREG || am1 == AREG))
812 error("cannot use '.b' with an address register");
816 // Keep a backup of chptr (used for optimisations during codegen)
817 chptr_opcode = chptr;
819 while (!(m->mnattr & siz) || (amsk0 & m->mn0) == 0 || (amsk1 & m->mn1) == 0)
820 m = &machtab[m->mncont];
822 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); }
824 (*m->mnfunc)(m->mninst, siz);
830 // Handle the creation of labels
832 int HandleLabel(char * label, int labelType)
834 // Check for dot in front of label; means this is a local label if present
835 int environment = (*label == '.' ? curenv : 0);
836 SYM * symbol = lookup(label, LABEL, environment);
840 symbol = NewSymbol(label, LABEL, environment);
844 else if (symbol->sattr & DEFINED)
845 return error("multiply-defined label '%s'", label);
847 // Put symbol in "order of definition" list if it's not already in it
848 AddToSymbolDeclarationList(symbol);
852 symbol->svalue = orgaddr;
853 symbol->sattr |= ABS | DEFINED | EQUATED;
857 symbol->svalue = sloc;
858 symbol->sattr |= DEFINED | cursect;
863 // Yes, our CS professors told us to write checks for equality this way,
864 // but damn, it hurts my brain every time I look at it. :-/
865 if (0 == environment)
868 // Make label global if it has a double colon
869 if (labelType == DCOLON)
871 if (environment != 0)
872 return error(locgl_error);
874 symbol->sattr |= GLOBAL;