Skip to content

Commit

Permalink
test: use midenc driver to compile cargo-based fixtures
Browse files Browse the repository at this point in the history
in integration tests
  • Loading branch information
greenhat committed May 4, 2024
1 parent cc3f740 commit e485804
Show file tree
Hide file tree
Showing 26 changed files with 193 additions and 6,702 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codegen/masm/src/masm/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl Module {
imported.insert(import.alias.to_string(), path.clone());
if let Some(imported_fns) = self.imports.imported(&import.alias) {
for import_fn in imported_fns.iter().copied() {
let fname = import_fn.to_string();
let fname = import_fn.function.name;
let name = masm::ProcedureName::try_from(fname.as_str())
.expect("invalid function name");
let id = masm::ProcedureId::from_name(fname.as_str(), &path);
Expand Down
6 changes: 4 additions & 2 deletions frontend-wasm/src/code_translator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::fmt::Write;
use expect_test::expect;
use miden_hir::Ident;

use crate::{test_utils::test_diagnostics, translate_module, WasmTranslationConfig};
use crate::{test_utils::test_diagnostics, translate, WasmTranslationConfig};

/// Check IR generated for a Wasm op(s).
/// Wrap Wasm ops in a function and check the IR generated for the entry block of that function.
Expand All @@ -19,7 +19,9 @@ fn check_op(wat_op: &str, expected_ir: expect_test::Expect) {
);
let wasm = wat::parse_str(wat).unwrap();
let diagnostics = test_diagnostics();
let module = translate_module(&wasm, &WasmTranslationConfig::default(), &diagnostics).unwrap();
let module = translate(&wasm, &WasmTranslationConfig::default(), &diagnostics)
.unwrap()
.unwrap_one_module();
let func = module.function(Ident::from("test_wrapper")).unwrap();
// let fref = module.get_funcref_by_name("test_wrapper").unwrap();
// let func = module.get_function(fref).unwrap();
Expand Down
28 changes: 22 additions & 6 deletions frontend-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,25 @@ mod translation_utils;
#[cfg(test)]
mod test_utils;

pub use self::{
component::build_ir::translate_component,
config::*,
error::WasmError,
module::build_ir::{translate_module, translate_module_as_component},
};
use component::build_ir::translate_component;
use error::WasmResult;
use miden_diagnostics::DiagnosticsHandler;
use module::build_ir::translate_module_as_component;

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,
diagnostics: &DiagnosticsHandler,
) -> WasmResult<miden_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, diagnostics)
} else {
translate_component(wasm, config, diagnostics)
}
}
27 changes: 0 additions & 27 deletions frontend-wasm/src/module/build_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,6 @@ use crate::{
WasmError, WasmTranslationConfig,
};

/// Translate a valid Wasm core module binary into Miden IR module
pub fn translate_module(
wasm: &[u8],
config: &WasmTranslationConfig,
diagnostics: &DiagnosticsHandler,
) -> WasmResult<miden_hir::Module> {
let wasm_features = WasmFeatures::default();
let mut validator = Validator::new_with_features(wasm_features);
let parser = wasmparser::Parser::new(0);
let mut module_types_builder = Default::default();
let mut parsed_module = ModuleEnvironment::new(
config,
&mut validator,
&mut module_types_builder,
)
.parse(parser, wasm, diagnostics)?;
parsed_module.module.set_name_fallback(config.source_name.clone());
if let Some(name_override) = config.override_name.as_ref() {
parsed_module.module.set_name_override(name_override.clone());
}
let module_types = module_types_builder.finish();

let mut module_state =
ModuleTranslationState::new(&parsed_module.module, &module_types, vec![]);
build_ir_module(&mut parsed_module, &module_types, &mut module_state, config, diagnostics)
}

/// Translate a valid Wasm core module binary into Miden IR component building
/// component imports for well-known Miden ABI functions
///
Expand Down
3 changes: 3 additions & 0 deletions frontend-wasm/src/module/module_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
}
}
wasmparser::Name::Module { name, .. } => {
// Module name ends with `.wasm`,
// remove it if its there since MASM doesn't yet allow dots in the module name
let name = name.trim_end_matches(".wasm");
self.result.module.name_section.module_name = Some(Ident::from(name));
}
wasmparser::Name::Local(reader) => {
Expand Down
6 changes: 6 additions & 0 deletions hir/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ impl Component {
pub fn exports(&self) -> &BTreeMap<FunctionExportName, ComponentExport> {
&self.exports
}

/// Extracts the single module from this component, panicking if there is not exactly one.
pub fn unwrap_one_module(self) -> Box<Module> {
assert_eq!(self.modules.len(), 1, "Expected exactly one module in the component");
self.to_modules().drain(..).next().unwrap().1
}
}

impl fmt::Display for Component {
Expand Down
21 changes: 14 additions & 7 deletions midenc-compile/src/stages/link.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use midenc_session::ProjectType;

use super::*;

/// This type is used to represent the fact that depending on
Expand All @@ -21,14 +23,19 @@ impl Stage for LinkerStage {
_analyses: &mut AnalysisManager,
session: &Session,
) -> CompilerResult<Self::Output> {
if session.should_link() {
let mut builder = hir::ProgramBuilder::new(&session.diagnostics);
for module in input.into_iter() {
builder.add_module(module)?;
match session.project_type {
ProjectType::Program => {
if session.should_link() {
let mut builder = hir::ProgramBuilder::new(&session.diagnostics);
for module in input.into_iter() {
builder.add_module(module)?;
}
Ok(MaybeLinked::Linked(builder.link()?))
} else {
Ok(MaybeLinked::Unlinked(input.into_iter().collect()))
}
}
Ok(MaybeLinked::Linked(builder.link()?))
} else {
Ok(MaybeLinked::Unlinked(input.into_iter().collect()))
ProjectType::Library => Ok(MaybeLinked::Unlinked(input.into_iter().collect())),
}
}
}
12 changes: 6 additions & 6 deletions midenc-compile/src/stages/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ impl ParseStage {
session: &Session,
config: &WasmTranslationConfig,
) -> CompilerResult<ParseOutput> {
let module = wasm::translate_module(bytes, config, &session.diagnostics)?;
let module = wasm::translate(bytes, config, &session.diagnostics)?.unwrap_one_module();

Ok(ParseOutput::Hir(Box::new(module)))
Ok(ParseOutput::Hir(module))
}

fn parse_hir_from_wat_file(
Expand All @@ -132,9 +132,9 @@ impl ParseStage {
..Default::default()
};
let wasm = wat::parse_file(path)?;
let module = wasm::translate_module(&wasm, &config, &session.diagnostics)?;
let module = wasm::translate(&wasm, &config, &session.diagnostics)?.unwrap_one_module();

Ok(ParseOutput::Hir(Box::new(module)))
Ok(ParseOutput::Hir(module))
}

fn parse_hir_from_wat_bytes(
Expand All @@ -144,8 +144,8 @@ impl ParseStage {
config: &WasmTranslationConfig,
) -> CompilerResult<ParseOutput> {
let wasm = wat::parse_bytes(bytes)?;
let module = wasm::translate_module(&wasm, config, &session.diagnostics)?;
let module = wasm::translate(&wasm, config, &session.diagnostics)?.unwrap_one_module();

Ok(ParseOutput::Hir(Box::new(module)))
Ok(ParseOutput::Hir(module))
}
}
8 changes: 6 additions & 2 deletions midenc-session/src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ impl InputFile {

let mut input = Vec::with_capacity(1024);
std::io::stdin().read_to_end(&mut input)?;
let file_type = FileType::detect(&input)?;
Self::from_bytes(input, name)
}

pub fn from_bytes(bytes: Vec<u8>, name: FileName) -> Result<Self, InvalidInputError> {
let file_type = FileType::detect(&bytes)?;
match file_type {
FileType::Hir | FileType::Wasm | FileType::Wat => Ok(Self {
file: InputType::Stdin { name, input },
file: InputType::Stdin { name, input: bytes },
file_type,
}),
// We do not yet have frontends for these file types
Expand Down
1 change: 1 addition & 0 deletions tests/integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ miden-processor.workspace = true
miden-stdlib.workspace = true
miden-diagnostics.workspace = true
midenc-session.workspace = true
midenc-compile.workspace = true
expect-test = "1.4.1"
miden-integration-tests-rust-fib = { path = "../rust-apps/fib" }
wasmprinter = "0.2.63"
Expand Down
Loading

0 comments on commit e485804

Please sign in to comment.