Skip to content

Allow making reference to global variables. #22

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 2 commits into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
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
43 changes: 40 additions & 3 deletions Sources/LLVM/IRBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -935,7 +972,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.
///
Expand Down Expand Up @@ -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
Expand Down
24 changes: 0 additions & 24 deletions Sources/LLVM/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down