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