xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/asltransform.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /******************************************************************************
2  *
3  * Module Name: asltransform - Parse tree transforms
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2018, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include "aslcompiler.h"
45 #include "aslcompiler.y.h"
46 
47 #define _COMPONENT          ACPI_COMPILER
48         ACPI_MODULE_NAME    ("asltransform")
49 
50 /* Local prototypes */
51 
52 static void
53 TrTransformSubtree (
54     ACPI_PARSE_OBJECT       *Op);
55 
56 static char *
57 TrAmlGetNextTempName (
58     ACPI_PARSE_OBJECT       *Op,
59     UINT8                   *TempCount);
60 
61 static void
62 TrAmlInitLineNumbers (
63     ACPI_PARSE_OBJECT       *Op,
64     ACPI_PARSE_OBJECT       *Neighbor);
65 
66 static void
67 TrAmlInitNode (
68     ACPI_PARSE_OBJECT       *Op,
69     UINT16                  ParseOpcode);
70 
71 static void
72 TrAmlSetSubtreeParent (
73     ACPI_PARSE_OBJECT       *Op,
74     ACPI_PARSE_OBJECT       *Parent);
75 
76 static void
77 TrAmlInsertPeer (
78     ACPI_PARSE_OBJECT       *Op,
79     ACPI_PARSE_OBJECT       *NewPeer);
80 
81 static void
82 TrDoDefinitionBlock (
83     ACPI_PARSE_OBJECT       *Op);
84 
85 static void
86 TrDoSwitch (
87     ACPI_PARSE_OBJECT       *StartNode);
88 
89 
90 /*******************************************************************************
91  *
92  * FUNCTION:    TrAmlGetNextTempName
93  *
94  * PARAMETERS:  Op              - Current parse op
95  *              TempCount       - Current temporary counter. Was originally
96  *                                per-module; Currently per method, could be
97  *                                expanded to per-scope.
98  *
99  * RETURN:      A pointer to name (allocated here).
100  *
101  * DESCRIPTION: Generate an ACPI name of the form _T_x. These names are
102  *              reserved for use by the ASL compiler. (_T_0 through _T_Z)
103  *
104  ******************************************************************************/
105 
106 static char *
107 TrAmlGetNextTempName (
108     ACPI_PARSE_OBJECT       *Op,
109     UINT8                   *TempCount)
110 {
111     char                    *TempName;
112 
113 
114     if (*TempCount >= (10 + 26))  /* 0-35 valid: 0-9 and A-Z for TempName[3] */
115     {
116         /* Too many temps */
117 
118         AslError (ASL_ERROR, ASL_MSG_TOO_MANY_TEMPS, Op, NULL);
119         return (NULL);
120     }
121 
122     TempName = UtLocalCalloc (5);
123 
124     if (*TempCount < 10)    /* 0-9 */
125     {
126         TempName[3] = (char) (*TempCount + '0');
127     }
128     else                    /* 10-35: A-Z */
129     {
130         TempName[3] = (char) (*TempCount + ('A' - 10));
131     }
132 
133     (*TempCount)++;
134 
135     /* First three characters are always "_T_" */
136 
137     TempName[0] = '_';
138     TempName[1] = 'T';
139     TempName[2] = '_';
140 
141     return (TempName);
142 }
143 
144 
145 /*******************************************************************************
146  *
147  * FUNCTION:    TrAmlInitLineNumbers
148  *
149  * PARAMETERS:  Op              - Op to be initialized
150  *              Neighbor        - Op used for initialization values
151  *
152  * RETURN:      None
153  *
154  * DESCRIPTION: Initialized the various line numbers for a parse node.
155  *
156  ******************************************************************************/
157 
158 static void
159 TrAmlInitLineNumbers (
160     ACPI_PARSE_OBJECT       *Op,
161     ACPI_PARSE_OBJECT       *Neighbor)
162 {
163 
164     Op->Asl.EndLine           = Neighbor->Asl.EndLine;
165     Op->Asl.EndLogicalLine    = Neighbor->Asl.EndLogicalLine;
166     Op->Asl.LineNumber        = Neighbor->Asl.LineNumber;
167     Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
168     Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
169 }
170 
171 
172 /*******************************************************************************
173  *
174  * FUNCTION:    TrAmlInitNode
175  *
176  * PARAMETERS:  Op              - Op to be initialized
177  *              ParseOpcode     - Opcode for this node
178  *
179  * RETURN:      None
180  *
181  * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
182  *
183  ******************************************************************************/
184 
185 static void
186 TrAmlInitNode (
187     ACPI_PARSE_OBJECT       *Op,
188     UINT16                  ParseOpcode)
189 {
190 
191     Op->Asl.ParseOpcode = ParseOpcode;
192     UtSetParseOpName (Op);
193 }
194 
195 
196 /*******************************************************************************
197  *
198  * FUNCTION:    TrAmlSetSubtreeParent
199  *
200  * PARAMETERS:  Op              - First node in a list of peer nodes
201  *              Parent          - Parent of the subtree
202  *
203  * RETURN:      None
204  *
205  * DESCRIPTION: Set the parent for all peer nodes in a subtree
206  *
207  ******************************************************************************/
208 
209 static void
210 TrAmlSetSubtreeParent (
211     ACPI_PARSE_OBJECT       *Op,
212     ACPI_PARSE_OBJECT       *Parent)
213 {
214     ACPI_PARSE_OBJECT       *Next;
215 
216 
217     Next = Op;
218     while (Next)
219     {
220         Next->Asl.Parent = Parent;
221         Next = Next->Asl.Next;
222     }
223 }
224 
225 
226 /*******************************************************************************
227  *
228  * FUNCTION:    TrAmlInsertPeer
229  *
230  * PARAMETERS:  Op              - First node in a list of peer nodes
231  *              NewPeer         - Peer node to insert
232  *
233  * RETURN:      None
234  *
235  * DESCRIPTION: Insert a new peer node into a list of peers.
236  *
237  ******************************************************************************/
238 
239 static void
240 TrAmlInsertPeer (
241     ACPI_PARSE_OBJECT       *Op,
242     ACPI_PARSE_OBJECT       *NewPeer)
243 {
244 
245     NewPeer->Asl.Next = Op->Asl.Next;
246     Op->Asl.Next = NewPeer;
247 }
248 
249 
250 /*******************************************************************************
251  *
252  * FUNCTION:    TrAmlTransformWalkBegin
253  *
254  * PARAMETERS:  ASL_WALK_CALLBACK
255  *
256  * RETURN:      None
257  *
258  * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
259  *              operands.
260  *
261  ******************************************************************************/
262 
263 ACPI_STATUS
264 TrAmlTransformWalkBegin (
265     ACPI_PARSE_OBJECT       *Op,
266     UINT32                  Level,
267     void                    *Context)
268 {
269 
270     TrTransformSubtree (Op);
271     return (AE_OK);
272 }
273 
274 
275 /*******************************************************************************
276  *
277  * FUNCTION:    TrAmlTransformWalkEnd
278  *
279  * PARAMETERS:  ASL_WALK_CALLBACK
280  *
281  * RETURN:      None
282  *
283  * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
284  *              operands.
285  *
286  ******************************************************************************/
287 
288 ACPI_STATUS
289 TrAmlTransformWalkEnd (
290     ACPI_PARSE_OBJECT       *Op,
291     UINT32                  Level,
292     void                    *Context)
293 {
294 
295     /* Save possible Externals list in the DefintionBlock Op */
296 
297     if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)
298     {
299         Op->Asl.Value.Arg = Gbl_ExternalsListHead;
300         Gbl_ExternalsListHead = NULL;
301     }
302 
303     return (AE_OK);
304 }
305 
306 
307 /*******************************************************************************
308  *
309  * FUNCTION:    TrTransformSubtree
310  *
311  * PARAMETERS:  Op        - The parent parse node
312  *
313  * RETURN:      None
314  *
315  * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
316  *              complex AML opcodes require processing of the child nodes
317  *              (arguments/operands).
318  *
319  ******************************************************************************/
320 
321 static void
322 TrTransformSubtree (
323     ACPI_PARSE_OBJECT           *Op)
324 {
325     ACPI_PARSE_OBJECT           *MethodOp;
326 
327 
328     if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
329     {
330         return;
331     }
332 
333     switch (Op->Asl.ParseOpcode)
334     {
335     case PARSEOP_DEFINITION_BLOCK:
336 
337         TrDoDefinitionBlock (Op);
338         break;
339 
340     case PARSEOP_SWITCH:
341 
342         TrDoSwitch (Op);
343         break;
344 
345     case PARSEOP_METHOD:
346         /*
347          * TBD: Zero the tempname (_T_x) count. Probably shouldn't be a global,
348          * however
349          */
350         Gbl_TempCount = 0;
351         break;
352 
353     case PARSEOP_EXTERNAL:
354 
355         if (Gbl_DoExternals == TRUE)
356         {
357             ExDoExternal (Op);
358         }
359 
360         break;
361 
362     case PARSEOP___METHOD__:
363 
364         /* Transform to a string op containing the parent method name */
365 
366         Op->Asl.ParseOpcode = PARSEOP_STRING_LITERAL;
367         UtSetParseOpName (Op);
368 
369         /* Find the parent control method op */
370 
371         MethodOp = Op;
372         while (MethodOp)
373         {
374             if (MethodOp->Asl.ParseOpcode == PARSEOP_METHOD)
375             {
376                 /* First child contains the method name */
377 
378                 MethodOp = MethodOp->Asl.Child;
379                 Op->Asl.Value.String = MethodOp->Asl.Value.String;
380                 return;
381             }
382 
383             MethodOp = MethodOp->Asl.Parent;
384         }
385 
386         /* At the root, invocation not within a control method */
387 
388         Op->Asl.Value.String = "\\";
389         break;
390 
391     default:
392 
393         /* Nothing to do here for other opcodes */
394 
395         break;
396     }
397 }
398 
399 
400 /*******************************************************************************
401  *
402  * FUNCTION:    TrDoDefinitionBlock
403  *
404  * PARAMETERS:  Op        - Parse node
405  *
406  * RETURN:      None
407  *
408  * DESCRIPTION: Find the end of the definition block and set a global to this
409  *              node. It is used by the compiler to insert compiler-generated
410  *              names at the root level of the namespace.
411  *
412  ******************************************************************************/
413 
414 static void
415 TrDoDefinitionBlock (
416     ACPI_PARSE_OBJECT       *Op)
417 {
418     ACPI_PARSE_OBJECT       *Next;
419     UINT32                  i;
420 
421 
422     /* Reset external list when starting a definition block */
423 
424     Gbl_ExternalsListHead = NULL;
425 
426     Next = Op->Asl.Child;
427     for (i = 0; i < 5; i++)
428     {
429         Next = Next->Asl.Next;
430         if (i == 0)
431         {
432             /*
433              * This is the table signature. Only the DSDT can be assumed
434              * to be at the root of the namespace;  Therefore, namepath
435              * optimization can only be performed on the DSDT.
436              */
437             if (!ACPI_COMPARE_NAME (Next->Asl.Value.String, ACPI_SIG_DSDT))
438             {
439                 Gbl_ReferenceOptimizationFlag = FALSE;
440             }
441         }
442     }
443 
444     Gbl_FirstLevelInsertionNode = Next;
445 }
446 
447 
448 /*******************************************************************************
449  *
450  * FUNCTION:    TrDoSwitch
451  *
452  * PARAMETERS:  StartNode        - Parse node for SWITCH
453  *
454  * RETURN:      None
455  *
456  * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
457  *              no actual AML opcode for SWITCH -- it must be simulated.
458  *
459  ******************************************************************************/
460 
461 static void
462 TrDoSwitch (
463     ACPI_PARSE_OBJECT       *StartNode)
464 {
465     ACPI_PARSE_OBJECT       *Next;
466     ACPI_PARSE_OBJECT       *CaseOp = NULL;
467     ACPI_PARSE_OBJECT       *CaseBlock = NULL;
468     ACPI_PARSE_OBJECT       *DefaultOp = NULL;
469     ACPI_PARSE_OBJECT       *CurrentParentNode;
470     ACPI_PARSE_OBJECT       *Conditional = NULL;
471     ACPI_PARSE_OBJECT       *Predicate;
472     ACPI_PARSE_OBJECT       *Peer;
473     ACPI_PARSE_OBJECT       *NewOp;
474     ACPI_PARSE_OBJECT       *NewOp2;
475     ACPI_PARSE_OBJECT       *MethodOp;
476     ACPI_PARSE_OBJECT       *StoreOp;
477     ACPI_PARSE_OBJECT       *BreakOp;
478     ACPI_PARSE_OBJECT       *BufferOp;
479     char                    *PredicateValueName;
480     UINT16                  Index;
481     UINT32                  Btype;
482 
483 
484     /* Start node is the Switch() node */
485 
486     CurrentParentNode  = StartNode;
487 
488     /* Create a new temp name of the form _T_x */
489 
490     PredicateValueName = TrAmlGetNextTempName (StartNode, &Gbl_TempCount);
491     if (!PredicateValueName)
492     {
493         return;
494     }
495 
496     /* First child is the Switch() predicate */
497 
498     Next = StartNode->Asl.Child;
499 
500     /*
501      * Examine the return type of the Switch Value -
502      * must be Integer/Buffer/String
503      */
504     Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
505     Btype = AslKeywordMapping[Index].AcpiBtype;
506     if ((Btype != ACPI_BTYPE_INTEGER) &&
507         (Btype != ACPI_BTYPE_STRING)  &&
508         (Btype != ACPI_BTYPE_BUFFER))
509     {
510         AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
511         Btype = ACPI_BTYPE_INTEGER;
512     }
513 
514     /* CASE statements start at next child */
515 
516     Peer = Next->Asl.Next;
517     while (Peer)
518     {
519         Next = Peer;
520         Peer = Next->Asl.Next;
521 
522         if (Next->Asl.ParseOpcode == PARSEOP_CASE)
523         {
524             if (CaseOp)
525             {
526                 /* Add an ELSE to complete the previous CASE */
527 
528                 NewOp = TrCreateLeafOp (PARSEOP_ELSE);
529                 NewOp->Asl.Parent = Conditional->Asl.Parent;
530                 TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
531 
532                 /* Link ELSE node as a peer to the previous IF */
533 
534                 TrAmlInsertPeer (Conditional, NewOp);
535                 CurrentParentNode = NewOp;
536             }
537 
538             CaseOp = Next;
539             Conditional = CaseOp;
540             CaseBlock = CaseOp->Asl.Child->Asl.Next;
541             Conditional->Asl.Child->Asl.Next = NULL;
542             Predicate = CaseOp->Asl.Child;
543 
544             if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
545                 (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
546             {
547                 /*
548                  * Convert the package declaration to this form:
549                  *
550                  * If (LNotEqual (Match (Package(<size>){<data>},
551                  *                       MEQ, _T_x, MTR, Zero, Zero), Ones))
552                  */
553                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCHTYPE_MEQ);
554                 Predicate->Asl.Next = NewOp2;
555                 TrAmlInitLineNumbers (NewOp2, Conditional);
556 
557                 NewOp               = NewOp2;
558                 NewOp2              = TrCreateValuedLeafOp (PARSEOP_NAMESTRING,
559                                         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
560                 NewOp->Asl.Next     = NewOp2;
561                 TrAmlInitLineNumbers (NewOp2, Predicate);
562 
563                 NewOp               = NewOp2;
564                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCHTYPE_MTR);
565                 NewOp->Asl.Next     = NewOp2;
566                 TrAmlInitLineNumbers (NewOp2, Predicate);
567 
568                 NewOp               = NewOp2;
569                 NewOp2              = TrCreateLeafOp (PARSEOP_ZERO);
570                 NewOp->Asl.Next     = NewOp2;
571                 TrAmlInitLineNumbers (NewOp2, Predicate);
572 
573                 NewOp               = NewOp2;
574                 NewOp2              = TrCreateLeafOp (PARSEOP_ZERO);
575                 NewOp->Asl.Next     = NewOp2;
576                 TrAmlInitLineNumbers (NewOp2, Predicate);
577 
578                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCH);
579                 NewOp2->Asl.Child   = Predicate;  /* PARSEOP_PACKAGE */
580                 TrAmlInitLineNumbers (NewOp2, Conditional);
581                 TrAmlSetSubtreeParent (Predicate, NewOp2);
582 
583                 NewOp               = NewOp2;
584                 NewOp2              = TrCreateLeafOp (PARSEOP_ONES);
585                 NewOp->Asl.Next     = NewOp2;
586                 TrAmlInitLineNumbers (NewOp2, Conditional);
587 
588                 NewOp2              = TrCreateLeafOp (PARSEOP_LEQUAL);
589                 NewOp2->Asl.Child   = NewOp;
590                 NewOp->Asl.Parent   = NewOp2;
591                 TrAmlInitLineNumbers (NewOp2, Conditional);
592                 TrAmlSetSubtreeParent (NewOp, NewOp2);
593 
594                 NewOp               = NewOp2;
595                 NewOp2              = TrCreateLeafOp (PARSEOP_LNOT);
596                 NewOp2->Asl.Child   = NewOp;
597                 NewOp2->Asl.Parent  = Conditional;
598                 NewOp->Asl.Parent   = NewOp2;
599                 TrAmlInitLineNumbers (NewOp2, Conditional);
600 
601                 Conditional->Asl.Child = NewOp2;
602                 NewOp2->Asl.Next = CaseBlock;
603             }
604             else
605             {
606                 /*
607                  * Integer and Buffer case.
608                  *
609                  * Change CaseOp() to:  If (LEqual (SwitchValue, CaseValue)) {...}
610                  * Note: SwitchValue is first to allow the CaseValue to be implicitly
611                  * converted to the type of SwitchValue if necessary.
612                  *
613                  * CaseOp->Child is the case value
614                  * CaseOp->Child->Peer is the beginning of the case block
615                  */
616                 NewOp = TrCreateValuedLeafOp (PARSEOP_NAMESTRING,
617                     (UINT64) ACPI_TO_INTEGER (PredicateValueName));
618                 NewOp->Asl.Next = Predicate;
619                 TrAmlInitLineNumbers (NewOp, Predicate);
620 
621                 NewOp2              = TrCreateLeafOp (PARSEOP_LEQUAL);
622                 NewOp2->Asl.Parent  = Conditional;
623                 NewOp2->Asl.Child   = NewOp;
624                 TrAmlInitLineNumbers (NewOp2, Conditional);
625 
626                 TrAmlSetSubtreeParent (NewOp, NewOp2);
627 
628                 Predicate           = NewOp2;
629                 Predicate->Asl.Next = CaseBlock;
630 
631                 TrAmlSetSubtreeParent (Predicate, Conditional);
632                 Conditional->Asl.Child = Predicate;
633             }
634 
635             /* Reinitialize the CASE node to an IF node */
636 
637             TrAmlInitNode (Conditional, PARSEOP_IF);
638 
639             /*
640              * The first CASE(IF) is not nested under an ELSE.
641              * All other CASEs are children of a parent ELSE.
642              */
643             if (CurrentParentNode == StartNode)
644             {
645                 Conditional->Asl.Next = NULL;
646             }
647             else
648             {
649                 /*
650                  * The IF is a child of previous IF/ELSE. It
651                  * is therefore without peer.
652                  */
653                 CurrentParentNode->Asl.Child = Conditional;
654                 Conditional->Asl.Parent      = CurrentParentNode;
655                 Conditional->Asl.Next        = NULL;
656             }
657         }
658         else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
659         {
660             if (DefaultOp)
661             {
662                 /*
663                  * More than one Default
664                  * (Parser does not catch this, must check here)
665                  */
666                 AslError (ASL_ERROR, ASL_MSG_MULTIPLE_DEFAULT, Next, NULL);
667             }
668             else
669             {
670                 /* Save the DEFAULT node for later, after CASEs */
671 
672                 DefaultOp = Next;
673             }
674         }
675         else
676         {
677             /* Unknown peer opcode */
678 
679             AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%u)\n",
680                 Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
681         }
682     }
683 
684     /* Add the default case at the end of the if/else construct */
685 
686     if (DefaultOp)
687     {
688         /* If no CASE statements, this is an error - see below */
689 
690         if (CaseOp)
691         {
692             /* Convert the DEFAULT node to an ELSE */
693 
694             TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
695             DefaultOp->Asl.Parent = Conditional->Asl.Parent;
696 
697             /* Link ELSE node as a peer to the previous IF */
698 
699             TrAmlInsertPeer (Conditional, DefaultOp);
700         }
701     }
702 
703     if (!CaseOp)
704     {
705         AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
706     }
707 
708 
709     /*
710      * Create a Name(_T_x, ...) statement. This statement must appear at the
711      * method level, in case a loop surrounds the switch statement and could
712      * cause the name to be created twice (error).
713      */
714 
715     /* Create the Name node */
716 
717     Predicate = StartNode->Asl.Child;
718     NewOp = TrCreateLeafOp (PARSEOP_NAME);
719     TrAmlInitLineNumbers (NewOp, StartNode);
720 
721     /* Find the parent method */
722 
723     Next = StartNode;
724     while ((Next->Asl.ParseOpcode != PARSEOP_METHOD) &&
725            (Next->Asl.ParseOpcode != PARSEOP_DEFINITION_BLOCK))
726     {
727         Next = Next->Asl.Parent;
728     }
729     MethodOp = Next;
730 
731     NewOp->Asl.CompileFlags |= OP_COMPILER_EMITTED;
732     NewOp->Asl.Parent = Next;
733 
734     /* Insert name after the method name and arguments */
735 
736     Next = Next->Asl.Child; /* Name */
737     Next = Next->Asl.Next;  /* NumArgs */
738     Next = Next->Asl.Next;  /* SerializeRule */
739 
740     /*
741      * If method is not Serialized, we must make is so, because of the way
742      * that Switch() must be implemented -- we cannot allow multiple threads
743      * to execute this method concurrently since we need to create local
744      * temporary name(s).
745      */
746     if (Next->Asl.ParseOpcode != PARSEOP_SERIALIZERULE_SERIAL)
747     {
748         AslError (ASL_REMARK, ASL_MSG_SERIALIZED, MethodOp,
749             "Due to use of Switch operator");
750         Next->Asl.ParseOpcode = PARSEOP_SERIALIZERULE_SERIAL;
751     }
752 
753     Next = Next->Asl.Next;  /* SyncLevel */
754     Next = Next->Asl.Next;  /* ReturnType */
755     Next = Next->Asl.Next;  /* ParameterTypes */
756 
757     TrAmlInsertPeer (Next, NewOp);
758     TrAmlInitLineNumbers (NewOp, Next);
759 
760     /* Create the NameSeg child for the Name node */
761 
762     NewOp2 = TrCreateValuedLeafOp (PARSEOP_NAMESEG,
763         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
764     TrAmlInitLineNumbers (NewOp2, NewOp);
765     NewOp2->Asl.CompileFlags |= OP_IS_NAME_DECLARATION;
766     NewOp->Asl.Child  = NewOp2;
767 
768     /* Create the initial value for the Name. Btype was already validated above */
769 
770     switch (Btype)
771     {
772     case ACPI_BTYPE_INTEGER:
773 
774         NewOp2->Asl.Next = TrCreateValuedLeafOp (PARSEOP_ZERO,
775             (UINT64) 0);
776         TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
777         break;
778 
779     case ACPI_BTYPE_STRING:
780 
781         NewOp2->Asl.Next = TrCreateValuedLeafOp (PARSEOP_STRING_LITERAL,
782             (UINT64) ACPI_TO_INTEGER (""));
783         TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
784         break;
785 
786     case ACPI_BTYPE_BUFFER:
787 
788         (void) TrLinkPeerOp (NewOp2, TrCreateValuedLeafOp (PARSEOP_BUFFER,
789             (UINT64) 0));
790         Next = NewOp2->Asl.Next;
791         TrAmlInitLineNumbers (Next, NewOp2);
792 
793         (void) TrLinkOpChildren (Next, 1, TrCreateValuedLeafOp (PARSEOP_ZERO,
794             (UINT64) 1));
795         TrAmlInitLineNumbers (Next->Asl.Child, Next);
796 
797         BufferOp = TrCreateValuedLeafOp (PARSEOP_DEFAULT_ARG, (UINT64) 0);
798         TrAmlInitLineNumbers (BufferOp, Next->Asl.Child);
799         (void) TrLinkPeerOp (Next->Asl.Child, BufferOp);
800 
801         TrAmlSetSubtreeParent (Next->Asl.Child, Next);
802         break;
803 
804     default:
805 
806         break;
807     }
808 
809     TrAmlSetSubtreeParent (NewOp2, NewOp);
810 
811     /*
812      * Transform the Switch() into a While(One)-Break node.
813      * And create a Store() node which will be used to save the
814      * Switch() value. The store is of the form: Store (Value, _T_x)
815      * where _T_x is the temp variable.
816      */
817     TrAmlInitNode (StartNode, PARSEOP_WHILE);
818     NewOp = TrCreateLeafOp (PARSEOP_ONE);
819     TrAmlInitLineNumbers (NewOp, StartNode);
820     NewOp->Asl.Next = Predicate->Asl.Next;
821     NewOp->Asl.Parent = StartNode;
822     StartNode->Asl.Child = NewOp;
823 
824     /* Create a Store() node */
825 
826     StoreOp = TrCreateLeafOp (PARSEOP_STORE);
827     TrAmlInitLineNumbers (StoreOp, NewOp);
828     StoreOp->Asl.Parent = StartNode;
829     TrAmlInsertPeer (NewOp, StoreOp);
830 
831     /* Complete the Store subtree */
832 
833     StoreOp->Asl.Child = Predicate;
834     Predicate->Asl.Parent = StoreOp;
835 
836     NewOp = TrCreateValuedLeafOp (PARSEOP_NAMESEG,
837         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
838     TrAmlInitLineNumbers (NewOp, StoreOp);
839     NewOp->Asl.Parent    = StoreOp;
840     Predicate->Asl.Next  = NewOp;
841 
842     /* Create a Break() node and insert it into the end of While() */
843 
844     Conditional = StartNode->Asl.Child;
845     while (Conditional->Asl.Next)
846     {
847         Conditional = Conditional->Asl.Next;
848     }
849 
850     BreakOp = TrCreateLeafOp (PARSEOP_BREAK);
851     TrAmlInitLineNumbers (BreakOp, NewOp);
852     BreakOp->Asl.Parent = StartNode;
853     TrAmlInsertPeer (Conditional, BreakOp);
854 }
855