Skip to content

Commit

Permalink
riscv64: Add the remainder of Zca and Zcd instructions (#7080)
Browse files Browse the repository at this point in the history
* riscv64: Add `c.li` and `c.lui`

* riscv64: Add CB type instructions

`c.srli` / `c.srai` / `c.andi`

* riscv64: Add `sp` relative load instructions

* riscv64: Return Option from try_emit_compressed

* riscv64: Implement stack based stores

* riscv64: Add compressed stores

* riscv64: Add compressed loads
  • Loading branch information
afonso360 authored Sep 25, 2023
1 parent 38bc7e9 commit 6c438d4
Show file tree
Hide file tree
Showing 7 changed files with 1,058 additions and 41 deletions.
32 changes: 32 additions & 0 deletions cranelift/codegen/src/isa/riscv64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -741,13 +741,45 @@
(CAddiw)
(CAddi16sp)
(CSlli)
(CLi)
(CLui)
(CLwsp)
(CLdsp)
(CFldsp)
))

;; Opcodes for the CIW compressed instruction format
(type CiwOp (enum
(CAddi4spn)
))

;; Opcodes for the CB compressed instruction format
(type CbOp (enum
(CSrli)
(CSrai)
(CAndi)
))

;; Opcodes for the CSS compressed instruction format
(type CssOp (enum
(CSwsp)
(CSdsp)
(CFsdsp)
))

;; Opcodes for the CS compressed instruction format
(type CsOp (enum
(CSw)
(CSd)
(CFsd)
))

;; Opcodes for the CL compressed instruction format
(type ClOp (enum
(CLw)
(CLd)
(CFld)
))

(type CsrRegOP (enum
;; Atomic Read/Write CSR
Expand Down
111 changes: 106 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, CiOp, CiwOp, CjOp, CrOp,
COpcodeSpace, CaOp, CbOp, CiOp, CiwOp, CjOp, ClOp, CrOp, CsOp, CssOp,
};
use crate::machinst::isle::WritableReg;

Expand Down Expand Up @@ -1317,6 +1317,15 @@ impl LoadOP {
}
}

pub(crate) fn size(&self) -> i64 {
match self {
Self::Lb | Self::Lbu => 1,
Self::Lh | Self::Lhu => 2,
Self::Lw | Self::Lwu | Self::Flw => 4,
Self::Ld | Self::Fld => 8,
}
}

pub(crate) fn op_code(self) -> u32 {
match self {
Self::Lb | Self::Lh | Self::Lw | Self::Lbu | Self::Lhu | Self::Lwu | Self::Ld => {
Expand Down Expand Up @@ -1363,6 +1372,16 @@ impl StoreOP {
_ => unreachable!(),
}
}

pub(crate) fn size(&self) -> i64 {
match self {
Self::Sb => 1,
Self::Sh => 2,
Self::Sw | Self::Fsw => 4,
Self::Sd | Self::Fsd => 8,
}
}

pub(crate) fn op_code(self) -> u32 {
match self {
Self::Sb | Self::Sh | Self::Sw | Self::Sd => 0b0100011,
Expand Down Expand Up @@ -1983,16 +2002,19 @@ impl CiOp {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CiOp::CAddi | CiOp::CSlli => 0b000,
CiOp::CAddiw => 0b001,
CiOp::CAddi16sp => 0b011,
CiOp::CAddiw | CiOp::CFldsp => 0b001,
CiOp::CLi | CiOp::CLwsp => 0b010,
CiOp::CAddi16sp | CiOp::CLui | CiOp::CLdsp => 0b011,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CiOp::CAddi | CiOp::CAddiw | CiOp::CAddi16sp => COpcodeSpace::C1,
CiOp::CSlli => COpcodeSpace::C2,
CiOp::CAddi | CiOp::CAddiw | CiOp::CAddi16sp | CiOp::CLi | CiOp::CLui => {
COpcodeSpace::C1
}
CiOp::CSlli | CiOp::CLwsp | CiOp::CLdsp | CiOp::CFldsp => COpcodeSpace::C2,
}
}
}
Expand All @@ -2012,3 +2034,82 @@ impl CiwOp {
}
}
}

impl CbOp {
pub fn funct3(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CbOp::CSrli | CbOp::CSrai | CbOp::CAndi => 0b100,
}
}

pub fn funct2(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CbOp::CSrli => 0b00,
CbOp::CSrai => 0b01,
CbOp::CAndi => 0b10,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CbOp::CSrli | CbOp::CSrai | CbOp::CAndi => COpcodeSpace::C1,
}
}
}

impl CssOp {
pub fn funct3(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CssOp::CFsdsp => 0b101,
CssOp::CSwsp => 0b110,
CssOp::CSdsp => 0b111,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CssOp::CSwsp | CssOp::CSdsp | CssOp::CFsdsp => COpcodeSpace::C2,
}
}
}

impl CsOp {
pub fn funct3(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CsOp::CFsd => 0b101,
CsOp::CSw => 0b110,
CsOp::CSd => 0b111,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CsOp::CSw | CsOp::CSd | CsOp::CFsd => COpcodeSpace::C0,
}
}
}

impl ClOp {
pub fn funct3(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
ClOp::CFld => 0b001,
ClOp::CLw => 0b010,
ClOp::CLd => 0b011,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
ClOp::CLw | ClOp::CLd | ClOp::CFld => COpcodeSpace::C0,
}
}
}
Loading

0 comments on commit 6c438d4

Please sign in to comment.