ir_print_visitor.cpp revision affc1413ac9f1f077a4ba1a1b7135f73d7a71167
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#include <cstdio>
24#include "ir_print_visitor.h"
25#include "glsl_types.h"
26
27static void
28print_type(const glsl_type *t)
29{
30   if (t->base_type == GLSL_TYPE_ARRAY) {
31      printf("array (");
32      print_type(t->fields.array);
33      printf(") (%u))", t->length);
34   } else if (t->base_type == GLSL_TYPE_STRUCT) {
35      printf("struct (%s %u ", t->name ? t->name : "@", t->length);
36      printf("(FINISHME: structure fields go here) ");
37      printf(")");
38   } else {
39      printf("%s", t->name);
40   }
41}
42
43
44void ir_print_visitor::visit(ir_variable *ir)
45{
46   if (deref_depth) {
47      printf("(%s)", ir->name);
48   } else {
49      printf("(declare ");
50
51      const char *const cent = (ir->centroid) ? "centroid " : "";
52      const char *const inv = (ir->invariant) ? "invariant " : "";
53      const char *const mode[] = { "", "uniform ", "in ", "out ", "inout " };
54      const char *const interp[] = { "", "flat", "noperspective" };
55
56      printf("(%s%s%s%s) ",
57	     cent, inv, mode[ir->mode], interp[ir->interpolation]);
58
59      printf("(");
60      print_type(ir->type);
61      printf(") ");
62      printf("(%s)) ", ir->name);
63   }
64}
65
66
67void ir_print_visitor::visit(ir_label *ir)
68{
69   printf("\n(label %s)", ir->label);
70}
71
72
73void ir_print_visitor::visit(ir_function_signature *ir)
74{
75   printf("%s:%d:\n", __func__, __LINE__);
76   (void) ir;
77}
78
79
80void ir_print_visitor::visit(ir_function *ir)
81{
82   printf("(function %s\n", ir->name);
83   printf(")\n");
84}
85
86
87void ir_print_visitor::visit(ir_expression *ir)
88{
89   printf("(expression ");
90
91   printf("(FINISHME: operator) ");
92
93   printf("(");
94   if (ir->operands[0])
95      ir->operands[0]->accept(this);
96   printf(") ");
97
98   printf("(");
99   if (ir->operands[1])
100      ir->operands[1]->accept(this);
101   printf(")) ");
102}
103
104
105void ir_print_visitor::visit(ir_swizzle *ir)
106{
107   const unsigned swiz[4] = {
108      ir->mask.x,
109      ir->mask.y,
110      ir->mask.z,
111      ir->mask.w,
112   };
113
114   printf("(swiz ");
115   for (unsigned i = 0; i < ir->mask.num_components; i++) {
116      printf("%c", "xyzw"[swiz[i]]);
117   }
118   printf(" ");
119   ir->val->accept(this);
120   printf(")");
121}
122
123
124void ir_print_visitor::visit(ir_dereference *ir)
125{
126   deref_depth++;
127
128   switch (ir->mode) {
129   case ir_dereference::ir_reference_variable: {
130      printf("(var_ref ");
131      ir->var->accept(this);
132      printf(") ");
133      break;
134   }
135   case ir_dereference::ir_reference_array:
136      printf("(array_ref ");
137      ir->var->accept(this);
138      ir->selector.array_index->accept(this);
139      printf(") ");
140      break;
141   case ir_dereference::ir_reference_record:
142      printf("(record_ref ");
143      ir->var->accept(this);
144      printf("(%s)) ", ir->selector.field);
145      break;
146   }
147
148   deref_depth--;
149}
150
151
152void ir_print_visitor::visit(ir_assignment *ir)
153{
154   printf("(assign (");
155
156   if (ir->condition)
157      ir->condition->accept(this);
158   else
159      printf("true");
160
161   printf(") (");
162
163   ir->lhs->accept(this);
164
165   printf(") (");
166
167   ir->rhs->accept(this);
168   printf(") ");
169}
170
171
172void ir_print_visitor::visit(ir_constant *ir)
173{
174   const glsl_type *const base_type = ir->type->get_base_type();
175
176   printf("(constant (");
177   print_type(base_type);
178   printf(") ");
179
180   const unsigned num_values = 1
181      * ((ir->type->vector_elements > 0) ? ir->type->vector_elements : 1)
182      * ((ir->type->matrix_columns > 0) ? ir->type->matrix_columns : 1);
183
184   printf("(%d) (", num_values);
185   for (unsigned i = 0; i < num_values; i++) {
186      if (i != 0)
187	 printf(", ");
188
189      switch (base_type->base_type) {
190      case GLSL_TYPE_UINT:  printf("%u", ir->value.u[i]); break;
191      case GLSL_TYPE_INT:   printf("%d", ir->value.i[i]); break;
192      case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
193      case GLSL_TYPE_BOOL:  printf("%d", ir->value.b[i]); break;
194      default: assert(0);
195      }
196   }
197   printf(")) ");
198}
199
200
201void
202ir_print_visitor::visit(ir_call *ir)
203{
204   (void) ir;
205
206   printf("(call FINISHME: function name here\n");
207   printf("    (FINISHME: function paramaters here))\n");
208}
209
210
211void
212ir_print_visitor::visit(ir_return *ir)
213{
214   printf("(return");
215
216   ir_rvalue *const value = ir->get_value();
217   if (value) {
218      printf(" ");
219      value->accept(this);
220   }
221
222   printf(")");
223}
224