@@ -6,7 +6,12 @@ use std::path::{Path, PathBuf};
6
6
use std:: sync:: Arc ;
7
7
use std:: thread:: JoinHandle ;
8
8
9
- use cranelift_object:: { ObjectBuilder , ObjectModule } ;
9
+ use cranelift_codegen:: control:: ControlPlane ;
10
+ use cranelift_codegen:: ir:: Signature ;
11
+ use cranelift_codegen:: isa:: { TargetFrontendConfig , TargetIsa } ;
12
+ use cranelift_codegen:: FinalizedMachReloc ;
13
+ use cranelift_module:: ModuleResult ;
14
+ use cranelift_object:: { ObjectBuilder , ObjectModule , ObjectProduct } ;
10
15
use rustc_codegen_ssa:: assert_module_sources:: CguReuse ;
11
16
use rustc_codegen_ssa:: back:: link:: ensure_removed;
12
17
use rustc_codegen_ssa:: back:: metadata:: create_compressed_metadata_file;
@@ -318,7 +323,91 @@ fn produce_final_output_artifacts(
318
323
// These are used in linking steps and will be cleaned up afterward.
319
324
}
320
325
321
- fn make_module ( sess : & Session , backend_config : & BackendConfig , name : String ) -> ObjectModule {
326
+ struct AOTModule {
327
+ aot_module : ObjectModule ,
328
+ unwind_context : UnwindContext ,
329
+ }
330
+
331
+ impl AOTModule {
332
+ fn finish ( self ) -> ObjectProduct {
333
+ let mut product = self . aot_module . finish ( ) ;
334
+ self . unwind_context . emit ( & mut product) ;
335
+ product
336
+ }
337
+ }
338
+
339
+ impl Module for AOTModule {
340
+ fn isa ( & self ) -> & dyn TargetIsa {
341
+ self . aot_module . isa ( )
342
+ }
343
+
344
+ fn declarations ( & self ) -> & cranelift_module:: ModuleDeclarations {
345
+ self . aot_module . declarations ( )
346
+ }
347
+
348
+ fn get_name ( & self , name : & str ) -> Option < cranelift_module:: FuncOrDataId > {
349
+ self . aot_module . get_name ( name)
350
+ }
351
+
352
+ fn target_config ( & self ) -> TargetFrontendConfig {
353
+ self . aot_module . target_config ( )
354
+ }
355
+
356
+ fn declare_function (
357
+ & mut self ,
358
+ name : & str ,
359
+ linkage : Linkage ,
360
+ signature : & Signature ,
361
+ ) -> ModuleResult < FuncId > {
362
+ self . aot_module . declare_function ( name, linkage, signature)
363
+ }
364
+
365
+ fn declare_anonymous_function ( & mut self , signature : & Signature ) -> ModuleResult < FuncId > {
366
+ self . aot_module . declare_anonymous_function ( signature)
367
+ }
368
+
369
+ fn declare_data (
370
+ & mut self ,
371
+ name : & str ,
372
+ linkage : Linkage ,
373
+ writable : bool ,
374
+ tls : bool ,
375
+ ) -> ModuleResult < DataId > {
376
+ self . aot_module . declare_data ( name, linkage, writable, tls)
377
+ }
378
+
379
+ fn declare_anonymous_data ( & mut self , writable : bool , tls : bool ) -> ModuleResult < DataId > {
380
+ self . aot_module . declare_anonymous_data ( writable, tls)
381
+ }
382
+
383
+ fn define_function_with_control_plane (
384
+ & mut self ,
385
+ func : FuncId ,
386
+ ctx : & mut Context ,
387
+ ctrl_plane : & mut ControlPlane ,
388
+ ) -> ModuleResult < ( ) > {
389
+ self . aot_module . define_function_with_control_plane ( func, ctx, ctrl_plane) ?;
390
+ self . unwind_context . add_function ( func, ctx, self . aot_module . isa ( ) ) ;
391
+ Ok ( ( ) )
392
+ }
393
+
394
+ fn define_function_bytes (
395
+ & mut self ,
396
+ _func_id : FuncId ,
397
+ _func : & Function ,
398
+ _alignment : u64 ,
399
+ _bytes : & [ u8 ] ,
400
+ _relocs : & [ FinalizedMachReloc ] ,
401
+ ) -> ModuleResult < ( ) > {
402
+ unimplemented ! ( )
403
+ }
404
+
405
+ fn define_data ( & mut self , data_id : DataId , data : & DataDescription ) -> ModuleResult < ( ) > {
406
+ self . aot_module . define_data ( data_id, data)
407
+ }
408
+ }
409
+
410
+ fn make_module ( sess : & Session , backend_config : & BackendConfig , name : String ) -> AOTModule {
322
411
let isa = crate :: build_isa ( sess, backend_config) ;
323
412
324
413
let mut builder =
@@ -327,16 +416,17 @@ fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) ->
327
416
// is important, while cg_clif cares more about compilation times. Enabling -Zfunction-sections
328
417
// can easily double the amount of time necessary to perform linking.
329
418
builder. per_function_section ( sess. opts . unstable_opts . function_sections . unwrap_or ( false ) ) ;
330
- ObjectModule :: new ( builder)
419
+ let aot_module = ObjectModule :: new ( builder) ;
420
+ let unwind_context = UnwindContext :: new ( aot_module. isa ( ) , true ) ;
421
+ AOTModule { aot_module, unwind_context }
331
422
}
332
423
333
424
fn emit_cgu (
334
425
output_filenames : & OutputFilenames ,
335
426
prof : & SelfProfilerRef ,
336
427
name : String ,
337
- module : ObjectModule ,
428
+ module : AOTModule ,
338
429
debug : Option < DebugContext > ,
339
- unwind_context : UnwindContext ,
340
430
global_asm_object_file : Option < PathBuf > ,
341
431
producer : & str ,
342
432
) -> Result < ModuleCodegenResult , String > {
@@ -346,8 +436,6 @@ fn emit_cgu(
346
436
debug. emit ( & mut product) ;
347
437
}
348
438
349
- unwind_context. emit ( & mut product) ;
350
-
351
439
let module_regular = emit_module (
352
440
output_filenames,
353
441
prof,
@@ -494,7 +582,6 @@ fn module_codegen(
494
582
495
583
let mut cx = crate :: CodegenCx :: new (
496
584
tcx,
497
- backend_config. clone ( ) ,
498
585
module. isa ( ) ,
499
586
tcx. sess . opts . debuginfo != DebugInfo :: None ,
500
587
cgu_name,
@@ -531,13 +618,7 @@ fn module_codegen(
531
618
}
532
619
}
533
620
}
534
- crate :: main_shim:: maybe_create_entry_wrapper (
535
- tcx,
536
- & mut module,
537
- & mut cx. unwind_context ,
538
- false ,
539
- cgu. is_primary ( ) ,
540
- ) ;
621
+ crate :: main_shim:: maybe_create_entry_wrapper ( tcx, & mut module, false , cgu. is_primary ( ) ) ;
541
622
542
623
let cgu_name = cgu. name ( ) . as_str ( ) . to_owned ( ) ;
543
624
@@ -571,7 +652,6 @@ fn module_codegen(
571
652
cgu_name,
572
653
module,
573
654
cx. debug_context ,
574
- cx. unwind_context ,
575
655
global_asm_object_file,
576
656
& producer,
577
657
)
@@ -665,13 +745,10 @@ pub(crate) fn run_aot(
665
745
} ) ;
666
746
667
747
let mut allocator_module = make_module ( tcx. sess , & backend_config, "allocator_shim" . to_string ( ) ) ;
668
- let mut allocator_unwind_context = UnwindContext :: new ( allocator_module. isa ( ) , true ) ;
669
- let created_alloc_shim =
670
- crate :: allocator:: codegen ( tcx, & mut allocator_module, & mut allocator_unwind_context) ;
748
+ let created_alloc_shim = crate :: allocator:: codegen ( tcx, & mut allocator_module) ;
671
749
672
750
let allocator_module = if created_alloc_shim {
673
- let mut product = allocator_module. finish ( ) ;
674
- allocator_unwind_context. emit ( & mut product) ;
751
+ let product = allocator_module. finish ( ) ;
675
752
676
753
match emit_module (
677
754
tcx. output_filenames ( ( ) ) ,
0 commit comments