nir/spirv: Make the header file C++ safe
[mesa.git] / src / glsl / ir_print_visitor.cpp
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
33 ir_instruction::print(void) const
34 {
35 this->fprint(stdout);
36 }
37
38 void
39 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
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
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
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
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
104 void ir_print_visitor::indent(void)
105 {
106 for (int i = 0; i < indentation; i++)
107 fprintf(f, " ");
108 }
109
110 const char *
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
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
155 void ir_print_visitor::visit(ir_rvalue *)
156 {
157 fprintf(f, "error");
158 }
159
160 void ir_print_visitor::visit(ir_variable *ir)
161 {
162 fprintf(f, "(declare ");
163
164 char loc[256] = {0};
165 if (ir->data.location != -1)
166 snprintf(loc, sizeof(loc), "location=%i ", ir->data.location);
167
168 const char *const cent = (ir->data.centroid) ? "centroid " : "";
169 const char *const samp = (ir->data.sample) ? "sample " : "";
170 const char *const inv = (ir->data.invariant) ? "invariant " : "";
171 const char *const mode[] = { "", "uniform ", "shader_in ", "shader_out ",
172 "in ", "out ", "inout ",
173 "const_in ", "sys ", "temporary " };
174 STATIC_ASSERT(ARRAY_SIZE(mode) == ir_var_mode_count);
175 const char *const stream [] = {"", "stream1 ", "stream2 ", "stream3 "};
176 const char *const interp[] = { "", "smooth", "flat", "noperspective" };
177 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_QUALIFIER_COUNT);
178
179 fprintf(f, "(%s%s%s%s%s%s%s) ",
180 loc, cent, samp, inv, mode[ir->data.mode],
181 stream[ir->data.stream],
182 interp[ir->data.interpolation]);
183
184 print_type(f, ir->type);
185 fprintf(f, " %s)", unique_name(ir));
186 }
187
188
189 void ir_print_visitor::visit(ir_function_signature *ir)
190 {
191 _mesa_symbol_table_push_scope(symbols);
192 fprintf(f, "(signature ");
193 indentation++;
194
195 print_type(f, ir->return_type);
196 fprintf(f, "\n");
197 indent();
198
199 fprintf(f, "(parameters\n");
200 indentation++;
201
202 foreach_in_list(ir_variable, inst, &ir->parameters) {
203 indent();
204 inst->accept(this);
205 fprintf(f, "\n");
206 }
207 indentation--;
208
209 indent();
210 fprintf(f, ")\n");
211
212 indent();
213
214 fprintf(f, "(\n");
215 indentation++;
216
217 foreach_in_list(ir_instruction, inst, &ir->body) {
218 indent();
219 inst->accept(this);
220 fprintf(f, "\n");
221 }
222 indentation--;
223 indent();
224 fprintf(f, "))\n");
225 indentation--;
226 _mesa_symbol_table_pop_scope(symbols);
227 }
228
229
230 void ir_print_visitor::visit(ir_function *ir)
231 {
232 fprintf(f, "(function %s\n", ir->name);
233 indentation++;
234 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
235 indent();
236 sig->accept(this);
237 fprintf(f, "\n");
238 }
239 indentation--;
240 indent();
241 fprintf(f, ")\n\n");
242 }
243
244
245 void ir_print_visitor::visit(ir_expression *ir)
246 {
247 fprintf(f, "(expression ");
248
249 print_type(f, ir->type);
250
251 fprintf(f, " %s ", ir->operator_string());
252
253 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
254 ir->operands[i]->accept(this);
255 }
256
257 fprintf(f, ") ");
258 }
259
260
261 void ir_print_visitor::visit(ir_texture *ir)
262 {
263 fprintf(f, "(%s ", ir->opcode_string());
264
265 print_type(f, ir->type);
266 fprintf(f, " ");
267
268 ir->sampler->accept(this);
269 fprintf(f, " ");
270
271 if (ir->op != ir_txs && ir->op != ir_query_levels) {
272 ir->coordinate->accept(this);
273
274 fprintf(f, " ");
275
276 if (ir->offset != NULL) {
277 ir->offset->accept(this);
278 } else {
279 fprintf(f, "0");
280 }
281
282 fprintf(f, " ");
283 }
284
285 if (ir->op != ir_txf && ir->op != ir_txf_ms &&
286 ir->op != ir_txs && ir->op != ir_tg4 &&
287 ir->op != ir_query_levels) {
288 if (ir->projector)
289 ir->projector->accept(this);
290 else
291 fprintf(f, "1");
292
293 if (ir->shadow_comparitor) {
294 fprintf(f, " ");
295 ir->shadow_comparitor->accept(this);
296 } else {
297 fprintf(f, " ()");
298 }
299 }
300
301 fprintf(f, " ");
302 switch (ir->op)
303 {
304 case ir_tex:
305 case ir_lod:
306 case ir_query_levels:
307 break;
308 case ir_txb:
309 ir->lod_info.bias->accept(this);
310 break;
311 case ir_txl:
312 case ir_txf:
313 case ir_txs:
314 ir->lod_info.lod->accept(this);
315 break;
316 case ir_txf_ms:
317 ir->lod_info.sample_index->accept(this);
318 break;
319 case ir_txd:
320 fprintf(f, "(");
321 ir->lod_info.grad.dPdx->accept(this);
322 fprintf(f, " ");
323 ir->lod_info.grad.dPdy->accept(this);
324 fprintf(f, ")");
325 break;
326 case ir_tg4:
327 ir->lod_info.component->accept(this);
328 break;
329 };
330 fprintf(f, ")");
331 }
332
333
334 void ir_print_visitor::visit(ir_swizzle *ir)
335 {
336 const unsigned swiz[4] = {
337 ir->mask.x,
338 ir->mask.y,
339 ir->mask.z,
340 ir->mask.w,
341 };
342
343 fprintf(f, "(swiz ");
344 for (unsigned i = 0; i < ir->mask.num_components; i++) {
345 fprintf(f, "%c", "xyzw"[swiz[i]]);
346 }
347 fprintf(f, " ");
348 ir->val->accept(this);
349 fprintf(f, ")");
350 }
351
352
353 void ir_print_visitor::visit(ir_dereference_variable *ir)
354 {
355 ir_variable *var = ir->variable_referenced();
356 fprintf(f, "(var_ref %s) ", unique_name(var));
357 }
358
359
360 void ir_print_visitor::visit(ir_dereference_array *ir)
361 {
362 fprintf(f, "(array_ref ");
363 ir->array->accept(this);
364 ir->array_index->accept(this);
365 fprintf(f, ") ");
366 }
367
368
369 void ir_print_visitor::visit(ir_dereference_record *ir)
370 {
371 fprintf(f, "(record_ref ");
372 ir->record->accept(this);
373 fprintf(f, " %s) ", ir->field);
374 }
375
376
377 void ir_print_visitor::visit(ir_assignment *ir)
378 {
379 fprintf(f, "(assign ");
380
381 if (ir->condition)
382 ir->condition->accept(this);
383
384 char mask[5];
385 unsigned j = 0;
386
387 for (unsigned i = 0; i < 4; i++) {
388 if ((ir->write_mask & (1 << i)) != 0) {
389 mask[j] = "xyzw"[i];
390 j++;
391 }
392 }
393 mask[j] = '\0';
394
395 fprintf(f, " (%s) ", mask);
396
397 ir->lhs->accept(this);
398
399 fprintf(f, " ");
400
401 ir->rhs->accept(this);
402 fprintf(f, ") ");
403 }
404
405
406 void ir_print_visitor::visit(ir_constant *ir)
407 {
408 fprintf(f, "(constant ");
409 print_type(f, ir->type);
410 fprintf(f, " (");
411
412 if (ir->type->is_array()) {
413 for (unsigned i = 0; i < ir->type->length; i++)
414 ir->get_array_element(i)->accept(this);
415 } else if (ir->type->is_record()) {
416 ir_constant *value = (ir_constant *) ir->components.get_head();
417 for (unsigned i = 0; i < ir->type->length; i++) {
418 fprintf(f, "(%s ", ir->type->fields.structure[i].name);
419 value->accept(this);
420 fprintf(f, ")");
421
422 value = (ir_constant *) value->next;
423 }
424 } else {
425 for (unsigned i = 0; i < ir->type->components(); i++) {
426 if (i != 0)
427 fprintf(f, " ");
428 switch (ir->type->base_type) {
429 case GLSL_TYPE_UINT: fprintf(f, "%u", ir->value.u[i]); break;
430 case GLSL_TYPE_INT: fprintf(f, "%d", ir->value.i[i]); break;
431 case GLSL_TYPE_FLOAT:
432 if (ir->value.f[i] == 0.0f)
433 /* 0.0 == -0.0, so print with %f to get the proper sign. */
434 fprintf(f, "%f", ir->value.f[i]);
435 else if (fabs(ir->value.f[i]) < 0.000001f)
436 fprintf(f, "%a", ir->value.f[i]);
437 else if (fabs(ir->value.f[i]) > 1000000.0f)
438 fprintf(f, "%e", ir->value.f[i]);
439 else
440 fprintf(f, "%f", ir->value.f[i]);
441 break;
442 case GLSL_TYPE_BOOL: fprintf(f, "%d", ir->value.b[i]); break;
443 case GLSL_TYPE_DOUBLE:
444 if (ir->value.d[i] == 0.0)
445 /* 0.0 == -0.0, so print with %f to get the proper sign. */
446 fprintf(f, "%.1f", ir->value.d[i]);
447 else if (fabs(ir->value.d[i]) < 0.000001)
448 fprintf(f, "%a", ir->value.d[i]);
449 else if (fabs(ir->value.d[i]) > 1000000.0)
450 fprintf(f, "%e", ir->value.d[i]);
451 else
452 fprintf(f, "%f", ir->value.d[i]);
453 break;
454 default: assert(0);
455 }
456 }
457 }
458 fprintf(f, ")) ");
459 }
460
461
462 void
463 ir_print_visitor::visit(ir_call *ir)
464 {
465 fprintf(f, "(call %s ", ir->callee_name());
466 if (ir->return_deref)
467 ir->return_deref->accept(this);
468 fprintf(f, " (");
469 foreach_in_list(ir_rvalue, param, &ir->actual_parameters) {
470 param->accept(this);
471 }
472 fprintf(f, "))\n");
473 }
474
475
476 void
477 ir_print_visitor::visit(ir_return *ir)
478 {
479 fprintf(f, "(return");
480
481 ir_rvalue *const value = ir->get_value();
482 if (value) {
483 fprintf(f, " ");
484 value->accept(this);
485 }
486
487 fprintf(f, ")");
488 }
489
490
491 void
492 ir_print_visitor::visit(ir_discard *ir)
493 {
494 fprintf(f, "(discard ");
495
496 if (ir->condition != NULL) {
497 fprintf(f, " ");
498 ir->condition->accept(this);
499 }
500
501 fprintf(f, ")");
502 }
503
504
505 void
506 ir_print_visitor::visit(ir_if *ir)
507 {
508 fprintf(f, "(if ");
509 ir->condition->accept(this);
510
511 fprintf(f, "(\n");
512 indentation++;
513
514 foreach_in_list(ir_instruction, inst, &ir->then_instructions) {
515 indent();
516 inst->accept(this);
517 fprintf(f, "\n");
518 }
519
520 indentation--;
521 indent();
522 fprintf(f, ")\n");
523
524 indent();
525 if (!ir->else_instructions.is_empty()) {
526 fprintf(f, "(\n");
527 indentation++;
528
529 foreach_in_list(ir_instruction, inst, &ir->else_instructions) {
530 indent();
531 inst->accept(this);
532 fprintf(f, "\n");
533 }
534 indentation--;
535 indent();
536 fprintf(f, "))\n");
537 } else {
538 fprintf(f, "())\n");
539 }
540 }
541
542
543 void
544 ir_print_visitor::visit(ir_loop *ir)
545 {
546 fprintf(f, "(loop (\n");
547 indentation++;
548
549 foreach_in_list(ir_instruction, inst, &ir->body_instructions) {
550 indent();
551 inst->accept(this);
552 fprintf(f, "\n");
553 }
554 indentation--;
555 indent();
556 fprintf(f, "))\n");
557 }
558
559
560 void
561 ir_print_visitor::visit(ir_loop_jump *ir)
562 {
563 fprintf(f, "%s", ir->is_break() ? "break" : "continue");
564 }
565
566 void
567 ir_print_visitor::visit(ir_emit_vertex *ir)
568 {
569 fprintf(f, "(emit-vertex ");
570 ir->stream->accept(this);
571 fprintf(f, ")\n");
572 }
573
574 void
575 ir_print_visitor::visit(ir_end_primitive *ir)
576 {
577 fprintf(f, "(end-primitive ");
578 ir->stream->accept(this);
579 fprintf(f, ")\n");
580 }
581
582 void
583 ir_print_visitor::visit(ir_barrier *ir)
584 {
585 fprintf(f, "(barrier)\n");
586 }