diff options
author | Fangrui Song <i@maskray.me> | 2024-06-22 19:44:52 -0700 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2024-06-22 19:44:52 -0700 |
commit | 05ba5c0648ae5e80d5afce270495bf3b1eef9af4 (patch) | |
tree | 576d79ebb285cacacc78dbcaef8e63f01b381b4b | |
parent | f73ac218a666e2017565f2210b47332ddcf55f00 (diff) | |
download | llvm-05ba5c0648ae5e80d5afce270495bf3b1eef9af4.zip llvm-05ba5c0648ae5e80d5afce270495bf3b1eef9af4.tar.gz llvm-05ba5c0648ae5e80d5afce270495bf3b1eef9af4.tar.bz2 |
[MC] MCSectionSubPair: replace const MCExpr * with uint32_t
-rw-r--r-- | llvm/include/llvm/MC/MCStreamer.h | 32 | ||||
-rw-r--r-- | llvm/lib/MC/MCObjectStreamer.cpp | 19 | ||||
-rw-r--r-- | llvm/lib/MC/MCParser/ELFAsmParser.cpp | 5 | ||||
-rw-r--r-- | llvm/lib/MC/MCStreamer.cpp | 58 |
4 files changed, 67 insertions, 47 deletions
diff --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h index b7468cf..7faa077 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 @@ public: /// 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 @@ public: /// 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 7550651..6257ad2 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 d4c4bcb..f1c36b2 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 680b056..4b86bc2 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. |