]> git.proxmox.com Git - rustc.git/blame - src/librustc_codegen_llvm/llvm/mod.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_codegen_llvm / llvm / mod.rs
CommitLineData
b7449926 1#![allow(non_snake_case)]
b7449926 2
b7449926 3pub use self::AtomicRmwBinOp::*;
b7449926 4pub use self::CallConv::*;
dfeec247
XL
5pub use self::CodeGenOptSize::*;
6pub use self::IntPredicate::*;
b7449926 7pub use self::Linkage::*;
dfeec247
XL
8pub use self::MetadataType::*;
9pub use self::RealPredicate::*;
b7449926 10
60c5eb7d 11use libc::c_uint;
b7449926 12use rustc_data_structures::small_c_str::SmallCStr;
60c5eb7d 13use rustc_llvm::RustString;
dfeec247 14use std::cell::RefCell;
3dfed10e 15use std::ffi::{CStr, CString};
dfeec247
XL
16use std::str::FromStr;
17use std::string::FromUtf8Error;
b7449926
XL
18
19pub mod archive_ro;
20pub mod diagnostic;
21mod ffi;
22
23pub use self::ffi::*;
24
25impl LLVMRustResult {
26 pub fn into_result(self) -> Result<(), ()> {
27 match self {
28 LLVMRustResult::Success => Ok(()),
29 LLVMRustResult::Failure => Err(()),
30 }
31 }
32}
33
dfeec247 34pub fn AddFunctionAttrStringValue(llfn: &'a Value, idx: AttributePlace, attr: &CStr, value: &CStr) {
b7449926 35 unsafe {
dfeec247 36 LLVMRustAddFunctionAttrStringValue(llfn, idx.as_uint(), attr.as_ptr(), value.as_ptr())
b7449926
XL
37 }
38}
39
40#[derive(Copy, Clone)]
41pub enum AttributePlace {
42 ReturnValue,
43 Argument(u32),
44 Function,
45}
46
47impl AttributePlace {
48 pub fn as_uint(self) -> c_uint {
49 match self {
50 AttributePlace::ReturnValue => 0,
51 AttributePlace::Argument(i) => 1 + i,
52 AttributePlace::Function => !0,
53 }
54 }
55}
56
57#[derive(Copy, Clone, PartialEq)]
58#[repr(C)]
59pub enum CodeGenOptSize {
60 CodeGenOptSizeNone = 0,
61 CodeGenOptSizeDefault = 1,
62 CodeGenOptSizeAggressive = 2,
63}
64
65impl FromStr for ArchiveKind {
66 type Err = ();
67
68 fn from_str(s: &str) -> Result<Self, Self::Err> {
69 match s {
70 "gnu" => Ok(ArchiveKind::K_GNU),
71 "bsd" => Ok(ArchiveKind::K_BSD),
74b04a01 72 "darwin" => Ok(ArchiveKind::K_DARWIN),
b7449926
XL
73 "coff" => Ok(ArchiveKind::K_COFF),
74 _ => Err(()),
75 }
76 }
77}
78
b7449926
XL
79pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
80 unsafe {
81 LLVMSetInstructionCallConv(instr, cc as c_uint);
82 }
83}
84pub fn SetFunctionCallConv(fn_: &'a Value, cc: CallConv) {
85 unsafe {
86 LLVMSetFunctionCallConv(fn_, cc as c_uint);
87 }
88}
89
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
94// function.
0731742a 95// For more details on COMDAT sections see e.g., http://www.airs.com/blog/archives/52
b7449926
XL
96pub fn SetUniqueComdat(llmod: &Module, val: &'a Value) {
97 unsafe {
60c5eb7d
XL
98 let name = get_value_name(val);
99 LLVMRustSetComdat(llmod, val, name.as_ptr().cast(), name.len());
b7449926
XL
100 }
101}
102
103pub fn UnsetComdat(val: &'a Value) {
104 unsafe {
105 LLVMRustUnsetComdat(val);
106 }
107}
108
ba9703b0 109pub fn SetUnnamedAddress(global: &'a Value, unnamed: UnnamedAddr) {
b7449926 110 unsafe {
ba9703b0 111 LLVMSetUnnamedAddress(global, unnamed);
b7449926
XL
112 }
113}
114
115pub fn set_thread_local(global: &'a Value, is_thread_local: bool) {
116 unsafe {
117 LLVMSetThreadLocal(global, is_thread_local as Bool);
118 }
119}
120pub fn set_thread_local_mode(global: &'a Value, mode: ThreadLocalMode) {
121 unsafe {
122 LLVMSetThreadLocalMode(global, mode);
123 }
124}
125
126impl Attribute {
127 pub fn apply_llfn(&self, idx: AttributePlace, llfn: &Value) {
128 unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
129 }
130
131 pub fn apply_callsite(&self, idx: AttributePlace, callsite: &Value) {
132 unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
133 }
134
135 pub fn unapply_llfn(&self, idx: AttributePlace, llfn: &Value) {
136 unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
137 }
138
139 pub fn toggle_llfn(&self, idx: AttributePlace, llfn: &Value, set: bool) {
140 if set {
141 self.apply_llfn(idx, llfn);
142 } else {
143 self.unapply_llfn(idx, llfn);
144 }
145 }
146}
147
148// Memory-managed interface to object files.
149
150pub struct ObjectFile {
151 pub llof: &'static mut ffi::ObjectFile,
152}
153
154unsafe impl Send for ObjectFile {}
155
156impl ObjectFile {
157 // This will take ownership of llmb
158 pub fn new(llmb: &'static mut MemoryBuffer) -> Option<ObjectFile> {
159 unsafe {
160 let llof = LLVMCreateObjectFile(llmb)?;
a1dfa0c6 161 Some(ObjectFile { llof })
b7449926
XL
162 }
163 }
164}
165
166impl Drop for ObjectFile {
167 fn drop(&mut self) {
168 unsafe {
169 LLVMDisposeObjectFile(&mut *(self.llof as *mut _));
170 }
171 }
172}
173
174// Memory-managed interface to section iterators.
175
176pub struct SectionIter<'a> {
177 pub llsi: &'a mut SectionIterator<'a>,
178}
179
180impl Drop for SectionIter<'a> {
181 fn drop(&mut self) {
182 unsafe {
183 LLVMDisposeSectionIterator(&mut *(self.llsi as *mut _));
184 }
185 }
186}
187
ba9703b0 188pub fn mk_section_iter(llof: &ffi::ObjectFile) -> SectionIter<'_> {
b7449926
XL
189 unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
190}
191
3dfed10e
XL
192pub fn set_section(llglobal: &Value, section_name: &str) {
193 let section_name_cstr = CString::new(section_name).expect("unexpected CString error");
194 unsafe {
195 LLVMSetSection(llglobal, section_name_cstr.as_ptr());
196 }
197}
198
199pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name: &str) -> &'a Value {
200 let name_cstr = CString::new(name).expect("unexpected CString error");
201 unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
202}
203
204pub fn set_initializer(llglobal: &Value, constant_val: &Value) {
205 unsafe {
206 LLVMSetInitializer(llglobal, constant_val);
207 }
208}
209
210pub fn set_global_constant(llglobal: &Value, is_constant: bool) {
211 unsafe {
212 LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
213 }
214}
215
216pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
217 unsafe {
218 LLVMRustSetLinkage(llglobal, linkage);
219 }
220}
221
222pub fn set_alignment(llglobal: &Value, bytes: usize) {
223 unsafe {
224 ffi::LLVMSetAlignment(llglobal, bytes as c_uint);
225 }
226}
227
b7449926 228/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
ba9703b0 229pub fn get_param(llfn: &Value, index: c_uint) -> &Value {
b7449926 230 unsafe {
dfeec247
XL
231 assert!(
232 index < LLVMCountParams(llfn),
233 "out of bounds argument access: {} out of {} arguments",
234 index,
235 LLVMCountParams(llfn)
236 );
b7449926
XL
237 LLVMGetParam(llfn, index)
238 }
239}
240
60c5eb7d 241/// Safe wrapper for `LLVMGetValueName2` into a byte slice
ba9703b0 242pub fn get_value_name(value: &Value) -> &[u8] {
60c5eb7d
XL
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
251pub 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
b7449926 258pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
dfeec247 259 let sr = RustString { bytes: RefCell::new(Vec::new()) };
b7449926
XL
260 f(&sr);
261 String::from_utf8(sr.bytes.into_inner())
262}
263
3dfed10e
XL
264pub 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
b7449926
XL
270pub fn twine_to_string(tr: &Twine) -> String {
271 unsafe {
dfeec247 272 build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
b7449926
XL
273 }
274}
275
276pub 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
290pub struct OperandBundleDef<'a> {
291 pub raw: &'a mut ffi::OperandBundleDef<'a>,
292}
293
294impl 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
304impl Drop for OperandBundleDef<'a> {
305 fn drop(&mut self) {
306 unsafe {
307 LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _));
308 }
309 }
310}