[llvm] 05ba5c0 - [MC] MCSectionSubPair: replace const MCExpr * with uint32_t

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 22 19:44:57 PDT 2024


Author: Fangrui Song
Date: 2024-06-22T19:44:52-07:00
New Revision: 05ba5c0648ae5e80d5afce270495bf3b1eef9af4

URL: https://github.com/llvm/llvm-project/commit/05ba5c0648ae5e80d5afce270495bf3b1eef9af4
DIFF: https://github.com/llvm/llvm-project/commit/05ba5c0648ae5e80d5afce270495bf3b1eef9af4.diff

LOG: [MC] MCSectionSubPair: replace const MCExpr * with uint32_t

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCStreamer.h
    llvm/lib/MC/MCObjectStreamer.cpp
    llvm/lib/MC/MCParser/ELFAsmParser.cpp
    llvm/lib/MC/MCStreamer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h
index b7468cf70a664..7faa077060fff 100644
--- a/llvm/include/llvm/MC/MCStreamer.h
+++ b/llvm/include/llvm/MC/MCStreamer.h
@@ -63,7 +63,7 @@ struct DefRangeRegisterHeader;
 struct DefRangeFramePointerRelHeader;
 }
 
-using MCSectionSubPair = std::pair<MCSection *, const MCExpr *>;
+using MCSectionSubPair = std::pair<MCSection *, uint32_t>;
 
 /// Target specific streamer interface. This is used so that targets can
 /// implement support for target specific assembly directives.
@@ -423,28 +423,9 @@ class MCStreamer {
   /// Calls changeSection as needed.
   ///
   /// Returns false if the stack was empty.
-  bool popSection() {
-    if (SectionStack.size() <= 1)
-      return false;
-    auto I = SectionStack.end();
-    --I;
-    MCSectionSubPair OldSection = I->first;
-    --I;
-    MCSectionSubPair NewSection = I->first;
-
-    if (NewSection.first && OldSection != NewSection)
-      changeSection(NewSection.first, NewSection.second);
-    SectionStack.pop_back();
-    return true;
-  }
-
-  bool subSection(const MCExpr *Subsection) {
-    if (SectionStack.empty())
-      return false;
+  bool popSection();
 
-    switchSection(SectionStack.back().first.first, Subsection);
-    return true;
-  }
+  bool subSection(const MCExpr *Subsection);
 
   /// Set the current section where code is being emitted to \p Section.  This
   /// is required to update CurSection.
@@ -452,17 +433,16 @@ class MCStreamer {
   /// This corresponds to assembler directives like .section, .text, etc.
   virtual void switchSection(MCSection *Section,
                              const MCExpr *Subsection = nullptr);
+  void switchSection(MCSection *Section, uint32_t Subsec);
 
   /// Set the current section where code is being emitted to \p Section.
   /// This is required to update CurSection. This version does not call
   /// changeSection.
-  void switchSectionNoChange(MCSection *Section,
-                             const MCExpr *Subsection = nullptr) {
+  void switchSectionNoChange(MCSection *Section) {
     assert(Section && "Cannot switch to a null section!");
     MCSectionSubPair curSection = SectionStack.back().first;
     SectionStack.back().second = curSection;
-    if (MCSectionSubPair(Section, Subsection) != curSection)
-      SectionStack.back().first = MCSectionSubPair(Section, Subsection);
+    SectionStack.back().first = MCSectionSubPair(Section, 0);
   }
 
   /// Create the default sections and set the initial one.

diff  --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 75506514368a7..6257ad24b0458 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -291,25 +291,16 @@ void MCObjectStreamer::changeSection(MCSection *Section,
 }
 
 bool MCObjectStreamer::changeSectionImpl(MCSection *Section,
-                                         const MCExpr *Subsection) {
+                                         const MCExpr *SubsecExpr) {
   assert(Section && "Cannot switch to a null section!");
   getContext().clearDwarfLocSeen();
 
   bool Created = getAssembler().registerSection(*Section);
 
-  int64_t IntSubsection = 0;
-  if (Subsection &&
-      !Subsection->evaluateAsAbsolute(IntSubsection, getAssemblerPtr())) {
-    getContext().reportError(Subsection->getLoc(),
-                             "cannot evaluate subsection number");
-  }
-  if (!isUInt<31>(IntSubsection)) {
-    getContext().reportError(Subsection->getLoc(),
-                             "subsection number " + Twine(IntSubsection) +
-                                 " is not within [0,2147483647]");
-  }
-
-  CurSubsectionIdx = unsigned(IntSubsection);
+  int64_t Subsec = 0;
+  if (SubsecExpr)
+    (void)SubsecExpr->evaluateAsAbsolute(Subsec, getAssemblerPtr());
+  CurSubsectionIdx = uint32_t(Subsec);
   Section->switchSubsection(CurSubsectionIdx);
   return Created;
 }

diff  --git a/llvm/lib/MC/MCParser/ELFAsmParser.cpp b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
index d4c4bcb856488..f1c36b20f6eb1 100644
--- a/llvm/lib/MC/MCParser/ELFAsmParser.cpp
+++ b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
@@ -916,7 +916,7 @@ bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
 }
 
 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
-  const MCExpr *Subsection = nullptr;
+  const MCExpr *Subsection = MCConstantExpr::create(0, getContext());
   if (getLexer().isNot(AsmToken::EndOfStatement)) {
     if (getParser().parseExpression(Subsection))
      return true;
@@ -927,8 +927,7 @@ bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
 
   Lex();
 
-  getStreamer().subSection(Subsection);
-  return false;
+  return getStreamer().subSection(Subsection);
 }
 
 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef S, SMLoc Loc) {

diff  --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp
index 680b0567c1493..4b86bc2d70a2e 100644
--- a/llvm/lib/MC/MCStreamer.cpp
+++ b/llvm/lib/MC/MCStreamer.cpp
@@ -1242,13 +1242,57 @@ void MCStreamer::emitBundleLock(bool AlignToEnd) {}
 void MCStreamer::finishImpl() {}
 void MCStreamer::emitBundleUnlock() {}
 
-void MCStreamer::switchSection(MCSection *Section, const MCExpr *Subsection) {
+bool MCStreamer::popSection() {
+  if (SectionStack.size() <= 1)
+    return false;
+  auto I = SectionStack.end();
+  --I;
+  MCSectionSubPair OldSec = I->first;
+  --I;
+  MCSectionSubPair NewSec = I->first;
+
+  if (NewSec.first && OldSec != NewSec)
+    changeSection(NewSec.first, NewSec.second ? MCConstantExpr::create(
+                                                    NewSec.second, getContext())
+                                              : nullptr);
+  SectionStack.pop_back();
+  return true;
+}
+
+bool MCStreamer::subSection(const MCExpr *SubsecExpr) {
+  if (SectionStack.empty())
+    return true;
+
+  int64_t Subsec;
+  if (!SubsecExpr->evaluateAsAbsolute(Subsec, getAssemblerPtr())) {
+    getContext().reportError(SubsecExpr->getLoc(),
+                             "cannot evaluate subsection number");
+    return true;
+  }
+  if (!isUInt<31>(Subsec)) {
+    getContext().reportError(SubsecExpr->getLoc(),
+                             "subsection number " + Twine(Subsec) +
+                                 " is not within [0,2147483647]");
+    return true;
+  }
+
+  MCSectionSubPair CurPair = SectionStack.back().first;
+  SectionStack.back().second = CurPair;
+  SectionStack.back().first.second = Subsec;
+  changeSection(CurPair.first, SubsecExpr);
+  return false;
+}
+
+void MCStreamer::switchSection(MCSection *Section, const MCExpr *SubsecExpr) {
   assert(Section && "Cannot switch to a null section!");
   MCSectionSubPair curSection = SectionStack.back().first;
   SectionStack.back().second = curSection;
-  if (MCSectionSubPair(Section, Subsection) != curSection) {
-    changeSection(Section, Subsection);
-    SectionStack.back().first = MCSectionSubPair(Section, Subsection);
+  uint32_t Subsec = 0;
+  if (SubsecExpr)
+    Subsec = cast<MCConstantExpr>(SubsecExpr)->getValue();
+  if (MCSectionSubPair(Section, Subsec) != curSection) {
+    changeSection(Section, SubsecExpr);
+    SectionStack.back().first = MCSectionSubPair(Section, Subsec);
     assert(!Section->hasEnded() && "Section already ended");
     MCSymbol *Sym = Section->getBeginSymbol();
     if (Sym && !Sym->isInSection())
@@ -1256,6 +1300,12 @@ void MCStreamer::switchSection(MCSection *Section, const MCExpr *Subsection) {
   }
 }
 
+void MCStreamer::switchSection(MCSection *Section, uint32_t Subsec) {
+  switchSection(Section, Subsec == 0
+                             ? nullptr
+                             : MCConstantExpr::create(Subsec, getContext()));
+}
+
 MCSymbol *MCStreamer::endSection(MCSection *Section) {
   // TODO: keep track of the last subsection so that this symbol appears in the
   // correct place.


        


More information about the llvm-commits mailing list