1 //! Codegen the completed AST to the LLVM IR.
3 //! Some functions here, such as codegen_block and codegen_expr, return a value --
4 //! the result of the codegen to LLVM -- while others, such as codegen_fn
5 //! and mono_item, are called only for the side effect of adding a
6 //! particular definition to the LLVM IR output we're producing.
8 //! Hopefully useful general knowledge about codegen:
10 //! * There's no way to find out the `Ty` type of a Value. Doing so
11 //! would be "trying to get the eggs out of an omelette" (credit:
12 //! pcwalton). You can, instead, find out its `llvm::Type` by calling `val_ty`,
13 //! but one `llvm::Type` corresponds to many `Ty`s; for instance, `tup(int, int,
14 //! int)` and `rec(x=int, y=int, z=int)` will have the same `llvm::Type`.
16 use super::{LlvmCodegenBackend, ModuleLlvm};
17 use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
18 use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
22 use crate::builder::Builder;
24 use crate::context::CodegenCx;
25 use crate::monomorphize::partitioning::CodegenUnitExt;
27 use rustc::mir::mono::{Linkage, Visibility, Stats};
28 use rustc::middle::cstore::{EncodedMetadata};
29 use rustc::ty::TyCtxt;
30 use rustc::middle::exported_symbols;
31 use rustc::session::config::{self, DebugInfo};
32 use rustc_codegen_ssa::mono_item::MonoItemExt;
33 use rustc_data_structures::small_c_str::SmallCStr;
35 use rustc_codegen_ssa::traits::*;
36 use rustc_codegen_ssa::back::write::submit_codegened_module_to_llvm;
38 use std::ffi::CString;
39 use std::time::Instant;
40 use syntax_pos::symbol::InternedString;
41 use rustc::hir::CodegenFnAttrs;
43 use crate::value::Value;
46 pub fn write_metadata<'a, 'gcx>(
47 tcx: TyCtxt<'a, 'gcx, 'gcx>,
48 llvm_module: &mut ModuleLlvm
49 ) -> EncodedMetadata {
51 use flate2::Compression;
52 use flate2::write::DeflateEncoder;
54 let (metadata_llcx, metadata_llmod) = (&*llvm_module.llcx, llvm_module.llmod());
56 #[derive(PartialEq, Eq, PartialOrd, Ord)]
63 let kind = tcx.sess.crate_types.borrow().iter().map(|ty| {
65 config::CrateType::Executable |
66 config::CrateType::Staticlib |
67 config::CrateType::Cdylib => MetadataKind::None,
69 config::CrateType::Rlib => MetadataKind::Uncompressed,
71 config::CrateType::Dylib |
72 config::CrateType::ProcMacro => MetadataKind::Compressed,
74 }).max().unwrap_or(MetadataKind::None);
76 if kind == MetadataKind::None {
77 return EncodedMetadata::new();
80 let metadata = tcx.encode_metadata();
81 if kind == MetadataKind::Uncompressed {
85 assert!(kind == MetadataKind::Compressed);
86 let mut compressed = tcx.metadata_encoding_version();
87 DeflateEncoder::new(&mut compressed, Compression::fast())
88 .write_all(&metadata.raw_data).unwrap();
90 let llmeta = common::bytes_in_context(metadata_llcx, &compressed);
91 let llconst = common::struct_in_context(metadata_llcx, &[llmeta], false);
92 let name = exported_symbols::metadata_symbol_name(tcx);
93 let buf = CString::new(name).unwrap();
94 let llglobal = unsafe {
95 llvm::LLVMAddGlobal(metadata_llmod, common::val_ty(llconst), buf.as_ptr())
98 llvm::LLVMSetInitializer(llglobal, llconst);
99 let section_name = metadata::metadata_section_name(&tcx.sess.target.target);
100 let name = SmallCStr::new(section_name);
101 llvm::LLVMSetSection(llglobal, name.as_ptr());
103 // Also generate a .section directive to force no
104 // flags, at least for ELF outputs, so that the
105 // metadata doesn't get loaded into memory.
106 let directive = format!(".section {}", section_name);
107 let directive = CString::new(directive).unwrap();
108 llvm::LLVMSetModuleInlineAsm(metadata_llmod, directive.as_ptr())
113 pub struct ValueIter<'ll> {
114 cur: Option<&'ll Value>,
115 step: unsafe extern "C" fn(&'ll Value) -> Option<&'ll Value>,
118 impl Iterator for ValueIter<'ll> {
119 type Item = &'ll Value;
121 fn next(&mut self) -> Option<&'ll Value> {
123 if let Some(old) = old {
124 self.cur = unsafe { (self.step)(old) };
130 pub fn iter_globals(llmod: &'ll llvm::Module) -> ValueIter<'ll> {
133 cur: llvm::LLVMGetFirstGlobal(llmod),
134 step: llvm::LLVMGetNextGlobal,
139 pub fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
140 cgu_name: InternedString)
142 let start_time = Instant::now();
144 let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
145 let ((stats, module), _) = tcx.dep_graph.with_task(dep_node,
149 dep_graph::hash_result);
150 let time_to_codegen = start_time.elapsed();
152 // We assume that the cost to run LLVM on a CGU is proportional to
153 // the time we needed for codegenning it.
154 let cost = time_to_codegen.as_secs() * 1_000_000_000 +
155 time_to_codegen.subsec_nanos() as u64;
157 submit_codegened_module_to_llvm(&LlvmCodegenBackend(()), tcx, module, cost);
160 fn module_codegen<'ll, 'tcx>(
161 tcx: TyCtxt<'ll, 'tcx, 'tcx>,
162 cgu_name: InternedString)
163 -> (Stats, ModuleCodegen<ModuleLlvm>)
165 let cgu = tcx.codegen_unit(cgu_name);
166 // Instantiate monomorphizations without filling out definitions yet...
167 let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
169 let cx = CodegenCx::new(tcx, cgu, &llvm_module);
170 let mono_items = cx.codegen_unit
171 .items_in_deterministic_order(cx.tcx);
172 for &(mono_item, (linkage, visibility)) in &mono_items {
173 mono_item.predefine::<Builder<'_, '_, '_>>(&cx, linkage, visibility);
176 // ... and now that we have everything pre-defined, fill out those definitions.
177 for &(mono_item, _) in &mono_items {
178 mono_item.define::<Builder<'_, '_, '_>>(&cx);
181 // If this codegen unit contains the main function, also create the
183 maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx);
185 // Run replace-all-uses-with for statics that need it
186 for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
188 let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
189 llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
190 llvm::LLVMDeleteGlobal(old_g);
194 // Create the llvm.used variable
195 // This variable has type [N x i8*] and is stored in the llvm.metadata section
196 if !cx.used_statics().borrow().is_empty() {
197 cx.create_used_variable()
200 // Finalize debuginfo
201 if cx.sess().opts.debuginfo != DebugInfo::None {
202 cx.debuginfo_finalize();
205 cx.consume_stats().into_inner()
208 (stats, ModuleCodegen {
209 name: cgu_name.to_string(),
210 module_llvm: llvm_module,
211 kind: ModuleKind::Regular,
216 pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
217 let sect = match attrs.link_section {
222 let buf = SmallCStr::new(§.as_str());
223 llvm::LLVMSetSection(llval, buf.as_ptr());
227 pub fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
229 Linkage::External => llvm::Linkage::ExternalLinkage,
230 Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
231 Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
232 Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
233 Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
234 Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
235 Linkage::Appending => llvm::Linkage::AppendingLinkage,
236 Linkage::Internal => llvm::Linkage::InternalLinkage,
237 Linkage::Private => llvm::Linkage::PrivateLinkage,
238 Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
239 Linkage::Common => llvm::Linkage::CommonLinkage,
243 pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
245 Visibility::Default => llvm::Visibility::Default,
246 Visibility::Hidden => llvm::Visibility::Hidden,
247 Visibility::Protected => llvm::Visibility::Protected,