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