blob: 7c29422ec1b9030675f712543904aee09db20407 [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;
47
48 OperandInfo(std::string D)
49 : Decoder(D) { }
50
51 void addField(unsigned Base, unsigned Width, unsigned Offset) {
52 Fields.push_back(EncodingField(Base, Width, Offset));
53 }
54
55 unsigned numFields() const { return Fields.size(); }
56
57 typedef std::vector<EncodingField>::const_iterator const_iterator;
58
59 const_iterator begin() const { return Fields.begin(); }
60 const_iterator end() const { return Fields.end(); }
61};
Jim Grosbachecaef492012-08-14 19:06:05 +000062
63typedef std::vector<uint8_t> DecoderTable;
64typedef uint32_t DecoderFixup;
65typedef std::vector<DecoderFixup> FixupList;
66typedef std::vector<FixupList> FixupScopeList;
67typedef SetVector<std::string> PredicateSet;
68typedef SetVector<std::string> DecoderSet;
69struct DecoderTableInfo {
70 DecoderTable Table;
71 FixupScopeList FixupStack;
72 PredicateSet Predicates;
73 DecoderSet Decoders;
74};
75
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000076} // End anonymous namespace
77
78namespace {
79class FixedLenDecoderEmitter {
Jim Grosbachecaef492012-08-14 19:06:05 +000080 const std::vector<const CodeGenInstruction*> *NumberedInstructions;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000081public:
82
83 // Defaults preserved here for documentation, even though they aren't
84 // strictly necessary given the way that this is currently being called.
85 FixedLenDecoderEmitter(RecordKeeper &R,
86 std::string PredicateNamespace,
87 std::string GPrefix = "if (",
88 std::string GPostfix = " == MCDisassembler::Fail)"
89 " return MCDisassembler::Fail;",
90 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
211/// even registers, while VST4q8b is a vst4 to double-spaced odd regsisters.
212///
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.
309 const std::vector<const CodeGenInstruction*> &AllInstructions;
310
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
Craig Topper5c2b4ac2014-09-03 05:49:07 +0000336 FilterChooser(const FilterChooser &) LLVM_DELETED_FUNCTION;
337 void operator=(const FilterChooser &) LLVM_DELETED_FUNCTION;
Owen Anderson4e818902011-02-18 21:51:29 +0000338public:
Owen Anderson4e818902011-02-18 21:51:29 +0000339
340 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
341 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
351 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
352 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,
Craig Topper48c112b2012-03-16 05:58:09 +0000451 const OperandInfo &OpInfo) const;
Owen Andersone3591652011-07-28 21:54:31 +0000452
Jim Grosbachecaef492012-08-14 19:06:05 +0000453 void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc) const;
454 unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc) const;
455
Owen Anderson4e818902011-02-18 21:51:29 +0000456 // Assign a single filter and run with it.
Craig Topper48c112b2012-03-16 05:58:09 +0000457 void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed);
Owen Anderson4e818902011-02-18 21:51:29 +0000458
459 // reportRegion is a helper function for filterProcessor to mark a region as
460 // eligible for use as a filter region.
461 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000462 bool AllowMixed);
Owen Anderson4e818902011-02-18 21:51:29 +0000463
464 // FilterProcessor scans the well-known encoding bits of the instructions and
465 // builds up a list of candidate filters. It chooses the best filter and
466 // recursively descends down the decoding tree.
467 bool filterProcessor(bool AllowMixed, bool Greedy = true);
468
469 // Decides on the best configuration of filter(s) to use in order to decode
470 // the instructions. A conflict of instructions may occur, in which case we
471 // dump the conflict set to the standard error.
472 void doFilter();
473
Jim Grosbachecaef492012-08-14 19:06:05 +0000474public:
475 // emitTableEntries - Emit state machine entries to decode our share of
476 // instructions.
477 void emitTableEntries(DecoderTableInfo &TableInfo) const;
Owen Anderson4e818902011-02-18 21:51:29 +0000478};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000479} // End anonymous namespace
Owen Anderson4e818902011-02-18 21:51:29 +0000480
481///////////////////////////
482// //
Craig Topper93e64342012-03-16 00:56:01 +0000483// Filter Implementation //
Owen Anderson4e818902011-02-18 21:51:29 +0000484// //
485///////////////////////////
486
Craig Topper5c2b4ac2014-09-03 05:49:07 +0000487Filter::Filter(Filter &&f)
Craig Topper82d0d5f2012-03-16 01:19:24 +0000488 : Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
Craig Topper5c2b4ac2014-09-03 05:49:07 +0000489 FilteredInstructions(std::move(f.FilteredInstructions)),
490 VariableInstructions(std::move(f.VariableInstructions)),
491 FilterChooserMap(std::move(f.FilterChooserMap)), NumFiltered(f.NumFiltered),
Craig Topper82d0d5f2012-03-16 01:19:24 +0000492 LastOpcFiltered(f.LastOpcFiltered) {
Owen Anderson4e818902011-02-18 21:51:29 +0000493}
494
495Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000496 bool mixed)
497 : Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) {
Owen Andersonc78e03c2011-07-19 21:06:00 +0000498 assert(StartBit + NumBits - 1 < Owner->BitWidth);
Owen Anderson4e818902011-02-18 21:51:29 +0000499
500 NumFiltered = 0;
501 LastOpcFiltered = 0;
Owen Anderson4e818902011-02-18 21:51:29 +0000502
503 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
504 insn_t Insn;
505
506 // Populates the insn given the uid.
507 Owner->insnWithID(Insn, Owner->Opcodes[i]);
508
509 uint64_t Field;
510 // Scans the segment for possibly well-specified encoding bits.
511 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
512
513 if (ok) {
514 // The encoding bits are well-known. Lets add the uid of the
515 // instruction into the bucket keyed off the constant field value.
516 LastOpcFiltered = Owner->Opcodes[i];
517 FilteredInstructions[Field].push_back(LastOpcFiltered);
518 ++NumFiltered;
519 } else {
Craig Topper93e64342012-03-16 00:56:01 +0000520 // Some of the encoding bit(s) are unspecified. This contributes to
Owen Anderson4e818902011-02-18 21:51:29 +0000521 // one additional member of "Variable" instructions.
522 VariableInstructions.push_back(Owner->Opcodes[i]);
Owen Anderson4e818902011-02-18 21:51:29 +0000523 }
524 }
525
526 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
527 && "Filter returns no instruction categories");
528}
529
530Filter::~Filter() {
Owen Anderson4e818902011-02-18 21:51:29 +0000531}
532
533// Divides the decoding task into sub tasks and delegates them to the
534// inferior FilterChooser's.
535//
536// A special case arises when there's only one entry in the filtered
537// instructions. In order to unambiguously decode the singleton, we need to
538// match the remaining undecoded encoding bits against the singleton.
539void Filter::recurse() {
Owen Anderson4e818902011-02-18 21:51:29 +0000540 // Starts by inheriting our parent filter chooser's filter bit values.
Owen Andersonc78e03c2011-07-19 21:06:00 +0000541 std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
Owen Anderson4e818902011-02-18 21:51:29 +0000542
Owen Anderson4e818902011-02-18 21:51:29 +0000543 if (VariableInstructions.size()) {
544 // Conservatively marks each segment position as BIT_UNSET.
Craig Topper29688ab2012-08-17 05:42:16 +0000545 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex)
Owen Anderson4e818902011-02-18 21:51:29 +0000546 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
547
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000548 // Delegates to an inferior filter chooser for further processing on this
Owen Anderson4e818902011-02-18 21:51:29 +0000549 // group of instructions whose segment values are variable.
Yaron Kerene499db02014-09-03 08:22:30 +0000550 FilterChooserMap.insert(
551 std::make_pair(-1U, llvm::make_unique<FilterChooser>(
552 Owner->AllInstructions, VariableInstructions,
553 Owner->Operands, BitValueArray, *Owner)));
Owen Anderson4e818902011-02-18 21:51:29 +0000554 }
555
556 // No need to recurse for a singleton filtered instruction.
Jim Grosbachecaef492012-08-14 19:06:05 +0000557 // See also Filter::emit*().
Owen Anderson4e818902011-02-18 21:51:29 +0000558 if (getNumFiltered() == 1) {
559 //Owner->SingletonExists(LastOpcFiltered);
560 assert(FilterChooserMap.size() == 1);
561 return;
562 }
563
564 // Otherwise, create sub choosers.
Craig Topper1f7604d2014-12-13 05:12:19 +0000565 for (const auto &Inst : FilteredInstructions) {
Owen Anderson4e818902011-02-18 21:51:29 +0000566
567 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
Craig Topper29688ab2012-08-17 05:42:16 +0000568 for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) {
Craig Topper1f7604d2014-12-13 05:12:19 +0000569 if (Inst.first & (1ULL << bitIndex))
Owen Anderson4e818902011-02-18 21:51:29 +0000570 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
571 else
572 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
573 }
574
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000575 // Delegates to an inferior filter chooser for further processing on this
Owen Anderson4e818902011-02-18 21:51:29 +0000576 // category of instructions.
Craig Toppercf05f912014-09-03 06:07:54 +0000577 FilterChooserMap.insert(std::make_pair(
Craig Topper1f7604d2014-12-13 05:12:19 +0000578 Inst.first, llvm::make_unique<FilterChooser>(
579 Owner->AllInstructions, Inst.second,
Yaron Kerene499db02014-09-03 08:22:30 +0000580 Owner->Operands, BitValueArray, *Owner)));
Owen Anderson4e818902011-02-18 21:51:29 +0000581 }
582}
583
Jim Grosbachecaef492012-08-14 19:06:05 +0000584static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
585 uint32_t DestIdx) {
586 // Any NumToSkip fixups in the current scope can resolve to the
587 // current location.
588 for (FixupList::const_reverse_iterator I = Fixups.rbegin(),
589 E = Fixups.rend();
590 I != E; ++I) {
591 // Calculate the distance from the byte following the fixup entry byte
592 // to the destination. The Target is calculated from after the 16-bit
593 // NumToSkip entry itself, so subtract two from the displacement here
594 // to account for that.
595 uint32_t FixupIdx = *I;
596 uint32_t Delta = DestIdx - FixupIdx - 2;
597 // Our NumToSkip entries are 16-bits. Make sure our table isn't too
598 // big.
599 assert(Delta < 65536U && "disassembler decoding table too large!");
600 Table[FixupIdx] = (uint8_t)Delta;
601 Table[FixupIdx + 1] = (uint8_t)(Delta >> 8);
602 }
603}
Owen Anderson4e818902011-02-18 21:51:29 +0000604
Jim Grosbachecaef492012-08-14 19:06:05 +0000605// Emit table entries to decode instructions given a segment or segments
606// of bits.
607void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
608 TableInfo.Table.push_back(MCD::OPC_ExtractField);
609 TableInfo.Table.push_back(StartBit);
610 TableInfo.Table.push_back(NumBits);
Owen Anderson4e818902011-02-18 21:51:29 +0000611
Jim Grosbachecaef492012-08-14 19:06:05 +0000612 // A new filter entry begins a new scope for fixup resolution.
613 TableInfo.FixupStack.push_back(FixupList());
Owen Anderson4e818902011-02-18 21:51:29 +0000614
Jim Grosbachecaef492012-08-14 19:06:05 +0000615 DecoderTable &Table = TableInfo.Table;
616
617 size_t PrevFilter = 0;
618 bool HasFallthrough = false;
Craig Topper1f7604d2014-12-13 05:12:19 +0000619 for (auto &Filter : FilterChooserMap) {
Owen Anderson4e818902011-02-18 21:51:29 +0000620 // Field value -1 implies a non-empty set of variable instructions.
621 // See also recurse().
Craig Topper1f7604d2014-12-13 05:12:19 +0000622 if (Filter.first == (unsigned)-1) {
Jim Grosbachecaef492012-08-14 19:06:05 +0000623 HasFallthrough = true;
Owen Anderson4e818902011-02-18 21:51:29 +0000624
Jim Grosbachecaef492012-08-14 19:06:05 +0000625 // Each scope should always have at least one filter value to check
626 // for.
627 assert(PrevFilter != 0 && "empty filter set!");
628 FixupList &CurScope = TableInfo.FixupStack.back();
629 // Resolve any NumToSkip fixups in the current scope.
630 resolveTableFixups(Table, CurScope, Table.size());
631 CurScope.clear();
632 PrevFilter = 0; // Don't re-process the filter's fallthrough.
633 } else {
634 Table.push_back(MCD::OPC_FilterValue);
635 // Encode and emit the value to filter against.
636 uint8_t Buffer[8];
Craig Topper1f7604d2014-12-13 05:12:19 +0000637 unsigned Len = encodeULEB128(Filter.first, Buffer);
Jim Grosbachecaef492012-08-14 19:06:05 +0000638 Table.insert(Table.end(), Buffer, Buffer + Len);
639 // Reserve space for the NumToSkip entry. We'll backpatch the value
640 // later.
641 PrevFilter = Table.size();
642 Table.push_back(0);
643 Table.push_back(0);
644 }
Owen Anderson4e818902011-02-18 21:51:29 +0000645
646 // We arrive at a category of instructions with the same segment value.
647 // Now delegate to the sub filter chooser for further decodings.
648 // The case may fallthrough, which happens if the remaining well-known
649 // encoding bits do not match exactly.
Craig Topper1f7604d2014-12-13 05:12:19 +0000650 Filter.second->emitTableEntries(TableInfo);
Owen Anderson4e818902011-02-18 21:51:29 +0000651
Jim Grosbachecaef492012-08-14 19:06:05 +0000652 // Now that we've emitted the body of the handler, update the NumToSkip
653 // of the filter itself to be able to skip forward when false. Subtract
654 // two as to account for the width of the NumToSkip field itself.
655 if (PrevFilter) {
656 uint32_t NumToSkip = Table.size() - PrevFilter - 2;
657 assert(NumToSkip < 65536U && "disassembler decoding table too large!");
658 Table[PrevFilter] = (uint8_t)NumToSkip;
659 Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8);
660 }
Owen Anderson4e818902011-02-18 21:51:29 +0000661 }
662
Jim Grosbachecaef492012-08-14 19:06:05 +0000663 // Any remaining unresolved fixups bubble up to the parent fixup scope.
664 assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!");
665 FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1;
666 FixupScopeList::iterator Dest = Source - 1;
667 Dest->insert(Dest->end(), Source->begin(), Source->end());
668 TableInfo.FixupStack.pop_back();
669
670 // If there is no fallthrough, then the final filter should get fixed
671 // up according to the enclosing scope rather than the current position.
672 if (!HasFallthrough)
673 TableInfo.FixupStack.back().push_back(PrevFilter);
Owen Anderson4e818902011-02-18 21:51:29 +0000674}
675
676// Returns the number of fanout produced by the filter. More fanout implies
677// the filter distinguishes more categories of instructions.
678unsigned Filter::usefulness() const {
679 if (VariableInstructions.size())
680 return FilteredInstructions.size();
681 else
682 return FilteredInstructions.size() + 1;
683}
684
685//////////////////////////////////
686// //
687// Filterchooser Implementation //
688// //
689//////////////////////////////////
690
Jim Grosbachecaef492012-08-14 19:06:05 +0000691// Emit the decoder state machine table.
692void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
693 DecoderTable &Table,
694 unsigned Indentation,
695 unsigned BitWidth,
696 StringRef Namespace) const {
697 OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace
698 << BitWidth << "[] = {\n";
Owen Anderson4e818902011-02-18 21:51:29 +0000699
Jim Grosbachecaef492012-08-14 19:06:05 +0000700 Indentation += 2;
Owen Anderson4e818902011-02-18 21:51:29 +0000701
Jim Grosbachecaef492012-08-14 19:06:05 +0000702 // FIXME: We may be able to use the NumToSkip values to recover
703 // appropriate indentation levels.
704 DecoderTable::const_iterator I = Table.begin();
705 DecoderTable::const_iterator E = Table.end();
706 while (I != E) {
707 assert (I < E && "incomplete decode table entry!");
Owen Anderson4e818902011-02-18 21:51:29 +0000708
Jim Grosbachecaef492012-08-14 19:06:05 +0000709 uint64_t Pos = I - Table.begin();
710 OS << "/* " << Pos << " */";
711 OS.PadToColumn(12);
Owen Anderson4e818902011-02-18 21:51:29 +0000712
Jim Grosbachecaef492012-08-14 19:06:05 +0000713 switch (*I) {
714 default:
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000715 PrintFatalError("invalid decode table opcode");
Jim Grosbachecaef492012-08-14 19:06:05 +0000716 case MCD::OPC_ExtractField: {
717 ++I;
718 unsigned Start = *I++;
719 unsigned Len = *I++;
720 OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
721 << Len << ", // Inst{";
722 if (Len > 1)
723 OS << (Start + Len - 1) << "-";
724 OS << Start << "} ...\n";
725 break;
726 }
727 case MCD::OPC_FilterValue: {
728 ++I;
729 OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
730 // The filter value is ULEB128 encoded.
731 while (*I >= 128)
732 OS << utostr(*I++) << ", ";
733 OS << utostr(*I++) << ", ";
734
735 // 16-bit numtoskip value.
736 uint8_t Byte = *I++;
737 uint32_t NumToSkip = Byte;
738 OS << utostr(Byte) << ", ";
739 Byte = *I++;
740 OS << utostr(Byte) << ", ";
741 NumToSkip |= Byte << 8;
742 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
743 break;
744 }
745 case MCD::OPC_CheckField: {
746 ++I;
747 unsigned Start = *I++;
748 unsigned Len = *I++;
749 OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
750 << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
751 // ULEB128 encoded field value.
752 for (; *I >= 128; ++I)
753 OS << utostr(*I) << ", ";
754 OS << utostr(*I++) << ", ";
755 // 16-bit numtoskip value.
756 uint8_t Byte = *I++;
757 uint32_t NumToSkip = Byte;
758 OS << utostr(Byte) << ", ";
759 Byte = *I++;
760 OS << utostr(Byte) << ", ";
761 NumToSkip |= Byte << 8;
762 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
763 break;
764 }
765 case MCD::OPC_CheckPredicate: {
766 ++I;
767 OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
768 for (; *I >= 128; ++I)
769 OS << utostr(*I) << ", ";
770 OS << utostr(*I++) << ", ";
771
772 // 16-bit numtoskip value.
773 uint8_t Byte = *I++;
774 uint32_t NumToSkip = Byte;
775 OS << utostr(Byte) << ", ";
776 Byte = *I++;
777 OS << utostr(Byte) << ", ";
778 NumToSkip |= Byte << 8;
779 OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
780 break;
781 }
782 case MCD::OPC_Decode: {
783 ++I;
784 // Extract the ULEB128 encoded Opcode to a buffer.
785 uint8_t Buffer[8], *p = Buffer;
786 while ((*p++ = *I++) >= 128)
787 assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer)
788 && "ULEB128 value too large!");
789 // Decode the Opcode value.
790 unsigned Opc = decodeULEB128(Buffer);
791 OS.indent(Indentation) << "MCD::OPC_Decode, ";
792 for (p = Buffer; *p >= 128; ++p)
793 OS << utostr(*p) << ", ";
794 OS << utostr(*p) << ", ";
795
796 // Decoder index.
797 for (; *I >= 128; ++I)
798 OS << utostr(*I) << ", ";
799 OS << utostr(*I++) << ", ";
800
801 OS << "// Opcode: "
802 << NumberedInstructions->at(Opc)->TheDef->getName() << "\n";
803 break;
804 }
805 case MCD::OPC_SoftFail: {
806 ++I;
807 OS.indent(Indentation) << "MCD::OPC_SoftFail";
808 // Positive mask
809 uint64_t Value = 0;
810 unsigned Shift = 0;
811 do {
812 OS << ", " << utostr(*I);
813 Value += (*I & 0x7f) << Shift;
814 Shift += 7;
815 } while (*I++ >= 128);
816 if (Value > 127)
817 OS << " /* 0x" << utohexstr(Value) << " */";
818 // Negative mask
819 Value = 0;
820 Shift = 0;
821 do {
822 OS << ", " << utostr(*I);
823 Value += (*I & 0x7f) << Shift;
824 Shift += 7;
825 } while (*I++ >= 128);
826 if (Value > 127)
827 OS << " /* 0x" << utohexstr(Value) << " */";
828 OS << ",\n";
829 break;
830 }
831 case MCD::OPC_Fail: {
832 ++I;
833 OS.indent(Indentation) << "MCD::OPC_Fail,\n";
834 break;
835 }
836 }
837 }
838 OS.indent(Indentation) << "0\n";
839
840 Indentation -= 2;
841
842 OS.indent(Indentation) << "};\n\n";
843}
844
845void FixedLenDecoderEmitter::
846emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
847 unsigned Indentation) const {
848 // The predicate function is just a big switch statement based on the
849 // input predicate index.
850 OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
851 << "uint64_t Bits) {\n";
852 Indentation += 2;
Aaron Ballmane59e3582013-07-15 16:53:32 +0000853 if (!Predicates.empty()) {
854 OS.indent(Indentation) << "switch (Idx) {\n";
855 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
856 unsigned Index = 0;
Craig Topper1f7604d2014-12-13 05:12:19 +0000857 for (const auto &Predicate : Predicates) {
858 OS.indent(Indentation) << "case " << Index++ << ":\n";
859 OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
Aaron Ballmane59e3582013-07-15 16:53:32 +0000860 }
861 OS.indent(Indentation) << "}\n";
862 } else {
863 // No case statement to emit
864 OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000865 }
Jim Grosbachecaef492012-08-14 19:06:05 +0000866 Indentation -= 2;
867 OS.indent(Indentation) << "}\n\n";
868}
869
870void FixedLenDecoderEmitter::
871emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
872 unsigned Indentation) const {
873 // The decoder function is just a big switch statement based on the
874 // input decoder index.
875 OS.indent(Indentation) << "template<typename InsnType>\n";
876 OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
877 << " unsigned Idx, InsnType insn, MCInst &MI,\n";
878 OS.indent(Indentation) << " uint64_t "
Benjamin Kramer26b568d2012-08-15 10:26:44 +0000879 << "Address, const void *Decoder) {\n";
Jim Grosbachecaef492012-08-14 19:06:05 +0000880 Indentation += 2;
881 OS.indent(Indentation) << "InsnType tmp;\n";
882 OS.indent(Indentation) << "switch (Idx) {\n";
883 OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
884 unsigned Index = 0;
Craig Topper1f7604d2014-12-13 05:12:19 +0000885 for (const auto &Decoder : Decoders) {
886 OS.indent(Indentation) << "case " << Index++ << ":\n";
887 OS << Decoder;
Jim Grosbachecaef492012-08-14 19:06:05 +0000888 OS.indent(Indentation+2) << "return S;\n";
889 }
890 OS.indent(Indentation) << "}\n";
891 Indentation -= 2;
892 OS.indent(Indentation) << "}\n\n";
Owen Anderson4e818902011-02-18 21:51:29 +0000893}
894
895// Populates the field of the insn given the start position and the number of
896// consecutive bits to scan for.
897//
898// Returns false if and on the first uninitialized bit value encountered.
899// Returns true, otherwise.
900bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
Craig Topper48c112b2012-03-16 05:58:09 +0000901 unsigned StartBit, unsigned NumBits) const {
Owen Anderson4e818902011-02-18 21:51:29 +0000902 Field = 0;
903
904 for (unsigned i = 0; i < NumBits; ++i) {
905 if (Insn[StartBit + i] == BIT_UNSET)
906 return false;
907
908 if (Insn[StartBit + i] == BIT_TRUE)
909 Field = Field | (1ULL << i);
910 }
911
912 return true;
913}
914
915/// dumpFilterArray - dumpFilterArray prints out debugging info for the given
916/// filter array as a series of chars.
917void FilterChooser::dumpFilterArray(raw_ostream &o,
Craig Topper48c112b2012-03-16 05:58:09 +0000918 const std::vector<bit_value_t> &filter) const {
Craig Topper29688ab2012-08-17 05:42:16 +0000919 for (unsigned bitIndex = BitWidth; bitIndex > 0; bitIndex--) {
Owen Anderson4e818902011-02-18 21:51:29 +0000920 switch (filter[bitIndex - 1]) {
921 case BIT_UNFILTERED:
922 o << ".";
923 break;
924 case BIT_UNSET:
925 o << "_";
926 break;
927 case BIT_TRUE:
928 o << "1";
929 break;
930 case BIT_FALSE:
931 o << "0";
932 break;
933 }
934 }
935}
936
937/// dumpStack - dumpStack traverses the filter chooser chain and calls
938/// dumpFilterArray on each filter chooser up to the top level one.
Craig Topper48c112b2012-03-16 05:58:09 +0000939void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const {
940 const FilterChooser *current = this;
Owen Anderson4e818902011-02-18 21:51:29 +0000941
942 while (current) {
943 o << prefix;
944 dumpFilterArray(o, current->FilterBitValues);
945 o << '\n';
946 current = current->Parent;
947 }
948}
949
950// Called from Filter::recurse() when singleton exists. For debug purpose.
Craig Topper48c112b2012-03-16 05:58:09 +0000951void FilterChooser::SingletonExists(unsigned Opc) const {
Owen Anderson4e818902011-02-18 21:51:29 +0000952 insn_t Insn0;
953 insnWithID(Insn0, Opc);
954
955 errs() << "Singleton exists: " << nameWithID(Opc)
956 << " with its decoding dominating ";
957 for (unsigned i = 0; i < Opcodes.size(); ++i) {
958 if (Opcodes[i] == Opc) continue;
959 errs() << nameWithID(Opcodes[i]) << ' ';
960 }
961 errs() << '\n';
962
963 dumpStack(errs(), "\t\t");
Craig Topper82d0d5f2012-03-16 01:19:24 +0000964 for (unsigned i = 0; i < Opcodes.size(); ++i) {
Owen Anderson4e818902011-02-18 21:51:29 +0000965 const std::string &Name = nameWithID(Opcodes[i]);
966
967 errs() << '\t' << Name << " ";
968 dumpBits(errs(),
969 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
970 errs() << '\n';
971 }
972}
973
974// Calculates the island(s) needed to decode the instruction.
975// This returns a list of undecoded bits of an instructions, for example,
976// Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
977// decoded bits in order to verify that the instruction matches the Opcode.
978unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
Craig Topper82d0d5f2012-03-16 01:19:24 +0000979 std::vector<unsigned> &EndBits,
980 std::vector<uint64_t> &FieldVals,
Craig Topper48c112b2012-03-16 05:58:09 +0000981 const insn_t &Insn) const {
Owen Anderson4e818902011-02-18 21:51:29 +0000982 unsigned Num, BitNo;
983 Num = BitNo = 0;
984
985 uint64_t FieldVal = 0;
986
987 // 0: Init
988 // 1: Water (the bit value does not affect decoding)
989 // 2: Island (well-known bit value needed for decoding)
990 int State = 0;
991 int Val = -1;
992
Owen Andersonc78e03c2011-07-19 21:06:00 +0000993 for (unsigned i = 0; i < BitWidth; ++i) {
Owen Anderson4e818902011-02-18 21:51:29 +0000994 Val = Value(Insn[i]);
995 bool Filtered = PositionFiltered(i);
996 switch (State) {
Craig Topperc4965bc2012-02-05 07:21:30 +0000997 default: llvm_unreachable("Unreachable code!");
Owen Anderson4e818902011-02-18 21:51:29 +0000998 case 0:
999 case 1:
1000 if (Filtered || Val == -1)
1001 State = 1; // Still in Water
1002 else {
1003 State = 2; // Into the Island
1004 BitNo = 0;
1005 StartBits.push_back(i);
1006 FieldVal = Val;
1007 }
1008 break;
1009 case 2:
1010 if (Filtered || Val == -1) {
1011 State = 1; // Into the Water
1012 EndBits.push_back(i - 1);
1013 FieldVals.push_back(FieldVal);
1014 ++Num;
1015 } else {
1016 State = 2; // Still in Island
1017 ++BitNo;
1018 FieldVal = FieldVal | Val << BitNo;
1019 }
1020 break;
1021 }
1022 }
1023 // If we are still in Island after the loop, do some housekeeping.
1024 if (State == 2) {
Owen Andersonc78e03c2011-07-19 21:06:00 +00001025 EndBits.push_back(BitWidth - 1);
Owen Anderson4e818902011-02-18 21:51:29 +00001026 FieldVals.push_back(FieldVal);
1027 ++Num;
1028 }
1029
1030 assert(StartBits.size() == Num && EndBits.size() == Num &&
1031 FieldVals.size() == Num);
1032 return Num;
1033}
1034
Owen Andersone3591652011-07-28 21:54:31 +00001035void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
Craig Topper48c112b2012-03-16 05:58:09 +00001036 const OperandInfo &OpInfo) const {
1037 const std::string &Decoder = OpInfo.Decoder;
Owen Andersone3591652011-07-28 21:54:31 +00001038
Craig Topper5546f8c2014-09-27 05:26:42 +00001039 if (OpInfo.numFields() != 1)
Craig Topperebc3aa22012-08-17 05:16:15 +00001040 o.indent(Indentation) << "tmp = 0;\n";
Craig Topper5546f8c2014-09-27 05:26:42 +00001041
1042 for (const EncodingField &EF : OpInfo) {
1043 o.indent(Indentation) << "tmp ";
1044 if (OpInfo.numFields() != 1) o << '|';
1045 o << "= fieldFromInstruction"
1046 << "(insn, " << EF.Base << ", " << EF.Width << ')';
1047 if (OpInfo.numFields() != 1 || EF.Offset != 0)
1048 o << " << " << EF.Offset;
1049 o << ";\n";
Owen Andersone3591652011-07-28 21:54:31 +00001050 }
1051
1052 if (Decoder != "")
Craig Topperebc3aa22012-08-17 05:16:15 +00001053 o.indent(Indentation) << Emitter->GuardPrefix << Decoder
Jim Grosbach3f4b2392012-02-29 22:07:56 +00001054 << "(MI, tmp, Address, Decoder)"
1055 << Emitter->GuardPostfix << "\n";
Owen Andersone3591652011-07-28 21:54:31 +00001056 else
Craig Topperebc3aa22012-08-17 05:16:15 +00001057 o.indent(Indentation) << "MI.addOperand(MCOperand::CreateImm(tmp));\n";
Owen Andersone3591652011-07-28 21:54:31 +00001058
1059}
1060
Jim Grosbachecaef492012-08-14 19:06:05 +00001061void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
1062 unsigned Opc) const {
Craig Topper1f7604d2014-12-13 05:12:19 +00001063 for (const auto &Op : Operands.find(Opc)->second) {
Jim Grosbachecaef492012-08-14 19:06:05 +00001064 // If a custom instruction decoder was specified, use that.
Craig Topper1f7604d2014-12-13 05:12:19 +00001065 if (Op.numFields() == 0 && Op.Decoder.size()) {
1066 OS.indent(Indentation) << Emitter->GuardPrefix << Op.Decoder
Jim Grosbachecaef492012-08-14 19:06:05 +00001067 << "(MI, insn, Address, Decoder)"
1068 << Emitter->GuardPostfix << "\n";
1069 break;
1070 }
1071
Craig Topper1f7604d2014-12-13 05:12:19 +00001072 emitBinaryParser(OS, Indentation, Op);
Jim Grosbachecaef492012-08-14 19:06:05 +00001073 }
1074}
1075
1076unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
1077 unsigned Opc) const {
1078 // Build up the predicate string.
1079 SmallString<256> Decoder;
1080 // FIXME: emitDecoder() function can take a buffer directly rather than
1081 // a stream.
1082 raw_svector_ostream S(Decoder);
Craig Topperebc3aa22012-08-17 05:16:15 +00001083 unsigned I = 4;
Jim Grosbachecaef492012-08-14 19:06:05 +00001084 emitDecoder(S, I, Opc);
1085 S.flush();
1086
1087 // Using the full decoder string as the key value here is a bit
1088 // heavyweight, but is effective. If the string comparisons become a
1089 // performance concern, we can implement a mangling of the predicate
1090 // data easilly enough with a map back to the actual string. That's
1091 // overkill for now, though.
1092
1093 // Make sure the predicate is in the table.
1094 Decoders.insert(Decoder.str());
1095 // Now figure out the index for when we write out the table.
1096 DecoderSet::const_iterator P = std::find(Decoders.begin(),
1097 Decoders.end(),
1098 Decoder.str());
1099 return (unsigned)(P - Decoders.begin());
1100}
1101
James Molloy8067df92011-09-07 19:42:28 +00001102static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
Craig Topper48c112b2012-03-16 05:58:09 +00001103 const std::string &PredicateNamespace) {
Andrew Trick43674ad2011-09-08 05:25:49 +00001104 if (str[0] == '!')
1105 o << "!(Bits & " << PredicateNamespace << "::"
1106 << str.slice(1,str.size()) << ")";
James Molloy8067df92011-09-07 19:42:28 +00001107 else
Andrew Trick43674ad2011-09-08 05:25:49 +00001108 o << "(Bits & " << PredicateNamespace << "::" << str << ")";
James Molloy8067df92011-09-07 19:42:28 +00001109}
1110
1111bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
Craig Topper48c112b2012-03-16 05:58:09 +00001112 unsigned Opc) const {
Jim Grosbach3f4b2392012-02-29 22:07:56 +00001113 ListInit *Predicates =
1114 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
James Molloy8067df92011-09-07 19:42:28 +00001115 for (unsigned i = 0; i < Predicates->getSize(); ++i) {
1116 Record *Pred = Predicates->getElementAsRecord(i);
1117 if (!Pred->getValue("AssemblerMatcherPredicate"))
1118 continue;
1119
1120 std::string P = Pred->getValueAsString("AssemblerCondString");
1121
1122 if (!P.length())
1123 continue;
1124
1125 if (i != 0)
1126 o << " && ";
1127
1128 StringRef SR(P);
1129 std::pair<StringRef, StringRef> pairs = SR.split(',');
1130 while (pairs.second.size()) {
1131 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1132 o << " && ";
1133 pairs = pairs.second.split(',');
1134 }
1135 emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace);
1136 }
1137 return Predicates->getSize() > 0;
Andrew Trick61abca62011-09-08 05:23:14 +00001138}
James Molloy8067df92011-09-07 19:42:28 +00001139
Jim Grosbachecaef492012-08-14 19:06:05 +00001140bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const {
1141 ListInit *Predicates =
1142 AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates");
1143 for (unsigned i = 0; i < Predicates->getSize(); ++i) {
1144 Record *Pred = Predicates->getElementAsRecord(i);
1145 if (!Pred->getValue("AssemblerMatcherPredicate"))
1146 continue;
1147
1148 std::string P = Pred->getValueAsString("AssemblerCondString");
1149
1150 if (!P.length())
1151 continue;
1152
1153 return true;
1154 }
1155 return false;
1156}
1157
1158unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo,
1159 StringRef Predicate) const {
1160 // Using the full predicate string as the key value here is a bit
1161 // heavyweight, but is effective. If the string comparisons become a
1162 // performance concern, we can implement a mangling of the predicate
1163 // data easilly enough with a map back to the actual string. That's
1164 // overkill for now, though.
1165
1166 // Make sure the predicate is in the table.
1167 TableInfo.Predicates.insert(Predicate.str());
1168 // Now figure out the index for when we write out the table.
1169 PredicateSet::const_iterator P = std::find(TableInfo.Predicates.begin(),
1170 TableInfo.Predicates.end(),
1171 Predicate.str());
1172 return (unsigned)(P - TableInfo.Predicates.begin());
1173}
1174
1175void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
1176 unsigned Opc) const {
1177 if (!doesOpcodeNeedPredicate(Opc))
1178 return;
1179
1180 // Build up the predicate string.
1181 SmallString<256> Predicate;
1182 // FIXME: emitPredicateMatch() functions can take a buffer directly rather
1183 // than a stream.
1184 raw_svector_ostream PS(Predicate);
1185 unsigned I = 0;
1186 emitPredicateMatch(PS, I, Opc);
1187
1188 // Figure out the index into the predicate table for the predicate just
1189 // computed.
1190 unsigned PIdx = getPredicateIndex(TableInfo, PS.str());
1191 SmallString<16> PBytes;
1192 raw_svector_ostream S(PBytes);
1193 encodeULEB128(PIdx, S);
1194 S.flush();
1195
1196 TableInfo.Table.push_back(MCD::OPC_CheckPredicate);
1197 // Predicate index
Craig Topper29688ab2012-08-17 05:42:16 +00001198 for (unsigned i = 0, e = PBytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001199 TableInfo.Table.push_back(PBytes[i]);
1200 // Push location for NumToSkip backpatching.
1201 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1202 TableInfo.Table.push_back(0);
1203 TableInfo.Table.push_back(0);
1204}
1205
1206void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
1207 unsigned Opc) const {
Jim Grosbach3f4b2392012-02-29 22:07:56 +00001208 BitsInit *SFBits =
1209 AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail");
James Molloyd9ba4fd2012-02-09 10:56:31 +00001210 if (!SFBits) return;
1211 BitsInit *InstBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("Inst");
1212
1213 APInt PositiveMask(BitWidth, 0ULL);
1214 APInt NegativeMask(BitWidth, 0ULL);
1215 for (unsigned i = 0; i < BitWidth; ++i) {
1216 bit_value_t B = bitFromBits(*SFBits, i);
1217 bit_value_t IB = bitFromBits(*InstBits, i);
1218
1219 if (B != BIT_TRUE) continue;
1220
1221 switch (IB) {
1222 case BIT_FALSE:
1223 // The bit is meant to be false, so emit a check to see if it is true.
1224 PositiveMask.setBit(i);
1225 break;
1226 case BIT_TRUE:
1227 // The bit is meant to be true, so emit a check to see if it is false.
1228 NegativeMask.setBit(i);
1229 break;
1230 default:
1231 // The bit is not set; this must be an error!
1232 StringRef Name = AllInstructions[Opc]->TheDef->getName();
Jim Grosbachecaef492012-08-14 19:06:05 +00001233 errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in " << Name
1234 << " is set but Inst{" << i << "} is unset!\n"
James Molloyd9ba4fd2012-02-09 10:56:31 +00001235 << " - You can only mark a bit as SoftFail if it is fully defined"
1236 << " (1/0 - not '?') in Inst\n";
Jim Grosbachecaef492012-08-14 19:06:05 +00001237 return;
James Molloyd9ba4fd2012-02-09 10:56:31 +00001238 }
1239 }
1240
1241 bool NeedPositiveMask = PositiveMask.getBoolValue();
1242 bool NeedNegativeMask = NegativeMask.getBoolValue();
1243
1244 if (!NeedPositiveMask && !NeedNegativeMask)
1245 return;
1246
Jim Grosbachecaef492012-08-14 19:06:05 +00001247 TableInfo.Table.push_back(MCD::OPC_SoftFail);
James Molloyd9ba4fd2012-02-09 10:56:31 +00001248
Jim Grosbachecaef492012-08-14 19:06:05 +00001249 SmallString<16> MaskBytes;
1250 raw_svector_ostream S(MaskBytes);
1251 if (NeedPositiveMask) {
1252 encodeULEB128(PositiveMask.getZExtValue(), S);
1253 S.flush();
Craig Topper29688ab2012-08-17 05:42:16 +00001254 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001255 TableInfo.Table.push_back(MaskBytes[i]);
1256 } else
1257 TableInfo.Table.push_back(0);
1258 if (NeedNegativeMask) {
1259 MaskBytes.clear();
1260 S.resync();
1261 encodeULEB128(NegativeMask.getZExtValue(), S);
1262 S.flush();
Craig Topper29688ab2012-08-17 05:42:16 +00001263 for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001264 TableInfo.Table.push_back(MaskBytes[i]);
1265 } else
1266 TableInfo.Table.push_back(0);
James Molloyd9ba4fd2012-02-09 10:56:31 +00001267}
1268
Jim Grosbachecaef492012-08-14 19:06:05 +00001269// Emits table entries to decode the singleton.
1270void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1271 unsigned Opc) const {
Owen Anderson4e818902011-02-18 21:51:29 +00001272 std::vector<unsigned> StartBits;
1273 std::vector<unsigned> EndBits;
1274 std::vector<uint64_t> FieldVals;
1275 insn_t Insn;
1276 insnWithID(Insn, Opc);
1277
1278 // Look for islands of undecoded bits of the singleton.
1279 getIslands(StartBits, EndBits, FieldVals, Insn);
1280
1281 unsigned Size = StartBits.size();
Owen Anderson4e818902011-02-18 21:51:29 +00001282
Jim Grosbachecaef492012-08-14 19:06:05 +00001283 // Emit the predicate table entry if one is needed.
1284 emitPredicateTableEntry(TableInfo, Opc);
Owen Anderson4e818902011-02-18 21:51:29 +00001285
Jim Grosbachecaef492012-08-14 19:06:05 +00001286 // Check any additional encoding fields needed.
Craig Topper29688ab2012-08-17 05:42:16 +00001287 for (unsigned I = Size; I != 0; --I) {
1288 unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1;
Jim Grosbachecaef492012-08-14 19:06:05 +00001289 TableInfo.Table.push_back(MCD::OPC_CheckField);
1290 TableInfo.Table.push_back(StartBits[I-1]);
1291 TableInfo.Table.push_back(NumBits);
1292 uint8_t Buffer[8], *p;
1293 encodeULEB128(FieldVals[I-1], Buffer);
1294 for (p = Buffer; *p >= 128 ; ++p)
1295 TableInfo.Table.push_back(*p);
1296 TableInfo.Table.push_back(*p);
1297 // Push location for NumToSkip backpatching.
1298 TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1299 // The fixup is always 16-bits, so go ahead and allocate the space
1300 // in the table so all our relative position calculations work OK even
1301 // before we fully resolve the real value here.
1302 TableInfo.Table.push_back(0);
1303 TableInfo.Table.push_back(0);
Owen Anderson4e818902011-02-18 21:51:29 +00001304 }
Owen Anderson4e818902011-02-18 21:51:29 +00001305
Jim Grosbachecaef492012-08-14 19:06:05 +00001306 // Check for soft failure of the match.
1307 emitSoftFailTableEntry(TableInfo, Opc);
Owen Anderson4e818902011-02-18 21:51:29 +00001308
Jim Grosbachecaef492012-08-14 19:06:05 +00001309 TableInfo.Table.push_back(MCD::OPC_Decode);
1310 uint8_t Buffer[8], *p;
1311 encodeULEB128(Opc, Buffer);
1312 for (p = Buffer; *p >= 128 ; ++p)
1313 TableInfo.Table.push_back(*p);
1314 TableInfo.Table.push_back(*p);
1315
1316 unsigned DIdx = getDecoderIndex(TableInfo.Decoders, Opc);
1317 SmallString<16> Bytes;
1318 raw_svector_ostream S(Bytes);
1319 encodeULEB128(DIdx, S);
1320 S.flush();
1321
1322 // Decoder index
Craig Topper29688ab2012-08-17 05:42:16 +00001323 for (unsigned i = 0, e = Bytes.size(); i != e; ++i)
Jim Grosbachecaef492012-08-14 19:06:05 +00001324 TableInfo.Table.push_back(Bytes[i]);
Owen Anderson4e818902011-02-18 21:51:29 +00001325}
1326
Jim Grosbachecaef492012-08-14 19:06:05 +00001327// Emits table entries to decode the singleton, and then to decode the rest.
1328void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1329 const Filter &Best) const {
Owen Anderson4e818902011-02-18 21:51:29 +00001330 unsigned Opc = Best.getSingletonOpc();
1331
Jim Grosbachecaef492012-08-14 19:06:05 +00001332 // complex singletons need predicate checks from the first singleton
1333 // to refer forward to the variable filterchooser that follows.
1334 TableInfo.FixupStack.push_back(FixupList());
Owen Anderson4e818902011-02-18 21:51:29 +00001335
Jim Grosbachecaef492012-08-14 19:06:05 +00001336 emitSingletonTableEntry(TableInfo, Opc);
Owen Anderson4e818902011-02-18 21:51:29 +00001337
Jim Grosbachecaef492012-08-14 19:06:05 +00001338 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
1339 TableInfo.Table.size());
1340 TableInfo.FixupStack.pop_back();
1341
1342 Best.getVariableFC().emitTableEntries(TableInfo);
Owen Anderson4e818902011-02-18 21:51:29 +00001343}
1344
Jim Grosbachecaef492012-08-14 19:06:05 +00001345
Owen Anderson4e818902011-02-18 21:51:29 +00001346// Assign a single filter and run with it. Top level API client can initialize
1347// with a single filter to start the filtering process.
Craig Topper48c112b2012-03-16 05:58:09 +00001348void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit,
1349 bool mixed) {
Owen Anderson4e818902011-02-18 21:51:29 +00001350 Filters.clear();
Craig Topper5c2b4ac2014-09-03 05:49:07 +00001351 Filters.push_back(Filter(*this, startBit, numBit, true));
Owen Anderson4e818902011-02-18 21:51:29 +00001352 BestIndex = 0; // Sole Filter instance to choose from.
1353 bestFilter().recurse();
1354}
1355
1356// reportRegion is a helper function for filterProcessor to mark a region as
1357// eligible for use as a filter region.
1358void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
Craig Topper82d0d5f2012-03-16 01:19:24 +00001359 unsigned BitIndex, bool AllowMixed) {
Owen Anderson4e818902011-02-18 21:51:29 +00001360 if (RA == ATTR_MIXED && AllowMixed)
1361 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true));
1362 else if (RA == ATTR_ALL_SET && !AllowMixed)
1363 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false));
1364}
1365
1366// FilterProcessor scans the well-known encoding bits of the instructions and
1367// builds up a list of candidate filters. It chooses the best filter and
1368// recursively descends down the decoding tree.
1369bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1370 Filters.clear();
1371 BestIndex = -1;
1372 unsigned numInstructions = Opcodes.size();
1373
1374 assert(numInstructions && "Filter created with no instructions");
1375
1376 // No further filtering is necessary.
1377 if (numInstructions == 1)
1378 return true;
1379
1380 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1381 // instructions is 3.
1382 if (AllowMixed && !Greedy) {
1383 assert(numInstructions == 3);
1384
1385 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1386 std::vector<unsigned> StartBits;
1387 std::vector<unsigned> EndBits;
1388 std::vector<uint64_t> FieldVals;
1389 insn_t Insn;
1390
1391 insnWithID(Insn, Opcodes[i]);
1392
1393 // Look for islands of undecoded bits of any instruction.
1394 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1395 // Found an instruction with island(s). Now just assign a filter.
Craig Topper48c112b2012-03-16 05:58:09 +00001396 runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true);
Owen Anderson4e818902011-02-18 21:51:29 +00001397 return true;
1398 }
1399 }
1400 }
1401
Craig Topper29688ab2012-08-17 05:42:16 +00001402 unsigned BitIndex;
Owen Anderson4e818902011-02-18 21:51:29 +00001403
1404 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1405 // The automaton consumes the corresponding bit from each
1406 // instruction.
1407 //
1408 // Input symbols: 0, 1, and _ (unset).
1409 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1410 // Initial state: NONE.
1411 //
1412 // (NONE) ------- [01] -> (ALL_SET)
1413 // (NONE) ------- _ ----> (ALL_UNSET)
1414 // (ALL_SET) ---- [01] -> (ALL_SET)
1415 // (ALL_SET) ---- _ ----> (MIXED)
1416 // (ALL_UNSET) -- [01] -> (MIXED)
1417 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1418 // (MIXED) ------ . ----> (MIXED)
1419 // (FILTERED)---- . ----> (FILTERED)
1420
Owen Andersonc78e03c2011-07-19 21:06:00 +00001421 std::vector<bitAttr_t> bitAttrs;
Owen Anderson4e818902011-02-18 21:51:29 +00001422
1423 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1424 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
Owen Andersonc78e03c2011-07-19 21:06:00 +00001425 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
Owen Anderson4e818902011-02-18 21:51:29 +00001426 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1427 FilterBitValues[BitIndex] == BIT_FALSE)
Owen Andersonc78e03c2011-07-19 21:06:00 +00001428 bitAttrs.push_back(ATTR_FILTERED);
Owen Anderson4e818902011-02-18 21:51:29 +00001429 else
Owen Andersonc78e03c2011-07-19 21:06:00 +00001430 bitAttrs.push_back(ATTR_NONE);
Owen Anderson4e818902011-02-18 21:51:29 +00001431
Craig Topper29688ab2012-08-17 05:42:16 +00001432 for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
Owen Anderson4e818902011-02-18 21:51:29 +00001433 insn_t insn;
1434
1435 insnWithID(insn, Opcodes[InsnIndex]);
1436
Owen Andersonc78e03c2011-07-19 21:06:00 +00001437 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
Owen Anderson4e818902011-02-18 21:51:29 +00001438 switch (bitAttrs[BitIndex]) {
1439 case ATTR_NONE:
1440 if (insn[BitIndex] == BIT_UNSET)
1441 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1442 else
1443 bitAttrs[BitIndex] = ATTR_ALL_SET;
1444 break;
1445 case ATTR_ALL_SET:
1446 if (insn[BitIndex] == BIT_UNSET)
1447 bitAttrs[BitIndex] = ATTR_MIXED;
1448 break;
1449 case ATTR_ALL_UNSET:
1450 if (insn[BitIndex] != BIT_UNSET)
1451 bitAttrs[BitIndex] = ATTR_MIXED;
1452 break;
1453 case ATTR_MIXED:
1454 case ATTR_FILTERED:
1455 break;
1456 }
1457 }
1458 }
1459
1460 // The regionAttr automaton consumes the bitAttrs automatons' state,
1461 // lowest-to-highest.
1462 //
1463 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1464 // States: NONE, ALL_SET, MIXED
1465 // Initial state: NONE
1466 //
1467 // (NONE) ----- F --> (NONE)
1468 // (NONE) ----- S --> (ALL_SET) ; and set region start
1469 // (NONE) ----- U --> (NONE)
1470 // (NONE) ----- M --> (MIXED) ; and set region start
1471 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1472 // (ALL_SET) -- S --> (ALL_SET)
1473 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1474 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1475 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1476 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1477 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1478 // (MIXED) ---- M --> (MIXED)
1479
1480 bitAttr_t RA = ATTR_NONE;
1481 unsigned StartBit = 0;
1482
Craig Topper29688ab2012-08-17 05:42:16 +00001483 for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
Owen Anderson4e818902011-02-18 21:51:29 +00001484 bitAttr_t bitAttr = bitAttrs[BitIndex];
1485
1486 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1487
1488 switch (RA) {
1489 case ATTR_NONE:
1490 switch (bitAttr) {
1491 case ATTR_FILTERED:
1492 break;
1493 case ATTR_ALL_SET:
1494 StartBit = BitIndex;
1495 RA = ATTR_ALL_SET;
1496 break;
1497 case ATTR_ALL_UNSET:
1498 break;
1499 case ATTR_MIXED:
1500 StartBit = BitIndex;
1501 RA = ATTR_MIXED;
1502 break;
1503 default:
Craig Topperc4965bc2012-02-05 07:21:30 +00001504 llvm_unreachable("Unexpected bitAttr!");
Owen Anderson4e818902011-02-18 21:51:29 +00001505 }
1506 break;
1507 case ATTR_ALL_SET:
1508 switch (bitAttr) {
1509 case ATTR_FILTERED:
1510 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1511 RA = ATTR_NONE;
1512 break;
1513 case ATTR_ALL_SET:
1514 break;
1515 case ATTR_ALL_UNSET:
1516 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1517 RA = ATTR_NONE;
1518 break;
1519 case ATTR_MIXED:
1520 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1521 StartBit = BitIndex;
1522 RA = ATTR_MIXED;
1523 break;
1524 default:
Craig Topperc4965bc2012-02-05 07:21:30 +00001525 llvm_unreachable("Unexpected bitAttr!");
Owen Anderson4e818902011-02-18 21:51:29 +00001526 }
1527 break;
1528 case ATTR_MIXED:
1529 switch (bitAttr) {
1530 case ATTR_FILTERED:
1531 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1532 StartBit = BitIndex;
1533 RA = ATTR_NONE;
1534 break;
1535 case ATTR_ALL_SET:
1536 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1537 StartBit = BitIndex;
1538 RA = ATTR_ALL_SET;
1539 break;
1540 case ATTR_ALL_UNSET:
1541 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1542 RA = ATTR_NONE;
1543 break;
1544 case ATTR_MIXED:
1545 break;
1546 default:
Craig Topperc4965bc2012-02-05 07:21:30 +00001547 llvm_unreachable("Unexpected bitAttr!");
Owen Anderson4e818902011-02-18 21:51:29 +00001548 }
1549 break;
1550 case ATTR_ALL_UNSET:
Craig Topperc4965bc2012-02-05 07:21:30 +00001551 llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
Owen Anderson4e818902011-02-18 21:51:29 +00001552 case ATTR_FILTERED:
Craig Topperc4965bc2012-02-05 07:21:30 +00001553 llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
Owen Anderson4e818902011-02-18 21:51:29 +00001554 }
1555 }
1556
1557 // At the end, if we're still in ALL_SET or MIXED states, report a region
1558 switch (RA) {
1559 case ATTR_NONE:
1560 break;
1561 case ATTR_FILTERED:
1562 break;
1563 case ATTR_ALL_SET:
1564 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1565 break;
1566 case ATTR_ALL_UNSET:
1567 break;
1568 case ATTR_MIXED:
1569 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1570 break;
1571 }
1572
1573 // We have finished with the filter processings. Now it's time to choose
1574 // the best performing filter.
1575 BestIndex = 0;
1576 bool AllUseless = true;
1577 unsigned BestScore = 0;
1578
1579 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1580 unsigned Usefulness = Filters[i].usefulness();
1581
1582 if (Usefulness)
1583 AllUseless = false;
1584
1585 if (Usefulness > BestScore) {
1586 BestIndex = i;
1587 BestScore = Usefulness;
1588 }
1589 }
1590
1591 if (!AllUseless)
1592 bestFilter().recurse();
1593
1594 return !AllUseless;
1595} // end of FilterChooser::filterProcessor(bool)
1596
1597// Decides on the best configuration of filter(s) to use in order to decode
1598// the instructions. A conflict of instructions may occur, in which case we
1599// dump the conflict set to the standard error.
1600void FilterChooser::doFilter() {
1601 unsigned Num = Opcodes.size();
1602 assert(Num && "FilterChooser created with no instructions");
1603
1604 // Try regions of consecutive known bit values first.
1605 if (filterProcessor(false))
1606 return;
1607
1608 // Then regions of mixed bits (both known and unitialized bit values allowed).
1609 if (filterProcessor(true))
1610 return;
1611
1612 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1613 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1614 // well-known encoding pattern. In such case, we backtrack and scan for the
1615 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1616 if (Num == 3 && filterProcessor(true, false))
1617 return;
1618
1619 // If we come to here, the instruction decoding has failed.
1620 // Set the BestIndex to -1 to indicate so.
1621 BestIndex = -1;
1622}
1623
Jim Grosbachecaef492012-08-14 19:06:05 +00001624// emitTableEntries - Emit state machine entries to decode our share of
1625// instructions.
1626void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
1627 if (Opcodes.size() == 1) {
Owen Anderson4e818902011-02-18 21:51:29 +00001628 // There is only one instruction in the set, which is great!
1629 // Call emitSingletonDecoder() to see whether there are any remaining
1630 // encodings bits.
Jim Grosbachecaef492012-08-14 19:06:05 +00001631 emitSingletonTableEntry(TableInfo, Opcodes[0]);
1632 return;
1633 }
Owen Anderson4e818902011-02-18 21:51:29 +00001634
1635 // Choose the best filter to do the decodings!
1636 if (BestIndex != -1) {
Craig Topper48c112b2012-03-16 05:58:09 +00001637 const Filter &Best = Filters[BestIndex];
Owen Anderson4e818902011-02-18 21:51:29 +00001638 if (Best.getNumFiltered() == 1)
Jim Grosbachecaef492012-08-14 19:06:05 +00001639 emitSingletonTableEntry(TableInfo, Best);
Owen Anderson4e818902011-02-18 21:51:29 +00001640 else
Jim Grosbachecaef492012-08-14 19:06:05 +00001641 Best.emitTableEntry(TableInfo);
1642 return;
Owen Anderson4e818902011-02-18 21:51:29 +00001643 }
1644
Jim Grosbachecaef492012-08-14 19:06:05 +00001645 // We don't know how to decode these instructions! Dump the
1646 // conflict set and bail.
Owen Anderson4e818902011-02-18 21:51:29 +00001647
1648 // Print out useful conflict information for postmortem analysis.
1649 errs() << "Decoding Conflict:\n";
1650
1651 dumpStack(errs(), "\t\t");
1652
Craig Topper82d0d5f2012-03-16 01:19:24 +00001653 for (unsigned i = 0; i < Opcodes.size(); ++i) {
Owen Anderson4e818902011-02-18 21:51:29 +00001654 const std::string &Name = nameWithID(Opcodes[i]);
1655
1656 errs() << '\t' << Name << " ";
1657 dumpBits(errs(),
1658 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1659 errs() << '\n';
1660 }
Owen Anderson4e818902011-02-18 21:51:29 +00001661}
1662
Hal Finkel71b2e202013-12-19 16:12:53 +00001663static bool populateInstruction(CodeGenTarget &Target,
1664 const CodeGenInstruction &CGI, unsigned Opc,
Craig Topper82d0d5f2012-03-16 01:19:24 +00001665 std::map<unsigned, std::vector<OperandInfo> > &Operands){
Owen Anderson4e818902011-02-18 21:51:29 +00001666 const Record &Def = *CGI.TheDef;
1667 // If all the bit positions are not specified; do not decode this instruction.
1668 // We are bound to fail! For proper disassembly, the well-known encoding bits
1669 // of the instruction must be fully specified.
Owen Anderson4e818902011-02-18 21:51:29 +00001670
David Greeneaf8ee2c2011-07-29 22:43:06 +00001671 BitsInit &Bits = getBitsField(Def, "Inst");
Jim Grosbachf3fd36e2011-07-06 21:33:38 +00001672 if (Bits.allInComplete()) return false;
1673
Owen Anderson4e818902011-02-18 21:51:29 +00001674 std::vector<OperandInfo> InsnOperands;
1675
1676 // If the instruction has specified a custom decoding hook, use that instead
1677 // of trying to auto-generate the decoder.
1678 std::string InstDecoder = Def.getValueAsString("DecoderMethod");
1679 if (InstDecoder != "") {
Owen Andersone3591652011-07-28 21:54:31 +00001680 InsnOperands.push_back(OperandInfo(InstDecoder));
Owen Anderson4e818902011-02-18 21:51:29 +00001681 Operands[Opc] = InsnOperands;
1682 return true;
1683 }
1684
1685 // Generate a description of the operand of the instruction that we know
1686 // how to decode automatically.
1687 // FIXME: We'll need to have a way to manually override this as needed.
1688
1689 // Gather the outputs/inputs of the instruction, so we can find their
1690 // positions in the encoding. This assumes for now that they appear in the
1691 // MCInst in the order that they're listed.
David Greeneaf8ee2c2011-07-29 22:43:06 +00001692 std::vector<std::pair<Init*, std::string> > InOutOperands;
1693 DagInit *Out = Def.getValueAsDag("OutOperandList");
1694 DagInit *In = Def.getValueAsDag("InOperandList");
Owen Anderson4e818902011-02-18 21:51:29 +00001695 for (unsigned i = 0; i < Out->getNumArgs(); ++i)
1696 InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i)));
1697 for (unsigned i = 0; i < In->getNumArgs(); ++i)
1698 InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i)));
1699
Owen Anderson53562d02011-07-28 23:56:20 +00001700 // Search for tied operands, so that we can correctly instantiate
1701 // operands that are not explicitly represented in the encoding.
Owen Andersoncb32ce22011-07-29 18:28:52 +00001702 std::map<std::string, std::string> TiedNames;
Owen Anderson53562d02011-07-28 23:56:20 +00001703 for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
1704 int tiedTo = CGI.Operands[i].getTiedRegister();
Owen Andersoncb32ce22011-07-29 18:28:52 +00001705 if (tiedTo != -1) {
Hal Finkel71b2e202013-12-19 16:12:53 +00001706 std::pair<unsigned, unsigned> SO =
1707 CGI.Operands.getSubOperandNumber(tiedTo);
1708 TiedNames[InOutOperands[i].second] = InOutOperands[SO.first].second;
1709 TiedNames[InOutOperands[SO.first].second] = InOutOperands[i].second;
1710 }
1711 }
1712
1713 std::map<std::string, std::vector<OperandInfo> > NumberedInsnOperands;
1714 std::set<std::string> NumberedInsnOperandsNoTie;
1715 if (Target.getInstructionSet()->
1716 getValueAsBit("decodePositionallyEncodedOperands")) {
1717 const std::vector<RecordVal> &Vals = Def.getValues();
1718 unsigned NumberedOp = 0;
1719
Hal Finkel5457bd02014-03-13 07:57:54 +00001720 std::set<unsigned> NamedOpIndices;
1721 if (Target.getInstructionSet()->
1722 getValueAsBit("noNamedPositionallyEncodedOperands"))
1723 // Collect the set of operand indices that might correspond to named
1724 // operand, and skip these when assigning operands based on position.
1725 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1726 unsigned OpIdx;
1727 if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
1728 continue;
1729
1730 NamedOpIndices.insert(OpIdx);
1731 }
1732
Hal Finkel71b2e202013-12-19 16:12:53 +00001733 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1734 // Ignore fixed fields in the record, we're looking for values like:
1735 // bits<5> RST = { ?, ?, ?, ?, ? };
1736 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
1737 continue;
1738
1739 // Determine if Vals[i] actually contributes to the Inst encoding.
1740 unsigned bi = 0;
1741 for (; bi < Bits.getNumBits(); ++bi) {
Craig Topper24064772014-04-15 07:20:03 +00001742 VarInit *Var = nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001743 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1744 if (BI)
1745 Var = dyn_cast<VarInit>(BI->getBitVar());
1746 else
1747 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1748
1749 if (Var && Var->getName() == Vals[i].getName())
1750 break;
1751 }
1752
1753 if (bi == Bits.getNumBits())
1754 continue;
1755
1756 // Skip variables that correspond to explicitly-named operands.
1757 unsigned OpIdx;
1758 if (CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
1759 continue;
1760
1761 // Get the bit range for this operand:
1762 unsigned bitStart = bi++, bitWidth = 1;
1763 for (; bi < Bits.getNumBits(); ++bi) {
Craig Topper24064772014-04-15 07:20:03 +00001764 VarInit *Var = nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001765 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
1766 if (BI)
1767 Var = dyn_cast<VarInit>(BI->getBitVar());
1768 else
1769 Var = dyn_cast<VarInit>(Bits.getBit(bi));
1770
1771 if (!Var)
1772 break;
1773
1774 if (Var->getName() != Vals[i].getName())
1775 break;
1776
1777 ++bitWidth;
1778 }
1779
1780 unsigned NumberOps = CGI.Operands.size();
1781 while (NumberedOp < NumberOps &&
Hal Finkel5457bd02014-03-13 07:57:54 +00001782 (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
1783 (NamedOpIndices.size() && NamedOpIndices.count(
1784 CGI.Operands.getSubOperandNumber(NumberedOp).first))))
Hal Finkel71b2e202013-12-19 16:12:53 +00001785 ++NumberedOp;
1786
1787 OpIdx = NumberedOp++;
1788
1789 // OpIdx now holds the ordered operand number of Vals[i].
1790 std::pair<unsigned, unsigned> SO =
1791 CGI.Operands.getSubOperandNumber(OpIdx);
1792 const std::string &Name = CGI.Operands[SO.first].Name;
1793
1794 DEBUG(dbgs() << "Numbered operand mapping for " << Def.getName() << ": " <<
1795 Name << "(" << SO.first << ", " << SO.second << ") => " <<
1796 Vals[i].getName() << "\n");
1797
1798 std::string Decoder = "";
1799 Record *TypeRecord = CGI.Operands[SO.first].Rec;
1800
1801 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
1802 StringInit *String = DecoderString ?
Craig Topper24064772014-04-15 07:20:03 +00001803 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001804 if (String && String->getValue() != "")
1805 Decoder = String->getValue();
1806
1807 if (Decoder == "" &&
1808 CGI.Operands[SO.first].MIOperandInfo &&
1809 CGI.Operands[SO.first].MIOperandInfo->getNumArgs()) {
1810 Init *Arg = CGI.Operands[SO.first].MIOperandInfo->
1811 getArg(SO.second);
1812 if (TypedInit *TI = cast<TypedInit>(Arg)) {
1813 RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
1814 TypeRecord = Type->getRecord();
1815 }
1816 }
1817
1818 bool isReg = false;
1819 if (TypeRecord->isSubClassOf("RegisterOperand"))
1820 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1821 if (TypeRecord->isSubClassOf("RegisterClass")) {
1822 Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
1823 isReg = true;
1824 } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
1825 Decoder = "DecodePointerLikeRegClass" +
1826 utostr(TypeRecord->getValueAsInt("RegClassKind"));
1827 isReg = true;
1828 }
1829
1830 DecoderString = TypeRecord->getValue("DecoderMethod");
1831 String = DecoderString ?
Craig Topper24064772014-04-15 07:20:03 +00001832 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
Hal Finkel71b2e202013-12-19 16:12:53 +00001833 if (!isReg && String && String->getValue() != "")
1834 Decoder = String->getValue();
1835
1836 OperandInfo OpInfo(Decoder);
1837 OpInfo.addField(bitStart, bitWidth, 0);
1838
1839 NumberedInsnOperands[Name].push_back(OpInfo);
1840
1841 // FIXME: For complex operands with custom decoders we can't handle tied
1842 // sub-operands automatically. Skip those here and assume that this is
1843 // fixed up elsewhere.
1844 if (CGI.Operands[SO.first].MIOperandInfo &&
1845 CGI.Operands[SO.first].MIOperandInfo->getNumArgs() > 1 &&
1846 String && String->getValue() != "")
1847 NumberedInsnOperandsNoTie.insert(Name);
Owen Andersoncb32ce22011-07-29 18:28:52 +00001848 }
Owen Anderson53562d02011-07-28 23:56:20 +00001849 }
1850
Owen Anderson4e818902011-02-18 21:51:29 +00001851 // For each operand, see if we can figure out where it is encoded.
Craig Topper1f7604d2014-12-13 05:12:19 +00001852 for (const auto &Op : InOutOperands) {
1853 if (!NumberedInsnOperands[Op.second].empty()) {
Hal Finkel71b2e202013-12-19 16:12:53 +00001854 InsnOperands.insert(InsnOperands.end(),
Craig Topper1f7604d2014-12-13 05:12:19 +00001855 NumberedInsnOperands[Op.second].begin(),
1856 NumberedInsnOperands[Op.second].end());
Hal Finkel71b2e202013-12-19 16:12:53 +00001857 continue;
Craig Topper1f7604d2014-12-13 05:12:19 +00001858 }
1859 if (!NumberedInsnOperands[TiedNames[Op.second]].empty()) {
1860 if (!NumberedInsnOperandsNoTie.count(TiedNames[Op.second])) {
Hal Finkel71b2e202013-12-19 16:12:53 +00001861 // Figure out to which (sub)operand we're tied.
Craig Topper1f7604d2014-12-13 05:12:19 +00001862 unsigned i = CGI.Operands.getOperandNamed(TiedNames[Op.second]);
Hal Finkel71b2e202013-12-19 16:12:53 +00001863 int tiedTo = CGI.Operands[i].getTiedRegister();
1864 if (tiedTo == -1) {
Craig Topper1f7604d2014-12-13 05:12:19 +00001865 i = CGI.Operands.getOperandNamed(Op.second);
Hal Finkel71b2e202013-12-19 16:12:53 +00001866 tiedTo = CGI.Operands[i].getTiedRegister();
1867 }
1868
1869 if (tiedTo != -1) {
1870 std::pair<unsigned, unsigned> SO =
1871 CGI.Operands.getSubOperandNumber(tiedTo);
1872
Craig Topper1f7604d2014-12-13 05:12:19 +00001873 InsnOperands.push_back(NumberedInsnOperands[TiedNames[Op.second]]
Hal Finkel71b2e202013-12-19 16:12:53 +00001874 [SO.second]);
1875 }
1876 }
1877 continue;
1878 }
1879
Owen Anderson4e818902011-02-18 21:51:29 +00001880 std::string Decoder = "";
1881
Owen Andersone3591652011-07-28 21:54:31 +00001882 // At this point, we can locate the field, but we need to know how to
1883 // interpret it. As a first step, require the target to provide callbacks
1884 // for decoding register classes.
1885 // FIXME: This need to be extended to handle instructions with custom
1886 // decoder methods, and operands with (simple) MIOperandInfo's.
Craig Topper1f7604d2014-12-13 05:12:19 +00001887 TypedInit *TI = cast<TypedInit>(Op.first);
Sean Silva88eb8dd2012-10-10 20:24:47 +00001888 RecordRecTy *Type = cast<RecordRecTy>(TI->getType());
Owen Andersone3591652011-07-28 21:54:31 +00001889 Record *TypeRecord = Type->getRecord();
1890 bool isReg = false;
1891 if (TypeRecord->isSubClassOf("RegisterOperand"))
1892 TypeRecord = TypeRecord->getValueAsDef("RegClass");
1893 if (TypeRecord->isSubClassOf("RegisterClass")) {
1894 Decoder = "Decode" + TypeRecord->getName() + "RegisterClass";
1895 isReg = true;
Hal Finkel9d95e8d2013-12-19 14:58:22 +00001896 } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
1897 Decoder = "DecodePointerLikeRegClass" +
1898 utostr(TypeRecord->getValueAsInt("RegClassKind"));
1899 isReg = true;
Owen Andersone3591652011-07-28 21:54:31 +00001900 }
1901
1902 RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
David Greeneaf8ee2c2011-07-29 22:43:06 +00001903 StringInit *String = DecoderString ?
Craig Topper24064772014-04-15 07:20:03 +00001904 dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
Owen Andersone3591652011-07-28 21:54:31 +00001905 if (!isReg && String && String->getValue() != "")
1906 Decoder = String->getValue();
1907
1908 OperandInfo OpInfo(Decoder);
1909 unsigned Base = ~0U;
1910 unsigned Width = 0;
1911 unsigned Offset = 0;
1912
Owen Anderson4e818902011-02-18 21:51:29 +00001913 for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
Craig Topper24064772014-04-15 07:20:03 +00001914 VarInit *Var = nullptr;
Sean Silvafb509ed2012-10-10 20:24:43 +00001915 VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
Owen Anderson3022d672011-08-01 22:45:43 +00001916 if (BI)
Sean Silvafb509ed2012-10-10 20:24:43 +00001917 Var = dyn_cast<VarInit>(BI->getBitVar());
Owen Anderson3022d672011-08-01 22:45:43 +00001918 else
Sean Silvafb509ed2012-10-10 20:24:43 +00001919 Var = dyn_cast<VarInit>(Bits.getBit(bi));
Owen Anderson3022d672011-08-01 22:45:43 +00001920
1921 if (!Var) {
Owen Andersone3591652011-07-28 21:54:31 +00001922 if (Base != ~0U) {
1923 OpInfo.addField(Base, Width, Offset);
1924 Base = ~0U;
1925 Width = 0;
1926 Offset = 0;
1927 }
1928 continue;
1929 }
Owen Anderson4e818902011-02-18 21:51:29 +00001930
Craig Topper1f7604d2014-12-13 05:12:19 +00001931 if (Var->getName() != Op.second &&
1932 Var->getName() != TiedNames[Op.second]) {
Owen Andersone3591652011-07-28 21:54:31 +00001933 if (Base != ~0U) {
1934 OpInfo.addField(Base, Width, Offset);
1935 Base = ~0U;
1936 Width = 0;
1937 Offset = 0;
1938 }
1939 continue;
Owen Anderson4e818902011-02-18 21:51:29 +00001940 }
1941
Owen Andersone3591652011-07-28 21:54:31 +00001942 if (Base == ~0U) {
1943 Base = bi;
1944 Width = 1;
Owen Anderson3022d672011-08-01 22:45:43 +00001945 Offset = BI ? BI->getBitNum() : 0;
1946 } else if (BI && BI->getBitNum() != Offset + Width) {
Owen Andersone08f5b52011-07-29 23:01:18 +00001947 OpInfo.addField(Base, Width, Offset);
1948 Base = bi;
1949 Width = 1;
1950 Offset = BI->getBitNum();
Owen Andersone3591652011-07-28 21:54:31 +00001951 } else {
1952 ++Width;
Owen Anderson4e818902011-02-18 21:51:29 +00001953 }
Owen Anderson4e818902011-02-18 21:51:29 +00001954 }
1955
Owen Andersone3591652011-07-28 21:54:31 +00001956 if (Base != ~0U)
1957 OpInfo.addField(Base, Width, Offset);
1958
1959 if (OpInfo.numFields() > 0)
1960 InsnOperands.push_back(OpInfo);
Owen Anderson4e818902011-02-18 21:51:29 +00001961 }
1962
1963 Operands[Opc] = InsnOperands;
1964
1965
1966#if 0
1967 DEBUG({
1968 // Dumps the instruction encoding bits.
1969 dumpBits(errs(), Bits);
1970
1971 errs() << '\n';
1972
1973 // Dumps the list of operand info.
1974 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1975 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
1976 const std::string &OperandName = Info.Name;
1977 const Record &OperandDef = *Info.Rec;
1978
1979 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
1980 }
1981 });
1982#endif
1983
1984 return true;
1985}
1986
Jim Grosbachecaef492012-08-14 19:06:05 +00001987// emitFieldFromInstruction - Emit the templated helper function
1988// fieldFromInstruction().
1989static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
1990 OS << "// Helper function for extracting fields from encoded instructions.\n"
1991 << "template<typename InsnType>\n"
1992 << "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n"
1993 << " unsigned numBits) {\n"
1994 << " assert(startBit + numBits <= (sizeof(InsnType)*8) &&\n"
1995 << " \"Instruction field out of bounds!\");\n"
1996 << " InsnType fieldMask;\n"
1997 << " if (numBits == sizeof(InsnType)*8)\n"
1998 << " fieldMask = (InsnType)(-1LL);\n"
1999 << " else\n"
NAKAMURA Takumibf99a422012-12-26 06:43:14 +00002000 << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002001 << " return (insn & fieldMask) >> startBit;\n"
2002 << "}\n\n";
2003}
Owen Anderson4e818902011-02-18 21:51:29 +00002004
Jim Grosbachecaef492012-08-14 19:06:05 +00002005// emitDecodeInstruction - Emit the templated helper function
2006// decodeInstruction().
2007static void emitDecodeInstruction(formatted_raw_ostream &OS) {
2008 OS << "template<typename InsnType>\n"
2009 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,\n"
2010 << " InsnType insn, uint64_t Address,\n"
2011 << " const void *DisAsm,\n"
2012 << " const MCSubtargetInfo &STI) {\n"
2013 << " uint64_t Bits = STI.getFeatureBits();\n"
2014 << "\n"
2015 << " const uint8_t *Ptr = DecodeTable;\n"
Jim Grosbach4c363492012-09-17 18:00:53 +00002016 << " uint32_t CurFieldValue = 0;\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002017 << " DecodeStatus S = MCDisassembler::Success;\n"
2018 << " for (;;) {\n"
2019 << " ptrdiff_t Loc = Ptr - DecodeTable;\n"
2020 << " switch (*Ptr) {\n"
2021 << " default:\n"
2022 << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
2023 << " return MCDisassembler::Fail;\n"
2024 << " case MCD::OPC_ExtractField: {\n"
2025 << " unsigned Start = *++Ptr;\n"
2026 << " unsigned Len = *++Ptr;\n"
2027 << " ++Ptr;\n"
2028 << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
2029 << " DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << \", \"\n"
2030 << " << Len << \"): \" << CurFieldValue << \"\\n\");\n"
2031 << " break;\n"
2032 << " }\n"
2033 << " case MCD::OPC_FilterValue: {\n"
2034 << " // Decode the field value.\n"
2035 << " unsigned Len;\n"
2036 << " InsnType Val = decodeULEB128(++Ptr, &Len);\n"
2037 << " Ptr += Len;\n"
2038 << " // NumToSkip is a plain 16-bit integer.\n"
2039 << " unsigned NumToSkip = *Ptr++;\n"
2040 << " NumToSkip |= (*Ptr++) << 8;\n"
2041 << "\n"
2042 << " // Perform the filter operation.\n"
2043 << " if (Val != CurFieldValue)\n"
2044 << " Ptr += NumToSkip;\n"
2045 << " DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << \", \" << NumToSkip\n"
2046 << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" : \"PASS:\")\n"
2047 << " << \" continuing at \" << (Ptr - DecodeTable) << \"\\n\");\n"
2048 << "\n"
2049 << " break;\n"
2050 << " }\n"
2051 << " case MCD::OPC_CheckField: {\n"
2052 << " unsigned Start = *++Ptr;\n"
2053 << " unsigned Len = *++Ptr;\n"
2054 << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n"
2055 << " // Decode the field value.\n"
2056 << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\n"
2057 << " Ptr += Len;\n"
2058 << " // NumToSkip is a plain 16-bit integer.\n"
2059 << " unsigned NumToSkip = *Ptr++;\n"
2060 << " NumToSkip |= (*Ptr++) << 8;\n"
2061 << "\n"
2062 << " // If the actual and expected values don't match, skip.\n"
2063 << " if (ExpectedValue != FieldValue)\n"
2064 << " Ptr += NumToSkip;\n"
2065 << " DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << \", \"\n"
2066 << " << Len << \", \" << ExpectedValue << \", \" << NumToSkip\n"
2067 << " << \"): FieldValue = \" << FieldValue << \", ExpectedValue = \"\n"
2068 << " << ExpectedValue << \": \"\n"
2069 << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2070 << " break;\n"
2071 << " }\n"
2072 << " case MCD::OPC_CheckPredicate: {\n"
2073 << " unsigned Len;\n"
2074 << " // Decode the Predicate Index value.\n"
2075 << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
2076 << " Ptr += Len;\n"
2077 << " // NumToSkip is a plain 16-bit integer.\n"
2078 << " unsigned NumToSkip = *Ptr++;\n"
2079 << " NumToSkip |= (*Ptr++) << 8;\n"
2080 << " // Check the predicate.\n"
2081 << " bool Pred;\n"
2082 << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
2083 << " Ptr += NumToSkip;\n"
2084 << " (void)Pred;\n"
2085 << " DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx << \"): \"\n"
2086 << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2087 << "\n"
2088 << " break;\n"
2089 << " }\n"
2090 << " case MCD::OPC_Decode: {\n"
2091 << " unsigned Len;\n"
2092 << " // Decode the Opcode value.\n"
2093 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2094 << " Ptr += Len;\n"
2095 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2096 << " Ptr += Len;\n"
2097 << " DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
2098 << " << \", using decoder \" << DecodeIdx << \"\\n\" );\n"
2099 << " DEBUG(dbgs() << \"----- DECODE SUCCESSFUL -----\\n\");\n"
2100 << "\n"
2101 << " MI.setOpcode(Opc);\n"
Benjamin Kramer26b568d2012-08-15 10:26:44 +00002102 << " return decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm);\n"
Jim Grosbachecaef492012-08-14 19:06:05 +00002103 << " }\n"
2104 << " case MCD::OPC_SoftFail: {\n"
2105 << " // Decode the mask values.\n"
2106 << " unsigned Len;\n"
2107 << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n"
2108 << " Ptr += Len;\n"
2109 << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n"
2110 << " Ptr += Len;\n"
2111 << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n"
2112 << " if (Fail)\n"
2113 << " S = MCDisassembler::SoftFail;\n"
2114 << " DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? \"FAIL\\n\":\"PASS\\n\"));\n"
2115 << " break;\n"
2116 << " }\n"
2117 << " case MCD::OPC_Fail: {\n"
2118 << " DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
2119 << " return MCDisassembler::Fail;\n"
2120 << " }\n"
2121 << " }\n"
2122 << " }\n"
2123 << " llvm_unreachable(\"bogosity detected in disassembler state machine!\");\n"
2124 << "}\n\n";
Owen Anderson4e818902011-02-18 21:51:29 +00002125}
2126
2127// Emits disassembler code for instruction decoding.
Craig Topper82d0d5f2012-03-16 01:19:24 +00002128void FixedLenDecoderEmitter::run(raw_ostream &o) {
Jim Grosbachecaef492012-08-14 19:06:05 +00002129 formatted_raw_ostream OS(o);
2130 OS << "#include \"llvm/MC/MCInst.h\"\n";
2131 OS << "#include \"llvm/Support/Debug.h\"\n";
2132 OS << "#include \"llvm/Support/DataTypes.h\"\n";
2133 OS << "#include \"llvm/Support/LEB128.h\"\n";
2134 OS << "#include \"llvm/Support/raw_ostream.h\"\n";
2135 OS << "#include <assert.h>\n";
2136 OS << '\n';
2137 OS << "namespace llvm {\n\n";
2138
2139 emitFieldFromInstruction(OS);
Owen Anderson4e818902011-02-18 21:51:29 +00002140
Hal Finkel81e6fcc2013-12-17 22:37:50 +00002141 Target.reverseBitsForLittleEndianEncoding();
2142
Owen Andersonc78e03c2011-07-19 21:06:00 +00002143 // Parameterize the decoders based on namespace and instruction width.
Jim Grosbachecaef492012-08-14 19:06:05 +00002144 NumberedInstructions = &Target.getInstructionsByEnumValue();
Owen Andersonc78e03c2011-07-19 21:06:00 +00002145 std::map<std::pair<std::string, unsigned>,
2146 std::vector<unsigned> > OpcMap;
2147 std::map<unsigned, std::vector<OperandInfo> > Operands;
2148
Jim Grosbachecaef492012-08-14 19:06:05 +00002149 for (unsigned i = 0; i < NumberedInstructions->size(); ++i) {
2150 const CodeGenInstruction *Inst = NumberedInstructions->at(i);
Craig Topper48c112b2012-03-16 05:58:09 +00002151 const Record *Def = Inst->TheDef;
Owen Andersonc78e03c2011-07-19 21:06:00 +00002152 unsigned Size = Def->getValueAsInt("Size");
2153 if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
2154 Def->getValueAsBit("isPseudo") ||
2155 Def->getValueAsBit("isAsmParserOnly") ||
2156 Def->getValueAsBit("isCodeGenOnly"))
2157 continue;
2158
2159 std::string DecoderNamespace = Def->getValueAsString("DecoderNamespace");
2160
2161 if (Size) {
Hal Finkel71b2e202013-12-19 16:12:53 +00002162 if (populateInstruction(Target, *Inst, i, Operands)) {
Owen Andersonc78e03c2011-07-19 21:06:00 +00002163 OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i);
2164 }
2165 }
2166 }
2167
Jim Grosbachecaef492012-08-14 19:06:05 +00002168 DecoderTableInfo TableInfo;
Craig Topper1f7604d2014-12-13 05:12:19 +00002169 for (const auto &Opc : OpcMap) {
Owen Andersonc78e03c2011-07-19 21:06:00 +00002170 // Emit the decoder for this namespace+width combination.
Craig Topper1f7604d2014-12-13 05:12:19 +00002171 FilterChooser FC(*NumberedInstructions, Opc.second, Operands,
2172 8*Opc.first.second, this);
Jim Grosbachecaef492012-08-14 19:06:05 +00002173
2174 // The decode table is cleared for each top level decoder function. The
2175 // predicates and decoders themselves, however, are shared across all
2176 // decoders to give more opportunities for uniqueing.
2177 TableInfo.Table.clear();
2178 TableInfo.FixupStack.clear();
2179 TableInfo.Table.reserve(16384);
2180 TableInfo.FixupStack.push_back(FixupList());
2181 FC.emitTableEntries(TableInfo);
2182 // Any NumToSkip fixups in the top level scope can resolve to the
2183 // OPC_Fail at the end of the table.
2184 assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
2185 // Resolve any NumToSkip fixups in the current scope.
2186 resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
2187 TableInfo.Table.size());
2188 TableInfo.FixupStack.clear();
2189
2190 TableInfo.Table.push_back(MCD::OPC_Fail);
2191
2192 // Print the table to the output stream.
Craig Topper1f7604d2014-12-13 05:12:19 +00002193 emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), Opc.first.first);
Jim Grosbachecaef492012-08-14 19:06:05 +00002194 OS.flush();
Owen Andersonc78e03c2011-07-19 21:06:00 +00002195 }
Owen Anderson4e818902011-02-18 21:51:29 +00002196
Jim Grosbachecaef492012-08-14 19:06:05 +00002197 // Emit the predicate function.
2198 emitPredicateFunction(OS, TableInfo.Predicates, 0);
2199
2200 // Emit the decoder function.
2201 emitDecoderFunction(OS, TableInfo.Decoders, 0);
2202
2203 // Emit the main entry point for the decoder, decodeInstruction().
2204 emitDecodeInstruction(OS);
2205
2206 OS << "\n} // End llvm namespace\n";
Owen Anderson4e818902011-02-18 21:51:29 +00002207}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00002208
2209namespace llvm {
2210
2211void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
2212 std::string PredicateNamespace,
2213 std::string GPrefix,
2214 std::string GPostfix,
2215 std::string ROK,
2216 std::string RFail,
2217 std::string L) {
2218 FixedLenDecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix,
2219 ROK, RFail, L).run(OS);
2220}
2221
2222} // End llvm namespace