1 /******************************************************************************
3 * Module Name: dtexpress.c - Support for integer expressions and labels
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/compiler/dtcompiler.h>
46 #include "dtparser.y.h"
48 #define _COMPONENT DT_COMPILER
49 ACPI_MODULE_NAME ("dtexpress")
52 /* Local prototypes */
62 /* Global used for errors during parse and related functions */
64 DT_FIELD *Gbl_CurrentField;
67 /******************************************************************************
69 * FUNCTION: DtResolveIntegerExpression
71 * PARAMETERS: Field - Field object with Integer expression
72 * ReturnValue - Where the integer is returned
74 * RETURN: Status, and the resolved 64-bit integer value
76 * DESCRIPTION: Resolve an integer expression to a single value. Supports
77 * both integer constants and labels.
79 *****************************************************************************/
82 DtResolveIntegerExpression (
89 DbgPrint (ASL_DEBUG_OUTPUT, "Full Integer expression: %s\n",
92 Gbl_CurrentField = Field;
94 Result = DtEvaluateExpression (Field->Value);
95 *ReturnValue = Result;
100 /******************************************************************************
102 * FUNCTION: DtDoOperator
104 * PARAMETERS: LeftValue - First 64-bit operand
105 * Operator - Parse token for the operator (EXPOP_*)
106 * RightValue - Second 64-bit operand
108 * RETURN: 64-bit result of the requested operation
110 * DESCRIPTION: Perform the various 64-bit integer math functions
112 *****************************************************************************/
123 /* Perform the requested operation */
127 case EXPOP_ONES_COMPLIMENT:
129 Result = ~RightValue;
132 case EXPOP_LOGICAL_NOT:
134 Result = !RightValue;
139 Result = LeftValue * RightValue;
146 DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
147 Gbl_CurrentField, NULL);
150 Result = LeftValue / RightValue;
157 DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
158 Gbl_CurrentField, NULL);
161 Result = LeftValue % RightValue;
165 Result = LeftValue + RightValue;
170 Result = LeftValue - RightValue;
173 case EXPOP_SHIFT_RIGHT:
175 Result = LeftValue >> RightValue;
178 case EXPOP_SHIFT_LEFT:
180 Result = LeftValue << RightValue;
185 Result = LeftValue < RightValue;
190 Result = LeftValue > RightValue;
193 case EXPOP_LESS_EQUAL:
195 Result = LeftValue <= RightValue;
198 case EXPOP_GREATER_EQUAL:
200 Result = LeftValue >= RightValue;
205 Result = LeftValue == RightValue;
208 case EXPOP_NOT_EQUAL:
210 Result = LeftValue != RightValue;
215 Result = LeftValue & RightValue;
220 Result = LeftValue ^ RightValue;
225 Result = LeftValue | RightValue;
228 case EXPOP_LOGICAL_AND:
230 Result = LeftValue && RightValue;
233 case EXPOP_LOGICAL_OR:
235 Result = LeftValue || RightValue;
240 /* Unknown operator */
242 DtFatal (ASL_MSG_INVALID_EXPRESSION,
243 Gbl_CurrentField, NULL);
247 DbgPrint (ASL_DEBUG_OUTPUT,
248 "IntegerEval: (%8.8X%8.8X %s %8.8X%8.8X) = %8.8X%8.8X\n",
249 ACPI_FORMAT_UINT64 (LeftValue),
250 DtGetOpName (Operator),
251 ACPI_FORMAT_UINT64 (RightValue),
252 ACPI_FORMAT_UINT64 (Result));
258 /******************************************************************************
260 * FUNCTION: DtResolveLabel
262 * PARAMETERS: LabelString - Contains the label
264 * RETURN: Table offset associated with the label
266 * DESCRIPTION: Lookup a lable and return its value.
268 *****************************************************************************/
274 DT_FIELD *LabelField;
277 DbgPrint (ASL_DEBUG_OUTPUT, "Resolve Label: %s\n", LabelString);
279 /* Resolve a label reference to an integer (table offset) */
281 if (*LabelString != '$')
286 LabelField = DtLookupLabel (LabelString);
289 DtError (ASL_ERROR, ASL_MSG_UNKNOWN_LABEL,
290 Gbl_CurrentField, LabelString);
294 /* All we need from the label is the offset in the table */
296 DbgPrint (ASL_DEBUG_OUTPUT, "Resolved Label: 0x%8.8X\n",
297 LabelField->TableOffset);
299 return (LabelField->TableOffset);
303 /******************************************************************************
305 * FUNCTION: DtDetectAllLabels
307 * PARAMETERS: FieldList - Field object at start of generic list
311 * DESCRIPTION: Detect all labels in a list of "generic" opcodes (such as
312 * a UEFI table.) and insert them into the global label list.
314 *****************************************************************************/
320 ACPI_DMTABLE_INFO *Info;
321 DT_FIELD *GenericField;
325 TableOffset = Gbl_CurrentTableOffset;
326 GenericField = FieldList;
329 * Process all "Label:" fields within the parse tree. We need
330 * to know the offsets for all labels before we can compile
331 * the parse tree in order to handle forward references. Traverse
332 * tree and get/set all field lengths of all operators in order to
333 * determine the label offsets.
337 Info = DtGetGenericTableInfo (GenericField->Name);
340 /* Maintain table offsets */
342 GenericField->TableOffset = TableOffset;
343 TableOffset += DtGetFieldLength (GenericField, Info);
345 /* Insert all labels in the global label list */
347 if (Info->Opcode == ACPI_DMT_LABEL)
349 DtInsertLabelField (GenericField);
353 GenericField = GenericField->Next;
358 /******************************************************************************
360 * FUNCTION: DtInsertLabelField
362 * PARAMETERS: Field - Field object with Label to be inserted
366 * DESCRIPTION: Insert a label field into the global label list
368 *****************************************************************************/
375 DbgPrint (ASL_DEBUG_OUTPUT,
376 "DtInsertLabelField: Found Label : %s at output table offset %X\n",
377 Field->Value, Field->TableOffset);
379 Field->NextLabel = Gbl_LabelList;
380 Gbl_LabelList = Field;
384 /******************************************************************************
386 * FUNCTION: DtLookupLabel
388 * PARAMETERS: Name - Label to be resolved
390 * RETURN: Field object associated with the label
392 * DESCRIPTION: Lookup a label in the global label list. Used during the
393 * resolution of integer expressions.
395 *****************************************************************************/
401 DT_FIELD *LabelField;
404 /* Skip a leading $ */
411 /* Search global list */
413 LabelField = Gbl_LabelList;
416 if (!ACPI_STRCMP (Name, LabelField->Value))
420 LabelField = LabelField->NextLabel;