]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | #![allow(non_snake_case)] |
b7449926 | 2 | |
b7449926 | 3 | pub use self::AtomicRmwBinOp::*; |
b7449926 | 4 | pub use self::CallConv::*; |
dfeec247 XL |
5 | pub use self::CodeGenOptSize::*; |
6 | pub use self::IntPredicate::*; | |
b7449926 | 7 | pub use self::Linkage::*; |
dfeec247 XL |
8 | pub use self::MetadataType::*; |
9 | pub use self::RealPredicate::*; | |
b7449926 | 10 | |
60c5eb7d | 11 | use libc::c_uint; |
b7449926 | 12 | use rustc_data_structures::small_c_str::SmallCStr; |
60c5eb7d | 13 | use rustc_llvm::RustString; |
dfeec247 | 14 | use std::cell::RefCell; |
3dfed10e | 15 | use std::ffi::{CStr, CString}; |
dfeec247 XL |
16 | use std::str::FromStr; |
17 | use std::string::FromUtf8Error; | |
b7449926 XL |
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 | ||
a2a8927a | 34 | pub fn AddFunctionAttrStringValue(llfn: &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 | ||
a2a8927a | 40 | pub fn AddFunctionAttrString(llfn: &Value, idx: AttributePlace, attr: &CStr) { |
1b1a35ee XL |
41 | unsafe { |
42 | LLVMRustAddFunctionAttrStringValue(llfn, idx.as_uint(), attr.as_ptr(), std::ptr::null()) | |
43 | } | |
44 | } | |
45 | ||
5869c6ff XL |
46 | pub fn AddCallSiteAttrString(callsite: &Value, idx: AttributePlace, attr: &CStr) { |
47 | unsafe { LLVMRustAddCallSiteAttrString(callsite, idx.as_uint(), attr.as_ptr()) } | |
48 | } | |
49 | ||
b7449926 XL |
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), | |
74b04a01 | 82 | "darwin" => Ok(ArchiveKind::K_DARWIN), |
b7449926 XL |
83 | "coff" => Ok(ArchiveKind::K_COFF), |
84 | _ => Err(()), | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
a2a8927a | 89 | pub fn SetInstructionCallConv(instr: &Value, cc: CallConv) { |
b7449926 XL |
90 | unsafe { |
91 | LLVMSetInstructionCallConv(instr, cc as c_uint); | |
92 | } | |
93 | } | |
a2a8927a | 94 | pub fn SetFunctionCallConv(fn_: &Value, cc: CallConv) { |
b7449926 XL |
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. | |
136023e0 | 105 | // For more details on COMDAT sections see e.g., https://www.airs.com/blog/archives/52 |
a2a8927a | 106 | pub fn SetUniqueComdat(llmod: &Module, val: &Value) { |
b7449926 | 107 | unsafe { |
60c5eb7d XL |
108 | let name = get_value_name(val); |
109 | LLVMRustSetComdat(llmod, val, name.as_ptr().cast(), name.len()); | |
b7449926 XL |
110 | } |
111 | } | |
112 | ||
a2a8927a | 113 | pub fn UnsetComdat(val: &Value) { |
b7449926 XL |
114 | unsafe { |
115 | LLVMRustUnsetComdat(val); | |
116 | } | |
117 | } | |
118 | ||
a2a8927a | 119 | pub fn SetUnnamedAddress(global: &Value, unnamed: UnnamedAddr) { |
b7449926 | 120 | unsafe { |
ba9703b0 | 121 | LLVMSetUnnamedAddress(global, unnamed); |
b7449926 XL |
122 | } |
123 | } | |
124 | ||
a2a8927a | 125 | pub fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) { |
b7449926 XL |
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 | ||
3dfed10e XL |
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 | ||
fc512014 XL |
183 | pub fn set_visibility(llglobal: &Value, visibility: Visibility) { |
184 | unsafe { | |
185 | LLVMRustSetVisibility(llglobal, visibility); | |
186 | } | |
187 | } | |
188 | ||
3dfed10e XL |
189 | pub fn set_alignment(llglobal: &Value, bytes: usize) { |
190 | unsafe { | |
191 | ffi::LLVMSetAlignment(llglobal, bytes as c_uint); | |
192 | } | |
193 | } | |
194 | ||
fc512014 XL |
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 | ||
b7449926 | 201 | /// Safe wrapper around `LLVMGetParam`, because segfaults are no fun. |
ba9703b0 | 202 | pub fn get_param(llfn: &Value, index: c_uint) -> &Value { |
b7449926 | 203 | unsafe { |
dfeec247 XL |
204 | assert!( |
205 | index < LLVMCountParams(llfn), | |
206 | "out of bounds argument access: {} out of {} arguments", | |
207 | index, | |
208 | LLVMCountParams(llfn) | |
209 | ); | |
b7449926 XL |
210 | LLVMGetParam(llfn, index) |
211 | } | |
212 | } | |
213 | ||
60c5eb7d | 214 | /// Safe wrapper for `LLVMGetValueName2` into a byte slice |
ba9703b0 | 215 | pub fn get_value_name(value: &Value) -> &[u8] { |
60c5eb7d XL |
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 | ||
b7449926 | 231 | pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> { |
dfeec247 | 232 | let sr = RustString { bytes: RefCell::new(Vec::new()) }; |
b7449926 XL |
233 | f(&sr); |
234 | String::from_utf8(sr.bytes.into_inner()) | |
235 | } | |
236 | ||
3dfed10e XL |
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 | ||
b7449926 XL |
243 | pub fn twine_to_string(tr: &Twine) -> String { |
244 | unsafe { | |
dfeec247 | 245 | build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM") |
b7449926 XL |
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 | ||
a2a8927a | 267 | impl<'a> OperandBundleDef<'a> { |
b7449926 XL |
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 | ||
a2a8927a | 277 | impl Drop for OperandBundleDef<'_> { |
b7449926 XL |
278 | fn drop(&mut self) { |
279 | unsafe { | |
280 | LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _)); | |
281 | } | |
282 | } | |
283 | } |