1 //===------------ ARMDecoderEmitter.cpp - Decoder Generator ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
32 /////////////////////////////////////////////////////
34 // Enums and Utilities for ARM Instruction Format //
36 /////////////////////////////////////////////////////
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,
90 // Converts enum to const char*.
91 static const char *stringForARMFormat(ARMFormat form
) {
92 #define ENTRY(n, v) case n: return #n;
109 /////////////////////////
111 // Utility functions //
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!");
127 for (index
= 0; index
< width
; index
++) {
128 if (static_cast<BitInit
*>(init
.getBit(index
))->getValue())
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
);
147 /// sameStringExceptSuffix - Return true if the two strings differ only in RHS's
148 /// suffix. ("VST4d8", "VST4d8_UPD", "_UPD") as input returns true.
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();
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
168 // BIT_UNFILTERED is used as the init value for a filter position. It is used
169 // only for filter processings.
174 BIT_UNFILTERED
// unfiltered
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.
193 // Prints the bit value for each position.
194 static void dumpBits(raw_ostream
&o
, BitsInit
&bits
) {
197 for (index
= bits
.getNumBits(); index
> 0; index
--) {
198 switch (bitFromBits(bits
, index
- 1)) {
209 assert(0 && "unexpected return value from bitFromBits");
214 // Enums for the available target names.
220 // FIXME: Possibly auto-detected?
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
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
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.
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
;
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
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
,
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.
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.
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
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
;
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.
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
),
390 for (unsigned i
= 0; i
< BIT_WIDTH
; ++i
)
391 FilterBitValues
[i
] = BIT_UNFILTERED
;
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
),
402 for (unsigned i
= 0; i
< BIT_WIDTH
; ++i
)
403 FilterBitValues
[i
] = ParentFilterBitValues
[i
];
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
419 void emitBot(raw_ostream
&o
, unsigned &Indentation
);
422 // Populates the insn given the uid.
423 void insnWithID(insn_t
&Insn
, unsigned Opcode
) const {
424 if (AllInstructions
[Opcode
]->isPseudo
)
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
)
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
,
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
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")
500 // Emits code to decode the singleton. Return true if we have matched all the
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
,
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
,
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.
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 ///////////////////////////
535 // Filter Implmenetation //
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
),
550 assert(StartBit
+ NumBits
- 1 < BIT_WIDTH
);
556 for (unsigned i
= 0, e
= Owner
->Opcodes
.size(); i
!= e
; ++i
) {
559 // Populates the insn given the uid.
560 Owner
->insnWithID(Insn
, Owner
->Opcodes
[i
]);
563 // Scans the segment for possibly well-specified encoding bits.
564 bool ok
= Owner
->fieldFromInsn(Field
, Insn
, StartBit
, NumBits
);
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
);
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
]);
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();
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
));
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
*>(
617 new ARMFilterChooser(Owner
->AllInstructions
,
618 VariableInstructions
,
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);
632 // Otherwise, create sub choosers.
633 for (mapIterator
= FilteredInstructions
.begin();
634 mapIterator
!= FilteredInstructions
.end();
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
;
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
*>(
649 new ARMFilterChooser(Owner
->AllInstructions
,
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{";
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();
676 // Field value -1 implies a non-empty set of variable instructions.
677 // See also recurse().
678 if (filterIterator
->first
== (unsigned)-1) {
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";
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
)
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.
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();
722 return FilteredInstructions
.size() + 1;
725 //////////////////////////////////
727 // Filterchooser Implementation //
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"))
744 stringForARMFormat((ARMFormat
)getByteField(Def
, "Form"));
746 o
<< " ARM_FORMAT_NA";
748 o
<< ",\t// Inst #" << i
<< " = " << Name
<< '\n';
750 o
<< " ARM_FORMAT_NA\t// Unreachable.\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.
762 o
.indent(Indentation
) << "typedef uint8_t field_t;\n";
765 o
.indent(Indentation
) << "typedef uint16_t field_t;\n";
768 o
.indent(Indentation
) << "typedef uint32_t field_t;\n";
771 o
.indent(Indentation
) << "typedef uint64_t field_t;\n";
774 assert(0 && "Unexpected instruction size!");
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";
788 o
.indent(Indentation
) << "field_t fieldMask;\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
;
803 o
.indent(Indentation
) << "return (insn & fieldMask) >> startBit;\n";
804 --Indentation
; --Indentation
;
806 o
.indent(Indentation
) << "}\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
);
817 o
.indent(Indentation
) << "return 0;\n";
818 --Indentation
; --Indentation
;
820 o
.indent(Indentation
) << "}\n";
825 // This provides an opportunity for target specific code emission after
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
);
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 {
856 for (unsigned i
= 0; i
< NumBits
; ++i
) {
857 if (Insn
[StartBit
+ i
] == BIT_UNSET
)
860 if (Insn
[StartBit
+ i
] == BIT_TRUE
)
861 Field
= Field
| (1ULL << i
);
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
]) {
873 for (bitIndex
= BIT_WIDTH
; bitIndex
> 0; bitIndex
--) {
874 switch (filter
[bitIndex
- 1]) {
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;
898 dumpFilterArray(o
, current
->FilterBitValues
);
900 current
= current
->Parent
;
904 // Called from Filter::recurse() when singleton exists. For debug purpose.
905 void ARMFilterChooser::SingletonExists(unsigned Opc
) {
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
]) << ' ';
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
<< " ";
923 getBitsField(*AllInstructions
[Opcodes
[i
]]->TheDef
, "Inst"));
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
,
938 uint64_t FieldVal
= 0;
941 // 1: Water (the bit value does not affect decoding)
942 // 2: Island (well-known bit value needed for decoding)
946 for (unsigned i
= 0; i
< BIT_WIDTH
; ++i
) {
947 Val
= Value(Insn
[i
]);
948 bool Filtered
= PositionFiltered(i
);
951 assert(0 && "Unreachable code!");
955 if (Filtered
|| Val
== -1)
956 State
= 1; // Still in Water
958 State
= 2; // Into the Island
960 StartBits
.push_back(i
);
965 if (Filtered
|| Val
== -1) {
966 State
= 1; // Into the Water
967 EndBits
.push_back(i
- 1);
968 FieldVals
.push_back(FieldVal
);
971 State
= 2; // Still in Island
973 FieldVal
= FieldVal
| Val
<< BitNo
;
978 // If we are still in Island after the loop, do some housekeeping.
980 EndBits
.push_back(BIT_WIDTH
- 1);
981 FieldVals
.push_back(FieldVal
);
985 assert(StartBits
.size() == Num
&& EndBits
.size() == Num
&&
986 FieldVals
.size() == Num
);
990 // Emits code to decode the singleton. Return true if we have matched all the
992 bool ARMFilterChooser::emitSingletonDecoder(raw_ostream
&o
, unsigned &Indentation
,
994 std::vector
<unsigned> StartBits
;
995 std::vector
<unsigned> EndBits
;
996 std::vector
<uint64_t> FieldVals
;
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.
1018 o
.indent(Indentation
) << "return " << Opc
<< "; // " << nameWithID(Opc
)
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] << "} ";
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];
1048 o
.indent(Indentation
) << " return " << Opc
<< "; // " << nameWithID(Opc
)
1054 // Emits code to decode the singleton, and then to decode the rest.
1055 void ARMFilterChooser::emitSingletonDecoder(raw_ostream
&o
,
1056 unsigned &Indentation
,
1059 unsigned Opc
= Best
.getSingletonOpc();
1061 emitSingletonDecoder(o
, Indentation
, Opc
);
1063 // Emit code for the rest.
1064 o
.indent(Indentation
) << "else\n";
1067 Best
.getVariableFC().emit(o
, Indentation
);
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
,
1075 unsigned numBit
, bool mixed
) {
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
) {
1099 unsigned numInstructions
= Opcodes
.size();
1101 assert(numInstructions
&& "Filter created with no instructions");
1103 // No further filtering is necessary.
1104 if (numInstructions
== 1)
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
;
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,
1130 unsigned BitIndex
, InsnIndex
;
1132 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1133 // The automaton consumes the corresponding bit from each
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
;
1158 bitAttrs
[BitIndex
] = ATTR_NONE
;
1160 for (InsnIndex
= 0; InsnIndex
< numInstructions
; ++InsnIndex
) {
1163 insnWithID(insn
, Opcodes
[InsnIndex
]);
1165 for (BitIndex
= 0; BitIndex
< BIT_WIDTH
; ++BitIndex
) {
1166 switch (bitAttrs
[BitIndex
]) {
1168 if (insn
[BitIndex
] == BIT_UNSET
)
1169 bitAttrs
[BitIndex
] = ATTR_ALL_UNSET
;
1171 bitAttrs
[BitIndex
] = ATTR_ALL_SET
;
1174 if (insn
[BitIndex
] == BIT_UNSET
)
1175 bitAttrs
[BitIndex
] = ATTR_MIXED
;
1177 case ATTR_ALL_UNSET
:
1178 if (insn
[BitIndex
] != BIT_UNSET
)
1179 bitAttrs
[BitIndex
] = ATTR_MIXED
;
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");
1222 StartBit
= BitIndex
;
1225 case ATTR_ALL_UNSET
:
1228 StartBit
= BitIndex
;
1232 assert(0 && "Unexpected bitAttr!");
1238 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1243 case ATTR_ALL_UNSET
:
1244 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1248 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1249 StartBit
= BitIndex
;
1253 assert(0 && "Unexpected bitAttr!");
1259 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1260 StartBit
= BitIndex
;
1264 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1265 StartBit
= BitIndex
;
1268 case ATTR_ALL_UNSET
:
1269 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1275 assert(0 && "Unexpected bitAttr!");
1278 case ATTR_ALL_UNSET
:
1279 assert(0 && "regionAttr state machine has no ATTR_UNSET state");
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
1292 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1294 case ATTR_ALL_UNSET
:
1297 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1301 // We have finished with the filter processings. Now it's time to choose
1302 // the best performing filter.
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();
1313 if (Usefulness
> BestScore
) {
1315 BestScore
= Usefulness
;
1320 bestFilter().recurse();
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);
1338 // Try regions of consecutive known bit values first.
1339 if (filterProcessor(false))
1342 // Then regions of mixed bits (both known and unitialized bit values allowed).
1343 if (filterProcessor(true))
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))
1353 // If we come to here, the instruction decoding has failed.
1354 // Set the BestIndex to -1 to indicate so.
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
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
);
1374 bestFilter().emit(o
, Indentation
);
1378 // If we reach here, there is a conflict in decoding. Let's resolve the known
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
;
1423 if (name1
== "LDR" && name2
== "LDRcp") {
1424 o
.indent(Indentation
)
1425 << "return " << Opcodes
[0]
1426 << "; // Returning LDR for {LDR, LDRcp}\n";
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
;
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";
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
1463 o
.indent(Indentation
) << "return 0;" << " // Conflict set: ";
1464 for (int i
= 0, N
= Opcodes
.size(); i
< N
; ++i
) {
1465 o
<< nameWithID(Opcodes
[i
]);
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
<< " ";
1482 getBitsField(*AllInstructions
[Opcodes
[i
]]->TheDef
, "Inst"));
1490 ////////////////////////////////////////////
1493 // (Helper class for ARMDecoderEmitter) //
1495 ////////////////////////////////////////////
1497 class ARMDecoderEmitter::ARMDEBackend
{
1499 ARMDEBackend(ARMDecoderEmitter
&frontend
, RecordKeeper
&Records
) :
1500 NumberedInstructions(),
1506 if (Target
.getName() == "ARM")
1507 TargetName
= TARGET_ARM
;
1509 errs() << "Target name " << Target
.getName() << " not recognized\n";
1510 assert(0 && "Unknown target");
1513 // Populate the instructions for our TargetName.
1514 populateInstructions();
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
);
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
,
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"))
1575 if (TN
== TARGET_ARM
) {
1576 if (Form
== ARM_FORMAT_PSEUDO
)
1578 if (thumbInstruction(Form
))
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" ||
1590 // Delegate ADR disassembly to the more generic ADDri/SUBri instructions.
1595 // The following special cases are for conflict resolutions.
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
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")
1613 } else if (TN
== TARGET_THUMB
) {
1614 if (!thumbInstruction(Form
))
1617 // A8.6.189 STM / STMIA / STMEA -- Encoding T1
1618 // There's only STMIA_UPD for Thumb1.
1619 if (Name
== "tSTMIA")
1622 // On Darwin R9 is call-clobbered. Ignore the non-Darwin counterparts.
1623 if (Name
== "tBL" || Name
== "tBLXi" || Name
== "tBLXr")
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")
1630 // Ignore tADR, prefer tADDrPCi.
1634 // Delegate t2ADR disassembly to the more generic t2ADDri12/t2SUBri12
1636 if (Name
== "t2ADR")
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")
1646 // FIXME: Use ldr.n to work around a Darwin assembler bug.
1647 // Introduce a workaround with tLDRpciDIS opcode.
1648 if (Name
== "tLDRpci")
1651 // Ignore t2LDRDpci, prefer the generic t2LDRDi8, t2LDRD_PRE, t2LDRD_POST.
1652 if (Name
== "t2LDRDpci")
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" ||
1667 Name
== "t2MOVCCi16")
1672 // Dumps the instruction encoding format.
1673 switch (TargetName
) {
1676 errs() << Name
<< " " << stringForARMFormat((ARMFormat
)Form
);
1682 // Dumps the instruction encoding bits.
1683 dumpBits(errs(), Bits
);
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";
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"))
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"))
1722 if (populateInstruction(*NumberedInstructions
[uid
], TARGET_THUMB
))
1723 Opcodes2
.push_back(uid
);
1729 // For other targets.
1730 for (unsigned uid
= 0; uid
< numUIDs
; uid
++) {
1731 Record
*R
= NumberedInstructions
[uid
]->TheDef
;
1732 if (R
->getValueAsString("Namespace") == "TargetOpcode")
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
) {
1745 Frontend
.EmitSourceFileHeader("ARM/Thumb Decoders", o
);
1748 assert(0 && "Unreachable code!");
1751 o
<< "#include \"llvm/Support/DataTypes.h\"\n";
1752 o
<< "#include <assert.h>\n";
1754 o
<< "namespace llvm {\n\n";
1756 ARMFilterChooser::setTargetName(TargetName
);
1758 switch (TargetName
) {
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
);
1767 // Emit Thumb ISA decoder as well.
1768 ARMFilterChooser::setTargetName(TARGET_THUMB
);
1769 FC
= new ARMFilterChooser(NumberedInstructions
, Opcodes2
);
1770 // Reset indentation level.
1772 FC
->emitBot(o
, Indentation
);
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
)
1796 void ARMDecoderEmitter::shutdownBackend()