cranelift: Use ValueConversionKind in branches
This commit is contained in:
committed by
Andrew Brown
parent
037bd41c67
commit
7526cdc65e
@@ -142,16 +142,16 @@ where
|
|||||||
// Interpret a Cranelift instruction.
|
// Interpret a Cranelift instruction.
|
||||||
Ok(match inst.opcode() {
|
Ok(match inst.opcode() {
|
||||||
Opcode::Jump | Opcode::Fallthrough => ControlFlow::ContinueAt(branch(), args()?),
|
Opcode::Jump | Opcode::Fallthrough => ControlFlow::ContinueAt(branch(), args()?),
|
||||||
Opcode::Brz => branch_when(match arg(0)?.ty() {
|
Opcode::Brz => branch_when(
|
||||||
ty if ty.is_bool() => !arg(0)?.into_bool()?,
|
!arg(0)?
|
||||||
ty if ty.is_int() => arg(0)?.into_int()? == 0,
|
.convert(ValueConversionKind::ToBoolean)?
|
||||||
_ => return Err(StepError::ValueError(ValueError::InvalidValue(types::B1))),
|
.into_bool()?,
|
||||||
})?,
|
)?,
|
||||||
Opcode::Brnz => branch_when(match arg(0)?.ty() {
|
Opcode::Brnz => branch_when(
|
||||||
ty if ty.is_bool() => arg(0)?.into_bool()?,
|
arg(0)?
|
||||||
ty if ty.is_int() => arg(0)?.into_int()? != 0,
|
.convert(ValueConversionKind::ToBoolean)?
|
||||||
_ => return Err(StepError::ValueError(ValueError::InvalidValue(types::B1))),
|
.into_bool()?,
|
||||||
})?,
|
)?,
|
||||||
Opcode::BrIcmp => branch_when(icmp(inst.cond_code().unwrap(), &arg(0)?, &arg(1)?)?)?,
|
Opcode::BrIcmp => branch_when(icmp(inst.cond_code().unwrap(), &arg(0)?, &arg(1)?)?)?,
|
||||||
Opcode::Brif => branch_when(state.has_iflag(inst.cond_code().unwrap()))?,
|
Opcode::Brif => branch_when(state.has_iflag(inst.cond_code().unwrap()))?,
|
||||||
Opcode::Brff => branch_when(state.has_fflag(inst.fp_cond_code().unwrap()))?,
|
Opcode::Brff => branch_when(state.has_fflag(inst.fp_cond_code().unwrap()))?,
|
||||||
|
|||||||
@@ -108,6 +108,9 @@ pub enum ValueConversionKind {
|
|||||||
/// Convert a floating point number by rounding to the nearest possible value with ties to even.
|
/// Convert a floating point number by rounding to the nearest possible value with ties to even.
|
||||||
/// See `fdemote`, e.g.
|
/// See `fdemote`, e.g.
|
||||||
RoundNearestEven(Type),
|
RoundNearestEven(Type),
|
||||||
|
/// Converts an integer into a boolean, zero integers are converted into a
|
||||||
|
/// `false`, while other integers are converted into `true`. Booleans are passed through.
|
||||||
|
ToBoolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper for creating match expressions over [DataValue].
|
/// Helper for creating match expressions over [DataValue].
|
||||||
@@ -263,6 +266,11 @@ impl Value for DataValue {
|
|||||||
(types::F64, types::F32) => unimplemented!(),
|
(types::F64, types::F32) => unimplemented!(),
|
||||||
_ => unimplemented!("conversion: {} -> {:?}", self.ty(), kind),
|
_ => unimplemented!("conversion: {} -> {:?}", self.ty(), kind),
|
||||||
},
|
},
|
||||||
|
ValueConversionKind::ToBoolean => match self.ty() {
|
||||||
|
ty if ty.is_bool() => DataValue::B(self.into_bool()?),
|
||||||
|
ty if ty.is_int() => DataValue::B(self.into_int()? != 0),
|
||||||
|
ty => unimplemented!("conversion: {} -> {:?}", ty, kind),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user