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