Skip to content

Commit d172cb7

Browse files
committed
chore: add Wasm component translation support to the integration tests;
Add Wasm component translation support to `CompileTest`. Draft a text representation of the IR `Component` and use it in the expected tests.
1 parent 067220e commit d172cb7

File tree

19 files changed

+1930
-117
lines changed

19 files changed

+1930
-117
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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ 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
32+
.unwrap()
33+
.unwrap_program()
34+
.segments()
35+
.last()
36+
.unwrap()
37+
.is_readonly(),
3238
"data segment should be readonly"
3339
);
3440
}
@@ -39,7 +45,14 @@ fn rust_static_mut() {
3945
test.expect_wasm(expect_file!["./expected/static_mut.wat"]);
4046
test.expect_ir(expect_file!["./expected/static_mut.hir"]);
4147
assert!(
42-
!test.hir.unwrap().segments().last().unwrap().is_readonly(),
48+
!test
49+
.hir
50+
.unwrap()
51+
.unwrap_program()
52+
.segments()
53+
.last()
54+
.unwrap()
55+
.is_readonly(),
4356
"data segment should be mutable"
4457
);
4558
}

hir-type/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,29 @@ pub struct LiftedFunctionType {
638638
pub results: Vec<Type>,
639639
}
640640

641+
impl fmt::Display for LiftedFunctionType {
642+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
643+
write!(f, "fn(")?;
644+
for (i, param) in self.params.iter().enumerate() {
645+
if i > 0 {
646+
write!(f, ", ")?;
647+
}
648+
write!(f, "{}", param)?;
649+
}
650+
write!(f, ")")?;
651+
if !self.results.is_empty() {
652+
write!(f, " -> ")?;
653+
for (i, result) in self.results.iter().enumerate() {
654+
if i > 0 {
655+
write!(f, ", ")?;
656+
}
657+
write!(f, "{}", result)?;
658+
}
659+
}
660+
Ok(())
661+
}
662+
}
663+
641664
/// This error is raised when parsing an [AddressSpace]
642665
#[derive(Debug)]
643666
pub enum InvalidAddressSpaceError {

hir/src/component/interface.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ impl InterfaceFunctionIdent {
3535
}
3636
}
3737
}
38+
39+
impl std::fmt::Display for InterfaceFunctionIdent {
40+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41+
write!(f, "{}::{}", self.interface.full_name, self.function)
42+
}
43+
}

hir/src/component/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ pub enum FunctionInvocationMethod {
2222
Exec,
2323
}
2424

25+
impl fmt::Display for FunctionInvocationMethod {
26+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
match self {
28+
FunctionInvocationMethod::Call => write!(f, "\"call\""),
29+
FunctionInvocationMethod::Exec => write!(f, "\"exec\""),
30+
}
31+
}
32+
}
33+
2534
/// A component import
2635
#[derive(Debug)]
2736
pub struct ComponentImport {
@@ -35,6 +44,19 @@ pub struct ComponentImport {
3544
pub digest: RpoDigest,
3645
}
3746

47+
impl fmt::Display for ComponentImport {
48+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49+
write!(
50+
f,
51+
"{} {} {} mast#0x{}",
52+
self.invoke_method,
53+
self.interface_function,
54+
self.function_ty,
55+
self.function_mast_root_hash
56+
)
57+
}
58+
}
59+
3860
/// The name of a exported function
3961
#[derive(Debug, Ord, PartialEq, PartialOrd, Eq, Hash, derive_more::From, derive_more::Into)]
4062
pub struct FunctionExportName(Symbol);
@@ -100,6 +122,26 @@ impl Component {
100122
}
101123
}
102124

125+
impl fmt::Display for Component {
126+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127+
writeln!(f, "component")?;
128+
writeln!(f, "")?;
129+
for (function_id, import) in self.imports.iter() {
130+
writeln!(f, "import {import} lower {function_id}",)?;
131+
}
132+
writeln!(f, "")?;
133+
for module in self.modules.iter() {
134+
// temporary hack to separate modules, until curly braces and indentation are implemented
135+
writeln!(
136+
f,
137+
"// ===================================================================="
138+
)?;
139+
writeln!(f, "{}", module)?;
140+
}
141+
Ok(())
142+
}
143+
}
144+
103145
/// This struct provides an ergonomic way to construct a [Component] in an imperative fashion.
104146
///
105147
/// Simply create the builder, add/build one or more modules, then call `link` to obtain a [Component].

tests/integration/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ wasmprinter = "0.2.63"
2727
sha2 = "0.10"
2828
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)