1#![allow(missing_docs)]
6use super::{
13 address_map::FunctionAddressMap,
14 relocation::Relocation,
15 section::{CustomSection, SectionIndex},
16 unwind::{
17 ArchivedCompiledFunctionUnwindInfo, CompiledFunctionUnwindInfo,
18 CompiledFunctionUnwindInfoLike,
19 },
20};
21use rkyv::{
22 Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize, option::ArchivedOption,
23};
24#[cfg(feature = "enable-serde")]
25use serde::{Deserialize, Serialize};
26use wasmer_types::{
27 FunctionIndex, LocalFunctionIndex, SignatureIndex, TrapInformation, entity::PrimaryMap,
28};
29
30#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
35#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
36#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq, Default)]
37#[rkyv(derive(Debug))]
38pub struct CompiledFunctionFrameInfo {
39 pub traps: Vec<TrapInformation>,
43
44 pub address_map: FunctionAddressMap,
46}
47
48#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
50#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
51#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
52#[rkyv(derive(Debug))]
53pub struct FunctionBody {
54 #[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))]
56 pub body: Vec<u8>,
57
58 pub unwind_info: Option<CompiledFunctionUnwindInfo>,
60}
61
62pub enum CompiledFunctionBody {
63 Rkyv(FunctionBody),
64 Elf(Vec<u8>),
65}
66
67#[allow(missing_docs)]
69pub trait FunctionBodyLike<'a> {
70 type UnwindInfo: CompiledFunctionUnwindInfoLike<'a>;
71
72 fn body(&'a self) -> &'a [u8];
73 fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo>;
74}
75
76impl<'a> FunctionBodyLike<'a> for FunctionBody {
77 type UnwindInfo = CompiledFunctionUnwindInfo;
78
79 fn body(&'a self) -> &'a [u8] {
80 self.body.as_ref()
81 }
82
83 fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo> {
84 self.unwind_info.as_ref()
85 }
86}
87
88impl<'a> FunctionBodyLike<'a> for ArchivedFunctionBody {
89 type UnwindInfo = ArchivedCompiledFunctionUnwindInfo;
90
91 fn body(&'a self) -> &'a [u8] {
92 self.body.as_ref()
93 }
94
95 fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo> {
96 match self.unwind_info {
97 ArchivedOption::Some(ref x) => Some(x),
98 ArchivedOption::None => None,
99 }
100 }
101}
102
103#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
109#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
110#[rkyv(derive(Debug))]
111pub struct CompiledFunction {
112 pub body: FunctionBody,
114
115 pub relocations: Vec<Relocation>,
117
118 pub frame_info: CompiledFunctionFrameInfo,
120
121 pub maximum_stack_usage: Option<usize>,
124}
125
126pub type Functions = PrimaryMap<LocalFunctionIndex, CompiledFunction>;
128
129pub type CustomSections = PrimaryMap<SectionIndex, CustomSection>;
131
132#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
139#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
140#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, PartialEq, Eq, Clone, Default)]
141#[rkyv(derive(Debug), compare(PartialEq))]
142pub struct UnwindInfo {
143 pub eh_frame: Option<SectionIndex>,
147 pub compact_unwind: Option<SectionIndex>,
148}
149
150impl UnwindInfo {
151 pub fn new(eh_frame: SectionIndex) -> Self {
153 Self {
154 eh_frame: Some(eh_frame),
155 compact_unwind: None,
156 }
157 }
158
159 pub fn new_cu(compact_unwind: SectionIndex) -> Self {
160 Self {
161 eh_frame: None,
162 compact_unwind: Some(compact_unwind),
163 }
164 }
165}
166
167#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
172#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
173#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, PartialEq, Eq, Clone, Default)]
174#[rkyv(derive(Debug))]
175pub struct GOT {
176 pub index: Option<SectionIndex>,
178}
179
180impl GOT {
181 pub fn empty() -> Self {
182 Self { index: None }
183 }
184}
185#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
187#[derive(Debug, PartialEq, Eq)]
188pub struct RkyvCompilation {
189 pub functions: Functions,
191
192 pub custom_sections: CustomSections,
196
197 pub function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
207
208 pub dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
228
229 pub unwind_info: UnwindInfo,
231
232 pub got: GOT,
234}
235
236#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
239#[derive(Debug, PartialEq, Eq)]
240pub enum Compilation {
241 Rkyv {
242 compilation: RkyvCompilation,
243 function_max_stack_usage: PrimaryMap<LocalFunctionIndex, Option<usize>>,
244 },
245 Elf {
246 data: Vec<u8>,
247 function_max_stack_usage: PrimaryMap<LocalFunctionIndex, Option<usize>>,
248 },
249}