1 /******************************************************************************
3 * Module Name: dtexpress.c - Support for integer expressions and labels
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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 "aslcompiler.h"
47 #include "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
:
131 Result
= ~RightValue
;
134 case EXPOP_LOGICAL_NOT
:
136 Result
= !RightValue
;
141 Result
= LeftValue
* RightValue
;
148 DtError (ASL_ERROR
, ASL_MSG_DIVIDE_BY_ZERO
,
149 Gbl_CurrentField
, NULL
);
152 Result
= LeftValue
/ RightValue
;
159 DtError (ASL_ERROR
, ASL_MSG_DIVIDE_BY_ZERO
,
160 Gbl_CurrentField
, NULL
);
163 Result
= LeftValue
% RightValue
;
167 Result
= LeftValue
+ RightValue
;
172 Result
= LeftValue
- RightValue
;
175 case EXPOP_SHIFT_RIGHT
:
177 Result
= LeftValue
>> RightValue
;
180 case EXPOP_SHIFT_LEFT
:
182 Result
= LeftValue
<< RightValue
;
187 Result
= LeftValue
< RightValue
;
192 Result
= LeftValue
> RightValue
;
195 case EXPOP_LESS_EQUAL
:
197 Result
= LeftValue
<= RightValue
;
200 case EXPOP_GREATER_EQUAL
:
202 Result
= LeftValue
>= RightValue
;
207 Result
= LeftValue
== RightValue
;
210 case EXPOP_NOT_EQUAL
:
212 Result
= LeftValue
!= RightValue
;
217 Result
= LeftValue
& RightValue
;
222 Result
= LeftValue
^ RightValue
;
227 Result
= LeftValue
| RightValue
;
230 case EXPOP_LOGICAL_AND
:
232 Result
= LeftValue
&& RightValue
;
235 case EXPOP_LOGICAL_OR
:
237 Result
= LeftValue
|| RightValue
;
242 /* Unknown operator */
244 DtFatal (ASL_MSG_INVALID_EXPRESSION
,
245 Gbl_CurrentField
, NULL
);
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
));
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 *****************************************************************************/
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
!= '$')
288 LabelField
= DtLookupLabel (LabelString
);
291 DtError (ASL_ERROR
, ASL_MSG_UNKNOWN_LABEL
,
292 Gbl_CurrentField
, LabelString
);
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
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 *****************************************************************************/
322 ACPI_DMTABLE_INFO
*Info
;
323 DT_FIELD
*GenericField
;
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.
339 Info
= DtGetGenericTableInfo (GenericField
->Name
);
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
368 * DESCRIPTION: Insert a label field into the global label list
370 *****************************************************************************/
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 *****************************************************************************/
403 DT_FIELD
*LabelField
;
406 /* Skip a leading $ */
413 /* Search global list */
415 LabelField
= Gbl_LabelList
;
418 if (!ACPI_STRCMP (Name
, LabelField
->Value
))
422 LabelField
= LabelField
->NextLabel
;