@@ -48,30 +48,29 @@ using namespace llvm;
4848namespace {
4949
5050class CodeEmitterGen {
51- const RecordKeeper &Records;
51+ const RecordKeeper &RK;
52+ CodeGenTarget Target;
53+ const CodeGenHwModes &CGH;
5254
5355public:
54- CodeEmitterGen (const RecordKeeper &R) : Records(R) {}
56+ explicit CodeEmitterGen (const RecordKeeper &RK);
5557
5658 void run (raw_ostream &O);
5759
5860private:
5961 int getVariableBit (const std::string &VarName, const BitsInit *BI, int Bit);
60- std::pair<std::string, std::string>
61- getInstructionCases (const Record *R, const CodeGenTarget &Target);
62+ std::pair<std::string, std::string> getInstructionCases (const Record *R);
6263 void addInstructionCasesForEncoding (const Record *R,
6364 const Record *EncodingDef,
64- const CodeGenTarget &Target,
6565 std::string &Case,
6666 std::string &BitOffsetCase);
6767 bool addCodeToMergeInOperand (const Record *R, const BitsInit *BI,
6868 const std::string &VarName, std::string &Case,
69- std::string &BitOffsetCase,
70- const CodeGenTarget &Target);
69+ std::string &BitOffsetCase);
7170
7271 void emitInstructionBaseValues (
7372 raw_ostream &O, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
74- const CodeGenTarget &Target, unsigned HwMode = DefaultMode);
73+ unsigned HwMode = DefaultMode);
7574 void
7675 emitCaseMap (raw_ostream &O,
7776 const std::map<std::string, std::vector<std::string>> &CaseMap);
@@ -102,8 +101,7 @@ bool CodeEmitterGen::addCodeToMergeInOperand(const Record *R,
102101 const BitsInit *BI,
103102 const std::string &VarName,
104103 std::string &Case,
105- std::string &BitOffsetCase,
106- const CodeGenTarget &Target) {
104+ std::string &BitOffsetCase) {
107105 CodeGenInstruction &CGI = Target.getInstruction (R);
108106
109107 // Determine if VarName actually contributes to the Inst encoding.
@@ -277,8 +275,7 @@ bool CodeEmitterGen::addCodeToMergeInOperand(const Record *R,
277275}
278276
279277std::pair<std::string, std::string>
280- CodeEmitterGen::getInstructionCases (const Record *R,
281- const CodeGenTarget &Target) {
278+ CodeEmitterGen::getInstructionCases (const Record *R) {
282279 std::string Case, BitOffsetCase;
283280
284281 auto Append = [&](const std::string &S) {
@@ -287,8 +284,7 @@ CodeEmitterGen::getInstructionCases(const Record *R,
287284 };
288285
289286 if (const Record *RV = R->getValueAsOptionalDef (" EncodingInfos" )) {
290- const CodeGenHwModes &HWM = Target.getHwModes ();
291- EncodingInfoByHwMode EBM (RV, HWM);
287+ EncodingInfoByHwMode EBM (RV, CGH);
292288
293289 // Invoke the interface to obtain the HwMode ID controlling the
294290 // EncodingInfo for the current subtarget. This interface will
@@ -304,7 +300,7 @@ CodeEmitterGen::getInstructionCases(const Record *R,
304300 " case " + itostr (DefaultMode) + " : InstBitsByHw = InstBits" ;
305301 } else {
306302 Case += " case " + itostr (ModeId) + " : InstBitsByHw = InstBits_" +
307- HWM .getMode (ModeId).Name .str ();
303+ CGH .getMode (ModeId).Name .str ();
308304 }
309305 Case += " ; break;\n " ;
310306 }
@@ -326,20 +322,20 @@ CodeEmitterGen::getInstructionCases(const Record *R,
326322 Append (" default: llvm_unreachable(\" Unhandled HwMode\" );\n " );
327323 for (auto &[ModeId, Encoding] : EBM) {
328324 Append (" case " + itostr (ModeId) + " : {\n " );
329- addInstructionCasesForEncoding (R, Encoding, Target, Case, BitOffsetCase);
325+ addInstructionCasesForEncoding (R, Encoding, Case, BitOffsetCase);
330326 Append (" break;\n " );
331327 Append (" }\n " );
332328 }
333329 Append (" }\n " );
334330 return {std::move (Case), std::move (BitOffsetCase)};
335331 }
336- addInstructionCasesForEncoding (R, R, Target, Case, BitOffsetCase);
332+ addInstructionCasesForEncoding (R, R, Case, BitOffsetCase);
337333 return {std::move (Case), std::move (BitOffsetCase)};
338334}
339335
340336void CodeEmitterGen::addInstructionCasesForEncoding (
341- const Record *R, const Record *EncodingDef, const CodeGenTarget &Target ,
342- std::string &Case, std::string & BitOffsetCase) {
337+ const Record *R, const Record *EncodingDef, std::string &Case ,
338+ std::string &BitOffsetCase) {
343339 const BitsInit *BI = EncodingDef->getValueAsBitsInit (" Inst" );
344340
345341 // Loop over all of the fields in the instruction, determining which are the
@@ -354,8 +350,8 @@ void CodeEmitterGen::addInstructionCasesForEncoding(
354350 if (RV.isNonconcreteOK () || RV.getValue ()->isComplete ())
355351 continue ;
356352
357- Success &= addCodeToMergeInOperand (R, BI, RV. getName (). str (), Case,
358- BitOffsetCase, Target );
353+ Success &=
354+ addCodeToMergeInOperand (R, BI, RV. getName (). str (), Case, BitOffsetCase );
359355 }
360356 // Avoid empty switches.
361357 if (BitOffsetCase.size () == BitOffsetCaseSizeBeforeLoop)
@@ -389,19 +385,18 @@ static void emitInstBits(raw_ostream &OS, const APInt &Bits) {
389385
390386void CodeEmitterGen::emitInstructionBaseValues (
391387 raw_ostream &O, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
392- const CodeGenTarget &Target, unsigned HwMode) {
393- const CodeGenHwModes &HWM = Target.getHwModes ();
388+ unsigned HwMode) {
394389 if (HwMode == DefaultMode)
395390 O << " static const uint64_t InstBits[] = {\n " ;
396391 else
397- O << " static const uint64_t InstBits_"
398- << HWM. getModeName (HwMode, /* IncludeDefault= */ true ) << " [] = {\n " ;
392+ O << " static const uint64_t InstBits_" << CGH. getModeName (HwMode)
393+ << " [] = {\n " ;
399394
400395 for (const CodeGenInstruction *CGI : NumberedInstructions) {
401396 const Record *R = CGI->TheDef ;
402397 const Record *EncodingDef = R;
403398 if (const Record *RV = R->getValueAsOptionalDef (" EncodingInfos" )) {
404- EncodingInfoByHwMode EBM (RV, HWM );
399+ EncodingInfoByHwMode EBM (RV, CGH );
405400 if (EBM.hasMode (HwMode)) {
406401 EncodingDef = EBM.get (HwMode);
407402 } else {
@@ -447,29 +442,29 @@ void CodeEmitterGen::emitCaseMap(
447442 }
448443}
449444
445+ CodeEmitterGen::CodeEmitterGen (const RecordKeeper &RK)
446+ : RK(RK), Target(RK), CGH(Target.getHwModes()) {
447+ // For little-endian instruction bit encodings, reverse the bit order.
448+ Target.reverseBitsForLittleEndianEncoding ();
449+ }
450+
450451void CodeEmitterGen::run (raw_ostream &O) {
451452 emitSourceFileHeader (" Machine Code Emitter" , O);
452453
453- CodeGenTarget Target (Records);
454-
455- // For little-endian instruction bit encodings, reverse the bit order
456- Target.reverseBitsForLittleEndianEncoding ();
457-
458454 ArrayRef<const CodeGenInstruction *> EncodedInstructions =
459455 Target.getTargetNonPseudoInstructions ();
460456
461457 if (Target.hasVariableLengthEncodings ()) {
462- emitVarLenCodeEmitter (Records , O);
458+ emitVarLenCodeEmitter (RK , O);
463459 return ;
464460 }
465- const CodeGenHwModes &HWM = Target.getHwModes ();
466461 // The set of HwModes used by instruction encodings.
467462 std::set<unsigned > HwModes;
468463 BitWidth = 0 ;
469464 for (const CodeGenInstruction *CGI : EncodedInstructions) {
470465 const Record *R = CGI->TheDef ;
471466 if (const Record *RV = R->getValueAsOptionalDef (" EncodingInfos" )) {
472- EncodingInfoByHwMode EBM (RV, HWM );
467+ EncodingInfoByHwMode EBM (RV, CGH );
473468 for (const auto &[Key, Value] : EBM) {
474469 const BitsInit *BI = Value->getValueAsBitsInit (" Inst" );
475470 BitWidth = std::max (BitWidth, BI->getNumBits ());
@@ -498,13 +493,13 @@ void CodeEmitterGen::run(raw_ostream &O) {
498493 }
499494
500495 // Emit instruction base values
501- emitInstructionBaseValues (O, EncodedInstructions, Target, DefaultMode);
496+ emitInstructionBaseValues (O, EncodedInstructions, DefaultMode);
502497 if (!HwModes.empty ()) {
503498 // Emit table for instrs whose encodings are controlled by HwModes.
504499 for (unsigned HwMode : HwModes) {
505500 if (HwMode == DefaultMode)
506501 continue ;
507- emitInstructionBaseValues (O, EncodedInstructions, Target, HwMode);
502+ emitInstructionBaseValues (O, EncodedInstructions, HwMode);
508503 }
509504
510505 // This pointer will be assigned to the HwMode table later.
@@ -521,7 +516,7 @@ void CodeEmitterGen::run(raw_ostream &O) {
521516 std::string InstName =
522517 (R->getValueAsString (" Namespace" ) + " ::" + R->getName ()).str ();
523518 std::string Case, BitOffsetCase;
524- std::tie (Case, BitOffsetCase) = getInstructionCases (R, Target );
519+ std::tie (Case, BitOffsetCase) = getInstructionCases (R);
525520
526521 CaseMap[Case].push_back (InstName);
527522 BitOffsetCaseMap[BitOffsetCase].push_back (std::move (InstName));
0 commit comments