Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 1 | //===- SymbolSerializer.cpp -----------------------------------------------===// |
Zachary Turner | 407dec5 | 2017-03-13 14:57:45 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/DebugInfo/CodeView/SymbolSerializer.h" |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/ArrayRef.h" |
| 12 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
| 13 | #include "llvm/Support/Endian.h" |
| 14 | #include "llvm/Support/Error.h" |
| 15 | #include <cassert> |
| 16 | #include <cstdint> |
| 17 | #include <cstring> |
Zachary Turner | 407dec5 | 2017-03-13 14:57:45 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | using namespace llvm::codeview; |
| 21 | |
Zachary Turner | ebd3ae8 | 2017-06-01 21:52:41 +0000 | [diff] [blame] | 22 | SymbolSerializer::SymbolSerializer(BumpPtrAllocator &Allocator, |
| 23 | CodeViewContainer Container) |
| 24 | : Storage(Allocator), RecordBuffer(MaxRecordLength), |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 25 | Stream(RecordBuffer, support::little), Writer(Stream), |
Zachary Turner | ebd3ae8 | 2017-06-01 21:52:41 +0000 | [diff] [blame] | 26 | Mapping(Writer, Container) {} |
Zachary Turner | 407dec5 | 2017-03-13 14:57:45 +0000 | [diff] [blame] | 27 | |
| 28 | Error SymbolSerializer::visitSymbolBegin(CVSymbol &Record) { |
| 29 | assert(!CurrentSymbol.hasValue() && "Already in a symbol mapping!"); |
| 30 | |
| 31 | Writer.setOffset(0); |
| 32 | |
| 33 | if (auto EC = writeRecordPrefix(Record.kind())) |
| 34 | return EC; |
| 35 | |
| 36 | CurrentSymbol = Record.kind(); |
| 37 | if (auto EC = Mapping.visitSymbolBegin(Record)) |
| 38 | return EC; |
| 39 | |
| 40 | return Error::success(); |
| 41 | } |
| 42 | |
| 43 | Error SymbolSerializer::visitSymbolEnd(CVSymbol &Record) { |
| 44 | assert(CurrentSymbol.hasValue() && "Not in a symbol mapping!"); |
| 45 | |
| 46 | if (auto EC = Mapping.visitSymbolEnd(Record)) |
| 47 | return EC; |
| 48 | |
| 49 | uint32_t RecordEnd = Writer.getOffset(); |
| 50 | uint16_t Length = RecordEnd - 2; |
| 51 | Writer.setOffset(0); |
| 52 | if (auto EC = Writer.writeInteger(Length)) |
| 53 | return EC; |
| 54 | |
| 55 | uint8_t *StableStorage = Storage.Allocate<uint8_t>(RecordEnd); |
| 56 | ::memcpy(StableStorage, &RecordBuffer[0], RecordEnd); |
| 57 | Record.RecordData = ArrayRef<uint8_t>(StableStorage, RecordEnd); |
| 58 | CurrentSymbol.reset(); |
| 59 | |
| 60 | return Error::success(); |
| 61 | } |