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