@@ -21,9 +21,9 @@ use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
2121use std:: vec:: Vec ;
2222
2323/// Implementation of a relocation sink that just saves all the information for later
24- struct RelocSink {
24+ pub struct RelocSink {
2525 /// Relocations recorded for the function.
26- func_relocs : Vec < Relocation > ,
26+ pub func_relocs : Vec < Relocation > ,
2727}
2828
2929impl binemit:: RelocSink for RelocSink {
@@ -109,69 +109,75 @@ fn get_address_transform(
109109 result
110110}
111111
112- /// Compile the module using Cranelift, producing a compilation result with
113- /// associated relocations.
114- pub fn compile_module < ' data , ' module > (
115- module : & ' module Module ,
116- function_body_inputs : PrimaryMap < DefinedFuncIndex , FunctionBodyData < ' data > > ,
117- isa : & dyn isa:: TargetIsa ,
118- generate_debug_info : bool ,
119- ) -> Result < ( Compilation , Relocations , AddressTransforms ) , CompileError > {
120- let mut functions = PrimaryMap :: with_capacity ( function_body_inputs. len ( ) ) ;
121- let mut relocations = PrimaryMap :: with_capacity ( function_body_inputs. len ( ) ) ;
122- let mut address_transforms = PrimaryMap :: with_capacity ( function_body_inputs. len ( ) ) ;
112+ /// A compiler that compiles a WebAssembly module with Cranelift, translating the Wasm to Cranelift IR,
113+ /// optimizing it and then translating to assembly.
114+ pub struct Cranelift ;
123115
124- function_body_inputs
125- . into_iter ( )
126- . collect :: < Vec < ( DefinedFuncIndex , & FunctionBodyData < ' data > ) > > ( )
127- . par_iter ( )
128- . map ( |( i, input) | {
129- let func_index = module. func_index ( * i) ;
130- let mut context = Context :: new ( ) ;
131- context. func . name = get_func_name ( func_index) ;
132- context. func . signature = module. signatures [ module. functions [ func_index] ] . clone ( ) ;
116+ impl crate :: compilation:: Compiler for Cranelift {
117+ /// Compile the module using Cranelift, producing a compilation result with
118+ /// associated relocations.
119+ fn compile_module < ' data , ' module > (
120+ module : & ' module Module ,
121+ function_body_inputs : PrimaryMap < DefinedFuncIndex , FunctionBodyData < ' data > > ,
122+ isa : & dyn isa:: TargetIsa ,
123+ generate_debug_info : bool ,
124+ ) -> Result < ( Compilation , Relocations , AddressTransforms ) , CompileError > {
125+ let mut functions = PrimaryMap :: with_capacity ( function_body_inputs. len ( ) ) ;
126+ let mut relocations = PrimaryMap :: with_capacity ( function_body_inputs. len ( ) ) ;
127+ let mut address_transforms = PrimaryMap :: with_capacity ( function_body_inputs. len ( ) ) ;
133128
134- let mut trans = FuncTranslator :: new ( ) ;
135- trans
136- . translate (
137- input . data ,
138- input . module_offset ,
139- & mut context . func ,
140- & mut FuncEnvironment :: new ( isa . frontend_config ( ) , module ) ,
141- )
142- . map_err ( CompileError :: Wasm ) ? ;
129+ function_body_inputs
130+ . into_iter ( )
131+ . collect :: < Vec < ( DefinedFuncIndex , & FunctionBodyData < ' data > ) > > ( )
132+ . par_iter ( )
133+ . map ( | ( i , input ) | {
134+ let func_index = module . func_index ( * i ) ;
135+ let mut context = Context :: new ( ) ;
136+ context . func . name = get_func_name ( func_index ) ;
137+ context . func . signature = module . signatures [ module . functions [ func_index ] ] . clone ( ) ;
143138
144- let mut code_buf: Vec < u8 > = Vec :: new ( ) ;
145- let mut reloc_sink = RelocSink :: new ( ) ;
146- let mut trap_sink = binemit:: NullTrapSink { } ;
147- context
148- . compile_and_emit ( isa, & mut code_buf, & mut reloc_sink, & mut trap_sink)
149- . map_err ( CompileError :: Codegen ) ?;
139+ let mut trans = FuncTranslator :: new ( ) ;
140+ trans
141+ . translate (
142+ input. data ,
143+ input. module_offset ,
144+ & mut context. func ,
145+ & mut FuncEnvironment :: new ( isa. frontend_config ( ) , module) ,
146+ )
147+ . map_err ( CompileError :: Wasm ) ?;
150148
151- let address_transform = if generate_debug_info {
152- let body_len = code_buf. len ( ) ;
153- let at = get_address_transform ( & context, isa) ;
154- Some ( FunctionAddressTransform {
155- locations : at,
156- body_offset : 0 ,
157- body_len,
158- } )
159- } else {
160- None
161- } ;
149+ let mut code_buf: Vec < u8 > = Vec :: new ( ) ;
150+ let mut reloc_sink = RelocSink :: new ( ) ;
151+ let mut trap_sink = binemit:: NullTrapSink { } ;
152+ context
153+ . compile_and_emit ( isa, & mut code_buf, & mut reloc_sink, & mut trap_sink)
154+ . map_err ( CompileError :: Codegen ) ?;
162155
163- Ok ( ( code_buf, reloc_sink. func_relocs , address_transform) )
164- } )
165- . collect :: < Result < Vec < _ > , CompileError > > ( ) ?
166- . into_iter ( )
167- . for_each ( |( function, relocs, address_transform) | {
168- functions. push ( function) ;
169- relocations. push ( relocs) ;
170- if let Some ( address_transform) = address_transform {
171- address_transforms. push ( address_transform) ;
172- }
173- } ) ;
156+ let address_transform = if generate_debug_info {
157+ let body_len = code_buf. len ( ) ;
158+ let at = get_address_transform ( & context, isa) ;
159+ Some ( FunctionAddressTransform {
160+ locations : at,
161+ body_offset : 0 ,
162+ body_len,
163+ } )
164+ } else {
165+ None
166+ } ;
167+
168+ Ok ( ( code_buf, reloc_sink. func_relocs , address_transform) )
169+ } )
170+ . collect :: < Result < Vec < _ > , CompileError > > ( ) ?
171+ . into_iter ( )
172+ . for_each ( |( function, relocs, address_transform) | {
173+ functions. push ( function) ;
174+ relocations. push ( relocs) ;
175+ if let Some ( address_transform) = address_transform {
176+ address_transforms. push ( address_transform) ;
177+ }
178+ } ) ;
174179
175- // TODO: Reorganize where we create the Vec for the resolved imports.
176- Ok ( ( Compilation :: new ( functions) , relocations, address_transforms) )
180+ // TODO: Reorganize where we create the Vec for the resolved imports.
181+ Ok ( ( Compilation :: new ( functions) , relocations, address_transforms) )
182+ }
177183}
0 commit comments