1*81ad6265SDimitry Andric //===---------------- DecoderEmitter.cpp - Decoder Generator --------------===// 2*81ad6265SDimitry Andric // 3*81ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*81ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*81ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*81ad6265SDimitry Andric // 7*81ad6265SDimitry Andric //===----------------------------------------------------------------------===// 8*81ad6265SDimitry Andric // 9*81ad6265SDimitry Andric // It contains the tablegen backend that emits the decoder functions for 10*81ad6265SDimitry Andric // targets with fixed/variable length instruction set. 11*81ad6265SDimitry Andric // 12*81ad6265SDimitry Andric //===----------------------------------------------------------------------===// 13*81ad6265SDimitry Andric 14*81ad6265SDimitry Andric #include "CodeGenInstruction.h" 15*81ad6265SDimitry Andric #include "CodeGenTarget.h" 16*81ad6265SDimitry Andric #include "InfoByHwMode.h" 17*81ad6265SDimitry Andric #include "VarLenCodeEmitterGen.h" 18*81ad6265SDimitry Andric #include "llvm/ADT/APInt.h" 19*81ad6265SDimitry Andric #include "llvm/ADT/ArrayRef.h" 20*81ad6265SDimitry Andric #include "llvm/ADT/CachedHashString.h" 21*81ad6265SDimitry Andric #include "llvm/ADT/STLExtras.h" 22*81ad6265SDimitry Andric #include "llvm/ADT/SetVector.h" 23*81ad6265SDimitry Andric #include "llvm/ADT/SmallString.h" 24*81ad6265SDimitry Andric #include "llvm/ADT/Statistic.h" 25*81ad6265SDimitry Andric #include "llvm/ADT/StringExtras.h" 26*81ad6265SDimitry Andric #include "llvm/ADT/StringRef.h" 27*81ad6265SDimitry Andric #include "llvm/MC/MCDecoderOps.h" 28*81ad6265SDimitry Andric #include "llvm/Support/Casting.h" 29*81ad6265SDimitry Andric #include "llvm/Support/Debug.h" 30*81ad6265SDimitry Andric #include "llvm/Support/ErrorHandling.h" 31*81ad6265SDimitry Andric #include "llvm/Support/FormattedStream.h" 32*81ad6265SDimitry Andric #include "llvm/Support/LEB128.h" 33*81ad6265SDimitry Andric #include "llvm/Support/raw_ostream.h" 34*81ad6265SDimitry Andric #include "llvm/TableGen/Error.h" 35*81ad6265SDimitry Andric #include "llvm/TableGen/Record.h" 36*81ad6265SDimitry Andric #include <algorithm> 37*81ad6265SDimitry Andric #include <cassert> 38*81ad6265SDimitry Andric #include <cstddef> 39*81ad6265SDimitry Andric #include <cstdint> 40*81ad6265SDimitry Andric #include <map> 41*81ad6265SDimitry Andric #include <memory> 42*81ad6265SDimitry Andric #include <set> 43*81ad6265SDimitry Andric #include <string> 44*81ad6265SDimitry Andric #include <utility> 45*81ad6265SDimitry Andric #include <vector> 46*81ad6265SDimitry Andric 47*81ad6265SDimitry Andric using namespace llvm; 48*81ad6265SDimitry Andric 49*81ad6265SDimitry Andric #define DEBUG_TYPE "decoder-emitter" 50*81ad6265SDimitry Andric 51*81ad6265SDimitry Andric namespace { 52*81ad6265SDimitry Andric 53*81ad6265SDimitry Andric STATISTIC(NumEncodings, "Number of encodings considered"); 54*81ad6265SDimitry Andric STATISTIC(NumEncodingsLackingDisasm, "Number of encodings without disassembler info"); 55*81ad6265SDimitry Andric STATISTIC(NumInstructions, "Number of instructions considered"); 56*81ad6265SDimitry Andric STATISTIC(NumEncodingsSupported, "Number of encodings supported"); 57*81ad6265SDimitry Andric STATISTIC(NumEncodingsOmitted, "Number of encodings omitted"); 58*81ad6265SDimitry Andric 59*81ad6265SDimitry Andric struct EncodingField { 60*81ad6265SDimitry Andric unsigned Base, Width, Offset; 61*81ad6265SDimitry Andric EncodingField(unsigned B, unsigned W, unsigned O) 62*81ad6265SDimitry Andric : Base(B), Width(W), Offset(O) { } 63*81ad6265SDimitry Andric }; 64*81ad6265SDimitry Andric 65*81ad6265SDimitry Andric struct OperandInfo { 66*81ad6265SDimitry Andric std::vector<EncodingField> Fields; 67*81ad6265SDimitry Andric std::string Decoder; 68*81ad6265SDimitry Andric bool HasCompleteDecoder; 69*81ad6265SDimitry Andric uint64_t InitValue; 70*81ad6265SDimitry Andric 71*81ad6265SDimitry Andric OperandInfo(std::string D, bool HCD) 72*81ad6265SDimitry Andric : Decoder(std::move(D)), HasCompleteDecoder(HCD), InitValue(0) {} 73*81ad6265SDimitry Andric 74*81ad6265SDimitry Andric void addField(unsigned Base, unsigned Width, unsigned Offset) { 75*81ad6265SDimitry Andric Fields.push_back(EncodingField(Base, Width, Offset)); 76*81ad6265SDimitry Andric } 77*81ad6265SDimitry Andric 78*81ad6265SDimitry Andric unsigned numFields() const { return Fields.size(); } 79*81ad6265SDimitry Andric 80*81ad6265SDimitry Andric typedef std::vector<EncodingField>::const_iterator const_iterator; 81*81ad6265SDimitry Andric 82*81ad6265SDimitry Andric const_iterator begin() const { return Fields.begin(); } 83*81ad6265SDimitry Andric const_iterator end() const { return Fields.end(); } 84*81ad6265SDimitry Andric }; 85*81ad6265SDimitry Andric 86*81ad6265SDimitry Andric typedef std::vector<uint8_t> DecoderTable; 87*81ad6265SDimitry Andric typedef uint32_t DecoderFixup; 88*81ad6265SDimitry Andric typedef std::vector<DecoderFixup> FixupList; 89*81ad6265SDimitry Andric typedef std::vector<FixupList> FixupScopeList; 90*81ad6265SDimitry Andric typedef SmallSetVector<CachedHashString, 16> PredicateSet; 91*81ad6265SDimitry Andric typedef SmallSetVector<CachedHashString, 16> DecoderSet; 92*81ad6265SDimitry Andric struct DecoderTableInfo { 93*81ad6265SDimitry Andric DecoderTable Table; 94*81ad6265SDimitry Andric FixupScopeList FixupStack; 95*81ad6265SDimitry Andric PredicateSet Predicates; 96*81ad6265SDimitry Andric DecoderSet Decoders; 97*81ad6265SDimitry Andric }; 98*81ad6265SDimitry Andric 99*81ad6265SDimitry Andric struct EncodingAndInst { 100*81ad6265SDimitry Andric const Record *EncodingDef; 101*81ad6265SDimitry Andric const CodeGenInstruction *Inst; 102*81ad6265SDimitry Andric StringRef HwModeName; 103*81ad6265SDimitry Andric 104*81ad6265SDimitry Andric EncodingAndInst(const Record *EncodingDef, const CodeGenInstruction *Inst, 105*81ad6265SDimitry Andric StringRef HwModeName = "") 106*81ad6265SDimitry Andric : EncodingDef(EncodingDef), Inst(Inst), HwModeName(HwModeName) {} 107*81ad6265SDimitry Andric }; 108*81ad6265SDimitry Andric 109*81ad6265SDimitry Andric struct EncodingIDAndOpcode { 110*81ad6265SDimitry Andric unsigned EncodingID; 111*81ad6265SDimitry Andric unsigned Opcode; 112*81ad6265SDimitry Andric 113*81ad6265SDimitry Andric EncodingIDAndOpcode() : EncodingID(0), Opcode(0) {} 114*81ad6265SDimitry Andric EncodingIDAndOpcode(unsigned EncodingID, unsigned Opcode) 115*81ad6265SDimitry Andric : EncodingID(EncodingID), Opcode(Opcode) {} 116*81ad6265SDimitry Andric }; 117*81ad6265SDimitry Andric 118*81ad6265SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const EncodingAndInst &Value) { 119*81ad6265SDimitry Andric if (Value.EncodingDef != Value.Inst->TheDef) 120*81ad6265SDimitry Andric OS << Value.EncodingDef->getName() << ":"; 121*81ad6265SDimitry Andric OS << Value.Inst->TheDef->getName(); 122*81ad6265SDimitry Andric return OS; 123*81ad6265SDimitry Andric } 124*81ad6265SDimitry Andric 125*81ad6265SDimitry Andric class DecoderEmitter { 126*81ad6265SDimitry Andric RecordKeeper &RK; 127*81ad6265SDimitry Andric std::vector<EncodingAndInst> NumberedEncodings; 128*81ad6265SDimitry Andric 129*81ad6265SDimitry Andric public: 130*81ad6265SDimitry Andric // Defaults preserved here for documentation, even though they aren't 131*81ad6265SDimitry Andric // strictly necessary given the way that this is currently being called. 132*81ad6265SDimitry Andric DecoderEmitter(RecordKeeper &R, std::string PredicateNamespace, 133*81ad6265SDimitry Andric std::string GPrefix = "if (", 134*81ad6265SDimitry Andric std::string GPostfix = " == MCDisassembler::Fail)", 135*81ad6265SDimitry Andric std::string ROK = "MCDisassembler::Success", 136*81ad6265SDimitry Andric std::string RFail = "MCDisassembler::Fail", std::string L = "") 137*81ad6265SDimitry Andric : RK(R), Target(R), PredicateNamespace(std::move(PredicateNamespace)), 138*81ad6265SDimitry Andric GuardPrefix(std::move(GPrefix)), GuardPostfix(std::move(GPostfix)), 139*81ad6265SDimitry Andric ReturnOK(std::move(ROK)), ReturnFail(std::move(RFail)), 140*81ad6265SDimitry Andric Locals(std::move(L)) {} 141*81ad6265SDimitry Andric 142*81ad6265SDimitry Andric // Emit the decoder state machine table. 143*81ad6265SDimitry Andric void emitTable(formatted_raw_ostream &o, DecoderTable &Table, 144*81ad6265SDimitry Andric unsigned Indentation, unsigned BitWidth, 145*81ad6265SDimitry Andric StringRef Namespace) const; 146*81ad6265SDimitry Andric void emitInstrLenTable(formatted_raw_ostream &OS, 147*81ad6265SDimitry Andric std::vector<unsigned> &InstrLen) const; 148*81ad6265SDimitry Andric void emitPredicateFunction(formatted_raw_ostream &OS, 149*81ad6265SDimitry Andric PredicateSet &Predicates, 150*81ad6265SDimitry Andric unsigned Indentation) const; 151*81ad6265SDimitry Andric void emitDecoderFunction(formatted_raw_ostream &OS, 152*81ad6265SDimitry Andric DecoderSet &Decoders, 153*81ad6265SDimitry Andric unsigned Indentation) const; 154*81ad6265SDimitry Andric 155*81ad6265SDimitry Andric // run - Output the code emitter 156*81ad6265SDimitry Andric void run(raw_ostream &o); 157*81ad6265SDimitry Andric 158*81ad6265SDimitry Andric private: 159*81ad6265SDimitry Andric CodeGenTarget Target; 160*81ad6265SDimitry Andric 161*81ad6265SDimitry Andric public: 162*81ad6265SDimitry Andric std::string PredicateNamespace; 163*81ad6265SDimitry Andric std::string GuardPrefix, GuardPostfix; 164*81ad6265SDimitry Andric std::string ReturnOK, ReturnFail; 165*81ad6265SDimitry Andric std::string Locals; 166*81ad6265SDimitry Andric }; 167*81ad6265SDimitry Andric 168*81ad6265SDimitry Andric } // end anonymous namespace 169*81ad6265SDimitry Andric 170*81ad6265SDimitry Andric // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system 171*81ad6265SDimitry Andric // for a bit value. 172*81ad6265SDimitry Andric // 173*81ad6265SDimitry Andric // BIT_UNFILTERED is used as the init value for a filter position. It is used 174*81ad6265SDimitry Andric // only for filter processings. 175*81ad6265SDimitry Andric typedef enum { 176*81ad6265SDimitry Andric BIT_TRUE, // '1' 177*81ad6265SDimitry Andric BIT_FALSE, // '0' 178*81ad6265SDimitry Andric BIT_UNSET, // '?' 179*81ad6265SDimitry Andric BIT_UNFILTERED // unfiltered 180*81ad6265SDimitry Andric } bit_value_t; 181*81ad6265SDimitry Andric 182*81ad6265SDimitry Andric static bool ValueSet(bit_value_t V) { 183*81ad6265SDimitry Andric return (V == BIT_TRUE || V == BIT_FALSE); 184*81ad6265SDimitry Andric } 185*81ad6265SDimitry Andric 186*81ad6265SDimitry Andric static bool ValueNotSet(bit_value_t V) { 187*81ad6265SDimitry Andric return (V == BIT_UNSET); 188*81ad6265SDimitry Andric } 189*81ad6265SDimitry Andric 190*81ad6265SDimitry Andric static int Value(bit_value_t V) { 191*81ad6265SDimitry Andric return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1); 192*81ad6265SDimitry Andric } 193*81ad6265SDimitry Andric 194*81ad6265SDimitry Andric static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) { 195*81ad6265SDimitry Andric if (BitInit *bit = dyn_cast<BitInit>(bits.getBit(index))) 196*81ad6265SDimitry Andric return bit->getValue() ? BIT_TRUE : BIT_FALSE; 197*81ad6265SDimitry Andric 198*81ad6265SDimitry Andric // The bit is uninitialized. 199*81ad6265SDimitry Andric return BIT_UNSET; 200*81ad6265SDimitry Andric } 201*81ad6265SDimitry Andric 202*81ad6265SDimitry Andric // Prints the bit value for each position. 203*81ad6265SDimitry Andric static void dumpBits(raw_ostream &o, const BitsInit &bits) { 204*81ad6265SDimitry Andric for (unsigned index = bits.getNumBits(); index > 0; --index) { 205*81ad6265SDimitry Andric switch (bitFromBits(bits, index - 1)) { 206*81ad6265SDimitry Andric case BIT_TRUE: 207*81ad6265SDimitry Andric o << "1"; 208*81ad6265SDimitry Andric break; 209*81ad6265SDimitry Andric case BIT_FALSE: 210*81ad6265SDimitry Andric o << "0"; 211*81ad6265SDimitry Andric break; 212*81ad6265SDimitry Andric case BIT_UNSET: 213*81ad6265SDimitry Andric o << "_"; 214*81ad6265SDimitry Andric break; 215*81ad6265SDimitry Andric default: 216*81ad6265SDimitry Andric llvm_unreachable("unexpected return value from bitFromBits"); 217*81ad6265SDimitry Andric } 218*81ad6265SDimitry Andric } 219*81ad6265SDimitry Andric } 220*81ad6265SDimitry Andric 221*81ad6265SDimitry Andric static BitsInit &getBitsField(const Record &def, StringRef str) { 222*81ad6265SDimitry Andric const RecordVal *RV = def.getValue(str); 223*81ad6265SDimitry Andric if (BitsInit *Bits = dyn_cast<BitsInit>(RV->getValue())) 224*81ad6265SDimitry Andric return *Bits; 225*81ad6265SDimitry Andric 226*81ad6265SDimitry Andric // variable length instruction 227*81ad6265SDimitry Andric VarLenInst VLI = VarLenInst(cast<DagInit>(RV->getValue()), RV); 228*81ad6265SDimitry Andric SmallVector<Init *, 16> Bits; 229*81ad6265SDimitry Andric 230*81ad6265SDimitry Andric for (auto &SI : VLI) { 231*81ad6265SDimitry Andric if (const BitsInit *BI = dyn_cast<BitsInit>(SI.Value)) { 232*81ad6265SDimitry Andric for (unsigned Idx = 0U; Idx < BI->getNumBits(); ++Idx) { 233*81ad6265SDimitry Andric Bits.push_back(BI->getBit(Idx)); 234*81ad6265SDimitry Andric } 235*81ad6265SDimitry Andric } else if (const BitInit *BI = dyn_cast<BitInit>(SI.Value)) { 236*81ad6265SDimitry Andric Bits.push_back(const_cast<BitInit *>(BI)); 237*81ad6265SDimitry Andric } else { 238*81ad6265SDimitry Andric for (unsigned Idx = 0U; Idx < SI.BitWidth; ++Idx) 239*81ad6265SDimitry Andric Bits.push_back(UnsetInit::get(def.getRecords())); 240*81ad6265SDimitry Andric } 241*81ad6265SDimitry Andric } 242*81ad6265SDimitry Andric 243*81ad6265SDimitry Andric return *BitsInit::get(def.getRecords(), Bits); 244*81ad6265SDimitry Andric } 245*81ad6265SDimitry Andric 246*81ad6265SDimitry Andric // Representation of the instruction to work on. 247*81ad6265SDimitry Andric typedef std::vector<bit_value_t> insn_t; 248*81ad6265SDimitry Andric 249*81ad6265SDimitry Andric namespace { 250*81ad6265SDimitry Andric 251*81ad6265SDimitry Andric static const uint64_t NO_FIXED_SEGMENTS_SENTINEL = -1ULL; 252*81ad6265SDimitry Andric 253*81ad6265SDimitry Andric class FilterChooser; 254*81ad6265SDimitry Andric 255*81ad6265SDimitry Andric /// Filter - Filter works with FilterChooser to produce the decoding tree for 256*81ad6265SDimitry Andric /// the ISA. 257*81ad6265SDimitry Andric /// 258*81ad6265SDimitry Andric /// It is useful to think of a Filter as governing the switch stmts of the 259*81ad6265SDimitry Andric /// decoding tree in a certain level. Each case stmt delegates to an inferior 260*81ad6265SDimitry Andric /// FilterChooser to decide what further decoding logic to employ, or in another 261*81ad6265SDimitry Andric /// words, what other remaining bits to look at. The FilterChooser eventually 262*81ad6265SDimitry Andric /// chooses a best Filter to do its job. 263*81ad6265SDimitry Andric /// 264*81ad6265SDimitry Andric /// This recursive scheme ends when the number of Opcodes assigned to the 265*81ad6265SDimitry Andric /// FilterChooser becomes 1 or if there is a conflict. A conflict happens when 266*81ad6265SDimitry Andric /// the Filter/FilterChooser combo does not know how to distinguish among the 267*81ad6265SDimitry Andric /// Opcodes assigned. 268*81ad6265SDimitry Andric /// 269*81ad6265SDimitry Andric /// An example of a conflict is 270*81ad6265SDimitry Andric /// 271*81ad6265SDimitry Andric /// Conflict: 272*81ad6265SDimitry Andric /// 111101000.00........00010000.... 273*81ad6265SDimitry Andric /// 111101000.00........0001........ 274*81ad6265SDimitry Andric /// 1111010...00........0001........ 275*81ad6265SDimitry Andric /// 1111010...00.................... 276*81ad6265SDimitry Andric /// 1111010......................... 277*81ad6265SDimitry Andric /// 1111............................ 278*81ad6265SDimitry Andric /// ................................ 279*81ad6265SDimitry Andric /// VST4q8a 111101000_00________00010000____ 280*81ad6265SDimitry Andric /// VST4q8b 111101000_00________00010000____ 281*81ad6265SDimitry Andric /// 282*81ad6265SDimitry Andric /// The Debug output shows the path that the decoding tree follows to reach the 283*81ad6265SDimitry Andric /// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced 284*81ad6265SDimitry Andric /// even registers, while VST4q8b is a vst4 to double-spaced odd registers. 285*81ad6265SDimitry Andric /// 286*81ad6265SDimitry Andric /// The encoding info in the .td files does not specify this meta information, 287*81ad6265SDimitry Andric /// which could have been used by the decoder to resolve the conflict. The 288*81ad6265SDimitry Andric /// decoder could try to decode the even/odd register numbering and assign to 289*81ad6265SDimitry Andric /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a" 290*81ad6265SDimitry Andric /// version and return the Opcode since the two have the same Asm format string. 291*81ad6265SDimitry Andric class Filter { 292*81ad6265SDimitry Andric protected: 293*81ad6265SDimitry Andric const FilterChooser *Owner;// points to the FilterChooser who owns this filter 294*81ad6265SDimitry Andric unsigned StartBit; // the starting bit position 295*81ad6265SDimitry Andric unsigned NumBits; // number of bits to filter 296*81ad6265SDimitry Andric bool Mixed; // a mixed region contains both set and unset bits 297*81ad6265SDimitry Andric 298*81ad6265SDimitry Andric // Map of well-known segment value to the set of uid's with that value. 299*81ad6265SDimitry Andric std::map<uint64_t, std::vector<EncodingIDAndOpcode>> 300*81ad6265SDimitry Andric FilteredInstructions; 301*81ad6265SDimitry Andric 302*81ad6265SDimitry Andric // Set of uid's with non-constant segment values. 303*81ad6265SDimitry Andric std::vector<EncodingIDAndOpcode> VariableInstructions; 304*81ad6265SDimitry Andric 305*81ad6265SDimitry Andric // Map of well-known segment value to its delegate. 306*81ad6265SDimitry Andric std::map<uint64_t, std::unique_ptr<const FilterChooser>> FilterChooserMap; 307*81ad6265SDimitry Andric 308*81ad6265SDimitry Andric // Number of instructions which fall under FilteredInstructions category. 309*81ad6265SDimitry Andric unsigned NumFiltered; 310*81ad6265SDimitry Andric 311*81ad6265SDimitry Andric // Keeps track of the last opcode in the filtered bucket. 312*81ad6265SDimitry Andric EncodingIDAndOpcode LastOpcFiltered; 313*81ad6265SDimitry Andric 314*81ad6265SDimitry Andric public: 315*81ad6265SDimitry Andric Filter(Filter &&f); 316*81ad6265SDimitry Andric Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed); 317*81ad6265SDimitry Andric 318*81ad6265SDimitry Andric ~Filter() = default; 319*81ad6265SDimitry Andric 320*81ad6265SDimitry Andric unsigned getNumFiltered() const { return NumFiltered; } 321*81ad6265SDimitry Andric 322*81ad6265SDimitry Andric EncodingIDAndOpcode getSingletonOpc() const { 323*81ad6265SDimitry Andric assert(NumFiltered == 1); 324*81ad6265SDimitry Andric return LastOpcFiltered; 325*81ad6265SDimitry Andric } 326*81ad6265SDimitry Andric 327*81ad6265SDimitry Andric // Return the filter chooser for the group of instructions without constant 328*81ad6265SDimitry Andric // segment values. 329*81ad6265SDimitry Andric const FilterChooser &getVariableFC() const { 330*81ad6265SDimitry Andric assert(NumFiltered == 1); 331*81ad6265SDimitry Andric assert(FilterChooserMap.size() == 1); 332*81ad6265SDimitry Andric return *(FilterChooserMap.find(NO_FIXED_SEGMENTS_SENTINEL)->second); 333*81ad6265SDimitry Andric } 334*81ad6265SDimitry Andric 335*81ad6265SDimitry Andric // Divides the decoding task into sub tasks and delegates them to the 336*81ad6265SDimitry Andric // inferior FilterChooser's. 337*81ad6265SDimitry Andric // 338*81ad6265SDimitry Andric // A special case arises when there's only one entry in the filtered 339*81ad6265SDimitry Andric // instructions. In order to unambiguously decode the singleton, we need to 340*81ad6265SDimitry Andric // match the remaining undecoded encoding bits against the singleton. 341*81ad6265SDimitry Andric void recurse(); 342*81ad6265SDimitry Andric 343*81ad6265SDimitry Andric // Emit table entries to decode instructions given a segment or segments of 344*81ad6265SDimitry Andric // bits. 345*81ad6265SDimitry Andric void emitTableEntry(DecoderTableInfo &TableInfo) const; 346*81ad6265SDimitry Andric 347*81ad6265SDimitry Andric // Returns the number of fanout produced by the filter. More fanout implies 348*81ad6265SDimitry Andric // the filter distinguishes more categories of instructions. 349*81ad6265SDimitry Andric unsigned usefulness() const; 350*81ad6265SDimitry Andric }; // end class Filter 351*81ad6265SDimitry Andric 352*81ad6265SDimitry Andric } // end anonymous namespace 353*81ad6265SDimitry Andric 354*81ad6265SDimitry Andric // These are states of our finite state machines used in FilterChooser's 355*81ad6265SDimitry Andric // filterProcessor() which produces the filter candidates to use. 356*81ad6265SDimitry Andric typedef enum { 357*81ad6265SDimitry Andric ATTR_NONE, 358*81ad6265SDimitry Andric ATTR_FILTERED, 359*81ad6265SDimitry Andric ATTR_ALL_SET, 360*81ad6265SDimitry Andric ATTR_ALL_UNSET, 361*81ad6265SDimitry Andric ATTR_MIXED 362*81ad6265SDimitry Andric } bitAttr_t; 363*81ad6265SDimitry Andric 364*81ad6265SDimitry Andric /// FilterChooser - FilterChooser chooses the best filter among a set of Filters 365*81ad6265SDimitry Andric /// in order to perform the decoding of instructions at the current level. 366*81ad6265SDimitry Andric /// 367*81ad6265SDimitry Andric /// Decoding proceeds from the top down. Based on the well-known encoding bits 368*81ad6265SDimitry Andric /// of instructions available, FilterChooser builds up the possible Filters that 369*81ad6265SDimitry Andric /// can further the task of decoding by distinguishing among the remaining 370*81ad6265SDimitry Andric /// candidate instructions. 371*81ad6265SDimitry Andric /// 372*81ad6265SDimitry Andric /// Once a filter has been chosen, it is called upon to divide the decoding task 373*81ad6265SDimitry Andric /// into sub-tasks and delegates them to its inferior FilterChoosers for further 374*81ad6265SDimitry Andric /// processings. 375*81ad6265SDimitry Andric /// 376*81ad6265SDimitry Andric /// It is useful to think of a Filter as governing the switch stmts of the 377*81ad6265SDimitry Andric /// decoding tree. And each case is delegated to an inferior FilterChooser to 378*81ad6265SDimitry Andric /// decide what further remaining bits to look at. 379*81ad6265SDimitry Andric namespace { 380*81ad6265SDimitry Andric 381*81ad6265SDimitry Andric class FilterChooser { 382*81ad6265SDimitry Andric protected: 383*81ad6265SDimitry Andric friend class Filter; 384*81ad6265SDimitry Andric 385*81ad6265SDimitry Andric // Vector of codegen instructions to choose our filter. 386*81ad6265SDimitry Andric ArrayRef<EncodingAndInst> AllInstructions; 387*81ad6265SDimitry Andric 388*81ad6265SDimitry Andric // Vector of uid's for this filter chooser to work on. 389*81ad6265SDimitry Andric // The first member of the pair is the opcode id being decoded, the second is 390*81ad6265SDimitry Andric // the opcode id that should be emitted. 391*81ad6265SDimitry Andric const std::vector<EncodingIDAndOpcode> &Opcodes; 392*81ad6265SDimitry Andric 393*81ad6265SDimitry Andric // Lookup table for the operand decoding of instructions. 394*81ad6265SDimitry Andric const std::map<unsigned, std::vector<OperandInfo>> &Operands; 395*81ad6265SDimitry Andric 396*81ad6265SDimitry Andric // Vector of candidate filters. 397*81ad6265SDimitry Andric std::vector<Filter> Filters; 398*81ad6265SDimitry Andric 399*81ad6265SDimitry Andric // Array of bit values passed down from our parent. 400*81ad6265SDimitry Andric // Set to all BIT_UNFILTERED's for Parent == NULL. 401*81ad6265SDimitry Andric std::vector<bit_value_t> FilterBitValues; 402*81ad6265SDimitry Andric 403*81ad6265SDimitry Andric // Links to the FilterChooser above us in the decoding tree. 404*81ad6265SDimitry Andric const FilterChooser *Parent; 405*81ad6265SDimitry Andric 406*81ad6265SDimitry Andric // Index of the best filter from Filters. 407*81ad6265SDimitry Andric int BestIndex; 408*81ad6265SDimitry Andric 409*81ad6265SDimitry Andric // Width of instructions 410*81ad6265SDimitry Andric unsigned BitWidth; 411*81ad6265SDimitry Andric 412*81ad6265SDimitry Andric // Parent emitter 413*81ad6265SDimitry Andric const DecoderEmitter *Emitter; 414*81ad6265SDimitry Andric 415*81ad6265SDimitry Andric public: 416*81ad6265SDimitry Andric FilterChooser(ArrayRef<EncodingAndInst> Insts, 417*81ad6265SDimitry Andric const std::vector<EncodingIDAndOpcode> &IDs, 418*81ad6265SDimitry Andric const std::map<unsigned, std::vector<OperandInfo>> &Ops, 419*81ad6265SDimitry Andric unsigned BW, const DecoderEmitter *E) 420*81ad6265SDimitry Andric : AllInstructions(Insts), Opcodes(IDs), Operands(Ops), 421*81ad6265SDimitry Andric FilterBitValues(BW, BIT_UNFILTERED), Parent(nullptr), BestIndex(-1), 422*81ad6265SDimitry Andric BitWidth(BW), Emitter(E) { 423*81ad6265SDimitry Andric doFilter(); 424*81ad6265SDimitry Andric } 425*81ad6265SDimitry Andric 426*81ad6265SDimitry Andric FilterChooser(ArrayRef<EncodingAndInst> Insts, 427*81ad6265SDimitry Andric const std::vector<EncodingIDAndOpcode> &IDs, 428*81ad6265SDimitry Andric const std::map<unsigned, std::vector<OperandInfo>> &Ops, 429*81ad6265SDimitry Andric const std::vector<bit_value_t> &ParentFilterBitValues, 430*81ad6265SDimitry Andric const FilterChooser &parent) 431*81ad6265SDimitry Andric : AllInstructions(Insts), Opcodes(IDs), Operands(Ops), 432*81ad6265SDimitry Andric FilterBitValues(ParentFilterBitValues), Parent(&parent), BestIndex(-1), 433*81ad6265SDimitry Andric BitWidth(parent.BitWidth), Emitter(parent.Emitter) { 434*81ad6265SDimitry Andric doFilter(); 435*81ad6265SDimitry Andric } 436*81ad6265SDimitry Andric 437*81ad6265SDimitry Andric FilterChooser(const FilterChooser &) = delete; 438*81ad6265SDimitry Andric void operator=(const FilterChooser &) = delete; 439*81ad6265SDimitry Andric 440*81ad6265SDimitry Andric unsigned getBitWidth() const { return BitWidth; } 441*81ad6265SDimitry Andric 442*81ad6265SDimitry Andric protected: 443*81ad6265SDimitry Andric // Populates the insn given the uid. 444*81ad6265SDimitry Andric void insnWithID(insn_t &Insn, unsigned Opcode) const { 445*81ad6265SDimitry Andric BitsInit &Bits = getBitsField(*AllInstructions[Opcode].EncodingDef, "Inst"); 446*81ad6265SDimitry Andric Insn.resize(BitWidth > Bits.getNumBits() ? BitWidth : Bits.getNumBits(), 447*81ad6265SDimitry Andric BIT_UNSET); 448*81ad6265SDimitry Andric // We may have a SoftFail bitmask, which specifies a mask where an encoding 449*81ad6265SDimitry Andric // may differ from the value in "Inst" and yet still be valid, but the 450*81ad6265SDimitry Andric // disassembler should return SoftFail instead of Success. 451*81ad6265SDimitry Andric // 452*81ad6265SDimitry Andric // This is used for marking UNPREDICTABLE instructions in the ARM world. 453*81ad6265SDimitry Andric const RecordVal *RV = 454*81ad6265SDimitry Andric AllInstructions[Opcode].EncodingDef->getValue("SoftFail"); 455*81ad6265SDimitry Andric const BitsInit *SFBits = RV ? dyn_cast<BitsInit>(RV->getValue()) : nullptr; 456*81ad6265SDimitry Andric for (unsigned i = 0; i < Bits.getNumBits(); ++i) { 457*81ad6265SDimitry Andric if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE) 458*81ad6265SDimitry Andric Insn[i] = BIT_UNSET; 459*81ad6265SDimitry Andric else 460*81ad6265SDimitry Andric Insn[i] = bitFromBits(Bits, i); 461*81ad6265SDimitry Andric } 462*81ad6265SDimitry Andric } 463*81ad6265SDimitry Andric 464*81ad6265SDimitry Andric // Emit the name of the encoding/instruction pair. 465*81ad6265SDimitry Andric void emitNameWithID(raw_ostream &OS, unsigned Opcode) const { 466*81ad6265SDimitry Andric const Record *EncodingDef = AllInstructions[Opcode].EncodingDef; 467*81ad6265SDimitry Andric const Record *InstDef = AllInstructions[Opcode].Inst->TheDef; 468*81ad6265SDimitry Andric if (EncodingDef != InstDef) 469*81ad6265SDimitry Andric OS << EncodingDef->getName() << ":"; 470*81ad6265SDimitry Andric OS << InstDef->getName(); 471*81ad6265SDimitry Andric } 472*81ad6265SDimitry Andric 473*81ad6265SDimitry Andric // Populates the field of the insn given the start position and the number of 474*81ad6265SDimitry Andric // consecutive bits to scan for. 475*81ad6265SDimitry Andric // 476*81ad6265SDimitry Andric // Returns false if there exists any uninitialized bit value in the range. 477*81ad6265SDimitry Andric // Returns true, otherwise. 478*81ad6265SDimitry Andric bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit, 479*81ad6265SDimitry Andric unsigned NumBits) const; 480*81ad6265SDimitry Andric 481*81ad6265SDimitry Andric /// dumpFilterArray - dumpFilterArray prints out debugging info for the given 482*81ad6265SDimitry Andric /// filter array as a series of chars. 483*81ad6265SDimitry Andric void dumpFilterArray(raw_ostream &o, 484*81ad6265SDimitry Andric const std::vector<bit_value_t> & filter) const; 485*81ad6265SDimitry Andric 486*81ad6265SDimitry Andric /// dumpStack - dumpStack traverses the filter chooser chain and calls 487*81ad6265SDimitry Andric /// dumpFilterArray on each filter chooser up to the top level one. 488*81ad6265SDimitry Andric void dumpStack(raw_ostream &o, const char *prefix) const; 489*81ad6265SDimitry Andric 490*81ad6265SDimitry Andric Filter &bestFilter() { 491*81ad6265SDimitry Andric assert(BestIndex != -1 && "BestIndex not set"); 492*81ad6265SDimitry Andric return Filters[BestIndex]; 493*81ad6265SDimitry Andric } 494*81ad6265SDimitry Andric 495*81ad6265SDimitry Andric bool PositionFiltered(unsigned i) const { 496*81ad6265SDimitry Andric return ValueSet(FilterBitValues[i]); 497*81ad6265SDimitry Andric } 498*81ad6265SDimitry Andric 499*81ad6265SDimitry Andric // Calculates the island(s) needed to decode the instruction. 500*81ad6265SDimitry Andric // This returns a lit of undecoded bits of an instructions, for example, 501*81ad6265SDimitry Andric // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be 502*81ad6265SDimitry Andric // decoded bits in order to verify that the instruction matches the Opcode. 503*81ad6265SDimitry Andric unsigned getIslands(std::vector<unsigned> &StartBits, 504*81ad6265SDimitry Andric std::vector<unsigned> &EndBits, 505*81ad6265SDimitry Andric std::vector<uint64_t> &FieldVals, 506*81ad6265SDimitry Andric const insn_t &Insn) const; 507*81ad6265SDimitry Andric 508*81ad6265SDimitry Andric // Emits code to check the Predicates member of an instruction are true. 509*81ad6265SDimitry Andric // Returns true if predicate matches were emitted, false otherwise. 510*81ad6265SDimitry Andric bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation, 511*81ad6265SDimitry Andric unsigned Opc) const; 512*81ad6265SDimitry Andric bool emitPredicateMatchAux(const Init &Val, bool ParenIfBinOp, 513*81ad6265SDimitry Andric raw_ostream &OS) const; 514*81ad6265SDimitry Andric 515*81ad6265SDimitry Andric bool doesOpcodeNeedPredicate(unsigned Opc) const; 516*81ad6265SDimitry Andric unsigned getPredicateIndex(DecoderTableInfo &TableInfo, StringRef P) const; 517*81ad6265SDimitry Andric void emitPredicateTableEntry(DecoderTableInfo &TableInfo, 518*81ad6265SDimitry Andric unsigned Opc) const; 519*81ad6265SDimitry Andric 520*81ad6265SDimitry Andric void emitSoftFailTableEntry(DecoderTableInfo &TableInfo, 521*81ad6265SDimitry Andric unsigned Opc) const; 522*81ad6265SDimitry Andric 523*81ad6265SDimitry Andric // Emits table entries to decode the singleton. 524*81ad6265SDimitry Andric void emitSingletonTableEntry(DecoderTableInfo &TableInfo, 525*81ad6265SDimitry Andric EncodingIDAndOpcode Opc) const; 526*81ad6265SDimitry Andric 527*81ad6265SDimitry Andric // Emits code to decode the singleton, and then to decode the rest. 528*81ad6265SDimitry Andric void emitSingletonTableEntry(DecoderTableInfo &TableInfo, 529*81ad6265SDimitry Andric const Filter &Best) const; 530*81ad6265SDimitry Andric 531*81ad6265SDimitry Andric void emitBinaryParser(raw_ostream &o, unsigned &Indentation, 532*81ad6265SDimitry Andric const OperandInfo &OpInfo, 533*81ad6265SDimitry Andric bool &OpHasCompleteDecoder) const; 534*81ad6265SDimitry Andric 535*81ad6265SDimitry Andric void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc, 536*81ad6265SDimitry Andric bool &HasCompleteDecoder) const; 537*81ad6265SDimitry Andric unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc, 538*81ad6265SDimitry Andric bool &HasCompleteDecoder) const; 539*81ad6265SDimitry Andric 540*81ad6265SDimitry Andric // Assign a single filter and run with it. 541*81ad6265SDimitry Andric void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed); 542*81ad6265SDimitry Andric 543*81ad6265SDimitry Andric // reportRegion is a helper function for filterProcessor to mark a region as 544*81ad6265SDimitry Andric // eligible for use as a filter region. 545*81ad6265SDimitry Andric void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex, 546*81ad6265SDimitry Andric bool AllowMixed); 547*81ad6265SDimitry Andric 548*81ad6265SDimitry Andric // FilterProcessor scans the well-known encoding bits of the instructions and 549*81ad6265SDimitry Andric // builds up a list of candidate filters. It chooses the best filter and 550*81ad6265SDimitry Andric // recursively descends down the decoding tree. 551*81ad6265SDimitry Andric bool filterProcessor(bool AllowMixed, bool Greedy = true); 552*81ad6265SDimitry Andric 553*81ad6265SDimitry Andric // Decides on the best configuration of filter(s) to use in order to decode 554*81ad6265SDimitry Andric // the instructions. A conflict of instructions may occur, in which case we 555*81ad6265SDimitry Andric // dump the conflict set to the standard error. 556*81ad6265SDimitry Andric void doFilter(); 557*81ad6265SDimitry Andric 558*81ad6265SDimitry Andric public: 559*81ad6265SDimitry Andric // emitTableEntries - Emit state machine entries to decode our share of 560*81ad6265SDimitry Andric // instructions. 561*81ad6265SDimitry Andric void emitTableEntries(DecoderTableInfo &TableInfo) const; 562*81ad6265SDimitry Andric }; 563*81ad6265SDimitry Andric 564*81ad6265SDimitry Andric } // end anonymous namespace 565*81ad6265SDimitry Andric 566*81ad6265SDimitry Andric /////////////////////////// 567*81ad6265SDimitry Andric // // 568*81ad6265SDimitry Andric // Filter Implementation // 569*81ad6265SDimitry Andric // // 570*81ad6265SDimitry Andric /////////////////////////// 571*81ad6265SDimitry Andric 572*81ad6265SDimitry Andric Filter::Filter(Filter &&f) 573*81ad6265SDimitry Andric : Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed), 574*81ad6265SDimitry Andric FilteredInstructions(std::move(f.FilteredInstructions)), 575*81ad6265SDimitry Andric VariableInstructions(std::move(f.VariableInstructions)), 576*81ad6265SDimitry Andric FilterChooserMap(std::move(f.FilterChooserMap)), NumFiltered(f.NumFiltered), 577*81ad6265SDimitry Andric LastOpcFiltered(f.LastOpcFiltered) { 578*81ad6265SDimitry Andric } 579*81ad6265SDimitry Andric 580*81ad6265SDimitry Andric Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, 581*81ad6265SDimitry Andric bool mixed) 582*81ad6265SDimitry Andric : Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) { 583*81ad6265SDimitry Andric assert(StartBit + NumBits - 1 < Owner->BitWidth); 584*81ad6265SDimitry Andric 585*81ad6265SDimitry Andric NumFiltered = 0; 586*81ad6265SDimitry Andric LastOpcFiltered = {0, 0}; 587*81ad6265SDimitry Andric 588*81ad6265SDimitry Andric for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) { 589*81ad6265SDimitry Andric insn_t Insn; 590*81ad6265SDimitry Andric 591*81ad6265SDimitry Andric // Populates the insn given the uid. 592*81ad6265SDimitry Andric Owner->insnWithID(Insn, Owner->Opcodes[i].EncodingID); 593*81ad6265SDimitry Andric 594*81ad6265SDimitry Andric uint64_t Field; 595*81ad6265SDimitry Andric // Scans the segment for possibly well-specified encoding bits. 596*81ad6265SDimitry Andric bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits); 597*81ad6265SDimitry Andric 598*81ad6265SDimitry Andric if (ok) { 599*81ad6265SDimitry Andric // The encoding bits are well-known. Lets add the uid of the 600*81ad6265SDimitry Andric // instruction into the bucket keyed off the constant field value. 601*81ad6265SDimitry Andric LastOpcFiltered = Owner->Opcodes[i]; 602*81ad6265SDimitry Andric FilteredInstructions[Field].push_back(LastOpcFiltered); 603*81ad6265SDimitry Andric ++NumFiltered; 604*81ad6265SDimitry Andric } else { 605*81ad6265SDimitry Andric // Some of the encoding bit(s) are unspecified. This contributes to 606*81ad6265SDimitry Andric // one additional member of "Variable" instructions. 607*81ad6265SDimitry Andric VariableInstructions.push_back(Owner->Opcodes[i]); 608*81ad6265SDimitry Andric } 609*81ad6265SDimitry Andric } 610*81ad6265SDimitry Andric 611*81ad6265SDimitry Andric assert((FilteredInstructions.size() + VariableInstructions.size() > 0) 612*81ad6265SDimitry Andric && "Filter returns no instruction categories"); 613*81ad6265SDimitry Andric } 614*81ad6265SDimitry Andric 615*81ad6265SDimitry Andric // Divides the decoding task into sub tasks and delegates them to the 616*81ad6265SDimitry Andric // inferior FilterChooser's. 617*81ad6265SDimitry Andric // 618*81ad6265SDimitry Andric // A special case arises when there's only one entry in the filtered 619*81ad6265SDimitry Andric // instructions. In order to unambiguously decode the singleton, we need to 620*81ad6265SDimitry Andric // match the remaining undecoded encoding bits against the singleton. 621*81ad6265SDimitry Andric void Filter::recurse() { 622*81ad6265SDimitry Andric // Starts by inheriting our parent filter chooser's filter bit values. 623*81ad6265SDimitry Andric std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues); 624*81ad6265SDimitry Andric 625*81ad6265SDimitry Andric if (!VariableInstructions.empty()) { 626*81ad6265SDimitry Andric // Conservatively marks each segment position as BIT_UNSET. 627*81ad6265SDimitry Andric for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) 628*81ad6265SDimitry Andric BitValueArray[StartBit + bitIndex] = BIT_UNSET; 629*81ad6265SDimitry Andric 630*81ad6265SDimitry Andric // Delegates to an inferior filter chooser for further processing on this 631*81ad6265SDimitry Andric // group of instructions whose segment values are variable. 632*81ad6265SDimitry Andric FilterChooserMap.insert(std::make_pair(NO_FIXED_SEGMENTS_SENTINEL, 633*81ad6265SDimitry Andric std::make_unique<FilterChooser>(Owner->AllInstructions, 634*81ad6265SDimitry Andric VariableInstructions, Owner->Operands, BitValueArray, *Owner))); 635*81ad6265SDimitry Andric } 636*81ad6265SDimitry Andric 637*81ad6265SDimitry Andric // No need to recurse for a singleton filtered instruction. 638*81ad6265SDimitry Andric // See also Filter::emit*(). 639*81ad6265SDimitry Andric if (getNumFiltered() == 1) { 640*81ad6265SDimitry Andric assert(FilterChooserMap.size() == 1); 641*81ad6265SDimitry Andric return; 642*81ad6265SDimitry Andric } 643*81ad6265SDimitry Andric 644*81ad6265SDimitry Andric // Otherwise, create sub choosers. 645*81ad6265SDimitry Andric for (const auto &Inst : FilteredInstructions) { 646*81ad6265SDimitry Andric 647*81ad6265SDimitry Andric // Marks all the segment positions with either BIT_TRUE or BIT_FALSE. 648*81ad6265SDimitry Andric for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) { 649*81ad6265SDimitry Andric if (Inst.first & (1ULL << bitIndex)) 650*81ad6265SDimitry Andric BitValueArray[StartBit + bitIndex] = BIT_TRUE; 651*81ad6265SDimitry Andric else 652*81ad6265SDimitry Andric BitValueArray[StartBit + bitIndex] = BIT_FALSE; 653*81ad6265SDimitry Andric } 654*81ad6265SDimitry Andric 655*81ad6265SDimitry Andric // Delegates to an inferior filter chooser for further processing on this 656*81ad6265SDimitry Andric // category of instructions. 657*81ad6265SDimitry Andric FilterChooserMap.insert(std::make_pair( 658*81ad6265SDimitry Andric Inst.first, std::make_unique<FilterChooser>( 659*81ad6265SDimitry Andric Owner->AllInstructions, Inst.second, 660*81ad6265SDimitry Andric Owner->Operands, BitValueArray, *Owner))); 661*81ad6265SDimitry Andric } 662*81ad6265SDimitry Andric } 663*81ad6265SDimitry Andric 664*81ad6265SDimitry Andric static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups, 665*81ad6265SDimitry Andric uint32_t DestIdx) { 666*81ad6265SDimitry Andric // Any NumToSkip fixups in the current scope can resolve to the 667*81ad6265SDimitry Andric // current location. 668*81ad6265SDimitry Andric for (FixupList::const_reverse_iterator I = Fixups.rbegin(), 669*81ad6265SDimitry Andric E = Fixups.rend(); 670*81ad6265SDimitry Andric I != E; ++I) { 671*81ad6265SDimitry Andric // Calculate the distance from the byte following the fixup entry byte 672*81ad6265SDimitry Andric // to the destination. The Target is calculated from after the 16-bit 673*81ad6265SDimitry Andric // NumToSkip entry itself, so subtract two from the displacement here 674*81ad6265SDimitry Andric // to account for that. 675*81ad6265SDimitry Andric uint32_t FixupIdx = *I; 676*81ad6265SDimitry Andric uint32_t Delta = DestIdx - FixupIdx - 3; 677*81ad6265SDimitry Andric // Our NumToSkip entries are 24-bits. Make sure our table isn't too 678*81ad6265SDimitry Andric // big. 679*81ad6265SDimitry Andric assert(Delta < (1u << 24)); 680*81ad6265SDimitry Andric Table[FixupIdx] = (uint8_t)Delta; 681*81ad6265SDimitry Andric Table[FixupIdx + 1] = (uint8_t)(Delta >> 8); 682*81ad6265SDimitry Andric Table[FixupIdx + 2] = (uint8_t)(Delta >> 16); 683*81ad6265SDimitry Andric } 684*81ad6265SDimitry Andric } 685*81ad6265SDimitry Andric 686*81ad6265SDimitry Andric // Emit table entries to decode instructions given a segment or segments 687*81ad6265SDimitry Andric // of bits. 688*81ad6265SDimitry Andric void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const { 689*81ad6265SDimitry Andric TableInfo.Table.push_back(MCD::OPC_ExtractField); 690*81ad6265SDimitry Andric TableInfo.Table.push_back(StartBit); 691*81ad6265SDimitry Andric TableInfo.Table.push_back(NumBits); 692*81ad6265SDimitry Andric 693*81ad6265SDimitry Andric // A new filter entry begins a new scope for fixup resolution. 694*81ad6265SDimitry Andric TableInfo.FixupStack.emplace_back(); 695*81ad6265SDimitry Andric 696*81ad6265SDimitry Andric DecoderTable &Table = TableInfo.Table; 697*81ad6265SDimitry Andric 698*81ad6265SDimitry Andric size_t PrevFilter = 0; 699*81ad6265SDimitry Andric bool HasFallthrough = false; 700*81ad6265SDimitry Andric for (auto &Filter : FilterChooserMap) { 701*81ad6265SDimitry Andric // Field value -1 implies a non-empty set of variable instructions. 702*81ad6265SDimitry Andric // See also recurse(). 703*81ad6265SDimitry Andric if (Filter.first == NO_FIXED_SEGMENTS_SENTINEL) { 704*81ad6265SDimitry Andric HasFallthrough = true; 705*81ad6265SDimitry Andric 706*81ad6265SDimitry Andric // Each scope should always have at least one filter value to check 707*81ad6265SDimitry Andric // for. 708*81ad6265SDimitry Andric assert(PrevFilter != 0 && "empty filter set!"); 709*81ad6265SDimitry Andric FixupList &CurScope = TableInfo.FixupStack.back(); 710*81ad6265SDimitry Andric // Resolve any NumToSkip fixups in the current scope. 711*81ad6265SDimitry Andric resolveTableFixups(Table, CurScope, Table.size()); 712*81ad6265SDimitry Andric CurScope.clear(); 713*81ad6265SDimitry Andric PrevFilter = 0; // Don't re-process the filter's fallthrough. 714*81ad6265SDimitry Andric } else { 715*81ad6265SDimitry Andric Table.push_back(MCD::OPC_FilterValue); 716*81ad6265SDimitry Andric // Encode and emit the value to filter against. 717*81ad6265SDimitry Andric uint8_t Buffer[16]; 718*81ad6265SDimitry Andric unsigned Len = encodeULEB128(Filter.first, Buffer); 719*81ad6265SDimitry Andric Table.insert(Table.end(), Buffer, Buffer + Len); 720*81ad6265SDimitry Andric // Reserve space for the NumToSkip entry. We'll backpatch the value 721*81ad6265SDimitry Andric // later. 722*81ad6265SDimitry Andric PrevFilter = Table.size(); 723*81ad6265SDimitry Andric Table.push_back(0); 724*81ad6265SDimitry Andric Table.push_back(0); 725*81ad6265SDimitry Andric Table.push_back(0); 726*81ad6265SDimitry Andric } 727*81ad6265SDimitry Andric 728*81ad6265SDimitry Andric // We arrive at a category of instructions with the same segment value. 729*81ad6265SDimitry Andric // Now delegate to the sub filter chooser for further decodings. 730*81ad6265SDimitry Andric // The case may fallthrough, which happens if the remaining well-known 731*81ad6265SDimitry Andric // encoding bits do not match exactly. 732*81ad6265SDimitry Andric Filter.second->emitTableEntries(TableInfo); 733*81ad6265SDimitry Andric 734*81ad6265SDimitry Andric // Now that we've emitted the body of the handler, update the NumToSkip 735*81ad6265SDimitry Andric // of the filter itself to be able to skip forward when false. Subtract 736*81ad6265SDimitry Andric // two as to account for the width of the NumToSkip field itself. 737*81ad6265SDimitry Andric if (PrevFilter) { 738*81ad6265SDimitry Andric uint32_t NumToSkip = Table.size() - PrevFilter - 3; 739*81ad6265SDimitry Andric assert(NumToSkip < (1u << 24) && "disassembler decoding table too large!"); 740*81ad6265SDimitry Andric Table[PrevFilter] = (uint8_t)NumToSkip; 741*81ad6265SDimitry Andric Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8); 742*81ad6265SDimitry Andric Table[PrevFilter + 2] = (uint8_t)(NumToSkip >> 16); 743*81ad6265SDimitry Andric } 744*81ad6265SDimitry Andric } 745*81ad6265SDimitry Andric 746*81ad6265SDimitry Andric // Any remaining unresolved fixups bubble up to the parent fixup scope. 747*81ad6265SDimitry Andric assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!"); 748*81ad6265SDimitry Andric FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1; 749*81ad6265SDimitry Andric FixupScopeList::iterator Dest = Source - 1; 750*81ad6265SDimitry Andric llvm::append_range(*Dest, *Source); 751*81ad6265SDimitry Andric TableInfo.FixupStack.pop_back(); 752*81ad6265SDimitry Andric 753*81ad6265SDimitry Andric // If there is no fallthrough, then the final filter should get fixed 754*81ad6265SDimitry Andric // up according to the enclosing scope rather than the current position. 755*81ad6265SDimitry Andric if (!HasFallthrough) 756*81ad6265SDimitry Andric TableInfo.FixupStack.back().push_back(PrevFilter); 757*81ad6265SDimitry Andric } 758*81ad6265SDimitry Andric 759*81ad6265SDimitry Andric // Returns the number of fanout produced by the filter. More fanout implies 760*81ad6265SDimitry Andric // the filter distinguishes more categories of instructions. 761*81ad6265SDimitry Andric unsigned Filter::usefulness() const { 762*81ad6265SDimitry Andric if (!VariableInstructions.empty()) 763*81ad6265SDimitry Andric return FilteredInstructions.size(); 764*81ad6265SDimitry Andric else 765*81ad6265SDimitry Andric return FilteredInstructions.size() + 1; 766*81ad6265SDimitry Andric } 767*81ad6265SDimitry Andric 768*81ad6265SDimitry Andric ////////////////////////////////// 769*81ad6265SDimitry Andric // // 770*81ad6265SDimitry Andric // Filterchooser Implementation // 771*81ad6265SDimitry Andric // // 772*81ad6265SDimitry Andric ////////////////////////////////// 773*81ad6265SDimitry Andric 774*81ad6265SDimitry Andric // Emit the decoder state machine table. 775*81ad6265SDimitry Andric void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table, 776*81ad6265SDimitry Andric unsigned Indentation, unsigned BitWidth, 777*81ad6265SDimitry Andric StringRef Namespace) const { 778*81ad6265SDimitry Andric OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace 779*81ad6265SDimitry Andric << BitWidth << "[] = {\n"; 780*81ad6265SDimitry Andric 781*81ad6265SDimitry Andric Indentation += 2; 782*81ad6265SDimitry Andric 783*81ad6265SDimitry Andric // FIXME: We may be able to use the NumToSkip values to recover 784*81ad6265SDimitry Andric // appropriate indentation levels. 785*81ad6265SDimitry Andric DecoderTable::const_iterator I = Table.begin(); 786*81ad6265SDimitry Andric DecoderTable::const_iterator E = Table.end(); 787*81ad6265SDimitry Andric while (I != E) { 788*81ad6265SDimitry Andric assert (I < E && "incomplete decode table entry!"); 789*81ad6265SDimitry Andric 790*81ad6265SDimitry Andric uint64_t Pos = I - Table.begin(); 791*81ad6265SDimitry Andric OS << "/* " << Pos << " */"; 792*81ad6265SDimitry Andric OS.PadToColumn(12); 793*81ad6265SDimitry Andric 794*81ad6265SDimitry Andric switch (*I) { 795*81ad6265SDimitry Andric default: 796*81ad6265SDimitry Andric PrintFatalError("invalid decode table opcode"); 797*81ad6265SDimitry Andric case MCD::OPC_ExtractField: { 798*81ad6265SDimitry Andric ++I; 799*81ad6265SDimitry Andric unsigned Start = *I++; 800*81ad6265SDimitry Andric unsigned Len = *I++; 801*81ad6265SDimitry Andric OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", " 802*81ad6265SDimitry Andric << Len << ", // Inst{"; 803*81ad6265SDimitry Andric if (Len > 1) 804*81ad6265SDimitry Andric OS << (Start + Len - 1) << "-"; 805*81ad6265SDimitry Andric OS << Start << "} ...\n"; 806*81ad6265SDimitry Andric break; 807*81ad6265SDimitry Andric } 808*81ad6265SDimitry Andric case MCD::OPC_FilterValue: { 809*81ad6265SDimitry Andric ++I; 810*81ad6265SDimitry Andric OS.indent(Indentation) << "MCD::OPC_FilterValue, "; 811*81ad6265SDimitry Andric // The filter value is ULEB128 encoded. 812*81ad6265SDimitry Andric while (*I >= 128) 813*81ad6265SDimitry Andric OS << (unsigned)*I++ << ", "; 814*81ad6265SDimitry Andric OS << (unsigned)*I++ << ", "; 815*81ad6265SDimitry Andric 816*81ad6265SDimitry Andric // 24-bit numtoskip value. 817*81ad6265SDimitry Andric uint8_t Byte = *I++; 818*81ad6265SDimitry Andric uint32_t NumToSkip = Byte; 819*81ad6265SDimitry Andric OS << (unsigned)Byte << ", "; 820*81ad6265SDimitry Andric Byte = *I++; 821*81ad6265SDimitry Andric OS << (unsigned)Byte << ", "; 822*81ad6265SDimitry Andric NumToSkip |= Byte << 8; 823*81ad6265SDimitry Andric Byte = *I++; 824*81ad6265SDimitry Andric OS << utostr(Byte) << ", "; 825*81ad6265SDimitry Andric NumToSkip |= Byte << 16; 826*81ad6265SDimitry Andric OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n"; 827*81ad6265SDimitry Andric break; 828*81ad6265SDimitry Andric } 829*81ad6265SDimitry Andric case MCD::OPC_CheckField: { 830*81ad6265SDimitry Andric ++I; 831*81ad6265SDimitry Andric unsigned Start = *I++; 832*81ad6265SDimitry Andric unsigned Len = *I++; 833*81ad6265SDimitry Andric OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", " 834*81ad6265SDimitry Andric << Len << ", ";// << Val << ", " << NumToSkip << ",\n"; 835*81ad6265SDimitry Andric // ULEB128 encoded field value. 836*81ad6265SDimitry Andric for (; *I >= 128; ++I) 837*81ad6265SDimitry Andric OS << (unsigned)*I << ", "; 838*81ad6265SDimitry Andric OS << (unsigned)*I++ << ", "; 839*81ad6265SDimitry Andric // 24-bit numtoskip value. 840*81ad6265SDimitry Andric uint8_t Byte = *I++; 841*81ad6265SDimitry Andric uint32_t NumToSkip = Byte; 842*81ad6265SDimitry Andric OS << (unsigned)Byte << ", "; 843*81ad6265SDimitry Andric Byte = *I++; 844*81ad6265SDimitry Andric OS << (unsigned)Byte << ", "; 845*81ad6265SDimitry Andric NumToSkip |= Byte << 8; 846*81ad6265SDimitry Andric Byte = *I++; 847*81ad6265SDimitry Andric OS << utostr(Byte) << ", "; 848*81ad6265SDimitry Andric NumToSkip |= Byte << 16; 849*81ad6265SDimitry Andric OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n"; 850*81ad6265SDimitry Andric break; 851*81ad6265SDimitry Andric } 852*81ad6265SDimitry Andric case MCD::OPC_CheckPredicate: { 853*81ad6265SDimitry Andric ++I; 854*81ad6265SDimitry Andric OS.indent(Indentation) << "MCD::OPC_CheckPredicate, "; 855*81ad6265SDimitry Andric for (; *I >= 128; ++I) 856*81ad6265SDimitry Andric OS << (unsigned)*I << ", "; 857*81ad6265SDimitry Andric OS << (unsigned)*I++ << ", "; 858*81ad6265SDimitry Andric 859*81ad6265SDimitry Andric // 24-bit numtoskip value. 860*81ad6265SDimitry Andric uint8_t Byte = *I++; 861*81ad6265SDimitry Andric uint32_t NumToSkip = Byte; 862*81ad6265SDimitry Andric OS << (unsigned)Byte << ", "; 863*81ad6265SDimitry Andric Byte = *I++; 864*81ad6265SDimitry Andric OS << (unsigned)Byte << ", "; 865*81ad6265SDimitry Andric NumToSkip |= Byte << 8; 866*81ad6265SDimitry Andric Byte = *I++; 867*81ad6265SDimitry Andric OS << utostr(Byte) << ", "; 868*81ad6265SDimitry Andric NumToSkip |= Byte << 16; 869*81ad6265SDimitry Andric OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n"; 870*81ad6265SDimitry Andric break; 871*81ad6265SDimitry Andric } 872*81ad6265SDimitry Andric case MCD::OPC_Decode: 873*81ad6265SDimitry Andric case MCD::OPC_TryDecode: { 874*81ad6265SDimitry Andric bool IsTry = *I == MCD::OPC_TryDecode; 875*81ad6265SDimitry Andric ++I; 876*81ad6265SDimitry Andric // Extract the ULEB128 encoded Opcode to a buffer. 877*81ad6265SDimitry Andric uint8_t Buffer[16], *p = Buffer; 878*81ad6265SDimitry Andric while ((*p++ = *I++) >= 128) 879*81ad6265SDimitry Andric assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer) 880*81ad6265SDimitry Andric && "ULEB128 value too large!"); 881*81ad6265SDimitry Andric // Decode the Opcode value. 882*81ad6265SDimitry Andric unsigned Opc = decodeULEB128(Buffer); 883*81ad6265SDimitry Andric OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "") 884*81ad6265SDimitry Andric << "Decode, "; 885*81ad6265SDimitry Andric for (p = Buffer; *p >= 128; ++p) 886*81ad6265SDimitry Andric OS << (unsigned)*p << ", "; 887*81ad6265SDimitry Andric OS << (unsigned)*p << ", "; 888*81ad6265SDimitry Andric 889*81ad6265SDimitry Andric // Decoder index. 890*81ad6265SDimitry Andric for (; *I >= 128; ++I) 891*81ad6265SDimitry Andric OS << (unsigned)*I << ", "; 892*81ad6265SDimitry Andric OS << (unsigned)*I++ << ", "; 893*81ad6265SDimitry Andric 894*81ad6265SDimitry Andric if (!IsTry) { 895*81ad6265SDimitry Andric OS << "// Opcode: " << NumberedEncodings[Opc] << "\n"; 896*81ad6265SDimitry Andric break; 897*81ad6265SDimitry Andric } 898*81ad6265SDimitry Andric 899*81ad6265SDimitry Andric // Fallthrough for OPC_TryDecode. 900*81ad6265SDimitry Andric 901*81ad6265SDimitry Andric // 24-bit numtoskip value. 902*81ad6265SDimitry Andric uint8_t Byte = *I++; 903*81ad6265SDimitry Andric uint32_t NumToSkip = Byte; 904*81ad6265SDimitry Andric OS << (unsigned)Byte << ", "; 905*81ad6265SDimitry Andric Byte = *I++; 906*81ad6265SDimitry Andric OS << (unsigned)Byte << ", "; 907*81ad6265SDimitry Andric NumToSkip |= Byte << 8; 908*81ad6265SDimitry Andric Byte = *I++; 909*81ad6265SDimitry Andric OS << utostr(Byte) << ", "; 910*81ad6265SDimitry Andric NumToSkip |= Byte << 16; 911*81ad6265SDimitry Andric 912*81ad6265SDimitry Andric OS << "// Opcode: " << NumberedEncodings[Opc] 913*81ad6265SDimitry Andric << ", skip to: " << ((I - Table.begin()) + NumToSkip) << "\n"; 914*81ad6265SDimitry Andric break; 915*81ad6265SDimitry Andric } 916*81ad6265SDimitry Andric case MCD::OPC_SoftFail: { 917*81ad6265SDimitry Andric ++I; 918*81ad6265SDimitry Andric OS.indent(Indentation) << "MCD::OPC_SoftFail"; 919*81ad6265SDimitry Andric // Positive mask 920*81ad6265SDimitry Andric uint64_t Value = 0; 921*81ad6265SDimitry Andric unsigned Shift = 0; 922*81ad6265SDimitry Andric do { 923*81ad6265SDimitry Andric OS << ", " << (unsigned)*I; 924*81ad6265SDimitry Andric Value += (*I & 0x7f) << Shift; 925*81ad6265SDimitry Andric Shift += 7; 926*81ad6265SDimitry Andric } while (*I++ >= 128); 927*81ad6265SDimitry Andric if (Value > 127) { 928*81ad6265SDimitry Andric OS << " /* 0x"; 929*81ad6265SDimitry Andric OS.write_hex(Value); 930*81ad6265SDimitry Andric OS << " */"; 931*81ad6265SDimitry Andric } 932*81ad6265SDimitry Andric // Negative mask 933*81ad6265SDimitry Andric Value = 0; 934*81ad6265SDimitry Andric Shift = 0; 935*81ad6265SDimitry Andric do { 936*81ad6265SDimitry Andric OS << ", " << (unsigned)*I; 937*81ad6265SDimitry Andric Value += (*I & 0x7f) << Shift; 938*81ad6265SDimitry Andric Shift += 7; 939*81ad6265SDimitry Andric } while (*I++ >= 128); 940*81ad6265SDimitry Andric if (Value > 127) { 941*81ad6265SDimitry Andric OS << " /* 0x"; 942*81ad6265SDimitry Andric OS.write_hex(Value); 943*81ad6265SDimitry Andric OS << " */"; 944*81ad6265SDimitry Andric } 945*81ad6265SDimitry Andric OS << ",\n"; 946*81ad6265SDimitry Andric break; 947*81ad6265SDimitry Andric } 948*81ad6265SDimitry Andric case MCD::OPC_Fail: { 949*81ad6265SDimitry Andric ++I; 950*81ad6265SDimitry Andric OS.indent(Indentation) << "MCD::OPC_Fail,\n"; 951*81ad6265SDimitry Andric break; 952*81ad6265SDimitry Andric } 953*81ad6265SDimitry Andric } 954*81ad6265SDimitry Andric } 955*81ad6265SDimitry Andric OS.indent(Indentation) << "0\n"; 956*81ad6265SDimitry Andric 957*81ad6265SDimitry Andric Indentation -= 2; 958*81ad6265SDimitry Andric 959*81ad6265SDimitry Andric OS.indent(Indentation) << "};\n\n"; 960*81ad6265SDimitry Andric } 961*81ad6265SDimitry Andric 962*81ad6265SDimitry Andric void DecoderEmitter::emitInstrLenTable(formatted_raw_ostream &OS, 963*81ad6265SDimitry Andric std::vector<unsigned> &InstrLen) const { 964*81ad6265SDimitry Andric OS << "static const uint8_t InstrLenTable[] = {\n"; 965*81ad6265SDimitry Andric for (unsigned &Len : InstrLen) { 966*81ad6265SDimitry Andric OS << Len << ",\n"; 967*81ad6265SDimitry Andric } 968*81ad6265SDimitry Andric OS << "};\n\n"; 969*81ad6265SDimitry Andric } 970*81ad6265SDimitry Andric 971*81ad6265SDimitry Andric void DecoderEmitter::emitPredicateFunction(formatted_raw_ostream &OS, 972*81ad6265SDimitry Andric PredicateSet &Predicates, 973*81ad6265SDimitry Andric unsigned Indentation) const { 974*81ad6265SDimitry Andric // The predicate function is just a big switch statement based on the 975*81ad6265SDimitry Andric // input predicate index. 976*81ad6265SDimitry Andric OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, " 977*81ad6265SDimitry Andric << "const FeatureBitset &Bits) {\n"; 978*81ad6265SDimitry Andric Indentation += 2; 979*81ad6265SDimitry Andric if (!Predicates.empty()) { 980*81ad6265SDimitry Andric OS.indent(Indentation) << "switch (Idx) {\n"; 981*81ad6265SDimitry Andric OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n"; 982*81ad6265SDimitry Andric unsigned Index = 0; 983*81ad6265SDimitry Andric for (const auto &Predicate : Predicates) { 984*81ad6265SDimitry Andric OS.indent(Indentation) << "case " << Index++ << ":\n"; 985*81ad6265SDimitry Andric OS.indent(Indentation+2) << "return (" << Predicate << ");\n"; 986*81ad6265SDimitry Andric } 987*81ad6265SDimitry Andric OS.indent(Indentation) << "}\n"; 988*81ad6265SDimitry Andric } else { 989*81ad6265SDimitry Andric // No case statement to emit 990*81ad6265SDimitry Andric OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n"; 991*81ad6265SDimitry Andric } 992*81ad6265SDimitry Andric Indentation -= 2; 993*81ad6265SDimitry Andric OS.indent(Indentation) << "}\n\n"; 994*81ad6265SDimitry Andric } 995*81ad6265SDimitry Andric 996*81ad6265SDimitry Andric void DecoderEmitter::emitDecoderFunction(formatted_raw_ostream &OS, 997*81ad6265SDimitry Andric DecoderSet &Decoders, 998*81ad6265SDimitry Andric unsigned Indentation) const { 999*81ad6265SDimitry Andric // The decoder function is just a big switch statement based on the 1000*81ad6265SDimitry Andric // input decoder index. 1001*81ad6265SDimitry Andric OS.indent(Indentation) << "template <typename InsnType>\n"; 1002*81ad6265SDimitry Andric OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S," 1003*81ad6265SDimitry Andric << " unsigned Idx, InsnType insn, MCInst &MI,\n"; 1004*81ad6265SDimitry Andric OS.indent(Indentation) 1005*81ad6265SDimitry Andric << " uint64_t " 1006*81ad6265SDimitry Andric << "Address, const MCDisassembler *Decoder, bool &DecodeComplete) {\n"; 1007*81ad6265SDimitry Andric Indentation += 2; 1008*81ad6265SDimitry Andric OS.indent(Indentation) << "DecodeComplete = true;\n"; 1009*81ad6265SDimitry Andric // TODO: When InsnType is large, using uint64_t limits all fields to 64 bits 1010*81ad6265SDimitry Andric // It would be better for emitBinaryParser to use a 64-bit tmp whenever 1011*81ad6265SDimitry Andric // possible but fall back to an InsnType-sized tmp for truly large fields. 1012*81ad6265SDimitry Andric OS.indent(Indentation) << "using TmpType = " 1013*81ad6265SDimitry Andric "std::conditional_t<std::is_integral<InsnType>::" 1014*81ad6265SDimitry Andric "value, InsnType, uint64_t>;\n"; 1015*81ad6265SDimitry Andric OS.indent(Indentation) << "TmpType tmp;\n"; 1016*81ad6265SDimitry Andric OS.indent(Indentation) << "switch (Idx) {\n"; 1017*81ad6265SDimitry Andric OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n"; 1018*81ad6265SDimitry Andric unsigned Index = 0; 1019*81ad6265SDimitry Andric for (const auto &Decoder : Decoders) { 1020*81ad6265SDimitry Andric OS.indent(Indentation) << "case " << Index++ << ":\n"; 1021*81ad6265SDimitry Andric OS << Decoder; 1022*81ad6265SDimitry Andric OS.indent(Indentation+2) << "return S;\n"; 1023*81ad6265SDimitry Andric } 1024*81ad6265SDimitry Andric OS.indent(Indentation) << "}\n"; 1025*81ad6265SDimitry Andric Indentation -= 2; 1026*81ad6265SDimitry Andric OS.indent(Indentation) << "}\n\n"; 1027*81ad6265SDimitry Andric } 1028*81ad6265SDimitry Andric 1029*81ad6265SDimitry Andric // Populates the field of the insn given the start position and the number of 1030*81ad6265SDimitry Andric // consecutive bits to scan for. 1031*81ad6265SDimitry Andric // 1032*81ad6265SDimitry Andric // Returns false if and on the first uninitialized bit value encountered. 1033*81ad6265SDimitry Andric // Returns true, otherwise. 1034*81ad6265SDimitry Andric bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn, 1035*81ad6265SDimitry Andric unsigned StartBit, unsigned NumBits) const { 1036*81ad6265SDimitry Andric Field = 0; 1037*81ad6265SDimitry Andric 1038*81ad6265SDimitry Andric for (unsigned i = 0; i < NumBits; ++i) { 1039*81ad6265SDimitry Andric if (Insn[StartBit + i] == BIT_UNSET) 1040*81ad6265SDimitry Andric return false; 1041*81ad6265SDimitry Andric 1042*81ad6265SDimitry Andric if (Insn[StartBit + i] == BIT_TRUE) 1043*81ad6265SDimitry Andric Field = Field | (1ULL << i); 1044*81ad6265SDimitry Andric } 1045*81ad6265SDimitry Andric 1046*81ad6265SDimitry Andric return true; 1047*81ad6265SDimitry Andric } 1048*81ad6265SDimitry Andric 1049*81ad6265SDimitry Andric /// dumpFilterArray - dumpFilterArray prints out debugging info for the given 1050*81ad6265SDimitry Andric /// filter array as a series of chars. 1051*81ad6265SDimitry Andric void FilterChooser::dumpFilterArray(raw_ostream &o, 1052*81ad6265SDimitry Andric const std::vector<bit_value_t> &filter) const { 1053*81ad6265SDimitry Andric for (unsigned bitIndex = BitWidth; bitIndex > 0; bitIndex--) { 1054*81ad6265SDimitry Andric switch (filter[bitIndex - 1]) { 1055*81ad6265SDimitry Andric case BIT_UNFILTERED: 1056*81ad6265SDimitry Andric o << "."; 1057*81ad6265SDimitry Andric break; 1058*81ad6265SDimitry Andric case BIT_UNSET: 1059*81ad6265SDimitry Andric o << "_"; 1060*81ad6265SDimitry Andric break; 1061*81ad6265SDimitry Andric case BIT_TRUE: 1062*81ad6265SDimitry Andric o << "1"; 1063*81ad6265SDimitry Andric break; 1064*81ad6265SDimitry Andric case BIT_FALSE: 1065*81ad6265SDimitry Andric o << "0"; 1066*81ad6265SDimitry Andric break; 1067*81ad6265SDimitry Andric } 1068*81ad6265SDimitry Andric } 1069*81ad6265SDimitry Andric } 1070*81ad6265SDimitry Andric 1071*81ad6265SDimitry Andric /// dumpStack - dumpStack traverses the filter chooser chain and calls 1072*81ad6265SDimitry Andric /// dumpFilterArray on each filter chooser up to the top level one. 1073*81ad6265SDimitry Andric void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const { 1074*81ad6265SDimitry Andric const FilterChooser *current = this; 1075*81ad6265SDimitry Andric 1076*81ad6265SDimitry Andric while (current) { 1077*81ad6265SDimitry Andric o << prefix; 1078*81ad6265SDimitry Andric dumpFilterArray(o, current->FilterBitValues); 1079*81ad6265SDimitry Andric o << '\n'; 1080*81ad6265SDimitry Andric current = current->Parent; 1081*81ad6265SDimitry Andric } 1082*81ad6265SDimitry Andric } 1083*81ad6265SDimitry Andric 1084*81ad6265SDimitry Andric // Calculates the island(s) needed to decode the instruction. 1085*81ad6265SDimitry Andric // This returns a list of undecoded bits of an instructions, for example, 1086*81ad6265SDimitry Andric // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be 1087*81ad6265SDimitry Andric // decoded bits in order to verify that the instruction matches the Opcode. 1088*81ad6265SDimitry Andric unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits, 1089*81ad6265SDimitry Andric std::vector<unsigned> &EndBits, 1090*81ad6265SDimitry Andric std::vector<uint64_t> &FieldVals, 1091*81ad6265SDimitry Andric const insn_t &Insn) const { 1092*81ad6265SDimitry Andric unsigned Num, BitNo; 1093*81ad6265SDimitry Andric Num = BitNo = 0; 1094*81ad6265SDimitry Andric 1095*81ad6265SDimitry Andric uint64_t FieldVal = 0; 1096*81ad6265SDimitry Andric 1097*81ad6265SDimitry Andric // 0: Init 1098*81ad6265SDimitry Andric // 1: Water (the bit value does not affect decoding) 1099*81ad6265SDimitry Andric // 2: Island (well-known bit value needed for decoding) 1100*81ad6265SDimitry Andric int State = 0; 1101*81ad6265SDimitry Andric 1102*81ad6265SDimitry Andric for (unsigned i = 0; i < BitWidth; ++i) { 1103*81ad6265SDimitry Andric int64_t Val = Value(Insn[i]); 1104*81ad6265SDimitry Andric bool Filtered = PositionFiltered(i); 1105*81ad6265SDimitry Andric switch (State) { 1106*81ad6265SDimitry Andric default: llvm_unreachable("Unreachable code!"); 1107*81ad6265SDimitry Andric case 0: 1108*81ad6265SDimitry Andric case 1: 1109*81ad6265SDimitry Andric if (Filtered || Val == -1) 1110*81ad6265SDimitry Andric State = 1; // Still in Water 1111*81ad6265SDimitry Andric else { 1112*81ad6265SDimitry Andric State = 2; // Into the Island 1113*81ad6265SDimitry Andric BitNo = 0; 1114*81ad6265SDimitry Andric StartBits.push_back(i); 1115*81ad6265SDimitry Andric FieldVal = Val; 1116*81ad6265SDimitry Andric } 1117*81ad6265SDimitry Andric break; 1118*81ad6265SDimitry Andric case 2: 1119*81ad6265SDimitry Andric if (Filtered || Val == -1) { 1120*81ad6265SDimitry Andric State = 1; // Into the Water 1121*81ad6265SDimitry Andric EndBits.push_back(i - 1); 1122*81ad6265SDimitry Andric FieldVals.push_back(FieldVal); 1123*81ad6265SDimitry Andric ++Num; 1124*81ad6265SDimitry Andric } else { 1125*81ad6265SDimitry Andric State = 2; // Still in Island 1126*81ad6265SDimitry Andric ++BitNo; 1127*81ad6265SDimitry Andric FieldVal = FieldVal | Val << BitNo; 1128*81ad6265SDimitry Andric } 1129*81ad6265SDimitry Andric break; 1130*81ad6265SDimitry Andric } 1131*81ad6265SDimitry Andric } 1132*81ad6265SDimitry Andric // If we are still in Island after the loop, do some housekeeping. 1133*81ad6265SDimitry Andric if (State == 2) { 1134*81ad6265SDimitry Andric EndBits.push_back(BitWidth - 1); 1135*81ad6265SDimitry Andric FieldVals.push_back(FieldVal); 1136*81ad6265SDimitry Andric ++Num; 1137*81ad6265SDimitry Andric } 1138*81ad6265SDimitry Andric 1139*81ad6265SDimitry Andric assert(StartBits.size() == Num && EndBits.size() == Num && 1140*81ad6265SDimitry Andric FieldVals.size() == Num); 1141*81ad6265SDimitry Andric return Num; 1142*81ad6265SDimitry Andric } 1143*81ad6265SDimitry Andric 1144*81ad6265SDimitry Andric void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation, 1145*81ad6265SDimitry Andric const OperandInfo &OpInfo, 1146*81ad6265SDimitry Andric bool &OpHasCompleteDecoder) const { 1147*81ad6265SDimitry Andric const std::string &Decoder = OpInfo.Decoder; 1148*81ad6265SDimitry Andric 1149*81ad6265SDimitry Andric bool UseInsertBits = OpInfo.numFields() != 1 || OpInfo.InitValue != 0; 1150*81ad6265SDimitry Andric 1151*81ad6265SDimitry Andric if (UseInsertBits) { 1152*81ad6265SDimitry Andric o.indent(Indentation) << "tmp = 0x"; 1153*81ad6265SDimitry Andric o.write_hex(OpInfo.InitValue); 1154*81ad6265SDimitry Andric o << ";\n"; 1155*81ad6265SDimitry Andric } 1156*81ad6265SDimitry Andric 1157*81ad6265SDimitry Andric for (const EncodingField &EF : OpInfo) { 1158*81ad6265SDimitry Andric o.indent(Indentation); 1159*81ad6265SDimitry Andric if (UseInsertBits) 1160*81ad6265SDimitry Andric o << "insertBits(tmp, "; 1161*81ad6265SDimitry Andric else 1162*81ad6265SDimitry Andric o << "tmp = "; 1163*81ad6265SDimitry Andric o << "fieldFromInstruction(insn, " << EF.Base << ", " << EF.Width << ')'; 1164*81ad6265SDimitry Andric if (UseInsertBits) 1165*81ad6265SDimitry Andric o << ", " << EF.Offset << ", " << EF.Width << ')'; 1166*81ad6265SDimitry Andric else if (EF.Offset != 0) 1167*81ad6265SDimitry Andric o << " << " << EF.Offset; 1168*81ad6265SDimitry Andric o << ";\n"; 1169*81ad6265SDimitry Andric } 1170*81ad6265SDimitry Andric 1171*81ad6265SDimitry Andric if (Decoder != "") { 1172*81ad6265SDimitry Andric OpHasCompleteDecoder = OpInfo.HasCompleteDecoder; 1173*81ad6265SDimitry Andric o.indent(Indentation) << Emitter->GuardPrefix << Decoder 1174*81ad6265SDimitry Andric << "(MI, tmp, Address, Decoder)" 1175*81ad6265SDimitry Andric << Emitter->GuardPostfix 1176*81ad6265SDimitry Andric << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ") 1177*81ad6265SDimitry Andric << "return MCDisassembler::Fail; }\n"; 1178*81ad6265SDimitry Andric } else { 1179*81ad6265SDimitry Andric OpHasCompleteDecoder = true; 1180*81ad6265SDimitry Andric o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n"; 1181*81ad6265SDimitry Andric } 1182*81ad6265SDimitry Andric } 1183*81ad6265SDimitry Andric 1184*81ad6265SDimitry Andric void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation, 1185*81ad6265SDimitry Andric unsigned Opc, bool &HasCompleteDecoder) const { 1186*81ad6265SDimitry Andric HasCompleteDecoder = true; 1187*81ad6265SDimitry Andric 1188*81ad6265SDimitry Andric for (const auto &Op : Operands.find(Opc)->second) { 1189*81ad6265SDimitry Andric // If a custom instruction decoder was specified, use that. 1190*81ad6265SDimitry Andric if (Op.numFields() == 0 && !Op.Decoder.empty()) { 1191*81ad6265SDimitry Andric HasCompleteDecoder = Op.HasCompleteDecoder; 1192*81ad6265SDimitry Andric OS.indent(Indentation) << Emitter->GuardPrefix << Op.Decoder 1193*81ad6265SDimitry Andric << "(MI, insn, Address, Decoder)" 1194*81ad6265SDimitry Andric << Emitter->GuardPostfix 1195*81ad6265SDimitry Andric << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ") 1196*81ad6265SDimitry Andric << "return MCDisassembler::Fail; }\n"; 1197*81ad6265SDimitry Andric break; 1198*81ad6265SDimitry Andric } 1199*81ad6265SDimitry Andric 1200*81ad6265SDimitry Andric bool OpHasCompleteDecoder; 1201*81ad6265SDimitry Andric emitBinaryParser(OS, Indentation, Op, OpHasCompleteDecoder); 1202*81ad6265SDimitry Andric if (!OpHasCompleteDecoder) 1203*81ad6265SDimitry Andric HasCompleteDecoder = false; 1204*81ad6265SDimitry Andric } 1205*81ad6265SDimitry Andric } 1206*81ad6265SDimitry Andric 1207*81ad6265SDimitry Andric unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders, 1208*81ad6265SDimitry Andric unsigned Opc, 1209*81ad6265SDimitry Andric bool &HasCompleteDecoder) const { 1210*81ad6265SDimitry Andric // Build up the predicate string. 1211*81ad6265SDimitry Andric SmallString<256> Decoder; 1212*81ad6265SDimitry Andric // FIXME: emitDecoder() function can take a buffer directly rather than 1213*81ad6265SDimitry Andric // a stream. 1214*81ad6265SDimitry Andric raw_svector_ostream S(Decoder); 1215*81ad6265SDimitry Andric unsigned I = 4; 1216*81ad6265SDimitry Andric emitDecoder(S, I, Opc, HasCompleteDecoder); 1217*81ad6265SDimitry Andric 1218*81ad6265SDimitry Andric // Using the full decoder string as the key value here is a bit 1219*81ad6265SDimitry Andric // heavyweight, but is effective. If the string comparisons become a 1220*81ad6265SDimitry Andric // performance concern, we can implement a mangling of the predicate 1221*81ad6265SDimitry Andric // data easily enough with a map back to the actual string. That's 1222*81ad6265SDimitry Andric // overkill for now, though. 1223*81ad6265SDimitry Andric 1224*81ad6265SDimitry Andric // Make sure the predicate is in the table. 1225*81ad6265SDimitry Andric Decoders.insert(CachedHashString(Decoder)); 1226*81ad6265SDimitry Andric // Now figure out the index for when we write out the table. 1227*81ad6265SDimitry Andric DecoderSet::const_iterator P = find(Decoders, Decoder.str()); 1228*81ad6265SDimitry Andric return (unsigned)(P - Decoders.begin()); 1229*81ad6265SDimitry Andric } 1230*81ad6265SDimitry Andric 1231*81ad6265SDimitry Andric // If ParenIfBinOp is true, print a surrounding () if Val uses && or ||. 1232*81ad6265SDimitry Andric bool FilterChooser::emitPredicateMatchAux(const Init &Val, bool ParenIfBinOp, 1233*81ad6265SDimitry Andric raw_ostream &OS) const { 1234*81ad6265SDimitry Andric if (auto *D = dyn_cast<DefInit>(&Val)) { 1235*81ad6265SDimitry Andric if (!D->getDef()->isSubClassOf("SubtargetFeature")) 1236*81ad6265SDimitry Andric return true; 1237*81ad6265SDimitry Andric OS << "Bits[" << Emitter->PredicateNamespace << "::" << D->getAsString() 1238*81ad6265SDimitry Andric << "]"; 1239*81ad6265SDimitry Andric return false; 1240*81ad6265SDimitry Andric } 1241*81ad6265SDimitry Andric if (auto *D = dyn_cast<DagInit>(&Val)) { 1242*81ad6265SDimitry Andric std::string Op = D->getOperator()->getAsString(); 1243*81ad6265SDimitry Andric if (Op == "not" && D->getNumArgs() == 1) { 1244*81ad6265SDimitry Andric OS << '!'; 1245*81ad6265SDimitry Andric return emitPredicateMatchAux(*D->getArg(0), true, OS); 1246*81ad6265SDimitry Andric } 1247*81ad6265SDimitry Andric if ((Op == "any_of" || Op == "all_of") && D->getNumArgs() > 0) { 1248*81ad6265SDimitry Andric bool Paren = D->getNumArgs() > 1 && std::exchange(ParenIfBinOp, true); 1249*81ad6265SDimitry Andric if (Paren) 1250*81ad6265SDimitry Andric OS << '('; 1251*81ad6265SDimitry Andric ListSeparator LS(Op == "any_of" ? " || " : " && "); 1252*81ad6265SDimitry Andric for (auto *Arg : D->getArgs()) { 1253*81ad6265SDimitry Andric OS << LS; 1254*81ad6265SDimitry Andric if (emitPredicateMatchAux(*Arg, ParenIfBinOp, OS)) 1255*81ad6265SDimitry Andric return true; 1256*81ad6265SDimitry Andric } 1257*81ad6265SDimitry Andric if (Paren) 1258*81ad6265SDimitry Andric OS << ')'; 1259*81ad6265SDimitry Andric return false; 1260*81ad6265SDimitry Andric } 1261*81ad6265SDimitry Andric } 1262*81ad6265SDimitry Andric return true; 1263*81ad6265SDimitry Andric } 1264*81ad6265SDimitry Andric 1265*81ad6265SDimitry Andric bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation, 1266*81ad6265SDimitry Andric unsigned Opc) const { 1267*81ad6265SDimitry Andric ListInit *Predicates = 1268*81ad6265SDimitry Andric AllInstructions[Opc].EncodingDef->getValueAsListInit("Predicates"); 1269*81ad6265SDimitry Andric bool IsFirstEmission = true; 1270*81ad6265SDimitry Andric for (unsigned i = 0; i < Predicates->size(); ++i) { 1271*81ad6265SDimitry Andric Record *Pred = Predicates->getElementAsRecord(i); 1272*81ad6265SDimitry Andric if (!Pred->getValue("AssemblerMatcherPredicate")) 1273*81ad6265SDimitry Andric continue; 1274*81ad6265SDimitry Andric 1275*81ad6265SDimitry Andric if (!isa<DagInit>(Pred->getValue("AssemblerCondDag")->getValue())) 1276*81ad6265SDimitry Andric continue; 1277*81ad6265SDimitry Andric 1278*81ad6265SDimitry Andric if (!IsFirstEmission) 1279*81ad6265SDimitry Andric o << " && "; 1280*81ad6265SDimitry Andric if (emitPredicateMatchAux(*Pred->getValueAsDag("AssemblerCondDag"), 1281*81ad6265SDimitry Andric Predicates->size() > 1, o)) 1282*81ad6265SDimitry Andric PrintFatalError(Pred->getLoc(), "Invalid AssemblerCondDag!"); 1283*81ad6265SDimitry Andric IsFirstEmission = false; 1284*81ad6265SDimitry Andric } 1285*81ad6265SDimitry Andric return !Predicates->empty(); 1286*81ad6265SDimitry Andric } 1287*81ad6265SDimitry Andric 1288*81ad6265SDimitry Andric bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const { 1289*81ad6265SDimitry Andric ListInit *Predicates = 1290*81ad6265SDimitry Andric AllInstructions[Opc].EncodingDef->getValueAsListInit("Predicates"); 1291*81ad6265SDimitry Andric for (unsigned i = 0; i < Predicates->size(); ++i) { 1292*81ad6265SDimitry Andric Record *Pred = Predicates->getElementAsRecord(i); 1293*81ad6265SDimitry Andric if (!Pred->getValue("AssemblerMatcherPredicate")) 1294*81ad6265SDimitry Andric continue; 1295*81ad6265SDimitry Andric 1296*81ad6265SDimitry Andric if (isa<DagInit>(Pred->getValue("AssemblerCondDag")->getValue())) 1297*81ad6265SDimitry Andric return true; 1298*81ad6265SDimitry Andric } 1299*81ad6265SDimitry Andric return false; 1300*81ad6265SDimitry Andric } 1301*81ad6265SDimitry Andric 1302*81ad6265SDimitry Andric unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo, 1303*81ad6265SDimitry Andric StringRef Predicate) const { 1304*81ad6265SDimitry Andric // Using the full predicate string as the key value here is a bit 1305*81ad6265SDimitry Andric // heavyweight, but is effective. If the string comparisons become a 1306*81ad6265SDimitry Andric // performance concern, we can implement a mangling of the predicate 1307*81ad6265SDimitry Andric // data easily enough with a map back to the actual string. That's 1308*81ad6265SDimitry Andric // overkill for now, though. 1309*81ad6265SDimitry Andric 1310*81ad6265SDimitry Andric // Make sure the predicate is in the table. 1311*81ad6265SDimitry Andric TableInfo.Predicates.insert(CachedHashString(Predicate)); 1312*81ad6265SDimitry Andric // Now figure out the index for when we write out the table. 1313*81ad6265SDimitry Andric PredicateSet::const_iterator P = find(TableInfo.Predicates, Predicate); 1314*81ad6265SDimitry Andric return (unsigned)(P - TableInfo.Predicates.begin()); 1315*81ad6265SDimitry Andric } 1316*81ad6265SDimitry Andric 1317*81ad6265SDimitry Andric void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo, 1318*81ad6265SDimitry Andric unsigned Opc) const { 1319*81ad6265SDimitry Andric if (!doesOpcodeNeedPredicate(Opc)) 1320*81ad6265SDimitry Andric return; 1321*81ad6265SDimitry Andric 1322*81ad6265SDimitry Andric // Build up the predicate string. 1323*81ad6265SDimitry Andric SmallString<256> Predicate; 1324*81ad6265SDimitry Andric // FIXME: emitPredicateMatch() functions can take a buffer directly rather 1325*81ad6265SDimitry Andric // than a stream. 1326*81ad6265SDimitry Andric raw_svector_ostream PS(Predicate); 1327*81ad6265SDimitry Andric unsigned I = 0; 1328*81ad6265SDimitry Andric emitPredicateMatch(PS, I, Opc); 1329*81ad6265SDimitry Andric 1330*81ad6265SDimitry Andric // Figure out the index into the predicate table for the predicate just 1331*81ad6265SDimitry Andric // computed. 1332*81ad6265SDimitry Andric unsigned PIdx = getPredicateIndex(TableInfo, PS.str()); 1333*81ad6265SDimitry Andric SmallString<16> PBytes; 1334*81ad6265SDimitry Andric raw_svector_ostream S(PBytes); 1335*81ad6265SDimitry Andric encodeULEB128(PIdx, S); 1336*81ad6265SDimitry Andric 1337*81ad6265SDimitry Andric TableInfo.Table.push_back(MCD::OPC_CheckPredicate); 1338*81ad6265SDimitry Andric // Predicate index 1339*81ad6265SDimitry Andric for (unsigned i = 0, e = PBytes.size(); i != e; ++i) 1340*81ad6265SDimitry Andric TableInfo.Table.push_back(PBytes[i]); 1341*81ad6265SDimitry Andric // Push location for NumToSkip backpatching. 1342*81ad6265SDimitry Andric TableInfo.FixupStack.back().push_back(TableInfo.Table.size()); 1343*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1344*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1345*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1346*81ad6265SDimitry Andric } 1347*81ad6265SDimitry Andric 1348*81ad6265SDimitry Andric void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo, 1349*81ad6265SDimitry Andric unsigned Opc) const { 1350*81ad6265SDimitry Andric const RecordVal *RV = AllInstructions[Opc].EncodingDef->getValue("SoftFail"); 1351*81ad6265SDimitry Andric BitsInit *SFBits = RV ? dyn_cast<BitsInit>(RV->getValue()) : nullptr; 1352*81ad6265SDimitry Andric 1353*81ad6265SDimitry Andric if (!SFBits) return; 1354*81ad6265SDimitry Andric BitsInit *InstBits = 1355*81ad6265SDimitry Andric AllInstructions[Opc].EncodingDef->getValueAsBitsInit("Inst"); 1356*81ad6265SDimitry Andric 1357*81ad6265SDimitry Andric APInt PositiveMask(BitWidth, 0ULL); 1358*81ad6265SDimitry Andric APInt NegativeMask(BitWidth, 0ULL); 1359*81ad6265SDimitry Andric for (unsigned i = 0; i < BitWidth; ++i) { 1360*81ad6265SDimitry Andric bit_value_t B = bitFromBits(*SFBits, i); 1361*81ad6265SDimitry Andric bit_value_t IB = bitFromBits(*InstBits, i); 1362*81ad6265SDimitry Andric 1363*81ad6265SDimitry Andric if (B != BIT_TRUE) continue; 1364*81ad6265SDimitry Andric 1365*81ad6265SDimitry Andric switch (IB) { 1366*81ad6265SDimitry Andric case BIT_FALSE: 1367*81ad6265SDimitry Andric // The bit is meant to be false, so emit a check to see if it is true. 1368*81ad6265SDimitry Andric PositiveMask.setBit(i); 1369*81ad6265SDimitry Andric break; 1370*81ad6265SDimitry Andric case BIT_TRUE: 1371*81ad6265SDimitry Andric // The bit is meant to be true, so emit a check to see if it is false. 1372*81ad6265SDimitry Andric NegativeMask.setBit(i); 1373*81ad6265SDimitry Andric break; 1374*81ad6265SDimitry Andric default: 1375*81ad6265SDimitry Andric // The bit is not set; this must be an error! 1376*81ad6265SDimitry Andric errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in " 1377*81ad6265SDimitry Andric << AllInstructions[Opc] << " is set but Inst{" << i 1378*81ad6265SDimitry Andric << "} is unset!\n" 1379*81ad6265SDimitry Andric << " - You can only mark a bit as SoftFail if it is fully defined" 1380*81ad6265SDimitry Andric << " (1/0 - not '?') in Inst\n"; 1381*81ad6265SDimitry Andric return; 1382*81ad6265SDimitry Andric } 1383*81ad6265SDimitry Andric } 1384*81ad6265SDimitry Andric 1385*81ad6265SDimitry Andric bool NeedPositiveMask = PositiveMask.getBoolValue(); 1386*81ad6265SDimitry Andric bool NeedNegativeMask = NegativeMask.getBoolValue(); 1387*81ad6265SDimitry Andric 1388*81ad6265SDimitry Andric if (!NeedPositiveMask && !NeedNegativeMask) 1389*81ad6265SDimitry Andric return; 1390*81ad6265SDimitry Andric 1391*81ad6265SDimitry Andric TableInfo.Table.push_back(MCD::OPC_SoftFail); 1392*81ad6265SDimitry Andric 1393*81ad6265SDimitry Andric SmallString<16> MaskBytes; 1394*81ad6265SDimitry Andric raw_svector_ostream S(MaskBytes); 1395*81ad6265SDimitry Andric if (NeedPositiveMask) { 1396*81ad6265SDimitry Andric encodeULEB128(PositiveMask.getZExtValue(), S); 1397*81ad6265SDimitry Andric for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i) 1398*81ad6265SDimitry Andric TableInfo.Table.push_back(MaskBytes[i]); 1399*81ad6265SDimitry Andric } else 1400*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1401*81ad6265SDimitry Andric if (NeedNegativeMask) { 1402*81ad6265SDimitry Andric MaskBytes.clear(); 1403*81ad6265SDimitry Andric encodeULEB128(NegativeMask.getZExtValue(), S); 1404*81ad6265SDimitry Andric for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i) 1405*81ad6265SDimitry Andric TableInfo.Table.push_back(MaskBytes[i]); 1406*81ad6265SDimitry Andric } else 1407*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1408*81ad6265SDimitry Andric } 1409*81ad6265SDimitry Andric 1410*81ad6265SDimitry Andric // Emits table entries to decode the singleton. 1411*81ad6265SDimitry Andric void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo, 1412*81ad6265SDimitry Andric EncodingIDAndOpcode Opc) const { 1413*81ad6265SDimitry Andric std::vector<unsigned> StartBits; 1414*81ad6265SDimitry Andric std::vector<unsigned> EndBits; 1415*81ad6265SDimitry Andric std::vector<uint64_t> FieldVals; 1416*81ad6265SDimitry Andric insn_t Insn; 1417*81ad6265SDimitry Andric insnWithID(Insn, Opc.EncodingID); 1418*81ad6265SDimitry Andric 1419*81ad6265SDimitry Andric // Look for islands of undecoded bits of the singleton. 1420*81ad6265SDimitry Andric getIslands(StartBits, EndBits, FieldVals, Insn); 1421*81ad6265SDimitry Andric 1422*81ad6265SDimitry Andric unsigned Size = StartBits.size(); 1423*81ad6265SDimitry Andric 1424*81ad6265SDimitry Andric // Emit the predicate table entry if one is needed. 1425*81ad6265SDimitry Andric emitPredicateTableEntry(TableInfo, Opc.EncodingID); 1426*81ad6265SDimitry Andric 1427*81ad6265SDimitry Andric // Check any additional encoding fields needed. 1428*81ad6265SDimitry Andric for (unsigned I = Size; I != 0; --I) { 1429*81ad6265SDimitry Andric unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1; 1430*81ad6265SDimitry Andric TableInfo.Table.push_back(MCD::OPC_CheckField); 1431*81ad6265SDimitry Andric TableInfo.Table.push_back(StartBits[I-1]); 1432*81ad6265SDimitry Andric TableInfo.Table.push_back(NumBits); 1433*81ad6265SDimitry Andric uint8_t Buffer[16], *p; 1434*81ad6265SDimitry Andric encodeULEB128(FieldVals[I-1], Buffer); 1435*81ad6265SDimitry Andric for (p = Buffer; *p >= 128 ; ++p) 1436*81ad6265SDimitry Andric TableInfo.Table.push_back(*p); 1437*81ad6265SDimitry Andric TableInfo.Table.push_back(*p); 1438*81ad6265SDimitry Andric // Push location for NumToSkip backpatching. 1439*81ad6265SDimitry Andric TableInfo.FixupStack.back().push_back(TableInfo.Table.size()); 1440*81ad6265SDimitry Andric // The fixup is always 24-bits, so go ahead and allocate the space 1441*81ad6265SDimitry Andric // in the table so all our relative position calculations work OK even 1442*81ad6265SDimitry Andric // before we fully resolve the real value here. 1443*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1444*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1445*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1446*81ad6265SDimitry Andric } 1447*81ad6265SDimitry Andric 1448*81ad6265SDimitry Andric // Check for soft failure of the match. 1449*81ad6265SDimitry Andric emitSoftFailTableEntry(TableInfo, Opc.EncodingID); 1450*81ad6265SDimitry Andric 1451*81ad6265SDimitry Andric bool HasCompleteDecoder; 1452*81ad6265SDimitry Andric unsigned DIdx = 1453*81ad6265SDimitry Andric getDecoderIndex(TableInfo.Decoders, Opc.EncodingID, HasCompleteDecoder); 1454*81ad6265SDimitry Andric 1455*81ad6265SDimitry Andric // Produce OPC_Decode or OPC_TryDecode opcode based on the information 1456*81ad6265SDimitry Andric // whether the instruction decoder is complete or not. If it is complete 1457*81ad6265SDimitry Andric // then it handles all possible values of remaining variable/unfiltered bits 1458*81ad6265SDimitry Andric // and for any value can determine if the bitpattern is a valid instruction 1459*81ad6265SDimitry Andric // or not. This means OPC_Decode will be the final step in the decoding 1460*81ad6265SDimitry Andric // process. If it is not complete, then the Fail return code from the 1461*81ad6265SDimitry Andric // decoder method indicates that additional processing should be done to see 1462*81ad6265SDimitry Andric // if there is any other instruction that also matches the bitpattern and 1463*81ad6265SDimitry Andric // can decode it. 1464*81ad6265SDimitry Andric TableInfo.Table.push_back(HasCompleteDecoder ? MCD::OPC_Decode : 1465*81ad6265SDimitry Andric MCD::OPC_TryDecode); 1466*81ad6265SDimitry Andric NumEncodingsSupported++; 1467*81ad6265SDimitry Andric uint8_t Buffer[16], *p; 1468*81ad6265SDimitry Andric encodeULEB128(Opc.Opcode, Buffer); 1469*81ad6265SDimitry Andric for (p = Buffer; *p >= 128 ; ++p) 1470*81ad6265SDimitry Andric TableInfo.Table.push_back(*p); 1471*81ad6265SDimitry Andric TableInfo.Table.push_back(*p); 1472*81ad6265SDimitry Andric 1473*81ad6265SDimitry Andric SmallString<16> Bytes; 1474*81ad6265SDimitry Andric raw_svector_ostream S(Bytes); 1475*81ad6265SDimitry Andric encodeULEB128(DIdx, S); 1476*81ad6265SDimitry Andric 1477*81ad6265SDimitry Andric // Decoder index 1478*81ad6265SDimitry Andric for (unsigned i = 0, e = Bytes.size(); i != e; ++i) 1479*81ad6265SDimitry Andric TableInfo.Table.push_back(Bytes[i]); 1480*81ad6265SDimitry Andric 1481*81ad6265SDimitry Andric if (!HasCompleteDecoder) { 1482*81ad6265SDimitry Andric // Push location for NumToSkip backpatching. 1483*81ad6265SDimitry Andric TableInfo.FixupStack.back().push_back(TableInfo.Table.size()); 1484*81ad6265SDimitry Andric // Allocate the space for the fixup. 1485*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1486*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1487*81ad6265SDimitry Andric TableInfo.Table.push_back(0); 1488*81ad6265SDimitry Andric } 1489*81ad6265SDimitry Andric } 1490*81ad6265SDimitry Andric 1491*81ad6265SDimitry Andric // Emits table entries to decode the singleton, and then to decode the rest. 1492*81ad6265SDimitry Andric void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo, 1493*81ad6265SDimitry Andric const Filter &Best) const { 1494*81ad6265SDimitry Andric EncodingIDAndOpcode Opc = Best.getSingletonOpc(); 1495*81ad6265SDimitry Andric 1496*81ad6265SDimitry Andric // complex singletons need predicate checks from the first singleton 1497*81ad6265SDimitry Andric // to refer forward to the variable filterchooser that follows. 1498*81ad6265SDimitry Andric TableInfo.FixupStack.emplace_back(); 1499*81ad6265SDimitry Andric 1500*81ad6265SDimitry Andric emitSingletonTableEntry(TableInfo, Opc); 1501*81ad6265SDimitry Andric 1502*81ad6265SDimitry Andric resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(), 1503*81ad6265SDimitry Andric TableInfo.Table.size()); 1504*81ad6265SDimitry Andric TableInfo.FixupStack.pop_back(); 1505*81ad6265SDimitry Andric 1506*81ad6265SDimitry Andric Best.getVariableFC().emitTableEntries(TableInfo); 1507*81ad6265SDimitry Andric } 1508*81ad6265SDimitry Andric 1509*81ad6265SDimitry Andric // Assign a single filter and run with it. Top level API client can initialize 1510*81ad6265SDimitry Andric // with a single filter to start the filtering process. 1511*81ad6265SDimitry Andric void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit, 1512*81ad6265SDimitry Andric bool mixed) { 1513*81ad6265SDimitry Andric Filters.clear(); 1514*81ad6265SDimitry Andric Filters.emplace_back(*this, startBit, numBit, true); 1515*81ad6265SDimitry Andric BestIndex = 0; // Sole Filter instance to choose from. 1516*81ad6265SDimitry Andric bestFilter().recurse(); 1517*81ad6265SDimitry Andric } 1518*81ad6265SDimitry Andric 1519*81ad6265SDimitry Andric // reportRegion is a helper function for filterProcessor to mark a region as 1520*81ad6265SDimitry Andric // eligible for use as a filter region. 1521*81ad6265SDimitry Andric void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit, 1522*81ad6265SDimitry Andric unsigned BitIndex, bool AllowMixed) { 1523*81ad6265SDimitry Andric if (RA == ATTR_MIXED && AllowMixed) 1524*81ad6265SDimitry Andric Filters.emplace_back(*this, StartBit, BitIndex - StartBit, true); 1525*81ad6265SDimitry Andric else if (RA == ATTR_ALL_SET && !AllowMixed) 1526*81ad6265SDimitry Andric Filters.emplace_back(*this, StartBit, BitIndex - StartBit, false); 1527*81ad6265SDimitry Andric } 1528*81ad6265SDimitry Andric 1529*81ad6265SDimitry Andric // FilterProcessor scans the well-known encoding bits of the instructions and 1530*81ad6265SDimitry Andric // builds up a list of candidate filters. It chooses the best filter and 1531*81ad6265SDimitry Andric // recursively descends down the decoding tree. 1532*81ad6265SDimitry Andric bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) { 1533*81ad6265SDimitry Andric Filters.clear(); 1534*81ad6265SDimitry Andric BestIndex = -1; 1535*81ad6265SDimitry Andric unsigned numInstructions = Opcodes.size(); 1536*81ad6265SDimitry Andric 1537*81ad6265SDimitry Andric assert(numInstructions && "Filter created with no instructions"); 1538*81ad6265SDimitry Andric 1539*81ad6265SDimitry Andric // No further filtering is necessary. 1540*81ad6265SDimitry Andric if (numInstructions == 1) 1541*81ad6265SDimitry Andric return true; 1542*81ad6265SDimitry Andric 1543*81ad6265SDimitry Andric // Heuristics. See also doFilter()'s "Heuristics" comment when num of 1544*81ad6265SDimitry Andric // instructions is 3. 1545*81ad6265SDimitry Andric if (AllowMixed && !Greedy) { 1546*81ad6265SDimitry Andric assert(numInstructions == 3); 1547*81ad6265SDimitry Andric 1548*81ad6265SDimitry Andric for (auto Opcode : Opcodes) { 1549*81ad6265SDimitry Andric std::vector<unsigned> StartBits; 1550*81ad6265SDimitry Andric std::vector<unsigned> EndBits; 1551*81ad6265SDimitry Andric std::vector<uint64_t> FieldVals; 1552*81ad6265SDimitry Andric insn_t Insn; 1553*81ad6265SDimitry Andric 1554*81ad6265SDimitry Andric insnWithID(Insn, Opcode.EncodingID); 1555*81ad6265SDimitry Andric 1556*81ad6265SDimitry Andric // Look for islands of undecoded bits of any instruction. 1557*81ad6265SDimitry Andric if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) { 1558*81ad6265SDimitry Andric // Found an instruction with island(s). Now just assign a filter. 1559*81ad6265SDimitry Andric runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true); 1560*81ad6265SDimitry Andric return true; 1561*81ad6265SDimitry Andric } 1562*81ad6265SDimitry Andric } 1563*81ad6265SDimitry Andric } 1564*81ad6265SDimitry Andric 1565*81ad6265SDimitry Andric unsigned BitIndex; 1566*81ad6265SDimitry Andric 1567*81ad6265SDimitry Andric // We maintain BIT_WIDTH copies of the bitAttrs automaton. 1568*81ad6265SDimitry Andric // The automaton consumes the corresponding bit from each 1569*81ad6265SDimitry Andric // instruction. 1570*81ad6265SDimitry Andric // 1571*81ad6265SDimitry Andric // Input symbols: 0, 1, and _ (unset). 1572*81ad6265SDimitry Andric // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED. 1573*81ad6265SDimitry Andric // Initial state: NONE. 1574*81ad6265SDimitry Andric // 1575*81ad6265SDimitry Andric // (NONE) ------- [01] -> (ALL_SET) 1576*81ad6265SDimitry Andric // (NONE) ------- _ ----> (ALL_UNSET) 1577*81ad6265SDimitry Andric // (ALL_SET) ---- [01] -> (ALL_SET) 1578*81ad6265SDimitry Andric // (ALL_SET) ---- _ ----> (MIXED) 1579*81ad6265SDimitry Andric // (ALL_UNSET) -- [01] -> (MIXED) 1580*81ad6265SDimitry Andric // (ALL_UNSET) -- _ ----> (ALL_UNSET) 1581*81ad6265SDimitry Andric // (MIXED) ------ . ----> (MIXED) 1582*81ad6265SDimitry Andric // (FILTERED)---- . ----> (FILTERED) 1583*81ad6265SDimitry Andric 1584*81ad6265SDimitry Andric std::vector<bitAttr_t> bitAttrs; 1585*81ad6265SDimitry Andric 1586*81ad6265SDimitry Andric // FILTERED bit positions provide no entropy and are not worthy of pursuing. 1587*81ad6265SDimitry Andric // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position. 1588*81ad6265SDimitry Andric for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) 1589*81ad6265SDimitry Andric if (FilterBitValues[BitIndex] == BIT_TRUE || 1590*81ad6265SDimitry Andric FilterBitValues[BitIndex] == BIT_FALSE) 1591*81ad6265SDimitry Andric bitAttrs.push_back(ATTR_FILTERED); 1592*81ad6265SDimitry Andric else 1593*81ad6265SDimitry Andric bitAttrs.push_back(ATTR_NONE); 1594*81ad6265SDimitry Andric 1595*81ad6265SDimitry Andric for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) { 1596*81ad6265SDimitry Andric insn_t insn; 1597*81ad6265SDimitry Andric 1598*81ad6265SDimitry Andric insnWithID(insn, Opcodes[InsnIndex].EncodingID); 1599*81ad6265SDimitry Andric 1600*81ad6265SDimitry Andric for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) { 1601*81ad6265SDimitry Andric switch (bitAttrs[BitIndex]) { 1602*81ad6265SDimitry Andric case ATTR_NONE: 1603*81ad6265SDimitry Andric if (insn[BitIndex] == BIT_UNSET) 1604*81ad6265SDimitry Andric bitAttrs[BitIndex] = ATTR_ALL_UNSET; 1605*81ad6265SDimitry Andric else 1606*81ad6265SDimitry Andric bitAttrs[BitIndex] = ATTR_ALL_SET; 1607*81ad6265SDimitry Andric break; 1608*81ad6265SDimitry Andric case ATTR_ALL_SET: 1609*81ad6265SDimitry Andric if (insn[BitIndex] == BIT_UNSET) 1610*81ad6265SDimitry Andric bitAttrs[BitIndex] = ATTR_MIXED; 1611*81ad6265SDimitry Andric break; 1612*81ad6265SDimitry Andric case ATTR_ALL_UNSET: 1613*81ad6265SDimitry Andric if (insn[BitIndex] != BIT_UNSET) 1614*81ad6265SDimitry Andric bitAttrs[BitIndex] = ATTR_MIXED; 1615*81ad6265SDimitry Andric break; 1616*81ad6265SDimitry Andric case ATTR_MIXED: 1617*81ad6265SDimitry Andric case ATTR_FILTERED: 1618*81ad6265SDimitry Andric break; 1619*81ad6265SDimitry Andric } 1620*81ad6265SDimitry Andric } 1621*81ad6265SDimitry Andric } 1622*81ad6265SDimitry Andric 1623*81ad6265SDimitry Andric // The regionAttr automaton consumes the bitAttrs automatons' state, 1624*81ad6265SDimitry Andric // lowest-to-highest. 1625*81ad6265SDimitry Andric // 1626*81ad6265SDimitry Andric // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed) 1627*81ad6265SDimitry Andric // States: NONE, ALL_SET, MIXED 1628*81ad6265SDimitry Andric // Initial state: NONE 1629*81ad6265SDimitry Andric // 1630*81ad6265SDimitry Andric // (NONE) ----- F --> (NONE) 1631*81ad6265SDimitry Andric // (NONE) ----- S --> (ALL_SET) ; and set region start 1632*81ad6265SDimitry Andric // (NONE) ----- U --> (NONE) 1633*81ad6265SDimitry Andric // (NONE) ----- M --> (MIXED) ; and set region start 1634*81ad6265SDimitry Andric // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region 1635*81ad6265SDimitry Andric // (ALL_SET) -- S --> (ALL_SET) 1636*81ad6265SDimitry Andric // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region 1637*81ad6265SDimitry Andric // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region 1638*81ad6265SDimitry Andric // (MIXED) ---- F --> (NONE) ; and report a MIXED region 1639*81ad6265SDimitry Andric // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region 1640*81ad6265SDimitry Andric // (MIXED) ---- U --> (NONE) ; and report a MIXED region 1641*81ad6265SDimitry Andric // (MIXED) ---- M --> (MIXED) 1642*81ad6265SDimitry Andric 1643*81ad6265SDimitry Andric bitAttr_t RA = ATTR_NONE; 1644*81ad6265SDimitry Andric unsigned StartBit = 0; 1645*81ad6265SDimitry Andric 1646*81ad6265SDimitry Andric for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) { 1647*81ad6265SDimitry Andric bitAttr_t bitAttr = bitAttrs[BitIndex]; 1648*81ad6265SDimitry Andric 1649*81ad6265SDimitry Andric assert(bitAttr != ATTR_NONE && "Bit without attributes"); 1650*81ad6265SDimitry Andric 1651*81ad6265SDimitry Andric switch (RA) { 1652*81ad6265SDimitry Andric case ATTR_NONE: 1653*81ad6265SDimitry Andric switch (bitAttr) { 1654*81ad6265SDimitry Andric case ATTR_FILTERED: 1655*81ad6265SDimitry Andric break; 1656*81ad6265SDimitry Andric case ATTR_ALL_SET: 1657*81ad6265SDimitry Andric StartBit = BitIndex; 1658*81ad6265SDimitry Andric RA = ATTR_ALL_SET; 1659*81ad6265SDimitry Andric break; 1660*81ad6265SDimitry Andric case ATTR_ALL_UNSET: 1661*81ad6265SDimitry Andric break; 1662*81ad6265SDimitry Andric case ATTR_MIXED: 1663*81ad6265SDimitry Andric StartBit = BitIndex; 1664*81ad6265SDimitry Andric RA = ATTR_MIXED; 1665*81ad6265SDimitry Andric break; 1666*81ad6265SDimitry Andric default: 1667*81ad6265SDimitry Andric llvm_unreachable("Unexpected bitAttr!"); 1668*81ad6265SDimitry Andric } 1669*81ad6265SDimitry Andric break; 1670*81ad6265SDimitry Andric case ATTR_ALL_SET: 1671*81ad6265SDimitry Andric switch (bitAttr) { 1672*81ad6265SDimitry Andric case ATTR_FILTERED: 1673*81ad6265SDimitry Andric reportRegion(RA, StartBit, BitIndex, AllowMixed); 1674*81ad6265SDimitry Andric RA = ATTR_NONE; 1675*81ad6265SDimitry Andric break; 1676*81ad6265SDimitry Andric case ATTR_ALL_SET: 1677*81ad6265SDimitry Andric break; 1678*81ad6265SDimitry Andric case ATTR_ALL_UNSET: 1679*81ad6265SDimitry Andric reportRegion(RA, StartBit, BitIndex, AllowMixed); 1680*81ad6265SDimitry Andric RA = ATTR_NONE; 1681*81ad6265SDimitry Andric break; 1682*81ad6265SDimitry Andric case ATTR_MIXED: 1683*81ad6265SDimitry Andric reportRegion(RA, StartBit, BitIndex, AllowMixed); 1684*81ad6265SDimitry Andric StartBit = BitIndex; 1685*81ad6265SDimitry Andric RA = ATTR_MIXED; 1686*81ad6265SDimitry Andric break; 1687*81ad6265SDimitry Andric default: 1688*81ad6265SDimitry Andric llvm_unreachable("Unexpected bitAttr!"); 1689*81ad6265SDimitry Andric } 1690*81ad6265SDimitry Andric break; 1691*81ad6265SDimitry Andric case ATTR_MIXED: 1692*81ad6265SDimitry Andric switch (bitAttr) { 1693*81ad6265SDimitry Andric case ATTR_FILTERED: 1694*81ad6265SDimitry Andric reportRegion(RA, StartBit, BitIndex, AllowMixed); 1695*81ad6265SDimitry Andric StartBit = BitIndex; 1696*81ad6265SDimitry Andric RA = ATTR_NONE; 1697*81ad6265SDimitry Andric break; 1698*81ad6265SDimitry Andric case ATTR_ALL_SET: 1699*81ad6265SDimitry Andric reportRegion(RA, StartBit, BitIndex, AllowMixed); 1700*81ad6265SDimitry Andric StartBit = BitIndex; 1701*81ad6265SDimitry Andric RA = ATTR_ALL_SET; 1702*81ad6265SDimitry Andric break; 1703*81ad6265SDimitry Andric case ATTR_ALL_UNSET: 1704*81ad6265SDimitry Andric reportRegion(RA, StartBit, BitIndex, AllowMixed); 1705*81ad6265SDimitry Andric RA = ATTR_NONE; 1706*81ad6265SDimitry Andric break; 1707*81ad6265SDimitry Andric case ATTR_MIXED: 1708*81ad6265SDimitry Andric break; 1709*81ad6265SDimitry Andric default: 1710*81ad6265SDimitry Andric llvm_unreachable("Unexpected bitAttr!"); 1711*81ad6265SDimitry Andric } 1712*81ad6265SDimitry Andric break; 1713*81ad6265SDimitry Andric case ATTR_ALL_UNSET: 1714*81ad6265SDimitry Andric llvm_unreachable("regionAttr state machine has no ATTR_UNSET state"); 1715*81ad6265SDimitry Andric case ATTR_FILTERED: 1716*81ad6265SDimitry Andric llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state"); 1717*81ad6265SDimitry Andric } 1718*81ad6265SDimitry Andric } 1719*81ad6265SDimitry Andric 1720*81ad6265SDimitry Andric // At the end, if we're still in ALL_SET or MIXED states, report a region 1721*81ad6265SDimitry Andric switch (RA) { 1722*81ad6265SDimitry Andric case ATTR_NONE: 1723*81ad6265SDimitry Andric break; 1724*81ad6265SDimitry Andric case ATTR_FILTERED: 1725*81ad6265SDimitry Andric break; 1726*81ad6265SDimitry Andric case ATTR_ALL_SET: 1727*81ad6265SDimitry Andric reportRegion(RA, StartBit, BitIndex, AllowMixed); 1728*81ad6265SDimitry Andric break; 1729*81ad6265SDimitry Andric case ATTR_ALL_UNSET: 1730*81ad6265SDimitry Andric break; 1731*81ad6265SDimitry Andric case ATTR_MIXED: 1732*81ad6265SDimitry Andric reportRegion(RA, StartBit, BitIndex, AllowMixed); 1733*81ad6265SDimitry Andric break; 1734*81ad6265SDimitry Andric } 1735*81ad6265SDimitry Andric 1736*81ad6265SDimitry Andric // We have finished with the filter processings. Now it's time to choose 1737*81ad6265SDimitry Andric // the best performing filter. 1738*81ad6265SDimitry Andric BestIndex = 0; 1739*81ad6265SDimitry Andric bool AllUseless = true; 1740*81ad6265SDimitry Andric unsigned BestScore = 0; 1741*81ad6265SDimitry Andric 1742*81ad6265SDimitry Andric for (unsigned i = 0, e = Filters.size(); i != e; ++i) { 1743*81ad6265SDimitry Andric unsigned Usefulness = Filters[i].usefulness(); 1744*81ad6265SDimitry Andric 1745*81ad6265SDimitry Andric if (Usefulness) 1746*81ad6265SDimitry Andric AllUseless = false; 1747*81ad6265SDimitry Andric 1748*81ad6265SDimitry Andric if (Usefulness > BestScore) { 1749*81ad6265SDimitry Andric BestIndex = i; 1750*81ad6265SDimitry Andric BestScore = Usefulness; 1751*81ad6265SDimitry Andric } 1752*81ad6265SDimitry Andric } 1753*81ad6265SDimitry Andric 1754*81ad6265SDimitry Andric if (!AllUseless) 1755*81ad6265SDimitry Andric bestFilter().recurse(); 1756*81ad6265SDimitry Andric 1757*81ad6265SDimitry Andric return !AllUseless; 1758*81ad6265SDimitry Andric } // end of FilterChooser::filterProcessor(bool) 1759*81ad6265SDimitry Andric 1760*81ad6265SDimitry Andric // Decides on the best configuration of filter(s) to use in order to decode 1761*81ad6265SDimitry Andric // the instructions. A conflict of instructions may occur, in which case we 1762*81ad6265SDimitry Andric // dump the conflict set to the standard error. 1763*81ad6265SDimitry Andric void FilterChooser::doFilter() { 1764*81ad6265SDimitry Andric unsigned Num = Opcodes.size(); 1765*81ad6265SDimitry Andric assert(Num && "FilterChooser created with no instructions"); 1766*81ad6265SDimitry Andric 1767*81ad6265SDimitry Andric // Try regions of consecutive known bit values first. 1768*81ad6265SDimitry Andric if (filterProcessor(false)) 1769*81ad6265SDimitry Andric return; 1770*81ad6265SDimitry Andric 1771*81ad6265SDimitry Andric // Then regions of mixed bits (both known and unitialized bit values allowed). 1772*81ad6265SDimitry Andric if (filterProcessor(true)) 1773*81ad6265SDimitry Andric return; 1774*81ad6265SDimitry Andric 1775*81ad6265SDimitry Andric // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where 1776*81ad6265SDimitry Andric // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a 1777*81ad6265SDimitry Andric // well-known encoding pattern. In such case, we backtrack and scan for the 1778*81ad6265SDimitry Andric // the very first consecutive ATTR_ALL_SET region and assign a filter to it. 1779*81ad6265SDimitry Andric if (Num == 3 && filterProcessor(true, false)) 1780*81ad6265SDimitry Andric return; 1781*81ad6265SDimitry Andric 1782*81ad6265SDimitry Andric // If we come to here, the instruction decoding has failed. 1783*81ad6265SDimitry Andric // Set the BestIndex to -1 to indicate so. 1784*81ad6265SDimitry Andric BestIndex = -1; 1785*81ad6265SDimitry Andric } 1786*81ad6265SDimitry Andric 1787*81ad6265SDimitry Andric // emitTableEntries - Emit state machine entries to decode our share of 1788*81ad6265SDimitry Andric // instructions. 1789*81ad6265SDimitry Andric void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const { 1790*81ad6265SDimitry Andric if (Opcodes.size() == 1) { 1791*81ad6265SDimitry Andric // There is only one instruction in the set, which is great! 1792*81ad6265SDimitry Andric // Call emitSingletonDecoder() to see whether there are any remaining 1793*81ad6265SDimitry Andric // encodings bits. 1794*81ad6265SDimitry Andric emitSingletonTableEntry(TableInfo, Opcodes[0]); 1795*81ad6265SDimitry Andric return; 1796*81ad6265SDimitry Andric } 1797*81ad6265SDimitry Andric 1798*81ad6265SDimitry Andric // Choose the best filter to do the decodings! 1799*81ad6265SDimitry Andric if (BestIndex != -1) { 1800*81ad6265SDimitry Andric const Filter &Best = Filters[BestIndex]; 1801*81ad6265SDimitry Andric if (Best.getNumFiltered() == 1) 1802*81ad6265SDimitry Andric emitSingletonTableEntry(TableInfo, Best); 1803*81ad6265SDimitry Andric else 1804*81ad6265SDimitry Andric Best.emitTableEntry(TableInfo); 1805*81ad6265SDimitry Andric return; 1806*81ad6265SDimitry Andric } 1807*81ad6265SDimitry Andric 1808*81ad6265SDimitry Andric // We don't know how to decode these instructions! Dump the 1809*81ad6265SDimitry Andric // conflict set and bail. 1810*81ad6265SDimitry Andric 1811*81ad6265SDimitry Andric // Print out useful conflict information for postmortem analysis. 1812*81ad6265SDimitry Andric errs() << "Decoding Conflict:\n"; 1813*81ad6265SDimitry Andric 1814*81ad6265SDimitry Andric dumpStack(errs(), "\t\t"); 1815*81ad6265SDimitry Andric 1816*81ad6265SDimitry Andric for (auto Opcode : Opcodes) { 1817*81ad6265SDimitry Andric errs() << '\t'; 1818*81ad6265SDimitry Andric emitNameWithID(errs(), Opcode.EncodingID); 1819*81ad6265SDimitry Andric errs() << " "; 1820*81ad6265SDimitry Andric dumpBits( 1821*81ad6265SDimitry Andric errs(), 1822*81ad6265SDimitry Andric getBitsField(*AllInstructions[Opcode.EncodingID].EncodingDef, "Inst")); 1823*81ad6265SDimitry Andric errs() << '\n'; 1824*81ad6265SDimitry Andric } 1825*81ad6265SDimitry Andric } 1826*81ad6265SDimitry Andric 1827*81ad6265SDimitry Andric static std::string findOperandDecoderMethod(Record *Record) { 1828*81ad6265SDimitry Andric std::string Decoder; 1829*81ad6265SDimitry Andric 1830*81ad6265SDimitry Andric RecordVal *DecoderString = Record->getValue("DecoderMethod"); 1831*81ad6265SDimitry Andric StringInit *String = DecoderString ? 1832*81ad6265SDimitry Andric dyn_cast<StringInit>(DecoderString->getValue()) : nullptr; 1833*81ad6265SDimitry Andric if (String) { 1834*81ad6265SDimitry Andric Decoder = std::string(String->getValue()); 1835*81ad6265SDimitry Andric if (!Decoder.empty()) 1836*81ad6265SDimitry Andric return Decoder; 1837*81ad6265SDimitry Andric } 1838*81ad6265SDimitry Andric 1839*81ad6265SDimitry Andric if (Record->isSubClassOf("RegisterOperand")) 1840*81ad6265SDimitry Andric Record = Record->getValueAsDef("RegClass"); 1841*81ad6265SDimitry Andric 1842*81ad6265SDimitry Andric if (Record->isSubClassOf("RegisterClass")) { 1843*81ad6265SDimitry Andric Decoder = "Decode" + Record->getName().str() + "RegisterClass"; 1844*81ad6265SDimitry Andric } else if (Record->isSubClassOf("PointerLikeRegClass")) { 1845*81ad6265SDimitry Andric Decoder = "DecodePointerLikeRegClass" + 1846*81ad6265SDimitry Andric utostr(Record->getValueAsInt("RegClassKind")); 1847*81ad6265SDimitry Andric } 1848*81ad6265SDimitry Andric 1849*81ad6265SDimitry Andric return Decoder; 1850*81ad6265SDimitry Andric } 1851*81ad6265SDimitry Andric 1852*81ad6265SDimitry Andric OperandInfo getOpInfo(Record *TypeRecord) { 1853*81ad6265SDimitry Andric std::string Decoder = findOperandDecoderMethod(TypeRecord); 1854*81ad6265SDimitry Andric 1855*81ad6265SDimitry Andric RecordVal *HasCompleteDecoderVal = TypeRecord->getValue("hasCompleteDecoder"); 1856*81ad6265SDimitry Andric BitInit *HasCompleteDecoderBit = 1857*81ad6265SDimitry Andric HasCompleteDecoderVal 1858*81ad6265SDimitry Andric ? dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) 1859*81ad6265SDimitry Andric : nullptr; 1860*81ad6265SDimitry Andric bool HasCompleteDecoder = 1861*81ad6265SDimitry Andric HasCompleteDecoderBit ? HasCompleteDecoderBit->getValue() : true; 1862*81ad6265SDimitry Andric 1863*81ad6265SDimitry Andric return OperandInfo(Decoder, HasCompleteDecoder); 1864*81ad6265SDimitry Andric } 1865*81ad6265SDimitry Andric 1866*81ad6265SDimitry Andric void parseVarLenInstOperand(const Record &Def, 1867*81ad6265SDimitry Andric std::vector<OperandInfo> &Operands, 1868*81ad6265SDimitry Andric const CodeGenInstruction &CGI) { 1869*81ad6265SDimitry Andric 1870*81ad6265SDimitry Andric const RecordVal *RV = Def.getValue("Inst"); 1871*81ad6265SDimitry Andric VarLenInst VLI(cast<DagInit>(RV->getValue()), RV); 1872*81ad6265SDimitry Andric SmallVector<int> TiedTo; 1873*81ad6265SDimitry Andric 1874*81ad6265SDimitry Andric for (unsigned Idx = 0; Idx < CGI.Operands.size(); ++Idx) { 1875*81ad6265SDimitry Andric auto &Op = CGI.Operands[Idx]; 1876*81ad6265SDimitry Andric if (Op.MIOperandInfo && Op.MIOperandInfo->getNumArgs() > 0) 1877*81ad6265SDimitry Andric for (auto *Arg : Op.MIOperandInfo->getArgs()) 1878*81ad6265SDimitry Andric Operands.push_back(getOpInfo(cast<DefInit>(Arg)->getDef())); 1879*81ad6265SDimitry Andric else 1880*81ad6265SDimitry Andric Operands.push_back(getOpInfo(Op.Rec)); 1881*81ad6265SDimitry Andric 1882*81ad6265SDimitry Andric int TiedReg = Op.getTiedRegister(); 1883*81ad6265SDimitry Andric TiedTo.push_back(-1); 1884*81ad6265SDimitry Andric if (TiedReg != -1) { 1885*81ad6265SDimitry Andric TiedTo[Idx] = TiedReg; 1886*81ad6265SDimitry Andric TiedTo[TiedReg] = Idx; 1887*81ad6265SDimitry Andric } 1888*81ad6265SDimitry Andric } 1889*81ad6265SDimitry Andric 1890*81ad6265SDimitry Andric unsigned CurrBitPos = 0; 1891*81ad6265SDimitry Andric for (auto &EncodingSegment : VLI) { 1892*81ad6265SDimitry Andric unsigned Offset = 0; 1893*81ad6265SDimitry Andric StringRef OpName; 1894*81ad6265SDimitry Andric 1895*81ad6265SDimitry Andric if (const StringInit *SI = dyn_cast<StringInit>(EncodingSegment.Value)) { 1896*81ad6265SDimitry Andric OpName = SI->getValue(); 1897*81ad6265SDimitry Andric } else if (const DagInit *DI = dyn_cast<DagInit>(EncodingSegment.Value)) { 1898*81ad6265SDimitry Andric OpName = cast<StringInit>(DI->getArg(0))->getValue(); 1899*81ad6265SDimitry Andric Offset = cast<IntInit>(DI->getArg(2))->getValue(); 1900*81ad6265SDimitry Andric } 1901*81ad6265SDimitry Andric 1902*81ad6265SDimitry Andric if (!OpName.empty()) { 1903*81ad6265SDimitry Andric auto OpSubOpPair = 1904*81ad6265SDimitry Andric const_cast<CodeGenInstruction &>(CGI).Operands.ParseOperandName( 1905*81ad6265SDimitry Andric OpName); 1906*81ad6265SDimitry Andric unsigned OpIdx = CGI.Operands.getFlattenedOperandNumber(OpSubOpPair); 1907*81ad6265SDimitry Andric Operands[OpIdx].addField(CurrBitPos, EncodingSegment.BitWidth, Offset); 1908*81ad6265SDimitry Andric 1909*81ad6265SDimitry Andric int TiedReg = TiedTo[OpSubOpPair.first]; 1910*81ad6265SDimitry Andric if (TiedReg != -1) { 1911*81ad6265SDimitry Andric unsigned OpIdx = CGI.Operands.getFlattenedOperandNumber( 1912*81ad6265SDimitry Andric std::make_pair(TiedReg, OpSubOpPair.second)); 1913*81ad6265SDimitry Andric Operands[OpIdx].addField(CurrBitPos, EncodingSegment.BitWidth, Offset); 1914*81ad6265SDimitry Andric } 1915*81ad6265SDimitry Andric } 1916*81ad6265SDimitry Andric 1917*81ad6265SDimitry Andric CurrBitPos += EncodingSegment.BitWidth; 1918*81ad6265SDimitry Andric } 1919*81ad6265SDimitry Andric } 1920*81ad6265SDimitry Andric 1921*81ad6265SDimitry Andric static unsigned 1922*81ad6265SDimitry Andric populateInstruction(CodeGenTarget &Target, const Record &EncodingDef, 1923*81ad6265SDimitry Andric const CodeGenInstruction &CGI, unsigned Opc, 1924*81ad6265SDimitry Andric std::map<unsigned, std::vector<OperandInfo>> &Operands, 1925*81ad6265SDimitry Andric bool IsVarLenInst) { 1926*81ad6265SDimitry Andric const Record &Def = *CGI.TheDef; 1927*81ad6265SDimitry Andric // If all the bit positions are not specified; do not decode this instruction. 1928*81ad6265SDimitry Andric // We are bound to fail! For proper disassembly, the well-known encoding bits 1929*81ad6265SDimitry Andric // of the instruction must be fully specified. 1930*81ad6265SDimitry Andric 1931*81ad6265SDimitry Andric BitsInit &Bits = getBitsField(EncodingDef, "Inst"); 1932*81ad6265SDimitry Andric if (Bits.allInComplete()) 1933*81ad6265SDimitry Andric return 0; 1934*81ad6265SDimitry Andric 1935*81ad6265SDimitry Andric std::vector<OperandInfo> InsnOperands; 1936*81ad6265SDimitry Andric 1937*81ad6265SDimitry Andric // If the instruction has specified a custom decoding hook, use that instead 1938*81ad6265SDimitry Andric // of trying to auto-generate the decoder. 1939*81ad6265SDimitry Andric StringRef InstDecoder = EncodingDef.getValueAsString("DecoderMethod"); 1940*81ad6265SDimitry Andric if (InstDecoder != "") { 1941*81ad6265SDimitry Andric bool HasCompleteInstDecoder = EncodingDef.getValueAsBit("hasCompleteDecoder"); 1942*81ad6265SDimitry Andric InsnOperands.push_back( 1943*81ad6265SDimitry Andric OperandInfo(std::string(InstDecoder), HasCompleteInstDecoder)); 1944*81ad6265SDimitry Andric Operands[Opc] = InsnOperands; 1945*81ad6265SDimitry Andric return Bits.getNumBits(); 1946*81ad6265SDimitry Andric } 1947*81ad6265SDimitry Andric 1948*81ad6265SDimitry Andric // Generate a description of the operand of the instruction that we know 1949*81ad6265SDimitry Andric // how to decode automatically. 1950*81ad6265SDimitry Andric // FIXME: We'll need to have a way to manually override this as needed. 1951*81ad6265SDimitry Andric 1952*81ad6265SDimitry Andric // Gather the outputs/inputs of the instruction, so we can find their 1953*81ad6265SDimitry Andric // positions in the encoding. This assumes for now that they appear in the 1954*81ad6265SDimitry Andric // MCInst in the order that they're listed. 1955*81ad6265SDimitry Andric std::vector<std::pair<Init*, StringRef>> InOutOperands; 1956*81ad6265SDimitry Andric DagInit *Out = Def.getValueAsDag("OutOperandList"); 1957*81ad6265SDimitry Andric DagInit *In = Def.getValueAsDag("InOperandList"); 1958*81ad6265SDimitry Andric for (unsigned i = 0; i < Out->getNumArgs(); ++i) 1959*81ad6265SDimitry Andric InOutOperands.push_back( 1960*81ad6265SDimitry Andric std::make_pair(Out->getArg(i), Out->getArgNameStr(i))); 1961*81ad6265SDimitry Andric for (unsigned i = 0; i < In->getNumArgs(); ++i) 1962*81ad6265SDimitry Andric InOutOperands.push_back( 1963*81ad6265SDimitry Andric std::make_pair(In->getArg(i), In->getArgNameStr(i))); 1964*81ad6265SDimitry Andric 1965*81ad6265SDimitry Andric // Search for tied operands, so that we can correctly instantiate 1966*81ad6265SDimitry Andric // operands that are not explicitly represented in the encoding. 1967*81ad6265SDimitry Andric std::map<std::string, std::string> TiedNames; 1968*81ad6265SDimitry Andric for (unsigned i = 0; i < CGI.Operands.size(); ++i) { 1969*81ad6265SDimitry Andric int tiedTo = CGI.Operands[i].getTiedRegister(); 1970*81ad6265SDimitry Andric if (tiedTo != -1) { 1971*81ad6265SDimitry Andric std::pair<unsigned, unsigned> SO = 1972*81ad6265SDimitry Andric CGI.Operands.getSubOperandNumber(tiedTo); 1973*81ad6265SDimitry Andric TiedNames[std::string(InOutOperands[i].second)] = 1974*81ad6265SDimitry Andric std::string(InOutOperands[SO.first].second); 1975*81ad6265SDimitry Andric TiedNames[std::string(InOutOperands[SO.first].second)] = 1976*81ad6265SDimitry Andric std::string(InOutOperands[i].second); 1977*81ad6265SDimitry Andric } 1978*81ad6265SDimitry Andric } 1979*81ad6265SDimitry Andric 1980*81ad6265SDimitry Andric if (IsVarLenInst) { 1981*81ad6265SDimitry Andric parseVarLenInstOperand(EncodingDef, InsnOperands, CGI); 1982*81ad6265SDimitry Andric } else { 1983*81ad6265SDimitry Andric std::map<std::string, std::vector<OperandInfo>> NumberedInsnOperands; 1984*81ad6265SDimitry Andric std::set<std::string> NumberedInsnOperandsNoTie; 1985*81ad6265SDimitry Andric if (Target.getInstructionSet()->getValueAsBit( 1986*81ad6265SDimitry Andric "decodePositionallyEncodedOperands")) { 1987*81ad6265SDimitry Andric const std::vector<RecordVal> &Vals = Def.getValues(); 1988*81ad6265SDimitry Andric unsigned NumberedOp = 0; 1989*81ad6265SDimitry Andric 1990*81ad6265SDimitry Andric std::set<unsigned> NamedOpIndices; 1991*81ad6265SDimitry Andric if (Target.getInstructionSet()->getValueAsBit( 1992*81ad6265SDimitry Andric "noNamedPositionallyEncodedOperands")) 1993*81ad6265SDimitry Andric // Collect the set of operand indices that might correspond to named 1994*81ad6265SDimitry Andric // operand, and skip these when assigning operands based on position. 1995*81ad6265SDimitry Andric for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 1996*81ad6265SDimitry Andric unsigned OpIdx; 1997*81ad6265SDimitry Andric if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx)) 1998*81ad6265SDimitry Andric continue; 1999*81ad6265SDimitry Andric 2000*81ad6265SDimitry Andric NamedOpIndices.insert(OpIdx); 2001*81ad6265SDimitry Andric } 2002*81ad6265SDimitry Andric 2003*81ad6265SDimitry Andric for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 2004*81ad6265SDimitry Andric // Ignore fixed fields in the record, we're looking for values like: 2005*81ad6265SDimitry Andric // bits<5> RST = { ?, ?, ?, ?, ? }; 2006*81ad6265SDimitry Andric if (Vals[i].isNonconcreteOK() || Vals[i].getValue()->isComplete()) 2007*81ad6265SDimitry Andric continue; 2008*81ad6265SDimitry Andric 2009*81ad6265SDimitry Andric // Determine if Vals[i] actually contributes to the Inst encoding. 2010*81ad6265SDimitry Andric unsigned bi = 0; 2011*81ad6265SDimitry Andric for (; bi < Bits.getNumBits(); ++bi) { 2012*81ad6265SDimitry Andric VarInit *Var = nullptr; 2013*81ad6265SDimitry Andric VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi)); 2014*81ad6265SDimitry Andric if (BI) 2015*81ad6265SDimitry Andric Var = dyn_cast<VarInit>(BI->getBitVar()); 2016*81ad6265SDimitry Andric else 2017*81ad6265SDimitry Andric Var = dyn_cast<VarInit>(Bits.getBit(bi)); 2018*81ad6265SDimitry Andric 2019*81ad6265SDimitry Andric if (Var && Var->getName() == Vals[i].getName()) 2020*81ad6265SDimitry Andric break; 2021*81ad6265SDimitry Andric } 2022*81ad6265SDimitry Andric 2023*81ad6265SDimitry Andric if (bi == Bits.getNumBits()) 2024*81ad6265SDimitry Andric continue; 2025*81ad6265SDimitry Andric 2026*81ad6265SDimitry Andric // Skip variables that correspond to explicitly-named operands. 2027*81ad6265SDimitry Andric unsigned OpIdx; 2028*81ad6265SDimitry Andric if (CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx)) 2029*81ad6265SDimitry Andric continue; 2030*81ad6265SDimitry Andric 2031*81ad6265SDimitry Andric // Get the bit range for this operand: 2032*81ad6265SDimitry Andric unsigned bitStart = bi++, bitWidth = 1; 2033*81ad6265SDimitry Andric for (; bi < Bits.getNumBits(); ++bi) { 2034*81ad6265SDimitry Andric VarInit *Var = nullptr; 2035*81ad6265SDimitry Andric VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi)); 2036*81ad6265SDimitry Andric if (BI) 2037*81ad6265SDimitry Andric Var = dyn_cast<VarInit>(BI->getBitVar()); 2038*81ad6265SDimitry Andric else 2039*81ad6265SDimitry Andric Var = dyn_cast<VarInit>(Bits.getBit(bi)); 2040*81ad6265SDimitry Andric 2041*81ad6265SDimitry Andric if (!Var) 2042*81ad6265SDimitry Andric break; 2043*81ad6265SDimitry Andric 2044*81ad6265SDimitry Andric if (Var->getName() != Vals[i].getName()) 2045*81ad6265SDimitry Andric break; 2046*81ad6265SDimitry Andric 2047*81ad6265SDimitry Andric ++bitWidth; 2048*81ad6265SDimitry Andric } 2049*81ad6265SDimitry Andric 2050*81ad6265SDimitry Andric unsigned NumberOps = CGI.Operands.size(); 2051*81ad6265SDimitry Andric while (NumberedOp < NumberOps && 2052*81ad6265SDimitry Andric (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) || 2053*81ad6265SDimitry Andric (!NamedOpIndices.empty() && 2054*81ad6265SDimitry Andric NamedOpIndices.count( 2055*81ad6265SDimitry Andric CGI.Operands.getSubOperandNumber(NumberedOp).first)))) 2056*81ad6265SDimitry Andric ++NumberedOp; 2057*81ad6265SDimitry Andric 2058*81ad6265SDimitry Andric OpIdx = NumberedOp++; 2059*81ad6265SDimitry Andric 2060*81ad6265SDimitry Andric // OpIdx now holds the ordered operand number of Vals[i]. 2061*81ad6265SDimitry Andric std::pair<unsigned, unsigned> SO = 2062*81ad6265SDimitry Andric CGI.Operands.getSubOperandNumber(OpIdx); 2063*81ad6265SDimitry Andric const std::string &Name = CGI.Operands[SO.first].Name; 2064*81ad6265SDimitry Andric 2065*81ad6265SDimitry Andric LLVM_DEBUG(dbgs() << "Numbered operand mapping for " << Def.getName() 2066*81ad6265SDimitry Andric << ": " << Name << "(" << SO.first << ", " 2067*81ad6265SDimitry Andric << SO.second << ") => " << Vals[i].getName() << "\n"); 2068*81ad6265SDimitry Andric 2069*81ad6265SDimitry Andric std::string Decoder; 2070*81ad6265SDimitry Andric Record *TypeRecord = CGI.Operands[SO.first].Rec; 2071*81ad6265SDimitry Andric 2072*81ad6265SDimitry Andric RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod"); 2073*81ad6265SDimitry Andric StringInit *String = 2074*81ad6265SDimitry Andric DecoderString ? dyn_cast<StringInit>(DecoderString->getValue()) 2075*81ad6265SDimitry Andric : nullptr; 2076*81ad6265SDimitry Andric if (String && String->getValue() != "") 2077*81ad6265SDimitry Andric Decoder = std::string(String->getValue()); 2078*81ad6265SDimitry Andric 2079*81ad6265SDimitry Andric if (Decoder == "" && CGI.Operands[SO.first].MIOperandInfo && 2080*81ad6265SDimitry Andric CGI.Operands[SO.first].MIOperandInfo->getNumArgs()) { 2081*81ad6265SDimitry Andric Init *Arg = CGI.Operands[SO.first].MIOperandInfo->getArg(SO.second); 2082*81ad6265SDimitry Andric if (DefInit *DI = cast<DefInit>(Arg)) 2083*81ad6265SDimitry Andric TypeRecord = DI->getDef(); 2084*81ad6265SDimitry Andric } 2085*81ad6265SDimitry Andric 2086*81ad6265SDimitry Andric bool isReg = false; 2087*81ad6265SDimitry Andric if (TypeRecord->isSubClassOf("RegisterOperand")) 2088*81ad6265SDimitry Andric TypeRecord = TypeRecord->getValueAsDef("RegClass"); 2089*81ad6265SDimitry Andric if (TypeRecord->isSubClassOf("RegisterClass")) { 2090*81ad6265SDimitry Andric Decoder = "Decode" + TypeRecord->getName().str() + "RegisterClass"; 2091*81ad6265SDimitry Andric isReg = true; 2092*81ad6265SDimitry Andric } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) { 2093*81ad6265SDimitry Andric Decoder = "DecodePointerLikeRegClass" + 2094*81ad6265SDimitry Andric utostr(TypeRecord->getValueAsInt("RegClassKind")); 2095*81ad6265SDimitry Andric isReg = true; 2096*81ad6265SDimitry Andric } 2097*81ad6265SDimitry Andric 2098*81ad6265SDimitry Andric DecoderString = TypeRecord->getValue("DecoderMethod"); 2099*81ad6265SDimitry Andric String = DecoderString ? dyn_cast<StringInit>(DecoderString->getValue()) 2100*81ad6265SDimitry Andric : nullptr; 2101*81ad6265SDimitry Andric if (!isReg && String && String->getValue() != "") 2102*81ad6265SDimitry Andric Decoder = std::string(String->getValue()); 2103*81ad6265SDimitry Andric 2104*81ad6265SDimitry Andric RecordVal *HasCompleteDecoderVal = 2105*81ad6265SDimitry Andric TypeRecord->getValue("hasCompleteDecoder"); 2106*81ad6265SDimitry Andric BitInit *HasCompleteDecoderBit = 2107*81ad6265SDimitry Andric HasCompleteDecoderVal 2108*81ad6265SDimitry Andric ? dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) 2109*81ad6265SDimitry Andric : nullptr; 2110*81ad6265SDimitry Andric bool HasCompleteDecoder = 2111*81ad6265SDimitry Andric HasCompleteDecoderBit ? HasCompleteDecoderBit->getValue() : true; 2112*81ad6265SDimitry Andric 2113*81ad6265SDimitry Andric OperandInfo OpInfo(Decoder, HasCompleteDecoder); 2114*81ad6265SDimitry Andric OpInfo.addField(bitStart, bitWidth, 0); 2115*81ad6265SDimitry Andric 2116*81ad6265SDimitry Andric NumberedInsnOperands[Name].push_back(OpInfo); 2117*81ad6265SDimitry Andric 2118*81ad6265SDimitry Andric // FIXME: For complex operands with custom decoders we can't handle tied 2119*81ad6265SDimitry Andric // sub-operands automatically. Skip those here and assume that this is 2120*81ad6265SDimitry Andric // fixed up elsewhere. 2121*81ad6265SDimitry Andric if (CGI.Operands[SO.first].MIOperandInfo && 2122*81ad6265SDimitry Andric CGI.Operands[SO.first].MIOperandInfo->getNumArgs() > 1 && String && 2123*81ad6265SDimitry Andric String->getValue() != "") 2124*81ad6265SDimitry Andric NumberedInsnOperandsNoTie.insert(Name); 2125*81ad6265SDimitry Andric } 2126*81ad6265SDimitry Andric } 2127*81ad6265SDimitry Andric 2128*81ad6265SDimitry Andric // For each operand, see if we can figure out where it is encoded. 2129*81ad6265SDimitry Andric for (const auto &Op : InOutOperands) { 2130*81ad6265SDimitry Andric if (!NumberedInsnOperands[std::string(Op.second)].empty()) { 2131*81ad6265SDimitry Andric llvm::append_range(InsnOperands, 2132*81ad6265SDimitry Andric NumberedInsnOperands[std::string(Op.second)]); 2133*81ad6265SDimitry Andric continue; 2134*81ad6265SDimitry Andric } 2135*81ad6265SDimitry Andric if (!NumberedInsnOperands[TiedNames[std::string(Op.second)]].empty()) { 2136*81ad6265SDimitry Andric if (!NumberedInsnOperandsNoTie.count( 2137*81ad6265SDimitry Andric TiedNames[std::string(Op.second)])) { 2138*81ad6265SDimitry Andric // Figure out to which (sub)operand we're tied. 2139*81ad6265SDimitry Andric unsigned i = 2140*81ad6265SDimitry Andric CGI.Operands.getOperandNamed(TiedNames[std::string(Op.second)]); 2141*81ad6265SDimitry Andric int tiedTo = CGI.Operands[i].getTiedRegister(); 2142*81ad6265SDimitry Andric if (tiedTo == -1) { 2143*81ad6265SDimitry Andric i = CGI.Operands.getOperandNamed(Op.second); 2144*81ad6265SDimitry Andric tiedTo = CGI.Operands[i].getTiedRegister(); 2145*81ad6265SDimitry Andric } 2146*81ad6265SDimitry Andric 2147*81ad6265SDimitry Andric if (tiedTo != -1) { 2148*81ad6265SDimitry Andric std::pair<unsigned, unsigned> SO = 2149*81ad6265SDimitry Andric CGI.Operands.getSubOperandNumber(tiedTo); 2150*81ad6265SDimitry Andric 2151*81ad6265SDimitry Andric InsnOperands.push_back( 2152*81ad6265SDimitry Andric NumberedInsnOperands[TiedNames[std::string(Op.second)]] 2153*81ad6265SDimitry Andric [SO.second]); 2154*81ad6265SDimitry Andric } 2155*81ad6265SDimitry Andric } 2156*81ad6265SDimitry Andric continue; 2157*81ad6265SDimitry Andric } 2158*81ad6265SDimitry Andric 2159*81ad6265SDimitry Andric // At this point, we can locate the decoder field, but we need to know how 2160*81ad6265SDimitry Andric // to interpret it. As a first step, require the target to provide 2161*81ad6265SDimitry Andric // callbacks for decoding register classes. 2162*81ad6265SDimitry Andric 2163*81ad6265SDimitry Andric OperandInfo OpInfo = getOpInfo(cast<DefInit>(Op.first)->getDef()); 2164*81ad6265SDimitry Andric 2165*81ad6265SDimitry Andric // Some bits of the operand may be required to be 1 depending on the 2166*81ad6265SDimitry Andric // instruction's encoding. Collect those bits. 2167*81ad6265SDimitry Andric if (const RecordVal *EncodedValue = EncodingDef.getValue(Op.second)) 2168*81ad6265SDimitry Andric if (const BitsInit *OpBits = 2169*81ad6265SDimitry Andric dyn_cast<BitsInit>(EncodedValue->getValue())) 2170*81ad6265SDimitry Andric for (unsigned I = 0; I < OpBits->getNumBits(); ++I) 2171*81ad6265SDimitry Andric if (const BitInit *OpBit = dyn_cast<BitInit>(OpBits->getBit(I))) 2172*81ad6265SDimitry Andric if (OpBit->getValue()) 2173*81ad6265SDimitry Andric OpInfo.InitValue |= 1ULL << I; 2174*81ad6265SDimitry Andric 2175*81ad6265SDimitry Andric unsigned Base = ~0U; 2176*81ad6265SDimitry Andric unsigned Width = 0; 2177*81ad6265SDimitry Andric unsigned Offset = 0; 2178*81ad6265SDimitry Andric 2179*81ad6265SDimitry Andric for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) { 2180*81ad6265SDimitry Andric VarInit *Var = nullptr; 2181*81ad6265SDimitry Andric VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi)); 2182*81ad6265SDimitry Andric if (BI) 2183*81ad6265SDimitry Andric Var = dyn_cast<VarInit>(BI->getBitVar()); 2184*81ad6265SDimitry Andric else 2185*81ad6265SDimitry Andric Var = dyn_cast<VarInit>(Bits.getBit(bi)); 2186*81ad6265SDimitry Andric 2187*81ad6265SDimitry Andric if (!Var) { 2188*81ad6265SDimitry Andric if (Base != ~0U) { 2189*81ad6265SDimitry Andric OpInfo.addField(Base, Width, Offset); 2190*81ad6265SDimitry Andric Base = ~0U; 2191*81ad6265SDimitry Andric Width = 0; 2192*81ad6265SDimitry Andric Offset = 0; 2193*81ad6265SDimitry Andric } 2194*81ad6265SDimitry Andric continue; 2195*81ad6265SDimitry Andric } 2196*81ad6265SDimitry Andric 2197*81ad6265SDimitry Andric if ((Var->getName() != Op.second && 2198*81ad6265SDimitry Andric Var->getName() != TiedNames[std::string(Op.second)])) { 2199*81ad6265SDimitry Andric if (Base != ~0U) { 2200*81ad6265SDimitry Andric OpInfo.addField(Base, Width, Offset); 2201*81ad6265SDimitry Andric Base = ~0U; 2202*81ad6265SDimitry Andric Width = 0; 2203*81ad6265SDimitry Andric Offset = 0; 2204*81ad6265SDimitry Andric } 2205*81ad6265SDimitry Andric continue; 2206*81ad6265SDimitry Andric } 2207*81ad6265SDimitry Andric 2208*81ad6265SDimitry Andric if (Base == ~0U) { 2209*81ad6265SDimitry Andric Base = bi; 2210*81ad6265SDimitry Andric Width = 1; 2211*81ad6265SDimitry Andric Offset = BI ? BI->getBitNum() : 0; 2212*81ad6265SDimitry Andric } else if (BI && BI->getBitNum() != Offset + Width) { 2213*81ad6265SDimitry Andric OpInfo.addField(Base, Width, Offset); 2214*81ad6265SDimitry Andric Base = bi; 2215*81ad6265SDimitry Andric Width = 1; 2216*81ad6265SDimitry Andric Offset = BI->getBitNum(); 2217*81ad6265SDimitry Andric } else { 2218*81ad6265SDimitry Andric ++Width; 2219*81ad6265SDimitry Andric } 2220*81ad6265SDimitry Andric } 2221*81ad6265SDimitry Andric 2222*81ad6265SDimitry Andric if (Base != ~0U) 2223*81ad6265SDimitry Andric OpInfo.addField(Base, Width, Offset); 2224*81ad6265SDimitry Andric 2225*81ad6265SDimitry Andric if (OpInfo.numFields() > 0) 2226*81ad6265SDimitry Andric InsnOperands.push_back(OpInfo); 2227*81ad6265SDimitry Andric } 2228*81ad6265SDimitry Andric } 2229*81ad6265SDimitry Andric 2230*81ad6265SDimitry Andric Operands[Opc] = InsnOperands; 2231*81ad6265SDimitry Andric 2232*81ad6265SDimitry Andric #if 0 2233*81ad6265SDimitry Andric LLVM_DEBUG({ 2234*81ad6265SDimitry Andric // Dumps the instruction encoding bits. 2235*81ad6265SDimitry Andric dumpBits(errs(), Bits); 2236*81ad6265SDimitry Andric 2237*81ad6265SDimitry Andric errs() << '\n'; 2238*81ad6265SDimitry Andric 2239*81ad6265SDimitry Andric // Dumps the list of operand info. 2240*81ad6265SDimitry Andric for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) { 2241*81ad6265SDimitry Andric const CGIOperandList::OperandInfo &Info = CGI.Operands[i]; 2242*81ad6265SDimitry Andric const std::string &OperandName = Info.Name; 2243*81ad6265SDimitry Andric const Record &OperandDef = *Info.Rec; 2244*81ad6265SDimitry Andric 2245*81ad6265SDimitry Andric errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n"; 2246*81ad6265SDimitry Andric } 2247*81ad6265SDimitry Andric }); 2248*81ad6265SDimitry Andric #endif 2249*81ad6265SDimitry Andric 2250*81ad6265SDimitry Andric return Bits.getNumBits(); 2251*81ad6265SDimitry Andric } 2252*81ad6265SDimitry Andric 2253*81ad6265SDimitry Andric // emitFieldFromInstruction - Emit the templated helper function 2254*81ad6265SDimitry Andric // fieldFromInstruction(). 2255*81ad6265SDimitry Andric // On Windows we make sure that this function is not inlined when 2256*81ad6265SDimitry Andric // using the VS compiler. It has a bug which causes the function 2257*81ad6265SDimitry Andric // to be optimized out in some circustances. See llvm.org/pr38292 2258*81ad6265SDimitry Andric static void emitFieldFromInstruction(formatted_raw_ostream &OS) { 2259*81ad6265SDimitry Andric OS << "// Helper functions for extracting fields from encoded instructions.\n" 2260*81ad6265SDimitry Andric << "// InsnType must either be integral or an APInt-like object that " 2261*81ad6265SDimitry Andric "must:\n" 2262*81ad6265SDimitry Andric << "// * be default-constructible and copy-constructible\n" 2263*81ad6265SDimitry Andric << "// * be constructible from an APInt (this can be private)\n" 2264*81ad6265SDimitry Andric << "// * Support insertBits(bits, startBit, numBits)\n" 2265*81ad6265SDimitry Andric << "// * Support extractBitsAsZExtValue(numBits, startBit)\n" 2266*81ad6265SDimitry Andric << "// * Support the ~, &, ==, and != operators with other objects of " 2267*81ad6265SDimitry Andric "the same type\n" 2268*81ad6265SDimitry Andric << "// * Support the != and bitwise & with uint64_t\n" 2269*81ad6265SDimitry Andric << "// * Support put (<<) to raw_ostream&\n" 2270*81ad6265SDimitry Andric << "template <typename InsnType>\n" 2271*81ad6265SDimitry Andric << "#if defined(_MSC_VER) && !defined(__clang__)\n" 2272*81ad6265SDimitry Andric << "__declspec(noinline)\n" 2273*81ad6265SDimitry Andric << "#endif\n" 2274*81ad6265SDimitry Andric << "static std::enable_if_t<std::is_integral<InsnType>::value, InsnType>\n" 2275*81ad6265SDimitry Andric << "fieldFromInstruction(const InsnType &insn, unsigned startBit,\n" 2276*81ad6265SDimitry Andric << " unsigned numBits) {\n" 2277*81ad6265SDimitry Andric << " assert(startBit + numBits <= 64 && \"Cannot support >64-bit " 2278*81ad6265SDimitry Andric "extractions!\");\n" 2279*81ad6265SDimitry Andric << " assert(startBit + numBits <= (sizeof(InsnType) * 8) &&\n" 2280*81ad6265SDimitry Andric << " \"Instruction field out of bounds!\");\n" 2281*81ad6265SDimitry Andric << " InsnType fieldMask;\n" 2282*81ad6265SDimitry Andric << " if (numBits == sizeof(InsnType) * 8)\n" 2283*81ad6265SDimitry Andric << " fieldMask = (InsnType)(-1LL);\n" 2284*81ad6265SDimitry Andric << " else\n" 2285*81ad6265SDimitry Andric << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n" 2286*81ad6265SDimitry Andric << " return (insn & fieldMask) >> startBit;\n" 2287*81ad6265SDimitry Andric << "}\n" 2288*81ad6265SDimitry Andric << "\n" 2289*81ad6265SDimitry Andric << "template <typename InsnType>\n" 2290*81ad6265SDimitry Andric << "static std::enable_if_t<!std::is_integral<InsnType>::value, " 2291*81ad6265SDimitry Andric "uint64_t>\n" 2292*81ad6265SDimitry Andric << "fieldFromInstruction(const InsnType &insn, unsigned startBit,\n" 2293*81ad6265SDimitry Andric << " unsigned numBits) {\n" 2294*81ad6265SDimitry Andric << " return insn.extractBitsAsZExtValue(numBits, startBit);\n" 2295*81ad6265SDimitry Andric << "}\n\n"; 2296*81ad6265SDimitry Andric } 2297*81ad6265SDimitry Andric 2298*81ad6265SDimitry Andric // emitInsertBits - Emit the templated helper function insertBits(). 2299*81ad6265SDimitry Andric static void emitInsertBits(formatted_raw_ostream &OS) { 2300*81ad6265SDimitry Andric OS << "// Helper function for inserting bits extracted from an encoded " 2301*81ad6265SDimitry Andric "instruction into\n" 2302*81ad6265SDimitry Andric << "// a field.\n" 2303*81ad6265SDimitry Andric << "template <typename InsnType>\n" 2304*81ad6265SDimitry Andric << "static std::enable_if_t<std::is_integral<InsnType>::value>\n" 2305*81ad6265SDimitry Andric << "insertBits(InsnType &field, InsnType bits, unsigned startBit, " 2306*81ad6265SDimitry Andric "unsigned numBits) {\n" 2307*81ad6265SDimitry Andric << " assert(startBit + numBits <= sizeof field * 8);\n" 2308*81ad6265SDimitry Andric << " field |= (InsnType)bits << startBit;\n" 2309*81ad6265SDimitry Andric << "}\n" 2310*81ad6265SDimitry Andric << "\n" 2311*81ad6265SDimitry Andric << "template <typename InsnType>\n" 2312*81ad6265SDimitry Andric << "static std::enable_if_t<!std::is_integral<InsnType>::value>\n" 2313*81ad6265SDimitry Andric << "insertBits(InsnType &field, uint64_t bits, unsigned startBit, " 2314*81ad6265SDimitry Andric "unsigned numBits) {\n" 2315*81ad6265SDimitry Andric << " field.insertBits(bits, startBit, numBits);\n" 2316*81ad6265SDimitry Andric << "}\n\n"; 2317*81ad6265SDimitry Andric } 2318*81ad6265SDimitry Andric 2319*81ad6265SDimitry Andric // emitDecodeInstruction - Emit the templated helper function 2320*81ad6265SDimitry Andric // decodeInstruction(). 2321*81ad6265SDimitry Andric static void emitDecodeInstruction(formatted_raw_ostream &OS, 2322*81ad6265SDimitry Andric bool IsVarLenInst) { 2323*81ad6265SDimitry Andric OS << "template <typename InsnType>\n" 2324*81ad6265SDimitry Andric << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], " 2325*81ad6265SDimitry Andric "MCInst &MI,\n" 2326*81ad6265SDimitry Andric << " InsnType insn, uint64_t " 2327*81ad6265SDimitry Andric "Address,\n" 2328*81ad6265SDimitry Andric << " const MCDisassembler *DisAsm,\n" 2329*81ad6265SDimitry Andric << " const MCSubtargetInfo &STI"; 2330*81ad6265SDimitry Andric if (IsVarLenInst) { 2331*81ad6265SDimitry Andric OS << ",\n" 2332*81ad6265SDimitry Andric << " llvm::function_ref<void(APInt " 2333*81ad6265SDimitry Andric "&," 2334*81ad6265SDimitry Andric << " uint64_t)> makeUp"; 2335*81ad6265SDimitry Andric } 2336*81ad6265SDimitry Andric OS << ") {\n" 2337*81ad6265SDimitry Andric << " const FeatureBitset &Bits = STI.getFeatureBits();\n" 2338*81ad6265SDimitry Andric << "\n" 2339*81ad6265SDimitry Andric << " const uint8_t *Ptr = DecodeTable;\n" 2340*81ad6265SDimitry Andric << " uint64_t CurFieldValue = 0;\n" 2341*81ad6265SDimitry Andric << " DecodeStatus S = MCDisassembler::Success;\n" 2342*81ad6265SDimitry Andric << " while (true) {\n" 2343*81ad6265SDimitry Andric << " ptrdiff_t Loc = Ptr - DecodeTable;\n" 2344*81ad6265SDimitry Andric << " switch (*Ptr) {\n" 2345*81ad6265SDimitry Andric << " default:\n" 2346*81ad6265SDimitry Andric << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n" 2347*81ad6265SDimitry Andric << " return MCDisassembler::Fail;\n" 2348*81ad6265SDimitry Andric << " case MCD::OPC_ExtractField: {\n" 2349*81ad6265SDimitry Andric << " unsigned Start = *++Ptr;\n" 2350*81ad6265SDimitry Andric << " unsigned Len = *++Ptr;\n" 2351*81ad6265SDimitry Andric << " ++Ptr;\n"; 2352*81ad6265SDimitry Andric if (IsVarLenInst) 2353*81ad6265SDimitry Andric OS << " makeUp(insn, Start + Len);\n"; 2354*81ad6265SDimitry Andric OS << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n" 2355*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << " 2356*81ad6265SDimitry Andric "\", \"\n" 2357*81ad6265SDimitry Andric << " << Len << \"): \" << CurFieldValue << \"\\n\");\n" 2358*81ad6265SDimitry Andric << " break;\n" 2359*81ad6265SDimitry Andric << " }\n" 2360*81ad6265SDimitry Andric << " case MCD::OPC_FilterValue: {\n" 2361*81ad6265SDimitry Andric << " // Decode the field value.\n" 2362*81ad6265SDimitry Andric << " unsigned Len;\n" 2363*81ad6265SDimitry Andric << " uint64_t Val = decodeULEB128(++Ptr, &Len);\n" 2364*81ad6265SDimitry Andric << " Ptr += Len;\n" 2365*81ad6265SDimitry Andric << " // NumToSkip is a plain 24-bit integer.\n" 2366*81ad6265SDimitry Andric << " unsigned NumToSkip = *Ptr++;\n" 2367*81ad6265SDimitry Andric << " NumToSkip |= (*Ptr++) << 8;\n" 2368*81ad6265SDimitry Andric << " NumToSkip |= (*Ptr++) << 16;\n" 2369*81ad6265SDimitry Andric << "\n" 2370*81ad6265SDimitry Andric << " // Perform the filter operation.\n" 2371*81ad6265SDimitry Andric << " if (Val != CurFieldValue)\n" 2372*81ad6265SDimitry Andric << " Ptr += NumToSkip;\n" 2373*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << " 2374*81ad6265SDimitry Andric "\", \" << NumToSkip\n" 2375*81ad6265SDimitry Andric << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" " 2376*81ad6265SDimitry Andric ": \"PASS:\")\n" 2377*81ad6265SDimitry Andric << " << \" continuing at \" << (Ptr - DecodeTable) << " 2378*81ad6265SDimitry Andric "\"\\n\");\n" 2379*81ad6265SDimitry Andric << "\n" 2380*81ad6265SDimitry Andric << " break;\n" 2381*81ad6265SDimitry Andric << " }\n" 2382*81ad6265SDimitry Andric << " case MCD::OPC_CheckField: {\n" 2383*81ad6265SDimitry Andric << " unsigned Start = *++Ptr;\n" 2384*81ad6265SDimitry Andric << " unsigned Len = *++Ptr;\n"; 2385*81ad6265SDimitry Andric if (IsVarLenInst) 2386*81ad6265SDimitry Andric OS << " makeUp(insn, Start + Len);\n"; 2387*81ad6265SDimitry Andric OS << " uint64_t FieldValue = fieldFromInstruction(insn, Start, Len);\n" 2388*81ad6265SDimitry Andric << " // Decode the field value.\n" 2389*81ad6265SDimitry Andric << " unsigned PtrLen = 0;\n" 2390*81ad6265SDimitry Andric << " uint64_t ExpectedValue = decodeULEB128(++Ptr, &PtrLen);\n" 2391*81ad6265SDimitry Andric << " Ptr += PtrLen;\n" 2392*81ad6265SDimitry Andric << " // NumToSkip is a plain 24-bit integer.\n" 2393*81ad6265SDimitry Andric << " unsigned NumToSkip = *Ptr++;\n" 2394*81ad6265SDimitry Andric << " NumToSkip |= (*Ptr++) << 8;\n" 2395*81ad6265SDimitry Andric << " NumToSkip |= (*Ptr++) << 16;\n" 2396*81ad6265SDimitry Andric << "\n" 2397*81ad6265SDimitry Andric << " // If the actual and expected values don't match, skip.\n" 2398*81ad6265SDimitry Andric << " if (ExpectedValue != FieldValue)\n" 2399*81ad6265SDimitry Andric << " Ptr += NumToSkip;\n" 2400*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << " 2401*81ad6265SDimitry Andric "\", \"\n" 2402*81ad6265SDimitry Andric << " << Len << \", \" << ExpectedValue << \", \" << " 2403*81ad6265SDimitry Andric "NumToSkip\n" 2404*81ad6265SDimitry Andric << " << \"): FieldValue = \" << FieldValue << \", " 2405*81ad6265SDimitry Andric "ExpectedValue = \"\n" 2406*81ad6265SDimitry Andric << " << ExpectedValue << \": \"\n" 2407*81ad6265SDimitry Andric << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : " 2408*81ad6265SDimitry Andric "\"FAIL\\n\"));\n" 2409*81ad6265SDimitry Andric << " break;\n" 2410*81ad6265SDimitry Andric << " }\n" 2411*81ad6265SDimitry Andric << " case MCD::OPC_CheckPredicate: {\n" 2412*81ad6265SDimitry Andric << " unsigned Len;\n" 2413*81ad6265SDimitry Andric << " // Decode the Predicate Index value.\n" 2414*81ad6265SDimitry Andric << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n" 2415*81ad6265SDimitry Andric << " Ptr += Len;\n" 2416*81ad6265SDimitry Andric << " // NumToSkip is a plain 24-bit integer.\n" 2417*81ad6265SDimitry Andric << " unsigned NumToSkip = *Ptr++;\n" 2418*81ad6265SDimitry Andric << " NumToSkip |= (*Ptr++) << 8;\n" 2419*81ad6265SDimitry Andric << " NumToSkip |= (*Ptr++) << 16;\n" 2420*81ad6265SDimitry Andric << " // Check the predicate.\n" 2421*81ad6265SDimitry Andric << " bool Pred;\n" 2422*81ad6265SDimitry Andric << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n" 2423*81ad6265SDimitry Andric << " Ptr += NumToSkip;\n" 2424*81ad6265SDimitry Andric << " (void)Pred;\n" 2425*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx " 2426*81ad6265SDimitry Andric "<< \"): \"\n" 2427*81ad6265SDimitry Andric << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n" 2428*81ad6265SDimitry Andric << "\n" 2429*81ad6265SDimitry Andric << " break;\n" 2430*81ad6265SDimitry Andric << " }\n" 2431*81ad6265SDimitry Andric << " case MCD::OPC_Decode: {\n" 2432*81ad6265SDimitry Andric << " unsigned Len;\n" 2433*81ad6265SDimitry Andric << " // Decode the Opcode value.\n" 2434*81ad6265SDimitry Andric << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n" 2435*81ad6265SDimitry Andric << " Ptr += Len;\n" 2436*81ad6265SDimitry Andric << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n" 2437*81ad6265SDimitry Andric << " Ptr += Len;\n" 2438*81ad6265SDimitry Andric << "\n" 2439*81ad6265SDimitry Andric << " MI.clear();\n" 2440*81ad6265SDimitry Andric << " MI.setOpcode(Opc);\n" 2441*81ad6265SDimitry Andric << " bool DecodeComplete;\n"; 2442*81ad6265SDimitry Andric if (IsVarLenInst) { 2443*81ad6265SDimitry Andric OS << " Len = InstrLenTable[Opc];\n" 2444*81ad6265SDimitry Andric << " makeUp(insn, Len);\n"; 2445*81ad6265SDimitry Andric } 2446*81ad6265SDimitry Andric OS << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, " 2447*81ad6265SDimitry Andric "DecodeComplete);\n" 2448*81ad6265SDimitry Andric << " assert(DecodeComplete);\n" 2449*81ad6265SDimitry Andric << "\n" 2450*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n" 2451*81ad6265SDimitry Andric << " << \", using decoder \" << DecodeIdx << \": \"\n" 2452*81ad6265SDimitry Andric << " << (S != MCDisassembler::Fail ? \"PASS\" : " 2453*81ad6265SDimitry Andric "\"FAIL\") << \"\\n\");\n" 2454*81ad6265SDimitry Andric << " return S;\n" 2455*81ad6265SDimitry Andric << " }\n" 2456*81ad6265SDimitry Andric << " case MCD::OPC_TryDecode: {\n" 2457*81ad6265SDimitry Andric << " unsigned Len;\n" 2458*81ad6265SDimitry Andric << " // Decode the Opcode value.\n" 2459*81ad6265SDimitry Andric << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n" 2460*81ad6265SDimitry Andric << " Ptr += Len;\n" 2461*81ad6265SDimitry Andric << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n" 2462*81ad6265SDimitry Andric << " Ptr += Len;\n" 2463*81ad6265SDimitry Andric << " // NumToSkip is a plain 24-bit integer.\n" 2464*81ad6265SDimitry Andric << " unsigned NumToSkip = *Ptr++;\n" 2465*81ad6265SDimitry Andric << " NumToSkip |= (*Ptr++) << 8;\n" 2466*81ad6265SDimitry Andric << " NumToSkip |= (*Ptr++) << 16;\n" 2467*81ad6265SDimitry Andric << "\n" 2468*81ad6265SDimitry Andric << " // Perform the decode operation.\n" 2469*81ad6265SDimitry Andric << " MCInst TmpMI;\n" 2470*81ad6265SDimitry Andric << " TmpMI.setOpcode(Opc);\n" 2471*81ad6265SDimitry Andric << " bool DecodeComplete;\n" 2472*81ad6265SDimitry Andric << " S = decodeToMCInst(S, DecodeIdx, insn, TmpMI, Address, DisAsm, " 2473*81ad6265SDimitry Andric "DecodeComplete);\n" 2474*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << Loc << \": OPC_TryDecode: opcode \" << " 2475*81ad6265SDimitry Andric "Opc\n" 2476*81ad6265SDimitry Andric << " << \", using decoder \" << DecodeIdx << \": \");\n" 2477*81ad6265SDimitry Andric << "\n" 2478*81ad6265SDimitry Andric << " if (DecodeComplete) {\n" 2479*81ad6265SDimitry Andric << " // Decoding complete.\n" 2480*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << (S != MCDisassembler::Fail ? \"PASS\" : " 2481*81ad6265SDimitry Andric "\"FAIL\") << \"\\n\");\n" 2482*81ad6265SDimitry Andric << " MI = TmpMI;\n" 2483*81ad6265SDimitry Andric << " return S;\n" 2484*81ad6265SDimitry Andric << " } else {\n" 2485*81ad6265SDimitry Andric << " assert(S == MCDisassembler::Fail);\n" 2486*81ad6265SDimitry Andric << " // If the decoding was incomplete, skip.\n" 2487*81ad6265SDimitry Andric << " Ptr += NumToSkip;\n" 2488*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << \"FAIL: continuing at \" << (Ptr - " 2489*81ad6265SDimitry Andric "DecodeTable) << \"\\n\");\n" 2490*81ad6265SDimitry Andric << " // Reset decode status. This also drops a SoftFail status " 2491*81ad6265SDimitry Andric "that could be\n" 2492*81ad6265SDimitry Andric << " // set before the decode attempt.\n" 2493*81ad6265SDimitry Andric << " S = MCDisassembler::Success;\n" 2494*81ad6265SDimitry Andric << " }\n" 2495*81ad6265SDimitry Andric << " break;\n" 2496*81ad6265SDimitry Andric << " }\n" 2497*81ad6265SDimitry Andric << " case MCD::OPC_SoftFail: {\n" 2498*81ad6265SDimitry Andric << " // Decode the mask values.\n" 2499*81ad6265SDimitry Andric << " unsigned Len;\n" 2500*81ad6265SDimitry Andric << " uint64_t PositiveMask = decodeULEB128(++Ptr, &Len);\n" 2501*81ad6265SDimitry Andric << " Ptr += Len;\n" 2502*81ad6265SDimitry Andric << " uint64_t NegativeMask = decodeULEB128(Ptr, &Len);\n" 2503*81ad6265SDimitry Andric << " Ptr += Len;\n" 2504*81ad6265SDimitry Andric << " bool Fail = (insn & PositiveMask) != 0 || (~insn & " 2505*81ad6265SDimitry Andric "NegativeMask) != 0;\n" 2506*81ad6265SDimitry Andric << " if (Fail)\n" 2507*81ad6265SDimitry Andric << " S = MCDisassembler::SoftFail;\n" 2508*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? " 2509*81ad6265SDimitry Andric "\"FAIL\\n\" : \"PASS\\n\"));\n" 2510*81ad6265SDimitry Andric << " break;\n" 2511*81ad6265SDimitry Andric << " }\n" 2512*81ad6265SDimitry Andric << " case MCD::OPC_Fail: {\n" 2513*81ad6265SDimitry Andric << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n" 2514*81ad6265SDimitry Andric << " return MCDisassembler::Fail;\n" 2515*81ad6265SDimitry Andric << " }\n" 2516*81ad6265SDimitry Andric << " }\n" 2517*81ad6265SDimitry Andric << " }\n" 2518*81ad6265SDimitry Andric << " llvm_unreachable(\"bogosity detected in disassembler state " 2519*81ad6265SDimitry Andric "machine!\");\n" 2520*81ad6265SDimitry Andric << "}\n\n"; 2521*81ad6265SDimitry Andric } 2522*81ad6265SDimitry Andric 2523*81ad6265SDimitry Andric // Emits disassembler code for instruction decoding. 2524*81ad6265SDimitry Andric void DecoderEmitter::run(raw_ostream &o) { 2525*81ad6265SDimitry Andric formatted_raw_ostream OS(o); 2526*81ad6265SDimitry Andric OS << "#include \"llvm/MC/MCInst.h\"\n"; 2527*81ad6265SDimitry Andric OS << "#include \"llvm/MC/MCSubtargetInfo.h\"\n"; 2528*81ad6265SDimitry Andric OS << "#include \"llvm/MC/SubtargetFeature.h\"\n"; 2529*81ad6265SDimitry Andric OS << "#include \"llvm/Support/DataTypes.h\"\n"; 2530*81ad6265SDimitry Andric OS << "#include \"llvm/Support/Debug.h\"\n"; 2531*81ad6265SDimitry Andric OS << "#include \"llvm/Support/LEB128.h\"\n"; 2532*81ad6265SDimitry Andric OS << "#include \"llvm/Support/raw_ostream.h\"\n"; 2533*81ad6265SDimitry Andric OS << "#include <assert.h>\n"; 2534*81ad6265SDimitry Andric OS << '\n'; 2535*81ad6265SDimitry Andric OS << "namespace llvm {\n\n"; 2536*81ad6265SDimitry Andric 2537*81ad6265SDimitry Andric emitFieldFromInstruction(OS); 2538*81ad6265SDimitry Andric emitInsertBits(OS); 2539*81ad6265SDimitry Andric 2540*81ad6265SDimitry Andric Target.reverseBitsForLittleEndianEncoding(); 2541*81ad6265SDimitry Andric 2542*81ad6265SDimitry Andric // Parameterize the decoders based on namespace and instruction width. 2543*81ad6265SDimitry Andric std::set<StringRef> HwModeNames; 2544*81ad6265SDimitry Andric const auto &NumberedInstructions = Target.getInstructionsByEnumValue(); 2545*81ad6265SDimitry Andric NumberedEncodings.reserve(NumberedInstructions.size()); 2546*81ad6265SDimitry Andric DenseMap<Record *, unsigned> IndexOfInstruction; 2547*81ad6265SDimitry Andric // First, collect all HwModes referenced by the target. 2548*81ad6265SDimitry Andric for (const auto &NumberedInstruction : NumberedInstructions) { 2549*81ad6265SDimitry Andric IndexOfInstruction[NumberedInstruction->TheDef] = NumberedEncodings.size(); 2550*81ad6265SDimitry Andric 2551*81ad6265SDimitry Andric if (const RecordVal *RV = 2552*81ad6265SDimitry Andric NumberedInstruction->TheDef->getValue("EncodingInfos")) { 2553*81ad6265SDimitry Andric if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) { 2554*81ad6265SDimitry Andric const CodeGenHwModes &HWM = Target.getHwModes(); 2555*81ad6265SDimitry Andric EncodingInfoByHwMode EBM(DI->getDef(), HWM); 2556*81ad6265SDimitry Andric for (auto &KV : EBM) 2557*81ad6265SDimitry Andric HwModeNames.insert(HWM.getMode(KV.first).Name); 2558*81ad6265SDimitry Andric } 2559*81ad6265SDimitry Andric } 2560*81ad6265SDimitry Andric } 2561*81ad6265SDimitry Andric 2562*81ad6265SDimitry Andric // If HwModeNames is empty, add the empty string so we always have one HwMode. 2563*81ad6265SDimitry Andric if (HwModeNames.empty()) 2564*81ad6265SDimitry Andric HwModeNames.insert(""); 2565*81ad6265SDimitry Andric 2566*81ad6265SDimitry Andric for (const auto &NumberedInstruction : NumberedInstructions) { 2567*81ad6265SDimitry Andric IndexOfInstruction[NumberedInstruction->TheDef] = NumberedEncodings.size(); 2568*81ad6265SDimitry Andric 2569*81ad6265SDimitry Andric if (const RecordVal *RV = 2570*81ad6265SDimitry Andric NumberedInstruction->TheDef->getValue("EncodingInfos")) { 2571*81ad6265SDimitry Andric if (DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) { 2572*81ad6265SDimitry Andric const CodeGenHwModes &HWM = Target.getHwModes(); 2573*81ad6265SDimitry Andric EncodingInfoByHwMode EBM(DI->getDef(), HWM); 2574*81ad6265SDimitry Andric for (auto &KV : EBM) { 2575*81ad6265SDimitry Andric NumberedEncodings.emplace_back(KV.second, NumberedInstruction, 2576*81ad6265SDimitry Andric HWM.getMode(KV.first).Name); 2577*81ad6265SDimitry Andric HwModeNames.insert(HWM.getMode(KV.first).Name); 2578*81ad6265SDimitry Andric } 2579*81ad6265SDimitry Andric continue; 2580*81ad6265SDimitry Andric } 2581*81ad6265SDimitry Andric } 2582*81ad6265SDimitry Andric // This instruction is encoded the same on all HwModes. Emit it for all 2583*81ad6265SDimitry Andric // HwModes. 2584*81ad6265SDimitry Andric for (StringRef HwModeName : HwModeNames) 2585*81ad6265SDimitry Andric NumberedEncodings.emplace_back(NumberedInstruction->TheDef, 2586*81ad6265SDimitry Andric NumberedInstruction, HwModeName); 2587*81ad6265SDimitry Andric } 2588*81ad6265SDimitry Andric for (const auto &NumberedAlias : RK.getAllDerivedDefinitions("AdditionalEncoding")) 2589*81ad6265SDimitry Andric NumberedEncodings.emplace_back( 2590*81ad6265SDimitry Andric NumberedAlias, 2591*81ad6265SDimitry Andric &Target.getInstruction(NumberedAlias->getValueAsDef("AliasOf"))); 2592*81ad6265SDimitry Andric 2593*81ad6265SDimitry Andric std::map<std::pair<std::string, unsigned>, std::vector<EncodingIDAndOpcode>> 2594*81ad6265SDimitry Andric OpcMap; 2595*81ad6265SDimitry Andric std::map<unsigned, std::vector<OperandInfo>> Operands; 2596*81ad6265SDimitry Andric std::vector<unsigned> InstrLen; 2597*81ad6265SDimitry Andric 2598*81ad6265SDimitry Andric bool IsVarLenInst = 2599*81ad6265SDimitry Andric any_of(NumberedInstructions, [](const CodeGenInstruction *CGI) { 2600*81ad6265SDimitry Andric RecordVal *RV = CGI->TheDef->getValue("Inst"); 2601*81ad6265SDimitry Andric return RV && isa<DagInit>(RV->getValue()); 2602*81ad6265SDimitry Andric }); 2603*81ad6265SDimitry Andric unsigned MaxInstLen = 0; 2604*81ad6265SDimitry Andric 2605*81ad6265SDimitry Andric for (unsigned i = 0; i < NumberedEncodings.size(); ++i) { 2606*81ad6265SDimitry Andric const Record *EncodingDef = NumberedEncodings[i].EncodingDef; 2607*81ad6265SDimitry Andric const CodeGenInstruction *Inst = NumberedEncodings[i].Inst; 2608*81ad6265SDimitry Andric const Record *Def = Inst->TheDef; 2609*81ad6265SDimitry Andric unsigned Size = EncodingDef->getValueAsInt("Size"); 2610*81ad6265SDimitry Andric if (Def->getValueAsString("Namespace") == "TargetOpcode" || 2611*81ad6265SDimitry Andric Def->getValueAsBit("isPseudo") || 2612*81ad6265SDimitry Andric Def->getValueAsBit("isAsmParserOnly") || 2613*81ad6265SDimitry Andric Def->getValueAsBit("isCodeGenOnly")) { 2614*81ad6265SDimitry Andric NumEncodingsLackingDisasm++; 2615*81ad6265SDimitry Andric continue; 2616*81ad6265SDimitry Andric } 2617*81ad6265SDimitry Andric 2618*81ad6265SDimitry Andric if (i < NumberedInstructions.size()) 2619*81ad6265SDimitry Andric NumInstructions++; 2620*81ad6265SDimitry Andric NumEncodings++; 2621*81ad6265SDimitry Andric 2622*81ad6265SDimitry Andric if (!Size && !IsVarLenInst) 2623*81ad6265SDimitry Andric continue; 2624*81ad6265SDimitry Andric 2625*81ad6265SDimitry Andric if (IsVarLenInst) 2626*81ad6265SDimitry Andric InstrLen.resize(NumberedInstructions.size(), 0); 2627*81ad6265SDimitry Andric 2628*81ad6265SDimitry Andric if (unsigned Len = populateInstruction(Target, *EncodingDef, *Inst, i, 2629*81ad6265SDimitry Andric Operands, IsVarLenInst)) { 2630*81ad6265SDimitry Andric if (IsVarLenInst) { 2631*81ad6265SDimitry Andric MaxInstLen = std::max(MaxInstLen, Len); 2632*81ad6265SDimitry Andric InstrLen[i] = Len; 2633*81ad6265SDimitry Andric } 2634*81ad6265SDimitry Andric std::string DecoderNamespace = 2635*81ad6265SDimitry Andric std::string(EncodingDef->getValueAsString("DecoderNamespace")); 2636*81ad6265SDimitry Andric if (!NumberedEncodings[i].HwModeName.empty()) 2637*81ad6265SDimitry Andric DecoderNamespace += 2638*81ad6265SDimitry Andric std::string("_") + NumberedEncodings[i].HwModeName.str(); 2639*81ad6265SDimitry Andric OpcMap[std::make_pair(DecoderNamespace, Size)].emplace_back( 2640*81ad6265SDimitry Andric i, IndexOfInstruction.find(Def)->second); 2641*81ad6265SDimitry Andric } else { 2642*81ad6265SDimitry Andric NumEncodingsOmitted++; 2643*81ad6265SDimitry Andric } 2644*81ad6265SDimitry Andric } 2645*81ad6265SDimitry Andric 2646*81ad6265SDimitry Andric DecoderTableInfo TableInfo; 2647*81ad6265SDimitry Andric for (const auto &Opc : OpcMap) { 2648*81ad6265SDimitry Andric // Emit the decoder for this namespace+width combination. 2649*81ad6265SDimitry Andric ArrayRef<EncodingAndInst> NumberedEncodingsRef( 2650*81ad6265SDimitry Andric NumberedEncodings.data(), NumberedEncodings.size()); 2651*81ad6265SDimitry Andric FilterChooser FC(NumberedEncodingsRef, Opc.second, Operands, 2652*81ad6265SDimitry Andric IsVarLenInst ? MaxInstLen : 8 * Opc.first.second, this); 2653*81ad6265SDimitry Andric 2654*81ad6265SDimitry Andric // The decode table is cleared for each top level decoder function. The 2655*81ad6265SDimitry Andric // predicates and decoders themselves, however, are shared across all 2656*81ad6265SDimitry Andric // decoders to give more opportunities for uniqueing. 2657*81ad6265SDimitry Andric TableInfo.Table.clear(); 2658*81ad6265SDimitry Andric TableInfo.FixupStack.clear(); 2659*81ad6265SDimitry Andric TableInfo.Table.reserve(16384); 2660*81ad6265SDimitry Andric TableInfo.FixupStack.emplace_back(); 2661*81ad6265SDimitry Andric FC.emitTableEntries(TableInfo); 2662*81ad6265SDimitry Andric // Any NumToSkip fixups in the top level scope can resolve to the 2663*81ad6265SDimitry Andric // OPC_Fail at the end of the table. 2664*81ad6265SDimitry Andric assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!"); 2665*81ad6265SDimitry Andric // Resolve any NumToSkip fixups in the current scope. 2666*81ad6265SDimitry Andric resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(), 2667*81ad6265SDimitry Andric TableInfo.Table.size()); 2668*81ad6265SDimitry Andric TableInfo.FixupStack.clear(); 2669*81ad6265SDimitry Andric 2670*81ad6265SDimitry Andric TableInfo.Table.push_back(MCD::OPC_Fail); 2671*81ad6265SDimitry Andric 2672*81ad6265SDimitry Andric // Print the table to the output stream. 2673*81ad6265SDimitry Andric emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), Opc.first.first); 2674*81ad6265SDimitry Andric OS.flush(); 2675*81ad6265SDimitry Andric } 2676*81ad6265SDimitry Andric 2677*81ad6265SDimitry Andric // For variable instruction, we emit a instruction length table 2678*81ad6265SDimitry Andric // to let the decoder know how long the instructions are. 2679*81ad6265SDimitry Andric // You can see example usage in M68k's disassembler. 2680*81ad6265SDimitry Andric if (IsVarLenInst) 2681*81ad6265SDimitry Andric emitInstrLenTable(OS, InstrLen); 2682*81ad6265SDimitry Andric // Emit the predicate function. 2683*81ad6265SDimitry Andric emitPredicateFunction(OS, TableInfo.Predicates, 0); 2684*81ad6265SDimitry Andric 2685*81ad6265SDimitry Andric // Emit the decoder function. 2686*81ad6265SDimitry Andric emitDecoderFunction(OS, TableInfo.Decoders, 0); 2687*81ad6265SDimitry Andric 2688*81ad6265SDimitry Andric // Emit the main entry point for the decoder, decodeInstruction(). 2689*81ad6265SDimitry Andric emitDecodeInstruction(OS, IsVarLenInst); 2690*81ad6265SDimitry Andric 2691*81ad6265SDimitry Andric OS << "\n} // end namespace llvm\n"; 2692*81ad6265SDimitry Andric } 2693*81ad6265SDimitry Andric 2694*81ad6265SDimitry Andric namespace llvm { 2695*81ad6265SDimitry Andric 2696*81ad6265SDimitry Andric void EmitDecoder(RecordKeeper &RK, raw_ostream &OS, 2697*81ad6265SDimitry Andric const std::string &PredicateNamespace, 2698*81ad6265SDimitry Andric const std::string &GPrefix, const std::string &GPostfix, 2699*81ad6265SDimitry Andric const std::string &ROK, const std::string &RFail, 2700*81ad6265SDimitry Andric const std::string &L) { 2701*81ad6265SDimitry Andric DecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix, ROK, RFail, L) 2702*81ad6265SDimitry Andric .run(OS); 2703*81ad6265SDimitry Andric } 2704*81ad6265SDimitry Andric 2705*81ad6265SDimitry Andric } // end namespace llvm 2706