1/*
2 * Copyright (C) 2019 Connor Abbott <cwabbott0@gmail.com>
3 * Copyright (C) 2019 Lyude Paul <thatslyude@gmail.com>
4 * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
5 * Copyright (C) 2019-2020 Collabora, Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27#include "compiler.h"
28#include "bi_print_common.h"
29
30static const char *
31bi_reg_op_name(enum bifrost_reg_op op)
32{
33        switch (op) {
34        case BIFROST_OP_IDLE: return "idle";
35        case BIFROST_OP_READ: return "read";
36        case BIFROST_OP_WRITE: return "write";
37        case BIFROST_OP_WRITE_LO: return "write lo";
38        case BIFROST_OP_WRITE_HI: return "write hi";
39        default: return "invalid";
40        }
41}
42
43void
44bi_print_slots(bi_registers *regs, FILE *fp)
45{
46        for (unsigned i = 0; i < 2; ++i) {
47                if (regs->enabled[i])
48                        fprintf(fp, "slot %u: %u\n", i, regs->slot[i]);
49        }
50
51        if (regs->slot23.slot2) {
52                fprintf(fp, "slot 2 (%s%s): %u\n",
53                                bi_reg_op_name(regs->slot23.slot2),
54                                regs->slot23.slot2 >= BIFROST_OP_WRITE ?
55                                        " FMA": "",
56                                regs->slot[2]);
57        }
58
59        if (regs->slot23.slot3) {
60                fprintf(fp, "slot 3 (%s %s): %u\n",
61                                bi_reg_op_name(regs->slot23.slot3),
62                                regs->slot23.slot3_fma ? "FMA" : "ADD",
63                                regs->slot[3]);
64        }
65}
66
67void
68bi_print_tuple(bi_tuple *tuple, FILE *fp)
69{
70        bi_instr *ins[2] = { tuple->fma, tuple->add };
71
72        for (unsigned i = 0; i < 2; ++i) {
73                fprintf(fp, (i == 0) ? "\t* " : "\t+ ");
74
75                if (ins[i])
76                        bi_print_instr(ins[i], fp);
77                else
78                        fprintf(fp, "NOP\n");
79        }
80}
81
82void
83bi_print_clause(bi_clause *clause, FILE *fp)
84{
85        fprintf(fp, "id(%u)", clause->scoreboard_id);
86
87        if (clause->dependencies) {
88                fprintf(fp, " wait(");
89
90                for (unsigned i = 0; i < 8; ++i) {
91                        if (clause->dependencies & (1 << i))
92                                fprintf(fp, "%u ", i);
93                }
94
95                fprintf(fp, ")");
96        }
97
98        fprintf(fp, " %s", bi_flow_control_name(clause->flow_control));
99
100        if (!clause->next_clause_prefetch)
101               fprintf(fp, " no_prefetch");
102
103        if (clause->staging_barrier)
104                fprintf(fp, " osrb");
105
106        if (clause->td)
107                fprintf(fp, " td");
108
109        if (clause->pcrel_idx != ~0)
110                fprintf(fp, " pcrel(%u)", clause->pcrel_idx);
111
112        fprintf(fp, "\n");
113
114        for (unsigned i = 0; i < clause->tuple_count; ++i)
115                bi_print_tuple(&clause->tuples[i], fp);
116
117        if (clause->constant_count) {
118                for (unsigned i = 0; i < clause->constant_count; ++i)
119                        fprintf(fp, "%" PRIx64 " ", clause->constants[i]);
120
121                if (clause->branch_constant)
122                        fprintf(fp, "*");
123
124                fprintf(fp, "\n");
125        }
126
127        fprintf(fp, "\n");
128}
129
130void
131bi_print_block(bi_block *block, FILE *fp)
132{
133        fprintf(fp, "block%u {\n", block->name);
134
135        if (block->scheduled) {
136                bi_foreach_clause_in_block(block, clause)
137                        bi_print_clause(clause, fp);
138        } else {
139                bi_foreach_instr_in_block(block, ins)
140                        bi_print_instr((bi_instr *) ins, fp);
141        }
142
143        fprintf(fp, "}");
144
145        if (block->successors[0]) {
146                fprintf(fp, " -> ");
147
148                bi_foreach_successor((block), succ)
149                        fprintf(fp, "block%u ", succ->name);
150        }
151
152        if (block->predecessors->entries) {
153                fprintf(fp, " from");
154
155                bi_foreach_predecessor(block, pred)
156                        fprintf(fp, " block%u", pred->name);
157        }
158
159        fprintf(fp, "\n\n");
160}
161
162void
163bi_print_shader(bi_context *ctx, FILE *fp)
164{
165        bi_foreach_block(ctx, block)
166                bi_print_block(block, fp);
167}
168