blob: 9efc09e1822d31278b8ec3b4915511ef51066dd9 [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
Owen Anderson4e818902011-02-18 21:51:29 +000015#include "CodeGenTarget.h"
James Molloyd9ba4fd2012-02-09 10:56:31 +000016#include "llvm/ADT/APInt.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000017#include "llvm/ADT/SmallString.h"
Owen Anderson4e818902011-02-18 21:51:29 +000018#include "llvm/ADT/StringExtras.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000019#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Twine.h"
21#include "llvm/MC/MCFixedLenDisassembler.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000022#include "llvm/Support/DataTypes.h"
Owen Anderson4e818902011-02-18 21:51:29 +000023#include "llvm/Support/Debug.h"
Jim Grosbachecaef492012-08-14 19:06:05 +000024#include "llvm/Support/FormattedStream.h"
25#include "llvm/Support/LEB128.h"
Owen Anderson4e818902011-02-18 21:51:29 +000026#include "llvm/Support/raw_ostream.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000027#include "llvm/TableGen/Error.h"
28#include "llvm/TableGen/Record.h"
Owen Anderson4e818902011-02-18 21:51:29 +000029#include <map>
30#include <string>
Chandler Carruth91d19d82012-12-04 10:37:14 +000031#include <vector>
Owen Anderson4e818902011-02-18 21:51:29 +000032
33using namespace llvm;
34
Chandler Carruth97acce22014-04-22 03:06:00 +000035#define DEBUG_TYPE "decoder-emitter"
36
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000037namespace {
38struct EncodingField {
39 unsigned Base, Width, Offset;
40 EncodingField(unsigned B, unsigned W, unsigned O)
41 : Base(B), Width(W), Offset(O) { }
42};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000043
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000044struct OperandInfo {
45 std::vector<EncodingField> Fields;
46 std::string Decoder;
Petr Pavlu182b0572015-07-15 08:04:27 +000047 bool HasCompleteDecoder;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000048
Petr Pavlu182b0572015-07-15 08:04:27 +000049 OperandInfo(std::string D, bool HCD)
50 : Decoder(D), HasCompleteDecoder(HCD) { }
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000051
52 void addField(unsigned Base, unsigned Width, unsigned Offset) {
53 Fields.push_back(EncodingField(Base, Width, Offset));
54 }
55
56 unsigned numFields() const { return Fields.size(); }
57
58 typedef std::vector<EncodingField>::const_iterator const_iterator;
59
60 const_iterator begin() const { return Fields.begin(); }
61 const_iterator end() const { return Fields.end(); }
62};
Jim Grosbachecaef492012-08-14 19:06:05 +000063
64typedef std::vector<uint8_t> DecoderTable;
65typedef uint32_t DecoderFixup;
66typedef std::vector<DecoderFixup> FixupList;
67typedef std::vector<FixupList> FixupScopeList;
Rafael Espindola55512f92015-11-18 06:52:18 +000068typedef SmallSetVector<std::string, 16> PredicateSet;
69typedef SmallSetVector<std::string, 16> DecoderSet;
Jim Grosbachecaef492012-08-14 19:06:05 +000070struct DecoderTableInfo {
71 DecoderTable Table;
72 FixupScopeList FixupStack;
73 PredicateSet Predicates;
74 DecoderSet Decoders;
75};
76
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000077} // End anonymous namespace
78
79namespace {
80class FixedLenDecoderEmitter {
Craig Topperf9265322016-01-17 20:38:14 +000081 ArrayRef<const CodeGenInstruction *> NumberedInstructions;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000082public:
83
84 // Defaults preserved here for documentation, even though they aren't
85 // strictly necessary given the way that this is currently being called.
86 FixedLenDecoderEmitter(RecordKeeper &R,
87 std::string PredicateNamespace,
88 std::string GPrefix = "if (",
Petr Pavlu182b0572015-07-15 08:04:27 +000089 std::string GPostfix = " == MCDisassembler::Fail)",
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000090 std::string ROK = "MCDisassembler::Success",
91 std::string RFail = "MCDisassembler::Fail",
92 std::string L = "") :
93 Target(R),
94 PredicateNamespace(PredicateNamespace),
95 GuardPrefix(GPrefix), GuardPostfix(GPostfix),
96 ReturnOK(ROK), ReturnFail(RFail), Locals(L) {}
97
Jim Grosbachecaef492012-08-14 19:06:05 +000098 // Emit the decoder state machine table.
99 void emitTable(formatted_raw_ostream &o, DecoderTable &Table,
100 unsigned Indentation, unsigned BitWidth,
101 StringRef Namespace) const;
102 void emitPredicateFunction(formatted_raw_ostream &OS,
103 PredicateSet &Predicates,
104 unsigned Indentation) const;
105 void emitDecoderFunction(formatted_raw_ostream &OS,
106 DecoderSet &Decoders,
107 unsigned Indentation) const;
108
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000109 // run - Output the code emitter
110 void run(raw_ostream &o);
111
112private:
113 CodeGenTarget Target;
114public:
115 std::string PredicateNamespace;
116 std::string GuardPrefix, GuardPostfix;
117 std::string ReturnOK, ReturnFail;
118 std::string Locals;
119};
120} // End anonymous namespace
121
Owen Anderson4e818902011-02-18 21:51:29 +0000122// The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
123// for a bit value.
124//
125// BIT_UNFILTERED is used as the init value for a filter position. It is used
126// only for filter processings.
127typedef enum {
128 BIT_TRUE, // '1'
129 BIT_FALSE, // '0'
130 BIT_UNSET, // '?'
131 BIT_UNFILTERED // unfiltered
132} bit_value_t;
133
134static bool ValueSet(bit_value_t V) {
135 return (V == BIT_TRUE || V == BIT_FALSE);
136}
137static bool ValueNotSet(bit_value_t V) {
138 return (V == BIT_UNSET);
139}
140static int Value(bit_value_t V) {
141 return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
142}
Craig Topper48c112b2012-03-16 05:58:09 +0000143static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000144 if (BitInit *bit = dyn_cast<BitInit>(bits.getBit(index)))
Owen Anderson4e818902011-02-18 21:51:29 +0000145 return bit->getValue() ? BIT_TRUE : BIT_FALSE;
146
147 // The bit is uninitialized.
148 return BIT_UNSET;
149}
150// Prints the bit value for each position.
Craig Topper48c112b2012-03-16 05:58:09 +0000151static void dumpBits(raw_ostream &o, const BitsInit &bits) {
Craig Topper29688ab2012-08-17 05:42:16 +0000152 for (unsigned index = bits.getNumBits(); index > 0; --index) {
Owen Anderson4e818902011-02-18 21:51:29 +0000153 switch (bitFromBits(bits, index - 1)) {
154 case BIT_TRUE:
155 o << "1";
156 break;
157 case BIT_FALSE:
158 o << "0";
159 break;
160 case BIT_UNSET:
161 o << "_";
162 break;
163 default:
Craig Topperc4965bc2012-02-05 07:21:30 +0000164 llvm_unreachable("unexpected return value from bitFromBits");
Owen Anderson4e818902011-02-18 21:51:29 +0000165 }
166 }
167}
168
David Greeneaf8ee2c2011-07-29 22:43:06 +0000169static BitsInit &getBitsField(const Record &def, const char *str) {
170 BitsInit *bits = def.getValueAsBitsInit(str);
Owen Anderson4e818902011-02-18 21:51:29 +0000171 return *bits;
172}
173
174// Forward declaration.
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000175namespace {
Owen Anderson4e818902011-02-18 21:51:29 +0000176class FilterChooser;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000177} // End anonymous namespace
Owen Anderson4e818902011-02-18 21:51:29 +0000178
Owen Anderson4e818902011-02-18 21:51:29 +0000179// Representation of the instruction to work on.
Owen Andersonc78e03c2011-07-19 21:06:00 +0000180typedef std::vector<bit_value_t> insn_t;
Owen Anderson4e818902011-02-18 21:51:29 +0000181
182/// Filter - Filter works with FilterChooser to produce the decoding tree for
183/// the ISA.
184///
185/// It is useful to think of a Filter as governing the switch stmts of the
186/// decoding tree in a certain level. Each case stmt delegates to an inferior
187/// FilterChooser to decide what further decoding logic to employ, or in another
188/// words, what other remaining bits to look at. The FilterChooser eventually
189/// chooses a best Filter to do its job.
190///
191/// This recursive scheme ends when the number of Opcodes assigned to the
192/// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
193/// the Filter/FilterChooser combo does not know how to distinguish among the
194/// Opcodes assigned.
195///
196/// An example of a conflict is
197///
198/// Conflict:
199/// 111101000.00........00010000....
200/// 111101000.00........0001........
201/// 1111010...00........0001........
202/// 1111010...00....................
203/// 1111010.........................
204/// 1111............................
205/// ................................
206/// VST4q8a 111101000_00________00010000____
207/// VST4q8b 111101000_00________00010000____
208///
209/// The Debug output shows the path that the decoding tree follows to reach the
210/// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
Petr Pavlu21894652015-07-14 08:00:34 +0000211/// even registers, while VST4q8b is a vst4 to double-spaced odd registers.
Owen Anderson4e818902011-02-18 21:51:29 +0000212///
213/// The encoding info in the .td files does not specify this meta information,
214/// which could have been used by the decoder to resolve the conflict. The
215/// decoder could try to decode the even/odd register numbering and assign to
216/// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
217/// version and return the Opcode since the two have the same Asm format string.
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000218namespace {
Owen Anderson4e818902011-02-18 21:51:29 +0000219class Filter {
220protected:
Craig Topper501d95c2012-03-16 06:52:56 +0000221 const FilterChooser *Owner;// points to the FilterChooser who owns this filter
Owen Anderson4e818902011-02-18 21:51:29 +0000222 unsigned StartBit; // the starting bit position
223 unsigned NumBits; // number of bits to filter
224 bool Mixed; // a mixed region contains both set and unset bits
225
226 // Map of well-known segment value to the set of uid's with that value.
227 std::map<uint64_t, std::vector<unsigned> > FilteredInstructions;
228
229 // Set of uid's with non-constant segment values.
230 std::vector<unsigned> VariableInstructions;
231
232 // Map of well-known segment value to its delegate.
Craig Toppercf05f912014-09-03 06:07:54 +0000233 std::map<unsigned, std::unique_ptr<const FilterChooser>> FilterChooserMap;
Owen Anderson4e818902011-02-18 21:51:29 +0000234
235 // Number of instructions which fall under FilteredInstructions category.
236 unsigned NumFiltered;
237
238 // Keeps track of the last opcode in the filtered bucket.
239 unsigned LastOpcFiltered;
240
Owen Anderson4e818902011-02-18 21:51:29 +0000241public:
Craig Topper48c112b2012-03-16 05:58:09 +0000242 unsigned getNumFiltered() const { return NumFiltered; }
243 unsigned getSingletonOpc() const {
Owen Anderson4e818902011-02-18 21:51:29 +0000244 assert(NumFiltered == 1);
245 return LastOpcFiltered;
246 }
247 // Return the filter chooser for the group of instructions without constant
248 // segment values.
Craig Topper48c112b2012-03-16 05:58:09 +0000249 const FilterChooser &getVariableFC() const {
Owen Anderson4e818902011-02-18 21:51:29 +0000250 assert(NumFiltered == 1);
251 assert(FilterChooserMap.size() == 1);
252 return *(FilterChooserMap.find((unsigned)-1)->second);
253 }
254
Craig Topper5c2b4ac2014-09-03 05:49:07 +0000255 Filter(Filter &&f);
Owen Anderson4e818902011-02-18 21:51:29 +0000256 Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
257
258 ~Filter();
259
260 // Divides the decoding task into sub tasks and delegates them to the
261 // inferior FilterChooser's.
262 //
263 // A special case arises when there's only one entry in the filtered
264 // instructions. In order to unambiguously decode the singleton, we need to
265 // match the remaining undecoded encoding bits against the singleton.
266 void recurse();
267
Jim Grosbachecaef492012-08-14 19:06:05 +0000268 // Emit table entries to decode instructions given a segment or segments of
269 // bits.
270 void emitTableEntry(DecoderTableInfo &TableInfo) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000271
272 // Returns the number of fanout produced by the filter. More fanout implies
273 // the filter distinguishes more categories of instructions.
274 unsigned usefulness() const;
275}; // End of class Filter
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000276} // End anonymous namespace
Owen Anderson4e818902011-02-18 21:51:29 +0000277
278// These are states of our finite state machines used in FilterChooser's
279// filterProcessor() which produces the filter candidates to use.
280typedef enum {
281 ATTR_NONE,
282 ATTR_FILTERED,
283 ATTR_ALL_SET,
284 ATTR_ALL_UNSET,
285 ATTR_MIXED
286} bitAttr_t;
287
288/// FilterChooser - FilterChooser chooses the best filter among a set of Filters
289/// in order to perform the decoding of instructions at the current level.
290///
291/// Decoding proceeds from the top down. Based on the well-known encoding bits
292/// of instructions available, FilterChooser builds up the possible Filters that
293/// can further the task of decoding by distinguishing among the remaining
294/// candidate instructions.
295///
296/// Once a filter has been chosen, it is called upon to divide the decoding task
297/// into sub-tasks and delegates them to its inferior FilterChoosers for further
298/// processings.
299///
300/// It is useful to think of a Filter as governing the switch stmts of the
301/// decoding tree. And each case is delegated to an inferior FilterChooser to
302/// decide what further remaining bits to look at.
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000303namespace {
Owen Anderson4e818902011-02-18 21:51:29 +0000304class FilterChooser {
305protected:
306 friend class Filter;
307
308 // Vector of codegen instructions to choose our filter.
Craig Topperf9265322016-01-17 20:38:14 +0000309 ArrayRef<const CodeGenInstruction *> AllInstructions;
Owen Anderson4e818902011-02-18 21:51:29 +0000310
311 // Vector of uid's for this filter chooser to work on.
Craig Topper501d95c2012-03-16 06:52:56 +0000312 const std::vector<unsigned> &Opcodes;
Owen Anderson4e818902011-02-18 21:51:29 +0000313
314 // Lookup table for the operand decoding of instructions.
Craig Topper501d95c2012-03-16 06:52:56 +0000315 const std::map<unsigned, std::vector<OperandInfo> > &Operands;
Owen Anderson4e818902011-02-18 21:51:29 +0000316
317 // Vector of candidate filters.
318 std::vector<Filter> Filters;
319
320 // Array of bit values passed down from our parent.
321 // Set to all BIT_UNFILTERED's for Parent == NULL.
Owen Andersonc78e03c2011-07-19 21:06:00 +0000322 std::vector<bit_value_t> FilterBitValues;
Owen Anderson4e818902011-02-18 21:51:29 +0000323
324 // Links to the FilterChooser above us in the decoding tree.
Craig Topper501d95c2012-03-16 06:52:56 +0000325 const FilterChooser *Parent;
Owen Anderson4e818902011-02-18 21:51:29 +0000326
327 // Index of the best filter from Filters.
328 int BestIndex;
329
Owen Andersonc78e03c2011-07-19 21:06:00 +0000330 // Width of instructions
331 unsigned BitWidth;
332
Owen Andersona4043c42011-08-17 17:44:15 +0000333 // Parent emitter
334 const FixedLenDecoderEmitter *Emitter;
335
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000336 FilterChooser(const FilterChooser &) = delete;
337 void operator=(const FilterChooser &) = delete;
Owen Anderson4e818902011-02-18 21:51:29 +0000338public:
Owen Anderson4e818902011-02-18 21:51:29 +0000339
Craig Topperf9265322016-01-17 20:38:14 +0000340 FilterChooser(ArrayRef<const CodeGenInstruction *> Insts,
Owen Anderson4e818902011-02-18 21:51:29 +0000341 const std::vector<unsigned> &IDs,
Craig Topper501d95c2012-03-16 06:52:56 +0000342 const std::map<unsigned, std::vector<OperandInfo> > &Ops,
Owen Andersona4043c42011-08-17 17:44:15 +0000343 unsigned BW,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000344 const FixedLenDecoderEmitter *E)
345 : AllInstructions(Insts), Opcodes(IDs), Operands(Ops), Filters(),
Craig Topper1ddc2882014-09-04 04:49:03 +0000346 FilterBitValues(BW, BIT_UNFILTERED), Parent(nullptr), BestIndex(-1),
347 BitWidth(BW), Emitter(E) {
Owen Anderson4e818902011-02-18 21:51:29 +0000348 doFilter();
349 }
350
Craig Topperf9265322016-01-17 20:38:14 +0000351 FilterChooser(ArrayRef<const CodeGenInstruction *> Insts,
Owen Anderson4e818902011-02-18 21:51:29 +0000352 const std::vector<unsigned> &IDs,
Craig Topper501d95c2012-03-16 06:52:56 +0000353 const std::map<unsigned, std::vector<OperandInfo> > &Ops,
354 const std::vector<bit_value_t> &ParentFilterBitValues,
355 const FilterChooser &parent)
Craig Topper82d0d5f2012-03-16 01:19:24 +0000356 : AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
Owen Andersonc78e03c2011-07-19 21:06:00 +0000357 Filters(), FilterBitValues(ParentFilterBitValues),
Owen Andersona4043c42011-08-17 17:44:15 +0000358 Parent(&parent), BestIndex(-1), BitWidth(parent.BitWidth),
359 Emitter(parent.Emitter) {
Owen Anderson4e818902011-02-18 21:51:29 +0000360 doFilter();
361 }
362
Jim Grosbachecaef492012-08-14 19:06:05 +0000363 unsigned getBitWidth() const { return BitWidth; }
Owen Anderson4e818902011-02-18 21:51:29 +0000364
365protected:
366 // Populates the insn given the uid.
367 void insnWithID(insn_t &Insn, unsigned Opcode) const {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000368 BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
Owen Anderson4e818902011-02-18 21:51:29 +0000369
James Molloyd9ba4fd2012-02-09 10:56:31 +0000370 // We may have a SoftFail bitmask, which specifies a mask where an encoding
371 // may differ from the value in "Inst" and yet still be valid, but the
372 // disassembler should return SoftFail instead of Success.
373 //
374 // This is used for marking UNPREDICTABLE instructions in the ARM world.
Jim Grosbach3f4b2392012-02-29 22:07:56 +0000375 BitsInit *SFBits =
376 AllInstructions[Opcode]->TheDef->getValueAsBitsInit("SoftFail");
James Molloyd9ba4fd2012-02-09 10:56:31 +0000377
378 for (unsigned i = 0; i < BitWidth; ++i) {
379 if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE)
380 Insn.push_back(BIT_UNSET);
381 else
382 Insn.push_back(bitFromBits(Bits, i));
383 }
Owen Anderson4e818902011-02-18 21:51:29 +0000384 }
385
386 // Returns the record name.
387 const std::string &nameWithID(unsigned Opcode) const {
388 return AllInstructions[Opcode]->TheDef->getName();
389 }
390
391 // Populates the field of the insn given the start position and the number of
392 // consecutive bits to scan for.
393 //
394 // Returns false if there exists any uninitialized bit value in the range.
395 // Returns true, otherwise.
396 bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000397 unsigned NumBits) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000398
399 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
400 /// filter array as a series of chars.
Craig Topper48c112b2012-03-16 05:58:09 +0000401 void dumpFilterArray(raw_ostream &o,
402 const std::vector<bit_value_t> & filter) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000403
404 /// dumpStack - dumpStack traverses the filter chooser chain and calls
405 /// dumpFilterArray on each filter chooser up to the top level one.
Craig Topper48c112b2012-03-16 05:58:09 +0000406 void dumpStack(raw_ostream &o, const char *prefix) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000407
408 Filter &bestFilter() {
409 assert(BestIndex != -1 && "BestIndex not set");
410 return Filters[BestIndex];
411 }
412
413 // Called from Filter::recurse() when singleton exists. For debug purpose.
Craig Topper48c112b2012-03-16 05:58:09 +0000414 void SingletonExists(unsigned Opc) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000415
Craig Topper48c112b2012-03-16 05:58:09 +0000416 bool PositionFiltered(unsigned i) const {
Owen Anderson4e818902011-02-18 21:51:29 +0000417 return ValueSet(FilterBitValues[i]);
418 }
419
420 // Calculates the island(s) needed to decode the instruction.
421 // This returns a lit of undecoded bits of an instructions, for example,
422 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
423 // decoded bits in order to verify that the instruction matches the Opcode.
424 unsigned getIslands(std::vector<unsigned> &StartBits,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000425 std::vector<unsigned> &EndBits,
Craig Topper48c112b2012-03-16 05:58:09 +0000426 std::vector<uint64_t> &FieldVals,
427 const insn_t &Insn) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000428
James Molloy8067df92011-09-07 19:42:28 +0000429 // Emits code to check the Predicates member of an instruction are true.
430 // Returns true if predicate matches were emitted, false otherwise.
Craig Topper48c112b2012-03-16 05:58:09 +0000431 bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
432 unsigned Opc) const;
James Molloy8067df92011-09-07 19:42:28 +0000433
Jim Grosbachecaef492012-08-14 19:06:05 +0000434 bool doesOpcodeNeedPredicate(unsigned Opc) const;
435 unsigned getPredicateIndex(DecoderTableInfo &TableInfo, StringRef P) const;
436 void emitPredicateTableEntry(DecoderTableInfo &TableInfo,
437 unsigned Opc) const;
James Molloyd9ba4fd2012-02-09 10:56:31 +0000438
Jim Grosbachecaef492012-08-14 19:06:05 +0000439 void emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
440 unsigned Opc) const;
441
442 // Emits table entries to decode the singleton.
443 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
444 unsigned Opc) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000445
446 // Emits code to decode the singleton, and then to decode the rest.
Jim Grosbachecaef492012-08-14 19:06:05 +0000447 void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
448 const Filter &Best) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000449
Jim Grosbachecaef492012-08-14 19:06:05 +0000450 void emitBinaryParser(raw_ostream &o, unsigned &Indentation,
Petr Pavlu182b0572015-07-15 08:04:27 +0000451 const OperandInfo &OpInfo,
452 bool &OpHasCompleteDecoder) const;
Owen Andersone3591652011-07-28 21:54:31 +0000453
Petr Pavlu182b0572015-07-15 08:04:27 +0000454 void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc,
455 bool &HasCompleteDecoder) const;
456 unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc,
457 bool &HasCompleteDecoder) const;
Jim Grosbachecaef492012-08-14 19:06:05 +0000458
Owen Anderson4e818902011-02-18 21:51:29 +0000459 // Assign a single filter and run with it.
Craig Topper48c112b2012-03-16 05:58:09 +0000460 void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed);
Owen Anderson4e818902011-02-18 21:51:29 +0000461
462 // reportRegion is a helper function for filterProcessor to mark a region as
463 // eligible for use as a filter region.
464 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000465 bool AllowMixed);
Owen Anderson4e818902011-02-18 21:51:29 +0000466
467 // FilterProcessor scans the well-known encoding bits of the instructions and
468 // builds up a list of candidate filters. It chooses the best filter and
469 // recursively descends down the decoding tree.
470 bool filterProcessor(bool AllowMixed, bool Greedy = true);
471
472 // Decides on the best configuration of filter(s) to use in order to decode
473 // the instructions. A conflict of instructions may occur, in which case we
474 // dump the conflict set to the standard error.
475 void doFilter();
476
Jim Grosbachecaef492012-08-14 19:06:05 +0000477public:
478 // emitTableEntries - Emit state machine entries to decode our share of
479 // instructions.
480 void emitTableEntries(DecoderTableInfo &TableInfo) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000481};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000482} // End anonymous namespace
Owen Anderson4e818902011-02-18 21:51:29 +0000483
484///////////////////////////
485// //
Craig Topper93e64342012-03-16 00:56:01 +0000486// Filter Implementation //
Owen Anderson4e818902011-02-18 21:51:29 +0000487// //
488///////////////////////////
489
Craig Topper5c2b4ac2014-09-03 05:49:07 +0000490Filter::Filter(Filter &&f)
Craig Topper82d0d5f2012-03-16 01:19:24 +0000491 : Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
Craig Topper5c2b4ac2014-09-03 05:49:07 +0000492 FilteredInstructions(std::move(f.FilteredInstructions)),
493 VariableInstructions(std::move(f.VariableInstructions)),
494 FilterChooserMap(std::move(f.FilterChooserMap)), NumFiltered(f.NumFiltered),
Craig Topper82d0d5f2012-03-16 01:19:24 +0000495 LastOpcFiltered(f.LastOpcFiltered) {
Owen Anderson4e818902011-02-18 21:51:29 +0000496}
497
498Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000499 bool mixed)
500 : Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) {
Owen Andersonc78e03c2011-07-19 21:06:00 +0000501 assert(StartBit + NumBits - 1 < Owner->BitWidth);
Owen Anderson4e818902011-02-18 21:51:29 +0000502
503 NumFiltered = 0;
504 LastOpcFiltered = 0;
Owen Anderson4e818902011-02-18 21:51:29 +0000505
506 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
507 insn_t Insn;
508
509 // Populates the insn given the uid.
510 Owner->insnWithID(Insn, Owner->Opcodes[i]);
511
512 uint64_t Field;
513 // Scans the segment for possibly well-specified encoding bits.
514 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
515
516 if (ok) {
517 // The encoding bits are well-known. Lets add the uid of the
518 // instruction into the bucket keyed off the constant field value.
519 LastOpcFiltered = Owner->Opcodes[i];
520 FilteredInstructions[Field].push_back(LastOpcFiltered);
521 ++NumFiltered;
522 } else {
Craig Topper93e64342012-03-16 00:56:01 +0000523 // Some of the encoding bit(s) are unspecified. This contributes to
Owen Anderson4e818902011-02-18 21:51:29 +0000524 // one additional member of "Variable" instructions.
525 VariableInstructions.push_back(Owner->Opcodes[i]);
Owen Anderson4e818902011-02-18 21:51:29 +0000526 }
527 }
528
529 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
530 && "Filter returns no instruction categories");
531}
532
533Filter::~Filter() {
Owen Anderson4e818902011-02-18 21:51:29 +0000534}
535
536// Divides the decoding task into sub tasks and delegates them to the
537// inferior FilterChooser's.
538//
539// A special case arises when there's only one entry in the filtered
540// instructions. In order to unambiguously decode the singleton, we need to
541// match the remaining undecoded encoding bits against the singleton.
542void Filter::recurse() {
Owen Anderson4e818902011-02-18 21:51:29 +0000543 // Starts by inheriting our parent filter chooser's filter bit values.
Owen Andersonc78e03c2011-07-19 21:06:00 +0000544 std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
Owen Anderson4e818902011-02-18 21:51:29 +0000545
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000546 if (!VariableInstructions.empty()) {
Owen Anderson4e818902011-02-18 21:51:29 +0000547 // Conservatively marks each segment position as BIT_UNSET.
Craig Topper29688ab2012-08-17 05:42:16 +0000548 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex)
Owen Anderson4e818902011-02-18 21:51:29 +0000549 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
550
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000551 // Delegates to an inferior filter chooser for further processing on this
Owen Anderson4e818902011-02-18 21:51:29 +0000552 // group of instructions whose segment values are variable.
Yaron Kerene499db02014-09-03 08:22:30 +0000553 FilterChooserMap.insert(
554 std::make_pair(-1U, llvm::make_unique<FilterChooser>(
555 Owner->AllInstructions, VariableInstructions,
556 Owner->Operands, BitValueArray, *Owner)));
Owen Anderson4e818902011-02-18 21:51:29 +0000557 }
558
559 // No need to recurse for a singleton filtered instruction.
Jim Grosbachecaef492012-08-14 19:06:05 +0000560 // See also Filter::emit*().
Owen Anderson4e818902011-02-18 21:51:29 +0000561 if (getNumFiltered() == 1) {
562 //Owner->SingletonExists(LastOpcFiltered);
563 assert(FilterChooserMap.size() == 1);
564 return;
565 }
566
567 // Otherwise, create sub choosers.
Craig Topper1f7604d2014-12-13 05:12:19 +0000568 for (const auto &Inst : FilteredInstructions) {
Owen Anderson4e818902011-02-18 21:51:29 +0000569
570 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
Craig Topper29688ab2012-08-17 05:42:16 +0000571 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) {
Craig Topper1f7604d2014-12-13 05:12:19 +0000572 if (Inst.first & (1ULL << bitIndex))
Owen Anderson4e818902011-02-18 21:51:29 +0000573 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
574 else
575 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
576 }
577
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000578 // Delegates to an inferior filter chooser for further processing on this
Owen Anderson4e818902011-02-18 21:51:29 +0000579 // category of instructions.
Craig Toppercf05f912014-09-03 06:07:54 +0000580 FilterChooserMap.insert(std::make_pair(
Craig Topper1f7604d2014-12-13 05:12:19 +0000581 Inst.first, llvm::make_unique<FilterChooser>(
582 Owner->AllInstructions, Inst.second,
Yaron Kerene499db02014-09-03 08:22:30 +0000583 Owner->Operands, BitValueArray, *Owner)));
Owen Anderson4e818902011-02-18 21:51:29 +0000584 }
585}
586
Jim Grosbachecaef492012-08-14 19:06:05 +0000587static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
588 uint32_t DestIdx) {
589 // Any NumToSkip fixups in the current scope can resolve to the
590 // current location.
591 for (FixupList::const_reverse_iterator I = Fixups.rbegin(),
592 E = Fixups.rend();
593 I != E; ++I) {
594 // Calculate the distance from the byte following the fixup entry byte
595 // to the destination. The Target is calculated from after the 16-bit
596 // NumToSkip entry itself, so subtract two from the displacement here
597 // to account for that.
598 uint32_t FixupIdx = *I;
599 uint32_t Delta = DestIdx - FixupIdx - 2;
600 // Our NumToSkip entries are 16-bits. Make sure our table isn't too
601 // big.
602 assert(Delta < 65536U && "disassembler decoding table too large!");
603 Table[FixupIdx] = (uint8_t)Delta;
604 Table[FixupIdx + 1] = (uint8_t)(Delta >> 8);
605 }
606}
Owen Anderson4e818902011-02-18 21:51:29 +0000607
Jim Grosbachecaef492012-08-14 19:06:05 +0000608// Emit table entries to decode instructions given a segment or segments
609// of bits.
610void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
611 TableInfo.Table.push_back(MCD::OPC_ExtractField);
612 TableInfo.Table.push_back(StartBit);
613 TableInfo.Table.push_back(NumBits);
Owen Anderson4e818902011-02-18 21:51:29 +0000614
Jim Grosbachecaef492012-08-14 19:06:05 +0000615 // A new filter entry begins a new scope for fixup resolution.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000616 TableInfo.FixupStack.emplace_back();
Owen Anderson4e818902011-02-18 21:51:29 +0000617
Jim Grosbachecaef492012-08-14 19:06:05 +0000618 DecoderTable &Table = TableInfo.Table;
619
620 size_t PrevFilter = 0;
621 bool HasFallthrough = false;
Craig Topper1f7604d2014-12-13 05:12:19 +0000622 for (auto &Filter : FilterChooserMap) {
Owen Anderson4e818902011-02-18 21:51:29 +0000623 // Field value -1 implies a non-empty set of variable instructions.
624 // See also recurse().
Craig Topper1f7604d2014-12-13 05:12:19 +0000625 if (Filter.first == (unsigned)-1) {
Jim Grosbachecaef492012-08-14 19:06:05 +0000626 HasFallthrough = true;
Owen Anderson4e818902011-02-18 21:51:29 +0000627
Jim Grosbachecaef492012-08-14 19:06:05 +0000628 // Each scope should always have at least one filter value to check
629 // for.
630 assert(PrevFilter != 0 && "empty filter set!");
631 FixupList &CurScope = TableInfo.FixupStack.back();
632 // Resolve any NumToSkip fixups in the current scope.
633 resolveTableFixups(Table, CurScope, Table.size());
634 CurScope.clear();
635 PrevFilter = 0; // Don't re-process the filter's fallthrough.
636 } else {
637 Table.push_back(MCD::OPC_FilterValue);
638 // Encode and emit the value to filter against.
639 uint8_t Buffer[8];
Craig Topper1f7604d2014-12-13 05:12:19 +0000640 unsigned Len = encodeULEB128(Filter.first, Buffer);
Jim Grosbachecaef492012-08-14 19:06:05 +0000641 Table.insert(Table.end(), Buffer, Buffer + Len);
642 // Reserve space for the NumToSkip entry. We'll backpatch the value
643 // later.
644 PrevFilter = Table.size();
645 Table.push_back(0);
646 Table.push_back(0);
647 }
Owen Anderson4e818902011-02-18 21:51:29 +0000648
649 // We arrive at a category of instructions with the same segment value.
650 // Now delegate to the sub filter chooser for further decodings.
651 // The case may fallthrough, which happens if the remaining well-known
652 // encoding bits do not match exactly.
Craig Topper1f7604d2014-12-13 05:12:19 +0000653 Filter.second->emitTableEntries(TableInfo);
Owen Anderson4e818902011-02-18 21:51:29 +0000654
Jim Grosbachecaef492012-08-14 19:06:05 +0000655 // Now that we've emitted the body of the handler, update the NumToSkip
656 // of the filter itself to be able to skip forward when false. Subtract
657 // two as to account for the width of the NumToSkip field itself.
658 if (PrevFilter) {
659 uint32_t NumToSkip = Table.size() - PrevFilter - 2;
660 assert(NumToSkip < 65536U && "disassembler decoding table too large!");
661 Table[PrevFilter] = (uint8_t)NumToSkip;
662 Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8);
663 }
Owen Anderson4e818902011-02-18 21:51:29 +0000664 }
665
Jim Grosbachecaef492012-08-14 19:06:05 +0000666 // Any remaining unresolved fixups bubble up to the parent fixup scope.
667 assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!");
668 FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1;
669 FixupScopeList::iterator Dest = Source - 1;
670 Dest->insert(Dest->end(), Source->begin(), Source->end());
671 TableInfo.FixupStack.pop_back();
672
673 // If there is no fallthrough, then the final filter should get fixed
674 // up according to the enclosing scope rather than the current position.
675 if (!HasFallthrough)
676 TableInfo.FixupStack.back().push_back(PrevFilter);
Owen Anderson4e818902011-02-18 21:51:29 +0000677}
678
679// Returns the number of fanout produced by the filter. More fanout implies
680// the filter distinguishes more categories of instructions.
681unsigned Filter::usefulness() const {
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000682 if (!VariableInstructions.empty())
Owen Anderson4e818902011-02-18 21:51:29 +0000683 return FilteredInstructions.size();
684 else
685 return FilteredInstructions.size() + 1;
686}
687
688//////////////////////////////////
689// //
690// Filterchooser Implementation //
691// //
692//////////////////////////////////
693
Jim Grosbachecaef492012-08-14 19:06:05 +0000694// Emit the decoder state machine table.
695void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
696 DecoderTable &Table,
697 unsigned Indentation,
698 unsigned BitWidth,
699 StringRef Namespace) const {
700 OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace
701 << BitWidth << "[] = {\n";
Owen Anderson4e818902011-02-18 21:51:29 +0000702
Jim Grosbachecaef492012-08-14 19:06:05 +0000703 Indentation += 2;
Owen Anderson4e818902011-02-18 21:51:29 +0000704
Jim Grosbachecaef492012-08-14 19:06:05 +0000705 // FIXME: We may be able to use the NumToSkip values to recover
706 // appropriate indentation levels.
707 DecoderTable::const_iterator I = Table.begin();
708 DecoderTable::const_iterator E = Table.end();
709 while (I != E) {
710 assert (I < E && "incomplete decode table entry!");
Owen Anderson4e818902011-02-18 21:51:29 +0000711
Jim Grosbachecaef492012-08-14 19:06:05 +0000712 uint64_t Pos = I - Table.begin();
713 OS << "/* " << Pos << " */";
714 OS.PadToColumn(12);
Owen Anderson4e818902011-02-18 21:51:29 +0000715
Jim Grosbachecaef492012-08-14 19:06:05 +0000716 switch (*I) {
717 default:
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000718 PrintFatalError("invalid decode table opcode");
Jim Grosbachecaef492012-08-14 19:06:05 +0000719 case MCD::OPC_ExtractField: {
720 ++I;
721 unsigned Start = *I++;
722 unsigned Len = *I++;
723 OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
724 << Len << ", // Inst{";
725 if (Len > 1)
726 OS << (Start + Len - 1) << "-";
727 OS << Start << "} ...\n";
728 break;
729 }
730 case MCD::OPC_FilterValue: {
731 ++I;
732 OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
733 // The filter value is ULEB128 encoded.
734 while (*I >= 128)
735 OS << utostr(*I++) << ", ";
736 OS << utostr(*I++) << ", ";
737
738 // 16-bit numtoskip value.
739 uint8_t Byte = *I++;
740 uint32_t NumToSkip = Byte;
741 OS << utostr(Byte) << ", ";
742 Byte = *I++;
743 OS << utostr(Byte) << ", ";
744 NumToSkip |= Byte << 8;
745 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
746 break;
747 }
748 case MCD::OPC_CheckField: {
749 ++I;
750 unsigned Start = *I++;
751 unsigned Len = *I++;
752 OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
753 << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
754 // ULEB128 encoded field value.
755 for (; *I >= 128; ++I)
756 OS << utostr(*I) << ", ";
757 OS << utostr(*I++) << ", ";
758 // 16-bit numtoskip value.
759 uint8_t Byte = *I++;
760 uint32_t NumToSkip = Byte;
761 OS << utostr(Byte) << ", ";
762 Byte = *I++;
763 OS << utostr(Byte) << ", ";
764 NumToSkip |= Byte << 8;
765 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
766 break;
767 }
768 case MCD::OPC_CheckPredicate: {
769 ++I;
770 OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
771 for (; *I >= 128; ++I)
772 OS << utostr(*I) << ", ";
773 OS << utostr(*I++) << ", ";
774
775 // 16-bit numtoskip value.
776 uint8_t Byte = *I++;
777 uint32_t NumToSkip = Byte;
778 OS << utostr(Byte) << ", ";
779 Byte = *I++;
780 OS << utostr(Byte) << ", ";
781 NumToSkip |= Byte << 8;
782 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
783 break;
784 }
Petr Pavlu182b0572015-07-15 08:04:27 +0000785 case MCD::OPC_Decode:
786 case MCD::OPC_TryDecode: {
787 bool IsTry = *I == MCD::OPC_TryDecode;
Jim Grosbachecaef492012-08-14 19:06:05 +0000788 ++I;
789 // Extract the ULEB128 encoded Opcode to a buffer.
790 uint8_t Buffer[8], *p = Buffer;
791 while ((*p++ = *I++) >= 128)
792 assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer)
793 && "ULEB128 value too large!");
794 // Decode the Opcode value.
795 unsigned Opc = decodeULEB128(Buffer);
Petr Pavlu182b0572015-07-15 08:04:27 +0000796 OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
797 << "Decode, ";
Jim Grosbachecaef492012-08-14 19:06:05 +0000798 for (p = Buffer; *p >= 128; ++p)
799 OS << utostr(*p) << ", ";
800 OS << utostr(*p) << ", ";
801
802 // Decoder index.
803 for (; *I >= 128; ++I)
804 OS << utostr(*I) << ", ";
805 OS << utostr(*I++) << ", ";
806
Petr Pavlu182b0572015-07-15 08:04:27 +0000807 if (!IsTry) {
808 OS << "// Opcode: "
Craig Topperf9265322016-01-17 20:38:14 +0000809 << NumberedInstructions[Opc]->TheDef->getName() << "\n";
Petr Pavlu182b0572015-07-15 08:04:27 +0000810 break;
811 }
812
813 // Fallthrough for OPC_TryDecode.
814
815 // 16-bit numtoskip value.
816 uint8_t Byte = *I++;
817 uint32_t NumToSkip = Byte;
818 OS << utostr(Byte) << ", ";
819 Byte = *I++;
820 OS << utostr(Byte) << ", ";
821 NumToSkip |= Byte << 8;
822
Jim Grosbachecaef492012-08-14 19:06:05 +0000823 OS << "// Opcode: "
Craig Topperf9265322016-01-17 20:38:14 +0000824 << NumberedInstructions[Opc]->TheDef->getName()
Petr Pavlu182b0572015-07-15 08:04:27 +0000825 << ", skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000826 break;
827 }
828 case MCD::OPC_SoftFail: {
829 ++I;
830 OS.indent(Indentation) << "MCD::OPC_SoftFail";
831 // Positive mask
832 uint64_t Value = 0;
833 unsigned Shift = 0;
834 do {
835 OS << ", " << utostr(*I);
836 Value += (*I & 0x7f) << Shift;
837 Shift += 7;
838 } while (*I++ >= 128);
839 if (Value > 127)
840 OS << " /* 0x" << utohexstr(Value) << " */";
841 // Negative mask
842 Value = 0;
843 Shift = 0;
844 do {
845 OS << ", " << utostr(*I);
846 Value += (*I & 0x7f) << Shift;
847 Shift += 7;
848 } while (*I++ >= 128);
849 if (Value > 127)
850 OS << " /* 0x" << utohexstr(Value) << " */";
851 OS << ",\n";
852 break;
853 }
854 case MCD::OPC_Fail: {
855 ++I;
856 OS.indent(Indentation) << "MCD::OPC_Fail,\n";
857 break;
858 }
859 }
860 }
861 OS.indent(Indentation) << "0\n";
862
863 Indentation -= 2;
864
865 OS.indent(Indentation) << "};\n\n";
866}
867
868void FixedLenDecoderEmitter::
869emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
870 unsigned Indentation) const {
871 // The predicate function is just a big switch statement based on the
872 // input predicate index.
873 OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000874 << "const FeatureBitset& Bits) {\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000875 Indentation += 2;
Aaron Ballmane59e3582013-07-15 16:53:32 +0000876 if (!Predicates.empty()) {
877 OS.indent(Indentation) << "switch (Idx) {\n";
878 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
879 unsigned Index = 0;
Craig Topper1f7604d2014-12-13 05:12:19 +0000880 for (const auto &Predicate : Predicates) {
881 OS.indent(Indentation) << "case " << Index++ << ":\n";
882 OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
Aaron Ballmane59e3582013-07-15 16:53:32 +0000883 }
884 OS.indent(Indentation) << "}\n";
885 } else {
886 // No case statement to emit
887 OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000888 }
Jim Grosbachecaef492012-08-14 19:06:05 +0000889 Indentation -= 2;
890 OS.indent(Indentation) << "}\n\n";
891}
892
893void FixedLenDecoderEmitter::
894emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
895 unsigned Indentation) const {
896 // The decoder function is just a big switch statement based on the
897 // input decoder index.
898 OS.indent(Indentation) << "template<typename InsnType>\n";
899 OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
900 << " unsigned Idx, InsnType insn, MCInst &MI,\n";
901 OS.indent(Indentation) << " uint64_t "
Petr Pavlu182b0572015-07-15 08:04:27 +0000902 << "Address, const void *Decoder, bool &DecodeComplete) {\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000903 Indentation += 2;
Petr Pavlu182b0572015-07-15 08:04:27 +0000904 OS.indent(Indentation) << "DecodeComplete = true;\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000905 OS.indent(Indentation) << "InsnType tmp;\n";
906 OS.indent(Indentation) << "switch (Idx) {\n";
907 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
908 unsigned Index = 0;
Craig Topper1f7604d2014-12-13 05:12:19 +0000909 for (const auto &Decoder : Decoders) {
910 OS.indent(Indentation) << "case " << Index++ << ":\n";
911 OS << Decoder;
Jim Grosbachecaef492012-08-14 19:06:05 +0000912 OS.indent(Indentation+2) << "return S;\n";
913 }
914 OS.indent(Indentation) << "}\n";
915 Indentation -= 2;
916 OS.indent(Indentation) << "}\n\n";
Owen Anderson4e818902011-02-18 21:51:29 +0000917}
918
919// Populates the field of the insn given the start position and the number of
920// consecutive bits to scan for.
921//
922// Returns false if and on the first uninitialized bit value encountered.
923// Returns true, otherwise.
924bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
Craig Topper48c112b2012-03-16 05:58:09 +0000925 unsigned StartBit, unsigned NumBits) const {
Owen Anderson4e818902011-02-18 21:51:29 +0000926 Field = 0;
927
928 for (unsigned i = 0; i < NumBits; ++i) {
929 if (Insn[StartBit + i] == BIT_UNSET)
930 return false;
931
932 if (Insn[StartBit + i] == BIT_TRUE)
933 Field = Field | (1ULL << i);
934 }
935
936 return true;
937}
938
939/// dumpFilterArray - dumpFilterArray prints out debugging info for the given
940/// filter array as a series of chars.
941void FilterChooser::dumpFilterArray(raw_ostream &o,
Craig Topper48c112b2012-03-16 05:58:09 +0000942 const std::vector<bit_value_t> &filter) const {
Craig Topper29688ab2012-08-17 05:42:16 +0000943 for (unsigned bitIndex = BitWidth; bitIndex > 0; bitIndex--) {
Owen Anderson4e818902011-02-18 21:51:29 +0000944 switch (filter[bitIndex - 1]) {
945 case BIT_UNFILTERED:
946 o << ".";
947 break;
948 case BIT_UNSET:
949 o << "_";
950 break;
951 case BIT_TRUE:
952 o << "1";
953 break;
954 case BIT_FALSE:
955 o << "0";
956 break;
957 }
958 }
959}
960
961/// dumpStack - dumpStack traverses the filter chooser chain and calls
962/// dumpFilterArray on each filter chooser up to the top level one.
Craig Topper48c112b2012-03-16 05:58:09 +0000963void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const {
964 const FilterChooser *current = this;
Owen Anderson4e818902011-02-18 21:51:29 +0000965
966 while (current) {
967 o << prefix;
968 dumpFilterArray(o, current->FilterBitValues);
969 o << '\n';
970 current = current->Parent;
971 }
972}
973
974// Called from Filter::recurse() when singleton exists. For debug purpose.
Craig Topper48c112b2012-03-16 05:58:09 +0000975void FilterChooser::SingletonExists(unsigned Opc) const {
Owen Anderson4e818902011-02-18 21:51:29 +0000976 insn_t Insn0;
977 insnWithID(Insn0, Opc);
978
979 errs() << "Singleton exists: " << nameWithID(Opc)
980 << " with its decoding dominating ";
981 for (unsigned i = 0; i < Opcodes.size(); ++i) {
982 if (Opcodes[i] == Opc) continue;
983 errs() << nameWithID(Opcodes[i]) << ' ';
984 }
985 errs() << '\n';
986
987 dumpStack(errs(), "\t\t");
Craig Topper82d0d5f2012-03-16 01:19:24 +0000988 for (unsigned i = 0; i < Opcodes.size(); ++i) {
Owen Anderson4e818902011-02-18 21:51:29 +0000989 const std::string &Name = nameWithID(Opcodes[i]);
990
991 errs() << '\t' << Name << " ";
992 dumpBits(errs(),
993 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
994 errs() << '\n';
995 }
996}
997
998// Calculates the island(s) needed to decode the instruction.
999// This returns a list of undecoded bits of an instructions, for example,
1000// Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
1001// decoded bits in order to verify that the instruction matches the Opcode.
1002unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
Craig Topper82d0d5f2012-03-16 01:19:24 +00001003 std::vector<unsigned> &EndBits,
1004 std::vector<uint64_t> &FieldVals,
Craig Topper48c112b2012-03-16 05:58:09 +00001005 const insn_t &Insn) const {
Owen Anderson4e818902011-02-18 21:51:29 +00001006 unsigned Num, BitNo;
1007 Num = BitNo = 0;
1008
1009 uint64_t FieldVal = 0;
1010
1011 // 0: Init
1012 // 1: Water (the bit value does not affect decoding)
1013 // 2: Island (well-known bit value needed for decoding)
1014 int State = 0;
1015 int Val = -1;
1016
Owen Andersonc78e03c2011-07-19 21:06:00 +00001017 for (unsigned i = 0; i < BitWidth; ++i) {
Owen Anderson4e818902011-02-18 21:51:29 +00001018 Val = Value(Insn[i]);
1019 bool Filtered = PositionFiltered(i);
1020 switch (State) {
Craig Topperc4965bc2012-02-05 07:21:30 +00001021 default: llvm_unreachable("Unreachable code!");
Owen Anderson4e818902011-02-18 21:51:29 +00001022 case 0:
1023 case 1:
1024 if (Filtered || Val == -1)
1025 State = 1; // Still in Water
1026 else {
1027 State = 2; // Into the Island
1028 BitNo = 0;
1029 StartBits.push_back(i);
1030 FieldVal = Val;
1031 }
1032 break;
1033 case 2:
1034 if (Filtered || Val == -1) {
1035 State = 1; // Into the Water
1036 EndBits.push_back(i - 1);
1037 FieldVals.push_back(FieldVal);
1038 ++Num;
1039 } else {
1040 State = 2; // Still in Island
1041 ++BitNo;
1042 FieldVal = FieldVal | Val << BitNo;
1043 }
1044 break;
1045 }
1046 }
1047 // If we are still in Island after the loop, do some housekeeping.
1048 if (State == 2) {
Owen Andersonc78e03c2011-07-19 21:06:00 +00001049 EndBits.push_back(BitWidth - 1);
Owen Anderson4e818902011-02-18 21:51:29 +00001050 FieldVals.push_back(FieldVal);
1051 ++Num;
1052 }
1053
1054 assert(StartBits.size() == Num && EndBits.size() == Num &&
1055 FieldVals.size() == Num);
1056 return Num;
1057}
1058
Owen Andersone3591652011-07-28 21:54:31 +00001059void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
Petr Pavlu182b0572015-07-15 08:04:27 +00001060 const OperandInfo &OpInfo,
1061 bool &OpHasCompleteDecoder) const {
Craig Topper48c112b2012-03-16 05:58:09 +00001062 const std::string &Decoder = OpInfo.Decoder;
Owen Andersone3591652011-07-28 21:54:31 +00001063
Craig Topper5546f8c2014-09-27 05:26:42 +00001064 if (OpInfo.numFields() != 1)
Craig Topperebc3aa22012-08-17 05:16:15 +00001065 o.indent(Indentation) << "tmp = 0;\n";
Craig Topper5546f8c2014-09-27 05:26:42 +00001066
1067 for (const EncodingField &EF : OpInfo) {
1068 o.indent(Indentation) << "tmp ";
1069 if (OpInfo.numFields() != 1) o << '|';
1070 o << "= fieldFromInstruction"
1071 << "(insn, " << EF.Base << ", " << EF.Width << ')';
1072 if (OpInfo.numFields() != 1 || EF.Offset != 0)
1073 o << " << " << EF.Offset;
1074 o << ";\n";
Owen Andersone3591652011-07-28 21:54:31 +00001075 }
1076
Petr Pavlu182b0572015-07-15 08:04:27 +00001077 if (Decoder != "") {
1078 OpHasCompleteDecoder = OpInfo.HasCompleteDecoder;
Craig Topperebc3aa22012-08-17 05:16:15 +00001079 o.indent(Indentation) << Emitter->GuardPrefix << Decoder
Petr Pavlu182b0572015-07-15 08:04:27 +00001080 << "(MI, tmp, Address, Decoder)"
1081 << Emitter->GuardPostfix
1082 << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
1083 << "return MCDisassembler::Fail; }\n";
1084 } else {
1085 OpHasCompleteDecoder = true;
Jim Grosbache9119e42015-05-13 18:37:00 +00001086 o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
Petr Pavlu182b0572015-07-15 08:04:27 +00001087 }
Owen Andersone3591652011-07-28 21:54:31 +00001088}
1089
Jim Grosbachecaef492012-08-14 19:06:05 +00001090void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
Petr Pavlu182b0572015-07-15 08:04:27 +00001091 unsigned Opc, bool &HasCompleteDecoder) const {
1092 HasCompleteDecoder = true;
1093
Craig Topper1f7604d2014-12-13 05:12:19 +00001094 for (const auto &Op : Operands.find(Opc)->second) {
Jim Grosbachecaef492012-08-14 19:06:05 +00001095 // If a custom instruction decoder was specified, use that.
Craig Topper1f7604d2014-12-13 05:12:19 +00001096 if (Op.numFields() == 0 && Op.Decoder.size()) {
Petr Pavlu182b0572015-07-15 08:04:27 +00001097 HasCompleteDecoder = Op.HasCompleteDecoder;
Craig Topper1f7604d2014-12-13 05:12:19 +00001098 OS.indent(Indentation) << Emitter->GuardPrefix << Op.Decoder
Jim Grosbachecaef492012-08-14 19:06:05 +00001099 << "(MI, insn, Address, Decoder)"
Petr Pavlu182b0572015-07-15 08:04:27 +00001100 << Emitter->GuardPostfix
1101 << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
1102 << "return MCDisassembler::Fail; }\n";
Jim Grosbachecaef492012-08-14 19:06:05 +00001103 break;
1104 }
1105
Petr Pavlu182b0572015-07-15 08:04:27 +00001106 bool OpHasCompleteDecoder;
1107 emitBinaryParser(OS, Indentation, Op, OpHasCompleteDecoder);
1108 if (!OpHasCompleteDecoder)
1109 HasCompleteDecoder = false;
Jim Grosbachecaef492012-08-14 19:06:05 +00001110 }
1111}
1112
1113unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
Petr Pavlu182b0572015-07-15 08:04:27 +00001114 unsigned Opc,
1115 bool &HasCompleteDecoder) const {
Jim Grosbachecaef492012-08-14 19:06:05 +00001116 // Build up the predicate string.
1117 SmallString<256> Decoder;
1118 // FIXME: emitDecoder() function can take a buffer directly rather than
1119 // a stream.
1120 raw_svector_ostream S(Decoder);
Craig Topperebc3aa22012-08-17 05:16:15 +00001121 unsigned I = 4;
Petr Pavlu182b0572015-07-15 08:04:27 +00001122 emitDecoder(S, I, Opc, HasCompleteDecoder);
Jim Grosbachecaef492012-08-14 19:06:05 +00001123
1124 // Using the full decoder string as the key value here is a bit
1125 // heavyweight, but is effective. If the string comparisons become a
1126 // performance concern, we can implement a mangling of the predicate
Nick Lewycky06b0ea22015-08-18 22:41:58 +00001127 // data easily enough with a map back to the actual string. That's
Jim Grosbachecaef492012-08-14 19:06:05 +00001128 // overkill for now, though.
1129
1130 // Make sure the predicate is in the table.
Yaron Keren92e1b622015-03-18 10:17:07 +00001131 Decoders.insert(StringRef(Decoder));
Jim Grosbachecaef492012-08-14 19:06:05 +00001132 // Now figure out the index for when we write out the table.
1133 DecoderSet::const_iterator P = std::find(Decoders.begin(),
1134 Decoders.end(),
1135 Decoder.str());
1136 return (unsigned)(P - Decoders.begin());
1137}
1138
James Molloy8067df92011-09-07 19:42:28 +00001139static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
Craig Topper48c112b2012-03-16 05:58:09 +00001140 const std::string &PredicateNamespace) {
Andrew Trick43674ad2011-09-08 05:25:49 +00001141 if (str[0] == '!')
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001142 o << "!Bits[" << PredicateNamespace << "::"
1143 << str.slice(1,str.size()) << "]";
James Molloy8067df92011-09-07 19:42:28 +00001144 else
Michael Kupersteindb0712f2015-05-26 10:47:10 +00001145 o << "Bits[" << PredicateNamespace << "::" << str << "]";
James Molloy8067df92011-09-07 19:42:28 +00001146}
1147
1148bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
Craig Topper48c112b2012-03-16 05:58:09 +00001149 unsigned Opc) const {
Jim Grosbach3f4b2392012-02-29 22:07:56 +00001150 ListInit *Predicates =
1151 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
Toma Tabacu3d5ce492015-04-07 12:10:11 +00001152 bool IsFirstEmission = true;
Craig Topper664f6a02015-06-02 04:15:57 +00001153 for (unsigned i = 0; i < Predicates->size(); ++i) {
James Molloy8067df92011-09-07 19:42:28 +00001154 Record *Pred = Predicates->getElementAsRecord(i);
1155 if (!Pred->getValue("AssemblerMatcherPredicate"))
1156 continue;
1157
1158 std::string P = Pred->getValueAsString("AssemblerCondString");
1159
1160 if (!P.length())
1161 continue;
1162
Toma Tabacu3d5ce492015-04-07 12:10:11 +00001163 if (!IsFirstEmission)
James Molloy8067df92011-09-07 19:42:28 +00001164 o << " && ";
1165
1166 StringRef SR(P);
1167 std::pair<StringRef, StringRef> pairs = SR.split(',');
1168 while (pairs.second.size()) {
1169 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1170 o << " && ";
1171 pairs = pairs.second.split(',');
1172 }
1173 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
Toma Tabacu3d5ce492015-04-07 12:10:11 +00001174 IsFirstEmission = false;
James Molloy8067df92011-09-07 19:42:28 +00001175 }
Craig Topper664f6a02015-06-02 04:15:57 +00001176 return !Predicates->empty();
Andrew Trick61abca62011-09-08 05:23:14 +00001177}
James Molloy8067df92011-09-07 19:42:28 +00001178
Jim Grosbachecaef492012-08-14 19:06:05 +00001179bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const {
1180 ListInit *Predicates =
1181 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
Craig Topper664f6a02015-06-02 04:15:57 +00001182 for (unsigned i = 0; i < Predicates->size(); ++i) {
Jim Grosbachecaef492012-08-14 19:06:05 +00001183 Record *Pred = Predicates->getElementAsRecord(i);
1184 if (!Pred->getValue("AssemblerMatcherPredicate"))
1185 continue;
1186
1187 std::string P = Pred->getValueAsString("AssemblerCondString");
1188
1189 if (!P.length())
1190 continue;
1191
1192 return true;
1193 }
1194 return false;
1195}
1196
1197unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo,
1198 StringRef Predicate) const {
1199 // Using the full predicate string as the key value here is a bit
1200 // heavyweight, but is effective. If the string comparisons become a
1201 // performance concern, we can implement a mangling of the predicate
Nick Lewycky06b0ea22015-08-18 22:41:58 +00001202 // data easily enough with a map back to the actual string. That's
Jim Grosbachecaef492012-08-14 19:06:05 +00001203 // overkill for now, though.
1204
1205 // Make sure the predicate is in the table.
1206 TableInfo.Predicates.insert(Predicate.str());
1207 // Now figure out the index for when we write out the table.
1208 PredicateSet::const_iterator P = std::find(TableInfo.Predicates.begin(),
1209 TableInfo.Predicates.end(),
1210 Predicate.str());
1211 return (unsigned)(P - TableInfo.Predicates.begin());
1212}
1213
1214void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
1215 unsigned Opc) const {
1216 if (!doesOpcodeNeedPredicate(Opc))
1217 return;
1218
1219 // Build up the predicate string.
1220 SmallString<256> Predicate;
1221 // FIXME: emitPredicateMatch() functions can take a buffer directly rather
1222 // than a stream.
1223 raw_svector_ostream PS(Predicate);
1224 unsigned I = 0;
1225 emitPredicateMatch(PS, I, Opc);
1226
1227 // Figure out the index into the predicate table for the predicate just
1228 // computed.
1229 unsigned PIdx = getPredicateIndex(TableInfo, PS.str());
1230 SmallString<16> PBytes;
1231 raw_svector_ostream S(PBytes);
1232 encodeULEB128(PIdx, S);
Jim Grosbachecaef492012-08-14 19:06:05 +00001233
1234 TableInfo.Table.push_back(MCD::OPC_CheckPredicate);
1235 // Predicate index
Craig Topper29688ab2012-08-17 05:42:16 +00001236 for (unsigned i = 0, e = PBytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001237 TableInfo.Table.push_back(PBytes[i]);
1238 // Push location for NumToSkip backpatching.
1239 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1240 TableInfo.Table.push_back(0);
1241 TableInfo.Table.push_back(0);
1242}
1243
1244void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
1245 unsigned Opc) const {
Jim Grosbach3f4b2392012-02-29 22:07:56 +00001246 BitsInit *SFBits =
1247 AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail");
James Molloyd9ba4fd2012-02-09 10:56:31 +00001248 if (!SFBits) return;
1249 BitsInit *InstBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("Inst");
1250
1251 APInt PositiveMask(BitWidth, 0ULL);
1252 APInt NegativeMask(BitWidth, 0ULL);
1253 for (unsigned i = 0; i < BitWidth; ++i) {
1254 bit_value_t B = bitFromBits(*SFBits, i);
1255 bit_value_t IB = bitFromBits(*InstBits, i);
1256
1257 if (B != BIT_TRUE) continue;
1258
1259 switch (IB) {
1260 case BIT_FALSE:
1261 // The bit is meant to be false, so emit a check to see if it is true.
1262 PositiveMask.setBit(i);
1263 break;
1264 case BIT_TRUE:
1265 // The bit is meant to be true, so emit a check to see if it is false.
1266 NegativeMask.setBit(i);
1267 break;
1268 default:
1269 // The bit is not set; this must be an error!
1270 StringRef Name = AllInstructions[Opc]->TheDef->getName();
Jim Grosbachecaef492012-08-14 19:06:05 +00001271 errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in " << Name
1272 << " is set but Inst{" << i << "} is unset!\n"
James Molloyd9ba4fd2012-02-09 10:56:31 +00001273 << " - You can only mark a bit as SoftFail if it is fully defined"
1274 << " (1/0 - not '?') in Inst\n";
Jim Grosbachecaef492012-08-14 19:06:05 +00001275 return;
James Molloyd9ba4fd2012-02-09 10:56:31 +00001276 }
1277 }
1278
1279 bool NeedPositiveMask = PositiveMask.getBoolValue();
1280 bool NeedNegativeMask = NegativeMask.getBoolValue();
1281
1282 if (!NeedPositiveMask && !NeedNegativeMask)
1283 return;
1284
Jim Grosbachecaef492012-08-14 19:06:05 +00001285 TableInfo.Table.push_back(MCD::OPC_SoftFail);
James Molloyd9ba4fd2012-02-09 10:56:31 +00001286
Jim Grosbachecaef492012-08-14 19:06:05 +00001287 SmallString<16> MaskBytes;
1288 raw_svector_ostream S(MaskBytes);
1289 if (NeedPositiveMask) {
1290 encodeULEB128(PositiveMask.getZExtValue(), S);
Craig Topper29688ab2012-08-17 05:42:16 +00001291 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001292 TableInfo.Table.push_back(MaskBytes[i]);
1293 } else
1294 TableInfo.Table.push_back(0);
1295 if (NeedNegativeMask) {
1296 MaskBytes.clear();
Jim Grosbachecaef492012-08-14 19:06:05 +00001297 encodeULEB128(NegativeMask.getZExtValue(), S);
Craig Topper29688ab2012-08-17 05:42:16 +00001298 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001299 TableInfo.Table.push_back(MaskBytes[i]);
1300 } else
1301 TableInfo.Table.push_back(0);
James Molloyd9ba4fd2012-02-09 10:56:31 +00001302}
1303
Jim Grosbachecaef492012-08-14 19:06:05 +00001304// Emits table entries to decode the singleton.
1305void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1306 unsigned Opc) const {
Owen Anderson4e818902011-02-18 21:51:29 +00001307 std::vector<unsigned> StartBits;
1308 std::vector<unsigned> EndBits;
1309 std::vector<uint64_t> FieldVals;
1310 insn_t Insn;
1311 insnWithID(Insn, Opc);
1312
1313 // Look for islands of undecoded bits of the singleton.
1314 getIslands(StartBits, EndBits, FieldVals, Insn);
1315
1316 unsigned Size = StartBits.size();
Owen Anderson4e818902011-02-18 21:51:29 +00001317
Jim Grosbachecaef492012-08-14 19:06:05 +00001318 // Emit the predicate table entry if one is needed.
1319 emitPredicateTableEntry(TableInfo, Opc);
Owen Anderson4e818902011-02-18 21:51:29 +00001320
Jim Grosbachecaef492012-08-14 19:06:05 +00001321 // Check any additional encoding fields needed.
Craig Topper29688ab2012-08-17 05:42:16 +00001322 for (unsigned I = Size; I != 0; --I) {
1323 unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1;
Jim Grosbachecaef492012-08-14 19:06:05 +00001324 TableInfo.Table.push_back(MCD::OPC_CheckField);
1325 TableInfo.Table.push_back(StartBits[I-1]);
1326 TableInfo.Table.push_back(NumBits);
1327 uint8_t Buffer[8], *p;
1328 encodeULEB128(FieldVals[I-1], Buffer);
1329 for (p = Buffer; *p >= 128 ; ++p)
1330 TableInfo.Table.push_back(*p);
1331 TableInfo.Table.push_back(*p);
1332 // Push location for NumToSkip backpatching.
1333 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1334 // The fixup is always 16-bits, so go ahead and allocate the space
1335 // in the table so all our relative position calculations work OK even
1336 // before we fully resolve the real value here.
1337 TableInfo.Table.push_back(0);
1338 TableInfo.Table.push_back(0);
Owen Anderson4e818902011-02-18 21:51:29 +00001339 }
Owen Anderson4e818902011-02-18 21:51:29 +00001340
Jim Grosbachecaef492012-08-14 19:06:05 +00001341 // Check for soft failure of the match.
1342 emitSoftFailTableEntry(TableInfo, Opc);
Owen Anderson4e818902011-02-18 21:51:29 +00001343
Petr Pavlu182b0572015-07-15 08:04:27 +00001344 bool HasCompleteDecoder;
1345 unsigned DIdx = getDecoderIndex(TableInfo.Decoders, Opc, HasCompleteDecoder);
1346
1347 // Produce OPC_Decode or OPC_TryDecode opcode based on the information
1348 // whether the instruction decoder is complete or not. If it is complete
1349 // then it handles all possible values of remaining variable/unfiltered bits
1350 // and for any value can determine if the bitpattern is a valid instruction
1351 // or not. This means OPC_Decode will be the final step in the decoding
1352 // process. If it is not complete, then the Fail return code from the
1353 // decoder method indicates that additional processing should be done to see
1354 // if there is any other instruction that also matches the bitpattern and
1355 // can decode it.
1356 TableInfo.Table.push_back(HasCompleteDecoder ? MCD::OPC_Decode :
1357 MCD::OPC_TryDecode);
Jim Grosbachecaef492012-08-14 19:06:05 +00001358 uint8_t Buffer[8], *p;
1359 encodeULEB128(Opc, Buffer);
1360 for (p = Buffer; *p >= 128 ; ++p)
1361 TableInfo.Table.push_back(*p);
1362 TableInfo.Table.push_back(*p);
1363
Jim Grosbachecaef492012-08-14 19:06:05 +00001364 SmallString<16> Bytes;
1365 raw_svector_ostream S(Bytes);
1366 encodeULEB128(DIdx, S);
Jim Grosbachecaef492012-08-14 19:06:05 +00001367
1368 // Decoder index
Craig Topper29688ab2012-08-17 05:42:16 +00001369 for (unsigned i = 0, e = Bytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001370 TableInfo.Table.push_back(Bytes[i]);
Petr Pavlu182b0572015-07-15 08:04:27 +00001371
1372 if (!HasCompleteDecoder) {
1373 // Push location for NumToSkip backpatching.
1374 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1375 // Allocate the space for the fixup.
1376 TableInfo.Table.push_back(0);
1377 TableInfo.Table.push_back(0);
1378 }
Owen Anderson4e818902011-02-18 21:51:29 +00001379}
1380
Jim Grosbachecaef492012-08-14 19:06:05 +00001381// Emits table entries to decode the singleton, and then to decode the rest.
1382void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1383 const Filter &Best) const {
Owen Anderson4e818902011-02-18 21:51:29 +00001384 unsigned Opc = Best.getSingletonOpc();
1385
Jim Grosbachecaef492012-08-14 19:06:05 +00001386 // complex singletons need predicate checks from the first singleton
1387 // to refer forward to the variable filterchooser that follows.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001388 TableInfo.FixupStack.emplace_back();
Owen Anderson4e818902011-02-18 21:51:29 +00001389
Jim Grosbachecaef492012-08-14 19:06:05 +00001390 emitSingletonTableEntry(TableInfo, Opc);
Owen Anderson4e818902011-02-18 21:51:29 +00001391
Jim Grosbachecaef492012-08-14 19:06:05 +00001392 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
1393 TableInfo.Table.size());
1394 TableInfo.FixupStack.pop_back();
1395
1396 Best.getVariableFC().emitTableEntries(TableInfo);
Owen Anderson4e818902011-02-18 21:51:29 +00001397}
1398
Jim Grosbachecaef492012-08-14 19:06:05 +00001399
Owen Anderson4e818902011-02-18 21:51:29 +00001400// Assign a single filter and run with it. Top level API client can initialize
1401// with a single filter to start the filtering process.
Craig Topper48c112b2012-03-16 05:58:09 +00001402void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit,
1403 bool mixed) {
Owen Anderson4e818902011-02-18 21:51:29 +00001404 Filters.clear();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001405 Filters.emplace_back(*this, startBit, numBit, true);
Owen Anderson4e818902011-02-18 21:51:29 +00001406 BestIndex = 0; // Sole Filter instance to choose from.
1407 bestFilter().recurse();
1408}
1409
1410// reportRegion is a helper function for filterProcessor to mark a region as
1411// eligible for use as a filter region.
1412void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
Craig Topper82d0d5f2012-03-16 01:19:24 +00001413 unsigned BitIndex, bool AllowMixed) {
Owen Anderson4e818902011-02-18 21:51:29 +00001414 if (RA == ATTR_MIXED && AllowMixed)
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001415 Filters.emplace_back(*this, StartBit, BitIndex - StartBit, true);
Owen Anderson4e818902011-02-18 21:51:29 +00001416 else if (RA == ATTR_ALL_SET && !AllowMixed)
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001417 Filters.emplace_back(*this, StartBit, BitIndex - StartBit, false);
Owen Anderson4e818902011-02-18 21:51:29 +00001418}
1419
1420// FilterProcessor scans the well-known encoding bits of the instructions and
1421// builds up a list of candidate filters. It chooses the best filter and
1422// recursively descends down the decoding tree.
1423bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1424 Filters.clear();
1425 BestIndex = -1;
1426 unsigned numInstructions = Opcodes.size();
1427
1428 assert(numInstructions && "Filter created with no instructions");
1429
1430 // No further filtering is necessary.
1431 if (numInstructions == 1)
1432 return true;
1433
1434 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1435 // instructions is 3.
1436 if (AllowMixed && !Greedy) {
1437 assert(numInstructions == 3);
1438
1439 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1440 std::vector<unsigned> StartBits;
1441 std::vector<unsigned> EndBits;
1442 std::vector<uint64_t> FieldVals;
1443 insn_t Insn;
1444
1445 insnWithID(Insn, Opcodes[i]);
1446
1447 // Look for islands of undecoded bits of any instruction.
1448 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1449 // Found an instruction with island(s). Now just assign a filter.
Craig Topper48c112b2012-03-16 05:58:09 +00001450 runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true);
Owen Anderson4e818902011-02-18 21:51:29 +00001451 return true;
1452 }
1453 }
1454 }
1455
Craig Topper29688ab2012-08-17 05:42:16 +00001456 unsigned BitIndex;
Owen Anderson4e818902011-02-18 21:51:29 +00001457
1458 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1459 // The automaton consumes the corresponding bit from each
1460 // instruction.
1461 //
1462 // Input symbols: 0, 1, and _ (unset).
1463 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1464 // Initial state: NONE.
1465 //
1466 // (NONE) ------- [01] -> (ALL_SET)
1467 // (NONE) ------- _ ----> (ALL_UNSET)
1468 // (ALL_SET) ---- [01] -> (ALL_SET)
1469 // (ALL_SET) ---- _ ----> (MIXED)
1470 // (ALL_UNSET) -- [01] -> (MIXED)
1471 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1472 // (MIXED) ------ . ----> (MIXED)
1473 // (FILTERED)---- . ----> (FILTERED)
1474
Owen Andersonc78e03c2011-07-19 21:06:00 +00001475 std::vector<bitAttr_t> bitAttrs;
Owen Anderson4e818902011-02-18 21:51:29 +00001476
1477 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1478 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
Owen Andersonc78e03c2011-07-19 21:06:00 +00001479 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
Owen Anderson4e818902011-02-18 21:51:29 +00001480 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1481 FilterBitValues[BitIndex] == BIT_FALSE)
Owen Andersonc78e03c2011-07-19 21:06:00 +00001482 bitAttrs.push_back(ATTR_FILTERED);
Owen Anderson4e818902011-02-18 21:51:29 +00001483 else
Owen Andersonc78e03c2011-07-19 21:06:00 +00001484 bitAttrs.push_back(ATTR_NONE);
Owen Anderson4e818902011-02-18 21:51:29 +00001485
Craig Topper29688ab2012-08-17 05:42:16 +00001486 for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
Owen Anderson4e818902011-02-18 21:51:29 +00001487 insn_t insn;
1488
1489 insnWithID(insn, Opcodes[InsnIndex]);
1490
Owen Andersonc78e03c2011-07-19 21:06:00 +00001491 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
Owen Anderson4e818902011-02-18 21:51:29 +00001492 switch (bitAttrs[BitIndex]) {
1493 case ATTR_NONE:
1494 if (insn[BitIndex] == BIT_UNSET)
1495 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1496 else
1497 bitAttrs[BitIndex] = ATTR_ALL_SET;
1498 break;
1499 case ATTR_ALL_SET:
1500 if (insn[BitIndex] == BIT_UNSET)
1501 bitAttrs[BitIndex] = ATTR_MIXED;
1502 break;
1503 case ATTR_ALL_UNSET:
1504 if (insn[BitIndex] != BIT_UNSET)
1505 bitAttrs[BitIndex] = ATTR_MIXED;
1506 break;
1507 case ATTR_MIXED:
1508 case ATTR_FILTERED:
1509 break;
1510 }
1511 }
1512 }
1513
1514 // The regionAttr automaton consumes the bitAttrs automatons' state,
1515 // lowest-to-highest.
1516 //
1517 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1518 // States: NONE, ALL_SET, MIXED
1519 // Initial state: NONE
1520 //
1521 // (NONE) ----- F --> (NONE)
1522 // (NONE) ----- S --> (ALL_SET) ; and set region start
1523 // (NONE) ----- U --> (NONE)
1524 // (NONE) ----- M --> (MIXED) ; and set region start
1525 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1526 // (ALL_SET) -- S --> (ALL_SET)
1527 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1528 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1529 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1530 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1531 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1532 // (MIXED) ---- M --> (MIXED)
1533
1534 bitAttr_t RA = ATTR_NONE;
1535 unsigned StartBit = 0;
1536
Craig Topper29688ab2012-08-17 05:42:16 +00001537 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
Owen Anderson4e818902011-02-18 21:51:29 +00001538 bitAttr_t bitAttr = bitAttrs[BitIndex];
1539
1540 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1541
1542 switch (RA) {
1543 case ATTR_NONE:
1544 switch (bitAttr) {
1545 case ATTR_FILTERED:
1546 break;
1547 case ATTR_ALL_SET:
1548 StartBit = BitIndex;
1549 RA = ATTR_ALL_SET;
1550 break;
1551 case ATTR_ALL_UNSET:
1552 break;
1553 case ATTR_MIXED:
1554 StartBit = BitIndex;
1555 RA = ATTR_MIXED;
1556 break;
1557 default:
Craig Topperc4965bc2012-02-05 07:21:30 +00001558 llvm_unreachable("Unexpected bitAttr!");
Owen Anderson4e818902011-02-18 21:51:29 +00001559 }
1560 break;
1561 case ATTR_ALL_SET:
1562 switch (bitAttr) {
1563 case ATTR_FILTERED:
1564 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1565 RA = ATTR_NONE;
1566 break;
1567 case ATTR_ALL_SET:
1568 break;
1569 case ATTR_ALL_UNSET:
1570 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1571 RA = ATTR_NONE;
1572 break;
1573 case ATTR_MIXED:
1574 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1575 StartBit = BitIndex;
1576 RA = ATTR_MIXED;
1577 break;
1578 default:
Craig Topperc4965bc2012-02-05 07:21:30 +00001579 llvm_unreachable("Unexpected bitAttr!");
Owen Anderson4e818902011-02-18 21:51:29 +00001580 }
1581 break;
1582 case ATTR_MIXED:
1583 switch (bitAttr) {
1584 case ATTR_FILTERED:
1585 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1586 StartBit = BitIndex;
1587 RA = ATTR_NONE;
1588 break;
1589 case ATTR_ALL_SET:
1590 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1591 StartBit = BitIndex;
1592 RA = ATTR_ALL_SET;
1593 break;
1594 case ATTR_ALL_UNSET:
1595 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1596 RA = ATTR_NONE;
1597 break;
1598 case ATTR_MIXED:
1599 break;
1600 default:
Craig Topperc4965bc2012-02-05 07:21:30 +00001601 llvm_unreachable("Unexpected bitAttr!");
Owen Anderson4e818902011-02-18 21:51:29 +00001602 }
1603 break;
1604 case ATTR_ALL_UNSET:
Craig Topperc4965bc2012-02-05 07:21:30 +00001605 llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
Owen Anderson4e818902011-02-18 21:51:29 +00001606 case ATTR_FILTERED:
Craig Topperc4965bc2012-02-05 07:21:30 +00001607 llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
Owen Anderson4e818902011-02-18 21:51:29 +00001608 }
1609 }
1610
1611 // At the end, if we're still in ALL_SET or MIXED states, report a region
1612 switch (RA) {
1613 case ATTR_NONE:
1614 break;
1615 case ATTR_FILTERED:
1616 break;
1617 case ATTR_ALL_SET:
1618 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1619 break;
1620 case ATTR_ALL_UNSET:
1621 break;
1622 case ATTR_MIXED:
1623 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1624 break;
1625 }
1626
1627 // We have finished with the filter processings. Now it's time to choose
1628 // the best performing filter.
1629 BestIndex = 0;
1630 bool AllUseless = true;
1631 unsigned BestScore = 0;
1632
1633 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1634 unsigned Usefulness = Filters[i].usefulness();
1635
1636 if (Usefulness)
1637 AllUseless = false;
1638
1639 if (Usefulness > BestScore) {
1640 BestIndex = i;
1641 BestScore = Usefulness;
1642 }
1643 }
1644
1645 if (!AllUseless)
1646 bestFilter().recurse();
1647
1648 return !AllUseless;
1649} // end of FilterChooser::filterProcessor(bool)
1650
1651// Decides on the best configuration of filter(s) to use in order to decode
1652// the instructions. A conflict of instructions may occur, in which case we
1653// dump the conflict set to the standard error.
1654void FilterChooser::doFilter() {
1655 unsigned Num = Opcodes.size();
1656 assert(Num && "FilterChooser created with no instructions");
1657
1658 // Try regions of consecutive known bit values first.
1659 if (filterProcessor(false))
1660 return;
1661
1662 // Then regions of mixed bits (both known and unitialized bit values allowed).
1663 if (filterProcessor(true))
1664 return;
1665
1666 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1667 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1668 // well-known encoding pattern. In such case, we backtrack and scan for the
1669 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1670 if (Num == 3 && filterProcessor(true, false))
1671 return;
1672
1673 // If we come to here, the instruction decoding has failed.
1674 // Set the BestIndex to -1 to indicate so.
1675 BestIndex = -1;
1676}
1677
Jim Grosbachecaef492012-08-14 19:06:05 +00001678// emitTableEntries - Emit state machine entries to decode our share of
1679// instructions.
1680void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
1681 if (Opcodes.size() == 1) {
Owen Anderson4e818902011-02-18 21:51:29 +00001682 // There is only one instruction in the set, which is great!
1683 // Call emitSingletonDecoder() to see whether there are any remaining
1684 // encodings bits.
Jim Grosbachecaef492012-08-14 19:06:05 +00001685 emitSingletonTableEntry(TableInfo, Opcodes[0]);
1686 return;
1687 }
Owen Anderson4e818902011-02-18 21:51:29 +00001688
1689 // Choose the best filter to do the decodings!
1690 if (BestIndex != -1) {
Craig Topper48c112b2012-03-16 05:58:09 +00001691 const Filter &Best = Filters[BestIndex];
Owen Anderson4e818902011-02-18 21:51:29 +00001692 if (Best.getNumFiltered() == 1)
Jim Grosbachecaef492012-08-14 19:06:05 +00001693 emitSingletonTableEntry(TableInfo, Best);
Owen Anderson4e818902011-02-18 21:51:29 +00001694 else
Jim Grosbachecaef492012-08-14 19:06:05 +00001695 Best.emitTableEntry(TableInfo);
1696 return;
Owen Anderson4e818902011-02-18 21:51:29 +00001697 }
1698
Jim Grosbachecaef492012-08-14 19:06:05 +00001699 // We don't know how to decode these instructions! Dump the
1700 // conflict set and bail.
Owen Anderson4e818902011-02-18 21:51:29 +00001701
1702 // Print out useful conflict information for postmortem analysis.
1703 errs() << "Decoding Conflict:\n";
1704
1705 dumpStack(errs(), "\t\t");
1706
Craig Topper82d0d5f2012-03-16 01:19:24 +00001707 for (unsigned i = 0; i < Opcodes.size(); ++i) {
Owen Anderson4e818902011-02-18 21:51:29 +00001708 const std::string &Name = nameWithID(Opcodes[i]);
1709
1710 errs() << '\t' << Name << " ";
1711 dumpBits(errs(),
1712 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1713 errs() << '\n';
1714 }
Owen Anderson4e818902011-02-18 21:51:29 +00001715}
1716
Hal Finkel71b2e202013-12-19 16:12:53 +00001717static bool populateInstruction(CodeGenTarget &Target,
1718 const CodeGenInstruction &CGI, unsigned Opc,
Craig Topper82d0d5f2012-03-16 01:19:24 +00001719 std::map<unsigned, std::vector<OperandInfo> > &Operands){
Owen Anderson4e818902011-02-18 21:51:29 +00001720 const Record &Def = *CGI.TheDef;
1721 // If all the bit positions are not specified; do not decode this instruction.
1722 // We are bound to fail! For proper disassembly, the well-known encoding bits
1723 // of the instruction must be fully specified.
Owen Anderson4e818902011-02-18 21:51:29 +00001724
David Greeneaf8ee2c2011-07-29 22:43:06 +00001725 BitsInit &Bits = getBitsField(Def, "Inst");
Jim Grosbachf3fd36e2011-07-06 21:33:38 +00001726 if (Bits.allInComplete()) return false;
1727
Owen Anderson4e818902011-02-18 21:51:29 +00001728 std::vector<OperandInfo> InsnOperands;
1729
1730 // If the instruction has specified a custom decoding hook, use that instead
1731 // of trying to auto-generate the decoder.
1732 std::string InstDecoder = Def.getValueAsString("DecoderMethod");
1733 if (InstDecoder != "") {
Petr Pavlu182b0572015-07-15 08:04:27 +00001734 bool HasCompleteInstDecoder = Def.getValueAsBit("hasCompleteDecoder");
1735 InsnOperands.push_back(OperandInfo(InstDecoder, HasCompleteInstDecoder));
Owen Anderson4e818902011-02-18 21:51:29 +00001736 Operands[Opc] = InsnOperands;
1737 return true;
1738 }
1739
1740 // Generate a description of the operand of the instruction that we know
1741 // how to decode automatically.
1742 // FIXME: We'll need to have a way to manually override this as needed.
1743
1744 // Gather the outputs/inputs of the instruction, so we can find their
1745 // positions in the encoding. This assumes for now that they appear in the
1746 // MCInst in the order that they're listed.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001747 std::vector<std::pair<Init*, std::string> > InOutOperands;
1748 DagInit *Out = Def.getValueAsDag("OutOperandList");
1749 DagInit *In = Def.getValueAsDag("InOperandList");
Owen Anderson4e818902011-02-18 21:51:29 +00001750 for (unsigned i = 0; i < Out->getNumArgs(); ++i)
1751 InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i)));
1752 for (unsigned i = 0; i < In->getNumArgs(); ++i)
1753 InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i)));
1754
Owen Anderson53562d02011-07-28 23:56:20 +00001755 // Search for tied operands, so that we can correctly instantiate
1756 // operands that are not explicitly represented in the encoding.
Owen Andersoncb32ce22011-07-29 18:28:52 +00001757 std::map<std::string, std::string> TiedNames;
Owen Anderson53562d02011-07-28 23:56:20 +00001758 for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
1759 int tiedTo = CGI.Operands[i].getTiedRegister();
Owen Andersoncb32ce22011-07-29 18:28:52 +00001760 if (tiedTo != -1) {
Hal Finkel71b2e202013-12-19 16:12:53 +00001761 std::pair<unsigned, unsigned> SO =
1762 CGI.Operands.getSubOperandNumber(tiedTo);
1763 TiedNames[InOutOperands[i].second] = InOutOperands[SO.first].second;
1764 TiedNames[InOutOperands[SO.first].second] = InOutOperands[i].second;
1765 }
1766 }
1767
1768 std::map<std::string, std::vector<OperandInfo> > NumberedInsnOperands;
1769 std::set<std::string> NumberedInsnOperandsNoTie;
1770 if (Target.getInstructionSet()->
1771 getValueAsBit("decodePositionallyEncodedOperands")) {
1772 const std::vector<RecordVal> &Vals = Def.getValues();
1773 unsigned NumberedOp = 0;
1774
Hal Finkel5457bd02014-03-13 07:57:54 +00001775 std::set<unsigned> NamedOpIndices;
1776 if (Target.getInstructionSet()->
1777 getValueAsBit("noNamedPositionallyEncodedOperands"))
1778 // Collect the set of operand indices that might correspond to named
1779 // operand, and skip these when assigning operands based on position.
1780 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1781 unsigned OpIdx;
1782 if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
1783 continue;
1784
1785 NamedOpIndices.insert(OpIdx);
1786 }
1787
Hal Finkel71b2e202013-12-19 16:12:53 +00001788 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1789 // Ignore fixed fields in the record, we're looking for values like:
1790 // bits<5> RST = { ?, ?, ?, ?, ? };
1791 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
1792 continue;
1793
1794 // Determine if Vals[i] actually contributes to the Inst encoding.
1795 unsigned bi = 0;
1796 for (; bi < Bits.getNumBits(); ++bi) {
Craig Topper24064772014-04-15 07:20:03 +00001797 VarInit *Var = nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001798 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1799 if (BI)
1800 Var = dyn_cast<VarInit>(BI->getBitVar());
1801 else
1802 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1803
1804 if (Var && Var->getName() == Vals[i].getName())
1805 break;
1806 }
1807
1808 if (bi == Bits.getNumBits())
1809 continue;
1810
1811 // Skip variables that correspond to explicitly-named operands.
1812 unsigned OpIdx;
1813 if (CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
1814 continue;
1815
1816 // Get the bit range for this operand:
1817 unsigned bitStart = bi++, bitWidth = 1;
1818 for (; bi < Bits.getNumBits(); ++bi) {
Craig Topper24064772014-04-15 07:20:03 +00001819 VarInit *Var = nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001820 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1821 if (BI)
1822 Var = dyn_cast<VarInit>(BI->getBitVar());
1823 else
1824 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1825
1826 if (!Var)
1827 break;
1828
1829 if (Var->getName() != Vals[i].getName())
1830 break;
1831
1832 ++bitWidth;
1833 }
1834
1835 unsigned NumberOps = CGI.Operands.size();
1836 while (NumberedOp < NumberOps &&
Hal Finkel5457bd02014-03-13 07:57:54 +00001837 (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001838 (!NamedOpIndices.empty() && NamedOpIndices.count(
Hal Finkel5457bd02014-03-13 07:57:54 +00001839 CGI.Operands.getSubOperandNumber(NumberedOp).first))))
Hal Finkel71b2e202013-12-19 16:12:53 +00001840 ++NumberedOp;
1841
1842 OpIdx = NumberedOp++;
1843
1844 // OpIdx now holds the ordered operand number of Vals[i].
1845 std::pair<unsigned, unsigned> SO =
1846 CGI.Operands.getSubOperandNumber(OpIdx);
1847 const std::string &Name = CGI.Operands[SO.first].Name;
1848
1849 DEBUG(dbgs() << "Numbered operand mapping for " << Def.getName() << ": " <<
1850 Name << "(" << SO.first << ", " << SO.second << ") => " <<
1851 Vals[i].getName() << "\n");
1852
1853 std::string Decoder = "";
1854 Record *TypeRecord = CGI.Operands[SO.first].Rec;
1855
1856 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
1857 StringInit *String = DecoderString ?
Craig Topper24064772014-04-15 07:20:03 +00001858 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001859 if (String && String->getValue() != "")
1860 Decoder = String->getValue();
1861
1862 if (Decoder == "" &&
1863 CGI.Operands[SO.first].MIOperandInfo &&
1864 CGI.Operands[SO.first].MIOperandInfo->getNumArgs()) {
1865 Init *Arg = CGI.Operands[SO.first].MIOperandInfo->
1866 getArg(SO.second);
1867 if (TypedInit *TI = cast<TypedInit>(Arg)) {
1868 RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
1869 TypeRecord = Type->getRecord();
1870 }
1871 }
1872
1873 bool isReg = false;
1874 if (TypeRecord->isSubClassOf("RegisterOperand"))
1875 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1876 if (TypeRecord->isSubClassOf("RegisterClass")) {
1877 Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
1878 isReg = true;
1879 } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
1880 Decoder = "DecodePointerLikeRegClass" +
1881 utostr(TypeRecord->getValueAsInt("RegClassKind"));
1882 isReg = true;
1883 }
1884
1885 DecoderString = TypeRecord->getValue("DecoderMethod");
1886 String = DecoderString ?
Craig Topper24064772014-04-15 07:20:03 +00001887 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001888 if (!isReg && String && String->getValue() != "")
1889 Decoder = String->getValue();
1890
Petr Pavlu182b0572015-07-15 08:04:27 +00001891 RecordVal *HasCompleteDecoderVal =
1892 TypeRecord->getValue("hasCompleteDecoder");
1893 BitInit *HasCompleteDecoderBit = HasCompleteDecoderVal ?
1894 dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) : nullptr;
1895 bool HasCompleteDecoder = HasCompleteDecoderBit ?
1896 HasCompleteDecoderBit->getValue() : true;
1897
1898 OperandInfo OpInfo(Decoder, HasCompleteDecoder);
Hal Finkel71b2e202013-12-19 16:12:53 +00001899 OpInfo.addField(bitStart, bitWidth, 0);
1900
1901 NumberedInsnOperands[Name].push_back(OpInfo);
1902
1903 // FIXME: For complex operands with custom decoders we can't handle tied
1904 // sub-operands automatically. Skip those here and assume that this is
1905 // fixed up elsewhere.
1906 if (CGI.Operands[SO.first].MIOperandInfo &&
1907 CGI.Operands[SO.first].MIOperandInfo->getNumArgs() > 1 &&
1908 String && String->getValue() != "")
1909 NumberedInsnOperandsNoTie.insert(Name);
Owen Andersoncb32ce22011-07-29 18:28:52 +00001910 }
Owen Anderson53562d02011-07-28 23:56:20 +00001911 }
1912
Owen Anderson4e818902011-02-18 21:51:29 +00001913 // For each operand, see if we can figure out where it is encoded.
Craig Topper1f7604d2014-12-13 05:12:19 +00001914 for (const auto &Op : InOutOperands) {
1915 if (!NumberedInsnOperands[Op.second].empty()) {
Hal Finkel71b2e202013-12-19 16:12:53 +00001916 InsnOperands.insert(InsnOperands.end(),
Craig Topper1f7604d2014-12-13 05:12:19 +00001917 NumberedInsnOperands[Op.second].begin(),
1918 NumberedInsnOperands[Op.second].end());
Hal Finkel71b2e202013-12-19 16:12:53 +00001919 continue;
Craig Topper1f7604d2014-12-13 05:12:19 +00001920 }
1921 if (!NumberedInsnOperands[TiedNames[Op.second]].empty()) {
1922 if (!NumberedInsnOperandsNoTie.count(TiedNames[Op.second])) {
Hal Finkel71b2e202013-12-19 16:12:53 +00001923 // Figure out to which (sub)operand we're tied.
Craig Topper1f7604d2014-12-13 05:12:19 +00001924 unsigned i = CGI.Operands.getOperandNamed(TiedNames[Op.second]);
Hal Finkel71b2e202013-12-19 16:12:53 +00001925 int tiedTo = CGI.Operands[i].getTiedRegister();
1926 if (tiedTo == -1) {
Craig Topper1f7604d2014-12-13 05:12:19 +00001927 i = CGI.Operands.getOperandNamed(Op.second);
Hal Finkel71b2e202013-12-19 16:12:53 +00001928 tiedTo = CGI.Operands[i].getTiedRegister();
1929 }
1930
1931 if (tiedTo != -1) {
1932 std::pair<unsigned, unsigned> SO =
1933 CGI.Operands.getSubOperandNumber(tiedTo);
1934
Craig Topper1f7604d2014-12-13 05:12:19 +00001935 InsnOperands.push_back(NumberedInsnOperands[TiedNames[Op.second]]
Hal Finkel71b2e202013-12-19 16:12:53 +00001936 [SO.second]);
1937 }
1938 }
1939 continue;
1940 }
1941
Owen Anderson4e818902011-02-18 21:51:29 +00001942 std::string Decoder = "";
1943
Owen Andersone3591652011-07-28 21:54:31 +00001944 // At this point, we can locate the field, but we need to know how to
1945 // interpret it. As a first step, require the target to provide callbacks
1946 // for decoding register classes.
1947 // FIXME: This need to be extended to handle instructions with custom
1948 // decoder methods, and operands with (simple) MIOperandInfo's.
Craig Topper1f7604d2014-12-13 05:12:19 +00001949 TypedInit *TI = cast<TypedInit>(Op.first);
Sean Silva88eb8dd2012-10-10 20:24:47 +00001950 RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
Owen Andersone3591652011-07-28 21:54:31 +00001951 Record *TypeRecord = Type->getRecord();
1952 bool isReg = false;
1953 if (TypeRecord->isSubClassOf("RegisterOperand"))
1954 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1955 if (TypeRecord->isSubClassOf("RegisterClass")) {
1956 Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
1957 isReg = true;
Hal Finkel9d95e8d2013-12-19 14:58:22 +00001958 } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
1959 Decoder = "DecodePointerLikeRegClass" +
1960 utostr(TypeRecord->getValueAsInt("RegClassKind"));
1961 isReg = true;
Owen Andersone3591652011-07-28 21:54:31 +00001962 }
1963
1964 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001965 StringInit *String = DecoderString ?
Craig Topper24064772014-04-15 07:20:03 +00001966 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
Owen Andersone3591652011-07-28 21:54:31 +00001967 if (!isReg && String && String->getValue() != "")
1968 Decoder = String->getValue();
1969
Petr Pavlu182b0572015-07-15 08:04:27 +00001970 RecordVal *HasCompleteDecoderVal =
1971 TypeRecord->getValue("hasCompleteDecoder");
1972 BitInit *HasCompleteDecoderBit = HasCompleteDecoderVal ?
1973 dyn_cast<BitInit>(HasCompleteDecoderVal->getValue()) : nullptr;
1974 bool HasCompleteDecoder = HasCompleteDecoderBit ?
1975 HasCompleteDecoderBit->getValue() : true;
1976
1977 OperandInfo OpInfo(Decoder, HasCompleteDecoder);
Owen Andersone3591652011-07-28 21:54:31 +00001978 unsigned Base = ~0U;
1979 unsigned Width = 0;
1980 unsigned Offset = 0;
1981
Owen Anderson4e818902011-02-18 21:51:29 +00001982 for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
Craig Topper24064772014-04-15 07:20:03 +00001983 VarInit *Var = nullptr;
Sean Silvafb509ed2012-10-10 20:24:43 +00001984 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
Owen Anderson3022d672011-08-01 22:45:43 +00001985 if (BI)
Sean Silvafb509ed2012-10-10 20:24:43 +00001986 Var = dyn_cast<VarInit>(BI->getBitVar());
Owen Anderson3022d672011-08-01 22:45:43 +00001987 else
Sean Silvafb509ed2012-10-10 20:24:43 +00001988 Var = dyn_cast<VarInit>(Bits.getBit(bi));
Owen Anderson3022d672011-08-01 22:45:43 +00001989
1990 if (!Var) {
Owen Andersone3591652011-07-28 21:54:31 +00001991 if (Base != ~0U) {
1992 OpInfo.addField(Base, Width, Offset);
1993 Base = ~0U;
1994 Width = 0;
1995 Offset = 0;
1996 }
1997 continue;
1998 }
Owen Anderson4e818902011-02-18 21:51:29 +00001999
Craig Topper1f7604d2014-12-13 05:12:19 +00002000 if (Var->getName() != Op.second &&
2001 Var->getName() != TiedNames[Op.second]) {
Owen Andersone3591652011-07-28 21:54:31 +00002002 if (Base != ~0U) {
2003 OpInfo.addField(Base, Width, Offset);
2004 Base = ~0U;
2005 Width = 0;
2006 Offset = 0;
2007 }
2008 continue;
Owen Anderson4e818902011-02-18 21:51:29 +00002009 }
2010
Owen Andersone3591652011-07-28 21:54:31 +00002011 if (Base == ~0U) {
2012 Base = bi;
2013 Width = 1;
Owen Anderson3022d672011-08-01 22:45:43 +00002014 Offset = BI ? BI->getBitNum() : 0;
2015 } else if (BI && BI->getBitNum() != Offset + Width) {
Owen Andersone08f5b52011-07-29 23:01:18 +00002016 OpInfo.addField(Base, Width, Offset);
2017 Base = bi;
2018 Width = 1;
2019 Offset = BI->getBitNum();
Owen Andersone3591652011-07-28 21:54:31 +00002020 } else {
2021 ++Width;
Owen Anderson4e818902011-02-18 21:51:29 +00002022 }
Owen Anderson4e818902011-02-18 21:51:29 +00002023 }
2024
Owen Andersone3591652011-07-28 21:54:31 +00002025 if (Base != ~0U)
2026 OpInfo.addField(Base, Width, Offset);
2027
2028 if (OpInfo.numFields() > 0)
2029 InsnOperands.push_back(OpInfo);
Owen Anderson4e818902011-02-18 21:51:29 +00002030 }
2031
2032 Operands[Opc] = InsnOperands;
2033
2034
2035#if 0
2036 DEBUG({
2037 // Dumps the instruction encoding bits.
2038 dumpBits(errs(), Bits);
2039
2040 errs() << '\n';
2041
2042 // Dumps the list of operand info.
2043 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
2044 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
2045 const std::string &OperandName = Info.Name;
2046 const Record &OperandDef = *Info.Rec;
2047
2048 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
2049 }
2050 });
2051#endif
2052
2053 return true;
2054}
2055
Jim Grosbachecaef492012-08-14 19:06:05 +00002056// emitFieldFromInstruction - Emit the templated helper function
2057// fieldFromInstruction().
2058static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
2059 OS << "// Helper function for extracting fields from encoded instructions.\n"
2060 << "template<typename InsnType>\n"
2061 << "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n"
2062 << " unsigned numBits) {\n"
2063 << " assert(startBit + numBits <= (sizeof(InsnType)*8) &&\n"
2064 << " \"Instruction field out of bounds!\");\n"
2065 << " InsnType fieldMask;\n"
2066 << " if (numBits == sizeof(InsnType)*8)\n"
2067 << " fieldMask = (InsnType)(-1LL);\n"
2068 << " else\n"
NAKAMURA Takumibf99a422012-12-26 06:43:14 +00002069 << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002070 << " return (insn & fieldMask) >> startBit;\n"
2071 << "}\n\n";
2072}
Owen Anderson4e818902011-02-18 21:51:29 +00002073
Jim Grosbachecaef492012-08-14 19:06:05 +00002074// emitDecodeInstruction - Emit the templated helper function
2075// decodeInstruction().
2076static void emitDecodeInstruction(formatted_raw_ostream &OS) {
2077 OS << "template<typename InsnType>\n"
2078 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,\n"
2079 << " InsnType insn, uint64_t Address,\n"
2080 << " const void *DisAsm,\n"
2081 << " const MCSubtargetInfo &STI) {\n"
Michael Kupersteindb0712f2015-05-26 10:47:10 +00002082 << " const FeatureBitset& Bits = STI.getFeatureBits();\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002083 << "\n"
2084 << " const uint8_t *Ptr = DecodeTable;\n"
Jim Grosbach4c363492012-09-17 18:00:53 +00002085 << " uint32_t CurFieldValue = 0;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002086 << " DecodeStatus S = MCDisassembler::Success;\n"
2087 << " for (;;) {\n"
2088 << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
2089 << " switch (*Ptr) {\n"
2090 << " default:\n"
2091 << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
2092 << " return MCDisassembler::Fail;\n"
2093 << " case MCD::OPC_ExtractField: {\n"
2094 << " unsigned Start = *++Ptr;\n"
2095 << " unsigned Len = *++Ptr;\n"
2096 << " ++Ptr;\n"
2097 << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
2098 << " DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << \", \"\n"
2099 << " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
2100 << " break;\n"
2101 << " }\n"
2102 << " case MCD::OPC_FilterValue: {\n"
2103 << " // Decode the field value.\n"
2104 << " unsigned Len;\n"
2105 << " InsnType Val = decodeULEB128(++Ptr, &Len);\n"
2106 << " Ptr += Len;\n"
2107 << " // NumToSkip is a plain 16-bit integer.\n"
2108 << " unsigned NumToSkip = *Ptr++;\n"
2109 << " NumToSkip |= (*Ptr++) << 8;\n"
2110 << "\n"
2111 << " // Perform the filter operation.\n"
2112 << " if (Val != CurFieldValue)\n"
2113 << " Ptr += NumToSkip;\n"
2114 << " DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << \", \" << NumToSkip\n"
2115 << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" : \"PASS:\")\n"
2116 << " << \" continuing at \" << (Ptr - DecodeTable) << \"\\n\");\n"
2117 << "\n"
2118 << " break;\n"
2119 << " }\n"
2120 << " case MCD::OPC_CheckField: {\n"
2121 << " unsigned Start = *++Ptr;\n"
2122 << " unsigned Len = *++Ptr;\n"
2123 << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
2124 << " // Decode the field value.\n"
2125 << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
2126 << " Ptr += Len;\n"
2127 << " // NumToSkip is a plain 16-bit integer.\n"
2128 << " unsigned NumToSkip = *Ptr++;\n"
2129 << " NumToSkip |= (*Ptr++) << 8;\n"
2130 << "\n"
2131 << " // If the actual and expected values don't match, skip.\n"
2132 << " if (ExpectedValue != FieldValue)\n"
2133 << " Ptr += NumToSkip;\n"
2134 << " DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << \", \"\n"
2135 << " << Len << \", \" << ExpectedValue << \", \" << NumToSkip\n"
2136 << " << \"): FieldValue = \" << FieldValue << \", ExpectedValue = \"\n"
2137 << " << ExpectedValue << \": \"\n"
2138 << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2139 << " break;\n"
2140 << " }\n"
2141 << " case MCD::OPC_CheckPredicate: {\n"
2142 << " unsigned Len;\n"
2143 << " // Decode the Predicate Index value.\n"
2144 << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
2145 << " Ptr += Len;\n"
2146 << " // NumToSkip is a plain 16-bit integer.\n"
2147 << " unsigned NumToSkip = *Ptr++;\n"
2148 << " NumToSkip |= (*Ptr++) << 8;\n"
2149 << " // Check the predicate.\n"
2150 << " bool Pred;\n"
2151 << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
2152 << " Ptr += NumToSkip;\n"
2153 << " (void)Pred;\n"
2154 << " DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx << \"): \"\n"
2155 << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2156 << "\n"
2157 << " break;\n"
2158 << " }\n"
2159 << " case MCD::OPC_Decode: {\n"
2160 << " unsigned Len;\n"
2161 << " // Decode the Opcode value.\n"
2162 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2163 << " Ptr += Len;\n"
2164 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2165 << " Ptr += Len;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002166 << "\n"
Cameron Esfahanif97999d2015-08-11 01:15:07 +00002167 << " MI.clear();\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002168 << " MI.setOpcode(Opc);\n"
Petr Pavlu182b0572015-07-15 08:04:27 +00002169 << " bool DecodeComplete;\n"
2170 << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, DecodeComplete);\n"
2171 << " assert(DecodeComplete);\n"
2172 << "\n"
2173 << " DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
2174 << " << \", using decoder \" << DecodeIdx << \": \"\n"
2175 << " << (S != MCDisassembler::Fail ? \"PASS\" : \"FAIL\") << \"\\n\");\n"
2176 << " return S;\n"
2177 << " }\n"
2178 << " case MCD::OPC_TryDecode: {\n"
2179 << " unsigned Len;\n"
2180 << " // Decode the Opcode value.\n"
2181 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2182 << " Ptr += Len;\n"
2183 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2184 << " Ptr += Len;\n"
2185 << " // NumToSkip is a plain 16-bit integer.\n"
2186 << " unsigned NumToSkip = *Ptr++;\n"
2187 << " NumToSkip |= (*Ptr++) << 8;\n"
2188 << "\n"
2189 << " // Perform the decode operation.\n"
2190 << " MCInst TmpMI;\n"
2191 << " TmpMI.setOpcode(Opc);\n"
2192 << " bool DecodeComplete;\n"
2193 << " S = decodeToMCInst(S, DecodeIdx, insn, TmpMI, Address, DisAsm, DecodeComplete);\n"
2194 << " DEBUG(dbgs() << Loc << \": OPC_TryDecode: opcode \" << Opc\n"
2195 << " << \", using decoder \" << DecodeIdx << \": \");\n"
2196 << "\n"
2197 << " if (DecodeComplete) {\n"
2198 << " // Decoding complete.\n"
2199 << " DEBUG(dbgs() << (S != MCDisassembler::Fail ? \"PASS\" : \"FAIL\") << \"\\n\");\n"
2200 << " MI = TmpMI;\n"
2201 << " return S;\n"
2202 << " } else {\n"
2203 << " assert(S == MCDisassembler::Fail);\n"
2204 << " // If the decoding was incomplete, skip.\n"
2205 << " Ptr += NumToSkip;\n"
2206 << " DEBUG(dbgs() << \"FAIL: continuing at \" << (Ptr - DecodeTable) << \"\\n\");\n"
2207 << " // Reset decode status. This also drops a SoftFail status that could be\n"
2208 << " // set before the decode attempt.\n"
2209 << " S = MCDisassembler::Success;\n"
2210 << " }\n"
2211 << " break;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002212 << " }\n"
2213 << " case MCD::OPC_SoftFail: {\n"
2214 << " // Decode the mask values.\n"
2215 << " unsigned Len;\n"
2216 << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n"
2217 << " Ptr += Len;\n"
2218 << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n"
2219 << " Ptr += Len;\n"
2220 << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n"
2221 << " if (Fail)\n"
2222 << " S = MCDisassembler::SoftFail;\n"
2223 << " DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? \"FAIL\\n\":\"PASS\\n\"));\n"
2224 << " break;\n"
2225 << " }\n"
2226 << " case MCD::OPC_Fail: {\n"
2227 << " DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
2228 << " return MCDisassembler::Fail;\n"
2229 << " }\n"
2230 << " }\n"
2231 << " }\n"
2232 << " llvm_unreachable(\"bogosity detected in disassembler state machine!\");\n"
2233 << "}\n\n";
Owen Anderson4e818902011-02-18 21:51:29 +00002234}
2235
2236// Emits disassembler code for instruction decoding.
Craig Topper82d0d5f2012-03-16 01:19:24 +00002237void FixedLenDecoderEmitter::run(raw_ostream &o) {
Jim Grosbachecaef492012-08-14 19:06:05 +00002238 formatted_raw_ostream OS(o);
2239 OS << "#include \"llvm/MC/MCInst.h\"\n";
2240 OS << "#include \"llvm/Support/Debug.h\"\n";
2241 OS << "#include \"llvm/Support/DataTypes.h\"\n";
2242 OS << "#include \"llvm/Support/LEB128.h\"\n";
2243 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
2244 OS << "#include <assert.h>\n";
2245 OS << '\n';
2246 OS << "namespace llvm {\n\n";
2247
2248 emitFieldFromInstruction(OS);
Owen Anderson4e818902011-02-18 21:51:29 +00002249
Hal Finkel81e6fcc2013-12-17 22:37:50 +00002250 Target.reverseBitsForLittleEndianEncoding();
2251
Owen Andersonc78e03c2011-07-19 21:06:00 +00002252 // Parameterize the decoders based on namespace and instruction width.
Craig Topperf9265322016-01-17 20:38:14 +00002253 NumberedInstructions = Target.getInstructionsByEnumValue();
Owen Andersonc78e03c2011-07-19 21:06:00 +00002254 std::map<std::pair<std::string, unsigned>,
2255 std::vector<unsigned> > OpcMap;
2256 std::map<unsigned, std::vector<OperandInfo> > Operands;
2257
Craig Topperf9265322016-01-17 20:38:14 +00002258 for (unsigned i = 0; i < NumberedInstructions.size(); ++i) {
2259 const CodeGenInstruction *Inst = NumberedInstructions[i];
Craig Topper48c112b2012-03-16 05:58:09 +00002260 const Record *Def = Inst->TheDef;
Owen Andersonc78e03c2011-07-19 21:06:00 +00002261 unsigned Size = Def->getValueAsInt("Size");
2262 if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
2263 Def->getValueAsBit("isPseudo") ||
2264 Def->getValueAsBit("isAsmParserOnly") ||
2265 Def->getValueAsBit("isCodeGenOnly"))
2266 continue;
2267
2268 std::string DecoderNamespace = Def->getValueAsString("DecoderNamespace");
2269
2270 if (Size) {
Hal Finkel71b2e202013-12-19 16:12:53 +00002271 if (populateInstruction(Target, *Inst, i, Operands)) {
Owen Andersonc78e03c2011-07-19 21:06:00 +00002272 OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
2273 }
2274 }
2275 }
2276
Jim Grosbachecaef492012-08-14 19:06:05 +00002277 DecoderTableInfo TableInfo;
Craig Topper1f7604d2014-12-13 05:12:19 +00002278 for (const auto &Opc : OpcMap) {
Owen Andersonc78e03c2011-07-19 21:06:00 +00002279 // Emit the decoder for this namespace+width combination.
Craig Topperf9265322016-01-17 20:38:14 +00002280 FilterChooser FC(NumberedInstructions, Opc.second, Operands,
Craig Topper1f7604d2014-12-13 05:12:19 +00002281 8*Opc.first.second, this);
Jim Grosbachecaef492012-08-14 19:06:05 +00002282
2283 // The decode table is cleared for each top level decoder function. The
2284 // predicates and decoders themselves, however, are shared across all
2285 // decoders to give more opportunities for uniqueing.
2286 TableInfo.Table.clear();
2287 TableInfo.FixupStack.clear();
2288 TableInfo.Table.reserve(16384);
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002289 TableInfo.FixupStack.emplace_back();
Jim Grosbachecaef492012-08-14 19:06:05 +00002290 FC.emitTableEntries(TableInfo);
2291 // Any NumToSkip fixups in the top level scope can resolve to the
2292 // OPC_Fail at the end of the table.
2293 assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
2294 // Resolve any NumToSkip fixups in the current scope.
2295 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
2296 TableInfo.Table.size());
2297 TableInfo.FixupStack.clear();
2298
2299 TableInfo.Table.push_back(MCD::OPC_Fail);
2300
2301 // Print the table to the output stream.
Craig Topper1f7604d2014-12-13 05:12:19 +00002302 emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), Opc.first.first);
Jim Grosbachecaef492012-08-14 19:06:05 +00002303 OS.flush();
Owen Andersonc78e03c2011-07-19 21:06:00 +00002304 }
Owen Anderson4e818902011-02-18 21:51:29 +00002305
Jim Grosbachecaef492012-08-14 19:06:05 +00002306 // Emit the predicate function.
2307 emitPredicateFunction(OS, TableInfo.Predicates, 0);
2308
2309 // Emit the decoder function.
2310 emitDecoderFunction(OS, TableInfo.Decoders, 0);
2311
2312 // Emit the main entry point for the decoder, decodeInstruction().
2313 emitDecodeInstruction(OS);
2314
2315 OS << "\n} // End llvm namespace\n";
Owen Anderson4e818902011-02-18 21:51:29 +00002316}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00002317
2318namespace llvm {
2319
2320void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
2321 std::string PredicateNamespace,
2322 std::string GPrefix,
2323 std::string GPostfix,
2324 std::string ROK,
2325 std::string RFail,
2326 std::string L) {
2327 FixedLenDecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix,
2328 ROK, RFail, L).run(OS);
2329}
2330
2331} // End llvm namespace