Home | History | Annotate | Line # | Download | only in compiler
dtparser.y revision 1.12
      1   1.1    jruoho %{
      2   1.1    jruoho /******************************************************************************
      3   1.1    jruoho  *
      4   1.1    jruoho  * Module Name: dtparser.y - Bison input file for table compiler parser
      5   1.1    jruoho  *
      6   1.1    jruoho  *****************************************************************************/
      7   1.1    jruoho 
      8   1.1    jruoho /*
      9  1.12  christos  * Copyright (C) 2000 - 2018, Intel Corp.
     10   1.1    jruoho  * All rights reserved.
     11   1.1    jruoho  *
     12   1.1    jruoho  * Redistribution and use in source and binary forms, with or without
     13   1.1    jruoho  * modification, are permitted provided that the following conditions
     14   1.1    jruoho  * are met:
     15   1.1    jruoho  * 1. Redistributions of source code must retain the above copyright
     16   1.1    jruoho  *    notice, this list of conditions, and the following disclaimer,
     17   1.1    jruoho  *    without modification.
     18   1.1    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     19   1.1    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     20   1.1    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     21   1.1    jruoho  *    including a substantially similar Disclaimer requirement for further
     22   1.1    jruoho  *    binary redistribution.
     23   1.1    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     24   1.1    jruoho  *    of any contributors may be used to endorse or promote products derived
     25   1.1    jruoho  *    from this software without specific prior written permission.
     26   1.1    jruoho  *
     27   1.1    jruoho  * Alternatively, this software may be distributed under the terms of the
     28   1.1    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     29   1.1    jruoho  * Software Foundation.
     30   1.1    jruoho  *
     31   1.1    jruoho  * NO WARRANTY
     32   1.1    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     33   1.1    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     34   1.1    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     35   1.1    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     36   1.1    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37   1.1    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38   1.1    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39   1.1    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     40   1.1    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     41   1.1    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     42   1.1    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     43   1.1    jruoho  */
     44   1.1    jruoho 
     45   1.1    jruoho #include "aslcompiler.h"
     46   1.1    jruoho 
     47   1.1    jruoho #define _COMPONENT          DT_COMPILER
     48   1.1    jruoho         ACPI_MODULE_NAME    ("dtparser")
     49   1.1    jruoho 
     50   1.5  christos void *                      AslLocalAllocate (unsigned int Size);
     51   1.5  christos 
     52   1.5  christos /* Bison/yacc configuration */
     53   1.5  christos 
     54   1.5  christos #undef alloca
     55   1.5  christos #define alloca              AslLocalAllocate
     56   1.5  christos 
     57   1.1    jruoho int                         DtParserlex (void);
     58   1.1    jruoho int                         DtParserparse (void);
     59   1.1    jruoho void                        DtParsererror (char const *msg);
     60   1.1    jruoho extern char                 *DtParsertext;
     61   1.1    jruoho extern DT_FIELD             *Gbl_CurrentField;
     62   1.1    jruoho 
     63   1.1    jruoho UINT64                      DtParserResult; /* Expression return value */
     64   1.1    jruoho 
     65   1.1    jruoho /* Bison/yacc configuration */
     66   1.1    jruoho 
     67   1.2  christos #ifndef yytname
     68   1.1    jruoho #define yytname             DtParsername
     69   1.2  christos #endif
     70   1.1    jruoho #define YYDEBUG             1               /* Enable debug output */
     71   1.1    jruoho #define YYERROR_VERBOSE     1               /* Verbose error messages */
     72   1.1    jruoho #define YYFLAG              -32768
     73   1.1    jruoho 
     74   1.2  christos /* Define YYMALLOC/YYFREE to prevent redefinition errors  */
     75   1.2  christos 
     76   1.2  christos #define YYMALLOC            malloc
     77   1.2  christos #define YYFREE              free
     78   1.1    jruoho %}
     79   1.1    jruoho 
     80   1.1    jruoho %union
     81   1.1    jruoho {
     82   1.1    jruoho      UINT64                 value;
     83   1.1    jruoho      UINT32                 op;
     84   1.1    jruoho }
     85   1.1    jruoho 
     86   1.1    jruoho /*! [Begin] no source code translation */
     87   1.1    jruoho 
     88   1.1    jruoho %type  <value>  Expression
     89   1.1    jruoho 
     90  1.12  christos %token <op>     OP_EXP_EOF
     91  1.12  christos %token <op>     OP_EXP_NEW_LINE
     92  1.12  christos %token <op>     OP_EXP_NUMBER
     93  1.12  christos %token <op>     OP_EXP_HEX_NUMBER
     94  1.12  christos %token <op>     OP_EXP_DECIMAL_NUMBER
     95  1.12  christos %token <op>     OP_EXP_LABEL
     96  1.12  christos %token <op>     OP_EXP_PAREN_OPEN
     97  1.12  christos %token <op>     OP_EXP_PAREN_CLOSE
     98  1.12  christos 
     99  1.12  christos %left <op>      OP_EXP_LOGICAL_OR
    100  1.12  christos %left <op>      OP_EXP_LOGICAL_AND
    101  1.12  christos %left <op>      OP_EXP_OR
    102  1.12  christos %left <op>      OP_EXP_XOR
    103  1.12  christos %left <op>      OP_EXP_AND
    104  1.12  christos %left <op>      OP_EXP_EQUAL OP_EXP_NOT_EQUAL
    105  1.12  christos %left <op>      OP_EXP_GREATER OP_EXP_LESS OP_EXP_GREATER_EQUAL OP_EXP_LESS_EQUAL
    106  1.12  christos %left <op>      OP_EXP_SHIFT_RIGHT OP_EXP_SHIFT_LEFT
    107  1.12  christos %left <op>      OP_EXP_ADD OP_EXP_SUBTRACT
    108  1.12  christos %left <op>      OP_EXP_MULTIPLY OP_EXP_DIVIDE OP_EXP_MODULO
    109  1.12  christos %right <op>     OP_EXP_ONES_COMPLIMENT OP_EXP_LOGICAL_NOT
    110   1.1    jruoho 
    111   1.1    jruoho %%
    112   1.1    jruoho 
    113   1.1    jruoho /*
    114   1.1    jruoho  *  Operator precedence rules (from K&R)
    115   1.1    jruoho  *
    116   1.1    jruoho  *  1)      ( )
    117   1.1    jruoho  *  2)      ! ~ (unary operators that are supported here)
    118   1.1    jruoho  *  3)      *   /   %
    119   1.1    jruoho  *  4)      +   -
    120   1.1    jruoho  *  5)      >>  <<
    121   1.1    jruoho  *  6)      <   >   <=  >=
    122   1.1    jruoho  *  7)      ==  !=
    123   1.1    jruoho  *  8)      &
    124   1.1    jruoho  *  9)      ^
    125   1.1    jruoho  *  10)     |
    126   1.1    jruoho  *  11)     &&
    127   1.1    jruoho  *  12)     ||
    128   1.1    jruoho  */
    129   1.1    jruoho Value
    130  1.12  christos     : Expression OP_EXP_NEW_LINE                     { DtParserResult=$1; return 0; } /* End of line (newline) */
    131  1.12  christos     | Expression OP_EXP_EOF                          { DtParserResult=$1; return 0; } /* End of string (0) */
    132   1.1    jruoho     ;
    133   1.1    jruoho 
    134   1.1    jruoho Expression
    135   1.1    jruoho 
    136   1.1    jruoho       /* Unary operators */
    137   1.1    jruoho 
    138  1.12  christos     : OP_EXP_LOGICAL_NOT         Expression          { $$ = DtDoOperator ($2, OP_EXP_LOGICAL_NOT,     $2);}
    139  1.12  christos     | OP_EXP_ONES_COMPLIMENT     Expression          { $$ = DtDoOperator ($2, OP_EXP_ONES_COMPLIMENT, $2);}
    140   1.1    jruoho 
    141   1.1    jruoho       /* Binary operators */
    142   1.1    jruoho 
    143  1.12  christos     | Expression OP_EXP_MULTIPLY         Expression  { $$ = DtDoOperator ($1, OP_EXP_MULTIPLY,        $3);}
    144  1.12  christos     | Expression OP_EXP_DIVIDE           Expression  { $$ = DtDoOperator ($1, OP_EXP_DIVIDE,          $3);}
    145  1.12  christos     | Expression OP_EXP_MODULO           Expression  { $$ = DtDoOperator ($1, OP_EXP_MODULO,          $3);}
    146  1.12  christos     | Expression OP_EXP_ADD              Expression  { $$ = DtDoOperator ($1, OP_EXP_ADD,             $3);}
    147  1.12  christos     | Expression OP_EXP_SUBTRACT         Expression  { $$ = DtDoOperator ($1, OP_EXP_SUBTRACT,        $3);}
    148  1.12  christos     | Expression OP_EXP_SHIFT_RIGHT      Expression  { $$ = DtDoOperator ($1, OP_EXP_SHIFT_RIGHT,     $3);}
    149  1.12  christos     | Expression OP_EXP_SHIFT_LEFT       Expression  { $$ = DtDoOperator ($1, OP_EXP_SHIFT_LEFT,      $3);}
    150  1.12  christos     | Expression OP_EXP_GREATER          Expression  { $$ = DtDoOperator ($1, OP_EXP_GREATER,         $3);}
    151  1.12  christos     | Expression OP_EXP_LESS             Expression  { $$ = DtDoOperator ($1, OP_EXP_LESS,            $3);}
    152  1.12  christos     | Expression OP_EXP_GREATER_EQUAL    Expression  { $$ = DtDoOperator ($1, OP_EXP_GREATER_EQUAL,   $3);}
    153  1.12  christos     | Expression OP_EXP_LESS_EQUAL       Expression  { $$ = DtDoOperator ($1, OP_EXP_LESS_EQUAL,      $3);}
    154  1.12  christos     | Expression OP_EXP_EQUAL            Expression  { $$ = DtDoOperator ($1, OP_EXP_EQUAL,           $3);}
    155  1.12  christos     | Expression OP_EXP_NOT_EQUAL        Expression  { $$ = DtDoOperator ($1, OP_EXP_NOT_EQUAL,       $3);}
    156  1.12  christos     | Expression OP_EXP_AND              Expression  { $$ = DtDoOperator ($1, OP_EXP_AND,             $3);}
    157  1.12  christos     | Expression OP_EXP_XOR              Expression  { $$ = DtDoOperator ($1, OP_EXP_XOR,             $3);}
    158  1.12  christos     | Expression OP_EXP_OR               Expression  { $$ = DtDoOperator ($1, OP_EXP_OR,              $3);}
    159  1.12  christos     | Expression OP_EXP_LOGICAL_AND      Expression  { $$ = DtDoOperator ($1, OP_EXP_LOGICAL_AND,     $3);}
    160  1.12  christos     | Expression OP_EXP_LOGICAL_OR       Expression  { $$ = DtDoOperator ($1, OP_EXP_LOGICAL_OR,      $3);}
    161   1.1    jruoho 
    162   1.1    jruoho       /* Parentheses: '(' Expression ')' */
    163   1.1    jruoho 
    164  1.12  christos     | OP_EXP_PAREN_OPEN          Expression
    165  1.12  christos         OP_EXP_PAREN_CLOSE                           { $$ = $2;}
    166   1.1    jruoho 
    167   1.1    jruoho       /* Label references (prefixed with $) */
    168   1.1    jruoho 
    169  1.12  christos     | OP_EXP_LABEL                                   { $$ = DtResolveLabel (DtParsertext);}
    170   1.1    jruoho 
    171  1.10  christos       /*
    172  1.10  christos        * All constants for the data table compiler are in hex, whether a (optional) 0x
    173  1.10  christos        * prefix is present or not. For example, these two input strings are equivalent:
    174  1.10  christos        *    1234
    175  1.10  christos        *    0x1234
    176  1.10  christos        */
    177   1.1    jruoho 
    178  1.10  christos       /* Non-prefixed hex number */
    179  1.10  christos 
    180  1.12  christos     | OP_EXP_NUMBER                                  { $$ = DtDoConstant (DtParsertext);}
    181   1.1    jruoho 
    182   1.1    jruoho       /* Standard hex number (0x1234) */
    183   1.1    jruoho 
    184  1.12  christos     | OP_EXP_HEX_NUMBER                              { $$ = DtDoConstant (DtParsertext);}
    185   1.1    jruoho 
    186  1.10  christos       /* Possible TBD: Decimal number with prefix (0d1234) - Not supported this time */
    187   1.1    jruoho 
    188  1.12  christos     | OP_EXP_DECIMAL_NUMBER                          { $$ = DtDoConstant (DtParsertext);}
    189   1.1    jruoho     ;
    190   1.1    jruoho %%
    191   1.1    jruoho 
    192   1.1    jruoho /*! [End] no source code translation !*/
    193   1.1    jruoho 
    194   1.1    jruoho /*
    195   1.1    jruoho  * Local support functions, including parser entry point
    196   1.1    jruoho  */
    197  1.12  christos #define PR_FIRST_PARSE_OPCODE   OP_EXP_EOF
    198   1.1    jruoho #define PR_YYTNAME_START        3
    199   1.1    jruoho 
    200   1.1    jruoho 
    201   1.1    jruoho /******************************************************************************
    202   1.1    jruoho  *
    203   1.1    jruoho  * FUNCTION:    DtParsererror
    204   1.1    jruoho  *
    205   1.1    jruoho  * PARAMETERS:  Message             - Parser-generated error message
    206   1.1    jruoho  *
    207   1.1    jruoho  * RETURN:      None
    208   1.1    jruoho  *
    209   1.1    jruoho  * DESCRIPTION: Handler for parser errors
    210   1.1    jruoho  *
    211   1.1    jruoho  *****************************************************************************/
    212   1.1    jruoho 
    213   1.1    jruoho void
    214   1.1    jruoho DtParsererror (
    215   1.1    jruoho     char const              *Message)
    216   1.1    jruoho {
    217   1.1    jruoho     DtError (ASL_ERROR, ASL_MSG_SYNTAX,
    218   1.1    jruoho         Gbl_CurrentField, (char *) Message);
    219   1.1    jruoho }
    220   1.1    jruoho 
    221   1.1    jruoho 
    222   1.1    jruoho /******************************************************************************
    223   1.1    jruoho  *
    224   1.1    jruoho  * FUNCTION:    DtGetOpName
    225   1.1    jruoho  *
    226  1.12  christos  * PARAMETERS:  ParseOpcode         - Parser token (OP_EXP_*)
    227   1.1    jruoho  *
    228   1.1    jruoho  * RETURN:      Pointer to the opcode name
    229   1.1    jruoho  *
    230   1.1    jruoho  * DESCRIPTION: Get the ascii name of the parse opcode for debug output
    231   1.1    jruoho  *
    232   1.1    jruoho  *****************************************************************************/
    233   1.1    jruoho 
    234   1.1    jruoho char *
    235   1.1    jruoho DtGetOpName (
    236   1.1    jruoho     UINT32                  ParseOpcode)
    237   1.1    jruoho {
    238   1.1    jruoho #ifdef ASL_YYTNAME_START
    239   1.1    jruoho     /*
    240   1.1    jruoho      * First entries (PR_YYTNAME_START) in yytname are special reserved names.
    241  1.12  christos      * Ignore first 6 characters of name (OP_EXP_)
    242   1.1    jruoho      */
    243   1.1    jruoho     return ((char *) yytname
    244   1.1    jruoho         [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
    245   1.1    jruoho #else
    246   1.1    jruoho     return ("[Unknown parser generator]");
    247   1.1    jruoho #endif
    248   1.1    jruoho }
    249   1.1    jruoho 
    250   1.1    jruoho 
    251   1.1    jruoho /******************************************************************************
    252   1.1    jruoho  *
    253   1.1    jruoho  * FUNCTION:    DtEvaluateExpression
    254   1.1    jruoho  *
    255   1.1    jruoho  * PARAMETERS:  ExprString          - Expression to be evaluated. Must be
    256   1.1    jruoho  *                                    terminated by either a newline or a NUL
    257   1.1    jruoho  *                                    string terminator
    258   1.1    jruoho  *
    259   1.1    jruoho  * RETURN:      64-bit value for the expression
    260   1.1    jruoho  *
    261   1.1    jruoho  * DESCRIPTION: Main entry point for the DT expression parser
    262   1.1    jruoho  *
    263   1.1    jruoho  *****************************************************************************/
    264   1.1    jruoho 
    265   1.1    jruoho UINT64
    266   1.1    jruoho DtEvaluateExpression (
    267   1.1    jruoho     char                    *ExprString)
    268   1.1    jruoho {
    269   1.1    jruoho 
    270   1.1    jruoho     DbgPrint (ASL_DEBUG_OUTPUT,
    271   1.1    jruoho         "**** Input expression: %s  (Base 16)\n", ExprString);
    272   1.1    jruoho 
    273   1.1    jruoho     /* Point lexer to the input string */
    274   1.1    jruoho 
    275   1.1    jruoho     if (DtInitLexer (ExprString))
    276   1.1    jruoho     {
    277   1.1    jruoho         DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
    278   1.1    jruoho             Gbl_CurrentField, "Could not initialize lexer");
    279   1.1    jruoho         return (0);
    280   1.1    jruoho     }
    281   1.1    jruoho 
    282   1.1    jruoho     /* Parse/Evaluate the input string (value returned in DtParserResult) */
    283   1.1    jruoho 
    284   1.1    jruoho     DtParserparse ();
    285   1.1    jruoho     DtTerminateLexer ();
    286   1.1    jruoho 
    287   1.1    jruoho     DbgPrint (ASL_DEBUG_OUTPUT,
    288   1.1    jruoho         "**** Parser returned value: %u (%8.8X%8.8X)\n",
    289   1.1    jruoho         (UINT32) DtParserResult, ACPI_FORMAT_UINT64 (DtParserResult));
    290   1.1    jruoho 
    291   1.1    jruoho     return (DtParserResult);
    292   1.1    jruoho }
    293