2 * Copyright © 2010 Intel Corporation
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:
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
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.
24 #include "ir_print_visitor.h"
25 #include "glsl_types.h"
26 #include "glsl_parser_extras.h"
28 static void print_type(const glsl_type
*t
);
31 _mesa_print_ir(exec_list
*instructions
,
32 struct _mesa_glsl_parse_state
*state
)
34 for (unsigned i
= 0; i
< state
->num_user_structures
; i
++) {
35 const glsl_type
*const s
= state
->user_structures
[i
];
37 printf("(structure (%s) (%s@%p) (%u) (\n",
38 s
->name
, s
->name
, s
, s
->length
);
40 for (unsigned j
= 0; j
< s
->length
; j
++) {
42 print_type(s
->fields
.structure
[j
].type
);
43 printf(")(%s))\n", s
->fields
.structure
[j
].name
);
50 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
53 ((ir_instruction
*)iter
.get())->accept(& v
);
60 print_type(const glsl_type
*t
)
62 if (t
->base_type
== GLSL_TYPE_ARRAY
) {
64 print_type(t
->fields
.array
);
65 printf(" %u)", t
->length
);
66 } else if ((t
->base_type
== GLSL_TYPE_STRUCT
)
67 && (strncmp("gl_", t
->name
, 3) != 0)) {
68 printf("%s@%p", t
->name
, t
);
70 printf("%s", t
->name
);
75 void ir_print_visitor::visit(ir_variable
*ir
)
79 const char *const cent
= (ir
->centroid
) ? "centroid " : "";
80 const char *const inv
= (ir
->invariant
) ? "invariant " : "";
81 const char *const mode
[] = { "", "uniform ", "in ", "out ", "inout " };
82 const char *const interp
[] = { "", "flat", "noperspective" };
85 cent
, inv
, mode
[ir
->mode
], interp
[ir
->interpolation
]);
88 printf(" %s)", ir
->name
);
92 void ir_print_visitor::visit(ir_function_signature
*ir
)
94 printf("(signature ");
95 print_type(ir
->return_type
);
96 printf("\n (parameters\n");
97 foreach_iter(exec_list_iterator
, iter
, ir
->parameters
) {
98 ir_variable
*const inst
= (ir_variable
*) iter
.get();
105 foreach_iter(exec_list_iterator
, iter
, ir
->body
) {
106 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
115 void ir_print_visitor::visit(ir_function
*ir
)
117 printf("(function %s\n", ir
->name
);
118 foreach_iter(exec_list_iterator
, iter
, *ir
) {
119 ir_function_signature
*const sig
= (ir_function_signature
*) iter
.get();
129 void ir_print_visitor::visit(ir_expression
*ir
)
131 printf("(expression ");
133 print_type(ir
->type
);
135 printf(" %s ", ir
->operator_string());
138 ir
->operands
[0]->accept(this);
141 ir
->operands
[1]->accept(this);
146 void ir_print_visitor::visit(ir_texture
*ir
)
148 printf("(%s ", ir
->opcode_string());
150 ir
->sampler
->accept(this);
153 ir
->coordinate
->accept(this);
155 printf(" (%d %d %d) ", ir
->offsets
[0], ir
->offsets
[1], ir
->offsets
[2]);
157 if (ir
->op
!= ir_txf
) {
159 ir
->projector
->accept(this);
163 if (ir
->shadow_comparitor
) {
165 ir
->shadow_comparitor
->accept(this);
177 ir
->lod_info
.bias
->accept(this);
181 ir
->lod_info
.lod
->accept(this);
185 ir
->lod_info
.grad
.dPdx
->accept(this);
187 ir
->lod_info
.grad
.dPdy
->accept(this);
195 void ir_print_visitor::visit(ir_swizzle
*ir
)
197 const unsigned swiz
[4] = {
205 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
206 printf("%c", "xyzw"[swiz
[i
]]);
209 ir
->val
->accept(this);
214 void ir_print_visitor::visit(ir_dereference_variable
*ir
)
216 printf("(var_ref %s) ", ir
->variable_referenced()->name
);
220 void ir_print_visitor::visit(ir_dereference_array
*ir
)
222 printf("(array_ref ");
223 ir
->array
->accept(this);
224 ir
->array_index
->accept(this);
229 void ir_print_visitor::visit(ir_dereference_record
*ir
)
231 printf("(record_ref ");
232 ir
->record
->accept(this);
233 printf(" %s) ", ir
->field
);
237 void ir_print_visitor::visit(ir_assignment
*ir
)
242 ir
->condition
->accept(this);
244 printf("(constant bool (1))");
248 ir
->lhs
->accept(this);
252 ir
->rhs
->accept(this);
257 void ir_print_visitor::visit(ir_constant
*ir
)
259 const glsl_type
*const base_type
= ir
->type
->get_base_type();
261 printf("(constant ");
262 print_type(ir
->type
);
265 for (unsigned i
= 0; i
< ir
->type
->components(); i
++) {
269 switch (base_type
->base_type
) {
270 case GLSL_TYPE_UINT
: printf("%u", ir
->value
.u
[i
]); break;
271 case GLSL_TYPE_INT
: printf("%d", ir
->value
.i
[i
]); break;
272 case GLSL_TYPE_FLOAT
: printf("%f", ir
->value
.f
[i
]); break;
273 case GLSL_TYPE_BOOL
: printf("%d", ir
->value
.b
[i
]); break;
282 ir_print_visitor::visit(ir_call
*ir
)
284 printf("(call %s (", ir
->callee_name());
285 foreach_iter(exec_list_iterator
, iter
, *ir
) {
286 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
295 ir_print_visitor::visit(ir_return
*ir
)
299 ir_rvalue
*const value
= ir
->get_value();
310 ir_print_visitor::visit(ir_if
*ir
)
313 ir
->condition
->accept(this);
316 foreach_iter(exec_list_iterator
, iter
, ir
->then_instructions
) {
317 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
325 foreach_iter(exec_list_iterator
, iter
, ir
->else_instructions
) {
326 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
336 ir_print_visitor::visit(ir_loop
*ir
)
339 if (ir
->counter
!= NULL
)
340 ir
->counter
->accept(this);
342 if (ir
->from
!= NULL
)
343 ir
->from
->accept(this);
346 ir
->to
->accept(this);
348 if (ir
->increment
!= NULL
)
349 ir
->increment
->accept(this);
351 foreach_iter(exec_list_iterator
, iter
, ir
->body_instructions
) {
352 ir_instruction
*const inst
= (ir_instruction
*) iter
.get();
362 ir_print_visitor::visit(ir_loop_jump
*ir
)
364 printf("%s", ir
->is_break() ? "break" : "continue");