1 #![allow(non_snake_case)]
3 pub use self::IntPredicate::*;
4 pub use self::RealPredicate::*;
5 pub use self::AtomicRmwBinOp::*;
6 pub use self::MetadataType::*;
7 pub use self::CodeGenOptSize::*;
8 pub use self::CallConv::*;
9 pub use self::Linkage::*;
11 use std::str::FromStr;
12 use std::string::FromUtf8Error;
15 use std::cell::RefCell;
16 use libc::{c_uint, c_char, size_t};
17 use rustc_data_structures::small_c_str::SmallCStr;
26 pub fn into_result(self) -> Result<(), ()> {
28 LLVMRustResult::Success => Ok(()),
29 LLVMRustResult::Failure => Err(()),
34 pub fn AddFunctionAttrStringValue(llfn: &'a Value,
39 LLVMRustAddFunctionAttrStringValue(llfn,
46 #[derive(Copy, Clone)]
47 pub enum AttributePlace {
54 pub fn as_uint(self) -> c_uint {
56 AttributePlace::ReturnValue => 0,
57 AttributePlace::Argument(i) => 1 + i,
58 AttributePlace::Function => !0,
63 #[derive(Copy, Clone, PartialEq)]
65 pub enum CodeGenOptSize {
66 CodeGenOptSizeNone = 0,
67 CodeGenOptSizeDefault = 1,
68 CodeGenOptSizeAggressive = 2,
71 impl FromStr for ArchiveKind {
74 fn from_str(s: &str) -> Result<Self, Self::Err> {
76 "gnu" => Ok(ArchiveKind::K_GNU),
77 "bsd" => Ok(ArchiveKind::K_BSD),
78 "coff" => Ok(ArchiveKind::K_COFF),
85 pub struct RustString {
86 bytes: RefCell<Vec<u8>>,
89 /// Appending to a Rust string -- used by RawRustStringOstream.
91 pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: &RustString,
94 let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
96 sr.bytes.borrow_mut().extend_from_slice(slice);
99 pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
101 LLVMSetInstructionCallConv(instr, cc as c_uint);
104 pub fn SetFunctionCallConv(fn_: &'a Value, cc: CallConv) {
106 LLVMSetFunctionCallConv(fn_, cc as c_uint);
110 // Externally visible symbols that might appear in multiple codegen units need to appear in
111 // their own comdat section so that the duplicates can be discarded at link time. This can for
112 // example happen for generics when using multiple codegen units. This function simply uses the
113 // value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
115 // For more details on COMDAT sections see e.g., http://www.airs.com/blog/archives/52
116 pub fn SetUniqueComdat(llmod: &Module, val: &'a Value) {
118 let name = get_value_name(val);
119 LLVMRustSetComdat(llmod, val, name.as_ptr().cast(), name.len());
123 pub fn UnsetComdat(val: &'a Value) {
125 LLVMRustUnsetComdat(val);
129 pub fn SetUnnamedAddr(global: &'a Value, unnamed: bool) {
131 LLVMSetUnnamedAddr(global, unnamed as Bool);
135 pub fn set_thread_local(global: &'a Value, is_thread_local: bool) {
137 LLVMSetThreadLocal(global, is_thread_local as Bool);
140 pub fn set_thread_local_mode(global: &'a Value, mode: ThreadLocalMode) {
142 LLVMSetThreadLocalMode(global, mode);
147 pub fn apply_llfn(&self, idx: AttributePlace, llfn: &Value) {
148 unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
151 pub fn apply_callsite(&self, idx: AttributePlace, callsite: &Value) {
152 unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
155 pub fn unapply_llfn(&self, idx: AttributePlace, llfn: &Value) {
156 unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
159 pub fn toggle_llfn(&self, idx: AttributePlace, llfn: &Value, set: bool) {
161 self.apply_llfn(idx, llfn);
163 self.unapply_llfn(idx, llfn);
168 // Memory-managed interface to object files.
170 pub struct ObjectFile {
171 pub llof: &'static mut ffi::ObjectFile,
174 unsafe impl Send for ObjectFile {}
177 // This will take ownership of llmb
178 pub fn new(llmb: &'static mut MemoryBuffer) -> Option<ObjectFile> {
180 let llof = LLVMCreateObjectFile(llmb)?;
181 Some(ObjectFile { llof })
186 impl Drop for ObjectFile {
189 LLVMDisposeObjectFile(&mut *(self.llof as *mut _));
194 // Memory-managed interface to section iterators.
196 pub struct SectionIter<'a> {
197 pub llsi: &'a mut SectionIterator<'a>,
200 impl Drop for SectionIter<'a> {
203 LLVMDisposeSectionIterator(&mut *(self.llsi as *mut _));
208 pub fn mk_section_iter(llof: &'a ffi::ObjectFile) -> SectionIter<'a> {
209 unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
212 /// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
213 pub fn get_param(llfn: &'a Value, index: c_uint) -> &'a Value {
215 assert!(index < LLVMCountParams(llfn),
216 "out of bounds argument access: {} out of {} arguments", index, LLVMCountParams(llfn));
217 LLVMGetParam(llfn, index)
221 /// Safe wrapper for `LLVMGetValueName2` into a byte slice
222 pub fn get_value_name(value: &'a Value) -> &'a [u8] {
225 let data = LLVMGetValueName2(value, &mut len);
226 std::slice::from_raw_parts(data.cast(), len)
230 /// Safe wrapper for `LLVMSetValueName2` from a byte slice
231 pub fn set_value_name(value: &Value, name: &[u8]) {
233 let data = name.as_ptr().cast();
234 LLVMSetValueName2(value, data, name.len());
238 pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
239 let sr = RustString {
240 bytes: RefCell::new(Vec::new()),
243 String::from_utf8(sr.bytes.into_inner())
246 pub fn twine_to_string(tr: &Twine) -> String {
248 build_string(|s| LLVMRustWriteTwineToString(tr, s))
249 .expect("got a non-UTF8 Twine from LLVM")
253 pub fn last_error() -> Option<String> {
255 let cstr = LLVMRustGetLastError();
259 let err = CStr::from_ptr(cstr).to_bytes();
260 let err = String::from_utf8_lossy(err).to_string();
261 libc::free(cstr as *mut _);
267 pub struct OperandBundleDef<'a> {
268 pub raw: &'a mut ffi::OperandBundleDef<'a>,
271 impl OperandBundleDef<'a> {
272 pub fn new(name: &str, vals: &[&'a Value]) -> Self {
273 let name = SmallCStr::new(name);
275 LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
277 OperandBundleDef { raw: def }
281 impl Drop for OperandBundleDef<'a> {
284 LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _));