Skip to content

Commit

Permalink
Merge pull request #131 from 0xPolygonMiden/greenhat/i109-basic-walle…
Browse files Browse the repository at this point in the history
…t-trans

[2/x] Translate basic wallet Wasm component
  • Loading branch information
bitwalker authored Mar 15, 2024
2 parents 0be6a64 + eb3c0c9 commit 9d62244
Show file tree
Hide file tree
Showing 34 changed files with 2,323 additions and 1,153 deletions.
1 change: 1 addition & 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',
Expand Down
76 changes: 32 additions & 44 deletions frontend-wasm/src/code_translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@
//!
//! Based on Cranelift's Wasm -> CLIF translator v11.0.0
use std::collections::hash_map;
use std::u64;
use std::{collections::hash_map, u64};

use crate::error::{WasmError, WasmResult};
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};
use crate::module::Module;
use crate::ssa::Variable;
use crate::unsupported_diag;
use miden_diagnostics::{DiagnosticsHandler, SourceSpan};
use miden_hir::cranelift_entity::packed_option::ReservedValue;
use miden_hir::Type::*;
use miden_hir::{Block, Inst, InstBuilder, Value};
use miden_hir::{Immediate, Type};
use miden_hir::{
cranelift_entity::packed_option::ReservedValue, Block, Immediate, Inst, InstBuilder, Type,
Type::*, Value,
};
use rustc_hash::FxHashMap;
use wasmparser::{MemArg, Operator};

use crate::{
error::{WasmError, WasmResult},
module::{
func_env::FuncEnvironment,
func_translation_state::{ControlStackFrame, ElseData, FuncTranslationState},
function_builder_ext::FunctionBuilderExt,
types::{ir_type, BlockType, FuncIndex, GlobalIndex, ModuleTypes},
Module,
},
ssa::Variable,
unsupported_diag,
};

#[cfg(test)]
mod tests;

Expand All @@ -44,6 +49,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 +127,14 @@ 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:
}
/******************************* Memory management *********************************/
Operator::MemoryGrow { .. } => {
let arg = state.pop1_casted(U32, builder, span);
Expand Down Expand Up @@ -624,25 +632,21 @@ fn prepare_addr(
.add_imm_checked(addr_u32, Immediate::U32(memarg.offset as u32), span);
}
};
builder
.ins()
.inttoptr(full_addr_int, Type::Ptr(ptr_ty.clone().into()), span)
builder.ins().inttoptr(full_addr_int, Type::Ptr(ptr_ty.clone().into()), span)
}

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 Expand Up @@ -718,9 +722,7 @@ fn translate_br_if(
let else_dest = next_block;
let else_args = &[];
let cond_i1 = builder.ins().neq_imm(cond, Immediate::I32(0), span);
builder
.ins()
.cond_br(cond_i1, then_dest, then_args, else_dest, else_args, span);
builder.ins().cond_br(cond_i1, then_dest, then_args, else_dest, else_args, span);
builder.seal_block(next_block); // The only predecessor is the current block.
builder.switch_to_block(next_block);
}
Expand Down Expand Up @@ -791,9 +793,7 @@ fn translate_end(
}

frame.truncate_value_stack_to_original_size(&mut state.stack);
state
.stack
.extend_from_slice(builder.block_params(next_block));
state.stack.extend_from_slice(builder.block_params(next_block));
}

fn translate_else(
Expand Down Expand Up @@ -840,9 +840,7 @@ fn translate_else(
else_block
}
ElseData::WithElse { else_block } => {
builder
.ins()
.br(destination, state.peekn(num_return_values), span);
builder.ins().br(destination, state.peekn(num_return_values), span);
state.popn(num_return_values);
else_block
}
Expand Down Expand Up @@ -920,13 +918,7 @@ fn translate_if(
};
builder.seal_block(next_block);
builder.switch_to_block(next_block);
state.push_if(
destination,
else_data,
blockty.params.len(),
blockty.results.len(),
blockty,
);
state.push_if(destination, else_data, blockty.params.len(), blockty.results.len(), blockty);
Ok(())
}

Expand All @@ -940,14 +932,10 @@ fn translate_loop(
let blockty = BlockType::from_wasm(blockty, mod_types)?;
let loop_body = builder.create_block_with_params(blockty.params.clone(), span);
let next = builder.create_block_with_params(blockty.results.clone(), span);
builder
.ins()
.br(loop_body, state.peekn(blockty.params.len()), span);
builder.ins().br(loop_body, state.peekn(blockty.params.len()), span);
state.push_loop(loop_body, next, blockty.params.len(), blockty.results.len());
state.popn(blockty.params.len());
state
.stack
.extend_from_slice(builder.block_params(loop_body));
state.stack.extend_from_slice(builder.block_params(loop_body));
builder.switch_to_block(loop_body);
Ok(())
}
Expand Down
51 changes: 19 additions & 32 deletions frontend-wasm/src/code_translator/tests_unsupported.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use miden_diagnostics::SourceSpan;
use miden_hir::CallConv;
use miden_hir::Linkage;
use miden_hir::ModuleBuilder;
use miden_hir::Signature;

use wasmparser::MemArg;
use wasmparser::Operator;
use wasmparser::Operator::*;

use crate::module::func_translation_state::FuncTranslationState;
use crate::module::function_builder_ext::FunctionBuilderContext;
use crate::module::function_builder_ext::FunctionBuilderExt;
use crate::module::Module;
use crate::test_utils::test_diagnostics;
use miden_hir::{CallConv, Linkage, ModuleBuilder, Signature};
use wasmparser::{MemArg, Operator, Operator::*};

use super::translate_operator;
use crate::{
module::{
func_env::FuncEnvironment,
func_translation_state::FuncTranslationState,
function_builder_ext::{FunctionBuilderContext, FunctionBuilderExt},
Module,
},
test_utils::test_diagnostics,
};

fn check_unsupported(op: &Operator) {
let diagnostics = test_diagnostics();
Expand All @@ -29,41 +26,31 @@ fn check_unsupported(op: &Operator) {
};
let mut module_func_builder = module_builder.function("func_name", sig.clone()).unwrap();
let mut fb_ctx = FunctionBuilderContext::new();
let mod_types = Default::default();
let func_env = FuncEnvironment::new(&module_info, &mod_types, vec![]);
let mut state = FuncTranslationState::new();
let mut builder_ext = FunctionBuilderExt::new(&mut module_func_builder, &mut fb_ctx);
let mod_types = Default::default();
let result = translate_operator(
op,
&mut builder_ext,
&mut state,
&module_info,
&mod_types,
&func_env,
&diagnostics,
SourceSpan::default(),
);
assert!(
result.is_err(),
"Expected unsupported op error for {:?}",
op
);
assert!(result.is_err(), "Expected unsupported op error for {:?}", op);
assert_eq!(
result.unwrap_err().to_string(),
format!("Unsupported Wasm: Wasm op {:?} is not supported", op)
);
assert!(
diagnostics.has_errors(),
"Expected diagnostics to have errors"
);
assert!(diagnostics.has_errors(), "Expected diagnostics to have errors");
}

// Wasm Spec v1.0
const UNSUPPORTED_WASM_V1_OPS: &[Operator] = &[
CallIndirect {
type_index: 0,
table_index: 0,
table_byte: 0,
},
/****************************** Memory Operators ************************************/
/****************************** Memory Operators *********************************** */
F32Load {
memarg: MemArg {
align: 0,
Expand Down Expand Up @@ -155,7 +142,7 @@ const UNSUPPORTED_WASM_V1_OPS: &[Operator] = &[
F64ReinterpretI64,
I32ReinterpretF32,
I64ReinterpretF64,
/****************************** Binary Operators ************************************/
/****************************** Binary Operators *********************************** */
F32Add,
F32Sub,
F32Mul,
Expand All @@ -169,7 +156,7 @@ const UNSUPPORTED_WASM_V1_OPS: &[Operator] = &[
F64Div,
F64Min,
F64Max,
/**************************** Comparison Operators **********************************/
/**************************** Comparison Operators ********************************* */
F32Eq,
F32Ne,
F32Gt,
Expand Down
Loading

0 comments on commit 9d62244

Please sign in to comment.