Riscv64InstructionBuilder

Struct Riscv64InstructionBuilder 

Source
pub struct Riscv64InstructionBuilder { /* private fields */ }
Expand description

Instruction builder for generating RISC-V instructions

Implementations§

Source§

impl Riscv64InstructionBuilder

Source

pub fn new() -> Self

Source

pub fn raw_instructions(&self) -> &[Instruction]

Returns a slice of the raw instructions.

This method exposes the internal instruction buffer directly as a slice. Prefer using the instructions() method from the InstructionBuilder trait for most use cases, as it provides a higher-level abstraction and is part of the public API. Use raw_instructions only if you specifically need access to the underlying slice for migration or performance reasons.

Source

pub fn push(&mut self, instr: Instruction) -> &mut Self

Source

pub fn clear(&mut self) -> &mut Self

Source§

impl Riscv64InstructionBuilder

Source

pub fn csrrw(&mut self, rd: Register, csr: Csr, rs1: Register) -> &mut Self

Generate CSR read-write instruction

Source

pub fn csrrs(&mut self, rd: Register, csr: Csr, rs1: Register) -> &mut Self

Generate CSR read-set instruction

Source

pub fn csrrc(&mut self, rd: Register, csr: Csr, rs1: Register) -> &mut Self

Generate CSR read-clear instruction

Source

pub fn csrrwi(&mut self, rd: Register, csr: Csr, uimm: u8) -> &mut Self

Generate CSR read-write immediate instruction

Source

pub fn csrrsi(&mut self, rd: Register, csr: Csr, uimm: u8) -> &mut Self

Generate CSR read-set immediate instruction

Source

pub fn csrrci(&mut self, rd: Register, csr: Csr, uimm: u8) -> &mut Self

Generate CSR read-clear immediate instruction

Source

pub fn csrr(&mut self, rd: Register, csr: Csr) -> &mut Self

CSR read (alias for csrrs with rs1=x0) This is a common alias in RISC-V assembly

Source

pub fn csrw(&mut self, csr: Csr, rs1: Register) -> &mut Self

CSR write (alias for csrrw with rd=x0) This is a common alias in RISC-V assembly for writing to CSR without reading old value

Source

pub fn csrs(&mut self, csr: Csr, rs1: Register) -> &mut Self

CSR set (alias for csrrs with rd=x0) This is a common alias in RISC-V assembly for setting bits in CSR without reading old value

Source

pub fn csrc(&mut self, csr: Csr, rs1: Register) -> &mut Self

CSR clear (alias for csrrc with rd=x0) This is a common alias in RISC-V assembly for clearing bits in CSR without reading old value

Source

pub fn csrwi(&mut self, csr: Csr, uimm: u8) -> &mut Self

CSR write immediate (alias for csrrwi with rd=x0) This is a common alias in RISC-V assembly for writing immediate to CSR without reading old value

Source

pub fn csrsi(&mut self, csr: Csr, uimm: u8) -> &mut Self

CSR set immediate (alias for csrrsi with rd=x0) This is a common alias in RISC-V assembly for setting bits in CSR with immediate without reading old value

Source

pub fn csrci(&mut self, csr: Csr, uimm: u8) -> &mut Self

CSR clear immediate (alias for csrrci with rd=x0) This is a common alias in RISC-V assembly for clearing bits in CSR with immediate without reading old value

Source

pub fn add(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate add instruction

Source

pub fn addi(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate add immediate instruction

Source

pub fn sub(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate subtract instruction

Source

pub fn subi(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate subtract immediate instruction

Source

pub fn xor(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate XOR instruction

Source

pub fn xori(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate XOR immediate instruction

Source

pub fn or(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate OR instruction

Source

pub fn ori(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate OR immediate instruction

Source

pub fn slt(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate Set Less Than instruction

Source

pub fn slti(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate Set Less Than immediate instruction

Source

pub fn sltu(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate Set Less Than Unsigned instruction

Source

pub fn sltiu(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate Set Less Than immediate Unsigned instruction

Source

pub fn sll(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate SLL (Shift Left Logical) instruction

Source

pub fn srl(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate SRL (Shift Right Logical) instruction

Source

pub fn sra(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate SRA (Shift Right Arithmetic) instruction

Source

pub fn slli(&mut self, rd: Register, rs1: Register, shamt: u8) -> &mut Self

Generate SLLI (Shift Left Logical Immediate) instruction

Source

pub fn srli(&mut self, rd: Register, rs1: Register, shamt: u8) -> &mut Self

Generate SRLI (Shift Right Logical Immediate) instruction

Source

pub fn srai(&mut self, rd: Register, rs1: Register, shamt: u8) -> &mut Self

Generate SRAI (Shift Right Arithmetic Immediate) instruction

Source

pub fn and(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate AND instruction

Source

pub fn andi(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate AND immediate instruction

Source

pub fn mul(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate MUL (Multiply) instruction Performs signed multiplication and returns the lower 64 bits of the result

Source

pub fn mulh(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate MULH (Multiply High) instruction Performs signed × signed multiplication and returns the upper 64 bits of the result

Source

pub fn mulhsu( &mut self, rd: Register, rs1: Register, rs2: Register, ) -> &mut Self

Generate MULHSU (Multiply High Signed × Unsigned) instruction Performs signed × unsigned multiplication and returns the upper 64 bits of the result

Source

pub fn mulhu(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate MULHU (Multiply High Unsigned) instruction Performs unsigned × unsigned multiplication and returns the upper 64 bits of the result

Source

pub fn div(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate DIV (Divide) instruction Performs signed division: rs1 ÷ rs2

Source

pub fn divu(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate DIVU (Divide Unsigned) instruction Performs unsigned division: rs1 ÷ rs2

Source

pub fn rem(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate REM (Remainder) instruction Computes signed remainder: rs1 % rs2

Source

pub fn remu(&mut self, rd: Register, rs1: Register, rs2: Register) -> &mut Self

Generate REMU (Remainder Unsigned) instruction Computes unsigned remainder: rs1 % rs2

Source

pub fn ld(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate LD (Load Doubleword) instruction

Source

pub fn lw(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate LW (Load Word) instruction

Source

pub fn lh(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate LH (Load Halfword) instruction

Source

pub fn lb(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate LB (Load Byte) instruction

Source

pub fn lbu(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate LBU (Load Byte Unsigned) instruction

Source

pub fn lhu(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate LHU (Load Halfword Unsigned) instruction

Source

pub fn lwu(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate LWU (Load Word Unsigned) instruction

Source

pub fn li(&mut self, rd: Register, imm: i32) -> &mut Self

Load immediate value into register (handles large immediates) This is a common pseudo-instruction in RISC-V assembly

Source

pub fn sd(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate SD (Store Doubleword) instruction

Source

pub fn sw(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate SW (Store Word) instruction

Source

pub fn sh(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate SH (Store Halfword) instruction

Source

pub fn sb(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate SB (Store Byte) instruction

Source

pub fn lui(&mut self, rd: Register, imm: u32) -> &mut Self

Generate LUI (Load Upper Immediate) instruction

Source

pub fn auipc(&mut self, rd: Register, imm: u32) -> &mut Self

Generate AUIPC (Add Upper Immediate to PC) instruction

Source

pub fn jal(&mut self, rd: Register, imm: i32) -> &mut Self

Generate JAL (Jump and Link) instruction

Source

pub fn jalr(&mut self, rd: Register, rs1: Register, imm: i16) -> &mut Self

Generate JALR (Jump and Link Register) instruction

Source

pub fn beq(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate BEQ (Branch if Equal) instruction

Source

pub fn bne(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate BNE (Branch if Not Equal) instruction

Source

pub fn blt(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate BLT (Branch if Less Than) instruction

Source

pub fn bge(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate BGE (Branch if Greater or Equal) instruction

Source

pub fn bltu(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate BLTU (Branch if Less Than Unsigned) instruction

Source

pub fn bgeu(&mut self, rs1: Register, rs2: Register, imm: i16) -> &mut Self

Generate BGEU (Branch if Greater or Equal Unsigned) instruction

Source

pub fn sret(&mut self) -> &mut Self

Supervisor return instruction Returns from supervisor mode to user mode or previous privilege level This is a privileged instruction that can only be executed in supervisor mode or higher

Source

pub fn mret(&mut self) -> &mut Self

Machine return instruction Returns from machine mode to previous privilege level This is a privileged instruction that can only be executed in machine mode

Source

pub fn ecall(&mut self) -> &mut Self

Environment call instruction Generates a system call to the execution environment

Source

pub fn ebreak(&mut self) -> &mut Self

Environment break instruction Generates a breakpoint exception

Source

pub fn wfi(&mut self) -> &mut Self

Wait for interrupt instruction Puts the processor in a low-power state until an interrupt occurs This is a privileged instruction

Source

pub fn ret(&mut self) -> &mut Self

Return instruction (alias for jalr x0, x1, 0) This is a common alias in RISC-V assembly for returning from a function

Trait Implementations§

Source§

impl InstructionBuilder<Instruction> for Riscv64InstructionBuilder

Source§

unsafe fn function<F>(&self) -> Result<CallableJitFunction<F>, JitError>

Create a JIT-compiled function from the assembled instructions (std-only)

This method converts the assembled instructions into executable machine code that can be called directly as a function. The generic type parameter F specifies the function signature.

§Safety

This function is unsafe because:

  • It allocates executable memory
  • It assumes the assembled code follows the correct ABI
  • The caller must ensure the function signature matches the actual code
§Examples
use jit_assembler::riscv64::{reg, Riscv64InstructionBuilder};
use jit_assembler::common::InstructionBuilder;
 
let add_func = unsafe {
    Riscv64InstructionBuilder::new()
        .add(reg::A0, reg::A0, reg::A1) // Add first two arguments
        .ret()
        .function::<fn(u64, u64) -> u64>()
}.expect("Failed to create JIT function");
 
// Call the JIT function directly (only works on RISC-V hosts)
let result = add_func.call(10, 20); // Should return 30
Source§

type Register = Register

The register type used by this architecture
Source§

fn new() -> Self

Create a new instruction builder
Source§

fn instructions(&self) -> InstructionCollection<Instruction>

Get the generated instructions
Source§

fn push(&mut self, instr: Instruction)

Add an instruction to the builder
Source§

fn clear(&mut self)

Clear all instructions
Source§

unsafe fn raw_function(&self) -> Result<RawCallableJitFunction, JitError>

Create a raw JIT-compiled function for manual type conversion (std-only) Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.