Skip to content

Commit b1a4e5d

Browse files
committed
Allow making reference to global variables.
1 parent 2ae4e58 commit b1a4e5d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ public class IRBuilder {
935935
return LLVMBuildInsertElement(llvm, vector.asLLVM(), element.asLLVM(), index.asLLVM(), name)
936936
}
937937

938-
// MARK: Global Variable Creation Instructions
938+
// MARK: Global Variable Instructions
939939

940940
/// Build a named global of the given type.
941941
///
@@ -989,6 +989,19 @@ public class IRBuilder {
989989
public func buildGlobalStringPtr(_ string: String, name: String = "") -> IRValue {
990990
return LLVMBuildGlobalStringPtr(llvm, string, name)
991991
}
992+
993+
/// Builds a named reference to a global variable with the given name, if it
994+
/// exists.
995+
///
996+
/// - parameter name: The name of the global to reference.
997+
///
998+
/// - returns: A value representing the referenced global if it exists.
999+
public func referenceGlobal(named name: String) -> Global? {
1000+
guard let ref = LLVMGetNamedGlobal(module.llvm, name) else {
1001+
return nil
1002+
}
1003+
return Global(llvm: ref)
1004+
}
9921005

9931006
deinit {
9941007
LLVMDisposeBuilder(llvm)

0 commit comments

Comments
 (0)