From b1a4e5d8ce3446363f0e953dec64ffc7e4458ae8 Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Wed, 11 Jan 2017 22:36:21 -0700 Subject: [PATCH 1/2] Allow making reference to global variables. --- Sources/LLVM/IRBuilder.swift | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Sources/LLVM/IRBuilder.swift b/Sources/LLVM/IRBuilder.swift index 7647403c..5420729f 100644 --- a/Sources/LLVM/IRBuilder.swift +++ b/Sources/LLVM/IRBuilder.swift @@ -935,7 +935,7 @@ public class IRBuilder { return LLVMBuildInsertElement(llvm, vector.asLLVM(), element.asLLVM(), index.asLLVM(), name) } - // MARK: Global Variable Creation Instructions + // MARK: Global Variable Instructions /// Build a named global of the given type. /// @@ -989,6 +989,19 @@ public class IRBuilder { public func buildGlobalStringPtr(_ string: String, name: String = "") -> IRValue { return LLVMBuildGlobalStringPtr(llvm, string, name) } + + /// Builds a named reference to a global variable with the given name, if it + /// exists. + /// + /// - parameter name: The name of the global to reference. + /// + /// - returns: A value representing the referenced global if it exists. + public func referenceGlobal(named name: String) -> Global? { + guard let ref = LLVMGetNamedGlobal(module.llvm, name) else { + return nil + } + return Global(llvm: ref) + } deinit { LLVMDisposeBuilder(llvm) From 6dd3084fc2bd71da092c5cf8810f70a2e1e95c81 Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Thu, 12 Jan 2017 15:22:07 -0700 Subject: [PATCH 2/2] Extract global retrieval functions and move them to IRBuilder --- Sources/LLVM/IRBuilder.swift | 54 ++++++++++++++++++++++++++---------- Sources/LLVM/Module.swift | 24 ---------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/Sources/LLVM/IRBuilder.swift b/Sources/LLVM/IRBuilder.swift index 5420729f..cdf96861 100644 --- a/Sources/LLVM/IRBuilder.swift +++ b/Sources/LLVM/IRBuilder.swift @@ -108,6 +108,43 @@ public enum RealPredicate { } } +extension Module { + /// Searches for and retrieves a global variable with the given name in this + /// module if that name references an existing global variable. + /// + /// - parameter name: The name of the global to reference. + /// + /// - returns: A value representing the referenced global if it exists. + public func global(named name: String) -> Global? { + guard let ref = LLVMGetNamedGlobal(llvm, name) else { return nil } + return Global(llvm: ref) + } + + /// Searches for and retrieves a type with the given name in this module if + /// that name references an existing type. + /// + /// - parameter name: The name of the type to create. + /// + /// - returns: A representation of the newly created type with the given name + /// or nil if such a representation could not be created. + public func type(named name: String) -> IRType? { + guard let type = LLVMGetTypeByName(llvm, name) else { return nil } + return convertType(type) + } + + /// Searches for and retrieves a function with the given name in this module + /// if that name references an existing function. + /// + /// - parameter name: The name of the function to create. + /// + /// - returns: A representation of the newly created function with the given + /// name or nil if such a representation could not be created. + public func function(named name: String) -> Function? { + guard let fn = LLVMGetNamedFunction(llvm, name) else { return nil } + return Function(llvm: fn) + } +} + /// An `IRBuilder` is a helper object that generates LLVM instructions. IR /// Builders keep track of a position within a function or basic block and has /// methods to insert instructions at that position. @@ -974,8 +1011,8 @@ public class IRBuilder { /// - parameter name: The name for the newly inserted instruction. /// /// - returns: A value representing the newly inserted global string variable. - public func buildGlobalString(_ string: String, name: String = "") -> IRValue { - return LLVMBuildGlobalString(llvm, string, name) + public func buildGlobalString(_ string: String, name: String = "") -> Global { + return Global(llvm: LLVMBuildGlobalString(llvm, string, name)) } /// Builds a named global variable containing a pointer to the contents of the @@ -989,19 +1026,6 @@ public class IRBuilder { public func buildGlobalStringPtr(_ string: String, name: String = "") -> IRValue { return LLVMBuildGlobalStringPtr(llvm, string, name) } - - /// Builds a named reference to a global variable with the given name, if it - /// exists. - /// - /// - parameter name: The name of the global to reference. - /// - /// - returns: A value representing the referenced global if it exists. - public func referenceGlobal(named name: String) -> Global? { - guard let ref = LLVMGetNamedGlobal(module.llvm, name) else { - return nil - } - return Global(llvm: ref) - } deinit { LLVMDisposeBuilder(llvm) diff --git a/Sources/LLVM/Module.swift b/Sources/LLVM/Module.swift index a7e67bc7..ecc17303 100644 --- a/Sources/LLVM/Module.swift +++ b/Sources/LLVM/Module.swift @@ -110,30 +110,6 @@ public final class Module { } } - /// Searches for and retrieves a type with the given name in this module if - /// that name references an existing type. - /// - /// - parameter name: The name of the type to create. - /// - /// - returns: A representation of the newly created type with the given name - /// or nil if such a representation could not be created. - public func type(named name: String) -> IRType? { - guard let type = LLVMGetTypeByName(llvm, name) else { return nil } - return convertType(type) - } - - /// Searches for and retrieves a function with the given name in this module - /// if that name references an existing function. - /// - /// - parameter name: The name of the function to create. - /// - /// - returns: A representation of the newly created function with the given - /// name or nil if such a representation could not be created. - public func function(named name: String) -> Function? { - guard let fn = LLVMGetNamedFunction(llvm, name) else { return nil } - return Function(llvm: fn) - } - /// Verifies that this module is valid, taking the specified action if not. /// If this module did not pass verification, a description of any invalid /// constructs is provided with the thrown