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
32 IFENT *ifent; // Current ifent
33 static IFENT ifent0; // Root ifent
34 static IFENT *f_ifent; // Freelist of ifents
35 static int disabled; // Assembly conditionally disabled
36 int just_bss; // 1, ds.b in microprocessor mode
37 VALUE pcloc; // Value of "PC" at beginning of line
38 IFENT *ifent; // Current ifent
39 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,
92 // --- Initialize Line Processor -------------------------------------------------------------------
95 void init_procln(void) {
98 f_ifent = ifent0.if_prev = NULL;
103 // --- Line Processor ------------------------------------------------------------------------------
106 void assemble(void) {
107 int state; // Keyword machine state (output)
108 int j; // Random int, must be fast
109 char *p; // Random char ptr, must be fast
110 TOKEN *tk; // First token in line
111 char *label; // Symbol (or NULL)
112 char *equate; // Symbol (or NULL)
113 int labtyp = 0; // Label type (':', DCOLON)
114 int equtyp = 0; // Equ type ('=', DEQUALS)
115 VALUE eval; // Expression value
116 WORD eattr; // Expression attributes
117 SYM *esym; // External symbol involved in expr.
118 WORD siz = 0; // Size suffix to mnem/diretve/macro
119 LONG amsk0, amsk1; // Address-type masks for ea0, ea1
120 MNTAB *m; // Code generation table pointer
121 SYM *sy, *sy2; // Symbol (temp usage)
122 char *opname = NULL; // Name of dirctve/mnemonic/macro
123 int listflag; // 0: Don't call listeol()
124 int as68mode = 0; // 1: Handle multiple labels
125 WORD rmask; // Register list, for REG
126 int registerbank; // RISC register bank
127 int riscreg; // RISC register
129 listflag = 0; // Initialise listing flag
131 loop: // Line processing loop label
133 if(tokln() == TKEOF) { // Get another line of tokens
134 if(list_flag && listflag) // Flush last line of source
136 if(ifent->if_prev != NULL) // Check conditional token
137 error("hit EOF without finding matching .endif");
142 if(listflag && listing > 0) listeol(); // Tell listing generator about EOL
143 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
144 listflag = 1; // OK to call `listeol' now
145 just_bss = 0; // Reset just_bss mode
148 state = -3; // No keyword (just EOL)
149 label = NULL; // No label
150 lab_sym = NULL; // No (exported) label
151 equate = NULL; // No equate
152 tk = tok; // Save first token in line
153 pcloc = (VALUE)sloc; // Set beginning-of-line PC
155 loop1: // Internal line processing loop
157 if(*tok == EOL) // Restart loop if end-of-line
160 if(*tok != SYMBOL) { // First token MUST be a symbol
165 j = (int)tok[2]; // Skip equates (normal statements)
166 if(j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF) {
167 equate = (char *)tok[1];
173 if(j == ':' || j == DCOLON) { // Skip past label (but record it)
177 label = (char *)tok[1]; // Get label name
178 labtyp = tok[2]; // Get label type
179 tok += 3; // Go to next line token
181 // Handle multiple labels; if there's another label, go process it,
182 // and come back at `as68label' above.
185 if(*tok == SYMBOL && tok[2] == ':') {
192 if(*tok == EOL) // EOL is legal here...
195 if(*tok++ != SYMBOL) { // Next token MUST be a symbol
199 opname = p = (char *)*tok++; // Store opcode name here
201 // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
202 // On output, `state' will have one of the values:
203 // -3 there was no symbol (EOL)
204 // -2..-1 the symbol didn't match any keyword
205 // 0..499 vanilla directives (dc, ds, etc.)
206 // 500..999 electric directives (macro, rept, etc.)
207 // 1000..+ mnemonics (move, lsr, etc.)
208 for(state = 0; state >= 0;) {
209 j = mnbase[state] + (int)tolowertab[*p];
210 if(mncheck[j] != state) { // Reject, character doesn't match
211 state = -1; // No match
214 if(!*++p) { // Must accept or reject at EOS
215 state = mnaccept[j]; // (-1 on no terminal match)
221 // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
225 else if(*tok == DOTL)
227 else if(*tok == DOTB)
230 // Do special directives (500..999) (These must be handled in "real time")
231 if(state >= 500 && state < 1000)
242 case MN_IIF: // .iif --- immediate if
243 if(disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
245 if(!(eattr & DEFINED)) {
256 case MN_MACRO: // .macro --- macro definition
263 case MN_EXITM: // .exitm --- exit macro
264 case MN_ENDM: // .endm --- same as .exitm
280 error("mis-nested .endr");
286 if(disabled) // Conditionally disabled code
291 j = 0; // Pick global or local sym enviroment
295 sy = lookup(equate, LABEL, j);
297 sy = newsym(equate, LABEL, j);
299 if(equtyp == DEQUALS) {
300 if(j) { // Can't GLOBAL a local symbol
306 } else if((sy->sattr & DEFINED) && equtyp != SET) {
307 if((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR)) {
308 sy->sattre |= ~UNDEF_EQUR;
310 } else if((equtyp == CCDEF) && (sy->sattre & UNDEF_CC)) {
311 sy->sattre |= ~UNDEF_CC;
314 errors("multiple equate to '%s'", sy->sname);
319 // Put symbol in "order of definition" list
320 if(!(sy->sattr & SDECLLIST)) sym_decl(sy);
322 // Parse value to equate symbol to;
326 if(equtyp == EQUREG) {
327 if(!rgpu && !rdsp) { // Check that we are in a RISC section
328 error(".equr/.regequ must be defined in .gpu/.dsp section");
331 if((*tok >= KW_R0) && (*tok <= KW_R31)) { // Check for register to equate to
332 sy->sattre = EQUATEDREG | RISCSYM; // Mark as equated register
333 riscreg = (*tok - KW_R0);
334 sy->sattre |= (riscreg << 8); // Store register number
335 if((tok[1] == ',') && (tok[2] == CONST)) {
337 if(*tok == 0) registerbank = BANK_0;
338 else if(*tok == 1) registerbank = BANK_1;
339 else registerbank = BANK_N;
341 registerbank = BANK_N;
343 sy->sattre |= regbank; // Store register bank
344 eattr = ABS | DEFINED | GLOBAL;
345 eval = 0x80000080 + (riscreg) + (registerbank << 8);
347 } else if(tok[0] == SYMBOL) { // Checking for a register symbol
348 sy2 = lookup((char *)tok[1], LABEL, j);
349 if(!sy2 || !(sy2->sattre & EQUATEDREG)) { // Make sure symbol is a valid equreg
350 error("invalid GPU/DSP .equr/.regequ definition");
353 eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
354 sy->sattre = sy2->sattre;
355 eval = (sy2->svalue & 0xFFFFF0FF);
359 error("invalid GPU/DSP .equr/.regequ definition");
362 } else if(equtyp == REG) {
363 if(reglist(&rmask) < 0)
366 eattr = ABS | DEFINED;
367 } else if(equtyp == CCDEF) {
368 sy->sattre |= EQUATEDCC;
369 eattr = ABS | DEFINED | GLOBAL;
370 if(tok[0] == SYMBOL) {
371 sy2 = lookup((char *)tok[1], LABEL, j);
372 if(!sy2 || !(sy2->sattre & EQUATEDCC)) {
373 error("invalid gpu/dsp .ccdef definition");
376 eattr = ABS | DEFINED | GLOBAL;
377 sy->sattre = sy2->sattre;
382 if(expr(exprbuf, &eval, &eattr, &esym) != OK)
384 } else if(*tok == SYMBOL) { //equ a equr
385 sy2 = lookup((char *)tok[1], LABEL, j);
386 if(sy2 && (sy2->sattre & EQUATEDREG)) {
387 sy->stype = sy2->stype;
388 sy->sattr = sy2->sattr;
389 sy->sattre = sy2->sattre;
390 sy->svalue = (sy2->svalue & 0xFFFFF0FF);
393 if(expr(exprbuf, &eval, &eattr, &esym) != OK)
396 if(expr(exprbuf, &eval, &eattr, &esym) != OK)
399 if(!(eattr & DEFINED)) {
405 sy->sattr |= eattr | EQUATED; // Symbol inherits value and attributes
407 if(list_flag) // Put value in listing
410 at_eol(); // Must be at EOL now
422 sy = lookup(label, LABEL, j);
425 sy = newsym(label, LABEL, j);
427 sy->sattre = RISCSYM;
428 } else if(sy->sattr & DEFINED) {
429 errors("multiply-defined label '%s'", label);
433 // Put symbol in "order of definition" list
434 if(!(sy->sattr & SDECLLIST)) sym_decl(sy);
437 sy->svalue = orgaddr;
438 sy->sattr |= ABS | DEFINED | EQUATED;
441 sy->sattr |= DEFINED | cursect;
448 if(labtyp == DCOLON) { // Make label global
456 // If we're in as68 mode, and there's another label, go back and handle it
457 if(as68_flag && as68mode)
465 // If we are in GPU or DSP mode and still in need of a mnemonic then search for one
466 if((rgpu || rdsp) && (state < 0 || state >= 1000)) {
467 for(state = 0, p = opname; state >= 0;) {
468 j = mrbase[state] + (int)tolowertab[*p];
469 if(mrcheck[j] != state) { // Reject, character doesn't match
470 state = -1; // No match
474 if(!*++p) { // Must accept or reject at EOS
475 state = mraccept[j]; // (-1 on no terminal match)
481 // Call RISC code generator if we found a mnemonic
488 // Invoke macro or complain about bad mnemonic
490 if((sy = lookup(opname, MACRO, 0)) != NULL)
492 else errors("unknown op '%s'", opname);
496 // Call directive handlers
498 (*dirtab[state])(siz);
503 // o can't deposit instrs in BSS or ABS
504 // o do automatic .EVEN for instrs
505 // o allocate space for largest possible instr
506 // o can't do ".b" operations with an address register
508 error("cannot initialize non-storage (BSS) section");
512 if(sloc & 1) // Automatic .even
515 if(challoc - ch_size < 18) // Make sure have space in current chunk
518 m = &machtab[state - 1000];
519 if(m->mnattr & CGSPECIAL) { // Call special-mode handler
520 (*m->mnfunc)(m->mninst, siz);
524 if(amode(1) < 0) // Parse 0, 1 or 2 addr modes
530 amsk0 = amsktab[am0];
531 amsk1 = amsktab[am1];
533 // Catch attempts to use ".B" with an address register (yes, this check does work at this level)
534 if(siz == SIZB && (am0 == AREG || am1 == AREG)) {
535 error("cannot use '.b' with an address register");
540 if((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0) {
541 (*m->mnfunc)(m->mninst, siz);
544 m = &machtab[m->mncont];
549 // --- .if, Start Conditional Assembly -------------------------------------------------------------
559 if((rif = f_ifent) == NULL) rif = (IFENT *)amem((LONG)sizeof(IFENT));
560 else f_ifent = rif->if_prev;
562 rif->if_prev = ifent;
566 if(expr(exprbuf, &eval, &eattr, &esym) != OK) return(0);
567 if((eattr & DEFINED) == 0) return(error(undef_error));
570 rif->if_state = (WORD)disabled;
575 // --- .else, Do Alternate Case For .if ------------------------------------------------------------
583 if(rif->if_prev == NULL) return(error("mismatched .else"));
585 if(disabled) disabled = rif->if_prev->if_state;
588 rif->if_state = (WORD)disabled;
593 // -------------------------------------------------------------------------------------------------
594 // .endif, End of conditional assembly block
595 // This is also called by fpop() to pop levels of IFENTs in case a macro or include file exits
596 // early with `exitm' or `end'.
597 // -------------------------------------------------------------------------------------------------
604 if(rif->if_prev == NULL) return(error("mismatched .endif"));
606 ifent = rif->if_prev;
607 disabled = rif->if_prev->if_state;
608 rif->if_prev = f_ifent;