Skip to content

Commit 0c6b336

Browse files
authored
Merge pull request #22 from CodaFi/mister-worldwide
Allow making reference to global variables.
2 parents 1ca4af8 + 6dd3084 commit 0c6b336

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,43 @@ public enum RealPredicate {
108108
}
109109
}
110110

111+
extension Module {
112+
/// Searches for and retrieves a global variable with the given name in this
113+
/// module if that name references an existing global variable.
114+
///
115+
/// - parameter name: The name of the global to reference.
116+
///
117+
/// - returns: A value representing the referenced global if it exists.
118+
public func global(named name: String) -> Global? {
119+
guard let ref = LLVMGetNamedGlobal(llvm, name) else { return nil }
120+
return Global(llvm: ref)
121+
}
122+
123+
/// Searches for and retrieves a type with the given name in this module if
124+
/// that name references an existing type.
125+
///
126+
/// - parameter name: The name of the type to create.
127+
///
128+
/// - returns: A representation of the newly created type with the given name
129+
/// or nil if such a representation could not be created.
130+
public func type(named name: String) -> IRType? {
131+
guard let type = LLVMGetTypeByName(llvm, name) else { return nil }
132+
return convertType(type)
133+
}
134+
135+
/// Searches for and retrieves a function with the given name in this module
136+
/// if that name references an existing function.
137+
///
138+
/// - parameter name: The name of the function to create.
139+
///
140+
/// - returns: A representation of the newly created function with the given
141+
/// name or nil if such a representation could not be created.
142+
public func function(named name: String) -> Function? {
143+
guard let fn = LLVMGetNamedFunction(llvm, name) else { return nil }
144+
return Function(llvm: fn)
145+
}
146+
}
147+
111148
/// An `IRBuilder` is a helper object that generates LLVM instructions. IR
112149
/// Builders keep track of a position within a function or basic block and has
113150
/// methods to insert instructions at that position.
@@ -935,7 +972,7 @@ public class IRBuilder {
935972
return LLVMBuildInsertElement(llvm, vector.asLLVM(), element.asLLVM(), index.asLLVM(), name)
936973
}
937974

938-
// MARK: Global Variable Creation Instructions
975+
// MARK: Global Variable Instructions
939976

940977
/// Build a named global of the given type.
941978
///
@@ -974,8 +1011,8 @@ public class IRBuilder {
9741011
/// - parameter name: The name for the newly inserted instruction.
9751012
///
9761013
/// - returns: A value representing the newly inserted global string variable.
977-
public func buildGlobalString(_ string: String, name: String = "") -> IRValue {
978-
return LLVMBuildGlobalString(llvm, string, name)
1014+
public func buildGlobalString(_ string: String, name: String = "") -> Global {
1015+
return Global(llvm: LLVMBuildGlobalString(llvm, string, name))
9791016
}
9801017

9811018
/// Builds a named global variable containing a pointer to the contents of the

Sources/LLVM/Module.swift

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,6 @@ public final class Module {
110110
}
111111
}
112112

113-
/// Searches for and retrieves a type with the given name in this module if
114-
/// that name references an existing type.
115-
///
116-
/// - parameter name: The name of the type to create.
117-
///
118-
/// - returns: A representation of the newly created type with the given name
119-
/// or nil if such a representation could not be created.
120-
public func type(named name: String) -> IRType? {
121-
guard let type = LLVMGetTypeByName(llvm, name) else { return nil }
122-
return convertType(type)
123-
}
124-
125-
/// Searches for and retrieves a function with the given name in this module
126-
/// if that name references an existing function.
127-
///
128-
/// - parameter name: The name of the function to create.
129-
///
130-
/// - returns: A representation of the newly created function with the given
131-
/// name or nil if such a representation could not be created.
132-
public func function(named name: String) -> Function? {
133-
guard let fn = LLVMGetNamedFunction(llvm, name) else { return nil }
134-
return Function(llvm: fn)
135-
}
136-
137113
/// Verifies that this module is valid, taking the specified action if not.
138114
/// If this module did not pass verification, a description of any invalid
139115
/// constructs is provided with the thrown

0 commit comments

Comments
 (0)