2 /******************************************************************************
4 * Module Name: asltransform - Parse tree transforms
7 *****************************************************************************/
9 /******************************************************************************
13 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
14 * All rights reserved.
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights. You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code. No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
39 * The above copyright and patent license is granted only if the following
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision. In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change. Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee. Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution. In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
74 * 3.4. Intel retains all right, title, and interest in and to the Original
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
82 * 4. Disclaimer and Export Compliance
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government. In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
116 *****************************************************************************/
119 #include "aslcompiler.h"
120 #include "aslcompiler.y.h"
122 #define _COMPONENT ACPI_COMPILER
123 ACPI_MODULE_NAME ("asltransform")
126 /*******************************************************************************
128 * FUNCTION: TrAmlGetNextTempName
130 * PARAMETERS: TempCount - Current temporary counter. Was originally
131 * per-module; Currently per method, could be
132 * expanded to per-scope.
134 * RETURN: A pointer to name (allocated here).
136 * DESCRIPTION: Generate an ACPI name of the form _T_x. These names are
137 * reserved for use by the ASL compiler. (_T_0 through _T_Z)
139 ******************************************************************************/
142 TrAmlGetNextTempName (
143 ACPI_PARSE_OBJECT
*Op
,
149 if (*TempCount
>= (10+26)) /* 0-35 valid: 0-9 and A-Z for TempName[3] */
153 AslError (ASL_ERROR
, ASL_MSG_TOO_MANY_TEMPS
, Op
, NULL
);
157 TempName
= UtLocalCalloc (5);
159 if (*TempCount
< 10) /* 0-9 */
161 TempName
[3] = (char) (*TempCount
+ '0');
163 else /* 10-35: A-Z */
165 TempName
[3] = (char) (*TempCount
+ ('A' - 10));
169 /* First three characters are always "_T_" */
179 /*******************************************************************************
181 * FUNCTION: TrAmlInitLineNumbers
183 * PARAMETERS: Op - Op to be initialized
184 * Neighbor - Op used for initialization values
188 * DESCRIPTION: Initialized the various line numbers for a parse node.
190 ******************************************************************************/
193 TrAmlInitLineNumbers (
194 ACPI_PARSE_OBJECT
*Op
,
195 ACPI_PARSE_OBJECT
*Neighbor
)
198 Op
->Asl
.EndLine
= Neighbor
->Asl
.EndLine
;
199 Op
->Asl
.EndLogicalLine
= Neighbor
->Asl
.EndLogicalLine
;
200 Op
->Asl
.LineNumber
= Neighbor
->Asl
.LineNumber
;
201 Op
->Asl
.LogicalByteOffset
= Neighbor
->Asl
.LogicalByteOffset
;
202 Op
->Asl
.LogicalLineNumber
= Neighbor
->Asl
.LogicalLineNumber
;
206 /*******************************************************************************
208 * FUNCTION: TrAmlInitNode
210 * PARAMETERS: Op - Op to be initialized
211 * ParseOpcode - Opcode for this node
215 * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
217 ******************************************************************************/
221 ACPI_PARSE_OBJECT
*Op
,
225 Op
->Asl
.ParseOpcode
= ParseOpcode
;
226 UtSetParseOpName (Op
);
230 /*******************************************************************************
232 * FUNCTION: TrAmlSetSubtreeParent
234 * PARAMETERS: Op - First node in a list of peer nodes
235 * Parent - Parent of the subtree
239 * DESCRIPTION: Set the parent for all peer nodes in a subtree
241 ******************************************************************************/
244 TrAmlSetSubtreeParent (
245 ACPI_PARSE_OBJECT
*Op
,
246 ACPI_PARSE_OBJECT
*Parent
)
248 ACPI_PARSE_OBJECT
*Next
;
254 Next
->Asl
.Parent
= Parent
;
255 Next
= Next
->Asl
.Next
;
260 /*******************************************************************************
262 * FUNCTION: TrAmlInsertPeer
264 * PARAMETERS: Op - First node in a list of peer nodes
265 * NewPeer - Peer node to insert
269 * DESCRIPTION: Insert a new peer node into a list of peers.
271 ******************************************************************************/
275 ACPI_PARSE_OBJECT
*Op
,
276 ACPI_PARSE_OBJECT
*NewPeer
)
279 NewPeer
->Asl
.Next
= Op
->Asl
.Next
;
280 Op
->Asl
.Next
= NewPeer
;
284 /*******************************************************************************
286 * FUNCTION: TrAmlTransformWalk
288 * PARAMETERS: ASL_WALK_CALLBACK
292 * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
295 ******************************************************************************/
299 ACPI_PARSE_OBJECT
*Op
,
304 TrTransformSubtree (Op
);
309 /*******************************************************************************
311 * FUNCTION: TrTransformSubtree
313 * PARAMETERS: Op - The parent parse node
317 * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
318 * complex AML opcodes require processing of the child nodes
319 * (arguments/operands).
321 ******************************************************************************/
325 ACPI_PARSE_OBJECT
*Op
)
328 if (Op
->Asl
.AmlOpcode
== AML_RAW_DATA_BYTE
)
333 switch (Op
->Asl
.ParseOpcode
)
335 case PARSEOP_DEFINITIONBLOCK
:
336 TrDoDefinitionBlock (Op
);
345 /* Zero the tempname (_T_x) count. Probably shouldn't be a global, however */
351 /* Nothing to do here for other opcodes */
357 /*******************************************************************************
359 * FUNCTION: TrDoDefinitionBlock
361 * PARAMETERS: Op - Parse node
365 * DESCRIPTION: Find the end of the definition block and set a global to this
366 * node. It is used by the compiler to insert compiler-generated
367 * names at the root level of the namespace.
369 ******************************************************************************/
372 TrDoDefinitionBlock (
373 ACPI_PARSE_OBJECT
*Op
)
375 ACPI_PARSE_OBJECT
*Next
;
379 Next
= Op
->Asl
.Child
;
380 for (i
= 0; i
< 5; i
++)
382 Next
= Next
->Asl
.Next
;
386 * This is the table signature. Only the DSDT can be assumed to
387 * be at the root of the namespace; Therefore, namepath optimization
388 * can only be performed on the DSDT.
390 if (ACPI_STRNCMP (Next
->Asl
.Value
.String
, "DSDT", 4))
392 Gbl_ReferenceOptimizationFlag
= FALSE
;
397 Gbl_FirstLevelInsertionNode
= Next
;
401 /*******************************************************************************
403 * FUNCTION: TrDoSwitch
405 * PARAMETERS: StartNode - Parse node for SWITCH
410 * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
411 * no actual AML opcode for SWITCH -- it must be simulated.
413 ******************************************************************************/
417 ACPI_PARSE_OBJECT
*StartNode
)
419 ACPI_PARSE_OBJECT
*Next
;
420 ACPI_PARSE_OBJECT
*CaseOp
= NULL
;
421 ACPI_PARSE_OBJECT
*CaseBlock
= NULL
;
422 ACPI_PARSE_OBJECT
*DefaultOp
= NULL
;
423 ACPI_PARSE_OBJECT
*CurrentParentNode
;
424 ACPI_PARSE_OBJECT
*Conditional
= NULL
;
425 ACPI_PARSE_OBJECT
*Predicate
;
426 ACPI_PARSE_OBJECT
*Peer
;
427 ACPI_PARSE_OBJECT
*NewOp
;
428 ACPI_PARSE_OBJECT
*NewOp2
;
429 char *PredicateValueName
;
434 /* Start node is the Switch() node */
436 CurrentParentNode
= StartNode
;
438 /* Create a new temp name of the form _T_x */
440 PredicateValueName
= TrAmlGetNextTempName (StartNode
, &Gbl_TempCount
);
441 if (!PredicateValueName
)
446 /* First child is the Switch() predicate */
448 Next
= StartNode
->Asl
.Child
;
450 /* Examine the return type of the Switch Value - must be Integer/Buffer/String */
452 Index
= (UINT16
) (Next
->Asl
.ParseOpcode
- ASL_PARSE_OPCODE_BASE
);
453 Btype
= AslKeywordMapping
[Index
].AcpiBtype
;
454 if ((Btype
!= ACPI_BTYPE_INTEGER
) &&
455 (Btype
!= ACPI_BTYPE_STRING
) &&
456 (Btype
!= ACPI_BTYPE_BUFFER
))
458 AslError (ASL_WARNING
, ASL_MSG_SWITCH_TYPE
, Next
, NULL
);
459 Btype
= ACPI_BTYPE_INTEGER
;
462 /* CASE statements start at next child */
464 Peer
= Next
->Asl
.Next
;
468 Peer
= Next
->Asl
.Next
;
470 if (Next
->Asl
.ParseOpcode
== PARSEOP_CASE
)
474 /* Add an ELSE to complete the previous CASE */
476 NewOp
= TrCreateLeafNode (PARSEOP_ELSE
);
477 NewOp
->Asl
.Parent
= Conditional
->Asl
.Parent
;
478 TrAmlInitLineNumbers (NewOp
, NewOp
->Asl
.Parent
);
480 /* Link ELSE node as a peer to the previous IF */
482 TrAmlInsertPeer (Conditional
, NewOp
);
483 CurrentParentNode
= NewOp
;
487 Conditional
= CaseOp
;
488 CaseBlock
= CaseOp
->Asl
.Child
->Asl
.Next
;
489 Conditional
->Asl
.Child
->Asl
.Next
= NULL
;
490 Predicate
= CaseOp
->Asl
.Child
;
492 if (Predicate
->Asl
.ParseOpcode
== PARSEOP_PACKAGE
)
495 * Convert the package declaration to this form:
497 * If (LNotEqual (Match (Package(<size>){<data>}, MEQ, _T_x, MTR, Zero, Zero), Ones))
499 NewOp2
= TrCreateLeafNode (PARSEOP_MATCHTYPE_MEQ
);
500 Predicate
->Asl
.Next
= NewOp2
;
501 TrAmlInitLineNumbers (NewOp2
, Conditional
);
504 NewOp2
= TrCreateValuedLeafNode (PARSEOP_NAMESTRING
,
505 (ACPI_INTEGER
) ACPI_TO_INTEGER (PredicateValueName
));
506 NewOp
->Asl
.Next
= NewOp2
;
507 TrAmlInitLineNumbers (NewOp2
, Predicate
);
510 NewOp2
= TrCreateLeafNode (PARSEOP_MATCHTYPE_MTR
);
511 NewOp
->Asl
.Next
= NewOp2
;
512 TrAmlInitLineNumbers (NewOp2
, Predicate
);
515 NewOp2
= TrCreateLeafNode (PARSEOP_ZERO
);
516 NewOp
->Asl
.Next
= NewOp2
;
517 TrAmlInitLineNumbers (NewOp2
, Predicate
);
520 NewOp2
= TrCreateLeafNode (PARSEOP_ZERO
);
521 NewOp
->Asl
.Next
= NewOp2
;
522 TrAmlInitLineNumbers (NewOp2
, Predicate
);
524 NewOp2
= TrCreateLeafNode (PARSEOP_MATCH
);
525 NewOp2
->Asl
.Child
= Predicate
; /* PARSEOP_PACKAGE */
526 TrAmlInitLineNumbers (NewOp2
, Conditional
);
527 TrAmlSetSubtreeParent (Predicate
, NewOp2
);
530 NewOp2
= TrCreateLeafNode (PARSEOP_ONES
);
531 NewOp
->Asl
.Next
= NewOp2
;
532 TrAmlInitLineNumbers (NewOp2
, Conditional
);
534 NewOp2
= TrCreateLeafNode (PARSEOP_LEQUAL
);
535 NewOp2
->Asl
.Child
= NewOp
;
536 NewOp
->Asl
.Parent
= NewOp2
;
537 TrAmlInitLineNumbers (NewOp2
, Conditional
);
538 TrAmlSetSubtreeParent (NewOp
, NewOp2
);
541 NewOp2
= TrCreateLeafNode (PARSEOP_LNOT
);
542 NewOp2
->Asl
.Child
= NewOp
;
543 NewOp2
->Asl
.Parent
= Conditional
;
544 NewOp
->Asl
.Parent
= NewOp2
;
545 TrAmlInitLineNumbers (NewOp2
, Conditional
);
547 Conditional
->Asl
.Child
= NewOp2
;
548 NewOp2
->Asl
.Next
= CaseBlock
;
553 * Integer and Buffer case.
555 * Change CaseOp() to: If (LEqual (SwitchValue, CaseValue)) {...}
556 * Note: SwitchValue is first to allow the CaseValue to be implicitly
557 * converted to the type of SwitchValue if necessary.
559 * CaseOp->Child is the case value
560 * CaseOp->Child->Peer is the beginning of the case block
562 NewOp
= TrCreateValuedLeafNode (PARSEOP_NAMESTRING
,
563 (ACPI_INTEGER
) ACPI_TO_INTEGER (PredicateValueName
));
564 NewOp
->Asl
.Next
= Predicate
;
565 TrAmlInitLineNumbers (NewOp
, Predicate
);
567 NewOp2
= TrCreateLeafNode (PARSEOP_LEQUAL
);
568 NewOp2
->Asl
.Parent
= Conditional
;
569 NewOp2
->Asl
.Child
= NewOp
;
570 TrAmlInitLineNumbers (NewOp2
, Conditional
);
572 TrAmlSetSubtreeParent (NewOp
, NewOp2
);
575 Predicate
->Asl
.Next
= CaseBlock
;
577 TrAmlSetSubtreeParent (Predicate
, Conditional
);
578 Conditional
->Asl
.Child
= Predicate
;
581 /* Reinitialize the CASE node to an IF node */
583 TrAmlInitNode (Conditional
, PARSEOP_IF
);
586 * The first CASE(IF) is not nested under an ELSE.
587 * All other CASEs are children of a parent ELSE.
589 if (CurrentParentNode
== StartNode
)
591 Conditional
->Asl
.Parent
= CurrentParentNode
->Asl
.Parent
;
593 /* Link IF into the peer list */
595 TrAmlInsertPeer (CurrentParentNode
, Conditional
);
600 * The IF is a child of previous IF/ELSE. It
601 * is therefore without peer.
603 CurrentParentNode
->Asl
.Child
= Conditional
;
604 Conditional
->Asl
.Parent
= CurrentParentNode
;
605 Conditional
->Asl
.Next
= NULL
;
608 else if (Next
->Asl
.ParseOpcode
== PARSEOP_DEFAULT
)
613 * More than one Default
614 * (Parser should catch this, should not get here)
616 AslError (ASL_ERROR
, ASL_MSG_COMPILER_INTERNAL
, Next
,
617 "Found more than one Default()");
620 /* Save the DEFAULT node for later, after CASEs */
626 /* Unknown peer opcode */
628 AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%d)\n",
629 Next
->Asl
.ParseOpName
, Next
->Asl
.ParseOpcode
);
633 /* Add the default case at the end of the if/else construct */
637 /* If no CASE statements, this is an error - see below */
641 /* Convert the DEFAULT node to an ELSE */
643 TrAmlInitNode (DefaultOp
, PARSEOP_ELSE
);
644 DefaultOp
->Asl
.Parent
= Conditional
->Asl
.Parent
;
646 /* Link ELSE node as a peer to the previous IF */
648 TrAmlInsertPeer (Conditional
, DefaultOp
);
654 AslError (ASL_ERROR
, ASL_MSG_NO_CASES
, StartNode
, NULL
);
658 * Add a NAME node for the temp integer:
659 * Change the SWITCH node to a Name (_T_x, Type)
661 Predicate
= StartNode
->Asl
.Child
;
662 TrAmlInitNode (StartNode
, PARSEOP_NAME
);
666 NewOp
->Asl
.CompileFlags
|= NODE_COMPILER_EMITTED
;
668 NewOp2
= TrCreateValuedLeafNode (PARSEOP_NAMESEG
,
669 (ACPI_INTEGER
) ACPI_TO_INTEGER (PredicateValueName
));
670 NewOp2
->Asl
.CompileFlags
|= NODE_IS_NAME_DECLARATION
;
671 NewOp
->Asl
.Child
= NewOp2
;
673 /* Btype was already validated above */
677 case ACPI_BTYPE_INTEGER
:
678 NewOp2
->Asl
.Next
= TrCreateValuedLeafNode (PARSEOP_ZERO
, (ACPI_INTEGER
) 0);
681 case ACPI_BTYPE_STRING
:
682 NewOp2
->Asl
.Next
= TrCreateValuedLeafNode (PARSEOP_STRING_LITERAL
, (ACPI_INTEGER
) "");
685 case ACPI_BTYPE_BUFFER
:
686 TrLinkPeerNode (NewOp2
, TrCreateValuedLeafNode (PARSEOP_BUFFER
, (ACPI_INTEGER
) 0));
687 Next
= NewOp2
->Asl
.Next
;
688 TrLinkChildren (Next
, 1, TrCreateValuedLeafNode (PARSEOP_ZERO
, (ACPI_INTEGER
) 1));
689 TrLinkPeerNode (Next
->Asl
.Child
, TrCreateValuedLeafNode (PARSEOP_DEFAULT_ARG
, (ACPI_INTEGER
) 0));
691 TrAmlSetSubtreeParent (Next
->Asl
.Child
, Next
);
698 TrAmlSetSubtreeParent (NewOp2
, NewOp
);
701 * Create and insert a new Store() node which will be used to save the Switch() value.
702 * The store is of the form: Store (Value, _T_x) where _T_x is the temp variable.
704 Next
= TrCreateLeafNode (PARSEOP_STORE
);
705 TrAmlInsertPeer (StartNode
, Next
);
706 TrAmlSetSubtreeParent (Next
, StartNode
->Asl
.Parent
);
708 TrAmlInitLineNumbers (Next
, StartNode
);
709 TrAmlInitLineNumbers (NewOp2
, StartNode
);
710 TrAmlInitLineNumbers (NewOp2
->Asl
.Next
, StartNode
);
712 /* Complete the Store subtree */
714 Next
->Asl
.Child
= Predicate
;
715 Predicate
->Asl
.Parent
= Next
;
717 NewOp
= TrCreateValuedLeafNode (PARSEOP_NAMESEG
,
718 (ACPI_INTEGER
) ACPI_TO_INTEGER (PredicateValueName
));
719 NewOp
->Asl
.Parent
= Next
;
720 Predicate
->Asl
.Next
= NewOp
;