@@ -47,28 +47,30 @@ using namespace llvm;
4747namespace {
4848
4949class CodeEmitterGen {
50- RecordKeeper &Records;
50+ const RecordKeeper &Records;
5151
5252public:
53- CodeEmitterGen (RecordKeeper &R) : Records(R) {}
53+ CodeEmitterGen (const RecordKeeper &R) : Records(R) {}
5454
5555 void run (raw_ostream &o);
5656
5757private:
58- int getVariableBit (const std::string &VarName, BitsInit *BI, int bit);
58+ int getVariableBit (const std::string &VarName, const BitsInit *BI, int bit);
5959 std::pair<std::string, std::string>
60- getInstructionCases (Record *R, CodeGenTarget &Target);
61- void addInstructionCasesForEncoding (Record *R, const Record *EncodingDef,
62- CodeGenTarget &Target, std::string &Case,
60+ getInstructionCases (const Record *R, const CodeGenTarget &Target);
61+ void addInstructionCasesForEncoding (const Record *R,
62+ const Record *EncodingDef,
63+ const CodeGenTarget &Target,
64+ std::string &Case,
6365 std::string &BitOffsetCase);
64- bool addCodeToMergeInOperand (Record *R, BitsInit *BI,
66+ bool addCodeToMergeInOperand (const Record *R, const BitsInit *BI,
6567 const std::string &VarName, std::string &Case,
6668 std::string &BitOffsetCase,
67- CodeGenTarget &Target);
69+ const CodeGenTarget &Target);
6870
6971 void emitInstructionBaseValues (
7072 raw_ostream &o, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
71- CodeGenTarget &Target, unsigned HwMode = DefaultMode);
73+ const CodeGenTarget &Target, unsigned HwMode = DefaultMode);
7274 void
7375 emitCaseMap (raw_ostream &o,
7476 const std::map<std::string, std::vector<std::string>> &CaseMap);
@@ -78,13 +80,13 @@ class CodeEmitterGen {
7880
7981// If the VarBitInit at position 'bit' matches the specified variable then
8082// return the variable bit position. Otherwise return -1.
81- int CodeEmitterGen::getVariableBit (const std::string &VarName, BitsInit *BI,
82- int bit) {
83- if (VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit (bit))) {
84- if (VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar ()))
83+ int CodeEmitterGen::getVariableBit (const std::string &VarName,
84+ const BitsInit *BI, int bit) {
85+ if (const VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit (bit))) {
86+ if (const VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar ()))
8587 if (VI->getName () == VarName)
8688 return VBI->getBitNum ();
87- } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit (bit))) {
89+ } else if (const VarInit *VI = dyn_cast<VarInit>(BI->getBit (bit))) {
8890 if (VI->getName () == VarName)
8991 return 0 ;
9092 }
@@ -93,11 +95,12 @@ int CodeEmitterGen::getVariableBit(const std::string &VarName, BitsInit *BI,
9395}
9496
9597// Returns true if it succeeds, false if an error.
96- bool CodeEmitterGen::addCodeToMergeInOperand (Record *R, BitsInit *BI,
98+ bool CodeEmitterGen::addCodeToMergeInOperand (const Record *R,
99+ const BitsInit *BI,
97100 const std::string &VarName,
98101 std::string &Case,
99102 std::string &BitOffsetCase,
100- CodeGenTarget &Target) {
103+ const CodeGenTarget &Target) {
101104 CodeGenInstruction &CGI = Target.getInstruction (R);
102105
103106 // Determine if VarName actually contributes to the Inst encoding.
@@ -278,7 +281,8 @@ bool CodeEmitterGen::addCodeToMergeInOperand(Record *R, BitsInit *BI,
278281}
279282
280283std::pair<std::string, std::string>
281- CodeEmitterGen::getInstructionCases (Record *R, CodeGenTarget &Target) {
284+ CodeEmitterGen::getInstructionCases (const Record *R,
285+ const CodeGenTarget &Target) {
282286 std::string Case, BitOffsetCase;
283287
284288 auto append = [&](const std::string &S) {
@@ -287,7 +291,7 @@ CodeEmitterGen::getInstructionCases(Record *R, CodeGenTarget &Target) {
287291 };
288292
289293 if (const RecordVal *RV = R->getValue (" EncodingInfos" )) {
290- if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue ())) {
294+ if (const auto *DI = dyn_cast_or_null<DefInit>(RV->getValue ())) {
291295 const CodeGenHwModes &HWM = Target.getHwModes ();
292296 EncodingInfoByHwMode EBM (DI->getDef (), HWM);
293297
@@ -342,7 +346,7 @@ CodeEmitterGen::getInstructionCases(Record *R, CodeGenTarget &Target) {
342346}
343347
344348void CodeEmitterGen::addInstructionCasesForEncoding (
345- Record *R, const Record *EncodingDef, CodeGenTarget &Target,
349+ const Record *R, const Record *EncodingDef, const CodeGenTarget &Target,
346350 std::string &Case, std::string &BitOffsetCase) {
347351 BitsInit *BI = EncodingDef->getValueAsBitsInit (" Inst" );
348352
@@ -394,7 +398,7 @@ static void emitInstBits(raw_ostream &OS, const APInt &Bits) {
394398
395399void CodeEmitterGen::emitInstructionBaseValues (
396400 raw_ostream &o, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
397- CodeGenTarget &Target, unsigned HwMode) {
401+ const CodeGenTarget &Target, unsigned HwMode) {
398402 const CodeGenHwModes &HWM = Target.getHwModes ();
399403 if (HwMode == DefaultMode)
400404 o << " static const uint64_t InstBits[] = {\n " ;
@@ -430,12 +434,12 @@ void CodeEmitterGen::emitInstructionBaseValues(
430434 }
431435 }
432436 }
433- BitsInit *BI = EncodingDef->getValueAsBitsInit (" Inst" );
437+ const BitsInit *BI = EncodingDef->getValueAsBitsInit (" Inst" );
434438
435439 // Start by filling in fixed values.
436440 APInt Value (BitWidth, 0 );
437441 for (unsigned i = 0 , e = BI->getNumBits (); i != e; ++i) {
438- if (auto *B = dyn_cast<BitInit>(BI->getBit (i)); B && B->getValue ())
442+ if (const auto *B = dyn_cast<BitInit>(BI->getBit (i)); B && B->getValue ())
439443 Value.setBit (i);
440444 }
441445 o << " " ;
@@ -448,15 +452,13 @@ void CodeEmitterGen::emitInstructionBaseValues(
448452void CodeEmitterGen::emitCaseMap (
449453 raw_ostream &o,
450454 const std::map<std::string, std::vector<std::string>> &CaseMap) {
451- std::map<std::string, std::vector<std::string>>::const_iterator IE, EE;
452- for (IE = CaseMap.begin (), EE = CaseMap.end (); IE != EE; ++IE) {
453- const std::string &Case = IE->first ;
454- const std::vector<std::string> &InstList = IE->second ;
455-
456- for (int i = 0 , N = InstList.size (); i < N; i++) {
457- if (i)
455+ for (const auto &[Case, InstList] : CaseMap) {
456+ bool First = true ;
457+ for (const auto &Inst : InstList) {
458+ if (!First)
458459 o << " \n " ;
459- o << " case " << InstList[i] << " :" ;
460+ o << " case " << Inst << " :" ;
461+ First = false ;
460462 }
461463 o << " {\n " ;
462464 o << Case;
@@ -469,7 +471,6 @@ void CodeEmitterGen::run(raw_ostream &o) {
469471 emitSourceFileHeader (" Machine Code Emitter" , o);
470472
471473 CodeGenTarget Target (Records);
472- std::vector<Record *> Insts = Records.getAllDerivedDefinitions (" Instruction" );
473474
474475 // For little-endian instruction bit encodings, reverse the bit order
475476 Target.reverseBitsForLittleEndianEncoding ();
@@ -491,17 +492,17 @@ void CodeEmitterGen::run(raw_ostream &o) {
491492 continue ;
492493
493494 if (const RecordVal *RV = R->getValue (" EncodingInfos" )) {
494- if (DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue ())) {
495+ if (const DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue ())) {
495496 EncodingInfoByHwMode EBM (DI->getDef (), HWM);
496- for (auto &KV : EBM) {
497- BitsInit *BI = KV. second ->getValueAsBitsInit (" Inst" );
497+ for (const auto &[Key, Value] : EBM) {
498+ const BitsInit *BI = Value ->getValueAsBitsInit (" Inst" );
498499 BitWidth = std::max (BitWidth, BI->getNumBits ());
499- HwModes.insert (KV. first );
500+ HwModes.insert (Key );
500501 }
501502 continue ;
502503 }
503504 }
504- BitsInit *BI = R->getValueAsBitsInit (" Inst" );
505+ const BitsInit *BI = R->getValueAsBitsInit (" Inst" );
505506 BitWidth = std::max (BitWidth, BI->getNumBits ());
506507 }
507508 UseAPInt = BitWidth > 64 ;
@@ -540,7 +541,7 @@ void CodeEmitterGen::run(raw_ostream &o) {
540541 std::map<std::string, std::vector<std::string>> BitOffsetCaseMap;
541542
542543 // Construct all cases statement for each opcode
543- for (Record *R : Insts ) {
544+ for (const Record *R : Records. getAllDerivedDefinitions ( " Instruction " ) ) {
544545 if (R->getValueAsString (" Namespace" ) == " TargetOpcode" ||
545546 R->getValueAsBit (" isPseudo" ))
546547 continue ;
0 commit comments