@@ -67,7 +67,7 @@ using namespace llvm;
67
67
namespace {
68
68
69
69
class VarLenCodeEmitterGen {
70
- RecordKeeper &Records;
70
+ const RecordKeeper &Records;
71
71
72
72
// Representaton of alternative encodings used for HwModes.
73
73
using AltEncodingTy = int ;
@@ -83,15 +83,16 @@ class VarLenCodeEmitterGen {
83
83
void emitInstructionBaseValues (
84
84
raw_ostream &OS,
85
85
ArrayRef<const CodeGenInstruction *> NumberedInstructions,
86
- CodeGenTarget &Target, AltEncodingTy Mode);
86
+ const CodeGenTarget &Target, AltEncodingTy Mode);
87
87
88
- std::string getInstructionCases (Record *R, CodeGenTarget &Target);
89
- std::string getInstructionCaseForEncoding (Record *R, AltEncodingTy Mode,
88
+ std::string getInstructionCases (const Record *R, const CodeGenTarget &Target);
89
+ std::string getInstructionCaseForEncoding (const Record *R, AltEncodingTy Mode,
90
90
const VarLenInst &VLI,
91
- CodeGenTarget &Target, int I);
91
+ const CodeGenTarget &Target,
92
+ int Indent);
92
93
93
94
public:
94
- explicit VarLenCodeEmitterGen (RecordKeeper &R) : Records(R) {}
95
+ explicit VarLenCodeEmitterGen (const RecordKeeper &R) : Records(R) {}
95
96
96
97
void run (raw_ostream &OS);
97
98
};
@@ -222,7 +223,6 @@ void VarLenInst::buildRec(const DagInit *DI) {
222
223
223
224
void VarLenCodeEmitterGen::run (raw_ostream &OS) {
224
225
CodeGenTarget Target (Records);
225
- auto Insts = Records.getAllDerivedDefinitions (" Instruction" );
226
226
227
227
auto NumberedInstructions = Target.getInstructionsByEnumValue ();
228
228
@@ -238,10 +238,8 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
238
238
if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue ())) {
239
239
const CodeGenHwModes &HWM = Target.getHwModes ();
240
240
EncodingInfoByHwMode EBM (DI->getDef (), HWM);
241
- for (auto &KV : EBM) {
242
- AltEncodingTy Mode = KV.first ;
241
+ for (const auto [Mode, EncodingDef] : EBM) {
243
242
Modes.insert ({Mode, " _" + HWM.getMode (Mode).Name .str ()});
244
- const Record *EncodingDef = KV.second ;
245
243
const RecordVal *RV = EncodingDef->getValue (" Inst" );
246
244
DagInit *DI = cast<DagInit>(RV->getValue ());
247
245
VarLenInsts[R].insert ({Mode, VarLenInst (DI, RV)});
@@ -250,7 +248,7 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
250
248
}
251
249
}
252
250
const RecordVal *RV = R->getValue (" Inst" );
253
- DagInit *DI = cast<DagInit>(RV->getValue ());
251
+ const DagInit *DI = cast<DagInit>(RV->getValue ());
254
252
VarLenInsts[R].insert ({Universal, VarLenInst (DI, RV)});
255
253
}
256
254
@@ -291,7 +289,7 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
291
289
std::map<std::string, std::vector<std::string>> CaseMap;
292
290
293
291
// Construct all cases statement for each opcode
294
- for (Record *R : Insts ) {
292
+ for (const Record *R : Records. getAllDerivedDefinitions ( " Instruction " ) ) {
295
293
if (R->getValueAsString (" Namespace" ) == " TargetOpcode" ||
296
294
R->getValueAsBit (" isPseudo" ))
297
295
continue ;
@@ -347,7 +345,7 @@ static void emitInstBits(raw_ostream &IS, raw_ostream &SS, const APInt &Bits,
347
345
348
346
void VarLenCodeEmitterGen::emitInstructionBaseValues (
349
347
raw_ostream &OS, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
350
- CodeGenTarget &Target, AltEncodingTy Mode) {
348
+ const CodeGenTarget &Target, AltEncodingTy Mode) {
351
349
std::string IndexArray, StorageArray;
352
350
raw_string_ostream IS (IndexArray), SS (StorageArray);
353
351
@@ -408,8 +406,9 @@ void VarLenCodeEmitterGen::emitInstructionBaseValues(
408
406
OS << IndexArray << StorageArray;
409
407
}
410
408
411
- std::string VarLenCodeEmitterGen::getInstructionCases (Record *R,
412
- CodeGenTarget &Target) {
409
+ std::string
410
+ VarLenCodeEmitterGen::getInstructionCases (const Record *R,
411
+ const CodeGenTarget &Target) {
413
412
auto It = VarLenInsts.find (R);
414
413
if (It == VarLenInsts.end ())
415
414
PrintFatalError (R, " Parsed encoding record not found" );
@@ -421,7 +420,8 @@ std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
421
420
// Universal, just pick the first mode.
422
421
AltEncodingTy Mode = Modes.begin ()->first ;
423
422
const auto &Encoding = Map.begin ()->second ;
424
- return getInstructionCaseForEncoding (R, Mode, Encoding, Target, 6 );
423
+ return getInstructionCaseForEncoding (R, Mode, Encoding, Target,
424
+ /* Indent=*/ 6 );
425
425
}
426
426
427
427
std::string Case;
@@ -434,8 +434,8 @@ std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
434
434
Case +=
435
435
" llvm_unreachable(\" Undefined encoding in this mode\" );\n " ;
436
436
} else {
437
- Case +=
438
- getInstructionCaseForEncoding (R, It-> first , It-> second , Target, 8 );
437
+ Case += getInstructionCaseForEncoding (R, It-> first , It-> second , Target,
438
+ /* Indent= */ 8 );
439
439
}
440
440
Case += " break;\n " ;
441
441
Case += " }\n " ;
@@ -445,15 +445,14 @@ std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
445
445
}
446
446
447
447
std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding (
448
- Record *R, AltEncodingTy Mode, const VarLenInst &VLI, CodeGenTarget &Target,
449
- int I) {
450
-
448
+ const Record *R, AltEncodingTy Mode, const VarLenInst &VLI,
449
+ const CodeGenTarget &Target, int Indent) {
451
450
CodeGenInstruction &CGI = Target.getInstruction (R);
452
451
453
452
std::string Case;
454
453
raw_string_ostream SS (Case);
455
454
// Populate based value.
456
- SS.indent (I ) << " Inst = getInstBits" << Modes[Mode] << " (opcode);\n " ;
455
+ SS.indent (Indent ) << " Inst = getInstBits" << Modes[Mode] << " (opcode);\n " ;
457
456
458
457
// Process each segment in VLI.
459
458
size_t Offset = 0U ;
@@ -482,19 +481,21 @@ std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
482
481
if (ES.CustomEncoder .size ())
483
482
CustomEncoder = ES.CustomEncoder ;
484
483
485
- SS.indent (I ) << " Scratch.clearAllBits();\n " ;
486
- SS.indent (I ) << " // op: " << OperandName.drop_front (1 ) << " \n " ;
484
+ SS.indent (Indent ) << " Scratch.clearAllBits();\n " ;
485
+ SS.indent (Indent ) << " // op: " << OperandName.drop_front (1 ) << " \n " ;
487
486
if (CustomEncoder.empty ())
488
- SS.indent (I ) << " getMachineOpValue(MI, MI.getOperand("
489
- << utostr (FlatOpIdx) << " )" ;
487
+ SS.indent (Indent ) << " getMachineOpValue(MI, MI.getOperand("
488
+ << utostr (FlatOpIdx) << " )" ;
490
489
else
491
- SS.indent (I) << CustomEncoder << " (MI, /*OpIdx=*/" << utostr (FlatOpIdx);
490
+ SS.indent (Indent) << CustomEncoder << " (MI, /*OpIdx=*/"
491
+ << utostr (FlatOpIdx);
492
492
493
493
SS << " , /*Pos=*/" << utostr (Offset) << " , Scratch, Fixups, STI);\n " ;
494
494
495
- SS.indent (I) << " Inst.insertBits(" << " Scratch.extractBits("
496
- << utostr (NumBits) << " , " << utostr (LoBit) << " )" << " , "
497
- << Offset << " );\n " ;
495
+ SS.indent (Indent) << " Inst.insertBits("
496
+ << " Scratch.extractBits(" << utostr (NumBits) << " , "
497
+ << utostr (LoBit) << " )"
498
+ << " , " << Offset << " );\n " ;
498
499
499
500
HighScratchAccess = std::max (HighScratchAccess, NumBits + LoBit);
500
501
}
@@ -503,24 +504,20 @@ std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
503
504
504
505
StringRef PostEmitter = R->getValueAsString (" PostEncoderMethod" );
505
506
if (!PostEmitter.empty ())
506
- SS.indent (I ) << " Inst = " << PostEmitter << " (MI, Inst, STI);\n " ;
507
+ SS.indent (Indent ) << " Inst = " << PostEmitter << " (MI, Inst, STI);\n " ;
507
508
508
509
// Resize the scratch buffer if it's to small.
509
510
std::string ScratchResizeStr;
510
511
if (VLI.size () && !VLI.isFixedValueOnly ()) {
511
512
raw_string_ostream RS (ScratchResizeStr);
512
- RS.indent (I ) << " if (Scratch.getBitWidth() < " << HighScratchAccess
513
- << " ) { Scratch = Scratch.zext(" << HighScratchAccess
514
- << " ); }\n " ;
513
+ RS.indent (Indent ) << " if (Scratch.getBitWidth() < " << HighScratchAccess
514
+ << " ) { Scratch = Scratch.zext(" << HighScratchAccess
515
+ << " ); }\n " ;
515
516
}
516
517
517
518
return ScratchResizeStr + Case;
518
519
}
519
520
520
- namespace llvm {
521
-
522
- void emitVarLenCodeEmitter (RecordKeeper &R, raw_ostream &OS) {
521
+ void llvm::emitVarLenCodeEmitter (const RecordKeeper &R, raw_ostream &OS) {
523
522
VarLenCodeEmitterGen (R).run (OS);
524
523
}
525
-
526
- } // end namespace llvm
0 commit comments