Debian Codegraph @

tests/test_xcore.c

raw symbols SCIP
1 /* Capstone Disassembler Engine */cxx . . $ `<file>/tests/test_xcore.c`/
2 /* By Nguyen Anh Quynh <[email protected]>, 2013-2014 */
3
4 #include <stdio.h>
5
6 #include <capstone/platform.h>cxx . . $ `<file>/include/capstone/platform.h`/
7 #include <capstone/capstone.h>cxx . . $ `<file>/include/capstone/capstone.h`/
8
9 struct platformcxx . . $ platform# {
10 cs_archcxx . . $ cs_arch# archcxx . . $ platform#arch.;
11 cs_modecxx . . $ cs_mode# modecxx . . $ platform#mode.;
12 unsigned char *codecxx . . $ platform#code.;
13 size_t sizecxx . . $ platform#size.;
14 const char *commentcxx . . $ platform#comment.;
15 };
16
17 static cshcxx . . $ csh# handlecxx . . $ handle.;
18
19 static void print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).(const char *comment, unsigned char *str, size_t len)
20 {
21 unsigned char *c;
22
23 printfcxx . . $ printf(581fddd72a8db8fc).("%s", comment);
24 for (c = str; c < str + len; c++) {
25 printfcxx . . $ printf(581fddd72a8db8fc).("0x%02x ", *c & 0xff);
26 }
27
28 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
29 }
30
31 static void print_insn_detailcxx . . $ print_insn_detail(ecef60df25d96a55).(cs_insncxx . . $ cs_insn# *ins)
32 {
33 cs_xcorecxx . . $ cs_xcore# *xcore;
34 int i;
35
36 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
37 if (ins->detailcxx . . $ cs_insn#detail. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
38 return;
39
40 xcore = &(ins->detailcxx . . $ cs_insn#detail.->xcorecxx . . $ cs_detail#$anonymous_type_0#xcore.);
41 if (xcore->op_countcxx . . $ cs_xcore#op_count.)
42 printfcxx . . $ printf(581fddd72a8db8fc).("\top_count: %u\n", xcore->op_countcxx . . $ cs_xcore#op_count.);
43
44 for (i = 0; i < xcore->op_countcxx . . $ cs_xcore#op_count.; i++) {
45 cs_xcore_opcxx . . $ cs_xcore_op# *op = &(xcore->operandscxx . . $ cs_xcore#operands.[i]);
46 switch((int)op->typecxx . . $ cs_xcore_op#type.) {
47 default:
48 break;
49 case XCORE_OP_REGcxx . . $ XCORE_OP_REG.:
50 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: REG = %s\n", i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->regcxx . . $ cs_xcore_op#$anonymous_type_0#reg.));
51 break;
52 case XCORE_OP_IMMcxx . . $ XCORE_OP_IMM.:
53 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: IMM = 0x%x\n", i, op->immcxx . . $ cs_xcore_op#$anonymous_type_0#imm.);
54 break;
55 case XCORE_OP_MEMcxx . . $ XCORE_OP_MEM.:
56 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: MEM\n", i);
57 if (op->memcxx . . $ cs_xcore_op#$anonymous_type_0#mem..basecxx . . $ xcore_op_mem#base. != XCORE_REG_INVALIDcxx . . $ XCORE_REG_INVALID.)
58 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.base: REG = %s\n",
59 i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->memcxx . . $ cs_xcore_op#$anonymous_type_0#mem..basecxx . . $ xcore_op_mem#base.));
60 if (op->memcxx . . $ cs_xcore_op#$anonymous_type_0#mem..indexcxx . . $ xcore_op_mem#index. != XCORE_REG_INVALIDcxx . . $ XCORE_REG_INVALID.)
61 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.index: REG = %s\n",
62 i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->memcxx . . $ cs_xcore_op#$anonymous_type_0#mem..indexcxx . . $ xcore_op_mem#index.));
63 if (op->memcxx . . $ cs_xcore_op#$anonymous_type_0#mem..dispcxx . . $ xcore_op_mem#disp. != 0)
64 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->memcxx . . $ cs_xcore_op#$anonymous_type_0#mem..dispcxx . . $ xcore_op_mem#disp.);
65 if (op->memcxx . . $ cs_xcore_op#$anonymous_type_0#mem..directcxx . . $ xcore_op_mem#direct. != 1)
66 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.direct: -1\n", i);
67
68
69 break;
70 }
71 }
72
73 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
74 }
75
76 static void testcxx . . $ test(d878ad36e69247c7).()
77 {
78 #define XCORE_CODEcxx . . $ `tests/test_xcore.c:78:9`! "\xfe\x0f\xfe\x17\x13\x17\xc6\xfe\xec\x17\x97\xf8\xec\x4f\x1f\xfd\xec\x37\x07\xf2\x45\x5b\xf9\xfa\x02\x06\x1b\x10\x09\xfd\xec\xa7"
79
80 struct platformcxx . . $ platform# platforms[] = {
81 {
82 CS_ARCH_XCOREcxx . . $ CS_ARCH_XCORE.,
83 CS_MODE_BIG_ENDIANcxx . . $ CS_MODE_BIG_ENDIAN.,
84 (unsigned char*)XCORE_CODEcxx . . $ `tests/test_xcore.c:78:9`!,
85 sizeof(XCORE_CODEcxx . . $ `tests/test_xcore.c:78:9`!) - 1,
86 "XCore",
87 },
88 };
89
90 uint64_tcxx . . $ uint64_t# address = 0x1000;
91 cs_insncxx . . $ cs_insn# *insn;
92 int i;
93 size_t count;
94
95 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
96 cs_errcxx . . $ cs_err# err = cs_opencxx . . $ cs_open(b2cb849d8ee082cd).(platforms[i].archcxx . . $ platform#arch., platforms[i].modecxx . . $ platform#mode., &handlecxx . . $ handle.);
97 if (err) {
98 printfcxx . . $ printf(581fddd72a8db8fc).("Failed on cs_open() with error returned: %u\n", err);
99 abortcxx . . $ abort(8df32a62017fbbf6).();
100 }
101
102 cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handlecxx . . $ handle., CS_OPT_DETAILcxx . . $ CS_OPT_DETAIL., CS_OPT_ONcxx . . $ CS_OPT_ON.);
103
104 count = cs_disasmcxx . . $ cs_disasm(d552efbd9b22be5).(handlecxx . . $ handle., platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size., address, 0, &insn);
105 if (count) {
106 size_t j;
107
108 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
109 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
110 print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
111 printfcxx . . $ printf(581fddd72a8db8fc).("Disasm:\n");
112
113 for (j = 0; j < count; j++) {
114 printfcxx . . $ printf(581fddd72a8db8fc).("0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! ":\t%s\t%s\n", insn[j].addresscxx . . $ cs_insn#address., insn[j].mnemoniccxx . . $ cs_insn#mnemonic., insn[j].op_strcxx . . $ cs_insn#op_str.);
115 print_insn_detailcxx . . $ print_insn_detail(ecef60df25d96a55).(&insn[j]);
116 }
117 printfcxx . . $ printf(581fddd72a8db8fc).("0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! ":\n", insn[j-1].addresscxx . . $ cs_insn#address. + insn[j-1].sizecxx . . $ cs_insn#size.);
118
119 // free memory allocated by cs_disasm()
120 cs_freecxx . . $ cs_free(d0ae7023b33cf0c8).(insn, count);
121 } else {
122 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
123 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
124 print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
125 printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Failed to disasm given code!\n");
126 abortcxx . . $ abort(8df32a62017fbbf6).();
127 }
128
129 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
130
131 cs_closecxx . . $ cs_close(b9ab39fbab147149).(&handlecxx . . $ handle.);
132 }
133 }
134
135 int maincxx . . $ main(d1b40a211a1a4e4c).()
136 {
137 testcxx . . $ test(d878ad36e69247c7).();
138
139 return 0;
140 }