@@ -6,6 +6,17 @@ use alloc::{boxed::Box, format, vec::Vec};
66use tinywasm_types:: * ;
77use wasmparser:: { CompositeInnerType , OperatorsReader , OperatorsReaderAllocations , UnpackedIndex } ;
88
9+ /// Function identity and result layout shared by lowering and finalization.
10+ #[ derive( Clone , Copy ) ]
11+ pub ( crate ) struct FunctionLoweringContext {
12+ /// Module type index of the function signature.
13+ pub ( crate ) ty_idx : u32 ,
14+ /// Module function index, including imported functions.
15+ pub ( crate ) self_func_addr : u32 ,
16+ /// Result counts in each physical value lane.
17+ pub ( crate ) function_results : ValueCounts ,
18+ }
19+
920pub ( crate ) fn value_lane ( ty : wasmparser:: ValType ) -> ValueLane {
1021 match ty {
1122 wasmparser:: ValType :: I32 | wasmparser:: ValType :: F32 | wasmparser:: ValType :: Ref ( _) => ValueLane :: S32 ,
@@ -148,11 +159,11 @@ pub(crate) fn convert_module_code(
148159 validator : Option < FuncValidator < ValidatorResources > > ,
149160 reader_allocs : OperatorsReaderAllocations ,
150161 metadata : & crate :: visit:: ModuleMetadata ,
151- ty_idx : u32 ,
162+ context : FunctionLoweringContext ,
152163 options : & ParserOptions ,
153164) -> Result < ( FunctionCode , Option < FuncValidatorAllocations > , OperatorsReaderAllocations ) > {
154165 let mut locals_reader = func. get_locals_reader ( ) ?;
155- let mut local_types = metadata. signature ( ty_idx) ?. params . clone ( ) ;
166+ let mut local_types = metadata. signature ( context . ty_idx ) ?. params . clone ( ) ;
156167
157168 #[ cfg( feature = "validate" ) ]
158169 let mut validator = validator;
@@ -193,39 +204,27 @@ pub(crate) fn convert_module_code(
193204 func,
194205 ( local_types, local_addr_map) ,
195206 metadata,
196- ty_idx ,
207+ context ,
197208 reader_allocs,
198- options. deduplicate_operands ( ) ,
209+ options,
199210 ) ?;
200211 ( body, data, uses_local_memory, Some ( validator_allocs) , reader_allocs)
201212 }
202213 None => {
203- let ( body, data, uses_local_memory, reader_allocs) = process_operators (
204- func,
205- ( local_types, local_addr_map) ,
206- metadata,
207- ty_idx,
208- reader_allocs,
209- options. deduplicate_operands ( ) ,
210- ) ?;
214+ let ( body, data, uses_local_memory, reader_allocs) =
215+ process_operators ( func, ( local_types, local_addr_map) , metadata, context, reader_allocs, options) ?;
211216 ( body, data, uses_local_memory, None , reader_allocs)
212217 }
213218 } ;
214219 #[ cfg( not( feature = "validate" ) ) ]
215220 let ( body, data, uses_local_memory, validator_allocs, reader_allocs) = {
216221 let _ = validator;
217- let ( body, data, uses_local_memory, reader_allocs) = process_operators (
218- func,
219- ( local_types, local_addr_map) ,
220- metadata,
221- ty_idx,
222- reader_allocs,
223- options. deduplicate_operands ( ) ,
224- ) ?;
222+ let ( body, data, uses_local_memory, reader_allocs) =
223+ process_operators ( func, ( local_types, local_addr_map) , metadata, context, reader_allocs, options) ?;
225224 ( body, data, uses_local_memory, None , reader_allocs)
226225 } ;
227226 Ok ( (
228- FunctionCode { instructions : body, data, locals : local_counts, uses_local_memory } ,
227+ FunctionCode { instructions : body, data : data . finish ( ) , locals : local_counts, uses_local_memory } ,
229228 validator_allocs,
230229 reader_allocs,
231230 ) )
0 commit comments