Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ smallstr = { version = "0.3", features = ["union"] }
thiserror = "1.0"
toml = { version = "0.5", features = ["preserve_order"] }
derive_more = "0.99"
indexmap = "2.1"
# 211152c631d16a943aae503466b198b93c61150f is latest (as of Jan 25th) commit in the next branch
miden-assembly = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "211152c631d16a943aae503466b198b93c61150f"}
miden-core = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "211152c631d16a943aae503466b198b93c61150f" }
Expand Down
2 changes: 1 addition & 1 deletion frontend-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ log.workspace = true
anyhow.workspace = true
wasmparser = "0.118.1"
derive_more.workspace = true
indexmap = "2.1"
indexmap.workspace = true
gimli = { version = "0.28.0", default-features = false, features = ['read', 'std'] }
rustc-hash.workspace = true

Expand Down
18 changes: 12 additions & 6 deletions frontend-wasm/src/code_translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::collections::hash_map;
use std::u64;

use crate::error::{WasmError, WasmResult};
use crate::module::func_env::FuncEnvironment;
use crate::module::func_translation_state::{ControlStackFrame, ElseData, FuncTranslationState};
use crate::module::function_builder_ext::FunctionBuilderExt;
use crate::module::types::{ir_type, BlockType, FuncIndex, GlobalIndex, ModuleTypes};
Expand Down Expand Up @@ -44,6 +45,7 @@ pub fn translate_operator(
state: &mut FuncTranslationState,
module: &Module,
mod_types: &ModuleTypes,
func_env: &FuncEnvironment,
diagnostics: &DiagnosticsHandler,
span: SourceSpan,
) -> WasmResult<()> {
Expand Down Expand Up @@ -121,12 +123,18 @@ pub fn translate_operator(
state,
builder,
FuncIndex::from_u32(*function_index),
module,
mod_types,
func_env,
span,
diagnostics,
)?;
}
Operator::CallIndirect {
type_index: _,
table_index: _,
table_byte: _,
} => {
// TODO: implement call_indirect
}
/******************************* Memory management *********************************/
Operator::MemoryGrow { .. } => {
let arg = state.pop1_casted(U32, builder, span);
Expand Down Expand Up @@ -633,16 +641,14 @@ fn translate_call(
state: &mut FuncTranslationState,
builder: &mut FunctionBuilderExt,
function_index: FuncIndex,
module: &Module,
mod_types: &ModuleTypes,
func_env: &FuncEnvironment,
span: SourceSpan,
diagnostics: &DiagnosticsHandler,
) -> WasmResult<()> {
let (fident, num_args) = state.get_direct_func(
builder.data_flow_graph_mut(),
function_index,
module,
mod_types,
func_env,
diagnostics,
)?;
let args = state.peekn_mut(num_args);
Expand Down
216 changes: 112 additions & 104 deletions frontend-wasm/src/code_translator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ fn module() {
)
"#,
expect![[r#"
module noname
module noname {

pub fn main() {
block0:
v0 = const.i32 0 : i32;
br block1;
pub fn main() {
block0:
v0 = const.i32 0 : i32;
br block1;

block1:
ret;
block1:
ret;
}
}
"#]],
);
Expand All @@ -86,16 +87,17 @@ fn locals() {
)
"#,
expect![[r#"
module noname
module noname {

pub fn main() {
block0:
v0 = const.i32 0 : i32;
v1 = const.i32 1 : i32;
br block1;
pub fn main() {
block0:
v0 = const.i32 0 : i32;
v1 = const.i32 1 : i32;
br block1;

block1:
ret;
block1:
ret;
}
}
"#]],
);
Expand Down Expand Up @@ -124,26 +126,27 @@ fn locals_inter_block() {
)
"#,
expect![[r#"
module noname
module noname {

pub fn main() -> i32 {
block0:
v1 = const.i32 0 : i32;
v2 = const.i32 3 : i32;
br block2;
pub fn main() -> i32 {
block0:
v1 = const.i32 0 : i32;
v2 = const.i32 3 : i32;
br block2;

block1(v0: i32):
ret v0;
block1(v0: i32):
ret v0;

block2:
v3 = const.i32 5 : i32;
v4 = add.wrapping v2, v3 : i32;
br block3;
block2:
v3 = const.i32 5 : i32;
v4 = add.wrapping v2, v3 : i32;
br block3;

block3:
v5 = const.i32 7 : i32;
v6 = add.wrapping v5, v4 : i32;
br block1(v6);
block3:
v5 = const.i32 7 : i32;
v6 = add.wrapping v5, v4 : i32;
br block1(v6);
}
}
"#]],
);
Expand All @@ -167,26 +170,27 @@ fn func_call() {
)
"#,
expect![[r#"
module noname
module noname {

pub fn add(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
v3 = add.wrapping v0, v1 : i32;
br block1(v3);
pub fn add(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
v3 = add.wrapping v0, v1 : i32;
br block1(v3);

block1(v2: i32):
ret v2;
}
block1(v2: i32):
ret v2;
}

pub fn main() -> i32 {
block0:
v1 = const.i32 3 : i32;
v2 = const.i32 5 : i32;
v3 = call noname::add(v1, v2) : i32;
br block1(v3);
pub fn main() -> i32 {
block0:
v1 = const.i32 3 : i32;
v2 = const.i32 5 : i32;
v3 = call noname::add(v1, v2) : i32;
br block1(v3);

block1(v0: i32):
ret v0;
block1(v0: i32):
ret v0;
}
}
"#]],
);
Expand All @@ -208,19 +212,20 @@ fn br() {
)
"#,
expect![[r#"
module noname
module noname {

pub fn main() -> i32 {
block0:
v1 = const.i32 0 : i32;
v2 = const.i32 3 : i32;
br block2;
pub fn main() -> i32 {
block0:
v1 = const.i32 0 : i32;
v2 = const.i32 3 : i32;
br block2;

block1(v0: i32):
ret v0;
block1(v0: i32):
ret v0;

block2:
br block1(v2);
block2:
br block1(v2);
}
}
"#]],
);
Expand Down Expand Up @@ -251,29 +256,30 @@ fn loop_br_if() {
)
"#,
expect![[r#"
module noname
module noname {

pub fn main() -> i32 {
block0:
v1 = const.i32 0 : i32;
v2 = const.i32 2 : i32;
br block2(v2, v1);
pub fn main() -> i32 {
block0:
v1 = const.i32 0 : i32;
v2 = const.i32 2 : i32;
br block2(v2, v1);

block1(v0: i32):
ret v0;
block1(v0: i32):
ret v0;

block2(v3: i32, v4: i32):
v5 = add.wrapping v3, v4 : i32;
v6 = const.i32 1 : i32;
v7 = sub.wrapping v3, v6 : i32;
v8 = neq v7, 0 : i1;
condbr v8, block2(v7, v5), block4;
block2(v3: i32, v4: i32):
v5 = add.wrapping v3, v4 : i32;
v6 = const.i32 1 : i32;
v7 = sub.wrapping v3, v6 : i32;
v8 = neq v7, 0 : i1;
condbr v8, block2(v7, v5), block4;

block3:
br block1(v5);
block3:
br block1(v5);

block4:
br block3;
block4:
br block3;
}
}
"#]],
);
Expand All @@ -295,27 +301,28 @@ fn if_then_else() {
)
"#,
expect![[r#"
module noname
module noname {

pub fn main() -> i32 {
block0:
v1 = const.i32 2 : i32;
v2 = neq v1, 0 : i1;
condbr v2, block2, block4;
pub fn main() -> i32 {
block0:
v1 = const.i32 2 : i32;
v2 = neq v1, 0 : i1;
condbr v2, block2, block4;

block1(v0: i32):
ret v0;
block1(v0: i32):
ret v0;

block2:
v4 = const.i32 3 : i32;
br block3(v4);
block2:
v4 = const.i32 3 : i32;
br block3(v4);

block3(v3: i32):
br block1(v3);
block3(v3: i32):
br block1(v3);

block4:
v5 = const.i32 5 : i32;
br block3(v5);
block4:
v5 = const.i32 5 : i32;
br block3(v5);
}
}
"#]],
);
Expand All @@ -336,23 +343,24 @@ fn global_var() {
)
"#,
expect![[r#"
module noname
module noname {

const $0 = 0x0000002a;
const $0 = 0x0000002a;

global external @MyGlobalVal : i32 = $0 { id = 0 };
global external @MyGlobalVal : i32 = $0 { id = 0 };

pub fn main() {
block0:
v0 = global.load (@MyGlobalVal) as *mut i8 : i32;
v1 = const.i32 9 : i32;
v2 = add.wrapping v0, v1 : i32;
v3 = global.symbol @MyGlobalVal : *mut i32;
store v3, v2;
br block1;
pub fn main() {
block0:
v0 = global.load (@MyGlobalVal) as *mut i8 : i32;
v1 = const.i32 9 : i32;
v2 = add.wrapping v0, v1 : i32;
v3 = global.symbol @MyGlobalVal : *mut i32;
store v3, v2;
br block1;

block1:
ret;
block1:
ret;
}
}
"#]],
);
Expand Down
Loading