Skip to content

Commit 5252664

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 7bc8936 commit 5252664

File tree

20 files changed

+1939
-118
lines changed

20 files changed

+1939
-118
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/src/asm/isa.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,3 +1363,12 @@ impl MastRootHash {
13631363
&self.0
13641364
}
13651365
}
1366+
1367+
impl fmt::Display for MastRootHash {
1368+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1369+
for byte in &self.0 {
1370+
write!(f, "{:02x}", byte)?;
1371+
}
1372+
Ok(())
1373+
}
1374+
}

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
@@ -21,6 +21,15 @@ pub enum FunctionInvocationMethod {
2121
Exec,
2222
}
2323

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

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

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

hir/src/function.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,29 @@ pub struct LiftedFunctionType {
233233
pub results: Vec<Type>,
234234
}
235235

236+
impl fmt::Display for LiftedFunctionType {
237+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
238+
write!(f, "fn(")?;
239+
for (i, param) in self.params.iter().enumerate() {
240+
if i > 0 {
241+
write!(f, ", ")?;
242+
}
243+
write!(f, "{}", param)?;
244+
}
245+
write!(f, ")")?;
246+
if !self.results.is_empty() {
247+
write!(f, " -> ")?;
248+
for (i, result) in self.results.iter().enumerate() {
249+
if i > 0 {
250+
write!(f, ", ")?;
251+
}
252+
write!(f, "{}", result)?;
253+
}
254+
}
255+
Ok(())
256+
}
257+
}
258+
236259
/// An [ExternalFunction] represents a function whose name and signature are known,
237260
/// but which may or may not be compiled as part of the current translation unit.
238261
///

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
proptest.workspace = true

0 commit comments

Comments
 (0)