Adds isa definitions and encodings for bit manipulation instructions.

PiperOrigin-RevId: 668931079
Change-Id: I1f6a272196467488011ab09a160b95f6f169bda0
diff --git a/riscv/BUILD b/riscv/BUILD
index 8eb8fea..123b886 100644
--- a/riscv/BUILD
+++ b/riscv/BUILD
@@ -292,6 +292,37 @@
 )
 
 mpact_isa_decoder(
+    name = "riscv32gzb_isa",
+    src = "riscv32gzb.isa",
+    includes = [
+        "riscv32g.isa",
+        "riscv32zb.isa",
+    ],
+    isa_name = "RiscV32GZB",
+    prefix = "riscv32gzb",
+    deps = [
+        ":riscv_bitmanip_instructions",
+        ":riscv_g",
+        ":riscv_v",
+        "@com_google_absl//absl/functional:bind_front",
+    ],
+)
+
+mpact_bin_fmt_decoder(
+    name = "riscv32gzb_bin_fmt",
+    src = "riscv32gzb.bin_fmt",
+    decoder_name = "RiscV32GZB",
+    includes = [
+        "riscv32g.bin_fmt",
+        "riscv32zb.bin_fmt",
+    ],
+    prefix = "riscv32gzb",
+    deps = [
+        ":riscv32gzb_isa",
+    ],
+)
+
+mpact_isa_decoder(
     name = "riscv64g_isa",
     src = "riscv64g.isa",
     includes = [],
diff --git a/riscv/riscv32gzb.bin_fmt b/riscv/riscv32gzb.bin_fmt
new file mode 100644
index 0000000..65e6ef0
--- /dev/null
+++ b/riscv/riscv32gzb.bin_fmt
@@ -0,0 +1,19 @@
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+decoder RiscV32GZB {
+  namespace mpact::sim::riscv::isa32gzb;
+  opcode_enum = "OpcodeEnum";
+  includes {
+    #include "riscv/riscv32gzb_decoder.h"
+  }
+  RiscV32GZBInst32 = {RiscVGInst32, RiscVZbaInst32, RiscVZbbInst32, RiscVZbcInst32, RiscVZbsInst32};
+  RiscV32GZBInst16 = {RiscVCInst16};
+}
+
+#include "riscv/riscv32g.bin_fmt"
+#include "riscv/riscv32zb.bin_fmt"
diff --git a/riscv/riscv32gzb.isa b/riscv/riscv32gzb.isa
new file mode 100644
index 0000000..490d7a0
--- /dev/null
+++ b/riscv/riscv32gzb.isa
@@ -0,0 +1,30 @@
+// Copyright 2024 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+isa RiscV32GZB {
+  namespace mpact::sim::riscv::isa32gzb;
+  slots {
+    riscv32gzb;
+  }
+}
+
+#include "riscv/riscv32g.isa"
+#include "riscv/riscv32zb.isa"
+
+slot riscv32gzb : riscv32g, riscv32_zba, riscv32_zbb, riscv32_zbc, riscv32_zbs {
+  default size = 4;
+  default opcode =
+    disasm: "Illegal instruction at 0x%(@:08x)",
+    semfunc: "&RiscVIllegalInstruction";
+}
diff --git a/riscv/riscv32zb.bin_fmt b/riscv/riscv32zb.bin_fmt
new file mode 100644
index 0000000..5f932ff
--- /dev/null
+++ b/riscv/riscv32zb.bin_fmt
@@ -0,0 +1,57 @@
+// Copyright 2024 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+instruction group RiscVZbaInst32[32] : Inst32Format {
+  sh1add: RType : func7 == 0b001'0000, func3 == 0b010, opcode == 0b011'0011;
+  sh2add: RType : func7 == 0b001'0000, func3 == 0b100, opcode == 0b011'0011;
+  sh3add: RType : func7 == 0b001'0000, func3 == 0b110, opcode == 0b011'0011;
+};
+
+instruction group RiscVZbbInst32[32] : Inst32Format {
+  andn:   RType : func7 == 0b010'0000, func3 == 0b111, opcode == 0b011'0011;
+  orn:    RType : func7 == 0b010'0000, func3 == 0b110, opcode == 0b011'0011;
+  xnor:   RType : func7 == 0b010'0000, func3 == 0b100, opcode == 0b011'0011;
+  clz:    RType : func7 == 0b011'0000, rs2 == 0b0'0000, func3 == 0b001, opcode == 0b001'0011;
+  ctz:    RType : func7 == 0b011'0000, rs2 == 0b0'0001, func3 == 0b001, opcode == 0b001'0011;
+  cpop:   RType : func7 == 0b011'0000, rs2 == 0b0'0010, func3 == 0b001, opcode == 0b001'0011;
+  max:    RType : func7 == 0b000'0101, func3 == 0b110, opcode == 0b011'0011;
+  maxu:   RType : func7 == 0b000'0101, func3 == 0b111, opcode == 0b011'0011;
+  min:    RType : func7 == 0b000'0101, func3 == 0b100, opcode == 0b011'0011;
+  minu:   RType : func7 == 0b000'0101, func3 == 0b101, opcode == 0b011'0011;
+  sext_b: RType : func7 == 0b011'0000, rs2 == 0b0'0100, func3 == 0b001, opcode == 0b001'0011;
+  sext_h: RType : func7 == 0b011'0000, rs2 == 0b0'0101, func3 == 0b001, opcode == 0b001'0011;
+  zext_h: RType : func7 == 0b000'0100, rs2 == 0b0'0000, func3 == 0b100, opcode == 0b011'0011;
+  rol:    RType : func7 == 0b011'0000, func3 == 0b001, opcode == 0b011'0011;
+  ror:    RType : func7 == 0b011'0000, func3 == 0b101, opcode == 0b011'0011;
+  rori:   RType : func7 == 0b011'0000, func3 == 0b101, opcode == 0b001'0011;
+  orcb:   RType : func7 == 0b001'0100, rs2 == 0b0'0111, func3 == 0b101, opcode == 0b001'0011;
+  rev8:   RType : func7 == 0b011'0100, rs2 == 0b1'1000, func3 == 0b101, opcode == 0b001'0011;
+};
+
+instruction group RiscVZbcInst32[32] : Inst32Format {
+  clmul:  RType : func7 == 0b000'0101, func3 == 0b001, opcode == 0b011'0011;
+  clmulh: RType : func7 == 0b000'0101, func3 == 0b011, opcode == 0b011'0011;
+  clmulr: RType : func7 == 0b000'0101, func3 == 0b010, opcode == 0b011'0011;
+};
+
+instruction group RiscVZbsInst32[32] : Inst32Format {
+  bclr:   RType : func7 == 0b010'0100, func3 == 0b001, opcode == 0b011'0011;
+  bclri:  RType : func7 == 0b010'0100, func3 == 0b001, opcode == 0b001'0011;
+  bext:   RType : func7 == 0b010'0100, func3 == 0b101, opcode == 0b011'0011;
+  bexti:  RType : func7 == 0b010'0100, func3 == 0b101, opcode == 0b001'0011;
+  binv:   RType : func7 == 0b011'0100, func3 == 0b001, opcode == 0b011'0011;
+  binvi:  RType : func7 == 0b011'0100, func3 == 0b001, opcode == 0b001'0011;
+  bset:   RType : func7 == 0b001'0100, func3 == 0b001, opcode == 0b011'0011;
+  bseti:  RType : func7 == 0b001'0100, func3 == 0b001, opcode == 0b001'0011;
+};
\ No newline at end of file
diff --git a/riscv/riscv32zb.isa b/riscv/riscv32zb.isa
new file mode 100644
index 0000000..02502c1
--- /dev/null
+++ b/riscv/riscv32zb.isa
@@ -0,0 +1,202 @@
+// Copyright 2024 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// This file contains the ISA description for the RiscV32 bitmanipulation
+// extension.
+
+includes {
+  #include "riscv/riscv_bitmanip_instructions.h"
+}
+
+disasm widths = {-18};
+
+slot riscv32_zba {
+  default size = 4;
+  default opcode =
+    disasm: "Illegal instruction at 0x%(@:08x)",
+    semfunc: "&RiscVIllegalInstruction";
+  resources ThreeOp = { next_pc, rs1, rs2 : rd[..rd]};
+  opcodes {
+    sh1add{: rs1, rs2, const1 : rd},
+      resources: ThreeOp,
+      disasm: "sh1add", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVShAdd";
+    sh2add{: rs1, rs2, const2 : rd},
+      resources: ThreeOp,
+      disasm: "sh2add", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVShAdd";
+    sh3add{: rs1, rs2, const3 : rd},
+      resources: ThreeOp,
+      disasm: "sh3add", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVShAdd";
+  }
+}
+
+slot riscv32_zbb {
+  default size = 4;
+  default opcode =
+    disasm: "Illegal instruction at 0x%(@:08x)",
+    semfunc: "&RiscVIllegalInstruction";
+  resources TwoOp = { next_pc, rs1 : rd[..rd]};
+  resources ThreeOp = { next_pc, rs1, rs2 : rd[..rd]};
+  opcodes {
+    // Logical with negate.
+    andn{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "andn", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVAndn";
+    orn{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "orn", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVOrn";
+    xnor{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "xor", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVXnor";
+    // Counte leading/trailing zero bits.
+    clz{: rs1 : rd},
+      resources: TwoOp,
+      disasm: "clz", "%rd, %rs1",
+      semfunc: "&RV32::RiscVClz";
+    ctz{: rs1 : rd},
+      resources: TwoOp,
+      disasm: "ctz", "%rd, %rs1",
+      semfunc: "&RV32::RiscVCtz";
+    // Count population
+    cpop{: rs1 : rd},
+      resources: TwoOp,
+      disasm: "cpop", "%rd, %rs1",
+      semfunc: "&RV32::RiscVCpop";
+    // Integer minimum/maximum.
+    max{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "max", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVMax";
+    maxu{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "max", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVMaxu";
+    min{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "min", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVMin";
+    minu{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "min", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVMinu";
+    // Sign and zero extension.
+    sext_b{: rs1 : rd},
+      resources: TwoOp,
+      disasm: "sext.b", "%rd, %rs1",
+      semfunc: "&RV32::RiscVSextB";
+    sext_h{: rs1 : rd},
+      resources: TwoOp,
+      disasm: "sext.h", "%rd, %rs1",
+      semfunc: "&RV32::RiscVSextH";
+    zext_h{: rs1 : rd},
+      resources: TwoOp,
+      disasm: "zext.h", "%rd, %rs1",
+      semfunc: "&RV32::RiscVZextH";
+    // Bitwise rotation.
+    rol{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "rol", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVrol";
+    ror{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "ror", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVror";
+    rori{: rs1, r_uimm5 : rd},
+      resources: TwoOp,
+      disasm: "rori", "%rd, %rs1, %r_uimm5",
+      semfunc: "&RV32::RiscVror";
+    // OR combine.
+    orcb{: rs1 : rd},
+      resources: TwoOp,
+      disasm: "orc", "%rd, %rs1",
+      semfunc: "&RV32::RiscVOrcb";
+    // Byte reverse.
+    rev8{: rs1 : rd},
+      resources: TwoOp,
+      disasm: "rev8", "%rd, %rs1",
+      semfunc: "&RV32::RiscVRev8";
+  }
+}
+
+slot riscv32_zbc {
+  default size = 4;
+  default opcode =
+    disasm: "Illegal instruction at 0x%(@:08x)",
+    semfunc: "&RiscVIllegalInstruction";
+  resources ThreeOp = { next_pc, rs1, rs2 : rd[..rd]};
+  opcodes {
+    // Carry-less multiplication.
+    clmul{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "clmul", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVClmul";
+    clmulh{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "clmulh", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVClmulh";
+    clmulr{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "clmulr", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVClmulr";
+  }
+}
+
+slot riscv32_zbs {
+  default size = 4;
+  default opcode =
+    disasm: "Illegal instruction at 0x%(@:08x)",
+    semfunc: "&RiscVIllegalInstruction";
+  resources TwoOp = { next_pc, rs1 : rd[..rd]};
+  resources ThreeOp = { next_pc, rs1, rs2 : rd[..rd]};
+  opcodes {
+    // Single-bit instructions.
+    bclr{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "bclr", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVBclr";
+    bclri{: rs1, r_uimm5 : rd},
+      resources: TwoOp,
+      disasm: "bclri", "%rd, %rs1, %r_uimm5",
+      semfunc: "&RV32::RiscVBclr";
+    bext{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "bext", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVBext";
+    bexti{: rs1, r_uimm5 : rd},
+      resources: TwoOp,
+      disasm: "bexti", "%rd, %rs1, %r_uimm5",
+      semfunc: "&RV32::RiscVBext";
+    binv{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "binv", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVBinv";
+    binvi{: rs1, r_uimm5 : rd},
+      resources: TwoOp,
+      disasm: "binvi", "%rd, %rs1, %r_uimm5",
+      semfunc: "&RV32::RiscVBinv";
+    bset{: rs1, rs2 : rd},
+      resources: ThreeOp,
+      disasm: "bset", "%rd, %rs1, %rs2",
+      semfunc: "&RV32::RiscVBset";
+    bseti{: rs1, r_uimm5 : rd},
+      resources: TwoOp,
+      disasm: "bseti", "%rd, %rs1, %r_uimm5",
+      semfunc: "&RV32::RiscVBset";
+  }
+}