1a580b014SDimitry Andric //===- SymbolSerializer.cpp -----------------------------------------------===//
27a7e6055SDimitry Andric //
37a7e6055SDimitry Andric //                     The LLVM Compiler Infrastructure
47a7e6055SDimitry Andric //
57a7e6055SDimitry Andric // This file is distributed under the University of Illinois Open Source
67a7e6055SDimitry Andric // License. See LICENSE.TXT for details.
77a7e6055SDimitry Andric //
87a7e6055SDimitry Andric //===----------------------------------------------------------------------===//
97a7e6055SDimitry Andric 
107a7e6055SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
11a580b014SDimitry Andric #include "llvm/ADT/ArrayRef.h"
12a580b014SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
13a580b014SDimitry Andric #include "llvm/Support/Endian.h"
14a580b014SDimitry Andric #include "llvm/Support/Error.h"
15a580b014SDimitry Andric #include <cassert>
16a580b014SDimitry Andric #include <cstdint>
17a580b014SDimitry Andric #include <cstring>
187a7e6055SDimitry Andric 
197a7e6055SDimitry Andric using namespace llvm;
207a7e6055SDimitry Andric using namespace llvm::codeview;
217a7e6055SDimitry Andric 
SymbolSerializer(BumpPtrAllocator & Allocator,CodeViewContainer Container)226d97bb29SDimitry Andric SymbolSerializer::SymbolSerializer(BumpPtrAllocator &Allocator,
236d97bb29SDimitry Andric                                    CodeViewContainer Container)
24*2cab237bSDimitry Andric     : Storage(Allocator), Stream(RecordBuffer, support::little), Writer(Stream),
256d97bb29SDimitry Andric       Mapping(Writer, Container) {}
267a7e6055SDimitry Andric 
visitSymbolBegin(CVSymbol & Record)277a7e6055SDimitry Andric Error SymbolSerializer::visitSymbolBegin(CVSymbol &Record) {
287a7e6055SDimitry Andric   assert(!CurrentSymbol.hasValue() && "Already in a symbol mapping!");
297a7e6055SDimitry Andric 
307a7e6055SDimitry Andric   Writer.setOffset(0);
317a7e6055SDimitry Andric 
327a7e6055SDimitry Andric   if (auto EC = writeRecordPrefix(Record.kind()))
337a7e6055SDimitry Andric     return EC;
347a7e6055SDimitry Andric 
357a7e6055SDimitry Andric   CurrentSymbol = Record.kind();
367a7e6055SDimitry Andric   if (auto EC = Mapping.visitSymbolBegin(Record))
377a7e6055SDimitry Andric     return EC;
387a7e6055SDimitry Andric 
397a7e6055SDimitry Andric   return Error::success();
407a7e6055SDimitry Andric }
417a7e6055SDimitry Andric 
visitSymbolEnd(CVSymbol & Record)427a7e6055SDimitry Andric Error SymbolSerializer::visitSymbolEnd(CVSymbol &Record) {
437a7e6055SDimitry Andric   assert(CurrentSymbol.hasValue() && "Not in a symbol mapping!");
447a7e6055SDimitry Andric 
457a7e6055SDimitry Andric   if (auto EC = Mapping.visitSymbolEnd(Record))
467a7e6055SDimitry Andric     return EC;
477a7e6055SDimitry Andric 
487a7e6055SDimitry Andric   uint32_t RecordEnd = Writer.getOffset();
497a7e6055SDimitry Andric   uint16_t Length = RecordEnd - 2;
507a7e6055SDimitry Andric   Writer.setOffset(0);
517a7e6055SDimitry Andric   if (auto EC = Writer.writeInteger(Length))
527a7e6055SDimitry Andric     return EC;
537a7e6055SDimitry Andric 
547a7e6055SDimitry Andric   uint8_t *StableStorage = Storage.Allocate<uint8_t>(RecordEnd);
557a7e6055SDimitry Andric   ::memcpy(StableStorage, &RecordBuffer[0], RecordEnd);
567a7e6055SDimitry Andric   Record.RecordData = ArrayRef<uint8_t>(StableStorage, RecordEnd);
577a7e6055SDimitry Andric   CurrentSymbol.reset();
587a7e6055SDimitry Andric 
597a7e6055SDimitry Andric   return Error::success();
607a7e6055SDimitry Andric }
61