Rizin
unix-like reverse engineering framework and cli tools
asm_tms320c64x.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_asm.h>
5 #include <rz_lib.h>
6 #include <capstone/capstone.h>
7 static csh cd = 0;
8 #include "cs_mnemonics.c"
9 
10 #ifdef CAPSTONE_TMS320C64X_H
11 #define CAPSTONE_HAS_TMS320C64X 1
12 #else
13 #define CAPSTONE_HAS_TMS320C64X 0
14 #warning Cannot find capstone-tms320c64x support
15 #endif
16 
17 #if CAPSTONE_HAS_TMS320C64X
18 
19 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
20  cs_insn *insn;
21  int n = -1, ret = -1;
22  int mode = 0;
23  if (op) {
24  memset(op, 0, sizeof(RzAsmOp));
25  op->size = 4;
26  }
27  if (cd != 0) {
28  cs_close(&cd);
29  }
31  if (ret) {
32  goto fin;
33  }
35  if (!op) {
36  return 0;
37  }
38  n = cs_disasm(cd, buf, len, a->pc, 1, &insn);
39  if (n < 1) {
40  rz_asm_op_set_asm(op, "invalid");
41  op->size = 4;
42  ret = -1;
43  goto beach;
44  } else {
45  ret = 4;
46  }
47  if (insn->size < 1) {
48  goto beach;
49  }
50  op->size = insn->size;
51  rz_asm_op_set_asm(op, sdb_fmt("%s%s%s", insn->mnemonic, insn->op_str[0] ? " " : "", insn->op_str));
52  rz_str_replace_char(rz_strbuf_get(&op->buf_asm), '%', 0);
53  rz_str_case(rz_strbuf_get(&op->buf_asm), false);
54  cs_free(insn, n);
55 beach:
56 // cs_close (&cd);
57 fin:
58  return ret;
59 }
60 
62  .name = "tms320c64x",
63  .desc = "Capstone TMS320c64x disassembler",
64  .license = "BSD",
65  .arch = "tms320c64x",
66  .bits = 32,
68  .disassemble = &disassemble,
69  .mnemonics = mnemonics
70 };
71 
72 #else
73 
75  .name = "tms320c64x",
76  .desc = "Capstone TMS320c64x disassembler (unsupported)",
77  .license = "BSD",
78  .arch = "tms320c64x",
79  .bits = 32,
80  .endian = RZ_SYS_ENDIAN_LITTLE,
81  .mnemonics = mnemonics
82 };
83 
84 #endif
85 
86 #ifndef RZ_PLUGIN_INCORE
89  .data = &rz_asm_plugin_tms320c64x,
91 };
92 #endif
size_t len
Definition: 6502dis.c:15
RZ_API void rz_asm_op_set_asm(RzAsmOp *op, const char *str)
Definition: aop.c:53
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_6502.c:12
RZ_API RzLibStruct rizin_plugin
static csh cd
Definition: asm_tms320c64x.c:7
RzAsmPlugin rz_asm_plugin_tms320c64x
@ CS_ARCH_TMS320C64X
TMS320C64x architecture.
Definition: capstone.h:84
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
size_t csh
Definition: capstone.h:71
@ CS_OPT_OFF
Turn OFF an option - default for CS_OPT_DETAIL, CS_OPT_SKIPDATA, CS_OPT_UNSIGNED.
Definition: capstone.h:182
#define RZ_API
CAPSTONE_EXPORT size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Definition: cs.c:798
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:646
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
int n
Definition: mipsasm.c:19
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
RZ_API void rz_str_case(char *str, bool up)
Definition: str.c:341
RZ_API int rz_str_replace_char(char *s, int a, int b)
Definition: str.c:169
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526
#define RZ_VERSION
Definition: rz_version.h:8
#define a(i)
Definition: sha256.c:41
const char * name
Definition: rz_asm.h:130
const char * version
Definition: rz_asm.h:133
Definition: dis.c:32
static const char * mnemonics[]
Definition: z80asm.c:43