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