Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 46edb8d

Browse files
committedMay 14, 2025
[NFC][TableGen] Use StringRef in X86RecognizableInstr
- Use `StringRef` instead of `std::string` in several functions. - Fix some variable names to conform to LLVM coding standard.
1 parent 86c5112 commit 46edb8d

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed
 

‎llvm/utils/TableGen/X86RecognizableInstr.cpp

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,11 @@ void RecognizableInstr::adjustOperandEncoding(OperandEncoding &encoding) {
437437
"Invalid CDisp scaling");
438438
}
439439

440-
void RecognizableInstr::handleOperand(
441-
bool optional, unsigned &operandIndex, unsigned &physicalOperandIndex,
442-
unsigned numPhysicalOperands, const unsigned *operandMapping,
443-
OperandEncoding (*encodingFromString)(const std::string &,
444-
uint8_t OpSize)) {
440+
void RecognizableInstr::handleOperand(bool optional, unsigned &operandIndex,
441+
unsigned &physicalOperandIndex,
442+
unsigned numPhysicalOperands,
443+
const unsigned *operandMapping,
444+
EncodingFn encodingFromString) {
445445
if (optional) {
446446
if (physicalOperandIndex >= numPhysicalOperands)
447447
return;
@@ -458,12 +458,12 @@ void RecognizableInstr::handleOperand(
458458

459459
StringRef typeName = (*Operands)[operandIndex].Rec->getName();
460460

461-
OperandEncoding encoding = encodingFromString(typeName.str(), OpSize);
461+
OperandEncoding encoding = encodingFromString(typeName, OpSize);
462462
// Adjust the encoding type for an operand based on the instruction.
463463
adjustOperandEncoding(encoding);
464464
Spec->operands[operandIndex].encoding = encoding;
465465
Spec->operands[operandIndex].type =
466-
typeFromString(typeName.str(), HasREX_W, OpSize);
466+
typeFromString(typeName, HasREX_W, OpSize);
467467

468468
++operandIndex;
469469
++physicalOperandIndex;
@@ -1020,11 +1020,12 @@ void RecognizableInstr::emitDecodePath(DisassemblerTables &tables) const {
10201020
#undef MAP
10211021
}
10221022

1023-
#define TYPE(str, type) \
1024-
if (s == str) \
1025-
return type;
1026-
OperandType RecognizableInstr::typeFromString(const std::string &s,
1027-
bool hasREX_W, uint8_t OpSize) {
1023+
#define TYPE(Expected, Type) \
1024+
if (Str == Expected) \
1025+
return Type;
1026+
1027+
OperandType RecognizableInstr::typeFromString(StringRef Str, bool hasREX_W,
1028+
uint8_t OpSize) {
10281029
if (hasREX_W) {
10291030
// For instructions with a REX_W prefix, a declared 32-bit register encoding
10301031
// is special.
@@ -1163,17 +1164,17 @@ OperandType RecognizableInstr::typeFromString(const std::string &s,
11631164
TYPE("BNDR", TYPE_BNDR)
11641165
TYPE("TILE", TYPE_TMM)
11651166
TYPE("TILEPair", TYPE_TMM_PAIR)
1166-
errs() << "Unhandled type string " << s << "\n";
1167+
errs() << "Unhandled type string " << Str << "\n";
11671168
llvm_unreachable("Unhandled type string");
11681169
}
11691170
#undef TYPE
11701171

1171-
#define ENCODING(str, encoding) \
1172-
if (s == str) \
1173-
return encoding;
1174-
OperandEncoding
1175-
RecognizableInstr::immediateEncodingFromString(const std::string &s,
1176-
uint8_t OpSize) {
1172+
#define ENCODING(Expected, Encoding) \
1173+
if (Str == Expected) \
1174+
return Encoding;
1175+
1176+
OperandEncoding RecognizableInstr::immediateEncodingFromString(StringRef Str,
1177+
uint8_t OpSize) {
11771178
if (OpSize != X86Local::OpSize16) {
11781179
// For instructions without an OpSize prefix, a declared 16-bit register or
11791180
// immediate encoding is special.
@@ -1208,13 +1209,12 @@ RecognizableInstr::immediateEncodingFromString(const std::string &s,
12081209
ENCODING("VR256X", ENCODING_IB)
12091210
ENCODING("VR512", ENCODING_IB)
12101211
ENCODING("TILE", ENCODING_IB)
1211-
errs() << "Unhandled immediate encoding " << s << "\n";
1212+
errs() << "Unhandled immediate encoding " << Str << "\n";
12121213
llvm_unreachable("Unhandled immediate encoding");
12131214
}
12141215

12151216
OperandEncoding
1216-
RecognizableInstr::rmRegisterEncodingFromString(const std::string &s,
1217-
uint8_t OpSize) {
1217+
RecognizableInstr::rmRegisterEncodingFromString(StringRef Str, uint8_t OpSize) {
12181218
ENCODING("RST", ENCODING_FP)
12191219
ENCODING("RSTi", ENCODING_FP)
12201220
ENCODING("GR16", ENCODING_RM)
@@ -1245,13 +1245,12 @@ RecognizableInstr::rmRegisterEncodingFromString(const std::string &s,
12451245
ENCODING("BNDR", ENCODING_RM)
12461246
ENCODING("TILE", ENCODING_RM)
12471247
ENCODING("TILEPair", ENCODING_RM)
1248-
errs() << "Unhandled R/M register encoding " << s << "\n";
1248+
errs() << "Unhandled R/M register encoding " << Str << "\n";
12491249
llvm_unreachable("Unhandled R/M register encoding");
12501250
}
12511251

12521252
OperandEncoding
1253-
RecognizableInstr::roRegisterEncodingFromString(const std::string &s,
1254-
uint8_t OpSize) {
1253+
RecognizableInstr::roRegisterEncodingFromString(StringRef Str, uint8_t OpSize) {
12551254
ENCODING("GR16", ENCODING_REG)
12561255
ENCODING("GR16orGR32orGR64", ENCODING_REG)
12571256
ENCODING("GR32", ENCODING_REG)
@@ -1295,12 +1294,12 @@ RecognizableInstr::roRegisterEncodingFromString(const std::string &s,
12951294
ENCODING("BNDR", ENCODING_REG)
12961295
ENCODING("TILE", ENCODING_REG)
12971296
ENCODING("TILEPair", ENCODING_REG)
1298-
errs() << "Unhandled reg/opcode register encoding " << s << "\n";
1297+
errs() << "Unhandled reg/opcode register encoding " << Str << "\n";
12991298
llvm_unreachable("Unhandled reg/opcode register encoding");
13001299
}
13011300

13021301
OperandEncoding
1303-
RecognizableInstr::vvvvRegisterEncodingFromString(const std::string &s,
1302+
RecognizableInstr::vvvvRegisterEncodingFromString(StringRef Str,
13041303
uint8_t OpSize) {
13051304
ENCODING("GR8", ENCODING_VVVV)
13061305
ENCODING("GR16", ENCODING_VVVV)
@@ -1326,12 +1325,12 @@ RecognizableInstr::vvvvRegisterEncodingFromString(const std::string &s,
13261325
ENCODING("VK64", ENCODING_VVVV)
13271326
ENCODING("TILE", ENCODING_VVVV)
13281327
ENCODING("TILEPair", ENCODING_VVVV)
1329-
errs() << "Unhandled VEX.vvvv register encoding " << s << "\n";
1328+
errs() << "Unhandled VEX.vvvv register encoding " << Str << "\n";
13301329
llvm_unreachable("Unhandled VEX.vvvv register encoding");
13311330
}
13321331

13331332
OperandEncoding
1334-
RecognizableInstr::writemaskRegisterEncodingFromString(const std::string &s,
1333+
RecognizableInstr::writemaskRegisterEncodingFromString(StringRef Str,
13351334
uint8_t OpSize) {
13361335
ENCODING("VK1WM", ENCODING_WRITEMASK)
13371336
ENCODING("VK2WM", ENCODING_WRITEMASK)
@@ -1340,13 +1339,12 @@ RecognizableInstr::writemaskRegisterEncodingFromString(const std::string &s,
13401339
ENCODING("VK16WM", ENCODING_WRITEMASK)
13411340
ENCODING("VK32WM", ENCODING_WRITEMASK)
13421341
ENCODING("VK64WM", ENCODING_WRITEMASK)
1343-
errs() << "Unhandled mask register encoding " << s << "\n";
1342+
errs() << "Unhandled mask register encoding " << Str << "\n";
13441343
llvm_unreachable("Unhandled mask register encoding");
13451344
}
13461345

1347-
OperandEncoding
1348-
RecognizableInstr::memoryEncodingFromString(const std::string &s,
1349-
uint8_t OpSize) {
1346+
OperandEncoding RecognizableInstr::memoryEncodingFromString(StringRef Str,
1347+
uint8_t OpSize) {
13501348
ENCODING("i16mem", ENCODING_RM)
13511349
ENCODING("i32mem", ENCODING_RM)
13521350
ENCODING("i64mem", ENCODING_RM)
@@ -1384,13 +1382,12 @@ RecognizableInstr::memoryEncodingFromString(const std::string &s,
13841382
ENCODING("vy64xmem", ENCODING_VSIB)
13851383
ENCODING("vz32mem", ENCODING_VSIB)
13861384
ENCODING("vz64mem", ENCODING_VSIB)
1387-
errs() << "Unhandled memory encoding " << s << "\n";
1385+
errs() << "Unhandled memory encoding " << Str << "\n";
13881386
llvm_unreachable("Unhandled memory encoding");
13891387
}
13901388

13911389
OperandEncoding
1392-
RecognizableInstr::relocationEncodingFromString(const std::string &s,
1393-
uint8_t OpSize) {
1390+
RecognizableInstr::relocationEncodingFromString(StringRef Str, uint8_t OpSize) {
13941391
if (OpSize != X86Local::OpSize16) {
13951392
// For instructions without an OpSize prefix, a declared 16-bit register or
13961393
// immediate encoding is special.
@@ -1434,19 +1431,19 @@ RecognizableInstr::relocationEncodingFromString(const std::string &s,
14341431
ENCODING("dstidx16", ENCODING_DI)
14351432
ENCODING("dstidx32", ENCODING_DI)
14361433
ENCODING("dstidx64", ENCODING_DI)
1437-
errs() << "Unhandled relocation encoding " << s << "\n";
1434+
errs() << "Unhandled relocation encoding " << Str << "\n";
14381435
llvm_unreachable("Unhandled relocation encoding");
14391436
}
14401437

14411438
OperandEncoding
1442-
RecognizableInstr::opcodeModifierEncodingFromString(const std::string &s,
1439+
RecognizableInstr::opcodeModifierEncodingFromString(StringRef Str,
14431440
uint8_t OpSize) {
14441441
ENCODING("GR32", ENCODING_Rv)
14451442
ENCODING("GR64", ENCODING_RO)
14461443
ENCODING("GR16", ENCODING_Rv)
14471444
ENCODING("GR8", ENCODING_RB)
14481445
ENCODING("ccode", ENCODING_CC)
1449-
errs() << "Unhandled opcode modifier encoding " << s << "\n";
1446+
errs() << "Unhandled opcode modifier encoding " << Str << "\n";
14501447
llvm_unreachable("Unhandled opcode modifier encoding");
14511448
}
14521449
#undef ENCODING

‎llvm/utils/TableGen/X86RecognizableInstr.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class RecognizableInstr : public RecognizableInstrBase {
281281
/// If register size does not match OpSize, then
282282
/// register sizes keep their size.
283283
/// @return - The operand's type.
284-
static OperandType typeFromString(const std::string &s, bool hasREX_W,
284+
static OperandType typeFromString(StringRef Str, bool hasREX_W,
285285
uint8_t OpSize);
286286

287287
/// immediateEncodingFromString - Translates an immediate encoding from the
@@ -292,28 +292,28 @@ class RecognizableInstr : public RecognizableInstrBase {
292292
/// @param OpSize - Indicates whether this is an OpSize16 instruction.
293293
/// If it is not, then 16-bit immediate operands stay 16-bit.
294294
/// @return - The operand's encoding.
295-
static OperandEncoding immediateEncodingFromString(const std::string &s,
295+
static OperandEncoding immediateEncodingFromString(StringRef Str,
296296
uint8_t OpSize);
297297

298298
/// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
299299
/// handles operands that are in the REG field of the ModR/M byte.
300-
static OperandEncoding rmRegisterEncodingFromString(const std::string &s,
300+
static OperandEncoding rmRegisterEncodingFromString(StringRef Str,
301301
uint8_t OpSize);
302302

303303
/// rmRegisterEncodingFromString - Like immediateEncodingFromString, but
304304
/// handles operands that are in the REG field of the ModR/M byte.
305-
static OperandEncoding roRegisterEncodingFromString(const std::string &s,
305+
static OperandEncoding roRegisterEncodingFromString(StringRef Str,
306306
uint8_t OpSize);
307-
static OperandEncoding memoryEncodingFromString(const std::string &s,
307+
static OperandEncoding memoryEncodingFromString(StringRef Str,
308308
uint8_t OpSize);
309-
static OperandEncoding relocationEncodingFromString(const std::string &s,
309+
static OperandEncoding relocationEncodingFromString(StringRef Str,
310310
uint8_t OpSize);
311-
static OperandEncoding opcodeModifierEncodingFromString(const std::string &s,
311+
static OperandEncoding opcodeModifierEncodingFromString(StringRef Str,
312312
uint8_t OpSize);
313-
static OperandEncoding vvvvRegisterEncodingFromString(const std::string &s,
313+
static OperandEncoding vvvvRegisterEncodingFromString(StringRef Str,
314314
uint8_t OpSize);
315-
static OperandEncoding
316-
writemaskRegisterEncodingFromString(const std::string &s, uint8_t OpSize);
315+
static OperandEncoding writemaskRegisterEncodingFromString(StringRef Str,
316+
uint8_t OpSize);
317317

318318
/// Adjust the encoding type for an operand based on the instruction.
319319
void adjustOperandEncoding(OperandEncoding &encoding);
@@ -336,12 +336,13 @@ class RecognizableInstr : public RecognizableInstrBase {
336336
/// @param operandMapping - The operand mapping, which has an entry for
337337
/// each operand that indicates whether it is a
338338
/// duplicate, and of what.
339+
using EncodingFn =
340+
llvm::function_ref<OperandEncoding(StringRef s, uint8_t OpSize)>;
339341
void handleOperand(bool optional, unsigned &operandIndex,
340342
unsigned &physicalOperandIndex,
341343
unsigned numPhysicalOperands,
342344
const unsigned *operandMapping,
343-
OperandEncoding (*encodingFromString)(const std::string &,
344-
uint8_t OpSize));
345+
EncodingFn encodingFromString);
345346

346347
/// emitInstructionSpecifier - Loads the instruction specifier for the current
347348
/// instruction into a DisassemblerTables.

0 commit comments

Comments
 (0)
Failed to load comments.