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,
92 // Function prototypes
93 int HandleLabel(char *, int);
97 // Initialize Line Processor
99 void InitLineProcessor(void)
103 f_ifent = ifent0.if_prev = NULL;
113 int state; // Keyword machine state (output)
114 int j; // Random int, must be fast
115 char * p; // Random char ptr, must be fast
116 TOKEN * tk; // First token in line
117 char * label; // Symbol (or NULL)
118 char * equate; // Symbol (or NULL)
119 int labtyp = 0; // Label type (':', DCOLON)
120 int equtyp = 0; // Equ type ('=', DEQUALS)
121 VALUE eval; // Expression value
122 WORD eattr; // Expression attributes
123 SYM * esym; // External symbol involved in expr.
124 WORD siz = 0; // Size suffix to mnem/diretve/macro
125 LONG amsk0, amsk1; // Address-type masks for ea0, ea1
126 MNTAB * m; // Code generation table pointer
127 SYM * sy, * sy2; // Symbol (temp usage)
128 char * opname = NULL; // Name of dirctve/mnemonic/macro
129 int listflag; // 0: Don't call listeol()
130 // int as68mode = 0; // 1: Handle multiple labels
131 WORD rmask; // Register list, for REG
132 int registerbank; // RISC register bank
133 int riscreg; // RISC register
134 listflag = 0; // Initialise listing flag
136 loop: // Line processing loop label
138 // Get another line of tokens
139 if (TokenizeLine() == TKEOF)
141 if (verb_flag) printf("Assemble: Found TKEOF flag...\n");
142 if (list_flag && listflag) // Flush last line of source
145 if (ifent->if_prev != NULL) // Check conditional token
146 error("hit EOF without finding matching .endif");
151 DEBUG DumpTokenBuffer();
155 if (listflag && listing > 0)
156 listeol(); // Tell listing generator about EOL
158 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
159 listflag = 1; // OK to call `listeol' now
160 just_bss = 0; // Reset just_bss mode
163 state = -3; // No keyword (just EOL)
164 label = NULL; // No label
165 lab_sym = NULL; // No (exported) label
166 equate = NULL; // No equate
167 tk = tok; // Save first token in line
168 pcloc = (VALUE)sloc; // Set beginning-of-line PC
170 loop1: // Internal line processing loop
172 if (*tok == EOL) // Restart loop if end-of-line
175 // First token MUST be a symbol
178 // error(syntax_error);
179 error("syntax error; expected symbol");
183 j = (int)tok[2]; // Skip equates (normal statements)
185 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
187 equate = string[tok[1]];
193 // Skip past label (but record it)
194 if (j == ':' || j == DCOLON)
197 // label = (char *)tok[1]; // Get label name
198 label = string[tok[1]]; // Get label name
199 labtyp = tok[2]; // Get label type
200 tok += 3; // Go to next line token
202 // Handle multiple labels; if there's another label, go process it,
203 // and come back at `as68label' above.
208 // Looks like another label follows the previous one, so handle
210 if (*tok == SYMBOL && tok[2] == ':')
214 if (HandleLabel(label, labtyp) != 0)
222 // EOL is legal here...
226 // Next token MUST be a symbol
227 if (*tok++ != SYMBOL)
229 // error(syntax_error);
230 error("syntax error; expected symbol");
234 // This is the problem here: On 64-bit platforms, this cuts the native pointer
235 // in half. We need to figure out how to fix this.
236 //#warning "!!! Bad pointer !!!"
238 opname = p = (char *)*tok++; // Store opcode name here
240 opname = p = string[*tok++];
243 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
244 // On output, `state' will have one of the values:
245 // -3 there was no symbol (EOL)
246 // -2..-1 the symbol didn't match any keyword
247 // 0..499 vanilla directives (dc, ds, etc.)
248 // 500..999 electric directives (macro, rept, etc.)
249 // 1000..+ mnemonics (move, lsr, etc.)
250 for(state=0; state>=0;)
252 j = mnbase[state] + (int)tolowertab[*p];
254 // Reject, character doesn't match
255 if (mncheck[j] != state)
257 state = -1; // No match
261 // Must accept or reject at EOS
264 state = mnaccept[j]; // (-1 on no terminal match)
271 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
276 else if (*tok == DOTL)
278 else if (*tok == DOTB)
281 // Do special directives (500..999) (These must be handled in "real time")
282 if (state >= 500 && state < 1000)
295 case MN_IIF: // .iif --- immediate if
296 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
299 if (!(eattr & DEFINED))
315 case MN_MACRO: // .macro --- macro definition
325 case MN_EXITM: // .exitm --- exit macro
326 case MN_ENDM: // .endm --- same as .exitm
329 // Label on a macro definition is bad mojo... Warn the user
344 // Handle labels on REPT directive lines...
347 if (HandleLabel(label, labtyp) != 0)
358 error("mis-nested .endr");
365 if (disabled) // Conditionally disabled code
371 // Pick global or local symbol enviroment
378 j = (*equate == '.' ? curenv : 0);
380 sy = lookup(equate, LABEL, j);
384 sy = NewSymbol(equate, LABEL, j);
387 if (equtyp == DEQUALS)
389 // Can't GLOBAL a local symbol
399 else if ((sy->sattr & DEFINED) && equtyp != SET)
401 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
403 //REALLY? sy->sattre |= ~UNDEF_EQUR;
404 sy->sattre &= ~UNDEF_EQUR;
407 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
409 //REALLY? sy->sattre |= ~UNDEF_CC;
410 sy->sattre &= ~UNDEF_CC;
415 errors("multiple equate to '%s'", sy->sname);
420 // Put symbol in "order of definition" list
421 if (!(sy->sattr & SDECLLIST))
424 // Parse value to equate symbol to;
428 if (equtyp == EQUREG)
430 //Linko's request to issue a warning on labels that equated to the same register
431 //would go here. Not sure how to implement it though. :-/
434 have an array of bools with 64 entries. Whenever a register is equated, set the
435 corresponding register bool to true. Whenever it's undef'ed, set it to false. When
436 checking to see if it's already been equated, issue a warning.
438 // Check that we are in a RISC section
441 error(".equr/.regequ must be defined in .gpu/.dsp section");
445 // Check for register to equate to
446 if ((*tok >= KW_R0) && (*tok <= KW_R31))
448 sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
449 riscreg = (*tok - KW_R0);
450 //is there any reason to do this, since we're putting this in svalue?
451 //i'm thinking, no. Let's test that out! :-D
452 // sy->sattre |= (riscreg << 8); // Store register number
453 //everything seems to build fine without it... We'll leave it here Just In Case(tm)
455 #define DEBODGE_REGBANK
456 #ifdef DEBODGE_REGBANK
457 // Default is current state of "regbank"
458 registerbank = regbank;
460 // Default is no register bank specified
461 registerbank = BANK_N;
464 // Check for ",<bank #>" override notation
465 if ((tok[1] == ',') && (tok[2] == CONST))
467 // Advance token pointer to the constant
470 // Anything other than a 0 or a 1 will result in "No Bank"
472 registerbank = BANK_0;
474 registerbank = BANK_1;
477 #ifdef DEBODGE_REGBANK
478 sy->sattre |= registerbank; // Store register bank
480 // What needs to happen here is to prime registerbank with regbank, then use
481 // registerbank down below for the bank marking.
482 #warning "!!! regbank <-> registerbank confusion here !!!"
483 // The question here is why, if we're allowed to override the ".regbankN" rules above,
484 // then why is it using the one set by the directive in the extended attributes and
485 // not in what ends up in symbol->svalue?
486 // ".regbankN" is not an original Madmac directive, so it's suspect
487 sy->sattre |= regbank; // Store register bank
489 eattr = ABS | DEFINED | GLOBAL;
490 // & what does this $80000080 constant mean???
491 // eval = 0x80000080 + (riscreg) + (registerbank << 8);
495 // Checking for a register symbol
496 else if (tok[0] == SYMBOL)
498 sy2 = lookup(string[tok[1]], LABEL, j);
500 // Make sure symbol is a valid equreg
501 if (!sy2 || !(sy2->sattre & EQUATEDREG))
503 error("invalid GPU/DSP .equr/.regequ definition");
508 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
509 sy->sattre = sy2->sattre;
510 eval = (sy2->svalue & 0xFFFFF0FF);
516 error("invalid GPU/DSP .equr/.regequ definition");
520 else if (equtyp == REG)
522 if (reglist(&rmask) < 0)
526 eattr = ABS | DEFINED;
528 else if (equtyp == CCDEF)
530 sy->sattre |= EQUATEDCC;
531 eattr = ABS | DEFINED | GLOBAL;
533 if (tok[0] == SYMBOL)
535 sy2 = lookup(string[tok[1]], LABEL, j);
537 if (!sy2 || !(sy2->sattre & EQUATEDCC))
539 error("invalid gpu/dsp .ccdef definition");
544 eattr = ABS | DEFINED | GLOBAL;
545 sy->sattre = sy2->sattre;
550 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
554 else if (*tok == SYMBOL)
556 sy2 = lookup(string[tok[1]], LABEL, j);
558 if (sy2 && (sy2->sattre & EQUATEDREG))
560 sy->stype = sy2->stype;
561 sy->sattr = sy2->sattr;
562 sy->sattre = sy2->sattre;
563 //ICK sy->svalue = (sy2->svalue & 0xFFFFF0FF);
564 sy->svalue = sy2->svalue;
567 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
570 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
573 if (!(eattr & DEFINED))
579 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
582 if (list_flag) // Put value in listing
585 at_eol(); // Must be at EOL now
594 // Check for dot in front of label; means this is a local label if present
595 j = (*label == '.' ? curenv : 0);
596 sy = lookup(label, LABEL, j);
600 sy = NewSymbol(label, LABEL, j);
602 sy->sattre = RISCSYM;
604 else if (sy->sattr & DEFINED)
606 errors("multiply-defined label '%s'", label);
610 // Put symbol in "order of definition" list
611 if (!(sy->sattr & SDECLLIST))
616 sy->svalue = orgaddr;
617 sy->sattr |= ABS | DEFINED | EQUATED;
622 sy->sattr |= DEFINED | cursect;
631 if (labtyp == DCOLON)
642 // Non-zero == error occurred
643 if (HandleLabel(label, labtyp) != 0)
646 // If we're in as68 mode, and there's another label, go back and handle it
647 // if (as68_flag && as68mode)
655 // If we are in GPU or DSP mode and still in need of a mnemonic then search
657 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
659 for(state=0, p=opname; state>=0;)
661 j = mrbase[state] + (int)tolowertab[*p];
663 // Reject, character doesn't match
664 if (mrcheck[j] != state)
666 state = -1; // No match
670 // Must accept or reject at EOS
673 state = mraccept[j]; // (-1 on no terminal match)
680 // Call RISC code generator if we found a mnemonic
683 GenerateRISCCode(state);
688 // Invoke macro or complain about bad mnemonic
691 if ((sy = lookup(opname, MACRO, 0)) != NULL)
692 InvokeMacro(sy, siz);
694 errors("unknown op '%s'", opname);
699 // Call directive handlers
702 (*dirtab[state])(siz);
707 // o can't deposit instrs in BSS or ABS
708 // o do automatic .EVEN for instrs
709 // o allocate space for largest possible instr
710 // o can't do ".b" operations with an address register
713 error("cannot initialize non-storage (BSS) section");
717 if (sloc & 1) // Automatic .even
720 if (challoc - ch_size < 18) // Make sure have space in current chunk
723 m = &machtab[state - 1000];
725 // Call special-mode handler
726 if (m->mnattr & CGSPECIAL)
728 (*m->mnfunc)(m->mninst, siz);
732 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
738 amsk0 = amsktab[am0];
739 amsk1 = amsktab[am1];
741 // Catch attempts to use ".B" with an address register (yes, this check
742 // does work at this level)
743 if (siz == SIZB && (am0 == AREG || am1 == AREG))
745 error("cannot use '.b' with an address register");
751 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
753 (*m->mnfunc)(m->mninst, siz);
757 m = &machtab[m->mncont];
763 // Handle the creation of labels
765 int HandleLabel(char * label, int labelType)
767 // Check for dot in front of label; means this is a local label if present
768 int j = (*label == '.' ? curenv : 0);
769 SYM * sy = lookup(label, LABEL, j);
773 sy = NewSymbol(label, LABEL, j);
775 sy->sattre = RISCSYM;
777 else if (sy->sattr & DEFINED)
778 return errors("multiply-defined label '%s'", label);
780 // Put symbol in "order of definition" list
781 if (!(sy->sattr & SDECLLIST))
786 sy->svalue = orgaddr;
787 sy->sattr |= ABS | DEFINED | EQUATED;
792 sy->sattr |= DEFINED | cursect;
800 // Make label global if it has a double colon
801 if (labelType == DCOLON)
804 return error(locgl_error);
814 // .if, Start Conditional Assembly
824 if ((rif = f_ifent) == NULL)
825 rif = (IFENT *)malloc(sizeof(IFENT));
827 f_ifent = rif->if_prev;
829 rif->if_prev = ifent;
834 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
837 if ((eattr & DEFINED) == 0)
838 return error(undef_error);
843 rif->if_state = (WORD)disabled;
849 // .else, Do Alternate Case For .if
855 if (rif->if_prev == NULL)
856 return error("mismatched .else");
859 disabled = rif->if_prev->if_state;
863 rif->if_state = (WORD)disabled;
869 // .endif, End of conditional assembly block
870 // This is also called by fpop() to pop levels of IFENTs in case a macro or
871 // include file exits early with `exitm' or `end'.
877 if (rif->if_prev == NULL)
878 return error("mismatched .endif");
880 ifent = rif->if_prev;
881 disabled = rif->if_prev->if_state;
882 rif->if_prev = f_ifent;