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