2 /******************************************************************************
4 * Module Name: asltransform - Parse tree transforms
7 *****************************************************************************/
9 /******************************************************************************
13 * Some or all of this work - Copyright (c) 1999 - 2003, 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: NamePath - Where a pointer to the temp name is returned
132 * RETURN: A pointer to the second character of the name
134 * DESCRIPTION: Generate an ACPI name of the form _Txx. These names are
135 * reserved for use by the ASL compiler.
137 ******************************************************************************/
140 TrAmlGetNextTempName (
146 if (Gbl_TempCount > 255)
149 /* TBD: issue eror message */
154 TempName = UtLocalCalloc (6);
155 UtConvertByteToHex ((UINT8) Gbl_TempCount, (UINT8 *) &TempName [1]);
158 /* First three characters are always "\_T" */
164 *NamePath = TempName;
165 return (&TempName[1]);
169 /*******************************************************************************
171 * FUNCTION: TrAmlInitLineNumbers
173 * PARAMETERS: Op - Op to be initialized
174 * Neighbor - Op used for initialization values
178 * DESCRIPTION: Initialized the various line numbers for a parse node.
180 ******************************************************************************/
183 TrAmlInitLineNumbers (
184 ACPI_PARSE_OBJECT *Op,
185 ACPI_PARSE_OBJECT *Neighbor)
188 Op->Asl.EndLine = Neighbor->Asl.EndLine;
189 Op->Asl.EndLogicalLine = Neighbor->Asl.EndLogicalLine;
190 Op->Asl.LineNumber = Neighbor->Asl.LineNumber;
191 Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
192 Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
196 /*******************************************************************************
198 * FUNCTION: TrAmlInitNode
200 * PARAMETERS: Op - Op to be initialized
201 * ParseOpcode - Opcode for this node
205 * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
207 ******************************************************************************/
211 ACPI_PARSE_OBJECT *Op,
215 Op->Asl.ParseOpcode = ParseOpcode;
216 UtSetParseOpName (Op);
220 /*******************************************************************************
222 * FUNCTION: TrAmlSetSubtreeParent
224 * PARAMETERS: Op - First node in a list of peer nodes
225 * Parent - Parent of the subtree
229 * DESCRIPTION: Set the parent for all peer nodes in a subtree
231 ******************************************************************************/
234 TrAmlSetSubtreeParent (
235 ACPI_PARSE_OBJECT *Op,
236 ACPI_PARSE_OBJECT *Parent)
238 ACPI_PARSE_OBJECT *Next;
244 Next->Asl.Parent = Parent;
245 Next = Next->Asl.Next;
250 /*******************************************************************************
252 * FUNCTION: TrAmlInsertPeer
254 * PARAMETERS: Op - First node in a list of peer nodes
255 * NewPeer - Peer node to insert
259 * DESCRIPTION: Insert a new peer node into a list of peers.
261 ******************************************************************************/
265 ACPI_PARSE_OBJECT *Op,
266 ACPI_PARSE_OBJECT *NewPeer)
269 NewPeer->Asl.Next = Op->Asl.Next;
270 Op->Asl.Next = NewPeer;
274 /*******************************************************************************
276 * FUNCTION: TrAmlTransformWalk
278 * PARAMETERS: ASL_WALK_CALLBACK
282 * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
285 ******************************************************************************/
289 ACPI_PARSE_OBJECT *Op,
294 TrTransformSubtree (Op);
299 /*******************************************************************************
301 * FUNCTION: TrTransformSubtree
303 * PARAMETERS: Op - The parent parse node
307 * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
308 * complex AML opcodes require processing of the child nodes
309 * (arguments/operands).
311 ******************************************************************************/
315 ACPI_PARSE_OBJECT *Op)
318 if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
323 switch (Op->Asl.ParseOpcode)
325 case PARSEOP_DEFINITIONBLOCK:
326 TrDoDefinitionBlock (Op);
338 /* Nothing to do here for other opcodes */
344 /*******************************************************************************
346 * FUNCTION: TrDoDefinitionBlock
348 * PARAMETERS: Op - Parse node
352 * DESCRIPTION: Find the end of the definition block and set a global to this
353 * node. It is used by the compiler to insert compiler-generated
354 * names at the root level of the namespace.
356 ******************************************************************************/
359 TrDoDefinitionBlock (
360 ACPI_PARSE_OBJECT *Op)
362 ACPI_PARSE_OBJECT *Next;
366 Next = Op->Asl.Child;
367 for (i = 0; i < 5; i++)
369 Next = Next->Asl.Next;
372 Gbl_FirstLevelInsertionNode = Next;
376 /*******************************************************************************
378 * FUNCTION: TrDoElseif
380 * PARAMETERS: Op - Parse node for ELSEIF
384 * DESCRIPTION: Transform an Elseif into an Else and If AML opcode pair.
385 * There is no AML opcode for ELSEIF -- it must be simulated
386 * with an if/else pair.
388 ******************************************************************************/
392 ACPI_PARSE_OBJECT *ElseNode)
394 ACPI_PARSE_OBJECT *IfNode = NULL;
395 ACPI_PARSE_OBJECT *NextNode;
398 /* Change the ELSEIF into an ELSE */
400 TrAmlInitNode (ElseNode, PARSEOP_ELSE);
402 /* Create a new IF node */
404 IfNode = TrCreateLeafNode (PARSEOP_IF);
405 IfNode->Asl.Parent = ElseNode;
406 TrAmlInitLineNumbers (IfNode, ElseNode);
408 /* Insert the the IF node first in the ELSE child list */
410 IfNode->Asl.Child = ElseNode->Asl.Child;
411 ElseNode->Asl.Child = IfNode;
413 /* Go to the end of the IF <Predicate><TermList> block */
415 NextNode = IfNode->Asl.Child; /* Next = Predicate */
416 NextNode = NextNode->Asl.Next; /* Nest = TermList */
418 /* Make the next node after the IF the rest of the original tree */
420 IfNode->Asl.Next = NextNode->Asl.Next;
422 /* Terminate the IF subtree and set IF node as the parent for all nodes */
424 NextNode->Asl.Next = NULL;
425 TrAmlSetSubtreeParent (IfNode->Asl.Child, IfNode);
429 /*******************************************************************************
431 * FUNCTION: TrDoSwitch
433 * PARAMETERS: StartNode - Parse node for SWITCH
438 * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
439 * no actual AML opcode for SWITCH -- it must be simulated.
441 ******************************************************************************/
445 ACPI_PARSE_OBJECT *StartNode)
447 ACPI_PARSE_OBJECT *Next;
448 ACPI_PARSE_OBJECT *CaseOp = NULL;
449 ACPI_PARSE_OBJECT *CaseBlock = NULL;
450 ACPI_PARSE_OBJECT *DefaultOp = NULL;
451 ACPI_PARSE_OBJECT *CurrentParentNode;
452 ACPI_PARSE_OBJECT *Conditional = NULL;
453 ACPI_PARSE_OBJECT *Predicate;
454 ACPI_PARSE_OBJECT *Peer;
455 ACPI_PARSE_OBJECT *NewOp;
456 ACPI_PARSE_OBJECT *NewOp2;
457 char *PredicateValueName;
458 char *PredicateValuePath;
461 CurrentParentNode = StartNode;
462 PredicateValueName = TrAmlGetNextTempName (&PredicateValuePath);
464 /* First child is the predicate */
466 Next = StartNode->Asl.Child;
467 Peer = Next->Asl.Next;
469 /* CASE statements start at next child */
474 Peer = Next->Asl.Next;
476 if (Next->Asl.ParseOpcode == PARSEOP_CASE)
480 /* Add an ELSE to complete the previous CASE */
482 NewOp = TrCreateLeafNode (PARSEOP_ELSE);
483 NewOp->Asl.Parent = Conditional->Asl.Parent;
484 TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
486 /* Link ELSE node as a peer to the previous IF */
488 TrAmlInsertPeer (Conditional, NewOp);
489 CurrentParentNode = NewOp;
493 Conditional = CaseOp;
494 CaseBlock = CaseOp->Asl.Child->Asl.Next;
495 Conditional->Asl.Child->Asl.Next = NULL;
498 * change CaseOp() to: If (PredicateValue == CaseValue) {...}
499 * CaseOp->Child is the case value
500 * CaseOp->Child->Peer is the beginning of the case block
502 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
503 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValuePath));
505 Predicate = CaseOp->Asl.Child;
506 Predicate->Asl.Next = NewOp;
507 TrAmlInitLineNumbers (NewOp, Predicate);
509 NewOp2 = TrCreateLeafNode (PARSEOP_LEQUAL);
510 NewOp2->Asl.Parent = Conditional;
511 NewOp2->Asl.Child = Predicate;
512 TrAmlInitLineNumbers (NewOp2, Conditional);
514 TrAmlSetSubtreeParent (Predicate, NewOp2);
517 Predicate->Asl.Next = CaseBlock;
519 TrAmlSetSubtreeParent (Predicate, Conditional);
521 /* Reinitialize the CASE node to an IF node */
523 Conditional->Asl.Child = Predicate;
524 TrAmlInitNode (Conditional, PARSEOP_IF);
527 * The first CASE(IF) is not nested under an ELSE.
528 * All other CASEs are children of a parent ELSE.
530 if (CurrentParentNode == StartNode)
532 Conditional->Asl.Parent = CurrentParentNode->Asl.Parent;
534 /* Link IF into the peer list */
536 TrAmlInsertPeer (CurrentParentNode, Conditional);
541 * The IF is a child of previous IF/ELSE. It
542 * is therefore without peer.
544 CurrentParentNode->Asl.Child = Conditional;
545 Conditional->Asl.Parent = CurrentParentNode;
546 Conditional->Asl.Next = NULL;
549 else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
553 /* More than one Default */
556 /* Save the DEFAULT node for later, after CASEs */
562 /* Unkown peer opcode */
564 printf ("Unknown parse opcode for switch statement: %s (%d)\n",
565 Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
570 * Add the default at the end of the if/else construct
576 /* Add an ELSE first */
578 TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
579 DefaultOp->Asl.Parent = Conditional->Asl.Parent;
583 /* There were no CASE statements, no ELSE needed */
585 TrAmlInsertPeer (CurrentParentNode, DefaultOp->Asl.Child);
590 * Add a NAME node for the temp integer
592 NewOp = TrCreateLeafNode (PARSEOP_NAME);
593 NewOp->Asl.Parent = Gbl_FirstLevelInsertionNode->Asl.Parent;
595 NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
596 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
597 NewOp->Asl.Child = NewOp2;
598 NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_INTEGER, (ACPI_INTEGER) 0);
600 TrAmlSetSubtreeParent (NewOp2, NewOp);
602 /* Insert this node at the global level of the ASL */
604 TrAmlInsertPeer (Gbl_FirstLevelInsertionNode, NewOp);
605 TrAmlInitLineNumbers (NewOp, Gbl_FirstLevelInsertionNode);
606 TrAmlInitLineNumbers (NewOp2, Gbl_FirstLevelInsertionNode);
607 TrAmlInitLineNumbers (NewOp2->Asl.Next, Gbl_FirstLevelInsertionNode);
610 * Change the SWITCH node to a STORE (predicate value, _Txx)
612 TrAmlInitNode (StartNode, PARSEOP_STORE);
614 Predicate = StartNode->Asl.Child;
615 Predicate->Asl.Child = NULL;
617 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
618 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValuePath));
619 NewOp->Asl.Parent = StartNode;
620 Predicate->Asl.Next = NewOp;