Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial implementation of the Sail-generated RISCV disassembler module #2498

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 0 additions & 106 deletions arch/RISCV/RISCVBaseInfo.h

This file was deleted.

36 changes: 36 additions & 0 deletions arch/RISCV/RISCVDetails.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "RISCVDetails.h"

/*
The size calculation algorithm according to the RISCV spec:
1- Check the first (least-significant) 2 bits...
1.1- If they're not 11, then the instruction is a 16-bits instruction.

2- Otherwise, if they're 11, Check the next 3 bits (3rd-5th)...
2.1- If they're not 111, then the instruction is a 32-bits instruction.

3- Otherwise, if they're 111, check the next (6th) bit...
3.1- If it's not 1, then the instruction is a 48-bits instruction.

4- Otherwise, if it's 1, check the next (7th) bit...
4.1- If it's not 1, then the instruction is 1 64-bits instruction.

5- Otherwise, the instruction size can be determined from other bits further from the first byte.

(The spec actually specifies valid sizes up to 192-bits instructions, even reserving a pattern for
instructions beyond 192 bits. In practice, even 48-bits or 64-bits instructions are rare in practice,
and it's not worth complicating the code with a bitvector type to represent bigger instructions.)
*/
bool riscv_fill_size(cs_insn *insn, uint8_t first_byte) {
if (first_byte & 0x3 != 0x3) {
insn->size = 2;
} else if ((first_byte >> 2) & 0x7 != 0x7) {
insn->size = 4;
} else if ((first_byte >> 5) & 0x1 == 0x0) {
insn->size = 6;
} else if ((first_byte >> 6) & 0x1 == 0x0) {
insn->size = 8;
} else {
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fprintf a warning here that instructions >64bit are not supported yet.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or do it below where you check the result of this function. Either way, just please inform the user about it.

}
return true;
}
3 changes: 3 additions & 0 deletions arch/RISCV/RISCVDetails.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "capstone.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "capstone.h"
#include <capstone/capstone.h>


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