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