Skip to content

Commit a4e59f7

Browse files
authored
Merge pull request #155 from CodaFi/irconstant
Add IRConstant protocol
2 parents da0ac16 + 68a0b54 commit a4e59f7

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

Sources/LLVM/Constant.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
import cllvm
33
#endif
44

5+
/// An `IRConstant` is an entity whose value doees not change during the
6+
/// runtime of a program. This includes global variables and functions, whose
7+
/// addresses are constant, and constant expressions.
8+
public protocol IRConstant: IRValue {}
9+
510
/// A protocol to which the phantom types for a constant's representation conform.
611
public protocol ConstantRepresentation {}
712
/// A protocol to which the phantom types for all numerical constants conform.
@@ -29,7 +34,7 @@ public enum Vector: ConstantRepresentation {}
2934
/// `Constant`s keep track of the values they represent at the type level to
3035
/// disallow mixed-type arithmetic. Use the `cast` family of operations to
3136
/// safely convert constants to other representations.
32-
public struct Constant<Repr: ConstantRepresentation>: IRValue {
37+
public struct Constant<Repr: ConstantRepresentation>: IRConstant {
3338
internal let llvm: LLVMValueRef
3439

3540
internal init(llvm: LLVMValueRef!) {
@@ -1009,10 +1014,10 @@ extension Constant where Repr == Struct {
10091014
/// of the aggregate object are indexed.
10101015
///
10111016
/// - returns: The value in the struct at the provided index.
1012-
public func getElement(indices: [Int]) -> IRValue {
1017+
public func getElement(indices: [Int]) -> IRConstant {
10131018
var indices = indices.map({ UInt32($0) })
10141019
return indices.withUnsafeMutableBufferPointer { buf in
1015-
return LLVMConstExtractValue(asLLVM(), buf.baseAddress, UInt32(buf.count))
1020+
return Constant<Struct>(llvm: LLVMConstExtractValue(asLLVM(), buf.baseAddress, UInt32(buf.count)))
10161021
}
10171022
}
10181023

@@ -1029,10 +1034,10 @@ extension Constant where Repr == Struct {
10291034
///
10301035
/// - returns: A value representing the address of a subelement of the given
10311036
/// aggregate data structure value.
1032-
public func getElementPointer(indices: [IRValue]) -> IRValue {
1037+
public func getElementPointer(indices: [IRConstant]) -> IRConstant {
10331038
var indices = indices.map({ $0.asLLVM() as LLVMValueRef? })
10341039
return indices.withUnsafeMutableBufferPointer { buf in
1035-
return LLVMConstGEP(asLLVM(), buf.baseAddress, UInt32(buf.count))
1040+
return Constant<Struct>(llvm: LLVMConstGEP(asLLVM(), buf.baseAddress, UInt32(buf.count)))
10361041
}
10371042
}
10381043

@@ -1047,10 +1052,10 @@ extension Constant where Repr == Struct {
10471052
///
10481053
/// - returns: A value representing the address of a subelement of the given
10491054
/// aggregate data structure value.
1050-
public func inBoundsGetElementPointer(indices: [IRValue]) -> IRValue {
1055+
public func inBoundsGetElementPointer(indices: [IRConstant]) -> IRConstant {
10511056
var indices = indices.map({ $0.asLLVM() as LLVMValueRef? })
10521057
return indices.withUnsafeMutableBufferPointer { buf in
1053-
return LLVMConstInBoundsGEP(asLLVM(), buf.baseAddress, UInt32(buf.count))
1058+
return Constant<Struct>(llvm: LLVMConstInBoundsGEP(asLLVM(), buf.baseAddress, UInt32(buf.count)))
10541059
}
10551060
}
10561061
}

Sources/LLVM/IRGlobal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import cllvm
44

55
/// An `IRGlobal` is a value, alias, or function that exists at the top level of
66
/// an LLVM module.
7-
public protocol IRGlobal: IRValue {}
7+
public protocol IRGlobal: IRConstant {}
88

99
extension IRGlobal {
1010
/// Retrieves the alignment of this value.

0 commit comments

Comments
 (0)