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 char extra_stuff[] = "extra (unexpected) text found after addressing mode";
43 char * comma_error = "missing comma";
44 char * syntax_error = "syntax error";
45 char * locgl_error = "cannot GLOBL local symbol";
46 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 init_procln(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
131 listflag = 0; // Initialise listing flag
133 loop: // Line processing loop label
135 // Get another line of tokens
136 if (tokln() == TKEOF)
138 if (verb_flag) printf("Assemble: Found TKEOF flag...\n");
139 if (list_flag && listflag) // Flush last line of source
142 if (ifent->if_prev != NULL) // Check conditional token
143 error("hit EOF without finding matching .endif");
148 DEBUG DumpTokenBuffer();
152 if (listflag && listing > 0)
153 listeol(); // Tell listing generator about EOL
155 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
156 listflag = 1; // OK to call `listeol' now
157 just_bss = 0; // Reset just_bss mode
160 state = -3; // No keyword (just EOL)
161 label = NULL; // No label
162 lab_sym = NULL; // No (exported) label
163 equate = NULL; // No equate
164 tk = tok; // Save first token in line
165 pcloc = (VALUE)sloc; // Set beginning-of-line PC
167 loop1: // Internal line processing loop
169 if (*tok == EOL) // Restart loop if end-of-line
172 // First token MUST be a symbol
179 j = (int)tok[2]; // Skip equates (normal statements)
181 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
183 // equate = (char *)tok[1];
184 equate = string[tok[1]];
190 // Skip past label (but record it)
191 if (j == ':' || j == DCOLON)
194 // label = (char *)tok[1]; // Get label name
195 label = string[tok[1]]; // Get label name
196 labtyp = tok[2]; // Get label type
197 tok += 3; // Go to next line token
199 // Handle multiple labels; if there's another label, go process it,
200 // and come back at `as68label' above.
205 if (*tok == SYMBOL && tok[2] == ':')
213 if (*tok == EOL) // EOL is legal here...
216 // Next token MUST be a symbol
217 if (*tok++ != SYMBOL)
223 // This is the problem here: On 64-bit platforms, this cuts the native pointer
224 // in half. We need to figure out how to fix this.
225 //#warning "!!! Bad pointer !!!"
227 opname = p = (char *)*tok++; // Store opcode name here
229 opname = p = string[*tok++];
232 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
233 // On output, `state' will have one of the values:
234 // -3 there was no symbol (EOL)
235 // -2..-1 the symbol didn't match any keyword
236 // 0..499 vanilla directives (dc, ds, etc.)
237 // 500..999 electric directives (macro, rept, etc.)
238 // 1000..+ mnemonics (move, lsr, etc.)
239 for(state=0; state>=0;)
241 j = mnbase[state] + (int)tolowertab[*p];
243 // Reject, character doesn't match
244 if (mncheck[j] != state)
246 state = -1; // No match
250 // Must accept or reject at EOS
253 state = mnaccept[j]; // (-1 on no terminal match)
260 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
265 else if (*tok == DOTL)
267 else if (*tok == DOTB)
270 // Do special directives (500..999) (These must be handled in "real time")
271 if (state >= 500 && state < 1000)
284 case MN_IIF: // .iif --- immediate if
285 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
288 if (!(eattr & DEFINED))
304 case MN_MACRO: // .macro --- macro definition
314 case MN_EXITM: // .exitm --- exit macro
315 case MN_ENDM: // .endm --- same as .exitm
337 error("mis-nested .endr");
344 if (disabled) // Conditionally disabled code
350 j = 0; // Pick global or local sym enviroment
355 sy = lookup(equate, LABEL, j);
359 sy = NewSymbol(equate, LABEL, j);
362 if (equtyp == DEQUALS)
364 // Can't GLOBAL a local symbol
374 else if ((sy->sattr & DEFINED) && equtyp != SET)
376 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
378 sy->sattre |= ~UNDEF_EQUR;
381 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
383 sy->sattre |= ~UNDEF_CC;
388 errors("multiple equate to '%s'", sy->sname);
393 // Put symbol in "order of definition" list
394 if (!(sy->sattr & SDECLLIST))
397 // Parse value to equate symbol to;
401 if (equtyp == EQUREG)
403 // Check that we are in a RISC section
406 error(".equr/.regequ must be defined in .gpu/.dsp section");
410 // Check for register to equate to
411 if ((*tok >= KW_R0) && (*tok <= KW_R31))
413 sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
414 riscreg = (*tok - KW_R0);
415 sy->sattre |= (riscreg << 8); // Store register number
417 if ((tok[1] == ',') && (tok[2] == CONST))
422 registerbank = BANK_0;
424 registerbank = BANK_1;
426 registerbank = BANK_N;
430 registerbank = BANK_N;
433 sy->sattre |= regbank; // Store register bank
434 eattr = ABS | DEFINED | GLOBAL;
435 eval = 0x80000080 + (riscreg) + (registerbank << 8);
438 // Checking for a register symbol
439 else if (tok[0] == SYMBOL)
441 // sy2 = lookup((char *)tok[1], LABEL, j);
442 sy2 = lookup(string[tok[1]], LABEL, j);
444 // Make sure symbol is a valid equreg
445 if (!sy2 || !(sy2->sattre & EQUATEDREG))
447 error("invalid GPU/DSP .equr/.regequ definition");
452 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
453 sy->sattre = sy2->sattre;
454 eval = (sy2->svalue & 0xFFFFF0FF);
460 error("invalid GPU/DSP .equr/.regequ definition");
464 else if (equtyp == REG)
466 if (reglist(&rmask) < 0)
470 eattr = ABS | DEFINED;
472 else if (equtyp == CCDEF)
474 sy->sattre |= EQUATEDCC;
475 eattr = ABS | DEFINED | GLOBAL;
477 if (tok[0] == SYMBOL)
479 // sy2 = lookup((char *)tok[1], LABEL, j);
480 sy2 = lookup(string[tok[1]], LABEL, j);
482 if (!sy2 || !(sy2->sattre & EQUATEDCC))
484 error("invalid gpu/dsp .ccdef definition");
489 eattr = ABS | DEFINED | GLOBAL;
490 sy->sattre = sy2->sattre;
495 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
499 else if (*tok == SYMBOL)
501 // sy2 = lookup((char *)tok[1], LABEL, j);
502 sy2 = lookup(string[tok[1]], LABEL, j);
504 if (sy2 && (sy2->sattre & EQUATEDREG))
506 sy->stype = sy2->stype;
507 sy->sattr = sy2->sattr;
508 sy->sattre = sy2->sattre;
509 sy->svalue = (sy2->svalue & 0xFFFFF0FF);
512 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
515 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
518 if (!(eattr & DEFINED))
524 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
527 if (list_flag) // Put value in listing
530 at_eol(); // Must be at EOL now
538 // Check for dot in front of label; means this is a local label if present
545 j = (*label == '.' ? curenv : 0);
548 sy = lookup(label, LABEL, j);
552 sy = NewSymbol(label, LABEL, j);
554 sy->sattre = RISCSYM;
556 else if (sy->sattr & DEFINED)
558 errors("multiply-defined label '%s'", label);
562 // Put symbol in "order of definition" list
563 if (!(sy->sattr & SDECLLIST))
568 sy->svalue = orgaddr;
569 sy->sattr |= ABS | DEFINED | EQUATED;
574 sy->sattr |= DEFINED | cursect;
583 if (labtyp == DCOLON)
594 // If we're in as68 mode, and there's another label, go back and handle it
595 if (as68_flag && as68mode)
603 // If we are in GPU or DSP mode and still in need of a mnemonic then search
605 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
607 for(state=0, p=opname; state>=0;)
609 j = mrbase[state] + (int)tolowertab[*p];
611 // Reject, character doesn't match
612 if (mrcheck[j] != state)
614 state = -1; // No match
618 // Must accept or reject at EOS
621 state = mraccept[j]; // (-1 on no terminal match)
628 // Call RISC code generator if we found a mnemonic
631 GenerateRISCCode(state);
636 // Invoke macro or complain about bad mnemonic
639 if ((sy = lookup(opname, MACRO, 0)) != NULL)
640 InvokeMacro(sy, siz);
642 errors("unknown op '%s'", opname);
647 // Call directive handlers
650 (*dirtab[state])(siz);
655 // o can't deposit instrs in BSS or ABS
656 // o do automatic .EVEN for instrs
657 // o allocate space for largest possible instr
658 // o can't do ".b" operations with an address register
661 error("cannot initialize non-storage (BSS) section");
665 if (sloc & 1) // Automatic .even
668 if (challoc - ch_size < 18) // Make sure have space in current chunk
671 m = &machtab[state - 1000];
673 // Call special-mode handler
674 if (m->mnattr & CGSPECIAL)
676 (*m->mnfunc)(m->mninst, siz);
680 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
686 amsk0 = amsktab[am0];
687 amsk1 = amsktab[am1];
689 // Catch attempts to use ".B" with an address register (yes, this check
690 // does work at this level)
691 if (siz == SIZB && (am0 == AREG || am1 == AREG))
693 error("cannot use '.b' with an address register");
699 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
701 (*m->mnfunc)(m->mninst, siz);
705 m = &machtab[m->mncont];
711 // .if, Start Conditional Assembly
721 if ((rif = f_ifent) == NULL)
722 rif = (IFENT *)malloc(sizeof(IFENT));
724 f_ifent = rif->if_prev;
726 rif->if_prev = ifent;
731 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
734 if ((eattr & DEFINED) == 0)
735 return error(undef_error);
740 rif->if_state = (WORD)disabled;
746 // .else, Do Alternate Case For .if
752 if (rif->if_prev == NULL)
753 return error("mismatched .else");
756 disabled = rif->if_prev->if_state;
760 rif->if_state = (WORD)disabled;
766 // .endif, End of conditional assembly block
767 // This is also called by fpop() to pop levels of IFENTs in case a macro or
768 // include file exits early with `exitm' or `end'.
774 if (rif->if_prev == NULL)
775 return error("mismatched .endif");
777 ifent = rif->if_prev;
778 disabled = rif->if_prev->if_state;
779 rif->if_prev = f_ifent;