Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / compiler / dtexpress.c
bloba3f9c8c1b1052942339464ea3bc822fdf51a1ada
1 /******************************************************************************
3 * Module Name: dtexpress.c - Support for integer expressions and labels
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 #define __DTEXPRESS_C__
46 #include "aslcompiler.h"
47 #include "dtcompiler.h"
48 #include "dtparser.y.h"
50 #define _COMPONENT DT_COMPILER
51 ACPI_MODULE_NAME ("dtexpress")
54 /* Local prototypes */
56 static void
57 DtInsertLabelField (
58 DT_FIELD *Field);
60 static DT_FIELD *
61 DtLookupLabel (
62 char *Name);
64 /* Global used for errors during parse and related functions */
66 DT_FIELD *Gbl_CurrentField;
69 /******************************************************************************
71 * FUNCTION: DtResolveIntegerExpression
73 * PARAMETERS: Field - Field object with Integer expression
74 * ReturnValue - Where the integer is returned
76 * RETURN: Status, and the resolved 64-bit integer value
78 * DESCRIPTION: Resolve an integer expression to a single value. Supports
79 * both integer constants and labels.
81 *****************************************************************************/
83 ACPI_STATUS
84 DtResolveIntegerExpression (
85 DT_FIELD *Field,
86 UINT64 *ReturnValue)
88 UINT64 Result;
91 DbgPrint (ASL_DEBUG_OUTPUT, "Full Integer expression: %s\n",
92 Field->Value);
94 Gbl_CurrentField = Field;
96 Result = DtEvaluateExpression (Field->Value);
97 *ReturnValue = Result;
98 return (AE_OK);
102 /******************************************************************************
104 * FUNCTION: DtDoOperator
106 * PARAMETERS: LeftValue - First 64-bit operand
107 * Operator - Parse token for the operator (EXPOP_*)
108 * RightValue - Second 64-bit operand
110 * RETURN: 64-bit result of the requested operation
112 * DESCRIPTION: Perform the various 64-bit integer math functions
114 *****************************************************************************/
116 UINT64
117 DtDoOperator (
118 UINT64 LeftValue,
119 UINT32 Operator,
120 UINT64 RightValue)
122 UINT64 Result;
125 /* Perform the requested operation */
127 switch (Operator)
129 case EXPOP_ONES_COMPLIMENT:
131 Result = ~RightValue;
132 break;
134 case EXPOP_LOGICAL_NOT:
136 Result = !RightValue;
137 break;
139 case EXPOP_MULTIPLY:
141 Result = LeftValue * RightValue;
142 break;
144 case EXPOP_DIVIDE:
146 if (!RightValue)
148 DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
149 Gbl_CurrentField, NULL);
150 return (0);
152 Result = LeftValue / RightValue;
153 break;
155 case EXPOP_MODULO:
157 if (!RightValue)
159 DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
160 Gbl_CurrentField, NULL);
161 return (0);
163 Result = LeftValue % RightValue;
164 break;
166 case EXPOP_ADD:
167 Result = LeftValue + RightValue;
168 break;
170 case EXPOP_SUBTRACT:
172 Result = LeftValue - RightValue;
173 break;
175 case EXPOP_SHIFT_RIGHT:
177 Result = LeftValue >> RightValue;
178 break;
180 case EXPOP_SHIFT_LEFT:
182 Result = LeftValue << RightValue;
183 break;
185 case EXPOP_LESS:
187 Result = LeftValue < RightValue;
188 break;
190 case EXPOP_GREATER:
192 Result = LeftValue > RightValue;
193 break;
195 case EXPOP_LESS_EQUAL:
197 Result = LeftValue <= RightValue;
198 break;
200 case EXPOP_GREATER_EQUAL:
202 Result = LeftValue >= RightValue;
203 break;
205 case EXPOP_EQUAL:
207 Result = LeftValue == RightValue;
208 break;
210 case EXPOP_NOT_EQUAL:
212 Result = LeftValue != RightValue;
213 break;
215 case EXPOP_AND:
217 Result = LeftValue & RightValue;
218 break;
220 case EXPOP_XOR:
222 Result = LeftValue ^ RightValue;
223 break;
225 case EXPOP_OR:
227 Result = LeftValue | RightValue;
228 break;
230 case EXPOP_LOGICAL_AND:
232 Result = LeftValue && RightValue;
233 break;
235 case EXPOP_LOGICAL_OR:
237 Result = LeftValue || RightValue;
238 break;
240 default:
242 /* Unknown operator */
244 DtFatal (ASL_MSG_INVALID_EXPRESSION,
245 Gbl_CurrentField, NULL);
246 return (0);
249 DbgPrint (ASL_DEBUG_OUTPUT,
250 "IntegerEval: (%8.8X%8.8X %s %8.8X%8.8X) = %8.8X%8.8X\n",
251 ACPI_FORMAT_UINT64 (LeftValue),
252 DtGetOpName (Operator),
253 ACPI_FORMAT_UINT64 (RightValue),
254 ACPI_FORMAT_UINT64 (Result));
256 return (Result);
260 /******************************************************************************
262 * FUNCTION: DtResolveLabel
264 * PARAMETERS: LabelString - Contains the label
266 * RETURN: Table offset associated with the label
268 * DESCRIPTION: Lookup a lable and return its value.
270 *****************************************************************************/
272 UINT64
273 DtResolveLabel (
274 char *LabelString)
276 DT_FIELD *LabelField;
279 DbgPrint (ASL_DEBUG_OUTPUT, "Resolve Label: %s\n", LabelString);
281 /* Resolve a label reference to an integer (table offset) */
283 if (*LabelString != '$')
285 return (0);
288 LabelField = DtLookupLabel (LabelString);
289 if (!LabelField)
291 DtError (ASL_ERROR, ASL_MSG_UNKNOWN_LABEL,
292 Gbl_CurrentField, LabelString);
293 return (0);
296 /* All we need from the label is the offset in the table */
298 DbgPrint (ASL_DEBUG_OUTPUT, "Resolved Label: 0x%8.8X\n",
299 LabelField->TableOffset);
301 return (LabelField->TableOffset);
305 /******************************************************************************
307 * FUNCTION: DtDetectAllLabels
309 * PARAMETERS: FieldList - Field object at start of generic list
311 * RETURN: None
313 * DESCRIPTION: Detect all labels in a list of "generic" opcodes (such as
314 * a UEFI table.) and insert them into the global label list.
316 *****************************************************************************/
318 void
319 DtDetectAllLabels (
320 DT_FIELD *FieldList)
322 ACPI_DMTABLE_INFO *Info;
323 DT_FIELD *GenericField;
324 UINT32 TableOffset;
327 TableOffset = Gbl_CurrentTableOffset;
328 GenericField = FieldList;
331 * Process all "Label:" fields within the parse tree. We need
332 * to know the offsets for all labels before we can compile
333 * the parse tree in order to handle forward references. Traverse
334 * tree and get/set all field lengths of all operators in order to
335 * determine the label offsets.
337 while (GenericField)
339 Info = DtGetGenericTableInfo (GenericField->Name);
340 if (Info)
342 /* Maintain table offsets */
344 GenericField->TableOffset = TableOffset;
345 TableOffset += DtGetFieldLength (GenericField, Info);
347 /* Insert all labels in the global label list */
349 if (Info->Opcode == ACPI_DMT_LABEL)
351 DtInsertLabelField (GenericField);
355 GenericField = GenericField->Next;
360 /******************************************************************************
362 * FUNCTION: DtInsertLabelField
364 * PARAMETERS: Field - Field object with Label to be inserted
366 * RETURN: None
368 * DESCRIPTION: Insert a label field into the global label list
370 *****************************************************************************/
372 static void
373 DtInsertLabelField (
374 DT_FIELD *Field)
377 DbgPrint (ASL_DEBUG_OUTPUT,
378 "DtInsertLabelField: Found Label : %s at output table offset %X\n",
379 Field->Value, Field->TableOffset);
381 Field->NextLabel = Gbl_LabelList;
382 Gbl_LabelList = Field;
386 /******************************************************************************
388 * FUNCTION: DtLookupLabel
390 * PARAMETERS: Name - Label to be resolved
392 * RETURN: Field object associated with the label
394 * DESCRIPTION: Lookup a label in the global label list. Used during the
395 * resolution of integer expressions.
397 *****************************************************************************/
399 static DT_FIELD *
400 DtLookupLabel (
401 char *Name)
403 DT_FIELD *LabelField;
406 /* Skip a leading $ */
408 if (*Name == '$')
410 Name++;
413 /* Search global list */
415 LabelField = Gbl_LabelList;
416 while (LabelField)
418 if (!ACPI_STRCMP (Name, LabelField->Value))
420 return (LabelField);
422 LabelField = LabelField->NextLabel;
425 return (NULL);