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 * const std_opcodes[] = {
22 [OP_ADDIU] = "addiu ",
24 [OP_SLTIU] = "sltiu ",
45 static const char * const 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 * const 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 * const 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 * const cp2_basic_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 * const cp2_opcodes[] = {
99 [OP_CP2_RTPS] = "rtps ",
100 [OP_CP2_NCLIP] = "nclip ",
102 [OP_CP2_DPCS] = "dpcs ",
103 [OP_CP2_INTPL] = "intpl ",
104 [OP_CP2_MVMVA] = "mvmva ",
105 [OP_CP2_NCDS] = "ncds ",
106 [OP_CP2_CDP] = "cdp ",
107 [OP_CP2_NCDT] = "ncdt ",
108 [OP_CP2_NCCS] = "nccs ",
110 [OP_CP2_NCS] = "ncs ",
111 [OP_CP2_NCT] = "nct ",
112 [OP_CP2_SQR] = "sqr ",
113 [OP_CP2_DCPL] = "dcpl ",
114 [OP_CP2_DPCT] = "dpct ",
115 [OP_CP2_AVSZ3] = "avsz3 ",
116 [OP_CP2_AVSZ4] = "avsz4 ",
117 [OP_CP2_RTPT] = "rtpt ",
118 [OP_CP2_GPF] = "gpf ",
119 [OP_CP2_GPL] = "gpl ",
120 [OP_CP2_NCCT] = "ncct ",
123 static const char * const mult2_opcodes[] = {
127 static const char * const opcode_flags[] = {
128 "switched branch/DS",
132 static const char * const opcode_io_flags[] = {
133 "self-modifying code",
138 static const char * const opcode_io_modes[] = {
147 static const char * const opcode_branch_flags[] = {
152 static const char * const opcode_multdiv_flags[] = {
158 static size_t do_snprintf(char *buf, size_t len, bool *first,
159 const char *arg1, const char *arg2)
164 bytes = snprintf(buf, len, "(%s%s", arg1, arg2);
166 bytes = snprintf(buf, len, ", %s%s", arg1, arg2);
173 static const char * const reg_op_token[3] = {
177 static int print_flags(char *buf, size_t len, const struct opcode *op,
178 const char * const *array, size_t array_size,
181 const char *flag_name, *io_mode_name;
182 unsigned int i, io_mode;
183 size_t count = 0, bytes;
185 u32 flags = op->flags;
188 for (i = 0; i < array_size + ARRAY_SIZE(opcode_flags); i++) {
189 if (!(flags & BIT(i)))
192 if (i < ARRAY_SIZE(opcode_flags))
193 flag_name = opcode_flags[i];
195 flag_name = array[i - ARRAY_SIZE(opcode_flags)];
197 bytes = do_snprintf(buf, len, &first, "", flag_name);
204 io_mode = LIGHTREC_FLAGS_GET_IO_MODE(flags);
206 io_mode_name = opcode_io_modes[io_mode - 1];
208 bytes = do_snprintf(buf, len, &first, "", io_mode_name);
215 if (OPT_EARLY_UNLOAD) {
216 reg_op = LIGHTREC_FLAGS_GET_RS(flags);
218 bytes = do_snprintf(buf, len, &first,
219 reg_op_token[reg_op - 1],
220 lightrec_reg_name(op->i.rs));
226 reg_op = LIGHTREC_FLAGS_GET_RT(flags);
228 bytes = do_snprintf(buf, len, &first,
229 reg_op_token[reg_op - 1],
230 lightrec_reg_name(op->i.rt));
236 reg_op = LIGHTREC_FLAGS_GET_RD(flags);
238 bytes = do_snprintf(buf, len, &first,
239 reg_op_token[reg_op - 1],
240 lightrec_reg_name(op->r.rd));
248 count += snprintf(buf, len, ")");
255 static int print_op_special(union code c, char *buf, size_t len,
256 const char * const **flags_ptr, size_t *nb_flags)
262 return snprintf(buf, len, "%s%s,%s,%u",
263 special_opcodes[c.r.op],
264 lightrec_reg_name(c.r.rd),
265 lightrec_reg_name(c.r.rt),
267 case OP_SPECIAL_SLLV:
268 case OP_SPECIAL_SRLV:
269 case OP_SPECIAL_SRAV:
271 case OP_SPECIAL_ADDU:
273 case OP_SPECIAL_SUBU:
279 case OP_SPECIAL_SLTU:
280 return snprintf(buf, len, "%s%s,%s,%s",
281 special_opcodes[c.r.op],
282 lightrec_reg_name(c.r.rd),
283 lightrec_reg_name(c.r.rt),
284 lightrec_reg_name(c.r.rs));
286 *flags_ptr = opcode_branch_flags;
287 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
289 case OP_SPECIAL_MTHI:
290 case OP_SPECIAL_MTLO:
291 return snprintf(buf, len, "%s%s",
292 special_opcodes[c.r.op],
293 lightrec_reg_name(c.r.rs));
294 case OP_SPECIAL_JALR:
295 return snprintf(buf, len, "%s%s,%s",
296 special_opcodes[c.r.op],
297 lightrec_reg_name(c.r.rd),
298 lightrec_reg_name(c.r.rs));
299 case OP_SPECIAL_SYSCALL:
300 case OP_SPECIAL_BREAK:
301 return snprintf(buf, len, "%s", special_opcodes[c.r.op]);
302 case OP_SPECIAL_MFHI:
303 case OP_SPECIAL_MFLO:
304 return snprintf(buf, len, "%s%s",
305 special_opcodes[c.r.op],
306 lightrec_reg_name(c.r.rd));
307 case OP_SPECIAL_MULT:
308 case OP_SPECIAL_MULTU:
310 case OP_SPECIAL_DIVU:
311 *flags_ptr = opcode_multdiv_flags;
312 *nb_flags = ARRAY_SIZE(opcode_multdiv_flags);
313 return snprintf(buf, len, "%s%s,%s,%s,%s",
314 special_opcodes[c.r.op],
315 lightrec_reg_name(get_mult_div_hi(c)),
316 lightrec_reg_name(get_mult_div_lo(c)),
317 lightrec_reg_name(c.r.rs),
318 lightrec_reg_name(c.r.rt));
320 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
324 static int print_op_cp(union code c, char *buf, size_t len, unsigned int cp)
329 return snprintf(buf, len, "%s%s,%u",
330 cp2_basic_opcodes[c.i.rs],
331 lightrec_reg_name(c.i.rt),
334 return snprintf(buf, len, "%s", cp2_opcodes[c.r.op]);
342 return snprintf(buf, len, "%s%s,%u",
344 lightrec_reg_name(c.i.rt),
347 return snprintf(buf, len, "rfe ");
349 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
354 static int print_op(union code c, u32 pc, char *buf, size_t len,
355 const char * const **flags_ptr, size_t *nb_flags,
359 return snprintf(buf, len, "nop ");
363 return print_op_special(c, buf, len, flags_ptr, nb_flags);
365 *flags_ptr = opcode_branch_flags;
366 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
367 return snprintf(buf, len, "%s%s,0x%x",
368 regimm_opcodes[c.i.rt],
369 lightrec_reg_name(c.i.rs),
370 pc + 4 + ((s16)c.i.imm << 2));
373 *flags_ptr = opcode_branch_flags;
374 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
375 return snprintf(buf, len, "%s0x%x",
377 (pc & 0xf0000000) | (c.j.imm << 2));
379 if (c.i.rs == c.i.rt) {
380 *flags_ptr = opcode_branch_flags;
381 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
382 return snprintf(buf, len, "b 0x%x",
383 pc + 4 + ((s16)c.i.imm << 2));
389 *flags_ptr = opcode_branch_flags;
390 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
391 return snprintf(buf, len, "%s%s,%s,0x%x",
393 lightrec_reg_name(c.i.rs),
394 lightrec_reg_name(c.i.rt),
395 pc + 4 + ((s16)c.i.imm << 2));
403 return snprintf(buf, len, "%s%s,%s,0x%04hx",
405 lightrec_reg_name(c.i.rt),
406 lightrec_reg_name(c.i.rs),
410 return snprintf(buf, len, "%s%s,0x%04hx",
412 lightrec_reg_name(c.i.rt),
415 return print_op_cp(c, buf, len, 0);
417 return print_op_cp(c, buf, len, 2);
430 *flags_ptr = opcode_io_flags;
431 *nb_flags = ARRAY_SIZE(opcode_io_flags);
433 return snprintf(buf, len, "%s%s,%hd(%s)",
435 lightrec_reg_name(c.i.rt),
437 lightrec_reg_name(c.i.rs));
440 *flags_ptr = opcode_io_flags;
441 *nb_flags = ARRAY_SIZE(opcode_io_flags);
442 return snprintf(buf, len, "%s%s,%hd(%s)",
444 lightrec_reg_name(c.i.rt),
446 lightrec_reg_name(c.i.rs));
448 return snprintf(buf, len, "move %s,%s",
449 lightrec_reg_name(c.r.rd),
450 lightrec_reg_name(c.r.rs));
452 return snprintf(buf, len, "extc %s,%s",
453 lightrec_reg_name(c.i.rt),
454 lightrec_reg_name(c.i.rs));
456 return snprintf(buf, len, "exts %s,%s",
457 lightrec_reg_name(c.i.rt),
458 lightrec_reg_name(c.i.rs));
461 *flags_ptr = opcode_multdiv_flags;
462 *nb_flags = ARRAY_SIZE(opcode_multdiv_flags);
463 return snprintf(buf, len, "%s%s,%s,%s,%u",
464 mult2_opcodes[c.i.op == OP_META_MULTU2],
465 lightrec_reg_name(get_mult_div_hi(c)),
466 lightrec_reg_name(get_mult_div_lo(c)),
467 lightrec_reg_name(c.r.rs), c.r.op);
469 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
473 void lightrec_print_disassembly(const struct block *block, const u32 *code_ptr)
475 const struct opcode *op;
476 const char * const *flags_ptr;
477 size_t nb_flags, count, count2;
478 char buf[256], buf2[256], buf3[256];
480 u32 pc, branch_pc, code;
483 for (i = 0; i < block->nb_ops; i++) {
484 op = &block->opcode_list[i];
485 branch_pc = get_branch_pc(block, i, 0);
486 pc = block->pc + (i << 2);
487 code = LE32TOH(code_ptr[i]);
489 count = print_op((union code)code, pc, buf, sizeof(buf),
490 &flags_ptr, &nb_flags, &is_io);
495 count2 = print_op(op->c, branch_pc, buf2, sizeof(buf2),
496 &flags_ptr, &nb_flags, &is_io);
498 if (code == op->c.opcode) {
503 print_flags(buf3, sizeof(buf3), op, flags_ptr, nb_flags, is_io);
505 printf("0x%08x (0x%x)\t%s%*c%s%*c%s\n", pc, i << 2,
506 buf, 30 - (int)count, ' ', buf2, 30 - (int)count2, ' ', buf3);