1233237Sjkim%{
2233237Sjkim/******************************************************************************
3233237Sjkim *
4233237Sjkim * Module Name: prparser.y - Bison input file for preprocessor parser
5233237Sjkim *
6233237Sjkim *****************************************************************************/
7233237Sjkim
8233237Sjkim/*
9245582Sjkim * Copyright (C) 2000 - 2013, Intel Corp.
10233237Sjkim * All rights reserved.
11233237Sjkim *
12233237Sjkim * Redistribution and use in source and binary forms, with or without
13233237Sjkim * modification, are permitted provided that the following conditions
14233237Sjkim * are met:
15233237Sjkim * 1. Redistributions of source code must retain the above copyright
16233237Sjkim *    notice, this list of conditions, and the following disclaimer,
17233237Sjkim *    without modification.
18233237Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19233237Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
20233237Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
21233237Sjkim *    including a substantially similar Disclaimer requirement for further
22233237Sjkim *    binary redistribution.
23233237Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
24233237Sjkim *    of any contributors may be used to endorse or promote products derived
25233237Sjkim *    from this software without specific prior written permission.
26233237Sjkim *
27233237Sjkim * Alternatively, this software may be distributed under the terms of the
28233237Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
29233237Sjkim * Software Foundation.
30233237Sjkim *
31233237Sjkim * NO WARRANTY
32233237Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33233237Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34233237Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35233237Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36233237Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37233237Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38233237Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39233237Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40233237Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41233237Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42233237Sjkim * POSSIBILITY OF SUCH DAMAGES.
43233237Sjkim */
44233237Sjkim
45233250Sjkim#include <contrib/dev/acpica/compiler/aslcompiler.h>
46233250Sjkim#include <contrib/dev/acpica/compiler/dtcompiler.h>
47233237Sjkim
48233237Sjkim#define _COMPONENT          ASL_PREPROCESSOR
49233237Sjkim        ACPI_MODULE_NAME    ("prparser")
50233237Sjkim
51233237Sjkimint                         PrParserlex (void);
52233237Sjkimint                         PrParserparse (void);
53233237Sjkimvoid                        PrParsererror (char const *msg);
54233237Sjkimextern char                 *PrParsertext;
55233237Sjkim
56233237SjkimUINT64                      PrParserResult; /* Expression return value */
57233237Sjkim
58233237Sjkim/* Bison/yacc configuration */
59233237Sjkim
60233237Sjkim#define yytname             PrParsername
61233237Sjkim#define YYDEBUG             1               /* Enable debug output */
62233237Sjkim#define YYERROR_VERBOSE     1               /* Verbose error messages */
63233237Sjkim#define YYFLAG              -32768
64233237Sjkim
65233237Sjkim/* Define YYMALLOC/YYFREE to prevent redefinition errors  */
66233237Sjkim
67233237Sjkim#define YYMALLOC            malloc
68233237Sjkim#define YYFREE              free
69233237Sjkim%}
70233237Sjkim
71233237Sjkim%union
72233237Sjkim{
73233237Sjkim     UINT64                 value;
74233237Sjkim     UINT32                 op;
75233237Sjkim     char                   *str;
76233237Sjkim}
77233237Sjkim
78233237Sjkim/*! [Begin] no source code translation */
79233237Sjkim
80233237Sjkim%type  <value>  Expression
81233237Sjkim
82233237Sjkim%token <op>     EXPOP_EOF
83233237Sjkim%token <op>     EXPOP_NEW_LINE
84233237Sjkim%token <op>     EXPOP_NUMBER
85233237Sjkim%token <op>     EXPOP_HEX_NUMBER
86233237Sjkim%token <op>     EXPOP_RESERVED1
87233237Sjkim%token <op>     EXPOP_RESERVED2
88233237Sjkim%token <op>     EXPOP_PAREN_OPEN
89233237Sjkim%token <op>     EXPOP_PAREN_CLOSE
90233237Sjkim
91233237Sjkim%left <op>      EXPOP_LOGICAL_OR
92233237Sjkim%left <op>      EXPOP_LOGICAL_AND
93233237Sjkim%left <op>      EXPOP_OR
94233237Sjkim%left <op>      EXPOP_XOR
95233237Sjkim%left <op>      EXPOP_AND
96233237Sjkim%left <op>      EXPOP_EQUAL EXPOP_NOT_EQUAL
97233237Sjkim%left <op>      EXPOP_GREATER EXPOP_LESS EXPOP_GREATER_EQUAL EXPOP_LESS_EQUAL
98233237Sjkim%left <op>      EXPOP_SHIFT_RIGHT EXPOP_SHIFT_LEFT
99233237Sjkim%left <op>      EXPOP_ADD EXPOP_SUBTRACT
100233237Sjkim%left <op>      EXPOP_MULTIPLY EXPOP_DIVIDE EXPOP_MODULO
101233237Sjkim%right <op>     EXPOP_ONES_COMPLIMENT EXPOP_LOGICAL_NOT
102233237Sjkim
103233237Sjkim/* Tokens above must be kept in synch with dtparser.y */
104233237Sjkim
105233237Sjkim%token <op>     EXPOP_DEFINE
106233237Sjkim%token <op>     EXPOP_IDENTIFIER
107233237Sjkim
108233237Sjkim%%
109233237Sjkim
110233237Sjkim/*
111233237Sjkim *  Operator precedence rules (from K&R)
112233237Sjkim *
113233237Sjkim *  1)      ( )
114233237Sjkim *  2)      ! ~ (unary operators that are supported here)
115233237Sjkim *  3)      *   /   %
116233237Sjkim *  4)      +   -
117233237Sjkim *  5)      >>  <<
118233237Sjkim *  6)      <   >   <=  >=
119233237Sjkim *  7)      ==  !=
120233237Sjkim *  8)      &
121233237Sjkim *  9)      ^
122233237Sjkim *  10)     |
123233237Sjkim *  11)     &&
124233237Sjkim *  12)     ||
125233237Sjkim */
126233237Sjkim
127233237Sjkim/*! [End] no source code translation !*/
128233237Sjkim
129233237SjkimValue
130233237Sjkim    : Expression EXPOP_NEW_LINE                     { PrParserResult=$1; return 0; } /* End of line (newline) */
131233237Sjkim    | Expression EXPOP_EOF                          { PrParserResult=$1; return 0; } /* End of string (0) */
132233237Sjkim    ;
133233237Sjkim
134233237SjkimExpression
135233237Sjkim
136233237Sjkim      /* Unary operators */
137233237Sjkim
138233237Sjkim    : EXPOP_LOGICAL_NOT         Expression          { $$ = DtDoOperator ($2, EXPOP_LOGICAL_NOT,     $2);}
139233237Sjkim    | EXPOP_ONES_COMPLIMENT     Expression          { $$ = DtDoOperator ($2, EXPOP_ONES_COMPLIMENT, $2);}
140233237Sjkim
141233237Sjkim      /* Binary operators */
142233237Sjkim
143233237Sjkim    | Expression EXPOP_MULTIPLY         Expression  { $$ = DtDoOperator ($1, EXPOP_MULTIPLY,        $3);}
144233237Sjkim    | Expression EXPOP_DIVIDE           Expression  { $$ = DtDoOperator ($1, EXPOP_DIVIDE,          $3);}
145233237Sjkim    | Expression EXPOP_MODULO           Expression  { $$ = DtDoOperator ($1, EXPOP_MODULO,          $3);}
146233237Sjkim    | Expression EXPOP_ADD              Expression  { $$ = DtDoOperator ($1, EXPOP_ADD,             $3);}
147233237Sjkim    | Expression EXPOP_SUBTRACT         Expression  { $$ = DtDoOperator ($1, EXPOP_SUBTRACT,        $3);}
148233237Sjkim    | Expression EXPOP_SHIFT_RIGHT      Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_RIGHT,     $3);}
149233237Sjkim    | Expression EXPOP_SHIFT_LEFT       Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_LEFT,      $3);}
150233237Sjkim    | Expression EXPOP_GREATER          Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER,         $3);}
151233237Sjkim    | Expression EXPOP_LESS             Expression  { $$ = DtDoOperator ($1, EXPOP_LESS,            $3);}
152233237Sjkim    | Expression EXPOP_GREATER_EQUAL    Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER_EQUAL,   $3);}
153233237Sjkim    | Expression EXPOP_LESS_EQUAL       Expression  { $$ = DtDoOperator ($1, EXPOP_LESS_EQUAL,      $3);}
154233237Sjkim    | Expression EXPOP_EQUAL            Expression  { $$ = DtDoOperator ($1, EXPOP_EQUAL,           $3);}
155233237Sjkim    | Expression EXPOP_NOT_EQUAL        Expression  { $$ = DtDoOperator ($1, EXPOP_NOT_EQUAL,       $3);}
156233237Sjkim    | Expression EXPOP_AND              Expression  { $$ = DtDoOperator ($1, EXPOP_AND,             $3);}
157233237Sjkim    | Expression EXPOP_XOR              Expression  { $$ = DtDoOperator ($1, EXPOP_XOR,             $3);}
158233237Sjkim    | Expression EXPOP_OR               Expression  { $$ = DtDoOperator ($1, EXPOP_OR,              $3);}
159233237Sjkim    | Expression EXPOP_LOGICAL_AND      Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_AND,     $3);}
160233237Sjkim    | Expression EXPOP_LOGICAL_OR       Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_OR,      $3);}
161233237Sjkim
162233237Sjkim      /* Parentheses: '(' Expression ')' */
163233237Sjkim
164233237Sjkim    | EXPOP_PAREN_OPEN          Expression
165233237Sjkim        EXPOP_PAREN_CLOSE                           { $$ = $2;}
166233237Sjkim
167233237Sjkim      /* #if defined (ID) or #if defined ID */
168233237Sjkim
169233237Sjkim    | EXPOP_DEFINE EXPOP_PAREN_OPEN EXPOP_IDENTIFIER
170233237Sjkim        EXPOP_PAREN_CLOSE                           { $$ = PrIsDefined (PrParserlval.str);}
171233237Sjkim
172233237Sjkim    | EXPOP_DEFINE EXPOP_IDENTIFIER                 { $$ = PrIsDefined (PrParserlval.str);}
173233237Sjkim
174233237Sjkim    | EXPOP_IDENTIFIER                              { $$ = PrResolveDefine (PrParserlval.str);}
175233237Sjkim
176233237Sjkim      /* Default base for a non-prefixed integer is 10 */
177233237Sjkim
178233237Sjkim    | EXPOP_NUMBER                                  { UtStrtoul64 (PrParsertext, 10, &$$);}
179233237Sjkim
180233237Sjkim      /* Standard hex number (0x1234) */
181233237Sjkim
182233237Sjkim    | EXPOP_HEX_NUMBER                              { UtStrtoul64 (PrParsertext, 16, &$$);}
183233237Sjkim    ;
184233237Sjkim%%
185233237Sjkim
186233237Sjkim/*
187233237Sjkim * Local support functions, including parser entry point
188233237Sjkim */
189233237Sjkim#define PR_FIRST_PARSE_OPCODE   EXPOP_EOF
190233237Sjkim#define PR_YYTNAME_START        3
191233237Sjkim
192233237Sjkim
193233237Sjkim/******************************************************************************
194233237Sjkim *
195233237Sjkim * FUNCTION:    PrParsererror
196233237Sjkim *
197233237Sjkim * PARAMETERS:  Message             - Parser-generated error message
198233237Sjkim *
199233237Sjkim * RETURN:      None
200233237Sjkim *
201233237Sjkim * DESCRIPTION: Handler for parser errors
202233237Sjkim *
203233237Sjkim *****************************************************************************/
204233237Sjkim
205233237Sjkimvoid
206233237SjkimPrParsererror (
207233237Sjkim    char const              *Message)
208233237Sjkim{
209233237Sjkim    DtError (ASL_ERROR, ASL_MSG_SYNTAX,
210233237Sjkim        NULL, (char *) Message);
211233237Sjkim}
212233237Sjkim
213233237Sjkim
214233237Sjkim/******************************************************************************
215233237Sjkim *
216233237Sjkim * FUNCTION:    PrGetOpName
217233237Sjkim *
218233237Sjkim * PARAMETERS:  ParseOpcode         - Parser token (EXPOP_*)
219233237Sjkim *
220233237Sjkim * RETURN:      Pointer to the opcode name
221233237Sjkim *
222233237Sjkim * DESCRIPTION: Get the ascii name of the parse opcode for debug output
223233237Sjkim *
224233237Sjkim *****************************************************************************/
225233237Sjkim
226233237Sjkimchar *
227233237SjkimPrGetOpName (
228233237Sjkim    UINT32                  ParseOpcode)
229233237Sjkim{
230233237Sjkim#ifdef ASL_YYTNAME_START
231233237Sjkim    /*
232233237Sjkim     * First entries (PR_YYTNAME_START) in yytname are special reserved names.
233233237Sjkim     * Ignore first 6 characters of name (EXPOP_)
234233237Sjkim     */
235233237Sjkim    return ((char *) yytname
236233237Sjkim        [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
237233237Sjkim#else
238233237Sjkim    return ("[Unknown parser generator]");
239233237Sjkim#endif
240233237Sjkim}
241233237Sjkim
242233237Sjkim
243233237Sjkim/******************************************************************************
244233237Sjkim *
245233237Sjkim * FUNCTION:    PrEvaluateExpression
246233237Sjkim *
247233237Sjkim * PARAMETERS:  ExprString          - Expression to be evaluated. Must be
248233237Sjkim *                                    terminated by either a newline or a NUL
249233237Sjkim *                                    string terminator
250233237Sjkim *
251233237Sjkim * RETURN:      64-bit value for the expression
252233237Sjkim *
253233237Sjkim * DESCRIPTION: Main entry point for the DT expression parser
254233237Sjkim *
255233237Sjkim *****************************************************************************/
256233237Sjkim
257233237SjkimUINT64
258233237SjkimPrEvaluateExpression (
259233237Sjkim    char                    *ExprString)
260233237Sjkim{
261233237Sjkim
262233237Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
263233237Sjkim        "**** Input expression: %s\n", ExprString);
264233237Sjkim
265233237Sjkim    /* Point lexer to the input string */
266233237Sjkim
267233237Sjkim    if (PrInitLexer (ExprString))
268233237Sjkim    {
269233237Sjkim        DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
270233237Sjkim            NULL, "Could not initialize lexer");
271233237Sjkim        return (0);
272233237Sjkim    }
273233237Sjkim
274233237Sjkim    /* Parse/Evaluate the input string (value returned in PrParserResult) */
275233237Sjkim
276233237Sjkim    PrParserparse ();
277233237Sjkim    PrTerminateLexer ();
278233237Sjkim
279233237Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
280233237Sjkim        "**** Parser returned value: %u (%8.8X%8.8X)\n",
281233237Sjkim        (UINT32) PrParserResult, ACPI_FORMAT_UINT64 (PrParserResult));
282233237Sjkim
283233237Sjkim    return (PrParserResult);
284233237Sjkim}
285