Home | History | Annotate | Line # | Download | only in compiler
      1       1.1    jruoho /******************************************************************************
      2       1.1    jruoho  *
      3       1.1    jruoho  * Module Name: aslfold - Constant folding
      4       1.1    jruoho  *
      5       1.1    jruoho  *****************************************************************************/
      6       1.1    jruoho 
      7  1.1.1.18  christos /******************************************************************************
      8  1.1.1.18  christos  *
      9  1.1.1.18  christos  * 1. Copyright Notice
     10  1.1.1.18  christos  *
     11  1.1.1.19  christos  * Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.
     12       1.1    jruoho  * All rights reserved.
     13       1.1    jruoho  *
     14  1.1.1.18  christos  * 2. License
     15  1.1.1.18  christos  *
     16  1.1.1.18  christos  * 2.1. This is your license from Intel Corp. under its intellectual property
     17  1.1.1.18  christos  * rights. You may have additional license terms from the party that provided
     18  1.1.1.18  christos  * you this software, covering your right to use that party's intellectual
     19  1.1.1.18  christos  * property rights.
     20  1.1.1.18  christos  *
     21  1.1.1.18  christos  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
     22  1.1.1.18  christos  * copy of the source code appearing in this file ("Covered Code") an
     23  1.1.1.18  christos  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
     24  1.1.1.18  christos  * base code distributed originally by Intel ("Original Intel Code") to copy,
     25  1.1.1.18  christos  * make derivatives, distribute, use and display any portion of the Covered
     26  1.1.1.18  christos  * Code in any form, with the right to sublicense such rights; and
     27  1.1.1.18  christos  *
     28  1.1.1.18  christos  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
     29  1.1.1.18  christos  * license (with the right to sublicense), under only those claims of Intel
     30  1.1.1.18  christos  * patents that are infringed by the Original Intel Code, to make, use, sell,
     31  1.1.1.18  christos  * offer to sell, and import the Covered Code and derivative works thereof
     32  1.1.1.18  christos  * solely to the minimum extent necessary to exercise the above copyright
     33  1.1.1.18  christos  * license, and in no event shall the patent license extend to any additions
     34  1.1.1.18  christos  * to or modifications of the Original Intel Code. No other license or right
     35  1.1.1.18  christos  * is granted directly or by implication, estoppel or otherwise;
     36  1.1.1.18  christos  *
     37  1.1.1.18  christos  * The above copyright and patent license is granted only if the following
     38  1.1.1.18  christos  * conditions are met:
     39  1.1.1.18  christos  *
     40  1.1.1.18  christos  * 3. Conditions
     41  1.1.1.18  christos  *
     42  1.1.1.18  christos  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
     43  1.1.1.18  christos  * Redistribution of source code of any substantial portion of the Covered
     44  1.1.1.18  christos  * Code or modification with rights to further distribute source must include
     45  1.1.1.18  christos  * the above Copyright Notice, the above License, this list of Conditions,
     46  1.1.1.18  christos  * and the following Disclaimer and Export Compliance provision. In addition,
     47  1.1.1.18  christos  * Licensee must cause all Covered Code to which Licensee contributes to
     48  1.1.1.18  christos  * contain a file documenting the changes Licensee made to create that Covered
     49  1.1.1.18  christos  * Code and the date of any change. Licensee must include in that file the
     50  1.1.1.18  christos  * documentation of any changes made by any predecessor Licensee. Licensee
     51  1.1.1.18  christos  * must include a prominent statement that the modification is derived,
     52  1.1.1.18  christos  * directly or indirectly, from Original Intel Code.
     53  1.1.1.18  christos  *
     54  1.1.1.18  christos  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
     55  1.1.1.18  christos  * Redistribution of source code of any substantial portion of the Covered
     56  1.1.1.18  christos  * Code or modification without rights to further distribute source must
     57  1.1.1.18  christos  * include the following Disclaimer and Export Compliance provision in the
     58  1.1.1.18  christos  * documentation and/or other materials provided with distribution. In
     59  1.1.1.18  christos  * addition, Licensee may not authorize further sublicense of source of any
     60  1.1.1.18  christos  * portion of the Covered Code, and must include terms to the effect that the
     61  1.1.1.18  christos  * license from Licensee to its licensee is limited to the intellectual
     62  1.1.1.18  christos  * property embodied in the software Licensee provides to its licensee, and
     63  1.1.1.18  christos  * not to intellectual property embodied in modifications its licensee may
     64  1.1.1.18  christos  * make.
     65  1.1.1.18  christos  *
     66  1.1.1.18  christos  * 3.3. Redistribution of Executable. Redistribution in executable form of any
     67  1.1.1.18  christos  * substantial portion of the Covered Code or modification must reproduce the
     68  1.1.1.18  christos  * above Copyright Notice, and the following Disclaimer and Export Compliance
     69  1.1.1.18  christos  * provision in the documentation and/or other materials provided with the
     70  1.1.1.18  christos  * distribution.
     71  1.1.1.18  christos  *
     72  1.1.1.18  christos  * 3.4. Intel retains all right, title, and interest in and to the Original
     73  1.1.1.18  christos  * Intel Code.
     74  1.1.1.18  christos  *
     75  1.1.1.18  christos  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
     76  1.1.1.18  christos  * Intel shall be used in advertising or otherwise to promote the sale, use or
     77  1.1.1.18  christos  * other dealings in products derived from or relating to the Covered Code
     78  1.1.1.18  christos  * without prior written authorization from Intel.
     79  1.1.1.18  christos  *
     80  1.1.1.18  christos  * 4. Disclaimer and Export Compliance
     81  1.1.1.18  christos  *
     82  1.1.1.18  christos  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
     83  1.1.1.18  christos  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
     84  1.1.1.18  christos  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
     85  1.1.1.18  christos  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
     86  1.1.1.18  christos  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
     87  1.1.1.18  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
     88  1.1.1.18  christos  * PARTICULAR PURPOSE.
     89  1.1.1.18  christos  *
     90  1.1.1.18  christos  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
     91  1.1.1.18  christos  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
     92  1.1.1.18  christos  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
     93  1.1.1.18  christos  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
     94  1.1.1.18  christos  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
     95  1.1.1.18  christos  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
     96  1.1.1.18  christos  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
     97  1.1.1.18  christos  * LIMITED REMEDY.
     98  1.1.1.18  christos  *
     99  1.1.1.18  christos  * 4.3. Licensee shall not export, either directly or indirectly, any of this
    100  1.1.1.18  christos  * software or system incorporating such software without first obtaining any
    101  1.1.1.18  christos  * required license or other approval from the U. S. Department of Commerce or
    102  1.1.1.18  christos  * any other agency or department of the United States Government. In the
    103  1.1.1.18  christos  * event Licensee exports any such software from the United States or
    104  1.1.1.18  christos  * re-exports any such software from a foreign destination, Licensee shall
    105  1.1.1.18  christos  * ensure that the distribution and export/re-export of the software is in
    106  1.1.1.18  christos  * compliance with all laws, regulations, orders, or other restrictions of the
    107  1.1.1.18  christos  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
    108  1.1.1.18  christos  * any of its subsidiaries will export/re-export any technical data, process,
    109  1.1.1.18  christos  * software, or service, directly or indirectly, to any country for which the
    110  1.1.1.18  christos  * United States government or any agency thereof requires an export license,
    111  1.1.1.18  christos  * other governmental approval, or letter of assurance, without first obtaining
    112  1.1.1.18  christos  * such license, approval or letter.
    113  1.1.1.18  christos  *
    114  1.1.1.18  christos  *****************************************************************************
    115  1.1.1.18  christos  *
    116  1.1.1.18  christos  * Alternatively, you may choose to be licensed under the terms of the
    117  1.1.1.18  christos  * following license:
    118  1.1.1.18  christos  *
    119   1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
    120   1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
    121   1.1.1.2    jruoho  * are met:
    122   1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
    123   1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
    124   1.1.1.2    jruoho  *    without modification.
    125   1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
    126   1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
    127   1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
    128   1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
    129   1.1.1.2    jruoho  *    binary redistribution.
    130   1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
    131   1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
    132   1.1.1.2    jruoho  *    from this software without specific prior written permission.
    133   1.1.1.2    jruoho  *
    134   1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    135   1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    136  1.1.1.15  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    137   1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    138  1.1.1.18  christos  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    139  1.1.1.18  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    140  1.1.1.18  christos  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    141  1.1.1.18  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    142  1.1.1.18  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    143  1.1.1.18  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    144  1.1.1.18  christos  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    145  1.1.1.18  christos  *
    146  1.1.1.18  christos  * Alternatively, you may choose to be licensed under the terms of the
    147  1.1.1.18  christos  * GNU General Public License ("GPL") version 2 as published by the Free
    148  1.1.1.18  christos  * Software Foundation.
    149  1.1.1.18  christos  *
    150  1.1.1.18  christos  *****************************************************************************/
    151       1.1    jruoho 
    152       1.1    jruoho #include "aslcompiler.h"
    153       1.1    jruoho #include "aslcompiler.y.h"
    154       1.1    jruoho #include "amlcode.h"
    155       1.1    jruoho 
    156       1.1    jruoho #include "acdispat.h"
    157       1.1    jruoho #include "acparser.h"
    158       1.1    jruoho 
    159       1.1    jruoho #define _COMPONENT          ACPI_COMPILER
    160       1.1    jruoho         ACPI_MODULE_NAME    ("aslfold")
    161       1.1    jruoho 
    162       1.1    jruoho /* Local prototypes */
    163       1.1    jruoho 
    164       1.1    jruoho static ACPI_STATUS
    165       1.1    jruoho OpcAmlEvaluationWalk1 (
    166       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    167       1.1    jruoho     UINT32                  Level,
    168       1.1    jruoho     void                    *Context);
    169       1.1    jruoho 
    170       1.1    jruoho static ACPI_STATUS
    171       1.1    jruoho OpcAmlEvaluationWalk2 (
    172       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    173       1.1    jruoho     UINT32                  Level,
    174       1.1    jruoho     void                    *Context);
    175       1.1    jruoho 
    176       1.1    jruoho static ACPI_STATUS
    177       1.1    jruoho OpcAmlCheckForConstant (
    178       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    179       1.1    jruoho     UINT32                  Level,
    180       1.1    jruoho     void                    *Context);
    181       1.1    jruoho 
    182   1.1.1.3  christos static void
    183   1.1.1.3  christos OpcUpdateIntegerNode (
    184   1.1.1.3  christos     ACPI_PARSE_OBJECT       *Op,
    185   1.1.1.3  christos     UINT64                  Value);
    186   1.1.1.3  christos 
    187   1.1.1.5  christos static ACPI_STATUS
    188   1.1.1.5  christos TrTransformToStoreOp (
    189   1.1.1.5  christos     ACPI_PARSE_OBJECT       *Op,
    190   1.1.1.5  christos     ACPI_WALK_STATE         *WalkState);
    191   1.1.1.5  christos 
    192   1.1.1.5  christos static ACPI_STATUS
    193   1.1.1.5  christos TrSimpleConstantReduction (
    194   1.1.1.5  christos     ACPI_PARSE_OBJECT       *Op,
    195   1.1.1.5  christos     ACPI_WALK_STATE         *WalkState);
    196   1.1.1.5  christos 
    197   1.1.1.5  christos static void
    198   1.1.1.5  christos TrInstallReducedConstant (
    199   1.1.1.5  christos     ACPI_PARSE_OBJECT       *Op,
    200   1.1.1.5  christos     ACPI_OPERAND_OBJECT     *ObjDesc);
    201   1.1.1.5  christos 
    202       1.1    jruoho 
    203       1.1    jruoho /*******************************************************************************
    204       1.1    jruoho  *
    205   1.1.1.5  christos  * FUNCTION:    OpcAmlConstantWalk
    206       1.1    jruoho  *
    207       1.1    jruoho  * PARAMETERS:  ASL_WALK_CALLBACK
    208       1.1    jruoho  *
    209       1.1    jruoho  * RETURN:      Status
    210       1.1    jruoho  *
    211   1.1.1.7  christos  * DESCRIPTION: Reduce an Op and its subtree to a constant if possible.
    212   1.1.1.8  christos  *              Called during ascent of the parse tree.
    213       1.1    jruoho  *
    214       1.1    jruoho  ******************************************************************************/
    215       1.1    jruoho 
    216   1.1.1.5  christos ACPI_STATUS
    217   1.1.1.5  christos OpcAmlConstantWalk (
    218       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    219       1.1    jruoho     UINT32                  Level,
    220       1.1    jruoho     void                    *Context)
    221       1.1    jruoho {
    222   1.1.1.5  christos     ACPI_WALK_STATE         *WalkState;
    223   1.1.1.5  christos     ACPI_STATUS             Status = AE_OK;
    224       1.1    jruoho 
    225       1.1    jruoho 
    226   1.1.1.5  christos     if (Op->Asl.CompileFlags == 0)
    227   1.1.1.5  christos     {
    228   1.1.1.5  christos         return (AE_OK);
    229   1.1.1.5  christos     }
    230       1.1    jruoho 
    231   1.1.1.5  christos     /*
    232   1.1.1.5  christos      * Only interested in subtrees that could possibly contain
    233   1.1.1.5  christos      * expressions that can be evaluated at this time
    234   1.1.1.5  christos      */
    235  1.1.1.10  christos     if ((!(Op->Asl.CompileFlags & OP_COMPILE_TIME_CONST)) ||
    236  1.1.1.10  christos           (Op->Asl.CompileFlags & OP_IS_TARGET))
    237       1.1    jruoho     {
    238   1.1.1.5  christos         return (AE_OK);
    239       1.1    jruoho     }
    240       1.1    jruoho 
    241   1.1.1.5  christos     /* Create a new walk state */
    242       1.1    jruoho 
    243   1.1.1.5  christos     WalkState = AcpiDsCreateWalkState (0, NULL, NULL, NULL);
    244   1.1.1.5  christos     if (!WalkState)
    245       1.1    jruoho     {
    246   1.1.1.5  christos         return (AE_NO_MEMORY);
    247       1.1    jruoho     }
    248       1.1    jruoho 
    249   1.1.1.5  christos     WalkState->NextOp = NULL;
    250   1.1.1.5  christos     WalkState->Params = NULL;
    251       1.1    jruoho 
    252   1.1.1.5  christos     /*
    253   1.1.1.5  christos      * Examine the entire subtree -- all nodes must be constants
    254   1.1.1.5  christos      * or type 3/4/5 opcodes
    255   1.1.1.5  christos      */
    256   1.1.1.5  christos     Status = TrWalkParseTree (Op, ASL_WALK_VISIT_DOWNWARD,
    257   1.1.1.5  christos         OpcAmlCheckForConstant, NULL, WalkState);
    258       1.1    jruoho 
    259   1.1.1.5  christos     /*
    260   1.1.1.5  christos      * Did we find an entire subtree that contains all constants
    261   1.1.1.5  christos      * and type 3/4/5 opcodes?
    262   1.1.1.5  christos      */
    263   1.1.1.5  christos     switch (Status)
    264   1.1.1.5  christos     {
    265   1.1.1.5  christos     case AE_OK:
    266       1.1    jruoho 
    267   1.1.1.5  christos         /* Simple case, like Add(3,4) -> 7 */
    268       1.1    jruoho 
    269   1.1.1.5  christos         Status = TrSimpleConstantReduction (Op, WalkState);
    270   1.1.1.5  christos         break;
    271       1.1    jruoho 
    272   1.1.1.5  christos     case AE_CTRL_RETURN_VALUE:
    273       1.1    jruoho 
    274   1.1.1.5  christos         /* More complex case, like Add(3,4,Local0) -> Store(7,Local0) */
    275       1.1    jruoho 
    276   1.1.1.5  christos         Status = TrTransformToStoreOp (Op, WalkState);
    277   1.1.1.5  christos         break;
    278       1.1    jruoho 
    279   1.1.1.5  christos     case AE_TYPE:
    280   1.1.1.5  christos 
    281   1.1.1.5  christos         AcpiDsDeleteWalkState (WalkState);
    282   1.1.1.5  christos         return (AE_OK);
    283   1.1.1.5  christos 
    284   1.1.1.5  christos     default:
    285   1.1.1.5  christos         AcpiDsDeleteWalkState (WalkState);
    286   1.1.1.5  christos         break;
    287   1.1.1.5  christos     }
    288       1.1    jruoho 
    289       1.1    jruoho     if (ACPI_FAILURE (Status))
    290       1.1    jruoho     {
    291   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT, "Cannot resolve, %s\n",
    292   1.1.1.5  christos             AcpiFormatException (Status));
    293   1.1.1.5  christos 
    294   1.1.1.5  christos         /* We could not resolve the subtree for some reason */
    295   1.1.1.5  christos 
    296   1.1.1.5  christos         AslError (ASL_ERROR, ASL_MSG_CONSTANT_EVALUATION, Op,
    297   1.1.1.5  christos             (char *) AcpiFormatException (Status));
    298   1.1.1.5  christos 
    299   1.1.1.5  christos         /* Set the subtree value to ZERO anyway. Eliminates further errors */
    300   1.1.1.5  christos 
    301   1.1.1.5  christos         OpcUpdateIntegerNode (Op, 0);
    302       1.1    jruoho     }
    303       1.1    jruoho 
    304   1.1.1.8  christos     return (AE_OK);
    305       1.1    jruoho }
    306       1.1    jruoho 
    307       1.1    jruoho 
    308       1.1    jruoho /*******************************************************************************
    309       1.1    jruoho  *
    310       1.1    jruoho  * FUNCTION:    OpcAmlCheckForConstant
    311       1.1    jruoho  *
    312       1.1    jruoho  * PARAMETERS:  ASL_WALK_CALLBACK
    313       1.1    jruoho  *
    314       1.1    jruoho  * RETURN:      Status
    315       1.1    jruoho  *
    316   1.1.1.7  christos  * DESCRIPTION: Check one Op for a reducible type 3/4/5 AML opcode.
    317   1.1.1.8  christos  *              This is performed via an upward walk of the parse subtree.
    318       1.1    jruoho  *
    319       1.1    jruoho  ******************************************************************************/
    320       1.1    jruoho 
    321       1.1    jruoho static ACPI_STATUS
    322       1.1    jruoho OpcAmlCheckForConstant (
    323       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    324       1.1    jruoho     UINT32                  Level,
    325       1.1    jruoho     void                    *Context)
    326       1.1    jruoho {
    327       1.1    jruoho     ACPI_WALK_STATE         *WalkState = Context;
    328   1.1.1.5  christos     ACPI_STATUS             Status = AE_OK;
    329   1.1.1.7  christos     ACPI_PARSE_OBJECT       *NextOp;
    330   1.1.1.7  christos     const ACPI_OPCODE_INFO  *OpInfo;
    331       1.1    jruoho 
    332       1.1    jruoho 
    333       1.1    jruoho     WalkState->Op = Op;
    334       1.1    jruoho     WalkState->Opcode = Op->Common.AmlOpcode;
    335       1.1    jruoho     WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
    336       1.1    jruoho 
    337       1.1    jruoho     DbgPrint (ASL_PARSE_OUTPUT, "[%.4d] Opcode: %12.12s ",
    338   1.1.1.5  christos         Op->Asl.LogicalLineNumber, Op->Asl.ParseOpName);
    339   1.1.1.5  christos 
    340   1.1.1.5  christos     /*
    341   1.1.1.3  christos      * These opcodes do not appear in the OpcodeInfo table, but
    342   1.1.1.3  christos      * they represent constants, so abort the constant walk now.
    343   1.1.1.3  christos      */
    344   1.1.1.3  christos     if ((WalkState->Opcode == AML_RAW_DATA_BYTE) ||
    345   1.1.1.3  christos         (WalkState->Opcode == AML_RAW_DATA_WORD) ||
    346   1.1.1.3  christos         (WalkState->Opcode == AML_RAW_DATA_DWORD) ||
    347   1.1.1.3  christos         (WalkState->Opcode == AML_RAW_DATA_QWORD))
    348   1.1.1.3  christos     {
    349   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT, "RAW DATA");
    350   1.1.1.5  christos         Status = AE_TYPE;
    351   1.1.1.5  christos         goto CleanupAndExit;
    352   1.1.1.3  christos     }
    353   1.1.1.3  christos 
    354   1.1.1.7  christos     /*
    355   1.1.1.7  christos      * Search upwards for a possible Name() operator. This is done
    356   1.1.1.7  christos      * because a type 3/4/5 opcode within a Name() expression
    357   1.1.1.7  christos      * MUST be reduced to a simple constant.
    358   1.1.1.7  christos      */
    359   1.1.1.7  christos     NextOp = Op->Asl.Parent;
    360   1.1.1.7  christos     while (NextOp)
    361   1.1.1.7  christos     {
    362   1.1.1.7  christos         /* Finished if we find a Name() opcode */
    363   1.1.1.7  christos 
    364   1.1.1.7  christos         if (NextOp->Asl.AmlOpcode == AML_NAME_OP)
    365   1.1.1.7  christos         {
    366   1.1.1.7  christos             break;
    367   1.1.1.7  christos         }
    368   1.1.1.7  christos 
    369   1.1.1.7  christos         /*
    370   1.1.1.7  christos          * Any "deferred" opcodes contain one or more TermArg parameters,
    371   1.1.1.7  christos          * and thus are not required to be folded to constants at compile
    372   1.1.1.7  christos          * time. This affects things like Buffer() and Package() objects.
    373   1.1.1.7  christos          * We just ignore them here. However, any sub-expressions can and
    374   1.1.1.7  christos          * will still be typechecked. Note: These are called the
    375   1.1.1.7  christos          * "deferred" opcodes in the AML interpreter.
    376   1.1.1.7  christos          */
    377   1.1.1.7  christos         OpInfo = AcpiPsGetOpcodeInfo (NextOp->Common.AmlOpcode);
    378   1.1.1.7  christos         if (OpInfo->Flags & AML_DEFER)
    379   1.1.1.7  christos         {
    380   1.1.1.7  christos             NextOp = NULL;
    381   1.1.1.7  christos             break;
    382   1.1.1.7  christos         }
    383   1.1.1.7  christos 
    384   1.1.1.7  christos         NextOp = NextOp->Asl.Parent;
    385   1.1.1.7  christos     }
    386   1.1.1.7  christos 
    387   1.1.1.5  christos     /* Type 3/4/5 opcodes have the AML_CONSTANT flag set */
    388   1.1.1.5  christos 
    389       1.1    jruoho     if (!(WalkState->OpInfo->Flags & AML_CONSTANT))
    390       1.1    jruoho     {
    391   1.1.1.7  christos         /*
    392   1.1.1.7  christos          * From the ACPI specification:
    393   1.1.1.7  christos          *
    394   1.1.1.7  christos          * "The Type 3/4/5 opcodes return a value and can be used in an
    395   1.1.1.7  christos          * expression that evaluates to a constant. These opcodes may be
    396   1.1.1.7  christos          * evaluated at ASL compile-time. To ensure that these opcodes
    397   1.1.1.7  christos          * will evaluate to a constant, the following rules apply: The
    398   1.1.1.7  christos          * term cannot have a destination (target) operand, and must have
    399   1.1.1.7  christos          * either a Type3Opcode, Type4Opcode, Type5Opcode, ConstExprTerm,
    400   1.1.1.7  christos          * Integer, BufferTerm, Package, or String for all arguments."
    401   1.1.1.7  christos          */
    402   1.1.1.7  christos 
    403   1.1.1.7  christos         /*
    404   1.1.1.7  christos          * The value (second) operand for the Name() operator MUST
    405   1.1.1.7  christos          * reduce to a single constant, as per the ACPI specification
    406   1.1.1.7  christos          * (the operand is a DataObject). This also implies that there
    407   1.1.1.7  christos          * can be no target operand. Name() is the only ASL operator
    408   1.1.1.7  christos          * with a "DataObject" as an operand and is thus special-
    409   1.1.1.7  christos          * cased here.
    410   1.1.1.7  christos          */
    411   1.1.1.7  christos         if (NextOp) /* Inspect a Name() operator */
    412   1.1.1.7  christos         {
    413   1.1.1.7  christos             /* Error if there is a target operand */
    414   1.1.1.7  christos 
    415  1.1.1.10  christos             if (Op->Asl.CompileFlags & OP_IS_TARGET)
    416   1.1.1.7  christos             {
    417   1.1.1.7  christos                 AslError (ASL_ERROR, ASL_MSG_INVALID_TARGET, Op, NULL);
    418   1.1.1.7  christos                 Status = AE_TYPE;
    419   1.1.1.7  christos             }
    420   1.1.1.7  christos 
    421   1.1.1.7  christos             /* Error if expression cannot be reduced (folded) */
    422   1.1.1.7  christos 
    423  1.1.1.10  christos             if (!(NextOp->Asl.CompileFlags & OP_COULD_NOT_REDUCE))
    424   1.1.1.7  christos             {
    425   1.1.1.7  christos                 /* Ensure only one error message per statement */
    426   1.1.1.7  christos 
    427  1.1.1.10  christos                 NextOp->Asl.CompileFlags |= OP_COULD_NOT_REDUCE;
    428   1.1.1.7  christos                 DbgPrint (ASL_PARSE_OUTPUT,
    429   1.1.1.7  christos                     "**** Could not reduce operands for NAME opcode ****\n");
    430   1.1.1.7  christos 
    431   1.1.1.7  christos                 AslError (ASL_ERROR, ASL_MSG_CONSTANT_REQUIRED, Op,
    432   1.1.1.7  christos                     "Constant is required for Name operator");
    433   1.1.1.7  christos                 Status = AE_TYPE;
    434   1.1.1.7  christos             }
    435   1.1.1.7  christos         }
    436   1.1.1.7  christos 
    437   1.1.1.7  christos         if (ACPI_FAILURE (Status))
    438   1.1.1.7  christos         {
    439   1.1.1.7  christos             goto CleanupAndExit;
    440   1.1.1.7  christos         }
    441   1.1.1.7  christos 
    442   1.1.1.7  christos         /* This is not a 3/4/5 opcode, but maybe can convert to STORE */
    443       1.1    jruoho 
    444  1.1.1.10  christos         if (Op->Asl.CompileFlags & OP_IS_TARGET)
    445       1.1    jruoho         {
    446       1.1    jruoho             DbgPrint (ASL_PARSE_OUTPUT,
    447  1.1.1.11  christos                 "**** Valid Target, transform to Store or CopyObject ****\n");
    448   1.1.1.5  christos             return (AE_CTRL_RETURN_VALUE);
    449       1.1    jruoho         }
    450       1.1    jruoho 
    451   1.1.1.5  christos         /* Expression cannot be reduced */
    452       1.1    jruoho 
    453   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
    454   1.1.1.7  christos             "**** Not a Type 3/4/5 opcode or cannot reduce/fold (%s) ****\n",
    455   1.1.1.5  christos              Op->Asl.ParseOpName);
    456       1.1    jruoho 
    457   1.1.1.5  christos         Status = AE_TYPE;
    458   1.1.1.5  christos         goto CleanupAndExit;
    459       1.1    jruoho     }
    460       1.1    jruoho 
    461   1.1.1.7  christos     /*
    462   1.1.1.7  christos      * TBD: Ignore buffer constants for now. The problem is that these
    463   1.1.1.7  christos      * constants have been transformed into RAW_DATA at this point, from
    464   1.1.1.7  christos      * the parse tree transform process which currently happens before
    465   1.1.1.7  christos      * the constant folding process. We may need to defer this transform
    466   1.1.1.7  christos      * for buffer until after the constant folding.
    467   1.1.1.7  christos      */
    468   1.1.1.7  christos     if (WalkState->Opcode == AML_BUFFER_OP)
    469   1.1.1.7  christos     {
    470   1.1.1.7  christos         DbgPrint (ASL_PARSE_OUTPUT,
    471  1.1.1.11  christos             "\nBuffer constant reduction is currently not supported\n");
    472   1.1.1.7  christos 
    473   1.1.1.7  christos         if (NextOp) /* Found a Name() operator, error */
    474   1.1.1.7  christos         {
    475   1.1.1.7  christos             AslError (ASL_ERROR, ASL_MSG_UNSUPPORTED, Op,
    476   1.1.1.7  christos                 "Buffer expression cannot be reduced");
    477   1.1.1.7  christos         }
    478   1.1.1.7  christos 
    479   1.1.1.7  christos         Status = AE_TYPE;
    480   1.1.1.7  christos         goto CleanupAndExit;
    481   1.1.1.7  christos     }
    482   1.1.1.7  christos 
    483       1.1    jruoho     /* Debug output */
    484       1.1    jruoho 
    485       1.1    jruoho     DbgPrint (ASL_PARSE_OUTPUT, "TYPE_345");
    486       1.1    jruoho 
    487  1.1.1.10  christos     if (Op->Asl.CompileFlags & OP_IS_TARGET)
    488       1.1    jruoho     {
    489   1.1.1.5  christos         if (Op->Asl.ParseOpcode == PARSEOP_ZERO)
    490   1.1.1.5  christos         {
    491   1.1.1.5  christos             DbgPrint (ASL_PARSE_OUTPUT, "%-16s", " NULL TARGET");
    492   1.1.1.5  christos         }
    493   1.1.1.5  christos         else
    494   1.1.1.5  christos         {
    495   1.1.1.5  christos             DbgPrint (ASL_PARSE_OUTPUT, "%-16s", " VALID TARGET");
    496   1.1.1.5  christos         }
    497       1.1    jruoho     }
    498   1.1.1.7  christos 
    499  1.1.1.10  christos     if (Op->Asl.CompileFlags & OP_IS_TERM_ARG)
    500       1.1    jruoho     {
    501   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT, "%-16s", " TERMARG");
    502       1.1    jruoho     }
    503       1.1    jruoho 
    504   1.1.1.5  christos CleanupAndExit:
    505   1.1.1.5  christos 
    506   1.1.1.5  christos     /* Dump the node compile flags also */
    507   1.1.1.5  christos 
    508  1.1.1.10  christos     TrPrintOpFlags (Op->Asl.CompileFlags, ASL_PARSE_OUTPUT);
    509   1.1.1.3  christos     DbgPrint (ASL_PARSE_OUTPUT, "\n");
    510   1.1.1.5  christos     return (Status);
    511       1.1    jruoho }
    512       1.1    jruoho 
    513       1.1    jruoho 
    514       1.1    jruoho /*******************************************************************************
    515       1.1    jruoho  *
    516   1.1.1.5  christos  * FUNCTION:    TrSimpleConstantReduction
    517       1.1    jruoho  *
    518   1.1.1.5  christos  * PARAMETERS:  Op                  - Parent operator to be transformed
    519   1.1.1.5  christos  *              WalkState           - Current walk state
    520       1.1    jruoho  *
    521       1.1    jruoho  * RETURN:      Status
    522       1.1    jruoho  *
    523   1.1.1.5  christos  * DESCRIPTION: Reduce an entire AML operation to a single constant. The
    524   1.1.1.5  christos  *              operation must not have a target operand.
    525   1.1.1.5  christos  *
    526   1.1.1.5  christos  *              Add (32,64) --> 96
    527       1.1    jruoho  *
    528       1.1    jruoho  ******************************************************************************/
    529       1.1    jruoho 
    530   1.1.1.5  christos static ACPI_STATUS
    531   1.1.1.5  christos TrSimpleConstantReduction (
    532       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    533   1.1.1.5  christos     ACPI_WALK_STATE         *WalkState)
    534       1.1    jruoho {
    535       1.1    jruoho     ACPI_PARSE_OBJECT       *RootOp;
    536       1.1    jruoho     ACPI_PARSE_OBJECT       *OriginalParentOp;
    537   1.1.1.5  christos     ACPI_OPERAND_OBJECT     *ObjDesc;
    538   1.1.1.5  christos     ACPI_STATUS             Status;
    539       1.1    jruoho 
    540       1.1    jruoho 
    541   1.1.1.5  christos     DbgPrint (ASL_PARSE_OUTPUT,
    542   1.1.1.5  christos         "Simple subtree constant reduction, operator to constant\n");
    543   1.1.1.5  christos 
    544   1.1.1.5  christos     /* Allocate a new temporary root for this subtree */
    545   1.1.1.5  christos 
    546  1.1.1.10  christos     RootOp = TrAllocateOp (PARSEOP_INTEGER);
    547   1.1.1.5  christos     if (!RootOp)
    548       1.1    jruoho     {
    549   1.1.1.5  christos         return (AE_NO_MEMORY);
    550       1.1    jruoho     }
    551       1.1    jruoho 
    552   1.1.1.5  christos     RootOp->Common.AmlOpcode = AML_INT_EVAL_SUBTREE_OP;
    553       1.1    jruoho 
    554   1.1.1.5  christos     OriginalParentOp = Op->Common.Parent;
    555   1.1.1.5  christos     Op->Common.Parent = RootOp;
    556       1.1    jruoho 
    557   1.1.1.5  christos     /* Hand off the subtree to the AML interpreter */
    558       1.1    jruoho 
    559   1.1.1.5  christos     WalkState->CallerReturnDesc = &ObjDesc;
    560   1.1.1.5  christos 
    561   1.1.1.5  christos     Status = TrWalkParseTree (Op, ASL_WALK_VISIT_TWICE,
    562   1.1.1.5  christos         OpcAmlEvaluationWalk1, OpcAmlEvaluationWalk2, WalkState);
    563   1.1.1.5  christos 
    564   1.1.1.5  christos     /* Restore original parse tree */
    565       1.1    jruoho 
    566   1.1.1.5  christos     Op->Common.Parent = OriginalParentOp;
    567   1.1.1.5  christos 
    568   1.1.1.5  christos     if (ACPI_FAILURE (Status))
    569   1.1.1.5  christos     {
    570   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
    571   1.1.1.5  christos             "Constant Subtree evaluation(1), %s\n",
    572   1.1.1.5  christos             AcpiFormatException (Status));
    573   1.1.1.5  christos         return (Status);
    574       1.1    jruoho     }
    575       1.1    jruoho 
    576   1.1.1.5  christos     /* Get the final result */
    577       1.1    jruoho 
    578   1.1.1.5  christos     Status = AcpiDsResultPop (&ObjDesc, WalkState);
    579   1.1.1.5  christos     if (ACPI_FAILURE (Status))
    580       1.1    jruoho     {
    581   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
    582   1.1.1.5  christos             "Constant Subtree evaluation(2), %s\n",
    583   1.1.1.5  christos             AcpiFormatException (Status));
    584   1.1.1.5  christos         return (Status);
    585       1.1    jruoho     }
    586       1.1    jruoho 
    587   1.1.1.6  christos     /* Disconnect any existing children, install new constant */
    588   1.1.1.6  christos 
    589   1.1.1.6  christos     Op->Asl.Child = NULL;
    590   1.1.1.5  christos     TrInstallReducedConstant (Op, ObjDesc);
    591       1.1    jruoho 
    592   1.1.1.5  christos     UtSetParseOpName (Op);
    593   1.1.1.5  christos     return (AE_OK);
    594   1.1.1.5  christos }
    595   1.1.1.5  christos 
    596   1.1.1.5  christos 
    597   1.1.1.5  christos /*******************************************************************************
    598   1.1.1.5  christos  *
    599   1.1.1.5  christos  * FUNCTION:    TrTransformToStoreOp
    600   1.1.1.5  christos  *
    601   1.1.1.5  christos  * PARAMETERS:  Op                  - Parent operator to be transformed
    602   1.1.1.5  christos  *              WalkState           - Current walk state
    603   1.1.1.5  christos  *
    604   1.1.1.5  christos  * RETURN:      Status
    605   1.1.1.5  christos  *
    606   1.1.1.5  christos  * DESCRIPTION: Transforms a single AML operation with a constant and target
    607   1.1.1.5  christos  *              to a simple store operation:
    608   1.1.1.5  christos  *
    609   1.1.1.5  christos  *              Add (32,64,DATA) --> Store (96,DATA)
    610   1.1.1.5  christos  *
    611   1.1.1.5  christos  ******************************************************************************/
    612   1.1.1.5  christos 
    613   1.1.1.5  christos static ACPI_STATUS
    614   1.1.1.5  christos TrTransformToStoreOp (
    615   1.1.1.5  christos     ACPI_PARSE_OBJECT       *Op,
    616   1.1.1.5  christos     ACPI_WALK_STATE         *WalkState)
    617   1.1.1.5  christos {
    618   1.1.1.5  christos     ACPI_PARSE_OBJECT       *OriginalTarget;
    619   1.1.1.5  christos     ACPI_PARSE_OBJECT       *NewTarget;
    620   1.1.1.5  christos     ACPI_PARSE_OBJECT       *Child1;
    621   1.1.1.5  christos     ACPI_PARSE_OBJECT       *Child2;
    622   1.1.1.5  christos     ACPI_OPERAND_OBJECT     *ObjDesc;
    623   1.1.1.5  christos     ACPI_PARSE_OBJECT       *NewParent;
    624   1.1.1.5  christos     ACPI_PARSE_OBJECT       *OriginalParent;
    625   1.1.1.5  christos     ACPI_STATUS             Status;
    626  1.1.1.11  christos     UINT16                  NewParseOpcode;
    627  1.1.1.11  christos     UINT16                  NewAmlOpcode;
    628   1.1.1.5  christos 
    629   1.1.1.5  christos 
    630   1.1.1.5  christos     /* Extract the operands */
    631   1.1.1.5  christos 
    632   1.1.1.5  christos     Child1 = Op->Asl.Child;
    633   1.1.1.5  christos     Child2 = Child1->Asl.Next;
    634       1.1    jruoho 
    635       1.1    jruoho     /*
    636   1.1.1.5  christos      * Special case for DIVIDE -- it has two targets. The first
    637   1.1.1.5  christos      * is for the remainder and if present, we will not attempt
    638   1.1.1.5  christos      * to reduce the expression.
    639       1.1    jruoho      */
    640   1.1.1.5  christos     if (Op->Asl.ParseOpcode == PARSEOP_DIVIDE)
    641       1.1    jruoho     {
    642   1.1.1.5  christos         Child2 = Child2->Asl.Next;
    643   1.1.1.5  christos         if (Child2->Asl.ParseOpcode != PARSEOP_ZERO)
    644       1.1    jruoho         {
    645   1.1.1.5  christos             DbgPrint (ASL_PARSE_OUTPUT,
    646   1.1.1.5  christos                 "Cannot reduce DIVIDE - has two targets\n\n");
    647       1.1    jruoho             return (AE_OK);
    648       1.1    jruoho         }
    649   1.1.1.5  christos     }
    650   1.1.1.5  christos 
    651  1.1.1.11  christos     switch (Op->Asl.ParseOpcode)
    652  1.1.1.11  christos     {
    653  1.1.1.11  christos     /*
    654  1.1.1.11  christos      * Folding of the explicit conversion opcodes must use CopyObject
    655  1.1.1.11  christos      * instead of Store. This can change the object type of the target
    656  1.1.1.11  christos      * operand, as per the ACPI specification:
    657  1.1.1.11  christos      *
    658  1.1.1.11  christos      * "If the ASL operator is one of the explicit conversion operators
    659  1.1.1.11  christos      * (ToString, ToInteger, etc., and the CopyObject operator), no
    660  1.1.1.11  christos      * [implicit] conversion is performed. (In other words, the result
    661  1.1.1.11  christos      * object is stored directly to the target and completely overwrites
    662  1.1.1.11  christos      * any existing object already stored at the target)"
    663  1.1.1.11  christos      */
    664  1.1.1.11  christos     case PARSEOP_TOINTEGER:
    665  1.1.1.11  christos     case PARSEOP_TOSTRING:
    666  1.1.1.11  christos     case PARSEOP_TOBUFFER:
    667  1.1.1.11  christos     case PARSEOP_TODECIMALSTRING:
    668  1.1.1.11  christos     case PARSEOP_TOHEXSTRING:
    669  1.1.1.11  christos     case PARSEOP_TOBCD:
    670  1.1.1.11  christos     case PARSEOP_FROMBCD:
    671  1.1.1.11  christos 
    672  1.1.1.11  christos         NewParseOpcode = PARSEOP_COPYOBJECT;
    673  1.1.1.11  christos         NewAmlOpcode = AML_COPY_OBJECT_OP;
    674  1.1.1.11  christos 
    675  1.1.1.11  christos         DbgPrint (ASL_PARSE_OUTPUT,
    676  1.1.1.11  christos             "Reduction/Transform to CopyObjectOp: CopyObject(%s, %s)\n",
    677  1.1.1.11  christos             Child1->Asl.ParseOpName, Child2->Asl.ParseOpName);
    678  1.1.1.11  christos         break;
    679  1.1.1.11  christos 
    680  1.1.1.11  christos     default:
    681  1.1.1.11  christos 
    682  1.1.1.11  christos         NewParseOpcode = PARSEOP_STORE;
    683  1.1.1.11  christos         NewAmlOpcode = AML_STORE_OP;
    684  1.1.1.11  christos 
    685  1.1.1.11  christos         DbgPrint (ASL_PARSE_OUTPUT,
    686  1.1.1.11  christos             "Reduction/Transform to StoreOp: Store(%s, %s)\n",
    687  1.1.1.11  christos             Child1->Asl.ParseOpName, Child2->Asl.ParseOpName);
    688  1.1.1.11  christos         break;
    689  1.1.1.11  christos     }
    690   1.1.1.8  christos 
    691   1.1.1.5  christos     /*
    692   1.1.1.5  christos      * Create a NULL (zero) target so that we can use the
    693   1.1.1.5  christos      * interpreter to evaluate the expression.
    694   1.1.1.5  christos      */
    695  1.1.1.10  christos     NewTarget = TrCreateNullTargetOp ();
    696   1.1.1.5  christos     NewTarget->Common.AmlOpcode = AML_INT_NAMEPATH_OP;
    697       1.1    jruoho 
    698   1.1.1.5  christos     /* Handle one-operand cases (NOT, TOBCD, etc.) */
    699       1.1    jruoho 
    700   1.1.1.5  christos     if (!Child2->Asl.Next)
    701       1.1    jruoho     {
    702   1.1.1.5  christos         Child2 = Child1;
    703   1.1.1.5  christos     }
    704       1.1    jruoho 
    705   1.1.1.5  christos     /* Link in new NULL target as the last operand */
    706       1.1    jruoho 
    707   1.1.1.5  christos     OriginalTarget = Child2->Asl.Next;
    708   1.1.1.5  christos     Child2->Asl.Next = NewTarget;
    709   1.1.1.5  christos     NewTarget->Asl.Parent = OriginalTarget->Asl.Parent;
    710       1.1    jruoho 
    711  1.1.1.10  christos     NewParent = TrAllocateOp (PARSEOP_INTEGER);
    712   1.1.1.5  christos     NewParent->Common.AmlOpcode = AML_INT_EVAL_SUBTREE_OP;
    713       1.1    jruoho 
    714   1.1.1.5  christos     OriginalParent = Op->Common.Parent;
    715   1.1.1.5  christos     Op->Common.Parent = NewParent;
    716       1.1    jruoho 
    717   1.1.1.5  christos     /* Hand off the subtree to the AML interpreter */
    718       1.1    jruoho 
    719   1.1.1.5  christos     WalkState->CallerReturnDesc = &ObjDesc;
    720       1.1    jruoho 
    721   1.1.1.5  christos     Status = TrWalkParseTree (Op, ASL_WALK_VISIT_TWICE,
    722   1.1.1.5  christos         OpcAmlEvaluationWalk1, OpcAmlEvaluationWalk2, WalkState);
    723   1.1.1.5  christos     if (ACPI_FAILURE (Status))
    724   1.1.1.5  christos     {
    725   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
    726   1.1.1.5  christos             "Constant Subtree evaluation(3), %s\n",
    727   1.1.1.5  christos             AcpiFormatException (Status));
    728   1.1.1.5  christos         goto EvalError;
    729   1.1.1.5  christos     }
    730       1.1    jruoho 
    731   1.1.1.5  christos     /* Get the final result */
    732       1.1    jruoho 
    733   1.1.1.5  christos     Status = AcpiDsResultPop (&ObjDesc, WalkState);
    734   1.1.1.5  christos     if (ACPI_FAILURE (Status))
    735   1.1.1.5  christos     {
    736   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
    737   1.1.1.5  christos             "Constant Subtree evaluation(4), %s\n",
    738   1.1.1.5  christos             AcpiFormatException (Status));
    739   1.1.1.5  christos         goto EvalError;
    740   1.1.1.5  christos     }
    741       1.1    jruoho 
    742   1.1.1.6  christos     /* Truncate any subtree expressions, they have been evaluated */
    743   1.1.1.6  christos 
    744   1.1.1.6  christos     Child1->Asl.Child = NULL;
    745   1.1.1.6  christos 
    746   1.1.1.5  christos     /* Folded constant is in ObjDesc, store into Child1 */
    747   1.1.1.3  christos 
    748   1.1.1.5  christos     TrInstallReducedConstant (Child1, ObjDesc);
    749   1.1.1.3  christos 
    750  1.1.1.11  christos     /* Convert operator to STORE or COPYOBJECT */
    751       1.1    jruoho 
    752  1.1.1.11  christos     Op->Asl.ParseOpcode = NewParseOpcode;
    753  1.1.1.11  christos     Op->Asl.AmlOpcode = NewAmlOpcode;
    754   1.1.1.5  christos     UtSetParseOpName (Op);
    755   1.1.1.5  christos     Op->Common.Parent = OriginalParent;
    756       1.1    jruoho 
    757   1.1.1.5  christos     /* First child is the folded constant */
    758       1.1    jruoho 
    759   1.1.1.5  christos     /* Second child will be the target */
    760       1.1    jruoho 
    761   1.1.1.5  christos     Child1->Asl.Next = OriginalTarget;
    762   1.1.1.5  christos     return (AE_OK);
    763       1.1    jruoho 
    764       1.1    jruoho 
    765   1.1.1.5  christos EvalError:
    766   1.1.1.5  christos 
    767   1.1.1.5  christos     /* Restore original links */
    768   1.1.1.5  christos 
    769   1.1.1.5  christos     Op->Common.Parent = OriginalParent;
    770   1.1.1.5  christos     Child2->Asl.Next = OriginalTarget;
    771   1.1.1.5  christos     return (Status);
    772   1.1.1.5  christos }
    773   1.1.1.5  christos 
    774   1.1.1.5  christos 
    775   1.1.1.5  christos /*******************************************************************************
    776   1.1.1.5  christos  *
    777   1.1.1.5  christos  * FUNCTION:    TrInstallReducedConstant
    778   1.1.1.5  christos  *
    779   1.1.1.5  christos  * PARAMETERS:  Op                  - Parent operator to be transformed
    780   1.1.1.5  christos  *              ObjDesc             - Reduced constant to be installed
    781   1.1.1.5  christos  *
    782   1.1.1.5  christos  * RETURN:      None
    783   1.1.1.5  christos  *
    784   1.1.1.5  christos  * DESCRIPTION: Transform the original operator to a simple constant.
    785   1.1.1.5  christos  *              Handles Integers, Strings, and Buffers.
    786   1.1.1.5  christos  *
    787   1.1.1.5  christos  ******************************************************************************/
    788       1.1    jruoho 
    789   1.1.1.5  christos static void
    790   1.1.1.5  christos TrInstallReducedConstant (
    791   1.1.1.5  christos     ACPI_PARSE_OBJECT       *Op,
    792   1.1.1.5  christos     ACPI_OPERAND_OBJECT     *ObjDesc)
    793   1.1.1.5  christos {
    794   1.1.1.6  christos     ACPI_PARSE_OBJECT       *LengthOp;
    795   1.1.1.6  christos     ACPI_PARSE_OBJECT       *DataOp;
    796       1.1    jruoho 
    797       1.1    jruoho 
    798  1.1.1.12  christos     AslGbl_TotalFolds++;
    799   1.1.1.5  christos     AslError (ASL_OPTIMIZATION, ASL_MSG_CONSTANT_FOLDED, Op,
    800   1.1.1.5  christos         Op->Asl.ParseOpName);
    801       1.1    jruoho 
    802   1.1.1.5  christos     /*
    803   1.1.1.5  christos      * Because we know we executed type 3/4/5 opcodes above, we know that
    804   1.1.1.5  christos      * the result must be either an Integer, String, or Buffer.
    805   1.1.1.5  christos      */
    806   1.1.1.5  christos     switch (ObjDesc->Common.Type)
    807   1.1.1.5  christos     {
    808   1.1.1.5  christos     case ACPI_TYPE_INTEGER:
    809       1.1    jruoho 
    810   1.1.1.5  christos         OpcUpdateIntegerNode (Op, ObjDesc->Integer.Value);
    811       1.1    jruoho 
    812   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
    813   1.1.1.5  christos             "Constant expression reduced to (%s) %8.8X%8.8X\n\n",
    814   1.1.1.5  christos             Op->Asl.ParseOpName,
    815   1.1.1.5  christos             ACPI_FORMAT_UINT64 (Op->Common.Value.Integer));
    816   1.1.1.5  christos         break;
    817       1.1    jruoho 
    818   1.1.1.5  christos     case ACPI_TYPE_STRING:
    819       1.1    jruoho 
    820   1.1.1.5  christos         Op->Asl.ParseOpcode = PARSEOP_STRING_LITERAL;
    821   1.1.1.5  christos         Op->Common.AmlOpcode = AML_STRING_OP;
    822   1.1.1.6  christos         Op->Asl.AmlLength = strlen (ObjDesc->String.Pointer) + 1;
    823   1.1.1.5  christos         Op->Common.Value.String = ObjDesc->String.Pointer;
    824   1.1.1.5  christos 
    825   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
    826   1.1.1.5  christos             "Constant expression reduced to (STRING) %s\n\n",
    827   1.1.1.5  christos             Op->Common.Value.String);
    828   1.1.1.5  christos         break;
    829       1.1    jruoho 
    830   1.1.1.5  christos     case ACPI_TYPE_BUFFER:
    831   1.1.1.6  christos         /*
    832   1.1.1.6  christos          * Create a new parse subtree of the form:
    833   1.1.1.6  christos          *
    834   1.1.1.6  christos          * BUFFER (Buffer AML opcode)
    835   1.1.1.6  christos          *    INTEGER (Buffer length in bytes)
    836   1.1.1.6  christos          *    RAW_DATA (Buffer byte data)
    837   1.1.1.6  christos          */
    838   1.1.1.5  christos         Op->Asl.ParseOpcode = PARSEOP_BUFFER;
    839   1.1.1.5  christos         Op->Common.AmlOpcode = AML_BUFFER_OP;
    840  1.1.1.10  christos         Op->Asl.CompileFlags = OP_AML_PACKAGE;
    841   1.1.1.5  christos         UtSetParseOpName (Op);
    842       1.1    jruoho 
    843   1.1.1.5  christos         /* Child node is the buffer length */
    844       1.1    jruoho 
    845  1.1.1.10  christos         LengthOp = TrAllocateOp (PARSEOP_INTEGER);
    846       1.1    jruoho 
    847   1.1.1.6  christos         LengthOp->Asl.AmlOpcode = AML_DWORD_OP;
    848   1.1.1.6  christos         LengthOp->Asl.Value.Integer = ObjDesc->Buffer.Length;
    849   1.1.1.6  christos         LengthOp->Asl.Parent = Op;
    850   1.1.1.6  christos         (void) OpcSetOptimalIntegerSize (LengthOp);
    851   1.1.1.5  christos 
    852   1.1.1.6  christos         Op->Asl.Child = LengthOp;
    853   1.1.1.5  christos 
    854   1.1.1.6  christos         /* Next child is the raw buffer data */
    855   1.1.1.5  christos 
    856  1.1.1.10  christos         DataOp = TrAllocateOp (PARSEOP_RAW_DATA);
    857   1.1.1.6  christos         DataOp->Asl.AmlOpcode = AML_RAW_DATA_BUFFER;
    858   1.1.1.6  christos         DataOp->Asl.AmlLength = ObjDesc->Buffer.Length;
    859   1.1.1.6  christos         DataOp->Asl.Value.String = (char *) ObjDesc->Buffer.Pointer;
    860   1.1.1.6  christos         DataOp->Asl.Parent = Op;
    861   1.1.1.5  christos 
    862   1.1.1.6  christos         LengthOp->Asl.Next = DataOp;
    863   1.1.1.5  christos 
    864   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
    865   1.1.1.5  christos             "Constant expression reduced to (BUFFER) length %X\n\n",
    866   1.1.1.5  christos             ObjDesc->Buffer.Length);
    867   1.1.1.5  christos         break;
    868   1.1.1.5  christos 
    869   1.1.1.5  christos     default:
    870   1.1.1.5  christos         break;
    871   1.1.1.5  christos     }
    872       1.1    jruoho }
    873       1.1    jruoho 
    874   1.1.1.3  christos 
    875   1.1.1.3  christos /*******************************************************************************
    876   1.1.1.3  christos  *
    877   1.1.1.3  christos  * FUNCTION:    OpcUpdateIntegerNode
    878   1.1.1.3  christos  *
    879   1.1.1.3  christos  * PARAMETERS:  Op                  - Current parse object
    880   1.1.1.5  christos  *              Value               - Value for the integer op
    881   1.1.1.3  christos  *
    882   1.1.1.3  christos  * RETURN:      None
    883   1.1.1.3  christos  *
    884   1.1.1.5  christos  * DESCRIPTION: Update node to the correct Integer type and value
    885   1.1.1.3  christos  *
    886   1.1.1.3  christos  ******************************************************************************/
    887   1.1.1.3  christos 
    888   1.1.1.3  christos static void
    889   1.1.1.3  christos OpcUpdateIntegerNode (
    890   1.1.1.3  christos     ACPI_PARSE_OBJECT       *Op,
    891   1.1.1.3  christos     UINT64                  Value)
    892   1.1.1.3  christos {
    893   1.1.1.3  christos 
    894   1.1.1.3  christos     Op->Common.Value.Integer = Value;
    895   1.1.1.3  christos 
    896   1.1.1.3  christos     /*
    897   1.1.1.3  christos      * The AmlLength is used by the parser to indicate a constant,
    898   1.1.1.3  christos      * (if non-zero). Length is either (1/2/4/8)
    899   1.1.1.3  christos      */
    900   1.1.1.3  christos     switch (Op->Asl.AmlLength)
    901   1.1.1.3  christos     {
    902   1.1.1.3  christos     case 1:
    903   1.1.1.3  christos 
    904  1.1.1.10  christos         TrSetOpIntegerValue (PARSEOP_BYTECONST, Op);
    905   1.1.1.3  christos         Op->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
    906   1.1.1.3  christos         break;
    907   1.1.1.3  christos 
    908   1.1.1.3  christos     case 2:
    909   1.1.1.3  christos 
    910  1.1.1.10  christos         TrSetOpIntegerValue (PARSEOP_WORDCONST, Op);
    911   1.1.1.3  christos         Op->Asl.AmlOpcode = AML_RAW_DATA_WORD;
    912   1.1.1.3  christos         break;
    913   1.1.1.3  christos 
    914   1.1.1.3  christos     case 4:
    915   1.1.1.3  christos 
    916  1.1.1.10  christos         TrSetOpIntegerValue (PARSEOP_DWORDCONST, Op);
    917   1.1.1.3  christos         Op->Asl.AmlOpcode = AML_RAW_DATA_DWORD;
    918   1.1.1.3  christos         break;
    919   1.1.1.3  christos 
    920   1.1.1.3  christos     case 8:
    921   1.1.1.3  christos 
    922  1.1.1.10  christos         TrSetOpIntegerValue (PARSEOP_QWORDCONST, Op);
    923   1.1.1.3  christos         Op->Asl.AmlOpcode = AML_RAW_DATA_QWORD;
    924   1.1.1.3  christos         break;
    925   1.1.1.3  christos 
    926   1.1.1.3  christos     case 0:
    927   1.1.1.3  christos     default:
    928   1.1.1.3  christos 
    929   1.1.1.3  christos         OpcSetOptimalIntegerSize (Op);
    930  1.1.1.10  christos         TrSetOpIntegerValue (PARSEOP_INTEGER, Op);
    931   1.1.1.3  christos         break;
    932   1.1.1.3  christos     }
    933   1.1.1.3  christos 
    934   1.1.1.3  christos     Op->Asl.AmlLength = 0;
    935   1.1.1.3  christos }
    936   1.1.1.5  christos 
    937   1.1.1.5  christos 
    938   1.1.1.5  christos /*******************************************************************************
    939   1.1.1.5  christos  *
    940   1.1.1.5  christos  * FUNCTION:    OpcAmlEvaluationWalk1
    941   1.1.1.5  christos  *
    942   1.1.1.5  christos  * PARAMETERS:  ASL_WALK_CALLBACK
    943   1.1.1.5  christos  *
    944   1.1.1.5  christos  * RETURN:      Status
    945   1.1.1.5  christos  *
    946   1.1.1.5  christos  * DESCRIPTION: Descending callback for AML execution of constant subtrees
    947   1.1.1.5  christos  *
    948   1.1.1.5  christos  ******************************************************************************/
    949   1.1.1.5  christos 
    950   1.1.1.5  christos static ACPI_STATUS
    951   1.1.1.5  christos OpcAmlEvaluationWalk1 (
    952   1.1.1.5  christos     ACPI_PARSE_OBJECT       *Op,
    953   1.1.1.5  christos     UINT32                  Level,
    954   1.1.1.5  christos     void                    *Context)
    955   1.1.1.5  christos {
    956   1.1.1.5  christos     ACPI_WALK_STATE         *WalkState = Context;
    957   1.1.1.5  christos     ACPI_STATUS             Status;
    958   1.1.1.5  christos     ACPI_PARSE_OBJECT       *OutOp;
    959   1.1.1.5  christos 
    960   1.1.1.5  christos 
    961   1.1.1.5  christos     WalkState->Op = Op;
    962   1.1.1.5  christos     WalkState->Opcode = Op->Common.AmlOpcode;
    963   1.1.1.5  christos     WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
    964   1.1.1.5  christos 
    965   1.1.1.5  christos     /* Copy child pointer to Arg for compatibility with Interpreter */
    966   1.1.1.5  christos 
    967   1.1.1.5  christos     if (Op->Asl.Child)
    968   1.1.1.5  christos     {
    969   1.1.1.5  christos         Op->Common.Value.Arg = Op->Asl.Child;
    970   1.1.1.5  christos     }
    971   1.1.1.5  christos 
    972   1.1.1.5  christos     /* Call AML dispatcher */
    973   1.1.1.5  christos 
    974   1.1.1.5  christos     Status = AcpiDsExecBeginOp (WalkState, &OutOp);
    975   1.1.1.5  christos     if (ACPI_FAILURE (Status))
    976   1.1.1.5  christos     {
    977   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
    978   1.1.1.5  christos             "%s Constant interpretation failed (1) - %s\n",
    979   1.1.1.5  christos             Op->Asl.ParseOpName, AcpiFormatException (Status));
    980   1.1.1.5  christos     }
    981   1.1.1.5  christos 
    982   1.1.1.5  christos     return (Status);
    983   1.1.1.5  christos }
    984   1.1.1.5  christos 
    985   1.1.1.5  christos 
    986   1.1.1.5  christos /*******************************************************************************
    987   1.1.1.5  christos  *
    988   1.1.1.5  christos  * FUNCTION:    OpcAmlEvaluationWalk2
    989   1.1.1.5  christos  *
    990   1.1.1.5  christos  * PARAMETERS:  ASL_WALK_CALLBACK
    991   1.1.1.5  christos  *
    992   1.1.1.5  christos  * RETURN:      Status
    993   1.1.1.5  christos  *
    994   1.1.1.5  christos  * DESCRIPTION: Ascending callback for AML execution of constant subtrees
    995   1.1.1.5  christos  *
    996   1.1.1.5  christos  ******************************************************************************/
    997   1.1.1.5  christos 
    998   1.1.1.5  christos static ACPI_STATUS
    999   1.1.1.5  christos OpcAmlEvaluationWalk2 (
   1000   1.1.1.5  christos     ACPI_PARSE_OBJECT       *Op,
   1001   1.1.1.5  christos     UINT32                  Level,
   1002   1.1.1.5  christos     void                    *Context)
   1003   1.1.1.5  christos {
   1004   1.1.1.5  christos     ACPI_WALK_STATE         *WalkState = Context;
   1005   1.1.1.5  christos     ACPI_STATUS             Status;
   1006   1.1.1.5  christos 
   1007   1.1.1.5  christos 
   1008   1.1.1.5  christos     WalkState->Op = Op;
   1009   1.1.1.5  christos     WalkState->Opcode = Op->Common.AmlOpcode;
   1010   1.1.1.5  christos     WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
   1011   1.1.1.5  christos 
   1012   1.1.1.5  christos     /* Copy child pointer to Arg for compatibility with Interpreter */
   1013   1.1.1.5  christos 
   1014   1.1.1.5  christos     if (Op->Asl.Child)
   1015   1.1.1.5  christos     {
   1016   1.1.1.5  christos         Op->Common.Value.Arg = Op->Asl.Child;
   1017   1.1.1.5  christos     }
   1018   1.1.1.5  christos 
   1019   1.1.1.5  christos     /* Call AML dispatcher */
   1020   1.1.1.5  christos 
   1021   1.1.1.5  christos     Status = AcpiDsExecEndOp (WalkState);
   1022   1.1.1.5  christos     if (ACPI_FAILURE (Status))
   1023   1.1.1.5  christos     {
   1024   1.1.1.5  christos         DbgPrint (ASL_PARSE_OUTPUT,
   1025   1.1.1.5  christos             "%s: Constant interpretation failed (2) - %s\n",
   1026   1.1.1.5  christos             Op->Asl.ParseOpName, AcpiFormatException (Status));
   1027   1.1.1.5  christos     }
   1028   1.1.1.5  christos 
   1029   1.1.1.5  christos     return (Status);
   1030   1.1.1.5  christos }
   1031