Skip to content

[1/x] Add Wasm component translation support to the integration tests #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

4 changes: 2 additions & 2 deletions frontend-wasm/tests/test_rust_comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn rust_array() {
test.expect_wasm(expect_file!["./expected/array.wat"]);
test.expect_ir(expect_file!["./expected/array.hir"]);
assert!(
test.hir.unwrap().segments().last().unwrap().is_readonly(),
test.hir.unwrap().unwrap_program().segments().last().unwrap().is_readonly(),
"data segment should be readonly"
);
}
Expand All @@ -39,7 +39,7 @@ fn rust_static_mut() {
test.expect_wasm(expect_file!["./expected/static_mut.wat"]);
test.expect_ir(expect_file!["./expected/static_mut.hir"]);
assert!(
!test.hir.unwrap().segments().last().unwrap().is_readonly(),
!test.hir.unwrap().unwrap_program().segments().last().unwrap().is_readonly(),
"data segment should be mutable"
);
}
18 changes: 18 additions & 0 deletions hir-type/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,24 @@ pub struct LiftedFunctionType {
/// The results returned by this function
pub results: Vec<Type>,
}
impl fmt::Display for LiftedFunctionType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use core::fmt::Write;

f.write_str("(func")?;
for ty in self.params.iter() {
write!(f, " (param {ty})")?;
}
if !self.results.is_empty() {
f.write_str(" (result")?;
for ty in self.results.iter() {
write!(f, " {ty}")?;
}
f.write_char(')')?;
}
f.write_char(')')
}
}

/// This error is raised when parsing an [AddressSpace]
#[derive(Debug)]
Expand Down
33 changes: 31 additions & 2 deletions hir/src/component/interface.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use core::fmt;

use miden_hir_symbol::Symbol;

use crate::formatter::PrettyPrint;

/// A fully-qualified identifier for the interface being imported, e.g.
/// `namespace::package/interface@version`
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InterfaceIdent {
/// A fully-qualified identifier for the interface being imported, e.g.
/// `namespace::package/interface@version`
Expand All @@ -19,8 +23,14 @@ impl InterfaceIdent {
}
}

impl fmt::Display for InterfaceIdent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\"{}\"", self.full_name.as_str().escape_default())
}
}

/// An identifier for a function in an interface
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InterfaceFunctionIdent {
/// An interface identifier for the interface being imported (e.g.
/// `namespace::package/interface@version`)
Expand All @@ -39,3 +49,22 @@ impl InterfaceFunctionIdent {
}
}
}

impl fmt::Display for InterfaceFunctionIdent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.pretty_print(f)
}
}
impl PrettyPrint for InterfaceFunctionIdent {
fn render(&self) -> crate::formatter::Document {
use crate::formatter::*;

flatten(
const_text("(")
+ display(self.interface)
+ const_text(" ")
+ text(format!("#{}", self.function))
+ const_text(")"),
)
}
}
92 changes: 91 additions & 1 deletion hir/src/component/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use alloc::collections::BTreeMap;
use core::ops::{Deref, DerefMut};
use std::collections::BTreeMap;

use intrusive_collections::RBTree;
use miden_core::crypto::hash::RpoDigest;

use self::formatter::PrettyPrint;
use super::*;

mod interface;
Expand All @@ -19,6 +20,14 @@ pub enum FunctionInvocationMethod {
#[default]
Exec,
}
impl fmt::Display for FunctionInvocationMethod {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Call => f.write_str("call"),
Self::Exec => f.write_str("exec"),
}
}
}

/// A component import
#[derive(Debug)]
Expand All @@ -32,6 +41,32 @@ pub struct ComponentImport {
/// The MAST root hash of the function to be used in codegen
pub digest: RpoDigest,
}
impl fmt::Display for ComponentImport {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.pretty_print(f)
}
}
impl formatter::PrettyPrint for ComponentImport {
fn render(&self) -> formatter::Document {
use crate::formatter::*;

const_text("(")
+ const_text("import")
+ const_text(" ")
+ display(self.digest)
+ const_text(" ")
+ const_text("(")
+ display(self.invoke_method)
+ const_text(")")
+ const_text(" ")
+ const_text("(")
+ const_text("type")
+ const_text(" ")
+ text(format!("{}", &self.function_ty))
+ const_text(")")
+ const_text(")")
}
}

/// The name of a exported function
#[derive(Debug, Ord, PartialEq, PartialOrd, Eq, Hash, derive_more::From, derive_more::Into)]
Expand Down Expand Up @@ -99,6 +134,61 @@ impl Component {
}
}

impl fmt::Display for Component {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.pretty_print(f)
}
}

impl formatter::PrettyPrint for Component {
fn render(&self) -> formatter::Document {
use crate::formatter::*;

let imports = self
.imports
.iter()
.map(|(id, import)| {
const_text("(")
+ const_text("lower")
+ const_text(" ")
+ import.render()
+ const_text(" ")
+ id.render()
+ const_text(")")
})
.reduce(|acc, doc| acc + nl() + doc)
.map(|doc| const_text(";; Component Imports") + nl() + doc)
.unwrap_or(Document::Empty);

let modules = self
.modules
.iter()
.map(PrettyPrint::render)
.reduce(|acc, doc| acc + nl() + doc)
.map(|doc| const_text(";; Modules") + nl() + doc)
.unwrap_or(Document::Empty);

let body = vec![imports, modules].into_iter().filter(|section| !section.is_empty()).fold(
nl(),
|a, b| {
if matches!(a, Document::Newline) {
indent(4, a + b)
} else {
a + nl() + indent(4, nl() + b)
}
},
);

let header = const_text("(") + const_text("component") + const_text(" ");

if body.is_empty() {
header + const_text(")") + nl()
} else {
header + body + nl() + const_text(")") + nl()
}
}
}

/// This struct provides an ergonomic way to construct a [Component] in an imperative fashion.
///
/// Simply create the builder, add/build one or more modules, then call `link` to obtain a
Expand Down
2 changes: 1 addition & 1 deletion hir/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl PrettyPrint for FunctionIdent {
fn render(&self) -> formatter::Document {
use crate::formatter::*;

flatten(display(self.module) + const_text("::") + display(self.function))
flatten(const_text("(") + display(self.module) + const_text(" ") + display(self.function))
}
}
impl PartialOrd for FunctionIdent {
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ miden-stdlib.workspace = true
miden-diagnostics.workspace = true
midenc-session.workspace = true
expect-test = "1.4.1"
miden-integration-tests-rust-fib = {path = "../rust-apps/fib"}
miden-integration-tests-rust-fib = { path = "../rust-apps/fib" }
wasmprinter = "0.2.63"
sha2 = "0.10"
rustc-demangle = {version = "0.1.19", features = ["std"]}
rustc-demangle = { version = "0.1.19", features = ["std"] }
cargo_metadata = "0.18"
derive_more.workspace = true

[dev-dependencies]
miden-core.workspace = true
Expand Down
Loading