wg.conf.5: Fix a typo (in-inline comments are *not* allowed)
[dragonfly.git] / sys / contrib / dev / acpica / source / components / disassembler / dmcstyle.c
blob0b82cc83c3af96007c20bdbe534fe81ab95a0053
1 /*******************************************************************************
3 * Module Name: dmcstyle - Support for C-style operator disassembly
5 ******************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2021, Intel Corp.
12 * All rights reserved.
14 * 2. License
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
19 * property rights.
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
38 * conditions are met:
40 * 3. Conditions
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
64 * make.
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
70 * distribution.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
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
88 * PARTICULAR PURPOSE.
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
97 * LIMITED REMEDY.
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 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
121 * are met:
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
152 #include "acpi.h"
153 #include "accommon.h"
154 #include "acparser.h"
155 #include "amlcode.h"
156 #include "acdebug.h"
157 #include "acconvert.h"
159 #ifdef ACPI_DISASSEMBLER
161 #define _COMPONENT ACPI_CA_DEBUGGER
162 ACPI_MODULE_NAME ("dmcstyle")
165 /* Local prototypes */
167 static char *
168 AcpiDmGetCompoundSymbol (
169 UINT16 AslOpcode);
171 static void
172 AcpiDmPromoteTarget (
173 ACPI_PARSE_OBJECT *Op,
174 ACPI_PARSE_OBJECT *Target);
176 static BOOLEAN
177 AcpiDmIsValidTarget (
178 ACPI_PARSE_OBJECT *Op);
180 static BOOLEAN
181 AcpiDmIsTargetAnOperand (
182 ACPI_PARSE_OBJECT *Target,
183 ACPI_PARSE_OBJECT *Operand,
184 BOOLEAN TopLevel);
186 static BOOLEAN
187 AcpiDmIsOptimizationIgnored (
188 ACPI_PARSE_OBJECT *StoreOp,
189 ACPI_PARSE_OBJECT *StoreArgument);
192 /*******************************************************************************
194 * FUNCTION: AcpiDmCheckForSymbolicOpcode
196 * PARAMETERS: Op - Current parse object
197 * Walk - Current parse tree walk info
199 * RETURN: TRUE if opcode can be converted to symbolic, FALSE otherwise
201 * DESCRIPTION: This is the main code that implements disassembly of AML code
202 * to C-style operators. Called during descending phase of the
203 * parse tree walk.
205 ******************************************************************************/
207 BOOLEAN
208 AcpiDmCheckForSymbolicOpcode (
209 ACPI_PARSE_OBJECT *Op,
210 ACPI_OP_WALK_INFO *Info)
212 char *OperatorSymbol = NULL;
213 ACPI_PARSE_OBJECT *Argument1;
214 ACPI_PARSE_OBJECT *Argument2;
215 ACPI_PARSE_OBJECT *Target;
216 ACPI_PARSE_OBJECT *Target2;
219 /* Exit immediately if ASL+ not enabled */
221 if (!AcpiGbl_CstyleDisassembly)
223 return (FALSE);
226 /* Get the first operand */
228 Argument1 = AcpiPsGetArg (Op, 0);
229 if (!Argument1)
231 return (FALSE);
234 /* Get the second operand */
236 Argument2 = Argument1->Common.Next;
238 /* Setup the operator string for this opcode */
240 switch (Op->Common.AmlOpcode)
242 case AML_ADD_OP:
243 OperatorSymbol = " + ";
244 break;
246 case AML_SUBTRACT_OP:
247 OperatorSymbol = " - ";
248 break;
250 case AML_MULTIPLY_OP:
251 OperatorSymbol = " * ";
252 break;
254 case AML_DIVIDE_OP:
255 OperatorSymbol = " / ";
256 break;
258 case AML_MOD_OP:
259 OperatorSymbol = " % ";
260 break;
262 case AML_SHIFT_LEFT_OP:
263 OperatorSymbol = " << ";
264 break;
266 case AML_SHIFT_RIGHT_OP:
267 OperatorSymbol = " >> ";
268 break;
270 case AML_BIT_AND_OP:
271 OperatorSymbol = " & ";
272 break;
274 case AML_BIT_OR_OP:
275 OperatorSymbol = " | ";
276 break;
278 case AML_BIT_XOR_OP:
279 OperatorSymbol = " ^ ";
280 break;
282 /* Logical operators, no target */
284 case AML_LOGICAL_AND_OP:
285 OperatorSymbol = " && ";
286 break;
288 case AML_LOGICAL_EQUAL_OP:
289 OperatorSymbol = " == ";
290 break;
292 case AML_LOGICAL_GREATER_OP:
293 OperatorSymbol = " > ";
294 break;
296 case AML_LOGICAL_LESS_OP:
297 OperatorSymbol = " < ";
298 break;
300 case AML_LOGICAL_OR_OP:
301 OperatorSymbol = " || ";
302 break;
304 case AML_LOGICAL_NOT_OP:
306 * Check for the LNOT sub-opcodes. These correspond to
307 * LNotEqual, LLessEqual, and LGreaterEqual. There are
308 * no actual AML opcodes for these operators.
310 switch (Argument1->Common.AmlOpcode)
312 case AML_LOGICAL_EQUAL_OP:
313 OperatorSymbol = " != ";
314 break;
316 case AML_LOGICAL_GREATER_OP:
317 OperatorSymbol = " <= ";
318 break;
320 case AML_LOGICAL_LESS_OP:
321 OperatorSymbol = " >= ";
322 break;
324 default:
326 /* Unary LNOT case, emit "!" immediately */
328 AcpiOsPrintf ("!");
329 return (TRUE);
332 Argument1->Common.DisasmOpcode = ACPI_DASM_LNOT_SUFFIX;
333 Op->Common.DisasmOpcode = ACPI_DASM_LNOT_PREFIX;
335 /* Save symbol string in the next child (not peer) */
337 Argument2 = AcpiPsGetArg (Argument1, 0);
338 if (!Argument2)
340 return (FALSE);
343 Argument2->Common.OperatorSymbol = OperatorSymbol;
344 return (TRUE);
346 case AML_INDEX_OP:
348 * Check for constant source operand. Note: although technically
349 * legal syntax, the iASL compiler does not support this with
350 * the symbolic operators for Index(). It doesn't make sense to
351 * use Index() with a constant anyway.
353 if ((Argument1->Common.AmlOpcode == AML_STRING_OP) ||
354 (Argument1->Common.AmlOpcode == AML_BUFFER_OP) ||
355 (Argument1->Common.AmlOpcode == AML_PACKAGE_OP) ||
356 (Argument1->Common.AmlOpcode == AML_VARIABLE_PACKAGE_OP))
358 Op->Common.DisasmFlags |= ACPI_PARSEOP_CLOSING_PAREN;
359 return (FALSE);
362 /* Index operator is [] */
364 Argument1->Common.OperatorSymbol = " [";
365 Argument2->Common.OperatorSymbol = "]";
366 break;
368 /* Unary operators */
370 case AML_DECREMENT_OP:
371 OperatorSymbol = "--";
372 break;
374 case AML_INCREMENT_OP:
375 OperatorSymbol = "++";
376 break;
378 case AML_BIT_NOT_OP:
379 case AML_STORE_OP:
380 OperatorSymbol = NULL;
381 break;
383 default:
384 return (FALSE);
387 if (Argument1->Common.DisasmOpcode == ACPI_DASM_LNOT_SUFFIX)
389 return (TRUE);
393 * This is the key to how the disassembly of the C-style operators
394 * works. We save the operator symbol in the first child, thus
395 * deferring symbol output until after the first operand has been
396 * emitted.
398 if (!Argument1->Common.OperatorSymbol)
400 Argument1->Common.OperatorSymbol = OperatorSymbol;
404 * Check for a valid target as the 3rd (or sometimes 2nd) operand
406 * Compound assignment operator support:
407 * Attempt to optimize constructs of the form:
408 * Add (Local1, 0xFF, Local1)
409 * to:
410 * Local1 += 0xFF
412 * Only the math operators and Store() have a target.
413 * Logicals have no target.
415 switch (Op->Common.AmlOpcode)
417 case AML_ADD_OP:
418 case AML_SUBTRACT_OP:
419 case AML_MULTIPLY_OP:
420 case AML_DIVIDE_OP:
421 case AML_MOD_OP:
422 case AML_SHIFT_LEFT_OP:
423 case AML_SHIFT_RIGHT_OP:
424 case AML_BIT_AND_OP:
425 case AML_BIT_OR_OP:
426 case AML_BIT_XOR_OP:
428 /* Target is 3rd operand */
430 Target = Argument2->Common.Next;
431 if (Op->Common.AmlOpcode == AML_DIVIDE_OP)
433 Target2 = Target->Common.Next;
436 * Divide has an extra target operand (Remainder).
437 * Default behavior is to simply ignore ASL+ conversion
438 * if the remainder target (modulo) is specified.
440 if (!AcpiGbl_DoDisassemblerOptimizations)
442 if (AcpiDmIsValidTarget (Target))
444 Argument1->Common.OperatorSymbol = NULL;
445 Op->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
446 return (FALSE);
449 Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
450 Target = Target2;
452 else
455 * Divide has an extra target operand (Remainder).
456 * If both targets are specified, it cannot be converted
457 * to a C-style operator.
459 if (AcpiDmIsValidTarget (Target) &&
460 AcpiDmIsValidTarget (Target2))
462 Argument1->Common.OperatorSymbol = NULL;
463 Op->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
464 return (FALSE);
467 if (AcpiDmIsValidTarget (Target)) /* Only first Target is valid (remainder) */
469 /* Convert the Divide to Modulo */
471 Op->Common.AmlOpcode = AML_MOD_OP;
473 Argument1->Common.OperatorSymbol = " % ";
474 Target2->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
476 else /* Only second Target (quotient) is valid */
478 Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
479 Target = Target2;
484 /* Parser should ensure there is at least a placeholder target */
486 if (!Target)
488 return (FALSE);
491 if (!AcpiDmIsValidTarget (Target))
493 /* Not a valid target (placeholder only, from parser) */
494 break;
498 * Promote the target up to the first child in the parse
499 * tree. This is done because the target will be output
500 * first, in the form:
501 * <Target> = Operands...
503 AcpiDmPromoteTarget (Op, Target);
505 /* Check operands for conversion to a "Compound Assignment" */
507 switch (Op->Common.AmlOpcode)
509 /* Commutative operators */
511 case AML_ADD_OP:
512 case AML_MULTIPLY_OP:
513 case AML_BIT_AND_OP:
514 case AML_BIT_OR_OP:
515 case AML_BIT_XOR_OP:
517 * For the commutative operators, we can convert to a
518 * compound statement only if at least one (either) operand
519 * is the same as the target.
521 * Add (A, B, A) --> A += B
522 * Add (B, A, A) --> A += B
523 * Add (B, C, A) --> A = (B + C)
525 if ((AcpiDmIsTargetAnOperand (Target, Argument1, TRUE)) ||
526 (AcpiDmIsTargetAnOperand (Target, Argument2, TRUE)))
528 Target->Common.OperatorSymbol =
529 AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
531 /* Convert operator to compound assignment */
533 Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
534 Argument1->Common.OperatorSymbol = NULL;
535 return (TRUE);
537 break;
539 /* Non-commutative operators */
541 case AML_SUBTRACT_OP:
542 case AML_DIVIDE_OP:
543 case AML_MOD_OP:
544 case AML_SHIFT_LEFT_OP:
545 case AML_SHIFT_RIGHT_OP:
547 * For the non-commutative operators, we can convert to a
548 * compound statement only if the target is the same as the
549 * first operand.
551 * Subtract (A, B, A) --> A -= B
552 * Subtract (B, A, A) --> A = (B - A)
554 if ((AcpiDmIsTargetAnOperand (Target, Argument1, TRUE)))
556 Target->Common.OperatorSymbol =
557 AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
559 /* Convert operator to compound assignment */
561 Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
562 Argument1->Common.OperatorSymbol = NULL;
563 return (TRUE);
565 break;
567 default:
568 break;
572 * If we are within a C-style expression, emit an extra open
573 * paren. Implemented by examining the parent op.
575 switch (Op->Common.Parent->Common.AmlOpcode)
577 case AML_ADD_OP:
578 case AML_SUBTRACT_OP:
579 case AML_MULTIPLY_OP:
580 case AML_DIVIDE_OP:
581 case AML_MOD_OP:
582 case AML_SHIFT_LEFT_OP:
583 case AML_SHIFT_RIGHT_OP:
584 case AML_BIT_AND_OP:
585 case AML_BIT_OR_OP:
586 case AML_BIT_XOR_OP:
587 case AML_LOGICAL_AND_OP:
588 case AML_LOGICAL_EQUAL_OP:
589 case AML_LOGICAL_GREATER_OP:
590 case AML_LOGICAL_LESS_OP:
591 case AML_LOGICAL_OR_OP:
593 Op->Common.DisasmFlags |= ACPI_PARSEOP_ASSIGNMENT;
594 AcpiOsPrintf ("(");
595 break;
597 default:
598 break;
601 /* Normal output for ASL/AML operators with a target operand */
603 Target->Common.OperatorSymbol = " = (";
604 return (TRUE);
606 /* Binary operators, no parens */
608 case AML_DECREMENT_OP:
609 case AML_INCREMENT_OP:
610 return (TRUE);
612 case AML_INDEX_OP:
614 /* Target is optional, 3rd operand */
616 Target = Argument2->Common.Next;
617 if (AcpiDmIsValidTarget (Target))
619 AcpiDmPromoteTarget (Op, Target);
621 if (!Target->Common.OperatorSymbol)
623 Target->Common.OperatorSymbol = " = ";
626 return (TRUE);
628 case AML_STORE_OP:
630 * For Store, the Target is the 2nd operand. We know the target
631 * is valid, because it is not optional.
633 * Ignore any optimizations/folding if flag is set.
634 * Used for iASL/disassembler test suite only.
636 if (AcpiDmIsOptimizationIgnored (Op, Argument1))
638 return (FALSE);
642 * Perform conversion.
643 * In the parse tree, simply swap the target with the
644 * source so that the target is processed first.
646 Target = Argument1->Common.Next;
647 if (!Target)
649 return (FALSE);
652 AcpiDmPromoteTarget (Op, Target);
653 if (!Target->Common.OperatorSymbol)
655 Target->Common.OperatorSymbol = " = ";
657 return (TRUE);
659 case AML_BIT_NOT_OP:
661 /* Target is optional, 2nd operand */
663 Target = Argument1->Common.Next;
664 if (!Target)
666 return (FALSE);
669 if (AcpiDmIsValidTarget (Target))
671 /* Valid target, not a placeholder */
673 AcpiDmPromoteTarget (Op, Target);
674 Target->Common.OperatorSymbol = " = ~";
676 else
678 /* No target. Emit this prefix operator immediately */
680 AcpiOsPrintf ("~");
682 return (TRUE);
684 default:
685 break;
688 /* All other operators, emit an open paren */
690 AcpiOsPrintf ("(");
691 return (TRUE);
695 /*******************************************************************************
697 * FUNCTION: AcpiDmIsOptimizationIgnored
699 * PARAMETERS: StoreOp - Store operator parse object
700 * StoreArgument - Target associate with the Op
702 * RETURN: TRUE if this Store operator should not be converted/removed.
704 * DESCRIPTION: The following function implements "Do not optimize if a
705 * store is immediately followed by a math/bit operator that
706 * has no target".
708 * Function is ignored if DoDisassemblerOptimizations is TRUE.
709 * This is the default, ignore this function.
711 * Disables these types of optimizations, and simply emits
712 * legacy ASL code:
713 * Store (Add (INT1, 4), INT2) --> Add (INT1, 4, INT2)
714 * --> INT2 = INT1 + 4
716 * Store (Not (INT1), INT2) --> Not (INT1, INT2)
717 * --> INT2 = ~INT1
719 * Used only for the ASL test suite. For the test suite, we
720 * don't want to perform some optimizations to ensure binary
721 * compatibility with the generation of the legacy ASL->AML.
722 * In other words, for all test modules we want exactly:
723 * (ASL+ -> AML) == (ASL- -> AML)
725 ******************************************************************************/
727 static BOOLEAN
728 AcpiDmIsOptimizationIgnored (
729 ACPI_PARSE_OBJECT *StoreOp,
730 ACPI_PARSE_OBJECT *StoreArgument)
732 ACPI_PARSE_OBJECT *Argument1;
733 ACPI_PARSE_OBJECT *Argument2;
734 ACPI_PARSE_OBJECT *Target;
737 /* No optimizations/folding for the typical case */
739 if (AcpiGbl_DoDisassemblerOptimizations)
741 return (FALSE);
745 * Only a small subset of ASL/AML operators can be optimized.
746 * Can only optimize/fold if there is no target (or targets)
747 * specified for the operator. And of course, the operator
748 * is surrounded by a Store() operator.
750 switch (StoreArgument->Common.AmlOpcode)
752 case AML_ADD_OP:
753 case AML_SUBTRACT_OP:
754 case AML_MULTIPLY_OP:
755 case AML_MOD_OP:
756 case AML_SHIFT_LEFT_OP:
757 case AML_SHIFT_RIGHT_OP:
758 case AML_BIT_AND_OP:
759 case AML_BIT_OR_OP:
760 case AML_BIT_XOR_OP:
761 case AML_INDEX_OP:
763 /* These operators have two arguments and one target */
765 Argument1 = StoreArgument->Common.Value.Arg;
766 Argument2 = Argument1->Common.Next;
767 Target = Argument2->Common.Next;
769 if (!AcpiDmIsValidTarget (Target))
771 StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
772 return (TRUE);
774 break;
776 case AML_DIVIDE_OP:
778 /* This operator has two arguments and two targets */
780 Argument1 = StoreArgument->Common.Value.Arg;
781 Argument2 = Argument1->Common.Next;
782 Target = Argument2->Common.Next;
784 if (!AcpiDmIsValidTarget (Target) ||
785 !AcpiDmIsValidTarget (Target->Common.Next))
787 StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
788 return (TRUE);
790 break;
792 case AML_BIT_NOT_OP:
794 /* This operator has one operand and one target */
796 Argument1 = StoreArgument->Common.Value.Arg;
797 Target = Argument1->Common.Next;
799 if (!AcpiDmIsValidTarget (Target))
801 StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
802 return (TRUE);
804 break;
806 default:
807 break;
810 return (FALSE);
814 /*******************************************************************************
816 * FUNCTION: AcpiDmCloseOperator
818 * PARAMETERS: Op - Current parse object
820 * RETURN: None
822 * DESCRIPTION: Closes an operator by adding a closing parentheses if and
823 * when necessary. Called during ascending phase of the
824 * parse tree walk.
826 ******************************************************************************/
828 void
829 AcpiDmCloseOperator (
830 ACPI_PARSE_OBJECT *Op)
833 /* Always emit paren if ASL+ disassembly disabled */
835 if (!AcpiGbl_CstyleDisassembly)
837 AcpiOsPrintf (")");
838 ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
839 return;
842 if (Op->Common.DisasmFlags & ACPI_PARSEOP_LEGACY_ASL_ONLY)
844 AcpiOsPrintf (")");
845 ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
846 return;
849 /* Check if we need to add an additional closing paren */
851 switch (Op->Common.AmlOpcode)
853 case AML_ADD_OP:
854 case AML_SUBTRACT_OP:
855 case AML_MULTIPLY_OP:
856 case AML_DIVIDE_OP:
857 case AML_MOD_OP:
858 case AML_SHIFT_LEFT_OP:
859 case AML_SHIFT_RIGHT_OP:
860 case AML_BIT_AND_OP:
861 case AML_BIT_OR_OP:
862 case AML_BIT_XOR_OP:
863 case AML_LOGICAL_AND_OP:
864 case AML_LOGICAL_EQUAL_OP:
865 case AML_LOGICAL_GREATER_OP:
866 case AML_LOGICAL_LESS_OP:
867 case AML_LOGICAL_OR_OP:
869 /* Emit paren only if this is not a compound assignment */
871 if (Op->Common.DisasmFlags & ACPI_PARSEOP_COMPOUND_ASSIGNMENT)
873 ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
874 return;
877 /* Emit extra close paren for assignment within an expression */
879 if (Op->Common.DisasmFlags & ACPI_PARSEOP_ASSIGNMENT)
881 AcpiOsPrintf (")");
883 break;
885 case AML_INDEX_OP:
887 /* This is case for unsupported Index() source constants */
889 if (Op->Common.DisasmFlags & ACPI_PARSEOP_CLOSING_PAREN)
891 AcpiOsPrintf (")");
893 ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
894 return;
896 /* No need for parens for these */
898 case AML_DECREMENT_OP:
899 case AML_INCREMENT_OP:
900 case AML_LOGICAL_NOT_OP:
901 case AML_BIT_NOT_OP:
902 case AML_STORE_OP:
903 ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
904 return;
906 default:
908 /* Always emit paren for non-ASL+ operators */
909 break;
912 AcpiOsPrintf (")");
913 ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_END_NODE, NULL, 0);
915 return;
919 /*******************************************************************************
921 * FUNCTION: AcpiDmGetCompoundSymbol
923 * PARAMETERS: AslOpcode
925 * RETURN: String containing the compound assignment symbol
927 * DESCRIPTION: Detect opcodes that can be converted to compound assignment,
928 * return the appropriate operator string.
930 ******************************************************************************/
932 static char *
933 AcpiDmGetCompoundSymbol (
934 UINT16 AmlOpcode)
936 char *Symbol;
939 switch (AmlOpcode)
941 case AML_ADD_OP:
942 Symbol = " += ";
943 break;
945 case AML_SUBTRACT_OP:
946 Symbol = " -= ";
947 break;
949 case AML_MULTIPLY_OP:
950 Symbol = " *= ";
951 break;
953 case AML_DIVIDE_OP:
954 Symbol = " /= ";
955 break;
957 case AML_MOD_OP:
958 Symbol = " %= ";
959 break;
961 case AML_SHIFT_LEFT_OP:
962 Symbol = " <<= ";
963 break;
965 case AML_SHIFT_RIGHT_OP:
966 Symbol = " >>= ";
967 break;
969 case AML_BIT_AND_OP:
970 Symbol = " &= ";
971 break;
973 case AML_BIT_OR_OP:
974 Symbol = " |= ";
975 break;
977 case AML_BIT_XOR_OP:
978 Symbol = " ^= ";
979 break;
981 default:
983 /* No operator string for all other opcodes */
985 return (NULL);
988 return (Symbol);
992 /*******************************************************************************
994 * FUNCTION: AcpiDmPromoteTarget
996 * PARAMETERS: Op - Operator parse object
997 * Target - Target associate with the Op
999 * RETURN: None
1001 * DESCRIPTION: Transform the parse tree by moving the target up to the first
1002 * child of the Op.
1004 ******************************************************************************/
1006 static void
1007 AcpiDmPromoteTarget (
1008 ACPI_PARSE_OBJECT *Op,
1009 ACPI_PARSE_OBJECT *Target)
1011 ACPI_PARSE_OBJECT *Child;
1014 /* Link target directly to the Op as first child */
1016 Child = Op->Common.Value.Arg;
1017 Op->Common.Value.Arg = Target;
1018 Target->Common.Next = Child;
1020 /* Find the last peer, it is linked to the target. Unlink it. */
1022 while (Child->Common.Next != Target)
1024 Child = Child->Common.Next;
1027 Child->Common.Next = NULL;
1031 /*******************************************************************************
1033 * FUNCTION: AcpiDmIsValidTarget
1035 * PARAMETERS: Target - Target Op from the parse tree
1037 * RETURN: TRUE if the Target is real. FALSE if it is just a placeholder
1038 * Op that was inserted by the parser.
1040 * DESCRIPTION: Determine if a Target Op is a placeholder Op or a real Target.
1041 * In other words, determine if the optional target is used or
1042 * not. Note: If Target is NULL, something is seriously wrong,
1043 * probably with the parse tree.
1045 ******************************************************************************/
1047 static BOOLEAN
1048 AcpiDmIsValidTarget (
1049 ACPI_PARSE_OBJECT *Target)
1052 if (!Target)
1054 return (FALSE);
1057 if ((Target->Common.AmlOpcode == AML_INT_NAMEPATH_OP) &&
1058 (Target->Common.Value.Arg == NULL))
1060 return (FALSE);
1063 return (TRUE);
1067 /*******************************************************************************
1069 * FUNCTION: AcpiDmIsTargetAnOperand
1071 * PARAMETERS: Target - Target associated with the expression
1072 * Operand - An operand associated with expression
1074 * RETURN: TRUE if expression can be converted to a compound assignment.
1075 * FALSE otherwise.
1077 * DESCRIPTION: Determine if the Target duplicates the operand, in order to
1078 * detect if the expression can be converted to a compound
1079 * assignment. (+=, *=, etc.)
1081 ******************************************************************************/
1083 static BOOLEAN
1084 AcpiDmIsTargetAnOperand (
1085 ACPI_PARSE_OBJECT *Target,
1086 ACPI_PARSE_OBJECT *Operand,
1087 BOOLEAN TopLevel)
1089 const ACPI_OPCODE_INFO *OpInfo;
1090 BOOLEAN Same;
1094 * Opcodes must match. Note: ignoring the difference between nameseg
1095 * and namepath for now. May be needed later.
1097 if (Target->Common.AmlOpcode != Operand->Common.AmlOpcode)
1099 return (FALSE);
1102 /* Nodes should match, even if they are NULL */
1104 if (Target->Common.Node != Operand->Common.Node)
1106 return (FALSE);
1109 /* Determine if a child exists */
1111 OpInfo = AcpiPsGetOpcodeInfo (Operand->Common.AmlOpcode);
1112 if (OpInfo->Flags & AML_HAS_ARGS)
1114 Same = AcpiDmIsTargetAnOperand (Target->Common.Value.Arg,
1115 Operand->Common.Value.Arg, FALSE);
1116 if (!Same)
1118 return (FALSE);
1122 /* Check the next peer, as long as we are not at the top level */
1124 if ((!TopLevel) &&
1125 Target->Common.Next)
1127 Same = AcpiDmIsTargetAnOperand (Target->Common.Next,
1128 Operand->Common.Next, FALSE);
1129 if (!Same)
1131 return (FALSE);
1135 /* Suppress the duplicate operand at the top-level */
1137 if (TopLevel)
1139 Operand->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
1141 return (TRUE);
1144 #endif