2007-10-23 21:29:33 +00:00
|
|
|
//==- Deserialize.cpp - Generic Object Serialization to Bitcode --*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Ted Kremenek and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the internal methods used for object serialization.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
#include "llvm/Bitcode/Deserialize.h"
|
2007-10-23 21:29:33 +00:00
|
|
|
|
2007-11-06 19:49:16 +00:00
|
|
|
#ifdef DEBUG_BACKPATCH
|
|
|
|
#include "llvm/Support/Streams.h"
|
|
|
|
#endif
|
|
|
|
|
2007-10-23 21:29:33 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
Deserializer::Deserializer(BitstreamReader& stream)
|
2007-10-28 21:17:59 +00:00
|
|
|
: Stream(stream), RecIdx(0), FreeList(NULL) {
|
2007-10-23 21:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Deserializer::~Deserializer() {
|
|
|
|
assert (RecIdx >= Record.size() &&
|
|
|
|
"Still scanning bitcode record when deserialization completed.");
|
2007-10-28 23:38:38 +00:00
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
for (MapTy::iterator I=BPatchMap.begin(), E=BPatchMap.end(); I!=E; ++I)
|
2007-11-01 00:57:37 +00:00
|
|
|
assert (I->first.hasFinalPtr() &&
|
2007-10-28 23:38:38 +00:00
|
|
|
"Some pointers were not backpatched.");
|
|
|
|
#endif
|
2007-10-25 00:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Deserializer::inRecord() {
|
|
|
|
if (Record.size() > 0) {
|
|
|
|
if (RecIdx >= Record.size()) {
|
|
|
|
RecIdx = 0;
|
|
|
|
Record.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
else return false;
|
2007-10-23 21:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Deserializer::ReadRecord() {
|
|
|
|
// FIXME: Check if we haven't run off the edge of the stream.
|
|
|
|
// FIXME: Handle abbreviations.
|
2007-10-24 19:06:40 +00:00
|
|
|
|
2007-11-05 21:36:35 +00:00
|
|
|
assert (Record.size() == 0);
|
|
|
|
|
2007-11-05 20:47:27 +00:00
|
|
|
unsigned Code;
|
2007-10-24 19:06:40 +00:00
|
|
|
|
2007-11-05 20:47:27 +00:00
|
|
|
while (true) {
|
|
|
|
|
2007-11-05 21:36:35 +00:00
|
|
|
if (Stream.AtEndOfStream())
|
|
|
|
return;
|
|
|
|
|
2007-11-05 20:47:27 +00:00
|
|
|
Code = Stream.ReadCode();
|
|
|
|
|
|
|
|
if (Code == bitc::ENTER_SUBBLOCK) {
|
|
|
|
// No known subblocks, always skip them.
|
|
|
|
unsigned id = Stream.ReadSubBlockID();
|
|
|
|
Stream.EnterSubBlock(id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Code == bitc::END_BLOCK) {
|
|
|
|
bool x = Stream.ReadBlockEnd();
|
|
|
|
assert (!x && "Error at block end.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-11-06 19:49:16 +00:00
|
|
|
if (Code == bitc::DEFINE_ABBREV) {
|
|
|
|
Stream.ReadAbbrevRecord();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-11-05 20:47:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
assert (Record.size() == 0);
|
|
|
|
Stream.ReadRecord(Code,Record);
|
2007-11-05 21:36:35 +00:00
|
|
|
assert (Record.size() > 0 || Stream.AtEndOfStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Deserializer::AtEnd() {
|
|
|
|
if (inRecord())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ReadRecord();
|
|
|
|
|
|
|
|
return Stream.AtEndOfStream();
|
2007-10-23 21:29:33 +00:00
|
|
|
}
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
uint64_t Deserializer::ReadInt() {
|
2007-10-23 21:29:33 +00:00
|
|
|
// FIXME: Any error recovery/handling with incomplete or bad files?
|
|
|
|
if (!inRecord())
|
|
|
|
ReadRecord();
|
|
|
|
|
|
|
|
return Record[RecIdx++];
|
|
|
|
}
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
char* Deserializer::ReadCStr(char* cstr, unsigned MaxLen, bool isNullTerm) {
|
2007-10-23 21:29:33 +00:00
|
|
|
if (cstr == NULL)
|
|
|
|
MaxLen = 0; // Zero this just in case someone does something funny.
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
unsigned len = ReadInt();
|
2007-10-23 21:29:33 +00:00
|
|
|
|
|
|
|
assert (MaxLen == 0 || (len + (isNullTerm ? 1 : 0)) <= MaxLen);
|
|
|
|
|
|
|
|
if (!cstr)
|
|
|
|
cstr = new char[len + (isNullTerm ? 1 : 0)];
|
|
|
|
|
|
|
|
assert (cstr != NULL);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < len; ++i)
|
2007-10-24 19:06:40 +00:00
|
|
|
cstr[i] = (char) ReadInt();
|
2007-10-23 21:29:33 +00:00
|
|
|
|
|
|
|
if (isNullTerm)
|
|
|
|
cstr[len+1] = '\0';
|
|
|
|
|
|
|
|
return cstr;
|
|
|
|
}
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
void Deserializer::ReadCStr(std::vector<char>& buff, bool isNullTerm) {
|
|
|
|
unsigned len = ReadInt();
|
2007-10-23 21:29:33 +00:00
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
buff.clear();
|
2007-10-23 21:29:33 +00:00
|
|
|
buff.reserve(len);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < len; ++i)
|
2007-10-24 19:06:40 +00:00
|
|
|
buff.push_back((char) ReadInt());
|
2007-10-23 21:29:33 +00:00
|
|
|
|
|
|
|
if (isNullTerm)
|
|
|
|
buff.push_back('\0');
|
|
|
|
}
|
2007-10-24 19:06:40 +00:00
|
|
|
|
2007-10-31 22:42:03 +00:00
|
|
|
void Deserializer::RegisterPtr(unsigned PtrId, const void* Ptr) {
|
2007-11-01 00:57:37 +00:00
|
|
|
MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId));
|
|
|
|
|
|
|
|
assert (!HasFinalPtr(E) && "Pointer already registered.");
|
|
|
|
|
2007-11-06 19:49:16 +00:00
|
|
|
#ifdef DEBUG_BACKPATCH
|
|
|
|
llvm::cerr << "RegisterPtr: " << PtrId << " => " << Ptr << "\n";
|
|
|
|
#endif
|
|
|
|
|
2007-11-01 00:57:37 +00:00
|
|
|
SetPtr(E,Ptr);
|
2007-10-25 00:10:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-28 21:17:59 +00:00
|
|
|
void Deserializer::ReadUIntPtr(uintptr_t& PtrRef) {
|
2007-10-25 00:10:21 +00:00
|
|
|
unsigned PtrId = ReadInt();
|
|
|
|
|
2007-10-25 18:42:52 +00:00
|
|
|
if (PtrId == 0) {
|
2007-10-29 18:43:39 +00:00
|
|
|
PtrRef = 0;
|
2007-10-25 18:42:52 +00:00
|
|
|
return;
|
2007-11-06 19:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_BACKPATCH
|
|
|
|
llvm::cerr << "ReadUintPtr: " << PtrId << "\n";
|
|
|
|
#endif
|
|
|
|
|
2007-11-01 00:57:37 +00:00
|
|
|
MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId));
|
2007-10-25 18:42:52 +00:00
|
|
|
|
2007-11-01 00:57:37 +00:00
|
|
|
if (HasFinalPtr(E))
|
|
|
|
PtrRef = GetFinalPtr(E);
|
2007-10-28 21:17:59 +00:00
|
|
|
else {
|
|
|
|
// Register backpatch. Check the freelist for a BPNode.
|
|
|
|
BPNode* N;
|
|
|
|
|
|
|
|
if (FreeList) {
|
|
|
|
N = FreeList;
|
|
|
|
FreeList = FreeList->Next;
|
|
|
|
}
|
|
|
|
else // No available BPNode. Allocate one.
|
|
|
|
N = (BPNode*) Allocator.Allocate<BPNode>();
|
|
|
|
|
2007-11-01 00:57:37 +00:00
|
|
|
new (N) BPNode(GetBPNode(E),PtrRef);
|
|
|
|
SetBPNode(E,N);
|
2007-10-25 00:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 19:58:32 +00:00
|
|
|
uintptr_t Deserializer::ReadInternalRefPtr() {
|
|
|
|
unsigned PtrId = ReadInt();
|
|
|
|
|
|
|
|
assert (PtrId != 0 && "References cannot refer the NULL address.");
|
|
|
|
|
2007-11-01 00:57:37 +00:00
|
|
|
MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId));
|
2007-10-31 19:58:32 +00:00
|
|
|
|
2007-11-05 20:47:27 +00:00
|
|
|
assert (HasFinalPtr(E) &&
|
2007-10-31 19:58:32 +00:00
|
|
|
"Cannot backpatch references. Object must be already deserialized.");
|
|
|
|
|
2007-11-01 00:57:37 +00:00
|
|
|
return GetFinalPtr(E);
|
2007-10-31 19:58:32 +00:00
|
|
|
}
|
2007-10-28 21:17:59 +00:00
|
|
|
|
2007-11-01 00:57:37 +00:00
|
|
|
void Deserializer::BPEntry::SetPtr(BPNode*& FreeList, void* P) {
|
2007-10-28 21:17:59 +00:00
|
|
|
BPNode* Last = NULL;
|
|
|
|
|
2007-11-01 00:57:37 +00:00
|
|
|
for (BPNode* N = Head; N != NULL; N=N->Next) {
|
2007-10-28 21:17:59 +00:00
|
|
|
Last = N;
|
|
|
|
N->PtrRef |= reinterpret_cast<uintptr_t>(P);
|
2007-10-25 00:10:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-28 21:17:59 +00:00
|
|
|
if (Last) {
|
|
|
|
Last->Next = FreeList;
|
2007-11-01 00:57:37 +00:00
|
|
|
FreeList = Head;
|
2007-10-28 21:17:59 +00:00
|
|
|
}
|
|
|
|
|
2007-11-01 00:57:37 +00:00
|
|
|
Ptr = const_cast<void*>(P);
|
2007-10-25 00:10:21 +00:00
|
|
|
}
|
2007-10-24 19:06:40 +00:00
|
|
|
|
2007-10-28 21:17:59 +00:00
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
#define INT_READ(TYPE)\
|
|
|
|
void SerializeTrait<TYPE>::Read(Deserializer& D, TYPE& X) {\
|
2007-11-01 22:23:34 +00:00
|
|
|
X = (TYPE) D.ReadInt(); }
|
2007-10-24 19:06:40 +00:00
|
|
|
|
|
|
|
INT_READ(bool)
|
|
|
|
INT_READ(unsigned char)
|
|
|
|
INT_READ(unsigned short)
|
|
|
|
INT_READ(unsigned int)
|
|
|
|
INT_READ(unsigned long)
|