Sync ACPICA with Intel's version 20161222.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / parser / pstree.c
bloba38eb435a28b0efca1925ccfb422373138feb4f9
1 /******************************************************************************
3 * Module Name: pstree - Parser op tree manipulation/traversal/search
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, 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.
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acparser.h"
47 #include "amlcode.h"
49 #define _COMPONENT ACPI_PARSER
50 ACPI_MODULE_NAME ("pstree")
52 /* Local prototypes */
54 #ifdef ACPI_OBSOLETE_FUNCTIONS
55 ACPI_PARSE_OBJECT *
56 AcpiPsGetChild (
57 ACPI_PARSE_OBJECT *op);
58 #endif
61 /*******************************************************************************
63 * FUNCTION: AcpiPsGetArg
65 * PARAMETERS: Op - Get an argument for this op
66 * Argn - Nth argument to get
68 * RETURN: The argument (as an Op object). NULL if argument does not exist
70 * DESCRIPTION: Get the specified op's argument.
72 ******************************************************************************/
74 ACPI_PARSE_OBJECT *
75 AcpiPsGetArg (
76 ACPI_PARSE_OBJECT *Op,
77 UINT32 Argn)
79 ACPI_PARSE_OBJECT *Arg = NULL;
80 const ACPI_OPCODE_INFO *OpInfo;
83 ACPI_FUNCTION_ENTRY ();
86 if (Op->Common.AmlOpcode == AML_INT_CONNECTION_OP)
88 return (Op->Common.Value.Arg);
91 /* Get the info structure for this opcode */
93 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
94 if (OpInfo->Class == AML_CLASS_UNKNOWN)
96 /* Invalid opcode or ASCII character */
98 return (NULL);
101 /* Check if this opcode requires argument sub-objects */
103 if (!(OpInfo->Flags & AML_HAS_ARGS))
105 /* Has no linked argument objects */
107 return (NULL);
110 /* Get the requested argument object */
112 Arg = Op->Common.Value.Arg;
113 while (Arg && Argn)
115 Argn--;
116 Arg = Arg->Common.Next;
119 return (Arg);
123 /*******************************************************************************
125 * FUNCTION: AcpiPsAppendArg
127 * PARAMETERS: Op - Append an argument to this Op.
128 * Arg - Argument Op to append
130 * RETURN: None.
132 * DESCRIPTION: Append an argument to an op's argument list (a NULL arg is OK)
134 ******************************************************************************/
136 void
137 AcpiPsAppendArg (
138 ACPI_PARSE_OBJECT *Op,
139 ACPI_PARSE_OBJECT *Arg)
141 ACPI_PARSE_OBJECT *PrevArg;
142 const ACPI_OPCODE_INFO *OpInfo;
145 ACPI_FUNCTION_TRACE (PsAppendArg);
148 if (!Op)
150 return_VOID;
153 /* Get the info structure for this opcode */
155 OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
156 if (OpInfo->Class == AML_CLASS_UNKNOWN)
158 /* Invalid opcode */
160 ACPI_ERROR ((AE_INFO, "Invalid AML Opcode: 0x%2.2X",
161 Op->Common.AmlOpcode));
162 return_VOID;
165 /* Check if this opcode requires argument sub-objects */
167 if (!(OpInfo->Flags & AML_HAS_ARGS))
169 /* Has no linked argument objects */
171 return_VOID;
174 /* Append the argument to the linked argument list */
176 if (Op->Common.Value.Arg)
178 /* Append to existing argument list */
180 PrevArg = Op->Common.Value.Arg;
181 while (PrevArg->Common.Next)
183 PrevArg = PrevArg->Common.Next;
185 PrevArg->Common.Next = Arg;
187 else
189 /* No argument list, this will be the first argument */
191 Op->Common.Value.Arg = Arg;
194 /* Set the parent in this arg and any args linked after it */
196 while (Arg)
198 Arg->Common.Parent = Op;
199 Arg = Arg->Common.Next;
201 Op->Common.ArgListLength++;
204 return_VOID;
208 /*******************************************************************************
210 * FUNCTION: AcpiPsGetDepthNext
212 * PARAMETERS: Origin - Root of subtree to search
213 * Op - Last (previous) Op that was found
215 * RETURN: Next Op found in the search.
217 * DESCRIPTION: Get next op in tree (walking the tree in depth-first order)
218 * Return NULL when reaching "origin" or when walking up from root
220 ******************************************************************************/
222 ACPI_PARSE_OBJECT *
223 AcpiPsGetDepthNext (
224 ACPI_PARSE_OBJECT *Origin,
225 ACPI_PARSE_OBJECT *Op)
227 ACPI_PARSE_OBJECT *Next = NULL;
228 ACPI_PARSE_OBJECT *Parent;
229 ACPI_PARSE_OBJECT *Arg;
232 ACPI_FUNCTION_ENTRY ();
235 if (!Op)
237 return (NULL);
240 /* Look for an argument or child */
242 Next = AcpiPsGetArg (Op, 0);
243 if (Next)
245 return (Next);
248 /* Look for a sibling */
250 Next = Op->Common.Next;
251 if (Next)
253 return (Next);
256 /* Look for a sibling of parent */
258 Parent = Op->Common.Parent;
260 while (Parent)
262 Arg = AcpiPsGetArg (Parent, 0);
263 while (Arg && (Arg != Origin) && (Arg != Op))
265 Arg = Arg->Common.Next;
268 if (Arg == Origin)
270 /* Reached parent of origin, end search */
272 return (NULL);
275 if (Parent->Common.Next)
277 /* Found sibling of parent */
279 return (Parent->Common.Next);
282 Op = Parent;
283 Parent = Parent->Common.Parent;
286 return (Next);
290 #ifdef ACPI_OBSOLETE_FUNCTIONS
291 /*******************************************************************************
293 * FUNCTION: AcpiPsGetChild
295 * PARAMETERS: Op - Get the child of this Op
297 * RETURN: Child Op, Null if none is found.
299 * DESCRIPTION: Get op's children or NULL if none
301 ******************************************************************************/
303 ACPI_PARSE_OBJECT *
304 AcpiPsGetChild (
305 ACPI_PARSE_OBJECT *Op)
307 ACPI_PARSE_OBJECT *Child = NULL;
310 ACPI_FUNCTION_ENTRY ();
313 switch (Op->Common.AmlOpcode)
315 case AML_SCOPE_OP:
316 case AML_ELSE_OP:
317 case AML_DEVICE_OP:
318 case AML_THERMAL_ZONE_OP:
319 case AML_INT_METHODCALL_OP:
321 Child = AcpiPsGetArg (Op, 0);
322 break;
324 case AML_BUFFER_OP:
325 case AML_PACKAGE_OP:
326 case AML_METHOD_OP:
327 case AML_IF_OP:
328 case AML_WHILE_OP:
329 case AML_FIELD_OP:
331 Child = AcpiPsGetArg (Op, 1);
332 break;
334 case AML_POWER_RES_OP:
335 case AML_INDEX_FIELD_OP:
337 Child = AcpiPsGetArg (Op, 2);
338 break;
340 case AML_PROCESSOR_OP:
341 case AML_BANK_FIELD_OP:
343 Child = AcpiPsGetArg (Op, 3);
344 break;
346 default:
348 /* All others have no children */
350 break;
353 return (Child);
355 #endif