Debian Codegraph @

tests/test_ppc.c

raw symbols SCIP
1 /* Capstone Disassembler Engine */cxx . . $ `<file>/tests/test_ppc.c`/
2 /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */
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_tcxx . . $ 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_tcxx . . $ 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 const char* get_bc_namecxx . . $ get_bc_name(f7ae7e56280f18ae).(int bc)
32 {
33 switch(bc) {
34 default:
35 case PPC_BC_INVALIDcxx . . $ PPC_BC_INVALID.:
36 return ("invalid");
37 case PPC_BC_LTcxx . . $ PPC_BC_LT.:
38 return ("lt");
39 case PPC_BC_LEcxx . . $ PPC_BC_LE.:
40 return ("le");
41 case PPC_BC_EQcxx . . $ PPC_BC_EQ.:
42 return ("eq");
43 case PPC_BC_GEcxx . . $ PPC_BC_GE.:
44 return ("ge");
45 case PPC_BC_GTcxx . . $ PPC_BC_GT.:
46 return ("gt");
47 case PPC_BC_NEcxx . . $ PPC_BC_NE.:
48 return ("ne");
49 case PPC_BC_UNcxx . . $ PPC_BC_UN.:
50 return ("un");
51 case PPC_BC_NUcxx . . $ PPC_BC_NU.:
52 return ("nu");
53 case PPC_BC_SOcxx . . $ PPC_BC_SO.:
54 return ("so");
55 case PPC_BC_NScxx . . $ PPC_BC_NS.:
56 return ("ns");
57 }
58 }
59
60 static void print_insn_detailcxx . . $ print_insn_detail(ecef60df25d96a55).(cs_insncxx . . $ cs_insn# *ins)
61 {
62 cs_ppccxx . . $ cs_ppc# *ppc;
63 int i;
64
65 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
66 if (ins->detailcxx . . $ cs_insn#detail. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
67 return;
68
69 ppc = &(ins->detailcxx . . $ cs_insn#detail.->ppccxx . . $ cs_detail#$anonymous_type_0#ppc.);
70 if (ppc->op_countcxx . . $ cs_ppc#op_count.)
71 printfcxx . . $ printf(581fddd72a8db8fc).("\top_count: %u\n", ppc->op_countcxx . . $ cs_ppc#op_count.);
72
73 for (i = 0; i < ppc->op_countcxx . . $ cs_ppc#op_count.; i++) {
74 cs_ppc_opcxx . . $ cs_ppc_op# *op = &(ppc->operandscxx . . $ cs_ppc#operands.[i]);
75 switch((int)op->typecxx . . $ cs_ppc_op#type.) {
76 default:
77 break;
78 case PPC_OP_REGcxx . . $ PPC_OP_REG.:
79 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: REG = %s\n", i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->regcxx . . $ cs_ppc_op#$anonymous_type_0#reg.));
80 break;
81 case PPC_OP_IMMcxx . . $ PPC_OP_IMM.:
82 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: IMM = 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! "\n", i, op->immcxx . . $ cs_ppc_op#$anonymous_type_0#imm.);
83 break;
84 case PPC_OP_MEMcxx . . $ PPC_OP_MEM.:
85 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: MEM\n", i);
86 if (op->memcxx . . $ cs_ppc_op#$anonymous_type_0#mem..basecxx . . $ ppc_op_mem#base. != PPC_REG_INVALIDcxx . . $ PPC_REG_INVALID.)
87 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.base: REG = %s\n",
88 i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->memcxx . . $ cs_ppc_op#$anonymous_type_0#mem..basecxx . . $ ppc_op_mem#base.));
89 if (op->memcxx . . $ cs_ppc_op#$anonymous_type_0#mem..dispcxx . . $ ppc_op_mem#disp. != 0)
90 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->memcxx . . $ cs_ppc_op#$anonymous_type_0#mem..dispcxx . . $ ppc_op_mem#disp.);
91
92 break;
93 case PPC_OP_CRXcxx . . $ PPC_OP_CRX.:
94 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: CRX\n", i);
95 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].crx.scale: %d\n", i, op->crxcxx . . $ cs_ppc_op#$anonymous_type_0#crx..scalecxx . . $ ppc_op_crx#scale.);
96 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].crx.reg: %s\n", i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->crxcxx . . $ cs_ppc_op#$anonymous_type_0#crx..regcxx . . $ ppc_op_crx#reg.));
97 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].crx.cond: %s\n", i, get_bc_namecxx . . $ get_bc_name(f7ae7e56280f18ae).(op->crxcxx . . $ cs_ppc_op#$anonymous_type_0#crx..condcxx . . $ ppc_op_crx#cond.));
98 break;
99 }
100 }
101
102 if (ppc->bccxx . . $ cs_ppc#bc. != 0)
103 printfcxx . . $ printf(581fddd72a8db8fc).("\tBranch code: %u\n", ppc->bccxx . . $ cs_ppc#bc.);
104
105 if (ppc->bhcxx . . $ cs_ppc#bh. != 0)
106 printfcxx . . $ printf(581fddd72a8db8fc).("\tBranch hint: %u\n", ppc->bhcxx . . $ cs_ppc#bh.);
107
108 if (ppc->update_cr0cxx . . $ cs_ppc#update_cr0.)
109 printfcxx . . $ printf(581fddd72a8db8fc).("\tUpdate-CR0: True\n");
110
111 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
112 }
113
114 static void testcxx . . $ test(d878ad36e69247c7).()
115 {
116 #define PPC_CODEcxx . . $ `tests/test_ppc.c:116:9`! "\x43\x20\x0c\x07\x41\x56\xff\x17\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21\x40\x82\x00\x14"
117 #define PPC_CODE2cxx . . $ `tests/test_ppc.c:117:9`! "\x10\x60\x2a\x10\x10\x64\x28\x88\x7c\x4a\x5d\x0f"
118 #define PPC_CODE3cxx . . $ `tests/test_ppc.c:118:9`! "\x10\x00\x1f\xec\xe0\x6d\x80\x04\xe4\x6d\x80\x04\x10\x60\x1c\x4c\x10\x60\x1c\x0c\xf0\x6d\x80\x04\xf4\x6d\x80\x04\x10\x60\x1c\x4e\x10\x60\x1c\x0e\x10\x60\x1a\x10\x10\x60\x1a\x11\x10\x63\x20\x2a\x10\x63\x20\x2b\x10\x83\x20\x40\x10\x83\x20\xC0\x10\x83\x20\x00\x10\x83\x20\x80\x10\x63\x20\x24\x10\x63\x20\x25\x10\x63\x29\x3a\x10\x63\x29\x3b\x10\x63\x29\x1c\x10\x63\x29\x1d\x10\x63\x29\x1e\x10\x63\x29\x1f\x10\x63\x24\x20\x10\x63\x24\x21\x10\x63\x24\x60\x10\x63\x24\x61\x10\x63\x24\xA0\x10\x63\x24\xA1\x10\x63\x24\xE0\x10\x63\x24\xE1\x10\x60\x20\x90\x10\x60\x20\x91\x10\x63\x29\x38\x10\x63\x29\x39\x10\x63\x01\x32\x10\x63\x01\x33\x10\x63\x01\x18\x10\x63\x01\x19\x10\x63\x01\x1A\x10\x63\x01\x1B\x10\x60\x19\x10\x10\x60\x19\x11\x10\x60\x18\x50\x10\x60\x18\x51\x10\x63\x29\x3e\x10\x63\x29\x3f\x10\x63\x29\x3c\x10\x63\x29\x3d\x10\x60\x18\x30\x10\x60\x18\x31\x10\x60\x18\x34\x10\x60\x18\x35\x10\x63\x29\x2e\x10\x63\x29\x2f\x10\x63\x20\x28\x10\x63\x20\x29\x10\x63\x29\x14\x10\x63\x29\x15\x10\x63\x29\x16\x10\x63\x29\x17"
119
120 struct platformcxx . . $ platform# platforms[] = {
121 {
122 CS_ARCH_PPCcxx . . $ CS_ARCH_PPC.,
123 CS_MODE_BIG_ENDIANcxx . . $ CS_MODE_BIG_ENDIAN.,
124 (unsigned char*)PPC_CODEcxx . . $ `tests/test_ppc.c:116:9`!,
125 sizeof(PPC_CODEcxx . . $ `tests/test_ppc.c:116:9`!) - 1,
126 "PPC-64",
127 },
128 {
129 CS_ARCH_PPCcxx . . $ CS_ARCH_PPC.,
130 (cs_modecxx . . $ cs_mode#)(CS_MODE_BIG_ENDIANcxx . . $ CS_MODE_BIG_ENDIAN. + CS_MODE_QPXcxx . . $ CS_MODE_QPX.),
131 (unsigned char*)PPC_CODE2cxx . . $ `tests/test_ppc.c:117:9`!,
132 sizeof(PPC_CODE2cxx . . $ `tests/test_ppc.c:117:9`!) - 1,
133 "PPC-64 + QPX",
134 },
135 {
136 CS_ARCH_PPCcxx . . $ CS_ARCH_PPC.,
137 (cs_modecxx . . $ cs_mode#)(CS_MODE_BIG_ENDIANcxx . . $ CS_MODE_BIG_ENDIAN. + CS_MODE_PScxx . . $ CS_MODE_PS.),
138 (unsigned char*)PPC_CODE3cxx . . $ `tests/test_ppc.c:118:9`!,
139 sizeof(PPC_CODE3cxx . . $ `tests/test_ppc.c:118:9`!) - 1,
140 "PPC + PS",
141 },
142 };
143
144 uint64_tcxx . . $ uint64_t# address = 0x1000;
145 cs_insncxx . . $ cs_insn# *insn;
146 int i;
147 size_tcxx . . $ size_t# count;
148
149 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
150 cs_errcxx . . $ cs_err# err = cs_opencxx . . $ cs_open(b2cb849d8ee082cd).(platforms[i].archcxx . . $ platform#arch., platforms[i].modecxx . . $ platform#mode., &handlecxx . . $ handle.);
151 if (err) {
152 printfcxx . . $ printf(581fddd72a8db8fc).("Failed on cs_open() with error returned: %u\n", err);
153 abortcxx . . $ abort(8df32a62017fbbf6).();
154 }
155
156 cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handlecxx . . $ handle., CS_OPT_DETAILcxx . . $ CS_OPT_DETAIL., CS_OPT_ONcxx . . $ CS_OPT_ON.);
157
158 count = cs_disasmcxx . . $ cs_disasm(d552efbd9b22be5).(handlecxx . . $ handle., platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size., address, 0, &insn);
159 if (count) {
160 size_tcxx . . $ size_t# j;
161
162 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
163 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
164 print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
165 printfcxx . . $ printf(581fddd72a8db8fc).("Disasm:\n");
166
167 for (j = 0; j < count; j++) {
168 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.);
169 print_insn_detailcxx . . $ print_insn_detail(ecef60df25d96a55).(&insn[j]);
170 }
171 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.);
172
173 // free memory allocated by cs_disasm()
174 cs_freecxx . . $ cs_free(d0ae7023b33cf0c8).(insn, count);
175 } else {
176 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
177 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
178 print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
179 printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Failed to disasm given code!\n");
180 abortcxx . . $ abort(8df32a62017fbbf6).();
181 }
182
183 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
184
185 cs_closecxx . . $ cs_close(b9ab39fbab147149).(&handlecxx . . $ handle.);
186 }
187 }
188
189 int maincxx . . $ main(d1b40a211a1a4e4c).()
190 {
191 testcxx . . $ test(d878ad36e69247c7).();
192
193 return 0;
194 }