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
;
39 namespace dwarflinker_parallel
{
41 using DebugNamesUnitsOffsets
= std::vector
<std::variant
<MCSymbol
*, uint64_t>>;
42 using CompUnitIDToIdx
= DenseMap
<unsigned, size_t>;
44 /// This class emits DWARF data to the output stream. It emits already
45 /// generated section data and specific data, which could not be generated
47 class DwarfEmitterImpl
: public ExtraDwarfEmitter
{
49 DwarfEmitterImpl(DWARFLinker::OutputFileType OutFileType
,
50 raw_pwrite_stream
&OutFile
)
51 : OutFile(OutFile
), OutFileType(OutFileType
) {}
53 /// Initialize AsmPrinter data.
54 Error
init(Triple TheTriple
, StringRef Swift5ReflectionSegmentName
);
56 /// Returns triple of output stream.
57 const Triple
&getTargetTriple() { return MC
->getTargetTriple(); }
59 /// Dump the file to the disk.
60 void finish() override
{ MS
->finish(); }
62 /// Returns AsmPrinter.
63 AsmPrinter
&getAsmPrinter() const override
{ return *Asm
; }
65 /// Emit the swift_ast section stored in \p Buffer.
66 void emitSwiftAST(StringRef Buffer
) override
;
68 /// Emit the swift reflection section stored in \p Buffer.
69 void emitSwiftReflectionSection(
70 llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind
,
71 StringRef Buffer
, uint32_t Alignment
, uint32_t) override
;
73 /// Emit specified section data.
74 void emitSectionContents(StringRef SecData
, StringRef SecName
) override
;
76 /// Emit abbreviations.
77 void emitAbbrevs(const SmallVector
<std::unique_ptr
<DIEAbbrev
>> &Abbrevs
,
78 unsigned DwarfVersion
);
80 /// Emit compile unit header.
81 void emitCompileUnitHeader(DwarfUnit
&Unit
);
83 /// Emit DIE recursively.
84 void emitDIE(DIE
&Die
);
86 /// Returns size of generated .debug_info section.
87 uint64_t getDebugInfoSectionSize() const { return DebugInfoSectionSize
; }
89 /// Emits .debug_names section according to the specified \p Table.
90 void emitDebugNames(DWARF5AccelTable
&Table
,
91 DebugNamesUnitsOffsets
&CUOffsets
,
92 CompUnitIDToIdx
&UnitIDToIdxMap
);
94 /// Emits .apple_names section according to the specified \p Table.
95 void emitAppleNames(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
);
97 /// Emits .apple_namespaces section according to the specified \p Table.
98 void emitAppleNamespaces(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
);
100 /// Emits .apple_objc section according to the specified \p Table.
101 void emitAppleObjc(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
);
103 /// Emits .apple_types section according to the specified \p Table.
104 void emitAppleTypes(AccelTable
<AppleAccelTableStaticTypeData
> &Table
);
107 // Enumerate all string patches and write them into the destination section.
108 // Order of patches is the same as in original input file. To avoid emitting
109 // the same string twice we accumulate NextOffset value. Thus if string
110 // offset smaller than NextOffset value then the patch is skipped (as that
111 // string was emitted earlier).
112 template <typename PatchTy
>
113 void emitStringsImpl(ArrayList
<PatchTy
> &StringPatches
,
114 const StringEntryToDwarfStringPoolEntryMap
&Strings
,
115 uint64_t &NextOffset
, MCSection
*OutSection
);
117 MCSection
*switchSection(StringRef SecName
);
119 /// \defgroup MCObjects MC layer objects constructed by the streamer
121 std::unique_ptr
<MCRegisterInfo
> MRI
;
122 std::unique_ptr
<MCAsmInfo
> MAI
;
123 std::unique_ptr
<MCObjectFileInfo
> MOFI
;
124 std::unique_ptr
<MCContext
> MC
;
125 MCAsmBackend
*MAB
; // Owned by MCStreamer
126 std::unique_ptr
<MCInstrInfo
> MII
;
127 std::unique_ptr
<MCSubtargetInfo
> MSTI
;
128 MCInstPrinter
*MIP
; // Owned by AsmPrinter
129 MCCodeEmitter
*MCE
; // Owned by MCStreamer
130 MCStreamer
*MS
; // Owned by AsmPrinter
131 std::unique_ptr
<TargetMachine
> TM
;
132 std::unique_ptr
<AsmPrinter
> Asm
;
135 /// The output file we stream the linked Dwarf to.
136 raw_pwrite_stream
&OutFile
;
137 DWARFLinker::OutputFileType OutFileType
= DWARFLinker::OutputFileType::Object
;
139 uint64_t DebugInfoSectionSize
= 0;
142 } // end namespace dwarflinker_parallel
143 } // end namespace llvm
145 #endif // LLVM_LIB_DWARFLINKERPARALLEL_DWARFEMITTERIMPL_H