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 OS.indent(Indentation) << "switch (Idx) {\n";
883 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
885 for (PredicateSet::const_iterator I = Predicates.begin(), E = Predicates.end();
886 I != E; ++I, ++Index) {
887 OS.indent(Indentation) << "case " << Index << ":\n";
888 OS.indent(Indentation+2) << "return (" << *I << ");\n";
890 OS.indent(Indentation) << "}\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) {\n";
906 OS.indent(Indentation) << "InsnType tmp;\n";
907 OS.indent(Indentation) << "switch (Idx) {\n";
908 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
910 for (DecoderSet::const_iterator I = Decoders.begin(), E = Decoders.end();
911 I != E; ++I, ++Index) {
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 // Called from Filter::recurse() when singleton exists. For debug purpose.
977 void FilterChooser::SingletonExists(unsigned Opc) const {
979 insnWithID(Insn0, Opc);
981 errs() << "Singleton exists: " << nameWithID(Opc)
982 << " with its decoding dominating ";
983 for (unsigned i = 0; i < Opcodes.size(); ++i) {
984 if (Opcodes[i] == Opc) continue;
985 errs() << nameWithID(Opcodes[i]) << ' ';
989 dumpStack(errs(), "\t\t");
990 for (unsigned i = 0; i < Opcodes.size(); ++i) {
991 const std::string &Name = nameWithID(Opcodes[i]);
993 errs() << '\t' << Name << " ";
995 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1000 // Calculates the island(s) needed to decode the instruction.
1001 // This returns a list of undecoded bits of an instructions, for example,
1002 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
1003 // decoded bits in order to verify that the instruction matches the Opcode.
1004 unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
1005 std::vector<unsigned> &EndBits,
1006 std::vector<uint64_t> &FieldVals,
1007 const insn_t &Insn) const {
1008 unsigned Num, BitNo;
1011 uint64_t FieldVal = 0;
1014 // 1: Water (the bit value does not affect decoding)
1015 // 2: Island (well-known bit value needed for decoding)
1019 for (unsigned i = 0; i < BitWidth; ++i) {
1020 Val = Value(Insn[i]);
1021 bool Filtered = PositionFiltered(i);
1023 default: llvm_unreachable("Unreachable code!");
1026 if (Filtered || Val == -1)
1027 State = 1; // Still in Water
1029 State = 2; // Into the Island
1031 StartBits.push_back(i);
1036 if (Filtered || Val == -1) {
1037 State = 1; // Into the Water
1038 EndBits.push_back(i - 1);
1039 FieldVals.push_back(FieldVal);
1042 State = 2; // Still in Island
1044 FieldVal = FieldVal | Val << BitNo;
1049 // If we are still in Island after the loop, do some housekeeping.
1051 EndBits.push_back(BitWidth - 1);
1052 FieldVals.push_back(FieldVal);
1056 assert(StartBits.size() == Num && EndBits.size() == Num &&
1057 FieldVals.size() == Num);
1061 void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
1062 const OperandInfo &OpInfo) const {
1063 const std::string &Decoder = OpInfo.Decoder;
1065 if (OpInfo.numFields() == 1) {
1066 OperandInfo::const_iterator OI = OpInfo.begin();
1067 o.indent(Indentation) << "tmp = fieldFromInstruction"
1068 << "(insn, " << OI->Base << ", " << OI->Width
1071 o.indent(Indentation) << "tmp = 0;\n";
1072 for (OperandInfo::const_iterator OI = OpInfo.begin(), OE = OpInfo.end();
1074 o.indent(Indentation) << "tmp |= (fieldFromInstruction"
1075 << "(insn, " << OI->Base << ", " << OI->Width
1076 << ") << " << OI->Offset << ");\n";
1081 o.indent(Indentation) << Emitter->GuardPrefix << Decoder
1082 << "(MI, tmp, Address, Decoder)"
1083 << Emitter->GuardPostfix << "\n";
1085 o.indent(Indentation) << "MI.addOperand(MCOperand::CreateImm(tmp));\n";
1089 void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
1090 unsigned Opc) const {
1091 std::map<unsigned, std::vector<OperandInfo> >::const_iterator OpIter =
1093 const std::vector<OperandInfo>& InsnOperands = OpIter->second;
1094 for (std::vector<OperandInfo>::const_iterator
1095 I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
1096 // If a custom instruction decoder was specified, use that.
1097 if (I->numFields() == 0 && I->Decoder.size()) {
1098 OS.indent(Indentation) << Emitter->GuardPrefix << I->Decoder
1099 << "(MI, insn, Address, Decoder)"
1100 << Emitter->GuardPostfix << "\n";
1104 emitBinaryParser(OS, Indentation, *I);
1108 unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
1109 unsigned Opc) const {
1110 // Build up the predicate string.
1111 SmallString<256> Decoder;
1112 // FIXME: emitDecoder() function can take a buffer directly rather than
1114 raw_svector_ostream S(Decoder);
1116 emitDecoder(S, I, Opc);
1119 // Using the full decoder string as the key value here is a bit
1120 // heavyweight, but is effective. If the string comparisons become a
1121 // performance concern, we can implement a mangling of the predicate
1122 // data easilly enough with a map back to the actual string. That's
1123 // overkill for now, though.
1125 // Make sure the predicate is in the table.
1126 Decoders.insert(Decoder.str());
1127 // Now figure out the index for when we write out the table.
1128 DecoderSet::const_iterator P = std::find(Decoders.begin(),
1131 return (unsigned)(P - Decoders.begin());
1134 static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
1135 const std::string &PredicateNamespace) {
1137 o << "!(Bits & " << PredicateNamespace << "::"
1138 << str.slice(1,str.size()) << ")";
1140 o << "(Bits & " << PredicateNamespace << "::" << str << ")";
1143 bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
1144 unsigned Opc) const {
1145 ListInit *Predicates =
1146 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
1147 for (unsigned i = 0; i < Predicates->getSize(); ++i) {
1148 Record *Pred = Predicates->getElementAsRecord(i);
1149 if (!Pred->getValue("AssemblerMatcherPredicate"))
1152 std::string P = Pred->getValueAsString("AssemblerCondString");
1161 std::pair<StringRef, StringRef> pairs = SR.split(',');
1162 while (pairs.second.size()) {
1163 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1165 pairs = pairs.second.split(',');
1167 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1169 return Predicates->getSize() > 0;
1172 bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const {
1173 ListInit *Predicates =
1174 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
1175 for (unsigned i = 0; i < Predicates->getSize(); ++i) {
1176 Record *Pred = Predicates->getElementAsRecord(i);
1177 if (!Pred->getValue("AssemblerMatcherPredicate"))
1180 std::string P = Pred->getValueAsString("AssemblerCondString");
1190 unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo,
1191 StringRef Predicate) const {
1192 // Using the full predicate string as the key value here is a bit
1193 // heavyweight, but is effective. If the string comparisons become a
1194 // performance concern, we can implement a mangling of the predicate
1195 // data easilly enough with a map back to the actual string. That's
1196 // overkill for now, though.
1198 // Make sure the predicate is in the table.
1199 TableInfo.Predicates.insert(Predicate.str());
1200 // Now figure out the index for when we write out the table.
1201 PredicateSet::const_iterator P = std::find(TableInfo.Predicates.begin(),
1202 TableInfo.Predicates.end(),
1204 return (unsigned)(P - TableInfo.Predicates.begin());
1207 void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
1208 unsigned Opc) const {
1209 if (!doesOpcodeNeedPredicate(Opc))
1212 // Build up the predicate string.
1213 SmallString<256> Predicate;
1214 // FIXME: emitPredicateMatch() functions can take a buffer directly rather
1216 raw_svector_ostream PS(Predicate);
1218 emitPredicateMatch(PS, I, Opc);
1220 // Figure out the index into the predicate table for the predicate just
1222 unsigned PIdx = getPredicateIndex(TableInfo, PS.str());
1223 SmallString<16> PBytes;
1224 raw_svector_ostream S(PBytes);
1225 encodeULEB128(PIdx, S);
1228 TableInfo.Table.push_back(MCD::OPC_CheckPredicate);
1230 for (unsigned i = 0, e = PBytes.size(); i != e; ++i)
1231 TableInfo.Table.push_back(PBytes[i]);
1232 // Push location for NumToSkip backpatching.
1233 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1234 TableInfo.Table.push_back(0);
1235 TableInfo.Table.push_back(0);
1238 void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
1239 unsigned Opc) const {
1241 AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail");
1242 if (!SFBits) return;
1243 BitsInit *InstBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("Inst");
1245 APInt PositiveMask(BitWidth, 0ULL);
1246 APInt NegativeMask(BitWidth, 0ULL);
1247 for (unsigned i = 0; i < BitWidth; ++i) {
1248 bit_value_t B = bitFromBits(*SFBits, i);
1249 bit_value_t IB = bitFromBits(*InstBits, i);
1251 if (B != BIT_TRUE) continue;
1255 // The bit is meant to be false, so emit a check to see if it is true.
1256 PositiveMask.setBit(i);
1259 // The bit is meant to be true, so emit a check to see if it is false.
1260 NegativeMask.setBit(i);
1263 // The bit is not set; this must be an error!
1264 StringRef Name = AllInstructions[Opc]->TheDef->getName();
1265 errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in " << Name
1266 << " is set but Inst{" << i << "} is unset!\n"
1267 << " - You can only mark a bit as SoftFail if it is fully defined"
1268 << " (1/0 - not '?') in Inst\n";
1273 bool NeedPositiveMask = PositiveMask.getBoolValue();
1274 bool NeedNegativeMask = NegativeMask.getBoolValue();
1276 if (!NeedPositiveMask && !NeedNegativeMask)
1279 TableInfo.Table.push_back(MCD::OPC_SoftFail);
1281 SmallString<16> MaskBytes;
1282 raw_svector_ostream S(MaskBytes);
1283 if (NeedPositiveMask) {
1284 encodeULEB128(PositiveMask.getZExtValue(), S);
1286 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
1287 TableInfo.Table.push_back(MaskBytes[i]);
1289 TableInfo.Table.push_back(0);
1290 if (NeedNegativeMask) {
1293 encodeULEB128(NegativeMask.getZExtValue(), S);
1295 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
1296 TableInfo.Table.push_back(MaskBytes[i]);
1298 TableInfo.Table.push_back(0);
1301 // Emits table entries to decode the singleton.
1302 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1303 unsigned Opc) const {
1304 std::vector<unsigned> StartBits;
1305 std::vector<unsigned> EndBits;
1306 std::vector<uint64_t> FieldVals;
1308 insnWithID(Insn, Opc);
1310 // Look for islands of undecoded bits of the singleton.
1311 getIslands(StartBits, EndBits, FieldVals, Insn);
1313 unsigned Size = StartBits.size();
1315 // Emit the predicate table entry if one is needed.
1316 emitPredicateTableEntry(TableInfo, Opc);
1318 // Check any additional encoding fields needed.
1319 for (unsigned I = Size; I != 0; --I) {
1320 unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1;
1321 TableInfo.Table.push_back(MCD::OPC_CheckField);
1322 TableInfo.Table.push_back(StartBits[I-1]);
1323 TableInfo.Table.push_back(NumBits);
1324 uint8_t Buffer[8], *p;
1325 encodeULEB128(FieldVals[I-1], Buffer);
1326 for (p = Buffer; *p >= 128 ; ++p)
1327 TableInfo.Table.push_back(*p);
1328 TableInfo.Table.push_back(*p);
1329 // Push location for NumToSkip backpatching.
1330 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1331 // The fixup is always 16-bits, so go ahead and allocate the space
1332 // in the table so all our relative position calculations work OK even
1333 // before we fully resolve the real value here.
1334 TableInfo.Table.push_back(0);
1335 TableInfo.Table.push_back(0);
1338 // Check for soft failure of the match.
1339 emitSoftFailTableEntry(TableInfo, Opc);
1341 TableInfo.Table.push_back(MCD::OPC_Decode);
1342 uint8_t Buffer[8], *p;
1343 encodeULEB128(Opc, Buffer);
1344 for (p = Buffer; *p >= 128 ; ++p)
1345 TableInfo.Table.push_back(*p);
1346 TableInfo.Table.push_back(*p);
1348 unsigned DIdx = getDecoderIndex(TableInfo.Decoders, Opc);
1349 SmallString<16> Bytes;
1350 raw_svector_ostream S(Bytes);
1351 encodeULEB128(DIdx, S);
1355 for (unsigned i = 0, e = Bytes.size(); i != e; ++i)
1356 TableInfo.Table.push_back(Bytes[i]);
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.push_back(FixupList());
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 Filter F(*this, startBit, numBit, true);
1384 Filters.push_back(F);
1385 BestIndex = 0; // Sole Filter instance to choose from.
1386 bestFilter().recurse();
1389 // reportRegion is a helper function for filterProcessor to mark a region as
1390 // eligible for use as a filter region.
1391 void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1392 unsigned BitIndex, bool AllowMixed) {
1393 if (RA == ATTR_MIXED && AllowMixed)
1394 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true));
1395 else if (RA == ATTR_ALL_SET && !AllowMixed)
1396 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false));
1399 // FilterProcessor scans the well-known encoding bits of the instructions and
1400 // builds up a list of candidate filters. It chooses the best filter and
1401 // recursively descends down the decoding tree.
1402 bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1405 unsigned numInstructions = Opcodes.size();
1407 assert(numInstructions && "Filter created with no instructions");
1409 // No further filtering is necessary.
1410 if (numInstructions == 1)
1413 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1414 // instructions is 3.
1415 if (AllowMixed && !Greedy) {
1416 assert(numInstructions == 3);
1418 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1419 std::vector<unsigned> StartBits;
1420 std::vector<unsigned> EndBits;
1421 std::vector<uint64_t> FieldVals;
1424 insnWithID(Insn, Opcodes[i]);
1426 // Look for islands of undecoded bits of any instruction.
1427 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1428 // Found an instruction with island(s). Now just assign a filter.
1429 runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true);
1437 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1438 // The automaton consumes the corresponding bit from each
1441 // Input symbols: 0, 1, and _ (unset).
1442 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1443 // Initial state: NONE.
1445 // (NONE) ------- [01] -> (ALL_SET)
1446 // (NONE) ------- _ ----> (ALL_UNSET)
1447 // (ALL_SET) ---- [01] -> (ALL_SET)
1448 // (ALL_SET) ---- _ ----> (MIXED)
1449 // (ALL_UNSET) -- [01] -> (MIXED)
1450 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1451 // (MIXED) ------ . ----> (MIXED)
1452 // (FILTERED)---- . ----> (FILTERED)
1454 std::vector<bitAttr_t> bitAttrs;
1456 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1457 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1458 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
1459 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1460 FilterBitValues[BitIndex] == BIT_FALSE)
1461 bitAttrs.push_back(ATTR_FILTERED);
1463 bitAttrs.push_back(ATTR_NONE);
1465 for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
1468 insnWithID(insn, Opcodes[InsnIndex]);
1470 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
1471 switch (bitAttrs[BitIndex]) {
1473 if (insn[BitIndex] == BIT_UNSET)
1474 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1476 bitAttrs[BitIndex] = ATTR_ALL_SET;
1479 if (insn[BitIndex] == BIT_UNSET)
1480 bitAttrs[BitIndex] = ATTR_MIXED;
1482 case ATTR_ALL_UNSET:
1483 if (insn[BitIndex] != BIT_UNSET)
1484 bitAttrs[BitIndex] = ATTR_MIXED;
1493 // The regionAttr automaton consumes the bitAttrs automatons' state,
1494 // lowest-to-highest.
1496 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1497 // States: NONE, ALL_SET, MIXED
1498 // Initial state: NONE
1500 // (NONE) ----- F --> (NONE)
1501 // (NONE) ----- S --> (ALL_SET) ; and set region start
1502 // (NONE) ----- U --> (NONE)
1503 // (NONE) ----- M --> (MIXED) ; and set region start
1504 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1505 // (ALL_SET) -- S --> (ALL_SET)
1506 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1507 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1508 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1509 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1510 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1511 // (MIXED) ---- M --> (MIXED)
1513 bitAttr_t RA = ATTR_NONE;
1514 unsigned StartBit = 0;
1516 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
1517 bitAttr_t bitAttr = bitAttrs[BitIndex];
1519 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1527 StartBit = BitIndex;
1530 case ATTR_ALL_UNSET:
1533 StartBit = BitIndex;
1537 llvm_unreachable("Unexpected bitAttr!");
1543 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1548 case ATTR_ALL_UNSET:
1549 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1553 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1554 StartBit = BitIndex;
1558 llvm_unreachable("Unexpected bitAttr!");
1564 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1565 StartBit = BitIndex;
1569 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1570 StartBit = BitIndex;
1573 case ATTR_ALL_UNSET:
1574 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1580 llvm_unreachable("Unexpected bitAttr!");
1583 case ATTR_ALL_UNSET:
1584 llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
1586 llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
1590 // At the end, if we're still in ALL_SET or MIXED states, report a region
1597 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1599 case ATTR_ALL_UNSET:
1602 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1606 // We have finished with the filter processings. Now it's time to choose
1607 // the best performing filter.
1609 bool AllUseless = true;
1610 unsigned BestScore = 0;
1612 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1613 unsigned Usefulness = Filters[i].usefulness();
1618 if (Usefulness > BestScore) {
1620 BestScore = Usefulness;
1625 bestFilter().recurse();
1628 } // end of FilterChooser::filterProcessor(bool)
1630 // Decides on the best configuration of filter(s) to use in order to decode
1631 // the instructions. A conflict of instructions may occur, in which case we
1632 // dump the conflict set to the standard error.
1633 void FilterChooser::doFilter() {
1634 unsigned Num = Opcodes.size();
1635 assert(Num && "FilterChooser created with no instructions");
1637 // Try regions of consecutive known bit values first.
1638 if (filterProcessor(false))
1641 // Then regions of mixed bits (both known and unitialized bit values allowed).
1642 if (filterProcessor(true))
1645 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1646 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1647 // well-known encoding pattern. In such case, we backtrack and scan for the
1648 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1649 if (Num == 3 && filterProcessor(true, false))
1652 // If we come to here, the instruction decoding has failed.
1653 // Set the BestIndex to -1 to indicate so.
1657 // emitTableEntries - Emit state machine entries to decode our share of
1659 void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
1660 if (Opcodes.size() == 1) {
1661 // There is only one instruction in the set, which is great!
1662 // Call emitSingletonDecoder() to see whether there are any remaining
1664 emitSingletonTableEntry(TableInfo, Opcodes[0]);
1668 // Choose the best filter to do the decodings!
1669 if (BestIndex != -1) {
1670 const Filter &Best = Filters[BestIndex];
1671 if (Best.getNumFiltered() == 1)
1672 emitSingletonTableEntry(TableInfo, Best);
1674 Best.emitTableEntry(TableInfo);
1678 // We don't know how to decode these instructions! Dump the
1679 // conflict set and bail.
1681 // Print out useful conflict information for postmortem analysis.
1682 errs() << "Decoding Conflict:\n";
1684 dumpStack(errs(), "\t\t");
1686 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1687 const std::string &Name = nameWithID(Opcodes[i]);
1689 errs() << '\t' << Name << " ";
1691 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1696 static bool populateInstruction(const CodeGenInstruction &CGI, unsigned Opc,
1697 std::map<unsigned, std::vector<OperandInfo> > &Operands){
1698 const Record &Def = *CGI.TheDef;
1699 // If all the bit positions are not specified; do not decode this instruction.
1700 // We are bound to fail! For proper disassembly, the well-known encoding bits
1701 // of the instruction must be fully specified.
1703 // This also removes pseudo instructions from considerations of disassembly,
1704 // which is a better design and less fragile than the name matchings.
1705 // Ignore "asm parser only" instructions.
1706 if (Def.getValueAsBit("isAsmParserOnly") ||
1707 Def.getValueAsBit("isCodeGenOnly"))
1710 BitsInit &Bits = getBitsField(Def, "Inst");
1711 if (Bits.allInComplete()) return false;
1713 std::vector<OperandInfo> InsnOperands;
1715 // If the instruction has specified a custom decoding hook, use that instead
1716 // of trying to auto-generate the decoder.
1717 std::string InstDecoder = Def.getValueAsString("DecoderMethod");
1718 if (InstDecoder != "") {
1719 InsnOperands.push_back(OperandInfo(InstDecoder));
1720 Operands[Opc] = InsnOperands;
1724 // Generate a description of the operand of the instruction that we know
1725 // how to decode automatically.
1726 // FIXME: We'll need to have a way to manually override this as needed.
1728 // Gather the outputs/inputs of the instruction, so we can find their
1729 // positions in the encoding. This assumes for now that they appear in the
1730 // MCInst in the order that they're listed.
1731 std::vector<std::pair<Init*, std::string> > InOutOperands;
1732 DagInit *Out = Def.getValueAsDag("OutOperandList");
1733 DagInit *In = Def.getValueAsDag("InOperandList");
1734 for (unsigned i = 0; i < Out->getNumArgs(); ++i)
1735 InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i)));
1736 for (unsigned i = 0; i < In->getNumArgs(); ++i)
1737 InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i)));
1739 // Search for tied operands, so that we can correctly instantiate
1740 // operands that are not explicitly represented in the encoding.
1741 std::map<std::string, std::string> TiedNames;
1742 for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
1743 int tiedTo = CGI.Operands[i].getTiedRegister();
1745 TiedNames[InOutOperands[i].second] = InOutOperands[tiedTo].second;
1746 TiedNames[InOutOperands[tiedTo].second] = InOutOperands[i].second;
1750 // For each operand, see if we can figure out where it is encoded.
1751 for (std::vector<std::pair<Init*, std::string> >::const_iterator
1752 NI = InOutOperands.begin(), NE = InOutOperands.end(); NI != NE; ++NI) {
1753 std::string Decoder = "";
1755 // At this point, we can locate the field, but we need to know how to
1756 // interpret it. As a first step, require the target to provide callbacks
1757 // for decoding register classes.
1758 // FIXME: This need to be extended to handle instructions with custom
1759 // decoder methods, and operands with (simple) MIOperandInfo's.
1760 TypedInit *TI = cast<TypedInit>(NI->first);
1761 RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
1762 Record *TypeRecord = Type->getRecord();
1764 if (TypeRecord->isSubClassOf("RegisterOperand"))
1765 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1766 if (TypeRecord->isSubClassOf("RegisterClass")) {
1767 Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
1771 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
1772 StringInit *String = DecoderString ?
1773 dyn_cast<StringInit>(DecoderString->getValue()) : 0;
1774 if (!isReg && String && String->getValue() != "")
1775 Decoder = String->getValue();
1777 OperandInfo OpInfo(Decoder);
1778 unsigned Base = ~0U;
1780 unsigned Offset = 0;
1782 for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
1784 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1786 Var = dyn_cast<VarInit>(BI->getBitVar());
1788 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1792 OpInfo.addField(Base, Width, Offset);
1800 if (Var->getName() != NI->second &&
1801 Var->getName() != TiedNames[NI->second]) {
1803 OpInfo.addField(Base, Width, Offset);
1814 Offset = BI ? BI->getBitNum() : 0;
1815 } else if (BI && BI->getBitNum() != Offset + Width) {
1816 OpInfo.addField(Base, Width, Offset);
1819 Offset = BI->getBitNum();
1826 OpInfo.addField(Base, Width, Offset);
1828 if (OpInfo.numFields() > 0)
1829 InsnOperands.push_back(OpInfo);
1832 Operands[Opc] = InsnOperands;
1837 // Dumps the instruction encoding bits.
1838 dumpBits(errs(), Bits);
1842 // Dumps the list of operand info.
1843 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1844 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
1845 const std::string &OperandName = Info.Name;
1846 const Record &OperandDef = *Info.Rec;
1848 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
1856 // emitFieldFromInstruction - Emit the templated helper function
1857 // fieldFromInstruction().
1858 static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
1859 OS << "// Helper function for extracting fields from encoded instructions.\n"
1860 << "template<typename InsnType>\n"
1861 << "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n"
1862 << " unsigned numBits) {\n"
1863 << " assert(startBit + numBits <= (sizeof(InsnType)*8) &&\n"
1864 << " \"Instruction field out of bounds!\");\n"
1865 << " InsnType fieldMask;\n"
1866 << " if (numBits == sizeof(InsnType)*8)\n"
1867 << " fieldMask = (InsnType)(-1LL);\n"
1869 << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
1870 << " return (insn & fieldMask) >> startBit;\n"
1874 // emitDecodeInstruction - Emit the templated helper function
1875 // decodeInstruction().
1876 static void emitDecodeInstruction(formatted_raw_ostream &OS) {
1877 OS << "template<typename InsnType>\n"
1878 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,\n"
1879 << " InsnType insn, uint64_t Address,\n"
1880 << " const void *DisAsm,\n"
1881 << " const MCSubtargetInfo &STI) {\n"
1882 << " uint64_t Bits = STI.getFeatureBits();\n"
1884 << " const uint8_t *Ptr = DecodeTable;\n"
1885 << " uint32_t CurFieldValue = 0;\n"
1886 << " DecodeStatus S = MCDisassembler::Success;\n"
1888 << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
1889 << " switch (*Ptr) {\n"
1891 << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
1892 << " return MCDisassembler::Fail;\n"
1893 << " case MCD::OPC_ExtractField: {\n"
1894 << " unsigned Start = *++Ptr;\n"
1895 << " unsigned Len = *++Ptr;\n"
1897 << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
1898 << " DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << \", \"\n"
1899 << " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
1902 << " case MCD::OPC_FilterValue: {\n"
1903 << " // Decode the field value.\n"
1904 << " unsigned Len;\n"
1905 << " InsnType Val = decodeULEB128(++Ptr, &Len);\n"
1907 << " // NumToSkip is a plain 16-bit integer.\n"
1908 << " unsigned NumToSkip = *Ptr++;\n"
1909 << " NumToSkip |= (*Ptr++) << 8;\n"
1911 << " // Perform the filter operation.\n"
1912 << " if (Val != CurFieldValue)\n"
1913 << " Ptr += NumToSkip;\n"
1914 << " DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << \", \" << NumToSkip\n"
1915 << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" : \"PASS:\")\n"
1916 << " << \" continuing at \" << (Ptr - DecodeTable) << \"\\n\");\n"
1920 << " case MCD::OPC_CheckField: {\n"
1921 << " unsigned Start = *++Ptr;\n"
1922 << " unsigned Len = *++Ptr;\n"
1923 << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
1924 << " // Decode the field value.\n"
1925 << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
1927 << " // NumToSkip is a plain 16-bit integer.\n"
1928 << " unsigned NumToSkip = *Ptr++;\n"
1929 << " NumToSkip |= (*Ptr++) << 8;\n"
1931 << " // If the actual and expected values don't match, skip.\n"
1932 << " if (ExpectedValue != FieldValue)\n"
1933 << " Ptr += NumToSkip;\n"
1934 << " DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << \", \"\n"
1935 << " << Len << \", \" << ExpectedValue << \", \" << NumToSkip\n"
1936 << " << \"): FieldValue = \" << FieldValue << \", ExpectedValue = \"\n"
1937 << " << ExpectedValue << \": \"\n"
1938 << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : \"FAIL\\n\"));\n"
1941 << " case MCD::OPC_CheckPredicate: {\n"
1942 << " unsigned Len;\n"
1943 << " // Decode the Predicate Index value.\n"
1944 << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
1946 << " // NumToSkip is a plain 16-bit integer.\n"
1947 << " unsigned NumToSkip = *Ptr++;\n"
1948 << " NumToSkip |= (*Ptr++) << 8;\n"
1949 << " // Check the predicate.\n"
1951 << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
1952 << " Ptr += NumToSkip;\n"
1954 << " DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx << \"): \"\n"
1955 << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
1959 << " case MCD::OPC_Decode: {\n"
1960 << " unsigned Len;\n"
1961 << " // Decode the Opcode value.\n"
1962 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
1964 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
1966 << " DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
1967 << " << \", using decoder \" << DecodeIdx << \"\\n\" );\n"
1968 << " DEBUG(dbgs() << \"----- DECODE SUCCESSFUL -----\\n\");\n"
1970 << " MI.setOpcode(Opc);\n"
1971 << " return decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm);\n"
1973 << " case MCD::OPC_SoftFail: {\n"
1974 << " // Decode the mask values.\n"
1975 << " unsigned Len;\n"
1976 << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n"
1978 << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n"
1980 << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n"
1982 << " S = MCDisassembler::SoftFail;\n"
1983 << " DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? \"FAIL\\n\":\"PASS\\n\"));\n"
1986 << " case MCD::OPC_Fail: {\n"
1987 << " DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
1988 << " return MCDisassembler::Fail;\n"
1992 << " llvm_unreachable(\"bogosity detected in disassembler state machine!\");\n"
1996 // Emits disassembler code for instruction decoding.
1997 void FixedLenDecoderEmitter::run(raw_ostream &o) {
1998 formatted_raw_ostream OS(o);
1999 OS << "#include \"llvm/MC/MCInst.h\"\n";
2000 OS << "#include \"llvm/Support/Debug.h\"\n";
2001 OS << "#include \"llvm/Support/DataTypes.h\"\n";
2002 OS << "#include \"llvm/Support/LEB128.h\"\n";
2003 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
2004 OS << "#include <assert.h>\n";
2006 OS << "namespace llvm {\n\n";
2008 emitFieldFromInstruction(OS);
2010 // Parameterize the decoders based on namespace and instruction width.
2011 NumberedInstructions = &Target.getInstructionsByEnumValue();
2012 std::map<std::pair<std::string, unsigned>,
2013 std::vector<unsigned> > OpcMap;
2014 std::map<unsigned, std::vector<OperandInfo> > Operands;
2016 for (unsigned i = 0; i < NumberedInstructions->size(); ++i) {
2017 const CodeGenInstruction *Inst = NumberedInstructions->at(i);
2018 const Record *Def = Inst->TheDef;
2019 unsigned Size = Def->getValueAsInt("Size");
2020 if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
2021 Def->getValueAsBit("isPseudo") ||
2022 Def->getValueAsBit("isAsmParserOnly") ||
2023 Def->getValueAsBit("isCodeGenOnly"))
2026 std::string DecoderNamespace = Def->getValueAsString("DecoderNamespace");
2029 if (populateInstruction(*Inst, i, Operands)) {
2030 OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
2035 DecoderTableInfo TableInfo;
2036 std::set<unsigned> Sizes;
2037 for (std::map<std::pair<std::string, unsigned>,
2038 std::vector<unsigned> >::const_iterator
2039 I = OpcMap.begin(), E = OpcMap.end(); I != E; ++I) {
2040 // Emit the decoder for this namespace+width combination.
2041 FilterChooser FC(*NumberedInstructions, I->second, Operands,
2042 8*I->first.second, this);
2044 // The decode table is cleared for each top level decoder function. The
2045 // predicates and decoders themselves, however, are shared across all
2046 // decoders to give more opportunities for uniqueing.
2047 TableInfo.Table.clear();
2048 TableInfo.FixupStack.clear();
2049 TableInfo.Table.reserve(16384);
2050 TableInfo.FixupStack.push_back(FixupList());
2051 FC.emitTableEntries(TableInfo);
2052 // Any NumToSkip fixups in the top level scope can resolve to the
2053 // OPC_Fail at the end of the table.
2054 assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
2055 // Resolve any NumToSkip fixups in the current scope.
2056 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
2057 TableInfo.Table.size());
2058 TableInfo.FixupStack.clear();
2060 TableInfo.Table.push_back(MCD::OPC_Fail);
2062 // Print the table to the output stream.
2063 emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), I->first.first);
2067 // Emit the predicate function.
2068 emitPredicateFunction(OS, TableInfo.Predicates, 0);
2070 // Emit the decoder function.
2071 emitDecoderFunction(OS, TableInfo.Decoders, 0);
2073 // Emit the main entry point for the decoder, decodeInstruction().
2074 emitDecodeInstruction(OS);
2076 OS << "\n} // End llvm namespace\n";
2081 void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
2082 std::string PredicateNamespace,
2083 std::string GPrefix,
2084 std::string GPostfix,
2088 FixedLenDecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix,
2089 ROK, RFail, L).run(OS);
2092 } // End llvm namespace