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
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 static IFENT * f_ifent; // Freelist of ifents
40 static int disabled; // Assembly conditionally disabled
41 int just_bss; // 1, ds.b in microprocessor mode
42 VALUE 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[0112] = {
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,
95 // Function prototypes
96 int HandleLabel(char *, int);
100 // Initialize line processor
102 void InitLineProcessor(void)
106 f_ifent = ifent0.if_prev = NULL;
116 int state; // Keyword machine state (output)
117 int j; // Random int, must be fast
118 char * p; // Random char ptr, must be fast
119 TOKEN * tk; // First token in line
120 char * label; // Symbol (or NULL)
121 char * equate; // Symbol (or NULL)
122 int labtyp = 0; // Label type (':', DCOLON)
123 int equtyp = 0; // Equ type ('=', DEQUALS)
124 VALUE eval; // Expression value
125 WORD eattr; // Expression attributes
126 SYM * esym; // External symbol involved in expr.
127 WORD siz = 0; // Size suffix to mnem/diretve/macro
128 LONG amsk0, amsk1; // Address-type masks for ea0, ea1
129 MNTAB * m; // Code generation table pointer
130 SYM * sy, * sy2; // Symbol (temp usage)
131 char * opname = NULL; // Name of dirctve/mnemonic/macro
132 int listflag; // 0: Don't call listeol()
133 WORD rmask; // Register list, for REG
134 int registerbank; // RISC register bank
135 int riscreg; // RISC register
136 listflag = 0; // Initialise listing flag
138 loop: // Line processing loop label
140 // Get another line of tokens
141 if (TokenizeLine() == TKEOF)
143 if (debug) printf("Assemble: Found TKEOF flag...\n");
144 if (list_flag && listflag) // Flush last line of source
147 if (ifent->if_prev != NULL) // Check conditional token
148 error("hit EOF without finding matching .endif");
153 DEBUG DumpTokenBuffer();
157 if (listflag && listing > 0)
158 listeol(); // Tell listing generator about EOL
160 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
161 listflag = 1; // OK to call `listeol' now
162 just_bss = 0; // Reset just_bss mode
165 state = -3; // No keyword (just EOL)
166 label = NULL; // No label
167 lab_sym = NULL; // No (exported) label
168 equate = NULL; // No equate
169 tk = tok; // Save first token in line
170 pcloc = (VALUE)sloc; // Set beginning-of-line PC
172 loop1: // Internal line processing loop
174 if (*tok == EOL) // Restart loop if end-of-line
177 // First token MUST be a symbol (Shamus: not sure why :-/)
180 error("syntax error; expected symbol");
184 j = (int)tok[2]; // Skip equates (normal statements)
186 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
188 equate = string[tok[1]];
194 // Skip past label (but record it)
195 if (j == ':' || j == DCOLON)
198 label = string[tok[1]]; // Get label name
199 labtyp = tok[2]; // Get label type
200 tok += 3; // Go to next line token
203 // Looks like another label follows the previous one, so handle
204 // the previous one until there aren't any more
205 if (as68_flag && (*tok == SYMBOL && tok[2] == ':'))
207 if (HandleLabel(label, labtyp) != 0)
214 // EOL is legal here...
218 // Next token MUST be a symbol
219 if (*tok++ != SYMBOL)
221 error("syntax error; expected symbol");
225 opname = p = string[*tok++];
227 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
228 // On output, `state' will have one of the values:
229 // -3 there was no symbol (EOL)
230 // -2..-1 the symbol didn't match any keyword
231 // 0..499 vanilla directives (dc, ds, etc.)
232 // 500..999 electric directives (macro, rept, etc.)
233 // 1000..+ mnemonics (move, lsr, etc.)
234 for(state=0; state>=0;)
236 j = mnbase[state] + (int)tolowertab[*p];
238 // Reject, character doesn't match
239 if (mncheck[j] != state)
241 state = -1; // No match
245 // Must accept or reject at EOS
248 state = mnaccept[j]; // (-1 on no terminal match)
255 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
260 else if (*tok == DOTL)
262 else if (*tok == DOTB)
265 // Do special directives (500..999) (These must be handled in "real time")
266 if (state >= 500 && state < 1000)
279 case MN_IIF: // .iif --- immediate if
280 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
283 if (!(eattr & DEFINED))
299 case MN_MACRO: // .macro --- macro definition
302 // Label on a macro definition is bad mojo... Warn the user
310 case MN_EXITM: // .exitm --- exit macro
311 case MN_ENDM: // .endm --- same as .exitm
324 // Handle labels on REPT directive lines...
327 if (HandleLabel(label, labtyp) != 0)
337 error("mis-nested .endr");
344 if (disabled) // Conditionally disabled code
350 // Pick global or local symbol enviroment
351 j = (*equate == '.' ? curenv : 0);
352 sy = lookup(equate, LABEL, j);
356 sy = NewSymbol(equate, LABEL, j);
359 if (equtyp == DEQUALS)
361 // Can't GLOBAL a local symbol
371 else if ((sy->sattr & DEFINED) && equtyp != SET)
373 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
375 //REALLY? sy->sattre |= ~UNDEF_EQUR;
376 sy->sattre &= ~UNDEF_EQUR;
379 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
381 //REALLY? sy->sattre |= ~UNDEF_CC;
382 sy->sattre &= ~UNDEF_CC;
387 errors("multiple equate to '%s'", sy->sname);
392 // Put symbol in "order of definition" list if it's not already there
393 AddToSymbolDeclarationList(sy);
395 // Parse value to equate symbol to;
399 if (equtyp == EQUREG)
401 //Linko's request to issue a warning on labels that equated to the same
402 //register would go here. Not sure how to implement it though. :-/
405 have an array of bools with 64 entries. Whenever a register is equated, set the
406 corresponding register bool to true. Whenever it's undef'ed, set it to false.
407 When checking to see if it's already been equated, issue a warning.
409 // Check that we are in a RISC section
412 error(".equr/.regequ must be defined in .gpu/.dsp section");
416 // Check for register to equate to
417 if ((*tok >= KW_R0) && (*tok <= KW_R31))
419 // sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
420 sy->sattre = EQUATEDREG; // Mark as equated register
421 riscreg = (*tok - KW_R0);
422 //is there any reason to do this, since we're putting this in svalue?
423 //i'm thinking, no. Let's test that out! :-D
424 // sy->sattre |= (riscreg << 8); // Store register number
425 //everything seems to build fine without it... We'll leave it here Just In Case(tm)
427 #define DEBODGE_REGBANK
428 #ifdef DEBODGE_REGBANK
429 // Default is current state of "regbank"
430 registerbank = regbank;
432 // Default is no register bank specified
433 registerbank = BANK_N;
436 // Check for ",<bank #>" override notation
437 if ((tok[1] == ',') && (tok[2] == CONST))
439 // Advance token pointer to the constant
442 // Anything other than a 0 or a 1 will result in "No Bank"
444 registerbank = BANK_0;
446 registerbank = BANK_1;
449 #ifdef DEBODGE_REGBANK
450 sy->sattre |= registerbank; // Store register bank
452 // What needs to happen here is to prime registerbank with regbank, then use
453 // registerbank down below for the bank marking.
454 #warning "!!! regbank <-> registerbank confusion here !!!"
455 // The question here is why, if we're allowed to override the ".regbankN" rules
456 // above, then why is it using the one set by the directive in the extended
457 // attributes and not in what ends up in symbol->svalue?
458 // ".regbankN" is not an original Madmac directive, so it's suspect
459 sy->sattre |= regbank; // Store register bank
461 eattr = ABS | DEFINED | GLOBAL;
462 // & what does this $80000080 constant mean???
463 // eval = 0x80000080 + (riscreg) + (registerbank << 8);
467 // Checking for a register symbol
468 else if (tok[0] == SYMBOL)
470 sy2 = lookup(string[tok[1]], LABEL, j);
472 // Make sure symbol is a valid equreg
473 if (!sy2 || !(sy2->sattre & EQUATEDREG))
475 error("invalid GPU/DSP .equr/.regequ definition");
480 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
481 sy->sattre = sy2->sattre;
482 eval = (sy2->svalue & 0xFFFFF0FF);
488 error("invalid GPU/DSP .equr/.regequ definition");
492 else if (equtyp == REG)
494 if (reglist(&rmask) < 0)
498 eattr = ABS | DEFINED;
500 else if (equtyp == CCDEF)
502 sy->sattre |= EQUATEDCC;
503 eattr = ABS | DEFINED | GLOBAL;
505 if (tok[0] == SYMBOL)
507 sy2 = lookup(string[tok[1]], LABEL, j);
509 if (!sy2 || !(sy2->sattre & EQUATEDCC))
511 error("invalid gpu/dsp .ccdef definition");
516 eattr = ABS | DEFINED | GLOBAL;
517 sy->sattre = sy2->sattre;
522 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
526 else if (*tok == SYMBOL)
528 sy2 = lookup(string[tok[1]], LABEL, j);
530 if (sy2 && (sy2->sattre & EQUATEDREG))
532 sy->stype = sy2->stype;
533 sy->sattr = sy2->sattr;
534 sy->sattre = sy2->sattre;
535 //ICK sy->svalue = (sy2->svalue & 0xFFFFF0FF);
536 sy->svalue = sy2->svalue;
539 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
542 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
545 if (!(eattr & DEFINED))
551 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
554 if (list_flag) // Put value in listing
557 at_eol(); // Must be at EOL now
564 // Non-zero == error occurred
565 if (HandleLabel(label, labtyp) != 0)
573 // If we're in 6502 mode and are still in need of a mnemonic, then search
574 // for valid 6502 mnemonic.
575 if (m6502 && (state < 0 || state >= 1000))
578 state = kmatch(opname, mpbase, mpcheck, mptab, mpaccept);
580 for(state=0, p=opname; state>= 0; )
582 j = mpbase[state] + tolowertab[*p];
584 if (mpcheck[j] != state) // Reject, character doesn't match
586 state = -1; // No match
591 { // Must accept or reject at EOS
592 state = mpaccept[j]; // (-1 on no terminal match)
600 // Call 6502 code generator if we found a mnemonic
603 m6502cg(state - 2000);
608 // If we are in GPU or DSP mode and still in need of a mnemonic then search
610 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
612 for(state=0, p=opname; state>=0;)
614 j = mrbase[state] + (int)tolowertab[*p];
616 // Reject, character doesn't match
617 if (mrcheck[j] != state)
619 state = -1; // No match
623 // Must accept or reject at EOS
626 state = mraccept[j]; // (-1 on no terminal match)
633 // Call RISC code generator if we found a mnemonic
636 GenerateRISCCode(state);
641 // Invoke macro or complain about bad mnemonic
644 if ((sy = lookup(opname, MACRO, 0)) != NULL)
645 InvokeMacro(sy, siz);
647 errors("unknown op '%s'", opname);
652 // Call directive handlers
655 (*dirtab[state])(siz);
660 // o can't deposit instrs in BSS or ABS
661 // o do automatic .EVEN for instrs
662 // o allocate space for largest possible instr
663 // o can't do ".b" operations with an address register
666 error("cannot initialize non-storage (BSS) section");
670 if (sloc & 1) // Automatic .even
673 if (challoc - ch_size < 18) // Make sure have space in current chunk
676 m = &machtab[state - 1000];
678 // Call special-mode handler
679 if (m->mnattr & CGSPECIAL)
681 (*m->mnfunc)(m->mninst, siz);
685 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
691 amsk0 = amsktab[am0];
692 amsk1 = amsktab[am1];
694 // Catch attempts to use ".B" with an address register (yes, this check
695 // does work at this level)
696 if (siz == SIZB && (am0 == AREG || am1 == AREG))
698 error("cannot use '.b' with an address register");
702 // Keep a backup of chptr (used for optimisations during codegen)
703 chptr_opcode = chptr;
707 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
709 (*m->mnfunc)(m->mninst, siz);
713 m = &machtab[m->mncont];
719 // Handle the creation of labels
721 int HandleLabel(char * label, int labelType)
723 // Check for dot in front of label; means this is a local label if present
724 int environment = (*label == '.' ? curenv : 0);
725 SYM * symbol = lookup(label, LABEL, environment);
729 symbol = NewSymbol(label, LABEL, environment);
731 // symbol->sattre = RISCSYM;
734 else if (symbol->sattr & DEFINED)
735 return errors("multiply-defined label '%s'", label);
737 // Put symbol in "order of definition" list if it's not already in it
738 AddToSymbolDeclarationList(symbol);
742 symbol->svalue = orgaddr;
743 symbol->sattr |= ABS | DEFINED | EQUATED;
747 symbol->svalue = sloc;
748 symbol->sattr |= DEFINED | cursect;
753 if (0 == environment)
756 // Make label global if it has a double colon
757 if (labelType == DCOLON)
759 if (environment != 0)
760 return error(locgl_error);
762 symbol->sattr |= GLOBAL;
770 // .if, Start conditional assembly
780 if ((rif = f_ifent) == NULL)
781 rif = (IFENT *)malloc(sizeof(IFENT));
783 f_ifent = rif->if_prev;
785 rif->if_prev = ifent;
790 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
793 if ((eattr & DEFINED) == 0)
794 return error(undef_error);
799 rif->if_state = (WORD)disabled;
805 // .else, Do alternate case for .if
811 if (rif->if_prev == NULL)
812 return error("mismatched .else");
815 disabled = rif->if_prev->if_state;
819 rif->if_state = (WORD)disabled;
825 // .endif, End of conditional assembly block
826 // This is also called by fpop() to pop levels of IFENTs in case a macro or
827 // include file exits early with `exitm' or `end'.
833 if (rif->if_prev == NULL)
834 return error("mismatched .endif");
836 ifent = rif->if_prev;
837 disabled = rif->if_prev->if_state;
838 rif->if_prev = f_ifent;