MachInst backends: handle SourceLocs out-of-band, not in Insts.
In existing MachInst backends, many instructions -- any that can trap or result in a relocation -- carry `SourceLoc` values in order to propagate the location-in-original-source to use to describe resulting traps or relocation errors. This is quite tedious, and also error-prone: it is likely that the necessary plumbing will be missed in some cases, and in any case, it's unnecessarily verbose. This PR factors out the `SourceLoc` handling so that it is tracked during emission as part of the `EmitState`, and plumbed through automatically by the machine-independent framework. Instruction emission code that directly emits trap or relocation records can query the current location as necessary. Then we only need to ensure that memory references and trap instructions, at their (one) emission point rather than their (many) lowering/generation points, are wired up correctly. This does have the side-effect that some loads and stores that do not correspond directly to user code's heap accesses will have unnecessary but harmless trap metadata. For example, the load that fetches a code offset from a jump table will have a 'heap out of bounds' trap record attached to it; but because it is bounds-checked, and will never actually trap if the lowering is correct, this should be harmless. The simplicity improvement here seemed more worthwhile to me than plumbing through a "corresponds to user-level load/store" bit, because the latter is a bit complex when we allow for op merging. Closes #2290: though it does not implement a full "metadata" scheme as described in that issue, this seems simpler overall.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//! Implementation of the standard x64 ABI.
|
||||
|
||||
use crate::ir::types::*;
|
||||
use crate::ir::{self, types, SourceLoc, TrapCode, Type};
|
||||
use crate::ir::{self, types, TrapCode, Type};
|
||||
use crate::isa;
|
||||
use crate::isa::{x64::inst::*, CallConv};
|
||||
use crate::machinst::abi_impl::*;
|
||||
@@ -252,11 +252,11 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
_ if ty.bytes() == 16 => ExtKind::None,
|
||||
_ => panic!("load_stack({})", ty),
|
||||
};
|
||||
Inst::load(ty, mem, into_reg, ext_kind, /* infallible */ None)
|
||||
Inst::load(ty, mem, into_reg, ext_kind)
|
||||
}
|
||||
|
||||
fn gen_store_stack(mem: StackAMode, from_reg: Reg, ty: Type) -> Self::I {
|
||||
Inst::store(ty, from_reg, mem, /* infallible */ None)
|
||||
Inst::store(ty, from_reg, mem)
|
||||
}
|
||||
|
||||
fn gen_move(to_reg: Writable<Reg>, from_reg: Reg, ty: Type) -> Self::I {
|
||||
@@ -274,9 +274,9 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
let ext_mode = ExtMode::new(from_bits as u16, to_bits as u16)
|
||||
.expect(&format!("invalid extension: {} -> {}", from_bits, to_bits));
|
||||
if is_signed {
|
||||
Inst::movsx_rm_r(ext_mode, RegMem::reg(from_reg), to_reg, None)
|
||||
Inst::movsx_rm_r(ext_mode, RegMem::reg(from_reg), to_reg)
|
||||
} else {
|
||||
Inst::movzx_rm_r(ext_mode, RegMem::reg(from_reg), to_reg, None)
|
||||
Inst::movzx_rm_r(ext_mode, RegMem::reg(from_reg), to_reg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,7 +308,6 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
Inst::TrapIf {
|
||||
// NBE == "> unsigned"; args above are reversed; this tests limit_reg > rsp.
|
||||
cc: CC::NBE,
|
||||
srcloc: SourceLoc::default(),
|
||||
trap_code: TrapCode::StackOverflow,
|
||||
},
|
||||
]
|
||||
@@ -335,13 +334,13 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
assert_eq!(ty, I64);
|
||||
let simm32 = offset as u32;
|
||||
let mem = Amode::imm_reg(simm32, base);
|
||||
Inst::load(ty, mem, into_reg, ExtKind::None, None)
|
||||
Inst::load(ty, mem, into_reg, ExtKind::None)
|
||||
}
|
||||
|
||||
fn gen_store_base_offset(base: Reg, offset: i32, from_reg: Reg, ty: Type) -> Self::I {
|
||||
let simm32 = offset as u32;
|
||||
let mem = Amode::imm_reg(simm32, base);
|
||||
Inst::store(ty, from_reg, mem, None)
|
||||
Inst::store(ty, from_reg, mem)
|
||||
}
|
||||
|
||||
fn gen_sp_reg_adjust(amount: i32) -> SmallVec<[Self::I; 2]> {
|
||||
@@ -426,7 +425,6 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
/* bytes = */ 8,
|
||||
r_reg.to_reg(),
|
||||
Amode::imm_reg(cur_offset, regs::rsp()),
|
||||
None,
|
||||
));
|
||||
cur_offset += 8;
|
||||
}
|
||||
@@ -461,7 +459,6 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
insts.push(Inst::mov64_m_r(
|
||||
Amode::imm_reg(cur_offset, regs::rsp()),
|
||||
Writable::from_reg(rreg.to_reg()),
|
||||
None,
|
||||
));
|
||||
cur_offset += 8;
|
||||
}
|
||||
@@ -486,7 +483,6 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
insts.push(Inst::mov64_m_r(
|
||||
Amode::imm_reg(off as u32, regs::rbp()),
|
||||
Writable::from_reg(regs::r14()),
|
||||
None,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -498,7 +494,6 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
dest: &CallDest,
|
||||
uses: Vec<Reg>,
|
||||
defs: Vec<Writable<Reg>>,
|
||||
loc: SourceLoc,
|
||||
opcode: ir::Opcode,
|
||||
tmp: Writable<Reg>,
|
||||
_callee_conv: isa::CallConv,
|
||||
@@ -509,7 +504,7 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
&CallDest::ExtName(ref name, RelocDistance::Near) => {
|
||||
insts.push((
|
||||
InstIsSafepoint::Yes,
|
||||
Inst::call_known(name.clone(), uses, defs, loc, opcode),
|
||||
Inst::call_known(name.clone(), uses, defs, opcode),
|
||||
));
|
||||
}
|
||||
&CallDest::ExtName(ref name, RelocDistance::Far) => {
|
||||
@@ -519,18 +514,17 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
dst: tmp,
|
||||
name: Box::new(name.clone()),
|
||||
offset: 0,
|
||||
srcloc: loc,
|
||||
},
|
||||
));
|
||||
insts.push((
|
||||
InstIsSafepoint::Yes,
|
||||
Inst::call_unknown(RegMem::reg(tmp.to_reg()), uses, defs, loc, opcode),
|
||||
Inst::call_unknown(RegMem::reg(tmp.to_reg()), uses, defs, opcode),
|
||||
));
|
||||
}
|
||||
&CallDest::Reg(reg) => {
|
||||
insts.push((
|
||||
InstIsSafepoint::Yes,
|
||||
Inst::call_unknown(RegMem::reg(reg), uses, defs, loc, opcode),
|
||||
Inst::call_unknown(RegMem::reg(reg), uses, defs, opcode),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user