From 5e06e0faf45b393403dc97209b6ee96ee9040ab5 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 23 Apr 2007 21:26:05 +0000 Subject: [PATCH] Read global symtab llvm-svn: 36378 --- lib/Bitcode/Reader/BitcodeReader.cpp | 59 ++++++++++++++++++++++++++-- lib/Bitcode/Reader/BitcodeReader.h | 3 ++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index ba4ba8db80e..ca74e3f6b7f 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -15,12 +15,14 @@ #include "llvm/Bitcode/BitstreamReader.h" #include "llvm/DerivedTypes.h" #include "llvm/Module.h" +#include "llvm/ADT/SmallString.h" using namespace llvm; /// ConvertToString - Convert a string from a record into an std::string, return /// true on failure. +template static bool ConvertToString(SmallVector &Record, unsigned Idx, - std::string &Result) { + StrTy &Result) { if (Record.size() < Idx+1 || Record.size() < Record[Idx]+Idx+1) return true; @@ -255,6 +257,52 @@ bool BitcodeReader::ParseTypeSymbolTable(BitstreamReader &Stream) { } } +bool BitcodeReader::ParseValueSymbolTable(BitstreamReader &Stream) { + if (Stream.EnterSubBlock()) + return Error("Malformed block record"); + + SmallVector Record; + + // Read all the records for this value table. + SmallString<128> ValueName; + while (1) { + unsigned Code = Stream.ReadCode(); + if (Code == bitc::END_BLOCK) + return Stream.ReadBlockEnd(); + + if (Code == bitc::ENTER_SUBBLOCK) { + // No known subblocks, always skip them. + Stream.ReadSubBlockID(); + if (Stream.SkipBlock()) + return Error("Malformed block record"); + continue; + } + + if (Code == bitc::DEFINE_ABBREV) { + Stream.ReadAbbrevRecord(); + continue; + } + + // Read a record. + Record.clear(); + switch (Stream.ReadRecord(Code, Record)) { + default: // Default behavior: unknown type. + break; + case bitc::VST_ENTRY_CODE: // VST_ENTRY: [valueid, namelen, namechar x N] + if (ConvertToString(Record, 1, ValueName)) + return Error("Invalid TST_ENTRY record"); + unsigned ValueID = Record[0]; + if (ValueID >= ValueList.size()) + return Error("Invalid Value ID in VST_ENTRY record"); + Value *V = ValueList[ValueID]; + + V->setName(&ValueName[0], ValueName.size()); + ValueName.clear(); + break; + } + } +} + bool BitcodeReader::ParseModule(BitstreamReader &Stream, const std::string &ModuleID) { @@ -291,6 +339,10 @@ bool BitcodeReader::ParseModule(BitstreamReader &Stream, if (ParseTypeSymbolTable(Stream)) return true; break; + case bitc::VALUE_SYMTAB_BLOCK_ID: + if (ParseValueSymbolTable(Stream)) + return true; + break; } continue; } @@ -377,7 +429,8 @@ bool BitcodeReader::ParseModule(BitstreamReader &Stream, NewGV->setVisibility(Visibility); NewGV->setThreadLocal(isThreadLocal); - // TODO: Add to value table. + ValueList.push_back(NewGV); + // TODO: remember initializer/global pair for later substitution. break; } @@ -407,7 +460,7 @@ bool BitcodeReader::ParseModule(BitstreamReader &Stream, } Func->setVisibility(GetDecodedVisibility(Record[6])); - // TODO: Add to value table. + ValueList.push_back(Func); // TODO: remember initializer/global pair for later substitution. break; } diff --git a/lib/Bitcode/Reader/BitcodeReader.h b/lib/Bitcode/Reader/BitcodeReader.h index e6e24de8bde..45452ba0cb3 100644 --- a/lib/Bitcode/Reader/BitcodeReader.h +++ b/lib/Bitcode/Reader/BitcodeReader.h @@ -21,11 +21,13 @@ namespace llvm { class BitstreamReader; + class Value; class BitcodeReader : public ModuleProvider { const char *ErrorString; std::vector TypeList; + std::vector ValueList; public: virtual ~BitcodeReader() {} @@ -59,6 +61,7 @@ private: bool ParseModule(BitstreamReader &Stream, const std::string &ModuleID); bool ParseTypeTable(BitstreamReader &Stream); bool ParseTypeSymbolTable(BitstreamReader &Stream); + bool ParseValueSymbolTable(BitstreamReader &Stream); }; } // End llvm namespace