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
62#[allow(missing_docs)]
64pub trait FunctionBodyLike<'a> {
65    type UnwindInfo: CompiledFunctionUnwindInfoLike<'a>;
66
67    fn body(&'a self) -> &'a [u8];
68    fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo>;
69}
70
71impl<'a> FunctionBodyLike<'a> for FunctionBody {
72    type UnwindInfo = CompiledFunctionUnwindInfo;
73
74    fn body(&'a self) -> &'a [u8] {
75        self.body.as_ref()
76    }
77
78    fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo> {
79        self.unwind_info.as_ref()
80    }
81}
82
83impl<'a> FunctionBodyLike<'a> for ArchivedFunctionBody {
84    type UnwindInfo = ArchivedCompiledFunctionUnwindInfo;
85
86    fn body(&'a self) -> &'a [u8] {
87        self.body.as_ref()
88    }
89
90    fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo> {
91        match self.unwind_info {
92            ArchivedOption::Some(ref x) => Some(x),
93            ArchivedOption::None => None,
94        }
95    }
96}
97
98#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
104#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
105#[rkyv(derive(Debug))]
106pub struct CompiledFunction {
107    pub body: FunctionBody,
109
110    pub relocations: Vec<Relocation>,
112
113    pub frame_info: CompiledFunctionFrameInfo,
115}
116
117pub type Functions = PrimaryMap<LocalFunctionIndex, CompiledFunction>;
119
120pub type CustomSections = PrimaryMap<SectionIndex, CustomSection>;
122
123#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
130#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
131#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, PartialEq, Eq, Clone, Default)]
132#[rkyv(derive(Debug), compare(PartialEq))]
133pub struct UnwindInfo {
134    pub eh_frame: Option<SectionIndex>,
138    pub compact_unwind: Option<SectionIndex>,
139}
140
141impl UnwindInfo {
142    pub fn new(eh_frame: SectionIndex) -> Self {
144        Self {
145            eh_frame: Some(eh_frame),
146            compact_unwind: None,
147        }
148    }
149
150    pub fn new_cu(compact_unwind: SectionIndex) -> Self {
151        Self {
152            eh_frame: None,
153            compact_unwind: Some(compact_unwind),
154        }
155    }
156}
157
158#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
163#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
164#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, PartialEq, Eq, Clone, Default)]
165#[rkyv(derive(Debug))]
166pub struct GOT {
167    pub index: Option<SectionIndex>,
169}
170
171impl GOT {
172    pub fn empty() -> Self {
173        Self { index: None }
174    }
175}
176#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
178#[derive(Debug, PartialEq, Eq)]
179pub struct Compilation {
180    pub functions: Functions,
182
183    pub custom_sections: CustomSections,
187
188    pub function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
198
199    pub dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
219
220    pub unwind_info: UnwindInfo,
222
223    pub got: GOT,
225}