13
13
//!
14
14
//! Based on Cranelift's Wasm -> CLIF translator v11.0.0
15
15
16
- use std:: collections:: hash_map;
17
- use std:: u64;
16
+ use std:: { collections:: hash_map, u64} ;
18
17
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;
26
18
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
+ } ;
31
23
use rustc_hash:: FxHashMap ;
32
24
use wasmparser:: { MemArg , Operator } ;
33
25
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
+
34
39
#[ cfg( test) ]
35
40
mod tests;
36
41
@@ -44,6 +49,7 @@ pub fn translate_operator(
44
49
state : & mut FuncTranslationState ,
45
50
module : & Module ,
46
51
mod_types : & ModuleTypes ,
52
+ func_env : & FuncEnvironment ,
47
53
diagnostics : & DiagnosticsHandler ,
48
54
span : SourceSpan ,
49
55
) -> WasmResult < ( ) > {
@@ -121,12 +127,14 @@ pub fn translate_operator(
121
127
state,
122
128
builder,
123
129
FuncIndex :: from_u32 ( * function_index) ,
124
- module,
125
- mod_types,
130
+ func_env,
126
131
span,
127
132
diagnostics,
128
133
) ?;
129
134
}
135
+ Operator :: CallIndirect { type_index : _, table_index : _, table_byte : _ } => {
136
+ // TODO:
137
+ }
130
138
/******************************* Memory management *********************************/
131
139
Operator :: MemoryGrow { .. } => {
132
140
let arg = state. pop1_casted ( U32 , builder, span) ;
@@ -624,25 +632,21 @@ fn prepare_addr(
624
632
. add_imm_checked ( addr_u32, Immediate :: U32 ( memarg. offset as u32 ) , span) ;
625
633
}
626
634
} ;
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)
630
636
}
631
637
632
638
fn translate_call (
633
639
state : & mut FuncTranslationState ,
634
640
builder : & mut FunctionBuilderExt ,
635
641
function_index : FuncIndex ,
636
- module : & Module ,
637
- mod_types : & ModuleTypes ,
642
+ func_env : & FuncEnvironment ,
638
643
span : SourceSpan ,
639
644
diagnostics : & DiagnosticsHandler ,
640
645
) -> WasmResult < ( ) > {
641
646
let ( fident, num_args) = state. get_direct_func (
642
647
builder. data_flow_graph_mut ( ) ,
643
648
function_index,
644
- module,
645
- mod_types,
649
+ func_env,
646
650
diagnostics,
647
651
) ?;
648
652
let args = state. peekn_mut ( num_args) ;
@@ -718,9 +722,7 @@ fn translate_br_if(
718
722
let else_dest = next_block;
719
723
let else_args = & [ ] ;
720
724
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) ;
724
726
builder. seal_block ( next_block) ; // The only predecessor is the current block.
725
727
builder. switch_to_block ( next_block) ;
726
728
}
@@ -791,9 +793,7 @@ fn translate_end(
791
793
}
792
794
793
795
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) ) ;
797
797
}
798
798
799
799
fn translate_else (
@@ -840,9 +840,7 @@ fn translate_else(
840
840
else_block
841
841
}
842
842
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) ;
846
844
state. popn ( num_return_values) ;
847
845
else_block
848
846
}
@@ -920,13 +918,7 @@ fn translate_if(
920
918
} ;
921
919
builder. seal_block ( next_block) ;
922
920
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) ;
930
922
Ok ( ( ) )
931
923
}
932
924
@@ -940,14 +932,10 @@ fn translate_loop(
940
932
let blockty = BlockType :: from_wasm ( blockty, mod_types) ?;
941
933
let loop_body = builder. create_block_with_params ( blockty. params . clone ( ) , span) ;
942
934
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) ;
946
936
state. push_loop ( loop_body, next, blockty. params . len ( ) , blockty. results . len ( ) ) ;
947
937
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) ) ;
951
939
builder. switch_to_block ( loop_body) ;
952
940
Ok ( ( ) )
953
941
}
0 commit comments