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 
25 #ifndef IR_HIERARCHICAL_VISITOR_H
26 #define IR_HIERARCHICAL_VISITOR_H
27 
28 /**
29  * Enumeration values returned by visit methods to guide processing
30  */
31 enum ir_visitor_status {
32    visit_continue,		/**< Continue visiting as normal. */
33    visit_continue_with_parent,	/**< Don't visit siblings, continue w/parent. */
34    visit_stop			/**< Stop visiting immediately. */
35 };
36 
37 
38 #ifdef __cplusplus
39 /**
40  * Base class of hierarchical visitors of IR instruction trees
41  *
42  * Hierarchical visitors differ from traditional visitors in a couple of
43  * important ways.  Rather than having a single \c visit method for each
44  * subclass in the composite, there are three kinds of visit methods.
45  * Leaf-node classes have a traditional \c visit method.  Internal-node
46  * classes have a \c visit_enter method, which is invoked just before
47  * processing child nodes, and a \c visit_leave method which is invoked just
48  * after processing child nodes.
49  *
50  * In addition, each visit method and the \c accept methods in the composite
51  * have a return value which guides the navigation.  Any of the visit methods
52  * can choose to continue visiting the tree as normal (by returning \c
53  * visit_continue), terminate visiting any further nodes immediately (by
54  * returning \c visit_stop), or stop visiting sibling nodes (by returning \c
55  * visit_continue_with_parent).
56  *
57  * These two changes combine to allow navigation of children to be implemented
58  * in the composite's \c accept method.  The \c accept method for a leaf-node
59  * class will simply call the \c visit method, as usual, and pass its return
60  * value on.  The \c accept method for internal-node classes will call the \c
61  * visit_enter method, call the \c accept method of each child node, and,
62  * finally, call the \c visit_leave method.  If any of these return a value
63  * other that \c visit_continue, the correct action must be taken.
64  *
65  * The final benefit is that the hierarchical visitor base class need not be
66  * abstract.  Default implementations of every \c visit, \c visit_enter, and
67  * \c visit_leave method can be provided.  By default each of these methods
68  * simply returns \c visit_continue.  This allows a significant reduction in
69  * derived class code.
70  *
71  * For more information about hierarchical visitors, see:
72  *
73  *    http://c2.com/cgi/wiki?HierarchicalVisitorPattern
74  *    http://c2.com/cgi/wiki?HierarchicalVisitorDiscussion
75  */
76 
77 class ir_hierarchical_visitor {
78 public:
79    ir_hierarchical_visitor();
80 
81    /**
82     * \name Visit methods for leaf-node classes
83     */
84    /*@{*/
85    virtual ir_visitor_status visit(class ir_rvalue *);
86    virtual ir_visitor_status visit(class ir_variable *);
87    virtual ir_visitor_status visit(class ir_constant *);
88    virtual ir_visitor_status visit(class ir_loop_jump *);
89    virtual ir_visitor_status visit(class ir_precision_statement *);
90    virtual ir_visitor_status visit(class ir_typedecl_statement *);
91    virtual ir_visitor_status visit(class ir_barrier *);
92 
93    /**
94     * ir_dereference_variable isn't technically a leaf, but it is treated as a
95     * leaf here for a couple reasons.  By not automatically visiting the one
96     * child ir_variable node from the ir_dereference_variable, ir_variable
97     * nodes can always be handled as variable declarations.  Code that used
98     * non-hierarchical visitors had to set an "in a dereference" flag to
99     * determine how to handle an ir_variable.  By forcing the visitor to
100     * handle the ir_variable within the ir_dereference_variable visitor, this
101     * kludge can be avoided.
102     *
103     * In addition, I can envision no use for having separate enter and leave
104     * methods.  Anything that could be done in the enter and leave methods
105     * that couldn't just be done in the visit method.
106     */
107    virtual ir_visitor_status visit(class ir_dereference_variable *);
108    /*@}*/
109 
110    /**
111     * \name Visit methods for internal-node classes
112     */
113    /*@{*/
114    virtual ir_visitor_status visit_enter(class ir_loop *);
115    virtual ir_visitor_status visit_leave(class ir_loop *);
116    virtual ir_visitor_status visit_enter(class ir_function_signature *);
117    virtual ir_visitor_status visit_leave(class ir_function_signature *);
118    virtual ir_visitor_status visit_enter(class ir_function *);
119    virtual ir_visitor_status visit_leave(class ir_function *);
120    virtual ir_visitor_status visit_enter(class ir_expression *);
121    virtual ir_visitor_status visit_leave(class ir_expression *);
122    virtual ir_visitor_status visit_enter(class ir_texture *);
123    virtual ir_visitor_status visit_leave(class ir_texture *);
124    virtual ir_visitor_status visit_enter(class ir_swizzle *);
125    virtual ir_visitor_status visit_leave(class ir_swizzle *);
126    virtual ir_visitor_status visit_enter(class ir_dereference_array *);
127    virtual ir_visitor_status visit_leave(class ir_dereference_array *);
128    virtual ir_visitor_status visit_enter(class ir_dereference_record *);
129    virtual ir_visitor_status visit_leave(class ir_dereference_record *);
130    virtual ir_visitor_status visit_enter(class ir_assignment *);
131    virtual ir_visitor_status visit_leave(class ir_assignment *);
132    virtual ir_visitor_status visit_enter(class ir_call *);
133    virtual ir_visitor_status visit_leave(class ir_call *);
134    virtual ir_visitor_status visit_enter(class ir_return *);
135    virtual ir_visitor_status visit_leave(class ir_return *);
136    virtual ir_visitor_status visit_enter(class ir_discard *);
137    virtual ir_visitor_status visit_leave(class ir_discard *);
138    virtual ir_visitor_status visit_enter(class ir_demote *);
139    virtual ir_visitor_status visit_leave(class ir_demote *);
140    virtual ir_visitor_status visit_enter(class ir_if *);
141    virtual ir_visitor_status visit_leave(class ir_if *);
142    virtual ir_visitor_status visit_enter(class ir_emit_vertex *);
143    virtual ir_visitor_status visit_leave(class ir_emit_vertex *);
144    virtual ir_visitor_status visit_enter(class ir_end_primitive *);
145    virtual ir_visitor_status visit_leave(class ir_end_primitive *);
146    /*@}*/
147 
148 
149    /**
150     * Utility function to process a linked list of instructions with a visitor
151     */
152    void run(struct exec_list *instructions);
153 
154    /**
155     * Utility function to call both the leave and enter callback functions.
156     * This is used for leaf nodes.
157     */
158    void call_enter_leave_callbacks(class ir_instruction *ir);
159 
160    /* Some visitors may need to insert new variable declarations and
161     * assignments for portions of a subtree, which means they need a
162     * pointer to the current instruction in the stream, not just their
163     * node in the tree rooted at that instruction.
164     *
165     * This is implemented by visit_list_elements -- if the visitor is
166     * not called by it, nothing good will happen.
167     */
168    class ir_instruction *base_ir;
169 
170    /**
171     * Callback function that is invoked on entry to each node visited.
172     *
173     * \warning
174     * Visitor classes derived from \c ir_hierarchical_visitor \b may \b not
175     * invoke this function.  This can be used, for example, to cause the
176     * callback to be invoked on every node type except one.
177     */
178    void (*callback_enter)(class ir_instruction *ir, void *data);
179 
180    /**
181     * Callback function that is invoked on exit of each node visited.
182     *
183     * \warning
184     * Visitor classes derived from \c ir_hierarchical_visitor \b may \b not
185     * invoke this function.  This can be used, for example, to cause the
186     * callback to be invoked on every node type except one.
187     */
188    void (*callback_leave)(class ir_instruction *ir, void *data);
189 
190    /**
191     * Extra data parameter passed to the per-node callback_enter function
192     */
193    void *data_enter;
194 
195    /**
196     * Extra data parameter passed to the per-node callback_leave function
197     */
198    void *data_leave;
199 
200    /**
201     * Currently in the LHS of an assignment?
202     *
203     * This is set and cleared by the \c ir_assignment::accept method.
204     */
205    bool in_assignee;
206 };
207 
208 void visit_tree(ir_instruction *ir,
209 		void (*callback_enter)(class ir_instruction *ir, void *data),
210 		void *data_enter,
211 		void (*callback_leave)(class ir_instruction *ir, void *data) = NULL,
212 		void *data_leave = NULL);
213 
214 ir_visitor_status visit_list_elements(ir_hierarchical_visitor *v, exec_list *l,
215                                       bool statement_list = true);
216 #endif /* __cplusplus */
217 
218 #endif /* IR_HIERARCHICAL_VISITOR_H */
219