1 //==- Deserialize.cpp - Generic Object Serialization to Bitcode --*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Ted Kremenek and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the internal methods used for object serialization.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/Deserialize.h"
18 Deserializer::Deserializer(BitstreamReader& stream)
19 : Stream(stream), RecIdx(0), FreeList(NULL) {
22 Deserializer::~Deserializer() {
23 assert (RecIdx >= Record.size() &&
24 "Still scanning bitcode record when deserialization completed.");
27 for (MapTy::iterator I=BPatchMap.begin(), E=BPatchMap.end(); I!=E; ++I)
28 assert (I->first.hasFinalPtr() &&
29 "Some pointers were not backpatched.");
34 bool Deserializer::inRecord() {
35 if (Record.size() > 0) {
36 if (RecIdx >= Record.size()) {
46 void Deserializer::ReadRecord() {
47 // FIXME: Check if we haven't run off the edge of the stream.
48 // FIXME: Handle abbreviations.
50 assert (Record.size() == 0);
56 if (Stream.AtEndOfStream())
59 Code = Stream.ReadCode();
61 if (Code == bitc::ENTER_SUBBLOCK) {
62 // No known subblocks, always skip them.
63 unsigned id = Stream.ReadSubBlockID();
64 Stream.EnterSubBlock(id);
68 if (Code == bitc::END_BLOCK) {
69 bool x = Stream.ReadBlockEnd();
70 assert (!x && "Error at block end.");
77 assert (Record.size() == 0);
78 Stream.ReadRecord(Code,Record);
79 assert (Record.size() > 0 || Stream.AtEndOfStream());
82 bool Deserializer::AtEnd() {
88 return Stream.AtEndOfStream();
91 uint64_t Deserializer::ReadInt() {
92 // FIXME: Any error recovery/handling with incomplete or bad files?
96 return Record[RecIdx++];
99 char* Deserializer::ReadCStr(char* cstr, unsigned MaxLen, bool isNullTerm) {
101 MaxLen = 0; // Zero this just in case someone does something funny.
103 unsigned len = ReadInt();
105 assert (MaxLen == 0 || (len + (isNullTerm ? 1 : 0)) <= MaxLen);
108 cstr = new char[len + (isNullTerm ? 1 : 0)];
110 assert (cstr != NULL);
112 for (unsigned i = 0; i < len; ++i)
113 cstr[i] = (char) ReadInt();
121 void Deserializer::ReadCStr(std::vector<char>& buff, bool isNullTerm) {
122 unsigned len = ReadInt();
127 for (unsigned i = 0; i < len; ++i)
128 buff.push_back((char) ReadInt());
131 buff.push_back('\0');
134 void Deserializer::RegisterPtr(unsigned PtrId, const void* Ptr) {
135 MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId));
137 assert (!HasFinalPtr(E) && "Pointer already registered.");
142 void Deserializer::ReadUIntPtr(uintptr_t& PtrRef) {
143 unsigned PtrId = ReadInt();
150 MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId));
153 PtrRef = GetFinalPtr(E);
155 // Register backpatch. Check the freelist for a BPNode.
160 FreeList = FreeList->Next;
162 else // No available BPNode. Allocate one.
163 N = (BPNode*) Allocator.Allocate<BPNode>();
165 new (N) BPNode(GetBPNode(E),PtrRef);
170 uintptr_t Deserializer::ReadInternalRefPtr() {
171 unsigned PtrId = ReadInt();
173 assert (PtrId != 0 && "References cannot refer the NULL address.");
175 MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId));
177 assert (HasFinalPtr(E) &&
178 "Cannot backpatch references. Object must be already deserialized.");
180 return GetFinalPtr(E);
183 void Deserializer::BPEntry::SetPtr(BPNode*& FreeList, void* P) {
186 for (BPNode* N = Head; N != NULL; N=N->Next) {
188 N->PtrRef |= reinterpret_cast<uintptr_t>(P);
192 Last->Next = FreeList;
196 Ptr = const_cast<void*>(P);
200 #define INT_READ(TYPE)\
201 void SerializeTrait<TYPE>::Read(Deserializer& D, TYPE& X) {\
202 X = (TYPE) D.ReadInt(); }
205 INT_READ(unsigned char)
206 INT_READ(unsigned short)
207 INT_READ(unsigned int)
208 INT_READ(unsigned long)