| 1 | #cxx . . $ `<file>/tests/test_riscv.c`/include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include <capstone/platform.h>cxx . . $ `<file>/include/capstone/platform.h`/ |
| 5 | #include <capstone/capstone.h>cxx . . $ `<file>/include/capstone/capstone.h`/ |
| 6 | |
| 7 | struct platformcxx . . $ platform# { |
| 8 | cs_archcxx . . $ cs_arch# archcxx . . $ platform#arch.; |
| 9 | cs_modecxx . . $ cs_mode# modecxx . . $ platform#mode.; |
| 10 | unsigned char *codecxx . . $ platform#code.; |
| 11 | size_tcxx . . $ size_t# sizecxx . . $ platform#size.; |
| 12 | const char *commentcxx . . $ platform#comment.; |
| 13 | }; |
| 14 | |
| 15 | static cshcxx . . $ csh# handlecxx . . $ handle.; |
| 16 | |
| 17 | static void print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).(const char *comment, unsigned char *str, size_tcxx . . $ size_t# len) |
| 18 | { |
| 19 | unsigned char *c; |
| 20 | |
| 21 | printfcxx . . $ printf(581fddd72a8db8fc).("%s", comment); |
| 22 | for (c = str; c < str + len; c++) { |
| 23 | printfcxx . . $ printf(581fddd72a8db8fc).("0x%02x ", *c & 0xff); |
| 24 | } |
| 25 | |
| 26 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 27 | } |
| 28 | |
| 29 | static void print_insn_detailcxx . . $ print_insn_detail(ecef60df25d96a55).(cs_insncxx . . $ cs_insn# *ins) |
| 30 | { |
| 31 | int i; |
| 32 | int n; |
| 33 | cs_riscvcxx . . $ cs_riscv# *riscv; |
| 34 | cs_detailcxx . . $ cs_detail# *detail; |
| 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 | riscv = &(ins->detailcxx . . $ cs_insn#detail.->riscvcxx . . $ cs_detail#$anonymous_type_0#riscv.); |
| 41 | detail = ins->detailcxx . . $ cs_insn#detail.; |
| 42 | if (riscv->op_countcxx . . $ cs_riscv#op_count.) |
| 43 | printfcxx . . $ printf(581fddd72a8db8fc).("\top_count: %u\n", riscv->op_countcxx . . $ cs_riscv#op_count.); |
| 44 | |
| 45 | for (i = 0; i < riscv->op_countcxx . . $ cs_riscv#op_count.; i++) { |
| 46 | cs_riscv_opcxx . . $ cs_riscv_op# *op = &(riscv->operandscxx . . $ cs_riscv#operands.[i]); |
| 47 | switch((int)op->typecxx . . $ cs_riscv_op#type.) { |
| 48 | default: |
| 49 | printfcxx . . $ printf(581fddd72a8db8fc).("\terror in opt_type: %u\n", (int)op->typecxx . . $ cs_riscv_op#type.); |
| 50 | break; |
| 51 | case RISCV_OP_REGcxx . . $ RISCV_OP_REG.: |
| 52 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: REG = %s\n", i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->regcxx . . $ cs_riscv_op#$anonymous_type_0#reg.)); |
| 53 | break; |
| 54 | case RISCV_OP_IMMcxx . . $ RISCV_OP_IMM.: |
| 55 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: IMM = 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! "\n", i, op->immcxx . . $ cs_riscv_op#$anonymous_type_0#imm.); |
| 56 | break; |
| 57 | case RISCV_OP_MEMcxx . . $ RISCV_OP_MEM.: |
| 58 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: MEM\n", i); |
| 59 | if (op->memcxx . . $ cs_riscv_op#$anonymous_type_0#mem..basecxx . . $ riscv_op_mem#base. != RISCV_REG_INVALIDcxx . . $ RISCV_REG_INVALID.) |
| 60 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.base: REG = %s\n", |
| 61 | i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->memcxx . . $ cs_riscv_op#$anonymous_type_0#mem..basecxx . . $ riscv_op_mem#base.)); |
| 62 | if (op->memcxx . . $ cs_riscv_op#$anonymous_type_0#mem..dispcxx . . $ riscv_op_mem#disp. != 0) |
| 63 | printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.disp: 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! "\n", i, op->memcxx . . $ cs_riscv_op#$anonymous_type_0#mem..dispcxx . . $ riscv_op_mem#disp.); |
| 64 | |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | //print the groups this instruction belongs to |
| 71 | if (detail->groups_countcxx . . $ cs_detail#groups_count. > 0) { |
| 72 | printfcxx . . $ printf(581fddd72a8db8fc).("\tThis instruction belongs to groups: "); |
| 73 | for (n = 0; n < detail->groups_countcxx . . $ cs_detail#groups_count.; n++) { |
| 74 | printfcxx . . $ printf(581fddd72a8db8fc).("%s ", cs_group_namecxx . . $ cs_group_name(efd1348d9ae5172e).(handlecxx . . $ handle., detail->groupscxx . . $ cs_detail#groups.[n])); |
| 75 | } |
| 76 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 77 | } |
| 78 | |
| 79 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 80 | } |
| 81 | |
| 82 | static void testcxx . . $ test(d878ad36e69247c7).() |
| 83 | { |
| 84 | #define RISCV_CODE32cxx . . $ `tests/test_riscv.c:84:9`! "\x37\x34\x00\x00\x97\x82\x00\x00\xef\x00\x80\x00\xef\xf0\x1f\xff\xe7\x00\x45\x00\xe7\x00\xc0\xff\x63\x05\x41\x00\xe3\x9d\x61\xfe\x63\xca\x93\x00\x63\x53\xb5\x00\x63\x65\xd6\x00\x63\x76\xf7\x00\x03\x88\x18\x00\x03\x99\x49\x00\x03\xaa\x6a\x00\x03\xcb\x2b\x01\x03\xdc\x8c\x01\x23\x86\xad\x03\x23\x9a\xce\x03\x23\x8f\xef\x01\x93\x00\xe0\x00\x13\xa1\x01\x01\x13\xb2\x02\x7d\x13\xc3\x03\xdd\x13\xe4\xc4\x12\x13\xf5\x85\x0c\x13\x96\xe6\x01\x13\xd7\x97\x01\x13\xd8\xf8\x40\x33\x89\x49\x01\xb3\x0a\x7b\x41\x33\xac\xac\x01\xb3\x3d\xde\x01\x33\xd2\x62\x40\xb3\x43\x94\x00\x33\xe5\xc5\x00\xb3\x76\xf7\x00\xb3\x54\x39\x01\xb3\x50\x31\x00\x33\x9f\x0f\x00" |
| 85 | #define RISCV_CODE64cxx . . $ `tests/test_riscv.c:85:9`! "\x13\x04\xa8\x7a" // aaa80413 |
| 86 | struct platformcxx . . $ platform# platforms[] = { |
| 87 | { |
| 88 | CS_ARCH_RISCVcxx . . $ CS_ARCH_RISCV., |
| 89 | CS_MODE_RISCV32cxx . . $ CS_MODE_RISCV32., |
| 90 | (unsigned char *)RISCV_CODE32cxx . . $ `tests/test_riscv.c:84:9`!, |
| 91 | sizeof(RISCV_CODE32cxx . . $ `tests/test_riscv.c:84:9`!) - 1, |
| 92 | "riscv32" |
| 93 | }, |
| 94 | { |
| 95 | CS_ARCH_RISCVcxx . . $ CS_ARCH_RISCV., |
| 96 | CS_MODE_RISCV64cxx . . $ CS_MODE_RISCV64., |
| 97 | (unsigned char *)RISCV_CODE64cxx . . $ `tests/test_riscv.c:85:9`!, |
| 98 | sizeof(RISCV_CODE64cxx . . $ `tests/test_riscv.c:85:9`!) - 1, |
| 99 | "riscv64" |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | uint64_tcxx . . $ uint64_t# address = 0x1000; |
| 104 | cs_insncxx . . $ cs_insn# *insn; |
| 105 | int i; |
| 106 | size_tcxx . . $ size_t# count; |
| 107 | |
| 108 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
| 109 | cs_errcxx . . $ cs_err# err = cs_opencxx . . $ cs_open(b2cb849d8ee082cd).(platforms[i].archcxx . . $ platform#arch., platforms[i].modecxx . . $ platform#mode., &handlecxx . . $ handle.); |
| 110 | if (err) { |
| 111 | printfcxx . . $ printf(581fddd72a8db8fc).("Failed on cs_open() with error returned: %u\n", err); |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | //To turn on or off the Print Details option |
| 116 | //cs_option(handle, CS_OPT_DETAIL, CS_OPT_OFF); |
| 117 | cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handlecxx . . $ handle., CS_OPT_DETAILcxx . . $ CS_OPT_DETAIL., CS_OPT_ONcxx . . $ CS_OPT_ON.); |
| 118 | |
| 119 | count = cs_disasmcxx . . $ cs_disasm(d552efbd9b22be5).(handlecxx . . $ handle., platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size., address, 0, &insn); |
| 120 | if (count) { |
| 121 | size_tcxx . . $ size_t# j; |
| 122 | |
| 123 | printfcxx . . $ printf(581fddd72a8db8fc).("****************\n"); |
| 124 | printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.); |
| 125 | print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.); |
| 126 | printfcxx . . $ printf(581fddd72a8db8fc).("Disasm:\n"); |
| 127 | |
| 128 | for (j = 0; j < count; j++) { |
| 129 | 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.); |
| 130 | print_insn_detailcxx . . $ print_insn_detail(ecef60df25d96a55).(&insn[j]); |
| 131 | } |
| 132 | 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.); |
| 133 | |
| 134 | // free memory allocated by cs_disasm() |
| 135 | cs_freecxx . . $ cs_free(d0ae7023b33cf0c8).(insn, count); |
| 136 | } else { |
| 137 | printfcxx . . $ printf(581fddd72a8db8fc).("****************\n"); |
| 138 | printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.); |
| 139 | print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.); |
| 140 | printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Failed to disasm given code!\n"); |
| 141 | } |
| 142 | |
| 143 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 144 | |
| 145 | cs_closecxx . . $ cs_close(b9ab39fbab147149).(&handlecxx . . $ handle.); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | int maincxx . . $ main(d1b40a211a1a4e4c).() |
| 150 | { |
| 151 | testcxx . . $ test(d878ad36e69247c7).(); |
| 152 | |
| 153 | return 0; |
| 154 | } |