| 1 | /* Capstone Disassembly Engine */cxx . . $ `<file>/tests/test_tms320c64x.c`/ |
| 2 | /* TMS320C64x Backend by Fotis Loukos <[email protected]> 2016 */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #include <capstone/capstone.h>cxx . . $ `<file>/include/capstone/capstone.h`/ |
| 7 | |
| 8 | struct platformcxx . . $ platform# { |
| 9 | cs_archcxx . . $ cs_arch# archcxx . . $ platform#arch.; |
| 10 | cs_modecxx . . $ cs_mode# modecxx . . $ platform#mode.; |
| 11 | unsigned char *codecxx . . $ platform#code.; |
| 12 | size_t sizecxx . . $ platform#size.; |
| 13 | const char *commentcxx . . $ platform#comment.; |
| 14 | }; |
| 15 | |
| 16 | static cshcxx . . $ csh# handlecxx . . $ handle.; |
| 17 | |
| 18 | static void print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).(const char *comment, unsigned char *str, size_t len) |
| 19 | { |
| 20 | unsigned char *c; |
| 21 | |
| 22 | printfcxx . . $ printf(581fddd72a8db8fc).("%s", comment); |
| 23 | for (c = str; c < str + len; c++) { |
| 24 | printfcxx . . $ printf(581fddd72a8db8fc).("0x%02x ", *c & 0xff); |
| 25 | } |
| 26 | |
| 27 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 28 | } |
| 29 | |
| 30 | static void print_insn_detailcxx . . $ print_insn_detail(ecef60df25d96a55).(cs_insncxx . . $ cs_insn# *ins) |
| 31 | { |
| 32 | cs_tms320c64xcxx . . $ cs_tms320c64x# *tms320c64x; |
| 33 | int i; |
| 34 | |
| 35 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 36 | if (ins->detailcxx . . $ cs_insn#detail. == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) |
| 37 | return; |
| 38 | |
| 39 | tms320c64x = &(ins->detailcxx . . $ cs_insn#detail.->tms320c64xcxx . . $ cs_detail#$anonymous_type_0#tms320c64x.); |
| 40 | if (tms320c64x->op_countcxx . . $ cs_tms320c64x#op_count.) |
| 41 | printfcxx . . $ printf(581fddd72a8db8fc).("\top_count: %u\n", tms320c64x->op_countcxx . . $ cs_tms320c64x#op_count.); |
| 42 | |
| 43 | for (i = 0; i < tms320c64x->op_countcxx . . $ cs_tms320c64x#op_count.; i++) { |
| 44 | cs_tms320c64x_opcxx . . $ cs_tms320c64x_op# *op = &(tms320c64x->operandscxx . . $ cs_tms320c64x#operands.[i]); |
| 45 | switch((int)op->typecxx . . $ cs_tms320c64x_op#type.) { |
| 46 | default: |
| 47 | break; |
| 48 | case TMS320C64X_OP_REGcxx . . $ TMS320C64X_OP_REG.: |
| 49 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: REG = %s\n", i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->regcxx . . $ cs_tms320c64x_op#$anonymous_type_0#reg.)); |
| 50 | break; |
| 51 | case TMS320C64X_OP_IMMcxx . . $ TMS320C64X_OP_IMM.: |
| 52 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: IMM = 0x%x\n", i, op->immcxx . . $ cs_tms320c64x_op#$anonymous_type_0#imm.); |
| 53 | break; |
| 54 | case TMS320C64X_OP_MEMcxx . . $ TMS320C64X_OP_MEM.: |
| 55 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: MEM\n", i); |
| 56 | if (op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..basecxx . . $ tms320c64x_op_mem#base. != TMS320C64X_REG_INVALIDcxx . . $ TMS320C64X_REG_INVALID.) |
| 57 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.base: REG = %s\n", |
| 58 | i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..basecxx . . $ tms320c64x_op_mem#base.)); |
| 59 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.disptype: ", i); |
| 60 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..disptypecxx . . $ tms320c64x_op_mem#disptype. == TMS320C64X_MEM_DISP_INVALIDcxx . . $ TMS320C64X_MEM_DISP_INVALID.) { |
| 61 | printfcxx . . $ printf(581fddd72a8db8fc).("Invalid\n"); |
| 62 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.disp: %u\n", i, op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..dispcxx . . $ tms320c64x_op_mem#disp.); |
| 63 | } |
| 64 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..disptypecxx . . $ tms320c64x_op_mem#disptype. == TMS320C64X_MEM_DISP_CONSTANTcxx . . $ TMS320C64X_MEM_DISP_CONSTANT.) { |
| 65 | printfcxx . . $ printf(581fddd72a8db8fc).("Constant\n"); |
| 66 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.disp: %u\n", i, op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..dispcxx . . $ tms320c64x_op_mem#disp.); |
| 67 | } |
| 68 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..disptypecxx . . $ tms320c64x_op_mem#disptype. == TMS320C64X_MEM_DISP_REGISTERcxx . . $ TMS320C64X_MEM_DISP_REGISTER.) { |
| 69 | printfcxx . . $ printf(581fddd72a8db8fc).("Register\n"); |
| 70 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.disp: %s\n", i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..dispcxx . . $ tms320c64x_op_mem#disp.)); |
| 71 | } |
| 72 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.unit: %u\n", i, op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..unitcxx . . $ tms320c64x_op_mem#unit.); |
| 73 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.direction: ", i); |
| 74 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..directioncxx . . $ tms320c64x_op_mem#direction. == TMS320C64X_MEM_DIR_INVALIDcxx . . $ TMS320C64X_MEM_DIR_INVALID.) |
| 75 | printfcxx . . $ printf(581fddd72a8db8fc).("Invalid\n"); |
| 76 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..directioncxx . . $ tms320c64x_op_mem#direction. == TMS320C64X_MEM_DIR_FWcxx . . $ TMS320C64X_MEM_DIR_FW.) |
| 77 | printfcxx . . $ printf(581fddd72a8db8fc).("Forward\n"); |
| 78 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..directioncxx . . $ tms320c64x_op_mem#direction. == TMS320C64X_MEM_DIR_BWcxx . . $ TMS320C64X_MEM_DIR_BW.) |
| 79 | printfcxx . . $ printf(581fddd72a8db8fc).("Backward\n"); |
| 80 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.modify: ", i); |
| 81 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..modifycxx . . $ tms320c64x_op_mem#modify. == TMS320C64X_MEM_MOD_INVALIDcxx . . $ TMS320C64X_MEM_MOD_INVALID.) |
| 82 | printfcxx . . $ printf(581fddd72a8db8fc).("Invalid\n"); |
| 83 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..modifycxx . . $ tms320c64x_op_mem#modify. == TMS320C64X_MEM_MOD_NOcxx . . $ TMS320C64X_MEM_MOD_NO.) |
| 84 | printfcxx . . $ printf(581fddd72a8db8fc).("No\n"); |
| 85 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..modifycxx . . $ tms320c64x_op_mem#modify. == TMS320C64X_MEM_MOD_PREcxx . . $ TMS320C64X_MEM_MOD_PRE.) |
| 86 | printfcxx . . $ printf(581fddd72a8db8fc).("Pre\n"); |
| 87 | if(op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..modifycxx . . $ tms320c64x_op_mem#modify. == TMS320C64X_MEM_MOD_POSTcxx . . $ TMS320C64X_MEM_MOD_POST.) |
| 88 | printfcxx . . $ printf(581fddd72a8db8fc).("Post\n"); |
| 89 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.scaled: %u\n", i, op->memcxx . . $ cs_tms320c64x_op#$anonymous_type_0#mem..scaledcxx . . $ tms320c64x_op_mem#scaled.); |
| 90 | |
| 91 | |
| 92 | break; |
| 93 | case TMS320C64X_OP_REGPAIRcxx . . $ TMS320C64X_OP_REGPAIR.: |
| 94 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: REGPAIR = %s:%s\n", i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->regcxx . . $ cs_tms320c64x_op#$anonymous_type_0#reg. + 1), cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->regcxx . . $ cs_tms320c64x_op#$anonymous_type_0#reg.)); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | printfcxx . . $ printf(581fddd72a8db8fc).("\tFunctional unit: "); |
| 100 | switch(tms320c64x->funitcxx . . $ cs_tms320c64x#funit..unitcxx . . $ cs_tms320c64x#$anonymous_type_2#unit.) { |
| 101 | case TMS320C64X_FUNIT_Dcxx . . $ TMS320C64X_FUNIT_D.: |
| 102 | printfcxx . . $ printf(581fddd72a8db8fc).("D%u\n", tms320c64x->funitcxx . . $ cs_tms320c64x#funit..sidecxx . . $ cs_tms320c64x#$anonymous_type_2#side.); |
| 103 | break; |
| 104 | case TMS320C64X_FUNIT_Lcxx . . $ TMS320C64X_FUNIT_L.: |
| 105 | printfcxx . . $ printf(581fddd72a8db8fc).("L%u\n", tms320c64x->funitcxx . . $ cs_tms320c64x#funit..sidecxx . . $ cs_tms320c64x#$anonymous_type_2#side.); |
| 106 | break; |
| 107 | case TMS320C64X_FUNIT_Mcxx . . $ TMS320C64X_FUNIT_M.: |
| 108 | printfcxx . . $ printf(581fddd72a8db8fc).("M%u\n", tms320c64x->funitcxx . . $ cs_tms320c64x#funit..sidecxx . . $ cs_tms320c64x#$anonymous_type_2#side.); |
| 109 | break; |
| 110 | case TMS320C64X_FUNIT_Scxx . . $ TMS320C64X_FUNIT_S.: |
| 111 | printfcxx . . $ printf(581fddd72a8db8fc).("S%u\n", tms320c64x->funitcxx . . $ cs_tms320c64x#funit..sidecxx . . $ cs_tms320c64x#$anonymous_type_2#side.); |
| 112 | break; |
| 113 | case TMS320C64X_FUNIT_NOcxx . . $ TMS320C64X_FUNIT_NO.: |
| 114 | printfcxx . . $ printf(581fddd72a8db8fc).("No Functional Unit\n"); |
| 115 | break; |
| 116 | default: |
| 117 | printfcxx . . $ printf(581fddd72a8db8fc).("Unknown (Unit %u, Side %u)\n", tms320c64x->funitcxx . . $ cs_tms320c64x#funit..unitcxx . . $ cs_tms320c64x#$anonymous_type_2#unit., tms320c64x->funitcxx . . $ cs_tms320c64x#funit..sidecxx . . $ cs_tms320c64x#$anonymous_type_2#side.); |
| 118 | break; |
| 119 | } |
| 120 | if(tms320c64x->funitcxx . . $ cs_tms320c64x#funit..crosspathcxx . . $ cs_tms320c64x#$anonymous_type_2#crosspath. == 1) |
| 121 | printfcxx . . $ printf(581fddd72a8db8fc).("\tCrosspath: 1\n"); |
| 122 | |
| 123 | if(tms320c64x->conditioncxx . . $ cs_tms320c64x#condition..regcxx . . $ cs_tms320c64x#$anonymous_type_1#reg. != TMS320C64X_REG_INVALIDcxx . . $ TMS320C64X_REG_INVALID.) |
| 124 | printfcxx . . $ printf(581fddd72a8db8fc).("\tCondition: [%c%s]\n", (tms320c64x->conditioncxx . . $ cs_tms320c64x#condition..zerocxx . . $ cs_tms320c64x#$anonymous_type_1#zero. == 1) ? '!' : ' ', cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., tms320c64x->conditioncxx . . $ cs_tms320c64x#condition..regcxx . . $ cs_tms320c64x#$anonymous_type_1#reg.)); |
| 125 | printfcxx . . $ printf(581fddd72a8db8fc).("\tParallel: %s\n", (tms320c64x->parallelcxx . . $ cs_tms320c64x#parallel. == 1) ? "true" : "false"); |
| 126 | |
| 127 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 128 | } |
| 129 | |
| 130 | static void testcxx . . $ test(d878ad36e69247c7).() |
| 131 | { |
| 132 | #define TMS320C64X_CODEcxx . . $ `tests/test_tms320c64x.c:132:9`! "\x01\xac\x88\x40\x81\xac\x88\x43\x00\x00\x00\x00\x02\x90\x32\x96\x02\x80\x46\x9e\x05\x3c\x83\xe6\x0b\x0c\x8b\x24" |
| 133 | |
| 134 | struct platformcxx . . $ platform# platforms[] = { |
| 135 | { |
| 136 | CS_ARCH_TMS320C64Xcxx . . $ CS_ARCH_TMS320C64X., |
| 137 | CS_MODE_BIG_ENDIANcxx . . $ CS_MODE_BIG_ENDIAN., |
| 138 | (unsigned char*)TMS320C64X_CODEcxx . . $ `tests/test_tms320c64x.c:132:9`!, |
| 139 | sizeof(TMS320C64X_CODEcxx . . $ `tests/test_tms320c64x.c:132:9`!) - 1, |
| 140 | "TMS320C64x", |
| 141 | }, |
| 142 | }; |
| 143 | |
| 144 | uint64_tcxx . . $ uint64_t# address = 0x1000; |
| 145 | cs_insncxx . . $ cs_insn# *insn; |
| 146 | int i; |
| 147 | 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 | continue; |
| 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_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 | } |
| 181 | |
| 182 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 183 | |
| 184 | cs_closecxx . . $ cs_close(b9ab39fbab147149).(&handlecxx . . $ handle.); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | int maincxx . . $ main(d1b40a211a1a4e4c).() |
| 189 | { |
| 190 | testcxx . . $ test(d878ad36e69247c7).(); |
| 191 | |
| 192 | return 0; |
| 193 | } |