1 #![allow(non_upper_case_globals)]
2 #![allow(non_camel_case_types)]
3 #![allow(non_snake_case)]
4 #![deny(bare_trait_objects)]
6 pub use self::IntPredicate::*;
7 pub use self::RealPredicate::*;
8 pub use self::AtomicRmwBinOp::*;
9 pub use self::MetadataType::*;
10 pub use self::CodeGenOptSize::*;
11 pub use self::CallConv::*;
12 pub use self::Linkage::*;
14 use std::str::FromStr;
15 use std::string::FromUtf8Error;
18 use std::cell::RefCell;
19 use libc::{c_uint, c_char, size_t};
20 use rustc_data_structures::small_c_str::SmallCStr;
29 pub fn into_result(self) -> Result<(), ()> {
31 LLVMRustResult::Success => Ok(()),
32 LLVMRustResult::Failure => Err(()),
37 pub fn AddFunctionAttrStringValue(llfn: &'a Value,
42 LLVMRustAddFunctionAttrStringValue(llfn,
49 #[derive(Copy, Clone)]
50 pub enum AttributePlace {
57 pub fn as_uint(self) -> c_uint {
59 AttributePlace::ReturnValue => 0,
60 AttributePlace::Argument(i) => 1 + i,
61 AttributePlace::Function => !0,
66 #[derive(Copy, Clone, PartialEq)]
68 pub enum CodeGenOptSize {
69 CodeGenOptSizeNone = 0,
70 CodeGenOptSizeDefault = 1,
71 CodeGenOptSizeAggressive = 2,
74 impl FromStr for ArchiveKind {
77 fn from_str(s: &str) -> Result<Self, Self::Err> {
79 "gnu" => Ok(ArchiveKind::K_GNU),
80 "bsd" => Ok(ArchiveKind::K_BSD),
81 "coff" => Ok(ArchiveKind::K_COFF),
88 pub struct RustString {
89 bytes: RefCell<Vec<u8>>,
92 /// Appending to a Rust string -- used by RawRustStringOstream.
94 pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: &RustString,
97 let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
99 sr.bytes.borrow_mut().extend_from_slice(slice);
102 pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
104 LLVMSetInstructionCallConv(instr, cc as c_uint);
107 pub fn SetFunctionCallConv(fn_: &'a Value, cc: CallConv) {
109 LLVMSetFunctionCallConv(fn_, cc as c_uint);
113 // Externally visible symbols that might appear in multiple codegen units need to appear in
114 // their own comdat section so that the duplicates can be discarded at link time. This can for
115 // example happen for generics when using multiple codegen units. This function simply uses the
116 // value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
118 // For more details on COMDAT sections see e.g., http://www.airs.com/blog/archives/52
119 pub fn SetUniqueComdat(llmod: &Module, val: &'a Value) {
121 LLVMRustSetComdat(llmod, val, LLVMGetValueName(val));
125 pub fn UnsetComdat(val: &'a Value) {
127 LLVMRustUnsetComdat(val);
131 pub fn SetUnnamedAddr(global: &'a Value, unnamed: bool) {
133 LLVMSetUnnamedAddr(global, unnamed as Bool);
137 pub fn set_thread_local(global: &'a Value, is_thread_local: bool) {
139 LLVMSetThreadLocal(global, is_thread_local as Bool);
142 pub fn set_thread_local_mode(global: &'a Value, mode: ThreadLocalMode) {
144 LLVMSetThreadLocalMode(global, mode);
149 pub fn apply_llfn(&self, idx: AttributePlace, llfn: &Value) {
150 unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
153 pub fn apply_callsite(&self, idx: AttributePlace, callsite: &Value) {
154 unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
157 pub fn unapply_llfn(&self, idx: AttributePlace, llfn: &Value) {
158 unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
161 pub fn toggle_llfn(&self, idx: AttributePlace, llfn: &Value, set: bool) {
163 self.apply_llfn(idx, llfn);
165 self.unapply_llfn(idx, llfn);
170 // Memory-managed interface to object files.
172 pub struct ObjectFile {
173 pub llof: &'static mut ffi::ObjectFile,
176 unsafe impl Send for ObjectFile {}
179 // This will take ownership of llmb
180 pub fn new(llmb: &'static mut MemoryBuffer) -> Option<ObjectFile> {
182 let llof = LLVMCreateObjectFile(llmb)?;
183 Some(ObjectFile { llof })
188 impl Drop for ObjectFile {
191 LLVMDisposeObjectFile(&mut *(self.llof as *mut _));
196 // Memory-managed interface to section iterators.
198 pub struct SectionIter<'a> {
199 pub llsi: &'a mut SectionIterator<'a>,
202 impl Drop for SectionIter<'a> {
205 LLVMDisposeSectionIterator(&mut *(self.llsi as *mut _));
210 pub fn mk_section_iter(llof: &'a ffi::ObjectFile) -> SectionIter<'a> {
211 unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
214 /// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
215 pub fn get_param(llfn: &'a Value, index: c_uint) -> &'a Value {
217 assert!(index < LLVMCountParams(llfn),
218 "out of bounds argument access: {} out of {} arguments", index, LLVMCountParams(llfn));
219 LLVMGetParam(llfn, index)
223 pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
224 let sr = RustString {
225 bytes: RefCell::new(Vec::new()),
228 String::from_utf8(sr.bytes.into_inner())
231 pub fn twine_to_string(tr: &Twine) -> String {
233 build_string(|s| LLVMRustWriteTwineToString(tr, s))
234 .expect("got a non-UTF8 Twine from LLVM")
238 pub fn last_error() -> Option<String> {
240 let cstr = LLVMRustGetLastError();
244 let err = CStr::from_ptr(cstr).to_bytes();
245 let err = String::from_utf8_lossy(err).to_string();
246 libc::free(cstr as *mut _);
252 pub struct OperandBundleDef<'a> {
253 pub raw: &'a mut ffi::OperandBundleDef<'a>,
256 impl OperandBundleDef<'a> {
257 pub fn new(name: &str, vals: &[&'a Value]) -> Self {
258 let name = SmallCStr::new(name);
260 LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
262 OperandBundleDef { raw: def }
266 impl Drop for OperandBundleDef<'a> {
269 LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _));