1 //===- DwarfEmitterImpl.h ---------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIB_DWARFLINKERPARALLEL_DWARFEMITTERIMPL_H
10 #define LLVM_LIB_DWARFLINKERPARALLEL_DWARFEMITTERIMPL_H
12 #include "DWARFLinkerCompileUnit.h"
13 #include "llvm/BinaryFormat/Swift.h"
14 #include "llvm/CodeGen/AccelTable.h"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/DWARFLinkerParallel/DWARFLinker.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCObjectFileInfo.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Target/TargetMachine.h"
28 /// User of DwarfEmitterImpl should call initialization code
31 /// InitializeAllTargetInfos();
32 /// InitializeAllTargetMCs();
33 /// InitializeAllTargets();
34 /// InitializeAllAsmPrinters();
36 template <typename DataT
> class AccelTable
;
38 class DWARFDebugMacro
;
40 namespace dwarflinker_parallel
{
42 /// This class emits DWARF data to the output stream. It emits already
43 /// generated section data and specific data, which could not be generated
45 class DwarfEmitterImpl
: public ExtraDwarfEmitter
{
47 DwarfEmitterImpl(DWARFLinker::OutputFileType OutFileType
,
48 raw_pwrite_stream
&OutFile
)
49 : OutFile(OutFile
), OutFileType(OutFileType
) {}
51 /// Initialize AsmPrinter data.
52 Error
init(Triple TheTriple
, StringRef Swift5ReflectionSegmentName
);
54 /// Returns triple of output stream.
55 const Triple
&getTargetTriple() { return MC
->getTargetTriple(); }
57 /// Dump the file to the disk.
58 void finish() override
{ MS
->finish(); }
60 /// Returns AsmPrinter.
61 AsmPrinter
&getAsmPrinter() const override
{ return *Asm
; }
63 /// Emit the swift_ast section stored in \p Buffer.
64 void emitSwiftAST(StringRef Buffer
) override
;
66 /// Emit the swift reflection section stored in \p Buffer.
67 void emitSwiftReflectionSection(
68 llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind
,
69 StringRef Buffer
, uint32_t Alignment
, uint32_t) override
;
71 /// Emit specified section data.
72 void emitSectionContents(StringRef SecData
, StringRef SecName
) override
;
74 /// Emit temporary symbol.
75 MCSymbol
*emitTempSym(StringRef SecName
, StringRef SymName
) override
;
77 /// Emit abbreviations.
78 void emitAbbrevs(const SmallVector
<std::unique_ptr
<DIEAbbrev
>> &Abbrevs
,
79 unsigned DwarfVersion
);
81 /// Emit compile unit header.
82 void emitCompileUnitHeader(DwarfUnit
&Unit
);
84 /// Emit DIE recursively.
85 void emitDIE(DIE
&Die
);
87 /// Returns size of generated .debug_info section.
88 uint64_t getDebugInfoSectionSize() const { return DebugInfoSectionSize
; }
91 // Enumerate all string patches and write them into the destination section.
92 // Order of patches is the same as in original input file. To avoid emitting
93 // the same string twice we accumulate NextOffset value. Thus if string
94 // offset smaller than NextOffset value then the patch is skipped (as that
95 // string was emitted earlier).
96 template <typename PatchTy
>
97 void emitStringsImpl(ArrayList
<PatchTy
> &StringPatches
,
98 const StringEntryToDwarfStringPoolEntryMap
&Strings
,
99 uint64_t &NextOffset
, MCSection
*OutSection
);
101 MCSection
*switchSection(StringRef SecName
);
103 /// \defgroup MCObjects MC layer objects constructed by the streamer
105 std::unique_ptr
<MCRegisterInfo
> MRI
;
106 std::unique_ptr
<MCAsmInfo
> MAI
;
107 std::unique_ptr
<MCObjectFileInfo
> MOFI
;
108 std::unique_ptr
<MCContext
> MC
;
109 MCAsmBackend
*MAB
; // Owned by MCStreamer
110 std::unique_ptr
<MCInstrInfo
> MII
;
111 std::unique_ptr
<MCSubtargetInfo
> MSTI
;
112 MCInstPrinter
*MIP
; // Owned by AsmPrinter
113 MCCodeEmitter
*MCE
; // Owned by MCStreamer
114 MCStreamer
*MS
; // Owned by AsmPrinter
115 std::unique_ptr
<TargetMachine
> TM
;
116 std::unique_ptr
<AsmPrinter
> Asm
;
119 /// The output file we stream the linked Dwarf to.
120 raw_pwrite_stream
&OutFile
;
121 DWARFLinker::OutputFileType OutFileType
= DWARFLinker::OutputFileType::Object
;
123 uint64_t DebugInfoSectionSize
= 0;
126 } // end namespace dwarflinker_parallel
127 } // end namespace llvm
129 #endif // LLVM_LIB_DWARFLINKERPARALLEL_DWARFEMITTERIMPL_H