xref: /freebsd/sys/contrib/dev/acpica/compiler/asltransform.c (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
1 /******************************************************************************
2  *
3  * Module Name: asltransform - Parse tree transforms
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2020, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
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.
20  *
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
27  *
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;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
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.
53  *
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.
65  *
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.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
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.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
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.
89  *
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.
98  *
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.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
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.
133  *
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.
145  *
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.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/compiler/aslcompiler.h>
153 #include "aslcompiler.y.h"
154 #include <contrib/dev/acpica/include/acnamesp.h>
155 
156 #define _COMPONENT          ACPI_COMPILER
157         ACPI_MODULE_NAME    ("asltransform")
158 
159 /* Local prototypes */
160 
161 static void
162 TrTransformSubtree (
163     ACPI_PARSE_OBJECT       *Op);
164 
165 static char *
166 TrAmlGetNextTempName (
167     ACPI_PARSE_OBJECT       *Op,
168     UINT8                   *TempCount);
169 
170 static void
171 TrAmlInitLineNumbers (
172     ACPI_PARSE_OBJECT       *Op,
173     ACPI_PARSE_OBJECT       *Neighbor);
174 
175 static void
176 TrAmlInitNode (
177     ACPI_PARSE_OBJECT       *Op,
178     UINT16                  ParseOpcode);
179 
180 static void
181 TrAmlSetSubtreeParent (
182     ACPI_PARSE_OBJECT       *Op,
183     ACPI_PARSE_OBJECT       *Parent);
184 
185 static void
186 TrAmlInsertPeer (
187     ACPI_PARSE_OBJECT       *Op,
188     ACPI_PARSE_OBJECT       *NewPeer);
189 
190 static void
191 TrDoDefinitionBlock (
192     ACPI_PARSE_OBJECT       *Op);
193 
194 static void
195 TrDoSwitch (
196     ACPI_PARSE_OBJECT       *StartNode);
197 
198 static void
199 TrCheckForDuplicateCase (
200     ACPI_PARSE_OBJECT       *CaseOp,
201     ACPI_PARSE_OBJECT       *Predicate1);
202 
203 static BOOLEAN
204 TrCheckForBufferMatch (
205     ACPI_PARSE_OBJECT       *Next1,
206     ACPI_PARSE_OBJECT       *Next2);
207 
208 
209 /*******************************************************************************
210  *
211  * FUNCTION:    TrAmlGetNextTempName
212  *
213  * PARAMETERS:  Op              - Current parse op
214  *              TempCount       - Current temporary counter. Was originally
215  *                                per-module; Currently per method, could be
216  *                                expanded to per-scope.
217  *
218  * RETURN:      A pointer to name (allocated here).
219  *
220  * DESCRIPTION: Generate an ACPI name of the form _T_x. These names are
221  *              reserved for use by the ASL compiler. (_T_0 through _T_Z)
222  *
223  ******************************************************************************/
224 
225 static char *
226 TrAmlGetNextTempName (
227     ACPI_PARSE_OBJECT       *Op,
228     UINT8                   *TempCount)
229 {
230     char                    *TempName;
231 
232 
233     if (*TempCount >= (10 + 26))  /* 0-35 valid: 0-9 and A-Z for TempName[3] */
234     {
235         /* Too many temps */
236 
237         AslError (ASL_ERROR, ASL_MSG_TOO_MANY_TEMPS, Op, NULL);
238         return (NULL);
239     }
240 
241     TempName = UtLocalCalloc (5);
242 
243     if (*TempCount < 10)    /* 0-9 */
244     {
245         TempName[3] = (char) (*TempCount + '0');
246     }
247     else                    /* 10-35: A-Z */
248     {
249         TempName[3] = (char) (*TempCount + ('A' - 10));
250     }
251 
252     (*TempCount)++;
253 
254     /* First three characters are always "_T_" */
255 
256     TempName[0] = '_';
257     TempName[1] = 'T';
258     TempName[2] = '_';
259 
260     return (TempName);
261 }
262 
263 
264 /*******************************************************************************
265  *
266  * FUNCTION:    TrAmlInitLineNumbers
267  *
268  * PARAMETERS:  Op              - Op to be initialized
269  *              Neighbor        - Op used for initialization values
270  *
271  * RETURN:      None
272  *
273  * DESCRIPTION: Initialized the various line numbers for a parse node.
274  *
275  ******************************************************************************/
276 
277 static void
278 TrAmlInitLineNumbers (
279     ACPI_PARSE_OBJECT       *Op,
280     ACPI_PARSE_OBJECT       *Neighbor)
281 {
282 
283     Op->Asl.EndLine           = Neighbor->Asl.EndLine;
284     Op->Asl.EndLogicalLine    = Neighbor->Asl.EndLogicalLine;
285     Op->Asl.LineNumber        = Neighbor->Asl.LineNumber;
286     Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
287     Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
288 }
289 
290 
291 /*******************************************************************************
292  *
293  * FUNCTION:    TrAmlInitNode
294  *
295  * PARAMETERS:  Op              - Op to be initialized
296  *              ParseOpcode     - Opcode for this node
297  *
298  * RETURN:      None
299  *
300  * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
301  *
302  ******************************************************************************/
303 
304 static void
305 TrAmlInitNode (
306     ACPI_PARSE_OBJECT       *Op,
307     UINT16                  ParseOpcode)
308 {
309 
310     Op->Asl.ParseOpcode = ParseOpcode;
311     UtSetParseOpName (Op);
312 }
313 
314 
315 /*******************************************************************************
316  *
317  * FUNCTION:    TrAmlSetSubtreeParent
318  *
319  * PARAMETERS:  Op              - First node in a list of peer nodes
320  *              Parent          - Parent of the subtree
321  *
322  * RETURN:      None
323  *
324  * DESCRIPTION: Set the parent for all peer nodes in a subtree
325  *
326  ******************************************************************************/
327 
328 static void
329 TrAmlSetSubtreeParent (
330     ACPI_PARSE_OBJECT       *Op,
331     ACPI_PARSE_OBJECT       *Parent)
332 {
333     ACPI_PARSE_OBJECT       *Next;
334 
335 
336     Next = Op;
337     while (Next)
338     {
339         Next->Asl.Parent = Parent;
340         Next = Next->Asl.Next;
341     }
342 }
343 
344 
345 /*******************************************************************************
346  *
347  * FUNCTION:    TrAmlInsertPeer
348  *
349  * PARAMETERS:  Op              - First node in a list of peer nodes
350  *              NewPeer         - Peer node to insert
351  *
352  * RETURN:      None
353  *
354  * DESCRIPTION: Insert a new peer node into a list of peers.
355  *
356  ******************************************************************************/
357 
358 static void
359 TrAmlInsertPeer (
360     ACPI_PARSE_OBJECT       *Op,
361     ACPI_PARSE_OBJECT       *NewPeer)
362 {
363 
364     NewPeer->Asl.Next = Op->Asl.Next;
365     Op->Asl.Next = NewPeer;
366 }
367 
368 
369 /*******************************************************************************
370  *
371  * FUNCTION:    TrAmlTransformWalkBegin
372  *
373  * PARAMETERS:  ASL_WALK_CALLBACK
374  *
375  * RETURN:      None
376  *
377  * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
378  *              operands.
379  *
380  ******************************************************************************/
381 
382 ACPI_STATUS
383 TrAmlTransformWalkBegin (
384     ACPI_PARSE_OBJECT       *Op,
385     UINT32                  Level,
386     void                    *Context)
387 {
388 
389     TrTransformSubtree (Op);
390     return (AE_OK);
391 }
392 
393 
394 /*******************************************************************************
395  *
396  * FUNCTION:    TrAmlTransformWalkEnd
397  *
398  * PARAMETERS:  ASL_WALK_CALLBACK
399  *
400  * RETURN:      None
401  *
402  * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
403  *              operands.
404  *
405  ******************************************************************************/
406 
407 ACPI_STATUS
408 TrAmlTransformWalkEnd (
409     ACPI_PARSE_OBJECT       *Op,
410     UINT32                  Level,
411     void                    *Context)
412 {
413 
414     /* Save possible Externals list in the DefintionBlock Op */
415 
416     if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)
417     {
418         Op->Asl.Value.Arg = AslGbl_ExternalsListHead;
419         AslGbl_ExternalsListHead = NULL;
420     }
421 
422     return (AE_OK);
423 }
424 
425 
426 /*******************************************************************************
427  *
428  * FUNCTION:    TrTransformSubtree
429  *
430  * PARAMETERS:  Op        - The parent parse node
431  *
432  * RETURN:      None
433  *
434  * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
435  *              complex AML opcodes require processing of the child nodes
436  *              (arguments/operands).
437  *
438  ******************************************************************************/
439 
440 static void
441 TrTransformSubtree (
442     ACPI_PARSE_OBJECT           *Op)
443 {
444     ACPI_PARSE_OBJECT           *MethodOp;
445     ACPI_NAMESTRING_INFO        Info;
446 
447 
448     if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
449     {
450         return;
451     }
452 
453     switch (Op->Asl.ParseOpcode)
454     {
455     case PARSEOP_DEFINITION_BLOCK:
456 
457         TrDoDefinitionBlock (Op);
458         break;
459 
460     case PARSEOP_SWITCH:
461 
462         TrDoSwitch (Op);
463         break;
464 
465     case PARSEOP_METHOD:
466         /*
467          * TBD: Zero the tempname (_T_x) count. Probably shouldn't be a global,
468          * however
469          */
470         AslGbl_TempCount = 0;
471         break;
472 
473     case PARSEOP_EXTERNAL:
474 
475         ExDoExternal (Op);
476         break;
477 
478     case PARSEOP___METHOD__:
479 
480         /* Transform to a string op containing the parent method name */
481 
482         Op->Asl.ParseOpcode = PARSEOP_STRING_LITERAL;
483         UtSetParseOpName (Op);
484 
485         /* Find the parent control method op */
486 
487         MethodOp = Op;
488         while (MethodOp)
489         {
490             if (MethodOp->Asl.ParseOpcode == PARSEOP_METHOD)
491             {
492                 /* First child contains the method name */
493 
494                 MethodOp = MethodOp->Asl.Child;
495                 Op->Asl.Value.String = MethodOp->Asl.Value.String;
496                 return;
497             }
498 
499             MethodOp = MethodOp->Asl.Parent;
500         }
501 
502         /* At the root, invocation not within a control method */
503 
504         Op->Asl.Value.String = "\\";
505         break;
506 
507     case PARSEOP_NAMESTRING:
508         /*
509          * A NameString can be up to 255 (0xFF) individual NameSegs maximum
510          * (with 254 dot separators) - as per the ACPI specification. Note:
511          * Cannot check for NumSegments == 0 because things like
512          * Scope(\) are legal and OK.
513          */
514         Info.ExternalName = Op->Asl.Value.String;
515         AcpiNsGetInternalNameLength (&Info);
516 
517         if (Info.NumSegments > 255)
518         {
519             AslError (ASL_ERROR, ASL_MSG_NAMESTRING_LENGTH, Op, NULL);
520         }
521         break;
522 
523     case PARSEOP_UNLOAD:
524 
525         AslError (ASL_WARNING, ASL_MSG_UNLOAD, Op, NULL);
526         break;
527 
528     case PARSEOP_SLEEP:
529 
530         /* Remark for very long sleep values */
531 
532         if (Op->Asl.Child->Asl.Value.Integer > 1000)
533         {
534             AslError (ASL_REMARK, ASL_MSG_LONG_SLEEP, Op, NULL);
535         }
536         break;
537 
538     case PARSEOP_PROCESSOR:
539 
540         AslError (ASL_WARNING, ASL_MSG_LEGACY_PROCESSOR_OP, Op, Op->Asl.ExternalName);
541         break;
542 
543     default:
544 
545         /* Nothing to do here for other opcodes */
546 
547         break;
548     }
549 }
550 
551 
552 /*******************************************************************************
553  *
554  * FUNCTION:    TrDoDefinitionBlock
555  *
556  * PARAMETERS:  Op        - Parse node
557  *
558  * RETURN:      None
559  *
560  * DESCRIPTION: Find the end of the definition block and set a global to this
561  *              node. It is used by the compiler to insert compiler-generated
562  *              names at the root level of the namespace.
563  *
564  ******************************************************************************/
565 
566 static void
567 TrDoDefinitionBlock (
568     ACPI_PARSE_OBJECT       *Op)
569 {
570     ACPI_PARSE_OBJECT       *Next;
571     UINT32                  i;
572 
573 
574     /* Reset external list when starting a definition block */
575 
576     AslGbl_ExternalsListHead = NULL;
577 
578     Next = Op->Asl.Child;
579     for (i = 0; i < 5; i++)
580     {
581         Next = Next->Asl.Next;
582         if (i == 0)
583         {
584             /*
585              * This is the table signature. Only the DSDT can be assumed
586              * to be at the root of the namespace;  Therefore, namepath
587              * optimization can only be performed on the DSDT.
588              */
589             if (!ACPI_COMPARE_NAMESEG (Next->Asl.Value.String, ACPI_SIG_DSDT))
590             {
591                 AslGbl_ReferenceOptimizationFlag = FALSE;
592             }
593         }
594     }
595 
596     AslGbl_FirstLevelInsertionNode = Next;
597 }
598 
599 
600 /*******************************************************************************
601  *
602  * FUNCTION:    TrDoSwitch
603  *
604  * PARAMETERS:  StartNode        - Parse node for SWITCH
605  *
606  * RETURN:      None
607  *
608  * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
609  *              no actual AML opcode for SWITCH -- it must be simulated.
610  *
611  ******************************************************************************/
612 
613 static void
614 TrDoSwitch (
615     ACPI_PARSE_OBJECT       *StartNode)
616 {
617     ACPI_PARSE_OBJECT       *Next;
618     ACPI_PARSE_OBJECT       *CaseOp = NULL;
619     ACPI_PARSE_OBJECT       *CaseBlock = NULL;
620     ACPI_PARSE_OBJECT       *DefaultOp = NULL;
621     ACPI_PARSE_OBJECT       *CurrentParentNode;
622     ACPI_PARSE_OBJECT       *Conditional = NULL;
623     ACPI_PARSE_OBJECT       *Predicate;
624     ACPI_PARSE_OBJECT       *Peer;
625     ACPI_PARSE_OBJECT       *NewOp;
626     ACPI_PARSE_OBJECT       *NewOp2;
627     ACPI_PARSE_OBJECT       *MethodOp;
628     ACPI_PARSE_OBJECT       *StoreOp;
629     ACPI_PARSE_OBJECT       *BreakOp;
630     ACPI_PARSE_OBJECT       *BufferOp;
631     char                    *PredicateValueName;
632     UINT16                  Index;
633     UINT32                  Btype;
634 
635 
636     /* Start node is the Switch() node */
637 
638     CurrentParentNode  = StartNode;
639 
640     /* Create a new temp name of the form _T_x */
641 
642     PredicateValueName = TrAmlGetNextTempName (StartNode, &AslGbl_TempCount);
643     if (!PredicateValueName)
644     {
645         return;
646     }
647 
648     /* First child is the Switch() predicate */
649 
650     Next = StartNode->Asl.Child;
651 
652     /*
653      * Examine the return type of the Switch Value -
654      * must be Integer/Buffer/String
655      */
656     Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
657     Btype = AslKeywordMapping[Index].AcpiBtype;
658     if ((Btype != ACPI_BTYPE_INTEGER) &&
659         (Btype != ACPI_BTYPE_STRING)  &&
660         (Btype != ACPI_BTYPE_BUFFER))
661     {
662         AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
663         Btype = ACPI_BTYPE_INTEGER;
664     }
665 
666     /* CASE statements start at next child */
667 
668     Peer = Next->Asl.Next;
669     while (Peer)
670     {
671         Next = Peer;
672         Peer = Next->Asl.Next;
673 
674         if (Next->Asl.ParseOpcode == PARSEOP_CASE)
675         {
676             TrCheckForDuplicateCase (Next, Next->Asl.Child);
677 
678             if (CaseOp)
679             {
680                 /* Add an ELSE to complete the previous CASE */
681 
682                 NewOp = TrCreateLeafOp (PARSEOP_ELSE);
683                 NewOp->Asl.Parent = Conditional->Asl.Parent;
684                 TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
685 
686                 /* Link ELSE node as a peer to the previous IF */
687 
688                 TrAmlInsertPeer (Conditional, NewOp);
689                 CurrentParentNode = NewOp;
690             }
691 
692             CaseOp = Next;
693             Conditional = CaseOp;
694             CaseBlock = CaseOp->Asl.Child->Asl.Next;
695             Conditional->Asl.Child->Asl.Next = NULL;
696             Predicate = CaseOp->Asl.Child;
697 
698             if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
699                 (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
700             {
701                 /*
702                  * Convert the package declaration to this form:
703                  *
704                  * If (LNotEqual (Match (Package(<size>){<data>},
705                  *                       MEQ, _T_x, MTR, Zero, Zero), Ones))
706                  */
707                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCHTYPE_MEQ);
708                 Predicate->Asl.Next = NewOp2;
709                 TrAmlInitLineNumbers (NewOp2, Conditional);
710 
711                 NewOp               = NewOp2;
712                 NewOp2              = TrCreateValuedLeafOp (PARSEOP_NAMESTRING,
713                                         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
714                 NewOp->Asl.Next     = NewOp2;
715                 TrAmlInitLineNumbers (NewOp2, Predicate);
716 
717                 NewOp               = NewOp2;
718                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCHTYPE_MTR);
719                 NewOp->Asl.Next     = NewOp2;
720                 TrAmlInitLineNumbers (NewOp2, Predicate);
721 
722                 NewOp               = NewOp2;
723                 NewOp2              = TrCreateLeafOp (PARSEOP_ZERO);
724                 NewOp->Asl.Next     = NewOp2;
725                 TrAmlInitLineNumbers (NewOp2, Predicate);
726 
727                 NewOp               = NewOp2;
728                 NewOp2              = TrCreateLeafOp (PARSEOP_ZERO);
729                 NewOp->Asl.Next     = NewOp2;
730                 TrAmlInitLineNumbers (NewOp2, Predicate);
731 
732                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCH);
733                 NewOp2->Asl.Child   = Predicate;  /* PARSEOP_PACKAGE */
734                 TrAmlInitLineNumbers (NewOp2, Conditional);
735                 TrAmlSetSubtreeParent (Predicate, NewOp2);
736 
737                 NewOp               = NewOp2;
738                 NewOp2              = TrCreateLeafOp (PARSEOP_ONES);
739                 NewOp->Asl.Next     = NewOp2;
740                 TrAmlInitLineNumbers (NewOp2, Conditional);
741 
742                 NewOp2              = TrCreateLeafOp (PARSEOP_LEQUAL);
743                 NewOp2->Asl.Child   = NewOp;
744                 NewOp->Asl.Parent   = NewOp2;
745                 TrAmlInitLineNumbers (NewOp2, Conditional);
746                 TrAmlSetSubtreeParent (NewOp, NewOp2);
747 
748                 NewOp               = NewOp2;
749                 NewOp2              = TrCreateLeafOp (PARSEOP_LNOT);
750                 NewOp2->Asl.Child   = NewOp;
751                 NewOp2->Asl.Parent  = Conditional;
752                 NewOp->Asl.Parent   = NewOp2;
753                 TrAmlInitLineNumbers (NewOp2, Conditional);
754 
755                 Conditional->Asl.Child = NewOp2;
756                 NewOp2->Asl.Next = CaseBlock;
757             }
758             else
759             {
760                 /*
761                  * Integer and Buffer case.
762                  *
763                  * Change CaseOp() to:  If (LEqual (SwitchValue, CaseValue)) {...}
764                  * Note: SwitchValue is first to allow the CaseValue to be implicitly
765                  * converted to the type of SwitchValue if necessary.
766                  *
767                  * CaseOp->Child is the case value
768                  * CaseOp->Child->Peer is the beginning of the case block
769                  */
770                 NewOp = TrCreateValuedLeafOp (PARSEOP_NAMESTRING,
771                     (UINT64) ACPI_TO_INTEGER (PredicateValueName));
772                 NewOp->Asl.Next = Predicate;
773                 TrAmlInitLineNumbers (NewOp, Predicate);
774 
775                 NewOp2              = TrCreateLeafOp (PARSEOP_LEQUAL);
776                 NewOp2->Asl.Parent  = Conditional;
777                 NewOp2->Asl.Child   = NewOp;
778                 TrAmlInitLineNumbers (NewOp2, Conditional);
779 
780                 TrAmlSetSubtreeParent (NewOp, NewOp2);
781 
782                 Predicate           = NewOp2;
783                 Predicate->Asl.Next = CaseBlock;
784 
785                 TrAmlSetSubtreeParent (Predicate, Conditional);
786                 Conditional->Asl.Child = Predicate;
787             }
788 
789             /* Reinitialize the CASE node to an IF node */
790 
791             TrAmlInitNode (Conditional, PARSEOP_IF);
792 
793             /*
794              * The first CASE(IF) is not nested under an ELSE.
795              * All other CASEs are children of a parent ELSE.
796              */
797             if (CurrentParentNode == StartNode)
798             {
799                 Conditional->Asl.Next = NULL;
800             }
801             else
802             {
803                 /*
804                  * The IF is a child of previous IF/ELSE. It
805                  * is therefore without peer.
806                  */
807                 CurrentParentNode->Asl.Child = Conditional;
808                 Conditional->Asl.Parent      = CurrentParentNode;
809                 Conditional->Asl.Next        = NULL;
810             }
811         }
812         else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
813         {
814             if (DefaultOp)
815             {
816                 /*
817                  * More than one Default
818                  * (Parser does not catch this, must check here)
819                  */
820                 AslError (ASL_ERROR, ASL_MSG_MULTIPLE_DEFAULT, Next, NULL);
821             }
822             else
823             {
824                 /* Save the DEFAULT node for later, after CASEs */
825 
826                 DefaultOp = Next;
827             }
828         }
829         else
830         {
831             /* Unknown peer opcode */
832 
833             AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%u)\n",
834                 Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
835         }
836     }
837 
838     /* Add the default case at the end of the if/else construct */
839 
840     if (DefaultOp)
841     {
842         /* If no CASE statements, this is an error - see below */
843 
844         if (CaseOp)
845         {
846             /* Convert the DEFAULT node to an ELSE */
847 
848             TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
849             DefaultOp->Asl.Parent = Conditional->Asl.Parent;
850 
851             /* Link ELSE node as a peer to the previous IF */
852 
853             TrAmlInsertPeer (Conditional, DefaultOp);
854         }
855     }
856 
857     if (!CaseOp)
858     {
859         AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
860     }
861 
862 
863     /*
864      * Create a Name(_T_x, ...) statement. This statement must appear at the
865      * method level, in case a loop surrounds the switch statement and could
866      * cause the name to be created twice (error).
867      */
868 
869     /* Create the Name node */
870 
871     Predicate = StartNode->Asl.Child;
872     NewOp = TrCreateLeafOp (PARSEOP_NAME);
873     TrAmlInitLineNumbers (NewOp, StartNode);
874 
875     /* Find the parent method */
876 
877     Next = StartNode;
878     while ((Next->Asl.ParseOpcode != PARSEOP_METHOD) &&
879            (Next->Asl.ParseOpcode != PARSEOP_DEFINITION_BLOCK))
880     {
881         Next = Next->Asl.Parent;
882     }
883     MethodOp = Next;
884 
885     NewOp->Asl.CompileFlags |= OP_COMPILER_EMITTED;
886     NewOp->Asl.Parent = Next;
887 
888     /* Insert name after the method name and arguments */
889 
890     Next = Next->Asl.Child; /* Name */
891     Next = Next->Asl.Next;  /* NumArgs */
892     Next = Next->Asl.Next;  /* SerializeRule */
893 
894     /*
895      * If method is not Serialized, we must make is so, because of the way
896      * that Switch() must be implemented -- we cannot allow multiple threads
897      * to execute this method concurrently since we need to create local
898      * temporary name(s).
899      */
900     if (Next->Asl.ParseOpcode != PARSEOP_SERIALIZERULE_SERIAL)
901     {
902         AslError (ASL_REMARK, ASL_MSG_SERIALIZED, MethodOp,
903             "Due to use of Switch operator");
904         Next->Asl.ParseOpcode = PARSEOP_SERIALIZERULE_SERIAL;
905     }
906 
907     Next = Next->Asl.Next;  /* SyncLevel */
908     Next = Next->Asl.Next;  /* ReturnType */
909     Next = Next->Asl.Next;  /* ParameterTypes */
910 
911     TrAmlInsertPeer (Next, NewOp);
912     TrAmlInitLineNumbers (NewOp, Next);
913 
914     /* Create the NameSeg child for the Name node */
915 
916     NewOp2 = TrCreateValuedLeafOp (PARSEOP_NAMESEG,
917         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
918     TrAmlInitLineNumbers (NewOp2, NewOp);
919     NewOp2->Asl.CompileFlags |= OP_IS_NAME_DECLARATION;
920     NewOp->Asl.Child  = NewOp2;
921 
922     /* Create the initial value for the Name. Btype was already validated above */
923 
924     switch (Btype)
925     {
926     case ACPI_BTYPE_INTEGER:
927 
928         NewOp2->Asl.Next = TrCreateValuedLeafOp (PARSEOP_ZERO,
929             (UINT64) 0);
930         TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
931         break;
932 
933     case ACPI_BTYPE_STRING:
934 
935         NewOp2->Asl.Next = TrCreateValuedLeafOp (PARSEOP_STRING_LITERAL,
936             (UINT64) ACPI_TO_INTEGER (""));
937         TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
938         break;
939 
940     case ACPI_BTYPE_BUFFER:
941 
942         (void) TrLinkPeerOp (NewOp2, TrCreateValuedLeafOp (PARSEOP_BUFFER,
943             (UINT64) 0));
944         Next = NewOp2->Asl.Next;
945         TrAmlInitLineNumbers (Next, NewOp2);
946 
947         (void) TrLinkOpChildren (Next, 1, TrCreateValuedLeafOp (PARSEOP_ZERO,
948             (UINT64) 1));
949         TrAmlInitLineNumbers (Next->Asl.Child, Next);
950 
951         BufferOp = TrCreateValuedLeafOp (PARSEOP_DEFAULT_ARG, (UINT64) 0);
952         TrAmlInitLineNumbers (BufferOp, Next->Asl.Child);
953         (void) TrLinkPeerOp (Next->Asl.Child, BufferOp);
954 
955         TrAmlSetSubtreeParent (Next->Asl.Child, Next);
956         break;
957 
958     default:
959 
960         break;
961     }
962 
963     TrAmlSetSubtreeParent (NewOp2, NewOp);
964 
965     /*
966      * Transform the Switch() into a While(One)-Break node.
967      * And create a Store() node which will be used to save the
968      * Switch() value. The store is of the form: Store (Value, _T_x)
969      * where _T_x is the temp variable.
970      */
971     TrAmlInitNode (StartNode, PARSEOP_WHILE);
972     NewOp = TrCreateLeafOp (PARSEOP_ONE);
973     TrAmlInitLineNumbers (NewOp, StartNode);
974     NewOp->Asl.Next = Predicate->Asl.Next;
975     NewOp->Asl.Parent = StartNode;
976     StartNode->Asl.Child = NewOp;
977 
978     /* Create a Store() node */
979 
980     StoreOp = TrCreateLeafOp (PARSEOP_STORE);
981     TrAmlInitLineNumbers (StoreOp, NewOp);
982     StoreOp->Asl.Parent = StartNode;
983     TrAmlInsertPeer (NewOp, StoreOp);
984 
985     /* Complete the Store subtree */
986 
987     StoreOp->Asl.Child = Predicate;
988     Predicate->Asl.Parent = StoreOp;
989 
990     NewOp = TrCreateValuedLeafOp (PARSEOP_NAMESEG,
991         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
992     TrAmlInitLineNumbers (NewOp, StoreOp);
993     NewOp->Asl.Parent    = StoreOp;
994     Predicate->Asl.Next  = NewOp;
995 
996     /* Create a Break() node and insert it into the end of While() */
997 
998     Conditional = StartNode->Asl.Child;
999     while (Conditional->Asl.Next)
1000     {
1001         Conditional = Conditional->Asl.Next;
1002     }
1003 
1004     BreakOp = TrCreateLeafOp (PARSEOP_BREAK);
1005     TrAmlInitLineNumbers (BreakOp, NewOp);
1006     BreakOp->Asl.Parent = StartNode;
1007     TrAmlInsertPeer (Conditional, BreakOp);
1008 }
1009 
1010 
1011 /*******************************************************************************
1012  *
1013  * FUNCTION:    TrCheckForDuplicateCase
1014  *
1015  * PARAMETERS:  CaseOp          - Parse node for first Case statement in list
1016  *              Predicate1      - Case value for the input CaseOp
1017  *
1018  * RETURN:      None
1019  *
1020  * DESCRIPTION: Check for duplicate case values. Currently, only handles
1021  *              Integers, Strings and Buffers. No support for Package objects.
1022  *
1023  ******************************************************************************/
1024 
1025 static void
1026 TrCheckForDuplicateCase (
1027     ACPI_PARSE_OBJECT       *CaseOp,
1028     ACPI_PARSE_OBJECT       *Predicate1)
1029 {
1030     ACPI_PARSE_OBJECT       *Next;
1031     ACPI_PARSE_OBJECT       *Predicate2;
1032 
1033 
1034     /* Walk the list of CASE opcodes */
1035 
1036     Next = CaseOp->Asl.Next;
1037     while (Next)
1038     {
1039         if (Next->Asl.ParseOpcode == PARSEOP_CASE)
1040         {
1041             /* Emit error only once */
1042 
1043             if (Next->Asl.CompileFlags & OP_IS_DUPLICATE)
1044             {
1045                 goto NextCase;
1046             }
1047 
1048             /* Check for a duplicate plain integer */
1049 
1050             Predicate2 = Next->Asl.Child;
1051             if ((Predicate1->Asl.ParseOpcode == PARSEOP_INTEGER) &&
1052                 (Predicate2->Asl.ParseOpcode == PARSEOP_INTEGER))
1053             {
1054                 if (Predicate1->Asl.Value.Integer == Predicate2->Asl.Value.Integer)
1055                 {
1056                     goto FoundDuplicate;
1057                 }
1058             }
1059 
1060             /* Check for pairs of the constants ZERO, ONE, ONES */
1061 
1062             else if (((Predicate1->Asl.ParseOpcode == PARSEOP_ZERO) &&
1063                 (Predicate2->Asl.ParseOpcode == PARSEOP_ZERO)) ||
1064                 ((Predicate1->Asl.ParseOpcode == PARSEOP_ONE) &&
1065                 (Predicate2->Asl.ParseOpcode == PARSEOP_ONE)) ||
1066                 ((Predicate1->Asl.ParseOpcode == PARSEOP_ONES) &&
1067                 (Predicate2->Asl.ParseOpcode == PARSEOP_ONES)))
1068             {
1069                 goto FoundDuplicate;
1070             }
1071 
1072             /* Check for a duplicate string constant (literal) */
1073 
1074             else if ((Predicate1->Asl.ParseOpcode == PARSEOP_STRING_LITERAL) &&
1075                 (Predicate2->Asl.ParseOpcode == PARSEOP_STRING_LITERAL))
1076             {
1077                 if (!strcmp (Predicate1->Asl.Value.String,
1078                         Predicate2->Asl.Value.String))
1079                 {
1080                     goto FoundDuplicate;
1081                 }
1082             }
1083 
1084             /* Check for a duplicate buffer constant */
1085 
1086             else if ((Predicate1->Asl.ParseOpcode == PARSEOP_BUFFER) &&
1087                 (Predicate2->Asl.ParseOpcode == PARSEOP_BUFFER))
1088             {
1089                 if (TrCheckForBufferMatch (Predicate1->Asl.Child,
1090                         Predicate2->Asl.Child))
1091                 {
1092                     goto FoundDuplicate;
1093                 }
1094             }
1095         }
1096         goto NextCase;
1097 
1098 FoundDuplicate:
1099         /* Emit error message only once */
1100 
1101         Next->Asl.CompileFlags |= OP_IS_DUPLICATE;
1102 
1103         AslDualParseOpError (ASL_ERROR, ASL_MSG_DUPLICATE_CASE, Next,
1104             Next->Asl.Value.String, ASL_MSG_CASE_FOUND_HERE, CaseOp,
1105             CaseOp->Asl.ExternalName);
1106 
1107 NextCase:
1108         Next = Next->Asl.Next;
1109     }
1110 }
1111 
1112 /*******************************************************************************
1113  *
1114  * FUNCTION:    TrBufferIsAllZero
1115  *
1116  * PARAMETERS:  Op          - Parse node for first opcode in buffer initializer
1117  *                            list
1118  *
1119  * RETURN:      TRUE if buffer contains all zeros or a DEFAULT_ARG
1120  *
1121  * DESCRIPTION: Check for duplicate Buffer case values.
1122  *
1123  ******************************************************************************/
1124 
1125 static BOOLEAN
1126 TrBufferIsAllZero (
1127     ACPI_PARSE_OBJECT       *Op)
1128 {
1129     while (Op)
1130     {
1131         if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
1132         {
1133             return (TRUE);
1134         }
1135         else if (Op->Asl.Value.Integer != 0)
1136         {
1137             return (FALSE);
1138         }
1139 
1140         Op = Op->Asl.Next;
1141     }
1142 
1143     return (TRUE);
1144 }
1145 
1146 
1147 /*******************************************************************************
1148  *
1149  * FUNCTION:    TrCheckForBufferMatch
1150  *
1151  * PARAMETERS:  Next1       - Parse node for first opcode in first buffer list
1152  *                              (The DEFAULT_ARG or INTEGER node)
1153  *              Next2       - Parse node for first opcode in second buffer list
1154  *                              (The DEFAULT_ARG or INTEGER node)
1155  *
1156  * RETURN:      TRUE if buffers match, FALSE otherwise
1157  *
1158  * DESCRIPTION: Check for duplicate Buffer case values.
1159  *
1160  ******************************************************************************/
1161 
1162 static BOOLEAN
1163 TrCheckForBufferMatch (
1164     ACPI_PARSE_OBJECT       *NextOp1,
1165     ACPI_PARSE_OBJECT       *NextOp2)
1166 {
1167     /*
1168      * The buffer length can be a DEFAULT_ARG or INTEGER. If any of the nodes
1169      * are DEFAULT_ARG, it means that the length has yet to be computed.
1170      * However, the initializer list can be compared to determine if these two
1171      * buffers match.
1172      */
1173     if ((NextOp1->Asl.ParseOpcode == PARSEOP_INTEGER &&
1174         NextOp2->Asl.ParseOpcode == PARSEOP_INTEGER) &&
1175         NextOp1->Asl.Value.Integer != NextOp2->Asl.Value.Integer)
1176     {
1177         return (FALSE);
1178     }
1179 
1180     /*
1181      * Buffers that have explicit lengths but no initializer lists are
1182      * filled with zeros at runtime. This is equivalent to buffers that have the
1183      * same length that are filled with zeros.
1184      *
1185      * In other words, the following buffers are equivalent:
1186      *
1187      * Buffer(0x4) {}
1188      * Buffer() {0x0, 0x0, 0x0, 0x0}
1189      *
1190      * This statement checks for matches where one buffer does not have an
1191      * initializer list and another buffer contains all zeros.
1192      */
1193     if (NextOp1->Asl.ParseOpcode != NextOp2->Asl.ParseOpcode &&
1194         TrBufferIsAllZero (NextOp1->Asl.Next) &&
1195         TrBufferIsAllZero (NextOp2->Asl.Next))
1196     {
1197         return (TRUE);
1198     }
1199 
1200     /* Start at the BYTECONST initializer node list */
1201 
1202     NextOp1 = NextOp1->Asl.Next;
1203     NextOp2 = NextOp2->Asl.Next;
1204 
1205     /*
1206      * Walk both lists until either a mismatch is found, or one or more
1207      * end-of-lists are found
1208      */
1209     while (NextOp1 && NextOp2)
1210     {
1211         if ((NextOp1->Asl.ParseOpcode == PARSEOP_STRING_LITERAL) &&
1212             (NextOp2->Asl.ParseOpcode == PARSEOP_STRING_LITERAL))
1213         {
1214             if (!strcmp (NextOp1->Asl.Value.String, NextOp2->Asl.Value.String))
1215             {
1216                 return (TRUE);
1217             }
1218             else
1219             {
1220                 return (FALSE);
1221             }
1222         }
1223         if ((UINT8) NextOp1->Asl.Value.Integer != (UINT8) NextOp2->Asl.Value.Integer)
1224         {
1225             return (FALSE);
1226         }
1227 
1228         NextOp1 = NextOp1->Asl.Next;
1229         NextOp2 = NextOp2->Asl.Next;
1230     }
1231 
1232     /* Not a match if one of the lists is not at end-of-list */
1233 
1234     if (NextOp1 || NextOp2)
1235     {
1236         return (FALSE);
1237     }
1238 
1239     /* Otherwise, the buffers match */
1240 
1241     return (TRUE);
1242 }
1243