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