dtparser.y revision 229989
1220604Sjkim%{
2220604Sjkim/******************************************************************************
3220604Sjkim *
4220604Sjkim * Module Name: dtparser.y - Bison input file for table compiler parser
5220604Sjkim *
6220604Sjkim *****************************************************************************/
7220604Sjkim
8220604Sjkim/*
9229989Sjkim * Copyright (C) 2000 - 2012, Intel Corp.
10220604Sjkim * All rights reserved.
11220604Sjkim *
12220604Sjkim * Redistribution and use in source and binary forms, with or without
13220604Sjkim * modification, are permitted provided that the following conditions
14220604Sjkim * are met:
15220604Sjkim * 1. Redistributions of source code must retain the above copyright
16220604Sjkim *    notice, this list of conditions, and the following disclaimer,
17220604Sjkim *    without modification.
18220604Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19220604Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
20220604Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
21220604Sjkim *    including a substantially similar Disclaimer requirement for further
22220604Sjkim *    binary redistribution.
23220604Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
24220604Sjkim *    of any contributors may be used to endorse or promote products derived
25220604Sjkim *    from this software without specific prior written permission.
26220604Sjkim *
27220604Sjkim * Alternatively, this software may be distributed under the terms of the
28220604Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
29220604Sjkim * Software Foundation.
30220604Sjkim *
31220604Sjkim * NO WARRANTY
32220604Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33220604Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34220604Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35220604Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36220604Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37220604Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38220604Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39220604Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40220604Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41220604Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42220604Sjkim * POSSIBILITY OF SUCH DAMAGES.
43220604Sjkim */
44220604Sjkim
45220663Sjkim#include <contrib/dev/acpica/compiler/aslcompiler.h>
46220663Sjkim#include <contrib/dev/acpica/compiler/dtcompiler.h>
47220604Sjkim
48220681Sjkim#define _COMPONENT          DT_COMPILER
49220604Sjkim        ACPI_MODULE_NAME    ("dtparser")
50220604Sjkim
51220681Sjkimint                         DtParserlex (void);
52220681Sjkimint                         DtParserparse (void);
53220681Sjkimvoid                        DtParsererror (char const *msg);
54220681Sjkimextern char                 *DtParsertext;
55220681Sjkimextern DT_FIELD             *Gbl_CurrentField;
56220604Sjkim
57220681SjkimUINT64                      DtParserResult; /* Expression return value */
58220604Sjkim
59220681Sjkim/* Bison/yacc configuration */
60220681Sjkim
61220681Sjkim#define yytname             DtParsername
62220681Sjkim#define YYDEBUG             1               /* Enable debug output */
63220681Sjkim#define YYERROR_VERBOSE     1               /* Verbose error messages */
64220681Sjkim#define YYFLAG              -32768
65220681Sjkim
66220604Sjkim%}
67220604Sjkim
68220604Sjkim%union
69220604Sjkim{
70220604Sjkim     UINT64                 value;
71220604Sjkim     UINT32                 op;
72220604Sjkim}
73220604Sjkim
74220681Sjkim/*! [Begin] no source code translation */
75220681Sjkim
76220604Sjkim%type  <value>  Expression
77220604Sjkim
78220604Sjkim%token <op>     EXPOP_EOF
79220604Sjkim%token <op>     EXPOP_NEW_LINE
80220604Sjkim%token <op>     EXPOP_NUMBER
81220604Sjkim%token <op>     EXPOP_HEX_NUMBER
82220604Sjkim%token <op>     EXPOP_DECIMAL_NUMBER
83220604Sjkim%token <op>     EXPOP_LABEL
84220604Sjkim%token <op>     EXPOP_PAREN_OPEN
85220604Sjkim%token <op>     EXPOP_PAREN_CLOSE
86220604Sjkim
87220604Sjkim%left <op>      EXPOP_LOGICAL_OR
88220604Sjkim%left <op>      EXPOP_LOGICAL_AND
89220604Sjkim%left <op>      EXPOP_OR
90220604Sjkim%left <op>      EXPOP_XOR
91220604Sjkim%left <op>      EXPOP_AND
92220604Sjkim%left <op>      EXPOP_EQUAL EXPOP_NOT_EQUAL
93220604Sjkim%left <op>      EXPOP_GREATER EXPOP_LESS EXPOP_GREATER_EQUAL EXPOP_LESS_EQUAL
94220604Sjkim%left <op>      EXPOP_SHIFT_RIGHT EXPOP_SHIFT_LEFT
95220604Sjkim%left <op>      EXPOP_ADD EXPOP_SUBTRACT
96220604Sjkim%left <op>      EXPOP_MULTIPLY EXPOP_DIVIDE EXPOP_MODULO
97220604Sjkim%right <op>     EXPOP_ONES_COMPLIMENT EXPOP_LOGICAL_NOT
98220604Sjkim
99220604Sjkim%%
100220604Sjkim
101220604Sjkim/*
102220604Sjkim *  Operator precedence rules (from K&R)
103220604Sjkim *
104220604Sjkim *  1)      ( )
105220604Sjkim *  2)      ! ~ (unary operators that are supported here)
106220604Sjkim *  3)      *   /   %
107220604Sjkim *  4)      +   -
108220604Sjkim *  5)      >>  <<
109220604Sjkim *  6)      <   >   <=  >=
110220604Sjkim *  7)      ==  !=
111220604Sjkim *  8)      &
112220604Sjkim *  9)      ^
113220604Sjkim *  10)     |
114220604Sjkim *  11)     &&
115220604Sjkim *  12)     ||
116220604Sjkim */
117220604SjkimValue
118220604Sjkim    : Expression EXPOP_NEW_LINE                     { DtParserResult=$1; return 0; } /* End of line (newline) */
119220604Sjkim    | Expression EXPOP_EOF                          { DtParserResult=$1; return 0; } /* End of string (0) */
120220604Sjkim    ;
121220604Sjkim
122220604SjkimExpression
123220604Sjkim
124220604Sjkim      /* Unary operators */
125220604Sjkim
126220604Sjkim    : EXPOP_LOGICAL_NOT         Expression          { $$ = DtDoOperator ($2, EXPOP_LOGICAL_NOT,     $2);}
127220604Sjkim    | EXPOP_ONES_COMPLIMENT     Expression          { $$ = DtDoOperator ($2, EXPOP_ONES_COMPLIMENT, $2);}
128220604Sjkim
129220604Sjkim      /* Binary operators */
130220604Sjkim
131220604Sjkim    | Expression EXPOP_MULTIPLY         Expression  { $$ = DtDoOperator ($1, EXPOP_MULTIPLY,        $3);}
132220604Sjkim    | Expression EXPOP_DIVIDE           Expression  { $$ = DtDoOperator ($1, EXPOP_DIVIDE,          $3);}
133220604Sjkim    | Expression EXPOP_MODULO           Expression  { $$ = DtDoOperator ($1, EXPOP_MODULO,          $3);}
134220604Sjkim    | Expression EXPOP_ADD              Expression  { $$ = DtDoOperator ($1, EXPOP_ADD,             $3);}
135220604Sjkim    | Expression EXPOP_SUBTRACT         Expression  { $$ = DtDoOperator ($1, EXPOP_SUBTRACT,        $3);}
136220604Sjkim    | Expression EXPOP_SHIFT_RIGHT      Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_RIGHT,     $3);}
137220604Sjkim    | Expression EXPOP_SHIFT_LEFT       Expression  { $$ = DtDoOperator ($1, EXPOP_SHIFT_LEFT,      $3);}
138220604Sjkim    | Expression EXPOP_GREATER          Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER,         $3);}
139220604Sjkim    | Expression EXPOP_LESS             Expression  { $$ = DtDoOperator ($1, EXPOP_LESS,            $3);}
140220604Sjkim    | Expression EXPOP_GREATER_EQUAL    Expression  { $$ = DtDoOperator ($1, EXPOP_GREATER_EQUAL,   $3);}
141220604Sjkim    | Expression EXPOP_LESS_EQUAL       Expression  { $$ = DtDoOperator ($1, EXPOP_LESS_EQUAL,      $3);}
142220604Sjkim    | Expression EXPOP_EQUAL            Expression  { $$ = DtDoOperator ($1, EXPOP_EQUAL,           $3);}
143220604Sjkim    | Expression EXPOP_NOT_EQUAL        Expression  { $$ = DtDoOperator ($1, EXPOP_NOT_EQUAL,       $3);}
144220604Sjkim    | Expression EXPOP_AND              Expression  { $$ = DtDoOperator ($1, EXPOP_AND,             $3);}
145220604Sjkim    | Expression EXPOP_XOR              Expression  { $$ = DtDoOperator ($1, EXPOP_XOR,             $3);}
146220604Sjkim    | Expression EXPOP_OR               Expression  { $$ = DtDoOperator ($1, EXPOP_OR,              $3);}
147220604Sjkim    | Expression EXPOP_LOGICAL_AND      Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_AND,     $3);}
148220604Sjkim    | Expression EXPOP_LOGICAL_OR       Expression  { $$ = DtDoOperator ($1, EXPOP_LOGICAL_OR,      $3);}
149220604Sjkim
150220604Sjkim      /* Parentheses: '(' Expression ')' */
151220604Sjkim
152220604Sjkim    | EXPOP_PAREN_OPEN          Expression
153220604Sjkim        EXPOP_PAREN_CLOSE                           { $$ = $2;}
154220604Sjkim
155220604Sjkim      /* Label references (prefixed with $) */
156220604Sjkim
157220604Sjkim    | EXPOP_LABEL                                   { $$ = DtResolveLabel (DtParsertext);}
158220604Sjkim
159220604Sjkim      /* Default base for a non-prefixed integer is 16 */
160220604Sjkim
161220604Sjkim    | EXPOP_NUMBER                                  { UtStrtoul64 (DtParsertext, 16, &$$);}
162220604Sjkim
163220604Sjkim      /* Standard hex number (0x1234) */
164220604Sjkim
165220604Sjkim    | EXPOP_HEX_NUMBER                              { UtStrtoul64 (DtParsertext, 16, &$$);}
166220604Sjkim
167220604Sjkim      /* TBD: Decimal number with prefix (0d1234) - Not supported by UtStrtoul64 at this time */
168220604Sjkim
169220604Sjkim    | EXPOP_DECIMAL_NUMBER                          { UtStrtoul64 (DtParsertext, 10, &$$);}
170220604Sjkim    ;
171220604Sjkim%%
172220604Sjkim
173220681Sjkim/*! [End] no source code translation !*/
174220681Sjkim
175220604Sjkim/*
176220604Sjkim * Local support functions, including parser entry point
177220604Sjkim */
178220604Sjkim#define PR_FIRST_PARSE_OPCODE   EXPOP_EOF
179220604Sjkim#define PR_YYTNAME_START        3
180220604Sjkim
181220604Sjkim
182220604Sjkim/******************************************************************************
183220604Sjkim *
184220604Sjkim * FUNCTION:    DtParsererror
185220604Sjkim *
186220604Sjkim * PARAMETERS:  Message             - Parser-generated error message
187220604Sjkim *
188220604Sjkim * RETURN:      None
189220604Sjkim *
190220604Sjkim * DESCRIPTION: Handler for parser errors
191220604Sjkim *
192220604Sjkim *****************************************************************************/
193220604Sjkim
194220604Sjkimvoid
195220604SjkimDtParsererror (
196220604Sjkim    char const              *Message)
197220604Sjkim{
198220604Sjkim    DtError (ASL_ERROR, ASL_MSG_SYNTAX,
199220604Sjkim        Gbl_CurrentField, (char *) Message);
200220604Sjkim}
201220604Sjkim
202220604Sjkim
203220604Sjkim/******************************************************************************
204220604Sjkim *
205220604Sjkim * FUNCTION:    DtGetOpName
206220604Sjkim *
207220604Sjkim * PARAMETERS:  ParseOpcode         - Parser token (EXPOP_*)
208220604Sjkim *
209220604Sjkim * RETURN:      Pointer to the opcode name
210220604Sjkim *
211220604Sjkim * DESCRIPTION: Get the ascii name of the parse opcode for debug output
212220604Sjkim *
213220604Sjkim *****************************************************************************/
214220604Sjkim
215220604Sjkimchar *
216220604SjkimDtGetOpName (
217220604Sjkim    UINT32                  ParseOpcode)
218220604Sjkim{
219220681Sjkim#ifdef ASL_YYTNAME_START
220220604Sjkim    /*
221220604Sjkim     * First entries (PR_YYTNAME_START) in yytname are special reserved names.
222220604Sjkim     * Ignore first 6 characters of name (EXPOP_)
223220604Sjkim     */
224220604Sjkim    return ((char *) yytname
225220604Sjkim        [(ParseOpcode - PR_FIRST_PARSE_OPCODE) + PR_YYTNAME_START] + 6);
226220681Sjkim#else
227220681Sjkim    return ("[Unknown parser generator]");
228220681Sjkim#endif
229220604Sjkim}
230220604Sjkim
231220604Sjkim
232220604Sjkim/******************************************************************************
233220604Sjkim *
234220604Sjkim * FUNCTION:    DtEvaluateExpression
235220604Sjkim *
236220604Sjkim * PARAMETERS:  ExprString          - Expression to be evaluated. Must be
237220604Sjkim *                                    terminated by either a newline or a NUL
238220604Sjkim *                                    string terminator
239220604Sjkim *
240220604Sjkim * RETURN:      64-bit value for the expression
241220604Sjkim *
242220604Sjkim * DESCRIPTION: Main entry point for the DT expression parser
243220604Sjkim *
244220604Sjkim *****************************************************************************/
245220604Sjkim
246220604SjkimUINT64
247220604SjkimDtEvaluateExpression (
248220604Sjkim    char                    *ExprString)
249220604Sjkim{
250220604Sjkim
251220604Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
252220604Sjkim        "**** Input expression: %s  (Base 16)\n", ExprString);
253220604Sjkim
254220604Sjkim    /* Point lexer to the input string */
255220604Sjkim
256220604Sjkim    if (DtInitLexer (ExprString))
257220604Sjkim    {
258220604Sjkim        DtError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
259220604Sjkim            Gbl_CurrentField, "Could not initialize lexer");
260220604Sjkim        return (0);
261220604Sjkim    }
262220604Sjkim
263220604Sjkim    /* Parse/Evaluate the input string (value returned in DtParserResult) */
264220604Sjkim
265220604Sjkim    DtParserparse ();
266220604Sjkim    DtTerminateLexer ();
267220604Sjkim
268220604Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
269220604Sjkim        "**** Parser returned value: %u (%8.8X%8.8X)\n",
270220604Sjkim        (UINT32) DtParserResult, ACPI_FORMAT_UINT64 (DtParserResult));
271220604Sjkim
272220604Sjkim    return (DtParserResult);
273220604Sjkim}
274