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[] = {
103 static const char *opcode_io_flags[] = {
104 "self-modifying code",
109 static const char *opcode_io_modes[] = {
117 static const char *opcode_branch_flags[] = {
122 static const char *opcode_multdiv_flags[] = {
128 static size_t do_snprintf(char *buf, size_t len, bool *first,
129 const char *arg1, const char *arg2)
134 bytes = snprintf(buf, len, "(%s%s", arg1, arg2);
136 bytes = snprintf(buf, len, ", %s%s", arg1, arg2);
143 static const char * const reg_op_token[3] = {
147 static int print_flags(char *buf, size_t len, const struct opcode *op,
148 const char **array, size_t array_size,
151 const char *flag_name, *io_mode_name;
152 unsigned int i, io_mode;
153 size_t count = 0, bytes;
155 u32 flags = op->flags;
158 for (i = 0; i < array_size + ARRAY_SIZE(opcode_flags); i++) {
159 if (!(flags & BIT(i)))
162 if (i < ARRAY_SIZE(opcode_flags))
163 flag_name = opcode_flags[i];
165 flag_name = array[i - ARRAY_SIZE(opcode_flags)];
167 bytes = do_snprintf(buf, len, &first, "", flag_name);
174 io_mode = LIGHTREC_FLAGS_GET_IO_MODE(flags);
176 io_mode_name = opcode_io_modes[io_mode - 1];
178 bytes = do_snprintf(buf, len, &first, "", io_mode_name);
185 if (OPT_EARLY_UNLOAD) {
186 reg_op = LIGHTREC_FLAGS_GET_RS(flags);
188 bytes = do_snprintf(buf, len, &first,
189 reg_op_token[reg_op - 1],
190 lightrec_reg_name(op->i.rs));
196 reg_op = LIGHTREC_FLAGS_GET_RT(flags);
198 bytes = do_snprintf(buf, len, &first,
199 reg_op_token[reg_op - 1],
200 lightrec_reg_name(op->i.rt));
206 reg_op = LIGHTREC_FLAGS_GET_RD(flags);
208 bytes = do_snprintf(buf, len, &first,
209 reg_op_token[reg_op - 1],
210 lightrec_reg_name(op->r.rd));
218 count += snprintf(buf, len, ")");
225 static int print_op_special(union code c, char *buf, size_t len,
226 const char ***flags_ptr, size_t *nb_flags)
232 return snprintf(buf, len, "%s%s,%s,%u",
233 special_opcodes[c.r.op],
234 lightrec_reg_name(c.r.rd),
235 lightrec_reg_name(c.r.rt),
237 case OP_SPECIAL_SLLV:
238 case OP_SPECIAL_SRLV:
239 case OP_SPECIAL_SRAV:
241 case OP_SPECIAL_ADDU:
243 case OP_SPECIAL_SUBU:
249 case OP_SPECIAL_SLTU:
250 return snprintf(buf, len, "%s%s,%s,%s",
251 special_opcodes[c.r.op],
252 lightrec_reg_name(c.r.rd),
253 lightrec_reg_name(c.r.rt),
254 lightrec_reg_name(c.r.rs));
256 *flags_ptr = opcode_branch_flags;
257 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
259 case OP_SPECIAL_MTHI:
260 case OP_SPECIAL_MTLO:
261 return snprintf(buf, len, "%s%s",
262 special_opcodes[c.r.op],
263 lightrec_reg_name(c.r.rs));
264 case OP_SPECIAL_JALR:
265 return snprintf(buf, len, "%s%s,%s",
266 special_opcodes[c.r.op],
267 lightrec_reg_name(c.r.rd),
268 lightrec_reg_name(c.r.rt));
269 case OP_SPECIAL_SYSCALL:
270 case OP_SPECIAL_BREAK:
271 return snprintf(buf, len, "%s", special_opcodes[c.r.op]);
272 case OP_SPECIAL_MFHI:
273 case OP_SPECIAL_MFLO:
274 return snprintf(buf, len, "%s%s",
275 special_opcodes[c.r.op],
276 lightrec_reg_name(c.r.rd));
277 case OP_SPECIAL_MULT:
278 case OP_SPECIAL_MULTU:
280 case OP_SPECIAL_DIVU:
281 *flags_ptr = opcode_multdiv_flags;
282 *nb_flags = ARRAY_SIZE(opcode_multdiv_flags);
283 return snprintf(buf, len, "%s%s,%s,%s,%s",
284 special_opcodes[c.r.op],
285 lightrec_reg_name(get_mult_div_hi(c)),
286 lightrec_reg_name(get_mult_div_lo(c)),
287 lightrec_reg_name(c.r.rs),
288 lightrec_reg_name(c.r.rt));
290 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
294 static int print_op_cp(union code c, char *buf, size_t len, unsigned int cp)
302 return snprintf(buf, len, "%s%s,%u",
304 lightrec_reg_name(c.i.rt),
307 return snprintf(buf, len, "cp2 (0x%08x)", c.opcode);
315 return snprintf(buf, len, "%s%s,%u",
317 lightrec_reg_name(c.i.rt),
320 return snprintf(buf, len, "rfe ");
322 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
327 static int print_op(union code c, u32 pc, char *buf, size_t len,
328 const char ***flags_ptr, size_t *nb_flags,
332 return snprintf(buf, len, "nop ");
336 return print_op_special(c, buf, len, flags_ptr, nb_flags);
338 *flags_ptr = opcode_branch_flags;
339 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
340 return snprintf(buf, len, "%s%s,0x%x",
341 regimm_opcodes[c.i.rt],
342 lightrec_reg_name(c.i.rs),
343 pc + 4 + ((s16)c.i.imm << 2));
346 *flags_ptr = opcode_branch_flags;
347 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
348 return snprintf(buf, len, "%s0x%x",
350 (pc & 0xf0000000) | (c.j.imm << 2));
352 if (c.i.rs == c.i.rt) {
353 *flags_ptr = opcode_branch_flags;
354 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
355 return snprintf(buf, len, "b 0x%x",
356 pc + 4 + ((s16)c.i.imm << 2));
362 *flags_ptr = opcode_branch_flags;
363 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
364 return snprintf(buf, len, "%s%s,%s,0x%x",
366 lightrec_reg_name(c.i.rs),
367 lightrec_reg_name(c.i.rt),
368 pc + 4 + ((s16)c.i.imm << 2));
376 return snprintf(buf, len, "%s%s,%s,0x%04hx",
378 lightrec_reg_name(c.i.rt),
379 lightrec_reg_name(c.i.rs),
383 return snprintf(buf, len, "%s%s,0x%04hx",
385 lightrec_reg_name(c.i.rt),
388 return print_op_cp(c, buf, len, 0);
390 return print_op_cp(c, buf, len, 2);
403 *flags_ptr = opcode_io_flags;
404 *nb_flags = ARRAY_SIZE(opcode_io_flags);
406 return snprintf(buf, len, "%s%s,%hd(%s)",
408 lightrec_reg_name(c.i.rt),
410 lightrec_reg_name(c.i.rs));
413 *flags_ptr = opcode_io_flags;
414 *nb_flags = ARRAY_SIZE(opcode_io_flags);
415 return snprintf(buf, len, "%s%s,%hd(%s)",
417 lightrec_reg_name(c.i.rt),
419 lightrec_reg_name(c.i.rs));
421 return snprintf(buf, len, "move %s,%s",
422 lightrec_reg_name(c.r.rd),
423 lightrec_reg_name(c.r.rs));
425 return snprintf(buf, len, "extc %s,%s",
426 lightrec_reg_name(c.i.rt),
427 lightrec_reg_name(c.i.rs));
429 return snprintf(buf, len, "exts %s,%s",
430 lightrec_reg_name(c.i.rt),
431 lightrec_reg_name(c.i.rs));
433 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
437 void lightrec_print_disassembly(const struct block *block, const u32 *code_ptr)
439 const struct opcode *op;
440 const char **flags_ptr;
441 size_t nb_flags, count, count2;
442 char buf[256], buf2[256], buf3[256];
444 u32 pc, branch_pc, code;
447 for (i = 0; i < block->nb_ops; i++) {
448 op = &block->opcode_list[i];
449 branch_pc = get_branch_pc(block, i, 0);
450 pc = block->pc + (i << 2);
451 code = LE32TOH(code_ptr[i]);
453 count = print_op((union code)code, pc, buf, sizeof(buf),
454 &flags_ptr, &nb_flags, &is_io);
459 count2 = print_op(op->c, branch_pc, buf2, sizeof(buf2),
460 &flags_ptr, &nb_flags, &is_io);
462 if (code == op->c.opcode) {
467 print_flags(buf3, sizeof(buf3), op, flags_ptr, nb_flags, is_io);
469 printf("0x%08x (0x%x)\t%s%*c%s%*c%s\n", pc, i << 2,
470 buf, 30 - (int)count, ' ', buf2, 30 - (int)count2, ' ', buf3);