wasmer_types/
libcalls.rs

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