Skip to content

RISC-V: add reg_access and test its usages in C and Python#2895

Open
moste00 wants to merge 1 commit intocapstone-engine:nextfrom
moste00:fix/reg_access_callback_impl
Open

RISC-V: add reg_access and test its usages in C and Python#2895
moste00 wants to merge 1 commit intocapstone-engine:nextfrom
moste00:fix/reg_access_callback_impl

Conversation

@moste00
Copy link
Copy Markdown
Contributor

@moste00 moste00 commented Apr 16, 2026

Your checklist for this pull request

  • I've documented or updated the documentation of every API function and struct this PR changes.
  • I've added tests that prove my fix is effective or that my feature works (if possible)

Detailed description

Capstone module callback reg_access is a convenience wrapper over the operands array that filters out the register operands (including those used as memory base address) from the rest of the operands and returns them sorted into read and written registers. It wasn't implemented for RISC-V, this PR implements it.

The following decisions were made for RISC-V's reg_access:

1- System registers (CSRs) are not registers

This follows existing Capstone convention, where almost every architecture that have system registers except x86 treats them as a seperate address space.

From a purely practical POV, the reg_access API returns registers as an array of integers, and the address space of normal registers intersects with that of system registers so there is nothing in the return value to distinguish them.

2- The PC is not an implicit register

Whenever an instruction reads PC (e.g. all call-ish instructions JAL[R]?) this is NOT counted as an implicit read of the PC.

The reason is that the PC is somewhat "second class" in RISC-V, it's an archiectural register but has no actual index and can never be directly written to by any instruction in any standard extension no matter the privliege.

Meanwhile, all instruction that read the PC have names that make it obvious they read the PC so adding that information to the implicit reads array would be redundant.

Again from a purely practical POV, the PC has no index defined by riscv.h, which makes sense as it's never referenced by any instruction.

Test plan

Added two unit tests to prove the callback can be run from the consumer side of the C and Python API of Capstone.

Closing issues

closes #2406 .

reg_access is a convenience wrapper over the `operands` array that filters the register operands (including those used as memory base address) and returns them sorted into read
and written registers. It wasn't implemented for RISC-V, this PR implements it.

The following decisions were made for RISC-V:
1- System registers (CSRs) are not registers

This follows existing Capstone convention, where almost every archiceture that have system registers except x86 treats them as a seperate address space.

From a purely practical POV, the reg_access function API returns registers as an array of integers, and the address space of normal registers intersects with that of system registers
so there is nothing in the return value to distinguish them.

2- PC is not an implicit register

Whenever an instruction reads PC (e.g. all call-ish instructions JAL[R]?) this is NOT counted as an implicit read of the PC.

The reason is that the PC is somewhat "second class" in RISC-V, it's an archiectural register but has no actual index and can never be directly written to by any instruction in any
standard extension no matter the privliege.

Meanwhile, all instruction that read the PC have names that make it obvious they read the PC so adding that information to the implicit reads array would be redundant.
@github-actions github-actions bot added RISCV Arch Python Bindings labels Apr 16, 2026
@moste00
Copy link
Copy Markdown
Contributor Author

moste00 commented Apr 16, 2026

@Rot127 please review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Python Bindings RISCV Arch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

regs_access() for RISC-V yield error

1 participant