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