1 /*******************************************************************************
3 * Module Name: dmcstyle - Support for C-style operator disassembly
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
50 #ifdef ACPI_DISASSEMBLER
52 #define _COMPONENT ACPI_CA_DEBUGGER
53 ACPI_MODULE_NAME ("dmcstyle")
56 /* Local prototypes */
59 AcpiDmGetCompoundSymbol (
64 ACPI_PARSE_OBJECT *Op,
65 ACPI_PARSE_OBJECT *Target);
69 ACPI_PARSE_OBJECT *Op);
72 AcpiDmIsTargetAnOperand (
73 ACPI_PARSE_OBJECT *Target,
74 ACPI_PARSE_OBJECT *Operand,
78 AcpiDmIsOptimizationIgnored (
79 ACPI_PARSE_OBJECT *StoreOp,
80 ACPI_PARSE_OBJECT *StoreArgument);
83 /*******************************************************************************
85 * FUNCTION: AcpiDmCheckForSymbolicOpcode
87 * PARAMETERS: Op - Current parse object
88 * Walk - Current parse tree walk info
90 * RETURN: TRUE if opcode can be converted to symbolic, FALSE otherwise
92 * DESCRIPTION: This is the main code that implements disassembly of AML code
93 * to C-style operators. Called during descending phase of the
96 ******************************************************************************/
99 AcpiDmCheckForSymbolicOpcode (
100 ACPI_PARSE_OBJECT *Op,
101 ACPI_OP_WALK_INFO *Info)
103 char *OperatorSymbol = NULL;
104 ACPI_PARSE_OBJECT *Argument1;
105 ACPI_PARSE_OBJECT *Argument2;
106 ACPI_PARSE_OBJECT *Target;
107 ACPI_PARSE_OBJECT *Target2;
110 /* Exit immediately if ASL+ not enabled */
112 if (!AcpiGbl_CstyleDisassembly)
117 /* Get the first operand */
119 Argument1 = AcpiPsGetArg (Op, 0);
125 /* Get the second operand */
127 Argument2 = Argument1->Common.Next;
129 /* Setup the operator string for this opcode */
131 switch (Op->Common.AmlOpcode)
134 OperatorSymbol = " + ";
137 case AML_SUBTRACT_OP:
138 OperatorSymbol = " - ";
141 case AML_MULTIPLY_OP:
142 OperatorSymbol = " * ";
146 OperatorSymbol = " / ";
150 OperatorSymbol = " % ";
153 case AML_SHIFT_LEFT_OP:
154 OperatorSymbol = " << ";
157 case AML_SHIFT_RIGHT_OP:
158 OperatorSymbol = " >> ";
162 OperatorSymbol = " & ";
166 OperatorSymbol = " | ";
170 OperatorSymbol = " ^ ";
173 /* Logical operators, no target */
176 OperatorSymbol = " && ";
180 OperatorSymbol = " == ";
183 case AML_LGREATER_OP:
184 OperatorSymbol = " > ";
188 OperatorSymbol = " < ";
192 OperatorSymbol = " || ";
197 * Check for the LNOT sub-opcodes. These correspond to
198 * LNotEqual, LLessEqual, and LGreaterEqual. There are
199 * no actual AML opcodes for these operators.
201 switch (Argument1->Common.AmlOpcode)
204 OperatorSymbol = " != ";
207 case AML_LGREATER_OP:
208 OperatorSymbol = " <= ";
212 OperatorSymbol = " >= ";
217 /* Unary LNOT case, emit "!" immediately */
223 Argument1->Common.DisasmOpcode = ACPI_DASM_LNOT_SUFFIX;
224 Op->Common.DisasmOpcode = ACPI_DASM_LNOT_PREFIX;
226 /* Save symbol string in the next child (not peer) */
228 Argument2 = AcpiPsGetArg (Argument1, 0);
234 Argument2->Common.OperatorSymbol = OperatorSymbol;
239 * Check for constant source operand. Note: although technically
240 * legal syntax, the iASL compiler does not support this with
241 * the symbolic operators for Index(). It doesn't make sense to
242 * use Index() with a constant anyway.
244 if ((Argument1->Common.AmlOpcode == AML_STRING_OP) ||
245 (Argument1->Common.AmlOpcode == AML_BUFFER_OP) ||
246 (Argument1->Common.AmlOpcode == AML_PACKAGE_OP) ||
247 (Argument1->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
249 Op->Common.DisasmFlags |= ACPI_PARSEOP_CLOSING_PAREN;
253 /* Index operator is [] */
255 Argument1->Common.OperatorSymbol = " [";
256 Argument2->Common.OperatorSymbol = "]";
259 /* Unary operators */
261 case AML_DECREMENT_OP:
262 OperatorSymbol = "--";
265 case AML_INCREMENT_OP:
266 OperatorSymbol = "++";
271 OperatorSymbol = NULL;
278 if (Argument1->Common.DisasmOpcode == ACPI_DASM_LNOT_SUFFIX)
284 * This is the key to how the disassembly of the C-style operators
285 * works. We save the operator symbol in the first child, thus
286 * deferring symbol output until after the first operand has been
289 if (!Argument1->Common.OperatorSymbol)
291 Argument1->Common.OperatorSymbol = OperatorSymbol;
295 * Check for a valid target as the 3rd (or sometimes 2nd) operand
297 * Compound assignment operator support:
298 * Attempt to optimize constructs of the form:
299 * Add (Local1, 0xFF, Local1)
303 * Only the math operators and Store() have a target.
304 * Logicals have no target.
306 switch (Op->Common.AmlOpcode)
309 case AML_SUBTRACT_OP:
310 case AML_MULTIPLY_OP:
313 case AML_SHIFT_LEFT_OP:
314 case AML_SHIFT_RIGHT_OP:
319 /* Target is 3rd operand */
321 Target = Argument2->Common.Next;
322 if (Op->Common.AmlOpcode == AML_DIVIDE_OP)
324 Target2 = Target->Common.Next;
327 * Divide has an extra target operand (Remainder).
328 * Default behavior is to simply ignore ASL+ conversion
329 * if the remainder target (modulo) is specified.
331 if (!AcpiGbl_DoDisassemblerOptimizations)
333 if (AcpiDmIsValidTarget (Target))
335 Argument1->Common.OperatorSymbol = NULL;
336 Op->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
340 Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
346 * Divide has an extra target operand (Remainder).
347 * If both targets are specified, it cannot be converted
348 * to a C-style operator.
350 if (AcpiDmIsValidTarget (Target) &&
351 AcpiDmIsValidTarget (Target2))
353 Argument1->Common.OperatorSymbol = NULL;
354 Op->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
358 if (AcpiDmIsValidTarget (Target)) /* Only first Target is valid (remainder) */
360 /* Convert the Divide to Modulo */
362 Op->Common.AmlOpcode = AML_MOD_OP;
364 Argument1->Common.OperatorSymbol = " % ";
365 Target2->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
367 else /* Only second Target (quotient) is valid */
369 Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
375 /* Parser should ensure there is at least a placeholder target */
382 if (!AcpiDmIsValidTarget (Target))
384 /* Not a valid target (placeholder only, from parser) */
389 * Promote the target up to the first child in the parse
390 * tree. This is done because the target will be output
391 * first, in the form:
392 * <Target> = Operands...
394 AcpiDmPromoteTarget (Op, Target);
396 /* Check operands for conversion to a "Compound Assignment" */
398 switch (Op->Common.AmlOpcode)
400 /* Commutative operators */
403 case AML_MULTIPLY_OP:
408 * For the commutative operators, we can convert to a
409 * compound statement only if at least one (either) operand
410 * is the same as the target.
412 * Add (A, B, A) --> A += B
413 * Add (B, A, A) --> A += B
414 * Add (B, C, A) --> A = (B + C)
416 if ((AcpiDmIsTargetAnOperand (Target, Argument1, TRUE)) ||
417 (AcpiDmIsTargetAnOperand (Target, Argument2, TRUE)))
419 Target->Common.OperatorSymbol =
420 AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
422 /* Convert operator to compound assignment */
424 Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
425 Argument1->Common.OperatorSymbol = NULL;
430 /* Non-commutative operators */
432 case AML_SUBTRACT_OP:
435 case AML_SHIFT_LEFT_OP:
436 case AML_SHIFT_RIGHT_OP:
438 * For the non-commutative operators, we can convert to a
439 * compound statement only if the target is the same as the
442 * Subtract (A, B, A) --> A -= B
443 * Subtract (B, A, A) --> A = (B - A)
445 if ((AcpiDmIsTargetAnOperand (Target, Argument1, TRUE)))
447 Target->Common.OperatorSymbol =
448 AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
450 /* Convert operator to compound assignment */
452 Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
453 Argument1->Common.OperatorSymbol = NULL;
463 * If we are within a C-style expression, emit an extra open
464 * paren. Implemented by examining the parent op.
466 switch (Op->Common.Parent->Common.AmlOpcode)
469 case AML_SUBTRACT_OP:
470 case AML_MULTIPLY_OP:
473 case AML_SHIFT_LEFT_OP:
474 case AML_SHIFT_RIGHT_OP:
480 case AML_LGREATER_OP:
484 Op->Common.DisasmFlags |= ACPI_PARSEOP_ASSIGNMENT;
492 /* Normal output for ASL/AML operators with a target operand */
494 Target->Common.OperatorSymbol = " = (";
497 /* Binary operators, no parens */
499 case AML_DECREMENT_OP:
500 case AML_INCREMENT_OP:
505 /* Target is optional, 3rd operand */
507 Target = Argument2->Common.Next;
508 if (AcpiDmIsValidTarget (Target))
510 AcpiDmPromoteTarget (Op, Target);
512 if (!Target->Common.OperatorSymbol)
514 Target->Common.OperatorSymbol = " = ";
521 * For Store, the Target is the 2nd operand. We know the target
522 * is valid, because it is not optional.
524 * Ignore any optimizations/folding if flag is set.
525 * Used for iASL/disassembler test suite only.
527 if (AcpiDmIsOptimizationIgnored (Op, Argument1))
533 * Perform conversion.
534 * In the parse tree, simply swap the target with the
535 * source so that the target is processed first.
537 Target = Argument1->Common.Next;
543 AcpiDmPromoteTarget (Op, Target);
544 if (!Target->Common.OperatorSymbol)
546 Target->Common.OperatorSymbol = " = ";
552 /* Target is optional, 2nd operand */
554 Target = Argument1->Common.Next;
560 if (AcpiDmIsValidTarget (Target))
562 /* Valid target, not a placeholder */
564 AcpiDmPromoteTarget (Op, Target);
565 Target->Common.OperatorSymbol = " = ~";
569 /* No target. Emit this prefix operator immediately */
579 /* All other operators, emit an open paren */
586 /*******************************************************************************
588 * FUNCTION: AcpiDmIsOptimizationIgnored
590 * PARAMETERS: StoreOp - Store operator parse object
591 * StoreArgument - Target associate with the Op
593 * RETURN: TRUE if this Store operator should not be converted/removed.
595 * DESCRIPTION: The following function implements "Do not optimize if a
596 * store is immediately followed by a math/bit operator that
599 * Function is ignored if DoDisassemblerOptimizations is TRUE.
600 * This is the default, ignore this function.
602 * Disables these types of optimizations, and simply emits
604 * Store (Add (INT1, 4), INT2) --> Add (INT1, 4, INT2)
605 * --> INT2 = INT1 + 4
607 * Store (Not (INT1), INT2) --> Not (INT1, INT2)
610 * Used only for the ASL test suite. For the test suite, we
611 * don't want to perform some optimizations to ensure binary
612 * compatibility with the generation of the legacy ASL->AML.
613 * In other words, for all test modules we want exactly:
614 * (ASL+ -> AML) == (ASL- -> AML)
616 ******************************************************************************/
619 AcpiDmIsOptimizationIgnored (
620 ACPI_PARSE_OBJECT *StoreOp,
621 ACPI_PARSE_OBJECT *StoreArgument)
623 ACPI_PARSE_OBJECT *Argument1;
624 ACPI_PARSE_OBJECT *Argument2;
625 ACPI_PARSE_OBJECT *Target;
628 /* No optimizations/folding for the typical case */
630 if (AcpiGbl_DoDisassemblerOptimizations)
636 * Only a small subset of ASL/AML operators can be optimized.
637 * Can only optimize/fold if there is no target (or targets)
638 * specified for the operator. And of course, the operator
639 * is surrrounded by a Store() operator.
641 switch (StoreArgument->Common.AmlOpcode)
644 case AML_SUBTRACT_OP:
645 case AML_MULTIPLY_OP:
647 case AML_SHIFT_LEFT_OP:
648 case AML_SHIFT_RIGHT_OP:
654 /* These operators have two arguments and one target */
656 Argument1 = StoreArgument->Common.Value.Arg;
657 Argument2 = Argument1->Common.Next;
658 Target = Argument2->Common.Next;
660 if (!AcpiDmIsValidTarget (Target))
662 StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
669 /* This operator has two arguments and two targets */
671 Argument1 = StoreArgument->Common.Value.Arg;
672 Argument2 = Argument1->Common.Next;
673 Target = Argument2->Common.Next;
675 if (!AcpiDmIsValidTarget (Target) ||
676 !AcpiDmIsValidTarget (Target->Common.Next))
678 StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
685 /* This operator has one operand and one target */
687 Argument1 = StoreArgument->Common.Value.Arg;
688 Target = Argument1->Common.Next;
690 if (!AcpiDmIsValidTarget (Target))
692 StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
705 /*******************************************************************************
707 * FUNCTION: AcpiDmCloseOperator
709 * PARAMETERS: Op - Current parse object
713 * DESCRIPTION: Closes an operator by adding a closing parentheses if and
714 * when necessary. Called during ascending phase of the
717 ******************************************************************************/
720 AcpiDmCloseOperator (
721 ACPI_PARSE_OBJECT *Op)
724 /* Always emit paren if ASL+ disassembly disabled */
726 if (!AcpiGbl_CstyleDisassembly)
732 if (Op->Common.DisasmFlags & ACPI_PARSEOP_LEGACY_ASL_ONLY)
738 /* Check if we need to add an additional closing paren */
740 switch (Op->Common.AmlOpcode)
743 case AML_SUBTRACT_OP:
744 case AML_MULTIPLY_OP:
747 case AML_SHIFT_LEFT_OP:
748 case AML_SHIFT_RIGHT_OP:
754 case AML_LGREATER_OP:
758 /* Emit paren only if this is not a compound assignment */
760 if (Op->Common.DisasmFlags & ACPI_PARSEOP_COMPOUND_ASSIGNMENT)
765 /* Emit extra close paren for assignment within an expression */
767 if (Op->Common.DisasmFlags & ACPI_PARSEOP_ASSIGNMENT)
775 /* This is case for unsupported Index() source constants */
777 if (Op->Common.DisasmFlags & ACPI_PARSEOP_CLOSING_PAREN)
783 /* No need for parens for these */
785 case AML_DECREMENT_OP:
786 case AML_INCREMENT_OP:
794 /* Always emit paren for non-ASL+ operators */
802 /*******************************************************************************
804 * FUNCTION: AcpiDmGetCompoundSymbol
806 * PARAMETERS: AslOpcode
808 * RETURN: String containing the compound assignment symbol
810 * DESCRIPTION: Detect opcodes that can be converted to compound assignment,
811 * return the appropriate operator string.
813 ******************************************************************************/
816 AcpiDmGetCompoundSymbol (
828 case AML_SUBTRACT_OP:
832 case AML_MULTIPLY_OP:
844 case AML_SHIFT_LEFT_OP:
848 case AML_SHIFT_RIGHT_OP:
866 /* No operator string for all other opcodes */
875 /*******************************************************************************
877 * FUNCTION: AcpiDmPromoteTarget
879 * PARAMETERS: Op - Operator parse object
880 * Target - Target associate with the Op
884 * DESCRIPTION: Transform the parse tree by moving the target up to the first
887 ******************************************************************************/
890 AcpiDmPromoteTarget (
891 ACPI_PARSE_OBJECT *Op,
892 ACPI_PARSE_OBJECT *Target)
894 ACPI_PARSE_OBJECT *Child;
897 /* Link target directly to the Op as first child */
899 Child = Op->Common.Value.Arg;
900 Op->Common.Value.Arg = Target;
901 Target->Common.Next = Child;
903 /* Find the last peer, it is linked to the target. Unlink it. */
905 while (Child->Common.Next != Target)
907 Child = Child->Common.Next;
910 Child->Common.Next = NULL;
914 /*******************************************************************************
916 * FUNCTION: AcpiDmIsValidTarget
918 * PARAMETERS: Target - Target Op from the parse tree
920 * RETURN: TRUE if the Target is real. FALSE if it is just a placeholder
921 * Op that was inserted by the parser.
923 * DESCRIPTION: Determine if a Target Op is a placeholder Op or a real Target.
924 * In other words, determine if the optional target is used or
925 * not. Note: If Target is NULL, something is seriously wrong,
926 * probably with the parse tree.
928 ******************************************************************************/
931 AcpiDmIsValidTarget (
932 ACPI_PARSE_OBJECT *Target)
940 if ((Target->Common.AmlOpcode == AML_INT_NAMEPATH_OP) &&
941 (Target->Common.Value.Arg == NULL))
950 /*******************************************************************************
952 * FUNCTION: AcpiDmIsTargetAnOperand
954 * PARAMETERS: Target - Target associated with the expression
955 * Operand - An operand associated with expression
957 * RETURN: TRUE if expression can be converted to a compound assignment.
960 * DESCRIPTION: Determine if the Target duplicates the operand, in order to
961 * detect if the expression can be converted to a compound
962 * assigment. (+=, *=, etc.)
964 ******************************************************************************/
967 AcpiDmIsTargetAnOperand (
968 ACPI_PARSE_OBJECT *Target,
969 ACPI_PARSE_OBJECT *Operand,
972 const ACPI_OPCODE_INFO *OpInfo;
977 * Opcodes must match. Note: ignoring the difference between nameseg
978 * and namepath for now. May be needed later.
980 if (Target->Common.AmlOpcode != Operand->Common.AmlOpcode)
985 /* Nodes should match, even if they are NULL */
987 if (Target->Common.Node != Operand->Common.Node)
992 /* Determine if a child exists */
994 OpInfo = AcpiPsGetOpcodeInfo (Operand->Common.AmlOpcode);
995 if (OpInfo->Flags & AML_HAS_ARGS)
997 Same = AcpiDmIsTargetAnOperand (Target->Common.Value.Arg,
998 Operand->Common.Value.Arg, FALSE);
1005 /* Check the next peer, as long as we are not at the top level */
1008 Target->Common.Next)
1010 Same = AcpiDmIsTargetAnOperand (Target->Common.Next,
1011 Operand->Common.Next, FALSE);
1018 /* Supress the duplicate operand at the top-level */
1022 Operand->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;