#![allow(missing_docs)]
use super::{
address_map::FunctionAddressMap,
relocation::Relocation,
section::{CustomSection, SectionIndex},
unwind::{
ArchivedCompiledFunctionUnwindInfo, CompiledFunctionUnwindInfo,
CompiledFunctionUnwindInfoLike,
},
};
use rkyv::{
Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize, option::ArchivedOption,
};
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use wasmer_types::{
FunctionIndex, LocalFunctionIndex, SignatureIndex, TrapInformation, entity::PrimaryMap,
};
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq, Default)]
#[rkyv(derive(Debug))]
pub struct CompiledFunctionFrameInfo {
pub traps: Vec<TrapInformation>,
pub address_map: FunctionAddressMap,
}
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
#[rkyv(derive(Debug))]
pub struct FunctionBody {
#[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))]
pub body: Vec<u8>,
pub unwind_info: Option<CompiledFunctionUnwindInfo>,
}
#[allow(missing_docs)]
pub trait FunctionBodyLike<'a> {
type UnwindInfo: CompiledFunctionUnwindInfoLike<'a>;
fn body(&'a self) -> &'a [u8];
fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo>;
}
impl<'a> FunctionBodyLike<'a> for FunctionBody {
type UnwindInfo = CompiledFunctionUnwindInfo;
fn body(&'a self) -> &'a [u8] {
self.body.as_ref()
}
fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo> {
self.unwind_info.as_ref()
}
}
impl<'a> FunctionBodyLike<'a> for ArchivedFunctionBody {
type UnwindInfo = ArchivedCompiledFunctionUnwindInfo;
fn body(&'a self) -> &'a [u8] {
self.body.as_ref()
}
fn unwind_info(&'a self) -> Option<&'a Self::UnwindInfo> {
match self.unwind_info {
ArchivedOption::Some(ref x) => Some(x),
ArchivedOption::None => None,
}
}
}
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
#[rkyv(derive(Debug))]
pub struct CompiledFunction {
pub body: FunctionBody,
pub relocations: Vec<Relocation>,
pub frame_info: CompiledFunctionFrameInfo,
}
pub type Functions = PrimaryMap<LocalFunctionIndex, CompiledFunction>;
pub type CustomSections = PrimaryMap<SectionIndex, CustomSection>;
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, PartialEq, Eq, Clone, Default)]
#[rkyv(derive(Debug), compare(PartialEq))]
pub struct UnwindInfo {
pub eh_frame: Option<SectionIndex>,
pub compact_unwind: Option<SectionIndex>,
}
impl UnwindInfo {
pub fn new(eh_frame: SectionIndex) -> Self {
Self {
eh_frame: Some(eh_frame),
compact_unwind: None,
}
}
pub fn new_cu(compact_unwind: SectionIndex) -> Self {
Self {
eh_frame: None,
compact_unwind: Some(compact_unwind),
}
}
}
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, PartialEq, Eq, Clone, Default)]
#[rkyv(derive(Debug))]
pub struct GOT {
pub index: Option<SectionIndex>,
}
impl GOT {
pub fn empty() -> Self {
Self { index: None }
}
}
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq)]
pub struct Compilation {
pub functions: Functions,
pub custom_sections: CustomSections,
pub function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
pub dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
pub unwind_info: UnwindInfo,
pub got: GOT,
}