blob: 19e86db41a7037bd742d3f5c964c4b0aa621b192 [file] [log] [blame]
Owen Andersond8c87882011-02-18 21:51:29 +00001//===------------ 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"
Peter Collingbourne7c788882011-10-01 16:41:13 +000019#include "llvm/TableGen/Record.h"
James Molloy3015dfb2012-02-09 10:56:31 +000020#include "llvm/ADT/APInt.h"
Owen Andersond8c87882011-02-18 21:51:29 +000021#include "llvm/ADT/StringExtras.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
24
25#include <vector>
26#include <map>
27#include <string>
28
29using namespace llvm;
30
31// The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
32// for a bit value.
33//
34// BIT_UNFILTERED is used as the init value for a filter position. It is used
35// only for filter processings.
36typedef enum {
37 BIT_TRUE, // '1'
38 BIT_FALSE, // '0'
39 BIT_UNSET, // '?'
40 BIT_UNFILTERED // unfiltered
41} bit_value_t;
42
43static bool ValueSet(bit_value_t V) {
44 return (V == BIT_TRUE || V == BIT_FALSE);
45}
46static bool ValueNotSet(bit_value_t V) {
47 return (V == BIT_UNSET);
48}
49static int Value(bit_value_t V) {
50 return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
51}
David Greene05bce0b2011-07-29 22:43:06 +000052static bit_value_t bitFromBits(BitsInit &bits, unsigned index) {
53 if (BitInit *bit = dynamic_cast<BitInit*>(bits.getBit(index)))
Owen Andersond8c87882011-02-18 21:51:29 +000054 return bit->getValue() ? BIT_TRUE : BIT_FALSE;
55
56 // The bit is uninitialized.
57 return BIT_UNSET;
58}
59// Prints the bit value for each position.
David Greene05bce0b2011-07-29 22:43:06 +000060static void dumpBits(raw_ostream &o, BitsInit &bits) {
Owen Andersond8c87882011-02-18 21:51:29 +000061 unsigned index;
62
63 for (index = bits.getNumBits(); index > 0; index--) {
64 switch (bitFromBits(bits, index - 1)) {
65 case BIT_TRUE:
66 o << "1";
67 break;
68 case BIT_FALSE:
69 o << "0";
70 break;
71 case BIT_UNSET:
72 o << "_";
73 break;
74 default:
Craig Topper655b8de2012-02-05 07:21:30 +000075 llvm_unreachable("unexpected return value from bitFromBits");
Owen Andersond8c87882011-02-18 21:51:29 +000076 }
77 }
78}
79
David Greene05bce0b2011-07-29 22:43:06 +000080static BitsInit &getBitsField(const Record &def, const char *str) {
81 BitsInit *bits = def.getValueAsBitsInit(str);
Owen Andersond8c87882011-02-18 21:51:29 +000082 return *bits;
83}
84
85// Forward declaration.
86class FilterChooser;
87
Owen Andersond8c87882011-02-18 21:51:29 +000088// Representation of the instruction to work on.
Owen Andersonf1a00902011-07-19 21:06:00 +000089typedef std::vector<bit_value_t> insn_t;
Owen Andersond8c87882011-02-18 21:51:29 +000090
91/// Filter - Filter works with FilterChooser to produce the decoding tree for
92/// the ISA.
93///
94/// It is useful to think of a Filter as governing the switch stmts of the
95/// decoding tree in a certain level. Each case stmt delegates to an inferior
96/// FilterChooser to decide what further decoding logic to employ, or in another
97/// words, what other remaining bits to look at. The FilterChooser eventually
98/// chooses a best Filter to do its job.
99///
100/// This recursive scheme ends when the number of Opcodes assigned to the
101/// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
102/// the Filter/FilterChooser combo does not know how to distinguish among the
103/// Opcodes assigned.
104///
105/// An example of a conflict is
106///
107/// Conflict:
108/// 111101000.00........00010000....
109/// 111101000.00........0001........
110/// 1111010...00........0001........
111/// 1111010...00....................
112/// 1111010.........................
113/// 1111............................
114/// ................................
115/// VST4q8a 111101000_00________00010000____
116/// VST4q8b 111101000_00________00010000____
117///
118/// The Debug output shows the path that the decoding tree follows to reach the
119/// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
120/// even registers, while VST4q8b is a vst4 to double-spaced odd regsisters.
121///
122/// The encoding info in the .td files does not specify this meta information,
123/// which could have been used by the decoder to resolve the conflict. The
124/// decoder could try to decode the even/odd register numbering and assign to
125/// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
126/// version and return the Opcode since the two have the same Asm format string.
127class Filter {
128protected:
129 FilterChooser *Owner; // points to the FilterChooser who owns this filter
130 unsigned StartBit; // the starting bit position
131 unsigned NumBits; // number of bits to filter
132 bool Mixed; // a mixed region contains both set and unset bits
133
134 // Map of well-known segment value to the set of uid's with that value.
135 std::map<uint64_t, std::vector<unsigned> > FilteredInstructions;
136
137 // Set of uid's with non-constant segment values.
138 std::vector<unsigned> VariableInstructions;
139
140 // Map of well-known segment value to its delegate.
141 std::map<unsigned, FilterChooser*> FilterChooserMap;
142
143 // Number of instructions which fall under FilteredInstructions category.
144 unsigned NumFiltered;
145
146 // Keeps track of the last opcode in the filtered bucket.
147 unsigned LastOpcFiltered;
148
149 // Number of instructions which fall under VariableInstructions category.
150 unsigned NumVariable;
151
152public:
153 unsigned getNumFiltered() { return NumFiltered; }
154 unsigned getNumVariable() { return NumVariable; }
155 unsigned getSingletonOpc() {
156 assert(NumFiltered == 1);
157 return LastOpcFiltered;
158 }
159 // Return the filter chooser for the group of instructions without constant
160 // segment values.
161 FilterChooser &getVariableFC() {
162 assert(NumFiltered == 1);
163 assert(FilterChooserMap.size() == 1);
164 return *(FilterChooserMap.find((unsigned)-1)->second);
165 }
166
167 Filter(const Filter &f);
168 Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
169
170 ~Filter();
171
172 // Divides the decoding task into sub tasks and delegates them to the
173 // inferior FilterChooser's.
174 //
175 // A special case arises when there's only one entry in the filtered
176 // instructions. In order to unambiguously decode the singleton, we need to
177 // match the remaining undecoded encoding bits against the singleton.
178 void recurse();
179
180 // Emit code to decode instructions given a segment or segments of bits.
181 void emit(raw_ostream &o, unsigned &Indentation);
182
183 // Returns the number of fanout produced by the filter. More fanout implies
184 // the filter distinguishes more categories of instructions.
185 unsigned usefulness() const;
186}; // End of class Filter
187
188// These are states of our finite state machines used in FilterChooser's
189// filterProcessor() which produces the filter candidates to use.
190typedef enum {
191 ATTR_NONE,
192 ATTR_FILTERED,
193 ATTR_ALL_SET,
194 ATTR_ALL_UNSET,
195 ATTR_MIXED
196} bitAttr_t;
197
198/// FilterChooser - FilterChooser chooses the best filter among a set of Filters
199/// in order to perform the decoding of instructions at the current level.
200///
201/// Decoding proceeds from the top down. Based on the well-known encoding bits
202/// of instructions available, FilterChooser builds up the possible Filters that
203/// can further the task of decoding by distinguishing among the remaining
204/// candidate instructions.
205///
206/// Once a filter has been chosen, it is called upon to divide the decoding task
207/// into sub-tasks and delegates them to its inferior FilterChoosers for further
208/// processings.
209///
210/// It is useful to think of a Filter as governing the switch stmts of the
211/// decoding tree. And each case is delegated to an inferior FilterChooser to
212/// decide what further remaining bits to look at.
213class FilterChooser {
214protected:
215 friend class Filter;
216
217 // Vector of codegen instructions to choose our filter.
218 const std::vector<const CodeGenInstruction*> &AllInstructions;
219
220 // Vector of uid's for this filter chooser to work on.
221 const std::vector<unsigned> Opcodes;
222
223 // Lookup table for the operand decoding of instructions.
224 std::map<unsigned, std::vector<OperandInfo> > &Operands;
225
226 // Vector of candidate filters.
227 std::vector<Filter> Filters;
228
229 // Array of bit values passed down from our parent.
230 // Set to all BIT_UNFILTERED's for Parent == NULL.
Owen Andersonf1a00902011-07-19 21:06:00 +0000231 std::vector<bit_value_t> FilterBitValues;
Owen Andersond8c87882011-02-18 21:51:29 +0000232
233 // Links to the FilterChooser above us in the decoding tree.
234 FilterChooser *Parent;
235
236 // Index of the best filter from Filters.
237 int BestIndex;
238
Owen Andersonf1a00902011-07-19 21:06:00 +0000239 // Width of instructions
240 unsigned BitWidth;
241
Owen Anderson83e3f672011-08-17 17:44:15 +0000242 // Parent emitter
243 const FixedLenDecoderEmitter *Emitter;
244
Owen Andersond8c87882011-02-18 21:51:29 +0000245public:
246 FilterChooser(const FilterChooser &FC) :
247 AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
Owen Andersonf1a00902011-07-19 21:06:00 +0000248 Operands(FC.Operands), Filters(FC.Filters),
249 FilterBitValues(FC.FilterBitValues), Parent(FC.Parent),
Owen Anderson83e3f672011-08-17 17:44:15 +0000250 BestIndex(FC.BestIndex), BitWidth(FC.BitWidth),
251 Emitter(FC.Emitter) { }
Owen Andersond8c87882011-02-18 21:51:29 +0000252
253 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
254 const std::vector<unsigned> &IDs,
Owen Andersonf1a00902011-07-19 21:06:00 +0000255 std::map<unsigned, std::vector<OperandInfo> > &Ops,
Owen Anderson83e3f672011-08-17 17:44:15 +0000256 unsigned BW,
257 const FixedLenDecoderEmitter *E) :
Owen Andersond8c87882011-02-18 21:51:29 +0000258 AllInstructions(Insts), Opcodes(IDs), Operands(Ops), Filters(),
Owen Anderson83e3f672011-08-17 17:44:15 +0000259 Parent(NULL), BestIndex(-1), BitWidth(BW), Emitter(E) {
Owen Andersonf1a00902011-07-19 21:06:00 +0000260 for (unsigned i = 0; i < BitWidth; ++i)
261 FilterBitValues.push_back(BIT_UNFILTERED);
Owen Andersond8c87882011-02-18 21:51:29 +0000262
263 doFilter();
264 }
265
266 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
267 const std::vector<unsigned> &IDs,
268 std::map<unsigned, std::vector<OperandInfo> > &Ops,
Owen Andersonf1a00902011-07-19 21:06:00 +0000269 std::vector<bit_value_t> &ParentFilterBitValues,
Owen Andersond8c87882011-02-18 21:51:29 +0000270 FilterChooser &parent) :
271 AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
Owen Andersonf1a00902011-07-19 21:06:00 +0000272 Filters(), FilterBitValues(ParentFilterBitValues),
Owen Anderson83e3f672011-08-17 17:44:15 +0000273 Parent(&parent), BestIndex(-1), BitWidth(parent.BitWidth),
274 Emitter(parent.Emitter) {
Owen Andersond8c87882011-02-18 21:51:29 +0000275 doFilter();
276 }
277
278 // The top level filter chooser has NULL as its parent.
279 bool isTopLevel() { return Parent == NULL; }
280
281 // Emit the top level typedef and decodeInstruction() function.
Owen Andersonf1a00902011-07-19 21:06:00 +0000282 void emitTop(raw_ostream &o, unsigned Indentation, std::string Namespace);
Owen Andersond8c87882011-02-18 21:51:29 +0000283
284protected:
285 // Populates the insn given the uid.
286 void insnWithID(insn_t &Insn, unsigned Opcode) const {
David Greene05bce0b2011-07-29 22:43:06 +0000287 BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
Owen Andersond8c87882011-02-18 21:51:29 +0000288
James Molloy3015dfb2012-02-09 10:56:31 +0000289 // We may have a SoftFail bitmask, which specifies a mask where an encoding
290 // may differ from the value in "Inst" and yet still be valid, but the
291 // disassembler should return SoftFail instead of Success.
292 //
293 // This is used for marking UNPREDICTABLE instructions in the ARM world.
Jim Grosbach9c826d22012-02-29 22:07:56 +0000294 BitsInit *SFBits =
295 AllInstructions[Opcode]->TheDef->getValueAsBitsInit("SoftFail");
James Molloy3015dfb2012-02-09 10:56:31 +0000296
297 for (unsigned i = 0; i < BitWidth; ++i) {
298 if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE)
299 Insn.push_back(BIT_UNSET);
300 else
301 Insn.push_back(bitFromBits(Bits, i));
302 }
Owen Andersond8c87882011-02-18 21:51:29 +0000303 }
304
305 // Returns the record name.
306 const std::string &nameWithID(unsigned Opcode) const {
307 return AllInstructions[Opcode]->TheDef->getName();
308 }
309
310 // Populates the field of the insn given the start position and the number of
311 // consecutive bits to scan for.
312 //
313 // Returns false if there exists any uninitialized bit value in the range.
314 // Returns true, otherwise.
315 bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
316 unsigned NumBits) const;
317
318 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
319 /// filter array as a series of chars.
Owen Andersonf1a00902011-07-19 21:06:00 +0000320 void dumpFilterArray(raw_ostream &o, std::vector<bit_value_t> & filter);
Owen Andersond8c87882011-02-18 21:51:29 +0000321
322 /// dumpStack - dumpStack traverses the filter chooser chain and calls
323 /// dumpFilterArray on each filter chooser up to the top level one.
324 void dumpStack(raw_ostream &o, const char *prefix);
325
326 Filter &bestFilter() {
327 assert(BestIndex != -1 && "BestIndex not set");
328 return Filters[BestIndex];
329 }
330
331 // Called from Filter::recurse() when singleton exists. For debug purpose.
332 void SingletonExists(unsigned Opc);
333
334 bool PositionFiltered(unsigned i) {
335 return ValueSet(FilterBitValues[i]);
336 }
337
338 // Calculates the island(s) needed to decode the instruction.
339 // This returns a lit of undecoded bits of an instructions, for example,
340 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
341 // decoded bits in order to verify that the instruction matches the Opcode.
342 unsigned getIslands(std::vector<unsigned> &StartBits,
343 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
344 insn_t &Insn);
345
James Molloya5d58562011-09-07 19:42:28 +0000346 // Emits code to check the Predicates member of an instruction are true.
347 // Returns true if predicate matches were emitted, false otherwise.
348 bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation,unsigned Opc);
349
James Molloy3015dfb2012-02-09 10:56:31 +0000350 void emitSoftFailCheck(raw_ostream &o, unsigned Indentation, unsigned Opc);
351
Owen Andersond8c87882011-02-18 21:51:29 +0000352 // Emits code to decode the singleton. Return true if we have matched all the
353 // well-known bits.
354 bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc);
355
356 // Emits code to decode the singleton, and then to decode the rest.
357 void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,Filter &Best);
358
Owen Andersond1e38df2011-07-28 21:54:31 +0000359 void emitBinaryParser(raw_ostream &o , unsigned &Indentation,
360 OperandInfo &OpInfo);
361
Owen Andersond8c87882011-02-18 21:51:29 +0000362 // Assign a single filter and run with it.
363 void runSingleFilter(FilterChooser &owner, unsigned startBit, unsigned numBit,
364 bool mixed);
365
366 // reportRegion is a helper function for filterProcessor to mark a region as
367 // eligible for use as a filter region.
368 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
369 bool AllowMixed);
370
371 // FilterProcessor scans the well-known encoding bits of the instructions and
372 // builds up a list of candidate filters. It chooses the best filter and
373 // recursively descends down the decoding tree.
374 bool filterProcessor(bool AllowMixed, bool Greedy = true);
375
376 // Decides on the best configuration of filter(s) to use in order to decode
377 // the instructions. A conflict of instructions may occur, in which case we
378 // dump the conflict set to the standard error.
379 void doFilter();
380
381 // Emits code to decode our share of instructions. Returns true if the
382 // emitted code causes a return, which occurs if we know how to decode
383 // the instruction at this level or the instruction is not decodeable.
384 bool emit(raw_ostream &o, unsigned &Indentation);
385};
386
387///////////////////////////
388// //
389// Filter Implmenetation //
390// //
391///////////////////////////
392
393Filter::Filter(const Filter &f) :
394 Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
395 FilteredInstructions(f.FilteredInstructions),
396 VariableInstructions(f.VariableInstructions),
397 FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered),
398 LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) {
399}
400
401Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
402 bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits),
403 Mixed(mixed) {
Owen Andersonf1a00902011-07-19 21:06:00 +0000404 assert(StartBit + NumBits - 1 < Owner->BitWidth);
Owen Andersond8c87882011-02-18 21:51:29 +0000405
406 NumFiltered = 0;
407 LastOpcFiltered = 0;
408 NumVariable = 0;
409
410 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
411 insn_t Insn;
412
413 // Populates the insn given the uid.
414 Owner->insnWithID(Insn, Owner->Opcodes[i]);
415
416 uint64_t Field;
417 // Scans the segment for possibly well-specified encoding bits.
418 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
419
420 if (ok) {
421 // The encoding bits are well-known. Lets add the uid of the
422 // instruction into the bucket keyed off the constant field value.
423 LastOpcFiltered = Owner->Opcodes[i];
424 FilteredInstructions[Field].push_back(LastOpcFiltered);
425 ++NumFiltered;
426 } else {
427 // Some of the encoding bit(s) are unspecfied. This contributes to
428 // one additional member of "Variable" instructions.
429 VariableInstructions.push_back(Owner->Opcodes[i]);
430 ++NumVariable;
431 }
432 }
433
434 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
435 && "Filter returns no instruction categories");
436}
437
438Filter::~Filter() {
439 std::map<unsigned, FilterChooser*>::iterator filterIterator;
440 for (filterIterator = FilterChooserMap.begin();
441 filterIterator != FilterChooserMap.end();
442 filterIterator++) {
443 delete filterIterator->second;
444 }
445}
446
447// Divides the decoding task into sub tasks and delegates them to the
448// inferior FilterChooser's.
449//
450// A special case arises when there's only one entry in the filtered
451// instructions. In order to unambiguously decode the singleton, we need to
452// match the remaining undecoded encoding bits against the singleton.
453void Filter::recurse() {
454 std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator;
455
Owen Andersond8c87882011-02-18 21:51:29 +0000456 // Starts by inheriting our parent filter chooser's filter bit values.
Owen Andersonf1a00902011-07-19 21:06:00 +0000457 std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
Owen Andersond8c87882011-02-18 21:51:29 +0000458
459 unsigned bitIndex;
460
461 if (VariableInstructions.size()) {
462 // Conservatively marks each segment position as BIT_UNSET.
463 for (bitIndex = 0; bitIndex < NumBits; bitIndex++)
464 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
465
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000466 // Delegates to an inferior filter chooser for further processing on this
Owen Andersond8c87882011-02-18 21:51:29 +0000467 // group of instructions whose segment values are variable.
468 FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
469 (unsigned)-1,
470 new FilterChooser(Owner->AllInstructions,
471 VariableInstructions,
472 Owner->Operands,
473 BitValueArray,
474 *Owner)
475 ));
476 }
477
478 // No need to recurse for a singleton filtered instruction.
479 // See also Filter::emit().
480 if (getNumFiltered() == 1) {
481 //Owner->SingletonExists(LastOpcFiltered);
482 assert(FilterChooserMap.size() == 1);
483 return;
484 }
485
486 // Otherwise, create sub choosers.
487 for (mapIterator = FilteredInstructions.begin();
488 mapIterator != FilteredInstructions.end();
489 mapIterator++) {
490
491 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
492 for (bitIndex = 0; bitIndex < NumBits; bitIndex++) {
493 if (mapIterator->first & (1ULL << bitIndex))
494 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
495 else
496 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
497 }
498
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000499 // Delegates to an inferior filter chooser for further processing on this
Owen Andersond8c87882011-02-18 21:51:29 +0000500 // category of instructions.
501 FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
502 mapIterator->first,
503 new FilterChooser(Owner->AllInstructions,
504 mapIterator->second,
505 Owner->Operands,
506 BitValueArray,
507 *Owner)
508 ));
509 }
510}
511
512// Emit code to decode instructions given a segment or segments of bits.
513void Filter::emit(raw_ostream &o, unsigned &Indentation) {
514 o.indent(Indentation) << "// Check Inst{";
515
516 if (NumBits > 1)
517 o << (StartBit + NumBits - 1) << '-';
518
519 o << StartBit << "} ...\n";
520
Owen Andersonf1a00902011-07-19 21:06:00 +0000521 o.indent(Indentation) << "switch (fieldFromInstruction" << Owner->BitWidth
522 << "(insn, " << StartBit << ", "
523 << NumBits << ")) {\n";
Owen Andersond8c87882011-02-18 21:51:29 +0000524
525 std::map<unsigned, FilterChooser*>::iterator filterIterator;
526
527 bool DefaultCase = false;
528 for (filterIterator = FilterChooserMap.begin();
529 filterIterator != FilterChooserMap.end();
530 filterIterator++) {
531
532 // Field value -1 implies a non-empty set of variable instructions.
533 // See also recurse().
534 if (filterIterator->first == (unsigned)-1) {
535 DefaultCase = true;
536
537 o.indent(Indentation) << "default:\n";
538 o.indent(Indentation) << " break; // fallthrough\n";
539
540 // Closing curly brace for the switch statement.
541 // This is unconventional because we want the default processing to be
542 // performed for the fallthrough cases as well, i.e., when the "cases"
543 // did not prove a decoded instruction.
544 o.indent(Indentation) << "}\n";
545
546 } else
547 o.indent(Indentation) << "case " << filterIterator->first << ":\n";
548
549 // We arrive at a category of instructions with the same segment value.
550 // Now delegate to the sub filter chooser for further decodings.
551 // The case may fallthrough, which happens if the remaining well-known
552 // encoding bits do not match exactly.
553 if (!DefaultCase) { ++Indentation; ++Indentation; }
554
555 bool finished = filterIterator->second->emit(o, Indentation);
556 // For top level default case, there's no need for a break statement.
557 if (Owner->isTopLevel() && DefaultCase)
558 break;
559 if (!finished)
560 o.indent(Indentation) << "break;\n";
561
562 if (!DefaultCase) { --Indentation; --Indentation; }
563 }
564
565 // If there is no default case, we still need to supply a closing brace.
566 if (!DefaultCase) {
567 // Closing curly brace for the switch statement.
568 o.indent(Indentation) << "}\n";
569 }
570}
571
572// Returns the number of fanout produced by the filter. More fanout implies
573// the filter distinguishes more categories of instructions.
574unsigned Filter::usefulness() const {
575 if (VariableInstructions.size())
576 return FilteredInstructions.size();
577 else
578 return FilteredInstructions.size() + 1;
579}
580
581//////////////////////////////////
582// //
583// Filterchooser Implementation //
584// //
585//////////////////////////////////
586
587// Emit the top level typedef and decodeInstruction() function.
Owen Andersonf1a00902011-07-19 21:06:00 +0000588void FilterChooser::emitTop(raw_ostream &o, unsigned Indentation,
589 std::string Namespace) {
Owen Andersond8c87882011-02-18 21:51:29 +0000590 o.indent(Indentation) <<
Jim Grosbach9c826d22012-02-29 22:07:56 +0000591 "static MCDisassembler::DecodeStatus decode" << Namespace << "Instruction"
592 << BitWidth << "(MCInst &MI, uint" << BitWidth
593 << "_t insn, uint64_t Address, "
James Molloya5d58562011-09-07 19:42:28 +0000594 << "const void *Decoder, const MCSubtargetInfo &STI) {\n";
Owen Anderson684dfcf2011-10-17 16:56:47 +0000595 o.indent(Indentation) << " unsigned tmp = 0;\n";
596 o.indent(Indentation) << " (void)tmp;\n";
597 o.indent(Indentation) << Emitter->Locals << "\n";
Bob Wilson1cea66c2011-10-01 02:47:54 +0000598 o.indent(Indentation) << " uint64_t Bits = STI.getFeatureBits();\n";
Owen Anderson684dfcf2011-10-17 16:56:47 +0000599 o.indent(Indentation) << " (void)Bits;\n";
Owen Andersond8c87882011-02-18 21:51:29 +0000600
601 ++Indentation; ++Indentation;
602 // Emits code to decode the instructions.
603 emit(o, Indentation);
604
605 o << '\n';
Owen Anderson83e3f672011-08-17 17:44:15 +0000606 o.indent(Indentation) << "return " << Emitter->ReturnFail << ";\n";
Owen Andersond8c87882011-02-18 21:51:29 +0000607 --Indentation; --Indentation;
608
609 o.indent(Indentation) << "}\n";
610
611 o << '\n';
612}
613
614// Populates the field of the insn given the start position and the number of
615// consecutive bits to scan for.
616//
617// Returns false if and on the first uninitialized bit value encountered.
618// Returns true, otherwise.
619bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
620 unsigned StartBit, unsigned NumBits) const {
621 Field = 0;
622
623 for (unsigned i = 0; i < NumBits; ++i) {
624 if (Insn[StartBit + i] == BIT_UNSET)
625 return false;
626
627 if (Insn[StartBit + i] == BIT_TRUE)
628 Field = Field | (1ULL << i);
629 }
630
631 return true;
632}
633
634/// dumpFilterArray - dumpFilterArray prints out debugging info for the given
635/// filter array as a series of chars.
636void FilterChooser::dumpFilterArray(raw_ostream &o,
Owen Andersonf1a00902011-07-19 21:06:00 +0000637 std::vector<bit_value_t> &filter) {
Owen Andersond8c87882011-02-18 21:51:29 +0000638 unsigned bitIndex;
639
Owen Andersonf1a00902011-07-19 21:06:00 +0000640 for (bitIndex = BitWidth; bitIndex > 0; bitIndex--) {
Owen Andersond8c87882011-02-18 21:51:29 +0000641 switch (filter[bitIndex - 1]) {
642 case BIT_UNFILTERED:
643 o << ".";
644 break;
645 case BIT_UNSET:
646 o << "_";
647 break;
648 case BIT_TRUE:
649 o << "1";
650 break;
651 case BIT_FALSE:
652 o << "0";
653 break;
654 }
655 }
656}
657
658/// dumpStack - dumpStack traverses the filter chooser chain and calls
659/// dumpFilterArray on each filter chooser up to the top level one.
660void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) {
661 FilterChooser *current = this;
662
663 while (current) {
664 o << prefix;
665 dumpFilterArray(o, current->FilterBitValues);
666 o << '\n';
667 current = current->Parent;
668 }
669}
670
671// Called from Filter::recurse() when singleton exists. For debug purpose.
672void FilterChooser::SingletonExists(unsigned Opc) {
673 insn_t Insn0;
674 insnWithID(Insn0, Opc);
675
676 errs() << "Singleton exists: " << nameWithID(Opc)
677 << " with its decoding dominating ";
678 for (unsigned i = 0; i < Opcodes.size(); ++i) {
679 if (Opcodes[i] == Opc) continue;
680 errs() << nameWithID(Opcodes[i]) << ' ';
681 }
682 errs() << '\n';
683
684 dumpStack(errs(), "\t\t");
685 for (unsigned i = 0; i < Opcodes.size(); i++) {
686 const std::string &Name = nameWithID(Opcodes[i]);
687
688 errs() << '\t' << Name << " ";
689 dumpBits(errs(),
690 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
691 errs() << '\n';
692 }
693}
694
695// Calculates the island(s) needed to decode the instruction.
696// This returns a list of undecoded bits of an instructions, for example,
697// Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
698// decoded bits in order to verify that the instruction matches the Opcode.
699unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
700 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
701 insn_t &Insn) {
702 unsigned Num, BitNo;
703 Num = BitNo = 0;
704
705 uint64_t FieldVal = 0;
706
707 // 0: Init
708 // 1: Water (the bit value does not affect decoding)
709 // 2: Island (well-known bit value needed for decoding)
710 int State = 0;
711 int Val = -1;
712
Owen Andersonf1a00902011-07-19 21:06:00 +0000713 for (unsigned i = 0; i < BitWidth; ++i) {
Owen Andersond8c87882011-02-18 21:51:29 +0000714 Val = Value(Insn[i]);
715 bool Filtered = PositionFiltered(i);
716 switch (State) {
Craig Topper655b8de2012-02-05 07:21:30 +0000717 default: llvm_unreachable("Unreachable code!");
Owen Andersond8c87882011-02-18 21:51:29 +0000718 case 0:
719 case 1:
720 if (Filtered || Val == -1)
721 State = 1; // Still in Water
722 else {
723 State = 2; // Into the Island
724 BitNo = 0;
725 StartBits.push_back(i);
726 FieldVal = Val;
727 }
728 break;
729 case 2:
730 if (Filtered || Val == -1) {
731 State = 1; // Into the Water
732 EndBits.push_back(i - 1);
733 FieldVals.push_back(FieldVal);
734 ++Num;
735 } else {
736 State = 2; // Still in Island
737 ++BitNo;
738 FieldVal = FieldVal | Val << BitNo;
739 }
740 break;
741 }
742 }
743 // If we are still in Island after the loop, do some housekeeping.
744 if (State == 2) {
Owen Andersonf1a00902011-07-19 21:06:00 +0000745 EndBits.push_back(BitWidth - 1);
Owen Andersond8c87882011-02-18 21:51:29 +0000746 FieldVals.push_back(FieldVal);
747 ++Num;
748 }
749
750 assert(StartBits.size() == Num && EndBits.size() == Num &&
751 FieldVals.size() == Num);
752 return Num;
753}
754
Owen Andersond1e38df2011-07-28 21:54:31 +0000755void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
756 OperandInfo &OpInfo) {
757 std::string &Decoder = OpInfo.Decoder;
758
759 if (OpInfo.numFields() == 1) {
760 OperandInfo::iterator OI = OpInfo.begin();
761 o.indent(Indentation) << " tmp = fieldFromInstruction" << BitWidth
762 << "(insn, " << OI->Base << ", " << OI->Width
763 << ");\n";
764 } else {
765 o.indent(Indentation) << " tmp = 0;\n";
766 for (OperandInfo::iterator OI = OpInfo.begin(), OE = OpInfo.end();
767 OI != OE; ++OI) {
768 o.indent(Indentation) << " tmp |= (fieldFromInstruction" << BitWidth
Andrew Tricked968a92011-09-08 05:23:14 +0000769 << "(insn, " << OI->Base << ", " << OI->Width
Owen Andersond1e38df2011-07-28 21:54:31 +0000770 << ") << " << OI->Offset << ");\n";
771 }
772 }
773
774 if (Decoder != "")
Owen Anderson83e3f672011-08-17 17:44:15 +0000775 o.indent(Indentation) << " " << Emitter->GuardPrefix << Decoder
Jim Grosbach9c826d22012-02-29 22:07:56 +0000776 << "(MI, tmp, Address, Decoder)"
777 << Emitter->GuardPostfix << "\n";
Owen Andersond1e38df2011-07-28 21:54:31 +0000778 else
779 o.indent(Indentation) << " MI.addOperand(MCOperand::CreateImm(tmp));\n";
780
781}
782
James Molloya5d58562011-09-07 19:42:28 +0000783static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
784 std::string PredicateNamespace) {
Andrew Trick22b4c812011-09-08 05:25:49 +0000785 if (str[0] == '!')
786 o << "!(Bits & " << PredicateNamespace << "::"
787 << str.slice(1,str.size()) << ")";
James Molloya5d58562011-09-07 19:42:28 +0000788 else
Andrew Trick22b4c812011-09-08 05:25:49 +0000789 o << "(Bits & " << PredicateNamespace << "::" << str << ")";
James Molloya5d58562011-09-07 19:42:28 +0000790}
791
792bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
793 unsigned Opc) {
Jim Grosbach9c826d22012-02-29 22:07:56 +0000794 ListInit *Predicates =
795 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
James Molloya5d58562011-09-07 19:42:28 +0000796 for (unsigned i = 0; i < Predicates->getSize(); ++i) {
797 Record *Pred = Predicates->getElementAsRecord(i);
798 if (!Pred->getValue("AssemblerMatcherPredicate"))
799 continue;
800
801 std::string P = Pred->getValueAsString("AssemblerCondString");
802
803 if (!P.length())
804 continue;
805
806 if (i != 0)
807 o << " && ";
808
809 StringRef SR(P);
810 std::pair<StringRef, StringRef> pairs = SR.split(',');
811 while (pairs.second.size()) {
812 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
813 o << " && ";
814 pairs = pairs.second.split(',');
815 }
816 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
817 }
818 return Predicates->getSize() > 0;
Andrew Tricked968a92011-09-08 05:23:14 +0000819}
James Molloya5d58562011-09-07 19:42:28 +0000820
Jim Grosbach9c826d22012-02-29 22:07:56 +0000821void FilterChooser::emitSoftFailCheck(raw_ostream &o, unsigned Indentation,
822 unsigned Opc) {
823 BitsInit *SFBits =
824 AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail");
James Molloy3015dfb2012-02-09 10:56:31 +0000825 if (!SFBits) return;
826 BitsInit *InstBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("Inst");
827
828 APInt PositiveMask(BitWidth, 0ULL);
829 APInt NegativeMask(BitWidth, 0ULL);
830 for (unsigned i = 0; i < BitWidth; ++i) {
831 bit_value_t B = bitFromBits(*SFBits, i);
832 bit_value_t IB = bitFromBits(*InstBits, i);
833
834 if (B != BIT_TRUE) continue;
835
836 switch (IB) {
837 case BIT_FALSE:
838 // The bit is meant to be false, so emit a check to see if it is true.
839 PositiveMask.setBit(i);
840 break;
841 case BIT_TRUE:
842 // The bit is meant to be true, so emit a check to see if it is false.
843 NegativeMask.setBit(i);
844 break;
845 default:
846 // The bit is not set; this must be an error!
847 StringRef Name = AllInstructions[Opc]->TheDef->getName();
848 errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in "
849 << Name
850 << " is set but Inst{" << i <<"} is unset!\n"
851 << " - You can only mark a bit as SoftFail if it is fully defined"
852 << " (1/0 - not '?') in Inst\n";
853 o << "#error SoftFail Conflict, " << Name << "::SoftFail{" << i
854 << "} set but Inst{" << i << "} undefined!\n";
855 }
856 }
857
858 bool NeedPositiveMask = PositiveMask.getBoolValue();
859 bool NeedNegativeMask = NegativeMask.getBoolValue();
860
861 if (!NeedPositiveMask && !NeedNegativeMask)
862 return;
863
864 std::string PositiveMaskStr = PositiveMask.toString(16, /*signed=*/false);
865 std::string NegativeMaskStr = NegativeMask.toString(16, /*signed=*/false);
866 StringRef BitExt = "";
867 if (BitWidth > 32)
868 BitExt = "ULL";
869
870 o.indent(Indentation) << "if (";
871 if (NeedPositiveMask)
872 o << "insn & 0x" << PositiveMaskStr << BitExt;
873 if (NeedPositiveMask && NeedNegativeMask)
874 o << " || ";
875 if (NeedNegativeMask)
876 o << "~insn & 0x" << NegativeMaskStr << BitExt;
877 o << ")\n";
878 o.indent(Indentation+2) << "S = MCDisassembler::SoftFail;\n";
879}
880
Owen Andersond8c87882011-02-18 21:51:29 +0000881// Emits code to decode the singleton. Return true if we have matched all the
882// well-known bits.
883bool FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
884 unsigned Opc) {
885 std::vector<unsigned> StartBits;
886 std::vector<unsigned> EndBits;
887 std::vector<uint64_t> FieldVals;
888 insn_t Insn;
889 insnWithID(Insn, Opc);
890
891 // Look for islands of undecoded bits of the singleton.
892 getIslands(StartBits, EndBits, FieldVals, Insn);
893
894 unsigned Size = StartBits.size();
895 unsigned I, NumBits;
896
897 // If we have matched all the well-known bits, just issue a return.
898 if (Size == 0) {
James Molloya5d58562011-09-07 19:42:28 +0000899 o.indent(Indentation) << "if (";
Eli Friedman64a17b32011-09-08 21:00:31 +0000900 if (!emitPredicateMatch(o, Indentation, Opc))
901 o << "1";
James Molloya5d58562011-09-07 19:42:28 +0000902 o << ") {\n";
James Molloy3015dfb2012-02-09 10:56:31 +0000903 emitSoftFailCheck(o, Indentation+2, Opc);
Owen Andersond8c87882011-02-18 21:51:29 +0000904 o.indent(Indentation) << " MI.setOpcode(" << Opc << ");\n";
905 std::vector<OperandInfo>& InsnOperands = Operands[Opc];
906 for (std::vector<OperandInfo>::iterator
907 I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
908 // If a custom instruction decoder was specified, use that.
Owen Andersond1e38df2011-07-28 21:54:31 +0000909 if (I->numFields() == 0 && I->Decoder.size()) {
Owen Anderson83e3f672011-08-17 17:44:15 +0000910 o.indent(Indentation) << " " << Emitter->GuardPrefix << I->Decoder
Jim Grosbach9c826d22012-02-29 22:07:56 +0000911 << "(MI, insn, Address, Decoder)"
912 << Emitter->GuardPostfix << "\n";
Owen Andersond8c87882011-02-18 21:51:29 +0000913 break;
914 }
915
Owen Andersond1e38df2011-07-28 21:54:31 +0000916 emitBinaryParser(o, Indentation, *I);
Owen Andersond8c87882011-02-18 21:51:29 +0000917 }
918
Jim Grosbach9c826d22012-02-29 22:07:56 +0000919 o.indent(Indentation) << " return " << Emitter->ReturnOK << "; // "
920 << nameWithID(Opc) << '\n';
James Molloya5d58562011-09-07 19:42:28 +0000921 o.indent(Indentation) << "}\n"; // Closing predicate block.
Owen Andersond8c87882011-02-18 21:51:29 +0000922 return true;
923 }
924
925 // Otherwise, there are more decodings to be done!
926
927 // Emit code to match the island(s) for the singleton.
928 o.indent(Indentation) << "// Check ";
929
930 for (I = Size; I != 0; --I) {
931 o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} ";
932 if (I > 1)
James Molloya5d58562011-09-07 19:42:28 +0000933 o << " && ";
Owen Andersond8c87882011-02-18 21:51:29 +0000934 else
935 o << "for singleton decoding...\n";
936 }
937
938 o.indent(Indentation) << "if (";
James Molloy0d76b192011-09-08 08:12:01 +0000939 if (emitPredicateMatch(o, Indentation, Opc)) {
James Molloya5d58562011-09-07 19:42:28 +0000940 o << " &&\n";
941 o.indent(Indentation+4);
942 }
Owen Andersond8c87882011-02-18 21:51:29 +0000943
944 for (I = Size; I != 0; --I) {
945 NumBits = EndBits[I-1] - StartBits[I-1] + 1;
Owen Andersonf1a00902011-07-19 21:06:00 +0000946 o << "fieldFromInstruction" << BitWidth << "(insn, "
947 << StartBits[I-1] << ", " << NumBits
Owen Andersond8c87882011-02-18 21:51:29 +0000948 << ") == " << FieldVals[I-1];
949 if (I > 1)
950 o << " && ";
951 else
952 o << ") {\n";
953 }
James Molloy3015dfb2012-02-09 10:56:31 +0000954 emitSoftFailCheck(o, Indentation+2, Opc);
Owen Andersond8c87882011-02-18 21:51:29 +0000955 o.indent(Indentation) << " MI.setOpcode(" << Opc << ");\n";
956 std::vector<OperandInfo>& InsnOperands = Operands[Opc];
957 for (std::vector<OperandInfo>::iterator
958 I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
959 // If a custom instruction decoder was specified, use that.
Owen Andersond1e38df2011-07-28 21:54:31 +0000960 if (I->numFields() == 0 && I->Decoder.size()) {
Owen Anderson83e3f672011-08-17 17:44:15 +0000961 o.indent(Indentation) << " " << Emitter->GuardPrefix << I->Decoder
Jim Grosbach9c826d22012-02-29 22:07:56 +0000962 << "(MI, insn, Address, Decoder)"
963 << Emitter->GuardPostfix << "\n";
Owen Andersond8c87882011-02-18 21:51:29 +0000964 break;
965 }
966
Owen Andersond1e38df2011-07-28 21:54:31 +0000967 emitBinaryParser(o, Indentation, *I);
Owen Andersond8c87882011-02-18 21:51:29 +0000968 }
Jim Grosbach9c826d22012-02-29 22:07:56 +0000969 o.indent(Indentation) << " return " << Emitter->ReturnOK << "; // "
970 << nameWithID(Opc) << '\n';
Owen Andersond8c87882011-02-18 21:51:29 +0000971 o.indent(Indentation) << "}\n";
972
973 return false;
974}
975
976// Emits code to decode the singleton, and then to decode the rest.
977void FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
978 Filter &Best) {
979
980 unsigned Opc = Best.getSingletonOpc();
981
982 emitSingletonDecoder(o, Indentation, Opc);
983
984 // Emit code for the rest.
985 o.indent(Indentation) << "else\n";
986
987 Indentation += 2;
988 Best.getVariableFC().emit(o, Indentation);
989 Indentation -= 2;
990}
991
992// Assign a single filter and run with it. Top level API client can initialize
993// with a single filter to start the filtering process.
994void FilterChooser::runSingleFilter(FilterChooser &owner, unsigned startBit,
995 unsigned numBit, bool mixed) {
996 Filters.clear();
997 Filter F(*this, startBit, numBit, true);
998 Filters.push_back(F);
999 BestIndex = 0; // Sole Filter instance to choose from.
1000 bestFilter().recurse();
1001}
1002
1003// reportRegion is a helper function for filterProcessor to mark a region as
1004// eligible for use as a filter region.
1005void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1006 unsigned BitIndex, bool AllowMixed) {
1007 if (RA == ATTR_MIXED && AllowMixed)
1008 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true));
1009 else if (RA == ATTR_ALL_SET && !AllowMixed)
1010 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false));
1011}
1012
1013// FilterProcessor scans the well-known encoding bits of the instructions and
1014// builds up a list of candidate filters. It chooses the best filter and
1015// recursively descends down the decoding tree.
1016bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1017 Filters.clear();
1018 BestIndex = -1;
1019 unsigned numInstructions = Opcodes.size();
1020
1021 assert(numInstructions && "Filter created with no instructions");
1022
1023 // No further filtering is necessary.
1024 if (numInstructions == 1)
1025 return true;
1026
1027 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1028 // instructions is 3.
1029 if (AllowMixed && !Greedy) {
1030 assert(numInstructions == 3);
1031
1032 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1033 std::vector<unsigned> StartBits;
1034 std::vector<unsigned> EndBits;
1035 std::vector<uint64_t> FieldVals;
1036 insn_t Insn;
1037
1038 insnWithID(Insn, Opcodes[i]);
1039
1040 // Look for islands of undecoded bits of any instruction.
1041 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1042 // Found an instruction with island(s). Now just assign a filter.
1043 runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1,
1044 true);
1045 return true;
1046 }
1047 }
1048 }
1049
1050 unsigned BitIndex, InsnIndex;
1051
1052 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1053 // The automaton consumes the corresponding bit from each
1054 // instruction.
1055 //
1056 // Input symbols: 0, 1, and _ (unset).
1057 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1058 // Initial state: NONE.
1059 //
1060 // (NONE) ------- [01] -> (ALL_SET)
1061 // (NONE) ------- _ ----> (ALL_UNSET)
1062 // (ALL_SET) ---- [01] -> (ALL_SET)
1063 // (ALL_SET) ---- _ ----> (MIXED)
1064 // (ALL_UNSET) -- [01] -> (MIXED)
1065 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1066 // (MIXED) ------ . ----> (MIXED)
1067 // (FILTERED)---- . ----> (FILTERED)
1068
Owen Andersonf1a00902011-07-19 21:06:00 +00001069 std::vector<bitAttr_t> bitAttrs;
Owen Andersond8c87882011-02-18 21:51:29 +00001070
1071 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1072 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
Owen Andersonf1a00902011-07-19 21:06:00 +00001073 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
Owen Andersond8c87882011-02-18 21:51:29 +00001074 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1075 FilterBitValues[BitIndex] == BIT_FALSE)
Owen Andersonf1a00902011-07-19 21:06:00 +00001076 bitAttrs.push_back(ATTR_FILTERED);
Owen Andersond8c87882011-02-18 21:51:29 +00001077 else
Owen Andersonf1a00902011-07-19 21:06:00 +00001078 bitAttrs.push_back(ATTR_NONE);
Owen Andersond8c87882011-02-18 21:51:29 +00001079
1080 for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
1081 insn_t insn;
1082
1083 insnWithID(insn, Opcodes[InsnIndex]);
1084
Owen Andersonf1a00902011-07-19 21:06:00 +00001085 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
Owen Andersond8c87882011-02-18 21:51:29 +00001086 switch (bitAttrs[BitIndex]) {
1087 case ATTR_NONE:
1088 if (insn[BitIndex] == BIT_UNSET)
1089 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1090 else
1091 bitAttrs[BitIndex] = ATTR_ALL_SET;
1092 break;
1093 case ATTR_ALL_SET:
1094 if (insn[BitIndex] == BIT_UNSET)
1095 bitAttrs[BitIndex] = ATTR_MIXED;
1096 break;
1097 case ATTR_ALL_UNSET:
1098 if (insn[BitIndex] != BIT_UNSET)
1099 bitAttrs[BitIndex] = ATTR_MIXED;
1100 break;
1101 case ATTR_MIXED:
1102 case ATTR_FILTERED:
1103 break;
1104 }
1105 }
1106 }
1107
1108 // The regionAttr automaton consumes the bitAttrs automatons' state,
1109 // lowest-to-highest.
1110 //
1111 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1112 // States: NONE, ALL_SET, MIXED
1113 // Initial state: NONE
1114 //
1115 // (NONE) ----- F --> (NONE)
1116 // (NONE) ----- S --> (ALL_SET) ; and set region start
1117 // (NONE) ----- U --> (NONE)
1118 // (NONE) ----- M --> (MIXED) ; and set region start
1119 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1120 // (ALL_SET) -- S --> (ALL_SET)
1121 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1122 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1123 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1124 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1125 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1126 // (MIXED) ---- M --> (MIXED)
1127
1128 bitAttr_t RA = ATTR_NONE;
1129 unsigned StartBit = 0;
1130
Owen Andersonf1a00902011-07-19 21:06:00 +00001131 for (BitIndex = 0; BitIndex < BitWidth; BitIndex++) {
Owen Andersond8c87882011-02-18 21:51:29 +00001132 bitAttr_t bitAttr = bitAttrs[BitIndex];
1133
1134 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1135
1136 switch (RA) {
1137 case ATTR_NONE:
1138 switch (bitAttr) {
1139 case ATTR_FILTERED:
1140 break;
1141 case ATTR_ALL_SET:
1142 StartBit = BitIndex;
1143 RA = ATTR_ALL_SET;
1144 break;
1145 case ATTR_ALL_UNSET:
1146 break;
1147 case ATTR_MIXED:
1148 StartBit = BitIndex;
1149 RA = ATTR_MIXED;
1150 break;
1151 default:
Craig Topper655b8de2012-02-05 07:21:30 +00001152 llvm_unreachable("Unexpected bitAttr!");
Owen Andersond8c87882011-02-18 21:51:29 +00001153 }
1154 break;
1155 case ATTR_ALL_SET:
1156 switch (bitAttr) {
1157 case ATTR_FILTERED:
1158 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1159 RA = ATTR_NONE;
1160 break;
1161 case ATTR_ALL_SET:
1162 break;
1163 case ATTR_ALL_UNSET:
1164 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1165 RA = ATTR_NONE;
1166 break;
1167 case ATTR_MIXED:
1168 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1169 StartBit = BitIndex;
1170 RA = ATTR_MIXED;
1171 break;
1172 default:
Craig Topper655b8de2012-02-05 07:21:30 +00001173 llvm_unreachable("Unexpected bitAttr!");
Owen Andersond8c87882011-02-18 21:51:29 +00001174 }
1175 break;
1176 case ATTR_MIXED:
1177 switch (bitAttr) {
1178 case ATTR_FILTERED:
1179 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1180 StartBit = BitIndex;
1181 RA = ATTR_NONE;
1182 break;
1183 case ATTR_ALL_SET:
1184 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1185 StartBit = BitIndex;
1186 RA = ATTR_ALL_SET;
1187 break;
1188 case ATTR_ALL_UNSET:
1189 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1190 RA = ATTR_NONE;
1191 break;
1192 case ATTR_MIXED:
1193 break;
1194 default:
Craig Topper655b8de2012-02-05 07:21:30 +00001195 llvm_unreachable("Unexpected bitAttr!");
Owen Andersond8c87882011-02-18 21:51:29 +00001196 }
1197 break;
1198 case ATTR_ALL_UNSET:
Craig Topper655b8de2012-02-05 07:21:30 +00001199 llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
Owen Andersond8c87882011-02-18 21:51:29 +00001200 case ATTR_FILTERED:
Craig Topper655b8de2012-02-05 07:21:30 +00001201 llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
Owen Andersond8c87882011-02-18 21:51:29 +00001202 }
1203 }
1204
1205 // At the end, if we're still in ALL_SET or MIXED states, report a region
1206 switch (RA) {
1207 case ATTR_NONE:
1208 break;
1209 case ATTR_FILTERED:
1210 break;
1211 case ATTR_ALL_SET:
1212 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1213 break;
1214 case ATTR_ALL_UNSET:
1215 break;
1216 case ATTR_MIXED:
1217 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1218 break;
1219 }
1220
1221 // We have finished with the filter processings. Now it's time to choose
1222 // the best performing filter.
1223 BestIndex = 0;
1224 bool AllUseless = true;
1225 unsigned BestScore = 0;
1226
1227 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1228 unsigned Usefulness = Filters[i].usefulness();
1229
1230 if (Usefulness)
1231 AllUseless = false;
1232
1233 if (Usefulness > BestScore) {
1234 BestIndex = i;
1235 BestScore = Usefulness;
1236 }
1237 }
1238
1239 if (!AllUseless)
1240 bestFilter().recurse();
1241
1242 return !AllUseless;
1243} // end of FilterChooser::filterProcessor(bool)
1244
1245// Decides on the best configuration of filter(s) to use in order to decode
1246// the instructions. A conflict of instructions may occur, in which case we
1247// dump the conflict set to the standard error.
1248void FilterChooser::doFilter() {
1249 unsigned Num = Opcodes.size();
1250 assert(Num && "FilterChooser created with no instructions");
1251
1252 // Try regions of consecutive known bit values first.
1253 if (filterProcessor(false))
1254 return;
1255
1256 // Then regions of mixed bits (both known and unitialized bit values allowed).
1257 if (filterProcessor(true))
1258 return;
1259
1260 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1261 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1262 // well-known encoding pattern. In such case, we backtrack and scan for the
1263 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1264 if (Num == 3 && filterProcessor(true, false))
1265 return;
1266
1267 // If we come to here, the instruction decoding has failed.
1268 // Set the BestIndex to -1 to indicate so.
1269 BestIndex = -1;
1270}
1271
1272// Emits code to decode our share of instructions. Returns true if the
1273// emitted code causes a return, which occurs if we know how to decode
1274// the instruction at this level or the instruction is not decodeable.
1275bool FilterChooser::emit(raw_ostream &o, unsigned &Indentation) {
1276 if (Opcodes.size() == 1)
1277 // There is only one instruction in the set, which is great!
1278 // Call emitSingletonDecoder() to see whether there are any remaining
1279 // encodings bits.
1280 return emitSingletonDecoder(o, Indentation, Opcodes[0]);
1281
1282 // Choose the best filter to do the decodings!
1283 if (BestIndex != -1) {
1284 Filter &Best = bestFilter();
1285 if (Best.getNumFiltered() == 1)
1286 emitSingletonDecoder(o, Indentation, Best);
1287 else
1288 bestFilter().emit(o, Indentation);
1289 return false;
1290 }
1291
1292 // We don't know how to decode these instructions! Return 0 and dump the
1293 // conflict set!
1294 o.indent(Indentation) << "return 0;" << " // Conflict set: ";
1295 for (int i = 0, N = Opcodes.size(); i < N; ++i) {
1296 o << nameWithID(Opcodes[i]);
1297 if (i < (N - 1))
1298 o << ", ";
1299 else
1300 o << '\n';
1301 }
1302
1303 // Print out useful conflict information for postmortem analysis.
1304 errs() << "Decoding Conflict:\n";
1305
1306 dumpStack(errs(), "\t\t");
1307
1308 for (unsigned i = 0; i < Opcodes.size(); i++) {
1309 const std::string &Name = nameWithID(Opcodes[i]);
1310
1311 errs() << '\t' << Name << " ";
1312 dumpBits(errs(),
1313 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1314 errs() << '\n';
1315 }
1316
1317 return true;
1318}
1319
Owen Andersonf1a00902011-07-19 21:06:00 +00001320static bool populateInstruction(const CodeGenInstruction &CGI,
1321 unsigned Opc,
1322 std::map<unsigned, std::vector<OperandInfo> >& Operands){
Owen Andersond8c87882011-02-18 21:51:29 +00001323 const Record &Def = *CGI.TheDef;
1324 // If all the bit positions are not specified; do not decode this instruction.
1325 // We are bound to fail! For proper disassembly, the well-known encoding bits
1326 // of the instruction must be fully specified.
1327 //
1328 // This also removes pseudo instructions from considerations of disassembly,
1329 // which is a better design and less fragile than the name matchings.
Owen Andersond8c87882011-02-18 21:51:29 +00001330 // Ignore "asm parser only" instructions.
Owen Anderson4dd27eb2011-03-14 20:58:49 +00001331 if (Def.getValueAsBit("isAsmParserOnly") ||
1332 Def.getValueAsBit("isCodeGenOnly"))
Owen Andersond8c87882011-02-18 21:51:29 +00001333 return false;
1334
David Greene05bce0b2011-07-29 22:43:06 +00001335 BitsInit &Bits = getBitsField(Def, "Inst");
Jim Grosbach806fcc02011-07-06 21:33:38 +00001336 if (Bits.allInComplete()) return false;
1337
Owen Andersond8c87882011-02-18 21:51:29 +00001338 std::vector<OperandInfo> InsnOperands;
1339
1340 // If the instruction has specified a custom decoding hook, use that instead
1341 // of trying to auto-generate the decoder.
1342 std::string InstDecoder = Def.getValueAsString("DecoderMethod");
1343 if (InstDecoder != "") {
Owen Andersond1e38df2011-07-28 21:54:31 +00001344 InsnOperands.push_back(OperandInfo(InstDecoder));
Owen Andersond8c87882011-02-18 21:51:29 +00001345 Operands[Opc] = InsnOperands;
1346 return true;
1347 }
1348
1349 // Generate a description of the operand of the instruction that we know
1350 // how to decode automatically.
1351 // FIXME: We'll need to have a way to manually override this as needed.
1352
1353 // Gather the outputs/inputs of the instruction, so we can find their
1354 // positions in the encoding. This assumes for now that they appear in the
1355 // MCInst in the order that they're listed.
David Greene05bce0b2011-07-29 22:43:06 +00001356 std::vector<std::pair<Init*, std::string> > InOutOperands;
1357 DagInit *Out = Def.getValueAsDag("OutOperandList");
1358 DagInit *In = Def.getValueAsDag("InOperandList");
Owen Andersond8c87882011-02-18 21:51:29 +00001359 for (unsigned i = 0; i < Out->getNumArgs(); ++i)
1360 InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i)));
1361 for (unsigned i = 0; i < In->getNumArgs(); ++i)
1362 InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i)));
1363
Owen Anderson00ef6e32011-07-28 23:56:20 +00001364 // Search for tied operands, so that we can correctly instantiate
1365 // operands that are not explicitly represented in the encoding.
Owen Andersonea242982011-07-29 18:28:52 +00001366 std::map<std::string, std::string> TiedNames;
Owen Anderson00ef6e32011-07-28 23:56:20 +00001367 for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
1368 int tiedTo = CGI.Operands[i].getTiedRegister();
Owen Andersonea242982011-07-29 18:28:52 +00001369 if (tiedTo != -1) {
1370 TiedNames[InOutOperands[i].second] = InOutOperands[tiedTo].second;
1371 TiedNames[InOutOperands[tiedTo].second] = InOutOperands[i].second;
1372 }
Owen Anderson00ef6e32011-07-28 23:56:20 +00001373 }
1374
Owen Andersond8c87882011-02-18 21:51:29 +00001375 // For each operand, see if we can figure out where it is encoded.
David Greene05bce0b2011-07-29 22:43:06 +00001376 for (std::vector<std::pair<Init*, std::string> >::iterator
Owen Andersond8c87882011-02-18 21:51:29 +00001377 NI = InOutOperands.begin(), NE = InOutOperands.end(); NI != NE; ++NI) {
Owen Andersond8c87882011-02-18 21:51:29 +00001378 std::string Decoder = "";
1379
Owen Andersond1e38df2011-07-28 21:54:31 +00001380 // At this point, we can locate the field, but we need to know how to
1381 // interpret it. As a first step, require the target to provide callbacks
1382 // for decoding register classes.
1383 // FIXME: This need to be extended to handle instructions with custom
1384 // decoder methods, and operands with (simple) MIOperandInfo's.
David Greene05bce0b2011-07-29 22:43:06 +00001385 TypedInit *TI = dynamic_cast<TypedInit*>(NI->first);
Owen Andersond1e38df2011-07-28 21:54:31 +00001386 RecordRecTy *Type = dynamic_cast<RecordRecTy*>(TI->getType());
1387 Record *TypeRecord = Type->getRecord();
1388 bool isReg = false;
1389 if (TypeRecord->isSubClassOf("RegisterOperand"))
1390 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1391 if (TypeRecord->isSubClassOf("RegisterClass")) {
1392 Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
1393 isReg = true;
1394 }
1395
1396 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
David Greene05bce0b2011-07-29 22:43:06 +00001397 StringInit *String = DecoderString ?
1398 dynamic_cast<StringInit*>(DecoderString->getValue()) : 0;
Owen Andersond1e38df2011-07-28 21:54:31 +00001399 if (!isReg && String && String->getValue() != "")
1400 Decoder = String->getValue();
1401
1402 OperandInfo OpInfo(Decoder);
1403 unsigned Base = ~0U;
1404 unsigned Width = 0;
1405 unsigned Offset = 0;
1406
Owen Andersond8c87882011-02-18 21:51:29 +00001407 for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
Owen Andersoncf603952011-08-01 22:45:43 +00001408 VarInit *Var = 0;
David Greene05bce0b2011-07-29 22:43:06 +00001409 VarBitInit *BI = dynamic_cast<VarBitInit*>(Bits.getBit(bi));
Owen Andersoncf603952011-08-01 22:45:43 +00001410 if (BI)
1411 Var = dynamic_cast<VarInit*>(BI->getVariable());
1412 else
1413 Var = dynamic_cast<VarInit*>(Bits.getBit(bi));
1414
1415 if (!Var) {
Owen Andersond1e38df2011-07-28 21:54:31 +00001416 if (Base != ~0U) {
1417 OpInfo.addField(Base, Width, Offset);
1418 Base = ~0U;
1419 Width = 0;
1420 Offset = 0;
1421 }
1422 continue;
1423 }
Owen Andersond8c87882011-02-18 21:51:29 +00001424
Owen Anderson00ef6e32011-07-28 23:56:20 +00001425 if (Var->getName() != NI->second &&
Owen Andersonea242982011-07-29 18:28:52 +00001426 Var->getName() != TiedNames[NI->second]) {
Owen Andersond1e38df2011-07-28 21:54:31 +00001427 if (Base != ~0U) {
1428 OpInfo.addField(Base, Width, Offset);
1429 Base = ~0U;
1430 Width = 0;
1431 Offset = 0;
1432 }
1433 continue;
Owen Andersond8c87882011-02-18 21:51:29 +00001434 }
1435
Owen Andersond1e38df2011-07-28 21:54:31 +00001436 if (Base == ~0U) {
1437 Base = bi;
1438 Width = 1;
Owen Andersoncf603952011-08-01 22:45:43 +00001439 Offset = BI ? BI->getBitNum() : 0;
1440 } else if (BI && BI->getBitNum() != Offset + Width) {
Owen Andersoneb809f52011-07-29 23:01:18 +00001441 OpInfo.addField(Base, Width, Offset);
1442 Base = bi;
1443 Width = 1;
1444 Offset = BI->getBitNum();
Owen Andersond1e38df2011-07-28 21:54:31 +00001445 } else {
1446 ++Width;
Owen Andersond8c87882011-02-18 21:51:29 +00001447 }
Owen Andersond8c87882011-02-18 21:51:29 +00001448 }
1449
Owen Andersond1e38df2011-07-28 21:54:31 +00001450 if (Base != ~0U)
1451 OpInfo.addField(Base, Width, Offset);
1452
1453 if (OpInfo.numFields() > 0)
1454 InsnOperands.push_back(OpInfo);
Owen Andersond8c87882011-02-18 21:51:29 +00001455 }
1456
1457 Operands[Opc] = InsnOperands;
1458
1459
1460#if 0
1461 DEBUG({
1462 // Dumps the instruction encoding bits.
1463 dumpBits(errs(), Bits);
1464
1465 errs() << '\n';
1466
1467 // Dumps the list of operand info.
1468 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1469 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
1470 const std::string &OperandName = Info.Name;
1471 const Record &OperandDef = *Info.Rec;
1472
1473 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
1474 }
1475 });
1476#endif
1477
1478 return true;
1479}
1480
Owen Andersonf1a00902011-07-19 21:06:00 +00001481static void emitHelper(llvm::raw_ostream &o, unsigned BitWidth) {
1482 unsigned Indentation = 0;
1483 std::string WidthStr = "uint" + utostr(BitWidth) + "_t";
Owen Andersond8c87882011-02-18 21:51:29 +00001484
Owen Andersonf1a00902011-07-19 21:06:00 +00001485 o << '\n';
1486
1487 o.indent(Indentation) << "static " << WidthStr <<
1488 " fieldFromInstruction" << BitWidth <<
1489 "(" << WidthStr <<" insn, unsigned startBit, unsigned numBits)\n";
1490
1491 o.indent(Indentation) << "{\n";
1492
1493 ++Indentation; ++Indentation;
1494 o.indent(Indentation) << "assert(startBit + numBits <= " << BitWidth
1495 << " && \"Instruction field out of bounds!\");\n";
1496 o << '\n';
1497 o.indent(Indentation) << WidthStr << " fieldMask;\n";
1498 o << '\n';
1499 o.indent(Indentation) << "if (numBits == " << BitWidth << ")\n";
1500
1501 ++Indentation; ++Indentation;
1502 o.indent(Indentation) << "fieldMask = (" << WidthStr << ")-1;\n";
1503 --Indentation; --Indentation;
1504
1505 o.indent(Indentation) << "else\n";
1506
1507 ++Indentation; ++Indentation;
1508 o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n";
1509 --Indentation; --Indentation;
1510
1511 o << '\n';
1512 o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n";
1513 --Indentation; --Indentation;
1514
1515 o.indent(Indentation) << "}\n";
1516
1517 o << '\n';
Owen Andersond8c87882011-02-18 21:51:29 +00001518}
1519
1520// Emits disassembler code for instruction decoding.
1521void FixedLenDecoderEmitter::run(raw_ostream &o)
1522{
1523 o << "#include \"llvm/MC/MCInst.h\"\n";
1524 o << "#include \"llvm/Support/DataTypes.h\"\n";
1525 o << "#include <assert.h>\n";
1526 o << '\n';
1527 o << "namespace llvm {\n\n";
1528
Owen Andersonf1a00902011-07-19 21:06:00 +00001529 // Parameterize the decoders based on namespace and instruction width.
Owen Andersond8c87882011-02-18 21:51:29 +00001530 NumberedInstructions = Target.getInstructionsByEnumValue();
Owen Andersonf1a00902011-07-19 21:06:00 +00001531 std::map<std::pair<std::string, unsigned>,
1532 std::vector<unsigned> > OpcMap;
1533 std::map<unsigned, std::vector<OperandInfo> > Operands;
1534
1535 for (unsigned i = 0; i < NumberedInstructions.size(); ++i) {
1536 const CodeGenInstruction *Inst = NumberedInstructions[i];
1537 Record *Def = Inst->TheDef;
1538 unsigned Size = Def->getValueAsInt("Size");
1539 if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
1540 Def->getValueAsBit("isPseudo") ||
1541 Def->getValueAsBit("isAsmParserOnly") ||
1542 Def->getValueAsBit("isCodeGenOnly"))
1543 continue;
1544
1545 std::string DecoderNamespace = Def->getValueAsString("DecoderNamespace");
1546
1547 if (Size) {
1548 if (populateInstruction(*Inst, i, Operands)) {
1549 OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
1550 }
1551 }
1552 }
1553
1554 std::set<unsigned> Sizes;
1555 for (std::map<std::pair<std::string, unsigned>,
1556 std::vector<unsigned> >::iterator
1557 I = OpcMap.begin(), E = OpcMap.end(); I != E; ++I) {
1558 // If we haven't visited this instruction width before, emit the
1559 // helper method to extract fields.
1560 if (!Sizes.count(I->first.second)) {
1561 emitHelper(o, 8*I->first.second);
1562 Sizes.insert(I->first.second);
1563 }
1564
1565 // Emit the decoder for this namespace+width combination.
1566 FilterChooser FC(NumberedInstructions, I->second, Operands,
Owen Anderson83e3f672011-08-17 17:44:15 +00001567 8*I->first.second, this);
Owen Andersonf1a00902011-07-19 21:06:00 +00001568 FC.emitTop(o, 0, I->first.first);
1569 }
Owen Andersond8c87882011-02-18 21:51:29 +00001570
1571 o << "\n} // End llvm namespace \n";
1572}