Commit 8963d622 authored by Heiko Stuebner's avatar Heiko Stuebner Committed by Palmer Dabbelt

RISC-V: add U-type imm parsing to insn.h header

Similar to other existing types, allow extracting the immediate
for a U-type instruction.

U-type immediates are special in that regard, that the value
in the instruction in bits [31:12] already represents the same
bits of the immediate, so no shifting is required.

U-type immediates are for example used in the auipc instruction,
so these constants make it easier to parse such instructions.
Reviewed-by: default avatarConor Dooley <conor.dooley@microchip.com>
Reviewed-by: default avatarAndrew Jones <ajones@ventanamicro.com>
Reviewed-by: default avatarLad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Signed-off-by: default avatarHeiko Stuebner <heiko.stuebner@vrull.eu>
Link: https://lore.kernel.org/r/20221223221332.4127602-10-heiko@sntech.deSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
parent debe28d0
...@@ -34,6 +34,15 @@ ...@@ -34,6 +34,15 @@
#define RV_J_IMM_11_MASK GENMASK(0, 0) #define RV_J_IMM_11_MASK GENMASK(0, 0)
#define RV_J_IMM_19_12_MASK GENMASK(7, 0) #define RV_J_IMM_19_12_MASK GENMASK(7, 0)
/*
* U-type IMMs contain the upper 20bits [31:20] of an immediate with
* the rest filled in by zeros, so no shifting required. Similarly,
* bit31 contains the signed state, so no sign extension necessary.
*/
#define RV_U_IMM_SIGN_OPOFF 31
#define RV_U_IMM_31_12_OPOFF 0
#define RV_U_IMM_31_12_MASK GENMASK(31, 12)
/* The bit field of immediate value in B-type instruction */ /* The bit field of immediate value in B-type instruction */
#define RV_B_IMM_SIGN_OPOFF 31 #define RV_B_IMM_SIGN_OPOFF 31
#define RV_B_IMM_10_5_OPOFF 25 #define RV_B_IMM_10_5_OPOFF 25
...@@ -235,6 +244,10 @@ static __always_inline bool riscv_insn_is_branch(u32 code) ...@@ -235,6 +244,10 @@ static __always_inline bool riscv_insn_is_branch(u32 code)
#define RV_X(X, s, mask) (((X) >> (s)) & (mask)) #define RV_X(X, s, mask) (((X) >> (s)) & (mask))
#define RVC_X(X, s, mask) RV_X(X, s, mask) #define RVC_X(X, s, mask) RV_X(X, s, mask)
#define RV_EXTRACT_UTYPE_IMM(x) \
({typeof(x) x_ = (x); \
(RV_X(x_, RV_U_IMM_31_12_OPOFF, RV_U_IMM_31_12_MASK)); })
#define RV_EXTRACT_JTYPE_IMM(x) \ #define RV_EXTRACT_JTYPE_IMM(x) \
({typeof(x) x_ = (x); \ ({typeof(x) x_ = (x); \
(RV_X(x_, RV_J_IMM_10_1_OPOFF, RV_J_IMM_10_1_MASK) << RV_J_IMM_10_1_OFF) | \ (RV_X(x_, RV_J_IMM_10_1_OPOFF, RV_J_IMM_10_1_MASK) << RV_J_IMM_10_1_OFF) | \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment