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