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

@@ -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);
}