infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
abi_riscv64_common.h
Go to the documentation of this file.
1#pragma once
36#include <stdint.h>
134// Base opcodes (bits 6:0)
135#define RV_OP_LOAD 0x03U // Loads (lb, lh, lw, ld, lbu, lhu, lwu)
136#define RV_OP_OP_IMM 0x13U // Register-immediate ALU ops (addi, slli, ...)
137#define RV_OP_OP_IMM_32 0x1BU // Register-immediate 32-bit ALU ops (addiw)
138#define RV_OP_AUIPC 0x17U // Add upper immediate to pc
139#define RV_OP_STORE 0x23U // Stores (sb, sh, sw, sd)
140#define RV_OP_OP 0x33U // Register-register ALU ops (add, sub, sll, ...)
141#define RV_OP_LUI 0x37U // Load upper immediate
142#define RV_OP_BRANCH 0x63U // Conditional branches
143#define RV_OP_JALR 0x67U // Jump and link register
144#define RV_OP_JAL 0x6FU // Jump and link
145#define RV_OP_FP 0x53U // Floating-point register ops
146#define RV_OP_SYSTEM 0x73U // System instructions
147#define RV_OP_FLOAD 0x07U // Floating-point loads (flw, fld)
148#define RV_OP_FSTORE 0x27U // Floating-point stores (fsw, fsd)
149// funct3 fields (bits 14:12)
150#define RV_F3_ADD_SUB 0x0U // ADDI / ADD / SUB / SLL
151#define RV_F3_SLL 0x1U // SLLI / SLL
152#define RV_F3_SLT 0x2U
153#define RV_F3_SLTU 0x3U
154#define RV_F3_XOR 0x4U
155#define RV_F3_SR 0x5U // SRLI / SRAI / SRL / SRA
156#define RV_F3_OR 0x6U
157#define RV_F3_AND 0x7U
158// funct3 fields for loads/stores
159#define RV_F3_LD 0x3U // LD / SD
160#define RV_F3_LW 0x2U // LW / SW
161#define RV_F3_LH 0x1U // LH / SH
162#define RV_F3_LB 0x0U // LB / SB
163#define RV_F3_LBU 0x4U // LBU (no unsigned store exists)
164#define RV_F3_LHU 0x5U // LHU
165#define RV_F3_LWU 0x6U // LWU
166#define RV_F3_FLD 0x3U // FLD / FSD
167#define RV_F3_FLW 0x2U // FLW / FSW
168#define RV_F3_FLH 0x1U // FLH / FSH (Zfh)
169// funct3 fields for branches
170#define RV_F3_BEQ 0x0U
171#define RV_F3_BNE 0x1U
172#define RV_F3_BLT 0x4U
173#define RV_F3_BGE 0x5U
174#define RV_F3_BLTU 0x6U
175#define RV_F3_BGEU 0x7U
176// funct7 fields (bits 31:25) for R-type integer ops
177#define RV_F7_ADD 0x00U
178#define RV_F7_SUB 0x20U
179#define RV_F7_SLL 0x00U
180#define RV_F7_SRL 0x00U
181#define RV_F7_SRA 0x20U
182// Shift immediate funct6 (bits 31:26)
183#define RV_F6_SLLI 0x00U
184#define RV_F6_SRLI 0x00U
185#define RV_F6_SRAI 0x10U
186// Floating-point move instructions (funct7 selects the operation).
187// The RV64 W-level moves transfer between GPRs and single-precision FP registers.
188#define RV_F7_FMV_X_S 0x70U // FMV.X.W: FP(single) -> GPR
189#define RV_F7_FMV_S_X 0x78U // FMV.W.X: GPR -> FP(single)
190#define RV_F7_FMV_X_D 0x71U // FMV.X.D: FP(double) -> GPR
191#define RV_F7_FMV_D_X 0x79U // FMV.D.X: GPR -> FP(double)
192#define RV_F7_FMV_X_H 0x72U // FMV.X.H (Zfh)
193#define RV_F7_FMV_H_X 0x7AU // FMV.H.X (Zfh)
194// FP-to-FP moves are the FSGNJ pseudo-instructions (fmv.s rd,rs1 == fsgnj.s rd,rs1,rs1).
195#define RV_F7_FSGNJ_S 0x10U // FSGNJ.S
196#define RV_F7_FSGNJ_D 0x11U // FSGNJ.D
197// The 'imm' (rs2) encodings used by the fcvt family to select source/target precision.
198// The source precision selects funct7 bit 0: 0 = 32-bit source (FCVT.S.D), 1 = 64-bit source (FCVT.D.S).
199#define RV_FCVT_RM_RNE 0x0U // Round to nearest, ties to even
200#define RV_F7_FCVT_S_D 0x20U // FCVT.S.D: source is double-precision, dest is single
201#define RV_F7_FCVT_D_S 0x21U // FCVT.D.S: source is single-precision, dest is double
202#define RV_RS2_FCVT_D_S 0x00U // rs2 field for FCVT.D.S
203#define RV_RS2_FCVT_S_D 0x01U // rs2 field for FCVT.S.D
204// Instruction bit-field helper macros
205#define RV_RS1_SHIFT 15U
206#define RV_RS2_SHIFT 20U
207#define RV_RD_SHIFT 7U
208#define RV_FUNCT3_SHIFT 12U
// end riscv64_opcodes
210
211// Trampoline register roles (internal to the RISC-V backend).
213#define RV_CTX_TARGET_REG X_S1_REG
215#define RV_CTX_RET_REG X_S2_REG
217#define RV_CTX_ARGS_REG X_S3_REG
219#define RV_SCRATCH0_REG X_T0_REG
220#define RV_SCRATCH1_REG X_T1_REG
221#define RV_SCRATCH2_REG X_T2_REG
222#define RV_SCRATCH3_REG X_T3_REG
riscv_fpr
Definition abi_riscv64_common.h:89
@ F_FT10_REG
f30: Temporary / caller-saved.
Definition abi_riscv64_common.h:120
@ F_FA5_REG
f15: FP Argument 6.
Definition abi_riscv64_common.h:105
@ F_FS0_REG
f8: Saved register. Callee-saved.
Definition abi_riscv64_common.h:98
@ F_FT8_REG
f28: Temporary / caller-saved.
Definition abi_riscv64_common.h:118
@ F_FA1_REG
f11: FP Argument 2 / caller-saved.
Definition abi_riscv64_common.h:101
@ F_FS10_REG
f26: Saved register. Callee-saved.
Definition abi_riscv64_common.h:116
@ F_FS2_REG
f18: Saved register. Callee-saved.
Definition abi_riscv64_common.h:108
@ F_FT6_REG
f6: Temporary / caller-saved.
Definition abi_riscv64_common.h:96
@ F_FA7_REG
f17: FP Argument 8.
Definition abi_riscv64_common.h:107
@ F_FT7_REG
f7: Temporary / caller-saved.
Definition abi_riscv64_common.h:97
@ F_FT2_REG
f2: Temporary / caller-saved.
Definition abi_riscv64_common.h:92
@ F_FS8_REG
f24: Saved register. Callee-saved.
Definition abi_riscv64_common.h:114
@ F_FS1_REG
f9: Saved register. Callee-saved.
Definition abi_riscv64_common.h:99
@ F_FS9_REG
f25: Saved register. Callee-saved.
Definition abi_riscv64_common.h:115
@ F_FA4_REG
f14: FP Argument 5.
Definition abi_riscv64_common.h:104
@ F_FS7_REG
f23: Saved register. Callee-saved.
Definition abi_riscv64_common.h:113
@ F_FS11_REG
f27: Saved register. Callee-saved.
Definition abi_riscv64_common.h:117
@ F_FA0_REG
f10: FP Argument 1 / FP Return value / caller-saved.
Definition abi_riscv64_common.h:100
@ F_FS5_REG
f21: Saved register. Callee-saved.
Definition abi_riscv64_common.h:111
@ F_FS6_REG
f22: Saved register. Callee-saved.
Definition abi_riscv64_common.h:112
@ F_FT11_REG
f31: Temporary / caller-saved.
Definition abi_riscv64_common.h:121
@ F_FT9_REG
f29: Temporary / caller-saved.
Definition abi_riscv64_common.h:119
@ F_FS4_REG
f20: Saved register. Callee-saved.
Definition abi_riscv64_common.h:110
@ F_FS3_REG
f19: Saved register. Callee-saved.
Definition abi_riscv64_common.h:109
@ F_FA2_REG
f12: FP Argument 3.
Definition abi_riscv64_common.h:102
@ F_FT1_REG
f1: Temporary / caller-saved.
Definition abi_riscv64_common.h:91
@ F_FT4_REG
f4: Temporary / caller-saved.
Definition abi_riscv64_common.h:94
@ F_FT0_REG
f0: Temporary / caller-saved.
Definition abi_riscv64_common.h:90
@ F_FT3_REG
f3: Temporary / caller-saved.
Definition abi_riscv64_common.h:93
@ F_FA3_REG
f13: FP Argument 4.
Definition abi_riscv64_common.h:103
@ F_FT5_REG
f5: Temporary / caller-saved.
Definition abi_riscv64_common.h:95
@ F_FA6_REG
f16: FP Argument 7.
Definition abi_riscv64_common.h:106
riscv_gpr
Definition abi_riscv64_common.h:46
@ X_S6_REG
x22: Saved register. Callee-saved.
Definition abi_riscv64_common.h:69
@ X_T4_REG
x29: Temporary / caller-saved.
Definition abi_riscv64_common.h:76
@ X_A7_REG
x17: Argument 8.
Definition abi_riscv64_common.h:64
@ X_SP_REG
x2: Stack Pointer. Must always be 16-byte aligned.
Definition abi_riscv64_common.h:49
@ X_S4_REG
x20: Saved register. Callee-saved.
Definition abi_riscv64_common.h:67
@ X_A4_REG
x14: Argument 5.
Definition abi_riscv64_common.h:61
@ X_A6_REG
x16: Argument 7.
Definition abi_riscv64_common.h:63
@ X_A1_REG
x11: Argument 2 / Return value / caller-saved.
Definition abi_riscv64_common.h:58
@ X_S5_REG
x21: Saved register. Callee-saved.
Definition abi_riscv64_common.h:68
@ X_S2_REG
x18: Saved register. Callee-saved.
Definition abi_riscv64_common.h:65
@ X_T2_REG
x7: Temporary / caller-saved.
Definition abi_riscv64_common.h:54
@ X_T5_REG
x30: Temporary / caller-saved.
Definition abi_riscv64_common.h:77
@ X_S0_REG
x8: Saved register / Frame Pointer (fp). Callee-saved.
Definition abi_riscv64_common.h:55
@ X_S10_REG
x26: Saved register. Callee-saved.
Definition abi_riscv64_common.h:73
@ X_A3_REG
x13: Argument 4.
Definition abi_riscv64_common.h:60
@ X_T1_REG
x6: Temporary / caller-saved.
Definition abi_riscv64_common.h:53
@ X_ZERO_REG
x0: Hardwired zero. Reads return 0, writes are ignored.
Definition abi_riscv64_common.h:47
@ X_T3_REG
x28: Temporary / caller-saved.
Definition abi_riscv64_common.h:75
@ X_S7_REG
x23: Saved register. Callee-saved.
Definition abi_riscv64_common.h:70
@ X_S3_REG
x19: Saved register. Callee-saved.
Definition abi_riscv64_common.h:66
@ X_RA_REG
x1: Return Address. Set by jal/jalr; the standard link register.
Definition abi_riscv64_common.h:48
@ X_GP_REG
x3: Global Pointer (reserved for the ABI).
Definition abi_riscv64_common.h:50
@ X_A0_REG
x10: Argument 1 / Return value / caller-saved.
Definition abi_riscv64_common.h:57
@ X_TP_REG
x4: Thread Pointer (reserved for the ABI).
Definition abi_riscv64_common.h:51
@ X_T6_REG
x31: Temporary / caller-saved.
Definition abi_riscv64_common.h:78
@ X_T0_REG
x5: Temporary / caller-saved.
Definition abi_riscv64_common.h:52
@ X_S9_REG
x25: Saved register. Callee-saved.
Definition abi_riscv64_common.h:72
@ X_S11_REG
x27: Saved register. Callee-saved.
Definition abi_riscv64_common.h:74
@ X_A2_REG
x12: Argument 3 / caller-saved... (volatile).
Definition abi_riscv64_common.h:59
@ X_S1_REG
x9: Saved register. Callee-saved.
Definition abi_riscv64_common.h:56
@ X_S8_REG
x24: Saved register. Callee-saved.
Definition abi_riscv64_common.h:71
@ X_A5_REG
x15: Argument 6.
Definition abi_riscv64_common.h:62