1 /*
2  * Copyright © 2022 Imagination Technologies Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * 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 THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include <inttypes.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 
28 #include "rogue_dump.h"
29 #include "rogue_shader.h"
30 #include "rogue_util.h"
31 #include "util/bitscan.h"
32 
33 /**
34  * \file rogue_dump.c
35  *
36  * \brief Contains functions to dump Rogue data structures into a textual
37  * format.
38  */
39 
40 static const char *const rogue_operand_string[ROGUE_OPERAND_TYPE_COUNT] = {
41    [ROGUE_OPERAND_TYPE_REG_TEMP] = "r",
42    [ROGUE_OPERAND_TYPE_REG_COEFF] = "cf",
43    [ROGUE_OPERAND_TYPE_REG_CONST] = "c",
44    [ROGUE_OPERAND_TYPE_REG_SHARED] = "sh",
45    [ROGUE_OPERAND_TYPE_REG_PIXEL_OUT] = "po",
46    [ROGUE_OPERAND_TYPE_REG_VERTEX_IN] = "vi",
47    [ROGUE_OPERAND_TYPE_REG_INTERNAL] = "i",
48    [ROGUE_OPERAND_TYPE_IMMEDIATE] = "#",
49    [ROGUE_OPERAND_TYPE_DRC] = "drc",
50    [ROGUE_OPERAND_TYPE_VREG] = "V",
51 };
52 
53 static const char *const rogue_opcode_string[ROGUE_OP_COUNT] = {
54    [ROGUE_OP_NOP] = "nop",
55    [ROGUE_OP_END_FRAG] = "end.frag",
56    [ROGUE_OP_END_VERT] = "end.vert",
57    [ROGUE_OP_WDF] = "wdf",
58    [ROGUE_OP_PIX_ITER_W] = "pixiter.w",
59    [ROGUE_OP_MAX] = "max",
60    [ROGUE_OP_MIN] = "min",
61    [ROGUE_OP_PACK_U8888] = "pack.u8888",
62    [ROGUE_OP_MOV] = "mov",
63    [ROGUE_OP_MOV_IMM] = "mov.imm",
64    [ROGUE_OP_FMA] = "fma",
65    [ROGUE_OP_MUL] = "mul",
66    [ROGUE_OP_VTXOUT] = "vtxout",
67 };
68 
69 static const char *const rogue_instr_flag_string[ROGUE_INSTR_FLAG_COUNT] = {
70    [ROGUE_INSTR_FLAG_SAT] = "sat",
71    [ROGUE_INSTR_FLAG_LP] = "lp",
72    [ROGUE_INSTR_FLAG_OLCHK] = "olchk",
73 };
74 
75 static const char rogue_vector_string[4] = {
76    'x',
77    'y',
78    'z',
79    'w',
80 };
81 
82 /**
83  * \brief Dumps an operand as text to a file pointer.
84  *
85  * \param[in] operand The operand.
86  * \param[in] fp The file pointer.
87  * \return true if successful, otherwise false.
88  */
rogue_dump_operand(const struct rogue_operand * operand,FILE * fp)89 bool rogue_dump_operand(const struct rogue_operand *operand, FILE *fp)
90 {
91    ASSERT_OPERAND_RANGE(operand->type);
92 
93    fprintf(fp, "%s", rogue_operand_string[operand->type]);
94 
95    if (operand->type == ROGUE_OPERAND_TYPE_IMMEDIATE)
96       fprintf(fp, "%" PRIu64, operand->immediate.value);
97    else if (operand->type == ROGUE_OPERAND_TYPE_DRC)
98       fprintf(fp, "%zu", operand->drc.number);
99    else if (rogue_check_bitset(rogue_onehot(operand->type), ROGUE_MASK_ANY_REG))
100       fprintf(fp, "%zu", operand->reg.number);
101    else if (operand->type == ROGUE_OPERAND_TYPE_VREG) {
102       fprintf(fp, "%zu", operand->vreg.number);
103       if (operand->vreg.is_vector)
104          fprintf(fp, ".%c", rogue_vector_string[operand->vreg.component]);
105    }
106 
107    return true;
108 }
109 
110 /**
111  * \brief Dumps an instruction as text to a file pointer.
112  *
113  * \param[in] instr The instruction.
114  * \param[in] fp The file pointer.
115  * \return true if successful, otherwise false.
116  */
rogue_dump_instr(const struct rogue_instr * instr,FILE * fp)117 bool rogue_dump_instr(const struct rogue_instr *instr, FILE *fp)
118 {
119    uint64_t flags = 0U;
120 
121    ASSERT_OPCODE_RANGE(instr->opcode);
122 
123    flags = instr->flags;
124 
125    fprintf(fp, "%s", rogue_opcode_string[instr->opcode]);
126 
127    /* Iterate over each flag bit and print its string form. */
128    while (flags) {
129       uint64_t flag = u_bit_scan64(&flags);
130       ASSERT_INSTR_FLAG_RANGE(flag);
131       fprintf(fp, ".%s", rogue_instr_flag_string[flag]);
132    }
133 
134    if (instr->num_operands)
135       fprintf(fp, " ");
136 
137    /* Dump each operand. */
138    for (size_t u = 0U; u < instr->num_operands; ++u) {
139       CHECKF(rogue_dump_operand(&instr->operands[u], fp),
140              "Failed to dump operand.");
141       if (u < (instr->num_operands - 1))
142          fprintf(fp, ", ");
143    }
144 
145    fprintf(fp, ";");
146 
147    return true;
148 }
149 
150 /**
151  * \brief Dumps a shader as text to a file pointer.
152  *
153  * \param[in] shader The shader.
154  * \param[in] fp The file pointer.
155  * \return true if successful, otherwise false.
156  */
rogue_dump_shader(const struct rogue_shader * shader,FILE * fp)157 bool rogue_dump_shader(const struct rogue_shader *shader, FILE *fp)
158 {
159    /* Dump the shader stage. */
160    fprintf(fp, "# %s shader\n", _mesa_shader_stage_to_string(shader->stage));
161 
162    /* Dump each instruction. */
163    foreach_instr (instr, &shader->instr_list) {
164       CHECKF(rogue_dump_instr(instr, fp), "Failed to dump instruction.");
165       fprintf(fp, "\n");
166    }
167    fprintf(fp, "\n");
168 
169    return true;
170 }
171