2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // PROCLN.C - Line Processing
4 // Copyright (C) 199x Landon Dyer, 2017 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 #define DEF_MP /* include 6502 keyword definitions */
33 #define DECL_MP /* include 6502 keyword state machine tables */
35 extern int m6502; /* 1, assembler in .6502 mode */
36 extern VOID m6502cg(); /* 6502 code generator */
37 extern VOID m6502obj(int ofd);
39 IFENT * ifent; // Current ifent
40 static IFENT ifent0; // Root ifent
41 static IFENT * f_ifent; // Freelist of ifents
42 static int disabled; // Assembly conditionally disabled
43 int just_bss; // 1, ds.b in microprocessor mode
44 VALUE pcloc; // Value of "PC" at beginning of line
45 SYM * lab_sym; // Label on line (or NULL)
47 const char extra_stuff[] = "extra (unexpected) text found after addressing mode";
48 const char comma_error[] = "missing comma";
49 const char syntax_error[] = "syntax error";
50 const char locgl_error[] = "cannot GLOBL local symbol";
51 const char lab_ignored[] = "label ignored";
53 // Table to convert an addressing-mode number to a bitmask.
54 LONG amsktab[0112] = {
55 M_DREG, M_DREG, M_DREG, M_DREG,
56 M_DREG, M_DREG, M_DREG, M_DREG,
58 M_AREG, M_AREG, M_AREG, M_AREG,
59 M_AREG, M_AREG, M_AREG, M_AREG,
61 M_AIND, M_AIND, M_AIND, M_AIND,
62 M_AIND, M_AIND, M_AIND, M_AIND,
64 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
65 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
67 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
68 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
70 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
71 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
73 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
74 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
97 // Function prototypes
98 int HandleLabel(char *, int);
102 // Initialize line processor
104 void InitLineProcessor(void)
108 f_ifent = ifent0.if_prev = NULL;
118 int state; // Keyword machine state (output)
119 int j; // Random int, must be fast
120 char * p; // Random char ptr, must be fast
121 TOKEN * tk; // First token in line
122 char * label; // Symbol (or NULL)
123 char * equate; // Symbol (or NULL)
124 int labtyp = 0; // Label type (':', DCOLON)
125 int equtyp = 0; // Equ type ('=', DEQUALS)
126 VALUE eval; // Expression value
127 WORD eattr; // Expression attributes
128 SYM * esym; // External symbol involved in expr.
129 WORD siz = 0; // Size suffix to mnem/diretve/macro
130 LONG amsk0, amsk1; // Address-type masks for ea0, ea1
131 MNTAB * m; // Code generation table pointer
132 SYM * sy, * sy2; // Symbol (temp usage)
133 char * opname = NULL; // Name of dirctve/mnemonic/macro
134 int listflag; // 0: Don't call listeol()
135 WORD rmask; // Register list, for REG
136 int registerbank; // RISC register bank
137 int riscreg; // RISC register
138 listflag = 0; // Initialise listing flag
140 loop: // Line processing loop label
142 // Get another line of tokens
143 if (TokenizeLine() == TKEOF)
145 if (debug) printf("Assemble: Found TKEOF flag...\n");
146 if (list_flag && listflag) // Flush last line of source
149 if (ifent->if_prev != NULL) // Check conditional token
150 error("hit EOF without finding matching .endif");
155 DEBUG DumpTokenBuffer();
159 if (listflag && listing > 0)
160 listeol(); // Tell listing generator about EOL
162 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
163 listflag = 1; // OK to call `listeol' now
164 just_bss = 0; // Reset just_bss mode
167 state = -3; // No keyword (just EOL)
168 label = NULL; // No label
169 lab_sym = NULL; // No (exported) label
170 equate = NULL; // No equate
171 tk = tok; // Save first token in line
172 pcloc = (VALUE)sloc; // Set beginning-of-line PC
174 loop1: // Internal line processing loop
176 if (*tok == EOL) // Restart loop if end-of-line
179 // First token MUST be a symbol (Shamus: not sure why :-/)
182 error("syntax error; expected symbol");
186 j = (int)tok[2]; // Skip equates (normal statements)
188 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
190 equate = string[tok[1]];
196 // Skip past label (but record it)
197 if (j == ':' || j == DCOLON)
200 label = string[tok[1]]; // Get label name
201 labtyp = tok[2]; // Get label type
202 tok += 3; // Go to next line token
205 // Looks like another label follows the previous one, so handle
206 // the previous one until there aren't any more
207 if (as68_flag && (*tok == SYMBOL && tok[2] == ':'))
209 if (HandleLabel(label, labtyp) != 0)
216 // EOL is legal here...
220 // Next token MUST be a symbol
221 if (*tok++ != SYMBOL)
223 error("syntax error; expected symbol");
227 opname = p = string[*tok++];
229 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
230 // On output, `state' will have one of the values:
231 // -3 there was no symbol (EOL)
232 // -2..-1 the symbol didn't match any keyword
233 // 0..499 vanilla directives (dc, ds, etc.)
234 // 500..999 electric directives (macro, rept, etc.)
235 // 1000..+ mnemonics (move, lsr, etc.)
236 for(state=0; state>=0;)
238 j = mnbase[state] + (int)tolowertab[*p];
240 // Reject, character doesn't match
241 if (mncheck[j] != state)
243 state = -1; // No match
247 // Must accept or reject at EOS
250 state = mnaccept[j]; // (-1 on no terminal match)
257 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
262 else if (*tok == DOTL)
264 else if (*tok == DOTB)
267 // Do special directives (500..999) (These must be handled in "real time")
268 if (state >= 500 && state < 1000)
281 case MN_IIF: // .iif --- immediate if
282 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
285 if (!(eattr & DEFINED))
301 case MN_MACRO: // .macro --- macro definition
304 // Label on a macro definition is bad mojo... Warn the user
312 case MN_EXITM: // .exitm --- exit macro
313 case MN_ENDM: // .endm --- same as .exitm
326 // Handle labels on REPT directive lines...
329 if (HandleLabel(label, labtyp) != 0)
339 error("mis-nested .endr");
346 if (disabled) // Conditionally disabled code
352 // Pick global or local symbol enviroment
353 j = (*equate == '.' ? curenv : 0);
354 sy = lookup(equate, LABEL, j);
358 sy = NewSymbol(equate, LABEL, j);
361 if (equtyp == DEQUALS)
363 // Can't GLOBAL a local symbol
373 else if ((sy->sattr & DEFINED) && equtyp != SET)
375 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
377 //REALLY? sy->sattre |= ~UNDEF_EQUR;
378 sy->sattre &= ~UNDEF_EQUR;
381 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
383 //REALLY? sy->sattre |= ~UNDEF_CC;
384 sy->sattre &= ~UNDEF_CC;
389 errors("multiple equate to '%s'", sy->sname);
394 // Put symbol in "order of definition" list if it's not already there
395 AddToSymbolDeclarationList(sy);
397 // Parse value to equate symbol to;
401 if (equtyp == EQUREG)
403 //Linko's request to issue a warning on labels that equated to the same
404 //register would go here. Not sure how to implement it though. :-/
407 have an array of bools with 64 entries. Whenever a register is equated, set the
408 corresponding register bool to true. Whenever it's undef'ed, set it to false.
409 When checking to see if it's already been equated, issue a warning.
411 // Check that we are in a RISC section
414 error(".equr/.regequ must be defined in .gpu/.dsp section");
418 // Check for register to equate to
419 if ((*tok >= KW_R0) && (*tok <= KW_R31))
421 // sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
422 sy->sattre = EQUATEDREG; // Mark as equated register
423 riscreg = (*tok - KW_R0);
424 //is there any reason to do this, since we're putting this in svalue?
425 //i'm thinking, no. Let's test that out! :-D
426 // sy->sattre |= (riscreg << 8); // Store register number
427 //everything seems to build fine without it... We'll leave it here Just In Case(tm)
429 #define DEBODGE_REGBANK
430 #ifdef DEBODGE_REGBANK
431 // Default is current state of "regbank"
432 registerbank = regbank;
434 // Default is no register bank specified
435 registerbank = BANK_N;
438 // Check for ",<bank #>" override notation
439 if ((tok[1] == ',') && (tok[2] == CONST))
441 // Advance token pointer to the constant
444 // Anything other than a 0 or a 1 will result in "No Bank"
446 registerbank = BANK_0;
448 registerbank = BANK_1;
451 #ifdef DEBODGE_REGBANK
452 sy->sattre |= registerbank; // Store register bank
454 // What needs to happen here is to prime registerbank with regbank, then use
455 // registerbank down below for the bank marking.
456 #warning "!!! regbank <-> registerbank confusion here !!!"
457 // The question here is why, if we're allowed to override the ".regbankN" rules
458 // above, then why is it using the one set by the directive in the extended
459 // attributes and not in what ends up in symbol->svalue?
460 // ".regbankN" is not an original Madmac directive, so it's suspect
461 sy->sattre |= regbank; // Store register bank
463 eattr = ABS | DEFINED | GLOBAL;
464 // & what does this $80000080 constant mean???
465 // eval = 0x80000080 + (riscreg) + (registerbank << 8);
469 // Checking for a register symbol
470 else if (tok[0] == SYMBOL)
472 sy2 = lookup(string[tok[1]], LABEL, j);
474 // Make sure symbol is a valid equreg
475 if (!sy2 || !(sy2->sattre & EQUATEDREG))
477 error("invalid GPU/DSP .equr/.regequ definition");
482 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
483 sy->sattre = sy2->sattre;
484 eval = (sy2->svalue & 0xFFFFF0FF);
490 error("invalid GPU/DSP .equr/.regequ definition");
494 else if (equtyp == REG)
496 if (reglist(&rmask) < 0)
500 eattr = ABS | DEFINED;
502 else if (equtyp == CCDEF)
504 sy->sattre |= EQUATEDCC;
505 eattr = ABS | DEFINED | GLOBAL;
507 if (tok[0] == SYMBOL)
509 sy2 = lookup(string[tok[1]], LABEL, j);
511 if (!sy2 || !(sy2->sattre & EQUATEDCC))
513 error("invalid gpu/dsp .ccdef definition");
518 eattr = ABS | DEFINED | GLOBAL;
519 sy->sattre = sy2->sattre;
524 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
528 else if (*tok == SYMBOL)
530 sy2 = lookup(string[tok[1]], LABEL, j);
532 if (sy2 && (sy2->sattre & EQUATEDREG))
534 sy->stype = sy2->stype;
535 sy->sattr = sy2->sattr;
536 sy->sattre = sy2->sattre;
537 //ICK sy->svalue = (sy2->svalue & 0xFFFFF0FF);
538 sy->svalue = sy2->svalue;
541 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
544 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
547 if (!(eattr & DEFINED))
553 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
556 if (list_flag) // Put value in listing
559 at_eol(); // Must be at EOL now
566 // Non-zero == error occurred
567 if (HandleLabel(label, labtyp) != 0)
576 * If we're in 6502 mode and are still in need
577 * of a mnemonic, then search for valid 6502 mnemonic.
580 (state < 0 || state >= 1000))
583 state = kmatch(opname, mpbase, mpcheck, mptab, mpaccept);
585 for (state = 0, p = opname; state >= 0;)
587 j = mpbase[state] + tolowertab[*p];
588 if (mpcheck[j] != state) /* reject, character doesn't match */
590 state = -1; /* no match */
595 { /* must accept or reject at EOS */
596 state = mpaccept[j]; /* (-1 on no terminal match) */
604 * Call 6502 code generator if we found a mnemonic
608 m6502cg(state - 2000);
613 // If we are in GPU or DSP mode and still in need of a mnemonic then search
615 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
617 for(state=0, p=opname; state>=0;)
619 j = mrbase[state] + (int)tolowertab[*p];
621 // Reject, character doesn't match
622 if (mrcheck[j] != state)
624 state = -1; // No match
628 // Must accept or reject at EOS
631 state = mraccept[j]; // (-1 on no terminal match)
638 // Call RISC code generator if we found a mnemonic
641 GenerateRISCCode(state);
646 // Invoke macro or complain about bad mnemonic
649 if ((sy = lookup(opname, MACRO, 0)) != NULL)
650 InvokeMacro(sy, siz);
652 errors("unknown op '%s'", opname);
657 // Call directive handlers
660 (*dirtab[state])(siz);
665 // o can't deposit instrs in BSS or ABS
666 // o do automatic .EVEN for instrs
667 // o allocate space for largest possible instr
668 // o can't do ".b" operations with an address register
671 error("cannot initialize non-storage (BSS) section");
675 if (sloc & 1) // Automatic .even
678 if (challoc - ch_size < 18) // Make sure have space in current chunk
681 m = &machtab[state - 1000];
683 // Call special-mode handler
684 if (m->mnattr & CGSPECIAL)
686 (*m->mnfunc)(m->mninst, siz);
690 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
696 amsk0 = amsktab[am0];
697 amsk1 = amsktab[am1];
699 // Catch attempts to use ".B" with an address register (yes, this check
700 // does work at this level)
701 if (siz == SIZB && (am0 == AREG || am1 == AREG))
703 error("cannot use '.b' with an address register");
707 // Keep a backup of chptr (used for optimisations during codegen)
708 chptr_opcode = chptr;
712 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
714 (*m->mnfunc)(m->mninst, siz);
718 m = &machtab[m->mncont];
724 // Handle the creation of labels
726 int HandleLabel(char * label, int labelType)
728 // Check for dot in front of label; means this is a local label if present
729 int environment = (*label == '.' ? curenv : 0);
730 SYM * symbol = lookup(label, LABEL, environment);
734 symbol = NewSymbol(label, LABEL, environment);
736 // symbol->sattre = RISCSYM;
739 else if (symbol->sattr & DEFINED)
740 return errors("multiply-defined label '%s'", label);
742 // Put symbol in "order of definition" list if it's not already in it
743 AddToSymbolDeclarationList(symbol);
747 symbol->svalue = orgaddr;
748 symbol->sattr |= ABS | DEFINED | EQUATED;
752 symbol->svalue = sloc;
753 symbol->sattr |= DEFINED | cursect;
758 if (0 == environment)
761 // Make label global if it has a double colon
762 if (labelType == DCOLON)
764 if (environment != 0)
765 return error(locgl_error);
767 symbol->sattr |= GLOBAL;
775 // .if, Start conditional assembly
785 if ((rif = f_ifent) == NULL)
786 rif = (IFENT *)malloc(sizeof(IFENT));
788 f_ifent = rif->if_prev;
790 rif->if_prev = ifent;
795 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
798 if ((eattr & DEFINED) == 0)
799 return error(undef_error);
804 rif->if_state = (WORD)disabled;
810 // .else, Do alternate case for .if
816 if (rif->if_prev == NULL)
817 return error("mismatched .else");
820 disabled = rif->if_prev->if_state;
824 rif->if_state = (WORD)disabled;
830 // .endif, End of conditional assembly block
831 // This is also called by fpop() to pop levels of IFENTs in case a macro or
832 // include file exits early with `exitm' or `end'.
838 if (rif->if_prev == NULL)
839 return error("mismatched .endif");
841 ifent = rif->if_prev;
842 disabled = rif->if_prev->if_state;
843 rif->if_prev = f_ifent;