xref: /freebsd/sys/contrib/dev/acpica/compiler/asltree.c (revision 722b16673c40aedf280895f2f2f676bb494518d7)
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 - 2023, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
153 #include "aslcompiler.y.h"
154 #include <contrib/dev/acpica/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 *
TrSetOpIntegerValue(UINT32 ParseOpcode,ACPI_PARSE_OBJECT * Op)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 (AcpiGbl_CaptureComments &&
257         (ParseOpcode == PARSEOP_METHODCALL))
258     {
259         AslGbl_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 *
TrSetOpFlags(ACPI_PARSE_OBJECT * Op,UINT32 Flags)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 *
TrSetOpAmlLength(ACPI_PARSE_OBJECT * Op,UINT32 Length)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
TrSetOpParent(ACPI_PARSE_OBJECT * Op,ACPI_PARSE_OBJECT * ParentOp)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
TrSetOpCurrentFilename(ACPI_PARSE_OBJECT * Op)371 TrSetOpCurrentFilename (
372     ACPI_PARSE_OBJECT       *Op)
373 {
374 
375     Op->Asl.Filename = AslGbl_PreviousIncludeFilename;
376 }
377 
378 
379 /*******************************************************************************
380  *
381  * FUNCTION:    TrSetOpIntegerWidth
382  *
383  * PARAMETERS:  Op                  - An existing parse op
384  *
385  * RETURN:      None
386  *
387  * DESCRIPTION:
388  *
389  ******************************************************************************/
390 
391 void
TrSetOpIntegerWidth(ACPI_PARSE_OBJECT * TableSignatureOp,ACPI_PARSE_OBJECT * RevisionOp)392 TrSetOpIntegerWidth (
393     ACPI_PARSE_OBJECT       *TableSignatureOp,
394     ACPI_PARSE_OBJECT       *RevisionOp)
395 {
396 
397     /* TBD: Check table sig? (DSDT vs. SSDT) */
398 
399     /* Handle command-line version override */
400 
401     if (AslGbl_RevisionOverride)
402     {
403         AcpiUtSetIntegerWidth (AslGbl_RevisionOverride);
404     }
405     else
406     {
407         AcpiUtSetIntegerWidth ((UINT8) RevisionOp->Asl.Value.Integer);
408     }
409 }
410 
411 
412 /*******************************************************************************
413  *
414  * FUNCTION:    TrSetOpEndLineNumber
415  *
416  * PARAMETERS:  Op                - An existing parse op
417  *
418  * RETURN:      None.
419  *
420  * DESCRIPTION: Set the ending line numbers (file line and logical line) of a
421  *              parse op to the current line numbers.
422  *
423  ******************************************************************************/
424 
425 void
TrSetOpEndLineNumber(ACPI_PARSE_OBJECT * Op)426 TrSetOpEndLineNumber (
427     ACPI_PARSE_OBJECT       *Op)
428 {
429 
430     /* If the end line # is already set, just return */
431 
432     if (Op->Asl.EndLine)
433     {
434         return;
435     }
436 
437     Op->Asl.EndLine = AslGbl_CurrentLineNumber;
438     Op->Asl.EndLogicalLine = AslGbl_LogicalLineNumber;
439 }
440 
441 
442 /*******************************************************************************
443  *
444  * FUNCTION:    TrLinkOpChildren
445  *
446  * PARAMETERS:  Op                - An existing parse op
447  *              NumChildren        - Number of children to follow
448  *              ...                - A list of child ops to link to the new
449  *                                   op. NumChildren long.
450  *
451  * RETURN:      The updated (linked) op
452  *
453  * DESCRIPTION: Link a group of ops to an existing parse op
454  *
455  ******************************************************************************/
456 
457 ACPI_PARSE_OBJECT *
TrLinkOpChildren(ACPI_PARSE_OBJECT * Op,UINT32 NumChildren,...)458 TrLinkOpChildren (
459     ACPI_PARSE_OBJECT       *Op,
460     UINT32                  NumChildren,
461     ...)
462 {
463     ACPI_PARSE_OBJECT       *Child;
464     ACPI_PARSE_OBJECT       *PrevChild;
465     ACPI_PARSE_OBJECT       *LastSibling;
466     va_list                 ap;
467     UINT32                  i;
468     BOOLEAN                 FirstChild;
469 
470     ACPI_FUNCTION_NAME (TrLinkOpChildren);
471 
472     va_start (ap, NumChildren);
473 
474     TrSetOpEndLineNumber (Op);
475 
476     DbgPrint (ASL_PARSE_OUTPUT,
477         "\nLinkChildren  Line [%u to %u] NewParent %p Child %u Op %s  ",
478         Op->Asl.LineNumber, Op->Asl.EndLine,
479         Op, NumChildren, UtGetOpName(Op->Asl.ParseOpcode));
480 
481     switch (Op->Asl.ParseOpcode)
482     {
483     case PARSEOP_ASL_CODE:
484 
485         if (!AslGbl_ParseTreeRoot)
486         {
487             DbgPrint (ASL_PARSE_OUTPUT, "Creating first Definition Block\n");
488             AslGbl_ParseTreeRoot = Op;
489             Op->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
490         }
491         else
492         {
493             DbgPrint (ASL_PARSE_OUTPUT, "Creating subsequent Definition Block\n");
494             Op = AslGbl_ParseTreeRoot;
495         }
496 
497         DbgPrint (ASL_PARSE_OUTPUT, "ASLCODE (Tree Completed)->");
498         break;
499 
500     case PARSEOP_DEFINITION_BLOCK:
501 
502         DbgPrint (ASL_PARSE_OUTPUT, "DEFINITION_BLOCK (Tree Completed)->");
503         break;
504 
505     case PARSEOP_OPERATIONREGION:
506 
507         DbgPrint (ASL_PARSE_OUTPUT, "OPREGION->");
508         break;
509 
510     case PARSEOP_OR:
511 
512         DbgPrint (ASL_PARSE_OUTPUT, "OR->");
513         break;
514 
515     default:
516 
517         /* Nothing to do for other opcodes */
518 
519         break;
520     }
521 
522     /* The following is for capturing comments */
523 
524     if (AcpiGbl_CaptureComments)
525     {
526         /*
527          * If there are "regular comments" detected at this point,
528          * then is an endBlk comment. Categorize it as so and distribute
529          * all regular comments to this parse op.
530          */
531         if (AslGbl_CommentListHead)
532         {
533             Op->Asl.EndBlkComment = AslGbl_CommentListHead;
534             CvDbgPrint ("EndBlk Comment for %s: %s",
535                 Op->Asl.ParseOpName, AslGbl_CommentListHead->Comment);
536             AslGbl_CommentListHead = NULL;
537             AslGbl_CommentListTail = NULL;
538         }
539     }
540 
541     /* Link the new op to it's children */
542 
543     PrevChild = NULL;
544     FirstChild = TRUE;
545     for (i = 0; i < NumChildren; i++)
546     {
547         Child = va_arg (ap, ACPI_PARSE_OBJECT *);
548 
549         if ((Child == PrevChild) && (Child != NULL))
550         {
551             AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Child,
552                 "Child op list invalid");
553             va_end(ap);
554             return (Op);
555         }
556 
557         DbgPrint (ASL_PARSE_OUTPUT, "%p, ", Child);
558 
559         /*
560          * If child is NULL, this means that an optional argument
561          * was omitted. We must create a placeholder with a special
562          * opcode (DEFAULT_ARG) so that the code generator will know
563          * that it must emit the correct default for this argument
564          */
565         if (!Child)
566         {
567             Child = TrAllocateOp (PARSEOP_DEFAULT_ARG);
568         }
569 
570         /* Link first child to parent */
571 
572         if (FirstChild)
573         {
574             FirstChild = FALSE;
575 
576             /*
577              * In the case that multiple definition blocks are being compiled,
578              * append the definition block to the end of the child list as the
579              * last sibling. This is done to facilitate namespace cross-
580              * reference between multiple definition blocks.
581              */
582             if (Op->Asl.Child &&
583                 (Op->Asl.Child->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK))
584             {
585                 LastSibling = Op->Asl.Child;
586                 while (LastSibling->Asl.Next)
587                 {
588                     LastSibling = LastSibling->Asl.Next;
589                 }
590                 LastSibling->Asl.Next = Child;
591             }
592             else
593             {
594                 Op->Asl.Child = Child;
595             }
596         }
597 
598         /* Point all children to parent */
599 
600         Child->Asl.Parent = Op;
601 
602         /* Link children in a peer list */
603 
604         if (PrevChild)
605         {
606             PrevChild->Asl.Next = Child;
607         }
608 
609         /*
610          * This child might be a list, point all ops in the list
611          * to the same parent
612          */
613         while (Child->Asl.Next)
614         {
615             Child = Child->Asl.Next;
616             Child->Asl.Parent = Op;
617         }
618 
619         PrevChild = Child;
620     }
621 
622     va_end(ap);
623     DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
624 
625     if (AcpiGbl_CaptureComments)
626     {
627         AslGbl_CommentState.LatestParseOp = Op;
628         CvDbgPrint ("%s=====Set latest parse op to this op.\n",  ACPI_GET_FUNCTION_NAME);
629     }
630 
631     return (Op);
632 }
633 
634 
635 /*******************************************************************************
636  *
637  * FUNCTION:    TrLinkPeerOp
638  *
639  * PARAMETERS:  Op1           - First peer
640  *              Op2           - Second peer
641  *
642  * RETURN:      Op1 or the non-null op.
643  *
644  * DESCRIPTION: Link two ops as peers. Handles cases where one peer is null.
645  *
646  ******************************************************************************/
647 
648 ACPI_PARSE_OBJECT *
TrLinkPeerOp(ACPI_PARSE_OBJECT * Op1,ACPI_PARSE_OBJECT * Op2)649 TrLinkPeerOp (
650     ACPI_PARSE_OBJECT       *Op1,
651     ACPI_PARSE_OBJECT       *Op2)
652 {
653     ACPI_PARSE_OBJECT       *Next;
654 
655 
656     DbgPrint (ASL_PARSE_OUTPUT,
657         "\nLinkPeerOp: 1=%p (%s), 2=%p (%s)\n",
658         Op1, Op1 ? UtGetOpName(Op1->Asl.ParseOpcode) : NULL,
659         Op2, Op2 ? UtGetOpName(Op2->Asl.ParseOpcode) : NULL);
660 
661 
662     if ((!Op1) && (!Op2))
663     {
664         DbgPrint (ASL_PARSE_OUTPUT, "\nTwo Null ops!\n");
665         return (Op1);
666     }
667 
668     /* If one of the ops is null, just return the non-null op */
669 
670     if (!Op2)
671     {
672         return (Op1);
673     }
674 
675     if (!Op1)
676     {
677         return (Op2);
678     }
679 
680     if (Op1 == Op2)
681     {
682         DbgPrint (ASL_DEBUG_OUTPUT,
683             "\n************* Internal error, linking op to itself %p\n",
684             Op1);
685         AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op1,
686             "Linking op to itself");
687         return (Op1);
688     }
689 
690     Op1->Asl.Parent = Op2->Asl.Parent;
691 
692     /*
693      * Op 1 may already have a peer list (such as an IF/ELSE pair),
694      * so we must walk to the end of the list and attach the new
695      * peer at the end
696      */
697     Next = Op1;
698     while (Next->Asl.Next)
699     {
700         Next = Next->Asl.Next;
701     }
702 
703     Next->Asl.Next = Op2;
704     return (Op1);
705 }
706 
707 
708 /*******************************************************************************
709  *
710  * FUNCTION:    TrLinkPeerOps
711  *
712  * PARAMETERS:  NumPeers            - The number of ops in the list to follow
713  *              ...                 - A list of ops to link together as peers
714  *
715  * RETURN:      The first op in the list (head of the peer list)
716  *
717  * DESCRIPTION: Link together an arbitrary number of peer ops.
718  *
719  ******************************************************************************/
720 
721 ACPI_PARSE_OBJECT *
TrLinkPeerOps(UINT32 NumPeers,...)722 TrLinkPeerOps (
723     UINT32                  NumPeers,
724     ...)
725 {
726     ACPI_PARSE_OBJECT       *This;
727     ACPI_PARSE_OBJECT       *Next;
728     va_list                 ap;
729     UINT32                  i;
730     ACPI_PARSE_OBJECT       *Start;
731 
732 
733     DbgPrint (ASL_PARSE_OUTPUT,
734         "\nLinkPeerOps: (%u) ", NumPeers);
735 
736     va_start (ap, NumPeers);
737     This = va_arg (ap, ACPI_PARSE_OBJECT *);
738     Start = This;
739 
740     /*
741      * Link all peers
742      */
743     for (i = 0; i < (NumPeers -1); i++)
744     {
745         DbgPrint (ASL_PARSE_OUTPUT, "%u=%p ", (i+1), This);
746 
747         while (This->Asl.Next)
748         {
749             This = This->Asl.Next;
750         }
751 
752         /* Get another peer op */
753 
754         Next = va_arg (ap, ACPI_PARSE_OBJECT *);
755         if (!Next)
756         {
757             Next = TrAllocateOp (PARSEOP_DEFAULT_ARG);
758         }
759 
760         /* link new op to the current op */
761 
762         This->Asl.Next = Next;
763         This = Next;
764     }
765 
766     va_end (ap);
767     DbgPrint (ASL_PARSE_OUTPUT,"\n");
768     return (Start);
769 }
770 
771 
772 /*******************************************************************************
773  *
774  * FUNCTION:    TrLinkChildOp
775  *
776  * PARAMETERS:  Op1           - Parent op
777  *              Op2           - Op to become a child
778  *
779  * RETURN:      The parent op
780  *
781  * DESCRIPTION: Link two ops together as a parent and child
782  *
783  ******************************************************************************/
784 
785 ACPI_PARSE_OBJECT *
TrLinkChildOp(ACPI_PARSE_OBJECT * Op1,ACPI_PARSE_OBJECT * Op2)786 TrLinkChildOp (
787     ACPI_PARSE_OBJECT       *Op1,
788     ACPI_PARSE_OBJECT       *Op2)
789 {
790     ACPI_PARSE_OBJECT       *Next;
791 
792 
793     DbgPrint (ASL_PARSE_OUTPUT,
794         "\nLinkChildOp: Parent=%p (%s), Child=%p (%s)\n",
795         Op1, Op1 ? UtGetOpName(Op1->Asl.ParseOpcode): NULL,
796         Op2, Op2 ? UtGetOpName(Op2->Asl.ParseOpcode): NULL);
797 
798     /*
799      * Converter: if TrLinkChildOp is called to link a method call,
800      * turn on capture comments as it signifies that we are done parsing
801      * a method call.
802      */
803     if (AcpiGbl_CaptureComments && Op1)
804     {
805         if (Op1->Asl.ParseOpcode == PARSEOP_METHODCALL)
806         {
807             AslGbl_CommentState.CaptureComments = TRUE;
808         }
809         AslGbl_CommentState.LatestParseOp = Op1;
810     }
811 
812     if (!Op1 || !Op2)
813     {
814         return (Op1);
815     }
816 
817     Op1->Asl.Child = Op2;
818 
819     /* Set the child and all peers of the child to point to the parent */
820 
821     Next = Op2;
822     while (Next)
823     {
824         Next->Asl.Parent = Op1;
825         Next = Next->Asl.Next;
826     }
827 
828     return (Op1);
829 }
830 
831 
832 /*******************************************************************************
833  *
834  * FUNCTION:    TrWalkParseTree
835  *
836  * PARAMETERS:  Op                      - Walk starting point
837  *              Visitation              - Type of walk
838  *              DescendingCallback      - Called during tree descent
839  *              AscendingCallback       - Called during tree ascent
840  *              Context                 - To be passed to the callbacks
841  *
842  * RETURN:      Status from callback(s)
843  *
844  * DESCRIPTION: Walk the entire parse tree.
845  *
846  ******************************************************************************/
847 
848 ACPI_STATUS
TrWalkParseTree(ACPI_PARSE_OBJECT * Op,UINT32 Visitation,ASL_WALK_CALLBACK DescendingCallback,ASL_WALK_CALLBACK AscendingCallback,void * Context)849 TrWalkParseTree (
850     ACPI_PARSE_OBJECT       *Op,
851     UINT32                  Visitation,
852     ASL_WALK_CALLBACK       DescendingCallback,
853     ASL_WALK_CALLBACK       AscendingCallback,
854     void                    *Context)
855 {
856     UINT32                  Level;
857     BOOLEAN                 OpPreviouslyVisited;
858     ACPI_PARSE_OBJECT       *StartOp = Op;
859     ACPI_STATUS             Status;
860     ACPI_PARSE_OBJECT       *Restore = NULL;
861     BOOLEAN                 WalkOneDefinitionBlock = Visitation & ASL_WALK_VISIT_DB_SEPARATELY;
862 
863 
864     if (!AslGbl_ParseTreeRoot)
865     {
866         return (AE_OK);
867     }
868 
869     Level = 0;
870     OpPreviouslyVisited = FALSE;
871 
872     if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK &&
873         WalkOneDefinitionBlock)
874     {
875         Restore = Op->Asl.Next;
876         Op->Asl.Next = NULL;
877     }
878     switch (Visitation & ~ASL_WALK_VISIT_DB_SEPARATELY)
879     {
880     case ASL_WALK_VISIT_DOWNWARD:
881 
882         while (Op)
883         {
884             if (!OpPreviouslyVisited)
885             {
886                 /* Let the callback process the op. */
887 
888                 Status = DescendingCallback (Op, Level, Context);
889                 if (ACPI_SUCCESS (Status))
890                 {
891                     /* Visit children first, once */
892 
893                     if (Op->Asl.Child)
894                     {
895                         Level++;
896                         Op = Op->Asl.Child;
897                         continue;
898                     }
899                 }
900                 else if (Status != AE_CTRL_DEPTH)
901                 {
902                     /* Exit immediately on any error */
903 
904                     goto ErrorExit;
905                 }
906             }
907 
908             /* Terminate walk at start op */
909 
910             if (Op == StartOp)
911             {
912                 break;
913             }
914 
915             /* No more children, visit peers */
916 
917             if (Op->Asl.Next)
918             {
919                 Op = Op->Asl.Next;
920                 OpPreviouslyVisited = FALSE;
921             }
922             else
923             {
924                 /* No children or peers, re-visit parent */
925 
926                 if (Level != 0 )
927                 {
928                     Level--;
929                 }
930                 Op = Op->Asl.Parent;
931                 OpPreviouslyVisited = TRUE;
932             }
933         }
934         break;
935 
936     case ASL_WALK_VISIT_UPWARD:
937 
938         while (Op)
939         {
940             /* Visit leaf op (no children) or parent op on return trip */
941 
942             if ((!Op->Asl.Child) ||
943                 (OpPreviouslyVisited))
944             {
945                 /* Let the callback process the op. */
946 
947                 Status = AscendingCallback (Op, Level, Context);
948                 if (ACPI_FAILURE (Status))
949                 {
950                     goto ErrorExit;
951                 }
952             }
953             else
954             {
955                 /* Visit children first, once */
956 
957                 Level++;
958                 Op = Op->Asl.Child;
959                 continue;
960             }
961 
962             /* Terminate walk at start op */
963 
964             if (Op == StartOp)
965             {
966                 break;
967             }
968 
969             /* No more children, visit peers */
970 
971             if (Op->Asl.Next)
972             {
973                 Op = Op->Asl.Next;
974                 OpPreviouslyVisited = FALSE;
975             }
976             else
977             {
978                 /* No children or peers, re-visit parent */
979 
980                 if (Level != 0 )
981                 {
982                     Level--;
983                 }
984                 Op = Op->Asl.Parent;
985                 OpPreviouslyVisited = TRUE;
986             }
987         }
988         break;
989 
990      case ASL_WALK_VISIT_TWICE:
991 
992         while (Op)
993         {
994             if (OpPreviouslyVisited)
995             {
996                 Status = AscendingCallback (Op, Level, Context);
997                 if (ACPI_FAILURE (Status))
998                 {
999                     goto ErrorExit;
1000                 }
1001             }
1002             else
1003             {
1004                 /* Let the callback process the op. */
1005 
1006                 Status = DescendingCallback (Op, Level, Context);
1007                 if (ACPI_SUCCESS (Status))
1008                 {
1009                     /* Visit children first, once */
1010 
1011                     if (Op->Asl.Child)
1012                     {
1013                         Level++;
1014                         Op = Op->Asl.Child;
1015                         continue;
1016                     }
1017                 }
1018                 else if (Status != AE_CTRL_DEPTH)
1019                 {
1020                     /* Exit immediately on any error */
1021 
1022                     goto ErrorExit;
1023                 }
1024             }
1025 
1026             /* Terminate walk at start op */
1027 
1028             if (Op == StartOp)
1029             {
1030                 break;
1031             }
1032 
1033             /* No more children, visit peers */
1034 
1035             if (Op->Asl.Next)
1036             {
1037                 Op = Op->Asl.Next;
1038                 OpPreviouslyVisited = FALSE;
1039             }
1040             else
1041             {
1042                 /* No children or peers, re-visit parent */
1043 
1044                 if (Level != 0 )
1045                 {
1046                     Level--;
1047                 }
1048                 Op = Op->Asl.Parent;
1049                 OpPreviouslyVisited = TRUE;
1050             }
1051         }
1052         break;
1053 
1054     default:
1055         /* No other types supported */
1056         break;
1057     }
1058 
1059     /* If we get here, the walk completed with no errors */
1060 
1061     if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK &&
1062         WalkOneDefinitionBlock)
1063     {
1064         Op->Asl.Next = Restore;
1065     }
1066 
1067     return (AE_OK);
1068 
1069 ErrorExit:
1070 
1071     if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK &&
1072         WalkOneDefinitionBlock)
1073     {
1074         Op->Asl.Next = Restore;
1075     }
1076     return (Status);
1077 }
1078