1 /******************************************************************************
3 * Module Name: prexpress - Preprocessor #if expression support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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 "aslcompiler.h"
45 #include "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
);
88 if (Buffer
[Length
] != '\n')
90 Buffer
[strlen(Buffer
)] = ' ';
96 /******************************************************************************
98 * FUNCTION: PrExpandMacros
100 * PARAMETERS: Line - Pointer into the current line
102 * RETURN: Updated pointer into the current line
104 * DESCRIPTION: Expand any macros found in the current line buffer.
106 *****************************************************************************/
114 PR_DEFINE_INFO
*DefineInfo
;
115 ACPI_SIZE TokenOffset
;
120 strcpy (Gbl_ExpressionTokenBuffer
, Gbl_CurrentLineBuffer
);
121 Token
= PrGetNextToken (Gbl_ExpressionTokenBuffer
, PR_EXPR_SEPARATORS
, &Next
);
126 DefineInfo
= PrMatchDefine (Token
);
129 if (DefineInfo
->Body
)
131 /* This is a macro. TBD: Is this allowed? */
133 DbgPrint (ASL_DEBUG_OUTPUT
, PR_PREFIX_ID
134 "Matched Macro: %s->%s\n",
135 Gbl_CurrentLineNumber
, DefineInfo
->Identifier
,
136 DefineInfo
->Replacement
);
138 PrDoMacroInvocation (Gbl_ExpressionTokenBuffer
, Token
,
143 ReplaceString
= DefineInfo
->Replacement
;
145 /* Replace the name in the original line buffer */
147 TokenOffset
= Token
- Gbl_ExpressionTokenBuffer
+ OffsetAdjust
;
149 &Gbl_CurrentLineBuffer
[TokenOffset
], strlen (Token
),
150 ReplaceString
, strlen (ReplaceString
));
152 /* Adjust for length difference between old and new name length */
154 OffsetAdjust
+= strlen (ReplaceString
) - strlen (Token
);
156 DbgPrint (ASL_DEBUG_OUTPUT
, PR_PREFIX_ID
157 "Matched #define within expression: %s->%s\n",
158 Gbl_CurrentLineNumber
, Token
,
159 *ReplaceString
? ReplaceString
: "(NULL STRING)");
163 Token
= PrGetNextToken (NULL
, PR_EXPR_SEPARATORS
, &Next
);
170 /******************************************************************************
172 * FUNCTION: PrIsDefined
174 * PARAMETERS: Identifier - Name to be resolved
176 * RETURN: 64-bit boolean integer value
178 * DESCRIPTION: Returns TRUE if the name is defined, FALSE otherwise (0).
180 *****************************************************************************/
187 PR_DEFINE_INFO
*DefineInfo
;
190 DbgPrint (ASL_DEBUG_OUTPUT
, PR_PREFIX_ID
191 "**** Is defined?: %s\n", Gbl_CurrentLineNumber
, Identifier
);
193 Value
= 0; /* Default is "Not defined" -- FALSE */
195 DefineInfo
= PrMatchDefine (Identifier
);
198 Value
= ACPI_UINT64_MAX
; /* TRUE */
201 DbgPrint (ASL_DEBUG_OUTPUT
, PR_PREFIX_ID
202 "[#if defined %s] resolved to: %8.8X%8.8X\n",
203 Gbl_CurrentLineNumber
, Identifier
, ACPI_FORMAT_UINT64 (Value
));
209 /******************************************************************************
211 * FUNCTION: PrResolveDefine
213 * PARAMETERS: Identifier - Name to be resolved
215 * RETURN: A 64-bit boolean integer value
217 * DESCRIPTION: Returns TRUE if the name is defined, FALSE otherwise (0).
219 *****************************************************************************/
226 PR_DEFINE_INFO
*DefineInfo
;
229 DbgPrint (ASL_DEBUG_OUTPUT
, PR_PREFIX_ID
230 "**** Resolve #define: %s\n", Gbl_CurrentLineNumber
, Identifier
);
232 Value
= 0; /* Default is "Not defined" -- FALSE */
234 DefineInfo
= PrMatchDefine (Identifier
);
237 Value
= ACPI_UINT64_MAX
; /* TRUE */
240 DbgPrint (ASL_DEBUG_OUTPUT
, PR_PREFIX_ID
241 "[#if defined %s] resolved to: %8.8X%8.8X\n",
242 Gbl_CurrentLineNumber
, Identifier
, ACPI_FORMAT_UINT64 (Value
));
248 /******************************************************************************
250 * FUNCTION: PrResolveIntegerExpression
252 * PARAMETERS: Line - Pointer to integer expression
253 * ReturnValue - Where the resolved 64-bit integer is
258 * DESCRIPTION: Resolve an integer expression to a single value. Supports
259 * both integer constants and labels.
261 *****************************************************************************/
264 PrResolveIntegerExpression (
272 DbgPrint (ASL_DEBUG_OUTPUT
, PR_PREFIX_ID
273 "**** Resolve #if: %s\n", Gbl_CurrentLineNumber
, Line
);
275 /* Expand all macros within the expression first */
277 ExpandedLine
= PrExpandMacros (Line
);
279 /* Now we can evaluate the expression */
281 Result
= PrEvaluateExpression (ExpandedLine
);
282 DbgPrint (ASL_DEBUG_OUTPUT
, PR_PREFIX_ID
283 "**** Expression Resolved to: %8.8X%8.8X\n",
284 Gbl_CurrentLineNumber
, ACPI_FORMAT_UINT64 (Result
));
286 *ReturnValue
= Result
;
292 ACPI_FREE (EvalBuffer
);
293 PrError (ASL_ERROR
, ASL_MSG_INVALID_EXPRESSION
, 0);
299 DbgPrint (ASL_DEBUG_OUTPUT
, PR_PREFIX_ID
300 "**** Expression Resolved to: %8.8X%8.8X\n",
301 Gbl_CurrentLineNumber
, ACPI_FORMAT_UINT64 (Value1
));
303 *ReturnValue
= Value1
;