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.
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.
11 #![allow(non_upper_case_globals)]
12 #![allow(non_camel_case_types)]
13 #![allow(non_snake_case)]
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))]
25 #![feature(associated_consts)]
26 #![feature(box_syntax)]
27 #![feature(concat_idents)]
29 #![feature(link_args)]
30 #![feature(staged_api)]
31 #![feature(rustc_private)]
36 extern crate rustc_bitflags;
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::*;
49 use std::str::FromStr;
51 use std::ffi::{CString, CStr};
52 use std::cell::RefCell;
53 use libc::{c_uint, c_char, size_t};
62 pub fn into_result(self) -> Result<(), ()> {
64 LLVMRustResult::Success => Ok(()),
65 LLVMRustResult::Failure => Err(()),
70 pub fn AddFunctionAttrStringValue(llfn: ValueRef,
75 LLVMRustAddFunctionAttrStringValue(llfn,
83 #[derive(Copy, Clone)]
84 pub enum AttributePlace {
90 pub fn ReturnValue() -> Self {
91 AttributePlace::Argument(0)
94 pub fn as_uint(self) -> c_uint {
96 AttributePlace::Function => !0,
97 AttributePlace::Argument(i) => i,
102 #[derive(Copy, Clone, PartialEq)]
104 pub enum CodeGenOptSize {
105 CodeGenOptSizeNone = 0,
106 CodeGenOptSizeDefault = 1,
107 CodeGenOptSizeAggressive = 2,
110 impl FromStr for ArchiveKind {
113 fn from_str(s: &str) -> Result<Self, Self::Err> {
115 "gnu" => Ok(ArchiveKind::K_GNU),
116 "mips64" => Ok(ArchiveKind::K_MIPS64),
117 "bsd" => Ok(ArchiveKind::K_BSD),
118 "coff" => Ok(ArchiveKind::K_COFF),
124 #[allow(missing_copy_implementations)]
125 pub enum RustString_opaque {}
126 pub type RustStringRef = *mut RustString_opaque;
127 type RustStringRepr = *mut RefCell<Vec<u8>>;
129 /// Appending to a Rust string -- used by raw_rust_string_ostream.
131 pub unsafe extern "C" fn rust_llvm_string_write_impl(sr: RustStringRef,
134 let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
136 let sr = sr as RustStringRepr;
137 (*sr).borrow_mut().extend_from_slice(slice);
140 pub fn SetInstructionCallConv(instr: ValueRef, cc: CallConv) {
142 LLVMSetInstructionCallConv(instr, cc as c_uint);
145 pub fn SetFunctionCallConv(fn_: ValueRef, cc: CallConv) {
147 LLVMSetFunctionCallConv(fn_, cc as c_uint);
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
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) {
159 LLVMRustSetComdat(llmod, val, LLVMGetValueName(val));
163 pub fn UnsetComdat(val: ValueRef) {
165 LLVMRustUnsetComdat(val);
169 pub fn SetUnnamedAddr(global: ValueRef, unnamed: bool) {
171 LLVMSetUnnamedAddr(global, unnamed as Bool);
175 pub fn set_thread_local(global: ValueRef, is_thread_local: bool) {
177 LLVMSetThreadLocal(global, is_thread_local as Bool);
182 pub fn apply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
183 unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
186 pub fn apply_callsite(&self, idx: AttributePlace, callsite: ValueRef) {
187 unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
190 pub fn unapply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
191 unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
194 pub fn toggle_llfn(&self, idx: AttributePlace, llfn: ValueRef, set: bool) {
196 self.apply_llfn(idx, llfn);
198 self.unapply_llfn(idx, llfn);
203 // Memory-managed interface to target data.
205 pub struct TargetData {
206 pub lltd: TargetDataRef,
209 impl Drop for TargetData {
212 LLVMDisposeTargetData(self.lltd);
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()) } }
222 // Memory-managed interface to object files.
224 pub struct ObjectFile {
225 pub llof: ObjectFileRef,
229 // This will take ownership of llmb
230 pub fn new(llmb: MemoryBufferRef) -> Option<ObjectFile> {
232 let llof = LLVMCreateObjectFile(llmb);
233 if llof as isize == 0 {
234 // LLVMCreateObjectFile took ownership of llmb
238 Some(ObjectFile { llof: llof })
243 impl Drop for ObjectFile {
246 LLVMDisposeObjectFile(self.llof);
251 // Memory-managed interface to section iterators.
253 pub struct SectionIter {
254 pub llsi: SectionIteratorRef,
257 impl Drop for SectionIter {
260 LLVMDisposeSectionIterator(self.llsi);
265 pub fn mk_section_iter(llof: ObjectFileRef) -> SectionIter {
266 unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
269 /// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
270 pub fn get_param(llfn: ValueRef, index: c_uint) -> ValueRef {
272 assert!(index < LLVMCountParams(llfn),
273 "out of bounds argument access: {} out of {} arguments", index, LLVMCountParams(llfn));
274 LLVMGetParam(llfn, index)
278 pub fn get_params(llfn: ValueRef) -> Vec<ValueRef> {
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));
290 pub fn build_string<F>(f: F) -> Option<String>
291 where F: FnOnce(RustStringRef)
293 let mut buf = RefCell::new(Vec::new());
294 f(&mut buf as RustStringRepr as RustStringRef);
295 String::from_utf8(buf.into_inner()).ok()
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")
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")
307 pub fn initialize_available_targets() {
308 macro_rules! init_target(
309 ($cfg:meta, $($method:ident),*) => { {
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);
386 pub fn last_error() -> Option<String> {
388 let cstr = LLVMRustGetLastError();
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 _);
400 pub struct OperandBundleDef {
401 inner: OperandBundleDefRef,
404 impl OperandBundleDef {
405 pub fn new(name: &str, vals: &[ValueRef]) -> OperandBundleDef {
406 let name = CString::new(name).unwrap();
408 LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
410 OperandBundleDef { inner: def }
413 pub fn raw(&self) -> OperandBundleDefRef {
418 impl Drop for OperandBundleDef {
421 LLVMRustFreeOperandBundleDef(self.inner);
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))]
433 include! { env!("CFG_LLVM_LINKAGE_FILE") }