]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/llvm/mod.rs
Move `{core,std}::stream::Stream` to `{core,std}::async_iter::AsyncIterator`.
[rust.git] / compiler / rustc_codegen_llvm / src / llvm / mod.rs
1 #![allow(non_snake_case)]
2
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::*;
10
11 use libc::c_uint;
12 use rustc_data_structures::small_c_str::SmallCStr;
13 use rustc_llvm::RustString;
14 use std::cell::RefCell;
15 use std::ffi::{CStr, CString};
16 use std::str::FromStr;
17 use std::string::FromUtf8Error;
18
19 pub mod archive_ro;
20 pub mod diagnostic;
21 mod ffi;
22
23 pub use self::ffi::*;
24
25 impl LLVMRustResult {
26     pub fn into_result(self) -> Result<(), ()> {
27         match self {
28             LLVMRustResult::Success => Ok(()),
29             LLVMRustResult::Failure => Err(()),
30         }
31     }
32 }
33
34 pub fn AddFunctionAttrStringValue(llfn: &Value, idx: AttributePlace, attr: &CStr, value: &CStr) {
35     unsafe {
36         LLVMRustAddFunctionAttrStringValue(llfn, idx.as_uint(), attr.as_ptr(), value.as_ptr())
37     }
38 }
39
40 pub fn AddFunctionAttrString(llfn: &Value, idx: AttributePlace, attr: &CStr) {
41     unsafe {
42         LLVMRustAddFunctionAttrStringValue(llfn, idx.as_uint(), attr.as_ptr(), std::ptr::null())
43     }
44 }
45
46 pub fn AddCallSiteAttrString(callsite: &Value, idx: AttributePlace, attr: &CStr) {
47     unsafe { LLVMRustAddCallSiteAttrString(callsite, idx.as_uint(), attr.as_ptr()) }
48 }
49
50 #[derive(Copy, Clone)]
51 pub enum AttributePlace {
52     ReturnValue,
53     Argument(u32),
54     Function,
55 }
56
57 impl AttributePlace {
58     pub fn as_uint(self) -> c_uint {
59         match self {
60             AttributePlace::ReturnValue => 0,
61             AttributePlace::Argument(i) => 1 + i,
62             AttributePlace::Function => !0,
63         }
64     }
65 }
66
67 #[derive(Copy, Clone, PartialEq)]
68 #[repr(C)]
69 pub enum CodeGenOptSize {
70     CodeGenOptSizeNone = 0,
71     CodeGenOptSizeDefault = 1,
72     CodeGenOptSizeAggressive = 2,
73 }
74
75 impl FromStr for ArchiveKind {
76     type Err = ();
77
78     fn from_str(s: &str) -> Result<Self, Self::Err> {
79         match s {
80             "gnu" => Ok(ArchiveKind::K_GNU),
81             "bsd" => Ok(ArchiveKind::K_BSD),
82             "darwin" => Ok(ArchiveKind::K_DARWIN),
83             "coff" => Ok(ArchiveKind::K_COFF),
84             _ => Err(()),
85         }
86     }
87 }
88
89 pub fn SetInstructionCallConv(instr: &Value, cc: CallConv) {
90     unsafe {
91         LLVMSetInstructionCallConv(instr, cc as c_uint);
92     }
93 }
94 pub fn SetFunctionCallConv(fn_: &Value, cc: CallConv) {
95     unsafe {
96         LLVMSetFunctionCallConv(fn_, cc as c_uint);
97     }
98 }
99
100 // Externally visible symbols that might appear in multiple codegen units need to appear in
101 // their own comdat section so that the duplicates can be discarded at link time. This can for
102 // example happen for generics when using multiple codegen units. This function simply uses the
103 // value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
104 // function.
105 // For more details on COMDAT sections see e.g., https://www.airs.com/blog/archives/52
106 pub fn SetUniqueComdat(llmod: &Module, val: &Value) {
107     unsafe {
108         let name = get_value_name(val);
109         LLVMRustSetComdat(llmod, val, name.as_ptr().cast(), name.len());
110     }
111 }
112
113 pub fn UnsetComdat(val: &Value) {
114     unsafe {
115         LLVMRustUnsetComdat(val);
116     }
117 }
118
119 pub fn SetUnnamedAddress(global: &Value, unnamed: UnnamedAddr) {
120     unsafe {
121         LLVMSetUnnamedAddress(global, unnamed);
122     }
123 }
124
125 pub fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) {
126     unsafe {
127         LLVMSetThreadLocalMode(global, mode);
128     }
129 }
130
131 impl Attribute {
132     pub fn apply_llfn(&self, idx: AttributePlace, llfn: &Value) {
133         unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
134     }
135
136     pub fn apply_callsite(&self, idx: AttributePlace, callsite: &Value) {
137         unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
138     }
139
140     pub fn unapply_llfn(&self, idx: AttributePlace, llfn: &Value) {
141         unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
142     }
143
144     pub fn toggle_llfn(&self, idx: AttributePlace, llfn: &Value, set: bool) {
145         if set {
146             self.apply_llfn(idx, llfn);
147         } else {
148             self.unapply_llfn(idx, llfn);
149         }
150     }
151 }
152
153 pub fn set_section(llglobal: &Value, section_name: &str) {
154     let section_name_cstr = CString::new(section_name).expect("unexpected CString error");
155     unsafe {
156         LLVMSetSection(llglobal, section_name_cstr.as_ptr());
157     }
158 }
159
160 pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name: &str) -> &'a Value {
161     let name_cstr = CString::new(name).expect("unexpected CString error");
162     unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
163 }
164
165 pub fn set_initializer(llglobal: &Value, constant_val: &Value) {
166     unsafe {
167         LLVMSetInitializer(llglobal, constant_val);
168     }
169 }
170
171 pub fn set_global_constant(llglobal: &Value, is_constant: bool) {
172     unsafe {
173         LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
174     }
175 }
176
177 pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
178     unsafe {
179         LLVMRustSetLinkage(llglobal, linkage);
180     }
181 }
182
183 pub fn set_visibility(llglobal: &Value, visibility: Visibility) {
184     unsafe {
185         LLVMRustSetVisibility(llglobal, visibility);
186     }
187 }
188
189 pub fn set_alignment(llglobal: &Value, bytes: usize) {
190     unsafe {
191         ffi::LLVMSetAlignment(llglobal, bytes as c_uint);
192     }
193 }
194
195 pub fn set_comdat(llmod: &Module, llglobal: &Value, name: &str) {
196     unsafe {
197         LLVMRustSetComdat(llmod, llglobal, name.as_ptr().cast(), name.len());
198     }
199 }
200
201 /// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
202 pub fn get_param(llfn: &Value, index: c_uint) -> &Value {
203     unsafe {
204         assert!(
205             index < LLVMCountParams(llfn),
206             "out of bounds argument access: {} out of {} arguments",
207             index,
208             LLVMCountParams(llfn)
209         );
210         LLVMGetParam(llfn, index)
211     }
212 }
213
214 /// Safe wrapper for `LLVMGetValueName2` into a byte slice
215 pub fn get_value_name(value: &Value) -> &[u8] {
216     unsafe {
217         let mut len = 0;
218         let data = LLVMGetValueName2(value, &mut len);
219         std::slice::from_raw_parts(data.cast(), len)
220     }
221 }
222
223 /// Safe wrapper for `LLVMSetValueName2` from a byte slice
224 pub fn set_value_name(value: &Value, name: &[u8]) {
225     unsafe {
226         let data = name.as_ptr().cast();
227         LLVMSetValueName2(value, data, name.len());
228     }
229 }
230
231 pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
232     let sr = RustString { bytes: RefCell::new(Vec::new()) };
233     f(&sr);
234     String::from_utf8(sr.bytes.into_inner())
235 }
236
237 pub fn build_byte_buffer(f: impl FnOnce(&RustString)) -> Vec<u8> {
238     let sr = RustString { bytes: RefCell::new(Vec::new()) };
239     f(&sr);
240     sr.bytes.into_inner()
241 }
242
243 pub fn twine_to_string(tr: &Twine) -> String {
244     unsafe {
245         build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
246     }
247 }
248
249 pub fn last_error() -> Option<String> {
250     unsafe {
251         let cstr = LLVMRustGetLastError();
252         if cstr.is_null() {
253             None
254         } else {
255             let err = CStr::from_ptr(cstr).to_bytes();
256             let err = String::from_utf8_lossy(err).to_string();
257             libc::free(cstr as *mut _);
258             Some(err)
259         }
260     }
261 }
262
263 pub struct OperandBundleDef<'a> {
264     pub raw: &'a mut ffi::OperandBundleDef<'a>,
265 }
266
267 impl<'a> OperandBundleDef<'a> {
268     pub fn new(name: &str, vals: &[&'a Value]) -> Self {
269         let name = SmallCStr::new(name);
270         let def = unsafe {
271             LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
272         };
273         OperandBundleDef { raw: def }
274     }
275 }
276
277 impl Drop for OperandBundleDef<'_> {
278     fn drop(&mut self) {
279         unsafe {
280             LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _));
281         }
282     }
283 }