1#![allow(missing_docs)]
6use std::path::PathBuf;
13
14use super::{
15 address_map::FunctionAddressMap,
16 relocation::Relocation,
17 section::{CustomSection, SectionIndex},
18 unwind::{
19 ArchivedCompiledFunctionUnwindInfo, CompiledFunctionUnwindInfo,
20 CompiledFunctionUnwindInfoLike,
21 },
22};
23use rkyv::{
24 Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize, option::ArchivedOption,
25};
26#[cfg(feature = "enable-serde")]
27use serde::{Deserialize, Serialize};
28use wasmer_types::{
29 FunctionIndex, LocalFunctionIndex, SignatureIndex, TrapInformation, entity::PrimaryMap,
30};
31
32#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
37#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
38#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq, Default)]
39#[rkyv(derive(Debug))]
40pub struct CompiledFunctionFrameInfo {
41 pub traps: Vec<TrapInformation>,
45
46 pub address_map: FunctionAddressMap,
48}
49
50#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
52#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
53#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
54#[rkyv(derive(Debug))]
55pub struct FunctionBody {
56 #[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))]
58 pub body: Vec<u8>,
59
60 pub unwind_info: Option<CompiledFunctionUnwindInfo>,
62}
63
64pub enum CompiledFunctionBody {
65 Rkyv(FunctionBody),
66 Elf(PathBuf),
67}
68
69#[allow(missing_docs)]
71pub trait FunctionBodyLike<'a> {
72 type UnwindInfo: CompiledFunctionUnwindInfoLike<'a>;
73
74 fn body(&'a self) -> &'a [u8];
75 fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo>;
76}
77
78impl<'a> FunctionBodyLike<'a> for FunctionBody {
79 type UnwindInfo = CompiledFunctionUnwindInfo;
80
81 fn body(&'a self) -> &'a [u8] {
82 self.body.as_ref()
83 }
84
85 fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo> {
86 self.unwind_info.as_ref()
87 }
88}
89
90impl<'a> FunctionBodyLike<'a> for ArchivedFunctionBody {
91 type UnwindInfo = ArchivedCompiledFunctionUnwindInfo;
92
93 fn body(&'a self) -> &'a [u8] {
94 self.body.as_ref()
95 }
96
97 fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo> {
98 match self.unwind_info {
99 ArchivedOption::Some(ref x) => Some(x),
100 ArchivedOption::None => None,
101 }
102 }
103}
104
105#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
111#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
112#[rkyv(derive(Debug))]
113pub struct CompiledFunction {
114 pub body: FunctionBody,
116
117 pub relocations: Vec<Relocation>,
119
120 pub frame_info: CompiledFunctionFrameInfo,
122
123 pub maximum_stack_usage: Option<usize>,
126}
127
128pub type Functions = PrimaryMap<LocalFunctionIndex, CompiledFunction>;
130
131pub type CustomSections = PrimaryMap<SectionIndex, CustomSection>;
133
134#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
141#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
142#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, PartialEq, Eq, Clone, Default)]
143#[rkyv(derive(Debug), compare(PartialEq))]
144pub struct UnwindInfo {
145 pub eh_frame: Option<SectionIndex>,
149 pub compact_unwind: Option<SectionIndex>,
150}
151
152impl UnwindInfo {
153 pub fn new(eh_frame: SectionIndex) -> Self {
155 Self {
156 eh_frame: Some(eh_frame),
157 compact_unwind: None,
158 }
159 }
160
161 pub fn new_cu(compact_unwind: SectionIndex) -> Self {
162 Self {
163 eh_frame: None,
164 compact_unwind: Some(compact_unwind),
165 }
166 }
167}
168
169#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
174#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
175#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, PartialEq, Eq, Clone, Default)]
176#[rkyv(derive(Debug))]
177pub struct GOT {
178 pub index: Option<SectionIndex>,
180}
181
182impl GOT {
183 pub fn empty() -> Self {
184 Self { index: None }
185 }
186}
187#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
189#[derive(Debug, PartialEq, Eq)]
190pub struct RkyvCompilation {
191 pub functions: Functions,
193
194 pub custom_sections: CustomSections,
198
199 pub function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
209
210 pub dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
230
231 pub unwind_info: UnwindInfo,
233
234 pub got: GOT,
236}
237
238#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
241#[derive(Debug, PartialEq, Eq)]
242pub enum Compilation {
243 Rkyv {
244 compilation: RkyvCompilation,
245 function_max_stack_usage: PrimaryMap<LocalFunctionIndex, Option<usize>>,
246 },
247 Elf {
248 data: Vec<u8>,
249 function_max_stack_usage: PrimaryMap<LocalFunctionIndex, Option<usize>>,
250 },
251}