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