1 //===------------ FixedLenDecoderEmitter.cpp - Decoder Generator ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // It contains the tablegen backend that emits the decoder functions for
11 // targets with fixed length instruction set.
13 //===----------------------------------------------------------------------===//
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"
36 #define DEBUG_TYPE "decoder-emitter"
39 struct EncodingField {
40 unsigned Base, Width, Offset;
41 EncodingField(unsigned B, unsigned W, unsigned O)
42 : Base(B), Width(W), Offset(O) { }
46 std::vector<EncodingField> Fields;
48 bool HasCompleteDecoder;
50 OperandInfo(std::string D, bool HCD)
51 : Decoder(std::move(D)), HasCompleteDecoder(HCD) {}
53 void addField(unsigned Base, unsigned Width, unsigned Offset) {
54 Fields.push_back(EncodingField(Base, Width, Offset));
57 unsigned numFields() const { return Fields.size(); }
59 typedef std::vector<EncodingField>::const_iterator const_iterator;
61 const_iterator begin() const { return Fields.begin(); }
62 const_iterator end() const { return Fields.end(); }
65 typedef std::vector<uint8_t> DecoderTable;
66 typedef uint32_t DecoderFixup;
67 typedef std::vector<DecoderFixup> FixupList;
68 typedef std::vector<FixupList> FixupScopeList;
69 typedef SmallSetVector<std::string, 16> PredicateSet;
70 typedef SmallSetVector<std::string, 16> DecoderSet;
71 struct DecoderTableInfo {
73 FixupScopeList FixupStack;
74 PredicateSet Predicates;
78 } // End anonymous namespace
81 class FixedLenDecoderEmitter {
82 ArrayRef<const CodeGenInstruction *> NumberedInstructions;
85 // Defaults preserved here for documentation, even though they aren't
86 // strictly necessary given the way that this is currently being called.
87 FixedLenDecoderEmitter(RecordKeeper &R, 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",
93 : Target(R), PredicateNamespace(std::move(PredicateNamespace)),
94 GuardPrefix(std::move(GPrefix)), GuardPostfix(std::move(GPostfix)),
95 ReturnOK(std::move(ROK)), ReturnFail(std::move(RFail)),
96 Locals(std::move(L)) {}
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;
109 // run - Output the code emitter
110 void run(raw_ostream &o);
113 CodeGenTarget Target;
115 std::string PredicateNamespace;
116 std::string GuardPrefix, GuardPostfix;
117 std::string ReturnOK, ReturnFail;
120 } // End anonymous namespace
122 // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
125 // BIT_UNFILTERED is used as the init value for a filter position. It is used
126 // only for filter processings.
131 BIT_UNFILTERED // unfiltered
134 static bool ValueSet(bit_value_t V) {
135 return (V == BIT_TRUE || V == BIT_FALSE);
137 static bool ValueNotSet(bit_value_t V) {
138 return (V == BIT_UNSET);
140 static int Value(bit_value_t V) {
141 return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
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;
147 // The bit is uninitialized.
150 // Prints the bit value for each position.
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)) {
164 llvm_unreachable("unexpected return value from bitFromBits");
169 static BitsInit &getBitsField(const Record &def, const char *str) {
170 BitsInit *bits = def.getValueAsBitsInit(str);
174 // Forward declaration.
177 } // End anonymous namespace
179 // Representation of the instruction to work on.
180 typedef std::vector<bit_value_t> insn_t;
182 /// Filter - Filter works with FilterChooser to produce the decoding tree for
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.
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.
196 /// An example of a conflict is
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____
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.
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.
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
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;
229 // Set of uid's with non-constant segment values.
230 std::vector<unsigned> VariableInstructions;
232 // Map of well-known segment value to its delegate.
233 std::map<unsigned, std::unique_ptr<const FilterChooser>> FilterChooserMap;
235 // Number of instructions which fall under FilteredInstructions category.
236 unsigned NumFiltered;
238 // Keeps track of the last opcode in the filtered bucket.
239 unsigned LastOpcFiltered;
242 unsigned getNumFiltered() const { return NumFiltered; }
243 unsigned getSingletonOpc() const {
244 assert(NumFiltered == 1);
245 return LastOpcFiltered;
247 // Return the filter chooser for the group of instructions without constant
249 const FilterChooser &getVariableFC() const {
250 assert(NumFiltered == 1);
251 assert(FilterChooserMap.size() == 1);
252 return *(FilterChooserMap.find((unsigned)-1)->second);
256 Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
260 // Divides the decoding task into sub tasks and delegates them to the
261 // inferior FilterChooser's.
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.
268 // Emit table entries to decode instructions given a segment or segments of
270 void emitTableEntry(DecoderTableInfo &TableInfo) const;
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
278 // These are states of our finite state machines used in FilterChooser's
279 // filterProcessor() which produces the filter candidates to use.
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.
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.
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
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.
304 class FilterChooser {
308 // Vector of codegen instructions to choose our filter.
309 ArrayRef<const CodeGenInstruction *> AllInstructions;
311 // Vector of uid's for this filter chooser to work on.
312 const std::vector<unsigned> &Opcodes;
314 // Lookup table for the operand decoding of instructions.
315 const std::map<unsigned, std::vector<OperandInfo> > &Operands;
317 // Vector of candidate filters.
318 std::vector<Filter> Filters;
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;
324 // Links to the FilterChooser above us in the decoding tree.
325 const FilterChooser *Parent;
327 // Index of the best filter from Filters.
330 // Width of instructions
334 const FixedLenDecoderEmitter *Emitter;
336 FilterChooser(const FilterChooser &) = delete;
337 void operator=(const FilterChooser &) = delete;
340 FilterChooser(ArrayRef<const CodeGenInstruction *> Insts,
341 const std::vector<unsigned> &IDs,
342 const std::map<unsigned, std::vector<OperandInfo> > &Ops,
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) {
351 FilterChooser(ArrayRef<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) {
363 unsigned getBitWidth() const { return BitWidth; }
366 // Populates the insn given the uid.
367 void insnWithID(insn_t &Insn, unsigned Opcode) const {
368 BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
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.
374 // This is used for marking UNPREDICTABLE instructions in the ARM world.
376 AllInstructions[Opcode]->TheDef->getValueAsBitsInit("SoftFail");
378 for (unsigned i = 0; i < BitWidth; ++i) {
379 if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE)
380 Insn.push_back(BIT_UNSET);
382 Insn.push_back(bitFromBits(Bits, i));
386 // Returns the record name.
387 const std::string &nameWithID(unsigned Opcode) const {
388 return AllInstructions[Opcode]->TheDef->getName();
391 // Populates the field of the insn given the start position and the number of
392 // consecutive bits to scan for.
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;
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;
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;
408 Filter &bestFilter() {
409 assert(BestIndex != -1 && "BestIndex not set");
410 return Filters[BestIndex];
413 bool PositionFiltered(unsigned i) const {
414 return ValueSet(FilterBitValues[i]);
417 // Calculates the island(s) needed to decode the instruction.
418 // This returns a lit of undecoded bits of an instructions, for example,
419 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
420 // decoded bits in order to verify that the instruction matches the Opcode.
421 unsigned getIslands(std::vector<unsigned> &StartBits,
422 std::vector<unsigned> &EndBits,
423 std::vector<uint64_t> &FieldVals,
424 const insn_t &Insn) const;
426 // Emits code to check the Predicates member of an instruction are true.
427 // Returns true if predicate matches were emitted, false otherwise.
428 bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
431 bool doesOpcodeNeedPredicate(unsigned Opc) const;
432 unsigned getPredicateIndex(DecoderTableInfo &TableInfo, StringRef P) const;
433 void emitPredicateTableEntry(DecoderTableInfo &TableInfo,
436 void emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
439 // Emits table entries to decode the singleton.
440 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
443 // Emits code to decode the singleton, and then to decode the rest.
444 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
445 const Filter &Best) const;
447 void emitBinaryParser(raw_ostream &o, unsigned &Indentation,
448 const OperandInfo &OpInfo,
449 bool &OpHasCompleteDecoder) const;
451 void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc,
452 bool &HasCompleteDecoder) const;
453 unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc,
454 bool &HasCompleteDecoder) const;
456 // Assign a single filter and run with it.
457 void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed);
459 // reportRegion is a helper function for filterProcessor to mark a region as
460 // eligible for use as a filter region.
461 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
464 // FilterProcessor scans the well-known encoding bits of the instructions and
465 // builds up a list of candidate filters. It chooses the best filter and
466 // recursively descends down the decoding tree.
467 bool filterProcessor(bool AllowMixed, bool Greedy = true);
469 // Decides on the best configuration of filter(s) to use in order to decode
470 // the instructions. A conflict of instructions may occur, in which case we
471 // dump the conflict set to the standard error.
475 // emitTableEntries - Emit state machine entries to decode our share of
477 void emitTableEntries(DecoderTableInfo &TableInfo) const;
479 } // End anonymous namespace
481 ///////////////////////////
483 // Filter Implementation //
485 ///////////////////////////
487 Filter::Filter(Filter &&f)
488 : Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
489 FilteredInstructions(std::move(f.FilteredInstructions)),
490 VariableInstructions(std::move(f.VariableInstructions)),
491 FilterChooserMap(std::move(f.FilterChooserMap)), NumFiltered(f.NumFiltered),
492 LastOpcFiltered(f.LastOpcFiltered) {
495 Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
497 : Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) {
498 assert(StartBit + NumBits - 1 < Owner->BitWidth);
503 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
506 // Populates the insn given the uid.
507 Owner->insnWithID(Insn, Owner->Opcodes[i]);
510 // Scans the segment for possibly well-specified encoding bits.
511 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
514 // The encoding bits are well-known. Lets add the uid of the
515 // instruction into the bucket keyed off the constant field value.
516 LastOpcFiltered = Owner->Opcodes[i];
517 FilteredInstructions[Field].push_back(LastOpcFiltered);
520 // Some of the encoding bit(s) are unspecified. This contributes to
521 // one additional member of "Variable" instructions.
522 VariableInstructions.push_back(Owner->Opcodes[i]);
526 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
527 && "Filter returns no instruction categories");
533 // Divides the decoding task into sub tasks and delegates them to the
534 // inferior FilterChooser's.
536 // A special case arises when there's only one entry in the filtered
537 // instructions. In order to unambiguously decode the singleton, we need to
538 // match the remaining undecoded encoding bits against the singleton.
539 void Filter::recurse() {
540 // Starts by inheriting our parent filter chooser's filter bit values.
541 std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
543 if (!VariableInstructions.empty()) {
544 // Conservatively marks each segment position as BIT_UNSET.
545 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex)
546 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
548 // Delegates to an inferior filter chooser for further processing on this
549 // group of instructions whose segment values are variable.
550 FilterChooserMap.insert(
551 std::make_pair(-1U, llvm::make_unique<FilterChooser>(
552 Owner->AllInstructions, VariableInstructions,
553 Owner->Operands, BitValueArray, *Owner)));
556 // No need to recurse for a singleton filtered instruction.
557 // See also Filter::emit*().
558 if (getNumFiltered() == 1) {
559 assert(FilterChooserMap.size() == 1);
563 // Otherwise, create sub choosers.
564 for (const auto &Inst : FilteredInstructions) {
566 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
567 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) {
568 if (Inst.first & (1ULL << bitIndex))
569 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
571 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
574 // Delegates to an inferior filter chooser for further processing on this
575 // category of instructions.
576 FilterChooserMap.insert(std::make_pair(
577 Inst.first, llvm::make_unique<FilterChooser>(
578 Owner->AllInstructions, Inst.second,
579 Owner->Operands, BitValueArray, *Owner)));
583 static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
585 // Any NumToSkip fixups in the current scope can resolve to the
587 for (FixupList::const_reverse_iterator I = Fixups.rbegin(),
590 // Calculate the distance from the byte following the fixup entry byte
591 // to the destination. The Target is calculated from after the 16-bit
592 // NumToSkip entry itself, so subtract two from the displacement here
593 // to account for that.
594 uint32_t FixupIdx = *I;
595 uint32_t Delta = DestIdx - FixupIdx - 2;
596 // Our NumToSkip entries are 16-bits. Make sure our table isn't too
598 assert(Delta < 65536U && "disassembler decoding table too large!");
599 Table[FixupIdx] = (uint8_t)Delta;
600 Table[FixupIdx + 1] = (uint8_t)(Delta >> 8);
604 // Emit table entries to decode instructions given a segment or segments
606 void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
607 TableInfo.Table.push_back(MCD::OPC_ExtractField);
608 TableInfo.Table.push_back(StartBit);
609 TableInfo.Table.push_back(NumBits);
611 // A new filter entry begins a new scope for fixup resolution.
612 TableInfo.FixupStack.emplace_back();
614 DecoderTable &Table = TableInfo.Table;
616 size_t PrevFilter = 0;
617 bool HasFallthrough = false;
618 for (auto &Filter : FilterChooserMap) {
619 // Field value -1 implies a non-empty set of variable instructions.
620 // See also recurse().
621 if (Filter.first == (unsigned)-1) {
622 HasFallthrough = true;
624 // Each scope should always have at least one filter value to check
626 assert(PrevFilter != 0 && "empty filter set!");
627 FixupList &CurScope = TableInfo.FixupStack.back();
628 // Resolve any NumToSkip fixups in the current scope.
629 resolveTableFixups(Table, CurScope, Table.size());
631 PrevFilter = 0; // Don't re-process the filter's fallthrough.
633 Table.push_back(MCD::OPC_FilterValue);
634 // Encode and emit the value to filter against.
636 unsigned Len = encodeULEB128(Filter.first, Buffer);
637 Table.insert(Table.end(), Buffer, Buffer + Len);
638 // Reserve space for the NumToSkip entry. We'll backpatch the value
640 PrevFilter = Table.size();
645 // We arrive at a category of instructions with the same segment value.
646 // Now delegate to the sub filter chooser for further decodings.
647 // The case may fallthrough, which happens if the remaining well-known
648 // encoding bits do not match exactly.
649 Filter.second->emitTableEntries(TableInfo);
651 // Now that we've emitted the body of the handler, update the NumToSkip
652 // of the filter itself to be able to skip forward when false. Subtract
653 // two as to account for the width of the NumToSkip field itself.
655 uint32_t NumToSkip = Table.size() - PrevFilter - 2;
656 assert(NumToSkip < 65536U && "disassembler decoding table too large!");
657 Table[PrevFilter] = (uint8_t)NumToSkip;
658 Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8);
662 // Any remaining unresolved fixups bubble up to the parent fixup scope.
663 assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!");
664 FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1;
665 FixupScopeList::iterator Dest = Source - 1;
666 Dest->insert(Dest->end(), Source->begin(), Source->end());
667 TableInfo.FixupStack.pop_back();
669 // If there is no fallthrough, then the final filter should get fixed
670 // up according to the enclosing scope rather than the current position.
672 TableInfo.FixupStack.back().push_back(PrevFilter);
675 // Returns the number of fanout produced by the filter. More fanout implies
676 // the filter distinguishes more categories of instructions.
677 unsigned Filter::usefulness() const {
678 if (!VariableInstructions.empty())
679 return FilteredInstructions.size();
681 return FilteredInstructions.size() + 1;
684 //////////////////////////////////
686 // Filterchooser Implementation //
688 //////////////////////////////////
690 // Emit the decoder state machine table.
691 void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
693 unsigned Indentation,
695 StringRef Namespace) const {
696 OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace
697 << BitWidth << "[] = {\n";
701 // FIXME: We may be able to use the NumToSkip values to recover
702 // appropriate indentation levels.
703 DecoderTable::const_iterator I = Table.begin();
704 DecoderTable::const_iterator E = Table.end();
706 assert (I < E && "incomplete decode table entry!");
708 uint64_t Pos = I - Table.begin();
709 OS << "/* " << Pos << " */";
714 PrintFatalError("invalid decode table opcode");
715 case MCD::OPC_ExtractField: {
717 unsigned Start = *I++;
719 OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
720 << Len << ", // Inst{";
722 OS << (Start + Len - 1) << "-";
723 OS << Start << "} ...\n";
726 case MCD::OPC_FilterValue: {
728 OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
729 // The filter value is ULEB128 encoded.
731 OS << (unsigned)*I++ << ", ";
732 OS << (unsigned)*I++ << ", ";
734 // 16-bit numtoskip value.
736 uint32_t NumToSkip = Byte;
737 OS << (unsigned)Byte << ", ";
739 OS << (unsigned)Byte << ", ";
740 NumToSkip |= Byte << 8;
741 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
744 case MCD::OPC_CheckField: {
746 unsigned Start = *I++;
748 OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
749 << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
750 // ULEB128 encoded field value.
751 for (; *I >= 128; ++I)
752 OS << (unsigned)*I << ", ";
753 OS << (unsigned)*I++ << ", ";
754 // 16-bit numtoskip value.
756 uint32_t NumToSkip = Byte;
757 OS << (unsigned)Byte << ", ";
759 OS << (unsigned)Byte << ", ";
760 NumToSkip |= Byte << 8;
761 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
764 case MCD::OPC_CheckPredicate: {
766 OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
767 for (; *I >= 128; ++I)
768 OS << (unsigned)*I << ", ";
769 OS << (unsigned)*I++ << ", ";
771 // 16-bit numtoskip value.
773 uint32_t NumToSkip = Byte;
774 OS << (unsigned)Byte << ", ";
776 OS << (unsigned)Byte << ", ";
777 NumToSkip |= Byte << 8;
778 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
781 case MCD::OPC_Decode:
782 case MCD::OPC_TryDecode: {
783 bool IsTry = *I == MCD::OPC_TryDecode;
785 // Extract the ULEB128 encoded Opcode to a buffer.
786 uint8_t Buffer[8], *p = Buffer;
787 while ((*p++ = *I++) >= 128)
788 assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer)
789 && "ULEB128 value too large!");
790 // Decode the Opcode value.
791 unsigned Opc = decodeULEB128(Buffer);
792 OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
794 for (p = Buffer; *p >= 128; ++p)
795 OS << (unsigned)*p << ", ";
796 OS << (unsigned)*p << ", ";
799 for (; *I >= 128; ++I)
800 OS << (unsigned)*I << ", ";
801 OS << (unsigned)*I++ << ", ";
805 << NumberedInstructions[Opc]->TheDef->getName() << "\n";
809 // Fallthrough for OPC_TryDecode.
811 // 16-bit numtoskip value.
813 uint32_t NumToSkip = Byte;
814 OS << (unsigned)Byte << ", ";
816 OS << (unsigned)Byte << ", ";
817 NumToSkip |= Byte << 8;
820 << NumberedInstructions[Opc]->TheDef->getName()
821 << ", skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
824 case MCD::OPC_SoftFail: {
826 OS.indent(Indentation) << "MCD::OPC_SoftFail";
831 OS << ", " << (unsigned)*I;
832 Value += (*I & 0x7f) << Shift;
834 } while (*I++ >= 128);
844 OS << ", " << (unsigned)*I;
845 Value += (*I & 0x7f) << Shift;
847 } while (*I++ >= 128);
856 case MCD::OPC_Fail: {
858 OS.indent(Indentation) << "MCD::OPC_Fail,\n";
863 OS.indent(Indentation) << "0\n";
867 OS.indent(Indentation) << "};\n\n";
870 void FixedLenDecoderEmitter::
871 emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
872 unsigned Indentation) const {
873 // The predicate function is just a big switch statement based on the
874 // input predicate index.
875 OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
876 << "const FeatureBitset& Bits) {\n";
878 if (!Predicates.empty()) {
879 OS.indent(Indentation) << "switch (Idx) {\n";
880 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
882 for (const auto &Predicate : Predicates) {
883 OS.indent(Indentation) << "case " << Index++ << ":\n";
884 OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
886 OS.indent(Indentation) << "}\n";
888 // No case statement to emit
889 OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
892 OS.indent(Indentation) << "}\n\n";
895 void FixedLenDecoderEmitter::
896 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, bool &DecodeComplete) {\n";
906 OS.indent(Indentation) << "DecodeComplete = true;\n";
907 OS.indent(Indentation) << "InsnType tmp;\n";
908 OS.indent(Indentation) << "switch (Idx) {\n";
909 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
911 for (const auto &Decoder : Decoders) {
912 OS.indent(Indentation) << "case " << Index++ << ":\n";
914 OS.indent(Indentation+2) << "return S;\n";
916 OS.indent(Indentation) << "}\n";
918 OS.indent(Indentation) << "}\n\n";
921 // Populates the field of the insn given the start position and the number of
922 // consecutive bits to scan for.
924 // Returns false if and on the first uninitialized bit value encountered.
925 // Returns true, otherwise.
926 bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
927 unsigned StartBit, unsigned NumBits) const {
930 for (unsigned i = 0; i < NumBits; ++i) {
931 if (Insn[StartBit + i] == BIT_UNSET)
934 if (Insn[StartBit + i] == BIT_TRUE)
935 Field = Field | (1ULL << i);
941 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
942 /// filter array as a series of chars.
943 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]) {
963 /// dumpStack - dumpStack traverses the filter chooser chain and calls
964 /// dumpFilterArray on each filter chooser up to the top level one.
965 void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const {
966 const FilterChooser *current = this;
970 dumpFilterArray(o, current->FilterBitValues);
972 current = current->Parent;
976 // Calculates the island(s) needed to decode the instruction.
977 // This returns a list of undecoded bits of an instructions, for example,
978 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
979 // decoded bits in order to verify that the instruction matches the Opcode.
980 unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
981 std::vector<unsigned> &EndBits,
982 std::vector<uint64_t> &FieldVals,
983 const insn_t &Insn) const {
987 uint64_t FieldVal = 0;
990 // 1: Water (the bit value does not affect decoding)
991 // 2: Island (well-known bit value needed for decoding)
995 for (unsigned i = 0; i < BitWidth; ++i) {
996 Val = Value(Insn[i]);
997 bool Filtered = PositionFiltered(i);
999 default: llvm_unreachable("Unreachable code!");
1002 if (Filtered || Val == -1)
1003 State = 1; // Still in Water
1005 State = 2; // Into the Island
1007 StartBits.push_back(i);
1012 if (Filtered || Val == -1) {
1013 State = 1; // Into the Water
1014 EndBits.push_back(i - 1);
1015 FieldVals.push_back(FieldVal);
1018 State = 2; // Still in Island
1020 FieldVal = FieldVal | Val << BitNo;
1025 // If we are still in Island after the loop, do some housekeeping.
1027 EndBits.push_back(BitWidth - 1);
1028 FieldVals.push_back(FieldVal);
1032 assert(StartBits.size() == Num && EndBits.size() == Num &&
1033 FieldVals.size() == Num);
1037 void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
1038 const OperandInfo &OpInfo,
1039 bool &OpHasCompleteDecoder) const {
1040 const std::string &Decoder = OpInfo.Decoder;
1042 if (OpInfo.numFields() != 1)
1043 o.indent(Indentation) << "tmp = 0;\n";
1045 for (const EncodingField &EF : OpInfo) {
1046 o.indent(Indentation) << "tmp ";
1047 if (OpInfo.numFields() != 1) o << '|';
1048 o << "= fieldFromInstruction"
1049 << "(insn, " << EF.Base << ", " << EF.Width << ')';
1050 if (OpInfo.numFields() != 1 || EF.Offset != 0)
1051 o << " << " << EF.Offset;
1055 if (Decoder != "") {
1056 OpHasCompleteDecoder = OpInfo.HasCompleteDecoder;
1057 o.indent(Indentation) << Emitter->GuardPrefix << Decoder
1058 << "(MI, tmp, Address, Decoder)"
1059 << Emitter->GuardPostfix
1060 << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
1061 << "return MCDisassembler::Fail; }\n";
1063 OpHasCompleteDecoder = true;
1064 o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
1068 void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
1069 unsigned Opc, bool &HasCompleteDecoder) const {
1070 HasCompleteDecoder = true;
1072 for (const auto &Op : Operands.find(Opc)->second) {
1073 // If a custom instruction decoder was specified, use that.
1074 if (Op.numFields() == 0 && Op.Decoder.size()) {
1075 HasCompleteDecoder = Op.HasCompleteDecoder;
1076 OS.indent(Indentation) << Emitter->GuardPrefix << Op.Decoder
1077 << "(MI, insn, Address, Decoder)"
1078 << Emitter->GuardPostfix
1079 << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
1080 << "return MCDisassembler::Fail; }\n";
1084 bool OpHasCompleteDecoder;
1085 emitBinaryParser(OS, Indentation, Op, OpHasCompleteDecoder);
1086 if (!OpHasCompleteDecoder)
1087 HasCompleteDecoder = false;
1091 unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
1093 bool &HasCompleteDecoder) const {
1094 // Build up the predicate string.
1095 SmallString<256> Decoder;
1096 // FIXME: emitDecoder() function can take a buffer directly rather than
1098 raw_svector_ostream S(Decoder);
1100 emitDecoder(S, I, Opc, HasCompleteDecoder);
1102 // Using the full decoder string as the key value here is a bit
1103 // heavyweight, but is effective. If the string comparisons become a
1104 // performance concern, we can implement a mangling of the predicate
1105 // data easily enough with a map back to the actual string. That's
1106 // overkill for now, though.
1108 // Make sure the predicate is in the table.
1109 Decoders.insert(StringRef(Decoder));
1110 // Now figure out the index for when we write out the table.
1111 DecoderSet::const_iterator P = std::find(Decoders.begin(),
1114 return (unsigned)(P - Decoders.begin());
1117 static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
1118 const std::string &PredicateNamespace) {
1120 o << "!Bits[" << PredicateNamespace << "::"
1121 << str.slice(1,str.size()) << "]";
1123 o << "Bits[" << PredicateNamespace << "::" << str << "]";
1126 bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
1127 unsigned Opc) const {
1128 ListInit *Predicates =
1129 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
1130 bool IsFirstEmission = true;
1131 for (unsigned i = 0; i < Predicates->size(); ++i) {
1132 Record *Pred = Predicates->getElementAsRecord(i);
1133 if (!Pred->getValue("AssemblerMatcherPredicate"))
1136 std::string P = Pred->getValueAsString("AssemblerCondString");
1141 if (!IsFirstEmission)
1145 std::pair<StringRef, StringRef> pairs = SR.split(',');
1146 while (pairs.second.size()) {
1147 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1149 pairs = pairs.second.split(',');
1151 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1152 IsFirstEmission = false;
1154 return !Predicates->empty();
1157 bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const {
1158 ListInit *Predicates =
1159 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
1160 for (unsigned i = 0; i < Predicates->size(); ++i) {
1161 Record *Pred = Predicates->getElementAsRecord(i);
1162 if (!Pred->getValue("AssemblerMatcherPredicate"))
1165 std::string P = Pred->getValueAsString("AssemblerCondString");
1175 unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo,
1176 StringRef Predicate) const {
1177 // Using the full predicate string as the key value here is a bit
1178 // heavyweight, but is effective. If the string comparisons become a
1179 // performance concern, we can implement a mangling of the predicate
1180 // data easily enough with a map back to the actual string. That's
1181 // overkill for now, though.
1183 // Make sure the predicate is in the table.
1184 TableInfo.Predicates.insert(Predicate.str());
1185 // Now figure out the index for when we write out the table.
1186 PredicateSet::const_iterator P = std::find(TableInfo.Predicates.begin(),
1187 TableInfo.Predicates.end(),
1189 return (unsigned)(P - TableInfo.Predicates.begin());
1192 void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
1193 unsigned Opc) const {
1194 if (!doesOpcodeNeedPredicate(Opc))
1197 // Build up the predicate string.
1198 SmallString<256> Predicate;
1199 // FIXME: emitPredicateMatch() functions can take a buffer directly rather
1201 raw_svector_ostream PS(Predicate);
1203 emitPredicateMatch(PS, I, Opc);
1205 // Figure out the index into the predicate table for the predicate just
1207 unsigned PIdx = getPredicateIndex(TableInfo, PS.str());
1208 SmallString<16> PBytes;
1209 raw_svector_ostream S(PBytes);
1210 encodeULEB128(PIdx, S);
1212 TableInfo.Table.push_back(MCD::OPC_CheckPredicate);
1214 for (unsigned i = 0, e = PBytes.size(); i != e; ++i)
1215 TableInfo.Table.push_back(PBytes[i]);
1216 // Push location for NumToSkip backpatching.
1217 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1218 TableInfo.Table.push_back(0);
1219 TableInfo.Table.push_back(0);
1222 void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
1223 unsigned Opc) const {
1225 AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail");
1226 if (!SFBits) return;
1227 BitsInit *InstBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("Inst");
1229 APInt PositiveMask(BitWidth, 0ULL);
1230 APInt NegativeMask(BitWidth, 0ULL);
1231 for (unsigned i = 0; i < BitWidth; ++i) {
1232 bit_value_t B = bitFromBits(*SFBits, i);
1233 bit_value_t IB = bitFromBits(*InstBits, i);
1235 if (B != BIT_TRUE) continue;
1239 // The bit is meant to be false, so emit a check to see if it is true.
1240 PositiveMask.setBit(i);
1243 // The bit is meant to be true, so emit a check to see if it is false.
1244 NegativeMask.setBit(i);
1247 // The bit is not set; this must be an error!
1248 StringRef Name = AllInstructions[Opc]->TheDef->getName();
1249 errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in " << Name
1250 << " is set but Inst{" << i << "} is unset!\n"
1251 << " - You can only mark a bit as SoftFail if it is fully defined"
1252 << " (1/0 - not '?') in Inst\n";
1257 bool NeedPositiveMask = PositiveMask.getBoolValue();
1258 bool NeedNegativeMask = NegativeMask.getBoolValue();
1260 if (!NeedPositiveMask && !NeedNegativeMask)
1263 TableInfo.Table.push_back(MCD::OPC_SoftFail);
1265 SmallString<16> MaskBytes;
1266 raw_svector_ostream S(MaskBytes);
1267 if (NeedPositiveMask) {
1268 encodeULEB128(PositiveMask.getZExtValue(), S);
1269 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
1270 TableInfo.Table.push_back(MaskBytes[i]);
1272 TableInfo.Table.push_back(0);
1273 if (NeedNegativeMask) {
1275 encodeULEB128(NegativeMask.getZExtValue(), S);
1276 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
1277 TableInfo.Table.push_back(MaskBytes[i]);
1279 TableInfo.Table.push_back(0);
1282 // Emits table entries to decode the singleton.
1283 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1284 unsigned Opc) const {
1285 std::vector<unsigned> StartBits;
1286 std::vector<unsigned> EndBits;
1287 std::vector<uint64_t> FieldVals;
1289 insnWithID(Insn, Opc);
1291 // Look for islands of undecoded bits of the singleton.
1292 getIslands(StartBits, EndBits, FieldVals, Insn);
1294 unsigned Size = StartBits.size();
1296 // Emit the predicate table entry if one is needed.
1297 emitPredicateTableEntry(TableInfo, Opc);
1299 // Check any additional encoding fields needed.
1300 for (unsigned I = Size; I != 0; --I) {
1301 unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1;
1302 TableInfo.Table.push_back(MCD::OPC_CheckField);
1303 TableInfo.Table.push_back(StartBits[I-1]);
1304 TableInfo.Table.push_back(NumBits);
1305 uint8_t Buffer[8], *p;
1306 encodeULEB128(FieldVals[I-1], Buffer);
1307 for (p = Buffer; *p >= 128 ; ++p)
1308 TableInfo.Table.push_back(*p);
1309 TableInfo.Table.push_back(*p);
1310 // Push location for NumToSkip backpatching.
1311 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1312 // The fixup is always 16-bits, so go ahead and allocate the space
1313 // in the table so all our relative position calculations work OK even
1314 // before we fully resolve the real value here.
1315 TableInfo.Table.push_back(0);
1316 TableInfo.Table.push_back(0);
1319 // Check for soft failure of the match.
1320 emitSoftFailTableEntry(TableInfo, Opc);
1322 bool HasCompleteDecoder;
1323 unsigned DIdx = getDecoderIndex(TableInfo.Decoders, Opc, HasCompleteDecoder);
1325 // Produce OPC_Decode or OPC_TryDecode opcode based on the information
1326 // whether the instruction decoder is complete or not. If it is complete
1327 // then it handles all possible values of remaining variable/unfiltered bits
1328 // and for any value can determine if the bitpattern is a valid instruction
1329 // or not. This means OPC_Decode will be the final step in the decoding
1330 // process. If it is not complete, then the Fail return code from the
1331 // decoder method indicates that additional processing should be done to see
1332 // if there is any other instruction that also matches the bitpattern and
1334 TableInfo.Table.push_back(HasCompleteDecoder ? MCD::OPC_Decode :
1335 MCD::OPC_TryDecode);
1336 uint8_t Buffer[8], *p;
1337 encodeULEB128(Opc, Buffer);
1338 for (p = Buffer; *p >= 128 ; ++p)
1339 TableInfo.Table.push_back(*p);
1340 TableInfo.Table.push_back(*p);
1342 SmallString<16> Bytes;
1343 raw_svector_ostream S(Bytes);
1344 encodeULEB128(DIdx, S);
1347 for (unsigned i = 0, e = Bytes.size(); i != e; ++i)
1348 TableInfo.Table.push_back(Bytes[i]);
1350 if (!HasCompleteDecoder) {
1351 // Push location for NumToSkip backpatching.
1352 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1353 // Allocate the space for the fixup.
1354 TableInfo.Table.push_back(0);
1355 TableInfo.Table.push_back(0);
1359 // Emits table entries to decode the singleton, and then to decode the rest.
1360 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1361 const Filter &Best) const {
1362 unsigned Opc = Best.getSingletonOpc();
1364 // complex singletons need predicate checks from the first singleton
1365 // to refer forward to the variable filterchooser that follows.
1366 TableInfo.FixupStack.emplace_back();
1368 emitSingletonTableEntry(TableInfo, Opc);
1370 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
1371 TableInfo.Table.size());
1372 TableInfo.FixupStack.pop_back();
1374 Best.getVariableFC().emitTableEntries(TableInfo);
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.
1380 void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit,
1383 Filters.emplace_back(*this, startBit, numBit, true);
1384 BestIndex = 0; // Sole Filter instance to choose from.
1385 bestFilter().recurse();
1388 // reportRegion is a helper function for filterProcessor to mark a region as
1389 // eligible for use as a filter region.
1390 void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1391 unsigned BitIndex, bool AllowMixed) {
1392 if (RA == ATTR_MIXED && AllowMixed)
1393 Filters.emplace_back(*this, StartBit, BitIndex - StartBit, true);
1394 else if (RA == ATTR_ALL_SET && !AllowMixed)
1395 Filters.emplace_back(*this, StartBit, BitIndex - StartBit, false);
1398 // FilterProcessor scans the well-known encoding bits of the instructions and
1399 // builds up a list of candidate filters. It chooses the best filter and
1400 // recursively descends down the decoding tree.
1401 bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1404 unsigned numInstructions = Opcodes.size();
1406 assert(numInstructions && "Filter created with no instructions");
1408 // No further filtering is necessary.
1409 if (numInstructions == 1)
1412 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1413 // instructions is 3.
1414 if (AllowMixed && !Greedy) {
1415 assert(numInstructions == 3);
1417 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1418 std::vector<unsigned> StartBits;
1419 std::vector<unsigned> EndBits;
1420 std::vector<uint64_t> FieldVals;
1423 insnWithID(Insn, Opcodes[i]);
1425 // Look for islands of undecoded bits of any instruction.
1426 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1427 // Found an instruction with island(s). Now just assign a filter.
1428 runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true);
1436 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1437 // The automaton consumes the corresponding bit from each
1440 // Input symbols: 0, 1, and _ (unset).
1441 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1442 // Initial state: NONE.
1444 // (NONE) ------- [01] -> (ALL_SET)
1445 // (NONE) ------- _ ----> (ALL_UNSET)
1446 // (ALL_SET) ---- [01] -> (ALL_SET)
1447 // (ALL_SET) ---- _ ----> (MIXED)
1448 // (ALL_UNSET) -- [01] -> (MIXED)
1449 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1450 // (MIXED) ------ . ----> (MIXED)
1451 // (FILTERED)---- . ----> (FILTERED)
1453 std::vector<bitAttr_t> bitAttrs;
1455 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1456 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1457 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
1458 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1459 FilterBitValues[BitIndex] == BIT_FALSE)
1460 bitAttrs.push_back(ATTR_FILTERED);
1462 bitAttrs.push_back(ATTR_NONE);
1464 for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
1467 insnWithID(insn, Opcodes[InsnIndex]);
1469 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
1470 switch (bitAttrs[BitIndex]) {
1472 if (insn[BitIndex] == BIT_UNSET)
1473 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1475 bitAttrs[BitIndex] = ATTR_ALL_SET;
1478 if (insn[BitIndex] == BIT_UNSET)
1479 bitAttrs[BitIndex] = ATTR_MIXED;
1481 case ATTR_ALL_UNSET:
1482 if (insn[BitIndex] != BIT_UNSET)
1483 bitAttrs[BitIndex] = ATTR_MIXED;
1492 // The regionAttr automaton consumes the bitAttrs automatons' state,
1493 // lowest-to-highest.
1495 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1496 // States: NONE, ALL_SET, MIXED
1497 // Initial state: NONE
1499 // (NONE) ----- F --> (NONE)
1500 // (NONE) ----- S --> (ALL_SET) ; and set region start
1501 // (NONE) ----- U --> (NONE)
1502 // (NONE) ----- M --> (MIXED) ; and set region start
1503 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1504 // (ALL_SET) -- S --> (ALL_SET)
1505 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1506 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1507 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1508 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1509 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1510 // (MIXED) ---- M --> (MIXED)
1512 bitAttr_t RA = ATTR_NONE;
1513 unsigned StartBit = 0;
1515 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
1516 bitAttr_t bitAttr = bitAttrs[BitIndex];
1518 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1526 StartBit = BitIndex;
1529 case ATTR_ALL_UNSET:
1532 StartBit = BitIndex;
1536 llvm_unreachable("Unexpected bitAttr!");
1542 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1547 case ATTR_ALL_UNSET:
1548 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1552 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1553 StartBit = BitIndex;
1557 llvm_unreachable("Unexpected bitAttr!");
1563 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1564 StartBit = BitIndex;
1568 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1569 StartBit = BitIndex;
1572 case ATTR_ALL_UNSET:
1573 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1579 llvm_unreachable("Unexpected bitAttr!");
1582 case ATTR_ALL_UNSET:
1583 llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
1585 llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
1589 // At the end, if we're still in ALL_SET or MIXED states, report a region
1596 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1598 case ATTR_ALL_UNSET:
1601 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1605 // We have finished with the filter processings. Now it's time to choose
1606 // the best performing filter.
1608 bool AllUseless = true;
1609 unsigned BestScore = 0;
1611 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1612 unsigned Usefulness = Filters[i].usefulness();
1617 if (Usefulness > BestScore) {
1619 BestScore = Usefulness;
1624 bestFilter().recurse();
1627 } // end of FilterChooser::filterProcessor(bool)
1629 // Decides on the best configuration of filter(s) to use in order to decode
1630 // the instructions. A conflict of instructions may occur, in which case we
1631 // dump the conflict set to the standard error.
1632 void FilterChooser::doFilter() {
1633 unsigned Num = Opcodes.size();
1634 assert(Num && "FilterChooser created with no instructions");
1636 // Try regions of consecutive known bit values first.
1637 if (filterProcessor(false))
1640 // Then regions of mixed bits (both known and unitialized bit values allowed).
1641 if (filterProcessor(true))
1644 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1645 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1646 // well-known encoding pattern. In such case, we backtrack and scan for the
1647 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1648 if (Num == 3 && filterProcessor(true, false))
1651 // If we come to here, the instruction decoding has failed.
1652 // Set the BestIndex to -1 to indicate so.
1656 // emitTableEntries - Emit state machine entries to decode our share of
1658 void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
1659 if (Opcodes.size() == 1) {
1660 // There is only one instruction in the set, which is great!
1661 // Call emitSingletonDecoder() to see whether there are any remaining
1663 emitSingletonTableEntry(TableInfo, Opcodes[0]);
1667 // Choose the best filter to do the decodings!
1668 if (BestIndex != -1) {
1669 const Filter &Best = Filters[BestIndex];
1670 if (Best.getNumFiltered() == 1)
1671 emitSingletonTableEntry(TableInfo, Best);
1673 Best.emitTableEntry(TableInfo);
1677 // We don't know how to decode these instructions! Dump the
1678 // conflict set and bail.
1680 // Print out useful conflict information for postmortem analysis.
1681 errs() << "Decoding Conflict:\n";
1683 dumpStack(errs(), "\t\t");
1685 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1686 const std::string &Name = nameWithID(Opcodes[i]);
1688 errs() << '\t' << Name << " ";
1690 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1695 static bool populateInstruction(CodeGenTarget &Target,
1696 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.
1703 BitsInit &Bits = getBitsField(Def, "Inst");
1704 if (Bits.allInComplete()) return false;
1706 std::vector<OperandInfo> InsnOperands;
1708 // If the instruction has specified a custom decoding hook, use that instead
1709 // of trying to auto-generate the decoder.
1710 std::string InstDecoder = Def.getValueAsString("DecoderMethod");
1711 if (InstDecoder != "") {
1712 bool HasCompleteInstDecoder = Def.getValueAsBit("hasCompleteDecoder");
1713 InsnOperands.push_back(OperandInfo(InstDecoder, HasCompleteInstDecoder));
1714 Operands[Opc] = InsnOperands;
1718 // Generate a description of the operand of the instruction that we know
1719 // how to decode automatically.
1720 // FIXME: We'll need to have a way to manually override this as needed.
1722 // Gather the outputs/inputs of the instruction, so we can find their
1723 // positions in the encoding. This assumes for now that they appear in the
1724 // MCInst in the order that they're listed.
1725 std::vector<std::pair<Init*, std::string> > InOutOperands;
1726 DagInit *Out = Def.getValueAsDag("OutOperandList");
1727 DagInit *In = Def.getValueAsDag("InOperandList");
1728 for (unsigned i = 0; i < Out->getNumArgs(); ++i)
1729 InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i)));
1730 for (unsigned i = 0; i < In->getNumArgs(); ++i)
1731 InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i)));
1733 // Search for tied operands, so that we can correctly instantiate
1734 // operands that are not explicitly represented in the encoding.
1735 std::map<std::string, std::string> TiedNames;
1736 for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
1737 int tiedTo = CGI.Operands[i].getTiedRegister();
1739 std::pair<unsigned, unsigned> SO =
1740 CGI.Operands.getSubOperandNumber(tiedTo);
1741 TiedNames[InOutOperands[i].second] = InOutOperands[SO.first].second;
1742 TiedNames[InOutOperands[SO.first].second] = InOutOperands[i].second;
1746 std::map<std::string, std::vector<OperandInfo> > NumberedInsnOperands;
1747 std::set<std::string> NumberedInsnOperandsNoTie;
1748 if (Target.getInstructionSet()->
1749 getValueAsBit("decodePositionallyEncodedOperands")) {
1750 const std::vector<RecordVal> &Vals = Def.getValues();
1751 unsigned NumberedOp = 0;
1753 std::set<unsigned> NamedOpIndices;
1754 if (Target.getInstructionSet()->
1755 getValueAsBit("noNamedPositionallyEncodedOperands"))
1756 // Collect the set of operand indices that might correspond to named
1757 // operand, and skip these when assigning operands based on position.
1758 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1760 if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
1763 NamedOpIndices.insert(OpIdx);
1766 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1767 // Ignore fixed fields in the record, we're looking for values like:
1768 // bits<5> RST = { ?, ?, ?, ?, ? };
1769 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
1772 // Determine if Vals[i] actually contributes to the Inst encoding.
1774 for (; bi < Bits.getNumBits(); ++bi) {
1775 VarInit *Var = nullptr;
1776 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1778 Var = dyn_cast<VarInit>(BI->getBitVar());
1780 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1782 if (Var && Var->getName() == Vals[i].getName())
1786 if (bi == Bits.getNumBits())
1789 // Skip variables that correspond to explicitly-named operands.
1791 if (CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
1794 // Get the bit range for this operand:
1795 unsigned bitStart = bi++, bitWidth = 1;
1796 for (; bi < Bits.getNumBits(); ++bi) {
1797 VarInit *Var = nullptr;
1798 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1800 Var = dyn_cast<VarInit>(BI->getBitVar());
1802 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1807 if (Var->getName() != Vals[i].getName())
1813 unsigned NumberOps = CGI.Operands.size();
1814 while (NumberedOp < NumberOps &&
1815 (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
1816 (!NamedOpIndices.empty() && NamedOpIndices.count(
1817 CGI.Operands.getSubOperandNumber(NumberedOp).first))))
1820 OpIdx = NumberedOp++;
1822 // OpIdx now holds the ordered operand number of Vals[i].
1823 std::pair<unsigned, unsigned> SO =
1824 CGI.Operands.getSubOperandNumber(OpIdx);
1825 const std::string &Name = CGI.Operands[SO.first].Name;
1827 DEBUG(dbgs() << "Numbered operand mapping for " << Def.getName() << ": " <<
1828 Name << "(" << SO.first << ", " << SO.second << ") => " <<
1829 Vals[i].getName() << "\n");
1831 std::string Decoder = "";
1832 Record *TypeRecord = CGI.Operands[SO.first].Rec;
1834 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
1835 StringInit *String = DecoderString ?
1836 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
1837 if (String && String->getValue() != "")
1838 Decoder = String->getValue();
1840 if (Decoder == "" &&
1841 CGI.Operands[SO.first].MIOperandInfo &&
1842 CGI.Operands[SO.first].MIOperandInfo->getNumArgs()) {
1843 Init *Arg = CGI.Operands[SO.first].MIOperandInfo->
1845 if (TypedInit *TI = cast<TypedInit>(Arg)) {
1846 RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
1847 TypeRecord = Type->getRecord();
1852 if (TypeRecord->isSubClassOf("RegisterOperand"))
1853 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1854 if (TypeRecord->isSubClassOf("RegisterClass")) {
1855 Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
1857 } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
1858 Decoder = "DecodePointerLikeRegClass" +
1859 utostr(TypeRecord->getValueAsInt("RegClassKind"));
1863 DecoderString = TypeRecord->getValue("DecoderMethod");
1864 String = DecoderString ?
1865 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
1866 if (!isReg && String && String->getValue() != "")
1867 Decoder = String->getValue();
1869 RecordVal *HasCompleteDecoderVal =
1870 TypeRecord->getValue("hasCompleteDecoder");
1871 BitInit *HasCompleteDecoderBit = HasCompleteDecoderVal ?
1872 dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) : nullptr;
1873 bool HasCompleteDecoder = HasCompleteDecoderBit ?
1874 HasCompleteDecoderBit->getValue() : true;
1876 OperandInfo OpInfo(Decoder, HasCompleteDecoder);
1877 OpInfo.addField(bitStart, bitWidth, 0);
1879 NumberedInsnOperands[Name].push_back(OpInfo);
1881 // FIXME: For complex operands with custom decoders we can't handle tied
1882 // sub-operands automatically. Skip those here and assume that this is
1883 // fixed up elsewhere.
1884 if (CGI.Operands[SO.first].MIOperandInfo &&
1885 CGI.Operands[SO.first].MIOperandInfo->getNumArgs() > 1 &&
1886 String && String->getValue() != "")
1887 NumberedInsnOperandsNoTie.insert(Name);
1891 // For each operand, see if we can figure out where it is encoded.
1892 for (const auto &Op : InOutOperands) {
1893 if (!NumberedInsnOperands[Op.second].empty()) {
1894 InsnOperands.insert(InsnOperands.end(),
1895 NumberedInsnOperands[Op.second].begin(),
1896 NumberedInsnOperands[Op.second].end());
1899 if (!NumberedInsnOperands[TiedNames[Op.second]].empty()) {
1900 if (!NumberedInsnOperandsNoTie.count(TiedNames[Op.second])) {
1901 // Figure out to which (sub)operand we're tied.
1902 unsigned i = CGI.Operands.getOperandNamed(TiedNames[Op.second]);
1903 int tiedTo = CGI.Operands[i].getTiedRegister();
1905 i = CGI.Operands.getOperandNamed(Op.second);
1906 tiedTo = CGI.Operands[i].getTiedRegister();
1910 std::pair<unsigned, unsigned> SO =
1911 CGI.Operands.getSubOperandNumber(tiedTo);
1913 InsnOperands.push_back(NumberedInsnOperands[TiedNames[Op.second]]
1920 std::string Decoder = "";
1922 // At this point, we can locate the field, but we need to know how to
1923 // interpret it. As a first step, require the target to provide callbacks
1924 // for decoding register classes.
1925 // FIXME: This need to be extended to handle instructions with custom
1926 // decoder methods, and operands with (simple) MIOperandInfo's.
1927 TypedInit *TI = cast<TypedInit>(Op.first);
1928 RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
1929 Record *TypeRecord = Type->getRecord();
1931 if (TypeRecord->isSubClassOf("RegisterOperand"))
1932 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1933 if (TypeRecord->isSubClassOf("RegisterClass")) {
1934 Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
1936 } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
1937 Decoder = "DecodePointerLikeRegClass" +
1938 utostr(TypeRecord->getValueAsInt("RegClassKind"));
1942 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
1943 StringInit *String = DecoderString ?
1944 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
1945 if (!isReg && String && String->getValue() != "")
1946 Decoder = String->getValue();
1948 RecordVal *HasCompleteDecoderVal =
1949 TypeRecord->getValue("hasCompleteDecoder");
1950 BitInit *HasCompleteDecoderBit = HasCompleteDecoderVal ?
1951 dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) : nullptr;
1952 bool HasCompleteDecoder = HasCompleteDecoderBit ?
1953 HasCompleteDecoderBit->getValue() : true;
1955 OperandInfo OpInfo(Decoder, HasCompleteDecoder);
1956 unsigned Base = ~0U;
1958 unsigned Offset = 0;
1960 for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
1961 VarInit *Var = nullptr;
1962 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1964 Var = dyn_cast<VarInit>(BI->getBitVar());
1966 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1970 OpInfo.addField(Base, Width, Offset);
1978 if (Var->getName() != Op.second &&
1979 Var->getName() != TiedNames[Op.second]) {
1981 OpInfo.addField(Base, Width, Offset);
1992 Offset = BI ? BI->getBitNum() : 0;
1993 } else if (BI && BI->getBitNum() != Offset + Width) {
1994 OpInfo.addField(Base, Width, Offset);
1997 Offset = BI->getBitNum();
2004 OpInfo.addField(Base, Width, Offset);
2006 if (OpInfo.numFields() > 0)
2007 InsnOperands.push_back(OpInfo);
2010 Operands[Opc] = InsnOperands;
2015 // Dumps the instruction encoding bits.
2016 dumpBits(errs(), Bits);
2020 // Dumps the list of operand info.
2021 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
2022 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
2023 const std::string &OperandName = Info.Name;
2024 const Record &OperandDef = *Info.Rec;
2026 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
2034 // emitFieldFromInstruction - Emit the templated helper function
2035 // fieldFromInstruction().
2036 static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
2037 OS << "// Helper function for extracting fields from encoded instructions.\n"
2038 << "template<typename InsnType>\n"
2039 << "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n"
2040 << " unsigned numBits) {\n"
2041 << " assert(startBit + numBits <= (sizeof(InsnType)*8) &&\n"
2042 << " \"Instruction field out of bounds!\");\n"
2043 << " InsnType fieldMask;\n"
2044 << " if (numBits == sizeof(InsnType)*8)\n"
2045 << " fieldMask = (InsnType)(-1LL);\n"
2047 << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
2048 << " return (insn & fieldMask) >> startBit;\n"
2052 // emitDecodeInstruction - Emit the templated helper function
2053 // decodeInstruction().
2054 static void emitDecodeInstruction(formatted_raw_ostream &OS) {
2055 OS << "template<typename InsnType>\n"
2056 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,\n"
2057 << " InsnType insn, uint64_t Address,\n"
2058 << " const void *DisAsm,\n"
2059 << " const MCSubtargetInfo &STI) {\n"
2060 << " const FeatureBitset& Bits = STI.getFeatureBits();\n"
2062 << " const uint8_t *Ptr = DecodeTable;\n"
2063 << " uint32_t CurFieldValue = 0;\n"
2064 << " DecodeStatus S = MCDisassembler::Success;\n"
2066 << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
2067 << " switch (*Ptr) {\n"
2069 << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
2070 << " return MCDisassembler::Fail;\n"
2071 << " case MCD::OPC_ExtractField: {\n"
2072 << " unsigned Start = *++Ptr;\n"
2073 << " unsigned Len = *++Ptr;\n"
2075 << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
2076 << " DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << \", \"\n"
2077 << " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
2080 << " case MCD::OPC_FilterValue: {\n"
2081 << " // Decode the field value.\n"
2082 << " unsigned Len;\n"
2083 << " InsnType Val = decodeULEB128(++Ptr, &Len);\n"
2085 << " // NumToSkip is a plain 16-bit integer.\n"
2086 << " unsigned NumToSkip = *Ptr++;\n"
2087 << " NumToSkip |= (*Ptr++) << 8;\n"
2089 << " // Perform the filter operation.\n"
2090 << " if (Val != CurFieldValue)\n"
2091 << " Ptr += NumToSkip;\n"
2092 << " DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << \", \" << NumToSkip\n"
2093 << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" : \"PASS:\")\n"
2094 << " << \" continuing at \" << (Ptr - DecodeTable) << \"\\n\");\n"
2098 << " case MCD::OPC_CheckField: {\n"
2099 << " unsigned Start = *++Ptr;\n"
2100 << " unsigned Len = *++Ptr;\n"
2101 << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
2102 << " // Decode the field value.\n"
2103 << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
2105 << " // NumToSkip is a plain 16-bit integer.\n"
2106 << " unsigned NumToSkip = *Ptr++;\n"
2107 << " NumToSkip |= (*Ptr++) << 8;\n"
2109 << " // If the actual and expected values don't match, skip.\n"
2110 << " if (ExpectedValue != FieldValue)\n"
2111 << " Ptr += NumToSkip;\n"
2112 << " DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << \", \"\n"
2113 << " << Len << \", \" << ExpectedValue << \", \" << NumToSkip\n"
2114 << " << \"): FieldValue = \" << FieldValue << \", ExpectedValue = \"\n"
2115 << " << ExpectedValue << \": \"\n"
2116 << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2119 << " case MCD::OPC_CheckPredicate: {\n"
2120 << " unsigned Len;\n"
2121 << " // Decode the Predicate Index value.\n"
2122 << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
2124 << " // NumToSkip is a plain 16-bit integer.\n"
2125 << " unsigned NumToSkip = *Ptr++;\n"
2126 << " NumToSkip |= (*Ptr++) << 8;\n"
2127 << " // Check the predicate.\n"
2129 << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
2130 << " Ptr += NumToSkip;\n"
2132 << " DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx << \"): \"\n"
2133 << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2137 << " case MCD::OPC_Decode: {\n"
2138 << " unsigned Len;\n"
2139 << " // Decode the Opcode value.\n"
2140 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2142 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2146 << " MI.setOpcode(Opc);\n"
2147 << " bool DecodeComplete;\n"
2148 << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, DecodeComplete);\n"
2149 << " assert(DecodeComplete);\n"
2151 << " DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
2152 << " << \", using decoder \" << DecodeIdx << \": \"\n"
2153 << " << (S != MCDisassembler::Fail ? \"PASS\" : \"FAIL\") << \"\\n\");\n"
2156 << " case MCD::OPC_TryDecode: {\n"
2157 << " unsigned Len;\n"
2158 << " // Decode the Opcode value.\n"
2159 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2161 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2163 << " // NumToSkip is a plain 16-bit integer.\n"
2164 << " unsigned NumToSkip = *Ptr++;\n"
2165 << " NumToSkip |= (*Ptr++) << 8;\n"
2167 << " // Perform the decode operation.\n"
2168 << " MCInst TmpMI;\n"
2169 << " TmpMI.setOpcode(Opc);\n"
2170 << " bool DecodeComplete;\n"
2171 << " S = decodeToMCInst(S, DecodeIdx, insn, TmpMI, Address, DisAsm, DecodeComplete);\n"
2172 << " DEBUG(dbgs() << Loc << \": OPC_TryDecode: opcode \" << Opc\n"
2173 << " << \", using decoder \" << DecodeIdx << \": \");\n"
2175 << " if (DecodeComplete) {\n"
2176 << " // Decoding complete.\n"
2177 << " DEBUG(dbgs() << (S != MCDisassembler::Fail ? \"PASS\" : \"FAIL\") << \"\\n\");\n"
2181 << " assert(S == MCDisassembler::Fail);\n"
2182 << " // If the decoding was incomplete, skip.\n"
2183 << " Ptr += NumToSkip;\n"
2184 << " DEBUG(dbgs() << \"FAIL: continuing at \" << (Ptr - DecodeTable) << \"\\n\");\n"
2185 << " // Reset decode status. This also drops a SoftFail status that could be\n"
2186 << " // set before the decode attempt.\n"
2187 << " S = MCDisassembler::Success;\n"
2191 << " case MCD::OPC_SoftFail: {\n"
2192 << " // Decode the mask values.\n"
2193 << " unsigned Len;\n"
2194 << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n"
2196 << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n"
2198 << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n"
2200 << " S = MCDisassembler::SoftFail;\n"
2201 << " DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? \"FAIL\\n\":\"PASS\\n\"));\n"
2204 << " case MCD::OPC_Fail: {\n"
2205 << " DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
2206 << " return MCDisassembler::Fail;\n"
2210 << " llvm_unreachable(\"bogosity detected in disassembler state machine!\");\n"
2214 // Emits disassembler code for instruction decoding.
2215 void FixedLenDecoderEmitter::run(raw_ostream &o) {
2216 formatted_raw_ostream OS(o);
2217 OS << "#include \"llvm/MC/MCInst.h\"\n";
2218 OS << "#include \"llvm/Support/Debug.h\"\n";
2219 OS << "#include \"llvm/Support/DataTypes.h\"\n";
2220 OS << "#include \"llvm/Support/LEB128.h\"\n";
2221 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
2222 OS << "#include <assert.h>\n";
2224 OS << "namespace llvm {\n\n";
2226 emitFieldFromInstruction(OS);
2228 Target.reverseBitsForLittleEndianEncoding();
2230 // Parameterize the decoders based on namespace and instruction width.
2231 NumberedInstructions = Target.getInstructionsByEnumValue();
2232 std::map<std::pair<std::string, unsigned>,
2233 std::vector<unsigned> > OpcMap;
2234 std::map<unsigned, std::vector<OperandInfo> > Operands;
2236 for (unsigned i = 0; i < NumberedInstructions.size(); ++i) {
2237 const CodeGenInstruction *Inst = NumberedInstructions[i];
2238 const Record *Def = Inst->TheDef;
2239 unsigned Size = Def->getValueAsInt("Size");
2240 if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
2241 Def->getValueAsBit("isPseudo") ||
2242 Def->getValueAsBit("isAsmParserOnly") ||
2243 Def->getValueAsBit("isCodeGenOnly"))
2246 std::string DecoderNamespace = Def->getValueAsString("DecoderNamespace");
2249 if (populateInstruction(Target, *Inst, i, Operands)) {
2250 OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
2255 DecoderTableInfo TableInfo;
2256 for (const auto &Opc : OpcMap) {
2257 // Emit the decoder for this namespace+width combination.
2258 FilterChooser FC(NumberedInstructions, Opc.second, Operands,
2259 8*Opc.first.second, this);
2261 // The decode table is cleared for each top level decoder function. The
2262 // predicates and decoders themselves, however, are shared across all
2263 // decoders to give more opportunities for uniqueing.
2264 TableInfo.Table.clear();
2265 TableInfo.FixupStack.clear();
2266 TableInfo.Table.reserve(16384);
2267 TableInfo.FixupStack.emplace_back();
2268 FC.emitTableEntries(TableInfo);
2269 // Any NumToSkip fixups in the top level scope can resolve to the
2270 // OPC_Fail at the end of the table.
2271 assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
2272 // Resolve any NumToSkip fixups in the current scope.
2273 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
2274 TableInfo.Table.size());
2275 TableInfo.FixupStack.clear();
2277 TableInfo.Table.push_back(MCD::OPC_Fail);
2279 // Print the table to the output stream.
2280 emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), Opc.first.first);
2284 // Emit the predicate function.
2285 emitPredicateFunction(OS, TableInfo.Predicates, 0);
2287 // Emit the decoder function.
2288 emitDecoderFunction(OS, TableInfo.Decoders, 0);
2290 // Emit the main entry point for the decoder, decodeInstruction().
2291 emitDecodeInstruction(OS);
2293 OS << "\n} // End llvm namespace\n";
2298 void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
2299 const std::string &PredicateNamespace,
2300 const std::string &GPrefix,
2301 const std::string &GPostfix, const std::string &ROK,
2302 const std::string &RFail, const std::string &L) {
2303 FixedLenDecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix,
2304 ROK, RFail, L).run(OS);
2307 } // End llvm namespace