Skip to content

Commit 3e93017

Browse files
MaskRaytstellar
authored andcommitted
MC: Better handle backslash-escaped symbols (llvm#158780)
The MCContext::getOrCreateSymbol change in llvm#138817 was a workaround. With llvm#158106, we can replace `getOrCreateSymbol` with `parseSymbol`, in llvm/lib/MC/MCParser to handle backslash-escaped symbols. (cherry picked from commit 0cf6688)
1 parent bc5e9a5 commit 3e93017

File tree

12 files changed

+65
-48
lines changed

12 files changed

+65
-48
lines changed

llvm/include/llvm/MC/MCContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ class MCContext {
489489
/// \param Name - The symbol name, which must be unique across all symbols.
490490
LLVM_ABI MCSymbol *getOrCreateSymbol(const Twine &Name);
491491

492+
/// Variant of getOrCreateSymbol that handles backslash-escaped symbols.
493+
/// For example, parse "a\"b\\" as a"\.
494+
LLVM_ABI MCSymbol *parseSymbol(const Twine &Name);
495+
492496
/// Gets a symbol that will be defined to the final stack offset of a local
493497
/// variable after codegen.
494498
///

llvm/lib/MC/MCContext.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -217,27 +217,6 @@ MCDataFragment *MCContext::allocInitialFragment(MCSection &Sec) {
217217
MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
218218
SmallString<128> NameSV;
219219
StringRef NameRef = Name.toStringRef(NameSV);
220-
if (NameRef.contains('\\')) {
221-
NameSV = NameRef;
222-
size_t S = 0;
223-
// Support escaped \\ and \" as in GNU Assembler. GAS issues a warning for
224-
// other characters following \\, which we do not implement due to code
225-
// structure.
226-
for (size_t I = 0, E = NameSV.size(); I != E; ++I) {
227-
char C = NameSV[I];
228-
if (C == '\\' && I + 1 != E) {
229-
switch (NameSV[I + 1]) {
230-
case '"':
231-
case '\\':
232-
C = NameSV[++I];
233-
break;
234-
}
235-
}
236-
NameSV[S++] = C;
237-
}
238-
NameSV.resize(S);
239-
NameRef = NameSV;
240-
}
241220

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

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

240+
MCSymbol *MCContext::parseSymbol(const Twine &Name) {
241+
SmallString<128> SV;
242+
StringRef NameRef = Name.toStringRef(SV);
243+
if (NameRef.contains('\\')) {
244+
SV = NameRef;
245+
size_t S = 0;
246+
// Support escaped \\ and \" as in GNU Assembler. GAS issues a warning for
247+
// other characters following \\, which we do not implement due to code
248+
// structure.
249+
for (size_t I = 0, E = SV.size(); I != E; ++I) {
250+
char C = SV[I];
251+
if (C == '\\' && I + 1 != E) {
252+
switch (SV[I + 1]) {
253+
case '"':
254+
case '\\':
255+
C = SV[++I];
256+
break;
257+
}
258+
}
259+
SV[S++] = C;
260+
}
261+
SV.resize(S);
262+
NameRef = SV;
263+
}
264+
265+
return getOrCreateSymbol(NameRef);
266+
}
267+
261268
MCSymbol *MCContext::getOrCreateFrameAllocSymbol(const Twine &FuncName,
262269
unsigned Idx) {
263270
return getOrCreateSymbol(MAI->getPrivateGlobalPrefix() + FuncName +

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,8 +1222,8 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
12221222

12231223
MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
12241224
if (!Sym)
1225-
Sym = getContext().getOrCreateSymbol(MAI.isHLASM() ? SymbolName.upper()
1226-
: SymbolName);
1225+
Sym = getContext().parseSymbol(MAI.isHLASM() ? SymbolName.upper()
1226+
: SymbolName);
12271227

12281228
// If this is an absolute variable reference, substitute it now to preserve
12291229
// semantics in the face of reassignment.
@@ -1854,7 +1854,7 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info,
18541854
RewrittenLabel);
18551855
IDVal = RewrittenLabel;
18561856
}
1857-
Sym = getContext().getOrCreateSymbol(IDVal);
1857+
Sym = getContext().parseSymbol(IDVal);
18581858
} else
18591859
Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
18601860
// End of Labels should be treated as end of line for lexing
@@ -4953,7 +4953,7 @@ bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
49534953
if (discardLTOSymbol(Name))
49544954
return false;
49554955

4956-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4956+
MCSymbol *Sym = getContext().parseSymbol(Name);
49574957

49584958
// Assembler local symbols don't make any sense here, except for directives
49594959
// that the symbol should be tagged.
@@ -6213,7 +6213,7 @@ bool HLASMAsmParser::parseAsHLASMLabel(ParseStatementInfo &Info,
62136213
return Error(LabelLoc,
62146214
"Cannot have just a label for an HLASM inline asm statement");
62156215

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

62196219
// Emit the label.
@@ -6340,7 +6340,7 @@ bool parseAssignmentExpression(StringRef Name, bool allow_redef,
63406340
Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc);
63416341
return false;
63426342
} else
6343-
Sym = Parser.getContext().getOrCreateSymbol(Name);
6343+
Sym = Parser.getContext().parseSymbol(Name);
63446344

63456345
Sym->setRedefinable(allow_redef);
63466346

llvm/lib/MC/MCParser/COFFMasmParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ bool COFFMasmParser::parseDirectiveAlias(StringRef Directive, SMLoc Loc) {
511511
getParser().parseAngleBracketString(ActualName))
512512
return Error(getTok().getLoc(), "expected <actualName>");
513513

514-
MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
515-
MCSymbol *Actual = getContext().getOrCreateSymbol(ActualName);
514+
MCSymbol *Alias = getContext().parseSymbol(AliasName);
515+
MCSymbol *Actual = getContext().parseSymbol(ActualName);
516516

517517
getStreamer().emitWeakReference(Alias, Actual);
518518

llvm/lib/MC/MCParser/ELFAsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ bool ELFAsmParser::parseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
164164
continue;
165165
}
166166

167-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
167+
MCSymbol *Sym = getContext().parseSymbol(Name);
168168

169169
getStreamer().emitSymbolAttribute(Sym, Attr);
170170

llvm/lib/MC/MCParser/MCAsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ bool MCAsmParser::parseSymbol(MCSymbol *&Res) {
168168
if (parseIdentifier(Name))
169169
return true;
170170

171-
Res = getContext().getOrCreateSymbol(Name);
171+
Res = getContext().parseSymbol(Name);
172172
return false;
173173
}
174174

llvm/lib/MC/MCParser/MCAsmParserExtension.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ bool MCAsmParserExtension::parseDirectiveCGProfile(StringRef, SMLoc) {
5050
if (getLexer().isNot(AsmToken::EndOfStatement))
5151
return TokError("unexpected token in directive");
5252

53-
MCSymbol *FromSym = getContext().getOrCreateSymbol(From);
54-
MCSymbol *ToSym = getContext().getOrCreateSymbol(To);
53+
MCSymbol *FromSym = getContext().parseSymbol(From);
54+
MCSymbol *ToSym = getContext().parseSymbol(To);
5555

5656
getStreamer().emitCGProfileEntry(
5757
MCSymbolRefExpr::create(FromSym, getContext(), FromLoc),

llvm/lib/MC/MCParser/MasmParser.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ bool MasmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
14801480
auto VarIt = Variables.find(SymbolName.lower());
14811481
if (VarIt != Variables.end())
14821482
SymbolName = VarIt->second.Name;
1483-
Sym = getContext().getOrCreateSymbol(SymbolName);
1483+
Sym = getContext().parseSymbol(SymbolName);
14841484
}
14851485

14861486
// If this is an absolute variable reference, substitute it now to preserve
@@ -1965,7 +1965,7 @@ bool MasmParser::parseStatement(ParseStatementInfo &Info,
19651965
if (IDVal == "@@") {
19661966
Sym = Ctx.createDirectionalLocalSymbol(0);
19671967
} else {
1968-
Sym = getContext().getOrCreateSymbol(IDVal);
1968+
Sym = getContext().parseSymbol(IDVal);
19691969
}
19701970

19711971
// End of Labels should be treated as end of line for lexing
@@ -3009,8 +3009,7 @@ bool MasmParser::parseDirectiveEquate(StringRef IDVal, StringRef Name,
30093009
return false;
30103010
}
30113011

3012-
MCSymbol *Sym = getContext().getOrCreateSymbol(Var.Name);
3013-
3012+
auto *Sym = getContext().parseSymbol(Var.Name);
30143013
const MCConstantExpr *PrevValue =
30153014
Sym->isVariable()
30163015
? dyn_cast_or_null<MCConstantExpr>(Sym->getVariableValue())
@@ -3318,7 +3317,7 @@ bool MasmParser::parseDirectiveNamedValue(StringRef TypeName, unsigned Size,
33183317
StringRef Name, SMLoc NameLoc) {
33193318
if (StructInProgress.empty()) {
33203319
// Initialize named data value.
3321-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
3320+
MCSymbol *Sym = getContext().parseSymbol(Name);
33223321
getStreamer().emitLabel(Sym);
33233322
unsigned Count;
33243323
if (emitIntegralValues(Size, &Count))
@@ -3509,7 +3508,7 @@ bool MasmParser::parseDirectiveNamedRealValue(StringRef TypeName,
35093508
SMLoc NameLoc) {
35103509
if (StructInProgress.empty()) {
35113510
// Initialize named data value.
3512-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
3511+
MCSymbol *Sym = getContext().parseSymbol(Name);
35133512
getStreamer().emitLabel(Sym);
35143513
unsigned Count;
35153514
if (emitRealValues(Semantics, &Count))
@@ -4003,7 +4002,7 @@ bool MasmParser::parseDirectiveNamedStructValue(const StructInfo &Structure,
40034002
SMLoc DirLoc, StringRef Name) {
40044003
if (StructInProgress.empty()) {
40054004
// Initialize named data value.
4006-
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4005+
MCSymbol *Sym = getContext().parseSymbol(Name);
40074006
getStreamer().emitLabel(Sym);
40084007
unsigned Count;
40094008
if (emitStructValues(Structure, &Count))

llvm/lib/MC/MCParser/WasmAsmParser.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,8 @@ class WasmAsmParser : public MCAsmParserExtension {
240240
if (!Lexer->is(AsmToken::Identifier))
241241
return error("Expected label after .type directive, got: ",
242242
Lexer->getTok());
243-
auto WasmSym = cast<MCSymbolWasm>(
244-
getStreamer().getContext().getOrCreateSymbol(
245-
Lexer->getTok().getString()));
243+
auto *WasmSym = cast<MCSymbolWasm>(
244+
getStreamer().getContext().parseSymbol(Lexer->getTok().getString()));
246245
Lex();
247246
if (!(isNext(AsmToken::Comma) && isNext(AsmToken::At) &&
248247
Lexer->is(AsmToken::Identifier)))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; RUN: llc < %s -mtriple=x86_64 -relocation-model=pic | FileCheck %s
2+
3+
; CHECK: .globl "\\\""
4+
; CHECK-NEXT: "\\\"":
5+
@"\\\22" = constant i8 0

0 commit comments

Comments
 (0)