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 <inttypes.h> /* for PRIx64 macro */ 25#include "ir_print_visitor.h" 26#include "compiler/glsl_types.h" 27#include "glsl_parser_extras.h" 28#include "main/macros.h" 29#include "util/hash_table.h" 30#include "util/u_string.h" 31 32static void print_type(FILE *f, const glsl_type *t); 33 34void 35ir_instruction::print(void) const 36{ 37 this->fprint(stdout); 38} 39 40void 41ir_instruction::fprint(FILE *f) const 42{ 43 ir_instruction *deconsted = const_cast<ir_instruction *>(this); 44 45 ir_print_visitor v(f); 46 deconsted->accept(&v); 47} 48 49extern "C" { 50void 51_mesa_print_ir(FILE *f, exec_list *instructions, 52 struct _mesa_glsl_parse_state *state) 53{ 54 if (state) { 55 for (unsigned i = 0; i < state->num_user_structures; i++) { 56 const glsl_type *const s = state->user_structures[i]; 57 58 fprintf(f, "(structure (%s) (%s@%p) (%u) (\n", 59 s->name, s->name, (void *) s, s->length); 60 61 for (unsigned j = 0; j < s->length; j++) { 62 fprintf(f, "\t(("); 63 print_type(f, s->fields.structure[j].type); 64 fprintf(f, ")(%s))\n", s->fields.structure[j].name); 65 } 66 67 fprintf(f, ")\n"); 68 } 69 } 70 71 fprintf(f, "(\n"); 72 foreach_in_list(ir_instruction, ir, instructions) { 73 ir->fprint(f); 74 if (ir->ir_type != ir_type_function) 75 fprintf(f, "\n"); 76 } 77 fprintf(f, ")\n"); 78} 79 80void 81fprint_ir(FILE *f, const void *instruction) 82{ 83 const ir_instruction *ir = (const ir_instruction *)instruction; 84 ir->fprint(f); 85} 86 87} /* extern "C" */ 88 89ir_print_visitor::ir_print_visitor(FILE *f) 90 : f(f) 91{ 92 indentation = 0; 93 printable_names = _mesa_pointer_hash_table_create(NULL); 94 symbols = _mesa_symbol_table_ctor(); 95 mem_ctx = ralloc_context(NULL); 96} 97 98ir_print_visitor::~ir_print_visitor() 99{ 100 _mesa_hash_table_destroy(printable_names, NULL); 101 _mesa_symbol_table_dtor(symbols); 102 ralloc_free(mem_ctx); 103} 104 105void ir_print_visitor::indent(void) 106{ 107 for (int i = 0; i < indentation; i++) 108 fprintf(f, " "); 109} 110 111const char * 112ir_print_visitor::unique_name(ir_variable *var) 113{ 114 /* var->name can be NULL in function prototypes when a type is given for a 115 * parameter but no name is given. In that case, just return an empty 116 * string. Don't worry about tracking the generated name in the printable 117 * names hash because this is the only scope where it can ever appear. 118 */ 119 if (var->name == NULL) { 120 static unsigned arg = 1; 121 return ralloc_asprintf(this->mem_ctx, "parameter@%u", arg++); 122 } 123 124 /* Do we already have a name for this variable? */ 125 struct hash_entry * entry = 126 _mesa_hash_table_search(this->printable_names, var); 127 128 if (entry != NULL) { 129 return (const char *) entry->data; 130 } 131 132 /* If there's no conflict, just use the original name */ 133 const char* name = NULL; 134 if (_mesa_symbol_table_find_symbol(this->symbols, var->name) == NULL) { 135 name = var->name; 136 } else { 137 static unsigned i = 1; 138 name = ralloc_asprintf(this->mem_ctx, "%s@%u", var->name, ++i); 139 } 140 _mesa_hash_table_insert(this->printable_names, var, (void *) name); 141 _mesa_symbol_table_add_symbol(this->symbols, name, var); 142 return name; 143} 144 145static void 146print_type(FILE *f, const glsl_type *t) 147{ 148 if (t->is_array()) { 149 fprintf(f, "(array "); 150 print_type(f, t->fields.array); 151 fprintf(f, " %u)", t->length); 152 } else if (t->is_struct() && !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 159void ir_print_visitor::visit(ir_rvalue *) 160{ 161 fprintf(f, "error"); 162} 163 164void ir_print_visitor::visit(ir_variable *ir) 165{ 166 fprintf(f, "(declare "); 167 168 char binding[32] = {0}; 169 if (ir->data.binding) 170 util_snprintf(binding, sizeof(binding), "binding=%i ", ir->data.binding); 171 172 char loc[32] = {0}; 173 if (ir->data.location != -1) 174 util_snprintf(loc, sizeof(loc), "location=%i ", ir->data.location); 175 176 char component[32] = {0}; 177 if (ir->data.explicit_component || ir->data.location_frac != 0) 178 util_snprintf(component, sizeof(component), "component=%i ", 179 ir->data.location_frac); 180 181 char stream[32] = {0}; 182 if (ir->data.stream & (1u << 31)) { 183 if (ir->data.stream & ~(1u << 31)) { 184 util_snprintf(stream, sizeof(stream), "stream(%u,%u,%u,%u) ", 185 ir->data.stream & 3, (ir->data.stream >> 2) & 3, 186 (ir->data.stream >> 4) & 3, (ir->data.stream >> 6) & 3); 187 } 188 } else if (ir->data.stream) { 189 util_snprintf(stream, sizeof(stream), "stream%u ", ir->data.stream); 190 } 191 192 char image_format[32] = {0}; 193 if (ir->data.image_format) { 194 util_snprintf(image_format, sizeof(image_format), "format=%x ", 195 ir->data.image_format); 196 } 197 198 const char *const cent = (ir->data.centroid) ? "centroid " : ""; 199 const char *const samp = (ir->data.sample) ? "sample " : ""; 200 const char *const patc = (ir->data.patch) ? "patch " : ""; 201 const char *const inv = (ir->data.invariant) ? "invariant " : ""; 202 const char *const explicit_inv = (ir->data.explicit_invariant) ? "explicit_invariant " : ""; 203 const char *const prec = (ir->data.precise) ? "precise " : ""; 204 const char *const bindless = (ir->data.bindless) ? "bindless " : ""; 205 const char *const bound = (ir->data.bound) ? "bound " : ""; 206 const char *const memory_read_only = (ir->data.memory_read_only) ? "readonly " : ""; 207 const char *const memory_write_only = (ir->data.memory_write_only) ? "writeonly " : ""; 208 const char *const memory_coherent = (ir->data.memory_coherent) ? "coherent " : ""; 209 const char *const memory_volatile = (ir->data.memory_volatile) ? "volatile " : ""; 210 const char *const memory_restrict = (ir->data.memory_restrict) ? "restrict " : ""; 211 const char *const mode[] = { "", "uniform ", "shader_storage ", 212 "shader_shared ", "shader_in ", "shader_out ", 213 "in ", "out ", "inout ", 214 "const_in ", "sys ", "temporary " }; 215 STATIC_ASSERT(ARRAY_SIZE(mode) == ir_var_mode_count); 216 const char *const interp[] = { "", "smooth", "flat", "noperspective" }; 217 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_MODE_COUNT); 218 219 fprintf(f, "(%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s) ", 220 binding, loc, component, cent, bindless, bound, 221 image_format, memory_read_only, memory_write_only, 222 memory_coherent, memory_volatile, memory_restrict, 223 samp, patc, inv, explicit_inv, prec, mode[ir->data.mode], 224 stream, 225 interp[ir->data.interpolation]); 226 227 print_type(f, ir->type); 228 fprintf(f, " %s)", unique_name(ir)); 229} 230 231 232void ir_print_visitor::visit(ir_function_signature *ir) 233{ 234 _mesa_symbol_table_push_scope(symbols); 235 fprintf(f, "(signature "); 236 indentation++; 237 238 print_type(f, ir->return_type); 239 fprintf(f, "\n"); 240 indent(); 241 242 fprintf(f, "(parameters\n"); 243 indentation++; 244 245 foreach_in_list(ir_variable, inst, &ir->parameters) { 246 indent(); 247 inst->accept(this); 248 fprintf(f, "\n"); 249 } 250 indentation--; 251 252 indent(); 253 fprintf(f, ")\n"); 254 255 indent(); 256 257 fprintf(f, "(\n"); 258 indentation++; 259 260 foreach_in_list(ir_instruction, inst, &ir->body) { 261 indent(); 262 inst->accept(this); 263 fprintf(f, "\n"); 264 } 265 indentation--; 266 indent(); 267 fprintf(f, "))\n"); 268 indentation--; 269 _mesa_symbol_table_pop_scope(symbols); 270} 271 272 273void ir_print_visitor::visit(ir_function *ir) 274{ 275 fprintf(f, "(%s function %s\n", ir->is_subroutine ? "subroutine" : "", ir->name); 276 indentation++; 277 foreach_in_list(ir_function_signature, sig, &ir->signatures) { 278 indent(); 279 sig->accept(this); 280 fprintf(f, "\n"); 281 } 282 indentation--; 283 indent(); 284 fprintf(f, ")\n\n"); 285} 286 287 288void ir_print_visitor::visit(ir_expression *ir) 289{ 290 fprintf(f, "(expression "); 291 292 print_type(f, ir->type); 293 294 fprintf(f, " %s ", ir_expression_operation_strings[ir->operation]); 295 296 for (unsigned i = 0; i < ir->num_operands; i++) { 297 ir->operands[i]->accept(this); 298 } 299 300 fprintf(f, ") "); 301} 302 303 304void ir_print_visitor::visit(ir_texture *ir) 305{ 306 fprintf(f, "(%s ", ir->opcode_string()); 307 308 if (ir->op == ir_samples_identical) { 309 ir->sampler->accept(this); 310 fprintf(f, " "); 311 ir->coordinate->accept(this); 312 fprintf(f, ")"); 313 return; 314 } 315 316 print_type(f, ir->type); 317 fprintf(f, " "); 318 319 ir->sampler->accept(this); 320 fprintf(f, " "); 321 322 if (ir->op != ir_txs && ir->op != ir_query_levels && 323 ir->op != ir_texture_samples) { 324 ir->coordinate->accept(this); 325 326 fprintf(f, " "); 327 328 if (ir->offset != NULL) { 329 ir->offset->accept(this); 330 } else { 331 fprintf(f, "0"); 332 } 333 334 fprintf(f, " "); 335 } 336 337 if (ir->op != ir_txf && ir->op != ir_txf_ms && 338 ir->op != ir_txs && ir->op != ir_tg4 && 339 ir->op != ir_query_levels && ir->op != ir_texture_samples) { 340 if (ir->projector) 341 ir->projector->accept(this); 342 else 343 fprintf(f, "1"); 344 345 if (ir->shadow_comparator) { 346 fprintf(f, " "); 347 ir->shadow_comparator->accept(this); 348 } else { 349 fprintf(f, " ()"); 350 } 351 } 352 353 fprintf(f, " "); 354 switch (ir->op) 355 { 356 case ir_tex: 357 case ir_lod: 358 case ir_query_levels: 359 case ir_texture_samples: 360 break; 361 case ir_txb: 362 ir->lod_info.bias->accept(this); 363 break; 364 case ir_txl: 365 case ir_txf: 366 case ir_txs: 367 ir->lod_info.lod->accept(this); 368 break; 369 case ir_txf_ms: 370 ir->lod_info.sample_index->accept(this); 371 break; 372 case ir_txd: 373 fprintf(f, "("); 374 ir->lod_info.grad.dPdx->accept(this); 375 fprintf(f, " "); 376 ir->lod_info.grad.dPdy->accept(this); 377 fprintf(f, ")"); 378 break; 379 case ir_tg4: 380 ir->lod_info.component->accept(this); 381 break; 382 case ir_samples_identical: 383 unreachable("ir_samples_identical was already handled"); 384 }; 385 fprintf(f, ")"); 386} 387 388 389void ir_print_visitor::visit(ir_swizzle *ir) 390{ 391 const unsigned swiz[4] = { 392 ir->mask.x, 393 ir->mask.y, 394 ir->mask.z, 395 ir->mask.w, 396 }; 397 398 fprintf(f, "(swiz "); 399 for (unsigned i = 0; i < ir->mask.num_components; i++) { 400 fprintf(f, "%c", "xyzw"[swiz[i]]); 401 } 402 fprintf(f, " "); 403 ir->val->accept(this); 404 fprintf(f, ")"); 405} 406 407 408void ir_print_visitor::visit(ir_dereference_variable *ir) 409{ 410 ir_variable *var = ir->variable_referenced(); 411 fprintf(f, "(var_ref %s) ", unique_name(var)); 412} 413 414 415void ir_print_visitor::visit(ir_dereference_array *ir) 416{ 417 fprintf(f, "(array_ref "); 418 ir->array->accept(this); 419 ir->array_index->accept(this); 420 fprintf(f, ") "); 421} 422 423 424void ir_print_visitor::visit(ir_dereference_record *ir) 425{ 426 fprintf(f, "(record_ref "); 427 ir->record->accept(this); 428 429 const char *field_name = 430 ir->record->type->fields.structure[ir->field_idx].name; 431 fprintf(f, " %s) ", field_name); 432} 433 434 435void ir_print_visitor::visit(ir_assignment *ir) 436{ 437 fprintf(f, "(assign "); 438 439 if (ir->condition) 440 ir->condition->accept(this); 441 442 char mask[5]; 443 unsigned j = 0; 444 445 for (unsigned i = 0; i < 4; i++) { 446 if ((ir->write_mask & (1 << i)) != 0) { 447 mask[j] = "xyzw"[i]; 448 j++; 449 } 450 } 451 mask[j] = '\0'; 452 453 fprintf(f, " (%s) ", mask); 454 455 ir->lhs->accept(this); 456 457 fprintf(f, " "); 458 459 ir->rhs->accept(this); 460 fprintf(f, ") "); 461} 462 463 464void ir_print_visitor::visit(ir_constant *ir) 465{ 466 fprintf(f, "(constant "); 467 print_type(f, ir->type); 468 fprintf(f, " ("); 469 470 if (ir->type->is_array()) { 471 for (unsigned i = 0; i < ir->type->length; i++) 472 ir->get_array_element(i)->accept(this); 473 } else if (ir->type->is_struct()) { 474 for (unsigned i = 0; i < ir->type->length; i++) { 475 fprintf(f, "(%s ", ir->type->fields.structure[i].name); 476 ir->get_record_field(i)->accept(this); 477 fprintf(f, ")"); 478 } 479 } else { 480 for (unsigned i = 0; i < ir->type->components(); i++) { 481 if (i != 0) 482 fprintf(f, " "); 483 switch (ir->type->base_type) { 484 case GLSL_TYPE_UINT: fprintf(f, "%u", ir->value.u[i]); break; 485 case GLSL_TYPE_INT: fprintf(f, "%d", ir->value.i[i]); break; 486 case GLSL_TYPE_FLOAT: 487 if (ir->value.f[i] == 0.0f) 488 /* 0.0 == -0.0, so print with %f to get the proper sign. */ 489 fprintf(f, "%f", ir->value.f[i]); 490 else if (fabs(ir->value.f[i]) < 0.000001f) 491 fprintf(f, "%a", ir->value.f[i]); 492 else if (fabs(ir->value.f[i]) > 1000000.0f) 493 fprintf(f, "%e", ir->value.f[i]); 494 else 495 fprintf(f, "%f", ir->value.f[i]); 496 break; 497 case GLSL_TYPE_SAMPLER: 498 case GLSL_TYPE_IMAGE: 499 case GLSL_TYPE_UINT64: 500 fprintf(f, "%" PRIu64, ir->value.u64[i]); 501 break; 502 case GLSL_TYPE_INT64: fprintf(f, "%" PRIi64, ir->value.i64[i]); break; 503 case GLSL_TYPE_BOOL: fprintf(f, "%d", ir->value.b[i]); break; 504 case GLSL_TYPE_DOUBLE: 505 if (ir->value.d[i] == 0.0) 506 /* 0.0 == -0.0, so print with %f to get the proper sign. */ 507 fprintf(f, "%.1f", ir->value.d[i]); 508 else if (fabs(ir->value.d[i]) < 0.000001) 509 fprintf(f, "%a", ir->value.d[i]); 510 else if (fabs(ir->value.d[i]) > 1000000.0) 511 fprintf(f, "%e", ir->value.d[i]); 512 else 513 fprintf(f, "%f", ir->value.d[i]); 514 break; 515 default: 516 unreachable("Invalid constant type"); 517 } 518 } 519 } 520 fprintf(f, ")) "); 521} 522 523 524void 525ir_print_visitor::visit(ir_call *ir) 526{ 527 fprintf(f, "(call %s ", ir->callee_name()); 528 if (ir->return_deref) 529 ir->return_deref->accept(this); 530 fprintf(f, " ("); 531 foreach_in_list(ir_rvalue, param, &ir->actual_parameters) { 532 param->accept(this); 533 } 534 fprintf(f, "))\n"); 535} 536 537 538void 539ir_print_visitor::visit(ir_return *ir) 540{ 541 fprintf(f, "(return"); 542 543 ir_rvalue *const value = ir->get_value(); 544 if (value) { 545 fprintf(f, " "); 546 value->accept(this); 547 } 548 549 fprintf(f, ")"); 550} 551 552 553void 554ir_print_visitor::visit(ir_discard *ir) 555{ 556 fprintf(f, "(discard "); 557 558 if (ir->condition != NULL) { 559 fprintf(f, " "); 560 ir->condition->accept(this); 561 } 562 563 fprintf(f, ")"); 564} 565 566 567void 568ir_print_visitor::visit(ir_if *ir) 569{ 570 fprintf(f, "(if "); 571 ir->condition->accept(this); 572 573 fprintf(f, "(\n"); 574 indentation++; 575 576 foreach_in_list(ir_instruction, inst, &ir->then_instructions) { 577 indent(); 578 inst->accept(this); 579 fprintf(f, "\n"); 580 } 581 582 indentation--; 583 indent(); 584 fprintf(f, ")\n"); 585 586 indent(); 587 if (!ir->else_instructions.is_empty()) { 588 fprintf(f, "(\n"); 589 indentation++; 590 591 foreach_in_list(ir_instruction, inst, &ir->else_instructions) { 592 indent(); 593 inst->accept(this); 594 fprintf(f, "\n"); 595 } 596 indentation--; 597 indent(); 598 fprintf(f, "))\n"); 599 } else { 600 fprintf(f, "())\n"); 601 } 602} 603 604 605void 606ir_print_visitor::visit(ir_loop *ir) 607{ 608 fprintf(f, "(loop (\n"); 609 indentation++; 610 611 foreach_in_list(ir_instruction, inst, &ir->body_instructions) { 612 indent(); 613 inst->accept(this); 614 fprintf(f, "\n"); 615 } 616 indentation--; 617 indent(); 618 fprintf(f, "))\n"); 619} 620 621 622void 623ir_print_visitor::visit(ir_loop_jump *ir) 624{ 625 fprintf(f, "%s", ir->is_break() ? "break" : "continue"); 626} 627 628void 629ir_print_visitor::visit(ir_emit_vertex *ir) 630{ 631 fprintf(f, "(emit-vertex "); 632 ir->stream->accept(this); 633 fprintf(f, ")\n"); 634} 635 636void 637ir_print_visitor::visit(ir_end_primitive *ir) 638{ 639 fprintf(f, "(end-primitive "); 640 ir->stream->accept(this); 641 fprintf(f, ")\n"); 642} 643 644void 645ir_print_visitor::visit(ir_barrier *) 646{ 647 fprintf(f, "(barrier)\n"); 648} 649