@@ -352,6 +352,71 @@ extension Module {
352
352
guard let comdat = LLVMGetOrInsertComdat ( llvm, name) else { fatalError ( ) }
353
353
return Comdat ( llvm: comdat)
354
354
}
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
+ }
355
420
}
356
421
357
422
/// An `IRBuilder` is a helper object that generates LLVM instructions. IR
@@ -1803,13 +1868,7 @@ public class IRBuilder {
1803
1868
///
1804
1869
/// - returns: A value representing the newly inserted global variable.
1805
1870
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)
1813
1872
}
1814
1873
1815
1874
/// Build a named global of the given type.
@@ -1821,9 +1880,7 @@ public class IRBuilder {
1821
1880
///
1822
1881
/// - returns: A value representing the newly inserted global variable.
1823
1882
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)
1827
1884
}
1828
1885
1829
1886
/// Build a named global string consisting of an array of `i8` type filled in
@@ -1834,15 +1891,7 @@ public class IRBuilder {
1834
1891
///
1835
1892
/// - returns: A value representing the newly inserted global string variable.
1836
1893
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)
1846
1895
}
1847
1896
1848
1897
/// Build a named global variable containing the characters of the given
@@ -1880,7 +1929,7 @@ public class IRBuilder {
1880
1929
///
1881
1930
/// - returns: A value representing the newly created alias.
1882
1931
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 )
1884
1933
}
1885
1934
1886
1935
// MARK: Inline Assembly
0 commit comments