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