home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / m / motorola / !AsRef / Sources / c / ifd < prev    next >
Text File  |  1993-07-18  |  13KB  |  277 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. #include "mselect.h"    /*external selection of microprocessor symbol table*/
  6. #include "proto.h"
  7. #include "as.h"
  8. #include "extvars.h"
  9. #include "structs.h"
  10.  
  11.  
  12. /*
  13.  * IfMachine() --- This function implements the IFD & IFND conditional
  14.  * assembly machine. version 1.0 made for release TER_2.0 cross assembler 27
  15.  * Jun 89
  16.  */
  17.  
  18. #define FALSE_BLOCK     0       /* values for state variable */
  19. #define TRUE_BLOCK      1
  20. #define POP_TEST        2
  21. #define ELSE_TEST       3
  22. #define FALSE           0
  23. #define TRUE            1
  24.  
  25. void
  26. IfMachine(int Token)
  27. /* input token from calling machine */
  28. /* See file as.h for definition (globals) */
  29. {
  30.         static int      State = TRUE_BLOCK, StackPt = 0, IfStack[MAXIFD];
  31.         /* State variable, pointer to "IF stack pointer" and "Stack" */
  32.         int             Hiatus; /* flag to break out of FSM & resume normal
  33.                                  * processing */
  34.  
  35.         Hiatus = FALSE;         /* infinite loop to operate machine until
  36.                                  * time to break out */
  37.         do {                    /* test at end */
  38.  
  39. #ifdef DEBUG3
  40.                 printf("IfMachine state=%u , token=%u\n", State, Token);
  41. #endif
  42.  
  43.                 if (State == TRUE_BLOCK)        /* a block of statements
  44.                                                  * processed normally */
  45.                         switch (Token) {
  46.                         case IF_TRUE:
  47.                                 IfStack[StackPt] = TRUE;
  48.                                 if (++StackPt > MAXIFD) {       /* check for over flow */
  49.                                         StackPt = MAXIFD;
  50.                                         error("Error:IFD/IFND nested too deep");
  51.                                 }
  52.                                 /*
  53.                                  * print_line() will be done in normal
  54.                                  * processing
  55.                                  */
  56.                                 Hiatus = TRUE;  /* resume normal line
  57.                                                  * processing */
  58.                                 break;
  59.                         case IF_FALSE:
  60.                                 IfStack[StackPt] = TRUE;
  61.                                 if (++StackPt > MAXIFD) {       /* check for over flow */
  62.                                         StackPt = MAXIFD;
  63.                                         error("Error:IFD/IFND nested too deep");
  64.                                 }
  65.                                 if (Pass == 2 && Lflag && !N_page)      /* need to print here */
  66.                                         print_line();   /* cuz will not return
  67.                                                          * to normal */
  68.                                 Token = GetToken();     /* get next line &
  69.                                                          * examine for IF */
  70.                                 State = FALSE_BLOCK;    /* change state */
  71.                                 break;
  72.                         case IF_ELSE:
  73.                                 if (StackPt == 0)       /* bad IF nesting */
  74.                                         error("Error: ELSE without IF");
  75.                                 if (Pass == 2 && Lflag && !N_page)
  76.                                         print_line();
  77.                                 Token = GetToken();     /* get next line &
  78.                                                          * examine for IF */
  79.                                 State = FALSE_BLOCK;
  80.                                 break;
  81.                         case IF_ENDIF:
  82.                                 if (StackPt == 0)       /* bad IF nesting */
  83.                                         error("Error: ENDIF without IF");
  84.                                 else
  85.                                         StackPt--;      /* popped state must be
  86.                                                          * TRUE */
  87.                                 Hiatus = TRUE;
  88.                                 break;
  89.                                 /*
  90.                                  * case NORMAL is not implemented as it
  91.                                  * represents normal line processing.
  92.                                  */
  93.                         case IF_END:    /* file ended with improperly nested
  94.                                          * IFD */
  95.                                 /*
  96.                                  * this code can't actually be reached at
  97.                                  * present in a TRUE_BLOCK
  98.                                  */
  99.                                 fatal("Error: file ended before IFD/IFND/ELSE/ENDIF");
  100.                                 break;
  101.                         default:        /* This code can't be reached at the
  102.                                          * present. Logically would happen if
  103.                                          * EOF but handled else where */
  104.                                 fatal("Can't get here from there.");
  105.                                 break;
  106.                         }
  107.                 else if (State == FALSE_BLOCK)  /* statements not processed */
  108.                         switch (Token) {
  109.                         case IF_TRUE:   /* doesn't make any diff. Just nest
  110.                                          * IFs */
  111.                         case IF_FALSE:
  112.                                 IfStack[StackPt] = FALSE;
  113.                                 if (++StackPt > MAXIFD) {
  114.                                         StackPt = MAXIFD;
  115.                                         error("Error:IFD/IFND nested too deep");
  116.                                 }
  117.                                 if (Pass == 2 && Lflag && !N_page)
  118.                                         print_line();
  119.                                 Token = GetToken();
  120.                                 break;
  121.                         case IF_ELSE:
  122.                                 if (Pass == 2 && Lflag && !N_page)
  123.                                         print_line();
  124.                                 State = ELSE_TEST;
  125.                                 break;
  126.                         case IF_ENDIF:
  127.                                 State = POP_TEST;
  128.                                 break;
  129.                         case IF_END:    /* file ended with improperly nested
  130.                                          * IFD */
  131.                                 fatal("Fatal Error: file ended before last ENDIF");
  132.                                 /*
  133.                                  * Fatal will exit assembler.  Things are too
  134.                                  * messed up.  Include file handling is else
  135.                                  * where and don't want to duplicate here.
  136.                                  */
  137.                                 break;
  138.                         case IF_NORMAL: /* normal line in a FALSE
  139.                                                  * BLOCK */
  140.                                 if (Pass == 2 && Lflag && !N_page)
  141.                                         print_line();
  142.                                 Token = GetToken();
  143.                                 break;
  144.                         default:
  145.                                 fatal("Fatal Error: file ended before last ENDIF");
  146.                                 /* must be EOF or something else terrible */
  147.                                 break;
  148.                         }
  149.                 else if (State == POP_TEST) {   /* pop up outside nest state */
  150.                         if (StackPt == 0) {     /* bad IF nesting */
  151.                                 error("Error: ENDIF without IF");
  152.                                 if (Pass == 2 && Lflag && !N_page)
  153.                                         print_line();
  154.                                 State = TRUE;
  155.                         } else {
  156.                                 StackPt--;      /* pop stack */
  157.                                 if (IfStack[StackPt] == TRUE) { /* back to normal */
  158.                                         /*
  159.                                          * had to come from FALSE block cuz
  160.                                          * TRUE block cannot be inside FALSE
  161.                                          * block
  162.