Skip to content

Commit 162f163

Browse files
authored
Merge pull request #163 from CodaFi/documentary-crew
Restore Documentation to 100%
2 parents a82c156 + 1279f11 commit 162f163

File tree

270 files changed

+118175
-7243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

270 files changed

+118175
-7243
lines changed

Sources/LLVM/JIT.swift

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import cllvm
55
/// JITError represents the different kinds of errors the JIT compiler can
66
/// throw.
77
public enum JITError: Error, CustomStringConvertible {
8+
/// A generic error thrown by the JIT during exceptional circumstances.
9+
///
10+
/// In general, it is not safe to catch and continue after this exception has
11+
/// been thrown.
812
case generic(String)
913

1014
public var description: String {
@@ -23,7 +27,31 @@ public enum JITError: Error, CustomStringConvertible {
2327
/// The JIT is fundamentally lazy, and allows control over when and how symbols
2428
/// are resolved.
2529
public final class JIT {
26-
public typealias TargetAddress = LLVMOrcTargetAddress
30+
/// A type that represents an address, either symbolically within the JIT or
31+
/// physically in the execution environment.
32+
public struct TargetAddress: Comparable {
33+
fileprivate var llvm: LLVMOrcTargetAddress
34+
35+
/// Creates a target address value of `0`.
36+
public init() {
37+
self.llvm = 0
38+
}
39+
40+
/// Creates a target address from a raw address value.
41+
public init(raw: LLVMOrcTargetAddress) {
42+
self.llvm = raw
43+
}
44+
45+
public static func == (lhs: TargetAddress, rhs: TargetAddress) -> Bool {
46+
return lhs.llvm == rhs.llvm
47+
}
48+
49+
public static func < (lhs: TargetAddress, rhs: TargetAddress) -> Bool {
50+
return lhs.llvm < rhs.llvm
51+
}
52+
}
53+
54+
/// Represents a handle to a module owned by the JIT stack.
2755
public struct ModuleHandle {
2856
fileprivate var llvm: LLVMOrcModuleHandle
2957
}
@@ -77,13 +105,13 @@ public final class JIT {
77105
/// restrict the search, if any.
78106
/// - returns: The address of the symbol, or 0 if it does not exist.
79107
public func address(of symbol: String, in module: ModuleHandle? = nil) throws -> TargetAddress {
80-
var retAddr: TargetAddress = 0
108+
var retAddr: LLVMOrcTargetAddress = 0
81109
if let targetModule = module {
82110
try checkForJITError(LLVMOrcGetSymbolAddressIn(self.llvm, &retAddr, targetModule.llvm, symbol))
83111
} else {
84112
try checkForJITError(LLVMOrcGetSymbolAddress(self.llvm, &retAddr, symbol))
85113
}
86-
return retAddr
114+
return TargetAddress(raw: retAddr)
87115
}
88116

89117
// MARK: Lazy Compilation
@@ -103,11 +131,11 @@ public final class JIT {
103131
/// - returns: The target address representing a stub. Calling this stub
104132
/// forces the given compilation callback to fire.
105133
public func registerLazyCompile(_ callback: @escaping (JIT) -> TargetAddress) throws -> TargetAddress {
106-
var addr: TargetAddress = 0
134+
var addr: LLVMOrcTargetAddress = 0
107135
let callbackContext = ORCLazyCompileCallbackContext(callback)
108136
let contextPtr = Unmanaged<ORCLazyCompileCallbackContext>.passRetained(callbackContext).toOpaque()
109137
try checkForJITError(LLVMOrcCreateLazyCompileCallback(self.llvm, &addr, lazyCompileBlockTrampoline, contextPtr))
110-
return addr
138+
return TargetAddress(raw: addr)
111139
}
112140

113141
// MARK: Stubs
@@ -120,7 +148,7 @@ public final class JIT {
120148
/// - parameter name: The name of the indirect stub.
121149
/// - parameter address: The address of the indirect stub.
122150
public func createIndirectStub(named name: String, address: TargetAddress) throws {
123-
try checkForJITError(LLVMOrcCreateIndirectStub(self.llvm, name, address))
151+
try checkForJITError(LLVMOrcCreateIndirectStub(self.llvm, name, address.llvm))
124152
}
125153

126154
/// Resets the address of an indirect stub.
@@ -132,7 +160,7 @@ public final class JIT {
132160
/// - parameter name: The name of an indirect stub.
133161
/// - parameter address: The address to set the indirect stub to point to.
134162
public func setIndirectStubPointer(named name: String, address: TargetAddress) throws {
135-
try checkForJITError(LLVMOrcSetIndirectStubPointer(self.llvm, name, address))
163+
try checkForJITError(LLVMOrcSetIndirectStubPointer(self.llvm, name, address.llvm))
136164
}
137165

138166
// MARK: Adding Code to the JIT
@@ -251,7 +279,7 @@ private let lazyCompileBlockTrampoline : LLVMOrcLazyCompileCallbackFn = { (callb
251279

252280
let tempJIT = JIT(llvm: jit, ownsContext: false)
253281
let callback = Unmanaged<ORCLazyCompileCallbackContext>.fromOpaque(ctx).takeUnretainedValue()
254-
return callback.block(tempJIT)
282+
return callback.block(tempJIT).llvm
255283
}
256284

257285
private let symbolBlockTrampoline : LLVMOrcSymbolResolverFn = { (callbackName, callbackCtx) in
@@ -261,7 +289,7 @@ private let symbolBlockTrampoline : LLVMOrcSymbolResolverFn = { (callbackName, c
261289

262290
let name = String(cString: cname)
263291
let callback = Unmanaged<ORCSymbolCallbackContext>.fromOpaque(ctx).takeUnretainedValue()
264-
return callback.block(name)
292+
return callback.block(name).llvm
265293
}
266294

267295
private class ORCLazyCompileCallbackContext {

Sources/LLVM/Metadata.swift

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

5+
/// An unfortunate artifact of the design of the metadata class hierarchy is
6+
/// that you may find it convenient to force-cast metadata nodes. In general,
7+
/// this behavior is not encouraged, but it will be supported for now.
58
public protocol _IRMetadataInitializerHack {
9+
/// Initialize a metadata node from a raw LLVM metadata ref.
610
init(llvm: LLVMMetadataRef)
711
}
812

@@ -38,6 +42,7 @@ public protocol _IRMetadataInitializerHack {
3842
/// formats. This allows compatibility with traditional machine-code level
3943
/// debuggers, like GDB, DBX, or CodeView.
4044
public protocol Metadata: _IRMetadataInitializerHack {
45+
/// Retrieves the underlying LLVM metadata object.
4146
func asMetadata() -> LLVMMetadataRef
4247
}
4348

@@ -56,6 +61,11 @@ extension Metadata {
5661
LLVMDumpValue(LLVMMetadataAsValue(LLVMGetGlobalContext(), self.asMetadata()))
5762
}
5863

64+
/// Force-casts metadata to a destination type.
65+
///
66+
/// - warning: In general, use of this method is discouraged and can
67+
/// lead to unpredictable results or undefined behavior. No checks are
68+
/// performed before, during, or after the cast.
5969
public func forceCast<DestTy: Metadata>(to: DestTy.Type) -> DestTy {
6070
return DestTy(llvm: self.asMetadata())
6171
}
@@ -300,6 +310,8 @@ public struct ImportedEntityMetadata: DIType {
300310
}
301311
}
302312

313+
/// `NameSpaceMetadata` nodes represent subroutines in the source program.
314+
/// They are attached to corresponding LLVM IR functions.
303315
public struct FunctionMetadata: DIScope {
304316
internal let llvm: LLVMMetadataRef
305317

@@ -312,6 +324,7 @@ public struct FunctionMetadata: DIScope {
312324
}
313325
}
314326

327+
/// `NameSpaceMetadata` nodes represent entities like C++ modules.
315328
public struct ModuleMetadata: DIScope {
316329
internal let llvm: LLVMMetadataRef
317330

@@ -324,6 +337,7 @@ public struct ModuleMetadata: DIScope {
324337
}
325338
}
326339

340+
/// `NameSpaceMetadata` nodes represent entities like C++ namespaces.
327341
public struct NameSpaceMetadata: DIScope {
328342
internal let llvm: LLVMMetadataRef
329343

Sources/LLVM/MetadataAttributes.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ public enum DIAttributeTypeEncoding {
435435
}
436436
}
437437

438+
/// Enumerates a set of flags that can be applied to metadata nodes to change
439+
/// their interpretation at compile time or attach additional semantic
440+
/// significance at runtime.
438441
public struct DIFlags : OptionSet {
439442
public let rawValue: LLVMDIFlags.RawValue
440443

Sources/LLVM/NamedMetadata.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import cllvm
66
/// by a user-provided name. Named metadata is generated lazily when operands
77
/// are attached.
88
public class NamedMetadata {
9+
/// The module with which this named metadata is associated.
910
public let module: Module
11+
/// The name associated with this named metadata.
1012
public let name: String
1113

1214
init(module: Module, name: String) {

Sources/LLVM/Units.swift

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public struct Size {
140140
return Size(value & ~mask)
141141
}
142142

143+
/// Returns the remainder of dividing the first size value
144+
/// by the second alignment value. This has the effect of aligning the
145+
/// size value subtractively.
143146
public static func % (lhs: Size, rhs: Alignment) -> Size {
144147
return Size(lhs.rawValue % UInt64(rhs.rawValue))
145148
}
@@ -232,20 +235,6 @@ extension Size: UnsignedInteger {
232235
lhs = Size(lhs.rawValue * rhs.rawValue)
233236
}
234237

235-
public static func * (lhs: Size, rhs: UInt64) -> Size {
236-
var lhs = lhs
237-
lhs *= Size(rhs)
238-
return lhs
239-
}
240-
public static func * (lhs: UInt64, rhs: Size) -> Size {
241-
var lhs = Size(lhs)
242-
lhs *= rhs
243-
return lhs
244-
}
245-
public static func *= (lhs: inout Size, rhs: UInt64) {
246-
lhs = Size(lhs.rawValue * rhs)
247-
}
248-
249238
public static func / (lhs: Size, rhs: Size) -> Size {
250239
var lhs = lhs
251240
lhs /= rhs
@@ -296,3 +285,25 @@ extension Size: UnsignedInteger {
296285
public typealias IntegerLiteralType = UInt64
297286
public typealias Words = UInt64.Words
298287
}
288+
289+
extension Size {
290+
/// Multiplies a size value by a raw unitless value and produces their product.
291+
public static func * (lhs: Size, rhs: UInt64) -> Size {
292+
var lhs = lhs
293+
lhs *= Size(rhs)
294+
return lhs
295+
}
296+
297+
/// Multiplies a raw unitless value by a size value and produces their product.
298+
public static func * (lhs: UInt64, rhs: Size) -> Size {
299+
var lhs = Size(lhs)
300+
lhs *= rhs
301+
return lhs
302+
}
303+
304+
/// Multiplies a size value by a raw unitless value and stores the result in
305+
/// the left-hand-side size variable.
306+
public static func *= (lhs: inout Size, rhs: UInt64) {
307+
lhs = Size(lhs.rawValue * rhs)
308+
}
309+
}

Tests/LLVMTests/JITSpec.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class JITSpec : XCTestCase {
104104
_ = try jit.addEagerlyCompiledIR(module) { (name) -> JIT.TargetAddress in
105105
gotForced = true
106106
guard name == testFuncName else {
107-
return 0
107+
return JIT.TargetAddress()
108108
}
109109
return getUnderlyingCDecl(calculateFibs)
110110
}
@@ -134,7 +134,7 @@ class JITSpec : XCTestCase {
134134
_ = try jit.addLazilyCompiledIR(module) { (name) -> JIT.TargetAddress in
135135
gotForced = true
136136
guard name == testFuncName else {
137-
return 0
137+
return JIT.TargetAddress()
138138
}
139139
return getUnderlyingCDecl(calculateFibs)
140140
}
@@ -169,12 +169,12 @@ class JITSpec : XCTestCase {
169169
let testFuncName = jit.mangle(symbol: "calculateSwiftFibs")
170170
_ = try jit.addObjectFile(objBuffer) { (name) -> JIT.TargetAddress in
171171
guard name == testFuncName else {
172-
return 0
172+
return JIT.TargetAddress()
173173
}
174174
return getUnderlyingCDecl(calculateFibs)
175175
}
176176
let mainAddr = try jit.address(of: "main")
177-
XCTAssert(mainAddr != 0)
177+
XCTAssert(mainAddr != JIT.TargetAddress())
178178
} catch _ {
179179
XCTFail()
180180
}
@@ -190,7 +190,7 @@ class JITSpec : XCTestCase {
190190
let sm = self.buildTestModule()
191191
_ = try! jit.addEagerlyCompiledIR(sm) { (name) -> JIT.TargetAddress in
192192
guard name == testFuncName else {
193-
return 0
193+
return JIT.TargetAddress()
194194
}
195195
return getUnderlyingCDecl(calculateFibs)
196196
}
@@ -223,7 +223,7 @@ class JITSpec : XCTestCase {
223223
let sm = self.buildTestModule()
224224
_ = try! jit.addEagerlyCompiledIR(sm) { (name) -> JIT.TargetAddress in
225225
guard name == testFuncName else {
226-
return 0
226+
return JIT.TargetAddress()
227227
}
228228
return getUnderlyingCDecl(calculateFibs)
229229
}

0 commit comments

Comments
 (0)