]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/back/write.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / compiler / rustc_codegen_gcc / src / back / write.rs
1 use std::{env, 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                 if env::var("CG_GCCJIT_DUMP_MODULE_NAMES").as_deref() == Ok("1") {
46                     println!("Module {}", module.name);
47                 }
48                 if env::var("CG_GCCJIT_DUMP_ALL_MODULES").as_deref() == Ok("1") || env::var("CG_GCCJIT_DUMP_MODULE").as_deref() == Ok(&module.name) {
49                     println!("Dumping reproducer {}", module.name);
50                     let _ = fs::create_dir("/tmp/reproducers");
51                     // FIXME(antoyo): segfault in dump_reproducer_to_file() might be caused by
52                     // transmuting an rvalue to an lvalue.
53                     // Segfault is actually in gcc::jit::reproducer::get_identifier_as_lvalue
54                     context.dump_reproducer_to_file(&format!("/tmp/reproducers/{}.c", module.name));
55                     println!("Dumped reproducer {}", module.name);
56                 }
57                 if env::var("CG_GCCJIT_DUMP_TO_FILE").as_deref() == Ok("1") {
58                     let _ = fs::create_dir("/tmp/gccjit_dumps");
59                     let path = &format!("/tmp/gccjit_dumps/{}.c", module.name);
60                     context.dump_to_file(path, true);
61                 }
62                 context.compile_to_file(OutputKind::ObjectFile, obj_out.to_str().expect("path to str"));
63             }
64
65             EmitObj::Bitcode => {
66                 // TODO(antoyo)
67             }
68
69             EmitObj::None => {}
70         }
71     }
72
73     Ok(module.into_compiled_module(
74         config.emit_obj != EmitObj::None,
75         cgcx.target_can_use_split_dwarf && cgcx.split_debuginfo == SplitDebuginfo::Unpacked,
76         config.emit_bc,
77         &cgcx.output_filenames,
78     ))
79 }
80
81 pub(crate) fn link(_cgcx: &CodegenContext<GccCodegenBackend>, _diag_handler: &Handler, mut _modules: Vec<ModuleCodegen<GccContext>>) -> Result<ModuleCodegen<GccContext>, FatalError> {
82     unimplemented!();
83 }