2 /******************************************************************************
4 * Module Name: asltransform - Parse tree transforms
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
46 #include <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include "aslcompiler.y.h"
49 #define _COMPONENT ACPI_COMPILER
50 ACPI_MODULE_NAME ("asltransform")
52 /* Local prototypes */
56 ACPI_PARSE_OBJECT *Op);
59 TrAmlGetNextTempName (
60 ACPI_PARSE_OBJECT *Op,
64 TrAmlInitLineNumbers (
65 ACPI_PARSE_OBJECT *Op,
66 ACPI_PARSE_OBJECT *Neighbor);
70 ACPI_PARSE_OBJECT *Op,
74 TrAmlSetSubtreeParent (
75 ACPI_PARSE_OBJECT *Op,
76 ACPI_PARSE_OBJECT *Parent);
80 ACPI_PARSE_OBJECT *Op,
81 ACPI_PARSE_OBJECT *NewPeer);
85 ACPI_PARSE_OBJECT *Op);
89 ACPI_PARSE_OBJECT *StartNode);
92 /*******************************************************************************
94 * FUNCTION: TrAmlGetNextTempName
96 * PARAMETERS: Op - Current parse op
97 * TempCount - Current temporary counter. Was originally
98 * per-module; Currently per method, could be
99 * expanded to per-scope.
101 * RETURN: A pointer to name (allocated here).
103 * DESCRIPTION: Generate an ACPI name of the form _T_x. These names are
104 * reserved for use by the ASL compiler. (_T_0 through _T_Z)
106 ******************************************************************************/
109 TrAmlGetNextTempName (
110 ACPI_PARSE_OBJECT *Op,
116 if (*TempCount >= (10+26)) /* 0-35 valid: 0-9 and A-Z for TempName[3] */
120 AslError (ASL_ERROR, ASL_MSG_TOO_MANY_TEMPS, Op, NULL);
124 TempName = UtLocalCalloc (5);
126 if (*TempCount < 10) /* 0-9 */
128 TempName[3] = (char) (*TempCount + '0');
130 else /* 10-35: A-Z */
132 TempName[3] = (char) (*TempCount + ('A' - 10));
136 /* First three characters are always "_T_" */
146 /*******************************************************************************
148 * FUNCTION: TrAmlInitLineNumbers
150 * PARAMETERS: Op - Op to be initialized
151 * Neighbor - Op used for initialization values
155 * DESCRIPTION: Initialized the various line numbers for a parse node.
157 ******************************************************************************/
160 TrAmlInitLineNumbers (
161 ACPI_PARSE_OBJECT *Op,
162 ACPI_PARSE_OBJECT *Neighbor)
165 Op->Asl.EndLine = Neighbor->Asl.EndLine;
166 Op->Asl.EndLogicalLine = Neighbor->Asl.EndLogicalLine;
167 Op->Asl.LineNumber = Neighbor->Asl.LineNumber;
168 Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
169 Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
173 /*******************************************************************************
175 * FUNCTION: TrAmlInitNode
177 * PARAMETERS: Op - Op to be initialized
178 * ParseOpcode - Opcode for this node
182 * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
184 ******************************************************************************/
188 ACPI_PARSE_OBJECT *Op,
192 Op->Asl.ParseOpcode = ParseOpcode;
193 UtSetParseOpName (Op);
197 /*******************************************************************************
199 * FUNCTION: TrAmlSetSubtreeParent
201 * PARAMETERS: Op - First node in a list of peer nodes
202 * Parent - Parent of the subtree
206 * DESCRIPTION: Set the parent for all peer nodes in a subtree
208 ******************************************************************************/
211 TrAmlSetSubtreeParent (
212 ACPI_PARSE_OBJECT *Op,
213 ACPI_PARSE_OBJECT *Parent)
215 ACPI_PARSE_OBJECT *Next;
221 Next->Asl.Parent = Parent;
222 Next = Next->Asl.Next;
227 /*******************************************************************************
229 * FUNCTION: TrAmlInsertPeer
231 * PARAMETERS: Op - First node in a list of peer nodes
232 * NewPeer - Peer node to insert
236 * DESCRIPTION: Insert a new peer node into a list of peers.
238 ******************************************************************************/
242 ACPI_PARSE_OBJECT *Op,
243 ACPI_PARSE_OBJECT *NewPeer)
246 NewPeer->Asl.Next = Op->Asl.Next;
247 Op->Asl.Next = NewPeer;
251 /*******************************************************************************
253 * FUNCTION: TrAmlTransformWalk
255 * PARAMETERS: ASL_WALK_CALLBACK
259 * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
262 ******************************************************************************/
266 ACPI_PARSE_OBJECT *Op,
271 TrTransformSubtree (Op);
276 /*******************************************************************************
278 * FUNCTION: TrTransformSubtree
280 * PARAMETERS: Op - The parent parse node
284 * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
285 * complex AML opcodes require processing of the child nodes
286 * (arguments/operands).
288 ******************************************************************************/
292 ACPI_PARSE_OBJECT *Op)
295 if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
300 switch (Op->Asl.ParseOpcode)
302 case PARSEOP_DEFINITIONBLOCK:
303 TrDoDefinitionBlock (Op);
313 * TBD: Zero the tempname (_T_x) count. Probably shouldn't be a global,
320 /* Nothing to do here for other opcodes */
326 /*******************************************************************************
328 * FUNCTION: TrDoDefinitionBlock
330 * PARAMETERS: Op - Parse node
334 * DESCRIPTION: Find the end of the definition block and set a global to this
335 * node. It is used by the compiler to insert compiler-generated
336 * names at the root level of the namespace.
338 ******************************************************************************/
341 TrDoDefinitionBlock (
342 ACPI_PARSE_OBJECT *Op)
344 ACPI_PARSE_OBJECT *Next;
348 Next = Op->Asl.Child;
349 for (i = 0; i < 5; i++)
351 Next = Next->Asl.Next;
355 * This is the table signature. Only the DSDT can be assumed
356 * to be at the root of the namespace; Therefore, namepath
357 * optimization can only be performed on the DSDT.
359 if (!ACPI_COMPARE_NAME (Next->Asl.Value.String, ACPI_SIG_DSDT))
361 Gbl_ReferenceOptimizationFlag = FALSE;
366 Gbl_FirstLevelInsertionNode = Next;
370 /*******************************************************************************
372 * FUNCTION: TrDoSwitch
374 * PARAMETERS: StartNode - Parse node for SWITCH
379 * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
380 * no actual AML opcode for SWITCH -- it must be simulated.
382 ******************************************************************************/
386 ACPI_PARSE_OBJECT *StartNode)
388 ACPI_PARSE_OBJECT *Next;
389 ACPI_PARSE_OBJECT *CaseOp = NULL;
390 ACPI_PARSE_OBJECT *CaseBlock = NULL;
391 ACPI_PARSE_OBJECT *DefaultOp = NULL;
392 ACPI_PARSE_OBJECT *CurrentParentNode;
393 ACPI_PARSE_OBJECT *Conditional = NULL;
394 ACPI_PARSE_OBJECT *Predicate;
395 ACPI_PARSE_OBJECT *Peer;
396 ACPI_PARSE_OBJECT *NewOp;
397 ACPI_PARSE_OBJECT *NewOp2;
398 ACPI_PARSE_OBJECT *MethodOp;
399 ACPI_PARSE_OBJECT *StoreOp;
400 ACPI_PARSE_OBJECT *BreakOp;
401 char *PredicateValueName;
406 /* Start node is the Switch() node */
408 CurrentParentNode = StartNode;
410 /* Create a new temp name of the form _T_x */
412 PredicateValueName = TrAmlGetNextTempName (StartNode, &Gbl_TempCount);
413 if (!PredicateValueName)
418 /* First child is the Switch() predicate */
420 Next = StartNode->Asl.Child;
423 * Examine the return type of the Switch Value -
424 * must be Integer/Buffer/String
426 Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
427 Btype = AslKeywordMapping[Index].AcpiBtype;
428 if ((Btype != ACPI_BTYPE_INTEGER) &&
429 (Btype != ACPI_BTYPE_STRING) &&
430 (Btype != ACPI_BTYPE_BUFFER))
432 AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
433 Btype = ACPI_BTYPE_INTEGER;
436 /* CASE statements start at next child */
438 Peer = Next->Asl.Next;
442 Peer = Next->Asl.Next;
444 if (Next->Asl.ParseOpcode == PARSEOP_CASE)
448 /* Add an ELSE to complete the previous CASE */
454 NewOp = TrCreateLeafNode (PARSEOP_ELSE);
455 NewOp->Asl.Parent = Conditional->Asl.Parent;
456 TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
458 /* Link ELSE node as a peer to the previous IF */
460 TrAmlInsertPeer (Conditional, NewOp);
461 CurrentParentNode = NewOp;
465 Conditional = CaseOp;
466 CaseBlock = CaseOp->Asl.Child->Asl.Next;
467 Conditional->Asl.Child->Asl.Next = NULL;
468 Predicate = CaseOp->Asl.Child;
470 if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
471 (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
474 * Convert the package declaration to this form:
476 * If (LNotEqual (Match (Package(<size>){<data>},
477 * MEQ, _T_x, MTR, Zero, Zero), Ones))
479 NewOp2 = TrCreateLeafNode (PARSEOP_MATCHTYPE_MEQ);
480 Predicate->Asl.Next = NewOp2;
481 TrAmlInitLineNumbers (NewOp2, Conditional);
484 NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
485 (UINT64) ACPI_TO_INTEGER (PredicateValueName));
486 NewOp->Asl.Next = NewOp2;
487 TrAmlInitLineNumbers (NewOp2, Predicate);
490 NewOp2 = TrCreateLeafNode (PARSEOP_MATCHTYPE_MTR);
491 NewOp->Asl.Next = NewOp2;
492 TrAmlInitLineNumbers (NewOp2, Predicate);
495 NewOp2 = TrCreateLeafNode (PARSEOP_ZERO);
496 NewOp->Asl.Next = NewOp2;
497 TrAmlInitLineNumbers (NewOp2, Predicate);
500 NewOp2 = TrCreateLeafNode (PARSEOP_ZERO);
501 NewOp->Asl.Next = NewOp2;
502 TrAmlInitLineNumbers (NewOp2, Predicate);
504 NewOp2 = TrCreateLeafNode (PARSEOP_MATCH);
505 NewOp2->Asl.Child = Predicate; /* PARSEOP_PACKAGE */
506 TrAmlInitLineNumbers (NewOp2, Conditional);
507 TrAmlSetSubtreeParent (Predicate, NewOp2);
510 NewOp2 = TrCreateLeafNode (PARSEOP_ONES);
511 NewOp->Asl.Next = NewOp2;
512 TrAmlInitLineNumbers (NewOp2, Conditional);
514 NewOp2 = TrCreateLeafNode (PARSEOP_LEQUAL);
515 NewOp2->Asl.Child = NewOp;
516 NewOp->Asl.Parent = NewOp2;
517 TrAmlInitLineNumbers (NewOp2, Conditional);
518 TrAmlSetSubtreeParent (NewOp, NewOp2);
521 NewOp2 = TrCreateLeafNode (PARSEOP_LNOT);
522 NewOp2->Asl.Child = NewOp;
523 NewOp2->Asl.Parent = Conditional;
524 NewOp->Asl.Parent = NewOp2;
525 TrAmlInitLineNumbers (NewOp2, Conditional);
527 Conditional->Asl.Child = NewOp2;
528 NewOp2->Asl.Next = CaseBlock;
533 * Integer and Buffer case.
535 * Change CaseOp() to: If (LEqual (SwitchValue, CaseValue)) {...}
536 * Note: SwitchValue is first to allow the CaseValue to be implicitly
537 * converted to the type of SwitchValue if necessary.
539 * CaseOp->Child is the case value
540 * CaseOp->Child->Peer is the beginning of the case block
542 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
543 (UINT64) ACPI_TO_INTEGER (PredicateValueName));
544 NewOp->Asl.Next = Predicate;
545 TrAmlInitLineNumbers (NewOp, Predicate);
547 NewOp2 = TrCreateLeafNode (PARSEOP_LEQUAL);
548 NewOp2->Asl.Parent = Conditional;
549 NewOp2->Asl.Child = NewOp;
550 TrAmlInitLineNumbers (NewOp2, Conditional);
552 TrAmlSetSubtreeParent (NewOp, NewOp2);
555 Predicate->Asl.Next = CaseBlock;
557 TrAmlSetSubtreeParent (Predicate, Conditional);
558 Conditional->Asl.Child = Predicate;
561 /* Reinitialize the CASE node to an IF node */
563 TrAmlInitNode (Conditional, PARSEOP_IF);
566 * The first CASE(IF) is not nested under an ELSE.
567 * All other CASEs are children of a parent ELSE.
569 if (CurrentParentNode == StartNode)
571 Conditional->Asl.Next = NULL;
576 * The IF is a child of previous IF/ELSE. It
577 * is therefore without peer.
579 CurrentParentNode->Asl.Child = Conditional;
580 Conditional->Asl.Parent = CurrentParentNode;
581 Conditional->Asl.Next = NULL;
584 else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
589 * More than one Default
590 * (Parser does not catch this, must check here)
592 AslError (ASL_ERROR, ASL_MSG_MULTIPLE_DEFAULT, Next, NULL);
596 /* Save the DEFAULT node for later, after CASEs */
603 /* Unknown peer opcode */
605 AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%u)\n",
606 Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
610 /* Add the default case at the end of the if/else construct */
614 /* If no CASE statements, this is an error - see below */
618 /* Convert the DEFAULT node to an ELSE */
625 TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
626 DefaultOp->Asl.Parent = Conditional->Asl.Parent;
628 /* Link ELSE node as a peer to the previous IF */
630 TrAmlInsertPeer (Conditional, DefaultOp);
636 AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
641 * Create a Name(_T_x, ...) statement. This statement must appear at the
642 * method level, in case a loop surrounds the switch statement and could
643 * cause the name to be created twice (error).
646 /* Create the Name node */
648 Predicate = StartNode->Asl.Child;
649 NewOp = TrCreateLeafNode (PARSEOP_NAME);
651 /* Find the parent method */
654 while ((Next->Asl.ParseOpcode != PARSEOP_METHOD) &&
655 (Next->Asl.ParseOpcode != PARSEOP_DEFINITIONBLOCK))
657 Next = Next->Asl.Parent;
661 NewOp->Asl.CompileFlags |= NODE_COMPILER_EMITTED;
662 NewOp->Asl.Parent = Next;
664 /* Insert name after the method name and arguments */
666 Next = Next->Asl.Child; /* Name */
667 Next = Next->Asl.Next; /* NumArgs */
668 Next = Next->Asl.Next; /* SerializeRule */
671 * If method is not Serialized, we must make is so, because of the way
672 * that Switch() must be implemented -- we cannot allow multiple threads
673 * to execute this method concurrently since we need to create local
676 if (Next->Asl.ParseOpcode != PARSEOP_SERIALIZERULE_SERIAL)
678 AslError (ASL_REMARK, ASL_MSG_SERIALIZED, MethodOp, "Due to use of Switch operator");
679 Next->Asl.ParseOpcode = PARSEOP_SERIALIZERULE_SERIAL;
682 Next = Next->Asl.Next; /* SyncLevel */
683 Next = Next->Asl.Next; /* ReturnType */
684 Next = Next->Asl.Next; /* ParameterTypes */
686 TrAmlInsertPeer (Next, NewOp);
687 TrAmlInitLineNumbers (NewOp, Next);
689 /* Create the NameSeg child for the Name node */
691 NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
692 (UINT64) ACPI_TO_INTEGER (PredicateValueName));
693 NewOp2->Asl.CompileFlags |= NODE_IS_NAME_DECLARATION;
694 NewOp->Asl.Child = NewOp2;
696 /* Create the initial value for the Name. Btype was already validated above */
700 case ACPI_BTYPE_INTEGER:
701 NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_ZERO,
705 case ACPI_BTYPE_STRING:
706 NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_STRING_LITERAL,
707 (UINT64) ACPI_TO_INTEGER (""));
710 case ACPI_BTYPE_BUFFER:
711 (void) TrLinkPeerNode (NewOp2, TrCreateValuedLeafNode (PARSEOP_BUFFER,
713 Next = NewOp2->Asl.Next;
714 (void) TrLinkChildren (Next, 1, TrCreateValuedLeafNode (PARSEOP_ZERO,
716 (void) TrLinkPeerNode (Next->Asl.Child,
717 TrCreateValuedLeafNode (PARSEOP_DEFAULT_ARG, (UINT64) 0));
719 TrAmlSetSubtreeParent (Next->Asl.Child, Next);
726 TrAmlSetSubtreeParent (NewOp2, NewOp);
729 * Transform the Switch() into a While(One)-Break node.
730 * And create a Store() node which will be used to save the
731 * Switch() value. The store is of the form: Store (Value, _T_x)
732 * where _T_x is the temp variable.
734 TrAmlInitNode (StartNode, PARSEOP_WHILE);
735 NewOp = TrCreateLeafNode (PARSEOP_ONE);
736 NewOp->Asl.Next = Predicate->Asl.Next;
737 NewOp->Asl.Parent = StartNode;
738 StartNode->Asl.Child = NewOp;
740 /* Create a Store() node */
742 StoreOp = TrCreateLeafNode (PARSEOP_STORE);
743 StoreOp->Asl.Parent = StartNode;
744 TrAmlInsertPeer (NewOp, StoreOp);
746 /* Complete the Store subtree */
748 StoreOp->Asl.Child = Predicate;
749 Predicate->Asl.Parent = StoreOp;
751 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
752 (UINT64) ACPI_TO_INTEGER (PredicateValueName));
753 NewOp->Asl.Parent = StoreOp;
754 Predicate->Asl.Next = NewOp;
756 /* Create a Break() node and insert it into the end of While() */
758 Conditional = StartNode->Asl.Child;
759 while (Conditional->Asl.Next)
761 Conditional = Conditional->Asl.Next;
764 BreakOp = TrCreateLeafNode (PARSEOP_BREAK);
765 BreakOp->Asl.Parent = StartNode;
766 TrAmlInsertPeer (Conditional, BreakOp);