17a7e6055SDimitry Andric //===- SymbolSerializer.cpp -------------------------------------*- C++ -*-===//
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"
117a7e6055SDimitry Andric 
127a7e6055SDimitry Andric using namespace llvm;
137a7e6055SDimitry Andric using namespace llvm::codeview;
147a7e6055SDimitry Andric 
15*6d97bb29SDimitry Andric SymbolSerializer::SymbolSerializer(BumpPtrAllocator &Allocator,
16*6d97bb29SDimitry Andric                                    CodeViewContainer Container)
17*6d97bb29SDimitry Andric     : Storage(Allocator), RecordBuffer(MaxRecordLength),
18*6d97bb29SDimitry Andric       Stream(RecordBuffer, llvm::support::little), Writer(Stream),
19*6d97bb29SDimitry Andric       Mapping(Writer, Container) {}
207a7e6055SDimitry Andric 
217a7e6055SDimitry Andric Error SymbolSerializer::visitSymbolBegin(CVSymbol &Record) {
227a7e6055SDimitry Andric   assert(!CurrentSymbol.hasValue() && "Already in a symbol mapping!");
237a7e6055SDimitry Andric 
247a7e6055SDimitry Andric   Writer.setOffset(0);
257a7e6055SDimitry Andric 
267a7e6055SDimitry Andric   if (auto EC = writeRecordPrefix(Record.kind()))
277a7e6055SDimitry Andric     return EC;
287a7e6055SDimitry Andric 
297a7e6055SDimitry Andric   CurrentSymbol = Record.kind();
307a7e6055SDimitry Andric   if (auto EC = Mapping.visitSymbolBegin(Record))
317a7e6055SDimitry Andric     return EC;
327a7e6055SDimitry Andric 
337a7e6055SDimitry Andric   return Error::success();
347a7e6055SDimitry Andric }
357a7e6055SDimitry Andric 
367a7e6055SDimitry Andric Error SymbolSerializer::visitSymbolEnd(CVSymbol &Record) {
377a7e6055SDimitry Andric   assert(CurrentSymbol.hasValue() && "Not in a symbol mapping!");
387a7e6055SDimitry Andric 
397a7e6055SDimitry Andric   if (auto EC = Mapping.visitSymbolEnd(Record))
407a7e6055SDimitry Andric     return EC;
417a7e6055SDimitry Andric 
427a7e6055SDimitry Andric   uint32_t RecordEnd = Writer.getOffset();
437a7e6055SDimitry Andric   uint16_t Length = RecordEnd - 2;
447a7e6055SDimitry Andric   Writer.setOffset(0);
457a7e6055SDimitry Andric   if (auto EC = Writer.writeInteger(Length))
467a7e6055SDimitry Andric     return EC;
477a7e6055SDimitry Andric 
487a7e6055SDimitry Andric   uint8_t *StableStorage = Storage.Allocate<uint8_t>(RecordEnd);
497a7e6055SDimitry Andric   ::memcpy(StableStorage, &RecordBuffer[0], RecordEnd);
507a7e6055SDimitry Andric   Record.RecordData = ArrayRef<uint8_t>(StableStorage, RecordEnd);
517a7e6055SDimitry Andric   CurrentSymbol.reset();
527a7e6055SDimitry Andric 
537a7e6055SDimitry Andric   return Error::success();
547a7e6055SDimitry Andric }
55