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 that, given a MachineInstr, 11 // returns the (currently, 32-bit unsigned) value of the instruction. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "CodeGenInstruction.h" 16 #include "CodeGenTarget.h" 17 #include "SubtargetFeatureInfo.h" 18 #include "Types.h" 19 #include "VarLenCodeEmitterGen.h" 20 #include "llvm/ADT/APInt.h" 21 #include "llvm/ADT/ArrayRef.h" 22 #include "llvm/ADT/StringExtras.h" 23 #include "llvm/Support/Casting.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include "llvm/TableGen/Error.h" 26 #include "llvm/TableGen/Record.h" 27 #include "llvm/TableGen/TableGenBackend.h" 28 #include <cassert> 29 #include <cstdint> 30 #include <map> 31 #include <set> 32 #include <string> 33 #include <utility> 34 #include <vector> 35 36 using namespace llvm; 37 38 namespace { 39 40 class CodeEmitterGen { 41 RecordKeeper &Records; 42 43 public: 44 CodeEmitterGen(RecordKeeper &R) : Records(R) {} 45 46 void run(raw_ostream &o); 47 48 private: 49 int getVariableBit(const std::string &VarName, BitsInit *BI, int bit); 50 std::string getInstructionCase(Record *R, CodeGenTarget &Target); 51 std::string getInstructionCaseForEncoding(Record *R, Record *EncodingDef, 52 CodeGenTarget &Target); 53 void AddCodeToMergeInOperand(Record *R, BitsInit *BI, 54 const std::string &VarName, 55 unsigned &NumberedOp, 56 std::set<unsigned> &NamedOpIndices, 57 std::string &Case, CodeGenTarget &Target); 58 59 void emitInstructionBaseValues( 60 raw_ostream &o, ArrayRef<const CodeGenInstruction *> NumberedInstructions, 61 CodeGenTarget &Target, int HwMode = -1); 62 unsigned BitWidth; 63 bool UseAPInt; 64 }; 65 66 // If the VarBitInit at position 'bit' matches the specified variable then 67 // return the variable bit position. Otherwise return -1. 68 int CodeEmitterGen::getVariableBit(const std::string &VarName, 69 BitsInit *BI, int bit) { 70 if (VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit(bit))) { 71 if (VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar())) 72 if (VI->getName() == VarName) 73 return VBI->getBitNum(); 74 } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit(bit))) { 75 if (VI->getName() == VarName) 76 return 0; 77 } 78 79 return -1; 80 } 81 82 void CodeEmitterGen:: 83 AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName, 84 unsigned &NumberedOp, 85 std::set<unsigned> &NamedOpIndices, 86 std::string &Case, CodeGenTarget &Target) { 87 CodeGenInstruction &CGI = Target.getInstruction(R); 88 89 // Determine if VarName actually contributes to the Inst encoding. 90 int bit = BI->getNumBits()-1; 91 92 // Scan for a bit that this contributed to. 93 for (; bit >= 0; ) { 94 if (getVariableBit(VarName, BI, bit) != -1) 95 break; 96 97 --bit; 98 } 99 100 // If we found no bits, ignore this value, otherwise emit the call to get the 101 // operand encoding. 102 if (bit < 0) return; 103 104 // If the operand matches by name, reference according to that 105 // operand number. Non-matching operands are assumed to be in 106 // order. 107 unsigned OpIdx; 108 if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) { 109 // Get the machine operand number for the indicated operand. 110 OpIdx = CGI.Operands[OpIdx].MIOperandNo; 111 assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) && 112 "Explicitly used operand also marked as not emitted!"); 113 } else { 114 unsigned NumberOps = CGI.Operands.size(); 115 /// If this operand is not supposed to be emitted by the 116 /// generated emitter, skip it. 117 while (NumberedOp < NumberOps && 118 (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) || 119 (!NamedOpIndices.empty() && NamedOpIndices.count( 120 CGI.Operands.getSubOperandNumber(NumberedOp).first)))) { 121 ++NumberedOp; 122 } 123 124 if (NumberedOp >= 125 CGI.Operands.back().MIOperandNo + CGI.Operands.back().MINumOperands) { 126 std::string E; 127 raw_string_ostream S(E); 128 S << "Too few operands in record " << R->getName() 129 << " (no match for variable " << VarName << "):\n"; 130 S << *R; 131 PrintFatalError(R, E); 132 } 133 134 OpIdx = NumberedOp++; 135 } 136 137 std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx); 138 std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName; 139 140 if (UseAPInt) 141 Case += " op.clearAllBits();\n"; 142 143 // If the source operand has a custom encoder, use it. This will 144 // get the encoding for all of the suboperands. 145 if (!EncoderMethodName.empty()) { 146 // A custom encoder has all of the information for the 147 // sub-operands, if there are more than one, so only 148 // query the encoder once per source operand. 149 if (SO.second == 0) { 150 Case += " // op: " + VarName + "\n"; 151 if (UseAPInt) { 152 Case += " " + EncoderMethodName + "(MI, " + utostr(OpIdx); 153 Case += ", op"; 154 } else { 155 Case += " op = " + EncoderMethodName + "(MI, " + utostr(OpIdx); 156 } 157 Case += ", Fixups, STI);\n"; 158 } 159 } else { 160 Case += " // op: " + VarName + "\n"; 161 if (UseAPInt) { 162 Case += " getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")"; 163 Case += ", op, Fixups, STI"; 164 } else { 165 Case += " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")"; 166 Case += ", Fixups, STI"; 167 } 168 Case += ");\n"; 169 } 170 171 // Precalculate the number of lits this variable contributes to in the 172 // operand. If there is a single lit (consecutive range of bits) we can use a 173 // destructive sequence on APInt that reduces memory allocations. 174 int numOperandLits = 0; 175 for (int tmpBit = bit; tmpBit >= 0;) { 176 int varBit = getVariableBit(VarName, BI, tmpBit); 177 178 // If this bit isn't from a variable, skip it. 179 if (varBit == -1) { 180 --tmpBit; 181 continue; 182 } 183 184 // Figure out the consecutive range of bits covered by this operand, in 185 // order to generate better encoding code. 186 int beginVarBit = varBit; 187 int N = 1; 188 for (--tmpBit; tmpBit >= 0;) { 189 varBit = getVariableBit(VarName, BI, tmpBit); 190 if (varBit == -1 || varBit != (beginVarBit - N)) 191 break; 192 ++N; 193 --tmpBit; 194 } 195 ++numOperandLits; 196 } 197 198 for (; bit >= 0; ) { 199 int varBit = getVariableBit(VarName, BI, bit); 200 201 // If this bit isn't from a variable, skip it. 202 if (varBit == -1) { 203 --bit; 204 continue; 205 } 206 207 // Figure out the consecutive range of bits covered by this operand, in 208 // order to generate better encoding code. 209 int beginInstBit = bit; 210 int beginVarBit = varBit; 211 int N = 1; 212 for (--bit; bit >= 0;) { 213 varBit = getVariableBit(VarName, BI, bit); 214 if (varBit == -1 || varBit != (beginVarBit - N)) break; 215 ++N; 216 --bit; 217 } 218 219 std::string maskStr; 220 int opShift; 221 222 unsigned loBit = beginVarBit - N + 1; 223 unsigned hiBit = loBit + N; 224 unsigned loInstBit = beginInstBit - N + 1; 225 if (UseAPInt) { 226 std::string extractStr; 227 if (N >= 64) { 228 extractStr = "op.extractBits(" + itostr(hiBit - loBit) + ", " + 229 itostr(loBit) + ")"; 230 Case += " Value.insertBits(" + extractStr + ", " + 231 itostr(loInstBit) + ");\n"; 232 } else { 233 extractStr = "op.extractBitsAsZExtValue(" + itostr(hiBit - loBit) + 234 ", " + itostr(loBit) + ")"; 235 Case += " Value.insertBits(" + extractStr + ", " + 236 itostr(loInstBit) + ", " + itostr(hiBit - loBit) + ");\n"; 237 } 238 } else { 239 uint64_t opMask = ~(uint64_t)0 >> (64 - N); 240 opShift = beginVarBit - N + 1; 241 opMask <<= opShift; 242 maskStr = "UINT64_C(" + utostr(opMask) + ")"; 243 opShift = beginInstBit - beginVarBit; 244 245 if (numOperandLits == 1) { 246 Case += " op &= " + maskStr + ";\n"; 247 if (opShift > 0) { 248 Case += " op <<= " + itostr(opShift) + ";\n"; 249 } else if (opShift < 0) { 250 Case += " op >>= " + itostr(-opShift) + ";\n"; 251 } 252 Case += " Value |= op;\n"; 253 } else { 254 if (opShift > 0) { 255 Case += " Value |= (op & " + maskStr + ") << " + 256 itostr(opShift) + ";\n"; 257 } else if (opShift < 0) { 258 Case += " Value |= (op & " + maskStr + ") >> " + 259 itostr(-opShift) + ";\n"; 260 } else { 261 Case += " Value |= (op & " + maskStr + ");\n"; 262 } 263 } 264 } 265 } 266 } 267 268 std::string CodeEmitterGen::getInstructionCase(Record *R, 269 CodeGenTarget &Target) { 270 std::string Case; 271 if (const RecordVal *RV = R->getValue("EncodingInfos")) { 272 if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) { 273 const CodeGenHwModes &HWM = Target.getHwModes(); 274 EncodingInfoByHwMode EBM(DI->getDef(), HWM); 275 Case += " switch (HwMode) {\n"; 276 Case += " default: llvm_unreachable(\"Unhandled HwMode\");\n"; 277 for (auto &KV : EBM) { 278 Case += " case " + itostr(KV.first) + ": {\n"; 279 Case += getInstructionCaseForEncoding(R, KV.second, Target); 280 Case += " break;\n"; 281 Case += " }\n"; 282 } 283 Case += " }\n"; 284 return Case; 285 } 286 } 287 return getInstructionCaseForEncoding(R, R, Target); 288 } 289 290 std::string CodeEmitterGen::getInstructionCaseForEncoding(Record *R, Record *EncodingDef, 291 CodeGenTarget &Target) { 292 std::string Case; 293 BitsInit *BI = EncodingDef->getValueAsBitsInit("Inst"); 294 unsigned NumberedOp = 0; 295 std::set<unsigned> NamedOpIndices; 296 297 // Collect the set of operand indices that might correspond to named 298 // operand, and skip these when assigning operands based on position. 299 if (Target.getInstructionSet()-> 300 getValueAsBit("noNamedPositionallyEncodedOperands")) { 301 CodeGenInstruction &CGI = Target.getInstruction(R); 302 for (const RecordVal &RV : R->getValues()) { 303 unsigned OpIdx; 304 if (!CGI.Operands.hasOperandNamed(RV.getName(), OpIdx)) 305 continue; 306 307 NamedOpIndices.insert(OpIdx); 308 } 309 } 310 311 // Loop over all of the fields in the instruction, determining which are the 312 // operands to the instruction. 313 for (const RecordVal &RV : EncodingDef->getValues()) { 314 // Ignore fixed fields in the record, we're looking for values like: 315 // bits<5> RST = { ?, ?, ?, ?, ? }; 316 if (RV.isNonconcreteOK() || RV.getValue()->isComplete()) 317 continue; 318 319 AddCodeToMergeInOperand(R, BI, std::string(RV.getName()), NumberedOp, 320 NamedOpIndices, Case, Target); 321 } 322 323 StringRef PostEmitter = R->getValueAsString("PostEncoderMethod"); 324 if (!PostEmitter.empty()) { 325 Case += " Value = "; 326 Case += PostEmitter; 327 Case += "(MI, Value"; 328 Case += ", STI"; 329 Case += ");\n"; 330 } 331 332 return Case; 333 } 334 335 static std::string 336 getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) { 337 std::string Name = "CEFBS"; 338 for (const auto &Feature : FeatureBitset) 339 Name += ("_" + Feature->getName()).str(); 340 return Name; 341 } 342 343 static void emitInstBits(raw_ostream &OS, const APInt &Bits) { 344 for (unsigned I = 0; I < Bits.getNumWords(); ++I) 345 OS << ((I > 0) ? ", " : "") << "UINT64_C(" << utostr(Bits.getRawData()[I]) 346 << ")"; 347 } 348 349 void CodeEmitterGen::emitInstructionBaseValues( 350 raw_ostream &o, ArrayRef<const CodeGenInstruction *> NumberedInstructions, 351 CodeGenTarget &Target, int HwMode) { 352 const CodeGenHwModes &HWM = Target.getHwModes(); 353 if (HwMode == -1) 354 o << " static const uint64_t InstBits[] = {\n"; 355 else 356 o << " static const uint64_t InstBits_" << HWM.getMode(HwMode).Name 357 << "[] = {\n"; 358 359 for (const CodeGenInstruction *CGI : NumberedInstructions) { 360 Record *R = CGI->TheDef; 361 362 if (R->getValueAsString("Namespace") == "TargetOpcode" || 363 R->getValueAsBit("isPseudo")) { 364 o << " "; emitInstBits(o, APInt(BitWidth, 0)); o << ",\n"; 365 continue; 366 } 367 368 Record *EncodingDef = R; 369 if (const RecordVal *RV = R->getValue("EncodingInfos")) { 370 if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) { 371 EncodingInfoByHwMode EBM(DI->getDef(), HWM); 372 if (EBM.hasMode(HwMode)) 373 EncodingDef = EBM.get(HwMode); 374 } 375 } 376 BitsInit *BI = EncodingDef->getValueAsBitsInit("Inst"); 377 378 // Start by filling in fixed values. 379 APInt Value(BitWidth, 0); 380 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) { 381 if (BitInit *B = dyn_cast<BitInit>(BI->getBit(e - i - 1))) 382 Value |= APInt(BitWidth, (uint64_t)B->getValue()) << (e - i - 1); 383 } 384 o << " "; 385 emitInstBits(o, Value); 386 o << "," << '\t' << "// " << R->getName() << "\n"; 387 } 388 o << " UINT64_C(0)\n };\n"; 389 } 390 391 void CodeEmitterGen::run(raw_ostream &o) { 392 CodeGenTarget Target(Records); 393 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); 394 395 // For little-endian instruction bit encodings, reverse the bit order 396 Target.reverseBitsForLittleEndianEncoding(); 397 398 ArrayRef<const CodeGenInstruction*> NumberedInstructions = 399 Target.getInstructionsByEnumValue(); 400 401 if (any_of(NumberedInstructions, [](const CodeGenInstruction *CGI) { 402 Record *R = CGI->TheDef; 403 return R->getValue("Inst") && isa<DagInit>(R->getValueInit("Inst")); 404 })) { 405 emitVarLenCodeEmitter(Records, o); 406 } else { 407 const CodeGenHwModes &HWM = Target.getHwModes(); 408 // The set of HwModes used by instruction encodings. 409 std::set<unsigned> HwModes; 410 BitWidth = 0; 411 for (const CodeGenInstruction *CGI : NumberedInstructions) { 412 Record *R = CGI->TheDef; 413 if (R->getValueAsString("Namespace") == "TargetOpcode" || 414 R->getValueAsBit("isPseudo")) 415 continue; 416 417 if (const RecordVal *RV = R->getValue("EncodingInfos")) { 418 if (DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) { 419 EncodingInfoByHwMode EBM(DI->getDef(), HWM); 420 for (auto &KV : EBM) { 421 BitsInit *BI = KV.second->getValueAsBitsInit("Inst"); 422 BitWidth = std::max(BitWidth, BI->getNumBits()); 423 HwModes.insert(KV.first); 424 } 425 continue; 426 } 427 } 428 BitsInit *BI = R->getValueAsBitsInit("Inst"); 429 BitWidth = std::max(BitWidth, BI->getNumBits()); 430 } 431 UseAPInt = BitWidth > 64; 432 433 // Emit function declaration 434 if (UseAPInt) { 435 o << "void " << Target.getName() 436 << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n" 437 << " SmallVectorImpl<MCFixup> &Fixups,\n" 438 << " APInt &Inst,\n" 439 << " APInt &Scratch,\n" 440 << " const MCSubtargetInfo &STI) const {\n"; 441 } else { 442 o << "uint64_t " << Target.getName(); 443 o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n" 444 << " SmallVectorImpl<MCFixup> &Fixups,\n" 445 << " const MCSubtargetInfo &STI) const {\n"; 446 } 447 448 // Emit instruction base values 449 if (HwModes.empty()) { 450 emitInstructionBaseValues(o, NumberedInstructions, Target, -1); 451 } else { 452 for (unsigned HwMode : HwModes) 453 emitInstructionBaseValues(o, NumberedInstructions, Target, (int)HwMode); 454 } 455 456 if (!HwModes.empty()) { 457 o << " const uint64_t *InstBits;\n"; 458 o << " unsigned HwMode = STI.getHwMode();\n"; 459 o << " switch (HwMode) {\n"; 460 o << " default: llvm_unreachable(\"Unknown hardware mode!\"); break;\n"; 461 for (unsigned I : HwModes) { 462 o << " case " << I << ": InstBits = InstBits_" << HWM.getMode(I).Name 463 << "; break;\n"; 464 } 465 o << " };\n"; 466 } 467 468 // Map to accumulate all the cases. 469 std::map<std::string, std::vector<std::string>> CaseMap; 470 471 // Construct all cases statement for each opcode 472 for (Record *R : Insts) { 473 if (R->getValueAsString("Namespace") == "TargetOpcode" || 474 R->getValueAsBit("isPseudo")) 475 continue; 476 std::string InstName = 477 (R->getValueAsString("Namespace") + "::" + R->getName()).str(); 478 std::string Case = getInstructionCase(R, Target); 479 480 CaseMap[Case].push_back(std::move(InstName)); 481 } 482 483 // Emit initial function code 484 if (UseAPInt) { 485 int NumWords = APInt::getNumWords(BitWidth); 486 o << " const unsigned opcode = MI.getOpcode();\n" 487 << " if (Scratch.getBitWidth() != " << BitWidth << ")\n" 488 << " Scratch = Scratch.zext(" << BitWidth << ");\n" 489 << " Inst = APInt(" << BitWidth 490 << ", makeArrayRef(InstBits + opcode * " << NumWords << ", " << NumWords 491 << "));\n" 492 << " APInt &Value = Inst;\n" 493 << " APInt &op = Scratch;\n" 494 << " switch (opcode) {\n"; 495 } else { 496 o << " const unsigned opcode = MI.getOpcode();\n" 497 << " uint64_t Value = InstBits[opcode];\n" 498 << " uint64_t op = 0;\n" 499 << " (void)op; // suppress warning\n" 500 << " switch (opcode) {\n"; 501 } 502 503 // Emit each case statement 504 std::map<std::string, std::vector<std::string>>::iterator IE, EE; 505 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) { 506 const std::string &Case = IE->first; 507 std::vector<std::string> &InstList = IE->second; 508 509 for (int i = 0, N = InstList.size(); i < N; i++) { 510 if (i) 511 o << "\n"; 512 o << " case " << InstList[i] << ":"; 513 } 514 o << " {\n"; 515 o << Case; 516 o << " break;\n" 517 << " }\n"; 518 } 519 520 // Default case: unhandled opcode 521 o << " default:\n" 522 << " std::string msg;\n" 523 << " raw_string_ostream Msg(msg);\n" 524 << " Msg << \"Not supported instr: \" << MI;\n" 525 << " report_fatal_error(Msg.str().c_str());\n" 526 << " }\n"; 527 if (UseAPInt) 528 o << " Inst = Value;\n"; 529 else 530 o << " return Value;\n"; 531 o << "}\n\n"; 532 } 533 534 const auto &All = SubtargetFeatureInfo::getAll(Records); 535 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> SubtargetFeatures; 536 SubtargetFeatures.insert(All.begin(), All.end()); 537 538 o << "#ifdef ENABLE_INSTR_PREDICATE_VERIFIER\n" 539 << "#undef ENABLE_INSTR_PREDICATE_VERIFIER\n" 540 << "#include <sstream>\n\n"; 541 542 // Emit the subtarget feature enumeration. 543 SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures, 544 o); 545 546 // Emit the name table for error messages. 547 o << "#ifndef NDEBUG\n"; 548 SubtargetFeatureInfo::emitNameTable(SubtargetFeatures, o); 549 o << "#endif // NDEBUG\n"; 550 551 // Emit the available features compute function. 552 SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures( 553 Target.getName(), "MCCodeEmitter", "computeAvailableFeatures", 554 SubtargetFeatures, o); 555 556 std::vector<std::vector<Record *>> FeatureBitsets; 557 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) { 558 FeatureBitsets.emplace_back(); 559 for (Record *Predicate : Inst->TheDef->getValueAsListOfDefs("Predicates")) { 560 const auto &I = SubtargetFeatures.find(Predicate); 561 if (I != SubtargetFeatures.end()) 562 FeatureBitsets.back().push_back(I->second.TheDef); 563 } 564 } 565 566 llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A, 567 const std::vector<Record *> &B) { 568 if (A.size() < B.size()) 569 return true; 570 if (A.size() > B.size()) 571 return false; 572 for (auto Pair : zip(A, B)) { 573 if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName()) 574 return true; 575 if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName()) 576 return false; 577 } 578 return false; 579 }); 580 FeatureBitsets.erase( 581 std::unique(FeatureBitsets.begin(), FeatureBitsets.end()), 582 FeatureBitsets.end()); 583 o << "#ifndef NDEBUG\n" 584 << "// Feature bitsets.\n" 585 << "enum : " << getMinimalTypeForRange(FeatureBitsets.size()) << " {\n" 586 << " CEFBS_None,\n"; 587 for (const auto &FeatureBitset : FeatureBitsets) { 588 if (FeatureBitset.empty()) 589 continue; 590 o << " " << getNameForFeatureBitset(FeatureBitset) << ",\n"; 591 } 592 o << "};\n\n" 593 << "static constexpr FeatureBitset FeatureBitsets[] = {\n" 594 << " {}, // CEFBS_None\n"; 595 for (const auto &FeatureBitset : FeatureBitsets) { 596 if (FeatureBitset.empty()) 597 continue; 598 o << " {"; 599 for (const auto &Feature : FeatureBitset) { 600 const auto &I = SubtargetFeatures.find(Feature); 601 assert(I != SubtargetFeatures.end() && "Didn't import predicate?"); 602 o << I->second.getEnumBitName() << ", "; 603 } 604 o << "},\n"; 605 } 606 o << "};\n" 607 << "#endif // NDEBUG\n\n"; 608 609 610 // Emit the predicate verifier. 611 o << "void " << Target.getName() 612 << "MCCodeEmitter::verifyInstructionPredicates(\n" 613 << " const MCInst &Inst, const FeatureBitset &AvailableFeatures) const {\n" 614 << "#ifndef NDEBUG\n" 615 << " static " << getMinimalTypeForRange(FeatureBitsets.size()) 616 << " RequiredFeaturesRefs[] = {\n"; 617 unsigned InstIdx = 0; 618 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) { 619 o << " CEFBS"; 620 unsigned NumPredicates = 0; 621 for (Record *Predicate : Inst->TheDef->getValueAsListOfDefs("Predicates")) { 622 const auto &I = SubtargetFeatures.find(Predicate); 623 if (I != SubtargetFeatures.end()) { 624 o << '_' << I->second.TheDef->getName(); 625 NumPredicates++; 626 } 627 } 628 if (!NumPredicates) 629 o << "_None"; 630 o << ", // " << Inst->TheDef->getName() << " = " << InstIdx << "\n"; 631 InstIdx++; 632 } 633 o << " };\n\n"; 634 o << " assert(Inst.getOpcode() < " << InstIdx << ");\n"; 635 o << " const FeatureBitset &RequiredFeatures = " 636 "FeatureBitsets[RequiredFeaturesRefs[Inst.getOpcode()]];\n"; 637 o << " FeatureBitset MissingFeatures =\n" 638 << " (AvailableFeatures & RequiredFeatures) ^\n" 639 << " RequiredFeatures;\n" 640 << " if (MissingFeatures.any()) {\n" 641 << " std::ostringstream Msg;\n" 642 << " Msg << \"Attempting to emit \" << " 643 "MCII.getName(Inst.getOpcode()).str()\n" 644 << " << \" instruction but the \";\n" 645 << " for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i)\n" 646 << " if (MissingFeatures.test(i))\n" 647 << " Msg << SubtargetFeatureNames[i] << \" \";\n" 648 << " Msg << \"predicate(s) are not met\";\n" 649 << " report_fatal_error(Msg.str().c_str());\n" 650 << " }\n" 651 << "#else\n" 652 << " // Silence unused variable warning on targets that don't use MCII for " 653 "other purposes (e.g. BPF).\n" 654 << " (void)MCII;\n" 655 << "#endif // NDEBUG\n"; 656 o << "}\n"; 657 o << "#endif\n"; 658 } 659 660 } // end anonymous namespace 661 662 namespace llvm { 663 664 void EmitCodeEmitter(RecordKeeper &RK, raw_ostream &OS) { 665 emitSourceFileHeader("Machine Code Emitter", OS); 666 CodeEmitterGen(RK).run(OS); 667 } 668 669 } // end namespace llvm 670