diff --git a/lib/compiler-llvm/src/translator/code.rs b/lib/compiler-llvm/src/translator/code.rs index df89a7665..a679c8e51 100644 --- a/lib/compiler-llvm/src/translator/code.rs +++ b/lib/compiler-llvm/src/translator/code.rs @@ -749,11 +749,14 @@ fn trap_if_not_representable_as_int<'ctx>( builder.build_conditional_branch(out_of_bounds, failure_block, continue_block); builder.position_at_end(failure_block); - builder.build_call( - intrinsics.throw_trap, - &[intrinsics.trap_illegal_arithmetic], - "throw", + let is_nan = builder.build_float_compare(FloatPredicate::UNO, value, value, "is_nan"); + let trap_code = builder.build_select( + is_nan, + intrinsics.trap_bad_conversion_to_integer, + intrinsics.trap_illegal_arithmetic, + "", ); + builder.build_call(intrinsics.throw_trap, &[trap_code], "throw"); builder.build_unreachable(); builder.position_at_end(continue_block); } diff --git a/lib/compiler-llvm/src/translator/intrinsics.rs b/lib/compiler-llvm/src/translator/intrinsics.rs index d1822d3b9..b077d69b5 100644 --- a/lib/compiler-llvm/src/translator/intrinsics.rs +++ b/lib/compiler-llvm/src/translator/intrinsics.rs @@ -147,6 +147,7 @@ pub struct Intrinsics<'ctx> { pub trap_memory_oob: BasicValueEnum<'ctx>, pub trap_illegal_arithmetic: BasicValueEnum<'ctx>, pub trap_integer_division_by_zero: BasicValueEnum<'ctx>, + pub trap_bad_conversion_to_integer: BasicValueEnum<'ctx>, pub trap_misaligned_atomic: BasicValueEnum<'ctx>, pub trap_table_access_oob: BasicValueEnum<'ctx>, @@ -493,6 +494,9 @@ impl<'ctx> Intrinsics<'ctx> { trap_integer_division_by_zero: i32_ty .const_int(TrapCode::IntegerDivisionByZero as _, false) .as_basic_value_enum(), + trap_bad_conversion_to_integer: i32_ty + .const_int(TrapCode::BadConversionToInteger as _, false) + .as_basic_value_enum(), // TODO: add misaligned atomic traps to wasmer runtime trap_misaligned_atomic: i32_ty .const_int(TrapCode::Interrupt as _, false)