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    // Soft-float routines emitted by LLVM when targeting platforms without hardware floating-point.
151    // Standard toolchains (compiler-rt / libgcc) provide these, but wasmer needs to know their
152    // addresses to link JIT-compiled objects.
153    // Ordered to match the GCC runtime library documentation (§3.2).
154    // https://gcc.gnu.org/onlinedocs/gccint/Soft-float-library-routines.html
155
156    // §3.2.1 Arithmetic
157    /// __addsf3
158    Addsf3,
159    /// __adddf3
160    Adddf3,
161    /// __subsf3
162    Subsf3,
163    /// __subdf3
164    Subdf3,
165    /// __mulsf3
166    Mulsf3,
167    /// __muldf3
168    Muldf3,
169    /// __divsf3
170    Divsf3,
171    /// __divdf3
172    Divdf3,
173    /// __negsf2
174    Negsf2,
175    /// __negdf2
176    Negdf2,
177
178    // §3.2.2 Conversion
179    /// __extendsfdf2
180    Extendsfdf2,
181    /// __truncdfsf2
182    Truncdfsf2,
183    /// __fixsfsi
184    Fixsfsi,
185    /// __fixdfsi
186    Fixdfsi,
187    /// __fixsfdi
188    Fixsfdi,
189    /// __fixdfdi
190    Fixdfdi,
191    /// __fixunssfsi
192    Fixunssfsi,
193    /// __fixunsdfsi
194    Fixunsdfsi,
195    /// __fixunssfdi
196    Fixunssfdi,
197    /// __fixunsdfdi
198    Fixunsdfdi,
199    /// __floatsisf
200    Floatsisf,
201    /// __floatsidf
202    Floatsidf,
203    /// __floatdisf
204    Floatdisf,
205    /// __floatdidf
206    Floatdidf,
207    /// __floatunsisf
208    Floatunsisf,
209    /// __floatunsidf
210    Floatunsidf,
211    /// __floatundisf
212    Floatundisf,
213    /// __floatundidf
214    Floatundidf,
215
216    // §3.2.3 Comparison
217    /// __unordsf2
218    Unordsf2,
219    /// __unorddf2
220    Unorddf2,
221    /// __eqsf2
222    Eqsf2,
223    /// __eqdf2
224    Eqdf2,
225    /// __nesf2
226    Nesf2,
227    /// __nedf2
228    Nedf2,
229    /// __gesf2
230    Gesf2,
231    /// __gedf2
232    Gedf2,
233    /// __ltsf2
234    Ltsf2,
235    /// __ltdf2
236    Ltdf2,
237    /// __lesf2
238    Lesf2,
239    /// __ledf2
240    Ledf2,
241    /// __gtsf2
242    Gtsf2,
243    /// __gtdf2
244    Gtdf2,
245}
246
247impl LibCall {
248    /// Return the function name associated to the libcall.
249    pub fn to_function_name(&self) -> &str {
250        match self {
251            Self::CeilF32 => "wasmer_vm_f32_ceil",
252            Self::CeilF64 => "wasmer_vm_f64_ceil",
253            Self::FloorF32 => "wasmer_vm_f32_floor",
254            Self::FloorF64 => "wasmer_vm_f64_floor",
255            Self::NearestF32 => "wasmer_vm_f32_nearest",
256            Self::NearestF64 => "wasmer_vm_f64_nearest",
257            Self::TruncF32 => "wasmer_vm_f32_trunc",
258            Self::TruncF64 => "wasmer_vm_f64_trunc",
259            Self::Memory32Size => "wasmer_vm_memory32_size",
260            Self::ImportedMemory32Size => "wasmer_vm_imported_memory32_size",
261            Self::TableCopy => "wasmer_vm_table_copy",
262            Self::TableInit => "wasmer_vm_table_init",
263            Self::TableFill => "wasmer_vm_table_fill",
264            Self::TableSize => "wasmer_vm_table_size",
265            Self::ImportedTableSize => "wasmer_vm_imported_table_size",
266            Self::TableGet => "wasmer_vm_table_get",
267            Self::ImportedTableGet => "wasmer_vm_imported_table_get",
268            Self::TableSet => "wasmer_vm_table_set",
269            Self::ImportedTableSet => "wasmer_vm_imported_table_set",
270            Self::TableGrow => "wasmer_vm_table_grow",
271            Self::ImportedTableGrow => "wasmer_vm_imported_table_grow",
272            Self::FuncRef => "wasmer_vm_func_ref",
273            Self::ElemDrop => "wasmer_vm_elem_drop",
274            Self::Memory32Copy => "wasmer_vm_memory32_copy",
275            Self::ImportedMemory32Copy => "wasmer_vm_imported_memory32_copy",
276            Self::Memory32Fill => "wasmer_vm_memory32_fill",
277            Self::ImportedMemory32Fill => "wasmer_vm_imported_memory32_fill",
278            Self::Memory32Init => "wasmer_vm_memory32_init",
279            Self::DataDrop => "wasmer_vm_data_drop",
280            Self::RaiseTrap => "wasmer_vm_raise_trap",
281            // We have to do this because macOS requires a leading `_` and it's not
282            // a normal function, it's a static variable, so we have to do it manually.
283            #[cfg(target_vendor = "apple")]
284            Self::Probestack => "_wasmer_vm_probestack",
285            #[cfg(not(target_vendor = "apple"))]
286            Self::Probestack => "wasmer_vm_probestack",
287            Self::Memory32AtomicWait32 => "wasmer_vm_memory32_atomic_wait32",
288            Self::ImportedMemory32AtomicWait32 => "wasmer_vm_imported_memory32_atomic_wait32",
289            Self::Memory32AtomicWait64 => "wasmer_vm_memory32_atomic_wait64",
290            Self::ImportedMemory32AtomicWait64 => "wasmer_vm_imported_memory32_atomic_wait64",
291            Self::Memory32AtomicNotify => "wasmer_vm_memory32_atomic_notify",
292            Self::ImportedMemory32AtomicNotify => "wasmer_vm_imported_memory32_atomic_notify",
293            Self::Throw => "wasmer_vm_throw",
294            Self::EHPersonality => "wasmer_eh_personality",
295            Self::EHPersonality2 => "wasmer_eh_personality2",
296            Self::AllocException => "wasmer_vm_alloc_exception",
297            Self::ReadExnRef => "wasmer_vm_read_exnref",
298            Self::LibunwindExceptionIntoExnRef => "wasmer_vm_exception_into_exnref",
299            Self::DebugUsize => "wasmer_vm_dbg_usize",
300            Self::DebugStr => "wasmer_vm_dbg_str",
301            // --- Soft-float libcalls ---
302            Self::Addsf3 => "__addsf3",
303            Self::Adddf3 => "__adddf3",
304            Self::Subsf3 => "__subsf3",
305            Self::Subdf3 => "__subdf3",
306            Self::Mulsf3 => "__mulsf3",
307            Self::Muldf3 => "__muldf3",
308            Self::Divsf3 => "__divsf3",
309            Self::Divdf3 => "__divdf3",
310            Self::Negsf2 => "__negsf2",
311            Self::Negdf2 => "__negdf2",
312            Self::Extendsfdf2 => "__extendsfdf2",
313            Self::Truncdfsf2 => "__truncdfsf2",
314            Self::Fixsfsi => "__fixsfsi",
315            Self::Fixdfsi => "__fixdfsi",
316            Self::Fixsfdi => "__fixsfdi",
317            Self::Fixdfdi => "__fixdfdi",
318            Self::Fixunssfsi => "__fixunssfsi",
319            Self::Fixunsdfsi => "__fixunsdfsi",
320            Self::Fixunssfdi => "__fixunssfdi",
321            Self::Fixunsdfdi => "__fixunsdfdi",
322            Self::Floatsisf => "__floatsisf",
323            Self::Floatsidf => "__floatsidf",
324            Self::Floatdisf => "__floatdisf",
325            Self::Floatdidf => "__floatdidf",
326            Self::Floatunsisf => "__floatunsisf",
327            Self::Floatunsidf => "__floatunsidf",
328            Self::Floatundisf => "__floatundisf",
329            Self::Floatundidf => "__floatundidf",
330            Self::Unordsf2 => "__unordsf2",
331            Self::Unorddf2 => "__unorddf2",
332            Self::Eqsf2 => "__eqsf2",
333            Self::Eqdf2 => "__eqdf2",
334            Self::Nesf2 => "__nesf2",
335            Self::Nedf2 => "__nedf2",
336            Self::Gesf2 => "__gesf2",
337            Self::Gedf2 => "__gedf2",
338            Self::Ltsf2 => "__ltsf2",
339            Self::Ltdf2 => "__ltdf2",
340            Self::Lesf2 => "__lesf2",
341            Self::Ledf2 => "__ledf2",
342            Self::Gtsf2 => "__gtsf2",
343            Self::Gtdf2 => "__gtdf2",
344        }
345    }
346}
347
348impl fmt::Display for LibCall {
349    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
350        fmt::Debug::fmt(self, f)
351    }
352}