Skip to content

Commit faa2ada

Browse files
authored
Merge pull request #157 from CodaFi/more-globals
Correct some more constant mistakes
2 parents dece2fc + 9ed4e23 commit faa2ada

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

Sources/LLVM/Constant.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ extension IRConstant {
2121
return Constant<Struct>(llvm: LLVMConstGEP(asLLVM(), buf.baseAddress, UInt32(buf.count)))
2222
}
2323
}
24+
25+
/// Build a constant bitcast to convert the given value to a value of the
26+
/// given type by just copying the bit pattern.
27+
///
28+
/// - parameter type: The destination type.
29+
///
30+
/// - returns: A constant value representing the result of bitcasting this
31+
/// constant value to fit the given type.
32+
public func bitCast(to type: IRType) -> IRConstant {
33+
return Constant<Struct>(llvm: LLVMConstBitCast(asLLVM(), type.asLLVM()))
34+
}
2435
}
2536

2637
/// A protocol to which the phantom types for a constant's representation conform.
@@ -1014,7 +1025,7 @@ extension Constant where Repr: IntegralConstantRepresentation {
10141025
///
10151026
/// - returns: An constant value representing the constant value of the given
10161027
/// pointer converted to the given integer type.
1017-
public static func pointerToInt(_ val: IRGlobal, _ intType: IntType) -> Constant {
1028+
public static func pointerToInt(_ val: IRConstant, _ intType: IntType) -> Constant {
10181029
precondition(val.isConstant, "May only convert global constant pointers to integers")
10191030
return Constant<Repr>(llvm: LLVMConstPtrToInt(val.asLLVM(), intType.asLLVM()))
10201031
}

Sources/LLVM/Global.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ public enum ThreadLocalModel {
7979
/// A `Global` represents a region of memory allocated at compile time instead
8080
/// of at runtime. A global variable must either have an initializer, or make
8181
/// reference to an external definition that has an initializer.
82-
public struct Global: IRGlobal {
82+
public final class Global: IRGlobal {
8383
internal let llvm: LLVMValueRef
8484

85+
internal init(llvm: LLVMValueRef) {
86+
self.llvm = llvm
87+
}
88+
8589
/// Returns whether this global variable has no initializer because it makes
8690
/// reference to an initialized value in another translation unit.
8791
public var isExternallyInitialized: Bool {

Sources/LLVM/IRBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ extension Module {
380380
///
381381
/// - returns: A value representing the newly inserted global variable.
382382
public func addGlobal(_ name: String, initializer: IRValue, addressSpace: Int? = nil) -> Global {
383-
var global = addGlobal(name, type: initializer.type)
383+
let global = addGlobal(name, type: initializer.type)
384384
global.initializer = initializer
385385
return global
386386
}

0 commit comments

Comments
 (0)