1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::compiler::Compiler;
use anyhow::{bail, Result};
use std::sync::Arc;
use wasmtime_cranelift_shared::isa_builder::IsaBuilder;
use wasmtime_environ::{CompilerBuilder, Setting};
use winch_codegen::{isa, TargetIsa};

/// Compiler builder.
struct Builder {
    inner: IsaBuilder<Result<Box<dyn TargetIsa>>>,
}

pub fn builder() -> Box<dyn CompilerBuilder> {
    Box::new(Builder {
        inner: IsaBuilder::new(|triple| isa::lookup(triple).map_err(|e| e.into())),
    })
}

impl CompilerBuilder for Builder {
    fn triple(&self) -> &target_lexicon::Triple {
        self.inner.triple()
    }

    fn target(&mut self, target: target_lexicon::Triple) -> Result<()> {
        self.inner.target(target)?;
        Ok(())
    }

    fn set(&mut self, name: &str, value: &str) -> Result<()> {
        self.inner.set(name, value)
    }

    fn enable(&mut self, name: &str) -> Result<()> {
        self.inner.enable(name)
    }

    fn settings(&self) -> Vec<Setting> {
        self.inner.settings()
    }

    fn set_tunables(&mut self, tunables: wasmtime_environ::Tunables) -> Result<()> {
        let _ = tunables;
        Ok(())
    }

    fn build(&self) -> Result<Box<dyn wasmtime_environ::Compiler>> {
        let isa = self.inner.build()?;

        Ok(Box::new(Compiler::new(isa)))
    }

    fn enable_incremental_compilation(
        &mut self,
        _cache_store: Arc<dyn wasmtime_environ::CacheStore>,
    ) -> Result<()> {
        bail!("incremental compilation is not supported on this platform");
    }
}

impl std::fmt::Debug for Builder {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "Builder: {{ triple: {:?} }}", self.triple())
    }
}