@@ -26,6 +26,7 @@ use crate::{
26
26
module:: { FuncIdx , ModuleHeader , WasmiValueType } ,
27
27
Engine ,
28
28
Error ,
29
+ FuncType ,
29
30
} ;
30
31
use wasmparser:: WasmFeatures ;
31
32
@@ -146,23 +147,12 @@ impl FuncTranslator {
146
147
. then ( || config. fuel_costs ( ) )
147
148
. cloned ( ) ;
148
149
let FuncTranslatorAllocations {
149
- mut stack,
150
+ stack,
150
151
layout,
151
- mut labels,
152
- mut instrs,
153
- } = {
154
- let mut alloc = alloc;
155
- alloc. reset ( ) ;
156
- alloc
157
- } ;
158
- let func_ty = module. get_type_of_func ( func) ;
159
- let block_ty = BlockType :: func_type ( func_ty) ;
160
- let end_label = labels. new_label ( ) ;
161
- let consume_fuel = fuel_costs
162
- . as_ref ( )
163
- . map ( |_| instrs. push_instr ( Instruction :: consume_fuel ( 1 ) ) ) ;
164
- stack. push_block ( block_ty, end_label, consume_fuel) ?;
165
- Ok ( Self {
152
+ labels,
153
+ instrs,
154
+ } = alloc. into_reset ( ) ;
155
+ let mut translator = Self {
166
156
func,
167
157
engine,
168
158
module,
@@ -172,7 +162,39 @@ impl FuncTranslator {
172
162
layout,
173
163
labels,
174
164
instrs,
175
- } )
165
+ } ;
166
+ translator. init_func_body_block ( ) ?;
167
+ translator. init_func_params ( ) ?;
168
+ Ok ( translator)
169
+ }
170
+
171
+ /// Initializes the function body enclosing control block.
172
+ fn init_func_body_block ( & mut self ) -> Result < ( ) , Error > {
173
+ let func_ty = self . module . get_type_of_func ( self . func ) ;
174
+ let block_ty = BlockType :: func_type ( func_ty) ;
175
+ let end_label = self . labels . new_label ( ) ;
176
+ let consume_fuel = self
177
+ . fuel_costs
178
+ . as_ref ( )
179
+ . map ( |_| self . instrs . push_instr ( Instruction :: consume_fuel ( 1 ) ) ) ;
180
+ self . stack . push_block ( block_ty, end_label, consume_fuel) ?;
181
+ Ok ( ( ) )
182
+ }
183
+
184
+ /// Initializes the function's parameters.
185
+ fn init_func_params ( & mut self ) -> Result < ( ) , Error > {
186
+ for ty in self . func_type ( ) . params ( ) {
187
+ self . stack . register_locals ( 1 , * ty) ?;
188
+ self . layout . register_locals ( 1 , * ty) ?;
189
+ }
190
+ Ok ( ( ) )
191
+ }
192
+
193
+ /// Returns the [`FuncType`] of the function that is currently translated.
194
+ fn func_type ( & self ) -> FuncType {
195
+ let dedup_func_type = self . module . get_type_of_func ( self . func ) ;
196
+ self . engine ( )
197
+ . resolve_func_type ( dedup_func_type, Clone :: clone)
176
198
}
177
199
178
200
/// Consumes `self` and returns the underlying reusable [`FuncTranslatorAllocations`].
0 commit comments