2 * Copyright © 2010 Intel Corporation
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:
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
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.
24 #include "ir_print_visitor.h"
25 #include "glsl_types.h"
26 #include "glsl_parser_extras.h"
28 static void print_type(const glsl_type
*t
);
31 ir_instruction::print(void) const
33 ir_instruction
*deconsted
= const_cast<ir_instruction
*>(this);
36 deconsted
->accept(&v
);
40 _mesa_print_ir(exec_list
*instructions
,
41 struct _mesa_glsl_parse_state
*state
)
44 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
45 const glsl_type
*const s
= state
->user_structures
[i
];
47 printf("(structure (%s) (%s@%p) (%u) (\n",
48 s
->name
, s
->name
, (void *) s
, s
->length
);
50 for (unsigned j
= 0; j
< s
->length
; j
++) {
52 print_type(s
->fields
.structure
[j
].type
);
53 printf(")(%s))\n", s
->fields
.structure
[j
].name
);
61 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
62 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
64 if (ir
->ir_type
!= ir_type_function
)
71 void ir_print_visitor::indent(void)
73 for (int i
= 0; i
< indentation
; i
++)
78 print_type(const glsl_type
*t
)
80 if (t
->base_type
== GLSL_TYPE_ARRAY
) {
82 print_type(t
->fields
.array
);
83 printf(" %u)", t
->length
);
84 } else if ((t
->base_type
== GLSL_TYPE_STRUCT
)
85 && (strncmp("gl_", t
->name
, 3) != 0)) {
86 printf("%s@%p", t
->name
, (void *) t
);
88 printf("%s", t
->name
);
93 void ir_print_visitor::visit(ir_variable
*ir
)
97 const char *const cent
= (ir
->centroid
) ? "centroid " : "";
98 const char *const inv
= (ir
->invariant
) ? "invariant " : "";
99 const char *const mode
[] = { "", "uniform ", "in ", "out ", "inout ",
101 const char *const interp
[] = { "", "flat", "noperspective" };
103 printf("(%s%s%s%s) ",
104 cent
, inv
, mode
[ir
->mode
], interp
[ir
->interpolation
]);
106 print_type(ir
->type
);
107 printf(" %s@%p)", ir
->name
, (void *) ir
);
111 void ir_print_visitor::visit(ir_function_signature
*ir
)
113 printf("(signature ");
116 print_type(ir
->return_type
);
120 printf("(parameters\n");
123 foreach_iter(exec_list_iterator
, iter
, ir
->parameters
) {
124 ir_variable
*const inst
= (ir_variable
*) iter
.get();
140 foreach_iter(exec_list_iterator
, iter
, ir
->body
) {
141 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
154 void ir_print_visitor::visit(ir_function
*ir
)
156 bool found_non_builtin_proto
= false;
158 foreach_iter(exec_list_iterator
, iter
, *ir
) {
159 ir_function_signature
*const sig
= (ir_function_signature
*) iter
.get();
160 if (sig
->is_defined
|| !sig
->is_built_in
)
161 found_non_builtin_proto
= true;
163 if (!found_non_builtin_proto
)
166 printf("(function %s\n", ir
->name
);
168 foreach_iter(exec_list_iterator
, iter
, *ir
) {
169 ir_function_signature
*const sig
= (ir_function_signature
*) iter
.get();
181 void ir_print_visitor::visit(ir_expression
*ir
)
183 printf("(expression ");
185 print_type(ir
->type
);
187 printf(" %s ", ir
->operator_string());
190 ir
->operands
[0]->accept(this);
193 ir
->operands
[1]->accept(this);
198 void ir_print_visitor::visit(ir_texture
*ir
)
200 printf("(%s ", ir
->opcode_string());
202 ir
->sampler
->accept(this);
205 ir
->coordinate
->accept(this);
207 printf(" (%d %d %d) ", ir
->offsets
[0], ir
->offsets
[1], ir
->offsets
[2]);
209 if (ir
->op
!= ir_txf
) {
211 ir
->projector
->accept(this);
215 if (ir
->shadow_comparitor
) {
217 ir
->shadow_comparitor
->accept(this);
229 ir
->lod_info
.bias
->accept(this);
233 ir
->lod_info
.lod
->accept(this);
237 ir
->lod_info
.grad
.dPdx
->accept(this);
239 ir
->lod_info
.grad
.dPdy
->accept(this);
247 void ir_print_visitor::visit(ir_swizzle
*ir
)
249 const unsigned swiz
[4] = {
257 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
258 printf("%c", "xyzw"[swiz
[i
]]);
261 ir
->val
->accept(this);
266 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
268 ir_variable
*var
= ir
->variable_referenced();
269 printf("(var_ref %s@%p) ", var
->name
, (void *) var
);
273 void ir_print_visitor::visit(ir_dereference_array
*ir
)
275 printf("(array_ref ");
276 ir
->array
->accept(this);
277 ir
->array_index
->accept(this);
282 void ir_print_visitor::visit(ir_dereference_record
*ir
)
284 printf("(record_ref ");
285 ir
->record
->accept(this);
286 printf(" %s) ", ir
->field
);
290 void ir_print_visitor::visit(ir_assignment
*ir
)
295 ir
->condition
->accept(this);
297 printf("(constant bool (1))");
303 for (unsigned i
= 0; i
< 4; i
++) {
304 if ((ir
->write_mask
& (1 << i
)) != 0) {
311 printf(" (%s) ", mask
);
313 ir
->lhs
->accept(this);
317 ir
->rhs
->accept(this);
322 void ir_print_visitor::visit(ir_constant
*ir
)
324 const glsl_type
*const base_type
= ir
->type
->get_base_type();
326 printf("(constant ");
327 print_type(ir
->type
);
330 if (ir
->type
->is_array()) {
331 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
332 ir
->get_array_element(i
)->accept(this);
334 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
337 switch (base_type
->base_type
) {
338 case GLSL_TYPE_UINT
: printf("%u", ir
->value
.u
[i
]); break;
339 case GLSL_TYPE_INT
: printf("%d", ir
->value
.i
[i
]); break;
340 case GLSL_TYPE_FLOAT
: printf("%f", ir
->value
.f
[i
]); break;
341 case GLSL_TYPE_BOOL
: printf("%d", ir
->value
.b
[i
]); break;
351 ir_print_visitor::visit(ir_call
*ir
)
353 printf("(call %s (", ir
->callee_name());
354 foreach_iter(exec_list_iterator
, iter
, *ir
) {
355 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
364 ir_print_visitor::visit(ir_return
*ir
)
368 ir_rvalue
*const value
= ir
->get_value();
379 ir_print_visitor::visit(ir_discard
*ir
)
383 if (ir
->condition
!= NULL
) {
385 ir
->condition
->accept(this);
393 ir_print_visitor::visit(ir_if
*ir
)
396 ir
->condition
->accept(this);
401 foreach_iter(exec_list_iterator
, iter
, ir
->then_instructions
) {
402 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
417 foreach_iter(exec_list_iterator
, iter
, ir
->else_instructions
) {
418 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
431 ir_print_visitor::visit(ir_loop
*ir
)
434 if (ir
->counter
!= NULL
)
435 ir
->counter
->accept(this);
437 if (ir
->from
!= NULL
)
438 ir
->from
->accept(this);
441 ir
->to
->accept(this);
443 if (ir
->increment
!= NULL
)
444 ir
->increment
->accept(this);
448 foreach_iter(exec_list_iterator
, iter
, ir
->body_instructions
) {
449 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
462 ir_print_visitor::visit(ir_loop_jump
*ir
)
464 printf("%s", ir
->is_break() ? "break" : "continue");