1233237Sjkim%{
2233237Sjkim/******************************************************************************
3233237Sjkim *
4233237Sjkim * Module Name: prparser.y - Bison input file for preprocessor parser
5233237Sjkim *
6233237Sjkim *****************************************************************************/
7233237Sjkim
8233237Sjkim/*
9306536Sjkim * Copyright (C) 2000 - 2016, 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
51306536Sjkimvoid *                      AslLocalAllocate (unsigned int Size);
52306536Sjkim
53306536Sjkim/* Bison/yacc configuration */
54306536Sjkim
55306536Sjkim#undef alloca
56306536Sjkim#define alloca              AslLocalAllocate
57306536Sjkim
58233237Sjkimint                         PrParserlex (void);
59233237Sjkimint                         PrParserparse (void);
60233237Sjkimvoid                        PrParsererror (char const *msg);
61233237Sjkimextern char                 *PrParsertext;
62233237Sjkim
63233237SjkimUINT64                      PrParserResult; /* Expression return value */
64233237Sjkim
65233237Sjkim/* Bison/yacc configuration */
66233237Sjkim
67233237Sjkim#define yytname             PrParsername
68233237Sjkim#define YYDEBUG             1               /* Enable debug output */
69233237Sjkim#define YYERROR_VERBOSE     1               /* Verbose error messages */
70233237Sjkim#define YYFLAG              -32768
71233237Sjkim
72233237Sjkim/* Define YYMALLOC/YYFREE to prevent redefinition errors  */
73233237Sjkim
74233237Sjkim#define YYMALLOC            malloc
75233237Sjkim#define YYFREE              free
76233237Sjkim%}
77233237Sjkim
78233237Sjkim%union
79233237Sjkim{
80233237Sjkim     UINT64                 value;
81233237Sjkim     UINT32                 op;
82233237Sjkim     char                   *str;
83233237Sjkim}
84233237Sjkim
85233237Sjkim/*! [Begin] no source code translation */
86233237Sjkim
87233237Sjkim%type  <value>  Expression
88233237Sjkim
89233237Sjkim%token <op>     EXPOP_EOF
90233237Sjkim%token <op>     EXPOP_NEW_LINE
91233237Sjkim%token <op>     EXPOP_NUMBER
92233237Sjkim%token <op>     EXPOP_HEX_NUMBER
93233237Sjkim%token <op>     EXPOP_RESERVED1
94233237Sjkim%token <op>     EXPOP_RESERVED2
95233237Sjkim%token <op>     EXPOP_PAREN_OPEN
96233237Sjkim%token <op>     EXPOP_PAREN_CLOSE
97233237Sjkim
98233237Sjkim%left <op>      EXPOP_LOGICAL_OR
99233237Sjkim%left <op>      EXPOP_LOGICAL_AND
100233237Sjkim%left <op>      EXPOP_OR
101233237Sjkim%left <op>      EXPOP_XOR
102233237Sjkim%left <op>      EXPOP_AND
103233237Sjkim%left <op>      EXPOP_EQUAL EXPOP_NOT_EQUAL
104233237Sjkim%left <op>      EXPOP_GREATER EXPOP_LESS EXPOP_GREATER_EQUAL EXPOP_LESS_EQUAL
105233237Sjkim%left <op>      EXPOP_SHIFT_RIGHT EXPOP_SHIFT_LEFT
106233237Sjkim%left <op>      EXPOP_ADD EXPOP_SUBTRACT
107233237Sjkim%left <op>      EXPOP_MULTIPLY EXPOP_DIVIDE EXPOP_MODULO
108233237Sjkim%right <op>     EXPOP_ONES_COMPLIMENT EXPOP_LOGICAL_NOT
109233237Sjkim
110233237Sjkim/* Tokens above must be kept in synch with dtparser.y */
111233237Sjkim
112233237Sjkim%token <op>     EXPOP_DEFINE
113233237Sjkim%token <op>     EXPOP_IDENTIFIER
114233237Sjkim
115233237Sjkim%%
116233237Sjkim
117233237Sjkim/*
118233237Sjkim *  Operator precedence rules (from K&R)
119233237Sjkim *
120233237Sjkim *  1)      ( )
121233237Sjkim *  2)      ! ~ (unary operators that are supported here)
122233237Sjkim *  3)      *   /   %
123233237Sjkim *  4)      +   -
124233237Sjkim *  5)      >>  <<
125233237Sjkim *  6)      <   >   <=  >=
126233237Sjkim *  7)      ==  !=
127233237Sjkim *  8)      &
128233237Sjkim *  9)      ^
129233237Sjkim *  10)     |
130233237Sjkim *  11)     &&
131233237Sjkim *  12)     ||
132233237Sjkim */
133233237Sjkim
134233237Sjkim/*! [End] no source code translation !*/
135233237Sjkim
136233237SjkimValue
137233237Sjkim    : Expression EXPOP_NEW_LINE                     { PrParserResult=$1; return 0; } /* End of line (newline) */
138233237Sjkim    | Expression EXPOP_EOF                          { PrParserResult=$1; return 0; } /* End of string (0) */
139233237Sjkim    ;
140233237Sjkim
141233237SjkimExpression
142233237Sjkim
143233237Sjkim      /* Unary operators */
144233237Sjkim
145233237Sjkim    : EXPOP_LOGICAL_NOT         Expression          { $$ = DtDoOperator ($2, EXPOP_LOGICAL_NOT,     $2);}
146233237Sjkim    | EXPOP_ONES_COMPLIMENT     Expression          { $$ = DtDoOperator ($2, EXPOP_ONES_COMPLIMENT, $2);}
147233237Sjkim
148233237Sjkim      /* Binary operators */
149233237Sjkim
150233237Sjkim    | Expression EXPOP_MULTIPLY         Expression  { $$ = DtDoOperator ($1, EXPOP_MULTIPLY,        $3);}
151233237Sjkim    | Expression EXPOP_DIVIDE           Expression  { $$ = DtDoOperator ($1, EXPOP_DIVIDE,          $3);}
152233237Sjkim    | Expression EXPOP_MODULO           Expression  { $$ = DtDoOperator ($1, EXPOP_MODULO,          $3);}
153233237Sjkim    | Expression EXPOP_ADD              Expression  { $$ = DtDoOperator ($1, EXPOP_ADD,             $3);}
154233237Sjkim    | Expression EXPOP_SUBTRACT         Expression  { $$ = DtDoOperator ($1, EXPOP_SUBTRACT,        $3);}
155233237Sjkim    | Expression EXPOP_SHIFT_RIGHT      Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_RIGHT,     $3);}
156233237Sjkim    | Expression EXPOP_SHIFT_LEFT       Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_LEFT,      $3);}
157233237Sjkim    | Expression EXPOP_GREATER          Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER,         $3);}
158233237Sjkim    | Expression EXPOP_LESS             Expression  { $$ = DtDoOperator ($1, EXPOP_LESS,            $3);}
159233237Sjkim    | Expression EXPOP_GREATER_EQUAL    Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER_EQUAL,   $3);}
160233237Sjkim    | Expression EXPOP_LESS_EQUAL       Expression  { $$ = DtDoOperator ($1, EXPOP_LESS_EQUAL,      $3);}
161233237Sjkim    | Expression EXPOP_EQUAL            Expression  { $$ = DtDoOperator ($1, EXPOP_EQUAL,           $3);}
162233237Sjkim    | Expression EXPOP_NOT_EQUAL        Expression  { $$ = DtDoOperator ($1, EXPOP_NOT_EQUAL,       $3);}
163233237Sjkim    | Expression EXPOP_AND              Expression  { $$ = DtDoOperator ($1, EXPOP_AND,             $3);}
164233237Sjkim    | Expression EXPOP_XOR              Expression  { $$ = DtDoOperator ($1, EXPOP_XOR,             $3);}
165233237Sjkim    | Expression EXPOP_OR               Expression  { $$ = DtDoOperator ($1, EXPOP_OR,              $3);}
166233237Sjkim    | Expression EXPOP_LOGICAL_AND      Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_AND,     $3);}
167233237Sjkim    | Expression EXPOP_LOGICAL_OR       Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_OR,      $3);}
168233237Sjkim
169233237Sjkim      /* Parentheses: '(' Expression ')' */
170233237Sjkim
171233237Sjkim    | EXPOP_PAREN_OPEN          Expression
172233237Sjkim        EXPOP_PAREN_CLOSE                           { $$ = $2;}
173233237Sjkim
174233237Sjkim      /* #if defined (ID) or #if defined ID */
175233237Sjkim
176233237Sjkim    | EXPOP_DEFINE EXPOP_PAREN_OPEN EXPOP_IDENTIFIER
177233237Sjkim        EXPOP_PAREN_CLOSE                           { $$ = PrIsDefined (PrParserlval.str);}
178233237Sjkim
179233237Sjkim    | EXPOP_DEFINE EXPOP_IDENTIFIER                 { $$ = PrIsDefined (PrParserlval.str);}
180233237Sjkim
181233237Sjkim    | EXPOP_IDENTIFIER                              { $$ = PrResolveDefine (PrParserlval.str);}
182233237Sjkim
183233237Sjkim      /* Default base for a non-prefixed integer is 10 */
184233237Sjkim
185306536Sjkim    | EXPOP_NUMBER                                  { AcpiUtStrtoul64 (PrParsertext, 10, ACPI_MAX64_BYTE_WIDTH, &$$);}
186233237Sjkim
187233237Sjkim      /* Standard hex number (0x1234) */
188233237Sjkim
189306536Sjkim    | EXPOP_HEX_NUMBER                              { AcpiUtStrtoul64 (PrParsertext, 16, ACPI_MAX64_BYTE_WIDTH, &$$);}
190233237Sjkim    ;
191233237Sjkim%%
192233237Sjkim
193233237Sjkim/*
194233237Sjkim * Local support functions, including parser entry point
195233237Sjkim */
196233237Sjkim#define PR_FIRST_PARSE_OPCODE   EXPOP_EOF
197233237Sjkim#define PR_YYTNAME_START        3
198233237Sjkim
199233237Sjkim
200233237Sjkim/******************************************************************************
201233237Sjkim *
202233237Sjkim * FUNCTION:    PrParsererror
203233237Sjkim *
204233237Sjkim * PARAMETERS:  Message             - Parser-generated error message
205233237Sjkim *
206233237Sjkim * RETURN:      None
207233237Sjkim *
208233237Sjkim * DESCRIPTION: Handler for parser errors
209233237Sjkim *
210233237Sjkim *****************************************************************************/
211233237Sjkim
212233237Sjkimvoid
213233237SjkimPrParsererror (
214233237Sjkim    char const              *Message)
215233237Sjkim{
216306536Sjkim
217306536Sjkim    sprintf (StringBuffer, "Preprocessor Parser : %s (near line %u)",
218306536Sjkim        Message, Gbl_CurrentLineNumber);
219233237Sjkim    DtError (ASL_ERROR, ASL_MSG_SYNTAX,
220306536Sjkim        NULL, (char *) StringBuffer);
221233237Sjkim}
222233237Sjkim
223233237Sjkim
224233237Sjkim/******************************************************************************
225233237Sjkim *
226233237Sjkim * FUNCTION:    PrGetOpName
227233237Sjkim *
228233237Sjkim * PARAMETERS:  ParseOpcode         - Parser token (EXPOP_*)
229233237Sjkim *
230233237Sjkim * RETURN:      Pointer to the opcode name
231233237Sjkim *
232233237Sjkim * DESCRIPTION: Get the ascii name of the parse opcode for debug output
233233237Sjkim *
234233237Sjkim *****************************************************************************/
235233237Sjkim
236233237Sjkimchar *
237233237SjkimPrGetOpName (
238233237Sjkim    UINT32                  ParseOpcode)
239233237Sjkim{
240233237Sjkim#ifdef ASL_YYTNAME_START
241233237Sjkim    /*
242233237Sjkim     * First entries (PR_YYTNAME_START) in yytname are special reserved names.
243233237Sjkim     * Ignore first 6 characters of name (EXPOP_)
244233237Sjkim     */
245233237Sjkim    return ((char *) yytname
246233237Sjkim        [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
247233237Sjkim#else
248233237Sjkim    return ("[Unknown parser generator]");
249233237Sjkim#endif
250233237Sjkim}
251233237Sjkim
252233237Sjkim
253233237Sjkim/******************************************************************************
254233237Sjkim *
255233237Sjkim * FUNCTION:    PrEvaluateExpression
256233237Sjkim *
257233237Sjkim * PARAMETERS:  ExprString          - Expression to be evaluated. Must be
258233237Sjkim *                                    terminated by either a newline or a NUL
259233237Sjkim *                                    string terminator
260233237Sjkim *
261233237Sjkim * RETURN:      64-bit value for the expression
262233237Sjkim *
263233237Sjkim * DESCRIPTION: Main entry point for the DT expression parser
264233237Sjkim *
265233237Sjkim *****************************************************************************/
266233237Sjkim
267233237SjkimUINT64
268233237SjkimPrEvaluateExpression (
269233237Sjkim    char                    *ExprString)
270233237Sjkim{
271233237Sjkim
272233237Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
273233237Sjkim        "**** Input expression: %s\n", ExprString);
274233237Sjkim
275233237Sjkim    /* Point lexer to the input string */
276233237Sjkim
277233237Sjkim    if (PrInitLexer (ExprString))
278233237Sjkim    {
279233237Sjkim        DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
280233237Sjkim            NULL, "Could not initialize lexer");
281233237Sjkim        return (0);
282233237Sjkim    }
283233237Sjkim
284233237Sjkim    /* Parse/Evaluate the input string (value returned in PrParserResult) */
285233237Sjkim
286233237Sjkim    PrParserparse ();
287233237Sjkim    PrTerminateLexer ();
288233237Sjkim
289233237Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
290233237Sjkim        "**** Parser returned value: %u (%8.8X%8.8X)\n",
291233237Sjkim        (UINT32) PrParserResult, ACPI_FORMAT_UINT64 (PrParserResult));
292233237Sjkim
293233237Sjkim    return (PrParserResult);
294233237Sjkim}
295