Skip to content

Commit d00ee17

Browse files
committed
impl translation for fallible f2i conversions
1 parent 10dee0f commit d00ee17

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

crates/wasmi/src/engine/translator/func2/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,38 @@ impl FuncTranslator {
742742
)
743743
}
744744

745+
/// Translates a unary Wasm instruction to Wasmi bytecode.
746+
fn translate_unary_fallible<T, R>(
747+
&mut self,
748+
make_instr: fn(result: Reg, input: Reg) -> Instruction,
749+
consteval: fn(input: T) -> Result<R, TrapCode>,
750+
) -> Result<(), Error>
751+
where
752+
T: From<TypedVal>,
753+
R: Into<TypedVal> + Typed,
754+
{
755+
bail_unreachable!(self);
756+
let input = self.stack.pop();
757+
if let Operand::Immediate(input) = input {
758+
let input = T::from(input.val());
759+
match consteval(input) {
760+
Ok(result) => {
761+
self.stack.push_immediate(result)?;
762+
}
763+
Err(trap) => {
764+
self.translate_trap(trap)?;
765+
}
766+
}
767+
return Ok(());
768+
}
769+
let input = self.layout.operand_to_reg(input)?;
770+
self.push_instr_with_result(
771+
<R as Typed>::TY,
772+
|result| make_instr(result, input),
773+
FuelCostsProvider::base,
774+
)
775+
}
776+
745777
/// Creates a new 16-bit encoded [`Operand16`] from the given `value`.
746778
pub fn make_imm16<T>(&mut self, value: T) -> Result<Operand16<T>, Error>
747779
where

crates/wasmi/src/engine/translator/func2/visit.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,19 +1061,31 @@ impl<'a> VisitOperator<'a> for FuncTranslator {
10611061
}
10621062

10631063
fn visit_i32_trunc_f32_s(&mut self) -> Self::Output {
1064-
todo!()
1064+
self.translate_unary_fallible::<f32, i32>(
1065+
Instruction::i32_trunc_f32_s,
1066+
wasm::i32_trunc_f32_s,
1067+
)
10651068
}
10661069

10671070
fn visit_i32_trunc_f32_u(&mut self) -> Self::Output {
1068-
todo!()
1071+
self.translate_unary_fallible::<f32, u32>(
1072+
Instruction::i32_trunc_f32_u,
1073+
wasm::i32_trunc_f32_u,
1074+
)
10691075
}
10701076

10711077
fn visit_i32_trunc_f64_s(&mut self) -> Self::Output {
1072-
todo!()
1078+
self.translate_unary_fallible::<f64, i32>(
1079+
Instruction::i32_trunc_f64_s,
1080+
wasm::i32_trunc_f64_s,
1081+
)
10731082
}
10741083

10751084
fn visit_i32_trunc_f64_u(&mut self) -> Self::Output {
1076-
todo!()
1085+
self.translate_unary_fallible::<f64, u32>(
1086+
Instruction::i32_trunc_f64_u,
1087+
wasm::i32_trunc_f64_u,
1088+
)
10771089
}
10781090

10791091
fn visit_i64_extend_i32_s(&mut self) -> Self::Output {
@@ -1085,19 +1097,31 @@ impl<'a> VisitOperator<'a> for FuncTranslator {
10851097
}
10861098

10871099
fn visit_i64_trunc_f32_s(&mut self) -> Self::Output {
1088-
todo!()
1100+
self.translate_unary_fallible::<f32, i64>(
1101+
Instruction::i64_trunc_f32_s,
1102+
wasm::i64_trunc_f32_s,
1103+
)
10891104
}
10901105

10911106
fn visit_i64_trunc_f32_u(&mut self) -> Self::Output {
1092-
todo!()
1107+
self.translate_unary_fallible::<f32, u64>(
1108+
Instruction::i64_trunc_f32_u,
1109+
wasm::i64_trunc_f32_u,
1110+
)
10931111
}
10941112

10951113
fn visit_i64_trunc_f64_s(&mut self) -> Self::Output {
1096-
todo!()
1114+
self.translate_unary_fallible::<f64, i64>(
1115+
Instruction::i64_trunc_f64_s,
1116+
wasm::i64_trunc_f64_s,
1117+
)
10971118
}
10981119

10991120
fn visit_i64_trunc_f64_u(&mut self) -> Self::Output {
1100-
todo!()
1121+
self.translate_unary_fallible::<f64, u64>(
1122+
Instruction::i64_trunc_f64_u,
1123+
wasm::i64_trunc_f64_u,
1124+
)
11011125
}
11021126

11031127
fn visit_f32_convert_i32_s(&mut self) -> Self::Output {

0 commit comments

Comments
 (0)