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