Skip to content

Allow adding globals at the level of a module #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 69 additions & 20 deletions Sources/LLVM/IRBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down