Skip to content

Commit 05ba5c0

Browse files
committed
[MC] MCSectionSubPair: replace const MCExpr * with uint32_t
1 parent f73ac21 commit 05ba5c0

File tree

4 files changed

+67
-47
lines changed

4 files changed

+67
-47
lines changed

llvm/include/llvm/MC/MCStreamer.h

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct DefRangeRegisterHeader;
6363
struct DefRangeFramePointerRelHeader;
6464
}
6565

66-
using MCSectionSubPair = std::pair<MCSection *, const MCExpr *>;
66+
using MCSectionSubPair = std::pair<MCSection *, uint32_t>;
6767

6868
/// Target specific streamer interface. This is used so that targets can
6969
/// implement support for target specific assembly directives.
@@ -423,46 +423,26 @@ class MCStreamer {
423423
/// Calls changeSection as needed.
424424
///
425425
/// Returns false if the stack was empty.
426-
bool popSection() {
427-
if (SectionStack.size() <= 1)
428-
return false;
429-
auto I = SectionStack.end();
430-
--I;
431-
MCSectionSubPair OldSection = I->first;
432-
--I;
433-
MCSectionSubPair NewSection = I->first;
434-
435-
if (NewSection.first && OldSection != NewSection)
436-
changeSection(NewSection.first, NewSection.second);
437-
SectionStack.pop_back();
438-
return true;
439-
}
440-
441-
bool subSection(const MCExpr *Subsection) {
442-
if (SectionStack.empty())
443-
return false;
426+
bool popSection();
444427

445-
switchSection(SectionStack.back().first.first, Subsection);
446-
return true;
447-
}
428+
bool subSection(const MCExpr *Subsection);
448429

449430
/// Set the current section where code is being emitted to \p Section. This
450431
/// is required to update CurSection.
451432
///
452433
/// This corresponds to assembler directives like .section, .text, etc.
453434
virtual void switchSection(MCSection *Section,
454435
const MCExpr *Subsection = nullptr);
436+
void switchSection(MCSection *Section, uint32_t Subsec);
455437

456438
/// Set the current section where code is being emitted to \p Section.
457439
/// This is required to update CurSection. This version does not call
458440
/// changeSection.
459-
void switchSectionNoChange(MCSection *Section,
460-
const MCExpr *Subsection = nullptr) {
441+
void switchSectionNoChange(MCSection *Section) {
461442
assert(Section && "Cannot switch to a null section!");
462443
MCSectionSubPair curSection = SectionStack.back().first;
463444
SectionStack.back().second = curSection;
464-
if (MCSectionSubPair(Section, Subsection) != curSection)
465-
SectionStack.back().first = MCSectionSubPair(Section, Subsection);
445+
SectionStack.back().first = MCSectionSubPair(Section, 0);
466446
}
467447

468448
/// Create the default sections and set the initial one.

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,25 +291,16 @@ void MCObjectStreamer::changeSection(MCSection *Section,
291291
}
292292

293293
bool MCObjectStreamer::changeSectionImpl(MCSection *Section,
294-
const MCExpr *Subsection) {
294+
const MCExpr *SubsecExpr) {
295295
assert(Section && "Cannot switch to a null section!");
296296
getContext().clearDwarfLocSeen();
297297

298298
bool Created = getAssembler().registerSection(*Section);
299299

300-
int64_t IntSubsection = 0;
301-
if (Subsection &&
302-
!Subsection->evaluateAsAbsolute(IntSubsection, getAssemblerPtr())) {
303-
getContext().reportError(Subsection->getLoc(),
304-
"cannot evaluate subsection number");
305-
}
306-
if (!isUInt<31>(IntSubsection)) {
307-
getContext().reportError(Subsection->getLoc(),
308-
"subsection number " + Twine(IntSubsection) +
309-
" is not within [0,2147483647]");
310-
}
311-
312-
CurSubsectionIdx = unsigned(IntSubsection);
300+
int64_t Subsec = 0;
301+
if (SubsecExpr)
302+
(void)SubsecExpr->evaluateAsAbsolute(Subsec, getAssemblerPtr());
303+
CurSubsectionIdx = uint32_t(Subsec);
313304
Section->switchSubsection(CurSubsectionIdx);
314305
return Created;
315306
}

llvm/lib/MC/MCParser/ELFAsmParser.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
916916
}
917917

918918
bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
919-
const MCExpr *Subsection = nullptr;
919+
const MCExpr *Subsection = MCConstantExpr::create(0, getContext());
920920
if (getLexer().isNot(AsmToken::EndOfStatement)) {
921921
if (getParser().parseExpression(Subsection))
922922
return true;
@@ -927,8 +927,7 @@ bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
927927

928928
Lex();
929929

930-
getStreamer().subSection(Subsection);
931-
return false;
930+
return getStreamer().subSection(Subsection);
932931
}
933932

934933
bool ELFAsmParser::ParseDirectiveCGProfile(StringRef S, SMLoc Loc) {

llvm/lib/MC/MCStreamer.cpp

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,20 +1242,70 @@ void MCStreamer::emitBundleLock(bool AlignToEnd) {}
12421242
void MCStreamer::finishImpl() {}
12431243
void MCStreamer::emitBundleUnlock() {}
12441244

1245-
void MCStreamer::switchSection(MCSection *Section, const MCExpr *Subsection) {
1245+
bool MCStreamer::popSection() {
1246+
if (SectionStack.size() <= 1)
1247+
return false;
1248+
auto I = SectionStack.end();
1249+
--I;
1250+
MCSectionSubPair OldSec = I->first;
1251+
--I;
1252+
MCSectionSubPair NewSec = I->first;
1253+
1254+
if (NewSec.first && OldSec != NewSec)
1255+
changeSection(NewSec.first, NewSec.second ? MCConstantExpr::create(
1256+
NewSec.second, getContext())
1257+
: nullptr);
1258+
SectionStack.pop_back();
1259+
return true;
1260+
}
1261+
1262+
bool MCStreamer::subSection(const MCExpr *SubsecExpr) {
1263+
if (SectionStack.empty())
1264+
return true;
1265+
1266+
int64_t Subsec;
1267+
if (!SubsecExpr->evaluateAsAbsolute(Subsec, getAssemblerPtr())) {
1268+
getContext().reportError(SubsecExpr->getLoc(),
1269+
"cannot evaluate subsection number");
1270+
return true;
1271+
}
1272+
if (!isUInt<31>(Subsec)) {
1273+
getContext().reportError(SubsecExpr->getLoc(),
1274+
"subsection number " + Twine(Subsec) +
1275+
" is not within [0,2147483647]");
1276+
return true;
1277+
}
1278+
1279+
MCSectionSubPair CurPair = SectionStack.back().first;
1280+
SectionStack.back().second = CurPair;
1281+
SectionStack.back().first.second = Subsec;
1282+
changeSection(CurPair.first, SubsecExpr);
1283+
return false;
1284+
}
1285+
1286+
void MCStreamer::switchSection(MCSection *Section, const MCExpr *SubsecExpr) {
12461287
assert(Section && "Cannot switch to a null section!");
12471288
MCSectionSubPair curSection = SectionStack.back().first;
12481289
SectionStack.back().second = curSection;
1249-
if (MCSectionSubPair(Section, Subsection) != curSection) {
1250-
changeSection(Section, Subsection);
1251-
SectionStack.back().first = MCSectionSubPair(Section, Subsection);
1290+
uint32_t Subsec = 0;
1291+
if (SubsecExpr)
1292+
Subsec = cast<MCConstantExpr>(SubsecExpr)->getValue();
1293+
if (MCSectionSubPair(Section, Subsec) != curSection) {
1294+
changeSection(Section, SubsecExpr);
1295+
SectionStack.back().first = MCSectionSubPair(Section, Subsec);
12521296
assert(!Section->hasEnded() && "Section already ended");
12531297
MCSymbol *Sym = Section->getBeginSymbol();
12541298
if (Sym && !Sym->isInSection())
12551299
emitLabel(Sym);
12561300
}
12571301
}
12581302

1303+
void MCStreamer::switchSection(MCSection *Section, uint32_t Subsec) {
1304+
switchSection(Section, Subsec == 0
1305+
? nullptr
1306+
: MCConstantExpr::create(Subsec, getContext()));
1307+
}
1308+
12591309
MCSymbol *MCStreamer::endSection(MCSection *Section) {
12601310
// TODO: keep track of the last subsection so that this symbol appears in the
12611311
// correct place.

0 commit comments

Comments
 (0)