1use crate::lib::std::vec::Vec;
8use crate::section::{CustomSection, SectionIndex};
9use crate::trap::TrapInformation;
10use crate::{
11 CompiledFunctionUnwindInfo, CompiledFunctionUnwindInfoRef, FunctionAddressMap,
12 JumpTableOffsets, Relocation,
13};
14use wasmer_types::entity::PrimaryMap;
15use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
16
17#[derive(
22 rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq, Default,
23)]
24pub struct CompiledFunctionFrameInfo {
25 pub traps: Vec<TrapInformation>,
29
30 pub address_map: FunctionAddressMap,
32}
33
34#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq)]
36pub struct FunctionBody {
37 pub body: Vec<u8>,
39
40 pub unwind_info: Option<CompiledFunctionUnwindInfo>,
42}
43
44#[derive(Clone, Copy)]
46pub struct FunctionBodyRef<'a> {
47 pub body: &'a [u8],
49 pub unwind_info: Option<CompiledFunctionUnwindInfoRef<'a>>,
51}
52
53impl<'a> From<&'a FunctionBody> for FunctionBodyRef<'a> {
54 fn from(body: &'a FunctionBody) -> Self {
55 FunctionBodyRef {
56 body: &*body.body,
57 unwind_info: body.unwind_info.as_ref().map(Into::into),
58 }
59 }
60}
61
62impl<'a> From<&'a ArchivedFunctionBody> for FunctionBodyRef<'a> {
63 fn from(body: &'a ArchivedFunctionBody) -> Self {
64 FunctionBodyRef {
65 body: &*body.body,
66 unwind_info: body.unwind_info.as_ref().map(Into::into),
67 }
68 }
69}
70
71#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq)]
77pub struct CompiledFunction {
78 pub body: FunctionBody,
80
81 pub relocations: Vec<Relocation>,
83
84 pub jt_offsets: JumpTableOffsets,
86
87 pub frame_info: CompiledFunctionFrameInfo,
89}
90
91pub type Functions = PrimaryMap<LocalFunctionIndex, CompiledFunction>;
93
94pub type CustomSections = PrimaryMap<SectionIndex, CustomSection>;
96
97#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, PartialEq, Eq, Clone)]
104pub struct Dwarf {
105 pub eh_frame: SectionIndex,
109}
110
111impl Dwarf {
112 pub fn new(eh_frame: SectionIndex) -> Self {
114 Self { eh_frame }
115 }
116}
117
118#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, PartialEq, Eq, Clone)]
120pub struct TrampolinesSection {
121 pub section_index: SectionIndex,
123 pub slots: usize,
125 pub size: usize,
127}
128
129impl TrampolinesSection {
130 pub fn new(section_index: SectionIndex, slots: usize, size: usize) -> Self {
132 Self {
133 section_index,
134 slots,
135 size,
136 }
137 }
138}
139
140#[derive(Debug, PartialEq, Eq)]
142pub struct Compilation {
143 functions: Functions,
145
146 custom_sections: CustomSections,
150
151 function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
161
162 dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
182
183 debug: Option<Dwarf>,
185
186 trampolines: Option<TrampolinesSection>,
188}
189
190impl Compilation {
191 pub fn new(
193 functions: Functions,
194 custom_sections: CustomSections,
195 function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
196 dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
197 debug: Option<Dwarf>,
198 trampolines: Option<TrampolinesSection>,
199 ) -> Self {
200 Self {
201 functions,
202 custom_sections,
203 function_call_trampolines,
204 dynamic_function_trampolines,
205 debug,
206 trampolines,
207 }
208 }
209
210 pub fn get(&self, func: LocalFunctionIndex) -> &CompiledFunction {
212 &self.functions[func]
213 }
214
215 pub fn len(&self) -> usize {
217 self.functions.len()
218 }
219
220 pub fn is_empty(&self) -> bool {
222 self.functions.is_empty()
223 }
224
225 pub fn get_relocations(&self) -> PrimaryMap<LocalFunctionIndex, Vec<Relocation>> {
227 self.functions
228 .iter()
229 .map(|(_, func)| func.relocations.clone())
230 .collect::<PrimaryMap<LocalFunctionIndex, _>>()
231 }
232
233 pub fn get_function_bodies(&self) -> PrimaryMap<LocalFunctionIndex, FunctionBody> {
235 self.functions
236 .iter()
237 .map(|(_, func)| func.body.clone())
238 .collect::<PrimaryMap<LocalFunctionIndex, _>>()
239 }
240
241 pub fn get_jt_offsets(&self) -> PrimaryMap<LocalFunctionIndex, JumpTableOffsets> {
243 self.functions
244 .iter()
245 .map(|(_, func)| func.jt_offsets.clone())
246 .collect::<PrimaryMap<LocalFunctionIndex, _>>()
247 }
248
249 pub fn get_frame_info(&self) -> PrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo> {
251 self.functions
252 .iter()
253 .map(|(_, func)| func.frame_info.clone())
254 .collect::<PrimaryMap<LocalFunctionIndex, _>>()
255 }
256
257 pub fn get_function_call_trampolines(&self) -> PrimaryMap<SignatureIndex, FunctionBody> {
259 self.function_call_trampolines.clone()
260 }
261
262 pub fn get_dynamic_function_trampolines(&self) -> PrimaryMap<FunctionIndex, FunctionBody> {
264 self.dynamic_function_trampolines.clone()
265 }
266
267 pub fn get_custom_sections(&self) -> PrimaryMap<SectionIndex, CustomSection> {
269 self.custom_sections.clone()
270 }
271
272 pub fn get_custom_section_relocations(&self) -> PrimaryMap<SectionIndex, Vec<Relocation>> {
274 self.custom_sections
275 .iter()
276 .map(|(_, section)| section.relocations.clone())
277 .collect::<PrimaryMap<SectionIndex, _>>()
278 }
279
280 pub fn get_debug(&self) -> Option<Dwarf> {
282 self.debug.clone()
283 }
284
285 pub fn get_trampolines(&self) -> Option<TrampolinesSection> {
287 self.trampolines.clone()
288 }
289}
290
291impl<'a> IntoIterator for &'a Compilation {
292 type IntoIter = Iter<'a>;
293 type Item = <Self::IntoIter as Iterator>::Item;
294
295 fn into_iter(self) -> Self::IntoIter {
296 Iter {
297 iterator: self.functions.iter(),
298 }
299 }
300}
301
302pub struct Iter<'a> {
303 iterator: <&'a Functions as IntoIterator>::IntoIter,
304}
305
306impl<'a> Iterator for Iter<'a> {
307 type Item = &'a CompiledFunction;
308
309 fn next(&mut self) -> Option<Self::Item> {
310 self.iterator.next().map(|(_, b)| b)
311 }
312}