1 /******************************************************************************
3 * Module Name: prexpress - Preprocessor #if expression support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/compiler/dtcompiler.h>
48 #define _COMPONENT ASL_PREPROCESSOR
49 ACPI_MODULE_NAME ("prexpress")
51 /* Local prototypes */
58 #ifdef _UNDER_DEVELOPMENT
59 /******************************************************************************
61 * FUNCTION: PrUnTokenize
63 * PARAMETERS: Buffer - Token Buffer
64 * Next - "Next" buffer from GetNextToken
68 * DESCRIPTION: Un-tokenized the current token buffer. The implementation is
69 * to simply set the null inserted by GetNextToken to a blank.
70 * If Next is NULL, there were no tokens found in the Buffer,
71 * so there is nothing to do.
73 *****************************************************************************/
80 UINT32 Length = strlen (Buffer);
87 if (Buffer[Length] != '\n')
89 Buffer[strlen(Buffer)] = ' ';
95 /******************************************************************************
97 * FUNCTION: PrExpandMacros
99 * PARAMETERS: Line - Pointer into the current line
101 * RETURN: Updated pointer into the current line
103 * DESCRIPTION: Expand any macros found in the current line buffer.
105 *****************************************************************************/
113 PR_DEFINE_INFO *DefineInfo;
114 ACPI_SIZE TokenOffset;
119 strcpy (Gbl_ExpressionTokenBuffer, Gbl_CurrentLineBuffer);
120 Token = PrGetNextToken (Gbl_ExpressionTokenBuffer, PR_EXPR_SEPARATORS, &Next);
125 DefineInfo = PrMatchDefine (Token);
128 if (DefineInfo->Body)
130 /* This is a macro. TBD: Is this allowed? */
132 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
133 "Matched Macro: %s->%s\n",
134 Gbl_CurrentLineNumber, DefineInfo->Identifier,
135 DefineInfo->Replacement);
137 PrDoMacroInvocation (Gbl_ExpressionTokenBuffer, Token,
142 ReplaceString = DefineInfo->Replacement;
144 /* Replace the name in the original line buffer */
146 TokenOffset = Token - Gbl_ExpressionTokenBuffer + OffsetAdjust;
148 &Gbl_CurrentLineBuffer[TokenOffset], strlen (Token),
149 ReplaceString, strlen (ReplaceString));
151 /* Adjust for length difference between old and new name length */
153 OffsetAdjust += strlen (ReplaceString) - strlen (Token);
155 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
156 "Matched #define within expression: %s->%s\n",
157 Gbl_CurrentLineNumber, Token,
158 *ReplaceString ? ReplaceString : "(NULL STRING)");
162 Token = PrGetNextToken (NULL, PR_EXPR_SEPARATORS, &Next);
169 /******************************************************************************
171 * FUNCTION: PrIsDefined
173 * PARAMETERS: Identifier - Name to be resolved
175 * RETURN: 64-bit boolean integer value
177 * DESCRIPTION: Returns TRUE if the name is defined, FALSE otherwise (0).
179 *****************************************************************************/
186 PR_DEFINE_INFO *DefineInfo;
189 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
190 "**** Is defined?: %s\n", Gbl_CurrentLineNumber, Identifier);
192 Value = 0; /* Default is "Not defined" -- FALSE */
194 DefineInfo = PrMatchDefine (Identifier);
197 Value = ACPI_UINT64_MAX; /* TRUE */
200 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
201 "[#if defined %s] resolved to: %8.8X%8.8X\n",
202 Gbl_CurrentLineNumber, Identifier, ACPI_FORMAT_UINT64 (Value));
208 /******************************************************************************
210 * FUNCTION: PrResolveDefine
212 * PARAMETERS: Identifier - Name to be resolved
214 * RETURN: A 64-bit boolean integer value
216 * DESCRIPTION: Returns TRUE if the name is defined, FALSE otherwise (0).
218 *****************************************************************************/
225 PR_DEFINE_INFO *DefineInfo;
228 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
229 "**** Resolve #define: %s\n", Gbl_CurrentLineNumber, Identifier);
231 Value = 0; /* Default is "Not defined" -- FALSE */
233 DefineInfo = PrMatchDefine (Identifier);
236 Value = ACPI_UINT64_MAX; /* TRUE */
239 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
240 "[#if defined %s] resolved to: %8.8X%8.8X\n",
241 Gbl_CurrentLineNumber, Identifier, ACPI_FORMAT_UINT64 (Value));
247 /******************************************************************************
249 * FUNCTION: PrResolveIntegerExpression
251 * PARAMETERS: Line - Pointer to integer expression
252 * ReturnValue - Where the resolved 64-bit integer is
257 * DESCRIPTION: Resolve an integer expression to a single value. Supports
258 * both integer constants and labels.
260 *****************************************************************************/
263 PrResolveIntegerExpression (
271 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
272 "**** Resolve #if: %s\n", Gbl_CurrentLineNumber, Line);
274 /* Expand all macros within the expression first */
276 ExpandedLine = PrExpandMacros (Line);
278 /* Now we can evaluate the expression */
280 Result = PrEvaluateExpression (ExpandedLine);
281 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
282 "**** Expression Resolved to: %8.8X%8.8X\n",
283 Gbl_CurrentLineNumber, ACPI_FORMAT_UINT64 (Result));
285 *ReturnValue = Result;
291 ACPI_FREE (EvalBuffer);
292 PrError (ASL_ERROR, ASL_MSG_INVALID_EXPRESSION, 0);
298 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
299 "**** Expression Resolved to: %8.8X%8.8X\n",
300 Gbl_CurrentLineNumber, ACPI_FORMAT_UINT64 (Value1));
302 *ReturnValue = Value1;