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) {
22 Deserializer::~Deserializer() {
23 assert (RecIdx >= Record.size() &&
24 "Still scanning bitcode record when deserialization completed.");
30 bool Deserializer::inRecord() {
31 if (Record.size() > 0) {
32 if (RecIdx >= Record.size()) {
42 void Deserializer::ReadRecord() {
43 // FIXME: Check if we haven't run off the edge of the stream.
44 // FIXME: Handle abbreviations.
46 // FIXME: Check for the correct code.
47 unsigned Code = Stream.ReadCode();
49 assert (Record.size() == 0);
50 Stream.ReadRecord(Code,Record);
51 assert (Record.size() > 0);
54 uint64_t Deserializer::ReadInt() {
55 // FIXME: Any error recovery/handling with incomplete or bad files?
59 return Record[RecIdx++];
62 char* Deserializer::ReadCStr(char* cstr, unsigned MaxLen, bool isNullTerm) {
64 MaxLen = 0; // Zero this just in case someone does something funny.
66 unsigned len = ReadInt();
68 assert (MaxLen == 0 || (len + (isNullTerm ? 1 : 0)) <= MaxLen);
71 cstr = new char[len + (isNullTerm ? 1 : 0)];
73 assert (cstr != NULL);
75 for (unsigned i = 0; i < len; ++i)
76 cstr[i] = (char) ReadInt();
84 void Deserializer::ReadCStr(std::vector<char>& buff, bool isNullTerm) {
85 unsigned len = ReadInt();
90 for (unsigned i = 0; i < len; ++i)
91 buff.push_back((char) ReadInt());
97 void Deserializer::RegisterPtr(unsigned PtrId,void* Ptr) {
98 BPatchEntry& E = BPatchMap[PtrId];
99 assert (E.Ptr == NULL && "Pointer already registered.");
103 void Deserializer::ReadPtr(void*& PtrRef) {
104 unsigned PtrId = ReadInt();
106 BPatchEntry& E = BPatchMap[PtrId];
109 // Register backpatch.
110 void* P = Allocator.Allocate<BPatchNode>();
111 E.Head = new (P) BPatchNode(E.Head,PtrRef);
117 void Deserializer::BackpatchPointers() {
118 for (MapTy::iterator I=BPatchMap.begin(),E=BPatchMap.end(); I!=E; ++I) {
120 BPatchEntry& E = I->second;
121 assert (E.Ptr && "No pointer found for backpatch.");
123 for (BPatchNode* N = E.Head; N != NULL; N = N->Next)
132 #define INT_READ(TYPE)\
133 void SerializeTrait<TYPE>::Read(Deserializer& D, TYPE& X) {\
134 X = (TYPE) D.ReadInt(); }\
135 TYPE SerializeTrait<TYPE>::ReadVal(Deserializer& D) {\
136 return (TYPE) D.ReadInt(); }
139 INT_READ(unsigned char)
140 INT_READ(unsigned short)
141 INT_READ(unsigned int)
142 INT_READ(unsigned long)