11use std:: path:: PathBuf ;
22
33use anyhow:: Result ;
4- use orca_wasm:: ir:: function:: { FunctionBuilder , FunctionModifier } ;
5- use orca_wasm:: ir:: id:: { ExportsID , FunctionID , LocalID } ;
6- use orca_wasm:: ir:: module:: Module ;
7- use orca_wasm:: ir:: types:: { BlockType , ElementItems , InstrumentationMode } ;
8- use orca_wasm:: module_builder:: AddLocal ;
9- use orca_wasm:: opcode:: { Inject , InjectAt } ;
10- use orca_wasm:: { DataType , Opcode } ;
114use wasm_encoder:: { Encode , Section } ;
125use wasmparser:: ExternalKind ;
136use wasmparser:: MemArg ;
147use wasmparser:: Operator ;
8+ use wirm:: ir:: function:: { FunctionBuilder , FunctionModifier } ;
9+ use wirm:: ir:: id:: { ExportsID , FunctionID , LocalID } ;
10+ use wirm:: ir:: module:: Module ;
11+ use wirm:: ir:: types:: { BlockType , ElementItems , InstrumentationMode } ;
12+ use wirm:: module_builder:: AddLocal ;
13+ use wirm:: opcode:: { Inject , InjectAt } ;
14+ use wirm:: { DataType , Opcode } ;
1515use wit_component:: metadata:: { decode, Bindgen } ;
1616use wit_component:: StringEncoding ;
1717use wit_parser:: Resolve ;
@@ -725,7 +725,7 @@ fn synthesize_import_functions(
725725
726726 // create imported function table
727727 let els = module. elements . iter_mut ( ) . next ( ) . unwrap ( ) ;
728- if let ElementItems :: Functions ( ref mut funcs) = & mut els. 1 {
728+ if let ElementItems :: Functions ( ref mut funcs) = & mut els. items {
729729 for fid in import_fnids {
730730 funcs. push ( fid) ;
731731 }
@@ -757,20 +757,25 @@ fn synthesize_import_functions(
757757 . get_fn_modifier ( coreabi_get_import_fid)
758758 . unwrap ( ) ;
759759
760- // walk until we get to the const representing the table index
761- let mut table_instr_idx = 0 ;
762- for ( idx, instr) in builder. body . instructions . iter_mut ( ) . enumerate ( ) {
763- if let Operator :: I32Const { value : ref mut v } = instr. op {
764- // we specifically need the const "around" 3393
765- // which is the coreabi_sample_i32 table offset
766- if * v < 1000 || * v > 5000 {
767- continue ;
760+ // Find the I32Const base index and compute the delta to new base
761+ let mut table_instr_idx = 0usize ;
762+ let mut delta: i32 = 0 ;
763+ {
764+ let ops_ro = builder. body . instructions . get_ops ( ) ;
765+ for ( idx, op) in ops_ro. iter ( ) . enumerate ( ) {
766+ if let Operator :: I32Const { value } = op {
767+ // we specifically need the const "around" 3393
768+ // which is the coreabi_sample_i32 table offset
769+ if * value < 1000 || * value > 5000 {
770+ continue ;
771+ }
772+ delta = import_fn_table_start_idx - * value;
773+ table_instr_idx = idx;
774+ break ;
768775 }
769- * v = import_fn_table_start_idx;
770- table_instr_idx = idx;
771- break ;
772776 }
773777 }
778+
774779 builder. inject_at (
775780 table_instr_idx,
776781 InstrumentationMode :: Before ,
@@ -783,6 +788,17 @@ fn synthesize_import_functions(
783788 InstrumentationMode :: Before ,
784789 Operator :: I32Add ,
785790 ) ;
791+
792+ if delta != 0 {
793+ builder
794+ . body
795+ . instructions
796+ . add_instr ( table_instr_idx, Operator :: I32Const { value : delta } ) ;
797+ builder
798+ . body
799+ . instructions
800+ . add_instr ( table_instr_idx, Operator :: I32Add ) ;
801+ }
786802 }
787803
788804 // remove unnecessary exports
0 commit comments