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