Home | History | Annotate | Line # | Download | only in compiler
dtparser.y revision 1.10
      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.9  christos  * Copyright (C) 2000 - 2017, 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 #include "dtcompiler.h"
     47   1.1    jruoho 
     48   1.1    jruoho #define _COMPONENT          DT_COMPILER
     49   1.1    jruoho         ACPI_MODULE_NAME    ("dtparser")
     50   1.1    jruoho 
     51   1.5  christos void *                      AslLocalAllocate (unsigned int Size);
     52   1.5  christos 
     53   1.5  christos /* Bison/yacc configuration */
     54   1.5  christos 
     55   1.5  christos #undef alloca
     56   1.5  christos #define alloca              AslLocalAllocate
     57   1.5  christos 
     58   1.1    jruoho int                         DtParserlex (void);
     59   1.1    jruoho int                         DtParserparse (void);
     60   1.1    jruoho void                        DtParsererror (char const *msg);
     61   1.1    jruoho extern char                 *DtParsertext;
     62   1.1    jruoho extern DT_FIELD             *Gbl_CurrentField;
     63   1.1    jruoho 
     64   1.1    jruoho UINT64                      DtParserResult; /* Expression return value */
     65   1.1    jruoho 
     66   1.1    jruoho /* Bison/yacc configuration */
     67   1.1    jruoho 
     68   1.2  christos #ifndef yytname
     69   1.1    jruoho #define yytname             DtParsername
     70   1.2  christos #endif
     71   1.1    jruoho #define YYDEBUG             1               /* Enable debug output */
     72   1.1    jruoho #define YYERROR_VERBOSE     1               /* Verbose error messages */
     73   1.1    jruoho #define YYFLAG              -32768
     74   1.1    jruoho 
     75   1.2  christos /* Define YYMALLOC/YYFREE to prevent redefinition errors  */
     76   1.2  christos 
     77   1.2  christos #define YYMALLOC            malloc
     78   1.2  christos #define YYFREE              free
     79   1.1    jruoho %}
     80   1.1    jruoho 
     81   1.1    jruoho %union
     82   1.1    jruoho {
     83   1.1    jruoho      UINT64                 value;
     84   1.1    jruoho      UINT32                 op;
     85   1.1    jruoho }
     86   1.1    jruoho 
     87   1.1    jruoho /*! [Begin] no source code translation */
     88   1.1    jruoho 
     89   1.1    jruoho %type  <value>  Expression
     90   1.1    jruoho 
     91   1.1    jruoho %token <op>     EXPOP_EOF
     92   1.1    jruoho %token <op>     EXPOP_NEW_LINE
     93   1.1    jruoho %token <op>     EXPOP_NUMBER
     94   1.1    jruoho %token <op>     EXPOP_HEX_NUMBER
     95   1.1    jruoho %token <op>     EXPOP_DECIMAL_NUMBER
     96   1.1    jruoho %token <op>     EXPOP_LABEL
     97   1.1    jruoho %token <op>     EXPOP_PAREN_OPEN
     98   1.1    jruoho %token <op>     EXPOP_PAREN_CLOSE
     99   1.1    jruoho 
    100   1.1    jruoho %left <op>      EXPOP_LOGICAL_OR
    101   1.1    jruoho %left <op>      EXPOP_LOGICAL_AND
    102   1.1    jruoho %left <op>      EXPOP_OR
    103   1.1    jruoho %left <op>      EXPOP_XOR
    104   1.1    jruoho %left <op>      EXPOP_AND
    105   1.1    jruoho %left <op>      EXPOP_EQUAL EXPOP_NOT_EQUAL
    106   1.1    jruoho %left <op>      EXPOP_GREATER EXPOP_LESS EXPOP_GREATER_EQUAL EXPOP_LESS_EQUAL
    107   1.1    jruoho %left <op>      EXPOP_SHIFT_RIGHT EXPOP_SHIFT_LEFT
    108   1.1    jruoho %left <op>      EXPOP_ADD EXPOP_SUBTRACT
    109   1.1    jruoho %left <op>      EXPOP_MULTIPLY EXPOP_DIVIDE EXPOP_MODULO
    110   1.1    jruoho %right <op>     EXPOP_ONES_COMPLIMENT EXPOP_LOGICAL_NOT
    111   1.1    jruoho 
    112   1.1    jruoho %%
    113   1.1    jruoho 
    114   1.1    jruoho /*
    115   1.1    jruoho  *  Operator precedence rules (from K&R)
    116   1.1    jruoho  *
    117   1.1    jruoho  *  1)      ( )
    118   1.1    jruoho  *  2)      ! ~ (unary operators that are supported here)
    119   1.1    jruoho  *  3)      *   /   %
    120   1.1    jruoho  *  4)      +   -
    121   1.1    jruoho  *  5)      >>  <<
    122   1.1    jruoho  *  6)      <   >   <=  >=
    123   1.1    jruoho  *  7)      ==  !=
    124   1.1    jruoho  *  8)      &
    125   1.1    jruoho  *  9)      ^
    126   1.1    jruoho  *  10)     |
    127   1.1    jruoho  *  11)     &&
    128   1.1    jruoho  *  12)     ||
    129   1.1    jruoho  */
    130   1.1    jruoho Value
    131   1.1    jruoho     : Expression EXPOP_NEW_LINE                     { DtParserResult=$1; return 0; } /* End of line (newline) */
    132   1.1    jruoho     | Expression EXPOP_EOF                          { DtParserResult=$1; return 0; } /* End of string (0) */
    133   1.1    jruoho     ;
    134   1.1    jruoho 
    135   1.1    jruoho Expression
    136   1.1    jruoho 
    137   1.1    jruoho       /* Unary operators */
    138   1.1    jruoho 
    139   1.1    jruoho     : EXPOP_LOGICAL_NOT         Expression          { $$ = DtDoOperator ($2, EXPOP_LOGICAL_NOT,     $2);}
    140   1.1    jruoho     | EXPOP_ONES_COMPLIMENT     Expression          { $$ = DtDoOperator ($2, EXPOP_ONES_COMPLIMENT, $2);}
    141   1.1    jruoho 
    142   1.1    jruoho       /* Binary operators */
    143   1.1    jruoho 
    144   1.1    jruoho     | Expression EXPOP_MULTIPLY         Expression  { $$ = DtDoOperator ($1, EXPOP_MULTIPLY,        $3);}
    145   1.1    jruoho     | Expression EXPOP_DIVIDE           Expression  { $$ = DtDoOperator ($1, EXPOP_DIVIDE,          $3);}
    146   1.1    jruoho     | Expression EXPOP_MODULO           Expression  { $$ = DtDoOperator ($1, EXPOP_MODULO,          $3);}
    147   1.1    jruoho     | Expression EXPOP_ADD              Expression  { $$ = DtDoOperator ($1, EXPOP_ADD,             $3);}
    148   1.1    jruoho     | Expression EXPOP_SUBTRACT         Expression  { $$ = DtDoOperator ($1, EXPOP_SUBTRACT,        $3);}
    149   1.1    jruoho     | Expression EXPOP_SHIFT_RIGHT      Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_RIGHT,     $3);}
    150   1.1    jruoho     | Expression EXPOP_SHIFT_LEFT       Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_LEFT,      $3);}
    151   1.1    jruoho     | Expression EXPOP_GREATER          Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER,         $3);}
    152   1.1    jruoho     | Expression EXPOP_LESS             Expression  { $$ = DtDoOperator ($1, EXPOP_LESS,            $3);}
    153   1.1    jruoho     | Expression EXPOP_GREATER_EQUAL    Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER_EQUAL,   $3);}
    154   1.1    jruoho     | Expression EXPOP_LESS_EQUAL       Expression  { $$ = DtDoOperator ($1, EXPOP_LESS_EQUAL,      $3);}
    155   1.1    jruoho     | Expression EXPOP_EQUAL            Expression  { $$ = DtDoOperator ($1, EXPOP_EQUAL,           $3);}
    156   1.1    jruoho     | Expression EXPOP_NOT_EQUAL        Expression  { $$ = DtDoOperator ($1, EXPOP_NOT_EQUAL,       $3);}
    157   1.1    jruoho     | Expression EXPOP_AND              Expression  { $$ = DtDoOperator ($1, EXPOP_AND,             $3);}
    158   1.1    jruoho     | Expression EXPOP_XOR              Expression  { $$ = DtDoOperator ($1, EXPOP_XOR,             $3);}
    159   1.1    jruoho     | Expression EXPOP_OR               Expression  { $$ = DtDoOperator ($1, EXPOP_OR,              $3);}
    160   1.1    jruoho     | Expression EXPOP_LOGICAL_AND      Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_AND,     $3);}
    161   1.1    jruoho     | Expression EXPOP_LOGICAL_OR       Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_OR,      $3);}
    162   1.1    jruoho 
    163   1.1    jruoho       /* Parentheses: '(' Expression ')' */
    164   1.1    jruoho 
    165   1.1    jruoho     | EXPOP_PAREN_OPEN          Expression
    166   1.1    jruoho         EXPOP_PAREN_CLOSE                           { $$ = $2;}
    167   1.1    jruoho 
    168   1.1    jruoho       /* Label references (prefixed with $) */
    169   1.1    jruoho 
    170   1.1    jruoho     | EXPOP_LABEL                                   { $$ = DtResolveLabel (DtParsertext);}
    171   1.1    jruoho 
    172  1.10  christos       /*
    173  1.10  christos        * All constants for the data table compiler are in hex, whether a (optional) 0x
    174  1.10  christos        * prefix is present or not. For example, these two input strings are equivalent:
    175  1.10  christos        *    1234
    176  1.10  christos        *    0x1234
    177  1.10  christos        */
    178   1.1    jruoho 
    179  1.10  christos       /* Non-prefixed hex number */
    180  1.10  christos 
    181  1.10  christos     | EXPOP_NUMBER                                  { $$ = DtDoConstant (DtParsertext);}
    182   1.1    jruoho 
    183   1.1    jruoho       /* Standard hex number (0x1234) */
    184   1.1    jruoho 
    185  1.10  christos     | EXPOP_HEX_NUMBER                              { $$ = DtDoConstant (DtParsertext);}
    186   1.1    jruoho 
    187  1.10  christos       /* Possible TBD: Decimal number with prefix (0d1234) - Not supported this time */
    188   1.1    jruoho 
    189  1.10  christos     | EXPOP_DECIMAL_NUMBER                          { $$ = DtDoConstant (DtParsertext);}
    190   1.1    jruoho     ;
    191   1.1    jruoho %%
    192   1.1    jruoho 
    193   1.1    jruoho /*! [End] no source code translation !*/
    194   1.1    jruoho 
    195   1.1    jruoho /*
    196   1.1    jruoho  * Local support functions, including parser entry point
    197   1.1    jruoho  */
    198   1.1    jruoho #define PR_FIRST_PARSE_OPCODE   EXPOP_EOF
    199   1.1    jruoho #define PR_YYTNAME_START        3
    200   1.1    jruoho 
    201   1.1    jruoho 
    202   1.1    jruoho /******************************************************************************
    203   1.1    jruoho  *
    204   1.1    jruoho  * FUNCTION:    DtParsererror
    205   1.1    jruoho  *
    206   1.1    jruoho  * PARAMETERS:  Message             - Parser-generated error message
    207   1.1    jruoho  *
    208   1.1    jruoho  * RETURN:      None
    209   1.1    jruoho  *
    210   1.1    jruoho  * DESCRIPTION: Handler for parser errors
    211   1.1    jruoho  *
    212   1.1    jruoho  *****************************************************************************/
    213   1.1    jruoho 
    214   1.1    jruoho void
    215   1.1    jruoho DtParsererror (
    216   1.1    jruoho     char const              *Message)
    217   1.1    jruoho {
    218   1.1    jruoho     DtError (ASL_ERROR, ASL_MSG_SYNTAX,
    219   1.1    jruoho         Gbl_CurrentField, (char *) Message);
    220   1.1    jruoho }
    221   1.1    jruoho 
    222   1.1    jruoho 
    223   1.1    jruoho /******************************************************************************
    224   1.1    jruoho  *
    225   1.1    jruoho  * FUNCTION:    DtGetOpName
    226   1.1    jruoho  *
    227   1.1    jruoho  * PARAMETERS:  ParseOpcode         - Parser token (EXPOP_*)
    228   1.1    jruoho  *
    229   1.1    jruoho  * RETURN:      Pointer to the opcode name
    230   1.1    jruoho  *
    231   1.1    jruoho  * DESCRIPTION: Get the ascii name of the parse opcode for debug output
    232   1.1    jruoho  *
    233   1.1    jruoho  *****************************************************************************/
    234   1.1    jruoho 
    235   1.1    jruoho char *
    236   1.1    jruoho DtGetOpName (
    237   1.1    jruoho     UINT32                  ParseOpcode)
    238   1.1    jruoho {
    239   1.1    jruoho #ifdef ASL_YYTNAME_START
    240   1.1    jruoho     /*
    241   1.1    jruoho      * First entries (PR_YYTNAME_START) in yytname are special reserved names.
    242   1.1    jruoho      * Ignore first 6 characters of name (EXPOP_)
    243   1.1    jruoho      */
    244   1.1    jruoho     return ((char *) yytname
    245   1.1    jruoho         [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
    246   1.1    jruoho #else
    247   1.1    jruoho     return ("[Unknown parser generator]");
    248   1.1    jruoho #endif
    249   1.1    jruoho }
    250   1.1    jruoho 
    251   1.1    jruoho 
    252   1.1    jruoho /******************************************************************************
    253   1.1    jruoho  *
    254   1.1    jruoho  * FUNCTION:    DtEvaluateExpression
    255   1.1    jruoho  *
    256   1.1    jruoho  * PARAMETERS:  ExprString          - Expression to be evaluated. Must be
    257   1.1    jruoho  *                                    terminated by either a newline or a NUL
    258   1.1    jruoho  *                                    string terminator
    259   1.1    jruoho  *
    260   1.1    jruoho  * RETURN:      64-bit value for the expression
    261   1.1    jruoho  *
    262   1.1    jruoho  * DESCRIPTION: Main entry point for the DT expression parser
    263   1.1    jruoho  *
    264   1.1    jruoho  *****************************************************************************/
    265   1.1    jruoho 
    266   1.1    jruoho UINT64
    267   1.1    jruoho DtEvaluateExpression (
    268   1.1    jruoho     char                    *ExprString)
    269   1.1    jruoho {
    270   1.1    jruoho 
    271   1.1    jruoho     DbgPrint (ASL_DEBUG_OUTPUT,
    272   1.1    jruoho         "**** Input expression: %s  (Base 16)\n", ExprString);
    273   1.1    jruoho 
    274   1.1    jruoho     /* Point lexer to the input string */
    275   1.1    jruoho 
    276   1.1    jruoho     if (DtInitLexer (ExprString))
    277   1.1    jruoho     {
    278   1.1    jruoho         DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
    279   1.1    jruoho             Gbl_CurrentField, "Could not initialize lexer");
    280   1.1    jruoho         return (0);
    281   1.1    jruoho     }
    282   1.1    jruoho 
    283   1.1    jruoho     /* Parse/Evaluate the input string (value returned in DtParserResult) */
    284   1.1    jruoho 
    285   1.1    jruoho     DtParserparse ();
    286   1.1    jruoho     DtTerminateLexer ();
    287   1.1    jruoho 
    288   1.1    jruoho     DbgPrint (ASL_DEBUG_OUTPUT,
    289   1.1    jruoho         "**** Parser returned value: %u (%8.8X%8.8X)\n",
    290   1.1    jruoho         (UINT32) DtParserResult, ACPI_FORMAT_UINT64 (DtParserResult));
    291   1.1    jruoho 
    292   1.1    jruoho     return (DtParserResult);
    293   1.1    jruoho }
    294