17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2019 Connor Abbott <cwabbott0@gmail.com>
37ec681f3Smrg * Copyright (C) 2019 Lyude Paul <thatslyude@gmail.com>
47ec681f3Smrg * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
57ec681f3Smrg * Copyright (C) 2019-2020 Collabora, Ltd.
67ec681f3Smrg *
77ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
87ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
97ec681f3Smrg * to deal in the Software without restriction, including without limitation
107ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
117ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
127ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
137ec681f3Smrg *
147ec681f3Smrg * The above copyright notice and this permission notice (including the next
157ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
167ec681f3Smrg * Software.
177ec681f3Smrg *
187ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
197ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
207ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
217ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
227ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
237ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
247ec681f3Smrg * SOFTWARE.
257ec681f3Smrg */
267ec681f3Smrg
277ec681f3Smrg#include "compiler.h"
287ec681f3Smrg#include "bi_print_common.h"
297ec681f3Smrg
307ec681f3Smrgstatic const char *
317ec681f3Smrgbi_reg_op_name(enum bifrost_reg_op op)
327ec681f3Smrg{
337ec681f3Smrg        switch (op) {
347ec681f3Smrg        case BIFROST_OP_IDLE: return "idle";
357ec681f3Smrg        case BIFROST_OP_READ: return "read";
367ec681f3Smrg        case BIFROST_OP_WRITE: return "write";
377ec681f3Smrg        case BIFROST_OP_WRITE_LO: return "write lo";
387ec681f3Smrg        case BIFROST_OP_WRITE_HI: return "write hi";
397ec681f3Smrg        default: return "invalid";
407ec681f3Smrg        }
417ec681f3Smrg}
427ec681f3Smrg
437ec681f3Smrgvoid
447ec681f3Smrgbi_print_slots(bi_registers *regs, FILE *fp)
457ec681f3Smrg{
467ec681f3Smrg        for (unsigned i = 0; i < 2; ++i) {
477ec681f3Smrg                if (regs->enabled[i])
487ec681f3Smrg                        fprintf(fp, "slot %u: %u\n", i, regs->slot[i]);
497ec681f3Smrg        }
507ec681f3Smrg
517ec681f3Smrg        if (regs->slot23.slot2) {
527ec681f3Smrg                fprintf(fp, "slot 2 (%s%s): %u\n",
537ec681f3Smrg                                bi_reg_op_name(regs->slot23.slot2),
547ec681f3Smrg                                regs->slot23.slot2 >= BIFROST_OP_WRITE ?
557ec681f3Smrg                                        " FMA": "",
567ec681f3Smrg                                regs->slot[2]);
577ec681f3Smrg        }
587ec681f3Smrg
597ec681f3Smrg        if (regs->slot23.slot3) {
607ec681f3Smrg                fprintf(fp, "slot 3 (%s %s): %u\n",
617ec681f3Smrg                                bi_reg_op_name(regs->slot23.slot3),
627ec681f3Smrg                                regs->slot23.slot3_fma ? "FMA" : "ADD",
637ec681f3Smrg                                regs->slot[3]);
647ec681f3Smrg        }
657ec681f3Smrg}
667ec681f3Smrg
677ec681f3Smrgvoid
687ec681f3Smrgbi_print_tuple(bi_tuple *tuple, FILE *fp)
697ec681f3Smrg{
707ec681f3Smrg        bi_instr *ins[2] = { tuple->fma, tuple->add };
717ec681f3Smrg
727ec681f3Smrg        for (unsigned i = 0; i < 2; ++i) {
737ec681f3Smrg                fprintf(fp, (i == 0) ? "\t* " : "\t+ ");
747ec681f3Smrg
757ec681f3Smrg                if (ins[i])
767ec681f3Smrg                        bi_print_instr(ins[i], fp);
777ec681f3Smrg                else
787ec681f3Smrg                        fprintf(fp, "NOP\n");
797ec681f3Smrg        }
807ec681f3Smrg}
817ec681f3Smrg
827ec681f3Smrgvoid
837ec681f3Smrgbi_print_clause(bi_clause *clause, FILE *fp)
847ec681f3Smrg{
857ec681f3Smrg        fprintf(fp, "id(%u)", clause->scoreboard_id);
867ec681f3Smrg
877ec681f3Smrg        if (clause->dependencies) {
887ec681f3Smrg                fprintf(fp, " wait(");
897ec681f3Smrg
907ec681f3Smrg                for (unsigned i = 0; i < 8; ++i) {
917ec681f3Smrg                        if (clause->dependencies & (1 << i))
927ec681f3Smrg                                fprintf(fp, "%u ", i);
937ec681f3Smrg                }
947ec681f3Smrg
957ec681f3Smrg                fprintf(fp, ")");
967ec681f3Smrg        }
977ec681f3Smrg
987ec681f3Smrg        fprintf(fp, " %s", bi_flow_control_name(clause->flow_control));
997ec681f3Smrg
1007ec681f3Smrg        if (!clause->next_clause_prefetch)
1017ec681f3Smrg               fprintf(fp, " no_prefetch");
1027ec681f3Smrg
1037ec681f3Smrg        if (clause->staging_barrier)
1047ec681f3Smrg                fprintf(fp, " osrb");
1057ec681f3Smrg
1067ec681f3Smrg        if (clause->td)
1077ec681f3Smrg                fprintf(fp, " td");
1087ec681f3Smrg
1097ec681f3Smrg        if (clause->pcrel_idx != ~0)
1107ec681f3Smrg                fprintf(fp, " pcrel(%u)", clause->pcrel_idx);
1117ec681f3Smrg
1127ec681f3Smrg        fprintf(fp, "\n");
1137ec681f3Smrg
1147ec681f3Smrg        for (unsigned i = 0; i < clause->tuple_count; ++i)
1157ec681f3Smrg                bi_print_tuple(&clause->tuples[i], fp);
1167ec681f3Smrg
1177ec681f3Smrg        if (clause->constant_count) {
1187ec681f3Smrg                for (unsigned i = 0; i < clause->constant_count; ++i)
1197ec681f3Smrg                        fprintf(fp, "%" PRIx64 " ", clause->constants[i]);
1207ec681f3Smrg
1217ec681f3Smrg                if (clause->branch_constant)
1227ec681f3Smrg                        fprintf(fp, "*");
1237ec681f3Smrg
1247ec681f3Smrg                fprintf(fp, "\n");
1257ec681f3Smrg        }
1267ec681f3Smrg
1277ec681f3Smrg        fprintf(fp, "\n");
1287ec681f3Smrg}
1297ec681f3Smrg
1307ec681f3Smrgvoid
1317ec681f3Smrgbi_print_block(bi_block *block, FILE *fp)
1327ec681f3Smrg{
1337ec681f3Smrg        fprintf(fp, "block%u {\n", block->name);
1347ec681f3Smrg
1357ec681f3Smrg        if (block->scheduled) {
1367ec681f3Smrg                bi_foreach_clause_in_block(block, clause)
1377ec681f3Smrg                        bi_print_clause(clause, fp);
1387ec681f3Smrg        } else {
1397ec681f3Smrg                bi_foreach_instr_in_block(block, ins)
1407ec681f3Smrg                        bi_print_instr((bi_instr *) ins, fp);
1417ec681f3Smrg        }
1427ec681f3Smrg
1437ec681f3Smrg        fprintf(fp, "}");
1447ec681f3Smrg
1457ec681f3Smrg        if (block->successors[0]) {
1467ec681f3Smrg                fprintf(fp, " -> ");
1477ec681f3Smrg
1487ec681f3Smrg                bi_foreach_successor((block), succ)
1497ec681f3Smrg                        fprintf(fp, "block%u ", succ->name);
1507ec681f3Smrg        }
1517ec681f3Smrg
1527ec681f3Smrg        if (block->predecessors->entries) {
1537ec681f3Smrg                fprintf(fp, " from");
1547ec681f3Smrg
1557ec681f3Smrg                bi_foreach_predecessor(block, pred)
1567ec681f3Smrg                        fprintf(fp, " block%u", pred->name);
1577ec681f3Smrg        }
1587ec681f3Smrg
1597ec681f3Smrg        fprintf(fp, "\n\n");
1607ec681f3Smrg}
1617ec681f3Smrg
1627ec681f3Smrgvoid
1637ec681f3Smrgbi_print_shader(bi_context *ctx, FILE *fp)
1647ec681f3Smrg{
1657ec681f3Smrg        bi_foreach_block(ctx, block)
1667ec681f3Smrg                bi_print_block(block, fp);
1677ec681f3Smrg}
168