zpu: first sign of being able to build a .S file
[llvm/zpu.git] / utils / TableGen / ARMDecoderEmitter.cpp
blob533fca0db0ae9646c5b3617ff1b9fd8a4714e4b6
1 //===------------ ARMDecoderEmitter.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 // This file is part of the ARM Disassembler.
11 // It contains the tablegen backend that emits the decoder functions for ARM and
12 // Thumb. The disassembler core includes the auto-generated file, invokes the
13 // decoder functions, and builds up the MCInst based on the decoded Opcode.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "arm-decoder-emitter"
19 #include "ARMDecoderEmitter.h"
20 #include "CodeGenTarget.h"
21 #include "Record.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
26 #include <vector>
27 #include <map>
28 #include <string>
30 using namespace llvm;
32 /////////////////////////////////////////////////////
33 // //
34 // Enums and Utilities for ARM Instruction Format //
35 // //
36 /////////////////////////////////////////////////////
38 #define ARM_FORMATS \
39 ENTRY(ARM_FORMAT_PSEUDO, 0) \
40 ENTRY(ARM_FORMAT_MULFRM, 1) \
41 ENTRY(ARM_FORMAT_BRFRM, 2) \
42 ENTRY(ARM_FORMAT_BRMISCFRM, 3) \
43 ENTRY(ARM_FORMAT_DPFRM, 4) \
44 ENTRY(ARM_FORMAT_DPSOREGFRM, 5) \
45 ENTRY(ARM_FORMAT_LDFRM, 6) \
46 ENTRY(ARM_FORMAT_STFRM, 7) \
47 ENTRY(ARM_FORMAT_LDMISCFRM, 8) \
48 ENTRY(ARM_FORMAT_STMISCFRM, 9) \
49 ENTRY(ARM_FORMAT_LDSTMULFRM, 10) \
50 ENTRY(ARM_FORMAT_LDSTEXFRM, 11) \
51 ENTRY(ARM_FORMAT_ARITHMISCFRM, 12) \
52 ENTRY(ARM_FORMAT_SATFRM, 13) \
53 ENTRY(ARM_FORMAT_EXTFRM, 14) \
54 ENTRY(ARM_FORMAT_VFPUNARYFRM, 15) \
55 ENTRY(ARM_FORMAT_VFPBINARYFRM, 16) \
56 ENTRY(ARM_FORMAT_VFPCONV1FRM, 17) \
57 ENTRY(ARM_FORMAT_VFPCONV2FRM, 18) \
58 ENTRY(ARM_FORMAT_VFPCONV3FRM, 19) \
59 ENTRY(ARM_FORMAT_VFPCONV4FRM, 20) \
60 ENTRY(ARM_FORMAT_VFPCONV5FRM, 21) \
61 ENTRY(ARM_FORMAT_VFPLDSTFRM, 22) \
62 ENTRY(ARM_FORMAT_VFPLDSTMULFRM, 23) \
63 ENTRY(ARM_FORMAT_VFPMISCFRM, 24) \
64 ENTRY(ARM_FORMAT_THUMBFRM, 25) \
65 ENTRY(ARM_FORMAT_MISCFRM, 26) \
66 ENTRY(ARM_FORMAT_NEONGETLNFRM, 27) \
67 ENTRY(ARM_FORMAT_NEONSETLNFRM, 28) \
68 ENTRY(ARM_FORMAT_NEONDUPFRM, 29) \
69 ENTRY(ARM_FORMAT_NLdSt, 30) \
70 ENTRY(ARM_FORMAT_N1RegModImm, 31) \
71 ENTRY(ARM_FORMAT_N2Reg, 32) \
72 ENTRY(ARM_FORMAT_NVCVT, 33) \
73 ENTRY(ARM_FORMAT_NVecDupLn, 34) \
74 ENTRY(ARM_FORMAT_N2RegVecShL, 35) \
75 ENTRY(ARM_FORMAT_N2RegVecShR, 36) \
76 ENTRY(ARM_FORMAT_N3Reg, 37) \
77 ENTRY(ARM_FORMAT_N3RegVecSh, 38) \
78 ENTRY(ARM_FORMAT_NVecExtract, 39) \
79 ENTRY(ARM_FORMAT_NVecMulScalar, 40) \
80 ENTRY(ARM_FORMAT_NVTBL, 41)
82 // ARM instruction format specifies the encoding used by the instruction.
83 #define ENTRY(n, v) n = v,
84 typedef enum {
85 ARM_FORMATS
86 ARM_FORMAT_NA
87 } ARMFormat;
88 #undef ENTRY
90 // Converts enum to const char*.
91 static const char *stringForARMFormat(ARMFormat form) {
92 #define ENTRY(n, v) case n: return #n;
93 switch(form) {
94 ARM_FORMATS
95 case ARM_FORMAT_NA:
96 default:
97 return "";
99 #undef ENTRY
102 enum {
103 IndexModeNone = 0,
104 IndexModePre = 1,
105 IndexModePost = 2,
106 IndexModeUpd = 3
109 /////////////////////////
110 // //
111 // Utility functions //
112 // //
113 /////////////////////////
115 /// byteFromBitsInit - Return the byte value from a BitsInit.
116 /// Called from getByteField().
117 static uint8_t byteFromBitsInit(BitsInit &init) {
118 int width = init.getNumBits();
120 assert(width <= 8 && "Field is too large for uint8_t!");
122 int index;
123 uint8_t mask = 0x01;
125 uint8_t ret = 0;
127 for (index = 0; index < width; index++) {
128 if (static_cast<BitInit*>(init.getBit(index))->getValue())
129 ret |= mask;
131 mask <<= 1;
134 return ret;
137 static uint8_t getByteField(const Record &def, const char *str) {
138 BitsInit *bits = def.getValueAsBitsInit(str);
139 return byteFromBitsInit(*bits);
142 static BitsInit &getBitsField(const Record &def, const char *str) {
143 BitsInit *bits = def.getValueAsBitsInit(str);
144 return *bits;
147 /// sameStringExceptSuffix - Return true if the two strings differ only in RHS's
148 /// suffix. ("VST4d8", "VST4d8_UPD", "_UPD") as input returns true.
149 static
150 bool sameStringExceptSuffix(const StringRef LHS, const StringRef RHS,
151 const StringRef Suffix) {
153 if (RHS.startswith(LHS) && RHS.endswith(Suffix))
154 return RHS.size() == LHS.size() + Suffix.size();
156 return false;
159 /// thumbInstruction - Determine whether we have a Thumb instruction.
160 /// See also ARMInstrFormats.td.
161 static bool thumbInstruction(uint8_t Form) {
162 return Form == ARM_FORMAT_THUMBFRM;
165 // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
166 // for a bit value.
168 // BIT_UNFILTERED is used as the init value for a filter position. It is used
169 // only for filter processings.
170 typedef enum {
171 BIT_TRUE, // '1'
172 BIT_FALSE, // '0'
173 BIT_UNSET, // '?'
174 BIT_UNFILTERED // unfiltered
175 } bit_value_t;
177 static bool ValueSet(bit_value_t V) {
178 return (V == BIT_TRUE || V == BIT_FALSE);
180 static bool ValueNotSet(bit_value_t V) {
181 return (V == BIT_UNSET);
183 static int Value(bit_value_t V) {
184 return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
186 static bit_value_t bitFromBits(BitsInit &bits, unsigned index) {
187 if (BitInit *bit = dynamic_cast<BitInit*>(bits.getBit(index)))
188 return bit->getValue() ? BIT_TRUE : BIT_FALSE;
190 // The bit is uninitialized.
191 return BIT_UNSET;
193 // Prints the bit value for each position.
194 static void dumpBits(raw_ostream &o, BitsInit &bits) {
195 unsigned index;
197 for (index = bits.getNumBits(); index > 0; index--) {
198 switch (bitFromBits(bits, index - 1)) {
199 case BIT_TRUE:
200 o << "1";
201 break;
202 case BIT_FALSE:
203 o << "0";
204 break;
205 case BIT_UNSET:
206 o << "_";
207 break;
208 default:
209 assert(0 && "unexpected return value from bitFromBits");
214 // Enums for the available target names.
215 typedef enum {
216 TARGET_ARM = 0,
217 TARGET_THUMB
218 } TARGET_NAME_t;
220 // FIXME: Possibly auto-detected?
221 #define BIT_WIDTH 32
223 // Forward declaration.
224 class FilterChooser;
226 // Representation of the instruction to work on.
227 typedef bit_value_t insn_t[BIT_WIDTH];
229 /// Filter - Filter works with FilterChooser to produce the decoding tree for
230 /// the ISA.
232 /// It is useful to think of a Filter as governing the switch stmts of the
233 /// decoding tree in a certain level. Each case stmt delegates to an inferior
234 /// FilterChooser to decide what further decoding logic to employ, or in another
235 /// words, what other remaining bits to look at. The FilterChooser eventually
236 /// chooses a best Filter to do its job.
238 /// This recursive scheme ends when the number of Opcodes assigned to the
239 /// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
240 /// the Filter/FilterChooser combo does not know how to distinguish among the
241 /// Opcodes assigned.
243 /// An example of a conflict is
245 /// Conflict:
246 /// 111101000.00........00010000....
247 /// 111101000.00........0001........
248 /// 1111010...00........0001........
249 /// 1111010...00....................
250 /// 1111010.........................
251 /// 1111............................
252 /// ................................
253 /// VST4q8a 111101000_00________00010000____
254 /// VST4q8b 111101000_00________00010000____
256 /// The Debug output shows the path that the decoding tree follows to reach the
257 /// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
258 /// even registers, while VST4q8b is a vst4 to double-spaced odd regsisters.
260 /// The encoding info in the .td files does not specify this meta information,
261 /// which could have been used by the decoder to resolve the conflict. The
262 /// decoder could try to decode the even/odd register numbering and assign to
263 /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
264 /// version and return the Opcode since the two have the same Asm format string.
265 class Filter {
266 protected:
267 FilterChooser *Owner; // points to the FilterChooser who owns this filter
268 unsigned StartBit; // the starting bit position
269 unsigned NumBits; // number of bits to filter
270 bool Mixed; // a mixed region contains both set and unset bits
272 // Map of well-known segment value to the set of uid's with that value.
273 std::map<uint64_t, std::vector<unsigned> > FilteredInstructions;
275 // Set of uid's with non-constant segment values.
276 std::vector<unsigned> VariableInstructions;
278 // Map of well-known segment value to its delegate.
279 std::map<unsigned, FilterChooser*> FilterChooserMap;
281 // Number of instructions which fall under FilteredInstructions category.
282 unsigned NumFiltered;
284 // Keeps track of the last opcode in the filtered bucket.
285 unsigned LastOpcFiltered;
287 // Number of instructions which fall under VariableInstructions category.
288 unsigned NumVariable;
290 public:
291 unsigned getNumFiltered() { return NumFiltered; }
292 unsigned getNumVariable() { return NumVariable; }
293 unsigned getSingletonOpc() {
294 assert(NumFiltered == 1);
295 return LastOpcFiltered;
297 // Return the filter chooser for the group of instructions without constant
298 // segment values.
299 FilterChooser &getVariableFC() {
300 assert(NumFiltered == 1);
301 assert(FilterChooserMap.size() == 1);
302 return *(FilterChooserMap.find((unsigned)-1)->second);
305 Filter(const Filter &f);
306 Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
308 ~Filter();
310 // Divides the decoding task into sub tasks and delegates them to the
311 // inferior FilterChooser's.
313 // A special case arises when there's only one entry in the filtered
314 // instructions. In order to unambiguously decode the singleton, we need to
315 // match the remaining undecoded encoding bits against the singleton.
316 void recurse();
318 // Emit code to decode instructions given a segment or segments of bits.
319 void emit(raw_ostream &o, unsigned &Indentation);
321 // Returns the number of fanout produced by the filter. More fanout implies
322 // the filter distinguishes more categories of instructions.
323 unsigned usefulness() const;
324 }; // End of class Filter
326 // These are states of our finite state machines used in FilterChooser's
327 // filterProcessor() which produces the filter candidates to use.
328 typedef enum {
329 ATTR_NONE,
330 ATTR_FILTERED,
331 ATTR_ALL_SET,
332 ATTR_ALL_UNSET,
333 ATTR_MIXED
334 } bitAttr_t;
336 /// FilterChooser - FilterChooser chooses the best filter among a set of Filters
337 /// in order to perform the decoding of instructions at the current level.
339 /// Decoding proceeds from the top down. Based on the well-known encoding bits
340 /// of instructions available, FilterChooser builds up the possible Filters that
341 /// can further the task of decoding by distinguishing among the remaining
342 /// candidate instructions.
344 /// Once a filter has been chosen, it is called upon to divide the decoding task
345 /// into sub-tasks and delegates them to its inferior FilterChoosers for further
346 /// processings.
348 /// It is useful to think of a Filter as governing the switch stmts of the
349 /// decoding tree. And each case is delegated to an inferior FilterChooser to
350 /// decide what further remaining bits to look at.
351 class FilterChooser {
352 static TARGET_NAME_t TargetName;
354 protected:
355 friend class Filter;
357 // Vector of codegen instructions to choose our filter.
358 const std::vector<const CodeGenInstruction*> &AllInstructions;
360 // Vector of uid's for this filter chooser to work on.
361 const std::vector<unsigned> Opcodes;
363 // Vector of candidate filters.
364 std::vector<Filter> Filters;
366 // Array of bit values passed down from our parent.
367 // Set to all BIT_UNFILTERED's for Parent == NULL.
368 bit_value_t FilterBitValues[BIT_WIDTH];
370 // Links to the FilterChooser above us in the decoding tree.
371 FilterChooser *Parent;
373 // Index of the best filter from Filters.
374 int BestIndex;
376 public:
377 static void setTargetName(TARGET_NAME_t tn) { TargetName = tn; }
379 FilterChooser(const FilterChooser &FC) :
380 AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
381 Filters(FC.Filters), Parent(FC.Parent), BestIndex(FC.BestIndex) {
382 memcpy(FilterBitValues, FC.FilterBitValues, sizeof(FilterBitValues));
385 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
386 const std::vector<unsigned> &IDs) :
387 AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(NULL),
388 BestIndex(-1) {
389 for (unsigned i = 0; i < BIT_WIDTH; ++i)
390 FilterBitValues[i] = BIT_UNFILTERED;
392 doFilter();
395 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
396 const std::vector<unsigned> &IDs,
397 bit_value_t (&ParentFilterBitValues)[BIT_WIDTH],
398 FilterChooser &parent) :
399 AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(&parent),
400 BestIndex(-1) {
401 for (unsigned i = 0; i < BIT_WIDTH; ++i)
402 FilterBitValues[i] = ParentFilterBitValues[i];
404 doFilter();
407 // The top level filter chooser has NULL as its parent.
408 bool isTopLevel() { return Parent == NULL; }
410 // This provides an opportunity for target specific code emission.
411 void emitTopHook(raw_ostream &o);
413 // Emit the top level typedef and decodeInstruction() function.
414 void emitTop(raw_ostream &o, unsigned &Indentation);
416 // This provides an opportunity for target specific code emission after
417 // emitTop().
418 void emitBot(raw_ostream &o, unsigned &Indentation);
420 protected:
421 // Populates the insn given the uid.
422 void insnWithID(insn_t &Insn, unsigned Opcode) const {
423 BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
425 for (unsigned i = 0; i < BIT_WIDTH; ++i)
426 Insn[i] = bitFromBits(Bits, i);
428 // Set Inst{21} to 1 (wback) when IndexModeBits == IndexModeUpd.
429 if (getByteField(*AllInstructions[Opcode]->TheDef, "IndexModeBits")
430 == IndexModeUpd)
431 Insn[21] = BIT_TRUE;
434 // Returns the record name.
435 const std::string &nameWithID(unsigned Opcode) const {
436 return AllInstructions[Opcode]->TheDef->getName();
439 // Populates the field of the insn given the start position and the number of
440 // consecutive bits to scan for.
442 // Returns false if there exists any uninitialized bit value in the range.
443 // Returns true, otherwise.
444 bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
445 unsigned NumBits) const;
447 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
448 /// filter array as a series of chars.
449 void dumpFilterArray(raw_ostream &o, bit_value_t (&filter)[BIT_WIDTH]);
451 /// dumpStack - dumpStack traverses the filter chooser chain and calls
452 /// dumpFilterArray on each filter chooser up to the top level one.
453 void dumpStack(raw_ostream &o, const char *prefix);
455 Filter &bestFilter() {
456 assert(BestIndex != -1 && "BestIndex not set");
457 return Filters[BestIndex];
460 // Called from Filter::recurse() when singleton exists. For debug purpose.
461 void SingletonExists(unsigned Opc);
463 bool PositionFiltered(unsigned i) {
464 return ValueSet(FilterBitValues[i]);
467 // Calculates the island(s) needed to decode the instruction.
468 // This returns a lit of undecoded bits of an instructions, for example,
469 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
470 // decoded bits in order to verify that the instruction matches the Opcode.
471 unsigned getIslands(std::vector<unsigned> &StartBits,
472 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
473 insn_t &Insn);
475 // The purpose of this function is for the API client to detect possible
476 // Load/Store Coprocessor instructions. If the coprocessor number is of
477 // the instruction is either 10 or 11, the decoder should not report the
478 // instruction as LDC/LDC2/STC/STC2, but should match against Advanced SIMD or
479 // VFP instructions.
480 bool LdStCopEncoding1(unsigned Opc) {
481 const std::string &Name = nameWithID(Opc);
482 if (Name == "LDC_OFFSET" || Name == "LDC_OPTION" ||
483 Name == "LDC_POST" || Name == "LDC_PRE" ||
484 Name == "LDCL_OFFSET" || Name == "LDCL_OPTION" ||
485 Name == "LDCL_POST" || Name == "LDCL_PRE" ||
486 Name == "STC_OFFSET" || Name == "STC_OPTION" ||
487 Name == "STC_POST" || Name == "STC_PRE" ||
488 Name == "STCL_OFFSET" || Name == "STCL_OPTION" ||
489 Name == "STCL_POST" || Name == "STCL_PRE")
490 return true;
491 else
492 return false;
495 // Emits code to decode the singleton. Return true if we have matched all the
496 // well-known bits.
497 bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc);
499 // Emits code to decode the singleton, and then to decode the rest.
500 void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,Filter &Best);
502 // Assign a single filter and run with it.
503 void runSingleFilter(FilterChooser &owner, unsigned startBit, unsigned numBit,
504 bool mixed);
506 // reportRegion is a helper function for filterProcessor to mark a region as
507 // eligible for use as a filter region.
508 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
509 bool AllowMixed);
511 // FilterProcessor scans the well-known encoding bits of the instructions and
512 // builds up a list of candidate filters. It chooses the best filter and
513 // recursively descends down the decoding tree.
514 bool filterProcessor(bool AllowMixed, bool Greedy = true);
516 // Decides on the best configuration of filter(s) to use in order to decode
517 // the instructions. A conflict of instructions may occur, in which case we
518 // dump the conflict set to the standard error.
519 void doFilter();
521 // Emits code to decode our share of instructions. Returns true if the
522 // emitted code causes a return, which occurs if we know how to decode
523 // the instruction at this level or the instruction is not decodeable.
524 bool emit(raw_ostream &o, unsigned &Indentation);
527 ///////////////////////////
528 // //
529 // Filter Implmenetation //
530 // //
531 ///////////////////////////
533 Filter::Filter(const Filter &f) :
534 Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
535 FilteredInstructions(f.FilteredInstructions),
536 VariableInstructions(f.VariableInstructions),
537 FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered),
538 LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) {
541 Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
542 bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits),
543 Mixed(mixed) {
544 assert(StartBit + NumBits - 1 < BIT_WIDTH);
546 NumFiltered = 0;
547 LastOpcFiltered = 0;
548 NumVariable = 0;
550 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
551 insn_t Insn;
553 // Populates the insn given the uid.
554 Owner->insnWithID(Insn, Owner->Opcodes[i]);
556 uint64_t Field;
557 // Scans the segment for possibly well-specified encoding bits.
558 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
560 if (ok) {
561 // The encoding bits are well-known. Lets add the uid of the
562 // instruction into the bucket keyed off the constant field value.
563 LastOpcFiltered = Owner->Opcodes[i];
564 FilteredInstructions[Field].push_back(LastOpcFiltered);
565 ++NumFiltered;
566 } else {
567 // Some of the encoding bit(s) are unspecfied. This contributes to
568 // one additional member of "Variable" instructions.
569 VariableInstructions.push_back(Owner->Opcodes[i]);
570 ++NumVariable;
574 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
575 && "Filter returns no instruction categories");
578 Filter::~Filter() {
579 std::map<unsigned, FilterChooser*>::iterator filterIterator;
580 for (filterIterator = FilterChooserMap.begin();
581 filterIterator != FilterChooserMap.end();
582 filterIterator++) {
583 delete filterIterator->second;
587 // Divides the decoding task into sub tasks and delegates them to the
588 // inferior FilterChooser's.
590 // A special case arises when there's only one entry in the filtered
591 // instructions. In order to unambiguously decode the singleton, we need to
592 // match the remaining undecoded encoding bits against the singleton.
593 void Filter::recurse() {
594 std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator;
596 bit_value_t BitValueArray[BIT_WIDTH];
597 // Starts by inheriting our parent filter chooser's filter bit values.
598 memcpy(BitValueArray, Owner->FilterBitValues, sizeof(BitValueArray));
600 unsigned bitIndex;
602 if (VariableInstructions.size()) {
603 // Conservatively marks each segment position as BIT_UNSET.
604 for (bitIndex = 0; bitIndex < NumBits; bitIndex++)
605 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
607 // Delegates to an inferior filter chooser for futher processing on this
608 // group of instructions whose segment values are variable.
609 FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
610 (unsigned)-1,
611 new FilterChooser(Owner->AllInstructions,
612 VariableInstructions,
613 BitValueArray,
614 *Owner)
618 // No need to recurse for a singleton filtered instruction.
619 // See also Filter::emit().
620 if (getNumFiltered() == 1) {
621 //Owner->SingletonExists(LastOpcFiltered);
622 assert(FilterChooserMap.size() == 1);
623 return;
626 // Otherwise, create sub choosers.
627 for (mapIterator = FilteredInstructions.begin();
628 mapIterator != FilteredInstructions.end();
629 mapIterator++) {
631 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
632 for (bitIndex = 0; bitIndex < NumBits; bitIndex++) {
633 if (mapIterator->first & (1ULL << bitIndex))
634 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
635 else
636 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
639 // Delegates to an inferior filter chooser for futher processing on this
640 // category of instructions.
641 FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
642 mapIterator->first,
643 new FilterChooser(Owner->AllInstructions,
644 mapIterator->second,
645 BitValueArray,
646 *Owner)
651 // Emit code to decode instructions given a segment or segments of bits.
652 void Filter::emit(raw_ostream &o, unsigned &Indentation) {
653 o.indent(Indentation) << "// Check Inst{";
655 if (NumBits > 1)
656 o << (StartBit + NumBits - 1) << '-';
658 o << StartBit << "} ...\n";
660 o.indent(Indentation) << "switch (fieldFromInstruction(insn, "
661 << StartBit << ", " << NumBits << ")) {\n";
663 std::map<unsigned, FilterChooser*>::iterator filterIterator;
665 bool DefaultCase = false;
666 for (filterIterator = FilterChooserMap.begin();
667 filterIterator != FilterChooserMap.end();
668 filterIterator++) {
670 // Field value -1 implies a non-empty set of variable instructions.
671 // See also recurse().
672 if (filterIterator->first == (unsigned)-1) {
673 DefaultCase = true;
675 o.indent(Indentation) << "default:\n";
676 o.indent(Indentation) << " break; // fallthrough\n";
678 // Closing curly brace for the switch statement.
679 // This is unconventional because we want the default processing to be
680 // performed for the fallthrough cases as well, i.e., when the "cases"
681 // did not prove a decoded instruction.
682 o.indent(Indentation) << "}\n";
684 } else
685 o.indent(Indentation) << "case " << filterIterator->first << ":\n";
687 // We arrive at a category of instructions with the same segment value.
688 // Now delegate to the sub filter chooser for further decodings.
689 // The case may fallthrough, which happens if the remaining well-known
690 // encoding bits do not match exactly.
691 if (!DefaultCase) { ++Indentation; ++Indentation; }
693 bool finished = filterIterator->second->emit(o, Indentation);
694 // For top level default case, there's no need for a break statement.
695 if (Owner->isTopLevel() && DefaultCase)
696 break;
697 if (!finished)
698 o.indent(Indentation) << "break;\n";
700 if (!DefaultCase) { --Indentation; --Indentation; }
703 // If there is no default case, we still need to supply a closing brace.
704 if (!DefaultCase) {
705 // Closing curly brace for the switch statement.
706 o.indent(Indentation) << "}\n";
710 // Returns the number of fanout produced by the filter. More fanout implies
711 // the filter distinguishes more categories of instructions.
712 unsigned Filter::usefulness() const {
713 if (VariableInstructions.size())
714 return FilteredInstructions.size();
715 else
716 return FilteredInstructions.size() + 1;
719 //////////////////////////////////
720 // //
721 // Filterchooser Implementation //
722 // //
723 //////////////////////////////////
725 // Define the symbol here.
726 TARGET_NAME_t FilterChooser::TargetName;
728 // This provides an opportunity for target specific code emission.
729 void FilterChooser::emitTopHook(raw_ostream &o) {
730 if (TargetName == TARGET_ARM) {
731 // Emit code that references the ARMFormat data type.
732 o << "static const ARMFormat ARMFormats[] = {\n";
733 for (unsigned i = 0, e = AllInstructions.size(); i != e; ++i) {
734 const Record &Def = *(AllInstructions[i]->TheDef);
735 const std::string &Name = Def.getName();
736 if (Def.isSubClassOf("InstARM") || Def.isSubClassOf("InstThumb"))
737 o.indent(2) <<
738 stringForARMFormat((ARMFormat)getByteField(Def, "Form"));
739 else
740 o << " ARM_FORMAT_NA";
742 o << ",\t// Inst #" << i << " = " << Name << '\n';
744 o << " ARM_FORMAT_NA\t// Unreachable.\n";
745 o << "};\n\n";
749 // Emit the top level typedef and decodeInstruction() function.
750 void FilterChooser::emitTop(raw_ostream &o, unsigned &Indentation) {
751 // Run the target specific emit hook.
752 emitTopHook(o);
754 switch (BIT_WIDTH) {
755 case 8:
756 o.indent(Indentation) << "typedef uint8_t field_t;\n";
757 break;
758 case 16:
759 o.indent(Indentation) << "typedef uint16_t field_t;\n";
760 break;
761 case 32:
762 o.indent(Indentation) << "typedef uint32_t field_t;\n";
763 break;
764 case 64:
765 o.indent(Indentation) << "typedef uint64_t field_t;\n";
766 break;
767 default:
768 assert(0 && "Unexpected instruction size!");
771 o << '\n';
773 o.indent(Indentation) << "static field_t " <<
774 "fieldFromInstruction(field_t insn, unsigned startBit, unsigned numBits)\n";
776 o.indent(Indentation) << "{\n";
778 ++Indentation; ++Indentation;
779 o.indent(Indentation) << "assert(startBit + numBits <= " << BIT_WIDTH
780 << " && \"Instruction field out of bounds!\");\n";
781 o << '\n';
782 o.indent(Indentation) << "field_t fieldMask;\n";
783 o << '\n';
784 o.indent(Indentation) << "if (numBits == " << BIT_WIDTH << ")\n";
786 ++Indentation; ++Indentation;
787 o.indent(Indentation) << "fieldMask = (field_t)-1;\n";
788 --Indentation; --Indentation;
790 o.indent(Indentation) << "else\n";
792 ++Indentation; ++Indentation;
793 o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n";
794 --Indentation; --Indentation;
796 o << '\n';
797 o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n";
798 --Indentation; --Indentation;
800 o.indent(Indentation) << "}\n";
802 o << '\n';
804 o.indent(Indentation) <<"static uint16_t decodeInstruction(field_t insn) {\n";
806 ++Indentation; ++Indentation;
807 // Emits code to decode the instructions.
808 emit(o, Indentation);
810 o << '\n';
811 o.indent(Indentation) << "return 0;\n";
812 --Indentation; --Indentation;
814 o.indent(Indentation) << "}\n";
816 o << '\n';
819 // This provides an opportunity for target specific code emission after
820 // emitTop().
821 void FilterChooser::emitBot(raw_ostream &o, unsigned &Indentation) {
822 if (TargetName != TARGET_THUMB) return;
824 // Emit code that decodes the Thumb ISA.
825 o.indent(Indentation)
826 << "static uint16_t decodeThumbInstruction(field_t insn) {\n";
828 ++Indentation; ++Indentation;
830 // Emits code to decode the instructions.
831 emit(o, Indentation);
833 o << '\n';
834 o.indent(Indentation) << "return 0;\n";
836 --Indentation; --Indentation;
838 o.indent(Indentation) << "}\n";
841 // Populates the field of the insn given the start position and the number of
842 // consecutive bits to scan for.
844 // Returns false if and on the first uninitialized bit value encountered.
845 // Returns true, otherwise.
846 bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
847 unsigned StartBit, unsigned NumBits) const {
848 Field = 0;
850 for (unsigned i = 0; i < NumBits; ++i) {
851 if (Insn[StartBit + i] == BIT_UNSET)
852 return false;
854 if (Insn[StartBit + i] == BIT_TRUE)
855 Field = Field | (1ULL << i);
858 return true;
861 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
862 /// filter array as a series of chars.
863 void FilterChooser::dumpFilterArray(raw_ostream &o,
864 bit_value_t (&filter)[BIT_WIDTH]) {
865 unsigned bitIndex;
867 for (bitIndex = BIT_WIDTH; bitIndex > 0; bitIndex--) {
868 switch (filter[bitIndex - 1]) {
869 case BIT_UNFILTERED:
870 o << ".";
871 break;
872 case BIT_UNSET:
873 o << "_";
874 break;
875 case BIT_TRUE:
876 o << "1";
877 break;
878 case BIT_FALSE:
879 o << "0";
880 break;
885 /// dumpStack - dumpStack traverses the filter chooser chain and calls
886 /// dumpFilterArray on each filter chooser up to the top level one.
887 void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) {
888 FilterChooser *current = this;
890 while (current) {
891 o << prefix;
892 dumpFilterArray(o, current->FilterBitValues);
893 o << '\n';
894 current = current->Parent;
898 // Called from Filter::recurse() when singleton exists. For debug purpose.
899 void FilterChooser::SingletonExists(unsigned Opc) {
900 insn_t Insn0;
901 insnWithID(Insn0, Opc);
903 errs() << "Singleton exists: " << nameWithID(Opc)
904 << " with its decoding dominating ";
905 for (unsigned i = 0; i < Opcodes.size(); ++i) {
906 if (Opcodes[i] == Opc) continue;
907 errs() << nameWithID(Opcodes[i]) << ' ';
909 errs() << '\n';
911 dumpStack(errs(), "\t\t");
912 for (unsigned i = 0; i < Opcodes.size(); i++) {
913 const std::string &Name = nameWithID(Opcodes[i]);
915 errs() << '\t' << Name << " ";
916 dumpBits(errs(),
917 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
918 errs() << '\n';
922 // Calculates the island(s) needed to decode the instruction.
923 // This returns a list of undecoded bits of an instructions, for example,
924 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
925 // decoded bits in order to verify that the instruction matches the Opcode.
926 unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
927 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
928 insn_t &Insn) {
929 unsigned Num, BitNo;
930 Num = BitNo = 0;
932 uint64_t FieldVal = 0;
934 // 0: Init
935 // 1: Water (the bit value does not affect decoding)
936 // 2: Island (well-known bit value needed for decoding)
937 int State = 0;
938 int Val = -1;
940 for (unsigned i = 0; i < BIT_WIDTH; ++i) {
941 Val = Value(Insn[i]);
942 bool Filtered = PositionFiltered(i);
943 switch (State) {
944 default:
945 assert(0 && "Unreachable code!");
946 break;
947 case 0:
948 case 1:
949 if (Filtered || Val == -1)
950 State = 1; // Still in Water
951 else {
952 State = 2; // Into the Island
953 BitNo = 0;
954 StartBits.push_back(i);
955 FieldVal = Val;
957 break;
958 case 2:
959 if (Filtered || Val == -1) {
960 State = 1; // Into the Water
961 EndBits.push_back(i - 1);
962 FieldVals.push_back(FieldVal);
963 ++Num;
964 } else {
965 State = 2; // Still in Island
966 ++BitNo;
967 FieldVal = FieldVal | Val << BitNo;
969 break;
972 // If we are still in Island after the loop, do some housekeeping.
973 if (State == 2) {
974 EndBits.push_back(BIT_WIDTH - 1);
975 FieldVals.push_back(FieldVal);
976 ++Num;
979 assert(StartBits.size() == Num && EndBits.size() == Num &&
980 FieldVals.size() == Num);
981 return Num;
984 // Emits code to decode the singleton. Return true if we have matched all the
985 // well-known bits.
986 bool FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
987 unsigned Opc) {
988 std::vector<unsigned> StartBits;
989 std::vector<unsigned> EndBits;
990 std::vector<uint64_t> FieldVals;
991 insn_t Insn;
992 insnWithID(Insn, Opc);
994 // This provides a good opportunity to check for possible Ld/St Coprocessor
995 // Opcode and escapes if the coproc # is either 10 or 11. It is a NEON/VFP
996 // instruction is disguise.
997 if (TargetName == TARGET_ARM && LdStCopEncoding1(Opc)) {
998 o.indent(Indentation);
999 // A8.6.51 & A8.6.188
1000 // If coproc = 0b101?, i.e, slice(insn, 11, 8) = 10 or 11, escape.
1001 o << "if (fieldFromInstruction(insn, 9, 3) == 5) break; // fallthrough\n";
1004 // Look for islands of undecoded bits of the singleton.
1005 getIslands(StartBits, EndBits, FieldVals, Insn);
1007 unsigned Size = StartBits.size();
1008 unsigned I, NumBits;
1010 // If we have matched all the well-known bits, just issue a return.
1011 if (Size == 0) {
1012 o.indent(Indentation) << "return " << Opc << "; // " << nameWithID(Opc)
1013 << '\n';
1014 return true;
1017 // Otherwise, there are more decodings to be done!
1019 // Emit code to match the island(s) for the singleton.
1020 o.indent(Indentation) << "// Check ";
1022 for (I = Size; I != 0; --I) {
1023 o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} ";
1024 if (I > 1)
1025 o << "&& ";
1026 else
1027 o << "for singleton decoding...\n";
1030 o.indent(Indentation) << "if (";
1032 for (I = Size; I != 0; --I) {
1033 NumBits = EndBits[I-1] - StartBits[I-1] + 1;
1034 o << "fieldFromInstruction(insn, " << StartBits[I-1] << ", " << NumBits
1035 << ") == " << FieldVals[I-1];
1036 if (I > 1)
1037 o << " && ";
1038 else
1039 o << ")\n";
1042 o.indent(Indentation) << " return " << Opc << "; // " << nameWithID(Opc)
1043 << '\n';
1045 return false;
1048 // Emits code to decode the singleton, and then to decode the rest.
1049 void FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
1050 Filter &Best) {
1052 unsigned Opc = Best.getSingletonOpc();
1054 emitSingletonDecoder(o, Indentation, Opc);
1056 // Emit code for the rest.
1057 o.indent(Indentation) << "else\n";
1059 Indentation += 2;
1060 Best.getVariableFC().emit(o, Indentation);
1061 Indentation -= 2;
1064 // Assign a single filter and run with it. Top level API client can initialize
1065 // with a single filter to start the filtering process.
1066 void FilterChooser::runSingleFilter(FilterChooser &owner, unsigned startBit,
1067 unsigned numBit, bool mixed) {
1068 Filters.clear();
1069 Filter F(*this, startBit, numBit, true);
1070 Filters.push_back(F);
1071 BestIndex = 0; // Sole Filter instance to choose from.
1072 bestFilter().recurse();
1075 // reportRegion is a helper function for filterProcessor to mark a region as
1076 // eligible for use as a filter region.
1077 void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1078 unsigned BitIndex, bool AllowMixed) {
1079 if (RA == ATTR_MIXED && AllowMixed)
1080 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true));
1081 else if (RA == ATTR_ALL_SET && !AllowMixed)
1082 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false));
1085 // FilterProcessor scans the well-known encoding bits of the instructions and
1086 // builds up a list of candidate filters. It chooses the best filter and
1087 // recursively descends down the decoding tree.
1088 bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1089 Filters.clear();
1090 BestIndex = -1;
1091 unsigned numInstructions = Opcodes.size();
1093 assert(numInstructions && "Filter created with no instructions");
1095 // No further filtering is necessary.
1096 if (numInstructions == 1)
1097 return true;
1099 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1100 // instructions is 3.
1101 if (AllowMixed && !Greedy) {
1102 assert(numInstructions == 3);
1104 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1105 std::vector<unsigned> StartBits;
1106 std::vector<unsigned> EndBits;
1107 std::vector<uint64_t> FieldVals;
1108 insn_t Insn;
1110 insnWithID(Insn, Opcodes[i]);
1112 // Look for islands of undecoded bits of any instruction.
1113 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1114 // Found an instruction with island(s). Now just assign a filter.
1115 runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1,
1116 true);
1117 return true;
1122 unsigned BitIndex, InsnIndex;
1124 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1125 // The automaton consumes the corresponding bit from each
1126 // instruction.
1128 // Input symbols: 0, 1, and _ (unset).
1129 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1130 // Initial state: NONE.
1132 // (NONE) ------- [01] -> (ALL_SET)
1133 // (NONE) ------- _ ----> (ALL_UNSET)
1134 // (ALL_SET) ---- [01] -> (ALL_SET)
1135 // (ALL_SET) ---- _ ----> (MIXED)
1136 // (ALL_UNSET) -- [01] -> (MIXED)
1137 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1138 // (MIXED) ------ . ----> (MIXED)
1139 // (FILTERED)---- . ----> (FILTERED)
1141 bitAttr_t bitAttrs[BIT_WIDTH];
1143 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1144 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1145 for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex)
1146 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1147 FilterBitValues[BitIndex] == BIT_FALSE)
1148 bitAttrs[BitIndex] = ATTR_FILTERED;
1149 else
1150 bitAttrs[BitIndex] = ATTR_NONE;
1152 for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
1153 insn_t insn;
1155 insnWithID(insn, Opcodes[InsnIndex]);
1157 for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex) {
1158 switch (bitAttrs[BitIndex]) {
1159 case ATTR_NONE:
1160 if (insn[BitIndex] == BIT_UNSET)
1161 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1162 else
1163 bitAttrs[BitIndex] = ATTR_ALL_SET;
1164 break;
1165 case ATTR_ALL_SET:
1166 if (insn[BitIndex] == BIT_UNSET)
1167 bitAttrs[BitIndex] = ATTR_MIXED;
1168 break;
1169 case ATTR_ALL_UNSET:
1170 if (insn[BitIndex] != BIT_UNSET)
1171 bitAttrs[BitIndex] = ATTR_MIXED;
1172 break;
1173 case ATTR_MIXED:
1174 case ATTR_FILTERED:
1175 break;
1180 // The regionAttr automaton consumes the bitAttrs automatons' state,
1181 // lowest-to-highest.
1183 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1184 // States: NONE, ALL_SET, MIXED
1185 // Initial state: NONE
1187 // (NONE) ----- F --> (NONE)
1188 // (NONE) ----- S --> (ALL_SET) ; and set region start
1189 // (NONE) ----- U --> (NONE)
1190 // (NONE) ----- M --> (MIXED) ; and set region start
1191 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1192 // (ALL_SET) -- S --> (ALL_SET)
1193 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1194 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1195 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1196 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1197 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1198 // (MIXED) ---- M --> (MIXED)
1200 bitAttr_t RA = ATTR_NONE;
1201 unsigned StartBit = 0;
1203 for (BitIndex = 0; BitIndex < BIT_WIDTH; BitIndex++) {
1204 bitAttr_t bitAttr = bitAttrs[BitIndex];
1206 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1208 switch (RA) {
1209 case ATTR_NONE:
1210 switch (bitAttr) {
1211 case ATTR_FILTERED:
1212 break;
1213 case ATTR_ALL_SET:
1214 StartBit = BitIndex;
1215 RA = ATTR_ALL_SET;
1216 break;
1217 case ATTR_ALL_UNSET:
1218 break;
1219 case ATTR_MIXED:
1220 StartBit = BitIndex;
1221 RA = ATTR_MIXED;
1222 break;
1223 default:
1224 assert(0 && "Unexpected bitAttr!");
1226 break;
1227 case ATTR_ALL_SET:
1228 switch (bitAttr) {
1229 case ATTR_FILTERED:
1230 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1231 RA = ATTR_NONE;
1232 break;
1233 case ATTR_ALL_SET:
1234 break;
1235 case ATTR_ALL_UNSET:
1236 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1237 RA = ATTR_NONE;
1238 break;
1239 case ATTR_MIXED:
1240 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1241 StartBit = BitIndex;
1242 RA = ATTR_MIXED;
1243 break;
1244 default:
1245 assert(0 && "Unexpected bitAttr!");
1247 break;
1248 case ATTR_MIXED:
1249 switch (bitAttr) {
1250 case ATTR_FILTERED:
1251 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1252 StartBit = BitIndex;
1253 RA = ATTR_NONE;
1254 break;
1255 case ATTR_ALL_SET:
1256 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1257 StartBit = BitIndex;
1258 RA = ATTR_ALL_SET;
1259 break;
1260 case ATTR_ALL_UNSET:
1261 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1262 RA = ATTR_NONE;
1263 break;
1264 case ATTR_MIXED:
1265 break;
1266 default:
1267 assert(0 && "Unexpected bitAttr!");
1269 break;
1270 case ATTR_ALL_UNSET:
1271 assert(0 && "regionAttr state machine has no ATTR_UNSET state");
1272 case ATTR_FILTERED:
1273 assert(0 && "regionAttr state machine has no ATTR_FILTERED state");
1277 // At the end, if we're still in ALL_SET or MIXED states, report a region
1278 switch (RA) {
1279 case ATTR_NONE:
1280 break;
1281 case ATTR_FILTERED:
1282 break;
1283 case ATTR_ALL_SET:
1284 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1285 break;
1286 case ATTR_ALL_UNSET:
1287 break;
1288 case ATTR_MIXED:
1289 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1290 break;
1293 // We have finished with the filter processings. Now it's time to choose
1294 // the best performing filter.
1295 BestIndex = 0;
1296 bool AllUseless = true;
1297 unsigned BestScore = 0;
1299 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1300 unsigned Usefulness = Filters[i].usefulness();
1302 if (Usefulness)
1303 AllUseless = false;
1305 if (Usefulness > BestScore) {
1306 BestIndex = i;
1307 BestScore = Usefulness;
1311 if (!AllUseless)
1312 bestFilter().recurse();
1314 return !AllUseless;
1315 } // end of FilterChooser::filterProcessor(bool)
1317 // Decides on the best configuration of filter(s) to use in order to decode
1318 // the instructions. A conflict of instructions may occur, in which case we
1319 // dump the conflict set to the standard error.
1320 void FilterChooser::doFilter() {
1321 unsigned Num = Opcodes.size();
1322 assert(Num && "FilterChooser created with no instructions");
1324 // Heuristics: Use Inst{31-28} as the top level filter for ARM ISA.
1325 if (TargetName == TARGET_ARM && Parent == NULL) {
1326 runSingleFilter(*this, 28, 4, false);
1327 return;
1330 // Try regions of consecutive known bit values first.
1331 if (filterProcessor(false))
1332 return;
1334 // Then regions of mixed bits (both known and unitialized bit values allowed).
1335 if (filterProcessor(true))
1336 return;
1338 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1339 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1340 // well-known encoding pattern. In such case, we backtrack and scan for the
1341 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1342 if (Num == 3 && filterProcessor(true, false))
1343 return;
1345 // If we come to here, the instruction decoding has failed.
1346 // Set the BestIndex to -1 to indicate so.
1347 BestIndex = -1;
1350 // Emits code to decode our share of instructions. Returns true if the
1351 // emitted code causes a return, which occurs if we know how to decode
1352 // the instruction at this level or the instruction is not decodeable.
1353 bool FilterChooser::emit(raw_ostream &o, unsigned &Indentation) {
1354 if (Opcodes.size() == 1)
1355 // There is only one instruction in the set, which is great!
1356 // Call emitSingletonDecoder() to see whether there are any remaining
1357 // encodings bits.
1358 return emitSingletonDecoder(o, Indentation, Opcodes[0]);
1360 // Choose the best filter to do the decodings!
1361 if (BestIndex != -1) {
1362 Filter &Best = bestFilter();
1363 if (Best.getNumFiltered() == 1)
1364 emitSingletonDecoder(o, Indentation, Best);
1365 else
1366 bestFilter().emit(o, Indentation);
1367 return false;
1370 // If we reach here, there is a conflict in decoding. Let's resolve the known
1371 // conflicts!
1372 if ((TargetName == TARGET_ARM || TargetName == TARGET_THUMB) &&
1373 Opcodes.size() == 2) {
1374 // Resolve the known conflict sets:
1376 // 1. source registers are identical => VMOVDneon; otherwise => VORRd
1377 // 2. source registers are identical => VMOVQ; otherwise => VORRq
1378 // 3. LDR, LDRcp => return LDR for now.
1379 // FIXME: How can we distinguish between LDR and LDRcp? Do we need to?
1380 // 4. tLDM, tLDM_UPD => Rn = Inst{10-8}, reglist = Inst{7-0},
1381 // wback = registers<Rn> = 0
1382 // NOTE: (tLDM, tLDM_UPD) resolution must come before Advanced SIMD
1383 // addressing mode resolution!!!
1384 // 5. VLD[234]LN*/VST[234]LN* vs. VLD[234]LN*_UPD/VST[234]LN*_UPD conflicts
1385 // are resolved returning the non-UPD versions of the instructions if the
1386 // Rm field, i.e., Inst{3-0} is 0b1111. This is specified in A7.7.1
1387 // Advanced SIMD addressing mode.
1388 const std::string &name1 = nameWithID(Opcodes[0]);
1389 const std::string &name2 = nameWithID(Opcodes[1]);
1390 if ((name1 == "VMOVDneon" && name2 == "VORRd") ||
1391 (name1 == "VMOVQ" && name2 == "VORRq")) {
1392 // Inserting the opening curly brace for this case block.
1393 --Indentation; --Indentation;
1394 o.indent(Indentation) << "{\n";
1395 ++Indentation; ++Indentation;
1397 o.indent(Indentation)
1398 << "field_t N = fieldFromInstruction(insn, 7, 1), "
1399 << "M = fieldFromInstruction(insn, 5, 1);\n";
1400 o.indent(Indentation)
1401 << "field_t Vn = fieldFromInstruction(insn, 16, 4), "
1402 << "Vm = fieldFromInstruction(insn, 0, 4);\n";
1403 o.indent(Indentation)
1404 << "return (N == M && Vn == Vm) ? "
1405 << Opcodes[0] << " /* " << name1 << " */ : "
1406 << Opcodes[1] << " /* " << name2 << " */ ;\n";
1408 // Inserting the closing curly brace for this case block.
1409 --Indentation; --Indentation;
1410 o.indent(Indentation) << "}\n";
1411 ++Indentation; ++Indentation;
1413 return true;
1415 if (name1 == "LDR" && name2 == "LDRcp") {
1416 o.indent(Indentation)
1417 << "return " << Opcodes[0]
1418 << "; // Returning LDR for {LDR, LDRcp}\n";
1419 return true;
1421 if (name1 == "tLDM" && name2 == "tLDM_UPD") {
1422 // Inserting the opening curly brace for this case block.
1423 --Indentation; --Indentation;
1424 o.indent(Indentation) << "{\n";
1425 ++Indentation; ++Indentation;
1427 o.indent(Indentation)
1428 << "unsigned Rn = fieldFromInstruction(insn, 8, 3), "
1429 << "list = fieldFromInstruction(insn, 0, 8);\n";
1430 o.indent(Indentation)
1431 << "return ((list >> Rn) & 1) == 0 ? "
1432 << Opcodes[1] << " /* " << name2 << " */ : "
1433 << Opcodes[0] << " /* " << name1 << " */ ;\n";
1435 // Inserting the closing curly brace for this case block.
1436 --Indentation; --Indentation;
1437 o.indent(Indentation) << "}\n";
1438 ++Indentation; ++Indentation;
1440 return true;
1442 if (sameStringExceptSuffix(name1, name2, "_UPD")) {
1443 o.indent(Indentation)
1444 << "return fieldFromInstruction(insn, 0, 4) == 15 ? " << Opcodes[0]
1445 << " /* " << name1 << " */ : " << Opcodes[1] << "/* " << name2
1446 << " */ ; // Advanced SIMD addressing mode\n";
1447 return true;
1450 // Otherwise, it does not belong to the known conflict sets.
1453 // We don't know how to decode these instructions! Return 0 and dump the
1454 // conflict set!
1455 o.indent(Indentation) << "return 0;" << " // Conflict set: ";
1456 for (int i = 0, N = Opcodes.size(); i < N; ++i) {
1457 o << nameWithID(Opcodes[i]);
1458 if (i < (N - 1))
1459 o << ", ";
1460 else
1461 o << '\n';
1464 // Print out useful conflict information for postmortem analysis.
1465 errs() << "Decoding Conflict:\n";
1467 dumpStack(errs(), "\t\t");
1469 for (unsigned i = 0; i < Opcodes.size(); i++) {
1470 const std::string &Name = nameWithID(Opcodes[i]);
1472 errs() << '\t' << Name << " ";
1473 dumpBits(errs(),
1474 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1475 errs() << '\n';
1478 return true;
1482 ////////////////////////////////////////////
1483 // //
1484 // ARMDEBackend //
1485 // (Helper class for ARMDecoderEmitter) //
1486 // //
1487 ////////////////////////////////////////////
1489 class ARMDecoderEmitter::ARMDEBackend {
1490 public:
1491 ARMDEBackend(ARMDecoderEmitter &frontend) :
1492 NumberedInstructions(),
1493 Opcodes(),
1494 Frontend(frontend),
1495 Target(),
1496 FC(NULL)
1498 if (Target.getName() == "ARM")
1499 TargetName = TARGET_ARM;
1500 else {
1501 errs() << "Target name " << Target.getName() << " not recognized\n";
1502 assert(0 && "Unknown target");
1505 // Populate the instructions for our TargetName.
1506 populateInstructions();
1509 ~ARMDEBackend() {
1510 if (FC) {
1511 delete FC;
1512 FC = NULL;
1516 void getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
1517 &NumberedInstructions) {
1518 // We must emit the PHI opcode first...
1519 std::string Namespace = Target.getInstNamespace();
1520 assert(!Namespace.empty() && "No instructions defined.");
1522 NumberedInstructions = Target.getInstructionsByEnumValue();
1525 bool populateInstruction(const CodeGenInstruction &CGI, TARGET_NAME_t TN);
1527 void populateInstructions();
1529 // Emits disassembler code for instruction decoding. This delegates to the
1530 // FilterChooser instance to do the heavy lifting.
1531 void emit(raw_ostream &o);
1533 protected:
1534 std::vector<const CodeGenInstruction*> NumberedInstructions;
1535 std::vector<unsigned> Opcodes;
1536 // Special case for the ARM chip, which supports ARM and Thumb ISAs.
1537 // Opcodes2 will be populated with the Thumb opcodes.
1538 std::vector<unsigned> Opcodes2;
1539 ARMDecoderEmitter &Frontend;
1540 CodeGenTarget Target;
1541 FilterChooser *FC;
1543 TARGET_NAME_t TargetName;
1546 bool ARMDecoderEmitter::ARMDEBackend::populateInstruction(
1547 const CodeGenInstruction &CGI, TARGET_NAME_t TN) {
1548 const Record &Def = *CGI.TheDef;
1549 const StringRef Name = Def.getName();
1550 uint8_t Form = getByteField(Def, "Form");
1552 BitsInit &Bits = getBitsField(Def, "Inst");
1554 // If all the bit positions are not specified; do not decode this instruction.
1555 // We are bound to fail! For proper disassembly, the well-known encoding bits
1556 // of the instruction must be fully specified.
1558 // This also removes pseudo instructions from considerations of disassembly,
1559 // which is a better design and less fragile than the name matchings.
1560 if (Bits.allInComplete()) return false;
1562 if (TN == TARGET_ARM) {
1563 // FIXME: what about Int_MemBarrierV6 and Int_SyncBarrierV6?
1564 if ((Name != "Int_MemBarrierV7" && Name != "Int_SyncBarrierV7") &&
1565 Form == ARM_FORMAT_PSEUDO)
1566 return false;
1567 if (thumbInstruction(Form))
1568 return false;
1569 if (Name.find("CMPz") != std::string::npos /* ||
1570 Name.find("CMNz") != std::string::npos */)
1571 return false;
1573 // Ignore pseudo instructions.
1574 if (Name == "BXr9" || Name == "BMOVPCRX" || Name == "BMOVPCRXr9")
1575 return false;
1577 // Tail calls are other patterns that generate existing instructions.
1578 if (Name == "TCRETURNdi" || Name == "TCRETURNdiND" ||
1579 Name == "TCRETURNri" || Name == "TCRETURNriND" ||
1580 Name == "TAILJMPd" || Name == "TAILJMPdt" ||
1581 Name == "TAILJMPdND" || Name == "TAILJMPdNDt" ||
1582 Name == "TAILJMPr" || Name == "TAILJMPrND" ||
1583 Name == "MOVr_TC")
1584 return false;
1586 // VLDMQ/VSTMQ can be handled with the more generic VLDMD/VSTMD.
1587 if (Name == "VLDMQ" || Name == "VLDMQ_UPD" ||
1588 Name == "VSTMQ" || Name == "VSTMQ_UPD")
1589 return false;
1592 // The following special cases are for conflict resolutions.
1595 // NEON NLdStFrm conflict resolutions:
1597 // 1. Ignore suffix "odd" and "odd_UPD", prefer the "even" register-
1598 // numbered ones which have the same Asm format string.
1599 // 2. Ignore VST2d64_UPD, which conflicts with VST1q64_UPD.
1600 // 3. Ignore VLD2d64_UPD, which conflicts with VLD1q64_UPD.
1601 // 4. Ignore VLD1q[_UPD], which conflicts with VLD1q64[_UPD].
1602 // 5. Ignore VST1q[_UPD], which conflicts with VST1q64[_UPD].
1603 if (Name.endswith("odd") || Name.endswith("odd_UPD") ||
1604 Name == "VST2d64_UPD" || Name == "VLD2d64_UPD" ||
1605 Name == "VLD1q" || Name == "VLD1q_UPD" ||
1606 Name == "VST1q" || Name == "VST1q_UPD")
1607 return false;
1609 // RSCSri and RSCSrs set the 's' bit, but are not predicated. We are
1610 // better off using the generic RSCri and RSCrs instructions.
1611 if (Name == "RSCSri" || Name == "RSCSrs") return false;
1613 // MOVCCr, MOVCCs, MOVCCi, MOVCCi16, FCYPScc, FCYPDcc, FNEGScc, and
1614 // FNEGDcc are used in the compiler to implement conditional moves.
1615 // We can ignore them in favor of their more generic versions of
1616 // instructions. See also SDNode *ARMDAGToDAGISel::Select(SDValue Op).
1617 if (Name == "MOVCCr" || Name == "MOVCCs" || Name == "MOVCCi" ||
1618 Name == "MOVCCi16" || Name == "FCPYScc" || Name == "FCPYDcc" ||
1619 Name == "FNEGScc" || Name == "FNEGDcc")
1620 return false;
1622 // Ditto for VMOVDcc, VMOVScc, VNEGDcc, and VNEGScc.
1623 if (Name == "VMOVDcc" || Name == "VMOVScc" || Name == "VNEGDcc" ||
1624 Name == "VNEGScc")
1625 return false;
1627 // Ignore the *_sfp instructions when decoding. They are used by the
1628 // compiler to implement scalar floating point operations using vector
1629 // operations in order to work around some performance issues.
1630 if (Name.find("_sfp") != std::string::npos) return false;
1632 // LDM_RET is a special case of LDM (Load Multiple) where the registers
1633 // loaded include the PC, causing a branch to a loaded address. Ignore
1634 // the LDM_RET instruction when decoding.
1635 if (Name == "LDM_RET") return false;
1637 // Bcc is in a more generic form than B. Ignore B when decoding.
1638 if (Name == "B") return false;
1640 // Ignore the non-Darwin BL instructions and the TPsoft (TLS) instruction.
1641 if (Name == "BL" || Name == "BL_pred" || Name == "BLX" || Name == "BX" ||
1642 Name == "TPsoft")
1643 return false;
1645 // Ignore VDUPf[d|q] instructions known to conflict with VDUP32[d-q] for
1646 // decoding. The instruction duplicates an element from an ARM core
1647 // register into every element of the destination vector. There is no
1648 // distinction between data types.
1649 if (Name == "VDUPfd" || Name == "VDUPfq") return false;
1651 // A8-598: VEXT
1652 // Vector Extract extracts elements from the bottom end of the second
1653 // operand vector and the top end of the first, concatenates them and
1654 // places the result in the destination vector. The elements of the
1655 // vectors are treated as being 8-bit bitfields. There is no distinction
1656 // between data types. The size of the operation can be specified in
1657 // assembler as vext.size. If the value is 16, 32, or 64, the syntax is
1658 // a pseudo-instruction for a VEXT instruction specifying the equivalent
1659 // number of bytes.
1661 // Variants VEXTd16, VEXTd32, VEXTd8, and VEXTdf are reduced to VEXTd8;
1662 // variants VEXTq16, VEXTq32, VEXTq8, and VEXTqf are reduced to VEXTq8.
1663 if (Name == "VEXTd16" || Name == "VEXTd32" || Name == "VEXTdf" ||
1664 Name == "VEXTq16" || Name == "VEXTq32" || Name == "VEXTqf")
1665 return false;
1667 // Vector Reverse is similar to Vector Extract. There is no distinction
1668 // between data types, other than size.
1670 // VREV64df is equivalent to VREV64d32.
1671 // VREV64qf is equivalent to VREV64q32.
1672 if (Name == "VREV64df" || Name == "VREV64qf") return false;
1674 // VDUPLNfd is equivalent to VDUPLN32d.
1675 // VDUPLNfq is equivalent to VDUPLN32q.
1676 // VLD1df is equivalent to VLD1d32.
1677 // VLD1qf is equivalent to VLD1q32.
1678 // VLD2d64 is equivalent to VLD1q64.
1679 // VST1df is equivalent to VST1d32.
1680 // VST1qf is equivalent to VST1q32.
1681 // VST2d64 is equivalent to VST1q64.
1682 if (Name == "VDUPLNfd" || Name == "VDUPLNfq" ||
1683 Name == "VLD1df" || Name == "VLD1qf" || Name == "VLD2d64" ||
1684 Name == "VST1df" || Name == "VST1qf" || Name == "VST2d64")
1685 return false;
1686 } else if (TN == TARGET_THUMB) {
1687 if (!thumbInstruction(Form))
1688 return false;
1690 // On Darwin R9 is call-clobbered. Ignore the non-Darwin counterparts.
1691 if (Name == "tBL" || Name == "tBLXi" || Name == "tBLXr")
1692 return false;
1694 // Ignore the TPsoft (TLS) instructions, which conflict with tBLr9.
1695 if (Name == "tTPsoft" || Name == "t2TPsoft")
1696 return false;
1698 // Ignore tLEApcrel and tLEApcrelJT, prefer tADDrPCi.
1699 if (Name == "tLEApcrel" || Name == "tLEApcrelJT")
1700 return false;
1702 // Ignore t2LEApcrel, prefer the generic t2ADD* for disassembly printing.
1703 if (Name == "t2LEApcrel")
1704 return false;
1706 // Ignore tADDrSP, tADDspr, and tPICADD, prefer the generic tADDhirr.
1707 // Ignore t2SUBrSPs, prefer the t2SUB[S]r[r|s].
1708 // Ignore t2ADDrSPs, prefer the t2ADD[S]r[r|s].
1709 // Ignore t2ADDrSPi/t2SUBrSPi, which have more generic couterparts.
1710 // Ignore t2ADDrSPi12/t2SUBrSPi12, which have more generic couterparts
1711 if (Name == "tADDrSP" || Name == "tADDspr" || Name == "tPICADD" ||
1712 Name == "t2SUBrSPs" || Name == "t2ADDrSPs" ||
1713 Name == "t2ADDrSPi" || Name == "t2SUBrSPi" ||
1714 Name == "t2ADDrSPi12" || Name == "t2SUBrSPi12")
1715 return false;
1717 // Ignore t2LDRDpci, prefer the generic t2LDRDi8, t2LDRD_PRE, t2LDRD_POST.
1718 if (Name == "t2LDRDpci")
1719 return false;
1721 // Ignore t2TBB, t2TBH and prefer the generic t2TBBgen, t2TBHgen.
1722 if (Name == "t2TBB" || Name == "t2TBH")
1723 return false;
1725 // Resolve conflicts:
1727 // tBfar conflicts with tBLr9
1728 // tCMNz conflicts with tCMN (with assembly format strings being equal)
1729 // tPOP_RET/t2LDM_RET conflict with tPOP/t2LDM (ditto)
1730 // tMOVCCi conflicts with tMOVi8
1731 // tMOVCCr conflicts with tMOVgpr2gpr
1732 // tBR_JTr conflicts with tBRIND
1733 // tSpill conflicts with tSTRspi
1734 // tLDRcp conflicts with tLDRspi
1735 // tRestore conflicts with tLDRspi
1736 // t2LEApcrelJT conflicts with t2LEApcrel
1737 // t2MOVCCi16 conflicts with tMOVi16
1738 if (Name == "tBfar" ||
1739 /* Name == "tCMNz" || */ Name == "tCMPzi8" || Name == "tCMPzr" ||
1740 Name == "tCMPzhir" || /* Name == "t2CMNzrr" || Name == "t2CMNzrs" ||
1741 Name == "t2CMNzri" || */ Name == "t2CMPzrr" || Name == "t2CMPzrs" ||
1742 Name == "t2CMPzri" || Name == "tPOP_RET" || Name == "t2LDM_RET" ||
1743 Name == "tMOVCCi" || Name == "tMOVCCr" || Name == "tBR_JTr" ||
1744 Name == "tSpill" || Name == "tLDRcp" || Name == "tRestore" ||
1745 Name == "t2LEApcrelJT" || Name == "t2MOVCCi16")
1746 return false;
1749 // Dumps the instruction encoding format.
1750 switch (TargetName) {
1751 case TARGET_ARM:
1752 case TARGET_THUMB:
1753 DEBUG(errs() << Name << " " << stringForARMFormat((ARMFormat)Form));
1754 break;
1757 DEBUG({
1758 errs() << " ";
1760 // Dumps the instruction encoding bits.
1761 dumpBits(errs(), Bits);
1763 errs() << '\n';
1765 // Dumps the list of operand info.
1766 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1767 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
1768 const std::string &OperandName = Info.Name;
1769 const Record &OperandDef = *Info.Rec;
1771 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
1775 return true;
1778 void ARMDecoderEmitter::ARMDEBackend::populateInstructions() {
1779 getInstructionsByEnumValue(NumberedInstructions);
1781 uint16_t numUIDs = NumberedInstructions.size();
1782 uint16_t uid;
1784 const char *instClass = NULL;
1786 switch (TargetName) {
1787 case TARGET_ARM:
1788 instClass = "InstARM";
1789 break;
1790 default:
1791 assert(0 && "Unreachable code!");
1794 for (uid = 0; uid < numUIDs; uid++) {
1795 // filter out intrinsics
1796 if (!NumberedInstructions[uid]->TheDef->isSubClassOf(instClass))
1797 continue;
1799 if (populateInstruction(*NumberedInstructions[uid], TargetName))
1800 Opcodes.push_back(uid);
1803 // Special handling for the ARM chip, which supports two modes of execution.
1804 // This branch handles the Thumb opcodes.
1805 if (TargetName == TARGET_ARM) {
1806 for (uid = 0; uid < numUIDs; uid++) {
1807 // filter out intrinsics
1808 if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM")
1809 && !NumberedInstructions[uid]->TheDef->isSubClassOf("InstThumb"))
1810 continue;
1812 if (populateInstruction(*NumberedInstructions[uid], TARGET_THUMB))
1813 Opcodes2.push_back(uid);
1818 // Emits disassembler code for instruction decoding. This delegates to the
1819 // FilterChooser instance to do the heavy lifting.
1820 void ARMDecoderEmitter::ARMDEBackend::emit(raw_ostream &o) {
1821 switch (TargetName) {
1822 case TARGET_ARM:
1823 Frontend.EmitSourceFileHeader("ARM/Thumb Decoders", o);
1824 break;
1825 default:
1826 assert(0 && "Unreachable code!");
1829 o << "#include \"llvm/System/DataTypes.h\"\n";
1830 o << "#include <assert.h>\n";
1831 o << '\n';
1832 o << "namespace llvm {\n\n";
1834 FilterChooser::setTargetName(TargetName);
1836 switch (TargetName) {
1837 case TARGET_ARM: {
1838 // Emit common utility and ARM ISA decoder.
1839 FC = new FilterChooser(NumberedInstructions, Opcodes);
1840 // Reset indentation level.
1841 unsigned Indentation = 0;
1842 FC->emitTop(o, Indentation);
1843 delete FC;
1845 // Emit Thumb ISA decoder as well.
1846 FilterChooser::setTargetName(TARGET_THUMB);
1847 FC = new FilterChooser(NumberedInstructions, Opcodes2);
1848 // Reset indentation level.
1849 Indentation = 0;
1850 FC->emitBot(o, Indentation);
1851 break;
1853 default:
1854 assert(0 && "Unreachable code!");
1857 o << "\n} // End llvm namespace \n";
1860 /////////////////////////
1861 // Backend interface //
1862 /////////////////////////
1864 void ARMDecoderEmitter::initBackend()
1866 Backend = new ARMDEBackend(*this);
1869 void ARMDecoderEmitter::run(raw_ostream &o)
1871 Backend->emit(o);
1874 void ARMDecoderEmitter::shutdownBackend()
1876 delete Backend;
1877 Backend = NULL;