1220604Sjkim%{
2220604Sjkim/******************************************************************************
3220604Sjkim *
4220604Sjkim * Module Name: dtparser.y - Bison input file for table compiler parser
5220604Sjkim *
6220604Sjkim *****************************************************************************/
7220604Sjkim
8220604Sjkim/*
9281075Sdim * Copyright (C) 2000 - 2015, Intel Corp.
10220604Sjkim * All rights reserved.
11220604Sjkim *
12220604Sjkim * Redistribution and use in source and binary forms, with or without
13220604Sjkim * modification, are permitted provided that the following conditions
14220604Sjkim * are met:
15220604Sjkim * 1. Redistributions of source code must retain the above copyright
16220604Sjkim *    notice, this list of conditions, and the following disclaimer,
17220604Sjkim *    without modification.
18220604Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19220604Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
20220604Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
21220604Sjkim *    including a substantially similar Disclaimer requirement for further
22220604Sjkim *    binary redistribution.
23220604Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
24220604Sjkim *    of any contributors may be used to endorse or promote products derived
25220604Sjkim *    from this software without specific prior written permission.
26220604Sjkim *
27220604Sjkim * Alternatively, this software may be distributed under the terms of the
28220604Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
29220604Sjkim * Software Foundation.
30220604Sjkim *
31220604Sjkim * NO WARRANTY
32220604Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33220604Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34220604Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35220604Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36220604Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37220604Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38220604Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39220604Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40220604Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41220604Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42220604Sjkim * POSSIBILITY OF SUCH DAMAGES.
43220604Sjkim */
44220604Sjkim
45220663Sjkim#include <contrib/dev/acpica/compiler/aslcompiler.h>
46220663Sjkim#include <contrib/dev/acpica/compiler/dtcompiler.h>
47220604Sjkim
48220681Sjkim#define _COMPONENT          DT_COMPILER
49220604Sjkim        ACPI_MODULE_NAME    ("dtparser")
50220604Sjkim
51220681Sjkimint                         DtParserlex (void);
52220681Sjkimint                         DtParserparse (void);
53220681Sjkimvoid                        DtParsererror (char const *msg);
54220681Sjkimextern char                 *DtParsertext;
55220681Sjkimextern DT_FIELD             *Gbl_CurrentField;
56220604Sjkim
57220681SjkimUINT64                      DtParserResult; /* Expression return value */
58220604Sjkim
59220681Sjkim/* Bison/yacc configuration */
60220681Sjkim
61220681Sjkim#define yytname             DtParsername
62220681Sjkim#define YYDEBUG             1               /* Enable debug output */
63220681Sjkim#define YYERROR_VERBOSE     1               /* Verbose error messages */
64220681Sjkim#define YYFLAG              -32768
65220681Sjkim
66233250Sjkim/* Define YYMALLOC/YYFREE to prevent redefinition errors  */
67233250Sjkim
68233250Sjkim#define YYMALLOC            malloc
69233250Sjkim#define YYFREE              free
70220604Sjkim%}
71220604Sjkim
72220604Sjkim%union
73220604Sjkim{
74220604Sjkim     UINT64                 value;
75220604Sjkim     UINT32                 op;
76220604Sjkim}
77220604Sjkim
78220681Sjkim/*! [Begin] no source code translation */
79220681Sjkim
80220604Sjkim%type  <value>  Expression
81220604Sjkim
82220604Sjkim%token <op>     EXPOP_EOF
83220604Sjkim%token <op>     EXPOP_NEW_LINE
84220604Sjkim%token <op>     EXPOP_NUMBER
85220604Sjkim%token <op>     EXPOP_HEX_NUMBER
86220604Sjkim%token <op>     EXPOP_DECIMAL_NUMBER
87220604Sjkim%token <op>     EXPOP_LABEL
88220604Sjkim%token <op>     EXPOP_PAREN_OPEN
89220604Sjkim%token <op>     EXPOP_PAREN_CLOSE
90220604Sjkim
91220604Sjkim%left <op>      EXPOP_LOGICAL_OR
92220604Sjkim%left <op>      EXPOP_LOGICAL_AND
93220604Sjkim%left <op>      EXPOP_OR
94220604Sjkim%left <op>      EXPOP_XOR
95220604Sjkim%left <op>      EXPOP_AND
96220604Sjkim%left <op>      EXPOP_EQUAL EXPOP_NOT_EQUAL
97220604Sjkim%left <op>      EXPOP_GREATER EXPOP_LESS EXPOP_GREATER_EQUAL EXPOP_LESS_EQUAL
98220604Sjkim%left <op>      EXPOP_SHIFT_RIGHT EXPOP_SHIFT_LEFT
99220604Sjkim%left <op>      EXPOP_ADD EXPOP_SUBTRACT
100220604Sjkim%left <op>      EXPOP_MULTIPLY EXPOP_DIVIDE EXPOP_MODULO
101220604Sjkim%right <op>     EXPOP_ONES_COMPLIMENT EXPOP_LOGICAL_NOT
102220604Sjkim
103220604Sjkim%%
104220604Sjkim
105220604Sjkim/*
106220604Sjkim *  Operator precedence rules (from K&R)
107220604Sjkim *
108220604Sjkim *  1)      ( )
109220604Sjkim *  2)      ! ~ (unary operators that are supported here)
110220604Sjkim *  3)      *   /   %
111220604Sjkim *  4)      +   -
112220604Sjkim *  5)      >>  <<
113220604Sjkim *  6)      <   >   <=  >=
114220604Sjkim *  7)      ==  !=
115220604Sjkim *  8)      &
116220604Sjkim *  9)      ^
117220604Sjkim *  10)     |
118220604Sjkim *  11)     &&
119220604Sjkim *  12)     ||
120220604Sjkim */
121220604SjkimValue
122220604Sjkim    : Expression EXPOP_NEW_LINE                     { DtParserResult=$1; return 0; } /* End of line (newline) */
123220604Sjkim    | Expression EXPOP_EOF                          { DtParserResult=$1; return 0; } /* End of string (0) */
124220604Sjkim    ;
125220604Sjkim
126220604SjkimExpression
127220604Sjkim
128220604Sjkim      /* Unary operators */
129220604Sjkim
130220604Sjkim    : EXPOP_LOGICAL_NOT         Expression          { $$ = DtDoOperator ($2, EXPOP_LOGICAL_NOT,     $2);}
131220604Sjkim    | EXPOP_ONES_COMPLIMENT     Expression          { $$ = DtDoOperator ($2, EXPOP_ONES_COMPLIMENT, $2);}
132220604Sjkim
133220604Sjkim      /* Binary operators */
134220604Sjkim
135220604Sjkim    | Expression EXPOP_MULTIPLY         Expression  { $$ = DtDoOperator ($1, EXPOP_MULTIPLY,        $3);}
136220604Sjkim    | Expression EXPOP_DIVIDE           Expression  { $$ = DtDoOperator ($1, EXPOP_DIVIDE,          $3);}
137220604Sjkim    | Expression EXPOP_MODULO           Expression  { $$ = DtDoOperator ($1, EXPOP_MODULO,          $3);}
138220604Sjkim    | Expression EXPOP_ADD              Expression  { $$ = DtDoOperator ($1, EXPOP_ADD,             $3);}
139220604Sjkim    | Expression EXPOP_SUBTRACT         Expression  { $$ = DtDoOperator ($1, EXPOP_SUBTRACT,        $3);}
140220604Sjkim    | Expression EXPOP_SHIFT_RIGHT      Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_RIGHT,     $3);}
141220604Sjkim    | Expression EXPOP_SHIFT_LEFT       Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_LEFT,      $3);}
142220604Sjkim    | Expression EXPOP_GREATER          Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER,         $3);}
143220604Sjkim    | Expression EXPOP_LESS             Expression  { $$ = DtDoOperator ($1, EXPOP_LESS,            $3);}
144220604Sjkim    | Expression EXPOP_GREATER_EQUAL    Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER_EQUAL,   $3);}
145220604Sjkim    | Expression EXPOP_LESS_EQUAL       Expression  { $$ = DtDoOperator ($1, EXPOP_LESS_EQUAL,      $3);}
146220604Sjkim    | Expression EXPOP_EQUAL            Expression  { $$ = DtDoOperator ($1, EXPOP_EQUAL,           $3);}
147220604Sjkim    | Expression EXPOP_NOT_EQUAL        Expression  { $$ = DtDoOperator ($1, EXPOP_NOT_EQUAL,       $3);}
148220604Sjkim    | Expression EXPOP_AND              Expression  { $$ = DtDoOperator ($1, EXPOP_AND,             $3);}
149220604Sjkim    | Expression EXPOP_XOR              Expression  { $$ = DtDoOperator ($1, EXPOP_XOR,             $3);}
150220604Sjkim    | Expression EXPOP_OR               Expression  { $$ = DtDoOperator ($1, EXPOP_OR,              $3);}
151220604Sjkim    | Expression EXPOP_LOGICAL_AND      Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_AND,     $3);}
152220604Sjkim    | Expression EXPOP_LOGICAL_OR       Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_OR,      $3);}
153220604Sjkim
154220604Sjkim      /* Parentheses: '(' Expression ')' */
155220604Sjkim
156220604Sjkim    | EXPOP_PAREN_OPEN          Expression
157220604Sjkim        EXPOP_PAREN_CLOSE                           { $$ = $2;}
158220604Sjkim
159220604Sjkim      /* Label references (prefixed with $) */
160220604Sjkim
161220604Sjkim    | EXPOP_LABEL                                   { $$ = DtResolveLabel (DtParsertext);}
162220604Sjkim
163220604Sjkim      /* Default base for a non-prefixed integer is 16 */
164220604Sjkim
165220604Sjkim    | EXPOP_NUMBER                                  { UtStrtoul64 (DtParsertext, 16, &$$);}
166220604Sjkim
167220604Sjkim      /* Standard hex number (0x1234) */
168220604Sjkim
169220604Sjkim    | EXPOP_HEX_NUMBER                              { UtStrtoul64 (DtParsertext, 16, &$$);}
170220604Sjkim
171220604Sjkim      /* TBD: Decimal number with prefix (0d1234) - Not supported by UtStrtoul64 at this time */
172220604Sjkim
173220604Sjkim    | EXPOP_DECIMAL_NUMBER                          { UtStrtoul64 (DtParsertext, 10, &$$);}
174220604Sjkim    ;
175220604Sjkim%%
176220604Sjkim
177220681Sjkim/*! [End] no source code translation !*/
178220681Sjkim
179220604Sjkim/*
180220604Sjkim * Local support functions, including parser entry point
181220604Sjkim */
182220604Sjkim#define PR_FIRST_PARSE_OPCODE   EXPOP_EOF
183220604Sjkim#define PR_YYTNAME_START        3
184220604Sjkim
185220604Sjkim
186220604Sjkim/******************************************************************************
187220604Sjkim *
188220604Sjkim * FUNCTION:    DtParsererror
189220604Sjkim *
190220604Sjkim * PARAMETERS:  Message             - Parser-generated error message
191220604Sjkim *
192220604Sjkim * RETURN:      None
193220604Sjkim *
194220604Sjkim * DESCRIPTION: Handler for parser errors
195220604Sjkim *
196220604Sjkim *****************************************************************************/
197220604Sjkim
198220604Sjkimvoid
199220604SjkimDtParsererror (
200220604Sjkim    char const              *Message)
201220604Sjkim{
202220604Sjkim    DtError (ASL_ERROR, ASL_MSG_SYNTAX,
203220604Sjkim        Gbl_CurrentField, (char *) Message);
204220604Sjkim}
205220604Sjkim
206220604Sjkim
207220604Sjkim/******************************************************************************
208220604Sjkim *
209220604Sjkim * FUNCTION:    DtGetOpName
210220604Sjkim *
211220604Sjkim * PARAMETERS:  ParseOpcode         - Parser token (EXPOP_*)
212220604Sjkim *
213220604Sjkim * RETURN:      Pointer to the opcode name
214220604Sjkim *
215220604Sjkim * DESCRIPTION: Get the ascii name of the parse opcode for debug output
216220604Sjkim *
217220604Sjkim *****************************************************************************/
218220604Sjkim
219220604Sjkimchar *
220220604SjkimDtGetOpName (
221220604Sjkim    UINT32                  ParseOpcode)
222220604Sjkim{
223220681Sjkim#ifdef ASL_YYTNAME_START
224220604Sjkim    /*
225220604Sjkim     * First entries (PR_YYTNAME_START) in yytname are special reserved names.
226220604Sjkim     * Ignore first 6 characters of name (EXPOP_)
227220604Sjkim     */
228220604Sjkim    return ((char *) yytname
229220604Sjkim        [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
230220681Sjkim#else
231220681Sjkim    return ("[Unknown parser generator]");
232220681Sjkim#endif
233220604Sjkim}
234220604Sjkim
235220604Sjkim
236220604Sjkim/******************************************************************************
237220604Sjkim *
238220604Sjkim * FUNCTION:    DtEvaluateExpression
239220604Sjkim *
240220604Sjkim * PARAMETERS:  ExprString          - Expression to be evaluated. Must be
241220604Sjkim *                                    terminated by either a newline or a NUL
242220604Sjkim *                                    string terminator
243220604Sjkim *
244220604Sjkim * RETURN:      64-bit value for the expression
245220604Sjkim *
246220604Sjkim * DESCRIPTION: Main entry point for the DT expression parser
247220604Sjkim *
248220604Sjkim *****************************************************************************/
249220604Sjkim
250220604SjkimUINT64
251220604SjkimDtEvaluateExpression (
252220604Sjkim    char                    *ExprString)
253220604Sjkim{
254220604Sjkim
255220604Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
256220604Sjkim        "**** Input expression: %s  (Base 16)\n", ExprString);
257220604Sjkim
258220604Sjkim    /* Point lexer to the input string */
259220604Sjkim
260220604Sjkim    if (DtInitLexer (ExprString))
261220604Sjkim    {
262220604Sjkim        DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
263220604Sjkim            Gbl_CurrentField, "Could not initialize lexer");
264220604Sjkim        return (0);
265220604Sjkim    }
266220604Sjkim
267220604Sjkim    /* Parse/Evaluate the input string (value returned in DtParserResult) */
268220604Sjkim
269220604Sjkim    DtParserparse ();
270220604Sjkim    DtTerminateLexer ();
271220604Sjkim
272220604Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
273220604Sjkim        "**** Parser returned value: %u (%8.8X%8.8X)\n",
274220604Sjkim        (UINT32) DtParserResult, ACPI_FORMAT_UINT64 (DtParserResult));
275220604Sjkim
276220604Sjkim    return (DtParserResult);
277220604Sjkim}
278