aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorChristoph Müllner <christoph.muellner@vrull.eu>2022-06-28 17:44:15 +0200
committerPhilipp Tomsich <philipp.tomsich@vrull.eu>2022-09-22 18:06:09 +0200
commit8b7419c429919884adced1fd2eae8c805b45b49a (patch)
tree0ac96804e7726bb8ff28a03a429309f3a9e0665b /include
parent547c18d9bb95571261dbd17f4767194037eb82bd (diff)
downloadfsf-binutils-gdb-8b7419c429919884adced1fd2eae8c805b45b49a.zip
fsf-binutils-gdb-8b7419c429919884adced1fd2eae8c805b45b49a.tar.gz
fsf-binutils-gdb-8b7419c429919884adced1fd2eae8c805b45b49a.tar.bz2
RISC-V: Add support for arbitrary immediate encoding formats
This patch introduces support for arbitrary signed or unsigned immediate encoding formats. The formats have the form "XsN@S" and "XuN@S" with N being the number of bits and S the LSB position. For example an immediate field of 5 bytes that encodes a signed value and is stored in the bits 24-20 of the instruction word can use the format specifier "Xs5@20". Co-developed-by: Lifang Xia <lifang_xia@linux.alibaba.com> Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
Diffstat (limited to 'include')
-rw-r--r--include/opcode/riscv.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/include/opcode/riscv.h b/include/opcode/riscv.h
index 6fdc9c9..faef28a 100644
--- a/include/opcode/riscv.h
+++ b/include/opcode/riscv.h
@@ -60,6 +60,7 @@ static const char * const riscv_pred_succ[16] =
#define RV_X(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1))
#define RV_IMM_SIGN(x) (-(((x) >> 31) & 1))
+#define RV_X_SIGNED(x, s, n) (RV_X(x, s, n) | ((-(RV_X(x, (s + n - 1), 1))) << (n)))
#define EXTRACT_ITYPE_IMM(x) \
(RV_X(x, 20, 12) | (RV_IMM_SIGN(x) << 12))
@@ -347,6 +348,22 @@ static const char * const riscv_pred_succ[16] =
#define EXTRACT_OPERAND(FIELD, INSN) \
EXTRACT_BITS ((INSN), OP_MASK_##FIELD, OP_SH_##FIELD)
+/* Extract an unsigned immediate operand on position s with n bits. */
+#define EXTRACT_U_IMM(n, s, l) \
+ RV_X (l, s, n)
+
+/* Extract an signed immediate operand on position s with n bits. */
+#define EXTRACT_S_IMM(n, s, l) \
+ RV_X_SIGNED (l, s, n)
+
+/* Validate that unsigned n-bit immediate is within bounds. */
+#define VALIDATE_U_IMM(v, n) \
+ ((unsigned long) v < (1UL << n))
+
+/* Validate that signed n-bit immediate is within bounds. */
+#define VALIDATE_S_IMM(v, n) \
+ (v < (long) (1UL << (n-1)) && v >= -(offsetT) (1UL << (n-1)))
+
/* The maximal number of subset can be required. */
#define MAX_SUBSET_NUM 4