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;
385 Gbl_FirstLevelInsertionNode = Next;
389 /*******************************************************************************
391 * FUNCTION: TrDoSwitch
393 * PARAMETERS: StartNode - Parse node for SWITCH
398 * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
399 * no actual AML opcode for SWITCH -- it must be simulated.
401 ******************************************************************************/
405 ACPI_PARSE_OBJECT *StartNode)
407 ACPI_PARSE_OBJECT *Next;
408 ACPI_PARSE_OBJECT *CaseOp = NULL;
409 ACPI_PARSE_OBJECT *CaseBlock = NULL;
410 ACPI_PARSE_OBJECT *DefaultOp = NULL;
411 ACPI_PARSE_OBJECT *CurrentParentNode;
412 ACPI_PARSE_OBJECT *Conditional = NULL;
413 ACPI_PARSE_OBJECT *Predicate;
414 ACPI_PARSE_OBJECT *Peer;
415 ACPI_PARSE_OBJECT *NewOp;
416 ACPI_PARSE_OBJECT *NewOp2;
417 char *PredicateValueName;
422 /* Start node is the Switch() node */
424 CurrentParentNode = StartNode;
426 /* Create a new temp name of the form _T_x */
428 PredicateValueName = TrAmlGetNextTempName (StartNode, &Gbl_TempCount);
429 if (!PredicateValueName)
434 /* First child is the Switch() predicate */
436 Next = StartNode->Asl.Child;
438 /* Examine the return type of the Switch Value - must be Integer/Buffer/String */
440 Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
441 Btype = AslKeywordMapping[Index].AcpiBtype;
442 if ((Btype != ACPI_BTYPE_INTEGER) &&
443 (Btype != ACPI_BTYPE_STRING) &&
444 (Btype != ACPI_BTYPE_BUFFER))
446 AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
447 Btype = ACPI_BTYPE_INTEGER;
450 /* CASE statements start at next child */
452 Peer = Next->Asl.Next;
456 Peer = Next->Asl.Next;
458 if (Next->Asl.ParseOpcode == PARSEOP_CASE)
462 /* Add an ELSE to complete the previous CASE */
464 NewOp = TrCreateLeafNode (PARSEOP_ELSE);
465 NewOp->Asl.Parent = Conditional->Asl.Parent;
466 TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
468 /* Link ELSE node as a peer to the previous IF */
470 TrAmlInsertPeer (Conditional, NewOp);
471 CurrentParentNode = NewOp;
475 Conditional = CaseOp;
476 CaseBlock = CaseOp->Asl.Child->Asl.Next;
477 Conditional->Asl.Child->Asl.Next = NULL;
478 Predicate = CaseOp->Asl.Child;
480 if (Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE)
483 * Convert the package declaration to this form:
485 * If (LNotEqual (Match (Package(<size>){<data>}, MEQ, _T_x, MTR, Zero, Zero), Ones))
487 NewOp2 = TrCreateLeafNode (PARSEOP_MATCHTYPE_MEQ);
488 Predicate->Asl.Next = NewOp2;
489 TrAmlInitLineNumbers (NewOp2, Conditional);
492 NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
493 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
494 NewOp->Asl.Next = NewOp2;
495 TrAmlInitLineNumbers (NewOp2, Predicate);
498 NewOp2 = TrCreateLeafNode (PARSEOP_MATCHTYPE_MTR);
499 NewOp->Asl.Next = NewOp2;
500 TrAmlInitLineNumbers (NewOp2, Predicate);
503 NewOp2 = TrCreateLeafNode (PARSEOP_ZERO);
504 NewOp->Asl.Next = NewOp2;
505 TrAmlInitLineNumbers (NewOp2, Predicate);
508 NewOp2 = TrCreateLeafNode (PARSEOP_ZERO);
509 NewOp->Asl.Next = NewOp2;
510 TrAmlInitLineNumbers (NewOp2, Predicate);
512 NewOp2 = TrCreateLeafNode (PARSEOP_MATCH);
513 NewOp2->Asl.Child = Predicate; /* PARSEOP_PACKAGE */
514 TrAmlInitLineNumbers (NewOp2, Conditional);
515 TrAmlSetSubtreeParent (Predicate, NewOp2);
518 NewOp2 = TrCreateLeafNode (PARSEOP_ONES);
519 NewOp->Asl.Next = NewOp2;
520 TrAmlInitLineNumbers (NewOp2, Conditional);
522 NewOp2 = TrCreateLeafNode (PARSEOP_LEQUAL);
523 NewOp2->Asl.Child = NewOp;
524 NewOp->Asl.Parent = NewOp2;
525 TrAmlInitLineNumbers (NewOp2, Conditional);
526 TrAmlSetSubtreeParent (NewOp, NewOp2);
529 NewOp2 = TrCreateLeafNode (PARSEOP_LNOT);
530 NewOp2->Asl.Child = NewOp;
531 NewOp2->Asl.Parent = Conditional;
532 NewOp->Asl.Parent = NewOp2;
533 TrAmlInitLineNumbers (NewOp2, Conditional);
535 Conditional->Asl.Child = NewOp2;
536 NewOp2->Asl.Next = CaseBlock;
541 * Integer and Buffer case.
543 * Change CaseOp() to: If (LEqual (SwitchValue, CaseValue)) {...}
544 * Note: SwitchValue is first to allow the CaseValue to be implicitly
545 * converted to the type of SwitchValue if necessary.
547 * CaseOp->Child is the case value
548 * CaseOp->Child->Peer is the beginning of the case block
550 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
551 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
552 NewOp->Asl.Next = Predicate;
553 TrAmlInitLineNumbers (NewOp, Predicate);
555 NewOp2 = TrCreateLeafNode (PARSEOP_LEQUAL);
556 NewOp2->Asl.Parent = Conditional;
557 NewOp2->Asl.Child = NewOp;
558 TrAmlInitLineNumbers (NewOp2, Conditional);
560 TrAmlSetSubtreeParent (NewOp, NewOp2);
563 Predicate->Asl.Next = CaseBlock;
565 TrAmlSetSubtreeParent (Predicate, Conditional);
566 Conditional->Asl.Child = Predicate;
569 /* Reinitialize the CASE node to an IF node */
571 TrAmlInitNode (Conditional, PARSEOP_IF);
574 * The first CASE(IF) is not nested under an ELSE.
575 * All other CASEs are children of a parent ELSE.
577 if (CurrentParentNode == StartNode)
579 Conditional->Asl.Parent = CurrentParentNode->Asl.Parent;
581 /* Link IF into the peer list */
583 TrAmlInsertPeer (CurrentParentNode, Conditional);
588 * The IF is a child of previous IF/ELSE. It
589 * is therefore without peer.
591 CurrentParentNode->Asl.Child = Conditional;
592 Conditional->Asl.Parent = CurrentParentNode;
593 Conditional->Asl.Next = NULL;
596 else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
601 * More than one Default
602 * (Parser should catch this, should not get here)
604 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Next,
605 "Found more than one Default()");
608 /* Save the DEFAULT node for later, after CASEs */
614 /* Unknown peer opcode */
616 AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%d)\n",
617 Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
621 /* Add the default case at the end of the if/else construct */
625 /* If no CASE statements, this is an error - see below */
629 /* Convert the DEFAULT node to an ELSE */
631 TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
632 DefaultOp->Asl.Parent = Conditional->Asl.Parent;
634 /* Link ELSE node as a peer to the previous IF */
636 TrAmlInsertPeer (Conditional, DefaultOp);
642 AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
646 * Add a NAME node for the temp integer:
647 * Change the SWITCH node to a Name (_T_x, Type)
649 Predicate = StartNode->Asl.Child;
650 TrAmlInitNode (StartNode, PARSEOP_NAME);
654 NewOp->Asl.CompileFlags |= NODE_COMPILER_EMITTED;
656 NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
657 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
658 NewOp2->Asl.CompileFlags |= NODE_IS_NAME_DECLARATION;
659 NewOp->Asl.Child = NewOp2;
661 /* Btype was already validated above */
665 case ACPI_BTYPE_INTEGER:
666 NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_ZERO, (ACPI_INTEGER) 0);
669 case ACPI_BTYPE_STRING:
670 NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_STRING_LITERAL, (ACPI_INTEGER) "");
673 case ACPI_BTYPE_BUFFER:
674 TrLinkPeerNode (NewOp2, TrCreateValuedLeafNode (PARSEOP_BUFFER, (ACPI_INTEGER) 0));
675 Next = NewOp2->Asl.Next;
676 TrLinkChildren (Next, 1, TrCreateValuedLeafNode (PARSEOP_ZERO, (ACPI_INTEGER) 1));
677 TrLinkPeerNode (Next->Asl.Child, TrCreateValuedLeafNode (PARSEOP_DEFAULT_ARG, (ACPI_INTEGER) 0));
679 TrAmlSetSubtreeParent (Next->Asl.Child, Next);
686 TrAmlSetSubtreeParent (NewOp2, NewOp);
689 * Create and insert a new Store() node which will be used to save the Switch() value.
690 * The store is of the form: Store (Value, _T_x) where _T_x is the temp variable.
692 Next = TrCreateLeafNode (PARSEOP_STORE);
693 TrAmlInsertPeer (StartNode, Next);
694 TrAmlSetSubtreeParent (Next, StartNode->Asl.Parent);
696 TrAmlInitLineNumbers (Next, StartNode);
697 TrAmlInitLineNumbers (NewOp2, StartNode);
698 TrAmlInitLineNumbers (NewOp2->Asl.Next, StartNode);
700 /* Complete the Store subtree */
702 Next->Asl.Child = Predicate;
703 Predicate->Asl.Parent = Next;
705 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
706 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
707 NewOp->Asl.Parent = Next;
708 Predicate->Asl.Next = NewOp;