Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/include/llvm/MC/MCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ class MCContext {
/// \param Name - The symbol name, which must be unique across all symbols.
LLVM_ABI MCSymbol *getOrCreateSymbol(const Twine &Name);

/// Variant of getOrCreateSymbol that handles backslash-escaped symbols.
/// For example, parse "a\"b\\" as a"\.
LLVM_ABI MCSymbol *parseSymbol(const Twine &Name);

/// Gets a symbol that will be defined to the final stack offset of a local
/// variable after codegen.
///
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/MC/MCParser/MCAsmParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ class LLVM_ABI MCAsmParser {
/// Res to the identifier contents.
virtual bool parseIdentifier(StringRef &Res) = 0;

/// Parse identifier and get or create symbol for it.
bool parseSymbol(MCSymbol *&Res);

/// Parse up to the end of statement and return the contents from the
/// current token until the end of the statement; the current token on exit
/// will be either the EndOfStatement or EOF.
Expand Down
49 changes: 28 additions & 21 deletions llvm/lib/MC/MCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,6 @@ MCDataFragment *MCContext::allocInitialFragment(MCSection &Sec) {
MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
SmallString<128> NameSV;
StringRef NameRef = Name.toStringRef(NameSV);
if (NameRef.contains('\\')) {
NameSV = NameRef;
size_t S = 0;
// Support escaped \\ and \" as in GNU Assembler. GAS issues a warning for
// other characters following \\, which we do not implement due to code
// structure.
for (size_t I = 0, E = NameSV.size(); I != E; ++I) {
char C = NameSV[I];
if (C == '\\' && I + 1 != E) {
switch (NameSV[I + 1]) {
case '"':
case '\\':
C = NameSV[++I];
break;
}
}
NameSV[S++] = C;
}
NameSV.resize(S);
NameRef = NameSV;
}

assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");

Expand All @@ -258,6 +237,34 @@ MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
return Entry.second.Symbol;
}

MCSymbol *MCContext::parseSymbol(const Twine &Name) {
SmallString<128> SV;
StringRef NameRef = Name.toStringRef(SV);
if (NameRef.contains('\\')) {
SV = NameRef;
size_t S = 0;
// Support escaped \\ and \" as in GNU Assembler. GAS issues a warning for
// other characters following \\, which we do not implement due to code
// structure.
for (size_t I = 0, E = SV.size(); I != E; ++I) {
char C = SV[I];
if (C == '\\' && I + 1 != E) {
switch (SV[I + 1]) {
case '"':
case '\\':
C = SV[++I];
break;
}
}
SV[S++] = C;
}
SV.resize(S);
NameRef = SV;
}

return getOrCreateSymbol(NameRef);
}

MCSymbol *MCContext::getOrCreateFrameAllocSymbol(const Twine &FuncName,
unsigned Idx) {
return getOrCreateSymbol(MAI->getPrivateGlobalPrefix() + FuncName +
Expand Down
65 changes: 24 additions & 41 deletions llvm/lib/MC/MCParser/AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,8 +1222,8 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,

MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
if (!Sym)
Sym = getContext().getOrCreateSymbol(MAI.isHLASM() ? SymbolName.upper()
: SymbolName);
Sym = getContext().parseSymbol(MAI.isHLASM() ? SymbolName.upper()
: SymbolName);

// If this is an absolute variable reference, substitute it now to preserve
// semantics in the face of reassignment.
Expand Down Expand Up @@ -1854,7 +1854,7 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info,
RewrittenLabel);
IDVal = RewrittenLabel;
}
Sym = getContext().getOrCreateSymbol(IDVal);
Sym = getContext().parseSymbol(IDVal);
} else
Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
// End of Labels should be treated as end of line for lexing
Expand Down Expand Up @@ -3897,20 +3897,15 @@ bool AsmParser::parseDirectiveCVLoc() {
/// ::= .cv_linetable FunctionId, FnStart, FnEnd
bool AsmParser::parseDirectiveCVLinetable() {
int64_t FunctionId;
StringRef FnStartName, FnEndName;
MCSymbol *FnStartSym, *FnEndSym;
SMLoc Loc = getTok().getLoc();
if (parseCVFunctionId(FunctionId, ".cv_linetable") || parseComma() ||
parseTokenLoc(Loc) ||
check(parseIdentifier(FnStartName), Loc,
"expected identifier in directive") ||
check(parseSymbol(FnStartSym), Loc, "expected identifier in directive") ||
parseComma() || parseTokenLoc(Loc) ||
check(parseIdentifier(FnEndName), Loc,
"expected identifier in directive"))
check(parseSymbol(FnEndSym), Loc, "expected identifier in directive"))
return true;

MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);

getStreamer().emitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
return false;
}
Expand All @@ -3919,7 +3914,7 @@ bool AsmParser::parseDirectiveCVLinetable() {
/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
bool AsmParser::parseDirectiveCVInlineLinetable() {
int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
StringRef FnStartName, FnEndName;
MCSymbol *FnStartSym, *FnEndSym;
SMLoc Loc = getTok().getLoc();
if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
parseTokenLoc(Loc) ||
Expand All @@ -3929,16 +3924,14 @@ bool AsmParser::parseDirectiveCVInlineLinetable() {
parseIntToken(SourceLineNum, "expected SourceLineNum") ||
check(SourceLineNum < 0, Loc, "Line number less than zero") ||
parseTokenLoc(Loc) ||
check(parseIdentifier(FnStartName), Loc, "expected identifier") ||
check(parseSymbol(FnStartSym), Loc, "expected identifier") ||
parseTokenLoc(Loc) ||
check(parseIdentifier(FnEndName), Loc, "expected identifier"))
check(parseSymbol(FnEndSym), Loc, "expected identifier"))
return true;

if (parseEOL())
return true;

MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
SourceLineNum, FnStartSym,
FnEndSym);
Expand All @@ -3959,16 +3952,14 @@ bool AsmParser::parseDirectiveCVDefRange() {
std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
while (getLexer().is(AsmToken::Identifier)) {
Loc = getLexer().getLoc();
StringRef GapStartName;
if (parseIdentifier(GapStartName))
MCSymbol *GapStartSym;
if (parseSymbol(GapStartSym))
return Error(Loc, "expected identifier in directive");
MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);

Loc = getLexer().getLoc();
StringRef GapEndName;
if (parseIdentifier(GapEndName))
MCSymbol *GapEndSym;
if (parseSymbol(GapEndSym))
return Error(Loc, "expected identifier in directive");
MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);

Ranges.push_back({GapStartSym, GapEndSym});
}
Expand Down Expand Up @@ -4105,12 +4096,11 @@ bool AsmParser::parseDirectiveCVFileChecksumOffset() {
/// ::= .cv_fpo_data procsym
bool AsmParser::parseDirectiveCVFPOData() {
SMLoc DirLoc = getLexer().getLoc();
StringRef ProcName;
if (parseIdentifier(ProcName))
MCSymbol *ProcSym;
if (parseSymbol(ProcSym))
return TokError("expected symbol name");
if (parseEOL())
return true;
MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
getStreamer().emitCVFPOData(ProcSym, DirLoc);
return false;
}
Expand Down Expand Up @@ -4329,15 +4319,12 @@ bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
if (Encoding == dwarf::DW_EH_PE_omit)
return false;

StringRef Name;
MCSymbol *Sym;
if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
parseComma() ||
check(parseIdentifier(Name), "expected identifier in directive") ||
parseEOL())
check(parseSymbol(Sym), "expected identifier in directive") || parseEOL())
return true;

MCSymbol *Sym = getContext().getOrCreateSymbol(Name);

if (IsPersonality)
getStreamer().emitCFIPersonality(Sym, Encoding);
else
Expand Down Expand Up @@ -4966,7 +4953,7 @@ bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
if (discardLTOSymbol(Name))
return false;

MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
MCSymbol *Sym = getContext().parseSymbol(Name);

// Assembler local symbols don't make any sense here, except for directives
// that the symbol should be tagged.
Expand All @@ -4988,13 +4975,10 @@ bool AsmParser::parseDirectiveComm(bool IsLocal) {
return true;

SMLoc IDLoc = getLexer().getLoc();
StringRef Name;
if (parseIdentifier(Name))
MCSymbol *Sym;
if (parseSymbol(Sym))
return TokError("expected identifier in directive");

// Handle the identifier as the key symbol.
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);

if (parseComma())
return true;

Expand Down Expand Up @@ -5827,10 +5811,9 @@ bool AsmParser::parseDirectiveAddrsig() {
}

bool AsmParser::parseDirectiveAddrsigSym() {
StringRef Name;
if (check(parseIdentifier(Name), "expected identifier") || parseEOL())
MCSymbol *Sym;
if (check(parseSymbol(Sym), "expected identifier") || parseEOL())
return true;
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
getStreamer().emitAddrsigSym(Sym);
return false;
}
Expand Down Expand Up @@ -6230,7 +6213,7 @@ bool HLASMAsmParser::parseAsHLASMLabel(ParseStatementInfo &Info,
return Error(LabelLoc,
"Cannot have just a label for an HLASM inline asm statement");

MCSymbol *Sym = getContext().getOrCreateSymbol(
MCSymbol *Sym = getContext().parseSymbol(
getContext().getAsmInfo()->isHLASM() ? LabelVal.upper() : LabelVal);

// Emit the label.
Expand Down Expand Up @@ -6357,7 +6340,7 @@ bool parseAssignmentExpression(StringRef Name, bool allow_redef,
Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc);
return false;
} else
Sym = Parser.getContext().getOrCreateSymbol(Name);
Sym = Parser.getContext().parseSymbol(Name);

Sym->setRedefinable(allow_redef);

Expand Down
Loading
Loading