1 /*******************************************************************************
3 * Module Name: dmcstyle - Support for C-style operator disassembly
5 ******************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2015, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************/
117 #include "accommon.h"
118 #include "acparser.h"
120 #include "acdisasm.h"
123 #ifdef ACPI_DISASSEMBLER
125 #define _COMPONENT ACPI_CA_DEBUGGER
126 ACPI_MODULE_NAME ("dmcstyle")
129 /* Local prototypes */
132 AcpiDmGetCompoundSymbol (
136 AcpiDmPromoteTarget (
137 ACPI_PARSE_OBJECT *Op,
138 ACPI_PARSE_OBJECT *Target);
141 AcpiDmIsValidTarget (
142 ACPI_PARSE_OBJECT *Op);
145 AcpiDmIsTargetAnOperand (
146 ACPI_PARSE_OBJECT *Target,
147 ACPI_PARSE_OBJECT *Operand,
151 /*******************************************************************************
153 * FUNCTION: AcpiDmCheckForSymbolicOpcode
155 * PARAMETERS: Op - Current parse object
156 * Walk - Current parse tree walk info
158 * RETURN: TRUE if opcode can be converted to symbolic, FALSE otherwise
160 * DESCRIPTION: This is the main code that implements disassembly of AML code
161 * to C-style operators. Called during descending phase of the
164 ******************************************************************************/
167 AcpiDmCheckForSymbolicOpcode (
168 ACPI_PARSE_OBJECT *Op,
169 ACPI_OP_WALK_INFO *Info)
171 char *OperatorSymbol = NULL;
172 ACPI_PARSE_OBJECT *Child1;
173 ACPI_PARSE_OBJECT *Child2;
174 ACPI_PARSE_OBJECT *Target;
177 /* Exit immediately if ASL+ not enabled */
179 if (!AcpiGbl_CstyleDisassembly)
184 /* Get the first operand */
186 Child1 = AcpiPsGetArg (Op, 0);
192 /* Get the second operand */
194 Child2 = Child1->Common.Next;
196 /* Setup the operator string for this opcode */
198 switch (Op->Common.AmlOpcode)
201 OperatorSymbol = " + ";
204 case AML_SUBTRACT_OP:
205 OperatorSymbol = " - ";
208 case AML_MULTIPLY_OP:
209 OperatorSymbol = " * ";
213 OperatorSymbol = " / ";
217 OperatorSymbol = " % ";
220 case AML_SHIFT_LEFT_OP:
221 OperatorSymbol = " << ";
224 case AML_SHIFT_RIGHT_OP:
225 OperatorSymbol = " >> ";
229 OperatorSymbol = " & ";
233 OperatorSymbol = " | ";
237 OperatorSymbol = " ^ ";
240 /* Logical operators, no target */
243 OperatorSymbol = " && ";
247 OperatorSymbol = " == ";
250 case AML_LGREATER_OP:
251 OperatorSymbol = " > ";
255 OperatorSymbol = " < ";
259 OperatorSymbol = " || ";
264 * Check for the LNOT sub-opcodes. These correspond to
265 * LNotEqual, LLessEqual, and LGreaterEqual. There are
266 * no actual AML opcodes for these operators.
268 switch (Child1->Common.AmlOpcode)
271 OperatorSymbol = " != ";
274 case AML_LGREATER_OP:
275 OperatorSymbol = " <= ";
279 OperatorSymbol = " >= ";
284 /* Unary LNOT case, emit "!" immediately */
290 Child1->Common.DisasmOpcode = ACPI_DASM_LNOT_SUFFIX;
291 Op->Common.DisasmOpcode = ACPI_DASM_LNOT_PREFIX;
293 /* Save symbol string in the next child (not peer) */
295 Child2 = AcpiPsGetArg (Child1, 0);
301 Child2->Common.OperatorSymbol = OperatorSymbol;
306 Child1->Common.OperatorSymbol = " [";
307 Child2->Common.OperatorSymbol = "]";
311 /* Unary operators */
313 case AML_DECREMENT_OP:
314 OperatorSymbol = "--";
317 case AML_INCREMENT_OP:
318 OperatorSymbol = "++";
323 OperatorSymbol = NULL;
330 if (Child1->Common.DisasmOpcode == ACPI_DASM_LNOT_SUFFIX)
336 * This is the key to how the disassembly of the C-style operators
337 * works. We save the operator symbol in the first child, thus
338 * deferring symbol output until after the first operand has been
341 if (!Child1->Common.OperatorSymbol)
343 Child1->Common.OperatorSymbol = OperatorSymbol;
347 * Check for a valid target as the 3rd (or sometimes 2nd) operand
349 * Compound assignment operator support:
350 * Attempt to optimize constructs of the form:
351 * Add (Local1, 0xFF, Local1)
355 * Only the math operators and Store() have a target.
356 * Logicals have no target.
358 switch (Op->Common.AmlOpcode)
361 case AML_SUBTRACT_OP:
362 case AML_MULTIPLY_OP:
365 case AML_SHIFT_LEFT_OP:
366 case AML_SHIFT_RIGHT_OP:
371 /* Target is 3rd operand */
373 Target = Child2->Common.Next;
374 if (Op->Common.AmlOpcode == AML_DIVIDE_OP)
377 * Divide has an extra target operand (Remainder).
378 * If this extra target is specified, it cannot be converted
379 * to a C-style operator
381 if (AcpiDmIsValidTarget (Target))
383 Child1->Common.OperatorSymbol = NULL;
387 Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
388 Target = Target->Common.Next;
391 /* Parser should ensure there is at least a placeholder target */
398 if (!AcpiDmIsValidTarget (Target))
400 /* Not a valid target (placeholder only, from parser) */
405 * Promote the target up to the first child in the parse
406 * tree. This is done because the target will be output
407 * first, in the form:
408 * <Target> = Operands...
410 AcpiDmPromoteTarget (Op, Target);
413 * Check for possible conversion to a "Compound Assignment".
415 * Determine if either operand is the same as the target
416 * and display compound assignment operator and other operand.
418 if ((AcpiDmIsTargetAnOperand (Target, Child1, TRUE)) ||
419 (AcpiDmIsTargetAnOperand (Target, Child2, TRUE)))
421 Target->Common.OperatorSymbol =
422 AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
424 /* Convert operator to compound assignment */
426 Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND;
427 Child1->Common.OperatorSymbol = NULL;
432 * If we are within a C-style expression, emit an extra open
433 * paren. Implemented by examining the parent op.
435 switch (Op->Common.Parent->Common.AmlOpcode)
438 case AML_SUBTRACT_OP:
439 case AML_MULTIPLY_OP:
442 case AML_SHIFT_LEFT_OP:
443 case AML_SHIFT_RIGHT_OP:
449 case AML_LGREATER_OP:
453 Op->Common.DisasmFlags |= ACPI_PARSEOP_ASSIGNMENT;
461 /* Normal output for ASL/AML operators with a target operand */
463 Target->Common.OperatorSymbol = " = (";
466 /* Binary operators, no parens */
468 case AML_DECREMENT_OP:
469 case AML_INCREMENT_OP:
475 /* Target is optional, 3rd operand */
477 Target = Child2->Common.Next;
478 if (AcpiDmIsValidTarget (Target))
480 AcpiDmPromoteTarget (Op, Target);
482 if (!Target->Common.OperatorSymbol)
484 Target->Common.OperatorSymbol = " = ";
492 * Target is the 2nd operand.
493 * We know the target is valid, it is not optional.
494 * In the parse tree, simply swap the target with the
495 * source so that the target is processed first.
497 Target = Child1->Common.Next;
498 AcpiDmPromoteTarget (Op, Target);
500 if (!Target->Common.OperatorSymbol)
502 Target->Common.OperatorSymbol = " = ";
508 /* Target is optional, 2nd operand */
510 Target = Child1->Common.Next;
516 if (AcpiDmIsValidTarget (Target))
518 /* Valid target, not a placeholder */
520 AcpiDmPromoteTarget (Op, Target);
521 Target->Common.OperatorSymbol = " = ~";
525 /* No target. Emit this prefix operator immediately */
535 /* All other operators, emit an open paren */
542 /*******************************************************************************
544 * FUNCTION: AcpiDmCloseOperator
546 * PARAMETERS: Op - Current parse object
550 * DESCRIPTION: Closes an operator by adding a closing parentheses if and
551 * when necessary. Called during ascending phase of the
554 ******************************************************************************/
557 AcpiDmCloseOperator (
558 ACPI_PARSE_OBJECT *Op)
561 /* Always emit paren if ASL+ disassembly disabled */
563 if (!AcpiGbl_CstyleDisassembly)
569 /* Check if we need to add an additional closing paren */
571 switch (Op->Common.AmlOpcode)
574 case AML_SUBTRACT_OP:
575 case AML_MULTIPLY_OP:
578 case AML_SHIFT_LEFT_OP:
579 case AML_SHIFT_RIGHT_OP:
585 case AML_LGREATER_OP:
589 /* Emit paren only if this is not a compound assignment */
591 if (Op->Common.DisasmFlags & ACPI_PARSEOP_COMPOUND)
596 /* Emit extra close paren for assignment within an expression */
598 if (Op->Common.DisasmFlags & ACPI_PARSEOP_ASSIGNMENT)
605 /* No need for parens for these */
610 case AML_DECREMENT_OP:
611 case AML_INCREMENT_OP:
619 /* Always emit paren for non-ASL+ operators */
627 /*******************************************************************************
629 * FUNCTION: AcpiDmGetCompoundSymbol
631 * PARAMETERS: AslOpcode
633 * RETURN: String containing the compound assignment symbol
635 * DESCRIPTION: Detect opcodes that can be converted to compound assignment,
636 * return the appropriate operator string.
638 ******************************************************************************/
641 AcpiDmGetCompoundSymbol (
653 case AML_SUBTRACT_OP:
657 case AML_MULTIPLY_OP:
669 case AML_SHIFT_LEFT_OP:
673 case AML_SHIFT_RIGHT_OP:
691 /* No operator string for all other opcodes */
699 /*******************************************************************************
701 * FUNCTION: AcpiDmPromoteTarget
703 * PARAMETERS: Op - Operator parse object
704 * Target - Target associate with the Op
708 * DESCRIPTION: Transform the parse tree by moving the target up to the first
711 ******************************************************************************/
714 AcpiDmPromoteTarget (
715 ACPI_PARSE_OBJECT *Op,
716 ACPI_PARSE_OBJECT *Target)
718 ACPI_PARSE_OBJECT *Child;
721 /* Link target directly to the Op as first child */
723 Child = Op->Common.Value.Arg;
724 Op->Common.Value.Arg = Target;
725 Target->Common.Next = Child;
727 /* Find the last peer, it is linked to the target. Unlink it. */
729 while (Child->Common.Next != Target)
731 Child = Child->Common.Next;
734 Child->Common.Next = NULL;
738 /*******************************************************************************
740 * FUNCTION: AcpiDmIsValidTarget
742 * PARAMETERS: Target - Target Op from the parse tree
744 * RETURN: TRUE if the Target is real. FALSE if it is just a placeholder
745 * Op that was inserted by the parser.
747 * DESCRIPTION: Determine if a Target Op is a placeholder Op or a real Target.
748 * In other words, determine if the optional target is used or
751 ******************************************************************************/
754 AcpiDmIsValidTarget (
755 ACPI_PARSE_OBJECT *Target)
758 if ((Target->Common.AmlOpcode == AML_INT_NAMEPATH_OP) &&
759 (Target->Common.Value.Arg == NULL))
768 /*******************************************************************************
770 * FUNCTION: AcpiDmIsTargetAnOperand
772 * PARAMETERS: Target - Target associated with the expression
773 * Operand - An operand associated with expression
775 * RETURN: TRUE if expression can be converted to a compound assignment.
778 * DESCRIPTION: Determine if the Target duplicates the operand, in order to
779 * detect if the expression can be converted to a compound
780 * assigment. (+=, *=, etc.)
782 ******************************************************************************/
785 AcpiDmIsTargetAnOperand (
786 ACPI_PARSE_OBJECT *Target,
787 ACPI_PARSE_OBJECT *Operand,
790 const ACPI_OPCODE_INFO *OpInfo;
795 * Opcodes must match. Note: ignoring the difference between nameseg
796 * and namepath for now. May be needed later.
798 if (Target->Common.AmlOpcode != Operand->Common.AmlOpcode)
803 /* Nodes should match, even if they are NULL */
805 if (Target->Common.Node != Operand->Common.Node)
810 /* Determine if a child exists */
812 OpInfo = AcpiPsGetOpcodeInfo (Operand->Common.AmlOpcode);
813 if (OpInfo->Flags & AML_HAS_ARGS)
815 Same = AcpiDmIsTargetAnOperand (Target->Common.Value.Arg,
816 Operand->Common.Value.Arg, FALSE);
823 /* Check the next peer, as long as we are not at the top level */
828 Same = AcpiDmIsTargetAnOperand (Target->Common.Next,
829 Operand->Common.Next, FALSE);
836 /* Supress the duplicate operand at the top-level */
840 Operand->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;