Skip to content

Commit 0be6a64

Browse files
authored
Merge pull request #127 from 0xPolygonMiden/greenhat/wasmcm-itest-support
[1/x] Add Wasm component translation support to the integration tests
2 parents b325490 + 1960cfd commit 0be6a64

File tree

22 files changed

+2159
-151
lines changed

22 files changed

+2159
-151
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend-wasm/tests/test_rust_comp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn rust_array() {
2828
test.expect_wasm(expect_file!["./expected/array.wat"]);
2929
test.expect_ir(expect_file!["./expected/array.hir"]);
3030
assert!(
31-
test.hir.unwrap().segments().last().unwrap().is_readonly(),
31+
test.hir.unwrap().unwrap_program().segments().last().unwrap().is_readonly(),
3232
"data segment should be readonly"
3333
);
3434
}
@@ -39,7 +39,7 @@ fn rust_static_mut() {
3939
test.expect_wasm(expect_file!["./expected/static_mut.wat"]);
4040
test.expect_ir(expect_file!["./expected/static_mut.hir"]);
4141
assert!(
42-
!test.hir.unwrap().segments().last().unwrap().is_readonly(),
42+
!test.hir.unwrap().unwrap_program().segments().last().unwrap().is_readonly(),
4343
"data segment should be mutable"
4444
);
4545
}

hir-type/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,24 @@ pub struct LiftedFunctionType {
635635
/// The results returned by this function
636636
pub results: Vec<Type>,
637637
}
638+
impl fmt::Display for LiftedFunctionType {
639+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
640+
use core::fmt::Write;
641+
642+
f.write_str("(func")?;
643+
for ty in self.params.iter() {
644+
write!(f, " (param {ty})")?;
645+
}
646+
if !self.results.is_empty() {
647+
f.write_str(" (result")?;
648+
for ty in self.results.iter() {
649+
write!(f, " {ty}")?;
650+
}
651+
f.write_char(')')?;
652+
}
653+
f.write_char(')')
654+
}
655+
}
638656

639657
/// This error is raised when parsing an [AddressSpace]
640658
#[derive(Debug)]

hir/src/component/interface.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
use core::fmt;
2+
13
use miden_hir_symbol::Symbol;
24

5+
use crate::formatter::PrettyPrint;
6+
37
/// A fully-qualified identifier for the interface being imported, e.g.
48
/// `namespace::package/interface@version`
5-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9+
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
610
pub struct InterfaceIdent {
711
/// A fully-qualified identifier for the interface being imported, e.g.
812
/// `namespace::package/interface@version`
@@ -19,8 +23,14 @@ impl InterfaceIdent {
1923
}
2024
}
2125

26+
impl fmt::Display for InterfaceIdent {
27+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28+
write!(f, "\"{}\"", self.full_name.as_str().escape_default())
29+
}
30+
}
31+
2232
/// An identifier for a function in an interface
23-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
33+
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2434
pub struct InterfaceFunctionIdent {
2535
/// An interface identifier for the interface being imported (e.g.
2636
/// `namespace::package/interface@version`)
@@ -39,3 +49,22 @@ impl InterfaceFunctionIdent {
3949
}
4050
}
4151
}
52+
53+
impl fmt::Display for InterfaceFunctionIdent {
54+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55+
self.pretty_print(f)
56+
}
57+
}
58+
impl PrettyPrint for InterfaceFunctionIdent {
59+
fn render(&self) -> crate::formatter::Document {
60+
use crate::formatter::*;
61+
62+
flatten(
63+
const_text("(")
64+
+ display(self.interface)
65+
+ const_text(" ")
66+
+ text(format!("#{}", self.function))
67+
+ const_text(")"),
68+
)
69+
}
70+
}

hir/src/component/mod.rs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use alloc::collections::BTreeMap;
12
use core::ops::{Deref, DerefMut};
2-
use std::collections::BTreeMap;
33

44
use intrusive_collections::RBTree;
55
use miden_core::crypto::hash::RpoDigest;
66

7+
use self::formatter::PrettyPrint;
78
use super::*;
89

910
mod interface;
@@ -19,6 +20,14 @@ pub enum FunctionInvocationMethod {
1920
#[default]
2021
Exec,
2122
}
23+
impl fmt::Display for FunctionInvocationMethod {
24+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25+
match self {
26+
Self::Call => f.write_str("call"),
27+
Self::Exec => f.write_str("exec"),
28+
}
29+
}
30+
}
2231

2332
/// A component import
2433
#[derive(Debug)]
@@ -32,6 +41,32 @@ pub struct ComponentImport {
3241
/// The MAST root hash of the function to be used in codegen
3342
pub digest: RpoDigest,
3443
}
44+
impl fmt::Display for ComponentImport {
45+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46+
self.pretty_print(f)
47+
}
48+
}
49+
impl formatter::PrettyPrint for ComponentImport {
50+
fn render(&self) -> formatter::Document {
51+
use crate::formatter::*;
52+
53+
const_text("(")
54+
+ const_text("import")
55+
+ const_text(" ")
56+
+ display(self.digest)
57+
+ const_text(" ")
58+
+ const_text("(")
59+
+ display(self.invoke_method)
60+
+ const_text(")")
61+
+ const_text(" ")
62+
+ const_text("(")
63+
+ const_text("type")
64+
+ const_text(" ")
65+
+ text(format!("{}", &self.function_ty))
66+
+ const_text(")")
67+
+ const_text(")")
68+
}
69+
}
3570

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

137+
impl fmt::Display for Component {
138+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139+
self.pretty_print(f)
140+
}
141+
}
142+
143+
impl formatter::PrettyPrint for Component {
144+
fn render(&self) -> formatter::Document {
145+
use crate::formatter::*;
146+
147+
let imports = self
148+
.imports
149+
.iter()
150+
.map(|(id, import)| {
151+
const_text("(")
152+
+ const_text("lower")
153+
+ const_text(" ")
154+
+ import.render()
155+
+ const_text(" ")
156+
+ id.render()
157+
+ const_text(")")
158+
})
159+
.reduce(|acc, doc| acc + nl() + doc)
160+
.map(|doc| const_text(";; Component Imports") + nl() + doc)
161+
.unwrap_or(Document::Empty);
162+
163+
let modules = self
164+
.modules
165+
.iter()
166+
.map(PrettyPrint::render)
167+
.reduce(|acc, doc| acc + nl() + doc)
168+
.map(|doc| const_text(";; Modules") + nl() + doc)
169+
.unwrap_or(Document::Empty);
170+
171+
let body = vec![imports, modules].into_iter().filter(|section| !section.is_empty()).fold(
172+
nl(),
173+
|a, b| {
174+
if matches!(a, Document::Newline) {
175+
indent(4, a + b)
176+
} else {
177+
a + nl() + indent(4, nl() + b)
178+
}
179+
},
180+
);
181+
182+
let header = const_text("(") + const_text("component") + const_text(" ");
183+
184+
if body.is_empty() {
185+
header + const_text(")") + nl()
186+
} else {
187+
header + body + nl() + const_text(")") + nl()
188+
}
189+
}
190+
}
191+
102192
/// This struct provides an ergonomic way to construct a [Component] in an imperative fashion.
103193
///
104194
/// Simply create the builder, add/build one or more modules, then call `link` to obtain a

hir/src/ident.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl PrettyPrint for FunctionIdent {
5454
fn render(&self) -> formatter::Document {
5555
use crate::formatter::*;
5656

57-
flatten(display(self.module) + const_text("::") + display(self.function))
57+
flatten(const_text("(") + display(self.module) + const_text(" ") + display(self.function))
5858
}
5959
}
6060
impl PartialOrd for FunctionIdent {

tests/integration/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ miden-stdlib.workspace = true
2222
miden-diagnostics.workspace = true
2323
midenc-session.workspace = true
2424
expect-test = "1.4.1"
25-
miden-integration-tests-rust-fib = {path = "../rust-apps/fib"}
25+
miden-integration-tests-rust-fib = { path = "../rust-apps/fib" }
2626
wasmprinter = "0.2.63"
2727
sha2 = "0.10"
28-
rustc-demangle = {version = "0.1.19", features = ["std"]}
28+
rustc-demangle = { version = "0.1.19", features = ["std"] }
2929
cargo_metadata = "0.18"
30+
derive_more.workspace = true
3031

3132
[dev-dependencies]
3233
miden-core.workspace = true

0 commit comments

Comments
 (0)