asltransform.c revision 151937
1118611Snjl
2118611Snjl/******************************************************************************
3118611Snjl *
4118611Snjl * Module Name: asltransform - Parse tree transforms
5118611Snjl *              $Revision: 1.35 $
6118611Snjl *
7118611Snjl *****************************************************************************/
8118611Snjl
9118611Snjl/******************************************************************************
10118611Snjl *
11118611Snjl * 1. Copyright Notice
12193529Sjkim *
13118611Snjl * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
14118611Snjl * All rights reserved.
15118611Snjl *
16118611Snjl * 2. License
17118611Snjl *
18118611Snjl * 2.1. This is your license from Intel Corp. under its intellectual property
19118611Snjl * rights.  You may have additional license terms from the party that provided
20118611Snjl * you this software, covering your right to use that party's intellectual
21118611Snjl * property rights.
22118611Snjl *
23118611Snjl * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24118611Snjl * copy of the source code appearing in this file ("Covered Code") an
25118611Snjl * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26118611Snjl * base code distributed originally by Intel ("Original Intel Code") to copy,
27118611Snjl * make derivatives, distribute, use and display any portion of the Covered
28118611Snjl * Code in any form, with the right to sublicense such rights; and
29118611Snjl *
30118611Snjl * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31118611Snjl * license (with the right to sublicense), under only those claims of Intel
32118611Snjl * patents that are infringed by the Original Intel Code, to make, use, sell,
33118611Snjl * offer to sell, and import the Covered Code and derivative works thereof
34118611Snjl * solely to the minimum extent necessary to exercise the above copyright
35118611Snjl * license, and in no event shall the patent license extend to any additions
36118611Snjl * to or modifications of the Original Intel Code.  No other license or right
37118611Snjl * is granted directly or by implication, estoppel or otherwise;
38118611Snjl *
39118611Snjl * The above copyright and patent license is granted only if the following
40118611Snjl * conditions are met:
41118611Snjl *
42118611Snjl * 3. Conditions
43118611Snjl *
44118611Snjl * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45118611Snjl * Redistribution of source code of any substantial portion of the Covered
46118611Snjl * Code or modification with rights to further distribute source must include
47118611Snjl * the above Copyright Notice, the above License, this list of Conditions,
48118611Snjl * and the following Disclaimer and Export Compliance provision.  In addition,
49118611Snjl * Licensee must cause all Covered Code to which Licensee contributes to
50118611Snjl * contain a file documenting the changes Licensee made to create that Covered
51118611Snjl * Code and the date of any change.  Licensee must include in that file the
52118611Snjl * documentation of any changes made by any predecessor Licensee.  Licensee
53118611Snjl * must include a prominent statement that the modification is derived,
54118611Snjl * directly or indirectly, from Original Intel Code.
55118611Snjl *
56118611Snjl * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57118611Snjl * Redistribution of source code of any substantial portion of the Covered
58118611Snjl * Code or modification without rights to further distribute source must
59118611Snjl * include the following Disclaimer and Export Compliance provision in the
60118611Snjl * documentation and/or other materials provided with distribution.  In
61118611Snjl * addition, Licensee may not authorize further sublicense of source of any
62118611Snjl * portion of the Covered Code, and must include terms to the effect that the
63118611Snjl * license from Licensee to its licensee is limited to the intellectual
64118611Snjl * property embodied in the software Licensee provides to its licensee, and
65118611Snjl * not to intellectual property embodied in modifications its licensee may
66118611Snjl * make.
67118611Snjl *
68118611Snjl * 3.3. Redistribution of Executable. Redistribution in executable form of any
69118611Snjl * substantial portion of the Covered Code or modification must reproduce the
70118611Snjl * above Copyright Notice, and the following Disclaimer and Export Compliance
71118611Snjl * provision in the documentation and/or other materials provided with the
72118611Snjl * distribution.
73118611Snjl *
74118611Snjl * 3.4. Intel retains all right, title, and interest in and to the Original
75118611Snjl * Intel Code.
76118611Snjl *
77118611Snjl * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78118611Snjl * Intel shall be used in advertising or otherwise to promote the sale, use or
79118611Snjl * other dealings in products derived from or relating to the Covered Code
80118611Snjl * without prior written authorization from Intel.
81118611Snjl *
82118611Snjl * 4. Disclaimer and Export Compliance
83118611Snjl *
84118611Snjl * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85118611Snjl * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86118611Snjl * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87118611Snjl * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88118611Snjl * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89118611Snjl * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90118611Snjl * PARTICULAR PURPOSE.
91118611Snjl *
92118611Snjl * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93118611Snjl * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94118611Snjl * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95118611Snjl * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96118611Snjl * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97118611Snjl * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98118611Snjl * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99118611Snjl * LIMITED REMEDY.
100118611Snjl *
101118611Snjl * 4.3. Licensee shall not export, either directly or indirectly, any of this
102118611Snjl * software or system incorporating such software without first obtaining any
103118611Snjl * required license or other approval from the U. S. Department of Commerce or
104118611Snjl * any other agency or department of the United States Government.  In the
105118611Snjl * event Licensee exports any such software from the United States or
106118611Snjl * re-exports any such software from a foreign destination, Licensee shall
107118611Snjl * ensure that the distribution and export/re-export of the software is in
108118611Snjl * compliance with all laws, regulations, orders, or other restrictions of the
109118611Snjl * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110118611Snjl * any of its subsidiaries will export/re-export any technical data, process,
111118611Snjl * software, or service, directly or indirectly, to any country for which the
112118611Snjl * United States government or any agency thereof requires an export license,
113118611Snjl * other governmental approval, or letter of assurance, without first obtaining
114118611Snjl * such license, approval or letter.
115118611Snjl *
116118611Snjl *****************************************************************************/
117118611Snjl
118151937Sjkim
119118611Snjl#include <contrib/dev/acpica/compiler/aslcompiler.h>
120118611Snjl#include "aslcompiler.y.h"
121118611Snjl
122118611Snjl#define _COMPONENT          ACPI_COMPILER
123118611Snjl        ACPI_MODULE_NAME    ("asltransform")
124151937Sjkim
125118611Snjl/* Local prototypes */
126151937Sjkim
127151937Sjkimstatic void
128151937SjkimTrTransformSubtree (
129151937Sjkim    ACPI_PARSE_OBJECT       *Op);
130151937Sjkim
131151937Sjkimstatic char *
132151937SjkimTrAmlGetNextTempName (
133151937Sjkim    ACPI_PARSE_OBJECT       *Op,
134151937Sjkim    UINT8                   *TempCount);
135151937Sjkim
136151937Sjkimstatic void
137151937SjkimTrAmlInitLineNumbers (
138151937Sjkim    ACPI_PARSE_OBJECT       *Op,
139151937Sjkim    ACPI_PARSE_OBJECT       *Neighbor);
140151937Sjkim
141151937Sjkimstatic void
142151937SjkimTrAmlInitNode (
143151937Sjkim    ACPI_PARSE_OBJECT       *Op,
144151937Sjkim    UINT16                  ParseOpcode);
145151937Sjkim
146151937Sjkimstatic void
147151937SjkimTrAmlSetSubtreeParent (
148151937Sjkim    ACPI_PARSE_OBJECT       *Op,
149151937Sjkim    ACPI_PARSE_OBJECT       *Parent);
150151937Sjkim
151151937Sjkimstatic void
152151937SjkimTrAmlInsertPeer (
153151937Sjkim    ACPI_PARSE_OBJECT       *Op,
154151937Sjkim    ACPI_PARSE_OBJECT       *NewPeer);
155151937Sjkim
156151937Sjkimstatic void
157151937SjkimTrDoDefinitionBlock (
158151937Sjkim    ACPI_PARSE_OBJECT       *Op);
159151937Sjkim
160151937Sjkimstatic void
161151937SjkimTrDoSwitch (
162151937Sjkim    ACPI_PARSE_OBJECT       *StartNode);
163151937Sjkim
164118611Snjl
165118611Snjl/*******************************************************************************
166118611Snjl *
167118611Snjl * FUNCTION:    TrAmlGetNextTempName
168151937Sjkim *
169151937Sjkim * PARAMETERS:  Op              - Current parse op
170151937Sjkim *              TempCount       - Current temporary counter. Was originally
171151937Sjkim *                                per-module; Currently per method, could be
172118611Snjl *                                expanded to per-scope.
173151937Sjkim *
174118611Snjl * RETURN:      A pointer to name (allocated here).
175138287Smarks *
176151937Sjkim * DESCRIPTION: Generate an ACPI name of the form _T_x.  These names are
177118611Snjl *              reserved for use by the ASL compiler. (_T_0 through _T_Z)
178118611Snjl *
179118611Snjl ******************************************************************************/
180151937Sjkim
181118611Snjlstatic char *
182151937SjkimTrAmlGetNextTempName (
183151937Sjkim    ACPI_PARSE_OBJECT       *Op,
184118611Snjl    UINT8                   *TempCount)
185118611Snjl{
186118611Snjl    char                    *TempName;
187118611Snjl
188151937Sjkim
189118611Snjl    if (*TempCount >= (10+26))  /* 0-35 valid: 0-9 and A-Z for TempName[3] */
190118611Snjl    {
191151937Sjkim        /* Too many temps */
192151937Sjkim
193151937Sjkim        AslError (ASL_ERROR, ASL_MSG_TOO_MANY_TEMPS, Op, NULL);
194118611Snjl        return (NULL);
195118611Snjl    }
196151937Sjkim
197138287Smarks    TempName = UtLocalCalloc (5);
198151937Sjkim
199138287Smarks    if (*TempCount < 10)    /* 0-9 */
200151937Sjkim    {
201138287Smarks        TempName[3] = (char) (*TempCount + '0');
202151937Sjkim    }
203138287Smarks    else                    /* 10-35: A-Z */
204151937Sjkim    {
205138287Smarks        TempName[3] = (char) (*TempCount + ('A' - 10));
206151937Sjkim    }
207118611Snjl    (*TempCount)++;
208151937Sjkim
209118611Snjl    /* First three characters are always "_T_" */
210151937Sjkim
211151937Sjkim    TempName[0] = '_';
212151937Sjkim    TempName[1] = 'T';
213118611Snjl    TempName[2] = '_';
214151937Sjkim
215118611Snjl    return (TempName);
216118611Snjl}
217118611Snjl
218118611Snjl
219118611Snjl/*******************************************************************************
220118611Snjl *
221118611Snjl * FUNCTION:    TrAmlInitLineNumbers
222151937Sjkim *
223118611Snjl * PARAMETERS:  Op              - Op to be initialized
224118611Snjl *              Neighbor        - Op used for initialization values
225118611Snjl *
226118611Snjl * RETURN:      None
227118611Snjl *
228118611Snjl * DESCRIPTION: Initialized the various line numbers for a parse node.
229118611Snjl *
230118611Snjl ******************************************************************************/
231151937Sjkim
232118611Snjlstatic void
233118611SnjlTrAmlInitLineNumbers (
234118611Snjl    ACPI_PARSE_OBJECT       *Op,
235118611Snjl    ACPI_PARSE_OBJECT       *Neighbor)
236118611Snjl{
237118611Snjl
238118611Snjl    Op->Asl.EndLine           = Neighbor->Asl.EndLine;
239118611Snjl    Op->Asl.EndLogicalLine    = Neighbor->Asl.EndLogicalLine;
240118611Snjl    Op->Asl.LineNumber        = Neighbor->Asl.LineNumber;
241118611Snjl    Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
242118611Snjl    Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
243118611Snjl}
244118611Snjl
245118611Snjl
246118611Snjl/*******************************************************************************
247118611Snjl *
248118611Snjl * FUNCTION:    TrAmlInitNode
249151937Sjkim *
250118611Snjl * PARAMETERS:  Op              - Op to be initialized
251118611Snjl *              ParseOpcode     - Opcode for this node
252118611Snjl *
253118611Snjl * RETURN:      None
254118611Snjl *
255118611Snjl * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
256118611Snjl *
257118611Snjl ******************************************************************************/
258151937Sjkim
259118611Snjlstatic void
260118611SnjlTrAmlInitNode (
261118611Snjl    ACPI_PARSE_OBJECT       *Op,
262118611Snjl    UINT16                  ParseOpcode)
263118611Snjl{
264118611Snjl
265118611Snjl    Op->Asl.ParseOpcode = ParseOpcode;
266118611Snjl    UtSetParseOpName (Op);
267118611Snjl}
268118611Snjl
269118611Snjl
270118611Snjl/*******************************************************************************
271118611Snjl *
272118611Snjl * FUNCTION:    TrAmlSetSubtreeParent
273151937Sjkim *
274118611Snjl * PARAMETERS:  Op              - First node in a list of peer nodes
275118611Snjl *              Parent          - Parent of the subtree
276118611Snjl *
277118611Snjl * RETURN:      None
278118611Snjl *
279118611Snjl * DESCRIPTION: Set the parent for all peer nodes in a subtree
280118611Snjl *
281118611Snjl ******************************************************************************/
282151937Sjkim
283118611Snjlstatic void
284118611SnjlTrAmlSetSubtreeParent (
285118611Snjl    ACPI_PARSE_OBJECT       *Op,
286118611Snjl    ACPI_PARSE_OBJECT       *Parent)
287118611Snjl{
288118611Snjl    ACPI_PARSE_OBJECT       *Next;
289118611Snjl
290118611Snjl
291118611Snjl    Next = Op;
292118611Snjl    while (Next)
293118611Snjl    {
294118611Snjl        Next->Asl.Parent = Parent;
295118611Snjl        Next             = Next->Asl.Next;
296118611Snjl    }
297118611Snjl}
298118611Snjl
299118611Snjl
300118611Snjl/*******************************************************************************
301118611Snjl *
302118611Snjl * FUNCTION:    TrAmlInsertPeer
303151937Sjkim *
304118611Snjl * PARAMETERS:  Op              - First node in a list of peer nodes
305118611Snjl *              NewPeer         - Peer node to insert
306118611Snjl *
307118611Snjl * RETURN:      None
308118611Snjl *
309118611Snjl * DESCRIPTION: Insert a new peer node into a list of peers.
310118611Snjl *
311118611Snjl ******************************************************************************/
312151937Sjkim
313118611Snjlstatic void
314118611SnjlTrAmlInsertPeer (
315118611Snjl    ACPI_PARSE_OBJECT       *Op,
316118611Snjl    ACPI_PARSE_OBJECT       *NewPeer)
317118611Snjl{
318118611Snjl
319118611Snjl    NewPeer->Asl.Next = Op->Asl.Next;
320118611Snjl    Op->Asl.Next      = NewPeer;
321118611Snjl}
322118611Snjl
323118611Snjl
324118611Snjl/*******************************************************************************
325118611Snjl *
326118611Snjl * FUNCTION:    TrAmlTransformWalk
327118611Snjl *
328118611Snjl * PARAMETERS:  ASL_WALK_CALLBACK
329118611Snjl *
330118611Snjl * RETURN:      None
331118611Snjl *
332118611Snjl * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
333118611Snjl *              operands.
334118611Snjl *
335118611Snjl ******************************************************************************/
336118611Snjl
337118611SnjlACPI_STATUS
338118611SnjlTrAmlTransformWalk (
339118611Snjl    ACPI_PARSE_OBJECT       *Op,
340118611Snjl    UINT32                  Level,
341118611Snjl    void                    *Context)
342118611Snjl{
343118611Snjl
344118611Snjl    TrTransformSubtree (Op);
345118611Snjl    return (AE_OK);
346118611Snjl}
347118611Snjl
348118611Snjl
349118611Snjl/*******************************************************************************
350118611Snjl *
351118611Snjl * FUNCTION:    TrTransformSubtree
352118611Snjl *
353118611Snjl * PARAMETERS:  Op        - The parent parse node
354118611Snjl *
355118611Snjl * RETURN:      None
356118611Snjl *
357118611Snjl * DESCRIPTION: Prepare nodes to be output as AML data and operands.  The more
358118611Snjl *              complex AML opcodes require processing of the child nodes
359118611Snjl *              (arguments/operands).
360118611Snjl *
361118611Snjl ******************************************************************************/
362151937Sjkim
363118611Snjlstatic void
364118611SnjlTrTransformSubtree (
365118611Snjl    ACPI_PARSE_OBJECT           *Op)
366118611Snjl{
367118611Snjl
368118611Snjl    if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
369118611Snjl    {
370118611Snjl        return;
371118611Snjl    }
372118611Snjl
373118611Snjl    switch (Op->Asl.ParseOpcode)
374118611Snjl    {
375118611Snjl    case PARSEOP_DEFINITIONBLOCK:
376118611Snjl        TrDoDefinitionBlock (Op);
377118611Snjl        break;
378118611Snjl
379118611Snjl    case PARSEOP_SWITCH:
380118611Snjl        TrDoSwitch (Op);
381118611Snjl        break;
382151937Sjkim
383151937Sjkim    case PARSEOP_METHOD:
384151937Sjkim
385151937Sjkim        /*
386151937Sjkim         * TBD: Zero the tempname (_T_x) count. Probably shouldn't be a global,
387151937Sjkim         * however
388151937Sjkim         */
389151937Sjkim        Gbl_TempCount = 0;
390151937Sjkim        break;
391118611Snjl
392118611Snjl    default:
393118611Snjl        /* Nothing to do here for other opcodes */
394118611Snjl        break;
395118611Snjl    }
396118611Snjl}
397118611Snjl
398118611Snjl
399118611Snjl/*******************************************************************************
400118611Snjl *
401118611Snjl * FUNCTION:    TrDoDefinitionBlock
402118611Snjl *
403118611Snjl * PARAMETERS:  Op        - Parse node
404118611Snjl *
405118611Snjl * RETURN:      None
406118611Snjl *
407118611Snjl * DESCRIPTION: Find the end of the definition block and set a global to this
408118611Snjl *              node.  It is used by the compiler to insert compiler-generated
409118611Snjl *              names at the root level of the namespace.
410118611Snjl *
411118611Snjl ******************************************************************************/
412151937Sjkim
413118611Snjlstatic void
414118611SnjlTrDoDefinitionBlock (
415118611Snjl    ACPI_PARSE_OBJECT       *Op)
416118611Snjl{
417118611Snjl    ACPI_PARSE_OBJECT       *Next;
418118611Snjl    UINT32                  i;
419118611Snjl
420118611Snjl
421118611Snjl    Next = Op->Asl.Child;
422118611Snjl    for (i = 0; i < 5; i++)
423118611Snjl    {
424151937Sjkim        Next = Next->Asl.Next;
425151937Sjkim        if (i == 0)
426151937Sjkim        {
427151937Sjkim            /*
428151937Sjkim             * This is the table signature. Only the DSDT can be assumed
429151937Sjkim             * to be at the root of the namespace;  Therefore, namepath
430151937Sjkim             * optimization can only be performed on the DSDT.
431167802Sjkim             */
432151937Sjkim            if (ACPI_STRNCMP (Next->Asl.Value.String, "DSDT", 4))
433151937Sjkim            {
434151937Sjkim                Gbl_ReferenceOptimizationFlag = FALSE;
435151937Sjkim            }
436118611Snjl        }
437118611Snjl    }
438118611Snjl
439118611Snjl    Gbl_FirstLevelInsertionNode = Next;
440118611Snjl}
441118611Snjl
442118611Snjl
443118611Snjl/*******************************************************************************
444118611Snjl *
445118611Snjl * FUNCTION:    TrDoSwitch
446118611Snjl *
447118611Snjl * PARAMETERS:  StartNode        - Parse node for SWITCH
448118611Snjl *
449118611Snjl * RETURN:      None
450118611Snjl *
451118611Snjl *
452118611Snjl * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs.  There is
453118611Snjl *              no actual AML opcode for SWITCH -- it must be simulated.
454118611Snjl *
455118611Snjl ******************************************************************************/
456151937Sjkim
457118611Snjlstatic void
458118611SnjlTrDoSwitch (
459118611Snjl    ACPI_PARSE_OBJECT       *StartNode)
460118611Snjl{
461118611Snjl    ACPI_PARSE_OBJECT       *Next;
462118611Snjl    ACPI_PARSE_OBJECT       *CaseOp = NULL;
463118611Snjl    ACPI_PARSE_OBJECT       *CaseBlock = NULL;
464118611Snjl    ACPI_PARSE_OBJECT       *DefaultOp = NULL;
465118611Snjl    ACPI_PARSE_OBJECT       *CurrentParentNode;
466118611Snjl    ACPI_PARSE_OBJECT       *Conditional = NULL;
467118611Snjl    ACPI_PARSE_OBJECT       *Predicate;
468118611Snjl    ACPI_PARSE_OBJECT       *Peer;
469118611Snjl    ACPI_PARSE_OBJECT       *NewOp;
470193529Sjkim    ACPI_PARSE_OBJECT       *NewOp2;
471199337Sjkim    char                    *PredicateValueName;
472199337Sjkim    UINT16                  Index;
473118611Snjl    UINT32                  Btype;
474151937Sjkim
475151937Sjkim
476118611Snjl    /* Start node is the Switch() node */
477118611Snjl
478151937Sjkim    CurrentParentNode  = StartNode;
479151937Sjkim
480118611Snjl    /* Create a new temp name of the form _T_x */
481118611Snjl
482151937Sjkim    PredicateValueName = TrAmlGetNextTempName (StartNode, &Gbl_TempCount);
483118611Snjl    if (!PredicateValueName)
484151937Sjkim    {
485151937Sjkim        return;
486151937Sjkim    }
487151937Sjkim
488151937Sjkim    /* First child is the Switch() predicate */
489151937Sjkim
490151937Sjkim    Next = StartNode->Asl.Child;
491151937Sjkim
492118611Snjl    /*
493118611Snjl     * Examine the return type of the Switch Value -
494151937Sjkim     * must be Integer/Buffer/String
495151937Sjkim     */
496151937Sjkim    Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
497151937Sjkim    Btype = AslKeywordMapping[Index].AcpiBtype;
498151937Sjkim    if ((Btype != ACPI_BTYPE_INTEGER) &&
499151937Sjkim        (Btype != ACPI_BTYPE_STRING)  &&
500151937Sjkim        (Btype != ACPI_BTYPE_BUFFER))
501151937Sjkim    {
502151937Sjkim        AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
503151937Sjkim        Btype = ACPI_BTYPE_INTEGER;
504151937Sjkim    }
505151937Sjkim
506151937Sjkim    /* CASE statements start at next child */
507151937Sjkim
508118611Snjl    Peer = Next->Asl.Next;
509118611Snjl    while (Peer)
510151937Sjkim    {
511118611Snjl        Next = Peer;
512118611Snjl        Peer = Next->Asl.Next;
513118611Snjl
514118611Snjl        if (Next->Asl.ParseOpcode == PARSEOP_CASE)
515118611Snjl        {
516118611Snjl            if (CaseOp)
517118611Snjl            {
518118611Snjl                /* Add an ELSE to complete the previous CASE */
519118611Snjl
520118611Snjl                if (!Conditional)
521118611Snjl                {
522151937Sjkim                    return;
523151937Sjkim                }
524151937Sjkim                NewOp             = TrCreateLeafNode (PARSEOP_ELSE);
525151937Sjkim                NewOp->Asl.Parent = Conditional->Asl.Parent;
526118611Snjl                TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
527118611Snjl
528118611Snjl                /* Link ELSE node as a peer to the previous IF */
529118611Snjl
530118611Snjl                TrAmlInsertPeer (Conditional, NewOp);
531118611Snjl                CurrentParentNode = NewOp;
532118611Snjl            }
533118611Snjl
534118611Snjl            CaseOp      = Next;
535118611Snjl            Conditional = CaseOp;
536118611Snjl            CaseBlock   = CaseOp->Asl.Child->Asl.Next;
537118611Snjl            Conditional->Asl.Child->Asl.Next = NULL;
538118611Snjl            Predicate = CaseOp->Asl.Child;
539118611Snjl
540138287Smarks            if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
541118611Snjl                (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
542151937Sjkim            {
543151937Sjkim                /*
544138287Smarks                 * Convert the package declaration to this form:
545138287Smarks                 *
546138287Smarks                 * If (LNotEqual (Match (Package(<size>){<data>},
547138287Smarks                 *                       MEQ, _T_x, MTR, Zero, Zero), Ones))
548151937Sjkim                 */
549151937Sjkim                NewOp2              = TrCreateLeafNode (PARSEOP_MATCHTYPE_MEQ);
550138287Smarks                Predicate->Asl.Next = NewOp2;
551138287Smarks                TrAmlInitLineNumbers (NewOp2, Conditional);
552138287Smarks
553138287Smarks                NewOp               = NewOp2;
554118611Snjl                NewOp2              = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
555138287Smarks                                        (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
556138287Smarks                NewOp->Asl.Next     = NewOp2;
557151937Sjkim                TrAmlInitLineNumbers (NewOp2, Predicate);
558138287Smarks
559138287Smarks                NewOp               = NewOp2;
560118611Snjl                NewOp2              = TrCreateLeafNode (PARSEOP_MATCHTYPE_MTR);
561138287Smarks                NewOp->Asl.Next     = NewOp2;
562138287Smarks                TrAmlInitLineNumbers (NewOp2, Predicate);
563138287Smarks
564138287Smarks                NewOp               = NewOp2;
565118611Snjl                NewOp2              = TrCreateLeafNode (PARSEOP_ZERO);
566138287Smarks                NewOp->Asl.Next     = NewOp2;
567138287Smarks                TrAmlInitLineNumbers (NewOp2, Predicate);
568138287Smarks
569138287Smarks                NewOp               = NewOp2;
570118611Snjl                NewOp2              = TrCreateLeafNode (PARSEOP_ZERO);
571138287Smarks                NewOp->Asl.Next     = NewOp2;
572138287Smarks                TrAmlInitLineNumbers (NewOp2, Predicate);
573138287Smarks
574138287Smarks                NewOp2              = TrCreateLeafNode (PARSEOP_MATCH);
575118611Snjl                NewOp2->Asl.Child   = Predicate;  /* PARSEOP_PACKAGE */
576138287Smarks                TrAmlInitLineNumbers (NewOp2, Conditional);
577138287Smarks                TrAmlSetSubtreeParent (Predicate, NewOp2);
578138287Smarks
579138287Smarks                NewOp               = NewOp2;
580138287Smarks                NewOp2              = TrCreateLeafNode (PARSEOP_ONES);
581138287Smarks                NewOp->Asl.Next     = NewOp2;
582138287Smarks                TrAmlInitLineNumbers (NewOp2, Conditional);
583138287Smarks
584138287Smarks                NewOp2              = TrCreateLeafNode (PARSEOP_LEQUAL);
585138287Smarks                NewOp2->Asl.Child   = NewOp;
586138287Smarks                NewOp->Asl.Parent   = NewOp2;
587138287Smarks                TrAmlInitLineNumbers (NewOp2, Conditional);
588138287Smarks                TrAmlSetSubtreeParent (NewOp, NewOp2);
589138287Smarks
590138287Smarks                NewOp               = NewOp2;
591138287Smarks                NewOp2              = TrCreateLeafNode (PARSEOP_LNOT);
592138287Smarks                NewOp2->Asl.Child   = NewOp;
593138287Smarks                NewOp2->Asl.Parent  = Conditional;
594138287Smarks                NewOp->Asl.Parent   = NewOp2;
595138287Smarks                TrAmlInitLineNumbers (NewOp2, Conditional);
596138287Smarks
597138287Smarks                Conditional->Asl.Child = NewOp2;
598138287Smarks                NewOp2->Asl.Next = CaseBlock;
599138287Smarks            }
600138287Smarks            else
601138287Smarks            {
602138287Smarks                /*
603138287Smarks                 * Integer and Buffer case.
604138287Smarks                 *
605151937Sjkim                 * Change CaseOp() to:  If (LEqual (SwitchValue, CaseValue)) {...}
606151937Sjkim                 * Note: SwitchValue is first to allow the CaseValue to be implicitly
607151937Sjkim                 * converted to the type of SwitchValue if necessary.
608151937Sjkim                 *
609151937Sjkim                 * CaseOp->Child is the case value
610151937Sjkim                 * CaseOp->Child->Peer is the beginning of the case block
611138287Smarks                 */
612138287Smarks                NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
613138287Smarks                            (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
614138287Smarks                NewOp->Asl.Next = Predicate;
615151937Sjkim                TrAmlInitLineNumbers (NewOp, Predicate);
616151937Sjkim
617138287Smarks                NewOp2              = TrCreateLeafNode (PARSEOP_LEQUAL);
618138287Smarks                NewOp2->Asl.Parent  = Conditional;
619138287Smarks                NewOp2->Asl.Child   = NewOp;
620138287Smarks                TrAmlInitLineNumbers (NewOp2, Conditional);
621151937Sjkim
622138287Smarks                TrAmlSetSubtreeParent (NewOp, NewOp2);
623138287Smarks
624151937Sjkim                Predicate           = NewOp2;
625138287Smarks                Predicate->Asl.Next = CaseBlock;
626138287Smarks
627138287Smarks                TrAmlSetSubtreeParent (Predicate, Conditional);
628138287Smarks                Conditional->Asl.Child = Predicate;
629138287Smarks            }
630138287Smarks
631138287Smarks            /* Reinitialize the CASE node to an IF node */
632138287Smarks
633118611Snjl            TrAmlInitNode (Conditional, PARSEOP_IF);
634118611Snjl
635118611Snjl            /*
636118611Snjl             * The first CASE(IF) is not nested under an ELSE.
637118611Snjl             * All other CASEs are children of a parent ELSE.
638118611Snjl             */
639118611Snjl            if (CurrentParentNode == StartNode)
640118611Snjl            {
641118611Snjl                Conditional->Asl.Parent = CurrentParentNode->Asl.Parent;
642118611Snjl
643199337Sjkim                /* Link IF into the peer list */
644118611Snjl
645118611Snjl                TrAmlInsertPeer (CurrentParentNode, Conditional);
646118611Snjl            }
647118611Snjl            else
648118611Snjl            {
649118611Snjl                /*
650118611Snjl                 * The IF is a child of previous IF/ELSE.  It
651118611Snjl                 * is therefore without peer.
652118611Snjl                 */
653118611Snjl                CurrentParentNode->Asl.Child = Conditional;
654118611Snjl                Conditional->Asl.Parent      = CurrentParentNode;
655118611Snjl                Conditional->Asl.Next        = NULL;
656118611Snjl            }
657118611Snjl        }
658118611Snjl        else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
659118611Snjl        {
660138287Smarks            if (DefaultOp)
661138287Smarks            {
662167802Sjkim                /*
663138287Smarks                 * More than one Default
664167802Sjkim                 * (Parser should catch this, should not get here)
665118611Snjl                 */
666167802Sjkim                AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Next,
667167802Sjkim                    "Found more than one Default()");
668167802Sjkim            }
669118611Snjl
670167802Sjkim            /* Save the DEFAULT node for later, after CASEs */
671167802Sjkim
672118611Snjl            DefaultOp = Next;
673118611Snjl        }
674118611Snjl        else
675138287Smarks        {
676118611Snjl            /* Unknown peer opcode */
677151937Sjkim
678118611Snjl            AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%d)\n",
679118611Snjl                        Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
680118611Snjl        }
681118611Snjl    }
682151937Sjkim
683151937Sjkim    /* Add the default case at the end of the if/else construct */
684118611Snjl
685118611Snjl    if (DefaultOp)
686138287Smarks    {
687138287Smarks        /* If no CASE statements, this is an error - see below */
688118611Snjl
689118611Snjl        if (CaseOp)
690138287Smarks        {
691118611Snjl            /* Convert the DEFAULT node to an ELSE */
692151937Sjkim
693151937Sjkim            if (!Conditional)
694151937Sjkim            {
695151937Sjkim                return;
696199337Sjkim            }
697118611Snjl            TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
698118611Snjl            DefaultOp->Asl.Parent = Conditional->Asl.Parent;
699118611Snjl
700138287Smarks            /* Link ELSE node as a peer to the previous IF */
701138287Smarks
702138287Smarks            TrAmlInsertPeer (Conditional, DefaultOp);
703118611Snjl        }
704118611Snjl    }
705118611Snjl
706138287Smarks    if (!CaseOp)
707138287Smarks    {
708138287Smarks        AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
709138287Smarks    }
710138287Smarks
711167802Sjkim    /*
712118611Snjl     * Add a NAME node for the temp integer:
713167802Sjkim     * Change the SWITCH node to a Name (_T_x, Type)
714167802Sjkim     */
715167802Sjkim    Predicate = StartNode->Asl.Child;
716118611Snjl    TrAmlInitNode (StartNode, PARSEOP_NAME);
717167802Sjkim
718167802Sjkim    NewOp = StartNode;
719167802Sjkim
720151937Sjkim    NewOp->Asl.CompileFlags |= NODE_COMPILER_EMITTED;
721167802Sjkim
722151937Sjkim    NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
723167802Sjkim                (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
724151937Sjkim    NewOp2->Asl.CompileFlags |= NODE_IS_NAME_DECLARATION;
725167802Sjkim    NewOp->Asl.Child  = NewOp2;
726167802Sjkim
727167802Sjkim    /* Btype was already validated above */
728167802Sjkim
729167802Sjkim    switch (Btype)
730167802Sjkim    {
731193529Sjkim    case ACPI_BTYPE_INTEGER:
732167802Sjkim        NewOp2->Asl.Next  = TrCreateValuedLeafNode (PARSEOP_ZERO,
733138287Smarks                                (ACPI_INTEGER) 0);
734167802Sjkim        break;
735118611Snjl
736167802Sjkim    case ACPI_BTYPE_STRING:
737167802Sjkim        NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_STRING_LITERAL,
738193529Sjkim                                (ACPI_INTEGER) "");
739193529Sjkim        break;
740193529Sjkim
741167802Sjkim    case ACPI_BTYPE_BUFFER:
742193529Sjkim        (void) TrLinkPeerNode (NewOp2, TrCreateValuedLeafNode (PARSEOP_BUFFER,
743193529Sjkim                                    (ACPI_INTEGER) 0));
744193529Sjkim        Next = NewOp2->Asl.Next;
745193529Sjkim        (void) TrLinkChildren (Next, 1, TrCreateValuedLeafNode (PARSEOP_ZERO,
746193529Sjkim                                    (ACPI_INTEGER) 1));
747193529Sjkim        (void) TrLinkPeerNode (Next->Asl.Child,
748193529Sjkim            TrCreateValuedLeafNode (PARSEOP_DEFAULT_ARG, (ACPI_INTEGER) 0));
749193529Sjkim
750193529Sjkim        TrAmlSetSubtreeParent (Next->Asl.Child, Next);
751193529Sjkim        break;
752193529Sjkim
753193529Sjkim    default:
754193529Sjkim        break;
755193529Sjkim    }
756193529Sjkim
757193529Sjkim    TrAmlSetSubtreeParent (NewOp2, NewOp);
758167802Sjkim
759167802Sjkim    /*
760167802Sjkim     * Create and insert a new Store() node which will be used to save the
761167802Sjkim     * Switch() value.  The store is of the form: Store (Value, _T_x)
762167802Sjkim     * where _T_x is the temp variable.
763151937Sjkim     */
764151937Sjkim    Next = TrCreateLeafNode (PARSEOP_STORE);
765151937Sjkim    TrAmlInsertPeer (StartNode, Next);
766118611Snjl    TrAmlSetSubtreeParent (Next, StartNode->Asl.Parent);
767118611Snjl
768167802Sjkim    TrAmlInitLineNumbers (Next, StartNode);
769118611Snjl    TrAmlInitLineNumbers (NewOp2, StartNode);
770151937Sjkim    TrAmlInitLineNumbers (NewOp2->Asl.Next, StartNode);
771151937Sjkim
772151937Sjkim    /* Complete the Store subtree */
773167802Sjkim
774151937Sjkim    Next->Asl.Child       = Predicate;
775151937Sjkim    Predicate->Asl.Parent = Next;
776118611Snjl
777151937Sjkim    NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
778151937Sjkim                (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
779193529Sjkim    NewOp->Asl.Parent    = Next;
780151937Sjkim    Predicate->Asl.Next  = NewOp;
781118611Snjl}
782151937Sjkim
783151937Sjkim
784151937Sjkim