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(FILE *f
, const glsl_type
*t
);
33 ir_instruction::print(void) const
39 ir_instruction::fprint(FILE *f
) const
41 ir_instruction
*deconsted
= const_cast<ir_instruction
*>(this);
43 ir_print_visitor
v(f
);
44 deconsted
->accept(&v
);
49 _mesa_print_ir(FILE *f
, exec_list
*instructions
,
50 struct _mesa_glsl_parse_state
*state
)
53 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
54 const glsl_type
*const s
= state
->user_structures
[i
];
56 fprintf(f
, "(structure (%s) (%s@%p) (%u) (\n",
57 s
->name
, s
->name
, (void *) s
, s
->length
);
59 for (unsigned j
= 0; j
< s
->length
; j
++) {
61 print_type(f
, s
->fields
.structure
[j
].type
);
62 fprintf(f
, ")(%s))\n", s
->fields
.structure
[j
].name
);
70 foreach_list(n
, instructions
) {
71 ir_instruction
*ir
= (ir_instruction
*) n
;
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 hash_table_ctor(32, hash_table_pointer_hash
, hash_table_pointer_compare
);
94 symbols
= _mesa_symbol_table_ctor();
95 mem_ctx
= ralloc_context(NULL
);
98 ir_print_visitor::~ir_print_visitor()
100 hash_table_dtor(printable_names
);
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 const char *name
= (const char *) hash_table_find(this->printable_names
, var
);
129 /* If there's no conflict, just use the original name */
130 if (_mesa_symbol_table_find_symbol(this->symbols
, -1, var
->name
) == NULL
) {
133 static unsigned i
= 1;
134 name
= ralloc_asprintf(this->mem_ctx
, "%s@%u", var
->name
, ++i
);
136 hash_table_insert(this->printable_names
, (void *) name
, var
);
137 _mesa_symbol_table_add_symbol(this->symbols
, -1, name
, var
);
142 print_type(FILE *f
, const glsl_type
*t
)
144 if (t
->base_type
== GLSL_TYPE_ARRAY
) {
145 fprintf(f
, "(array ");
146 print_type(f
, t
->fields
.array
);
147 fprintf(f
, " %u)", t
->length
);
148 } else if ((t
->base_type
== GLSL_TYPE_STRUCT
)
149 && !is_gl_identifier(t
->name
)) {
150 fprintf(f
, "%s@%p", t
->name
, (void *) t
);
152 fprintf(f
, "%s", t
->name
);
156 void ir_print_visitor::visit(ir_rvalue
*)
161 void ir_print_visitor::visit(ir_variable
*ir
)
163 fprintf(f
, "(declare ");
165 const char *const cent
= (ir
->data
.centroid
) ? "centroid " : "";
166 const char *const samp
= (ir
->data
.sample
) ? "sample " : "";
167 const char *const inv
= (ir
->data
.invariant
) ? "invariant " : "";
168 const char *const mode
[] = { "", "uniform ", "shader_in ", "shader_out ",
169 "in ", "out ", "inout ",
170 "const_in ", "sys ", "temporary " };
171 STATIC_ASSERT(ARRAY_SIZE(mode
) == ir_var_mode_count
);
172 const char *const interp
[] = { "", "smooth", "flat", "noperspective" };
173 STATIC_ASSERT(ARRAY_SIZE(interp
) == INTERP_QUALIFIER_COUNT
);
175 fprintf(f
, "(%s%s%s%s%s) ",
176 cent
, samp
, inv
, mode
[ir
->data
.mode
], interp
[ir
->data
.interpolation
]);
178 print_type(f
, ir
->type
);
179 fprintf(f
, " %s)", unique_name(ir
));
183 void ir_print_visitor::visit(ir_function_signature
*ir
)
185 _mesa_symbol_table_push_scope(symbols
);
186 fprintf(f
, "(signature ");
189 print_type(f
, ir
->return_type
);
193 fprintf(f
, "(parameters\n");
196 foreach_list(n
, &ir
->parameters
) {
197 ir_variable
*const inst
= (ir_variable
*) n
;
213 foreach_list(n
, &ir
->body
) {
214 ir_instruction
*const inst
= (ir_instruction
*) n
;
224 _mesa_symbol_table_pop_scope(symbols
);
228 void ir_print_visitor::visit(ir_function
*ir
)
230 fprintf(f
, "(function %s\n", ir
->name
);
232 foreach_list(n
, &ir
->signatures
) {
233 ir_function_signature
*const sig
= (ir_function_signature
*) n
;
244 void ir_print_visitor::visit(ir_expression
*ir
)
246 fprintf(f
, "(expression ");
248 print_type(f
, ir
->type
);
250 fprintf(f
, " %s ", ir
->operator_string());
252 for (unsigned i
= 0; i
< ir
->get_num_operands(); i
++) {
253 ir
->operands
[i
]->accept(this);
260 void ir_print_visitor::visit(ir_texture
*ir
)
262 fprintf(f
, "(%s ", ir
->opcode_string());
264 print_type(f
, ir
->type
);
267 ir
->sampler
->accept(this);
270 if (ir
->op
!= ir_txs
&& ir
->op
!= ir_query_levels
) {
271 ir
->coordinate
->accept(this);
275 if (ir
->offset
!= NULL
) {
276 ir
->offset
->accept(this);
284 if (ir
->op
!= ir_txf
&& ir
->op
!= ir_txf_ms
&&
285 ir
->op
!= ir_txs
&& ir
->op
!= ir_tg4
&&
286 ir
->op
!= ir_query_levels
) {
288 ir
->projector
->accept(this);
292 if (ir
->shadow_comparitor
) {
294 ir
->shadow_comparitor
->accept(this);
305 case ir_query_levels
:
308 ir
->lod_info
.bias
->accept(this);
313 ir
->lod_info
.lod
->accept(this);
316 ir
->lod_info
.sample_index
->accept(this);
320 ir
->lod_info
.grad
.dPdx
->accept(this);
322 ir
->lod_info
.grad
.dPdy
->accept(this);
326 ir
->lod_info
.component
->accept(this);
333 void ir_print_visitor::visit(ir_swizzle
*ir
)
335 const unsigned swiz
[4] = {
342 fprintf(f
, "(swiz ");
343 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
344 fprintf(f
, "%c", "xyzw"[swiz
[i
]]);
347 ir
->val
->accept(this);
352 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
354 ir_variable
*var
= ir
->variable_referenced();
355 fprintf(f
, "(var_ref %s) ", unique_name(var
));
359 void ir_print_visitor::visit(ir_dereference_array
*ir
)
361 fprintf(f
, "(array_ref ");
362 ir
->array
->accept(this);
363 ir
->array_index
->accept(this);
368 void ir_print_visitor::visit(ir_dereference_record
*ir
)
370 fprintf(f
, "(record_ref ");
371 ir
->record
->accept(this);
372 fprintf(f
, " %s) ", ir
->field
);
376 void ir_print_visitor::visit(ir_assignment
*ir
)
378 fprintf(f
, "(assign ");
381 ir
->condition
->accept(this);
386 for (unsigned i
= 0; i
< 4; i
++) {
387 if ((ir
->write_mask
& (1 << i
)) != 0) {
394 fprintf(f
, " (%s) ", mask
);
396 ir
->lhs
->accept(this);
400 ir
->rhs
->accept(this);
405 void ir_print_visitor::visit(ir_constant
*ir
)
407 fprintf(f
, "(constant ");
408 print_type(f
, ir
->type
);
411 if (ir
->type
->is_array()) {
412 for (unsigned i
= 0; i
< ir
->type
->length
; i
++)
413 ir
->get_array_element(i
)->accept(this);
414 } else if (ir
->type
->is_record()) {
415 ir_constant
*value
= (ir_constant
*) ir
->components
.get_head();
416 for (unsigned i
= 0; i
< ir
->type
->length
; i
++) {
417 fprintf(f
, "(%s ", ir
->type
->fields
.structure
[i
].name
);
421 value
= (ir_constant
*) value
->next
;
424 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
427 switch (ir
->type
->base_type
) {
428 case GLSL_TYPE_UINT
: fprintf(f
, "%u", ir
->value
.u
[i
]); break;
429 case GLSL_TYPE_INT
: fprintf(f
, "%d", ir
->value
.i
[i
]); break;
430 case GLSL_TYPE_FLOAT
:
431 if (ir
->value
.f
[i
] == 0.0f
)
432 /* 0.0 == -0.0, so print with %f to get the proper sign. */
433 fprintf(f
, "%f", ir
->value
.f
[i
]);
434 else if (fabs(ir
->value
.f
[i
]) < 0.000001f
)
435 fprintf(f
, "%a", ir
->value
.f
[i
]);
436 else if (fabs(ir
->value
.f
[i
]) > 1000000.0f
)
437 fprintf(f
, "%e", ir
->value
.f
[i
]);
439 fprintf(f
, "%f", ir
->value
.f
[i
]);
441 case GLSL_TYPE_BOOL
: fprintf(f
, "%d", ir
->value
.b
[i
]); break;
451 ir_print_visitor::visit(ir_call
*ir
)
453 fprintf(f
, "(call %s ", ir
->callee_name());
454 if (ir
->return_deref
)
455 ir
->return_deref
->accept(this);
457 foreach_list(n
, &ir
->actual_parameters
) {
458 ir_rvalue
*const param
= (ir_rvalue
*) n
;
467 ir_print_visitor::visit(ir_return
*ir
)
469 fprintf(f
, "(return");
471 ir_rvalue
*const value
= ir
->get_value();
482 ir_print_visitor::visit(ir_discard
*ir
)
484 fprintf(f
, "(discard ");
486 if (ir
->condition
!= NULL
) {
488 ir
->condition
->accept(this);
496 ir_print_visitor::visit(ir_if
*ir
)
499 ir
->condition
->accept(this);
504 foreach_list(n
, &ir
->then_instructions
) {
505 ir_instruction
*const inst
= (ir_instruction
*) n
;
517 if (!ir
->else_instructions
.is_empty()) {
521 foreach_list(n
, &ir
->else_instructions
) {
522 ir_instruction
*const inst
= (ir_instruction
*) n
;
538 ir_print_visitor::visit(ir_loop
*ir
)
540 fprintf(f
, "(loop (\n");
543 foreach_list(n
, &ir
->body_instructions
) {
544 ir_instruction
*const inst
= (ir_instruction
*) n
;
557 ir_print_visitor::visit(ir_loop_jump
*ir
)
559 fprintf(f
, "%s", ir
->is_break() ? "break" : "continue");
563 ir_print_visitor::visit(ir_emit_vertex
*ir
)
565 fprintf(f
, "(emit-vertex ");
566 ir
->stream
->accept(this);
571 ir_print_visitor::visit(ir_end_primitive
*ir
)
573 fprintf(f
, "(end-primitive ");
574 ir
->stream
->accept(this);