Skip to content

Instruction Set Architecture

View as Markdown

ThruVM implements a RISC-V virtual machine that executes smart contract bytecode on the Thru blockchain. The VM is fully compliant with the RISC-V specification and supports multiple standard extensions for comprehensive computational capabilities.

Execution on ThruVM is single-threaded and is entirely deterministic. There is no atomic access to data outside virtual machine execution, which implies sequential consistency for all memory operations.

ThruVM is fully compliant with the RISC-V Instruction Set Manual, Version 20250508. The implementation adheres to all architectural requirements and behavioral specifications defined in this version.

ThruVM supports the following RISC-V instruction set extensions:

Extension IDNameVersionPurpose
RV64IBase Integer Instruction Set2.164-bit base integer instructions including arithmetic, logical, memory, and control flow operations
MStandard Extension for Integer Multiplication and Division2.0Hardware multiplication, division, and remainder operations for both 32-bit and 64-bit integers
CStandard Extension for Compressed Instructions2.016-bit compressed instruction encodings to reduce code size and improve instruction fetch efficiency
BStandard Extension for Bit Manipulation1.0.0Bit manipulation instructions including count, rotate, permute, and logic operations
ZknhNIST Suite: Hash Function Instructions1.0.1Cryptographic hash function acceleration instructions for SHA-256 and SHA-512

The following sections provide a brief description of the base ISA and supported extensions.

The RV64I base instruction set provides:

  • 64-bit integer registers: 32 general-purpose registers (x0-x31) with x0 hardwired to zero
  • Load/Store architecture: Memory access only through explicit load and store instructions
  • Arithmetic operations: Addition, subtraction, logical operations (AND, OR, XOR)
  • Shift operations: Logical and arithmetic left/right shifts
  • Comparison operations: Set-less-than for signed and unsigned comparisons
  • Branch instructions: Conditional branches based on register comparisons
  • Jump instructions: Unconditional jumps with optional link register updates
  • System instructions: Environment calls and control/status register access

The M extension adds support for:

  • Integer multiplication: MUL, MULH, MULHSU, MULHU
  • Integer division: DIV, DIVU, REM, REMU
  • 32-bit variants: MULW, DIVW, DIVUW, REMW, REMUW for RV64

The C extension provides 16-bit instruction encodings for:

  • Common operations: Register-register operations, immediate operations
  • Memory access: Compressed load/store instructions
  • Control flow: Compressed branch and jump instructions
  • Stack operations: Stack pointer manipulation instructions

The B extension includes instructions for:

  • Bit counting: Count leading/trailing zeros (CLZ, CTZ)
  • Bit manipulation: Single-bit operations, bit field operations
  • Rotation: Left and right rotations (ROL, ROR)
  • Permutation: Byte and nibble permutation operations
  • Carry-less multiplication: CLMUL, CLMULH, CLMULR

Cryptographic Hash Functions (Zknh Extension)

Section titled “Cryptographic Hash Functions (Zknh Extension)”

The Zknh extension provides acceleration for:

  • SHA-256: SHA256SIG0, SHA256SIG1, SHA256SUM0, SHA256SUM1
  • SHA-512: SHA512SIG0, SHA512SIG1, SHA512SUM0, SHA512SUM1

Using instructions that are currently unimplemented will cause the VM to exit with an illegal instruction exception (SIGILL), resulting in transaction revert.

If an instruction becomes implemented in future versions, programs using those instruction encodings will automatically adopt the new behavior. To explicitly indicate illegal instructions, use encodings with all bits set to 0 or all bits set to 1.

ThruVM enforces strict alignment requirements:

  • Instructions: Must be aligned to 16-bit boundaries
  • Data access: Unaligned memory access is not supported and will trigger an exception

For details on the syscall calling convention and available system calls, refer to the Syscalls documentation.

ThruVM follows standard RISC-V register conventions:

RegisterABI NamePurpose
x0zeroHardwired to zero
x1raReturn address
x2spStack pointer
x3gpGlobal pointer
x4tpThread pointer
x5-x7t0-t2Temporary registers
x8fp/s0Frame pointer/saved register
x9s1Saved register
x10-x17a0-a7Function arguments/return values
x18-x27s2-s11Saved registers
x28-x31t3-t6Temporary registers