Generate instruction unwrapping code for binemit recipes.

Generate code to:

- Unwrap the instruction and generate an error if the instruction format
  doesn't match the recipe.
- Look up the value locations of register and stack arguments.

The recipe_* functions in the ISA binemit modules now take these
unwrapped items as arguments.

Also add an optional `emit` argument to the EncRecipe constructor which
makes it possible to provide inline Rust code snippets for code
emission. This requires a lot less boilerplate than recipe_* functions.
This commit is contained in:
Jakob Stoklund Olesen
2017-07-07 11:33:38 -07:00
parent 27d272ade0
commit 528e6ff3f5
6 changed files with 435 additions and 419 deletions

View File

@@ -33,6 +33,14 @@ impl ValueLoc {
}
}
/// Get the stack slot of this location, or panic.
pub fn unwrap_stack(self) -> StackSlot {
match self {
ValueLoc::Stack(ss) => ss,
_ => panic!("Expected stack slot: {:?}", self),
}
}
/// Return an object that can display this value location, using the register info from the
/// target ISA.
pub fn display<'a, R: Into<Option<&'a RegInfo>>>(self, regs: R) -> DisplayValueLoc<'a> {

View File

@@ -1,7 +1,8 @@
//! Emitting binary Intel machine code.
use binemit::{CodeSink, Reloc, bad_encoding};
use ir::{Function, Inst, InstructionData};
use ir::{self, Function, Inst, InstructionData, MemFlags};
use ir::immediates::{Imm64, Offset32};
use isa::RegUnit;
include!(concat!(env!("OUT_DIR"), "/binemit-intel.rs"));
@@ -100,257 +101,289 @@ fn modrm_disp32<CS: CodeSink + ?Sized>(rm: RegUnit, reg: RegUnit, sink: &mut CS)
sink.put1(b);
}
fn recipe_op1rr<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Binary { args, .. } = func.dfg[inst] {
put_op1(func.encodings[inst].bits(), sink);
modrm_rr(func.locations[args[0]].unwrap_reg(),
func.locations[args[1]].unwrap_reg(),
sink);
} else {
panic!("Expected Binary format: {:?}", func.dfg[inst]);
}
fn recipe_op1rr<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit) {
put_op1(bits, sink);
modrm_rr(in_reg0, in_reg1, sink);
}
fn recipe_op1ur<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Unary { arg, .. } = func.dfg[inst] {
put_op1(func.encodings[inst].bits(), sink);
let res = func.locations[func.dfg.first_result(inst)].unwrap_reg();
modrm_rr(res, func.locations[arg].unwrap_reg(), sink);
} else {
panic!("Expected Unary format: {:?}", func.dfg[inst]);
}
fn recipe_op1ur<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
out_reg0: RegUnit) {
put_op1(bits, sink);
modrm_rr(out_reg0, in_reg0, sink);
}
fn recipe_op1rc<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Binary { args, .. } = func.dfg[inst] {
let bits = func.encodings[inst].bits();
put_op1(bits, sink);
modrm_r_bits(func.locations[args[0]].unwrap_reg(), bits, sink);
} else {
panic!("Expected Binary format: {:?}", func.dfg[inst]);
}
fn recipe_op1rc<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit) {
put_op1(bits, sink);
modrm_r_bits(in_reg0, bits, sink);
}
fn recipe_op1rib<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::BinaryImm { arg, imm, .. } = func.dfg[inst] {
let bits = func.encodings[inst].bits();
put_op1(bits, sink);
modrm_r_bits(func.locations[arg].unwrap_reg(), bits, sink);
let imm: i64 = imm.into();
sink.put1(imm as u8);
} else {
panic!("Expected BinaryImm format: {:?}", func.dfg[inst]);
}
fn recipe_op1rib<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
imm: Imm64) {
put_op1(bits, sink);
modrm_r_bits(in_reg0, bits, sink);
let imm: i64 = imm.into();
sink.put1(imm as u8);
}
fn recipe_op1rid<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::BinaryImm { arg, imm, .. } = func.dfg[inst] {
let bits = func.encodings[inst].bits();
put_op1(bits, sink);
modrm_r_bits(func.locations[arg].unwrap_reg(), bits, sink);
let imm: i64 = imm.into();
sink.put4(imm as u32);
} else {
panic!("Expected BinaryImm format: {:?}", func.dfg[inst]);
}
fn recipe_op1rid<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
imm: Imm64) {
put_op1(bits, sink);
modrm_r_bits(in_reg0, bits, sink);
let imm: i64 = imm.into();
sink.put4(imm as u32);
}
fn recipe_op1uid<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::UnaryImm { imm, .. } = func.dfg[inst] {
let bits = func.encodings[inst].bits();
let reg = func.locations[func.dfg.first_result(inst)].unwrap_reg();
// The destination register is encoded in the low bits of the opcode. No ModR/M
put_op1(bits | (reg & 7), sink);
let imm: i64 = imm.into();
sink.put4(imm as u32);
} else {
panic!("Expected UnaryImm format: {:?}", func.dfg[inst]);
}
fn recipe_op1uid<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
imm: Imm64,
out_reg0: RegUnit) {
// The destination register is encoded in the low bits of the opcode. No ModR/M
put_op1(bits | (out_reg0 & 7), sink);
let imm: i64 = imm.into();
sink.put4(imm as u32);
}
// Store recipes.
fn recipe_op1st<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Store { args, .. } = func.dfg[inst] {
put_op1(func.encodings[inst].bits(), sink);
modrm_rm(func.locations[args[1]].unwrap_reg(),
func.locations[args[0]].unwrap_reg(),
sink);
} else {
panic!("Expected Store format: {:?}", func.dfg[inst]);
}
fn recipe_op1st<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit,
_flags: MemFlags,
_offset: Offset32) {
put_op1(bits, sink);
modrm_rm(in_reg1, in_reg0, sink);
}
// This is just a tighter register class constraint.
fn recipe_op1st_abcd<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
recipe_op1st(func, inst, sink)
fn recipe_op1st_abcd<CS: CodeSink + ?Sized>(func: &Function,
inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit,
flags: MemFlags,
offset: Offset32) {
recipe_op1st(func, inst, sink, bits, in_reg0, in_reg1, flags, offset)
}
fn recipe_mp1st<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Store { args, .. } = func.dfg[inst] {
put_mp1(func.encodings[inst].bits(), sink);
modrm_rm(func.locations[args[1]].unwrap_reg(),
func.locations[args[0]].unwrap_reg(),
sink);
} else {
panic!("Expected Store format: {:?}", func.dfg[inst]);
}
fn recipe_mp1st<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit,
_flags: MemFlags,
_offset: Offset32) {
put_mp1(bits, sink);
modrm_rm(in_reg1, in_reg0, sink);
}
fn recipe_op1stdisp8<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Store { args, offset, .. } = func.dfg[inst] {
put_op1(func.encodings[inst].bits(), sink);
modrm_disp8(func.locations[args[1]].unwrap_reg(),
func.locations[args[0]].unwrap_reg(),
sink);
let offset: i32 = offset.into();
sink.put1(offset as u8);
} else {
panic!("Expected Store format: {:?}", func.dfg[inst]);
}
fn recipe_op1stdisp8<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit,
_flags: MemFlags,
offset: Offset32) {
put_op1(bits, sink);
modrm_disp8(in_reg1, in_reg0, sink);
let offset: i32 = offset.into();
sink.put1(offset as u8);
}
fn recipe_op1stdisp8_abcd<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
recipe_op1stdisp8(func, inst, sink)
fn recipe_op1stdisp8_abcd<CS: CodeSink + ?Sized>(func: &Function,
inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit,
flags: MemFlags,
offset: Offset32) {
recipe_op1stdisp8(func, inst, sink, bits, in_reg0, in_reg1, flags, offset)
}
fn recipe_mp1stdisp8<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Store { args, offset, .. } = func.dfg[inst] {
put_mp1(func.encodings[inst].bits(), sink);
modrm_disp8(func.locations[args[1]].unwrap_reg(),
func.locations[args[0]].unwrap_reg(),
sink);
let offset: i32 = offset.into();
sink.put1(offset as u8);
} else {
panic!("Expected Store format: {:?}", func.dfg[inst]);
}
fn recipe_mp1stdisp8<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit,
_flags: MemFlags,
offset: Offset32) {
put_mp1(bits, sink);
modrm_disp8(in_reg1, in_reg0, sink);
let offset: i32 = offset.into();
sink.put1(offset as u8);
}
fn recipe_op1stdisp32<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Store { args, offset, .. } = func.dfg[inst] {
put_op1(func.encodings[inst].bits(), sink);
modrm_disp32(func.locations[args[1]].unwrap_reg(),
func.locations[args[0]].unwrap_reg(),
sink);
let offset: i32 = offset.into();
sink.put4(offset as u32);
} else {
panic!("Expected Store format: {:?}", func.dfg[inst]);
}
fn recipe_op1stdisp32<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit,
_flags: MemFlags,
offset: Offset32) {
put_op1(bits, sink);
modrm_disp32(in_reg1, in_reg0, sink);
let offset: i32 = offset.into();
sink.put4(offset as u32);
}
fn recipe_op1stdisp32_abcd<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
recipe_op1stdisp32(func, inst, sink)
fn recipe_op1stdisp32_abcd<CS: CodeSink + ?Sized>(func: &Function,
inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit,
flags: MemFlags,
offset: Offset32) {
recipe_op1stdisp32(func, inst, sink, bits, in_reg0, in_reg1, flags, offset)
}
fn recipe_mp1stdisp32<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Store { args, offset, .. } = func.dfg[inst] {
put_mp1(func.encodings[inst].bits(), sink);
modrm_disp32(func.locations[args[1]].unwrap_reg(),
func.locations[args[0]].unwrap_reg(),
sink);
let offset: i32 = offset.into();
sink.put4(offset as u32);
} else {
panic!("Expected Store format: {:?}", func.dfg[inst]);
}
fn recipe_mp1stdisp32<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
in_reg1: RegUnit,
_flags: MemFlags,
offset: Offset32) {
put_mp1(bits, sink);
modrm_disp32(in_reg1, in_reg0, sink);
let offset: i32 = offset.into();
sink.put4(offset as u32);
}
// Load recipes
fn recipe_op1ld<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Load { arg, .. } = func.dfg[inst] {
put_op1(func.encodings[inst].bits(), sink);
modrm_rm(func.locations[arg].unwrap_reg(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected Load format: {:?}", func.dfg[inst]);
}
}
fn recipe_op1lddisp8<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Load { arg, offset, .. } = func.dfg[inst] {
put_op1(func.encodings[inst].bits(), sink);
modrm_disp8(func.locations[arg].unwrap_reg(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
let offset: i32 = offset.into();
sink.put1(offset as u8);
} else {
panic!("Expected Load format: {:?}", func.dfg[inst]);
}
}
fn recipe_op1lddisp32<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Load { arg, offset, .. } = func.dfg[inst] {
put_op1(func.encodings[inst].bits(), sink);
modrm_disp32(func.locations[arg].unwrap_reg(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
let offset: i32 = offset.into();
sink.put4(offset as u32);
} else {
panic!("Expected Load format: {:?}", func.dfg[inst]);
}
}
fn recipe_op2ld<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Load { arg, .. } = func.dfg[inst] {
put_op2(func.encodings[inst].bits(), sink);
modrm_rm(func.locations[arg].unwrap_reg(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected Load format: {:?}", func.dfg[inst]);
}
}
fn recipe_op2lddisp8<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Load { arg, offset, .. } = func.dfg[inst] {
put_op2(func.encodings[inst].bits(), sink);
modrm_disp8(func.locations[arg].unwrap_reg(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
let offset: i32 = offset.into();
sink.put1(offset as u8);
} else {
panic!("Expected Load format: {:?}", func.dfg[inst]);
}
}
fn recipe_op2lddisp32<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Load { arg, offset, .. } = func.dfg[inst] {
put_op2(func.encodings[inst].bits(), sink);
modrm_disp32(func.locations[arg].unwrap_reg(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
let offset: i32 = offset.into();
sink.put4(offset as u32);
} else {
panic!("Expected Load format: {:?}", func.dfg[inst]);
}
}
fn recipe_op1call_id<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Call { func_ref, .. } = func.dfg[inst] {
put_op1(func.encodings[inst].bits(), sink);
sink.reloc_func(RelocKind::PCRel4.into(), func_ref);
sink.put4(0);
} else {
panic!("Expected Call format: {:?}", func.dfg[inst]);
}
}
fn recipe_op1call_r<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
let bits = func.encodings[inst].bits();
fn recipe_op1ld<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
_flags: MemFlags,
_offset: Offset32,
out_reg0: RegUnit) {
put_op1(bits, sink);
modrm_r_bits(func.locations[func.dfg.inst_args(inst)[0]].unwrap_reg(),
bits,
sink);
modrm_rm(in_reg0, out_reg0, sink);
}
fn recipe_op1ret<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
put_op1(func.encodings[inst].bits(), sink);
fn recipe_op1lddisp8<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
_flags: MemFlags,
offset: Offset32,
out_reg0: RegUnit) {
put_op1(bits, sink);
modrm_disp8(in_reg0, out_reg0, sink);
let offset: i32 = offset.into();
sink.put1(offset as u8);
}
fn recipe_op1lddisp32<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
_flags: MemFlags,
offset: Offset32,
out_reg0: RegUnit) {
put_op1(bits, sink);
modrm_disp32(in_reg0, out_reg0, sink);
let offset: i32 = offset.into();
sink.put4(offset as u32);
}
fn recipe_op2ld<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
_flags: MemFlags,
_offset: Offset32,
out_reg0: RegUnit) {
put_op2(bits, sink);
modrm_rm(in_reg0, out_reg0, sink);
}
fn recipe_op2lddisp8<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
_flags: MemFlags,
offset: Offset32,
out_reg0: RegUnit) {
put_op2(bits, sink);
modrm_disp8(in_reg0, out_reg0, sink);
let offset: i32 = offset.into();
sink.put1(offset as u8);
}
fn recipe_op2lddisp32<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
_flags: MemFlags,
offset: Offset32,
out_reg0: RegUnit) {
put_op2(bits, sink);
modrm_disp32(in_reg0, out_reg0, sink);
let offset: i32 = offset.into();
sink.put4(offset as u32);
}
fn recipe_op1call_id<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
func_ref: ir::FuncRef) {
put_op1(bits, sink);
sink.reloc_func(RelocKind::PCRel4.into(), func_ref);
sink.put4(0);
}
fn recipe_op1call_r<CS: CodeSink + ?Sized>(_func: &Function,
_inst: Inst,
sink: &mut CS,
bits: u16,
in_reg0: RegUnit,
_sig_ref: ir::SigRef) {
put_op1(bits, sink);
modrm_r_bits(in_reg0, bits, sink);
}
fn recipe_op1ret<CS: CodeSink + ?Sized>(_func: &Function, _inst: Inst, sink: &mut CS, bits: u16) {
put_op1(bits, sink);
}

View File

@@ -87,42 +87,6 @@ fn put_rshamt<CS: CodeSink + ?Sized>(bits: u16,
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
@@ -148,73 +112,6 @@ fn put_i<CS: CodeSink + ?Sized>(bits: u16, rs1: RegUnit, imm: i64, rd: RegUnit,
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_iz<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::UnaryImm { imm, .. } = func.dfg[inst] {
put_i(func.encodings[inst].bits(),
0,
imm.into(),
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected UnaryImm 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);
}
fn recipe_icall<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
// Indirect instructions are jalr with rd=%x1.
put_i(func.encodings[inst].bits(),
func.locations[func.dfg.inst_args(inst)[0]].unwrap_reg(),
0, // no offset.
1, // rd = %x1: link register.
sink);
}
fn recipe_icopy<CS: CodeSink + ?Sized>(func: &Function, inst: Inst, sink: &mut CS) {
if let InstructionData::Unary { arg, .. } = func.dfg[inst] {
put_i(func.encodings[inst].bits(),
func.locations[arg].unwrap_reg(),
0,
func.locations[func.dfg.first_result(inst)].unwrap_reg(),
sink);
} else {
panic!("Expected Unary format: {:?}", func.dfg[inst]);
}
}
/// U-type instructions.
///
/// 31 11 6
@@ -236,17 +133,6 @@ fn put_u<CS: CodeSink + ?Sized>(bits: u16, imm: i64, rd: RegUnit, sink: &mut CS)
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
@@ -280,44 +166,6 @@ fn put_sb<CS: CodeSink + ?Sized>(bits: u16, imm: i64, rs1: RegUnit, rs2: RegUnit
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
@@ -346,31 +194,3 @@ fn put_uj<CS: CodeSink + ?Sized>(bits: u16, imm: i64, rd: RegUnit, sink: &mut CS
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]);
}
}
fn recipe_gpsp<CS: CodeSink + ?Sized>(_func: &Function, _inst: Inst, _sink: &mut CS) {
unimplemented!();
}
fn recipe_gpfi<CS: CodeSink + ?Sized>(_func: &Function, _inst: Inst, _sink: &mut CS) {
unimplemented!();
}