Skip to content

Commit

Permalink
Assert that fixed allocations are to allocatable registers
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottt committed Oct 26, 2022
1 parent 79b47f0 commit e2221c2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cranelift/codegen/src/machinst/reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,26 @@ pub struct OperandCollector<'a, 'env, F: Fn(VReg) -> VReg> {
renamer: F,
}

macro_rules! debug_assert_allocatable {
($env:expr, $reg:expr) => {
if cfg!(debug_assertions) {
let class = $reg.class() as usize;
if let Some(reg) = pinned_vreg_to_preg($reg) {
assert!(
$env.preferred_regs_by_class[class]
.iter()
.copied()
.any(|r| r == reg)
|| $env.non_preferred_regs_by_class[class]
.iter()
.copied()
.any(|r| r == reg)
);
}
}
};
}

impl<'a, 'env, F: Fn(VReg) -> VReg> OperandCollector<'a, 'env, F> {
/// Start gathering operands into one flattened operand array.
pub fn new(operands: &'a mut Vec<Operand>, machine_env: &'env MachineEnv, renamer: F) -> Self {
Expand Down Expand Up @@ -367,13 +387,15 @@ impl<'a, 'env, F: Fn(VReg) -> VReg> OperandCollector<'a, 'env, F> {
/// Add a register "fixed use", which ties a vreg to a particular
/// RealReg at this point.
pub fn reg_fixed_use(&mut self, reg: Reg, rreg: Reg) {
debug_assert_allocatable!(self.machine_env, rreg.0);
let rreg = rreg.to_real_reg().expect("fixed reg is not a RealReg");
self.add_operand(Operand::reg_fixed_use(reg.into(), rreg.into()));
}

/// Add a register "fixed def", which ties a vreg to a particular
/// RealReg at this point.
pub fn reg_fixed_def(&mut self, reg: Writable<Reg>, rreg: Reg) {
debug_assert_allocatable!(self.machine_env, rreg.0);
let rreg = rreg.to_real_reg().expect("fixed reg is not a RealReg");
self.add_operand(Operand::reg_fixed_def(reg.to_reg().into(), rreg.into()));
}
Expand Down

0 comments on commit e2221c2

Please sign in to comment.