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>
49 #define _COMPONENT DT_COMPILER
50 ACPI_MODULE_NAME ("dtexpress")
53 /* Local prototypes */
69 /******************************************************************************
71 * FUNCTION: DtResolveIntegerExpression
73 * PARAMETERS: Field - Field object with Integer expression
75 * RETURN: A 64-bit integer value
77 * DESCRIPTION: Resolve an integer expression to a single value. Supports
78 * both integer constants and labels. Supported operators are:
81 *****************************************************************************/
84 DtResolveIntegerExpression (
93 DbgPrint (ASL_DEBUG_OUTPUT, "Full Integer expression: %s\n",
96 strcpy (MsgBuffer, Field->Value); /* Must take a copy for strtok() */
98 /* Obtain and resolve the first operand */
100 IntegerString = strtok (MsgBuffer, " ");
103 DtError (ASL_ERROR, ASL_MSG_INVALID_EXPRESSION, Field, Field->Value);
107 Value = DtResolveInteger (Field, IntegerString);
108 DbgPrint (ASL_DEBUG_OUTPUT, "Integer resolved to V1: %8.8X%8.8X\n",
109 ACPI_FORMAT_UINT64 (Value));
112 * Consume the entire expression string. For the rest of the
113 * expression string, values are of the form:
114 * <operator> <integer>
118 Operator = strtok (NULL, " ");
123 DbgPrint (ASL_DEBUG_OUTPUT, "Expression Resolved to: %8.8X%8.8X\n",
124 ACPI_FORMAT_UINT64 (Value));
129 IntegerString = strtok (NULL, " ");
130 if (!IntegerString ||
131 (strlen (Operator) > 1))
133 /* No corresponding operand for operator or invalid operator */
135 DtError (ASL_ERROR, ASL_MSG_INVALID_EXPRESSION, Field, Field->Value);
139 Value2 = DtResolveInteger (Field, IntegerString);
140 DbgPrint (ASL_DEBUG_OUTPUT, "Integer resolved to V2: %8.8X%8.8X\n",
141 ACPI_FORMAT_UINT64 (Value2));
143 /* Perform the requested operation */
174 DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO, Field, Field->Value);
183 DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO, Field, Field->Value);
191 /* Unknown operator */
193 DtFatal (ASL_MSG_INVALID_EXPRESSION, Field, Field->Value);
202 /******************************************************************************
204 * FUNCTION: DtResolveInteger
206 * PARAMETERS: Field - Field object with string to be resolved
207 * IntegerString - Integer to be resolved
209 * RETURN: A 64-bit integer value
211 * DESCRIPTION: Resolve a single integer string to a value. Supports both
212 * integer constants and labels.
214 * NOTE: References to labels must begin with a dollar sign ($)
216 *****************************************************************************/
223 DT_FIELD *LabelField;
225 char *Message = NULL;
229 DbgPrint (ASL_DEBUG_OUTPUT, "Resolve Integer: %s\n", IntegerString);
231 /* Resolve a label reference to an integer (table offset) */
233 if (*IntegerString == '$')
235 LabelField = DtLookupLabel (IntegerString);
238 DtError (ASL_ERROR, ASL_MSG_UNKNOWN_LABEL, Field, IntegerString);
242 /* All we need from the label is the offset in the table */
244 Value = LabelField->TableOffset;
248 /* Convert string to an actual integer */
250 Status = DtStrtoul64 (IntegerString, &Value);
251 if (ACPI_FAILURE (Status))
253 if (Status == AE_LIMIT)
255 Message = "Constant larger than 64 bits";
257 else if (Status == AE_BAD_CHARACTER)
259 Message = "Invalid character in constant";
262 DtError (ASL_ERROR, ASL_MSG_INVALID_HEX_INTEGER, Field, Message);
269 /******************************************************************************
271 * FUNCTION: DtDetectAllLabels
273 * PARAMETERS: FieldList - Field object at start of generic list
277 * DESCRIPTION: Detect all labels in a list of "generic" opcodes (such as
278 * a UEFI table.) and insert them into the global label list.
280 *****************************************************************************/
286 ACPI_DMTABLE_INFO *Info;
287 DT_FIELD *GenericField;
291 TableOffset = Gbl_CurrentTableOffset;
292 GenericField = FieldList;
295 * Process all "Label:" fields within the parse tree. We need
296 * to know the offsets for all labels before we can compile
297 * the parse tree in order to handle forward references. Traverse
298 * tree and get/set all field lengths of all operators in order to
299 * determine the label offsets.
303 Info = DtGetGenericTableInfo (GenericField->Name);
306 /* Maintain table offsets */
308 GenericField->TableOffset = TableOffset;
309 TableOffset += DtGetFieldLength (GenericField, Info);
311 /* Insert all labels in the global label list */
313 if (Info->Opcode == ACPI_DMT_LABEL)
315 DtInsertLabelField (GenericField);
319 GenericField = GenericField->Next;
324 /******************************************************************************
326 * FUNCTION: DtInsertLabelField
328 * PARAMETERS: Field - Field object with Label to be inserted
332 * DESCRIPTION: Insert a label field into the global label list
334 *****************************************************************************/
341 DbgPrint (ASL_DEBUG_OUTPUT,
342 "DtInsertLabelField: Found Label : %s at output table offset %X\n",
343 Field->Value, Field->TableOffset);
345 Field->NextLabel = Gbl_LabelList;
346 Gbl_LabelList = Field;
350 /******************************************************************************
352 * FUNCTION: DtLookupLabel
354 * PARAMETERS: Name - Label to be resolved
356 * RETURN: Field object associated with the label
358 * DESCRIPTION: Lookup a label in the global label list. Used during the
359 * resolution of integer expressions.
361 *****************************************************************************/
367 DT_FIELD *LabelField;
370 /* Skip a leading $ */
377 /* Search global list */
379 LabelField = Gbl_LabelList;
382 if (!ACPI_STRCMP (Name, LabelField->Value))
386 LabelField = LabelField->NextLabel;