wasmer_compiler_singlepass/
config.rs

1// Allow unused imports while developing
2#![allow(unused_imports, dead_code)]
3
4use crate::compiler::SinglepassCompiler;
5use crate::emitter_x64::Location;
6use smallvec::SmallVec;
7use std::sync::Arc;
8use wasmer_compiler::{Compiler, CompilerConfig, CpuFeature, Target};
9use wasmer_types::{Features, FunctionType, Type};
10
11#[derive(Debug, Clone)]
12pub(crate) enum IntrinsicKind {
13    Gas,
14}
15
16#[derive(Debug, Clone)]
17pub(crate) struct Intrinsic {
18    pub(crate) kind: IntrinsicKind,
19    pub(crate) name: String,
20    pub(crate) signature: FunctionType,
21}
22
23#[derive(Debug, Clone)]
24pub struct Singlepass {
25    pub(crate) enable_nan_canonicalization: bool,
26    pub(crate) enable_stack_check: bool,
27    /// Compiler intrinsics.
28    pub(crate) intrinsics: Vec<Intrinsic>,
29}
30
31impl Singlepass {
32    /// Creates a new configuration object with the default configuration
33    /// specified.
34    pub fn new() -> Self {
35        Self {
36            enable_nan_canonicalization: true,
37            enable_stack_check: false,
38            intrinsics: vec![Intrinsic {
39                kind: IntrinsicKind::Gas,
40                name: "gas".to_string(),
41                signature: ([Type::I32], []).into(),
42            }],
43        }
44    }
45
46    /// Enable stack check.
47    ///
48    /// When enabled, an explicit stack depth check will be performed on entry
49    /// to each function to prevent stack overflow.
50    ///
51    /// Note that this doesn't guarantee deterministic execution across
52    /// different platforms.
53    pub fn enable_stack_check(&mut self, enable: bool) -> &mut Self {
54        self.enable_stack_check = enable;
55        self
56    }
57
58    fn enable_nan_canonicalization(&mut self) {
59        self.enable_nan_canonicalization = true;
60    }
61
62    pub fn canonicalize_nans(&mut self, enable: bool) -> &mut Self {
63        self.enable_nan_canonicalization = enable;
64        self
65    }
66}
67
68impl CompilerConfig for Singlepass {
69    fn enable_pic(&mut self) {
70        // Do nothing, since singlepass already emits
71        // PIC code.
72    }
73
74    /// Transform it into the compiler
75    fn compiler(self: Box<Self>) -> Box<dyn Compiler> {
76        Box::new(SinglepassCompiler::new(*self))
77    }
78
79    /// Gets the default features for this compiler in the given target
80    fn default_features_for_target(&self, _target: &Target) -> Features {
81        let mut features = Features::default();
82        features.multi_value(false);
83        features
84    }
85}
86
87impl Default for Singlepass {
88    fn default() -> Singlepass {
89        Self::new()
90    }
91}
92
93impl Intrinsic {
94    pub(crate) fn is_params_ok(&self, params: &SmallVec<[Location; 8]>) -> bool {
95        match self.kind {
96            IntrinsicKind::Gas => match params[0] {
97                Location::Imm32(value) => value < i32::MAX as u32,
98                _ => false,
99            },
100        }
101    }
102}