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);
42 _mesa_print_ir(exec_list *instructions,
43 struct _mesa_glsl_parse_state *state)
46 for (unsigned i = 0; i < state->num_user_structures; i++) {
47 const glsl_type *const s = state->user_structures[i];
49 printf("(structure (%s) (%s@%p) (%u) (\n",
50 s->name, s->name, (void *) s, s->length);
52 for (unsigned j = 0; j < s->length; j++) {
54 print_type(s->fields.structure[j].type);
55 printf(")(%s))\n", s->fields.structure[j].name);
63 foreach_iter(exec_list_iterator, iter, *instructions) {
64 ir_instruction *ir = (ir_instruction *)iter.get();
66 if (ir->ir_type != ir_type_function)
72 ir_print_visitor::ir_print_visitor()
76 hash_table_ctor(32, hash_table_pointer_hash, hash_table_pointer_compare);
77 symbols = _mesa_symbol_table_ctor();
78 mem_ctx = ralloc_context(NULL);
81 ir_print_visitor::~ir_print_visitor()
83 hash_table_dtor(printable_names);
84 _mesa_symbol_table_dtor(symbols);
88 void ir_print_visitor::indent(void)
90 for (int i = 0; i < indentation; i++)
95 ir_print_visitor::unique_name(ir_variable *var)
97 /* var->name can be NULL in function prototypes when a type is given for a
98 * parameter but no name is given. In that case, just return an empty
99 * string. Don't worry about tracking the generated name in the printable
100 * names hash because this is the only scope where it can ever appear.
102 if (var->name == NULL) {
103 static unsigned arg = 1;
104 return ralloc_asprintf(this->mem_ctx, "parameter@%u", arg++);
107 /* Do we already have a name for this variable? */
108 const char *name = (const char *) hash_table_find(this->printable_names, var);
112 /* If there's no conflict, just use the original name */
113 if (_mesa_symbol_table_find_symbol(this->symbols, -1, var->name) == NULL) {
116 static unsigned i = 1;
117 name = ralloc_asprintf(this->mem_ctx, "%s@%u", var->name, ++i);
119 hash_table_insert(this->printable_names, (void *) name, var);
120 _mesa_symbol_table_add_symbol(this->symbols, -1, name, var);
125 print_type(const glsl_type *t)
127 if (t->base_type == GLSL_TYPE_ARRAY) {
129 print_type(t->fields.array);
130 printf(" %u)", t->length);
131 } else if ((t->base_type == GLSL_TYPE_STRUCT)
132 && (strncmp("gl_", t->name, 3) != 0)) {
133 printf("%s@%p", t->name, (void *) t);
135 printf("%s", t->name);
139 void ir_print_visitor::visit(ir_rvalue *ir)
144 void ir_print_visitor::visit(ir_variable *ir)
148 const char *const cent = (ir->centroid) ? "centroid " : "";
149 const char *const inv = (ir->invariant) ? "invariant " : "";
150 const char *const mode[] = { "", "uniform ", "shader_in ", "shader_out ",
151 "in ", "out ", "inout ",
152 "const_in ", "sys ", "temporary " };
153 STATIC_ASSERT(ARRAY_SIZE(mode) == ir_var_mode_count);
154 const char *const interp[] = { "", "smooth", "flat", "noperspective" };
155 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_QUALIFIER_COUNT);
157 printf("(%s%s%s%s) ",
158 cent, inv, mode[ir->mode], interp[ir->interpolation]);
160 print_type(ir->type);
161 printf(" %s)", unique_name(ir));
165 void ir_print_visitor::visit(ir_function_signature *ir)
167 _mesa_symbol_table_push_scope(symbols);
168 printf("(signature ");
171 print_type(ir->return_type);
175 printf("(parameters\n");
178 foreach_iter(exec_list_iterator, iter, ir->parameters) {
179 ir_variable *const inst = (ir_variable *) iter.get();
195 foreach_iter(exec_list_iterator, iter, ir->body) {
196 ir_instruction *const inst = (ir_instruction *) iter.get();
206 _mesa_symbol_table_pop_scope(symbols);
210 void ir_print_visitor::visit(ir_function *ir)
212 printf("(function %s\n", ir->name);
214 foreach_iter(exec_list_iterator, iter, *ir) {
215 ir_function_signature *const sig = (ir_function_signature *) iter.get();
226 void ir_print_visitor::visit(ir_expression *ir)
228 printf("(expression ");
230 print_type(ir->type);
232 printf(" %s ", ir->operator_string());
234 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
235 ir->operands[i]->accept(this);
242 void ir_print_visitor::visit(ir_texture *ir)
244 printf("(%s ", ir->opcode_string());
246 print_type(ir->type);
249 ir->sampler->accept(this);
252 if (ir->op != ir_txs) {
253 ir->coordinate->accept(this);
257 if (ir->offset != NULL) {
258 ir->offset->accept(this);
266 if (ir->op != ir_txf && ir->op != ir_txf_ms && ir->op != ir_txs) {
268 ir->projector->accept(this);
272 if (ir->shadow_comparitor) {
274 ir->shadow_comparitor->accept(this);
287 ir->lod_info.bias->accept(this);
292 ir->lod_info.lod->accept(this);
295 ir->lod_info.sample_index->accept(this);
299 ir->lod_info.grad.dPdx->accept(this);
301 ir->lod_info.grad.dPdy->accept(this);
309 void ir_print_visitor::visit(ir_swizzle *ir)
311 const unsigned swiz[4] = {
319 for (unsigned i = 0; i < ir->mask.num_components; i++) {
320 printf("%c", "xyzw"[swiz[i]]);
323 ir->val->accept(this);
328 void ir_print_visitor::visit(ir_dereference_variable *ir)
330 ir_variable *var = ir->variable_referenced();
331 printf("(var_ref %s) ", unique_name(var));
335 void ir_print_visitor::visit(ir_dereference_array *ir)
337 printf("(array_ref ");
338 ir->array->accept(this);
339 ir->array_index->accept(this);
344 void ir_print_visitor::visit(ir_dereference_record *ir)
346 printf("(record_ref ");
347 ir->record->accept(this);
348 printf(" %s) ", ir->field);
352 void ir_print_visitor::visit(ir_assignment *ir)
357 ir->condition->accept(this);
362 for (unsigned i = 0; i < 4; i++) {
363 if ((ir->write_mask & (1 << i)) != 0) {
370 printf(" (%s) ", mask);
372 ir->lhs->accept(this);
376 ir->rhs->accept(this);
381 void ir_print_visitor::visit(ir_constant *ir)
383 printf("(constant ");
384 print_type(ir->type);
387 if (ir->type->is_array()) {
388 for (unsigned i = 0; i < ir->type->length; i++)
389 ir->get_array_element(i)->accept(this);
390 } else if (ir->type->is_record()) {
391 ir_constant *value = (ir_constant *) ir->components.get_head();
392 for (unsigned i = 0; i < ir->type->length; i++) {
393 printf("(%s ", ir->type->fields.structure[i].name);
397 value = (ir_constant *) value->next;
400 for (unsigned i = 0; i < ir->type->components(); i++) {
403 switch (ir->type->base_type) {
404 case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
405 case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
406 case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
407 case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
417 ir_print_visitor::visit(ir_call *ir)
419 printf("(call %s ", ir->callee_name());
420 if (ir->return_deref)
421 ir->return_deref->accept(this);
423 foreach_iter(exec_list_iterator, iter, *ir) {
424 ir_instruction *const inst = (ir_instruction *) iter.get();
433 ir_print_visitor::visit(ir_return *ir)
437 ir_rvalue *const value = ir->get_value();
448 ir_print_visitor::visit(ir_discard *ir)
452 if (ir->condition != NULL) {
454 ir->condition->accept(this);
462 ir_print_visitor::visit(ir_if *ir)
465 ir->condition->accept(this);
470 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
471 ir_instruction *const inst = (ir_instruction *) iter.get();
483 if (!ir->else_instructions.is_empty()) {
487 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
488 ir_instruction *const inst = (ir_instruction *) iter.get();
504 ir_print_visitor::visit(ir_loop *ir)
507 if (ir->counter != NULL)
508 ir->counter->accept(this);
510 if (ir->from != NULL)
511 ir->from->accept(this);
514 ir->to->accept(this);
516 if (ir->increment != NULL)
517 ir->increment->accept(this);
521 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
522 ir_instruction *const inst = (ir_instruction *) iter.get();
535 ir_print_visitor::visit(ir_loop_jump *ir)
537 printf("%s", ir->is_break() ? "break" : "continue");