Add FixedLenDecoderEmitter, the skeleton of a new disassembler emitter for fixed...
[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 "FixedLenDecoderEmitter.h"
18 #include "CodeGenTarget.h"
19 #include "Record.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 #include <vector>
25 #include <map>
26 #include <string>
27
28 using namespace llvm;
29
30 // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
31 // for a bit value.
32 //
33 // BIT_UNFILTERED is used as the init value for a filter position.  It is used
34 // only for filter processings.
35 typedef enum {
36   BIT_TRUE,      // '1'
37   BIT_FALSE,     // '0'
38   BIT_UNSET,     // '?'
39   BIT_UNFILTERED // unfiltered
40 } bit_value_t;
41
42 static bool ValueSet(bit_value_t V) {
43   return (V == BIT_TRUE || V == BIT_FALSE);
44 }
45 static bool ValueNotSet(bit_value_t V) {
46   return (V == BIT_UNSET);
47 }
48 static int Value(bit_value_t V) {
49   return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
50 }
51 static bit_value_t bitFromBits(BitsInit &bits, unsigned index) {
52   if (BitInit *bit = dynamic_cast<BitInit*>(bits.getBit(index)))
53     return bit->getValue() ? BIT_TRUE : BIT_FALSE;
54
55   // The bit is uninitialized.
56   return BIT_UNSET;
57 }
58 // Prints the bit value for each position.
59 static void dumpBits(raw_ostream &o, BitsInit &bits) {
60   unsigned index;
61
62   for (index = bits.getNumBits(); index > 0; index--) {
63     switch (bitFromBits(bits, index - 1)) {
64     case BIT_TRUE:
65       o << "1";
66       break;
67     case BIT_FALSE:
68       o << "0";
69       break;
70     case BIT_UNSET:
71       o << "_";
72       break;
73     default:
74       assert(0 && "unexpected return value from bitFromBits");
75     }
76   }
77 }
78
79 static BitsInit &getBitsField(const Record &def, const char *str) {
80   BitsInit *bits = def.getValueAsBitsInit(str);
81   return *bits;
82 }
83
84 // Forward declaration.
85 class FilterChooser;
86
87 // FIXME: Possibly auto-detected?
88 #define BIT_WIDTH 32
89
90 // Representation of the instruction to work on.
91 typedef bit_value_t insn_t[BIT_WIDTH];
92
93 /// Filter - Filter works with FilterChooser to produce the decoding tree for
94 /// the ISA.
95 ///
96 /// It is useful to think of a Filter as governing the switch stmts of the
97 /// decoding tree in a certain level.  Each case stmt delegates to an inferior
98 /// FilterChooser to decide what further decoding logic to employ, or in another
99 /// words, what other remaining bits to look at.  The FilterChooser eventually
100 /// chooses a best Filter to do its job.
101 ///
102 /// This recursive scheme ends when the number of Opcodes assigned to the
103 /// FilterChooser becomes 1 or if there is a conflict.  A conflict happens when
104 /// the Filter/FilterChooser combo does not know how to distinguish among the
105 /// Opcodes assigned.
106 ///
107 /// An example of a conflict is
108 ///
109 /// Conflict:
110 ///                     111101000.00........00010000....
111 ///                     111101000.00........0001........
112 ///                     1111010...00........0001........
113 ///                     1111010...00....................
114 ///                     1111010.........................
115 ///                     1111............................
116 ///                     ................................
117 ///     VST4q8a         111101000_00________00010000____
118 ///     VST4q8b         111101000_00________00010000____
119 ///
120 /// The Debug output shows the path that the decoding tree follows to reach the
121 /// the conclusion that there is a conflict.  VST4q8a is a vst4 to double-spaced
122 /// even registers, while VST4q8b is a vst4 to double-spaced odd regsisters.
123 ///
124 /// The encoding info in the .td files does not specify this meta information,
125 /// which could have been used by the decoder to resolve the conflict.  The
126 /// decoder could try to decode the even/odd register numbering and assign to
127 /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
128 /// version and return the Opcode since the two have the same Asm format string.
129 class Filter {
130 protected:
131   FilterChooser *Owner; // points to the FilterChooser who owns this filter
132   unsigned StartBit; // the starting bit position
133   unsigned NumBits; // number of bits to filter
134   bool Mixed; // a mixed region contains both set and unset bits
135
136   // Map of well-known segment value to the set of uid's with that value.
137   std::map<uint64_t, std::vector<unsigned> > FilteredInstructions;
138
139   // Set of uid's with non-constant segment values.
140   std::vector<unsigned> VariableInstructions;
141
142   // Map of well-known segment value to its delegate.
143   std::map<unsigned, FilterChooser*> FilterChooserMap;
144
145   // Number of instructions which fall under FilteredInstructions category.
146   unsigned NumFiltered;
147
148   // Keeps track of the last opcode in the filtered bucket.
149   unsigned LastOpcFiltered;
150
151   // Number of instructions which fall under VariableInstructions category.
152   unsigned NumVariable;
153
154 public:
155   unsigned getNumFiltered() { return NumFiltered; }
156   unsigned getNumVariable() { return NumVariable; }
157   unsigned getSingletonOpc() {
158     assert(NumFiltered == 1);
159     return LastOpcFiltered;
160   }
161   // Return the filter chooser for the group of instructions without constant
162   // segment values.
163   FilterChooser &getVariableFC() {
164     assert(NumFiltered == 1);
165     assert(FilterChooserMap.size() == 1);
166     return *(FilterChooserMap.find((unsigned)-1)->second);
167   }
168
169   Filter(const Filter &f);
170   Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
171
172   ~Filter();
173
174   // Divides the decoding task into sub tasks and delegates them to the
175   // inferior FilterChooser's.
176   //
177   // A special case arises when there's only one entry in the filtered
178   // instructions.  In order to unambiguously decode the singleton, we need to
179   // match the remaining undecoded encoding bits against the singleton.
180   void recurse();
181
182   // Emit code to decode instructions given a segment or segments of bits.
183   void emit(raw_ostream &o, unsigned &Indentation);
184
185   // Returns the number of fanout produced by the filter.  More fanout implies
186   // the filter distinguishes more categories of instructions.
187   unsigned usefulness() const;
188 }; // End of class Filter
189
190 // These are states of our finite state machines used in FilterChooser's
191 // filterProcessor() which produces the filter candidates to use.
192 typedef enum {
193   ATTR_NONE,
194   ATTR_FILTERED,
195   ATTR_ALL_SET,
196   ATTR_ALL_UNSET,
197   ATTR_MIXED
198 } bitAttr_t;
199
200 /// FilterChooser - FilterChooser chooses the best filter among a set of Filters
201 /// in order to perform the decoding of instructions at the current level.
202 ///
203 /// Decoding proceeds from the top down.  Based on the well-known encoding bits
204 /// of instructions available, FilterChooser builds up the possible Filters that
205 /// can further the task of decoding by distinguishing among the remaining
206 /// candidate instructions.
207 ///
208 /// Once a filter has been chosen, it is called upon to divide the decoding task
209 /// into sub-tasks and delegates them to its inferior FilterChoosers for further
210 /// processings.
211 ///
212 /// It is useful to think of a Filter as governing the switch stmts of the
213 /// decoding tree.  And each case is delegated to an inferior FilterChooser to
214 /// decide what further remaining bits to look at.
215 class FilterChooser {
216 protected:
217   friend class Filter;
218
219   // Vector of codegen instructions to choose our filter.
220   const std::vector<const CodeGenInstruction*> &AllInstructions;
221
222   // Vector of uid's for this filter chooser to work on.
223   const std::vector<unsigned> Opcodes;
224
225   // Lookup table for the operand decoding of instructions.
226   std::map<unsigned, std::vector<OperandInfo> > &Operands;
227
228   // Vector of candidate filters.
229   std::vector<Filter> Filters;
230
231   // Array of bit values passed down from our parent.
232   // Set to all BIT_UNFILTERED's for Parent == NULL.
233   bit_value_t FilterBitValues[BIT_WIDTH];
234
235   // Links to the FilterChooser above us in the decoding tree.
236   FilterChooser *Parent;
237
238   // Index of the best filter from Filters.
239   int BestIndex;
240
241 public:
242   FilterChooser(const FilterChooser &FC) :
243     AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
244       Operands(FC.Operands), Filters(FC.Filters), Parent(FC.Parent),
245       BestIndex(FC.BestIndex) {
246     memcpy(FilterBitValues, FC.FilterBitValues, sizeof(FilterBitValues));
247   }
248
249   FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
250                 const std::vector<unsigned> &IDs,
251     std::map<unsigned, std::vector<OperandInfo> > &Ops) :
252       AllInstructions(Insts), Opcodes(IDs), Operands(Ops), Filters(),
253       Parent(NULL), BestIndex(-1) {
254     for (unsigned i = 0; i < BIT_WIDTH; ++i)
255       FilterBitValues[i] = BIT_UNFILTERED;
256
257     doFilter();
258   }
259
260   FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
261                 const std::vector<unsigned> &IDs,
262         std::map<unsigned, std::vector<OperandInfo> > &Ops,
263                 bit_value_t (&ParentFilterBitValues)[BIT_WIDTH],
264                 FilterChooser &parent) :
265       AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
266       Filters(), Parent(&parent), BestIndex(-1) {
267     for (unsigned i = 0; i < BIT_WIDTH; ++i)
268       FilterBitValues[i] = ParentFilterBitValues[i];
269
270     doFilter();
271   }
272
273   // The top level filter chooser has NULL as its parent.
274   bool isTopLevel() { return Parent == NULL; }
275
276   // Emit the top level typedef and decodeInstruction() function.
277   void emitTop(raw_ostream &o, unsigned Indentation);
278
279 protected:
280   // Populates the insn given the uid.
281   void insnWithID(insn_t &Insn, unsigned Opcode) const {
282     BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
283
284     for (unsigned i = 0; i < BIT_WIDTH; ++i)
285       Insn[i] = bitFromBits(Bits, i);
286   }
287
288   // Returns the record name.
289   const std::string &nameWithID(unsigned Opcode) const {
290     return AllInstructions[Opcode]->TheDef->getName();
291   }
292
293   // Populates the field of the insn given the start position and the number of
294   // consecutive bits to scan for.
295   //
296   // Returns false if there exists any uninitialized bit value in the range.
297   // Returns true, otherwise.
298   bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
299       unsigned NumBits) const;
300
301   /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
302   /// filter array as a series of chars.
303   void dumpFilterArray(raw_ostream &o, bit_value_t (&filter)[BIT_WIDTH]);
304
305   /// dumpStack - dumpStack traverses the filter chooser chain and calls
306   /// dumpFilterArray on each filter chooser up to the top level one.
307   void dumpStack(raw_ostream &o, const char *prefix);
308
309   Filter &bestFilter() {
310     assert(BestIndex != -1 && "BestIndex not set");
311     return Filters[BestIndex];
312   }
313
314   // Called from Filter::recurse() when singleton exists.  For debug purpose.
315   void SingletonExists(unsigned Opc);
316
317   bool PositionFiltered(unsigned i) {
318     return ValueSet(FilterBitValues[i]);
319   }
320
321   // Calculates the island(s) needed to decode the instruction.
322   // This returns a lit of undecoded bits of an instructions, for example,
323   // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
324   // decoded bits in order to verify that the instruction matches the Opcode.
325   unsigned getIslands(std::vector<unsigned> &StartBits,
326       std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
327       insn_t &Insn);
328
329   // Emits code to decode the singleton.  Return true if we have matched all the
330   // well-known bits.
331   bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc);
332
333   // Emits code to decode the singleton, and then to decode the rest.
334   void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,Filter &Best);
335
336   // Assign a single filter and run with it.
337   void runSingleFilter(FilterChooser &owner, unsigned startBit, unsigned numBit,
338       bool mixed);
339
340   // reportRegion is a helper function for filterProcessor to mark a region as
341   // eligible for use as a filter region.
342   void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
343       bool AllowMixed);
344
345   // FilterProcessor scans the well-known encoding bits of the instructions and
346   // builds up a list of candidate filters.  It chooses the best filter and
347   // recursively descends down the decoding tree.
348   bool filterProcessor(bool AllowMixed, bool Greedy = true);
349
350   // Decides on the best configuration of filter(s) to use in order to decode
351   // the instructions.  A conflict of instructions may occur, in which case we
352   // dump the conflict set to the standard error.
353   void doFilter();
354
355   // Emits code to decode our share of instructions.  Returns true if the
356   // emitted code causes a return, which occurs if we know how to decode
357   // the instruction at this level or the instruction is not decodeable.
358   bool emit(raw_ostream &o, unsigned &Indentation);
359 };
360
361 ///////////////////////////
362 //                       //
363 // Filter Implmenetation //
364 //                       //
365 ///////////////////////////
366
367 Filter::Filter(const Filter &f) :
368   Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
369   FilteredInstructions(f.FilteredInstructions),
370   VariableInstructions(f.VariableInstructions),
371   FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered),
372   LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) {
373 }
374
375 Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
376     bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits),
377                   Mixed(mixed) {
378   assert(StartBit + NumBits - 1 < BIT_WIDTH);
379
380   NumFiltered = 0;
381   LastOpcFiltered = 0;
382   NumVariable = 0;
383
384   for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
385     insn_t Insn;
386
387     // Populates the insn given the uid.
388     Owner->insnWithID(Insn, Owner->Opcodes[i]);
389
390     uint64_t Field;
391     // Scans the segment for possibly well-specified encoding bits.
392     bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
393
394     if (ok) {
395       // The encoding bits are well-known.  Lets add the uid of the
396       // instruction into the bucket keyed off the constant field value.
397       LastOpcFiltered = Owner->Opcodes[i];
398       FilteredInstructions[Field].push_back(LastOpcFiltered);
399       ++NumFiltered;
400     } else {
401       // Some of the encoding bit(s) are unspecfied.  This contributes to
402       // one additional member of "Variable" instructions.
403       VariableInstructions.push_back(Owner->Opcodes[i]);
404       ++NumVariable;
405     }
406   }
407
408   assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
409          && "Filter returns no instruction categories");
410 }
411
412 Filter::~Filter() {
413   std::map<unsigned, FilterChooser*>::iterator filterIterator;
414   for (filterIterator = FilterChooserMap.begin();
415        filterIterator != FilterChooserMap.end();
416        filterIterator++) {
417     delete filterIterator->second;
418   }
419 }
420
421 // Divides the decoding task into sub tasks and delegates them to the
422 // inferior FilterChooser's.
423 //
424 // A special case arises when there's only one entry in the filtered
425 // instructions.  In order to unambiguously decode the singleton, we need to
426 // match the remaining undecoded encoding bits against the singleton.
427 void Filter::recurse() {
428   std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator;
429
430   bit_value_t BitValueArray[BIT_WIDTH];
431   // Starts by inheriting our parent filter chooser's filter bit values.
432   memcpy(BitValueArray, Owner->FilterBitValues, sizeof(BitValueArray));
433
434   unsigned bitIndex;
435
436   if (VariableInstructions.size()) {
437     // Conservatively marks each segment position as BIT_UNSET.
438     for (bitIndex = 0; bitIndex < NumBits; bitIndex++)
439       BitValueArray[StartBit + bitIndex] = BIT_UNSET;
440
441     // Delegates to an inferior filter chooser for futher processing on this
442     // group of instructions whose segment values are variable.
443     FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
444                               (unsigned)-1,
445                               new FilterChooser(Owner->AllInstructions,
446                                                 VariableInstructions,
447                                                 Owner->Operands,
448                                                 BitValueArray,
449                                                 *Owner)
450                               ));
451   }
452
453   // No need to recurse for a singleton filtered instruction.
454   // See also Filter::emit().
455   if (getNumFiltered() == 1) {
456     //Owner->SingletonExists(LastOpcFiltered);
457     assert(FilterChooserMap.size() == 1);
458     return;
459   }
460
461   // Otherwise, create sub choosers.
462   for (mapIterator = FilteredInstructions.begin();
463        mapIterator != FilteredInstructions.end();
464        mapIterator++) {
465
466     // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
467     for (bitIndex = 0; bitIndex < NumBits; bitIndex++) {
468       if (mapIterator->first & (1ULL << bitIndex))
469         BitValueArray[StartBit + bitIndex] = BIT_TRUE;
470       else
471         BitValueArray[StartBit + bitIndex] = BIT_FALSE;
472     }
473
474     // Delegates to an inferior filter chooser for futher processing on this
475     // category of instructions.
476     FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
477                               mapIterator->first,
478                               new FilterChooser(Owner->AllInstructions,
479                                                 mapIterator->second,
480                                                 Owner->Operands,
481                                                 BitValueArray,
482                                                 *Owner)
483                               ));
484   }
485 }
486
487 // Emit code to decode instructions given a segment or segments of bits.
488 void Filter::emit(raw_ostream &o, unsigned &Indentation) {
489   o.indent(Indentation) << "// Check Inst{";
490
491   if (NumBits > 1)
492     o << (StartBit + NumBits - 1) << '-';
493
494   o << StartBit << "} ...\n";
495
496   o.indent(Indentation) << "switch (fieldFromInstruction(insn, "
497                         << StartBit << ", " << NumBits << ")) {\n";
498
499   std::map<unsigned, FilterChooser*>::iterator filterIterator;
500
501   bool DefaultCase = false;
502   for (filterIterator = FilterChooserMap.begin();
503        filterIterator != FilterChooserMap.end();
504        filterIterator++) {
505
506     // Field value -1 implies a non-empty set of variable instructions.
507     // See also recurse().
508     if (filterIterator->first == (unsigned)-1) {
509       DefaultCase = true;
510
511       o.indent(Indentation) << "default:\n";
512       o.indent(Indentation) << "  break; // fallthrough\n";
513
514       // Closing curly brace for the switch statement.
515       // This is unconventional because we want the default processing to be
516       // performed for the fallthrough cases as well, i.e., when the "cases"
517       // did not prove a decoded instruction.
518       o.indent(Indentation) << "}\n";
519
520     } else
521       o.indent(Indentation) << "case " << filterIterator->first << ":\n";
522
523     // We arrive at a category of instructions with the same segment value.
524     // Now delegate to the sub filter chooser for further decodings.
525     // The case may fallthrough, which happens if the remaining well-known
526     // encoding bits do not match exactly.
527     if (!DefaultCase) { ++Indentation; ++Indentation; }
528
529     bool finished = filterIterator->second->emit(o, Indentation);
530     // For top level default case, there's no need for a break statement.
531     if (Owner->isTopLevel() && DefaultCase)
532       break;
533     if (!finished)
534       o.indent(Indentation) << "break;\n";
535
536     if (!DefaultCase) { --Indentation; --Indentation; }
537   }
538
539   // If there is no default case, we still need to supply a closing brace.
540   if (!DefaultCase) {
541     // Closing curly brace for the switch statement.
542     o.indent(Indentation) << "}\n";
543   }
544 }
545
546 // Returns the number of fanout produced by the filter.  More fanout implies
547 // the filter distinguishes more categories of instructions.
548 unsigned Filter::usefulness() const {
549   if (VariableInstructions.size())
550     return FilteredInstructions.size();
551   else
552     return FilteredInstructions.size() + 1;
553 }
554
555 //////////////////////////////////
556 //                              //
557 // Filterchooser Implementation //
558 //                              //
559 //////////////////////////////////
560
561 // Emit the top level typedef and decodeInstruction() function.
562 void FilterChooser::emitTop(raw_ostream &o, unsigned Indentation) {
563   switch (BIT_WIDTH) {
564   case 8:
565     o.indent(Indentation) << "typedef uint8_t field_t;\n";
566     break;
567   case 16:
568     o.indent(Indentation) << "typedef uint16_t field_t;\n";
569     break;
570   case 32:
571     o.indent(Indentation) << "typedef uint32_t field_t;\n";
572     break;
573   case 64:
574     o.indent(Indentation) << "typedef uint64_t field_t;\n";
575     break;
576   default:
577     assert(0 && "Unexpected instruction size!");
578   }
579
580   o << '\n';
581
582   o.indent(Indentation) << "static field_t " <<
583     "fieldFromInstruction(field_t insn, unsigned startBit, unsigned numBits)\n";
584
585   o.indent(Indentation) << "{\n";
586
587   ++Indentation; ++Indentation;
588   o.indent(Indentation) << "assert(startBit + numBits <= " << BIT_WIDTH
589                         << " && \"Instruction field out of bounds!\");\n";
590   o << '\n';
591   o.indent(Indentation) << "field_t fieldMask;\n";
592   o << '\n';
593   o.indent(Indentation) << "if (numBits == " << BIT_WIDTH << ")\n";
594
595   ++Indentation; ++Indentation;
596   o.indent(Indentation) << "fieldMask = (field_t)-1;\n";
597   --Indentation; --Indentation;
598
599   o.indent(Indentation) << "else\n";
600
601   ++Indentation; ++Indentation;
602   o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n";
603   --Indentation; --Indentation;
604
605   o << '\n';
606   o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n";
607   --Indentation; --Indentation;
608
609   o.indent(Indentation) << "}\n";
610
611   o << '\n';
612
613   o.indent(Indentation) <<
614     "static bool decodeInstruction(MCInst &MI, field_t insn) {\n";
615   o.indent(Indentation) << "  unsigned tmp = 0;\n";
616
617   ++Indentation; ++Indentation;
618   // Emits code to decode the instructions.
619   emit(o, Indentation);
620
621   o << '\n';
622   o.indent(Indentation) << "return false;\n";
623   --Indentation; --Indentation;
624
625   o.indent(Indentation) << "}\n";
626
627   o << '\n';
628 }
629
630 // Populates the field of the insn given the start position and the number of
631 // consecutive bits to scan for.
632 //
633 // Returns false if and on the first uninitialized bit value encountered.
634 // Returns true, otherwise.
635 bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
636     unsigned StartBit, unsigned NumBits) const {
637   Field = 0;
638
639   for (unsigned i = 0; i < NumBits; ++i) {
640     if (Insn[StartBit + i] == BIT_UNSET)
641       return false;
642
643     if (Insn[StartBit + i] == BIT_TRUE)
644       Field = Field | (1ULL << i);
645   }
646
647   return true;
648 }
649
650 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
651 /// filter array as a series of chars.
652 void FilterChooser::dumpFilterArray(raw_ostream &o,
653                                     bit_value_t (&filter)[BIT_WIDTH]) {
654   unsigned bitIndex;
655
656   for (bitIndex = BIT_WIDTH; bitIndex > 0; bitIndex--) {
657     switch (filter[bitIndex - 1]) {
658     case BIT_UNFILTERED:
659       o << ".";
660       break;
661     case BIT_UNSET:
662       o << "_";
663       break;
664     case BIT_TRUE:
665       o << "1";
666       break;
667     case BIT_FALSE:
668       o << "0";
669       break;
670     }
671   }
672 }
673
674 /// dumpStack - dumpStack traverses the filter chooser chain and calls
675 /// dumpFilterArray on each filter chooser up to the top level one.
676 void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) {
677   FilterChooser *current = this;
678
679   while (current) {
680     o << prefix;
681     dumpFilterArray(o, current->FilterBitValues);
682     o << '\n';
683     current = current->Parent;
684   }
685 }
686
687 // Called from Filter::recurse() when singleton exists.  For debug purpose.
688 void FilterChooser::SingletonExists(unsigned Opc) {
689   insn_t Insn0;
690   insnWithID(Insn0, Opc);
691
692   errs() << "Singleton exists: " << nameWithID(Opc)
693          << " with its decoding dominating ";
694   for (unsigned i = 0; i < Opcodes.size(); ++i) {
695     if (Opcodes[i] == Opc) continue;
696     errs() << nameWithID(Opcodes[i]) << ' ';
697   }
698   errs() << '\n';
699
700   dumpStack(errs(), "\t\t");
701   for (unsigned i = 0; i < Opcodes.size(); i++) {
702     const std::string &Name = nameWithID(Opcodes[i]);
703
704     errs() << '\t' << Name << " ";
705     dumpBits(errs(),
706              getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
707     errs() << '\n';
708   }
709 }
710
711 // Calculates the island(s) needed to decode the instruction.
712 // This returns a list of undecoded bits of an instructions, for example,
713 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
714 // decoded bits in order to verify that the instruction matches the Opcode.
715 unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
716     std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
717     insn_t &Insn) {
718   unsigned Num, BitNo;
719   Num = BitNo = 0;
720
721   uint64_t FieldVal = 0;
722
723   // 0: Init
724   // 1: Water (the bit value does not affect decoding)
725   // 2: Island (well-known bit value needed for decoding)
726   int State = 0;
727   int Val = -1;
728
729   for (unsigned i = 0; i < BIT_WIDTH; ++i) {
730     Val = Value(Insn[i]);
731     bool Filtered = PositionFiltered(i);
732     switch (State) {
733     default:
734       assert(0 && "Unreachable code!");
735       break;
736     case 0:
737     case 1:
738       if (Filtered || Val == -1)
739         State = 1; // Still in Water
740       else {
741         State = 2; // Into the Island
742         BitNo = 0;
743         StartBits.push_back(i);
744         FieldVal = Val;
745       }
746       break;
747     case 2:
748       if (Filtered || Val == -1) {
749         State = 1; // Into the Water
750         EndBits.push_back(i - 1);
751         FieldVals.push_back(FieldVal);
752         ++Num;
753       } else {
754         State = 2; // Still in Island
755         ++BitNo;
756         FieldVal = FieldVal | Val << BitNo;
757       }
758       break;
759     }
760   }
761   // If we are still in Island after the loop, do some housekeeping.
762   if (State == 2) {
763     EndBits.push_back(BIT_WIDTH - 1);
764     FieldVals.push_back(FieldVal);
765     ++Num;
766   }
767
768   assert(StartBits.size() == Num && EndBits.size() == Num &&
769          FieldVals.size() == Num);
770   return Num;
771 }
772
773 // Emits code to decode the singleton.  Return true if we have matched all the
774 // well-known bits.
775 bool FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
776                                          unsigned Opc) {
777   std::vector<unsigned> StartBits;
778   std::vector<unsigned> EndBits;
779   std::vector<uint64_t> FieldVals;
780   insn_t Insn;
781   insnWithID(Insn, Opc);
782
783   // Look for islands of undecoded bits of the singleton.
784   getIslands(StartBits, EndBits, FieldVals, Insn);
785
786   unsigned Size = StartBits.size();
787   unsigned I, NumBits;
788
789   // If we have matched all the well-known bits, just issue a return.
790   if (Size == 0) {
791     o.indent(Indentation) << "{\n";
792     o.indent(Indentation) << "  MI.setOpcode(" << Opc << ");\n";
793     std::vector<OperandInfo>& InsnOperands = Operands[Opc];
794     for (std::vector<OperandInfo>::iterator
795          I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
796       // If a custom instruction decoder was specified, use that.
797       if (I->FieldBase == ~0U && I->FieldLength == ~0U) {
798         o.indent(Indentation) << "  " << I->Decoder << "(MI, insn);\n";
799         break;
800       }
801
802       o.indent(Indentation)
803         << "  tmp = fieldFromInstruction(insn, " << I->FieldBase
804         << ", " << I->FieldLength << ");\n";
805       if (I->Decoder != "") {
806         o.indent(Indentation) << "  " << I->Decoder << "(MI, tmp);\n";
807       } else {
808         o.indent(Indentation)
809           << "  MI.addOperand(MCOperand::CreateImm(tmp));\n";
810       }
811     }
812
813     o.indent(Indentation) << "  return true; // " << nameWithID(Opc)
814                           << '\n';
815     o.indent(Indentation) << "}\n";
816     return true;
817   }
818
819   // Otherwise, there are more decodings to be done!
820
821   // Emit code to match the island(s) for the singleton.
822   o.indent(Indentation) << "// Check ";
823
824   for (I = Size; I != 0; --I) {
825     o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} ";
826     if (I > 1)
827       o << "&& ";
828     else
829       o << "for singleton decoding...\n";
830   }
831
832   o.indent(Indentation) << "if (";
833
834   for (I = Size; I != 0; --I) {
835     NumBits = EndBits[I-1] - StartBits[I-1] + 1;
836     o << "fieldFromInstruction(insn, " << StartBits[I-1] << ", " << NumBits
837       << ") == " << FieldVals[I-1];
838     if (I > 1)
839       o << " && ";
840     else
841       o << ") {\n";
842   }
843   o.indent(Indentation) << "  MI.setOpcode(" << Opc << ");\n";
844   std::vector<OperandInfo>& InsnOperands = Operands[Opc];
845   for (std::vector<OperandInfo>::iterator
846        I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
847     // If a custom instruction decoder was specified, use that.
848     if (I->FieldBase == ~0U && I->FieldLength == ~0U) {
849       o.indent(Indentation) << "  " << I->Decoder << "(MI, insn);\n";
850       break;
851     }
852
853     o.indent(Indentation)
854       << "  tmp = fieldFromInstruction(insn, " << I->FieldBase
855       << ", " << I->FieldLength << ");\n";
856     if (I->Decoder != "") {
857       o.indent(Indentation) << "  " << I->Decoder << "(MI, tmp);\n";
858     } else {
859       o.indent(Indentation)
860         << "  MI.addOperand(MCOperand::CreateImm(tmp));\n";
861     }
862   }
863   o.indent(Indentation) << "  return true; // " << nameWithID(Opc)
864                         << '\n';
865   o.indent(Indentation) << "}\n";
866
867   return false;
868 }
869
870 // Emits code to decode the singleton, and then to decode the rest.
871 void FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
872     Filter &Best) {
873
874   unsigned Opc = Best.getSingletonOpc();
875
876   emitSingletonDecoder(o, Indentation, Opc);
877
878   // Emit code for the rest.
879   o.indent(Indentation) << "else\n";
880
881   Indentation += 2;
882   Best.getVariableFC().emit(o, Indentation);
883   Indentation -= 2;
884 }
885
886 // Assign a single filter and run with it.  Top level API client can initialize
887 // with a single filter to start the filtering process.
888 void FilterChooser::runSingleFilter(FilterChooser &owner, unsigned startBit,
889     unsigned numBit, bool mixed) {
890   Filters.clear();
891   Filter F(*this, startBit, numBit, true);
892   Filters.push_back(F);
893   BestIndex = 0; // Sole Filter instance to choose from.
894   bestFilter().recurse();
895 }
896
897 // reportRegion is a helper function for filterProcessor to mark a region as
898 // eligible for use as a filter region.
899 void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
900     unsigned BitIndex, bool AllowMixed) {
901   if (RA == ATTR_MIXED && AllowMixed)
902     Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true));
903   else if (RA == ATTR_ALL_SET && !AllowMixed)
904     Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false));
905 }
906
907 // FilterProcessor scans the well-known encoding bits of the instructions and
908 // builds up a list of candidate filters.  It chooses the best filter and
909 // recursively descends down the decoding tree.
910 bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
911   Filters.clear();
912   BestIndex = -1;
913   unsigned numInstructions = Opcodes.size();
914
915   assert(numInstructions && "Filter created with no instructions");
916
917   // No further filtering is necessary.
918   if (numInstructions == 1)
919     return true;
920
921   // Heuristics.  See also doFilter()'s "Heuristics" comment when num of
922   // instructions is 3.
923   if (AllowMixed && !Greedy) {
924     assert(numInstructions == 3);
925
926     for (unsigned i = 0; i < Opcodes.size(); ++i) {
927       std::vector<unsigned> StartBits;
928       std::vector<unsigned> EndBits;
929       std::vector<uint64_t> FieldVals;
930       insn_t Insn;
931
932       insnWithID(Insn, Opcodes[i]);
933
934       // Look for islands of undecoded bits of any instruction.
935       if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
936         // Found an instruction with island(s).  Now just assign a filter.
937         runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1,
938                         true);
939         return true;
940       }
941     }
942   }
943
944   unsigned BitIndex, InsnIndex;
945
946   // We maintain BIT_WIDTH copies of the bitAttrs automaton.
947   // The automaton consumes the corresponding bit from each
948   // instruction.
949   //
950   //   Input symbols: 0, 1, and _ (unset).
951   //   States:        NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
952   //   Initial state: NONE.
953   //
954   // (NONE) ------- [01] -> (ALL_SET)
955   // (NONE) ------- _ ----> (ALL_UNSET)
956   // (ALL_SET) ---- [01] -> (ALL_SET)
957   // (ALL_SET) ---- _ ----> (MIXED)
958   // (ALL_UNSET) -- [01] -> (MIXED)
959   // (ALL_UNSET) -- _ ----> (ALL_UNSET)
960   // (MIXED) ------ . ----> (MIXED)
961   // (FILTERED)---- . ----> (FILTERED)
962
963   bitAttr_t bitAttrs[BIT_WIDTH];
964
965   // FILTERED bit positions provide no entropy and are not worthy of pursuing.
966   // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
967   for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex)
968     if (FilterBitValues[BitIndex] == BIT_TRUE ||
969         FilterBitValues[BitIndex] == BIT_FALSE)
970       bitAttrs[BitIndex] = ATTR_FILTERED;
971     else
972       bitAttrs[BitIndex] = ATTR_NONE;
973
974   for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
975     insn_t insn;
976
977     insnWithID(insn, Opcodes[InsnIndex]);
978
979     for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex) {
980       switch (bitAttrs[BitIndex]) {
981       case ATTR_NONE:
982         if (insn[BitIndex] == BIT_UNSET)
983           bitAttrs[BitIndex] = ATTR_ALL_UNSET;
984         else
985           bitAttrs[BitIndex] = ATTR_ALL_SET;
986         break;
987       case ATTR_ALL_SET:
988         if (insn[BitIndex] == BIT_UNSET)
989           bitAttrs[BitIndex] = ATTR_MIXED;
990         break;
991       case ATTR_ALL_UNSET:
992         if (insn[BitIndex] != BIT_UNSET)
993           bitAttrs[BitIndex] = ATTR_MIXED;
994         break;
995       case ATTR_MIXED:
996       case ATTR_FILTERED:
997         break;
998       }
999     }
1000   }
1001
1002   // The regionAttr automaton consumes the bitAttrs automatons' state,
1003   // lowest-to-highest.
1004   //
1005   //   Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1006   //   States:        NONE, ALL_SET, MIXED
1007   //   Initial state: NONE
1008   //
1009   // (NONE) ----- F --> (NONE)
1010   // (NONE) ----- S --> (ALL_SET)     ; and set region start
1011   // (NONE) ----- U --> (NONE)
1012   // (NONE) ----- M --> (MIXED)       ; and set region start
1013   // (ALL_SET) -- F --> (NONE)        ; and report an ALL_SET region
1014   // (ALL_SET) -- S --> (ALL_SET)
1015   // (ALL_SET) -- U --> (NONE)        ; and report an ALL_SET region
1016   // (ALL_SET) -- M --> (MIXED)       ; and report an ALL_SET region
1017   // (MIXED) ---- F --> (NONE)        ; and report a MIXED region
1018   // (MIXED) ---- S --> (ALL_SET)     ; and report a MIXED region
1019   // (MIXED) ---- U --> (NONE)        ; and report a MIXED region
1020   // (MIXED) ---- M --> (MIXED)
1021
1022   bitAttr_t RA = ATTR_NONE;
1023   unsigned StartBit = 0;
1024
1025   for (BitIndex = 0; BitIndex < BIT_WIDTH; BitIndex++) {
1026     bitAttr_t bitAttr = bitAttrs[BitIndex];
1027
1028     assert(bitAttr != ATTR_NONE && "Bit without attributes");
1029
1030     switch (RA) {
1031     case ATTR_NONE:
1032       switch (bitAttr) {
1033       case ATTR_FILTERED:
1034         break;
1035       case ATTR_ALL_SET:
1036         StartBit = BitIndex;
1037         RA = ATTR_ALL_SET;
1038         break;
1039       case ATTR_ALL_UNSET:
1040         break;
1041       case ATTR_MIXED:
1042         StartBit = BitIndex;
1043         RA = ATTR_MIXED;
1044         break;
1045       default:
1046         assert(0 && "Unexpected bitAttr!");
1047       }
1048       break;
1049     case ATTR_ALL_SET:
1050       switch (bitAttr) {
1051       case ATTR_FILTERED:
1052         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1053         RA = ATTR_NONE;
1054         break;
1055       case ATTR_ALL_SET:
1056         break;
1057       case ATTR_ALL_UNSET:
1058         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1059         RA = ATTR_NONE;
1060         break;
1061       case ATTR_MIXED:
1062         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1063         StartBit = BitIndex;
1064         RA = ATTR_MIXED;
1065         break;
1066       default:
1067         assert(0 && "Unexpected bitAttr!");
1068       }
1069       break;
1070     case ATTR_MIXED:
1071       switch (bitAttr) {
1072       case ATTR_FILTERED:
1073         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1074         StartBit = BitIndex;
1075         RA = ATTR_NONE;
1076         break;
1077       case ATTR_ALL_SET:
1078         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1079         StartBit = BitIndex;
1080         RA = ATTR_ALL_SET;
1081         break;
1082       case ATTR_ALL_UNSET:
1083         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1084         RA = ATTR_NONE;
1085         break;
1086       case ATTR_MIXED:
1087         break;
1088       default:
1089         assert(0 && "Unexpected bitAttr!");
1090       }
1091       break;
1092     case ATTR_ALL_UNSET:
1093       assert(0 && "regionAttr state machine has no ATTR_UNSET state");
1094     case ATTR_FILTERED:
1095       assert(0 && "regionAttr state machine has no ATTR_FILTERED state");
1096     }
1097   }
1098
1099   // At the end, if we're still in ALL_SET or MIXED states, report a region
1100   switch (RA) {
1101   case ATTR_NONE:
1102     break;
1103   case ATTR_FILTERED:
1104     break;
1105   case ATTR_ALL_SET:
1106     reportRegion(RA, StartBit, BitIndex, AllowMixed);
1107     break;
1108   case ATTR_ALL_UNSET:
1109     break;
1110   case ATTR_MIXED:
1111     reportRegion(RA, StartBit, BitIndex, AllowMixed);
1112     break;
1113   }
1114
1115   // We have finished with the filter processings.  Now it's time to choose
1116   // the best performing filter.
1117   BestIndex = 0;
1118   bool AllUseless = true;
1119   unsigned BestScore = 0;
1120
1121   for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1122     unsigned Usefulness = Filters[i].usefulness();
1123
1124     if (Usefulness)
1125       AllUseless = false;
1126
1127     if (Usefulness > BestScore) {
1128       BestIndex = i;
1129       BestScore = Usefulness;
1130     }
1131   }
1132
1133   if (!AllUseless)
1134     bestFilter().recurse();
1135
1136   return !AllUseless;
1137 } // end of FilterChooser::filterProcessor(bool)
1138
1139 // Decides on the best configuration of filter(s) to use in order to decode
1140 // the instructions.  A conflict of instructions may occur, in which case we
1141 // dump the conflict set to the standard error.
1142 void FilterChooser::doFilter() {
1143   unsigned Num = Opcodes.size();
1144   assert(Num && "FilterChooser created with no instructions");
1145
1146   // Try regions of consecutive known bit values first.
1147   if (filterProcessor(false))
1148     return;
1149
1150   // Then regions of mixed bits (both known and unitialized bit values allowed).
1151   if (filterProcessor(true))
1152     return;
1153
1154   // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1155   // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1156   // well-known encoding pattern.  In such case, we backtrack and scan for the
1157   // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1158   if (Num == 3 && filterProcessor(true, false))
1159     return;
1160
1161   // If we come to here, the instruction decoding has failed.
1162   // Set the BestIndex to -1 to indicate so.
1163   BestIndex = -1;
1164 }
1165
1166 // Emits code to decode our share of instructions.  Returns true if the
1167 // emitted code causes a return, which occurs if we know how to decode
1168 // the instruction at this level or the instruction is not decodeable.
1169 bool FilterChooser::emit(raw_ostream &o, unsigned &Indentation) {
1170   if (Opcodes.size() == 1)
1171     // There is only one instruction in the set, which is great!
1172     // Call emitSingletonDecoder() to see whether there are any remaining
1173     // encodings bits.
1174     return emitSingletonDecoder(o, Indentation, Opcodes[0]);
1175
1176   // Choose the best filter to do the decodings!
1177   if (BestIndex != -1) {
1178     Filter &Best = bestFilter();
1179     if (Best.getNumFiltered() == 1)
1180       emitSingletonDecoder(o, Indentation, Best);
1181     else
1182       bestFilter().emit(o, Indentation);
1183     return false;
1184   }
1185
1186   // We don't know how to decode these instructions!  Return 0 and dump the
1187   // conflict set!
1188   o.indent(Indentation) << "return 0;" << " // Conflict set: ";
1189   for (int i = 0, N = Opcodes.size(); i < N; ++i) {
1190     o << nameWithID(Opcodes[i]);
1191     if (i < (N - 1))
1192       o << ", ";
1193     else
1194       o << '\n';
1195   }
1196
1197   // Print out useful conflict information for postmortem analysis.
1198   errs() << "Decoding Conflict:\n";
1199
1200   dumpStack(errs(), "\t\t");
1201
1202   for (unsigned i = 0; i < Opcodes.size(); i++) {
1203     const std::string &Name = nameWithID(Opcodes[i]);
1204
1205     errs() << '\t' << Name << " ";
1206     dumpBits(errs(),
1207              getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1208     errs() << '\n';
1209   }
1210
1211   return true;
1212 }
1213
1214 bool FixedLenDecoderEmitter::populateInstruction(const CodeGenInstruction &CGI,
1215                                                  unsigned Opc){
1216   const Record &Def = *CGI.TheDef;
1217   // If all the bit positions are not specified; do not decode this instruction.
1218   // We are bound to fail!  For proper disassembly, the well-known encoding bits
1219   // of the instruction must be fully specified.
1220   //
1221   // This also removes pseudo instructions from considerations of disassembly,
1222   // which is a better design and less fragile than the name matchings.
1223   BitsInit &Bits = getBitsField(Def, "Inst");
1224   if (Bits.allInComplete()) return false;
1225
1226   // Ignore "asm parser only" instructions.
1227   if (Def.getValueAsBit("isAsmParserOnly"))
1228     return false;
1229
1230   std::vector<OperandInfo> InsnOperands;
1231
1232   // If the instruction has specified a custom decoding hook, use that instead
1233   // of trying to auto-generate the decoder.
1234   std::string InstDecoder = Def.getValueAsString("DecoderMethod");
1235   if (InstDecoder != "") {
1236     InsnOperands.push_back(OperandInfo(~0U, ~0U, InstDecoder));
1237     Operands[Opc] = InsnOperands;
1238     return true;
1239   }
1240
1241   // Generate a description of the operand of the instruction that we know
1242   // how to decode automatically.
1243   // FIXME: We'll need to have a way to manually override this as needed.
1244
1245   // Gather the outputs/inputs of the instruction, so we can find their
1246   // positions in the encoding.  This assumes for now that they appear in the
1247   // MCInst in the order that they're listed.
1248   std::vector<std::pair<Init*, std::string> > InOutOperands;
1249   DagInit *Out  = Def.getValueAsDag("OutOperandList");
1250   DagInit *In  = Def.getValueAsDag("InOperandList");
1251   for (unsigned i = 0; i < Out->getNumArgs(); ++i)
1252     InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i)));
1253   for (unsigned i = 0; i < In->getNumArgs(); ++i)
1254     InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i)));
1255
1256   // For each operand, see if we can figure out where it is encoded.
1257   for (std::vector<std::pair<Init*, std::string> >::iterator
1258        NI = InOutOperands.begin(), NE = InOutOperands.end(); NI != NE; ++NI) {
1259     unsigned PrevBit = ~0;
1260     unsigned Base = ~0;
1261     unsigned PrevPos = ~0;
1262     std::string Decoder = "";
1263
1264     for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
1265       VarBitInit *BI = dynamic_cast<VarBitInit*>(Bits.getBit(bi));
1266       if (!BI) continue;
1267
1268       VarInit *Var = dynamic_cast<VarInit*>(BI->getVariable());
1269       assert(Var);
1270       unsigned CurrBit = BI->getBitNum();
1271       if (Var->getName() != NI->second) continue;
1272
1273       // Figure out the lowest bit of the value, and the width of the field.
1274       // Deliberately don't try to handle cases where the field is scattered,
1275       // or where not all bits of the the field are explicit.
1276       if (Base == ~0U && PrevBit == ~0U && PrevPos == ~0U) {
1277         if (CurrBit == 0)
1278           Base = bi;
1279         else
1280           continue;
1281       }
1282
1283       if ((PrevPos != ~0U && bi-1 != PrevPos) ||
1284           (CurrBit != ~0U && CurrBit-1 != PrevBit)) {
1285         PrevBit = ~0;
1286         Base = ~0;
1287         PrevPos = ~0;
1288       }
1289
1290       PrevPos = bi;
1291       PrevBit = CurrBit;
1292
1293       // At this point, we can locate the field, but we need to know how to
1294       // interpret it.  As a first step, require the target to provide callbacks
1295       // for decoding register classes.
1296       // FIXME: This need to be extended to handle instructions with custom
1297       // decoder methods, and operands with (simple) MIOperandInfo's.
1298       TypedInit *TI = dynamic_cast<TypedInit*>(NI->first);
1299       RecordRecTy *Type = dynamic_cast<RecordRecTy*>(TI->getType());
1300       Record *TypeRecord = Type->getRecord();
1301       bool isReg = false;
1302       if (TypeRecord->isSubClassOf("RegisterClass")) {
1303         Decoder = "Decode" + Type->getRecord()->getName() + "RegisterClass";
1304         isReg = true;
1305       }
1306
1307       RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
1308       StringInit *String = DecoderString ?
1309         dynamic_cast<StringInit*>(DecoderString->getValue()) :
1310         0;
1311       if (!isReg && String && String->getValue() != "")
1312         Decoder = String->getValue();
1313     }
1314
1315     if (Base != ~0U) {
1316       InsnOperands.push_back(OperandInfo(Base, PrevBit+1, Decoder));
1317       DEBUG(errs() << "ENCODED OPERAND: $" << NI->second << " @ ("
1318                    << utostr(Base+PrevBit) << ", " << utostr(Base) << ")\n");
1319     }
1320   }
1321
1322   Operands[Opc] = InsnOperands;
1323
1324
1325 #if 0
1326   DEBUG({
1327       // Dumps the instruction encoding bits.
1328       dumpBits(errs(), Bits);
1329
1330       errs() << '\n';
1331
1332       // Dumps the list of operand info.
1333       for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1334         const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
1335         const std::string &OperandName = Info.Name;
1336         const Record &OperandDef = *Info.Rec;
1337
1338         errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
1339       }
1340     });
1341 #endif
1342
1343   return true;
1344 }
1345
1346 void FixedLenDecoderEmitter::populateInstructions() {
1347   for (unsigned i = 0, e = NumberedInstructions.size(); i < e; ++i) {
1348     Record *R = NumberedInstructions[i]->TheDef;
1349     if (R->getValueAsString("Namespace") == "TargetOpcode")
1350       continue;
1351
1352     if (populateInstruction(*NumberedInstructions[i], i))
1353       Opcodes.push_back(i);
1354   }
1355 }
1356
1357 // Emits disassembler code for instruction decoding.
1358 void FixedLenDecoderEmitter::run(raw_ostream &o)
1359 {
1360   o << "#include \"llvm/MC/MCInst.h\"\n";
1361   o << "#include \"llvm/Support/DataTypes.h\"\n";
1362   o << "#include <assert.h>\n";
1363   o << '\n';
1364   o << "namespace llvm {\n\n";
1365
1366   NumberedInstructions = Target.getInstructionsByEnumValue();
1367   populateInstructions();
1368   FilterChooser FC(NumberedInstructions, Opcodes, Operands);
1369   FC.emitTop(o, 0);
1370
1371   o << "\n} // End llvm namespace \n";
1372 }