Skip to content

Commit 76aba5d

Browse files
authored
[MC] Add parseSymbol() helper (NFC) (#158106)
This combines parseIdentifier() + getOrCreateSymbol(). This should make it a bit easier if we want to change the parseIdentifier() API.
1 parent 4bb250d commit 76aba5d

File tree

9 files changed

+97
-156
lines changed

9 files changed

+97
-156
lines changed

llvm/include/llvm/MC/MCParser/MCAsmParser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ class LLVM_ABI MCAsmParser {
279279
/// Res to the identifier contents.
280280
virtual bool parseIdentifier(StringRef &Res) = 0;
281281

282+
/// Parse identifier and get or create symbol for it.
283+
bool parseSymbol(MCSymbol *&Res);
284+
282285
/// Parse up to the end of statement and return the contents from the
283286
/// current token until the end of the statement; the current token on exit
284287
/// will be either the EndOfStatement or EOF.

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3876,20 +3876,15 @@ bool AsmParser::parseDirectiveCVLoc() {
38763876
/// ::= .cv_linetable FunctionId, FnStart, FnEnd
38773877
bool AsmParser::parseDirectiveCVLinetable() {
38783878
int64_t FunctionId;
3879-
StringRef FnStartName, FnEndName;
3879+
MCSymbol *FnStartSym, *FnEndSym;
38803880
SMLoc Loc = getTok().getLoc();
38813881
if (parseCVFunctionId(FunctionId, ".cv_linetable") || parseComma() ||
38823882
parseTokenLoc(Loc) ||
3883-
check(parseIdentifier(FnStartName), Loc,
3884-
"expected identifier in directive") ||
3883+
check(parseSymbol(FnStartSym), Loc, "expected identifier in directive") ||
38853884
parseComma() || parseTokenLoc(Loc) ||
3886-
check(parseIdentifier(FnEndName), Loc,
3887-
"expected identifier in directive"))
3885+
check(parseSymbol(FnEndSym), Loc, "expected identifier in directive"))
38883886
return true;
38893887

3890-
MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3891-
MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3892-
38933888
getStreamer().emitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
38943889
return false;
38953890
}
@@ -3898,7 +3893,7 @@ bool AsmParser::parseDirectiveCVLinetable() {
38983893
/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
38993894
bool AsmParser::parseDirectiveCVInlineLinetable() {
39003895
int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
3901-
StringRef FnStartName, FnEndName;
3896+
MCSymbol *FnStartSym, *FnEndSym;
39023897
SMLoc Loc = getTok().getLoc();
39033898
if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
39043899
parseTokenLoc(Loc) ||
@@ -3908,16 +3903,14 @@ bool AsmParser::parseDirectiveCVInlineLinetable() {
39083903
parseIntToken(SourceLineNum, "expected SourceLineNum") ||
39093904
check(SourceLineNum < 0, Loc, "Line number less than zero") ||
39103905
parseTokenLoc(Loc) ||
3911-
check(parseIdentifier(FnStartName), Loc, "expected identifier") ||
3906+
check(parseSymbol(FnStartSym), Loc, "expected identifier") ||
39123907
parseTokenLoc(Loc) ||
3913-
check(parseIdentifier(FnEndName), Loc, "expected identifier"))
3908+
check(parseSymbol(FnEndSym), Loc, "expected identifier"))
39143909
return true;
39153910

39163911
if (parseEOL())
39173912
return true;
39183913

3919-
MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3920-
MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
39213914
getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
39223915
SourceLineNum, FnStartSym,
39233916
FnEndSym);
@@ -3938,16 +3931,14 @@ bool AsmParser::parseDirectiveCVDefRange() {
39383931
std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
39393932
while (getLexer().is(AsmToken::Identifier)) {
39403933
Loc = getLexer().getLoc();
3941-
StringRef GapStartName;
3942-
if (parseIdentifier(GapStartName))
3934+
MCSymbol *GapStartSym;
3935+
if (parseSymbol(GapStartSym))
39433936
return Error(Loc, "expected identifier in directive");
3944-
MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
39453937

39463938
Loc = getLexer().getLoc();
3947-
StringRef GapEndName;
3948-
if (parseIdentifier(GapEndName))
3939+
MCSymbol *GapEndSym;
3940+
if (parseSymbol(GapEndSym))
39493941
return Error(Loc, "expected identifier in directive");
3950-
MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
39513942

39523943
Ranges.push_back({GapStartSym, GapEndSym});
39533944
}
@@ -4084,12 +4075,11 @@ bool AsmParser::parseDirectiveCVFileChecksumOffset() {
40844075
/// ::= .cv_fpo_data procsym
40854076
bool AsmParser::parseDirectiveCVFPOData() {
40864077
SMLoc DirLoc = getLexer().getLoc();
4087-
StringRef ProcName;
4088-
if (parseIdentifier(ProcName))
4078+
MCSymbol *ProcSym;
4079+
if (parseSymbol(ProcSym))
40894080
return TokError("expected symbol name");
40904081
if (parseEOL())
40914082
return true;
4092-
MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
40934083
getStreamer().emitCVFPOData(ProcSym, DirLoc);
40944084
return false;
40954085
}
@@ -4311,15 +4301,12 @@ bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
43114301
if (Encoding == dwarf::DW_EH_PE_omit)
43124302
return false;
43134303

4314-
StringRef Name;
4304+
MCSymbol *Sym;
43154305
if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
43164306
parseComma() ||
4317-
check(parseIdentifier(Name), "expected identifier in directive") ||
4318-
parseEOL())
4307+
check(parseSymbol(Sym), "expected identifier in directive") || parseEOL())
43194308
return true;
43204309

4321-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4322-
43234310
if (IsPersonality)
43244311
getStreamer().emitCFIPersonality(Sym, Encoding);
43254312
else
@@ -4920,13 +4907,10 @@ bool AsmParser::parseDirectiveComm(bool IsLocal) {
49204907
return true;
49214908

49224909
SMLoc IDLoc = getLexer().getLoc();
4923-
StringRef Name;
4924-
if (parseIdentifier(Name))
4910+
MCSymbol *Sym;
4911+
if (parseSymbol(Sym))
49254912
return TokError("expected identifier in directive");
49264913

4927-
// Handle the identifier as the key symbol.
4928-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4929-
49304914
if (parseComma())
49314915
return true;
49324916

@@ -5756,10 +5740,9 @@ bool AsmParser::parseDirectiveAddrsig() {
57565740
}
57575741

57585742
bool AsmParser::parseDirectiveAddrsigSym() {
5759-
StringRef Name;
5760-
if (check(parseIdentifier(Name), "expected identifier") || parseEOL())
5743+
MCSymbol *Sym;
5744+
if (check(parseSymbol(Sym), "expected identifier") || parseEOL())
57615745
return true;
5762-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
57635746
getStreamer().emitAddrsigSym(Sym);
57645747
return false;
57655748
}

llvm/lib/MC/MCParser/COFFAsmParser.cpp

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,11 @@ bool COFFAsmParser::parseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
293293
assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
294294
if (getLexer().isNot(AsmToken::EndOfStatement)) {
295295
while (true) {
296-
StringRef Name;
296+
MCSymbol *Sym;
297297

298-
if (getParser().parseIdentifier(Name))
298+
if (getParser().parseSymbol(Sym))
299299
return TokError("expected identifier in directive");
300300

301-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
302-
303301
getStreamer().emitSymbolAttribute(Sym, Attr);
304302

305303
if (getLexer().is(AsmToken::EndOfStatement))
@@ -450,13 +448,11 @@ bool COFFAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
450448
}
451449

452450
bool COFFAsmParser::parseDirectiveDef(StringRef, SMLoc) {
453-
StringRef SymbolName;
451+
MCSymbol *Sym;
454452

455-
if (getParser().parseIdentifier(SymbolName))
453+
if (getParser().parseSymbol(Sym))
456454
return TokError("expected identifier in directive");
457455

458-
MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
459-
460456
getStreamer().beginCOFFSymbolDef(Sym);
461457

462458
Lex();
@@ -496,8 +492,8 @@ bool COFFAsmParser::parseDirectiveEndef(StringRef, SMLoc) {
496492
}
497493

498494
bool COFFAsmParser::parseDirectiveSecRel32(StringRef, SMLoc) {
499-
StringRef SymbolID;
500-
if (getParser().parseIdentifier(SymbolID))
495+
MCSymbol *Symbol;
496+
if (getParser().parseSymbol(Symbol))
501497
return TokError("expected identifier in directive");
502498

503499
int64_t Offset = 0;
@@ -517,17 +513,15 @@ bool COFFAsmParser::parseDirectiveSecRel32(StringRef, SMLoc) {
517513
"invalid '.secrel32' directive offset, can't be less "
518514
"than zero or greater than std::numeric_limits<uint32_t>::max()");
519515

520-
MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
521-
522516
Lex();
523517
getStreamer().emitCOFFSecRel32(Symbol, Offset);
524518
return false;
525519
}
526520

527521
bool COFFAsmParser::parseDirectiveRVA(StringRef, SMLoc) {
528522
auto parseOp = [&]() -> bool {
529-
StringRef SymbolID;
530-
if (getParser().parseIdentifier(SymbolID))
523+
MCSymbol *Symbol;
524+
if (getParser().parseSymbol(Symbol))
531525
return TokError("expected identifier in directive");
532526

533527
int64_t Offset = 0;
@@ -544,8 +538,6 @@ bool COFFAsmParser::parseDirectiveRVA(StringRef, SMLoc) {
544538
"than -2147483648 or greater than "
545539
"2147483647");
546540

547-
MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
548-
549541
getStreamer().emitCOFFImgRel32(Symbol, Offset);
550542
return false;
551543
};
@@ -556,75 +548,65 @@ bool COFFAsmParser::parseDirectiveRVA(StringRef, SMLoc) {
556548
}
557549

558550
bool COFFAsmParser::parseDirectiveSafeSEH(StringRef, SMLoc) {
559-
StringRef SymbolID;
560-
if (getParser().parseIdentifier(SymbolID))
551+
MCSymbol *Symbol;
552+
if (getParser().parseSymbol(Symbol))
561553
return TokError("expected identifier in directive");
562554

563555
if (getLexer().isNot(AsmToken::EndOfStatement))
564556
return TokError("unexpected token in directive");
565557

566-
MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
567-
568558
Lex();
569559
getStreamer().emitCOFFSafeSEH(Symbol);
570560
return false;
571561
}
572562

573563
bool COFFAsmParser::parseDirectiveSecIdx(StringRef, SMLoc) {
574-
StringRef SymbolID;
575-
if (getParser().parseIdentifier(SymbolID))
564+
MCSymbol *Symbol;
565+
if (getParser().parseSymbol(Symbol))
576566
return TokError("expected identifier in directive");
577567

578568
if (getLexer().isNot(AsmToken::EndOfStatement))
579569
return TokError("unexpected token in directive");
580570

581-
MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
582-
583571
Lex();
584572
getStreamer().emitCOFFSectionIndex(Symbol);
585573
return false;
586574
}
587575

588576
bool COFFAsmParser::parseDirectiveSymIdx(StringRef, SMLoc) {
589-
StringRef SymbolID;
590-
if (getParser().parseIdentifier(SymbolID))
577+
MCSymbol *Symbol;
578+
if (getParser().parseSymbol(Symbol))
591579
return TokError("expected identifier in directive");
592580

593581
if (getLexer().isNot(AsmToken::EndOfStatement))
594582
return TokError("unexpected token in directive");
595583

596-
MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
597-
598584
Lex();
599585
getStreamer().emitCOFFSymbolIndex(Symbol);
600586
return false;
601587
}
602588

603589
bool COFFAsmParser::parseDirectiveSecNum(StringRef, SMLoc) {
604-
StringRef SymbolID;
605-
if (getParser().parseIdentifier(SymbolID))
590+
MCSymbol *Symbol;
591+
if (getParser().parseSymbol(Symbol))
606592
return TokError("expected identifier in directive");
607593

608594
if (getLexer().isNot(AsmToken::EndOfStatement))
609595
return TokError("unexpected token in directive");
610596

611-
MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
612-
613597
Lex();
614598
getStreamer().emitCOFFSecNumber(Symbol);
615599
return false;
616600
}
617601

618602
bool COFFAsmParser::parseDirectiveSecOffset(StringRef, SMLoc) {
619-
StringRef SymbolID;
620-
if (getParser().parseIdentifier(SymbolID))
603+
MCSymbol *Symbol;
604+
if (getParser().parseSymbol(Symbol))
621605
return TokError("expected identifier in directive");
622606

623607
if (getLexer().isNot(AsmToken::EndOfStatement))
624608
return TokError("unexpected token in directive");
625609

626-
MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
627-
628610
Lex();
629611
getStreamer().emitCOFFSecOffset(Symbol);
630612
return false;
@@ -679,15 +661,13 @@ bool COFFAsmParser::parseDirectiveLinkOnce(StringRef, SMLoc Loc) {
679661
}
680662

681663
bool COFFAsmParser::parseSEHDirectiveStartProc(StringRef, SMLoc Loc) {
682-
StringRef SymbolID;
683-
if (getParser().parseIdentifier(SymbolID))
664+
MCSymbol *Symbol;
665+
if (getParser().parseSymbol(Symbol))
684666
return true;
685667

686668
if (getLexer().isNot(AsmToken::EndOfStatement))
687669
return TokError("unexpected token in directive");
688670

689-
MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
690-
691671
Lex();
692672
getStreamer().emitWinCFIStartProc(Symbol, Loc);
693673
return false;
@@ -718,8 +698,8 @@ bool COFFAsmParser::parseSEHDirectiveEndChained(StringRef, SMLoc Loc) {
718698
}
719699

720700
bool COFFAsmParser::parseSEHDirectiveHandler(StringRef, SMLoc Loc) {
721-
StringRef SymbolID;
722-
if (getParser().parseIdentifier(SymbolID))
701+
MCSymbol *handler;
702+
if (getParser().parseSymbol(handler))
723703
return true;
724704

725705
if (getLexer().isNot(AsmToken::Comma))
@@ -736,8 +716,6 @@ bool COFFAsmParser::parseSEHDirectiveHandler(StringRef, SMLoc Loc) {
736716
if (getLexer().isNot(AsmToken::EndOfStatement))
737717
return TokError("unexpected token in directive");
738718

739-
MCSymbol *handler = getContext().getOrCreateSymbol(SymbolID);
740-
741719
Lex();
742720
getStreamer().emitWinEHHandler(handler, unwind, except, Loc);
743721
return false;

llvm/lib/MC/MCParser/COFFMasmParser.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ bool COFFMasmParser::parseDirectiveProc(StringRef Directive, SMLoc Loc) {
443443
if (!getStreamer().getCurrentFragment())
444444
return Error(getTok().getLoc(), "expected section directive");
445445

446-
StringRef Label;
447-
if (getParser().parseIdentifier(Label))
446+
MCSymbol *Sym;
447+
if (getParser().parseSymbol(Sym))
448448
return Error(Loc, "expected identifier for procedure");
449449
if (getLexer().is(AsmToken::Identifier)) {
450450
StringRef nextVal = getTok().getString();
@@ -459,12 +459,12 @@ bool COFFMasmParser::parseDirectiveProc(StringRef Directive, SMLoc Loc) {
459459
nextLoc = getTok().getLoc();
460460
}
461461
}
462-
auto *Sym =
463-
static_cast<MCSymbolCOFF *>(getContext().getOrCreateSymbol(Label));
464462

465463
// Define symbol as simple external function
466-
Sym->setExternal(true);
467-
Sym->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION << COFF::SCT_COMPLEX_TYPE_SHIFT);
464+
auto *COFFSym = static_cast<MCSymbolCOFF *>(Sym);
465+
COFFSym->setExternal(true);
466+
COFFSym->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION
467+
<< COFF::SCT_COMPLEX_TYPE_SHIFT);
468468

469469
bool Framed = false;
470470
if (getLexer().is(AsmToken::Identifier) &&
@@ -475,7 +475,7 @@ bool COFFMasmParser::parseDirectiveProc(StringRef Directive, SMLoc Loc) {
475475
}
476476
getStreamer().emitLabel(Sym, Loc);
477477

478-
CurrentProcedures.push_back(Label);
478+
CurrentProcedures.push_back(Sym->getName());
479479
CurrentProceduresFramed.push_back(Framed);
480480
return false;
481481
}

0 commit comments

Comments
 (0)