1251652Sgjb%{
2251652Sgjb/******************************************************************************
3251652Sgjb *
4251652Sgjb * Module Name: dtparser.y - Bison input file for table compiler parser
5251652Sgjb *
6251652Sgjb *****************************************************************************/
7251652Sgjb
8251652Sgjb/*
9251652Sgjb * Copyright (C) 2000 - 2016, Intel Corp.
10262761Sgjb * All rights reserved.
11251652Sgjb *
12251652Sgjb * Redistribution and use in source and binary forms, with or without
13264106Sgjb * modification, are permitted provided that the following conditions
14254293Sgjb * are met:
15254293Sgjb * 1. Redistributions of source code must retain the above copyright
16251652Sgjb *    notice, this list of conditions, and the following disclaimer,
17252846Sgjb *    without modification.
18252846Sgjb * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19252846Sgjb *    substantially similar to the "NO WARRANTY" disclaimer below
20262761Sgjb *    ("Disclaimer") and any redistribution must be conditioned upon
21262761Sgjb *    including a substantially similar Disclaimer requirement for further
22262761Sgjb *    binary redistribution.
23262761Sgjb * 3. Neither the names of the above-listed copyright holders nor the names
24262761Sgjb *    of any contributors may be used to endorse or promote products derived
25262761Sgjb *    from this software without specific prior written permission.
26262761Sgjb *
27251652Sgjb * Alternatively, this software may be distributed under the terms of the
28251652Sgjb * GNU General Public License ("GPL") version 2 as published by the Free
29251652Sgjb * Software Foundation.
30251652Sgjb *
31252846Sgjb * NO WARRANTY
32252846Sgjb * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33251652Sgjb * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34251652Sgjb * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35251652Sgjb * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36251652Sgjb * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37251652Sgjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38251652Sgjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39251652Sgjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40251652Sgjb * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41251652Sgjb * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42251652Sgjb * POSSIBILITY OF SUCH DAMAGES.
43262793Sgjb */
44251652Sgjb
45251652Sgjb#include <contrib/dev/acpica/compiler/aslcompiler.h>
46251652Sgjb#include <contrib/dev/acpica/compiler/dtcompiler.h>
47251652Sgjb
48259151Sgjb#define _COMPONENT          DT_COMPILER
49264245Sgjb        ACPI_MODULE_NAME    ("dtparser")
50264106Sgjb
51264106Sgjbvoid *                      AslLocalAllocate (unsigned int Size);
52264106Sgjb
53264106Sgjb/* Bison/yacc configuration */
54326534Sgjb
55326534Sgjb#undef alloca
56326534Sgjb#define alloca              AslLocalAllocate
57326534Sgjb
58326534Sgjbint                         DtParserlex (void);
59326534Sgjbint                         DtParserparse (void);
60326534Sgjbvoid                        DtParsererror (char const *msg);
61326534Sgjbextern char                 *DtParsertext;
62326534Sgjbextern DT_FIELD             *Gbl_CurrentField;
63326534Sgjb
64326534SgjbUINT64                      DtParserResult; /* Expression return value */
65326534Sgjb
66326534Sgjb/* Bison/yacc configuration */
67326534Sgjb
68264106Sgjb#define yytname             DtParsername
69264106Sgjb#define YYDEBUG             1               /* Enable debug output */
70264106Sgjb#define YYERROR_VERBOSE     1               /* Verbose error messages */
71264106Sgjb#define YYFLAG              -32768
72264106Sgjb
73264440Sgjb/* Define YYMALLOC/YYFREE to prevent redefinition errors  */
74264440Sgjb
75264440Sgjb#define YYMALLOC            malloc
76264440Sgjb#define YYFREE              free
77264440Sgjb%}
78264440Sgjb
79264440Sgjb%union
80264440Sgjb{
81264440Sgjb     UINT64                 value;
82264440Sgjb     UINT32                 op;
83264440Sgjb}
84264440Sgjb
85264106Sgjb/*! [Begin] no source code translation */
86264106Sgjb
87264106Sgjb%type  <value>  Expression
88264106Sgjb
89273080Sgjb%token <op>     EXPOP_EOF
90273080Sgjb%token <op>     EXPOP_NEW_LINE
91273080Sgjb%token <op>     EXPOP_NUMBER
92273080Sgjb%token <op>     EXPOP_HEX_NUMBER
93273080Sgjb%token <op>     EXPOP_DECIMAL_NUMBER
94273080Sgjb%token <op>     EXPOP_LABEL
95273080Sgjb%token <op>     EXPOP_PAREN_OPEN
96273080Sgjb%token <op>     EXPOP_PAREN_CLOSE
97273080Sgjb
98273080Sgjb%left <op>      EXPOP_LOGICAL_OR
99273080Sgjb%left <op>      EXPOP_LOGICAL_AND
100273080Sgjb%left <op>      EXPOP_OR
101273080Sgjb%left <op>      EXPOP_XOR
102273080Sgjb%left <op>      EXPOP_AND
103273080Sgjb%left <op>      EXPOP_EQUAL EXPOP_NOT_EQUAL
104273080Sgjb%left <op>      EXPOP_GREATER EXPOP_LESS EXPOP_GREATER_EQUAL EXPOP_LESS_EQUAL
105273080Sgjb%left <op>      EXPOP_SHIFT_RIGHT EXPOP_SHIFT_LEFT
106273080Sgjb%left <op>      EXPOP_ADD EXPOP_SUBTRACT
107273080Sgjb%left <op>      EXPOP_MULTIPLY EXPOP_DIVIDE EXPOP_MODULO
108273080Sgjb%right <op>     EXPOP_ONES_COMPLIMENT EXPOP_LOGICAL_NOT
109273080Sgjb
110278985Sgjb%%
111278985Sgjb
112278985Sgjb/*
113278985Sgjb *  Operator precedence rules (from K&R)
114278985Sgjb *
115278985Sgjb *  1)      ( )
116278985Sgjb *  2)      ! ~ (unary operators that are supported here)
117278985Sgjb *  3)      *   /   %
118 *  4)      +   -
119 *  5)      >>  <<
120 *  6)      <   >   <=  >=
121 *  7)      ==  !=
122 *  8)      &
123 *  9)      ^
124 *  10)     |
125 *  11)     &&
126 *  12)     ||
127 */
128Value
129    : Expression EXPOP_NEW_LINE                     { DtParserResult=$1; return 0; } /* End of line (newline) */
130    | Expression EXPOP_EOF                          { DtParserResult=$1; return 0; } /* End of string (0) */
131    ;
132
133Expression
134
135      /* Unary operators */
136
137    : EXPOP_LOGICAL_NOT         Expression          { $$ = DtDoOperator ($2, EXPOP_LOGICAL_NOT,     $2);}
138    | EXPOP_ONES_COMPLIMENT     Expression          { $$ = DtDoOperator ($2, EXPOP_ONES_COMPLIMENT, $2);}
139
140      /* Binary operators */
141
142    | Expression EXPOP_MULTIPLY         Expression  { $$ = DtDoOperator ($1, EXPOP_MULTIPLY,        $3);}
143    | Expression EXPOP_DIVIDE           Expression  { $$ = DtDoOperator ($1, EXPOP_DIVIDE,          $3);}
144    | Expression EXPOP_MODULO           Expression  { $$ = DtDoOperator ($1, EXPOP_MODULO,          $3);}
145    | Expression EXPOP_ADD              Expression  { $$ = DtDoOperator ($1, EXPOP_ADD,             $3);}
146    | Expression EXPOP_SUBTRACT         Expression  { $$ = DtDoOperator ($1, EXPOP_SUBTRACT,        $3);}
147    | Expression EXPOP_SHIFT_RIGHT      Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_RIGHT,     $3);}
148    | Expression EXPOP_SHIFT_LEFT       Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_LEFT,      $3);}
149    | Expression EXPOP_GREATER          Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER,         $3);}
150    | Expression EXPOP_LESS             Expression  { $$ = DtDoOperator ($1, EXPOP_LESS,            $3);}
151    | Expression EXPOP_GREATER_EQUAL    Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER_EQUAL,   $3);}
152    | Expression EXPOP_LESS_EQUAL       Expression  { $$ = DtDoOperator ($1, EXPOP_LESS_EQUAL,      $3);}
153    | Expression EXPOP_EQUAL            Expression  { $$ = DtDoOperator ($1, EXPOP_EQUAL,           $3);}
154    | Expression EXPOP_NOT_EQUAL        Expression  { $$ = DtDoOperator ($1, EXPOP_NOT_EQUAL,       $3);}
155    | Expression EXPOP_AND              Expression  { $$ = DtDoOperator ($1, EXPOP_AND,             $3);}
156    | Expression EXPOP_XOR              Expression  { $$ = DtDoOperator ($1, EXPOP_XOR,             $3);}
157    | Expression EXPOP_OR               Expression  { $$ = DtDoOperator ($1, EXPOP_OR,              $3);}
158    | Expression EXPOP_LOGICAL_AND      Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_AND,     $3);}
159    | Expression EXPOP_LOGICAL_OR       Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_OR,      $3);}
160
161      /* Parentheses: '(' Expression ')' */
162
163    | EXPOP_PAREN_OPEN          Expression
164        EXPOP_PAREN_CLOSE                           { $$ = $2;}
165
166      /* Label references (prefixed with $) */
167
168    | EXPOP_LABEL                                   { $$ = DtResolveLabel (DtParsertext);}
169
170      /* Default base for a non-prefixed integer is 16 */
171
172    | EXPOP_NUMBER                                  { AcpiUtStrtoul64 (DtParsertext, 16, ACPI_MAX64_BYTE_WIDTH, &$$);}
173
174      /* Standard hex number (0x1234) */
175
176    | EXPOP_HEX_NUMBER                              { AcpiUtStrtoul64 (DtParsertext, 16, ACPI_MAX64_BYTE_WIDTH, &$$);}
177
178      /* TBD: Decimal number with prefix (0d1234) - Not supported by strtoul64 at this time */
179
180    | EXPOP_DECIMAL_NUMBER                          { AcpiUtStrtoul64 (DtParsertext, 10, ACPI_MAX64_BYTE_WIDTH, &$$);}
181    ;
182%%
183
184/*! [End] no source code translation !*/
185
186/*
187 * Local support functions, including parser entry point
188 */
189#define PR_FIRST_PARSE_OPCODE   EXPOP_EOF
190#define PR_YYTNAME_START        3
191
192
193/******************************************************************************
194 *
195 * FUNCTION:    DtParsererror
196 *
197 * PARAMETERS:  Message             - Parser-generated error message
198 *
199 * RETURN:      None
200 *
201 * DESCRIPTION: Handler for parser errors
202 *
203 *****************************************************************************/
204
205void
206DtParsererror (
207    char const              *Message)
208{
209    DtError (ASL_ERROR, ASL_MSG_SYNTAX,
210        Gbl_CurrentField, (char *) Message);
211}
212
213
214/******************************************************************************
215 *
216 * FUNCTION:    DtGetOpName
217 *
218 * PARAMETERS:  ParseOpcode         - Parser token (EXPOP_*)
219 *
220 * RETURN:      Pointer to the opcode name
221 *
222 * DESCRIPTION: Get the ascii name of the parse opcode for debug output
223 *
224 *****************************************************************************/
225
226char *
227DtGetOpName (
228    UINT32                  ParseOpcode)
229{
230#ifdef ASL_YYTNAME_START
231    /*
232     * First entries (PR_YYTNAME_START) in yytname are special reserved names.
233     * Ignore first 6 characters of name (EXPOP_)
234     */
235    return ((char *) yytname
236        [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
237#else
238    return ("[Unknown parser generator]");
239#endif
240}
241
242
243/******************************************************************************
244 *
245 * FUNCTION:    DtEvaluateExpression
246 *
247 * PARAMETERS:  ExprString          - Expression to be evaluated. Must be
248 *                                    terminated by either a newline or a NUL
249 *                                    string terminator
250 *
251 * RETURN:      64-bit value for the expression
252 *
253 * DESCRIPTION: Main entry point for the DT expression parser
254 *
255 *****************************************************************************/
256
257UINT64
258DtEvaluateExpression (
259    char                    *ExprString)
260{
261
262    DbgPrint (ASL_DEBUG_OUTPUT,
263        "**** Input expression: %s  (Base 16)\n", ExprString);
264
265    /* Point lexer to the input string */
266
267    if (DtInitLexer (ExprString))
268    {
269        DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
270            Gbl_CurrentField, "Could not initialize lexer");
271        return (0);
272    }
273
274    /* Parse/Evaluate the input string (value returned in DtParserResult) */
275
276    DtParserparse ();
277    DtTerminateLexer ();
278
279    DbgPrint (ASL_DEBUG_OUTPUT,
280        "**** Parser returned value: %u (%8.8X%8.8X)\n",
281        (UINT32) DtParserResult, ACPI_FORMAT_UINT64 (DtParserResult));
282
283    return (DtParserResult);
284}
285