home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / language / as / source / c / decode < prev    next >
Encoding:
Text File  |  1993-12-28  |  15.2 KB  |  392 lines

  1.  
  2. /*
  3.  * decode.c
  4.  * Copyright © 1992 Niklas Röjemo
  5.  */
  6.  
  7. #include <ctype.h>
  8. #include "decode.h"
  9. #include "input.h"
  10. #include "commands.h"
  11. #include "mnemonics.h"
  12. #include "storage.h"
  13. #include "option.h"
  14. #include "error.h"
  15. #include "filestack.h"
  16.  
  17. extern FILE *asmfile;   /* in input.c */
  18.  
  19.  
  20. BOOL notinput(char *str)
  21. {
  22.   int c;
  23.   for(;*str;) {
  24.      c = inputGet();
  25.     if(isupper(c))
  26.       c = tolower(c);
  27.     if(*str++ != c)
  28.       return TRUE;
  29.   }
  30.   return FALSE;
  31. }
  32.  
  33. int returnvalue = 0;
  34.  
  35. extern int verbose;
  36.  
  37. void c_end(void)
  38. {
  39.   FILE *fp;
  40.  
  41.   fp = pop_file();
  42.   if (!fp)
  43.     returnvalue = 1;
  44.   else {
  45.     fclose(asmfile);
  46.     asmfile = fp;
  47.     if(verbose)
  48.       printf("Returning from include file\n");
  49.   }
  50. }
  51.  
  52. #define C_FINISH_STR(string,fun)                     \
  53.         if(notinput(string)                          \
  54.            || (inputLook() && !isspace(inputGet()))) \
  55.           goto illegal;                              \
  56.         skipblanks();                                \
  57.         fun();                                       \
  58.         break;
  59.  
  60. #define C_FINISH_CHR(chr,fun)                        \
  61.         c=inputGet();                                \
  62.         if(isupper(c))                               \
  63.           c = tolower(c);                            \
  64.         if(c != chr                                  \
  65.            || (inputLook() && !isspace(inputGet()))) \
  66.           goto illegal;                              \
  67.         skipblanks();                                \
  68.         fun();                                       \
  69.         break;
  70.  
  71. #define C_FINISH(fun)                                \
  72.         if(inputLook() && !isspace(inputGet()))      \
  73.           goto illegal;                              \
  74.         skipblanks();                                \
  75.         fun();                                       \
  76.         break;
  77.  
  78. #define C_FINISH_SYMBOL(fun)                         \
  79.         if(inputLook() && !isspace(inputGet()))      \
  80.           goto illegal;                              \
  81.         skipblanks();                                \
  82.         fun(symbol);                                 \
  83.         break;
  84.  
  85. #define C_FINISH_CHR_SYMBOL(chr,fun)                 \
  86.         c=inputGet();                                \
  87.         if(isupper(c))                               \
  88.           c = tolower(c);                            \
  89.         if(c != chr                                  \
  90.            || (inputLook() && !isspace(inputGet()))) \
  91.           goto illegal;                              \
  92.         skipblanks();                                \
  93.         fun(symbol);                                 \
  94.         break;
  95.  
  96. #define M_FINISH_STR(string,fun,opt)            \
  97.         if(notinput(string))                    \
  98.           goto illegal;                         \
  99.         if(optionError == (option = opt()))     \
  100.           goto illegal;                         \
  101.         skipblanks();                           \
  102.         fun(option);                            \
  103.         break;
  104.  
  105. #define M_FINISH_CHR(chr,fun,opt)               \
  106.         c=inputGet();                           \
  107.         if(isupper(c))                          \
  108.           c = tolower(c);                       \
  109.         if(c != chr)                            \
  110.           goto illegal;                         \
  111.         if(optionError == (option = opt()))     \
  112.           goto illegal;                         \
  113.         skipblanks();                           \
  114.         fun(option);                            \
  115.         break;
  116.  
  117. #define M_FINISH(fun,opt)                       \
  118.         if(optionError == (option = opt()))     \
  119.           goto illegal;                         \
  120.         skipblanks();                           \
  121.         fun(option);                            \
  122.         break;
  123.  
  124. int decode(Symbol *symbol)
  125. {
  126.   int c;
  127.   WORD option;
  128.   inputMark();
  129.   switch(inputGet()) {
  130.     case '%': C_FINISH(c_reserve);         /* reserve space */
  131.     case '*': C_FINISH_SYMBOL(c_equ);      /* equ */
  132.     case '&': C_FINISH(c_dcd);             /* DCD    32 */
  133.     case '=': C_FINISH(c_dcb);             /* DCB     8 */
  134.     case '^': C_FINISH(c_record);          /* start of new record layout */
  135.     case '#': C_FINISH_SYMBOL(c_alloc);    /* reserve space in the current record */
  136.     case 'a':case 'A':
  137.       switch(inputGet()) {
  138.         case 'b':case 'B': M_FINISH_CHR('s',m_abs,optionCondPrecRound); /* abs CC P R */
  139.         case 'c':case 'C': M_FINISH_CHR('s',m_acs,optionCondPrecRound); /* acs CC P R */
  140.         case 'd':case 'D':
  141.           switch(inputGet()) {
  142.             case 'c':case 'C': M_FINISH(m_adc,optionCondS); /* adc CC s */
  143.             case 'd':case 'D': M_FINISH(m_add,optionCondS); /* add CC s */
  144.             case 'f':case 'F': M_FINISH(m_adf,optionCondPrecRound); /* adf CC P R */
  145.             case 'r':case 'R': M_FINISH(m_adr,optionCond); /* adr CC */
  146.             default:  goto illegal;
  147.           } break;
  148.         case 'n':case 'N': M_FINISH_CHR('d',m_and,optionCondS); /* and CC S */
  149.         case 'l':case 'L': C_FINISH_STR("ign",c_align); /* .align */
  150.         case 'r':case 'R': C_FINISH_STR("ea",c_area); /* AREA */
  151.         case 's':case 'S': M_FINISH_CHR('n',m_asn,optionCondPrecRound); /* asn CC P R */
  152.         case 't':case 'T': M_FINISH_CHR('n',m_atn,optionCondPrecRound); /* atn CC P R */
  153.         default:  goto illegal;
  154.       } break;
  155.     case 'b':case 'B':
  156.       switch(c=inputGet()) {
  157.         case 'i':case 'I':
  158.           switch(c=inputGet()) {
  159.             case 'c':case 'C': M_FINISH(m_bic,optionCondS); /* bic CC s */
  160.             case 'n':case 'N': C_FINISH(c_bin); /* bin */
  161.           } break;
  162.         default:  inputUnGet(c); M_FINISH(m_branch,optionLinkCond); /* bl and b */
  163.       } break;
  164.     case 'c':case 'C':
  165.       switch(inputGet()) {
  166.         case 'd':case 'D': M_FINISH_CHR('p',m_cdp,optionCond); /* cdp CC */
  167.         case 'm':case 'M':
  168.           switch(inputGet()) {
  169.             case 'f':case 'F': M_FINISH(m_cmf,optionExceptionCond); /* cmf CC  or cmfe CC */
  170.             case 'n':case 'N': M_FINISH(m_cmn,optionCondSP); /* cmn CC SP */
  171.             case 'p':case 'P': M_FINISH(m_cmp,optionCondSP); /* cmp CC sp */
  172.             default:  goto illegal;
  173.           } break;
  174.         case 'n':case 'N':
  175.           switch(c=inputGet()) {
  176.             case 'f':case 'F': M_FINISH(m_cnf,optionExceptionCond); /* cnf CC or cnfe CC */
  177.             default: inputUnGet(c); C_FINISH_SYMBOL(c_cn); /* CN */
  178.           } break;
  179.         case 'o':case 'O':
  180.           switch(inputGet()) {
  181.             case 's':case 'S': M_FINISH(m_cos,optionCondPrecRound);       /* cos CC P R */
  182.             default:  goto illegal;
  183.           } break;
  184.         default:  goto illegal;
  185.       } break; 
  186.     case 'd':case 'D':
  187.       switch(inputGet()) {
  188.         case 'v':case 'V': M_FINISH_CHR('f',m_dvf,optionCondPrecRound); /* dvf CC s */
  189.         case 'c':case 'C':
  190.           switch(inputGet()) {
  191.             case 'b':case 'B': C_FINISH(c_dcb); /* DCB    8 */
  192.             case 'd':case 'D': C_FINISH(c_dcd); /* DCD    32 */
  193.             case 'f':case 'F':
  194.               switch(inputGet()) {
  195.                 case 'd':case 'D': C_FINISH(c_dcfd); /* DCFD   64 float */
  196.                 case 'e':case 'E': C_FINISH(c_dcfe); /* DCFE   80 float */
  197.                 case 'p':case 'P': C_FINISH(c_dcfp); /* DCFP   80 packed bcd */
  198.                 case 's':case 'S': C_FINISH(c_dcfs); /* DCFS   32 float */
  199.               } break;
  200.             case 'w':case 'W': C_FINISH(c_dcw); /* DCW    16 */
  201.             default:    goto illegal;
  202.           } break;
  203.         default:  goto illegal;
  204.       } break;
  205.     case 'e':case 'E':
  206.       switch(inputGet()) {
  207.         case 'n':case 'N':
  208.           switch(inputGet()) {
  209.             case 'd':case 'D': C_FINISH(c_end);     /* END */
  210.             case 't':case 'T': C_FINISH_STR("ry",c_entry); /* entry */
  211.             default:  goto illegal;
  212.           } break;
  213.         case 'o':case 'O': M_FINISH_CHR('r',m_eor,optionCondS); /* eor CC s */
  214.         case 'q':case 'Q': C_FINISH_CHR_SYMBOL('u',c_equ); /* equ */
  215.         case 'x':case 'X':
  216.           switch(inputGet()) {
  217.             case 'p':case'P':
  218.               switch(c=inputGet()) {
  219.                 case 'o':case 'O': C_FINISH_STR("rt",c_globl); /* EXPORT */
  220.                 default: inputUnGet(c); M_FINISH(m_exp,optionCondPrecRound); /* exp CC P R */
  221.               } break;
  222.             default:  goto illegal;
  223.           } break;
  224.         default:  goto illegal;
  225.       }
  226.       break;
  227.     case 'f':case 'F':
  228.       switch(inputGet()) {
  229.         case 'd':case 'D': M_FINISH_CHR('v',m_fdv,optionCondPrecRound); /* fdv CC P R */
  230.         case 'i':case 'I': M_FINISH_CHR('x',m_fix,optionCondOptPrecRound); /* fix CC [P] R */
  231.         case 'l':case 'L': M_FINISH_CHR('t',m_flt,optionCondPrecRound); /* flt CC P R */
  232.         case 'm':case 'M': M_FINISH_CHR('l',m_fml,optionCondPrecRound); /* fml CC P R */
  233.         case 'n':case 'N': C_FINISH_SYMBOL(c_fn);              /* FN */
  234.         case 'r':case 'R': M_FINISH_CHR('d',m_frd,optionCondPrecRound); /* frd CC P R */
  235.         default:  goto illegal;
  236.       }
  237.       break;
  238.     case 'g':case 'G':
  239.       switch(inputGet()) {
  240.         case 'e':case 'E':  C_FINISH_CHR('t', c_get);
  241.         case 'l':case 'L':  C_FINISH_STR("obl",c_globl);   /* globl  */
  242.         default:  goto illegal;
  243.       }
  244.       break;
  245.     case 'i':case 'I':
  246.       switch(inputGet()) {
  247.         case 'd':case 'D': C_FINISH_STR("fn", c_idfn);   /* idfn */
  248.         case 'm':case 'M': C_FINISH_STR("port",c_import);  /*IMPORT */
  249.         default:  goto illegal;
  250.       }
  251.       break;
  252.     case 'l':case 'L':
  253.       switch(inputGet()) {
  254.         case 'd':case 'D':
  255.           switch(inputGet()) {
  256.             case 'c':case 'C': M_FINISH(m_ldc,optionCondL); /* ldc CC l */
  257.             case 'f':case 'F': M_FINISH(m_ldf,optionCondPrec_P); /* ldf CC P */
  258.             case 'm':case 'M': M_FINISH(m_ldm,optionCondDirLdm); /* ldm CC TYPE */
  259.             case 'r':case 'R': M_FINISH(m_ldr,optionCondBT); /* ldr CC BYTE */
  260.             default:  goto illegal;
  261.           }
  262.           break;
  263.         case 'g':case 'G': M_FINISH_CHR('n',m_lgn,optionCondPrecRound); /* lgn CC P R */
  264.         case 'n':case 'N': C_FINISH_CHR('k', c_lnk);               /* lnk */
  265.         case 'o':case 'O': M_FINISH_CHR('g',m_log,optionCondPrecRound); /* log CC P R */
  266.         case 't':case 'T': C_FINISH_STR("org",c_ltorg);                 /* ltorg */
  267.         default:  goto illegal;
  268.       }
  269.       break;
  270.     case 'm':case 'M':
  271.       switch(inputGet()) {
  272.         case 'c':case 'C': M_FINISH_CHR('r',m_mcr,optionCond); /* mcr CC */
  273.         case 'l':case 'L': M_FINISH_CHR('a',m_mla,optionCondS); /* mla CC s */
  274.         case 'n':case 'N': M_FINISH_CHR('f',m_mnf,optionCondPrecRound); /* mnf CC P R */
  275.         case 'o':case 'O': M_FINISH_CHR('v',m_mov,optionCondS); /* mov CC s */
  276.         case 'r':case 'R': M_FINISH_CHR('c',m_mrc,optionCond); /* mrc CC */
  277.         case 'u':case 'U':
  278.           switch(inputGet()) {
  279.             case 'f':case 'F': M_FINISH(m_muf,optionCondPrecRound); /* muf CC P R */
  280.             case 'l':case 'L': M_FINISH(m_mul,optionCondS); /* mul CC s */
  281.           default:  goto illegal;
  282.           }
  283.           break;
  284.         case 'v':case 'V':
  285.           switch(inputGet()) {
  286.             case 'f':case 'F': M_FINISH(m_mvf,optionCondPrecRound); /* mvf CC P R */
  287.             case 'n':case 'N': M_FINISH(m_mvn,optionCondS); /* mvn CC s */
  288.             default:  goto illegal;
  289.           }
  290.           break;
  291.         default:  goto illegal;
  292.       }
  293.       break;
  294.  
  295.     case 'o':case 'O': M_FINISH_STR("rr",m_orr,optionCondS); /* orr CC s */
  296.     case 'p':case 'P':
  297.       switch(inputGet()) {
  298.         case 'o':case 'O':
  299.           switch(inputGet()) {
  300.             case 'l':case 'L': M_FINISH(m_pol,optionCondPrecRound); /* pol CC P R */
  301.             case 'w':case 'W': M_FINISH(m_pow,optionCondPrecRound); /* pow CC P R */
  302.           default:  goto illegal;
  303.           }
  304.           break;
  305.         default:  goto illegal;
  306.       }
  307.       break;
  308.     case 'r':case 'R':
  309.       switch(inputGet()) {
  310.         case 'd':case 'D': M_FINISH_CHR('f',m_rdf,optionCondPrecRound); /* rdf CC P R */
  311.         case 'f':case 'F':
  312.           switch(inputGet()) {
  313.             case 'c':case 'C': M_FINISH(m_rfc,optionCond); /* rfc CC */
  314.             case 's':case 'S': M_FINISH(m_rfs,optionCond); /* rfs CC */
  315.             default:  goto illegal;
  316.           } break;
  317.         case 'm':case 'M': M_FINISH_CHR('f',m_rmf,optionCondPrecRound); /* rmf CC P R */
  318.         case 'n':case 'N':
  319.           switch(c=inputGet()) {
  320.             case 'd':case 'D': M_FINISH(m_rnd,optionCondPrecRound); /* rnd CC P R */
  321.             default: inputUnGet(c); C_FINISH_SYMBOL(c_rn); /* RN */
  322.           } break;
  323.         case 'p':case 'P': M_FINISH_CHR('w',m_rpw,optionCondPrecRound); /* rpw CC P R */
  324.         case 's':case 'S':
  325.           switch(inputGet()) {
  326.             case 'b':case 'B': M_FINISH(m_rsb,optionCondS); /* rsb CC s */
  327.             case 'c':case 'C': M_FINISH(m_rsc,optionCondS); /* rsc CC s */
  328.             case 'f':case 'F': M_FINISH(m_rsf,optionCondPrecRound); /* rsf CC P R */
  329.             default:  goto illegal;
  330.           }
  331.           break;
  332.         default:  goto illegal;
  333.       }
  334.       break;
  335.     case 's':case 'S':
  336.       switch(inputGet()) {
  337.         case 'b':case 'B': M_FINISH_CHR('c',m_sbc,optionCondS); /* sbc CC s */
  338.         case 'i':case 'I': M_FINISH_CHR('n',m_sin,optionCondPrecRound); /* sin CC P R */
  339.         case 'q':case 'Q': M_FINISH_CHR('t',m_sqt,optionCondPrecRound); /* sqt CC P R */
  340.         case 't':case 'T':
  341.           switch(inputGet()) {
  342.             case 'c':case 'C': M_FINISH(m_stc,optionCondL); /* stc CC l */
  343.             case 'f':case 'F': M_FINISH(m_stf,optionCondPrec_P); /* stf CC P */
  344.             case 'm':case 'M': M_FINISH(m_stm,optionCondDirStm); /* stm CC TYPE */
  345.             case 'r':case 'R': M_FINISH(m_str,optionCondBT); /* str CC BYTE */
  346.             default:  goto illegal;
  347.           } break;
  348.         case 'u':case 'U':
  349.           switch(inputGet()) {
  350.             case 'b':case 'B': M_FINISH(m_sub,optionCondS); /* sub CC s */
  351.             case 'f':case 'F': M_FINISH(m_suf,optionCondPrecRound); /* suf CC P R */
  352.             default:  goto illegal;
  353.           } break;
  354.         case 'w':case 'W':
  355.           switch(inputGet()) {
  356.             case 'i':case 'I': M_FINISH(m_swi,optionCond); /* swi CC */
  357.             case 'p':case 'P': M_FINISH(m_swp,optionCondB); /* swp CC BYTE */
  358.             default:  goto illegal;
  359.           } break;
  360.         default:  goto illegal;
  361.       } break;
  362.     case 't':case 'T':
  363.       switch(inputGet()) {
  364.         case 'a':case 'A': M_FINISH_CHR('n',m_tan,optionCondPrecRound); /* tan CC P R */
  365.         case 'e':case 'E': 
  366.           switch(inputGet()) {
  367.             case 'q':case 'Q': M_FINISH(m_teq,optionCondSP);                /* teq CC sp */
  368.             default:  goto illegal;
  369.           } break;
  370.         case 's':case 'S': M_FINISH_CHR('t',m_tst,optionCondSP); /* tst CC sp */
  371.         default:  goto illegal;
  372.       } break;
  373.     case 'w':case 'W':
  374.       switch(inputGet()) {
  375.         case 'f':case 'F':
  376.           switch(inputGet()) {
  377.             case 'c':case 'C': M_FINISH(m_wfc,optionCond); /* wfc CC */
  378.             case 's':case 'S': M_FINISH(m_wfs,optionCond); /* wfs CC */
  379.             default:  goto illegal;
  380.           } break;
  381.         default:  goto illegal;
  382.       } break;
  383.     default:
  384. illegal:
  385.       inputRollback();
  386.       error(ErrorError,FALSE,"Illegal line \"%s\".",inputRest());
  387.   }
  388.   skipblanks();
  389.   if(!inputComment()) { error(ErrorError,FALSE,"Skipping extra characters '%s'.",inputRest()); }
  390.   return returnvalue;
  391. }
  392.