Skip to content

Commit 22000f1

Browse files
authored
Merge pull request #152 from CodaFi/globalism
Allow adding globals at the level of a module
2 parents ed22f23 + c8e2386 commit 22000f1

File tree

1 file changed

+69
-20
lines changed

1 file changed

+69
-20
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,71 @@ extension Module {
352352
guard let comdat = LLVMGetOrInsertComdat(llvm, name) else { fatalError() }
353353
return Comdat(llvm: comdat)
354354
}
355+
356+
/// Build a named global of the given type.
357+
///
358+
/// - parameter name: The name of the newly inserted global value.
359+
/// - parameter type: The type of the newly inserted global value.
360+
/// - parameter addressSpace: The optional address space where the global
361+
/// variable resides.
362+
///
363+
/// - returns: A value representing the newly inserted global variable.
364+
public func addGlobal(_ name: String, type: IRType, addressSpace: Int? = nil) -> Global {
365+
let val: LLVMValueRef
366+
if let addressSpace = addressSpace {
367+
val = LLVMAddGlobalInAddressSpace(llvm, type.asLLVM(), name, UInt32(addressSpace))
368+
} else {
369+
val = LLVMAddGlobal(llvm, type.asLLVM(), name)
370+
}
371+
return Global(llvm: val)
372+
}
373+
374+
/// Build a named global of the given type.
375+
///
376+
/// - parameter name: The name of the newly inserted global value.
377+
/// - parameter initializer: The initial value for the global variable.
378+
/// - parameter addressSpace: The optional address space where the global
379+
/// variable resides.
380+
///
381+
/// - returns: A value representing the newly inserted global variable.
382+
public func addGlobal(_ name: String, initializer: IRValue, addressSpace: Int? = nil) -> Global {
383+
var global = addGlobal(name, type: initializer.type)
384+
global.initializer = initializer
385+
return global
386+
}
387+
388+
/// Build a named global string consisting of an array of `i8` type filled in
389+
/// with the nul terminated string value.
390+
///
391+
/// - parameter name: The name of the newly inserted global string value.
392+
/// - parameter value: The character contents of the newly inserted global.
393+
///
394+
/// - returns: A value representing the newly inserted global string variable.
395+
public func addGlobalString(name: String, value: String) -> Global {
396+
let length = value.utf8.count
397+
398+
var global = addGlobal(name, type:
399+
ArrayType(elementType: IntType.int8, count: length + 1))
400+
401+
global.alignment = 1
402+
global.initializer = value
403+
404+
return global
405+
}
406+
407+
/// Build a named alias to a global value or a constant expression.
408+
///
409+
/// Aliases, unlike function or variables, don’t create any new data. They are
410+
/// just a new symbol and metadata for an existing position.
411+
///
412+
/// - parameter name: The name of the newly inserted alias.
413+
/// - parameter aliasee: The value or constant to alias.
414+
/// - parameter type: The type of the aliased value or expression.
415+
///
416+
/// - returns: A value representing the newly created alias.
417+
public func addAlias(name: String, to aliasee: IRGlobal, type: IRType) -> Alias {
418+
return Alias(llvm: LLVMAddAlias(llvm, type.asLLVM(), aliasee.asLLVM(), name))
419+
}
355420
}
356421

357422
/// An `IRBuilder` is a helper object that generates LLVM instructions. IR
@@ -1803,13 +1868,7 @@ public class IRBuilder {
18031868
///
18041869
/// - returns: A value representing the newly inserted global variable.
18051870
public func addGlobal(_ name: String, type: IRType, addressSpace: Int? = nil) -> Global {
1806-
let llvm: LLVMValueRef
1807-
if let addressSpace = addressSpace {
1808-
llvm = LLVMAddGlobalInAddressSpace(module.llvm, type.asLLVM(), name, UInt32(addressSpace))
1809-
} else {
1810-
llvm = LLVMAddGlobal(module.llvm, type.asLLVM(), name)
1811-
}
1812-
return Global(llvm: llvm)
1871+
return self.module.addGlobal(name, type: type, addressSpace: addressSpace)
18131872
}
18141873

18151874
/// Build a named global of the given type.
@@ -1821,9 +1880,7 @@ public class IRBuilder {
18211880
///
18221881
/// - returns: A value representing the newly inserted global variable.
18231882
public func addGlobal(_ name: String, initializer: IRValue, addressSpace: Int? = nil) -> Global {
1824-
var global = addGlobal(name, type: initializer.type)
1825-
global.initializer = initializer
1826-
return global
1883+
return self.module.addGlobal(name, initializer: initializer, addressSpace: addressSpace)
18271884
}
18281885

18291886
/// Build a named global string consisting of an array of `i8` type filled in
@@ -1834,15 +1891,7 @@ public class IRBuilder {
18341891
///
18351892
/// - returns: A value representing the newly inserted global string variable.
18361893
public func addGlobalString(name: String, value: String) -> Global {
1837-
let length = value.utf8.count
1838-
1839-
var global = addGlobal(name, type:
1840-
ArrayType(elementType: IntType.int8, count: length + 1))
1841-
1842-
global.alignment = 1
1843-
global.initializer = value
1844-
1845-
return global
1894+
return self.module.addGlobalString(name: name, value: value)
18461895
}
18471896

18481897
/// Build a named global variable containing the characters of the given
@@ -1880,7 +1929,7 @@ public class IRBuilder {
18801929
///
18811930
/// - returns: A value representing the newly created alias.
18821931
public func addAlias(name: String, to aliasee: IRGlobal, type: IRType) -> Alias {
1883-
return Alias(llvm: LLVMAddAlias(module.llvm, type.asLLVM(), aliasee.asLLVM(), name))
1932+
return self.module.addAlias(name: name, to: aliasee, type: type)
18841933
}
18851934

18861935
// MARK: Inline Assembly

0 commit comments

Comments
 (0)