dtparser.y revision 220604
1%{
2/******************************************************************************
3 *
4 * Module Name: dtparser.y - Bison input file for table compiler parser
5 *
6 *****************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2011, 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#define YYDEBUG 1
46#define YYERROR_VERBOSE 1
47
48#include "aslcompiler.h"
49#include "dtcompiler.h"
50
51#define _COMPONENT          ACPI_COMPILER
52        ACPI_MODULE_NAME    ("dtparser")
53
54UINT64                  DtParserResult;    /* Global for expression return value */
55
56int DtParserlex (void); /* TBD: not sure why this is needed */
57extern char*            DtParsertext;
58extern void             DtParsererror (char const * msg);
59#define YYFLAG          -32768
60
61%}
62
63%union
64{
65     UINT64                 value;
66     UINT32                 op;
67}
68
69%type  <value>  Expression
70
71%token <op>     EXPOP_EOF
72%token <op>     EXPOP_NEW_LINE
73%token <op>     EXPOP_NUMBER
74%token <op>     EXPOP_HEX_NUMBER
75%token <op>     EXPOP_DECIMAL_NUMBER
76%token <op>     EXPOP_LABEL
77%token <op>     EXPOP_PAREN_OPEN
78%token <op>     EXPOP_PAREN_CLOSE
79
80%left <op>      EXPOP_LOGICAL_OR
81%left <op>      EXPOP_LOGICAL_AND
82%left <op>      EXPOP_OR
83%left <op>      EXPOP_XOR
84%left <op>      EXPOP_AND
85%left <op>      EXPOP_EQUAL EXPOP_NOT_EQUAL
86%left <op>      EXPOP_GREATER EXPOP_LESS EXPOP_GREATER_EQUAL EXPOP_LESS_EQUAL
87%left <op>      EXPOP_SHIFT_RIGHT EXPOP_SHIFT_LEFT
88%left <op>      EXPOP_ADD EXPOP_SUBTRACT
89%left <op>      EXPOP_MULTIPLY EXPOP_DIVIDE EXPOP_MODULO
90%right <op>     EXPOP_ONES_COMPLIMENT EXPOP_LOGICAL_NOT
91
92%%
93
94/*
95 *  Operator precedence rules (from K&R)
96 *
97 *  1)      ( )
98 *  2)      ! ~ (unary operators that are supported here)
99 *  3)      *   /   %
100 *  4)      +   -
101 *  5)      >>  <<
102 *  6)      <   >   <=  >=
103 *  7)      ==  !=
104 *  8)      &
105 *  9)      ^
106 *  10)     |
107 *  11)     &&
108 *  12)     ||
109 */
110Value
111    : Expression EXPOP_NEW_LINE                     { DtParserResult=$1; return 0; } /* End of line (newline) */
112    | Expression EXPOP_EOF                          { DtParserResult=$1; return 0; } /* End of string (0) */
113    ;
114
115Expression
116
117      /* Unary operators */
118
119    : EXPOP_LOGICAL_NOT         Expression          { $$ = DtDoOperator ($2, EXPOP_LOGICAL_NOT,     $2);}
120    | EXPOP_ONES_COMPLIMENT     Expression          { $$ = DtDoOperator ($2, EXPOP_ONES_COMPLIMENT, $2);}
121
122      /* Binary operators */
123
124    | Expression EXPOP_MULTIPLY         Expression  { $$ = DtDoOperator ($1, EXPOP_MULTIPLY,        $3);}
125    | Expression EXPOP_DIVIDE           Expression  { $$ = DtDoOperator ($1, EXPOP_DIVIDE,          $3);}
126    | Expression EXPOP_MODULO           Expression  { $$ = DtDoOperator ($1, EXPOP_MODULO,          $3);}
127    | Expression EXPOP_ADD              Expression  { $$ = DtDoOperator ($1, EXPOP_ADD,             $3);}
128    | Expression EXPOP_SUBTRACT         Expression  { $$ = DtDoOperator ($1, EXPOP_SUBTRACT,        $3);}
129    | Expression EXPOP_SHIFT_RIGHT      Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_RIGHT,     $3);}
130    | Expression EXPOP_SHIFT_LEFT       Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_LEFT,      $3);}
131    | Expression EXPOP_GREATER          Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER,         $3);}
132    | Expression EXPOP_LESS             Expression  { $$ = DtDoOperator ($1, EXPOP_LESS,            $3);}
133    | Expression EXPOP_GREATER_EQUAL    Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER_EQUAL,   $3);}
134    | Expression EXPOP_LESS_EQUAL       Expression  { $$ = DtDoOperator ($1, EXPOP_LESS_EQUAL,      $3);}
135    | Expression EXPOP_EQUAL            Expression  { $$ = DtDoOperator ($1, EXPOP_EQUAL,           $3);}
136    | Expression EXPOP_NOT_EQUAL        Expression  { $$ = DtDoOperator ($1, EXPOP_NOT_EQUAL,       $3);}
137    | Expression EXPOP_AND              Expression  { $$ = DtDoOperator ($1, EXPOP_AND,             $3);}
138    | Expression EXPOP_XOR              Expression  { $$ = DtDoOperator ($1, EXPOP_XOR,             $3);}
139    | Expression EXPOP_OR               Expression  { $$ = DtDoOperator ($1, EXPOP_OR,              $3);}
140    | Expression EXPOP_LOGICAL_AND      Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_AND,     $3);}
141    | Expression EXPOP_LOGICAL_OR       Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_OR,      $3);}
142
143      /* Parentheses: '(' Expression ')' */
144
145    | EXPOP_PAREN_OPEN          Expression
146        EXPOP_PAREN_CLOSE                           { $$ = $2;}
147
148      /* Label references (prefixed with $) */
149
150    | EXPOP_LABEL                                   { $$ = DtResolveLabel (DtParsertext);}
151
152      /* Default base for a non-prefixed integer is 16 */
153
154    | EXPOP_NUMBER                                  { UtStrtoul64 (DtParsertext, 16, &$$);}
155
156      /* Standard hex number (0x1234) */
157
158    | EXPOP_HEX_NUMBER                              { UtStrtoul64 (DtParsertext, 16, &$$);}
159
160      /* TBD: Decimal number with prefix (0d1234) - Not supported by UtStrtoul64 at this time */
161
162    | EXPOP_DECIMAL_NUMBER                          { UtStrtoul64 (DtParsertext, 10, &$$);}
163    ;
164%%
165
166/*
167 * Local support functions, including parser entry point
168 */
169extern DT_FIELD                 *Gbl_CurrentField;
170#define PR_FIRST_PARSE_OPCODE   EXPOP_EOF
171#define PR_YYTNAME_START        3
172
173#ifdef _USE_BERKELEY_YACC
174#define yytname DtParsername[];
175#endif
176
177
178/******************************************************************************
179 *
180 * FUNCTION:    DtParsererror
181 *
182 * PARAMETERS:  Message             - Parser-generated error message
183 *
184 * RETURN:      None
185 *
186 * DESCRIPTION: Handler for parser errors
187 *
188 *****************************************************************************/
189
190void
191DtParsererror (
192    char const              *Message)
193{
194    DtError (ASL_ERROR, ASL_MSG_SYNTAX,
195        Gbl_CurrentField, (char *) Message);
196}
197
198
199/******************************************************************************
200 *
201 * FUNCTION:    DtGetOpName
202 *
203 * PARAMETERS:  ParseOpcode         - Parser token (EXPOP_*)
204 *
205 * RETURN:      Pointer to the opcode name
206 *
207 * DESCRIPTION: Get the ascii name of the parse opcode for debug output
208 *
209 *****************************************************************************/
210
211char *
212DtGetOpName (
213    UINT32                  ParseOpcode)
214{
215
216    /*
217     * First entries (PR_YYTNAME_START) in yytname are special reserved names.
218     * Ignore first 6 characters of name (EXPOP_)
219     */
220    return ((char *) yytname
221        [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
222}
223
224
225/******************************************************************************
226 *
227 * FUNCTION:    DtEvaluateExpression
228 *
229 * PARAMETERS:  ExprString          - Expression to be evaluated. Must be
230 *                                    terminated by either a newline or a NUL
231 *                                    string terminator
232 *
233 * RETURN:      64-bit value for the expression
234 *
235 * DESCRIPTION: Main entry point for the DT expression parser
236 *
237 *****************************************************************************/
238
239UINT64
240DtEvaluateExpression (
241    char                    *ExprString)
242{
243
244    DbgPrint (ASL_DEBUG_OUTPUT,
245        "**** Input expression: %s  (Base 16)\n", ExprString);
246
247    /* Point lexer to the input string */
248
249    if (DtInitLexer (ExprString))
250    {
251        DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
252            Gbl_CurrentField, "Could not initialize lexer");
253        return (0);
254    }
255
256    /* Parse/Evaluate the input string (value returned in DtParserResult) */
257
258    DtParserparse ();
259    DtTerminateLexer ();
260
261    DbgPrint (ASL_DEBUG_OUTPUT,
262        "**** Parser returned value: %u (%8.8X%8.8X)\n",
263        (UINT32) DtParserResult, ACPI_FORMAT_UINT64 (DtParserResult));
264
265    return (DtParserResult);
266}
267