asltransform.c revision 250838
1285SN/A/******************************************************************************
2726Saefimov *
3285SN/A * Module Name: asltransform - Parse tree transforms
4285SN/A *
5285SN/A *****************************************************************************/
6285SN/A
7285SN/A/*
8285SN/A * Copyright (C) 2000 - 2013, Intel Corp.
9285SN/A * All rights reserved.
10285SN/A *
11285SN/A * Redistribution and use in source and binary forms, with or without
12285SN/A * modification, are permitted provided that the following conditions
13285SN/A * are met:
14285SN/A * 1. Redistributions of source code must retain the above copyright
15285SN/A *    notice, this list of conditions, and the following disclaimer,
16285SN/A *    without modification.
17285SN/A * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18285SN/A *    substantially similar to the "NO WARRANTY" disclaimer below
19285SN/A *    ("Disclaimer") and any redistribution must be conditioned upon
20285SN/A *    including a substantially similar Disclaimer requirement for further
21285SN/A *    binary redistribution.
22285SN/A * 3. Neither the names of the above-listed copyright holders nor the names
23285SN/A *    of any contributors may be used to endorse or promote products derived
24285SN/A *    from this software without specific prior written permission.
25285SN/A *
26285SN/A * Alternatively, this software may be distributed under the terms of the
27285SN/A * GNU General Public License ("GPL") version 2 as published by the Free
28285SN/A * Software Foundation.
29285SN/A *
30285SN/A * NO WARRANTY
31285SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32285SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33285SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34285SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35285SN/A * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36285SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37285SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38726Saefimov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39285SN/A * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40285SN/A * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41285SN/A * POSSIBILITY OF SUCH DAMAGES.
42285SN/A */
43285SN/A
44285SN/A
45726Saefimov#include <contrib/dev/acpica/compiler/aslcompiler.h>
46726Saefimov#include "aslcompiler.y.h"
47726Saefimov
48285SN/A#define _COMPONENT          ACPI_COMPILER
49285SN/A        ACPI_MODULE_NAME    ("asltransform")
50285SN/A
51285SN/A/* Local prototypes */
52285SN/A
53285SN/Astatic void
54285SN/ATrTransformSubtree (
55285SN/A    ACPI_PARSE_OBJECT       *Op);
56285SN/A
57285SN/Astatic char *
58285SN/ATrAmlGetNextTempName (
59285SN/A    ACPI_PARSE_OBJECT       *Op,
60285SN/A    UINT8                   *TempCount);
61285SN/A
62285SN/Astatic void
63285SN/ATrAmlInitLineNumbers (
64285SN/A    ACPI_PARSE_OBJECT       *Op,
65285SN/A    ACPI_PARSE_OBJECT       *Neighbor);
66285SN/A
67285SN/Astatic void
68285SN/ATrAmlInitNode (
69285SN/A    ACPI_PARSE_OBJECT       *Op,
70367SN/A    UINT16                  ParseOpcode);
71285SN/A
72367SN/Astatic void
73285SN/ATrAmlSetSubtreeParent (
74285SN/A    ACPI_PARSE_OBJECT       *Op,
75285SN/A    ACPI_PARSE_OBJECT       *Parent);
76285SN/A
77285SN/Astatic void
78285SN/ATrAmlInsertPeer (
79285SN/A    ACPI_PARSE_OBJECT       *Op,
80285SN/A    ACPI_PARSE_OBJECT       *NewPeer);
81285SN/A
82285SN/Astatic void
83285SN/ATrDoDefinitionBlock (
84285SN/A    ACPI_PARSE_OBJECT       *Op);
85285SN/A
86285SN/Astatic void
87285SN/ATrDoSwitch (
88285SN/A    ACPI_PARSE_OBJECT       *StartNode);
89285SN/A
90285SN/A
91285SN/A/*******************************************************************************
92285SN/A *
93285SN/A * FUNCTION:    TrAmlGetNextTempName
94285SN/A *
95285SN/A * PARAMETERS:  Op              - Current parse op
96285SN/A *              TempCount       - Current temporary counter. Was originally
97285SN/A *                                per-module; Currently per method, could be
98285SN/A *                                expanded to per-scope.
99285SN/A *
100285SN/A * RETURN:      A pointer to name (allocated here).
101285SN/A *
102285SN/A * DESCRIPTION: Generate an ACPI name of the form _T_x. These names are
103285SN/A *              reserved for use by the ASL compiler. (_T_0 through _T_Z)
104285SN/A *
105285SN/A ******************************************************************************/
106285SN/A
107285SN/Astatic char *
108285SN/ATrAmlGetNextTempName (
109285SN/A    ACPI_PARSE_OBJECT       *Op,
110285SN/A    UINT8                   *TempCount)
111285SN/A{
112285SN/A    char                    *TempName;
113367SN/A
114285SN/A
115285SN/A    if (*TempCount >= (10+26))  /* 0-35 valid: 0-9 and A-Z for TempName[3] */
116285SN/A    {
117285SN/A        /* Too many temps */
118285SN/A
119285SN/A        AslError (ASL_ERROR, ASL_MSG_TOO_MANY_TEMPS, Op, NULL);
120285SN/A        return (NULL);
121285SN/A    }
122285SN/A
123285SN/A    TempName = UtLocalCalloc (5);
124285SN/A
125285SN/A    if (*TempCount < 10)    /* 0-9 */
126285SN/A    {
127285SN/A        TempName[3] = (char) (*TempCount + '0');
128285SN/A    }
129285SN/A    else                    /* 10-35: A-Z */
130285SN/A    {
131285SN/A        TempName[3] = (char) (*TempCount + ('A' - 10));
132285SN/A    }
133285SN/A    (*TempCount)++;
134285SN/A
135285SN/A    /* First three characters are always "_T_" */
136285SN/A
137285SN/A    TempName[0] = '_';
138285SN/A    TempName[1] = 'T';
139285SN/A    TempName[2] = '_';
140285SN/A
141285SN/A    return (TempName);
142285SN/A}
143285SN/A
144285SN/A
145285SN/A/*******************************************************************************
146285SN/A *
147285SN/A * FUNCTION:    TrAmlInitLineNumbers
148285SN/A *
149285SN/A * PARAMETERS:  Op              - Op to be initialized
150285SN/A *              Neighbor        - Op used for initialization values
151285SN/A *
152285SN/A * RETURN:      None
153285SN/A *
154285SN/A * DESCRIPTION: Initialized the various line numbers for a parse node.
155285SN/A *
156285SN/A ******************************************************************************/
157285SN/A
158285SN/Astatic void
159285SN/ATrAmlInitLineNumbers (
160285SN/A    ACPI_PARSE_OBJECT       *Op,
161285SN/A    ACPI_PARSE_OBJECT       *Neighbor)
162285SN/A{
163285SN/A
164285SN/A    Op->Asl.EndLine           = Neighbor->Asl.EndLine;
165285SN/A    Op->Asl.EndLogicalLine    = Neighbor->Asl.EndLogicalLine;
166285SN/A    Op->Asl.LineNumber        = Neighbor->Asl.LineNumber;
167726Saefimov    Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
168726Saefimov    Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
169726Saefimov}
170726Saefimov
171726Saefimov
172726Saefimov/*******************************************************************************
173726Saefimov *
174726Saefimov * FUNCTION:    TrAmlInitNode
175726Saefimov *
176726Saefimov * PARAMETERS:  Op              - Op to be initialized
177726Saefimov *              ParseOpcode     - Opcode for this node
178726Saefimov *
179726Saefimov * RETURN:      None
180726Saefimov *
181726Saefimov * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
182726Saefimov *
183726Saefimov ******************************************************************************/
184726Saefimov
185726Saefimovstatic void
186726SaefimovTrAmlInitNode (
187726Saefimov    ACPI_PARSE_OBJECT       *Op,
188726Saefimov    UINT16                  ParseOpcode)
189726Saefimov{
190726Saefimov
191726Saefimov    Op->Asl.ParseOpcode = ParseOpcode;
192285SN/A    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
209static void
210TrAmlSetSubtreeParent (
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
239static void
240TrAmlInsertPeer (
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:    TrAmlTransformWalk
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
263ACPI_STATUS
264TrAmlTransformWalk (
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:    TrTransformSubtree
278 *
279 * PARAMETERS:  Op        - The parent parse node
280 *
281 * RETURN:      None
282 *
283 * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
284 *              complex AML opcodes require processing of the child nodes
285 *              (arguments/operands).
286 *
287 ******************************************************************************/
288
289static void
290TrTransformSubtree (
291    ACPI_PARSE_OBJECT           *Op)
292{
293
294    if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
295    {
296        return;
297    }
298
299    switch (Op->Asl.ParseOpcode)
300    {
301    case PARSEOP_DEFINITIONBLOCK:
302
303        TrDoDefinitionBlock (Op);
304        break;
305
306    case PARSEOP_SWITCH:
307
308        TrDoSwitch (Op);
309        break;
310
311    case PARSEOP_METHOD:
312        /*
313         * TBD: Zero the tempname (_T_x) count. Probably shouldn't be a global,
314         * however
315         */
316        Gbl_TempCount = 0;
317        break;
318
319    default:
320
321        /* Nothing to do here for other opcodes */
322
323        break;
324    }
325}
326
327
328/*******************************************************************************
329 *
330 * FUNCTION:    TrDoDefinitionBlock
331 *
332 * PARAMETERS:  Op        - Parse node
333 *
334 * RETURN:      None
335 *
336 * DESCRIPTION: Find the end of the definition block and set a global to this
337 *              node. It is used by the compiler to insert compiler-generated
338 *              names at the root level of the namespace.
339 *
340 ******************************************************************************/
341
342static void
343TrDoDefinitionBlock (
344    ACPI_PARSE_OBJECT       *Op)
345{
346    ACPI_PARSE_OBJECT       *Next;
347    UINT32                  i;
348
349
350    Next = Op->Asl.Child;
351    for (i = 0; i < 5; i++)
352    {
353        Next = Next->Asl.Next;
354        if (i == 0)
355        {
356            /*
357             * This is the table signature. Only the DSDT can be assumed
358             * to be at the root of the namespace;  Therefore, namepath
359             * optimization can only be performed on the DSDT.
360             */
361            if (!ACPI_COMPARE_NAME (Next->Asl.Value.String, ACPI_SIG_DSDT))
362            {
363                Gbl_ReferenceOptimizationFlag = FALSE;
364            }
365        }
366    }
367
368    Gbl_FirstLevelInsertionNode = Next;
369}
370
371
372/*******************************************************************************
373 *
374 * FUNCTION:    TrDoSwitch
375 *
376 * PARAMETERS:  StartNode        - Parse node for SWITCH
377 *
378 * RETURN:      None
379 *
380 *
381 * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
382 *              no actual AML opcode for SWITCH -- it must be simulated.
383 *
384 ******************************************************************************/
385
386static void
387TrDoSwitch (
388    ACPI_PARSE_OBJECT       *StartNode)
389{
390    ACPI_PARSE_OBJECT       *Next;
391    ACPI_PARSE_OBJECT       *CaseOp = NULL;
392    ACPI_PARSE_OBJECT       *CaseBlock = NULL;
393    ACPI_PARSE_OBJECT       *DefaultOp = NULL;
394    ACPI_PARSE_OBJECT       *CurrentParentNode;
395    ACPI_PARSE_OBJECT       *Conditional = NULL;
396    ACPI_PARSE_OBJECT       *Predicate;
397    ACPI_PARSE_OBJECT       *Peer;
398    ACPI_PARSE_OBJECT       *NewOp;
399    ACPI_PARSE_OBJECT       *NewOp2;
400    ACPI_PARSE_OBJECT       *MethodOp;
401    ACPI_PARSE_OBJECT       *StoreOp;
402    ACPI_PARSE_OBJECT       *BreakOp;
403    ACPI_PARSE_OBJECT       *BufferOp;
404    char                    *PredicateValueName;
405    UINT16                  Index;
406    UINT32                  Btype;
407
408
409    /* Start node is the Switch() node */
410
411    CurrentParentNode  = StartNode;
412
413    /* Create a new temp name of the form _T_x */
414
415    PredicateValueName = TrAmlGetNextTempName (StartNode, &Gbl_TempCount);
416    if (!PredicateValueName)
417    {
418        return;
419    }
420
421    /* First child is the Switch() predicate */
422
423    Next = StartNode->Asl.Child;
424
425    /*
426     * Examine the return type of the Switch Value -
427     * must be Integer/Buffer/String
428     */
429    Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
430    Btype = AslKeywordMapping[Index].AcpiBtype;
431    if ((Btype != ACPI_BTYPE_INTEGER) &&
432        (Btype != ACPI_BTYPE_STRING)  &&
433        (Btype != ACPI_BTYPE_BUFFER))
434    {
435        AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
436        Btype = ACPI_BTYPE_INTEGER;
437    }
438
439    /* CASE statements start at next child */
440
441    Peer = Next->Asl.Next;
442    while (Peer)
443    {
444        Next = Peer;
445        Peer = Next->Asl.Next;
446
447        if (Next->Asl.ParseOpcode == PARSEOP_CASE)
448        {
449            if (CaseOp)
450            {
451                /* Add an ELSE to complete the previous CASE */
452
453                if (!Conditional)
454                {
455                    return;
456                }
457                NewOp             = TrCreateLeafNode (PARSEOP_ELSE);
458                NewOp->Asl.Parent = Conditional->Asl.Parent;
459                TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
460
461                /* Link ELSE node as a peer to the previous IF */
462
463                TrAmlInsertPeer (Conditional, NewOp);
464                CurrentParentNode = NewOp;
465            }
466
467            CaseOp      = Next;
468            Conditional = CaseOp;
469            CaseBlock   = CaseOp->Asl.Child->Asl.Next;
470            Conditional->Asl.Child->Asl.Next = NULL;
471            Predicate = CaseOp->Asl.Child;
472
473            if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
474                (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
475            {
476                /*
477                 * Convert the package declaration to this form:
478                 *
479                 * If (LNotEqual (Match (Package(<size>){<data>},
480                 *                       MEQ, _T_x, MTR, Zero, Zero), Ones))
481                 */
482                NewOp2              = TrCreateLeafNode (PARSEOP_MATCHTYPE_MEQ);
483                Predicate->Asl.Next = NewOp2;
484                TrAmlInitLineNumbers (NewOp2, Conditional);
485
486                NewOp               = NewOp2;
487                NewOp2              = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
488                                        (UINT64) ACPI_TO_INTEGER (PredicateValueName));
489                NewOp->Asl.Next     = NewOp2;
490                TrAmlInitLineNumbers (NewOp2, Predicate);
491
492                NewOp               = NewOp2;
493                NewOp2              = TrCreateLeafNode (PARSEOP_MATCHTYPE_MTR);
494                NewOp->Asl.Next     = NewOp2;
495                TrAmlInitLineNumbers (NewOp2, Predicate);
496
497                NewOp               = NewOp2;
498                NewOp2              = TrCreateLeafNode (PARSEOP_ZERO);
499                NewOp->Asl.Next     = NewOp2;
500                TrAmlInitLineNumbers (NewOp2, Predicate);
501
502                NewOp               = NewOp2;
503                NewOp2              = TrCreateLeafNode (PARSEOP_ZERO);
504                NewOp->Asl.Next     = NewOp2;
505                TrAmlInitLineNumbers (NewOp2, Predicate);
506
507                NewOp2              = TrCreateLeafNode (PARSEOP_MATCH);
508                NewOp2->Asl.Child   = Predicate;  /* PARSEOP_PACKAGE */
509                TrAmlInitLineNumbers (NewOp2, Conditional);
510                TrAmlSetSubtreeParent (Predicate, NewOp2);
511
512                NewOp               = NewOp2;
513                NewOp2              = TrCreateLeafNode (PARSEOP_ONES);
514                NewOp->Asl.Next     = NewOp2;
515                TrAmlInitLineNumbers (NewOp2, Conditional);
516
517                NewOp2              = TrCreateLeafNode (PARSEOP_LEQUAL);
518                NewOp2->Asl.Child   = NewOp;
519                NewOp->Asl.Parent   = NewOp2;
520                TrAmlInitLineNumbers (NewOp2, Conditional);
521                TrAmlSetSubtreeParent (NewOp, NewOp2);
522
523                NewOp               = NewOp2;
524                NewOp2              = TrCreateLeafNode (PARSEOP_LNOT);
525                NewOp2->Asl.Child   = NewOp;
526                NewOp2->Asl.Parent  = Conditional;
527                NewOp->Asl.Parent   = NewOp2;
528                TrAmlInitLineNumbers (NewOp2, Conditional);
529
530                Conditional->Asl.Child = NewOp2;
531                NewOp2->Asl.Next = CaseBlock;
532            }
533            else
534            {
535                /*
536                 * Integer and Buffer case.
537                 *
538                 * Change CaseOp() to:  If (LEqual (SwitchValue, CaseValue)) {...}
539                 * Note: SwitchValue is first to allow the CaseValue to be implicitly
540                 * converted to the type of SwitchValue if necessary.
541                 *
542                 * CaseOp->Child is the case value
543                 * CaseOp->Child->Peer is the beginning of the case block
544                 */
545                NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
546                            (UINT64) ACPI_TO_INTEGER (PredicateValueName));
547                NewOp->Asl.Next = Predicate;
548                TrAmlInitLineNumbers (NewOp, Predicate);
549
550                NewOp2              = TrCreateLeafNode (PARSEOP_LEQUAL);
551                NewOp2->Asl.Parent  = Conditional;
552                NewOp2->Asl.Child   = NewOp;
553                TrAmlInitLineNumbers (NewOp2, Conditional);
554
555                TrAmlSetSubtreeParent (NewOp, NewOp2);
556
557                Predicate           = NewOp2;
558                Predicate->Asl.Next = CaseBlock;
559
560                TrAmlSetSubtreeParent (Predicate, Conditional);
561                Conditional->Asl.Child = Predicate;
562            }
563
564            /* Reinitialize the CASE node to an IF node */
565
566            TrAmlInitNode (Conditional, PARSEOP_IF);
567
568            /*
569             * The first CASE(IF) is not nested under an ELSE.
570             * All other CASEs are children of a parent ELSE.
571             */
572            if (CurrentParentNode == StartNode)
573            {
574                Conditional->Asl.Next = NULL;
575            }
576            else
577            {
578                /*
579                 * The IF is a child of previous IF/ELSE. It
580                 * is therefore without peer.
581                 */
582                CurrentParentNode->Asl.Child = Conditional;
583                Conditional->Asl.Parent      = CurrentParentNode;
584                Conditional->Asl.Next        = NULL;
585            }
586        }
587        else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
588        {
589            if (DefaultOp)
590            {
591                /*
592                 * More than one Default
593                 * (Parser does not catch this, must check here)
594                 */
595                AslError (ASL_ERROR, ASL_MSG_MULTIPLE_DEFAULT, Next, NULL);
596            }
597            else
598            {
599                /* Save the DEFAULT node for later, after CASEs */
600
601                DefaultOp = Next;
602            }
603        }
604        else
605        {
606            /* Unknown peer opcode */
607
608            AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%u)\n",
609                        Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
610        }
611    }
612
613    /* Add the default case at the end of the if/else construct */
614
615    if (DefaultOp)
616    {
617        /* If no CASE statements, this is an error - see below */
618
619        if (CaseOp)
620        {
621            /* Convert the DEFAULT node to an ELSE */
622
623            if (!Conditional)
624            {
625                return;
626            }
627
628            TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
629            DefaultOp->Asl.Parent = Conditional->Asl.Parent;
630
631            /* Link ELSE node as a peer to the previous IF */
632
633            TrAmlInsertPeer (Conditional, DefaultOp);
634        }
635    }
636
637    if (!CaseOp)
638    {
639        AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
640    }
641
642
643    /*
644     * Create a Name(_T_x, ...) statement. This statement must appear at the
645     * method level, in case a loop surrounds the switch statement and could
646     * cause the name to be created twice (error).
647     */
648
649    /* Create the Name node */
650
651    Predicate = StartNode->Asl.Child;
652    NewOp = TrCreateLeafNode (PARSEOP_NAME);
653    TrAmlInitLineNumbers (NewOp, StartNode);
654
655    /* Find the parent method */
656
657    Next = StartNode;
658    while ((Next->Asl.ParseOpcode != PARSEOP_METHOD) &&
659           (Next->Asl.ParseOpcode != PARSEOP_DEFINITIONBLOCK))
660    {
661        Next = Next->Asl.Parent;
662    }
663    MethodOp = Next;
664
665    NewOp->Asl.CompileFlags |= NODE_COMPILER_EMITTED;
666    NewOp->Asl.Parent = Next;
667
668    /* Insert name after the method name and arguments */
669
670    Next = Next->Asl.Child; /* Name */
671    Next = Next->Asl.Next;  /* NumArgs */
672    Next = Next->Asl.Next;  /* SerializeRule */
673
674    /*
675     * If method is not Serialized, we must make is so, because of the way
676     * that Switch() must be implemented -- we cannot allow multiple threads
677     * to execute this method concurrently since we need to create local
678     * temporary name(s).
679     */
680    if (Next->Asl.ParseOpcode != PARSEOP_SERIALIZERULE_SERIAL)
681    {
682        AslError (ASL_REMARK, ASL_MSG_SERIALIZED, MethodOp, "Due to use of Switch operator");
683        Next->Asl.ParseOpcode = PARSEOP_SERIALIZERULE_SERIAL;
684    }
685
686    Next = Next->Asl.Next;  /* SyncLevel */
687    Next = Next->Asl.Next;  /* ReturnType */
688    Next = Next->Asl.Next;  /* ParameterTypes */
689
690    TrAmlInsertPeer (Next, NewOp);
691    TrAmlInitLineNumbers (NewOp, Next);
692
693    /* Create the NameSeg child for the Name node */
694
695    NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
696                (UINT64) ACPI_TO_INTEGER (PredicateValueName));
697    TrAmlInitLineNumbers (NewOp2, NewOp);
698    NewOp2->Asl.CompileFlags |= NODE_IS_NAME_DECLARATION;
699    NewOp->Asl.Child  = NewOp2;
700
701    /* Create the initial value for the Name. Btype was already validated above */
702
703    switch (Btype)
704    {
705    case ACPI_BTYPE_INTEGER:
706
707        NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_ZERO,
708                                (UINT64) 0);
709        TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
710        break;
711
712    case ACPI_BTYPE_STRING:
713
714        NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_STRING_LITERAL,
715                                (UINT64) ACPI_TO_INTEGER (""));
716        TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
717        break;
718
719    case ACPI_BTYPE_BUFFER:
720
721        (void) TrLinkPeerNode (NewOp2, TrCreateValuedLeafNode (PARSEOP_BUFFER,
722                                    (UINT64) 0));
723        Next = NewOp2->Asl.Next;
724        TrAmlInitLineNumbers (Next, NewOp2);
725        (void) TrLinkChildren (Next, 1, TrCreateValuedLeafNode (PARSEOP_ZERO,
726                                    (UINT64) 1));
727        TrAmlInitLineNumbers (Next->Asl.Child, Next);
728
729        BufferOp = TrCreateValuedLeafNode (PARSEOP_DEFAULT_ARG, (UINT64) 0);
730        TrAmlInitLineNumbers (BufferOp, Next->Asl.Child);
731        (void) TrLinkPeerNode (Next->Asl.Child, BufferOp);
732
733        TrAmlSetSubtreeParent (Next->Asl.Child, Next);
734        break;
735
736    default:
737
738        break;
739    }
740
741    TrAmlSetSubtreeParent (NewOp2, NewOp);
742
743    /*
744     * Transform the Switch() into a While(One)-Break node.
745     * And create a Store() node which will be used to save the
746     * Switch() value. The store is of the form: Store (Value, _T_x)
747     * where _T_x is the temp variable.
748     */
749    TrAmlInitNode (StartNode, PARSEOP_WHILE);
750    NewOp = TrCreateLeafNode (PARSEOP_ONE);
751    TrAmlInitLineNumbers (NewOp, StartNode);
752    NewOp->Asl.Next = Predicate->Asl.Next;
753    NewOp->Asl.Parent = StartNode;
754    StartNode->Asl.Child = NewOp;
755
756    /* Create a Store() node */
757
758    StoreOp = TrCreateLeafNode (PARSEOP_STORE);
759    TrAmlInitLineNumbers (StoreOp, NewOp);
760    StoreOp->Asl.Parent = StartNode;
761    TrAmlInsertPeer (NewOp, StoreOp);
762
763    /* Complete the Store subtree */
764
765    StoreOp->Asl.Child = Predicate;
766    Predicate->Asl.Parent = StoreOp;
767
768    NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
769                (UINT64) ACPI_TO_INTEGER (PredicateValueName));
770    TrAmlInitLineNumbers (NewOp, StoreOp);
771    NewOp->Asl.Parent    = StoreOp;
772    Predicate->Asl.Next  = NewOp;
773
774    /* Create a Break() node and insert it into the end of While() */
775
776    Conditional = StartNode->Asl.Child;
777    while (Conditional->Asl.Next)
778    {
779        Conditional = Conditional->Asl.Next;
780    }
781
782    BreakOp = TrCreateLeafNode (PARSEOP_BREAK);
783    TrAmlInitLineNumbers (BreakOp, NewOp);
784    BreakOp->Asl.Parent = StartNode;
785    TrAmlInsertPeer (Conditional, BreakOp);
786}
787