Skip to content

Commit 1ef480c

Browse files
committed
Move BlockInfoCode to Bitstream
1 parent 237baa8 commit 1ef480c

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

Sources/TSCUtility/Bitstream.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ extension Bitstream {
147147
}
148148
}
149149

150+
extension Bitstream {
151+
/// A `BlockInfoCode` enumerates the bits that occur in the metadata for
152+
/// a block or record. Of these bits, only `setBID` is required. If
153+
/// a name is given to a block or record with `blockName` or
154+
/// `setRecordName`, debugging tools like `llvm-bcanalyzer` can be used to
155+
/// introspect the structure of blocks and records in the bitstream file.
156+
public enum BlockInfoCode: UInt8 {
157+
/// Indicates which block ID is being described.
158+
case setBID = 1
159+
/// An optional element that records which bytes of the record are the
160+
/// name of the block.
161+
case blockName = 2
162+
/// An optional element that records the record ID number and the bytes
163+
/// for the name of the corresponding record.
164+
case setRecordName = 3
165+
}
166+
}
167+
150168
extension Bitstream {
151169
/// A `BlockID` is a fixed-width field that occurs at the start of all blocks.
152170
///

Sources/TSCUtility/BitstreamReader.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,16 @@ private struct BitstreamReader {
259259
}
260260

261261
switch code {
262-
case 1:
262+
case UInt64(Bitstream.BlockInfoCode.setBID.rawValue):
263263
guard operands.count == 1 else { throw Error.invalidBlockInfoRecord(recordID: code) }
264264
currentBlockID = operands.first
265-
case 2:
265+
case UInt64(Bitstream.BlockInfoCode.blockName.rawValue):
266266
guard let blockID = currentBlockID else {
267267
throw Error.missingSETBID
268268
}
269269
if blockInfo[blockID] == nil { blockInfo[blockID] = BlockInfo() }
270270
blockInfo[blockID]!.name = String(bytes: operands.map { UInt8($0) }, encoding: .utf8) ?? "<invalid>"
271-
case 3:
271+
case UInt64(Bitstream.BlockInfoCode.setRecordName.rawValue):
272272
guard let blockID = currentBlockID else {
273273
throw Error.missingSETBID
274274
}

Sources/TSCUtility/BitstreamWriter.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import Foundation
12-
1311
/// A `BitstreamWriter` is an object that is capable of emitting data in the
1412
/// [LLVM Bitstream](https://llvm.org/docs/BitCodeFormat.html#bitstream-format)
1513
/// format.
@@ -33,6 +31,7 @@ import Foundation
3331
///
3432
/// Next, identify the kinds of records needed in the format and assign them
3533
/// unique, stable identifiers. For example:
34+
///
3635
/// ```
3736
/// enum DiagnosticRecordID: UInt8 {
3837
/// case version = 1
@@ -69,7 +68,7 @@ import Foundation
6968
/// }
7069
///
7170
/// versionAbbrev = recordWriter.defineBlockInfoAbbreviation(.metadata, .init([
72-
/// .literalCode(RoundTripRecordID.version),
71+
/// .literalCode(DiagnosticRecordID.version),
7372
/// .fixed(bitWidth: 32)
7473
/// ]))
7574
///
@@ -83,7 +82,7 @@ import Foundation
8382
/// ```
8483
/// recordWriter.writeBlock(.metadata, newAbbrevWidth: 3) {
8584
/// recordWriter.writeRecord(versionAbbrev!) {
86-
/// $0.append(RoundTripRecordID.version)
85+
/// $0.append(DiagnosticRecordID.version)
8786
/// $0.append(25 as UInt32)
8887
/// }
8988
/// }
@@ -92,12 +91,6 @@ import Foundation
9291
/// The higher-level APIs will automatically ensure that `BitstreamWriter.data`
9392
/// is valid. Once serialization has completed, simply emit this data to a file.
9493
public final class BitstreamWriter {
95-
public enum BlockInfoCode: UInt8 {
96-
case setBID = 1
97-
case blockName = 2
98-
case setRecordName = 3
99-
}
100-
10194
/// The buffer of data being written to.
10295
private(set) public var data: [UInt8]
10396

@@ -657,7 +650,7 @@ extension BitstreamWriter {
657650

658651
private func `switch`(to blockID: Bitstream.BlockID) {
659652
if currentBlockID == blockID { return }
660-
writeRecord(BlockInfoCode.setBID) {
653+
writeRecord(Bitstream.BlockInfoCode.setBID) {
661654
$0.append(blockID)
662655
}
663656
currentBlockID = blockID

Tests/TSCUtilityTests/BitstreamTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ final class BitstreamTests: XCTestCase {
387387
func testSimpleRecordWrite() {
388388
let recordWriter = BitstreamWriter()
389389
recordWriter.withSubBlock(.metadata, abbreviationBitWidth: 2) {
390-
recordWriter.writeRecord(BitstreamWriter.BlockInfoCode.setRecordName) {
390+
recordWriter.writeRecord(Bitstream.BlockInfoCode.setRecordName) {
391391
$0.append(Bitstream.AbbreviationID.mockAbbreviation)
392392
}
393393
}
@@ -399,7 +399,7 @@ final class BitstreamTests: XCTestCase {
399399
UInt8(1), UInt8(0), UInt8(0), UInt8(0),
400400
// Still 32-bit aligned, now here's the record data
401401
UInt8(Bitstream.AbbreviationID.unabbreviatedRecord.rawValue)
402-
| UInt8(BitstreamWriter.BlockInfoCode.setRecordName.rawValue << 2),
402+
| UInt8(Bitstream.BlockInfoCode.setRecordName.rawValue << 2),
403403
UInt8(1), // record length of 1 - which is just the ID field
404404
UInt8(1), // record ID itself - which is 0b00001000 but we're
405405
// 14 bits in by the time we get here and writing 6 bits,
@@ -409,19 +409,19 @@ final class BitstreamTests: XCTestCase {
409409
}
410410

411411
func emitBlockID(_ id: Bitstream.BlockID, named name: String, to stream: BitstreamWriter) {
412-
stream.writeRecord(BitstreamWriter.BlockInfoCode.setBID) {
412+
stream.writeRecord(Bitstream.BlockInfoCode.setBID) {
413413
$0.append(id)
414414
}
415415

416-
stream.writeRecord(BitstreamWriter.BlockInfoCode.blockName) {
416+
stream.writeRecord(Bitstream.BlockInfoCode.blockName) {
417417
$0.append(name)
418418
}
419419
}
420420

421421
func emitRecordID<CodeType>(_ id: CodeType, named name: String, to stream: BitstreamWriter)
422422
where CodeType: RawRepresentable, CodeType.RawValue: UnsignedInteger & ExpressibleByIntegerLiteral
423423
{
424-
stream.writeRecord(BitstreamWriter.BlockInfoCode.setRecordName) {
424+
stream.writeRecord(Bitstream.BlockInfoCode.setRecordName) {
425425
$0.append(id)
426426
$0.append(name)
427427
}

0 commit comments

Comments
 (0)