Code cleanup, version bump for last commit. :-)
[rmac] / procln.c
1 //
2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // PROCLN.C - Line Processing
4 // Copyright (C) 199x Landon Dyer, 2017 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
7 //
8
9 #include "procln.h"
10 #include "6502.h"
11 #include "amode.h"
12 #include "direct.h"
13 #include "error.h"
14 #include "expr.h"
15 #include "listing.h"
16 #include "mach.h"
17 #include "macro.h"
18 #include "riscasm.h"
19 #include "sect.h"
20 #include "symbol.h"
21
22 #define DEF_KW                                  // Declare keyword values
23 #include "kwtab.h"                              // Incl generated keyword tables & defs
24
25 #define DEF_MN                                  // Incl 68k keyword definitions
26 #define DECL_MN                                 // Incl 68k keyword state machine tables
27 #include "mntab.h"
28
29 #define DEF_MR
30 #define DECL_MR
31 #include "risckw.h"
32
33 #define DEF_MP                                  // Include 6502 keyword definitions
34 #define DECL_MP                                 // Include 6502 keyword state machine tables
35 #include "6502kw.h"
36
37 IFENT * ifent;                                  // Current ifent
38 static IFENT ifent0;                    // Root ifent
39 static IFENT * f_ifent;                 // Freelist of ifents
40 static int disabled;                    // Assembly conditionally disabled
41 int just_bss;                                   // 1, ds.b in microprocessor mode
42 VALUE pcloc;                                    // Value of "PC" at beginning of line
43 SYM * lab_sym;                                  // Label on line (or NULL)
44
45 const char extra_stuff[] = "extra (unexpected) text found after addressing mode";
46 const char comma_error[] = "missing comma";
47 const char syntax_error[] = "syntax error";
48 const char locgl_error[] = "cannot GLOBL local symbol";
49 const char lab_ignored[] = "label ignored";
50
51 // Table to convert an addressing-mode number to a bitmask.
52 LONG amsktab[0112] = {
53         M_DREG, M_DREG, M_DREG, M_DREG,
54         M_DREG, M_DREG, M_DREG, M_DREG,
55
56         M_AREG, M_AREG, M_AREG, M_AREG,
57         M_AREG, M_AREG, M_AREG, M_AREG,
58
59         M_AIND, M_AIND, M_AIND, M_AIND,
60         M_AIND, M_AIND, M_AIND, M_AIND,
61
62         M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
63         M_APOSTINC, M_APOSTINC, M_APOSTINC, M_APOSTINC,
64
65         M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
66         M_APREDEC, M_APREDEC, M_APREDEC, M_APREDEC,
67
68         M_ADISP, M_ADISP, M_ADISP, M_ADISP,
69         M_ADISP, M_ADISP, M_ADISP, M_ADISP,
70
71         M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
72         M_AINDEXED, M_AINDEXED, M_AINDEXED, M_AINDEXED,
73
74         M_ABSW,                                                                                 // 070
75         M_ABSL,                                                                                 // 071
76         M_PCDISP,                                                                               // 072
77         M_PCINDEXED,                                                                    // 073
78         M_IMMED,                                                                                // 074
79         0L,                                                                                             // 075
80         0L,                                                                                             // 076
81         0L,                                                                                             // 077
82         M_ABASE,                                                                                // 0100
83         M_MEMPOST,                                                                              // 0101
84         M_MEMPRE,                                                                               // 0102
85         M_PCBASE,                                                                               // 0103
86         M_PCMPOST,                                                                              // 0104
87         M_PCMPRE,                                                                               // 0105
88         M_AM_USP,                                                                               // 0106
89         M_AM_SR,                                                                                // 0107
90         M_AM_CCR,                                                                               // 0110
91         M_AM_NONE                                                                               // 0111
92 };                                                                                                      // 0112 length
93
94
95 // Function prototypes
96 int HandleLabel(char *, int);
97
98
99 //
100 // Initialize line processor
101 //
102 void InitLineProcessor(void)
103 {
104         disabled = 0;
105         ifent = &ifent0;
106         f_ifent = ifent0.if_prev = NULL;
107         ifent0.if_state = 0;
108 }
109
110
111 //
112 // Line processor
113 //
114 void Assemble(void)
115 {
116         int state;                                      // Keyword machine state (output)
117         int j;                                          // Random int, must be fast
118         char * p;                                       // Random char ptr, must be fast
119         TOKEN * tk;                                     // First token in line
120         char * label;                           // Symbol (or NULL)
121         char * equate;                          // Symbol (or NULL)
122         int labtyp = 0;                         // Label type (':', DCOLON)
123         int equtyp = 0;                         // Equ type ('=', DEQUALS)
124         VALUE eval;                                     // Expression value
125         WORD eattr;                                     // Expression attributes
126         SYM * esym;                                     // External symbol involved in expr.
127         WORD siz = 0;                           // Size suffix to mnem/diretve/macro
128         LONG amsk0, amsk1;                      // Address-type masks for ea0, ea1
129         MNTAB * m;                                      // Code generation table pointer
130         SYM * sy, * sy2;                        // Symbol (temp usage)
131         char * opname = NULL;           // Name of dirctve/mnemonic/macro
132         int listflag;                           // 0: Don't call listeol()
133         WORD rmask;                                     // Register list, for REG
134         int registerbank;                       // RISC register bank
135         int riscreg;                            // RISC register
136         listflag = 0;                           // Initialise listing flag
137
138 loop:                                                   // Line processing loop label
139
140         // Get another line of tokens
141         if (TokenizeLine() == TKEOF)
142         {
143 if (debug) printf("Assemble: Found TKEOF flag...\n");
144                 if (list_flag && listflag)                      // Flush last line of source
145                         listeol();
146
147                 if (ifent->if_prev != NULL)                     // Check conditional token
148                         error("hit EOF without finding matching .endif");
149
150                 return;
151         }
152
153         DEBUG DumpTokenBuffer();
154
155         if (list_flag)
156         {
157                 if (listflag && listing > 0)
158                         listeol();                                              // Tell listing generator about EOL
159
160                 lstout((char)(disabled ? '-' : lntag)); // Prepare new line for listing
161                 listflag = 1;                                           // OK to call `listeol' now
162                 just_bss = 0;                                           // Reset just_bss mode
163         }
164
165         state = -3;                                                             // No keyword (just EOL)
166         label = NULL;                                                   // No label
167         lab_sym = NULL;                                                 // No (exported) label
168         equate = NULL;                                                  // No equate
169         tk = tok;                                                               // Save first token in line
170         pcloc = (VALUE)sloc;                                    // Set beginning-of-line PC
171
172 loop1:                                                                          // Internal line processing loop
173
174         if (*tok == EOL)                                                // Restart loop if end-of-line
175                 goto loop;
176
177         // First token MUST be a symbol (Shamus: not sure why :-/)
178         if (*tok != SYMBOL)
179         {
180                 error("syntax error; expected symbol");
181                 goto loop;
182         }
183
184         j = (int)tok[2];                                                // Skip equates (normal statements)
185
186         if (j == '=' || j == DEQUALS || j == SET || j == REG || j == EQUREG || j == CCDEF)
187         {
188                 equate = string[tok[1]];
189                 equtyp = j;
190                 tok += 3;
191                 goto normal;
192         }
193
194         // Skip past label (but record it)
195         if (j == ':' || j == DCOLON)
196         {
197 as68label:
198                 label = string[tok[1]];                         // Get label name
199                 labtyp = tok[2];                                        // Get label type
200                 tok += 3;                                                       // Go to next line token
201
202                 // AS68 MODE:
203                 // Looks like another label follows the previous one, so handle
204                 // the previous one until there aren't any more
205                 if (as68_flag && (*tok == SYMBOL && tok[2] == ':'))
206                 {
207                         if (HandleLabel(label, labtyp) != 0)
208                                 goto loop;
209
210                         goto as68label;
211                 }
212         }
213
214         // EOL is legal here...
215         if (*tok == EOL)
216                 goto normal;
217
218         // Next token MUST be a symbol
219         if (*tok++ != SYMBOL)
220         {
221                 error("syntax error; expected symbol");
222                 goto loop;
223         }
224
225         opname = p = string[*tok++];
226
227         // Check to see if the SYMBOL is a keyword (a mnemonic or directive).
228         // On output, `state' will have one of the values:
229         //    -3          there was no symbol (EOL)
230         //    -2..-1      the symbol didn't match any keyword
231         //    0..499      vanilla directives (dc, ds, etc.)
232         //    500..999    electric directives (macro, rept, etc.)
233         //    1000..+     mnemonics (move, lsr, etc.)
234         for(state=0; state>=0;)
235         {
236                 j = mnbase[state] + (int)tolowertab[*p];
237
238                 // Reject, character doesn't match
239                 if (mncheck[j] != state)
240                 {
241                         state = -1;                                             // No match
242                         break;
243                 }
244
245                 // Must accept or reject at EOS
246                 if (!*++p)
247                 {
248                         state = mnaccept[j];                    // (-1 on no terminal match)
249                         break;
250                 }
251
252                 state = mntab[j];
253         }
254
255         // Check for ".b" ".w" ".l" after directive, macro or mnemonic.
256         siz = SIZN;
257
258         if (*tok == DOTW)
259                 siz = SIZW, tok++;
260         else if (*tok == DOTL)
261                 siz = SIZL, tok++;
262         else if (*tok == DOTB)
263                 siz = SIZB, tok++;
264
265         // Do special directives (500..999) (These must be handled in "real time")
266         if (state >= 500 && state < 1000)
267         {
268                 switch (state)
269                 {
270                 case MN_IF:
271                         d_if();
272                 goto loop;
273                 case MN_ELSE:
274                         d_else();
275                         goto loop;
276                 case MN_ENDIF:
277                         d_endif();
278                         goto loop;
279                 case MN_IIF:                                            // .iif --- immediate if
280                         if (disabled || expr(exprbuf, &eval, &eattr, &esym) != OK)
281                                 goto loop;
282
283                         if (!(eattr & DEFINED))
284                         {
285                                 error(undef_error);
286                                 goto loop;
287                         }
288
289                         if (*tok++ != ',')
290                         {
291                                 error(comma_error);
292                                 goto loop;
293                         }
294
295                         if (eval == 0)
296                                 goto loop;
297
298                         goto loop1;
299                 case MN_MACRO:                                          // .macro --- macro definition
300                         if (!disabled)
301                         {
302                                 // Label on a macro definition is bad mojo... Warn the user
303                                 if (label != NULL)
304                                         warn(lab_ignored);
305
306                                 DefineMacro();
307                         }
308
309                         goto loop;
310                 case MN_EXITM:                                          // .exitm --- exit macro
311                 case MN_ENDM:                                           // .endm --- same as .exitm
312                         if (!disabled)
313                         {
314                                 if (label != NULL)
315                                         warn(lab_ignored);
316
317                                 ExitMacro();
318                         }
319
320                         goto loop;
321                 case MN_REPT:
322                         if (!disabled)
323                         {
324                                 // Handle labels on REPT directive lines...
325                                 if (label)
326                                 {
327                                         if (HandleLabel(label, labtyp) != 0)
328                                                 goto loop;
329                                 }
330
331                                 defrept();
332                         }
333
334                         goto loop;
335                 case MN_ENDR:
336                         if (!disabled)
337                                 error("mis-nested .endr");
338
339                         goto loop;
340                 }
341         }
342
343 normal:
344         if (disabled)                                                   // Conditionally disabled code
345                 goto loop;
346
347         // Do equates
348         if (equate != NULL)
349         {
350                 // Pick global or local symbol enviroment
351                 j = (*equate == '.' ? curenv : 0);
352                 sy = lookup(equate, LABEL, j);
353
354                 if (sy == NULL)
355                 {
356                         sy = NewSymbol(equate, LABEL, j);
357                         sy->sattr = 0;
358
359                         if (equtyp == DEQUALS)
360                         {
361                                 // Can't GLOBAL a local symbol
362                                 if (j)
363                                 {
364                                         error(locgl_error);
365                                         goto loop;
366                                 }
367
368                                 sy->sattr = GLOBAL;
369                         }
370                 }
371                 else if ((sy->sattr & DEFINED) && equtyp != SET)
372                 {
373                         if ((equtyp == EQUREG) && (sy->sattre & UNDEF_EQUR))
374                         {
375 //REALLY?                               sy->sattre |= ~UNDEF_EQUR;
376                                 sy->sattre &= ~UNDEF_EQUR;
377                                 sy->svalue = 0;
378                         }
379                         else if ((equtyp == CCDEF) && (sy->sattre & UNDEF_CC))
380                         {
381 //REALLY?                               sy->sattre |= ~UNDEF_CC;
382                                 sy->sattre &= ~UNDEF_CC;
383                                 sy->svalue = 0;
384                         }
385                         else
386                         {
387                                 errors("multiple equate to '%s'", sy->sname);
388                                 goto loop;
389                         }
390                 }
391
392                 // Put symbol in "order of definition" list if it's not already there
393                 AddToSymbolDeclarationList(sy);
394
395                 // Parse value to equate symbol to;
396                 // o  .equr
397                 // o  .reg
398                 // o  everything else
399                 if (equtyp == EQUREG)
400                 {
401 //Linko's request to issue a warning on labels that equated to the same
402 //register would go here. Not sure how to implement it though. :-/
403 /*
404 Maybe like this way:
405 have an array of bools with 64 entries. Whenever a register is equated, set the
406 corresponding register bool to true. Whenever it's undef'ed, set it to false.
407 When checking to see if it's already been equated, issue a warning.
408 */
409                         // Check that we are in a RISC section
410                         if (!rgpu && !rdsp)
411                         {
412                                 error(".equr/.regequ must be defined in .gpu/.dsp section");
413                                 goto loop;
414                         }
415
416                         // Check for register to equate to
417                         if ((*tok >= KW_R0) && (*tok <= KW_R31))
418                         {
419 //                              sy->sattre  = EQUATEDREG | RISCSYM;     // Mark as equated register
420                                 sy->sattre  = EQUATEDREG;       // Mark as equated register
421                                 riscreg = (*tok - KW_R0);
422 //is there any reason to do this, since we're putting this in svalue?
423 //i'm thinking, no. Let's test that out! :-D
424 //                              sy->sattre |= (riscreg << 8);           // Store register number
425 //everything seems to build fine without it... We'll leave it here Just In Case(tm)
426
427 #define DEBODGE_REGBANK
428 #ifdef DEBODGE_REGBANK
429                                 // Default is current state of "regbank"
430                                 registerbank = regbank;
431 #else
432                                 // Default is no register bank specified
433                                 registerbank = BANK_N;
434 #endif
435
436                                 // Check for ",<bank #>" override notation
437                                 if ((tok[1] == ',') && (tok[2] == CONST))
438                                 {
439                                         // Advance token pointer to the constant
440                                         tok += 3;
441
442                                         // Anything other than a 0 or a 1 will result in "No Bank"
443                                         if (*tok == 0)
444                                                 registerbank = BANK_0;
445                                         else if (*tok == 1)
446                                                 registerbank = BANK_1;
447                                 }
448
449 #ifdef DEBODGE_REGBANK
450                                 sy->sattre |= registerbank;     // Store register bank
451 #else
452 // What needs to happen here is to prime registerbank with regbank, then use
453 // registerbank down below for the bank marking.
454 #warning "!!! regbank <-> registerbank confusion here !!!"
455 // The question here is why, if we're allowed to override the ".regbankN" rules
456 // above, then why is it using the one set by the directive in the extended
457 // attributes and not in what ends up in symbol->svalue?
458 // ".regbankN" is not an original Madmac directive, so it's suspect
459                                 sy->sattre |= regbank;          // Store register bank
460 #endif
461                                 eattr = ABS | DEFINED | GLOBAL;
462 // & what does this $80000080 constant mean???
463 //                              eval = 0x80000080 + (riscreg) + (registerbank << 8);
464                                 eval = riscreg;
465                                 tok++;
466                         }
467                         // Checking for a register symbol
468                         else if (tok[0] == SYMBOL)
469                         {
470                                 sy2 = lookup(string[tok[1]], LABEL, j);
471
472                                 // Make sure symbol is a valid equreg
473                                 if (!sy2 || !(sy2->sattre & EQUATEDREG))
474                                 {
475                                         error("invalid GPU/DSP .equr/.regequ definition");
476                                         goto loop;
477                                 }
478                                 else
479                                 {
480                                         eattr = ABS | DEFINED | GLOBAL; // Copy symbols attributes
481                                         sy->sattre = sy2->sattre;
482                                         eval = (sy2->svalue & 0xFFFFF0FF);
483                                         tok += 2;
484                                 }
485                         }
486                         else
487                         {
488                                 error("invalid GPU/DSP .equr/.regequ definition");
489                                 goto loop;
490                         }
491                 }
492                 else if (equtyp == REG)
493                 {
494                         if (reglist(&rmask) < 0)
495                                 goto loop;
496
497                         eval = (VALUE)rmask;
498                         eattr = ABS | DEFINED;
499                 }
500                 else if (equtyp == CCDEF)
501                 {
502                         sy->sattre |= EQUATEDCC;
503                         eattr = ABS | DEFINED | GLOBAL;
504
505                         if (tok[0] == SYMBOL)
506                         {
507                                 sy2 = lookup(string[tok[1]], LABEL, j);
508
509                                 if (!sy2 || !(sy2->sattre & EQUATEDCC))
510                                 {
511                                         error("invalid gpu/dsp .ccdef definition");
512                                         goto loop;
513                                 }
514                                 else
515                                 {
516                                         eattr = ABS | DEFINED | GLOBAL;
517                                         sy->sattre = sy2->sattre;
518                                         eval = sy2->svalue;
519                                         tok += 2;
520                                 }
521                         }
522                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
523                                 goto loop;
524                 }
525                 //equ a equr
526                 else if (*tok == SYMBOL)
527                 {
528                         sy2 = lookup(string[tok[1]], LABEL, j);
529
530                         if (sy2 && (sy2->sattre & EQUATEDREG))
531                         {
532                                 sy->stype = sy2->stype;
533                                 sy->sattr = sy2->sattr;
534                                 sy->sattre = sy2->sattre;
535 //ICK                           sy->svalue = (sy2->svalue & 0xFFFFF0FF);
536                                 sy->svalue = sy2->svalue;
537                                 goto loop;
538                         }
539                         else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
540                                 goto loop;
541                 }
542                 else if (expr(exprbuf, &eval, &eattr, &esym) != OK)
543                         goto loop;
544
545                 if (!(eattr & DEFINED))
546                 {
547                         error(undef_error);
548                         goto loop;
549                 }
550
551                 sy->sattr |= eattr | EQUATED;   // Symbol inherits value and attributes
552                 sy->svalue = eval;
553
554                 if (list_flag)                                  // Put value in listing
555                         listvalue(eval);
556
557                 at_eol();                                               // Must be at EOL now
558                 goto loop;
559         }
560
561         // Do labels
562         if (label != NULL)
563         {
564                 // Non-zero == error occurred
565                 if (HandleLabel(label, labtyp) != 0)
566                         goto loop;
567         }
568
569         // Punt on EOL
570         if (state == -3)
571                 goto loop;
572
573         // If we're in 6502 mode and are still in need of a mnemonic, then search
574         // for valid 6502 mnemonic.
575         if (m6502 && (state < 0 || state >= 1000))
576         {
577 #ifdef ST
578                 state = kmatch(opname, mpbase, mpcheck, mptab, mpaccept);
579 #else
580                 for(state=0, p=opname; state>= 0; )
581                 {
582                         j = mpbase[state] + tolowertab[*p];
583
584                         if (mpcheck[j] != state)        // Reject, character doesn't match
585                         {
586                                 state = -1;             // No match
587                                 break;
588                         }
589
590                         if (!*++p)
591                         {                       // Must accept or reject at EOS
592                                 state = mpaccept[j];    // (-1 on no terminal match)
593                                 break;
594                         }
595
596                         state = mptab[j];
597                 }
598 #endif
599
600                 // Call 6502 code generator if we found a mnemonic
601                 if (state >= 2000)
602                 {
603                         m6502cg(state - 2000);
604                         goto loop;
605                 }
606         }
607
608         // If we are in GPU or DSP mode and still in need of a mnemonic then search
609         // for one
610         if ((rgpu || rdsp) && (state < 0 || state >= 1000))
611         {
612                 for(state=0, p=opname; state>=0;)
613                 {
614                         j = mrbase[state] + (int)tolowertab[*p];
615
616                         // Reject, character doesn't match
617                         if (mrcheck[j] != state)
618                         {
619                                 state = -1;                                     // No match
620                                 break;
621                         }
622
623                         // Must accept or reject at EOS
624                         if (!*++p)
625                         {
626                                 state = mraccept[j];            // (-1 on no terminal match)
627                                 break;
628                         }
629
630                         state = mrtab[j];
631                 }
632
633                 // Call RISC code generator if we found a mnemonic
634                 if (state >= 3000)
635                 {
636                         GenerateRISCCode(state);
637                         goto loop;
638                 }
639         }
640
641         // Invoke macro or complain about bad mnemonic
642         if (state < 0)
643         {
644                 if ((sy = lookup(opname, MACRO, 0)) != NULL)
645                         InvokeMacro(sy, siz);
646                 else
647                         errors("unknown op '%s'", opname);
648
649                 goto loop;
650         }
651
652         // Call directive handlers
653         if (state < 500)
654         {
655                 (*dirtab[state])(siz);
656                 goto loop;
657         }
658
659         // Do mnemonics
660         // o  can't deposit instrs in BSS or ABS
661         // o  do automatic .EVEN for instrs
662         // o  allocate space for largest possible instr
663         // o  can't do ".b" operations with an address register
664         if (scattr & SBSS)
665         {
666                 error("cannot initialize non-storage (BSS) section");
667                 goto loop;
668         }
669
670         if (sloc & 1)                                   // Automatic .even
671                 auto_even();
672
673         if (challoc - ch_size < 18)             // Make sure have space in current chunk
674                 chcheck(0);
675
676         m = &machtab[state - 1000];
677
678         // Call special-mode handler
679         if (m->mnattr & CGSPECIAL)
680         {
681                 (*m->mnfunc)(m->mninst, siz);
682                 goto loop;
683         }
684
685         if (amode(1) < 0)                               // Parse 0, 1 or 2 addr modes
686                 goto loop;
687
688         if (*tok != EOL)
689                 error(extra_stuff);
690
691         amsk0 = amsktab[am0];
692         amsk1 = amsktab[am1];
693
694         // Catch attempts to use ".B" with an address register (yes, this check
695         // does work at this level)
696         if (siz == SIZB && (am0 == AREG || am1 == AREG))
697         {
698                 error("cannot use '.b' with an address register");
699                 goto loop;
700         }
701
702         // Keep a backup of chptr (used for optimisations during codegen)
703         chptr_opcode = chptr;
704
705         for(;;)
706         {
707                 if ((m->mnattr & siz) && (amsk0 & m->mn0) != 0 && (amsk1 & m->mn1) != 0)
708                 {
709                         (*m->mnfunc)(m->mninst, siz);
710                         goto loop;
711                 }
712
713                 m = &machtab[m->mncont];
714         }
715 }
716
717
718 //
719 // Handle the creation of labels
720 //
721 int HandleLabel(char * label, int labelType)
722 {
723         // Check for dot in front of label; means this is a local label if present
724         int environment = (*label == '.' ? curenv : 0);
725         SYM * symbol = lookup(label, LABEL, environment);
726
727         if (symbol == NULL)
728         {
729                 symbol = NewSymbol(label, LABEL, environment);
730                 symbol->sattr = 0;
731 //              symbol->sattre = RISCSYM;
732                 symbol->sattre = 0;
733         }
734         else if (symbol->sattr & DEFINED)
735                 return errors("multiply-defined label '%s'", label);
736
737         // Put symbol in "order of definition" list if it's not already in it
738         AddToSymbolDeclarationList(symbol);
739
740         if (orgactive)
741         {
742                 symbol->svalue = orgaddr;
743                 symbol->sattr |= ABS | DEFINED | EQUATED;
744         }
745         else
746         {
747                 symbol->svalue = sloc;
748                 symbol->sattr |= DEFINED | cursect;
749         }
750
751         lab_sym = symbol;
752
753         if (0 == environment)
754                 curenv++;
755
756         // Make label global if it has a double colon
757         if (labelType == DCOLON)
758         {
759                 if (environment != 0)
760                         return error(locgl_error);
761
762                 symbol->sattr |= GLOBAL;
763         }
764
765         return 0;
766 }
767
768
769 //
770 // .if, Start conditional assembly
771 //
772 int d_if(void)
773 {
774         IFENT * rif;
775         WORD eattr;
776         VALUE eval;
777         SYM * esym;
778
779         // Alloc an IFENTRY
780         if ((rif = f_ifent) == NULL)
781                 rif = (IFENT *)malloc(sizeof(IFENT));
782         else
783                 f_ifent = rif->if_prev;
784
785         rif->if_prev = ifent;
786         ifent = rif;
787
788         if (!disabled)
789         {
790                 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
791                         return 0;
792
793                 if ((eattr & DEFINED) == 0)
794                         return error(undef_error);
795
796                 disabled = !eval;
797         }
798
799         rif->if_state = (WORD)disabled;
800         return 0;
801 }
802
803
804 //
805 // .else, Do alternate case for .if
806 //
807 int d_else(void)
808 {
809         IFENT * rif = ifent;
810
811         if (rif->if_prev == NULL)
812                 return error("mismatched .else");
813
814         if (disabled)
815                 disabled = rif->if_prev->if_state;
816         else
817                 disabled = 1;
818
819         rif->if_state = (WORD)disabled;
820         return 0;
821 }
822
823
824 //
825 // .endif, End of conditional assembly block
826 // This is also called by fpop() to pop levels of IFENTs in case a macro or
827 // include file exits early with `exitm' or `end'.
828 //
829 int d_endif (void)
830 {
831         IFENT * rif = ifent;
832
833         if (rif->if_prev == NULL)
834                 return error("mismatched .endif");
835
836         ifent = rif->if_prev;
837         disabled = rif->if_prev->if_state;
838         rif->if_prev = f_ifent;
839         f_ifent = rif;
840         return 0;
841 }
842