• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Capstone testing regression */
2 /* By Do Minh Tuan <tuanit96@gmail.com>, 02-2019 */
3 
4 
5 #include "factory.h"
6 
7 static const char *s_access[] = {
8 	"UNCHANGED", "READ", "WRITE", "READ ; WRITE",
9 };
10 
print_read_write_regs(char * result,csh * handle,cs_detail * detail)11 static void print_read_write_regs(char *result, csh *handle, cs_detail *detail)
12 {
13 	int i;
14 
15 	if (detail->regs_read_count > 0) {
16 		add_str(&result, "\treading from regs: ");
17 
18 		for (i = 0; i < detail->regs_read_count; ++i) {
19 			if (i > 0)
20 				add_str(&result, ", ");
21 
22 			add_str(&result, "%s", cs_reg_name(*handle, detail->regs_read[i]));
23 		}
24 	}
25 
26 	if (detail->regs_write_count > 0) {
27 		add_str(&result, "\twriting to regs: ");
28 
29 		for (i = 0; i < detail->regs_write_count; ++i) {
30 			if (i > 0)
31 				add_str(&result, ", ");
32 
33 			add_str(&result, "%s", cs_reg_name(*handle, detail->regs_write[i]));
34 		}
35 	}
36 }
37 
get_detail_m680x(csh * handle,cs_mode mode,cs_insn * insn)38 char *get_detail_m680x(csh *handle, cs_mode mode, cs_insn *insn)
39 {
40 	cs_detail *detail = insn->detail;
41 	cs_m680x *m680x = NULL;
42 	int i;
43 	char *result;
44 
45 	result = (char *)malloc(sizeof(char));
46 	result[0] = '\0';
47 
48 	if (detail == NULL)
49 		return result;
50 
51 	m680x = &detail->m680x;
52 
53 	if (m680x->op_count)
54 		add_str(&result, " ; op_count: %u", m680x->op_count);
55 
56 	for (i = 0; i < m680x->op_count; i++) {
57 		cs_m680x_op *op = &(m680x->operands[i]);
58 		const char *comment;
59 
60 		switch ((int)op->type) {
61 			default:
62 				break;
63 
64 			case M680X_OP_REGISTER:
65 				comment = "";
66 
67 				if ((i == 0 && m680x->flags & M680X_FIRST_OP_IN_MNEM) ||
68 						(i == 1 && m680x->flags &
69 						 M680X_SECOND_OP_IN_MNEM))
70 					comment = " (in mnemonic)";
71 
72 				add_str(&result, " ; operands[%u].type: REGISTER = %s%s", i, cs_reg_name(*handle, op->reg), comment);
73 				break;
74 
75 			case M680X_OP_CONSTANT:
76 				add_str(&result, " ; operands[%u].type: CONSTANT = %u", i, op->const_val);
77 				break;
78 
79 			case M680X_OP_IMMEDIATE:
80 				add_str(&result, " ; operands[%u].type: IMMEDIATE = #%d", i, op->imm);
81 				break;
82 
83 			case M680X_OP_DIRECT:
84 				add_str(&result, " ; operands[%u].type: DIRECT = 0x%02X", i, op->direct_addr);
85 				break;
86 
87 			case M680X_OP_EXTENDED:
88 				add_str(&result, " ; operands[%u].type: EXTENDED %s = 0x%04X", i, op->ext.indirect ? "INDIRECT" : "", op->ext.address);
89 				break;
90 
91 			case M680X_OP_RELATIVE:
92 				add_str(&result, " ; operands[%u].type: RELATIVE = 0x%04X", i, op->rel.address);
93 				break;
94 
95 			case M680X_OP_INDEXED:
96 				add_str(&result, " ; operands[%u].type: INDEXED%s", i, (op->idx.flags & M680X_IDX_INDIRECT) ? " INDIRECT" : "");
97 
98 				if (op->idx.base_reg != M680X_REG_INVALID)
99 					add_str(&result, " ; base register: %s", cs_reg_name(*handle, op->idx.base_reg));
100 
101 				if (op->idx.offset_reg != M680X_REG_INVALID)
102 					add_str(&result, " ; offset register: %s", cs_reg_name(*handle, op->idx.offset_reg));
103 
104 				if ((op->idx.offset_bits != 0) &&
105 						(op->idx.offset_reg == M680X_REG_INVALID) &&
106 						!op->idx.inc_dec) {
107 					add_str(&result, " ; offset: %d", op->idx.offset);
108 
109 					if (op->idx.base_reg == M680X_REG_PC)
110 						add_str(&result, " ; offset address: 0x%X", op->idx.offset_addr);
111 
112 					add_str(&result, " ; offset bits: %u", op->idx.offset_bits);
113 				}
114 
115 				if (op->idx.inc_dec) {
116 					const char *post_pre = op->idx.flags &
117 						M680X_IDX_POST_INC_DEC ? "post" : "pre";
118 					const char *inc_dec = (op->idx.inc_dec > 0) ?
119 						"increment" : "decrement";
120 
121 					add_str(&result, " ; %s %s: %d", post_pre, inc_dec, abs(op->idx.inc_dec));
122 				}
123 
124 				break;
125 		}
126 
127 		if (op->size != 0)
128 			add_str(&result, " ; size: %u", op->size);
129 
130 		if (op->access != CS_AC_INVALID)
131 			add_str(&result, " ; access: %s", s_access[op->access]);
132 	}
133 
134 	print_read_write_regs(result, handle, detail);
135 
136 	return result;
137 }
138