Skip to content

Commit 3ab2d0b

Browse files
committed
Added context to all types
1 parent 098575a commit 3ab2d0b

File tree

8 files changed

+95
-18
lines changed

8 files changed

+95
-18
lines changed

Sources/LLVM/ArrayType.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public struct ArrayType: IRType {
1212
public let count: Int
1313

1414
/// Creates an array type from an underlying element type and count.
15+
/// - note: The context of this type is taken from it's `elementType`
1516
public init(elementType: IRType, count: Int) {
1617
self.elementType = elementType
1718
self.count = count

Sources/LLVM/FloatType.swift

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,51 @@ import cllvm
44

55
/// `FloatType` enumerates representations of a floating value of a particular
66
/// bit width and semantics.
7-
public enum FloatType: IRType {
8-
/// 16-bit floating point value
9-
case half
10-
/// 32-bit floating point value
11-
case float
12-
/// 64-bit floating point value
13-
case double
14-
/// 80-bit floating point value (X87)
15-
case x86FP80
16-
/// 128-bit floating point value (112-bit mantissa)
17-
case fp128
18-
/// 128-bit floating point value (two 64-bits)
19-
case ppcFP128
7+
public struct FloatType: IRType {
8+
9+
/// The kind of floating point type this is
10+
public var kind: Kind
11+
12+
/// Returns the context associated with this module.
13+
public let context: Context?
14+
15+
/// Creates a float type of a particular kind
16+
///
17+
/// - parameter kind: The kind of floating point type to create
18+
/// - parameter context: The context to create this type in
19+
/// - SeeAlso: http://llvm.org/docs/ProgrammersManual.html#achieving-isolation-with-llvmcontext
20+
public init(kind: Kind, in context: Context? = nil) {
21+
self.kind = kind
22+
self.context = context
23+
}
24+
25+
public enum Kind {
26+
/// 16-bit floating point value
27+
case half
28+
/// 32-bit floating point value
29+
case float
30+
/// 64-bit floating point value
31+
case double
32+
/// 80-bit floating point value (X87)
33+
case x86FP80
34+
/// 128-bit floating point value (112-bit mantissa)
35+
case fp128
36+
/// 128-bit floating point value (two 64-bits)
37+
case ppcFP128
38+
}
39+
40+
/// 16-bit floating point value in the global context
41+
static let half = FloatType(kind: .half)
42+
/// 32-bit floating point value in the global context
43+
static let float = FloatType(kind: .float)
44+
/// 64-bit floating point value in the global context
45+
static let double = FloatType(kind: .double)
46+
/// 80-bit floating point value (X87) in the global context
47+
static let x86FP80 = FloatType(kind: .x86FP80)
48+
/// 128-bit floating point value (112-bit mantissa) in the global context
49+
static let fp128 = FloatType(kind: .fp128)
50+
/// 128-bit floating point value (two 64-bits) in the global context
51+
static let ppcFP128 = FloatType(kind: .ppcFP128)
2052

2153
/// Creates a constant floating value of this type from a Swift `Double` value.
2254
public func constant(_ value: Double) -> Constant<Floating> {
@@ -25,7 +57,17 @@ public enum FloatType: IRType {
2557

2658
/// Retrieves the underlying LLVM type object.
2759
public func asLLVM() -> LLVMTypeRef {
28-
switch self {
60+
if let context = context {
61+
switch kind {
62+
case .half: return LLVMHalfTypeInContext(context.llvm)
63+
case .float: return LLVMFloatTypeInContext(context.llvm)
64+
case .double: return LLVMDoubleTypeInContext(context.llvm)
65+
case .x86FP80: return LLVMX86FP80TypeInContext(context.llvm)
66+
case .fp128: return LLVMFP128TypeInContext(context.llvm)
67+
case .ppcFP128: return LLVMPPCFP128TypeInContext(context.llvm)
68+
}
69+
}
70+
switch kind {
2971
case .half: return LLVMHalfType()
3072
case .float: return LLVMFloatType()
3173
case .double: return LLVMDoubleType()

Sources/LLVM/FunctionType.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public struct FunctionType: IRType {
2020
/// - parameter returnType: The return type of the function type.
2121
/// - parameter isVarArg: Indicates whether this function type is variadic.
2222
/// Defaults to `false`.
23+
/// - note: The context of this type is taken from it's `returnType`
2324
public init(argTypes: [IRType], returnType: IRType, isVarArg: Bool = false) {
2425
self.argTypes = argTypes
2526
self.returnType = returnType

Sources/LLVM/IntType.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@ public struct IntType: IRType {
1111
/// Retrieves the bit width of this integer type.
1212
public let width: Int
1313

14+
/// Returns the context associated with this module.
15+
public let context: Context?
16+
1417
/// Creates an integer type with the specified bit width.
15-
public init(width: Int) { self.width = width }
18+
///
19+
/// - parameter width: The width in bits of the integer type
20+
/// - parameter context: The context to create this type in
21+
/// - SeeAlso: http://llvm.org/docs/ProgrammersManual.html#achieving-isolation-with-llvmcontext
22+
public init(width: Int, in context: Context? = nil) {
23+
self.width = width
24+
self.context = context
25+
}
1626

1727
/// Retrieves the `i1` type.
1828
public static let int1 = IntType(width: 1)
@@ -68,6 +78,9 @@ public struct IntType: IRType {
6878

6979
/// Retrieves the underlying LLVM type object.
7080
public func asLLVM() -> LLVMTypeRef {
81+
if let context = context {
82+
return LLVMIntTypeInContext(context.llvm, UInt32(width))
83+
}
7184
return LLVMIntType(UInt32(width))
7285
}
7386
}

Sources/LLVM/PointerType.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public struct PointerType: IRType {
2323
/// - parameter pointee: The type of the pointed-to object.
2424
/// - parameter addressSpace: The optional address space where the pointed-to
2525
/// object resides.
26+
/// - note: The context of this type is taken from it's pointee
2627
public init(pointee: IRType, addressSpace: Int = 0) {
2728
// FIXME: This class of invalid reference is not caught by Module.verify(),
2829
// only `lli`.

Sources/LLVM/StructType.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ public struct StructType: IRType {
3535
/// - parameter elementTypes: A list of types of members of this structure.
3636
/// - parameter isPacked: Whether or not this structure is 1-byte aligned with
3737
/// no packing between fields. Defaults to `false`.
38-
public init(elementTypes: [IRType], isPacked: Bool = false) {
38+
/// - parameter context: The context to create this type in
39+
/// - SeeAlso: http://llvm.org/docs/ProgrammersManual.html#achieving-isolation-with-llvmcontext
40+
public init(elementTypes: [IRType], isPacked: Bool = false, in context: Context? = nil) {
3941
var irTypes = elementTypes.map { $0.asLLVM() as Optional }
4042
self.llvm = irTypes.withUnsafeMutableBufferPointer { buf in
41-
LLVMStructType(buf.baseAddress, UInt32(buf.count), isPacked.llvm)
43+
if let context = context {
44+
return LLVMStructTypeInContext(context.llvm, buf.baseAddress, UInt32(buf.count), isPacked.llvm)
45+
} else {
46+
return LLVMStructType(buf.baseAddress, UInt32(buf.count), isPacked.llvm)
47+
}
4248
}
4349
}
4450

Sources/LLVM/VectorType.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public struct VectorType: IRType {
1616
///
1717
/// - parameter elementType: The type of elements of this vector.
1818
/// - parameter count: The number of elements in this vector.
19+
/// - note: The context of this type is taken from it's `elementType`
1920
public init(elementType: IRType, count: Int) {
2021
self.elementType = elementType
2122
self.count = count

Sources/LLVM/VoidType.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@ import cllvm
44

55
/// The `Void` type represents any value and has no size.
66
public struct VoidType: IRType {
7+
8+
/// Returns the context associated with this module.
9+
public let context: Context?
10+
711
/// Creates an instance of the `Void` type.
8-
public init() {}
12+
///
13+
/// - parameter context: The context to create this type in
14+
/// - SeeAlso: http://llvm.org/docs/ProgrammersManual.html#achieving-isolation-with-llvmcontext
15+
public init(in context: Context? = nil) {
16+
self.context = context
17+
}
918

1019
/// Retrieves the underlying LLVM type object.
1120
public func asLLVM() -> LLVMTypeRef {
21+
if let context = context {
22+
return LLVMVoidTypeInContext(context.llvm)
23+
}
1224
return LLVMVoidType()
1325
}
1426
}

0 commit comments

Comments
 (0)