dtparser.y revision 1.1.1.15 1 1.1 jruoho %{
2 1.1 jruoho /******************************************************************************
3 1.1 jruoho *
4 1.1 jruoho * Module Name: dtparser.y - Bison input file for table compiler parser
5 1.1 jruoho *
6 1.1 jruoho *****************************************************************************/
7 1.1 jruoho
8 1.1 jruoho /*
9 1.1.1.15 christos * Copyright (C) 2000 - 2020, Intel Corp.
10 1.1 jruoho * All rights reserved.
11 1.1 jruoho *
12 1.1 jruoho * Redistribution and use in source and binary forms, with or without
13 1.1 jruoho * modification, are permitted provided that the following conditions
14 1.1 jruoho * are met:
15 1.1 jruoho * 1. Redistributions of source code must retain the above copyright
16 1.1 jruoho * notice, this list of conditions, and the following disclaimer,
17 1.1 jruoho * without modification.
18 1.1 jruoho * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 1.1 jruoho * substantially similar to the "NO WARRANTY" disclaimer below
20 1.1 jruoho * ("Disclaimer") and any redistribution must be conditioned upon
21 1.1 jruoho * including a substantially similar Disclaimer requirement for further
22 1.1 jruoho * binary redistribution.
23 1.1 jruoho * 3. Neither the names of the above-listed copyright holders nor the names
24 1.1 jruoho * of any contributors may be used to endorse or promote products derived
25 1.1 jruoho * from this software without specific prior written permission.
26 1.1 jruoho *
27 1.1 jruoho * Alternatively, this software may be distributed under the terms of the
28 1.1 jruoho * GNU General Public License ("GPL") version 2 as published by the Free
29 1.1 jruoho * Software Foundation.
30 1.1 jruoho *
31 1.1 jruoho * NO WARRANTY
32 1.1 jruoho * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 1.1 jruoho * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 1.1 jruoho * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 1.1 jruoho * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 1.1 jruoho * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 jruoho * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 jruoho * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 jruoho * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 1.1 jruoho * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 1.1 jruoho * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 1.1 jruoho * POSSIBILITY OF SUCH DAMAGES.
43 1.1 jruoho */
44 1.1 jruoho
45 1.1 jruoho #include "aslcompiler.h"
46 1.1 jruoho
47 1.1 jruoho #define _COMPONENT DT_COMPILER
48 1.1 jruoho ACPI_MODULE_NAME ("dtparser")
49 1.1 jruoho
50 1.1.1.5 christos void * AslLocalAllocate (unsigned int Size);
51 1.1.1.5 christos
52 1.1.1.5 christos /* Bison/yacc configuration */
53 1.1.1.5 christos
54 1.1.1.5 christos #undef alloca
55 1.1.1.5 christos #define alloca AslLocalAllocate
56 1.1.1.5 christos
57 1.1 jruoho int DtParserlex (void);
58 1.1 jruoho int DtParserparse (void);
59 1.1 jruoho void DtParsererror (char const *msg);
60 1.1 jruoho extern char *DtParsertext;
61 1.1.1.13 christos extern DT_FIELD *AslGbl_CurrentField;
62 1.1 jruoho
63 1.1 jruoho UINT64 DtParserResult; /* Expression return value */
64 1.1 jruoho
65 1.1 jruoho /* Bison/yacc configuration */
66 1.1 jruoho
67 1.1 jruoho #define yytname DtParsername
68 1.1 jruoho #define YYDEBUG 1 /* Enable debug output */
69 1.1 jruoho #define YYERROR_VERBOSE 1 /* Verbose error messages */
70 1.1 jruoho #define YYFLAG -32768
71 1.1 jruoho
72 1.1.1.2 christos /* Define YYMALLOC/YYFREE to prevent redefinition errors */
73 1.1.1.2 christos
74 1.1.1.2 christos #define YYMALLOC malloc
75 1.1.1.2 christos #define YYFREE free
76 1.1 jruoho %}
77 1.1 jruoho
78 1.1 jruoho %union
79 1.1 jruoho {
80 1.1 jruoho UINT64 value;
81 1.1 jruoho UINT32 op;
82 1.1 jruoho }
83 1.1 jruoho
84 1.1 jruoho /*! [Begin] no source code translation */
85 1.1 jruoho
86 1.1 jruoho %type <value> Expression
87 1.1 jruoho
88 1.1.1.12 christos %token <op> OP_EXP_EOF
89 1.1.1.12 christos %token <op> OP_EXP_NEW_LINE
90 1.1.1.12 christos %token <op> OP_EXP_NUMBER
91 1.1.1.12 christos %token <op> OP_EXP_HEX_NUMBER
92 1.1.1.12 christos %token <op> OP_EXP_DECIMAL_NUMBER
93 1.1.1.12 christos %token <op> OP_EXP_LABEL
94 1.1.1.12 christos %token <op> OP_EXP_PAREN_OPEN
95 1.1.1.12 christos %token <op> OP_EXP_PAREN_CLOSE
96 1.1.1.12 christos
97 1.1.1.12 christos %left <op> OP_EXP_LOGICAL_OR
98 1.1.1.12 christos %left <op> OP_EXP_LOGICAL_AND
99 1.1.1.12 christos %left <op> OP_EXP_OR
100 1.1.1.12 christos %left <op> OP_EXP_XOR
101 1.1.1.12 christos %left <op> OP_EXP_AND
102 1.1.1.12 christos %left <op> OP_EXP_EQUAL OP_EXP_NOT_EQUAL
103 1.1.1.12 christos %left <op> OP_EXP_GREATER OP_EXP_LESS OP_EXP_GREATER_EQUAL OP_EXP_LESS_EQUAL
104 1.1.1.12 christos %left <op> OP_EXP_SHIFT_RIGHT OP_EXP_SHIFT_LEFT
105 1.1.1.12 christos %left <op> OP_EXP_ADD OP_EXP_SUBTRACT
106 1.1.1.12 christos %left <op> OP_EXP_MULTIPLY OP_EXP_DIVIDE OP_EXP_MODULO
107 1.1.1.12 christos %right <op> OP_EXP_ONES_COMPLIMENT OP_EXP_LOGICAL_NOT
108 1.1 jruoho
109 1.1 jruoho %%
110 1.1 jruoho
111 1.1 jruoho /*
112 1.1 jruoho * Operator precedence rules (from K&R)
113 1.1 jruoho *
114 1.1 jruoho * 1) ( )
115 1.1 jruoho * 2) ! ~ (unary operators that are supported here)
116 1.1 jruoho * 3) * / %
117 1.1 jruoho * 4) + -
118 1.1 jruoho * 5) >> <<
119 1.1 jruoho * 6) < > <= >=
120 1.1 jruoho * 7) == !=
121 1.1 jruoho * 8) &
122 1.1 jruoho * 9) ^
123 1.1 jruoho * 10) |
124 1.1 jruoho * 11) &&
125 1.1 jruoho * 12) ||
126 1.1 jruoho */
127 1.1 jruoho Value
128 1.1.1.12 christos : Expression OP_EXP_NEW_LINE { DtParserResult=$1; return 0; } /* End of line (newline) */
129 1.1.1.12 christos | Expression OP_EXP_EOF { DtParserResult=$1; return 0; } /* End of string (0) */
130 1.1 jruoho ;
131 1.1 jruoho
132 1.1 jruoho Expression
133 1.1 jruoho
134 1.1 jruoho /* Unary operators */
135 1.1 jruoho
136 1.1.1.12 christos : OP_EXP_LOGICAL_NOT Expression { $$ = DtDoOperator ($2, OP_EXP_LOGICAL_NOT, $2);}
137 1.1.1.12 christos | OP_EXP_ONES_COMPLIMENT Expression { $$ = DtDoOperator ($2, OP_EXP_ONES_COMPLIMENT, $2);}
138 1.1 jruoho
139 1.1 jruoho /* Binary operators */
140 1.1 jruoho
141 1.1.1.12 christos | Expression OP_EXP_MULTIPLY Expression { $$ = DtDoOperator ($1, OP_EXP_MULTIPLY, $3);}
142 1.1.1.12 christos | Expression OP_EXP_DIVIDE Expression { $$ = DtDoOperator ($1, OP_EXP_DIVIDE, $3);}
143 1.1.1.12 christos | Expression OP_EXP_MODULO Expression { $$ = DtDoOperator ($1, OP_EXP_MODULO, $3);}
144 1.1.1.12 christos | Expression OP_EXP_ADD Expression { $$ = DtDoOperator ($1, OP_EXP_ADD, $3);}
145 1.1.1.12 christos | Expression OP_EXP_SUBTRACT Expression { $$ = DtDoOperator ($1, OP_EXP_SUBTRACT, $3);}
146 1.1.1.12 christos | Expression OP_EXP_SHIFT_RIGHT Expression { $$ = DtDoOperator ($1, OP_EXP_SHIFT_RIGHT, $3);}
147 1.1.1.12 christos | Expression OP_EXP_SHIFT_LEFT Expression { $$ = DtDoOperator ($1, OP_EXP_SHIFT_LEFT, $3);}
148 1.1.1.12 christos | Expression OP_EXP_GREATER Expression { $$ = DtDoOperator ($1, OP_EXP_GREATER, $3);}
149 1.1.1.12 christos | Expression OP_EXP_LESS Expression { $$ = DtDoOperator ($1, OP_EXP_LESS, $3);}
150 1.1.1.12 christos | Expression OP_EXP_GREATER_EQUAL Expression { $$ = DtDoOperator ($1, OP_EXP_GREATER_EQUAL, $3);}
151 1.1.1.12 christos | Expression OP_EXP_LESS_EQUAL Expression { $$ = DtDoOperator ($1, OP_EXP_LESS_EQUAL, $3);}
152 1.1.1.12 christos | Expression OP_EXP_EQUAL Expression { $$ = DtDoOperator ($1, OP_EXP_EQUAL, $3);}
153 1.1.1.12 christos | Expression OP_EXP_NOT_EQUAL Expression { $$ = DtDoOperator ($1, OP_EXP_NOT_EQUAL, $3);}
154 1.1.1.12 christos | Expression OP_EXP_AND Expression { $$ = DtDoOperator ($1, OP_EXP_AND, $3);}
155 1.1.1.12 christos | Expression OP_EXP_XOR Expression { $$ = DtDoOperator ($1, OP_EXP_XOR, $3);}
156 1.1.1.12 christos | Expression OP_EXP_OR Expression { $$ = DtDoOperator ($1, OP_EXP_OR, $3);}
157 1.1.1.12 christos | Expression OP_EXP_LOGICAL_AND Expression { $$ = DtDoOperator ($1, OP_EXP_LOGICAL_AND, $3);}
158 1.1.1.12 christos | Expression OP_EXP_LOGICAL_OR Expression { $$ = DtDoOperator ($1, OP_EXP_LOGICAL_OR, $3);}
159 1.1 jruoho
160 1.1 jruoho /* Parentheses: '(' Expression ')' */
161 1.1 jruoho
162 1.1.1.12 christos | OP_EXP_PAREN_OPEN Expression
163 1.1.1.12 christos OP_EXP_PAREN_CLOSE { $$ = $2;}
164 1.1 jruoho
165 1.1 jruoho /* Label references (prefixed with $) */
166 1.1 jruoho
167 1.1.1.12 christos | OP_EXP_LABEL { $$ = DtResolveLabel (DtParsertext);}
168 1.1 jruoho
169 1.1.1.10 christos /*
170 1.1.1.10 christos * All constants for the data table compiler are in hex, whether a (optional) 0x
171 1.1.1.10 christos * prefix is present or not. For example, these two input strings are equivalent:
172 1.1.1.10 christos * 1234
173 1.1.1.10 christos * 0x1234
174 1.1.1.10 christos */
175 1.1 jruoho
176 1.1.1.10 christos /* Non-prefixed hex number */
177 1.1.1.10 christos
178 1.1.1.12 christos | OP_EXP_NUMBER { $$ = DtDoConstant (DtParsertext);}
179 1.1 jruoho
180 1.1 jruoho /* Standard hex number (0x1234) */
181 1.1 jruoho
182 1.1.1.12 christos | OP_EXP_HEX_NUMBER { $$ = DtDoConstant (DtParsertext);}
183 1.1 jruoho
184 1.1.1.10 christos /* Possible TBD: Decimal number with prefix (0d1234) - Not supported this time */
185 1.1 jruoho
186 1.1.1.12 christos | OP_EXP_DECIMAL_NUMBER { $$ = DtDoConstant (DtParsertext);}
187 1.1 jruoho ;
188 1.1 jruoho %%
189 1.1 jruoho
190 1.1 jruoho /*! [End] no source code translation !*/
191 1.1 jruoho
192 1.1 jruoho /*
193 1.1 jruoho * Local support functions, including parser entry point
194 1.1 jruoho */
195 1.1.1.12 christos #define PR_FIRST_PARSE_OPCODE OP_EXP_EOF
196 1.1 jruoho #define PR_YYTNAME_START 3
197 1.1 jruoho
198 1.1 jruoho
199 1.1 jruoho /******************************************************************************
200 1.1 jruoho *
201 1.1 jruoho * FUNCTION: DtParsererror
202 1.1 jruoho *
203 1.1 jruoho * PARAMETERS: Message - Parser-generated error message
204 1.1 jruoho *
205 1.1 jruoho * RETURN: None
206 1.1 jruoho *
207 1.1 jruoho * DESCRIPTION: Handler for parser errors
208 1.1 jruoho *
209 1.1 jruoho *****************************************************************************/
210 1.1 jruoho
211 1.1 jruoho void
212 1.1 jruoho DtParsererror (
213 1.1 jruoho char const *Message)
214 1.1 jruoho {
215 1.1 jruoho DtError (ASL_ERROR, ASL_MSG_SYNTAX,
216 1.1.1.13 christos AslGbl_CurrentField, (char *) Message);
217 1.1 jruoho }
218 1.1 jruoho
219 1.1 jruoho
220 1.1 jruoho /******************************************************************************
221 1.1 jruoho *
222 1.1 jruoho * FUNCTION: DtGetOpName
223 1.1 jruoho *
224 1.1.1.12 christos * PARAMETERS: ParseOpcode - Parser token (OP_EXP_*)
225 1.1 jruoho *
226 1.1 jruoho * RETURN: Pointer to the opcode name
227 1.1 jruoho *
228 1.1 jruoho * DESCRIPTION: Get the ascii name of the parse opcode for debug output
229 1.1 jruoho *
230 1.1 jruoho *****************************************************************************/
231 1.1 jruoho
232 1.1 jruoho char *
233 1.1 jruoho DtGetOpName (
234 1.1 jruoho UINT32 ParseOpcode)
235 1.1 jruoho {
236 1.1 jruoho #ifdef ASL_YYTNAME_START
237 1.1 jruoho /*
238 1.1 jruoho * First entries (PR_YYTNAME_START) in yytname are special reserved names.
239 1.1.1.12 christos * Ignore first 6 characters of name (OP_EXP_)
240 1.1 jruoho */
241 1.1 jruoho return ((char *) yytname
242 1.1 jruoho [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
243 1.1 jruoho #else
244 1.1 jruoho return ("[Unknown parser generator]");
245 1.1 jruoho #endif
246 1.1 jruoho }
247 1.1 jruoho
248 1.1 jruoho
249 1.1 jruoho /******************************************************************************
250 1.1 jruoho *
251 1.1 jruoho * FUNCTION: DtEvaluateExpression
252 1.1 jruoho *
253 1.1 jruoho * PARAMETERS: ExprString - Expression to be evaluated. Must be
254 1.1 jruoho * terminated by either a newline or a NUL
255 1.1 jruoho * string terminator
256 1.1 jruoho *
257 1.1 jruoho * RETURN: 64-bit value for the expression
258 1.1 jruoho *
259 1.1 jruoho * DESCRIPTION: Main entry point for the DT expression parser
260 1.1 jruoho *
261 1.1 jruoho *****************************************************************************/
262 1.1 jruoho
263 1.1 jruoho UINT64
264 1.1 jruoho DtEvaluateExpression (
265 1.1 jruoho char *ExprString)
266 1.1 jruoho {
267 1.1 jruoho
268 1.1 jruoho DbgPrint (ASL_DEBUG_OUTPUT,
269 1.1 jruoho "**** Input expression: %s (Base 16)\n", ExprString);
270 1.1 jruoho
271 1.1 jruoho /* Point lexer to the input string */
272 1.1 jruoho
273 1.1 jruoho if (DtInitLexer (ExprString))
274 1.1 jruoho {
275 1.1 jruoho DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
276 1.1.1.13 christos AslGbl_CurrentField, "Could not initialize lexer");
277 1.1 jruoho return (0);
278 1.1 jruoho }
279 1.1 jruoho
280 1.1 jruoho /* Parse/Evaluate the input string (value returned in DtParserResult) */
281 1.1 jruoho
282 1.1 jruoho DtParserparse ();
283 1.1 jruoho DtTerminateLexer ();
284 1.1 jruoho
285 1.1 jruoho DbgPrint (ASL_DEBUG_OUTPUT,
286 1.1 jruoho "**** Parser returned value: %u (%8.8X%8.8X)\n",
287 1.1 jruoho (UINT32) DtParserResult, ACPI_FORMAT_UINT64 (DtParserResult));
288 1.1 jruoho
289 1.1 jruoho return (DtParserResult);
290 1.1 jruoho }
291