Skip to content

Commit 8cb23ea

Browse files
committed
XXX
1 parent c50633f commit 8cb23ea

File tree

6 files changed

+102
-25
lines changed

6 files changed

+102
-25
lines changed

Sources/LLVM/ArrayType.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cllvm
55
/// `ArrayType` is a very simple derived type that arranges elements
66
/// sequentially in memory. `ArrayType` requires a size (number of elements) and
77
/// an underlying data type.
8-
public struct ArrayType: IRType {
8+
public struct ArrayType: IRType, DIType {
99
/// The type of elements in this array.
1010
public let elementType: IRType
1111
/// The number of elements in this array.
@@ -34,4 +34,29 @@ public struct ArrayType: IRType {
3434
public func asLLVM() -> LLVMTypeRef {
3535
return LLVMArrayType(elementType.asLLVM(), UInt32(count))
3636
}
37+
38+
public func asMetadata(with builder: DIBuilder) -> LLVMMetadataRef {
39+
let elemDITy : LLVMMetadataRef
40+
if let elemDI = self.elementType as? DIType {
41+
elemDITy = elemDI.asMetadata(with: builder)
42+
} else {
43+
elemDITy = LLVMDIBuilderCreateUnspecifiedType(builder.llvm, "<unspecified>")
44+
}
45+
46+
var type : IRType = self
47+
var subscripts : [LLVMMetadataRef?] = []
48+
while let arrTy = type as? ArrayType {
49+
subscripts.append(LLVMDIBuilderGetOrCreateSubrange(builder.llvm, 0, Int64(arrTy.count)))
50+
type = arrTy.elementType
51+
}
52+
53+
return subscripts.withUnsafeMutableBufferPointer { buf in
54+
return LLVMDIBuilderCreateArrayType(
55+
builder.llvm, UInt64(self.count),
56+
UInt32(builder.module.dataLayout.abiAlignment(of: self)),
57+
elemDITy, buf.baseAddress!, UInt32(subscripts.count)
58+
)
59+
}
60+
}
61+
3762
}

Sources/LLVM/DIBuilder.swift

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,14 @@ public struct DICompileUnit: Metadata {
534534
}
535535
}
536536

537+
public struct DIExpression: Metadata {
538+
internal let llvm: LLVMMetadataRef
539+
540+
public func asMetadata() -> LLVMMetadataRef {
541+
return llvm
542+
}
543+
}
544+
537545

538546
public struct DebugLocation: Metadata {
539547
internal let llvm: LLVMMetadataRef
@@ -600,14 +608,12 @@ public final class DIBuilder {
600608
}
601609

602610
/// Insert a new llvm.dbg.value intrinsic call.
603-
// FIXME: complex address expression?
604-
public func insertDebugValueIntrinsicAtEnd(for value: IRValue, at offset: Int, with info: VariableMetadata, at loc: DebugLocation, in bb: BasicBlock, expr: Metadata? = nil) {
611+
public func insertDebugValueIntrinsicAtEnd(for value: IRValue, at offset: Int, with info: VariableMetadata, at loc: DebugLocation, in bb: BasicBlock, expr: DIExpression? = nil) {
605612
LLVMDIBuilderInsertDbgValueIntrinsicAtEnd(self.llvm, value.asLLVM(), UInt64(offset), info.llvm, expr?.asMetadata(), loc.llvm, bb.asLLVM())
606613
}
607614

608615
/// Insert a new llvm.dbg.value intrinsic call.
609-
// FIXME: complex address expression?
610-
public func insertDebugValueIntrinsicBefore(for value: IRValue, at offset: Int, with info: VariableMetadata, at loc: DebugLocation, in bb: BasicBlock, expr: Metadata? = nil) {
616+
public func insertDebugValueIntrinsicBefore(for value: IRValue, at offset: Int, with info: VariableMetadata, at loc: DebugLocation, in bb: BasicBlock, expr: DIExpression? = nil) {
611617
LLVMDIBuilderInsertDbgValueIntrinsicBefore(self.llvm, value.asLLVM(), UInt64(offset), info.llvm, expr?.asMetadata(), loc.llvm, bb.asLLVM())
612618
}
613619

@@ -674,6 +680,28 @@ public final class DIBuilder {
674680
return VariableMetadata(llvm: vari)
675681
}
676682

683+
/// Create debugging information entry for a struct.
684+
public func createStructType(for type: StructType, in scope: Scope, in file: FileMetadata, at line: Int, flags: DebugInfoFlags = .zero, uniqueID: String) -> Metadata {
685+
var elemMetadata = type.elementTypes.map { (ty) -> LLVMMetadataRef? in
686+
if let elemDI = ty as? DIType {
687+
return elemDI.asMetadata(with: self)
688+
} else {
689+
return LLVMDIBuilderCreateUnspecifiedType(self.llvm, "<unspecified>")
690+
}
691+
}
692+
693+
return elemMetadata.withUnsafeMutableBufferPointer { buf in
694+
return AnyMetadata(llvm:
695+
LLVMDIBuilderCreateStructType(
696+
self.llvm, scope.llvm, type.name, file.llvm, UInt32(line),
697+
UInt64(self.module.dataLayout.sizeOfTypeInBits(type)),
698+
UInt32(self.module.dataLayout.abiAlignment(of: type)),
699+
flags.llvm, nil,
700+
buf.baseAddress, UInt32(elemMetadata.count),
701+
0, nil, uniqueID
702+
))
703+
}
704+
}
677705

678706
/// Construct any deferred debug info descriptors.
679707
public func finalize() {
@@ -828,21 +856,6 @@ public func LLVMDIBuilderCreateBasicType(_ Builder: LLVMDIBuilderRef!, _ Name: U
828856
/// \param Name Pointer type name. (optional)
829857
public func LLVMDIBuilderCreatePointerType(_ Builder: LLVMDIBuilderRef!, _ PointeeTy: LLVMMetadataRef!, _ SizeInBits: UInt64, _ AlignInBits: UInt32, _ AddressSpace: UInt32, _ Name: UnsafePointer<Int8>!) -> LLVMMetadataRef!
830858

831-
/// Create debugging information entry for a struct.
832-
/// \param Builder The DIBuilder.
833-
/// \param Scope Scope in which this struct is defined.
834-
/// \param Name Struct name.
835-
/// \param File File where this member is defined.
836-
/// \param LineNumber Line number.
837-
/// \param SizeInBits Member size.
838-
/// \param AlignInBits Member alignment.
839-
/// \param Flags Flags to encode member attribute, e.g. private
840-
/// \param Elements Struct elements.
841-
/// \param RunTimeLang Optional parameter, Objective-C runtime version.
842-
/// \param VTableHolder The object containing the vtable for the struct.
843-
/// \param UniqueIdentifier A unique identifier for the struct.
844-
public func LLVMDIBuilderCreateStructType(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ Name: UnsafePointer<Int8>!, _ File: LLVMMetadataRef!, _ LineNumber: UInt32, _ SizeInBits: UInt64, _ AlignInBits: UInt32, _ Flags: LLVMDIFlags, _ DerivedFrom: LLVMMetadataRef!, _ Elements: UnsafeMutablePointer<LLVMMetadataRef?>!, _ NumElements: UInt32, _ RunTimeLang: UInt32, _ VTableHolder: LLVMMetadataRef!, _ UniqueId: UnsafePointer<Int8>!) -> LLVMMetadataRef!
845-
846859
/// Create debugging information entry for a member.
847860
/// \param Builder The DIBuilder.
848861
/// \param Scope Member scope.

Sources/LLVM/FloatType.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public enum FloatType: IRType, DIType {
5252
name = "ppcfloat128"
5353
}
5454
return LLVMDIBuilderCreateBasicType(
55-
builder.llvm,
56-
name,
55+
builder.llvm, name,
5756
UInt64(builder.module.dataLayout.sizeOfTypeInBits(self)),
5857
DWARFTypeEncoding.float.llvm
5958
)

Sources/LLVM/IntType.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import cllvm
77
/// The `IntType` is a very simple type that simply specifies an arbitrary bit
88
/// width for the integer type desired. Any bit width from 1 bit to (2^23)-1
99
/// (about 8 million) can be specified.
10-
public struct IntType: IRType {
10+
public struct IntType: IRType, DIType {
1111
/// Retrieves the bit width of this integer type.
1212
public let width: Int
1313

@@ -70,4 +70,11 @@ public struct IntType: IRType {
7070
public func asLLVM() -> LLVMTypeRef {
7171
return LLVMIntType(UInt32(width))
7272
}
73+
74+
public func asMetadata(with builder: DIBuilder) -> LLVMMetadataRef {
75+
return LLVMDIBuilderCreateBasicType(
76+
builder.llvm, "int\(self.width)",
77+
UInt64(self.width), DWARFTypeEncoding.signed.llvm
78+
)
79+
}
7380
}

Sources/LLVM/PointerType.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import cllvm
1212
///
1313
/// Note that LLVM does not permit pointers to void `(void*)` nor does it permit
1414
/// pointers to labels `(label*)`. Use `i8*` instead.
15-
public struct PointerType: IRType {
15+
public struct PointerType: IRType, DIType {
1616
/// Retrieves the type of the value being pointed to.
1717
public let pointee: IRType
1818
/// Retrieves the address space where the pointed-to object resides.
@@ -35,4 +35,20 @@ public struct PointerType: IRType {
3535
public func asLLVM() -> LLVMTypeRef {
3636
return LLVMPointerType(pointee.asLLVM(), UInt32(addressSpace))
3737
}
38+
39+
public func asMetadata(with builder: DIBuilder) -> LLVMMetadataRef {
40+
let pointeeTy : LLVMMetadataRef
41+
if let pointeeDI = self.pointee as? DIType {
42+
pointeeTy = pointeeDI.asMetadata(with: builder)
43+
} else {
44+
pointeeTy = LLVMDIBuilderCreateUnspecifiedType(builder.llvm, "<unspecified>")
45+
}
46+
return LLVMDIBuilderCreatePointerType(
47+
builder.llvm, pointeeTy,
48+
UInt64(builder.module.dataLayout.sizeOfTypeInBits(self)),
49+
UInt32(builder.module.dataLayout.abiAlignment(of: self)),
50+
UInt32(self.addressSpace),
51+
"PointerType"
52+
)
53+
}
3854
}

Sources/LLVM/VectorType.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import cllvm
66
/// elements. `VectorType`s are used when multiple primitive data are operated
77
/// in parallel using a single instruction (SIMD). A vector type requires a size
88
/// (number of elements) and an underlying primitive data type.
9-
public struct VectorType: IRType {
9+
public struct VectorType: IRType, DIType {
1010
/// Returns the type of elements in the vector.
1111
public let elementType: IRType
1212
/// Returns the number of elements in the vector.
@@ -25,4 +25,21 @@ public struct VectorType: IRType {
2525
public func asLLVM() -> LLVMTypeRef {
2626
return LLVMVectorType(elementType.asLLVM(), UInt32(count))
2727
}
28+
29+
public func asMetadata(with builder: DIBuilder) -> LLVMMetadataRef {
30+
let elemDITy : LLVMMetadataRef
31+
if let elemDI = self.elementType as? DIType {
32+
elemDITy = elemDI.asMetadata(with: builder)
33+
} else {
34+
elemDITy = LLVMDIBuilderCreateUnspecifiedType(builder.llvm, "<unspecified>")
35+
}
36+
37+
var ranges : [LLVMMetadataRef?] = [ LLVMDIBuilderGetOrCreateSubrange(builder.llvm, 0, Int64(self.count)).asLLVM() ]
38+
return ranges.withUnsafeMutableBufferPointer { buf in
39+
return LLVMDIBuilderCreateVectorType(
40+
builder.llvm, UInt64(self.count),
41+
UInt32(builder.module.dataLayout.abiAlignment(of: self)),
42+
elemDITy, buf.baseAddress!, UInt32(ranges.count))
43+
}
44+
}
2845
}

0 commit comments

Comments
 (0)