Skip to content

Commit 02366d0

Browse files
committed
Add support for reading binary swiftdeps files
1 parent 697e4ad commit 02366d0

File tree

8 files changed

+505
-71
lines changed

8 files changed

+505
-71
lines changed

Sources/SwiftDriver/Incremental Compilation/SourceFileDependencyGraph.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
@_implementationOnly import Yams
12+
import Foundation
1313

14-
public struct SourceFileDependencyGraph: Codable {
14+
public struct SourceFileDependencyGraph {
1515
public static let sourceFileProvidesInterfaceSequenceNumber: Int = 0
1616
public static let sourceFileProvidesImplementationSequenceNumber: Int = 1
1717

18+
public var majorVersion: UInt64
19+
public var minorVersion: UInt64
20+
public var compilerVersionString: String
1821
private var allNodes: [Node]
1922

2023
public var sourceFileNodePair: (interface: Node, implementation: Node) {
@@ -47,20 +50,14 @@ public struct SourceFileDependencyGraph: Codable {
4750
}
4851
return true
4952
}
50-
51-
public init(contents: String) throws {
52-
let decoder = YAMLDecoder()
53-
self = try decoder.decode(Self.self, from: contents)
54-
assert(verify())
55-
}
5653
}
5754

58-
public enum DeclAspect: String, Codable {
55+
public enum DeclAspect: UInt64 {
5956
case interface, implementation
6057
}
6158

62-
public struct DependencyKey: Codable {
63-
public enum Kind: String, Codable {
59+
public struct DependencyKey {
60+
public enum Kind: UInt64 {
6461
case topLevel
6562
case nominal
6663
case potentialMember
@@ -88,9 +85,8 @@ public struct DependencyKey: Codable {
8885
}
8986
}
9087

91-
9288
extension SourceFileDependencyGraph {
93-
public struct Node: Codable {
89+
public struct Node {
9490
public var key: DependencyKey
9591
public var fingerprint: String?
9692
public var sequenceNumber: Int

Sources/SwiftDriver/Utilities/Bitstream.swift

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ private extension Bits.Cursor {
7070
enum BitcodeError: Swift.Error {
7171
case vbrOverflow
7272
}
73-
73+
7474
mutating func readVBR(_ width: Int) throws -> UInt64 {
7575
precondition(width > 1)
7676
let testBit = UInt64(1 << (width &- 1))
7777
let mask = testBit &- 1
78-
78+
7979
var result: UInt64 = 0
8080
var offset: UInt64 = 0
8181
var next: UInt64
@@ -85,18 +85,11 @@ private extension Bits.Cursor {
8585
offset += UInt64(width &- 1)
8686
if offset > 64 { throw BitcodeError.vbrOverflow }
8787
} while next & testBit != 0
88-
88+
8989
return result
9090
}
9191
}
9292

93-
protocol BitstreamVisitor {
94-
func visit(record: BitcodeElement.Record) throws
95-
96-
func visitSubblock(id: UInt64) throws -> Bool
97-
func exitSubblock() throws
98-
}
99-
10093
private struct BitstreamReader {
10194
struct Abbrev {
10295
enum Operand {
@@ -106,18 +99,18 @@ private struct BitstreamReader {
10699
indirect case array(Operand)
107100
case char6
108101
case blob
109-
102+
110103
var isPayload: Bool {
111104
switch self {
112105
case .array, .blob: return true
113106
case .literal, .fixed, .vbr, .char6: return false
114107
}
115108
}
116109
}
117-
110+
118111
var operands: [Operand] = []
119112
}
120-
113+
121114
enum Error: Swift.Error {
122115
case invalidAbbrev
123116
case nestedBlockInBlockInfo
@@ -127,21 +120,21 @@ private struct BitstreamReader {
127120
case noSuchAbbrev(blockID: UInt64, abbrevID: Int)
128121
case missingEndBlock(blockID: UInt64)
129122
}
130-
123+
131124
var cursor: Bits.Cursor
132125
var blockInfo: [UInt64: BlockInfo] = [:]
133126
var globalAbbrevs: [UInt64: [Abbrev]] = [:]
134-
127+
135128
init(buffer: Data) {
136129
cursor = Bits.Cursor(buffer: buffer)
137130
}
138-
131+
139132
mutating func readAbbrevOp() throws -> Abbrev.Operand {
140133
let isLiteralFlag = try cursor.read(1)
141134
if isLiteralFlag == 1 {
142135
return .literal(try cursor.readVBR(8))
143136
}
144-
137+
145138
switch try cursor.read(3) {
146139
case 0:
147140
throw Error.invalidAbbrev
@@ -161,25 +154,25 @@ private struct BitstreamReader {
161154
fatalError()
162155
}
163156
}
164-
157+
165158
mutating func readAbbrev(numOps: Int) throws -> Abbrev {
166159
guard numOps > 0 else { throw Error.invalidAbbrev }
167-
160+
168161
var operands: [Abbrev.Operand] = []
169162
for i in 0..<numOps {
170163
operands.append(try readAbbrevOp())
171-
164+
172165
if case .array = operands.last! {
173166
guard i == numOps - 2 else { throw Error.invalidAbbrev }
174167
break
175168
} else if case .blob = operands.last! {
176169
guard i == numOps - 1 else { throw Error.invalidAbbrev }
177170
}
178171
}
179-
172+
180173
return Abbrev(operands: operands)
181174
}
182-
175+
183176
mutating func readSingleAbbreviatedRecordOperand(_ operand: Abbrev.Operand) throws -> UInt64 {
184177
switch operand {
185178
case .char6:
@@ -208,18 +201,18 @@ private struct BitstreamReader {
208201
fatalError()
209202
}
210203
}
211-
204+
212205
mutating func readAbbreviatedRecord(_ abbrev: Abbrev) throws -> BitcodeElement.Record {
213206
let code = try readSingleAbbreviatedRecordOperand(abbrev.operands.first!)
214-
207+
215208
let lastOperand = abbrev.operands.last!
216209
let lastRegularOperandIndex: Int = abbrev.operands.endIndex - (lastOperand.isPayload ? 1 : 0)
217-
210+
218211
var fields = [UInt64]()
219212
for op in abbrev.operands[1..<lastRegularOperandIndex] {
220213
fields.append(try readSingleAbbreviatedRecordOperand(op))
221214
}
222-
215+
223216
let payload: BitcodeElement.Record.Payload
224217
if !lastOperand.isPayload {
225218
payload = .none
@@ -245,10 +238,10 @@ private struct BitstreamReader {
245238
fatalError()
246239
}
247240
}
248-
241+
249242
return .init(id: code, fields: fields, payload: payload)
250243
}
251-
244+
252245
mutating func readBlockInfoBlock(abbrevWidth: Int) throws {
253246
var currentBlockID: UInt64?
254247
while true {
@@ -257,26 +250,26 @@ private struct BitstreamReader {
257250
try cursor.advance(toBitAlignment: 32)
258251
// FIXME: check expected length
259252
return
260-
253+
261254
case 1: // ENTER_BLOCK
262255
throw Error.nestedBlockInBlockInfo
263-
256+
264257
case 2: // DEFINE_ABBREV
265258
guard let blockID = currentBlockID else {
266259
throw Error.missingSETBID
267260
}
268261
let numOps = Int(try cursor.readVBR(5))
269262
if globalAbbrevs[blockID] == nil { globalAbbrevs[blockID] = [] }
270263
globalAbbrevs[blockID]!.append(try readAbbrev(numOps: numOps))
271-
264+
272265
case 3: // UNABBREV_RECORD
273266
let code = try cursor.readVBR(6)
274267
let numOps = try cursor.readVBR(6)
275268
var operands = [UInt64]()
276269
for _ in 0..<numOps {
277270
operands.append(try cursor.readVBR(6))
278271
}
279-
272+
280273
switch code {
281274
case 1:
282275
guard operands.count == 1 else { throw Error.invalidBlockInfoRecord(recordID: code) }
@@ -299,13 +292,13 @@ private struct BitstreamReader {
299292
default:
300293
throw Error.invalidBlockInfoRecord(recordID: code)
301294
}
302-
295+
303296
case let abbrevID:
304297
throw Error.noSuchAbbrev(blockID: 0, abbrevID: Int(abbrevID))
305298
}
306299
}
307300
}
308-
301+
309302
mutating func readBlock(id: UInt64, abbrevWidth: Int, abbrevInfo: [Abbrev]) throws -> [BitcodeElement] {
310303
var abbrevInfo = abbrevInfo
311304
var elements = [BitcodeElement]()
@@ -334,11 +327,11 @@ private struct BitstreamReader {
334327
id: blockID, abbrevWidth: newAbbrevWidth, abbrevInfo: globalAbbrevs[blockID] ?? [])
335328
elements.append(.block(.init(id: blockID, elements: innerElements)))
336329
}
337-
330+
338331
case 2: // DEFINE_ABBREV
339332
let numOps = Int(try cursor.readVBR(5))
340333
abbrevInfo.append(try readAbbrev(numOps: numOps))
341-
334+
342335
case 3: // UNABBREV_RECORD
343336
let code = try cursor.readVBR(6)
344337
let numOps = try cursor.readVBR(6)
@@ -355,13 +348,13 @@ private struct BitstreamReader {
355348
elements.append(.record(try readAbbreviatedRecord(abbrevInfo[Int(abbrevID) - 4])))
356349
}
357350
}
358-
351+
359352
guard id == Self.fakeTopLevelBlockID else {
360353
throw Error.missingEndBlock(blockID: id)
361354
}
362355
return elements
363356
}
364-
357+
365358
static let fakeTopLevelBlockID: UInt64 = ~0
366359
}
367360

1.75 KB
Binary file not shown.

0 commit comments

Comments
 (0)