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