pstree.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /******************************************************************************
  2. *
  3. * Module Name: pstree - Parser op tree manipulation/traversal/search
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acparser.h"
  45. #include "amlcode.h"
  46. #include "acconvert.h"
  47. #define _COMPONENT ACPI_PARSER
  48. ACPI_MODULE_NAME("pstree")
  49. /* Local prototypes */
  50. #ifdef ACPI_OBSOLETE_FUNCTIONS
  51. union acpi_parse_object *acpi_ps_get_child(union acpi_parse_object *op);
  52. #endif
  53. /*******************************************************************************
  54. *
  55. * FUNCTION: acpi_ps_get_arg
  56. *
  57. * PARAMETERS: op - Get an argument for this op
  58. * argn - Nth argument to get
  59. *
  60. * RETURN: The argument (as an Op object). NULL if argument does not exist
  61. *
  62. * DESCRIPTION: Get the specified op's argument.
  63. *
  64. ******************************************************************************/
  65. union acpi_parse_object *acpi_ps_get_arg(union acpi_parse_object *op, u32 argn)
  66. {
  67. union acpi_parse_object *arg = NULL;
  68. const struct acpi_opcode_info *op_info;
  69. ACPI_FUNCTION_ENTRY();
  70. /*
  71. if (Op->Common.aml_opcode == AML_INT_CONNECTION_OP)
  72. {
  73. return (Op->Common.Value.Arg);
  74. }
  75. */
  76. /* Get the info structure for this opcode */
  77. op_info = acpi_ps_get_opcode_info(op->common.aml_opcode);
  78. if (op_info->class == AML_CLASS_UNKNOWN) {
  79. /* Invalid opcode or ASCII character */
  80. return (NULL);
  81. }
  82. /* Check if this opcode requires argument sub-objects */
  83. if (!(op_info->flags & AML_HAS_ARGS)) {
  84. /* Has no linked argument objects */
  85. return (NULL);
  86. }
  87. /* Get the requested argument object */
  88. arg = op->common.value.arg;
  89. while (arg && argn) {
  90. argn--;
  91. arg = arg->common.next;
  92. }
  93. return (arg);
  94. }
  95. /*******************************************************************************
  96. *
  97. * FUNCTION: acpi_ps_append_arg
  98. *
  99. * PARAMETERS: op - Append an argument to this Op.
  100. * arg - Argument Op to append
  101. *
  102. * RETURN: None.
  103. *
  104. * DESCRIPTION: Append an argument to an op's argument list (a NULL arg is OK)
  105. *
  106. ******************************************************************************/
  107. void
  108. acpi_ps_append_arg(union acpi_parse_object *op, union acpi_parse_object *arg)
  109. {
  110. union acpi_parse_object *prev_arg;
  111. const struct acpi_opcode_info *op_info;
  112. ACPI_FUNCTION_TRACE(ps_append_arg);
  113. if (!op) {
  114. return_VOID;
  115. }
  116. /* Get the info structure for this opcode */
  117. op_info = acpi_ps_get_opcode_info(op->common.aml_opcode);
  118. if (op_info->class == AML_CLASS_UNKNOWN) {
  119. /* Invalid opcode */
  120. ACPI_ERROR((AE_INFO, "Invalid AML Opcode: 0x%2.2X",
  121. op->common.aml_opcode));
  122. return_VOID;
  123. }
  124. /* Check if this opcode requires argument sub-objects */
  125. if (!(op_info->flags & AML_HAS_ARGS)) {
  126. /* Has no linked argument objects */
  127. return_VOID;
  128. }
  129. /* Append the argument to the linked argument list */
  130. if (op->common.value.arg) {
  131. /* Append to existing argument list */
  132. prev_arg = op->common.value.arg;
  133. while (prev_arg->common.next) {
  134. prev_arg = prev_arg->common.next;
  135. }
  136. prev_arg->common.next = arg;
  137. } else {
  138. /* No argument list, this will be the first argument */
  139. op->common.value.arg = arg;
  140. }
  141. /* Set the parent in this arg and any args linked after it */
  142. while (arg) {
  143. arg->common.parent = op;
  144. arg = arg->common.next;
  145. op->common.arg_list_length++;
  146. }
  147. return_VOID;
  148. }
  149. /*******************************************************************************
  150. *
  151. * FUNCTION: acpi_ps_get_depth_next
  152. *
  153. * PARAMETERS: origin - Root of subtree to search
  154. * op - Last (previous) Op that was found
  155. *
  156. * RETURN: Next Op found in the search.
  157. *
  158. * DESCRIPTION: Get next op in tree (walking the tree in depth-first order)
  159. * Return NULL when reaching "origin" or when walking up from root
  160. *
  161. ******************************************************************************/
  162. union acpi_parse_object *acpi_ps_get_depth_next(union acpi_parse_object *origin,
  163. union acpi_parse_object *op)
  164. {
  165. union acpi_parse_object *next = NULL;
  166. union acpi_parse_object *parent;
  167. union acpi_parse_object *arg;
  168. ACPI_FUNCTION_ENTRY();
  169. if (!op) {
  170. return (NULL);
  171. }
  172. /* Look for an argument or child */
  173. next = acpi_ps_get_arg(op, 0);
  174. if (next) {
  175. ASL_CV_LABEL_FILENODE(next);
  176. return (next);
  177. }
  178. /* Look for a sibling */
  179. next = op->common.next;
  180. if (next) {
  181. ASL_CV_LABEL_FILENODE(next);
  182. return (next);
  183. }
  184. /* Look for a sibling of parent */
  185. parent = op->common.parent;
  186. while (parent) {
  187. arg = acpi_ps_get_arg(parent, 0);
  188. while (arg && (arg != origin) && (arg != op)) {
  189. ASL_CV_LABEL_FILENODE(arg);
  190. arg = arg->common.next;
  191. }
  192. if (arg == origin) {
  193. /* Reached parent of origin, end search */
  194. return (NULL);
  195. }
  196. if (parent->common.next) {
  197. /* Found sibling of parent */
  198. ASL_CV_LABEL_FILENODE(parent->common.next);
  199. return (parent->common.next);
  200. }
  201. op = parent;
  202. parent = parent->common.parent;
  203. }
  204. ASL_CV_LABEL_FILENODE(next);
  205. return (next);
  206. }
  207. #ifdef ACPI_OBSOLETE_FUNCTIONS
  208. /*******************************************************************************
  209. *
  210. * FUNCTION: acpi_ps_get_child
  211. *
  212. * PARAMETERS: op - Get the child of this Op
  213. *
  214. * RETURN: Child Op, Null if none is found.
  215. *
  216. * DESCRIPTION: Get op's children or NULL if none
  217. *
  218. ******************************************************************************/
  219. union acpi_parse_object *acpi_ps_get_child(union acpi_parse_object *op)
  220. {
  221. union acpi_parse_object *child = NULL;
  222. ACPI_FUNCTION_ENTRY();
  223. switch (op->common.aml_opcode) {
  224. case AML_SCOPE_OP:
  225. case AML_ELSE_OP:
  226. case AML_DEVICE_OP:
  227. case AML_THERMAL_ZONE_OP:
  228. case AML_INT_METHODCALL_OP:
  229. child = acpi_ps_get_arg(op, 0);
  230. break;
  231. case AML_BUFFER_OP:
  232. case AML_PACKAGE_OP:
  233. case AML_METHOD_OP:
  234. case AML_IF_OP:
  235. case AML_WHILE_OP:
  236. case AML_FIELD_OP:
  237. child = acpi_ps_get_arg(op, 1);
  238. break;
  239. case AML_POWER_RESOURCE_OP:
  240. case AML_INDEX_FIELD_OP:
  241. child = acpi_ps_get_arg(op, 2);
  242. break;
  243. case AML_PROCESSOR_OP:
  244. case AML_BANK_FIELD_OP:
  245. child = acpi_ps_get_arg(op, 3);
  246. break;
  247. default:
  248. /* All others have no children */
  249. break;
  250. }
  251. return (child);
  252. }
  253. #endif