Zibi Extension for RISC-V

Jeff Law jeffreyalaw@gmail.com
Fri Nov 20 22:39:41 GMT 2026


This is Kito's binutils-gdb patch for the Zibi RISC-V extension.

Zibi introduces two new instructions compare a small constant against a 
register and branch on equality/inequality.

I manually built the match/mask tables which lined up with Kito's 
implementation.  The only change I made was bumping the version to 0.9.

Given it's Kito's patch I'm really functioning as the reviewer here, but 
it's also the case that I don't think he's officially submitted for 
inclusion into binutils.

With all that in mind, my plan is to commit early next week unless Kito 
objects.

Jeff
-------------- next part --------------
    RISC-V: Support ZIBI extension
    
    Zibi provide branch with immediate instruction, beqi and bnei.
    the branch range is same as the branch with register instruction,
    and the immediate value range is 1~31 and -1.
    
    Spec: https://lists.riscv.org/g/tech-arch-review/message/327


diff --git a/bfd/elfxx-riscv.c b/bfd/elfxx-riscv.c
index 6a02d80bcd3..db7c6c20216 100644
--- a/bfd/elfxx-riscv.c
+++ b/bfd/elfxx-riscv.c
@@ -1569,6 +1569,7 @@ static const struct riscv_supported_ext riscv_supported_std_z_ext[] =
   {"zicfiss",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
   {"zicfilp",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
   {"zilsd",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
+  {"zibi",		ISA_SPEC_CLASS_DRAFT,		0, 9, 0 },
   {"zmmul",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
   {"za64rs",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
   {"za128rs",		ISA_SPEC_CLASS_DRAFT,		1, 0, 0 },
@@ -2955,6 +2956,8 @@ riscv_multi_subset_supports (riscv_parse_subset_t *rps,
       return riscv_subset_supports (rps, "zicbop");
     case INSN_CLASS_ZICBOZ:
       return riscv_subset_supports (rps, "zicboz");
+    case INSN_CLASS_ZIBI:
+      return riscv_subset_supports (rps, "zibi");
     case INSN_CLASS_ZICOND:
       return riscv_subset_supports (rps, "zicond");
     case INSN_CLASS_ZICSR:
@@ -3285,6 +3288,8 @@ riscv_multi_subset_supports_ext (riscv_parse_subset_t *rps,
       return "zicbop";
     case INSN_CLASS_ZICBOZ:
       return "zicboz";
+    case INSN_CLASS_ZIBI:
+      return "zibi";
     case INSN_CLASS_ZICOND:
       return "zicond";
     case INSN_CLASS_ZICSR:
diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index 667d015de45..fbdd46fed33 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -1556,6 +1556,16 @@ validate_riscv_insn (const struct riscv_opcode *opc, int length)
 		  goto unknown_validate_operand;
 		}
 	      break;
+	    case 'b':
+	      switch (*++oparg)
+		{
+		case 'i':
+		  USE_BITS (OP_MASK_RS2, OP_SH_RS2);
+		  break;
+		default:
+		  goto unknown_validate_operand;
+		}
+	      break;
 	    case 'f':
 	      switch (*++oparg)
 		{
@@ -3848,7 +3858,28 @@ riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
 		      goto unknown_riscv_ip_operand;
 		    }
 		  break;
-
+		case 'b':
+		  switch (*++oparg)
+		    {
+		    case 'i':
+		      my_getExpression (imm_expr, asarg, false);
+		      check_absolute_expr (ip, imm_expr, false);
+		      if (imm_expr->X_add_number > 31
+			  || imm_expr->X_add_number == 0
+			  || imm_expr->X_add_number < -1)
+			as_bad (_ ("improper beqi/bnei immediate value (%ld)"),
+				(long) imm_expr->X_add_number);
+		      unsigned encoded_value = imm_expr->X_add_number == -1
+						   ? 0
+						   : imm_expr->X_add_number;
+		      INSERT_OPERAND (RS2, *ip, encoded_value);
+		      imm_expr->X_op = O_absent;
+		      asarg = expr_parse_end;
+		      continue;
+		    default:
+		      goto unknown_riscv_ip_operand;
+		    }
+		  break;
 		case 'f':
 		  switch (*++oparg)
 		    {
diff --git a/gas/testsuite/gas/riscv/march-help.l b/gas/testsuite/gas/riscv/march-help.l
index d7bb5aa13a4..7ba92b9fd7f 100644
--- a/gas/testsuite/gas/riscv/march-help.l
+++ b/gas/testsuite/gas/riscv/march-help.l
@@ -31,6 +31,7 @@ All available -march extensions for RISC-V:
 	zicfiss                                 1.0
 	zicfilp                                 1.0
 	zilsd                                   1.0
+	zibi                                    0.9
 	zmmul                                   1.0
 	za64rs                                  1.0
 	za128rs                                 1.0
diff --git a/gas/testsuite/gas/riscv/zibi-fail.d b/gas/testsuite/gas/riscv/zibi-fail.d
new file mode 100644
index 00000000000..e530959a0ee
--- /dev/null
+++ b/gas/testsuite/gas/riscv/zibi-fail.d
@@ -0,0 +1,4 @@
+#as: -march=rv32i_zibi
+#objdump: -d
+#source: zibi-fail.s
+#error_output: zibi-fail.l
diff --git a/gas/testsuite/gas/riscv/zibi-fail.l b/gas/testsuite/gas/riscv/zibi-fail.l
new file mode 100644
index 00000000000..34e208a3451
--- /dev/null
+++ b/gas/testsuite/gas/riscv/zibi-fail.l
@@ -0,0 +1,9 @@
+.*: Assembler messages:
+.*: Error: improper beqi/bnei immediate value \(-2\)
+.*: Error: improper beqi/bnei immediate value \(32\)
+.*: Error: improper beqi/bnei immediate value \(34\)
+.*: Error: improper beqi/bnei immediate value \(0\)
+.*: Error: improper beqi/bnei immediate value \(-3\)
+.*: Error: improper beqi/bnei immediate value \(32\)
+.*: Error: improper beqi/bnei immediate value \(33\)
+.*: Error: improper beqi/bnei immediate value \(0\)
diff --git a/gas/testsuite/gas/riscv/zibi-fail.s b/gas/testsuite/gas/riscv/zibi-fail.s
new file mode 100644
index 00000000000..4e6f40b3c67
--- /dev/null
+++ b/gas/testsuite/gas/riscv/zibi-fail.s
@@ -0,0 +1,9 @@
+foo:
+	beqi t0, -2, foo
+	beqi t4, 32, foo
+	beqi t4, 34, foo
+	beqi t2, 0, foo
+	bnei t0, -3, foo
+	bnei t4, 32, foo
+	bnei t2, 33, foo
+	bnei t3, 0, foo
diff --git a/gas/testsuite/gas/riscv/zibi.d b/gas/testsuite/gas/riscv/zibi.d
new file mode 100644
index 00000000000..ee5e0fd81fd
--- /dev/null
+++ b/gas/testsuite/gas/riscv/zibi.d
@@ -0,0 +1,17 @@
+#as: -march=rv32i_zibi
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+[0-9a-f]+ <foo>:
+.*:[ 	]+0002a063[ 	]+beqi[ 	]+t0,-1,0 <foo>
+.*:[ 	]+fe5eaee3[ 	]+beqi[ 	]+t4,5,0 <foo>
+.*:[ 	]+fef3ace3[ 	]+beqi[ 	]+t2,15,0 <foo>
+.*:[ 	]+fffe2ae3[ 	]+beqi[ 	]+t3,31,0 <foo>
+.*:[ 	]+fe02b8e3[ 	]+bnei[ 	]+t0,-1,0 <foo>
+.*:[ 	]+fe5eb6e3[ 	]+bnei[ 	]+t4,5,0 <foo>
+.*:[ 	]+fef3b4e3[ 	]+bnei[ 	]+t2,15,0 <foo>
+.*:[ 	]+fffe32e3[ 	]+bnei[ 	]+t3,31,0 <foo>
diff --git a/gas/testsuite/gas/riscv/zibi.s b/gas/testsuite/gas/riscv/zibi.s
new file mode 100644
index 00000000000..a020e2be00d
--- /dev/null
+++ b/gas/testsuite/gas/riscv/zibi.s
@@ -0,0 +1,9 @@
+foo:
+	beqi t0, -1, foo
+	beqi t4, 5, foo
+	beqi t2, 15, foo
+	beqi t3, 31, foo
+	bnei t0, -1, foo
+	bnei t4, 5, foo
+	bnei t2, 15, foo
+	bnei t3, 31, foo
diff --git a/include/opcode/riscv-opc.h b/include/opcode/riscv-opc.h
index 9caefeb604b..c648a0c554c 100644
--- a/include/opcode/riscv-opc.h
+++ b/include/opcode/riscv-opc.h
@@ -63,6 +63,10 @@
 #define MASK_BEQ  0x707f
 #define MATCH_BNE 0x1063
 #define MASK_BNE  0x707f
+#define MATCH_BEQI 0x2063
+#define MASK_BEQI  0x707f
+#define MATCH_BNEI 0x3063
+#define MASK_BNEI  0x707f
 #define MATCH_BLT 0x4063
 #define MASK_BLT  0x707f
 #define MATCH_BGE 0x5063
diff --git a/include/opcode/riscv.h b/include/opcode/riscv.h
index ed962f4377b..3734d814a20 100644
--- a/include/opcode/riscv.h
+++ b/include/opcode/riscv.h
@@ -602,6 +602,7 @@ enum riscv_insn_class
   INSN_CLASS_ZICBOM,
   INSN_CLASS_ZICBOP,
   INSN_CLASS_ZICBOZ,
+  INSN_CLASS_ZIBI,
   INSN_CLASS_ZABHA,
   INSN_CLASS_ZACAS,
   INSN_CLASS_ZABHA_AND_ZACAS,
diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c
index 5672e3f5d88..9da420afe63 100644
--- a/opcodes/riscv-dis.c
+++ b/opcodes/riscv-dis.c
@@ -797,6 +797,22 @@ print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info
 		  goto undefined_modifier;
 		}
 	      break;
+	    case 'b':
+	      switch (*++oparg)
+		{
+		case 'i':
+		  {
+		    int value = EXTRACT_OPERAND (RS2, l);
+		    if (value == 0)
+		      value = -1;
+		    print (info->stream, dis_style_immediate, "%d",
+			   (int) value);
+		  }
+		  break;
+		default:
+		  goto undefined_modifier;
+		}
+	      break;
 	    case 'f':
 	      switch (*++oparg)
 		{
diff --git a/opcodes/riscv-opc.c b/opcodes/riscv-opc.c
index bb052bdc7bc..3f0e4b6d6ba 100644
--- a/opcodes/riscv-opc.c
+++ b/opcodes/riscv-opc.c
@@ -554,6 +554,8 @@ const struct riscv_opcode riscv_opcodes[] =
 {"bnez",        0, INSN_CLASS_I, "s,p",       MATCH_BNE, MASK_BNE|MASK_RS2, match_opcode, INSN_ALIAS|INSN_CONDBRANCH },
 {"bne",         0, INSN_CLASS_ZCA, "Cs,Cz,Cp",  MATCH_C_BNEZ, MASK_C_BNEZ, match_opcode, INSN_ALIAS|INSN_CONDBRANCH|INSN_NON_EXACT },
 {"bne",         0, INSN_CLASS_I, "s,t,p",     MATCH_BNE, MASK_BNE, match_opcode, INSN_CONDBRANCH },
+{"beqi",        0, INSN_CLASS_ZIBI, "s,Wbi,p", MATCH_BEQI, MASK_BEQI, match_opcode, INSN_CONDBRANCH },
+{"bnei",        0, INSN_CLASS_ZIBI, "s,Wbi,p", MATCH_BNEI, MASK_BNEI, match_opcode, INSN_CONDBRANCH },
 {"addi",        0, INSN_CLASS_ZCA, "Ct,Cc,CK",  MATCH_C_ADDI4SPN, MASK_C_ADDI4SPN, match_c_addi4spn, INSN_ALIAS | INSN_NON_EXACT },
 {"addi",        0, INSN_CLASS_ZCA, "d,CU,Cj",   MATCH_C_ADDI, MASK_C_ADDI, match_rd_nonzero, INSN_ALIAS | INSN_NON_EXACT },
 {"addi",        0, INSN_CLASS_ZCA, "d,CU,z",    MATCH_C_NOP, MASK_C_ADDI|MASK_RVC_IMM, match_c_nop, INSN_ALIAS | INSN_NON_EXACT },


More information about the Binutils mailing list