Skip to content

Commit bd20573

Browse files
committed
Remove the call group from JAL and JALR instructions because they're not always calls, formatting
1 parent 1a87d2e commit bd20573

8 files changed

Lines changed: 85 additions & 33 deletions

File tree

MathExtras.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ static inline bool isIntN(unsigned N, int64_t x)
6363
}
6464

6565
/// isShiftedIntN - Checks if a signed integer is an N bit number shifted left by S.
66-
static inline bool isShiftedIntN(unsigned N, unsigned S, int64_t x) {
67-
return isIntN(N + S, x) && (x % (UINT64_C(1) << S) == 0);
66+
static inline bool isShiftedIntN(unsigned N, unsigned S, int64_t x)
67+
{
68+
return isIntN(N + S, x) && (x % (UINT64_C(1) << S) == 0);
6869
}
6970

70-
static inline bool isShiftedUIntN(unsigned N, unsigned S, uint64_t x) {
71-
return isUIntN(N + S, x) && (x % (UINT64_C(1) << S) == 0);
71+
static inline bool isShiftedUIntN(unsigned N, unsigned S, uint64_t x)
72+
{
73+
return isUIntN(N + S, x) && (x % (UINT64_C(1) << S) == 0);
7274
}
7375

7476
/// isMask_32 - This function returns true if the argument is a sequence of ones

arch/RISCV/RISCVGenCSMappingInsn.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82962,7 +82962,7 @@
8296282962
/* c.jr $rs1 */
8296382963
RISCV_C_JR /* 11772 */, RISCV_INS_C_JR,
8296482964
#ifndef CAPSTONE_DIET
82965-
{ 0 }, { 0 }, { RISCV_GRP_JUMP, RISCV_FEATURE_HASSTDEXTCORZCA, 0 }, 1, 0, {{ 0 }}
82965+
{ 0 }, { 0 }, { RISCV_FEATURE_HASSTDEXTCORZCA, 0 }, 0, 0, {{ 0 }}
8296682966

8296782967
#endif
8296882968
},
@@ -85504,15 +85504,15 @@
8550485504
/* jal $rd, $imm20 */
8550585505
RISCV_JAL /* 12092 */, RISCV_INS_JAL,
8550685506
#ifndef CAPSTONE_DIET
85507-
{ 0 }, { 0 }, { RISCV_GRP_CALL, RISCV_GRP_BRANCH_RELATIVE, 0 }, 0, 0, {{ 0 }}
85507+
{ 0 }, { 0 }, { 0 }, 0, 0, {{ 0 }}
8550885508

8550985509
#endif
8551085510
},
8551185511
{
8551285512
/* jalr $rd, ${imm12}(${rs1}) */
8551385513
RISCV_JALR /* 12093 */, RISCV_INS_JALR,
8551485514
#ifndef CAPSTONE_DIET
85515-
{ 0 }, { 0 }, { RISCV_GRP_CALL, 0 }, 0, 0, {{ 0 }}
85515+
{ 0 }, { 0 }, { 0 }, 0, 0, {{ 0 }}
8551685516

8551785517
#endif
8551885518
},

arch/RISCV/RISCVMapping.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
#include "capstone/cs_operand.h"
23
#include "capstone/riscv.h"
34
#include <stdint.h>
45
#ifdef CAPSTONE_HAS_RISCV
@@ -166,11 +167,43 @@ static inline void RISCV_add_interrupt_ret_group(MCInst *MI)
166167
}
167168
}
168169

170+
// calls are implemented in RISCV as plain jumps that happen to set a link register containing the return address
171+
// but this link register could be given as the null register x0, discarding the return address and making them jumps
172+
static inline void RISCV_add_call_group(MCInst *MI)
173+
{
174+
if (MI->Opcode == RISCV_JAL || MI->Opcode == RISCV_JALR) {
175+
cs_riscv_op op = MI->flat_insn->detail->riscv.operands[0];
176+
if ((op.type == (riscv_op_type)CS_OP_REG) &&
177+
op.reg != RISCV_REG_X0 && (op.access & CS_AC_WRITE)) {
178+
add_group(MI, RISCV_GRP_CALL);
179+
}
180+
if (MI->Opcode == RISCV_JAL) {
181+
add_group(MI, RISCV_GRP_BRANCH_RELATIVE);
182+
}
183+
}
184+
}
185+
186+
// returns are implemented in RISCV as a plain indirect jump that happen to reference the return address register ra == x1
187+
static inline void RISCV_add_ret_group(MCInst *MI)
188+
{
189+
if (MI->Opcode == RISCV_C_JR) {
190+
cs_riscv_op op = MI->flat_insn->detail->riscv.operands[0];
191+
if ((op.type == (riscv_op_type)CS_OP_REG) &&
192+
op.reg == RISCV_REG_X1) {
193+
add_group(MI, RISCV_GRP_RET);
194+
} else {
195+
add_group(MI, RISCV_GRP_JUMP);
196+
}
197+
}
198+
}
199+
169200
static inline void RISCV_add_adhoc_groups(MCInst *MI)
170201
{
171202
RISCV_add_privileged_group(MI);
172203
RISCV_add_interrupt_group(MI);
173204
RISCV_add_interrupt_ret_group(MI);
205+
RISCV_add_call_group(MI);
206+
RISCV_add_ret_group(MI);
174207
}
175208

176209
// for weird reasons some instructions end up with valid operands that are

bindings/python/capstone/riscv_const.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
from . import (CS_OP_FP, CS_OP_IMM, CS_OP_INVALID, CS_OP_MEM, CS_OP_MEM_IMM, CS_OP_MEM_REG, CS_OP_PRED, CS_OP_REG, CS_OP_SPECIAL, UINT8_MAX, UINT16_MAX,)
22

33
# For Capstone Engine. AUTO-GENERATED FILE DO NOT EDIT [riscv_const.py]
4+
#
5+
# ####################################################### ###########################################################
6+
# ####################################################### ###########################################################
7+
# ##################################################### TODO ########################################################
8+
# ########################### This file is supposed to be generated automatically by the bindings generator
9+
# ########################### But the generator is currently broken, so for now all new constants are manually added
10+
# ########################### Fix and ensure all the constants are still correctly added when the generator is fixed
11+
# # ####################################################### ###########################################################
12+
# ####################################################### ###########################################################
13+
# ####################################################### ###########################################################
414

515
# Operand type for instruction's operands
616
RISCV_OP_INVALID = CS_OP_INVALID
@@ -2938,6 +2948,10 @@
29382948
RISCV_SYSREG_MCONFIGPTR = 0xf15
29392949
RISCV_SYSREG_MTOPI = 0xfb0
29402950

2951+
# ####################################################### ###########################################################
2952+
# ####################################################### ###########################################################
2953+
# ####################################################### ###########################################################
2954+
# Add functionality to the generator script to generate this from the SYSREG array
29412955
SYSREG_NAME_TO_VAL = {
29422956
"fflags": RISCV_SYSREG_FFLAGS, "frm": RISCV_SYSREG_FRM, "fcsr": RISCV_SYSREG_FCSR,
29432957
"vstart": RISCV_SYSREG_VSTART, "vxsat": RISCV_SYSREG_VXSAT, "vxrm": RISCV_SYSREG_VXRM,

cstool/cstool.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ static struct {
6464
{ CS_ARCH_LOONGARCH, CS_ARCH_MIPS, CS_ARCH_MAX },
6565
CS_OPT_SYNTAX_NO_DOLLAR,
6666
0 },
67-
{ "+noalias", "Does not print the text alias of an alias instruction", {
68-
CS_ARCH_RISCV, CS_ARCH_MAX
69-
}, CS_OPT_SYNTAX_NO_ALIAS_TEXT, 0},
67+
{ "+noalias",
68+
"Does not print the text alias of an alias instruction",
69+
{ CS_ARCH_RISCV, CS_ARCH_MAX },
70+
CS_OPT_SYNTAX_NO_ALIAS_TEXT,
71+
0 },
7072
// cs_mode only
7173
{ "+nofloat",
7274
"Disables floating point support",
@@ -150,12 +152,12 @@ static struct {
150152
CS_MODE_RISCV_FD },
151153
{ "+v",
152154
"Enables RISCV V extension.",
153-
{ CS_ARCH_RISCV, CS_ARCH_MAX },
154-
0,
155-
CS_MODE_RISCV_V },
155+
{ CS_ARCH_RISCV, CS_ARCH_MAX },
156+
0,
157+
CS_MODE_RISCV_V },
156158
{ "+inx",
157159
"Enables RISCV Zfinx, Zdinx, and Zhinx extensions,"
158-
" zhinxmin is also enabled as it's subset of zhinx ",
160+
" zhinxmin is also enabled as it's subset of zhinx ",
159161
{ CS_ARCH_RISCV, CS_ARCH_MAX },
160162
0,
161163
CS_MODE_RISCV_ZFINX },
@@ -166,7 +168,7 @@ static struct {
166168
CS_MODE_RISCV_ZCMP_ZCMT_ZCE },
167169
{ "a",
168170
"Enables the RISCV A extension",
169-
{ CS_ARCH_RISCV, CS_ARCH_MAX},
171+
{ CS_ARCH_RISCV, CS_ARCH_MAX },
170172
0,
171173
CS_MODE_RISCV_A },
172174
{ NULL }
@@ -990,7 +992,8 @@ int main(int argc, char **argv)
990992

991993
count = cs_disasm(handle, assembly, size, address, 0, &insn);
992994
printf("\nINSN TEXT: %s, %s", insn->mnemonic, insn->op_str);
993-
printf("\n ------------------------------ COUNT OF CS INSN OPS : %d \n", insn->detail->riscv.op_count);
995+
printf("\n ------------------------------ COUNT OF CS INSN OPS : %d \n",
996+
insn->detail->riscv.op_count);
994997
if (count > 0) {
995998
for (i = 0; i < count; i++) {
996999
int j;

include/capstone/capstone.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,13 @@ typedef enum cs_mode {
204204
CS_MODE_M680X_CPU12 = 1 << 9, ///< M680X Motorola/Freescale/NXP CPU12
205205
///< used on M68HC12/HCS12
206206
CS_MODE_M680X_HCS08 = 1 << 10, ///< M680X Freescale/NXP HCS08 mode
207-
CS_MODE_BPF_CLASSIC = 0, ///< Classic BPF mode (default)
208-
CS_MODE_BPF_EXTENDED = 1 << 0, ///< Extended BPF mode
209-
CS_MODE_RISCV32 = 1 << 0, ///< RISCV RV32G
210-
CS_MODE_RISCV64 = 1 << 1, ///< RISCV RV64G
211-
CS_MODE_RISCV_C = 1 << 2, ///< RISCV compressed instructure mode
212-
CS_MODE_RISCV_FD = 1 << 3,
213-
CS_MODE_RISCV_V = 1 << 4,
207+
CS_MODE_BPF_CLASSIC = 0, ///< Classic BPF mode (default)
208+
CS_MODE_BPF_EXTENDED = 1 << 0, ///< Extended BPF mode
209+
CS_MODE_RISCV32 = 1 << 0, ///< RISCV RV32G
210+
CS_MODE_RISCV64 = 1 << 1, ///< RISCV RV64G
211+
CS_MODE_RISCV_C = 1 << 2, ///< RISCV compressed instructure mode
212+
CS_MODE_RISCV_FD = 1 << 3,
213+
CS_MODE_RISCV_V = 1 << 4,
214214
CS_MODE_RISCV_ZFINX = 1 << 5,
215215
CS_MODE_RISCV_ZCMP_ZCMT_ZCE = 1 << 6,
216216
CS_MODE_RISCV_ZICFISS = 1 << 7,
@@ -220,13 +220,13 @@ typedef enum cs_mode {
220220
CS_MODE_RISCV_THEAD = 1 << 11,
221221
CS_MODE_RISCV_SIFIVE = 1 << 12,
222222
CS_MODE_RISCV_BITMANIP = 1 << 13,
223-
CS_MODE_RISCV_ZBA = 1 << 14,
224-
CS_MODE_RISCV_ZBB = 1 << 15,
225-
CS_MODE_RISCV_ZBC = 1 << 16,
223+
CS_MODE_RISCV_ZBA = 1 << 14,
224+
CS_MODE_RISCV_ZBB = 1 << 15,
225+
CS_MODE_RISCV_ZBC = 1 << 16,
226226
CS_MODE_RISCV_ZBKB = 1 << 17,
227227
CS_MODE_RISCV_ZBKC = 1 << 18,
228228
CS_MODE_RISCV_ZBKX = 1 << 19,
229-
CS_MODE_RISCV_ZBS = 1 << 20,
229+
CS_MODE_RISCV_ZBS = 1 << 20,
230230
CS_MODE_MOS65XX_6502 = 1 << 1, ///< MOS65XXX MOS 6502
231231
CS_MODE_MOS65XX_65C02 = 1 << 2, ///< MOS65XXX WDC 65c02
232232
CS_MODE_MOS65XX_W65C02 = 1 << 3, ///< MOS65XXX WDC W65c02

tests/details/riscv.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ test_cases:
16651665
- type: RISCV_OP_REG
16661666
reg: t0
16671667
access: CS_AC_READ
1668-
groups: [RISCV_GRP_JUMP, RISCV_FEATURE_HasStdExtCOrZca]
1668+
groups: [RISCV_FEATURE_HasStdExtCOrZca, RISCV_GRP_JUMP]
16691669
- asm_text: "c.jal 4140"
16701670
details:
16711671
riscv:
@@ -1736,7 +1736,7 @@ test_cases:
17361736
- type: RISCV_OP_IMM
17371737
imm: 0x8
17381738
access: CS_AC_READ
1739-
groups: [RISCV_GRP_CALL, RISCV_GRP_BRANCH_RELATIVE]
1739+
groups: [RISCV_GRP_BRANCH_RELATIVE]
17401740
- input:
17411741
bytes: [0xef, 0xf0, 0x1f, 0xff]
17421742
arch: "CS_ARCH_RISCV"
@@ -1751,7 +1751,7 @@ test_cases:
17511751
- type: RISCV_OP_IMM
17521752
imm: -0x10
17531753
access: CS_AC_READ
1754-
groups: [RISCV_GRP_CALL, RISCV_GRP_BRANCH_RELATIVE]
1754+
groups: [RISCV_GRP_BRANCH_RELATIVE]
17551755
- input:
17561756
bytes: [0xe7, 0x00, 0x45, 0x00]
17571757
arch: "CS_ARCH_RISCV"
@@ -3653,7 +3653,7 @@ test_cases:
36533653
- type: RISCV_OP_REG
36543654
reg: t0
36553655
access: CS_AC_READ
3656-
groups: [RISCV_GRP_JUMP, RISCV_FEATURE_HasStdExtCOrZca]
3656+
groups: [RISCV_FEATURE_HasStdExtCOrZca, RISCV_GRP_JUMP]
36573657
- input:
36583658
bytes: [0x11, 0x20]
36593659
arch: "CS_ARCH_RISCV"

tests/issues/issues.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ test_cases:
120120
-
121121
type: RISCV_OP_IMM
122122
imm: 0x4
123-
groups: [ call, branch_relative ]
123+
groups: [ branch_relative ]
124124
-
125125
input:
126126
name: "issue 2007 RISCV32 instruction groups"
@@ -172,7 +172,7 @@ test_cases:
172172
-
173173
type: RISCV_OP_IMM
174174
imm: 0x4
175-
groups: [ call, branch_relative ]
175+
groups: [ branch_relative ]
176176
-
177177
input:
178178
name: "issue 2007 RISCV32 instruction groups"

0 commit comments

Comments
 (0)