| 1 | // |
| 2 | // cstool_sh.c |
| 3 | // |
| 4 | // Yoshinori Sato 2022/09/07 |
| 5 | // |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <capstone/capstone.h> |
| 9 | #include "cstool.h" |
| 10 | |
| 11 | static const char* s_addressing_modes[] = { |
| 12 | "<invalid mode>", |
| 13 | |
| 14 | "Register Direct", /// Rn |
| 15 | |
| 16 | "Register Indirect", /// @Rn |
| 17 | "Register Indirect with Postincrement", /// @Rn+ |
| 18 | "Register Indirect with Predecrement", /// @-Rn |
| 19 | "Register Indirect with Displacement", /// @(disp,Rn) |
| 20 | |
| 21 | "Indexed register indirect", /// @(R0, Rn) |
| 22 | "GBR indirect with displacement", /// @(disp,GBR) |
| 23 | "Indexed GBR indirect", /// @(R0, GBR) |
| 24 | |
| 25 | "PC-relative with Displacement", /// @(disp, PC) |
| 26 | |
| 27 | "Immediate value", |
| 28 | }; |
| 29 | |
| 30 | static void print_read_write_regs(cs_detail* detail, csh handle) |
| 31 | { |
| 32 | int i; |
| 33 | |
| 34 | for (i = 0; i < detail->regs_read_count; ++i) { |
| 35 | uint16_t reg_id = detail->regs_read[i]; |
| 36 | const char* reg_name = cs_reg_name(handle, reg_id); |
| 37 | printf("\treading from reg: %s\n", reg_name); |
| 38 | } |
| 39 | |
| 40 | for (i = 0; i < detail->regs_write_count; ++i) { |
| 41 | uint16_t reg_id = detail->regs_write[i]; |
| 42 | const char* reg_name = cs_reg_name(handle, reg_id); |
| 43 | printf("\twriting to reg: %s\n", reg_name); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void print_insn_detail_sh(csh handle, cs_insn *ins) |
| 48 | { |
| 49 | cs_detail* detail; |
| 50 | int i; |
| 51 | |
| 52 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 53 | if (ins->detail == NULL) |
| 54 | return; |
| 55 | |
| 56 | detail = ins->detail; |
| 57 | |
| 58 | print_read_write_regs(detail, handle); |
| 59 | |
| 60 | printf("\tgroups_count: %u\n", detail->groups_count); |
| 61 | |
| 62 | for (i = 0; i < detail->sh.op_count; i++) { |
| 63 | cs_sh_op* op = &(detail->sh.operands[i]); |
| 64 | |
| 65 | switch((int)op->type) { |
| 66 | default: |
| 67 | break; |
| 68 | case SH_OP_REG: |
| 69 | printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg)); |
| 70 | break; |
| 71 | case SH_OP_IMM: |
| 72 | printf("\t\toperands[%u].type: IMM = 0x%x\n", i, (int)op->imm); |
| 73 | break; |
| 74 | case SH_OP_MEM: |
| 75 | printf("\t\toperands[%u].type: MEM\n", i); |
| 76 | if (op->mem.reg != SH_REG_INVALID) |
| 77 | printf("\t\t\toperands[%u].mem.reg: REG = %s\n", |
| 78 | i, cs_reg_name(handle, op->mem.reg)); |
| 79 | if (op->mem.disp != 0) |
| 80 | printf("\t\t\toperands[%u].mem.disp: 0x%x\n", |
| 81 | i, op->mem.disp); |
| 82 | printf("\t\taddress mode: %s\n", s_addressing_modes[op->mem.address]); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 |