Skip to content

Commit

Permalink
riscv64: Add Zcb extension instructions (#7123)
Browse files Browse the repository at this point in the history
* riscv64: Add `c.mul`

* riscv64: Add `c.not`

* riscv64: Add zbb and zba dependent compressed instructions

* riscv64: Add `Zcb` loads and stores

* riscv64: Restrict immediate encoding for halfword compressed stores and loads

* riscv64: Reverse imm bits for bytewise compressed loads and stores
  • Loading branch information
afonso360 authored Oct 2, 2023
1 parent c642a56 commit 233786c
Show file tree
Hide file tree
Showing 6 changed files with 591 additions and 34 deletions.
24 changes: 24 additions & 0 deletions cranelift/codegen/src/isa/riscv64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@
(CSub)
(CAddw)
(CSubw)
(CMul)
))

;; Opcodes for the CJ compressed instruction format
Expand Down Expand Up @@ -781,6 +782,29 @@
(CFld)
))

;; Opcodes for the CSZN compressed instruction format
(type CsznOp (enum
(CNot)
(CZextb)
(CZexth)
(CZextw)
(CSextb)
(CSexth)
))

;; This is a mix of all Zcb memory adressing instructions
;;
;; Technically they are split across 4 different formats.
;; But they are all very similar, so we just group them all together.
(type ZcbMemOp (enum
(CLbu)
(CLhu)
(CLh)
(CSb)
(CSh)
))


(type CsrRegOP (enum
;; Atomic Read/Write CSR
(CsrRW)
Expand Down
82 changes: 77 additions & 5 deletions cranelift/codegen/src/isa/riscv64/inst/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ir::condcodes::CondCode;
use crate::isa::riscv64::inst::{reg_name, reg_to_gpr_num};

use crate::isa::riscv64::lower::isle::generated_code::{
COpcodeSpace, CaOp, CbOp, CiOp, CiwOp, CjOp, ClOp, CrOp, CsOp, CssOp,
COpcodeSpace, CaOp, CbOp, CiOp, CiwOp, CjOp, ClOp, CrOp, CsOp, CssOp, CsznOp, ZcbMemOp,
};
use crate::machinst::isle::WritableReg;

Expand Down Expand Up @@ -1923,23 +1923,28 @@ impl CaOp {
CaOp::CSub => 0b00,
CaOp::CAddw => 0b01,
CaOp::CSubw => 0b00,
CaOp::CMul => 0b10,
}
}

pub fn funct6(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CaOp::CAnd | CaOp::COr | CaOp::CXor | CaOp::CSub => 0b100_011,
CaOp::CSubw | CaOp::CAddw => 0b100_111,
CaOp::CSubw | CaOp::CAddw | CaOp::CMul => 0b100_111,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CaOp::CAnd | CaOp::COr | CaOp::CXor | CaOp::CSub | CaOp::CAddw | CaOp::CSubw => {
COpcodeSpace::C1
}
CaOp::CAnd
| CaOp::COr
| CaOp::CXor
| CaOp::CSub
| CaOp::CAddw
| CaOp::CSubw
| CaOp::CMul => COpcodeSpace::C1,
}
}
}
Expand Down Expand Up @@ -2076,3 +2081,70 @@ impl ClOp {
}
}
}

impl CsznOp {
pub fn funct6(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CsznOp::CNot
| CsznOp::CZextw
| CsznOp::CZextb
| CsznOp::CZexth
| CsznOp::CSextb
| CsznOp::CSexth => 0b100_111,
}
}

pub fn funct5(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CsznOp::CNot => 0b11_101,
CsznOp::CZextb => 0b11_000,
CsznOp::CZexth => 0b11_010,
CsznOp::CZextw => 0b11_100,
CsznOp::CSextb => 0b11_001,
CsznOp::CSexth => 0b11_011,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CsznOp::CNot
| CsznOp::CZextb
| CsznOp::CZexth
| CsznOp::CZextw
| CsznOp::CSextb
| CsznOp::CSexth => COpcodeSpace::C1,
}
}
}

impl ZcbMemOp {
pub fn funct6(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
ZcbMemOp::CLbu => 0b100_000,
// These two opcodes are differentiated in the imm field of the instruction.
ZcbMemOp::CLhu | ZcbMemOp::CLh => 0b100_001,
ZcbMemOp::CSb => 0b100_010,
ZcbMemOp::CSh => 0b100_011,
}
}

pub fn imm_bits(&self) -> u8 {
match self {
ZcbMemOp::CLhu | ZcbMemOp::CLh | ZcbMemOp::CSh => 1,
ZcbMemOp::CLbu | ZcbMemOp::CSb => 2,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
ZcbMemOp::CLbu | ZcbMemOp::CLhu | ZcbMemOp::CLh | ZcbMemOp::CSb | ZcbMemOp::CSh => {
COpcodeSpace::C0
}
}
}
}
Loading

0 comments on commit 233786c

Please sign in to comment.