Sync ACPICA with Intel's version 20181213.
[dragonfly.git] / sys / contrib / dev / acpica / source / common / dmswitch.c
blob46546fbb43b270764bb8470a61e45e40ea8c131d
1 /******************************************************************************
3 * Module Name: adwalk - Disassembler routines for switch statements
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, 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 "acdisasm.h"
157 #include "acdispat.h"
158 #include "acnamesp.h"
159 #include "acapps.h"
162 #define _COMPONENT ACPI_CA_DISASSEMBLER
163 ACPI_MODULE_NAME ("dmswitch")
165 static BOOLEAN
166 AcpiDmIsSwitchBlock (
167 ACPI_PARSE_OBJECT *Op,
168 char **Temp);
170 static BOOLEAN
171 AcpiDmIsCaseBlock (
172 ACPI_PARSE_OBJECT *Op);
175 /*******************************************************************************
177 * FUNCTION: AcpiDmProcessSwitch
179 * PARAMETERS: Op - Object to be examined
181 * RETURN: ACPI_STATUS
183 * DESCRIPTION: Walk function to create a list of all temporary (_T_) objects.
184 * If a While loop is found that can be converted to a Switch, do
185 * the conversion, remove the temporary name from the list, and
186 * mark the parse op with an IGNORE flag.
188 ******************************************************************************/
190 ACPI_STATUS
191 AcpiDmProcessSwitch (
192 ACPI_PARSE_OBJECT *Op)
194 char *Temp = NULL;
195 ACPI_PARSE_OBJECT_LIST *NewTemp;
196 ACPI_PARSE_OBJECT_LIST *Current;
197 ACPI_PARSE_OBJECT_LIST *Previous;
198 BOOLEAN FoundTemp = FALSE;
201 switch (Op->Common.AmlOpcode)
203 case AML_NAME_OP:
205 Temp = (char *) (&Op->Named.Name);
207 if (!strncmp(Temp, "_T_", 3))
209 /* Allocate and init a new Temp List node */
211 NewTemp = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_PARSE_OBJECT_LIST));
212 if (!NewTemp)
214 return (AE_NO_MEMORY);
217 if (AcpiGbl_TempListHead)
219 Current = AcpiGbl_TempListHead;
220 AcpiGbl_TempListHead = NewTemp;
221 AcpiGbl_TempListHead->Op = Op;
222 AcpiGbl_TempListHead->Next = Current;
224 else
226 AcpiGbl_TempListHead = NewTemp;
227 AcpiGbl_TempListHead->Op = Op;
228 AcpiGbl_TempListHead->Next = NULL;
231 break;
233 case AML_WHILE_OP:
235 if (!AcpiDmIsSwitchBlock (Op, &Temp))
237 break;
240 /* Found a Switch */
242 Op->Common.DisasmOpcode = ACPI_DASM_SWITCH;
244 Previous = Current = AcpiGbl_TempListHead;
245 while (Current)
247 /* Note, if we get here Temp is not NULL */
249 if (!strncmp(Temp, (char *) (&Current->Op->Named.Name), 4))
251 /* Match found. Ignore disassembly */
253 Current->Op->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
255 /* Remove from list */
257 if (Current == AcpiGbl_TempListHead)
259 AcpiGbl_TempListHead = Current->Next;
261 else
263 Previous->Next = Current->Next;
266 Current->Op = NULL;
267 Current->Next = NULL;
268 ACPI_FREE (Current);
269 FoundTemp = TRUE;
270 break;
273 Previous = Current;
274 Current = Current->Next;
277 if (!FoundTemp)
279 fprintf (stderr,
280 "Warning: Declaration for temp name %.4s not found\n", Temp);
282 break;
284 default:
285 break;
288 return (AE_OK);
292 /*******************************************************************************
294 * FUNCTION: AcpiDmClearTempList
296 * PARAMETERS: None
298 * RETURN: None
300 * DESCRIPTION: Removes any remaining temporary objects from global list and
301 * frees
303 ******************************************************************************/
305 void
306 AcpiDmClearTempList (
307 void)
309 ACPI_PARSE_OBJECT_LIST *Current;
312 while (AcpiGbl_TempListHead)
314 Current = AcpiGbl_TempListHead;
315 AcpiGbl_TempListHead = AcpiGbl_TempListHead->Next;
316 Current->Op = NULL;
317 Current->Next = NULL;
318 ACPI_FREE (Current);
323 /*******************************************************************************
325 * FUNCTION: AcpiDmIsSwitchBlock
327 * PARAMETERS: Op - While Object
328 * Temp - Where the compiler temp name is returned
329 * (_T_x)
331 * RETURN: TRUE if While block can be converted to a Switch/Case block
333 * DESCRIPTION: Determines if While block is a Switch/Case statement. Modifies
334 * parse tree to allow for Switch/Case disassembly during walk.
336 * EXAMPLE: Example of parse tree to be converted
338 * While
339 * One
340 * Store
341 * ByteConst
342 * -NamePath-
343 * If
344 * LEqual
345 * -NamePath-
346 * Zero
347 * Return
348 * One
349 * Else
350 * Return
351 * WordConst
352 * Break
354 ******************************************************************************/
356 BOOLEAN
357 AcpiDmIsSwitchBlock (
358 ACPI_PARSE_OBJECT *Op,
359 char **Temp)
361 ACPI_PARSE_OBJECT *OneOp;
362 ACPI_PARSE_OBJECT *StoreOp;
363 ACPI_PARSE_OBJECT *NamePathOp;
364 ACPI_PARSE_OBJECT *PredicateOp;
365 ACPI_PARSE_OBJECT *CurrentOp;
366 ACPI_PARSE_OBJECT *TempOp;
369 /* Check for One Op Predicate */
371 OneOp = AcpiPsGetArg (Op, 0);
372 if (!OneOp || (OneOp->Common.AmlOpcode != AML_ONE_OP))
374 return (FALSE);
377 /* Check for Store Op */
379 StoreOp = OneOp->Common.Next;
380 if (!StoreOp || (StoreOp->Common.AmlOpcode != AML_STORE_OP))
382 return (FALSE);
385 /* Check for Name Op with _T_ string */
387 NamePathOp = AcpiPsGetArg (StoreOp, 1);
388 if (!NamePathOp ||
389 (NamePathOp->Common.AmlOpcode != AML_INT_NAMEPATH_OP))
391 return (FALSE);
394 if (strncmp ((char *) (NamePathOp->Common.Value.Name), "_T_", 3))
396 return (FALSE);
399 *Temp = (char *) (NamePathOp->Common.Value.Name);
401 /* This is a Switch/Case control block */
403 /* Ignore the One Op Predicate */
405 OneOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
407 /* Ignore the Store Op, but not the children */
409 StoreOp->Common.DisasmOpcode = ACPI_DASM_IGNORE_SINGLE;
412 * First arg of Store Op is the Switch condition.
413 * Mark it as a Switch predicate and as a parameter list for paren
414 * closing and correct indentation.
416 PredicateOp = AcpiPsGetArg (StoreOp, 0);
417 PredicateOp->Common.DisasmOpcode = ACPI_DASM_SWITCH_PREDICATE;
418 PredicateOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
420 /* Ignore the Name Op */
422 NamePathOp->Common.DisasmFlags = ACPI_PARSEOP_IGNORE;
424 /* Remaining opcodes are the Case statements (If/ElseIf's) */
426 CurrentOp = StoreOp->Common.Next;
427 while (AcpiDmIsCaseBlock (CurrentOp))
429 /* Block is a Case structure */
431 if (CurrentOp->Common.AmlOpcode == AML_ELSE_OP)
433 /* ElseIf */
435 CurrentOp->Common.DisasmOpcode = ACPI_DASM_CASE;
436 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
439 /* If */
441 CurrentOp->Common.DisasmOpcode = ACPI_DASM_CASE;
444 * Mark the parse tree for Case disassembly. There are two
445 * types of Case statements. The first type of statement begins with
446 * an LEqual. The second starts with an LNot and uses a Match statement
447 * on a Package of constants.
449 TempOp = AcpiPsGetArg (CurrentOp, 0);
450 switch (TempOp->Common.AmlOpcode)
452 case (AML_LOGICAL_EQUAL_OP):
454 /* Ignore just the LEqual Op */
456 TempOp->Common.DisasmOpcode = ACPI_DASM_IGNORE_SINGLE;
458 /* Ignore the NamePath Op */
460 TempOp = AcpiPsGetArg (TempOp, 0);
461 TempOp->Common.DisasmFlags = ACPI_PARSEOP_IGNORE;
464 * Second arg of LEqual will be the Case predicate.
465 * Mark it as a predicate and also as a parameter list for paren
466 * closing and correct indentation.
468 PredicateOp = TempOp->Common.Next;
469 PredicateOp->Common.DisasmOpcode = ACPI_DASM_SWITCH_PREDICATE;
470 PredicateOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
471 break;
473 case (AML_LOGICAL_NOT_OP):
476 * The Package will be the predicate of the Case statement.
477 * It's under:
478 * LNOT
479 * LEQUAL
480 * MATCH
481 * PACKAGE
484 /* Get the LEqual Op from LNot */
486 TempOp = AcpiPsGetArg (TempOp, 0);
488 /* Get the Match Op from LEqual */
490 TempOp = AcpiPsGetArg (TempOp, 0);
492 /* Get the Package Op from Match */
494 PredicateOp = AcpiPsGetArg (TempOp, 0);
496 /* Mark as parameter list for paren closing */
498 PredicateOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
501 * The Package list would be too deeply indented if we
502 * chose to simply ignore the all the parent opcodes, so
503 * we rearrange the parse tree instead.
507 * Save the second arg of the If/Else Op which is the
508 * block code of code for this Case statement.
510 TempOp = AcpiPsGetArg (CurrentOp, 1);
513 * Move the Package Op to the child (predicate) of the
514 * Case statement.
516 CurrentOp->Common.Value.Arg = PredicateOp;
517 PredicateOp->Common.Parent = CurrentOp;
519 /* Add the block code */
521 PredicateOp->Common.Next = TempOp;
522 break;
524 default:
526 /* Should never get here */
527 break;
530 /* Advance to next Case block */
532 CurrentOp = CurrentOp->Common.Next;
535 /* If CurrentOp is now an Else, then this is a Default block */
537 if (CurrentOp && CurrentOp->Common.AmlOpcode == AML_ELSE_OP)
539 CurrentOp->Common.DisasmOpcode = ACPI_DASM_DEFAULT;
543 * From the first If advance to the Break op. It's possible to
544 * have an Else (Default) op here when there is only one Case
545 * statement, so check for it.
547 CurrentOp = StoreOp->Common.Next->Common.Next;
548 if (!CurrentOp)
550 return (FALSE);
552 if (CurrentOp->Common.AmlOpcode == AML_ELSE_OP)
554 CurrentOp = CurrentOp->Common.Next;
555 if (!CurrentOp)
557 return (FALSE);
561 /* Ignore the Break Op */
563 CurrentOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
564 return (TRUE);
568 /*******************************************************************************
570 * FUNCTION: AcpiDmIsCaseBlock
572 * PARAMETERS: Op - Object to test
574 * RETURN: TRUE if Object is beginning of a Case block.
576 * DESCRIPTION: Determines if an Object is the beginning of a Case block for a
577 * Switch/Case statement. Parse tree must be one of the following
578 * forms:
580 * Else (Optional)
581 * If
582 * LEqual
583 * -NamePath- _T_x
585 * Else (Optional)
586 * If
587 * LNot
588 * LEqual
589 * Match
590 * Package
591 * ByteConst
592 * -NamePath- _T_x
594 ******************************************************************************/
596 static BOOLEAN
597 AcpiDmIsCaseBlock (
598 ACPI_PARSE_OBJECT *Op)
600 ACPI_PARSE_OBJECT *CurrentOp;
603 if (!Op)
605 return (FALSE);
608 /* Look for an If or ElseIf */
610 CurrentOp = Op;
611 if (CurrentOp->Common.AmlOpcode == AML_ELSE_OP)
613 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
614 if (!CurrentOp)
616 return (FALSE);
620 if (!CurrentOp || CurrentOp->Common.AmlOpcode != AML_IF_OP)
622 return (FALSE);
625 /* Child must be LEqual or LNot */
627 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
628 if (!CurrentOp)
630 return (FALSE);
633 switch (CurrentOp->Common.AmlOpcode)
635 case (AML_LOGICAL_EQUAL_OP):
637 /* Next child must be NamePath with string _T_ */
639 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
640 if (!CurrentOp || !CurrentOp->Common.Value.Name ||
641 strncmp(CurrentOp->Common.Value.Name, "_T_", 3))
643 return (FALSE);
645 break;
647 case (AML_LOGICAL_NOT_OP):
649 /* Child of LNot must be LEqual op */
651 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
652 if (!CurrentOp || (CurrentOp->Common.AmlOpcode != AML_LOGICAL_EQUAL_OP))
654 return (FALSE);
657 /* Child of LNot must be Match op */
659 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
660 if (!CurrentOp || (CurrentOp->Common.AmlOpcode != AML_MATCH_OP))
662 return (FALSE);
665 /* First child of Match must be Package op */
667 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
668 if (!CurrentOp || (CurrentOp->Common.AmlOpcode != AML_PACKAGE_OP))
670 return (FALSE);
673 /* Third child of Match must be NamePath with string _T_ */
675 CurrentOp = AcpiPsGetArg (CurrentOp->Common.Parent, 2);
676 if (!CurrentOp || !CurrentOp->Common.Value.Name ||
677 strncmp(CurrentOp->Common.Value.Name, "_T_", 3))
679 return (FALSE);
681 break;
683 default:
685 return (FALSE);
688 return (TRUE);