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