asltransform.c revision 209746
155682Smarkm
2233294Sstas/******************************************************************************
3233294Sstas *
4233294Sstas * Module Name: asltransform - Parse tree transforms
555682Smarkm *
6233294Sstas *****************************************************************************/
7233294Sstas
8233294Sstas/******************************************************************************
955682Smarkm *
10233294Sstas * 1. Copyright Notice
11233294Sstas *
1255682Smarkm * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
13233294Sstas * All rights reserved.
14233294Sstas *
15233294Sstas * 2. License
1655682Smarkm *
17233294Sstas * 2.1. This is your license from Intel Corp. under its intellectual property
18233294Sstas * rights.  You may have additional license terms from the party that provided
19233294Sstas * you this software, covering your right to use that party's intellectual
2055682Smarkm * property rights.
21233294Sstas *
22233294Sstas * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23233294Sstas * copy of the source code appearing in this file ("Covered Code") an
24233294Sstas * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25233294Sstas * base code distributed originally by Intel ("Original Intel Code") to copy,
26233294Sstas * make derivatives, distribute, use and display any portion of the Covered
27233294Sstas * Code in any form, with the right to sublicense such rights; and
28233294Sstas *
29233294Sstas * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30233294Sstas * license (with the right to sublicense), under only those claims of Intel
31233294Sstas * patents that are infringed by the Original Intel Code, to make, use, sell,
3255682Smarkm * offer to sell, and import the Covered Code and derivative works thereof
3355682Smarkm * solely to the minimum extent necessary to exercise the above copyright
3455682Smarkm * license, and in no event shall the patent license extend to any additions
3555682Smarkm * to or modifications of the Original Intel Code.  No other license or right
3655682Smarkm * is granted directly or by implication, estoppel or otherwise;
3755682Smarkm *
3855682Smarkm * The above copyright and patent license is granted only if the following
3955682Smarkm * conditions are met:
4055682Smarkm *
4155682Smarkm * 3. Conditions
4255682Smarkm *
43233294Sstas * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44233294Sstas * Redistribution of source code of any substantial portion of the Covered
45233294Sstas * Code or modification with rights to further distribute source must include
46233294Sstas * the above Copyright Notice, the above License, this list of Conditions,
47233294Sstas * and the following Disclaimer and Export Compliance provision.  In addition,
48233294Sstas * Licensee must cause all Covered Code to which Licensee contributes to
49233294Sstas * contain a file documenting the changes Licensee made to create that Covered
5072445Sassar * Code and the date of any change.  Licensee must include in that file the
5172445Sassar * documentation of any changes made by any predecessor Licensee.  Licensee
52233294Sstas * must include a prominent statement that the modification is derived,
5372445Sassar * directly or indirectly, from Original Intel Code.
5472445Sassar *
55233294Sstas * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
5655682Smarkm * Redistribution of source code of any substantial portion of the Covered
5755682Smarkm * Code or modification without rights to further distribute source must
5855682Smarkm * include the following Disclaimer and Export Compliance provision in the
5955682Smarkm * documentation and/or other materials provided with distribution.  In
6072445Sassar * addition, Licensee may not authorize further sublicense of source of any
6155682Smarkm * portion of the Covered Code, and must include terms to the effect that the
6290926Snectar * license from Licensee to its licensee is limited to the intellectual
63233294Sstas * property embodied in the software Licensee provides to its licensee, and
6455682Smarkm * not to intellectual property embodied in modifications its licensee may
6590926Snectar * make.
6655682Smarkm *
67233294Sstas * 3.3. Redistribution of Executable. Redistribution in executable form of any
68233294Sstas * substantial portion of the Covered Code or modification must reproduce the
6955682Smarkm * above Copyright Notice, and the following Disclaimer and Export Compliance
7055682Smarkm * provision in the documentation and/or other materials provided with the
7155682Smarkm * distribution.
7255682Smarkm *
7355682Smarkm * 3.4. Intel retains all right, title, and interest in and to the Original
7455682Smarkm * Intel Code.
75233294Sstas *
7655682Smarkm * 3.5. Neither the name Intel nor any other trademark owned or controlled by
7755682Smarkm * Intel shall be used in advertising or otherwise to promote the sale, use or
7855682Smarkm * other dealings in products derived from or relating to the Covered Code
7955682Smarkm * without prior written authorization from Intel.
8055682Smarkm *
81233294Sstas * 4. Disclaimer and Export Compliance
82233294Sstas *
83233294Sstas * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84233294Sstas * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85233294Sstas * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
8655682Smarkm * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87233294Sstas * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89233294Sstas * PARTICULAR PURPOSE.
90233294Sstas *
9155682Smarkm * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
9255682Smarkm * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
9355682Smarkm * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94233294Sstas * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
9555682Smarkm * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
9655682Smarkm * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
9755682Smarkm * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
9855682Smarkm * LIMITED REMEDY.
9955682Smarkm *
10055682Smarkm * 4.3. Licensee shall not export, either directly or indirectly, any of this
10155682Smarkm * software or system incorporating such software without first obtaining any
102233294Sstas * required license or other approval from the U. S. Department of Commerce or
10355682Smarkm * any other agency or department of the United States Government.  In the
10455682Smarkm * event Licensee exports any such software from the United States or
10555682Smarkm * re-exports any such software from a foreign destination, Licensee shall
10672445Sassar * ensure that the distribution and export/re-export of the software is in
10772445Sassar * compliance with all laws, regulations, orders, or other restrictions of the
10872445Sassar * U.S. Export Administration Regulations. Licensee agrees that neither it nor
10955682Smarkm * any of its subsidiaries will export/re-export any technical data, process,
11055682Smarkm * software, or service, directly or indirectly, to any country for which the
11155682Smarkm * United States government or any agency thereof requires an export license,
11255682Smarkm * other governmental approval, or letter of assurance, without first obtaining
113233294Sstas * such license, approval or letter.
114233294Sstas *
115233294Sstas *****************************************************************************/
116233294Sstas
11755682Smarkm
11855682Smarkm#include <contrib/dev/acpica/compiler/aslcompiler.h>
11955682Smarkm#include "aslcompiler.y.h"
12072445Sassar
121233294Sstas#define _COMPONENT          ACPI_COMPILER
12255682Smarkm        ACPI_MODULE_NAME    ("asltransform")
12355682Smarkm
12455682Smarkm/* Local prototypes */
12555682Smarkm
12655682Smarkmstatic void
12755682SmarkmTrTransformSubtree (
12872445Sassar    ACPI_PARSE_OBJECT       *Op);
12972445Sassar
13072445Sassarstatic char *
13172445SassarTrAmlGetNextTempName (
13272445Sassar    ACPI_PARSE_OBJECT       *Op,
133233294Sstas    UINT8                   *TempCount);
13472445Sassar
135233294Sstasstatic void
136233294SstasTrAmlInitLineNumbers (
13772445Sassar    ACPI_PARSE_OBJECT       *Op,
13872445Sassar    ACPI_PARSE_OBJECT       *Neighbor);
139233294Sstas
140233294Sstasstatic void
141233294SstasTrAmlInitNode (
14272445Sassar    ACPI_PARSE_OBJECT       *Op,
14372445Sassar    UINT16                  ParseOpcode);
14472445Sassar
145233294Sstasstatic void
146233294SstasTrAmlSetSubtreeParent (
147233294Sstas    ACPI_PARSE_OBJECT       *Op,
148233294Sstas    ACPI_PARSE_OBJECT       *Parent);
149233294Sstas
150233294Sstasstatic void
151233294SstasTrAmlInsertPeer (
152233294Sstas    ACPI_PARSE_OBJECT       *Op,
153233294Sstas    ACPI_PARSE_OBJECT       *NewPeer);
154233294Sstas
155233294Sstasstatic void
156233294SstasTrDoDefinitionBlock (
157233294Sstas    ACPI_PARSE_OBJECT       *Op);
15872445Sassar
15972445Sassarstatic void
16072445SassarTrDoSwitch (
161233294Sstas    ACPI_PARSE_OBJECT       *StartNode);
16272445Sassar
163233294Sstas
164233294Sstas/*******************************************************************************
16572445Sassar *
16672445Sassar * FUNCTION:    TrAmlGetNextTempName
16772445Sassar *
16872445Sassar * PARAMETERS:  Op              - Current parse op
16972445Sassar *              TempCount       - Current temporary counter. Was originally
17072445Sassar *                                per-module; Currently per method, could be
17172445Sassar *                                expanded to per-scope.
172233294Sstas *
17355682Smarkm * RETURN:      A pointer to name (allocated here).
17455682Smarkm *
17555682Smarkm * DESCRIPTION: Generate an ACPI name of the form _T_x.  These names are
17655682Smarkm *              reserved for use by the ASL compiler. (_T_0 through _T_Z)
17755682Smarkm *
17855682Smarkm ******************************************************************************/
17955682Smarkm
180178825Sdfrstatic char *
18155682SmarkmTrAmlGetNextTempName (
18255682Smarkm    ACPI_PARSE_OBJECT       *Op,
18372445Sassar    UINT8                   *TempCount)
18472445Sassar{
185233294Sstas    char                    *TempName;
186233294Sstas
187233294Sstas
18855682Smarkm    if (*TempCount >= (10+26))  /* 0-35 valid: 0-9 and A-Z for TempName[3] */
189178825Sdfr    {
190178825Sdfr        /* Too many temps */
191233294Sstas
192233294Sstas        AslError (ASL_ERROR, ASL_MSG_TOO_MANY_TEMPS, Op, NULL);
193233294Sstas        return (NULL);
194233294Sstas    }
195233294Sstas
196233294Sstas    TempName = UtLocalCalloc (5);
197233294Sstas
19872445Sassar    if (*TempCount < 10)    /* 0-9 */
19972445Sassar    {
20055682Smarkm        TempName[3] = (char) (*TempCount + '0');
201233294Sstas    }
20272445Sassar    else                    /* 10-35: A-Z */
20372445Sassar    {
204178825Sdfr        TempName[3] = (char) (*TempCount + ('A' - 10));
205233294Sstas    }
20672445Sassar    (*TempCount)++;
207233294Sstas
208178825Sdfr    /* First three characters are always "_T_" */
20972445Sassar
210178825Sdfr    TempName[0] = '_';
211233294Sstas    TempName[1] = 'T';
21272445Sassar    TempName[2] = '_';
21372445Sassar
214233294Sstas    return (TempName);
215233294Sstas}
216233294Sstas
217233294Sstas
218233294Sstas/*******************************************************************************
219178825Sdfr *
220178825Sdfr * FUNCTION:    TrAmlInitLineNumbers
221178825Sdfr *
222178825Sdfr * PARAMETERS:  Op              - Op to be initialized
223178825Sdfr *              Neighbor        - Op used for initialization values
224178825Sdfr *
225233294Sstas * RETURN:      None
22655682Smarkm *
227178825Sdfr * DESCRIPTION: Initialized the various line numbers for a parse node.
228178825Sdfr *
22955682Smarkm ******************************************************************************/
23055682Smarkm
231178825Sdfrstatic void
232178825SdfrTrAmlInitLineNumbers (
23355682Smarkm    ACPI_PARSE_OBJECT       *Op,
234178825Sdfr    ACPI_PARSE_OBJECT       *Neighbor)
235178825Sdfr{
23655682Smarkm
237178825Sdfr    Op->Asl.EndLine           = Neighbor->Asl.EndLine;
238233294Sstas    Op->Asl.EndLogicalLine    = Neighbor->Asl.EndLogicalLine;
239233294Sstas    Op->Asl.LineNumber        = Neighbor->Asl.LineNumber;
24055682Smarkm    Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
24155682Smarkm    Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
24255682Smarkm}
24355682Smarkm
24455682Smarkm
24555682Smarkm/*******************************************************************************
246233294Sstas *
247178825Sdfr * FUNCTION:    TrAmlInitNode
248178825Sdfr *
249233294Sstas * PARAMETERS:  Op              - Op to be initialized
250233294Sstas *              ParseOpcode     - Opcode for this node
251233294Sstas *
25255682Smarkm * RETURN:      None
25355682Smarkm *
25455682Smarkm * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
255233294Sstas *
256233294Sstas ******************************************************************************/
257233294Sstas
258233294Sstasstatic void
259233294SstasTrAmlInitNode (
260233294Sstas    ACPI_PARSE_OBJECT       *Op,
261233294Sstas    UINT16                  ParseOpcode)
262233294Sstas{
263233294Sstas
264233294Sstas    Op->Asl.ParseOpcode = ParseOpcode;
265233294Sstas    UtSetParseOpName (Op);
266233294Sstas}
267233294Sstas
268233294Sstas
269233294Sstas/*******************************************************************************
270233294Sstas *
271233294Sstas * FUNCTION:    TrAmlSetSubtreeParent
272233294Sstas *
273233294Sstas * PARAMETERS:  Op              - First node in a list of peer nodes
274233294Sstas *              Parent          - Parent of the subtree
275233294Sstas *
276233294Sstas * RETURN:      None
277233294Sstas *
278233294Sstas * DESCRIPTION: Set the parent for all peer nodes in a subtree
279233294Sstas *
280233294Sstas ******************************************************************************/
281233294Sstas
282233294Sstasstatic void
283233294SstasTrAmlSetSubtreeParent (
284233294Sstas    ACPI_PARSE_OBJECT       *Op,
285233294Sstas    ACPI_PARSE_OBJECT       *Parent)
286233294Sstas{
287233294Sstas    ACPI_PARSE_OBJECT       *Next;
288233294Sstas
289233294Sstas
290233294Sstas    Next = Op;
291233294Sstas    while (Next)
292233294Sstas    {
293233294Sstas        Next->Asl.Parent = Parent;
294233294Sstas        Next             = Next->Asl.Next;
295233294Sstas    }
296233294Sstas}
297233294Sstas
298233294Sstas
299233294Sstas/*******************************************************************************
300233294Sstas *
301233294Sstas * FUNCTION:    TrAmlInsertPeer
302233294Sstas *
303233294Sstas * PARAMETERS:  Op              - First node in a list of peer nodes
304233294Sstas *              NewPeer         - Peer node to insert
305233294Sstas *
306233294Sstas * RETURN:      None
307233294Sstas *
308233294Sstas * DESCRIPTION: Insert a new peer node into a list of peers.
309233294Sstas *
310233294Sstas ******************************************************************************/
311233294Sstas
312233294Sstasstatic void
313233294SstasTrAmlInsertPeer (
314233294Sstas    ACPI_PARSE_OBJECT       *Op,
315233294Sstas    ACPI_PARSE_OBJECT       *NewPeer)
316233294Sstas{
317233294Sstas
318233294Sstas    NewPeer->Asl.Next = Op->Asl.Next;
319233294Sstas    Op->Asl.Next      = NewPeer;
320233294Sstas}
321233294Sstas
322233294Sstas
323233294Sstas/*******************************************************************************
324233294Sstas *
325233294Sstas * FUNCTION:    TrAmlTransformWalk
326233294Sstas *
327233294Sstas * PARAMETERS:  ASL_WALK_CALLBACK
328233294Sstas *
329233294Sstas * RETURN:      None
330233294Sstas *
331233294Sstas * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
332233294Sstas *              operands.
333233294Sstas *
334233294Sstas ******************************************************************************/
335233294Sstas
336233294SstasACPI_STATUS
337233294SstasTrAmlTransformWalk (
338233294Sstas    ACPI_PARSE_OBJECT       *Op,
339233294Sstas    UINT32                  Level,
340233294Sstas    void                    *Context)
341233294Sstas{
342233294Sstas
343233294Sstas    TrTransformSubtree (Op);
344233294Sstas    return (AE_OK);
345233294Sstas}
346233294Sstas
347233294Sstas
348233294Sstas/*******************************************************************************
349233294Sstas *
350233294Sstas * FUNCTION:    TrTransformSubtree
351233294Sstas *
352233294Sstas * PARAMETERS:  Op        - The parent parse node
353233294Sstas *
354233294Sstas * RETURN:      None
355233294Sstas *
356233294Sstas * DESCRIPTION: Prepare nodes to be output as AML data and operands.  The more
357233294Sstas *              complex AML opcodes require processing of the child nodes
358233294Sstas *              (arguments/operands).
359233294Sstas *
360233294Sstas ******************************************************************************/
361233294Sstas
362233294Sstasstatic void
363233294SstasTrTransformSubtree (
364233294Sstas    ACPI_PARSE_OBJECT           *Op)
365233294Sstas{
366233294Sstas
367233294Sstas    if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
368233294Sstas    {
369233294Sstas        return;
370233294Sstas    }
371233294Sstas
372233294Sstas    switch (Op->Asl.ParseOpcode)
373233294Sstas    {
374233294Sstas    case PARSEOP_DEFINITIONBLOCK:
375233294Sstas        TrDoDefinitionBlock (Op);
376233294Sstas        break;
377233294Sstas
378233294Sstas    case PARSEOP_SWITCH:
379233294Sstas        TrDoSwitch (Op);
380233294Sstas        break;
381233294Sstas
382233294Sstas    case PARSEOP_METHOD:
383233294Sstas
384233294Sstas        /*
385233294Sstas         * TBD: Zero the tempname (_T_x) count. Probably shouldn't be a global,
386233294Sstas         * however
387233294Sstas         */
388233294Sstas        Gbl_TempCount = 0;
389233294Sstas        break;
390233294Sstas
391233294Sstas    default:
392233294Sstas        /* Nothing to do here for other opcodes */
393233294Sstas        break;
394233294Sstas    }
395233294Sstas}
396233294Sstas
397233294Sstas
398233294Sstas/*******************************************************************************
399233294Sstas *
400233294Sstas * FUNCTION:    TrDoDefinitionBlock
401233294Sstas *
402233294Sstas * PARAMETERS:  Op        - Parse node
403233294Sstas *
404233294Sstas * RETURN:      None
405233294Sstas *
406233294Sstas * DESCRIPTION: Find the end of the definition block and set a global to this
407233294Sstas *              node.  It is used by the compiler to insert compiler-generated
408233294Sstas *              names at the root level of the namespace.
409233294Sstas *
410233294Sstas ******************************************************************************/
411233294Sstas
41255682Smarkmstatic void
41355682SmarkmTrDoDefinitionBlock (
41455682Smarkm    ACPI_PARSE_OBJECT       *Op)
41555682Smarkm{
41655682Smarkm    ACPI_PARSE_OBJECT       *Next;
417233294Sstas    UINT32                  i;
41855682Smarkm
419233294Sstas
420233294Sstas    Next = Op->Asl.Child;
421233294Sstas    for (i = 0; i < 5; i++)
42255682Smarkm    {
42355682Smarkm        Next = Next->Asl.Next;
42455682Smarkm        if (i == 0)
425        {
426            /*
427             * This is the table signature. Only the DSDT can be assumed
428             * to be at the root of the namespace;  Therefore, namepath
429             * optimization can only be performed on the DSDT.
430             */
431            if (!ACPI_COMPARE_NAME (Next->Asl.Value.String, ACPI_SIG_DSDT))
432            {
433                Gbl_ReferenceOptimizationFlag = FALSE;
434            }
435        }
436    }
437
438    Gbl_FirstLevelInsertionNode = Next;
439}
440
441
442/*******************************************************************************
443 *
444 * FUNCTION:    TrDoSwitch
445 *
446 * PARAMETERS:  StartNode        - Parse node for SWITCH
447 *
448 * RETURN:      None
449 *
450 *
451 * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs.  There is
452 *              no actual AML opcode for SWITCH -- it must be simulated.
453 *
454 ******************************************************************************/
455
456static void
457TrDoSwitch (
458    ACPI_PARSE_OBJECT       *StartNode)
459{
460    ACPI_PARSE_OBJECT       *Next;
461    ACPI_PARSE_OBJECT       *CaseOp = NULL;
462    ACPI_PARSE_OBJECT       *CaseBlock = NULL;
463    ACPI_PARSE_OBJECT       *DefaultOp = NULL;
464    ACPI_PARSE_OBJECT       *CurrentParentNode;
465    ACPI_PARSE_OBJECT       *Conditional = NULL;
466    ACPI_PARSE_OBJECT       *Predicate;
467    ACPI_PARSE_OBJECT       *Peer;
468    ACPI_PARSE_OBJECT       *NewOp;
469    ACPI_PARSE_OBJECT       *NewOp2;
470    ACPI_PARSE_OBJECT       *MethodOp;
471    ACPI_PARSE_OBJECT       *StoreOp;
472    ACPI_PARSE_OBJECT       *BreakOp;
473    char                    *PredicateValueName;
474    UINT16                  Index;
475    UINT32                  Btype;
476
477
478    /* Start node is the Switch() node */
479
480    CurrentParentNode  = StartNode;
481
482    /* Create a new temp name of the form _T_x */
483
484    PredicateValueName = TrAmlGetNextTempName (StartNode, &Gbl_TempCount);
485    if (!PredicateValueName)
486    {
487        return;
488    }
489
490    /* First child is the Switch() predicate */
491
492    Next = StartNode->Asl.Child;
493
494    /*
495     * Examine the return type of the Switch Value -
496     * must be Integer/Buffer/String
497     */
498    Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
499    Btype = AslKeywordMapping[Index].AcpiBtype;
500    if ((Btype != ACPI_BTYPE_INTEGER) &&
501        (Btype != ACPI_BTYPE_STRING)  &&
502        (Btype != ACPI_BTYPE_BUFFER))
503    {
504        AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
505        Btype = ACPI_BTYPE_INTEGER;
506    }
507
508    /* CASE statements start at next child */
509
510    Peer = Next->Asl.Next;
511    while (Peer)
512    {
513        Next = Peer;
514        Peer = Next->Asl.Next;
515
516        if (Next->Asl.ParseOpcode == PARSEOP_CASE)
517        {
518            if (CaseOp)
519            {
520                /* Add an ELSE to complete the previous CASE */
521
522                if (!Conditional)
523                {
524                    return;
525                }
526                NewOp             = TrCreateLeafNode (PARSEOP_ELSE);
527                NewOp->Asl.Parent = Conditional->Asl.Parent;
528                TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
529
530                /* Link ELSE node as a peer to the previous IF */
531
532                TrAmlInsertPeer (Conditional, NewOp);
533                CurrentParentNode = NewOp;
534            }
535
536            CaseOp      = Next;
537            Conditional = CaseOp;
538            CaseBlock   = CaseOp->Asl.Child->Asl.Next;
539            Conditional->Asl.Child->Asl.Next = NULL;
540            Predicate = CaseOp->Asl.Child;
541
542            if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
543                (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
544            {
545                /*
546                 * Convert the package declaration to this form:
547                 *
548                 * If (LNotEqual (Match (Package(<size>){<data>},
549                 *                       MEQ, _T_x, MTR, Zero, Zero), Ones))
550                 */
551                NewOp2              = TrCreateLeafNode (PARSEOP_MATCHTYPE_MEQ);
552                Predicate->Asl.Next = NewOp2;
553                TrAmlInitLineNumbers (NewOp2, Conditional);
554
555                NewOp               = NewOp2;
556                NewOp2              = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
557                                        (UINT64) ACPI_TO_INTEGER (PredicateValueName));
558                NewOp->Asl.Next     = NewOp2;
559                TrAmlInitLineNumbers (NewOp2, Predicate);
560
561                NewOp               = NewOp2;
562                NewOp2              = TrCreateLeafNode (PARSEOP_MATCHTYPE_MTR);
563                NewOp->Asl.Next     = NewOp2;
564                TrAmlInitLineNumbers (NewOp2, Predicate);
565
566                NewOp               = NewOp2;
567                NewOp2              = TrCreateLeafNode (PARSEOP_ZERO);
568                NewOp->Asl.Next     = NewOp2;
569                TrAmlInitLineNumbers (NewOp2, Predicate);
570
571                NewOp               = NewOp2;
572                NewOp2              = TrCreateLeafNode (PARSEOP_ZERO);
573                NewOp->Asl.Next     = NewOp2;
574                TrAmlInitLineNumbers (NewOp2, Predicate);
575
576                NewOp2              = TrCreateLeafNode (PARSEOP_MATCH);
577                NewOp2->Asl.Child   = Predicate;  /* PARSEOP_PACKAGE */
578                TrAmlInitLineNumbers (NewOp2, Conditional);
579                TrAmlSetSubtreeParent (Predicate, NewOp2);
580
581                NewOp               = NewOp2;
582                NewOp2              = TrCreateLeafNode (PARSEOP_ONES);
583                NewOp->Asl.Next     = NewOp2;
584                TrAmlInitLineNumbers (NewOp2, Conditional);
585
586                NewOp2              = TrCreateLeafNode (PARSEOP_LEQUAL);
587                NewOp2->Asl.Child   = NewOp;
588                NewOp->Asl.Parent   = NewOp2;
589                TrAmlInitLineNumbers (NewOp2, Conditional);
590                TrAmlSetSubtreeParent (NewOp, NewOp2);
591
592                NewOp               = NewOp2;
593                NewOp2              = TrCreateLeafNode (PARSEOP_LNOT);
594                NewOp2->Asl.Child   = NewOp;
595                NewOp2->Asl.Parent  = Conditional;
596                NewOp->Asl.Parent   = NewOp2;
597                TrAmlInitLineNumbers (NewOp2, Conditional);
598
599                Conditional->Asl.Child = NewOp2;
600                NewOp2->Asl.Next = CaseBlock;
601            }
602            else
603            {
604                /*
605                 * Integer and Buffer case.
606                 *
607                 * Change CaseOp() to:  If (LEqual (SwitchValue, CaseValue)) {...}
608                 * Note: SwitchValue is first to allow the CaseValue to be implicitly
609                 * converted to the type of SwitchValue if necessary.
610                 *
611                 * CaseOp->Child is the case value
612                 * CaseOp->Child->Peer is the beginning of the case block
613                 */
614                NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
615                            (UINT64) ACPI_TO_INTEGER (PredicateValueName));
616                NewOp->Asl.Next = Predicate;
617                TrAmlInitLineNumbers (NewOp, Predicate);
618
619                NewOp2              = TrCreateLeafNode (PARSEOP_LEQUAL);
620                NewOp2->Asl.Parent  = Conditional;
621                NewOp2->Asl.Child   = NewOp;
622                TrAmlInitLineNumbers (NewOp2, Conditional);
623
624                TrAmlSetSubtreeParent (NewOp, NewOp2);
625
626                Predicate           = NewOp2;
627                Predicate->Asl.Next = CaseBlock;
628
629                TrAmlSetSubtreeParent (Predicate, Conditional);
630                Conditional->Asl.Child = Predicate;
631            }
632
633            /* Reinitialize the CASE node to an IF node */
634
635            TrAmlInitNode (Conditional, PARSEOP_IF);
636
637            /*
638             * The first CASE(IF) is not nested under an ELSE.
639             * All other CASEs are children of a parent ELSE.
640             */
641            if (CurrentParentNode == StartNode)
642            {
643                Conditional->Asl.Next = NULL;
644            }
645            else
646            {
647                /*
648                 * The IF is a child of previous IF/ELSE.  It
649                 * is therefore without peer.
650                 */
651                CurrentParentNode->Asl.Child = Conditional;
652                Conditional->Asl.Parent      = CurrentParentNode;
653                Conditional->Asl.Next        = NULL;
654            }
655        }
656        else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
657        {
658            if (DefaultOp)
659            {
660                /*
661                 * More than one Default
662                 * (Parser does not catch this, must check here)
663                 */
664                AslError (ASL_ERROR, ASL_MSG_MULTIPLE_DEFAULT, Next, NULL);
665            }
666            else
667            {
668                /* Save the DEFAULT node for later, after CASEs */
669
670                DefaultOp = Next;
671            }
672        }
673        else
674        {
675            /* Unknown peer opcode */
676
677            AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%u)\n",
678                        Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
679        }
680    }
681
682    /* Add the default case at the end of the if/else construct */
683
684    if (DefaultOp)
685    {
686        /* If no CASE statements, this is an error - see below */
687
688        if (CaseOp)
689        {
690            /* Convert the DEFAULT node to an ELSE */
691
692            if (!Conditional)
693            {
694                return;
695            }
696
697            TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
698            DefaultOp->Asl.Parent = Conditional->Asl.Parent;
699
700            /* Link ELSE node as a peer to the previous IF */
701
702            TrAmlInsertPeer (Conditional, DefaultOp);
703        }
704    }
705
706    if (!CaseOp)
707    {
708        AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
709    }
710
711
712    /*
713     * Create a Name(_T_x, ...) statement. This statement must appear at the
714     * method level, in case a loop surrounds the switch statement and could
715     * cause the name to be created twice (error).
716     */
717
718    /* Create the Name node */
719
720    Predicate = StartNode->Asl.Child;
721    NewOp = TrCreateLeafNode (PARSEOP_NAME);
722
723    /* Find the parent method */
724
725    Next = StartNode;
726    while ((Next->Asl.ParseOpcode != PARSEOP_METHOD) &&
727           (Next->Asl.ParseOpcode != PARSEOP_DEFINITIONBLOCK))
728    {
729        Next = Next->Asl.Parent;
730    }
731    MethodOp = Next;
732
733    NewOp->Asl.CompileFlags |= NODE_COMPILER_EMITTED;
734    NewOp->Asl.Parent = Next;
735
736    /* Insert name after the method name and arguments */
737
738    Next = Next->Asl.Child; /* Name */
739    Next = Next->Asl.Next;  /* NumArgs */
740    Next = Next->Asl.Next;  /* SerializeRule */
741
742    /*
743     * If method is not Serialized, we must make is so, because of the way
744     * that Switch() must be implemented -- we cannot allow multiple threads
745     * to execute this method concurrently since we need to create local
746     * temporary name(s).
747     */
748    if (Next->Asl.ParseOpcode != PARSEOP_SERIALIZERULE_SERIAL)
749    {
750        AslError (ASL_REMARK, ASL_MSG_SERIALIZED, MethodOp, "Due to use of Switch operator");
751        Next->Asl.ParseOpcode = PARSEOP_SERIALIZERULE_SERIAL;
752    }
753
754    Next = Next->Asl.Next;  /* SyncLevel */
755    Next = Next->Asl.Next;  /* ReturnType */
756    Next = Next->Asl.Next;  /* ParameterTypes */
757
758    TrAmlInsertPeer (Next, NewOp);
759    TrAmlInitLineNumbers (NewOp, Next);
760
761    /* Create the NameSeg child for the Name node */
762
763    NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
764                (UINT64) ACPI_TO_INTEGER (PredicateValueName));
765    NewOp2->Asl.CompileFlags |= NODE_IS_NAME_DECLARATION;
766    NewOp->Asl.Child  = NewOp2;
767
768    /* Create the initial value for the Name. Btype was already validated above */
769
770    switch (Btype)
771    {
772    case ACPI_BTYPE_INTEGER:
773        NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_ZERO,
774                                (UINT64) 0);
775        break;
776
777    case ACPI_BTYPE_STRING:
778        NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_STRING_LITERAL,
779                                (UINT64) ACPI_TO_INTEGER (""));
780        break;
781
782    case ACPI_BTYPE_BUFFER:
783        (void) TrLinkPeerNode (NewOp2, TrCreateValuedLeafNode (PARSEOP_BUFFER,
784                                    (UINT64) 0));
785        Next = NewOp2->Asl.Next;
786        (void) TrLinkChildren (Next, 1, TrCreateValuedLeafNode (PARSEOP_ZERO,
787                                    (UINT64) 1));
788        (void) TrLinkPeerNode (Next->Asl.Child,
789            TrCreateValuedLeafNode (PARSEOP_DEFAULT_ARG, (UINT64) 0));
790
791        TrAmlSetSubtreeParent (Next->Asl.Child, Next);
792        break;
793
794    default:
795        break;
796    }
797
798    TrAmlSetSubtreeParent (NewOp2, NewOp);
799
800    /*
801     * Transform the Switch() into a While(One)-Break node.
802     * And create a Store() node which will be used to save the
803     * Switch() value.  The store is of the form: Store (Value, _T_x)
804     * where _T_x is the temp variable.
805     */
806    TrAmlInitNode (StartNode, PARSEOP_WHILE);
807    NewOp = TrCreateLeafNode (PARSEOP_ONE);
808    NewOp->Asl.Next = Predicate->Asl.Next;
809    NewOp->Asl.Parent = StartNode;
810    StartNode->Asl.Child = NewOp;
811
812    /* Create a Store() node */
813
814    StoreOp = TrCreateLeafNode (PARSEOP_STORE);
815    StoreOp->Asl.Parent = StartNode;
816    TrAmlInsertPeer (NewOp, StoreOp);
817
818    /* Complete the Store subtree */
819
820    StoreOp->Asl.Child = Predicate;
821    Predicate->Asl.Parent = StoreOp;
822
823    NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
824                (UINT64) ACPI_TO_INTEGER (PredicateValueName));
825    NewOp->Asl.Parent    = StoreOp;
826    Predicate->Asl.Next  = NewOp;
827
828    /* Create a Break() node and insert it into the end of While() */
829
830    Conditional = StartNode->Asl.Child;
831    while (Conditional->Asl.Next)
832    {
833        Conditional = Conditional->Asl.Next;
834    }
835
836    BreakOp = TrCreateLeafNode (PARSEOP_BREAK);
837    BreakOp->Asl.Parent = StartNode;
838    TrAmlInsertPeer (Conditional, BreakOp);
839}
840
841
842