2018-05-29 20:41:36 +03:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
pub use self::AtomicRmwBinOp::*;
|
|
|
|
pub use self::CallConv::*;
|
|
|
|
pub use self::CodeGenOptSize::*;
|
|
|
|
pub use self::IntPredicate::*;
|
|
|
|
pub use self::Linkage::*;
|
|
|
|
pub use self::MetadataType::*;
|
|
|
|
pub use self::RealPredicate::*;
|
|
|
|
|
2019-12-12 10:51:19 -05:00
|
|
|
use libc::c_uint;
|
2018-08-07 16:04:34 +02:00
|
|
|
use rustc_data_structures::small_c_str::SmallCStr;
|
2019-12-12 10:51:19 -05:00
|
|
|
use rustc_llvm::RustString;
|
2024-03-24 01:03:39 +00:00
|
|
|
use rustc_target::abi::Align;
|
2018-05-29 20:41:36 +03:00
|
|
|
use std::cell::RefCell;
|
2020-07-02 11:27:15 -07:00
|
|
|
use std::ffi::{CStr, CString};
|
2018-05-29 20:41:36 +03:00
|
|
|
use std::str::FromStr;
|
2018-07-13 14:43:12 +03:00
|
|
|
use std::string::FromUtf8Error;
|
2018-05-29 20:41:36 +03:00
|
|
|
|
|
|
|
pub mod archive_ro;
|
|
|
|
pub mod diagnostic;
|
|
|
|
mod ffi;
|
|
|
|
|
|
|
|
pub use self::ffi::*;
|
|
|
|
|
|
|
|
impl LLVMRustResult {
|
|
|
|
pub fn into_result(self) -> Result<(), ()> {
|
|
|
|
match self {
|
|
|
|
LLVMRustResult::Success => Ok(()),
|
|
|
|
LLVMRustResult::Failure => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:16 -05:00
|
|
|
pub fn AddFunctionAttributes<'ll>(llfn: &'ll Value, idx: AttributePlace, attrs: &[&'ll Attribute]) {
|
|
|
|
unsafe {
|
|
|
|
LLVMRustAddFunctionAttributes(llfn, idx.as_uint(), attrs.as_ptr(), attrs.len());
|
|
|
|
}
|
2022-02-14 14:01:19 -05:00
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:16 -05:00
|
|
|
pub fn AddCallSiteAttributes<'ll>(
|
|
|
|
callsite: &'ll Value,
|
|
|
|
idx: AttributePlace,
|
|
|
|
attrs: &[&'ll Attribute],
|
|
|
|
) {
|
2020-09-28 21:10:38 +01:00
|
|
|
unsafe {
|
2022-02-21 11:19:16 -05:00
|
|
|
LLVMRustAddCallSiteAttributes(callsite, idx.as_uint(), attrs.as_ptr(), attrs.len());
|
2020-09-28 21:10:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 00:00:00 +00:00
|
|
|
pub fn CreateAttrStringValue<'ll>(llcx: &'ll Context, attr: &str, value: &str) -> &'ll Attribute {
|
|
|
|
unsafe {
|
|
|
|
LLVMCreateStringAttribute(
|
|
|
|
llcx,
|
|
|
|
attr.as_ptr().cast(),
|
|
|
|
attr.len().try_into().unwrap(),
|
|
|
|
value.as_ptr().cast(),
|
|
|
|
value.len().try_into().unwrap(),
|
|
|
|
)
|
|
|
|
}
|
2022-02-21 11:19:16 -05:00
|
|
|
}
|
|
|
|
|
2022-03-03 00:00:00 +00:00
|
|
|
pub fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &str) -> &'ll Attribute {
|
|
|
|
unsafe {
|
|
|
|
LLVMCreateStringAttribute(
|
|
|
|
llcx,
|
|
|
|
attr.as_ptr().cast(),
|
|
|
|
attr.len().try_into().unwrap(),
|
|
|
|
std::ptr::null(),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
}
|
2022-02-21 11:19:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn CreateAlignmentAttr(llcx: &Context, bytes: u64) -> &Attribute {
|
|
|
|
unsafe { LLVMRustCreateAlignmentAttr(llcx, bytes) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn CreateDereferenceableAttr(llcx: &Context, bytes: u64) -> &Attribute {
|
|
|
|
unsafe { LLVMRustCreateDereferenceableAttr(llcx, bytes) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn CreateDereferenceableOrNullAttr(llcx: &Context, bytes: u64) -> &Attribute {
|
|
|
|
unsafe { LLVMRustCreateDereferenceableOrNullAttr(llcx, bytes) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn CreateByValAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
|
|
|
|
unsafe { LLVMRustCreateByValAttr(llcx, ty) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn CreateStructRetAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
|
|
|
|
unsafe { LLVMRustCreateStructRetAttr(llcx, ty) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn CreateUWTableAttr(llcx: &Context, async_: bool) -> &Attribute {
|
|
|
|
unsafe { LLVMRustCreateUWTableAttr(llcx, async_) }
|
2021-01-24 17:15:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 15:30:54 -04:00
|
|
|
pub fn CreateAllocSizeAttr(llcx: &Context, size_arg: u32) -> &Attribute {
|
|
|
|
unsafe { LLVMRustCreateAllocSizeAttr(llcx, size_arg) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn CreateAllocKindAttr(llcx: &Context, kind_arg: AllocKindFlags) -> &Attribute {
|
|
|
|
unsafe { LLVMRustCreateAllocKindAttr(llcx, kind_arg.bits()) }
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:41:36 +03:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum AttributePlace {
|
|
|
|
ReturnValue,
|
|
|
|
Argument(u32),
|
|
|
|
Function,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AttributePlace {
|
|
|
|
pub fn as_uint(self) -> c_uint {
|
|
|
|
match self {
|
|
|
|
AttributePlace::ReturnValue => 0,
|
|
|
|
AttributePlace::Argument(i) => 1 + i,
|
|
|
|
AttributePlace::Function => !0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
#[repr(C)]
|
|
|
|
pub enum CodeGenOptSize {
|
|
|
|
CodeGenOptSizeNone = 0,
|
|
|
|
CodeGenOptSizeDefault = 1,
|
|
|
|
CodeGenOptSizeAggressive = 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for ArchiveKind {
|
|
|
|
type Err = ();
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"gnu" => Ok(ArchiveKind::K_GNU),
|
|
|
|
"bsd" => Ok(ArchiveKind::K_BSD),
|
2020-02-12 12:06:14 +01:00
|
|
|
"darwin" => Ok(ArchiveKind::K_DARWIN),
|
2018-05-29 20:41:36 +03:00
|
|
|
"coff" => Ok(ArchiveKind::K_COFF),
|
2023-01-11 11:27:29 +08:00
|
|
|
"aix_big" => Ok(ArchiveKind::K_AIXBIG),
|
2018-05-29 20:41:36 +03:00
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
pub fn SetInstructionCallConv(instr: &Value, cc: CallConv) {
|
2018-05-29 20:41:36 +03:00
|
|
|
unsafe {
|
|
|
|
LLVMSetInstructionCallConv(instr, cc as c_uint);
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 13:49:49 -05:00
|
|
|
pub fn SetFunctionCallConv(fn_: &Value, cc: CallConv) {
|
2018-05-29 20:41:36 +03:00
|
|
|
unsafe {
|
|
|
|
LLVMSetFunctionCallConv(fn_, cc as c_uint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Externally visible symbols that might appear in multiple codegen units need to appear in
|
|
|
|
// their own comdat section so that the duplicates can be discarded at link time. This can for
|
|
|
|
// example happen for generics when using multiple codegen units. This function simply uses the
|
|
|
|
// value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
|
|
|
|
// function.
|
2021-06-23 16:26:46 -04:00
|
|
|
// For more details on COMDAT sections see e.g., https://www.airs.com/blog/archives/52
|
2021-12-14 13:49:49 -05:00
|
|
|
pub fn SetUniqueComdat(llmod: &Module, val: &Value) {
|
2018-05-29 20:41:36 +03:00
|
|
|
unsafe {
|
2019-12-04 12:00:28 -08:00
|
|
|
let name = get_value_name(val);
|
|
|
|
LLVMRustSetComdat(llmod, val, name.as_ptr().cast(), name.len());
|
2018-05-29 20:41:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
pub fn SetUnnamedAddress(global: &Value, unnamed: UnnamedAddr) {
|
2018-05-29 20:41:36 +03:00
|
|
|
unsafe {
|
2020-03-11 00:00:00 +00:00
|
|
|
LLVMSetUnnamedAddress(global, unnamed);
|
2018-05-29 20:41:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
pub fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) {
|
2018-05-29 20:41:36 +03:00
|
|
|
unsafe {
|
|
|
|
LLVMSetThreadLocalMode(global, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:16 -05:00
|
|
|
impl AttributeKind {
|
|
|
|
/// Create an LLVM Attribute with no associated value.
|
|
|
|
pub fn create_attr(self, llcx: &Context) -> &Attribute {
|
|
|
|
unsafe { LLVMRustCreateAttrNoValue(llcx, self) }
|
2018-05-29 20:41:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 16:20:42 +00:00
|
|
|
impl MemoryEffects {
|
|
|
|
/// Create an LLVM Attribute with these memory effects.
|
|
|
|
pub fn create_attr(self, llcx: &Context) -> &Attribute {
|
|
|
|
unsafe { LLVMRustCreateMemoryEffectsAttr(llcx, self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:27:15 -07:00
|
|
|
pub fn set_section(llglobal: &Value, section_name: &str) {
|
|
|
|
let section_name_cstr = CString::new(section_name).expect("unexpected CString error");
|
|
|
|
unsafe {
|
|
|
|
LLVMSetSection(llglobal, section_name_cstr.as_ptr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name: &str) -> &'a Value {
|
|
|
|
let name_cstr = CString::new(name).expect("unexpected CString error");
|
|
|
|
unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_initializer(llglobal: &Value, constant_val: &Value) {
|
|
|
|
unsafe {
|
|
|
|
LLVMSetInitializer(llglobal, constant_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_global_constant(llglobal: &Value, is_constant: bool) {
|
|
|
|
unsafe {
|
|
|
|
LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
|
|
|
|
unsafe {
|
|
|
|
LLVMRustSetLinkage(llglobal, linkage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:56:07 -08:00
|
|
|
pub fn set_visibility(llglobal: &Value, visibility: Visibility) {
|
|
|
|
unsafe {
|
|
|
|
LLVMRustSetVisibility(llglobal, visibility);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-24 01:03:39 +00:00
|
|
|
pub fn set_alignment(llglobal: &Value, align: Align) {
|
2020-07-02 11:27:15 -07:00
|
|
|
unsafe {
|
2024-03-24 01:03:39 +00:00
|
|
|
ffi::LLVMSetAlignment(llglobal, align.bytes() as c_uint);
|
2020-07-02 11:27:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:56:07 -08:00
|
|
|
pub fn set_comdat(llmod: &Module, llglobal: &Value, name: &str) {
|
|
|
|
unsafe {
|
|
|
|
LLVMRustSetComdat(llmod, llglobal, name.as_ptr().cast(), name.len());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:41:36 +03:00
|
|
|
/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
|
2020-03-07 00:56:32 +01:00
|
|
|
pub fn get_param(llfn: &Value, index: c_uint) -> &Value {
|
2018-05-29 20:41:36 +03:00
|
|
|
unsafe {
|
|
|
|
assert!(
|
|
|
|
index < LLVMCountParams(llfn),
|
|
|
|
"out of bounds argument access: {} out of {} arguments",
|
|
|
|
index,
|
|
|
|
LLVMCountParams(llfn)
|
|
|
|
);
|
|
|
|
LLVMGetParam(llfn, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:00:28 -08:00
|
|
|
/// Safe wrapper for `LLVMGetValueName2` into a byte slice
|
2020-03-07 00:56:32 +01:00
|
|
|
pub fn get_value_name(value: &Value) -> &[u8] {
|
2019-12-04 12:00:28 -08:00
|
|
|
unsafe {
|
|
|
|
let mut len = 0;
|
|
|
|
let data = LLVMGetValueName2(value, &mut len);
|
|
|
|
std::slice::from_raw_parts(data.cast(), len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Safe wrapper for `LLVMSetValueName2` from a byte slice
|
|
|
|
pub fn set_value_name(value: &Value, name: &[u8]) {
|
|
|
|
unsafe {
|
|
|
|
let data = name.as_ptr().cast();
|
|
|
|
LLVMSetValueName2(value, data, name.len());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 14:43:12 +03:00
|
|
|
pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
|
|
|
|
let sr = RustString { bytes: RefCell::new(Vec::new()) };
|
|
|
|
f(&sr);
|
|
|
|
String::from_utf8(sr.bytes.into_inner())
|
2018-05-29 20:41:36 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 11:27:15 -07:00
|
|
|
pub fn build_byte_buffer(f: impl FnOnce(&RustString)) -> Vec<u8> {
|
|
|
|
let sr = RustString { bytes: RefCell::new(Vec::new()) };
|
|
|
|
f(&sr);
|
|
|
|
sr.bytes.into_inner()
|
|
|
|
}
|
|
|
|
|
2018-07-13 14:43:12 +03:00
|
|
|
pub fn twine_to_string(tr: &Twine) -> String {
|
|
|
|
unsafe {
|
|
|
|
build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
|
|
|
|
}
|
2018-05-29 20:41:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_error() -> Option<String> {
|
|
|
|
unsafe {
|
|
|
|
let cstr = LLVMRustGetLastError();
|
|
|
|
if cstr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let err = CStr::from_ptr(cstr).to_bytes();
|
|
|
|
let err = String::from_utf8_lossy(err).to_string();
|
|
|
|
libc::free(cstr as *mut _);
|
|
|
|
Some(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 16:02:31 +03:00
|
|
|
pub struct OperandBundleDef<'a> {
|
|
|
|
pub raw: &'a mut ffi::OperandBundleDef<'a>,
|
2018-05-29 20:41:36 +03:00
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl<'a> OperandBundleDef<'a> {
|
2018-07-16 16:02:31 +03:00
|
|
|
pub fn new(name: &str, vals: &[&'a Value]) -> Self {
|
2018-08-07 16:04:34 +02:00
|
|
|
let name = SmallCStr::new(name);
|
2018-05-29 20:41:36 +03:00
|
|
|
let def = unsafe {
|
|
|
|
LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
|
|
|
|
};
|
2018-07-16 16:02:31 +03:00
|
|
|
OperandBundleDef { raw: def }
|
2018-05-29 20:41:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl Drop for OperandBundleDef<'_> {
|
2018-05-29 20:41:36 +03:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2018-07-16 16:02:31 +03:00
|
|
|
LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _));
|
2018-05-29 20:41:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|