1
+ #include "RISCVDetails.h"
2
+
3
+ /*
4
+ The size calculation algorithm according to the RISCV spec:
5
+ 1- Check the first (least-significant) 2 bits...
6
+ 1.1- If they're not 11, then the instruction is a 16-bits instruction.
7
+
8
+ 2- Otherwise, if they're 11, Check the next 3 bits (3rd-5th)...
9
+ 2.1- If they're not 111, then the instruction is a 32-bits instruction.
10
+
11
+ 3- Otherwise, if they're 111, check the next (6th) bit...
12
+ 3.1- If it's not 1, then the instruction is a 48-bits instruction.
13
+
14
+ 4- Otherwise, if it's 1, check the next (7th) bit...
15
+ 4.1- If it's not 1, then the instruction is 1 64-bits instruction.
16
+
17
+ 5- Otherwise, the instruction size can be determined from other bits further from the first byte.
18
+
19
+ (The spec actually specifies valid sizes up to 192-bits instructions, even reserving a pattern for
20
+ instructions beyond 192 bits. In practice, even 48-bits or 64-bits instructions are rare in practice,
21
+ and it's not worth complicating the code with a bitvector type to represent bigger instructions.)
22
+ */
23
+ bool riscv_fill_size (cs_insn * insn , uint8_t first_byte ) {
24
+ if (first_byte & 0x3 != 0x3 ) {
25
+ insn -> size = 2 ;
26
+ } else if ((first_byte >> 2 ) & 0x7 != 0x7 ) {
27
+ insn -> size = 4 ;
28
+ } else if ((first_byte >> 5 ) & 0x1 == 0x0 ) {
29
+ insn -> size = 6 ;
30
+ } else if ((first_byte >> 6 ) & 0x1 == 0x0 ) {
31
+ insn -> size = 8 ;
32
+ } else {
33
+ return false;
34
+ }
35
+ return true;
36
+ }
0 commit comments