1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
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"
29
30 static void print_type(FILE *f, const glsl_type *t);
31
32 void
print(void) const33 ir_instruction::print(void) const
34 {
35 this->fprint(stdout);
36 }
37
38 void
fprint(FILE * f) const39 ir_instruction::fprint(FILE *f) const
40 {
41 ir_instruction *deconsted = const_cast<ir_instruction *>(this);
42
43 ir_print_visitor v(f);
44 deconsted->accept(&v);
45 }
46
47 extern "C" {
48 void
_mesa_print_ir(FILE * f,exec_list * instructions,struct _mesa_glsl_parse_state * state)49 _mesa_print_ir(FILE *f, exec_list *instructions,
50 struct _mesa_glsl_parse_state *state)
51 {
52 if (state) {
53 for (unsigned i = 0; i < state->num_user_structures; i++) {
54 const glsl_type *const s = state->user_structures[i];
55
56 fprintf(f, "(structure (%s) (%s@%p) (%u) (\n",
57 s->name, s->name, (void *) s, s->length);
58
59 for (unsigned j = 0; j < s->length; j++) {
60 fprintf(f, "\t((");
61 print_type(f, s->fields.structure[j].type);
62 fprintf(f, ")(%s))\n", s->fields.structure[j].name);
63 }
64
65 fprintf(f, ")\n");
66 }
67 }
68
69 fprintf(f, "(\n");
70 foreach_in_list(ir_instruction, ir, instructions) {
71 ir->fprint(f);
72 if (ir->ir_type != ir_type_function)
73 fprintf(f, "\n");
74 }
75 fprintf(f, "\n)");
76 }
77
78 void
fprint_ir(FILE * f,const void * instruction)79 fprint_ir(FILE *f, const void *instruction)
80 {
81 const ir_instruction *ir = (const ir_instruction *)instruction;
82 ir->fprint(f);
83 }
84
85 } /* extern "C" */
86
ir_print_visitor(FILE * f)87 ir_print_visitor::ir_print_visitor(FILE *f)
88 : f(f)
89 {
90 indentation = 0;
91 printable_names =
92 hash_table_ctor(32, hash_table_pointer_hash, hash_table_pointer_compare);
93 symbols = _mesa_symbol_table_ctor();
94 mem_ctx = ralloc_context(NULL);
95 }
96
~ir_print_visitor()97 ir_print_visitor::~ir_print_visitor()
98 {
99 hash_table_dtor(printable_names);
100 _mesa_symbol_table_dtor(symbols);
101 ralloc_free(mem_ctx);
102 }
103
indent(void)104 void ir_print_visitor::indent(void)
105 {
106 for (int i = 0; i < indentation; i++)
107 fprintf(f, " ");
108 }
109
110 const char *
unique_name(ir_variable * var)111 ir_print_visitor::unique_name(ir_variable *var)
112 {
113 /* var->name can be NULL in function prototypes when a type is given for a
114 * parameter but no name is given. In that case, just return an empty
115 * string. Don't worry about tracking the generated name in the printable
116 * names hash because this is the only scope where it can ever appear.
117 */
118 if (var->name == NULL) {
119 static unsigned arg = 1;
120 return ralloc_asprintf(this->mem_ctx, "parameter@%u", arg++);
121 }
122
123 /* Do we already have a name for this variable? */
124 const char *name = (const char *) hash_table_find(this->printable_names, var);
125 if (name != NULL)
126 return name;
127
128 /* If there's no conflict, just use the original name */
129 if (_mesa_symbol_table_find_symbol(this->symbols, -1, var->name) == NULL) {
130 name = var->name;
131 } else {
132 static unsigned i = 1;
133 name = ralloc_asprintf(this->mem_ctx, "%s@%u", var->name, ++i);
134 }
135 hash_table_insert(this->printable_names, (void *) name, var);
136 _mesa_symbol_table_add_symbol(this->symbols, -1, name, var);
137 return name;
138 }
139
140 static void
print_type(FILE * f,const glsl_type * t)141 print_type(FILE *f, const glsl_type *t)
142 {
143 if (t->base_type == GLSL_TYPE_ARRAY) {
144 fprintf(f, "(array ");
145 print_type(f, t->fields.array);
146 fprintf(f, " %u)", t->length);
147 } else if ((t->base_type == GLSL_TYPE_STRUCT)
148 && !is_gl_identifier(t->name)) {
149 fprintf(f, "%s@%p", t->name, (void *) t);
150 } else {
151 fprintf(f, "%s", t->name);
152 }
153 }
154
visit(ir_rvalue *)155 void ir_print_visitor::visit(ir_rvalue *)
156 {
157 fprintf(f, "error");
158 }
159
visit(ir_variable * ir)160 void ir_print_visitor::visit(ir_variable *ir)
161 {
162 fprintf(f, "(declare ");
163
164 const char *const cent = (ir->data.centroid) ? "centroid " : "";
165 const char *const samp = (ir->data.sample) ? "sample " : "";
166 const char *const inv = (ir->data.invariant) ? "invariant " : "";
167 const char *const mode[] = { "", "uniform ", "shader_in ", "shader_out ", "shader_inout ",
168 "in ", "out ", "inout ",
169 "const_in ", "sys ", "temporary " };
170 STATIC_ASSERT(ARRAY_SIZE(mode) == ir_var_mode_count);
171 const char *const stream [] = {"", "stream1 ", "stream2 ", "stream3 "};
172 const char *const interp[] = { "", "smooth", "flat", "noperspective" };
173 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_QUALIFIER_COUNT);
174
175 fprintf(f, "(%s%s%s%s%s%s) ",
176 cent, samp, inv, mode[ir->data.mode],
177 stream[ir->data.stream],
178 interp[ir->data.interpolation]);
179
180 print_type(f, ir->type);
181 fprintf(f, " %s)", unique_name(ir));
182 }
183
184
visit(ir_function_signature * ir)185 void ir_print_visitor::visit(ir_function_signature *ir)
186 {
187 _mesa_symbol_table_push_scope(symbols);
188 fprintf(f, "(signature ");
189 indentation++;
190
191 print_type(f, ir->return_type);
192 fprintf(f, "\n");
193 indent();
194
195 fprintf(f, "(parameters\n");
196 indentation++;
197
198 foreach_in_list(ir_variable, inst, &ir->parameters) {
199 indent();
200 inst->accept(this);
201 fprintf(f, "\n");
202 }
203 indentation--;
204
205 indent();
206 fprintf(f, ")\n");
207
208 indent();
209
210 fprintf(f, "(\n");
211 indentation++;
212
213 foreach_in_list(ir_instruction, inst, &ir->body) {
214 indent();
215 inst->accept(this);
216 fprintf(f, "\n");
217 }
218 indentation--;
219 indent();
220 fprintf(f, "))\n");
221 indentation--;
222 _mesa_symbol_table_pop_scope(symbols);
223 }
224
225
visit(ir_function * ir)226 void ir_print_visitor::visit(ir_function *ir)
227 {
228 fprintf(f, "(function %s\n", ir->name);
229 indentation++;
230 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
231 indent();
232 sig->accept(this);
233 fprintf(f, "\n");
234 }
235 indentation--;
236 indent();
237 fprintf(f, ")\n\n");
238 }
239
240
visit(ir_expression * ir)241 void ir_print_visitor::visit(ir_expression *ir)
242 {
243 fprintf(f, "(expression ");
244
245 print_type(f, ir->type);
246
247 fprintf(f, " %s ", ir->operator_string());
248
249 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
250 ir->operands[i]->accept(this);
251 }
252
253 fprintf(f, ") ");
254 }
255
256
visit(ir_texture * ir)257 void ir_print_visitor::visit(ir_texture *ir)
258 {
259 fprintf(f, "(%s ", ir->opcode_string());
260
261 print_type(f, ir->type);
262 fprintf(f, " ");
263
264 ir->sampler->accept(this);
265 fprintf(f, " ");
266
267 if (ir->op != ir_txs && ir->op != ir_query_levels) {
268 ir->coordinate->accept(this);
269
270 fprintf(f, " ");
271
272 if (ir->offset != NULL) {
273 ir->offset->accept(this);
274 } else {
275 fprintf(f, "0");
276 }
277
278 fprintf(f, " ");
279 }
280
281 fprintf(f, " ");
282 switch (ir->op)
283 {
284 case ir_tex:
285 case ir_lod:
286 case ir_query_levels:
287 break;
288 case ir_txb:
289 ir->lod_info.bias->accept(this);
290 break;
291 case ir_txl:
292 case ir_txf:
293 case ir_txs:
294 ir->lod_info.lod->accept(this);
295 break;
296 case ir_txf_ms:
297 ir->lod_info.sample_index->accept(this);
298 break;
299 case ir_txd:
300 fprintf(f, "(");
301 ir->lod_info.grad.dPdx->accept(this);
302 fprintf(f, " ");
303 ir->lod_info.grad.dPdy->accept(this);
304 fprintf(f, ")");
305 break;
306 case ir_tg4:
307 ir->lod_info.component->accept(this);
308 break;
309 };
310 fprintf(f, ")");
311 }
312
313
visit(ir_swizzle * ir)314 void ir_print_visitor::visit(ir_swizzle *ir)
315 {
316 const unsigned swiz[4] = {
317 ir->mask.x,
318 ir->mask.y,
319 ir->mask.z,
320 ir->mask.w,
321 };
322
323 fprintf(f, "(swiz ");
324 for (unsigned i = 0; i < ir->mask.num_components; i++) {
325 fprintf(f, "%c", "xyzw"[swiz[i]]);
326 }
327 fprintf(f, " ");
328 ir->val->accept(this);
329 fprintf(f, ")");
330 }
331
332
visit(ir_dereference_variable * ir)333 void ir_print_visitor::visit(ir_dereference_variable *ir)
334 {
335 ir_variable *var = ir->variable_referenced();
336 fprintf(f, "(var_ref %s) ", unique_name(var));
337 }
338
339
visit(ir_dereference_array * ir)340 void ir_print_visitor::visit(ir_dereference_array *ir)
341 {
342 fprintf(f, "(array_ref ");
343 ir->array->accept(this);
344 ir->array_index->accept(this);
345 fprintf(f, ") ");
346 }
347
348
visit(ir_dereference_record * ir)349 void ir_print_visitor::visit(ir_dereference_record *ir)
350 {
351 fprintf(f, "(record_ref ");
352 ir->record->accept(this);
353 fprintf(f, " %s) ", ir->field);
354 }
355
356
visit(ir_assignment * ir)357 void ir_print_visitor::visit(ir_assignment *ir)
358 {
359 fprintf(f, "(assign ");
360
361 if (ir->condition)
362 ir->condition->accept(this);
363
364 char mask[5];
365 unsigned j = 0;
366
367 for (unsigned i = 0; i < 4; i++) {
368 if ((ir->write_mask & (1 << i)) != 0) {
369 mask[j] = "xyzw"[i];
370 j++;
371 }
372 }
373 mask[j] = '\0';
374
375 fprintf(f, " (%s) ", mask);
376
377 ir->lhs->accept(this);
378
379 fprintf(f, " ");
380
381 ir->rhs->accept(this);
382 fprintf(f, ") ");
383 }
384
385
visit(ir_constant * ir)386 void ir_print_visitor::visit(ir_constant *ir)
387 {
388 fprintf(f, "(constant ");
389 print_type(f, ir->type);
390 fprintf(f, " (");
391
392 if (ir->type->is_array()) {
393 for (unsigned i = 0; i < ir->type->length; i++)
394 ir->get_array_element(i)->accept(this);
395 } else if (ir->type->is_record()) {
396 ir_constant *value = (ir_constant *) ir->components.get_head();
397 for (unsigned i = 0; i < ir->type->length; i++) {
398 fprintf(f, "(%s ", ir->type->fields.structure[i].name);
399 value->accept(this);
400 fprintf(f, ")");
401
402 value = (ir_constant *) value->next;
403 }
404 } else {
405 for (unsigned i = 0; i < ir->type->components(); i++) {
406 if (i != 0)
407 fprintf(f, " ");
408 switch (ir->type->base_type) {
409 case GLSL_TYPE_UINT: fprintf(f, "%u", ir->value.u[i]); break;
410 case GLSL_TYPE_INT: fprintf(f, "%d", ir->value.i[i]); break;
411 case GLSL_TYPE_FLOAT:
412 if (ir->value.f[i] == 0.0f)
413 /* 0.0 == -0.0, so print with %f to get the proper sign. */
414 fprintf(f, "%f", ir->value.f[i]);
415 else if (fabs(ir->value.f[i]) < 0.000001f)
416 fprintf(f, "%a", ir->value.f[i]);
417 else if (fabs(ir->value.f[i]) > 1000000.0f)
418 fprintf(f, "%e", ir->value.f[i]);
419 else
420 fprintf(f, "%f", ir->value.f[i]);
421 break;
422 case GLSL_TYPE_BOOL: fprintf(f, "%d", ir->value.b[i]); break;
423 default: assert(0);
424 }
425 }
426 }
427 fprintf(f, ")) ");
428 }
429
430
431 void
visit(ir_call * ir)432 ir_print_visitor::visit(ir_call *ir)
433 {
434 fprintf(f, "(call %s ", ir->callee_name());
435 if (ir->return_deref)
436 ir->return_deref->accept(this);
437 fprintf(f, " (");
438 foreach_in_list(ir_rvalue, param, &ir->actual_parameters) {
439 param->accept(this);
440 }
441 fprintf(f, "))\n");
442 }
443
444
445 void
visit(ir_return * ir)446 ir_print_visitor::visit(ir_return *ir)
447 {
448 fprintf(f, "(return");
449
450 ir_rvalue *const value = ir->get_value();
451 if (value) {
452 fprintf(f, " ");
453 value->accept(this);
454 }
455
456 fprintf(f, ")");
457 }
458
459
460 void
visit(ir_discard * ir)461 ir_print_visitor::visit(ir_discard *ir)
462 {
463 fprintf(f, "(discard ");
464
465 if (ir->condition != NULL) {
466 fprintf(f, " ");
467 ir->condition->accept(this);
468 }
469
470 fprintf(f, ")");
471 }
472
473
474 void
visit(ir_if * ir)475 ir_print_visitor::visit(ir_if *ir)
476 {
477 fprintf(f, "(if ");
478 ir->condition->accept(this);
479
480 fprintf(f, "(\n");
481 indentation++;
482
483 foreach_in_list(ir_instruction, inst, &ir->then_instructions) {
484 indent();
485 inst->accept(this);
486 fprintf(f, "\n");
487 }
488
489 indentation--;
490 indent();
491 fprintf(f, ")\n");
492
493 indent();
494 if (!ir->else_instructions.is_empty()) {
495 fprintf(f, "(\n");
496 indentation++;
497
498 foreach_in_list(ir_instruction, inst, &ir->else_instructions) {
499 indent();
500 inst->accept(this);
501 fprintf(f, "\n");
502 }
503 indentation--;
504 indent();
505 fprintf(f, "))\n");
506 } else {
507 fprintf(f, "())\n");
508 }
509 }
510
511
512 void
visit(ir_loop * ir)513 ir_print_visitor::visit(ir_loop *ir)
514 {
515 fprintf(f, "(loop (\n");
516 indentation++;
517
518 foreach_in_list(ir_instruction, inst, &ir->body_instructions) {
519 indent();
520 inst->accept(this);
521 fprintf(f, "\n");
522 }
523 indentation--;
524 indent();
525 fprintf(f, "))\n");
526 }
527
528
529 void
visit(ir_loop_jump * ir)530 ir_print_visitor::visit(ir_loop_jump *ir)
531 {
532 fprintf(f, "%s", ir->is_break() ? "break" : "continue");
533 }
534
535 void
visit(ir_precision_statement * ir)536 ir_print_visitor::visit(ir_precision_statement *ir)
537 {
538 //printf("%s", ir->precision_statement);
539 }
540
541 void
visit(ir_typedecl_statement * ir)542 ir_print_visitor::visit(ir_typedecl_statement *ir)
543 {
544 }
545
546 void
visit(ir_emit_vertex * ir)547 ir_print_visitor::visit(ir_emit_vertex *ir)
548 {
549 fprintf(f, "(emit-vertex ");
550 ir->stream->accept(this);
551 fprintf(f, ")\n");
552 }
553
554 void
visit(ir_end_primitive * ir)555 ir_print_visitor::visit(ir_end_primitive *ir)
556 {
557 fprintf(f, "(end-primitive ");
558 ir->stream->accept(this);
559 fprintf(f, ")\n");
560
561 }
562