Silence -Wunused-variable in release builds.
[llvm/stm8.git] / utils / TableGen / ARMDecoderEmitter.cpp
blob8a5dc8ba1543816e73506f2a34b7d5b6a5d5e80a
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 ARMFilterChooser;
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 ARMFilter {
266 protected:
267 ARMFilterChooser *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, ARMFilterChooser*> 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 ARMFilterChooser &getVariableFC() {
300 assert(NumFiltered == 1);
301 assert(FilterChooserMap.size() == 1);
302 return *(FilterChooserMap.find((unsigned)-1)->second);
305 ARMFilter(const ARMFilter &f);
306 ARMFilter(ARMFilterChooser &owner, unsigned startBit, unsigned numBits,
307 bool mixed);
309 ~ARMFilter();
311 // Divides the decoding task into sub tasks and delegates them to the
312 // inferior FilterChooser's.
314 // A special case arises when there's only one entry in the filtered
315 // instructions. In order to unambiguously decode the singleton, we need to
316 // match the remaining undecoded encoding bits against the singleton.
317 void recurse();
319 // Emit code to decode instructions given a segment or segments of bits.
320 void emit(raw_ostream &o, unsigned &Indentation);
322 // Returns the number of fanout produced by the filter. More fanout implies
323 // the filter distinguishes more categories of instructions.
324 unsigned usefulness() const;
325 }; // End of class Filter
327 // These are states of our finite state machines used in FilterChooser's
328 // filterProcessor() which produces the filter candidates to use.
329 typedef enum {
330 ATTR_NONE,
331 ATTR_FILTERED,
332 ATTR_ALL_SET,
333 ATTR_ALL_UNSET,
334 ATTR_MIXED
335 } bitAttr_t;
337 /// ARMFilterChooser - FilterChooser chooses the best filter among a set of Filters
338 /// in order to perform the decoding of instructions at the current level.
340 /// Decoding proceeds from the top down. Based on the well-known encoding bits
341 /// of instructions available, FilterChooser builds up the possible Filters that
342 /// can further the task of decoding by distinguishing among the remaining
343 /// candidate instructions.
345 /// Once a filter has been chosen, it is called upon to divide the decoding task
346 /// into sub-tasks and delegates them to its inferior FilterChoosers for further
347 /// processings.
349 /// It is useful to think of a Filter as governing the switch stmts of the
350 /// decoding tree. And each case is delegated to an inferior FilterChooser to
351 /// decide what further remaining bits to look at.
352 class ARMFilterChooser {
353 static TARGET_NAME_t TargetName;
355 protected:
356 friend class ARMFilter;
358 // Vector of codegen instructions to choose our filter.
359 const std::vector<const CodeGenInstruction*> &AllInstructions;
361 // Vector of uid's for this filter chooser to work on.
362 const std::vector<unsigned> Opcodes;
364 // Vector of candidate filters.
365 std::vector<ARMFilter> Filters;
367 // Array of bit values passed down from our parent.
368 // Set to all BIT_UNFILTERED's for Parent == NULL.
369 bit_value_t FilterBitValues[BIT_WIDTH];
371 // Links to the FilterChooser above us in the decoding tree.
372 ARMFilterChooser *Parent;
374 // Index of the best filter from Filters.
375 int BestIndex;
377 public:
378 static void setTargetName(TARGET_NAME_t tn) { TargetName = tn; }
380 ARMFilterChooser(const ARMFilterChooser &FC) :
381 AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
382 Filters(FC.Filters), Parent(FC.Parent), BestIndex(FC.BestIndex) {
383 memcpy(FilterBitValues, FC.FilterBitValues, sizeof(FilterBitValues));
386 ARMFilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
387 const std::vector<unsigned> &IDs) :
388 AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(NULL),
389 BestIndex(-1) {
390 for (unsigned i = 0; i < BIT_WIDTH; ++i)
391 FilterBitValues[i] = BIT_UNFILTERED;
393 doFilter();
396 ARMFilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
397 const std::vector<unsigned> &IDs,
398 bit_value_t (&ParentFilterBitValues)[BIT_WIDTH],
399 ARMFilterChooser &parent) :
400 AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(&parent),
401 BestIndex(-1) {
402 for (unsigned i = 0; i < BIT_WIDTH; ++i)
403 FilterBitValues[i] = ParentFilterBitValues[i];
405 doFilter();
408 // The top level filter chooser has NULL as its parent.
409 bool isTopLevel() { return Parent == NULL; }
411 // This provides an opportunity for target specific code emission.
412 void emitTopHook(raw_ostream &o);
414 // Emit the top level typedef and decodeInstruction() function.
415 void emitTop(raw_ostream &o, unsigned &Indentation);
417 // This provides an opportunity for target specific code emission after
418 // emitTop().
419 void emitBot(raw_ostream &o, unsigned &Indentation);
421 protected:
422 // Populates the insn given the uid.
423 void insnWithID(insn_t &Insn, unsigned Opcode) const {
424 if (AllInstructions[Opcode]->isPseudo)
425 return;
427 BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
429 for (unsigned i = 0; i < BIT_WIDTH; ++i)
430 Insn[i] = bitFromBits(Bits, i);
432 // Set Inst{21} to 1 (wback) when IndexModeBits == IndexModeUpd.
433 Record *R = AllInstructions[Opcode]->TheDef;
434 if (R->getValue("IndexModeBits") &&
435 getByteField(*R, "IndexModeBits") == IndexModeUpd)
436 Insn[21] = BIT_TRUE;
439 // Returns the record name.
440 const std::string &nameWithID(unsigned Opcode) const {
441 return AllInstructions[Opcode]->TheDef->getName();
444 // Populates the field of the insn given the start position and the number of
445 // consecutive bits to scan for.
447 // Returns false if there exists any uninitialized bit value in the range.
448 // Returns true, otherwise.
449 bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
450 unsigned NumBits) const;
452 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
453 /// filter array as a series of chars.
454 void dumpFilterArray(raw_ostream &o, bit_value_t (&filter)[BIT_WIDTH]);
456 /// dumpStack - dumpStack traverses the filter chooser chain and calls
457 /// dumpFilterArray on each filter chooser up to the top level one.
458 void dumpStack(raw_ostream &o, const char *prefix);
460 ARMFilter &bestFilter() {
461 assert(BestIndex != -1 && "BestIndex not set");
462 return Filters[BestIndex];
465 // Called from Filter::recurse() when singleton exists. For debug purpose.
466 void SingletonExists(unsigned Opc);
468 bool PositionFiltered(unsigned i) {
469 return ValueSet(FilterBitValues[i]);
472 // Calculates the island(s) needed to decode the instruction.
473 // This returns a lit of undecoded bits of an instructions, for example,
474 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
475 // decoded bits in order to verify that the instruction matches the Opcode.
476 unsigned getIslands(std::vector<unsigned> &StartBits,
477 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
478 insn_t &Insn);
480 // The purpose of this function is for the API client to detect possible
481 // Load/Store Coprocessor instructions. If the coprocessor number is of
482 // the instruction is either 10 or 11, the decoder should not report the
483 // instruction as LDC/LDC2/STC/STC2, but should match against Advanced SIMD or
484 // VFP instructions.
485 bool LdStCopEncoding1(unsigned Opc) {
486 const std::string &Name = nameWithID(Opc);
487 if (Name == "LDC_OFFSET" || Name == "LDC_OPTION" ||
488 Name == "LDC_POST" || Name == "LDC_PRE" ||
489 Name == "LDCL_OFFSET" || Name == "LDCL_OPTION" ||
490 Name == "LDCL_POST" || Name == "LDCL_PRE" ||
491 Name == "STC_OFFSET" || Name == "STC_OPTION" ||
492 Name == "STC_POST" || Name == "STC_PRE" ||
493 Name == "STCL_OFFSET" || Name == "STCL_OPTION" ||
494 Name == "STCL_POST" || Name == "STCL_PRE")
495 return true;
496 else
497 return false;
500 // Emits code to decode the singleton. Return true if we have matched all the
501 // well-known bits.
502 bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc);
504 // Emits code to decode the singleton, and then to decode the rest.
505 void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
506 ARMFilter &Best);
508 // Assign a single filter and run with it.
509 void runSingleFilter(ARMFilterChooser &owner, unsigned startBit,
510 unsigned numBit, bool mixed);
512 // reportRegion is a helper function for filterProcessor to mark a region as
513 // eligible for use as a filter region.
514 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
515 bool AllowMixed);
517 // FilterProcessor scans the well-known encoding bits of the instructions and
518 // builds up a list of candidate filters. It chooses the best filter and
519 // recursively descends down the decoding tree.
520 bool filterProcessor(bool AllowMixed, bool Greedy = true);
522 // Decides on the best configuration of filter(s) to use in order to decode
523 // the instructions. A conflict of instructions may occur, in which case we
524 // dump the conflict set to the standard error.
525 void doFilter();
527 // Emits code to decode our share of instructions. Returns true if the
528 // emitted code causes a return, which occurs if we know how to decode
529 // the instruction at this level or the instruction is not decodeable.
530 bool emit(raw_ostream &o, unsigned &Indentation);
533 ///////////////////////////
534 // //
535 // Filter Implmenetation //
536 // //
537 ///////////////////////////
539 ARMFilter::ARMFilter(const ARMFilter &f) :
540 Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
541 FilteredInstructions(f.FilteredInstructions),
542 VariableInstructions(f.VariableInstructions),
543 FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered),
544 LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) {
547 ARMFilter::ARMFilter(ARMFilterChooser &owner, unsigned startBit, unsigned numBits,
548 bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits),
549 Mixed(mixed) {
550 assert(StartBit + NumBits - 1 < BIT_WIDTH);
552 NumFiltered = 0;
553 LastOpcFiltered = 0;
554 NumVariable = 0;
556 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
557 insn_t Insn;
559 // Populates the insn given the uid.
560 Owner->insnWithID(Insn, Owner->Opcodes[i]);
562 uint64_t Field;
563 // Scans the segment for possibly well-specified encoding bits.
564 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
566 if (ok) {
567 // The encoding bits are well-known. Lets add the uid of the
568 // instruction into the bucket keyed off the constant field value.
569 LastOpcFiltered = Owner->Opcodes[i];
570 FilteredInstructions[Field].push_back(LastOpcFiltered);
571 ++NumFiltered;
572 } else {
573 // Some of the encoding bit(s) are unspecfied. This contributes to
574 // one additional member of "Variable" instructions.
575 VariableInstructions.push_back(Owner->Opcodes[i]);
576 ++NumVariable;
580 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
581 && "Filter returns no instruction categories");
584 ARMFilter::~ARMFilter() {
585 std::map<unsigned, ARMFilterChooser*>::iterator filterIterator;
586 for (filterIterator = FilterChooserMap.begin();
587 filterIterator != FilterChooserMap.end();
588 filterIterator++) {
589 delete filterIterator->second;
593 // Divides the decoding task into sub tasks and delegates them to the
594 // inferior FilterChooser's.
596 // A special case arises when there's only one entry in the filtered
597 // instructions. In order to unambiguously decode the singleton, we need to
598 // match the remaining undecoded encoding bits against the singleton.
599 void ARMFilter::recurse() {
600 std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator;
602 bit_value_t BitValueArray[BIT_WIDTH];
603 // Starts by inheriting our parent filter chooser's filter bit values.
604 memcpy(BitValueArray, Owner->FilterBitValues, sizeof(BitValueArray));
606 unsigned bitIndex;
608 if (VariableInstructions.size()) {
609 // Conservatively marks each segment position as BIT_UNSET.
610 for (bitIndex = 0; bitIndex < NumBits; bitIndex++)
611 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
613 // Delegates to an inferior filter chooser for further processing on this
614 // group of instructions whose segment values are variable.
615 FilterChooserMap.insert(std::pair<unsigned, ARMFilterChooser*>(
616 (unsigned)-1,
617 new ARMFilterChooser(Owner->AllInstructions,
618 VariableInstructions,
619 BitValueArray,
620 *Owner)
624 // No need to recurse for a singleton filtered instruction.
625 // See also Filter::emit().
626 if (getNumFiltered() == 1) {
627 //Owner->SingletonExists(LastOpcFiltered);
628 assert(FilterChooserMap.size() == 1);
629 return;
632 // Otherwise, create sub choosers.
633 for (mapIterator = FilteredInstructions.begin();
634 mapIterator != FilteredInstructions.end();
635 mapIterator++) {
637 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
638 for (bitIndex = 0; bitIndex < NumBits; bitIndex++) {
639 if (mapIterator->first & (1ULL << bitIndex))
640 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
641 else
642 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
645 // Delegates to an inferior filter chooser for further processing on this
646 // category of instructions.
647 FilterChooserMap.insert(std::pair<unsigned, ARMFilterChooser*>(
648 mapIterator->first,
649 new ARMFilterChooser(Owner->AllInstructions,
650 mapIterator->second,
651 BitValueArray,
652 *Owner)
657 // Emit code to decode instructions given a segment or segments of bits.
658 void ARMFilter::emit(raw_ostream &o, unsigned &Indentation) {
659 o.indent(Indentation) << "// Check Inst{";
661 if (NumBits > 1)
662 o << (StartBit + NumBits - 1) << '-';
664 o << StartBit << "} ...\n";
666 o.indent(Indentation) << "switch (fieldFromInstruction(insn, "
667 << StartBit << ", " << NumBits << ")) {\n";
669 std::map<unsigned, ARMFilterChooser*>::iterator filterIterator;
671 bool DefaultCase = false;
672 for (filterIterator = FilterChooserMap.begin();
673 filterIterator != FilterChooserMap.end();
674 filterIterator++) {
676 // Field value -1 implies a non-empty set of variable instructions.
677 // See also recurse().
678 if (filterIterator->first == (unsigned)-1) {
679 DefaultCase = true;
681 o.indent(Indentation) << "default:\n";
682 o.indent(Indentation) << " break; // fallthrough\n";
684 // Closing curly brace for the switch statement.
685 // This is unconventional because we want the default processing to be
686 // performed for the fallthrough cases as well, i.e., when the "cases"
687 // did not prove a decoded instruction.
688 o.indent(Indentation) << "}\n";
690 } else
691 o.indent(Indentation) << "case " << filterIterator->first << ":\n";
693 // We arrive at a category of instructions with the same segment value.
694 // Now delegate to the sub filter chooser for further decodings.
695 // The case may fallthrough, which happens if the remaining well-known
696 // encoding bits do not match exactly.
697 if (!DefaultCase) { ++Indentation; ++Indentation; }
699 bool finished = filterIterator->second->emit(o, Indentation);
700 // For top level default case, there's no need for a break statement.
701 if (Owner->isTopLevel() && DefaultCase)
702 break;
703 if (!finished)
704 o.indent(Indentation) << "break;\n";
706 if (!DefaultCase) { --Indentation; --Indentation; }
709 // If there is no default case, we still need to supply a closing brace.
710 if (!DefaultCase) {
711 // Closing curly brace for the switch statement.
712 o.indent(Indentation) << "}\n";
716 // Returns the number of fanout produced by the filter. More fanout implies
717 // the filter distinguishes more categories of instructions.
718 unsigned ARMFilter::usefulness() const {
719 if (VariableInstructions.size())
720 return FilteredInstructions.size();
721 else
722 return FilteredInstructions.size() + 1;
725 //////////////////////////////////
726 // //
727 // Filterchooser Implementation //
728 // //
729 //////////////////////////////////
731 // Define the symbol here.
732 TARGET_NAME_t ARMFilterChooser::TargetName;
734 // This provides an opportunity for target specific code emission.
735 void ARMFilterChooser::emitTopHook(raw_ostream &o) {
736 if (TargetName == TARGET_ARM) {
737 // Emit code that references the ARMFormat data type.
738 o << "static const ARMFormat ARMFormats[] = {\n";
739 for (unsigned i = 0, e = AllInstructions.size(); i != e; ++i) {
740 const Record &Def = *(AllInstructions[i]->TheDef);
741 const std::string &Name = Def.getName();
742 if (Def.isSubClassOf("InstARM") || Def.isSubClassOf("InstThumb"))
743 o.indent(2) <<
744 stringForARMFormat((ARMFormat)getByteField(Def, "Form"));
745 else
746 o << " ARM_FORMAT_NA";
748 o << ",\t// Inst #" << i << " = " << Name << '\n';
750 o << " ARM_FORMAT_NA\t// Unreachable.\n";
751 o << "};\n\n";
755 // Emit the top level typedef and decodeInstruction() function.
756 void ARMFilterChooser::emitTop(raw_ostream &o, unsigned &Indentation) {
757 // Run the target specific emit hook.
758 emitTopHook(o);
760 switch (BIT_WIDTH) {
761 case 8:
762 o.indent(Indentation) << "typedef uint8_t field_t;\n";
763 break;
764 case 16:
765 o.indent(Indentation) << "typedef uint16_t field_t;\n";
766 break;
767 case 32:
768 o.indent(Indentation) << "typedef uint32_t field_t;\n";
769 break;
770 case 64:
771 o.indent(Indentation) << "typedef uint64_t field_t;\n";
772 break;
773 default:
774 assert(0 && "Unexpected instruction size!");
777 o << '\n';
779 o.indent(Indentation) << "static field_t " <<
780 "fieldFromInstruction(field_t insn, unsigned startBit, unsigned numBits)\n";
782 o.indent(Indentation) << "{\n";
784 ++Indentation; ++Indentation;
785 o.indent(Indentation) << "assert(startBit + numBits <= " << BIT_WIDTH
786 << " && \"Instruction field out of bounds!\");\n";
787 o << '\n';
788 o.indent(Indentation) << "field_t fieldMask;\n";
789 o << '\n';
790 o.indent(Indentation) << "if (numBits == " << BIT_WIDTH << ")\n";
792 ++Indentation; ++Indentation;
793 o.indent(Indentation) << "fieldMask = (field_t)-1;\n";
794 --Indentation; --Indentation;
796 o.indent(Indentation) << "else\n";
798 ++Indentation; ++Indentation;
799 o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n";
800 --Indentation; --Indentation;
802 o << '\n';
803 o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n";
804 --Indentation; --Indentation;
806 o.indent(Indentation) << "}\n";
808 o << '\n';
810 o.indent(Indentation) <<"static uint16_t decodeInstruction(field_t insn) {\n";
812 ++Indentation; ++Indentation;
813 // Emits code to decode the instructions.
814 emit(o, Indentation);
816 o << '\n';
817 o.indent(Indentation) << "return 0;\n";
818 --Indentation; --Indentation;
820 o.indent(Indentation) << "}\n";
822 o << '\n';
825 // This provides an opportunity for target specific code emission after
826 // emitTop().
827 void ARMFilterChooser::emitBot(raw_ostream &o, unsigned &Indentation) {
828 if (TargetName != TARGET_THUMB) return;
830 // Emit code that decodes the Thumb ISA.
831 o.indent(Indentation)
832 << "static uint16_t decodeThumbInstruction(field_t insn) {\n";
834 ++Indentation; ++Indentation;
836 // Emits code to decode the instructions.
837 emit(o, Indentation);
839 o << '\n';
840 o.indent(Indentation) << "return 0;\n";
842 --Indentation; --Indentation;
844 o.indent(Indentation) << "}\n";
847 // Populates the field of the insn given the start position and the number of
848 // consecutive bits to scan for.
850 // Returns false if and on the first uninitialized bit value encountered.
851 // Returns true, otherwise.
852 bool ARMFilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
853 unsigned StartBit, unsigned NumBits) const {
854 Field = 0;
856 for (unsigned i = 0; i < NumBits; ++i) {
857 if (Insn[StartBit + i] == BIT_UNSET)
858 return false;
860 if (Insn[StartBit + i] == BIT_TRUE)
861 Field = Field | (1ULL << i);
864 return true;
867 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
868 /// filter array as a series of chars.
869 void ARMFilterChooser::dumpFilterArray(raw_ostream &o,
870 bit_value_t (&filter)[BIT_WIDTH]) {
871 unsigned bitIndex;
873 for (bitIndex = BIT_WIDTH; bitIndex > 0; bitIndex--) {
874 switch (filter[bitIndex - 1]) {
875 case BIT_UNFILTERED:
876 o << ".";
877 break;
878 case BIT_UNSET:
879 o << "_";
880 break;
881 case BIT_TRUE:
882 o << "1";
883 break;
884 case BIT_FALSE:
885 o << "0";
886 break;
891 /// dumpStack - dumpStack traverses the filter chooser chain and calls
892 /// dumpFilterArray on each filter chooser up to the top level one.
893 void ARMFilterChooser::dumpStack(raw_ostream &o, const char *prefix) {
894 ARMFilterChooser *current = this;
896 while (current) {
897 o << prefix;
898 dumpFilterArray(o, current->FilterBitValues);
899 o << '\n';
900 current = current->Parent;
904 // Called from Filter::recurse() when singleton exists. For debug purpose.
905 void ARMFilterChooser::SingletonExists(unsigned Opc) {
906 insn_t Insn0;
907 insnWithID(Insn0, Opc);
909 errs() << "Singleton exists: " << nameWithID(Opc)
910 << " with its decoding dominating ";
911 for (unsigned i = 0; i < Opcodes.size(); ++i) {
912 if (Opcodes[i] == Opc) continue;
913 errs() << nameWithID(Opcodes[i]) << ' ';
915 errs() << '\n';
917 dumpStack(errs(), "\t\t");
918 for (unsigned i = 0; i < Opcodes.size(); i++) {
919 const std::string &Name = nameWithID(Opcodes[i]);
921 errs() << '\t' << Name << " ";
922 dumpBits(errs(),
923 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
924 errs() << '\n';
928 // Calculates the island(s) needed to decode the instruction.
929 // This returns a list of undecoded bits of an instructions, for example,
930 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
931 // decoded bits in order to verify that the instruction matches the Opcode.
932 unsigned ARMFilterChooser::getIslands(std::vector<unsigned> &StartBits,
933 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
934 insn_t &Insn) {
935 unsigned Num, BitNo;
936 Num = BitNo = 0;
938 uint64_t FieldVal = 0;
940 // 0: Init
941 // 1: Water (the bit value does not affect decoding)
942 // 2: Island (well-known bit value needed for decoding)
943 int State = 0;
944 int Val = -1;
946 for (unsigned i = 0; i < BIT_WIDTH; ++i) {
947 Val = Value(Insn[i]);
948 bool Filtered = PositionFiltered(i);
949 switch (State) {
950 default:
951 assert(0 && "Unreachable code!");
952 break;
953 case 0:
954 case 1:
955 if (Filtered || Val == -1)
956 State = 1; // Still in Water
957 else {
958 State = 2; // Into the Island
959 BitNo = 0;
960 StartBits.push_back(i);
961 FieldVal = Val;
963 break;
964 case 2:
965 if (Filtered || Val == -1) {
966 State = 1; // Into the Water
967 EndBits.push_back(i - 1);
968 FieldVals.push_back(FieldVal);
969 ++Num;
970 } else {
971 State = 2; // Still in Island
972 ++BitNo;
973 FieldVal = FieldVal | Val << BitNo;
975 break;
978 // If we are still in Island after the loop, do some housekeeping.
979 if (State == 2) {
980 EndBits.push_back(BIT_WIDTH - 1);
981 FieldVals.push_back(FieldVal);
982 ++Num;
985 assert(StartBits.size() == Num && EndBits.size() == Num &&
986 FieldVals.size() == Num);
987 return Num;
990 // Emits code to decode the singleton. Return true if we have matched all the
991 // well-known bits.
992 bool ARMFilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
993 unsigned Opc) {
994 std::vector<unsigned> StartBits;
995 std::vector<unsigned> EndBits;
996 std::vector<uint64_t> FieldVals;
997 insn_t Insn;
998 insnWithID(Insn, Opc);
1000 // This provides a good opportunity to check for possible Ld/St Coprocessor
1001 // Opcode and escapes if the coproc # is either 10 or 11. It is a NEON/VFP
1002 // instruction is disguise.
1003 if (TargetName == TARGET_ARM && LdStCopEncoding1(Opc)) {
1004 o.indent(Indentation);
1005 // A8.6.51 & A8.6.188
1006 // If coproc = 0b101?, i.e, slice(insn, 11, 8) = 10 or 11, escape.
1007 o << "if (fieldFromInstruction(insn, 9, 3) == 5) break; // fallthrough\n";
1010 // Look for islands of undecoded bits of the singleton.
1011 getIslands(StartBits, EndBits, FieldVals, Insn);
1013 unsigned Size = StartBits.size();
1014 unsigned I, NumBits;
1016 // If we have matched all the well-known bits, just issue a return.
1017 if (Size == 0) {
1018 o.indent(Indentation) << "return " << Opc << "; // " << nameWithID(Opc)
1019 << '\n';
1020 return true;
1023 // Otherwise, there are more decodings to be done!
1025 // Emit code to match the island(s) for the singleton.
1026 o.indent(Indentation) << "// Check ";
1028 for (I = Size; I != 0; --I) {
1029 o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} ";
1030 if (I > 1)
1031 o << "&& ";
1032 else
1033 o << "for singleton decoding...\n";
1036 o.indent(Indentation) << "if (";
1038 for (I = Size; I != 0; --I) {
1039 NumBits = EndBits[I-1] - StartBits[I-1] + 1;
1040 o << "fieldFromInstruction(insn, " << StartBits[I-1] << ", " << NumBits
1041 << ") == " << FieldVals[I-1];
1042 if (I > 1)
1043 o << " && ";
1044 else
1045 o << ")\n";
1048 o.indent(Indentation) << " return " << Opc << "; // " << nameWithID(Opc)
1049 << '\n';
1051 return false;
1054 // Emits code to decode the singleton, and then to decode the rest.
1055 void ARMFilterChooser::emitSingletonDecoder(raw_ostream &o,
1056 unsigned &Indentation,
1057 ARMFilter &Best) {
1059 unsigned Opc = Best.getSingletonOpc();
1061 emitSingletonDecoder(o, Indentation, Opc);
1063 // Emit code for the rest.
1064 o.indent(Indentation) << "else\n";
1066 Indentation += 2;
1067 Best.getVariableFC().emit(o, Indentation);
1068 Indentation -= 2;
1071 // Assign a single filter and run with it. Top level API client can initialize
1072 // with a single filter to start the filtering process.
1073 void ARMFilterChooser::runSingleFilter(ARMFilterChooser &owner,
1074 unsigned startBit,
1075 unsigned numBit, bool mixed) {
1076 Filters.clear();
1077 ARMFilter F(*this, startBit, numBit, true);
1078 Filters.push_back(F);
1079 BestIndex = 0; // Sole Filter instance to choose from.
1080 bestFilter().recurse();
1083 // reportRegion is a helper function for filterProcessor to mark a region as
1084 // eligible for use as a filter region.
1085 void ARMFilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1086 unsigned BitIndex, bool AllowMixed) {
1087 if (RA == ATTR_MIXED && AllowMixed)
1088 Filters.push_back(ARMFilter(*this, StartBit, BitIndex - StartBit, true));
1089 else if (RA == ATTR_ALL_SET && !AllowMixed)
1090 Filters.push_back(ARMFilter(*this, StartBit, BitIndex - StartBit, false));
1093 // FilterProcessor scans the well-known encoding bits of the instructions and
1094 // builds up a list of candidate filters. It chooses the best filter and
1095 // recursively descends down the decoding tree.
1096 bool ARMFilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1097 Filters.clear();
1098 BestIndex = -1;
1099 unsigned numInstructions = Opcodes.size();
1101 assert(numInstructions && "Filter created with no instructions");
1103 // No further filtering is necessary.
1104 if (numInstructions == 1)
1105 return true;
1107 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1108 // instructions is 3.
1109 if (AllowMixed && !Greedy) {
1110 assert(numInstructions == 3);
1112 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1113 std::vector<unsigned> StartBits;
1114 std::vector<unsigned> EndBits;
1115 std::vector<uint64_t> FieldVals;
1116 insn_t Insn;
1118 insnWithID(Insn, Opcodes[i]);
1120 // Look for islands of undecoded bits of any instruction.
1121 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1122 // Found an instruction with island(s). Now just assign a filter.
1123 runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1,
1124 true);
1125 return true;
1130 unsigned BitIndex, InsnIndex;
1132 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1133 // The automaton consumes the corresponding bit from each
1134 // instruction.
1136 // Input symbols: 0, 1, and _ (unset).
1137 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1138 // Initial state: NONE.
1140 // (NONE) ------- [01] -> (ALL_SET)
1141 // (NONE) ------- _ ----> (ALL_UNSET)
1142 // (ALL_SET) ---- [01] -> (ALL_SET)
1143 // (ALL_SET) ---- _ ----> (MIXED)
1144 // (ALL_UNSET) -- [01] -> (MIXED)
1145 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1146 // (MIXED) ------ . ----> (MIXED)
1147 // (FILTERED)---- . ----> (FILTERED)
1149 bitAttr_t bitAttrs[BIT_WIDTH];
1151 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1152 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1153 for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex)
1154 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1155 FilterBitValues[BitIndex] == BIT_FALSE)
1156 bitAttrs[BitIndex] = ATTR_FILTERED;
1157 else
1158 bitAttrs[BitIndex] = ATTR_NONE;
1160 for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
1161 insn_t insn;
1163 insnWithID(insn, Opcodes[InsnIndex]);
1165 for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex) {
1166 switch (bitAttrs[BitIndex]) {
1167 case ATTR_NONE:
1168 if (insn[BitIndex] == BIT_UNSET)
1169 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1170 else
1171 bitAttrs[BitIndex] = ATTR_ALL_SET;
1172 break;
1173 case ATTR_ALL_SET:
1174 if (insn[BitIndex] == BIT_UNSET)
1175 bitAttrs[BitIndex] = ATTR_MIXED;
1176 break;
1177 case ATTR_ALL_UNSET:
1178 if (insn[BitIndex] != BIT_UNSET)
1179 bitAttrs[BitIndex] = ATTR_MIXED;
1180 break;
1181 case ATTR_MIXED:
1182 case ATTR_FILTERED:
1183 break;
1188 // The regionAttr automaton consumes the bitAttrs automatons' state,
1189 // lowest-to-highest.
1191 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1192 // States: NONE, ALL_SET, MIXED
1193 // Initial state: NONE
1195 // (NONE) ----- F --> (NONE)
1196 // (NONE) ----- S --> (ALL_SET) ; and set region start
1197 // (NONE) ----- U --> (NONE)
1198 // (NONE) ----- M --> (MIXED) ; and set region start
1199 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1200 // (ALL_SET) -- S --> (ALL_SET)
1201 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1202 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1203 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1204 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1205 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1206 // (MIXED) ---- M --> (MIXED)
1208 bitAttr_t RA = ATTR_NONE;
1209 unsigned StartBit = 0;
1211 for (BitIndex = 0; BitIndex < BIT_WIDTH; BitIndex++) {
1212 bitAttr_t bitAttr = bitAttrs[BitIndex];
1214 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1216 switch (RA) {
1217 case ATTR_NONE:
1218 switch (bitAttr) {
1219 case ATTR_FILTERED:
1220 break;
1221 case ATTR_ALL_SET:
1222 StartBit = BitIndex;
1223 RA = ATTR_ALL_SET;
1224 break;
1225 case ATTR_ALL_UNSET:
1226 break;
1227 case ATTR_MIXED:
1228 StartBit = BitIndex;
1229 RA = ATTR_MIXED;
1230 break;
1231 default:
1232 assert(0 && "Unexpected bitAttr!");
1234 break;
1235 case ATTR_ALL_SET:
1236 switch (bitAttr) {
1237 case ATTR_FILTERED:
1238 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1239 RA = ATTR_NONE;
1240 break;
1241 case ATTR_ALL_SET:
1242 break;
1243 case ATTR_ALL_UNSET:
1244 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1245 RA = ATTR_NONE;
1246 break;
1247 case ATTR_MIXED:
1248 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1249 StartBit = BitIndex;
1250 RA = ATTR_MIXED;
1251 break;
1252 default:
1253 assert(0 && "Unexpected bitAttr!");
1255 break;
1256 case ATTR_MIXED:
1257 switch (bitAttr) {
1258 case ATTR_FILTERED:
1259 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1260 StartBit = BitIndex;
1261 RA = ATTR_NONE;
1262 break;
1263 case ATTR_ALL_SET:
1264 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1265 StartBit = BitIndex;
1266 RA = ATTR_ALL_SET;
1267 break;
1268 case ATTR_ALL_UNSET:
1269 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1270 RA = ATTR_NONE;
1271 break;
1272 case ATTR_MIXED:
1273 break;
1274 default:
1275 assert(0 && "Unexpected bitAttr!");
1277 break;
1278 case ATTR_ALL_UNSET:
1279 assert(0 && "regionAttr state machine has no ATTR_UNSET state");
1280 case ATTR_FILTERED:
1281 assert(0 && "regionAttr state machine has no ATTR_FILTERED state");
1285 // At the end, if we're still in ALL_SET or MIXED states, report a region
1286 switch (RA) {
1287 case ATTR_NONE:
1288 break;
1289 case ATTR_FILTERED:
1290 break;
1291 case ATTR_ALL_SET:
1292 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1293 break;
1294 case ATTR_ALL_UNSET:
1295 break;
1296 case ATTR_MIXED:
1297 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1298 break;
1301 // We have finished with the filter processings. Now it's time to choose
1302 // the best performing filter.
1303 BestIndex = 0;
1304 bool AllUseless = true;
1305 unsigned BestScore = 0;
1307 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1308 unsigned Usefulness = Filters[i].usefulness();
1310 if (Usefulness)
1311 AllUseless = false;
1313 if (Usefulness > BestScore) {
1314 BestIndex = i;
1315 BestScore = Usefulness;
1319 if (!AllUseless)
1320 bestFilter().recurse();
1322 return !AllUseless;
1323 } // end of FilterChooser::filterProcessor(bool)
1325 // Decides on the best configuration of filter(s) to use in order to decode
1326 // the instructions. A conflict of instructions may occur, in which case we
1327 // dump the conflict set to the standard error.
1328 void ARMFilterChooser::doFilter() {
1329 unsigned Num = Opcodes.size();
1330 assert(Num && "FilterChooser created with no instructions");
1332 // Heuristics: Use Inst{31-28} as the top level filter for ARM ISA.
1333 if (TargetName == TARGET_ARM && Parent == NULL) {
1334 runSingleFilter(*this, 28, 4, false);
1335 return;
1338 // Try regions of consecutive known bit values first.
1339 if (filterProcessor(false))
1340 return;
1342 // Then regions of mixed bits (both known and unitialized bit values allowed).
1343 if (filterProcessor(true))
1344 return;
1346 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1347 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1348 // well-known encoding pattern. In such case, we backtrack and scan for the
1349 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1350 if (Num == 3 && filterProcessor(true, false))
1351 return;
1353 // If we come to here, the instruction decoding has failed.
1354 // Set the BestIndex to -1 to indicate so.
1355 BestIndex = -1;
1358 // Emits code to decode our share of instructions. Returns true if the
1359 // emitted code causes a return, which occurs if we know how to decode
1360 // the instruction at this level or the instruction is not decodeable.
1361 bool ARMFilterChooser::emit(raw_ostream &o, unsigned &Indentation) {
1362 if (Opcodes.size() == 1)
1363 // There is only one instruction in the set, which is great!
1364 // Call emitSingletonDecoder() to see whether there are any remaining
1365 // encodings bits.
1366 return emitSingletonDecoder(o, Indentation, Opcodes[0]);
1368 // Choose the best filter to do the decodings!
1369 if (BestIndex != -1) {
1370 ARMFilter &Best = bestFilter();
1371 if (Best.getNumFiltered() == 1)
1372 emitSingletonDecoder(o, Indentation, Best);
1373 else
1374 bestFilter().emit(o, Indentation);
1375 return false;
1378 // If we reach here, there is a conflict in decoding. Let's resolve the known
1379 // conflicts!
1380 if ((TargetName == TARGET_ARM || TargetName == TARGET_THUMB) &&
1381 Opcodes.size() == 2) {
1382 // Resolve the known conflict sets:
1384 // 1. source registers are identical => VMOVDneon; otherwise => VORRd
1385 // 2. source registers are identical => VMOVQ; otherwise => VORRq
1386 // 3. LDR, LDRcp => return LDR for now.
1387 // FIXME: How can we distinguish between LDR and LDRcp? Do we need to?
1388 // 4. tLDMIA, tLDMIA_UPD => Rn = Inst{10-8}, reglist = Inst{7-0},
1389 // wback = registers<Rn> = 0
1390 // NOTE: (tLDM, tLDM_UPD) resolution must come before Advanced SIMD
1391 // addressing mode resolution!!!
1392 // 5. VLD[234]LN*/VST[234]LN* vs. VLD[234]LN*_UPD/VST[234]LN*_UPD conflicts
1393 // are resolved returning the non-UPD versions of the instructions if the
1394 // Rm field, i.e., Inst{3-0} is 0b1111. This is specified in A7.7.1
1395 // Advanced SIMD addressing mode.
1396 const std::string &name1 = nameWithID(Opcodes[0]);
1397 const std::string &name2 = nameWithID(Opcodes[1]);
1398 if ((name1 == "VMOVDneon" && name2 == "VORRd") ||
1399 (name1 == "VMOVQ" && name2 == "VORRq")) {
1400 // Inserting the opening curly brace for this case block.
1401 --Indentation; --Indentation;
1402 o.indent(Indentation) << "{\n";
1403 ++Indentation; ++Indentation;
1405 o.indent(Indentation)
1406 << "field_t N = fieldFromInstruction(insn, 7, 1), "
1407 << "M = fieldFromInstruction(insn, 5, 1);\n";
1408 o.indent(Indentation)
1409 << "field_t Vn = fieldFromInstruction(insn, 16, 4), "
1410 << "Vm = fieldFromInstruction(insn, 0, 4);\n";
1411 o.indent(Indentation)
1412 << "return (N == M && Vn == Vm) ? "
1413 << Opcodes[0] << " /* " << name1 << " */ : "
1414 << Opcodes[1] << " /* " << name2 << " */ ;\n";
1416 // Inserting the closing curly brace for this case block.
1417 --Indentation; --Indentation;
1418 o.indent(Indentation) << "}\n";
1419 ++Indentation; ++Indentation;
1421 return true;
1423 if (name1 == "LDR" && name2 == "LDRcp") {
1424 o.indent(Indentation)
1425 << "return " << Opcodes[0]
1426 << "; // Returning LDR for {LDR, LDRcp}\n";
1427 return true;
1429 if (name1 == "tLDMIA" && name2 == "tLDMIA_UPD") {
1430 // Inserting the opening curly brace for this case block.
1431 --Indentation; --Indentation;
1432 o.indent(Indentation) << "{\n";
1433 ++Indentation; ++Indentation;
1435 o.indent(Indentation)
1436 << "unsigned Rn = fieldFromInstruction(insn, 8, 3), "
1437 << "list = fieldFromInstruction(insn, 0, 8);\n";
1438 o.indent(Indentation)
1439 << "return ((list >> Rn) & 1) == 0 ? "
1440 << Opcodes[1] << " /* " << name2 << " */ : "
1441 << Opcodes[0] << " /* " << name1 << " */ ;\n";
1443 // Inserting the closing curly brace for this case block.
1444 --Indentation; --Indentation;
1445 o.indent(Indentation) << "}\n";
1446 ++Indentation; ++Indentation;
1448 return true;
1450 if (sameStringExceptSuffix(name1, name2, "_UPD")) {
1451 o.indent(Indentation)
1452 << "return fieldFromInstruction(insn, 0, 4) == 15 ? " << Opcodes[0]
1453 << " /* " << name1 << " */ : " << Opcodes[1] << "/* " << name2
1454 << " */ ; // Advanced SIMD addressing mode\n";
1455 return true;
1458 // Otherwise, it does not belong to the known conflict sets.
1461 // We don't know how to decode these instructions! Return 0 and dump the
1462 // conflict set!
1463 o.indent(Indentation) << "return 0;" << " // Conflict set: ";
1464 for (int i = 0, N = Opcodes.size(); i < N; ++i) {
1465 o << nameWithID(Opcodes[i]);
1466 if (i < (N - 1))
1467 o << ", ";
1468 else
1469 o << '\n';
1472 // Print out useful conflict information for postmortem analysis.
1473 errs() << "Decoding Conflict:\n";
1475 dumpStack(errs(), "\t\t");
1477 for (unsigned i = 0; i < Opcodes.size(); i++) {
1478 const std::string &Name = nameWithID(Opcodes[i]);
1480 errs() << '\t' << Name << " ";
1481 dumpBits(errs(),
1482 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1483 errs() << '\n';
1486 return true;
1490 ////////////////////////////////////////////
1491 // //
1492 // ARMDEBackend //
1493 // (Helper class for ARMDecoderEmitter) //
1494 // //
1495 ////////////////////////////////////////////
1497 class ARMDecoderEmitter::ARMDEBackend {
1498 public:
1499 ARMDEBackend(ARMDecoderEmitter &frontend, RecordKeeper &Records) :
1500 NumberedInstructions(),
1501 Opcodes(),
1502 Frontend(frontend),
1503 Target(Records),
1504 FC(NULL)
1506 if (Target.getName() == "ARM")
1507 TargetName = TARGET_ARM;
1508 else {
1509 errs() << "Target name " << Target.getName() << " not recognized\n";
1510 assert(0 && "Unknown target");
1513 // Populate the instructions for our TargetName.
1514 populateInstructions();
1517 ~ARMDEBackend() {
1518 if (FC) {
1519 delete FC;
1520 FC = NULL;
1524 void getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
1525 &NumberedInstructions) {
1526 // We must emit the PHI opcode first...
1527 std::string Namespace = Target.getInstNamespace();
1528 assert(!Namespace.empty() && "No instructions defined.");
1530 NumberedInstructions = Target.getInstructionsByEnumValue();
1533 bool populateInstruction(const CodeGenInstruction &CGI, TARGET_NAME_t TN);
1535 void populateInstructions();
1537 // Emits disassembler code for instruction decoding. This delegates to the
1538 // FilterChooser instance to do the heavy lifting.
1539 void emit(raw_ostream &o);
1541 protected:
1542 std::vector<const CodeGenInstruction*> NumberedInstructions;
1543 std::vector<unsigned> Opcodes;
1544 // Special case for the ARM chip, which supports ARM and Thumb ISAs.
1545 // Opcodes2 will be populated with the Thumb opcodes.
1546 std::vector<unsigned> Opcodes2;
1547 ARMDecoderEmitter &Frontend;
1548 CodeGenTarget Target;
1549 ARMFilterChooser *FC;
1551 TARGET_NAME_t TargetName;
1554 bool ARMDecoderEmitter::
1555 ARMDEBackend::populateInstruction(const CodeGenInstruction &CGI,
1556 TARGET_NAME_t TN) {
1557 const Record &Def = *CGI.TheDef;
1558 const StringRef Name = Def.getName();
1559 uint8_t Form = getByteField(Def, "Form");
1561 BitsInit &Bits = getBitsField(Def, "Inst");
1563 // If all the bit positions are not specified; do not decode this instruction.
1564 // We are bound to fail! For proper disassembly, the well-known encoding bits
1565 // of the instruction must be fully specified.
1567 // This also removes pseudo instructions from considerations of disassembly,
1568 // which is a better design and less fragile than the name matchings.
1569 if (Bits.allInComplete()) return false;
1571 // Ignore "asm parser only" instructions.
1572 if (Def.getValueAsBit("isAsmParserOnly"))
1573 return false;
1575 if (TN == TARGET_ARM) {
1576 if (Form == ARM_FORMAT_PSEUDO)
1577 return false;
1578 if (thumbInstruction(Form))
1579 return false;
1581 // Tail calls are other patterns that generate existing instructions.
1582 if (Name == "TCRETURNdi" || Name == "TCRETURNdiND" ||
1583 Name == "TCRETURNri" || Name == "TCRETURNriND" ||
1584 Name == "TAILJMPd" || Name == "TAILJMPdt" ||
1585 Name == "TAILJMPdND" || Name == "TAILJMPdNDt" ||
1586 Name == "TAILJMPr" || Name == "TAILJMPrND" ||
1587 Name == "MOVr_TC")
1588 return false;
1590 // Delegate ADR disassembly to the more generic ADDri/SUBri instructions.
1591 if (Name == "ADR")
1592 return false;
1595 // The following special cases are for conflict resolutions.
1598 // A8-598: VEXT
1599 // Vector Extract extracts elements from the bottom end of the second
1600 // operand vector and the top end of the first, concatenates them and
1601 // places the result in the destination vector. The elements of the
1602 // vectors are treated as being 8-bit bitfields. There is no distinction
1603 // between data types. The size of the operation can be specified in
1604 // assembler as vext.size. If the value is 16, 32, or 64, the syntax is
1605 // a pseudo-instruction for a VEXT instruction specifying the equivalent
1606 // number of bytes.
1608 // Variants VEXTd16, VEXTd32, VEXTd8, and VEXTdf are reduced to VEXTd8;
1609 // variants VEXTq16, VEXTq32, VEXTq8, and VEXTqf are reduced to VEXTq8.
1610 if (Name == "VEXTd16" || Name == "VEXTd32" || Name == "VEXTdf" ||
1611 Name == "VEXTq16" || Name == "VEXTq32" || Name == "VEXTqf")
1612 return false;
1613 } else if (TN == TARGET_THUMB) {
1614 if (!thumbInstruction(Form))
1615 return false;
1617 // A8.6.189 STM / STMIA / STMEA -- Encoding T1
1618 // There's only STMIA_UPD for Thumb1.
1619 if (Name == "tSTMIA")
1620 return false;
1622 // On Darwin R9 is call-clobbered. Ignore the non-Darwin counterparts.
1623 if (Name == "tBL" || Name == "tBLXi" || Name == "tBLXr")
1624 return false;
1626 // A8.6.25 BX. Use the generic tBX_Rm, ignore tBX_RET and tBX_RET_vararg.
1627 if (Name == "tBX_RET" || Name == "tBX_RET_vararg")
1628 return false;
1630 // Ignore tADR, prefer tADDrPCi.
1631 if (Name == "tADR")
1632 return false;
1634 // Delegate t2ADR disassembly to the more generic t2ADDri12/t2SUBri12
1635 // instructions.
1636 if (Name == "t2ADR")
1637 return false;
1639 // Ignore tADDrSP, tADDspr, and tPICADD, prefer the generic tADDhirr.
1640 // Ignore t2SUBrSPs, prefer the t2SUB[S]r[r|s].
1641 // Ignore t2ADDrSPs, prefer the t2ADD[S]r[r|s].
1642 if (Name == "tADDrSP" || Name == "tADDspr" || Name == "tPICADD" ||
1643 Name == "t2SUBrSPs" || Name == "t2ADDrSPs")
1644 return false;
1646 // FIXME: Use ldr.n to work around a Darwin assembler bug.
1647 // Introduce a workaround with tLDRpciDIS opcode.
1648 if (Name == "tLDRpci")
1649 return false;
1651 // Ignore t2LDRDpci, prefer the generic t2LDRDi8, t2LDRD_PRE, t2LDRD_POST.
1652 if (Name == "t2LDRDpci")
1653 return false;
1655 // Resolve conflicts:
1657 // tBfar conflicts with tBLr9
1658 // t2LDMIA_RET conflict with t2LDM (ditto)
1659 // tMOVCCi conflicts with tMOVi8
1660 // tMOVCCr conflicts with tMOVgpr2gpr
1661 // tLDRcp conflicts with tLDRspi
1662 // t2MOVCCi16 conflicts with tMOVi16
1663 if (Name == "tBfar" ||
1664 Name == "t2LDMIA_RET" ||
1665 Name == "tMOVCCi" || Name == "tMOVCCr" ||
1666 Name == "tLDRcp" ||
1667 Name == "t2MOVCCi16")
1668 return false;
1671 DEBUG({
1672 // Dumps the instruction encoding format.
1673 switch (TargetName) {
1674 case TARGET_ARM:
1675 case TARGET_THUMB:
1676 errs() << Name << " " << stringForARMFormat((ARMFormat)Form);
1677 break;
1680 errs() << " ";
1682 // Dumps the instruction encoding bits.
1683 dumpBits(errs(), Bits);
1685 errs() << '\n';
1687 // Dumps the list of operand info.
1688 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1689 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
1690 const std::string &OperandName = Info.Name;
1691 const Record &OperandDef = *Info.Rec;
1693 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
1697 return true;
1700 void ARMDecoderEmitter::ARMDEBackend::populateInstructions() {
1701 getInstructionsByEnumValue(NumberedInstructions);
1703 unsigned numUIDs = NumberedInstructions.size();
1704 if (TargetName == TARGET_ARM) {
1705 for (unsigned uid = 0; uid < numUIDs; uid++) {
1706 // filter out intrinsics
1707 if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM"))
1708 continue;
1710 if (populateInstruction(*NumberedInstructions[uid], TargetName))
1711 Opcodes.push_back(uid);
1714 // Special handling for the ARM chip, which supports two modes of execution.
1715 // This branch handles the Thumb opcodes.
1716 for (unsigned uid = 0; uid < numUIDs; uid++) {
1717 // filter out intrinsics
1718 if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM")
1719 && !NumberedInstructions[uid]->TheDef->isSubClassOf("InstThumb"))
1720 continue;
1722 if (populateInstruction(*NumberedInstructions[uid], TARGET_THUMB))
1723 Opcodes2.push_back(uid);
1726 return;
1729 // For other targets.
1730 for (unsigned uid = 0; uid < numUIDs; uid++) {
1731 Record *R = NumberedInstructions[uid]->TheDef;
1732 if (R->getValueAsString("Namespace") == "TargetOpcode")
1733 continue;
1735 if (populateInstruction(*NumberedInstructions[uid], TargetName))
1736 Opcodes.push_back(uid);
1740 // Emits disassembler code for instruction decoding. This delegates to the
1741 // FilterChooser instance to do the heavy lifting.
1742 void ARMDecoderEmitter::ARMDEBackend::emit(raw_ostream &o) {
1743 switch (TargetName) {
1744 case TARGET_ARM:
1745 Frontend.EmitSourceFileHeader("ARM/Thumb Decoders", o);
1746 break;
1747 default:
1748 assert(0 && "Unreachable code!");
1751 o << "#include \"llvm/Support/DataTypes.h\"\n";
1752 o << "#include <assert.h>\n";
1753 o << '\n';
1754 o << "namespace llvm {\n\n";
1756 ARMFilterChooser::setTargetName(TargetName);
1758 switch (TargetName) {
1759 case TARGET_ARM: {
1760 // Emit common utility and ARM ISA decoder.
1761 FC = new ARMFilterChooser(NumberedInstructions, Opcodes);
1762 // Reset indentation level.
1763 unsigned Indentation = 0;
1764 FC->emitTop(o, Indentation);
1765 delete FC;
1767 // Emit Thumb ISA decoder as well.
1768 ARMFilterChooser::setTargetName(TARGET_THUMB);
1769 FC = new ARMFilterChooser(NumberedInstructions, Opcodes2);
1770 // Reset indentation level.
1771 Indentation = 0;
1772 FC->emitBot(o, Indentation);
1773 break;
1775 default:
1776 assert(0 && "Unreachable code!");
1779 o << "\n} // End llvm namespace \n";
1782 /////////////////////////
1783 // Backend interface //
1784 /////////////////////////
1786 void ARMDecoderEmitter::initBackend()
1788 Backend = new ARMDEBackend(*this, Records);
1791 void ARMDecoderEmitter::run(raw_ostream &o)
1793 Backend->emit(o);
1796 void ARMDecoderEmitter::shutdownBackend()
1798 delete Backend;
1799 Backend = NULL;