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