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