blob: 84ced42c70a7374f3f15407d7cfb6549705aae26 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08004#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08005#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <capstone.h>
9
10#include "cs_priv.h"
11
12#include "MCRegisterInfo.h"
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include "utils.h"
15
Nguyen Anh Quynh58747ad2013-12-22 13:37:13 +080016void (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +080017cs_err (*arch_option[MAX_ARCH]) (cs_struct*, cs_opt_type, size_t value);
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080018
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080019unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080020
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080021
22void cs_version(int *major, int *minor)
23{
24 *major = CS_API_MAJOR;
25 *minor = CS_API_MINOR;
26}
27
28unsigned int cs_version_ex(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080029{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080030 if (major != NULL && minor != NULL) {
31 *major = CS_API_MAJOR;
32 *minor = CS_API_MINOR;
33 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080034
35 return (CS_API_MAJOR << 8) + CS_API_MINOR;
36}
37
38bool cs_support(cs_arch arch)
39{
40 if (arch == CS_ARCH_ALL)
41 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
42 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86));
43
44 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080045}
46
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080047cs_err cs_errno(csh handle)
48{
49 if (!handle)
50 return CS_ERR_CSH;
51
52 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
53
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080054 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080055}
56
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080057cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
58{
danghvu2b192962013-12-19 22:40:28 -060059 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080060
danghvu2b192962013-12-19 22:40:28 -060061 ud = calloc(1, sizeof(*ud));
62 if (!ud) {
63 // memory insufficient
64 return CS_ERR_MEM;
65 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080066
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080067 if (arch < CS_ARCH_MAX && arch_init[ud->arch]) {
68 ud->errnum = CS_ERR_OK;
69 ud->arch = arch;
70 ud->mode = mode;
71 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
72 ud->reg_name = NULL;
73 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080074
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +080075 arch_init[ud->arch](ud);
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080076 } else {
77 *handle = 0;
78 return CS_ERR_ARCH;
79 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080080
danghvu2b192962013-12-19 22:40:28 -060081 *handle = (uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080082
danghvu2b192962013-12-19 22:40:28 -060083 return CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080084}
85
86cs_err cs_close(csh handle)
87{
88 if (!handle)
89 return CS_ERR_CSH;
90
91 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
92
93 switch (ud->arch) {
94 case CS_ARCH_X86:
95 break;
96 case CS_ARCH_ARM:
97 case CS_ARCH_MIPS:
98 case CS_ARCH_ARM64:
99 free(ud->printer_info);
100 break;
101 default: // unsupported architecture
102 return CS_ERR_HANDLE;
103 }
104
105 memset(ud, 0, sizeof(*ud));
106 free(ud);
107
108 return CS_ERR_OK;
109}
110
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800111#define MIN(x, y) ((x) < (y) ? (x) : (y))
112
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800113// fill insn with mnemonic & operands info
114static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800115 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800116{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800117 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800118 // avoiding copy insn->detail
119 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800120
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800121 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
122 // copy from @regs_read until @arm
123 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
124 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
125 // then copy from @arm until end
126 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
127 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800128 } else {
129 insn->address = mci->address;
130 insn->size = mci->insn_size;
131 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800132
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800133 // fill the instruction bytes
134 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
135
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800136 // map internal instruction opcode to public insn ID
137 if (handle->insn_id)
138 handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail);
139
140 // alias instruction might have ID saved in OpcodePub
141 if (MCInst_getOpcodePub(mci))
142 insn->id = MCInst_getOpcodePub(mci);
143
144 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800145 if (postprinter)
146 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800147
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800148 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800149 // find first space or tab
150 char *sp = buffer;
151 for (sp = buffer; *sp; sp++)
152 if (*sp == ' '||*sp == '\t')
153 break;
154 if (*sp) {
155 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800156 // find the next non-space char
157 sp++;
158 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
159 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800160 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
161 } else
162 insn->op_str[0] = '\0';
163
164 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
165 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
166}
167
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800168cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800169{
danghvu2b192962013-12-19 22:40:28 -0600170 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
171 if (!handle)
172 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800173
danghvu0b6ea042013-12-19 23:07:26 -0600174 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800175 handle->detail = value;
176 return CS_ERR_OK;
177 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800178
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800179 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800180}
181
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800182// dynamicly allocate memory to contain disasm insn
183// NOTE: caller must free() the allocated memory itself to avoid memory leaking
pancakef0e4eed2013-12-11 22:14:42 +0100184size_t cs_disasm_dyn(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800185{
186 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
187 MCInst mci;
188 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800189 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800190 cs_insn insn_cache[64];
191 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800192 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193
194 if (!handle) {
195 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800196 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800197 return 0;
198 }
199
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800200 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800201
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800202 memset(insn_cache, 0, sizeof(insn_cache));
203
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800204 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600205 MCInst_Init(&mci);
Nguyen Anh Quynh1f449282013-12-15 14:04:59 +0800206 mci.detail = handle->detail;
207 mci.mode = handle->mode;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800208
209 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
210 if (r) {
211 SStream ss;
212 SStream_Init(&ss);
213
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800214 // relative branches need to know the address & size of current insn
215 mci.insn_size = insn_size;
216 mci.address = offset;
217
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800218 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800219 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800220 mci.flat_insn.address = offset;
221 mci.flat_insn.size = insn_size;
222 // allocate memory for @detail pointer
223 insn_cache[f].detail = calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800224 }
225
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800226 handle->printer(&mci, &ss, handle->printer_info);
227
Joxean114df0e2013-12-04 07:11:32 +0100228 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800229
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800230 f++;
231
232 if (f == ARR_SIZE(insn_cache)) {
233 // resize total to contain newly disasm insns
234 total_size += sizeof(insn_cache);
235 void *tmp = realloc(total, total_size);
236 if (tmp == NULL) { // insufficient memory
237 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800238 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800239 return 0;
240 }
241
242 total = tmp;
243 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
244 // reset f back to 0
245 f = 0;
246 }
247
248 c++;
249 buffer += insn_size;
250 size -= insn_size;
251 offset += insn_size;
252
253 if (count > 0 && c == count)
254 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800255 } else {
256 // encounter a broken instruction
257 // XXX: TODO: JOXEAN continue here
258 break;
259 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800260 }
261
262 if (f) {
263 // resize total to contain newly disasm insns
264 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
265 if (tmp == NULL) { // insufficient memory
266 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800267 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800268 return 0;
269 }
270
271 total = tmp;
272 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
273 }
274
275 *insn = total;
276
277 return c;
278}
279
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800280void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800281{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800282 size_t i;
283
284 // free all detail pointers
285 for (i = 0; i < count; i++)
286 free(insn[i].detail);
287
288 // then free pointer to cs_insn array
289 free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800290}
291
292// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100293const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800294{
295 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
296
297 if (!handle || handle->reg_name == NULL) {
298 return NULL;
299 }
300
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800301 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800302}
303
pancakef0e4eed2013-12-11 22:14:42 +0100304const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305{
306 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
307
308 if (!handle || handle->insn_name == NULL) {
309 return NULL;
310 }
311
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800312 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313}
314
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800315static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800316{
317 int i;
318
319 for (i = 0; i < max; i++) {
320 if (arr[i] == id)
321 return true;
322 }
323
324 return false;
325}
326
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800327bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800328{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800329 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800330 return false;
331
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800332 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
333
334 if (!handle->detail) {
335 handle->errnum = CS_ERR_DETAIL;
336 return false;
337 }
338
339 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340}
341
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800342bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800343{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800344 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345 return false;
346
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800347 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
348
349 if (!handle->detail) {
350 handle->errnum = CS_ERR_DETAIL;
351 return false;
352 }
353
354 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800355}
356
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800357bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800358{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800359 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800360 return false;
361
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800362 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
363
364 if (!handle->detail) {
365 handle->errnum = CS_ERR_DETAIL;
366 return false;
367 }
368
369 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800370}
371
372int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
373{
374 if (!ud)
375 return -1;
376
377 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
378 unsigned int count = 0, i;
379
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800380 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381
382 switch (handle->arch) {
383 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800384 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800385 return -1;
386 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800387 for (i = 0; i < insn->detail->arm.op_count; i++)
388 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800389 count++;
390 break;
391 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800392 for (i = 0; i < insn->detail->arm64.op_count; i++)
393 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800394 count++;
395 break;
396 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800397 for (i = 0; i < insn->detail->x86.op_count; i++)
398 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800399 count++;
400 break;
401 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800402 for (i = 0; i < insn->detail->mips.op_count; i++)
403 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800404 count++;
405 break;
406 }
407
408 return count;
409}
410
411int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
412 unsigned int post)
413{
414 if (!ud)
415 return -1;
416
417 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
418 unsigned int count = 0, i;
419
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800420 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421
422 switch (handle->arch) {
423 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800424 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800425 return -1;
426 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800427 for (i = 0; i < insn->detail->arm.op_count; i++) {
428 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429 count++;
430 if (count == post)
431 return i;
432 }
433 break;
434 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800435 for (i = 0; i < insn->detail->arm64.op_count; i++) {
436 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800437 count++;
438 if (count == post)
439 return i;
440 }
441 break;
442 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800443 for (i = 0; i < insn->detail->x86.op_count; i++) {
444 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800445 count++;
446 if (count == post)
447 return i;
448 }
449 break;
450 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800451 for (i = 0; i < insn->detail->mips.op_count; i++) {
452 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800453 count++;
454 if (count == post)
455 return i;
456 }
457 break;
458 }
459
460 return -1;
461}