Author: Chen Zheng
Date: 2021-01-25T07:01:54-05:00
New Revision: 4fda9ab7444d93fa12e830975230fc4a1fdfe094

URL: 
https://github.com/llvm/llvm-project/commit/4fda9ab7444d93fa12e830975230fc4a1fdfe094
DIFF: 
https://github.com/llvm/llvm-project/commit/4fda9ab7444d93fa12e830975230fc4a1fdfe094.diff

LOG: [AIX DWARF] support aix dwarf for 32-bit assembly.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCAsmInfo.h
    llvm/include/llvm/MC/MCAsmStreamer.h
    llvm/include/llvm/MC/MCContext.h
    llvm/include/llvm/MC/MCDwarf.h
    llvm/include/llvm/MC/MCSectionXCOFF.h
    llvm/include/llvm/MC/MCStreamer.h
    llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
    llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
    llvm/lib/MC/MCAsmInfoXCOFF.cpp
    llvm/lib/MC/MCAsmStreamer.cpp
    llvm/lib/MC/MCContext.cpp
    llvm/lib/MC/MCDwarf.cpp
    llvm/lib/MC/MCObjectFileInfo.cpp
    llvm/lib/MC/MCSectionXCOFF.cpp
    llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCAsmInfo.h 
b/llvm/include/llvm/MC/MCAsmInfo.h
index 98e9c2fd3607..3c7d9420d963 100644
--- a/llvm/include/llvm/MC/MCAsmInfo.h
+++ b/llvm/include/llvm/MC/MCAsmInfo.h
@@ -388,6 +388,14 @@ class MCAsmInfo {
   /// absolute 
diff erence.
   bool DwarfFDESymbolsUseAbsDiff = false;
 
+  /// True if the target supports generating the DWARF line table through using
+  /// of the .loc/.file directives. Defaults to true.
+  bool UsesDwarfFileAndLocDirectives = true;
+
+  /// True if the target needs dwarf section size in the header of dwarf 
section
+  /// (if any) in assembly file. Defaults to true.
+  bool DwarfSectionSizeRequired = true;
+
   /// True if dwarf register numbers are printed instead of symbolic register
   /// names in .cfi_* directives.  Defaults to false.
   bool DwarfRegNumForCFI = false;
@@ -667,6 +675,14 @@ class MCAsmInfo {
     return SupportsExtendedDwarfLocDirective;
   }
 
+  bool usesDwarfFileAndLocDirectives() const {
+    return UsesDwarfFileAndLocDirectives;
+  }
+
+  bool requiredDwarfSectionSize() const {
+    return DwarfSectionSizeRequired;
+  }
+
   void addInitialFrameState(const MCCFIInstruction &Inst);
 
   const std::vector<MCCFIInstruction> &getInitialFrameState() const {

diff  --git a/llvm/include/llvm/MC/MCAsmStreamer.h 
b/llvm/include/llvm/MC/MCAsmStreamer.h
index 5b2f407841e5..3bb845c40b43 100644
--- a/llvm/include/llvm/MC/MCAsmStreamer.h
+++ b/llvm/include/llvm/MC/MCAsmStreamer.h
@@ -367,6 +367,9 @@ class MCAsmStreamer final : public MCStreamer {
   void emitRawTextImpl(StringRef String) override;
 
   void finishImpl() override;
+  void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
+                                const MCSymbol *Label,
+                                unsigned PointerSize) override;
 };
 
 } // end anonymous namespace.

diff  --git a/llvm/include/llvm/MC/MCContext.h 
b/llvm/include/llvm/MC/MCContext.h
index 49ab0ce8d6fd..1c31fdfc4165 100644
--- a/llvm/include/llvm/MC/MCContext.h
+++ b/llvm/include/llvm/MC/MCContext.h
@@ -574,7 +574,8 @@ namespace llvm {
                                     XCOFF::StorageMappingClass MappingClass,
                                     XCOFF::SymbolType CSectType, SectionKind K,
                                     bool MultiSymbolsAllowed = false,
-                                    const char *BeginSymName = nullptr);
+                                    const char *BeginSymName = nullptr,
+                                    unsigned SecFlags = 0);
 
     // Create and save a copy of STI and return a reference to the copy.
     MCSubtargetInfo &getSubtargetCopy(const MCSubtargetInfo &STI);

diff  --git a/llvm/include/llvm/MC/MCDwarf.h b/llvm/include/llvm/MC/MCDwarf.h
index 70da5f76e766..715035203cbc 100644
--- a/llvm/include/llvm/MC/MCDwarf.h
+++ b/llvm/include/llvm/MC/MCDwarf.h
@@ -172,7 +172,7 @@ class MCDwarfLineEntry : public MCDwarfLoc {
   // This is called when an instruction is assembled into the specified
   // section and if there is information from the last .loc directive that
   // has yet to have a line entry made for it is made.
-  static void Make(MCObjectStreamer *MCOS, MCSection *Section);
+  static void Make(MCStreamer *MCOS, MCSection *Section);
 };
 
 /// Instances of this class represent the line information for a compile
@@ -310,10 +310,10 @@ class MCDwarfLineTable {
 
 public:
   // This emits the Dwarf file and the line tables for all Compile Units.
-  static void Emit(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params);
+  static void Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params);
 
   // This emits the Dwarf file and the line tables for a given Compile Unit.
-  void EmitCU(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params,
+  void EmitCU(MCStreamer *MCOS, MCDwarfLineTableParams Params,
               Optional<MCDwarfLineStr> &LineStr) const;
 
   Expected<unsigned> tryGetFile(StringRef &Directory, StringRef &FileName,

diff  --git a/llvm/include/llvm/MC/MCSectionXCOFF.h 
b/llvm/include/llvm/MC/MCSectionXCOFF.h
index aa39dff07180..9d9c69f175dc 100644
--- a/llvm/include/llvm/MC/MCSectionXCOFF.h
+++ b/llvm/include/llvm/MC/MCSectionXCOFF.h
@@ -36,18 +36,20 @@ class MCSectionXCOFF final : public MCSection {
   XCOFF::SymbolType Type;
   MCSymbolXCOFF *const QualName;
   StringRef SymbolTableName;
+  unsigned SecFlags;
   bool MultiSymbolsAllowed;
   static constexpr unsigned DefaultAlignVal = 4;
 
   MCSectionXCOFF(StringRef Name, XCOFF::StorageMappingClass SMC,
                  XCOFF::SymbolType ST, SectionKind K, MCSymbolXCOFF *QualName,
-                 MCSymbol *Begin, StringRef SymbolTableName,
+                 unsigned SecFlags, MCSymbol *Begin, StringRef SymbolTableName,
                  bool MultiSymbolsAllowed)
       : MCSection(SV_XCOFF, Name, K, Begin), MappingClass(SMC), Type(ST),
         QualName(QualName), SymbolTableName(SymbolTableName),
-        MultiSymbolsAllowed(MultiSymbolsAllowed) {
-    assert((ST == XCOFF::XTY_SD || ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) 
&&
-           "Invalid or unhandled type for csect.");
+        SecFlags(SecFlags), MultiSymbolsAllowed(MultiSymbolsAllowed) {
+    assert(
+        (ST == XCOFF::XTY_SD || ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) &&
+        "Invalid or unhandled type for csect.");
     assert(QualName != nullptr && "QualName is needed.");
     QualName->setRepresentedCsect(this);
     QualName->setStorageClass(XCOFF::C_HIDEXT);
@@ -79,6 +81,7 @@ class MCSectionXCOFF final : public MCSection {
   bool isVirtualSection() const override;
   StringRef getSymbolTableName() const { return SymbolTableName; }
   bool isMultiSymbolsAllowed() const { return MultiSymbolsAllowed; }
+  unsigned getSecFlags() const { return SecFlags; }
 };
 
 } // end namespace llvm

diff  --git a/llvm/include/llvm/MC/MCStreamer.h 
b/llvm/include/llvm/MC/MCStreamer.h
index cf27c0e89f0c..6d0473196e21 100644
--- a/llvm/include/llvm/MC/MCStreamer.h
+++ b/llvm/include/llvm/MC/MCStreamer.h
@@ -1092,6 +1092,14 @@ class MCStreamer {
   void Finish(SMLoc EndLoc = SMLoc());
 
   virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
+
+  /// If targets does not support representing debug line section by .loc/.file
+  /// directives in assembly output, we need to populate debug line section 
with
+  /// raw debug line contents.
+  virtual void emitDwarfAdvanceLineAddr(int64_t LineDelta,
+                                        const MCSymbol *LastLabel,
+                                        const MCSymbol *Label,
+                                        unsigned PointerSize) {}
 };
 
 /// Create a dummy machine code streamer, which does nothing. This is useful 
for

diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
index c6e43445e7d0..8fb21a63d463 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
@@ -18,6 +18,7 @@
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCAsmStreamer.h"
 #include "llvm/MC/MCDwarf.h"
 #include "llvm/MC/MCRegisterInfo.h"
 #include "llvm/MC/MCSection.h"
@@ -207,6 +208,12 @@ void AsmPrinter::maybeEmitDwarf64Mark() const {
 
 void AsmPrinter::emitDwarfUnitLength(uint64_t Length,
                                      const Twine &Comment) const {
+  // If the assembler on some targets will fill the dwarf unit length, we
+  // don't need to emit the length in compiler. For exmaple AIX assembler
+  // requires the assembly file without unit length in debug sections header.
+  if (!OutStreamer->getContext().getAsmInfo()->requiredDwarfSectionSize() &&
+      isa<MCAsmStreamer>(OutStreamer))
+    return;
   assert(isDwarf64() || Length <= dwarf::DW_LENGTH_lo_reserved);
   maybeEmitDwarf64Mark();
   OutStreamer->AddComment(Comment);
@@ -215,6 +222,12 @@ void AsmPrinter::emitDwarfUnitLength(uint64_t Length,
 
 void AsmPrinter::emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo,
                                      const Twine &Comment) const {
+  // If the assembler on some targets will fill the dwarf unit length, we
+  // don't need to emit the length in compiler. For exmaple AIX assembler
+  // requires the assembly file without unit length in debug sections header.
+  if (!OutStreamer->getContext().getAsmInfo()->requiredDwarfSectionSize() &&
+      isa<MCAsmStreamer>(OutStreamer))
+    return;
   maybeEmitDwarf64Mark();
   OutStreamer->AddComment(Comment);
   OutStreamer->emitAbsoluteSymbolDiff(Hi, Lo, getDwarfOffsetByteSize());

diff  --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp 
b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
index befc4bba19a2..61bc7da773d7 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
@@ -27,6 +27,7 @@
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/DebugInfo.h"
 #include "llvm/IR/GlobalVariable.h"
+#include "llvm/MC/MCAsmStreamer.h"
 #include "llvm/MC/MCSection.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSymbol.h"
@@ -356,14 +357,32 @@ void DwarfCompileUnit::initStmtList() {
   // left in the skeleton CU and so not included.
   // The line table entries are not always emitted in assembly, so it
   // is not okay to use line_table_start here.
-      addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym,
-                      TLOF.getDwarfLineSection()->getBeginSymbol());
+  // If the assembler on some targets will fill the dwarf unit length, we
+  // don't need to emit the length in compiler. For exmaple AIX assembler
+  // requires an assembly file without unit length in debug sections header.
+  // We need to adjust the reference here to contain the assembler insertion
+  // length.
+  MCContext &Ctx = Asm->OutStreamer->getContext();
+  if (!Ctx.getAsmInfo()->requiredDwarfSectionSize() &&
+      isa<MCAsmStreamer>(Asm->OutStreamer))
+    addLabelPlusOffset(
+        getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym,
+        -dwarf::getUnitLengthFieldByteSize(Ctx.getDwarfFormat()));
+  else
+    addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym,
+                    TLOF.getDwarfLineSection()->getBeginSymbol());
 }
 
 void DwarfCompileUnit::applyStmtList(DIE &D) {
-  const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
-  addSectionLabel(D, dwarf::DW_AT_stmt_list, LineTableStartSym,
-                  TLOF.getDwarfLineSection()->getBeginSymbol());
+  MCContext &Ctx = Asm->OutStreamer->getContext();
+  if (!Ctx.getAsmInfo()->requiredDwarfSectionSize() && 
isa<MCAsmStreamer>(Asm->OutStreamer))
+    addLabelPlusOffset(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym,
+                       
-dwarf::getUnitLengthFieldByteSize(Ctx.getDwarfFormat()));
+  else {
+    const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
+    addSectionLabel(D, dwarf::DW_AT_stmt_list, LineTableStartSym,
+                    TLOF.getDwarfLineSection()->getBeginSymbol());
+  }
 }
 
 void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,

diff  --git a/llvm/lib/MC/MCAsmInfoXCOFF.cpp b/llvm/lib/MC/MCAsmInfoXCOFF.cpp
index 2f8bc6a49bb7..a23a71b865b4 100644
--- a/llvm/lib/MC/MCAsmInfoXCOFF.cpp
+++ b/llvm/lib/MC/MCAsmInfoXCOFF.cpp
@@ -23,6 +23,8 @@ MCAsmInfoXCOFF::MCAsmInfoXCOFF() {
   PrivateLabelPrefix = "L..";
   SupportsQuotedNames = false;
   UseDotAlignForAlignment = true;
+  UsesDwarfFileAndLocDirectives = false;
+  DwarfSectionSizeRequired = false;
   if (UseLEB128Directives == cl::BOU_UNSET)
     HasLEB128Directives = false;
   ZeroDirective = "\t.space\t";

diff  --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index 77c5f409789a..5185e66dd171 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -1070,6 +1070,10 @@ Expected<unsigned> 
MCAsmStreamer::tryEmitDwarfFileDirective(
   if (NumFiles == Table.getMCDwarfFiles().size())
     return FileNo;
 
+  // Target doesn't support .loc/.file directives, return early.
+  if (!MAI->usesDwarfFileAndLocDirectives())
+    return FileNo;
+
   SmallString<128> Str;
   raw_svector_ostream OS1(Str);
   printDwarfFileDirective(FileNo, Directory, Filename, Checksum, Source,
@@ -1096,6 +1100,10 @@ void MCAsmStreamer::emitDwarfFile0Directive(StringRef 
Directory,
   getContext().setMCLineTableRootFile(CUID, Directory, Filename, Checksum,
                                       Source);
 
+  // Target doesn't support .loc/.file directives, return early.
+  if (!MAI->usesDwarfFileAndLocDirectives())
+    return;
+
   SmallString<128> Str;
   raw_svector_ostream OS1(Str);
   printDwarfFileDirective(0, Directory, Filename, Checksum, Source,
@@ -1111,6 +1119,20 @@ void MCAsmStreamer::emitDwarfLocDirective(unsigned 
FileNo, unsigned Line,
                                           unsigned Column, unsigned Flags,
                                           unsigned Isa, unsigned Discriminator,
                                           StringRef FileName) {
+  // If target doesn't support .loc/.file directive, we need to record the 
lines
+  // same way like we do in object mode.
+  if (!MAI->usesDwarfFileAndLocDirectives()) {
+    if (!getContext().getDwarfLocSeen())
+      // This is the very start of the file, set the initial line number to 1.
+      // We need this because in assembly mode, we will not record new line 
when
+      // we emit a new instruction. See MCAsmStreamer::emitInstruction.
+      this->MCStreamer::emitDwarfLocDirective(FileNo, 1, 0, 0, 0, 0, FileName);
+    MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
+    this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa,
+                                            Discriminator, FileName);
+    return;
+  }
+
   OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
   if (MAI->supportsExtendedDwarfLocDirective()) {
     if (Flags & DWARF2_FLAG_BASIC_BLOCK)
@@ -1845,6 +1867,13 @@ void MCAsmStreamer::finishImpl() {
   if (getContext().getGenDwarfForAssembly())
     MCGenDwarfInfo::Emit(this);
 
+  // Now it is time to emit debug line sections if target doesn't support .loc
+  // and .line directives.
+  if (!MAI->usesDwarfFileAndLocDirectives()) {
+    MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams());
+    return;
+  }
+
   // Emit the label for the line table, if requested - since the rest of the
   // line table will be defined by .loc/.file directives, and not emitted
   // directly, the label is the only work required here.
@@ -1858,6 +1887,43 @@ void MCAsmStreamer::finishImpl() {
   }
 }
 
+// Generate dwarf line sections for assembly mode without .loc/.file
+void MCAsmStreamer::emitDwarfAdvanceLineAddr(int64_t LineDelta,
+                                             const MCSymbol *LastLabel,
+                                             const MCSymbol *Label,
+                                             unsigned PointerSize) {
+  assert(!MAI->usesDwarfFileAndLocDirectives() &&
+         ".loc/.file don't need raw data in debug line section!");
+  if (!LastLabel) {
+    // set to CU beginning.
+    emitIntValue(dwarf::DW_LNS_extended_op, 1);
+    emitULEB128IntValue(PointerSize + 1);
+    emitIntValue(dwarf::DW_LNE_set_address, 1);
+    emitSymbolValue(Label, PointerSize);
+    return;
+  }
+
+  // INT64_MAX is a signal of the end of the section. Emit DW_LNE_end_sequence
+  // for the end of the section.
+  if (LineDelta == INT64_MAX) {
+    emitIntValue(0, 1);
+    emitULEB128IntValue(1);
+    emitIntValue(dwarf::DW_LNE_end_sequence, 1);
+    return;
+  }
+
+  // advance line.
+  emitIntValue(dwarf::DW_LNS_advance_line, 1);
+  emitSLEB128IntValue(LineDelta);
+  emitIntValue(dwarf::DW_LNS_copy, 1);
+
+  // set new address to new label.
+  emitIntValue(dwarf::DW_LNS_extended_op, 1);
+  emitULEB128IntValue(PointerSize + 1);
+  emitIntValue(dwarf::DW_LNE_set_address, 1);
+  emitSymbolValue(Label, PointerSize);
+}
+
 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
                                     std::unique_ptr<formatted_raw_ostream> OS,
                                     bool isVerboseAsm, bool useDwarfDirectory,

diff  --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 9dab8a6c0910..afbd4c27cd6c 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -669,7 +669,8 @@ MCSectionWasm *MCContext::getWasmSection(const Twine 
&Section, SectionKind Kind,
 MCSectionXCOFF *
 MCContext::getXCOFFSection(StringRef Section, XCOFF::StorageMappingClass SMC,
                            XCOFF::SymbolType Type, SectionKind Kind,
-                           bool MultiSymbolsAllowed, const char *BeginSymName) 
{
+                           bool MultiSymbolsAllowed, const char *BeginSymName,
+                           unsigned SecFlags) {
   // Do the lookup. If we have a hit, return it.
   auto IterBool = XCOFFUniquingMap.insert(
       std::make_pair(XCOFFSectionKey{Section.str(), SMC}, nullptr));
@@ -695,7 +696,7 @@ MCContext::getXCOFFSection(StringRef Section, 
XCOFF::StorageMappingClass SMC,
   // CachedName contains invalid character(s) such as '$' for an XCOFF symbol.
   MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate())
       MCSectionXCOFF(QualName->getUnqualifiedName(), SMC, Type, Kind, QualName,
-                     Begin, CachedName, MultiSymbolsAllowed);
+                     SecFlags, Begin, CachedName, MultiSymbolsAllowed);
   Entry.second = Result;
 
   auto *F = new MCDataFragment();

diff  --git a/llvm/lib/MC/MCDwarf.cpp b/llvm/lib/MC/MCDwarf.cpp
index 12a7d9b6e589..ecd01d5e38cf 100644
--- a/llvm/lib/MC/MCDwarf.cpp
+++ b/llvm/lib/MC/MCDwarf.cpp
@@ -24,6 +24,7 @@
 #include "llvm/MC/MCObjectFileInfo.h"
 #include "llvm/MC/MCObjectStreamer.h"
 #include "llvm/MC/MCRegisterInfo.h"
+#include "llvm/MC/MCAsmStreamer.h"
 #include "llvm/MC/MCSection.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSymbol.h"
@@ -105,7 +106,7 @@ static inline uint64_t ScaleAddrDelta(MCContext &Context, 
uint64_t AddrDelta) {
 // and if there is information from the last .loc directive that has yet to 
have
 // a line entry made for it is made.
 //
-void MCDwarfLineEntry::Make(MCObjectStreamer *MCOS, MCSection *Section) {
+void MCDwarfLineEntry::Make(MCStreamer *MCOS, MCSection *Section) {
   if (!MCOS->getContext().getDwarfLocSeen())
     return;
 
@@ -163,7 +164,7 @@ makeStartPlusIntExpr(MCContext &Ctx, const MCSymbol &Start, 
int IntVal) {
 // in the LineSection.
 //
 static inline void emitDwarfLineTable(
-    MCObjectStreamer *MCOS, MCSection *Section,
+    MCStreamer *MCOS, MCSection *Section,
     const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
   unsigned FileNum = 1;
   unsigned LastLine = 1;
@@ -173,6 +174,9 @@ static inline void emitDwarfLineTable(
   unsigned Discriminator = 0;
   MCSymbol *LastLabel = nullptr;
 
+  const MCAsmInfo *AsmInfo = MCOS->getContext().getAsmInfo();
+  unsigned PointerSize = AsmInfo->getCodePointerSize();
+
   // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
   for (const MCDwarfLineEntry &LineEntry : LineEntries) {
     int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
@@ -217,15 +221,24 @@ static inline void emitDwarfLineTable(
     // At this point we want to emit/create the sequence to encode the delta in
     // line numbers and the increment of the address from the previous Label
     // and the current Label.
-    const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
-    MCOS->emitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
-                                   asmInfo->getCodePointerSize());
+    MCOS->emitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label, PointerSize);
 
     Discriminator = 0;
     LastLine = LineEntry.getLine();
     LastLabel = Label;
   }
 
+  // Directly add DW_LNE_end_sequence if we generate debug line section for
+  // not .loc/.file and assembly mode.
+  if (!AsmInfo->usesDwarfFileAndLocDirectives() && isa<MCAsmStreamer>(MCOS)) {
+    MCSymbol *Sym = Section->getEndSymbol(MCOS->getContext());
+    if (!Sym->isInSection())
+      MCOS->emitLabel(Sym);
+
+    MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, Sym, PointerSize);
+    return;
+  }
+
   // Emit a DW_LNE_end_sequence for the end of the section.
   // Use the section end label to compute the address delta and use INT64_MAX
   // as the line delta which is the signal that this is actually a
@@ -237,16 +250,13 @@ static inline void emitDwarfLineTable(
   MCContext &Ctx = MCOS->getContext();
   MCOS->SwitchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
 
-  const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
-  MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
-                                 AsmInfo->getCodePointerSize());
+  MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd, 
PointerSize);
 }
 
 //
 // This emits the Dwarf file and the line tables.
 //
-void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS,
-                            MCDwarfLineTableParams Params) {
+void MCDwarfLineTable::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params) {
   MCContext &context = MCOS->getContext();
 
   auto &LineTables = context.getMCDwarfLineTables();
@@ -481,24 +491,32 @@ MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, 
MCDwarfLineTableParams Params,
       dwarf::getUnitLengthFieldByteSize(context.getDwarfFormat());
   unsigned OffsetSize = 
dwarf::getDwarfOffsetByteSize(context.getDwarfFormat());
 
-  if (context.getDwarfFormat() == dwarf::DWARF64)
-    // Emit DWARF64 mark.
-    MCOS->emitInt32(dwarf::DW_LENGTH_DWARF64);
-
-  // The length field does not include itself and, in case of the 64-bit DWARF
-  // format, the DWARF64 mark.
-  emitAbsValue(*MCOS,
-               makeEndMinusStartExpr(context, *LineStartSym, *LineEndSym,
-                                     UnitLengthBytes),
-               OffsetSize);
+  // Keep track of the bytes between the very start and where the header length
+  // comes out.
+  unsigned PreHeaderLengthBytes = 0;
+
+  // If the assembler on some targets will fill the dwarf unit length, we
+  // don't need to emit the length in compiler. For exmaple AIX assembler
+  // requires the assembly file without unit length in debug sections header.
+  if (!(!MCOS->getContext().getAsmInfo()->requiredDwarfSectionSize() &&
+        isa<MCAsmStreamer>(MCOS))) {
+    if (context.getDwarfFormat() == dwarf::DWARF64)
+      // Emit DWARF64 mark.
+      MCOS->emitInt32(dwarf::DW_LENGTH_DWARF64);
+
+    // The length field does not include itself and, in case of the 64-bit 
DWARF
+    // format, the DWARF64 mark.
+    emitAbsValue(*MCOS,
+                 makeEndMinusStartExpr(context, *LineStartSym, *LineEndSym,
+                                       UnitLengthBytes),
+                 OffsetSize);
+    PreHeaderLengthBytes += UnitLengthBytes;
+  }
 
   // Next 2 bytes is the Version.
   unsigned LineTableVersion = context.getDwarfVersion();
   MCOS->emitInt16(LineTableVersion);
-
-  // Keep track of the bytes between the very start and where the header length
-  // comes out.
-  unsigned PreHeaderLengthBytes = UnitLengthBytes + 2;
+  PreHeaderLengthBytes += 2;
 
   // In v5, we get address info next.
   if (LineTableVersion >= 5) {
@@ -547,8 +565,7 @@ MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, 
MCDwarfLineTableParams Params,
   return std::make_pair(LineStartSym, LineEndSym);
 }
 
-void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS,
-                              MCDwarfLineTableParams Params,
+void MCDwarfLineTable::EmitCU(MCStreamer *MCOS, MCDwarfLineTableParams Params,
                               Optional<MCDwarfLineStr> &LineStr) const {
   MCSymbol *LineEndSym = Header.Emit(MCOS, Params, LineStr).second;
 

diff  --git a/llvm/lib/MC/MCObjectFileInfo.cpp 
b/llvm/lib/MC/MCObjectFileInfo.cpp
index 398de873fe0b..95f492fe8b40 100644
--- a/llvm/lib/MC/MCObjectFileInfo.cpp
+++ b/llvm/lib/MC/MCObjectFileInfo.cpp
@@ -898,18 +898,60 @@ void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const 
Triple &T) {
   // DWARF sections for XCOFF are not csects. They are special STYP_DWARF
   // sections, and the individual DWARF sections are distinguished by their
   // section subtype.
-  // TODO: Populate the DWARF sections appropriately.
-  DwarfAbbrevSection = nullptr;   // SSUBTYP_DWABREV
-  DwarfInfoSection = nullptr;     // SSUBTYP_DWINFO
-  DwarfLineSection = nullptr;     // SSUBTYP_DWLINE
-  DwarfFrameSection = nullptr;    // SSUBTYP_DWFRAME
-  DwarfPubNamesSection = nullptr; // SSUBTYP_DWPBNMS
-  DwarfPubTypesSection = nullptr; // SSUBTYP_DWPBTYP
-  DwarfStrSection = nullptr;      // SSUBTYP_DWSTR
-  DwarfLocSection = nullptr;      // SSUBTYP_DWLOC
-  DwarfARangesSection = nullptr;  // SSUBTYP_DWARNGE
-  DwarfRangesSection = nullptr;   // SSUBTYP_DWRNGES
-  DwarfMacinfoSection = nullptr;  // SSUBTYP_DWMAC
+  DwarfAbbrevSection = Ctx->getXCOFFSection(
+      ".dwabrev", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwabrev",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWABREV);
+
+  DwarfInfoSection = Ctx->getXCOFFSection(
+      ".dwinfo", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwinfo",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWINFO);
+
+  DwarfLineSection = Ctx->getXCOFFSection(
+      ".dwline", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwline",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWLINE);
+
+  DwarfFrameSection = Ctx->getXCOFFSection(
+      ".dwframe", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwframe",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWFRAME);
+
+  DwarfPubNamesSection = Ctx->getXCOFFSection(
+      ".dwpbnms", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwpbnms",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWPBNMS);
+
+  DwarfPubTypesSection = Ctx->getXCOFFSection(
+      ".dwpbtyp", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwpbtyp",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWPBTYP);
+
+  DwarfStrSection = Ctx->getXCOFFSection(
+      ".dwstr", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwstr",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWSTR);
+
+  DwarfLocSection = Ctx->getXCOFFSection(
+      ".dwloc", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwloc",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWLOC);
+
+  DwarfARangesSection = Ctx->getXCOFFSection(
+      ".dwarnge", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwarnge",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWARNGE);
+
+  DwarfRangesSection = Ctx->getXCOFFSection(
+      ".dwrnges", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwrnges",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWRNGES);
+
+  DwarfMacinfoSection = Ctx->getXCOFFSection(
+      ".dwmac", XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD,
+      SectionKind::getMetadata(), /* MultiSymbolsAllowed*/ true, ".dwmac",
+      XCOFF::STYP_DWARF | XCOFF::SSUBTYP_DWMAC);
 }
 
 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, bool PIC,

diff  --git a/llvm/lib/MC/MCSectionXCOFF.cpp b/llvm/lib/MC/MCSectionXCOFF.cpp
index 17b7b60a04ab..a737ca4f9e70 100644
--- a/llvm/lib/MC/MCSectionXCOFF.cpp
+++ b/llvm/lib/MC/MCSectionXCOFF.cpp
@@ -9,7 +9,9 @@
 #include "llvm/MC/MCSectionXCOFF.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCExpr.h"
+#include "llvm/Support/Format.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/Debug.h"
 
 using namespace llvm;
 
@@ -70,6 +72,16 @@ void MCSectionXCOFF::PrintSwitchToSection(const MCAsmInfo 
&MAI, const Triple &T,
     return;
   }
 
+  // XCOFF debug sections.
+  if (getKind().isMetadata() && (getSecFlags() & (unsigned)XCOFF::STYP_DWARF)) 
{
+    OS << "\n\t.dwsect "
+       << format("0x%" PRIx32,
+                 (getSecFlags() & ~((unsigned)(XCOFF::STYP_DWARF))))
+       << '\n';
+    OS << MAI.getPrivateLabelPrefix() << getName() << ':' << '\n';
+    return;
+  }
+
   report_fatal_error("Printing for this SectionKind is unimplemented.");
 }
 

diff  --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp 
b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp
index 2b76af279ce6..55da48a7ce51 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp
@@ -63,4 +63,10 @@ PPCXCOFFMCAsmInfo::PPCXCOFFMCAsmInfo(bool Is64Bit, const 
Triple &T) {
 
   // A size of 8 is only supported by the assembler under 64-bit.
   Data64bitsDirective = Is64Bit ? "\t.vbyte\t8, " : nullptr;
+
+  // Debug Information
+  SupportsDebugInformation = true;
+
+  // Set up dwarf DIRECTIVES
+  MinInstAlignment = 4;
 }


        
_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to