From f9a3ec86c138177c7d9b3a9d119e6d2247d14bd8 Mon Sep 17 00:00:00 2001
From: Chris Lattner
Date: Sun, 26 Apr 2009 22:21:57 +0000
Subject: [PATCH] Add two new record types to the blockinfo block: BLOCKNAME
and SETRECORDNAME. This allows a bitcode file to be self describing with
pretty names for records and blocks in addition to numbers. This enhances
llvm-bcanalyzer to use this to print prettily.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70165 91177308-0d34-0410-b5e6-96231b3b80d8
---
docs/BitCodeFormat.html | 11 +++++++
include/llvm/Bitcode/BitCodes.h | 6 ++--
include/llvm/Bitcode/BitstreamReader.h | 36 +++++++++++++++++---
tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp | 40 ++++++++++++++++++-----
4 files changed, 79 insertions(+), 14 deletions(-)
diff --git a/docs/BitCodeFormat.html b/docs/BitCodeFormat.html
index 54b9361c379..8813b017f91 100644
--- a/docs/BitCodeFormat.html
+++ b/docs/BitCodeFormat.html
@@ -563,6 +563,8 @@ blocks. The currently specified records are:
[SETBID (#1), blockid]
[DEFINE_ABBREV, ...]
+[BLOCKNAME, ...name...]
+[SETRECORDNAME, RecordID, ...name...]
@@ -582,6 +584,15 @@ in BLOCKINFO blocks receive abbreviation IDs as described
in DEFINE_ABBREV.
+The BLOCKNAME can optionally occur in this block. The elements of
+the record are the bytes for the string name of the block. llvm-bcanalyzer uses
+this to dump out bitcode files symbolically.
+
+The SETRECORDNAME record can optionally occur in this block. The
+first entry is a record ID number and the rest of the elements of the record are
+the bytes for the string name of the record. llvm-bcanalyzer uses
+this to dump out bitcode files symbolically.
+
Note that although the data in BLOCKINFO blocks is described as
"metadata," the abbreviations they contain are essential for parsing records
diff --git a/include/llvm/Bitcode/BitCodes.h b/include/llvm/Bitcode/BitCodes.h
index faf3fc73e88..449dc35d7de 100644
--- a/include/llvm/Bitcode/BitCodes.h
+++ b/include/llvm/Bitcode/BitCodes.h
@@ -66,10 +66,12 @@ namespace bitc {
/// BlockInfoCodes - The blockinfo block contains metadata about user-defined
/// blocks.
enum BlockInfoCodes {
- BLOCKINFO_CODE_SETBID = 1 // SETBID: [blockid#]
// DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
// block, instead of the BlockInfo block.
- // BLOCKNAME: give string name to block, if desired.
+
+ BLOCKINFO_CODE_SETBID = 1, // SETBID: [blockid#]
+ BLOCKINFO_CODE_BLOCKNAME = 2, // BLOCKNAME: [name]
+ BLOCKINFO_CODE_SETRECORDNAME = 3 // BLOCKINFO_CODE_SETRECORDNAME: [id, name]
};
} // End bitc namespace
diff --git a/include/llvm/Bitcode/BitstreamReader.h b/include/llvm/Bitcode/BitstreamReader.h
index d2489d19ac4..be3d8c55b7a 100644
--- a/include/llvm/Bitcode/BitstreamReader.h
+++ b/include/llvm/Bitcode/BitstreamReader.h
@@ -30,6 +30,9 @@ public:
struct BlockInfo {
unsigned BlockID;
std::vector Abbrevs;
+ std::string Name;
+
+ std::vector > RecordNames;
};
private:
/// FirstChar/LastChar - This remembers the first and last bytes of the
@@ -78,7 +81,7 @@ public:
/// getBlockInfo - If there is block info for the specified ID, return it,
/// otherwise return null.
- BlockInfo *getBlockInfo(unsigned BlockID) {
+ const BlockInfo *getBlockInfo(unsigned BlockID) const {
// Common case, the most recent entry matches BlockID.
if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
return &BlockInfoRecords.back();
@@ -91,8 +94,8 @@ public:
}
BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
- if (BlockInfo *BI = getBlockInfo(BlockID))
- return *BI;
+ if (const BlockInfo *BI = getBlockInfo(BlockID))
+ return *const_cast(BI);
// Otherwise, add a new record.
BlockInfoRecords.push_back(BlockInfo());
@@ -216,6 +219,13 @@ public:
return (NextChar-BitStream->getFirstChar())*CHAR_BIT - BitsInCurWord;
}
+ BitstreamReader *getBitStreamReader() {
+ return BitStream;
+ }
+ const BitstreamReader *getBitStreamReader() const {
+ return BitStream;
+ }
+
/// JumpToBit - Reset the stream to the specified bit number.
void JumpToBit(uint64_t BitNo) {
@@ -363,7 +373,8 @@ public:
BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
// Add the abbrevs specific to this block to the CurAbbrevs list.
- if (BitstreamReader::BlockInfo *Info = BitStream->getBlockInfo(BlockID)) {
+ if (const BitstreamReader::BlockInfo *Info =
+ BitStream->getBlockInfo(BlockID)) {
for (unsigned i = 0, e = static_cast(Info->Abbrevs.size());
i != e; ++i) {
CurAbbrevs.push_back(Info->Abbrevs[i]);
@@ -585,6 +596,23 @@ public:
if (Record.size() < 1) return true;
CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
break;
+ case bitc::BLOCKINFO_CODE_BLOCKNAME: {
+ if (!CurBlockInfo) return true;
+ std::string Name;
+ for (unsigned i = 0, e = Record.size(); i != e; ++i)
+ Name += (char)Record[i];
+ CurBlockInfo->Name = Name;
+ break;
+ }
+ case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
+ if (!CurBlockInfo) return true;
+ std::string Name;
+ for (unsigned i = 1, e = Record.size(); i != e; ++i)
+ Name += (char)Record[i];
+ CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
+ Name));
+ break;
+ }
}
}
}
diff --git a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
index 182404c4d15..e220ffa0959 100644
--- a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
+++ b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
@@ -72,7 +72,8 @@ static enum {
/// GetBlockName - Return a symbolic block name if known, otherwise return
/// null.
-static const char *GetBlockName(unsigned BlockID) {
+static const char *GetBlockName(unsigned BlockID,
+ const BitstreamReader &StreamFile) {
// Standard blocks for all bitcode files.
if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
@@ -80,6 +81,14 @@ static const char *GetBlockName(unsigned BlockID) {
return 0;
}
+ // Check to see if we have a blockinfo record for this block, with a name.
+ if (const BitstreamReader::BlockInfo *Info =
+ StreamFile.getBlockInfo(BlockID)) {
+ if (!Info->Name.empty())
+ return Info->Name.c_str();
+ }
+
+
if (CurStreamType != LLVMIRBitstream) return 0;
switch (BlockID) {
@@ -96,18 +105,30 @@ static const char *GetBlockName(unsigned BlockID) {
/// GetCodeName - Return a symbolic code name if known, otherwise return
/// null.
-static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
+static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
+ const BitstreamReader &StreamFile) {
// Standard blocks for all bitcode files.
if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
switch (CodeID) {
default: return 0;
- case bitc::MODULE_CODE_VERSION: return "VERSION";
+ case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
+ case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
+ case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
}
}
return 0;
}
+ // Check to see if we have a blockinfo record for this record, with a name.
+ if (const BitstreamReader::BlockInfo *Info =
+ StreamFile.getBlockInfo(BlockID)) {
+ for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
+ if (Info->RecordNames[i].first == CodeID)
+ return Info->RecordNames[i].second.c_str();
+ }
+
+
if (CurStreamType != LLVMIRBitstream) return 0;
switch (BlockID) {
@@ -289,7 +310,7 @@ static bool ParseBlock(BitstreamCursor &Stream, unsigned IndentLevel) {
const char *BlockName = 0;
if (Dump) {
std::cerr << Indent << "<";
- if ((BlockName = GetBlockName(BlockID)))
+ if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader())))
std::cerr << BlockName;
else
std::cerr << "UnknownBlock" << BlockID;
@@ -358,11 +379,13 @@ static bool ParseBlock(BitstreamCursor &Stream, unsigned IndentLevel) {
if (Dump) {
std::cerr << Indent << " <";
- if (const char *CodeName = GetCodeName(Code, BlockID))
+ if (const char *CodeName =
+ GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
std::cerr << CodeName;
else
std::cerr << "UnknownCode" << Code;
- if (NonSymbolic && GetCodeName(Code, BlockID))
+ if (NonSymbolic &&
+ GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
std::cerr << " codeid=" << Code;
if (AbbrevID != bitc::UNABBREV_RECORD)
std::cerr << " abbrevid=" << AbbrevID;
@@ -474,7 +497,7 @@ static int AnalyzeBitcode() {
for (std::map::iterator I = BlockIDStats.begin(),
E = BlockIDStats.end(); I != E; ++I) {
std::cerr << " Block ID #" << I->first;
- if (const char *BlockName = GetBlockName(I->first))
+ if (const char *BlockName = GetBlockName(I->first, StreamFile))
std::cerr << " (" << BlockName << ")";
std::cerr << ":\n";
@@ -517,7 +540,8 @@ static int AnalyzeBitcode() {
std::cerr << "\tCode Histogram:\n";
for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
std::cerr << "\t\t" << FreqPairs[i].first << "\t";
- if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first))
+ if (const char *CodeName =
+ GetCodeName(FreqPairs[i].second, I->first, StreamFile))
std::cerr << CodeName << "\n";
else
std::cerr << "UnknownCode" << FreqPairs[i].second << "\n";