Skip to content

Commit 1e4c76c

Browse files
authored
[MC][AsmParser] Make MatchRegisterName return MCRegister (NFC) (#81408)
`MCRegister` is preferred over `unsigned` nowadays.
1 parent 1a8c613 commit 1e4c76c

File tree

8 files changed

+39
-34
lines changed

8 files changed

+39
-34
lines changed

llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2600,7 +2600,7 @@ void AArch64Operand::print(raw_ostream &OS) const {
26002600
/// @name Auto-generated Match Functions
26012601
/// {
26022602

2603-
static unsigned MatchRegisterName(StringRef Name);
2603+
static MCRegister MatchRegisterName(StringRef Name);
26042604

26052605
/// }
26062606

llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4045,7 +4045,7 @@ void ARMOperand::print(raw_ostream &OS) const {
40454045
/// @name Auto-generated Match Functions
40464046
/// {
40474047

4048-
static unsigned MatchRegisterName(StringRef Name);
4048+
static MCRegister MatchRegisterName(StringRef Name);
40494049

40504050
/// }
40514051

llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class AVRAsmParser : public MCTargetAsmParser {
6868
ParseStatus parseMemriOperand(OperandVector &Operands);
6969

7070
bool parseOperand(OperandVector &Operands, bool maybeReg);
71-
int parseRegisterName(unsigned (*matchFn)(StringRef));
71+
int parseRegisterName(MCRegister (*matchFn)(StringRef));
7272
int parseRegisterName();
7373
int parseRegister(bool RestoreOnFailure = false);
7474
bool tryParseRegisterOperand(OperandVector &Operands);
@@ -276,11 +276,11 @@ class AVROperand : public MCParsedAsmOperand {
276276

277277
/// Maps from the set of all register names to a register number.
278278
/// \note Generated by TableGen.
279-
static unsigned MatchRegisterName(StringRef Name);
279+
static MCRegister MatchRegisterName(StringRef Name);
280280

281281
/// Maps from the set of all alternative registernames to a register number.
282282
/// \note Generated by TableGen.
283-
static unsigned MatchRegisterAltName(StringRef Name);
283+
static MCRegister MatchRegisterAltName(StringRef Name);
284284

285285
bool AVRAsmParser::invalidOperand(SMLoc const &Loc,
286286
OperandVector const &Operands,
@@ -346,7 +346,7 @@ bool AVRAsmParser::MatchAndEmitInstruction(SMLoc Loc, unsigned &Opcode,
346346

347347
/// Parses a register name using a given matching function.
348348
/// Checks for lowercase or uppercase if necessary.
349-
int AVRAsmParser::parseRegisterName(unsigned (*matchFn)(StringRef)) {
349+
int AVRAsmParser::parseRegisterName(MCRegister (*matchFn)(StringRef)) {
350350
StringRef Name = Parser.getTok().getString();
351351

352352
int RegNum = matchFn(Name);

llvm/lib/Target/Lanai/AsmParser/LanaiAsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
using namespace llvm;
4141

4242
// Auto-generated by TableGen
43-
static unsigned MatchRegisterName(StringRef Name);
43+
static MCRegister MatchRegisterName(StringRef Name);
4444

4545
namespace {
4646

llvm/lib/Target/MSP430/AsmParser/MSP430AsmParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ bool MSP430AsmParser::MatchAndEmitInstruction(SMLoc Loc, unsigned &Opcode,
286286
}
287287

288288
// Auto-generated by TableGen
289-
static unsigned MatchRegisterName(StringRef Name);
290-
static unsigned MatchRegisterAltName(StringRef Name);
289+
static MCRegister MatchRegisterName(StringRef Name);
290+
static MCRegister MatchRegisterAltName(StringRef Name);
291291

292292
bool MSP430AsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
293293
SMLoc &EndLoc) {

llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class VEAsmParser : public MCTargetAsmParser {
5656
uint64_t &ErrorInfo,
5757
bool MatchingInlineAsm) override;
5858
bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
59-
int parseRegisterName(unsigned (*matchFn)(StringRef));
59+
int parseRegisterName(MCRegister (*matchFn)(StringRef));
6060
ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
6161
SMLoc &EndLoc) override;
6262
bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
@@ -805,7 +805,7 @@ bool VEAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
805805

806806
/// Parses a register name using a given matching function.
807807
/// Checks for lowercase or uppercase if necessary.
808-
int VEAsmParser::parseRegisterName(unsigned (*matchFn)(StringRef)) {
808+
int VEAsmParser::parseRegisterName(MCRegister (*matchFn)(StringRef)) {
809809
StringRef Name = Parser.getTok().getString();
810810

811811
int RegNum = matchFn(Name);
@@ -821,11 +821,11 @@ int VEAsmParser::parseRegisterName(unsigned (*matchFn)(StringRef)) {
821821

822822
/// Maps from the set of all register names to a register number.
823823
/// \note Generated by TableGen.
824-
static unsigned MatchRegisterName(StringRef Name);
824+
static MCRegister MatchRegisterName(StringRef Name);
825825

826826
/// Maps from the set of all alternative registernames to a register number.
827827
/// \note Generated by TableGen.
828-
static unsigned MatchRegisterAltName(StringRef Name);
828+
static MCRegister MatchRegisterAltName(StringRef Name);
829829

830830
ParseStatus VEAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
831831
SMLoc &EndLoc) {

llvm/test/TableGen/AllowDuplicateRegisterNames.td

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def GPR64 : RegisterClass<"Arch", [i64], 64, (add
4141
(sequence "R%u_64", 0, 3)
4242
)>;
4343

44-
// CHECK: static unsigned MatchRegisterName(StringRef Name) {
44+
// CHECK: static MCRegister MatchRegisterName(StringRef Name) {
4545
// CHECK: switch (Name.size()) {
4646
// CHECK: default: break;
4747
// CHECK: case 2: // 8 strings to match.
@@ -50,20 +50,20 @@ def GPR64 : RegisterClass<"Arch", [i64], 64, (add
5050
// CHECK: switch (Name[1]) {
5151
// CHECK: default: break;
5252
// CHECK: case '0': // 2 strings to match.
53-
// CHECK: return 1; // "r0"
53+
// CHECK: return Arch::R0_32; // "r0"
5454
// CHECK: case '1': // 2 strings to match.
55-
// CHECK: return 3; // "r1"
55+
// CHECK: return Arch::R1_32; // "r1"
5656
// CHECK: case '2': // 2 strings to match.
57-
// CHECK: return 5; // "r2"
57+
// CHECK: return Arch::R2_32; // "r2"
5858
// CHECK: case '3': // 2 strings to match.
59-
// CHECK: return 7; // "r3"
59+
// CHECK: return Arch::R3_32; // "r3"
6060
// CHECK: }
6161
// CHECK: break;
6262
// CHECK: }
63-
// CHECK: return 0;
63+
// CHECK: return Arch::NoRegister;
6464
// CHECK: }
6565

66-
// CHECK: static unsigned MatchRegisterAltName(StringRef Name) {
66+
// CHECK: static MCRegister MatchRegisterAltName(StringRef Name) {
6767
// CHECK: switch (Name.size()) {
6868
// CHECK: default: break;
6969
// CHECK: case 2: // 8 strings to match.
@@ -72,15 +72,15 @@ def GPR64 : RegisterClass<"Arch", [i64], 64, (add
7272
// CHECK: switch (Name[1]) {
7373
// CHECK: default: break;
7474
// CHECK: case '0': // 2 strings to match.
75-
// CHECK: return 1; // "x0"
75+
// CHECK: return Arch::R0_32; // "x0"
7676
// CHECK: case '1': // 2 strings to match.
77-
// CHECK: return 3; // "x1"
77+
// CHECK: return Arch::R1_32; // "x1"
7878
// CHECK: case '2': // 2 strings to match.
79-
// CHECK: return 5; // "x2"
79+
// CHECK: return Arch::R2_32; // "x2"
8080
// CHECK: case '3': // 2 strings to match.
81-
// CHECK: return 7; // "x3"
81+
// CHECK: return Arch::R3_32; // "x3"
8282
// CHECK: }
8383
// CHECK: break;
8484
// CHECK: }
85-
// CHECK: return 0;
85+
// CHECK: return Arch::NoRegister;
8686
// CHECK: }

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,21 +2617,24 @@ static void emitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
26172617
// Construct the match list.
26182618
std::vector<StringMatcher::StringPair> Matches;
26192619
const auto &Regs = Target.getRegBank().getRegisters();
2620+
std::string Namespace =
2621+
Regs.front().TheDef->getValueAsString("Namespace").str();
26202622
for (const CodeGenRegister &Reg : Regs) {
2621-
if (Reg.TheDef->getValueAsString("AsmName").empty())
2623+
StringRef AsmName = Reg.TheDef->getValueAsString("AsmName");
2624+
if (AsmName.empty())
26222625
continue;
26232626

2624-
Matches.emplace_back(std::string(Reg.TheDef->getValueAsString("AsmName")),
2625-
"return " + utostr(Reg.EnumValue) + ";");
2627+
Matches.emplace_back(AsmName.str(), "return " + Namespace +
2628+
"::" + Reg.getName().str() + ';');
26262629
}
26272630

2628-
OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
2631+
OS << "static MCRegister MatchRegisterName(StringRef Name) {\n";
26292632

26302633
bool IgnoreDuplicates =
26312634
AsmParser->getValueAsBit("AllowDuplicateRegisterNames");
26322635
StringMatcher("Name", Matches, OS).Emit(0, IgnoreDuplicates);
26332636

2634-
OS << " return 0;\n";
2637+
OS << " return " << Namespace << "::NoRegister;\n";
26352638
OS << "}\n\n";
26362639
}
26372640

@@ -2642,6 +2645,8 @@ static void emitMatchRegisterAltName(CodeGenTarget &Target, Record *AsmParser,
26422645
// Construct the match list.
26432646
std::vector<StringMatcher::StringPair> Matches;
26442647
const auto &Regs = Target.getRegBank().getRegisters();
2648+
std::string Namespace =
2649+
Regs.front().TheDef->getValueAsString("Namespace").str();
26452650
for (const CodeGenRegister &Reg : Regs) {
26462651

26472652
auto AltNames = Reg.TheDef->getValueAsListOfStrings("AltNames");
@@ -2653,18 +2658,18 @@ static void emitMatchRegisterAltName(CodeGenTarget &Target, Record *AsmParser,
26532658
if (AltName.empty())
26542659
continue;
26552660

2656-
Matches.emplace_back(std::string(AltName),
2657-
"return " + utostr(Reg.EnumValue) + ";");
2661+
Matches.emplace_back(AltName.str(), "return " + Namespace +
2662+
"::" + Reg.getName().str() + ';');
26582663
}
26592664
}
26602665

2661-
OS << "static unsigned MatchRegisterAltName(StringRef Name) {\n";
2666+
OS << "static MCRegister MatchRegisterAltName(StringRef Name) {\n";
26622667

26632668
bool IgnoreDuplicates =
26642669
AsmParser->getValueAsBit("AllowDuplicateRegisterNames");
26652670
StringMatcher("Name", Matches, OS).Emit(0, IgnoreDuplicates);
26662671

2667-
OS << " return 0;\n";
2672+
OS << " return " << Namespace << "::NoRegister;\n";
26682673
OS << "}\n\n";
26692674
}
26702675

0 commit comments

Comments
 (0)