Files
wasmtime/lib/cretonne/src/isa/riscv/binemit.rs
Jakob Stoklund Olesen d66a9d196e Implement binary emission of RISC-V return instructions.
The return address is now always supplied in %x1, so the return address
predictor will recognize the jalr as a return and not some indirect
branch.
2017-04-19 16:26:04 -07:00

336 lines
10 KiB
Rust

//! Emitting binary RISC-V machine code.
use binemit::{CodeSink, Reloc, bad_encoding};
use ir::{Function, Inst, InstructionData};
use isa::RegUnit;
use predicates::is_signed_int;
include!(concat!(env!("OUT_DIR"), "/binemit-riscv.rs"));
/// RISC-V relocation kinds.
pub enum RelocKind {
/// A jal call to a function.
Call,
}
pub static RELOC_NAMES: [&'static str; 1] = ["Call"];
impl Into<Reloc> for RelocKind {
fn into(self) -> Reloc {
Reloc(self as u16)
}
}
/// R-type instructions.
///
/// 31 24 19 14 11 6
/// funct7 rs2 rs1 funct3 rd opcode
/// 25 20 15 12 7 0
///
/// Encoding bits: `opcode[6:2] | (funct3 << 5) | (funct7 << 8)`.
fn put_r<CS: CodeSink + ?Sized>(bits: u16,
rs1: RegUnit,
rs2: RegUnit,
rd: RegUnit,
sink: &mut CS) {
let bits = bits as u32;
let opcode5 = bits & 0x1f;
let funct3 = (bits >> 5) & 0x7;
let funct7 = (bits >> 8) & 0x7f;
let rs1 = rs1 as u32 & 0x1f;
let rs2 = rs2 as u32 & 0x1f;
let rd = rd as u32 & 0x1f;
// 0-6: opcode
let mut i = 0x3;
i |= opcode5 << 2;
i |= rd << 7;
i |= funct3 << 12;
i |= rs1 << 15;
i |= rs2 << 20;
i |= funct7 << 25;
sink.put4(i);
}
/// R-type instructions with a shift amount instead of rs2.
///
/// 31 25 19 14 11 6
/// funct7 shamt rs1 funct3 rd opcode
/// 25 20 15 12 7 0
///
/// Both funct7 and shamt contribute to bit 25. In RV64, shamt uses it for shifts > 31.
///
/// Encoding bits: `opcode[6:2] | (funct3 << 5) | (funct7 << 8)`.
fn put_rshamt<CS: CodeSink + ?Sized>(bits: u16,
rs1: RegUnit,
shamt: i64,
rd: RegUnit,
sink: &mut CS) {
let bits = bits as u32;
let opcode5 = bits & 0x1f;
let funct3 = (bits >> 5) & 0x7;
let funct7 = (bits >> 8) & 0x7f;
let rs1 = rs1 as u32 & 0x1f;
let shamt = shamt as u32 & 0x3f;
let rd = rd as u32 & 0x1f;
// 0-6: opcode
let mut i = 0x3;
i |= opcode5 << 2;
i |= rd << 7;
i |= funct3 << 12;
i |= rs1 << 15;
i |= shamt << 20;
i |= funct7 << 25;
sink.put4(i);
}
fn recipe_r<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Binary { args, .. } = func.dfg[inst] {
put_r(func.encodings[inst].bits(),
func.locations[args[0]].unwrap_reg(),
func.locations[args[1]].unwrap_reg(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected Binary format: {:?}", func.dfg[inst]);
}
}
fn recipe_ricmp<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::IntCompare { args, .. } = func.dfg[inst] {
put_r(func.encodings[inst].bits(),
func.locations[args[0]].unwrap_reg(),
func.locations[args[1]].unwrap_reg(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected IntCompare format: {:?}", func.dfg[inst]);
}
}
fn recipe_rshamt<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::BinaryImm { arg, imm, .. } = func.dfg[inst] {
put_rshamt(func.encodings[inst].bits(),
func.locations[arg].unwrap_reg(),
imm.into(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected BinaryImm format: {:?}", func.dfg[inst]);
}
}
/// I-type instructions.
///
/// 31 19 14 11 6
/// imm rs1 funct3 rd opcode
/// 20 15 12 7 0
///
/// Encoding bits: `opcode[6:2] | (funct3 << 5)`
fn put_i<CS: CodeSink + ?Sized>(bits: u16, rs1: RegUnit, imm: i64, rd: RegUnit, sink: &mut CS) {
let bits = bits as u32;
let opcode5 = bits & 0x1f;
let funct3 = (bits >> 5) & 0x7;
let rs1 = rs1 as u32 & 0x1f;
let rd = rd as u32 & 0x1f;
// 0-6: opcode
let mut i = 0x3;
i |= opcode5 << 2;
i |= rd << 7;
i |= funct3 << 12;
i |= rs1 << 15;
i |= (imm << 20) as u32;
sink.put4(i);
}
fn recipe_i<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::BinaryImm { arg, imm, .. } = func.dfg[inst] {
put_i(func.encodings[inst].bits(),
func.locations[arg].unwrap_reg(),
imm.into(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected BinaryImm format: {:?}", func.dfg[inst]);
}
}
fn recipe_iicmp<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::IntCompareImm { arg, imm, .. } = func.dfg[inst] {
put_i(func.encodings[inst].bits(),
func.locations[arg].unwrap_reg(),
imm.into(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected IntCompareImm format: {:?}", func.dfg[inst]);
}
}
fn recipe_iret<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
// Return instructions are always a jalr to %x1.
// The return address is provided as a special-purpose link argument.
put_i(func.encodings[inst].bits(),
1, // rs1 = %x1
0, // no offset.
0, // rd = %x0: no address written.
sink);
}
/// U-type instructions.
///
/// 31 11 6
/// imm rd opcode
/// 12 7 0
///
/// Encoding bits: `opcode[6:2] | (funct3 << 5)`
fn put_u<CS: CodeSink + ?Sized>(bits: u16, imm: i64, rd: RegUnit, sink: &mut CS) {
let bits = bits as u32;
let opcode5 = bits & 0x1f;
let rd = rd as u32 & 0x1f;
// 0-6: opcode
let mut i = 0x3;
i |= opcode5 << 2;
i |= rd << 7;
i |= imm as u32 & 0xfffff000;
sink.put4(i);
}
fn recipe_u<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::UnaryImm { imm, .. } = func.dfg[inst] {
put_u(func.encodings[inst].bits(),
imm.into(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected UnaryImm format: {:?}", func.dfg[inst]);
}
}
/// SB-type branch instructions.
///
/// 31 24 19 14 11 6
/// imm rs2 rs1 funct3 imm opcode
/// 25 20 15 12 7 0
///
/// Encoding bits: `opcode[6:2] | (funct3 << 5)`
fn put_sb<CS: CodeSink + ?Sized>(bits: u16, imm: i64, rs1: RegUnit, rs2: RegUnit, sink: &mut CS) {
let bits = bits as u32;
let opcode5 = bits & 0x1f;
let funct3 = (bits >> 5) & 0x7;
let rs1 = rs1 as u32 & 0x1f;
let rs2 = rs2 as u32 & 0x1f;
assert!(is_signed_int(imm, 13, 1), "SB out of range {:#x}", imm);
let imm = imm as u32;
// 0-6: opcode
let mut i = 0x3;
i |= opcode5 << 2;
i |= funct3 << 12;
i |= rs1 << 15;
i |= rs2 << 20;
// The displacement is completely hashed up.
i |= ((imm >> 11) & 0x1) << 7;
i |= ((imm >> 1) & 0xf) << 8;
i |= ((imm >> 5) & 0x3f) << 25;
i |= ((imm >> 12) & 0x1) << 31;
sink.put4(i);
}
fn recipe_sb<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::BranchIcmp {
destination,
ref args,
..
} = func.dfg[inst] {
let dest = func.offsets[destination] as i64;
let disp = dest - sink.offset() as i64;
let args = &args.as_slice(&func.dfg.value_lists)[0..2];
put_sb(func.encodings[inst].bits(),
disp,
func.locations[args[0]].unwrap_reg(),
func.locations[args[1]].unwrap_reg(),
sink);
} else {
panic!("Expected BranchIcmp format: {:?}", func.dfg[inst]);
}
}
fn recipe_sbzero<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Branch {
destination,
ref args,
..
} = func.dfg[inst] {
let dest = func.offsets[destination] as i64;
let disp = dest - sink.offset() as i64;
let args = &args.as_slice(&func.dfg.value_lists)[0..1];
put_sb(func.encodings[inst].bits(),
disp,
func.locations[args[0]].unwrap_reg(),
0,
sink);
} else {
panic!("Expected Branch format: {:?}", func.dfg[inst]);
}
}
/// UJ-type jump instructions.
///
/// 31 11 6
/// imm rd opcode
/// 12 7 0
///
/// Encoding bits: `opcode[6:2]`
fn put_uj<CS: CodeSink + ?Sized>(bits: u16, imm: i64, rd: RegUnit, sink: &mut CS) {
let bits = bits as u32;
let opcode5 = bits & 0x1f;
let rd = rd as u32 & 0x1f;
assert!(is_signed_int(imm, 21, 1), "UJ out of range {:#x}", imm);
let imm = imm as u32;
// 0-6: opcode
let mut i = 0x3;
i |= opcode5 << 2;
i |= rd << 7;
// The displacement is completely hashed up.
i |= imm & 0xff000;
i |= ((imm >> 11) & 0x1) << 20;
i |= ((imm >> 1) & 0x3ff) << 21;
i |= ((imm >> 20) & 0x1) << 31;
sink.put4(i);
}
fn recipe_uj<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Jump { destination, .. } = func.dfg[inst] {
let dest = func.offsets[destination] as i64;
let disp = dest - sink.offset() as i64;
put_uj(func.encodings[inst].bits(), disp, 0, sink);
} else {
panic!("Expected Jump format: {:?}", func.dfg[inst]);
}
}
fn recipe_ujcall<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Call { func_ref, .. } = func.dfg[inst] {
sink.reloc_func(RelocKind::Call.into(), func_ref);
// rd=%x1 is the standard link register.
put_uj(func.encodings[inst].bits(), 0, 1, sink);
} else {
panic!("Expected Call format: {:?}", func.dfg[inst]);
}
}