Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame^] | 1 | //==- Deserialize.cpp - Generic Object Serialization to Bitcode --*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the internal methods used for object serialization. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Bitcode/Serialization.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | Deserializer::Deserializer(BitstreamReader& stream) |
| 19 | : Stream(stream), RecIdx(0) { |
| 20 | } |
| 21 | |
| 22 | Deserializer::~Deserializer() { |
| 23 | assert (RecIdx >= Record.size() && |
| 24 | "Still scanning bitcode record when deserialization completed."); |
| 25 | } |
| 26 | |
| 27 | void Deserializer::ReadRecord() { |
| 28 | // FIXME: Check if we haven't run off the edge of the stream. |
| 29 | // FIXME: Handle abbreviations. |
| 30 | unsigned Code = Stream.ReadCode(); |
| 31 | // FIXME: Check for the correct code. |
| 32 | assert (Record.size() == 0); |
| 33 | |
| 34 | Stream.ReadRecord(Code,Record); |
| 35 | |
| 36 | assert (Record.size() > 0); |
| 37 | } |
| 38 | |
| 39 | uint64_t Deserializer::ReadInt(unsigned Bits) { |
| 40 | // FIXME: Any error recovery/handling with incomplete or bad files? |
| 41 | if (!inRecord()) |
| 42 | ReadRecord(); |
| 43 | |
| 44 | // FIXME: check for loss of precision in read (compare to Bits) |
| 45 | return Record[RecIdx++]; |
| 46 | } |
| 47 | |
| 48 | char* Deserializer::ReadCString(char* cstr, unsigned MaxLen, bool isNullTerm) { |
| 49 | if (cstr == NULL) |
| 50 | MaxLen = 0; // Zero this just in case someone does something funny. |
| 51 | |
| 52 | unsigned len = ReadInt(32); |
| 53 | |
| 54 | // FIXME: perform dynamic checking of lengths? |
| 55 | assert (MaxLen == 0 || (len + (isNullTerm ? 1 : 0)) <= MaxLen); |
| 56 | |
| 57 | if (!cstr) |
| 58 | cstr = new char[len + (isNullTerm ? 1 : 0)]; |
| 59 | |
| 60 | assert (cstr != NULL); |
| 61 | |
| 62 | for (unsigned i = 0; i < len; ++i) |
| 63 | cstr[i] = ReadInt(8); |
| 64 | |
| 65 | if (isNullTerm) |
| 66 | cstr[len+1] = '\0'; |
| 67 | |
| 68 | return cstr; |
| 69 | } |
| 70 | |
| 71 | void Deserializer::ReadCString(std::vector<char>& buff, bool isNullTerm) { |
| 72 | buff.clear(); |
| 73 | |
| 74 | unsigned len = ReadInt(32); |
| 75 | |
| 76 | buff.reserve(len); |
| 77 | |
| 78 | for (unsigned i = 0; i < len; ++i) |
| 79 | buff.push_back(ReadInt(8)); |
| 80 | |
| 81 | if (isNullTerm) |
| 82 | buff.push_back('\0'); |
| 83 | } |