Skip to content

Commit 9d62244

Browse files
authored
Merge pull request #131 from 0xPolygonMiden/greenhat/i109-basic-wallet-trans
[2/x] Translate basic wallet Wasm component
2 parents 0be6a64 + eb3c0c9 commit 9d62244

34 files changed

+2323
-1153
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ smallstr = { version = "0.3", features = ["union"] }
6262
thiserror = "1.0"
6363
toml = { version = "0.5", features = ["preserve_order"] }
6464
derive_more = "0.99"
65+
indexmap = "2.1"
6566
# 211152c631d16a943aae503466b198b93c61150f is latest (as of Jan 25th) commit in the next branch
6667
miden-assembly = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "211152c631d16a943aae503466b198b93c61150f" }
6768
miden-core = { git = "https://github.com/0xPolygonMiden/miden-vm", rev = "211152c631d16a943aae503466b198b93c61150f" }

frontend-wasm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ log.workspace = true
2222
anyhow.workspace = true
2323
wasmparser = "0.118.1"
2424
derive_more.workspace = true
25-
indexmap = "2.1"
25+
indexmap.workspace = true
2626
gimli = { version = "0.28.0", default-features = false, features = [
2727
'read',
2828
'std',

frontend-wasm/src/code_translator/mod.rs

+32-44
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,29 @@
1313
//!
1414
//! Based on Cranelift's Wasm -> CLIF translator v11.0.0
1515
16-
use std::collections::hash_map;
17-
use std::u64;
16+
use std::{collections::hash_map, u64};
1817

19-
use crate::error::{WasmError, WasmResult};
20-
use crate::module::func_translation_state::{ControlStackFrame, ElseData, FuncTranslationState};
21-
use crate::module::function_builder_ext::FunctionBuilderExt;
22-
use crate::module::types::{ir_type, BlockType, FuncIndex, GlobalIndex, ModuleTypes};
23-
use crate::module::Module;
24-
use crate::ssa::Variable;
25-
use crate::unsupported_diag;
2618
use miden_diagnostics::{DiagnosticsHandler, SourceSpan};
27-
use miden_hir::cranelift_entity::packed_option::ReservedValue;
28-
use miden_hir::Type::*;
29-
use miden_hir::{Block, Inst, InstBuilder, Value};
30-
use miden_hir::{Immediate, Type};
19+
use miden_hir::{
20+
cranelift_entity::packed_option::ReservedValue, Block, Immediate, Inst, InstBuilder, Type,
21+
Type::*, Value,
22+
};
3123
use rustc_hash::FxHashMap;
3224
use wasmparser::{MemArg, Operator};
3325

26+
use crate::{
27+
error::{WasmError, WasmResult},
28+
module::{
29+
func_env::FuncEnvironment,
30+
func_translation_state::{ControlStackFrame, ElseData, FuncTranslationState},
31+
function_builder_ext::FunctionBuilderExt,
32+
types::{ir_type, BlockType, FuncIndex, GlobalIndex, ModuleTypes},
33+
Module,
34+
},
35+
ssa::Variable,
36+
unsupported_diag,
37+
};
38+
3439
#[cfg(test)]
3540
mod tests;
3641

@@ -44,6 +49,7 @@ pub fn translate_operator(
4449
state: &mut FuncTranslationState,
4550
module: &Module,
4651
mod_types: &ModuleTypes,
52+
func_env: &FuncEnvironment,
4753
diagnostics: &DiagnosticsHandler,
4854
span: SourceSpan,
4955
) -> WasmResult<()> {
@@ -121,12 +127,14 @@ pub fn translate_operator(
121127
state,
122128
builder,
123129
FuncIndex::from_u32(*function_index),
124-
module,
125-
mod_types,
130+
func_env,
126131
span,
127132
diagnostics,
128133
)?;
129134
}
135+
Operator::CallIndirect { type_index: _, table_index: _, table_byte: _ } => {
136+
// TODO:
137+
}
130138
/******************************* Memory management *********************************/
131139
Operator::MemoryGrow { .. } => {
132140
let arg = state.pop1_casted(U32, builder, span);
@@ -624,25 +632,21 @@ fn prepare_addr(
624632
.add_imm_checked(addr_u32, Immediate::U32(memarg.offset as u32), span);
625633
}
626634
};
627-
builder
628-
.ins()
629-
.inttoptr(full_addr_int, Type::Ptr(ptr_ty.clone().into()), span)
635+
builder.ins().inttoptr(full_addr_int, Type::Ptr(ptr_ty.clone().into()), span)
630636
}
631637

632638
fn translate_call(
633639
state: &mut FuncTranslationState,
634640
builder: &mut FunctionBuilderExt,
635641
function_index: FuncIndex,
636-
module: &Module,
637-
mod_types: &ModuleTypes,
642+
func_env: &FuncEnvironment,
638643
span: SourceSpan,
639644
diagnostics: &DiagnosticsHandler,
640645
) -> WasmResult<()> {
641646
let (fident, num_args) = state.get_direct_func(
642647
builder.data_flow_graph_mut(),
643648
function_index,
644-
module,
645-
mod_types,
649+
func_env,
646650
diagnostics,
647651
)?;
648652
let args = state.peekn_mut(num_args);
@@ -718,9 +722,7 @@ fn translate_br_if(
718722
let else_dest = next_block;
719723
let else_args = &[];
720724
let cond_i1 = builder.ins().neq_imm(cond, Immediate::I32(0), span);
721-
builder
722-
.ins()
723-
.cond_br(cond_i1, then_dest, then_args, else_dest, else_args, span);
725+
builder.ins().cond_br(cond_i1, then_dest, then_args, else_dest, else_args, span);
724726
builder.seal_block(next_block); // The only predecessor is the current block.
725727
builder.switch_to_block(next_block);
726728
}
@@ -791,9 +793,7 @@ fn translate_end(
791793
}
792794

793795
frame.truncate_value_stack_to_original_size(&mut state.stack);
794-
state
795-
.stack
796-
.extend_from_slice(builder.block_params(next_block));
796+
state.stack.extend_from_slice(builder.block_params(next_block));
797797
}
798798

799799
fn translate_else(
@@ -840,9 +840,7 @@ fn translate_else(
840840
else_block
841841
}
842842
ElseData::WithElse { else_block } => {
843-
builder
844-
.ins()
845-
.br(destination, state.peekn(num_return_values), span);
843+
builder.ins().br(destination, state.peekn(num_return_values), span);
846844
state.popn(num_return_values);
847845
else_block
848846
}
@@ -920,13 +918,7 @@ fn translate_if(
920918
};
921919
builder.seal_block(next_block);
922920
builder.switch_to_block(next_block);
923-
state.push_if(
924-
destination,
925-
else_data,
926-
blockty.params.len(),
927-
blockty.results.len(),
928-
blockty,
929-
);
921+
state.push_if(destination, else_data, blockty.params.len(), blockty.results.len(), blockty);
930922
Ok(())
931923
}
932924

@@ -940,14 +932,10 @@ fn translate_loop(
940932
let blockty = BlockType::from_wasm(blockty, mod_types)?;
941933
let loop_body = builder.create_block_with_params(blockty.params.clone(), span);
942934
let next = builder.create_block_with_params(blockty.results.clone(), span);
943-
builder
944-
.ins()
945-
.br(loop_body, state.peekn(blockty.params.len()), span);
935+
builder.ins().br(loop_body, state.peekn(blockty.params.len()), span);
946936
state.push_loop(loop_body, next, blockty.params.len(), blockty.results.len());
947937
state.popn(blockty.params.len());
948-
state
949-
.stack
950-
.extend_from_slice(builder.block_params(loop_body));
938+
state.stack.extend_from_slice(builder.block_params(loop_body));
951939
builder.switch_to_block(loop_body);
952940
Ok(())
953941
}

frontend-wasm/src/code_translator/tests_unsupported.rs

+19-32
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
use miden_diagnostics::SourceSpan;
2-
use miden_hir::CallConv;
3-
use miden_hir::Linkage;
4-
use miden_hir::ModuleBuilder;
5-
use miden_hir::Signature;
6-
7-
use wasmparser::MemArg;
8-
use wasmparser::Operator;
9-
use wasmparser::Operator::*;
10-
11-
use crate::module::func_translation_state::FuncTranslationState;
12-
use crate::module::function_builder_ext::FunctionBuilderContext;
13-
use crate::module::function_builder_ext::FunctionBuilderExt;
14-
use crate::module::Module;
15-
use crate::test_utils::test_diagnostics;
2+
use miden_hir::{CallConv, Linkage, ModuleBuilder, Signature};
3+
use wasmparser::{MemArg, Operator, Operator::*};
164

175
use super::translate_operator;
6+
use crate::{
7+
module::{
8+
func_env::FuncEnvironment,
9+
func_translation_state::FuncTranslationState,
10+
function_builder_ext::{FunctionBuilderContext, FunctionBuilderExt},
11+
Module,
12+
},
13+
test_utils::test_diagnostics,
14+
};
1815

1916
fn check_unsupported(op: &Operator) {
2017
let diagnostics = test_diagnostics();
@@ -29,41 +26,31 @@ fn check_unsupported(op: &Operator) {
2926
};
3027
let mut module_func_builder = module_builder.function("func_name", sig.clone()).unwrap();
3128
let mut fb_ctx = FunctionBuilderContext::new();
29+
let mod_types = Default::default();
30+
let func_env = FuncEnvironment::new(&module_info, &mod_types, vec![]);
3231
let mut state = FuncTranslationState::new();
3332
let mut builder_ext = FunctionBuilderExt::new(&mut module_func_builder, &mut fb_ctx);
34-
let mod_types = Default::default();
3533
let result = translate_operator(
3634
op,
3735
&mut builder_ext,
3836
&mut state,
3937
&module_info,
4038
&mod_types,
39+
&func_env,
4140
&diagnostics,
4241
SourceSpan::default(),
4342
);
44-
assert!(
45-
result.is_err(),
46-
"Expected unsupported op error for {:?}",
47-
op
48-
);
43+
assert!(result.is_err(), "Expected unsupported op error for {:?}", op);
4944
assert_eq!(
5045
result.unwrap_err().to_string(),
5146
format!("Unsupported Wasm: Wasm op {:?} is not supported", op)
5247
);
53-
assert!(
54-
diagnostics.has_errors(),
55-
"Expected diagnostics to have errors"
56-
);
48+
assert!(diagnostics.has_errors(), "Expected diagnostics to have errors");
5749
}
5850

5951
// Wasm Spec v1.0
6052
const UNSUPPORTED_WASM_V1_OPS: &[Operator] = &[
61-
CallIndirect {
62-
type_index: 0,
63-
table_index: 0,
64-
table_byte: 0,
65-
},
66-
/****************************** Memory Operators ************************************/
53+
/****************************** Memory Operators *********************************** */
6754
F32Load {
6855
memarg: MemArg {
6956
align: 0,
@@ -155,7 +142,7 @@ const UNSUPPORTED_WASM_V1_OPS: &[Operator] = &[
155142
F64ReinterpretI64,
156143
I32ReinterpretF32,
157144
I64ReinterpretF64,
158-
/****************************** Binary Operators ************************************/
145+
/****************************** Binary Operators *********************************** */
159146
F32Add,
160147
F32Sub,
161148
F32Mul,
@@ -169,7 +156,7 @@ const UNSUPPORTED_WASM_V1_OPS: &[Operator] = &[
169156
F64Div,
170157
F64Min,
171158
F64Max,
172-
/**************************** Comparison Operators **********************************/
159+
/**************************** Comparison Operators ********************************* */
173160
F32Eq,
174161
F32Ne,
175162
F32Gt,

0 commit comments

Comments
 (0)