1 //===---------------- DecoderEmitter.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/variable length instruction set.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenHwModes.h"
15 #include "CodeGenInstruction.h"
16 #include "CodeGenTarget.h"
17 #include "InfoByHwMode.h"
18 #include "TableGenBackends.h"
19 #include "VarLenCodeEmitterGen.h"
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/CachedHashString.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/MC/MCDecoderOps.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/FormattedStream.h"
34 #include "llvm/Support/LEB128.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/TableGen/Error.h"
37 #include "llvm/TableGen/Record.h"
51 #define DEBUG_TYPE "decoder-emitter"
55 STATISTIC(NumEncodings
, "Number of encodings considered");
56 STATISTIC(NumEncodingsLackingDisasm
, "Number of encodings without disassembler info");
57 STATISTIC(NumInstructions
, "Number of instructions considered");
58 STATISTIC(NumEncodingsSupported
, "Number of encodings supported");
59 STATISTIC(NumEncodingsOmitted
, "Number of encodings omitted");
61 struct EncodingField
{
62 unsigned Base
, Width
, Offset
;
63 EncodingField(unsigned B
, unsigned W
, unsigned O
)
64 : Base(B
), Width(W
), Offset(O
) { }
68 std::vector
<EncodingField
> Fields
;
70 bool HasCompleteDecoder
;
73 OperandInfo(std::string D
, bool HCD
)
74 : Decoder(std::move(D
)), HasCompleteDecoder(HCD
), InitValue(0) {}
76 void addField(unsigned Base
, unsigned Width
, unsigned Offset
) {
77 Fields
.push_back(EncodingField(Base
, Width
, Offset
));
80 unsigned numFields() const { return Fields
.size(); }
82 typedef std::vector
<EncodingField
>::const_iterator const_iterator
;
84 const_iterator
begin() const { return Fields
.begin(); }
85 const_iterator
end() const { return Fields
.end(); }
88 typedef std::vector
<uint8_t> DecoderTable
;
89 typedef uint32_t DecoderFixup
;
90 typedef std::vector
<DecoderFixup
> FixupList
;
91 typedef std::vector
<FixupList
> FixupScopeList
;
92 typedef SmallSetVector
<CachedHashString
, 16> PredicateSet
;
93 typedef SmallSetVector
<CachedHashString
, 16> DecoderSet
;
94 struct DecoderTableInfo
{
96 FixupScopeList FixupStack
;
97 PredicateSet Predicates
;
101 struct EncodingAndInst
{
102 const Record
*EncodingDef
;
103 const CodeGenInstruction
*Inst
;
104 StringRef HwModeName
;
106 EncodingAndInst(const Record
*EncodingDef
, const CodeGenInstruction
*Inst
,
107 StringRef HwModeName
= "")
108 : EncodingDef(EncodingDef
), Inst(Inst
), HwModeName(HwModeName
) {}
111 struct EncodingIDAndOpcode
{
115 EncodingIDAndOpcode() : EncodingID(0), Opcode(0) {}
116 EncodingIDAndOpcode(unsigned EncodingID
, unsigned Opcode
)
117 : EncodingID(EncodingID
), Opcode(Opcode
) {}
120 raw_ostream
&operator<<(raw_ostream
&OS
, const EncodingAndInst
&Value
) {
121 if (Value
.EncodingDef
!= Value
.Inst
->TheDef
)
122 OS
<< Value
.EncodingDef
->getName() << ":";
123 OS
<< Value
.Inst
->TheDef
->getName();
127 class DecoderEmitter
{
129 std::vector
<EncodingAndInst
> NumberedEncodings
;
132 DecoderEmitter(RecordKeeper
&R
, std::string PredicateNamespace
)
133 : RK(R
), Target(R
), PredicateNamespace(std::move(PredicateNamespace
)) {}
135 // Emit the decoder state machine table.
136 void emitTable(formatted_raw_ostream
&o
, DecoderTable
&Table
,
137 unsigned Indentation
, unsigned BitWidth
,
138 StringRef Namespace
) const;
139 void emitInstrLenTable(formatted_raw_ostream
&OS
,
140 std::vector
<unsigned> &InstrLen
) const;
141 void emitPredicateFunction(formatted_raw_ostream
&OS
,
142 PredicateSet
&Predicates
,
143 unsigned Indentation
) const;
144 void emitDecoderFunction(formatted_raw_ostream
&OS
,
145 DecoderSet
&Decoders
,
146 unsigned Indentation
) const;
148 // run - Output the code emitter
149 void run(raw_ostream
&o
);
152 CodeGenTarget Target
;
155 std::string PredicateNamespace
;
158 } // end anonymous namespace
160 // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
163 // BIT_UNFILTERED is used as the init value for a filter position. It is used
164 // only for filter processings.
169 BIT_UNFILTERED
// unfiltered
172 static bool ValueSet(bit_value_t V
) {
173 return (V
== BIT_TRUE
|| V
== BIT_FALSE
);
176 static bool ValueNotSet(bit_value_t V
) {
177 return (V
== BIT_UNSET
);
180 static int Value(bit_value_t V
) {
181 return ValueNotSet(V
) ? -1 : (V
== BIT_FALSE
? 0 : 1);
184 static bit_value_t
bitFromBits(const BitsInit
&bits
, unsigned index
) {
185 if (BitInit
*bit
= dyn_cast
<BitInit
>(bits
.getBit(index
)))
186 return bit
->getValue() ? BIT_TRUE
: BIT_FALSE
;
188 // The bit is uninitialized.
192 // Prints the bit value for each position.
193 static void dumpBits(raw_ostream
&o
, const BitsInit
&bits
) {
194 for (unsigned index
= bits
.getNumBits(); index
> 0; --index
) {
195 switch (bitFromBits(bits
, index
- 1)) {
206 llvm_unreachable("unexpected return value from bitFromBits");
211 static BitsInit
&getBitsField(const Record
&def
, StringRef str
) {
212 const RecordVal
*RV
= def
.getValue(str
);
213 if (BitsInit
*Bits
= dyn_cast
<BitsInit
>(RV
->getValue()))
216 // variable length instruction
217 VarLenInst VLI
= VarLenInst(cast
<DagInit
>(RV
->getValue()), RV
);
218 SmallVector
<Init
*, 16> Bits
;
220 for (auto &SI
: VLI
) {
221 if (const BitsInit
*BI
= dyn_cast
<BitsInit
>(SI
.Value
)) {
222 for (unsigned Idx
= 0U; Idx
< BI
->getNumBits(); ++Idx
) {
223 Bits
.push_back(BI
->getBit(Idx
));
225 } else if (const BitInit
*BI
= dyn_cast
<BitInit
>(SI
.Value
)) {
226 Bits
.push_back(const_cast<BitInit
*>(BI
));
228 for (unsigned Idx
= 0U; Idx
< SI
.BitWidth
; ++Idx
)
229 Bits
.push_back(UnsetInit::get(def
.getRecords()));
233 return *BitsInit::get(def
.getRecords(), Bits
);
236 // Representation of the instruction to work on.
237 typedef std::vector
<bit_value_t
> insn_t
;
241 static const uint64_t NO_FIXED_SEGMENTS_SENTINEL
= -1ULL;
245 /// Filter - Filter works with FilterChooser to produce the decoding tree for
248 /// It is useful to think of a Filter as governing the switch stmts of the
249 /// decoding tree in a certain level. Each case stmt delegates to an inferior
250 /// FilterChooser to decide what further decoding logic to employ, or in another
251 /// words, what other remaining bits to look at. The FilterChooser eventually
252 /// chooses a best Filter to do its job.
254 /// This recursive scheme ends when the number of Opcodes assigned to the
255 /// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
256 /// the Filter/FilterChooser combo does not know how to distinguish among the
257 /// Opcodes assigned.
259 /// An example of a conflict is
262 /// 111101000.00........00010000....
263 /// 111101000.00........0001........
264 /// 1111010...00........0001........
265 /// 1111010...00....................
266 /// 1111010.........................
267 /// 1111............................
268 /// ................................
269 /// VST4q8a 111101000_00________00010000____
270 /// VST4q8b 111101000_00________00010000____
272 /// The Debug output shows the path that the decoding tree follows to reach the
273 /// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
274 /// even registers, while VST4q8b is a vst4 to double-spaced odd registers.
276 /// The encoding info in the .td files does not specify this meta information,
277 /// which could have been used by the decoder to resolve the conflict. The
278 /// decoder could try to decode the even/odd register numbering and assign to
279 /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
280 /// version and return the Opcode since the two have the same Asm format string.
283 const FilterChooser
*Owner
;// points to the FilterChooser who owns this filter
284 unsigned StartBit
; // the starting bit position
285 unsigned NumBits
; // number of bits to filter
286 bool Mixed
; // a mixed region contains both set and unset bits
288 // Map of well-known segment value to the set of uid's with that value.
289 std::map
<uint64_t, std::vector
<EncodingIDAndOpcode
>>
290 FilteredInstructions
;
292 // Set of uid's with non-constant segment values.
293 std::vector
<EncodingIDAndOpcode
> VariableInstructions
;
295 // Map of well-known segment value to its delegate.
296 std::map
<uint64_t, std::unique_ptr
<const FilterChooser
>> FilterChooserMap
;
298 // Number of instructions which fall under FilteredInstructions category.
299 unsigned NumFiltered
;
301 // Keeps track of the last opcode in the filtered bucket.
302 EncodingIDAndOpcode LastOpcFiltered
;
306 Filter(FilterChooser
&owner
, unsigned startBit
, unsigned numBits
, bool mixed
);
310 unsigned getNumFiltered() const { return NumFiltered
; }
312 EncodingIDAndOpcode
getSingletonOpc() const {
313 assert(NumFiltered
== 1);
314 return LastOpcFiltered
;
317 // Return the filter chooser for the group of instructions without constant
319 const FilterChooser
&getVariableFC() const {
320 assert(NumFiltered
== 1);
321 assert(FilterChooserMap
.size() == 1);
322 return *(FilterChooserMap
.find(NO_FIXED_SEGMENTS_SENTINEL
)->second
);
325 // Divides the decoding task into sub tasks and delegates them to the
326 // inferior FilterChooser's.
328 // A special case arises when there's only one entry in the filtered
329 // instructions. In order to unambiguously decode the singleton, we need to
330 // match the remaining undecoded encoding bits against the singleton.
333 // Emit table entries to decode instructions given a segment or segments of
335 void emitTableEntry(DecoderTableInfo
&TableInfo
) const;
337 // Returns the number of fanout produced by the filter. More fanout implies
338 // the filter distinguishes more categories of instructions.
339 unsigned usefulness() const;
340 }; // end class Filter
342 } // end anonymous namespace
344 // These are states of our finite state machines used in FilterChooser's
345 // filterProcessor() which produces the filter candidates to use.
354 /// FilterChooser - FilterChooser chooses the best filter among a set of Filters
355 /// in order to perform the decoding of instructions at the current level.
357 /// Decoding proceeds from the top down. Based on the well-known encoding bits
358 /// of instructions available, FilterChooser builds up the possible Filters that
359 /// can further the task of decoding by distinguishing among the remaining
360 /// candidate instructions.
362 /// Once a filter has been chosen, it is called upon to divide the decoding task
363 /// into sub-tasks and delegates them to its inferior FilterChoosers for further
366 /// It is useful to think of a Filter as governing the switch stmts of the
367 /// decoding tree. And each case is delegated to an inferior FilterChooser to
368 /// decide what further remaining bits to look at.
371 class FilterChooser
{
375 // Vector of codegen instructions to choose our filter.
376 ArrayRef
<EncodingAndInst
> AllInstructions
;
378 // Vector of uid's for this filter chooser to work on.
379 // The first member of the pair is the opcode id being decoded, the second is
380 // the opcode id that should be emitted.
381 const std::vector
<EncodingIDAndOpcode
> &Opcodes
;
383 // Lookup table for the operand decoding of instructions.
384 const std::map
<unsigned, std::vector
<OperandInfo
>> &Operands
;
386 // Vector of candidate filters.
387 std::vector
<Filter
> Filters
;
389 // Array of bit values passed down from our parent.
390 // Set to all BIT_UNFILTERED's for Parent == NULL.
391 std::vector
<bit_value_t
> FilterBitValues
;
393 // Links to the FilterChooser above us in the decoding tree.
394 const FilterChooser
*Parent
;
396 // Index of the best filter from Filters.
399 // Width of instructions
403 const DecoderEmitter
*Emitter
;
406 FilterChooser(ArrayRef
<EncodingAndInst
> Insts
,
407 const std::vector
<EncodingIDAndOpcode
> &IDs
,
408 const std::map
<unsigned, std::vector
<OperandInfo
>> &Ops
,
409 unsigned BW
, const DecoderEmitter
*E
)
410 : AllInstructions(Insts
), Opcodes(IDs
), Operands(Ops
),
411 FilterBitValues(BW
, BIT_UNFILTERED
), Parent(nullptr), BestIndex(-1),
412 BitWidth(BW
), Emitter(E
) {
416 FilterChooser(ArrayRef
<EncodingAndInst
> Insts
,
417 const std::vector
<EncodingIDAndOpcode
> &IDs
,
418 const std::map
<unsigned, std::vector
<OperandInfo
>> &Ops
,
419 const std::vector
<bit_value_t
> &ParentFilterBitValues
,
420 const FilterChooser
&parent
)
421 : AllInstructions(Insts
), Opcodes(IDs
), Operands(Ops
),
422 FilterBitValues(ParentFilterBitValues
), Parent(&parent
), BestIndex(-1),
423 BitWidth(parent
.BitWidth
), Emitter(parent
.Emitter
) {
427 FilterChooser(const FilterChooser
&) = delete;
428 void operator=(const FilterChooser
&) = delete;
430 unsigned getBitWidth() const { return BitWidth
; }
433 // Populates the insn given the uid.
434 void insnWithID(insn_t
&Insn
, unsigned Opcode
) const {
435 BitsInit
&Bits
= getBitsField(*AllInstructions
[Opcode
].EncodingDef
, "Inst");
436 Insn
.resize(BitWidth
> Bits
.getNumBits() ? BitWidth
: Bits
.getNumBits(),
438 // We may have a SoftFail bitmask, which specifies a mask where an encoding
439 // may differ from the value in "Inst" and yet still be valid, but the
440 // disassembler should return SoftFail instead of Success.
442 // This is used for marking UNPREDICTABLE instructions in the ARM world.
443 const RecordVal
*RV
=
444 AllInstructions
[Opcode
].EncodingDef
->getValue("SoftFail");
445 const BitsInit
*SFBits
= RV
? dyn_cast
<BitsInit
>(RV
->getValue()) : nullptr;
446 for (unsigned i
= 0; i
< Bits
.getNumBits(); ++i
) {
447 if (SFBits
&& bitFromBits(*SFBits
, i
) == BIT_TRUE
)
450 Insn
[i
] = bitFromBits(Bits
, i
);
454 // Emit the name of the encoding/instruction pair.
455 void emitNameWithID(raw_ostream
&OS
, unsigned Opcode
) const {
456 const Record
*EncodingDef
= AllInstructions
[Opcode
].EncodingDef
;
457 const Record
*InstDef
= AllInstructions
[Opcode
].Inst
->TheDef
;
458 if (EncodingDef
!= InstDef
)
459 OS
<< EncodingDef
->getName() << ":";
460 OS
<< InstDef
->getName();
463 // Populates the field of the insn given the start position and the number of
464 // consecutive bits to scan for.
466 // Returns false if there exists any uninitialized bit value in the range.
467 // Returns true, otherwise.
468 bool fieldFromInsn(uint64_t &Field
, insn_t
&Insn
, unsigned StartBit
,
469 unsigned NumBits
) const;
471 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
472 /// filter array as a series of chars.
473 void dumpFilterArray(raw_ostream
&o
,
474 const std::vector
<bit_value_t
> & filter
) const;
476 /// dumpStack - dumpStack traverses the filter chooser chain and calls
477 /// dumpFilterArray on each filter chooser up to the top level one.
478 void dumpStack(raw_ostream
&o
, const char *prefix
) const;
480 Filter
&bestFilter() {
481 assert(BestIndex
!= -1 && "BestIndex not set");
482 return Filters
[BestIndex
];
485 bool PositionFiltered(unsigned i
) const {
486 return ValueSet(FilterBitValues
[i
]);
489 // Calculates the island(s) needed to decode the instruction.
490 // This returns a lit of undecoded bits of an instructions, for example,
491 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
492 // decoded bits in order to verify that the instruction matches the Opcode.
493 unsigned getIslands(std::vector
<unsigned> &StartBits
,
494 std::vector
<unsigned> &EndBits
,
495 std::vector
<uint64_t> &FieldVals
,
496 const insn_t
&Insn
) const;
498 // Emits code to check the Predicates member of an instruction are true.
499 // Returns true if predicate matches were emitted, false otherwise.
500 bool emitPredicateMatch(raw_ostream
&o
, unsigned &Indentation
,
502 bool emitPredicateMatchAux(const Init
&Val
, bool ParenIfBinOp
,
503 raw_ostream
&OS
) const;
505 bool doesOpcodeNeedPredicate(unsigned Opc
) const;
506 unsigned getPredicateIndex(DecoderTableInfo
&TableInfo
, StringRef P
) const;
507 void emitPredicateTableEntry(DecoderTableInfo
&TableInfo
,
510 void emitSoftFailTableEntry(DecoderTableInfo
&TableInfo
,
513 // Emits table entries to decode the singleton.
514 void emitSingletonTableEntry(DecoderTableInfo
&TableInfo
,
515 EncodingIDAndOpcode Opc
) const;
517 // Emits code to decode the singleton, and then to decode the rest.
518 void emitSingletonTableEntry(DecoderTableInfo
&TableInfo
,
519 const Filter
&Best
) const;
521 void emitBinaryParser(raw_ostream
&o
, unsigned &Indentation
,
522 const OperandInfo
&OpInfo
,
523 bool &OpHasCompleteDecoder
) const;
525 void emitDecoder(raw_ostream
&OS
, unsigned Indentation
, unsigned Opc
,
526 bool &HasCompleteDecoder
) const;
527 unsigned getDecoderIndex(DecoderSet
&Decoders
, unsigned Opc
,
528 bool &HasCompleteDecoder
) const;
530 // Assign a single filter and run with it.
531 void runSingleFilter(unsigned startBit
, unsigned numBit
, bool mixed
);
533 // reportRegion is a helper function for filterProcessor to mark a region as
534 // eligible for use as a filter region.
535 void reportRegion(bitAttr_t RA
, unsigned StartBit
, unsigned BitIndex
,
538 // FilterProcessor scans the well-known encoding bits of the instructions and
539 // builds up a list of candidate filters. It chooses the best filter and
540 // recursively descends down the decoding tree.
541 bool filterProcessor(bool AllowMixed
, bool Greedy
= true);
543 // Decides on the best configuration of filter(s) to use in order to decode
544 // the instructions. A conflict of instructions may occur, in which case we
545 // dump the conflict set to the standard error.
549 // emitTableEntries - Emit state machine entries to decode our share of
551 void emitTableEntries(DecoderTableInfo
&TableInfo
) const;
554 } // end anonymous namespace
556 ///////////////////////////
558 // Filter Implementation //
560 ///////////////////////////
562 Filter::Filter(Filter
&&f
)
563 : Owner(f
.Owner
), StartBit(f
.StartBit
), NumBits(f
.NumBits
), Mixed(f
.Mixed
),
564 FilteredInstructions(std::move(f
.FilteredInstructions
)),
565 VariableInstructions(std::move(f
.VariableInstructions
)),
566 FilterChooserMap(std::move(f
.FilterChooserMap
)), NumFiltered(f
.NumFiltered
),
567 LastOpcFiltered(f
.LastOpcFiltered
) {
570 Filter::Filter(FilterChooser
&owner
, unsigned startBit
, unsigned numBits
,
572 : Owner(&owner
), StartBit(startBit
), NumBits(numBits
), Mixed(mixed
) {
573 assert(StartBit
+ NumBits
- 1 < Owner
->BitWidth
);
576 LastOpcFiltered
= {0, 0};
578 for (unsigned i
= 0, e
= Owner
->Opcodes
.size(); i
!= e
; ++i
) {
581 // Populates the insn given the uid.
582 Owner
->insnWithID(Insn
, Owner
->Opcodes
[i
].EncodingID
);
585 // Scans the segment for possibly well-specified encoding bits.
586 bool ok
= Owner
->fieldFromInsn(Field
, Insn
, StartBit
, NumBits
);
589 // The encoding bits are well-known. Lets add the uid of the
590 // instruction into the bucket keyed off the constant field value.
591 LastOpcFiltered
= Owner
->Opcodes
[i
];
592 FilteredInstructions
[Field
].push_back(LastOpcFiltered
);
595 // Some of the encoding bit(s) are unspecified. This contributes to
596 // one additional member of "Variable" instructions.
597 VariableInstructions
.push_back(Owner
->Opcodes
[i
]);
601 assert((FilteredInstructions
.size() + VariableInstructions
.size() > 0)
602 && "Filter returns no instruction categories");
605 // Divides the decoding task into sub tasks and delegates them to the
606 // inferior FilterChooser's.
608 // A special case arises when there's only one entry in the filtered
609 // instructions. In order to unambiguously decode the singleton, we need to
610 // match the remaining undecoded encoding bits against the singleton.
611 void Filter::recurse() {
612 // Starts by inheriting our parent filter chooser's filter bit values.
613 std::vector
<bit_value_t
> BitValueArray(Owner
->FilterBitValues
);
615 if (!VariableInstructions
.empty()) {
616 // Conservatively marks each segment position as BIT_UNSET.
617 for (unsigned bitIndex
= 0; bitIndex
< NumBits
; ++bitIndex
)
618 BitValueArray
[StartBit
+ bitIndex
] = BIT_UNSET
;
620 // Delegates to an inferior filter chooser for further processing on this
621 // group of instructions whose segment values are variable.
622 FilterChooserMap
.insert(std::make_pair(NO_FIXED_SEGMENTS_SENTINEL
,
623 std::make_unique
<FilterChooser
>(Owner
->AllInstructions
,
624 VariableInstructions
, Owner
->Operands
, BitValueArray
, *Owner
)));
627 // No need to recurse for a singleton filtered instruction.
628 // See also Filter::emit*().
629 if (getNumFiltered() == 1) {
630 assert(FilterChooserMap
.size() == 1);
634 // Otherwise, create sub choosers.
635 for (const auto &Inst
: FilteredInstructions
) {
637 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
638 for (unsigned bitIndex
= 0; bitIndex
< NumBits
; ++bitIndex
) {
639 if (Inst
.first
& (1ULL << bitIndex
))
640 BitValueArray
[StartBit
+ bitIndex
] = BIT_TRUE
;
642 BitValueArray
[StartBit
+ bitIndex
] = BIT_FALSE
;
645 // Delegates to an inferior filter chooser for further processing on this
646 // category of instructions.
647 FilterChooserMap
.insert(std::make_pair(
648 Inst
.first
, std::make_unique
<FilterChooser
>(
649 Owner
->AllInstructions
, Inst
.second
,
650 Owner
->Operands
, BitValueArray
, *Owner
)));
654 static void resolveTableFixups(DecoderTable
&Table
, const FixupList
&Fixups
,
656 // Any NumToSkip fixups in the current scope can resolve to the
658 for (FixupList::const_reverse_iterator I
= Fixups
.rbegin(),
661 // Calculate the distance from the byte following the fixup entry byte
662 // to the destination. The Target is calculated from after the 16-bit
663 // NumToSkip entry itself, so subtract two from the displacement here
664 // to account for that.
665 uint32_t FixupIdx
= *I
;
666 uint32_t Delta
= DestIdx
- FixupIdx
- 3;
667 // Our NumToSkip entries are 24-bits. Make sure our table isn't too
669 assert(Delta
< (1u << 24));
670 Table
[FixupIdx
] = (uint8_t)Delta
;
671 Table
[FixupIdx
+ 1] = (uint8_t)(Delta
>> 8);
672 Table
[FixupIdx
+ 2] = (uint8_t)(Delta
>> 16);
676 // Emit table entries to decode instructions given a segment or segments
678 void Filter::emitTableEntry(DecoderTableInfo
&TableInfo
) const {
679 TableInfo
.Table
.push_back(MCD::OPC_ExtractField
);
680 TableInfo
.Table
.push_back(StartBit
);
681 TableInfo
.Table
.push_back(NumBits
);
683 // A new filter entry begins a new scope for fixup resolution.
684 TableInfo
.FixupStack
.emplace_back();
686 DecoderTable
&Table
= TableInfo
.Table
;
688 size_t PrevFilter
= 0;
689 bool HasFallthrough
= false;
690 for (auto &Filter
: FilterChooserMap
) {
691 // Field value -1 implies a non-empty set of variable instructions.
692 // See also recurse().
693 if (Filter
.first
== NO_FIXED_SEGMENTS_SENTINEL
) {
694 HasFallthrough
= true;
696 // Each scope should always have at least one filter value to check
698 assert(PrevFilter
!= 0 && "empty filter set!");
699 FixupList
&CurScope
= TableInfo
.FixupStack
.back();
700 // Resolve any NumToSkip fixups in the current scope.
701 resolveTableFixups(Table
, CurScope
, Table
.size());
703 PrevFilter
= 0; // Don't re-process the filter's fallthrough.
705 Table
.push_back(MCD::OPC_FilterValue
);
706 // Encode and emit the value to filter against.
708 unsigned Len
= encodeULEB128(Filter
.first
, Buffer
);
709 Table
.insert(Table
.end(), Buffer
, Buffer
+ Len
);
710 // Reserve space for the NumToSkip entry. We'll backpatch the value
712 PrevFilter
= Table
.size();
718 // We arrive at a category of instructions with the same segment value.
719 // Now delegate to the sub filter chooser for further decodings.
720 // The case may fallthrough, which happens if the remaining well-known
721 // encoding bits do not match exactly.
722 Filter
.second
->emitTableEntries(TableInfo
);
724 // Now that we've emitted the body of the handler, update the NumToSkip
725 // of the filter itself to be able to skip forward when false. Subtract
726 // two as to account for the width of the NumToSkip field itself.
728 uint32_t NumToSkip
= Table
.size() - PrevFilter
- 3;
729 assert(NumToSkip
< (1u << 24) && "disassembler decoding table too large!");
730 Table
[PrevFilter
] = (uint8_t)NumToSkip
;
731 Table
[PrevFilter
+ 1] = (uint8_t)(NumToSkip
>> 8);
732 Table
[PrevFilter
+ 2] = (uint8_t)(NumToSkip
>> 16);
736 // Any remaining unresolved fixups bubble up to the parent fixup scope.
737 assert(TableInfo
.FixupStack
.size() > 1 && "fixup stack underflow!");
738 FixupScopeList::iterator Source
= TableInfo
.FixupStack
.end() - 1;
739 FixupScopeList::iterator Dest
= Source
- 1;
740 llvm::append_range(*Dest
, *Source
);
741 TableInfo
.FixupStack
.pop_back();
743 // If there is no fallthrough, then the final filter should get fixed
744 // up according to the enclosing scope rather than the current position.
746 TableInfo
.FixupStack
.back().push_back(PrevFilter
);
749 // Returns the number of fanout produced by the filter. More fanout implies
750 // the filter distinguishes more categories of instructions.
751 unsigned Filter::usefulness() const {
752 if (!VariableInstructions
.empty())
753 return FilteredInstructions
.size();
755 return FilteredInstructions
.size() + 1;
758 //////////////////////////////////
760 // Filterchooser Implementation //
762 //////////////////////////////////
764 // Emit the decoder state machine table.
765 void DecoderEmitter::emitTable(formatted_raw_ostream
&OS
, DecoderTable
&Table
,
766 unsigned Indentation
, unsigned BitWidth
,
767 StringRef Namespace
) const {
768 OS
.indent(Indentation
) << "static const uint8_t DecoderTable" << Namespace
769 << BitWidth
<< "[] = {\n";
773 // FIXME: We may be able to use the NumToSkip values to recover
774 // appropriate indentation levels.
775 DecoderTable::const_iterator I
= Table
.begin();
776 DecoderTable::const_iterator E
= Table
.end();
778 assert (I
< E
&& "incomplete decode table entry!");
780 uint64_t Pos
= I
- Table
.begin();
781 OS
<< "/* " << Pos
<< " */";
786 PrintFatalError("invalid decode table opcode");
787 case MCD::OPC_ExtractField
: {
789 unsigned Start
= *I
++;
791 OS
.indent(Indentation
) << "MCD::OPC_ExtractField, " << Start
<< ", "
792 << Len
<< ", // Inst{";
794 OS
<< (Start
+ Len
- 1) << "-";
795 OS
<< Start
<< "} ...\n";
798 case MCD::OPC_FilterValue
: {
800 OS
.indent(Indentation
) << "MCD::OPC_FilterValue, ";
801 // The filter value is ULEB128 encoded.
803 OS
<< (unsigned)*I
++ << ", ";
804 OS
<< (unsigned)*I
++ << ", ";
806 // 24-bit numtoskip value.
808 uint32_t NumToSkip
= Byte
;
809 OS
<< (unsigned)Byte
<< ", ";
811 OS
<< (unsigned)Byte
<< ", ";
812 NumToSkip
|= Byte
<< 8;
814 OS
<< utostr(Byte
) << ", ";
815 NumToSkip
|= Byte
<< 16;
816 OS
<< "// Skip to: " << ((I
- Table
.begin()) + NumToSkip
) << "\n";
819 case MCD::OPC_CheckField
: {
821 unsigned Start
= *I
++;
823 OS
.indent(Indentation
) << "MCD::OPC_CheckField, " << Start
<< ", "
824 << Len
<< ", ";// << Val << ", " << NumToSkip << ",\n";
825 // ULEB128 encoded field value.
826 for (; *I
>= 128; ++I
)
827 OS
<< (unsigned)*I
<< ", ";
828 OS
<< (unsigned)*I
++ << ", ";
829 // 24-bit numtoskip value.
831 uint32_t NumToSkip
= Byte
;
832 OS
<< (unsigned)Byte
<< ", ";
834 OS
<< (unsigned)Byte
<< ", ";
835 NumToSkip
|= Byte
<< 8;
837 OS
<< utostr(Byte
) << ", ";
838 NumToSkip
|= Byte
<< 16;
839 OS
<< "// Skip to: " << ((I
- Table
.begin()) + NumToSkip
) << "\n";
842 case MCD::OPC_CheckPredicate
: {
844 OS
.indent(Indentation
) << "MCD::OPC_CheckPredicate, ";
845 for (; *I
>= 128; ++I
)
846 OS
<< (unsigned)*I
<< ", ";
847 OS
<< (unsigned)*I
++ << ", ";
849 // 24-bit numtoskip value.
851 uint32_t NumToSkip
= Byte
;
852 OS
<< (unsigned)Byte
<< ", ";
854 OS
<< (unsigned)Byte
<< ", ";
855 NumToSkip
|= Byte
<< 8;
857 OS
<< utostr(Byte
) << ", ";
858 NumToSkip
|= Byte
<< 16;
859 OS
<< "// Skip to: " << ((I
- Table
.begin()) + NumToSkip
) << "\n";
862 case MCD::OPC_Decode
:
863 case MCD::OPC_TryDecode
: {
864 bool IsTry
= *I
== MCD::OPC_TryDecode
;
866 // Extract the ULEB128 encoded Opcode to a buffer.
867 uint8_t Buffer
[16], *p
= Buffer
;
868 while ((*p
++ = *I
++) >= 128)
869 assert((p
- Buffer
) <= (ptrdiff_t)sizeof(Buffer
)
870 && "ULEB128 value too large!");
871 // Decode the Opcode value.
872 unsigned Opc
= decodeULEB128(Buffer
);
873 OS
.indent(Indentation
) << "MCD::OPC_" << (IsTry
? "Try" : "")
875 for (p
= Buffer
; *p
>= 128; ++p
)
876 OS
<< (unsigned)*p
<< ", ";
877 OS
<< (unsigned)*p
<< ", ";
880 for (; *I
>= 128; ++I
)
881 OS
<< (unsigned)*I
<< ", ";
882 OS
<< (unsigned)*I
++ << ", ";
885 OS
<< "// Opcode: " << NumberedEncodings
[Opc
] << "\n";
889 // Fallthrough for OPC_TryDecode.
891 // 24-bit numtoskip value.
893 uint32_t NumToSkip
= Byte
;
894 OS
<< (unsigned)Byte
<< ", ";
896 OS
<< (unsigned)Byte
<< ", ";
897 NumToSkip
|= Byte
<< 8;
899 OS
<< utostr(Byte
) << ", ";
900 NumToSkip
|= Byte
<< 16;
902 OS
<< "// Opcode: " << NumberedEncodings
[Opc
]
903 << ", skip to: " << ((I
- Table
.begin()) + NumToSkip
) << "\n";
906 case MCD::OPC_SoftFail
: {
908 OS
.indent(Indentation
) << "MCD::OPC_SoftFail";
913 OS
<< ", " << (unsigned)*I
;
914 Value
+= (*I
& 0x7f) << Shift
;
916 } while (*I
++ >= 128);
926 OS
<< ", " << (unsigned)*I
;
927 Value
+= (*I
& 0x7f) << Shift
;
929 } while (*I
++ >= 128);
938 case MCD::OPC_Fail
: {
940 OS
.indent(Indentation
) << "MCD::OPC_Fail,\n";
945 OS
.indent(Indentation
) << "0\n";
949 OS
.indent(Indentation
) << "};\n\n";
952 void DecoderEmitter::emitInstrLenTable(formatted_raw_ostream
&OS
,
953 std::vector
<unsigned> &InstrLen
) const {
954 OS
<< "static const uint8_t InstrLenTable[] = {\n";
955 for (unsigned &Len
: InstrLen
) {
961 void DecoderEmitter::emitPredicateFunction(formatted_raw_ostream
&OS
,
962 PredicateSet
&Predicates
,
963 unsigned Indentation
) const {
964 // The predicate function is just a big switch statement based on the
965 // input predicate index.
966 OS
.indent(Indentation
) << "static bool checkDecoderPredicate(unsigned Idx, "
967 << "const FeatureBitset &Bits) {\n";
969 if (!Predicates
.empty()) {
970 OS
.indent(Indentation
) << "switch (Idx) {\n";
971 OS
.indent(Indentation
) << "default: llvm_unreachable(\"Invalid index!\");\n";
973 for (const auto &Predicate
: Predicates
) {
974 OS
.indent(Indentation
) << "case " << Index
++ << ":\n";
975 OS
.indent(Indentation
+2) << "return (" << Predicate
<< ");\n";
977 OS
.indent(Indentation
) << "}\n";
979 // No case statement to emit
980 OS
.indent(Indentation
) << "llvm_unreachable(\"Invalid index!\");\n";
983 OS
.indent(Indentation
) << "}\n\n";
986 void DecoderEmitter::emitDecoderFunction(formatted_raw_ostream
&OS
,
987 DecoderSet
&Decoders
,
988 unsigned Indentation
) const {
989 // The decoder function is just a big switch statement based on the
990 // input decoder index.
991 OS
.indent(Indentation
) << "template <typename InsnType>\n";
992 OS
.indent(Indentation
) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
993 << " unsigned Idx, InsnType insn, MCInst &MI,\n";
994 OS
.indent(Indentation
)
996 << "Address, const MCDisassembler *Decoder, bool &DecodeComplete) {\n";
998 OS
.indent(Indentation
) << "DecodeComplete = true;\n";
999 // TODO: When InsnType is large, using uint64_t limits all fields to 64 bits
1000 // It would be better for emitBinaryParser to use a 64-bit tmp whenever
1001 // possible but fall back to an InsnType-sized tmp for truly large fields.
1002 OS
.indent(Indentation
) << "using TmpType = "
1003 "std::conditional_t<std::is_integral<InsnType>::"
1004 "value, InsnType, uint64_t>;\n";
1005 OS
.indent(Indentation
) << "TmpType tmp;\n";
1006 OS
.indent(Indentation
) << "switch (Idx) {\n";
1007 OS
.indent(Indentation
) << "default: llvm_unreachable(\"Invalid index!\");\n";
1009 for (const auto &Decoder
: Decoders
) {
1010 OS
.indent(Indentation
) << "case " << Index
++ << ":\n";
1012 OS
.indent(Indentation
+2) << "return S;\n";
1014 OS
.indent(Indentation
) << "}\n";
1016 OS
.indent(Indentation
) << "}\n\n";
1019 // Populates the field of the insn given the start position and the number of
1020 // consecutive bits to scan for.
1022 // Returns false if and on the first uninitialized bit value encountered.
1023 // Returns true, otherwise.
1024 bool FilterChooser::fieldFromInsn(uint64_t &Field
, insn_t
&Insn
,
1025 unsigned StartBit
, unsigned NumBits
) const {
1028 for (unsigned i
= 0; i
< NumBits
; ++i
) {
1029 if (Insn
[StartBit
+ i
] == BIT_UNSET
)
1032 if (Insn
[StartBit
+ i
] == BIT_TRUE
)
1033 Field
= Field
| (1ULL << i
);
1039 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
1040 /// filter array as a series of chars.
1041 void FilterChooser::dumpFilterArray(raw_ostream
&o
,
1042 const std::vector
<bit_value_t
> &filter
) const {
1043 for (unsigned bitIndex
= BitWidth
; bitIndex
> 0; bitIndex
--) {
1044 switch (filter
[bitIndex
- 1]) {
1045 case BIT_UNFILTERED
:
1061 /// dumpStack - dumpStack traverses the filter chooser chain and calls
1062 /// dumpFilterArray on each filter chooser up to the top level one.
1063 void FilterChooser::dumpStack(raw_ostream
&o
, const char *prefix
) const {
1064 const FilterChooser
*current
= this;
1068 dumpFilterArray(o
, current
->FilterBitValues
);
1070 current
= current
->Parent
;
1074 // Calculates the island(s) needed to decode the instruction.
1075 // This returns a list of undecoded bits of an instructions, for example,
1076 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
1077 // decoded bits in order to verify that the instruction matches the Opcode.
1078 unsigned FilterChooser::getIslands(std::vector
<unsigned> &StartBits
,
1079 std::vector
<unsigned> &EndBits
,
1080 std::vector
<uint64_t> &FieldVals
,
1081 const insn_t
&Insn
) const {
1082 unsigned Num
, BitNo
;
1085 uint64_t FieldVal
= 0;
1088 // 1: Water (the bit value does not affect decoding)
1089 // 2: Island (well-known bit value needed for decoding)
1092 for (unsigned i
= 0; i
< BitWidth
; ++i
) {
1093 int64_t Val
= Value(Insn
[i
]);
1094 bool Filtered
= PositionFiltered(i
);
1096 default: llvm_unreachable("Unreachable code!");
1099 if (Filtered
|| Val
== -1)
1100 State
= 1; // Still in Water
1102 State
= 2; // Into the Island
1104 StartBits
.push_back(i
);
1109 if (Filtered
|| Val
== -1) {
1110 State
= 1; // Into the Water
1111 EndBits
.push_back(i
- 1);
1112 FieldVals
.push_back(FieldVal
);
1115 State
= 2; // Still in Island
1117 FieldVal
= FieldVal
| Val
<< BitNo
;
1122 // If we are still in Island after the loop, do some housekeeping.
1124 EndBits
.push_back(BitWidth
- 1);
1125 FieldVals
.push_back(FieldVal
);
1129 assert(StartBits
.size() == Num
&& EndBits
.size() == Num
&&
1130 FieldVals
.size() == Num
);
1134 void FilterChooser::emitBinaryParser(raw_ostream
&o
, unsigned &Indentation
,
1135 const OperandInfo
&OpInfo
,
1136 bool &OpHasCompleteDecoder
) const {
1137 const std::string
&Decoder
= OpInfo
.Decoder
;
1139 bool UseInsertBits
= OpInfo
.numFields() != 1 || OpInfo
.InitValue
!= 0;
1141 if (UseInsertBits
) {
1142 o
.indent(Indentation
) << "tmp = 0x";
1143 o
.write_hex(OpInfo
.InitValue
);
1147 for (const EncodingField
&EF
: OpInfo
) {
1148 o
.indent(Indentation
);
1150 o
<< "insertBits(tmp, ";
1153 o
<< "fieldFromInstruction(insn, " << EF
.Base
<< ", " << EF
.Width
<< ')';
1155 o
<< ", " << EF
.Offset
<< ", " << EF
.Width
<< ')';
1156 else if (EF
.Offset
!= 0)
1157 o
<< " << " << EF
.Offset
;
1161 if (Decoder
!= "") {
1162 OpHasCompleteDecoder
= OpInfo
.HasCompleteDecoder
;
1163 o
.indent(Indentation
) << "if (!Check(S, " << Decoder
1164 << "(MI, tmp, Address, Decoder))) { "
1165 << (OpHasCompleteDecoder
? ""
1166 : "DecodeComplete = false; ")
1167 << "return MCDisassembler::Fail; }\n";
1169 OpHasCompleteDecoder
= true;
1170 o
.indent(Indentation
) << "MI.addOperand(MCOperand::createImm(tmp));\n";
1174 void FilterChooser::emitDecoder(raw_ostream
&OS
, unsigned Indentation
,
1175 unsigned Opc
, bool &HasCompleteDecoder
) const {
1176 HasCompleteDecoder
= true;
1178 for (const auto &Op
: Operands
.find(Opc
)->second
) {
1179 // If a custom instruction decoder was specified, use that.
1180 if (Op
.numFields() == 0 && !Op
.Decoder
.empty()) {
1181 HasCompleteDecoder
= Op
.HasCompleteDecoder
;
1182 OS
.indent(Indentation
)
1183 << "if (!Check(S, " << Op
.Decoder
1184 << "(MI, insn, Address, Decoder))) { "
1185 << (HasCompleteDecoder
? "" : "DecodeComplete = false; ")
1186 << "return MCDisassembler::Fail; }\n";
1190 bool OpHasCompleteDecoder
;
1191 emitBinaryParser(OS
, Indentation
, Op
, OpHasCompleteDecoder
);
1192 if (!OpHasCompleteDecoder
)
1193 HasCompleteDecoder
= false;
1197 unsigned FilterChooser::getDecoderIndex(DecoderSet
&Decoders
,
1199 bool &HasCompleteDecoder
) const {
1200 // Build up the predicate string.
1201 SmallString
<256> Decoder
;
1202 // FIXME: emitDecoder() function can take a buffer directly rather than
1204 raw_svector_ostream
S(Decoder
);
1206 emitDecoder(S
, I
, Opc
, HasCompleteDecoder
);
1208 // Using the full decoder string as the key value here is a bit
1209 // heavyweight, but is effective. If the string comparisons become a
1210 // performance concern, we can implement a mangling of the predicate
1211 // data easily enough with a map back to the actual string. That's
1212 // overkill for now, though.
1214 // Make sure the predicate is in the table.
1215 Decoders
.insert(CachedHashString(Decoder
));
1216 // Now figure out the index for when we write out the table.
1217 DecoderSet::const_iterator P
= find(Decoders
, Decoder
.str());
1218 return (unsigned)(P
- Decoders
.begin());
1221 // If ParenIfBinOp is true, print a surrounding () if Val uses && or ||.
1222 bool FilterChooser::emitPredicateMatchAux(const Init
&Val
, bool ParenIfBinOp
,
1223 raw_ostream
&OS
) const {
1224 if (auto *D
= dyn_cast
<DefInit
>(&Val
)) {
1225 if (!D
->getDef()->isSubClassOf("SubtargetFeature"))
1227 OS
<< "Bits[" << Emitter
->PredicateNamespace
<< "::" << D
->getAsString()
1231 if (auto *D
= dyn_cast
<DagInit
>(&Val
)) {
1232 std::string Op
= D
->getOperator()->getAsString();
1233 if (Op
== "not" && D
->getNumArgs() == 1) {
1235 return emitPredicateMatchAux(*D
->getArg(0), true, OS
);
1237 if ((Op
== "any_of" || Op
== "all_of") && D
->getNumArgs() > 0) {
1238 bool Paren
= D
->getNumArgs() > 1 && std::exchange(ParenIfBinOp
, true);
1241 ListSeparator
LS(Op
== "any_of" ? " || " : " && ");
1242 for (auto *Arg
: D
->getArgs()) {
1244 if (emitPredicateMatchAux(*Arg
, ParenIfBinOp
, OS
))
1255 bool FilterChooser::emitPredicateMatch(raw_ostream
&o
, unsigned &Indentation
,
1256 unsigned Opc
) const {
1257 ListInit
*Predicates
=
1258 AllInstructions
[Opc
].EncodingDef
->getValueAsListInit("Predicates");
1259 bool IsFirstEmission
= true;
1260 for (unsigned i
= 0; i
< Predicates
->size(); ++i
) {
1261 Record
*Pred
= Predicates
->getElementAsRecord(i
);
1262 if (!Pred
->getValue("AssemblerMatcherPredicate"))
1265 if (!isa
<DagInit
>(Pred
->getValue("AssemblerCondDag")->getValue()))
1268 if (!IsFirstEmission
)
1270 if (emitPredicateMatchAux(*Pred
->getValueAsDag("AssemblerCondDag"),
1271 Predicates
->size() > 1, o
))
1272 PrintFatalError(Pred
->getLoc(), "Invalid AssemblerCondDag!");
1273 IsFirstEmission
= false;
1275 return !Predicates
->empty();
1278 bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc
) const {
1279 ListInit
*Predicates
=
1280 AllInstructions
[Opc
].EncodingDef
->getValueAsListInit("Predicates");
1281 for (unsigned i
= 0; i
< Predicates
->size(); ++i
) {
1282 Record
*Pred
= Predicates
->getElementAsRecord(i
);
1283 if (!Pred
->getValue("AssemblerMatcherPredicate"))
1286 if (isa
<DagInit
>(Pred
->getValue("AssemblerCondDag")->getValue()))
1292 unsigned FilterChooser::getPredicateIndex(DecoderTableInfo
&TableInfo
,
1293 StringRef Predicate
) const {
1294 // Using the full predicate string as the key value here is a bit
1295 // heavyweight, but is effective. If the string comparisons become a
1296 // performance concern, we can implement a mangling of the predicate
1297 // data easily enough with a map back to the actual string. That's
1298 // overkill for now, though.
1300 // Make sure the predicate is in the table.
1301 TableInfo
.Predicates
.insert(CachedHashString(Predicate
));
1302 // Now figure out the index for when we write out the table.
1303 PredicateSet::const_iterator P
= find(TableInfo
.Predicates
, Predicate
);
1304 return (unsigned)(P
- TableInfo
.Predicates
.begin());
1307 void FilterChooser::emitPredicateTableEntry(DecoderTableInfo
&TableInfo
,
1308 unsigned Opc
) const {
1309 if (!doesOpcodeNeedPredicate(Opc
))
1312 // Build up the predicate string.
1313 SmallString
<256> Predicate
;
1314 // FIXME: emitPredicateMatch() functions can take a buffer directly rather
1316 raw_svector_ostream
PS(Predicate
);
1318 emitPredicateMatch(PS
, I
, Opc
);
1320 // Figure out the index into the predicate table for the predicate just
1322 unsigned PIdx
= getPredicateIndex(TableInfo
, PS
.str());
1323 SmallString
<16> PBytes
;
1324 raw_svector_ostream
S(PBytes
);
1325 encodeULEB128(PIdx
, S
);
1327 TableInfo
.Table
.push_back(MCD::OPC_CheckPredicate
);
1329 for (unsigned i
= 0, e
= PBytes
.size(); i
!= e
; ++i
)
1330 TableInfo
.Table
.push_back(PBytes
[i
]);
1331 // Push location for NumToSkip backpatching.
1332 TableInfo
.FixupStack
.back().push_back(TableInfo
.Table
.size());
1333 TableInfo
.Table
.push_back(0);
1334 TableInfo
.Table
.push_back(0);
1335 TableInfo
.Table
.push_back(0);
1338 void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo
&TableInfo
,
1339 unsigned Opc
) const {
1340 const RecordVal
*RV
= AllInstructions
[Opc
].EncodingDef
->getValue("SoftFail");
1341 BitsInit
*SFBits
= RV
? dyn_cast
<BitsInit
>(RV
->getValue()) : nullptr;
1343 if (!SFBits
) return;
1344 BitsInit
*InstBits
=
1345 AllInstructions
[Opc
].EncodingDef
->getValueAsBitsInit("Inst");
1347 APInt
PositiveMask(BitWidth
, 0ULL);
1348 APInt
NegativeMask(BitWidth
, 0ULL);
1349 for (unsigned i
= 0; i
< BitWidth
; ++i
) {
1350 bit_value_t B
= bitFromBits(*SFBits
, i
);
1351 bit_value_t IB
= bitFromBits(*InstBits
, i
);
1353 if (B
!= BIT_TRUE
) continue;
1357 // The bit is meant to be false, so emit a check to see if it is true.
1358 PositiveMask
.setBit(i
);
1361 // The bit is meant to be true, so emit a check to see if it is false.
1362 NegativeMask
.setBit(i
);
1365 // The bit is not set; this must be an error!
1366 errs() << "SoftFail Conflict: bit SoftFail{" << i
<< "} in "
1367 << AllInstructions
[Opc
] << " is set but Inst{" << i
1369 << " - You can only mark a bit as SoftFail if it is fully defined"
1370 << " (1/0 - not '?') in Inst\n";
1375 bool NeedPositiveMask
= PositiveMask
.getBoolValue();
1376 bool NeedNegativeMask
= NegativeMask
.getBoolValue();
1378 if (!NeedPositiveMask
&& !NeedNegativeMask
)
1381 TableInfo
.Table
.push_back(MCD::OPC_SoftFail
);
1383 SmallString
<16> MaskBytes
;
1384 raw_svector_ostream
S(MaskBytes
);
1385 if (NeedPositiveMask
) {
1386 encodeULEB128(PositiveMask
.getZExtValue(), S
);
1387 for (unsigned i
= 0, e
= MaskBytes
.size(); i
!= e
; ++i
)
1388 TableInfo
.Table
.push_back(MaskBytes
[i
]);
1390 TableInfo
.Table
.push_back(0);
1391 if (NeedNegativeMask
) {
1393 encodeULEB128(NegativeMask
.getZExtValue(), S
);
1394 for (unsigned i
= 0, e
= MaskBytes
.size(); i
!= e
; ++i
)
1395 TableInfo
.Table
.push_back(MaskBytes
[i
]);
1397 TableInfo
.Table
.push_back(0);
1400 // Emits table entries to decode the singleton.
1401 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo
&TableInfo
,
1402 EncodingIDAndOpcode Opc
) const {
1403 std::vector
<unsigned> StartBits
;
1404 std::vector
<unsigned> EndBits
;
1405 std::vector
<uint64_t> FieldVals
;
1407 insnWithID(Insn
, Opc
.EncodingID
);
1409 // Look for islands of undecoded bits of the singleton.
1410 getIslands(StartBits
, EndBits
, FieldVals
, Insn
);
1412 unsigned Size
= StartBits
.size();
1414 // Emit the predicate table entry if one is needed.
1415 emitPredicateTableEntry(TableInfo
, Opc
.EncodingID
);
1417 // Check any additional encoding fields needed.
1418 for (unsigned I
= Size
; I
!= 0; --I
) {
1419 unsigned NumBits
= EndBits
[I
-1] - StartBits
[I
-1] + 1;
1420 TableInfo
.Table
.push_back(MCD::OPC_CheckField
);
1421 TableInfo
.Table
.push_back(StartBits
[I
-1]);
1422 TableInfo
.Table
.push_back(NumBits
);
1423 uint8_t Buffer
[16], *p
;
1424 encodeULEB128(FieldVals
[I
-1], Buffer
);
1425 for (p
= Buffer
; *p
>= 128 ; ++p
)
1426 TableInfo
.Table
.push_back(*p
);
1427 TableInfo
.Table
.push_back(*p
);
1428 // Push location for NumToSkip backpatching.
1429 TableInfo
.FixupStack
.back().push_back(TableInfo
.Table
.size());
1430 // The fixup is always 24-bits, so go ahead and allocate the space
1431 // in the table so all our relative position calculations work OK even
1432 // before we fully resolve the real value here.
1433 TableInfo
.Table
.push_back(0);
1434 TableInfo
.Table
.push_back(0);
1435 TableInfo
.Table
.push_back(0);
1438 // Check for soft failure of the match.
1439 emitSoftFailTableEntry(TableInfo
, Opc
.EncodingID
);
1441 bool HasCompleteDecoder
;
1443 getDecoderIndex(TableInfo
.Decoders
, Opc
.EncodingID
, HasCompleteDecoder
);
1445 // Produce OPC_Decode or OPC_TryDecode opcode based on the information
1446 // whether the instruction decoder is complete or not. If it is complete
1447 // then it handles all possible values of remaining variable/unfiltered bits
1448 // and for any value can determine if the bitpattern is a valid instruction
1449 // or not. This means OPC_Decode will be the final step in the decoding
1450 // process. If it is not complete, then the Fail return code from the
1451 // decoder method indicates that additional processing should be done to see
1452 // if there is any other instruction that also matches the bitpattern and
1454 TableInfo
.Table
.push_back(HasCompleteDecoder
? MCD::OPC_Decode
:
1455 MCD::OPC_TryDecode
);
1456 NumEncodingsSupported
++;
1457 uint8_t Buffer
[16], *p
;
1458 encodeULEB128(Opc
.Opcode
, Buffer
);
1459 for (p
= Buffer
; *p
>= 128 ; ++p
)
1460 TableInfo
.Table
.push_back(*p
);
1461 TableInfo
.Table
.push_back(*p
);
1463 SmallString
<16> Bytes
;
1464 raw_svector_ostream
S(Bytes
);
1465 encodeULEB128(DIdx
, S
);
1468 for (unsigned i
= 0, e
= Bytes
.size(); i
!= e
; ++i
)
1469 TableInfo
.Table
.push_back(Bytes
[i
]);
1471 if (!HasCompleteDecoder
) {
1472 // Push location for NumToSkip backpatching.
1473 TableInfo
.FixupStack
.back().push_back(TableInfo
.Table
.size());
1474 // Allocate the space for the fixup.
1475 TableInfo
.Table
.push_back(0);
1476 TableInfo
.Table
.push_back(0);
1477 TableInfo
.Table
.push_back(0);
1481 // Emits table entries to decode the singleton, and then to decode the rest.
1482 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo
&TableInfo
,
1483 const Filter
&Best
) const {
1484 EncodingIDAndOpcode Opc
= Best
.getSingletonOpc();
1486 // complex singletons need predicate checks from the first singleton
1487 // to refer forward to the variable filterchooser that follows.
1488 TableInfo
.FixupStack
.emplace_back();
1490 emitSingletonTableEntry(TableInfo
, Opc
);
1492 resolveTableFixups(TableInfo
.Table
, TableInfo
.FixupStack
.back(),
1493 TableInfo
.Table
.size());
1494 TableInfo
.FixupStack
.pop_back();
1496 Best
.getVariableFC().emitTableEntries(TableInfo
);
1499 // Assign a single filter and run with it. Top level API client can initialize
1500 // with a single filter to start the filtering process.
1501 void FilterChooser::runSingleFilter(unsigned startBit
, unsigned numBit
,
1504 Filters
.emplace_back(*this, startBit
, numBit
, true);
1505 BestIndex
= 0; // Sole Filter instance to choose from.
1506 bestFilter().recurse();
1509 // reportRegion is a helper function for filterProcessor to mark a region as
1510 // eligible for use as a filter region.
1511 void FilterChooser::reportRegion(bitAttr_t RA
, unsigned StartBit
,
1512 unsigned BitIndex
, bool AllowMixed
) {
1513 if (RA
== ATTR_MIXED
&& AllowMixed
)
1514 Filters
.emplace_back(*this, StartBit
, BitIndex
- StartBit
, true);
1515 else if (RA
== ATTR_ALL_SET
&& !AllowMixed
)
1516 Filters
.emplace_back(*this, StartBit
, BitIndex
- StartBit
, false);
1519 // FilterProcessor scans the well-known encoding bits of the instructions and
1520 // builds up a list of candidate filters. It chooses the best filter and
1521 // recursively descends down the decoding tree.
1522 bool FilterChooser::filterProcessor(bool AllowMixed
, bool Greedy
) {
1525 unsigned numInstructions
= Opcodes
.size();
1527 assert(numInstructions
&& "Filter created with no instructions");
1529 // No further filtering is necessary.
1530 if (numInstructions
== 1)
1533 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1534 // instructions is 3.
1535 if (AllowMixed
&& !Greedy
) {
1536 assert(numInstructions
== 3);
1538 for (auto Opcode
: Opcodes
) {
1539 std::vector
<unsigned> StartBits
;
1540 std::vector
<unsigned> EndBits
;
1541 std::vector
<uint64_t> FieldVals
;
1544 insnWithID(Insn
, Opcode
.EncodingID
);
1546 // Look for islands of undecoded bits of any instruction.
1547 if (getIslands(StartBits
, EndBits
, FieldVals
, Insn
) > 0) {
1548 // Found an instruction with island(s). Now just assign a filter.
1549 runSingleFilter(StartBits
[0], EndBits
[0] - StartBits
[0] + 1, true);
1557 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1558 // The automaton consumes the corresponding bit from each
1561 // Input symbols: 0, 1, and _ (unset).
1562 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1563 // Initial state: NONE.
1565 // (NONE) ------- [01] -> (ALL_SET)
1566 // (NONE) ------- _ ----> (ALL_UNSET)
1567 // (ALL_SET) ---- [01] -> (ALL_SET)
1568 // (ALL_SET) ---- _ ----> (MIXED)
1569 // (ALL_UNSET) -- [01] -> (MIXED)
1570 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1571 // (MIXED) ------ . ----> (MIXED)
1572 // (FILTERED)---- . ----> (FILTERED)
1574 std::vector
<bitAttr_t
> bitAttrs
;
1576 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1577 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1578 for (BitIndex
= 0; BitIndex
< BitWidth
; ++BitIndex
)
1579 if (FilterBitValues
[BitIndex
] == BIT_TRUE
||
1580 FilterBitValues
[BitIndex
] == BIT_FALSE
)
1581 bitAttrs
.push_back(ATTR_FILTERED
);
1583 bitAttrs
.push_back(ATTR_NONE
);
1585 for (unsigned InsnIndex
= 0; InsnIndex
< numInstructions
; ++InsnIndex
) {
1588 insnWithID(insn
, Opcodes
[InsnIndex
].EncodingID
);
1590 for (BitIndex
= 0; BitIndex
< BitWidth
; ++BitIndex
) {
1591 switch (bitAttrs
[BitIndex
]) {
1593 if (insn
[BitIndex
] == BIT_UNSET
)
1594 bitAttrs
[BitIndex
] = ATTR_ALL_UNSET
;
1596 bitAttrs
[BitIndex
] = ATTR_ALL_SET
;
1599 if (insn
[BitIndex
] == BIT_UNSET
)
1600 bitAttrs
[BitIndex
] = ATTR_MIXED
;
1602 case ATTR_ALL_UNSET
:
1603 if (insn
[BitIndex
] != BIT_UNSET
)
1604 bitAttrs
[BitIndex
] = ATTR_MIXED
;
1613 // The regionAttr automaton consumes the bitAttrs automatons' state,
1614 // lowest-to-highest.
1616 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1617 // States: NONE, ALL_SET, MIXED
1618 // Initial state: NONE
1620 // (NONE) ----- F --> (NONE)
1621 // (NONE) ----- S --> (ALL_SET) ; and set region start
1622 // (NONE) ----- U --> (NONE)
1623 // (NONE) ----- M --> (MIXED) ; and set region start
1624 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1625 // (ALL_SET) -- S --> (ALL_SET)
1626 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1627 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1628 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1629 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1630 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1631 // (MIXED) ---- M --> (MIXED)
1633 bitAttr_t RA
= ATTR_NONE
;
1634 unsigned StartBit
= 0;
1636 for (BitIndex
= 0; BitIndex
< BitWidth
; ++BitIndex
) {
1637 bitAttr_t bitAttr
= bitAttrs
[BitIndex
];
1639 assert(bitAttr
!= ATTR_NONE
&& "Bit without attributes");
1647 StartBit
= BitIndex
;
1650 case ATTR_ALL_UNSET
:
1653 StartBit
= BitIndex
;
1657 llvm_unreachable("Unexpected bitAttr!");
1663 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1668 case ATTR_ALL_UNSET
:
1669 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1673 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1674 StartBit
= BitIndex
;
1678 llvm_unreachable("Unexpected bitAttr!");
1684 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1685 StartBit
= BitIndex
;
1689 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1690 StartBit
= BitIndex
;
1693 case ATTR_ALL_UNSET
:
1694 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1700 llvm_unreachable("Unexpected bitAttr!");
1703 case ATTR_ALL_UNSET
:
1704 llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
1706 llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
1710 // At the end, if we're still in ALL_SET or MIXED states, report a region
1717 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1719 case ATTR_ALL_UNSET
:
1722 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1726 // We have finished with the filter processings. Now it's time to choose
1727 // the best performing filter.
1729 bool AllUseless
= true;
1730 unsigned BestScore
= 0;
1732 for (unsigned i
= 0, e
= Filters
.size(); i
!= e
; ++i
) {
1733 unsigned Usefulness
= Filters
[i
].usefulness();
1738 if (Usefulness
> BestScore
) {
1740 BestScore
= Usefulness
;
1745 bestFilter().recurse();
1748 } // end of FilterChooser::filterProcessor(bool)
1750 // Decides on the best configuration of filter(s) to use in order to decode
1751 // the instructions. A conflict of instructions may occur, in which case we
1752 // dump the conflict set to the standard error.
1753 void FilterChooser::doFilter() {
1754 unsigned Num
= Opcodes
.size();
1755 assert(Num
&& "FilterChooser created with no instructions");
1757 // Try regions of consecutive known bit values first.
1758 if (filterProcessor(false))
1761 // Then regions of mixed bits (both known and unitialized bit values allowed).
1762 if (filterProcessor(true))
1765 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1766 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1767 // well-known encoding pattern. In such case, we backtrack and scan for the
1768 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1769 if (Num
== 3 && filterProcessor(true, false))
1772 // If we come to here, the instruction decoding has failed.
1773 // Set the BestIndex to -1 to indicate so.
1777 // emitTableEntries - Emit state machine entries to decode our share of
1779 void FilterChooser::emitTableEntries(DecoderTableInfo
&TableInfo
) const {
1780 if (Opcodes
.size() == 1) {
1781 // There is only one instruction in the set, which is great!
1782 // Call emitSingletonDecoder() to see whether there are any remaining
1784 emitSingletonTableEntry(TableInfo
, Opcodes
[0]);
1788 // Choose the best filter to do the decodings!
1789 if (BestIndex
!= -1) {
1790 const Filter
&Best
= Filters
[BestIndex
];
1791 if (Best
.getNumFiltered() == 1)
1792 emitSingletonTableEntry(TableInfo
, Best
);
1794 Best
.emitTableEntry(TableInfo
);
1798 // We don't know how to decode these instructions! Dump the
1799 // conflict set and bail.
1801 // Print out useful conflict information for postmortem analysis.
1802 errs() << "Decoding Conflict:\n";
1804 dumpStack(errs(), "\t\t");
1806 for (auto Opcode
: Opcodes
) {
1808 emitNameWithID(errs(), Opcode
.EncodingID
);
1812 getBitsField(*AllInstructions
[Opcode
.EncodingID
].EncodingDef
, "Inst"));
1817 static std::string
findOperandDecoderMethod(Record
*Record
) {
1818 std::string Decoder
;
1820 RecordVal
*DecoderString
= Record
->getValue("DecoderMethod");
1821 StringInit
*String
= DecoderString
?
1822 dyn_cast
<StringInit
>(DecoderString
->getValue()) : nullptr;
1824 Decoder
= std::string(String
->getValue());
1825 if (!Decoder
.empty())
1829 if (Record
->isSubClassOf("RegisterOperand"))
1830 Record
= Record
->getValueAsDef("RegClass");
1832 if (Record
->isSubClassOf("RegisterClass")) {
1833 Decoder
= "Decode" + Record
->getName().str() + "RegisterClass";
1834 } else if (Record
->isSubClassOf("PointerLikeRegClass")) {
1835 Decoder
= "DecodePointerLikeRegClass" +
1836 utostr(Record
->getValueAsInt("RegClassKind"));
1842 OperandInfo
getOpInfo(Record
*TypeRecord
) {
1843 std::string Decoder
= findOperandDecoderMethod(TypeRecord
);
1845 RecordVal
*HasCompleteDecoderVal
= TypeRecord
->getValue("hasCompleteDecoder");
1846 BitInit
*HasCompleteDecoderBit
=
1847 HasCompleteDecoderVal
1848 ? dyn_cast
<BitInit
>(HasCompleteDecoderVal
->getValue())
1850 bool HasCompleteDecoder
=
1851 HasCompleteDecoderBit
? HasCompleteDecoderBit
->getValue() : true;
1853 return OperandInfo(Decoder
, HasCompleteDecoder
);
1856 void parseVarLenInstOperand(const Record
&Def
,
1857 std::vector
<OperandInfo
> &Operands
,
1858 const CodeGenInstruction
&CGI
) {
1860 const RecordVal
*RV
= Def
.getValue("Inst");
1861 VarLenInst
VLI(cast
<DagInit
>(RV
->getValue()), RV
);
1862 SmallVector
<int> TiedTo
;
1864 for (unsigned Idx
= 0; Idx
< CGI
.Operands
.size(); ++Idx
) {
1865 auto &Op
= CGI
.Operands
[Idx
];
1866 if (Op
.MIOperandInfo
&& Op
.MIOperandInfo
->getNumArgs() > 0)
1867 for (auto *Arg
: Op
.MIOperandInfo
->getArgs())
1868 Operands
.push_back(getOpInfo(cast
<DefInit
>(Arg
)->getDef()));
1870 Operands
.push_back(getOpInfo(Op
.Rec
));
1872 int TiedReg
= Op
.getTiedRegister();
1873 TiedTo
.push_back(-1);
1874 if (TiedReg
!= -1) {
1875 TiedTo
[Idx
] = TiedReg
;
1876 TiedTo
[TiedReg
] = Idx
;
1880 unsigned CurrBitPos
= 0;
1881 for (auto &EncodingSegment
: VLI
) {
1882 unsigned Offset
= 0;
1885 if (const StringInit
*SI
= dyn_cast
<StringInit
>(EncodingSegment
.Value
)) {
1886 OpName
= SI
->getValue();
1887 } else if (const DagInit
*DI
= dyn_cast
<DagInit
>(EncodingSegment
.Value
)) {
1888 OpName
= cast
<StringInit
>(DI
->getArg(0))->getValue();
1889 Offset
= cast
<IntInit
>(DI
->getArg(2))->getValue();
1892 if (!OpName
.empty()) {
1894 const_cast<CodeGenInstruction
&>(CGI
).Operands
.ParseOperandName(
1896 unsigned OpIdx
= CGI
.Operands
.getFlattenedOperandNumber(OpSubOpPair
);
1897 Operands
[OpIdx
].addField(CurrBitPos
, EncodingSegment
.BitWidth
, Offset
);
1898 if (!EncodingSegment
.CustomDecoder
.empty())
1899 Operands
[OpIdx
].Decoder
= EncodingSegment
.CustomDecoder
.str();
1901 int TiedReg
= TiedTo
[OpSubOpPair
.first
];
1902 if (TiedReg
!= -1) {
1903 unsigned OpIdx
= CGI
.Operands
.getFlattenedOperandNumber(
1904 std::make_pair(TiedReg
, OpSubOpPair
.second
));
1905 Operands
[OpIdx
].addField(CurrBitPos
, EncodingSegment
.BitWidth
, Offset
);
1909 CurrBitPos
+= EncodingSegment
.BitWidth
;
1913 static void debugDumpRecord(const Record
&Rec
) {
1914 // Dump the record, so we can see what's going on...
1916 raw_string_ostream
S(E
);
1917 S
<< "Dumping record for previous error:\n";
1922 /// For an operand field named OpName: populate OpInfo.InitValue with the
1923 /// constant-valued bit values, and OpInfo.Fields with the ranges of bits to
1924 /// insert from the decoded instruction.
1925 static void addOneOperandFields(const Record
&EncodingDef
, const BitsInit
&Bits
,
1926 std::map
<std::string
, std::string
> &TiedNames
,
1927 StringRef OpName
, OperandInfo
&OpInfo
) {
1928 // Some bits of the operand may be required to be 1 depending on the
1929 // instruction's encoding. Collect those bits.
1930 if (const RecordVal
*EncodedValue
= EncodingDef
.getValue(OpName
))
1931 if (const BitsInit
*OpBits
= dyn_cast
<BitsInit
>(EncodedValue
->getValue()))
1932 for (unsigned I
= 0; I
< OpBits
->getNumBits(); ++I
)
1933 if (const BitInit
*OpBit
= dyn_cast
<BitInit
>(OpBits
->getBit(I
)))
1934 if (OpBit
->getValue())
1935 OpInfo
.InitValue
|= 1ULL << I
;
1937 for (unsigned I
= 0, J
= 0; I
!= Bits
.getNumBits(); I
= J
) {
1939 unsigned Offset
= 0;
1940 for (; J
!= Bits
.getNumBits(); ++J
) {
1941 VarBitInit
*BJ
= dyn_cast
<VarBitInit
>(Bits
.getBit(J
));
1943 Var
= dyn_cast
<VarInit
>(BJ
->getBitVar());
1945 Offset
= BJ
->getBitNum();
1946 else if (BJ
->getBitNum() != Offset
+ J
- I
)
1949 Var
= dyn_cast
<VarInit
>(Bits
.getBit(J
));
1951 if (!Var
|| (Var
->getName() != OpName
&&
1952 Var
->getName() != TiedNames
[std::string(OpName
)]))
1958 OpInfo
.addField(I
, J
- I
, Offset
);
1963 populateInstruction(CodeGenTarget
&Target
, const Record
&EncodingDef
,
1964 const CodeGenInstruction
&CGI
, unsigned Opc
,
1965 std::map
<unsigned, std::vector
<OperandInfo
>> &Operands
,
1966 bool IsVarLenInst
) {
1967 const Record
&Def
= *CGI
.TheDef
;
1968 // If all the bit positions are not specified; do not decode this instruction.
1969 // We are bound to fail! For proper disassembly, the well-known encoding bits
1970 // of the instruction must be fully specified.
1972 BitsInit
&Bits
= getBitsField(EncodingDef
, "Inst");
1973 if (Bits
.allInComplete())
1976 std::vector
<OperandInfo
> InsnOperands
;
1978 // If the instruction has specified a custom decoding hook, use that instead
1979 // of trying to auto-generate the decoder.
1980 StringRef InstDecoder
= EncodingDef
.getValueAsString("DecoderMethod");
1981 if (InstDecoder
!= "") {
1982 bool HasCompleteInstDecoder
= EncodingDef
.getValueAsBit("hasCompleteDecoder");
1983 InsnOperands
.push_back(
1984 OperandInfo(std::string(InstDecoder
), HasCompleteInstDecoder
));
1985 Operands
[Opc
] = InsnOperands
;
1986 return Bits
.getNumBits();
1989 // Generate a description of the operand of the instruction that we know
1990 // how to decode automatically.
1991 // FIXME: We'll need to have a way to manually override this as needed.
1993 // Gather the outputs/inputs of the instruction, so we can find their
1994 // positions in the encoding. This assumes for now that they appear in the
1995 // MCInst in the order that they're listed.
1996 std::vector
<std::pair
<Init
*, StringRef
>> InOutOperands
;
1997 DagInit
*Out
= Def
.getValueAsDag("OutOperandList");
1998 DagInit
*In
= Def
.getValueAsDag("InOperandList");
1999 for (unsigned i
= 0; i
< Out
->getNumArgs(); ++i
)
2000 InOutOperands
.push_back(
2001 std::make_pair(Out
->getArg(i
), Out
->getArgNameStr(i
)));
2002 for (unsigned i
= 0; i
< In
->getNumArgs(); ++i
)
2003 InOutOperands
.push_back(
2004 std::make_pair(In
->getArg(i
), In
->getArgNameStr(i
)));
2006 // Search for tied operands, so that we can correctly instantiate
2007 // operands that are not explicitly represented in the encoding.
2008 std::map
<std::string
, std::string
> TiedNames
;
2009 for (unsigned i
= 0; i
< CGI
.Operands
.size(); ++i
) {
2010 auto &Op
= CGI
.Operands
[i
];
2011 for (unsigned j
= 0; j
< Op
.Constraints
.size(); ++j
) {
2012 const CGIOperandList::ConstraintInfo
&CI
= Op
.Constraints
[j
];
2014 int tiedTo
= CI
.getTiedOperand();
2015 std::pair
<unsigned, unsigned> SO
=
2016 CGI
.Operands
.getSubOperandNumber(tiedTo
);
2017 std::string TiedName
= CGI
.Operands
[SO
.first
].SubOpNames
[SO
.second
];
2018 if (TiedName
.empty())
2019 TiedName
= CGI
.Operands
[SO
.first
].Name
;
2020 std::string MyName
= Op
.SubOpNames
[j
];
2024 TiedNames
[MyName
] = TiedName
;
2025 TiedNames
[TiedName
] = MyName
;
2031 parseVarLenInstOperand(EncodingDef
, InsnOperands
, CGI
);
2033 // For each operand, see if we can figure out where it is encoded.
2034 for (const auto &Op
: InOutOperands
) {
2035 Init
*OpInit
= Op
.first
;
2036 StringRef OpName
= Op
.second
;
2038 // We're ready to find the instruction encoding locations for this operand.
2040 // First, find the operand type ("OpInit"), and sub-op names
2041 // ("SubArgDag") if present.
2042 DagInit
*SubArgDag
= dyn_cast
<DagInit
>(OpInit
);
2044 OpInit
= SubArgDag
->getOperator();
2045 Record
*OpTypeRec
= cast
<DefInit
>(OpInit
)->getDef();
2046 // Lookup the sub-operands from the operand type record (note that only
2047 // Operand subclasses have MIOperandInfo, see CodeGenInstruction.cpp).
2048 DagInit
*SubOps
= OpTypeRec
->isSubClassOf("Operand")
2049 ? OpTypeRec
->getValueAsDag("MIOperandInfo")
2052 // Lookup the decoder method and construct a new OperandInfo to hold our result.
2053 OperandInfo OpInfo
= getOpInfo(OpTypeRec
);
2055 // If we have named sub-operands...
2057 // Then there should not be a custom decoder specified on the top-level
2059 if (!OpInfo
.Decoder
.empty()) {
2060 PrintError(EncodingDef
.getLoc(),
2061 "DecoderEmitter: operand \"" + OpName
+ "\" has type \"" +
2062 OpInit
->getAsString() +
2063 "\" with a custom DecoderMethod, but also named "
2068 // Decode each of the sub-ops separately.
2069 assert(SubOps
&& SubArgDag
->getNumArgs() == SubOps
->getNumArgs());
2070 for (unsigned i
= 0; i
< SubOps
->getNumArgs(); ++i
) {
2071 StringRef SubOpName
= SubArgDag
->getArgNameStr(i
);
2072 OperandInfo SubOpInfo
=
2073 getOpInfo(cast
<DefInit
>(SubOps
->getArg(i
))->getDef());
2075 addOneOperandFields(EncodingDef
, Bits
, TiedNames
, SubOpName
,
2077 InsnOperands
.push_back(SubOpInfo
);
2082 // Otherwise, if we have an operand with sub-operands, but they aren't
2084 if (SubOps
&& OpInfo
.Decoder
.empty()) {
2085 // If it's a single sub-operand, and no custom decoder, use the decoder
2086 // from the one sub-operand.
2087 if (SubOps
->getNumArgs() == 1)
2088 OpInfo
= getOpInfo(cast
<DefInit
>(SubOps
->getArg(0))->getDef());
2090 // If we have multiple sub-ops, there'd better have a custom
2091 // decoder. (Otherwise we don't know how to populate them properly...)
2092 if (SubOps
->getNumArgs() > 1) {
2093 PrintError(EncodingDef
.getLoc(),
2094 "DecoderEmitter: operand \"" + OpName
+
2095 "\" uses MIOperandInfo with multiple ops, but doesn't "
2096 "have a custom decoder!");
2097 debugDumpRecord(EncodingDef
);
2102 addOneOperandFields(EncodingDef
, Bits
, TiedNames
, OpName
, OpInfo
);
2103 // FIXME: it should be an error not to find a definition for a given
2104 // operand, rather than just failing to add it to the resulting
2105 // instruction! (This is a longstanding bug, which will be addressed in an
2106 // upcoming change.)
2107 if (OpInfo
.numFields() > 0)
2108 InsnOperands
.push_back(OpInfo
);
2111 Operands
[Opc
] = InsnOperands
;
2115 // Dumps the instruction encoding bits.
2116 dumpBits(errs(), Bits
);
2120 // Dumps the list of operand info.
2121 for (unsigned i
= 0, e
= CGI
.Operands
.size(); i
!= e
; ++i
) {
2122 const CGIOperandList::OperandInfo
&Info
= CGI
.Operands
[i
];
2123 const std::string
&OperandName
= Info
.Name
;
2124 const Record
&OperandDef
= *Info
.Rec
;
2126 errs() << "\t" << OperandName
<< " (" << OperandDef
.getName() << ")\n";
2131 return Bits
.getNumBits();
2134 // emitFieldFromInstruction - Emit the templated helper function
2135 // fieldFromInstruction().
2136 // On Windows we make sure that this function is not inlined when
2137 // using the VS compiler. It has a bug which causes the function
2138 // to be optimized out in some circumstances. See llvm.org/pr38292
2139 static void emitFieldFromInstruction(formatted_raw_ostream
&OS
) {
2140 OS
<< "// Helper functions for extracting fields from encoded instructions.\n"
2141 << "// InsnType must either be integral or an APInt-like object that "
2143 << "// * be default-constructible and copy-constructible\n"
2144 << "// * be constructible from an APInt (this can be private)\n"
2145 << "// * Support insertBits(bits, startBit, numBits)\n"
2146 << "// * Support extractBitsAsZExtValue(numBits, startBit)\n"
2147 << "// * Support the ~, &, ==, and != operators with other objects of "
2149 << "// * Support the != and bitwise & with uint64_t\n"
2150 << "// * Support put (<<) to raw_ostream&\n"
2151 << "template <typename InsnType>\n"
2152 << "#if defined(_MSC_VER) && !defined(__clang__)\n"
2153 << "__declspec(noinline)\n"
2155 << "static std::enable_if_t<std::is_integral<InsnType>::value, InsnType>\n"
2156 << "fieldFromInstruction(const InsnType &insn, unsigned startBit,\n"
2157 << " unsigned numBits) {\n"
2158 << " assert(startBit + numBits <= 64 && \"Cannot support >64-bit "
2159 "extractions!\");\n"
2160 << " assert(startBit + numBits <= (sizeof(InsnType) * 8) &&\n"
2161 << " \"Instruction field out of bounds!\");\n"
2162 << " InsnType fieldMask;\n"
2163 << " if (numBits == sizeof(InsnType) * 8)\n"
2164 << " fieldMask = (InsnType)(-1LL);\n"
2166 << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
2167 << " return (insn & fieldMask) >> startBit;\n"
2170 << "template <typename InsnType>\n"
2171 << "static std::enable_if_t<!std::is_integral<InsnType>::value, "
2173 << "fieldFromInstruction(const InsnType &insn, unsigned startBit,\n"
2174 << " unsigned numBits) {\n"
2175 << " return insn.extractBitsAsZExtValue(numBits, startBit);\n"
2179 // emitInsertBits - Emit the templated helper function insertBits().
2180 static void emitInsertBits(formatted_raw_ostream
&OS
) {
2181 OS
<< "// Helper function for inserting bits extracted from an encoded "
2182 "instruction into\n"
2184 << "template <typename InsnType>\n"
2185 << "static std::enable_if_t<std::is_integral<InsnType>::value>\n"
2186 << "insertBits(InsnType &field, InsnType bits, unsigned startBit, "
2187 "unsigned numBits) {\n"
2188 << " assert(startBit + numBits <= sizeof field * 8);\n"
2189 << " field |= (InsnType)bits << startBit;\n"
2192 << "template <typename InsnType>\n"
2193 << "static std::enable_if_t<!std::is_integral<InsnType>::value>\n"
2194 << "insertBits(InsnType &field, uint64_t bits, unsigned startBit, "
2195 "unsigned numBits) {\n"
2196 << " field.insertBits(bits, startBit, numBits);\n"
2200 // emitDecodeInstruction - Emit the templated helper function
2201 // decodeInstruction().
2202 static void emitDecodeInstruction(formatted_raw_ostream
&OS
,
2203 bool IsVarLenInst
) {
2204 OS
<< "template <typename InsnType>\n"
2205 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
2207 << " InsnType insn, uint64_t "
2209 << " const MCDisassembler *DisAsm,\n"
2210 << " const MCSubtargetInfo &STI";
2213 << " llvm::function_ref<void(APInt "
2215 << " uint64_t)> makeUp";
2218 << " const FeatureBitset &Bits = STI.getFeatureBits();\n"
2220 << " const uint8_t *Ptr = DecodeTable;\n"
2221 << " uint64_t CurFieldValue = 0;\n"
2222 << " DecodeStatus S = MCDisassembler::Success;\n"
2223 << " while (true) {\n"
2224 << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
2225 << " switch (*Ptr) {\n"
2227 << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
2228 << " return MCDisassembler::Fail;\n"
2229 << " case MCD::OPC_ExtractField: {\n"
2230 << " unsigned Start = *++Ptr;\n"
2231 << " unsigned Len = *++Ptr;\n"
2234 OS
<< " makeUp(insn, Start + Len);\n";
2235 OS
<< " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
2236 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << "
2238 << " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
2241 << " case MCD::OPC_FilterValue: {\n"
2242 << " // Decode the field value.\n"
2243 << " unsigned Len;\n"
2244 << " uint64_t Val = decodeULEB128(++Ptr, &Len);\n"
2246 << " // NumToSkip is a plain 24-bit integer.\n"
2247 << " unsigned NumToSkip = *Ptr++;\n"
2248 << " NumToSkip |= (*Ptr++) << 8;\n"
2249 << " NumToSkip |= (*Ptr++) << 16;\n"
2251 << " // Perform the filter operation.\n"
2252 << " if (Val != CurFieldValue)\n"
2253 << " Ptr += NumToSkip;\n"
2254 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << "
2255 "\", \" << NumToSkip\n"
2256 << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" "
2258 << " << \" continuing at \" << (Ptr - DecodeTable) << "
2263 << " case MCD::OPC_CheckField: {\n"
2264 << " unsigned Start = *++Ptr;\n"
2265 << " unsigned Len = *++Ptr;\n";
2267 OS
<< " makeUp(insn, Start + Len);\n";
2268 OS
<< " uint64_t FieldValue = fieldFromInstruction(insn, Start, Len);\n"
2269 << " // Decode the field value.\n"
2270 << " unsigned PtrLen = 0;\n"
2271 << " uint64_t ExpectedValue = decodeULEB128(++Ptr, &PtrLen);\n"
2272 << " Ptr += PtrLen;\n"
2273 << " // NumToSkip is a plain 24-bit integer.\n"
2274 << " unsigned NumToSkip = *Ptr++;\n"
2275 << " NumToSkip |= (*Ptr++) << 8;\n"
2276 << " NumToSkip |= (*Ptr++) << 16;\n"
2278 << " // If the actual and expected values don't match, skip.\n"
2279 << " if (ExpectedValue != FieldValue)\n"
2280 << " Ptr += NumToSkip;\n"
2281 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << "
2283 << " << Len << \", \" << ExpectedValue << \", \" << "
2285 << " << \"): FieldValue = \" << FieldValue << \", "
2286 "ExpectedValue = \"\n"
2287 << " << ExpectedValue << \": \"\n"
2288 << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : "
2292 << " case MCD::OPC_CheckPredicate: {\n"
2293 << " unsigned Len;\n"
2294 << " // Decode the Predicate Index value.\n"
2295 << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
2297 << " // NumToSkip is a plain 24-bit integer.\n"
2298 << " unsigned NumToSkip = *Ptr++;\n"
2299 << " NumToSkip |= (*Ptr++) << 8;\n"
2300 << " NumToSkip |= (*Ptr++) << 16;\n"
2301 << " // Check the predicate.\n"
2303 << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
2304 << " Ptr += NumToSkip;\n"
2306 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx "
2308 << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2312 << " case MCD::OPC_Decode: {\n"
2313 << " unsigned Len;\n"
2314 << " // Decode the Opcode value.\n"
2315 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2317 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2321 << " MI.setOpcode(Opc);\n"
2322 << " bool DecodeComplete;\n";
2324 OS
<< " Len = InstrLenTable[Opc];\n"
2325 << " makeUp(insn, Len);\n";
2327 OS
<< " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, "
2328 "DecodeComplete);\n"
2329 << " assert(DecodeComplete);\n"
2331 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
2332 << " << \", using decoder \" << DecodeIdx << \": \"\n"
2333 << " << (S != MCDisassembler::Fail ? \"PASS\" : "
2334 "\"FAIL\") << \"\\n\");\n"
2337 << " case MCD::OPC_TryDecode: {\n"
2338 << " unsigned Len;\n"
2339 << " // Decode the Opcode value.\n"
2340 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2342 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2344 << " // NumToSkip is a plain 24-bit integer.\n"
2345 << " unsigned NumToSkip = *Ptr++;\n"
2346 << " NumToSkip |= (*Ptr++) << 8;\n"
2347 << " NumToSkip |= (*Ptr++) << 16;\n"
2349 << " // Perform the decode operation.\n"
2350 << " MCInst TmpMI;\n"
2351 << " TmpMI.setOpcode(Opc);\n"
2352 << " bool DecodeComplete;\n"
2353 << " S = decodeToMCInst(S, DecodeIdx, insn, TmpMI, Address, DisAsm, "
2354 "DecodeComplete);\n"
2355 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_TryDecode: opcode \" << "
2357 << " << \", using decoder \" << DecodeIdx << \": \");\n"
2359 << " if (DecodeComplete) {\n"
2360 << " // Decoding complete.\n"
2361 << " LLVM_DEBUG(dbgs() << (S != MCDisassembler::Fail ? \"PASS\" : "
2362 "\"FAIL\") << \"\\n\");\n"
2366 << " assert(S == MCDisassembler::Fail);\n"
2367 << " // If the decoding was incomplete, skip.\n"
2368 << " Ptr += NumToSkip;\n"
2369 << " LLVM_DEBUG(dbgs() << \"FAIL: continuing at \" << (Ptr - "
2370 "DecodeTable) << \"\\n\");\n"
2371 << " // Reset decode status. This also drops a SoftFail status "
2373 << " // set before the decode attempt.\n"
2374 << " S = MCDisassembler::Success;\n"
2378 << " case MCD::OPC_SoftFail: {\n"
2379 << " // Decode the mask values.\n"
2380 << " unsigned Len;\n"
2381 << " uint64_t PositiveMask = decodeULEB128(++Ptr, &Len);\n"
2383 << " uint64_t NegativeMask = decodeULEB128(Ptr, &Len);\n"
2385 << " bool Fail = (insn & PositiveMask) != 0 || (~insn & "
2386 "NegativeMask) != 0;\n"
2388 << " S = MCDisassembler::SoftFail;\n"
2389 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? "
2390 "\"FAIL\\n\" : \"PASS\\n\"));\n"
2393 << " case MCD::OPC_Fail: {\n"
2394 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
2395 << " return MCDisassembler::Fail;\n"
2399 << " llvm_unreachable(\"bogosity detected in disassembler state "
2404 // Helper to propagate SoftFail status. Returns false if the status is Fail;
2405 // callers are expected to early-exit in that condition. (Note, the '&' operator
2406 // is correct to propagate the values of this enum; see comment on 'enum
2408 static void emitCheck(formatted_raw_ostream
&OS
) {
2409 OS
<< "static bool Check(DecodeStatus &Out, DecodeStatus In) {\n"
2410 << " Out = static_cast<DecodeStatus>(Out & In);\n"
2411 << " return Out != MCDisassembler::Fail;\n"
2415 // Emits disassembler code for instruction decoding.
2416 void DecoderEmitter::run(raw_ostream
&o
) {
2417 formatted_raw_ostream
OS(o
);
2418 OS
<< "#include \"llvm/MC/MCInst.h\"\n";
2419 OS
<< "#include \"llvm/MC/MCSubtargetInfo.h\"\n";
2420 OS
<< "#include \"llvm/Support/DataTypes.h\"\n";
2421 OS
<< "#include \"llvm/Support/Debug.h\"\n";
2422 OS
<< "#include \"llvm/Support/LEB128.h\"\n";
2423 OS
<< "#include \"llvm/Support/raw_ostream.h\"\n";
2424 OS
<< "#include \"llvm/TargetParser/SubtargetFeature.h\"\n";
2425 OS
<< "#include <assert.h>\n";
2427 OS
<< "namespace llvm {\n\n";
2429 emitFieldFromInstruction(OS
);
2433 Target
.reverseBitsForLittleEndianEncoding();
2435 // Parameterize the decoders based on namespace and instruction width.
2436 std::set
<StringRef
> HwModeNames
;
2437 const auto &NumberedInstructions
= Target
.getInstructionsByEnumValue();
2438 NumberedEncodings
.reserve(NumberedInstructions
.size());
2439 DenseMap
<Record
*, unsigned> IndexOfInstruction
;
2440 // First, collect all HwModes referenced by the target.
2441 for (const auto &NumberedInstruction
: NumberedInstructions
) {
2442 IndexOfInstruction
[NumberedInstruction
->TheDef
] = NumberedEncodings
.size();
2444 if (const RecordVal
*RV
=
2445 NumberedInstruction
->TheDef
->getValue("EncodingInfos")) {
2446 if (auto *DI
= dyn_cast_or_null
<DefInit
>(RV
->getValue())) {
2447 const CodeGenHwModes
&HWM
= Target
.getHwModes();
2448 EncodingInfoByHwMode
EBM(DI
->getDef(), HWM
);
2449 for (auto &KV
: EBM
)
2450 HwModeNames
.insert(HWM
.getMode(KV
.first
).Name
);
2455 // If HwModeNames is empty, add the empty string so we always have one HwMode.
2456 if (HwModeNames
.empty())
2457 HwModeNames
.insert("");
2459 for (const auto &NumberedInstruction
: NumberedInstructions
) {
2460 IndexOfInstruction
[NumberedInstruction
->TheDef
] = NumberedEncodings
.size();
2462 if (const RecordVal
*RV
=
2463 NumberedInstruction
->TheDef
->getValue("EncodingInfos")) {
2464 if (DefInit
*DI
= dyn_cast_or_null
<DefInit
>(RV
->getValue())) {
2465 const CodeGenHwModes
&HWM
= Target
.getHwModes();
2466 EncodingInfoByHwMode
EBM(DI
->getDef(), HWM
);
2467 for (auto &KV
: EBM
) {
2468 NumberedEncodings
.emplace_back(KV
.second
, NumberedInstruction
,
2469 HWM
.getMode(KV
.first
).Name
);
2470 HwModeNames
.insert(HWM
.getMode(KV
.first
).Name
);
2475 // This instruction is encoded the same on all HwModes. Emit it for all
2477 for (StringRef HwModeName
: HwModeNames
)
2478 NumberedEncodings
.emplace_back(NumberedInstruction
->TheDef
,
2479 NumberedInstruction
, HwModeName
);
2481 for (const auto &NumberedAlias
: RK
.getAllDerivedDefinitions("AdditionalEncoding"))
2482 NumberedEncodings
.emplace_back(
2484 &Target
.getInstruction(NumberedAlias
->getValueAsDef("AliasOf")));
2486 std::map
<std::pair
<std::string
, unsigned>, std::vector
<EncodingIDAndOpcode
>>
2488 std::map
<unsigned, std::vector
<OperandInfo
>> Operands
;
2489 std::vector
<unsigned> InstrLen
;
2492 any_of(NumberedInstructions
, [](const CodeGenInstruction
*CGI
) {
2493 RecordVal
*RV
= CGI
->TheDef
->getValue("Inst");
2494 return RV
&& isa
<DagInit
>(RV
->getValue());
2496 unsigned MaxInstLen
= 0;
2498 for (unsigned i
= 0; i
< NumberedEncodings
.size(); ++i
) {
2499 const Record
*EncodingDef
= NumberedEncodings
[i
].EncodingDef
;
2500 const CodeGenInstruction
*Inst
= NumberedEncodings
[i
].Inst
;
2501 const Record
*Def
= Inst
->TheDef
;
2502 unsigned Size
= EncodingDef
->getValueAsInt("Size");
2503 if (Def
->getValueAsString("Namespace") == "TargetOpcode" ||
2504 Def
->getValueAsBit("isPseudo") ||
2505 Def
->getValueAsBit("isAsmParserOnly") ||
2506 Def
->getValueAsBit("isCodeGenOnly")) {
2507 NumEncodingsLackingDisasm
++;
2511 if (i
< NumberedInstructions
.size())
2515 if (!Size
&& !IsVarLenInst
)
2519 InstrLen
.resize(NumberedInstructions
.size(), 0);
2521 if (unsigned Len
= populateInstruction(Target
, *EncodingDef
, *Inst
, i
,
2522 Operands
, IsVarLenInst
)) {
2524 MaxInstLen
= std::max(MaxInstLen
, Len
);
2527 std::string DecoderNamespace
=
2528 std::string(EncodingDef
->getValueAsString("DecoderNamespace"));
2529 if (!NumberedEncodings
[i
].HwModeName
.empty())
2531 std::string("_") + NumberedEncodings
[i
].HwModeName
.str();
2532 OpcMap
[std::make_pair(DecoderNamespace
, Size
)].emplace_back(
2533 i
, IndexOfInstruction
.find(Def
)->second
);
2535 NumEncodingsOmitted
++;
2539 DecoderTableInfo TableInfo
;
2540 for (const auto &Opc
: OpcMap
) {
2541 // Emit the decoder for this namespace+width combination.
2542 ArrayRef
<EncodingAndInst
> NumberedEncodingsRef(
2543 NumberedEncodings
.data(), NumberedEncodings
.size());
2544 FilterChooser
FC(NumberedEncodingsRef
, Opc
.second
, Operands
,
2545 IsVarLenInst
? MaxInstLen
: 8 * Opc
.first
.second
, this);
2547 // The decode table is cleared for each top level decoder function. The
2548 // predicates and decoders themselves, however, are shared across all
2549 // decoders to give more opportunities for uniqueing.
2550 TableInfo
.Table
.clear();
2551 TableInfo
.FixupStack
.clear();
2552 TableInfo
.Table
.reserve(16384);
2553 TableInfo
.FixupStack
.emplace_back();
2554 FC
.emitTableEntries(TableInfo
);
2555 // Any NumToSkip fixups in the top level scope can resolve to the
2556 // OPC_Fail at the end of the table.
2557 assert(TableInfo
.FixupStack
.size() == 1 && "fixup stack phasing error!");
2558 // Resolve any NumToSkip fixups in the current scope.
2559 resolveTableFixups(TableInfo
.Table
, TableInfo
.FixupStack
.back(),
2560 TableInfo
.Table
.size());
2561 TableInfo
.FixupStack
.clear();
2563 TableInfo
.Table
.push_back(MCD::OPC_Fail
);
2565 // Print the table to the output stream.
2566 emitTable(OS
, TableInfo
.Table
, 0, FC
.getBitWidth(), Opc
.first
.first
);
2569 // For variable instruction, we emit a instruction length table
2570 // to let the decoder know how long the instructions are.
2571 // You can see example usage in M68k's disassembler.
2573 emitInstrLenTable(OS
, InstrLen
);
2574 // Emit the predicate function.
2575 emitPredicateFunction(OS
, TableInfo
.Predicates
, 0);
2577 // Emit the decoder function.
2578 emitDecoderFunction(OS
, TableInfo
.Decoders
, 0);
2580 // Emit the main entry point for the decoder, decodeInstruction().
2581 emitDecodeInstruction(OS
, IsVarLenInst
);
2583 OS
<< "\n} // end namespace llvm\n";
2588 void EmitDecoder(RecordKeeper
&RK
, raw_ostream
&OS
,
2589 const std::string
&PredicateNamespace
) {
2590 DecoderEmitter(RK
, PredicateNamespace
).run(OS
);
2593 } // end namespace llvm