1 #![allow(non_snake_case)]
3 pub use self::AtomicRmwBinOp::*;
4 pub use self::CallConv::*;
5 pub use self::CodeGenOptSize::*;
6 pub use self::IntPredicate::*;
7 pub use self::Linkage::*;
8 pub use self::MetadataType::*;
9 pub use self::RealPredicate::*;
12 use rustc_data_structures::small_c_str::SmallCStr;
13 use rustc_llvm::RustString;
14 use std::cell::RefCell;
16 use std::str::FromStr;
17 use std::string::FromUtf8Error;
26 pub fn into_result(self) -> Result<(), ()> {
28 LLVMRustResult::Success => Ok(()),
29 LLVMRustResult::Failure => Err(()),
34 pub fn AddFunctionAttrStringValue(llfn: &'a Value, idx: AttributePlace, attr: &CStr, value: &CStr) {
36 LLVMRustAddFunctionAttrStringValue(llfn, idx.as_uint(), attr.as_ptr(), value.as_ptr())
40 #[derive(Copy, Clone)]
41 pub enum AttributePlace {
48 pub fn as_uint(self) -> c_uint {
50 AttributePlace::ReturnValue => 0,
51 AttributePlace::Argument(i) => 1 + i,
52 AttributePlace::Function => !0,
57 #[derive(Copy, Clone, PartialEq)]
59 pub enum CodeGenOptSize {
60 CodeGenOptSizeNone = 0,
61 CodeGenOptSizeDefault = 1,
62 CodeGenOptSizeAggressive = 2,
65 impl FromStr for ArchiveKind {
68 fn from_str(s: &str) -> Result<Self, Self::Err> {
70 "gnu" => Ok(ArchiveKind::K_GNU),
71 "bsd" => Ok(ArchiveKind::K_BSD),
72 "darwin" => Ok(ArchiveKind::K_DARWIN),
73 "coff" => Ok(ArchiveKind::K_COFF),
79 pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
81 LLVMSetInstructionCallConv(instr, cc as c_uint);
84 pub fn SetFunctionCallConv(fn_: &'a Value, cc: CallConv) {
86 LLVMSetFunctionCallConv(fn_, cc as c_uint);
90 // Externally visible symbols that might appear in multiple codegen units need to appear in
91 // their own comdat section so that the duplicates can be discarded at link time. This can for
92 // example happen for generics when using multiple codegen units. This function simply uses the
93 // value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
95 // For more details on COMDAT sections see e.g., http://www.airs.com/blog/archives/52
96 pub fn SetUniqueComdat(llmod: &Module, val: &'a Value) {
98 let name = get_value_name(val);
99 LLVMRustSetComdat(llmod, val, name.as_ptr().cast(), name.len());
103 pub fn UnsetComdat(val: &'a Value) {
105 LLVMRustUnsetComdat(val);
109 pub fn SetUnnamedAddr(global: &'a Value, unnamed: bool) {
111 LLVMSetUnnamedAddr(global, unnamed as Bool);
115 pub fn set_thread_local(global: &'a Value, is_thread_local: bool) {
117 LLVMSetThreadLocal(global, is_thread_local as Bool);
120 pub fn set_thread_local_mode(global: &'a Value, mode: ThreadLocalMode) {
122 LLVMSetThreadLocalMode(global, mode);
127 pub fn apply_llfn(&self, idx: AttributePlace, llfn: &Value) {
128 unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
131 pub fn apply_callsite(&self, idx: AttributePlace, callsite: &Value) {
132 unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
135 pub fn unapply_llfn(&self, idx: AttributePlace, llfn: &Value) {
136 unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
139 pub fn toggle_llfn(&self, idx: AttributePlace, llfn: &Value, set: bool) {
141 self.apply_llfn(idx, llfn);
143 self.unapply_llfn(idx, llfn);
148 // Memory-managed interface to object files.
150 pub struct ObjectFile {
151 pub llof: &'static mut ffi::ObjectFile,
154 unsafe impl Send for ObjectFile {}
157 // This will take ownership of llmb
158 pub fn new(llmb: &'static mut MemoryBuffer) -> Option<ObjectFile> {
160 let llof = LLVMCreateObjectFile(llmb)?;
161 Some(ObjectFile { llof })
166 impl Drop for ObjectFile {
169 LLVMDisposeObjectFile(&mut *(self.llof as *mut _));
174 // Memory-managed interface to section iterators.
176 pub struct SectionIter<'a> {
177 pub llsi: &'a mut SectionIterator<'a>,
180 impl Drop for SectionIter<'a> {
183 LLVMDisposeSectionIterator(&mut *(self.llsi as *mut _));
188 pub fn mk_section_iter(llof: &'a ffi::ObjectFile) -> SectionIter<'a> {
189 unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
192 /// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
193 pub fn get_param(llfn: &'a Value, index: c_uint) -> &'a Value {
196 index < LLVMCountParams(llfn),
197 "out of bounds argument access: {} out of {} arguments",
199 LLVMCountParams(llfn)
201 LLVMGetParam(llfn, index)
205 /// Safe wrapper for `LLVMGetValueName2` into a byte slice
206 pub fn get_value_name(value: &'a Value) -> &'a [u8] {
209 let data = LLVMGetValueName2(value, &mut len);
210 std::slice::from_raw_parts(data.cast(), len)
214 /// Safe wrapper for `LLVMSetValueName2` from a byte slice
215 pub fn set_value_name(value: &Value, name: &[u8]) {
217 let data = name.as_ptr().cast();
218 LLVMSetValueName2(value, data, name.len());
222 pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
223 let sr = RustString { bytes: RefCell::new(Vec::new()) };
225 String::from_utf8(sr.bytes.into_inner())
228 pub fn twine_to_string(tr: &Twine) -> String {
230 build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
234 pub fn last_error() -> Option<String> {
236 let cstr = LLVMRustGetLastError();
240 let err = CStr::from_ptr(cstr).to_bytes();
241 let err = String::from_utf8_lossy(err).to_string();
242 libc::free(cstr as *mut _);
248 pub struct OperandBundleDef<'a> {
249 pub raw: &'a mut ffi::OperandBundleDef<'a>,
252 impl OperandBundleDef<'a> {
253 pub fn new(name: &str, vals: &[&'a Value]) -> Self {
254 let name = SmallCStr::new(name);
256 LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
258 OperandBundleDef { raw: def }
262 impl Drop for OperandBundleDef<'a> {
265 LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _));