Home | History | Annotate | Line # | Download | only in disassembler
dmcstyle.c revision 1.1.1.4
      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.1.4  christos         Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
    219      1.1  christos 
    220      1.1  christos         /* Save symbol string in the next child (not peer) */
    221      1.1  christos 
    222      1.1  christos         Child2 = AcpiPsGetArg (Child1, 0);
    223      1.1  christos         if (!Child2)
    224      1.1  christos         {
    225      1.1  christos             return (FALSE);
    226      1.1  christos         }
    227      1.1  christos 
    228      1.1  christos         Child2->Common.OperatorSymbol = OperatorSymbol;
    229      1.1  christos         return (TRUE);
    230      1.1  christos 
    231      1.1  christos     case AML_INDEX_OP:
    232  1.1.1.3  christos         /*
    233  1.1.1.3  christos          * Check for constant source operand. Note: although technically
    234  1.1.1.3  christos          * legal syntax, the iASL compiler does not support this with
    235  1.1.1.3  christos          * the symbolic operators for Index(). It doesn't make sense to
    236  1.1.1.3  christos          * use Index() with a constant anyway.
    237  1.1.1.3  christos          */
    238  1.1.1.3  christos         if ((Child1->Common.AmlOpcode == AML_STRING_OP)  ||
    239  1.1.1.3  christos             (Child1->Common.AmlOpcode == AML_BUFFER_OP)  ||
    240  1.1.1.3  christos             (Child1->Common.AmlOpcode == AML_PACKAGE_OP) ||
    241  1.1.1.3  christos             (Child1->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
    242  1.1.1.3  christos         {
    243  1.1.1.3  christos             Op->Common.DisasmFlags |= ACPI_PARSEOP_CLOSING_PAREN;
    244  1.1.1.3  christos             return (FALSE);
    245  1.1.1.3  christos         }
    246  1.1.1.3  christos 
    247  1.1.1.3  christos         /* Index operator is [] */
    248  1.1.1.3  christos 
    249      1.1  christos         Child1->Common.OperatorSymbol = " [";
    250      1.1  christos         Child2->Common.OperatorSymbol = "]";
    251      1.1  christos         break;
    252      1.1  christos 
    253      1.1  christos     /* Unary operators */
    254      1.1  christos 
    255      1.1  christos     case AML_DECREMENT_OP:
    256      1.1  christos         OperatorSymbol = "--";
    257      1.1  christos         break;
    258      1.1  christos 
    259      1.1  christos     case AML_INCREMENT_OP:
    260      1.1  christos         OperatorSymbol = "++";
    261      1.1  christos         break;
    262      1.1  christos 
    263      1.1  christos     case AML_BIT_NOT_OP:
    264      1.1  christos     case AML_STORE_OP:
    265      1.1  christos         OperatorSymbol = NULL;
    266      1.1  christos         break;
    267      1.1  christos 
    268      1.1  christos     default:
    269      1.1  christos         return (FALSE);
    270      1.1  christos     }
    271      1.1  christos 
    272      1.1  christos     if (Child1->Common.DisasmOpcode == ACPI_DASM_LNOT_SUFFIX)
    273      1.1  christos     {
    274      1.1  christos         return (TRUE);
    275      1.1  christos     }
    276      1.1  christos 
    277      1.1  christos     /*
    278      1.1  christos      * This is the key to how the disassembly of the C-style operators
    279      1.1  christos      * works. We save the operator symbol in the first child, thus
    280      1.1  christos      * deferring symbol output until after the first operand has been
    281      1.1  christos      * emitted.
    282      1.1  christos      */
    283      1.1  christos     if (!Child1->Common.OperatorSymbol)
    284      1.1  christos     {
    285      1.1  christos         Child1->Common.OperatorSymbol = OperatorSymbol;
    286      1.1  christos     }
    287      1.1  christos 
    288      1.1  christos     /*
    289      1.1  christos      * Check for a valid target as the 3rd (or sometimes 2nd) operand
    290      1.1  christos      *
    291      1.1  christos      * Compound assignment operator support:
    292      1.1  christos      * Attempt to optimize constructs of the form:
    293      1.1  christos      *      Add (Local1, 0xFF, Local1)
    294      1.1  christos      * to:
    295      1.1  christos      *      Local1 += 0xFF
    296      1.1  christos      *
    297      1.1  christos      * Only the math operators and Store() have a target.
    298      1.1  christos      * Logicals have no target.
    299      1.1  christos      */
    300      1.1  christos     switch (Op->Common.AmlOpcode)
    301      1.1  christos     {
    302      1.1  christos     case AML_ADD_OP:
    303      1.1  christos     case AML_SUBTRACT_OP:
    304      1.1  christos     case AML_MULTIPLY_OP:
    305      1.1  christos     case AML_DIVIDE_OP:
    306      1.1  christos     case AML_MOD_OP:
    307      1.1  christos     case AML_SHIFT_LEFT_OP:
    308      1.1  christos     case AML_SHIFT_RIGHT_OP:
    309      1.1  christos     case AML_BIT_AND_OP:
    310      1.1  christos     case AML_BIT_OR_OP:
    311      1.1  christos     case AML_BIT_XOR_OP:
    312      1.1  christos 
    313      1.1  christos         /* Target is 3rd operand */
    314      1.1  christos 
    315      1.1  christos         Target = Child2->Common.Next;
    316      1.1  christos         if (Op->Common.AmlOpcode == AML_DIVIDE_OP)
    317      1.1  christos         {
    318      1.1  christos             /*
    319      1.1  christos              * Divide has an extra target operand (Remainder).
    320      1.1  christos              * If this extra target is specified, it cannot be converted
    321      1.1  christos              * to a C-style operator
    322      1.1  christos              */
    323      1.1  christos             if (AcpiDmIsValidTarget (Target))
    324      1.1  christos             {
    325      1.1  christos                 Child1->Common.OperatorSymbol = NULL;
    326      1.1  christos                 return (FALSE);
    327      1.1  christos             }
    328      1.1  christos 
    329      1.1  christos             Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
    330      1.1  christos             Target = Target->Common.Next;
    331      1.1  christos         }
    332      1.1  christos 
    333      1.1  christos         /* Parser should ensure there is at least a placeholder target */
    334      1.1  christos 
    335      1.1  christos         if (!Target)
    336      1.1  christos         {
    337      1.1  christos             return (FALSE);
    338      1.1  christos         }
    339      1.1  christos 
    340      1.1  christos         if (!AcpiDmIsValidTarget (Target))
    341      1.1  christos         {
    342      1.1  christos             /* Not a valid target (placeholder only, from parser) */
    343      1.1  christos             break;
    344      1.1  christos         }
    345      1.1  christos 
    346      1.1  christos         /*
    347      1.1  christos          * Promote the target up to the first child in the parse
    348      1.1  christos          * tree. This is done because the target will be output
    349      1.1  christos          * first, in the form:
    350      1.1  christos          *     <Target> = Operands...
    351      1.1  christos          */
    352      1.1  christos         AcpiDmPromoteTarget (Op, Target);
    353      1.1  christos 
    354  1.1.1.2  christos         /* Check operands for conversion to a "Compound Assignment" */
    355  1.1.1.2  christos 
    356  1.1.1.2  christos         switch (Op->Common.AmlOpcode)
    357      1.1  christos         {
    358  1.1.1.2  christos             /* Commutative operators */
    359  1.1.1.2  christos 
    360  1.1.1.2  christos         case AML_ADD_OP:
    361  1.1.1.2  christos         case AML_MULTIPLY_OP:
    362  1.1.1.2  christos         case AML_BIT_AND_OP:
    363  1.1.1.2  christos         case AML_BIT_OR_OP:
    364  1.1.1.2  christos         case AML_BIT_XOR_OP:
    365  1.1.1.2  christos             /*
    366  1.1.1.2  christos              * For the commutative operators, we can convert to a
    367  1.1.1.2  christos              * compound statement only if at least one (either) operand
    368  1.1.1.2  christos              * is the same as the target.
    369  1.1.1.2  christos              *
    370  1.1.1.2  christos              *      Add (A, B, A) --> A += B
    371  1.1.1.2  christos              *      Add (B, A, A) --> A += B
    372  1.1.1.2  christos              *      Add (B, C, A) --> A = (B + C)
    373  1.1.1.2  christos              */
    374  1.1.1.2  christos             if ((AcpiDmIsTargetAnOperand (Target, Child1, TRUE)) ||
    375  1.1.1.2  christos                 (AcpiDmIsTargetAnOperand (Target, Child2, TRUE)))
    376  1.1.1.2  christos             {
    377  1.1.1.2  christos                 Target->Common.OperatorSymbol =
    378  1.1.1.2  christos                     AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
    379      1.1  christos 
    380  1.1.1.2  christos                 /* Convert operator to compound assignment */
    381      1.1  christos 
    382  1.1.1.4  christos                 Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
    383  1.1.1.2  christos                 Child1->Common.OperatorSymbol = NULL;
    384  1.1.1.2  christos                 return (TRUE);
    385  1.1.1.2  christos             }
    386  1.1.1.2  christos             break;
    387  1.1.1.2  christos 
    388  1.1.1.2  christos             /* Non-commutative operators */
    389  1.1.1.2  christos 
    390  1.1.1.2  christos         case AML_SUBTRACT_OP:
    391  1.1.1.2  christos         case AML_DIVIDE_OP:
    392  1.1.1.2  christos         case AML_MOD_OP:
    393  1.1.1.2  christos         case AML_SHIFT_LEFT_OP:
    394  1.1.1.2  christos         case AML_SHIFT_RIGHT_OP:
    395  1.1.1.2  christos             /*
    396  1.1.1.2  christos              * For the non-commutative operators, we can convert to a
    397  1.1.1.2  christos              * compound statement only if the target is the same as the
    398  1.1.1.2  christos              * first operand.
    399  1.1.1.2  christos              *
    400  1.1.1.2  christos              *      Subtract (A, B, A) --> A -= B
    401  1.1.1.2  christos              *      Subtract (B, A, A) --> A = (B - A)
    402  1.1.1.2  christos              */
    403  1.1.1.2  christos             if ((AcpiDmIsTargetAnOperand (Target, Child1, TRUE)))
    404  1.1.1.2  christos             {
    405  1.1.1.2  christos                 Target->Common.OperatorSymbol =
    406  1.1.1.2  christos                     AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
    407  1.1.1.2  christos 
    408  1.1.1.2  christos                 /* Convert operator to compound assignment */
    409  1.1.1.2  christos 
    410  1.1.1.4  christos                 Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
    411  1.1.1.2  christos                 Child1->Common.OperatorSymbol = NULL;
    412  1.1.1.2  christos                 return (TRUE);
    413  1.1.1.2  christos             }
    414  1.1.1.2  christos             break;
    415  1.1.1.2  christos 
    416  1.1.1.2  christos         default:
    417  1.1.1.2  christos             break;
    418      1.1  christos         }
    419      1.1  christos 
    420      1.1  christos         /*
    421      1.1  christos          * If we are within a C-style expression, emit an extra open
    422      1.1  christos          * paren. Implemented by examining the parent op.
    423      1.1  christos          */
    424      1.1  christos         switch (Op->Common.Parent->Common.AmlOpcode)
    425      1.1  christos         {
    426      1.1  christos         case AML_ADD_OP:
    427      1.1  christos         case AML_SUBTRACT_OP:
    428      1.1  christos         case AML_MULTIPLY_OP:
    429      1.1  christos         case AML_DIVIDE_OP:
    430      1.1  christos         case AML_MOD_OP:
    431      1.1  christos         case AML_SHIFT_LEFT_OP:
    432      1.1  christos         case AML_SHIFT_RIGHT_OP:
    433      1.1  christos         case AML_BIT_AND_OP:
    434      1.1  christos         case AML_BIT_OR_OP:
    435      1.1  christos         case AML_BIT_XOR_OP:
    436      1.1  christos         case AML_LAND_OP:
    437      1.1  christos         case AML_LEQUAL_OP:
    438      1.1  christos         case AML_LGREATER_OP:
    439      1.1  christos         case AML_LLESS_OP:
    440      1.1  christos         case AML_LOR_OP:
    441      1.1  christos 
    442      1.1  christos             Op->Common.DisasmFlags |= ACPI_PARSEOP_ASSIGNMENT;
    443      1.1  christos             AcpiOsPrintf ("(");
    444      1.1  christos             break;
    445      1.1  christos 
    446      1.1  christos         default:
    447      1.1  christos             break;
    448      1.1  christos         }
    449      1.1  christos 
    450      1.1  christos         /* Normal output for ASL/AML operators with a target operand */
    451      1.1  christos 
    452      1.1  christos         Target->Common.OperatorSymbol = " = (";
    453      1.1  christos         return (TRUE);
    454      1.1  christos 
    455      1.1  christos     /* Binary operators, no parens */
    456      1.1  christos 
    457      1.1  christos     case AML_DECREMENT_OP:
    458      1.1  christos     case AML_INCREMENT_OP:
    459      1.1  christos         return (TRUE);
    460      1.1  christos 
    461      1.1  christos     case AML_INDEX_OP:
    462      1.1  christos 
    463      1.1  christos         /* Target is optional, 3rd operand */
    464      1.1  christos 
    465      1.1  christos         Target = Child2->Common.Next;
    466      1.1  christos         if (AcpiDmIsValidTarget (Target))
    467      1.1  christos         {
    468      1.1  christos             AcpiDmPromoteTarget (Op, Target);
    469      1.1  christos 
    470      1.1  christos             if (!Target->Common.OperatorSymbol)
    471      1.1  christos             {
    472      1.1  christos                 Target->Common.OperatorSymbol = " = ";
    473      1.1  christos             }
    474      1.1  christos         }
    475      1.1  christos         return (TRUE);
    476      1.1  christos 
    477      1.1  christos     case AML_STORE_OP:
    478      1.1  christos         /*
    479      1.1  christos          * Target is the 2nd operand.
    480      1.1  christos          * We know the target is valid, it is not optional.
    481      1.1  christos          * In the parse tree, simply swap the target with the
    482      1.1  christos          * source so that the target is processed first.
    483      1.1  christos          */
    484      1.1  christos         Target = Child1->Common.Next;
    485  1.1.1.2  christos         if (!Target)
    486  1.1.1.2  christos         {
    487  1.1.1.2  christos             return (FALSE);
    488  1.1.1.2  christos         }
    489      1.1  christos 
    490  1.1.1.2  christos         AcpiDmPromoteTarget (Op, Target);
    491      1.1  christos         if (!Target->Common.OperatorSymbol)
    492      1.1  christos         {
    493      1.1  christos             Target->Common.OperatorSymbol = " = ";
    494      1.1  christos         }
    495      1.1  christos         return (TRUE);
    496      1.1  christos 
    497      1.1  christos     case AML_BIT_NOT_OP:
    498      1.1  christos 
    499      1.1  christos         /* Target is optional, 2nd operand */
    500      1.1  christos 
    501      1.1  christos         Target = Child1->Common.Next;
    502      1.1  christos         if (!Target)
    503      1.1  christos         {
    504      1.1  christos             return (FALSE);
    505      1.1  christos         }
    506      1.1  christos 
    507      1.1  christos         if (AcpiDmIsValidTarget (Target))
    508      1.1  christos         {
    509      1.1  christos             /* Valid target, not a placeholder */
    510      1.1  christos 
    511      1.1  christos             AcpiDmPromoteTarget (Op, Target);
    512      1.1  christos             Target->Common.OperatorSymbol = " = ~";
    513      1.1  christos         }
    514      1.1  christos         else
    515      1.1  christos         {
    516      1.1  christos             /* No target. Emit this prefix operator immediately */
    517      1.1  christos 
    518      1.1  christos             AcpiOsPrintf ("~");
    519      1.1  christos         }
    520      1.1  christos         return (TRUE);
    521      1.1  christos 
    522      1.1  christos     default:
    523      1.1  christos         break;
    524      1.1  christos     }
    525      1.1  christos 
    526  1.1.1.4  christos     /*
    527  1.1.1.4  christos      * Nodes marked with ACPI_PARSEOP_PARAMLIST don't need a parens
    528  1.1.1.4  christos      * output here. We also need to check the parent to see if this op
    529  1.1.1.4  christos      * is part of a compound test (!=, >=, <=).
    530  1.1.1.4  christos      */
    531  1.1.1.4  christos     if ((Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST) ||
    532  1.1.1.4  christos        ((Op->Common.Parent->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST) &&
    533  1.1.1.4  christos         (Op->Common.DisasmOpcode == ACPI_DASM_LNOT_SUFFIX)))
    534  1.1.1.4  christos     {
    535  1.1.1.4  christos         /* Do Nothing. Paren already generated */
    536  1.1.1.4  christos         return (TRUE);
    537  1.1.1.4  christos     }
    538  1.1.1.4  christos 
    539      1.1  christos     /* All other operators, emit an open paren */
    540      1.1  christos 
    541      1.1  christos     AcpiOsPrintf ("(");
    542      1.1  christos     return (TRUE);
    543      1.1  christos }
    544      1.1  christos 
    545      1.1  christos 
    546      1.1  christos /*******************************************************************************
    547      1.1  christos  *
    548      1.1  christos  * FUNCTION:    AcpiDmCloseOperator
    549      1.1  christos  *
    550      1.1  christos  * PARAMETERS:  Op                  - Current parse object
    551      1.1  christos  *
    552      1.1  christos  * RETURN:      None
    553      1.1  christos  *
    554      1.1  christos  * DESCRIPTION: Closes an operator by adding a closing parentheses if and
    555      1.1  christos  *              when necessary. Called during ascending phase of the
    556      1.1  christos  *              parse tree walk.
    557      1.1  christos  *
    558      1.1  christos  ******************************************************************************/
    559      1.1  christos 
    560      1.1  christos void
    561      1.1  christos AcpiDmCloseOperator (
    562      1.1  christos     ACPI_PARSE_OBJECT       *Op)
    563      1.1  christos {
    564  1.1.1.4  christos     BOOLEAN                 IsCStyleOp = FALSE;
    565      1.1  christos 
    566      1.1  christos     /* Always emit paren if ASL+ disassembly disabled */
    567      1.1  christos 
    568      1.1  christos     if (!AcpiGbl_CstyleDisassembly)
    569      1.1  christos     {
    570      1.1  christos         AcpiOsPrintf (")");
    571      1.1  christos         return;
    572      1.1  christos     }
    573      1.1  christos 
    574      1.1  christos     /* Check if we need to add an additional closing paren */
    575      1.1  christos 
    576      1.1  christos     switch (Op->Common.AmlOpcode)
    577      1.1  christos     {
    578      1.1  christos     case AML_ADD_OP:
    579      1.1  christos     case AML_SUBTRACT_OP:
    580      1.1  christos     case AML_MULTIPLY_OP:
    581      1.1  christos     case AML_DIVIDE_OP:
    582      1.1  christos     case AML_MOD_OP:
    583      1.1  christos     case AML_SHIFT_LEFT_OP:
    584      1.1  christos     case AML_SHIFT_RIGHT_OP:
    585      1.1  christos     case AML_BIT_AND_OP:
    586      1.1  christos     case AML_BIT_OR_OP:
    587      1.1  christos     case AML_BIT_XOR_OP:
    588      1.1  christos     case AML_LAND_OP:
    589      1.1  christos     case AML_LEQUAL_OP:
    590      1.1  christos     case AML_LGREATER_OP:
    591      1.1  christos     case AML_LLESS_OP:
    592      1.1  christos     case AML_LOR_OP:
    593      1.1  christos 
    594      1.1  christos         /* Emit paren only if this is not a compound assignment */
    595      1.1  christos 
    596  1.1.1.4  christos         if (Op->Common.DisasmFlags & ACPI_PARSEOP_COMPOUND_ASSIGNMENT)
    597      1.1  christos         {
    598      1.1  christos             return;
    599      1.1  christos         }
    600      1.1  christos 
    601      1.1  christos         /* Emit extra close paren for assignment within an expression */
    602      1.1  christos 
    603      1.1  christos         if (Op->Common.DisasmFlags & ACPI_PARSEOP_ASSIGNMENT)
    604      1.1  christos         {
    605      1.1  christos             AcpiOsPrintf (")");
    606      1.1  christos         }
    607  1.1.1.4  christos 
    608  1.1.1.4  christos         IsCStyleOp = TRUE;
    609      1.1  christos         break;
    610      1.1  christos 
    611  1.1.1.3  christos     case AML_INDEX_OP:
    612  1.1.1.3  christos 
    613  1.1.1.3  christos         /* This is case for unsupported Index() source constants */
    614  1.1.1.3  christos 
    615  1.1.1.3  christos         if (Op->Common.DisasmFlags & ACPI_PARSEOP_CLOSING_PAREN)
    616  1.1.1.3  christos         {
    617  1.1.1.3  christos             AcpiOsPrintf (")");
    618  1.1.1.3  christos         }
    619  1.1.1.3  christos         return;
    620      1.1  christos 
    621      1.1  christos     /* No need for parens for these */
    622      1.1  christos 
    623      1.1  christos     case AML_DECREMENT_OP:
    624      1.1  christos     case AML_INCREMENT_OP:
    625      1.1  christos     case AML_LNOT_OP:
    626      1.1  christos     case AML_BIT_NOT_OP:
    627      1.1  christos     case AML_STORE_OP:
    628      1.1  christos         return;
    629      1.1  christos 
    630      1.1  christos     default:
    631      1.1  christos 
    632      1.1  christos         /* Always emit paren for non-ASL+ operators */
    633      1.1  christos         break;
    634      1.1  christos     }
    635      1.1  christos 
    636  1.1.1.4  christos     /*
    637  1.1.1.4  christos      * Nodes marked with ACPI_PARSEOP_PARAMLIST don't need a parens
    638  1.1.1.4  christos      * output here. We also need to check the parent to see if this op
    639  1.1.1.4  christos      * is part of a compound test (!=, >=, <=).
    640  1.1.1.4  christos      */
    641  1.1.1.4  christos     if (IsCStyleOp &&
    642  1.1.1.4  christos        ((Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST) ||
    643  1.1.1.4  christos        ((Op->Common.Parent->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST) &&
    644  1.1.1.4  christos         (Op->Common.DisasmOpcode == ACPI_DASM_LNOT_SUFFIX))))
    645  1.1.1.4  christos     {
    646  1.1.1.4  christos         return;
    647  1.1.1.4  christos     }
    648  1.1.1.4  christos 
    649      1.1  christos     AcpiOsPrintf (")");
    650  1.1.1.4  christos     return;
    651      1.1  christos }
    652      1.1  christos 
    653      1.1  christos 
    654      1.1  christos /*******************************************************************************
    655      1.1  christos  *
    656      1.1  christos  * FUNCTION:    AcpiDmGetCompoundSymbol
    657      1.1  christos  *
    658      1.1  christos  * PARAMETERS:  AslOpcode
    659      1.1  christos  *
    660      1.1  christos  * RETURN:      String containing the compound assignment symbol
    661      1.1  christos  *
    662      1.1  christos  * DESCRIPTION: Detect opcodes that can be converted to compound assignment,
    663      1.1  christos  *              return the appropriate operator string.
    664      1.1  christos  *
    665      1.1  christos  ******************************************************************************/
    666      1.1  christos 
    667      1.1  christos static char *
    668      1.1  christos AcpiDmGetCompoundSymbol (
    669      1.1  christos    UINT16                   AmlOpcode)
    670      1.1  christos {
    671      1.1  christos     char                    *Symbol;
    672      1.1  christos 
    673      1.1  christos 
    674      1.1  christos     switch (AmlOpcode)
    675      1.1  christos     {
    676      1.1  christos     case AML_ADD_OP:
    677      1.1  christos         Symbol = " += ";
    678      1.1  christos         break;
    679      1.1  christos 
    680      1.1  christos     case AML_SUBTRACT_OP:
    681      1.1  christos         Symbol = " -= ";
    682      1.1  christos         break;
    683      1.1  christos 
    684      1.1  christos     case AML_MULTIPLY_OP:
    685      1.1  christos         Symbol = " *= ";
    686      1.1  christos         break;
    687      1.1  christos 
    688      1.1  christos     case AML_DIVIDE_OP:
    689      1.1  christos         Symbol = " /= ";
    690      1.1  christos         break;
    691      1.1  christos 
    692      1.1  christos     case AML_MOD_OP:
    693      1.1  christos         Symbol = " %= ";
    694      1.1  christos         break;
    695      1.1  christos 
    696      1.1  christos     case AML_SHIFT_LEFT_OP:
    697      1.1  christos         Symbol = " <<= ";
    698      1.1  christos         break;
    699      1.1  christos 
    700      1.1  christos     case AML_SHIFT_RIGHT_OP:
    701      1.1  christos         Symbol = " >>= ";
    702      1.1  christos         break;
    703      1.1  christos 
    704      1.1  christos     case AML_BIT_AND_OP:
    705      1.1  christos         Symbol = " &= ";
    706      1.1  christos         break;
    707      1.1  christos 
    708      1.1  christos     case AML_BIT_OR_OP:
    709      1.1  christos         Symbol = " |= ";
    710      1.1  christos         break;
    711      1.1  christos 
    712      1.1  christos     case AML_BIT_XOR_OP:
    713      1.1  christos         Symbol = " ^= ";
    714      1.1  christos         break;
    715      1.1  christos 
    716      1.1  christos     default:
    717      1.1  christos 
    718      1.1  christos         /* No operator string for all other opcodes */
    719  1.1.1.3  christos 
    720      1.1  christos         return (NULL);
    721      1.1  christos     }
    722      1.1  christos 
    723      1.1  christos     return (Symbol);
    724      1.1  christos }
    725      1.1  christos 
    726      1.1  christos 
    727      1.1  christos /*******************************************************************************
    728      1.1  christos  *
    729      1.1  christos  * FUNCTION:    AcpiDmPromoteTarget
    730      1.1  christos  *
    731      1.1  christos  * PARAMETERS:  Op                  - Operator parse object
    732      1.1  christos  *              Target              - Target associate with the Op
    733      1.1  christos  *
    734      1.1  christos  * RETURN:      None
    735      1.1  christos  *
    736      1.1  christos  * DESCRIPTION: Transform the parse tree by moving the target up to the first
    737      1.1  christos  *              child of the Op.
    738      1.1  christos  *
    739      1.1  christos  ******************************************************************************/
    740      1.1  christos 
    741      1.1  christos static void
    742      1.1  christos AcpiDmPromoteTarget (
    743      1.1  christos     ACPI_PARSE_OBJECT       *Op,
    744      1.1  christos     ACPI_PARSE_OBJECT       *Target)
    745      1.1  christos {
    746      1.1  christos     ACPI_PARSE_OBJECT       *Child;
    747      1.1  christos 
    748      1.1  christos 
    749      1.1  christos     /* Link target directly to the Op as first child */
    750      1.1  christos 
    751      1.1  christos     Child = Op->Common.Value.Arg;
    752      1.1  christos     Op->Common.Value.Arg = Target;
    753      1.1  christos     Target->Common.Next = Child;
    754      1.1  christos 
    755      1.1  christos     /* Find the last peer, it is linked to the target. Unlink it. */
    756      1.1  christos 
    757      1.1  christos     while (Child->Common.Next != Target)
    758      1.1  christos     {
    759      1.1  christos         Child = Child->Common.Next;
    760      1.1  christos     }
    761      1.1  christos 
    762      1.1  christos     Child->Common.Next = NULL;
    763      1.1  christos }
    764      1.1  christos 
    765      1.1  christos 
    766      1.1  christos /*******************************************************************************
    767      1.1  christos  *
    768      1.1  christos  * FUNCTION:    AcpiDmIsValidTarget
    769      1.1  christos  *
    770      1.1  christos  * PARAMETERS:  Target              - Target Op from the parse tree
    771      1.1  christos  *
    772      1.1  christos  * RETURN:      TRUE if the Target is real. FALSE if it is just a placeholder
    773      1.1  christos  *              Op that was inserted by the parser.
    774      1.1  christos  *
    775      1.1  christos  * DESCRIPTION: Determine if a Target Op is a placeholder Op or a real Target.
    776      1.1  christos  *              In other words, determine if the optional target is used or
    777  1.1.1.2  christos  *              not. Note: If Target is NULL, something is seriously wrong,
    778  1.1.1.2  christos  *              probably with the parse tree.
    779      1.1  christos  *
    780      1.1  christos  ******************************************************************************/
    781      1.1  christos 
    782      1.1  christos static BOOLEAN
    783      1.1  christos AcpiDmIsValidTarget (
    784      1.1  christos     ACPI_PARSE_OBJECT       *Target)
    785      1.1  christos {
    786      1.1  christos 
    787  1.1.1.2  christos     if (!Target)
    788  1.1.1.2  christos     {
    789  1.1.1.2  christos         return (FALSE);
    790  1.1.1.2  christos     }
    791  1.1.1.2  christos 
    792      1.1  christos     if ((Target->Common.AmlOpcode == AML_INT_NAMEPATH_OP) &&
    793      1.1  christos         (Target->Common.Value.Arg == NULL))
    794      1.1  christos     {
    795      1.1  christos         return (FALSE);
    796      1.1  christos     }
    797      1.1  christos 
    798      1.1  christos     return (TRUE);
    799      1.1  christos }
    800      1.1  christos 
    801      1.1  christos 
    802      1.1  christos /*******************************************************************************
    803      1.1  christos  *
    804      1.1  christos  * FUNCTION:    AcpiDmIsTargetAnOperand
    805      1.1  christos  *
    806      1.1  christos  * PARAMETERS:  Target              - Target associated with the expression
    807      1.1  christos  *              Operand             - An operand associated with expression
    808      1.1  christos  *
    809      1.1  christos  * RETURN:      TRUE if expression can be converted to a compound assignment.
    810      1.1  christos  *              FALSE otherwise.
    811      1.1  christos  *
    812      1.1  christos  * DESCRIPTION: Determine if the Target duplicates the operand, in order to
    813      1.1  christos  *              detect if the expression can be converted to a compound
    814      1.1  christos  *              assigment. (+=, *=, etc.)
    815      1.1  christos  *
    816      1.1  christos  ******************************************************************************/
    817      1.1  christos 
    818      1.1  christos static BOOLEAN
    819      1.1  christos AcpiDmIsTargetAnOperand (
    820      1.1  christos     ACPI_PARSE_OBJECT       *Target,
    821      1.1  christos     ACPI_PARSE_OBJECT       *Operand,
    822      1.1  christos     BOOLEAN                 TopLevel)
    823      1.1  christos {
    824      1.1  christos     const ACPI_OPCODE_INFO  *OpInfo;
    825      1.1  christos     BOOLEAN                 Same;
    826      1.1  christos 
    827      1.1  christos 
    828      1.1  christos     /*
    829      1.1  christos      * Opcodes must match. Note: ignoring the difference between nameseg
    830      1.1  christos      * and namepath for now. May be needed later.
    831      1.1  christos      */
    832      1.1  christos     if (Target->Common.AmlOpcode != Operand->Common.AmlOpcode)
    833      1.1  christos     {
    834      1.1  christos         return (FALSE);
    835      1.1  christos     }
    836      1.1  christos 
    837      1.1  christos     /* Nodes should match, even if they are NULL */
    838      1.1  christos 
    839      1.1  christos     if (Target->Common.Node != Operand->Common.Node)
    840      1.1  christos     {
    841      1.1  christos         return (FALSE);
    842      1.1  christos     }
    843      1.1  christos 
    844      1.1  christos     /* Determine if a child exists */
    845      1.1  christos 
    846      1.1  christos     OpInfo = AcpiPsGetOpcodeInfo (Operand->Common.AmlOpcode);
    847      1.1  christos     if (OpInfo->Flags & AML_HAS_ARGS)
    848      1.1  christos     {
    849      1.1  christos         Same = AcpiDmIsTargetAnOperand (Target->Common.Value.Arg,
    850      1.1  christos             Operand->Common.Value.Arg, FALSE);
    851      1.1  christos         if (!Same)
    852      1.1  christos         {
    853      1.1  christos             return (FALSE);
    854      1.1  christos         }
    855      1.1  christos     }
    856      1.1  christos 
    857      1.1  christos     /* Check the next peer, as long as we are not at the top level */
    858      1.1  christos 
    859      1.1  christos     if ((!TopLevel) &&
    860      1.1  christos          Target->Common.Next)
    861      1.1  christos     {
    862      1.1  christos         Same = AcpiDmIsTargetAnOperand (Target->Common.Next,
    863      1.1  christos             Operand->Common.Next, FALSE);
    864      1.1  christos         if (!Same)
    865      1.1  christos         {
    866      1.1  christos             return (FALSE);
    867      1.1  christos         }
    868      1.1  christos     }
    869      1.1  christos 
    870      1.1  christos     /* Supress the duplicate operand at the top-level */
    871      1.1  christos 
    872      1.1  christos     if (TopLevel)
    873      1.1  christos     {
    874      1.1  christos         Operand->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
    875      1.1  christos     }
    876      1.1  christos     return (TRUE);
    877      1.1  christos }
    878