Skip to content

Commit 08d68d1

Browse files
committed
fixed code till it compiled, cstool runs successfully (although with bad output)
1 parent d4bf832 commit 08d68d1

17 files changed

+23666
-115596
lines changed

CMakeLists.txt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -570,24 +570,22 @@ if(CAPSTONE_RISCV_SUPPORT)
570570
add_definitions(-DCAPSTONE_HAS_RISCV)
571571
set(SOURCES_RISCV
572572
arch/RISCV/RISCVDisassembler.c
573-
arch/RISCV/RISCVInstPrinter.c
574-
arch/RISCV/RISCVMapping.c
573+
arch/RISCV/RISCVDetails.c
575574
arch/RISCV/RISCVModule.c
576575
)
577576
set(HEADERS_RISCV
578-
arch/RISCV/RISCVBaseInfo.h
577+
arch/RISCV/riscv_ast.gen.inc
578+
arch/RISCV/riscv_decode.gen.inc
579+
arch/RISCV/riscv_ast2str.gen.inc
580+
arch/RISCV/riscv_ast2str_tbls.gen.inc
581+
"include/capstone/riscv_insn.gen.inc"
582+
arch/RISCV/riscv_insn_mapping.gen.inc
583+
584+
arch/RISCV/RISCVDetails.h
579585
arch/RISCV/RISCVDisassembler.h
580-
arch/RISCV/RISCVInstPrinter.h
581-
arch/RISCV/RISCVMapping.h
586+
arch/RISCV/riscv_helpers_ast2str.h
587+
arch/RISCV/riscv_helpers_rvconf.h
582588
arch/RISCV/RISCVModule.h
583-
arch/RISCV/RISCVGenAsmWriter.inc
584-
arch/RISCV/RISCVGenDisassemblerTables.inc
585-
arch/RISCV/RISCVGenInsnNameMaps.inc
586-
arch/RISCV/RISCVGenInstrInfo.inc
587-
arch/RISCV/RISCVGenRegisterInfo.inc
588-
arch/RISCV/RISCVGenSubtargetInfo.inc
589-
arch/RISCV/RISCVMappingInsn.inc
590-
arch/RISCV/RISCVMappingInsnOp.inc
591589
)
592590
endif()
593591

arch/RISCV/RISCVDetails.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ The size calculation algorithm according to the RISCV spec:
2121
and it's not worth complicating the code with a bitvector type to represent bigger instructions.)
2222
*/
2323
bool riscv_fill_size(cs_insn *insn, uint8_t first_byte) {
24-
if (first_byte & 0x3 != 0x3) {
24+
if ((first_byte & 0x3) != 0x3) {
2525
insn->size = 2;
26-
} else if ((first_byte >> 2) & 0x7 != 0x7) {
26+
} else if (((first_byte >> 2) & 0x7) != 0x7) {
2727
insn->size = 4;
28-
} else if ((first_byte >> 5) & 0x1 == 0x0) {
28+
} else if (((first_byte >> 5) & 0x1) == 0x0) {
2929
insn->size = 6;
30-
} else if ((first_byte >> 6) & 0x1 == 0x0) {
30+
} else if (((first_byte >> 6) & 0x1) == 0x0) {
3131
insn->size = 8;
3232
} else {
3333
return false;

arch/RISCV/RISCVDetails.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#include "capstone.h"
1+
#include "../include/capstone/capstone.h"
22

33
bool riscv_fill_size(cs_insn *insn, uint8_t binary);

arch/RISCV/RISCVDisassembler.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "RISCVDisassembler.h"
2+
#include "RISCVDetails.h"
3+
4+
#include "riscv_decode.gen.inc"
5+
#include "riscv_insn_mapping.gen.inc"
6+
#include "riscv_ast2str.gen.inc"
7+
8+
bool riscv_get_instruction(csh handle,
9+
const uint8_t *code, size_t code_len, MCInst *instr,
10+
uint16_t *size, uint64_t address, void *info) {
11+
cs_insn *insn = instr->flat_insn;
12+
13+
if (!riscv_fill_size(insn, code[0])) {
14+
return false;
15+
}
16+
// TODO: add compressed 2-bytes instructions
17+
if (insn->size == 2) {
18+
19+
} else if (insn->size == 4) {
20+
struct ast instruction;
21+
decode(&instruction, code[3] << 24 | code[2] << 16 | code[1] << 8 | code[0]);
22+
23+
insn->id = get_insn_type(&instruction);
24+
insn->address = address;
25+
*size = insn->size;
26+
memcpy(insn->bytes, code, insn->size);
27+
28+
char instruction_as_str[RISCV_MAX_INSTRUCTION_STR_LEN];
29+
riscv_conf conf;
30+
conf.sys_enable_fdext = NULL;
31+
conf.sys_enable_zfinx = NULL;
32+
33+
ast2str(&instruction, instruction_as_str, &conf);
34+
35+
char *curr = instruction_as_str;
36+
uint16_t mnemonic_len = 0;
37+
while (*curr != ' ') {
38+
mnemonic_len++;
39+
curr++;
40+
}
41+
uint16_t operand_len = 0;
42+
while (*curr) {
43+
operand_len++;
44+
curr++;
45+
}
46+
memcpy(insn->mnemonic, instruction_as_str, mnemonic_len);
47+
memcpy(insn->op_str, instruction_as_str + mnemonic_len + 1, operand_len);
48+
return true;
49+
} else {
50+
51+
}
52+
return false;
53+
}
File renamed without changes.

arch/RISCV/RISCVDisassembly.c

Lines changed: 0 additions & 15 deletions
This file was deleted.

arch/RISCV/RISCVModule.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,38 @@
22
/* RISC-V Backend By Rodrigo Cortes Porto <[email protected]> &
33
Shawn Chang <[email protected]>, HardenedLinux@2018 */
44

5-
//#ifdef CAPSTONE_HAS_RISCV
5+
#ifdef CAPSTONE_HAS_RISCV
66

77
#include "RISCVModule.h"
88

9+
void noop_printer(MCInst *MI, SStream *OS, void *info) {
10+
11+
}
12+
13+
void noop_postprinter(csh handle, cs_insn *, SStream *mnem, MCInst *mci) {
14+
15+
}
16+
17+
const char *noop_getname(csh handle, unsigned int id) {
18+
return "";
19+
}
20+
21+
void noop_getid(cs_struct *h, cs_insn *insn, unsigned int id) {
22+
23+
}
24+
925
cs_err RISCV_global_init(cs_struct * ud)
1026
{
11-
MCRegisterInfo *mri;
12-
mri = cs_mem_malloc(sizeof(*mri));
13-
14-
RISCV_init(mri);
15-
ud->printer = RISCV_printInst;
16-
ud->printer_info = mri;
17-
ud->getinsn_info = mri;
18-
ud->disasm = RISCV_getInstruction;
19-
ud->post_printer = NULL;
27+
ud->printer = noop_printer;
28+
ud->printer_info = NULL;
29+
ud->getinsn_info = NULL;
30+
ud->disasm = riscv_get_instruction;
31+
ud->post_printer = noop_postprinter;
2032

21-
ud->reg_name = RISCV_reg_name;
22-
ud->insn_id = RISCV_get_insn_id;
23-
ud->insn_name = RISCV_insn_name;
24-
ud->group_name = RISCV_group_name;
33+
ud->reg_name = noop_getname;
34+
ud->insn_id = noop_getid;
35+
ud->insn_name = noop_getname;
36+
ud->group_name = noop_getname;
2537

2638
return CS_ERR_OK;
2739
}
@@ -37,4 +49,4 @@ cs_err RISCV_option(cs_struct * handle, cs_opt_type type, size_t value)
3749
return CS_ERR_OK;
3850
}
3951

40-
//#endif
52+
#endif

arch/RISCV/RISCVModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define CS_RISCV_MODULE_H
66

77
#include "../../utils.h"
8+
#include "RISCVDisassembler.h"
89

910
cs_err RISCV_global_init(cs_struct * ud);
1011
cs_err RISCV_option(cs_struct * handle, cs_opt_type type, size_t value);

0 commit comments

Comments
 (0)