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 SYM * lab_sym; // Label on line (or NULL)
41 const char extra_stuff[] = "extra (unexpected) text found after addressing mode";
42 const char comma_error[] = "missing comma";
43 const char syntax_error[] = "syntax error";
44 const char locgl_error[] = "cannot GLOBL local symbol";
45 const char lab_ignored[] = "label ignored";
47 // Table to convert an addressing-mode number to a bitmask.
48 LONG amsktab[0112] = {
49 M_DREG, M_DREG, M_DREG, M_DREG,
50 M_DREG, M_DREG, M_DREG, M_DREG,
52 M_AREG, M_AREG, M_AREG, M_AREG,
53 M_AREG, M_AREG, M_AREG, M_AREG,
55 M_AIND, M_AIND, M_AIND, M_AIND,
56 M_AIND, M_AIND, M_AIND, M_AIND,
58 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
59 M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
61 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
62 M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
64 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
65 M_ADISP, M_ADISP, M_ADISP, M_ADISP,
67 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
68 M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
91 // Function prototypes
92 int HandleLabel(char *, int);
96 // Initialize line processor
98 void InitLineProcessor(void)
102 f_ifent = ifent0.if_prev = NULL;
112 int state; // Keyword machine state (output)
113 int j; // Random int, must be fast
114 char * p; // Random char ptr, must be fast
115 TOKEN * tk; // First token in line
116 char * label; // Symbol (or NULL)
117 char * equate; // Symbol (or NULL)
118 int labtyp = 0; // Label type (':', DCOLON)
119 int equtyp = 0; // Equ type ('=', DEQUALS)
120 VALUE eval; // Expression value
121 WORD eattr; // Expression attributes
122 SYM * esym; // External symbol involved in expr.
123 WORD siz = 0; // Size suffix to mnem/diretve/macro
124 LONG amsk0, amsk1; // Address-type masks for ea0, ea1
125 MNTAB * m; // Code generation table pointer
126 SYM * sy, * sy2; // Symbol (temp usage)
127 char * opname = NULL; // Name of dirctve/mnemonic/macro
128 int listflag; // 0: Don't call listeol()
129 WORD rmask; // Register list, for REG
130 int registerbank; // RISC register bank
131 int riscreg; // RISC register
132 listflag = 0; // Initialise listing flag
134 loop: // Line processing loop label
136 // Get another line of tokens
137 if (TokenizeLine() == TKEOF)
139 if (debug) printf("Assemble: Found TKEOF flag...\n");
140 if (list_flag && listflag) // Flush last line of source
143 if (ifent->if_prev != NULL) // Check conditional token
144 error("hit EOF without finding matching .endif");
149 DEBUG DumpTokenBuffer();
153 if (listflag && listing > 0)
154 listeol(); // Tell listing generator about EOL
156 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
157 listflag = 1; // OK to call `listeol' now
158 just_bss = 0; // Reset just_bss mode
161 state = -3; // No keyword (just EOL)
162 label = NULL; // No label
163 lab_sym = NULL; // No (exported) label
164 equate = NULL; // No equate
165 tk = tok; // Save first token in line
166 pcloc = (VALUE)sloc; // Set beginning-of-line PC
168 loop1: // Internal line processing loop
170 if (*tok == EOL) // Restart loop if end-of-line
173 // First token MUST be a symbol (Shamus: not sure why :-/)
176 error("syntax error; expected symbol");
180 j = (int)tok[2]; // Skip equates (normal statements)
182 if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
184 equate = string[tok[1]];
190 // Skip past label (but record it)
191 if (j == ':' || j == DCOLON)
194 label = string[tok[1]]; // Get label name
195 labtyp = tok[2]; // Get label type
196 tok += 3; // Go to next line token
199 // Looks like another label follows the previous one, so handle
200 // the previous one until there aren't any more
201 if (as68_flag && (*tok == SYMBOL && tok[2] == ':'))
203 if (HandleLabel(label, labtyp) != 0)
210 // EOL is legal here...
214 // Next token MUST be a symbol
215 if (*tok++ != SYMBOL)
217 error("syntax error; expected symbol");
221 opname = p = string[*tok++];
223 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
224 // On output, `state' will have one of the values:
225 // -3 there was no symbol (EOL)
226 // -2..-1 the symbol didn't match any keyword
227 // 0..499 vanilla directives (dc, ds, etc.)
228 // 500..999 electric directives (macro, rept, etc.)
229 // 1000..+ mnemonics (move, lsr, etc.)
230 for(state=0; state>=0;)
232 j = mnbase[state] + (int)tolowertab[*p];
234 // Reject, character doesn't match
235 if (mncheck[j] != state)
237 state = -1; // No match
241 // Must accept or reject at EOS
244 state = mnaccept[j]; // (-1 on no terminal match)
251 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
256 else if (*tok == DOTL)
258 else if (*tok == DOTB)
261 // Do special directives (500..999) (These must be handled in "real time")
262 if (state >= 500 && state < 1000)
275 case MN_IIF: // .iif --- immediate if
276 if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
279 if (!(eattr & DEFINED))
295 case MN_MACRO: // .macro --- macro definition
298 // Label on a macro definition is bad mojo... Warn the user
306 case MN_EXITM: // .exitm --- exit macro
307 case MN_ENDM: // .endm --- same as .exitm
320 // Handle labels on REPT directive lines...
323 if (HandleLabel(label, labtyp) != 0)
333 error("mis-nested .endr");
340 if (disabled) // Conditionally disabled code
346 // Pick global or local symbol enviroment
347 j = (*equate == '.' ? curenv : 0);
348 sy = lookup(equate, LABEL, j);
352 sy = NewSymbol(equate, LABEL, j);
355 if (equtyp == DEQUALS)
357 // Can't GLOBAL a local symbol
367 else if ((sy->sattr & DEFINED) && equtyp != SET)
369 if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
371 //REALLY? sy->sattre |= ~UNDEF_EQUR;
372 sy->sattre &= ~UNDEF_EQUR;
375 else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
377 //REALLY? sy->sattre |= ~UNDEF_CC;
378 sy->sattre &= ~UNDEF_CC;
383 errors("multiple equate to '%s'", sy->sname);
388 // Put symbol in "order of definition" list if it's not already there
389 AddToSymbolDeclarationList(sy);
391 // Parse value to equate symbol to;
395 if (equtyp == EQUREG)
397 //Linko's request to issue a warning on labels that equated to the same
398 //register would go here. Not sure how to implement it though. :-/
401 have an array of bools with 64 entries. Whenever a register is equated, set the
402 corresponding register bool to true. Whenever it's undef'ed, set it to false.
403 When checking to see if it's already been equated, issue a warning.
405 // Check that we are in a RISC section
408 error(".equr/.regequ must be defined in .gpu/.dsp section");
412 // Check for register to equate to
413 if ((*tok >= KW_R0) && (*tok <= KW_R31))
415 // sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
416 sy->sattre = EQUATEDREG; // Mark as equated register
417 riscreg = (*tok - KW_R0);
418 //is there any reason to do this, since we're putting this in svalue?
419 //i'm thinking, no. Let's test that out! :-D
420 // sy->sattre |= (riscreg << 8); // Store register number
421 //everything seems to build fine without it... We'll leave it here Just In Case(tm)
423 #define DEBODGE_REGBANK
424 #ifdef DEBODGE_REGBANK
425 // Default is current state of "regbank"
426 registerbank = regbank;
428 // Default is no register bank specified
429 registerbank = BANK_N;
432 // Check for ",<bank #>" override notation
433 if ((tok[1] == ',') && (tok[2] == CONST))
435 // Advance token pointer to the constant
438 // Anything other than a 0 or a 1 will result in "No Bank"
440 registerbank = BANK_0;
442 registerbank = BANK_1;
445 #ifdef DEBODGE_REGBANK
446 sy->sattre |= registerbank; // Store register bank
448 // What needs to happen here is to prime registerbank with regbank, then use
449 // registerbank down below for the bank marking.
450 #warning "!!! regbank <-> registerbank confusion here !!!"
451 // The question here is why, if we're allowed to override the ".regbankN" rules
452 // above, then why is it using the one set by the directive in the extended
453 // attributes and not in what ends up in symbol->svalue?
454 // ".regbankN" is not an original Madmac directive, so it's suspect
455 sy->sattre |= regbank; // Store register bank
457 eattr = ABS | DEFINED | GLOBAL;
458 // & what does this $80000080 constant mean???
459 // eval = 0x80000080 + (riscreg) + (registerbank << 8);
463 // Checking for a register symbol
464 else if (tok[0] == SYMBOL)
466 sy2 = lookup(string[tok[1]], LABEL, j);
468 // Make sure symbol is a valid equreg
469 if (!sy2 || !(sy2->sattre & EQUATEDREG))
471 error("invalid GPU/DSP .equr/.regequ definition");
476 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
477 sy->sattre = sy2->sattre;
478 eval = (sy2->svalue & 0xFFFFF0FF);
484 error("invalid GPU/DSP .equr/.regequ definition");
488 else if (equtyp == REG)
490 if (reglist(&rmask) < 0)
494 eattr = ABS | DEFINED;
496 else if (equtyp == CCDEF)
498 sy->sattre |= EQUATEDCC;
499 eattr = ABS | DEFINED | GLOBAL;
501 if (tok[0] == SYMBOL)
503 sy2 = lookup(string[tok[1]], LABEL, j);
505 if (!sy2 || !(sy2->sattre & EQUATEDCC))
507 error("invalid gpu/dsp .ccdef definition");
512 eattr = ABS | DEFINED | GLOBAL;
513 sy->sattre = sy2->sattre;
518 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
522 else if (*tok == SYMBOL)
524 sy2 = lookup(string[tok[1]], LABEL, j);
526 if (sy2 && (sy2->sattre & EQUATEDREG))
528 sy->stype = sy2->stype;
529 sy->sattr = sy2->sattr;
530 sy->sattre = sy2->sattre;
531 //ICK sy->svalue = (sy2->svalue & 0xFFFFF0FF);
532 sy->svalue = sy2->svalue;
535 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
538 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
541 if (!(eattr & DEFINED))
547 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
550 if (list_flag) // Put value in listing
553 at_eol(); // Must be at EOL now
560 // Non-zero == error occurred
561 if (HandleLabel(label, labtyp) != 0)
569 // If we are in GPU or DSP mode and still in need of a mnemonic then search
571 if ((rgpu || rdsp) && (state < 0 || state >= 1000))
573 for(state=0, p=opname; state>=0;)
575 j = mrbase[state] + (int)tolowertab[*p];
577 // Reject, character doesn't match
578 if (mrcheck[j] != state)
580 state = -1; // No match
584 // Must accept or reject at EOS
587 state = mraccept[j]; // (-1 on no terminal match)
594 // Call RISC code generator if we found a mnemonic
597 GenerateRISCCode(state);
602 // Invoke macro or complain about bad mnemonic
605 if ((sy = lookup(opname, MACRO, 0)) != NULL)
606 InvokeMacro(sy, siz);
608 errors("unknown op '%s'", opname);
613 // Call directive handlers
616 (*dirtab[state])(siz);
621 // o can't deposit instrs in BSS or ABS
622 // o do automatic .EVEN for instrs
623 // o allocate space for largest possible instr
624 // o can't do ".b" operations with an address register
627 error("cannot initialize non-storage (BSS) section");
631 if (sloc & 1) // Automatic .even
634 if (challoc - ch_size < 18) // Make sure have space in current chunk
637 m = &machtab[state - 1000];
639 // Call special-mode handler
640 if (m->mnattr & CGSPECIAL)
642 (*m->mnfunc)(m->mninst, siz);
646 if (amode(1) < 0) // Parse 0, 1 or 2 addr modes
652 amsk0 = amsktab[am0];
653 amsk1 = amsktab[am1];
655 // Catch attempts to use ".B" with an address register (yes, this check
656 // does work at this level)
657 if (siz == SIZB && (am0 == AREG || am1 == AREG))
659 error("cannot use '.b' with an address register");
665 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
667 (*m->mnfunc)(m->mninst, siz);
671 m = &machtab[m->mncont];
677 // Handle the creation of labels
679 int HandleLabel(char * label, int labelType)
681 // Check for dot in front of label; means this is a local label if present
682 int environment = (*label == '.' ? curenv : 0);
683 SYM * symbol = lookup(label, LABEL, environment);
687 symbol = NewSymbol(label, LABEL, environment);
689 // symbol->sattre = RISCSYM;
692 else if (symbol->sattr & DEFINED)
693 return errors("multiply-defined label '%s'", label);
695 // Put symbol in "order of definition" list if it's not already in it
696 AddToSymbolDeclarationList(symbol);
700 symbol->svalue = orgaddr;
701 symbol->sattr |= ABS | DEFINED | EQUATED;
705 symbol->svalue = sloc;
706 symbol->sattr |= DEFINED | cursect;
711 if (0 == environment)
714 // Make label global if it has a double colon
715 if (labelType == DCOLON)
717 if (environment != 0)
718 return error(locgl_error);
720 symbol->sattr |= GLOBAL;
728 // .if, Start conditional assembly
738 if ((rif = f_ifent) == NULL)
739 rif = (IFENT *)malloc(sizeof(IFENT));
741 f_ifent = rif->if_prev;
743 rif->if_prev = ifent;
748 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
751 if ((eattr & DEFINED) == 0)
752 return error(undef_error);
757 rif->if_state = (WORD)disabled;
763 // .else, Do alternate case for .if
769 if (rif->if_prev == NULL)
770 return error("mismatched .else");
773 disabled = rif->if_prev->if_state;
777 rif->if_state = (WORD)disabled;
783 // .endif, End of conditional assembly block
784 // This is also called by fpop() to pop levels of IFENTs in case a macro or
785 // include file exits early with `exitm' or `end'.
791 if (rif->if_prev == NULL)
792 return error("mismatched .endif");
794 ifent = rif->if_prev;
795 disabled = rif->if_prev->if_state;
796 rif->if_prev = f_ifent;