From e6c391ab33a6441d812190aededfeee3d32c0857 Mon Sep 17 00:00:00 2001 From: Noa Date: Tue, 27 Feb 2024 22:39:57 -0600 Subject: [PATCH] [wasm32] Add an intrinsic for the throw instruction --- crates/core_arch/src/wasm32/mod.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/core_arch/src/wasm32/mod.rs b/crates/core_arch/src/wasm32/mod.rs index e07b361e64..7603002993 100644 --- a/crates/core_arch/src/wasm32/mod.rs +++ b/crates/core_arch/src/wasm32/mod.rs @@ -31,3 +31,22 @@ pub use self::memory::*; pub fn unreachable() -> ! { crate::intrinsics::abort() } + +extern "C" { + #[link_name = "llvm.wasm.throw"] + fn wasm_throw(tag: i32, ptr: *mut u8) -> !; +} + +/// Generates the [`throw`] instruction from the [exception-handling proposal] for WASM. +/// +/// This function is unlikely to be stabilized until codegen backends have better support. +/// +/// [`throw`]: https://webassembly.github.io/exception-handling/core/syntax/instructions.html#syntax-instr-control +/// [exception-handling proposal]: https://github.com/WebAssembly/exception-handling +#[cfg_attr(test, assert_instr(throw, TAG = 0, ptr = core::ptr::null_mut()))] +#[inline] +#[unstable(feature = "wasm_exception_handling_intrinsic", issue = "none")] +pub unsafe fn throw(ptr: *mut u8) -> ! { + static_assert!(TAG == 0); // LLVM only supports tag 0 == C++ right now. + wasm_throw(TAG, ptr) +}