Home | History | Annotate | Line # | Download | only in disassembler
dmcstyle.c revision 1.8.8.2
      1  1.8.8.2  jdolecek /*******************************************************************************
      2  1.8.8.2  jdolecek  *
      3  1.8.8.2  jdolecek  * Module Name: dmcstyle - Support for C-style operator disassembly
      4  1.8.8.2  jdolecek  *
      5  1.8.8.2  jdolecek  ******************************************************************************/
      6  1.8.8.2  jdolecek 
      7  1.8.8.2  jdolecek /*
      8  1.8.8.2  jdolecek  * Copyright (C) 2000 - 2017, Intel Corp.
      9  1.8.8.2  jdolecek  * All rights reserved.
     10  1.8.8.2  jdolecek  *
     11  1.8.8.2  jdolecek  * Redistribution and use in source and binary forms, with or without
     12  1.8.8.2  jdolecek  * modification, are permitted provided that the following conditions
     13  1.8.8.2  jdolecek  * are met:
     14  1.8.8.2  jdolecek  * 1. Redistributions of source code must retain the above copyright
     15  1.8.8.2  jdolecek  *    notice, this list of conditions, and the following disclaimer,
     16  1.8.8.2  jdolecek  *    without modification.
     17  1.8.8.2  jdolecek  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  1.8.8.2  jdolecek  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  1.8.8.2  jdolecek  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  1.8.8.2  jdolecek  *    including a substantially similar Disclaimer requirement for further
     21  1.8.8.2  jdolecek  *    binary redistribution.
     22  1.8.8.2  jdolecek  * 3. Neither the names of the above-listed copyright holders nor the names
     23  1.8.8.2  jdolecek  *    of any contributors may be used to endorse or promote products derived
     24  1.8.8.2  jdolecek  *    from this software without specific prior written permission.
     25  1.8.8.2  jdolecek  *
     26  1.8.8.2  jdolecek  * Alternatively, this software may be distributed under the terms of the
     27  1.8.8.2  jdolecek  * GNU General Public License ("GPL") version 2 as published by the Free
     28  1.8.8.2  jdolecek  * Software Foundation.
     29  1.8.8.2  jdolecek  *
     30  1.8.8.2  jdolecek  * NO WARRANTY
     31  1.8.8.2  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  1.8.8.2  jdolecek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.8.8.2  jdolecek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  1.8.8.2  jdolecek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  1.8.8.2  jdolecek  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  1.8.8.2  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  1.8.8.2  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  1.8.8.2  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  1.8.8.2  jdolecek  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  1.8.8.2  jdolecek  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  1.8.8.2  jdolecek  * POSSIBILITY OF SUCH DAMAGES.
     42  1.8.8.2  jdolecek  */
     43  1.8.8.2  jdolecek 
     44  1.8.8.2  jdolecek #include "acpi.h"
     45  1.8.8.2  jdolecek #include "accommon.h"
     46  1.8.8.2  jdolecek #include "acparser.h"
     47  1.8.8.2  jdolecek #include "amlcode.h"
     48  1.8.8.2  jdolecek #include "acdebug.h"
     49  1.8.8.2  jdolecek #include "acconvert.h"
     50  1.8.8.2  jdolecek 
     51  1.8.8.2  jdolecek 
     52  1.8.8.2  jdolecek #define _COMPONENT          ACPI_CA_DEBUGGER
     53  1.8.8.2  jdolecek         ACPI_MODULE_NAME    ("dmcstyle")
     54  1.8.8.2  jdolecek 
     55  1.8.8.2  jdolecek 
     56  1.8.8.2  jdolecek /* Local prototypes */
     57  1.8.8.2  jdolecek 
     58  1.8.8.2  jdolecek static const char *
     59  1.8.8.2  jdolecek AcpiDmGetCompoundSymbol (
     60  1.8.8.2  jdolecek    UINT16                   AslOpcode);
     61  1.8.8.2  jdolecek 
     62  1.8.8.2  jdolecek static void
     63  1.8.8.2  jdolecek AcpiDmPromoteTarget (
     64  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Op,
     65  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Target);
     66  1.8.8.2  jdolecek 
     67  1.8.8.2  jdolecek static BOOLEAN
     68  1.8.8.2  jdolecek AcpiDmIsValidTarget (
     69  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Op);
     70  1.8.8.2  jdolecek 
     71  1.8.8.2  jdolecek static BOOLEAN
     72  1.8.8.2  jdolecek AcpiDmIsTargetAnOperand (
     73  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Target,
     74  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Operand,
     75  1.8.8.2  jdolecek     BOOLEAN                 TopLevel);
     76  1.8.8.2  jdolecek 
     77  1.8.8.2  jdolecek static BOOLEAN
     78  1.8.8.2  jdolecek AcpiDmIsOptimizationIgnored (
     79  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *StoreOp,
     80  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *StoreArgument);
     81  1.8.8.2  jdolecek 
     82  1.8.8.2  jdolecek 
     83  1.8.8.2  jdolecek /*******************************************************************************
     84  1.8.8.2  jdolecek  *
     85  1.8.8.2  jdolecek  * FUNCTION:    AcpiDmCheckForSymbolicOpcode
     86  1.8.8.2  jdolecek  *
     87  1.8.8.2  jdolecek  * PARAMETERS:  Op                  - Current parse object
     88  1.8.8.2  jdolecek  *              Walk                - Current parse tree walk info
     89  1.8.8.2  jdolecek  *
     90  1.8.8.2  jdolecek  * RETURN:      TRUE if opcode can be converted to symbolic, FALSE otherwise
     91  1.8.8.2  jdolecek  *
     92  1.8.8.2  jdolecek  * DESCRIPTION: This is the main code that implements disassembly of AML code
     93  1.8.8.2  jdolecek  *              to C-style operators. Called during descending phase of the
     94  1.8.8.2  jdolecek  *              parse tree walk.
     95  1.8.8.2  jdolecek  *
     96  1.8.8.2  jdolecek  ******************************************************************************/
     97  1.8.8.2  jdolecek 
     98  1.8.8.2  jdolecek BOOLEAN
     99  1.8.8.2  jdolecek AcpiDmCheckForSymbolicOpcode (
    100  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Op,
    101  1.8.8.2  jdolecek     ACPI_OP_WALK_INFO       *Info)
    102  1.8.8.2  jdolecek {
    103  1.8.8.2  jdolecek     const char              *OperatorSymbol = NULL;
    104  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Argument1;
    105  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Argument2;
    106  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Target;
    107  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Target2;
    108  1.8.8.2  jdolecek 
    109  1.8.8.2  jdolecek 
    110  1.8.8.2  jdolecek     /* Exit immediately if ASL+ not enabled */
    111  1.8.8.2  jdolecek 
    112  1.8.8.2  jdolecek     if (!AcpiGbl_CstyleDisassembly)
    113  1.8.8.2  jdolecek     {
    114  1.8.8.2  jdolecek         return (FALSE);
    115  1.8.8.2  jdolecek     }
    116  1.8.8.2  jdolecek 
    117  1.8.8.2  jdolecek     /* Get the first operand */
    118  1.8.8.2  jdolecek 
    119  1.8.8.2  jdolecek     Argument1 = AcpiPsGetArg (Op, 0);
    120  1.8.8.2  jdolecek     if (!Argument1)
    121  1.8.8.2  jdolecek     {
    122  1.8.8.2  jdolecek         return (FALSE);
    123  1.8.8.2  jdolecek     }
    124  1.8.8.2  jdolecek 
    125  1.8.8.2  jdolecek     /* Get the second operand */
    126  1.8.8.2  jdolecek 
    127  1.8.8.2  jdolecek     Argument2 = Argument1->Common.Next;
    128  1.8.8.2  jdolecek 
    129  1.8.8.2  jdolecek     /* Setup the operator string for this opcode */
    130  1.8.8.2  jdolecek 
    131  1.8.8.2  jdolecek     switch (Op->Common.AmlOpcode)
    132  1.8.8.2  jdolecek     {
    133  1.8.8.2  jdolecek     case AML_ADD_OP:
    134  1.8.8.2  jdolecek         OperatorSymbol = " + ";
    135  1.8.8.2  jdolecek         break;
    136  1.8.8.2  jdolecek 
    137  1.8.8.2  jdolecek     case AML_SUBTRACT_OP:
    138  1.8.8.2  jdolecek         OperatorSymbol = " - ";
    139  1.8.8.2  jdolecek         break;
    140  1.8.8.2  jdolecek 
    141  1.8.8.2  jdolecek     case AML_MULTIPLY_OP:
    142  1.8.8.2  jdolecek         OperatorSymbol = " * ";
    143  1.8.8.2  jdolecek         break;
    144  1.8.8.2  jdolecek 
    145  1.8.8.2  jdolecek     case AML_DIVIDE_OP:
    146  1.8.8.2  jdolecek         OperatorSymbol = " / ";
    147  1.8.8.2  jdolecek         break;
    148  1.8.8.2  jdolecek 
    149  1.8.8.2  jdolecek     case AML_MOD_OP:
    150  1.8.8.2  jdolecek         OperatorSymbol = " % ";
    151  1.8.8.2  jdolecek         break;
    152  1.8.8.2  jdolecek 
    153  1.8.8.2  jdolecek     case AML_SHIFT_LEFT_OP:
    154  1.8.8.2  jdolecek         OperatorSymbol = " << ";
    155  1.8.8.2  jdolecek         break;
    156  1.8.8.2  jdolecek 
    157  1.8.8.2  jdolecek     case AML_SHIFT_RIGHT_OP:
    158  1.8.8.2  jdolecek         OperatorSymbol = " >> ";
    159  1.8.8.2  jdolecek         break;
    160  1.8.8.2  jdolecek 
    161  1.8.8.2  jdolecek     case AML_BIT_AND_OP:
    162  1.8.8.2  jdolecek         OperatorSymbol = " & ";
    163  1.8.8.2  jdolecek         break;
    164  1.8.8.2  jdolecek 
    165  1.8.8.2  jdolecek     case AML_BIT_OR_OP:
    166  1.8.8.2  jdolecek         OperatorSymbol = " | ";
    167  1.8.8.2  jdolecek         break;
    168  1.8.8.2  jdolecek 
    169  1.8.8.2  jdolecek     case AML_BIT_XOR_OP:
    170  1.8.8.2  jdolecek         OperatorSymbol = " ^ ";
    171  1.8.8.2  jdolecek         break;
    172  1.8.8.2  jdolecek 
    173  1.8.8.2  jdolecek     /* Logical operators, no target */
    174  1.8.8.2  jdolecek 
    175  1.8.8.2  jdolecek     case AML_LOGICAL_AND_OP:
    176  1.8.8.2  jdolecek         OperatorSymbol = " && ";
    177  1.8.8.2  jdolecek         break;
    178  1.8.8.2  jdolecek 
    179  1.8.8.2  jdolecek     case AML_LOGICAL_EQUAL_OP:
    180  1.8.8.2  jdolecek         OperatorSymbol = " == ";
    181  1.8.8.2  jdolecek         break;
    182  1.8.8.2  jdolecek 
    183  1.8.8.2  jdolecek     case AML_LOGICAL_GREATER_OP:
    184  1.8.8.2  jdolecek         OperatorSymbol = " > ";
    185  1.8.8.2  jdolecek         break;
    186  1.8.8.2  jdolecek 
    187  1.8.8.2  jdolecek     case AML_LOGICAL_LESS_OP:
    188  1.8.8.2  jdolecek         OperatorSymbol = " < ";
    189  1.8.8.2  jdolecek         break;
    190  1.8.8.2  jdolecek 
    191  1.8.8.2  jdolecek     case AML_LOGICAL_OR_OP:
    192  1.8.8.2  jdolecek         OperatorSymbol = " || ";
    193  1.8.8.2  jdolecek         break;
    194  1.8.8.2  jdolecek 
    195  1.8.8.2  jdolecek     case AML_LOGICAL_NOT_OP:
    196  1.8.8.2  jdolecek         /*
    197  1.8.8.2  jdolecek          * Check for the LNOT sub-opcodes. These correspond to
    198  1.8.8.2  jdolecek          * LNotEqual, LLessEqual, and LGreaterEqual. There are
    199  1.8.8.2  jdolecek          * no actual AML opcodes for these operators.
    200  1.8.8.2  jdolecek          */
    201  1.8.8.2  jdolecek         switch (Argument1->Common.AmlOpcode)
    202  1.8.8.2  jdolecek         {
    203  1.8.8.2  jdolecek         case AML_LOGICAL_EQUAL_OP:
    204  1.8.8.2  jdolecek             OperatorSymbol = " != ";
    205  1.8.8.2  jdolecek             break;
    206  1.8.8.2  jdolecek 
    207  1.8.8.2  jdolecek         case AML_LOGICAL_GREATER_OP:
    208  1.8.8.2  jdolecek             OperatorSymbol = " <= ";
    209  1.8.8.2  jdolecek             break;
    210  1.8.8.2  jdolecek 
    211  1.8.8.2  jdolecek         case AML_LOGICAL_LESS_OP:
    212  1.8.8.2  jdolecek             OperatorSymbol = " >= ";
    213  1.8.8.2  jdolecek             break;
    214  1.8.8.2  jdolecek 
    215  1.8.8.2  jdolecek         default:
    216  1.8.8.2  jdolecek 
    217  1.8.8.2  jdolecek             /* Unary LNOT case, emit "!" immediately */
    218  1.8.8.2  jdolecek 
    219  1.8.8.2  jdolecek             AcpiOsPrintf ("!");
    220  1.8.8.2  jdolecek             return (TRUE);
    221  1.8.8.2  jdolecek         }
    222  1.8.8.2  jdolecek 
    223  1.8.8.2  jdolecek         Argument1->Common.DisasmOpcode = ACPI_DASM_LNOT_SUFFIX;
    224  1.8.8.2  jdolecek         Op->Common.DisasmOpcode = ACPI_DASM_LNOT_PREFIX;
    225  1.8.8.2  jdolecek 
    226  1.8.8.2  jdolecek         /* Save symbol string in the next child (not peer) */
    227  1.8.8.2  jdolecek 
    228  1.8.8.2  jdolecek         Argument2 = AcpiPsGetArg (Argument1, 0);
    229  1.8.8.2  jdolecek         if (!Argument2)
    230  1.8.8.2  jdolecek         {
    231  1.8.8.2  jdolecek             return (FALSE);
    232  1.8.8.2  jdolecek         }
    233  1.8.8.2  jdolecek 
    234  1.8.8.2  jdolecek         Argument2->Common.OperatorSymbol = OperatorSymbol;
    235  1.8.8.2  jdolecek         return (TRUE);
    236  1.8.8.2  jdolecek 
    237  1.8.8.2  jdolecek     case AML_INDEX_OP:
    238  1.8.8.2  jdolecek         /*
    239  1.8.8.2  jdolecek          * Check for constant source operand. Note: although technically
    240  1.8.8.2  jdolecek          * legal syntax, the iASL compiler does not support this with
    241  1.8.8.2  jdolecek          * the symbolic operators for Index(). It doesn't make sense to
    242  1.8.8.2  jdolecek          * use Index() with a constant anyway.
    243  1.8.8.2  jdolecek          */
    244  1.8.8.2  jdolecek         if ((Argument1->Common.AmlOpcode == AML_STRING_OP)  ||
    245  1.8.8.2  jdolecek             (Argument1->Common.AmlOpcode == AML_BUFFER_OP)  ||
    246  1.8.8.2  jdolecek             (Argument1->Common.AmlOpcode == AML_PACKAGE_OP) ||
    247  1.8.8.2  jdolecek             (Argument1->Common.AmlOpcode == AML_VARIABLE_PACKAGE_OP))
    248  1.8.8.2  jdolecek         {
    249  1.8.8.2  jdolecek             Op->Common.DisasmFlags |= ACPI_PARSEOP_CLOSING_PAREN;
    250  1.8.8.2  jdolecek             return (FALSE);
    251  1.8.8.2  jdolecek         }
    252  1.8.8.2  jdolecek 
    253  1.8.8.2  jdolecek         /* Index operator is [] */
    254  1.8.8.2  jdolecek 
    255  1.8.8.2  jdolecek         Argument1->Common.OperatorSymbol = " [";
    256  1.8.8.2  jdolecek         Argument2->Common.OperatorSymbol = "]";
    257  1.8.8.2  jdolecek         break;
    258  1.8.8.2  jdolecek 
    259  1.8.8.2  jdolecek     /* Unary operators */
    260  1.8.8.2  jdolecek 
    261  1.8.8.2  jdolecek     case AML_DECREMENT_OP:
    262  1.8.8.2  jdolecek         OperatorSymbol = "--";
    263  1.8.8.2  jdolecek         break;
    264  1.8.8.2  jdolecek 
    265  1.8.8.2  jdolecek     case AML_INCREMENT_OP:
    266  1.8.8.2  jdolecek         OperatorSymbol = "++";
    267  1.8.8.2  jdolecek         break;
    268  1.8.8.2  jdolecek 
    269  1.8.8.2  jdolecek     case AML_BIT_NOT_OP:
    270  1.8.8.2  jdolecek     case AML_STORE_OP:
    271  1.8.8.2  jdolecek         OperatorSymbol = NULL;
    272  1.8.8.2  jdolecek         break;
    273  1.8.8.2  jdolecek 
    274  1.8.8.2  jdolecek     default:
    275  1.8.8.2  jdolecek         return (FALSE);
    276  1.8.8.2  jdolecek     }
    277  1.8.8.2  jdolecek 
    278  1.8.8.2  jdolecek     if (Argument1->Common.DisasmOpcode == ACPI_DASM_LNOT_SUFFIX)
    279  1.8.8.2  jdolecek     {
    280  1.8.8.2  jdolecek         return (TRUE);
    281  1.8.8.2  jdolecek     }
    282  1.8.8.2  jdolecek 
    283  1.8.8.2  jdolecek     /*
    284  1.8.8.2  jdolecek      * This is the key to how the disassembly of the C-style operators
    285  1.8.8.2  jdolecek      * works. We save the operator symbol in the first child, thus
    286  1.8.8.2  jdolecek      * deferring symbol output until after the first operand has been
    287  1.8.8.2  jdolecek      * emitted.
    288  1.8.8.2  jdolecek      */
    289  1.8.8.2  jdolecek     if (!Argument1->Common.OperatorSymbol)
    290  1.8.8.2  jdolecek     {
    291  1.8.8.2  jdolecek         Argument1->Common.OperatorSymbol = OperatorSymbol;
    292  1.8.8.2  jdolecek     }
    293  1.8.8.2  jdolecek 
    294  1.8.8.2  jdolecek     /*
    295  1.8.8.2  jdolecek      * Check for a valid target as the 3rd (or sometimes 2nd) operand
    296  1.8.8.2  jdolecek      *
    297  1.8.8.2  jdolecek      * Compound assignment operator support:
    298  1.8.8.2  jdolecek      * Attempt to optimize constructs of the form:
    299  1.8.8.2  jdolecek      *      Add (Local1, 0xFF, Local1)
    300  1.8.8.2  jdolecek      * to:
    301  1.8.8.2  jdolecek      *      Local1 += 0xFF
    302  1.8.8.2  jdolecek      *
    303  1.8.8.2  jdolecek      * Only the math operators and Store() have a target.
    304  1.8.8.2  jdolecek      * Logicals have no target.
    305  1.8.8.2  jdolecek      */
    306  1.8.8.2  jdolecek     switch (Op->Common.AmlOpcode)
    307  1.8.8.2  jdolecek     {
    308  1.8.8.2  jdolecek     case AML_ADD_OP:
    309  1.8.8.2  jdolecek     case AML_SUBTRACT_OP:
    310  1.8.8.2  jdolecek     case AML_MULTIPLY_OP:
    311  1.8.8.2  jdolecek     case AML_DIVIDE_OP:
    312  1.8.8.2  jdolecek     case AML_MOD_OP:
    313  1.8.8.2  jdolecek     case AML_SHIFT_LEFT_OP:
    314  1.8.8.2  jdolecek     case AML_SHIFT_RIGHT_OP:
    315  1.8.8.2  jdolecek     case AML_BIT_AND_OP:
    316  1.8.8.2  jdolecek     case AML_BIT_OR_OP:
    317  1.8.8.2  jdolecek     case AML_BIT_XOR_OP:
    318  1.8.8.2  jdolecek 
    319  1.8.8.2  jdolecek         /* Target is 3rd operand */
    320  1.8.8.2  jdolecek 
    321  1.8.8.2  jdolecek         Target = Argument2->Common.Next;
    322  1.8.8.2  jdolecek         if (Op->Common.AmlOpcode == AML_DIVIDE_OP)
    323  1.8.8.2  jdolecek         {
    324  1.8.8.2  jdolecek             Target2 = Target->Common.Next;
    325  1.8.8.2  jdolecek 
    326  1.8.8.2  jdolecek             /*
    327  1.8.8.2  jdolecek              * Divide has an extra target operand (Remainder).
    328  1.8.8.2  jdolecek              * Default behavior is to simply ignore ASL+ conversion
    329  1.8.8.2  jdolecek              * if the remainder target (modulo) is specified.
    330  1.8.8.2  jdolecek              */
    331  1.8.8.2  jdolecek             if (!AcpiGbl_DoDisassemblerOptimizations)
    332  1.8.8.2  jdolecek             {
    333  1.8.8.2  jdolecek                 if (AcpiDmIsValidTarget (Target))
    334  1.8.8.2  jdolecek                 {
    335  1.8.8.2  jdolecek                     Argument1->Common.OperatorSymbol = NULL;
    336  1.8.8.2  jdolecek                     Op->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
    337  1.8.8.2  jdolecek                     return (FALSE);
    338  1.8.8.2  jdolecek                 }
    339  1.8.8.2  jdolecek 
    340  1.8.8.2  jdolecek                 Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
    341  1.8.8.2  jdolecek                 Target = Target2;
    342  1.8.8.2  jdolecek             }
    343  1.8.8.2  jdolecek             else
    344  1.8.8.2  jdolecek             {
    345  1.8.8.2  jdolecek                 /*
    346  1.8.8.2  jdolecek                  * Divide has an extra target operand (Remainder).
    347  1.8.8.2  jdolecek                  * If both targets are specified, it cannot be converted
    348  1.8.8.2  jdolecek                  * to a C-style operator.
    349  1.8.8.2  jdolecek                  */
    350  1.8.8.2  jdolecek                 if (AcpiDmIsValidTarget (Target) &&
    351  1.8.8.2  jdolecek                     AcpiDmIsValidTarget (Target2))
    352  1.8.8.2  jdolecek                 {
    353  1.8.8.2  jdolecek                     Argument1->Common.OperatorSymbol = NULL;
    354  1.8.8.2  jdolecek                     Op->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
    355  1.8.8.2  jdolecek                     return (FALSE);
    356  1.8.8.2  jdolecek                 }
    357  1.8.8.2  jdolecek 
    358  1.8.8.2  jdolecek                 if (AcpiDmIsValidTarget (Target)) /* Only first Target is valid (remainder) */
    359  1.8.8.2  jdolecek                 {
    360  1.8.8.2  jdolecek                     /* Convert the Divide to Modulo */
    361  1.8.8.2  jdolecek 
    362  1.8.8.2  jdolecek                     Op->Common.AmlOpcode = AML_MOD_OP;
    363  1.8.8.2  jdolecek 
    364  1.8.8.2  jdolecek                     Argument1->Common.OperatorSymbol = " % ";
    365  1.8.8.2  jdolecek                     Target2->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
    366  1.8.8.2  jdolecek                 }
    367  1.8.8.2  jdolecek                 else /* Only second Target (quotient) is valid */
    368  1.8.8.2  jdolecek                 {
    369  1.8.8.2  jdolecek                     Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
    370  1.8.8.2  jdolecek                     Target = Target2;
    371  1.8.8.2  jdolecek                 }
    372  1.8.8.2  jdolecek             }
    373  1.8.8.2  jdolecek         }
    374  1.8.8.2  jdolecek 
    375  1.8.8.2  jdolecek         /* Parser should ensure there is at least a placeholder target */
    376  1.8.8.2  jdolecek 
    377  1.8.8.2  jdolecek         if (!Target)
    378  1.8.8.2  jdolecek         {
    379  1.8.8.2  jdolecek             return (FALSE);
    380  1.8.8.2  jdolecek         }
    381  1.8.8.2  jdolecek 
    382  1.8.8.2  jdolecek         if (!AcpiDmIsValidTarget (Target))
    383  1.8.8.2  jdolecek         {
    384  1.8.8.2  jdolecek             /* Not a valid target (placeholder only, from parser) */
    385  1.8.8.2  jdolecek             break;
    386  1.8.8.2  jdolecek         }
    387  1.8.8.2  jdolecek 
    388  1.8.8.2  jdolecek         /*
    389  1.8.8.2  jdolecek          * Promote the target up to the first child in the parse
    390  1.8.8.2  jdolecek          * tree. This is done because the target will be output
    391  1.8.8.2  jdolecek          * first, in the form:
    392  1.8.8.2  jdolecek          *     <Target> = Operands...
    393  1.8.8.2  jdolecek          */
    394  1.8.8.2  jdolecek         AcpiDmPromoteTarget (Op, Target);
    395  1.8.8.2  jdolecek 
    396  1.8.8.2  jdolecek         /* Check operands for conversion to a "Compound Assignment" */
    397  1.8.8.2  jdolecek 
    398  1.8.8.2  jdolecek         switch (Op->Common.AmlOpcode)
    399  1.8.8.2  jdolecek         {
    400  1.8.8.2  jdolecek             /* Commutative operators */
    401  1.8.8.2  jdolecek 
    402  1.8.8.2  jdolecek         case AML_ADD_OP:
    403  1.8.8.2  jdolecek         case AML_MULTIPLY_OP:
    404  1.8.8.2  jdolecek         case AML_BIT_AND_OP:
    405  1.8.8.2  jdolecek         case AML_BIT_OR_OP:
    406  1.8.8.2  jdolecek         case AML_BIT_XOR_OP:
    407  1.8.8.2  jdolecek             /*
    408  1.8.8.2  jdolecek              * For the commutative operators, we can convert to a
    409  1.8.8.2  jdolecek              * compound statement only if at least one (either) operand
    410  1.8.8.2  jdolecek              * is the same as the target.
    411  1.8.8.2  jdolecek              *
    412  1.8.8.2  jdolecek              *      Add (A, B, A) --> A += B
    413  1.8.8.2  jdolecek              *      Add (B, A, A) --> A += B
    414  1.8.8.2  jdolecek              *      Add (B, C, A) --> A = (B + C)
    415  1.8.8.2  jdolecek              */
    416  1.8.8.2  jdolecek             if ((AcpiDmIsTargetAnOperand (Target, Argument1, TRUE)) ||
    417  1.8.8.2  jdolecek                 (AcpiDmIsTargetAnOperand (Target, Argument2, TRUE)))
    418  1.8.8.2  jdolecek             {
    419  1.8.8.2  jdolecek                 Target->Common.OperatorSymbol =
    420  1.8.8.2  jdolecek                     AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
    421  1.8.8.2  jdolecek 
    422  1.8.8.2  jdolecek                 /* Convert operator to compound assignment */
    423  1.8.8.2  jdolecek 
    424  1.8.8.2  jdolecek                 Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
    425  1.8.8.2  jdolecek                 Argument1->Common.OperatorSymbol = NULL;
    426  1.8.8.2  jdolecek                 return (TRUE);
    427  1.8.8.2  jdolecek             }
    428  1.8.8.2  jdolecek             break;
    429  1.8.8.2  jdolecek 
    430  1.8.8.2  jdolecek             /* Non-commutative operators */
    431  1.8.8.2  jdolecek 
    432  1.8.8.2  jdolecek         case AML_SUBTRACT_OP:
    433  1.8.8.2  jdolecek         case AML_DIVIDE_OP:
    434  1.8.8.2  jdolecek         case AML_MOD_OP:
    435  1.8.8.2  jdolecek         case AML_SHIFT_LEFT_OP:
    436  1.8.8.2  jdolecek         case AML_SHIFT_RIGHT_OP:
    437  1.8.8.2  jdolecek             /*
    438  1.8.8.2  jdolecek              * For the non-commutative operators, we can convert to a
    439  1.8.8.2  jdolecek              * compound statement only if the target is the same as the
    440  1.8.8.2  jdolecek              * first operand.
    441  1.8.8.2  jdolecek              *
    442  1.8.8.2  jdolecek              *      Subtract (A, B, A) --> A -= B
    443  1.8.8.2  jdolecek              *      Subtract (B, A, A) --> A = (B - A)
    444  1.8.8.2  jdolecek              */
    445  1.8.8.2  jdolecek             if ((AcpiDmIsTargetAnOperand (Target, Argument1, TRUE)))
    446  1.8.8.2  jdolecek             {
    447  1.8.8.2  jdolecek                 Target->Common.OperatorSymbol =
    448  1.8.8.2  jdolecek                     AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
    449  1.8.8.2  jdolecek 
    450  1.8.8.2  jdolecek                 /* Convert operator to compound assignment */
    451  1.8.8.2  jdolecek 
    452  1.8.8.2  jdolecek                 Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
    453  1.8.8.2  jdolecek                 Argument1->Common.OperatorSymbol = NULL;
    454  1.8.8.2  jdolecek                 return (TRUE);
    455  1.8.8.2  jdolecek             }
    456  1.8.8.2  jdolecek             break;
    457  1.8.8.2  jdolecek 
    458  1.8.8.2  jdolecek         default:
    459  1.8.8.2  jdolecek             break;
    460  1.8.8.2  jdolecek         }
    461  1.8.8.2  jdolecek 
    462  1.8.8.2  jdolecek         /*
    463  1.8.8.2  jdolecek          * If we are within a C-style expression, emit an extra open
    464  1.8.8.2  jdolecek          * paren. Implemented by examining the parent op.
    465  1.8.8.2  jdolecek          */
    466  1.8.8.2  jdolecek         switch (Op->Common.Parent->Common.AmlOpcode)
    467  1.8.8.2  jdolecek         {
    468  1.8.8.2  jdolecek         case AML_ADD_OP:
    469  1.8.8.2  jdolecek         case AML_SUBTRACT_OP:
    470  1.8.8.2  jdolecek         case AML_MULTIPLY_OP:
    471  1.8.8.2  jdolecek         case AML_DIVIDE_OP:
    472  1.8.8.2  jdolecek         case AML_MOD_OP:
    473  1.8.8.2  jdolecek         case AML_SHIFT_LEFT_OP:
    474  1.8.8.2  jdolecek         case AML_SHIFT_RIGHT_OP:
    475  1.8.8.2  jdolecek         case AML_BIT_AND_OP:
    476  1.8.8.2  jdolecek         case AML_BIT_OR_OP:
    477  1.8.8.2  jdolecek         case AML_BIT_XOR_OP:
    478  1.8.8.2  jdolecek         case AML_LOGICAL_AND_OP:
    479  1.8.8.2  jdolecek         case AML_LOGICAL_EQUAL_OP:
    480  1.8.8.2  jdolecek         case AML_LOGICAL_GREATER_OP:
    481  1.8.8.2  jdolecek         case AML_LOGICAL_LESS_OP:
    482  1.8.8.2  jdolecek         case AML_LOGICAL_OR_OP:
    483  1.8.8.2  jdolecek 
    484  1.8.8.2  jdolecek             Op->Common.DisasmFlags |= ACPI_PARSEOP_ASSIGNMENT;
    485  1.8.8.2  jdolecek             AcpiOsPrintf ("(");
    486  1.8.8.2  jdolecek             break;
    487  1.8.8.2  jdolecek 
    488  1.8.8.2  jdolecek         default:
    489  1.8.8.2  jdolecek             break;
    490  1.8.8.2  jdolecek         }
    491  1.8.8.2  jdolecek 
    492  1.8.8.2  jdolecek         /* Normal output for ASL/AML operators with a target operand */
    493  1.8.8.2  jdolecek 
    494  1.8.8.2  jdolecek         Target->Common.OperatorSymbol = " = (";
    495  1.8.8.2  jdolecek         return (TRUE);
    496  1.8.8.2  jdolecek 
    497  1.8.8.2  jdolecek     /* Binary operators, no parens */
    498  1.8.8.2  jdolecek 
    499  1.8.8.2  jdolecek     case AML_DECREMENT_OP:
    500  1.8.8.2  jdolecek     case AML_INCREMENT_OP:
    501  1.8.8.2  jdolecek         return (TRUE);
    502  1.8.8.2  jdolecek 
    503  1.8.8.2  jdolecek     case AML_INDEX_OP:
    504  1.8.8.2  jdolecek 
    505  1.8.8.2  jdolecek         /* Target is optional, 3rd operand */
    506  1.8.8.2  jdolecek 
    507  1.8.8.2  jdolecek         Target = Argument2->Common.Next;
    508  1.8.8.2  jdolecek         if (AcpiDmIsValidTarget (Target))
    509  1.8.8.2  jdolecek         {
    510  1.8.8.2  jdolecek             AcpiDmPromoteTarget (Op, Target);
    511  1.8.8.2  jdolecek 
    512  1.8.8.2  jdolecek             if (!Target->Common.OperatorSymbol)
    513  1.8.8.2  jdolecek             {
    514  1.8.8.2  jdolecek                 Target->Common.OperatorSymbol = " = ";
    515  1.8.8.2  jdolecek             }
    516  1.8.8.2  jdolecek         }
    517  1.8.8.2  jdolecek         return (TRUE);
    518  1.8.8.2  jdolecek 
    519  1.8.8.2  jdolecek     case AML_STORE_OP:
    520  1.8.8.2  jdolecek         /*
    521  1.8.8.2  jdolecek          * For Store, the Target is the 2nd operand. We know the target
    522  1.8.8.2  jdolecek          * is valid, because it is not optional.
    523  1.8.8.2  jdolecek          *
    524  1.8.8.2  jdolecek          * Ignore any optimizations/folding if flag is set.
    525  1.8.8.2  jdolecek          * Used for iASL/disassembler test suite only.
    526  1.8.8.2  jdolecek          */
    527  1.8.8.2  jdolecek         if (AcpiDmIsOptimizationIgnored (Op, Argument1))
    528  1.8.8.2  jdolecek         {
    529  1.8.8.2  jdolecek             return (FALSE);
    530  1.8.8.2  jdolecek         }
    531  1.8.8.2  jdolecek 
    532  1.8.8.2  jdolecek         /*
    533  1.8.8.2  jdolecek          * Perform conversion.
    534  1.8.8.2  jdolecek          * In the parse tree, simply swap the target with the
    535  1.8.8.2  jdolecek          * source so that the target is processed first.
    536  1.8.8.2  jdolecek          */
    537  1.8.8.2  jdolecek         Target = Argument1->Common.Next;
    538  1.8.8.2  jdolecek         if (!Target)
    539  1.8.8.2  jdolecek         {
    540  1.8.8.2  jdolecek             return (FALSE);
    541  1.8.8.2  jdolecek         }
    542  1.8.8.2  jdolecek 
    543  1.8.8.2  jdolecek         AcpiDmPromoteTarget (Op, Target);
    544  1.8.8.2  jdolecek         if (!Target->Common.OperatorSymbol)
    545  1.8.8.2  jdolecek         {
    546  1.8.8.2  jdolecek             Target->Common.OperatorSymbol = " = ";
    547  1.8.8.2  jdolecek         }
    548  1.8.8.2  jdolecek         return (TRUE);
    549  1.8.8.2  jdolecek 
    550  1.8.8.2  jdolecek     case AML_BIT_NOT_OP:
    551  1.8.8.2  jdolecek 
    552  1.8.8.2  jdolecek         /* Target is optional, 2nd operand */
    553  1.8.8.2  jdolecek 
    554  1.8.8.2  jdolecek         Target = Argument1->Common.Next;
    555  1.8.8.2  jdolecek         if (!Target)
    556  1.8.8.2  jdolecek         {
    557  1.8.8.2  jdolecek             return (FALSE);
    558  1.8.8.2  jdolecek         }
    559  1.8.8.2  jdolecek 
    560  1.8.8.2  jdolecek         if (AcpiDmIsValidTarget (Target))
    561  1.8.8.2  jdolecek         {
    562  1.8.8.2  jdolecek             /* Valid target, not a placeholder */
    563  1.8.8.2  jdolecek 
    564  1.8.8.2  jdolecek             AcpiDmPromoteTarget (Op, Target);
    565  1.8.8.2  jdolecek             Target->Common.OperatorSymbol = " = ~";
    566  1.8.8.2  jdolecek         }
    567  1.8.8.2  jdolecek         else
    568  1.8.8.2  jdolecek         {
    569  1.8.8.2  jdolecek             /* No target. Emit this prefix operator immediately */
    570  1.8.8.2  jdolecek 
    571  1.8.8.2  jdolecek             AcpiOsPrintf ("~");
    572  1.8.8.2  jdolecek         }
    573  1.8.8.2  jdolecek         return (TRUE);
    574  1.8.8.2  jdolecek 
    575  1.8.8.2  jdolecek     default:
    576  1.8.8.2  jdolecek         break;
    577  1.8.8.2  jdolecek     }
    578  1.8.8.2  jdolecek 
    579  1.8.8.2  jdolecek     /* All other operators, emit an open paren */
    580  1.8.8.2  jdolecek 
    581  1.8.8.2  jdolecek     AcpiOsPrintf ("(");
    582  1.8.8.2  jdolecek     return (TRUE);
    583  1.8.8.2  jdolecek }
    584  1.8.8.2  jdolecek 
    585  1.8.8.2  jdolecek 
    586  1.8.8.2  jdolecek /*******************************************************************************
    587  1.8.8.2  jdolecek  *
    588  1.8.8.2  jdolecek  * FUNCTION:    AcpiDmIsOptimizationIgnored
    589  1.8.8.2  jdolecek  *
    590  1.8.8.2  jdolecek  * PARAMETERS:  StoreOp             - Store operator parse object
    591  1.8.8.2  jdolecek  *              StoreArgument       - Target associate with the Op
    592  1.8.8.2  jdolecek  *
    593  1.8.8.2  jdolecek  * RETURN:      TRUE if this Store operator should not be converted/removed.
    594  1.8.8.2  jdolecek  *
    595  1.8.8.2  jdolecek  * DESCRIPTION: The following function implements "Do not optimize if a
    596  1.8.8.2  jdolecek  *              store is immediately followed by a math/bit operator that
    597  1.8.8.2  jdolecek  *              has no target".
    598  1.8.8.2  jdolecek  *
    599  1.8.8.2  jdolecek  *              Function is ignored if DoDisassemblerOptimizations is TRUE.
    600  1.8.8.2  jdolecek  *              This is the default, ignore this function.
    601  1.8.8.2  jdolecek  *
    602  1.8.8.2  jdolecek  *              Disables these types of optimizations, and simply emits
    603  1.8.8.2  jdolecek  *              legacy ASL code:
    604  1.8.8.2  jdolecek  *                  Store (Add (INT1, 4), INT2) --> Add (INT1, 4, INT2)
    605  1.8.8.2  jdolecek  *                                              --> INT2 = INT1 + 4
    606  1.8.8.2  jdolecek  *
    607  1.8.8.2  jdolecek  *                  Store (Not (INT1), INT2)    --> Not (INT1, INT2)
    608  1.8.8.2  jdolecek  *                                              --> INT2 = ~INT1
    609  1.8.8.2  jdolecek  *
    610  1.8.8.2  jdolecek  *              Used only for the ASL test suite. For the test suite, we
    611  1.8.8.2  jdolecek  *              don't want to perform some optimizations to ensure binary
    612  1.8.8.2  jdolecek  *              compatibility with the generation of the legacy ASL->AML.
    613  1.8.8.2  jdolecek  *              In other words, for all test modules we want exactly:
    614  1.8.8.2  jdolecek  *                  (ASL+ -> AML) == (ASL- -> AML)
    615  1.8.8.2  jdolecek  *
    616  1.8.8.2  jdolecek  ******************************************************************************/
    617  1.8.8.2  jdolecek 
    618  1.8.8.2  jdolecek static BOOLEAN
    619  1.8.8.2  jdolecek AcpiDmIsOptimizationIgnored (
    620  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *StoreOp,
    621  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *StoreArgument)
    622  1.8.8.2  jdolecek {
    623  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Argument1;
    624  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Argument2;
    625  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Target;
    626  1.8.8.2  jdolecek 
    627  1.8.8.2  jdolecek 
    628  1.8.8.2  jdolecek     /* No optimizations/folding for the typical case */
    629  1.8.8.2  jdolecek 
    630  1.8.8.2  jdolecek     if (AcpiGbl_DoDisassemblerOptimizations)
    631  1.8.8.2  jdolecek     {
    632  1.8.8.2  jdolecek         return (FALSE);
    633  1.8.8.2  jdolecek     }
    634  1.8.8.2  jdolecek 
    635  1.8.8.2  jdolecek     /*
    636  1.8.8.2  jdolecek      * Only a small subset of ASL/AML operators can be optimized.
    637  1.8.8.2  jdolecek      * Can only optimize/fold if there is no target (or targets)
    638  1.8.8.2  jdolecek      * specified for the operator. And of course, the operator
    639  1.8.8.2  jdolecek      * is surrrounded by a Store() operator.
    640  1.8.8.2  jdolecek      */
    641  1.8.8.2  jdolecek     switch (StoreArgument->Common.AmlOpcode)
    642  1.8.8.2  jdolecek     {
    643  1.8.8.2  jdolecek     case AML_ADD_OP:
    644  1.8.8.2  jdolecek     case AML_SUBTRACT_OP:
    645  1.8.8.2  jdolecek     case AML_MULTIPLY_OP:
    646  1.8.8.2  jdolecek     case AML_MOD_OP:
    647  1.8.8.2  jdolecek     case AML_SHIFT_LEFT_OP:
    648  1.8.8.2  jdolecek     case AML_SHIFT_RIGHT_OP:
    649  1.8.8.2  jdolecek     case AML_BIT_AND_OP:
    650  1.8.8.2  jdolecek     case AML_BIT_OR_OP:
    651  1.8.8.2  jdolecek     case AML_BIT_XOR_OP:
    652  1.8.8.2  jdolecek     case AML_INDEX_OP:
    653  1.8.8.2  jdolecek 
    654  1.8.8.2  jdolecek         /* These operators have two arguments and one target */
    655  1.8.8.2  jdolecek 
    656  1.8.8.2  jdolecek         Argument1 = StoreArgument->Common.Value.Arg;
    657  1.8.8.2  jdolecek         Argument2 = Argument1->Common.Next;
    658  1.8.8.2  jdolecek         Target = Argument2->Common.Next;
    659  1.8.8.2  jdolecek 
    660  1.8.8.2  jdolecek         if (!AcpiDmIsValidTarget (Target))
    661  1.8.8.2  jdolecek         {
    662  1.8.8.2  jdolecek             StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
    663  1.8.8.2  jdolecek             return (TRUE);
    664  1.8.8.2  jdolecek         }
    665  1.8.8.2  jdolecek         break;
    666  1.8.8.2  jdolecek 
    667  1.8.8.2  jdolecek     case AML_DIVIDE_OP:
    668  1.8.8.2  jdolecek 
    669  1.8.8.2  jdolecek         /* This operator has two arguments and two targets */
    670  1.8.8.2  jdolecek 
    671  1.8.8.2  jdolecek         Argument1 = StoreArgument->Common.Value.Arg;
    672  1.8.8.2  jdolecek         Argument2 = Argument1->Common.Next;
    673  1.8.8.2  jdolecek         Target = Argument2->Common.Next;
    674  1.8.8.2  jdolecek 
    675  1.8.8.2  jdolecek         if (!AcpiDmIsValidTarget (Target) ||
    676  1.8.8.2  jdolecek             !AcpiDmIsValidTarget (Target->Common.Next))
    677  1.8.8.2  jdolecek         {
    678  1.8.8.2  jdolecek             StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
    679  1.8.8.2  jdolecek             return (TRUE);
    680  1.8.8.2  jdolecek         }
    681  1.8.8.2  jdolecek         break;
    682  1.8.8.2  jdolecek 
    683  1.8.8.2  jdolecek     case AML_BIT_NOT_OP:
    684  1.8.8.2  jdolecek 
    685  1.8.8.2  jdolecek         /* This operator has one operand and one target */
    686  1.8.8.2  jdolecek 
    687  1.8.8.2  jdolecek         Argument1 = StoreArgument->Common.Value.Arg;
    688  1.8.8.2  jdolecek         Target = Argument1->Common.Next;
    689  1.8.8.2  jdolecek 
    690  1.8.8.2  jdolecek         if (!AcpiDmIsValidTarget (Target))
    691  1.8.8.2  jdolecek         {
    692  1.8.8.2  jdolecek             StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
    693  1.8.8.2  jdolecek             return (TRUE);
    694  1.8.8.2  jdolecek         }
    695  1.8.8.2  jdolecek         break;
    696  1.8.8.2  jdolecek 
    697  1.8.8.2  jdolecek     default:
    698  1.8.8.2  jdolecek         break;
    699  1.8.8.2  jdolecek     }
    700  1.8.8.2  jdolecek 
    701  1.8.8.2  jdolecek     return (FALSE);
    702  1.8.8.2  jdolecek }
    703  1.8.8.2  jdolecek 
    704  1.8.8.2  jdolecek 
    705  1.8.8.2  jdolecek /*******************************************************************************
    706  1.8.8.2  jdolecek  *
    707  1.8.8.2  jdolecek  * FUNCTION:    AcpiDmCloseOperator
    708  1.8.8.2  jdolecek  *
    709  1.8.8.2  jdolecek  * PARAMETERS:  Op                  - Current parse object
    710  1.8.8.2  jdolecek  *
    711  1.8.8.2  jdolecek  * RETURN:      None
    712  1.8.8.2  jdolecek  *
    713  1.8.8.2  jdolecek  * DESCRIPTION: Closes an operator by adding a closing parentheses if and
    714  1.8.8.2  jdolecek  *              when necessary. Called during ascending phase of the
    715  1.8.8.2  jdolecek  *              parse tree walk.
    716  1.8.8.2  jdolecek  *
    717  1.8.8.2  jdolecek  ******************************************************************************/
    718  1.8.8.2  jdolecek 
    719  1.8.8.2  jdolecek void
    720  1.8.8.2  jdolecek AcpiDmCloseOperator (
    721  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Op)
    722  1.8.8.2  jdolecek {
    723  1.8.8.2  jdolecek 
    724  1.8.8.2  jdolecek     /* Always emit paren if ASL+ disassembly disabled */
    725  1.8.8.2  jdolecek 
    726  1.8.8.2  jdolecek     if (!AcpiGbl_CstyleDisassembly)
    727  1.8.8.2  jdolecek     {
    728  1.8.8.2  jdolecek         AcpiOsPrintf (")");
    729  1.8.8.2  jdolecek         ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
    730  1.8.8.2  jdolecek         return;
    731  1.8.8.2  jdolecek     }
    732  1.8.8.2  jdolecek 
    733  1.8.8.2  jdolecek     if (Op->Common.DisasmFlags & ACPI_PARSEOP_LEGACY_ASL_ONLY)
    734  1.8.8.2  jdolecek     {
    735  1.8.8.2  jdolecek         AcpiOsPrintf (")");
    736  1.8.8.2  jdolecek         ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
    737  1.8.8.2  jdolecek         return;
    738  1.8.8.2  jdolecek     }
    739  1.8.8.2  jdolecek 
    740  1.8.8.2  jdolecek     /* Check if we need to add an additional closing paren */
    741  1.8.8.2  jdolecek 
    742  1.8.8.2  jdolecek     switch (Op->Common.AmlOpcode)
    743  1.8.8.2  jdolecek     {
    744  1.8.8.2  jdolecek     case AML_ADD_OP:
    745  1.8.8.2  jdolecek     case AML_SUBTRACT_OP:
    746  1.8.8.2  jdolecek     case AML_MULTIPLY_OP:
    747  1.8.8.2  jdolecek     case AML_DIVIDE_OP:
    748  1.8.8.2  jdolecek     case AML_MOD_OP:
    749  1.8.8.2  jdolecek     case AML_SHIFT_LEFT_OP:
    750  1.8.8.2  jdolecek     case AML_SHIFT_RIGHT_OP:
    751  1.8.8.2  jdolecek     case AML_BIT_AND_OP:
    752  1.8.8.2  jdolecek     case AML_BIT_OR_OP:
    753  1.8.8.2  jdolecek     case AML_BIT_XOR_OP:
    754  1.8.8.2  jdolecek     case AML_LOGICAL_AND_OP:
    755  1.8.8.2  jdolecek     case AML_LOGICAL_EQUAL_OP:
    756  1.8.8.2  jdolecek     case AML_LOGICAL_GREATER_OP:
    757  1.8.8.2  jdolecek     case AML_LOGICAL_LESS_OP:
    758  1.8.8.2  jdolecek     case AML_LOGICAL_OR_OP:
    759  1.8.8.2  jdolecek 
    760  1.8.8.2  jdolecek         /* Emit paren only if this is not a compound assignment */
    761  1.8.8.2  jdolecek 
    762  1.8.8.2  jdolecek         if (Op->Common.DisasmFlags & ACPI_PARSEOP_COMPOUND_ASSIGNMENT)
    763  1.8.8.2  jdolecek         {
    764  1.8.8.2  jdolecek             ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
    765  1.8.8.2  jdolecek             return;
    766  1.8.8.2  jdolecek         }
    767  1.8.8.2  jdolecek 
    768  1.8.8.2  jdolecek         /* Emit extra close paren for assignment within an expression */
    769  1.8.8.2  jdolecek 
    770  1.8.8.2  jdolecek         if (Op->Common.DisasmFlags & ACPI_PARSEOP_ASSIGNMENT)
    771  1.8.8.2  jdolecek         {
    772  1.8.8.2  jdolecek             AcpiOsPrintf (")");
    773  1.8.8.2  jdolecek         }
    774  1.8.8.2  jdolecek         break;
    775  1.8.8.2  jdolecek 
    776  1.8.8.2  jdolecek     case AML_INDEX_OP:
    777  1.8.8.2  jdolecek 
    778  1.8.8.2  jdolecek         /* This is case for unsupported Index() source constants */
    779  1.8.8.2  jdolecek 
    780  1.8.8.2  jdolecek         if (Op->Common.DisasmFlags & ACPI_PARSEOP_CLOSING_PAREN)
    781  1.8.8.2  jdolecek         {
    782  1.8.8.2  jdolecek             AcpiOsPrintf (")");
    783  1.8.8.2  jdolecek         }
    784  1.8.8.2  jdolecek         ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
    785  1.8.8.2  jdolecek         return;
    786  1.8.8.2  jdolecek 
    787  1.8.8.2  jdolecek     /* No need for parens for these */
    788  1.8.8.2  jdolecek 
    789  1.8.8.2  jdolecek     case AML_DECREMENT_OP:
    790  1.8.8.2  jdolecek     case AML_INCREMENT_OP:
    791  1.8.8.2  jdolecek     case AML_LOGICAL_NOT_OP:
    792  1.8.8.2  jdolecek     case AML_BIT_NOT_OP:
    793  1.8.8.2  jdolecek     case AML_STORE_OP:
    794  1.8.8.2  jdolecek         ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
    795  1.8.8.2  jdolecek         return;
    796  1.8.8.2  jdolecek 
    797  1.8.8.2  jdolecek     default:
    798  1.8.8.2  jdolecek 
    799  1.8.8.2  jdolecek         /* Always emit paren for non-ASL+ operators */
    800  1.8.8.2  jdolecek         break;
    801  1.8.8.2  jdolecek     }
    802  1.8.8.2  jdolecek 
    803  1.8.8.2  jdolecek     AcpiOsPrintf (")");
    804  1.8.8.2  jdolecek     ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
    805  1.8.8.2  jdolecek 
    806  1.8.8.2  jdolecek     return;
    807  1.8.8.2  jdolecek }
    808  1.8.8.2  jdolecek 
    809  1.8.8.2  jdolecek 
    810  1.8.8.2  jdolecek /*******************************************************************************
    811  1.8.8.2  jdolecek  *
    812  1.8.8.2  jdolecek  * FUNCTION:    AcpiDmGetCompoundSymbol
    813  1.8.8.2  jdolecek  *
    814  1.8.8.2  jdolecek  * PARAMETERS:  AslOpcode
    815  1.8.8.2  jdolecek  *
    816  1.8.8.2  jdolecek  * RETURN:      String containing the compound assignment symbol
    817  1.8.8.2  jdolecek  *
    818  1.8.8.2  jdolecek  * DESCRIPTION: Detect opcodes that can be converted to compound assignment,
    819  1.8.8.2  jdolecek  *              return the appropriate operator string.
    820  1.8.8.2  jdolecek  *
    821  1.8.8.2  jdolecek  ******************************************************************************/
    822  1.8.8.2  jdolecek 
    823  1.8.8.2  jdolecek static const char *
    824  1.8.8.2  jdolecek AcpiDmGetCompoundSymbol (
    825  1.8.8.2  jdolecek    UINT16                   AmlOpcode)
    826  1.8.8.2  jdolecek {
    827  1.8.8.2  jdolecek     const char               *Symbol;
    828  1.8.8.2  jdolecek 
    829  1.8.8.2  jdolecek 
    830  1.8.8.2  jdolecek     switch (AmlOpcode)
    831  1.8.8.2  jdolecek     {
    832  1.8.8.2  jdolecek     case AML_ADD_OP:
    833  1.8.8.2  jdolecek         Symbol = " += ";
    834  1.8.8.2  jdolecek         break;
    835  1.8.8.2  jdolecek 
    836  1.8.8.2  jdolecek     case AML_SUBTRACT_OP:
    837  1.8.8.2  jdolecek         Symbol = " -= ";
    838  1.8.8.2  jdolecek         break;
    839  1.8.8.2  jdolecek 
    840  1.8.8.2  jdolecek     case AML_MULTIPLY_OP:
    841  1.8.8.2  jdolecek         Symbol = " *= ";
    842  1.8.8.2  jdolecek         break;
    843  1.8.8.2  jdolecek 
    844  1.8.8.2  jdolecek     case AML_DIVIDE_OP:
    845  1.8.8.2  jdolecek         Symbol = " /= ";
    846  1.8.8.2  jdolecek         break;
    847  1.8.8.2  jdolecek 
    848  1.8.8.2  jdolecek     case AML_MOD_OP:
    849  1.8.8.2  jdolecek         Symbol = " %= ";
    850  1.8.8.2  jdolecek         break;
    851  1.8.8.2  jdolecek 
    852  1.8.8.2  jdolecek     case AML_SHIFT_LEFT_OP:
    853  1.8.8.2  jdolecek         Symbol = " <<= ";
    854  1.8.8.2  jdolecek         break;
    855  1.8.8.2  jdolecek 
    856  1.8.8.2  jdolecek     case AML_SHIFT_RIGHT_OP:
    857  1.8.8.2  jdolecek         Symbol = " >>= ";
    858  1.8.8.2  jdolecek         break;
    859  1.8.8.2  jdolecek 
    860  1.8.8.2  jdolecek     case AML_BIT_AND_OP:
    861  1.8.8.2  jdolecek         Symbol = " &= ";
    862  1.8.8.2  jdolecek         break;
    863  1.8.8.2  jdolecek 
    864  1.8.8.2  jdolecek     case AML_BIT_OR_OP:
    865  1.8.8.2  jdolecek         Symbol = " |= ";
    866  1.8.8.2  jdolecek         break;
    867  1.8.8.2  jdolecek 
    868  1.8.8.2  jdolecek     case AML_BIT_XOR_OP:
    869  1.8.8.2  jdolecek         Symbol = " ^= ";
    870  1.8.8.2  jdolecek         break;
    871  1.8.8.2  jdolecek 
    872  1.8.8.2  jdolecek     default:
    873  1.8.8.2  jdolecek 
    874  1.8.8.2  jdolecek         /* No operator string for all other opcodes */
    875  1.8.8.2  jdolecek 
    876  1.8.8.2  jdolecek         return (NULL);
    877  1.8.8.2  jdolecek     }
    878  1.8.8.2  jdolecek 
    879  1.8.8.2  jdolecek     return (Symbol);
    880  1.8.8.2  jdolecek }
    881  1.8.8.2  jdolecek 
    882  1.8.8.2  jdolecek 
    883  1.8.8.2  jdolecek /*******************************************************************************
    884  1.8.8.2  jdolecek  *
    885  1.8.8.2  jdolecek  * FUNCTION:    AcpiDmPromoteTarget
    886  1.8.8.2  jdolecek  *
    887  1.8.8.2  jdolecek  * PARAMETERS:  Op                  - Operator parse object
    888  1.8.8.2  jdolecek  *              Target              - Target associate with the Op
    889  1.8.8.2  jdolecek  *
    890  1.8.8.2  jdolecek  * RETURN:      None
    891  1.8.8.2  jdolecek  *
    892  1.8.8.2  jdolecek  * DESCRIPTION: Transform the parse tree by moving the target up to the first
    893  1.8.8.2  jdolecek  *              child of the Op.
    894  1.8.8.2  jdolecek  *
    895  1.8.8.2  jdolecek  ******************************************************************************/
    896  1.8.8.2  jdolecek 
    897  1.8.8.2  jdolecek static void
    898  1.8.8.2  jdolecek AcpiDmPromoteTarget (
    899  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Op,
    900  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Target)
    901  1.8.8.2  jdolecek {
    902  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Child;
    903  1.8.8.2  jdolecek 
    904  1.8.8.2  jdolecek 
    905  1.8.8.2  jdolecek     /* Link target directly to the Op as first child */
    906  1.8.8.2  jdolecek 
    907  1.8.8.2  jdolecek     Child = Op->Common.Value.Arg;
    908  1.8.8.2  jdolecek     Op->Common.Value.Arg = Target;
    909  1.8.8.2  jdolecek     Target->Common.Next = Child;
    910  1.8.8.2  jdolecek 
    911  1.8.8.2  jdolecek     /* Find the last peer, it is linked to the target. Unlink it. */
    912  1.8.8.2  jdolecek 
    913  1.8.8.2  jdolecek     while (Child->Common.Next != Target)
    914  1.8.8.2  jdolecek     {
    915  1.8.8.2  jdolecek         Child = Child->Common.Next;
    916  1.8.8.2  jdolecek     }
    917  1.8.8.2  jdolecek 
    918  1.8.8.2  jdolecek     Child->Common.Next = NULL;
    919  1.8.8.2  jdolecek }
    920  1.8.8.2  jdolecek 
    921  1.8.8.2  jdolecek 
    922  1.8.8.2  jdolecek /*******************************************************************************
    923  1.8.8.2  jdolecek  *
    924  1.8.8.2  jdolecek  * FUNCTION:    AcpiDmIsValidTarget
    925  1.8.8.2  jdolecek  *
    926  1.8.8.2  jdolecek  * PARAMETERS:  Target              - Target Op from the parse tree
    927  1.8.8.2  jdolecek  *
    928  1.8.8.2  jdolecek  * RETURN:      TRUE if the Target is real. FALSE if it is just a placeholder
    929  1.8.8.2  jdolecek  *              Op that was inserted by the parser.
    930  1.8.8.2  jdolecek  *
    931  1.8.8.2  jdolecek  * DESCRIPTION: Determine if a Target Op is a placeholder Op or a real Target.
    932  1.8.8.2  jdolecek  *              In other words, determine if the optional target is used or
    933  1.8.8.2  jdolecek  *              not. Note: If Target is NULL, something is seriously wrong,
    934  1.8.8.2  jdolecek  *              probably with the parse tree.
    935  1.8.8.2  jdolecek  *
    936  1.8.8.2  jdolecek  ******************************************************************************/
    937  1.8.8.2  jdolecek 
    938  1.8.8.2  jdolecek static BOOLEAN
    939  1.8.8.2  jdolecek AcpiDmIsValidTarget (
    940  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Target)
    941  1.8.8.2  jdolecek {
    942  1.8.8.2  jdolecek 
    943  1.8.8.2  jdolecek     if (!Target)
    944  1.8.8.2  jdolecek     {
    945  1.8.8.2  jdolecek         return (FALSE);
    946  1.8.8.2  jdolecek     }
    947  1.8.8.2  jdolecek 
    948  1.8.8.2  jdolecek     if ((Target->Common.AmlOpcode == AML_INT_NAMEPATH_OP) &&
    949  1.8.8.2  jdolecek         (Target->Common.Value.Arg == NULL))
    950  1.8.8.2  jdolecek     {
    951  1.8.8.2  jdolecek         return (FALSE);
    952  1.8.8.2  jdolecek     }
    953  1.8.8.2  jdolecek 
    954  1.8.8.2  jdolecek     return (TRUE);
    955  1.8.8.2  jdolecek }
    956  1.8.8.2  jdolecek 
    957  1.8.8.2  jdolecek 
    958  1.8.8.2  jdolecek /*******************************************************************************
    959  1.8.8.2  jdolecek  *
    960  1.8.8.2  jdolecek  * FUNCTION:    AcpiDmIsTargetAnOperand
    961  1.8.8.2  jdolecek  *
    962  1.8.8.2  jdolecek  * PARAMETERS:  Target              - Target associated with the expression
    963  1.8.8.2  jdolecek  *              Operand             - An operand associated with expression
    964  1.8.8.2  jdolecek  *
    965  1.8.8.2  jdolecek  * RETURN:      TRUE if expression can be converted to a compound assignment.
    966  1.8.8.2  jdolecek  *              FALSE otherwise.
    967  1.8.8.2  jdolecek  *
    968  1.8.8.2  jdolecek  * DESCRIPTION: Determine if the Target duplicates the operand, in order to
    969  1.8.8.2  jdolecek  *              detect if the expression can be converted to a compound
    970  1.8.8.2  jdolecek  *              assigment. (+=, *=, etc.)
    971  1.8.8.2  jdolecek  *
    972  1.8.8.2  jdolecek  ******************************************************************************/
    973  1.8.8.2  jdolecek 
    974  1.8.8.2  jdolecek static BOOLEAN
    975  1.8.8.2  jdolecek AcpiDmIsTargetAnOperand (
    976  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Target,
    977  1.8.8.2  jdolecek     ACPI_PARSE_OBJECT       *Operand,
    978  1.8.8.2  jdolecek     BOOLEAN                 TopLevel)
    979  1.8.8.2  jdolecek {
    980  1.8.8.2  jdolecek     const ACPI_OPCODE_INFO  *OpInfo;
    981  1.8.8.2  jdolecek     BOOLEAN                 Same;
    982  1.8.8.2  jdolecek 
    983  1.8.8.2  jdolecek 
    984  1.8.8.2  jdolecek     /*
    985  1.8.8.2  jdolecek      * Opcodes must match. Note: ignoring the difference between nameseg
    986  1.8.8.2  jdolecek      * and namepath for now. May be needed later.
    987  1.8.8.2  jdolecek      */
    988  1.8.8.2  jdolecek     if (Target->Common.AmlOpcode != Operand->Common.AmlOpcode)
    989  1.8.8.2  jdolecek     {
    990  1.8.8.2  jdolecek         return (FALSE);
    991  1.8.8.2  jdolecek     }
    992  1.8.8.2  jdolecek 
    993  1.8.8.2  jdolecek     /* Nodes should match, even if they are NULL */
    994  1.8.8.2  jdolecek 
    995  1.8.8.2  jdolecek     if (Target->Common.Node != Operand->Common.Node)
    996  1.8.8.2  jdolecek     {
    997  1.8.8.2  jdolecek         return (FALSE);
    998  1.8.8.2  jdolecek     }
    999  1.8.8.2  jdolecek 
   1000  1.8.8.2  jdolecek     /* Determine if a child exists */
   1001  1.8.8.2  jdolecek 
   1002  1.8.8.2  jdolecek     OpInfo = AcpiPsGetOpcodeInfo (Operand->Common.AmlOpcode);
   1003  1.8.8.2  jdolecek     if (OpInfo->Flags & AML_HAS_ARGS)
   1004  1.8.8.2  jdolecek     {
   1005  1.8.8.2  jdolecek         Same = AcpiDmIsTargetAnOperand (Target->Common.Value.Arg,
   1006  1.8.8.2  jdolecek             Operand->Common.Value.Arg, FALSE);
   1007  1.8.8.2  jdolecek         if (!Same)
   1008  1.8.8.2  jdolecek         {
   1009  1.8.8.2  jdolecek             return (FALSE);
   1010  1.8.8.2  jdolecek         }
   1011  1.8.8.2  jdolecek     }
   1012  1.8.8.2  jdolecek 
   1013  1.8.8.2  jdolecek     /* Check the next peer, as long as we are not at the top level */
   1014  1.8.8.2  jdolecek 
   1015  1.8.8.2  jdolecek     if ((!TopLevel) &&
   1016  1.8.8.2  jdolecek          Target->Common.Next)
   1017  1.8.8.2  jdolecek     {
   1018  1.8.8.2  jdolecek         Same = AcpiDmIsTargetAnOperand (Target->Common.Next,
   1019  1.8.8.2  jdolecek             Operand->Common.Next, FALSE);
   1020  1.8.8.2  jdolecek         if (!Same)
   1021  1.8.8.2  jdolecek         {
   1022  1.8.8.2  jdolecek             return (FALSE);
   1023  1.8.8.2  jdolecek         }
   1024  1.8.8.2  jdolecek     }
   1025  1.8.8.2  jdolecek 
   1026  1.8.8.2  jdolecek     /* Supress the duplicate operand at the top-level */
   1027  1.8.8.2  jdolecek 
   1028  1.8.8.2  jdolecek     if (TopLevel)
   1029  1.8.8.2  jdolecek     {
   1030  1.8.8.2  jdolecek         Operand->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
   1031  1.8.8.2  jdolecek     }
   1032  1.8.8.2  jdolecek     return (TRUE);
   1033  1.8.8.2  jdolecek }
   1034