Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ir_hierarchical_visitor.h 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* -*- c++ -*- */
  2. /*
  3. * Copyright © 2010 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #pragma once
  25. #ifndef IR_HIERARCHICAL_VISITOR_H
  26. #define IR_HIERARCHICAL_VISITOR_H
  27. /**
  28. * Enumeration values returned by visit methods to guide processing
  29. */
  30. enum ir_visitor_status {
  31. visit_continue, /**< Continue visiting as normal. */
  32. visit_continue_with_parent, /**< Don't visit siblings, continue w/parent. */
  33. visit_stop /**< Stop visiting immediately. */
  34. };
  35. /**
  36. * Base class of hierarchical visitors of IR instruction trees
  37. *
  38. * Hierarchical visitors differ from traditional visitors in a couple of
  39. * important ways. Rather than having a single \c visit method for each
  40. * subclass in the composite, there are three kinds of visit methods.
  41. * Leaf-node classes have a traditional \c visit method. Internal-node
  42. * classes have a \c visit_enter method, which is invoked just before
  43. * processing child nodes, and a \c visit_leave method which is invoked just
  44. * after processing child nodes.
  45. *
  46. * In addition, each visit method and the \c accept methods in the composite
  47. * have a return value which guides the navigation. Any of the visit methods
  48. * can choose to continue visiting the tree as normal (by returning \c
  49. * visit_continue), terminate visiting any further nodes immediately (by
  50. * returning \c visit_stop), or stop visiting sibling nodes (by returning \c
  51. * visit_continue_with_parent).
  52. *
  53. * These two changes combine to allow nagivation of children to be implemented
  54. * in the composite's \c accept method. The \c accept method for a leaf-node
  55. * class will simply call the \c visit method, as usual, and pass its return
  56. * value on. The \c accept method for internal-node classes will call the \c
  57. * visit_enter method, call the \c accpet method of each child node, and,
  58. * finally, call the \c visit_leave method. If any of these return a value
  59. * other that \c visit_continue, the correct action must be taken.
  60. *
  61. * The final benefit is that the hierarchical visitor base class need not be
  62. * abstract. Default implementations of every \c visit, \c visit_enter, and
  63. * \c visit_leave method can be provided. By default each of these methods
  64. * simply returns \c visit_continue. This allows a significant reduction in
  65. * derived class code.
  66. *
  67. * For more information about hierarchical visitors, see:
  68. *
  69. * http://c2.com/cgi/wiki?HierarchicalVisitorPattern
  70. * http://c2.com/cgi/wiki?HierarchicalVisitorDiscussion
  71. */
  72. class ir_hierarchical_visitor {
  73. public:
  74. ir_hierarchical_visitor();
  75. /**
  76. * \name Visit methods for leaf-node classes
  77. */
  78. /*@{*/
  79. virtual ir_visitor_status visit(class ir_variable *);
  80. virtual ir_visitor_status visit(class ir_constant *);
  81. virtual ir_visitor_status visit(class ir_loop_jump *);
  82. /**
  83. * ir_dereference_variable isn't technically a leaf, but it is treated as a
  84. * leaf here for a couple reasons. By not automatically visiting the one
  85. * child ir_variable node from the ir_dereference_variable, ir_variable
  86. * nodes can always be handled as variable declarations. Code that used
  87. * non-hierarchical visitors had to set an "in a dereference" flag to
  88. * determine how to handle an ir_variable. By forcing the visitor to
  89. * handle the ir_variable within the ir_dereference_variable visitor, this
  90. * kludge can be avoided.
  91. *
  92. * In addition, I can envision no use for having separate enter and leave
  93. * methods. Anything that could be done in the enter and leave methods
  94. * that couldn't just be done in the visit method.
  95. */
  96. virtual ir_visitor_status visit(class ir_dereference_variable *);
  97. /*@}*/
  98. /**
  99. * \name Visit methods for internal-node classes
  100. */
  101. /*@{*/
  102. virtual ir_visitor_status visit_enter(class ir_loop *);
  103. virtual ir_visitor_status visit_leave(class ir_loop *);
  104. virtual ir_visitor_status visit_enter(class ir_function_signature *);
  105. virtual ir_visitor_status visit_leave(class ir_function_signature *);
  106. virtual ir_visitor_status visit_enter(class ir_function *);
  107. virtual ir_visitor_status visit_leave(class ir_function *);
  108. virtual ir_visitor_status visit_enter(class ir_expression *);
  109. virtual ir_visitor_status visit_leave(class ir_expression *);
  110. virtual ir_visitor_status visit_enter(class ir_texture *);
  111. virtual ir_visitor_status visit_leave(class ir_texture *);
  112. virtual ir_visitor_status visit_enter(class ir_swizzle *);
  113. virtual ir_visitor_status visit_leave(class ir_swizzle *);
  114. virtual ir_visitor_status visit_enter(class ir_dereference_array *);
  115. virtual ir_visitor_status visit_leave(class ir_dereference_array *);
  116. virtual ir_visitor_status visit_enter(class ir_dereference_record *);
  117. virtual ir_visitor_status visit_leave(class ir_dereference_record *);
  118. virtual ir_visitor_status visit_enter(class ir_assignment *);
  119. virtual ir_visitor_status visit_leave(class ir_assignment *);
  120. virtual ir_visitor_status visit_enter(class ir_call *);
  121. virtual ir_visitor_status visit_leave(class ir_call *);
  122. virtual ir_visitor_status visit_enter(class ir_return *);
  123. virtual ir_visitor_status visit_leave(class ir_return *);
  124. virtual ir_visitor_status visit_enter(class ir_if *);
  125. virtual ir_visitor_status visit_leave(class ir_if *);
  126. /*@}*/
  127. /**
  128. * Utility function to process a linked list of instructions with a visitor
  129. */
  130. void run(struct exec_list *instructions);
  131. /**
  132. * Callback function that is invoked on entry to each node visited.
  133. *
  134. * \warning
  135. * Visitor classes derived from \c ir_hierarchical_visitor \b may \b not
  136. * invoke this function. This can be used, for example, to cause the
  137. * callback to be invoked on every node type execpt one.
  138. */
  139. void (*callback)(class ir_instruction *ir, void *data);
  140. /**
  141. * Extra data parameter passed to the per-node callback function
  142. */
  143. void *data;
  144. };
  145. void visit_tree(ir_instruction *ir,
  146. void (*callback)(class ir_instruction *ir, void *data),
  147. void *data);
  148. #endif /* IR_HIERARCHICAL_VISITOR_H */