home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / asm65.y < prev    next >
Text File  |  1993-01-19  |  12KB  |  323 lines

  1. %{
  2. /* ---------------------------------------------------------------------- */
  3. /*                   Copyright (C) 1992 by Natürlich!                     */
  4. /*                      This file is copyrighted!                         */
  5. /*                Refer to the documentation for details.                 */
  6. /* ---------------------------------------------------------------------- */
  7. /*#define YYDEBUG*/
  8. #include <stdio.h>
  9. #include "defines.h"
  10. #undef DEBUG
  11. #include "structs.h"
  12. #include "nasm.h"
  13. #include "labels.h"
  14. #include "code.h"
  15. #include "buffer.h"
  16. #include "exprfast.h"
  17. #include "op.h"
  18.  
  19. #if DEBUG
  20. #define bputchar( c)   putchar( c)
  21. #define dbprintf( s)    puts( s)
  22. #else
  23. #define dbputchar( c)
  24. #define dbprintf( s)
  25. #endif
  26.  
  27. #ifdef NIL
  28. #undef NIL
  29. #endif
  30. #define NIL   ((char *) 0)
  31. #define ENIL  ((expr *) 0)
  32.  
  33. extern int     _in_instr,
  34.                _in_doteq,
  35.                _call_macro,
  36.                wasstrlen,
  37.                state,
  38.                fully_mac65;
  39.  
  40.  
  41. extern buffer huge   *bp;
  42. extern word    __pc;
  43.  
  44. static char    undefined[] = { 9, 0, 'u','n','d','e','f','i','n','e','d' },
  45.                *lastident;
  46. static int     footok;
  47. static lword   fooval;
  48. #define adrmod footok      /* screws the debugger of course (har har) */
  49.  
  50. typedef union
  51. {
  52.    lword     u;
  53.    byte     *b;
  54.    char     *c;
  55.    expr     *e;
  56.    lexpr    *l;
  57. } YYSTYPE;
  58.  
  59. #define _ul    lword
  60. %}
  61.  
  62. %start file
  63. /* ---------------------------------------------------------- */
  64. /*     The order of these tokens MIGHT be VERY IMPORTANT      */
  65. /* ---------------------------------------------------------- */
  66. %token <u>  T_DOTEQ  T_IF     T_ELSE   T_LOCAL  T_EOL    T_NUMBER T_ZEXT
  67. %token <u>  T_ENDIF  T_ORG    T_DS     T_WORD   T_BYTE   T_SBYTE  T_OPT
  68. %token <u>  T_MACRO  T_ENDM   T_END    T_NUM    T_OBJ    T_MLIST  T_INCLUDE
  69. %token <u>  T_ERROR  T_TITLE  T_DBYTE  T_TAB    T_FLOAT  T_CBYTE  T_XREF
  70. %token <u>  T_SET    T_LIST   T_ERR    T_ACCU   T_CHAR   T_NOT    T_DEF
  71. %token <u>  T_REF    T_CLIST  T_EJECT  T_PAGE   T_XFLOAT T_UNDEF  T_WARN
  72. %token <u>  T_REPT   T_CALL
  73. %token <c>  T_IDENT  T_STRING T_LABEL  T_FILE   
  74. %token <e>  T_EXPR
  75. %token <b>  T_INSTR
  76.  
  77. %type  <e>  irest    s_expr   a_expr   modif   __expr
  78. %type  <l>  t_expr   u_expr   si_expr  mi_expr  i_expr   fubar    ei_expr
  79.  
  80. %left     <e>  ','   T_NO        /* not really... */
  81. %left     <e>  T_OR
  82. %left     <e>  T_AND
  83. %nonassoc <e>  T_LEQ T_GEQ '=' T_NEQ '>' '<'
  84. %left          MIDDLE
  85. %left     <e>  '!'   '^'   '&'
  86. %left     <e>  '+'   '-'
  87. %left     <e>  '*'   '/'   '\\'
  88. %left          HIGHEST
  89.  
  90.  
  91. /* next five tokens are actually not used by YACC, but elsewhere */
  92. %token <u>  T_MPARA
  93. %token <c>  T_MSPARA
  94. %token <u>  T_MLPARA
  95. %token <c>  T_MLSPARA
  96. %token <l>  T_PARA
  97. %token      REALHIGH
  98.  
  99. %%
  100. file  :  source   line
  101.       |  source
  102.       ;
  103.  
  104. source:  source   sline
  105.             {
  106.                inc_line();
  107.                _in_instr = 0;
  108.                dbputchar( '\n');
  109.             }
  110.       |
  111.       |  error
  112.             {
  113.                nterror("unwanted token appeared", yychar);
  114.                while( (yychar = yylex()) && yychar != T_EOL);
  115.                inc_line();
  116.                if( ! yychar)
  117.                   return;
  118.                yyerrok;
  119.                yyclearin;
  120.             }
  121.       ;
  122.  
  123.  
  124. sline :  line     T_EOL
  125.       |  T_EOL
  126.       ;
  127.  
  128. line  :  T_LABEL
  129.                         {
  130.                            enter_pclabel( $1);
  131.                         }
  132.                   nline
  133.                         {
  134.                            dbprintf("T_LABEL nline [done with line]\n\n");
  135.                         }
  136.       |  T_LABEL  '=' '='  __expr
  137.                         {
  138.                            enter_elabel( $1, $4, L_ZERO | L_LINKZERO);
  139.                            dbprintf("T_LABEL == s_expr [done with line]\n\n");
  140.                         }
  141.       |  T_LABEL  '='      __expr
  142.                         {
  143.                            enter_elabel( $1, $3, L_NORMAL);
  144.                            dbprintf("T_LABEL = s_expr [done with line]\n\n");
  145.                         }
  146.       |  T_LABEL  T_DOTEQ  { _in_doteq = 1; }
  147.             __expr
  148.                         {
  149.                            _in_doteq = 0;
  150.                            enter_elabel( $1, $4, L_EQU);
  151.                            dbprintf("T_LABEL .= s_expr [done with line]\n\n");
  152.                         }
  153.       |  T_LABEL        {
  154.                            enter_pclabel( $1);
  155.                            dbprintf("T_LABEL [done with line]\n\n");
  156.                         }
  157.       |  nline          {
  158.                            dbprintf("regular line [done]\n\n");
  159.                         }
  160.       ;
  161.  
  162.  
  163. nline :  T_INSTR  {  _in_instr = 1;  }    irest
  164.                                           { generate( $1,adrmod,$3);}
  165.       |  T_IF     __expr                  { if_treat( $2);          }
  166.       |  T_ELSE                           { else_treat();           }
  167.       |  T_ENDIF                          { endif_treat();          }
  168.       |  T_ORG    __expr                  { setorg( $2, 0);         }
  169.       |  T_DS     __expr                  { reserve( $2);           }
  170.       |  T_WORD   i_expr                  { dropwords(  $2);        }
  171.       |  T_DBYTE  i_expr                  { dropdbytes( $2);        }
  172.       |  T_BYTE   modif    si_expr        { dropbytes(  $2, $3, 0); }
  173.       |  T_CBYTE  modif    si_expr        { dropbytes(  $2, $3, 1); }
  174.       |  T_SBYTE  modif    si_expr        { dropsbytes( $2, $3);    }
  175.       |  T_ZEXT   T_IDENT                 { page0decl( $2);         }
  176.       |  T_FLOAT  f_expr
  177.       |  T_CALL   T_IDENT                 { call_macro( $2);        }
  178.       |  T_IDENT                          { lastident = NIL; 
  179.                                             _call_macro = 1;        }
  180.             ei_expr                       { _call_macro = 0;
  181.                                             do_macro( $1, $3);      }
  182.       |  T_REPT   __expr   T_IDENT        { lastident = NIL;        
  183.                                             _call_macro = 1;        }
  184.             ei_expr                       { _call_macro = 0;
  185.                                             rept_macro( $2, $3, $5);}
  186.       |  T_TAB    i_expr                  { dbprintf("T_TAB");      }
  187.       |  T_OPT    o_expr                  { dbprintf("T_OPT");      }
  188.       |  T_SET    __expr   ','   __expr   { dbprintf("T_SET");      }
  189.       |  T_UNDEF  T_IDENT                 { undefine( $2);          }
  190.       |  T_MACRO  T_IDENT                 {
  191.                                              load_macro( $2);
  192.                                              dbprintf("*** DONE WITH .MACRO ***");
  193.                                           }
  194.       |  T_INCLUDE '#'     T_FILE         {
  195.                                              footok = yylex();
  196.                                              fooval = yylval.u;
  197.                                              include( $3, ".h65");
  198.                                              fix_include( footok, fooval);
  199.                                           }
  200.       |  T_END                            { return( 0);          }
  201.       |  T_LOCAL                          { do_local();          }
  202.       |  T_TITLE  T_STRING                { dbprintf("TITLE\n"); }
  203.       |  T_PAGE   T_STRING                { dbprintf("PAGE\n");  }
  204.       |  T_ERROR  T_STRING                { nerror( $2 + 2);     }
  205.       |  T_WARN   T_STRING                { nwarning( $2 + 2);   }
  206.       ;
  207.  
  208.  
  209. irest :  __expr   ','      'X'            { adrmod = C_RELX; }
  210.       |  __expr   ','      'Y'            { adrmod = C_RELY; }
  211.       |  '('      __expr   ','  'X'   ')' { adrmod = C_INDX; $$ = $2;   }
  212.       |  '('      __expr   ')'  ','   'Y' { adrmod = C_INDY; $$ = $2;   }
  213.       |  '#'      __expr                  { adrmod = C_IMM;  $$ = $2;   }
  214.       |  '('      __expr   ')'            { adrmod = C_IND;  $$ = $2;   }
  215.       |  __expr                           { adrmod = C_ABS;             }
  216.       |  T_ACCU                           { adrmod = C_ACCU; $$ = ENIL; }
  217.       |                                   { adrmod = C_IMPL; $$ = ENIL; }
  218.       ;
  219.  
  220. a_expr:   T_NUMBER                     { $$ = ival_pl( (word) $1);         }
  221.       |   T_IDENT                      { $$ = lval_pl( lastident = $1);    }
  222.       |   T_EXPR
  223.       |   '*'                          {($$ = ival_pl( __pc))->op = O_PCREL;}
  224.       |   T_CHAR                       { $$ = ival_pl( (word) $1);         }
  225.       |   T_DEF    T_IDENT             { $$ = ival_pl( is_def( $2));       }
  226.       |   T_REF    T_IDENT             { $$ = ival_pl( is_ref( $2));       }
  227.       |   T_PARA                       { 
  228.                                           expr  *he;
  229.  
  230.                                           lastident = $1->string + 2;
  231.                                           he = copyexpr( $1->expr);
  232.                                           he->fix = 0;
  233.                                           $$ = he;
  234.                                        }
  235.       ;
  236.  
  237. __expr:   s_expr                          { $1->fix = FIX_NOTHING; }
  238.  
  239. s_expr:   '['      s_expr   ']'           { $$ = $2;                      }
  240.       |   s_expr   T_AND    s_expr        { $$ = op_pl( O_BAND, $1, $3);  }
  241.       |   s_expr   T_OR     s_expr        { $$ = op_pl( O_BOR, $1, $3);   }
  242.       |   s_expr   T_NEQ    s_expr        { $$ = op_pl( O_NEQ, $1, $3);   }
  243.       |   s_expr   T_GEQ    s_expr        { $$ = op_pl( O_GEQ, $1, $3);   }
  244.       |   s_expr   T_LEQ    s_expr        { $$ = op_pl( O_LEQ, $1, $3);   }
  245.       |   s_expr   '<'      s_expr        { $$ = op_pl( O_LT,  $1, $3);   }
  246.       |   s_expr   '>'      s_expr        { $$ = op_pl( O_GT,  $1, $3);   }
  247.       |   s_expr   '+'      s_expr        { $$ = op_pl( O_ADD, $1, $3);   }
  248.       |   s_expr   '-'      s_expr        { $$ = op_pl( O_SUB, $1, $3);   }
  249.       |   s_expr   '/'      s_expr        { $$ = op_pl( O_DIV, $1, $3);   }
  250.       |   s_expr   '*'      s_expr        { $$ = op_pl( O_MUL, $1, $3);   }
  251.       |   s_expr   '\\'     s_expr        { $$ = op_pl( O_MOD, $1, $3);   }
  252.       |   s_expr   '&'      s_expr        { $$ = op_pl( O_AND, $1, $3);   }
  253.       |   s_expr   '!'      s_expr        { $$ = op_pl( O_OR,  $1, $3);   }
  254.       |   s_expr   '^'      s_expr        { $$ = op_pl( O_EOR, $1, $3);   }
  255.       |   s_expr   '='      s_expr        { $$ = op_pl( O_EQ,  $1, $3);   }
  256.       |   T_NOT    s_expr   %prec HIGHEST { $$ = op_pl( O_BNOT,$2, ENIL); }
  257.       |   '-'      s_expr   %prec HIGHEST { $$ = op_pl( O_MIN, $2, ENIL); }
  258.       |   '>'      s_expr   %prec MIDDLE  { $$ = op_pl( O_MSB, $2, ENIL); }
  259.       |   '<'      s_expr   %prec MIDDLE  { $$ = op_pl( O_LSB, $2, ENIL); }
  260.       |   a_expr
  261.       ;
  262.  
  263. fubar :  __expr                           { $$ = lex_pl( NIL, $1); }
  264.       ;
  265.  
  266. i_expr:   fubar
  267.       |   fubar    ','      i_expr        { $$ = lex_ch( $1, $3); }
  268.       ;
  269.  
  270.  
  271. modif :  '+'      __expr    ','           { $$ = $2;   }
  272.       |                                   { $$ = ENIL; }
  273.       ;
  274.  
  275. t_expr:   T_STRING                        { $$ = lex_pl( $1, ENIL);  }
  276.       |   __expr                          { $$ = lex_pl( NIL, $1);   }
  277.       ;
  278.  
  279.  
  280. si_expr:  t_expr
  281.       |   t_expr   ','  si_expr           { $$ = lex_ch( $1, $3);   }
  282.       ;
  283.  
  284. u_expr:   T_STRING         { $$ = lex_pl( $1, ival_pl( wasstrlen)); }
  285.       |   __expr           { $$ = lex_pl( lastident
  286.                                              ? make_string( lastident)
  287.                                              : undefined, $1); }
  288.       ;
  289.  
  290. mi_expr:  u_expr
  291.       |   u_expr   ','                    { lastident = NIL;      }
  292.               mi_expr                     { $$ = lex_ch( $1, $4); }
  293.       ;
  294.  
  295. ei_expr:    { $$ = lex_pl( NIL, ival_pl( 0)); }
  296.       |   mi_expr
  297.             { $$ = slex_ch( lex_pl( NIL, ival_pl( lex_cnt( $1))), $1);  }
  298.       ;
  299.  
  300. f_expr:   T_XFLOAT
  301.       |   f_expr  ','      T_XFLOAT
  302.       ;
  303.  
  304.  
  305. o_expr:   op2
  306.       |   o_expr   ','      op2
  307.       ;
  308.  
  309. op2   :   option
  310.       |   T_NO     option
  311.       ;
  312.  
  313. option:   T_OBJ
  314.       |   T_LIST
  315.       |   T_MLIST
  316.       |   T_CLIST
  317.       |   T_XREF
  318.       |   T_ERR
  319.       |   T_EJECT
  320.       |   T_NUM
  321.       ;
  322.  
  323.