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