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
This commit is contained in:
Chris Lattner 2009-04-26 22:21:57 +00:00
parent 7919b966a8
commit f9a3ec86c1
4 changed files with 79 additions and 14 deletions

View File

@ -563,6 +563,8 @@ blocks. The currently specified records are:
<pre> <pre>
[SETBID (#1), blockid] [SETBID (#1), blockid]
[DEFINE_ABBREV, ...] [DEFINE_ABBREV, ...]
[BLOCKNAME, ...name...]
[SETRECORDNAME, RecordID, ...name...]
</pre> </pre>
</div> </div>
@ -582,6 +584,15 @@ in <tt>BLOCKINFO</tt> blocks receive abbreviation IDs as described
in <tt><a href="#DEFINE_ABBREV">DEFINE_ABBREV</a></tt>. in <tt><a href="#DEFINE_ABBREV">DEFINE_ABBREV</a></tt>.
</p> </p>
<p>The <tt>BLOCKNAME</tt> 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.</p>
<p>The <tt>SETRECORDNAME</tt> 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.</p>
<p> <p>
Note that although the data in <tt>BLOCKINFO</tt> blocks is described as Note that although the data in <tt>BLOCKINFO</tt> blocks is described as
"metadata," the abbreviations they contain are essential for parsing records "metadata," the abbreviations they contain are essential for parsing records

View File

@ -66,10 +66,12 @@ namespace bitc {
/// BlockInfoCodes - The blockinfo block contains metadata about user-defined /// BlockInfoCodes - The blockinfo block contains metadata about user-defined
/// blocks. /// blocks.
enum BlockInfoCodes { enum BlockInfoCodes {
BLOCKINFO_CODE_SETBID = 1 // SETBID: [blockid#]
// DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
// block, instead of the BlockInfo block. // 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 } // End bitc namespace

View File

@ -30,6 +30,9 @@ public:
struct BlockInfo { struct BlockInfo {
unsigned BlockID; unsigned BlockID;
std::vector<BitCodeAbbrev*> Abbrevs; std::vector<BitCodeAbbrev*> Abbrevs;
std::string Name;
std::vector<std::pair<unsigned, std::string> > RecordNames;
}; };
private: private:
/// FirstChar/LastChar - This remembers the first and last bytes of the /// 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, /// getBlockInfo - If there is block info for the specified ID, return it,
/// otherwise return null. /// otherwise return null.
BlockInfo *getBlockInfo(unsigned BlockID) { const BlockInfo *getBlockInfo(unsigned BlockID) const {
// Common case, the most recent entry matches BlockID. // Common case, the most recent entry matches BlockID.
if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID) if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
return &BlockInfoRecords.back(); return &BlockInfoRecords.back();
@ -91,8 +94,8 @@ public:
} }
BlockInfo &getOrCreateBlockInfo(unsigned BlockID) { BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
if (BlockInfo *BI = getBlockInfo(BlockID)) if (const BlockInfo *BI = getBlockInfo(BlockID))
return *BI; return *const_cast<BlockInfo*>(BI);
// Otherwise, add a new record. // Otherwise, add a new record.
BlockInfoRecords.push_back(BlockInfo()); BlockInfoRecords.push_back(BlockInfo());
@ -216,6 +219,13 @@ public:
return (NextChar-BitStream->getFirstChar())*CHAR_BIT - BitsInCurWord; 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. /// JumpToBit - Reset the stream to the specified bit number.
void JumpToBit(uint64_t BitNo) { void JumpToBit(uint64_t BitNo) {
@ -363,7 +373,8 @@ public:
BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
// Add the abbrevs specific to this block to the CurAbbrevs list. // 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<unsigned>(Info->Abbrevs.size()); for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
i != e; ++i) { i != e; ++i) {
CurAbbrevs.push_back(Info->Abbrevs[i]); CurAbbrevs.push_back(Info->Abbrevs[i]);
@ -585,6 +596,23 @@ public:
if (Record.size() < 1) return true; if (Record.size() < 1) return true;
CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
break; 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;
}
} }
} }
} }

View File

@ -72,7 +72,8 @@ static enum {
/// GetBlockName - Return a symbolic block name if known, otherwise return /// GetBlockName - Return a symbolic block name if known, otherwise return
/// null. /// null.
static const char *GetBlockName(unsigned BlockID) { static const char *GetBlockName(unsigned BlockID,
const BitstreamReader &StreamFile) {
// Standard blocks for all bitcode files. // Standard blocks for all bitcode files.
if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
if (BlockID == bitc::BLOCKINFO_BLOCK_ID) if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
@ -80,6 +81,14 @@ static const char *GetBlockName(unsigned BlockID) {
return 0; 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; if (CurStreamType != LLVMIRBitstream) return 0;
switch (BlockID) { switch (BlockID) {
@ -96,18 +105,30 @@ static const char *GetBlockName(unsigned BlockID) {
/// GetCodeName - Return a symbolic code name if known, otherwise return /// GetCodeName - Return a symbolic code name if known, otherwise return
/// null. /// 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. // Standard blocks for all bitcode files.
if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
switch (CodeID) { switch (CodeID) {
default: return 0; 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; 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; if (CurStreamType != LLVMIRBitstream) return 0;
switch (BlockID) { switch (BlockID) {
@ -289,7 +310,7 @@ static bool ParseBlock(BitstreamCursor &Stream, unsigned IndentLevel) {
const char *BlockName = 0; const char *BlockName = 0;
if (Dump) { if (Dump) {
std::cerr << Indent << "<"; std::cerr << Indent << "<";
if ((BlockName = GetBlockName(BlockID))) if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader())))
std::cerr << BlockName; std::cerr << BlockName;
else else
std::cerr << "UnknownBlock" << BlockID; std::cerr << "UnknownBlock" << BlockID;
@ -358,11 +379,13 @@ static bool ParseBlock(BitstreamCursor &Stream, unsigned IndentLevel) {
if (Dump) { if (Dump) {
std::cerr << Indent << " <"; std::cerr << Indent << " <";
if (const char *CodeName = GetCodeName(Code, BlockID)) if (const char *CodeName =
GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
std::cerr << CodeName; std::cerr << CodeName;
else else
std::cerr << "UnknownCode" << Code; std::cerr << "UnknownCode" << Code;
if (NonSymbolic && GetCodeName(Code, BlockID)) if (NonSymbolic &&
GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
std::cerr << " codeid=" << Code; std::cerr << " codeid=" << Code;
if (AbbrevID != bitc::UNABBREV_RECORD) if (AbbrevID != bitc::UNABBREV_RECORD)
std::cerr << " abbrevid=" << AbbrevID; std::cerr << " abbrevid=" << AbbrevID;
@ -474,7 +497,7 @@ static int AnalyzeBitcode() {
for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(), for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
E = BlockIDStats.end(); I != E; ++I) { E = BlockIDStats.end(); I != E; ++I) {
std::cerr << " Block ID #" << I->first; 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 << " (" << BlockName << ")";
std::cerr << ":\n"; std::cerr << ":\n";
@ -517,7 +540,8 @@ static int AnalyzeBitcode() {
std::cerr << "\tCode Histogram:\n"; std::cerr << "\tCode Histogram:\n";
for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) { for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
std::cerr << "\t\t" << FreqPairs[i].first << "\t"; 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"; std::cerr << CodeName << "\n";
else else
std::cerr << "UnknownCode" << FreqPairs[i].second << "\n"; std::cerr << "UnknownCode" << FreqPairs[i].second << "\n";