blob: 34eaf13ed79a6d8a6c7f847cc62e6bd30757c7d2 [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
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <capstone.h>
8
9#include "cs_priv.h"
10
11#include "MCRegisterInfo.h"
12
13#include "arch/X86/X86Disassembler.h"
14#include "arch/X86/X86InstPrinter.h"
15#include "arch/X86/mapping.h"
16
17#include "arch/ARM/ARMDisassembler.h"
18#include "arch/ARM/ARMInstPrinter.h"
19#include "arch/ARM/mapping.h"
20
21#include "arch/Mips/MipsDisassembler.h"
22#include "arch/Mips/MipsInstPrinter.h"
23#include "arch/Mips/mapping.h"
24
25#include "arch/AArch64/AArch64Disassembler.h"
26#include "arch/AArch64/AArch64InstPrinter.h"
27#include "arch/AArch64/mapping.h"
28
29#include "utils.h"
30
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080031
32void cs_version(int *major, int *minor)
33{
34 *major = CS_API_MAJOR;
35 *minor = CS_API_MINOR;
36}
37
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080038cs_err cs_errno(csh handle)
39{
40 if (!handle)
41 return CS_ERR_CSH;
42
43 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
44
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080045 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080046}
47
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080048cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
49{
50 cs_struct *ud;
51
52 ud = calloc(1, sizeof(*ud));
53 if (!ud) {
54 // memory insufficient
55 return CS_ERR_MEM;
56 }
57
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080058 ud->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080059 ud->arch = arch;
60 ud->mode = mode;
61 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
62 ud->reg_name = NULL;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +080063 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080064
65 switch (ud->arch) {
66 case CS_ARCH_X86:
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +080067 // by default, we use Intel syntax
68 ud->printer = X86_Intel_printInst;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080069 ud->printer_info = NULL;
70 ud->disasm = X86_getInstruction;
71 ud->reg_name = X86_reg_name;
72 ud->insn_id = X86_get_insn_id;
73 ud->insn_name = X86_insn_name;
Nguyen Anh Quynha01d1542013-12-12 15:54:30 +080074 ud->post_printer = X86_post_printer;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080075 break;
76 case CS_ARCH_ARM: {
77 MCRegisterInfo *mri = malloc(sizeof(*mri));
78
79 ARM_init(mri);
80
81 ud->printer = ARM_printInst;
82 ud->printer_info = mri;
83 ud->reg_name = ARM_reg_name;
84 ud->insn_id = ARM_get_insn_id;
85 ud->insn_name = ARM_insn_name;
86 ud->post_printer = ARM_post_printer;
87
88 if (ud->mode & CS_MODE_THUMB)
89 ud->disasm = Thumb_getInstruction;
90 else
91 ud->disasm = ARM_getInstruction;
92 break;
93 }
94 case CS_ARCH_MIPS: {
95 MCRegisterInfo *mri = malloc(sizeof(*mri));
96
97 Mips_init(mri);
98 ud->printer = Mips_printInst;
99 ud->printer_info = mri;
100 ud->getinsn_info = mri;
101 ud->reg_name = Mips_reg_name;
102 ud->insn_id = Mips_get_insn_id;
103 ud->insn_name = Mips_insn_name;
104
105 if (ud->mode & CS_MODE_32)
106 ud->disasm = Mips_getInstruction;
107 else
108 ud->disasm = Mips64_getInstruction;
109
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800110 break;
111 }
112 case CS_ARCH_ARM64: {
113 MCRegisterInfo *mri = malloc(sizeof(*mri));
114
115 AArch64_init(mri);
116 ud->printer = AArch64_printInst;
117 ud->printer_info = mri;
118 ud->getinsn_info = mri;
119 ud->disasm = AArch64_getInstruction;
120 ud->reg_name = AArch64_reg_name;
121 ud->insn_id = AArch64_get_insn_id;
122 ud->insn_name = AArch64_insn_name;
123 ud->post_printer = AArch64_post_printer;
124 break;
125 }
126 default: // unsupported architecture
127 free(ud);
128 return CS_ERR_ARCH;
129 }
130
131 *handle = (uintptr_t)ud;
132
133 return CS_ERR_OK;
134}
135
136cs_err cs_close(csh handle)
137{
138 if (!handle)
139 return CS_ERR_CSH;
140
141 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
142
143 switch (ud->arch) {
144 case CS_ARCH_X86:
145 break;
146 case CS_ARCH_ARM:
147 case CS_ARCH_MIPS:
148 case CS_ARCH_ARM64:
149 free(ud->printer_info);
150 break;
151 default: // unsupported architecture
152 return CS_ERR_HANDLE;
153 }
154
155 memset(ud, 0, sizeof(*ud));
156 free(ud);
157
158 return CS_ERR_OK;
159}
160
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800161#define MIN(x, y) ((x) < (y) ? (x) : (y))
162
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800163// fill insn with mnemonic & operands info
164static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
pancakef0e4eed2013-12-11 22:14:42 +0100165 PostPrinter_t printer, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800166{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800167 if (handle->detail) {
168 memcpy(insn, &mci->pub_insn, sizeof(*insn));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800169
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800170 // fill the instruction bytes
171 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
Nguyen Anh Quynhad61c492013-11-30 16:23:31 +0800172
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800173 // map internal instruction opcode to public insn ID
174 if (handle->insn_id)
175 handle->insn_id(insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800176
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800177 // alias instruction might have ID saved in OpcodePub
178 if (MCInst_getOpcodePub(mci))
179 insn->id = MCInst_getOpcodePub(mci);
180
181 if (printer)
182 printer(insn, buffer);
183 } else {
184 insn->address = mci->address;
185 insn->size = mci->insn_size;
186 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187
188 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800189 // find first space or tab
190 char *sp = buffer;
191 for (sp = buffer; *sp; sp++)
192 if (*sp == ' '||*sp == '\t')
193 break;
194 if (*sp) {
195 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800196 // find the next non-space char
197 sp++;
198 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
199 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800200 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
201 } else
202 insn->op_str[0] = '\0';
203
204 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
205 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
206}
207
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800208cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800209{
210 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
211 if (!handle)
212 return CS_ERR_CSH;
213
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800214 switch(type) {
215 default:
216 break;
217 case CS_OPT_DETAIL:
218 handle->detail = value;
219 return CS_ERR_OK;
220 }
221
222 // only selected archs care about CS_OPT_SYNTAX
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800223 switch (handle->arch) {
224 default:
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800225 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800226 return CS_ERR_OPTION;
227
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800228 case CS_ARCH_X86:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800229 if (type & CS_OPT_SYNTAX) {
230 switch(value) {
231 default:
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800232 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800233 return CS_ERR_OPTION;
234
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +0800235 case CS_OPT_SYNTAX_INTEL:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800236 handle->printer = X86_Intel_printInst;
237 break;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800238
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +0800239 case CS_OPT_SYNTAX_ATT:
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800240 handle->printer = X86_ATT_printInst;
241 break;
242 }
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800243 } else {
244 handle->errnum = CS_ERR_OPTION;
Nguyen Anh Quynh041e25d2013-12-06 00:37:32 +0800245 return CS_ERR_OPTION;
Nguyen Anh Quynhfe8030b2013-12-06 10:09:43 +0800246 }
Nguyen Anh Quynh4a60a562013-12-03 21:56:54 +0800247 break;
248 }
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800249
250 return CS_ERR_OK;
251}
252
pancakef0e4eed2013-12-11 22:14:42 +0100253size_t cs_disasm(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 +0800254{
255 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
256 MCInst mci;
257 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800258 size_t c = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800259
260 if (!handle) {
261 // FIXME: handle this case?
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800262 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800263 return 0;
264 }
265
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800266 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800267
268 while (size > 0) {
269 MCInst_Init(&mci);
270
271 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
272 if (r) {
273 SStream ss;
274 SStream_Init(&ss);
275
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800276 mci.detail = handle->detail;
277 // relative branches need to know the address & size of current insn
278 mci.insn_size = insn_size;
279 mci.address = offset;
280
281 // save all the information for non-detailed mode
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800282 mci.pub_insn.address = offset;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800283 mci.pub_insn.size = insn_size;
284
285 if (handle->detail == CS_OPT_ON) {
286 mci.mode = handle->mode;
287 }
288
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800289 handle->printer(&mci, &ss, handle->printer_info);
290
Joxean114df0e2013-12-04 07:11:32 +0100291 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800292
293 c++;
294 insn++;
295 buffer += insn_size;
296 size -= insn_size;
297 offset += insn_size;
298
299 if (count > 0) {
300 if (c == count)
301 return c;
302 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800303 } else
304 // face a broken instruction?
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305 return c;
306 }
307
308 return c;
309}
310
311// dynamicly allocate memory to contain disasm insn
312// NOTE: caller must free() the allocated memory itself to avoid memory leaking
pancakef0e4eed2013-12-11 22:14:42 +0100313size_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 +0800314{
315 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
316 MCInst mci;
317 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800318 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800319 cs_insn insn_cache[64];
320 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800321 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800322
323 if (!handle) {
324 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800325 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800326 return 0;
327 }
328
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800329 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800330
331 while (size > 0) {
332 MCInst_Init(&mci);
333
334 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
335 if (r) {
336 SStream ss;
337 SStream_Init(&ss);
338
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800339 mci.detail = handle->detail;
340 // relative branches need to know the address & size of current insn
341 mci.insn_size = insn_size;
342 mci.address = offset;
343
344 // save all the information for non-detailed mode
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345 mci.pub_insn.address = offset;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800346 mci.pub_insn.size = insn_size;
347
348 if (handle->detail == CS_OPT_ON) {
349 mci.mode = handle->mode;
350 }
351
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800352 handle->printer(&mci, &ss, handle->printer_info);
353
Joxean114df0e2013-12-04 07:11:32 +0100354 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800355
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800356 f++;
357
358 if (f == ARR_SIZE(insn_cache)) {
359 // resize total to contain newly disasm insns
360 total_size += sizeof(insn_cache);
361 void *tmp = realloc(total, total_size);
362 if (tmp == NULL) { // insufficient memory
363 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800364 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800365 return 0;
366 }
367
368 total = tmp;
369 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
370 // reset f back to 0
371 f = 0;
372 }
373
374 c++;
375 buffer += insn_size;
376 size -= insn_size;
377 offset += insn_size;
378
379 if (count > 0 && c == count)
380 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800381 } else {
382 // encounter a broken instruction
383 // XXX: TODO: JOXEAN continue here
384 break;
385 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800386 }
387
388 if (f) {
389 // resize total to contain newly disasm insns
390 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
391 if (tmp == NULL) { // insufficient memory
392 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800393 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800394 return 0;
395 }
396
397 total = tmp;
398 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
399 }
400
401 *insn = total;
402
403 return c;
404}
405
406void cs_free(void *m)
407{
408 free(m);
409}
410
411// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100412const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800413{
414 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
415
416 if (!handle || handle->reg_name == NULL) {
417 return NULL;
418 }
419
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800420 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421}
422
pancakef0e4eed2013-12-11 22:14:42 +0100423const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800424{
425 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
426
427 if (!handle || handle->insn_name == NULL) {
428 return NULL;
429 }
430
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800431 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432}
433
434static bool arr_exist(unsigned int *arr, int max, unsigned int id)
435{
436 int i;
437
438 for (i = 0; i < max; i++) {
439 if (arr[i] == id)
440 return true;
441 }
442
443 return false;
444}
445
446bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id)
447{
448 if (!handle)
449 return false;
450
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800451 return arr_exist(insn->groups, insn->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800452}
453
454bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id)
455{
456 if (!handle)
457 return false;
458
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800459 return arr_exist(insn->regs_read, insn->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800460}
461
462bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id)
463{
464 if (!handle)
465 return false;
466
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800467 return arr_exist(insn->regs_write, insn->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800468}
469
470int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
471{
472 if (!ud)
473 return -1;
474
475 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
476 unsigned int count = 0, i;
477
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800478 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800479
480 switch (handle->arch) {
481 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800482 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800483 return -1;
484 case CS_ARCH_ARM:
485 for (i = 0; i < insn->arm.op_count; i++)
486 if (insn->arm.operands[i].type == op_type)
487 count++;
488 break;
489 case CS_ARCH_ARM64:
490 for (i = 0; i < insn->arm64.op_count; i++)
491 if (insn->arm64.operands[i].type == op_type)
492 count++;
493 break;
494 case CS_ARCH_X86:
495 for (i = 0; i < insn->x86.op_count; i++)
496 if (insn->x86.operands[i].type == op_type)
497 count++;
498 break;
499 case CS_ARCH_MIPS:
500 for (i = 0; i < insn->mips.op_count; i++)
501 if (insn->mips.operands[i].type == op_type)
502 count++;
503 break;
504 }
505
506 return count;
507}
508
509int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
510 unsigned int post)
511{
512 if (!ud)
513 return -1;
514
515 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
516 unsigned int count = 0, i;
517
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800518 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800519
520 switch (handle->arch) {
521 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800522 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800523 return -1;
524 case CS_ARCH_ARM:
525 for (i = 0; i < insn->arm.op_count; i++) {
526 if (insn->arm.operands[i].type == op_type)
527 count++;
528 if (count == post)
529 return i;
530 }
531 break;
532 case CS_ARCH_ARM64:
533 for (i = 0; i < insn->arm64.op_count; i++) {
534 if (insn->arm64.operands[i].type == op_type)
535 count++;
536 if (count == post)
537 return i;
538 }
539 break;
540 case CS_ARCH_X86:
541 for (i = 0; i < insn->x86.op_count; i++) {
542 if (insn->x86.operands[i].type == op_type)
543 count++;
544 if (count == post)
545 return i;
546 }
547 break;
548 case CS_ARCH_MIPS:
549 for (i = 0; i < insn->mips.op_count; i++) {
550 if (insn->mips.operands[i].type == op_type)
551 count++;
552 if (count == post)
553 return i;
554 }
555 break;
556 }
557
558 return -1;
559}