Skip to content

Commit a7fd885

Browse files
committed
Support unique for coff asm
1 parent bd6f760 commit a7fd885

15 files changed

+148
-64
lines changed

llvm/include/llvm/MC/MCParser/MCAsmParserExtension.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ class MCAsmParserExtension {
100100

101101
bool parseDirectiveCGProfile(StringRef, SMLoc);
102102

103+
bool maybeParseUniqueID(int64_t &UniqueID);
104+
103105
bool check(bool P, const Twine &Msg) {
104106
return getParser().check(P, Msg);
105107
}

llvm/include/llvm/MC/MCSectionCOFF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class MCSectionCOFF final : public MCSection {
7575

7676
void setSelection(int Selection) const;
7777

78+
bool isUnique() const { return UniqueID != NonUniqueID; }
7879
unsigned getUniqueID() const { return UniqueID; }
7980

8081
void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,

llvm/lib/MC/MCParser/COFFAsmParser.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ class COFFAsmParser : public MCAsmParserExtension {
3838
bool parseSectionSwitch(StringRef Section, unsigned Characteristics);
3939

4040
bool parseSectionSwitch(StringRef Section, unsigned Characteristics,
41-
StringRef COMDATSymName, COFF::COMDATType Type);
41+
StringRef COMDATSymName, COFF::COMDATType Type,
42+
unsigned UniqueID);
4243

4344
bool parseSectionName(StringRef &SectionName);
4445
bool parseSectionFlags(StringRef SectionName, StringRef FlagsString,
4546
unsigned *Flags);
46-
4747
void Initialize(MCAsmParser &Parser) override {
4848
// Call the base implementation.
4949
MCAsmParserExtension::Initialize(Parser);
@@ -315,19 +315,21 @@ bool COFFAsmParser::parseDirectiveCGProfile(StringRef S, SMLoc Loc) {
315315

316316
bool COFFAsmParser::parseSectionSwitch(StringRef Section,
317317
unsigned Characteristics) {
318-
return parseSectionSwitch(Section, Characteristics, "", (COFF::COMDATType)0);
318+
return parseSectionSwitch(Section, Characteristics, "", (COFF::COMDATType)0,
319+
MCSection::NonUniqueID);
319320
}
320321

321322
bool COFFAsmParser::parseSectionSwitch(StringRef Section,
322323
unsigned Characteristics,
323324
StringRef COMDATSymName,
324-
COFF::COMDATType Type) {
325+
COFF::COMDATType Type,
326+
unsigned UniqueID) {
325327
if (getLexer().isNot(AsmToken::EndOfStatement))
326328
return TokError("unexpected token in section switching directive");
327329
Lex();
328330

329331
getStreamer().switchSection(getContext().getCOFFSection(
330-
Section, Characteristics, COMDATSymName, Type));
332+
Section, Characteristics, COMDATSymName, Type, UniqueID));
331333

332334
return false;
333335
}
@@ -386,7 +388,8 @@ bool COFFAsmParser::parseSectionArguments(StringRef, SMLoc) {
386388

387389
COFF::COMDATType Type = (COFF::COMDATType)0;
388390
StringRef COMDATSymName;
389-
if (getLexer().is(AsmToken::Comma)) {
391+
if (getLexer().is(AsmToken::Comma) &&
392+
getLexer().peekTok().getString() != "unique") {
390393
Type = COFF::IMAGE_COMDAT_SELECT_ANY;
391394
Lex();
392395

@@ -407,6 +410,10 @@ bool COFFAsmParser::parseSectionArguments(StringRef, SMLoc) {
407410
return TokError("expected identifier in directive");
408411
}
409412

413+
int64_t UniqueID = MCSection::NonUniqueID;
414+
if (maybeParseUniqueID(UniqueID))
415+
return true;
416+
410417
if (getLexer().isNot(AsmToken::EndOfStatement))
411418
return TokError("unexpected token in directive");
412419

@@ -415,7 +422,7 @@ bool COFFAsmParser::parseSectionArguments(StringRef, SMLoc) {
415422
if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb)
416423
Flags |= COFF::IMAGE_SCN_MEM_16BIT;
417424
}
418-
parseSectionSwitch(SectionName, Flags, COMDATSymName, Type);
425+
parseSectionSwitch(SectionName, Flags, COMDATSymName, Type, UniqueID);
419426
return false;
420427
}
421428

llvm/lib/MC/MCParser/ELFAsmParser.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ class ELFAsmParser : public MCAsmParserExtension {
158158
bool parseMergeSize(int64_t &Size);
159159
bool parseGroup(StringRef &GroupName, bool &IsComdat);
160160
bool parseLinkedToSym(MCSymbolELF *&LinkedToSym);
161-
bool maybeParseUniqueID(int64_t &UniqueID);
162161
};
163162

164163
} // end anonymous namespace
@@ -495,28 +494,6 @@ bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
495494
return false;
496495
}
497496

498-
bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
499-
MCAsmLexer &L = getLexer();
500-
if (L.isNot(AsmToken::Comma))
501-
return false;
502-
Lex();
503-
StringRef UniqueStr;
504-
if (getParser().parseIdentifier(UniqueStr))
505-
return TokError("expected identifier");
506-
if (UniqueStr != "unique")
507-
return TokError("expected 'unique'");
508-
if (L.isNot(AsmToken::Comma))
509-
return TokError("expected commma");
510-
Lex();
511-
if (getParser().parseAbsoluteExpression(UniqueID))
512-
return true;
513-
if (UniqueID < 0)
514-
return TokError("unique id must be positive");
515-
if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
516-
return TokError("unique id is too large");
517-
return false;
518-
}
519-
520497
static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
521498
return SectionName.consume_front(Prefix) &&
522499
(SectionName.empty() || SectionName[0] == '.');

llvm/lib/MC/MCParser/MCAsmParserExtension.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,25 @@ bool MCAsmParserExtension::parseDirectiveCGProfile(StringRef, SMLoc) {
6262
Count);
6363
return false;
6464
}
65+
66+
bool MCAsmParserExtension::maybeParseUniqueID(int64_t &UniqueID) {
67+
MCAsmLexer &L = getLexer();
68+
if (L.isNot(AsmToken::Comma))
69+
return false;
70+
Lex();
71+
StringRef UniqueStr;
72+
if (getParser().parseIdentifier(UniqueStr))
73+
return TokError("expected identifier");
74+
if (UniqueStr != "unique")
75+
return TokError("expected 'unique'");
76+
if (L.isNot(AsmToken::Comma))
77+
return TokError("expected commma");
78+
Lex();
79+
if (getParser().parseAbsoluteExpression(UniqueID))
80+
return true;
81+
if (UniqueID < 0)
82+
return TokError("unique id must be positive");
83+
if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
84+
return TokError("unique id is too large");
85+
return false;
86+
}

llvm/lib/MC/MCSectionCOFF.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using namespace llvm;
1818
// should be printed before the section name
1919
bool MCSectionCOFF::shouldOmitSectionDirective(StringRef Name,
2020
const MCAsmInfo &MAI) const {
21-
if (COMDATSymbol)
21+
if (COMDATSymbol || isUnique())
2222
return false;
2323

2424
// FIXME: Does .section .bss/.data/.text work everywhere??
@@ -67,6 +67,10 @@ void MCSectionCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
6767
OS << 'i';
6868
OS << '"';
6969

70+
// unique should be tail of .section directive.
71+
if (isUnique() && !COMDATSymbol)
72+
OS << ",unique," << UniqueID;
73+
7074
if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
7175
if (COMDATSymbol)
7276
OS << ",";
@@ -103,6 +107,10 @@ void MCSectionCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
103107
COMDATSymbol->print(OS, &MAI);
104108
}
105109
}
110+
111+
if (isUnique() && COMDATSymbol)
112+
OS << ",unique," << UniqueID;
113+
106114
OS << '\n';
107115
}
108116

llvm/test/CodeGen/X86/constructor.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ entry:
7777
; MCU-CTORS: .section .ctors,"aw",@progbits
7878
; MCU-INIT-ARRAY: .section .init_array,"aw",@init_array
7979

80-
; COFF-CTOR: .section .ctors.65520,"dw",associative,v
80+
; COFF-CTOR: .section .ctors.65520,"dw",associative,v,unique,0
8181
; COFF-CTOR-NEXT: .p2align 3
8282
; COFF-CTOR-NEXT: .quad g
83-
; COFF-CTOR-NEXT: .section .ctors.09980,"dw",associative,v
83+
; COFF-CTOR-NEXT: .section .ctors.09980,"dw",associative,v,unique,0
8484
; COFF-CTOR-NEXT: .p2align 3
8585
; COFF-CTOR-NEXT: .quad h
8686
; COFF-CTOR-NEXT: .section .ctors,"dw"

llvm/test/CodeGen/X86/ctor-priority-coff.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
; CHECK: .section .CRT$XCC00250,"dr"
1313
; CHECK: .p2align 3
1414
; CHECK: .quad k
15-
; CHECK: .section .CRT$XCL,"dr"
15+
; CHECK: .section .CRT$XCL,"dr",unique,0
1616
; CHECK: .p2align 3
1717
; CHECK: .quad j
1818
; CHECK: .section .CRT$XCT12345,"dr"
1919
; CHECK: .p2align 3
2020
; CHECK: .quad g
21-
; CHECK: .section .CRT$XCT23456,"dr",associative,h
21+
; CHECK: .section .CRT$XCT23456,"dr",associative,h,unique,0
2222
; CHECK: .p2align 3
2323
; CHECK: .quad init_h
2424

llvm/test/CodeGen/X86/data-section-prefix.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
; ELF-NOUNIQ: .section .bss.unlikely.,"aw",@nobits,unique,3
1414
; ELF-NOUNIQ: .section .bss,"aw",@nobits,unique,4
1515

16-
; COFF-MSVC: .section .data,"dw",one_only,foo
17-
; COFF-MSVC: .section .data,"dw",one_only,bar
18-
; COFF-MSVC: .section .bss,"bw",one_only,baz
19-
; COFF-MSVC: .section .bss,"bw",one_only,quz
16+
; COFF-MSVC: .section .data,"dw",one_only,foo,unique,0
17+
; COFF-MSVC: .section .data,"dw",one_only,bar,unique,1
18+
; COFF-MSVC: .section .bss,"bw",one_only,baz,unique,2
19+
; COFF-MSVC: .section .bss,"bw",one_only,quz,unique,3
2020

2121
@foo = global i32 1, !section_prefix !0
2222
@bar = global i32 2

llvm/test/CodeGen/X86/dtor-priority-coff.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
; CHECK: .section .CRT$XTT12345,"dr"
1010
; CHECK: .p2align 3
1111
; CHECK: .quad g
12-
; CHECK: .section .CRT$XTT23456,"dr",associative,h
12+
; CHECK: .section .CRT$XTT23456,"dr",associative,h,unique,0
1313
; CHECK: .p2align 3
1414
; CHECK: .quad init_h
1515
; CHECK: .section .CRT$XTX,"dr"

0 commit comments

Comments
 (0)