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 
9 use cranelift_codegen::ir;
10 use cranelift_codegen::ir::immediates::Offset32;
11 use cranelift_codegen::isa::TargetFrontendConfig;
12 use smallvec::SmallVec;
13 use wasmtime_environ::{GlobalConstValue, Tunables, TypeConvert, WasmHeapType};
14 
15 /// The value of a WebAssembly global variable.
16 #[derive(Clone, Copy)]
17 pub enum GlobalVariable {
18     /// The global is known to be a constant value.
19     Constant {
20         /// The global's known value.
21         value: GlobalConstValue,
22     },
23 
24     /// This is a variable in memory that should be referenced through a `GlobalValue`.
25     Memory {
26         /// The address of the global variable storage.
27         gv: ir::GlobalValue,
28         /// An offset to add to the address.
29         offset: Offset32,
30         /// The global variable's type.
31         ty: ir::Type,
32     },
33 
34     /// This is a global variable that needs to be handled by the environment.
35     Custom,
36 }
37 
38 /// Environment affecting the translation of a WebAssembly.
39 pub trait TargetEnvironment: TypeConvert {
40     /// Get the information needed to produce Cranelift IR for the given target.
target_config(&self) -> TargetFrontendConfig41     fn target_config(&self) -> TargetFrontendConfig;
42 
43     /// Whether to enable Spectre mitigations for heap accesses.
heap_access_spectre_mitigation(&self) -> bool44     fn heap_access_spectre_mitigation(&self) -> bool;
45 
46     /// Get the Cranelift reference type to use for the given Wasm reference
47     /// type.
48     ///
49     /// Returns a pair of the CLIF reference type to use and a boolean that
50     /// describes whether the value should be included in GC stack maps or not.
reference_type(&self, ty: WasmHeapType) -> (ir::Type, bool)51     fn reference_type(&self, ty: WasmHeapType) -> (ir::Type, bool);
52 
53     /// Returns the compilation knobs that are in effect.
tunables(&self) -> &Tunables54     fn tunables(&self) -> &Tunables;
55 }
56 
57 /// A smallvec that holds the IR values for a struct's fields.
58 pub type StructFieldsVec = SmallVec<[ir::Value; 4]>;
59