pstree.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /******************************************************************************
  2. *
  3. * Module Name: pstree - Parser op tree manipulation/traversal/search
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  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 <acpi/acparser.h>
  44. #include <acpi/amlcode.h>
  45. #define _COMPONENT ACPI_PARSER
  46. ACPI_MODULE_NAME("pstree")
  47. /* Local prototypes */
  48. #ifdef ACPI_OBSOLETE_FUNCTIONS
  49. union acpi_parse_object *acpi_ps_get_child(union acpi_parse_object *op);
  50. #endif
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_ps_get_arg
  54. *
  55. * PARAMETERS: Op - Get an argument for this op
  56. * Argn - Nth argument to get
  57. *
  58. * RETURN: The argument (as an Op object). NULL if argument does not exist
  59. *
  60. * DESCRIPTION: Get the specified op's argument.
  61. *
  62. ******************************************************************************/
  63. union acpi_parse_object *acpi_ps_get_arg(union acpi_parse_object *op, u32 argn)
  64. {
  65. union acpi_parse_object *arg = NULL;
  66. const struct acpi_opcode_info *op_info;
  67. ACPI_FUNCTION_ENTRY();
  68. /* Get the info structure for this opcode */
  69. op_info = acpi_ps_get_opcode_info(op->common.aml_opcode);
  70. if (op_info->class == AML_CLASS_UNKNOWN) {
  71. /* Invalid opcode or ASCII character */
  72. return (NULL);
  73. }
  74. /* Check if this opcode requires argument sub-objects */
  75. if (!(op_info->flags & AML_HAS_ARGS)) {
  76. /* Has no linked argument objects */
  77. return (NULL);
  78. }
  79. /* Get the requested argument object */
  80. arg = op->common.value.arg;
  81. while (arg && argn) {
  82. argn--;
  83. arg = arg->common.next;
  84. }
  85. return (arg);
  86. }
  87. /*******************************************************************************
  88. *
  89. * FUNCTION: acpi_ps_append_arg
  90. *
  91. * PARAMETERS: Op - Append an argument to this Op.
  92. * Arg - Argument Op to append
  93. *
  94. * RETURN: None.
  95. *
  96. * DESCRIPTION: Append an argument to an op's argument list (a NULL arg is OK)
  97. *
  98. ******************************************************************************/
  99. void
  100. acpi_ps_append_arg(union acpi_parse_object *op, union acpi_parse_object *arg)
  101. {
  102. union acpi_parse_object *prev_arg;
  103. const struct acpi_opcode_info *op_info;
  104. ACPI_FUNCTION_ENTRY();
  105. if (!op) {
  106. return;
  107. }
  108. /* Get the info structure for this opcode */
  109. op_info = acpi_ps_get_opcode_info(op->common.aml_opcode);
  110. if (op_info->class == AML_CLASS_UNKNOWN) {
  111. /* Invalid opcode */
  112. ACPI_REPORT_ERROR(("ps_append_arg: Invalid AML Opcode: 0x%2.2X\n", op->common.aml_opcode));
  113. return;
  114. }
  115. /* Check if this opcode requires argument sub-objects */
  116. if (!(op_info->flags & AML_HAS_ARGS)) {
  117. /* Has no linked argument objects */
  118. return;
  119. }
  120. /* Append the argument to the linked argument list */
  121. if (op->common.value.arg) {
  122. /* Append to existing argument list */
  123. prev_arg = op->common.value.arg;
  124. while (prev_arg->common.next) {
  125. prev_arg = prev_arg->common.next;
  126. }
  127. prev_arg->common.next = arg;
  128. } else {
  129. /* No argument list, this will be the first argument */
  130. op->common.value.arg = arg;
  131. }
  132. /* Set the parent in this arg and any args linked after it */
  133. while (arg) {
  134. arg->common.parent = op;
  135. arg = arg->common.next;
  136. }
  137. }
  138. #ifdef ACPI_FUTURE_USAGE
  139. /*******************************************************************************
  140. *
  141. * FUNCTION: acpi_ps_get_depth_next
  142. *
  143. * PARAMETERS: Origin - Root of subtree to search
  144. * Op - Last (previous) Op that was found
  145. *
  146. * RETURN: Next Op found in the search.
  147. *
  148. * DESCRIPTION: Get next op in tree (walking the tree in depth-first order)
  149. * Return NULL when reaching "origin" or when walking up from root
  150. *
  151. ******************************************************************************/
  152. union acpi_parse_object *acpi_ps_get_depth_next(union acpi_parse_object *origin,
  153. union acpi_parse_object *op)
  154. {
  155. union acpi_parse_object *next = NULL;
  156. union acpi_parse_object *parent;
  157. union acpi_parse_object *arg;
  158. ACPI_FUNCTION_ENTRY();
  159. if (!op) {
  160. return (NULL);
  161. }
  162. /* Look for an argument or child */
  163. next = acpi_ps_get_arg(op, 0);
  164. if (next) {
  165. return (next);
  166. }
  167. /* Look for a sibling */
  168. next = op->common.next;
  169. if (next) {
  170. return (next);
  171. }
  172. /* Look for a sibling of parent */
  173. parent = op->common.parent;
  174. while (parent) {
  175. arg = acpi_ps_get_arg(parent, 0);
  176. while (arg && (arg != origin) && (arg != op)) {
  177. arg = arg->common.next;
  178. }
  179. if (arg == origin) {
  180. /* Reached parent of origin, end search */
  181. return (NULL);
  182. }
  183. if (parent->common.next) {
  184. /* Found sibling of parent */
  185. return (parent->common.next);
  186. }
  187. op = parent;
  188. parent = parent->common.parent;
  189. }
  190. return (next);
  191. }
  192. #ifdef ACPI_OBSOLETE_FUNCTIONS
  193. /*******************************************************************************
  194. *
  195. * FUNCTION: acpi_ps_get_child
  196. *
  197. * PARAMETERS: Op - Get the child of this Op
  198. *
  199. * RETURN: Child Op, Null if none is found.
  200. *
  201. * DESCRIPTION: Get op's children or NULL if none
  202. *
  203. ******************************************************************************/
  204. union acpi_parse_object *acpi_ps_get_child(union acpi_parse_object *op)
  205. {
  206. union acpi_parse_object *child = NULL;
  207. ACPI_FUNCTION_ENTRY();
  208. switch (op->common.aml_opcode) {
  209. case AML_SCOPE_OP:
  210. case AML_ELSE_OP:
  211. case AML_DEVICE_OP:
  212. case AML_THERMAL_ZONE_OP:
  213. case AML_INT_METHODCALL_OP:
  214. child = acpi_ps_get_arg(op, 0);
  215. break;
  216. case AML_BUFFER_OP:
  217. case AML_PACKAGE_OP:
  218. case AML_METHOD_OP:
  219. case AML_IF_OP:
  220. case AML_WHILE_OP:
  221. case AML_FIELD_OP:
  222. child = acpi_ps_get_arg(op, 1);
  223. break;
  224. case AML_POWER_RES_OP:
  225. case AML_INDEX_FIELD_OP:
  226. child = acpi_ps_get_arg(op, 2);
  227. break;
  228. case AML_PROCESSOR_OP:
  229. case AML_BANK_FIELD_OP:
  230. child = acpi_ps_get_arg(op, 3);
  231. break;
  232. default:
  233. /* All others have no children */
  234. break;
  235. }
  236. return (child);
  237. }
  238. #endif
  239. #endif /* ACPI_FUTURE_USAGE */