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