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 #define DEBUG_TYPE "decoder-emitter"
17 #include "CodeGenTarget.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/MC/MCFixedLenDisassembler.h"
24 #include "llvm/Support/DataTypes.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Support/LEB128.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/TableGen/Error.h"
30 #include "llvm/TableGen/Record.h"
31 #include "llvm/TableGen/TableGenBackend.h"
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;
49 OperandInfo(std::string D)
52 void addField(unsigned Base, unsigned Width, unsigned Offset) {
53 Fields.push_back(EncodingField(Base, Width, Offset));
56 unsigned numFields() const { return Fields.size(); }
58 typedef std::vector<EncodingField>::const_iterator const_iterator;
60 const_iterator begin() const { return Fields.begin(); }
61 const_iterator end() const { return Fields.end(); }
64 typedef std::vector<uint8_t> DecoderTable;
65 typedef uint32_t DecoderFixup;
66 typedef std::vector<DecoderFixup> FixupList;
67 typedef std::vector<FixupList> FixupScopeList;
68 typedef SetVector<std::string> PredicateSet;
69 typedef SetVector<std::string> DecoderSet;
70 struct DecoderTableInfo {
72 FixupScopeList FixupStack;
73 PredicateSet Predicates;
77 } // End anonymous namespace
80 class FixedLenDecoderEmitter {
81 const std::vector<const CodeGenInstruction*> *NumberedInstructions;
84 // Defaults preserved here for documentation, even though they aren't
85 // strictly necessary given the way that this is currently being called.
86 FixedLenDecoderEmitter(RecordKeeper &R,
87 std::string PredicateNamespace,
88 std::string GPrefix = "if (",
89 std::string GPostfix = " == MCDisassembler::Fail)"
90 " return MCDisassembler::Fail;",
91 std::string ROK = "MCDisassembler::Success",
92 std::string RFail = "MCDisassembler::Fail",
95 PredicateNamespace(PredicateNamespace),
96 GuardPrefix(GPrefix), GuardPostfix(GPostfix),
97 ReturnOK(ROK), ReturnFail(RFail), Locals(L) {}
99 // Emit the decoder state machine table.
100 void emitTable(formatted_raw_ostream &o, DecoderTable &Table,
101 unsigned Indentation, unsigned BitWidth,
102 StringRef Namespace) const;
103 void emitPredicateFunction(formatted_raw_ostream &OS,
104 PredicateSet &Predicates,
105 unsigned Indentation) const;
106 void emitDecoderFunction(formatted_raw_ostream &OS,
107 DecoderSet &Decoders,
108 unsigned Indentation) const;
110 // run - Output the code emitter
111 void run(raw_ostream &o);
114 CodeGenTarget Target;
116 std::string PredicateNamespace;
117 std::string GuardPrefix, GuardPostfix;
118 std::string ReturnOK, ReturnFail;
121 } // End anonymous namespace
123 // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
126 // BIT_UNFILTERED is used as the init value for a filter position. It is used
127 // only for filter processings.
132 BIT_UNFILTERED // unfiltered
135 static bool ValueSet(bit_value_t V) {
136 return (V == BIT_TRUE || V == BIT_FALSE);
138 static bool ValueNotSet(bit_value_t V) {
139 return (V == BIT_UNSET);
141 static int Value(bit_value_t V) {
142 return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
144 static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) {
145 if (BitInit *bit = dyn_cast<BitInit>(bits.getBit(index)))
146 return bit->getValue() ? BIT_TRUE : BIT_FALSE;
148 // The bit is uninitialized.
151 // Prints the bit value for each position.
152 static void dumpBits(raw_ostream &o, const BitsInit &bits) {
153 for (unsigned index = bits.getNumBits(); index > 0; --index) {
154 switch (bitFromBits(bits, index - 1)) {
165 llvm_unreachable("unexpected return value from bitFromBits");
170 static BitsInit &getBitsField(const Record &def, const char *str) {
171 BitsInit *bits = def.getValueAsBitsInit(str);
175 // Forward declaration.
178 } // End anonymous namespace
180 // Representation of the instruction to work on.
181 typedef std::vector<bit_value_t> insn_t;
183 /// Filter - Filter works with FilterChooser to produce the decoding tree for
186 /// It is useful to think of a Filter as governing the switch stmts of the
187 /// decoding tree in a certain level. Each case stmt delegates to an inferior
188 /// FilterChooser to decide what further decoding logic to employ, or in another
189 /// words, what other remaining bits to look at. The FilterChooser eventually
190 /// chooses a best Filter to do its job.
192 /// This recursive scheme ends when the number of Opcodes assigned to the
193 /// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
194 /// the Filter/FilterChooser combo does not know how to distinguish among the
195 /// Opcodes assigned.
197 /// An example of a conflict is
200 /// 111101000.00........00010000....
201 /// 111101000.00........0001........
202 /// 1111010...00........0001........
203 /// 1111010...00....................
204 /// 1111010.........................
205 /// 1111............................
206 /// ................................
207 /// VST4q8a 111101000_00________00010000____
208 /// VST4q8b 111101000_00________00010000____
210 /// The Debug output shows the path that the decoding tree follows to reach the
211 /// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
212 /// even registers, while VST4q8b is a vst4 to double-spaced odd regsisters.
214 /// The encoding info in the .td files does not specify this meta information,
215 /// which could have been used by the decoder to resolve the conflict. The
216 /// decoder could try to decode the even/odd register numbering and assign to
217 /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
218 /// version and return the Opcode since the two have the same Asm format string.
222 const FilterChooser *Owner;// points to the FilterChooser who owns this filter
223 unsigned StartBit; // the starting bit position
224 unsigned NumBits; // number of bits to filter
225 bool Mixed; // a mixed region contains both set and unset bits
227 // Map of well-known segment value to the set of uid's with that value.
228 std::map<uint64_t, std::vector<unsigned> > FilteredInstructions;
230 // Set of uid's with non-constant segment values.
231 std::vector<unsigned> VariableInstructions;
233 // Map of well-known segment value to its delegate.
234 std::map<unsigned, const FilterChooser*> FilterChooserMap;
236 // Number of instructions which fall under FilteredInstructions category.
237 unsigned NumFiltered;
239 // Keeps track of the last opcode in the filtered bucket.
240 unsigned LastOpcFiltered;
243 unsigned getNumFiltered() const { return NumFiltered; }
244 unsigned getSingletonOpc() const {
245 assert(NumFiltered == 1);
246 return LastOpcFiltered;
248 // Return the filter chooser for the group of instructions without constant
250 const FilterChooser &getVariableFC() const {
251 assert(NumFiltered == 1);
252 assert(FilterChooserMap.size() == 1);
253 return *(FilterChooserMap.find((unsigned)-1)->second);
256 Filter(const Filter &f);
257 Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
261 // Divides the decoding task into sub tasks and delegates them to the
262 // inferior FilterChooser's.
264 // A special case arises when there's only one entry in the filtered
265 // instructions. In order to unambiguously decode the singleton, we need to
266 // match the remaining undecoded encoding bits against the singleton.
269 // Emit table entries to decode instructions given a segment or segments of
271 void emitTableEntry(DecoderTableInfo &TableInfo) const;
273 // Returns the number of fanout produced by the filter. More fanout implies
274 // the filter distinguishes more categories of instructions.
275 unsigned usefulness() const;
276 }; // End of class Filter
277 } // End anonymous namespace
279 // These are states of our finite state machines used in FilterChooser's
280 // filterProcessor() which produces the filter candidates to use.
289 /// FilterChooser - FilterChooser chooses the best filter among a set of Filters
290 /// in order to perform the decoding of instructions at the current level.
292 /// Decoding proceeds from the top down. Based on the well-known encoding bits
293 /// of instructions available, FilterChooser builds up the possible Filters that
294 /// can further the task of decoding by distinguishing among the remaining
295 /// candidate instructions.
297 /// Once a filter has been chosen, it is called upon to divide the decoding task
298 /// into sub-tasks and delegates them to its inferior FilterChoosers for further
301 /// It is useful to think of a Filter as governing the switch stmts of the
302 /// decoding tree. And each case is delegated to an inferior FilterChooser to
303 /// decide what further remaining bits to look at.
305 class FilterChooser {
309 // Vector of codegen instructions to choose our filter.
310 const std::vector<const CodeGenInstruction*> &AllInstructions;
312 // Vector of uid's for this filter chooser to work on.
313 const std::vector<unsigned> &Opcodes;
315 // Lookup table for the operand decoding of instructions.
316 const std::map<unsigned, std::vector<OperandInfo> > &Operands;
318 // Vector of candidate filters.
319 std::vector<Filter> Filters;
321 // Array of bit values passed down from our parent.
322 // Set to all BIT_UNFILTERED's for Parent == NULL.
323 std::vector<bit_value_t> FilterBitValues;
325 // Links to the FilterChooser above us in the decoding tree.
326 const FilterChooser *Parent;
328 // Index of the best filter from Filters.
331 // Width of instructions
335 const FixedLenDecoderEmitter *Emitter;
338 FilterChooser(const FilterChooser &FC)
339 : AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
340 Operands(FC.Operands), Filters(FC.Filters),
341 FilterBitValues(FC.FilterBitValues), Parent(FC.Parent),
342 BestIndex(FC.BestIndex), BitWidth(FC.BitWidth),
343 Emitter(FC.Emitter) { }
345 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
346 const std::vector<unsigned> &IDs,
347 const std::map<unsigned, std::vector<OperandInfo> > &Ops,
349 const FixedLenDecoderEmitter *E)
350 : AllInstructions(Insts), Opcodes(IDs), Operands(Ops), Filters(),
351 Parent(NULL), BestIndex(-1), BitWidth(BW), Emitter(E) {
352 for (unsigned i = 0; i < BitWidth; ++i)
353 FilterBitValues.push_back(BIT_UNFILTERED);
358 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
359 const std::vector<unsigned> &IDs,
360 const std::map<unsigned, std::vector<OperandInfo> > &Ops,
361 const std::vector<bit_value_t> &ParentFilterBitValues,
362 const FilterChooser &parent)
363 : AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
364 Filters(), FilterBitValues(ParentFilterBitValues),
365 Parent(&parent), BestIndex(-1), BitWidth(parent.BitWidth),
366 Emitter(parent.Emitter) {
370 unsigned getBitWidth() const { return BitWidth; }
373 // Populates the insn given the uid.
374 void insnWithID(insn_t &Insn, unsigned Opcode) const {
375 BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
377 // We may have a SoftFail bitmask, which specifies a mask where an encoding
378 // may differ from the value in "Inst" and yet still be valid, but the
379 // disassembler should return SoftFail instead of Success.
381 // This is used for marking UNPREDICTABLE instructions in the ARM world.
383 AllInstructions[Opcode]->TheDef->getValueAsBitsInit("SoftFail");
385 for (unsigned i = 0; i < BitWidth; ++i) {
386 if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE)
387 Insn.push_back(BIT_UNSET);
389 Insn.push_back(bitFromBits(Bits, i));
393 // Returns the record name.
394 const std::string &nameWithID(unsigned Opcode) const {
395 return AllInstructions[Opcode]->TheDef->getName();
398 // Populates the field of the insn given the start position and the number of
399 // consecutive bits to scan for.
401 // Returns false if there exists any uninitialized bit value in the range.
402 // Returns true, otherwise.
403 bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
404 unsigned NumBits) const;
406 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
407 /// filter array as a series of chars.
408 void dumpFilterArray(raw_ostream &o,
409 const std::vector<bit_value_t> & filter) const;
411 /// dumpStack - dumpStack traverses the filter chooser chain and calls
412 /// dumpFilterArray on each filter chooser up to the top level one.
413 void dumpStack(raw_ostream &o, const char *prefix) const;
415 Filter &bestFilter() {
416 assert(BestIndex != -1 && "BestIndex not set");
417 return Filters[BestIndex];
420 // Called from Filter::recurse() when singleton exists. For debug purpose.
421 void SingletonExists(unsigned Opc) const;
423 bool PositionFiltered(unsigned i) const {
424 return ValueSet(FilterBitValues[i]);
427 // Calculates the island(s) needed to decode the instruction.
428 // This returns a lit of undecoded bits of an instructions, for example,
429 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
430 // decoded bits in order to verify that the instruction matches the Opcode.
431 unsigned getIslands(std::vector<unsigned> &StartBits,
432 std::vector<unsigned> &EndBits,
433 std::vector<uint64_t> &FieldVals,
434 const insn_t &Insn) const;
436 // Emits code to check the Predicates member of an instruction are true.
437 // Returns true if predicate matches were emitted, false otherwise.
438 bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
441 bool doesOpcodeNeedPredicate(unsigned Opc) const;
442 unsigned getPredicateIndex(DecoderTableInfo &TableInfo, StringRef P) const;
443 void emitPredicateTableEntry(DecoderTableInfo &TableInfo,
446 void emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
449 // Emits table entries to decode the singleton.
450 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
453 // Emits code to decode the singleton, and then to decode the rest.
454 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
455 const Filter &Best) const;
457 void emitBinaryParser(raw_ostream &o, unsigned &Indentation,
458 const OperandInfo &OpInfo) const;
460 void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc) const;
461 unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc) const;
463 // Assign a single filter and run with it.
464 void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed);
466 // reportRegion is a helper function for filterProcessor to mark a region as
467 // eligible for use as a filter region.
468 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
471 // FilterProcessor scans the well-known encoding bits of the instructions and
472 // builds up a list of candidate filters. It chooses the best filter and
473 // recursively descends down the decoding tree.
474 bool filterProcessor(bool AllowMixed, bool Greedy = true);
476 // Decides on the best configuration of filter(s) to use in order to decode
477 // the instructions. A conflict of instructions may occur, in which case we
478 // dump the conflict set to the standard error.
482 // emitTableEntries - Emit state machine entries to decode our share of
484 void emitTableEntries(DecoderTableInfo &TableInfo) const;
486 } // End anonymous namespace
488 ///////////////////////////
490 // Filter Implementation //
492 ///////////////////////////
494 Filter::Filter(const Filter &f)
495 : Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
496 FilteredInstructions(f.FilteredInstructions),
497 VariableInstructions(f.VariableInstructions),
498 FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered),
499 LastOpcFiltered(f.LastOpcFiltered) {
502 Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
504 : Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) {
505 assert(StartBit + NumBits - 1 < Owner->BitWidth);
510 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
513 // Populates the insn given the uid.
514 Owner->insnWithID(Insn, Owner->Opcodes[i]);
517 // Scans the segment for possibly well-specified encoding bits.
518 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
521 // The encoding bits are well-known. Lets add the uid of the
522 // instruction into the bucket keyed off the constant field value.
523 LastOpcFiltered = Owner->Opcodes[i];
524 FilteredInstructions[Field].push_back(LastOpcFiltered);
527 // Some of the encoding bit(s) are unspecified. This contributes to
528 // one additional member of "Variable" instructions.
529 VariableInstructions.push_back(Owner->Opcodes[i]);
533 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
534 && "Filter returns no instruction categories");
538 std::map<unsigned, const FilterChooser*>::iterator filterIterator;
539 for (filterIterator = FilterChooserMap.begin();
540 filterIterator != FilterChooserMap.end();
542 delete filterIterator->second;
546 // Divides the decoding task into sub tasks and delegates them to the
547 // inferior FilterChooser's.
549 // A special case arises when there's only one entry in the filtered
550 // instructions. In order to unambiguously decode the singleton, we need to
551 // match the remaining undecoded encoding bits against the singleton.
552 void Filter::recurse() {
553 std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator;
555 // Starts by inheriting our parent filter chooser's filter bit values.
556 std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
558 if (VariableInstructions.size()) {
559 // Conservatively marks each segment position as BIT_UNSET.
560 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex)
561 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
563 // Delegates to an inferior filter chooser for further processing on this
564 // group of instructions whose segment values are variable.
565 FilterChooserMap.insert(std::pair<unsigned, const FilterChooser*>(
567 new FilterChooser(Owner->AllInstructions,
568 VariableInstructions,
575 // No need to recurse for a singleton filtered instruction.
576 // See also Filter::emit*().
577 if (getNumFiltered() == 1) {
578 //Owner->SingletonExists(LastOpcFiltered);
579 assert(FilterChooserMap.size() == 1);
583 // Otherwise, create sub choosers.
584 for (mapIterator = FilteredInstructions.begin();
585 mapIterator != FilteredInstructions.end();
588 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
589 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) {
590 if (mapIterator->first & (1ULL << bitIndex))
591 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
593 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
596 // Delegates to an inferior filter chooser for further processing on this
597 // category of instructions.
598 FilterChooserMap.insert(std::pair<unsigned, const FilterChooser*>(
600 new FilterChooser(Owner->AllInstructions,
609 static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
611 // Any NumToSkip fixups in the current scope can resolve to the
613 for (FixupList::const_reverse_iterator I = Fixups.rbegin(),
616 // Calculate the distance from the byte following the fixup entry byte
617 // to the destination. The Target is calculated from after the 16-bit
618 // NumToSkip entry itself, so subtract two from the displacement here
619 // to account for that.
620 uint32_t FixupIdx = *I;
621 uint32_t Delta = DestIdx - FixupIdx - 2;
622 // Our NumToSkip entries are 16-bits. Make sure our table isn't too
624 assert(Delta < 65536U && "disassembler decoding table too large!");
625 Table[FixupIdx] = (uint8_t)Delta;
626 Table[FixupIdx + 1] = (uint8_t)(Delta >> 8);
630 // Emit table entries to decode instructions given a segment or segments
632 void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
633 TableInfo.Table.push_back(MCD::OPC_ExtractField);
634 TableInfo.Table.push_back(StartBit);
635 TableInfo.Table.push_back(NumBits);
637 // A new filter entry begins a new scope for fixup resolution.
638 TableInfo.FixupStack.push_back(FixupList());
640 std::map<unsigned, const FilterChooser*>::const_iterator filterIterator;
642 DecoderTable &Table = TableInfo.Table;
644 size_t PrevFilter = 0;
645 bool HasFallthrough = false;
646 for (filterIterator = FilterChooserMap.begin();
647 filterIterator != FilterChooserMap.end();
649 // Field value -1 implies a non-empty set of variable instructions.
650 // See also recurse().
651 if (filterIterator->first == (unsigned)-1) {
652 HasFallthrough = true;
654 // Each scope should always have at least one filter value to check
656 assert(PrevFilter != 0 && "empty filter set!");
657 FixupList &CurScope = TableInfo.FixupStack.back();
658 // Resolve any NumToSkip fixups in the current scope.
659 resolveTableFixups(Table, CurScope, Table.size());
661 PrevFilter = 0; // Don't re-process the filter's fallthrough.
663 Table.push_back(MCD::OPC_FilterValue);
664 // Encode and emit the value to filter against.
666 unsigned Len = encodeULEB128(filterIterator->first, Buffer);
667 Table.insert(Table.end(), Buffer, Buffer + Len);
668 // Reserve space for the NumToSkip entry. We'll backpatch the value
670 PrevFilter = Table.size();
675 // We arrive at a category of instructions with the same segment value.
676 // Now delegate to the sub filter chooser for further decodings.
677 // The case may fallthrough, which happens if the remaining well-known
678 // encoding bits do not match exactly.
679 filterIterator->second->emitTableEntries(TableInfo);
681 // Now that we've emitted the body of the handler, update the NumToSkip
682 // of the filter itself to be able to skip forward when false. Subtract
683 // two as to account for the width of the NumToSkip field itself.
685 uint32_t NumToSkip = Table.size() - PrevFilter - 2;
686 assert(NumToSkip < 65536U && "disassembler decoding table too large!");
687 Table[PrevFilter] = (uint8_t)NumToSkip;
688 Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8);
692 // Any remaining unresolved fixups bubble up to the parent fixup scope.
693 assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!");
694 FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1;
695 FixupScopeList::iterator Dest = Source - 1;
696 Dest->insert(Dest->end(), Source->begin(), Source->end());
697 TableInfo.FixupStack.pop_back();
699 // If there is no fallthrough, then the final filter should get fixed
700 // up according to the enclosing scope rather than the current position.
702 TableInfo.FixupStack.back().push_back(PrevFilter);
705 // Returns the number of fanout produced by the filter. More fanout implies
706 // the filter distinguishes more categories of instructions.
707 unsigned Filter::usefulness() const {
708 if (VariableInstructions.size())
709 return FilteredInstructions.size();
711 return FilteredInstructions.size() + 1;
714 //////////////////////////////////
716 // Filterchooser Implementation //
718 //////////////////////////////////
720 // Emit the decoder state machine table.
721 void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
723 unsigned Indentation,
725 StringRef Namespace) const {
726 OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace
727 << BitWidth << "[] = {\n";
731 // FIXME: We may be able to use the NumToSkip values to recover
732 // appropriate indentation levels.
733 DecoderTable::const_iterator I = Table.begin();
734 DecoderTable::const_iterator E = Table.end();
736 assert (I < E && "incomplete decode table entry!");
738 uint64_t Pos = I - Table.begin();
739 OS << "/* " << Pos << " */";
744 PrintFatalError("invalid decode table opcode");
745 case MCD::OPC_ExtractField: {
747 unsigned Start = *I++;
749 OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
750 << Len << ", // Inst{";
752 OS << (Start + Len - 1) << "-";
753 OS << Start << "} ...\n";
756 case MCD::OPC_FilterValue: {
758 OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
759 // The filter value is ULEB128 encoded.
761 OS << utostr(*I++) << ", ";
762 OS << utostr(*I++) << ", ";
764 // 16-bit numtoskip value.
766 uint32_t NumToSkip = Byte;
767 OS << utostr(Byte) << ", ";
769 OS << utostr(Byte) << ", ";
770 NumToSkip |= Byte << 8;
771 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
774 case MCD::OPC_CheckField: {
776 unsigned Start = *I++;
778 OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
779 << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
780 // ULEB128 encoded field value.
781 for (; *I >= 128; ++I)
782 OS << utostr(*I) << ", ";
783 OS << utostr(*I++) << ", ";
784 // 16-bit numtoskip value.
786 uint32_t NumToSkip = Byte;
787 OS << utostr(Byte) << ", ";
789 OS << utostr(Byte) << ", ";
790 NumToSkip |= Byte << 8;
791 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
794 case MCD::OPC_CheckPredicate: {
796 OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
797 for (; *I >= 128; ++I)
798 OS << utostr(*I) << ", ";
799 OS << utostr(*I++) << ", ";
801 // 16-bit numtoskip value.
803 uint32_t NumToSkip = Byte;
804 OS << utostr(Byte) << ", ";
806 OS << utostr(Byte) << ", ";
807 NumToSkip |= Byte << 8;
808 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
811 case MCD::OPC_Decode: {
813 // Extract the ULEB128 encoded Opcode to a buffer.
814 uint8_t Buffer[8], *p = Buffer;
815 while ((*p++ = *I++) >= 128)
816 assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer)
817 && "ULEB128 value too large!");
818 // Decode the Opcode value.
819 unsigned Opc = decodeULEB128(Buffer);
820 OS.indent(Indentation) << "MCD::OPC_Decode, ";
821 for (p = Buffer; *p >= 128; ++p)
822 OS << utostr(*p) << ", ";
823 OS << utostr(*p) << ", ";
826 for (; *I >= 128; ++I)
827 OS << utostr(*I) << ", ";
828 OS << utostr(*I++) << ", ";
831 << NumberedInstructions->at(Opc)->TheDef->getName() << "\n";
834 case MCD::OPC_SoftFail: {
836 OS.indent(Indentation) << "MCD::OPC_SoftFail";
841 OS << ", " << utostr(*I);
842 Value += (*I & 0x7f) << Shift;
844 } while (*I++ >= 128);
846 OS << " /* 0x" << utohexstr(Value) << " */";
851 OS << ", " << utostr(*I);
852 Value += (*I & 0x7f) << Shift;
854 } while (*I++ >= 128);
856 OS << " /* 0x" << utohexstr(Value) << " */";
860 case MCD::OPC_Fail: {
862 OS.indent(Indentation) << "MCD::OPC_Fail,\n";
867 OS.indent(Indentation) << "0\n";
871 OS.indent(Indentation) << "};\n\n";
874 void FixedLenDecoderEmitter::
875 emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
876 unsigned Indentation) const {
877 // The predicate function is just a big switch statement based on the
878 // input predicate index.
879 OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
880 << "uint64_t Bits) {\n";
882 if (!Predicates.empty()) {
883 OS.indent(Indentation) << "switch (Idx) {\n";
884 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
886 for (PredicateSet::const_iterator I = Predicates.begin(), E = Predicates.end();
887 I != E; ++I, ++Index) {
888 OS.indent(Indentation) << "case " << Index << ":\n";
889 OS.indent(Indentation+2) << "return (" << *I << ");\n";
891 OS.indent(Indentation) << "}\n";
893 // No case statement to emit
894 OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
897 OS.indent(Indentation) << "}\n\n";
900 void FixedLenDecoderEmitter::
901 emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
902 unsigned Indentation) const {
903 // The decoder function is just a big switch statement based on the
904 // input decoder index.
905 OS.indent(Indentation) << "template<typename InsnType>\n";
906 OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
907 << " unsigned Idx, InsnType insn, MCInst &MI,\n";
908 OS.indent(Indentation) << " uint64_t "
909 << "Address, const void *Decoder) {\n";
911 OS.indent(Indentation) << "InsnType tmp;\n";
912 OS.indent(Indentation) << "switch (Idx) {\n";
913 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
915 for (DecoderSet::const_iterator I = Decoders.begin(), E = Decoders.end();
916 I != E; ++I, ++Index) {
917 OS.indent(Indentation) << "case " << Index << ":\n";
919 OS.indent(Indentation+2) << "return S;\n";
921 OS.indent(Indentation) << "}\n";
923 OS.indent(Indentation) << "}\n\n";
926 // Populates the field of the insn given the start position and the number of
927 // consecutive bits to scan for.
929 // Returns false if and on the first uninitialized bit value encountered.
930 // Returns true, otherwise.
931 bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
932 unsigned StartBit, unsigned NumBits) const {
935 for (unsigned i = 0; i < NumBits; ++i) {
936 if (Insn[StartBit + i] == BIT_UNSET)
939 if (Insn[StartBit + i] == BIT_TRUE)
940 Field = Field | (1ULL << i);
946 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
947 /// filter array as a series of chars.
948 void FilterChooser::dumpFilterArray(raw_ostream &o,
949 const std::vector<bit_value_t> &filter) const {
950 for (unsigned bitIndex = BitWidth; bitIndex > 0; bitIndex--) {
951 switch (filter[bitIndex - 1]) {
968 /// dumpStack - dumpStack traverses the filter chooser chain and calls
969 /// dumpFilterArray on each filter chooser up to the top level one.
970 void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const {
971 const FilterChooser *current = this;
975 dumpFilterArray(o, current->FilterBitValues);
977 current = current->Parent;
981 // Called from Filter::recurse() when singleton exists. For debug purpose.
982 void FilterChooser::SingletonExists(unsigned Opc) const {
984 insnWithID(Insn0, Opc);
986 errs() << "Singleton exists: " << nameWithID(Opc)
987 << " with its decoding dominating ";
988 for (unsigned i = 0; i < Opcodes.size(); ++i) {
989 if (Opcodes[i] == Opc) continue;
990 errs() << nameWithID(Opcodes[i]) << ' ';
994 dumpStack(errs(), "\t\t");
995 for (unsigned i = 0; i < Opcodes.size(); ++i) {
996 const std::string &Name = nameWithID(Opcodes[i]);
998 errs() << '\t' << Name << " ";
1000 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1005 // Calculates the island(s) needed to decode the instruction.
1006 // This returns a list of undecoded bits of an instructions, for example,
1007 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
1008 // decoded bits in order to verify that the instruction matches the Opcode.
1009 unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
1010 std::vector<unsigned> &EndBits,
1011 std::vector<uint64_t> &FieldVals,
1012 const insn_t &Insn) const {
1013 unsigned Num, BitNo;
1016 uint64_t FieldVal = 0;
1019 // 1: Water (the bit value does not affect decoding)
1020 // 2: Island (well-known bit value needed for decoding)
1024 for (unsigned i = 0; i < BitWidth; ++i) {
1025 Val = Value(Insn[i]);
1026 bool Filtered = PositionFiltered(i);
1028 default: llvm_unreachable("Unreachable code!");
1031 if (Filtered || Val == -1)
1032 State = 1; // Still in Water
1034 State = 2; // Into the Island
1036 StartBits.push_back(i);
1041 if (Filtered || Val == -1) {
1042 State = 1; // Into the Water
1043 EndBits.push_back(i - 1);
1044 FieldVals.push_back(FieldVal);
1047 State = 2; // Still in Island
1049 FieldVal = FieldVal | Val << BitNo;
1054 // If we are still in Island after the loop, do some housekeeping.
1056 EndBits.push_back(BitWidth - 1);
1057 FieldVals.push_back(FieldVal);
1061 assert(StartBits.size() == Num && EndBits.size() == Num &&
1062 FieldVals.size() == Num);
1066 void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
1067 const OperandInfo &OpInfo) const {
1068 const std::string &Decoder = OpInfo.Decoder;
1070 if (OpInfo.numFields() == 1) {
1071 OperandInfo::const_iterator OI = OpInfo.begin();
1072 o.indent(Indentation) << "tmp = fieldFromInstruction"
1073 << "(insn, " << OI->Base << ", " << OI->Width
1076 o.indent(Indentation) << "tmp = 0;\n";
1077 for (OperandInfo::const_iterator OI = OpInfo.begin(), OE = OpInfo.end();
1079 o.indent(Indentation) << "tmp |= (fieldFromInstruction"
1080 << "(insn, " << OI->Base << ", " << OI->Width
1081 << ") << " << OI->Offset << ");\n";
1086 o.indent(Indentation) << Emitter->GuardPrefix << Decoder
1087 << "(MI, tmp, Address, Decoder)"
1088 << Emitter->GuardPostfix << "\n";
1090 o.indent(Indentation) << "MI.addOperand(MCOperand::CreateImm(tmp));\n";
1094 void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
1095 unsigned Opc) const {
1096 std::map<unsigned, std::vector<OperandInfo> >::const_iterator OpIter =
1098 const std::vector<OperandInfo>& InsnOperands = OpIter->second;
1099 for (std::vector<OperandInfo>::const_iterator
1100 I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
1101 // If a custom instruction decoder was specified, use that.
1102 if (I->numFields() == 0 && I->Decoder.size()) {
1103 OS.indent(Indentation) << Emitter->GuardPrefix << I->Decoder
1104 << "(MI, insn, Address, Decoder)"
1105 << Emitter->GuardPostfix << "\n";
1109 emitBinaryParser(OS, Indentation, *I);
1113 unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
1114 unsigned Opc) const {
1115 // Build up the predicate string.
1116 SmallString<256> Decoder;
1117 // FIXME: emitDecoder() function can take a buffer directly rather than
1119 raw_svector_ostream S(Decoder);
1121 emitDecoder(S, I, Opc);
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 easilly enough with a map back to the actual string. That's
1128 // overkill for now, though.
1130 // Make sure the predicate is in the table.
1131 Decoders.insert(Decoder.str());
1132 // Now figure out the index for when we write out the table.
1133 DecoderSet::const_iterator P = std::find(Decoders.begin(),
1136 return (unsigned)(P - Decoders.begin());
1139 static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
1140 const std::string &PredicateNamespace) {
1142 o << "!(Bits & " << PredicateNamespace << "::"
1143 << str.slice(1,str.size()) << ")";
1145 o << "(Bits & " << PredicateNamespace << "::" << str << ")";
1148 bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
1149 unsigned Opc) const {
1150 ListInit *Predicates =
1151 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
1152 for (unsigned i = 0; i < Predicates->getSize(); ++i) {
1153 Record *Pred = Predicates->getElementAsRecord(i);
1154 if (!Pred->getValue("AssemblerMatcherPredicate"))
1157 std::string P = Pred->getValueAsString("AssemblerCondString");
1166 std::pair<StringRef, StringRef> pairs = SR.split(',');
1167 while (pairs.second.size()) {
1168 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1170 pairs = pairs.second.split(',');
1172 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1174 return Predicates->getSize() > 0;
1177 bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const {
1178 ListInit *Predicates =
1179 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
1180 for (unsigned i = 0; i < Predicates->getSize(); ++i) {
1181 Record *Pred = Predicates->getElementAsRecord(i);
1182 if (!Pred->getValue("AssemblerMatcherPredicate"))
1185 std::string P = Pred->getValueAsString("AssemblerCondString");
1195 unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo,
1196 StringRef Predicate) const {
1197 // Using the full predicate string as the key value here is a bit
1198 // heavyweight, but is effective. If the string comparisons become a
1199 // performance concern, we can implement a mangling of the predicate
1200 // data easilly enough with a map back to the actual string. That's
1201 // overkill for now, though.
1203 // Make sure the predicate is in the table.
1204 TableInfo.Predicates.insert(Predicate.str());
1205 // Now figure out the index for when we write out the table.
1206 PredicateSet::const_iterator P = std::find(TableInfo.Predicates.begin(),
1207 TableInfo.Predicates.end(),
1209 return (unsigned)(P - TableInfo.Predicates.begin());
1212 void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
1213 unsigned Opc) const {
1214 if (!doesOpcodeNeedPredicate(Opc))
1217 // Build up the predicate string.
1218 SmallString<256> Predicate;
1219 // FIXME: emitPredicateMatch() functions can take a buffer directly rather
1221 raw_svector_ostream PS(Predicate);
1223 emitPredicateMatch(PS, I, Opc);
1225 // Figure out the index into the predicate table for the predicate just
1227 unsigned PIdx = getPredicateIndex(TableInfo, PS.str());
1228 SmallString<16> PBytes;
1229 raw_svector_ostream S(PBytes);
1230 encodeULEB128(PIdx, S);
1233 TableInfo.Table.push_back(MCD::OPC_CheckPredicate);
1235 for (unsigned i = 0, e = PBytes.size(); i != e; ++i)
1236 TableInfo.Table.push_back(PBytes[i]);
1237 // Push location for NumToSkip backpatching.
1238 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1239 TableInfo.Table.push_back(0);
1240 TableInfo.Table.push_back(0);
1243 void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
1244 unsigned Opc) const {
1246 AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail");
1247 if (!SFBits) return;
1248 BitsInit *InstBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("Inst");
1250 APInt PositiveMask(BitWidth, 0ULL);
1251 APInt NegativeMask(BitWidth, 0ULL);
1252 for (unsigned i = 0; i < BitWidth; ++i) {
1253 bit_value_t B = bitFromBits(*SFBits, i);
1254 bit_value_t IB = bitFromBits(*InstBits, i);
1256 if (B != BIT_TRUE) continue;
1260 // The bit is meant to be false, so emit a check to see if it is true.
1261 PositiveMask.setBit(i);
1264 // The bit is meant to be true, so emit a check to see if it is false.
1265 NegativeMask.setBit(i);
1268 // The bit is not set; this must be an error!
1269 StringRef Name = AllInstructions[Opc]->TheDef->getName();
1270 errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in " << Name
1271 << " is set but Inst{" << i << "} is unset!\n"
1272 << " - You can only mark a bit as SoftFail if it is fully defined"
1273 << " (1/0 - not '?') in Inst\n";
1278 bool NeedPositiveMask = PositiveMask.getBoolValue();
1279 bool NeedNegativeMask = NegativeMask.getBoolValue();
1281 if (!NeedPositiveMask && !NeedNegativeMask)
1284 TableInfo.Table.push_back(MCD::OPC_SoftFail);
1286 SmallString<16> MaskBytes;
1287 raw_svector_ostream S(MaskBytes);
1288 if (NeedPositiveMask) {
1289 encodeULEB128(PositiveMask.getZExtValue(), S);
1291 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
1292 TableInfo.Table.push_back(MaskBytes[i]);
1294 TableInfo.Table.push_back(0);
1295 if (NeedNegativeMask) {
1298 encodeULEB128(NegativeMask.getZExtValue(), S);
1300 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
1301 TableInfo.Table.push_back(MaskBytes[i]);
1303 TableInfo.Table.push_back(0);
1306 // Emits table entries to decode the singleton.
1307 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1308 unsigned Opc) const {
1309 std::vector<unsigned> StartBits;
1310 std::vector<unsigned> EndBits;
1311 std::vector<uint64_t> FieldVals;
1313 insnWithID(Insn, Opc);
1315 // Look for islands of undecoded bits of the singleton.
1316 getIslands(StartBits, EndBits, FieldVals, Insn);
1318 unsigned Size = StartBits.size();
1320 // Emit the predicate table entry if one is needed.
1321 emitPredicateTableEntry(TableInfo, Opc);
1323 // Check any additional encoding fields needed.
1324 for (unsigned I = Size; I != 0; --I) {
1325 unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1;
1326 TableInfo.Table.push_back(MCD::OPC_CheckField);
1327 TableInfo.Table.push_back(StartBits[I-1]);
1328 TableInfo.Table.push_back(NumBits);
1329 uint8_t Buffer[8], *p;
1330 encodeULEB128(FieldVals[I-1], Buffer);
1331 for (p = Buffer; *p >= 128 ; ++p)
1332 TableInfo.Table.push_back(*p);
1333 TableInfo.Table.push_back(*p);
1334 // Push location for NumToSkip backpatching.
1335 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1336 // The fixup is always 16-bits, so go ahead and allocate the space
1337 // in the table so all our relative position calculations work OK even
1338 // before we fully resolve the real value here.
1339 TableInfo.Table.push_back(0);
1340 TableInfo.Table.push_back(0);
1343 // Check for soft failure of the match.
1344 emitSoftFailTableEntry(TableInfo, Opc);
1346 TableInfo.Table.push_back(MCD::OPC_Decode);
1347 uint8_t Buffer[8], *p;
1348 encodeULEB128(Opc, Buffer);
1349 for (p = Buffer; *p >= 128 ; ++p)
1350 TableInfo.Table.push_back(*p);
1351 TableInfo.Table.push_back(*p);
1353 unsigned DIdx = getDecoderIndex(TableInfo.Decoders, Opc);
1354 SmallString<16> Bytes;
1355 raw_svector_ostream S(Bytes);
1356 encodeULEB128(DIdx, S);
1360 for (unsigned i = 0, e = Bytes.size(); i != e; ++i)
1361 TableInfo.Table.push_back(Bytes[i]);
1364 // Emits table entries to decode the singleton, and then to decode the rest.
1365 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1366 const Filter &Best) const {
1367 unsigned Opc = Best.getSingletonOpc();
1369 // complex singletons need predicate checks from the first singleton
1370 // to refer forward to the variable filterchooser that follows.
1371 TableInfo.FixupStack.push_back(FixupList());
1373 emitSingletonTableEntry(TableInfo, Opc);
1375 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
1376 TableInfo.Table.size());
1377 TableInfo.FixupStack.pop_back();
1379 Best.getVariableFC().emitTableEntries(TableInfo);
1383 // Assign a single filter and run with it. Top level API client can initialize
1384 // with a single filter to start the filtering process.
1385 void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit,
1388 Filter F(*this, startBit, numBit, true);
1389 Filters.push_back(F);
1390 BestIndex = 0; // Sole Filter instance to choose from.
1391 bestFilter().recurse();
1394 // reportRegion is a helper function for filterProcessor to mark a region as
1395 // eligible for use as a filter region.
1396 void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1397 unsigned BitIndex, bool AllowMixed) {
1398 if (RA == ATTR_MIXED && AllowMixed)
1399 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true));
1400 else if (RA == ATTR_ALL_SET && !AllowMixed)
1401 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false));
1404 // FilterProcessor scans the well-known encoding bits of the instructions and
1405 // builds up a list of candidate filters. It chooses the best filter and
1406 // recursively descends down the decoding tree.
1407 bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1410 unsigned numInstructions = Opcodes.size();
1412 assert(numInstructions && "Filter created with no instructions");
1414 // No further filtering is necessary.
1415 if (numInstructions == 1)
1418 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1419 // instructions is 3.
1420 if (AllowMixed && !Greedy) {
1421 assert(numInstructions == 3);
1423 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1424 std::vector<unsigned> StartBits;
1425 std::vector<unsigned> EndBits;
1426 std::vector<uint64_t> FieldVals;
1429 insnWithID(Insn, Opcodes[i]);
1431 // Look for islands of undecoded bits of any instruction.
1432 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1433 // Found an instruction with island(s). Now just assign a filter.
1434 runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true);
1442 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1443 // The automaton consumes the corresponding bit from each
1446 // Input symbols: 0, 1, and _ (unset).
1447 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1448 // Initial state: NONE.
1450 // (NONE) ------- [01] -> (ALL_SET)
1451 // (NONE) ------- _ ----> (ALL_UNSET)
1452 // (ALL_SET) ---- [01] -> (ALL_SET)
1453 // (ALL_SET) ---- _ ----> (MIXED)
1454 // (ALL_UNSET) -- [01] -> (MIXED)
1455 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1456 // (MIXED) ------ . ----> (MIXED)
1457 // (FILTERED)---- . ----> (FILTERED)
1459 std::vector<bitAttr_t> bitAttrs;
1461 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1462 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1463 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
1464 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1465 FilterBitValues[BitIndex] == BIT_FALSE)
1466 bitAttrs.push_back(ATTR_FILTERED);
1468 bitAttrs.push_back(ATTR_NONE);
1470 for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
1473 insnWithID(insn, Opcodes[InsnIndex]);
1475 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
1476 switch (bitAttrs[BitIndex]) {
1478 if (insn[BitIndex] == BIT_UNSET)
1479 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1481 bitAttrs[BitIndex] = ATTR_ALL_SET;
1484 if (insn[BitIndex] == BIT_UNSET)
1485 bitAttrs[BitIndex] = ATTR_MIXED;
1487 case ATTR_ALL_UNSET:
1488 if (insn[BitIndex] != BIT_UNSET)
1489 bitAttrs[BitIndex] = ATTR_MIXED;
1498 // The regionAttr automaton consumes the bitAttrs automatons' state,
1499 // lowest-to-highest.
1501 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1502 // States: NONE, ALL_SET, MIXED
1503 // Initial state: NONE
1505 // (NONE) ----- F --> (NONE)
1506 // (NONE) ----- S --> (ALL_SET) ; and set region start
1507 // (NONE) ----- U --> (NONE)
1508 // (NONE) ----- M --> (MIXED) ; and set region start
1509 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1510 // (ALL_SET) -- S --> (ALL_SET)
1511 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1512 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1513 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1514 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1515 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1516 // (MIXED) ---- M --> (MIXED)
1518 bitAttr_t RA = ATTR_NONE;
1519 unsigned StartBit = 0;
1521 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
1522 bitAttr_t bitAttr = bitAttrs[BitIndex];
1524 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1532 StartBit = BitIndex;
1535 case ATTR_ALL_UNSET:
1538 StartBit = BitIndex;
1542 llvm_unreachable("Unexpected bitAttr!");
1548 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1553 case ATTR_ALL_UNSET:
1554 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1558 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1559 StartBit = BitIndex;
1563 llvm_unreachable("Unexpected bitAttr!");
1569 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1570 StartBit = BitIndex;
1574 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1575 StartBit = BitIndex;
1578 case ATTR_ALL_UNSET:
1579 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1585 llvm_unreachable("Unexpected bitAttr!");
1588 case ATTR_ALL_UNSET:
1589 llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
1591 llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
1595 // At the end, if we're still in ALL_SET or MIXED states, report a region
1602 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1604 case ATTR_ALL_UNSET:
1607 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1611 // We have finished with the filter processings. Now it's time to choose
1612 // the best performing filter.
1614 bool AllUseless = true;
1615 unsigned BestScore = 0;
1617 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1618 unsigned Usefulness = Filters[i].usefulness();
1623 if (Usefulness > BestScore) {
1625 BestScore = Usefulness;
1630 bestFilter().recurse();
1633 } // end of FilterChooser::filterProcessor(bool)
1635 // Decides on the best configuration of filter(s) to use in order to decode
1636 // the instructions. A conflict of instructions may occur, in which case we
1637 // dump the conflict set to the standard error.
1638 void FilterChooser::doFilter() {
1639 unsigned Num = Opcodes.size();
1640 assert(Num && "FilterChooser created with no instructions");
1642 // Try regions of consecutive known bit values first.
1643 if (filterProcessor(false))
1646 // Then regions of mixed bits (both known and unitialized bit values allowed).
1647 if (filterProcessor(true))
1650 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1651 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1652 // well-known encoding pattern. In such case, we backtrack and scan for the
1653 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1654 if (Num == 3 && filterProcessor(true, false))
1657 // If we come to here, the instruction decoding has failed.
1658 // Set the BestIndex to -1 to indicate so.
1662 // emitTableEntries - Emit state machine entries to decode our share of
1664 void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
1665 if (Opcodes.size() == 1) {
1666 // There is only one instruction in the set, which is great!
1667 // Call emitSingletonDecoder() to see whether there are any remaining
1669 emitSingletonTableEntry(TableInfo, Opcodes[0]);
1673 // Choose the best filter to do the decodings!
1674 if (BestIndex != -1) {
1675 const Filter &Best = Filters[BestIndex];
1676 if (Best.getNumFiltered() == 1)
1677 emitSingletonTableEntry(TableInfo, Best);
1679 Best.emitTableEntry(TableInfo);
1683 // We don't know how to decode these instructions! Dump the
1684 // conflict set and bail.
1686 // Print out useful conflict information for postmortem analysis.
1687 errs() << "Decoding Conflict:\n";
1689 dumpStack(errs(), "\t\t");
1691 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1692 const std::string &Name = nameWithID(Opcodes[i]);
1694 errs() << '\t' << Name << " ";
1696 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1701 static bool populateInstruction(const CodeGenInstruction &CGI, unsigned Opc,
1702 std::map<unsigned, std::vector<OperandInfo> > &Operands){
1703 const Record &Def = *CGI.TheDef;
1704 // If all the bit positions are not specified; do not decode this instruction.
1705 // We are bound to fail! For proper disassembly, the well-known encoding bits
1706 // of the instruction must be fully specified.
1708 // This also removes pseudo instructions from considerations of disassembly,
1709 // which is a better design and less fragile than the name matchings.
1710 // Ignore "asm parser only" instructions.
1711 if (Def.getValueAsBit("isAsmParserOnly") ||
1712 Def.getValueAsBit("isCodeGenOnly"))
1715 BitsInit &Bits = getBitsField(Def, "Inst");
1716 if (Bits.allInComplete()) return false;
1718 std::vector<OperandInfo> InsnOperands;
1720 // If the instruction has specified a custom decoding hook, use that instead
1721 // of trying to auto-generate the decoder.
1722 std::string InstDecoder = Def.getValueAsString("DecoderMethod");
1723 if (InstDecoder != "") {
1724 InsnOperands.push_back(OperandInfo(InstDecoder));
1725 Operands[Opc] = InsnOperands;
1729 // Generate a description of the operand of the instruction that we know
1730 // how to decode automatically.
1731 // FIXME: We'll need to have a way to manually override this as needed.
1733 // Gather the outputs/inputs of the instruction, so we can find their
1734 // positions in the encoding. This assumes for now that they appear in the
1735 // MCInst in the order that they're listed.
1736 std::vector<std::pair<Init*, std::string> > InOutOperands;
1737 DagInit *Out = Def.getValueAsDag("OutOperandList");
1738 DagInit *In = Def.getValueAsDag("InOperandList");
1739 for (unsigned i = 0; i < Out->getNumArgs(); ++i)
1740 InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i)));
1741 for (unsigned i = 0; i < In->getNumArgs(); ++i)
1742 InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i)));
1744 // Search for tied operands, so that we can correctly instantiate
1745 // operands that are not explicitly represented in the encoding.
1746 std::map<std::string, std::string> TiedNames;
1747 for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
1748 int tiedTo = CGI.Operands[i].getTiedRegister();
1750 TiedNames[InOutOperands[i].second] = InOutOperands[tiedTo].second;
1751 TiedNames[InOutOperands[tiedTo].second] = InOutOperands[i].second;
1755 // For each operand, see if we can figure out where it is encoded.
1756 for (std::vector<std::pair<Init*, std::string> >::const_iterator
1757 NI = InOutOperands.begin(), NE = InOutOperands.end(); NI != NE; ++NI) {
1758 std::string Decoder = "";
1760 // At this point, we can locate the field, but we need to know how to
1761 // interpret it. As a first step, require the target to provide callbacks
1762 // for decoding register classes.
1763 // FIXME: This need to be extended to handle instructions with custom
1764 // decoder methods, and operands with (simple) MIOperandInfo's.
1765 TypedInit *TI = cast<TypedInit>(NI->first);
1766 RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
1767 Record *TypeRecord = Type->getRecord();
1769 if (TypeRecord->isSubClassOf("RegisterOperand"))
1770 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1771 if (TypeRecord->isSubClassOf("RegisterClass")) {
1772 Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
1776 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
1777 StringInit *String = DecoderString ?
1778 dyn_cast<StringInit>(DecoderString->getValue()) : 0;
1779 if (!isReg && String && String->getValue() != "")
1780 Decoder = String->getValue();
1782 OperandInfo OpInfo(Decoder);
1783 unsigned Base = ~0U;
1785 unsigned Offset = 0;
1787 for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
1789 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1791 Var = dyn_cast<VarInit>(BI->getBitVar());
1793 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1797 OpInfo.addField(Base, Width, Offset);
1805 if (Var->getName() != NI->second &&
1806 Var->getName() != TiedNames[NI->second]) {
1808 OpInfo.addField(Base, Width, Offset);
1819 Offset = BI ? BI->getBitNum() : 0;
1820 } else if (BI && BI->getBitNum() != Offset + Width) {
1821 OpInfo.addField(Base, Width, Offset);
1824 Offset = BI->getBitNum();
1831 OpInfo.addField(Base, Width, Offset);
1833 if (OpInfo.numFields() > 0)
1834 InsnOperands.push_back(OpInfo);
1837 Operands[Opc] = InsnOperands;
1842 // Dumps the instruction encoding bits.
1843 dumpBits(errs(), Bits);
1847 // Dumps the list of operand info.
1848 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1849 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
1850 const std::string &OperandName = Info.Name;
1851 const Record &OperandDef = *Info.Rec;
1853 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
1861 // emitFieldFromInstruction - Emit the templated helper function
1862 // fieldFromInstruction().
1863 static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
1864 OS << "// Helper function for extracting fields from encoded instructions.\n"
1865 << "template<typename InsnType>\n"
1866 << "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n"
1867 << " unsigned numBits) {\n"
1868 << " assert(startBit + numBits <= (sizeof(InsnType)*8) &&\n"
1869 << " \"Instruction field out of bounds!\");\n"
1870 << " InsnType fieldMask;\n"
1871 << " if (numBits == sizeof(InsnType)*8)\n"
1872 << " fieldMask = (InsnType)(-1LL);\n"
1874 << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
1875 << " return (insn & fieldMask) >> startBit;\n"
1879 // emitDecodeInstruction - Emit the templated helper function
1880 // decodeInstruction().
1881 static void emitDecodeInstruction(formatted_raw_ostream &OS) {
1882 OS << "template<typename InsnType>\n"
1883 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,\n"
1884 << " InsnType insn, uint64_t Address,\n"
1885 << " const void *DisAsm,\n"
1886 << " const MCSubtargetInfo &STI) {\n"
1887 << " uint64_t Bits = STI.getFeatureBits();\n"
1889 << " const uint8_t *Ptr = DecodeTable;\n"
1890 << " uint32_t CurFieldValue = 0;\n"
1891 << " DecodeStatus S = MCDisassembler::Success;\n"
1893 << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
1894 << " switch (*Ptr) {\n"
1896 << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
1897 << " return MCDisassembler::Fail;\n"
1898 << " case MCD::OPC_ExtractField: {\n"
1899 << " unsigned Start = *++Ptr;\n"
1900 << " unsigned Len = *++Ptr;\n"
1902 << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
1903 << " DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << \", \"\n"
1904 << " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
1907 << " case MCD::OPC_FilterValue: {\n"
1908 << " // Decode the field value.\n"
1909 << " unsigned Len;\n"
1910 << " InsnType Val = decodeULEB128(++Ptr, &Len);\n"
1912 << " // NumToSkip is a plain 16-bit integer.\n"
1913 << " unsigned NumToSkip = *Ptr++;\n"
1914 << " NumToSkip |= (*Ptr++) << 8;\n"
1916 << " // Perform the filter operation.\n"
1917 << " if (Val != CurFieldValue)\n"
1918 << " Ptr += NumToSkip;\n"
1919 << " DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << \", \" << NumToSkip\n"
1920 << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" : \"PASS:\")\n"
1921 << " << \" continuing at \" << (Ptr - DecodeTable) << \"\\n\");\n"
1925 << " case MCD::OPC_CheckField: {\n"
1926 << " unsigned Start = *++Ptr;\n"
1927 << " unsigned Len = *++Ptr;\n"
1928 << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
1929 << " // Decode the field value.\n"
1930 << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
1932 << " // NumToSkip is a plain 16-bit integer.\n"
1933 << " unsigned NumToSkip = *Ptr++;\n"
1934 << " NumToSkip |= (*Ptr++) << 8;\n"
1936 << " // If the actual and expected values don't match, skip.\n"
1937 << " if (ExpectedValue != FieldValue)\n"
1938 << " Ptr += NumToSkip;\n"
1939 << " DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << \", \"\n"
1940 << " << Len << \", \" << ExpectedValue << \", \" << NumToSkip\n"
1941 << " << \"): FieldValue = \" << FieldValue << \", ExpectedValue = \"\n"
1942 << " << ExpectedValue << \": \"\n"
1943 << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : \"FAIL\\n\"));\n"
1946 << " case MCD::OPC_CheckPredicate: {\n"
1947 << " unsigned Len;\n"
1948 << " // Decode the Predicate Index value.\n"
1949 << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
1951 << " // NumToSkip is a plain 16-bit integer.\n"
1952 << " unsigned NumToSkip = *Ptr++;\n"
1953 << " NumToSkip |= (*Ptr++) << 8;\n"
1954 << " // Check the predicate.\n"
1956 << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
1957 << " Ptr += NumToSkip;\n"
1959 << " DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx << \"): \"\n"
1960 << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
1964 << " case MCD::OPC_Decode: {\n"
1965 << " unsigned Len;\n"
1966 << " // Decode the Opcode value.\n"
1967 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
1969 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
1971 << " DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
1972 << " << \", using decoder \" << DecodeIdx << \"\\n\" );\n"
1973 << " DEBUG(dbgs() << \"----- DECODE SUCCESSFUL -----\\n\");\n"
1975 << " MI.setOpcode(Opc);\n"
1976 << " return decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm);\n"
1978 << " case MCD::OPC_SoftFail: {\n"
1979 << " // Decode the mask values.\n"
1980 << " unsigned Len;\n"
1981 << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n"
1983 << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n"
1985 << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n"
1987 << " S = MCDisassembler::SoftFail;\n"
1988 << " DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? \"FAIL\\n\":\"PASS\\n\"));\n"
1991 << " case MCD::OPC_Fail: {\n"
1992 << " DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
1993 << " return MCDisassembler::Fail;\n"
1997 << " llvm_unreachable(\"bogosity detected in disassembler state machine!\");\n"
2001 // Emits disassembler code for instruction decoding.
2002 void FixedLenDecoderEmitter::run(raw_ostream &o) {
2003 formatted_raw_ostream OS(o);
2004 OS << "#include \"llvm/MC/MCInst.h\"\n";
2005 OS << "#include \"llvm/Support/Debug.h\"\n";
2006 OS << "#include \"llvm/Support/DataTypes.h\"\n";
2007 OS << "#include \"llvm/Support/LEB128.h\"\n";
2008 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
2009 OS << "#include <assert.h>\n";
2011 OS << "namespace llvm {\n\n";
2013 emitFieldFromInstruction(OS);
2015 // Parameterize the decoders based on namespace and instruction width.
2016 NumberedInstructions = &Target.getInstructionsByEnumValue();
2017 std::map<std::pair<std::string, unsigned>,
2018 std::vector<unsigned> > OpcMap;
2019 std::map<unsigned, std::vector<OperandInfo> > Operands;
2021 for (unsigned i = 0; i < NumberedInstructions->size(); ++i) {
2022 const CodeGenInstruction *Inst = NumberedInstructions->at(i);
2023 const Record *Def = Inst->TheDef;
2024 unsigned Size = Def->getValueAsInt("Size");
2025 if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
2026 Def->getValueAsBit("isPseudo") ||
2027 Def->getValueAsBit("isAsmParserOnly") ||
2028 Def->getValueAsBit("isCodeGenOnly"))
2031 std::string DecoderNamespace = Def->getValueAsString("DecoderNamespace");
2034 if (populateInstruction(*Inst, i, Operands)) {
2035 OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
2040 DecoderTableInfo TableInfo;
2041 std::set<unsigned> Sizes;
2042 for (std::map<std::pair<std::string, unsigned>,
2043 std::vector<unsigned> >::const_iterator
2044 I = OpcMap.begin(), E = OpcMap.end(); I != E; ++I) {
2045 // Emit the decoder for this namespace+width combination.
2046 FilterChooser FC(*NumberedInstructions, I->second, Operands,
2047 8*I->first.second, this);
2049 // The decode table is cleared for each top level decoder function. The
2050 // predicates and decoders themselves, however, are shared across all
2051 // decoders to give more opportunities for uniqueing.
2052 TableInfo.Table.clear();
2053 TableInfo.FixupStack.clear();
2054 TableInfo.Table.reserve(16384);
2055 TableInfo.FixupStack.push_back(FixupList());
2056 FC.emitTableEntries(TableInfo);
2057 // Any NumToSkip fixups in the top level scope can resolve to the
2058 // OPC_Fail at the end of the table.
2059 assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
2060 // Resolve any NumToSkip fixups in the current scope.
2061 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
2062 TableInfo.Table.size());
2063 TableInfo.FixupStack.clear();
2065 TableInfo.Table.push_back(MCD::OPC_Fail);
2067 // Print the table to the output stream.
2068 emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), I->first.first);
2072 // Emit the predicate function.
2073 emitPredicateFunction(OS, TableInfo.Predicates, 0);
2075 // Emit the decoder function.
2076 emitDecoderFunction(OS, TableInfo.Decoders, 0);
2078 // Emit the main entry point for the decoder, decodeInstruction().
2079 emitDecodeInstruction(OS);
2081 OS << "\n} // End llvm namespace\n";
2086 void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
2087 std::string PredicateNamespace,
2088 std::string GPrefix,
2089 std::string GPostfix,
2093 FixedLenDecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix,
2094 ROK, RFail, L).run(OS);
2097 } // End llvm namespace