1 //===------------ FixedLenDecoderEmitter.cpp - Decoder Generator ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // It contains the tablegen backend that emits the decoder functions for
10 // targets with fixed length instruction set.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenInstruction.h"
15 #include "CodeGenTarget.h"
16 #include "InfoByHwMode.h"
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/CachedHashString.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/MC/MCFixedLenDisassembler.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/FormattedStream.h"
31 #include "llvm/Support/LEB128.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/TableGen/Error.h"
34 #include "llvm/TableGen/Record.h"
48 #define DEBUG_TYPE "decoder-emitter"
52 STATISTIC(NumEncodings
, "Number of encodings considered");
53 STATISTIC(NumEncodingsLackingDisasm
, "Number of encodings without disassembler info");
54 STATISTIC(NumInstructions
, "Number of instructions considered");
55 STATISTIC(NumEncodingsSupported
, "Number of encodings supported");
56 STATISTIC(NumEncodingsOmitted
, "Number of encodings omitted");
58 struct EncodingField
{
59 unsigned Base
, Width
, Offset
;
60 EncodingField(unsigned B
, unsigned W
, unsigned O
)
61 : Base(B
), Width(W
), Offset(O
) { }
65 std::vector
<EncodingField
> Fields
;
67 bool HasCompleteDecoder
;
70 OperandInfo(std::string D
, bool HCD
)
71 : Decoder(std::move(D
)), HasCompleteDecoder(HCD
), InitValue(0) {}
73 void addField(unsigned Base
, unsigned Width
, unsigned Offset
) {
74 Fields
.push_back(EncodingField(Base
, Width
, Offset
));
77 unsigned numFields() const { return Fields
.size(); }
79 typedef std::vector
<EncodingField
>::const_iterator const_iterator
;
81 const_iterator
begin() const { return Fields
.begin(); }
82 const_iterator
end() const { return Fields
.end(); }
85 typedef std::vector
<uint8_t> DecoderTable
;
86 typedef uint32_t DecoderFixup
;
87 typedef std::vector
<DecoderFixup
> FixupList
;
88 typedef std::vector
<FixupList
> FixupScopeList
;
89 typedef SmallSetVector
<CachedHashString
, 16> PredicateSet
;
90 typedef SmallSetVector
<CachedHashString
, 16> DecoderSet
;
91 struct DecoderTableInfo
{
93 FixupScopeList FixupStack
;
94 PredicateSet Predicates
;
98 struct EncodingAndInst
{
99 const Record
*EncodingDef
;
100 const CodeGenInstruction
*Inst
;
101 StringRef HwModeName
;
103 EncodingAndInst(const Record
*EncodingDef
, const CodeGenInstruction
*Inst
,
104 StringRef HwModeName
= "")
105 : EncodingDef(EncodingDef
), Inst(Inst
), HwModeName(HwModeName
) {}
108 struct EncodingIDAndOpcode
{
112 EncodingIDAndOpcode() : EncodingID(0), Opcode(0) {}
113 EncodingIDAndOpcode(unsigned EncodingID
, unsigned Opcode
)
114 : EncodingID(EncodingID
), Opcode(Opcode
) {}
117 raw_ostream
&operator<<(raw_ostream
&OS
, const EncodingAndInst
&Value
) {
118 if (Value
.EncodingDef
!= Value
.Inst
->TheDef
)
119 OS
<< Value
.EncodingDef
->getName() << ":";
120 OS
<< Value
.Inst
->TheDef
->getName();
124 class FixedLenDecoderEmitter
{
126 std::vector
<EncodingAndInst
> NumberedEncodings
;
129 // Defaults preserved here for documentation, even though they aren't
130 // strictly necessary given the way that this is currently being called.
131 FixedLenDecoderEmitter(RecordKeeper
&R
, std::string PredicateNamespace
,
132 std::string GPrefix
= "if (",
133 std::string GPostfix
= " == MCDisassembler::Fail)",
134 std::string ROK
= "MCDisassembler::Success",
135 std::string RFail
= "MCDisassembler::Fail",
137 : RK(R
), Target(R
), PredicateNamespace(std::move(PredicateNamespace
)),
138 GuardPrefix(std::move(GPrefix
)), GuardPostfix(std::move(GPostfix
)),
139 ReturnOK(std::move(ROK
)), ReturnFail(std::move(RFail
)),
140 Locals(std::move(L
)) {}
142 // Emit the decoder state machine table.
143 void emitTable(formatted_raw_ostream
&o
, DecoderTable
&Table
,
144 unsigned Indentation
, unsigned BitWidth
,
145 StringRef Namespace
) const;
146 void emitPredicateFunction(formatted_raw_ostream
&OS
,
147 PredicateSet
&Predicates
,
148 unsigned Indentation
) const;
149 void emitDecoderFunction(formatted_raw_ostream
&OS
,
150 DecoderSet
&Decoders
,
151 unsigned Indentation
) const;
153 // run - Output the code emitter
154 void run(raw_ostream
&o
);
157 CodeGenTarget Target
;
160 std::string PredicateNamespace
;
161 std::string GuardPrefix
, GuardPostfix
;
162 std::string ReturnOK
, ReturnFail
;
166 } // end anonymous namespace
168 // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
171 // BIT_UNFILTERED is used as the init value for a filter position. It is used
172 // only for filter processings.
177 BIT_UNFILTERED
// unfiltered
180 static bool ValueSet(bit_value_t V
) {
181 return (V
== BIT_TRUE
|| V
== BIT_FALSE
);
184 static bool ValueNotSet(bit_value_t V
) {
185 return (V
== BIT_UNSET
);
188 static int Value(bit_value_t V
) {
189 return ValueNotSet(V
) ? -1 : (V
== BIT_FALSE
? 0 : 1);
192 static bit_value_t
bitFromBits(const BitsInit
&bits
, unsigned index
) {
193 if (BitInit
*bit
= dyn_cast
<BitInit
>(bits
.getBit(index
)))
194 return bit
->getValue() ? BIT_TRUE
: BIT_FALSE
;
196 // The bit is uninitialized.
200 // Prints the bit value for each position.
201 static void dumpBits(raw_ostream
&o
, const BitsInit
&bits
) {
202 for (unsigned index
= bits
.getNumBits(); index
> 0; --index
) {
203 switch (bitFromBits(bits
, index
- 1)) {
214 llvm_unreachable("unexpected return value from bitFromBits");
219 static BitsInit
&getBitsField(const Record
&def
, StringRef str
) {
220 BitsInit
*bits
= def
.getValueAsBitsInit(str
);
224 // Representation of the instruction to work on.
225 typedef std::vector
<bit_value_t
> insn_t
;
231 /// Filter - Filter works with FilterChooser to produce the decoding tree for
234 /// It is useful to think of a Filter as governing the switch stmts of the
235 /// decoding tree in a certain level. Each case stmt delegates to an inferior
236 /// FilterChooser to decide what further decoding logic to employ, or in another
237 /// words, what other remaining bits to look at. The FilterChooser eventually
238 /// chooses a best Filter to do its job.
240 /// This recursive scheme ends when the number of Opcodes assigned to the
241 /// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
242 /// the Filter/FilterChooser combo does not know how to distinguish among the
243 /// Opcodes assigned.
245 /// An example of a conflict is
248 /// 111101000.00........00010000....
249 /// 111101000.00........0001........
250 /// 1111010...00........0001........
251 /// 1111010...00....................
252 /// 1111010.........................
253 /// 1111............................
254 /// ................................
255 /// VST4q8a 111101000_00________00010000____
256 /// VST4q8b 111101000_00________00010000____
258 /// The Debug output shows the path that the decoding tree follows to reach the
259 /// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
260 /// even registers, while VST4q8b is a vst4 to double-spaced odd registers.
262 /// The encoding info in the .td files does not specify this meta information,
263 /// which could have been used by the decoder to resolve the conflict. The
264 /// decoder could try to decode the even/odd register numbering and assign to
265 /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
266 /// version and return the Opcode since the two have the same Asm format string.
269 const FilterChooser
*Owner
;// points to the FilterChooser who owns this filter
270 unsigned StartBit
; // the starting bit position
271 unsigned NumBits
; // number of bits to filter
272 bool Mixed
; // a mixed region contains both set and unset bits
274 // Map of well-known segment value to the set of uid's with that value.
275 std::map
<uint64_t, std::vector
<EncodingIDAndOpcode
>>
276 FilteredInstructions
;
278 // Set of uid's with non-constant segment values.
279 std::vector
<EncodingIDAndOpcode
> VariableInstructions
;
281 // Map of well-known segment value to its delegate.
282 std::map
<unsigned, std::unique_ptr
<const FilterChooser
>> FilterChooserMap
;
284 // Number of instructions which fall under FilteredInstructions category.
285 unsigned NumFiltered
;
287 // Keeps track of the last opcode in the filtered bucket.
288 EncodingIDAndOpcode LastOpcFiltered
;
292 Filter(FilterChooser
&owner
, unsigned startBit
, unsigned numBits
, bool mixed
);
296 unsigned getNumFiltered() const { return NumFiltered
; }
298 EncodingIDAndOpcode
getSingletonOpc() const {
299 assert(NumFiltered
== 1);
300 return LastOpcFiltered
;
303 // Return the filter chooser for the group of instructions without constant
305 const FilterChooser
&getVariableFC() const {
306 assert(NumFiltered
== 1);
307 assert(FilterChooserMap
.size() == 1);
308 return *(FilterChooserMap
.find((unsigned)-1)->second
);
311 // Divides the decoding task into sub tasks and delegates them to the
312 // inferior FilterChooser's.
314 // A special case arises when there's only one entry in the filtered
315 // instructions. In order to unambiguously decode the singleton, we need to
316 // match the remaining undecoded encoding bits against the singleton.
319 // Emit table entries to decode instructions given a segment or segments of
321 void emitTableEntry(DecoderTableInfo
&TableInfo
) const;
323 // Returns the number of fanout produced by the filter. More fanout implies
324 // the filter distinguishes more categories of instructions.
325 unsigned usefulness() const;
326 }; // end class Filter
328 } // end anonymous namespace
330 // These are states of our finite state machines used in FilterChooser's
331 // filterProcessor() which produces the filter candidates to use.
340 /// FilterChooser - FilterChooser chooses the best filter among a set of Filters
341 /// in order to perform the decoding of instructions at the current level.
343 /// Decoding proceeds from the top down. Based on the well-known encoding bits
344 /// of instructions available, FilterChooser builds up the possible Filters that
345 /// can further the task of decoding by distinguishing among the remaining
346 /// candidate instructions.
348 /// Once a filter has been chosen, it is called upon to divide the decoding task
349 /// into sub-tasks and delegates them to its inferior FilterChoosers for further
352 /// It is useful to think of a Filter as governing the switch stmts of the
353 /// decoding tree. And each case is delegated to an inferior FilterChooser to
354 /// decide what further remaining bits to look at.
357 class FilterChooser
{
361 // Vector of codegen instructions to choose our filter.
362 ArrayRef
<EncodingAndInst
> AllInstructions
;
364 // Vector of uid's for this filter chooser to work on.
365 // The first member of the pair is the opcode id being decoded, the second is
366 // the opcode id that should be emitted.
367 const std::vector
<EncodingIDAndOpcode
> &Opcodes
;
369 // Lookup table for the operand decoding of instructions.
370 const std::map
<unsigned, std::vector
<OperandInfo
>> &Operands
;
372 // Vector of candidate filters.
373 std::vector
<Filter
> Filters
;
375 // Array of bit values passed down from our parent.
376 // Set to all BIT_UNFILTERED's for Parent == NULL.
377 std::vector
<bit_value_t
> FilterBitValues
;
379 // Links to the FilterChooser above us in the decoding tree.
380 const FilterChooser
*Parent
;
382 // Index of the best filter from Filters.
385 // Width of instructions
389 const FixedLenDecoderEmitter
*Emitter
;
392 FilterChooser(ArrayRef
<EncodingAndInst
> Insts
,
393 const std::vector
<EncodingIDAndOpcode
> &IDs
,
394 const std::map
<unsigned, std::vector
<OperandInfo
>> &Ops
,
395 unsigned BW
, const FixedLenDecoderEmitter
*E
)
396 : AllInstructions(Insts
), Opcodes(IDs
), Operands(Ops
),
397 FilterBitValues(BW
, BIT_UNFILTERED
), Parent(nullptr), BestIndex(-1),
398 BitWidth(BW
), Emitter(E
) {
402 FilterChooser(ArrayRef
<EncodingAndInst
> Insts
,
403 const std::vector
<EncodingIDAndOpcode
> &IDs
,
404 const std::map
<unsigned, std::vector
<OperandInfo
>> &Ops
,
405 const std::vector
<bit_value_t
> &ParentFilterBitValues
,
406 const FilterChooser
&parent
)
407 : AllInstructions(Insts
), Opcodes(IDs
), Operands(Ops
),
408 FilterBitValues(ParentFilterBitValues
), Parent(&parent
), BestIndex(-1),
409 BitWidth(parent
.BitWidth
), Emitter(parent
.Emitter
) {
413 FilterChooser(const FilterChooser
&) = delete;
414 void operator=(const FilterChooser
&) = delete;
416 unsigned getBitWidth() const { return BitWidth
; }
419 // Populates the insn given the uid.
420 void insnWithID(insn_t
&Insn
, unsigned Opcode
) const {
421 BitsInit
&Bits
= getBitsField(*AllInstructions
[Opcode
].EncodingDef
, "Inst");
423 // We may have a SoftFail bitmask, which specifies a mask where an encoding
424 // may differ from the value in "Inst" and yet still be valid, but the
425 // disassembler should return SoftFail instead of Success.
427 // This is used for marking UNPREDICTABLE instructions in the ARM world.
429 AllInstructions
[Opcode
].EncodingDef
->getValueAsBitsInit("SoftFail");
431 for (unsigned i
= 0; i
< BitWidth
; ++i
) {
432 if (SFBits
&& bitFromBits(*SFBits
, i
) == BIT_TRUE
)
433 Insn
.push_back(BIT_UNSET
);
435 Insn
.push_back(bitFromBits(Bits
, i
));
439 // Emit the name of the encoding/instruction pair.
440 void emitNameWithID(raw_ostream
&OS
, unsigned Opcode
) const {
441 const Record
*EncodingDef
= AllInstructions
[Opcode
].EncodingDef
;
442 const Record
*InstDef
= AllInstructions
[Opcode
].Inst
->TheDef
;
443 if (EncodingDef
!= InstDef
)
444 OS
<< EncodingDef
->getName() << ":";
445 OS
<< InstDef
->getName();
448 // Populates the field of the insn given the start position and the number of
449 // consecutive bits to scan for.
451 // Returns false if there exists any uninitialized bit value in the range.
452 // Returns true, otherwise.
453 bool fieldFromInsn(uint64_t &Field
, insn_t
&Insn
, unsigned StartBit
,
454 unsigned NumBits
) const;
456 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
457 /// filter array as a series of chars.
458 void dumpFilterArray(raw_ostream
&o
,
459 const std::vector
<bit_value_t
> & filter
) const;
461 /// dumpStack - dumpStack traverses the filter chooser chain and calls
462 /// dumpFilterArray on each filter chooser up to the top level one.
463 void dumpStack(raw_ostream
&o
, const char *prefix
) const;
465 Filter
&bestFilter() {
466 assert(BestIndex
!= -1 && "BestIndex not set");
467 return Filters
[BestIndex
];
470 bool PositionFiltered(unsigned i
) const {
471 return ValueSet(FilterBitValues
[i
]);
474 // Calculates the island(s) needed to decode the instruction.
475 // This returns a lit of undecoded bits of an instructions, for example,
476 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
477 // decoded bits in order to verify that the instruction matches the Opcode.
478 unsigned getIslands(std::vector
<unsigned> &StartBits
,
479 std::vector
<unsigned> &EndBits
,
480 std::vector
<uint64_t> &FieldVals
,
481 const insn_t
&Insn
) const;
483 // Emits code to check the Predicates member of an instruction are true.
484 // Returns true if predicate matches were emitted, false otherwise.
485 bool emitPredicateMatch(raw_ostream
&o
, unsigned &Indentation
,
488 bool doesOpcodeNeedPredicate(unsigned Opc
) const;
489 unsigned getPredicateIndex(DecoderTableInfo
&TableInfo
, StringRef P
) const;
490 void emitPredicateTableEntry(DecoderTableInfo
&TableInfo
,
493 void emitSoftFailTableEntry(DecoderTableInfo
&TableInfo
,
496 // Emits table entries to decode the singleton.
497 void emitSingletonTableEntry(DecoderTableInfo
&TableInfo
,
498 EncodingIDAndOpcode Opc
) const;
500 // Emits code to decode the singleton, and then to decode the rest.
501 void emitSingletonTableEntry(DecoderTableInfo
&TableInfo
,
502 const Filter
&Best
) const;
504 void emitBinaryParser(raw_ostream
&o
, unsigned &Indentation
,
505 const OperandInfo
&OpInfo
,
506 bool &OpHasCompleteDecoder
) const;
508 void emitDecoder(raw_ostream
&OS
, unsigned Indentation
, unsigned Opc
,
509 bool &HasCompleteDecoder
) const;
510 unsigned getDecoderIndex(DecoderSet
&Decoders
, unsigned Opc
,
511 bool &HasCompleteDecoder
) const;
513 // Assign a single filter and run with it.
514 void runSingleFilter(unsigned startBit
, unsigned numBit
, bool mixed
);
516 // reportRegion is a helper function for filterProcessor to mark a region as
517 // eligible for use as a filter region.
518 void reportRegion(bitAttr_t RA
, unsigned StartBit
, unsigned BitIndex
,
521 // FilterProcessor scans the well-known encoding bits of the instructions and
522 // builds up a list of candidate filters. It chooses the best filter and
523 // recursively descends down the decoding tree.
524 bool filterProcessor(bool AllowMixed
, bool Greedy
= true);
526 // Decides on the best configuration of filter(s) to use in order to decode
527 // the instructions. A conflict of instructions may occur, in which case we
528 // dump the conflict set to the standard error.
532 // emitTableEntries - Emit state machine entries to decode our share of
534 void emitTableEntries(DecoderTableInfo
&TableInfo
) const;
537 } // end anonymous namespace
539 ///////////////////////////
541 // Filter Implementation //
543 ///////////////////////////
545 Filter::Filter(Filter
&&f
)
546 : Owner(f
.Owner
), StartBit(f
.StartBit
), NumBits(f
.NumBits
), Mixed(f
.Mixed
),
547 FilteredInstructions(std::move(f
.FilteredInstructions
)),
548 VariableInstructions(std::move(f
.VariableInstructions
)),
549 FilterChooserMap(std::move(f
.FilterChooserMap
)), NumFiltered(f
.NumFiltered
),
550 LastOpcFiltered(f
.LastOpcFiltered
) {
553 Filter::Filter(FilterChooser
&owner
, unsigned startBit
, unsigned numBits
,
555 : Owner(&owner
), StartBit(startBit
), NumBits(numBits
), Mixed(mixed
) {
556 assert(StartBit
+ NumBits
- 1 < Owner
->BitWidth
);
559 LastOpcFiltered
= {0, 0};
561 for (unsigned i
= 0, e
= Owner
->Opcodes
.size(); i
!= e
; ++i
) {
564 // Populates the insn given the uid.
565 Owner
->insnWithID(Insn
, Owner
->Opcodes
[i
].EncodingID
);
568 // Scans the segment for possibly well-specified encoding bits.
569 bool ok
= Owner
->fieldFromInsn(Field
, Insn
, StartBit
, NumBits
);
572 // The encoding bits are well-known. Lets add the uid of the
573 // instruction into the bucket keyed off the constant field value.
574 LastOpcFiltered
= Owner
->Opcodes
[i
];
575 FilteredInstructions
[Field
].push_back(LastOpcFiltered
);
578 // Some of the encoding bit(s) are unspecified. This contributes to
579 // one additional member of "Variable" instructions.
580 VariableInstructions
.push_back(Owner
->Opcodes
[i
]);
584 assert((FilteredInstructions
.size() + VariableInstructions
.size() > 0)
585 && "Filter returns no instruction categories");
588 // Divides the decoding task into sub tasks and delegates them to the
589 // inferior FilterChooser's.
591 // A special case arises when there's only one entry in the filtered
592 // instructions. In order to unambiguously decode the singleton, we need to
593 // match the remaining undecoded encoding bits against the singleton.
594 void Filter::recurse() {
595 // Starts by inheriting our parent filter chooser's filter bit values.
596 std::vector
<bit_value_t
> BitValueArray(Owner
->FilterBitValues
);
598 if (!VariableInstructions
.empty()) {
599 // Conservatively marks each segment position as BIT_UNSET.
600 for (unsigned bitIndex
= 0; bitIndex
< NumBits
; ++bitIndex
)
601 BitValueArray
[StartBit
+ bitIndex
] = BIT_UNSET
;
603 // Delegates to an inferior filter chooser for further processing on this
604 // group of instructions whose segment values are variable.
605 FilterChooserMap
.insert(
606 std::make_pair(-1U, std::make_unique
<FilterChooser
>(
607 Owner
->AllInstructions
, VariableInstructions
,
608 Owner
->Operands
, BitValueArray
, *Owner
)));
611 // No need to recurse for a singleton filtered instruction.
612 // See also Filter::emit*().
613 if (getNumFiltered() == 1) {
614 assert(FilterChooserMap
.size() == 1);
618 // Otherwise, create sub choosers.
619 for (const auto &Inst
: FilteredInstructions
) {
621 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
622 for (unsigned bitIndex
= 0; bitIndex
< NumBits
; ++bitIndex
) {
623 if (Inst
.first
& (1ULL << bitIndex
))
624 BitValueArray
[StartBit
+ bitIndex
] = BIT_TRUE
;
626 BitValueArray
[StartBit
+ bitIndex
] = BIT_FALSE
;
629 // Delegates to an inferior filter chooser for further processing on this
630 // category of instructions.
631 FilterChooserMap
.insert(std::make_pair(
632 Inst
.first
, std::make_unique
<FilterChooser
>(
633 Owner
->AllInstructions
, Inst
.second
,
634 Owner
->Operands
, BitValueArray
, *Owner
)));
638 static void resolveTableFixups(DecoderTable
&Table
, const FixupList
&Fixups
,
640 // Any NumToSkip fixups in the current scope can resolve to the
642 for (FixupList::const_reverse_iterator I
= Fixups
.rbegin(),
645 // Calculate the distance from the byte following the fixup entry byte
646 // to the destination. The Target is calculated from after the 16-bit
647 // NumToSkip entry itself, so subtract two from the displacement here
648 // to account for that.
649 uint32_t FixupIdx
= *I
;
650 uint32_t Delta
= DestIdx
- FixupIdx
- 3;
651 // Our NumToSkip entries are 24-bits. Make sure our table isn't too
653 assert(Delta
< (1u << 24));
654 Table
[FixupIdx
] = (uint8_t)Delta
;
655 Table
[FixupIdx
+ 1] = (uint8_t)(Delta
>> 8);
656 Table
[FixupIdx
+ 2] = (uint8_t)(Delta
>> 16);
660 // Emit table entries to decode instructions given a segment or segments
662 void Filter::emitTableEntry(DecoderTableInfo
&TableInfo
) const {
663 TableInfo
.Table
.push_back(MCD::OPC_ExtractField
);
664 TableInfo
.Table
.push_back(StartBit
);
665 TableInfo
.Table
.push_back(NumBits
);
667 // A new filter entry begins a new scope for fixup resolution.
668 TableInfo
.FixupStack
.emplace_back();
670 DecoderTable
&Table
= TableInfo
.Table
;
672 size_t PrevFilter
= 0;
673 bool HasFallthrough
= false;
674 for (auto &Filter
: FilterChooserMap
) {
675 // Field value -1 implies a non-empty set of variable instructions.
676 // See also recurse().
677 if (Filter
.first
== (unsigned)-1) {
678 HasFallthrough
= true;
680 // Each scope should always have at least one filter value to check
682 assert(PrevFilter
!= 0 && "empty filter set!");
683 FixupList
&CurScope
= TableInfo
.FixupStack
.back();
684 // Resolve any NumToSkip fixups in the current scope.
685 resolveTableFixups(Table
, CurScope
, Table
.size());
687 PrevFilter
= 0; // Don't re-process the filter's fallthrough.
689 Table
.push_back(MCD::OPC_FilterValue
);
690 // Encode and emit the value to filter against.
692 unsigned Len
= encodeULEB128(Filter
.first
, Buffer
);
693 Table
.insert(Table
.end(), Buffer
, Buffer
+ Len
);
694 // Reserve space for the NumToSkip entry. We'll backpatch the value
696 PrevFilter
= Table
.size();
702 // We arrive at a category of instructions with the same segment value.
703 // Now delegate to the sub filter chooser for further decodings.
704 // The case may fallthrough, which happens if the remaining well-known
705 // encoding bits do not match exactly.
706 Filter
.second
->emitTableEntries(TableInfo
);
708 // Now that we've emitted the body of the handler, update the NumToSkip
709 // of the filter itself to be able to skip forward when false. Subtract
710 // two as to account for the width of the NumToSkip field itself.
712 uint32_t NumToSkip
= Table
.size() - PrevFilter
- 3;
713 assert(NumToSkip
< (1u << 24) && "disassembler decoding table too large!");
714 Table
[PrevFilter
] = (uint8_t)NumToSkip
;
715 Table
[PrevFilter
+ 1] = (uint8_t)(NumToSkip
>> 8);
716 Table
[PrevFilter
+ 2] = (uint8_t)(NumToSkip
>> 16);
720 // Any remaining unresolved fixups bubble up to the parent fixup scope.
721 assert(TableInfo
.FixupStack
.size() > 1 && "fixup stack underflow!");
722 FixupScopeList::iterator Source
= TableInfo
.FixupStack
.end() - 1;
723 FixupScopeList::iterator Dest
= Source
- 1;
724 Dest
->insert(Dest
->end(), Source
->begin(), Source
->end());
725 TableInfo
.FixupStack
.pop_back();
727 // If there is no fallthrough, then the final filter should get fixed
728 // up according to the enclosing scope rather than the current position.
730 TableInfo
.FixupStack
.back().push_back(PrevFilter
);
733 // Returns the number of fanout produced by the filter. More fanout implies
734 // the filter distinguishes more categories of instructions.
735 unsigned Filter::usefulness() const {
736 if (!VariableInstructions
.empty())
737 return FilteredInstructions
.size();
739 return FilteredInstructions
.size() + 1;
742 //////////////////////////////////
744 // Filterchooser Implementation //
746 //////////////////////////////////
748 // Emit the decoder state machine table.
749 void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream
&OS
,
751 unsigned Indentation
,
753 StringRef Namespace
) const {
754 OS
.indent(Indentation
) << "static const uint8_t DecoderTable" << Namespace
755 << BitWidth
<< "[] = {\n";
759 // FIXME: We may be able to use the NumToSkip values to recover
760 // appropriate indentation levels.
761 DecoderTable::const_iterator I
= Table
.begin();
762 DecoderTable::const_iterator E
= Table
.end();
764 assert (I
< E
&& "incomplete decode table entry!");
766 uint64_t Pos
= I
- Table
.begin();
767 OS
<< "/* " << Pos
<< " */";
772 PrintFatalError("invalid decode table opcode");
773 case MCD::OPC_ExtractField
: {
775 unsigned Start
= *I
++;
777 OS
.indent(Indentation
) << "MCD::OPC_ExtractField, " << Start
<< ", "
778 << Len
<< ", // Inst{";
780 OS
<< (Start
+ Len
- 1) << "-";
781 OS
<< Start
<< "} ...\n";
784 case MCD::OPC_FilterValue
: {
786 OS
.indent(Indentation
) << "MCD::OPC_FilterValue, ";
787 // The filter value is ULEB128 encoded.
789 OS
<< (unsigned)*I
++ << ", ";
790 OS
<< (unsigned)*I
++ << ", ";
792 // 24-bit numtoskip value.
794 uint32_t NumToSkip
= Byte
;
795 OS
<< (unsigned)Byte
<< ", ";
797 OS
<< (unsigned)Byte
<< ", ";
798 NumToSkip
|= Byte
<< 8;
800 OS
<< utostr(Byte
) << ", ";
801 NumToSkip
|= Byte
<< 16;
802 OS
<< "// Skip to: " << ((I
- Table
.begin()) + NumToSkip
) << "\n";
805 case MCD::OPC_CheckField
: {
807 unsigned Start
= *I
++;
809 OS
.indent(Indentation
) << "MCD::OPC_CheckField, " << Start
<< ", "
810 << Len
<< ", ";// << Val << ", " << NumToSkip << ",\n";
811 // ULEB128 encoded field value.
812 for (; *I
>= 128; ++I
)
813 OS
<< (unsigned)*I
<< ", ";
814 OS
<< (unsigned)*I
++ << ", ";
815 // 24-bit numtoskip value.
817 uint32_t NumToSkip
= Byte
;
818 OS
<< (unsigned)Byte
<< ", ";
820 OS
<< (unsigned)Byte
<< ", ";
821 NumToSkip
|= Byte
<< 8;
823 OS
<< utostr(Byte
) << ", ";
824 NumToSkip
|= Byte
<< 16;
825 OS
<< "// Skip to: " << ((I
- Table
.begin()) + NumToSkip
) << "\n";
828 case MCD::OPC_CheckPredicate
: {
830 OS
.indent(Indentation
) << "MCD::OPC_CheckPredicate, ";
831 for (; *I
>= 128; ++I
)
832 OS
<< (unsigned)*I
<< ", ";
833 OS
<< (unsigned)*I
++ << ", ";
835 // 24-bit numtoskip value.
837 uint32_t NumToSkip
= Byte
;
838 OS
<< (unsigned)Byte
<< ", ";
840 OS
<< (unsigned)Byte
<< ", ";
841 NumToSkip
|= Byte
<< 8;
843 OS
<< utostr(Byte
) << ", ";
844 NumToSkip
|= Byte
<< 16;
845 OS
<< "// Skip to: " << ((I
- Table
.begin()) + NumToSkip
) << "\n";
848 case MCD::OPC_Decode
:
849 case MCD::OPC_TryDecode
: {
850 bool IsTry
= *I
== MCD::OPC_TryDecode
;
852 // Extract the ULEB128 encoded Opcode to a buffer.
853 uint8_t Buffer
[16], *p
= Buffer
;
854 while ((*p
++ = *I
++) >= 128)
855 assert((p
- Buffer
) <= (ptrdiff_t)sizeof(Buffer
)
856 && "ULEB128 value too large!");
857 // Decode the Opcode value.
858 unsigned Opc
= decodeULEB128(Buffer
);
859 OS
.indent(Indentation
) << "MCD::OPC_" << (IsTry
? "Try" : "")
861 for (p
= Buffer
; *p
>= 128; ++p
)
862 OS
<< (unsigned)*p
<< ", ";
863 OS
<< (unsigned)*p
<< ", ";
866 for (; *I
>= 128; ++I
)
867 OS
<< (unsigned)*I
<< ", ";
868 OS
<< (unsigned)*I
++ << ", ";
871 OS
<< "// Opcode: " << NumberedEncodings
[Opc
] << "\n";
875 // Fallthrough for OPC_TryDecode.
877 // 24-bit numtoskip value.
879 uint32_t NumToSkip
= Byte
;
880 OS
<< (unsigned)Byte
<< ", ";
882 OS
<< (unsigned)Byte
<< ", ";
883 NumToSkip
|= Byte
<< 8;
885 OS
<< utostr(Byte
) << ", ";
886 NumToSkip
|= Byte
<< 16;
888 OS
<< "// Opcode: " << NumberedEncodings
[Opc
]
889 << ", skip to: " << ((I
- Table
.begin()) + NumToSkip
) << "\n";
892 case MCD::OPC_SoftFail
: {
894 OS
.indent(Indentation
) << "MCD::OPC_SoftFail";
899 OS
<< ", " << (unsigned)*I
;
900 Value
+= (*I
& 0x7f) << Shift
;
902 } while (*I
++ >= 128);
912 OS
<< ", " << (unsigned)*I
;
913 Value
+= (*I
& 0x7f) << Shift
;
915 } while (*I
++ >= 128);
924 case MCD::OPC_Fail
: {
926 OS
.indent(Indentation
) << "MCD::OPC_Fail,\n";
931 OS
.indent(Indentation
) << "0\n";
935 OS
.indent(Indentation
) << "};\n\n";
938 void FixedLenDecoderEmitter::
939 emitPredicateFunction(formatted_raw_ostream
&OS
, PredicateSet
&Predicates
,
940 unsigned Indentation
) const {
941 // The predicate function is just a big switch statement based on the
942 // input predicate index.
943 OS
.indent(Indentation
) << "static bool checkDecoderPredicate(unsigned Idx, "
944 << "const FeatureBitset& Bits) {\n";
946 if (!Predicates
.empty()) {
947 OS
.indent(Indentation
) << "switch (Idx) {\n";
948 OS
.indent(Indentation
) << "default: llvm_unreachable(\"Invalid index!\");\n";
950 for (const auto &Predicate
: Predicates
) {
951 OS
.indent(Indentation
) << "case " << Index
++ << ":\n";
952 OS
.indent(Indentation
+2) << "return (" << Predicate
<< ");\n";
954 OS
.indent(Indentation
) << "}\n";
956 // No case statement to emit
957 OS
.indent(Indentation
) << "llvm_unreachable(\"Invalid index!\");\n";
960 OS
.indent(Indentation
) << "}\n\n";
963 void FixedLenDecoderEmitter::
964 emitDecoderFunction(formatted_raw_ostream
&OS
, DecoderSet
&Decoders
,
965 unsigned Indentation
) const {
966 // The decoder function is just a big switch statement based on the
967 // input decoder index.
968 OS
.indent(Indentation
) << "template<typename InsnType>\n";
969 OS
.indent(Indentation
) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
970 << " unsigned Idx, InsnType insn, MCInst &MI,\n";
971 OS
.indent(Indentation
) << " uint64_t "
972 << "Address, const void *Decoder, bool &DecodeComplete) {\n";
974 OS
.indent(Indentation
) << "DecodeComplete = true;\n";
975 OS
.indent(Indentation
) << "InsnType tmp;\n";
976 OS
.indent(Indentation
) << "switch (Idx) {\n";
977 OS
.indent(Indentation
) << "default: llvm_unreachable(\"Invalid index!\");\n";
979 for (const auto &Decoder
: Decoders
) {
980 OS
.indent(Indentation
) << "case " << Index
++ << ":\n";
982 OS
.indent(Indentation
+2) << "return S;\n";
984 OS
.indent(Indentation
) << "}\n";
986 OS
.indent(Indentation
) << "}\n\n";
989 // Populates the field of the insn given the start position and the number of
990 // consecutive bits to scan for.
992 // Returns false if and on the first uninitialized bit value encountered.
993 // Returns true, otherwise.
994 bool FilterChooser::fieldFromInsn(uint64_t &Field
, insn_t
&Insn
,
995 unsigned StartBit
, unsigned NumBits
) const {
998 for (unsigned i
= 0; i
< NumBits
; ++i
) {
999 if (Insn
[StartBit
+ i
] == BIT_UNSET
)
1002 if (Insn
[StartBit
+ i
] == BIT_TRUE
)
1003 Field
= Field
| (1ULL << i
);
1009 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
1010 /// filter array as a series of chars.
1011 void FilterChooser::dumpFilterArray(raw_ostream
&o
,
1012 const std::vector
<bit_value_t
> &filter
) const {
1013 for (unsigned bitIndex
= BitWidth
; bitIndex
> 0; bitIndex
--) {
1014 switch (filter
[bitIndex
- 1]) {
1015 case BIT_UNFILTERED
:
1031 /// dumpStack - dumpStack traverses the filter chooser chain and calls
1032 /// dumpFilterArray on each filter chooser up to the top level one.
1033 void FilterChooser::dumpStack(raw_ostream
&o
, const char *prefix
) const {
1034 const FilterChooser
*current
= this;
1038 dumpFilterArray(o
, current
->FilterBitValues
);
1040 current
= current
->Parent
;
1044 // Calculates the island(s) needed to decode the instruction.
1045 // This returns a list of undecoded bits of an instructions, for example,
1046 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
1047 // decoded bits in order to verify that the instruction matches the Opcode.
1048 unsigned FilterChooser::getIslands(std::vector
<unsigned> &StartBits
,
1049 std::vector
<unsigned> &EndBits
,
1050 std::vector
<uint64_t> &FieldVals
,
1051 const insn_t
&Insn
) const {
1052 unsigned Num
, BitNo
;
1055 uint64_t FieldVal
= 0;
1058 // 1: Water (the bit value does not affect decoding)
1059 // 2: Island (well-known bit value needed for decoding)
1063 for (unsigned i
= 0; i
< BitWidth
; ++i
) {
1064 Val
= Value(Insn
[i
]);
1065 bool Filtered
= PositionFiltered(i
);
1067 default: llvm_unreachable("Unreachable code!");
1070 if (Filtered
|| Val
== -1)
1071 State
= 1; // Still in Water
1073 State
= 2; // Into the Island
1075 StartBits
.push_back(i
);
1080 if (Filtered
|| Val
== -1) {
1081 State
= 1; // Into the Water
1082 EndBits
.push_back(i
- 1);
1083 FieldVals
.push_back(FieldVal
);
1086 State
= 2; // Still in Island
1088 FieldVal
= FieldVal
| Val
<< BitNo
;
1093 // If we are still in Island after the loop, do some housekeeping.
1095 EndBits
.push_back(BitWidth
- 1);
1096 FieldVals
.push_back(FieldVal
);
1100 assert(StartBits
.size() == Num
&& EndBits
.size() == Num
&&
1101 FieldVals
.size() == Num
);
1105 void FilterChooser::emitBinaryParser(raw_ostream
&o
, unsigned &Indentation
,
1106 const OperandInfo
&OpInfo
,
1107 bool &OpHasCompleteDecoder
) const {
1108 const std::string
&Decoder
= OpInfo
.Decoder
;
1110 if (OpInfo
.numFields() != 1 || OpInfo
.InitValue
!= 0) {
1111 o
.indent(Indentation
) << "tmp = 0x";
1112 o
.write_hex(OpInfo
.InitValue
);
1116 for (const EncodingField
&EF
: OpInfo
) {
1117 o
.indent(Indentation
) << "tmp ";
1118 if (OpInfo
.numFields() != 1 || OpInfo
.InitValue
!= 0) o
<< '|';
1119 o
<< "= fieldFromInstruction"
1120 << "(insn, " << EF
.Base
<< ", " << EF
.Width
<< ')';
1121 if (OpInfo
.numFields() != 1 || EF
.Offset
!= 0)
1122 o
<< " << " << EF
.Offset
;
1126 if (Decoder
!= "") {
1127 OpHasCompleteDecoder
= OpInfo
.HasCompleteDecoder
;
1128 o
.indent(Indentation
) << Emitter
->GuardPrefix
<< Decoder
1129 << "(MI, tmp, Address, Decoder)"
1130 << Emitter
->GuardPostfix
1131 << " { " << (OpHasCompleteDecoder
? "" : "DecodeComplete = false; ")
1132 << "return MCDisassembler::Fail; }\n";
1134 OpHasCompleteDecoder
= true;
1135 o
.indent(Indentation
) << "MI.addOperand(MCOperand::createImm(tmp));\n";
1139 void FilterChooser::emitDecoder(raw_ostream
&OS
, unsigned Indentation
,
1140 unsigned Opc
, bool &HasCompleteDecoder
) const {
1141 HasCompleteDecoder
= true;
1143 for (const auto &Op
: Operands
.find(Opc
)->second
) {
1144 // If a custom instruction decoder was specified, use that.
1145 if (Op
.numFields() == 0 && !Op
.Decoder
.empty()) {
1146 HasCompleteDecoder
= Op
.HasCompleteDecoder
;
1147 OS
.indent(Indentation
) << Emitter
->GuardPrefix
<< Op
.Decoder
1148 << "(MI, insn, Address, Decoder)"
1149 << Emitter
->GuardPostfix
1150 << " { " << (HasCompleteDecoder
? "" : "DecodeComplete = false; ")
1151 << "return MCDisassembler::Fail; }\n";
1155 bool OpHasCompleteDecoder
;
1156 emitBinaryParser(OS
, Indentation
, Op
, OpHasCompleteDecoder
);
1157 if (!OpHasCompleteDecoder
)
1158 HasCompleteDecoder
= false;
1162 unsigned FilterChooser::getDecoderIndex(DecoderSet
&Decoders
,
1164 bool &HasCompleteDecoder
) const {
1165 // Build up the predicate string.
1166 SmallString
<256> Decoder
;
1167 // FIXME: emitDecoder() function can take a buffer directly rather than
1169 raw_svector_ostream
S(Decoder
);
1171 emitDecoder(S
, I
, Opc
, HasCompleteDecoder
);
1173 // Using the full decoder string as the key value here is a bit
1174 // heavyweight, but is effective. If the string comparisons become a
1175 // performance concern, we can implement a mangling of the predicate
1176 // data easily enough with a map back to the actual string. That's
1177 // overkill for now, though.
1179 // Make sure the predicate is in the table.
1180 Decoders
.insert(CachedHashString(Decoder
));
1181 // Now figure out the index for when we write out the table.
1182 DecoderSet::const_iterator P
= find(Decoders
, Decoder
.str());
1183 return (unsigned)(P
- Decoders
.begin());
1186 static void emitSinglePredicateMatch(raw_ostream
&o
, StringRef str
,
1187 const std::string
&PredicateNamespace
) {
1189 o
<< "!Bits[" << PredicateNamespace
<< "::"
1190 << str
.slice(1,str
.size()) << "]";
1192 o
<< "Bits[" << PredicateNamespace
<< "::" << str
<< "]";
1195 bool FilterChooser::emitPredicateMatch(raw_ostream
&o
, unsigned &Indentation
,
1196 unsigned Opc
) const {
1197 ListInit
*Predicates
=
1198 AllInstructions
[Opc
].EncodingDef
->getValueAsListInit("Predicates");
1199 bool IsFirstEmission
= true;
1200 for (unsigned i
= 0; i
< Predicates
->size(); ++i
) {
1201 Record
*Pred
= Predicates
->getElementAsRecord(i
);
1202 if (!Pred
->getValue("AssemblerMatcherPredicate"))
1205 StringRef P
= Pred
->getValueAsString("AssemblerCondString");
1210 if (!IsFirstEmission
)
1213 std::pair
<StringRef
, StringRef
> pairs
= P
.split(',');
1214 while (!pairs
.second
.empty()) {
1215 emitSinglePredicateMatch(o
, pairs
.first
, Emitter
->PredicateNamespace
);
1217 pairs
= pairs
.second
.split(',');
1219 emitSinglePredicateMatch(o
, pairs
.first
, Emitter
->PredicateNamespace
);
1220 IsFirstEmission
= false;
1222 return !Predicates
->empty();
1225 bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc
) const {
1226 ListInit
*Predicates
=
1227 AllInstructions
[Opc
].EncodingDef
->getValueAsListInit("Predicates");
1228 for (unsigned i
= 0; i
< Predicates
->size(); ++i
) {
1229 Record
*Pred
= Predicates
->getElementAsRecord(i
);
1230 if (!Pred
->getValue("AssemblerMatcherPredicate"))
1233 StringRef P
= Pred
->getValueAsString("AssemblerCondString");
1243 unsigned FilterChooser::getPredicateIndex(DecoderTableInfo
&TableInfo
,
1244 StringRef Predicate
) const {
1245 // Using the full predicate string as the key value here is a bit
1246 // heavyweight, but is effective. If the string comparisons become a
1247 // performance concern, we can implement a mangling of the predicate
1248 // data easily enough with a map back to the actual string. That's
1249 // overkill for now, though.
1251 // Make sure the predicate is in the table.
1252 TableInfo
.Predicates
.insert(CachedHashString(Predicate
));
1253 // Now figure out the index for when we write out the table.
1254 PredicateSet::const_iterator P
= find(TableInfo
.Predicates
, Predicate
);
1255 return (unsigned)(P
- TableInfo
.Predicates
.begin());
1258 void FilterChooser::emitPredicateTableEntry(DecoderTableInfo
&TableInfo
,
1259 unsigned Opc
) const {
1260 if (!doesOpcodeNeedPredicate(Opc
))
1263 // Build up the predicate string.
1264 SmallString
<256> Predicate
;
1265 // FIXME: emitPredicateMatch() functions can take a buffer directly rather
1267 raw_svector_ostream
PS(Predicate
);
1269 emitPredicateMatch(PS
, I
, Opc
);
1271 // Figure out the index into the predicate table for the predicate just
1273 unsigned PIdx
= getPredicateIndex(TableInfo
, PS
.str());
1274 SmallString
<16> PBytes
;
1275 raw_svector_ostream
S(PBytes
);
1276 encodeULEB128(PIdx
, S
);
1278 TableInfo
.Table
.push_back(MCD::OPC_CheckPredicate
);
1280 for (unsigned i
= 0, e
= PBytes
.size(); i
!= e
; ++i
)
1281 TableInfo
.Table
.push_back(PBytes
[i
]);
1282 // Push location for NumToSkip backpatching.
1283 TableInfo
.FixupStack
.back().push_back(TableInfo
.Table
.size());
1284 TableInfo
.Table
.push_back(0);
1285 TableInfo
.Table
.push_back(0);
1286 TableInfo
.Table
.push_back(0);
1289 void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo
&TableInfo
,
1290 unsigned Opc
) const {
1292 AllInstructions
[Opc
].EncodingDef
->getValueAsBitsInit("SoftFail");
1293 if (!SFBits
) return;
1294 BitsInit
*InstBits
=
1295 AllInstructions
[Opc
].EncodingDef
->getValueAsBitsInit("Inst");
1297 APInt
PositiveMask(BitWidth
, 0ULL);
1298 APInt
NegativeMask(BitWidth
, 0ULL);
1299 for (unsigned i
= 0; i
< BitWidth
; ++i
) {
1300 bit_value_t B
= bitFromBits(*SFBits
, i
);
1301 bit_value_t IB
= bitFromBits(*InstBits
, i
);
1303 if (B
!= BIT_TRUE
) continue;
1307 // The bit is meant to be false, so emit a check to see if it is true.
1308 PositiveMask
.setBit(i
);
1311 // The bit is meant to be true, so emit a check to see if it is false.
1312 NegativeMask
.setBit(i
);
1315 // The bit is not set; this must be an error!
1316 errs() << "SoftFail Conflict: bit SoftFail{" << i
<< "} in "
1317 << AllInstructions
[Opc
] << " is set but Inst{" << i
1319 << " - You can only mark a bit as SoftFail if it is fully defined"
1320 << " (1/0 - not '?') in Inst\n";
1325 bool NeedPositiveMask
= PositiveMask
.getBoolValue();
1326 bool NeedNegativeMask
= NegativeMask
.getBoolValue();
1328 if (!NeedPositiveMask
&& !NeedNegativeMask
)
1331 TableInfo
.Table
.push_back(MCD::OPC_SoftFail
);
1333 SmallString
<16> MaskBytes
;
1334 raw_svector_ostream
S(MaskBytes
);
1335 if (NeedPositiveMask
) {
1336 encodeULEB128(PositiveMask
.getZExtValue(), S
);
1337 for (unsigned i
= 0, e
= MaskBytes
.size(); i
!= e
; ++i
)
1338 TableInfo
.Table
.push_back(MaskBytes
[i
]);
1340 TableInfo
.Table
.push_back(0);
1341 if (NeedNegativeMask
) {
1343 encodeULEB128(NegativeMask
.getZExtValue(), S
);
1344 for (unsigned i
= 0, e
= MaskBytes
.size(); i
!= e
; ++i
)
1345 TableInfo
.Table
.push_back(MaskBytes
[i
]);
1347 TableInfo
.Table
.push_back(0);
1350 // Emits table entries to decode the singleton.
1351 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo
&TableInfo
,
1352 EncodingIDAndOpcode Opc
) const {
1353 std::vector
<unsigned> StartBits
;
1354 std::vector
<unsigned> EndBits
;
1355 std::vector
<uint64_t> FieldVals
;
1357 insnWithID(Insn
, Opc
.EncodingID
);
1359 // Look for islands of undecoded bits of the singleton.
1360 getIslands(StartBits
, EndBits
, FieldVals
, Insn
);
1362 unsigned Size
= StartBits
.size();
1364 // Emit the predicate table entry if one is needed.
1365 emitPredicateTableEntry(TableInfo
, Opc
.EncodingID
);
1367 // Check any additional encoding fields needed.
1368 for (unsigned I
= Size
; I
!= 0; --I
) {
1369 unsigned NumBits
= EndBits
[I
-1] - StartBits
[I
-1] + 1;
1370 TableInfo
.Table
.push_back(MCD::OPC_CheckField
);
1371 TableInfo
.Table
.push_back(StartBits
[I
-1]);
1372 TableInfo
.Table
.push_back(NumBits
);
1373 uint8_t Buffer
[16], *p
;
1374 encodeULEB128(FieldVals
[I
-1], Buffer
);
1375 for (p
= Buffer
; *p
>= 128 ; ++p
)
1376 TableInfo
.Table
.push_back(*p
);
1377 TableInfo
.Table
.push_back(*p
);
1378 // Push location for NumToSkip backpatching.
1379 TableInfo
.FixupStack
.back().push_back(TableInfo
.Table
.size());
1380 // The fixup is always 24-bits, so go ahead and allocate the space
1381 // in the table so all our relative position calculations work OK even
1382 // before we fully resolve the real value here.
1383 TableInfo
.Table
.push_back(0);
1384 TableInfo
.Table
.push_back(0);
1385 TableInfo
.Table
.push_back(0);
1388 // Check for soft failure of the match.
1389 emitSoftFailTableEntry(TableInfo
, Opc
.EncodingID
);
1391 bool HasCompleteDecoder
;
1393 getDecoderIndex(TableInfo
.Decoders
, Opc
.EncodingID
, HasCompleteDecoder
);
1395 // Produce OPC_Decode or OPC_TryDecode opcode based on the information
1396 // whether the instruction decoder is complete or not. If it is complete
1397 // then it handles all possible values of remaining variable/unfiltered bits
1398 // and for any value can determine if the bitpattern is a valid instruction
1399 // or not. This means OPC_Decode will be the final step in the decoding
1400 // process. If it is not complete, then the Fail return code from the
1401 // decoder method indicates that additional processing should be done to see
1402 // if there is any other instruction that also matches the bitpattern and
1404 TableInfo
.Table
.push_back(HasCompleteDecoder
? MCD::OPC_Decode
:
1405 MCD::OPC_TryDecode
);
1406 NumEncodingsSupported
++;
1407 uint8_t Buffer
[16], *p
;
1408 encodeULEB128(Opc
.Opcode
, Buffer
);
1409 for (p
= Buffer
; *p
>= 128 ; ++p
)
1410 TableInfo
.Table
.push_back(*p
);
1411 TableInfo
.Table
.push_back(*p
);
1413 SmallString
<16> Bytes
;
1414 raw_svector_ostream
S(Bytes
);
1415 encodeULEB128(DIdx
, S
);
1418 for (unsigned i
= 0, e
= Bytes
.size(); i
!= e
; ++i
)
1419 TableInfo
.Table
.push_back(Bytes
[i
]);
1421 if (!HasCompleteDecoder
) {
1422 // Push location for NumToSkip backpatching.
1423 TableInfo
.FixupStack
.back().push_back(TableInfo
.Table
.size());
1424 // Allocate the space for the fixup.
1425 TableInfo
.Table
.push_back(0);
1426 TableInfo
.Table
.push_back(0);
1427 TableInfo
.Table
.push_back(0);
1431 // Emits table entries to decode the singleton, and then to decode the rest.
1432 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo
&TableInfo
,
1433 const Filter
&Best
) const {
1434 EncodingIDAndOpcode Opc
= Best
.getSingletonOpc();
1436 // complex singletons need predicate checks from the first singleton
1437 // to refer forward to the variable filterchooser that follows.
1438 TableInfo
.FixupStack
.emplace_back();
1440 emitSingletonTableEntry(TableInfo
, Opc
);
1442 resolveTableFixups(TableInfo
.Table
, TableInfo
.FixupStack
.back(),
1443 TableInfo
.Table
.size());
1444 TableInfo
.FixupStack
.pop_back();
1446 Best
.getVariableFC().emitTableEntries(TableInfo
);
1449 // Assign a single filter and run with it. Top level API client can initialize
1450 // with a single filter to start the filtering process.
1451 void FilterChooser::runSingleFilter(unsigned startBit
, unsigned numBit
,
1454 Filters
.emplace_back(*this, startBit
, numBit
, true);
1455 BestIndex
= 0; // Sole Filter instance to choose from.
1456 bestFilter().recurse();
1459 // reportRegion is a helper function for filterProcessor to mark a region as
1460 // eligible for use as a filter region.
1461 void FilterChooser::reportRegion(bitAttr_t RA
, unsigned StartBit
,
1462 unsigned BitIndex
, bool AllowMixed
) {
1463 if (RA
== ATTR_MIXED
&& AllowMixed
)
1464 Filters
.emplace_back(*this, StartBit
, BitIndex
- StartBit
, true);
1465 else if (RA
== ATTR_ALL_SET
&& !AllowMixed
)
1466 Filters
.emplace_back(*this, StartBit
, BitIndex
- StartBit
, false);
1469 // FilterProcessor scans the well-known encoding bits of the instructions and
1470 // builds up a list of candidate filters. It chooses the best filter and
1471 // recursively descends down the decoding tree.
1472 bool FilterChooser::filterProcessor(bool AllowMixed
, bool Greedy
) {
1475 unsigned numInstructions
= Opcodes
.size();
1477 assert(numInstructions
&& "Filter created with no instructions");
1479 // No further filtering is necessary.
1480 if (numInstructions
== 1)
1483 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1484 // instructions is 3.
1485 if (AllowMixed
&& !Greedy
) {
1486 assert(numInstructions
== 3);
1488 for (unsigned i
= 0; i
< Opcodes
.size(); ++i
) {
1489 std::vector
<unsigned> StartBits
;
1490 std::vector
<unsigned> EndBits
;
1491 std::vector
<uint64_t> FieldVals
;
1494 insnWithID(Insn
, Opcodes
[i
].EncodingID
);
1496 // Look for islands of undecoded bits of any instruction.
1497 if (getIslands(StartBits
, EndBits
, FieldVals
, Insn
) > 0) {
1498 // Found an instruction with island(s). Now just assign a filter.
1499 runSingleFilter(StartBits
[0], EndBits
[0] - StartBits
[0] + 1, true);
1507 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1508 // The automaton consumes the corresponding bit from each
1511 // Input symbols: 0, 1, and _ (unset).
1512 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1513 // Initial state: NONE.
1515 // (NONE) ------- [01] -> (ALL_SET)
1516 // (NONE) ------- _ ----> (ALL_UNSET)
1517 // (ALL_SET) ---- [01] -> (ALL_SET)
1518 // (ALL_SET) ---- _ ----> (MIXED)
1519 // (ALL_UNSET) -- [01] -> (MIXED)
1520 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1521 // (MIXED) ------ . ----> (MIXED)
1522 // (FILTERED)---- . ----> (FILTERED)
1524 std::vector
<bitAttr_t
> bitAttrs
;
1526 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1527 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1528 for (BitIndex
= 0; BitIndex
< BitWidth
; ++BitIndex
)
1529 if (FilterBitValues
[BitIndex
] == BIT_TRUE
||
1530 FilterBitValues
[BitIndex
] == BIT_FALSE
)
1531 bitAttrs
.push_back(ATTR_FILTERED
);
1533 bitAttrs
.push_back(ATTR_NONE
);
1535 for (unsigned InsnIndex
= 0; InsnIndex
< numInstructions
; ++InsnIndex
) {
1538 insnWithID(insn
, Opcodes
[InsnIndex
].EncodingID
);
1540 for (BitIndex
= 0; BitIndex
< BitWidth
; ++BitIndex
) {
1541 switch (bitAttrs
[BitIndex
]) {
1543 if (insn
[BitIndex
] == BIT_UNSET
)
1544 bitAttrs
[BitIndex
] = ATTR_ALL_UNSET
;
1546 bitAttrs
[BitIndex
] = ATTR_ALL_SET
;
1549 if (insn
[BitIndex
] == BIT_UNSET
)
1550 bitAttrs
[BitIndex
] = ATTR_MIXED
;
1552 case ATTR_ALL_UNSET
:
1553 if (insn
[BitIndex
] != BIT_UNSET
)
1554 bitAttrs
[BitIndex
] = ATTR_MIXED
;
1563 // The regionAttr automaton consumes the bitAttrs automatons' state,
1564 // lowest-to-highest.
1566 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1567 // States: NONE, ALL_SET, MIXED
1568 // Initial state: NONE
1570 // (NONE) ----- F --> (NONE)
1571 // (NONE) ----- S --> (ALL_SET) ; and set region start
1572 // (NONE) ----- U --> (NONE)
1573 // (NONE) ----- M --> (MIXED) ; and set region start
1574 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1575 // (ALL_SET) -- S --> (ALL_SET)
1576 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1577 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1578 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1579 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1580 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1581 // (MIXED) ---- M --> (MIXED)
1583 bitAttr_t RA
= ATTR_NONE
;
1584 unsigned StartBit
= 0;
1586 for (BitIndex
= 0; BitIndex
< BitWidth
; ++BitIndex
) {
1587 bitAttr_t bitAttr
= bitAttrs
[BitIndex
];
1589 assert(bitAttr
!= ATTR_NONE
&& "Bit without attributes");
1597 StartBit
= BitIndex
;
1600 case ATTR_ALL_UNSET
:
1603 StartBit
= BitIndex
;
1607 llvm_unreachable("Unexpected bitAttr!");
1613 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1618 case ATTR_ALL_UNSET
:
1619 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1623 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1624 StartBit
= BitIndex
;
1628 llvm_unreachable("Unexpected bitAttr!");
1634 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1635 StartBit
= BitIndex
;
1639 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1640 StartBit
= BitIndex
;
1643 case ATTR_ALL_UNSET
:
1644 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1650 llvm_unreachable("Unexpected bitAttr!");
1653 case ATTR_ALL_UNSET
:
1654 llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
1656 llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
1660 // At the end, if we're still in ALL_SET or MIXED states, report a region
1667 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1669 case ATTR_ALL_UNSET
:
1672 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1676 // We have finished with the filter processings. Now it's time to choose
1677 // the best performing filter.
1679 bool AllUseless
= true;
1680 unsigned BestScore
= 0;
1682 for (unsigned i
= 0, e
= Filters
.size(); i
!= e
; ++i
) {
1683 unsigned Usefulness
= Filters
[i
].usefulness();
1688 if (Usefulness
> BestScore
) {
1690 BestScore
= Usefulness
;
1695 bestFilter().recurse();
1698 } // end of FilterChooser::filterProcessor(bool)
1700 // Decides on the best configuration of filter(s) to use in order to decode
1701 // the instructions. A conflict of instructions may occur, in which case we
1702 // dump the conflict set to the standard error.
1703 void FilterChooser::doFilter() {
1704 unsigned Num
= Opcodes
.size();
1705 assert(Num
&& "FilterChooser created with no instructions");
1707 // Try regions of consecutive known bit values first.
1708 if (filterProcessor(false))
1711 // Then regions of mixed bits (both known and unitialized bit values allowed).
1712 if (filterProcessor(true))
1715 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1716 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1717 // well-known encoding pattern. In such case, we backtrack and scan for the
1718 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1719 if (Num
== 3 && filterProcessor(true, false))
1722 // If we come to here, the instruction decoding has failed.
1723 // Set the BestIndex to -1 to indicate so.
1727 // emitTableEntries - Emit state machine entries to decode our share of
1729 void FilterChooser::emitTableEntries(DecoderTableInfo
&TableInfo
) const {
1730 if (Opcodes
.size() == 1) {
1731 // There is only one instruction in the set, which is great!
1732 // Call emitSingletonDecoder() to see whether there are any remaining
1734 emitSingletonTableEntry(TableInfo
, Opcodes
[0]);
1738 // Choose the best filter to do the decodings!
1739 if (BestIndex
!= -1) {
1740 const Filter
&Best
= Filters
[BestIndex
];
1741 if (Best
.getNumFiltered() == 1)
1742 emitSingletonTableEntry(TableInfo
, Best
);
1744 Best
.emitTableEntry(TableInfo
);
1748 // We don't know how to decode these instructions! Dump the
1749 // conflict set and bail.
1751 // Print out useful conflict information for postmortem analysis.
1752 errs() << "Decoding Conflict:\n";
1754 dumpStack(errs(), "\t\t");
1756 for (unsigned i
= 0; i
< Opcodes
.size(); ++i
) {
1758 emitNameWithID(errs(), Opcodes
[i
].EncodingID
);
1762 getBitsField(*AllInstructions
[Opcodes
[i
].EncodingID
].EncodingDef
, "Inst"));
1767 static std::string
findOperandDecoderMethod(TypedInit
*TI
) {
1768 std::string Decoder
;
1770 Record
*Record
= cast
<DefInit
>(TI
)->getDef();
1772 RecordVal
*DecoderString
= Record
->getValue("DecoderMethod");
1773 StringInit
*String
= DecoderString
?
1774 dyn_cast
<StringInit
>(DecoderString
->getValue()) : nullptr;
1776 Decoder
= String
->getValue();
1777 if (!Decoder
.empty())
1781 if (Record
->isSubClassOf("RegisterOperand"))
1782 Record
= Record
->getValueAsDef("RegClass");
1784 if (Record
->isSubClassOf("RegisterClass")) {
1785 Decoder
= "Decode" + Record
->getName().str() + "RegisterClass";
1786 } else if (Record
->isSubClassOf("PointerLikeRegClass")) {
1787 Decoder
= "DecodePointerLikeRegClass" +
1788 utostr(Record
->getValueAsInt("RegClassKind"));
1795 populateInstruction(CodeGenTarget
&Target
, const Record
&EncodingDef
,
1796 const CodeGenInstruction
&CGI
, unsigned Opc
,
1797 std::map
<unsigned, std::vector
<OperandInfo
>> &Operands
) {
1798 const Record
&Def
= *CGI
.TheDef
;
1799 // If all the bit positions are not specified; do not decode this instruction.
1800 // We are bound to fail! For proper disassembly, the well-known encoding bits
1801 // of the instruction must be fully specified.
1803 BitsInit
&Bits
= getBitsField(EncodingDef
, "Inst");
1804 if (Bits
.allInComplete()) return false;
1806 std::vector
<OperandInfo
> InsnOperands
;
1808 // If the instruction has specified a custom decoding hook, use that instead
1809 // of trying to auto-generate the decoder.
1810 StringRef InstDecoder
= EncodingDef
.getValueAsString("DecoderMethod");
1811 if (InstDecoder
!= "") {
1812 bool HasCompleteInstDecoder
= EncodingDef
.getValueAsBit("hasCompleteDecoder");
1813 InsnOperands
.push_back(OperandInfo(InstDecoder
, HasCompleteInstDecoder
));
1814 Operands
[Opc
] = InsnOperands
;
1818 // Generate a description of the operand of the instruction that we know
1819 // how to decode automatically.
1820 // FIXME: We'll need to have a way to manually override this as needed.
1822 // Gather the outputs/inputs of the instruction, so we can find their
1823 // positions in the encoding. This assumes for now that they appear in the
1824 // MCInst in the order that they're listed.
1825 std::vector
<std::pair
<Init
*, StringRef
>> InOutOperands
;
1826 DagInit
*Out
= Def
.getValueAsDag("OutOperandList");
1827 DagInit
*In
= Def
.getValueAsDag("InOperandList");
1828 for (unsigned i
= 0; i
< Out
->getNumArgs(); ++i
)
1829 InOutOperands
.push_back(std::make_pair(Out
->getArg(i
),
1830 Out
->getArgNameStr(i
)));
1831 for (unsigned i
= 0; i
< In
->getNumArgs(); ++i
)
1832 InOutOperands
.push_back(std::make_pair(In
->getArg(i
),
1833 In
->getArgNameStr(i
)));
1835 // Search for tied operands, so that we can correctly instantiate
1836 // operands that are not explicitly represented in the encoding.
1837 std::map
<std::string
, std::string
> TiedNames
;
1838 for (unsigned i
= 0; i
< CGI
.Operands
.size(); ++i
) {
1839 int tiedTo
= CGI
.Operands
[i
].getTiedRegister();
1841 std::pair
<unsigned, unsigned> SO
=
1842 CGI
.Operands
.getSubOperandNumber(tiedTo
);
1843 TiedNames
[InOutOperands
[i
].second
] = InOutOperands
[SO
.first
].second
;
1844 TiedNames
[InOutOperands
[SO
.first
].second
] = InOutOperands
[i
].second
;
1848 std::map
<std::string
, std::vector
<OperandInfo
>> NumberedInsnOperands
;
1849 std::set
<std::string
> NumberedInsnOperandsNoTie
;
1850 if (Target
.getInstructionSet()->
1851 getValueAsBit("decodePositionallyEncodedOperands")) {
1852 const std::vector
<RecordVal
> &Vals
= Def
.getValues();
1853 unsigned NumberedOp
= 0;
1855 std::set
<unsigned> NamedOpIndices
;
1856 if (Target
.getInstructionSet()->
1857 getValueAsBit("noNamedPositionallyEncodedOperands"))
1858 // Collect the set of operand indices that might correspond to named
1859 // operand, and skip these when assigning operands based on position.
1860 for (unsigned i
= 0, e
= Vals
.size(); i
!= e
; ++i
) {
1862 if (!CGI
.Operands
.hasOperandNamed(Vals
[i
].getName(), OpIdx
))
1865 NamedOpIndices
.insert(OpIdx
);
1868 for (unsigned i
= 0, e
= Vals
.size(); i
!= e
; ++i
) {
1869 // Ignore fixed fields in the record, we're looking for values like:
1870 // bits<5> RST = { ?, ?, ?, ?, ? };
1871 if (Vals
[i
].getPrefix() || Vals
[i
].getValue()->isComplete())
1874 // Determine if Vals[i] actually contributes to the Inst encoding.
1876 for (; bi
< Bits
.getNumBits(); ++bi
) {
1877 VarInit
*Var
= nullptr;
1878 VarBitInit
*BI
= dyn_cast
<VarBitInit
>(Bits
.getBit(bi
));
1880 Var
= dyn_cast
<VarInit
>(BI
->getBitVar());
1882 Var
= dyn_cast
<VarInit
>(Bits
.getBit(bi
));
1884 if (Var
&& Var
->getName() == Vals
[i
].getName())
1888 if (bi
== Bits
.getNumBits())
1891 // Skip variables that correspond to explicitly-named operands.
1893 if (CGI
.Operands
.hasOperandNamed(Vals
[i
].getName(), OpIdx
))
1896 // Get the bit range for this operand:
1897 unsigned bitStart
= bi
++, bitWidth
= 1;
1898 for (; bi
< Bits
.getNumBits(); ++bi
) {
1899 VarInit
*Var
= nullptr;
1900 VarBitInit
*BI
= dyn_cast
<VarBitInit
>(Bits
.getBit(bi
));
1902 Var
= dyn_cast
<VarInit
>(BI
->getBitVar());
1904 Var
= dyn_cast
<VarInit
>(Bits
.getBit(bi
));
1909 if (Var
->getName() != Vals
[i
].getName())
1915 unsigned NumberOps
= CGI
.Operands
.size();
1916 while (NumberedOp
< NumberOps
&&
1917 (CGI
.Operands
.isFlatOperandNotEmitted(NumberedOp
) ||
1918 (!NamedOpIndices
.empty() && NamedOpIndices
.count(
1919 CGI
.Operands
.getSubOperandNumber(NumberedOp
).first
))))
1922 OpIdx
= NumberedOp
++;
1924 // OpIdx now holds the ordered operand number of Vals[i].
1925 std::pair
<unsigned, unsigned> SO
=
1926 CGI
.Operands
.getSubOperandNumber(OpIdx
);
1927 const std::string
&Name
= CGI
.Operands
[SO
.first
].Name
;
1929 LLVM_DEBUG(dbgs() << "Numbered operand mapping for " << Def
.getName()
1930 << ": " << Name
<< "(" << SO
.first
<< ", " << SO
.second
1931 << ") => " << Vals
[i
].getName() << "\n");
1933 std::string Decoder
;
1934 Record
*TypeRecord
= CGI
.Operands
[SO
.first
].Rec
;
1936 RecordVal
*DecoderString
= TypeRecord
->getValue("DecoderMethod");
1937 StringInit
*String
= DecoderString
?
1938 dyn_cast
<StringInit
>(DecoderString
->getValue()) : nullptr;
1939 if (String
&& String
->getValue() != "")
1940 Decoder
= String
->getValue();
1942 if (Decoder
== "" &&
1943 CGI
.Operands
[SO
.first
].MIOperandInfo
&&
1944 CGI
.Operands
[SO
.first
].MIOperandInfo
->getNumArgs()) {
1945 Init
*Arg
= CGI
.Operands
[SO
.first
].MIOperandInfo
->
1947 if (DefInit
*DI
= cast
<DefInit
>(Arg
))
1948 TypeRecord
= DI
->getDef();
1952 if (TypeRecord
->isSubClassOf("RegisterOperand"))
1953 TypeRecord
= TypeRecord
->getValueAsDef("RegClass");
1954 if (TypeRecord
->isSubClassOf("RegisterClass")) {
1955 Decoder
= "Decode" + TypeRecord
->getName().str() + "RegisterClass";
1957 } else if (TypeRecord
->isSubClassOf("PointerLikeRegClass")) {
1958 Decoder
= "DecodePointerLikeRegClass" +
1959 utostr(TypeRecord
->getValueAsInt("RegClassKind"));
1963 DecoderString
= TypeRecord
->getValue("DecoderMethod");
1964 String
= DecoderString
?
1965 dyn_cast
<StringInit
>(DecoderString
->getValue()) : nullptr;
1966 if (!isReg
&& String
&& String
->getValue() != "")
1967 Decoder
= String
->getValue();
1969 RecordVal
*HasCompleteDecoderVal
=
1970 TypeRecord
->getValue("hasCompleteDecoder");
1971 BitInit
*HasCompleteDecoderBit
= HasCompleteDecoderVal
?
1972 dyn_cast
<BitInit
>(HasCompleteDecoderVal
->getValue()) : nullptr;
1973 bool HasCompleteDecoder
= HasCompleteDecoderBit
?
1974 HasCompleteDecoderBit
->getValue() : true;
1976 OperandInfo
OpInfo(Decoder
, HasCompleteDecoder
);
1977 OpInfo
.addField(bitStart
, bitWidth
, 0);
1979 NumberedInsnOperands
[Name
].push_back(OpInfo
);
1981 // FIXME: For complex operands with custom decoders we can't handle tied
1982 // sub-operands automatically. Skip those here and assume that this is
1983 // fixed up elsewhere.
1984 if (CGI
.Operands
[SO
.first
].MIOperandInfo
&&
1985 CGI
.Operands
[SO
.first
].MIOperandInfo
->getNumArgs() > 1 &&
1986 String
&& String
->getValue() != "")
1987 NumberedInsnOperandsNoTie
.insert(Name
);
1991 // For each operand, see if we can figure out where it is encoded.
1992 for (const auto &Op
: InOutOperands
) {
1993 if (!NumberedInsnOperands
[Op
.second
].empty()) {
1994 InsnOperands
.insert(InsnOperands
.end(),
1995 NumberedInsnOperands
[Op
.second
].begin(),
1996 NumberedInsnOperands
[Op
.second
].end());
1999 if (!NumberedInsnOperands
[TiedNames
[Op
.second
]].empty()) {
2000 if (!NumberedInsnOperandsNoTie
.count(TiedNames
[Op
.second
])) {
2001 // Figure out to which (sub)operand we're tied.
2002 unsigned i
= CGI
.Operands
.getOperandNamed(TiedNames
[Op
.second
]);
2003 int tiedTo
= CGI
.Operands
[i
].getTiedRegister();
2005 i
= CGI
.Operands
.getOperandNamed(Op
.second
);
2006 tiedTo
= CGI
.Operands
[i
].getTiedRegister();
2010 std::pair
<unsigned, unsigned> SO
=
2011 CGI
.Operands
.getSubOperandNumber(tiedTo
);
2013 InsnOperands
.push_back(NumberedInsnOperands
[TiedNames
[Op
.second
]]
2020 TypedInit
*TI
= cast
<TypedInit
>(Op
.first
);
2022 // At this point, we can locate the decoder field, but we need to know how
2023 // to interpret it. As a first step, require the target to provide
2024 // callbacks for decoding register classes.
2025 std::string Decoder
= findOperandDecoderMethod(TI
);
2026 Record
*TypeRecord
= cast
<DefInit
>(TI
)->getDef();
2028 RecordVal
*HasCompleteDecoderVal
=
2029 TypeRecord
->getValue("hasCompleteDecoder");
2030 BitInit
*HasCompleteDecoderBit
= HasCompleteDecoderVal
?
2031 dyn_cast
<BitInit
>(HasCompleteDecoderVal
->getValue()) : nullptr;
2032 bool HasCompleteDecoder
= HasCompleteDecoderBit
?
2033 HasCompleteDecoderBit
->getValue() : true;
2035 OperandInfo
OpInfo(Decoder
, HasCompleteDecoder
);
2037 // Some bits of the operand may be required to be 1 depending on the
2038 // instruction's encoding. Collect those bits.
2039 if (const RecordVal
*EncodedValue
= EncodingDef
.getValue(Op
.second
))
2040 if (const BitsInit
*OpBits
= dyn_cast
<BitsInit
>(EncodedValue
->getValue()))
2041 for (unsigned I
= 0; I
< OpBits
->getNumBits(); ++I
)
2042 if (const BitInit
*OpBit
= dyn_cast
<BitInit
>(OpBits
->getBit(I
)))
2043 if (OpBit
->getValue())
2044 OpInfo
.InitValue
|= 1ULL << I
;
2046 unsigned Base
= ~0U;
2048 unsigned Offset
= 0;
2050 for (unsigned bi
= 0; bi
< Bits
.getNumBits(); ++bi
) {
2051 VarInit
*Var
= nullptr;
2052 VarBitInit
*BI
= dyn_cast
<VarBitInit
>(Bits
.getBit(bi
));
2054 Var
= dyn_cast
<VarInit
>(BI
->getBitVar());
2056 Var
= dyn_cast
<VarInit
>(Bits
.getBit(bi
));
2060 OpInfo
.addField(Base
, Width
, Offset
);
2068 if (Var
->getName() != Op
.second
&&
2069 Var
->getName() != TiedNames
[Op
.second
]) {
2071 OpInfo
.addField(Base
, Width
, Offset
);
2082 Offset
= BI
? BI
->getBitNum() : 0;
2083 } else if (BI
&& BI
->getBitNum() != Offset
+ Width
) {
2084 OpInfo
.addField(Base
, Width
, Offset
);
2087 Offset
= BI
->getBitNum();
2094 OpInfo
.addField(Base
, Width
, Offset
);
2096 if (OpInfo
.numFields() > 0)
2097 InsnOperands
.push_back(OpInfo
);
2100 Operands
[Opc
] = InsnOperands
;
2104 // Dumps the instruction encoding bits.
2105 dumpBits(errs(), Bits
);
2109 // Dumps the list of operand info.
2110 for (unsigned i
= 0, e
= CGI
.Operands
.size(); i
!= e
; ++i
) {
2111 const CGIOperandList::OperandInfo
&Info
= CGI
.Operands
[i
];
2112 const std::string
&OperandName
= Info
.Name
;
2113 const Record
&OperandDef
= *Info
.Rec
;
2115 errs() << "\t" << OperandName
<< " (" << OperandDef
.getName() << ")\n";
2123 // emitFieldFromInstruction - Emit the templated helper function
2124 // fieldFromInstruction().
2125 // On Windows we make sure that this function is not inlined when
2126 // using the VS compiler. It has a bug which causes the function
2127 // to be optimized out in some circustances. See llvm.org/pr38292
2128 static void emitFieldFromInstruction(formatted_raw_ostream
&OS
) {
2129 OS
<< "// Helper functions for extracting fields from encoded instructions.\n"
2130 << "// InsnType must either be integral or an APInt-like object that "
2132 << "// * Have a static const max_size_in_bits equal to the number of bits "
2135 << "// * be default-constructible and copy-constructible\n"
2136 << "// * be constructible from a uint64_t\n"
2137 << "// * be constructible from an APInt (this can be private)\n"
2138 << "// * Support getBitsSet(loBit, hiBit)\n"
2139 << "// * be convertible to uint64_t\n"
2140 << "// * Support the ~, &, ==, !=, and |= operators with other objects of "
2142 << "// * Support shift (<<, >>) with signed and unsigned integers on the "
2144 << "// * Support put (<<) to raw_ostream&\n"
2145 << "template<typename InsnType>\n"
2146 << "#if defined(_MSC_VER) && !defined(__clang__)\n"
2147 << "__declspec(noinline)\n"
2149 << "static InsnType fieldFromInstruction(InsnType insn, unsigned "
2151 << " unsigned numBits, "
2152 "std::true_type) {\n"
2153 << " assert(startBit + numBits <= 64 && \"Cannot support >64-bit "
2154 "extractions!\");\n"
2155 << " assert(startBit + numBits <= (sizeof(InsnType) * 8) &&\n"
2156 << " \"Instruction field out of bounds!\");\n"
2157 << " InsnType fieldMask;\n"
2158 << " if (numBits == sizeof(InsnType) * 8)\n"
2159 << " fieldMask = (InsnType)(-1LL);\n"
2161 << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
2162 << " return (insn & fieldMask) >> startBit;\n"
2165 << "template<typename InsnType>\n"
2166 << "static InsnType fieldFromInstruction(InsnType insn, unsigned "
2168 << " unsigned numBits, "
2169 "std::false_type) {\n"
2170 << " assert(startBit + numBits <= InsnType::max_size_in_bits && "
2171 "\"Instruction field out of bounds!\");\n"
2172 << " InsnType fieldMask = InsnType::getBitsSet(0, numBits);\n"
2173 << " return (insn >> startBit) & fieldMask;\n"
2176 << "template<typename InsnType>\n"
2177 << "static InsnType fieldFromInstruction(InsnType insn, unsigned "
2179 << " unsigned numBits) {\n"
2180 << " return fieldFromInstruction(insn, startBit, numBits, "
2181 "std::is_integral<InsnType>());\n"
2185 // emitDecodeInstruction - Emit the templated helper function
2186 // decodeInstruction().
2187 static void emitDecodeInstruction(formatted_raw_ostream
&OS
) {
2188 OS
<< "template<typename InsnType>\n"
2189 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
2191 << " InsnType insn, uint64_t "
2193 << " const void *DisAsm,\n"
2194 << " const MCSubtargetInfo &STI) {\n"
2195 << " const FeatureBitset& Bits = STI.getFeatureBits();\n"
2197 << " const uint8_t *Ptr = DecodeTable;\n"
2198 << " InsnType CurFieldValue = 0;\n"
2199 << " DecodeStatus S = MCDisassembler::Success;\n"
2200 << " while (true) {\n"
2201 << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
2202 << " switch (*Ptr) {\n"
2204 << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
2205 << " return MCDisassembler::Fail;\n"
2206 << " case MCD::OPC_ExtractField: {\n"
2207 << " unsigned Start = *++Ptr;\n"
2208 << " unsigned Len = *++Ptr;\n"
2210 << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
2211 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << "
2213 << " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
2216 << " case MCD::OPC_FilterValue: {\n"
2217 << " // Decode the field value.\n"
2218 << " unsigned Len;\n"
2219 << " InsnType Val = decodeULEB128(++Ptr, &Len);\n"
2221 << " // NumToSkip is a plain 24-bit integer.\n"
2222 << " unsigned NumToSkip = *Ptr++;\n"
2223 << " NumToSkip |= (*Ptr++) << 8;\n"
2224 << " NumToSkip |= (*Ptr++) << 16;\n"
2226 << " // Perform the filter operation.\n"
2227 << " if (Val != CurFieldValue)\n"
2228 << " Ptr += NumToSkip;\n"
2229 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << "
2230 "\", \" << NumToSkip\n"
2231 << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" "
2233 << " << \" continuing at \" << (Ptr - DecodeTable) << "
2238 << " case MCD::OPC_CheckField: {\n"
2239 << " unsigned Start = *++Ptr;\n"
2240 << " unsigned Len = *++Ptr;\n"
2241 << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
2242 << " // Decode the field value.\n"
2243 << " InsnType ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
2245 << " // NumToSkip is a plain 24-bit integer.\n"
2246 << " unsigned NumToSkip = *Ptr++;\n"
2247 << " NumToSkip |= (*Ptr++) << 8;\n"
2248 << " NumToSkip |= (*Ptr++) << 16;\n"
2250 << " // If the actual and expected values don't match, skip.\n"
2251 << " if (ExpectedValue != FieldValue)\n"
2252 << " Ptr += NumToSkip;\n"
2253 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << "
2255 << " << Len << \", \" << ExpectedValue << \", \" << "
2257 << " << \"): FieldValue = \" << FieldValue << \", "
2258 "ExpectedValue = \"\n"
2259 << " << ExpectedValue << \": \"\n"
2260 << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : "
2264 << " case MCD::OPC_CheckPredicate: {\n"
2265 << " unsigned Len;\n"
2266 << " // Decode the Predicate Index value.\n"
2267 << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
2269 << " // NumToSkip is a plain 24-bit integer.\n"
2270 << " unsigned NumToSkip = *Ptr++;\n"
2271 << " NumToSkip |= (*Ptr++) << 8;\n"
2272 << " NumToSkip |= (*Ptr++) << 16;\n"
2273 << " // Check the predicate.\n"
2275 << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
2276 << " Ptr += NumToSkip;\n"
2278 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx "
2280 << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2284 << " case MCD::OPC_Decode: {\n"
2285 << " unsigned Len;\n"
2286 << " // Decode the Opcode value.\n"
2287 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2289 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2293 << " MI.setOpcode(Opc);\n"
2294 << " bool DecodeComplete;\n"
2295 << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, "
2296 "DecodeComplete);\n"
2297 << " assert(DecodeComplete);\n"
2299 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
2300 << " << \", using decoder \" << DecodeIdx << \": \"\n"
2301 << " << (S != MCDisassembler::Fail ? \"PASS\" : "
2302 "\"FAIL\") << \"\\n\");\n"
2305 << " case MCD::OPC_TryDecode: {\n"
2306 << " unsigned Len;\n"
2307 << " // Decode the Opcode value.\n"
2308 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2310 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2312 << " // NumToSkip is a plain 24-bit integer.\n"
2313 << " unsigned NumToSkip = *Ptr++;\n"
2314 << " NumToSkip |= (*Ptr++) << 8;\n"
2315 << " NumToSkip |= (*Ptr++) << 16;\n"
2317 << " // Perform the decode operation.\n"
2318 << " MCInst TmpMI;\n"
2319 << " TmpMI.setOpcode(Opc);\n"
2320 << " bool DecodeComplete;\n"
2321 << " S = decodeToMCInst(S, DecodeIdx, insn, TmpMI, Address, DisAsm, "
2322 "DecodeComplete);\n"
2323 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_TryDecode: opcode \" << "
2325 << " << \", using decoder \" << DecodeIdx << \": \");\n"
2327 << " if (DecodeComplete) {\n"
2328 << " // Decoding complete.\n"
2329 << " LLVM_DEBUG(dbgs() << (S != MCDisassembler::Fail ? \"PASS\" : "
2330 "\"FAIL\") << \"\\n\");\n"
2334 << " assert(S == MCDisassembler::Fail);\n"
2335 << " // If the decoding was incomplete, skip.\n"
2336 << " Ptr += NumToSkip;\n"
2337 << " LLVM_DEBUG(dbgs() << \"FAIL: continuing at \" << (Ptr - "
2338 "DecodeTable) << \"\\n\");\n"
2339 << " // Reset decode status. This also drops a SoftFail status "
2341 << " // set before the decode attempt.\n"
2342 << " S = MCDisassembler::Success;\n"
2346 << " case MCD::OPC_SoftFail: {\n"
2347 << " // Decode the mask values.\n"
2348 << " unsigned Len;\n"
2349 << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n"
2351 << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n"
2353 << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n"
2355 << " S = MCDisassembler::SoftFail;\n"
2356 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? "
2357 "\"FAIL\\n\":\"PASS\\n\"));\n"
2360 << " case MCD::OPC_Fail: {\n"
2361 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
2362 << " return MCDisassembler::Fail;\n"
2366 << " llvm_unreachable(\"bogosity detected in disassembler state "
2371 // Emits disassembler code for instruction decoding.
2372 void FixedLenDecoderEmitter::run(raw_ostream
&o
) {
2373 formatted_raw_ostream
OS(o
);
2374 OS
<< "#include \"llvm/MC/MCInst.h\"\n";
2375 OS
<< "#include \"llvm/Support/Debug.h\"\n";
2376 OS
<< "#include \"llvm/Support/DataTypes.h\"\n";
2377 OS
<< "#include \"llvm/Support/LEB128.h\"\n";
2378 OS
<< "#include \"llvm/Support/raw_ostream.h\"\n";
2379 OS
<< "#include <assert.h>\n";
2381 OS
<< "namespace llvm {\n\n";
2383 emitFieldFromInstruction(OS
);
2385 Target
.reverseBitsForLittleEndianEncoding();
2387 // Parameterize the decoders based on namespace and instruction width.
2388 std::set
<StringRef
> HwModeNames
;
2389 const auto &NumberedInstructions
= Target
.getInstructionsByEnumValue();
2390 NumberedEncodings
.reserve(NumberedInstructions
.size());
2391 DenseMap
<Record
*, unsigned> IndexOfInstruction
;
2392 // First, collect all HwModes referenced by the target.
2393 for (const auto &NumberedInstruction
: NumberedInstructions
) {
2394 IndexOfInstruction
[NumberedInstruction
->TheDef
] = NumberedEncodings
.size();
2396 if (const RecordVal
*RV
=
2397 NumberedInstruction
->TheDef
->getValue("EncodingInfos")) {
2398 if (auto *DI
= dyn_cast_or_null
<DefInit
>(RV
->getValue())) {
2399 const CodeGenHwModes
&HWM
= Target
.getHwModes();
2400 EncodingInfoByHwMode
EBM(DI
->getDef(), HWM
);
2401 for (auto &KV
: EBM
.Map
)
2402 HwModeNames
.insert(HWM
.getMode(KV
.first
).Name
);
2407 // If HwModeNames is empty, add the empty string so we always have one HwMode.
2408 if (HwModeNames
.empty())
2409 HwModeNames
.insert("");
2411 for (const auto &NumberedInstruction
: NumberedInstructions
) {
2412 IndexOfInstruction
[NumberedInstruction
->TheDef
] = NumberedEncodings
.size();
2414 if (const RecordVal
*RV
=
2415 NumberedInstruction
->TheDef
->getValue("EncodingInfos")) {
2416 if (DefInit
*DI
= dyn_cast_or_null
<DefInit
>(RV
->getValue())) {
2417 const CodeGenHwModes
&HWM
= Target
.getHwModes();
2418 EncodingInfoByHwMode
EBM(DI
->getDef(), HWM
);
2419 for (auto &KV
: EBM
.Map
) {
2420 NumberedEncodings
.emplace_back(KV
.second
, NumberedInstruction
,
2421 HWM
.getMode(KV
.first
).Name
);
2422 HwModeNames
.insert(HWM
.getMode(KV
.first
).Name
);
2427 // This instruction is encoded the same on all HwModes. Emit it for all
2429 for (StringRef HwModeName
: HwModeNames
)
2430 NumberedEncodings
.emplace_back(NumberedInstruction
->TheDef
,
2431 NumberedInstruction
, HwModeName
);
2433 for (const auto &NumberedAlias
: RK
.getAllDerivedDefinitions("AdditionalEncoding"))
2434 NumberedEncodings
.emplace_back(
2436 &Target
.getInstruction(NumberedAlias
->getValueAsDef("AliasOf")));
2438 std::map
<std::pair
<std::string
, unsigned>, std::vector
<EncodingIDAndOpcode
>>
2440 std::map
<unsigned, std::vector
<OperandInfo
>> Operands
;
2442 for (unsigned i
= 0; i
< NumberedEncodings
.size(); ++i
) {
2443 const Record
*EncodingDef
= NumberedEncodings
[i
].EncodingDef
;
2444 const CodeGenInstruction
*Inst
= NumberedEncodings
[i
].Inst
;
2445 const Record
*Def
= Inst
->TheDef
;
2446 unsigned Size
= EncodingDef
->getValueAsInt("Size");
2447 if (Def
->getValueAsString("Namespace") == "TargetOpcode" ||
2448 Def
->getValueAsBit("isPseudo") ||
2449 Def
->getValueAsBit("isAsmParserOnly") ||
2450 Def
->getValueAsBit("isCodeGenOnly")) {
2451 NumEncodingsLackingDisasm
++;
2455 if (i
< NumberedInstructions
.size())
2462 if (populateInstruction(Target
, *EncodingDef
, *Inst
, i
, Operands
)) {
2463 std::string DecoderNamespace
=
2464 EncodingDef
->getValueAsString("DecoderNamespace");
2465 if (!NumberedEncodings
[i
].HwModeName
.empty())
2467 std::string("_") + NumberedEncodings
[i
].HwModeName
.str();
2468 OpcMap
[std::make_pair(DecoderNamespace
, Size
)].emplace_back(
2469 i
, IndexOfInstruction
.find(Def
)->second
);
2471 NumEncodingsOmitted
++;
2475 DecoderTableInfo TableInfo
;
2476 for (const auto &Opc
: OpcMap
) {
2477 // Emit the decoder for this namespace+width combination.
2478 ArrayRef
<EncodingAndInst
> NumberedEncodingsRef(
2479 NumberedEncodings
.data(), NumberedEncodings
.size());
2480 FilterChooser
FC(NumberedEncodingsRef
, Opc
.second
, Operands
,
2481 8 * Opc
.first
.second
, this);
2483 // The decode table is cleared for each top level decoder function. The
2484 // predicates and decoders themselves, however, are shared across all
2485 // decoders to give more opportunities for uniqueing.
2486 TableInfo
.Table
.clear();
2487 TableInfo
.FixupStack
.clear();
2488 TableInfo
.Table
.reserve(16384);
2489 TableInfo
.FixupStack
.emplace_back();
2490 FC
.emitTableEntries(TableInfo
);
2491 // Any NumToSkip fixups in the top level scope can resolve to the
2492 // OPC_Fail at the end of the table.
2493 assert(TableInfo
.FixupStack
.size() == 1 && "fixup stack phasing error!");
2494 // Resolve any NumToSkip fixups in the current scope.
2495 resolveTableFixups(TableInfo
.Table
, TableInfo
.FixupStack
.back(),
2496 TableInfo
.Table
.size());
2497 TableInfo
.FixupStack
.clear();
2499 TableInfo
.Table
.push_back(MCD::OPC_Fail
);
2501 // Print the table to the output stream.
2502 emitTable(OS
, TableInfo
.Table
, 0, FC
.getBitWidth(), Opc
.first
.first
);
2506 // Emit the predicate function.
2507 emitPredicateFunction(OS
, TableInfo
.Predicates
, 0);
2509 // Emit the decoder function.
2510 emitDecoderFunction(OS
, TableInfo
.Decoders
, 0);
2512 // Emit the main entry point for the decoder, decodeInstruction().
2513 emitDecodeInstruction(OS
);
2515 OS
<< "\n} // end namespace llvm\n";
2520 void EmitFixedLenDecoder(RecordKeeper
&RK
, raw_ostream
&OS
,
2521 const std::string
&PredicateNamespace
,
2522 const std::string
&GPrefix
,
2523 const std::string
&GPostfix
, const std::string
&ROK
,
2524 const std::string
&RFail
, const std::string
&L
) {
2525 FixedLenDecoderEmitter(RK
, PredicateNamespace
, GPrefix
, GPostfix
,
2526 ROK
, RFail
, L
).run(OS
);
2529 } // end namespace llvm