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 meta_opcodes[] = {
124 [OP_META_MOV] = "move ",
125 [OP_META_EXTC] = "extc ",
126 [OP_META_EXTS] = "exts ",
127 [OP_META_COM] = "com ",
130 static const char * const mult2_opcodes[] = {
134 static const char * const opcode_flags[] = {
135 "switched branch/DS",
139 static const char * const opcode_io_flags[] = {
140 "self-modifying code",
146 static const char * const opcode_io_modes[] = {
155 static const char * const opcode_branch_flags[] = {
160 static const char * const opcode_movi_flags[] = {
164 static const char * const opcode_multdiv_flags[] = {
170 static size_t do_snprintf(char *buf, size_t len, bool *first,
171 const char *arg1, const char *arg2)
176 bytes = snprintf(buf, len, "(%s%s", arg1, arg2);
178 bytes = snprintf(buf, len, ", %s%s", arg1, arg2);
185 static const char * const reg_op_token[3] = {
189 static int print_flags(char *buf, size_t len, const struct opcode *op,
190 const char * const *array, size_t array_size,
193 const char *flag_name, *io_mode_name;
194 unsigned int i, io_mode;
195 size_t count = 0, bytes;
197 u32 flags = op->flags;
200 for (i = 0; i < array_size + ARRAY_SIZE(opcode_flags); i++) {
201 if (!(flags & BIT(i)))
204 if (i < ARRAY_SIZE(opcode_flags))
205 flag_name = opcode_flags[i];
207 flag_name = array[i - ARRAY_SIZE(opcode_flags)];
209 bytes = do_snprintf(buf, len, &first, "", flag_name);
216 io_mode = LIGHTREC_FLAGS_GET_IO_MODE(flags);
218 io_mode_name = opcode_io_modes[io_mode - 1];
220 bytes = do_snprintf(buf, len, &first, "", io_mode_name);
227 if (OPT_EARLY_UNLOAD) {
228 reg_op = LIGHTREC_FLAGS_GET_RS(flags);
230 bytes = do_snprintf(buf, len, &first,
231 reg_op_token[reg_op - 1],
232 lightrec_reg_name(op->i.rs));
238 reg_op = LIGHTREC_FLAGS_GET_RT(flags);
240 bytes = do_snprintf(buf, len, &first,
241 reg_op_token[reg_op - 1],
242 lightrec_reg_name(op->i.rt));
248 reg_op = LIGHTREC_FLAGS_GET_RD(flags);
250 bytes = do_snprintf(buf, len, &first,
251 reg_op_token[reg_op - 1],
252 lightrec_reg_name(op->r.rd));
260 count += snprintf(buf, len, ")");
267 static int print_op_special(union code c, char *buf, size_t len,
268 const char * const **flags_ptr, size_t *nb_flags)
274 return snprintf(buf, len, "%s%s,%s,%u",
275 special_opcodes[c.r.op],
276 lightrec_reg_name(c.r.rd),
277 lightrec_reg_name(c.r.rt),
279 case OP_SPECIAL_SLLV:
280 case OP_SPECIAL_SRLV:
281 case OP_SPECIAL_SRAV:
283 case OP_SPECIAL_ADDU:
285 case OP_SPECIAL_SUBU:
291 case OP_SPECIAL_SLTU:
292 return snprintf(buf, len, "%s%s,%s,%s",
293 special_opcodes[c.r.op],
294 lightrec_reg_name(c.r.rd),
295 lightrec_reg_name(c.r.rt),
296 lightrec_reg_name(c.r.rs));
298 *flags_ptr = opcode_branch_flags;
299 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
301 case OP_SPECIAL_MTHI:
302 case OP_SPECIAL_MTLO:
303 return snprintf(buf, len, "%s%s",
304 special_opcodes[c.r.op],
305 lightrec_reg_name(c.r.rs));
306 case OP_SPECIAL_JALR:
307 return snprintf(buf, len, "%s%s,%s",
308 special_opcodes[c.r.op],
309 lightrec_reg_name(c.r.rd),
310 lightrec_reg_name(c.r.rs));
311 case OP_SPECIAL_SYSCALL:
312 case OP_SPECIAL_BREAK:
313 return snprintf(buf, len, "%s", special_opcodes[c.r.op]);
314 case OP_SPECIAL_MFHI:
315 case OP_SPECIAL_MFLO:
316 return snprintf(buf, len, "%s%s",
317 special_opcodes[c.r.op],
318 lightrec_reg_name(c.r.rd));
319 case OP_SPECIAL_MULT:
320 case OP_SPECIAL_MULTU:
322 case OP_SPECIAL_DIVU:
323 *flags_ptr = opcode_multdiv_flags;
324 *nb_flags = ARRAY_SIZE(opcode_multdiv_flags);
325 return snprintf(buf, len, "%s%s,%s,%s,%s",
326 special_opcodes[c.r.op],
327 lightrec_reg_name(get_mult_div_hi(c)),
328 lightrec_reg_name(get_mult_div_lo(c)),
329 lightrec_reg_name(c.r.rs),
330 lightrec_reg_name(c.r.rt));
332 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
336 static int print_op_cp(union code c, char *buf, size_t len, unsigned int cp)
341 return snprintf(buf, len, "%s%s,%u",
342 cp2_basic_opcodes[c.i.rs],
343 lightrec_reg_name(c.i.rt),
346 return snprintf(buf, len, "%s", cp2_opcodes[c.r.op]);
354 return snprintf(buf, len, "%s%s,%u",
356 lightrec_reg_name(c.i.rt),
359 return snprintf(buf, len, "rfe ");
361 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
366 static int print_op(union code c, u32 pc, char *buf, size_t len,
367 const char * const **flags_ptr, size_t *nb_flags,
371 return snprintf(buf, len, "nop ");
375 return print_op_special(c, buf, len, flags_ptr, nb_flags);
377 *flags_ptr = opcode_branch_flags;
378 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
379 return snprintf(buf, len, "%s%s,0x%x",
380 regimm_opcodes[c.i.rt],
381 lightrec_reg_name(c.i.rs),
382 pc + 4 + ((s16)c.i.imm << 2));
385 *flags_ptr = opcode_branch_flags;
386 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
387 return snprintf(buf, len, "%s0x%x",
389 (pc & 0xf0000000) | (c.j.imm << 2));
391 if (c.i.rs == c.i.rt) {
392 *flags_ptr = opcode_branch_flags;
393 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
394 return snprintf(buf, len, "b 0x%x",
395 pc + 4 + ((s16)c.i.imm << 2));
401 *flags_ptr = opcode_branch_flags;
402 *nb_flags = ARRAY_SIZE(opcode_branch_flags);
403 return snprintf(buf, len, "%s%s,%s,0x%x",
405 lightrec_reg_name(c.i.rs),
406 lightrec_reg_name(c.i.rt),
407 pc + 4 + ((s16)c.i.imm << 2));
411 *flags_ptr = opcode_movi_flags;
412 *nb_flags = ARRAY_SIZE(opcode_movi_flags);
418 return snprintf(buf, len, "%s%s,%s,0x%04hx",
420 lightrec_reg_name(c.i.rt),
421 lightrec_reg_name(c.i.rs),
425 *flags_ptr = opcode_movi_flags;
426 *nb_flags = ARRAY_SIZE(opcode_movi_flags);
427 return snprintf(buf, len, "%s%s,0x%04hx",
429 lightrec_reg_name(c.i.rt),
432 return print_op_cp(c, buf, len, 0);
434 return print_op_cp(c, buf, len, 2);
447 *flags_ptr = opcode_io_flags;
448 *nb_flags = ARRAY_SIZE(opcode_io_flags);
450 return snprintf(buf, len, "%s%s,%hd(%s)",
452 lightrec_reg_name(c.i.rt),
454 lightrec_reg_name(c.i.rs));
457 *flags_ptr = opcode_io_flags;
458 *nb_flags = ARRAY_SIZE(opcode_io_flags);
459 return snprintf(buf, len, "%s%s,%hd(%s)",
461 lightrec_reg_name(c.i.rt),
463 lightrec_reg_name(c.i.rs));
465 return snprintf(buf, len, "%s%s,%s",
466 meta_opcodes[c.m.op],
467 lightrec_reg_name(c.m.rd),
468 lightrec_reg_name(c.m.rs));
471 *flags_ptr = opcode_multdiv_flags;
472 *nb_flags = ARRAY_SIZE(opcode_multdiv_flags);
473 return snprintf(buf, len, "%s%s,%s,%s,%u",
474 mult2_opcodes[c.i.op == OP_META_MULTU2],
475 lightrec_reg_name(get_mult_div_hi(c)),
476 lightrec_reg_name(get_mult_div_lo(c)),
477 lightrec_reg_name(c.r.rs), c.r.op);
479 return snprintf(buf, len, "unknown (0x%08x)", c.opcode);
483 void lightrec_print_disassembly(const struct block *block, const u32 *code_ptr)
485 const struct opcode *op;
486 const char * const *flags_ptr;
487 size_t nb_flags, count, count2;
488 char buf[256], buf2[256], buf3[256];
490 u32 pc, branch_pc, code;
493 for (i = 0; i < block->nb_ops; i++) {
494 op = &block->opcode_list[i];
495 branch_pc = get_branch_pc(block, i, 0);
496 pc = block->pc + (i << 2);
497 code = LE32TOH(code_ptr[i]);
499 count = print_op((union code)code, pc, buf, sizeof(buf),
500 &flags_ptr, &nb_flags, &is_io);
505 count2 = print_op(op->c, branch_pc, buf2, sizeof(buf2),
506 &flags_ptr, &nb_flags, &is_io);
508 if (code == op->c.opcode) {
513 print_flags(buf3, sizeof(buf3), op, flags_ptr, nb_flags, is_io);
515 printf("0x%08x (0x%x)\t%s%*c%s%*c%s\n", pc, i << 2,
516 buf, 30 - (int)count, ' ', buf2, 30 - (int)count2, ' ', buf3);