Skip to content

Commit bc5e9a5

Browse files
nikictstellar
authored andcommitted
[MC] Add parseSymbol() helper (NFC) (llvm#158106)
This combines parseIdentifier() + getOrCreateSymbol(). This should make it a bit easier if we want to change the parseIdentifier() API. (cherry picked from commit 76aba5d)
1 parent e2e5eb2 commit bc5e9a5

File tree

9 files changed

+96
-153
lines changed

9 files changed

+96
-153
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
@@ -3897,20 +3897,15 @@ bool AsmParser::parseDirectiveCVLoc() {
38973897
/// ::= .cv_linetable FunctionId, FnStart, FnEnd
38983898
bool AsmParser::parseDirectiveCVLinetable() {
38993899
int64_t FunctionId;
3900-
StringRef FnStartName, FnEndName;
3900+
MCSymbol *FnStartSym, *FnEndSym;
39013901
SMLoc Loc = getTok().getLoc();
39023902
if (parseCVFunctionId(FunctionId, ".cv_linetable") || parseComma() ||
39033903
parseTokenLoc(Loc) ||
3904-
check(parseIdentifier(FnStartName), Loc,
3905-
"expected identifier in directive") ||
3904+
check(parseSymbol(FnStartSym), Loc, "expected identifier in directive") ||
39063905
parseComma() || parseTokenLoc(Loc) ||
3907-
check(parseIdentifier(FnEndName), Loc,
3908-
"expected identifier in directive"))
3906+
check(parseSymbol(FnEndSym), Loc, "expected identifier in directive"))
39093907
return true;
39103908

3911-
MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3912-
MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3913-
39143909
getStreamer().emitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
39153910
return false;
39163911
}
@@ -3919,7 +3914,7 @@ bool AsmParser::parseDirectiveCVLinetable() {
39193914
/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
39203915
bool AsmParser::parseDirectiveCVInlineLinetable() {
39213916
int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
3922-
StringRef FnStartName, FnEndName;
3917+
MCSymbol *FnStartSym, *FnEndSym;
39233918
SMLoc Loc = getTok().getLoc();
39243919
if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
39253920
parseTokenLoc(Loc) ||
@@ -3929,16 +3924,14 @@ bool AsmParser::parseDirectiveCVInlineLinetable() {
39293924
parseIntToken(SourceLineNum, "expected SourceLineNum") ||
39303925
check(SourceLineNum < 0, Loc, "Line number less than zero") ||
39313926
parseTokenLoc(Loc) ||
3932-
check(parseIdentifier(FnStartName), Loc, "expected identifier") ||
3927+
check(parseSymbol(FnStartSym), Loc, "expected identifier") ||
39333928
parseTokenLoc(Loc) ||
3934-
check(parseIdentifier(FnEndName), Loc, "expected identifier"))
3929+
check(parseSymbol(FnEndSym), Loc, "expected identifier"))
39353930
return true;
39363931

39373932
if (parseEOL())
39383933
return true;
39393934

3940-
MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3941-
MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
39423935
getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
39433936
SourceLineNum, FnStartSym,
39443937
FnEndSym);
@@ -3959,16 +3952,14 @@ bool AsmParser::parseDirectiveCVDefRange() {
39593952
std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
39603953
while (getLexer().is(AsmToken::Identifier)) {
39613954
Loc = getLexer().getLoc();
3962-
StringRef GapStartName;
3963-
if (parseIdentifier(GapStartName))
3955+
MCSymbol *GapStartSym;
3956+
if (parseSymbol(GapStartSym))
39643957
return Error(Loc, "expected identifier in directive");
3965-
MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
39663958

39673959
Loc = getLexer().getLoc();
3968-
StringRef GapEndName;
3969-
if (parseIdentifier(GapEndName))
3960+
MCSymbol *GapEndSym;
3961+
if (parseSymbol(GapEndSym))
39703962
return Error(Loc, "expected identifier in directive");
3971-
MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
39723963

39733964
Ranges.push_back({GapStartSym, GapEndSym});
39743965
}
@@ -4105,12 +4096,11 @@ bool AsmParser::parseDirectiveCVFileChecksumOffset() {
41054096
/// ::= .cv_fpo_data procsym
41064097
bool AsmParser::parseDirectiveCVFPOData() {
41074098
SMLoc DirLoc = getLexer().getLoc();
4108-
StringRef ProcName;
4109-
if (parseIdentifier(ProcName))
4099+
MCSymbol *ProcSym;
4100+
if (parseSymbol(ProcSym))
41104101
return TokError("expected symbol name");
41114102
if (parseEOL())
41124103
return true;
4113-
MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
41144104
getStreamer().emitCVFPOData(ProcSym, DirLoc);
41154105
return false;
41164106
}
@@ -4329,15 +4319,12 @@ bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
43294319
if (Encoding == dwarf::DW_EH_PE_omit)
43304320
return false;
43314321

4332-
StringRef Name;
4322+
MCSymbol *Sym;
43334323
if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
43344324
parseComma() ||
4335-
check(parseIdentifier(Name), "expected identifier in directive") ||
4336-
parseEOL())
4325+
check(parseSymbol(Sym), "expected identifier in directive") || parseEOL())
43374326
return true;
43384327

4339-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4340-
43414328
if (IsPersonality)
43424329
getStreamer().emitCFIPersonality(Sym, Encoding);
43434330
else
@@ -4988,13 +4975,10 @@ bool AsmParser::parseDirectiveComm(bool IsLocal) {
49884975
return true;
49894976

49904977
SMLoc IDLoc = getLexer().getLoc();
4991-
StringRef Name;
4992-
if (parseIdentifier(Name))
4978+
MCSymbol *Sym;
4979+
if (parseSymbol(Sym))
49934980
return TokError("expected identifier in directive");
49944981

4995-
// Handle the identifier as the key symbol.
4996-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4997-
49984982
if (parseComma())
49994983
return true;
50004984

@@ -5827,10 +5811,9 @@ bool AsmParser::parseDirectiveAddrsig() {
58275811
}
58285812

58295813
bool AsmParser::parseDirectiveAddrsigSym() {
5830-
StringRef Name;
5831-
if (check(parseIdentifier(Name), "expected identifier") || parseEOL())
5814+
MCSymbol *Sym;
5815+
if (check(parseSymbol(Sym), "expected identifier") || parseEOL())
58325816
return true;
5833-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
58345817
getStreamer().emitAddrsigSym(Sym);
58355818
return false;
58365819
}

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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ bool COFFMasmParser::parseDirectiveProc(StringRef Directive, SMLoc Loc) {
444444
if (!getStreamer().getCurrentFragment())
445445
return Error(getTok().getLoc(), "expected section directive");
446446

447-
StringRef Label;
448-
if (getParser().parseIdentifier(Label))
447+
MCSymbol *Sym;
448+
if (getParser().parseSymbol(Sym))
449449
return Error(Loc, "expected identifier for procedure");
450450
if (getLexer().is(AsmToken::Identifier)) {
451451
StringRef nextVal = getTok().getString();
@@ -460,11 +460,12 @@ bool COFFMasmParser::parseDirectiveProc(StringRef Directive, SMLoc Loc) {
460460
nextLoc = getTok().getLoc();
461461
}
462462
}
463-
MCSymbolCOFF *Sym = cast<MCSymbolCOFF>(getContext().getOrCreateSymbol(Label));
464463

465464
// Define symbol as simple external function
466-
Sym->setExternal(true);
467-
Sym->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION << COFF::SCT_COMPLEX_TYPE_SHIFT);
465+
auto *COFFSym = cast<MCSymbolCOFF>(Sym);
466+
COFFSym->setExternal(true);
467+
COFFSym->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION
468+
<< COFF::SCT_COMPLEX_TYPE_SHIFT);
468469

469470
bool Framed = false;
470471
if (getLexer().is(AsmToken::Identifier) &&
@@ -475,7 +476,7 @@ bool COFFMasmParser::parseDirectiveProc(StringRef Directive, SMLoc Loc) {
475476
}
476477
getStreamer().emitLabel(Sym, Loc);
477478

478-
CurrentProcedures.push_back(Label);
479+
CurrentProcedures.push_back(Sym->getName());
479480
CurrentProceduresFramed.push_back(Framed);
480481
return false;
481482
}

0 commit comments

Comments
 (0)