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
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
Deserializer::Deserializer(BitstreamReader& stream)
|
|
|
|
: Stream(stream), RecIdx(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Deserializer::~Deserializer() {
|
|
|
|
assert (RecIdx >= Record.size() &&
|
|
|
|
"Still scanning bitcode record when deserialization completed.");
|
2007-10-25 00:10:21 +00:00
|
|
|
|
|
|
|
BackpatchPointers();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-10-23 21:29:33 +00:00
|
|
|
// FIXME: Check for the correct code.
|
2007-10-24 19:06:40 +00:00
|
|
|
unsigned Code = Stream.ReadCode();
|
|
|
|
|
|
|
|
assert (Record.size() == 0);
|
|
|
|
Stream.ReadRecord(Code,Record);
|
2007-10-23 21:29:33 +00:00
|
|
|
assert (Record.size() > 0);
|
|
|
|
}
|
|
|
|
|
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-25 00:10:21 +00:00
|
|
|
void Deserializer::RegisterPtr(unsigned PtrId,void* Ptr) {
|
|
|
|
BPatchEntry& E = BPatchMap[PtrId];
|
|
|
|
assert (E.Ptr == NULL && "Pointer already registered.");
|
|
|
|
E.Ptr = Ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Deserializer::ReadPtr(void*& PtrRef) {
|
|
|
|
unsigned PtrId = ReadInt();
|
|
|
|
|
|
|
|
BPatchEntry& E = BPatchMap[PtrId];
|
|
|
|
|
|
|
|
if (E.Ptr == NULL) {
|
|
|
|
// Register backpatch.
|
|
|
|
void* P = Allocator.Allocate<BPatchNode>();
|
|
|
|
E.Head = new (P) BPatchNode(E.Head,PtrRef);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PtrRef = E.Ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Deserializer::BackpatchPointers() {
|
|
|
|
for (MapTy::iterator I=BPatchMap.begin(),E=BPatchMap.end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
BPatchEntry& E = I->second;
|
|
|
|
assert (E.Ptr && "No pointer found for backpatch.");
|
|
|
|
|
|
|
|
for (BPatchNode* N = E.Head; N != NULL; N = N->Next)
|
|
|
|
N->PtrRef = E.Ptr;
|
|
|
|
|
|
|
|
E.Head = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Allocator.Reset();
|
|
|
|
}
|
2007-10-24 19:06:40 +00:00
|
|
|
|
|
|
|
#define INT_READ(TYPE)\
|
|
|
|
void SerializeTrait<TYPE>::Read(Deserializer& D, TYPE& X) {\
|
2007-10-25 00:10:21 +00:00
|
|
|
X = (TYPE) D.ReadInt(); }\
|
|
|
|
TYPE SerializeTrait<TYPE>::ReadVal(Deserializer& D) {\
|
|
|
|
return (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)
|