aboutsummaryrefslogtreecommitdiffstats
path: root/src/compiler/glsl/ir_builder_print_visitor.cpp
blob: 3e30c5d7af30de0c34b60f05c64b061299aa53e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
 * Copyright © 2016 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <inttypes.h> /* for PRIx64 macro */
#include "ir.h"
#include "ir_hierarchical_visitor.h"
#include "ir_builder_print_visitor.h"
#include "compiler/glsl_types.h"
#include "glsl_parser_extras.h"
#include "main/macros.h"
#include "util/hash_table.h"

class ir_builder_print_visitor : public ir_hierarchical_visitor {
public:
   ir_builder_print_visitor(FILE *f);
   virtual ~ir_builder_print_visitor();

   void indent(void);

   virtual ir_visitor_status visit(class ir_variable *);
   virtual ir_visitor_status visit(class ir_dereference_variable *);
   virtual ir_visitor_status visit(class ir_constant *);
   virtual ir_visitor_status visit(class ir_loop_jump *);

   virtual ir_visitor_status visit_enter(class ir_if *);

   virtual ir_visitor_status visit_enter(class ir_loop *);
   virtual ir_visitor_status visit_leave(class ir_loop *);

   virtual ir_visitor_status visit_enter(class ir_function_signature *);
   virtual ir_visitor_status visit_leave(class ir_function_signature *);

   virtual ir_visitor_status visit_enter(class ir_expression *);

   virtual ir_visitor_status visit_enter(class ir_assignment *);
   virtual ir_visitor_status visit_leave(class ir_assignment *);

   virtual ir_visitor_status visit_leave(class ir_call *);
   virtual ir_visitor_status visit_leave(class ir_swizzle *);
   virtual ir_visitor_status visit_leave(class ir_return *);

private:
   void print_with_indent(const char *fmt, ...);
   void print_without_indent(const char *fmt, ...);

   void print_without_declaration(const ir_rvalue *ir);
   void print_without_declaration(const ir_constant *ir);
   void print_without_declaration(const ir_dereference_variable *ir);
   void print_without_declaration(const ir_swizzle *ir);
   void print_without_declaration(const ir_expression *ir);

   unsigned next_ir_index;

   /**
    * Mapping from ir_instruction * -> index used in the generated C code
    * variable name.
    */
   hash_table *index_map;

   FILE *f;

   int indentation;
};

/* An operand is "simple" if it can be compactly printed on one line.
 */
static bool
is_simple_operand(const ir_rvalue *ir, unsigned depth = 1)
{
   if (depth == 0)
      return false;

   switch (ir->ir_type) {
   case ir_type_dereference_variable:
      return true;

   case ir_type_constant: {
      if (ir->type == glsl_type::uint_type ||
          ir->type == glsl_type::int_type ||
          ir->type == glsl_type::float_type ||
          ir->type == glsl_type::bool_type)
         return true;

      const ir_constant *const c = (ir_constant *) ir;
      ir_constant_data all_zero;
      memset(&all_zero, 0, sizeof(all_zero));

      return memcmp(&c->value, &all_zero, sizeof(all_zero)) == 0;
   }

   case ir_type_swizzle: {
      const ir_swizzle *swiz = (ir_swizzle *) ir;
      return swiz->mask.num_components == 1 &&
             is_simple_operand(swiz->val, depth);
   }

   case ir_type_expression: {
      const ir_expression *expr = (ir_expression *) ir;

      for (unsigned i = 0; i < expr->num_operands; i++) {
         if (!is_simple_operand(expr->operands[i], depth - 1))
            return false;
      }

      return true;
   }

   default:
      return false;
   }
}

void
_mesa_print_builder_for_ir(FILE *f, exec_list *instructions)
{
   ir_builder_print_visitor v(f);
   v.run(instructions);
}

ir_builder_print_visitor::ir_builder_print_visitor(FILE *f)
   : next_ir_index(1), f(f), indentation(0)
{
   index_map =
      _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
}

ir_builder_print_visitor::~ir_builder_print_visitor()
{
   _mesa_hash_table_destroy(index_map, NULL);
}

void ir_builder_print_visitor::indent(void)
{
   for (int i = 0; i < indentation; i++)
      fprintf(f, "   ");
}

void
ir_builder_print_visitor::print_with_indent(const char *fmt, ...)
{
   va_list ap;

   indent();

   va_start(ap, fmt);
   vfprintf(f, fmt, ap);
   va_end(ap);
}

void
ir_builder_print_visitor::print_without_indent(const char *fmt, ...)
{
   va_list ap;

   va_start(ap, fmt);
   vfprintf(f, fmt, ap);
   va_end(ap);
}

void
ir_builder_print_visitor::print_without_declaration(const ir_rvalue *ir)
{
   switch (ir->ir_type) {
   case ir_type_dereference_variable:
      print_without_declaration((ir_dereference_variable *) ir);
      break;
   case ir_type_constant:
      print_without_declaration((ir_constant *) ir);
      break;
   case ir_type_swizzle:
      print_without_declaration((ir_swizzle *) ir);
      break;
   case ir_type_expression:
      print_without_declaration((ir_expression *) ir);
      break;
   default:
      unreachable("Invalid IR type.");
   }
}

ir_visitor_status
ir_builder_print_visitor::visit(ir_variable *ir)
{
   const unsigned my_index = next_ir_index++;

   _mesa_hash_table_insert(index_map, ir, (void *)(uintptr_t) my_index);

   const char *mode_str;
   switch (ir->data.mode) {
   case ir_var_auto: mode_str = "ir_var_auto"; break;
   case ir_var_uniform: mode_str = "ir_var_uniform"; break;
   case ir_var_shader_storage: mode_str = "ir_var_shader_storage"; break;
   case ir_var_shader_shared: mode_str = "ir_var_shader_shared"; break;
   case ir_var_shader_in: mode_str = "ir_var_shader_in"; break;
   case ir_var_shader_out: mode_str = "ir_var_shader_out"; break;
   case ir_var_function_in: mode_str = "ir_var_function_in"; break;
   case ir_var_function_out: mode_str = "ir_var_function_out"; break;
   case ir_var_function_inout: mode_str = "ir_var_function_inout"; break;
   case ir_var_const_in: mode_str = "ir_var_const_in"; break;
   case ir_var_system_value: mode_str = "ir_var_system_value"; break;
   case ir_var_temporary: mode_str = "ir_var_temporary"; break;
   default:
      unreachable("Invalid variable mode");
   }

   if (ir->data.mode == ir_var_temporary) {
      print_with_indent("ir_variable *const r%04X = body.make_temp(glsl_type::%s_type, \"%s\");\n",
                        my_index,
                        ir->type->name,
                        ir->name);
   } else {
      print_with_indent("ir_variable *const r%04X = new(mem_ctx) ir_variable(glsl_type::%s_type, \"%s\", %s);\n",
                        my_index,
                        ir->type->name,
                        ir->name,
                        mode_str);

      switch (ir->data.mode) {
      case ir_var_function_in:
      case ir_var_function_out:
      case ir_var_function_inout:
      case ir_var_const_in:
         print_with_indent("sig_parameters.push_tail(r%04X);\n", my_index);
         break;
      default:
         print_with_indent("body.emit(r%04X);\n", my_index);
         break;
      }
   }

   return visit_continue;
}

void
ir_builder_print_visitor::print_without_declaration(const ir_dereference_variable *ir)
{
   const struct hash_entry *const he =
      _mesa_hash_table_search(index_map, ir->var);

   print_without_indent("r%04X", (unsigned)(uintptr_t) he->data);
}

ir_visitor_status
ir_builder_print_visitor::visit(ir_dereference_variable *ir)
{
   const struct hash_entry *const he =
      _mesa_hash_table_search(index_map, ir->var);

   if (he != NULL)
      _mesa_hash_table_insert(index_map, ir, he->data);

   return visit_continue;
}

ir_visitor_status
ir_builder_print_visitor::visit_enter(ir_function_signature *ir)
{
   if (!ir->is_defined)
      return visit_continue_with_parent;

   print_with_indent("ir_function_signature *\n"
                     "%s(void *mem_ctx, builtin_available_predicate avail)\n"
                     "{\n",
                     ir->function_name());
   indentation++;
   print_with_indent("ir_function_signature *const sig =\n");
   print_with_indent("   new(mem_ctx) ir_function_signature(glsl_type::%s_type, avail);\n",
                     ir->return_type->name);

   print_with_indent("ir_factory body(&sig->body, mem_ctx);\n");
   print_with_indent("sig->is_defined = true;\n\n");

   if (!ir->parameters.is_empty())
      print_with_indent("exec_list sig_parameters;\n\n");

   return visit_continue;
}

ir_visitor_status
ir_builder_print_visitor::visit_leave(ir_function_signature *ir)
{
   if (!ir->parameters.is_empty())
      print_with_indent("sig->replace_parameters(&sig_parameters);\n");

   print_with_indent("return sig;\n");
   indentation--;
   print_with_indent("}\n");
   return visit_continue;
}

void
ir_builder_print_visitor::print_without_declaration(const ir_constant *ir)
{
  if (ir->type->is_scalar()) {
      switch (ir->type->base_type) {
      case GLSL_TYPE_UINT:
         print_without_indent("body.constant(%uu)", ir->value.u[0]);
         return;
      case GLSL_TYPE_INT:
         print_without_indent("body.constant(int(%d))", ir->value.i[0]);
         return;
      case GLSL_TYPE_FLOAT:
         print_without_indent("body.constant(%ff)", ir->value.f[0]);
         return;
      case GLSL_TYPE_BOOL:
         print_without_indent("body.constant(%s)",
                              ir->value.i[0] != 0 ? "true" : "false");
         return;
      default:
         break;
      }
   }

   ir_constant_data all_zero;
   memset(&all_zero, 0, sizeof(all_zero));

   if (memcmp(&ir->value, &all_zero, sizeof(all_zero)) == 0) {
      print_without_indent("ir_constant::zero(mem_ctx, glsl_type::%s_type)",
                           ir->type->name);
   }
}

ir_visitor_status
ir_builder_print_visitor::visit(ir_constant *ir)
{
   const unsigned my_index = next_ir_index++;

   _mesa_hash_table_insert(index_map, ir, (void *)(uintptr_t) my_index);

   if (ir->type == glsl_type::uint_type ||
       ir->type == glsl_type::int_type ||
       ir->type == glsl_type::float_type ||
       ir->type == glsl_type::bool_type) {
      print_with_indent("ir_constant *const r%04X = ", my_index);
      print_without_declaration(ir);
      print_without_indent(";\n");
      return visit_continue;
   }

   ir_constant_data all_zero;
   memset(&all_zero, 0, sizeof(all_zero));

   if (memcmp(&ir->value, &all_zero, sizeof(all_zero)) == 0) {
      print_with_indent("ir_constant *const r%04X = ", my_index);
      print_without_declaration(ir);
      print_without_indent(";\n");
   } else {
      print_with_indent("ir_constant_data r%04X_data;\n", my_index);
      print_with_indent("memset(&r%04X_data, 0, sizeof(ir_constant_data));\n",
                        my_index);
      for (unsigned i = 0; i < 16; i++) {
         switch (ir->type->base_type) {
         case GLSL_TYPE_UINT:
            if (ir->value.u[i] != 0)
               print_with_indent("r%04X_data.u[%u] = %u;\n",
                                    my_index, i, ir->value.u[i]);
            break;
         case GLSL_TYPE_INT:
            if (ir->value.i[i] != 0)
               print_with_indent("r%04X_data.i[%u] = %i;\n",
                                    my_index, i, ir->value.i[i]);
            break;
         case GLSL_TYPE_FLOAT:
            if (ir->value.u[i] != 0)
               print_with_indent("r%04X_data.u[%u] = 0x%08x; /* %f */\n",
                                    my_index,
                                    i,
                                    ir->value.u[i],
                                    ir->value.f[i]);
            break;
         case GLSL_TYPE_DOUBLE: {
            uint64_t v;

            STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));

            memcpy(&v, &ir->value.d[i], sizeof(v));
            if (v != 0)
               print_with_indent("r%04X_data.u64[%u] = 0x%016" PRIx64 "; /* %g */\n",
                                    my_index, i, v, ir->value.d[i]);
            break;
         }
         case GLSL_TYPE_UINT64:
            if (ir->value.u64[i] != 0)
               print_with_indent("r%04X_data.u64[%u] = %" PRIu64 ";\n",
                                    my_index,
                                    i,
                                    ir->value.u64[i]);
            break;
         case GLSL_TYPE_INT64:
            if (ir->value.i64[i] != 0)
               print_with_indent("r%04X_data.i64[%u] = %" PRId64 ";\n",
                                    my_index,
                                    i,
                                    ir->value.i64[i]);
            break;
         case GLSL_TYPE_BOOL:
            if (ir->value.u[i] != 0)
               print_with_indent("r%04X_data.u[%u] = 1;\n", my_index, i);
            break;
         default:
            unreachable("Invalid constant type");
         }
      }

      print_with_indent("ir_constant *const r%04X = new(mem_ctx) ir_constant(glsl_type::%s_type, &r%04X_data);\n",
                        my_index,
                        ir->type->name,
                        my_index);
   }

   return visit_continue;
}

void
ir_builder_print_visitor::print_without_declaration(const ir_swizzle *ir)
{
   const struct hash_entry *const he =
      _mesa_hash_table_search(index_map, ir->val);

   if (ir->mask.num_components == 1) {
      static const char swiz[4] = { 'x', 'y', 'z', 'w' };

      if (is_simple_operand(ir->val)) {
         print_without_indent("swizzle_%c(", swiz[ir->mask.x]);
         print_without_declaration(ir->val);
         print_without_indent(")");
      } else {
         print_without_indent("swizzle_%c(r%04X)",
                              swiz[ir->mask.x],
                              (unsigned)(uintptr_t) he->data);
      }
   } else {
      static const char swiz[4] = { 'X', 'Y', 'Z', 'W' };

      print_without_indent("swizzle(r%04X, MAKE_SWIZZLE4(SWIZZLE_%c, SWIZZLE_%c, SWIZZLE_%c, SWIZZLE_%c), %u)",
                           (unsigned)(uintptr_t) he->data,
                           swiz[ir->mask.x],
                           swiz[ir->mask.y],
                           swiz[ir->mask.z],
                           swiz[ir->mask.w],
                           ir->mask.num_components);
   }
}

ir_visitor_status
ir_builder_print_visitor::visit_leave(ir_swizzle *ir)
{
   const unsigned my_index = next_ir_index++;

   _mesa_hash_table_insert(index_map, ir, (void *)(uintptr_t) my_index);

   print_with_indent("ir_swizzle *const r%04X = ", my_index);
   print_without_declaration(ir);
   print_without_indent(";\n");

   return visit_continue;
}

ir_visitor_status
ir_builder_print_visitor::visit_enter(ir_assignment *ir)
{
   ir_expression *const rhs_expr = ir->rhs->as_expression();

   if (!is_simple_operand(ir->rhs) && rhs_expr == NULL)
      return visit_continue;

   if (rhs_expr != NULL) {
      const unsigned num_op = rhs_expr->num_operands;

      for (unsigned i = 0; i < num_op; i++) {
         if (is_simple_operand(rhs_expr->operands[i]))
            continue;

         rhs_expr->operands[i]->accept(this);
      }
   }

   ir_visitor_status s;

   this->in_assignee = true;
   s = ir->lhs->accept(this);
   this->in_assignee = false;
   if (s != visit_continue)
      return (s == visit_continue_with_parent) ? visit_continue : s;

   assert(ir->condition == NULL);

   const struct hash_entry *const he_lhs =
      _mesa_hash_table_search(index_map, ir->lhs);

   print_with_indent("body.emit(assign(r%04X, ",
                     (unsigned)(uintptr_t) he_lhs->data);
   print_without_declaration(ir->rhs);
   print_without_indent(", 0x%02x));\n\n", ir->write_mask);

   return visit_continue_with_parent;
}

ir_visitor_status
ir_builder_print_visitor::visit_leave(ir_assignment *ir)
{
   const struct hash_entry *const he_lhs =
      _mesa_hash_table_search(index_map, ir->lhs);

   const struct hash_entry *const he_rhs =
      _mesa_hash_table_search(index_map, ir->rhs);

   assert(ir->condition == NULL);

   print_with_indent("body.emit(assign(r%04X, r%04X, 0x%02x));\n\n",
                     (unsigned)(uintptr_t) he_lhs->data,
                     (unsigned)(uintptr_t) he_rhs->data,
                     ir->write_mask);

   return visit_continue;
}

void
ir_builder_print_visitor::print_without_declaration(const ir_expression *ir)
{
   const unsigned num_op = ir->num_operands;

   static const char *const arity[] = {
      "", "unop", "binop", "triop", "quadop"
   };

   switch (ir->operation) {
   case ir_unop_neg:
   case ir_binop_add:
   case ir_binop_sub:
   case ir_binop_mul:
   case ir_binop_imul_high:
   case ir_binop_less:
   case ir_binop_greater:
   case ir_binop_lequal:
   case ir_binop_gequal:
   case ir_binop_equal:
   case ir_binop_nequal:
   case ir_binop_lshift:
   case ir_binop_rshift:
   case ir_binop_bit_and:
   case ir_binop_bit_xor:
   case ir_binop_bit_or:
   case ir_binop_logic_and:
   case ir_binop_logic_xor:
   case ir_binop_logic_or:
      print_without_indent("%s(",
                           ir_expression_operation_enum_strings[ir->operation]);
      break;
   default:
      print_without_indent("expr(ir_%s_%s, ",
                           arity[num_op],
                           ir_expression_operation_enum_strings[ir->operation]);
      break;
   }

   for (unsigned i = 0; i < num_op; i++) {
      if (is_simple_operand(ir->operands[i]))
         print_without_declaration(ir->operands[i]);
      else {
         const struct hash_entry *const he =
            _mesa_hash_table_search(index_map, ir->operands[i]);

         print_without_indent("r%04X", (unsigned)(uintptr_t) he->data);
      }

      if (i < num_op - 1)
         print_without_indent(", ");
   }

   print_without_indent(")");
}

ir_visitor_status
ir_builder_print_visitor::visit_enter(ir_expression *ir)
{
   const unsigned num_op = ir->num_operands;

   for (unsigned i = 0; i < num_op; i++) {
      if (is_simple_operand(ir->operands[i]))
         continue;

      ir->operands[i]->accept(this);
   }

   const unsigned my_index = next_ir_index++;

   _mesa_hash_table_insert(index_map, ir, (void *)(uintptr_t) my_index);

   print_with_indent("ir_expression *const r%04X = ", my_index);
   print_without_declaration(ir);
   print_without_indent(";\n");

   return visit_continue_with_parent;
}

ir_visitor_status
ir_builder_print_visitor::visit_enter(ir_if *ir)
{
   const unsigned my_index = next_ir_index++;

   print_with_indent("/* IF CONDITION */\n");

   ir_visitor_status s = ir->condition->accept(this);
   if (s != visit_continue)
      return (s == visit_continue_with_parent) ? visit_continue : s;

   const struct hash_entry *const he =
      _mesa_hash_table_search(index_map, ir->condition);

   print_with_indent("ir_if *f%04X = new(mem_ctx) ir_if(operand(r%04X).val);\n",
                     my_index,
                     (unsigned)(uintptr_t) he->data);
   print_with_indent("exec_list *const f%04X_parent_instructions = body.instructions;\n\n",
                     my_index);

   indentation++;
   print_with_indent("/* THEN INSTRUCTIONS */\n");
   print_with_indent("body.instructions = &f%04X->then_instructions;\n\n",
                     my_index);

   if (s != visit_continue_with_parent) {
      s = visit_list_elements(this, &ir->then_instructions);
      if (s == visit_stop)
	 return s;
   }

   print_without_indent("\n");

   if (!ir->else_instructions.is_empty()) {
      print_with_indent("/* ELSE INSTRUCTIONS */\n");
      print_with_indent("body.instructions = &f%04X->else_instructions;\n\n",
              my_index);

      if (s != visit_continue_with_parent) {
         s = visit_list_elements(this, &ir->else_instructions);
         if (s == visit_stop)
            return s;
      }

      print_without_indent("\n");
   }

   indentation--;

   print_with_indent("body.instructions = f%04X_parent_instructions;\n",
                     my_index);
   print_with_indent("body.emit(f%04X);\n\n",
                     my_index);
   print_with_indent("/* END IF */\n\n");

   return visit_continue_with_parent;
}

ir_visitor_status
ir_builder_print_visitor::visit_leave(ir_return *ir)
{
   const struct hash_entry *const he =
      _mesa_hash_table_search(index_map, ir->value);

   print_with_indent("body.emit(ret(r%04X));\n\n",
                     (unsigned)(uintptr_t) he->data);

   return visit_continue;
}

ir_visitor_status
ir_builder_print_visitor::visit_leave(ir_call *ir)
{
   const unsigned my_index = next_ir_index++;

   print_without_indent("\n");
   print_with_indent("/* CALL %s */\n", ir->callee_name());
   print_with_indent("exec_list r%04X_parameters;\n", my_index);

   foreach_in_list(ir_dereference_variable, param, &ir->actual_parameters) {
      const struct hash_entry *const he =
         _mesa_hash_table_search(index_map, param);

      print_with_indent("r%04X_parameters.push_tail(operand(r%04X).val);\n",
                        my_index,
                        (unsigned)(uintptr_t) he->data);
   }

   char return_deref_string[32];
   if (ir->return_deref) {
      const struct hash_entry *const he =
         _mesa_hash_table_search(index_map, ir->return_deref);

      snprintf(return_deref_string, sizeof(return_deref_string),
               "operand(r%04X).val",
               (unsigned)(uintptr_t) he->data);
   } else {
      strcpy(return_deref_string, "NULL");
   }

   print_with_indent("body.emit(new(mem_ctx) ir_call(shader->symbols->get_function(\"%s\"),\n",
                     ir->callee_name());
   print_with_indent("                               %s, &r%04X_parameters);\n\n",
                     return_deref_string,
                     my_index);
   return visit_continue;
}

ir_visitor_status
ir_builder_print_visitor::visit_enter(ir_loop *ir)
{
   const unsigned my_index = next_ir_index++;

   _mesa_hash_table_insert(index_map, ir, (void *)(uintptr_t) my_index);

   print_with_indent("/* LOOP BEGIN */\n");
   print_with_indent("ir_loop *f%04X = new(mem_ctx) ir_loop();\n", my_index);
   print_with_indent("exec_list *const f%04X_parent_instructions = body.instructions;\n\n",
                     my_index);

   indentation++;

   print_with_indent("body.instructions = &f%04X->body_instructions;\n\n",
                     my_index);

   return visit_continue;
}

ir_visitor_status
ir_builder_print_visitor::visit_leave(ir_loop *ir)
{
   const struct hash_entry *const he =
      _mesa_hash_table_search(index_map, ir);

   indentation--;

   print_with_indent("/* LOOP END */\n\n");
   print_with_indent("body.instructions = f%04X_parent_instructions;\n",
                     (unsigned)(uintptr_t) he->data);
   print_with_indent("body.emit(f%04X);\n\n",
                     (unsigned)(uintptr_t) he->data);

   return visit_continue;
}

ir_visitor_status
ir_builder_print_visitor::visit(ir_loop_jump *ir)
{
   print_with_indent("body.emit(new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_%s));\n\n",
                     ir->is_break() ? "break" : "continue");
   return visit_continue;
}