Sync ACPICA with Intel's version 20160831.
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / prexpress.c
blobb6bc801f2580dff2aabce825a8160052b4c3909d
1 /******************************************************************************
3 * Module Name: prexpress - Preprocessor #if expression support
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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 */
53 static char *
54 PrExpandMacros (
55 char *Line);
58 #ifdef _UNDER_DEVELOPMENT
59 /******************************************************************************
61 * FUNCTION: PrUnTokenize
63 * PARAMETERS: Buffer - Token Buffer
64 * Next - "Next" buffer from GetNextToken
66 * RETURN: None
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 *****************************************************************************/
75 static void
76 PrUnTokenize (
77 char *Buffer,
78 char *Next)
80 UINT32 Length = strlen (Buffer);
83 if (!Next)
85 return;
88 if (Buffer[Length] != '\n')
90 Buffer[strlen(Buffer)] = ' ';
93 #endif
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 *****************************************************************************/
108 static char *
109 PrExpandMacros (
110 char *Line)
112 char *Token;
113 char *ReplaceString;
114 PR_DEFINE_INFO *DefineInfo;
115 ACPI_SIZE TokenOffset;
116 char *Next;
117 int OffsetAdjust;
120 strcpy (Gbl_ExpressionTokenBuffer, Gbl_CurrentLineBuffer);
121 Token = PrGetNextToken (Gbl_ExpressionTokenBuffer, PR_EXPR_SEPARATORS, &Next);
122 OffsetAdjust = 0;
124 while (Token)
126 DefineInfo = PrMatchDefine (Token);
127 if (DefineInfo)
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,
139 DefineInfo, &Next);
141 else
143 ReplaceString = DefineInfo->Replacement;
145 /* Replace the name in the original line buffer */
147 TokenOffset = Token - Gbl_ExpressionTokenBuffer + OffsetAdjust;
148 PrReplaceData (
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);
166 return (Line);
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 *****************************************************************************/
182 UINT64
183 PrIsDefined (
184 char *Identifier)
186 UINT64 Value;
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);
196 if (DefineInfo)
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));
205 return (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 *****************************************************************************/
221 UINT64
222 PrResolveDefine (
223 char *Identifier)
225 UINT64 Value;
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);
235 if (DefineInfo)
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));
244 return (Value);
248 /******************************************************************************
250 * FUNCTION: PrResolveIntegerExpression
252 * PARAMETERS: Line - Pointer to integer expression
253 * ReturnValue - Where the resolved 64-bit integer is
254 * returned.
256 * RETURN: Status
258 * DESCRIPTION: Resolve an integer expression to a single value. Supports
259 * both integer constants and labels.
261 *****************************************************************************/
263 ACPI_STATUS
264 PrResolveIntegerExpression (
265 char *Line,
266 UINT64 *ReturnValue)
268 UINT64 Result;
269 char *ExpandedLine;
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;
287 return (AE_OK);
289 #if 0
290 InvalidExpression:
292 ACPI_FREE (EvalBuffer);
293 PrError (ASL_ERROR, ASL_MSG_INVALID_EXPRESSION, 0);
294 return (AE_ERROR);
297 NormalExit:
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;
304 return (AE_OK);
305 #endif