blob: 58de52f4e4b08087d5a52e9e3fea6bcbafd871cd [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
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080022unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080023{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080024 if (major != NULL && minor != NULL) {
25 *major = CS_API_MAJOR;
26 *minor = CS_API_MINOR;
27 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080028
29 return (CS_API_MAJOR << 8) + CS_API_MINOR;
30}
31
32bool cs_support(cs_arch arch)
33{
34 if (arch == CS_ARCH_ALL)
35 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
36 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86));
37
38 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080039}
40
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080041cs_err cs_errno(csh handle)
42{
43 if (!handle)
44 return CS_ERR_CSH;
45
46 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
47
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080048 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080049}
50
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080051cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
52{
danghvu2b192962013-12-19 22:40:28 -060053 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080054
danghvu2b192962013-12-19 22:40:28 -060055 ud = calloc(1, sizeof(*ud));
56 if (!ud) {
57 // memory insufficient
58 return CS_ERR_MEM;
59 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080060
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080061 if (arch < CS_ARCH_MAX && arch_init[ud->arch]) {
62 ud->errnum = CS_ERR_OK;
63 ud->arch = arch;
64 ud->mode = mode;
65 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
66 ud->reg_name = NULL;
67 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080068
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +080069 arch_init[ud->arch](ud);
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080070 } else {
71 *handle = 0;
72 return CS_ERR_ARCH;
73 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080074
danghvu2b192962013-12-19 22:40:28 -060075 *handle = (uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080076
danghvu2b192962013-12-19 22:40:28 -060077 return CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080078}
79
80cs_err cs_close(csh handle)
81{
82 if (!handle)
83 return CS_ERR_CSH;
84
85 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
86
87 switch (ud->arch) {
88 case CS_ARCH_X86:
89 break;
90 case CS_ARCH_ARM:
91 case CS_ARCH_MIPS:
92 case CS_ARCH_ARM64:
93 free(ud->printer_info);
94 break;
95 default: // unsupported architecture
96 return CS_ERR_HANDLE;
97 }
98
99 memset(ud, 0, sizeof(*ud));
100 free(ud);
101
102 return CS_ERR_OK;
103}
104
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800105#define MIN(x, y) ((x) < (y) ? (x) : (y))
106
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800107// fill insn with mnemonic & operands info
108static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800109 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800110{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800111 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800112 // avoiding copy insn->detail
113 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800114
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800115 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
116 // copy from @regs_read until @arm
117 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
118 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
119 // then copy from @arm until end
120 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
121 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800122 } else {
123 insn->address = mci->address;
124 insn->size = mci->insn_size;
125 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800126
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800127 // fill the instruction bytes
128 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
129
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800130 // map internal instruction opcode to public insn ID
131 if (handle->insn_id)
132 handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail);
133
134 // alias instruction might have ID saved in OpcodePub
135 if (MCInst_getOpcodePub(mci))
136 insn->id = MCInst_getOpcodePub(mci);
137
138 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800139 if (postprinter)
140 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800141
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800142 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800143 // find first space or tab
144 char *sp = buffer;
145 for (sp = buffer; *sp; sp++)
146 if (*sp == ' '||*sp == '\t')
147 break;
148 if (*sp) {
149 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800150 // find the next non-space char
151 sp++;
152 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
153 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800154 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
155 } else
156 insn->op_str[0] = '\0';
157
158 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
159 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
160}
161
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800162cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800163{
danghvu2b192962013-12-19 22:40:28 -0600164 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
165 if (!handle)
166 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800167
danghvu0b6ea042013-12-19 23:07:26 -0600168 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800169 handle->detail = value;
170 return CS_ERR_OK;
171 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800172
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800173 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800174}
175
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800176// dynamicly allocate memory to contain disasm insn
177// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800178size_t cs_disasm_ex(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 +0800179{
180 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
181 MCInst mci;
182 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800183 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800184 cs_insn insn_cache[64];
185 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800186 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187
188 if (!handle) {
189 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800190 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800191 return 0;
192 }
193
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800194 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800195
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800196 memset(insn_cache, 0, sizeof(insn_cache));
197
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800198 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600199 MCInst_Init(&mci);
Nguyen Anh Quynh1f449282013-12-15 14:04:59 +0800200 mci.detail = handle->detail;
201 mci.mode = handle->mode;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800202
203 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
204 if (r) {
205 SStream ss;
206 SStream_Init(&ss);
207
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800208 // relative branches need to know the address & size of current insn
209 mci.insn_size = insn_size;
210 mci.address = offset;
211
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800212 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800213 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800214 mci.flat_insn.address = offset;
215 mci.flat_insn.size = insn_size;
216 // allocate memory for @detail pointer
217 insn_cache[f].detail = calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800218 }
219
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800220 handle->printer(&mci, &ss, handle->printer_info);
221
Joxean114df0e2013-12-04 07:11:32 +0100222 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800223
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800224 f++;
225
226 if (f == ARR_SIZE(insn_cache)) {
227 // resize total to contain newly disasm insns
228 total_size += sizeof(insn_cache);
229 void *tmp = realloc(total, total_size);
230 if (tmp == NULL) { // insufficient memory
231 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800232 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800233 return 0;
234 }
235
236 total = tmp;
237 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
238 // reset f back to 0
239 f = 0;
240 }
241
242 c++;
243 buffer += insn_size;
244 size -= insn_size;
245 offset += insn_size;
246
247 if (count > 0 && c == count)
248 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800249 } else {
250 // encounter a broken instruction
251 // XXX: TODO: JOXEAN continue here
252 break;
253 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800254 }
255
256 if (f) {
257 // resize total to contain newly disasm insns
258 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
259 if (tmp == NULL) { // insufficient memory
260 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800261 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800262 return 0;
263 }
264
265 total = tmp;
266 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
267 }
268
269 *insn = total;
270
271 return c;
272}
273
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800274void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800275{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800276 size_t i;
277
278 // free all detail pointers
279 for (i = 0; i < count; i++)
280 free(insn[i].detail);
281
282 // then free pointer to cs_insn array
283 free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800284}
285
286// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100287const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800288{
289 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
290
291 if (!handle || handle->reg_name == NULL) {
292 return NULL;
293 }
294
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800295 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800296}
297
pancakef0e4eed2013-12-11 22:14:42 +0100298const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800299{
300 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
301
302 if (!handle || handle->insn_name == NULL) {
303 return NULL;
304 }
305
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800306 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800307}
308
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800309static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800310{
311 int i;
312
313 for (i = 0; i < max; i++) {
314 if (arr[i] == id)
315 return true;
316 }
317
318 return false;
319}
320
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800321bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800322{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800323 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800324 return false;
325
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800326 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
327
328 if (!handle->detail) {
329 handle->errnum = CS_ERR_DETAIL;
330 return false;
331 }
332
333 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800334}
335
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800336bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800337{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800338 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800339 return false;
340
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800341 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
342
343 if (!handle->detail) {
344 handle->errnum = CS_ERR_DETAIL;
345 return false;
346 }
347
348 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800349}
350
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800351bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800352{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800353 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800354 return false;
355
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800356 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
357
358 if (!handle->detail) {
359 handle->errnum = CS_ERR_DETAIL;
360 return false;
361 }
362
363 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800364}
365
366int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
367{
368 if (!ud)
369 return -1;
370
371 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
372 unsigned int count = 0, i;
373
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800374 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800375
376 switch (handle->arch) {
377 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800378 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800379 return -1;
380 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800381 for (i = 0; i < insn->detail->arm.op_count; i++)
382 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800383 count++;
384 break;
385 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800386 for (i = 0; i < insn->detail->arm64.op_count; i++)
387 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800388 count++;
389 break;
390 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800391 for (i = 0; i < insn->detail->x86.op_count; i++)
392 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800393 count++;
394 break;
395 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800396 for (i = 0; i < insn->detail->mips.op_count; i++)
397 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800398 count++;
399 break;
400 }
401
402 return count;
403}
404
405int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
406 unsigned int post)
407{
408 if (!ud)
409 return -1;
410
411 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
412 unsigned int count = 0, i;
413
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800414 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800415
416 switch (handle->arch) {
417 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800418 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800419 return -1;
420 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800421 for (i = 0; i < insn->detail->arm.op_count; i++) {
422 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800423 count++;
424 if (count == post)
425 return i;
426 }
427 break;
428 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800429 for (i = 0; i < insn->detail->arm64.op_count; i++) {
430 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800431 count++;
432 if (count == post)
433 return i;
434 }
435 break;
436 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800437 for (i = 0; i < insn->detail->x86.op_count; i++) {
438 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800439 count++;
440 if (count == post)
441 return i;
442 }
443 break;
444 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800445 for (i = 0; i < insn->detail->mips.op_count; i++) {
446 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800447 count++;
448 if (count == post)
449 return i;
450 }
451 break;
452 }
453
454 return -1;
455}