1 /******************************************************************************
2  *
3  * Module Name: asltree - Parse tree management
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include "aslcompiler.h"
153 #include "aslcompiler.y.h"
154 #include "acapps.h"
155 
156 #define _COMPONENT          ACPI_COMPILER
157         ACPI_MODULE_NAME    ("asltree")
158 
159 
160 /*******************************************************************************
161  *
162  * FUNCTION:    TrSetOpIntegerValue
163  *
164  * PARAMETERS:  ParseOpcode         - New opcode to be assigned to the op
165  *              Op                  - An existing parse op
166  *
167  * RETURN:      The updated op
168  *
169  * DESCRIPTION: Used to set the integer value of a op,
170  *              usually to a specific size (8, 16, 32, or 64 bits)
171  *
172  ******************************************************************************/
173 
174 ACPI_PARSE_OBJECT *
175 TrSetOpIntegerValue (
176     UINT32                  ParseOpcode,
177     ACPI_PARSE_OBJECT       *Op)
178 {
179 
180     if (!Op)
181     {
182         return (NULL);
183     }
184 
185     DbgPrint (ASL_PARSE_OUTPUT,
186         "\nUpdateOp: Old - %s, New - %s\n",
187         UtGetOpName (Op->Asl.ParseOpcode),
188         UtGetOpName (ParseOpcode));
189 
190     /* Assign new opcode and name */
191 
192     if (Op->Asl.ParseOpcode == PARSEOP_ONES)
193     {
194         switch (ParseOpcode)
195         {
196         case PARSEOP_BYTECONST:
197 
198             Op->Asl.Value.Integer = ACPI_UINT8_MAX;
199             break;
200 
201         case PARSEOP_WORDCONST:
202 
203             Op->Asl.Value.Integer = ACPI_UINT16_MAX;
204             break;
205 
206         case PARSEOP_DWORDCONST:
207 
208             Op->Asl.Value.Integer = ACPI_UINT32_MAX;
209             break;
210 
211         /* Don't need to do the QWORD case */
212 
213         default:
214 
215             /* Don't care about others */
216             break;
217         }
218     }
219 
220     Op->Asl.ParseOpcode = (UINT16) ParseOpcode;
221     UtSetParseOpName (Op);
222 
223     /*
224      * For the BYTE, WORD, and DWORD constants, make sure that the integer
225      * that was passed in will actually fit into the data type
226      */
227     switch (ParseOpcode)
228     {
229     case PARSEOP_BYTECONST:
230 
231         UtCheckIntegerRange (Op, 0x00, ACPI_UINT8_MAX);
232         Op->Asl.Value.Integer &= ACPI_UINT8_MAX;
233         break;
234 
235     case PARSEOP_WORDCONST:
236 
237         UtCheckIntegerRange (Op, 0x00, ACPI_UINT16_MAX);
238         Op->Asl.Value.Integer &= ACPI_UINT16_MAX;
239         break;
240 
241     case PARSEOP_DWORDCONST:
242 
243         UtCheckIntegerRange (Op, 0x00, ACPI_UINT32_MAX);
244         Op->Asl.Value.Integer &= ACPI_UINT32_MAX;
245         break;
246 
247     default:
248 
249         /* Don't care about others, don't need to check QWORD */
250 
251         break;
252     }
253 
254     /* Converter: if this is a method invocation, turn off capture comments */
255 
256     if (Gbl_CaptureComments &&
257         (ParseOpcode == PARSEOP_METHODCALL))
258     {
259         Gbl_CommentState.CaptureComments = FALSE;
260     }
261 
262     return (Op);
263 }
264 
265 
266 /*******************************************************************************
267  *
268  * FUNCTION:    TrSetOpFlags
269  *
270  * PARAMETERS:  Op                  - An existing parse op
271  *              Flags               - New flags word
272  *
273  * RETURN:      The updated parser op
274  *
275  * DESCRIPTION: Set bits in the op flags word. Will not clear bits, only set
276  *
277  ******************************************************************************/
278 
279 ACPI_PARSE_OBJECT *
280 TrSetOpFlags (
281     ACPI_PARSE_OBJECT       *Op,
282     UINT32                  Flags)
283 {
284 
285     if (!Op)
286     {
287         return (NULL);
288     }
289 
290     DbgPrint (ASL_PARSE_OUTPUT,
291         "\nSetOpFlags: %s Op %p, %8.8X", Op->Asl.ParseOpName, Op, Flags);
292 
293     TrPrintOpFlags (Flags, ASL_PARSE_OUTPUT);
294     DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
295 
296     Op->Asl.CompileFlags |= Flags;
297     return (Op);
298 }
299 
300 
301 /*******************************************************************************
302  *
303  * FUNCTION:    TrSetOpAmlLength
304  *
305  * PARAMETERS:  Op                  - An existing parse op
306  *              Length              - AML Length
307  *
308  * RETURN:      The updated parser op
309  *
310  * DESCRIPTION: Set the AML Length in a op. Used by the parser to indicate
311  *              the presence of a op that must be reduced to a fixed length
312  *              constant.
313  *
314  ******************************************************************************/
315 
316 ACPI_PARSE_OBJECT *
317 TrSetOpAmlLength (
318     ACPI_PARSE_OBJECT       *Op,
319     UINT32                  Length)
320 {
321 
322     DbgPrint (ASL_PARSE_OUTPUT,
323         "\nSetOpAmlLength: Op %p, %8.8X\n", Op, Length);
324 
325     if (!Op)
326     {
327         return (NULL);
328     }
329 
330     Op->Asl.AmlLength = Length;
331     return (Op);
332 }
333 
334 
335 /*******************************************************************************
336  *
337  * FUNCTION:    TrSetOpParent
338  *
339  * PARAMETERS:  Op                  - To be set to new parent
340  *              ParentOp            - The parent
341  *
342  * RETURN:      None, sets Op parent directly
343  *
344  * DESCRIPTION: Change the parent of a parse op.
345  *
346  ******************************************************************************/
347 
348 void
349 TrSetOpParent (
350     ACPI_PARSE_OBJECT       *Op,
351     ACPI_PARSE_OBJECT       *ParentOp)
352 {
353 
354     Op->Asl.Parent = ParentOp;
355 }
356 
357 
358 /*******************************************************************************
359  *
360  * FUNCTION:    TrSetOpCurrentFilename
361  *
362  * PARAMETERS:  Op                  - An existing parse op
363  *
364  * RETURN:      None
365  *
366  * DESCRIPTION: Save the include file filename. Used for debug output only.
367  *
368  ******************************************************************************/
369 
370 void
371 TrSetOpCurrentFilename (
372     ACPI_PARSE_OBJECT       *Op)
373 {
374 
375     Op->Asl.Filename = Gbl_PreviousIncludeFilename;
376 }
377 
378 
379 /*******************************************************************************
380  *
381  * FUNCTION:    TrSetOpEndLineNumber
382  *
383  * PARAMETERS:  Op                - An existing parse op
384  *
385  * RETURN:      None.
386  *
387  * DESCRIPTION: Set the ending line numbers (file line and logical line) of a
388  *              parse op to the current line numbers.
389  *
390  ******************************************************************************/
391 
392 void
393 TrSetOpEndLineNumber (
394     ACPI_PARSE_OBJECT       *Op)
395 {
396 
397     /* If the end line # is already set, just return */
398 
399     if (Op->Asl.EndLine)
400     {
401         return;
402     }
403 
404     Op->Asl.EndLine = Gbl_CurrentLineNumber;
405     Op->Asl.EndLogicalLine = Gbl_LogicalLineNumber;
406 }
407 
408 
409 /*******************************************************************************
410  *
411  * FUNCTION:    TrLinkOpChildren
412  *
413  * PARAMETERS:  Op                - An existing parse op
414  *              NumChildren        - Number of children to follow
415  *              ...                - A list of child ops to link to the new
416  *                                   op. NumChildren long.
417  *
418  * RETURN:      The updated (linked) op
419  *
420  * DESCRIPTION: Link a group of ops to an existing parse op
421  *
422  ******************************************************************************/
423 
424 ACPI_PARSE_OBJECT *
425 TrLinkOpChildren (
426     ACPI_PARSE_OBJECT       *Op,
427     UINT32                  NumChildren,
428     ...)
429 {
430     ACPI_PARSE_OBJECT       *Child;
431     ACPI_PARSE_OBJECT       *PrevChild;
432     va_list                 ap;
433     UINT32                  i;
434     BOOLEAN                 FirstChild;
435 
436 
437     va_start (ap, NumChildren);
438 
439     TrSetOpEndLineNumber (Op);
440 
441     DbgPrint (ASL_PARSE_OUTPUT,
442         "\nLinkChildren  Line [%u to %u] NewParent %p Child %u Op %s  ",
443         Op->Asl.LineNumber, Op->Asl.EndLine,
444         Op, NumChildren, UtGetOpName(Op->Asl.ParseOpcode));
445 
446     switch (Op->Asl.ParseOpcode)
447     {
448     case PARSEOP_ASL_CODE:
449 
450         Gbl_ParseTreeRoot = Op;
451         Op->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
452         DbgPrint (ASL_PARSE_OUTPUT, "ASLCODE (Tree Completed)->");
453         break;
454 
455     case PARSEOP_DEFINITION_BLOCK:
456 
457         DbgPrint (ASL_PARSE_OUTPUT, "DEFINITION_BLOCK (Tree Completed)->");
458         break;
459 
460     case PARSEOP_OPERATIONREGION:
461 
462         DbgPrint (ASL_PARSE_OUTPUT, "OPREGION->");
463         break;
464 
465     case PARSEOP_OR:
466 
467         DbgPrint (ASL_PARSE_OUTPUT, "OR->");
468         break;
469 
470     default:
471 
472         /* Nothing to do for other opcodes */
473 
474         break;
475     }
476 
477     /* The following is for capturing comments */
478 
479     if(Gbl_CaptureComments)
480     {
481         /*
482          * If there are "regular comments" detected at this point,
483          * then is an endBlk comment. Categorize it as so and distribute
484          * all regular comments to this parse op.
485          */
486         if (Gbl_CommentListHead)
487         {
488             Op->Asl.EndBlkComment = Gbl_CommentListHead;
489             CvDbgPrint ("EndBlk Comment for %s: %s",
490                 Op->Asl.ParseOpName, Gbl_CommentListHead->Comment);
491             Gbl_CommentListHead = NULL;
492             Gbl_CommentListTail = NULL;
493         }
494     }
495 
496     /* Link the new op to it's children */
497 
498     PrevChild = NULL;
499     FirstChild = TRUE;
500     for (i = 0; i < NumChildren; i++)
501     {
502         Child = va_arg (ap, ACPI_PARSE_OBJECT *);
503 
504         if ((Child == PrevChild) && (Child != NULL))
505         {
506             AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Child,
507                 "Child op list invalid");
508             va_end(ap);
509             return (Op);
510         }
511 
512         DbgPrint (ASL_PARSE_OUTPUT, "%p, ", Child);
513 
514         /*
515          * If child is NULL, this means that an optional argument
516          * was omitted. We must create a placeholder with a special
517          * opcode (DEFAULT_ARG) so that the code generator will know
518          * that it must emit the correct default for this argument
519          */
520         if (!Child)
521         {
522             Child = TrAllocateOp (PARSEOP_DEFAULT_ARG);
523         }
524 
525         /* Link first child to parent */
526 
527         if (FirstChild)
528         {
529             FirstChild = FALSE;
530             Op->Asl.Child = Child;
531         }
532 
533         /* Point all children to parent */
534 
535         Child->Asl.Parent = Op;
536 
537         /* Link children in a peer list */
538 
539         if (PrevChild)
540         {
541             PrevChild->Asl.Next = Child;
542         }
543 
544         /*
545          * This child might be a list, point all ops in the list
546          * to the same parent
547          */
548         while (Child->Asl.Next)
549         {
550             Child = Child->Asl.Next;
551             Child->Asl.Parent = Op;
552         }
553 
554         PrevChild = Child;
555     }
556 
557     va_end(ap);
558     DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
559 
560     if(Gbl_CaptureComments)
561     {
562         Gbl_CommentState.LatestParseOp = Op;
563         CvDbgPrint ("TrLinkOpChildren=====Set latest parse op to this op.\n");
564     }
565 
566     return (Op);
567 }
568 
569 
570 /*******************************************************************************
571  *
572  * FUNCTION:    TrLinkPeerOp
573  *
574  * PARAMETERS:  Op1           - First peer
575  *              Op2           - Second peer
576  *
577  * RETURN:      Op1 or the non-null op.
578  *
579  * DESCRIPTION: Link two ops as peers. Handles cases where one peer is null.
580  *
581  ******************************************************************************/
582 
583 ACPI_PARSE_OBJECT *
584 TrLinkPeerOp (
585     ACPI_PARSE_OBJECT       *Op1,
586     ACPI_PARSE_OBJECT       *Op2)
587 {
588     ACPI_PARSE_OBJECT       *Next;
589 
590 
591     DbgPrint (ASL_PARSE_OUTPUT,
592         "\nLinkPeerOp: 1=%p (%s), 2=%p (%s)\n",
593         Op1, Op1 ? UtGetOpName(Op1->Asl.ParseOpcode) : NULL,
594         Op2, Op2 ? UtGetOpName(Op2->Asl.ParseOpcode) : NULL);
595 
596 
597     if ((!Op1) && (!Op2))
598     {
599         DbgPrint (ASL_PARSE_OUTPUT, "\nTwo Null ops!\n");
600         return (Op1);
601     }
602 
603     /* If one of the ops is null, just return the non-null op */
604 
605     if (!Op2)
606     {
607         return (Op1);
608     }
609 
610     if (!Op1)
611     {
612         return (Op2);
613     }
614 
615     if (Op1 == Op2)
616     {
617         DbgPrint (ASL_DEBUG_OUTPUT,
618             "\n************* Internal error, linking op to itself %p\n",
619             Op1);
620         AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op1,
621             "Linking op to itself");
622         return (Op1);
623     }
624 
625     Op1->Asl.Parent = Op2->Asl.Parent;
626 
627     /*
628      * Op 1 may already have a peer list (such as an IF/ELSE pair),
629      * so we must walk to the end of the list and attach the new
630      * peer at the end
631      */
632     Next = Op1;
633     while (Next->Asl.Next)
634     {
635         Next = Next->Asl.Next;
636     }
637 
638     Next->Asl.Next = Op2;
639     return (Op1);
640 }
641 
642 
643 /*******************************************************************************
644  *
645  * FUNCTION:    TrLinkPeerOps
646  *
647  * PARAMETERS:  NumPeers            - The number of ops in the list to follow
648  *              ...                 - A list of ops to link together as peers
649  *
650  * RETURN:      The first op in the list (head of the peer list)
651  *
652  * DESCRIPTION: Link together an arbitrary number of peer ops.
653  *
654  ******************************************************************************/
655 
656 ACPI_PARSE_OBJECT *
657 TrLinkPeerOps (
658     UINT32                  NumPeers,
659     ...)
660 {
661     ACPI_PARSE_OBJECT       *This;
662     ACPI_PARSE_OBJECT       *Next;
663     va_list                 ap;
664     UINT32                  i;
665     ACPI_PARSE_OBJECT       *Start;
666 
667 
668     DbgPrint (ASL_PARSE_OUTPUT,
669         "\nLinkPeerOps: (%u) ", NumPeers);
670 
671     va_start (ap, NumPeers);
672     This = va_arg (ap, ACPI_PARSE_OBJECT *);
673     Start = This;
674 
675     /*
676      * Link all peers
677      */
678     for (i = 0; i < (NumPeers -1); i++)
679     {
680         DbgPrint (ASL_PARSE_OUTPUT, "%u=%p ", (i+1), This);
681 
682         while (This->Asl.Next)
683         {
684             This = This->Asl.Next;
685         }
686 
687         /* Get another peer op */
688 
689         Next = va_arg (ap, ACPI_PARSE_OBJECT *);
690         if (!Next)
691         {
692             Next = TrAllocateOp (PARSEOP_DEFAULT_ARG);
693         }
694 
695         /* link new op to the current op */
696 
697         This->Asl.Next = Next;
698         This = Next;
699     }
700 
701     va_end (ap);
702     DbgPrint (ASL_PARSE_OUTPUT,"\n");
703     return (Start);
704 }
705 
706 
707 /*******************************************************************************
708  *
709  * FUNCTION:    TrLinkChildOp
710  *
711  * PARAMETERS:  Op1           - Parent op
712  *              Op2           - Op to become a child
713  *
714  * RETURN:      The parent op
715  *
716  * DESCRIPTION: Link two ops together as a parent and child
717  *
718  ******************************************************************************/
719 
720 ACPI_PARSE_OBJECT *
721 TrLinkChildOp (
722     ACPI_PARSE_OBJECT       *Op1,
723     ACPI_PARSE_OBJECT       *Op2)
724 {
725     ACPI_PARSE_OBJECT       *Next;
726 
727 
728     DbgPrint (ASL_PARSE_OUTPUT,
729         "\nLinkChildOp: Parent=%p (%s), Child=%p (%s)\n",
730         Op1, Op1 ? UtGetOpName(Op1->Asl.ParseOpcode): NULL,
731         Op2, Op2 ? UtGetOpName(Op2->Asl.ParseOpcode): NULL);
732 
733     /*
734      * Converter: if TrLinkChildOp is called to link a method call,
735      * turn on capture comments as it signifies that we are done parsing
736      * a method call.
737      */
738     if (Gbl_CaptureComments && Op1)
739     {
740         if (Op1->Asl.ParseOpcode == PARSEOP_METHODCALL)
741         {
742             Gbl_CommentState.CaptureComments = TRUE;
743         }
744         Gbl_CommentState.LatestParseOp = Op1;
745     }
746 
747     if (!Op1 || !Op2)
748     {
749         return (Op1);
750     }
751 
752     Op1->Asl.Child = Op2;
753 
754     /* Set the child and all peers of the child to point to the parent */
755 
756     Next = Op2;
757     while (Next)
758     {
759         Next->Asl.Parent = Op1;
760         Next = Next->Asl.Next;
761     }
762 
763     return (Op1);
764 }
765 
766 
767 /*******************************************************************************
768  *
769  * FUNCTION:    TrWalkParseTree
770  *
771  * PARAMETERS:  Op                      - Walk starting point
772  *              Visitation              - Type of walk
773  *              DescendingCallback      - Called during tree descent
774  *              AscendingCallback       - Called during tree ascent
775  *              Context                 - To be passed to the callbacks
776  *
777  * RETURN:      Status from callback(s)
778  *
779  * DESCRIPTION: Walk the entire parse tree.
780  *
781  ******************************************************************************/
782 
783 ACPI_STATUS
784 TrWalkParseTree (
785     ACPI_PARSE_OBJECT       *Op,
786     UINT32                  Visitation,
787     ASL_WALK_CALLBACK       DescendingCallback,
788     ASL_WALK_CALLBACK       AscendingCallback,
789     void                    *Context)
790 {
791     UINT32                  Level;
792     BOOLEAN                 OpPreviouslyVisited;
793     ACPI_PARSE_OBJECT       *StartOp = Op;
794     ACPI_STATUS             Status;
795 
796 
797     if (!Gbl_ParseTreeRoot)
798     {
799         return (AE_OK);
800     }
801 
802     Level = 0;
803     OpPreviouslyVisited = FALSE;
804 
805     switch (Visitation)
806     {
807     case ASL_WALK_VISIT_DOWNWARD:
808 
809         while (Op)
810         {
811             if (!OpPreviouslyVisited)
812             {
813                 /* Let the callback process the op. */
814 
815                 Status = DescendingCallback (Op, Level, Context);
816                 if (ACPI_SUCCESS (Status))
817                 {
818                     /* Visit children first, once */
819 
820                     if (Op->Asl.Child)
821                     {
822                         Level++;
823                         Op = Op->Asl.Child;
824                         continue;
825                     }
826                 }
827                 else if (Status != AE_CTRL_DEPTH)
828                 {
829                     /* Exit immediately on any error */
830 
831                     return (Status);
832                 }
833             }
834 
835             /* Terminate walk at start op */
836 
837             if (Op == StartOp)
838             {
839                 break;
840             }
841 
842             /* No more children, visit peers */
843 
844             if (Op->Asl.Next)
845             {
846                 Op = Op->Asl.Next;
847                 OpPreviouslyVisited = FALSE;
848             }
849             else
850             {
851                 /* No children or peers, re-visit parent */
852 
853                 if (Level != 0 )
854                 {
855                     Level--;
856                 }
857                 Op = Op->Asl.Parent;
858                 OpPreviouslyVisited = TRUE;
859             }
860         }
861         break;
862 
863     case ASL_WALK_VISIT_UPWARD:
864 
865         while (Op)
866         {
867             /* Visit leaf op (no children) or parent op on return trip */
868 
869             if ((!Op->Asl.Child) ||
870                 (OpPreviouslyVisited))
871             {
872                 /* Let the callback process the op. */
873 
874                 Status = AscendingCallback (Op, Level, Context);
875                 if (ACPI_FAILURE (Status))
876                 {
877                     return (Status);
878                 }
879             }
880             else
881             {
882                 /* Visit children first, once */
883 
884                 Level++;
885                 Op = Op->Asl.Child;
886                 continue;
887             }
888 
889             /* Terminate walk at start op */
890 
891             if (Op == StartOp)
892             {
893                 break;
894             }
895 
896             /* No more children, visit peers */
897 
898             if (Op->Asl.Next)
899             {
900                 Op = Op->Asl.Next;
901                 OpPreviouslyVisited = FALSE;
902             }
903             else
904             {
905                 /* No children or peers, re-visit parent */
906 
907                 if (Level != 0 )
908                 {
909                     Level--;
910                 }
911                 Op = Op->Asl.Parent;
912                 OpPreviouslyVisited = TRUE;
913             }
914         }
915         break;
916 
917      case ASL_WALK_VISIT_TWICE:
918 
919         while (Op)
920         {
921             if (OpPreviouslyVisited)
922             {
923                 Status = AscendingCallback (Op, Level, Context);
924                 if (ACPI_FAILURE (Status))
925                 {
926                     return (Status);
927                 }
928             }
929             else
930             {
931                 /* Let the callback process the op. */
932 
933                 Status = DescendingCallback (Op, Level, Context);
934                 if (ACPI_SUCCESS (Status))
935                 {
936                     /* Visit children first, once */
937 
938                     if (Op->Asl.Child)
939                     {
940                         Level++;
941                         Op = Op->Asl.Child;
942                         continue;
943                     }
944                 }
945                 else if (Status != AE_CTRL_DEPTH)
946                 {
947                     /* Exit immediately on any error */
948 
949                     return (Status);
950                 }
951             }
952 
953             /* Terminate walk at start op */
954 
955             if (Op == StartOp)
956             {
957                 break;
958             }
959 
960             /* No more children, visit peers */
961 
962             if (Op->Asl.Next)
963             {
964                 Op = Op->Asl.Next;
965                 OpPreviouslyVisited = FALSE;
966             }
967             else
968             {
969                 /* No children or peers, re-visit parent */
970 
971                 if (Level != 0 )
972                 {
973                     Level--;
974                 }
975                 Op = Op->Asl.Parent;
976                 OpPreviouslyVisited = TRUE;
977             }
978         }
979         break;
980 
981     default:
982         /* No other types supported */
983         break;
984     }
985 
986     /* If we get here, the walk completed with no errors */
987 
988     return (AE_OK);
989 }
990