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"
29 #include "program/hash_table.h"
32 static void print_type(const glsl_type
*t
);
35 ir_instruction::print(void) const
37 ir_instruction
*deconsted
= const_cast<ir_instruction
*>(this);
40 deconsted
->accept(&v
);
44 _mesa_print_ir(exec_list
*instructions
,
45 struct _mesa_glsl_parse_state
*state
)
48 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
49 const glsl_type
*const s
= state
->user_structures
[i
];
51 printf("(structure (%s) (%s@%p) (%u) (\n",
52 s
->name
, s
->name
, (void *) s
, s
->length
);
54 for (unsigned j
= 0; j
< s
->length
; j
++) {
56 print_type(s
->fields
.structure
[j
].type
);
57 printf(")(%s))\n", s
->fields
.structure
[j
].name
);
65 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
66 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
68 if (ir
->ir_type
!= ir_type_function
)
74 ir_print_visitor::ir_print_visitor()
78 hash_table_ctor(32, hash_table_pointer_hash
, hash_table_pointer_compare
);
79 symbols
= _mesa_symbol_table_ctor();
80 mem_ctx
= ralloc_context(NULL
);
83 ir_print_visitor::~ir_print_visitor()
85 hash_table_dtor(printable_names
);
86 _mesa_symbol_table_dtor(symbols
);
90 void ir_print_visitor::indent(void)
92 for (int i
= 0; i
< indentation
; i
++)
97 ir_print_visitor::unique_name(ir_variable
*var
)
99 /* Do we already have a name for this variable? */
100 const char *name
= (const char *) hash_table_find(this->printable_names
, var
);
104 /* If there's no conflict, just use the original name */
105 if (_mesa_symbol_table_find_symbol(this->symbols
, -1, var
->name
) == NULL
) {
108 static unsigned i
= 1;
109 name
= ralloc_asprintf(this->mem_ctx
, "%s@%u", var
->name
, ++i
);
111 hash_table_insert(this->printable_names
, (void *) name
, var
);
112 _mesa_symbol_table_add_symbol(this->symbols
, -1, name
, var
);
117 print_type(const glsl_type
*t
)
119 if (t
->base_type
== GLSL_TYPE_ARRAY
) {
121 print_type(t
->fields
.array
);
122 printf(" %u)", t
->length
);
123 } else if ((t
->base_type
== GLSL_TYPE_STRUCT
)
124 && (strncmp("gl_", t
->name
, 3) != 0)) {
125 printf("%s@%p", t
->name
, (void *) t
);
127 printf("%s", t
->name
);
132 void ir_print_visitor::visit(ir_variable
*ir
)
136 const char *const cent
= (ir
->centroid
) ? "centroid " : "";
137 const char *const inv
= (ir
->invariant
) ? "invariant " : "";
138 const char *const mode
[] = { "", "uniform ", "in ", "out ", "inout ",
139 "const_in ", "sys ", "temporary " };
140 const char *const interp
[] = { "", "flat", "noperspective" };
142 printf("(%s%s%s%s) ",
143 cent
, inv
, mode
[ir
->mode
], interp
[ir
->interpolation
]);
145 print_type(ir
->type
);
146 printf(" %s)", unique_name(ir
));
150 void ir_print_visitor::visit(ir_function_signature
*ir
)
152 _mesa_symbol_table_push_scope(symbols
);
153 printf("(signature ");
156 print_type(ir
->return_type
);
160 printf("(parameters\n");
163 foreach_iter(exec_list_iterator
, iter
, ir
->parameters
) {
164 ir_variable
*const inst
= (ir_variable
*) iter
.get();
180 foreach_iter(exec_list_iterator
, iter
, ir
->body
) {
181 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
191 _mesa_symbol_table_pop_scope(symbols
);
195 void ir_print_visitor::visit(ir_function
*ir
)
197 printf("(function %s\n", ir
->name
);
199 foreach_iter(exec_list_iterator
, iter
, *ir
) {
200 ir_function_signature
*const sig
= (ir_function_signature
*) iter
.get();
211 void ir_print_visitor::visit(ir_expression
*ir
)
213 printf("(expression ");
215 print_type(ir
->type
);
217 printf(" %s ", ir
->operator_string());
219 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++) {
220 ir
->operands
[i
]->accept(this);
227 void ir_print_visitor::visit(ir_texture
*ir
)
229 printf("(%s ", ir
->opcode_string());
231 print_type(ir
->type
);
234 ir
->sampler
->accept(this);
237 ir
->coordinate
->accept(this);
241 if (ir
->offset
!= NULL
) {
242 ir
->offset
->accept(this);
249 if (ir
->op
!= ir_txf
) {
251 ir
->projector
->accept(this);
255 if (ir
->shadow_comparitor
) {
257 ir
->shadow_comparitor
->accept(this);
269 ir
->lod_info
.bias
->accept(this);
273 ir
->lod_info
.lod
->accept(this);
277 ir
->lod_info
.grad
.dPdx
->accept(this);
279 ir
->lod_info
.grad
.dPdy
->accept(this);
287 void ir_print_visitor::visit(ir_swizzle
*ir
)
289 const unsigned swiz
[4] = {
297 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
298 printf("%c", "xyzw"[swiz
[i
]]);
301 ir
->val
->accept(this);
306 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
308 ir_variable
*var
= ir
->variable_referenced();
309 printf("(var_ref %s) ", unique_name(var
));
313 void ir_print_visitor::visit(ir_dereference_array
*ir
)
315 printf("(array_ref ");
316 ir
->array
->accept(this);
317 ir
->array_index
->accept(this);
322 void ir_print_visitor::visit(ir_dereference_record
*ir
)
324 printf("(record_ref ");
325 ir
->record
->accept(this);
326 printf(" %s) ", ir
->field
);
330 void ir_print_visitor::visit(ir_assignment
*ir
)
335 ir
->condition
->accept(this);
340 for (unsigned i
= 0; i
< 4; i
++) {
341 if ((ir
->write_mask
& (1 << i
)) != 0) {
348 printf(" (%s) ", mask
);
350 ir
->lhs
->accept(this);
354 ir
->rhs
->accept(this);
359 void ir_print_visitor::visit(ir_constant
*ir
)
361 const glsl_type
*const base_type
= ir
->type
->get_base_type();
363 printf("(constant ");
364 print_type(ir
->type
);
367 if (ir
->type
->is_array()) {
368 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
369 ir
->get_array_element(i
)->accept(this);
370 } else if (ir
->type
->is_record()) {
371 ir_constant
*value
= (ir_constant
*) ir
->components
.get_head();
372 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
373 printf("(%s ", ir
->type
->fields
.structure
[i
].name
);
377 value
= (ir_constant
*) value
->next
;
380 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
383 switch (base_type
->base_type
) {
384 case GLSL_TYPE_UINT
: printf("%u", ir
->value
.u
[i
]); break;
385 case GLSL_TYPE_INT
: printf("%d", ir
->value
.i
[i
]); break;
386 case GLSL_TYPE_FLOAT
: printf("%f", ir
->value
.f
[i
]); break;
387 case GLSL_TYPE_BOOL
: printf("%d", ir
->value
.b
[i
]); break;
397 ir_print_visitor::visit(ir_call
*ir
)
399 printf("(call %s (", ir
->callee_name());
400 foreach_iter(exec_list_iterator
, iter
, *ir
) {
401 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
410 ir_print_visitor::visit(ir_return
*ir
)
414 ir_rvalue
*const value
= ir
->get_value();
425 ir_print_visitor::visit(ir_discard
*ir
)
429 if (ir
->condition
!= NULL
) {
431 ir
->condition
->accept(this);
439 ir_print_visitor::visit(ir_if
*ir
)
442 ir
->condition
->accept(this);
447 foreach_iter(exec_list_iterator
, iter
, ir
->then_instructions
) {
448 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
460 if (!ir
->else_instructions
.is_empty()) {
464 foreach_iter(exec_list_iterator
, iter
, ir
->else_instructions
) {
465 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
481 ir_print_visitor::visit(ir_loop
*ir
)
484 if (ir
->counter
!= NULL
)
485 ir
->counter
->accept(this);
487 if (ir
->from
!= NULL
)
488 ir
->from
->accept(this);
491 ir
->to
->accept(this);
493 if (ir
->increment
!= NULL
)
494 ir
->increment
->accept(this);
498 foreach_iter(exec_list_iterator
, iter
, ir
->body_instructions
) {
499 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
512 ir_print_visitor::visit(ir_loop_jump
*ir
)
514 printf("%s", ir
->is_break() ? "break" : "continue");