@@ -46,7 +46,14 @@ pub fn generate_instruction_with_info<B: Backend>(
4646 let has_globals = info. has_mutable_globals ( ) ;
4747 let has_memory = info. has_memory ;
4848 let has_table = info. has_table ( ) ;
49- backend. emit_call ( * dest, * func_idx, args, has_globals, has_memory, has_table)
49+ backend. emit_call (
50+ * dest,
51+ func_idx. as_usize ( ) ,
52+ args,
53+ has_globals,
54+ has_memory,
55+ has_table,
56+ )
5057 }
5158
5259 IrInstr :: CallImport {
@@ -62,38 +69,26 @@ pub fn generate_instruction_with_info<B: Backend>(
6269 type_idx,
6370 table_idx,
6471 args,
65- } => generate_call_indirect ( * dest, * type_idx, * table_idx, args, info) ,
72+ } => generate_call_indirect ( * dest, type_idx. clone ( ) , * table_idx, args, info) ,
6673
6774 IrInstr :: Assign { dest, src } => backend. emit_assign ( * dest, * src) ,
6875
69- IrInstr :: GlobalGet { dest, index } => {
70- if * index < info. imported_globals . len ( ) {
71- // Imported global — access via host trait getter
72- let g = & info. imported_globals [ * index] ;
76+ IrInstr :: GlobalGet { dest, index } => match info. resolve_global ( * index) {
77+ ResolvedGlobal :: Imported ( _idx, g) => {
7378 format ! ( " {} = host.get_{}();" , dest, g. name)
74- } else {
75- // Local global — use corrected index and backend
76- let local_idx = index - info. imported_globals . len ( ) ;
77- let is_mutable = info
78- . globals
79- . get ( local_idx)
80- . map ( |g| g. mutable )
81- . unwrap_or ( true ) ;
82- backend. emit_global_get ( * dest, local_idx, is_mutable)
8379 }
84- }
80+ ResolvedGlobal :: Local ( idx, g) => {
81+ let is_mutable = g. mutable ;
82+ backend. emit_global_get ( * dest, idx. as_usize ( ) , is_mutable)
83+ }
84+ } ,
8585
86- IrInstr :: GlobalSet { index, value } => {
87- if * index < info. imported_globals . len ( ) {
88- // Imported global — access via host trait setter
89- let g = & info. imported_globals [ * index] ;
86+ IrInstr :: GlobalSet { index, value } => match info. resolve_global ( * index) {
87+ ResolvedGlobal :: Imported ( _idx, g) => {
9088 format ! ( " host.set_{}({});" , g. name, value)
91- } else {
92- // Local global — use corrected index and backend
93- let local_idx = index - info. imported_globals . len ( ) ;
94- backend. emit_global_set ( local_idx, * value)
9589 }
96- }
90+ ResolvedGlobal :: Local ( idx, _g) => backend. emit_global_set ( idx. as_usize ( ) , * value) ,
91+ } ,
9792
9893 IrInstr :: MemorySize { dest } => backend. emit_memory_size ( * dest) ,
9994
@@ -163,7 +158,7 @@ pub fn generate_terminator_with_mapping<B: Backend>(
163158/// 3. Dispatches to the matching function via a match on func_index
164159fn generate_call_indirect (
165160 dest : Option < VarId > ,
166- type_idx : usize ,
161+ type_idx : TypeIdx ,
167162 table_idx : VarId ,
168163 args : & [ VarId ] ,
169164 info : & ModuleInfo ,
@@ -174,11 +169,12 @@ fn generate_call_indirect(
174169
175170 // Canonicalize the type index for structural equivalence (Wasm spec §4.4.9).
176171 // Two different type indices with identical (params, results) must match.
172+ let type_idx_usize = type_idx. as_usize ( ) ;
177173 let canon_idx = info
178174 . canonical_type
179- . get ( type_idx )
175+ . get ( type_idx_usize )
180176 . copied ( )
181- . unwrap_or ( type_idx ) ;
177+ . unwrap_or ( type_idx_usize ) ;
182178
183179 let mut code = String :: new ( ) ;
184180
@@ -218,7 +214,7 @@ fn generate_call_indirect(
218214 ) ) ;
219215
220216 for ( func_idx, ir_func) in info. ir_functions . iter ( ) . enumerate ( ) {
221- if ir_func. type_idx == canon_idx {
217+ if ir_func. type_idx . as_usize ( ) == canon_idx {
222218 code. push_str ( & format ! (
223219 " {} => func_{}({})?,\n " ,
224220 func_idx, func_idx, args_str
0 commit comments