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_list(n
, instructions
) {
65 ir_instruction
*ir
= (ir_instruction
*) n
;
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
->data
.centroid
) ? "centroid " : "";
152 const char *const samp
= (ir
->data
.sample
) ? "sample " : "";
153 const char *const inv
= (ir
->data
.invariant
) ? "invariant " : "";
154 const char *const mode
[] = { "", "uniform ", "shader_in ", "shader_out ",
155 "in ", "out ", "inout ",
156 "const_in ", "sys ", "temporary " };
157 STATIC_ASSERT(ARRAY_SIZE(mode
) == ir_var_mode_count
);
158 const char *const interp
[] = { "", "smooth", "flat", "noperspective" };
159 STATIC_ASSERT(ARRAY_SIZE(interp
) == INTERP_QUALIFIER_COUNT
);
161 printf("(%s%s%s%s%s) ",
162 cent
, samp
, inv
, mode
[ir
->data
.mode
], interp
[ir
->data
.interpolation
]);
164 print_type(ir
->type
);
165 printf(" %s)", unique_name(ir
));
169 void ir_print_visitor::visit(ir_function_signature
*ir
)
171 _mesa_symbol_table_push_scope(symbols
);
172 printf("(signature ");
175 print_type(ir
->return_type
);
179 printf("(parameters\n");
182 foreach_list(n
, &ir
->parameters
) {
183 ir_variable
*const inst
= (ir_variable
*) n
;
199 foreach_list(n
, &ir
->body
) {
200 ir_instruction
*const inst
= (ir_instruction
*) n
;
210 _mesa_symbol_table_pop_scope(symbols
);
214 void ir_print_visitor::visit(ir_function
*ir
)
216 printf("(function %s\n", ir
->name
);
218 foreach_list(n
, &ir
->signatures
) {
219 ir_function_signature
*const sig
= (ir_function_signature
*) n
;
230 void ir_print_visitor::visit(ir_expression
*ir
)
232 printf("(expression ");
234 print_type(ir
->type
);
236 printf(" %s ", ir
->operator_string());
238 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++) {
239 ir
->operands
[i
]->accept(this);
246 void ir_print_visitor::visit(ir_texture
*ir
)
248 printf("(%s ", ir
->opcode_string());
250 print_type(ir
->type
);
253 ir
->sampler
->accept(this);
256 if (ir
->op
!= ir_txs
&& ir
->op
!= ir_query_levels
) {
257 ir
->coordinate
->accept(this);
261 if (ir
->offset
!= NULL
) {
262 ir
->offset
->accept(this);
270 if (ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
&&
271 ir
->op
!= ir_txs
&& ir
->op
!= ir_tg4
&&
272 ir
->op
!= ir_query_levels
) {
274 ir
->projector
->accept(this);
278 if (ir
->shadow_comparitor
) {
280 ir
->shadow_comparitor
->accept(this);
291 case ir_query_levels
:
294 ir
->lod_info
.bias
->accept(this);
299 ir
->lod_info
.lod
->accept(this);
302 ir
->lod_info
.sample_index
->accept(this);
306 ir
->lod_info
.grad
.dPdx
->accept(this);
308 ir
->lod_info
.grad
.dPdy
->accept(this);
312 ir
->lod_info
.component
->accept(this);
319 void ir_print_visitor::visit(ir_swizzle
*ir
)
321 const unsigned swiz
[4] = {
329 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
330 printf("%c", "xyzw"[swiz
[i
]]);
333 ir
->val
->accept(this);
338 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
340 ir_variable
*var
= ir
->variable_referenced();
341 printf("(var_ref %s) ", unique_name(var
));
345 void ir_print_visitor::visit(ir_dereference_array
*ir
)
347 printf("(array_ref ");
348 ir
->array
->accept(this);
349 ir
->array_index
->accept(this);
354 void ir_print_visitor::visit(ir_dereference_record
*ir
)
356 printf("(record_ref ");
357 ir
->record
->accept(this);
358 printf(" %s) ", ir
->field
);
362 void ir_print_visitor::visit(ir_assignment
*ir
)
367 ir
->condition
->accept(this);
372 for (unsigned i
= 0; i
< 4; i
++) {
373 if ((ir
->write_mask
& (1 << i
)) != 0) {
380 printf(" (%s) ", mask
);
382 ir
->lhs
->accept(this);
386 ir
->rhs
->accept(this);
391 void ir_print_visitor::visit(ir_constant
*ir
)
393 printf("(constant ");
394 print_type(ir
->type
);
397 if (ir
->type
->is_array()) {
398 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
399 ir
->get_array_element(i
)->accept(this);
400 } else if (ir
->type
->is_record()) {
401 ir_constant
*value
= (ir_constant
*) ir
->components
.get_head();
402 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
403 printf("(%s ", ir
->type
->fields
.structure
[i
].name
);
407 value
= (ir_constant
*) value
->next
;
410 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
413 switch (ir
->type
->base_type
) {
414 case GLSL_TYPE_UINT
: printf("%u", ir
->value
.u
[i
]); break;
415 case GLSL_TYPE_INT
: printf("%d", ir
->value
.i
[i
]); break;
416 case GLSL_TYPE_FLOAT
:
417 if (ir
->value
.f
[i
] == 0.0f
)
418 /* 0.0 == -0.0, so print with %f to get the proper sign. */
419 printf("%.1f", ir
->value
.f
[i
]);
420 else if (fabs(ir
->value
.f
[i
]) < 0.000001f
)
421 printf("%a", ir
->value
.f
[i
]);
422 else if (fabs(ir
->value
.f
[i
]) > 1000000.0f
)
423 printf("%e", ir
->value
.f
[i
]);
425 printf("%f", ir
->value
.f
[i
]);
427 case GLSL_TYPE_BOOL
: printf("%d", ir
->value
.b
[i
]); break;
437 ir_print_visitor::visit(ir_call
*ir
)
439 printf("(call %s ", ir
->callee_name());
440 if (ir
->return_deref
)
441 ir
->return_deref
->accept(this);
443 foreach_list(n
, &ir
->actual_parameters
) {
444 ir_rvalue
*const param
= (ir_rvalue
*) n
;
453 ir_print_visitor::visit(ir_return
*ir
)
457 ir_rvalue
*const value
= ir
->get_value();
468 ir_print_visitor::visit(ir_discard
*ir
)
472 if (ir
->condition
!= NULL
) {
474 ir
->condition
->accept(this);
482 ir_print_visitor::visit(ir_if
*ir
)
485 ir
->condition
->accept(this);
490 foreach_list(n
, &ir
->then_instructions
) {
491 ir_instruction
*const inst
= (ir_instruction
*) n
;
503 if (!ir
->else_instructions
.is_empty()) {
507 foreach_list(n
, &ir
->else_instructions
) {
508 ir_instruction
*const inst
= (ir_instruction
*) n
;
524 ir_print_visitor::visit(ir_loop
*ir
)
529 foreach_list(n
, &ir
->body_instructions
) {
530 ir_instruction
*const inst
= (ir_instruction
*) n
;
543 ir_print_visitor::visit(ir_loop_jump
*ir
)
545 printf("%s", ir
->is_break() ? "break" : "continue");
549 ir_print_visitor::visit(ir_emit_vertex
*ir
)
551 printf("(emit-vertex)");
555 ir_print_visitor::visit(ir_end_primitive
*ir
)
557 printf("(end-primitive)");