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