freebsd-dev/sys/contrib/dev/acpica/compiler/dtexpress.c

425 lines
11 KiB
C
Raw Normal View History

2011-02-11 22:56:14 +00:00
/******************************************************************************
*
* Module Name: dtexpress.c - Support for integer expressions and labels
*
*****************************************************************************/
/*
2015-02-18 20:33:00 +00:00
* Copyright (C) 2000 - 2015, Intel Corp.
2011-02-11 22:56:14 +00:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* substantially similar to the "NO WARRANTY" disclaimer below
* ("Disclaimer") and any redistribution must be conditioned upon
* including a substantially similar Disclaimer requirement for further
* binary redistribution.
* 3. Neither the names of the above-listed copyright holders nor the names
* of any contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*/
2011-02-12 01:03:15 +00:00
#include <contrib/dev/acpica/compiler/aslcompiler.h>
#include <contrib/dev/acpica/compiler/dtcompiler.h>
2011-04-13 18:18:52 +00:00
#include "dtparser.y.h"
2011-02-11 22:56:14 +00:00
#define _COMPONENT DT_COMPILER
ACPI_MODULE_NAME ("dtexpress")
/* Local prototypes */
static void
DtInsertLabelField (
DT_FIELD *Field);
static DT_FIELD *
DtLookupLabel (
char *Name);
2011-04-13 18:18:52 +00:00
/* Global used for errors during parse and related functions */
DT_FIELD *Gbl_CurrentField;
2011-02-11 22:56:14 +00:00
/******************************************************************************
*
* FUNCTION: DtResolveIntegerExpression
*
* PARAMETERS: Field - Field object with Integer expression
2011-04-13 18:18:52 +00:00
* ReturnValue - Where the integer is returned
2011-02-11 22:56:14 +00:00
*
2011-04-13 18:18:52 +00:00
* RETURN: Status, and the resolved 64-bit integer value
2011-02-11 22:56:14 +00:00
*
* DESCRIPTION: Resolve an integer expression to a single value. Supports
2011-04-13 18:18:52 +00:00
* both integer constants and labels.
2011-02-11 22:56:14 +00:00
*
*****************************************************************************/
2011-04-13 18:18:52 +00:00
ACPI_STATUS
2011-02-11 22:56:14 +00:00
DtResolveIntegerExpression (
2011-04-13 18:18:52 +00:00
DT_FIELD *Field,
UINT64 *ReturnValue)
2011-02-11 22:56:14 +00:00
{
2011-04-13 18:18:52 +00:00
UINT64 Result;
2011-02-11 22:56:14 +00:00
DbgPrint (ASL_DEBUG_OUTPUT, "Full Integer expression: %s\n",
Field->Value);
2011-04-13 18:18:52 +00:00
Gbl_CurrentField = Field;
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
Result = DtEvaluateExpression (Field->Value);
*ReturnValue = Result;
return (AE_OK);
}
/******************************************************************************
*
* FUNCTION: DtDoOperator
*
* PARAMETERS: LeftValue - First 64-bit operand
* Operator - Parse token for the operator (EXPOP_*)
* RightValue - Second 64-bit operand
*
* RETURN: 64-bit result of the requested operation
*
* DESCRIPTION: Perform the various 64-bit integer math functions
*
*****************************************************************************/
UINT64
DtDoOperator (
UINT64 LeftValue,
UINT32 Operator,
UINT64 RightValue)
{
UINT64 Result;
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
/* Perform the requested operation */
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
switch (Operator)
2011-02-11 22:56:14 +00:00
{
2011-04-13 18:18:52 +00:00
case EXPOP_ONES_COMPLIMENT:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = ~RightValue;
break;
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
case EXPOP_LOGICAL_NOT:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = !RightValue;
break;
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
case EXPOP_MULTIPLY:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue * RightValue;
break;
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
case EXPOP_DIVIDE:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
if (!RightValue)
2011-02-11 22:56:14 +00:00
{
2011-04-13 18:18:52 +00:00
DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
2012-03-20 18:17:33 +00:00
Gbl_CurrentField, NULL);
2011-04-13 18:18:52 +00:00
return (0);
}
Result = LeftValue / RightValue;
break;
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
case EXPOP_MODULO:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
if (!RightValue)
{
DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
2012-03-20 18:17:33 +00:00
Gbl_CurrentField, NULL);
2011-02-11 22:56:14 +00:00
return (0);
}
2011-04-13 18:18:52 +00:00
Result = LeftValue % RightValue;
break;
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
case EXPOP_ADD:
Result = LeftValue + RightValue;
break;
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
case EXPOP_SUBTRACT:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue - RightValue;
break;
2011-02-11 22:56:14 +00:00
2011-04-13 18:18:52 +00:00
case EXPOP_SHIFT_RIGHT:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue >> RightValue;
break;
case EXPOP_SHIFT_LEFT:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue << RightValue;
break;
case EXPOP_LESS:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue < RightValue;
break;
case EXPOP_GREATER:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue > RightValue;
break;
case EXPOP_LESS_EQUAL:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue <= RightValue;
break;
case EXPOP_GREATER_EQUAL:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue >= RightValue;
break;
case EXPOP_EQUAL:
2013-05-17 23:13:40 +00:00
2012-03-20 18:17:33 +00:00
Result = LeftValue == RightValue;
2011-04-13 18:18:52 +00:00
break;
case EXPOP_NOT_EQUAL:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue != RightValue;
break;
case EXPOP_AND:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue & RightValue;
break;
case EXPOP_XOR:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue ^ RightValue;
break;
case EXPOP_OR:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue | RightValue;
break;
case EXPOP_LOGICAL_AND:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue && RightValue;
break;
case EXPOP_LOGICAL_OR:
2013-05-17 23:13:40 +00:00
2011-04-13 18:18:52 +00:00
Result = LeftValue || RightValue;
break;
default:
/* Unknown operator */
DtFatal (ASL_MSG_INVALID_EXPRESSION,
2012-03-20 18:17:33 +00:00
Gbl_CurrentField, NULL);
2011-04-13 18:18:52 +00:00
return (0);
2011-02-11 22:56:14 +00:00
}
2011-04-13 18:18:52 +00:00
DbgPrint (ASL_DEBUG_OUTPUT,
2012-03-20 18:17:33 +00:00
"IntegerEval: (%8.8X%8.8X %s %8.8X%8.8X) = %8.8X%8.8X\n",
2011-04-13 18:18:52 +00:00
ACPI_FORMAT_UINT64 (LeftValue),
DtGetOpName (Operator),
ACPI_FORMAT_UINT64 (RightValue),
ACPI_FORMAT_UINT64 (Result));
return (Result);
2011-02-11 22:56:14 +00:00
}
/******************************************************************************
*
2011-04-13 18:18:52 +00:00
* FUNCTION: DtResolveLabel
2011-02-11 22:56:14 +00:00
*
2011-04-13 18:18:52 +00:00
* PARAMETERS: LabelString - Contains the label
2011-02-11 22:56:14 +00:00
*
2011-04-13 18:18:52 +00:00
* RETURN: Table offset associated with the label
2011-02-11 22:56:14 +00:00
*
2011-04-13 18:18:52 +00:00
* DESCRIPTION: Lookup a lable and return its value.
2011-02-11 22:56:14 +00:00
*
*****************************************************************************/
2011-04-13 18:18:52 +00:00
UINT64
DtResolveLabel (
char *LabelString)
2011-02-11 22:56:14 +00:00
{
DT_FIELD *LabelField;
2011-04-13 18:18:52 +00:00
DbgPrint (ASL_DEBUG_OUTPUT, "Resolve Label: %s\n", LabelString);
2011-02-11 22:56:14 +00:00
/* Resolve a label reference to an integer (table offset) */
2011-04-13 18:18:52 +00:00
if (*LabelString != '$')
2011-02-11 22:56:14 +00:00
{
2011-04-13 18:18:52 +00:00
return (0);
2011-02-11 22:56:14 +00:00
}
2011-04-13 18:18:52 +00:00
LabelField = DtLookupLabel (LabelString);
if (!LabelField)
2011-02-11 22:56:14 +00:00
{
2011-04-13 18:18:52 +00:00
DtError (ASL_ERROR, ASL_MSG_UNKNOWN_LABEL,
Gbl_CurrentField, LabelString);
return (0);
2011-02-11 22:56:14 +00:00
}
2011-04-13 18:18:52 +00:00
/* All we need from the label is the offset in the table */
DbgPrint (ASL_DEBUG_OUTPUT, "Resolved Label: 0x%8.8X\n",
LabelField->TableOffset);
return (LabelField->TableOffset);
2011-02-11 22:56:14 +00:00
}
/******************************************************************************
*
* FUNCTION: DtDetectAllLabels
*
* PARAMETERS: FieldList - Field object at start of generic list
*
* RETURN: None
*
* DESCRIPTION: Detect all labels in a list of "generic" opcodes (such as
* a UEFI table.) and insert them into the global label list.
*
*****************************************************************************/
void
DtDetectAllLabels (
DT_FIELD *FieldList)
{
ACPI_DMTABLE_INFO *Info;
DT_FIELD *GenericField;
UINT32 TableOffset;
TableOffset = Gbl_CurrentTableOffset;
GenericField = FieldList;
/*
* Process all "Label:" fields within the parse tree. We need
* to know the offsets for all labels before we can compile
* the parse tree in order to handle forward references. Traverse
* tree and get/set all field lengths of all operators in order to
* determine the label offsets.
*/
while (GenericField)
{
Info = DtGetGenericTableInfo (GenericField->Name);
if (Info)
{
/* Maintain table offsets */
GenericField->TableOffset = TableOffset;
TableOffset += DtGetFieldLength (GenericField, Info);
/* Insert all labels in the global label list */
if (Info->Opcode == ACPI_DMT_LABEL)
{
DtInsertLabelField (GenericField);
}
}
GenericField = GenericField->Next;
}
}
/******************************************************************************
*
* FUNCTION: DtInsertLabelField
*
* PARAMETERS: Field - Field object with Label to be inserted
*
* RETURN: None
*
* DESCRIPTION: Insert a label field into the global label list
*
*****************************************************************************/
static void
DtInsertLabelField (
DT_FIELD *Field)
{
DbgPrint (ASL_DEBUG_OUTPUT,
"DtInsertLabelField: Found Label : %s at output table offset %X\n",
Field->Value, Field->TableOffset);
Field->NextLabel = Gbl_LabelList;
Gbl_LabelList = Field;
}
/******************************************************************************
*
* FUNCTION: DtLookupLabel
*
* PARAMETERS: Name - Label to be resolved
*
* RETURN: Field object associated with the label
*
* DESCRIPTION: Lookup a label in the global label list. Used during the
* resolution of integer expressions.
*
*****************************************************************************/
static DT_FIELD *
DtLookupLabel (
char *Name)
{
DT_FIELD *LabelField;
/* Skip a leading $ */
if (*Name == '$')
{
Name++;
}
/* Search global list */
LabelField = Gbl_LabelList;
while (LabelField)
{
2015-06-16 19:48:16 +00:00
if (!strcmp (Name, LabelField->Value))
2011-02-11 22:56:14 +00:00
{
return (LabelField);
}
LabelField = LabelField->NextLabel;
}
return (NULL);
}