File: emscripten-catch-unwind-wasm-eh.rs

package info (click to toggle)
rustc 1.86.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid
  • size: 913,560 kB
  • sloc: xml: 158,127; python: 35,921; javascript: 19,689; sh: 19,600; cpp: 18,906; ansic: 13,124; asm: 4,376; makefile: 708; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (65 lines) | stat: -rw-r--r-- 2,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//@ compile-flags: -Copt-level=3 --target wasm32-unknown-emscripten -Z emscripten-wasm-eh
//@ needs-llvm-components: webassembly

// Emscripten catch_unwind using wasm exceptions

#![feature(no_core, lang_items, intrinsics, rustc_attrs)]
#![crate_type = "lib"]
#![no_std]
#![no_core]

#[lang = "sized"]
trait Sized {}
#[lang = "freeze"]
trait Freeze {}
#[lang = "copy"]
trait Copy {}

impl<T> Copy for *mut T {}

#[rustc_intrinsic]
fn size_of<T>() -> usize {
    loop {}
}

extern "rust-intrinsic" {
    fn catch_unwind(
        try_fn: fn(_: *mut u8),
        data: *mut u8,
        catch_fn: fn(_: *mut u8, _: *mut u8),
    ) -> i32;
}

// CHECK-LABEL: @ptr_size
#[no_mangle]
pub fn ptr_size() -> usize {
    // CHECK: ret [[PTR_SIZE:.*]]
    size_of::<*mut u8>()
}

// CHECK-LABEL: @test_catch_unwind
#[no_mangle]
pub unsafe fn test_catch_unwind(
    try_fn: fn(_: *mut u8),
    data: *mut u8,
    catch_fn: fn(_: *mut u8, _: *mut u8),
) -> i32 {
    // CHECK: start:
    // CHECK: invoke void %try_fn(ptr %data)
    // CHECK:         to label %__rust_try.exit unwind label %catchswitch.i
    // CHECK:   catchswitch.i:                                    ; preds = %start
    // CHECK:   %catchswitch1.i = catchswitch within none [label %catchpad.i] unwind to caller

    // CHECK: catchpad.i:                                       ; preds = %catchswitch.i
    // CHECK:   %catchpad2.i = catchpad within %catchswitch1.i [ptr null]
    // CHECK:   %0 = tail call ptr @llvm.wasm.get.exception(token %catchpad2.i)
    // CHECK:   %1 = tail call i32 @llvm.wasm.get.ehselector(token %catchpad2.i)
    // CHECK:   call void %catch_fn(ptr %data, ptr %0) [ "funclet"(token %catchpad2.i) ]
    // CHECK:   catchret from %catchpad2.i to label %__rust_try.exit

    // CHECK: __rust_try.exit:                                  ; preds = %start, %catchpad.i
    // CHECK:   %common.ret.op.i = phi i32 [ 0, %start ], [ 1, %catchpad.i ]
    // CHECK:   ret i32 %common.ret.op.i

    catch_unwind(try_fn, data, catch_fn)
}