1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2014-2021 Paul Cercueil <paul@crapouillou.net>
11 #include "lightrec-private.h"
14 static const char *std_opcodes[] = {
22 [OP_ADDIU] = "addiu ",
24 [OP_SLTIU] = "sltiu ",
45 static const char *special_opcodes[] = {
46 [OP_SPECIAL_SLL] = "sll ",
47 [OP_SPECIAL_SRL] = "srl ",
48 [OP_SPECIAL_SRA] = "sra ",
49 [OP_SPECIAL_SLLV] = "sllv ",
50 [OP_SPECIAL_SRLV] = "srlv ",
51 [OP_SPECIAL_SRAV] = "srav ",
52 [OP_SPECIAL_JR] = "jr ",
53 [OP_SPECIAL_JALR] = "jalr ",
54 [OP_SPECIAL_SYSCALL] = "syscall ",
55 [OP_SPECIAL_BREAK] = "break ",
56 [OP_SPECIAL_MFHI] = "mfhi ",
57 [OP_SPECIAL_MTHI] = "mthi ",
58 [OP_SPECIAL_MFLO] = "mflo ",
59 [OP_SPECIAL_MTLO] = "mtlo ",
60 [OP_SPECIAL_MULT] = "mult ",
61 [OP_SPECIAL_MULTU] = "multu ",
62 [OP_SPECIAL_DIV] = "div ",
63 [OP_SPECIAL_DIVU] = "divu ",
64 [OP_SPECIAL_ADD] = "add ",
65 [OP_SPECIAL_ADDU] = "addu ",
66 [OP_SPECIAL_SUB] = "sub ",
67 [OP_SPECIAL_SUBU] = "subu ",
68 [OP_SPECIAL_AND] = "and ",
69 [OP_SPECIAL_OR] = "or ",
70 [OP_SPECIAL_XOR] = "xor ",
71 [OP_SPECIAL_NOR] = "nor ",
72 [OP_SPECIAL_SLT] = "slt ",
73 [OP_SPECIAL_SLTU] = "sltu ",
76 static const char *regimm_opcodes[] = {
77 [OP_REGIMM_BLTZ] = "bltz ",
78 [OP_REGIMM_BGEZ] = "bgez ",
79 [OP_REGIMM_BLTZAL] = "bltzal ",
80 [OP_REGIMM_BGEZAL] = "bgezal ",
83 static const char *cp0_opcodes[] = {
84 [OP_CP0_MFC0] = "mfc0 ",
85 [OP_CP0_CFC0] = "cfc0 ",
86 [OP_CP0_MTC0] = "mtc0 ",
87 [OP_CP0_CTC0] = "ctc0 ",
91 static const char *cp2_opcodes[] = {
92 [OP_CP2_BASIC_MFC2] = "mfc2 ",
93 [OP_CP2_BASIC_CFC2] = "cfc2 ",
94 [OP_CP2_BASIC_MTC2] = "mtc2 ",
95 [OP_CP2_BASIC_CTC2] = "ctc2 ",
98 static const char *opcode_flags[] = {
106 static const char *opcode_io_flags[] = {
107 "self-modifying code",
112 static const char *opcode_io_modes[] = {
120 static const char *opcode_branch_flags[] = {
125 static const char *opcode_multdiv_flags[] = {
131 static int print_flags(char *buf, size_t len, u16 flags,
132 const char **array, size_t array_size,
135 const char *flag_name, *io_mode_name;
136 unsigned int i, io_mode;
137 size_t count = 0, bytes;
140 for (i = 0; i < array_size + ARRAY_SIZE(opcode_flags); i++) {
141 if (!(flags & BIT(i)))
144 if (i < ARRAY_SIZE(opcode_flags))
145 flag_name = opcode_flags[i];
147 flag_name = array[i - ARRAY_SIZE(opcode_flags)];
150 bytes = snprintf(buf, len, "(%s", flag_name);
152 bytes = snprintf(buf, len, ", %s", flag_name);
161 io_mode = LIGHTREC_FLAGS_GET_IO_MODE(flags);
163 io_mode_name = opcode_io_modes[io_mode - 1];
166 bytes = snprintf(buf, len, "(%s", io_mode_name);
168 bytes = snprintf(buf, len, ", %s", io_mode_name);
178 count += snprintf(buf, len, ")");
185 static int print_op_special(union code c, char *buf, size_t len,
186 const char ***flags_ptr, size_t *nb_flags)
192 return snprintf(buf, len, "%s%s,%s,%u",
193 special_opcodes[c.r.op],
194 lightrec_reg_name(c.r.rd),
195 lightrec_reg_name(c.r.rt),
197 case OP_SPECIAL_SLLV:
198 case OP_SPECIAL_SRLV:
199 case OP_SPECIAL_SRAV:
201 case OP_SPECIAL_ADDU:
203 case OP_SPECIAL_SUBU:
209 case OP_SPECIAL_SLTU:
210 return snprintf(buf, len, "%s%s,%s,%s",
211 special_opcodes[c.r.op],
212 lightrec_reg_name(c.r.rd),
213 lightrec_reg_name(c.r.rt),
214 lightrec_reg_name(c.r.rs));
216 case OP_SPECIAL_MTHI:
217 case OP_SPECIAL_MTLO:
218 return snprintf(buf, len, "%s%s",
219 special_opcodes[c.r.op],
220 lightrec_reg_name(c.r.rs));
221 case OP_SPECIAL_JALR:
222 return snprintf(buf, len, "%s%s,%s",
223 special_opcodes[c.r.op],
224 lightrec_reg_name(c.r.rd),
225 lightrec_reg_name(c.r.rt));
226 case OP_SPECIAL_SYSCALL:
227 case OP_SPECIAL_BREAK:
228 return snprintf(buf, len, "%s", special_opcodes[c.r.op]);
229 case OP_SPECIAL_MFHI:
230 case OP_SPECIAL_MFLO:
231 return snprintf(buf, len, "%s%s",
232 special_opcodes[c.r.op],
233 lightrec_reg_name(c.r.rd));
234 case OP_SPECIAL_MULT:
235 case OP_SPECIAL_MULTU:
237 case OP_SPECIAL_DIVU:
238 *flags_ptr = opcode_multdiv_flags;
239 *nb_flags = ARRAY_SIZE(opcode_multdiv_flags);
240 return snprintf(buf, len, "%s%s,%s,%s,%s",
241 special_opcodes[c.r.op],
242 lightrec_reg_name(get_mult_div_hi(c)),
243 lightrec_reg_name(get_mult_div_lo(c)),
244 lightrec_reg_name(c.r.rs),
245 lightrec_reg_name(c.r.rt));
247 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
251 static int print_op_cp(union code c, char *buf, size_t len, unsigned int cp)
259 return snprintf(buf, len, "%s%s,%u",
261 lightrec_reg_name(c.i.rt),
264 return snprintf(buf, len, "cp2 (0x%08x)", c.opcode);
272 return snprintf(buf, len, "%s%s,%u",
274 lightrec_reg_name(c.i.rt),
277 return snprintf(buf, len, "rfe ");
279 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
284 static int print_op(union code c, u32 pc, char *buf, size_t len,
285 const char ***flags_ptr, size_t *nb_flags,
289 return snprintf(buf, len, "nop ");
293 return print_op_special(c, buf, len, flags_ptr, nb_flags);
295 *flags_ptr = opcode_branch_flags;
296 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
297 return snprintf(buf, len, "%s%s,0x%x",
298 regimm_opcodes[c.i.rt],
299 lightrec_reg_name(c.i.rs),
300 pc + 4 + ((s16)c.i.imm << 2));
303 return snprintf(buf, len, "%s0x%x",
305 (pc & 0xf0000000) | (c.j.imm << 2));
310 *flags_ptr = opcode_branch_flags;
311 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
312 return snprintf(buf, len, "%s%s,%s,0x%x",
314 lightrec_reg_name(c.i.rs),
315 lightrec_reg_name(c.i.rt),
316 pc + 4 + ((s16)c.i.imm << 2));
324 return snprintf(buf, len, "%s%s,%s,0x%04hx",
326 lightrec_reg_name(c.i.rt),
327 lightrec_reg_name(c.i.rs),
331 return snprintf(buf, len, "%s%s,0x%04hx",
333 lightrec_reg_name(c.i.rt),
336 return print_op_cp(c, buf, len, 0);
338 return print_op_cp(c, buf, len, 2);
351 *flags_ptr = opcode_io_flags;
352 *nb_flags = ARRAY_SIZE(opcode_io_flags);
354 return snprintf(buf, len, "%s%s,%hd(%s)",
356 lightrec_reg_name(c.i.rt),
358 lightrec_reg_name(c.i.rs));
361 *flags_ptr = opcode_io_flags;
362 *nb_flags = ARRAY_SIZE(opcode_io_flags);
363 return snprintf(buf, len, "%s%s,%hd(%s)",
365 lightrec_reg_name(c.i.rt),
367 lightrec_reg_name(c.i.rs));
369 return snprintf(buf, len, "move %s,%s",
370 lightrec_reg_name(c.r.rd),
371 lightrec_reg_name(c.r.rs));
373 return snprintf(buf, len, "extc %s,%s",
374 lightrec_reg_name(c.i.rt),
375 lightrec_reg_name(c.i.rs));
377 return snprintf(buf, len, "exts %s,%s",
378 lightrec_reg_name(c.i.rt),
379 lightrec_reg_name(c.i.rs));
381 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
385 void lightrec_print_disassembly(const struct block *block, const u32 *code)
387 const struct opcode *op;
388 const char **flags_ptr;
389 size_t nb_flags, count, count2;
390 char buf[256], buf2[256], buf3[256];
395 for (i = 0; i < block->nb_ops; i++) {
396 op = &block->opcode_list[i];
397 branch_pc = get_branch_pc(block, i, 0);
398 pc = block->pc + (i << 2);
400 count = print_op((union code)code[i], pc, buf, sizeof(buf),
401 &flags_ptr, &nb_flags, &is_io);
406 count2 = print_op(op->c, branch_pc, buf2, sizeof(buf2),
407 &flags_ptr, &nb_flags, &is_io);
409 if (code[i] == op->c.opcode) {
414 print_flags(buf3, sizeof(buf3), op->flags, flags_ptr, nb_flags,
417 printf("0x%08x (0x%x)\t%s%*c%s%*c%s\n", pc, i << 2,
418 buf, 30 - (int)count, ' ', buf2, 30 - (int)count2, ' ', buf3);