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
//! Performs translation from Wasm to MidenIR

// Coding conventions
#![deny(warnings)]
#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]

extern crate alloc;

mod code_translator;
mod component;
mod config;
mod error;
mod intrinsics;
mod miden_abi;
mod module;
mod ssa;
mod translation_utils;

#[cfg(test)]
mod test_utils;

use component::build_ir::translate_component;
use error::WasmResult;
use midenc_session::Session;
use module::build_ir::translate_module_as_component;
use wasmparser::WasmFeatures;

pub use self::{config::*, error::WasmError};

/// Translate a valid Wasm core module or Wasm Component Model binary into Miden
/// IR Component
pub fn translate(
    wasm: &[u8],
    config: &WasmTranslationConfig,
    session: &Session,
) -> WasmResult<midenc_hir::Component> {
    if wasm[4..8] == [0x01, 0x00, 0x00, 0x00] {
        // Wasm core module
        // see https://github.com/WebAssembly/component-model/blob/main/design/mvp/Binary.md#component-definitions
        translate_module_as_component(wasm, config, session)
    } else {
        translate_component(wasm, config, session)
    }
}

/// The set of core WebAssembly features which we need to or wish to support
pub(crate) fn supported_features() -> WasmFeatures {
    WasmFeatures::BULK_MEMORY
        | WasmFeatures::FLOATS
        | WasmFeatures::FUNCTION_REFERENCES
        | WasmFeatures::MULTI_VALUE
        | WasmFeatures::MUTABLE_GLOBAL
        | WasmFeatures::SATURATING_FLOAT_TO_INT
        | WasmFeatures::SIGN_EXTENSION
        | WasmFeatures::TAIL_CALL
}

/// The extended set of WebAssembly features which are enabled when working with the Wasm Component
/// Model
pub(crate) fn supported_component_model_features() -> WasmFeatures {
    supported_features() | WasmFeatures::COMPONENT_MODEL
}