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