2 /******************************************************************************
4 * Module Name: asltree - parse tree management
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 ("asltree")
52 /* Local prototypes */
54 static ACPI_PARSE_OBJECT *
63 /*******************************************************************************
65 * FUNCTION: TrGetNextNode
69 * RETURN: New parse node. Aborts on allocation failure
71 * DESCRIPTION: Allocate a new parse node for the parse tree. Bypass the local
72 * dynamic memory manager for performance reasons (This has a
73 * major impact on the speed of the compiler.)
75 ******************************************************************************/
77 static ACPI_PARSE_OBJECT *
82 if (Gbl_NodeCacheNext >= Gbl_NodeCacheLast)
84 Gbl_NodeCacheNext = UtLocalCalloc (sizeof (ACPI_PARSE_OBJECT) *
86 Gbl_NodeCacheLast = Gbl_NodeCacheNext + ASL_NODE_CACHE_SIZE;
89 return (Gbl_NodeCacheNext++);
93 /*******************************************************************************
95 * FUNCTION: TrAllocateNode
97 * PARAMETERS: ParseOpcode - Opcode to be assigned to the node
99 * RETURN: New parse node. Aborts on allocation failure
101 * DESCRIPTION: Allocate and initialize a new parse node for the parse tree
103 ******************************************************************************/
109 ACPI_PARSE_OBJECT *Op;
112 Op = TrGetNextNode ();
114 Op->Asl.ParseOpcode = (UINT16) ParseOpcode;
115 Op->Asl.Filename = Gbl_Files[ASL_FILE_INPUT].Filename;
116 Op->Asl.LineNumber = Gbl_CurrentLineNumber;
117 Op->Asl.LogicalLineNumber = Gbl_LogicalLineNumber;
118 Op->Asl.LogicalByteOffset = Gbl_CurrentLineOffset;
119 Op->Asl.Column = Gbl_CurrentColumn;
121 UtSetParseOpName (Op);
126 /*******************************************************************************
128 * FUNCTION: TrReleaseNode
130 * PARAMETERS: Op - Op to be released
134 * DESCRIPTION: "release" a node. In truth, nothing is done since the node
135 * is part of a larger buffer
137 ******************************************************************************/
141 ACPI_PARSE_OBJECT *Op)
148 /*******************************************************************************
150 * FUNCTION: TrUpdateNode
152 * PARAMETERS: ParseOpcode - New opcode to be assigned to the node
153 * Op - An existing parse node
155 * RETURN: The updated node
157 * DESCRIPTION: Change the parse opcode assigned to a node. Usually used to
158 * change an opcode to DEFAULT_ARG so that the node is ignored
159 * during the code generation. Also used to set generic integers
160 * to a specific size (8, 16, 32, or 64 bits)
162 ******************************************************************************/
167 ACPI_PARSE_OBJECT *Op)
175 DbgPrint (ASL_PARSE_OUTPUT,
176 "\nUpdateNode: Old - %s, New - %s\n\n",
177 UtGetOpName (Op->Asl.ParseOpcode),
178 UtGetOpName (ParseOpcode));
180 /* Assign new opcode and name */
182 if (Op->Asl.ParseOpcode == PARSEOP_ONES)
186 case PARSEOP_BYTECONST:
187 Op->Asl.Value.Integer = 0xFF;
190 case PARSEOP_WORDCONST:
191 Op->Asl.Value.Integer = 0xFFFF;
194 case PARSEOP_DWORDCONST:
195 Op->Asl.Value.Integer = 0xFFFFFFFF;
199 /* Don't care about others, don't need to check QWORD */
204 Op->Asl.ParseOpcode = (UINT16) ParseOpcode;
205 UtSetParseOpName (Op);
208 * For the BYTE, WORD, and DWORD constants, make sure that the integer
209 * that was passed in will actually fit into the data type
213 case PARSEOP_BYTECONST:
214 Op = UtCheckIntegerRange (Op, 0x00, ACPI_UINT8_MAX);
217 case PARSEOP_WORDCONST:
218 Op = UtCheckIntegerRange (Op, 0x00, ACPI_UINT16_MAX);
221 case PARSEOP_DWORDCONST:
222 Op = UtCheckIntegerRange (Op, 0x00, ACPI_UINT32_MAX);
226 /* Don't care about others, don't need to check QWORD */
234 /*******************************************************************************
236 * FUNCTION: TrGetNodeFlagName
238 * PARAMETERS: Flags - Flags word to be decoded
240 * RETURN: Name string. Always returns a valid string pointer.
242 * DESCRIPTION: Decode a flags word
244 ******************************************************************************/
254 return ("NODE_VISITED");
256 case NODE_AML_PACKAGE:
257 return ("NODE_AML_PACKAGE");
260 return ("NODE_IS_TARGET");
262 case NODE_IS_RESOURCE_DESC:
263 return ("NODE_IS_RESOURCE_DESC");
265 case NODE_IS_RESOURCE_FIELD:
266 return ("NODE_IS_RESOURCE_FIELD");
268 case NODE_HAS_NO_EXIT:
269 return ("NODE_HAS_NO_EXIT");
271 case NODE_IF_HAS_NO_EXIT:
272 return ("NODE_IF_HAS_NO_EXIT");
274 case NODE_NAME_INTERNALIZED:
275 return ("NODE_NAME_INTERNALIZED");
277 case NODE_METHOD_NO_RETVAL:
278 return ("NODE_METHOD_NO_RETVAL");
280 case NODE_METHOD_SOME_NO_RETVAL:
281 return ("NODE_METHOD_SOME_NO_RETVAL");
283 case NODE_RESULT_NOT_USED:
284 return ("NODE_RESULT_NOT_USED");
286 case NODE_METHOD_TYPED:
287 return ("NODE_METHOD_TYPED");
289 case NODE_IS_BIT_OFFSET:
290 return ("NODE_IS_BIT_OFFSET");
292 case NODE_COMPILE_TIME_CONST:
293 return ("NODE_COMPILE_TIME_CONST");
295 case NODE_IS_TERM_ARG:
296 return ("NODE_IS_TERM_ARG");
298 case NODE_WAS_ONES_OP:
299 return ("NODE_WAS_ONES_OP");
301 case NODE_IS_NAME_DECLARATION:
302 return ("NODE_IS_NAME_DECLARATION");
305 return ("Multiple Flags (or unknown flag) set");
310 /*******************************************************************************
312 * FUNCTION: TrSetNodeFlags
314 * PARAMETERS: Op - An existing parse node
315 * Flags - New flags word
317 * RETURN: The updated parser op
319 * DESCRIPTION: Set bits in the node flags word. Will not clear bits, only set
321 ******************************************************************************/
325 ACPI_PARSE_OBJECT *Op,
329 DbgPrint (ASL_PARSE_OUTPUT,
330 "\nSetNodeFlags: Op %p, %8.8X %s\n\n", Op, Flags,
331 TrGetNodeFlagName (Flags));
338 Op->Asl.CompileFlags |= Flags;
344 /*******************************************************************************
346 * FUNCTION: TrSetEndLineNumber
348 * PARAMETERS: Op - An existing parse node
352 * DESCRIPTION: Set the ending line numbers (file line and logical line) of a
353 * parse node to the current line numbers.
355 ******************************************************************************/
359 ACPI_PARSE_OBJECT *Op)
362 /* If the end line # is already set, just return */
369 Op->Asl.EndLine = Gbl_CurrentLineNumber;
370 Op->Asl.EndLogicalLine = Gbl_LogicalLineNumber;
374 /*******************************************************************************
376 * FUNCTION: TrCreateLeafNode
378 * PARAMETERS: ParseOpcode - New opcode to be assigned to the node
380 * RETURN: Pointer to the new node. Aborts on allocation failure
382 * DESCRIPTION: Create a simple leaf node (no children or peers, and no value
383 * assigned to the node)
385 ******************************************************************************/
391 ACPI_PARSE_OBJECT *Op;
394 Op = TrAllocateNode (ParseOpcode);
396 DbgPrint (ASL_PARSE_OUTPUT,
397 "\nCreateLeafNode Ln/Col %u/%u NewNode %p Op %s\n\n",
398 Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName(ParseOpcode));
404 /*******************************************************************************
406 * FUNCTION: TrCreateValuedLeafNode
408 * PARAMETERS: ParseOpcode - New opcode to be assigned to the node
409 * Value - Value to be assigned to the node
411 * RETURN: Pointer to the new node. Aborts on allocation failure
413 * DESCRIPTION: Create a leaf node (no children or peers) with a value
416 ******************************************************************************/
419 TrCreateValuedLeafNode (
423 ACPI_PARSE_OBJECT *Op;
426 Op = TrAllocateNode (ParseOpcode);
428 DbgPrint (ASL_PARSE_OUTPUT,
429 "\nCreateValuedLeafNode Ln/Col %u/%u NewNode %p Op %s Value %8.8X%8.8X ",
430 Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName(ParseOpcode),
431 ACPI_FORMAT_UINT64 (Value));
432 Op->Asl.Value.Integer = Value;
436 case PARSEOP_STRING_LITERAL:
437 DbgPrint (ASL_PARSE_OUTPUT, "STRING->%s", Value);
440 case PARSEOP_NAMESEG:
441 DbgPrint (ASL_PARSE_OUTPUT, "NAMESEG->%s", Value);
444 case PARSEOP_NAMESTRING:
445 DbgPrint (ASL_PARSE_OUTPUT, "NAMESTRING->%s", Value);
449 DbgPrint (ASL_PARSE_OUTPUT, "EISAID->%s", Value);
453 DbgPrint (ASL_PARSE_OUTPUT, "METHOD");
456 case PARSEOP_INTEGER:
457 DbgPrint (ASL_PARSE_OUTPUT, "INTEGER");
464 DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
469 /*******************************************************************************
471 * FUNCTION: TrCreateNode
473 * PARAMETERS: ParseOpcode - Opcode to be assigned to the node
474 * NumChildren - Number of children to follow
475 * ... - A list of child nodes to link to the new
476 * node. NumChildren long.
478 * RETURN: Pointer to the new node. Aborts on allocation failure
480 * DESCRIPTION: Create a new parse node and link together a list of child
481 * nodes underneath the new node.
483 ******************************************************************************/
491 ACPI_PARSE_OBJECT *Op;
492 ACPI_PARSE_OBJECT *Child;
493 ACPI_PARSE_OBJECT *PrevChild;
499 va_start (ap, NumChildren);
501 /* Allocate one new node */
503 Op = TrAllocateNode (ParseOpcode);
505 DbgPrint (ASL_PARSE_OUTPUT,
506 "\nCreateNode Ln/Col %u/%u NewParent %p Child %u Op %s ",
507 Op->Asl.LineNumber, Op->Asl.Column, Op, NumChildren, UtGetOpName(ParseOpcode));
509 /* Some extra debug output based on the parse opcode */
513 case PARSEOP_DEFINITIONBLOCK:
515 DbgPrint (ASL_PARSE_OUTPUT, "DEFINITION_BLOCK (Tree Completed)->");
518 case PARSEOP_OPERATIONREGION:
519 DbgPrint (ASL_PARSE_OUTPUT, "OPREGION->");
523 DbgPrint (ASL_PARSE_OUTPUT, "OR->");
527 /* Nothing to do for other opcodes */
531 /* Link the new node to its children */
535 for (i = 0; i < NumChildren; i++)
537 /* Get the next child */
539 Child = va_arg (ap, ACPI_PARSE_OBJECT *);
540 DbgPrint (ASL_PARSE_OUTPUT, "%p, ", Child);
543 * If child is NULL, this means that an optional argument
544 * was omitted. We must create a placeholder with a special
545 * opcode (DEFAULT_ARG) so that the code generator will know
546 * that it must emit the correct default for this argument
550 Child = TrAllocateNode (PARSEOP_DEFAULT_ARG);
553 /* Link first child to parent */
558 Op->Asl.Child = Child;
561 /* Point all children to parent */
563 Child->Asl.Parent = Op;
565 /* Link children in a peer list */
569 PrevChild->Asl.Next = Child;
573 * This child might be a list, point all nodes in the list
576 while (Child->Asl.Next)
578 Child = Child->Asl.Next;
579 Child->Asl.Parent = Op;
586 DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
591 /*******************************************************************************
593 * FUNCTION: TrLinkChildren
595 * PARAMETERS: Op - An existing parse node
596 * NumChildren - Number of children to follow
597 * ... - A list of child nodes to link to the new
598 * node. NumChildren long.
600 * RETURN: The updated (linked) node
602 * DESCRIPTION: Link a group of nodes to an existing parse node
604 ******************************************************************************/
608 ACPI_PARSE_OBJECT *Op,
612 ACPI_PARSE_OBJECT *Child;
613 ACPI_PARSE_OBJECT *PrevChild;
619 va_start (ap, NumChildren);
622 TrSetEndLineNumber (Op);
624 DbgPrint (ASL_PARSE_OUTPUT,
625 "\nLinkChildren Line [%u to %u] NewParent %p Child %u Op %s ",
626 Op->Asl.LineNumber, Op->Asl.EndLine,
627 Op, NumChildren, UtGetOpName(Op->Asl.ParseOpcode));
629 switch (Op->Asl.ParseOpcode)
631 case PARSEOP_DEFINITIONBLOCK:
633 DbgPrint (ASL_PARSE_OUTPUT, "DEFINITION_BLOCK (Tree Completed)->");
636 case PARSEOP_OPERATIONREGION:
637 DbgPrint (ASL_PARSE_OUTPUT, "OPREGION->");
641 DbgPrint (ASL_PARSE_OUTPUT, "OR->");
645 /* Nothing to do for other opcodes */
649 /* Link the new node to it's children */
653 for (i = 0; i < NumChildren; i++)
655 Child = va_arg (ap, ACPI_PARSE_OBJECT *);
657 if ((Child == PrevChild) && (Child != NULL))
659 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Child,
660 "Child node list invalid");
664 DbgPrint (ASL_PARSE_OUTPUT, "%p, ", Child);
667 * If child is NULL, this means that an optional argument
668 * was omitted. We must create a placeholder with a special
669 * opcode (DEFAULT_ARG) so that the code generator will know
670 * that it must emit the correct default for this argument
674 Child = TrAllocateNode (PARSEOP_DEFAULT_ARG);
677 /* Link first child to parent */
682 Op->Asl.Child = Child;
685 /* Point all children to parent */
687 Child->Asl.Parent = Op;
689 /* Link children in a peer list */
693 PrevChild->Asl.Next = Child;
697 * This child might be a list, point all nodes in the list
700 while (Child->Asl.Next)
702 Child = Child->Asl.Next;
703 Child->Asl.Parent = Op;
709 DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
714 /*******************************************************************************
716 * FUNCTION: TrLinkPeerNode
718 * PARAMETERS: Op1 - First peer
721 * RETURN: Op1 or the non-null node.
723 * DESCRIPTION: Link two nodes as peers. Handles cases where one peer is null.
725 ******************************************************************************/
729 ACPI_PARSE_OBJECT *Op1,
730 ACPI_PARSE_OBJECT *Op2)
732 ACPI_PARSE_OBJECT *Next;
735 DbgPrint (ASL_PARSE_OUTPUT,
736 "\nLinkPeerNode: 1=%p (%s), 2=%p (%s)\n\n",
737 Op1, Op1 ? UtGetOpName(Op1->Asl.ParseOpcode) : NULL,
738 Op2, Op2 ? UtGetOpName(Op2->Asl.ParseOpcode) : NULL);
741 if ((!Op1) && (!Op2))
743 DbgPrint (ASL_PARSE_OUTPUT, "\nTwo Null nodes!\n");
747 /* If one of the nodes is null, just return the non-null node */
761 DbgPrint (ASL_DEBUG_OUTPUT,
762 "\n\n************* Internal error, linking node to itself %p\n\n\n",
764 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op1,
765 "Linking node to itself");
769 Op1->Asl.Parent = Op2->Asl.Parent;
772 * Op 1 may already have a peer list (such as an IF/ELSE pair),
773 * so we must walk to the end of the list and attach the new
777 while (Next->Asl.Next)
779 Next = Next->Asl.Next;
782 Next->Asl.Next = Op2;
787 /*******************************************************************************
789 * FUNCTION: TrLinkPeerNodes
791 * PARAMETERS: NumPeers - The number of nodes in the list to follow
792 * ... - A list of nodes to link together as peers
794 * RETURN: The first node in the list (head of the peer list)
796 * DESCRIPTION: Link together an arbitrary number of peer nodes.
798 ******************************************************************************/
805 ACPI_PARSE_OBJECT *This;
806 ACPI_PARSE_OBJECT *Next;
809 ACPI_PARSE_OBJECT *Start;
812 DbgPrint (ASL_PARSE_OUTPUT,
813 "\nLinkPeerNodes: (%u) ", NumPeers);
815 va_start (ap, NumPeers);
816 This = va_arg (ap, ACPI_PARSE_OBJECT *);
822 for (i = 0; i < (NumPeers -1); i++)
824 DbgPrint (ASL_PARSE_OUTPUT, "%u=%p ", (i+1), This);
826 while (This->Asl.Next)
828 This = This->Asl.Next;
831 /* Get another peer node */
833 Next = va_arg (ap, ACPI_PARSE_OBJECT *);
836 Next = TrAllocateNode (PARSEOP_DEFAULT_ARG);
839 /* link new node to the current node */
841 This->Asl.Next = Next;
846 DbgPrint (ASL_PARSE_OUTPUT,"\n\n");
851 /*******************************************************************************
853 * FUNCTION: TrLinkChildNode
855 * PARAMETERS: Op1 - Parent node
856 * Op2 - Op to become a child
858 * RETURN: The parent node
860 * DESCRIPTION: Link two nodes together as a parent and child
862 ******************************************************************************/
866 ACPI_PARSE_OBJECT *Op1,
867 ACPI_PARSE_OBJECT *Op2)
869 ACPI_PARSE_OBJECT *Next;
872 DbgPrint (ASL_PARSE_OUTPUT,
873 "\nLinkChildNode: Parent=%p (%s), Child=%p (%s)\n\n",
874 Op1, Op1 ? UtGetOpName(Op1->Asl.ParseOpcode): NULL,
875 Op2, Op2 ? UtGetOpName(Op2->Asl.ParseOpcode): NULL);
882 Op1->Asl.Child = Op2;
884 /* Set the child and all peers of the child to point to the parent */
889 Next->Asl.Parent = Op1;
890 Next = Next->Asl.Next;
897 /*******************************************************************************
899 * FUNCTION: TrWalkParseTree
901 * PARAMETERS: Visitation - Type of walk
902 * DescendingCallback - Called during tree descent
903 * AscendingCallback - Called during tree ascent
904 * Context - To be passed to the callbacks
906 * RETURN: Status from callback(s)
908 * DESCRIPTION: Walk the entire parse tree.
910 ******************************************************************************/
914 ACPI_PARSE_OBJECT *Op,
916 ASL_WALK_CALLBACK DescendingCallback,
917 ASL_WALK_CALLBACK AscendingCallback,
921 BOOLEAN NodePreviouslyVisited;
922 ACPI_PARSE_OBJECT *StartOp = Op;
932 NodePreviouslyVisited = FALSE;
936 case ASL_WALK_VISIT_DOWNWARD:
940 if (!NodePreviouslyVisited)
942 /* Let the callback process the node. */
944 Status = DescendingCallback (Op, Level, Context);
945 if (ACPI_SUCCESS (Status))
947 /* Visit children first, once */
956 else if (Status != AE_CTRL_DEPTH)
958 /* Exit immediately on any error */
964 /* Terminate walk at start op */
971 /* No more children, visit peers */
976 NodePreviouslyVisited = FALSE;
980 /* No children or peers, re-visit parent */
987 NodePreviouslyVisited = TRUE;
993 case ASL_WALK_VISIT_UPWARD:
997 /* Visit leaf node (no children) or parent node on return trip */
999 if ((!Op->Asl.Child) ||
1000 (NodePreviouslyVisited))
1002 /* Let the callback process the node. */
1004 Status = AscendingCallback (Op, Level, Context);
1005 if (ACPI_FAILURE (Status))
1012 /* Visit children first, once */
1019 /* Terminate walk at start op */
1026 /* No more children, visit peers */
1031 NodePreviouslyVisited = FALSE;
1035 /* No children or peers, re-visit parent */
1041 Op = Op->Asl.Parent;
1042 NodePreviouslyVisited = TRUE;
1048 case ASL_WALK_VISIT_TWICE:
1052 if (NodePreviouslyVisited)
1054 Status = AscendingCallback (Op, Level, Context);
1055 if (ACPI_FAILURE (Status))
1062 /* Let the callback process the node. */
1064 Status = DescendingCallback (Op, Level, Context);
1065 if (ACPI_SUCCESS (Status))
1067 /* Visit children first, once */
1076 else if (Status != AE_CTRL_DEPTH)
1078 /* Exit immediately on any error */
1084 /* Terminate walk at start op */
1091 /* No more children, visit peers */
1096 NodePreviouslyVisited = FALSE;
1100 /* No children or peers, re-visit parent */
1106 Op = Op->Asl.Parent;
1107 NodePreviouslyVisited = TRUE;
1113 /* No other types supported */
1117 /* If we get here, the walk completed with no errors */