Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / compiler / asltree.c
blobc8d05e4bc3c49730bad3e04f391f4988c163f5ef
1 /******************************************************************************
3 * Module Name: asltree - parse tree management
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include "aslcompiler.h"
46 #include "aslcompiler.y.h"
47 #include "acapps.h"
48 #include <time.h>
50 #define _COMPONENT ACPI_COMPILER
51 ACPI_MODULE_NAME ("asltree")
53 /* Local prototypes */
55 static ACPI_PARSE_OBJECT *
56 TrGetNextNode (
57 void);
59 static char *
60 TrGetNodeFlagName (
61 UINT32 Flags);
64 /*******************************************************************************
66 * FUNCTION: TrGetNextNode
68 * PARAMETERS: None
70 * RETURN: New parse node. Aborts on allocation failure
72 * DESCRIPTION: Allocate a new parse node for the parse tree. Bypass the local
73 * dynamic memory manager for performance reasons (This has a
74 * major impact on the speed of the compiler.)
76 ******************************************************************************/
78 static ACPI_PARSE_OBJECT *
79 TrGetNextNode (
80 void)
83 if (Gbl_NodeCacheNext >= Gbl_NodeCacheLast)
85 Gbl_NodeCacheNext = UtLocalCalloc (sizeof (ACPI_PARSE_OBJECT) *
86 ASL_NODE_CACHE_SIZE);
87 Gbl_NodeCacheLast = Gbl_NodeCacheNext + ASL_NODE_CACHE_SIZE;
90 return (Gbl_NodeCacheNext++);
94 /*******************************************************************************
96 * FUNCTION: TrAllocateNode
98 * PARAMETERS: ParseOpcode - Opcode to be assigned to the node
100 * RETURN: New parse node. Aborts on allocation failure
102 * DESCRIPTION: Allocate and initialize a new parse node for the parse tree
104 ******************************************************************************/
106 ACPI_PARSE_OBJECT *
107 TrAllocateNode (
108 UINT32 ParseOpcode)
110 ACPI_PARSE_OBJECT *Op;
113 Op = TrGetNextNode ();
115 Op->Asl.ParseOpcode = (UINT16) ParseOpcode;
116 Op->Asl.Filename = Gbl_Files[ASL_FILE_INPUT].Filename;
117 Op->Asl.LineNumber = Gbl_CurrentLineNumber;
118 Op->Asl.LogicalLineNumber = Gbl_LogicalLineNumber;
119 Op->Asl.LogicalByteOffset = Gbl_CurrentLineOffset;
120 Op->Asl.Column = Gbl_CurrentColumn;
122 UtSetParseOpName (Op);
123 return (Op);
127 /*******************************************************************************
129 * FUNCTION: TrReleaseNode
131 * PARAMETERS: Op - Op to be released
133 * RETURN: None
135 * DESCRIPTION: "release" a node. In truth, nothing is done since the node
136 * is part of a larger buffer
138 ******************************************************************************/
140 void
141 TrReleaseNode (
142 ACPI_PARSE_OBJECT *Op)
145 return;
149 /*******************************************************************************
151 * FUNCTION: TrUpdateNode
153 * PARAMETERS: ParseOpcode - New opcode to be assigned to the node
154 * Op - An existing parse node
156 * RETURN: The updated node
158 * DESCRIPTION: Change the parse opcode assigned to a node. Usually used to
159 * change an opcode to DEFAULT_ARG so that the node is ignored
160 * during the code generation. Also used to set generic integers
161 * to a specific size (8, 16, 32, or 64 bits)
163 ******************************************************************************/
165 ACPI_PARSE_OBJECT *
166 TrUpdateNode (
167 UINT32 ParseOpcode,
168 ACPI_PARSE_OBJECT *Op)
171 if (!Op)
173 return (NULL);
176 DbgPrint (ASL_PARSE_OUTPUT,
177 "\nUpdateNode: Old - %s, New - %s\n\n",
178 UtGetOpName (Op->Asl.ParseOpcode),
179 UtGetOpName (ParseOpcode));
181 /* Assign new opcode and name */
183 if (Op->Asl.ParseOpcode == PARSEOP_ONES)
185 switch (ParseOpcode)
187 case PARSEOP_BYTECONST:
189 Op->Asl.Value.Integer = ACPI_UINT8_MAX;
190 break;
192 case PARSEOP_WORDCONST:
194 Op->Asl.Value.Integer = ACPI_UINT16_MAX;
195 break;
197 case PARSEOP_DWORDCONST:
199 Op->Asl.Value.Integer = ACPI_UINT32_MAX;
200 break;
202 /* Don't need to do the QWORD case */
204 default:
206 /* Don't care about others */
207 break;
211 Op->Asl.ParseOpcode = (UINT16) ParseOpcode;
212 UtSetParseOpName (Op);
215 * For the BYTE, WORD, and DWORD constants, make sure that the integer
216 * that was passed in will actually fit into the data type
218 switch (ParseOpcode)
220 case PARSEOP_BYTECONST:
222 UtCheckIntegerRange (Op, 0x00, ACPI_UINT8_MAX);
223 Op->Asl.Value.Integer &= ACPI_UINT8_MAX;
224 break;
226 case PARSEOP_WORDCONST:
228 UtCheckIntegerRange (Op, 0x00, ACPI_UINT16_MAX);
229 Op->Asl.Value.Integer &= ACPI_UINT16_MAX;
230 break;
232 case PARSEOP_DWORDCONST:
234 UtCheckIntegerRange (Op, 0x00, ACPI_UINT32_MAX);
235 Op->Asl.Value.Integer &= ACPI_UINT32_MAX;
236 break;
238 default:
240 /* Don't care about others, don't need to check QWORD */
242 break;
245 return (Op);
249 /*******************************************************************************
251 * FUNCTION: TrGetNodeFlagName
253 * PARAMETERS: Flags - Flags word to be decoded
255 * RETURN: Name string. Always returns a valid string pointer.
257 * DESCRIPTION: Decode a flags word
259 ******************************************************************************/
261 static char *
262 TrGetNodeFlagName (
263 UINT32 Flags)
266 switch (Flags)
268 case NODE_VISITED:
270 return ("NODE_VISITED");
272 case NODE_AML_PACKAGE:
274 return ("NODE_AML_PACKAGE");
276 case NODE_IS_TARGET:
278 return ("NODE_IS_TARGET");
280 case NODE_IS_RESOURCE_DESC:
282 return ("NODE_IS_RESOURCE_DESC");
284 case NODE_IS_RESOURCE_FIELD:
286 return ("NODE_IS_RESOURCE_FIELD");
288 case NODE_HAS_NO_EXIT:
290 return ("NODE_HAS_NO_EXIT");
292 case NODE_IF_HAS_NO_EXIT:
294 return ("NODE_IF_HAS_NO_EXIT");
296 case NODE_NAME_INTERNALIZED:
298 return ("NODE_NAME_INTERNALIZED");
300 case NODE_METHOD_NO_RETVAL:
302 return ("NODE_METHOD_NO_RETVAL");
304 case NODE_METHOD_SOME_NO_RETVAL:
306 return ("NODE_METHOD_SOME_NO_RETVAL");
308 case NODE_RESULT_NOT_USED:
310 return ("NODE_RESULT_NOT_USED");
312 case NODE_METHOD_TYPED:
314 return ("NODE_METHOD_TYPED");
316 case NODE_COMPILE_TIME_CONST:
318 return ("NODE_COMPILE_TIME_CONST");
320 case NODE_IS_TERM_ARG:
322 return ("NODE_IS_TERM_ARG");
324 case NODE_WAS_ONES_OP:
326 return ("NODE_WAS_ONES_OP");
328 case NODE_IS_NAME_DECLARATION:
330 return ("NODE_IS_NAME_DECLARATION");
332 default:
334 return ("Multiple Flags (or unknown flag) set");
339 /*******************************************************************************
341 * FUNCTION: TrSetNodeFlags
343 * PARAMETERS: Op - An existing parse node
344 * Flags - New flags word
346 * RETURN: The updated parser op
348 * DESCRIPTION: Set bits in the node flags word. Will not clear bits, only set
350 ******************************************************************************/
352 ACPI_PARSE_OBJECT *
353 TrSetNodeFlags (
354 ACPI_PARSE_OBJECT *Op,
355 UINT32 Flags)
358 DbgPrint (ASL_PARSE_OUTPUT,
359 "\nSetNodeFlags: Op %p, %8.8X %s\n\n", Op, Flags,
360 TrGetNodeFlagName (Flags));
362 if (!Op)
364 return (NULL);
367 Op->Asl.CompileFlags |= Flags;
368 return (Op);
372 /*******************************************************************************
374 * FUNCTION: TrSetNodeAmlLength
376 * PARAMETERS: Op - An existing parse node
377 * Length - AML Length
379 * RETURN: The updated parser op
381 * DESCRIPTION: Set the AML Length in a node. Used by the parser to indicate
382 * the presence of a node that must be reduced to a fixed length
383 * constant.
385 ******************************************************************************/
387 ACPI_PARSE_OBJECT *
388 TrSetNodeAmlLength (
389 ACPI_PARSE_OBJECT *Op,
390 UINT32 Length)
393 DbgPrint (ASL_PARSE_OUTPUT,
394 "\nSetNodeAmlLength: Op %p, %8.8X\n", Op, Length);
396 if (!Op)
398 return (NULL);
401 Op->Asl.AmlLength = Length;
402 return (Op);
406 /*******************************************************************************
408 * FUNCTION: TrSetEndLineNumber
410 * PARAMETERS: Op - An existing parse node
412 * RETURN: None.
414 * DESCRIPTION: Set the ending line numbers (file line and logical line) of a
415 * parse node to the current line numbers.
417 ******************************************************************************/
419 void
420 TrSetEndLineNumber (
421 ACPI_PARSE_OBJECT *Op)
424 /* If the end line # is already set, just return */
426 if (Op->Asl.EndLine)
428 return;
431 Op->Asl.EndLine = Gbl_CurrentLineNumber;
432 Op->Asl.EndLogicalLine = Gbl_LogicalLineNumber;
436 /*******************************************************************************
438 * FUNCTION: TrCreateLeafNode
440 * PARAMETERS: ParseOpcode - New opcode to be assigned to the node
442 * RETURN: Pointer to the new node. Aborts on allocation failure
444 * DESCRIPTION: Create a simple leaf node (no children or peers, and no value
445 * assigned to the node)
447 ******************************************************************************/
449 ACPI_PARSE_OBJECT *
450 TrCreateLeafNode (
451 UINT32 ParseOpcode)
453 ACPI_PARSE_OBJECT *Op;
456 Op = TrAllocateNode (ParseOpcode);
458 DbgPrint (ASL_PARSE_OUTPUT,
459 "\nCreateLeafNode Ln/Col %u/%u NewNode %p Op %s\n\n",
460 Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName(ParseOpcode));
462 return (Op);
466 /*******************************************************************************
468 * FUNCTION: TrCreateConstantLeafNode
470 * PARAMETERS: ParseOpcode - The constant opcode
472 * RETURN: Pointer to the new node. Aborts on allocation failure
474 * DESCRIPTION: Create a leaf node (no children or peers) for one of the
475 * special constants - __LINE__, __FILE__, and __DATE__.
477 * Note: An implemenation of __FUNC__ cannot happen here because we don't
478 * have a full parse tree at this time and cannot find the parent control
479 * method. If it is ever needed, __FUNC__ must be implemented later, after
480 * the parse tree has been fully constructed.
482 ******************************************************************************/
484 ACPI_PARSE_OBJECT *
485 TrCreateConstantLeafNode (
486 UINT32 ParseOpcode)
488 ACPI_PARSE_OBJECT *Op = NULL;
489 time_t CurrentTime;
490 char *StaticTimeString;
491 char *TimeString;
492 char *Path;
493 char *Filename;
496 switch (ParseOpcode)
498 case PARSEOP___LINE__:
500 Op = TrAllocateNode (PARSEOP_INTEGER);
501 Op->Asl.Value.Integer = Op->Asl.LineNumber;
502 break;
504 case PARSEOP___PATH__:
506 Op = TrAllocateNode (PARSEOP_STRING_LITERAL);
508 /* Op.Asl.Filename contains the full pathname to the file */
510 Op->Asl.Value.String = Op->Asl.Filename;
511 break;
513 case PARSEOP___FILE__:
515 Op = TrAllocateNode (PARSEOP_STRING_LITERAL);
517 /* Get the simple filename from the full path */
519 FlSplitInputPathname (Op->Asl.Filename, &Path, &Filename);
520 ACPI_FREE (Path);
521 Op->Asl.Value.String = Filename;
522 break;
524 case PARSEOP___DATE__:
526 Op = TrAllocateNode (PARSEOP_STRING_LITERAL);
528 /* Get a copy of the current time */
530 CurrentTime = time (NULL);
531 StaticTimeString = ctime (&CurrentTime);
532 TimeString = UtLocalCalloc (strlen (StaticTimeString) + 1);
533 strcpy (TimeString, StaticTimeString);
535 TimeString[strlen(TimeString) -1] = 0; /* Remove trailing newline */
536 Op->Asl.Value.String = TimeString;
537 break;
539 default: /* This would be an internal error */
541 return (NULL);
544 DbgPrint (ASL_PARSE_OUTPUT,
545 "\nCreateConstantLeafNode Ln/Col %u/%u NewNode %p Op %s Value %8.8X%8.8X ",
546 Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName (ParseOpcode),
547 ACPI_FORMAT_UINT64 (Op->Asl.Value.Integer));
548 return (Op);
552 /*******************************************************************************
554 * FUNCTION: TrCreateValuedLeafNode
556 * PARAMETERS: ParseOpcode - New opcode to be assigned to the node
557 * Value - Value to be assigned to the node
559 * RETURN: Pointer to the new node. Aborts on allocation failure
561 * DESCRIPTION: Create a leaf node (no children or peers) with a value
562 * assigned to it
564 ******************************************************************************/
566 ACPI_PARSE_OBJECT *
567 TrCreateValuedLeafNode (
568 UINT32 ParseOpcode,
569 UINT64 Value)
571 ACPI_PARSE_OBJECT *Op;
574 Op = TrAllocateNode (ParseOpcode);
576 DbgPrint (ASL_PARSE_OUTPUT,
577 "\nCreateValuedLeafNode Ln/Col %u/%u NewNode %p Op %s Value %8.8X%8.8X ",
578 Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName(ParseOpcode),
579 ACPI_FORMAT_UINT64 (Value));
580 Op->Asl.Value.Integer = Value;
582 switch (ParseOpcode)
584 case PARSEOP_STRING_LITERAL:
586 DbgPrint (ASL_PARSE_OUTPUT, "STRING->%s", Value);
587 break;
589 case PARSEOP_NAMESEG:
591 DbgPrint (ASL_PARSE_OUTPUT, "NAMESEG->%s", Value);
592 break;
594 case PARSEOP_NAMESTRING:
596 DbgPrint (ASL_PARSE_OUTPUT, "NAMESTRING->%s", Value);
597 break;
599 case PARSEOP_EISAID:
601 DbgPrint (ASL_PARSE_OUTPUT, "EISAID->%s", Value);
602 break;
604 case PARSEOP_METHOD:
606 DbgPrint (ASL_PARSE_OUTPUT, "METHOD");
607 break;
609 case PARSEOP_INTEGER:
611 DbgPrint (ASL_PARSE_OUTPUT, "INTEGER");
612 break;
614 default:
616 break;
619 DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
620 return (Op);
624 /*******************************************************************************
626 * FUNCTION: TrCreateNode
628 * PARAMETERS: ParseOpcode - Opcode to be assigned to the node
629 * NumChildren - Number of children to follow
630 * ... - A list of child nodes to link to the new
631 * node. NumChildren long.
633 * RETURN: Pointer to the new node. Aborts on allocation failure
635 * DESCRIPTION: Create a new parse node and link together a list of child
636 * nodes underneath the new node.
638 ******************************************************************************/
640 ACPI_PARSE_OBJECT *
641 TrCreateNode (
642 UINT32 ParseOpcode,
643 UINT32 NumChildren,
644 ...)
646 ACPI_PARSE_OBJECT *Op;
647 ACPI_PARSE_OBJECT *Child;
648 ACPI_PARSE_OBJECT *PrevChild;
649 va_list ap;
650 UINT32 i;
651 BOOLEAN FirstChild;
654 va_start (ap, NumChildren);
656 /* Allocate one new node */
658 Op = TrAllocateNode (ParseOpcode);
660 DbgPrint (ASL_PARSE_OUTPUT,
661 "\nCreateNode Ln/Col %u/%u NewParent %p Child %u Op %s ",
662 Op->Asl.LineNumber, Op->Asl.Column, Op, NumChildren, UtGetOpName(ParseOpcode));
664 /* Some extra debug output based on the parse opcode */
666 switch (ParseOpcode)
668 case PARSEOP_DEFINITIONBLOCK:
670 RootNode = Op;
671 DbgPrint (ASL_PARSE_OUTPUT, "DEFINITION_BLOCK (Tree Completed)->");
672 break;
674 case PARSEOP_OPERATIONREGION:
676 DbgPrint (ASL_PARSE_OUTPUT, "OPREGION->");
677 break;
679 case PARSEOP_OR:
681 DbgPrint (ASL_PARSE_OUTPUT, "OR->");
682 break;
684 default:
686 /* Nothing to do for other opcodes */
688 break;
691 /* Link the new node to its children */
693 PrevChild = NULL;
694 FirstChild = TRUE;
695 for (i = 0; i < NumChildren; i++)
697 /* Get the next child */
699 Child = va_arg (ap, ACPI_PARSE_OBJECT *);
700 DbgPrint (ASL_PARSE_OUTPUT, "%p, ", Child);
703 * If child is NULL, this means that an optional argument
704 * was omitted. We must create a placeholder with a special
705 * opcode (DEFAULT_ARG) so that the code generator will know
706 * that it must emit the correct default for this argument
708 if (!Child)
710 Child = TrAllocateNode (PARSEOP_DEFAULT_ARG);
713 /* Link first child to parent */
715 if (FirstChild)
717 FirstChild = FALSE;
718 Op->Asl.Child = Child;
721 /* Point all children to parent */
723 Child->Asl.Parent = Op;
725 /* Link children in a peer list */
727 if (PrevChild)
729 PrevChild->Asl.Next = Child;
733 * This child might be a list, point all nodes in the list
734 * to the same parent
736 while (Child->Asl.Next)
738 Child = Child->Asl.Next;
739 Child->Asl.Parent = Op;
742 PrevChild = Child;
744 va_end(ap);
746 DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
747 return (Op);
751 /*******************************************************************************
753 * FUNCTION: TrLinkChildren
755 * PARAMETERS: Op - An existing parse node
756 * NumChildren - Number of children to follow
757 * ... - A list of child nodes to link to the new
758 * node. NumChildren long.
760 * RETURN: The updated (linked) node
762 * DESCRIPTION: Link a group of nodes to an existing parse node
764 ******************************************************************************/
766 ACPI_PARSE_OBJECT *
767 TrLinkChildren (
768 ACPI_PARSE_OBJECT *Op,
769 UINT32 NumChildren,
770 ...)
772 ACPI_PARSE_OBJECT *Child;
773 ACPI_PARSE_OBJECT *PrevChild;
774 va_list ap;
775 UINT32 i;
776 BOOLEAN FirstChild;
779 va_start (ap, NumChildren);
782 TrSetEndLineNumber (Op);
784 DbgPrint (ASL_PARSE_OUTPUT,
785 "\nLinkChildren Line [%u to %u] NewParent %p Child %u Op %s ",
786 Op->Asl.LineNumber, Op->Asl.EndLine,
787 Op, NumChildren, UtGetOpName(Op->Asl.ParseOpcode));
789 switch (Op->Asl.ParseOpcode)
791 case PARSEOP_DEFINITIONBLOCK:
793 RootNode = Op;
794 DbgPrint (ASL_PARSE_OUTPUT, "DEFINITION_BLOCK (Tree Completed)->");
795 break;
797 case PARSEOP_OPERATIONREGION:
799 DbgPrint (ASL_PARSE_OUTPUT, "OPREGION->");
800 break;
802 case PARSEOP_OR:
804 DbgPrint (ASL_PARSE_OUTPUT, "OR->");
805 break;
807 default:
809 /* Nothing to do for other opcodes */
811 break;
814 /* Link the new node to it's children */
816 PrevChild = NULL;
817 FirstChild = TRUE;
818 for (i = 0; i < NumChildren; i++)
820 Child = va_arg (ap, ACPI_PARSE_OBJECT *);
822 if ((Child == PrevChild) && (Child != NULL))
824 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Child,
825 "Child node list invalid");
826 va_end(ap);
827 return (Op);
830 DbgPrint (ASL_PARSE_OUTPUT, "%p, ", Child);
833 * If child is NULL, this means that an optional argument
834 * was omitted. We must create a placeholder with a special
835 * opcode (DEFAULT_ARG) so that the code generator will know
836 * that it must emit the correct default for this argument
838 if (!Child)
840 Child = TrAllocateNode (PARSEOP_DEFAULT_ARG);
843 /* Link first child to parent */
845 if (FirstChild)
847 FirstChild = FALSE;
848 Op->Asl.Child = Child;
851 /* Point all children to parent */
853 Child->Asl.Parent = Op;
855 /* Link children in a peer list */
857 if (PrevChild)
859 PrevChild->Asl.Next = Child;
863 * This child might be a list, point all nodes in the list
864 * to the same parent
866 while (Child->Asl.Next)
868 Child = Child->Asl.Next;
869 Child->Asl.Parent = Op;
871 PrevChild = Child;
874 va_end(ap);
875 DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
876 return (Op);
880 /*******************************************************************************
882 * FUNCTION: TrLinkPeerNode
884 * PARAMETERS: Op1 - First peer
885 * Op2 - Second peer
887 * RETURN: Op1 or the non-null node.
889 * DESCRIPTION: Link two nodes as peers. Handles cases where one peer is null.
891 ******************************************************************************/
893 ACPI_PARSE_OBJECT *
894 TrLinkPeerNode (
895 ACPI_PARSE_OBJECT *Op1,
896 ACPI_PARSE_OBJECT *Op2)
898 ACPI_PARSE_OBJECT *Next;
901 DbgPrint (ASL_PARSE_OUTPUT,
902 "\nLinkPeerNode: 1=%p (%s), 2=%p (%s)\n\n",
903 Op1, Op1 ? UtGetOpName(Op1->Asl.ParseOpcode) : NULL,
904 Op2, Op2 ? UtGetOpName(Op2->Asl.ParseOpcode) : NULL);
907 if ((!Op1) && (!Op2))
909 DbgPrint (ASL_PARSE_OUTPUT, "\nTwo Null nodes!\n");
910 return (Op1);
913 /* If one of the nodes is null, just return the non-null node */
915 if (!Op2)
917 return (Op1);
920 if (!Op1)
922 return (Op2);
925 if (Op1 == Op2)
927 DbgPrint (ASL_DEBUG_OUTPUT,
928 "\n\n************* Internal error, linking node to itself %p\n\n\n",
929 Op1);
930 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op1,
931 "Linking node to itself");
932 return (Op1);
935 Op1->Asl.Parent = Op2->Asl.Parent;
938 * Op 1 may already have a peer list (such as an IF/ELSE pair),
939 * so we must walk to the end of the list and attach the new
940 * peer at the end
942 Next = Op1;
943 while (Next->Asl.Next)
945 Next = Next->Asl.Next;
948 Next->Asl.Next = Op2;
949 return (Op1);
953 /*******************************************************************************
955 * FUNCTION: TrLinkPeerNodes
957 * PARAMETERS: NumPeers - The number of nodes in the list to follow
958 * ... - A list of nodes to link together as peers
960 * RETURN: The first node in the list (head of the peer list)
962 * DESCRIPTION: Link together an arbitrary number of peer nodes.
964 ******************************************************************************/
966 ACPI_PARSE_OBJECT *
967 TrLinkPeerNodes (
968 UINT32 NumPeers,
969 ...)
971 ACPI_PARSE_OBJECT *This;
972 ACPI_PARSE_OBJECT *Next;
973 va_list ap;
974 UINT32 i;
975 ACPI_PARSE_OBJECT *Start;
978 DbgPrint (ASL_PARSE_OUTPUT,
979 "\nLinkPeerNodes: (%u) ", NumPeers);
981 va_start (ap, NumPeers);
982 This = va_arg (ap, ACPI_PARSE_OBJECT *);
983 Start = This;
986 * Link all peers
988 for (i = 0; i < (NumPeers -1); i++)
990 DbgPrint (ASL_PARSE_OUTPUT, "%u=%p ", (i+1), This);
992 while (This->Asl.Next)
994 This = This->Asl.Next;
997 /* Get another peer node */
999 Next = va_arg (ap, ACPI_PARSE_OBJECT *);
1000 if (!Next)
1002 Next = TrAllocateNode (PARSEOP_DEFAULT_ARG);
1005 /* link new node to the current node */
1007 This->Asl.Next = Next;
1008 This = Next;
1010 va_end (ap);
1012 DbgPrint (ASL_PARSE_OUTPUT,"\n\n");
1013 return (Start);
1017 /*******************************************************************************
1019 * FUNCTION: TrLinkChildNode
1021 * PARAMETERS: Op1 - Parent node
1022 * Op2 - Op to become a child
1024 * RETURN: The parent node
1026 * DESCRIPTION: Link two nodes together as a parent and child
1028 ******************************************************************************/
1030 ACPI_PARSE_OBJECT *
1031 TrLinkChildNode (
1032 ACPI_PARSE_OBJECT *Op1,
1033 ACPI_PARSE_OBJECT *Op2)
1035 ACPI_PARSE_OBJECT *Next;
1038 DbgPrint (ASL_PARSE_OUTPUT,
1039 "\nLinkChildNode: Parent=%p (%s), Child=%p (%s)\n\n",
1040 Op1, Op1 ? UtGetOpName(Op1->Asl.ParseOpcode): NULL,
1041 Op2, Op2 ? UtGetOpName(Op2->Asl.ParseOpcode): NULL);
1043 if (!Op1 || !Op2)
1045 return (Op1);
1048 Op1->Asl.Child = Op2;
1050 /* Set the child and all peers of the child to point to the parent */
1052 Next = Op2;
1053 while (Next)
1055 Next->Asl.Parent = Op1;
1056 Next = Next->Asl.Next;
1059 return (Op1);
1063 /*******************************************************************************
1065 * FUNCTION: TrWalkParseTree
1067 * PARAMETERS: Visitation - Type of walk
1068 * DescendingCallback - Called during tree descent
1069 * AscendingCallback - Called during tree ascent
1070 * Context - To be passed to the callbacks
1072 * RETURN: Status from callback(s)
1074 * DESCRIPTION: Walk the entire parse tree.
1076 ******************************************************************************/
1078 ACPI_STATUS
1079 TrWalkParseTree (
1080 ACPI_PARSE_OBJECT *Op,
1081 UINT32 Visitation,
1082 ASL_WALK_CALLBACK DescendingCallback,
1083 ASL_WALK_CALLBACK AscendingCallback,
1084 void *Context)
1086 UINT32 Level;
1087 BOOLEAN NodePreviouslyVisited;
1088 ACPI_PARSE_OBJECT *StartOp = Op;
1089 ACPI_STATUS Status;
1092 if (!RootNode)
1094 return (AE_OK);
1097 Level = 0;
1098 NodePreviouslyVisited = FALSE;
1100 switch (Visitation)
1102 case ASL_WALK_VISIT_DOWNWARD:
1104 while (Op)
1106 if (!NodePreviouslyVisited)
1108 /* Let the callback process the node. */
1110 Status = DescendingCallback (Op, Level, Context);
1111 if (ACPI_SUCCESS (Status))
1113 /* Visit children first, once */
1115 if (Op->Asl.Child)
1117 Level++;
1118 Op = Op->Asl.Child;
1119 continue;
1122 else if (Status != AE_CTRL_DEPTH)
1124 /* Exit immediately on any error */
1126 return (Status);
1130 /* Terminate walk at start op */
1132 if (Op == StartOp)
1134 break;
1137 /* No more children, visit peers */
1139 if (Op->Asl.Next)
1141 Op = Op->Asl.Next;
1142 NodePreviouslyVisited = FALSE;
1144 else
1146 /* No children or peers, re-visit parent */
1148 if (Level != 0 )
1150 Level--;
1152 Op = Op->Asl.Parent;
1153 NodePreviouslyVisited = TRUE;
1156 break;
1158 case ASL_WALK_VISIT_UPWARD:
1160 while (Op)
1162 /* Visit leaf node (no children) or parent node on return trip */
1164 if ((!Op->Asl.Child) ||
1165 (NodePreviouslyVisited))
1167 /* Let the callback process the node. */
1169 Status = AscendingCallback (Op, Level, Context);
1170 if (ACPI_FAILURE (Status))
1172 return (Status);
1175 else
1177 /* Visit children first, once */
1179 Level++;
1180 Op = Op->Asl.Child;
1181 continue;
1184 /* Terminate walk at start op */
1186 if (Op == StartOp)
1188 break;
1191 /* No more children, visit peers */
1193 if (Op->Asl.Next)
1195 Op = Op->Asl.Next;
1196 NodePreviouslyVisited = FALSE;
1198 else
1200 /* No children or peers, re-visit parent */
1202 if (Level != 0 )
1204 Level--;
1206 Op = Op->Asl.Parent;
1207 NodePreviouslyVisited = TRUE;
1210 break;
1212 case ASL_WALK_VISIT_TWICE:
1214 while (Op)
1216 if (NodePreviouslyVisited)
1218 Status = AscendingCallback (Op, Level, Context);
1219 if (ACPI_FAILURE (Status))
1221 return (Status);
1224 else
1226 /* Let the callback process the node. */
1228 Status = DescendingCallback (Op, Level, Context);
1229 if (ACPI_SUCCESS (Status))
1231 /* Visit children first, once */
1233 if (Op->Asl.Child)
1235 Level++;
1236 Op = Op->Asl.Child;
1237 continue;
1240 else if (Status != AE_CTRL_DEPTH)
1242 /* Exit immediately on any error */
1244 return (Status);
1248 /* Terminate walk at start op */
1250 if (Op == StartOp)
1252 break;
1255 /* No more children, visit peers */
1257 if (Op->Asl.Next)
1259 Op = Op->Asl.Next;
1260 NodePreviouslyVisited = FALSE;
1262 else
1264 /* No children or peers, re-visit parent */
1266 if (Level != 0 )
1268 Level--;
1270 Op = Op->Asl.Parent;
1271 NodePreviouslyVisited = TRUE;
1274 break;
1276 default:
1277 /* No other types supported */
1278 break;
1281 /* If we get here, the walk completed with no errors */
1283 return (AE_OK);