]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/back/write.rs
Merge commit 'e228f0c16ea8c34794a6285bf57aab627c26b147' into libgccjit-codegen
[rust.git] / compiler / rustc_codegen_gcc / src / back / write.rs
1 use std::fs;
2
3 use gccjit::OutputKind;
4 use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
5 use rustc_codegen_ssa::back::write::{CodegenContext, EmitObj, ModuleConfig};
6 use rustc_errors::Handler;
7 use rustc_session::config::OutputType;
8 use rustc_span::fatal_error::FatalError;
9 use rustc_target::spec::SplitDebuginfo;
10
11 use crate::{GccCodegenBackend, GccContext};
12
13 pub(crate) unsafe fn codegen(cgcx: &CodegenContext<GccCodegenBackend>, _diag_handler: &Handler, module: ModuleCodegen<GccContext>, config: &ModuleConfig) -> Result<CompiledModule, FatalError> {
14     let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &module.name[..]);
15     {
16         let context = &module.module_llvm.context;
17
18         let module_name = module.name.clone();
19         let module_name = Some(&module_name[..]);
20
21         let _bc_out = cgcx.output_filenames.temp_path(OutputType::Bitcode, module_name);
22         let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name);
23
24         if config.bitcode_needed() {
25             // TODO(antoyo)
26         }
27
28         if config.emit_ir {
29             unimplemented!();
30         }
31
32         if config.emit_asm {
33             let _timer = cgcx
34                 .prof
35                 .generic_activity_with_arg("LLVM_module_codegen_emit_asm", &module.name[..]);
36             let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
37             context.compile_to_file(OutputKind::Assembler, path.to_str().expect("path to str"));
38         }
39
40         match config.emit_obj {
41             EmitObj::ObjectCode(_) => {
42                 let _timer = cgcx
43                     .prof
44                     .generic_activity_with_arg("LLVM_module_codegen_emit_obj", &module.name[..]);
45                 match &*module.name {
46                     "std_example.7rcbfp3g-cgu.15" => {
47                         println!("Dumping reproducer {}", module.name);
48                         let _ = fs::create_dir("/tmp/reproducers");
49                         // FIXME(antoyo): segfault in dump_reproducer_to_file() might be caused by
50                         // transmuting an rvalue to an lvalue.
51                         // Segfault is actually in gcc::jit::reproducer::get_identifier_as_lvalue
52                         context.dump_reproducer_to_file(&format!("/tmp/reproducers/{}.c", module.name));
53                         println!("Dumped reproducer {}", module.name);
54                     },
55                     _ => (),
56                 }
57                 context.compile_to_file(OutputKind::ObjectFile, obj_out.to_str().expect("path to str"));
58             }
59
60             EmitObj::Bitcode => {
61                 // TODO(antoyo)
62             }
63
64             EmitObj::None => {}
65         }
66     }
67
68     Ok(module.into_compiled_module(
69         config.emit_obj != EmitObj::None,
70         cgcx.target_can_use_split_dwarf && cgcx.split_debuginfo == SplitDebuginfo::Unpacked,
71         config.emit_bc,
72         &cgcx.output_filenames,
73     ))
74 }
75
76 pub(crate) fn link(_cgcx: &CodegenContext<GccCodegenBackend>, _diag_handler: &Handler, mut _modules: Vec<ModuleCodegen<GccContext>>) -> Result<ModuleCodegen<GccContext>, FatalError> {
77     unimplemented!();
78 }