Home | History | Annotate | Line # | Download | only in compiler
dtexpress.c revision 1.1.1.2.2.1
      1          1.1  jruoho /******************************************************************************
      2          1.1  jruoho  *
      3          1.1  jruoho  * Module Name: dtexpress.c - Support for integer expressions and labels
      4          1.1  jruoho  *
      5          1.1  jruoho  *****************************************************************************/
      6          1.1  jruoho 
      7          1.1  jruoho /*
      8  1.1.1.2.2.1    yamt  * Copyright (C) 2000 - 2013, Intel Corp.
      9          1.1  jruoho  * All rights reserved.
     10          1.1  jruoho  *
     11          1.1  jruoho  * Redistribution and use in source and binary forms, with or without
     12          1.1  jruoho  * modification, are permitted provided that the following conditions
     13          1.1  jruoho  * are met:
     14          1.1  jruoho  * 1. Redistributions of source code must retain the above copyright
     15          1.1  jruoho  *    notice, this list of conditions, and the following disclaimer,
     16          1.1  jruoho  *    without modification.
     17          1.1  jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18          1.1  jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19          1.1  jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20          1.1  jruoho  *    including a substantially similar Disclaimer requirement for further
     21          1.1  jruoho  *    binary redistribution.
     22          1.1  jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23          1.1  jruoho  *    of any contributors may be used to endorse or promote products derived
     24          1.1  jruoho  *    from this software without specific prior written permission.
     25          1.1  jruoho  *
     26          1.1  jruoho  * Alternatively, this software may be distributed under the terms of the
     27          1.1  jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28          1.1  jruoho  * Software Foundation.
     29          1.1  jruoho  *
     30          1.1  jruoho  * NO WARRANTY
     31          1.1  jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32          1.1  jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33          1.1  jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34          1.1  jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35          1.1  jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36          1.1  jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37          1.1  jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38          1.1  jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39          1.1  jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40          1.1  jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41          1.1  jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42          1.1  jruoho  */
     43          1.1  jruoho 
     44          1.1  jruoho #define __DTEXPRESS_C__
     45          1.1  jruoho 
     46          1.1  jruoho #include "aslcompiler.h"
     47          1.1  jruoho #include "dtcompiler.h"
     48      1.1.1.2  jruoho #include "dtparser.y.h"
     49          1.1  jruoho 
     50          1.1  jruoho #define _COMPONENT          DT_COMPILER
     51          1.1  jruoho         ACPI_MODULE_NAME    ("dtexpress")
     52          1.1  jruoho 
     53          1.1  jruoho 
     54          1.1  jruoho /* Local prototypes */
     55          1.1  jruoho 
     56          1.1  jruoho static void
     57          1.1  jruoho DtInsertLabelField (
     58          1.1  jruoho     DT_FIELD                *Field);
     59          1.1  jruoho 
     60          1.1  jruoho static DT_FIELD *
     61          1.1  jruoho DtLookupLabel (
     62          1.1  jruoho     char                    *Name);
     63          1.1  jruoho 
     64      1.1.1.2  jruoho /* Global used for errors during parse and related functions */
     65      1.1.1.2  jruoho 
     66      1.1.1.2  jruoho DT_FIELD                *Gbl_CurrentField;
     67      1.1.1.2  jruoho 
     68          1.1  jruoho 
     69          1.1  jruoho /******************************************************************************
     70          1.1  jruoho  *
     71          1.1  jruoho  * FUNCTION:    DtResolveIntegerExpression
     72          1.1  jruoho  *
     73          1.1  jruoho  * PARAMETERS:  Field               - Field object with Integer expression
     74      1.1.1.2  jruoho  *              ReturnValue         - Where the integer is returned
     75          1.1  jruoho  *
     76      1.1.1.2  jruoho  * RETURN:      Status, and the resolved 64-bit integer value
     77          1.1  jruoho  *
     78          1.1  jruoho  * DESCRIPTION: Resolve an integer expression to a single value. Supports
     79      1.1.1.2  jruoho  *              both integer constants and labels.
     80          1.1  jruoho  *
     81          1.1  jruoho  *****************************************************************************/
     82          1.1  jruoho 
     83      1.1.1.2  jruoho ACPI_STATUS
     84          1.1  jruoho DtResolveIntegerExpression (
     85      1.1.1.2  jruoho     DT_FIELD                *Field,
     86      1.1.1.2  jruoho     UINT64                  *ReturnValue)
     87          1.1  jruoho {
     88      1.1.1.2  jruoho     UINT64                  Result;
     89          1.1  jruoho 
     90          1.1  jruoho 
     91          1.1  jruoho     DbgPrint (ASL_DEBUG_OUTPUT, "Full Integer expression: %s\n",
     92          1.1  jruoho         Field->Value);
     93          1.1  jruoho 
     94      1.1.1.2  jruoho     Gbl_CurrentField = Field;
     95          1.1  jruoho 
     96      1.1.1.2  jruoho     Result = DtEvaluateExpression (Field->Value);
     97      1.1.1.2  jruoho     *ReturnValue = Result;
     98      1.1.1.2  jruoho     return (AE_OK);
     99      1.1.1.2  jruoho }
    100          1.1  jruoho 
    101          1.1  jruoho 
    102      1.1.1.2  jruoho /******************************************************************************
    103      1.1.1.2  jruoho  *
    104      1.1.1.2  jruoho  * FUNCTION:    DtDoOperator
    105      1.1.1.2  jruoho  *
    106      1.1.1.2  jruoho  * PARAMETERS:  LeftValue           - First 64-bit operand
    107      1.1.1.2  jruoho  *              Operator            - Parse token for the operator (EXPOP_*)
    108      1.1.1.2  jruoho  *              RightValue          - Second 64-bit operand
    109      1.1.1.2  jruoho  *
    110      1.1.1.2  jruoho  * RETURN:      64-bit result of the requested operation
    111      1.1.1.2  jruoho  *
    112      1.1.1.2  jruoho  * DESCRIPTION: Perform the various 64-bit integer math functions
    113      1.1.1.2  jruoho  *
    114      1.1.1.2  jruoho  *****************************************************************************/
    115      1.1.1.2  jruoho 
    116      1.1.1.2  jruoho UINT64
    117      1.1.1.2  jruoho DtDoOperator (
    118      1.1.1.2  jruoho     UINT64                  LeftValue,
    119      1.1.1.2  jruoho     UINT32                  Operator,
    120      1.1.1.2  jruoho     UINT64                  RightValue)
    121      1.1.1.2  jruoho {
    122      1.1.1.2  jruoho     UINT64                  Result;
    123          1.1  jruoho 
    124          1.1  jruoho 
    125      1.1.1.2  jruoho     /* Perform the requested operation */
    126          1.1  jruoho 
    127      1.1.1.2  jruoho     switch (Operator)
    128      1.1.1.2  jruoho     {
    129      1.1.1.2  jruoho     case EXPOP_ONES_COMPLIMENT:
    130  1.1.1.2.2.1    yamt 
    131      1.1.1.2  jruoho         Result = ~RightValue;
    132      1.1.1.2  jruoho         break;
    133      1.1.1.2  jruoho 
    134      1.1.1.2  jruoho     case EXPOP_LOGICAL_NOT:
    135  1.1.1.2.2.1    yamt 
    136      1.1.1.2  jruoho         Result = !RightValue;
    137      1.1.1.2  jruoho         break;
    138      1.1.1.2  jruoho 
    139      1.1.1.2  jruoho     case EXPOP_MULTIPLY:
    140  1.1.1.2.2.1    yamt 
    141      1.1.1.2  jruoho         Result = LeftValue * RightValue;
    142      1.1.1.2  jruoho         break;
    143          1.1  jruoho 
    144      1.1.1.2  jruoho     case EXPOP_DIVIDE:
    145  1.1.1.2.2.1    yamt 
    146      1.1.1.2  jruoho         if (!RightValue)
    147          1.1  jruoho         {
    148      1.1.1.2  jruoho             DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
    149  1.1.1.2.2.1    yamt                 Gbl_CurrentField, NULL);
    150      1.1.1.2  jruoho             return (0);
    151      1.1.1.2  jruoho         }
    152      1.1.1.2  jruoho         Result = LeftValue / RightValue;
    153      1.1.1.2  jruoho         break;
    154          1.1  jruoho 
    155      1.1.1.2  jruoho     case EXPOP_MODULO:
    156  1.1.1.2.2.1    yamt 
    157      1.1.1.2  jruoho         if (!RightValue)
    158      1.1.1.2  jruoho         {
    159      1.1.1.2  jruoho             DtError (ASL_ERROR, ASL_MSG_DIVIDE_BY_ZERO,
    160  1.1.1.2.2.1    yamt                 Gbl_CurrentField, NULL);
    161          1.1  jruoho             return (0);
    162          1.1  jruoho         }
    163      1.1.1.2  jruoho         Result = LeftValue % RightValue;
    164      1.1.1.2  jruoho         break;
    165          1.1  jruoho 
    166      1.1.1.2  jruoho     case EXPOP_ADD:
    167      1.1.1.2  jruoho         Result = LeftValue + RightValue;
    168      1.1.1.2  jruoho         break;
    169          1.1  jruoho 
    170      1.1.1.2  jruoho     case EXPOP_SUBTRACT:
    171  1.1.1.2.2.1    yamt 
    172      1.1.1.2  jruoho         Result = LeftValue - RightValue;
    173      1.1.1.2  jruoho         break;
    174          1.1  jruoho 
    175      1.1.1.2  jruoho     case EXPOP_SHIFT_RIGHT:
    176  1.1.1.2.2.1    yamt 
    177      1.1.1.2  jruoho         Result = LeftValue >> RightValue;
    178      1.1.1.2  jruoho         break;
    179          1.1  jruoho 
    180      1.1.1.2  jruoho     case EXPOP_SHIFT_LEFT:
    181  1.1.1.2.2.1    yamt 
    182      1.1.1.2  jruoho         Result = LeftValue << RightValue;
    183      1.1.1.2  jruoho         break;
    184          1.1  jruoho 
    185      1.1.1.2  jruoho     case EXPOP_LESS:
    186  1.1.1.2.2.1    yamt 
    187      1.1.1.2  jruoho         Result = LeftValue < RightValue;
    188      1.1.1.2  jruoho         break;
    189          1.1  jruoho 
    190      1.1.1.2  jruoho     case EXPOP_GREATER:
    191  1.1.1.2.2.1    yamt 
    192      1.1.1.2  jruoho         Result = LeftValue > RightValue;
    193      1.1.1.2  jruoho         break;
    194          1.1  jruoho 
    195      1.1.1.2  jruoho     case EXPOP_LESS_EQUAL:
    196  1.1.1.2.2.1    yamt 
    197      1.1.1.2  jruoho         Result = LeftValue <= RightValue;
    198      1.1.1.2  jruoho         break;
    199          1.1  jruoho 
    200      1.1.1.2  jruoho     case EXPOP_GREATER_EQUAL:
    201  1.1.1.2.2.1    yamt 
    202      1.1.1.2  jruoho         Result = LeftValue >= RightValue;
    203      1.1.1.2  jruoho         break;
    204      1.1.1.2  jruoho 
    205      1.1.1.2  jruoho     case EXPOP_EQUAL:
    206  1.1.1.2.2.1    yamt 
    207  1.1.1.2.2.1    yamt         Result = LeftValue == RightValue;
    208      1.1.1.2  jruoho         break;
    209      1.1.1.2  jruoho 
    210      1.1.1.2  jruoho     case EXPOP_NOT_EQUAL:
    211  1.1.1.2.2.1    yamt 
    212      1.1.1.2  jruoho         Result = LeftValue != RightValue;
    213      1.1.1.2  jruoho         break;
    214      1.1.1.2  jruoho 
    215      1.1.1.2  jruoho     case EXPOP_AND:
    216  1.1.1.2.2.1    yamt 
    217      1.1.1.2  jruoho         Result = LeftValue & RightValue;
    218      1.1.1.2  jruoho         break;
    219      1.1.1.2  jruoho 
    220      1.1.1.2  jruoho     case EXPOP_XOR:
    221  1.1.1.2.2.1    yamt 
    222      1.1.1.2  jruoho         Result = LeftValue ^ RightValue;
    223      1.1.1.2  jruoho         break;
    224      1.1.1.2  jruoho 
    225      1.1.1.2  jruoho     case EXPOP_OR:
    226  1.1.1.2.2.1    yamt 
    227      1.1.1.2  jruoho         Result = LeftValue | RightValue;
    228      1.1.1.2  jruoho         break;
    229      1.1.1.2  jruoho 
    230      1.1.1.2  jruoho     case EXPOP_LOGICAL_AND:
    231  1.1.1.2.2.1    yamt 
    232      1.1.1.2  jruoho         Result = LeftValue && RightValue;
    233      1.1.1.2  jruoho         break;
    234      1.1.1.2  jruoho 
    235      1.1.1.2  jruoho     case EXPOP_LOGICAL_OR:
    236  1.1.1.2.2.1    yamt 
    237      1.1.1.2  jruoho         Result = LeftValue || RightValue;
    238      1.1.1.2  jruoho         break;
    239      1.1.1.2  jruoho 
    240      1.1.1.2  jruoho    default:
    241      1.1.1.2  jruoho 
    242      1.1.1.2  jruoho         /* Unknown operator */
    243      1.1.1.2  jruoho 
    244      1.1.1.2  jruoho         DtFatal (ASL_MSG_INVALID_EXPRESSION,
    245  1.1.1.2.2.1    yamt             Gbl_CurrentField, NULL);
    246      1.1.1.2  jruoho         return (0);
    247          1.1  jruoho     }
    248          1.1  jruoho 
    249      1.1.1.2  jruoho     DbgPrint (ASL_DEBUG_OUTPUT,
    250  1.1.1.2.2.1    yamt         "IntegerEval: (%8.8X%8.8X %s %8.8X%8.8X) = %8.8X%8.8X\n",
    251      1.1.1.2  jruoho         ACPI_FORMAT_UINT64 (LeftValue),
    252      1.1.1.2  jruoho         DtGetOpName (Operator),
    253      1.1.1.2  jruoho         ACPI_FORMAT_UINT64 (RightValue),
    254      1.1.1.2  jruoho         ACPI_FORMAT_UINT64 (Result));
    255      1.1.1.2  jruoho 
    256      1.1.1.2  jruoho     return (Result);
    257          1.1  jruoho }
    258          1.1  jruoho 
    259          1.1  jruoho 
    260          1.1  jruoho /******************************************************************************
    261          1.1  jruoho  *
    262      1.1.1.2  jruoho  * FUNCTION:    DtResolveLabel
    263          1.1  jruoho  *
    264      1.1.1.2  jruoho  * PARAMETERS:  LabelString         - Contains the label
    265          1.1  jruoho  *
    266      1.1.1.2  jruoho  * RETURN:      Table offset associated with the label
    267          1.1  jruoho  *
    268      1.1.1.2  jruoho  * DESCRIPTION: Lookup a lable and return its value.
    269          1.1  jruoho  *
    270          1.1  jruoho  *****************************************************************************/
    271          1.1  jruoho 
    272      1.1.1.2  jruoho UINT64
    273      1.1.1.2  jruoho DtResolveLabel (
    274      1.1.1.2  jruoho     char                    *LabelString)
    275          1.1  jruoho {
    276          1.1  jruoho     DT_FIELD                *LabelField;
    277          1.1  jruoho 
    278          1.1  jruoho 
    279      1.1.1.2  jruoho     DbgPrint (ASL_DEBUG_OUTPUT, "Resolve Label: %s\n", LabelString);
    280          1.1  jruoho 
    281          1.1  jruoho     /* Resolve a label reference to an integer (table offset) */
    282          1.1  jruoho 
    283      1.1.1.2  jruoho     if (*LabelString != '$')
    284          1.1  jruoho     {
    285      1.1.1.2  jruoho         return (0);
    286          1.1  jruoho     }
    287          1.1  jruoho 
    288      1.1.1.2  jruoho     LabelField = DtLookupLabel (LabelString);
    289      1.1.1.2  jruoho     if (!LabelField)
    290          1.1  jruoho     {
    291      1.1.1.2  jruoho         DtError (ASL_ERROR, ASL_MSG_UNKNOWN_LABEL,
    292      1.1.1.2  jruoho             Gbl_CurrentField, LabelString);
    293      1.1.1.2  jruoho         return (0);
    294          1.1  jruoho     }
    295          1.1  jruoho 
    296      1.1.1.2  jruoho     /* All we need from the label is the offset in the table */
    297      1.1.1.2  jruoho 
    298      1.1.1.2  jruoho     DbgPrint (ASL_DEBUG_OUTPUT, "Resolved Label: 0x%8.8X\n",
    299      1.1.1.2  jruoho         LabelField->TableOffset);
    300      1.1.1.2  jruoho 
    301      1.1.1.2  jruoho     return (LabelField->TableOffset);
    302          1.1  jruoho }
    303          1.1  jruoho 
    304          1.1  jruoho 
    305          1.1  jruoho /******************************************************************************
    306          1.1  jruoho  *
    307          1.1  jruoho  * FUNCTION:    DtDetectAllLabels
    308          1.1  jruoho  *
    309          1.1  jruoho  * PARAMETERS:  FieldList           - Field object at start of generic list
    310          1.1  jruoho  *
    311          1.1  jruoho  * RETURN:      None
    312          1.1  jruoho  *
    313          1.1  jruoho  * DESCRIPTION: Detect all labels in a list of "generic" opcodes (such as
    314          1.1  jruoho  *              a UEFI table.) and insert them into the global label list.
    315          1.1  jruoho  *
    316          1.1  jruoho  *****************************************************************************/
    317          1.1  jruoho 
    318          1.1  jruoho void
    319          1.1  jruoho DtDetectAllLabels (
    320          1.1  jruoho     DT_FIELD                *FieldList)
    321          1.1  jruoho {
    322          1.1  jruoho     ACPI_DMTABLE_INFO       *Info;
    323          1.1  jruoho     DT_FIELD                *GenericField;
    324          1.1  jruoho     UINT32                  TableOffset;
    325          1.1  jruoho 
    326          1.1  jruoho 
    327          1.1  jruoho     TableOffset = Gbl_CurrentTableOffset;
    328          1.1  jruoho     GenericField = FieldList;
    329          1.1  jruoho 
    330          1.1  jruoho     /*
    331          1.1  jruoho      * Process all "Label:" fields within the parse tree. We need
    332          1.1  jruoho      * to know the offsets for all labels before we can compile
    333          1.1  jruoho      * the parse tree in order to handle forward references. Traverse
    334          1.1  jruoho      * tree and get/set all field lengths of all operators in order to
    335          1.1  jruoho      * determine the label offsets.
    336          1.1  jruoho      */
    337          1.1  jruoho     while (GenericField)
    338          1.1  jruoho     {
    339          1.1  jruoho         Info = DtGetGenericTableInfo (GenericField->Name);
    340          1.1  jruoho         if (Info)
    341          1.1  jruoho         {
    342          1.1  jruoho             /* Maintain table offsets */
    343          1.1  jruoho 
    344          1.1  jruoho             GenericField->TableOffset = TableOffset;
    345          1.1  jruoho             TableOffset += DtGetFieldLength (GenericField, Info);
    346          1.1  jruoho 
    347          1.1  jruoho             /* Insert all labels in the global label list */
    348          1.1  jruoho 
    349          1.1  jruoho             if (Info->Opcode == ACPI_DMT_LABEL)
    350          1.1  jruoho             {
    351          1.1  jruoho                 DtInsertLabelField (GenericField);
    352          1.1  jruoho             }
    353          1.1  jruoho         }
    354          1.1  jruoho 
    355          1.1  jruoho         GenericField = GenericField->Next;
    356          1.1  jruoho     }
    357          1.1  jruoho }
    358          1.1  jruoho 
    359          1.1  jruoho 
    360          1.1  jruoho /******************************************************************************
    361          1.1  jruoho  *
    362          1.1  jruoho  * FUNCTION:    DtInsertLabelField
    363          1.1  jruoho  *
    364          1.1  jruoho  * PARAMETERS:  Field               - Field object with Label to be inserted
    365          1.1  jruoho  *
    366          1.1  jruoho  * RETURN:      None
    367          1.1  jruoho  *
    368          1.1  jruoho  * DESCRIPTION: Insert a label field into the global label list
    369          1.1  jruoho  *
    370          1.1  jruoho  *****************************************************************************/
    371          1.1  jruoho 
    372          1.1  jruoho static void
    373          1.1  jruoho DtInsertLabelField (
    374          1.1  jruoho     DT_FIELD                *Field)
    375          1.1  jruoho {
    376          1.1  jruoho 
    377          1.1  jruoho     DbgPrint (ASL_DEBUG_OUTPUT,
    378          1.1  jruoho         "DtInsertLabelField: Found Label : %s at output table offset %X\n",
    379          1.1  jruoho         Field->Value, Field->TableOffset);
    380          1.1  jruoho 
    381          1.1  jruoho     Field->NextLabel = Gbl_LabelList;
    382          1.1  jruoho     Gbl_LabelList = Field;
    383          1.1  jruoho }
    384          1.1  jruoho 
    385          1.1  jruoho 
    386          1.1  jruoho /******************************************************************************
    387          1.1  jruoho  *
    388          1.1  jruoho  * FUNCTION:    DtLookupLabel
    389          1.1  jruoho  *
    390          1.1  jruoho  * PARAMETERS:  Name                - Label to be resolved
    391          1.1  jruoho  *
    392          1.1  jruoho  * RETURN:      Field object associated with the label
    393          1.1  jruoho  *
    394          1.1  jruoho  * DESCRIPTION: Lookup a label in the global label list. Used during the
    395          1.1  jruoho  *              resolution of integer expressions.
    396          1.1  jruoho  *
    397          1.1  jruoho  *****************************************************************************/
    398          1.1  jruoho 
    399          1.1  jruoho static DT_FIELD *
    400          1.1  jruoho DtLookupLabel (
    401          1.1  jruoho     char                    *Name)
    402          1.1  jruoho {
    403          1.1  jruoho     DT_FIELD                *LabelField;
    404          1.1  jruoho 
    405          1.1  jruoho 
    406          1.1  jruoho     /* Skip a leading $ */
    407          1.1  jruoho 
    408          1.1  jruoho     if (*Name == '$')
    409          1.1  jruoho     {
    410          1.1  jruoho         Name++;
    411          1.1  jruoho     }
    412          1.1  jruoho 
    413          1.1  jruoho     /* Search global list */
    414          1.1  jruoho 
    415          1.1  jruoho     LabelField = Gbl_LabelList;
    416          1.1  jruoho     while (LabelField)
    417          1.1  jruoho     {
    418          1.1  jruoho         if (!ACPI_STRCMP (Name, LabelField->Value))
    419          1.1  jruoho         {
    420          1.1  jruoho             return (LabelField);
    421          1.1  jruoho         }
    422          1.1  jruoho         LabelField = LabelField->NextLabel;
    423          1.1  jruoho     }
    424          1.1  jruoho 
    425          1.1  jruoho     return (NULL);
    426          1.1  jruoho }
    427