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 "program/hash_table.h"
29 static void print_type(const glsl_type
*t
);
32 ir_instruction::print(void) const
34 ir_instruction
*deconsted
= const_cast<ir_instruction
*>(this);
37 deconsted
->accept(&v
);
41 _mesa_print_ir(exec_list
*instructions
,
42 struct _mesa_glsl_parse_state
*state
)
45 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
46 const glsl_type
*const s
= state
->user_structures
[i
];
48 printf("(structure (%s) (%s@%p) (%u) (\n",
49 s
->name
, s
->name
, (void *) s
, s
->length
);
51 for (unsigned j
= 0; j
< s
->length
; j
++) {
53 print_type(s
->fields
.structure
[j
].type
);
54 printf(")(%s))\n", s
->fields
.structure
[j
].name
);
62 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
63 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
65 if (ir
->ir_type
!= ir_type_function
)
71 ir_print_visitor::ir_print_visitor()
75 hash_table_ctor(32, hash_table_pointer_hash
, hash_table_pointer_compare
);
76 symbols
= _mesa_symbol_table_ctor();
77 mem_ctx
= ralloc_context(NULL
);
80 ir_print_visitor::~ir_print_visitor()
82 hash_table_dtor(printable_names
);
83 _mesa_symbol_table_dtor(symbols
);
87 void ir_print_visitor::indent(void)
89 for (int i
= 0; i
< indentation
; i
++)
94 ir_print_visitor::unique_name(ir_variable
*var
)
96 /* var->name can be NULL in function prototypes when a type is given for a
97 * parameter but no name is given. In that case, just return an empty
98 * string. Don't worry about tracking the generated name in the printable
99 * names hash because this is the only scope where it can ever appear.
101 if (var
->name
== NULL
) {
102 static unsigned arg
= 1;
103 return ralloc_asprintf(this->mem_ctx
, "parameter@%u", arg
++);
106 /* Do we already have a name for this variable? */
107 const char *name
= (const char *) hash_table_find(this->printable_names
, var
);
111 /* If there's no conflict, just use the original name */
112 if (_mesa_symbol_table_find_symbol(this->symbols
, -1, var
->name
) == NULL
) {
115 static unsigned i
= 1;
116 name
= ralloc_asprintf(this->mem_ctx
, "%s@%u", var
->name
, ++i
);
118 hash_table_insert(this->printable_names
, (void *) name
, var
);
119 _mesa_symbol_table_add_symbol(this->symbols
, -1, name
, var
);
124 print_type(const glsl_type
*t
)
126 if (t
->base_type
== GLSL_TYPE_ARRAY
) {
128 print_type(t
->fields
.array
);
129 printf(" %u)", t
->length
);
130 } else if ((t
->base_type
== GLSL_TYPE_STRUCT
)
131 && (strncmp("gl_", t
->name
, 3) != 0)) {
132 printf("%s@%p", t
->name
, (void *) t
);
134 printf("%s", t
->name
);
139 void ir_print_visitor::visit(ir_variable
*ir
)
143 const char *const cent
= (ir
->centroid
) ? "centroid " : "";
144 const char *const inv
= (ir
->invariant
) ? "invariant " : "";
145 const char *const mode
[] = { "", "uniform ", "in ", "out ", "inout ",
146 "const_in ", "sys ", "temporary " };
147 const char *const interp
[] = { "", "flat", "noperspective" };
149 printf("(%s%s%s%s) ",
150 cent
, inv
, mode
[ir
->mode
], interp
[ir
->interpolation
]);
152 print_type(ir
->type
);
153 printf(" %s)", unique_name(ir
));
157 void ir_print_visitor::visit(ir_function_signature
*ir
)
159 _mesa_symbol_table_push_scope(symbols
);
160 printf("(signature ");
163 print_type(ir
->return_type
);
167 printf("(parameters\n");
170 foreach_iter(exec_list_iterator
, iter
, ir
->parameters
) {
171 ir_variable
*const inst
= (ir_variable
*) iter
.get();
187 foreach_iter(exec_list_iterator
, iter
, ir
->body
) {
188 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
198 _mesa_symbol_table_pop_scope(symbols
);
202 void ir_print_visitor::visit(ir_function
*ir
)
204 printf("(function %s\n", ir
->name
);
206 foreach_iter(exec_list_iterator
, iter
, *ir
) {
207 ir_function_signature
*const sig
= (ir_function_signature
*) iter
.get();
218 void ir_print_visitor::visit(ir_expression
*ir
)
220 printf("(expression ");
222 print_type(ir
->type
);
224 printf(" %s ", ir
->operator_string());
226 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++) {
227 ir
->operands
[i
]->accept(this);
234 void ir_print_visitor::visit(ir_texture
*ir
)
236 printf("(%s ", ir
->opcode_string());
238 print_type(ir
->type
);
241 ir
->sampler
->accept(this);
244 if (ir
->op
!= ir_txs
) {
245 ir
->coordinate
->accept(this);
249 if (ir
->offset
!= NULL
) {
250 ir
->offset
->accept(this);
258 if (ir
->op
!= ir_txf
&& ir
->op
!= ir_txs
) {
260 ir
->projector
->accept(this);
264 if (ir
->shadow_comparitor
) {
266 ir
->shadow_comparitor
->accept(this);
278 ir
->lod_info
.bias
->accept(this);
283 ir
->lod_info
.lod
->accept(this);
287 ir
->lod_info
.grad
.dPdx
->accept(this);
289 ir
->lod_info
.grad
.dPdy
->accept(this);
297 void ir_print_visitor::visit(ir_swizzle
*ir
)
299 const unsigned swiz
[4] = {
307 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
308 printf("%c", "xyzw"[swiz
[i
]]);
311 ir
->val
->accept(this);
316 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
318 ir_variable
*var
= ir
->variable_referenced();
319 printf("(var_ref %s) ", unique_name(var
));
323 void ir_print_visitor::visit(ir_dereference_array
*ir
)
325 printf("(array_ref ");
326 ir
->array
->accept(this);
327 ir
->array_index
->accept(this);
332 void ir_print_visitor::visit(ir_dereference_record
*ir
)
334 printf("(record_ref ");
335 ir
->record
->accept(this);
336 printf(" %s) ", ir
->field
);
340 void ir_print_visitor::visit(ir_assignment
*ir
)
345 ir
->condition
->accept(this);
350 for (unsigned i
= 0; i
< 4; i
++) {
351 if ((ir
->write_mask
& (1 << i
)) != 0) {
358 printf(" (%s) ", mask
);
360 ir
->lhs
->accept(this);
364 ir
->rhs
->accept(this);
369 void ir_print_visitor::visit(ir_constant
*ir
)
371 printf("(constant ");
372 print_type(ir
->type
);
375 if (ir
->type
->is_array()) {
376 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
377 ir
->get_array_element(i
)->accept(this);
378 } else if (ir
->type
->is_record()) {
379 ir_constant
*value
= (ir_constant
*) ir
->components
.get_head();
380 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
381 printf("(%s ", ir
->type
->fields
.structure
[i
].name
);
385 value
= (ir_constant
*) value
->next
;
388 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
391 switch (ir
->type
->base_type
) {
392 case GLSL_TYPE_UINT
: printf("%u", ir
->value
.u
[i
]); break;
393 case GLSL_TYPE_INT
: printf("%d", ir
->value
.i
[i
]); break;
394 case GLSL_TYPE_FLOAT
: printf("%f", ir
->value
.f
[i
]); break;
395 case GLSL_TYPE_BOOL
: printf("%d", ir
->value
.b
[i
]); break;
405 ir_print_visitor::visit(ir_call
*ir
)
407 printf("(call %s (", ir
->callee_name());
408 foreach_iter(exec_list_iterator
, iter
, *ir
) {
409 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
418 ir_print_visitor::visit(ir_return
*ir
)
422 ir_rvalue
*const value
= ir
->get_value();
433 ir_print_visitor::visit(ir_discard
*ir
)
437 if (ir
->condition
!= NULL
) {
439 ir
->condition
->accept(this);
447 ir_print_visitor::visit(ir_if
*ir
)
450 ir
->condition
->accept(this);
455 foreach_iter(exec_list_iterator
, iter
, ir
->then_instructions
) {
456 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
468 if (!ir
->else_instructions
.is_empty()) {
472 foreach_iter(exec_list_iterator
, iter
, ir
->else_instructions
) {
473 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
489 ir_print_visitor::visit(ir_loop
*ir
)
492 if (ir
->counter
!= NULL
)
493 ir
->counter
->accept(this);
495 if (ir
->from
!= NULL
)
496 ir
->from
->accept(this);
499 ir
->to
->accept(this);
501 if (ir
->increment
!= NULL
)
502 ir
->increment
->accept(this);
506 foreach_iter(exec_list_iterator
, iter
, ir
->body_instructions
) {
507 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
520 ir_print_visitor::visit(ir_loop_jump
*ir
)
522 printf("%s", ir
->is_break() ? "break" : "continue");