prparser.y revision 1.1.1.11 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.1.1.11 christos * Copyright (C) 2000 - 2018, 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
47 1.1 christos #define _COMPONENT ASL_PREPROCESSOR
48 1.1 christos ACPI_MODULE_NAME ("prparser")
49 1.1 christos
50 1.1.1.4 christos void * AslLocalAllocate (unsigned int Size);
51 1.1.1.4 christos
52 1.1.1.4 christos /* Bison/yacc configuration */
53 1.1.1.4 christos
54 1.1.1.4 christos #undef alloca
55 1.1.1.4 christos #define alloca AslLocalAllocate
56 1.1.1.4 christos
57 1.1 christos int PrParserlex (void);
58 1.1 christos int PrParserparse (void);
59 1.1 christos void PrParsererror (char const *msg);
60 1.1 christos extern char *PrParsertext;
61 1.1 christos
62 1.1 christos UINT64 PrParserResult; /* Expression return value */
63 1.1 christos
64 1.1 christos /* Bison/yacc configuration */
65 1.1 christos
66 1.1 christos #define yytname PrParsername
67 1.1 christos #define YYDEBUG 1 /* Enable debug output */
68 1.1 christos #define YYERROR_VERBOSE 1 /* Verbose error messages */
69 1.1 christos #define YYFLAG -32768
70 1.1 christos
71 1.1 christos /* Define YYMALLOC/YYFREE to prevent redefinition errors */
72 1.1 christos
73 1.1 christos #define YYMALLOC malloc
74 1.1 christos #define YYFREE free
75 1.1 christos %}
76 1.1 christos
77 1.1 christos %union
78 1.1 christos {
79 1.1 christos UINT64 value;
80 1.1 christos UINT32 op;
81 1.1 christos char *str;
82 1.1 christos }
83 1.1 christos
84 1.1 christos /*! [Begin] no source code translation */
85 1.1 christos
86 1.1 christos %type <value> Expression
87 1.1 christos
88 1.1 christos %token <op> EXPOP_EOF
89 1.1 christos %token <op> EXPOP_NEW_LINE
90 1.1 christos %token <op> EXPOP_NUMBER
91 1.1 christos %token <op> EXPOP_HEX_NUMBER
92 1.1 christos %token <op> EXPOP_RESERVED1
93 1.1 christos %token <op> EXPOP_RESERVED2
94 1.1 christos %token <op> EXPOP_PAREN_OPEN
95 1.1 christos %token <op> EXPOP_PAREN_CLOSE
96 1.1 christos
97 1.1 christos %left <op> EXPOP_LOGICAL_OR
98 1.1 christos %left <op> EXPOP_LOGICAL_AND
99 1.1 christos %left <op> EXPOP_OR
100 1.1 christos %left <op> EXPOP_XOR
101 1.1 christos %left <op> EXPOP_AND
102 1.1 christos %left <op> EXPOP_EQUAL EXPOP_NOT_EQUAL
103 1.1 christos %left <op> EXPOP_GREATER EXPOP_LESS EXPOP_GREATER_EQUAL EXPOP_LESS_EQUAL
104 1.1 christos %left <op> EXPOP_SHIFT_RIGHT EXPOP_SHIFT_LEFT
105 1.1 christos %left <op> EXPOP_ADD EXPOP_SUBTRACT
106 1.1 christos %left <op> EXPOP_MULTIPLY EXPOP_DIVIDE EXPOP_MODULO
107 1.1 christos %right <op> EXPOP_ONES_COMPLIMENT EXPOP_LOGICAL_NOT
108 1.1 christos
109 1.1 christos /* Tokens above must be kept in synch with dtparser.y */
110 1.1 christos
111 1.1 christos %token <op> EXPOP_DEFINE
112 1.1 christos %token <op> EXPOP_IDENTIFIER
113 1.1 christos
114 1.1 christos %%
115 1.1 christos
116 1.1 christos /*
117 1.1 christos * Operator precedence rules (from K&R)
118 1.1 christos *
119 1.1 christos * 1) ( )
120 1.1 christos * 2) ! ~ (unary operators that are supported here)
121 1.1 christos * 3) * / %
122 1.1 christos * 4) + -
123 1.1 christos * 5) >> <<
124 1.1 christos * 6) < > <= >=
125 1.1 christos * 7) == !=
126 1.1 christos * 8) &
127 1.1 christos * 9) ^
128 1.1 christos * 10) |
129 1.1 christos * 11) &&
130 1.1 christos * 12) ||
131 1.1 christos */
132 1.1 christos
133 1.1 christos /*! [End] no source code translation !*/
134 1.1 christos
135 1.1 christos Value
136 1.1 christos : Expression EXPOP_NEW_LINE { PrParserResult=$1; return 0; } /* End of line (newline) */
137 1.1 christos | Expression EXPOP_EOF { PrParserResult=$1; return 0; } /* End of string (0) */
138 1.1 christos ;
139 1.1 christos
140 1.1 christos Expression
141 1.1 christos
142 1.1 christos /* Unary operators */
143 1.1 christos
144 1.1 christos : EXPOP_LOGICAL_NOT Expression { $$ = DtDoOperator ($2, EXPOP_LOGICAL_NOT, $2);}
145 1.1 christos | EXPOP_ONES_COMPLIMENT Expression { $$ = DtDoOperator ($2, EXPOP_ONES_COMPLIMENT, $2);}
146 1.1 christos
147 1.1 christos /* Binary operators */
148 1.1 christos
149 1.1 christos | Expression EXPOP_MULTIPLY Expression { $$ = DtDoOperator ($1, EXPOP_MULTIPLY, $3);}
150 1.1 christos | Expression EXPOP_DIVIDE Expression { $$ = DtDoOperator ($1, EXPOP_DIVIDE, $3);}
151 1.1 christos | Expression EXPOP_MODULO Expression { $$ = DtDoOperator ($1, EXPOP_MODULO, $3);}
152 1.1 christos | Expression EXPOP_ADD Expression { $$ = DtDoOperator ($1, EXPOP_ADD, $3);}
153 1.1 christos | Expression EXPOP_SUBTRACT Expression { $$ = DtDoOperator ($1, EXPOP_SUBTRACT, $3);}
154 1.1 christos | Expression EXPOP_SHIFT_RIGHT Expression { $$ = DtDoOperator ($1, EXPOP_SHIFT_RIGHT, $3);}
155 1.1 christos | Expression EXPOP_SHIFT_LEFT Expression { $$ = DtDoOperator ($1, EXPOP_SHIFT_LEFT, $3);}
156 1.1 christos | Expression EXPOP_GREATER Expression { $$ = DtDoOperator ($1, EXPOP_GREATER, $3);}
157 1.1 christos | Expression EXPOP_LESS Expression { $$ = DtDoOperator ($1, EXPOP_LESS, $3);}
158 1.1 christos | Expression EXPOP_GREATER_EQUAL Expression { $$ = DtDoOperator ($1, EXPOP_GREATER_EQUAL, $3);}
159 1.1 christos | Expression EXPOP_LESS_EQUAL Expression { $$ = DtDoOperator ($1, EXPOP_LESS_EQUAL, $3);}
160 1.1 christos | Expression EXPOP_EQUAL Expression { $$ = DtDoOperator ($1, EXPOP_EQUAL, $3);}
161 1.1 christos | Expression EXPOP_NOT_EQUAL Expression { $$ = DtDoOperator ($1, EXPOP_NOT_EQUAL, $3);}
162 1.1 christos | Expression EXPOP_AND Expression { $$ = DtDoOperator ($1, EXPOP_AND, $3);}
163 1.1 christos | Expression EXPOP_XOR Expression { $$ = DtDoOperator ($1, EXPOP_XOR, $3);}
164 1.1 christos | Expression EXPOP_OR Expression { $$ = DtDoOperator ($1, EXPOP_OR, $3);}
165 1.1 christos | Expression EXPOP_LOGICAL_AND Expression { $$ = DtDoOperator ($1, EXPOP_LOGICAL_AND, $3);}
166 1.1 christos | Expression EXPOP_LOGICAL_OR Expression { $$ = DtDoOperator ($1, EXPOP_LOGICAL_OR, $3);}
167 1.1 christos
168 1.1 christos /* Parentheses: '(' Expression ')' */
169 1.1 christos
170 1.1 christos | EXPOP_PAREN_OPEN Expression
171 1.1 christos EXPOP_PAREN_CLOSE { $$ = $2;}
172 1.1 christos
173 1.1 christos /* #if defined (ID) or #if defined ID */
174 1.1 christos
175 1.1 christos | EXPOP_DEFINE EXPOP_PAREN_OPEN EXPOP_IDENTIFIER
176 1.1 christos EXPOP_PAREN_CLOSE { $$ = PrIsDefined (PrParserlval.str);}
177 1.1 christos
178 1.1 christos | EXPOP_DEFINE EXPOP_IDENTIFIER { $$ = PrIsDefined (PrParserlval.str);}
179 1.1 christos
180 1.1 christos | EXPOP_IDENTIFIER { $$ = PrResolveDefine (PrParserlval.str);}
181 1.1 christos
182 1.1 christos /* Default base for a non-prefixed integer is 10 */
183 1.1 christos
184 1.1.1.9 christos | EXPOP_NUMBER { AcpiUtStrtoul64 (PrParsertext, &$$);}
185 1.1 christos
186 1.1 christos /* Standard hex number (0x1234) */
187 1.1 christos
188 1.1.1.9 christos | EXPOP_HEX_NUMBER { AcpiUtStrtoul64 (PrParsertext, &$$);}
189 1.1 christos ;
190 1.1 christos %%
191 1.1 christos
192 1.1 christos /*
193 1.1 christos * Local support functions, including parser entry point
194 1.1 christos */
195 1.1 christos #define PR_FIRST_PARSE_OPCODE EXPOP_EOF
196 1.1 christos #define PR_YYTNAME_START 3
197 1.1 christos
198 1.1 christos
199 1.1 christos /******************************************************************************
200 1.1 christos *
201 1.1 christos * FUNCTION: PrParsererror
202 1.1 christos *
203 1.1 christos * PARAMETERS: Message - Parser-generated error message
204 1.1 christos *
205 1.1 christos * RETURN: None
206 1.1 christos *
207 1.1 christos * DESCRIPTION: Handler for parser errors
208 1.1 christos *
209 1.1 christos *****************************************************************************/
210 1.1 christos
211 1.1 christos void
212 1.1 christos PrParsererror (
213 1.1 christos char const *Message)
214 1.1 christos {
215 1.1.1.4 christos
216 1.1.1.4 christos sprintf (StringBuffer, "Preprocessor Parser : %s (near line %u)",
217 1.1.1.4 christos Message, Gbl_CurrentLineNumber);
218 1.1 christos DtError (ASL_ERROR, ASL_MSG_SYNTAX,
219 1.1.1.4 christos NULL, (char *) StringBuffer);
220 1.1 christos }
221 1.1 christos
222 1.1 christos
223 1.1 christos /******************************************************************************
224 1.1 christos *
225 1.1 christos * FUNCTION: PrGetOpName
226 1.1 christos *
227 1.1 christos * PARAMETERS: ParseOpcode - Parser token (EXPOP_*)
228 1.1 christos *
229 1.1 christos * RETURN: Pointer to the opcode name
230 1.1 christos *
231 1.1 christos * DESCRIPTION: Get the ascii name of the parse opcode for debug output
232 1.1 christos *
233 1.1 christos *****************************************************************************/
234 1.1 christos
235 1.1 christos char *
236 1.1 christos PrGetOpName (
237 1.1 christos UINT32 ParseOpcode)
238 1.1 christos {
239 1.1 christos #ifdef ASL_YYTNAME_START
240 1.1 christos /*
241 1.1 christos * First entries (PR_YYTNAME_START) in yytname are special reserved names.
242 1.1 christos * Ignore first 6 characters of name (EXPOP_)
243 1.1 christos */
244 1.1 christos return ((char *) yytname
245 1.1 christos [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
246 1.1 christos #else
247 1.1 christos return ("[Unknown parser generator]");
248 1.1 christos #endif
249 1.1 christos }
250 1.1 christos
251 1.1 christos
252 1.1 christos /******************************************************************************
253 1.1 christos *
254 1.1 christos * FUNCTION: PrEvaluateExpression
255 1.1 christos *
256 1.1 christos * PARAMETERS: ExprString - Expression to be evaluated. Must be
257 1.1 christos * terminated by either a newline or a NUL
258 1.1 christos * string terminator
259 1.1 christos *
260 1.1 christos * RETURN: 64-bit value for the expression
261 1.1 christos *
262 1.1 christos * DESCRIPTION: Main entry point for the DT expression parser
263 1.1 christos *
264 1.1 christos *****************************************************************************/
265 1.1 christos
266 1.1 christos UINT64
267 1.1 christos PrEvaluateExpression (
268 1.1 christos char *ExprString)
269 1.1 christos {
270 1.1 christos
271 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT,
272 1.1 christos "**** Input expression: %s\n", ExprString);
273 1.1 christos
274 1.1 christos /* Point lexer to the input string */
275 1.1 christos
276 1.1 christos if (PrInitLexer (ExprString))
277 1.1 christos {
278 1.1 christos DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
279 1.1 christos NULL, "Could not initialize lexer");
280 1.1 christos return (0);
281 1.1 christos }
282 1.1 christos
283 1.1 christos /* Parse/Evaluate the input string (value returned in PrParserResult) */
284 1.1 christos
285 1.1 christos PrParserparse ();
286 1.1 christos PrTerminateLexer ();
287 1.1 christos
288 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT,
289 1.1 christos "**** Parser returned value: %u (%8.8X%8.8X)\n",
290 1.1 christos (UINT32) PrParserResult, ACPI_FORMAT_UINT64 (PrParserResult));
291 1.1 christos
292 1.1 christos return (PrParserResult);
293 1.1 christos }
294