1//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// CodeEmitterGen uses the descriptions of instructions and their fields to
10// construct an automated code emitter: a function called
11// getBinaryCodeForInstr() that, given a MCInst, returns the value of the
12// instruction - either as an uint64_t or as an APInt, depending on the
13// maximum bit width of all Inst definitions.
14//
15// In addition, it generates another function called getOperandBitOffset()
16// that, given a MCInst and an operand index, returns the minimum of indices of
17// all bits that carry some portion of the respective operand. When the target's
18// encodeInstruction() stores the instruction in a little-endian byte order, the
19// returned value is the offset of the start of the operand in the encoded
20// instruction. Other targets might need to adjust the returned value according
21// to their encodeInstruction() implementation.
22//
23//===----------------------------------------------------------------------===//
24
25#include "Common/CodeGenHwModes.h"
26#include "Common/CodeGenInstruction.h"
27#include "Common/CodeGenTarget.h"
28#include "Common/InfoByHwMode.h"
29#include "Common/VarLenCodeEmitterGen.h"
30#include "llvm/ADT/APInt.h"
31#include "llvm/ADT/ArrayRef.h"
32#include "llvm/ADT/DenseMap.h"
33#include "llvm/ADT/StringExtras.h"
34#include "llvm/Support/Casting.h"
35#include "llvm/Support/Format.h"
36#include "llvm/Support/FormatVariadic.h"
37#include "llvm/Support/MathExtras.h"
38#include "llvm/Support/raw_ostream.h"
39#include "llvm/TableGen/CodeGenHelpers.h"
40#include "llvm/TableGen/Error.h"
41#include "llvm/TableGen/Record.h"
42#include "llvm/TableGen/TableGenBackend.h"
43#include <cstdint>
44#include <map>
45#include <set>
46#include <string>
47#include <utility>
48#include <vector>
49
50using namespace llvm;
51
52namespace {
53
54// A map of uniqued case statements. The key is the body of the case statement
55// and the value is a list of cases which share the same body.
56using CaseMapT = std::map<std::string, std::vector<unsigned>>;
57
58struct BaseEncodingPool {
59 std::vector<APInt> Values;
60 std::map<unsigned, std::vector<unsigned>> IndicesByHwMode;
61 unsigned IndexBitWidth = 0;
62
63 bool empty() const { return Values.empty(); }
64};
65
66class CodeEmitterGen {
67 const RecordKeeper &RK;
68 CodeGenTarget Target;
69 const CodeGenHwModes &CGH;
70
71public:
72 explicit CodeEmitterGen(const RecordKeeper &RK);
73
74 void run(raw_ostream &O);
75
76private:
77 int getVariableBit(const std::string &VarName,
78 ArrayRef<const Init *> InstBits, int Bit);
79 std::pair<std::string, std::string> getInstructionCases(const Record *R);
80 void addInstructionCasesForEncoding(const Record *R,
81 const Record *EncodingDef,
82 std::string &Case,
83 std::string &BitOffsetCase);
84 bool addCodeToMergeInOperand(const Record *R, ArrayRef<const Init *> InstBits,
85 const std::string &VarName, std::string &Case,
86 std::string &BitOffsetCase);
87
88 APInt getInstructionBaseValue(const CodeGenInstruction *CGI, unsigned HwMode);
89 void buildBaseEncodingPool(
90 ArrayRef<const CodeGenInstruction *> NumberedInstructions,
91 const std::set<unsigned> &HwModes);
92 void emitBaseEncodingPool(raw_ostream &O);
93 void emitInstructionBaseValues(
94 raw_ostream &O, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
95 unsigned HwMode = DefaultMode);
96 BaseEncodingPool BaseEncodings;
97 unsigned BitWidth = 0u;
98 bool UseAPInt = false;
99};
100
101} // end anonymous namespace
102
103// If the VarBitInit at position 'bit' matches the specified variable then
104// return the variable bit position. Otherwise return -1.
105int CodeEmitterGen::getVariableBit(const std::string &VarName,
106 ArrayRef<const Init *> InstBits, int Bit) {
107 if (const VarBitInit *VBI = dyn_cast<VarBitInit>(Val: InstBits[Bit])) {
108 if (const VarInit *VI = dyn_cast<VarInit>(Val: VBI->getBitVar()))
109 if (VI->getName() == VarName)
110 return VBI->getBitNum();
111 } else if (const VarInit *VI = dyn_cast<VarInit>(Val: InstBits[Bit])) {
112 if (VI->getName() == VarName)
113 return 0;
114 }
115
116 return -1;
117}
118
119// Returns true if it succeeds, false if an error.
120bool CodeEmitterGen::addCodeToMergeInOperand(const Record *R,
121 ArrayRef<const Init *> InstBits,
122 const std::string &VarName,
123 std::string &Case,
124 std::string &BitOffsetCase) {
125 const CodeGenInstruction &CGI = Target.getInstruction(InstRec: R);
126
127 // Determine if VarName actually contributes to the Inst encoding.
128 int Bit = InstBits.size() - 1;
129
130 // Scan for a bit that this contributed to.
131 for (; Bit >= 0;) {
132 if (getVariableBit(VarName, InstBits, Bit) != -1)
133 break;
134
135 --Bit;
136 }
137
138 // If we found no bits, ignore this value, otherwise emit the call to get the
139 // operand encoding.
140 if (Bit < 0)
141 return true;
142
143 // If the operand matches by name, reference according to that
144 // operand number. Non-matching operands are assumed to be in
145 // order.
146 unsigned OpIdx;
147 if (auto SubOp = CGI.Operands.findSubOperandAlias(Name: VarName)) {
148 OpIdx = CGI.Operands[SubOp->first].MIOperandNo + SubOp->second;
149 } else if (auto MayBeOpIdx = CGI.Operands.findOperandNamed(Name: VarName)) {
150 // Get the machine operand number for the indicated operand.
151 OpIdx = CGI.Operands[*MayBeOpIdx].MIOperandNo;
152 } else {
153 PrintError(Rec: R, Msg: Twine("No operand named ") + VarName + " in record " +
154 R->getName());
155 return false;
156 }
157
158 std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(Op: OpIdx);
159 StringRef EncoderMethodName =
160 CGI.Operands[SO.first].EncoderMethodNames[SO.second];
161
162 raw_string_ostream OS(Case);
163 indent Indent(6);
164
165 OS << Indent << "// op: " << VarName << '\n';
166
167 if (UseAPInt)
168 OS << Indent << "op.clearAllBits();\n";
169
170 if (!EncoderMethodName.empty()) {
171 if (UseAPInt)
172 OS << Indent << EncoderMethodName << "(MI, " << OpIdx
173 << ", op, Fixups, STI);\n";
174 else
175 OS << Indent << "op = " << EncoderMethodName << "(MI, " << OpIdx
176 << ", Fixups, STI);\n";
177 } else {
178 if (UseAPInt)
179 OS << Indent << "getMachineOpValue(MI, MI.getOperand(" << OpIdx
180 << "), op, Fixups, STI);\n";
181 else
182 OS << Indent << "op = getMachineOpValue(MI, MI.getOperand(" << OpIdx
183 << "), Fixups, STI);\n";
184 }
185
186 unsigned BitOffset = -1;
187 for (; Bit >= 0;) {
188 int VarBit = getVariableBit(VarName, InstBits, Bit);
189
190 // If this bit isn't from a variable, skip it.
191 if (VarBit == -1) {
192 --Bit;
193 continue;
194 }
195
196 // Figure out the consecutive range of bits covered by this operand, in
197 // order to generate better encoding code.
198 int BeginInstBit = Bit;
199 int BeginVarBit = VarBit;
200 int N = 1;
201 for (--Bit; Bit >= 0;) {
202 VarBit = getVariableBit(VarName, InstBits, Bit);
203 if (VarBit == -1 || VarBit != (BeginVarBit - N))
204 break;
205 ++N;
206 --Bit;
207 }
208
209 unsigned LoBit = BeginVarBit - N + 1;
210 unsigned LoInstBit = BeginInstBit - N + 1;
211 BitOffset = LoInstBit;
212 if (UseAPInt) {
213 if (N > 64)
214 OS << Indent << "Value.insertBits(op.extractBits(" << N << ", " << LoBit
215 << "), " << LoInstBit << ");\n";
216 else
217 OS << Indent << "Value.insertBits(op.extractBitsAsZExtValue(" << N
218 << ", " << LoBit << "), " << LoInstBit << ", " << N << ");\n";
219 } else {
220 uint64_t OpMask = maskTrailingOnes<uint64_t>(N) << LoBit;
221 OS << Indent << "Value |= (op & " << format_hex(N: OpMask, Width: 0) << ')';
222 int OpShift = BeginInstBit - BeginVarBit;
223 if (OpShift > 0)
224 OS << " << " << OpShift;
225 else if (OpShift < 0)
226 OS << " >> " << -OpShift;
227 OS << ";\n";
228 }
229 }
230
231 if (BitOffset != (unsigned)-1) {
232 BitOffsetCase += " case " + utostr(X: OpIdx) + ":\n";
233 BitOffsetCase += " // op: " + VarName + "\n";
234 BitOffsetCase += " return " + utostr(X: BitOffset) + ";\n";
235 }
236
237 return true;
238}
239
240static void emitCaseMap(raw_ostream &O, const CaseMapT &CaseMap,
241 function_ref<void(raw_ostream &, unsigned)> PrintCase) {
242 for (const auto &[CaseBody, Cases] : CaseMap) {
243 ListSeparator LS("\n");
244 for (unsigned Case : Cases) {
245 O << LS << " case ";
246 PrintCase(O, Case);
247 O << ":";
248 }
249 O << " {\n";
250 O << CaseBody;
251 O << " break;\n"
252 << " }\n";
253 }
254}
255
256std::pair<std::string, std::string>
257CodeEmitterGen::getInstructionCases(const Record *R) {
258 std::string Case, BitOffsetCase;
259
260 auto Append = [&](const std::string &S) {
261 Case += S;
262 BitOffsetCase += S;
263 };
264
265 if (const Record *RV = R->getValueAsOptionalDef(FieldName: "EncodingInfos")) {
266 EncodingInfoByHwMode EBM(RV, CGH);
267
268 // Invoke the interface to obtain the HwMode ID controlling the
269 // EncodingInfo for the current subtarget. This interface will
270 // mask off irrelevant HwMode IDs.
271 Append(" unsigned HwMode = "
272 "STI.getHwMode(MCSubtargetInfo::HwMode_EncodingInfo);\n");
273 Case += " switch (HwMode) {\n";
274 Case += " default: llvm_unreachable(\"Unknown hardware mode!\"); "
275 "break;\n";
276 std::string InstBitsTableName =
277 BaseEncodings.empty() ? "InstBits" : "InstBitsIndices";
278 for (auto &[ModeId, Encoding] : EBM) {
279 if (ModeId == DefaultMode) {
280 Case += " case " + itostr(X: DefaultMode) + ": " + InstBitsTableName +
281 "ByHw = " + InstBitsTableName;
282 } else {
283 Case += " case " + itostr(X: ModeId) + ": " + InstBitsTableName +
284 "ByHw = " + InstBitsTableName + "_" +
285 CGH.getModeName(Id: ModeId).str();
286 }
287 Case += "; break;\n";
288 }
289 Case += " };\n";
290
291 // Reload Inst from the selected HwMode table.
292 if (UseAPInt) {
293 if (BaseEncodings.empty()) {
294 int NumWords = APInt::getNumWords(BitWidth);
295 Case += " Inst = APInt(" + itostr(X: BitWidth);
296 Case += ", ArrayRef(InstBitsByHw + TableIndex * " + itostr(X: NumWords) +
297 ", " + itostr(X: NumWords);
298 Case += "));\n";
299 } else {
300 Case += " InstBitsIndex = InstBitsIndicesByHw[TableIndex];\n";
301 Case += " Inst = APInt(" + itostr(X: BitWidth) +
302 ", ArrayRef(InstBits[InstBitsIndex]));\n";
303 }
304 Case += " Value = Inst;\n";
305 } else {
306 Case += " Value = InstBitsByHw[TableIndex];\n";
307 }
308
309 Append(" switch (HwMode) {\n");
310 Append(" default: llvm_unreachable(\"Unhandled HwMode\");\n");
311
312 // Attempt to unique the per-hw-mode encoding case statements. This helps
313 // reduce the code size if 2 or more hw-modes share the same encoding for
314 // the fields of the instruction.
315 CaseMapT CaseMap, BitOffsetCaseMap;
316 std::string ModeCase, ModeBitOffsetCase;
317
318 auto PrintHWMode = [](raw_ostream &O, unsigned Mode) { O << Mode; };
319
320 for (auto &[ModeId, Encoding] : EBM) {
321 ModeCase.clear();
322 ModeBitOffsetCase.clear();
323 addInstructionCasesForEncoding(R, EncodingDef: Encoding, Case&: ModeCase, BitOffsetCase&: ModeBitOffsetCase);
324 CaseMap[ModeCase].push_back(x: ModeId);
325 BitOffsetCaseMap[ModeBitOffsetCase].push_back(x: ModeId);
326 }
327
328 raw_string_ostream CaseOS(Case);
329 raw_string_ostream BitOffsetCaseOS(BitOffsetCase);
330 emitCaseMap(O&: CaseOS, CaseMap, PrintCase: PrintHWMode);
331 emitCaseMap(O&: BitOffsetCaseOS, CaseMap: BitOffsetCaseMap, PrintCase: PrintHWMode);
332
333 Append(" }\n");
334 return {std::move(Case), std::move(BitOffsetCase)};
335 }
336 addInstructionCasesForEncoding(R, EncodingDef: R, Case, BitOffsetCase);
337 return {std::move(Case), std::move(BitOffsetCase)};
338}
339
340void CodeEmitterGen::addInstructionCasesForEncoding(
341 const Record *R, const Record *EncodingDef, std::string &Case,
342 std::string &BitOffsetCase) {
343 ArrayRef<const Init *> InstBits =
344 Target.getInstructionEncodingBits(EncodingDef);
345
346 // Loop over all of the fields in the instruction, determining which are the
347 // operands to the instruction.
348 bool Success = true;
349 size_t OrigBitOffsetCaseSize = BitOffsetCase.size();
350 BitOffsetCase += " switch (OpNum) {\n";
351 size_t BitOffsetCaseSizeBeforeLoop = BitOffsetCase.size();
352 for (const RecordVal &RV : EncodingDef->getValues()) {
353 // Ignore fixed fields in the record, we're looking for values like:
354 // bits<5> RST = { ?, ?, ?, ?, ? };
355 if (RV.isNonconcreteOK() || RV.getValue()->isComplete())
356 continue;
357
358 Success &= addCodeToMergeInOperand(R, InstBits, VarName: RV.getName().str(), Case,
359 BitOffsetCase);
360 }
361 // Avoid empty switches.
362 if (BitOffsetCase.size() == BitOffsetCaseSizeBeforeLoop)
363 BitOffsetCase.resize(n: OrigBitOffsetCaseSize);
364 else
365 BitOffsetCase += " }\n";
366
367 if (!Success) {
368 // Dump the record, so we can see what's going on...
369 std::string E;
370 raw_string_ostream S(E);
371 S << "Dumping record for previous error:\n";
372 S << *R;
373 PrintNote(Msg: E);
374 }
375
376 StringRef PostEmitter = R->getValueAsString(FieldName: "PostEncoderMethod");
377 if (!PostEmitter.empty()) {
378 Case += " Value = ";
379 Case += PostEmitter;
380 Case += "(MI, Value";
381 Case += ", STI";
382 Case += ");\n";
383 }
384}
385
386static void emitInstBits(raw_ostream &OS, const APInt &Bits) {
387 for (unsigned I = 0; I < Bits.getNumWords(); ++I)
388 OS << ((I > 0) ? ", " : "") << "UINT64_C(" << Bits.getRawData()[I] << ")";
389}
390
391APInt CodeEmitterGen::getInstructionBaseValue(const CodeGenInstruction *CGI,
392 unsigned HwMode) {
393 const Record *R = CGI->TheDef;
394 const Record *EncodingDef = R;
395 if (const Record *RV = R->getValueAsOptionalDef(FieldName: "EncodingInfos")) {
396 EncodingInfoByHwMode EBM(RV, CGH);
397 if (!EBM.hasMode(M: HwMode))
398 return APInt(BitWidth, 0);
399 EncodingDef = EBM.get(Mode: HwMode);
400 }
401
402 ArrayRef<const Init *> InstBits =
403 Target.getInstructionEncodingBits(EncodingDef);
404 APInt Value(BitWidth, 0);
405 for (auto [I, Bit] : enumerate(First&: InstBits)) {
406 if (const auto *B = dyn_cast<BitInit>(Val: Bit); B && B->getValue())
407 Value.setBit(I);
408 }
409 return Value;
410}
411
412void CodeEmitterGen::buildBaseEncodingPool(
413 ArrayRef<const CodeGenInstruction *> NumberedInstructions,
414 const std::set<unsigned> &HwModes) {
415 assert(UseAPInt && "pooling is only used for multiword encodings");
416
417 const unsigned NumWords = APInt::getNumWords(BitWidth);
418 DenseMap<APInt, unsigned> ValueToIndex;
419 BaseEncodings.IndicesByHwMode.try_emplace(k: DefaultMode);
420 for (unsigned HwMode : HwModes)
421 BaseEncodings.IndicesByHwMode.try_emplace(k: HwMode);
422
423 for (auto &[HwMode, Indices] : BaseEncodings.IndicesByHwMode) {
424 for (const CodeGenInstruction *CGI : NumberedInstructions) {
425 APInt Value = getInstructionBaseValue(CGI, HwMode);
426 auto [It, Inserted] =
427 ValueToIndex.try_emplace(Key: Value, Args: BaseEncodings.Values.size());
428 if (Inserted)
429 BaseEncodings.Values.push_back(x: std::move(Value));
430 Indices.push_back(x: It->second);
431 }
432 }
433
434 const uint64_t NumValues = BaseEncodings.Values.size();
435 BaseEncodings.IndexBitWidth =
436 std::max<uint64_t>(a: 8, b: PowerOf2Ceil(A: Log2_64_Ceil(Value: NumValues)));
437 if (BaseEncodings.IndexBitWidth > 32) {
438 BaseEncodings = {};
439 return;
440 }
441
442 const uint64_t NumTables = BaseEncodings.IndicesByHwMode.size();
443 const uint64_t RawSize =
444 NumTables * NumberedInstructions.size() * NumWords * sizeof(uint64_t);
445 const uint64_t PooledSize = NumValues * NumWords * sizeof(uint64_t) +
446 NumTables * NumberedInstructions.size() *
447 (BaseEncodings.IndexBitWidth / 8);
448 if (PooledSize >= RawSize)
449 BaseEncodings = {};
450}
451
452void CodeEmitterGen::emitBaseEncodingPool(raw_ostream &O) {
453 const unsigned NumWords = APInt::getNumWords(BitWidth);
454 O << " static const uint64_t InstBits[][" << NumWords << "] = {\n";
455 for (const APInt &Value : BaseEncodings.Values) {
456 O << " {";
457 emitInstBits(OS&: O, Bits: Value);
458 O << "},\n";
459 }
460 O << " };\n";
461
462 for (const auto &[HwMode, Indices] : BaseEncodings.IndicesByHwMode) {
463 O << formatv(Fmt: " static const uint{}_t InstBitsIndices",
464 Vals&: BaseEncodings.IndexBitWidth);
465 if (HwMode != DefaultMode)
466 O << "_" << CGH.getModeName(Id: HwMode);
467 O << "[] = {\n";
468 for (unsigned Index : Indices)
469 O << " " << Index << ",\n";
470 O << " };\n";
471 }
472 O << " static_assert(sizeof(InstBits) / sizeof(InstBits[0]) <=\n"
473 " (UINT64_C(1) << (sizeof(InstBitsIndices[0]) * 8)));\n";
474}
475
476void CodeEmitterGen::emitInstructionBaseValues(
477 raw_ostream &O, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
478 unsigned HwMode) {
479 if (HwMode == DefaultMode)
480 O << " static const uint64_t InstBits[] = {\n";
481 else
482 O << " static const uint64_t InstBits_" << CGH.getModeName(Id: HwMode)
483 << "[] = {\n";
484
485 for (const CodeGenInstruction *CGI : NumberedInstructions) {
486 const Record *R = CGI->TheDef;
487 APInt Value = getInstructionBaseValue(CGI, HwMode);
488 O << " ";
489 emitInstBits(OS&: O, Bits: Value);
490 O << "," << '\t' << "// " << R->getName() << "\n";
491 }
492 O << " };\n";
493}
494
495CodeEmitterGen::CodeEmitterGen(const RecordKeeper &RK)
496 : RK(RK), Target(RK), CGH(Target.getHwModes()) {}
497
498void CodeEmitterGen::run(raw_ostream &O) {
499 emitSourceFileHeader(Desc: "Machine Code Emitter", OS&: O);
500
501 ArrayRef<const CodeGenInstruction *> EncodedInstructions =
502 Target.getTargetNonPseudoInstructions();
503
504 if (Target.hasVariableLengthEncodings()) {
505 emitVarLenCodeEmitter(R: RK, OS&: O);
506 return;
507 }
508 // The set of HwModes used by instruction encodings.
509 std::set<unsigned> HwModes;
510 BitWidth = 0;
511 for (const CodeGenInstruction *CGI : EncodedInstructions) {
512 const Record *R = CGI->TheDef;
513 if (const Record *RV = R->getValueAsOptionalDef(FieldName: "EncodingInfos")) {
514 EncodingInfoByHwMode EBM(RV, CGH);
515 for (const auto &[Key, Value] : EBM) {
516 BitWidth = std::max<unsigned>(
517 a: BitWidth, b: Target.getInstructionEncodingBits(EncodingDef: Value).size());
518 HwModes.insert(x: Key);
519 }
520 continue;
521 }
522 BitWidth = std::max<unsigned>(a: BitWidth,
523 b: Target.getInstructionEncodingBits(EncodingDef: R).size());
524 }
525 UseAPInt = BitWidth > 64;
526 if (UseAPInt)
527 buildBaseEncodingPool(NumberedInstructions: EncodedInstructions, HwModes);
528
529 // Emit function declaration
530 if (UseAPInt) {
531 O << "void " << Target.getName()
532 << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
533 << " SmallVectorImpl<MCFixup> &Fixups,\n"
534 << " APInt &Inst,\n"
535 << " APInt &Scratch,\n"
536 << " const MCSubtargetInfo &STI) const {\n";
537 } else {
538 O << "uint64_t " << Target.getName();
539 O << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
540 << " SmallVectorImpl<MCFixup> &Fixups,\n"
541 << " const MCSubtargetInfo &STI) const {\n";
542 }
543
544 // Emit instruction base values
545 if (BaseEncodings.empty()) {
546 emitInstructionBaseValues(O, NumberedInstructions: EncodedInstructions, HwMode: DefaultMode);
547 for (unsigned HwMode : HwModes) {
548 if (HwMode == DefaultMode)
549 continue;
550 emitInstructionBaseValues(O, NumberedInstructions: EncodedInstructions, HwMode);
551 }
552 } else {
553 emitBaseEncodingPool(O);
554 }
555
556 if (!HwModes.empty()) {
557 // This pointer will be assigned to the HwMode table later.
558 if (BaseEncodings.empty())
559 O << " const uint64_t *InstBitsByHw;\n";
560 else
561 O << formatv(Fmt: " const uint{0}_t *InstBitsIndicesByHw;\n",
562 Vals&: BaseEncodings.IndexBitWidth);
563 }
564
565 // Map to accumulate all the cases.
566 CaseMapT CaseMap, BitOffsetCaseMap;
567
568 // Construct all cases statement for each opcode
569 for (auto [Index, CGI] : enumerate(First&: EncodedInstructions)) {
570 const Record *R = CGI->TheDef;
571 auto [Case, BitOffsetCase] = getInstructionCases(R);
572
573 CaseMap[Case].push_back(x: Index);
574 BitOffsetCaseMap[BitOffsetCase].push_back(x: Index);
575 }
576
577 auto PrintInstName = [&](raw_ostream &OS, unsigned Index) {
578 const CodeGenInstruction *CGI = EncodedInstructions[Index];
579 const Record *R = CGI->TheDef;
580 OS << R->getValueAsString(FieldName: "Namespace") << "::" << R->getName();
581 };
582
583 unsigned FirstSupportedOpcode = EncodedInstructions.front()->EnumVal;
584 O << " constexpr unsigned FirstSupportedOpcode = " << FirstSupportedOpcode
585 << ";\n";
586 O << R"(
587 const unsigned opcode = MI.getOpcode();
588 if (opcode < FirstSupportedOpcode)
589 reportUnsupportedInst(MI);
590 unsigned TableIndex = opcode - FirstSupportedOpcode;
591)";
592
593 // Emit initial function code
594 if (UseAPInt) {
595 int NumWords = APInt::getNumWords(BitWidth);
596 O << " if (Scratch.getBitWidth() != " << BitWidth << ")\n"
597 << " Scratch = Scratch.zext(" << BitWidth << ");\n";
598 if (BaseEncodings.empty()) {
599 O << " Inst = APInt(" << BitWidth
600 << ", ArrayRef(InstBits + TableIndex * " << NumWords << ", " << NumWords
601 << "));\n";
602 } else {
603 O << " unsigned InstBitsIndex = InstBitsIndices[TableIndex];\n"
604 << " Inst = APInt(" << BitWidth
605 << ", ArrayRef(InstBits[InstBitsIndex]));\n";
606 }
607 O << " APInt &Value = Inst;\n"
608 << " APInt &op = Scratch;\n"
609 << " switch (opcode) {\n";
610 } else {
611 O << " uint64_t Value = InstBits[TableIndex];\n"
612 << " uint64_t op = 0;\n"
613 << " (void)op; // suppress warning\n"
614 << " switch (opcode) {\n";
615 }
616
617 // Emit each case statement
618 emitCaseMap(O, CaseMap, PrintCase: PrintInstName);
619
620 // Default case: unhandled opcode.
621 O << " default:\n"
622 << " reportUnsupportedInst(MI);\n"
623 << " }\n";
624 if (UseAPInt)
625 O << " Inst = Value;\n";
626 else
627 O << " return Value;\n";
628 O << "}\n\n";
629
630 IfDefEmitter IfDef(O, "GET_OPERAND_BIT_OFFSET");
631 O << "uint32_t " << Target.getName()
632 << "MCCodeEmitter::getOperandBitOffset(const MCInst &MI,\n"
633 << " unsigned OpNum,\n"
634 << " const MCSubtargetInfo &STI) const {\n"
635 << " switch (MI.getOpcode()) {\n";
636 emitCaseMap(O, CaseMap: BitOffsetCaseMap, PrintCase: PrintInstName);
637 O << " default:\n"
638 << " reportUnsupportedInst(MI);\n"
639 << " }\n"
640 << " reportUnsupportedOperand(MI, OpNum);\n"
641 << "}\n";
642}
643
644static TableGen::Emitter::OptClass<CodeEmitterGen>
645 X("gen-emitter", "Generate machine code emitter");
646