llvm_lib/core/values/constants/global_variables.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
use super::{TypeRef, ValueRef};
use crate::core::module::ModuleRef;
use crate::core::AddressSpace;
use crate::{CInt, CString, GetRef};
use llvm_sys::{core, LLVMThreadLocalMode};
/// Represents the thread-local storage (TLS) model for a global variable in LLVM.
///
/// Thread-local storage allows each thread to have its own instance of a global variable. The different TLS models
/// dictate how the runtime handles accessing the variable across threads and whether the variable is accessed
/// dynamically or statically.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ThreadLocalMode {
/// The global variable is not thread-local.
NotThreadLocal,
/// General dynamic TLS model, suitable for global variables that are accessed across
/// multiple modules and can be dynamically allocated for each thread.
GeneralDynamicTLSModel,
/// Local dynamic TLS model, suitable for global variables that are dynamically allocated
/// but only accessed within the same module.
LocalDynamicTLSModel,
/// Initial execution TLS model, allowing for faster access to TLS variables when they are
/// known to be used early during program execution (such as in dynamic libraries).
InitialExecTLSModel,
/// Local execution TLS model, providing fast access to thread-local variables that are
/// only accessed within the current module, without requiring relocation.
LocalExecTLSModel,
}
impl From<LLVMThreadLocalMode> for ThreadLocalMode {
fn from(mode: LLVMThreadLocalMode) -> Self {
match mode {
LLVMThreadLocalMode::LLVMNotThreadLocal => Self::NotThreadLocal,
LLVMThreadLocalMode::LLVMGeneralDynamicTLSModel => Self::GeneralDynamicTLSModel,
LLVMThreadLocalMode::LLVMLocalDynamicTLSModel => Self::LocalDynamicTLSModel,
LLVMThreadLocalMode::LLVMInitialExecTLSModel => Self::InitialExecTLSModel,
LLVMThreadLocalMode::LLVMLocalExecTLSModel => Self::LocalExecTLSModel,
}
}
}
impl From<ThreadLocalMode> for LLVMThreadLocalMode {
fn from(mode: ThreadLocalMode) -> Self {
match mode {
ThreadLocalMode::NotThreadLocal => Self::LLVMNotThreadLocal,
ThreadLocalMode::GeneralDynamicTLSModel => Self::LLVMGeneralDynamicTLSModel,
ThreadLocalMode::LocalDynamicTLSModel => Self::LLVMLocalDynamicTLSModel,
ThreadLocalMode::InitialExecTLSModel => Self::LLVMInitialExecTLSModel,
ThreadLocalMode::LocalExecTLSModel => Self::LLVMLocalExecTLSModel,
}
}
}
/// Adds a new global variable of the specified type to the module.
///
/// This function wraps the `LLVMAddGlobal` function from the LLVM core library. It creates a new global variable
/// in the current module, with the provided type and name. The global variable is initialized with a null value by default
/// and can be further configured using other methods such as setting an initializer or modifying its linkage.
///
/// # Parameters
///
/// - `ty`: A reference to the `TypeRef` representing the type of the global variable.
/// - `name`: A string slice (`&str`) representing the name of the global variable.
///
/// # Returns
///
/// Returns a `ValueRef` representing the newly added global variable.
#[must_use]
pub fn add_global(m: &ModuleRef, ty: &TypeRef, name: &str) -> ValueRef {
let c_name = CString::from(name);
unsafe {
ValueRef(core::LLVMAddGlobal(
m.get_ref(),
ty.get_ref(),
c_name.as_ptr(),
))
}
}
/// Adds a new global variable of the specified type to the module in a specific address space.
///
/// This function wraps the `LLVMAddGlobalInAddressSpace` function from the LLVM core library. It creates a new global
/// variable in the specified address space within the current module, with the provided type and name. Address spaces
/// are used in LLVM to specify different memory regions for global variables, such as GPU memory or specialized
/// hardware regions.
///
/// # Parameters
///
/// - `ty`: A reference to the `TypeRef` representing the type of the global variable.
/// - `name`: A string slice (`&str`) representing the name of the global variable.
/// - `address_space`: A reference to the `AddressSpace` where the global variable should be allocated.
///
/// # Returns
///
/// Returns a `ValueRef` representing the newly added global variable in the specified address space.
#[must_use]
pub fn add_global_in_address_space(
m: &ModuleRef,
ty: &TypeRef,
name: &str,
address_space: &AddressSpace,
) -> ValueRef {
let c_name = CString::from(name);
unsafe {
ValueRef(core::LLVMAddGlobalInAddressSpace(
m.get_ref(),
ty.get_ref(),
c_name.as_ptr(),
***address_space,
))
}
}
/// Retrieves a global variable by its name from the module.
///
/// This function wraps the `LLVMGetNamedGlobal` function from the LLVM core library. It searches for a global
/// variable with the specified name in the current module and returns it if found. If no global variable with the
/// given name exists in the module, it returns `None`.
///
/// # Parameters
///
/// - `name`: A string slice (`&str`) representing the name of the global variable to search for.
///
/// # Returns
///
/// Returns an `Option<ValueRef>`:
/// - `Some(ValueRef)` if a global variable with the specified name is found.
/// - `None` if no global variable with the specified name exists in the module.
#[must_use]
pub fn get_named_global(m: &ModuleRef, name: &str) -> Option<ValueRef> {
let c_name = CString::from(name);
let global = unsafe { core::LLVMGetNamedGlobal(m.get_ref(), c_name.as_ptr()) };
if global.is_null() {
None
} else {
Some(ValueRef(global))
}
}
/// Retrieves the first global variable defined in the module.
///
/// This function wraps the `LLVMGetFirstGlobal` function from the LLVM core library. It returns the first global
/// variable in the current module, which can be useful for iterating through all global variables in the module.
///
/// # Returns
///
/// Returns an `Option<ValueRef>`:
/// - `Some(ValueRef)` if the module contains at least one global variable.
/// - `None` if the module does not have any global variables.
#[must_use]
pub fn get_first_global(m: &ModuleRef) -> Option<ValueRef> {
let global = unsafe { core::LLVMGetFirstGlobal(m.get_ref()) };
if global.is_null() {
None
} else {
Some(ValueRef(global))
}
}
/// Retrieves the last global variable defined in the module.
///
/// This function wraps the `LLVMGetLastGlobal` function from the LLVM core library. It returns the last global
/// variable in the current module, which can be useful for iterating through all global variables or accessing the
/// most recently defined one.
///
/// # Returns
///
/// Returns an `Option<ValueRef>`:
/// - `Some(ValueRef)` if the module contains at least one global variable.
/// - `None` if the module does not have any global variables.
#[must_use]
pub fn get_last_global(m: &ModuleRef) -> Option<ValueRef> {
let global = unsafe { core::LLVMGetLastGlobal(m.get_ref()) };
if global.is_null() {
None
} else {
Some(ValueRef(global))
}
}
/// Retrieves the next global variable following the current one in the module.
///
/// This function wraps the `LLVMGetNextGlobal` function from the LLVM core library. It returns the global variable
/// that comes after the current global variable in the module. This is useful for iterating through all global variables
/// in a module.
///
/// # Returns
///
/// Returns an `Option<ValueRef>`:
/// - `Some(ValueRef)` if there is another global variable following the current one.
/// - `None` if the current global variable is the last one in the module.
#[must_use]
pub fn get_next_global(val: &ValueRef) -> Option<ValueRef> {
let global = unsafe { core::LLVMGetNextGlobal(val.get_ref()) };
if global.is_null() {
None
} else {
Some(ValueRef(global))
}
}
/// Retrieves the previous global variable preceding the current one in the module.
///
/// This function wraps the `LLVMGetPreviousGlobal` function from the LLVM core library. It returns the global variable
/// that comes before the current global variable in the module. This is useful for iterating backward through all global
/// variables in a module.
///
/// # Returns
///
/// Returns an `Option<ValueRef>`:
/// - `Some(ValueRef)` if there is a global variable preceding the current one.
/// - `None` if the current global variable is the first one in the module.
#[must_use]
pub fn get_previous_global(val: &ValueRef) -> Option<ValueRef> {
let global = unsafe { core::LLVMGetPreviousGlobal(val.get_ref()) };
if global.is_null() {
None
} else {
Some(ValueRef(global))
}
}
/// Deletes the specified global variable.
///
/// This function wraps the `LLVMDeleteGlobal` function from the LLVM core library. It removes the global variable
/// represented by `ValueRef` from the module and deletes it. After this function is called, the global variable is no
/// longer valid and cannot be used.
///
/// # Note
///
/// Once a global variable is deleted, it cannot be accessed or modified. Be cautious when deleting global variables
/// to ensure that there are no further references to them.
///
/// # Example
/// ```rust
/// let global_var = module.add_global(&int32_type, "my_global");
/// global_var.delete_global(); // Deletes the global variable
/// ```
pub fn delete_global(val: &ValueRef) {
unsafe {
core::LLVMDeleteGlobal(val.get_ref());
}
}
/// Get the initializer for a global variable.
///
/// This function wraps the `LLVMGetInitializer` function from the LLVM core library. It returns the initializer of the
/// global variable represented by `ValueRef`. If the global variable has no initializer, the function returns `None`.
/// The initializer is the constant value assigned to the global variable at the time of its definition.
///
/// # Returns
///
/// Returns an `Option<ValueRef>`:
/// - `Some(ValueRef)` if the global variable has an initializer.
/// - `None` if the global variable does not have an initializer.
#[must_use]
pub fn get_initializer(val: &ValueRef) -> Option<ValueRef> {
let initializer = unsafe { core::LLVMGetInitializer(val.get_ref()) };
if initializer.is_null() {
None
} else {
Some(ValueRef(initializer))
}
}
/// Sets the initializer for a global variable.
///
/// This function wraps the `LLVMSetInitializer` function from the LLVM core library. It assigns the provided constant value
/// as the initializer for the global variable represented by `ValueRef`. The initializer is a constant value that the global
/// variable will be set to when the program starts. Only constant values can be used as initializers for global variables.
///
/// # Parameters
///
/// - `constant_val`: A reference to the constant value (`ValueRef`) that will be used as the initializer for the global variable.
pub fn set_initializer(val: &ValueRef, constant_val: &ValueRef) {
unsafe {
core::LLVMSetInitializer(val.get_ref(), constant_val.get_ref());
}
}
/// Determines if the global variable is thread-local.
///
/// This function wraps the `LLVMIsThreadLocal` function from the LLVM core library. It checks whether the global
/// variable represented by `ValueRef` is marked as thread-local. A thread-local variable has a separate instance for each
/// thread in a multi-threaded program, ensuring that threads do not share the same global variable.
///
/// # Returns
///
/// Returns `true` if the global variable is thread-local, otherwise returns `false`.
#[must_use]
pub fn is_thread_local(val: &ValueRef) -> bool {
unsafe { core::LLVMIsThreadLocal(val.get_ref()) != 0 }
}
/// Sets whether the global variable is thread-local.
///
/// This function wraps the `LLVMSetThreadLocal` function from the LLVM core library. It marks the global variable
/// represented by `ValueRef` as either thread-local or not, based on the provided boolean value. A thread-local variable
/// has a separate instance for each thread in a multi-threaded program, ensuring that threads do not share the same
/// global variable.
///
/// # Parameters
///
/// - `is_thread_local`: A boolean value. If `true`, the global variable is marked as thread-local. If `false`, it is not thread-local.
pub fn set_thread_local(val: &ValueRef, is_thread_local: bool) {
unsafe {
core::LLVMSetThreadLocal(val.get_ref(), *CInt::from(is_thread_local));
}
}
/// Determines if the global variable is a constant.
///
/// This function wraps the `LLVMIsGlobalConstant` function from the LLVM core library. It checks whether the global
/// variable represented by `ValueRef` is marked as a constant. A global constant cannot be modified after its initialization
/// and remains the same throughout the execution of the program.
///
/// # Returns
///
/// Returns `true` if the global variable is a constant, otherwise returns `false`.
#[must_use]
pub fn is_global_constant(val: &ValueRef) -> bool {
unsafe { core::LLVMIsGlobalConstant(val.get_ref()) != 0 }
}
/// Sets whether the global variable is a constant.
///
/// This function wraps the `LLVMSetGlobalConstant` function from the LLVM core library. It marks the global variable
/// represented by `ValueRef` as either a constant or not, based on the provided boolean value. A global constant cannot
/// be modified after its initialization and remains constant throughout the execution of the program.
///
/// # Parameters
///
/// - `is_constant`: A boolean value. If `true`, the global variable is marked as a constant. If `false`, it is not a constant.
pub fn set_global_constant(val: &ValueRef, is_constant: bool) {
unsafe {
core::LLVMSetGlobalConstant(val.get_ref(), *CInt::from(is_constant));
}
}
/// Retrieves the thread-local storage (TLS) mode of the global variable.
///
/// This function wraps the `LLVMGetThreadLocalMode` function from the LLVM core library. It returns the thread-local
/// mode of the global variable represented by `ValueRef`. The TLS mode defines how the thread-local variable is handled
/// by the runtime and can affect performance and behavior in multi-threaded environments.
///
/// # Returns
///
/// Returns a `ThreadLocalMode` enum value representing the thread-local mode of the global variable:
/// - `ThreadLocalMode::NotThreadLocal`: The global variable is not thread-local.
/// - `ThreadLocalMode::GeneralDynamicTLSModel`: General dynamic TLS model.
/// - `ThreadLocalMode::LocalDynamicTLSModel`: Local dynamic TLS model.
/// - `ThreadLocalMode::InitialExecTLSModel`: Initial exec TLS model.
/// - `ThreadLocalMode::LocalExecTLSModel`: Local exec TLS model.
#[must_use]
pub fn get_thread_local_mode(val: &ValueRef) -> ThreadLocalMode {
unsafe { core::LLVMGetThreadLocalMode(val.get_ref()).into() }
}
/// Sets the thread-local storage (TLS) mode for the global variable.
///
/// This function wraps the `LLVMSetThreadLocalMode` function from the LLVM core library. It configures the thread-local
/// mode for the global variable represented by `ValueRef`. The TLS mode defines how the runtime handles the thread-local
/// variable, influencing its performance and behavior in multi-threaded environments.
///
/// # Parameters
///
/// - `mode`: A `ThreadLocalMode` enum value representing the desired thread-local mode:
/// - `ThreadLocalMode::NotThreadLocal`: The global variable is not thread-local.
/// - `ThreadLocalMode::GeneralDynamicTLSModel`: General dynamic TLS model.
/// - `ThreadLocalMode::LocalDynamicTLSModel`: Local dynamic TLS model.
/// - `ThreadLocalMode::InitialExecTLSModel`: Initial exec TLS model.
/// - `ThreadLocalMode::LocalExecTLSModel`: Local exec TLS model.
pub fn set_thread_local_mode(val: &ValueRef, mode: ThreadLocalMode) {
unsafe {
core::LLVMSetThreadLocalMode(val.get_ref(), mode.into());
}
}
/// Determines if the global variable is externally initialized.
///
/// This function wraps the `LLVMIsExternallyInitialized` function from the LLVM core library. It checks whether
/// the global variable represented by `ValueRef` is marked as externally initialized. A global variable that is externally
/// initialized may have its initial value provided by external code, such as during dynamic linking.
///
/// # Returns
///
/// Returns `true` if the global variable is externally initialized, otherwise returns `false`.
#[must_use]
pub fn is_externally_initialized(val: &ValueRef) -> bool {
unsafe { core::LLVMIsExternallyInitialized(val.get_ref()) != 0 }
}
/// Sets whether the global variable is externally initialized.
///
/// This function wraps the `LLVMSetExternallyInitialized` function from the LLVM core library. It marks the global variable
/// represented by `ValueRef` as externally initialized or not, based on the provided boolean value. Externally initialized
/// global variables may receive their initial values from external code, such as during dynamic linking.
///
/// # Parameters
///
/// - `is_ext_init`: A boolean value. If `true`, the global variable is marked as externally initialized. If `false`, it is not externally initialized.
pub fn set_externally_initialized(val: &ValueRef, is_ext_init: bool) {
unsafe {
core::LLVMSetExternallyInitialized(val.get_ref(), *CInt::from(is_ext_init));
}
}