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.
54 Code = Stream.ReadCode();
56 if (Code == bitc::ENTER_SUBBLOCK) {
57 // No known subblocks, always skip them.
58 unsigned id = Stream.ReadSubBlockID();
59 Stream.EnterSubBlock(id);
63 if (Code == bitc::END_BLOCK) {
64 bool x = Stream.ReadBlockEnd();
65 assert (!x && "Error at block end.");
72 assert (Record.size() == 0);
73 Stream.ReadRecord(Code,Record);
74 assert (Record.size() > 0);
77 uint64_t Deserializer::ReadInt() {
78 // FIXME: Any error recovery/handling with incomplete or bad files?
82 return Record[RecIdx++];
85 char* Deserializer::ReadCStr(char* cstr, unsigned MaxLen, bool isNullTerm) {
87 MaxLen = 0; // Zero this just in case someone does something funny.
89 unsigned len = ReadInt();
91 assert (MaxLen == 0 || (len + (isNullTerm ? 1 : 0)) <= MaxLen);
94 cstr = new char[len + (isNullTerm ? 1 : 0)];
96 assert (cstr != NULL);
98 for (unsigned i = 0; i < len; ++i)
99 cstr[i] = (char) ReadInt();
107 void Deserializer::ReadCStr(std::vector<char>& buff, bool isNullTerm) {
108 unsigned len = ReadInt();
113 for (unsigned i = 0; i < len; ++i)
114 buff.push_back((char) ReadInt());
117 buff.push_back('\0');
120 void Deserializer::RegisterPtr(unsigned PtrId, const void* Ptr) {
121 MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId));
123 assert (!HasFinalPtr(E) && "Pointer already registered.");
128 void Deserializer::ReadUIntPtr(uintptr_t& PtrRef) {
129 unsigned PtrId = ReadInt();
136 MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId));
139 PtrRef = GetFinalPtr(E);
141 // Register backpatch. Check the freelist for a BPNode.
146 FreeList = FreeList->Next;
148 else // No available BPNode. Allocate one.
149 N = (BPNode*) Allocator.Allocate<BPNode>();
151 new (N) BPNode(GetBPNode(E),PtrRef);
156 uintptr_t Deserializer::ReadInternalRefPtr() {
157 unsigned PtrId = ReadInt();
159 assert (PtrId != 0 && "References cannot refer the NULL address.");
161 MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId));
163 assert (HasFinalPtr(E) &&
164 "Cannot backpatch references. Object must be already deserialized.");
166 return GetFinalPtr(E);
169 void Deserializer::BPEntry::SetPtr(BPNode*& FreeList, void* P) {
172 for (BPNode* N = Head; N != NULL; N=N->Next) {
174 N->PtrRef |= reinterpret_cast<uintptr_t>(P);
178 Last->Next = FreeList;
182 Ptr = const_cast<void*>(P);
186 #define INT_READ(TYPE)\
187 void SerializeTrait<TYPE>::Read(Deserializer& D, TYPE& X) {\
188 X = (TYPE) D.ReadInt(); }
191 INT_READ(unsigned char)
192 INT_READ(unsigned short)
193 INT_READ(unsigned int)
194 INT_READ(unsigned long)