]> git.lizzy.rs Git - rust.git/blob - src/librustc_llvm/lib.rs
Rollup merge of #38559 - japaric:ptx2, r=alexcrichton
[rust.git] / src / librustc_llvm / lib.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(non_upper_case_globals)]
12 #![allow(non_camel_case_types)]
13 #![allow(non_snake_case)]
14 #![allow(dead_code)]
15
16 #![crate_name = "rustc_llvm"]
17 #![unstable(feature = "rustc_private", issue = "27812")]
18 #![crate_type = "dylib"]
19 #![crate_type = "rlib"]
20 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
21        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
22        html_root_url = "https://doc.rust-lang.org/nightly/")]
23 #![cfg_attr(not(stage0), deny(warnings))]
24
25 #![feature(associated_consts)]
26 #![feature(box_syntax)]
27 #![feature(concat_idents)]
28 #![feature(libc)]
29 #![feature(link_args)]
30 #![feature(staged_api)]
31 #![feature(rustc_private)]
32
33 extern crate libc;
34 #[macro_use]
35 #[no_link]
36 extern crate rustc_bitflags;
37
38 pub use self::IntPredicate::*;
39 pub use self::RealPredicate::*;
40 pub use self::TypeKind::*;
41 pub use self::AtomicRmwBinOp::*;
42 pub use self::MetadataType::*;
43 pub use self::CodeGenOptSize::*;
44 pub use self::DiagnosticKind::*;
45 pub use self::CallConv::*;
46 pub use self::DiagnosticSeverity::*;
47 pub use self::Linkage::*;
48
49 use std::str::FromStr;
50 use std::slice;
51 use std::ffi::{CString, CStr};
52 use std::cell::RefCell;
53 use libc::{c_uint, c_char, size_t};
54
55 pub mod archive_ro;
56 pub mod diagnostic;
57 pub mod ffi;
58
59 pub use ffi::*;
60
61 impl LLVMRustResult {
62     pub fn into_result(self) -> Result<(), ()> {
63         match self {
64             LLVMRustResult::Success => Ok(()),
65             LLVMRustResult::Failure => Err(()),
66         }
67     }
68 }
69
70 pub fn AddFunctionAttrStringValue(llfn: ValueRef,
71                                   idx: AttributePlace,
72                                   attr: &CStr,
73                                   value: &CStr) {
74     unsafe {
75         LLVMRustAddFunctionAttrStringValue(llfn,
76                                            idx.as_uint(),
77                                            attr.as_ptr(),
78                                            value.as_ptr())
79     }
80 }
81
82 #[repr(C)]
83 #[derive(Copy, Clone)]
84 pub enum AttributePlace {
85     Argument(u32),
86     Function,
87 }
88
89 impl AttributePlace {
90     pub fn ReturnValue() -> Self {
91         AttributePlace::Argument(0)
92     }
93
94     pub fn as_uint(self) -> c_uint {
95         match self {
96             AttributePlace::Function => !0,
97             AttributePlace::Argument(i) => i,
98         }
99     }
100 }
101
102 #[derive(Copy, Clone, PartialEq)]
103 #[repr(C)]
104 pub enum CodeGenOptSize {
105     CodeGenOptSizeNone = 0,
106     CodeGenOptSizeDefault = 1,
107     CodeGenOptSizeAggressive = 2,
108 }
109
110 impl FromStr for ArchiveKind {
111     type Err = ();
112
113     fn from_str(s: &str) -> Result<Self, Self::Err> {
114         match s {
115             "gnu" => Ok(ArchiveKind::K_GNU),
116             "mips64" => Ok(ArchiveKind::K_MIPS64),
117             "bsd" => Ok(ArchiveKind::K_BSD),
118             "coff" => Ok(ArchiveKind::K_COFF),
119             _ => Err(()),
120         }
121     }
122 }
123
124 #[allow(missing_copy_implementations)]
125 pub enum RustString_opaque {}
126 pub type RustStringRef = *mut RustString_opaque;
127 type RustStringRepr = *mut RefCell<Vec<u8>>;
128
129 /// Appending to a Rust string -- used by raw_rust_string_ostream.
130 #[no_mangle]
131 pub unsafe extern "C" fn rust_llvm_string_write_impl(sr: RustStringRef,
132                                                      ptr: *const c_char,
133                                                      size: size_t) {
134     let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
135
136     let sr = sr as RustStringRepr;
137     (*sr).borrow_mut().extend_from_slice(slice);
138 }
139
140 pub fn SetInstructionCallConv(instr: ValueRef, cc: CallConv) {
141     unsafe {
142         LLVMSetInstructionCallConv(instr, cc as c_uint);
143     }
144 }
145 pub fn SetFunctionCallConv(fn_: ValueRef, cc: CallConv) {
146     unsafe {
147         LLVMSetFunctionCallConv(fn_, cc as c_uint);
148     }
149 }
150
151 // Externally visible symbols that might appear in multiple translation units need to appear in
152 // their own comdat section so that the duplicates can be discarded at link time. This can for
153 // example happen for generics when using multiple codegen units. This function simply uses the
154 // value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
155 // function.
156 // For more details on COMDAT sections see e.g. http://www.airs.com/blog/archives/52
157 pub fn SetUniqueComdat(llmod: ModuleRef, val: ValueRef) {
158     unsafe {
159         LLVMRustSetComdat(llmod, val, LLVMGetValueName(val));
160     }
161 }
162
163 pub fn UnsetComdat(val: ValueRef) {
164     unsafe {
165         LLVMRustUnsetComdat(val);
166     }
167 }
168
169 pub fn SetUnnamedAddr(global: ValueRef, unnamed: bool) {
170     unsafe {
171         LLVMSetUnnamedAddr(global, unnamed as Bool);
172     }
173 }
174
175 pub fn set_thread_local(global: ValueRef, is_thread_local: bool) {
176     unsafe {
177         LLVMSetThreadLocal(global, is_thread_local as Bool);
178     }
179 }
180
181 impl Attribute {
182     pub fn apply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
183         unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
184     }
185
186     pub fn apply_callsite(&self, idx: AttributePlace, callsite: ValueRef) {
187         unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
188     }
189
190     pub fn unapply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
191         unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
192     }
193
194     pub fn toggle_llfn(&self, idx: AttributePlace, llfn: ValueRef, set: bool) {
195         if set {
196             self.apply_llfn(idx, llfn);
197         } else {
198             self.unapply_llfn(idx, llfn);
199         }
200     }
201 }
202
203 // Memory-managed interface to target data.
204
205 pub struct TargetData {
206     pub lltd: TargetDataRef,
207 }
208
209 impl Drop for TargetData {
210     fn drop(&mut self) {
211         unsafe {
212             LLVMDisposeTargetData(self.lltd);
213         }
214     }
215 }
216
217 pub fn mk_target_data(string_rep: &str) -> TargetData {
218     let string_rep = CString::new(string_rep).unwrap();
219     TargetData { lltd: unsafe { LLVMCreateTargetData(string_rep.as_ptr()) } }
220 }
221
222 // Memory-managed interface to object files.
223
224 pub struct ObjectFile {
225     pub llof: ObjectFileRef,
226 }
227
228 impl ObjectFile {
229     // This will take ownership of llmb
230     pub fn new(llmb: MemoryBufferRef) -> Option<ObjectFile> {
231         unsafe {
232             let llof = LLVMCreateObjectFile(llmb);
233             if llof as isize == 0 {
234                 // LLVMCreateObjectFile took ownership of llmb
235                 return None;
236             }
237
238             Some(ObjectFile { llof: llof })
239         }
240     }
241 }
242
243 impl Drop for ObjectFile {
244     fn drop(&mut self) {
245         unsafe {
246             LLVMDisposeObjectFile(self.llof);
247         }
248     }
249 }
250
251 // Memory-managed interface to section iterators.
252
253 pub struct SectionIter {
254     pub llsi: SectionIteratorRef,
255 }
256
257 impl Drop for SectionIter {
258     fn drop(&mut self) {
259         unsafe {
260             LLVMDisposeSectionIterator(self.llsi);
261         }
262     }
263 }
264
265 pub fn mk_section_iter(llof: ObjectFileRef) -> SectionIter {
266     unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
267 }
268
269 /// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
270 pub fn get_param(llfn: ValueRef, index: c_uint) -> ValueRef {
271     unsafe {
272         assert!(index < LLVMCountParams(llfn),
273             "out of bounds argument access: {} out of {} arguments", index, LLVMCountParams(llfn));
274         LLVMGetParam(llfn, index)
275     }
276 }
277
278 pub fn get_params(llfn: ValueRef) -> Vec<ValueRef> {
279     unsafe {
280         let num_params = LLVMCountParams(llfn);
281         let mut params = Vec::with_capacity(num_params as usize);
282         for idx in 0..num_params {
283             params.push(LLVMGetParam(llfn, idx));
284         }
285
286         params
287     }
288 }
289
290 pub fn build_string<F>(f: F) -> Option<String>
291     where F: FnOnce(RustStringRef)
292 {
293     let mut buf = RefCell::new(Vec::new());
294     f(&mut buf as RustStringRepr as RustStringRef);
295     String::from_utf8(buf.into_inner()).ok()
296 }
297
298 pub unsafe fn twine_to_string(tr: TwineRef) -> String {
299     build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
300 }
301
302 pub unsafe fn debug_loc_to_string(c: ContextRef, tr: DebugLocRef) -> String {
303     build_string(|s| LLVMRustWriteDebugLocToString(c, tr, s))
304         .expect("got a non-UTF8 DebugLoc from LLVM")
305 }
306
307 pub fn initialize_available_targets() {
308     macro_rules! init_target(
309         ($cfg:meta, $($method:ident),*) => { {
310             #[cfg($cfg)]
311             fn init() {
312                 extern {
313                     $(fn $method();)*
314                 }
315                 unsafe {
316                     $($method();)*
317                 }
318             }
319             #[cfg(not($cfg))]
320             fn init() { }
321             init();
322         } }
323     );
324     init_target!(llvm_component = "x86",
325                  LLVMInitializeX86TargetInfo,
326                  LLVMInitializeX86Target,
327                  LLVMInitializeX86TargetMC,
328                  LLVMInitializeX86AsmPrinter,
329                  LLVMInitializeX86AsmParser);
330     init_target!(llvm_component = "arm",
331                  LLVMInitializeARMTargetInfo,
332                  LLVMInitializeARMTarget,
333                  LLVMInitializeARMTargetMC,
334                  LLVMInitializeARMAsmPrinter,
335                  LLVMInitializeARMAsmParser);
336     init_target!(llvm_component = "aarch64",
337                  LLVMInitializeAArch64TargetInfo,
338                  LLVMInitializeAArch64Target,
339                  LLVMInitializeAArch64TargetMC,
340                  LLVMInitializeAArch64AsmPrinter,
341                  LLVMInitializeAArch64AsmParser);
342     init_target!(llvm_component = "mips",
343                  LLVMInitializeMipsTargetInfo,
344                  LLVMInitializeMipsTarget,
345                  LLVMInitializeMipsTargetMC,
346                  LLVMInitializeMipsAsmPrinter,
347                  LLVMInitializeMipsAsmParser);
348     init_target!(llvm_component = "powerpc",
349                  LLVMInitializePowerPCTargetInfo,
350                  LLVMInitializePowerPCTarget,
351                  LLVMInitializePowerPCTargetMC,
352                  LLVMInitializePowerPCAsmPrinter,
353                  LLVMInitializePowerPCAsmParser);
354     init_target!(llvm_component = "pnacl",
355                  LLVMInitializePNaClTargetInfo,
356                  LLVMInitializePNaClTarget,
357                  LLVMInitializePNaClTargetMC);
358     init_target!(llvm_component = "systemz",
359                  LLVMInitializeSystemZTargetInfo,
360                  LLVMInitializeSystemZTarget,
361                  LLVMInitializeSystemZTargetMC,
362                  LLVMInitializeSystemZAsmPrinter,
363                  LLVMInitializeSystemZAsmParser);
364     init_target!(llvm_component = "jsbackend",
365                  LLVMInitializeJSBackendTargetInfo,
366                  LLVMInitializeJSBackendTarget,
367                  LLVMInitializeJSBackendTargetMC);
368     init_target!(llvm_component = "msp430",
369                  LLVMInitializeMSP430TargetInfo,
370                  LLVMInitializeMSP430Target,
371                  LLVMInitializeMSP430TargetMC,
372                  LLVMInitializeMSP430AsmPrinter);
373     init_target!(llvm_component = "sparc",
374                  LLVMInitializeSparcTargetInfo,
375                  LLVMInitializeSparcTarget,
376                  LLVMInitializeSparcTargetMC,
377                  LLVMInitializeSparcAsmPrinter,
378                  LLVMInitializeSparcAsmParser);
379     init_target!(llvm_component = "nvptx",
380                  LLVMInitializeNVPTXTargetInfo,
381                  LLVMInitializeNVPTXTarget,
382                  LLVMInitializeNVPTXTargetMC,
383                  LLVMInitializeNVPTXAsmPrinter);
384 }
385
386 pub fn last_error() -> Option<String> {
387     unsafe {
388         let cstr = LLVMRustGetLastError();
389         if cstr.is_null() {
390             None
391         } else {
392             let err = CStr::from_ptr(cstr).to_bytes();
393             let err = String::from_utf8_lossy(err).to_string();
394             libc::free(cstr as *mut _);
395             Some(err)
396         }
397     }
398 }
399
400 pub struct OperandBundleDef {
401     inner: OperandBundleDefRef,
402 }
403
404 impl OperandBundleDef {
405     pub fn new(name: &str, vals: &[ValueRef]) -> OperandBundleDef {
406         let name = CString::new(name).unwrap();
407         let def = unsafe {
408             LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
409         };
410         OperandBundleDef { inner: def }
411     }
412
413     pub fn raw(&self) -> OperandBundleDefRef {
414         self.inner
415     }
416 }
417
418 impl Drop for OperandBundleDef {
419     fn drop(&mut self) {
420         unsafe {
421             LLVMRustFreeOperandBundleDef(self.inner);
422         }
423     }
424 }
425
426 // The module containing the native LLVM dependencies, generated by the build system
427 // Note that this must come after the rustllvm extern declaration so that
428 // parts of LLVM that rustllvm depends on aren't thrown away by the linker.
429 // Works to the above fix for #15460 to ensure LLVM dependencies that
430 // are only used by rustllvm don't get stripped by the linker.
431 #[cfg(not(cargobuild))]
432 mod llvmdeps {
433     include! { env!("CFG_LLVM_LINKAGE_FILE") }
434 }