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"
27 #include "main/macros.h"
28 #include "program/hash_table.h"
30 static void print_type(const glsl_type
*t
);
33 ir_instruction::print(void) const
35 ir_instruction
*deconsted
= const_cast<ir_instruction
*>(this);
38 deconsted
->accept(&v
);
43 _mesa_print_ir(exec_list
*instructions
,
44 struct _mesa_glsl_parse_state
*state
)
47 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
48 const glsl_type
*const s
= state
->user_structures
[i
];
50 printf("(structure (%s) (%s@%p) (%u) (\n",
51 s
->name
, s
->name
, (void *) s
, s
->length
);
53 for (unsigned j
= 0; j
< s
->length
; j
++) {
55 print_type(s
->fields
.structure
[j
].type
);
56 printf(")(%s))\n", s
->fields
.structure
[j
].name
);
64 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
65 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
67 if (ir
->ir_type
!= ir_type_function
)
75 ir_print_visitor::ir_print_visitor()
79 hash_table_ctor(32, hash_table_pointer_hash
, hash_table_pointer_compare
);
80 symbols
= _mesa_symbol_table_ctor();
81 mem_ctx
= ralloc_context(NULL
);
84 ir_print_visitor::~ir_print_visitor()
86 hash_table_dtor(printable_names
);
87 _mesa_symbol_table_dtor(symbols
);
91 void ir_print_visitor::indent(void)
93 for (int i
= 0; i
< indentation
; i
++)
98 ir_print_visitor::unique_name(ir_variable
*var
)
100 /* var->name can be NULL in function prototypes when a type is given for a
101 * parameter but no name is given. In that case, just return an empty
102 * string. Don't worry about tracking the generated name in the printable
103 * names hash because this is the only scope where it can ever appear.
105 if (var
->name
== NULL
) {
106 static unsigned arg
= 1;
107 return ralloc_asprintf(this->mem_ctx
, "parameter@%u", arg
++);
110 /* Do we already have a name for this variable? */
111 const char *name
= (const char *) hash_table_find(this->printable_names
, var
);
115 /* If there's no conflict, just use the original name */
116 if (_mesa_symbol_table_find_symbol(this->symbols
, -1, var
->name
) == NULL
) {
119 static unsigned i
= 1;
120 name
= ralloc_asprintf(this->mem_ctx
, "%s@%u", var
->name
, ++i
);
122 hash_table_insert(this->printable_names
, (void *) name
, var
);
123 _mesa_symbol_table_add_symbol(this->symbols
, -1, name
, var
);
128 print_type(const glsl_type
*t
)
130 if (t
->base_type
== GLSL_TYPE_ARRAY
) {
132 print_type(t
->fields
.array
);
133 printf(" %u)", t
->length
);
134 } else if ((t
->base_type
== GLSL_TYPE_STRUCT
)
135 && (strncmp("gl_", t
->name
, 3) != 0)) {
136 printf("%s@%p", t
->name
, (void *) t
);
138 printf("%s", t
->name
);
142 void ir_print_visitor::visit(ir_rvalue
*ir
)
147 void ir_print_visitor::visit(ir_variable
*ir
)
151 const char *const cent
= (ir
->centroid
) ? "centroid " : "";
152 const char *const inv
= (ir
->invariant
) ? "invariant " : "";
153 const char *const mode
[] = { "", "uniform ", "shader_in ", "shader_out ",
154 "in ", "out ", "inout ",
155 "const_in ", "sys ", "temporary " };
156 STATIC_ASSERT(ARRAY_SIZE(mode
) == ir_var_mode_count
);
157 const char *const interp
[] = { "", "smooth", "flat", "noperspective" };
158 STATIC_ASSERT(ARRAY_SIZE(interp
) == INTERP_QUALIFIER_COUNT
);
160 printf("(%s%s%s%s) ",
161 cent
, inv
, mode
[ir
->mode
], interp
[ir
->interpolation
]);
163 print_type(ir
->type
);
164 printf(" %s)", unique_name(ir
));
168 void ir_print_visitor::visit(ir_function_signature
*ir
)
170 _mesa_symbol_table_push_scope(symbols
);
171 printf("(signature ");
174 print_type(ir
->return_type
);
178 printf("(parameters\n");
181 foreach_iter(exec_list_iterator
, iter
, ir
->parameters
) {
182 ir_variable
*const inst
= (ir_variable
*) iter
.get();
198 foreach_iter(exec_list_iterator
, iter
, ir
->body
) {
199 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
209 _mesa_symbol_table_pop_scope(symbols
);
213 void ir_print_visitor::visit(ir_function
*ir
)
215 printf("(function %s\n", ir
->name
);
217 foreach_iter(exec_list_iterator
, iter
, *ir
) {
218 ir_function_signature
*const sig
= (ir_function_signature
*) iter
.get();
229 void ir_print_visitor::visit(ir_expression
*ir
)
231 printf("(expression ");
233 print_type(ir
->type
);
235 printf(" %s ", ir
->operator_string());
237 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++) {
238 ir
->operands
[i
]->accept(this);
245 void ir_print_visitor::visit(ir_texture
*ir
)
247 printf("(%s ", ir
->opcode_string());
249 print_type(ir
->type
);
252 ir
->sampler
->accept(this);
255 if (ir
->op
!= ir_txs
) {
256 ir
->coordinate
->accept(this);
260 if (ir
->offset
!= NULL
) {
261 ir
->offset
->accept(this);
269 if (ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
&& ir
->op
!= ir_txs
&& ir
->op
!= ir_tg4
) {
271 ir
->projector
->accept(this);
275 if (ir
->shadow_comparitor
) {
277 ir
->shadow_comparitor
->accept(this);
291 ir
->lod_info
.bias
->accept(this);
296 ir
->lod_info
.lod
->accept(this);
299 ir
->lod_info
.sample_index
->accept(this);
303 ir
->lod_info
.grad
.dPdx
->accept(this);
305 ir
->lod_info
.grad
.dPdy
->accept(this);
313 void ir_print_visitor::visit(ir_swizzle
*ir
)
315 const unsigned swiz
[4] = {
323 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
324 printf("%c", "xyzw"[swiz
[i
]]);
327 ir
->val
->accept(this);
332 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
334 ir_variable
*var
= ir
->variable_referenced();
335 printf("(var_ref %s) ", unique_name(var
));
339 void ir_print_visitor::visit(ir_dereference_array
*ir
)
341 printf("(array_ref ");
342 ir
->array
->accept(this);
343 ir
->array_index
->accept(this);
348 void ir_print_visitor::visit(ir_dereference_record
*ir
)
350 printf("(record_ref ");
351 ir
->record
->accept(this);
352 printf(" %s) ", ir
->field
);
356 void ir_print_visitor::visit(ir_assignment
*ir
)
361 ir
->condition
->accept(this);
366 for (unsigned i
= 0; i
< 4; i
++) {
367 if ((ir
->write_mask
& (1 << i
)) != 0) {
374 printf(" (%s) ", mask
);
376 ir
->lhs
->accept(this);
380 ir
->rhs
->accept(this);
385 void ir_print_visitor::visit(ir_constant
*ir
)
387 printf("(constant ");
388 print_type(ir
->type
);
391 if (ir
->type
->is_array()) {
392 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
393 ir
->get_array_element(i
)->accept(this);
394 } else if (ir
->type
->is_record()) {
395 ir_constant
*value
= (ir_constant
*) ir
->components
.get_head();
396 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
397 printf("(%s ", ir
->type
->fields
.structure
[i
].name
);
401 value
= (ir_constant
*) value
->next
;
404 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
407 switch (ir
->type
->base_type
) {
408 case GLSL_TYPE_UINT
: printf("%u", ir
->value
.u
[i
]); break;
409 case GLSL_TYPE_INT
: printf("%d", ir
->value
.i
[i
]); break;
410 case GLSL_TYPE_FLOAT
:
411 if (ir
->value
.f
[i
] == 0.0f
)
412 /* 0.0 == -0.0, so print with %f to get the proper sign. */
413 printf("%.1f", ir
->value
.f
[i
]);
414 else if (abs(ir
->value
.f
[i
]) < 0.000001f
)
415 printf("%a", ir
->value
.f
[i
]);
416 else if (abs(ir
->value
.f
[i
]) > 1000000.0f
)
417 printf("%e", ir
->value
.f
[i
]);
419 printf("%f", ir
->value
.f
[i
]);
421 case GLSL_TYPE_BOOL
: printf("%d", ir
->value
.b
[i
]); break;
431 ir_print_visitor::visit(ir_call
*ir
)
433 printf("(call %s ", ir
->callee_name());
434 if (ir
->return_deref
)
435 ir
->return_deref
->accept(this);
437 foreach_iter(exec_list_iterator
, iter
, *ir
) {
438 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
447 ir_print_visitor::visit(ir_return
*ir
)
451 ir_rvalue
*const value
= ir
->get_value();
462 ir_print_visitor::visit(ir_discard
*ir
)
466 if (ir
->condition
!= NULL
) {
468 ir
->condition
->accept(this);
476 ir_print_visitor::visit(ir_if
*ir
)
479 ir
->condition
->accept(this);
484 foreach_iter(exec_list_iterator
, iter
, ir
->then_instructions
) {
485 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
497 if (!ir
->else_instructions
.is_empty()) {
501 foreach_iter(exec_list_iterator
, iter
, ir
->else_instructions
) {
502 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
518 ir_print_visitor::visit(ir_loop
*ir
)
521 if (ir
->counter
!= NULL
)
522 ir
->counter
->accept(this);
524 if (ir
->from
!= NULL
)
525 ir
->from
->accept(this);
528 ir
->to
->accept(this);
530 if (ir
->increment
!= NULL
)
531 ir
->increment
->accept(this);
535 foreach_iter(exec_list_iterator
, iter
, ir
->body_instructions
) {
536 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
549 ir_print_visitor::visit(ir_loop_jump
*ir
)
551 printf("%s", ir
->is_break() ? "break" : "continue");
555 ir_print_visitor::visit(ir_emit_vertex
*ir
)
557 printf("(emit-vertex)");
561 ir_print_visitor::visit(ir_end_primitive
*ir
)
563 printf("(end-primitive)");