@@ -180,7 +180,8 @@ pub fn translate_operator(
180180 * `get_global` and `set_global` are handled by the environment.
181181 ***********************************************************************************/
182182 Operator :: GlobalGet { global_index } => {
183- let val = match state. get_global ( builder. func , * global_index, environ) ? {
183+ let global_index = GlobalIndex :: from_u32 ( * global_index) ;
184+ let val = match state. get_global ( builder. func , global_index, environ) ? {
184185 GlobalVariable :: Memory { gv, offset, ty } => {
185186 let addr = builder. ins ( ) . global_value ( environ. pointer_type ( ) , gv) ;
186187 let mut flags = ir:: MemFlags :: trusted ( ) ;
@@ -194,13 +195,15 @@ pub fn translate_operator(
194195 flags. set_alias_region ( Some ( ir:: AliasRegion :: Table ) ) ;
195196 builder. ins ( ) . load ( ty, flags, addr, offset)
196197 }
197- GlobalVariable :: Custom => environ
198- . translate_custom_global_get ( builder, GlobalIndex :: from_u32 ( * global_index) ) ?,
198+ GlobalVariable :: Custom => {
199+ environ. translate_custom_global_get ( builder, global_index) ?
200+ }
199201 } ;
200202 state. push1 ( val) ;
201203 }
202204 Operator :: GlobalSet { global_index } => {
203- match state. get_global ( builder. func , * global_index, environ) ? {
205+ let global_index = GlobalIndex :: from_u32 ( * global_index) ;
206+ match state. get_global ( builder. func , global_index, environ) ? {
204207 GlobalVariable :: Memory { gv, offset, ty } => {
205208 let addr = builder. ins ( ) . global_value ( environ. pointer_type ( ) , gv) ;
206209 let mut flags = ir:: MemFlags :: trusted ( ) ;
@@ -217,15 +220,11 @@ pub fn translate_operator(
217220 }
218221 debug_assert_eq ! ( ty, builder. func. dfg. value_type( val) ) ;
219222 builder. ins ( ) . store ( flags, val, addr, offset) ;
220- environ. update_global ( builder, * global_index, val) ;
223+ environ. update_global ( builder, global_index, val) ;
221224 }
222225 GlobalVariable :: Custom => {
223226 let val = state. pop1 ( ) ;
224- environ. translate_custom_global_set (
225- builder,
226- GlobalIndex :: from_u32 ( * global_index) ,
227- val,
228- ) ?;
227+ environ. translate_custom_global_set ( builder, global_index, val) ?;
229228 }
230229 }
231230 }
@@ -623,7 +622,8 @@ pub fn translate_operator(
623622 * argument referring to an index in the external functions table of the module.
624623 ************************************************************************************/
625624 Operator :: Call { function_index } => {
626- let ( fref, num_args) = state. get_direct_func ( builder. func , * function_index, environ) ?;
625+ let function_index = FuncIndex :: from_u32 ( * function_index) ;
626+ let ( fref, num_args) = state. get_direct_func ( builder. func , function_index, environ) ?;
627627
628628 // Bitcast any vector arguments to their default type, I8X16, before calling.
629629 let args = state. peekn_mut ( num_args) ;
@@ -634,12 +634,7 @@ pub fn translate_operator(
634634 builder,
635635 ) ;
636636
637- let call = environ. translate_call (
638- builder,
639- FuncIndex :: from_u32 ( * function_index) ,
640- fref,
641- args,
642- ) ?;
637+ let call = environ. translate_call ( builder, function_index, fref, args) ?;
643638 let inst_results = builder. inst_results ( call) ;
644639 debug_assert_eq ! (
645640 inst_results. len( ) ,
@@ -658,7 +653,8 @@ pub fn translate_operator(
658653 // `type_index` is the index of the function's signature and
659654 // `table_index` is the index of the table to search the function
660655 // in.
661- let ( sigref, num_args) = state. get_indirect_sig ( builder. func , * type_index, environ) ?;
656+ let type_index = TypeIndex :: from_u32 ( * type_index) ;
657+ let ( sigref, num_args) = state. get_indirect_sig ( builder. func , type_index, environ) ?;
662658 let callee = state. pop1 ( ) ;
663659
664660 // Bitcast any vector arguments to their default type, I8X16, before calling.
@@ -669,7 +665,7 @@ pub fn translate_operator(
669665 builder,
670666 validator. features ( ) ,
671667 TableIndex :: from_u32 ( * table_index) ,
672- TypeIndex :: from_u32 ( * type_index) ,
668+ type_index,
673669 sigref,
674670 callee,
675671 state. peekn ( num_args) ,
@@ -698,7 +694,8 @@ pub fn translate_operator(
698694 * VM's runtime state via tables.
699695 ************************************************************************************/
700696 Operator :: ReturnCall { function_index } => {
701- let ( fref, num_args) = state. get_direct_func ( builder. func , * function_index, environ) ?;
697+ let function_index = FuncIndex :: from_u32 ( * function_index) ;
698+ let ( fref, num_args) = state. get_direct_func ( builder. func , function_index, environ) ?;
702699
703700 // Bitcast any vector arguments to their default type, I8X16, before calling.
704701 let args = state. peekn_mut ( num_args) ;
@@ -709,12 +706,7 @@ pub fn translate_operator(
709706 builder,
710707 ) ;
711708
712- environ. translate_return_call (
713- builder,
714- FuncIndex :: from_u32 ( * function_index) ,
715- fref,
716- args,
717- ) ?;
709+ environ. translate_return_call ( builder, function_index, fref, args) ?;
718710
719711 state. popn ( num_args) ;
720712 state. reachable = false ;
@@ -726,7 +718,8 @@ pub fn translate_operator(
726718 // `type_index` is the index of the function's signature and
727719 // `table_index` is the index of the table to search the function
728720 // in.
729- let ( sigref, num_args) = state. get_indirect_sig ( builder. func , * type_index, environ) ?;
721+ let type_index = TypeIndex :: from_u32 ( * type_index) ;
722+ let ( sigref, num_args) = state. get_indirect_sig ( builder. func , type_index, environ) ?;
730723 let callee = state. pop1 ( ) ;
731724
732725 // Bitcast any vector arguments to their default type, I8X16, before calling.
@@ -737,7 +730,7 @@ pub fn translate_operator(
737730 builder,
738731 validator. features ( ) ,
739732 TableIndex :: from_u32 ( * table_index) ,
740- TypeIndex :: from_u32 ( * type_index) ,
733+ type_index,
741734 sigref,
742735 callee,
743736 state. peekn ( num_args) ,
@@ -750,7 +743,8 @@ pub fn translate_operator(
750743 // Get function signature
751744 // `index` is the index of the function's signature and `table_index` is the index of
752745 // the table to search the function in.
753- let ( sigref, num_args) = state. get_indirect_sig ( builder. func , * type_index, environ) ?;
746+ let type_index = TypeIndex :: from_u32 ( * type_index) ;
747+ let ( sigref, num_args) = state. get_indirect_sig ( builder. func , type_index, environ) ?;
754748 let callee = state. pop1 ( ) ;
755749
756750 // Bitcast any vector arguments to their default type, I8X16, before calling.
@@ -769,16 +763,16 @@ pub fn translate_operator(
769763 Operator :: MemoryGrow { mem } => {
770764 // The WebAssembly MVP only supports one linear memory, but we expect the reserved
771765 // argument to be a memory index.
772- let heap_index = MemoryIndex :: from_u32 ( * mem) ;
773- let heap = state. get_heap ( builder. func , * mem, environ) ?;
766+ let mem = MemoryIndex :: from_u32 ( * mem) ;
767+ let _heap = state. get_heap ( builder. func , mem, environ) ?;
774768 let val = state. pop1 ( ) ;
775- environ. before_memory_grow ( builder, val, heap_index ) ;
776- state. push1 ( environ. translate_memory_grow ( builder, heap_index , heap , val) ?)
769+ environ. before_memory_grow ( builder, val, mem ) ;
770+ state. push1 ( environ. translate_memory_grow ( builder, mem , val) ?)
777771 }
778772 Operator :: MemorySize { mem } => {
779- let heap_index = MemoryIndex :: from_u32 ( * mem) ;
780- let heap = state. get_heap ( builder. func , * mem, environ) ?;
781- state. push1 ( environ. translate_memory_size ( builder. cursor ( ) , heap_index , heap ) ?) ;
773+ let mem = MemoryIndex :: from_u32 ( * mem) ;
774+ let _heap = state. get_heap ( builder. func , mem, environ) ?;
775+ state. push1 ( environ. translate_memory_size ( builder. cursor ( ) , mem ) ?) ;
782776 }
783777 /******************************* Load instructions ***********************************
784778 * Wasm specifies an integer alignment flag but we drop it in Cranelift.
@@ -1282,8 +1276,8 @@ pub fn translate_operator(
12821276 Operator :: MemoryAtomicWait32 { .. } => I32 ,
12831277 _ => unreachable ! ( ) ,
12841278 } ;
1285- let heap_index = MemoryIndex :: from_u32 ( memarg. memory ) ;
1286- let heap = state. get_heap ( builder. func , memarg . memory , environ) ?;
1279+ let memory_index = MemoryIndex :: from_u32 ( memarg. memory ) ;
1280+ let heap = state. get_heap ( builder. func , memory_index , environ) ?;
12871281 let timeout = state. pop1 ( ) ; // 64 (fixed)
12881282 let expected = state. pop1 ( ) ; // 32 or 64 (per the `Ixx` in `IxxAtomicWait`)
12891283 assert ! ( builder. func. dfg. value_type( expected) == implied_ty) ;
@@ -1299,7 +1293,7 @@ pub fn translate_operator(
12991293 // code it needs to generate, if it wants.
13001294 let res = environ. translate_atomic_wait (
13011295 builder,
1302- heap_index ,
1296+ memory_index ,
13031297 heap,
13041298 effective_addr,
13051299 expected,
@@ -1308,8 +1302,8 @@ pub fn translate_operator(
13081302 state. push1 ( res) ;
13091303 }
13101304 Operator :: MemoryAtomicNotify { memarg } => {
1311- let heap_index = MemoryIndex :: from_u32 ( memarg. memory ) ;
1312- let heap = state. get_heap ( builder. func , memarg . memory , environ) ?;
1305+ let memory_index = MemoryIndex :: from_u32 ( memarg. memory ) ;
1306+ let heap = state. get_heap ( builder. func , memory_index , environ) ?;
13131307 let count = state. pop1 ( ) ; // 32 (fixed)
13141308 let addr = state. pop1 ( ) ;
13151309 let effective_addr = if memarg. offset == 0 {
@@ -1321,7 +1315,7 @@ pub fn translate_operator(
13211315 } ;
13221316 let res = environ. translate_atomic_notify (
13231317 builder,
1324- heap_index ,
1318+ memory_index ,
13251319 heap,
13261320 effective_addr,
13271321 count,
@@ -1531,39 +1525,31 @@ pub fn translate_operator(
15311525 }
15321526 Operator :: MemoryCopy { src_mem, dst_mem } => {
15331527 let src_index = MemoryIndex :: from_u32 ( * src_mem) ;
1528+ let _src_heap = state. get_heap ( builder. func , src_index, environ) ?;
1529+
15341530 let dst_index = MemoryIndex :: from_u32 ( * dst_mem) ;
1535- let src_heap = state. get_heap ( builder. func , * src_mem , environ) ?;
1536- let dst_heap = state . get_heap ( builder . func , * dst_mem , environ ) ? ;
1531+ let _dst_heap = state. get_heap ( builder. func , dst_index , environ) ?;
1532+
15371533 let len = state. pop1 ( ) ;
15381534 let src_pos = state. pop1 ( ) ;
15391535 let dst_pos = state. pop1 ( ) ;
1540- environ. translate_memory_copy (
1541- builder, src_index, src_heap, dst_index, dst_heap, dst_pos, src_pos, len,
1542- ) ?;
1536+ environ. translate_memory_copy ( builder, src_index, dst_index, dst_pos, src_pos, len) ?;
15431537 }
15441538 Operator :: MemoryFill { mem } => {
1545- let heap_index = MemoryIndex :: from_u32 ( * mem) ;
1546- let heap = state. get_heap ( builder. func , * mem, environ) ?;
1539+ let mem = MemoryIndex :: from_u32 ( * mem) ;
1540+ let _heap = state. get_heap ( builder. func , mem, environ) ?;
15471541 let len = state. pop1 ( ) ;
15481542 let val = state. pop1 ( ) ;
15491543 let dest = state. pop1 ( ) ;
1550- environ. translate_memory_fill ( builder, heap_index , heap , dest, val, len) ?;
1544+ environ. translate_memory_fill ( builder, mem , dest, val, len) ?;
15511545 }
15521546 Operator :: MemoryInit { data_index, mem } => {
1553- let heap_index = MemoryIndex :: from_u32 ( * mem) ;
1554- let heap = state. get_heap ( builder. func , * mem, environ) ?;
1547+ let mem = MemoryIndex :: from_u32 ( * mem) ;
1548+ let _heap = state. get_heap ( builder. func , mem, environ) ?;
15551549 let len = state. pop1 ( ) ;
15561550 let src = state. pop1 ( ) ;
15571551 let dest = state. pop1 ( ) ;
1558- environ. translate_memory_init (
1559- builder,
1560- heap_index,
1561- heap,
1562- * data_index,
1563- dest,
1564- src,
1565- len,
1566- ) ?;
1552+ environ. translate_memory_init ( builder, mem, * data_index, dest, src, len) ?;
15671553 }
15681554 Operator :: DataDrop { data_index } => {
15691555 environ. translate_data_drop ( builder. cursor ( ) , * data_index) ?;
@@ -2498,7 +2484,8 @@ pub fn translate_operator(
24982484 // Get function signature
24992485 // `index` is the index of the function's signature and `table_index` is the index of
25002486 // the table to search the function in.
2501- let ( sigref, num_args) = state. get_indirect_sig ( builder. func , * type_index, environ) ?;
2487+ let type_index = TypeIndex :: from_u32 ( * type_index) ;
2488+ let ( sigref, num_args) = state. get_indirect_sig ( builder. func , type_index, environ) ?;
25022489 let callee = state. pop1 ( ) ;
25032490
25042491 // Bitcast any vector arguments to their default type, I8X16, before calling.
@@ -3224,7 +3211,9 @@ fn prepare_addr(
32243211 environ : & mut FuncEnvironment < ' _ > ,
32253212) -> WasmResult < Reachability < ( MemFlags , Value , Value ) > > {
32263213 let index = state. pop1 ( ) ;
3227- let heap = state. get_heap ( builder. func , memarg. memory , environ) ?;
3214+
3215+ let memory_index = MemoryIndex :: from_u32 ( memarg. memory ) ;
3216+ let heap = state. get_heap ( builder. func , memory_index, environ) ?;
32283217
32293218 // How exactly the bounds check is performed here and what it's performed
32303219 // on is a bit tricky. Generally we want to rely on access violations (e.g.
0 commit comments