xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/asltransform.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /******************************************************************************
2  *
3  * Module Name: asltransform - Parse tree transforms
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2019, 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 = AslGbl_ExternalsListHead;
300         AslGbl_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         AslGbl_TempCount = 0;
351         break;
352 
353     case PARSEOP_EXTERNAL:
354 
355         ExDoExternal (Op);
356         break;
357 
358     case PARSEOP___METHOD__:
359 
360         /* Transform to a string op containing the parent method name */
361 
362         Op->Asl.ParseOpcode = PARSEOP_STRING_LITERAL;
363         UtSetParseOpName (Op);
364 
365         /* Find the parent control method op */
366 
367         MethodOp = Op;
368         while (MethodOp)
369         {
370             if (MethodOp->Asl.ParseOpcode == PARSEOP_METHOD)
371             {
372                 /* First child contains the method name */
373 
374                 MethodOp = MethodOp->Asl.Child;
375                 Op->Asl.Value.String = MethodOp->Asl.Value.String;
376                 return;
377             }
378 
379             MethodOp = MethodOp->Asl.Parent;
380         }
381 
382         /* At the root, invocation not within a control method */
383 
384         Op->Asl.Value.String = "\\";
385         break;
386 
387     case PARSEOP_UNLOAD:
388 
389         AslError (ASL_WARNING, ASL_MSG_UNLOAD, Op, NULL);
390         break;
391 
392     case PARSEOP_SLEEP:
393 
394         /* Remark for very long sleep values */
395 
396         if (Op->Asl.Child->Asl.Value.Integer > 1000)
397         {
398             AslError (ASL_REMARK, ASL_MSG_LONG_SLEEP, Op, NULL);
399         }
400         break;
401 
402     default:
403 
404         /* Nothing to do here for other opcodes */
405 
406         break;
407     }
408 }
409 
410 
411 /*******************************************************************************
412  *
413  * FUNCTION:    TrDoDefinitionBlock
414  *
415  * PARAMETERS:  Op        - Parse node
416  *
417  * RETURN:      None
418  *
419  * DESCRIPTION: Find the end of the definition block and set a global to this
420  *              node. It is used by the compiler to insert compiler-generated
421  *              names at the root level of the namespace.
422  *
423  ******************************************************************************/
424 
425 static void
426 TrDoDefinitionBlock (
427     ACPI_PARSE_OBJECT       *Op)
428 {
429     ACPI_PARSE_OBJECT       *Next;
430     UINT32                  i;
431 
432 
433     /* Reset external list when starting a definition block */
434 
435     AslGbl_ExternalsListHead = NULL;
436 
437     Next = Op->Asl.Child;
438     for (i = 0; i < 5; i++)
439     {
440         Next = Next->Asl.Next;
441         if (i == 0)
442         {
443             /*
444              * This is the table signature. Only the DSDT can be assumed
445              * to be at the root of the namespace;  Therefore, namepath
446              * optimization can only be performed on the DSDT.
447              */
448             if (!ACPI_COMPARE_NAMESEG (Next->Asl.Value.String, ACPI_SIG_DSDT))
449             {
450                 AslGbl_ReferenceOptimizationFlag = FALSE;
451             }
452         }
453     }
454 
455     AslGbl_FirstLevelInsertionNode = Next;
456 }
457 
458 
459 /*******************************************************************************
460  *
461  * FUNCTION:    TrDoSwitch
462  *
463  * PARAMETERS:  StartNode        - Parse node for SWITCH
464  *
465  * RETURN:      None
466  *
467  * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
468  *              no actual AML opcode for SWITCH -- it must be simulated.
469  *
470  ******************************************************************************/
471 
472 static void
473 TrDoSwitch (
474     ACPI_PARSE_OBJECT       *StartNode)
475 {
476     ACPI_PARSE_OBJECT       *Next;
477     ACPI_PARSE_OBJECT       *CaseOp = NULL;
478     ACPI_PARSE_OBJECT       *CaseBlock = NULL;
479     ACPI_PARSE_OBJECT       *DefaultOp = NULL;
480     ACPI_PARSE_OBJECT       *CurrentParentNode;
481     ACPI_PARSE_OBJECT       *Conditional = NULL;
482     ACPI_PARSE_OBJECT       *Predicate;
483     ACPI_PARSE_OBJECT       *Peer;
484     ACPI_PARSE_OBJECT       *NewOp;
485     ACPI_PARSE_OBJECT       *NewOp2;
486     ACPI_PARSE_OBJECT       *MethodOp;
487     ACPI_PARSE_OBJECT       *StoreOp;
488     ACPI_PARSE_OBJECT       *BreakOp;
489     ACPI_PARSE_OBJECT       *BufferOp;
490     char                    *PredicateValueName;
491     UINT16                  Index;
492     UINT32                  Btype;
493 
494 
495     /* Start node is the Switch() node */
496 
497     CurrentParentNode  = StartNode;
498 
499     /* Create a new temp name of the form _T_x */
500 
501     PredicateValueName = TrAmlGetNextTempName (StartNode, &AslGbl_TempCount);
502     if (!PredicateValueName)
503     {
504         return;
505     }
506 
507     /* First child is the Switch() predicate */
508 
509     Next = StartNode->Asl.Child;
510 
511     /*
512      * Examine the return type of the Switch Value -
513      * must be Integer/Buffer/String
514      */
515     Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
516     Btype = AslKeywordMapping[Index].AcpiBtype;
517     if ((Btype != ACPI_BTYPE_INTEGER) &&
518         (Btype != ACPI_BTYPE_STRING)  &&
519         (Btype != ACPI_BTYPE_BUFFER))
520     {
521         AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
522         Btype = ACPI_BTYPE_INTEGER;
523     }
524 
525     /* CASE statements start at next child */
526 
527     Peer = Next->Asl.Next;
528     while (Peer)
529     {
530         Next = Peer;
531         Peer = Next->Asl.Next;
532 
533         if (Next->Asl.ParseOpcode == PARSEOP_CASE)
534         {
535             if (CaseOp)
536             {
537                 /* Add an ELSE to complete the previous CASE */
538 
539                 NewOp = TrCreateLeafOp (PARSEOP_ELSE);
540                 NewOp->Asl.Parent = Conditional->Asl.Parent;
541                 TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
542 
543                 /* Link ELSE node as a peer to the previous IF */
544 
545                 TrAmlInsertPeer (Conditional, NewOp);
546                 CurrentParentNode = NewOp;
547             }
548 
549             CaseOp = Next;
550             Conditional = CaseOp;
551             CaseBlock = CaseOp->Asl.Child->Asl.Next;
552             Conditional->Asl.Child->Asl.Next = NULL;
553             Predicate = CaseOp->Asl.Child;
554 
555             if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
556                 (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
557             {
558                 /*
559                  * Convert the package declaration to this form:
560                  *
561                  * If (LNotEqual (Match (Package(<size>){<data>},
562                  *                       MEQ, _T_x, MTR, Zero, Zero), Ones))
563                  */
564                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCHTYPE_MEQ);
565                 Predicate->Asl.Next = NewOp2;
566                 TrAmlInitLineNumbers (NewOp2, Conditional);
567 
568                 NewOp               = NewOp2;
569                 NewOp2              = TrCreateValuedLeafOp (PARSEOP_NAMESTRING,
570                                         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
571                 NewOp->Asl.Next     = NewOp2;
572                 TrAmlInitLineNumbers (NewOp2, Predicate);
573 
574                 NewOp               = NewOp2;
575                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCHTYPE_MTR);
576                 NewOp->Asl.Next     = NewOp2;
577                 TrAmlInitLineNumbers (NewOp2, Predicate);
578 
579                 NewOp               = NewOp2;
580                 NewOp2              = TrCreateLeafOp (PARSEOP_ZERO);
581                 NewOp->Asl.Next     = NewOp2;
582                 TrAmlInitLineNumbers (NewOp2, Predicate);
583 
584                 NewOp               = NewOp2;
585                 NewOp2              = TrCreateLeafOp (PARSEOP_ZERO);
586                 NewOp->Asl.Next     = NewOp2;
587                 TrAmlInitLineNumbers (NewOp2, Predicate);
588 
589                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCH);
590                 NewOp2->Asl.Child   = Predicate;  /* PARSEOP_PACKAGE */
591                 TrAmlInitLineNumbers (NewOp2, Conditional);
592                 TrAmlSetSubtreeParent (Predicate, NewOp2);
593 
594                 NewOp               = NewOp2;
595                 NewOp2              = TrCreateLeafOp (PARSEOP_ONES);
596                 NewOp->Asl.Next     = NewOp2;
597                 TrAmlInitLineNumbers (NewOp2, Conditional);
598 
599                 NewOp2              = TrCreateLeafOp (PARSEOP_LEQUAL);
600                 NewOp2->Asl.Child   = NewOp;
601                 NewOp->Asl.Parent   = NewOp2;
602                 TrAmlInitLineNumbers (NewOp2, Conditional);
603                 TrAmlSetSubtreeParent (NewOp, NewOp2);
604 
605                 NewOp               = NewOp2;
606                 NewOp2              = TrCreateLeafOp (PARSEOP_LNOT);
607                 NewOp2->Asl.Child   = NewOp;
608                 NewOp2->Asl.Parent  = Conditional;
609                 NewOp->Asl.Parent   = NewOp2;
610                 TrAmlInitLineNumbers (NewOp2, Conditional);
611 
612                 Conditional->Asl.Child = NewOp2;
613                 NewOp2->Asl.Next = CaseBlock;
614             }
615             else
616             {
617                 /*
618                  * Integer and Buffer case.
619                  *
620                  * Change CaseOp() to:  If (LEqual (SwitchValue, CaseValue)) {...}
621                  * Note: SwitchValue is first to allow the CaseValue to be implicitly
622                  * converted to the type of SwitchValue if necessary.
623                  *
624                  * CaseOp->Child is the case value
625                  * CaseOp->Child->Peer is the beginning of the case block
626                  */
627                 NewOp = TrCreateValuedLeafOp (PARSEOP_NAMESTRING,
628                     (UINT64) ACPI_TO_INTEGER (PredicateValueName));
629                 NewOp->Asl.Next = Predicate;
630                 TrAmlInitLineNumbers (NewOp, Predicate);
631 
632                 NewOp2              = TrCreateLeafOp (PARSEOP_LEQUAL);
633                 NewOp2->Asl.Parent  = Conditional;
634                 NewOp2->Asl.Child   = NewOp;
635                 TrAmlInitLineNumbers (NewOp2, Conditional);
636 
637                 TrAmlSetSubtreeParent (NewOp, NewOp2);
638 
639                 Predicate           = NewOp2;
640                 Predicate->Asl.Next = CaseBlock;
641 
642                 TrAmlSetSubtreeParent (Predicate, Conditional);
643                 Conditional->Asl.Child = Predicate;
644             }
645 
646             /* Reinitialize the CASE node to an IF node */
647 
648             TrAmlInitNode (Conditional, PARSEOP_IF);
649 
650             /*
651              * The first CASE(IF) is not nested under an ELSE.
652              * All other CASEs are children of a parent ELSE.
653              */
654             if (CurrentParentNode == StartNode)
655             {
656                 Conditional->Asl.Next = NULL;
657             }
658             else
659             {
660                 /*
661                  * The IF is a child of previous IF/ELSE. It
662                  * is therefore without peer.
663                  */
664                 CurrentParentNode->Asl.Child = Conditional;
665                 Conditional->Asl.Parent      = CurrentParentNode;
666                 Conditional->Asl.Next        = NULL;
667             }
668         }
669         else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
670         {
671             if (DefaultOp)
672             {
673                 /*
674                  * More than one Default
675                  * (Parser does not catch this, must check here)
676                  */
677                 AslError (ASL_ERROR, ASL_MSG_MULTIPLE_DEFAULT, Next, NULL);
678             }
679             else
680             {
681                 /* Save the DEFAULT node for later, after CASEs */
682 
683                 DefaultOp = Next;
684             }
685         }
686         else
687         {
688             /* Unknown peer opcode */
689 
690             AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%u)\n",
691                 Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
692         }
693     }
694 
695     /* Add the default case at the end of the if/else construct */
696 
697     if (DefaultOp)
698     {
699         /* If no CASE statements, this is an error - see below */
700 
701         if (CaseOp)
702         {
703             /* Convert the DEFAULT node to an ELSE */
704 
705             TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
706             DefaultOp->Asl.Parent = Conditional->Asl.Parent;
707 
708             /* Link ELSE node as a peer to the previous IF */
709 
710             TrAmlInsertPeer (Conditional, DefaultOp);
711         }
712     }
713 
714     if (!CaseOp)
715     {
716         AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
717     }
718 
719 
720     /*
721      * Create a Name(_T_x, ...) statement. This statement must appear at the
722      * method level, in case a loop surrounds the switch statement and could
723      * cause the name to be created twice (error).
724      */
725 
726     /* Create the Name node */
727 
728     Predicate = StartNode->Asl.Child;
729     NewOp = TrCreateLeafOp (PARSEOP_NAME);
730     TrAmlInitLineNumbers (NewOp, StartNode);
731 
732     /* Find the parent method */
733 
734     Next = StartNode;
735     while ((Next->Asl.ParseOpcode != PARSEOP_METHOD) &&
736            (Next->Asl.ParseOpcode != PARSEOP_DEFINITION_BLOCK))
737     {
738         Next = Next->Asl.Parent;
739     }
740     MethodOp = Next;
741 
742     NewOp->Asl.CompileFlags |= OP_COMPILER_EMITTED;
743     NewOp->Asl.Parent = Next;
744 
745     /* Insert name after the method name and arguments */
746 
747     Next = Next->Asl.Child; /* Name */
748     Next = Next->Asl.Next;  /* NumArgs */
749     Next = Next->Asl.Next;  /* SerializeRule */
750 
751     /*
752      * If method is not Serialized, we must make is so, because of the way
753      * that Switch() must be implemented -- we cannot allow multiple threads
754      * to execute this method concurrently since we need to create local
755      * temporary name(s).
756      */
757     if (Next->Asl.ParseOpcode != PARSEOP_SERIALIZERULE_SERIAL)
758     {
759         AslError (ASL_REMARK, ASL_MSG_SERIALIZED, MethodOp,
760             "Due to use of Switch operator");
761         Next->Asl.ParseOpcode = PARSEOP_SERIALIZERULE_SERIAL;
762     }
763 
764     Next = Next->Asl.Next;  /* SyncLevel */
765     Next = Next->Asl.Next;  /* ReturnType */
766     Next = Next->Asl.Next;  /* ParameterTypes */
767 
768     TrAmlInsertPeer (Next, NewOp);
769     TrAmlInitLineNumbers (NewOp, Next);
770 
771     /* Create the NameSeg child for the Name node */
772 
773     NewOp2 = TrCreateValuedLeafOp (PARSEOP_NAMESEG,
774         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
775     TrAmlInitLineNumbers (NewOp2, NewOp);
776     NewOp2->Asl.CompileFlags |= OP_IS_NAME_DECLARATION;
777     NewOp->Asl.Child  = NewOp2;
778 
779     /* Create the initial value for the Name. Btype was already validated above */
780 
781     switch (Btype)
782     {
783     case ACPI_BTYPE_INTEGER:
784 
785         NewOp2->Asl.Next = TrCreateValuedLeafOp (PARSEOP_ZERO,
786             (UINT64) 0);
787         TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
788         break;
789 
790     case ACPI_BTYPE_STRING:
791 
792         NewOp2->Asl.Next = TrCreateValuedLeafOp (PARSEOP_STRING_LITERAL,
793             (UINT64) ACPI_TO_INTEGER (""));
794         TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
795         break;
796 
797     case ACPI_BTYPE_BUFFER:
798 
799         (void) TrLinkPeerOp (NewOp2, TrCreateValuedLeafOp (PARSEOP_BUFFER,
800             (UINT64) 0));
801         Next = NewOp2->Asl.Next;
802         TrAmlInitLineNumbers (Next, NewOp2);
803 
804         (void) TrLinkOpChildren (Next, 1, TrCreateValuedLeafOp (PARSEOP_ZERO,
805             (UINT64) 1));
806         TrAmlInitLineNumbers (Next->Asl.Child, Next);
807 
808         BufferOp = TrCreateValuedLeafOp (PARSEOP_DEFAULT_ARG, (UINT64) 0);
809         TrAmlInitLineNumbers (BufferOp, Next->Asl.Child);
810         (void) TrLinkPeerOp (Next->Asl.Child, BufferOp);
811 
812         TrAmlSetSubtreeParent (Next->Asl.Child, Next);
813         break;
814 
815     default:
816 
817         break;
818     }
819 
820     TrAmlSetSubtreeParent (NewOp2, NewOp);
821 
822     /*
823      * Transform the Switch() into a While(One)-Break node.
824      * And create a Store() node which will be used to save the
825      * Switch() value. The store is of the form: Store (Value, _T_x)
826      * where _T_x is the temp variable.
827      */
828     TrAmlInitNode (StartNode, PARSEOP_WHILE);
829     NewOp = TrCreateLeafOp (PARSEOP_ONE);
830     TrAmlInitLineNumbers (NewOp, StartNode);
831     NewOp->Asl.Next = Predicate->Asl.Next;
832     NewOp->Asl.Parent = StartNode;
833     StartNode->Asl.Child = NewOp;
834 
835     /* Create a Store() node */
836 
837     StoreOp = TrCreateLeafOp (PARSEOP_STORE);
838     TrAmlInitLineNumbers (StoreOp, NewOp);
839     StoreOp->Asl.Parent = StartNode;
840     TrAmlInsertPeer (NewOp, StoreOp);
841 
842     /* Complete the Store subtree */
843 
844     StoreOp->Asl.Child = Predicate;
845     Predicate->Asl.Parent = StoreOp;
846 
847     NewOp = TrCreateValuedLeafOp (PARSEOP_NAMESEG,
848         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
849     TrAmlInitLineNumbers (NewOp, StoreOp);
850     NewOp->Asl.Parent    = StoreOp;
851     Predicate->Asl.Next  = NewOp;
852 
853     /* Create a Break() node and insert it into the end of While() */
854 
855     Conditional = StartNode->Asl.Child;
856     while (Conditional->Asl.Next)
857     {
858         Conditional = Conditional->Asl.Next;
859     }
860 
861     BreakOp = TrCreateLeafOp (PARSEOP_BREAK);
862     TrAmlInitLineNumbers (BreakOp, NewOp);
863     BreakOp->Asl.Parent = StartNode;
864     TrAmlInsertPeer (Conditional, BreakOp);
865 }
866