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 <inttypes.h> /* for PRIx64 macro */
25 #include "ir_print_visitor.h"
26 #include "compiler/glsl_types.h"
27 #include "glsl_parser_extras.h"
28 #include "main/macros.h"
29 #include "util/hash_table.h"
31 static void print_type(FILE *f
, const glsl_type
*t
);
34 ir_instruction::print(void) const
40 ir_instruction::fprint(FILE *f
) const
42 ir_instruction
*deconsted
= const_cast<ir_instruction
*>(this);
44 ir_print_visitor
v(f
);
45 deconsted
->accept(&v
);
50 _mesa_print_ir(FILE *f
, exec_list
*instructions
,
51 struct _mesa_glsl_parse_state
*state
)
54 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
55 const glsl_type
*const s
= state
->user_structures
[i
];
57 fprintf(f
, "(structure (%s) (%s@%p) (%u) (\n",
58 s
->name
, s
->name
, (void *) s
, s
->length
);
60 for (unsigned j
= 0; j
< s
->length
; j
++) {
62 print_type(f
, s
->fields
.structure
[j
].type
);
63 fprintf(f
, ")(%s))\n", s
->fields
.structure
[j
].name
);
71 foreach_in_list(ir_instruction
, ir
, instructions
) {
73 if (ir
->ir_type
!= ir_type_function
)
80 fprint_ir(FILE *f
, const void *instruction
)
82 const ir_instruction
*ir
= (const ir_instruction
*)instruction
;
88 ir_print_visitor::ir_print_visitor(FILE *f
)
93 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
, _mesa_key_pointer_equal
);
94 symbols
= _mesa_symbol_table_ctor();
95 mem_ctx
= ralloc_context(NULL
);
98 ir_print_visitor::~ir_print_visitor()
100 _mesa_hash_table_destroy(printable_names
, NULL
);
101 _mesa_symbol_table_dtor(symbols
);
102 ralloc_free(mem_ctx
);
105 void ir_print_visitor::indent(void)
107 for (int i
= 0; i
< indentation
; i
++)
112 ir_print_visitor::unique_name(ir_variable
*var
)
114 /* var->name can be NULL in function prototypes when a type is given for a
115 * parameter but no name is given. In that case, just return an empty
116 * string. Don't worry about tracking the generated name in the printable
117 * names hash because this is the only scope where it can ever appear.
119 if (var
->name
== NULL
) {
120 static unsigned arg
= 1;
121 return ralloc_asprintf(this->mem_ctx
, "parameter@%u", arg
++);
124 /* Do we already have a name for this variable? */
125 struct hash_entry
* entry
=
126 _mesa_hash_table_search(this->printable_names
, var
);
129 return (const char *) entry
->data
;
132 /* If there's no conflict, just use the original name */
133 const char* name
= NULL
;
134 if (_mesa_symbol_table_find_symbol(this->symbols
, var
->name
) == NULL
) {
137 static unsigned i
= 1;
138 name
= ralloc_asprintf(this->mem_ctx
, "%s@%u", var
->name
, ++i
);
140 _mesa_hash_table_insert(this->printable_names
, var
, (void *) name
);
141 _mesa_symbol_table_add_symbol(this->symbols
, name
, var
);
146 print_type(FILE *f
, const glsl_type
*t
)
149 fprintf(f
, "(array ");
150 print_type(f
, t
->fields
.array
);
151 fprintf(f
, " %u)", t
->length
);
152 } else if ((t
->base_type
== GLSL_TYPE_STRUCT
)
153 && !is_gl_identifier(t
->name
)) {
154 fprintf(f
, "%s@%p", t
->name
, (void *) t
);
156 fprintf(f
, "%s", t
->name
);
160 void ir_print_visitor::visit(ir_rvalue
*)
165 void ir_print_visitor::visit(ir_variable
*ir
)
167 fprintf(f
, "(declare ");
169 char binding
[32] = {0};
170 if (ir
->data
.binding
)
171 snprintf(binding
, sizeof(binding
), "binding=%i ", ir
->data
.binding
);
174 if (ir
->data
.location
!= -1)
175 snprintf(loc
, sizeof(loc
), "location=%i ", ir
->data
.location
);
177 char component
[32] = {0};
178 if (ir
->data
.explicit_component
)
179 snprintf(component
, sizeof(component
), "component=%i ", ir
->data
.location_frac
);
181 char stream
[32] = {0};
182 if (ir
->data
.stream
& (1u << 31)) {
183 if (ir
->data
.stream
& ~(1u << 31)) {
184 snprintf(stream
, sizeof(stream
), "stream(%u,%u,%u,%u) ",
185 ir
->data
.stream
& 3, (ir
->data
.stream
>> 2) & 3,
186 (ir
->data
.stream
>> 4) & 3, (ir
->data
.stream
>> 6) & 3);
188 } else if (ir
->data
.stream
) {
189 snprintf(stream
, sizeof(stream
), "stream%u ", ir
->data
.stream
);
192 const char *const cent
= (ir
->data
.centroid
) ? "centroid " : "";
193 const char *const samp
= (ir
->data
.sample
) ? "sample " : "";
194 const char *const patc
= (ir
->data
.patch
) ? "patch " : "";
195 const char *const inv
= (ir
->data
.invariant
) ? "invariant " : "";
196 const char *const prec
= (ir
->data
.precise
) ? "precise " : "";
197 const char *const mode
[] = { "", "uniform ", "shader_storage ",
198 "shader_shared ", "shader_in ", "shader_out ",
199 "in ", "out ", "inout ",
200 "const_in ", "sys ", "temporary " };
201 STATIC_ASSERT(ARRAY_SIZE(mode
) == ir_var_mode_count
);
202 const char *const interp
[] = { "", "smooth", "flat", "noperspective" };
203 STATIC_ASSERT(ARRAY_SIZE(interp
) == INTERP_MODE_COUNT
);
205 fprintf(f
, "(%s%s%s%s%s%s%s%s%s%s%s) ",
206 binding
, loc
, component
, cent
, samp
, patc
, inv
, prec
, mode
[ir
->data
.mode
],
208 interp
[ir
->data
.interpolation
]);
210 print_type(f
, ir
->type
);
211 fprintf(f
, " %s)", unique_name(ir
));
215 void ir_print_visitor::visit(ir_function_signature
*ir
)
217 _mesa_symbol_table_push_scope(symbols
);
218 fprintf(f
, "(signature ");
221 print_type(f
, ir
->return_type
);
225 fprintf(f
, "(parameters\n");
228 foreach_in_list(ir_variable
, inst
, &ir
->parameters
) {
243 foreach_in_list(ir_instruction
, inst
, &ir
->body
) {
252 _mesa_symbol_table_pop_scope(symbols
);
256 void ir_print_visitor::visit(ir_function
*ir
)
258 fprintf(f
, "(%s function %s\n", ir
->is_subroutine
? "subroutine" : "", ir
->name
);
260 foreach_in_list(ir_function_signature
, sig
, &ir
->signatures
) {
271 void ir_print_visitor::visit(ir_expression
*ir
)
273 fprintf(f
, "(expression ");
275 print_type(f
, ir
->type
);
277 fprintf(f
, " %s ", ir_expression_operation_strings
[ir
->operation
]);
279 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++) {
280 ir
->operands
[i
]->accept(this);
287 void ir_print_visitor::visit(ir_texture
*ir
)
289 fprintf(f
, "(%s ", ir
->opcode_string());
291 if (ir
->op
== ir_samples_identical
) {
292 ir
->sampler
->accept(this);
294 ir
->coordinate
->accept(this);
299 print_type(f
, ir
->type
);
302 ir
->sampler
->accept(this);
305 if (ir
->op
!= ir_txs
&& ir
->op
!= ir_query_levels
&&
306 ir
->op
!= ir_texture_samples
) {
307 ir
->coordinate
->accept(this);
311 if (ir
->offset
!= NULL
) {
312 ir
->offset
->accept(this);
320 if (ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
&&
321 ir
->op
!= ir_txs
&& ir
->op
!= ir_tg4
&&
322 ir
->op
!= ir_query_levels
&& ir
->op
!= ir_texture_samples
) {
324 ir
->projector
->accept(this);
328 if (ir
->shadow_comparator
) {
330 ir
->shadow_comparator
->accept(this);
341 case ir_query_levels
:
342 case ir_texture_samples
:
345 ir
->lod_info
.bias
->accept(this);
350 ir
->lod_info
.lod
->accept(this);
353 ir
->lod_info
.sample_index
->accept(this);
357 ir
->lod_info
.grad
.dPdx
->accept(this);
359 ir
->lod_info
.grad
.dPdy
->accept(this);
363 ir
->lod_info
.component
->accept(this);
365 case ir_samples_identical
:
366 unreachable("ir_samples_identical was already handled");
372 void ir_print_visitor::visit(ir_swizzle
*ir
)
374 const unsigned swiz
[4] = {
381 fprintf(f
, "(swiz ");
382 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
383 fprintf(f
, "%c", "xyzw"[swiz
[i
]]);
386 ir
->val
->accept(this);
391 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
393 ir_variable
*var
= ir
->variable_referenced();
394 fprintf(f
, "(var_ref %s) ", unique_name(var
));
398 void ir_print_visitor::visit(ir_dereference_array
*ir
)
400 fprintf(f
, "(array_ref ");
401 ir
->array
->accept(this);
402 ir
->array_index
->accept(this);
407 void ir_print_visitor::visit(ir_dereference_record
*ir
)
409 fprintf(f
, "(record_ref ");
410 ir
->record
->accept(this);
411 fprintf(f
, " %s) ", ir
->field
);
415 void ir_print_visitor::visit(ir_assignment
*ir
)
417 fprintf(f
, "(assign ");
420 ir
->condition
->accept(this);
425 for (unsigned i
= 0; i
< 4; i
++) {
426 if ((ir
->write_mask
& (1 << i
)) != 0) {
433 fprintf(f
, " (%s) ", mask
);
435 ir
->lhs
->accept(this);
439 ir
->rhs
->accept(this);
444 void ir_print_visitor::visit(ir_constant
*ir
)
446 fprintf(f
, "(constant ");
447 print_type(f
, ir
->type
);
450 if (ir
->type
->is_array()) {
451 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
452 ir
->get_array_element(i
)->accept(this);
453 } else if (ir
->type
->is_record()) {
454 ir_constant
*value
= (ir_constant
*) ir
->components
.get_head();
455 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
456 fprintf(f
, "(%s ", ir
->type
->fields
.structure
[i
].name
);
460 value
= (ir_constant
*) value
->next
;
463 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
466 switch (ir
->type
->base_type
) {
467 case GLSL_TYPE_UINT
: fprintf(f
, "%u", ir
->value
.u
[i
]); break;
468 case GLSL_TYPE_INT
: fprintf(f
, "%d", ir
->value
.i
[i
]); break;
469 case GLSL_TYPE_FLOAT
:
470 if (ir
->value
.f
[i
] == 0.0f
)
471 /* 0.0 == -0.0, so print with %f to get the proper sign. */
472 fprintf(f
, "%f", ir
->value
.f
[i
]);
473 else if (fabs(ir
->value
.f
[i
]) < 0.000001f
)
474 fprintf(f
, "%a", ir
->value
.f
[i
]);
475 else if (fabs(ir
->value
.f
[i
]) > 1000000.0f
)
476 fprintf(f
, "%e", ir
->value
.f
[i
]);
478 fprintf(f
, "%f", ir
->value
.f
[i
]);
480 case GLSL_TYPE_UINT64
:fprintf(f
, "%" PRIu64
, ir
->value
.u64
[i
]); break;
481 case GLSL_TYPE_INT64
: fprintf(f
, "%" PRIi64
, ir
->value
.i64
[i
]); break;
482 case GLSL_TYPE_BOOL
: fprintf(f
, "%d", ir
->value
.b
[i
]); break;
483 case GLSL_TYPE_DOUBLE
:
484 if (ir
->value
.d
[i
] == 0.0)
485 /* 0.0 == -0.0, so print with %f to get the proper sign. */
486 fprintf(f
, "%.1f", ir
->value
.d
[i
]);
487 else if (fabs(ir
->value
.d
[i
]) < 0.000001)
488 fprintf(f
, "%a", ir
->value
.d
[i
]);
489 else if (fabs(ir
->value
.d
[i
]) > 1000000.0)
490 fprintf(f
, "%e", ir
->value
.d
[i
]);
492 fprintf(f
, "%f", ir
->value
.d
[i
]);
495 unreachable("Invalid constant type");
504 ir_print_visitor::visit(ir_call
*ir
)
506 fprintf(f
, "(call %s ", ir
->callee_name());
507 if (ir
->return_deref
)
508 ir
->return_deref
->accept(this);
510 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
518 ir_print_visitor::visit(ir_return
*ir
)
520 fprintf(f
, "(return");
522 ir_rvalue
*const value
= ir
->get_value();
533 ir_print_visitor::visit(ir_discard
*ir
)
535 fprintf(f
, "(discard ");
537 if (ir
->condition
!= NULL
) {
539 ir
->condition
->accept(this);
547 ir_print_visitor::visit(ir_if
*ir
)
550 ir
->condition
->accept(this);
555 foreach_in_list(ir_instruction
, inst
, &ir
->then_instructions
) {
566 if (!ir
->else_instructions
.is_empty()) {
570 foreach_in_list(ir_instruction
, inst
, &ir
->else_instructions
) {
585 ir_print_visitor::visit(ir_loop
*ir
)
587 fprintf(f
, "(loop (\n");
590 foreach_in_list(ir_instruction
, inst
, &ir
->body_instructions
) {
602 ir_print_visitor::visit(ir_loop_jump
*ir
)
604 fprintf(f
, "%s", ir
->is_break() ? "break" : "continue");
608 ir_print_visitor::visit(ir_emit_vertex
*ir
)
610 fprintf(f
, "(emit-vertex ");
611 ir
->stream
->accept(this);
616 ir_print_visitor::visit(ir_end_primitive
*ir
)
618 fprintf(f
, "(end-primitive ");
619 ir
->stream
->accept(this);
624 ir_print_visitor::visit(ir_barrier
*)
626 fprintf(f
, "(barrier)\n");