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