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