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