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    /// The second stage of the EH personality function
157    EHPersonality2,
158
159    /// debug_usize
160    DebugUsize,
161    /// debug_str
162    DebugStr,
163}
164
165impl LibCall {
166    /// Return the function name associated to the libcall.
167    pub fn to_function_name(&self) -> &str {
168        match self {
169            Self::CeilF32 => "wasmer_vm_f32_ceil",
170            Self::CeilF64 => "wasmer_vm_f64_ceil",
171            Self::FloorF32 => "wasmer_vm_f32_floor",
172            Self::FloorF64 => "wasmer_vm_f64_floor",
173            Self::NearestF32 => "wasmer_vm_f32_nearest",
174            Self::NearestF64 => "wasmer_vm_f64_nearest",
175            Self::TruncF32 => "wasmer_vm_f32_trunc",
176            Self::TruncF64 => "wasmer_vm_f64_trunc",
177            Self::Memory32Size => "wasmer_vm_memory32_size",
178            Self::ImportedMemory32Size => "wasmer_vm_imported_memory32_size",
179            Self::TableCopy => "wasmer_vm_table_copy",
180            Self::TableInit => "wasmer_vm_table_init",
181            Self::TableFill => "wasmer_vm_table_fill",
182            Self::TableSize => "wasmer_vm_table_size",
183            Self::ImportedTableSize => "wasmer_vm_imported_table_size",
184            Self::TableGet => "wasmer_vm_table_get",
185            Self::ImportedTableGet => "wasmer_vm_imported_table_get",
186            Self::TableSet => "wasmer_vm_table_set",
187            Self::ImportedTableSet => "wasmer_vm_imported_table_set",
188            Self::TableGrow => "wasmer_vm_table_grow",
189            Self::ImportedTableGrow => "wasmer_vm_imported_table_grow",
190            Self::FuncRef => "wasmer_vm_func_ref",
191            Self::ElemDrop => "wasmer_vm_elem_drop",
192            Self::Memory32Copy => "wasmer_vm_memory32_copy",
193            Self::ImportedMemory32Copy => "wasmer_vm_imported_memory32_copy",
194            Self::Memory32Fill => "wasmer_vm_memory32_fill",
195            Self::ImportedMemory32Fill => "wasmer_vm_imported_memory32_fill",
196            Self::Memory32Init => "wasmer_vm_memory32_init",
197            Self::DataDrop => "wasmer_vm_data_drop",
198            Self::RaiseTrap => "wasmer_vm_raise_trap",
199            // We have to do this because macOS requires a leading `_` and it's not
200            // a normal function, it's a static variable, so we have to do it manually.
201            #[cfg(target_vendor = "apple")]
202            Self::Probestack => "_wasmer_vm_probestack",
203            #[cfg(not(target_vendor = "apple"))]
204            Self::Probestack => "wasmer_vm_probestack",
205            Self::Memory32AtomicWait32 => "wasmer_vm_memory32_atomic_wait32",
206            Self::ImportedMemory32AtomicWait32 => "wasmer_vm_imported_memory32_atomic_wait32",
207            Self::Memory32AtomicWait64 => "wasmer_vm_memory32_atomic_wait64",
208            Self::ImportedMemory32AtomicWait64 => "wasmer_vm_imported_memory32_atomic_wait64",
209            Self::Memory32AtomicNotify => "wasmer_vm_memory32_atomic_notify",
210            Self::ImportedMemory32AtomicNotify => "wasmer_vm_imported_memory32_atomic_notify",
211            Self::Throw => "wasmer_vm_throw",
212            Self::Rethrow => "wasmer_vm_rethrow",
213            Self::EHPersonality => "wasmer_eh_personality",
214            Self::EHPersonality2 => "wasmer_eh_personality2",
215            Self::AllocException => "wasmer_vm_alloc_exception",
216            Self::DeleteException => "wasmer_vm_delete_exception",
217            Self::ReadException => "wasmer_vm_read_exception",
218            Self::DebugUsize => "wasmer_vm_dbg_usize",
219            Self::DebugStr => "wasmer_vm_dbg_str",
220        }
221    }
222}
223
224impl fmt::Display for LibCall {
225    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
226        fmt::Debug::fmt(self, f)
227    }
228}