diff --git a/Sources/LLVM/IRBuilder.swift b/Sources/LLVM/IRBuilder.swift index df85332f..1bb8aaf6 100644 --- a/Sources/LLVM/IRBuilder.swift +++ b/Sources/LLVM/IRBuilder.swift @@ -352,6 +352,71 @@ extension Module { guard let comdat = LLVMGetOrInsertComdat(llvm, name) else { fatalError() } return Comdat(llvm: comdat) } + + /// Build a named global of the given type. + /// + /// - parameter name: The name of the newly inserted global value. + /// - parameter type: The type of the newly inserted global value. + /// - parameter addressSpace: The optional address space where the global + /// variable resides. + /// + /// - returns: A value representing the newly inserted global variable. + public func addGlobal(_ name: String, type: IRType, addressSpace: Int? = nil) -> Global { + let val: LLVMValueRef + if let addressSpace = addressSpace { + val = LLVMAddGlobalInAddressSpace(llvm, type.asLLVM(), name, UInt32(addressSpace)) + } else { + val = LLVMAddGlobal(llvm, type.asLLVM(), name) + } + return Global(llvm: val) + } + + /// Build a named global of the given type. + /// + /// - parameter name: The name of the newly inserted global value. + /// - parameter initializer: The initial value for the global variable. + /// - parameter addressSpace: The optional address space where the global + /// variable resides. + /// + /// - returns: A value representing the newly inserted global variable. + public func addGlobal(_ name: String, initializer: IRValue, addressSpace: Int? = nil) -> Global { + var global = addGlobal(name, type: initializer.type) + global.initializer = initializer + return global + } + + /// Build a named global string consisting of an array of `i8` type filled in + /// with the nul terminated string value. + /// + /// - parameter name: The name of the newly inserted global string value. + /// - parameter value: The character contents of the newly inserted global. + /// + /// - returns: A value representing the newly inserted global string variable. + public func addGlobalString(name: String, value: String) -> Global { + let length = value.utf8.count + + var global = addGlobal(name, type: + ArrayType(elementType: IntType.int8, count: length + 1)) + + global.alignment = 1 + global.initializer = value + + return global + } + + /// Build a named alias to a global value or a constant expression. + /// + /// Aliases, unlike function or variables, don’t create any new data. They are + /// just a new symbol and metadata for an existing position. + /// + /// - parameter name: The name of the newly inserted alias. + /// - parameter aliasee: The value or constant to alias. + /// - parameter type: The type of the aliased value or expression. + /// + /// - returns: A value representing the newly created alias. + public func addAlias(name: String, to aliasee: IRGlobal, type: IRType) -> Alias { + return Alias(llvm: LLVMAddAlias(llvm, type.asLLVM(), aliasee.asLLVM(), name)) + } } /// An `IRBuilder` is a helper object that generates LLVM instructions. IR @@ -1803,13 +1868,7 @@ public class IRBuilder { /// /// - returns: A value representing the newly inserted global variable. public func addGlobal(_ name: String, type: IRType, addressSpace: Int? = nil) -> Global { - let llvm: LLVMValueRef - if let addressSpace = addressSpace { - llvm = LLVMAddGlobalInAddressSpace(module.llvm, type.asLLVM(), name, UInt32(addressSpace)) - } else { - llvm = LLVMAddGlobal(module.llvm, type.asLLVM(), name) - } - return Global(llvm: llvm) + return self.module.addGlobal(name, type: type, addressSpace: addressSpace) } /// Build a named global of the given type. @@ -1821,9 +1880,7 @@ public class IRBuilder { /// /// - returns: A value representing the newly inserted global variable. public func addGlobal(_ name: String, initializer: IRValue, addressSpace: Int? = nil) -> Global { - var global = addGlobal(name, type: initializer.type) - global.initializer = initializer - return global + return self.module.addGlobal(name, initializer: initializer, addressSpace: addressSpace) } /// Build a named global string consisting of an array of `i8` type filled in @@ -1834,15 +1891,7 @@ public class IRBuilder { /// /// - returns: A value representing the newly inserted global string variable. public func addGlobalString(name: String, value: String) -> Global { - let length = value.utf8.count - - var global = addGlobal(name, type: - ArrayType(elementType: IntType.int8, count: length + 1)) - - global.alignment = 1 - global.initializer = value - - return global + return self.module.addGlobalString(name: name, value: value) } /// Build a named global variable containing the characters of the given @@ -1880,7 +1929,7 @@ public class IRBuilder { /// /// - returns: A value representing the newly created alias. public func addAlias(name: String, to aliasee: IRGlobal, type: IRType) -> Alias { - return Alias(llvm: LLVMAddAlias(module.llvm, type.asLLVM(), aliasee.asLLVM(), name)) + return self.module.addAlias(name: name, to: aliasee, type: type) } // MARK: Inline Assembly