Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / compiler / prexpress.c
blob22d797bad3020ed8aa0c2a987a94c75f87251c76
1 /******************************************************************************
3 * Module Name: prexpress - Preprocessor #if expression support
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, 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;
87 if (Buffer[Length] != '\n')
89 Buffer[strlen(Buffer)] = ' ';
92 #endif
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 *****************************************************************************/
107 static char *
108 PrExpandMacros (
109 char *Line)
111 char *Token;
112 char *ReplaceString;
113 PR_DEFINE_INFO *DefineInfo;
114 ACPI_SIZE TokenOffset;
115 char *Next;
116 int OffsetAdjust;
119 strcpy (Gbl_ExpressionTokenBuffer, Gbl_CurrentLineBuffer);
120 Token = PrGetNextToken (Gbl_ExpressionTokenBuffer, PR_EXPR_SEPARATORS, &Next);
121 OffsetAdjust = 0;
123 while (Token)
125 DefineInfo = PrMatchDefine (Token);
126 if (DefineInfo)
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,
138 DefineInfo, &Next);
140 else
142 ReplaceString = DefineInfo->Replacement;
144 /* Replace the name in the original line buffer */
146 TokenOffset = Token - Gbl_ExpressionTokenBuffer + OffsetAdjust;
147 PrReplaceData (
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);
165 return (Line);
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 *****************************************************************************/
181 UINT64
182 PrIsDefined (
183 char *Identifier)
185 UINT64 Value;
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);
195 if (DefineInfo)
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));
204 return (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 *****************************************************************************/
220 UINT64
221 PrResolveDefine (
222 char *Identifier)
224 UINT64 Value;
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);
234 if (DefineInfo)
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));
243 return (Value);
247 /******************************************************************************
249 * FUNCTION: PrResolveIntegerExpression
251 * PARAMETERS: Line - Pointer to integer expression
252 * ReturnValue - Where the resolved 64-bit integer is
253 * returned.
255 * RETURN: Status
257 * DESCRIPTION: Resolve an integer expression to a single value. Supports
258 * both integer constants and labels.
260 *****************************************************************************/
262 ACPI_STATUS
263 PrResolveIntegerExpression (
264 char *Line,
265 UINT64 *ReturnValue)
267 UINT64 Result;
268 char *ExpandedLine;
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;
286 return (AE_OK);
288 #if 0
289 InvalidExpression:
291 ACPI_FREE (EvalBuffer);
292 PrError (ASL_ERROR, ASL_MSG_INVALID_EXPRESSION, 0);
293 return (AE_ERROR);
296 NormalExit:
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;
303 return (AE_OK);
304 #endif