Skip to content
Merged
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 @@ -484,6 +484,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"\.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a typo? Where did 'b' go?

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
49 changes: 28 additions & 21 deletions llvm/lib/MC/MCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,6 @@ MCInst *MCContext::createMCInst() {
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 @@ -244,6 +223,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
12 changes: 6 additions & 6 deletions llvm/lib/MC/MCParser/AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,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 @@ -1845,7 +1845,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 @@ -4885,7 +4885,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 Down Expand Up @@ -6142,7 +6142,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 @@ -6270,7 +6270,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
4 changes: 2 additions & 2 deletions llvm/lib/MC/MCParser/COFFMasmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@ bool COFFMasmParser::parseDirectiveAlias(StringRef Directive, SMLoc Loc) {
getParser().parseAngleBracketString(ActualName))
return Error(getTok().getLoc(), "expected <actualName>");

MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
MCSymbol *Actual = getContext().getOrCreateSymbol(ActualName);
MCSymbol *Alias = getContext().parseSymbol(AliasName);
MCSymbol *Actual = getContext().parseSymbol(ActualName);

getStreamer().emitWeakReference(Alias, Actual);

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/MC/MCParser/ELFAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool ELFAsmParser::parseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
continue;
}

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

getStreamer().emitSymbolAttribute(Sym, Attr);

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/MC/MCParser/MCAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ bool MCAsmParser::parseSymbol(MCSymbol *&Res) {
if (parseIdentifier(Name))
return true;

Res = getContext().getOrCreateSymbol(Name);
Res = getContext().parseSymbol(Name);
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/MC/MCParser/MCAsmParserExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ bool MCAsmParserExtension::parseDirectiveCGProfile(StringRef, SMLoc) {
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in directive");

MCSymbol *FromSym = getContext().getOrCreateSymbol(From);
MCSymbol *ToSym = getContext().getOrCreateSymbol(To);
MCSymbol *FromSym = getContext().parseSymbol(From);
MCSymbol *ToSym = getContext().parseSymbol(To);

getStreamer().emitCGProfileEntry(
MCSymbolRefExpr::create(FromSym, getContext(), FromLoc),
Expand Down
13 changes: 6 additions & 7 deletions llvm/lib/MC/MCParser/MasmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ bool MasmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
auto VarIt = Variables.find(SymbolName.lower());
if (VarIt != Variables.end())
SymbolName = VarIt->second.Name;
Sym = getContext().getOrCreateSymbol(SymbolName);
Sym = getContext().parseSymbol(SymbolName);
}

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

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

auto *Sym =
static_cast<MCSymbolCOFF *>(getContext().getOrCreateSymbol(Var.Name));
auto *Sym = static_cast<MCSymbolCOFF *>(getContext().parseSymbol(Var.Name));
const MCConstantExpr *PrevValue =
Sym->isVariable()
? dyn_cast_or_null<MCConstantExpr>(Sym->getVariableValue())
Expand Down Expand Up @@ -3318,7 +3317,7 @@ bool MasmParser::parseDirectiveNamedValue(StringRef TypeName, unsigned Size,
StringRef Name, SMLoc NameLoc) {
if (StructInProgress.empty()) {
// Initialize named data value.
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
MCSymbol *Sym = getContext().parseSymbol(Name);
getStreamer().emitLabel(Sym);
unsigned Count;
if (emitIntegralValues(Size, &Count))
Expand Down Expand Up @@ -3509,7 +3508,7 @@ bool MasmParser::parseDirectiveNamedRealValue(StringRef TypeName,
SMLoc NameLoc) {
if (StructInProgress.empty()) {
// Initialize named data value.
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
MCSymbol *Sym = getContext().parseSymbol(Name);
getStreamer().emitLabel(Sym);
unsigned Count;
if (emitRealValues(Semantics, &Count))
Expand Down Expand Up @@ -4003,7 +4002,7 @@ bool MasmParser::parseDirectiveNamedStructValue(const StructInfo &Structure,
SMLoc DirLoc, StringRef Name) {
if (StructInProgress.empty()) {
// Initialize named data value.
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
MCSymbol *Sym = getContext().parseSymbol(Name);
getStreamer().emitLabel(Sym);
unsigned Count;
if (emitStructValues(Structure, &Count))
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/MC/MCParser/WasmAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ class WasmAsmParser : public MCAsmParserExtension {
return error("Expected label after .type directive, got: ",
Lexer->getTok());
auto *WasmSym = static_cast<MCSymbolWasm *>(
getStreamer().getContext().getOrCreateSymbol(
Lexer->getTok().getString()));
getStreamer().getContext().parseSymbol(Lexer->getTok().getString()));
Lex();
if (!(isNext(AsmToken::Comma) && isNext(AsmToken::At) &&
Lexer->is(AsmToken::Identifier)))
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/X86/symbol-name.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; RUN: llc < %s -mtriple=x86_64 -relocation-model=pic | FileCheck %s

; CHECK: .globl "\\\""
; CHECK-NEXT: "\\\"":
@"\\\22" = constant i8 0
10 changes: 5 additions & 5 deletions llvm/test/MC/ELF/cgprofile.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ a: .word b

.cg_profile a, b, 32
.cg_profile freq, a, 11
.cg_profile late, late2, 20
.cg_profile "late\\", late2, 20
.cg_profile .L.local, b, 42

.globl late
late:
.globl "late\\"
"late\\":
late2: .word 0
late3:
.L.local:
Expand All @@ -31,7 +31,7 @@ late3:
# CHECK-NEXT: 0010: 14000000 00000000 2A000000 00000000
# CHECK-NEXT: )

# CHECK: Name: .rel.llvm.call-graph-profile (28)
# CHECK: Name: .rel.llvm.call-graph-profile
# CHECK-NEXT: Type: SHT_REL (0x9)
# CHECK-NEXT: Flags [ (0x40)
# CHECK-NEXT: SHF_INFO_LINK
Expand Down Expand Up @@ -83,7 +83,7 @@ late3:
# CHECK-NEXT: Type:
# CHECK-NEXT: Other:
# CHECK-NEXT: Section: Undefined
# CHECK: Name: late
# CHECK: Name: late\ ([[#]])
# CHECK-NEXT: Value:
# CHECK-NEXT: Size:
# CHECK-NEXT: Binding: Global
Expand Down
3 changes: 3 additions & 0 deletions llvm/test/MC/ELF/symbol-names.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// CHECK-LABEL:SYMBOL TABLE:
// CHECK-NEXT: 0000000000000001 l F .text 0000000000000000 a"b\{{$}}
// CHECK-NEXT: 0000000000000006 l .text 0000000000000000 a\{{$}}
// CHECK-NEXT: 000000000000000b l .text 0000000000000000 a\\{{$}}
// CHECK-NEXT: 0000000000000000 g F .text 0000000000000000 foo?bar
// CHECK-NEXT: 0000000000000000 *UND* 0000000000000000 a"b\q{{$}}
// CHECK-EMPTY:
Expand All @@ -26,3 +27,5 @@ ret
"a\\":
/// GAS emits a warning for \q
call "a\"b\q"

"a\\\\" = .