Read global symtab

llvm-svn: 36378
This commit is contained in:
Chris Lattner 2007-04-23 21:26:05 +00:00
parent ed7414171f
commit 5e06e0faf4
2 changed files with 59 additions and 3 deletions

View File

@ -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<typename StrTy>
static bool ConvertToString(SmallVector<uint64_t, 64> &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<uint64_t, 64> 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;
}

View File

@ -21,11 +21,13 @@
namespace llvm {
class BitstreamReader;
class Value;
class BitcodeReader : public ModuleProvider {
const char *ErrorString;
std::vector<PATypeHolder> TypeList;
std::vector<Value*> 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