1 /******************************************************************************
3 * Module Name: dtexpress.c - Support for integer expressions and labels
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, 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 #define __DTEXPRESS_C__
46 #include <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include <contrib/dev/acpica/compiler/dtcompiler.h>
48 #include "dtparser.y.h"
50 #define _COMPONENT DT_COMPILER
51 ACPI_MODULE_NAME ("dtexpress")
54 /* Local prototypes */
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 *****************************************************************************/
84 DtResolveIntegerExpression (
91 DbgPrint (ASL_DEBUG_OUTPUT, "Full Integer expression: %s\n",
94 Gbl_CurrentField = Field;
96 Result = DtEvaluateExpression (Field->Value);
97 *ReturnValue = Result;
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 *****************************************************************************/
125 /* Perform the requested operation */
129 case EXPOP_ONES_COMPLIMENT:
130 Result = ~RightValue;
133 case EXPOP_LOGICAL_NOT:
134 Result = !RightValue;
138 Result = LeftValue * RightValue;
144 DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
145 Gbl_CurrentField, Gbl_CurrentField->Value);
148 Result = LeftValue / RightValue;
154 DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
155 Gbl_CurrentField, Gbl_CurrentField->Value);
158 Result = LeftValue % RightValue;
162 Result = LeftValue + RightValue;
166 Result = LeftValue - RightValue;
169 case EXPOP_SHIFT_RIGHT:
170 Result = LeftValue >> RightValue;
173 case EXPOP_SHIFT_LEFT:
174 Result = LeftValue << RightValue;
178 Result = LeftValue < RightValue;
182 Result = LeftValue > RightValue;
185 case EXPOP_LESS_EQUAL:
186 Result = LeftValue <= RightValue;
189 case EXPOP_GREATER_EQUAL:
190 Result = LeftValue >= RightValue;
194 Result = LeftValue = RightValue;
197 case EXPOP_NOT_EQUAL:
198 Result = LeftValue != RightValue;
202 Result = LeftValue & RightValue;
206 Result = LeftValue ^ RightValue;
210 Result = LeftValue | RightValue;
213 case EXPOP_LOGICAL_AND:
214 Result = LeftValue && RightValue;
217 case EXPOP_LOGICAL_OR:
218 Result = LeftValue || RightValue;
223 /* Unknown operator */
225 DtFatal (ASL_MSG_INVALID_EXPRESSION,
226 Gbl_CurrentField, Gbl_CurrentField->Value);
230 DbgPrint (ASL_DEBUG_OUTPUT,
231 "IntegerEval: %s (%8.8X%8.8X %s %8.8X%8.8X) = %8.8X%8.8X\n",
232 Gbl_CurrentField->Value,
233 ACPI_FORMAT_UINT64 (LeftValue),
234 DtGetOpName (Operator),
235 ACPI_FORMAT_UINT64 (RightValue),
236 ACPI_FORMAT_UINT64 (Result));
242 /******************************************************************************
244 * FUNCTION: DtResolveLabel
246 * PARAMETERS: LabelString - Contains the label
248 * RETURN: Table offset associated with the label
250 * DESCRIPTION: Lookup a lable and return its value.
252 *****************************************************************************/
258 DT_FIELD *LabelField;
261 DbgPrint (ASL_DEBUG_OUTPUT, "Resolve Label: %s\n", LabelString);
263 /* Resolve a label reference to an integer (table offset) */
265 if (*LabelString != '$')
270 LabelField = DtLookupLabel (LabelString);
273 DtError (ASL_ERROR, ASL_MSG_UNKNOWN_LABEL,
274 Gbl_CurrentField, LabelString);
278 /* All we need from the label is the offset in the table */
280 DbgPrint (ASL_DEBUG_OUTPUT, "Resolved Label: 0x%8.8X\n",
281 LabelField->TableOffset);
283 return (LabelField->TableOffset);
287 /******************************************************************************
289 * FUNCTION: DtDetectAllLabels
291 * PARAMETERS: FieldList - Field object at start of generic list
295 * DESCRIPTION: Detect all labels in a list of "generic" opcodes (such as
296 * a UEFI table.) and insert them into the global label list.
298 *****************************************************************************/
304 ACPI_DMTABLE_INFO *Info;
305 DT_FIELD *GenericField;
309 TableOffset = Gbl_CurrentTableOffset;
310 GenericField = FieldList;
313 * Process all "Label:" fields within the parse tree. We need
314 * to know the offsets for all labels before we can compile
315 * the parse tree in order to handle forward references. Traverse
316 * tree and get/set all field lengths of all operators in order to
317 * determine the label offsets.
321 Info = DtGetGenericTableInfo (GenericField->Name);
324 /* Maintain table offsets */
326 GenericField->TableOffset = TableOffset;
327 TableOffset += DtGetFieldLength (GenericField, Info);
329 /* Insert all labels in the global label list */
331 if (Info->Opcode == ACPI_DMT_LABEL)
333 DtInsertLabelField (GenericField);
337 GenericField = GenericField->Next;
342 /******************************************************************************
344 * FUNCTION: DtInsertLabelField
346 * PARAMETERS: Field - Field object with Label to be inserted
350 * DESCRIPTION: Insert a label field into the global label list
352 *****************************************************************************/
359 DbgPrint (ASL_DEBUG_OUTPUT,
360 "DtInsertLabelField: Found Label : %s at output table offset %X\n",
361 Field->Value, Field->TableOffset);
363 Field->NextLabel = Gbl_LabelList;
364 Gbl_LabelList = Field;
368 /******************************************************************************
370 * FUNCTION: DtLookupLabel
372 * PARAMETERS: Name - Label to be resolved
374 * RETURN: Field object associated with the label
376 * DESCRIPTION: Lookup a label in the global label list. Used during the
377 * resolution of integer expressions.
379 *****************************************************************************/
385 DT_FIELD *LabelField;
388 /* Skip a leading $ */
395 /* Search global list */
397 LabelField = Gbl_LabelList;
400 if (!ACPI_STRCMP (Name, LabelField->Value))
404 LabelField = LabelField->NextLabel;