@@ -70,12 +70,12 @@ private extension Bits.Cursor {
70
70
enum BitcodeError : Swift . Error {
71
71
case vbrOverflow
72
72
}
73
-
73
+
74
74
mutating func readVBR( _ width: Int ) throws -> UInt64 {
75
75
precondition ( width > 1 )
76
76
let testBit = UInt64 ( 1 << ( width &- 1 ) )
77
77
let mask = testBit &- 1
78
-
78
+
79
79
var result : UInt64 = 0
80
80
var offset : UInt64 = 0
81
81
var next : UInt64
@@ -85,18 +85,11 @@ private extension Bits.Cursor {
85
85
offset += UInt64 ( width &- 1 )
86
86
if offset > 64 { throw BitcodeError . vbrOverflow }
87
87
} while next & testBit != 0
88
-
88
+
89
89
return result
90
90
}
91
91
}
92
92
93
- protocol BitstreamVisitor {
94
- func visit( record: BitcodeElement . Record ) throws
95
-
96
- func visitSubblock( id: UInt64 ) throws -> Bool
97
- func exitSubblock( ) throws
98
- }
99
-
100
93
private struct BitstreamReader {
101
94
struct Abbrev {
102
95
enum Operand {
@@ -106,18 +99,18 @@ private struct BitstreamReader {
106
99
indirect case array( Operand )
107
100
case char6
108
101
case blob
109
-
102
+
110
103
var isPayload : Bool {
111
104
switch self {
112
105
case . array, . blob: return true
113
106
case . literal, . fixed, . vbr, . char6: return false
114
107
}
115
108
}
116
109
}
117
-
110
+
118
111
var operands : [ Operand ] = [ ]
119
112
}
120
-
113
+
121
114
enum Error : Swift . Error {
122
115
case invalidAbbrev
123
116
case nestedBlockInBlockInfo
@@ -127,21 +120,21 @@ private struct BitstreamReader {
127
120
case noSuchAbbrev( blockID: UInt64 , abbrevID: Int )
128
121
case missingEndBlock( blockID: UInt64 )
129
122
}
130
-
123
+
131
124
var cursor : Bits . Cursor
132
125
var blockInfo : [ UInt64 : BlockInfo ] = [ : ]
133
126
var globalAbbrevs : [ UInt64 : [ Abbrev ] ] = [ : ]
134
-
127
+
135
128
init ( buffer: Data ) {
136
129
cursor = Bits . Cursor ( buffer: buffer)
137
130
}
138
-
131
+
139
132
mutating func readAbbrevOp( ) throws -> Abbrev . Operand {
140
133
let isLiteralFlag = try cursor. read ( 1 )
141
134
if isLiteralFlag == 1 {
142
135
return . literal( try cursor. readVBR ( 8 ) )
143
136
}
144
-
137
+
145
138
switch try cursor. read ( 3 ) {
146
139
case 0 :
147
140
throw Error . invalidAbbrev
@@ -161,25 +154,25 @@ private struct BitstreamReader {
161
154
fatalError ( )
162
155
}
163
156
}
164
-
157
+
165
158
mutating func readAbbrev( numOps: Int ) throws -> Abbrev {
166
159
guard numOps > 0 else { throw Error . invalidAbbrev }
167
-
160
+
168
161
var operands : [ Abbrev . Operand ] = [ ]
169
162
for i in 0 ..< numOps {
170
163
operands. append ( try readAbbrevOp ( ) )
171
-
164
+
172
165
if case . array = operands. last! {
173
166
guard i == numOps - 2 else { throw Error . invalidAbbrev }
174
167
break
175
168
} else if case . blob = operands. last! {
176
169
guard i == numOps - 1 else { throw Error . invalidAbbrev }
177
170
}
178
171
}
179
-
172
+
180
173
return Abbrev ( operands: operands)
181
174
}
182
-
175
+
183
176
mutating func readSingleAbbreviatedRecordOperand( _ operand: Abbrev . Operand ) throws -> UInt64 {
184
177
switch operand {
185
178
case . char6:
@@ -208,18 +201,18 @@ private struct BitstreamReader {
208
201
fatalError ( )
209
202
}
210
203
}
211
-
204
+
212
205
mutating func readAbbreviatedRecord( _ abbrev: Abbrev ) throws -> BitcodeElement . Record {
213
206
let code = try readSingleAbbreviatedRecordOperand ( abbrev. operands. first!)
214
-
207
+
215
208
let lastOperand = abbrev. operands. last!
216
209
let lastRegularOperandIndex : Int = abbrev. operands. endIndex - ( lastOperand. isPayload ? 1 : 0 )
217
-
210
+
218
211
var fields = [ UInt64] ( )
219
212
for op in abbrev. operands [ 1 ..< lastRegularOperandIndex] {
220
213
fields. append ( try readSingleAbbreviatedRecordOperand ( op) )
221
214
}
222
-
215
+
223
216
let payload : BitcodeElement . Record . Payload
224
217
if !lastOperand. isPayload {
225
218
payload = . none
@@ -245,10 +238,10 @@ private struct BitstreamReader {
245
238
fatalError ( )
246
239
}
247
240
}
248
-
241
+
249
242
return . init( id: code, fields: fields, payload: payload)
250
243
}
251
-
244
+
252
245
mutating func readBlockInfoBlock( abbrevWidth: Int ) throws {
253
246
var currentBlockID : UInt64 ?
254
247
while true {
@@ -257,26 +250,26 @@ private struct BitstreamReader {
257
250
try cursor. advance ( toBitAlignment: 32 )
258
251
// FIXME: check expected length
259
252
return
260
-
253
+
261
254
case 1 : // ENTER_BLOCK
262
255
throw Error . nestedBlockInBlockInfo
263
-
256
+
264
257
case 2 : // DEFINE_ABBREV
265
258
guard let blockID = currentBlockID else {
266
259
throw Error . missingSETBID
267
260
}
268
261
let numOps = Int ( try cursor. readVBR ( 5 ) )
269
262
if globalAbbrevs [ blockID] == nil { globalAbbrevs [ blockID] = [ ] }
270
263
globalAbbrevs [ blockID] !. append ( try readAbbrev ( numOps: numOps) )
271
-
264
+
272
265
case 3 : // UNABBREV_RECORD
273
266
let code = try cursor. readVBR ( 6 )
274
267
let numOps = try cursor. readVBR ( 6 )
275
268
var operands = [ UInt64] ( )
276
269
for _ in 0 ..< numOps {
277
270
operands. append ( try cursor. readVBR ( 6 ) )
278
271
}
279
-
272
+
280
273
switch code {
281
274
case 1 :
282
275
guard operands. count == 1 else { throw Error . invalidBlockInfoRecord ( recordID: code) }
@@ -299,13 +292,13 @@ private struct BitstreamReader {
299
292
default :
300
293
throw Error . invalidBlockInfoRecord ( recordID: code)
301
294
}
302
-
295
+
303
296
case let abbrevID:
304
297
throw Error . noSuchAbbrev ( blockID: 0 , abbrevID: Int ( abbrevID) )
305
298
}
306
299
}
307
300
}
308
-
301
+
309
302
mutating func readBlock( id: UInt64 , abbrevWidth: Int , abbrevInfo: [ Abbrev ] ) throws -> [ BitcodeElement ] {
310
303
var abbrevInfo = abbrevInfo
311
304
var elements = [ BitcodeElement] ( )
@@ -334,11 +327,11 @@ private struct BitstreamReader {
334
327
id: blockID, abbrevWidth: newAbbrevWidth, abbrevInfo: globalAbbrevs [ blockID] ?? [ ] )
335
328
elements. append ( . block( . init( id: blockID, elements: innerElements) ) )
336
329
}
337
-
330
+
338
331
case 2 : // DEFINE_ABBREV
339
332
let numOps = Int ( try cursor. readVBR ( 5 ) )
340
333
abbrevInfo. append ( try readAbbrev ( numOps: numOps) )
341
-
334
+
342
335
case 3 : // UNABBREV_RECORD
343
336
let code = try cursor. readVBR ( 6 )
344
337
let numOps = try cursor. readVBR ( 6 )
@@ -355,13 +348,13 @@ private struct BitstreamReader {
355
348
elements. append ( . record( try readAbbreviatedRecord ( abbrevInfo [ Int ( abbrevID) - 4 ] ) ) )
356
349
}
357
350
}
358
-
351
+
359
352
guard id == Self . fakeTopLevelBlockID else {
360
353
throw Error . missingEndBlock ( blockID: id)
361
354
}
362
355
return elements
363
356
}
364
-
357
+
365
358
static let fakeTopLevelBlockID : UInt64 = ~ 0
366
359
}
367
360
0 commit comments