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;
2618use 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+ } ;
3123use rustc_hash:: FxHashMap ;
3224use 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) ]
3540mod 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
632638fn 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
799799fn 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}
0 commit comments