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 | |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 14 | #include "llvm/Bitcode/Deserialize.h" |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 15 | |
Ted Kremenek | 26e25b3 | 2007-11-06 19:49:16 +0000 | [diff] [blame] | 16 | #ifdef DEBUG_BACKPATCH |
| 17 | #include "llvm/Support/Streams.h" |
| 18 | #endif |
| 19 | |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | Deserializer::Deserializer(BitstreamReader& stream) |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 23 | : Stream(stream), RecIdx(0), FreeList(NULL), AbbrevNo(0), RecordCode(0) { |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 24 | |
| 25 | AdvanceStream(); |
| 26 | if (!AtEnd()) StreamStart = BlockStack.back(); |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | Deserializer::~Deserializer() { |
| 30 | assert (RecIdx >= Record.size() && |
| 31 | "Still scanning bitcode record when deserialization completed."); |
Ted Kremenek | adc9b9c | 2007-10-28 23:38:38 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 33 | #ifdef DEBUG_BACKPATCH |
Ted Kremenek | adc9b9c | 2007-10-28 23:38:38 +0000 | [diff] [blame] | 34 | for (MapTy::iterator I=BPatchMap.begin(), E=BPatchMap.end(); I!=E; ++I) |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 35 | assert (I->first.hasFinalPtr() && |
Ted Kremenek | adc9b9c | 2007-10-28 23:38:38 +0000 | [diff] [blame] | 36 | "Some pointers were not backpatched."); |
| 37 | #endif |
Ted Kremenek | fe2a012 | 2007-10-25 00:10:21 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | |
| 41 | bool Deserializer::inRecord() { |
| 42 | if (Record.size() > 0) { |
| 43 | if (RecIdx >= Record.size()) { |
| 44 | RecIdx = 0; |
| 45 | Record.clear(); |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 46 | AbbrevNo = 0; |
Ted Kremenek | fe2a012 | 2007-10-25 00:10:21 +0000 | [diff] [blame] | 47 | return false; |
| 48 | } |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 49 | else |
| 50 | return true; |
Ted Kremenek | fe2a012 | 2007-10-25 00:10:21 +0000 | [diff] [blame] | 51 | } |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 52 | |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | bool Deserializer::AdvanceStream() { |
| 57 | assert (!inRecord() && |
| 58 | "Cannot advance stream. Still processing a record."); |
| 59 | |
| 60 | if (AbbrevNo == bitc::ENTER_SUBBLOCK || |
| 61 | AbbrevNo >= bitc::UNABBREV_RECORD) |
| 62 | return true; |
| 63 | |
| 64 | while (!Stream.AtEndOfStream()) { |
| 65 | |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 66 | uint64_t Pos = Stream.GetCurrentBitNo(); |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 67 | AbbrevNo = Stream.ReadCode(); |
| 68 | |
| 69 | switch (AbbrevNo) { |
| 70 | case bitc::ENTER_SUBBLOCK: { |
| 71 | unsigned id = Stream.ReadSubBlockID(); |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 72 | |
| 73 | // Determine the extent of the block. This is useful for jumping around |
| 74 | // the stream. This is hack: we read the header of the block, save |
| 75 | // the length, and then revert the bitstream to a location just before |
| 76 | // the block is entered. |
| 77 | uint64_t BPos = Stream.GetCurrentBitNo(); |
| 78 | Stream.ReadVBR(bitc::CodeLenWidth); // Skip the code size. |
| 79 | Stream.SkipToWord(); |
| 80 | unsigned NumWords = Stream.Read(bitc::BlockSizeWidth); |
| 81 | Stream.JumpToBit(BPos); |
| 82 | |
| 83 | BlockStack.push_back(Location(Pos,id,NumWords)); |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | |
| 87 | case bitc::END_BLOCK: { |
| 88 | bool x = Stream.ReadBlockEnd(); |
| 89 | assert (!x && "Error at block end."); |
| 90 | BlockStack.pop_back(); |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | case bitc::DEFINE_ABBREV: |
| 95 | Stream.ReadAbbrevRecord(); |
| 96 | continue; |
| 97 | |
| 98 | default: |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | return false; |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void Deserializer::ReadRecord() { |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 109 | |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 110 | while (AdvanceStream() && AbbrevNo == bitc::ENTER_SUBBLOCK) { |
| 111 | assert (!BlockStack.empty()); |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 112 | Stream.EnterSubBlock(BlockStack.back().BlockID); |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 113 | AbbrevNo = 0; |
Ted Kremenek | 14ac17e | 2007-11-05 20:47:27 +0000 | [diff] [blame] | 114 | } |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 115 | |
| 116 | if (Stream.AtEndOfStream()) |
| 117 | return; |
Ted Kremenek | 14ac17e | 2007-11-05 20:47:27 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 119 | assert (Record.size() == 0); |
| 120 | assert (AbbrevNo >= bitc::UNABBREV_RECORD); |
| 121 | RecordCode = Stream.ReadRecord(AbbrevNo,Record); |
| 122 | assert (Record.size() > 0); |
Ted Kremenek | 0a6d98e | 2007-11-05 21:36:35 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 125 | void Deserializer::SkipBlock() { |
| 126 | assert (!inRecord()); |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 127 | |
| 128 | if (AtEnd()) |
| 129 | return; |
| 130 | |
| 131 | AdvanceStream(); |
| 132 | |
| 133 | assert (AbbrevNo == bitc::ENTER_SUBBLOCK); |
| 134 | BlockStack.pop_back(); |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 135 | Stream.SkipBlock(); |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 136 | |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 137 | AbbrevNo = 0; |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 138 | AdvanceStream(); |
| 139 | } |
| 140 | |
| 141 | bool Deserializer::SkipToBlock(unsigned BlockID) { |
| 142 | assert (!inRecord()); |
| 143 | |
| 144 | AdvanceStream(); |
| 145 | assert (AbbrevNo == bitc::ENTER_SUBBLOCK); |
| 146 | |
| 147 | unsigned BlockLevel = BlockStack.size(); |
| 148 | |
| 149 | while (!AtEnd() && |
| 150 | BlockLevel == BlockStack.size() && |
| 151 | getCurrentBlockID() != BlockID) |
| 152 | SkipBlock(); |
| 153 | |
| 154 | return !(AtEnd() || BlockLevel != BlockStack.size()); |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | Deserializer::Location Deserializer::getCurrentBlockLocation() { |
Ted Kremenek | ec8cd06 | 2007-11-08 19:50:46 +0000 | [diff] [blame] | 158 | if (!inRecord()) |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 159 | AdvanceStream(); |
Ted Kremenek | ec8cd06 | 2007-11-08 19:50:46 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 161 | return BlockStack.back(); |
| 162 | } |
| 163 | |
| 164 | bool Deserializer::JumpTo(const Location& Loc) { |
| 165 | |
| 166 | assert (!inRecord()); |
| 167 | |
| 168 | // AdvanceStream(); |
| 169 | |
| 170 | // assert (AbbrevNo == bitc::ENTER_SUBBLOCK); |
| 171 | assert (!BlockStack.empty()); |
| 172 | |
| 173 | uint64_t LastBPos = StreamStart.BitNo; |
| 174 | |
| 175 | while (!BlockStack.empty()) { |
| 176 | |
| 177 | LastBPos = BlockStack.back().BitNo; |
| 178 | |
| 179 | // Determine of the current block contains the location of the block |
| 180 | // we are looking for. |
| 181 | if (BlockStack.back().contains(Loc)) { |
| 182 | // We found the enclosing block. We must first POP it off to |
| 183 | // destroy any accumulated context within the block scope. We then |
| 184 | // jump to the position of the block and enter it. |
| 185 | Stream.JumpToBit(LastBPos); |
| 186 | BlockStack.pop_back(); |
| 187 | Stream.PopBlockScope(); |
| 188 | |
| 189 | AbbrevNo = 0; |
| 190 | AdvanceStream(); |
| 191 | assert (AbbrevNo == bitc::ENTER_SUBBLOCK); |
| 192 | |
| 193 | Stream.EnterSubBlock(BlockStack.back().BlockID); |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | // This block does not contain the block we are looking for. Pop it. |
| 198 | BlockStack.pop_back(); |
| 199 | Stream.PopBlockScope(); |
| 200 | } |
| 201 | |
| 202 | // Check if we have popped our way to the outermost scope. If so, |
| 203 | // we need to adjust our position. |
| 204 | if (BlockStack.empty()) { |
| 205 | Stream.JumpToBit(Loc.BitNo < LastBPos ? StreamStart.BitNo : LastBPos); |
| 206 | AbbrevNo = 0; |
| 207 | AdvanceStream(); |
| 208 | } |
| 209 | |
| 210 | assert (AbbrevNo == bitc::ENTER_SUBBLOCK); |
| 211 | assert (!BlockStack.empty()); |
| 212 | |
| 213 | while (!AtEnd() && BlockStack.back() != Loc) { |
| 214 | if (BlockStack.back().contains(Loc)) { |
| 215 | Stream.EnterSubBlock(BlockStack.back().BlockID); |
| 216 | AbbrevNo = 0; |
| 217 | AdvanceStream(); |
| 218 | continue; |
| 219 | } |
| 220 | else |
| 221 | SkipBlock(); |
| 222 | } |
| 223 | |
| 224 | if (AtEnd()) |
| 225 | return false; |
| 226 | |
| 227 | assert (BlockStack.back() == Loc); |
| 228 | |
| 229 | return true; |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | unsigned Deserializer::getCurrentBlockID() { |
| 233 | if (!inRecord()) |
| 234 | AdvanceStream(); |
| 235 | |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 236 | return BlockStack.back().BlockID; |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | unsigned Deserializer::getRecordCode() { |
| 240 | if (!inRecord()) { |
| 241 | AdvanceStream(); |
| 242 | assert (AbbrevNo >= bitc::UNABBREV_RECORD); |
| 243 | ReadRecord(); |
| 244 | } |
| 245 | |
| 246 | return RecordCode; |
Ted Kremenek | ec8cd06 | 2007-11-08 19:50:46 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | bool Deserializer::FinishedBlock(Location BlockLoc) { |
| 250 | if (!inRecord()) |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 251 | AdvanceStream(); |
| 252 | |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 253 | for (llvm::SmallVector<Location,8>::reverse_iterator |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 254 | I=BlockStack.rbegin(), E=BlockStack.rend(); I!=E; ++I) |
Ted Kremenek | 1a8a08a | 2007-11-10 02:02:34 +0000 | [diff] [blame^] | 255 | if (*I == BlockLoc) |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 256 | return false; |
Ted Kremenek | ec8cd06 | 2007-11-08 19:50:46 +0000 | [diff] [blame] | 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 261 | unsigned Deserializer::getAbbrevNo() { |
| 262 | if (!inRecord()) |
| 263 | AdvanceStream(); |
| 264 | |
| 265 | return AbbrevNo; |
| 266 | } |
| 267 | |
Ted Kremenek | 0a6d98e | 2007-11-05 21:36:35 +0000 | [diff] [blame] | 268 | bool Deserializer::AtEnd() { |
| 269 | if (inRecord()) |
| 270 | return false; |
| 271 | |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 272 | if (!AdvanceStream()) |
| 273 | return true; |
Ted Kremenek | 0a6d98e | 2007-11-05 21:36:35 +0000 | [diff] [blame] | 274 | |
Ted Kremenek | 48a3969 | 2007-11-09 00:43:51 +0000 | [diff] [blame] | 275 | return false; |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 278 | uint64_t Deserializer::ReadInt() { |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 279 | // FIXME: Any error recovery/handling with incomplete or bad files? |
| 280 | if (!inRecord()) |
| 281 | ReadRecord(); |
| 282 | |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 283 | return Record[RecIdx++]; |
| 284 | } |
| 285 | |
Ted Kremenek | 2e6452c | 2007-11-07 18:24:34 +0000 | [diff] [blame] | 286 | int64_t Deserializer::ReadSInt() { |
| 287 | uint64_t x = ReadInt(); |
| 288 | int64_t magnitude = x >> 1; |
| 289 | return x & 0x1 ? -magnitude : magnitude; |
| 290 | } |
| 291 | |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 292 | char* Deserializer::ReadCStr(char* cstr, unsigned MaxLen, bool isNullTerm) { |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 293 | if (cstr == NULL) |
| 294 | MaxLen = 0; // Zero this just in case someone does something funny. |
| 295 | |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 296 | unsigned len = ReadInt(); |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 298 | assert (MaxLen == 0 || (len + (isNullTerm ? 1 : 0)) <= MaxLen); |
| 299 | |
| 300 | if (!cstr) |
| 301 | cstr = new char[len + (isNullTerm ? 1 : 0)]; |
| 302 | |
| 303 | assert (cstr != NULL); |
| 304 | |
| 305 | for (unsigned i = 0; i < len; ++i) |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 306 | cstr[i] = (char) ReadInt(); |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 307 | |
| 308 | if (isNullTerm) |
| 309 | cstr[len+1] = '\0'; |
| 310 | |
| 311 | return cstr; |
| 312 | } |
| 313 | |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 314 | void Deserializer::ReadCStr(std::vector<char>& buff, bool isNullTerm) { |
| 315 | unsigned len = ReadInt(); |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 316 | |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 317 | buff.clear(); |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 318 | buff.reserve(len); |
| 319 | |
| 320 | for (unsigned i = 0; i < len; ++i) |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 321 | buff.push_back((char) ReadInt()); |
Ted Kremenek | 0b2d7aa | 2007-10-23 21:29:33 +0000 | [diff] [blame] | 322 | |
| 323 | if (isNullTerm) |
| 324 | buff.push_back('\0'); |
| 325 | } |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | a7963a5 | 2007-10-31 22:42:03 +0000 | [diff] [blame] | 327 | void Deserializer::RegisterPtr(unsigned PtrId, const void* Ptr) { |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 328 | MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId)); |
| 329 | |
| 330 | assert (!HasFinalPtr(E) && "Pointer already registered."); |
| 331 | |
Ted Kremenek | 26e25b3 | 2007-11-06 19:49:16 +0000 | [diff] [blame] | 332 | #ifdef DEBUG_BACKPATCH |
| 333 | llvm::cerr << "RegisterPtr: " << PtrId << " => " << Ptr << "\n"; |
| 334 | #endif |
| 335 | |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 336 | SetPtr(E,Ptr); |
Ted Kremenek | fe2a012 | 2007-10-25 00:10:21 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Ted Kremenek | b12a82d | 2007-11-06 22:21:14 +0000 | [diff] [blame] | 339 | void Deserializer::ReadUIntPtr(uintptr_t& PtrRef, bool AllowBackpatch) { |
Ted Kremenek | ec8cd06 | 2007-11-08 19:50:46 +0000 | [diff] [blame] | 340 | SerializedPtrID PtrId = ReadPtrID(); |
Ted Kremenek | fe2a012 | 2007-10-25 00:10:21 +0000 | [diff] [blame] | 341 | |
Ted Kremenek | 2423e03 | 2007-10-25 18:42:52 +0000 | [diff] [blame] | 342 | if (PtrId == 0) { |
Ted Kremenek | 8308a48 | 2007-10-29 18:43:39 +0000 | [diff] [blame] | 343 | PtrRef = 0; |
Ted Kremenek | 2423e03 | 2007-10-25 18:42:52 +0000 | [diff] [blame] | 344 | return; |
Ted Kremenek | 26e25b3 | 2007-11-06 19:49:16 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | #ifdef DEBUG_BACKPATCH |
| 348 | llvm::cerr << "ReadUintPtr: " << PtrId << "\n"; |
| 349 | #endif |
| 350 | |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 351 | MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId)); |
Ted Kremenek | 2423e03 | 2007-10-25 18:42:52 +0000 | [diff] [blame] | 352 | |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 353 | if (HasFinalPtr(E)) |
| 354 | PtrRef = GetFinalPtr(E); |
Ted Kremenek | 5973ef4 | 2007-10-28 21:17:59 +0000 | [diff] [blame] | 355 | else { |
Ted Kremenek | b12a82d | 2007-11-06 22:21:14 +0000 | [diff] [blame] | 356 | assert (AllowBackpatch && |
| 357 | "Client forbids backpatching for this pointer."); |
| 358 | |
Ted Kremenek | 5973ef4 | 2007-10-28 21:17:59 +0000 | [diff] [blame] | 359 | // Register backpatch. Check the freelist for a BPNode. |
| 360 | BPNode* N; |
| 361 | |
| 362 | if (FreeList) { |
| 363 | N = FreeList; |
| 364 | FreeList = FreeList->Next; |
| 365 | } |
| 366 | else // No available BPNode. Allocate one. |
| 367 | N = (BPNode*) Allocator.Allocate<BPNode>(); |
| 368 | |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 369 | new (N) BPNode(GetBPNode(E),PtrRef); |
| 370 | SetBPNode(E,N); |
Ted Kremenek | fe2a012 | 2007-10-25 00:10:21 +0000 | [diff] [blame] | 371 | } |
Ted Kremenek | fe2a012 | 2007-10-25 00:10:21 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Ted Kremenek | eb9409d | 2007-10-31 19:58:32 +0000 | [diff] [blame] | 374 | uintptr_t Deserializer::ReadInternalRefPtr() { |
Ted Kremenek | ec8cd06 | 2007-11-08 19:50:46 +0000 | [diff] [blame] | 375 | SerializedPtrID PtrId = ReadPtrID(); |
Ted Kremenek | eb9409d | 2007-10-31 19:58:32 +0000 | [diff] [blame] | 376 | |
| 377 | assert (PtrId != 0 && "References cannot refer the NULL address."); |
| 378 | |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 379 | MapTy::value_type& E = BPatchMap.FindAndConstruct(BPKey(PtrId)); |
Ted Kremenek | eb9409d | 2007-10-31 19:58:32 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | 14ac17e | 2007-11-05 20:47:27 +0000 | [diff] [blame] | 381 | assert (HasFinalPtr(E) && |
Ted Kremenek | eb9409d | 2007-10-31 19:58:32 +0000 | [diff] [blame] | 382 | "Cannot backpatch references. Object must be already deserialized."); |
| 383 | |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 384 | return GetFinalPtr(E); |
Ted Kremenek | eb9409d | 2007-10-31 19:58:32 +0000 | [diff] [blame] | 385 | } |
Ted Kremenek | 5973ef4 | 2007-10-28 21:17:59 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 387 | void Deserializer::BPEntry::SetPtr(BPNode*& FreeList, void* P) { |
Ted Kremenek | 5973ef4 | 2007-10-28 21:17:59 +0000 | [diff] [blame] | 388 | BPNode* Last = NULL; |
| 389 | |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 390 | for (BPNode* N = Head; N != NULL; N=N->Next) { |
Ted Kremenek | 5973ef4 | 2007-10-28 21:17:59 +0000 | [diff] [blame] | 391 | Last = N; |
| 392 | N->PtrRef |= reinterpret_cast<uintptr_t>(P); |
Ted Kremenek | fe2a012 | 2007-10-25 00:10:21 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Ted Kremenek | 5973ef4 | 2007-10-28 21:17:59 +0000 | [diff] [blame] | 395 | if (Last) { |
| 396 | Last->Next = FreeList; |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 397 | FreeList = Head; |
Ted Kremenek | 5973ef4 | 2007-10-28 21:17:59 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Ted Kremenek | 066ff5b | 2007-11-01 00:57:37 +0000 | [diff] [blame] | 400 | Ptr = const_cast<void*>(P); |
Ted Kremenek | fe2a012 | 2007-10-25 00:10:21 +0000 | [diff] [blame] | 401 | } |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 402 | |
Ted Kremenek | 5973ef4 | 2007-10-28 21:17:59 +0000 | [diff] [blame] | 403 | |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 404 | #define INT_READ(TYPE)\ |
| 405 | void SerializeTrait<TYPE>::Read(Deserializer& D, TYPE& X) {\ |
Ted Kremenek | ff37ccc | 2007-11-01 22:23:34 +0000 | [diff] [blame] | 406 | X = (TYPE) D.ReadInt(); } |
Ted Kremenek | 6e9b496 | 2007-10-24 19:06:40 +0000 | [diff] [blame] | 407 | |
| 408 | INT_READ(bool) |
| 409 | INT_READ(unsigned char) |
| 410 | INT_READ(unsigned short) |
| 411 | INT_READ(unsigned int) |
| 412 | INT_READ(unsigned long) |
Ted Kremenek | 2e6452c | 2007-11-07 18:24:34 +0000 | [diff] [blame] | 413 | |
| 414 | #define SINT_READ(TYPE)\ |
| 415 | void SerializeTrait<TYPE>::Read(Deserializer& D, TYPE& X) {\ |
| 416 | X = (TYPE) D.ReadSInt(); } |
| 417 | |
| 418 | INT_READ(signed char) |
| 419 | INT_READ(signed short) |
| 420 | INT_READ(signed int) |
| 421 | INT_READ(signed long) |