wasmtime_cranelift/translate/environ/
spec.rs

1//! All the runtime support necessary for the wasm to cranelift translation is formalized by the
2//! traits `FunctionEnvironment` and `ModuleEnvironment`.
3//!
4//! There are skeleton implementations of these traits in the `dummy` module, and complete
5//! implementations in [Wasmtime].
6//!
7//! [Wasmtime]: https://github.com/bytecodealliance/wasmtime
8
9use cranelift_codegen::ir;
10use cranelift_codegen::ir::immediates::Offset32;
11use cranelift_codegen::isa::TargetFrontendConfig;
12use smallvec::SmallVec;
13use wasmtime_environ::{Tunables, TypeConvert, WasmHeapType};
14
15/// The value of a WebAssembly global variable.
16#[derive(Clone, Copy)]
17pub enum GlobalVariable {
18    /// This is a variable in memory that should be referenced through a `GlobalValue`.
19    Memory {
20        /// The address of the global variable storage.
21        gv: ir::GlobalValue,
22        /// An offset to add to the address.
23        offset: Offset32,
24        /// The global variable's type.
25        ty: ir::Type,
26    },
27
28    /// This is a global variable that needs to be handled by the environment.
29    Custom,
30}
31
32/// Environment affecting the translation of a WebAssembly.
33pub trait TargetEnvironment: TypeConvert {
34    /// Get the information needed to produce Cranelift IR for the given target.
35    fn target_config(&self) -> TargetFrontendConfig;
36
37    /// Whether to enable Spectre mitigations for heap accesses.
38    fn heap_access_spectre_mitigation(&self) -> bool;
39
40    /// Whether to add proof-carrying-code facts to verify memory accesses.
41    fn proof_carrying_code(&self) -> bool;
42
43    /// Get the Cranelift reference type to use for the given Wasm reference
44    /// type.
45    ///
46    /// Returns a pair of the CLIF reference type to use and a boolean that
47    /// describes whether the value should be included in GC stack maps or not.
48    fn reference_type(&self, ty: WasmHeapType) -> (ir::Type, bool);
49
50    /// Returns the compilation knobs that are in effect.
51    fn tunables(&self) -> &Tunables;
52}
53
54/// A smallvec that holds the IR values for a struct's fields.
55pub type StructFieldsVec = SmallVec<[ir::Value; 4]>;