1 /* radare2 - LGPL - Copyright 2013-2015 - pancake */
2 
3 // instruction set : http://www.tachyonsoft.com/inst390m.htm
4 
5 #include <r_asm.h>
6 #include <r_lib.h>
7 #include <capstone.h>
8 
9 static csh cd = 0;
10 
the_end(void * p)11 static bool the_end(void *p) {
12 	if (cd) {
13 		cs_close (&cd);
14 		cd = 0;
15 	}
16 	return true;
17 }
18 
disassemble(RAsm * a,RAsmOp * op,const ut8 * buf,int len)19 static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
20 	static int omode = 0;
21 	int mode, n, ret;
22 	ut64 off = a->pc;
23 	cs_insn* insn = NULL;
24 	mode = CS_MODE_BIG_ENDIAN;
25 	if (cd && mode != omode) {
26 		cs_close (&cd);
27 		cd = 0;
28 	}
29 	op->size = 0;
30 	omode = mode;
31 	if (cd == 0) {
32 		ret = cs_open (CS_ARCH_SYSZ, mode, &cd);
33 		if (ret) {
34 			return 0;
35 		}
36 		cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF);
37 	}
38 	n = cs_disasm (cd, (const ut8*)buf, len, off, 1, &insn);
39 	if (n>0) {
40 		if (insn->size>0) {
41 			op->size = insn->size;
42 			char *buf_asm = sdb_fmt ("%s%s%s",
43 					insn->mnemonic, insn->op_str[0]?" ": "",
44 					insn->op_str);
45 			char *ptrstr = strstr (buf_asm, "ptr ");
46 			if (ptrstr) {
47 				memmove (ptrstr, ptrstr + 4, strlen (ptrstr + 4) + 1);
48 			}
49 			r_asm_op_set_asm (op, buf_asm);
50 		}
51 		cs_free (insn, n);
52 	}
53 	return op->size;
54 }
55 
56 RAsmPlugin r_asm_plugin_sysz = {
57 	.name = "sysz",
58 	.desc = "SystemZ CPU disassembler",
59 	.license = "BSD",
60 	.arch = "sysz",
61 	.bits = 32 | 64,
62 	.endian = R_SYS_ENDIAN_BIG,
63 	.fini = the_end,
64 	.disassemble = &disassemble,
65 };
66 
67 #ifndef R2_PLUGIN_INCORE
68 R_API RLibStruct radare_plugin = {
69 	.type = R_LIB_TYPE_ASM,
70 	.data = &r_asm_plugin_sysz,
71 	.version = R2_VERSION
72 };
73 #endif
74