Skip to main content

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    /// sqrt.f32
37    SqrtF32,
38
39    /// sqrt.f64
40    SqrtF64,
41
42    /// trunc.f32
43    TruncF32,
44
45    /// trunc.f64
46    TruncF64,
47
48    /// memory.size for local functions
49    Memory32Size,
50
51    /// memory.size for imported functions
52    ImportedMemory32Size,
53
54    /// table.copy
55    TableCopy,
56
57    /// table.init
58    TableInit,
59
60    /// table.fill
61    TableFill,
62
63    /// table.size for local tables
64    TableSize,
65
66    /// table.size for imported tables
67    ImportedTableSize,
68
69    /// table.get for local tables
70    TableGet,
71
72    /// table.get for imported tables
73    ImportedTableGet,
74
75    /// table.set for local tables
76    TableSet,
77
78    /// table.set for imported tables
79    ImportedTableSet,
80
81    /// table.grow for local tables
82    TableGrow,
83
84    /// table.grow for imported tables
85    ImportedTableGrow,
86
87    /// ref.func
88    FuncRef,
89
90    /// elem.drop
91    ElemDrop,
92
93    /// memory.copy
94    Memory32Copy,
95
96    /// memory.fill for local memories
97    Memory32Fill,
98
99    /// memory.fill for imported memories
100    ImportedMemory32Fill,
101
102    /// memory.init
103    Memory32Init,
104
105    /// data.drop
106    DataDrop,
107
108    /// A custom trap
109    RaiseTrap,
110
111    /// probe for stack overflow. These are emitted for functions which need
112    /// when the `enable_probestack` setting is true.
113    Probestack,
114
115    /// memory.atomic.wait32 for local memories
116    Memory32AtomicWait32,
117
118    /// memory.atomic.wait32 for imported memories
119    ImportedMemory32AtomicWait32,
120
121    /// memory.atomic.wait64 for local memories
122    Memory32AtomicWait64,
123
124    /// memory.atomic.wait64 for imported memories
125    ImportedMemory32AtomicWait64,
126
127    /// memory.atomic.notify for local memories
128    Memory32AtomicNotify,
129
130    /// memory.atomic.notify for imported memories
131    ImportedMemory32AtomicNotify,
132
133    /// throw
134    Throw,
135
136    /// allocate exception object and get an exnref for it
137    AllocException,
138    /// Get the values buffer pointer out of an exnref
139    ReadExnRef,
140    /// Given a caught native exception pointer, get the exnref and delete the exception itself
141    LibunwindExceptionIntoExnRef,
142
143    /// The personality function
144    EHPersonality,
145    /// The second stage of the EH personality function
146    EHPersonality2,
147
148    /// debug_usize
149    DebugUsize,
150    /// debug_str
151    DebugStr,
152
153    // Soft-float routines emitted by LLVM when targeting platforms without hardware floating-point.
154    // Standard toolchains (compiler-rt / libgcc) provide these, but wasmer needs to know their
155    // addresses to link JIT-compiled objects.
156    // Ordered to match the GCC runtime library documentation (§3.2).
157    // https://gcc.gnu.org/onlinedocs/gccint/Soft-float-library-routines.html
158
159    // §3.2.1 Arithmetic
160    /// __addsf3
161    Addsf3,
162    /// __adddf3
163    Adddf3,
164    /// __subsf3
165    Subsf3,
166    /// __subdf3
167    Subdf3,
168    /// __mulsf3
169    Mulsf3,
170    /// __muldf3
171    Muldf3,
172    /// __divsf3
173    Divsf3,
174    /// __divdf3
175    Divdf3,
176    /// __negsf2
177    Negsf2,
178    /// __negdf2
179    Negdf2,
180
181    // §3.2.2 Conversion
182    /// __extendsfdf2
183    Extendsfdf2,
184    /// __truncdfsf2
185    Truncdfsf2,
186    /// __fixsfsi
187    Fixsfsi,
188    /// __fixdfsi
189    Fixdfsi,
190    /// __fixsfdi
191    Fixsfdi,
192    /// __fixdfdi
193    Fixdfdi,
194    /// __fixunssfsi
195    Fixunssfsi,
196    /// __fixunsdfsi
197    Fixunsdfsi,
198    /// __fixunssfdi
199    Fixunssfdi,
200    /// __fixunsdfdi
201    Fixunsdfdi,
202    /// __floatsisf
203    Floatsisf,
204    /// __floatsidf
205    Floatsidf,
206    /// __floatdisf
207    Floatdisf,
208    /// __floatdidf
209    Floatdidf,
210    /// __floatunsisf
211    Floatunsisf,
212    /// __floatunsidf
213    Floatunsidf,
214    /// __floatundisf
215    Floatundisf,
216    /// __floatundidf
217    Floatundidf,
218
219    // §3.2.3 Comparison
220    /// __unordsf2
221    Unordsf2,
222    /// __unorddf2
223    Unorddf2,
224    /// __eqsf2
225    Eqsf2,
226    /// __eqdf2
227    Eqdf2,
228    /// __nesf2
229    Nesf2,
230    /// __nedf2
231    Nedf2,
232    /// __gesf2
233    Gesf2,
234    /// __gedf2
235    Gedf2,
236    /// __ltsf2
237    Ltsf2,
238    /// __ltdf2
239    Ltdf2,
240    /// __lesf2
241    Lesf2,
242    /// __ledf2
243    Ledf2,
244    /// __gtsf2
245    Gtsf2,
246    /// __gtdf2
247    Gtdf2,
248}
249
250impl LibCall {
251    /// Return the function name associated to the libcall.
252    pub fn to_function_name(&self) -> &str {
253        match self {
254            Self::CeilF32 => "wasmer_vm_f32_ceil",
255            Self::CeilF64 => "wasmer_vm_f64_ceil",
256            Self::FloorF32 => "wasmer_vm_f32_floor",
257            Self::FloorF64 => "wasmer_vm_f64_floor",
258            Self::NearestF32 => "wasmer_vm_f32_nearest",
259            Self::NearestF64 => "wasmer_vm_f64_nearest",
260            Self::SqrtF32 => "wasmer_vm_f32_sqrt",
261            Self::SqrtF64 => "wasmer_vm_f64_sqrt",
262            Self::TruncF32 => "wasmer_vm_f32_trunc",
263            Self::TruncF64 => "wasmer_vm_f64_trunc",
264            Self::Memory32Size => "wasmer_vm_memory32_size",
265            Self::ImportedMemory32Size => "wasmer_vm_imported_memory32_size",
266            Self::TableCopy => "wasmer_vm_table_copy",
267            Self::TableInit => "wasmer_vm_table_init",
268            Self::TableFill => "wasmer_vm_table_fill",
269            Self::TableSize => "wasmer_vm_table_size",
270            Self::ImportedTableSize => "wasmer_vm_imported_table_size",
271            Self::TableGet => "wasmer_vm_table_get",
272            Self::ImportedTableGet => "wasmer_vm_imported_table_get",
273            Self::TableSet => "wasmer_vm_table_set",
274            Self::ImportedTableSet => "wasmer_vm_imported_table_set",
275            Self::TableGrow => "wasmer_vm_table_grow",
276            Self::ImportedTableGrow => "wasmer_vm_imported_table_grow",
277            Self::FuncRef => "wasmer_vm_func_ref",
278            Self::ElemDrop => "wasmer_vm_elem_drop",
279            Self::Memory32Copy => "wasmer_vm_memory32_copy",
280            Self::Memory32Fill => "wasmer_vm_memory32_fill",
281            Self::ImportedMemory32Fill => "wasmer_vm_imported_memory32_fill",
282            Self::Memory32Init => "wasmer_vm_memory32_init",
283            Self::DataDrop => "wasmer_vm_data_drop",
284            Self::RaiseTrap => "wasmer_vm_raise_trap",
285            // We have to do this because macOS requires a leading `_` and it's not
286            // a normal function, it's a static variable, so we have to do it manually.
287            #[cfg(target_vendor = "apple")]
288            Self::Probestack => "_wasmer_vm_probestack",
289            #[cfg(not(target_vendor = "apple"))]
290            Self::Probestack => "wasmer_vm_probestack",
291            Self::Memory32AtomicWait32 => "wasmer_vm_memory32_atomic_wait32",
292            Self::ImportedMemory32AtomicWait32 => "wasmer_vm_imported_memory32_atomic_wait32",
293            Self::Memory32AtomicWait64 => "wasmer_vm_memory32_atomic_wait64",
294            Self::ImportedMemory32AtomicWait64 => "wasmer_vm_imported_memory32_atomic_wait64",
295            Self::Memory32AtomicNotify => "wasmer_vm_memory32_atomic_notify",
296            Self::ImportedMemory32AtomicNotify => "wasmer_vm_imported_memory32_atomic_notify",
297            Self::Throw => "wasmer_vm_throw",
298            Self::EHPersonality => "wasmer_eh_personality",
299            Self::EHPersonality2 => "wasmer_eh_personality2",
300            Self::AllocException => "wasmer_vm_alloc_exception",
301            Self::ReadExnRef => "wasmer_vm_read_exnref",
302            Self::LibunwindExceptionIntoExnRef => "wasmer_vm_exception_into_exnref",
303            Self::DebugUsize => "wasmer_vm_dbg_usize",
304            Self::DebugStr => "wasmer_vm_dbg_str",
305            // --- Soft-float libcalls ---
306            Self::Addsf3 => "__addsf3",
307            Self::Adddf3 => "__adddf3",
308            Self::Subsf3 => "__subsf3",
309            Self::Subdf3 => "__subdf3",
310            Self::Mulsf3 => "__mulsf3",
311            Self::Muldf3 => "__muldf3",
312            Self::Divsf3 => "__divsf3",
313            Self::Divdf3 => "__divdf3",
314            Self::Negsf2 => "__negsf2",
315            Self::Negdf2 => "__negdf2",
316            Self::Extendsfdf2 => "__extendsfdf2",
317            Self::Truncdfsf2 => "__truncdfsf2",
318            Self::Fixsfsi => "__fixsfsi",
319            Self::Fixdfsi => "__fixdfsi",
320            Self::Fixsfdi => "__fixsfdi",
321            Self::Fixdfdi => "__fixdfdi",
322            Self::Fixunssfsi => "__fixunssfsi",
323            Self::Fixunsdfsi => "__fixunsdfsi",
324            Self::Fixunssfdi => "__fixunssfdi",
325            Self::Fixunsdfdi => "__fixunsdfdi",
326            Self::Floatsisf => "__floatsisf",
327            Self::Floatsidf => "__floatsidf",
328            Self::Floatdisf => "__floatdisf",
329            Self::Floatdidf => "__floatdidf",
330            Self::Floatunsisf => "__floatunsisf",
331            Self::Floatunsidf => "__floatunsidf",
332            Self::Floatundisf => "__floatundisf",
333            Self::Floatundidf => "__floatundidf",
334            Self::Unordsf2 => "__unordsf2",
335            Self::Unorddf2 => "__unorddf2",
336            Self::Eqsf2 => "__eqsf2",
337            Self::Eqdf2 => "__eqdf2",
338            Self::Nesf2 => "__nesf2",
339            Self::Nedf2 => "__nedf2",
340            Self::Gesf2 => "__gesf2",
341            Self::Gedf2 => "__gedf2",
342            Self::Ltsf2 => "__ltsf2",
343            Self::Ltdf2 => "__ltdf2",
344            Self::Lesf2 => "__lesf2",
345            Self::Ledf2 => "__ledf2",
346            Self::Gtsf2 => "__gtsf2",
347            Self::Gtdf2 => "__gtdf2",
348        }
349    }
350}
351
352impl fmt::Display for LibCall {
353    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
354        fmt::Debug::fmt(self, f)
355    }
356}