midenc_frontend_wasm/
lib.rs

1//! Performs translation from Wasm to MidenIR
2
3// Coding conventions
4#![deny(warnings)]
5#![deny(missing_docs)]
6#![deny(rustdoc::broken_intra_doc_links)]
7// Allow unused code that we're going to need for implementing the missing Wasm features (call_direct, tables, etc.)
8#![allow(dead_code)]
9#![feature(iterator_try_collect)]
10
11extern crate alloc;
12
13mod callable;
14mod code_translator;
15mod component;
16mod config;
17mod error;
18mod intrinsics;
19mod miden_abi;
20mod module;
21mod ssa;
22mod translation_utils;
23
24use alloc::rc::Rc;
25
26use component::build_ir::translate_component;
27use error::WasmResult;
28use midenc_hir::{dialects::builtin, Context};
29use module::build_ir::translate_module_as_component;
30use wasmparser::WasmFeatures;
31
32pub use self::{config::*, error::WasmError};
33
34/// The output of the frontend Wasm translation stage
35pub struct FrontendOutput {
36    /// The IR component translated from the Wasm
37    pub component: builtin::ComponentRef,
38    /// The serialized AccountComponentMetadata (name, description, storage layout, etc.)
39    pub account_component_metadata_bytes: Option<Vec<u8>>,
40}
41
42/// Translate a valid Wasm core module or Wasm Component Model binary into Miden
43/// IR Component
44pub fn translate(
45    wasm: &[u8],
46    config: &WasmTranslationConfig,
47    context: Rc<Context>,
48) -> WasmResult<FrontendOutput> {
49    if wasm[4..8] == [0x01, 0x00, 0x00, 0x00] {
50        // Wasm core module
51        // see https://github.com/WebAssembly/component-model/blob/main/design/mvp/Binary.md#component-definitions
52        let component = translate_module_as_component(wasm, config, context)?;
53        Ok(FrontendOutput {
54            component,
55            account_component_metadata_bytes: None,
56        })
57    } else {
58        translate_component(wasm, config, context)
59    }
60}
61
62/// The set of core WebAssembly features which we need to or wish to support
63pub(crate) fn supported_features() -> WasmFeatures {
64    WasmFeatures::BULK_MEMORY
65        | WasmFeatures::FLOATS
66        | WasmFeatures::FUNCTION_REFERENCES
67        | WasmFeatures::MULTI_VALUE
68        | WasmFeatures::MUTABLE_GLOBAL
69        | WasmFeatures::SATURATING_FLOAT_TO_INT
70        | WasmFeatures::SIGN_EXTENSION
71        | WasmFeatures::TAIL_CALL
72        | WasmFeatures::WIDE_ARITHMETIC
73}
74
75/// The extended set of WebAssembly features which are enabled when working with the Wasm Component
76/// Model
77pub(crate) fn supported_component_model_features() -> WasmFeatures {
78    supported_features() | WasmFeatures::COMPONENT_MODEL
79}