blob: 361bad3830239f84525467cf2efbd78f3220216f [file] [log] [blame]
Owen Anderson4e818902011-02-18 21:51:29 +00001//===------------ FixedLenDecoderEmitter.cpp - Decoder Generator ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// It contains the tablegen backend that emits the decoder functions for
11// targets with fixed length instruction set.
12//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000015#include "CodeGenInstruction.h"
Owen Anderson4e818902011-02-18 21:51:29 +000016#include "CodeGenTarget.h"
James Molloyd9ba4fd2012-02-09 10:56:31 +000017#include "llvm/ADT/APInt.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000018#include "llvm/ADT/ArrayRef.h"
Justin Lebar5e83dfe2016-10-21 21:45:01 +000019#include "llvm/ADT/CachedHashString.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000020#include "llvm/ADT/SmallString.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000021#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/STLExtras.h"
Owen Anderson4e818902011-02-18 21:51:29 +000023#include "llvm/ADT/StringExtras.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000024#include "llvm/ADT/StringRef.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000025#include "llvm/MC/MCFixedLenDisassembler.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000026#include "llvm/Support/Casting.h"
Owen Anderson4e818902011-02-18 21:51:29 +000027#include "llvm/Support/Debug.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000028#include "llvm/Support/ErrorHandling.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000029#include "llvm/Support/FormattedStream.h"
30#include "llvm/Support/LEB128.h"
Owen Anderson4e818902011-02-18 21:51:29 +000031#include "llvm/Support/raw_ostream.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000032#include "llvm/TableGen/Error.h"
33#include "llvm/TableGen/Record.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000034#include <algorithm>
35#include <cassert>
36#include <cstddef>
37#include <cstdint>
Owen Anderson4e818902011-02-18 21:51:29 +000038#include <map>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000039#include <memory>
40#include <set>
Owen Anderson4e818902011-02-18 21:51:29 +000041#include <string>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000042#include <utility>
Chandler Carruth91d19d82012-12-04 10:37:14 +000043#include <vector>
Owen Anderson4e818902011-02-18 21:51:29 +000044
45using namespace llvm;
46
Chandler Carruth97acce22014-04-22 03:06:00 +000047#define DEBUG_TYPE "decoder-emitter"
48
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000049namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000050
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000051struct EncodingField {
52 unsigned Base, Width, Offset;
53 EncodingField(unsigned B, unsigned W, unsigned O)
54 : Base(B), Width(W), Offset(O) { }
55};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000056
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000057struct OperandInfo {
58 std::vector<EncodingField> Fields;
59 std::string Decoder;
Petr Pavlu182b0572015-07-15 08:04:27 +000060 bool HasCompleteDecoder;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000061
Petr Pavlu182b0572015-07-15 08:04:27 +000062 OperandInfo(std::string D, bool HCD)
Benjamin Kramer82de7d32016-05-27 14:27:24 +000063 : Decoder(std::move(D)), HasCompleteDecoder(HCD) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000064
65 void addField(unsigned Base, unsigned Width, unsigned Offset) {
66 Fields.push_back(EncodingField(Base, Width, Offset));
67 }
68
69 unsigned numFields() const { return Fields.size(); }
70
71 typedef std::vector<EncodingField>::const_iterator const_iterator;
72
73 const_iterator begin() const { return Fields.begin(); }
74 const_iterator end() const { return Fields.end(); }
75};
Jim Grosbachecaef492012-08-14 19:06:05 +000076
77typedef std::vector<uint8_t> DecoderTable;
78typedef uint32_t DecoderFixup;
79typedef std::vector<DecoderFixup> FixupList;
80typedef std::vector<FixupList> FixupScopeList;
Justin Lebar5e83dfe2016-10-21 21:45:01 +000081typedef SmallSetVector<CachedHashString, 16> PredicateSet;
82typedef SmallSetVector<CachedHashString, 16> DecoderSet;
Jim Grosbachecaef492012-08-14 19:06:05 +000083struct DecoderTableInfo {
84 DecoderTable Table;
85 FixupScopeList FixupStack;
86 PredicateSet Predicates;
87 DecoderSet Decoders;
88};
89
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000090class FixedLenDecoderEmitter {
Craig Topperf9265322016-01-17 20:38:14 +000091 ArrayRef<const CodeGenInstruction *> NumberedInstructions;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000092
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000093public:
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000094 // Defaults preserved here for documentation, even though they aren't
95 // strictly necessary given the way that this is currently being called.
Benjamin Kramer82de7d32016-05-27 14:27:24 +000096 FixedLenDecoderEmitter(RecordKeeper &R, std::string PredicateNamespace,
97 std::string GPrefix = "if (",
Petr Pavlu182b0572015-07-15 08:04:27 +000098 std::string GPostfix = " == MCDisassembler::Fail)",
Benjamin Kramer82de7d32016-05-27 14:27:24 +000099 std::string ROK = "MCDisassembler::Success",
100 std::string RFail = "MCDisassembler::Fail",
101 std::string L = "")
102 : Target(R), PredicateNamespace(std::move(PredicateNamespace)),
103 GuardPrefix(std::move(GPrefix)), GuardPostfix(std::move(GPostfix)),
104 ReturnOK(std::move(ROK)), ReturnFail(std::move(RFail)),
105 Locals(std::move(L)) {}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000106
Jim Grosbachecaef492012-08-14 19:06:05 +0000107 // Emit the decoder state machine table.
108 void emitTable(formatted_raw_ostream &o, DecoderTable &Table,
109 unsigned Indentation, unsigned BitWidth,
110 StringRef Namespace) const;
111 void emitPredicateFunction(formatted_raw_ostream &OS,
112 PredicateSet &Predicates,
113 unsigned Indentation) const;
114 void emitDecoderFunction(formatted_raw_ostream &OS,
115 DecoderSet &Decoders,
116 unsigned Indentation) const;
117
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000118 // run - Output the code emitter
119 void run(raw_ostream &o);
120
121private:
122 CodeGenTarget Target;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000123
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000124public:
125 std::string PredicateNamespace;
126 std::string GuardPrefix, GuardPostfix;
127 std::string ReturnOK, ReturnFail;
128 std::string Locals;
129};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000130
131} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000132
Owen Anderson4e818902011-02-18 21:51:29 +0000133// The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
134// for a bit value.
135//
136// BIT_UNFILTERED is used as the init value for a filter position. It is used
137// only for filter processings.
138typedef enum {
139 BIT_TRUE, // '1'
140 BIT_FALSE, // '0'
141 BIT_UNSET, // '?'
142 BIT_UNFILTERED // unfiltered
143} bit_value_t;
144
145static bool ValueSet(bit_value_t V) {
146 return (V == BIT_TRUE || V == BIT_FALSE);
147}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000148
Owen Anderson4e818902011-02-18 21:51:29 +0000149static bool ValueNotSet(bit_value_t V) {
150 return (V == BIT_UNSET);
151}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000152
Owen Anderson4e818902011-02-18 21:51:29 +0000153static int Value(bit_value_t V) {
154 return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
155}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000156
Craig Topper48c112b2012-03-16 05:58:09 +0000157static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000158 if (BitInit *bit = dyn_cast<BitInit>(bits.getBit(index)))
Owen Anderson4e818902011-02-18 21:51:29 +0000159 return bit->getValue() ? BIT_TRUE : BIT_FALSE;
160
161 // The bit is uninitialized.
162 return BIT_UNSET;
163}
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000164
Owen Anderson4e818902011-02-18 21:51:29 +0000165// Prints the bit value for each position.
Craig Topper48c112b2012-03-16 05:58:09 +0000166static void dumpBits(raw_ostream &o, const BitsInit &bits) {
Craig Topper29688ab2012-08-17 05:42:16 +0000167 for (unsigned index = bits.getNumBits(); index > 0; --index) {
Owen Anderson4e818902011-02-18 21:51:29 +0000168 switch (bitFromBits(bits, index - 1)) {
169 case BIT_TRUE:
170 o << "1";
171 break;
172 case BIT_FALSE:
173 o << "0";
174 break;
175 case BIT_UNSET:
176 o << "_";
177 break;
178 default:
Craig Topperc4965bc2012-02-05 07:21:30 +0000179 llvm_unreachable("unexpected return value from bitFromBits");
Owen Anderson4e818902011-02-18 21:51:29 +0000180 }
181 }
182}
183
Mehdi Amini32986ed2016-10-04 23:47:33 +0000184static BitsInit &getBitsField(const Record &def, StringRef str) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000185 BitsInit *bits = def.getValueAsBitsInit(str);
Owen Anderson4e818902011-02-18 21:51:29 +0000186 return *bits;
187}
188
Owen Anderson4e818902011-02-18 21:51:29 +0000189// Representation of the instruction to work on.
Owen Andersonc78e03c2011-07-19 21:06:00 +0000190typedef std::vector<bit_value_t> insn_t;
Owen Anderson4e818902011-02-18 21:51:29 +0000191
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000192namespace {
193
194class FilterChooser;
195
Owen Anderson4e818902011-02-18 21:51:29 +0000196/// Filter - Filter works with FilterChooser to produce the decoding tree for
197/// the ISA.
198///
199/// It is useful to think of a Filter as governing the switch stmts of the
200/// decoding tree in a certain level. Each case stmt delegates to an inferior
201/// FilterChooser to decide what further decoding logic to employ, or in another
202/// words, what other remaining bits to look at. The FilterChooser eventually
203/// chooses a best Filter to do its job.
204///
205/// This recursive scheme ends when the number of Opcodes assigned to the
206/// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
207/// the Filter/FilterChooser combo does not know how to distinguish among the
208/// Opcodes assigned.
209///
210/// An example of a conflict is
211///
212/// Conflict:
213/// 111101000.00........00010000....
214/// 111101000.00........0001........
215/// 1111010...00........0001........
216/// 1111010...00....................
217/// 1111010.........................
218/// 1111............................
219/// ................................
220/// VST4q8a 111101000_00________00010000____
221/// VST4q8b 111101000_00________00010000____
222///
223/// The Debug output shows the path that the decoding tree follows to reach the
224/// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
Petr Pavlu21894652015-07-14 08:00:34 +0000225/// even registers, while VST4q8b is a vst4 to double-spaced odd registers.
Owen Anderson4e818902011-02-18 21:51:29 +0000226///
227/// The encoding info in the .td files does not specify this meta information,
228/// which could have been used by the decoder to resolve the conflict. The
229/// decoder could try to decode the even/odd register numbering and assign to
230/// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
231/// version and return the Opcode since the two have the same Asm format string.
232class Filter {
233protected:
Craig Topper501d95c2012-03-16 06:52:56 +0000234 const FilterChooser *Owner;// points to the FilterChooser who owns this filter
Owen Anderson4e818902011-02-18 21:51:29 +0000235 unsigned StartBit; // the starting bit position
236 unsigned NumBits; // number of bits to filter
237 bool Mixed; // a mixed region contains both set and unset bits
238
239 // Map of well-known segment value to the set of uid's with that value.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000240 std::map<uint64_t, std::vector<unsigned>> FilteredInstructions;
Owen Anderson4e818902011-02-18 21:51:29 +0000241
242 // Set of uid's with non-constant segment values.
243 std::vector<unsigned> VariableInstructions;
244
245 // Map of well-known segment value to its delegate.
Craig Toppercf05f912014-09-03 06:07:54 +0000246 std::map<unsigned, std::unique_ptr<const FilterChooser>> FilterChooserMap;
Owen Anderson4e818902011-02-18 21:51:29 +0000247
248 // Number of instructions which fall under FilteredInstructions category.
249 unsigned NumFiltered;
250
251 // Keeps track of the last opcode in the filtered bucket.
252 unsigned LastOpcFiltered;
253
Owen Anderson4e818902011-02-18 21:51:29 +0000254public:
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000255 Filter(Filter &&f);
256 Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
257
258 ~Filter() = default;
259
Craig Topper48c112b2012-03-16 05:58:09 +0000260 unsigned getNumFiltered() const { return NumFiltered; }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000261
Craig Topper48c112b2012-03-16 05:58:09 +0000262 unsigned getSingletonOpc() const {
Owen Anderson4e818902011-02-18 21:51:29 +0000263 assert(NumFiltered == 1);
264 return LastOpcFiltered;
265 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000266
Owen Anderson4e818902011-02-18 21:51:29 +0000267 // Return the filter chooser for the group of instructions without constant
268 // segment values.
Craig Topper48c112b2012-03-16 05:58:09 +0000269 const FilterChooser &getVariableFC() const {
Owen Anderson4e818902011-02-18 21:51:29 +0000270 assert(NumFiltered == 1);
271 assert(FilterChooserMap.size() == 1);
272 return *(FilterChooserMap.find((unsigned)-1)->second);
273 }
274
Owen Anderson4e818902011-02-18 21:51:29 +0000275 // Divides the decoding task into sub tasks and delegates them to the
276 // inferior FilterChooser's.
277 //
278 // A special case arises when there's only one entry in the filtered
279 // instructions. In order to unambiguously decode the singleton, we need to
280 // match the remaining undecoded encoding bits against the singleton.
281 void recurse();
282
Jim Grosbachecaef492012-08-14 19:06:05 +0000283 // Emit table entries to decode instructions given a segment or segments of
284 // bits.
285 void emitTableEntry(DecoderTableInfo &TableInfo) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000286
287 // Returns the number of fanout produced by the filter. More fanout implies
288 // the filter distinguishes more categories of instructions.
289 unsigned usefulness() const;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000290}; // end class Filter
291
292} // end anonymous namespace
Owen Anderson4e818902011-02-18 21:51:29 +0000293
294// These are states of our finite state machines used in FilterChooser's
295// filterProcessor() which produces the filter candidates to use.
296typedef enum {
297 ATTR_NONE,
298 ATTR_FILTERED,
299 ATTR_ALL_SET,
300 ATTR_ALL_UNSET,
301 ATTR_MIXED
302} bitAttr_t;
303
304/// FilterChooser - FilterChooser chooses the best filter among a set of Filters
305/// in order to perform the decoding of instructions at the current level.
306///
307/// Decoding proceeds from the top down. Based on the well-known encoding bits
308/// of instructions available, FilterChooser builds up the possible Filters that
309/// can further the task of decoding by distinguishing among the remaining
310/// candidate instructions.
311///
312/// Once a filter has been chosen, it is called upon to divide the decoding task
313/// into sub-tasks and delegates them to its inferior FilterChoosers for further
314/// processings.
315///
316/// It is useful to think of a Filter as governing the switch stmts of the
317/// decoding tree. And each case is delegated to an inferior FilterChooser to
318/// decide what further remaining bits to look at.
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000319namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000320
Owen Anderson4e818902011-02-18 21:51:29 +0000321class FilterChooser {
322protected:
323 friend class Filter;
324
325 // Vector of codegen instructions to choose our filter.
Craig Topperf9265322016-01-17 20:38:14 +0000326 ArrayRef<const CodeGenInstruction *> AllInstructions;
Owen Anderson4e818902011-02-18 21:51:29 +0000327
328 // Vector of uid's for this filter chooser to work on.
Craig Topper501d95c2012-03-16 06:52:56 +0000329 const std::vector<unsigned> &Opcodes;
Owen Anderson4e818902011-02-18 21:51:29 +0000330
331 // Lookup table for the operand decoding of instructions.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000332 const std::map<unsigned, std::vector<OperandInfo>> &Operands;
Owen Anderson4e818902011-02-18 21:51:29 +0000333
334 // Vector of candidate filters.
335 std::vector<Filter> Filters;
336
337 // Array of bit values passed down from our parent.
338 // Set to all BIT_UNFILTERED's for Parent == NULL.
Owen Andersonc78e03c2011-07-19 21:06:00 +0000339 std::vector<bit_value_t> FilterBitValues;
Owen Anderson4e818902011-02-18 21:51:29 +0000340
341 // Links to the FilterChooser above us in the decoding tree.
Craig Topper501d95c2012-03-16 06:52:56 +0000342 const FilterChooser *Parent;
Owen Anderson4e818902011-02-18 21:51:29 +0000343
344 // Index of the best filter from Filters.
345 int BestIndex;
346
Owen Andersonc78e03c2011-07-19 21:06:00 +0000347 // Width of instructions
348 unsigned BitWidth;
349
Owen Andersona4043c42011-08-17 17:44:15 +0000350 // Parent emitter
351 const FixedLenDecoderEmitter *Emitter;
352
Owen Anderson4e818902011-02-18 21:51:29 +0000353public:
Craig Topperf9265322016-01-17 20:38:14 +0000354 FilterChooser(ArrayRef<const CodeGenInstruction *> Insts,
Owen Anderson4e818902011-02-18 21:51:29 +0000355 const std::vector<unsigned> &IDs,
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000356 const std::map<unsigned, std::vector<OperandInfo>> &Ops,
Owen Andersona4043c42011-08-17 17:44:15 +0000357 unsigned BW,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000358 const FixedLenDecoderEmitter *E)
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000359 : AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
Craig Topper1ddc2882014-09-04 04:49:03 +0000360 FilterBitValues(BW, BIT_UNFILTERED), Parent(nullptr), BestIndex(-1),
361 BitWidth(BW), Emitter(E) {
Owen Anderson4e818902011-02-18 21:51:29 +0000362 doFilter();
363 }
364
Craig Topperf9265322016-01-17 20:38:14 +0000365 FilterChooser(ArrayRef<const CodeGenInstruction *> Insts,
Owen Anderson4e818902011-02-18 21:51:29 +0000366 const std::vector<unsigned> &IDs,
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000367 const std::map<unsigned, std::vector<OperandInfo>> &Ops,
Craig Topper501d95c2012-03-16 06:52:56 +0000368 const std::vector<bit_value_t> &ParentFilterBitValues,
369 const FilterChooser &parent)
Craig Topper82d0d5f2012-03-16 01:19:24 +0000370 : AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000371 FilterBitValues(ParentFilterBitValues), Parent(&parent), BestIndex(-1),
372 BitWidth(parent.BitWidth), Emitter(parent.Emitter) {
Owen Anderson4e818902011-02-18 21:51:29 +0000373 doFilter();
374 }
375
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000376 FilterChooser(const FilterChooser &) = delete;
377 void operator=(const FilterChooser &) = delete;
378
Jim Grosbachecaef492012-08-14 19:06:05 +0000379 unsigned getBitWidth() const { return BitWidth; }
Owen Anderson4e818902011-02-18 21:51:29 +0000380
381protected:
382 // Populates the insn given the uid.
383 void insnWithID(insn_t &Insn, unsigned Opcode) const {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000384 BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
Owen Anderson4e818902011-02-18 21:51:29 +0000385
James Molloyd9ba4fd2012-02-09 10:56:31 +0000386 // We may have a SoftFail bitmask, which specifies a mask where an encoding
387 // may differ from the value in "Inst" and yet still be valid, but the
388 // disassembler should return SoftFail instead of Success.
389 //
390 // This is used for marking UNPREDICTABLE instructions in the ARM world.
Jim Grosbach3f4b2392012-02-29 22:07:56 +0000391 BitsInit *SFBits =
392 AllInstructions[Opcode]->TheDef->getValueAsBitsInit("SoftFail");
James Molloyd9ba4fd2012-02-09 10:56:31 +0000393
394 for (unsigned i = 0; i < BitWidth; ++i) {
395 if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE)
396 Insn.push_back(BIT_UNSET);
397 else
398 Insn.push_back(bitFromBits(Bits, i));
399 }
Owen Anderson4e818902011-02-18 21:51:29 +0000400 }
401
402 // Returns the record name.
Matthias Braun4a86d452016-12-04 05:48:16 +0000403 const StringRef nameWithID(unsigned Opcode) const {
Owen Anderson4e818902011-02-18 21:51:29 +0000404 return AllInstructions[Opcode]->TheDef->getName();
405 }
406
407 // Populates the field of the insn given the start position and the number of
408 // consecutive bits to scan for.
409 //
410 // Returns false if there exists any uninitialized bit value in the range.
411 // Returns true, otherwise.
412 bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000413 unsigned NumBits) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000414
415 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
416 /// filter array as a series of chars.
Craig Topper48c112b2012-03-16 05:58:09 +0000417 void dumpFilterArray(raw_ostream &o,
418 const std::vector<bit_value_t> & filter) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000419
420 /// dumpStack - dumpStack traverses the filter chooser chain and calls
421 /// dumpFilterArray on each filter chooser up to the top level one.
Craig Topper48c112b2012-03-16 05:58:09 +0000422 void dumpStack(raw_ostream &o, const char *prefix) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000423
424 Filter &bestFilter() {
425 assert(BestIndex != -1 && "BestIndex not set");
426 return Filters[BestIndex];
427 }
428
Craig Topper48c112b2012-03-16 05:58:09 +0000429 bool PositionFiltered(unsigned i) const {
Owen Anderson4e818902011-02-18 21:51:29 +0000430 return ValueSet(FilterBitValues[i]);
431 }
432
433 // Calculates the island(s) needed to decode the instruction.
434 // This returns a lit of undecoded bits of an instructions, for example,
435 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
436 // decoded bits in order to verify that the instruction matches the Opcode.
437 unsigned getIslands(std::vector<unsigned> &StartBits,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000438 std::vector<unsigned> &EndBits,
Craig Topper48c112b2012-03-16 05:58:09 +0000439 std::vector<uint64_t> &FieldVals,
440 const insn_t &Insn) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000441
James Molloy8067df92011-09-07 19:42:28 +0000442 // Emits code to check the Predicates member of an instruction are true.
443 // Returns true if predicate matches were emitted, false otherwise.
Craig Topper48c112b2012-03-16 05:58:09 +0000444 bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
445 unsigned Opc) const;
James Molloy8067df92011-09-07 19:42:28 +0000446
Jim Grosbachecaef492012-08-14 19:06:05 +0000447 bool doesOpcodeNeedPredicate(unsigned Opc) const;
448 unsigned getPredicateIndex(DecoderTableInfo &TableInfo, StringRef P) const;
449 void emitPredicateTableEntry(DecoderTableInfo &TableInfo,
450 unsigned Opc) const;
James Molloyd9ba4fd2012-02-09 10:56:31 +0000451
Jim Grosbachecaef492012-08-14 19:06:05 +0000452 void emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
453 unsigned Opc) const;
454
455 // Emits table entries to decode the singleton.
456 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
457 unsigned Opc) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000458
459 // Emits code to decode the singleton, and then to decode the rest.
Jim Grosbachecaef492012-08-14 19:06:05 +0000460 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
461 const Filter &Best) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000462
Jim Grosbachecaef492012-08-14 19:06:05 +0000463 void emitBinaryParser(raw_ostream &o, unsigned &Indentation,
Petr Pavlu182b0572015-07-15 08:04:27 +0000464 const OperandInfo &OpInfo,
465 bool &OpHasCompleteDecoder) const;
Owen Andersone3591652011-07-28 21:54:31 +0000466
Petr Pavlu182b0572015-07-15 08:04:27 +0000467 void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc,
468 bool &HasCompleteDecoder) const;
469 unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc,
470 bool &HasCompleteDecoder) const;
Jim Grosbachecaef492012-08-14 19:06:05 +0000471
Owen Anderson4e818902011-02-18 21:51:29 +0000472 // Assign a single filter and run with it.
Craig Topper48c112b2012-03-16 05:58:09 +0000473 void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed);
Owen Anderson4e818902011-02-18 21:51:29 +0000474
475 // reportRegion is a helper function for filterProcessor to mark a region as
476 // eligible for use as a filter region.
477 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000478 bool AllowMixed);
Owen Anderson4e818902011-02-18 21:51:29 +0000479
480 // FilterProcessor scans the well-known encoding bits of the instructions and
481 // builds up a list of candidate filters. It chooses the best filter and
482 // recursively descends down the decoding tree.
483 bool filterProcessor(bool AllowMixed, bool Greedy = true);
484
485 // Decides on the best configuration of filter(s) to use in order to decode
486 // the instructions. A conflict of instructions may occur, in which case we
487 // dump the conflict set to the standard error.
488 void doFilter();
489
Jim Grosbachecaef492012-08-14 19:06:05 +0000490public:
491 // emitTableEntries - Emit state machine entries to decode our share of
492 // instructions.
493 void emitTableEntries(DecoderTableInfo &TableInfo) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000494};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000495
496} // end anonymous namespace
Owen Anderson4e818902011-02-18 21:51:29 +0000497
498///////////////////////////
499// //
Craig Topper93e64342012-03-16 00:56:01 +0000500// Filter Implementation //
Owen Anderson4e818902011-02-18 21:51:29 +0000501// //
502///////////////////////////
503
Craig Topper5c2b4ac2014-09-03 05:49:07 +0000504Filter::Filter(Filter &&f)
Craig Topper82d0d5f2012-03-16 01:19:24 +0000505 : Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
Craig Topper5c2b4ac2014-09-03 05:49:07 +0000506 FilteredInstructions(std::move(f.FilteredInstructions)),
507 VariableInstructions(std::move(f.VariableInstructions)),
508 FilterChooserMap(std::move(f.FilterChooserMap)), NumFiltered(f.NumFiltered),
Craig Topper82d0d5f2012-03-16 01:19:24 +0000509 LastOpcFiltered(f.LastOpcFiltered) {
Owen Anderson4e818902011-02-18 21:51:29 +0000510}
511
512Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000513 bool mixed)
514 : Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) {
Owen Andersonc78e03c2011-07-19 21:06:00 +0000515 assert(StartBit + NumBits - 1 < Owner->BitWidth);
Owen Anderson4e818902011-02-18 21:51:29 +0000516
517 NumFiltered = 0;
518 LastOpcFiltered = 0;
Owen Anderson4e818902011-02-18 21:51:29 +0000519
520 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
521 insn_t Insn;
522
523 // Populates the insn given the uid.
524 Owner->insnWithID(Insn, Owner->Opcodes[i]);
525
526 uint64_t Field;
527 // Scans the segment for possibly well-specified encoding bits.
528 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
529
530 if (ok) {
531 // The encoding bits are well-known. Lets add the uid of the
532 // instruction into the bucket keyed off the constant field value.
533 LastOpcFiltered = Owner->Opcodes[i];
534 FilteredInstructions[Field].push_back(LastOpcFiltered);
535 ++NumFiltered;
536 } else {
Craig Topper93e64342012-03-16 00:56:01 +0000537 // Some of the encoding bit(s) are unspecified. This contributes to
Owen Anderson4e818902011-02-18 21:51:29 +0000538 // one additional member of "Variable" instructions.
539 VariableInstructions.push_back(Owner->Opcodes[i]);
Owen Anderson4e818902011-02-18 21:51:29 +0000540 }
541 }
542
543 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
544 && "Filter returns no instruction categories");
545}
546
Owen Anderson4e818902011-02-18 21:51:29 +0000547// Divides the decoding task into sub tasks and delegates them to the
548// inferior FilterChooser's.
549//
550// A special case arises when there's only one entry in the filtered
551// instructions. In order to unambiguously decode the singleton, we need to
552// match the remaining undecoded encoding bits against the singleton.
553void Filter::recurse() {
Owen Anderson4e818902011-02-18 21:51:29 +0000554 // Starts by inheriting our parent filter chooser's filter bit values.
Owen Andersonc78e03c2011-07-19 21:06:00 +0000555 std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
Owen Anderson4e818902011-02-18 21:51:29 +0000556
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000557 if (!VariableInstructions.empty()) {
Owen Anderson4e818902011-02-18 21:51:29 +0000558 // Conservatively marks each segment position as BIT_UNSET.
Craig Topper29688ab2012-08-17 05:42:16 +0000559 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex)
Owen Anderson4e818902011-02-18 21:51:29 +0000560 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
561
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000562 // Delegates to an inferior filter chooser for further processing on this
Owen Anderson4e818902011-02-18 21:51:29 +0000563 // group of instructions whose segment values are variable.
Yaron Kerene499db02014-09-03 08:22:30 +0000564 FilterChooserMap.insert(
565 std::make_pair(-1U, llvm::make_unique<FilterChooser>(
566 Owner->AllInstructions, VariableInstructions,
567 Owner->Operands, BitValueArray, *Owner)));
Owen Anderson4e818902011-02-18 21:51:29 +0000568 }
569
570 // No need to recurse for a singleton filtered instruction.
Jim Grosbachecaef492012-08-14 19:06:05 +0000571 // See also Filter::emit*().
Owen Anderson4e818902011-02-18 21:51:29 +0000572 if (getNumFiltered() == 1) {
Owen Anderson4e818902011-02-18 21:51:29 +0000573 assert(FilterChooserMap.size() == 1);
574 return;
575 }
576
577 // Otherwise, create sub choosers.
Craig Topper1f7604d2014-12-13 05:12:19 +0000578 for (const auto &Inst : FilteredInstructions) {
Owen Anderson4e818902011-02-18 21:51:29 +0000579
580 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
Craig Topper29688ab2012-08-17 05:42:16 +0000581 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) {
Craig Topper1f7604d2014-12-13 05:12:19 +0000582 if (Inst.first & (1ULL << bitIndex))
Owen Anderson4e818902011-02-18 21:51:29 +0000583 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
584 else
585 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
586 }
587
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000588 // Delegates to an inferior filter chooser for further processing on this
Owen Anderson4e818902011-02-18 21:51:29 +0000589 // category of instructions.
Craig Toppercf05f912014-09-03 06:07:54 +0000590 FilterChooserMap.insert(std::make_pair(
Craig Topper1f7604d2014-12-13 05:12:19 +0000591 Inst.first, llvm::make_unique<FilterChooser>(
592 Owner->AllInstructions, Inst.second,
Yaron Kerene499db02014-09-03 08:22:30 +0000593 Owner->Operands, BitValueArray, *Owner)));
Owen Anderson4e818902011-02-18 21:51:29 +0000594 }
595}
596
Jim Grosbachecaef492012-08-14 19:06:05 +0000597static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
598 uint32_t DestIdx) {
599 // Any NumToSkip fixups in the current scope can resolve to the
600 // current location.
601 for (FixupList::const_reverse_iterator I = Fixups.rbegin(),
602 E = Fixups.rend();
603 I != E; ++I) {
604 // Calculate the distance from the byte following the fixup entry byte
605 // to the destination. The Target is calculated from after the 16-bit
606 // NumToSkip entry itself, so subtract two from the displacement here
607 // to account for that.
608 uint32_t FixupIdx = *I;
Sander de Smalen13f94252018-07-05 10:39:15 +0000609 uint32_t Delta = DestIdx - FixupIdx - 3;
610 // Our NumToSkip entries are 24-bits. Make sure our table isn't too
Jim Grosbachecaef492012-08-14 19:06:05 +0000611 // big.
Sander de Smalen13f94252018-07-05 10:39:15 +0000612 assert(Delta < (1u << 24));
Jim Grosbachecaef492012-08-14 19:06:05 +0000613 Table[FixupIdx] = (uint8_t)Delta;
614 Table[FixupIdx + 1] = (uint8_t)(Delta >> 8);
Sander de Smalen13f94252018-07-05 10:39:15 +0000615 Table[FixupIdx + 2] = (uint8_t)(Delta >> 16);
Jim Grosbachecaef492012-08-14 19:06:05 +0000616 }
617}
Owen Anderson4e818902011-02-18 21:51:29 +0000618
Jim Grosbachecaef492012-08-14 19:06:05 +0000619// Emit table entries to decode instructions given a segment or segments
620// of bits.
621void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
622 TableInfo.Table.push_back(MCD::OPC_ExtractField);
623 TableInfo.Table.push_back(StartBit);
624 TableInfo.Table.push_back(NumBits);
Owen Anderson4e818902011-02-18 21:51:29 +0000625
Jim Grosbachecaef492012-08-14 19:06:05 +0000626 // A new filter entry begins a new scope for fixup resolution.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000627 TableInfo.FixupStack.emplace_back();
Owen Anderson4e818902011-02-18 21:51:29 +0000628
Jim Grosbachecaef492012-08-14 19:06:05 +0000629 DecoderTable &Table = TableInfo.Table;
630
631 size_t PrevFilter = 0;
632 bool HasFallthrough = false;
Craig Topper1f7604d2014-12-13 05:12:19 +0000633 for (auto &Filter : FilterChooserMap) {
Owen Anderson4e818902011-02-18 21:51:29 +0000634 // Field value -1 implies a non-empty set of variable instructions.
635 // See also recurse().
Craig Topper1f7604d2014-12-13 05:12:19 +0000636 if (Filter.first == (unsigned)-1) {
Jim Grosbachecaef492012-08-14 19:06:05 +0000637 HasFallthrough = true;
Owen Anderson4e818902011-02-18 21:51:29 +0000638
Jim Grosbachecaef492012-08-14 19:06:05 +0000639 // Each scope should always have at least one filter value to check
640 // for.
641 assert(PrevFilter != 0 && "empty filter set!");
642 FixupList &CurScope = TableInfo.FixupStack.back();
643 // Resolve any NumToSkip fixups in the current scope.
644 resolveTableFixups(Table, CurScope, Table.size());
645 CurScope.clear();
646 PrevFilter = 0; // Don't re-process the filter's fallthrough.
647 } else {
648 Table.push_back(MCD::OPC_FilterValue);
649 // Encode and emit the value to filter against.
Sander de Smalen13f94252018-07-05 10:39:15 +0000650 uint8_t Buffer[16];
Craig Topper1f7604d2014-12-13 05:12:19 +0000651 unsigned Len = encodeULEB128(Filter.first, Buffer);
Jim Grosbachecaef492012-08-14 19:06:05 +0000652 Table.insert(Table.end(), Buffer, Buffer + Len);
653 // Reserve space for the NumToSkip entry. We'll backpatch the value
654 // later.
655 PrevFilter = Table.size();
656 Table.push_back(0);
657 Table.push_back(0);
Sander de Smalen13f94252018-07-05 10:39:15 +0000658 Table.push_back(0);
Jim Grosbachecaef492012-08-14 19:06:05 +0000659 }
Owen Anderson4e818902011-02-18 21:51:29 +0000660
661 // We arrive at a category of instructions with the same segment value.
662 // Now delegate to the sub filter chooser for further decodings.
663 // The case may fallthrough, which happens if the remaining well-known
664 // encoding bits do not match exactly.
Craig Topper1f7604d2014-12-13 05:12:19 +0000665 Filter.second->emitTableEntries(TableInfo);
Owen Anderson4e818902011-02-18 21:51:29 +0000666
Jim Grosbachecaef492012-08-14 19:06:05 +0000667 // Now that we've emitted the body of the handler, update the NumToSkip
668 // of the filter itself to be able to skip forward when false. Subtract
669 // two as to account for the width of the NumToSkip field itself.
670 if (PrevFilter) {
Sander de Smalen13f94252018-07-05 10:39:15 +0000671 uint32_t NumToSkip = Table.size() - PrevFilter - 3;
672 assert(NumToSkip < (1u << 24) && "disassembler decoding table too large!");
Jim Grosbachecaef492012-08-14 19:06:05 +0000673 Table[PrevFilter] = (uint8_t)NumToSkip;
674 Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8);
Sander de Smalen13f94252018-07-05 10:39:15 +0000675 Table[PrevFilter + 2] = (uint8_t)(NumToSkip >> 16);
Jim Grosbachecaef492012-08-14 19:06:05 +0000676 }
Owen Anderson4e818902011-02-18 21:51:29 +0000677 }
678
Jim Grosbachecaef492012-08-14 19:06:05 +0000679 // Any remaining unresolved fixups bubble up to the parent fixup scope.
680 assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!");
681 FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1;
682 FixupScopeList::iterator Dest = Source - 1;
683 Dest->insert(Dest->end(), Source->begin(), Source->end());
684 TableInfo.FixupStack.pop_back();
685
686 // If there is no fallthrough, then the final filter should get fixed
687 // up according to the enclosing scope rather than the current position.
688 if (!HasFallthrough)
689 TableInfo.FixupStack.back().push_back(PrevFilter);
Owen Anderson4e818902011-02-18 21:51:29 +0000690}
691
692// Returns the number of fanout produced by the filter. More fanout implies
693// the filter distinguishes more categories of instructions.
694unsigned Filter::usefulness() const {
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000695 if (!VariableInstructions.empty())
Owen Anderson4e818902011-02-18 21:51:29 +0000696 return FilteredInstructions.size();
697 else
698 return FilteredInstructions.size() + 1;
699}
700
701//////////////////////////////////
702// //
703// Filterchooser Implementation //
704// //
705//////////////////////////////////
706
Jim Grosbachecaef492012-08-14 19:06:05 +0000707// Emit the decoder state machine table.
708void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
709 DecoderTable &Table,
710 unsigned Indentation,
711 unsigned BitWidth,
712 StringRef Namespace) const {
713 OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace
714 << BitWidth << "[] = {\n";
Owen Anderson4e818902011-02-18 21:51:29 +0000715
Jim Grosbachecaef492012-08-14 19:06:05 +0000716 Indentation += 2;
Owen Anderson4e818902011-02-18 21:51:29 +0000717
Jim Grosbachecaef492012-08-14 19:06:05 +0000718 // FIXME: We may be able to use the NumToSkip values to recover
719 // appropriate indentation levels.
720 DecoderTable::const_iterator I = Table.begin();
721 DecoderTable::const_iterator E = Table.end();
722 while (I != E) {
723 assert (I < E && "incomplete decode table entry!");
Owen Anderson4e818902011-02-18 21:51:29 +0000724
Jim Grosbachecaef492012-08-14 19:06:05 +0000725 uint64_t Pos = I - Table.begin();
726 OS << "/* " << Pos << " */";
727 OS.PadToColumn(12);
Owen Anderson4e818902011-02-18 21:51:29 +0000728
Jim Grosbachecaef492012-08-14 19:06:05 +0000729 switch (*I) {
730 default:
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000731 PrintFatalError("invalid decode table opcode");
Jim Grosbachecaef492012-08-14 19:06:05 +0000732 case MCD::OPC_ExtractField: {
733 ++I;
734 unsigned Start = *I++;
735 unsigned Len = *I++;
736 OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
737 << Len << ", // Inst{";
738 if (Len > 1)
739 OS << (Start + Len - 1) << "-";
740 OS << Start << "} ...\n";
741 break;
742 }
743 case MCD::OPC_FilterValue: {
744 ++I;
745 OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
746 // The filter value is ULEB128 encoded.
747 while (*I >= 128)
Craig Topper429093a2016-01-31 01:55:15 +0000748 OS << (unsigned)*I++ << ", ";
749 OS << (unsigned)*I++ << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000750
Sander de Smalen13f94252018-07-05 10:39:15 +0000751 // 24-bit numtoskip value.
Jim Grosbachecaef492012-08-14 19:06:05 +0000752 uint8_t Byte = *I++;
753 uint32_t NumToSkip = Byte;
Craig Topper429093a2016-01-31 01:55:15 +0000754 OS << (unsigned)Byte << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000755 Byte = *I++;
Craig Topper429093a2016-01-31 01:55:15 +0000756 OS << (unsigned)Byte << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000757 NumToSkip |= Byte << 8;
Sander de Smalen13f94252018-07-05 10:39:15 +0000758 Byte = *I++;
759 OS << utostr(Byte) << ", ";
760 NumToSkip |= Byte << 16;
Jim Grosbachecaef492012-08-14 19:06:05 +0000761 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
762 break;
763 }
764 case MCD::OPC_CheckField: {
765 ++I;
766 unsigned Start = *I++;
767 unsigned Len = *I++;
768 OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
769 << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
770 // ULEB128 encoded field value.
771 for (; *I >= 128; ++I)
Craig Topper429093a2016-01-31 01:55:15 +0000772 OS << (unsigned)*I << ", ";
773 OS << (unsigned)*I++ << ", ";
Sander de Smalen13f94252018-07-05 10:39:15 +0000774 // 24-bit numtoskip value.
Jim Grosbachecaef492012-08-14 19:06:05 +0000775 uint8_t Byte = *I++;
776 uint32_t NumToSkip = Byte;
Craig Topper429093a2016-01-31 01:55:15 +0000777 OS << (unsigned)Byte << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000778 Byte = *I++;
Craig Topper429093a2016-01-31 01:55:15 +0000779 OS << (unsigned)Byte << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000780 NumToSkip |= Byte << 8;
Sander de Smalen13f94252018-07-05 10:39:15 +0000781 Byte = *I++;
782 OS << utostr(Byte) << ", ";
783 NumToSkip |= Byte << 16;
Jim Grosbachecaef492012-08-14 19:06:05 +0000784 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
785 break;
786 }
787 case MCD::OPC_CheckPredicate: {
788 ++I;
789 OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
790 for (; *I >= 128; ++I)
Craig Topper429093a2016-01-31 01:55:15 +0000791 OS << (unsigned)*I << ", ";
792 OS << (unsigned)*I++ << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000793
Sander de Smalen13f94252018-07-05 10:39:15 +0000794 // 24-bit numtoskip value.
Jim Grosbachecaef492012-08-14 19:06:05 +0000795 uint8_t Byte = *I++;
796 uint32_t NumToSkip = Byte;
Craig Topper429093a2016-01-31 01:55:15 +0000797 OS << (unsigned)Byte << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000798 Byte = *I++;
Craig Topper429093a2016-01-31 01:55:15 +0000799 OS << (unsigned)Byte << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000800 NumToSkip |= Byte << 8;
Sander de Smalen13f94252018-07-05 10:39:15 +0000801 Byte = *I++;
802 OS << utostr(Byte) << ", ";
803 NumToSkip |= Byte << 16;
Jim Grosbachecaef492012-08-14 19:06:05 +0000804 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
805 break;
806 }
Petr Pavlu182b0572015-07-15 08:04:27 +0000807 case MCD::OPC_Decode:
808 case MCD::OPC_TryDecode: {
809 bool IsTry = *I == MCD::OPC_TryDecode;
Jim Grosbachecaef492012-08-14 19:06:05 +0000810 ++I;
811 // Extract the ULEB128 encoded Opcode to a buffer.
Sander de Smalen13f94252018-07-05 10:39:15 +0000812 uint8_t Buffer[16], *p = Buffer;
Jim Grosbachecaef492012-08-14 19:06:05 +0000813 while ((*p++ = *I++) >= 128)
814 assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer)
815 && "ULEB128 value too large!");
816 // Decode the Opcode value.
817 unsigned Opc = decodeULEB128(Buffer);
Petr Pavlu182b0572015-07-15 08:04:27 +0000818 OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
819 << "Decode, ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000820 for (p = Buffer; *p >= 128; ++p)
Craig Topper429093a2016-01-31 01:55:15 +0000821 OS << (unsigned)*p << ", ";
822 OS << (unsigned)*p << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000823
824 // Decoder index.
825 for (; *I >= 128; ++I)
Craig Topper429093a2016-01-31 01:55:15 +0000826 OS << (unsigned)*I << ", ";
827 OS << (unsigned)*I++ << ", ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000828
Petr Pavlu182b0572015-07-15 08:04:27 +0000829 if (!IsTry) {
830 OS << "// Opcode: "
Craig Topperf9265322016-01-17 20:38:14 +0000831 << NumberedInstructions[Opc]->TheDef->getName() << "\n";
Petr Pavlu182b0572015-07-15 08:04:27 +0000832 break;
833 }
834
835 // Fallthrough for OPC_TryDecode.
836
Sander de Smalen13f94252018-07-05 10:39:15 +0000837 // 24-bit numtoskip value.
Petr Pavlu182b0572015-07-15 08:04:27 +0000838 uint8_t Byte = *I++;
839 uint32_t NumToSkip = Byte;
Craig Topper429093a2016-01-31 01:55:15 +0000840 OS << (unsigned)Byte << ", ";
Petr Pavlu182b0572015-07-15 08:04:27 +0000841 Byte = *I++;
Craig Topper429093a2016-01-31 01:55:15 +0000842 OS << (unsigned)Byte << ", ";
Petr Pavlu182b0572015-07-15 08:04:27 +0000843 NumToSkip |= Byte << 8;
Sander de Smalen13f94252018-07-05 10:39:15 +0000844 Byte = *I++;
845 OS << utostr(Byte) << ", ";
846 NumToSkip |= Byte << 16;
Petr Pavlu182b0572015-07-15 08:04:27 +0000847
Jim Grosbachecaef492012-08-14 19:06:05 +0000848 OS << "// Opcode: "
Craig Topperf9265322016-01-17 20:38:14 +0000849 << NumberedInstructions[Opc]->TheDef->getName()
Petr Pavlu182b0572015-07-15 08:04:27 +0000850 << ", skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000851 break;
852 }
853 case MCD::OPC_SoftFail: {
854 ++I;
855 OS.indent(Indentation) << "MCD::OPC_SoftFail";
856 // Positive mask
857 uint64_t Value = 0;
858 unsigned Shift = 0;
859 do {
Craig Topper429093a2016-01-31 01:55:15 +0000860 OS << ", " << (unsigned)*I;
Jim Grosbachecaef492012-08-14 19:06:05 +0000861 Value += (*I & 0x7f) << Shift;
862 Shift += 7;
863 } while (*I++ >= 128);
Craig Topper429093a2016-01-31 01:55:15 +0000864 if (Value > 127) {
865 OS << " /* 0x";
866 OS.write_hex(Value);
867 OS << " */";
868 }
Jim Grosbachecaef492012-08-14 19:06:05 +0000869 // Negative mask
870 Value = 0;
871 Shift = 0;
872 do {
Craig Topper429093a2016-01-31 01:55:15 +0000873 OS << ", " << (unsigned)*I;
Jim Grosbachecaef492012-08-14 19:06:05 +0000874 Value += (*I & 0x7f) << Shift;
875 Shift += 7;
876 } while (*I++ >= 128);
Craig Topper429093a2016-01-31 01:55:15 +0000877 if (Value > 127) {
878 OS << " /* 0x";
879 OS.write_hex(Value);
880 OS << " */";
881 }
Jim Grosbachecaef492012-08-14 19:06:05 +0000882 OS << ",\n";
883 break;
884 }
885 case MCD::OPC_Fail: {
886 ++I;
887 OS.indent(Indentation) << "MCD::OPC_Fail,\n";
888 break;
889 }
890 }
891 }
892 OS.indent(Indentation) << "0\n";
893
894 Indentation -= 2;
895
896 OS.indent(Indentation) << "};\n\n";
897}
898
899void FixedLenDecoderEmitter::
900emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
901 unsigned Indentation) const {
902 // The predicate function is just a big switch statement based on the
903 // input predicate index.
904 OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000905 << "const FeatureBitset& Bits) {\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000906 Indentation += 2;
Aaron Ballmane59e3582013-07-15 16:53:32 +0000907 if (!Predicates.empty()) {
908 OS.indent(Indentation) << "switch (Idx) {\n";
909 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
910 unsigned Index = 0;
Craig Topper1f7604d2014-12-13 05:12:19 +0000911 for (const auto &Predicate : Predicates) {
912 OS.indent(Indentation) << "case " << Index++ << ":\n";
913 OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
Aaron Ballmane59e3582013-07-15 16:53:32 +0000914 }
915 OS.indent(Indentation) << "}\n";
916 } else {
917 // No case statement to emit
918 OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000919 }
Jim Grosbachecaef492012-08-14 19:06:05 +0000920 Indentation -= 2;
921 OS.indent(Indentation) << "}\n\n";
922}
923
924void FixedLenDecoderEmitter::
925emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
926 unsigned Indentation) const {
927 // The decoder function is just a big switch statement based on the
928 // input decoder index.
929 OS.indent(Indentation) << "template<typename InsnType>\n";
930 OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
931 << " unsigned Idx, InsnType insn, MCInst &MI,\n";
932 OS.indent(Indentation) << " uint64_t "
Petr Pavlu182b0572015-07-15 08:04:27 +0000933 << "Address, const void *Decoder, bool &DecodeComplete) {\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000934 Indentation += 2;
Petr Pavlu182b0572015-07-15 08:04:27 +0000935 OS.indent(Indentation) << "DecodeComplete = true;\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000936 OS.indent(Indentation) << "InsnType tmp;\n";
937 OS.indent(Indentation) << "switch (Idx) {\n";
938 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
939 unsigned Index = 0;
Craig Topper1f7604d2014-12-13 05:12:19 +0000940 for (const auto &Decoder : Decoders) {
941 OS.indent(Indentation) << "case " << Index++ << ":\n";
942 OS << Decoder;
Jim Grosbachecaef492012-08-14 19:06:05 +0000943 OS.indent(Indentation+2) << "return S;\n";
944 }
945 OS.indent(Indentation) << "}\n";
946 Indentation -= 2;
947 OS.indent(Indentation) << "}\n\n";
Owen Anderson4e818902011-02-18 21:51:29 +0000948}
949
950// Populates the field of the insn given the start position and the number of
951// consecutive bits to scan for.
952//
953// Returns false if and on the first uninitialized bit value encountered.
954// Returns true, otherwise.
955bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
Craig Topper48c112b2012-03-16 05:58:09 +0000956 unsigned StartBit, unsigned NumBits) const {
Owen Anderson4e818902011-02-18 21:51:29 +0000957 Field = 0;
958
959 for (unsigned i = 0; i < NumBits; ++i) {
960 if (Insn[StartBit + i] == BIT_UNSET)
961 return false;
962
963 if (Insn[StartBit + i] == BIT_TRUE)
964 Field = Field | (1ULL << i);
965 }
966
967 return true;
968}
969
970/// dumpFilterArray - dumpFilterArray prints out debugging info for the given
971/// filter array as a series of chars.
972void FilterChooser::dumpFilterArray(raw_ostream &o,
Craig Topper48c112b2012-03-16 05:58:09 +0000973 const std::vector<bit_value_t> &filter) const {
Craig Topper29688ab2012-08-17 05:42:16 +0000974 for (unsigned bitIndex = BitWidth; bitIndex > 0; bitIndex--) {
Owen Anderson4e818902011-02-18 21:51:29 +0000975 switch (filter[bitIndex - 1]) {
976 case BIT_UNFILTERED:
977 o << ".";
978 break;
979 case BIT_UNSET:
980 o << "_";
981 break;
982 case BIT_TRUE:
983 o << "1";
984 break;
985 case BIT_FALSE:
986 o << "0";
987 break;
988 }
989 }
990}
991
992/// dumpStack - dumpStack traverses the filter chooser chain and calls
993/// dumpFilterArray on each filter chooser up to the top level one.
Craig Topper48c112b2012-03-16 05:58:09 +0000994void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const {
995 const FilterChooser *current = this;
Owen Anderson4e818902011-02-18 21:51:29 +0000996
997 while (current) {
998 o << prefix;
999 dumpFilterArray(o, current->FilterBitValues);
1000 o << '\n';
1001 current = current->Parent;
1002 }
1003}
1004
Owen Anderson4e818902011-02-18 21:51:29 +00001005// Calculates the island(s) needed to decode the instruction.
1006// This returns a list of undecoded bits of an instructions, for example,
1007// Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
1008// decoded bits in order to verify that the instruction matches the Opcode.
1009unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
Craig Topper82d0d5f2012-03-16 01:19:24 +00001010 std::vector<unsigned> &EndBits,
1011 std::vector<uint64_t> &FieldVals,
Craig Topper48c112b2012-03-16 05:58:09 +00001012 const insn_t &Insn) const {
Owen Anderson4e818902011-02-18 21:51:29 +00001013 unsigned Num, BitNo;
1014 Num = BitNo = 0;
1015
1016 uint64_t FieldVal = 0;
1017
1018 // 0: Init
1019 // 1: Water (the bit value does not affect decoding)
1020 // 2: Island (well-known bit value needed for decoding)
1021 int State = 0;
1022 int Val = -1;
1023
Owen Andersonc78e03c2011-07-19 21:06:00 +00001024 for (unsigned i = 0; i < BitWidth; ++i) {
Owen Anderson4e818902011-02-18 21:51:29 +00001025 Val = Value(Insn[i]);
1026 bool Filtered = PositionFiltered(i);
1027 switch (State) {
Craig Topperc4965bc2012-02-05 07:21:30 +00001028 default: llvm_unreachable("Unreachable code!");
Owen Anderson4e818902011-02-18 21:51:29 +00001029 case 0:
1030 case 1:
1031 if (Filtered || Val == -1)
1032 State = 1; // Still in Water
1033 else {
1034 State = 2; // Into the Island
1035 BitNo = 0;
1036 StartBits.push_back(i);
1037 FieldVal = Val;
1038 }
1039 break;
1040 case 2:
1041 if (Filtered || Val == -1) {
1042 State = 1; // Into the Water
1043 EndBits.push_back(i - 1);
1044 FieldVals.push_back(FieldVal);
1045 ++Num;
1046 } else {
1047 State = 2; // Still in Island
1048 ++BitNo;
1049 FieldVal = FieldVal | Val << BitNo;
1050 }
1051 break;
1052 }
1053 }
1054 // If we are still in Island after the loop, do some housekeeping.
1055 if (State == 2) {
Owen Andersonc78e03c2011-07-19 21:06:00 +00001056 EndBits.push_back(BitWidth - 1);
Owen Anderson4e818902011-02-18 21:51:29 +00001057 FieldVals.push_back(FieldVal);
1058 ++Num;
1059 }
1060
1061 assert(StartBits.size() == Num && EndBits.size() == Num &&
1062 FieldVals.size() == Num);
1063 return Num;
1064}
1065
Owen Andersone3591652011-07-28 21:54:31 +00001066void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
Petr Pavlu182b0572015-07-15 08:04:27 +00001067 const OperandInfo &OpInfo,
1068 bool &OpHasCompleteDecoder) const {
Craig Topper48c112b2012-03-16 05:58:09 +00001069 const std::string &Decoder = OpInfo.Decoder;
Owen Andersone3591652011-07-28 21:54:31 +00001070
Craig Topper5546f8c2014-09-27 05:26:42 +00001071 if (OpInfo.numFields() != 1)
Craig Topperebc3aa22012-08-17 05:16:15 +00001072 o.indent(Indentation) << "tmp = 0;\n";
Craig Topper5546f8c2014-09-27 05:26:42 +00001073
1074 for (const EncodingField &EF : OpInfo) {
1075 o.indent(Indentation) << "tmp ";
1076 if (OpInfo.numFields() != 1) o << '|';
1077 o << "= fieldFromInstruction"
1078 << "(insn, " << EF.Base << ", " << EF.Width << ')';
1079 if (OpInfo.numFields() != 1 || EF.Offset != 0)
1080 o << " << " << EF.Offset;
1081 o << ";\n";
Owen Andersone3591652011-07-28 21:54:31 +00001082 }
1083
Petr Pavlu182b0572015-07-15 08:04:27 +00001084 if (Decoder != "") {
1085 OpHasCompleteDecoder = OpInfo.HasCompleteDecoder;
Craig Topperebc3aa22012-08-17 05:16:15 +00001086 o.indent(Indentation) << Emitter->GuardPrefix << Decoder
Petr Pavlu182b0572015-07-15 08:04:27 +00001087 << "(MI, tmp, Address, Decoder)"
1088 << Emitter->GuardPostfix
1089 << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
1090 << "return MCDisassembler::Fail; }\n";
1091 } else {
1092 OpHasCompleteDecoder = true;
Jim Grosbache9119e42015-05-13 18:37:00 +00001093 o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
Petr Pavlu182b0572015-07-15 08:04:27 +00001094 }
Owen Andersone3591652011-07-28 21:54:31 +00001095}
1096
Jim Grosbachecaef492012-08-14 19:06:05 +00001097void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
Petr Pavlu182b0572015-07-15 08:04:27 +00001098 unsigned Opc, bool &HasCompleteDecoder) const {
1099 HasCompleteDecoder = true;
1100
Craig Topper1f7604d2014-12-13 05:12:19 +00001101 for (const auto &Op : Operands.find(Opc)->second) {
Jim Grosbachecaef492012-08-14 19:06:05 +00001102 // If a custom instruction decoder was specified, use that.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001103 if (Op.numFields() == 0 && !Op.Decoder.empty()) {
Petr Pavlu182b0572015-07-15 08:04:27 +00001104 HasCompleteDecoder = Op.HasCompleteDecoder;
Craig Topper1f7604d2014-12-13 05:12:19 +00001105 OS.indent(Indentation) << Emitter->GuardPrefix << Op.Decoder
Jim Grosbachecaef492012-08-14 19:06:05 +00001106 << "(MI, insn, Address, Decoder)"
Petr Pavlu182b0572015-07-15 08:04:27 +00001107 << Emitter->GuardPostfix
1108 << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
1109 << "return MCDisassembler::Fail; }\n";
Jim Grosbachecaef492012-08-14 19:06:05 +00001110 break;
1111 }
1112
Petr Pavlu182b0572015-07-15 08:04:27 +00001113 bool OpHasCompleteDecoder;
1114 emitBinaryParser(OS, Indentation, Op, OpHasCompleteDecoder);
1115 if (!OpHasCompleteDecoder)
1116 HasCompleteDecoder = false;
Jim Grosbachecaef492012-08-14 19:06:05 +00001117 }
1118}
1119
1120unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
Petr Pavlu182b0572015-07-15 08:04:27 +00001121 unsigned Opc,
1122 bool &HasCompleteDecoder) const {
Jim Grosbachecaef492012-08-14 19:06:05 +00001123 // Build up the predicate string.
1124 SmallString<256> Decoder;
1125 // FIXME: emitDecoder() function can take a buffer directly rather than
1126 // a stream.
1127 raw_svector_ostream S(Decoder);
Craig Topperebc3aa22012-08-17 05:16:15 +00001128 unsigned I = 4;
Petr Pavlu182b0572015-07-15 08:04:27 +00001129 emitDecoder(S, I, Opc, HasCompleteDecoder);
Jim Grosbachecaef492012-08-14 19:06:05 +00001130
1131 // Using the full decoder string as the key value here is a bit
1132 // heavyweight, but is effective. If the string comparisons become a
1133 // performance concern, we can implement a mangling of the predicate
Nick Lewycky06b0ea22015-08-18 22:41:58 +00001134 // data easily enough with a map back to the actual string. That's
Jim Grosbachecaef492012-08-14 19:06:05 +00001135 // overkill for now, though.
1136
1137 // Make sure the predicate is in the table.
Justin Lebar5e83dfe2016-10-21 21:45:01 +00001138 Decoders.insert(CachedHashString(Decoder));
Jim Grosbachecaef492012-08-14 19:06:05 +00001139 // Now figure out the index for when we write out the table.
David Majnemer42531262016-08-12 03:55:06 +00001140 DecoderSet::const_iterator P = find(Decoders, Decoder.str());
Jim Grosbachecaef492012-08-14 19:06:05 +00001141 return (unsigned)(P - Decoders.begin());
1142}
1143
James Molloy8067df92011-09-07 19:42:28 +00001144static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
Craig Topper48c112b2012-03-16 05:58:09 +00001145 const std::string &PredicateNamespace) {
Andrew Trick43674ad2011-09-08 05:25:49 +00001146 if (str[0] == '!')
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001147 o << "!Bits[" << PredicateNamespace << "::"
1148 << str.slice(1,str.size()) << "]";
James Molloy8067df92011-09-07 19:42:28 +00001149 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001150 o << "Bits[" << PredicateNamespace << "::" << str << "]";
James Molloy8067df92011-09-07 19:42:28 +00001151}
1152
1153bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
Craig Topper48c112b2012-03-16 05:58:09 +00001154 unsigned Opc) const {
Jim Grosbach3f4b2392012-02-29 22:07:56 +00001155 ListInit *Predicates =
1156 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
Toma Tabacu3d5ce492015-04-07 12:10:11 +00001157 bool IsFirstEmission = true;
Craig Topper664f6a02015-06-02 04:15:57 +00001158 for (unsigned i = 0; i < Predicates->size(); ++i) {
James Molloy8067df92011-09-07 19:42:28 +00001159 Record *Pred = Predicates->getElementAsRecord(i);
1160 if (!Pred->getValue("AssemblerMatcherPredicate"))
1161 continue;
1162
Craig Topperbcd3c372017-05-31 21:12:46 +00001163 StringRef P = Pred->getValueAsString("AssemblerCondString");
James Molloy8067df92011-09-07 19:42:28 +00001164
Craig Topperbcd3c372017-05-31 21:12:46 +00001165 if (P.empty())
James Molloy8067df92011-09-07 19:42:28 +00001166 continue;
1167
Toma Tabacu3d5ce492015-04-07 12:10:11 +00001168 if (!IsFirstEmission)
James Molloy8067df92011-09-07 19:42:28 +00001169 o << " && ";
1170
Craig Topperbcd3c372017-05-31 21:12:46 +00001171 std::pair<StringRef, StringRef> pairs = P.split(',');
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001172 while (!pairs.second.empty()) {
James Molloy8067df92011-09-07 19:42:28 +00001173 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1174 o << " && ";
1175 pairs = pairs.second.split(',');
1176 }
1177 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
Toma Tabacu3d5ce492015-04-07 12:10:11 +00001178 IsFirstEmission = false;
James Molloy8067df92011-09-07 19:42:28 +00001179 }
Craig Topper664f6a02015-06-02 04:15:57 +00001180 return !Predicates->empty();
Andrew Trick61abca62011-09-08 05:23:14 +00001181}
James Molloy8067df92011-09-07 19:42:28 +00001182
Jim Grosbachecaef492012-08-14 19:06:05 +00001183bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const {
1184 ListInit *Predicates =
1185 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
Craig Topper664f6a02015-06-02 04:15:57 +00001186 for (unsigned i = 0; i < Predicates->size(); ++i) {
Jim Grosbachecaef492012-08-14 19:06:05 +00001187 Record *Pred = Predicates->getElementAsRecord(i);
1188 if (!Pred->getValue("AssemblerMatcherPredicate"))
1189 continue;
1190
Craig Topperbcd3c372017-05-31 21:12:46 +00001191 StringRef P = Pred->getValueAsString("AssemblerCondString");
Jim Grosbachecaef492012-08-14 19:06:05 +00001192
Craig Topperbcd3c372017-05-31 21:12:46 +00001193 if (P.empty())
Jim Grosbachecaef492012-08-14 19:06:05 +00001194 continue;
1195
1196 return true;
1197 }
1198 return false;
1199}
1200
1201unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo,
1202 StringRef Predicate) const {
1203 // Using the full predicate string as the key value here is a bit
1204 // heavyweight, but is effective. If the string comparisons become a
1205 // performance concern, we can implement a mangling of the predicate
Nick Lewycky06b0ea22015-08-18 22:41:58 +00001206 // data easily enough with a map back to the actual string. That's
Jim Grosbachecaef492012-08-14 19:06:05 +00001207 // overkill for now, though.
1208
1209 // Make sure the predicate is in the table.
Justin Lebar5e83dfe2016-10-21 21:45:01 +00001210 TableInfo.Predicates.insert(CachedHashString(Predicate));
Jim Grosbachecaef492012-08-14 19:06:05 +00001211 // Now figure out the index for when we write out the table.
Justin Lebar5e83dfe2016-10-21 21:45:01 +00001212 PredicateSet::const_iterator P = find(TableInfo.Predicates, Predicate);
Jim Grosbachecaef492012-08-14 19:06:05 +00001213 return (unsigned)(P - TableInfo.Predicates.begin());
1214}
1215
1216void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
1217 unsigned Opc) const {
1218 if (!doesOpcodeNeedPredicate(Opc))
1219 return;
1220
1221 // Build up the predicate string.
1222 SmallString<256> Predicate;
1223 // FIXME: emitPredicateMatch() functions can take a buffer directly rather
1224 // than a stream.
1225 raw_svector_ostream PS(Predicate);
1226 unsigned I = 0;
1227 emitPredicateMatch(PS, I, Opc);
1228
1229 // Figure out the index into the predicate table for the predicate just
1230 // computed.
1231 unsigned PIdx = getPredicateIndex(TableInfo, PS.str());
1232 SmallString<16> PBytes;
1233 raw_svector_ostream S(PBytes);
1234 encodeULEB128(PIdx, S);
Jim Grosbachecaef492012-08-14 19:06:05 +00001235
1236 TableInfo.Table.push_back(MCD::OPC_CheckPredicate);
1237 // Predicate index
Craig Topper29688ab2012-08-17 05:42:16 +00001238 for (unsigned i = 0, e = PBytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001239 TableInfo.Table.push_back(PBytes[i]);
1240 // Push location for NumToSkip backpatching.
1241 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1242 TableInfo.Table.push_back(0);
1243 TableInfo.Table.push_back(0);
Sander de Smalen13f94252018-07-05 10:39:15 +00001244 TableInfo.Table.push_back(0);
Jim Grosbachecaef492012-08-14 19:06:05 +00001245}
1246
1247void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
1248 unsigned Opc) const {
Jim Grosbach3f4b2392012-02-29 22:07:56 +00001249 BitsInit *SFBits =
1250 AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail");
James Molloyd9ba4fd2012-02-09 10:56:31 +00001251 if (!SFBits) return;
1252 BitsInit *InstBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("Inst");
1253
1254 APInt PositiveMask(BitWidth, 0ULL);
1255 APInt NegativeMask(BitWidth, 0ULL);
1256 for (unsigned i = 0; i < BitWidth; ++i) {
1257 bit_value_t B = bitFromBits(*SFBits, i);
1258 bit_value_t IB = bitFromBits(*InstBits, i);
1259
1260 if (B != BIT_TRUE) continue;
1261
1262 switch (IB) {
1263 case BIT_FALSE:
1264 // The bit is meant to be false, so emit a check to see if it is true.
1265 PositiveMask.setBit(i);
1266 break;
1267 case BIT_TRUE:
1268 // The bit is meant to be true, so emit a check to see if it is false.
1269 NegativeMask.setBit(i);
1270 break;
1271 default:
1272 // The bit is not set; this must be an error!
1273 StringRef Name = AllInstructions[Opc]->TheDef->getName();
Jim Grosbachecaef492012-08-14 19:06:05 +00001274 errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in " << Name
1275 << " is set but Inst{" << i << "} is unset!\n"
James Molloyd9ba4fd2012-02-09 10:56:31 +00001276 << " - You can only mark a bit as SoftFail if it is fully defined"
1277 << " (1/0 - not '?') in Inst\n";
Jim Grosbachecaef492012-08-14 19:06:05 +00001278 return;
James Molloyd9ba4fd2012-02-09 10:56:31 +00001279 }
1280 }
1281
1282 bool NeedPositiveMask = PositiveMask.getBoolValue();
1283 bool NeedNegativeMask = NegativeMask.getBoolValue();
1284
1285 if (!NeedPositiveMask && !NeedNegativeMask)
1286 return;
1287
Jim Grosbachecaef492012-08-14 19:06:05 +00001288 TableInfo.Table.push_back(MCD::OPC_SoftFail);
James Molloyd9ba4fd2012-02-09 10:56:31 +00001289
Jim Grosbachecaef492012-08-14 19:06:05 +00001290 SmallString<16> MaskBytes;
1291 raw_svector_ostream S(MaskBytes);
1292 if (NeedPositiveMask) {
1293 encodeULEB128(PositiveMask.getZExtValue(), S);
Craig Topper29688ab2012-08-17 05:42:16 +00001294 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001295 TableInfo.Table.push_back(MaskBytes[i]);
1296 } else
1297 TableInfo.Table.push_back(0);
1298 if (NeedNegativeMask) {
1299 MaskBytes.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +00001300 encodeULEB128(NegativeMask.getZExtValue(), S);
Craig Topper29688ab2012-08-17 05:42:16 +00001301 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001302 TableInfo.Table.push_back(MaskBytes[i]);
1303 } else
1304 TableInfo.Table.push_back(0);
James Molloyd9ba4fd2012-02-09 10:56:31 +00001305}
1306
Jim Grosbachecaef492012-08-14 19:06:05 +00001307// Emits table entries to decode the singleton.
1308void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1309 unsigned Opc) const {
Owen Anderson4e818902011-02-18 21:51:29 +00001310 std::vector<unsigned> StartBits;
1311 std::vector<unsigned> EndBits;
1312 std::vector<uint64_t> FieldVals;
1313 insn_t Insn;
1314 insnWithID(Insn, Opc);
1315
1316 // Look for islands of undecoded bits of the singleton.
1317 getIslands(StartBits, EndBits, FieldVals, Insn);
1318
1319 unsigned Size = StartBits.size();
Owen Anderson4e818902011-02-18 21:51:29 +00001320
Jim Grosbachecaef492012-08-14 19:06:05 +00001321 // Emit the predicate table entry if one is needed.
1322 emitPredicateTableEntry(TableInfo, Opc);
Owen Anderson4e818902011-02-18 21:51:29 +00001323
Jim Grosbachecaef492012-08-14 19:06:05 +00001324 // Check any additional encoding fields needed.
Craig Topper29688ab2012-08-17 05:42:16 +00001325 for (unsigned I = Size; I != 0; --I) {
1326 unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1;
Jim Grosbachecaef492012-08-14 19:06:05 +00001327 TableInfo.Table.push_back(MCD::OPC_CheckField);
1328 TableInfo.Table.push_back(StartBits[I-1]);
1329 TableInfo.Table.push_back(NumBits);
Sander de Smalen13f94252018-07-05 10:39:15 +00001330 uint8_t Buffer[16], *p;
Jim Grosbachecaef492012-08-14 19:06:05 +00001331 encodeULEB128(FieldVals[I-1], Buffer);
1332 for (p = Buffer; *p >= 128 ; ++p)
1333 TableInfo.Table.push_back(*p);
1334 TableInfo.Table.push_back(*p);
1335 // Push location for NumToSkip backpatching.
1336 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
Sander de Smalen13f94252018-07-05 10:39:15 +00001337 // The fixup is always 24-bits, so go ahead and allocate the space
Jim Grosbachecaef492012-08-14 19:06:05 +00001338 // in the table so all our relative position calculations work OK even
1339 // before we fully resolve the real value here.
1340 TableInfo.Table.push_back(0);
1341 TableInfo.Table.push_back(0);
Sander de Smalen13f94252018-07-05 10:39:15 +00001342 TableInfo.Table.push_back(0);
Owen Anderson4e818902011-02-18 21:51:29 +00001343 }
Owen Anderson4e818902011-02-18 21:51:29 +00001344
Jim Grosbachecaef492012-08-14 19:06:05 +00001345 // Check for soft failure of the match.
1346 emitSoftFailTableEntry(TableInfo, Opc);
Owen Anderson4e818902011-02-18 21:51:29 +00001347
Petr Pavlu182b0572015-07-15 08:04:27 +00001348 bool HasCompleteDecoder;
1349 unsigned DIdx = getDecoderIndex(TableInfo.Decoders, Opc, HasCompleteDecoder);
1350
1351 // Produce OPC_Decode or OPC_TryDecode opcode based on the information
1352 // whether the instruction decoder is complete or not. If it is complete
1353 // then it handles all possible values of remaining variable/unfiltered bits
1354 // and for any value can determine if the bitpattern is a valid instruction
1355 // or not. This means OPC_Decode will be the final step in the decoding
1356 // process. If it is not complete, then the Fail return code from the
1357 // decoder method indicates that additional processing should be done to see
1358 // if there is any other instruction that also matches the bitpattern and
1359 // can decode it.
1360 TableInfo.Table.push_back(HasCompleteDecoder ? MCD::OPC_Decode :
1361 MCD::OPC_TryDecode);
Sander de Smalen13f94252018-07-05 10:39:15 +00001362 uint8_t Buffer[16], *p;
Jim Grosbachecaef492012-08-14 19:06:05 +00001363 encodeULEB128(Opc, Buffer);
1364 for (p = Buffer; *p >= 128 ; ++p)
1365 TableInfo.Table.push_back(*p);
1366 TableInfo.Table.push_back(*p);
1367
Jim Grosbachecaef492012-08-14 19:06:05 +00001368 SmallString<16> Bytes;
1369 raw_svector_ostream S(Bytes);
1370 encodeULEB128(DIdx, S);
Jim Grosbachecaef492012-08-14 19:06:05 +00001371
1372 // Decoder index
Craig Topper29688ab2012-08-17 05:42:16 +00001373 for (unsigned i = 0, e = Bytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001374 TableInfo.Table.push_back(Bytes[i]);
Petr Pavlu182b0572015-07-15 08:04:27 +00001375
1376 if (!HasCompleteDecoder) {
1377 // Push location for NumToSkip backpatching.
1378 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1379 // Allocate the space for the fixup.
1380 TableInfo.Table.push_back(0);
1381 TableInfo.Table.push_back(0);
Sander de Smalen13f94252018-07-05 10:39:15 +00001382 TableInfo.Table.push_back(0);
Petr Pavlu182b0572015-07-15 08:04:27 +00001383 }
Owen Anderson4e818902011-02-18 21:51:29 +00001384}
1385
Jim Grosbachecaef492012-08-14 19:06:05 +00001386// Emits table entries to decode the singleton, and then to decode the rest.
1387void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1388 const Filter &Best) const {
Owen Anderson4e818902011-02-18 21:51:29 +00001389 unsigned Opc = Best.getSingletonOpc();
1390
Jim Grosbachecaef492012-08-14 19:06:05 +00001391 // complex singletons need predicate checks from the first singleton
1392 // to refer forward to the variable filterchooser that follows.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001393 TableInfo.FixupStack.emplace_back();
Owen Anderson4e818902011-02-18 21:51:29 +00001394
Jim Grosbachecaef492012-08-14 19:06:05 +00001395 emitSingletonTableEntry(TableInfo, Opc);
Owen Anderson4e818902011-02-18 21:51:29 +00001396
Jim Grosbachecaef492012-08-14 19:06:05 +00001397 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
1398 TableInfo.Table.size());
1399 TableInfo.FixupStack.pop_back();
1400
1401 Best.getVariableFC().emitTableEntries(TableInfo);
Owen Anderson4e818902011-02-18 21:51:29 +00001402}
1403
1404// Assign a single filter and run with it. Top level API client can initialize
1405// with a single filter to start the filtering process.
Craig Topper48c112b2012-03-16 05:58:09 +00001406void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit,
1407 bool mixed) {
Owen Anderson4e818902011-02-18 21:51:29 +00001408 Filters.clear();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001409 Filters.emplace_back(*this, startBit, numBit, true);
Owen Anderson4e818902011-02-18 21:51:29 +00001410 BestIndex = 0; // Sole Filter instance to choose from.
1411 bestFilter().recurse();
1412}
1413
1414// reportRegion is a helper function for filterProcessor to mark a region as
1415// eligible for use as a filter region.
1416void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
Craig Topper82d0d5f2012-03-16 01:19:24 +00001417 unsigned BitIndex, bool AllowMixed) {
Owen Anderson4e818902011-02-18 21:51:29 +00001418 if (RA == ATTR_MIXED && AllowMixed)
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001419 Filters.emplace_back(*this, StartBit, BitIndex - StartBit, true);
Owen Anderson4e818902011-02-18 21:51:29 +00001420 else if (RA == ATTR_ALL_SET && !AllowMixed)
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001421 Filters.emplace_back(*this, StartBit, BitIndex - StartBit, false);
Owen Anderson4e818902011-02-18 21:51:29 +00001422}
1423
1424// FilterProcessor scans the well-known encoding bits of the instructions and
1425// builds up a list of candidate filters. It chooses the best filter and
1426// recursively descends down the decoding tree.
1427bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1428 Filters.clear();
1429 BestIndex = -1;
1430 unsigned numInstructions = Opcodes.size();
1431
1432 assert(numInstructions && "Filter created with no instructions");
1433
1434 // No further filtering is necessary.
1435 if (numInstructions == 1)
1436 return true;
1437
1438 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1439 // instructions is 3.
1440 if (AllowMixed && !Greedy) {
1441 assert(numInstructions == 3);
1442
1443 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1444 std::vector<unsigned> StartBits;
1445 std::vector<unsigned> EndBits;
1446 std::vector<uint64_t> FieldVals;
1447 insn_t Insn;
1448
1449 insnWithID(Insn, Opcodes[i]);
1450
1451 // Look for islands of undecoded bits of any instruction.
1452 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1453 // Found an instruction with island(s). Now just assign a filter.
Craig Topper48c112b2012-03-16 05:58:09 +00001454 runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true);
Owen Anderson4e818902011-02-18 21:51:29 +00001455 return true;
1456 }
1457 }
1458 }
1459
Craig Topper29688ab2012-08-17 05:42:16 +00001460 unsigned BitIndex;
Owen Anderson4e818902011-02-18 21:51:29 +00001461
1462 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1463 // The automaton consumes the corresponding bit from each
1464 // instruction.
1465 //
1466 // Input symbols: 0, 1, and _ (unset).
1467 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1468 // Initial state: NONE.
1469 //
1470 // (NONE) ------- [01] -> (ALL_SET)
1471 // (NONE) ------- _ ----> (ALL_UNSET)
1472 // (ALL_SET) ---- [01] -> (ALL_SET)
1473 // (ALL_SET) ---- _ ----> (MIXED)
1474 // (ALL_UNSET) -- [01] -> (MIXED)
1475 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1476 // (MIXED) ------ . ----> (MIXED)
1477 // (FILTERED)---- . ----> (FILTERED)
1478
Owen Andersonc78e03c2011-07-19 21:06:00 +00001479 std::vector<bitAttr_t> bitAttrs;
Owen Anderson4e818902011-02-18 21:51:29 +00001480
1481 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1482 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
Owen Andersonc78e03c2011-07-19 21:06:00 +00001483 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
Owen Anderson4e818902011-02-18 21:51:29 +00001484 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1485 FilterBitValues[BitIndex] == BIT_FALSE)
Owen Andersonc78e03c2011-07-19 21:06:00 +00001486 bitAttrs.push_back(ATTR_FILTERED);
Owen Anderson4e818902011-02-18 21:51:29 +00001487 else
Owen Andersonc78e03c2011-07-19 21:06:00 +00001488 bitAttrs.push_back(ATTR_NONE);
Owen Anderson4e818902011-02-18 21:51:29 +00001489
Craig Topper29688ab2012-08-17 05:42:16 +00001490 for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
Owen Anderson4e818902011-02-18 21:51:29 +00001491 insn_t insn;
1492
1493 insnWithID(insn, Opcodes[InsnIndex]);
1494
Owen Andersonc78e03c2011-07-19 21:06:00 +00001495 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
Owen Anderson4e818902011-02-18 21:51:29 +00001496 switch (bitAttrs[BitIndex]) {
1497 case ATTR_NONE:
1498 if (insn[BitIndex] == BIT_UNSET)
1499 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1500 else
1501 bitAttrs[BitIndex] = ATTR_ALL_SET;
1502 break;
1503 case ATTR_ALL_SET:
1504 if (insn[BitIndex] == BIT_UNSET)
1505 bitAttrs[BitIndex] = ATTR_MIXED;
1506 break;
1507 case ATTR_ALL_UNSET:
1508 if (insn[BitIndex] != BIT_UNSET)
1509 bitAttrs[BitIndex] = ATTR_MIXED;
1510 break;
1511 case ATTR_MIXED:
1512 case ATTR_FILTERED:
1513 break;
1514 }
1515 }
1516 }
1517
1518 // The regionAttr automaton consumes the bitAttrs automatons' state,
1519 // lowest-to-highest.
1520 //
1521 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1522 // States: NONE, ALL_SET, MIXED
1523 // Initial state: NONE
1524 //
1525 // (NONE) ----- F --> (NONE)
1526 // (NONE) ----- S --> (ALL_SET) ; and set region start
1527 // (NONE) ----- U --> (NONE)
1528 // (NONE) ----- M --> (MIXED) ; and set region start
1529 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1530 // (ALL_SET) -- S --> (ALL_SET)
1531 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1532 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1533 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1534 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1535 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1536 // (MIXED) ---- M --> (MIXED)
1537
1538 bitAttr_t RA = ATTR_NONE;
1539 unsigned StartBit = 0;
1540
Craig Topper29688ab2012-08-17 05:42:16 +00001541 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
Owen Anderson4e818902011-02-18 21:51:29 +00001542 bitAttr_t bitAttr = bitAttrs[BitIndex];
1543
1544 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1545
1546 switch (RA) {
1547 case ATTR_NONE:
1548 switch (bitAttr) {
1549 case ATTR_FILTERED:
1550 break;
1551 case ATTR_ALL_SET:
1552 StartBit = BitIndex;
1553 RA = ATTR_ALL_SET;
1554 break;
1555 case ATTR_ALL_UNSET:
1556 break;
1557 case ATTR_MIXED:
1558 StartBit = BitIndex;
1559 RA = ATTR_MIXED;
1560 break;
1561 default:
Craig Topperc4965bc2012-02-05 07:21:30 +00001562 llvm_unreachable("Unexpected bitAttr!");
Owen Anderson4e818902011-02-18 21:51:29 +00001563 }
1564 break;
1565 case ATTR_ALL_SET:
1566 switch (bitAttr) {
1567 case ATTR_FILTERED:
1568 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1569 RA = ATTR_NONE;
1570 break;
1571 case ATTR_ALL_SET:
1572 break;
1573 case ATTR_ALL_UNSET:
1574 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1575 RA = ATTR_NONE;
1576 break;
1577 case ATTR_MIXED:
1578 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1579 StartBit = BitIndex;
1580 RA = ATTR_MIXED;
1581 break;
1582 default:
Craig Topperc4965bc2012-02-05 07:21:30 +00001583 llvm_unreachable("Unexpected bitAttr!");
Owen Anderson4e818902011-02-18 21:51:29 +00001584 }
1585 break;
1586 case ATTR_MIXED:
1587 switch (bitAttr) {
1588 case ATTR_FILTERED:
1589 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1590 StartBit = BitIndex;
1591 RA = ATTR_NONE;
1592 break;
1593 case ATTR_ALL_SET:
1594 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1595 StartBit = BitIndex;
1596 RA = ATTR_ALL_SET;
1597 break;
1598 case ATTR_ALL_UNSET:
1599 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1600 RA = ATTR_NONE;
1601 break;
1602 case ATTR_MIXED:
1603 break;
1604 default:
Craig Topperc4965bc2012-02-05 07:21:30 +00001605 llvm_unreachable("Unexpected bitAttr!");
Owen Anderson4e818902011-02-18 21:51:29 +00001606 }
1607 break;
1608 case ATTR_ALL_UNSET:
Craig Topperc4965bc2012-02-05 07:21:30 +00001609 llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
Owen Anderson4e818902011-02-18 21:51:29 +00001610 case ATTR_FILTERED:
Craig Topperc4965bc2012-02-05 07:21:30 +00001611 llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
Owen Anderson4e818902011-02-18 21:51:29 +00001612 }
1613 }
1614
1615 // At the end, if we're still in ALL_SET or MIXED states, report a region
1616 switch (RA) {
1617 case ATTR_NONE:
1618 break;
1619 case ATTR_FILTERED:
1620 break;
1621 case ATTR_ALL_SET:
1622 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1623 break;
1624 case ATTR_ALL_UNSET:
1625 break;
1626 case ATTR_MIXED:
1627 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1628 break;
1629 }
1630
1631 // We have finished with the filter processings. Now it's time to choose
1632 // the best performing filter.
1633 BestIndex = 0;
1634 bool AllUseless = true;
1635 unsigned BestScore = 0;
1636
1637 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1638 unsigned Usefulness = Filters[i].usefulness();
1639
1640 if (Usefulness)
1641 AllUseless = false;
1642
1643 if (Usefulness > BestScore) {
1644 BestIndex = i;
1645 BestScore = Usefulness;
1646 }
1647 }
1648
1649 if (!AllUseless)
1650 bestFilter().recurse();
1651
1652 return !AllUseless;
1653} // end of FilterChooser::filterProcessor(bool)
1654
1655// Decides on the best configuration of filter(s) to use in order to decode
1656// the instructions. A conflict of instructions may occur, in which case we
1657// dump the conflict set to the standard error.
1658void FilterChooser::doFilter() {
1659 unsigned Num = Opcodes.size();
1660 assert(Num && "FilterChooser created with no instructions");
1661
1662 // Try regions of consecutive known bit values first.
1663 if (filterProcessor(false))
1664 return;
1665
1666 // Then regions of mixed bits (both known and unitialized bit values allowed).
1667 if (filterProcessor(true))
1668 return;
1669
1670 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1671 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1672 // well-known encoding pattern. In such case, we backtrack and scan for the
1673 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1674 if (Num == 3 && filterProcessor(true, false))
1675 return;
1676
1677 // If we come to here, the instruction decoding has failed.
1678 // Set the BestIndex to -1 to indicate so.
1679 BestIndex = -1;
1680}
1681
Jim Grosbachecaef492012-08-14 19:06:05 +00001682// emitTableEntries - Emit state machine entries to decode our share of
1683// instructions.
1684void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
1685 if (Opcodes.size() == 1) {
Owen Anderson4e818902011-02-18 21:51:29 +00001686 // There is only one instruction in the set, which is great!
1687 // Call emitSingletonDecoder() to see whether there are any remaining
1688 // encodings bits.
Jim Grosbachecaef492012-08-14 19:06:05 +00001689 emitSingletonTableEntry(TableInfo, Opcodes[0]);
1690 return;
1691 }
Owen Anderson4e818902011-02-18 21:51:29 +00001692
1693 // Choose the best filter to do the decodings!
1694 if (BestIndex != -1) {
Craig Topper48c112b2012-03-16 05:58:09 +00001695 const Filter &Best = Filters[BestIndex];
Owen Anderson4e818902011-02-18 21:51:29 +00001696 if (Best.getNumFiltered() == 1)
Jim Grosbachecaef492012-08-14 19:06:05 +00001697 emitSingletonTableEntry(TableInfo, Best);
Owen Anderson4e818902011-02-18 21:51:29 +00001698 else
Jim Grosbachecaef492012-08-14 19:06:05 +00001699 Best.emitTableEntry(TableInfo);
1700 return;
Owen Anderson4e818902011-02-18 21:51:29 +00001701 }
1702
Jim Grosbachecaef492012-08-14 19:06:05 +00001703 // We don't know how to decode these instructions! Dump the
1704 // conflict set and bail.
Owen Anderson4e818902011-02-18 21:51:29 +00001705
1706 // Print out useful conflict information for postmortem analysis.
1707 errs() << "Decoding Conflict:\n";
1708
1709 dumpStack(errs(), "\t\t");
1710
Craig Topper82d0d5f2012-03-16 01:19:24 +00001711 for (unsigned i = 0; i < Opcodes.size(); ++i) {
Alexander Shaposhnikovd968f6f2017-07-05 20:14:54 +00001712 errs() << '\t' << nameWithID(Opcodes[i]) << " ";
Owen Anderson4e818902011-02-18 21:51:29 +00001713 dumpBits(errs(),
1714 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1715 errs() << '\n';
1716 }
Owen Anderson4e818902011-02-18 21:51:29 +00001717}
1718
Matt Arsenault4cb438b2016-07-18 23:20:46 +00001719static std::string findOperandDecoderMethod(TypedInit *TI) {
1720 std::string Decoder;
1721
Nicolai Haehnle0409b282018-03-05 14:01:30 +00001722 Record *Record = cast<DefInit>(TI)->getDef();
Matt Arsenault4cb438b2016-07-18 23:20:46 +00001723
Nicolai Haehnle0409b282018-03-05 14:01:30 +00001724 RecordVal *DecoderString = Record->getValue("DecoderMethod");
Matt Arsenault4cb438b2016-07-18 23:20:46 +00001725 StringInit *String = DecoderString ?
1726 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
1727 if (String) {
1728 Decoder = String->getValue();
1729 if (!Decoder.empty())
1730 return Decoder;
1731 }
1732
Nicolai Haehnle0409b282018-03-05 14:01:30 +00001733 if (Record->isSubClassOf("RegisterOperand"))
1734 Record = Record->getValueAsDef("RegClass");
Matt Arsenault4cb438b2016-07-18 23:20:46 +00001735
Nicolai Haehnle0409b282018-03-05 14:01:30 +00001736 if (Record->isSubClassOf("RegisterClass")) {
1737 Decoder = "Decode" + Record->getName().str() + "RegisterClass";
1738 } else if (Record->isSubClassOf("PointerLikeRegClass")) {
Matt Arsenault4cb438b2016-07-18 23:20:46 +00001739 Decoder = "DecodePointerLikeRegClass" +
Nicolai Haehnle0409b282018-03-05 14:01:30 +00001740 utostr(Record->getValueAsInt("RegClassKind"));
Matt Arsenault4cb438b2016-07-18 23:20:46 +00001741 }
1742
1743 return Decoder;
1744}
1745
Hal Finkel71b2e202013-12-19 16:12:53 +00001746static bool populateInstruction(CodeGenTarget &Target,
1747 const CodeGenInstruction &CGI, unsigned Opc,
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001748 std::map<unsigned, std::vector<OperandInfo>> &Operands){
Owen Anderson4e818902011-02-18 21:51:29 +00001749 const Record &Def = *CGI.TheDef;
1750 // If all the bit positions are not specified; do not decode this instruction.
1751 // We are bound to fail! For proper disassembly, the well-known encoding bits
1752 // of the instruction must be fully specified.
Owen Anderson4e818902011-02-18 21:51:29 +00001753
David Greeneaf8ee2c2011-07-29 22:43:06 +00001754 BitsInit &Bits = getBitsField(Def, "Inst");
Jim Grosbachf3fd36e2011-07-06 21:33:38 +00001755 if (Bits.allInComplete()) return false;
1756
Owen Anderson4e818902011-02-18 21:51:29 +00001757 std::vector<OperandInfo> InsnOperands;
1758
1759 // If the instruction has specified a custom decoding hook, use that instead
1760 // of trying to auto-generate the decoder.
Craig Topperbcd3c372017-05-31 21:12:46 +00001761 StringRef InstDecoder = Def.getValueAsString("DecoderMethod");
Owen Anderson4e818902011-02-18 21:51:29 +00001762 if (InstDecoder != "") {
Petr Pavlu182b0572015-07-15 08:04:27 +00001763 bool HasCompleteInstDecoder = Def.getValueAsBit("hasCompleteDecoder");
1764 InsnOperands.push_back(OperandInfo(InstDecoder, HasCompleteInstDecoder));
Owen Anderson4e818902011-02-18 21:51:29 +00001765 Operands[Opc] = InsnOperands;
1766 return true;
1767 }
1768
1769 // Generate a description of the operand of the instruction that we know
1770 // how to decode automatically.
1771 // FIXME: We'll need to have a way to manually override this as needed.
1772
1773 // Gather the outputs/inputs of the instruction, so we can find their
1774 // positions in the encoding. This assumes for now that they appear in the
1775 // MCInst in the order that they're listed.
Matthias Braunbb053162016-12-05 06:00:46 +00001776 std::vector<std::pair<Init*, StringRef>> InOutOperands;
David Greeneaf8ee2c2011-07-29 22:43:06 +00001777 DagInit *Out = Def.getValueAsDag("OutOperandList");
1778 DagInit *In = Def.getValueAsDag("InOperandList");
Owen Anderson4e818902011-02-18 21:51:29 +00001779 for (unsigned i = 0; i < Out->getNumArgs(); ++i)
Matthias Braunbb053162016-12-05 06:00:46 +00001780 InOutOperands.push_back(std::make_pair(Out->getArg(i),
1781 Out->getArgNameStr(i)));
Owen Anderson4e818902011-02-18 21:51:29 +00001782 for (unsigned i = 0; i < In->getNumArgs(); ++i)
Matthias Braunbb053162016-12-05 06:00:46 +00001783 InOutOperands.push_back(std::make_pair(In->getArg(i),
1784 In->getArgNameStr(i)));
Owen Anderson4e818902011-02-18 21:51:29 +00001785
Owen Anderson53562d02011-07-28 23:56:20 +00001786 // Search for tied operands, so that we can correctly instantiate
1787 // operands that are not explicitly represented in the encoding.
Owen Andersoncb32ce22011-07-29 18:28:52 +00001788 std::map<std::string, std::string> TiedNames;
Owen Anderson53562d02011-07-28 23:56:20 +00001789 for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
1790 int tiedTo = CGI.Operands[i].getTiedRegister();
Owen Andersoncb32ce22011-07-29 18:28:52 +00001791 if (tiedTo != -1) {
Hal Finkel71b2e202013-12-19 16:12:53 +00001792 std::pair<unsigned, unsigned> SO =
1793 CGI.Operands.getSubOperandNumber(tiedTo);
1794 TiedNames[InOutOperands[i].second] = InOutOperands[SO.first].second;
1795 TiedNames[InOutOperands[SO.first].second] = InOutOperands[i].second;
1796 }
1797 }
1798
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001799 std::map<std::string, std::vector<OperandInfo>> NumberedInsnOperands;
Hal Finkel71b2e202013-12-19 16:12:53 +00001800 std::set<std::string> NumberedInsnOperandsNoTie;
1801 if (Target.getInstructionSet()->
1802 getValueAsBit("decodePositionallyEncodedOperands")) {
1803 const std::vector<RecordVal> &Vals = Def.getValues();
1804 unsigned NumberedOp = 0;
1805
Hal Finkel5457bd02014-03-13 07:57:54 +00001806 std::set<unsigned> NamedOpIndices;
1807 if (Target.getInstructionSet()->
1808 getValueAsBit("noNamedPositionallyEncodedOperands"))
1809 // Collect the set of operand indices that might correspond to named
1810 // operand, and skip these when assigning operands based on position.
1811 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1812 unsigned OpIdx;
1813 if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
1814 continue;
1815
1816 NamedOpIndices.insert(OpIdx);
1817 }
1818
Hal Finkel71b2e202013-12-19 16:12:53 +00001819 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1820 // Ignore fixed fields in the record, we're looking for values like:
1821 // bits<5> RST = { ?, ?, ?, ?, ? };
1822 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
1823 continue;
1824
1825 // Determine if Vals[i] actually contributes to the Inst encoding.
1826 unsigned bi = 0;
1827 for (; bi < Bits.getNumBits(); ++bi) {
Craig Topper24064772014-04-15 07:20:03 +00001828 VarInit *Var = nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001829 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1830 if (BI)
1831 Var = dyn_cast<VarInit>(BI->getBitVar());
1832 else
1833 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1834
1835 if (Var && Var->getName() == Vals[i].getName())
1836 break;
1837 }
1838
1839 if (bi == Bits.getNumBits())
1840 continue;
1841
1842 // Skip variables that correspond to explicitly-named operands.
1843 unsigned OpIdx;
1844 if (CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
1845 continue;
1846
1847 // Get the bit range for this operand:
1848 unsigned bitStart = bi++, bitWidth = 1;
1849 for (; bi < Bits.getNumBits(); ++bi) {
Craig Topper24064772014-04-15 07:20:03 +00001850 VarInit *Var = nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001851 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1852 if (BI)
1853 Var = dyn_cast<VarInit>(BI->getBitVar());
1854 else
1855 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1856
1857 if (!Var)
1858 break;
1859
1860 if (Var->getName() != Vals[i].getName())
1861 break;
1862
1863 ++bitWidth;
1864 }
1865
1866 unsigned NumberOps = CGI.Operands.size();
1867 while (NumberedOp < NumberOps &&
Hal Finkel5457bd02014-03-13 07:57:54 +00001868 (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001869 (!NamedOpIndices.empty() && NamedOpIndices.count(
Hal Finkel5457bd02014-03-13 07:57:54 +00001870 CGI.Operands.getSubOperandNumber(NumberedOp).first))))
Hal Finkel71b2e202013-12-19 16:12:53 +00001871 ++NumberedOp;
1872
1873 OpIdx = NumberedOp++;
1874
1875 // OpIdx now holds the ordered operand number of Vals[i].
1876 std::pair<unsigned, unsigned> SO =
1877 CGI.Operands.getSubOperandNumber(OpIdx);
1878 const std::string &Name = CGI.Operands[SO.first].Name;
1879
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001880 LLVM_DEBUG(dbgs() << "Numbered operand mapping for " << Def.getName()
1881 << ": " << Name << "(" << SO.first << ", " << SO.second
1882 << ") => " << Vals[i].getName() << "\n");
Hal Finkel71b2e202013-12-19 16:12:53 +00001883
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001884 std::string Decoder;
Hal Finkel71b2e202013-12-19 16:12:53 +00001885 Record *TypeRecord = CGI.Operands[SO.first].Rec;
1886
1887 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
1888 StringInit *String = DecoderString ?
Craig Topper24064772014-04-15 07:20:03 +00001889 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001890 if (String && String->getValue() != "")
1891 Decoder = String->getValue();
1892
1893 if (Decoder == "" &&
1894 CGI.Operands[SO.first].MIOperandInfo &&
1895 CGI.Operands[SO.first].MIOperandInfo->getNumArgs()) {
1896 Init *Arg = CGI.Operands[SO.first].MIOperandInfo->
1897 getArg(SO.second);
Nicolai Haehnle0409b282018-03-05 14:01:30 +00001898 if (DefInit *DI = cast<DefInit>(Arg))
1899 TypeRecord = DI->getDef();
Hal Finkel71b2e202013-12-19 16:12:53 +00001900 }
1901
1902 bool isReg = false;
1903 if (TypeRecord->isSubClassOf("RegisterOperand"))
1904 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1905 if (TypeRecord->isSubClassOf("RegisterClass")) {
Matthias Braun4a86d452016-12-04 05:48:16 +00001906 Decoder = "Decode" + TypeRecord->getName().str() + "RegisterClass";
Hal Finkel71b2e202013-12-19 16:12:53 +00001907 isReg = true;
1908 } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
1909 Decoder = "DecodePointerLikeRegClass" +
1910 utostr(TypeRecord->getValueAsInt("RegClassKind"));
1911 isReg = true;
1912 }
1913
1914 DecoderString = TypeRecord->getValue("DecoderMethod");
1915 String = DecoderString ?
Craig Topper24064772014-04-15 07:20:03 +00001916 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001917 if (!isReg && String && String->getValue() != "")
1918 Decoder = String->getValue();
1919
Petr Pavlu182b0572015-07-15 08:04:27 +00001920 RecordVal *HasCompleteDecoderVal =
1921 TypeRecord->getValue("hasCompleteDecoder");
1922 BitInit *HasCompleteDecoderBit = HasCompleteDecoderVal ?
1923 dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) : nullptr;
1924 bool HasCompleteDecoder = HasCompleteDecoderBit ?
1925 HasCompleteDecoderBit->getValue() : true;
1926
1927 OperandInfo OpInfo(Decoder, HasCompleteDecoder);
Hal Finkel71b2e202013-12-19 16:12:53 +00001928 OpInfo.addField(bitStart, bitWidth, 0);
1929
1930 NumberedInsnOperands[Name].push_back(OpInfo);
1931
1932 // FIXME: For complex operands with custom decoders we can't handle tied
1933 // sub-operands automatically. Skip those here and assume that this is
1934 // fixed up elsewhere.
1935 if (CGI.Operands[SO.first].MIOperandInfo &&
1936 CGI.Operands[SO.first].MIOperandInfo->getNumArgs() > 1 &&
1937 String && String->getValue() != "")
1938 NumberedInsnOperandsNoTie.insert(Name);
Owen Andersoncb32ce22011-07-29 18:28:52 +00001939 }
Owen Anderson53562d02011-07-28 23:56:20 +00001940 }
1941
Owen Anderson4e818902011-02-18 21:51:29 +00001942 // For each operand, see if we can figure out where it is encoded.
Craig Topper1f7604d2014-12-13 05:12:19 +00001943 for (const auto &Op : InOutOperands) {
1944 if (!NumberedInsnOperands[Op.second].empty()) {
Hal Finkel71b2e202013-12-19 16:12:53 +00001945 InsnOperands.insert(InsnOperands.end(),
Craig Topper1f7604d2014-12-13 05:12:19 +00001946 NumberedInsnOperands[Op.second].begin(),
1947 NumberedInsnOperands[Op.second].end());
Hal Finkel71b2e202013-12-19 16:12:53 +00001948 continue;
Craig Topper1f7604d2014-12-13 05:12:19 +00001949 }
1950 if (!NumberedInsnOperands[TiedNames[Op.second]].empty()) {
1951 if (!NumberedInsnOperandsNoTie.count(TiedNames[Op.second])) {
Hal Finkel71b2e202013-12-19 16:12:53 +00001952 // Figure out to which (sub)operand we're tied.
Craig Topper1f7604d2014-12-13 05:12:19 +00001953 unsigned i = CGI.Operands.getOperandNamed(TiedNames[Op.second]);
Hal Finkel71b2e202013-12-19 16:12:53 +00001954 int tiedTo = CGI.Operands[i].getTiedRegister();
1955 if (tiedTo == -1) {
Craig Topper1f7604d2014-12-13 05:12:19 +00001956 i = CGI.Operands.getOperandNamed(Op.second);
Hal Finkel71b2e202013-12-19 16:12:53 +00001957 tiedTo = CGI.Operands[i].getTiedRegister();
1958 }
1959
1960 if (tiedTo != -1) {
1961 std::pair<unsigned, unsigned> SO =
1962 CGI.Operands.getSubOperandNumber(tiedTo);
1963
Craig Topper1f7604d2014-12-13 05:12:19 +00001964 InsnOperands.push_back(NumberedInsnOperands[TiedNames[Op.second]]
Hal Finkel71b2e202013-12-19 16:12:53 +00001965 [SO.second]);
1966 }
1967 }
1968 continue;
1969 }
1970
Craig Topper1f7604d2014-12-13 05:12:19 +00001971 TypedInit *TI = cast<TypedInit>(Op.first);
Owen Andersone3591652011-07-28 21:54:31 +00001972
Matt Arsenault4cb438b2016-07-18 23:20:46 +00001973 // At this point, we can locate the decoder field, but we need to know how
1974 // to interpret it. As a first step, require the target to provide
1975 // callbacks for decoding register classes.
1976 std::string Decoder = findOperandDecoderMethod(TI);
Nicolai Haehnle0409b282018-03-05 14:01:30 +00001977 Record *TypeRecord = cast<DefInit>(TI)->getDef();
Owen Andersone3591652011-07-28 21:54:31 +00001978
Petr Pavlu182b0572015-07-15 08:04:27 +00001979 RecordVal *HasCompleteDecoderVal =
1980 TypeRecord->getValue("hasCompleteDecoder");
1981 BitInit *HasCompleteDecoderBit = HasCompleteDecoderVal ?
1982 dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) : nullptr;
1983 bool HasCompleteDecoder = HasCompleteDecoderBit ?
1984 HasCompleteDecoderBit->getValue() : true;
1985
1986 OperandInfo OpInfo(Decoder, HasCompleteDecoder);
Owen Andersone3591652011-07-28 21:54:31 +00001987 unsigned Base = ~0U;
1988 unsigned Width = 0;
1989 unsigned Offset = 0;
1990
Owen Anderson4e818902011-02-18 21:51:29 +00001991 for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
Craig Topper24064772014-04-15 07:20:03 +00001992 VarInit *Var = nullptr;
Sean Silvafb509ed2012-10-10 20:24:43 +00001993 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
Owen Anderson3022d672011-08-01 22:45:43 +00001994 if (BI)
Sean Silvafb509ed2012-10-10 20:24:43 +00001995 Var = dyn_cast<VarInit>(BI->getBitVar());
Owen Anderson3022d672011-08-01 22:45:43 +00001996 else
Sean Silvafb509ed2012-10-10 20:24:43 +00001997 Var = dyn_cast<VarInit>(Bits.getBit(bi));
Owen Anderson3022d672011-08-01 22:45:43 +00001998
1999 if (!Var) {
Owen Andersone3591652011-07-28 21:54:31 +00002000 if (Base != ~0U) {
2001 OpInfo.addField(Base, Width, Offset);
2002 Base = ~0U;
2003 Width = 0;
2004 Offset = 0;
2005 }
2006 continue;
2007 }
Owen Anderson4e818902011-02-18 21:51:29 +00002008
Craig Topper1f7604d2014-12-13 05:12:19 +00002009 if (Var->getName() != Op.second &&
2010 Var->getName() != TiedNames[Op.second]) {
Owen Andersone3591652011-07-28 21:54:31 +00002011 if (Base != ~0U) {
2012 OpInfo.addField(Base, Width, Offset);
2013 Base = ~0U;
2014 Width = 0;
2015 Offset = 0;
2016 }
2017 continue;
Owen Anderson4e818902011-02-18 21:51:29 +00002018 }
2019
Owen Andersone3591652011-07-28 21:54:31 +00002020 if (Base == ~0U) {
2021 Base = bi;
2022 Width = 1;
Owen Anderson3022d672011-08-01 22:45:43 +00002023 Offset = BI ? BI->getBitNum() : 0;
2024 } else if (BI && BI->getBitNum() != Offset + Width) {
Owen Andersone08f5b52011-07-29 23:01:18 +00002025 OpInfo.addField(Base, Width, Offset);
2026 Base = bi;
2027 Width = 1;
2028 Offset = BI->getBitNum();
Owen Andersone3591652011-07-28 21:54:31 +00002029 } else {
2030 ++Width;
Owen Anderson4e818902011-02-18 21:51:29 +00002031 }
Owen Anderson4e818902011-02-18 21:51:29 +00002032 }
2033
Owen Andersone3591652011-07-28 21:54:31 +00002034 if (Base != ~0U)
2035 OpInfo.addField(Base, Width, Offset);
2036
2037 if (OpInfo.numFields() > 0)
2038 InsnOperands.push_back(OpInfo);
Owen Anderson4e818902011-02-18 21:51:29 +00002039 }
2040
2041 Operands[Opc] = InsnOperands;
2042
Owen Anderson4e818902011-02-18 21:51:29 +00002043#if 0
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002044 LLVM_DEBUG({
Owen Anderson4e818902011-02-18 21:51:29 +00002045 // Dumps the instruction encoding bits.
2046 dumpBits(errs(), Bits);
2047
2048 errs() << '\n';
2049
2050 // Dumps the list of operand info.
2051 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
2052 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
2053 const std::string &OperandName = Info.Name;
2054 const Record &OperandDef = *Info.Rec;
2055
2056 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
2057 }
2058 });
2059#endif
2060
2061 return true;
2062}
2063
Jim Grosbachecaef492012-08-14 19:06:05 +00002064// emitFieldFromInstruction - Emit the templated helper function
2065// fieldFromInstruction().
Stella Stamenovabb9fd462018-07-25 17:33:20 +00002066// On Windows we make sure that this function is not inlined when
2067// using the VS compiler. It has a bug which causes the function
2068// to be optimized out in some circustances. See llvm.org/pr38292
Jim Grosbachecaef492012-08-14 19:06:05 +00002069static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
Daniel Sandersd300ba12018-10-23 17:23:31 +00002070 OS << "// Helper functions for extracting fields from encoded instructions.\n"
2071 << "// InsnType must either be integral or an APInt-like object that "
2072 "must:\n"
2073 << "// * Have a static const max_size_in_bits equal to the number of bits "
2074 "in the\n"
2075 << "// encoding.\n"
2076 << "// * be default-constructible and copy-constructible\n"
2077 << "// * be constructible from a uint64_t\n"
2078 << "// * be constructible from an APInt (this can be private)\n"
2079 << "// * Support getBitsSet(loBit, hiBit)\n"
2080 << "// * be convertible to uint64_t\n"
2081 << "// * Support the ~, &, ==, !=, and |= operators with other objects of "
2082 "the same type\n"
2083 << "// * Support shift (<<, >>) with signed and unsigned integers on the "
2084 "RHS\n"
2085 << "// * Support put (<<) to raw_ostream&\n"
Stella Stamenovabb9fd462018-07-25 17:33:20 +00002086 << "#if defined(_MSC_VER) && !defined(__clang__)\n"
2087 << "__declspec(noinline)\n"
2088 << "#endif\n"
Daniel Sandersd300ba12018-10-23 17:23:31 +00002089 << "template<typename InsnType>\n"
2090 << "static InsnType fieldFromInstruction(InsnType insn, unsigned "
2091 "startBit,\n"
2092 << " unsigned numBits, "
2093 "std::true_type) {\n"
2094 << " assert(startBit + numBits <= 64 && \"Cannot support >64-bit "
2095 "extractions!\");\n"
2096 << " assert(startBit + numBits <= (sizeof(InsnType) * 8) &&\n"
2097 << " \"Instruction field out of bounds!\");\n"
2098 << " InsnType fieldMask;\n"
2099 << " if (numBits == sizeof(InsnType) * 8)\n"
2100 << " fieldMask = (InsnType)(-1LL);\n"
2101 << " else\n"
2102 << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
2103 << " return (insn & fieldMask) >> startBit;\n"
2104 << "}\n"
2105 << "\n"
2106 << "template<typename InsnType>\n"
2107 << "static InsnType fieldFromInstruction(InsnType insn, unsigned "
2108 "startBit,\n"
2109 << " unsigned numBits, "
2110 "std::false_type) {\n"
2111 << " assert(startBit + numBits <= InsnType::max_size_in_bits && "
2112 "\"Instruction field out of bounds!\");\n"
2113 << " InsnType fieldMask = InsnType::getBitsSet(0, numBits);\n"
2114 << " return (insn >> startBit) & fieldMask;\n"
2115 << "}\n"
2116 << "\n"
2117 << "template<typename InsnType>\n"
2118 << "static InsnType fieldFromInstruction(InsnType insn, unsigned "
2119 "startBit,\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002120 << " unsigned numBits) {\n"
Daniel Sandersd300ba12018-10-23 17:23:31 +00002121 << " return fieldFromInstruction(insn, startBit, numBits, "
2122 "std::is_integral<InsnType>());\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002123 << "}\n\n";
2124}
Owen Anderson4e818902011-02-18 21:51:29 +00002125
Jim Grosbachecaef492012-08-14 19:06:05 +00002126// emitDecodeInstruction - Emit the templated helper function
2127// decodeInstruction().
2128static void emitDecodeInstruction(formatted_raw_ostream &OS) {
2129 OS << "template<typename InsnType>\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002130 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
2131 "MCInst &MI,\n"
2132 << " InsnType insn, uint64_t "
2133 "Address,\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002134 << " const void *DisAsm,\n"
2135 << " const MCSubtargetInfo &STI) {\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00002136 << " const FeatureBitset& Bits = STI.getFeatureBits();\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002137 << "\n"
2138 << " const uint8_t *Ptr = DecodeTable;\n"
Jim Grosbach4c363492012-09-17 18:00:53 +00002139 << " uint32_t CurFieldValue = 0;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002140 << " DecodeStatus S = MCDisassembler::Success;\n"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00002141 << " while (true) {\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002142 << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
2143 << " switch (*Ptr) {\n"
2144 << " default:\n"
2145 << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
2146 << " return MCDisassembler::Fail;\n"
2147 << " case MCD::OPC_ExtractField: {\n"
2148 << " unsigned Start = *++Ptr;\n"
2149 << " unsigned Len = *++Ptr;\n"
2150 << " ++Ptr;\n"
2151 << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002152 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << "
2153 "\", \"\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002154 << " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
2155 << " break;\n"
2156 << " }\n"
2157 << " case MCD::OPC_FilterValue: {\n"
2158 << " // Decode the field value.\n"
2159 << " unsigned Len;\n"
2160 << " InsnType Val = decodeULEB128(++Ptr, &Len);\n"
2161 << " Ptr += Len;\n"
Sander de Smalen13f94252018-07-05 10:39:15 +00002162 << " // NumToSkip is a plain 24-bit integer.\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002163 << " unsigned NumToSkip = *Ptr++;\n"
2164 << " NumToSkip |= (*Ptr++) << 8;\n"
Sander de Smalen13f94252018-07-05 10:39:15 +00002165 << " NumToSkip |= (*Ptr++) << 16;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002166 << "\n"
2167 << " // Perform the filter operation.\n"
2168 << " if (Val != CurFieldValue)\n"
2169 << " Ptr += NumToSkip;\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002170 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << "
2171 "\", \" << NumToSkip\n"
2172 << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" "
2173 ": \"PASS:\")\n"
2174 << " << \" continuing at \" << (Ptr - DecodeTable) << "
2175 "\"\\n\");\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002176 << "\n"
2177 << " break;\n"
2178 << " }\n"
2179 << " case MCD::OPC_CheckField: {\n"
2180 << " unsigned Start = *++Ptr;\n"
2181 << " unsigned Len = *++Ptr;\n"
2182 << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
2183 << " // Decode the field value.\n"
2184 << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
2185 << " Ptr += Len;\n"
Sander de Smalen13f94252018-07-05 10:39:15 +00002186 << " // NumToSkip is a plain 24-bit integer.\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002187 << " unsigned NumToSkip = *Ptr++;\n"
2188 << " NumToSkip |= (*Ptr++) << 8;\n"
Sander de Smalen13f94252018-07-05 10:39:15 +00002189 << " NumToSkip |= (*Ptr++) << 16;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002190 << "\n"
2191 << " // If the actual and expected values don't match, skip.\n"
2192 << " if (ExpectedValue != FieldValue)\n"
2193 << " Ptr += NumToSkip;\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002194 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << "
2195 "\", \"\n"
2196 << " << Len << \", \" << ExpectedValue << \", \" << "
2197 "NumToSkip\n"
2198 << " << \"): FieldValue = \" << FieldValue << \", "
2199 "ExpectedValue = \"\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002200 << " << ExpectedValue << \": \"\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002201 << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : "
2202 "\"FAIL\\n\"));\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002203 << " break;\n"
2204 << " }\n"
2205 << " case MCD::OPC_CheckPredicate: {\n"
2206 << " unsigned Len;\n"
2207 << " // Decode the Predicate Index value.\n"
2208 << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
2209 << " Ptr += Len;\n"
Sander de Smalen13f94252018-07-05 10:39:15 +00002210 << " // NumToSkip is a plain 24-bit integer.\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002211 << " unsigned NumToSkip = *Ptr++;\n"
2212 << " NumToSkip |= (*Ptr++) << 8;\n"
Sander de Smalen13f94252018-07-05 10:39:15 +00002213 << " NumToSkip |= (*Ptr++) << 16;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002214 << " // Check the predicate.\n"
2215 << " bool Pred;\n"
2216 << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
2217 << " Ptr += NumToSkip;\n"
2218 << " (void)Pred;\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002219 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx "
2220 "<< \"): \"\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002221 << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2222 << "\n"
2223 << " break;\n"
2224 << " }\n"
2225 << " case MCD::OPC_Decode: {\n"
2226 << " unsigned Len;\n"
2227 << " // Decode the Opcode value.\n"
2228 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2229 << " Ptr += Len;\n"
2230 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2231 << " Ptr += Len;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002232 << "\n"
Cameron Esfahanif97999d2015-08-11 01:15:07 +00002233 << " MI.clear();\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002234 << " MI.setOpcode(Opc);\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002235 << " bool DecodeComplete;\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002236 << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, "
2237 "DecodeComplete);\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002238 << " assert(DecodeComplete);\n"
2239 << "\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002240 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002241 << " << \", using decoder \" << DecodeIdx << \": \"\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002242 << " << (S != MCDisassembler::Fail ? \"PASS\" : "
2243 "\"FAIL\") << \"\\n\");\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002244 << " return S;\n"
2245 << " }\n"
2246 << " case MCD::OPC_TryDecode: {\n"
2247 << " unsigned Len;\n"
2248 << " // Decode the Opcode value.\n"
2249 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2250 << " Ptr += Len;\n"
2251 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2252 << " Ptr += Len;\n"
Sander de Smalen13f94252018-07-05 10:39:15 +00002253 << " // NumToSkip is a plain 24-bit integer.\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002254 << " unsigned NumToSkip = *Ptr++;\n"
2255 << " NumToSkip |= (*Ptr++) << 8;\n"
Sander de Smalen13f94252018-07-05 10:39:15 +00002256 << " NumToSkip |= (*Ptr++) << 16;\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002257 << "\n"
2258 << " // Perform the decode operation.\n"
2259 << " MCInst TmpMI;\n"
2260 << " TmpMI.setOpcode(Opc);\n"
2261 << " bool DecodeComplete;\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002262 << " S = decodeToMCInst(S, DecodeIdx, insn, TmpMI, Address, DisAsm, "
2263 "DecodeComplete);\n"
2264 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_TryDecode: opcode \" << "
2265 "Opc\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002266 << " << \", using decoder \" << DecodeIdx << \": \");\n"
2267 << "\n"
2268 << " if (DecodeComplete) {\n"
2269 << " // Decoding complete.\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002270 << " LLVM_DEBUG(dbgs() << (S != MCDisassembler::Fail ? \"PASS\" : "
2271 "\"FAIL\") << \"\\n\");\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002272 << " MI = TmpMI;\n"
2273 << " return S;\n"
2274 << " } else {\n"
2275 << " assert(S == MCDisassembler::Fail);\n"
2276 << " // If the decoding was incomplete, skip.\n"
2277 << " Ptr += NumToSkip;\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002278 << " LLVM_DEBUG(dbgs() << \"FAIL: continuing at \" << (Ptr - "
2279 "DecodeTable) << \"\\n\");\n"
2280 << " // Reset decode status. This also drops a SoftFail status "
2281 "that could be\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002282 << " // set before the decode attempt.\n"
2283 << " S = MCDisassembler::Success;\n"
2284 << " }\n"
2285 << " break;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002286 << " }\n"
2287 << " case MCD::OPC_SoftFail: {\n"
2288 << " // Decode the mask values.\n"
2289 << " unsigned Len;\n"
2290 << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n"
2291 << " Ptr += Len;\n"
2292 << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n"
2293 << " Ptr += Len;\n"
2294 << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n"
2295 << " if (Fail)\n"
2296 << " S = MCDisassembler::SoftFail;\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002297 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? "
2298 "\"FAIL\\n\":\"PASS\\n\"));\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002299 << " break;\n"
2300 << " }\n"
2301 << " case MCD::OPC_Fail: {\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002302 << " LLVM_DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002303 << " return MCDisassembler::Fail;\n"
2304 << " }\n"
2305 << " }\n"
2306 << " }\n"
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002307 << " llvm_unreachable(\"bogosity detected in disassembler state "
2308 "machine!\");\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002309 << "}\n\n";
Owen Anderson4e818902011-02-18 21:51:29 +00002310}
2311
2312// Emits disassembler code for instruction decoding.
Craig Topper82d0d5f2012-03-16 01:19:24 +00002313void FixedLenDecoderEmitter::run(raw_ostream &o) {
Jim Grosbachecaef492012-08-14 19:06:05 +00002314 formatted_raw_ostream OS(o);
2315 OS << "#include \"llvm/MC/MCInst.h\"\n";
2316 OS << "#include \"llvm/Support/Debug.h\"\n";
2317 OS << "#include \"llvm/Support/DataTypes.h\"\n";
2318 OS << "#include \"llvm/Support/LEB128.h\"\n";
2319 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
2320 OS << "#include <assert.h>\n";
2321 OS << '\n';
2322 OS << "namespace llvm {\n\n";
2323
2324 emitFieldFromInstruction(OS);
Owen Anderson4e818902011-02-18 21:51:29 +00002325
Hal Finkel81e6fcc2013-12-17 22:37:50 +00002326 Target.reverseBitsForLittleEndianEncoding();
2327
Owen Andersonc78e03c2011-07-19 21:06:00 +00002328 // Parameterize the decoders based on namespace and instruction width.
Craig Topperf9265322016-01-17 20:38:14 +00002329 NumberedInstructions = Target.getInstructionsByEnumValue();
Owen Andersonc78e03c2011-07-19 21:06:00 +00002330 std::map<std::pair<std::string, unsigned>,
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00002331 std::vector<unsigned>> OpcMap;
2332 std::map<unsigned, std::vector<OperandInfo>> Operands;
Owen Andersonc78e03c2011-07-19 21:06:00 +00002333
Craig Topperf9265322016-01-17 20:38:14 +00002334 for (unsigned i = 0; i < NumberedInstructions.size(); ++i) {
2335 const CodeGenInstruction *Inst = NumberedInstructions[i];
Craig Topper48c112b2012-03-16 05:58:09 +00002336 const Record *Def = Inst->TheDef;
Owen Andersonc78e03c2011-07-19 21:06:00 +00002337 unsigned Size = Def->getValueAsInt("Size");
2338 if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
2339 Def->getValueAsBit("isPseudo") ||
2340 Def->getValueAsBit("isAsmParserOnly") ||
2341 Def->getValueAsBit("isCodeGenOnly"))
2342 continue;
2343
Craig Topperbcd3c372017-05-31 21:12:46 +00002344 StringRef DecoderNamespace = Def->getValueAsString("DecoderNamespace");
Owen Andersonc78e03c2011-07-19 21:06:00 +00002345
2346 if (Size) {
Hal Finkel71b2e202013-12-19 16:12:53 +00002347 if (populateInstruction(Target, *Inst, i, Operands)) {
Owen Andersonc78e03c2011-07-19 21:06:00 +00002348 OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
2349 }
2350 }
2351 }
2352
Jim Grosbachecaef492012-08-14 19:06:05 +00002353 DecoderTableInfo TableInfo;
Craig Topper1f7604d2014-12-13 05:12:19 +00002354 for (const auto &Opc : OpcMap) {
Owen Andersonc78e03c2011-07-19 21:06:00 +00002355 // Emit the decoder for this namespace+width combination.
Craig Topperf9265322016-01-17 20:38:14 +00002356 FilterChooser FC(NumberedInstructions, Opc.second, Operands,
Craig Topper1f7604d2014-12-13 05:12:19 +00002357 8*Opc.first.second, this);
Jim Grosbachecaef492012-08-14 19:06:05 +00002358
2359 // The decode table is cleared for each top level decoder function. The
2360 // predicates and decoders themselves, however, are shared across all
2361 // decoders to give more opportunities for uniqueing.
2362 TableInfo.Table.clear();
2363 TableInfo.FixupStack.clear();
2364 TableInfo.Table.reserve(16384);
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002365 TableInfo.FixupStack.emplace_back();
Jim Grosbachecaef492012-08-14 19:06:05 +00002366 FC.emitTableEntries(TableInfo);
2367 // Any NumToSkip fixups in the top level scope can resolve to the
2368 // OPC_Fail at the end of the table.
2369 assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
2370 // Resolve any NumToSkip fixups in the current scope.
2371 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
2372 TableInfo.Table.size());
2373 TableInfo.FixupStack.clear();
2374
2375 TableInfo.Table.push_back(MCD::OPC_Fail);
2376
2377 // Print the table to the output stream.
Craig Topper1f7604d2014-12-13 05:12:19 +00002378 emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), Opc.first.first);
Jim Grosbachecaef492012-08-14 19:06:05 +00002379 OS.flush();
Owen Andersonc78e03c2011-07-19 21:06:00 +00002380 }
Owen Anderson4e818902011-02-18 21:51:29 +00002381
Jim Grosbachecaef492012-08-14 19:06:05 +00002382 // Emit the predicate function.
2383 emitPredicateFunction(OS, TableInfo.Predicates, 0);
2384
2385 // Emit the decoder function.
2386 emitDecoderFunction(OS, TableInfo.Decoders, 0);
2387
2388 // Emit the main entry point for the decoder, decodeInstruction().
2389 emitDecodeInstruction(OS);
2390
2391 OS << "\n} // End llvm namespace\n";
Owen Anderson4e818902011-02-18 21:51:29 +00002392}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00002393
2394namespace llvm {
2395
2396void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
Benjamin Kramerc321e532016-06-08 19:09:22 +00002397 const std::string &PredicateNamespace,
2398 const std::string &GPrefix,
2399 const std::string &GPostfix, const std::string &ROK,
2400 const std::string &RFail, const std::string &L) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00002401 FixedLenDecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix,
2402 ROK, RFail, L).run(OS);
2403}
2404
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00002405} // end namespace llvm