ir_print_visitor.cpp revision 3c6fea3048a0d9add2fec621d30c32f3519d8868
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 static const char *const operators[] = { 90 "~", 91 "!", 92 "-", 93 "abs", 94 "rcp", 95 "rsq", 96 "sqrt", 97 "exp", 98 "log", 99 "exp2", 100 "log2", 101 "f2i", 102 "i2f", 103 "u2f", 104 "trunc", 105 "ceil", 106 "floor", 107 "+", 108 "-", 109 "*", 110 "/", 111 "%", 112 "<", 113 ">", 114 "<=", 115 ">=", 116 "==", 117 "!=", 118 "<<", 119 ">>", 120 "&", 121 "^", 122 "|", 123 "&&", 124 "^^", 125 "||", 126 "!", 127 "dot", 128 "min", 129 "max", 130 "pow", 131 }; 132 133 printf("(expression "); 134 135 assert((unsigned int)ir->operation < 136 sizeof(operators) / sizeof(operators[0])); 137 138 printf("%s", operators[ir->operation]); 139 printf("("); 140 if (ir->operands[0]) 141 ir->operands[0]->accept(this); 142 printf(") "); 143 144 printf("("); 145 if (ir->operands[1]) 146 ir->operands[1]->accept(this); 147 printf(")) "); 148} 149 150 151void ir_print_visitor::visit(ir_swizzle *ir) 152{ 153 const unsigned swiz[4] = { 154 ir->mask.x, 155 ir->mask.y, 156 ir->mask.z, 157 ir->mask.w, 158 }; 159 160 printf("(swiz "); 161 for (unsigned i = 0; i < ir->mask.num_components; i++) { 162 printf("%c", "xyzw"[swiz[i]]); 163 } 164 printf(" "); 165 ir->val->accept(this); 166 printf(")"); 167} 168 169 170void ir_print_visitor::visit(ir_dereference *ir) 171{ 172 deref_depth++; 173 174 switch (ir->mode) { 175 case ir_dereference::ir_reference_variable: { 176 printf("(var_ref "); 177 ir->var->accept(this); 178 printf(") "); 179 break; 180 } 181 case ir_dereference::ir_reference_array: 182 printf("(array_ref "); 183 ir->var->accept(this); 184 ir->selector.array_index->accept(this); 185 printf(") "); 186 break; 187 case ir_dereference::ir_reference_record: 188 printf("(record_ref "); 189 ir->var->accept(this); 190 printf("(%s)) ", ir->selector.field); 191 break; 192 } 193 194 deref_depth--; 195} 196 197 198void ir_print_visitor::visit(ir_assignment *ir) 199{ 200 printf("(assign ("); 201 202 if (ir->condition) 203 ir->condition->accept(this); 204 else 205 printf("true"); 206 207 printf(") ("); 208 209 ir->lhs->accept(this); 210 211 printf(") ("); 212 213 ir->rhs->accept(this); 214 printf(") "); 215} 216 217 218void ir_print_visitor::visit(ir_constant *ir) 219{ 220 const glsl_type *const base_type = ir->type->get_base_type(); 221 222 printf("(constant ("); 223 print_type(base_type); 224 printf(") "); 225 226 printf("(%d) (", ir->type->components()); 227 for (unsigned i = 0; i < ir->type->components(); i++) { 228 if (i != 0) 229 printf(", "); 230 231 switch (base_type->base_type) { 232 case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break; 233 case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break; 234 case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break; 235 case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break; 236 default: assert(0); 237 } 238 } 239 printf(")) "); 240} 241 242 243void 244ir_print_visitor::visit(ir_call *ir) 245{ 246 printf("(call (%s) ", ir->callee_name()); 247 foreach_iter(exec_list_iterator, iter, *ir) { 248 ir_instruction *const inst = (ir_instruction *) iter.get(); 249 250 inst->accept(this); 251 } 252} 253 254 255void 256ir_print_visitor::visit(ir_return *ir) 257{ 258 printf("(return"); 259 260 ir_rvalue *const value = ir->get_value(); 261 if (value) { 262 printf(" "); 263 value->accept(this); 264 } 265 266 printf(")"); 267} 268 269 270void 271ir_print_visitor::visit(ir_if *ir) 272{ 273 printf("(if "); 274 ir->condition->accept(this); 275 276 printf("(\n"); 277 foreach_iter(exec_list_iterator, iter, ir->then_instructions) { 278 ir_instruction *const inst = (ir_instruction *) iter.get(); 279 280 inst->accept(this); 281 } 282 printf(")\n"); 283 284 printf("(\n"); 285 foreach_iter(exec_list_iterator, iter, ir->else_instructions) { 286 ir_instruction *const inst = (ir_instruction *) iter.get(); 287 288 inst->accept(this); 289 } 290 printf("))\n"); 291} 292