Use LLVM-C APIs for getting/setting linkage

This commit is contained in:
Zalathar 2024-10-25 22:06:29 +11:00
parent ec41e6d1b0
commit 96993a9b5e
3 changed files with 24 additions and 93 deletions

View File

@ -5,6 +5,7 @@
use std::marker::PhantomData;
use libc::{c_char, c_int, c_uint, c_ulonglong, c_void, size_t};
use rustc_macros::TryFromU32;
use rustc_target::spec::SymbolVisibility;
use super::RustString;
@ -133,21 +134,31 @@ pub enum CallConv {
AvrInterrupt = 85,
}
/// LLVMRustLinkage
#[derive(Copy, Clone, PartialEq)]
/// Must match the layout of `LLVMLinkage`.
#[derive(Copy, Clone, PartialEq, TryFromU32)]
#[repr(C)]
pub enum Linkage {
ExternalLinkage = 0,
AvailableExternallyLinkage = 1,
LinkOnceAnyLinkage = 2,
LinkOnceODRLinkage = 3,
WeakAnyLinkage = 4,
WeakODRLinkage = 5,
AppendingLinkage = 6,
InternalLinkage = 7,
PrivateLinkage = 8,
ExternalWeakLinkage = 9,
CommonLinkage = 10,
#[deprecated = "marked obsolete by LLVM"]
LinkOnceODRAutoHideLinkage = 4,
WeakAnyLinkage = 5,
WeakODRLinkage = 6,
AppendingLinkage = 7,
InternalLinkage = 8,
PrivateLinkage = 9,
#[deprecated = "marked obsolete by LLVM"]
DLLImportLinkage = 10,
#[deprecated = "marked obsolete by LLVM"]
DLLExportLinkage = 11,
ExternalWeakLinkage = 12,
#[deprecated = "marked obsolete by LLVM"]
GhostLinkage = 13,
CommonLinkage = 14,
LinkerPrivateLinkage = 15,
LinkerPrivateWeakLinkage = 16,
}
// LLVMRustVisibility
@ -970,6 +981,8 @@ pub fn LLVMConstInBoundsGEP2<'a>(
// Operations on global variables, functions, and aliases (globals)
pub fn LLVMIsDeclaration(Global: &Value) -> Bool;
pub fn LLVMGetLinkage(Global: &Value) -> RawEnum<Linkage>;
pub fn LLVMSetLinkage(Global: &Value, RustLinkage: Linkage);
pub fn LLVMSetSection(Global: &Value, Section: *const c_char);
pub fn LLVMGetAlignment(Global: &Value) -> c_uint;
pub fn LLVMSetAlignment(Global: &Value, Bytes: c_uint);
@ -1546,8 +1559,6 @@ pub fn LLVMRustConstInt128Get(
) -> bool;
// Operations on global variables, functions, and aliases (globals)
pub fn LLVMRustGetLinkage(Global: &Value) -> Linkage;
pub fn LLVMRustSetLinkage(Global: &Value, RustLinkage: Linkage);
pub fn LLVMRustGetVisibility(Global: &Value) -> Visibility;
pub fn LLVMRustSetVisibility(Global: &Value, Viz: Visibility);
pub fn LLVMRustSetDSOLocal(Global: &Value, is_dso_local: bool);

View File

@ -233,12 +233,12 @@ pub fn set_global_constant(llglobal: &Value, is_constant: bool) {
}
pub fn get_linkage(llglobal: &Value) -> Linkage {
unsafe { LLVMRustGetLinkage(llglobal) }
unsafe { LLVMGetLinkage(llglobal) }.to_rust()
}
pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
unsafe {
LLVMRustSetLinkage(llglobal, linkage);
LLVMSetLinkage(llglobal, linkage);
}
}

View File

@ -1619,86 +1619,6 @@ extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B,
unwrap(B)->SetInsertPoint(unwrap(BB), Point);
}
enum class LLVMRustLinkage {
ExternalLinkage = 0,
AvailableExternallyLinkage = 1,
LinkOnceAnyLinkage = 2,
LinkOnceODRLinkage = 3,
WeakAnyLinkage = 4,
WeakODRLinkage = 5,
AppendingLinkage = 6,
InternalLinkage = 7,
PrivateLinkage = 8,
ExternalWeakLinkage = 9,
CommonLinkage = 10,
};
static LLVMRustLinkage toRust(LLVMLinkage Linkage) {
switch (Linkage) {
case LLVMExternalLinkage:
return LLVMRustLinkage::ExternalLinkage;
case LLVMAvailableExternallyLinkage:
return LLVMRustLinkage::AvailableExternallyLinkage;
case LLVMLinkOnceAnyLinkage:
return LLVMRustLinkage::LinkOnceAnyLinkage;
case LLVMLinkOnceODRLinkage:
return LLVMRustLinkage::LinkOnceODRLinkage;
case LLVMWeakAnyLinkage:
return LLVMRustLinkage::WeakAnyLinkage;
case LLVMWeakODRLinkage:
return LLVMRustLinkage::WeakODRLinkage;
case LLVMAppendingLinkage:
return LLVMRustLinkage::AppendingLinkage;
case LLVMInternalLinkage:
return LLVMRustLinkage::InternalLinkage;
case LLVMPrivateLinkage:
return LLVMRustLinkage::PrivateLinkage;
case LLVMExternalWeakLinkage:
return LLVMRustLinkage::ExternalWeakLinkage;
case LLVMCommonLinkage:
return LLVMRustLinkage::CommonLinkage;
default:
report_fatal_error("Invalid LLVMRustLinkage value!");
}
}
static LLVMLinkage fromRust(LLVMRustLinkage Linkage) {
switch (Linkage) {
case LLVMRustLinkage::ExternalLinkage:
return LLVMExternalLinkage;
case LLVMRustLinkage::AvailableExternallyLinkage:
return LLVMAvailableExternallyLinkage;
case LLVMRustLinkage::LinkOnceAnyLinkage:
return LLVMLinkOnceAnyLinkage;
case LLVMRustLinkage::LinkOnceODRLinkage:
return LLVMLinkOnceODRLinkage;
case LLVMRustLinkage::WeakAnyLinkage:
return LLVMWeakAnyLinkage;
case LLVMRustLinkage::WeakODRLinkage:
return LLVMWeakODRLinkage;
case LLVMRustLinkage::AppendingLinkage:
return LLVMAppendingLinkage;
case LLVMRustLinkage::InternalLinkage:
return LLVMInternalLinkage;
case LLVMRustLinkage::PrivateLinkage:
return LLVMPrivateLinkage;
case LLVMRustLinkage::ExternalWeakLinkage:
return LLVMExternalWeakLinkage;
case LLVMRustLinkage::CommonLinkage:
return LLVMCommonLinkage;
}
report_fatal_error("Invalid LLVMRustLinkage value!");
}
extern "C" LLVMRustLinkage LLVMRustGetLinkage(LLVMValueRef V) {
return toRust(LLVMGetLinkage(V));
}
extern "C" void LLVMRustSetLinkage(LLVMValueRef V,
LLVMRustLinkage RustLinkage) {
LLVMSetLinkage(V, fromRust(RustLinkage));
}
extern "C" bool LLVMRustConstIntGetZExtValue(LLVMValueRef CV, uint64_t *value) {
auto C = unwrap<llvm::ConstantInt>(CV);
if (C->getBitWidth() > 64)