unc_vm_compiler/
function.rs

1// This file contains code from external sources.
2// Attributions: https://github.com/wasmerio/wasmer/blob/master/ATTRIBUTIONS.md
3
4//! A `Compilation` contains the compiled function bodies for a WebAssembly
5//! module (`CompiledFunction`).
6
7use crate::lib::std::vec::Vec;
8use crate::section::{CustomSection, SectionIndex};
9use crate::trap::TrapInformation;
10use crate::{FunctionAddressMap, JumpTableOffsets, Relocation};
11use unc_vm_types::entity::PrimaryMap;
12use unc_vm_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
13
14/// The frame info for a Compiled function.
15///
16/// This structure is only used for reconstructing
17/// the frame information after a `Trap`.
18#[derive(
19    rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq, Default,
20)]
21pub struct CompiledFunctionFrameInfo {
22    /// The traps (in the function body).
23    ///
24    /// Code offsets of the traps MUST be in ascending order.
25    pub traps: Vec<TrapInformation>,
26
27    /// The address map.
28    pub address_map: FunctionAddressMap,
29}
30
31/// The function body.
32#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq)]
33pub struct FunctionBody {
34    /// The function body bytes.
35    pub body: Vec<u8>,
36}
37
38/// See [`FunctionBody`].
39#[derive(Clone, Copy)]
40pub struct FunctionBodyRef<'a> {
41    /// Function body bytes.
42    pub body: &'a [u8],
43}
44
45impl<'a> From<&'a FunctionBody> for FunctionBodyRef<'a> {
46    fn from(body: &'a FunctionBody) -> Self {
47        FunctionBodyRef { body: &*body.body }
48    }
49}
50
51impl<'a> From<&'a ArchivedFunctionBody> for FunctionBodyRef<'a> {
52    fn from(body: &'a ArchivedFunctionBody) -> Self {
53        FunctionBodyRef { body: &*body.body }
54    }
55}
56
57/// The result of compiling a WebAssembly function.
58///
59/// This structure only have the compiled information data
60/// (function bytecode body, relocations, traps, jump tables
61/// and unwind information).
62#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq)]
63pub struct CompiledFunction {
64    /// The function body.
65    pub body: FunctionBody,
66
67    /// The relocations (in the body)
68    pub relocations: Vec<Relocation>,
69
70    /// The jump tables offsets (in the body).
71    pub jt_offsets: JumpTableOffsets,
72
73    /// The frame information.
74    pub frame_info: CompiledFunctionFrameInfo,
75}
76
77/// The compiled functions map (index in the Wasm -> function)
78pub type Functions = PrimaryMap<LocalFunctionIndex, CompiledFunction>;
79
80/// The custom sections for a Compilation.
81pub type CustomSections = PrimaryMap<SectionIndex, CustomSection>;
82
83/// The DWARF information for this Compilation.
84///
85/// It is used for retrieving the unwind information once an exception
86/// happens.
87/// In the future this structure may also hold other information useful
88/// for debugging.
89#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, PartialEq, Eq, Clone)]
90pub struct Dwarf {
91    /// The section index in the [`Compilation`] that corresponds to the exception frames.
92    /// [Learn
93    /// more](https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html).
94    pub eh_frame: SectionIndex,
95}
96
97impl Dwarf {
98    /// Creates a `Dwarf` struct with the corresponding indices for its sections
99    pub fn new(eh_frame: SectionIndex) -> Self {
100        Self { eh_frame }
101    }
102}
103
104/// Trampolines section used by ARM short jump (26bits)
105#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, PartialEq, Eq, Clone)]
106pub struct TrampolinesSection {
107    /// SectionIndex for the actual Trampolines code
108    pub section_index: SectionIndex,
109    /// Number of jump slots in the section
110    pub slots: usize,
111    /// Slot size
112    pub size: usize,
113}
114
115impl TrampolinesSection {
116    /// Creates a `Trampolines` struct with the indice for its section, and number of slots and size of slot
117    pub fn new(section_index: SectionIndex, slots: usize, size: usize) -> Self {
118        Self { section_index, slots, size }
119    }
120}
121
122/// The result of compiling a WebAssembly module's functions.
123#[derive(Debug, PartialEq, Eq)]
124pub struct Compilation {
125    /// Compiled code for the function bodies.
126    pub functions: Functions,
127
128    /// Custom sections for the module.
129    /// It will hold the data, for example, for constants used in a
130    /// function, global variables, rodata_64, hot/cold function partitioning, ...
131    pub custom_sections: CustomSections,
132
133    /// Trampolines to call a function defined locally in the wasm via a
134    /// provided `Vec` of values.
135    ///
136    /// This allows us to call easily Wasm functions, such as:
137    ///
138    /// ```ignore
139    /// let func = instance.exports.get_function("my_func");
140    /// func.call(&[Value::I32(1)]);
141    /// ```
142    pub function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
143
144    /// Trampolines to call a dynamic function defined in
145    /// a host, from a Wasm module.
146    ///
147    /// This allows us to create dynamic Wasm functions, such as:
148    ///
149    /// ```ignore
150    /// fn my_func(values: &[Val]) -> Result<Vec<Val>, RuntimeError> {
151    ///     // do something
152    /// }
153    ///
154    /// let my_func_type = FunctionType::new(vec![Type::I32], vec![Type::I32]);
155    /// let imports = imports!{
156    ///     "namespace" => {
157    ///         "my_func" => Function::new(&store, my_func_type, my_func),
158    ///     }
159    /// }
160    /// ```
161    ///
162    /// Note: Dynamic function trampolines are only compiled for imported function types.
163    pub dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
164
165    /// Section ids corresponding to the Dwarf debug info
166    pub debug: Option<Dwarf>,
167
168    /// Trampolines for the arch that needs it
169    pub trampolines: Option<TrampolinesSection>,
170}