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