wasmer_types/
libcalls.rs

1use enum_iterator::Sequence;
2use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
3#[cfg(feature = "enable-serde")]
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7/// The name of a runtime library routine.
8///
9/// This list is likely to grow over time.
10#[derive(
11    Copy, Clone, Debug, PartialEq, Eq, Hash, Sequence, RkyvSerialize, RkyvDeserialize, Archive,
12)]
13#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
14#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
15#[rkyv(derive(Debug, Hash, PartialEq, Eq), compare(PartialEq))]
16#[repr(u16)]
17pub enum LibCall {
18    /// ceil.f32
19    CeilF32,
20
21    /// ceil.f64
22    CeilF64,
23
24    /// floor.f32
25    FloorF32,
26
27    /// floor.f64
28    FloorF64,
29
30    /// nearest.f32
31    NearestF32,
32
33    /// nearest.f64
34    NearestF64,
35
36    /// trunc.f32
37    TruncF32,
38
39    /// trunc.f64
40    TruncF64,
41
42    /// memory.size for local functions
43    Memory32Size,
44
45    /// memory.size for imported functions
46    ImportedMemory32Size,
47
48    /// table.copy
49    TableCopy,
50
51    /// table.init
52    TableInit,
53
54    /// table.fill
55    TableFill,
56
57    /// table.size for local tables
58    TableSize,
59
60    /// table.size for imported tables
61    ImportedTableSize,
62
63    /// table.get for local tables
64    TableGet,
65
66    /// table.get for imported tables
67    ImportedTableGet,
68
69    /// table.set for local tables
70    TableSet,
71
72    /// table.set for imported tables
73    ImportedTableSet,
74
75    /// table.grow for local tables
76    TableGrow,
77
78    /// table.grow for imported tables
79    ImportedTableGrow,
80
81    /// ref.func
82    FuncRef,
83
84    /// elem.drop
85    ElemDrop,
86
87    /// memory.copy for local memories
88    Memory32Copy,
89
90    /// memory.copy for imported memories
91    ImportedMemory32Copy,
92
93    /// memory.fill for local memories
94    Memory32Fill,
95
96    /// memory.fill for imported memories
97    ImportedMemory32Fill,
98
99    /// memory.init
100    Memory32Init,
101
102    /// data.drop
103    DataDrop,
104
105    /// A custom trap
106    RaiseTrap,
107
108    /// probe for stack overflow. These are emitted for functions which need
109    /// when the `enable_probestack` setting is true.
110    Probestack,
111
112    /// memory.atomic.wait32 for local memories
113    Memory32AtomicWait32,
114
115    /// memory.atomic.wait32 for imported memories
116    ImportedMemory32AtomicWait32,
117
118    /// memory.atomic.wait64 for local memories
119    Memory32AtomicWait64,
120
121    /// memory.atomic.wait64 for imported memories
122    ImportedMemory32AtomicWait64,
123
124    /// memory.atomic.notify for local memories
125    Memory32AtomicNotify,
126
127    /// memory.atomic.notify for imported memories
128    ImportedMemory32AtomicNotify,
129
130    /// throw
131    Throw,
132
133    /// allocate exception object and get an exnref for it
134    AllocException,
135    /// Get the values buffer pointer out of an exnref
136    ReadExnRef,
137    /// Given a caught native exception pointer, get the exnref and delete the exception itself
138    LibunwindExceptionIntoExnRef,
139
140    /// The personality function
141    EHPersonality,
142    /// The second stage of the EH personality function
143    EHPersonality2,
144
145    /// debug_usize
146    DebugUsize,
147    /// debug_str
148    DebugStr,
149}
150
151impl LibCall {
152    /// Return the function name associated to the libcall.
153    pub fn to_function_name(&self) -> &str {
154        match self {
155            Self::CeilF32 => "wasmer_vm_f32_ceil",
156            Self::CeilF64 => "wasmer_vm_f64_ceil",
157            Self::FloorF32 => "wasmer_vm_f32_floor",
158            Self::FloorF64 => "wasmer_vm_f64_floor",
159            Self::NearestF32 => "wasmer_vm_f32_nearest",
160            Self::NearestF64 => "wasmer_vm_f64_nearest",
161            Self::TruncF32 => "wasmer_vm_f32_trunc",
162            Self::TruncF64 => "wasmer_vm_f64_trunc",
163            Self::Memory32Size => "wasmer_vm_memory32_size",
164            Self::ImportedMemory32Size => "wasmer_vm_imported_memory32_size",
165            Self::TableCopy => "wasmer_vm_table_copy",
166            Self::TableInit => "wasmer_vm_table_init",
167            Self::TableFill => "wasmer_vm_table_fill",
168            Self::TableSize => "wasmer_vm_table_size",
169            Self::ImportedTableSize => "wasmer_vm_imported_table_size",
170            Self::TableGet => "wasmer_vm_table_get",
171            Self::ImportedTableGet => "wasmer_vm_imported_table_get",
172            Self::TableSet => "wasmer_vm_table_set",
173            Self::ImportedTableSet => "wasmer_vm_imported_table_set",
174            Self::TableGrow => "wasmer_vm_table_grow",
175            Self::ImportedTableGrow => "wasmer_vm_imported_table_grow",
176            Self::FuncRef => "wasmer_vm_func_ref",
177            Self::ElemDrop => "wasmer_vm_elem_drop",
178            Self::Memory32Copy => "wasmer_vm_memory32_copy",
179            Self::ImportedMemory32Copy => "wasmer_vm_imported_memory32_copy",
180            Self::Memory32Fill => "wasmer_vm_memory32_fill",
181            Self::ImportedMemory32Fill => "wasmer_vm_imported_memory32_fill",
182            Self::Memory32Init => "wasmer_vm_memory32_init",
183            Self::DataDrop => "wasmer_vm_data_drop",
184            Self::RaiseTrap => "wasmer_vm_raise_trap",
185            // We have to do this because macOS requires a leading `_` and it's not
186            // a normal function, it's a static variable, so we have to do it manually.
187            #[cfg(target_vendor = "apple")]
188            Self::Probestack => "_wasmer_vm_probestack",
189            #[cfg(not(target_vendor = "apple"))]
190            Self::Probestack => "wasmer_vm_probestack",
191            Self::Memory32AtomicWait32 => "wasmer_vm_memory32_atomic_wait32",
192            Self::ImportedMemory32AtomicWait32 => "wasmer_vm_imported_memory32_atomic_wait32",
193            Self::Memory32AtomicWait64 => "wasmer_vm_memory32_atomic_wait64",
194            Self::ImportedMemory32AtomicWait64 => "wasmer_vm_imported_memory32_atomic_wait64",
195            Self::Memory32AtomicNotify => "wasmer_vm_memory32_atomic_notify",
196            Self::ImportedMemory32AtomicNotify => "wasmer_vm_imported_memory32_atomic_notify",
197            Self::Throw => "wasmer_vm_throw",
198            Self::EHPersonality => "wasmer_eh_personality",
199            Self::EHPersonality2 => "wasmer_eh_personality2",
200            Self::AllocException => "wasmer_vm_alloc_exception",
201            Self::ReadExnRef => "wasmer_vm_read_exnref",
202            Self::LibunwindExceptionIntoExnRef => "wasmer_vm_exception_into_exnref",
203            Self::DebugUsize => "wasmer_vm_dbg_usize",
204            Self::DebugStr => "wasmer_vm_dbg_str",
205        }
206    }
207}
208
209impl fmt::Display for LibCall {
210    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
211        fmt::Debug::fmt(self, f)
212    }
213}