blob: 08e5be985e23cc2a41287153d8e45243781ace95 [file] [log] [blame]
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001//===------------ 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.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "arm-decoder-emitter"
18
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"
25
26#include <vector>
27#include <map>
28#include <string>
29
30using namespace llvm;
31
32/////////////////////////////////////////////////////
33// //
34// Enums and Utilities for ARM Instruction Format //
35// //
36/////////////////////////////////////////////////////
37
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) \
Bob Wilson9a1c1892010-08-11 00:01:18 +000052 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)
Johnny Chenb68a3ee2010-04-02 22:27:38 +000081
82// ARM instruction format specifies the encoding used by the instruction.
83#define ENTRY(n, v) n = v,
84typedef enum {
85 ARM_FORMATS
86 ARM_FORMAT_NA
87} ARMFormat;
88#undef ENTRY
89
90// Converts enum to const char*.
91static 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 "";
98 }
99#undef ENTRY
100}
101
Chandler Carruth1e86e3f2010-04-03 04:45:24 +0000102enum {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000103 IndexModeNone = 0,
104 IndexModePre = 1,
105 IndexModePost = 2,
106 IndexModeUpd = 3
107};
108
109/////////////////////////
110// //
111// Utility functions //
112// //
113/////////////////////////
114
115/// byteFromBitsInit - Return the byte value from a BitsInit.
116/// Called from getByteField().
117static uint8_t byteFromBitsInit(BitsInit &init) {
118 int width = init.getNumBits();
119
120 assert(width <= 8 && "Field is too large for uint8_t!");
121
122 int index;
123 uint8_t mask = 0x01;
124
125 uint8_t ret = 0;
126
127 for (index = 0; index < width; index++) {
128 if (static_cast<BitInit*>(init.getBit(index))->getValue())
129 ret |= mask;
130
131 mask <<= 1;
132 }
133
134 return ret;
135}
136
137static uint8_t getByteField(const Record &def, const char *str) {
138 BitsInit *bits = def.getValueAsBitsInit(str);
139 return byteFromBitsInit(*bits);
140}
141
142static BitsInit &getBitsField(const Record &def, const char *str) {
143 BitsInit *bits = def.getValueAsBitsInit(str);
144 return *bits;
145}
146
147/// sameStringExceptSuffix - Return true if the two strings differ only in RHS's
148/// suffix. ("VST4d8", "VST4d8_UPD", "_UPD") as input returns true.
149static
150bool sameStringExceptSuffix(const StringRef LHS, const StringRef RHS,
151 const StringRef Suffix) {
152
153 if (RHS.startswith(LHS) && RHS.endswith(Suffix))
154 return RHS.size() == LHS.size() + Suffix.size();
155
156 return false;
157}
158
159/// thumbInstruction - Determine whether we have a Thumb instruction.
160/// See also ARMInstrFormats.td.
161static bool thumbInstruction(uint8_t Form) {
162 return Form == ARM_FORMAT_THUMBFRM;
163}
164
165// The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
166// for a bit value.
167//
168// BIT_UNFILTERED is used as the init value for a filter position. It is used
169// only for filter processings.
170typedef enum {
171 BIT_TRUE, // '1'
172 BIT_FALSE, // '0'
173 BIT_UNSET, // '?'
174 BIT_UNFILTERED // unfiltered
175} bit_value_t;
176
177static bool ValueSet(bit_value_t V) {
178 return (V == BIT_TRUE || V == BIT_FALSE);
179}
180static bool ValueNotSet(bit_value_t V) {
181 return (V == BIT_UNSET);
182}
183static int Value(bit_value_t V) {
184 return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
185}
186static 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;
189
190 // The bit is uninitialized.
191 return BIT_UNSET;
192}
193// Prints the bit value for each position.
194static void dumpBits(raw_ostream &o, BitsInit &bits) {
195 unsigned index;
196
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");
210 }
211 }
212}
213
214// Enums for the available target names.
215typedef enum {
216 TARGET_ARM = 0,
217 TARGET_THUMB
218} TARGET_NAME_t;
219
220// FIXME: Possibly auto-detected?
221#define BIT_WIDTH 32
222
223// Forward declaration.
224class FilterChooser;
225
226// Representation of the instruction to work on.
227typedef bit_value_t insn_t[BIT_WIDTH];
228
229/// Filter - Filter works with FilterChooser to produce the decoding tree for
230/// the ISA.
231///
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.
237///
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.
242///
Nick Lewycky366b1e12010-10-07 21:55:16 +0000243/// An example of a conflict is
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000244///
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____
255///
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.
259///
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.
265class Filter {
266protected:
267 FilterChooser *Owner; // points to the FilterChooser who owns this filter
268 unsigned StartBit; // the starting bit position
269 unsigned NumBits; // number of bits to filter
270 bool Mixed; // a mixed region contains both set and unset bits
271
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;
274
275 // Set of uid's with non-constant segment values.
276 std::vector<unsigned> VariableInstructions;
277
278 // Map of well-known segment value to its delegate.
279 std::map<unsigned, FilterChooser*> FilterChooserMap;
280
281 // Number of instructions which fall under FilteredInstructions category.
282 unsigned NumFiltered;
283
284 // Keeps track of the last opcode in the filtered bucket.
285 unsigned LastOpcFiltered;
286
287 // Number of instructions which fall under VariableInstructions category.
288 unsigned NumVariable;
289
290public:
291 unsigned getNumFiltered() { return NumFiltered; }
292 unsigned getNumVariable() { return NumVariable; }
293 unsigned getSingletonOpc() {
294 assert(NumFiltered == 1);
295 return LastOpcFiltered;
296 }
297 // Return the filter chooser for the group of instructions without constant
298 // segment values.
299 FilterChooser &getVariableFC() {
300 assert(NumFiltered == 1);
301 assert(FilterChooserMap.size() == 1);
Johnny Chen493a4412010-04-02 22:51:04 +0000302 return *(FilterChooserMap.find((unsigned)-1)->second);
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000303 }
304
305 Filter(const Filter &f);
306 Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
307
308 ~Filter();
309
310 // Divides the decoding task into sub tasks and delegates them to the
311 // inferior FilterChooser's.
312 //
313 // A special case arises when there's only one entry in the filtered
314 // instructions. In order to unambiguously decode the singleton, we need to
315 // match the remaining undecoded encoding bits against the singleton.
316 void recurse();
317
318 // Emit code to decode instructions given a segment or segments of bits.
319 void emit(raw_ostream &o, unsigned &Indentation);
320
321 // Returns the number of fanout produced by the filter. More fanout implies
322 // the filter distinguishes more categories of instructions.
323 unsigned usefulness() const;
324}; // End of class Filter
325
326// These are states of our finite state machines used in FilterChooser's
327// filterProcessor() which produces the filter candidates to use.
328typedef enum {
329 ATTR_NONE,
330 ATTR_FILTERED,
331 ATTR_ALL_SET,
332 ATTR_ALL_UNSET,
333 ATTR_MIXED
334} bitAttr_t;
335
336/// FilterChooser - FilterChooser chooses the best filter among a set of Filters
337/// in order to perform the decoding of instructions at the current level.
338///
339/// Decoding proceeds from the top down. Based on the well-known encoding bits
340/// of instructions available, FilterChooser builds up the possible Filters that
341/// can further the task of decoding by distinguishing among the remaining
342/// candidate instructions.
343///
344/// Once a filter has been chosen, it is called upon to divide the decoding task
345/// into sub-tasks and delegates them to its inferior FilterChoosers for further
346/// processings.
347///
348/// It is useful to think of a Filter as governing the switch stmts of the
349/// decoding tree. And each case is delegated to an inferior FilterChooser to
350/// decide what further remaining bits to look at.
351class FilterChooser {
352 static TARGET_NAME_t TargetName;
353
354protected:
355 friend class Filter;
356
357 // Vector of codegen instructions to choose our filter.
358 const std::vector<const CodeGenInstruction*> &AllInstructions;
359
360 // Vector of uid's for this filter chooser to work on.
361 const std::vector<unsigned> Opcodes;
362
363 // Vector of candidate filters.
364 std::vector<Filter> Filters;
365
366 // Array of bit values passed down from our parent.
367 // Set to all BIT_UNFILTERED's for Parent == NULL.
368 bit_value_t FilterBitValues[BIT_WIDTH];
369
370 // Links to the FilterChooser above us in the decoding tree.
371 FilterChooser *Parent;
372
373 // Index of the best filter from Filters.
374 int BestIndex;
375
376public:
377 static void setTargetName(TARGET_NAME_t tn) { TargetName = tn; }
378
379 FilterChooser(const FilterChooser &FC) :
380 AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
381 Filters(FC.Filters), Parent(FC.Parent), BestIndex(FC.BestIndex) {
382 memcpy(FilterBitValues, FC.FilterBitValues, sizeof(FilterBitValues));
383 }
384
385 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
386 const std::vector<unsigned> &IDs) :
387 AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(NULL),
388 BestIndex(-1) {
389 for (unsigned i = 0; i < BIT_WIDTH; ++i)
390 FilterBitValues[i] = BIT_UNFILTERED;
391
392 doFilter();
393 }
394
395 FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
396 const std::vector<unsigned> &IDs,
397 bit_value_t (&ParentFilterBitValues)[BIT_WIDTH],
398 FilterChooser &parent) :
399 AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(&parent),
400 BestIndex(-1) {
401 for (unsigned i = 0; i < BIT_WIDTH; ++i)
402 FilterBitValues[i] = ParentFilterBitValues[i];
403
404 doFilter();
405 }
406
407 // The top level filter chooser has NULL as its parent.
408 bool isTopLevel() { return Parent == NULL; }
409
410 // This provides an opportunity for target specific code emission.
411 void emitTopHook(raw_ostream &o);
412
413 // Emit the top level typedef and decodeInstruction() function.
414 void emitTop(raw_ostream &o, unsigned &Indentation);
415
416 // This provides an opportunity for target specific code emission after
417 // emitTop().
418 void emitBot(raw_ostream &o, unsigned &Indentation);
419
420protected:
421 // Populates the insn given the uid.
422 void insnWithID(insn_t &Insn, unsigned Opcode) const {
423 BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
424
425 for (unsigned i = 0; i < BIT_WIDTH; ++i)
426 Insn[i] = bitFromBits(Bits, i);
427
428 // Set Inst{21} to 1 (wback) when IndexModeBits == IndexModeUpd.
429 if (getByteField(*AllInstructions[Opcode]->TheDef, "IndexModeBits")
430 == IndexModeUpd)
431 Insn[21] = BIT_TRUE;
432 }
433
434 // Returns the record name.
435 const std::string &nameWithID(unsigned Opcode) const {
436 return AllInstructions[Opcode]->TheDef->getName();
437 }
438
439 // Populates the field of the insn given the start position and the number of
440 // consecutive bits to scan for.
441 //
442 // Returns false if there exists any uninitialized bit value in the range.
443 // Returns true, otherwise.
444 bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
445 unsigned NumBits) const;
446
447 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
448 /// filter array as a series of chars.
449 void dumpFilterArray(raw_ostream &o, bit_value_t (&filter)[BIT_WIDTH]);
450
451 /// dumpStack - dumpStack traverses the filter chooser chain and calls
452 /// dumpFilterArray on each filter chooser up to the top level one.
453 void dumpStack(raw_ostream &o, const char *prefix);
454
455 Filter &bestFilter() {
456 assert(BestIndex != -1 && "BestIndex not set");
457 return Filters[BestIndex];
458 }
459
460 // Called from Filter::recurse() when singleton exists. For debug purpose.
461 void SingletonExists(unsigned Opc);
462
463 bool PositionFiltered(unsigned i) {
464 return ValueSet(FilterBitValues[i]);
465 }
466
467 // Calculates the island(s) needed to decode the instruction.
468 // This returns a lit of undecoded bits of an instructions, for example,
469 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
470 // decoded bits in order to verify that the instruction matches the Opcode.
471 unsigned getIslands(std::vector<unsigned> &StartBits,
472 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
473 insn_t &Insn);
474
475 // The purpose of this function is for the API client to detect possible
476 // Load/Store Coprocessor instructions. If the coprocessor number is of
477 // the instruction is either 10 or 11, the decoder should not report the
478 // instruction as LDC/LDC2/STC/STC2, but should match against Advanced SIMD or
479 // VFP instructions.
480 bool LdStCopEncoding1(unsigned Opc) {
481 const std::string &Name = nameWithID(Opc);
482 if (Name == "LDC_OFFSET" || Name == "LDC_OPTION" ||
483 Name == "LDC_POST" || Name == "LDC_PRE" ||
484 Name == "LDCL_OFFSET" || Name == "LDCL_OPTION" ||
485 Name == "LDCL_POST" || Name == "LDCL_PRE" ||
486 Name == "STC_OFFSET" || Name == "STC_OPTION" ||
487 Name == "STC_POST" || Name == "STC_PRE" ||
488 Name == "STCL_OFFSET" || Name == "STCL_OPTION" ||
489 Name == "STCL_POST" || Name == "STCL_PRE")
490 return true;
491 else
492 return false;
493 }
494
495 // Emits code to decode the singleton. Return true if we have matched all the
496 // well-known bits.
497 bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc);
498
499 // Emits code to decode the singleton, and then to decode the rest.
500 void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,Filter &Best);
501
502 // Assign a single filter and run with it.
503 void runSingleFilter(FilterChooser &owner, unsigned startBit, unsigned numBit,
504 bool mixed);
505
506 // reportRegion is a helper function for filterProcessor to mark a region as
507 // eligible for use as a filter region.
508 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
509 bool AllowMixed);
510
511 // FilterProcessor scans the well-known encoding bits of the instructions and
512 // builds up a list of candidate filters. It chooses the best filter and
513 // recursively descends down the decoding tree.
514 bool filterProcessor(bool AllowMixed, bool Greedy = true);
515
516 // Decides on the best configuration of filter(s) to use in order to decode
517 // the instructions. A conflict of instructions may occur, in which case we
518 // dump the conflict set to the standard error.
519 void doFilter();
520
521 // Emits code to decode our share of instructions. Returns true if the
522 // emitted code causes a return, which occurs if we know how to decode
523 // the instruction at this level or the instruction is not decodeable.
524 bool emit(raw_ostream &o, unsigned &Indentation);
525};
526
527///////////////////////////
528// //
529// Filter Implmenetation //
530// //
531///////////////////////////
532
533Filter::Filter(const Filter &f) :
534 Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
535 FilteredInstructions(f.FilteredInstructions),
536 VariableInstructions(f.VariableInstructions),
537 FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered),
538 LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) {
539}
540
541Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
542 bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits),
543 Mixed(mixed) {
544 assert(StartBit + NumBits - 1 < BIT_WIDTH);
545
546 NumFiltered = 0;
547 LastOpcFiltered = 0;
548 NumVariable = 0;
549
550 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
551 insn_t Insn;
552
553 // Populates the insn given the uid.
554 Owner->insnWithID(Insn, Owner->Opcodes[i]);
555
556 uint64_t Field;
557 // Scans the segment for possibly well-specified encoding bits.
558 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
559
560 if (ok) {
561 // The encoding bits are well-known. Lets add the uid of the
562 // instruction into the bucket keyed off the constant field value.
563 LastOpcFiltered = Owner->Opcodes[i];
564 FilteredInstructions[Field].push_back(LastOpcFiltered);
565 ++NumFiltered;
566 } else {
567 // Some of the encoding bit(s) are unspecfied. This contributes to
568 // one additional member of "Variable" instructions.
569 VariableInstructions.push_back(Owner->Opcodes[i]);
570 ++NumVariable;
571 }
572 }
573
574 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
575 && "Filter returns no instruction categories");
576}
577
578Filter::~Filter() {
579 std::map<unsigned, FilterChooser*>::iterator filterIterator;
580 for (filterIterator = FilterChooserMap.begin();
581 filterIterator != FilterChooserMap.end();
582 filterIterator++) {
583 delete filterIterator->second;
584 }
585}
586
587// Divides the decoding task into sub tasks and delegates them to the
588// inferior FilterChooser's.
589//
590// A special case arises when there's only one entry in the filtered
591// instructions. In order to unambiguously decode the singleton, we need to
592// match the remaining undecoded encoding bits against the singleton.
593void Filter::recurse() {
594 std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator;
595
596 bit_value_t BitValueArray[BIT_WIDTH];
597 // Starts by inheriting our parent filter chooser's filter bit values.
Johnny Chen2d16a672010-04-08 21:23:54 +0000598 memcpy(BitValueArray, Owner->FilterBitValues, sizeof(BitValueArray));
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000599
600 unsigned bitIndex;
601
602 if (VariableInstructions.size()) {
603 // Conservatively marks each segment position as BIT_UNSET.
604 for (bitIndex = 0; bitIndex < NumBits; bitIndex++)
605 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
606
607 // Delegates to an inferior filter chooser for futher processing on this
608 // group of instructions whose segment values are variable.
609 FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
610 (unsigned)-1,
611 new FilterChooser(Owner->AllInstructions,
612 VariableInstructions,
613 BitValueArray,
614 *Owner)
615 ));
616 }
617
618 // No need to recurse for a singleton filtered instruction.
619 // See also Filter::emit().
620 if (getNumFiltered() == 1) {
621 //Owner->SingletonExists(LastOpcFiltered);
622 assert(FilterChooserMap.size() == 1);
623 return;
624 }
Johnny Chen3c500e62010-04-07 20:53:12 +0000625
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000626 // Otherwise, create sub choosers.
627 for (mapIterator = FilteredInstructions.begin();
628 mapIterator != FilteredInstructions.end();
629 mapIterator++) {
630
631 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
632 for (bitIndex = 0; bitIndex < NumBits; bitIndex++) {
Benjamin Kramer454c4ce2010-04-08 15:25:57 +0000633 if (mapIterator->first & (1ULL << bitIndex))
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000634 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
635 else
636 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
637 }
638
639 // Delegates to an inferior filter chooser for futher processing on this
640 // category of instructions.
641 FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
642 mapIterator->first,
643 new FilterChooser(Owner->AllInstructions,
644 mapIterator->second,
645 BitValueArray,
646 *Owner)
647 ));
648 }
649}
650
651// Emit code to decode instructions given a segment or segments of bits.
652void Filter::emit(raw_ostream &o, unsigned &Indentation) {
653 o.indent(Indentation) << "// Check Inst{";
654
655 if (NumBits > 1)
656 o << (StartBit + NumBits - 1) << '-';
657
658 o << StartBit << "} ...\n";
659
660 o.indent(Indentation) << "switch (fieldFromInstruction(insn, "
661 << StartBit << ", " << NumBits << ")) {\n";
662
663 std::map<unsigned, FilterChooser*>::iterator filterIterator;
664
665 bool DefaultCase = false;
666 for (filterIterator = FilterChooserMap.begin();
667 filterIterator != FilterChooserMap.end();
668 filterIterator++) {
669
670 // Field value -1 implies a non-empty set of variable instructions.
671 // See also recurse().
672 if (filterIterator->first == (unsigned)-1) {
673 DefaultCase = true;
674
675 o.indent(Indentation) << "default:\n";
676 o.indent(Indentation) << " break; // fallthrough\n";
677
678 // Closing curly brace for the switch statement.
679 // This is unconventional because we want the default processing to be
680 // performed for the fallthrough cases as well, i.e., when the "cases"
681 // did not prove a decoded instruction.
682 o.indent(Indentation) << "}\n";
683
684 } else
685 o.indent(Indentation) << "case " << filterIterator->first << ":\n";
686
687 // We arrive at a category of instructions with the same segment value.
688 // Now delegate to the sub filter chooser for further decodings.
689 // The case may fallthrough, which happens if the remaining well-known
690 // encoding bits do not match exactly.
691 if (!DefaultCase) { ++Indentation; ++Indentation; }
692
693 bool finished = filterIterator->second->emit(o, Indentation);
694 // For top level default case, there's no need for a break statement.
695 if (Owner->isTopLevel() && DefaultCase)
696 break;
697 if (!finished)
698 o.indent(Indentation) << "break;\n";
699
700 if (!DefaultCase) { --Indentation; --Indentation; }
701 }
702
703 // If there is no default case, we still need to supply a closing brace.
704 if (!DefaultCase) {
705 // Closing curly brace for the switch statement.
706 o.indent(Indentation) << "}\n";
707 }
708}
709
710// Returns the number of fanout produced by the filter. More fanout implies
711// the filter distinguishes more categories of instructions.
712unsigned Filter::usefulness() const {
713 if (VariableInstructions.size())
714 return FilteredInstructions.size();
715 else
716 return FilteredInstructions.size() + 1;
717}
718
719//////////////////////////////////
720// //
721// Filterchooser Implementation //
722// //
723//////////////////////////////////
724
725// Define the symbol here.
726TARGET_NAME_t FilterChooser::TargetName;
727
728// This provides an opportunity for target specific code emission.
729void FilterChooser::emitTopHook(raw_ostream &o) {
730 if (TargetName == TARGET_ARM) {
731 // Emit code that references the ARMFormat data type.
732 o << "static const ARMFormat ARMFormats[] = {\n";
733 for (unsigned i = 0, e = AllInstructions.size(); i != e; ++i) {
734 const Record &Def = *(AllInstructions[i]->TheDef);
735 const std::string &Name = Def.getName();
736 if (Def.isSubClassOf("InstARM") || Def.isSubClassOf("InstThumb"))
737 o.indent(2) <<
738 stringForARMFormat((ARMFormat)getByteField(Def, "Form"));
739 else
740 o << " ARM_FORMAT_NA";
741
742 o << ",\t// Inst #" << i << " = " << Name << '\n';
743 }
744 o << " ARM_FORMAT_NA\t// Unreachable.\n";
745 o << "};\n\n";
746 }
747}
748
749// Emit the top level typedef and decodeInstruction() function.
750void FilterChooser::emitTop(raw_ostream &o, unsigned &Indentation) {
751 // Run the target specific emit hook.
752 emitTopHook(o);
753
754 switch (BIT_WIDTH) {
755 case 8:
756 o.indent(Indentation) << "typedef uint8_t field_t;\n";
757 break;
758 case 16:
759 o.indent(Indentation) << "typedef uint16_t field_t;\n";
760 break;
761 case 32:
762 o.indent(Indentation) << "typedef uint32_t field_t;\n";
763 break;
764 case 64:
765 o.indent(Indentation) << "typedef uint64_t field_t;\n";
766 break;
767 default:
768 assert(0 && "Unexpected instruction size!");
769 }
770
771 o << '\n';
772
773 o.indent(Indentation) << "static field_t " <<
774 "fieldFromInstruction(field_t insn, unsigned startBit, unsigned numBits)\n";
775
776 o.indent(Indentation) << "{\n";
777
778 ++Indentation; ++Indentation;
779 o.indent(Indentation) << "assert(startBit + numBits <= " << BIT_WIDTH
780 << " && \"Instruction field out of bounds!\");\n";
781 o << '\n';
782 o.indent(Indentation) << "field_t fieldMask;\n";
783 o << '\n';
784 o.indent(Indentation) << "if (numBits == " << BIT_WIDTH << ")\n";
785
786 ++Indentation; ++Indentation;
787 o.indent(Indentation) << "fieldMask = (field_t)-1;\n";
788 --Indentation; --Indentation;
789
790 o.indent(Indentation) << "else\n";
791
792 ++Indentation; ++Indentation;
793 o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n";
794 --Indentation; --Indentation;
795
796 o << '\n';
797 o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n";
798 --Indentation; --Indentation;
799
800 o.indent(Indentation) << "}\n";
801
802 o << '\n';
803
Jim Grosbachbb168242010-10-08 18:13:57 +0000804 o.indent(Indentation) <<"static uint16_t decodeInstruction(field_t insn) {\n";
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000805
806 ++Indentation; ++Indentation;
807 // Emits code to decode the instructions.
808 emit(o, Indentation);
809
810 o << '\n';
811 o.indent(Indentation) << "return 0;\n";
812 --Indentation; --Indentation;
813
814 o.indent(Indentation) << "}\n";
815
816 o << '\n';
817}
818
819// This provides an opportunity for target specific code emission after
820// emitTop().
821void FilterChooser::emitBot(raw_ostream &o, unsigned &Indentation) {
822 if (TargetName != TARGET_THUMB) return;
823
824 // Emit code that decodes the Thumb ISA.
825 o.indent(Indentation)
826 << "static uint16_t decodeThumbInstruction(field_t insn) {\n";
827
828 ++Indentation; ++Indentation;
829
830 // Emits code to decode the instructions.
831 emit(o, Indentation);
832
833 o << '\n';
834 o.indent(Indentation) << "return 0;\n";
835
836 --Indentation; --Indentation;
837
838 o.indent(Indentation) << "}\n";
839}
840
841// Populates the field of the insn given the start position and the number of
842// consecutive bits to scan for.
843//
844// Returns false if and on the first uninitialized bit value encountered.
845// Returns true, otherwise.
846bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
847 unsigned StartBit, unsigned NumBits) const {
848 Field = 0;
849
850 for (unsigned i = 0; i < NumBits; ++i) {
851 if (Insn[StartBit + i] == BIT_UNSET)
852 return false;
853
854 if (Insn[StartBit + i] == BIT_TRUE)
Benjamin Kramer454c4ce2010-04-08 15:25:57 +0000855 Field = Field | (1ULL << i);
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000856 }
857
858 return true;
859}
860
861/// dumpFilterArray - dumpFilterArray prints out debugging info for the given
862/// filter array as a series of chars.
863void FilterChooser::dumpFilterArray(raw_ostream &o,
864 bit_value_t (&filter)[BIT_WIDTH]) {
865 unsigned bitIndex;
866
867 for (bitIndex = BIT_WIDTH; bitIndex > 0; bitIndex--) {
868 switch (filter[bitIndex - 1]) {
869 case BIT_UNFILTERED:
870 o << ".";
871 break;
872 case BIT_UNSET:
873 o << "_";
874 break;
875 case BIT_TRUE:
876 o << "1";
877 break;
878 case BIT_FALSE:
879 o << "0";
880 break;
881 }
882 }
883}
884
885/// dumpStack - dumpStack traverses the filter chooser chain and calls
886/// dumpFilterArray on each filter chooser up to the top level one.
887void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) {
888 FilterChooser *current = this;
889
890 while (current) {
891 o << prefix;
892 dumpFilterArray(o, current->FilterBitValues);
893 o << '\n';
894 current = current->Parent;
895 }
896}
897
898// Called from Filter::recurse() when singleton exists. For debug purpose.
899void FilterChooser::SingletonExists(unsigned Opc) {
900 insn_t Insn0;
901 insnWithID(Insn0, Opc);
902
903 errs() << "Singleton exists: " << nameWithID(Opc)
904 << " with its decoding dominating ";
905 for (unsigned i = 0; i < Opcodes.size(); ++i) {
906 if (Opcodes[i] == Opc) continue;
907 errs() << nameWithID(Opcodes[i]) << ' ';
908 }
909 errs() << '\n';
910
911 dumpStack(errs(), "\t\t");
912 for (unsigned i = 0; i < Opcodes.size(); i++) {
913 const std::string &Name = nameWithID(Opcodes[i]);
914
915 errs() << '\t' << Name << " ";
916 dumpBits(errs(),
917 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
918 errs() << '\n';
919 }
920}
921
922// Calculates the island(s) needed to decode the instruction.
923// This returns a list of undecoded bits of an instructions, for example,
924// Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
925// decoded bits in order to verify that the instruction matches the Opcode.
926unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
927 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
928 insn_t &Insn) {
929 unsigned Num, BitNo;
930 Num = BitNo = 0;
931
932 uint64_t FieldVal = 0;
933
934 // 0: Init
935 // 1: Water (the bit value does not affect decoding)
936 // 2: Island (well-known bit value needed for decoding)
937 int State = 0;
938 int Val = -1;
939
940 for (unsigned i = 0; i < BIT_WIDTH; ++i) {
941 Val = Value(Insn[i]);
942 bool Filtered = PositionFiltered(i);
943 switch (State) {
944 default:
945 assert(0 && "Unreachable code!");
946 break;
947 case 0:
948 case 1:
949 if (Filtered || Val == -1)
950 State = 1; // Still in Water
951 else {
952 State = 2; // Into the Island
953 BitNo = 0;
954 StartBits.push_back(i);
955 FieldVal = Val;
956 }
957 break;
958 case 2:
959 if (Filtered || Val == -1) {
960 State = 1; // Into the Water
961 EndBits.push_back(i - 1);
962 FieldVals.push_back(FieldVal);
963 ++Num;
964 } else {
965 State = 2; // Still in Island
966 ++BitNo;
967 FieldVal = FieldVal | Val << BitNo;
968 }
969 break;
970 }
971 }
972 // If we are still in Island after the loop, do some housekeeping.
973 if (State == 2) {
974 EndBits.push_back(BIT_WIDTH - 1);
975 FieldVals.push_back(FieldVal);
976 ++Num;
977 }
978
979 assert(StartBits.size() == Num && EndBits.size() == Num &&
980 FieldVals.size() == Num);
981 return Num;
982}
983
984// Emits code to decode the singleton. Return true if we have matched all the
985// well-known bits.
986bool FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
987 unsigned Opc) {
988 std::vector<unsigned> StartBits;
989 std::vector<unsigned> EndBits;
990 std::vector<uint64_t> FieldVals;
991 insn_t Insn;
992 insnWithID(Insn, Opc);
993
994 // This provides a good opportunity to check for possible Ld/St Coprocessor
995 // Opcode and escapes if the coproc # is either 10 or 11. It is a NEON/VFP
996 // instruction is disguise.
997 if (TargetName == TARGET_ARM && LdStCopEncoding1(Opc)) {
998 o.indent(Indentation);
999 // A8.6.51 & A8.6.188
1000 // If coproc = 0b101?, i.e, slice(insn, 11, 8) = 10 or 11, escape.
1001 o << "if (fieldFromInstruction(insn, 9, 3) == 5) break; // fallthrough\n";
1002 }
1003
1004 // Look for islands of undecoded bits of the singleton.
1005 getIslands(StartBits, EndBits, FieldVals, Insn);
1006
1007 unsigned Size = StartBits.size();
1008 unsigned I, NumBits;
1009
1010 // If we have matched all the well-known bits, just issue a return.
1011 if (Size == 0) {
1012 o.indent(Indentation) << "return " << Opc << "; // " << nameWithID(Opc)
1013 << '\n';
1014 return true;
1015 }
1016
1017 // Otherwise, there are more decodings to be done!
1018
1019 // Emit code to match the island(s) for the singleton.
1020 o.indent(Indentation) << "// Check ";
1021
1022 for (I = Size; I != 0; --I) {
1023 o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} ";
1024 if (I > 1)
1025 o << "&& ";
1026 else
1027 o << "for singleton decoding...\n";
1028 }
1029
1030 o.indent(Indentation) << "if (";
1031
1032 for (I = Size; I != 0; --I) {
1033 NumBits = EndBits[I-1] - StartBits[I-1] + 1;
1034 o << "fieldFromInstruction(insn, " << StartBits[I-1] << ", " << NumBits
1035 << ") == " << FieldVals[I-1];
1036 if (I > 1)
1037 o << " && ";
1038 else
1039 o << ")\n";
1040 }
1041
1042 o.indent(Indentation) << " return " << Opc << "; // " << nameWithID(Opc)
1043 << '\n';
1044
1045 return false;
1046}
1047
1048// Emits code to decode the singleton, and then to decode the rest.
1049void FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
1050 Filter &Best) {
1051
1052 unsigned Opc = Best.getSingletonOpc();
1053
1054 emitSingletonDecoder(o, Indentation, Opc);
1055
1056 // Emit code for the rest.
1057 o.indent(Indentation) << "else\n";
1058
1059 Indentation += 2;
1060 Best.getVariableFC().emit(o, Indentation);
1061 Indentation -= 2;
1062}
1063
1064// Assign a single filter and run with it. Top level API client can initialize
1065// with a single filter to start the filtering process.
1066void FilterChooser::runSingleFilter(FilterChooser &owner, unsigned startBit,
1067 unsigned numBit, bool mixed) {
1068 Filters.clear();
1069 Filter F(*this, startBit, numBit, true);
1070 Filters.push_back(F);
1071 BestIndex = 0; // Sole Filter instance to choose from.
1072 bestFilter().recurse();
1073}
1074
1075// reportRegion is a helper function for filterProcessor to mark a region as
1076// eligible for use as a filter region.
1077void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1078 unsigned BitIndex, bool AllowMixed) {
1079 if (RA == ATTR_MIXED && AllowMixed)
1080 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true));
1081 else if (RA == ATTR_ALL_SET && !AllowMixed)
1082 Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false));
1083}
1084
1085// FilterProcessor scans the well-known encoding bits of the instructions and
1086// builds up a list of candidate filters. It chooses the best filter and
1087// recursively descends down the decoding tree.
1088bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1089 Filters.clear();
1090 BestIndex = -1;
1091 unsigned numInstructions = Opcodes.size();
1092
1093 assert(numInstructions && "Filter created with no instructions");
1094
1095 // No further filtering is necessary.
1096 if (numInstructions == 1)
1097 return true;
1098
1099 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1100 // instructions is 3.
1101 if (AllowMixed && !Greedy) {
1102 assert(numInstructions == 3);
1103
1104 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1105 std::vector<unsigned> StartBits;
1106 std::vector<unsigned> EndBits;
1107 std::vector<uint64_t> FieldVals;
1108 insn_t Insn;
1109
1110 insnWithID(Insn, Opcodes[i]);
1111
1112 // Look for islands of undecoded bits of any instruction.
1113 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1114 // Found an instruction with island(s). Now just assign a filter.
1115 runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1,
1116 true);
1117 return true;
1118 }
1119 }
1120 }
1121
1122 unsigned BitIndex, InsnIndex;
1123
1124 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1125 // The automaton consumes the corresponding bit from each
1126 // instruction.
1127 //
1128 // Input symbols: 0, 1, and _ (unset).
1129 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1130 // Initial state: NONE.
1131 //
1132 // (NONE) ------- [01] -> (ALL_SET)
1133 // (NONE) ------- _ ----> (ALL_UNSET)
1134 // (ALL_SET) ---- [01] -> (ALL_SET)
1135 // (ALL_SET) ---- _ ----> (MIXED)
1136 // (ALL_UNSET) -- [01] -> (MIXED)
1137 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1138 // (MIXED) ------ . ----> (MIXED)
1139 // (FILTERED)---- . ----> (FILTERED)
1140
1141 bitAttr_t bitAttrs[BIT_WIDTH];
1142
1143 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1144 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1145 for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex)
1146 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1147 FilterBitValues[BitIndex] == BIT_FALSE)
1148 bitAttrs[BitIndex] = ATTR_FILTERED;
1149 else
1150 bitAttrs[BitIndex] = ATTR_NONE;
1151
1152 for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
1153 insn_t insn;
1154
1155 insnWithID(insn, Opcodes[InsnIndex]);
1156
1157 for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex) {
1158 switch (bitAttrs[BitIndex]) {
1159 case ATTR_NONE:
1160 if (insn[BitIndex] == BIT_UNSET)
1161 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1162 else
1163 bitAttrs[BitIndex] = ATTR_ALL_SET;
1164 break;
1165 case ATTR_ALL_SET:
1166 if (insn[BitIndex] == BIT_UNSET)
1167 bitAttrs[BitIndex] = ATTR_MIXED;
1168 break;
1169 case ATTR_ALL_UNSET:
1170 if (insn[BitIndex] != BIT_UNSET)
1171 bitAttrs[BitIndex] = ATTR_MIXED;
1172 break;
1173 case ATTR_MIXED:
1174 case ATTR_FILTERED:
1175 break;
1176 }
1177 }
1178 }
1179
1180 // The regionAttr automaton consumes the bitAttrs automatons' state,
1181 // lowest-to-highest.
1182 //
1183 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1184 // States: NONE, ALL_SET, MIXED
1185 // Initial state: NONE
1186 //
1187 // (NONE) ----- F --> (NONE)
1188 // (NONE) ----- S --> (ALL_SET) ; and set region start
1189 // (NONE) ----- U --> (NONE)
1190 // (NONE) ----- M --> (MIXED) ; and set region start
1191 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1192 // (ALL_SET) -- S --> (ALL_SET)
1193 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1194 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1195 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1196 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1197 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1198 // (MIXED) ---- M --> (MIXED)
1199
1200 bitAttr_t RA = ATTR_NONE;
1201 unsigned StartBit = 0;
1202
1203 for (BitIndex = 0; BitIndex < BIT_WIDTH; BitIndex++) {
1204 bitAttr_t bitAttr = bitAttrs[BitIndex];
1205
1206 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1207
1208 switch (RA) {
1209 case ATTR_NONE:
1210 switch (bitAttr) {
1211 case ATTR_FILTERED:
1212 break;
1213 case ATTR_ALL_SET:
1214 StartBit = BitIndex;
1215 RA = ATTR_ALL_SET;
1216 break;
1217 case ATTR_ALL_UNSET:
1218 break;
1219 case ATTR_MIXED:
1220 StartBit = BitIndex;
1221 RA = ATTR_MIXED;
1222 break;
1223 default:
1224 assert(0 && "Unexpected bitAttr!");
1225 }
1226 break;
1227 case ATTR_ALL_SET:
1228 switch (bitAttr) {
1229 case ATTR_FILTERED:
1230 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1231 RA = ATTR_NONE;
1232 break;
1233 case ATTR_ALL_SET:
1234 break;
1235 case ATTR_ALL_UNSET:
1236 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1237 RA = ATTR_NONE;
1238 break;
1239 case ATTR_MIXED:
1240 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1241 StartBit = BitIndex;
1242 RA = ATTR_MIXED;
1243 break;
1244 default:
1245 assert(0 && "Unexpected bitAttr!");
1246 }
1247 break;
1248 case ATTR_MIXED:
1249 switch (bitAttr) {
1250 case ATTR_FILTERED:
1251 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1252 StartBit = BitIndex;
1253 RA = ATTR_NONE;
1254 break;
1255 case ATTR_ALL_SET:
1256 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1257 StartBit = BitIndex;
1258 RA = ATTR_ALL_SET;
1259 break;
1260 case ATTR_ALL_UNSET:
1261 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1262 RA = ATTR_NONE;
1263 break;
1264 case ATTR_MIXED:
1265 break;
1266 default:
1267 assert(0 && "Unexpected bitAttr!");
1268 }
1269 break;
1270 case ATTR_ALL_UNSET:
1271 assert(0 && "regionAttr state machine has no ATTR_UNSET state");
1272 case ATTR_FILTERED:
1273 assert(0 && "regionAttr state machine has no ATTR_FILTERED state");
1274 }
1275 }
1276
1277 // At the end, if we're still in ALL_SET or MIXED states, report a region
1278 switch (RA) {
1279 case ATTR_NONE:
1280 break;
1281 case ATTR_FILTERED:
1282 break;
1283 case ATTR_ALL_SET:
1284 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1285 break;
1286 case ATTR_ALL_UNSET:
1287 break;
1288 case ATTR_MIXED:
1289 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1290 break;
1291 }
1292
1293 // We have finished with the filter processings. Now it's time to choose
1294 // the best performing filter.
1295 BestIndex = 0;
1296 bool AllUseless = true;
1297 unsigned BestScore = 0;
1298
1299 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1300 unsigned Usefulness = Filters[i].usefulness();
1301
1302 if (Usefulness)
1303 AllUseless = false;
1304
1305 if (Usefulness > BestScore) {
1306 BestIndex = i;
1307 BestScore = Usefulness;
1308 }
1309 }
1310
1311 if (!AllUseless)
1312 bestFilter().recurse();
1313
1314 return !AllUseless;
1315} // end of FilterChooser::filterProcessor(bool)
1316
1317// Decides on the best configuration of filter(s) to use in order to decode
1318// the instructions. A conflict of instructions may occur, in which case we
1319// dump the conflict set to the standard error.
1320void FilterChooser::doFilter() {
1321 unsigned Num = Opcodes.size();
1322 assert(Num && "FilterChooser created with no instructions");
1323
1324 // Heuristics: Use Inst{31-28} as the top level filter for ARM ISA.
1325 if (TargetName == TARGET_ARM && Parent == NULL) {
1326 runSingleFilter(*this, 28, 4, false);
1327 return;
1328 }
1329
1330 // Try regions of consecutive known bit values first.
1331 if (filterProcessor(false))
1332 return;
1333
1334 // Then regions of mixed bits (both known and unitialized bit values allowed).
1335 if (filterProcessor(true))
1336 return;
1337
1338 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1339 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1340 // well-known encoding pattern. In such case, we backtrack and scan for the
1341 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1342 if (Num == 3 && filterProcessor(true, false))
1343 return;
1344
1345 // If we come to here, the instruction decoding has failed.
Johnny Chene0c74fb2010-04-09 19:31:33 +00001346 // Set the BestIndex to -1 to indicate so.
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001347 BestIndex = -1;
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001348}
1349
1350// Emits code to decode our share of instructions. Returns true if the
1351// emitted code causes a return, which occurs if we know how to decode
1352// the instruction at this level or the instruction is not decodeable.
1353bool FilterChooser::emit(raw_ostream &o, unsigned &Indentation) {
1354 if (Opcodes.size() == 1)
1355 // There is only one instruction in the set, which is great!
1356 // Call emitSingletonDecoder() to see whether there are any remaining
1357 // encodings bits.
1358 return emitSingletonDecoder(o, Indentation, Opcodes[0]);
1359
1360 // Choose the best filter to do the decodings!
1361 if (BestIndex != -1) {
1362 Filter &Best = bestFilter();
1363 if (Best.getNumFiltered() == 1)
1364 emitSingletonDecoder(o, Indentation, Best);
1365 else
1366 bestFilter().emit(o, Indentation);
1367 return false;
1368 }
1369
1370 // If we reach here, there is a conflict in decoding. Let's resolve the known
1371 // conflicts!
1372 if ((TargetName == TARGET_ARM || TargetName == TARGET_THUMB) &&
1373 Opcodes.size() == 2) {
1374 // Resolve the known conflict sets:
1375 //
1376 // 1. source registers are identical => VMOVDneon; otherwise => VORRd
1377 // 2. source registers are identical => VMOVQ; otherwise => VORRq
1378 // 3. LDR, LDRcp => return LDR for now.
1379 // FIXME: How can we distinguish between LDR and LDRcp? Do we need to?
1380 // 4. tLDM, tLDM_UPD => Rn = Inst{10-8}, reglist = Inst{7-0},
1381 // wback = registers<Rn> = 0
1382 // NOTE: (tLDM, tLDM_UPD) resolution must come before Advanced SIMD
1383 // addressing mode resolution!!!
1384 // 5. VLD[234]LN*/VST[234]LN* vs. VLD[234]LN*_UPD/VST[234]LN*_UPD conflicts
1385 // are resolved returning the non-UPD versions of the instructions if the
1386 // Rm field, i.e., Inst{3-0} is 0b1111. This is specified in A7.7.1
1387 // Advanced SIMD addressing mode.
1388 const std::string &name1 = nameWithID(Opcodes[0]);
1389 const std::string &name2 = nameWithID(Opcodes[1]);
1390 if ((name1 == "VMOVDneon" && name2 == "VORRd") ||
1391 (name1 == "VMOVQ" && name2 == "VORRq")) {
1392 // Inserting the opening curly brace for this case block.
1393 --Indentation; --Indentation;
1394 o.indent(Indentation) << "{\n";
1395 ++Indentation; ++Indentation;
1396
1397 o.indent(Indentation)
1398 << "field_t N = fieldFromInstruction(insn, 7, 1), "
1399 << "M = fieldFromInstruction(insn, 5, 1);\n";
1400 o.indent(Indentation)
1401 << "field_t Vn = fieldFromInstruction(insn, 16, 4), "
1402 << "Vm = fieldFromInstruction(insn, 0, 4);\n";
1403 o.indent(Indentation)
1404 << "return (N == M && Vn == Vm) ? "
1405 << Opcodes[0] << " /* " << name1 << " */ : "
1406 << Opcodes[1] << " /* " << name2 << " */ ;\n";
1407
1408 // Inserting the closing curly brace for this case block.
1409 --Indentation; --Indentation;
1410 o.indent(Indentation) << "}\n";
1411 ++Indentation; ++Indentation;
1412
1413 return true;
1414 }
1415 if (name1 == "LDR" && name2 == "LDRcp") {
1416 o.indent(Indentation)
1417 << "return " << Opcodes[0]
1418 << "; // Returning LDR for {LDR, LDRcp}\n";
1419 return true;
1420 }
1421 if (name1 == "tLDM" && name2 == "tLDM_UPD") {
1422 // Inserting the opening curly brace for this case block.
1423 --Indentation; --Indentation;
1424 o.indent(Indentation) << "{\n";
1425 ++Indentation; ++Indentation;
1426
1427 o.indent(Indentation)
1428 << "unsigned Rn = fieldFromInstruction(insn, 8, 3), "
1429 << "list = fieldFromInstruction(insn, 0, 8);\n";
1430 o.indent(Indentation)
1431 << "return ((list >> Rn) & 1) == 0 ? "
1432 << Opcodes[1] << " /* " << name2 << " */ : "
1433 << Opcodes[0] << " /* " << name1 << " */ ;\n";
1434
1435 // Inserting the closing curly brace for this case block.
1436 --Indentation; --Indentation;
1437 o.indent(Indentation) << "}\n";
1438 ++Indentation; ++Indentation;
1439
1440 return true;
1441 }
1442 if (sameStringExceptSuffix(name1, name2, "_UPD")) {
1443 o.indent(Indentation)
1444 << "return fieldFromInstruction(insn, 0, 4) == 15 ? " << Opcodes[0]
1445 << " /* " << name1 << " */ : " << Opcodes[1] << "/* " << name2
1446 << " */ ; // Advanced SIMD addressing mode\n";
1447 return true;
1448 }
1449
1450 // Otherwise, it does not belong to the known conflict sets.
1451 }
Johnny Chene0c74fb2010-04-09 19:31:33 +00001452
1453 // We don't know how to decode these instructions! Return 0 and dump the
1454 // conflict set!
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001455 o.indent(Indentation) << "return 0;" << " // Conflict set: ";
1456 for (int i = 0, N = Opcodes.size(); i < N; ++i) {
1457 o << nameWithID(Opcodes[i]);
1458 if (i < (N - 1))
1459 o << ", ";
1460 else
1461 o << '\n';
1462 }
Johnny Chene0c74fb2010-04-09 19:31:33 +00001463
1464 // Print out useful conflict information for postmortem analysis.
1465 errs() << "Decoding Conflict:\n";
1466
1467 dumpStack(errs(), "\t\t");
1468
1469 for (unsigned i = 0; i < Opcodes.size(); i++) {
1470 const std::string &Name = nameWithID(Opcodes[i]);
1471
1472 errs() << '\t' << Name << " ";
1473 dumpBits(errs(),
1474 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1475 errs() << '\n';
1476 }
1477
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001478 return true;
1479}
1480
1481
1482////////////////////////////////////////////
1483// //
1484// ARMDEBackend //
1485// (Helper class for ARMDecoderEmitter) //
1486// //
1487////////////////////////////////////////////
1488
1489class ARMDecoderEmitter::ARMDEBackend {
1490public:
1491 ARMDEBackend(ARMDecoderEmitter &frontend) :
1492 NumberedInstructions(),
1493 Opcodes(),
1494 Frontend(frontend),
1495 Target(),
1496 FC(NULL)
1497 {
1498 if (Target.getName() == "ARM")
1499 TargetName = TARGET_ARM;
1500 else {
1501 errs() << "Target name " << Target.getName() << " not recognized\n";
1502 assert(0 && "Unknown target");
1503 }
1504
1505 // Populate the instructions for our TargetName.
1506 populateInstructions();
1507 }
1508
1509 ~ARMDEBackend() {
1510 if (FC) {
1511 delete FC;
1512 FC = NULL;
1513 }
1514 }
1515
1516 void getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
1517 &NumberedInstructions) {
1518 // We must emit the PHI opcode first...
1519 std::string Namespace = Target.getInstNamespace();
1520 assert(!Namespace.empty() && "No instructions defined.");
1521
1522 NumberedInstructions = Target.getInstructionsByEnumValue();
1523 }
1524
1525 bool populateInstruction(const CodeGenInstruction &CGI, TARGET_NAME_t TN);
1526
1527 void populateInstructions();
1528
1529 // Emits disassembler code for instruction decoding. This delegates to the
1530 // FilterChooser instance to do the heavy lifting.
1531 void emit(raw_ostream &o);
1532
1533protected:
1534 std::vector<const CodeGenInstruction*> NumberedInstructions;
1535 std::vector<unsigned> Opcodes;
1536 // Special case for the ARM chip, which supports ARM and Thumb ISAs.
1537 // Opcodes2 will be populated with the Thumb opcodes.
1538 std::vector<unsigned> Opcodes2;
1539 ARMDecoderEmitter &Frontend;
1540 CodeGenTarget Target;
1541 FilterChooser *FC;
1542
1543 TARGET_NAME_t TargetName;
1544};
1545
1546bool ARMDecoderEmitter::ARMDEBackend::populateInstruction(
1547 const CodeGenInstruction &CGI, TARGET_NAME_t TN) {
1548 const Record &Def = *CGI.TheDef;
1549 const StringRef Name = Def.getName();
1550 uint8_t Form = getByteField(Def, "Form");
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001551
Johnny Chen1808e4d2010-04-09 21:01:02 +00001552 BitsInit &Bits = getBitsField(Def, "Inst");
1553
1554 // If all the bit positions are not specified; do not decode this instruction.
1555 // We are bound to fail! For proper disassembly, the well-known encoding bits
1556 // of the instruction must be fully specified.
1557 //
1558 // This also removes pseudo instructions from considerations of disassembly,
1559 // which is a better design and less fragile than the name matchings.
1560 if (Bits.allInComplete()) return false;
1561
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001562 if (TN == TARGET_ARM) {
1563 // FIXME: what about Int_MemBarrierV6 and Int_SyncBarrierV6?
1564 if ((Name != "Int_MemBarrierV7" && Name != "Int_SyncBarrierV7") &&
1565 Form == ARM_FORMAT_PSEUDO)
1566 return false;
1567 if (thumbInstruction(Form))
1568 return false;
1569 if (Name.find("CMPz") != std::string::npos /* ||
1570 Name.find("CMNz") != std::string::npos */)
1571 return false;
1572
1573 // Ignore pseudo instructions.
1574 if (Name == "BXr9" || Name == "BMOVPCRX" || Name == "BMOVPCRXr9")
1575 return false;
1576
Dale Johannesen51e28e62010-06-03 21:09:53 +00001577 // Tail calls are other patterns that generate existing instructions.
1578 if (Name == "TCRETURNdi" || Name == "TCRETURNdiND" ||
1579 Name == "TCRETURNri" || Name == "TCRETURNriND" ||
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001580 Name == "TAILJMPd" || Name == "TAILJMPdt" ||
1581 Name == "TAILJMPdND" || Name == "TAILJMPdNDt" ||
Dale Johannesen6470a112010-06-15 22:08:33 +00001582 Name == "TAILJMPr" || Name == "TAILJMPrND" ||
1583 Name == "MOVr_TC")
Dale Johannesen51e28e62010-06-03 21:09:53 +00001584 return false;
1585
Bob Wilsondbb350a2010-08-27 21:56:59 +00001586 // VLDMQ/VSTMQ can be handled with the more generic VLDMD/VSTMD.
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001587 if (Name == "VLDMQ" || Name == "VLDMQ_UPD" ||
1588 Name == "VSTMQ" || Name == "VSTMQ_UPD")
1589 return false;
1590
1591 //
1592 // The following special cases are for conflict resolutions.
1593 //
1594
1595 // NEON NLdStFrm conflict resolutions:
1596 //
1597 // 1. Ignore suffix "odd" and "odd_UPD", prefer the "even" register-
1598 // numbered ones which have the same Asm format string.
1599 // 2. Ignore VST2d64_UPD, which conflicts with VST1q64_UPD.
1600 // 3. Ignore VLD2d64_UPD, which conflicts with VLD1q64_UPD.
1601 // 4. Ignore VLD1q[_UPD], which conflicts with VLD1q64[_UPD].
1602 // 5. Ignore VST1q[_UPD], which conflicts with VST1q64[_UPD].
1603 if (Name.endswith("odd") || Name.endswith("odd_UPD") ||
1604 Name == "VST2d64_UPD" || Name == "VLD2d64_UPD" ||
1605 Name == "VLD1q" || Name == "VLD1q_UPD" ||
1606 Name == "VST1q" || Name == "VST1q_UPD")
1607 return false;
1608
1609 // RSCSri and RSCSrs set the 's' bit, but are not predicated. We are
1610 // better off using the generic RSCri and RSCrs instructions.
1611 if (Name == "RSCSri" || Name == "RSCSrs") return false;
1612
Jim Grosbach0e992192010-10-07 22:14:01 +00001613 // MOVCCr, MOVCCs, MOVCCi, MOVCCi16, FCYPScc, FCYPDcc, FNEGScc, and
1614 // FNEGDcc are used in the compiler to implement conditional moves.
Jim Grosbach3bbdcea2010-10-07 00:42:42 +00001615 // We can ignore them in favor of their more generic versions of
1616 // instructions. See also SDNode *ARMDAGToDAGISel::Select(SDValue Op).
Jim Grosbach0e992192010-10-07 22:14:01 +00001617 if (Name == "MOVCCr" || Name == "MOVCCs" || Name == "MOVCCi" ||
1618 Name == "MOVCCi16" || Name == "FCPYScc" || Name == "FCPYDcc" ||
1619 Name == "FNEGScc" || Name == "FNEGDcc")
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001620 return false;
1621
1622 // Ditto for VMOVDcc, VMOVScc, VNEGDcc, and VNEGScc.
1623 if (Name == "VMOVDcc" || Name == "VMOVScc" || Name == "VNEGDcc" ||
1624 Name == "VNEGScc")
1625 return false;
1626
1627 // Ignore the *_sfp instructions when decoding. They are used by the
1628 // compiler to implement scalar floating point operations using vector
1629 // operations in order to work around some performance issues.
1630 if (Name.find("_sfp") != std::string::npos) return false;
1631
Bill Wendling73fe34a2010-11-16 01:16:36 +00001632 // LDMIA_RET is a special case of LDM (Load Multiple) where the registers
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001633 // loaded include the PC, causing a branch to a loaded address. Ignore
Bill Wendling73fe34a2010-11-16 01:16:36 +00001634 // the LDMIA_RET instruction when decoding.
1635 if (Name == "LDMIA_RET") return false;
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001636
1637 // Bcc is in a more generic form than B. Ignore B when decoding.
1638 if (Name == "B") return false;
1639
1640 // Ignore the non-Darwin BL instructions and the TPsoft (TLS) instruction.
1641 if (Name == "BL" || Name == "BL_pred" || Name == "BLX" || Name == "BX" ||
1642 Name == "TPsoft")
1643 return false;
1644
1645 // Ignore VDUPf[d|q] instructions known to conflict with VDUP32[d-q] for
1646 // decoding. The instruction duplicates an element from an ARM core
1647 // register into every element of the destination vector. There is no
1648 // distinction between data types.
1649 if (Name == "VDUPfd" || Name == "VDUPfq") return false;
1650
1651 // A8-598: VEXT
1652 // Vector Extract extracts elements from the bottom end of the second
1653 // operand vector and the top end of the first, concatenates them and
1654 // places the result in the destination vector. The elements of the
1655 // vectors are treated as being 8-bit bitfields. There is no distinction
1656 // between data types. The size of the operation can be specified in
1657 // assembler as vext.size. If the value is 16, 32, or 64, the syntax is
1658 // a pseudo-instruction for a VEXT instruction specifying the equivalent
1659 // number of bytes.
1660 //
1661 // Variants VEXTd16, VEXTd32, VEXTd8, and VEXTdf are reduced to VEXTd8;
1662 // variants VEXTq16, VEXTq32, VEXTq8, and VEXTqf are reduced to VEXTq8.
1663 if (Name == "VEXTd16" || Name == "VEXTd32" || Name == "VEXTdf" ||
1664 Name == "VEXTq16" || Name == "VEXTq32" || Name == "VEXTqf")
1665 return false;
1666
1667 // Vector Reverse is similar to Vector Extract. There is no distinction
1668 // between data types, other than size.
1669 //
1670 // VREV64df is equivalent to VREV64d32.
1671 // VREV64qf is equivalent to VREV64q32.
1672 if (Name == "VREV64df" || Name == "VREV64qf") return false;
1673
Jim Grosbach4fc92e02010-10-06 21:17:07 +00001674 // VDUPLNfd is equivalent to VDUPLN32d.
1675 // VDUPLNfq is equivalent to VDUPLN32q.
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001676 // VLD1df is equivalent to VLD1d32.
1677 // VLD1qf is equivalent to VLD1q32.
1678 // VLD2d64 is equivalent to VLD1q64.
1679 // VST1df is equivalent to VST1d32.
1680 // VST1qf is equivalent to VST1q32.
1681 // VST2d64 is equivalent to VST1q64.
Jim Grosbach4fc92e02010-10-06 21:17:07 +00001682 if (Name == "VDUPLNfd" || Name == "VDUPLNfq" ||
1683 Name == "VLD1df" || Name == "VLD1qf" || Name == "VLD2d64" ||
1684 Name == "VST1df" || Name == "VST1qf" || Name == "VST2d64")
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001685 return false;
1686 } else if (TN == TARGET_THUMB) {
1687 if (!thumbInstruction(Form))
1688 return false;
1689
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001690 // On Darwin R9 is call-clobbered. Ignore the non-Darwin counterparts.
1691 if (Name == "tBL" || Name == "tBLXi" || Name == "tBLXr")
1692 return false;
1693
1694 // Ignore the TPsoft (TLS) instructions, which conflict with tBLr9.
1695 if (Name == "tTPsoft" || Name == "t2TPsoft")
1696 return false;
1697
1698 // Ignore tLEApcrel and tLEApcrelJT, prefer tADDrPCi.
1699 if (Name == "tLEApcrel" || Name == "tLEApcrelJT")
1700 return false;
1701
1702 // Ignore t2LEApcrel, prefer the generic t2ADD* for disassembly printing.
1703 if (Name == "t2LEApcrel")
1704 return false;
1705
1706 // Ignore tADDrSP, tADDspr, and tPICADD, prefer the generic tADDhirr.
1707 // Ignore t2SUBrSPs, prefer the t2SUB[S]r[r|s].
1708 // Ignore t2ADDrSPs, prefer the t2ADD[S]r[r|s].
Johnny Chen56a1afb2010-04-20 18:45:24 +00001709 // Ignore t2ADDrSPi/t2SUBrSPi, which have more generic couterparts.
1710 // Ignore t2ADDrSPi12/t2SUBrSPi12, which have more generic couterparts
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001711 if (Name == "tADDrSP" || Name == "tADDspr" || Name == "tPICADD" ||
Johnny Chen56a1afb2010-04-20 18:45:24 +00001712 Name == "t2SUBrSPs" || Name == "t2ADDrSPs" ||
1713 Name == "t2ADDrSPi" || Name == "t2SUBrSPi" ||
1714 Name == "t2ADDrSPi12" || Name == "t2SUBrSPi12")
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001715 return false;
1716
1717 // Ignore t2LDRDpci, prefer the generic t2LDRDi8, t2LDRD_PRE, t2LDRD_POST.
1718 if (Name == "t2LDRDpci")
1719 return false;
1720
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001721 // Resolve conflicts:
1722 //
1723 // tBfar conflicts with tBLr9
1724 // tCMNz conflicts with tCMN (with assembly format strings being equal)
Bill Wendling73fe34a2010-11-16 01:16:36 +00001725 // tPOP_RET/t2LDMIA_RET conflict with tPOP/t2LDM (ditto)
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001726 // tMOVCCi conflicts with tMOVi8
1727 // tMOVCCr conflicts with tMOVgpr2gpr
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001728 // tSpill conflicts with tSTRspi
1729 // tLDRcp conflicts with tLDRspi
1730 // tRestore conflicts with tLDRspi
1731 // t2LEApcrelJT conflicts with t2LEApcrel
Jim Grosbach0e992192010-10-07 22:14:01 +00001732 // t2MOVCCi16 conflicts with tMOVi16
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001733 if (Name == "tBfar" ||
1734 /* Name == "tCMNz" || */ Name == "tCMPzi8" || Name == "tCMPzr" ||
1735 Name == "tCMPzhir" || /* Name == "t2CMNzrr" || Name == "t2CMNzrs" ||
1736 Name == "t2CMNzri" || */ Name == "t2CMPzrr" || Name == "t2CMPzrs" ||
Bill Wendling73fe34a2010-11-16 01:16:36 +00001737 Name == "t2CMPzri" || Name == "tPOP_RET" || Name == "t2LDMIA_RET" ||
Jim Grosbach5ca66692010-11-29 22:37:40 +00001738 Name == "tMOVCCi" || Name == "tMOVCCr" ||
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001739 Name == "tSpill" || Name == "tLDRcp" || Name == "tRestore" ||
Jim Grosbach0e992192010-10-07 22:14:01 +00001740 Name == "t2LEApcrelJT" || Name == "t2MOVCCi16")
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001741 return false;
1742 }
1743
1744 // Dumps the instruction encoding format.
1745 switch (TargetName) {
1746 case TARGET_ARM:
1747 case TARGET_THUMB:
1748 DEBUG(errs() << Name << " " << stringForARMFormat((ARMFormat)Form));
1749 break;
1750 }
1751
1752 DEBUG({
1753 errs() << " ";
1754
1755 // Dumps the instruction encoding bits.
1756 dumpBits(errs(), Bits);
1757
1758 errs() << '\n';
1759
1760 // Dumps the list of operand info.
Chris Lattnerc240bb02010-11-01 04:03:32 +00001761 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1762 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001763 const std::string &OperandName = Info.Name;
1764 const Record &OperandDef = *Info.Rec;
1765
1766 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
1767 }
1768 });
1769
1770 return true;
1771}
1772
1773void ARMDecoderEmitter::ARMDEBackend::populateInstructions() {
1774 getInstructionsByEnumValue(NumberedInstructions);
1775
1776 uint16_t numUIDs = NumberedInstructions.size();
1777 uint16_t uid;
1778
1779 const char *instClass = NULL;
1780
1781 switch (TargetName) {
1782 case TARGET_ARM:
1783 instClass = "InstARM";
1784 break;
1785 default:
1786 assert(0 && "Unreachable code!");
1787 }
1788
1789 for (uid = 0; uid < numUIDs; uid++) {
1790 // filter out intrinsics
1791 if (!NumberedInstructions[uid]->TheDef->isSubClassOf(instClass))
1792 continue;
1793
1794 if (populateInstruction(*NumberedInstructions[uid], TargetName))
1795 Opcodes.push_back(uid);
1796 }
1797
1798 // Special handling for the ARM chip, which supports two modes of execution.
1799 // This branch handles the Thumb opcodes.
1800 if (TargetName == TARGET_ARM) {
1801 for (uid = 0; uid < numUIDs; uid++) {
1802 // filter out intrinsics
1803 if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM")
1804 && !NumberedInstructions[uid]->TheDef->isSubClassOf("InstThumb"))
1805 continue;
1806
1807 if (populateInstruction(*NumberedInstructions[uid], TARGET_THUMB))
1808 Opcodes2.push_back(uid);
1809 }
1810 }
1811}
1812
1813// Emits disassembler code for instruction decoding. This delegates to the
1814// FilterChooser instance to do the heavy lifting.
1815void ARMDecoderEmitter::ARMDEBackend::emit(raw_ostream &o) {
1816 switch (TargetName) {
1817 case TARGET_ARM:
1818 Frontend.EmitSourceFileHeader("ARM/Thumb Decoders", o);
1819 break;
1820 default:
1821 assert(0 && "Unreachable code!");
1822 }
1823
Michael J. Spencer1f6efa32010-11-29 18:16:10 +00001824 o << "#include \"llvm/Support/DataTypes.h\"\n";
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001825 o << "#include <assert.h>\n";
1826 o << '\n';
1827 o << "namespace llvm {\n\n";
1828
1829 FilterChooser::setTargetName(TargetName);
1830
1831 switch (TargetName) {
1832 case TARGET_ARM: {
1833 // Emit common utility and ARM ISA decoder.
1834 FC = new FilterChooser(NumberedInstructions, Opcodes);
1835 // Reset indentation level.
1836 unsigned Indentation = 0;
1837 FC->emitTop(o, Indentation);
1838 delete FC;
1839
1840 // Emit Thumb ISA decoder as well.
1841 FilterChooser::setTargetName(TARGET_THUMB);
1842 FC = new FilterChooser(NumberedInstructions, Opcodes2);
1843 // Reset indentation level.
1844 Indentation = 0;
1845 FC->emitBot(o, Indentation);
1846 break;
1847 }
1848 default:
1849 assert(0 && "Unreachable code!");
1850 }
1851
1852 o << "\n} // End llvm namespace \n";
1853}
1854
1855/////////////////////////
1856// Backend interface //
1857/////////////////////////
1858
1859void ARMDecoderEmitter::initBackend()
1860{
1861 Backend = new ARMDEBackend(*this);
1862}
1863
1864void ARMDecoderEmitter::run(raw_ostream &o)
1865{
1866 Backend->emit(o);
1867}
1868
1869void ARMDecoderEmitter::shutdownBackend()
1870{
1871 delete Backend;
1872 Backend = NULL;
1873}