1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "ir.h"
25 #include "ir_hierarchical_visitor.h"
26 
ir_hierarchical_visitor()27 ir_hierarchical_visitor::ir_hierarchical_visitor()
28 {
29    this->base_ir = NULL;
30    this->callback = NULL;
31    this->data = NULL;
32    this->in_assignee = false;
33 }
34 
35 ir_visitor_status
visit(ir_rvalue * ir)36 ir_hierarchical_visitor::visit(ir_rvalue *ir)
37 {
38    if (this->callback != NULL)
39       this->callback(ir, this->data);
40 
41    return visit_continue;
42 }
43 
44 ir_visitor_status
visit(ir_variable * ir)45 ir_hierarchical_visitor::visit(ir_variable *ir)
46 {
47    if (this->callback != NULL)
48       this->callback(ir, this->data);
49 
50    return visit_continue;
51 }
52 
53 ir_visitor_status
visit(ir_constant * ir)54 ir_hierarchical_visitor::visit(ir_constant *ir)
55 {
56    if (this->callback != NULL)
57       this->callback(ir, this->data);
58 
59    return visit_continue;
60 }
61 
62 ir_visitor_status
visit(ir_loop_jump * ir)63 ir_hierarchical_visitor::visit(ir_loop_jump *ir)
64 {
65    if (this->callback != NULL)
66       this->callback(ir, this->data);
67 
68    return visit_continue;
69 }
70 
71 ir_visitor_status
visit(ir_dereference_variable * ir)72 ir_hierarchical_visitor::visit(ir_dereference_variable *ir)
73 {
74    if (this->callback != NULL)
75       this->callback(ir, this->data);
76 
77    return visit_continue;
78 }
79 
80 ir_visitor_status
visit_enter(ir_loop * ir)81 ir_hierarchical_visitor::visit_enter(ir_loop *ir)
82 {
83    if (this->callback != NULL)
84       this->callback(ir, this->data);
85 
86    return visit_continue;
87 }
88 
89 ir_visitor_status
visit_leave(ir_loop * ir)90 ir_hierarchical_visitor::visit_leave(ir_loop *ir)
91 {
92    (void) ir;
93    return visit_continue;
94 }
95 
96 ir_visitor_status
visit_enter(ir_function_signature * ir)97 ir_hierarchical_visitor::visit_enter(ir_function_signature *ir)
98 {
99    if (this->callback != NULL)
100       this->callback(ir, this->data);
101 
102    return visit_continue;
103 }
104 
105 ir_visitor_status
visit_leave(ir_function_signature * ir)106 ir_hierarchical_visitor::visit_leave(ir_function_signature *ir)
107 {
108    (void) ir;
109    return visit_continue;
110 }
111 
112 ir_visitor_status
visit_enter(ir_function * ir)113 ir_hierarchical_visitor::visit_enter(ir_function *ir)
114 {
115    if (this->callback != NULL)
116       this->callback(ir, this->data);
117 
118    return visit_continue;
119 }
120 
121 ir_visitor_status
visit_leave(ir_function * ir)122 ir_hierarchical_visitor::visit_leave(ir_function *ir)
123 {
124    (void) ir;
125    return visit_continue;
126 }
127 
128 ir_visitor_status
visit_enter(ir_expression * ir)129 ir_hierarchical_visitor::visit_enter(ir_expression *ir)
130 {
131    if (this->callback != NULL)
132       this->callback(ir, this->data);
133 
134    return visit_continue;
135 }
136 
137 ir_visitor_status
visit_leave(ir_expression * ir)138 ir_hierarchical_visitor::visit_leave(ir_expression *ir)
139 {
140    (void) ir;
141    return visit_continue;
142 }
143 
144 ir_visitor_status
visit_enter(ir_texture * ir)145 ir_hierarchical_visitor::visit_enter(ir_texture *ir)
146 {
147    if (this->callback != NULL)
148       this->callback(ir, this->data);
149 
150    return visit_continue;
151 }
152 
153 ir_visitor_status
visit_leave(ir_texture * ir)154 ir_hierarchical_visitor::visit_leave(ir_texture *ir)
155 {
156    (void) ir;
157    return visit_continue;
158 }
159 
160 ir_visitor_status
visit_enter(ir_swizzle * ir)161 ir_hierarchical_visitor::visit_enter(ir_swizzle *ir)
162 {
163    if (this->callback != NULL)
164       this->callback(ir, this->data);
165 
166    return visit_continue;
167 }
168 
169 ir_visitor_status
visit_leave(ir_swizzle * ir)170 ir_hierarchical_visitor::visit_leave(ir_swizzle *ir)
171 {
172    (void) ir;
173    return visit_continue;
174 }
175 
176 ir_visitor_status
visit_enter(ir_dereference_array * ir)177 ir_hierarchical_visitor::visit_enter(ir_dereference_array *ir)
178 {
179    if (this->callback != NULL)
180       this->callback(ir, this->data);
181 
182    return visit_continue;
183 }
184 
185 ir_visitor_status
visit_leave(ir_dereference_array * ir)186 ir_hierarchical_visitor::visit_leave(ir_dereference_array *ir)
187 {
188    (void) ir;
189    return visit_continue;
190 }
191 
192 ir_visitor_status
visit_enter(ir_dereference_record * ir)193 ir_hierarchical_visitor::visit_enter(ir_dereference_record *ir)
194 {
195    if (this->callback != NULL)
196       this->callback(ir, this->data);
197 
198    return visit_continue;
199 }
200 
201 ir_visitor_status
visit_leave(ir_dereference_record * ir)202 ir_hierarchical_visitor::visit_leave(ir_dereference_record *ir)
203 {
204    (void) ir;
205    return visit_continue;
206 }
207 
208 ir_visitor_status
visit_enter(ir_assignment * ir)209 ir_hierarchical_visitor::visit_enter(ir_assignment *ir)
210 {
211    if (this->callback != NULL)
212       this->callback(ir, this->data);
213 
214    return visit_continue;
215 }
216 
217 ir_visitor_status
visit_leave(ir_assignment * ir)218 ir_hierarchical_visitor::visit_leave(ir_assignment *ir)
219 {
220    (void) ir;
221    return visit_continue;
222 }
223 
224 ir_visitor_status
visit_enter(ir_call * ir)225 ir_hierarchical_visitor::visit_enter(ir_call *ir)
226 {
227    if (this->callback != NULL)
228       this->callback(ir, this->data);
229 
230    return visit_continue;
231 }
232 
233 ir_visitor_status
visit_leave(ir_call * ir)234 ir_hierarchical_visitor::visit_leave(ir_call *ir)
235 {
236    (void) ir;
237    return visit_continue;
238 }
239 
240 ir_visitor_status
visit_enter(ir_return * ir)241 ir_hierarchical_visitor::visit_enter(ir_return *ir)
242 {
243    if (this->callback != NULL)
244       this->callback(ir, this->data);
245 
246    return visit_continue;
247 }
248 
249 ir_visitor_status
visit_leave(ir_return * ir)250 ir_hierarchical_visitor::visit_leave(ir_return *ir)
251 {
252    (void) ir;
253    return visit_continue;
254 }
255 
256 ir_visitor_status
visit_enter(ir_discard * ir)257 ir_hierarchical_visitor::visit_enter(ir_discard *ir)
258 {
259    if (this->callback != NULL)
260       this->callback(ir, this->data);
261 
262    return visit_continue;
263 }
264 
265 ir_visitor_status
visit_leave(ir_discard * ir)266 ir_hierarchical_visitor::visit_leave(ir_discard *ir)
267 {
268    (void) ir;
269    return visit_continue;
270 }
271 
272 ir_visitor_status
visit_enter(ir_if * ir)273 ir_hierarchical_visitor::visit_enter(ir_if *ir)
274 {
275    if (this->callback != NULL)
276       this->callback(ir, this->data);
277 
278    return visit_continue;
279 }
280 
281 ir_visitor_status
visit_leave(ir_if * ir)282 ir_hierarchical_visitor::visit_leave(ir_if *ir)
283 {
284    (void) ir;
285    return visit_continue;
286 }
287 
288 void
run(exec_list * instructions)289 ir_hierarchical_visitor::run(exec_list *instructions)
290 {
291    visit_list_elements(this, instructions);
292 }
293 
294 
295 void
visit_tree(ir_instruction * ir,void (* callback)(class ir_instruction * ir,void * data),void * data)296 visit_tree(ir_instruction *ir,
297 	   void (*callback)(class ir_instruction *ir, void *data),
298 	   void *data)
299 {
300    ir_hierarchical_visitor v;
301 
302    v.callback = callback;
303    v.data = data;
304 
305    ir->accept(&v);
306 }
307