Index: lib/CodeGen/AsmPrinter/DwarfDebug.h =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.h +++ lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -207,6 +207,9 @@ /// Maps a CU DIE with its corresponding DwarfCompileUnit. DenseMap CUDieMap; + /// Maps DwarfCompileUnit with label emitted before macro section entry. + DenseMap LabelsBeforeMSE; + /// List of all labels used in aranges generation. std::vector ArangeLabels; @@ -414,11 +417,9 @@ /// Emit macros into a debug macinfo section. void emitDebugMacinfo(); - unsigned emitMacro(AsmStreamerBase *AS, DIMacro &M); - unsigned emitMacroFile(AsmStreamerBase *AS, DIMacroFile &F, - DwarfCompileUnit &U); - unsigned handleMacroNodes(AsmStreamerBase *AS, DIMacroNodeArray Nodes, - DwarfCompileUnit &U); + void emitMacro(DIMacro &M); + void emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U); + void handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U); /// DWARF 5 Experimental Split Dwarf Emitters Index: lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -561,8 +561,8 @@ // Collect info for variables that were optimized out. collectDeadVariables(); - unsigned MacroOffset = 0; - std::unique_ptr AS(new SizeReporterAsmStreamer(Asm)); + MCSymbol *PrevLabel = nullptr; + unsigned CUID = 0; // Handle anything that needs to be done on a per-unit basis after // all other generation. for (const auto &P : CUMap) { @@ -617,13 +617,15 @@ } auto *CUNode = cast(P.first); - if (CUNode->getMacros()) { + MCSymbol *CurrLabel = Asm->createTempSymbol(Twine("macro") + Twine(CUID++)); + LabelsBeforeMSE[&U] = CurrLabel; + if (!PrevLabel) + PrevLabel = CurrLabel; + if (CUNode->getMacros()) // Compile Unit has macros, emit "DW_AT_macro_info" attribute. - U.addUInt(U.getUnitDie(), dwarf::DW_AT_macro_info, - dwarf::DW_FORM_sec_offset, MacroOffset); - // Update macro section offset - MacroOffset += handleMacroNodes(AS.get(), CUNode->getMacros(), U); - } + U.addLabelDelta(U.getUnitDie(), dwarf::DW_AT_macro_info, CurrLabel, + PrevLabel, dwarf::DW_FORM_sec_offset); + PrevLabel = CurrLabel; } // Compute DIE offsets and sizes. @@ -1865,50 +1867,41 @@ } } -unsigned DwarfDebug::handleMacroNodes(AsmStreamerBase *AS, - DIMacroNodeArray Nodes, - DwarfCompileUnit &U) { - unsigned Size = 0; +void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) { for (auto *MN : Nodes) { if (auto *M = dyn_cast(MN)) - Size += emitMacro(AS, *M); + emitMacro(*M); else if (auto *F = dyn_cast(MN)) - Size += emitMacroFile(AS, *F, U); + emitMacroFile(*F, U); else llvm_unreachable("Unexpected DI type!"); } - return Size; } -unsigned DwarfDebug::emitMacro(AsmStreamerBase *AS, DIMacro &M) { - int Size = 0; - Size += AS->emitULEB128(M.getMacinfoType()); - Size += AS->emitULEB128(M.getLine()); +void DwarfDebug::emitMacro(DIMacro &M) { + Asm->EmitULEB128(M.getMacinfoType()); + Asm->EmitULEB128(M.getLine()); StringRef Name = M.getName(); StringRef Value = M.getValue(); - Size += AS->emitBytes(Name); + Asm->OutStreamer->EmitBytes(Name); if (!Value.empty()) { // There should be one space between macro name and macro value. - Size += AS->emitInt8(' '); - Size += AS->emitBytes(Value); + Asm->EmitInt8(' '); + Asm->OutStreamer->EmitBytes(Value); } - Size += AS->emitInt8('\0'); - return Size; + Asm->EmitInt8('\0'); } -unsigned DwarfDebug::emitMacroFile(AsmStreamerBase *AS, DIMacroFile &F, - DwarfCompileUnit &U) { - int Size = 0; +void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) { assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file); - Size += AS->emitULEB128(dwarf::DW_MACINFO_start_file); - Size += AS->emitULEB128(F.getLine()); + Asm->EmitULEB128(dwarf::DW_MACINFO_start_file); + Asm->EmitULEB128(F.getLine()); DIFile *File = F.getFile(); unsigned FID = U.getOrCreateSourceID(File->getFilename(), File->getDirectory()); - Size += AS->emitULEB128(FID); - Size += handleMacroNodes(AS, F.getElements(), U); - Size += AS->emitULEB128(dwarf::DW_MACINFO_end_file); - return Size; + Asm->EmitULEB128(FID); + handleMacroNodes(F.getElements(), U); + Asm->EmitULEB128(dwarf::DW_MACINFO_end_file); } // Emit visible names into a debug macinfo section. @@ -1917,13 +1910,13 @@ // Start the dwarf macinfo section. Asm->OutStreamer->SwitchSection(Macinfo); } - std::unique_ptr AS(new EmittingAsmStreamer(Asm)); for (const auto &P : CUMap) { auto &TheCU = *P.second; auto *SkCU = TheCU.getSkeleton(); DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; auto *CUNode = cast(P.first); - handleMacroNodes(AS.get(), CUNode->getMacros(), U); + Asm->OutStreamer->EmitLabel(LabelsBeforeMSE[&U]); + handleMacroNodes(CUNode->getMacros(), U); } Asm->OutStreamer->AddComment("End Of Macro List Mark"); Asm->EmitInt8(0); Index: lib/CodeGen/AsmPrinter/DwarfUnit.h =================================================================== --- lib/CodeGen/AsmPrinter/DwarfUnit.h +++ lib/CodeGen/AsmPrinter/DwarfUnit.h @@ -212,7 +212,8 @@ /// Add a label delta attribute data and value. void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi, - const MCSymbol *Lo); + const MCSymbol *Lo, + dwarf::Form Form = dwarf::DW_FORM_data4); /// Add a DIE attribute data and value. void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry); Index: lib/CodeGen/AsmPrinter/DwarfUnit.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -259,8 +259,9 @@ } void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute, - const MCSymbol *Hi, const MCSymbol *Lo) { - Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_data4, + const MCSymbol *Hi, const MCSymbol *Lo, + dwarf::Form Form) { + Die.addValue(DIEValueAllocator, Attribute, Form, new (DIEValueAllocator) DIEDelta(Hi, Lo)); }