[MC] Replace MCSection*::getName() with MCSection::getName(). NFC
authorFangrui Song <maskray@google.com>
Wed, 15 Apr 2020 22:49:05 +0000 (15:49 -0700)
committerFangrui Song <maskray@google.com>
Thu, 16 Apr 2020 01:35:27 +0000 (18:35 -0700)
I plan to use MCSection::getName() in D78138. Having the function in the base class is also convenient for debugging.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D78251

13 files changed:
llvm/include/llvm/MC/MCSection.h
llvm/include/llvm/MC/MCSectionCOFF.h
llvm/include/llvm/MC/MCSectionELF.h
llvm/include/llvm/MC/MCSectionMachO.h
llvm/include/llvm/MC/MCSectionWasm.h
llvm/include/llvm/MC/MCSectionXCOFF.h
llvm/lib/MC/MCAssembler.cpp
llvm/lib/MC/MCContext.cpp
llvm/lib/MC/MCSection.cpp
llvm/lib/MC/MCSectionCOFF.cpp
llvm/lib/MC/MCSectionELF.cpp
llvm/lib/MC/MCSectionMachO.cpp
llvm/lib/MC/MCSectionWasm.cpp

index 14283ea..d5e1f3d 100644 (file)
@@ -100,16 +100,19 @@ private:
   SmallVector<PendingLabel, 2> PendingLabels;
 
 protected:
+  // TODO Make Name private when possible.
+  StringRef Name;
   SectionVariant Variant;
   SectionKind Kind;
 
-  MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
+  MCSection(SectionVariant V, StringRef Name, SectionKind K, MCSymbol *Begin);
   ~MCSection();
 
 public:
   MCSection(const MCSection &) = delete;
   MCSection &operator=(const MCSection &) = delete;
 
+  StringRef getName() const { return Name; }
   SectionKind getKind() const { return Kind; }
 
   SectionVariant getVariant() const { return Variant; }
index b1013ee..f52983e 100644 (file)
@@ -24,9 +24,6 @@ class MCSymbol;
 
 /// This represents a section on Windows
 class MCSectionCOFF final : public MCSection {
-  // The memory for this string is stored in the same MCContext as *this.
-  StringRef SectionName;
-
   // FIXME: The following fields should not be mutable, but are for now so the
   // asm parser can honor the .linkonce directive.
 
@@ -51,12 +48,12 @@ class MCSectionCOFF final : public MCSection {
 
 private:
   friend class MCContext;
-  MCSectionCOFF(StringRef Section, unsigned Characteristics,
+  // The storage of Name is owned by MCContext's COFFUniquingMap.
+  MCSectionCOFF(StringRef Name, unsigned Characteristics,
                 MCSymbol *COMDATSymbol, int Selection, SectionKind K,
                 MCSymbol *Begin)
-      : MCSection(SV_COFF, K, Begin), SectionName(Section),
-        Characteristics(Characteristics), COMDATSymbol(COMDATSymbol),
-        Selection(Selection) {
+      : MCSection(SV_COFF, Name, K, Begin), Characteristics(Characteristics),
+        COMDATSymbol(COMDATSymbol), Selection(Selection) {
     assert((Characteristics & 0x00F00000) == 0 &&
            "alignment must not be set upon section creation");
   }
@@ -66,7 +63,6 @@ public:
   /// section name
   bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const;
 
-  StringRef getName() const { return SectionName; }
   unsigned getCharacteristics() const { return Characteristics; }
   MCSymbol *getCOMDATSymbol() const { return COMDATSymbol; }
   int getSelection() const { return Selection; }
index 6f02ea7..466fe47 100644 (file)
@@ -25,10 +25,6 @@ class MCSymbol;
 /// This represents a section on linux, lots of unix variants and some bare
 /// metal systems.
 class MCSectionELF final : public MCSection {
-  /// This is the name of the section.  The referenced memory is owned by
-  /// TargetLoweringObjectFileELF's ELFUniqueMap.
-  StringRef SectionName;
-
   /// This is the sh_type field of a section, drawn from the enums below.
   unsigned Type;
 
@@ -51,24 +47,26 @@ class MCSectionELF final : public MCSection {
 private:
   friend class MCContext;
 
-  MCSectionELF(StringRef Section, unsigned type, unsigned flags, SectionKind K,
+  // The storage of Name is owned by MCContext's ELFUniquingMap.
+  MCSectionELF(StringRef Name, unsigned type, unsigned flags, SectionKind K,
                unsigned entrySize, const MCSymbolELF *group, unsigned UniqueID,
                MCSymbol *Begin, const MCSymbolELF *LinkedToSym)
-      : MCSection(SV_ELF, K, Begin), SectionName(Section), Type(type),
-        Flags(flags), UniqueID(UniqueID), EntrySize(entrySize), Group(group),
+      : MCSection(SV_ELF, Name, K, Begin), Type(type), Flags(flags),
+        UniqueID(UniqueID), EntrySize(entrySize), Group(group),
         LinkedToSym(LinkedToSym) {
     if (Group)
       Group->setIsSignature();
   }
 
-  void setSectionName(StringRef Name) { SectionName = Name; }
+  // TODO Delete after we stop supporting generation of GNU-style .zdebug_*
+  // sections.
+  void setSectionName(StringRef Name) { this->Name = Name; }
 
 public:
   /// Decides whether a '.section' directive should be printed before the
   /// section name
   bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const;
 
-  StringRef getName() const { return SectionName; }
   unsigned getType() const { return Type; }
   unsigned getFlags() const { return Flags; }
   unsigned getEntrySize() const { return EntrySize; }
index fa2168a..b675585 100644 (file)
@@ -23,7 +23,6 @@ namespace llvm {
 /// system, these are also described in /usr/include/mach-o/loader.h.
 class MCSectionMachO final : public MCSection {
   char SegmentName[16];  // Not necessarily null terminated!
-  char SectionName[16];  // Not necessarily null terminated!
 
   /// This is the SECTION_TYPE and SECTION_ATTRIBUTES field of a section, drawn
   /// from the enums below.
@@ -44,12 +43,6 @@ public:
       return StringRef(SegmentName, 16);
     return StringRef(SegmentName);
   }
-  StringRef getName() const {
-    // SectionName is not necessarily null terminated!
-    if (SectionName[15])
-      return StringRef(SectionName, 16);
-    return StringRef(SectionName);
-  }
 
   unsigned getTypeAndAttributes() const { return TypeAndAttributes; }
   unsigned getStubSize() const { return Reserved2; }
index 0598fc8..2cf64de 100644 (file)
@@ -25,10 +25,6 @@ class MCSymbol;
 
 /// This represents a section on wasm.
 class MCSectionWasm final : public MCSection {
-  /// This is the name of the section.  The referenced memory is owned by
-  /// TargetLoweringObjectFileWasm's WasmUniqueMap.
-  StringRef SectionName;
-
   unsigned UniqueID;
 
   const MCSymbolWasm *Group;
@@ -45,18 +41,17 @@ class MCSectionWasm final : public MCSection {
   // Whether this data segment is passive
   bool IsPassive = false;
 
+  // The storage of Name is owned by MCContext's WasmUniquingMap.
   friend class MCContext;
-  MCSectionWasm(StringRef Section, SectionKind K, const MCSymbolWasm *group,
+  MCSectionWasm(StringRef Name, SectionKind K, const MCSymbolWasm *group,
                 unsigned UniqueID, MCSymbol *Begin)
-      : MCSection(SV_Wasm, K, Begin), SectionName(Section), UniqueID(UniqueID),
-        Group(group) {}
+      : MCSection(SV_Wasm, Name, K, Begin), UniqueID(UniqueID), Group(group) {}
 
 public:
   /// Decides whether a '.section' directive should be printed before the
   /// section name
   bool shouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const;
 
-  StringRef getName() const { return SectionName; }
   const MCSymbolWasm *getGroup() const { return Group; }
 
   void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
index 9baa053..1ffb144 100644 (file)
@@ -33,17 +33,16 @@ namespace llvm {
 class MCSectionXCOFF final : public MCSection {
   friend class MCContext;
 
-  StringRef Name;
   XCOFF::StorageMappingClass MappingClass;
   XCOFF::SymbolType Type;
   XCOFF::StorageClass StorageClass;
   MCSymbolXCOFF *const QualName;
 
-  MCSectionXCOFF(StringRef Section, XCOFF::StorageMappingClass SMC,
+  MCSectionXCOFF(StringRef Name, XCOFF::StorageMappingClass SMC,
                  XCOFF::SymbolType ST, XCOFF::StorageClass SC, SectionKind K,
                  MCSymbolXCOFF *QualName, MCSymbol *Begin)
-      : MCSection(SV_XCOFF, K, Begin), Name(Section), MappingClass(SMC),
-        Type(ST), StorageClass(SC), QualName(QualName) {
+      : MCSection(SV_XCOFF, Name, K, Begin), MappingClass(SMC), Type(ST),
+        StorageClass(SC), QualName(QualName) {
     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.");
@@ -58,7 +57,6 @@ public:
     return S->getVariant() == SV_XCOFF;
   }
 
-  StringRef getName() const { return Name; }
   XCOFF::StorageMappingClass getMappingClass() const { return MappingClass; }
   XCOFF::StorageClass getStorageClass() const { return StorageClass; }
   XCOFF::SymbolType getCSectType() const { return Type; }
index d03fc15..0d28827 100644 (file)
@@ -686,11 +686,8 @@ void MCAssembler::writeSectionData(raw_ostream &OS, const MCSection *Sec,
           report_fatal_error("cannot have fixups in virtual section!");
         for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
           if (DF.getContents()[i]) {
-            if (auto *ELFSec = dyn_cast<const MCSectionELF>(Sec))
-              report_fatal_error("non-zero initializer found in section '" +
-                                 ELFSec->getName() + "'");
-            else
-              report_fatal_error("non-zero initializer found in virtual section");
+            report_fatal_error("non-zero initializer found in section '" +
+                               Sec->getName() + "'");
           }
         break;
       }
index ae78038..f68320f 100644 (file)
@@ -302,23 +302,25 @@ MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
   // diagnosed by the client as an error.
 
   // Form the name to look up.
-  SmallString<64> Name;
-  Name += Segment;
-  Name.push_back(',');
-  Name += Section;
+  assert(Section.size() <= 16 && "section name is too long");
+  assert(!memchr(Section.data(), '\0', Section.size()) &&
+         "section name cannot contain NUL");
 
   // Do the lookup, if we have a hit, return it.
-  MCSectionMachO *&Entry = MachOUniquingMap[Name];
-  if (Entry)
-    return Entry;
+  auto R = MachOUniquingMap.try_emplace((Segment + Twine(',') + Section).str());
+  if (!R.second)
+    return R.first->second;
 
   MCSymbol *Begin = nullptr;
   if (BeginSymName)
     Begin = createTempSymbol(BeginSymName, false);
 
   // Otherwise, return a new section.
-  return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
-             Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
+  StringRef Name = R.first->first();
+  R.first->second = new (MachOAllocator.Allocate())
+      MCSectionMachO(Segment, Name.substr(Name.size() - Section.size()),
+                     TypeAndAttributes, Reserved2, Kind, Begin);
+  return R.first->second;
 }
 
 void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
index 074534b..6697974 100644 (file)
 
 using namespace llvm;
 
-MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
+MCSection::MCSection(SectionVariant V, StringRef Name, SectionKind K,
+                     MCSymbol *Begin)
     : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
-      IsRegistered(false), DummyFragment(this), Variant(V), Kind(K) {}
+      IsRegistered(false), DummyFragment(this), Name(Name), Variant(V),
+      Kind(K) {}
 
 MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
   if (!End)
index b30b578..9108b0d 100644 (file)
@@ -38,7 +38,7 @@ void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
                                          raw_ostream &OS,
                                          const MCExpr *Subsection) const {
   // standard sections don't require the '.section'
-  if (ShouldOmitSectionDirective(SectionName, MAI)) {
+  if (ShouldOmitSectionDirective(getName(), MAI)) {
     OS << '\t' << getName() << '\n';
     return;
   }
@@ -61,7 +61,7 @@ void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
   if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
     OS << 's';
   if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) &&
-      !isImplicitlyDiscardable(SectionName))
+      !isImplicitlyDiscardable(getName()))
     OS << 'D';
   OS << '"';
 
index b6220fc..ba06737 100644 (file)
@@ -53,7 +53,7 @@ static void printName(raw_ostream &OS, StringRef Name) {
 void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
                                         raw_ostream &OS,
                                         const MCExpr *Subsection) const {
-  if (ShouldOmitSectionDirective(SectionName, MAI)) {
+  if (ShouldOmitSectionDirective(getName(), MAI)) {
     OS << '\t' << getName();
     if (Subsection) {
       OS << '\t';
index 621187f..21a63ce 100644 (file)
@@ -83,7 +83,7 @@ ENTRY("" /*FIXME*/,          S_ATTR_LOC_RELOC)
 MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
                                unsigned TAA, unsigned reserved2, SectionKind K,
                                MCSymbol *Begin)
-    : MCSection(SV_MachO, K, Begin), TypeAndAttributes(TAA),
+    : MCSection(SV_MachO, Section, K, Begin), TypeAndAttributes(TAA),
       Reserved2(reserved2) {
   assert(Segment.size() <= 16 && Section.size() <= 16 &&
          "Segment or section string too long");
@@ -92,11 +92,6 @@ MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
       SegmentName[i] = Segment[i];
     else
       SegmentName[i] = 0;
-
-    if (i < Section.size())
-      SectionName[i] = Section[i];
-    else
-      SectionName[i] = 0;
   }
 }
 
index b851286..995b811 100644 (file)
@@ -48,7 +48,7 @@ void MCSectionWasm::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
                                          raw_ostream &OS,
                                          const MCExpr *Subsection) const {
 
-  if (shouldOmitSectionDirective(SectionName, MAI)) {
+  if (shouldOmitSectionDirective(getName(), MAI)) {
     OS << '\t' << getName();
     if (Subsection) {
       OS << '\t';