2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // PROCLN.C - Line Processing
4 // Copyright (C) 199x Landon Dyer, 2011 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
33 IFENT * ifent; // Current ifent
34 static IFENT ifent0; // Root ifent
35 static IFENT * f_ifent; // Freelist of ifents
36 static int disabled; // Assembly conditionally disabled
37 int just_bss; // 1, ds.b in microprocessor mode
38 VALUE pcloc; // Value of "PC" at beginning of line
39 IFENT * ifent; // Current ifent
40 SYM * lab_sym; // Label on line (or NULL)
42 const char extra_stuff[] = "extra (unexpected) text found after addressing mode";
43 const char comma_error[] = "missing comma";
44 const char syntax_error[] = "syntax error";
45 const char locgl_error[] = "cannot GLOBL local symbol";
46 const char lab_ignored[] = "label ignored";
48 // Table to convert an addressing-mode number to a bitmask.
49 LONG amsktab[0112] = {
50 M_DREG, M_DREG, M_DREG, M_DREG,
51 M_DREG, M_DREG, M_DREG, M_DREG,
53 M_AREG, M_AREG, M_AREG, M_AREG,
54 M_AREG, M_AREG, M_AREG, M_AREG,
56 M_AIND, M_AIND, M_AIND, M_AIND,
57 M_AIND, M_AIND, M_AIND, M_AIND,
59 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
60 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
62 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
63 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
65 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
66 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
68 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
69 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
93 // Initialize Line Processor
95 void InitLineProcessor(void)
99 f_ifent = ifent0.if_prev = NULL;
109 int state; // Keyword machine state (output)
110 int j; // Random int, must be fast
111 char * p; // Random char ptr, must be fast
112 TOKEN * tk; // First token in line
113 char * label; // Symbol (or NULL)
114 char * equate; // Symbol (or NULL)
115 int labtyp = 0; // Label type (':', DCOLON)
116 int equtyp = 0; // Equ type ('=', DEQUALS)
117 VALUE eval; // Expression value
118 WORD eattr; // Expression attributes
119 SYM * esym; // External symbol involved in expr.
120 WORD siz = 0; // Size suffix to mnem/diretve/macro
121 LONG amsk0, amsk1; // Address-type masks for ea0, ea1
122 MNTAB * m; // Code generation table pointer
123 SYM * sy, * sy2; // Symbol (temp usage)
124 char * opname = NULL; // Name of dirctve/mnemonic/macro
125 int listflag; // 0: Don't call listeol()
126 int as68mode = 0; // 1: Handle multiple labels
127 WORD rmask; // Register list, for REG
128 int registerbank; // RISC register bank
129 int riscreg; // RISC register
130 listflag = 0; // Initialise listing flag
132 loop: // Line processing loop label
134 // Get another line of tokens
135 if (TokenizeLine() == TKEOF)
137 if (verb_flag) printf("Assemble: Found TKEOF flag...\n");
138 if (list_flag && listflag) // Flush last line of source
141 if (ifent->if_prev != NULL) // Check conditional token
142 error("hit EOF without finding matching .endif");
147 DEBUG DumpTokenBuffer();
151 if (listflag && listing > 0)
152 listeol(); // Tell listing generator about EOL
154 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
155 listflag = 1; // OK to call `listeol' now
156 just_bss = 0; // Reset just_bss mode
159 state = -3; // No keyword (just EOL)
160 label = NULL; // No label
161 lab_sym = NULL; // No (exported) label
162 equate = NULL; // No equate
163 tk = tok; // Save first token in line
164 pcloc = (VALUE)sloc; // Set beginning-of-line PC
166 loop1: // Internal line processing loop
168 if (*tok == EOL) // Restart loop if end-of-line
171 // First token MUST be a symbol
178 j = (int)tok[2]; // Skip equates (normal statements)
180 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
182 equate = string[tok[1]];
188 // Skip past label (but record it)
189 if (j == ':' || j == DCOLON)
192 // label = (char *)tok[1]; // Get label name
193 label = string[tok[1]]; // Get label name
194 labtyp = tok[2]; // Get label type
195 tok += 3; // Go to next line token
197 // Handle multiple labels; if there's another label, go process it,
198 // and come back at `as68label' above.
203 if (*tok == SYMBOL && tok[2] == ':')
211 if (*tok == EOL) // EOL is legal here...
214 // Next token MUST be a symbol
215 if (*tok++ != SYMBOL)
221 // This is the problem here: On 64-bit platforms, this cuts the native pointer
222 // in half. We need to figure out how to fix this.
223 //#warning "!!! Bad pointer !!!"
225 opname = p = (char *)*tok++; // Store opcode name here
227 opname = p = string[*tok++];
230 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
231 // On output, `state' will have one of the values:
232 // -3 there was no symbol (EOL)
233 // -2..-1 the symbol didn't match any keyword
234 // 0..499 vanilla directives (dc, ds, etc.)
235 // 500..999 electric directives (macro, rept, etc.)
236 // 1000..+ mnemonics (move, lsr, etc.)
237 for(state=0; state>=0;)
239 j = mnbase[state] + (int)tolowertab[*p];
241 // Reject, character doesn't match
242 if (mncheck[j] != state)
244 state = -1; // No match
248 // Must accept or reject at EOS
251 state = mnaccept[j]; // (-1 on no terminal match)
258 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
263 else if (*tok == DOTL)
265 else if (*tok == DOTB)
268 // Do special directives (500..999) (These must be handled in "real time")
269 if (state >= 500 && state < 1000)
282 case MN_IIF: // .iif --- immediate if
283 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
286 if (!(eattr & DEFINED))
302 case MN_MACRO: // .macro --- macro definition
312 case MN_EXITM: // .exitm --- exit macro
313 case MN_ENDM: // .endm --- same as .exitm
335 error("mis-nested .endr");
342 if (disabled) // Conditionally disabled code
348 // Pick global or local symbol enviroment
355 j = (*equate == '.' ? curenv : 0);
357 sy = lookup(equate, LABEL, j);
361 sy = NewSymbol(equate, LABEL, j);
364 if (equtyp == DEQUALS)
366 // Can't GLOBAL a local symbol
376 else if ((sy->sattr & DEFINED) && equtyp != SET)
378 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
380 //REALLY? sy->sattre |= ~UNDEF_EQUR;
381 sy->sattre &= ~UNDEF_EQUR;
384 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
386 //REALLY? sy->sattre |= ~UNDEF_CC;
387 sy->sattre &= ~UNDEF_CC;
392 errors("multiple equate to '%s'", sy->sname);
397 // Put symbol in "order of definition" list
398 if (!(sy->sattr & SDECLLIST))
401 // Parse value to equate symbol to;
405 if (equtyp == EQUREG)
407 //Linko's request to issue a warning on labels that equated to the same register
408 //would go here. Not sure how to implement it though. :-/
411 have an array of bools with 64 entries. Whenever a register is equated, set the
412 corresponding register bool to true. Whenever it's undef'ed, set it to false. When
413 checking to see if it's already been equated, issue a warning.
415 // Check that we are in a RISC section
418 error(".equr/.regequ must be defined in .gpu/.dsp section");
422 // Check for register to equate to
423 if ((*tok >= KW_R0) && (*tok <= KW_R31))
425 sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
426 riscreg = (*tok - KW_R0);
427 //is there any reason to do this, since we're putting this in svalue?
428 //i'm thinking, no. Let's test that out! :-D
429 // sy->sattre |= (riscreg << 8); // Store register number
430 //everything seems to build fine without it... We'll leave it here Just In Case(tm)
432 #define DEBODGE_REGBANK
433 #ifdef DEBODGE_REGBANK
434 // Default is current state of "regbank"
435 registerbank = regbank;
437 // Default is no register bank specified
438 registerbank = BANK_N;
441 // Check for ",<bank #>" override notation
442 if ((tok[1] == ',') && (tok[2] == CONST))
444 // Advance token pointer to the constant
447 // Anything other than a 0 or a 1 will result in "No Bank"
449 registerbank = BANK_0;
451 registerbank = BANK_1;
454 #ifdef DEBODGE_REGBANK
455 sy->sattre |= registerbank; // Store register bank
457 // What needs to happen here is to prime registerbank with regbank, then use
458 // registerbank down below for the bank marking.
459 #warning "!!! regbank <-> registerbank confusion here !!!"
460 // The question here is why, if we're allowed to override the ".regbankN" rules above,
461 // then why is it using the one set by the directive in the extended attributes and
462 // not in what ends up in symbol->svalue?
463 // ".regbankN" is not an original Madmac directive, so it's suspect
464 sy->sattre |= regbank; // Store register bank
466 eattr = ABS | DEFINED | GLOBAL;
467 // & what does this $80000080 constant mean???
468 // eval = 0x80000080 + (riscreg) + (registerbank << 8);
472 // Checking for a register symbol
473 else if (tok[0] == SYMBOL)
475 sy2 = lookup(string[tok[1]], LABEL, j);
477 // Make sure symbol is a valid equreg
478 if (!sy2 || !(sy2->sattre & EQUATEDREG))
480 error("invalid GPU/DSP .equr/.regequ definition");
485 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
486 sy->sattre = sy2->sattre;
487 eval = (sy2->svalue & 0xFFFFF0FF);
493 error("invalid GPU/DSP .equr/.regequ definition");
497 else if (equtyp == REG)
499 if (reglist(&rmask) < 0)
503 eattr = ABS | DEFINED;
505 else if (equtyp == CCDEF)
507 sy->sattre |= EQUATEDCC;
508 eattr = ABS | DEFINED | GLOBAL;
510 if (tok[0] == SYMBOL)
512 sy2 = lookup(string[tok[1]], LABEL, j);
514 if (!sy2 || !(sy2->sattre & EQUATEDCC))
516 error("invalid gpu/dsp .ccdef definition");
521 eattr = ABS | DEFINED | GLOBAL;
522 sy->sattre = sy2->sattre;
527 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
531 else if (*tok == SYMBOL)
533 sy2 = lookup(string[tok[1]], LABEL, j);
535 if (sy2 && (sy2->sattre & EQUATEDREG))
537 sy->stype = sy2->stype;
538 sy->sattr = sy2->sattr;
539 sy->sattre = sy2->sattre;
540 //ICK sy->svalue = (sy2->svalue & 0xFFFFF0FF);
541 sy->svalue = sy2->svalue;
544 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
547 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
550 if (!(eattr & DEFINED))
556 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
559 if (list_flag) // Put value in listing
562 at_eol(); // Must be at EOL now
570 // Check for dot in front of label; means this is a local label if present
571 j = (*label == '.' ? curenv : 0);
572 sy = lookup(label, LABEL, j);
576 sy = NewSymbol(label, LABEL, j);
578 sy->sattre = RISCSYM;
580 else if (sy->sattr & DEFINED)
582 errors("multiply-defined label '%s'", label);
586 // Put symbol in "order of definition" list
587 if (!(sy->sattr & SDECLLIST))
592 sy->svalue = orgaddr;
593 sy->sattr |= ABS | DEFINED | EQUATED;
598 sy->sattr |= DEFINED | cursect;
607 if (labtyp == DCOLON)
618 // If we're in as68 mode, and there's another label, go back and handle it
619 if (as68_flag && as68mode)
627 // If we are in GPU or DSP mode and still in need of a mnemonic then search
629 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
631 for(state=0, p=opname; state>=0;)
633 j = mrbase[state] + (int)tolowertab[*p];
635 // Reject, character doesn't match
636 if (mrcheck[j] != state)
638 state = -1; // No match
642 // Must accept or reject at EOS
645 state = mraccept[j]; // (-1 on no terminal match)
652 // Call RISC code generator if we found a mnemonic
655 GenerateRISCCode(state);
660 // Invoke macro or complain about bad mnemonic
663 if ((sy = lookup(opname, MACRO, 0)) != NULL)
664 InvokeMacro(sy, siz);
666 errors("unknown op '%s'", opname);
671 // Call directive handlers
674 (*dirtab[state])(siz);
679 // o can't deposit instrs in BSS or ABS
680 // o do automatic .EVEN for instrs
681 // o allocate space for largest possible instr
682 // o can't do ".b" operations with an address register
685 error("cannot initialize non-storage (BSS) section");
689 if (sloc & 1) // Automatic .even
692 if (challoc - ch_size < 18) // Make sure have space in current chunk
695 m = &machtab[state - 1000];
697 // Call special-mode handler
698 if (m->mnattr & CGSPECIAL)
700 (*m->mnfunc)(m->mninst, siz);
704 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
710 amsk0 = amsktab[am0];
711 amsk1 = amsktab[am1];
713 // Catch attempts to use ".B" with an address register (yes, this check
714 // does work at this level)
715 if (siz == SIZB && (am0 == AREG || am1 == AREG))
717 error("cannot use '.b' with an address register");
723 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
725 (*m->mnfunc)(m->mninst, siz);
729 m = &machtab[m->mncont];
735 // .if, Start Conditional Assembly
745 if ((rif = f_ifent) == NULL)
746 rif = (IFENT *)malloc(sizeof(IFENT));
748 f_ifent = rif->if_prev;
750 rif->if_prev = ifent;
755 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
758 if ((eattr & DEFINED) == 0)
759 return error(undef_error);
764 rif->if_state = (WORD)disabled;
770 // .else, Do Alternate Case For .if
776 if (rif->if_prev == NULL)
777 return error("mismatched .else");
780 disabled = rif->if_prev->if_state;
784 rif->if_state = (WORD)disabled;
790 // .endif, End of conditional assembly block
791 // This is also called by fpop() to pop levels of IFENTs in case a macro or
792 // include file exits early with `exitm' or `end'.
798 if (rif->if_prev == NULL)
799 return error("mismatched .endif");
801 ifent = rif->if_prev;
802 disabled = rif->if_prev->if_state;
803 rif->if_prev = f_ifent;