blob: 3721ed41b2a6d4f67be05694cb7fe3ba56812fd5 [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().
David Greened4a90662011-07-11 18:25:51 +0000117static uint8_t byteFromBitsInit(const BitsInit &init) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000118 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++) {
David Greened4a90662011-07-11 18:25:51 +0000128 if (static_cast<const BitInit*>(init.getBit(index))->getValue())
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000129 ret |= mask;
130
131 mask <<= 1;
132 }
133
134 return ret;
135}
136
137static uint8_t getByteField(const Record &def, const char *str) {
David Greened4a90662011-07-11 18:25:51 +0000138 const BitsInit *bits = def.getValueAsBitsInit(str);
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000139 return byteFromBitsInit(*bits);
140}
141
David Greened4a90662011-07-11 18:25:51 +0000142static const BitsInit &getBitsField(const Record &def, const char *str) {
143 const BitsInit *bits = def.getValueAsBitsInit(str);
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000144 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}
David Greened4a90662011-07-11 18:25:51 +0000186static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) {
187 if (const BitInit *bit = dynamic_cast<const BitInit*>(bits.getBit(index)))
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000188 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.
David Greened4a90662011-07-11 18:25:51 +0000194static void dumpBits(raw_ostream &o, const BitsInit &bits) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000195 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.
Owen Andersond8c87882011-02-18 21:51:29 +0000224class ARMFilterChooser;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000225
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.
Owen Andersond8c87882011-02-18 21:51:29 +0000265class ARMFilter {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000266protected:
Owen Andersond8c87882011-02-18 21:51:29 +0000267 ARMFilterChooser *Owner; // points to the FilterChooser who owns this filter
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000268 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.
Owen Andersond8c87882011-02-18 21:51:29 +0000279 std::map<unsigned, ARMFilterChooser*> FilterChooserMap;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000280
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.
Owen Andersond8c87882011-02-18 21:51:29 +0000299 ARMFilterChooser &getVariableFC() {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000300 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
Owen Andersond8c87882011-02-18 21:51:29 +0000305 ARMFilter(const ARMFilter &f);
306 ARMFilter(ARMFilterChooser &owner, unsigned startBit, unsigned numBits,
307 bool mixed);
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000308
Owen Andersond8c87882011-02-18 21:51:29 +0000309 ~ARMFilter();
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000310
311 // Divides the decoding task into sub tasks and delegates them to the
312 // inferior FilterChooser's.
313 //
314 // A special case arises when there's only one entry in the filtered
315 // instructions. In order to unambiguously decode the singleton, we need to
316 // match the remaining undecoded encoding bits against the singleton.
317 void recurse();
318
319 // Emit code to decode instructions given a segment or segments of bits.
320 void emit(raw_ostream &o, unsigned &Indentation);
321
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
326
327// These are states of our finite state machines used in FilterChooser's
328// filterProcessor() which produces the filter candidates to use.
329typedef enum {
330 ATTR_NONE,
331 ATTR_FILTERED,
332 ATTR_ALL_SET,
333 ATTR_ALL_UNSET,
334 ATTR_MIXED
335} bitAttr_t;
336
Owen Andersond8c87882011-02-18 21:51:29 +0000337/// ARMFilterChooser - FilterChooser chooses the best filter among a set of Filters
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000338/// in order to perform the decoding of instructions at the current level.
339///
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.
344///
345/// Once a filter has been chosen, it is called upon to divide the decoding task
346/// into sub-tasks and delegates them to its inferior FilterChoosers for further
347/// processings.
348///
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.
Owen Andersond8c87882011-02-18 21:51:29 +0000352class ARMFilterChooser {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000353 static TARGET_NAME_t TargetName;
354
355protected:
Owen Andersond8c87882011-02-18 21:51:29 +0000356 friend class ARMFilter;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000357
358 // Vector of codegen instructions to choose our filter.
359 const std::vector<const CodeGenInstruction*> &AllInstructions;
360
361 // Vector of uid's for this filter chooser to work on.
362 const std::vector<unsigned> Opcodes;
363
364 // Vector of candidate filters.
Owen Andersond8c87882011-02-18 21:51:29 +0000365 std::vector<ARMFilter> Filters;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000366
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];
370
371 // Links to the FilterChooser above us in the decoding tree.
Owen Andersond8c87882011-02-18 21:51:29 +0000372 ARMFilterChooser *Parent;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000373
374 // Index of the best filter from Filters.
375 int BestIndex;
376
377public:
378 static void setTargetName(TARGET_NAME_t tn) { TargetName = tn; }
379
Owen Andersond8c87882011-02-18 21:51:29 +0000380 ARMFilterChooser(const ARMFilterChooser &FC) :
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000381 AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
382 Filters(FC.Filters), Parent(FC.Parent), BestIndex(FC.BestIndex) {
383 memcpy(FilterBitValues, FC.FilterBitValues, sizeof(FilterBitValues));
384 }
385
Owen Andersond8c87882011-02-18 21:51:29 +0000386 ARMFilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000387 const std::vector<unsigned> &IDs) :
388 AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(NULL),
389 BestIndex(-1) {
390 for (unsigned i = 0; i < BIT_WIDTH; ++i)
391 FilterBitValues[i] = BIT_UNFILTERED;
392
393 doFilter();
394 }
395
Owen Andersond8c87882011-02-18 21:51:29 +0000396 ARMFilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
397 const std::vector<unsigned> &IDs,
398 bit_value_t (&ParentFilterBitValues)[BIT_WIDTH],
399 ARMFilterChooser &parent) :
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000400 AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(&parent),
401 BestIndex(-1) {
402 for (unsigned i = 0; i < BIT_WIDTH; ++i)
403 FilterBitValues[i] = ParentFilterBitValues[i];
404
405 doFilter();
406 }
407
408 // The top level filter chooser has NULL as its parent.
409 bool isTopLevel() { return Parent == NULL; }
410
411 // This provides an opportunity for target specific code emission.
412 void emitTopHook(raw_ostream &o);
413
414 // Emit the top level typedef and decodeInstruction() function.
415 void emitTop(raw_ostream &o, unsigned &Indentation);
416
417 // This provides an opportunity for target specific code emission after
418 // emitTop().
419 void emitBot(raw_ostream &o, unsigned &Indentation);
420
421protected:
422 // Populates the insn given the uid.
423 void insnWithID(insn_t &Insn, unsigned Opcode) const {
Jim Grosbach806fcc02011-07-06 21:33:38 +0000424 if (AllInstructions[Opcode]->isPseudo)
425 return;
426
David Greened4a90662011-07-11 18:25:51 +0000427 const BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef,
428 "Inst");
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000429
430 for (unsigned i = 0; i < BIT_WIDTH; ++i)
431 Insn[i] = bitFromBits(Bits, i);
432
433 // Set Inst{21} to 1 (wback) when IndexModeBits == IndexModeUpd.
Owen Andersond8c87882011-02-18 21:51:29 +0000434 Record *R = AllInstructions[Opcode]->TheDef;
435 if (R->getValue("IndexModeBits") &&
436 getByteField(*R, "IndexModeBits") == IndexModeUpd)
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000437 Insn[21] = BIT_TRUE;
438 }
439
440 // Returns the record name.
441 const std::string &nameWithID(unsigned Opcode) const {
442 return AllInstructions[Opcode]->TheDef->getName();
443 }
444
445 // Populates the field of the insn given the start position and the number of
446 // consecutive bits to scan for.
447 //
448 // Returns false if there exists any uninitialized bit value in the range.
449 // Returns true, otherwise.
450 bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
451 unsigned NumBits) const;
452
453 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
454 /// filter array as a series of chars.
455 void dumpFilterArray(raw_ostream &o, bit_value_t (&filter)[BIT_WIDTH]);
456
457 /// dumpStack - dumpStack traverses the filter chooser chain and calls
458 /// dumpFilterArray on each filter chooser up to the top level one.
459 void dumpStack(raw_ostream &o, const char *prefix);
460
Owen Andersond8c87882011-02-18 21:51:29 +0000461 ARMFilter &bestFilter() {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000462 assert(BestIndex != -1 && "BestIndex not set");
463 return Filters[BestIndex];
464 }
465
466 // Called from Filter::recurse() when singleton exists. For debug purpose.
467 void SingletonExists(unsigned Opc);
468
469 bool PositionFiltered(unsigned i) {
470 return ValueSet(FilterBitValues[i]);
471 }
472
473 // Calculates the island(s) needed to decode the instruction.
474 // This returns a lit of undecoded bits of an instructions, for example,
475 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
476 // decoded bits in order to verify that the instruction matches the Opcode.
477 unsigned getIslands(std::vector<unsigned> &StartBits,
478 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
479 insn_t &Insn);
480
481 // The purpose of this function is for the API client to detect possible
482 // Load/Store Coprocessor instructions. If the coprocessor number is of
483 // the instruction is either 10 or 11, the decoder should not report the
484 // instruction as LDC/LDC2/STC/STC2, but should match against Advanced SIMD or
485 // VFP instructions.
486 bool LdStCopEncoding1(unsigned Opc) {
487 const std::string &Name = nameWithID(Opc);
488 if (Name == "LDC_OFFSET" || Name == "LDC_OPTION" ||
489 Name == "LDC_POST" || Name == "LDC_PRE" ||
490 Name == "LDCL_OFFSET" || Name == "LDCL_OPTION" ||
491 Name == "LDCL_POST" || Name == "LDCL_PRE" ||
492 Name == "STC_OFFSET" || Name == "STC_OPTION" ||
493 Name == "STC_POST" || Name == "STC_PRE" ||
494 Name == "STCL_OFFSET" || Name == "STCL_OPTION" ||
495 Name == "STCL_POST" || Name == "STCL_PRE")
496 return true;
497 else
498 return false;
499 }
500
501 // Emits code to decode the singleton. Return true if we have matched all the
502 // well-known bits.
503 bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc);
504
505 // Emits code to decode the singleton, and then to decode the rest.
Owen Andersond8c87882011-02-18 21:51:29 +0000506 void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
507 ARMFilter &Best);
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000508
509 // Assign a single filter and run with it.
Owen Andersond8c87882011-02-18 21:51:29 +0000510 void runSingleFilter(ARMFilterChooser &owner, unsigned startBit,
511 unsigned numBit, bool mixed);
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000512
513 // reportRegion is a helper function for filterProcessor to mark a region as
514 // eligible for use as a filter region.
515 void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
516 bool AllowMixed);
517
518 // FilterProcessor scans the well-known encoding bits of the instructions and
519 // builds up a list of candidate filters. It chooses the best filter and
520 // recursively descends down the decoding tree.
521 bool filterProcessor(bool AllowMixed, bool Greedy = true);
522
523 // Decides on the best configuration of filter(s) to use in order to decode
524 // the instructions. A conflict of instructions may occur, in which case we
525 // dump the conflict set to the standard error.
526 void doFilter();
527
528 // Emits code to decode our share of instructions. Returns true if the
529 // emitted code causes a return, which occurs if we know how to decode
530 // the instruction at this level or the instruction is not decodeable.
531 bool emit(raw_ostream &o, unsigned &Indentation);
532};
533
534///////////////////////////
535// //
536// Filter Implmenetation //
537// //
538///////////////////////////
539
Owen Andersond8c87882011-02-18 21:51:29 +0000540ARMFilter::ARMFilter(const ARMFilter &f) :
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000541 Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
542 FilteredInstructions(f.FilteredInstructions),
543 VariableInstructions(f.VariableInstructions),
544 FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered),
545 LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) {
546}
547
Owen Andersond8c87882011-02-18 21:51:29 +0000548ARMFilter::ARMFilter(ARMFilterChooser &owner, unsigned startBit, unsigned numBits,
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000549 bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits),
550 Mixed(mixed) {
551 assert(StartBit + NumBits - 1 < BIT_WIDTH);
552
553 NumFiltered = 0;
554 LastOpcFiltered = 0;
555 NumVariable = 0;
556
557 for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
558 insn_t Insn;
559
560 // Populates the insn given the uid.
561 Owner->insnWithID(Insn, Owner->Opcodes[i]);
562
563 uint64_t Field;
564 // Scans the segment for possibly well-specified encoding bits.
565 bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
566
567 if (ok) {
568 // The encoding bits are well-known. Lets add the uid of the
569 // instruction into the bucket keyed off the constant field value.
570 LastOpcFiltered = Owner->Opcodes[i];
571 FilteredInstructions[Field].push_back(LastOpcFiltered);
572 ++NumFiltered;
573 } else {
574 // Some of the encoding bit(s) are unspecfied. This contributes to
575 // one additional member of "Variable" instructions.
576 VariableInstructions.push_back(Owner->Opcodes[i]);
577 ++NumVariable;
578 }
579 }
580
581 assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
582 && "Filter returns no instruction categories");
583}
584
Owen Andersond8c87882011-02-18 21:51:29 +0000585ARMFilter::~ARMFilter() {
586 std::map<unsigned, ARMFilterChooser*>::iterator filterIterator;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000587 for (filterIterator = FilterChooserMap.begin();
588 filterIterator != FilterChooserMap.end();
589 filterIterator++) {
590 delete filterIterator->second;
591 }
592}
593
594// Divides the decoding task into sub tasks and delegates them to the
595// inferior FilterChooser's.
596//
597// A special case arises when there's only one entry in the filtered
598// instructions. In order to unambiguously decode the singleton, we need to
599// match the remaining undecoded encoding bits against the singleton.
Owen Andersond8c87882011-02-18 21:51:29 +0000600void ARMFilter::recurse() {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000601 std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator;
602
603 bit_value_t BitValueArray[BIT_WIDTH];
604 // Starts by inheriting our parent filter chooser's filter bit values.
Johnny Chen2d16a672010-04-08 21:23:54 +0000605 memcpy(BitValueArray, Owner->FilterBitValues, sizeof(BitValueArray));
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000606
607 unsigned bitIndex;
608
609 if (VariableInstructions.size()) {
610 // Conservatively marks each segment position as BIT_UNSET.
611 for (bitIndex = 0; bitIndex < NumBits; bitIndex++)
612 BitValueArray[StartBit + bitIndex] = BIT_UNSET;
613
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000614 // Delegates to an inferior filter chooser for further processing on this
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000615 // group of instructions whose segment values are variable.
Owen Andersond8c87882011-02-18 21:51:29 +0000616 FilterChooserMap.insert(std::pair<unsigned, ARMFilterChooser*>(
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000617 (unsigned)-1,
Owen Andersond8c87882011-02-18 21:51:29 +0000618 new ARMFilterChooser(Owner->AllInstructions,
619 VariableInstructions,
620 BitValueArray,
621 *Owner)
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000622 ));
623 }
624
625 // No need to recurse for a singleton filtered instruction.
626 // See also Filter::emit().
627 if (getNumFiltered() == 1) {
628 //Owner->SingletonExists(LastOpcFiltered);
629 assert(FilterChooserMap.size() == 1);
630 return;
631 }
Johnny Chen3c500e62010-04-07 20:53:12 +0000632
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000633 // Otherwise, create sub choosers.
634 for (mapIterator = FilteredInstructions.begin();
635 mapIterator != FilteredInstructions.end();
636 mapIterator++) {
637
638 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
639 for (bitIndex = 0; bitIndex < NumBits; bitIndex++) {
Benjamin Kramer454c4ce2010-04-08 15:25:57 +0000640 if (mapIterator->first & (1ULL << bitIndex))
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000641 BitValueArray[StartBit + bitIndex] = BIT_TRUE;
642 else
643 BitValueArray[StartBit + bitIndex] = BIT_FALSE;
644 }
645
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000646 // Delegates to an inferior filter chooser for further processing on this
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000647 // category of instructions.
Owen Andersond8c87882011-02-18 21:51:29 +0000648 FilterChooserMap.insert(std::pair<unsigned, ARMFilterChooser*>(
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000649 mapIterator->first,
Owen Andersond8c87882011-02-18 21:51:29 +0000650 new ARMFilterChooser(Owner->AllInstructions,
651 mapIterator->second,
652 BitValueArray,
653 *Owner)
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000654 ));
655 }
656}
657
658// Emit code to decode instructions given a segment or segments of bits.
Owen Andersond8c87882011-02-18 21:51:29 +0000659void ARMFilter::emit(raw_ostream &o, unsigned &Indentation) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000660 o.indent(Indentation) << "// Check Inst{";
661
662 if (NumBits > 1)
663 o << (StartBit + NumBits - 1) << '-';
664
665 o << StartBit << "} ...\n";
666
667 o.indent(Indentation) << "switch (fieldFromInstruction(insn, "
668 << StartBit << ", " << NumBits << ")) {\n";
669
Owen Andersond8c87882011-02-18 21:51:29 +0000670 std::map<unsigned, ARMFilterChooser*>::iterator filterIterator;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000671
672 bool DefaultCase = false;
673 for (filterIterator = FilterChooserMap.begin();
674 filterIterator != FilterChooserMap.end();
675 filterIterator++) {
676
677 // Field value -1 implies a non-empty set of variable instructions.
678 // See also recurse().
679 if (filterIterator->first == (unsigned)-1) {
680 DefaultCase = true;
681
682 o.indent(Indentation) << "default:\n";
683 o.indent(Indentation) << " break; // fallthrough\n";
684
685 // Closing curly brace for the switch statement.
686 // This is unconventional because we want the default processing to be
687 // performed for the fallthrough cases as well, i.e., when the "cases"
688 // did not prove a decoded instruction.
689 o.indent(Indentation) << "}\n";
690
691 } else
692 o.indent(Indentation) << "case " << filterIterator->first << ":\n";
693
694 // We arrive at a category of instructions with the same segment value.
695 // Now delegate to the sub filter chooser for further decodings.
696 // The case may fallthrough, which happens if the remaining well-known
697 // encoding bits do not match exactly.
698 if (!DefaultCase) { ++Indentation; ++Indentation; }
699
700 bool finished = filterIterator->second->emit(o, Indentation);
701 // For top level default case, there's no need for a break statement.
702 if (Owner->isTopLevel() && DefaultCase)
703 break;
704 if (!finished)
705 o.indent(Indentation) << "break;\n";
706
707 if (!DefaultCase) { --Indentation; --Indentation; }
708 }
709
710 // If there is no default case, we still need to supply a closing brace.
711 if (!DefaultCase) {
712 // Closing curly brace for the switch statement.
713 o.indent(Indentation) << "}\n";
714 }
715}
716
717// Returns the number of fanout produced by the filter. More fanout implies
718// the filter distinguishes more categories of instructions.
Owen Andersond8c87882011-02-18 21:51:29 +0000719unsigned ARMFilter::usefulness() const {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000720 if (VariableInstructions.size())
721 return FilteredInstructions.size();
722 else
723 return FilteredInstructions.size() + 1;
724}
725
726//////////////////////////////////
727// //
728// Filterchooser Implementation //
729// //
730//////////////////////////////////
731
732// Define the symbol here.
Owen Andersond8c87882011-02-18 21:51:29 +0000733TARGET_NAME_t ARMFilterChooser::TargetName;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000734
735// This provides an opportunity for target specific code emission.
Owen Andersond8c87882011-02-18 21:51:29 +0000736void ARMFilterChooser::emitTopHook(raw_ostream &o) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000737 if (TargetName == TARGET_ARM) {
738 // Emit code that references the ARMFormat data type.
739 o << "static const ARMFormat ARMFormats[] = {\n";
740 for (unsigned i = 0, e = AllInstructions.size(); i != e; ++i) {
741 const Record &Def = *(AllInstructions[i]->TheDef);
742 const std::string &Name = Def.getName();
743 if (Def.isSubClassOf("InstARM") || Def.isSubClassOf("InstThumb"))
744 o.indent(2) <<
745 stringForARMFormat((ARMFormat)getByteField(Def, "Form"));
746 else
747 o << " ARM_FORMAT_NA";
748
749 o << ",\t// Inst #" << i << " = " << Name << '\n';
750 }
751 o << " ARM_FORMAT_NA\t// Unreachable.\n";
752 o << "};\n\n";
753 }
754}
755
756// Emit the top level typedef and decodeInstruction() function.
Owen Andersond8c87882011-02-18 21:51:29 +0000757void ARMFilterChooser::emitTop(raw_ostream &o, unsigned &Indentation) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000758 // Run the target specific emit hook.
759 emitTopHook(o);
760
761 switch (BIT_WIDTH) {
762 case 8:
763 o.indent(Indentation) << "typedef uint8_t field_t;\n";
764 break;
765 case 16:
766 o.indent(Indentation) << "typedef uint16_t field_t;\n";
767 break;
768 case 32:
769 o.indent(Indentation) << "typedef uint32_t field_t;\n";
770 break;
771 case 64:
772 o.indent(Indentation) << "typedef uint64_t field_t;\n";
773 break;
774 default:
775 assert(0 && "Unexpected instruction size!");
776 }
777
778 o << '\n';
779
780 o.indent(Indentation) << "static field_t " <<
781 "fieldFromInstruction(field_t insn, unsigned startBit, unsigned numBits)\n";
782
783 o.indent(Indentation) << "{\n";
784
785 ++Indentation; ++Indentation;
786 o.indent(Indentation) << "assert(startBit + numBits <= " << BIT_WIDTH
787 << " && \"Instruction field out of bounds!\");\n";
788 o << '\n';
789 o.indent(Indentation) << "field_t fieldMask;\n";
790 o << '\n';
791 o.indent(Indentation) << "if (numBits == " << BIT_WIDTH << ")\n";
792
793 ++Indentation; ++Indentation;
794 o.indent(Indentation) << "fieldMask = (field_t)-1;\n";
795 --Indentation; --Indentation;
796
797 o.indent(Indentation) << "else\n";
798
799 ++Indentation; ++Indentation;
800 o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n";
801 --Indentation; --Indentation;
802
803 o << '\n';
804 o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n";
805 --Indentation; --Indentation;
806
807 o.indent(Indentation) << "}\n";
808
809 o << '\n';
810
Jim Grosbachbb168242010-10-08 18:13:57 +0000811 o.indent(Indentation) <<"static uint16_t decodeInstruction(field_t insn) {\n";
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000812
813 ++Indentation; ++Indentation;
814 // Emits code to decode the instructions.
815 emit(o, Indentation);
816
817 o << '\n';
818 o.indent(Indentation) << "return 0;\n";
819 --Indentation; --Indentation;
820
821 o.indent(Indentation) << "}\n";
822
823 o << '\n';
824}
825
826// This provides an opportunity for target specific code emission after
827// emitTop().
Owen Andersond8c87882011-02-18 21:51:29 +0000828void ARMFilterChooser::emitBot(raw_ostream &o, unsigned &Indentation) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000829 if (TargetName != TARGET_THUMB) return;
830
831 // Emit code that decodes the Thumb ISA.
832 o.indent(Indentation)
833 << "static uint16_t decodeThumbInstruction(field_t insn) {\n";
834
835 ++Indentation; ++Indentation;
836
837 // Emits code to decode the instructions.
838 emit(o, Indentation);
839
840 o << '\n';
841 o.indent(Indentation) << "return 0;\n";
842
843 --Indentation; --Indentation;
844
845 o.indent(Indentation) << "}\n";
846}
847
848// Populates the field of the insn given the start position and the number of
849// consecutive bits to scan for.
850//
851// Returns false if and on the first uninitialized bit value encountered.
852// Returns true, otherwise.
Owen Andersond8c87882011-02-18 21:51:29 +0000853bool ARMFilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000854 unsigned StartBit, unsigned NumBits) const {
855 Field = 0;
856
857 for (unsigned i = 0; i < NumBits; ++i) {
858 if (Insn[StartBit + i] == BIT_UNSET)
859 return false;
860
861 if (Insn[StartBit + i] == BIT_TRUE)
Benjamin Kramer454c4ce2010-04-08 15:25:57 +0000862 Field = Field | (1ULL << i);
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000863 }
864
865 return true;
866}
867
868/// dumpFilterArray - dumpFilterArray prints out debugging info for the given
869/// filter array as a series of chars.
Owen Andersond8c87882011-02-18 21:51:29 +0000870void ARMFilterChooser::dumpFilterArray(raw_ostream &o,
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000871 bit_value_t (&filter)[BIT_WIDTH]) {
872 unsigned bitIndex;
873
874 for (bitIndex = BIT_WIDTH; bitIndex > 0; bitIndex--) {
875 switch (filter[bitIndex - 1]) {
876 case BIT_UNFILTERED:
877 o << ".";
878 break;
879 case BIT_UNSET:
880 o << "_";
881 break;
882 case BIT_TRUE:
883 o << "1";
884 break;
885 case BIT_FALSE:
886 o << "0";
887 break;
888 }
889 }
890}
891
892/// dumpStack - dumpStack traverses the filter chooser chain and calls
893/// dumpFilterArray on each filter chooser up to the top level one.
Owen Andersond8c87882011-02-18 21:51:29 +0000894void ARMFilterChooser::dumpStack(raw_ostream &o, const char *prefix) {
895 ARMFilterChooser *current = this;
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000896
897 while (current) {
898 o << prefix;
899 dumpFilterArray(o, current->FilterBitValues);
900 o << '\n';
901 current = current->Parent;
902 }
903}
904
905// Called from Filter::recurse() when singleton exists. For debug purpose.
Owen Andersond8c87882011-02-18 21:51:29 +0000906void ARMFilterChooser::SingletonExists(unsigned Opc) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000907 insn_t Insn0;
908 insnWithID(Insn0, Opc);
909
910 errs() << "Singleton exists: " << nameWithID(Opc)
911 << " with its decoding dominating ";
912 for (unsigned i = 0; i < Opcodes.size(); ++i) {
913 if (Opcodes[i] == Opc) continue;
914 errs() << nameWithID(Opcodes[i]) << ' ';
915 }
916 errs() << '\n';
917
918 dumpStack(errs(), "\t\t");
919 for (unsigned i = 0; i < Opcodes.size(); i++) {
920 const std::string &Name = nameWithID(Opcodes[i]);
921
922 errs() << '\t' << Name << " ";
923 dumpBits(errs(),
924 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
925 errs() << '\n';
926 }
927}
928
929// Calculates the island(s) needed to decode the instruction.
930// This returns a list of undecoded bits of an instructions, for example,
931// Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
932// decoded bits in order to verify that the instruction matches the Opcode.
Owen Andersond8c87882011-02-18 21:51:29 +0000933unsigned ARMFilterChooser::getIslands(std::vector<unsigned> &StartBits,
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000934 std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
935 insn_t &Insn) {
936 unsigned Num, BitNo;
937 Num = BitNo = 0;
938
939 uint64_t FieldVal = 0;
940
941 // 0: Init
942 // 1: Water (the bit value does not affect decoding)
943 // 2: Island (well-known bit value needed for decoding)
944 int State = 0;
945 int Val = -1;
946
947 for (unsigned i = 0; i < BIT_WIDTH; ++i) {
948 Val = Value(Insn[i]);
949 bool Filtered = PositionFiltered(i);
950 switch (State) {
951 default:
952 assert(0 && "Unreachable code!");
953 break;
954 case 0:
955 case 1:
956 if (Filtered || Val == -1)
957 State = 1; // Still in Water
958 else {
959 State = 2; // Into the Island
960 BitNo = 0;
961 StartBits.push_back(i);
962 FieldVal = Val;
963 }
964 break;
965 case 2:
966 if (Filtered || Val == -1) {
967 State = 1; // Into the Water
968 EndBits.push_back(i - 1);
969 FieldVals.push_back(FieldVal);
970 ++Num;
971 } else {
972 State = 2; // Still in Island
973 ++BitNo;
974 FieldVal = FieldVal | Val << BitNo;
975 }
976 break;
977 }
978 }
979 // If we are still in Island after the loop, do some housekeeping.
980 if (State == 2) {
981 EndBits.push_back(BIT_WIDTH - 1);
982 FieldVals.push_back(FieldVal);
983 ++Num;
984 }
985
986 assert(StartBits.size() == Num && EndBits.size() == Num &&
987 FieldVals.size() == Num);
988 return Num;
989}
990
991// Emits code to decode the singleton. Return true if we have matched all the
992// well-known bits.
Owen Andersond8c87882011-02-18 21:51:29 +0000993bool ARMFilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
Johnny Chenb68a3ee2010-04-02 22:27:38 +0000994 unsigned Opc) {
995 std::vector<unsigned> StartBits;
996 std::vector<unsigned> EndBits;
997 std::vector<uint64_t> FieldVals;
998 insn_t Insn;
999 insnWithID(Insn, Opc);
1000
1001 // This provides a good opportunity to check for possible Ld/St Coprocessor
1002 // Opcode and escapes if the coproc # is either 10 or 11. It is a NEON/VFP
1003 // instruction is disguise.
1004 if (TargetName == TARGET_ARM && LdStCopEncoding1(Opc)) {
1005 o.indent(Indentation);
1006 // A8.6.51 & A8.6.188
1007 // If coproc = 0b101?, i.e, slice(insn, 11, 8) = 10 or 11, escape.
1008 o << "if (fieldFromInstruction(insn, 9, 3) == 5) break; // fallthrough\n";
1009 }
1010
1011 // Look for islands of undecoded bits of the singleton.
1012 getIslands(StartBits, EndBits, FieldVals, Insn);
1013
1014 unsigned Size = StartBits.size();
1015 unsigned I, NumBits;
1016
1017 // If we have matched all the well-known bits, just issue a return.
1018 if (Size == 0) {
1019 o.indent(Indentation) << "return " << Opc << "; // " << nameWithID(Opc)
1020 << '\n';
1021 return true;
1022 }
1023
1024 // Otherwise, there are more decodings to be done!
1025
1026 // Emit code to match the island(s) for the singleton.
1027 o.indent(Indentation) << "// Check ";
1028
1029 for (I = Size; I != 0; --I) {
1030 o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} ";
1031 if (I > 1)
1032 o << "&& ";
1033 else
1034 o << "for singleton decoding...\n";
1035 }
1036
1037 o.indent(Indentation) << "if (";
1038
1039 for (I = Size; I != 0; --I) {
1040 NumBits = EndBits[I-1] - StartBits[I-1] + 1;
1041 o << "fieldFromInstruction(insn, " << StartBits[I-1] << ", " << NumBits
1042 << ") == " << FieldVals[I-1];
1043 if (I > 1)
1044 o << " && ";
1045 else
1046 o << ")\n";
1047 }
1048
1049 o.indent(Indentation) << " return " << Opc << "; // " << nameWithID(Opc)
1050 << '\n';
1051
1052 return false;
1053}
1054
1055// Emits code to decode the singleton, and then to decode the rest.
Owen Andersond8c87882011-02-18 21:51:29 +00001056void ARMFilterChooser::emitSingletonDecoder(raw_ostream &o,
1057 unsigned &Indentation,
1058 ARMFilter &Best) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001059
1060 unsigned Opc = Best.getSingletonOpc();
1061
1062 emitSingletonDecoder(o, Indentation, Opc);
1063
1064 // Emit code for the rest.
1065 o.indent(Indentation) << "else\n";
1066
1067 Indentation += 2;
1068 Best.getVariableFC().emit(o, Indentation);
1069 Indentation -= 2;
1070}
1071
1072// Assign a single filter and run with it. Top level API client can initialize
1073// with a single filter to start the filtering process.
Owen Andersond8c87882011-02-18 21:51:29 +00001074void ARMFilterChooser::runSingleFilter(ARMFilterChooser &owner,
1075 unsigned startBit,
1076 unsigned numBit, bool mixed) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001077 Filters.clear();
Owen Andersond8c87882011-02-18 21:51:29 +00001078 ARMFilter F(*this, startBit, numBit, true);
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001079 Filters.push_back(F);
1080 BestIndex = 0; // Sole Filter instance to choose from.
1081 bestFilter().recurse();
1082}
1083
1084// reportRegion is a helper function for filterProcessor to mark a region as
1085// eligible for use as a filter region.
Owen Andersond8c87882011-02-18 21:51:29 +00001086void ARMFilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1087 unsigned BitIndex, bool AllowMixed) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001088 if (RA == ATTR_MIXED && AllowMixed)
Owen Andersond8c87882011-02-18 21:51:29 +00001089 Filters.push_back(ARMFilter(*this, StartBit, BitIndex - StartBit, true));
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001090 else if (RA == ATTR_ALL_SET && !AllowMixed)
Owen Andersond8c87882011-02-18 21:51:29 +00001091 Filters.push_back(ARMFilter(*this, StartBit, BitIndex - StartBit, false));
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001092}
1093
1094// FilterProcessor scans the well-known encoding bits of the instructions and
1095// builds up a list of candidate filters. It chooses the best filter and
1096// recursively descends down the decoding tree.
Owen Andersond8c87882011-02-18 21:51:29 +00001097bool ARMFilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001098 Filters.clear();
1099 BestIndex = -1;
1100 unsigned numInstructions = Opcodes.size();
1101
1102 assert(numInstructions && "Filter created with no instructions");
1103
1104 // No further filtering is necessary.
1105 if (numInstructions == 1)
1106 return true;
1107
1108 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
1109 // instructions is 3.
1110 if (AllowMixed && !Greedy) {
1111 assert(numInstructions == 3);
1112
1113 for (unsigned i = 0; i < Opcodes.size(); ++i) {
1114 std::vector<unsigned> StartBits;
1115 std::vector<unsigned> EndBits;
1116 std::vector<uint64_t> FieldVals;
1117 insn_t Insn;
1118
1119 insnWithID(Insn, Opcodes[i]);
1120
1121 // Look for islands of undecoded bits of any instruction.
1122 if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1123 // Found an instruction with island(s). Now just assign a filter.
1124 runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1,
1125 true);
1126 return true;
1127 }
1128 }
1129 }
1130
1131 unsigned BitIndex, InsnIndex;
1132
1133 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1134 // The automaton consumes the corresponding bit from each
1135 // instruction.
1136 //
1137 // Input symbols: 0, 1, and _ (unset).
1138 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1139 // Initial state: NONE.
1140 //
1141 // (NONE) ------- [01] -> (ALL_SET)
1142 // (NONE) ------- _ ----> (ALL_UNSET)
1143 // (ALL_SET) ---- [01] -> (ALL_SET)
1144 // (ALL_SET) ---- _ ----> (MIXED)
1145 // (ALL_UNSET) -- [01] -> (MIXED)
1146 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1147 // (MIXED) ------ . ----> (MIXED)
1148 // (FILTERED)---- . ----> (FILTERED)
1149
1150 bitAttr_t bitAttrs[BIT_WIDTH];
1151
1152 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1153 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1154 for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex)
1155 if (FilterBitValues[BitIndex] == BIT_TRUE ||
1156 FilterBitValues[BitIndex] == BIT_FALSE)
1157 bitAttrs[BitIndex] = ATTR_FILTERED;
1158 else
1159 bitAttrs[BitIndex] = ATTR_NONE;
1160
1161 for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
1162 insn_t insn;
1163
1164 insnWithID(insn, Opcodes[InsnIndex]);
1165
1166 for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex) {
1167 switch (bitAttrs[BitIndex]) {
1168 case ATTR_NONE:
1169 if (insn[BitIndex] == BIT_UNSET)
1170 bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1171 else
1172 bitAttrs[BitIndex] = ATTR_ALL_SET;
1173 break;
1174 case ATTR_ALL_SET:
1175 if (insn[BitIndex] == BIT_UNSET)
1176 bitAttrs[BitIndex] = ATTR_MIXED;
1177 break;
1178 case ATTR_ALL_UNSET:
1179 if (insn[BitIndex] != BIT_UNSET)
1180 bitAttrs[BitIndex] = ATTR_MIXED;
1181 break;
1182 case ATTR_MIXED:
1183 case ATTR_FILTERED:
1184 break;
1185 }
1186 }
1187 }
1188
1189 // The regionAttr automaton consumes the bitAttrs automatons' state,
1190 // lowest-to-highest.
1191 //
1192 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1193 // States: NONE, ALL_SET, MIXED
1194 // Initial state: NONE
1195 //
1196 // (NONE) ----- F --> (NONE)
1197 // (NONE) ----- S --> (ALL_SET) ; and set region start
1198 // (NONE) ----- U --> (NONE)
1199 // (NONE) ----- M --> (MIXED) ; and set region start
1200 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1201 // (ALL_SET) -- S --> (ALL_SET)
1202 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1203 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1204 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1205 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1206 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1207 // (MIXED) ---- M --> (MIXED)
1208
1209 bitAttr_t RA = ATTR_NONE;
1210 unsigned StartBit = 0;
1211
1212 for (BitIndex = 0; BitIndex < BIT_WIDTH; BitIndex++) {
1213 bitAttr_t bitAttr = bitAttrs[BitIndex];
1214
1215 assert(bitAttr != ATTR_NONE && "Bit without attributes");
1216
1217 switch (RA) {
1218 case ATTR_NONE:
1219 switch (bitAttr) {
1220 case ATTR_FILTERED:
1221 break;
1222 case ATTR_ALL_SET:
1223 StartBit = BitIndex;
1224 RA = ATTR_ALL_SET;
1225 break;
1226 case ATTR_ALL_UNSET:
1227 break;
1228 case ATTR_MIXED:
1229 StartBit = BitIndex;
1230 RA = ATTR_MIXED;
1231 break;
1232 default:
1233 assert(0 && "Unexpected bitAttr!");
1234 }
1235 break;
1236 case ATTR_ALL_SET:
1237 switch (bitAttr) {
1238 case ATTR_FILTERED:
1239 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1240 RA = ATTR_NONE;
1241 break;
1242 case ATTR_ALL_SET:
1243 break;
1244 case ATTR_ALL_UNSET:
1245 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1246 RA = ATTR_NONE;
1247 break;
1248 case ATTR_MIXED:
1249 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1250 StartBit = BitIndex;
1251 RA = ATTR_MIXED;
1252 break;
1253 default:
1254 assert(0 && "Unexpected bitAttr!");
1255 }
1256 break;
1257 case ATTR_MIXED:
1258 switch (bitAttr) {
1259 case ATTR_FILTERED:
1260 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1261 StartBit = BitIndex;
1262 RA = ATTR_NONE;
1263 break;
1264 case ATTR_ALL_SET:
1265 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1266 StartBit = BitIndex;
1267 RA = ATTR_ALL_SET;
1268 break;
1269 case ATTR_ALL_UNSET:
1270 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1271 RA = ATTR_NONE;
1272 break;
1273 case ATTR_MIXED:
1274 break;
1275 default:
1276 assert(0 && "Unexpected bitAttr!");
1277 }
1278 break;
1279 case ATTR_ALL_UNSET:
1280 assert(0 && "regionAttr state machine has no ATTR_UNSET state");
1281 case ATTR_FILTERED:
1282 assert(0 && "regionAttr state machine has no ATTR_FILTERED state");
1283 }
1284 }
1285
1286 // At the end, if we're still in ALL_SET or MIXED states, report a region
1287 switch (RA) {
1288 case ATTR_NONE:
1289 break;
1290 case ATTR_FILTERED:
1291 break;
1292 case ATTR_ALL_SET:
1293 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1294 break;
1295 case ATTR_ALL_UNSET:
1296 break;
1297 case ATTR_MIXED:
1298 reportRegion(RA, StartBit, BitIndex, AllowMixed);
1299 break;
1300 }
1301
1302 // We have finished with the filter processings. Now it's time to choose
1303 // the best performing filter.
1304 BestIndex = 0;
1305 bool AllUseless = true;
1306 unsigned BestScore = 0;
1307
1308 for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1309 unsigned Usefulness = Filters[i].usefulness();
1310
1311 if (Usefulness)
1312 AllUseless = false;
1313
1314 if (Usefulness > BestScore) {
1315 BestIndex = i;
1316 BestScore = Usefulness;
1317 }
1318 }
1319
1320 if (!AllUseless)
1321 bestFilter().recurse();
1322
1323 return !AllUseless;
1324} // end of FilterChooser::filterProcessor(bool)
1325
1326// Decides on the best configuration of filter(s) to use in order to decode
1327// the instructions. A conflict of instructions may occur, in which case we
1328// dump the conflict set to the standard error.
Owen Andersond8c87882011-02-18 21:51:29 +00001329void ARMFilterChooser::doFilter() {
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001330 unsigned Num = Opcodes.size();
1331 assert(Num && "FilterChooser created with no instructions");
1332
1333 // Heuristics: Use Inst{31-28} as the top level filter for ARM ISA.
1334 if (TargetName == TARGET_ARM && Parent == NULL) {
1335 runSingleFilter(*this, 28, 4, false);
1336 return;
1337 }
1338
1339 // Try regions of consecutive known bit values first.
1340 if (filterProcessor(false))
1341 return;
1342
1343 // Then regions of mixed bits (both known and unitialized bit values allowed).
1344 if (filterProcessor(true))
1345 return;
1346
1347 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1348 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1349 // well-known encoding pattern. In such case, we backtrack and scan for the
1350 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1351 if (Num == 3 && filterProcessor(true, false))
1352 return;
1353
1354 // If we come to here, the instruction decoding has failed.
Johnny Chene0c74fb2010-04-09 19:31:33 +00001355 // Set the BestIndex to -1 to indicate so.
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001356 BestIndex = -1;
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001357}
1358
1359// Emits code to decode our share of instructions. Returns true if the
1360// emitted code causes a return, which occurs if we know how to decode
1361// the instruction at this level or the instruction is not decodeable.
Owen Andersond8c87882011-02-18 21:51:29 +00001362bool ARMFilterChooser::emit(raw_ostream &o, unsigned &Indentation) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001363 if (Opcodes.size() == 1)
1364 // There is only one instruction in the set, which is great!
1365 // Call emitSingletonDecoder() to see whether there are any remaining
1366 // encodings bits.
1367 return emitSingletonDecoder(o, Indentation, Opcodes[0]);
1368
1369 // Choose the best filter to do the decodings!
1370 if (BestIndex != -1) {
Owen Andersond8c87882011-02-18 21:51:29 +00001371 ARMFilter &Best = bestFilter();
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001372 if (Best.getNumFiltered() == 1)
1373 emitSingletonDecoder(o, Indentation, Best);
1374 else
1375 bestFilter().emit(o, Indentation);
1376 return false;
1377 }
1378
1379 // If we reach here, there is a conflict in decoding. Let's resolve the known
1380 // conflicts!
1381 if ((TargetName == TARGET_ARM || TargetName == TARGET_THUMB) &&
1382 Opcodes.size() == 2) {
1383 // Resolve the known conflict sets:
1384 //
1385 // 1. source registers are identical => VMOVDneon; otherwise => VORRd
1386 // 2. source registers are identical => VMOVQ; otherwise => VORRq
1387 // 3. LDR, LDRcp => return LDR for now.
1388 // FIXME: How can we distinguish between LDR and LDRcp? Do we need to?
Johnny Chen8c133352011-03-24 23:42:31 +00001389 // 4. tLDMIA, tLDMIA_UPD => Rn = Inst{10-8}, reglist = Inst{7-0},
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001390 // wback = registers<Rn> = 0
1391 // NOTE: (tLDM, tLDM_UPD) resolution must come before Advanced SIMD
1392 // addressing mode resolution!!!
1393 // 5. VLD[234]LN*/VST[234]LN* vs. VLD[234]LN*_UPD/VST[234]LN*_UPD conflicts
1394 // are resolved returning the non-UPD versions of the instructions if the
1395 // Rm field, i.e., Inst{3-0} is 0b1111. This is specified in A7.7.1
1396 // Advanced SIMD addressing mode.
1397 const std::string &name1 = nameWithID(Opcodes[0]);
1398 const std::string &name2 = nameWithID(Opcodes[1]);
1399 if ((name1 == "VMOVDneon" && name2 == "VORRd") ||
1400 (name1 == "VMOVQ" && name2 == "VORRq")) {
1401 // Inserting the opening curly brace for this case block.
1402 --Indentation; --Indentation;
1403 o.indent(Indentation) << "{\n";
1404 ++Indentation; ++Indentation;
1405
1406 o.indent(Indentation)
1407 << "field_t N = fieldFromInstruction(insn, 7, 1), "
1408 << "M = fieldFromInstruction(insn, 5, 1);\n";
1409 o.indent(Indentation)
1410 << "field_t Vn = fieldFromInstruction(insn, 16, 4), "
1411 << "Vm = fieldFromInstruction(insn, 0, 4);\n";
1412 o.indent(Indentation)
1413 << "return (N == M && Vn == Vm) ? "
1414 << Opcodes[0] << " /* " << name1 << " */ : "
1415 << Opcodes[1] << " /* " << name2 << " */ ;\n";
1416
1417 // Inserting the closing curly brace for this case block.
1418 --Indentation; --Indentation;
1419 o.indent(Indentation) << "}\n";
1420 ++Indentation; ++Indentation;
1421
1422 return true;
1423 }
1424 if (name1 == "LDR" && name2 == "LDRcp") {
1425 o.indent(Indentation)
1426 << "return " << Opcodes[0]
1427 << "; // Returning LDR for {LDR, LDRcp}\n";
1428 return true;
1429 }
Johnny Chen8c133352011-03-24 23:42:31 +00001430 if (name1 == "tLDMIA" && name2 == "tLDMIA_UPD") {
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001431 // Inserting the opening curly brace for this case block.
1432 --Indentation; --Indentation;
1433 o.indent(Indentation) << "{\n";
1434 ++Indentation; ++Indentation;
1435
1436 o.indent(Indentation)
1437 << "unsigned Rn = fieldFromInstruction(insn, 8, 3), "
1438 << "list = fieldFromInstruction(insn, 0, 8);\n";
1439 o.indent(Indentation)
1440 << "return ((list >> Rn) & 1) == 0 ? "
1441 << Opcodes[1] << " /* " << name2 << " */ : "
1442 << Opcodes[0] << " /* " << name1 << " */ ;\n";
1443
1444 // Inserting the closing curly brace for this case block.
1445 --Indentation; --Indentation;
1446 o.indent(Indentation) << "}\n";
1447 ++Indentation; ++Indentation;
1448
1449 return true;
1450 }
1451 if (sameStringExceptSuffix(name1, name2, "_UPD")) {
1452 o.indent(Indentation)
1453 << "return fieldFromInstruction(insn, 0, 4) == 15 ? " << Opcodes[0]
1454 << " /* " << name1 << " */ : " << Opcodes[1] << "/* " << name2
1455 << " */ ; // Advanced SIMD addressing mode\n";
1456 return true;
1457 }
1458
1459 // Otherwise, it does not belong to the known conflict sets.
1460 }
Johnny Chene0c74fb2010-04-09 19:31:33 +00001461
1462 // We don't know how to decode these instructions! Return 0 and dump the
1463 // conflict set!
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001464 o.indent(Indentation) << "return 0;" << " // Conflict set: ";
1465 for (int i = 0, N = Opcodes.size(); i < N; ++i) {
1466 o << nameWithID(Opcodes[i]);
1467 if (i < (N - 1))
1468 o << ", ";
1469 else
1470 o << '\n';
1471 }
Johnny Chene0c74fb2010-04-09 19:31:33 +00001472
1473 // Print out useful conflict information for postmortem analysis.
1474 errs() << "Decoding Conflict:\n";
1475
1476 dumpStack(errs(), "\t\t");
1477
1478 for (unsigned i = 0; i < Opcodes.size(); i++) {
1479 const std::string &Name = nameWithID(Opcodes[i]);
1480
1481 errs() << '\t' << Name << " ";
1482 dumpBits(errs(),
1483 getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
1484 errs() << '\n';
1485 }
1486
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001487 return true;
1488}
1489
1490
1491////////////////////////////////////////////
1492// //
1493// ARMDEBackend //
1494// (Helper class for ARMDecoderEmitter) //
1495// //
1496////////////////////////////////////////////
1497
1498class ARMDecoderEmitter::ARMDEBackend {
1499public:
Chris Lattner67db8832010-12-13 00:23:57 +00001500 ARMDEBackend(ARMDecoderEmitter &frontend, RecordKeeper &Records) :
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001501 NumberedInstructions(),
1502 Opcodes(),
1503 Frontend(frontend),
Chris Lattner67db8832010-12-13 00:23:57 +00001504 Target(Records),
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001505 FC(NULL)
1506 {
1507 if (Target.getName() == "ARM")
1508 TargetName = TARGET_ARM;
1509 else {
1510 errs() << "Target name " << Target.getName() << " not recognized\n";
1511 assert(0 && "Unknown target");
1512 }
1513
1514 // Populate the instructions for our TargetName.
1515 populateInstructions();
1516 }
1517
1518 ~ARMDEBackend() {
1519 if (FC) {
1520 delete FC;
1521 FC = NULL;
1522 }
1523 }
1524
1525 void getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
1526 &NumberedInstructions) {
1527 // We must emit the PHI opcode first...
1528 std::string Namespace = Target.getInstNamespace();
1529 assert(!Namespace.empty() && "No instructions defined.");
1530
1531 NumberedInstructions = Target.getInstructionsByEnumValue();
1532 }
1533
1534 bool populateInstruction(const CodeGenInstruction &CGI, TARGET_NAME_t TN);
1535
1536 void populateInstructions();
1537
1538 // Emits disassembler code for instruction decoding. This delegates to the
1539 // FilterChooser instance to do the heavy lifting.
1540 void emit(raw_ostream &o);
1541
1542protected:
1543 std::vector<const CodeGenInstruction*> NumberedInstructions;
1544 std::vector<unsigned> Opcodes;
1545 // Special case for the ARM chip, which supports ARM and Thumb ISAs.
1546 // Opcodes2 will be populated with the Thumb opcodes.
1547 std::vector<unsigned> Opcodes2;
1548 ARMDecoderEmitter &Frontend;
1549 CodeGenTarget Target;
Owen Andersond8c87882011-02-18 21:51:29 +00001550 ARMFilterChooser *FC;
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001551
1552 TARGET_NAME_t TargetName;
1553};
1554
Jim Grosbach37cc2f12010-11-30 19:00:13 +00001555bool ARMDecoderEmitter::
1556ARMDEBackend::populateInstruction(const CodeGenInstruction &CGI,
1557 TARGET_NAME_t TN) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001558 const Record &Def = *CGI.TheDef;
1559 const StringRef Name = Def.getName();
1560 uint8_t Form = getByteField(Def, "Form");
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001561
David Greened4a90662011-07-11 18:25:51 +00001562 const BitsInit &Bits = getBitsField(Def, "Inst");
Johnny Chen1808e4d2010-04-09 21:01:02 +00001563
1564 // If all the bit positions are not specified; do not decode this instruction.
1565 // We are bound to fail! For proper disassembly, the well-known encoding bits
1566 // of the instruction must be fully specified.
1567 //
1568 // This also removes pseudo instructions from considerations of disassembly,
1569 // which is a better design and less fragile than the name matchings.
1570 if (Bits.allInComplete()) return false;
1571
Bruno Cardoso Lopesa461d422011-01-18 20:45:56 +00001572 // Ignore "asm parser only" instructions.
1573 if (Def.getValueAsBit("isAsmParserOnly"))
1574 return false;
1575
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001576 if (TN == TARGET_ARM) {
Jim Grosbach5e973382011-03-10 19:05:48 +00001577 if (Form == ARM_FORMAT_PSEUDO)
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001578 return false;
1579 if (thumbInstruction(Form))
1580 return false;
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001581
Dale Johannesen51e28e62010-06-03 21:09:53 +00001582 // Tail calls are other patterns that generate existing instructions.
1583 if (Name == "TCRETURNdi" || Name == "TCRETURNdiND" ||
1584 Name == "TCRETURNri" || Name == "TCRETURNriND" ||
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001585 Name == "TAILJMPd" || Name == "TAILJMPdt" ||
1586 Name == "TAILJMPdND" || Name == "TAILJMPdNDt" ||
Dale Johannesen6470a112010-06-15 22:08:33 +00001587 Name == "TAILJMPr" || Name == "TAILJMPrND" ||
1588 Name == "MOVr_TC")
Dale Johannesen51e28e62010-06-03 21:09:53 +00001589 return false;
1590
Johnny Chene6d69e72011-03-24 20:42:48 +00001591 // Delegate ADR disassembly to the more generic ADDri/SUBri instructions.
1592 if (Name == "ADR")
1593 return false;
1594
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001595 //
1596 // The following special cases are for conflict resolutions.
1597 //
1598
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001599 // A8-598: VEXT
1600 // Vector Extract extracts elements from the bottom end of the second
1601 // operand vector and the top end of the first, concatenates them and
1602 // places the result in the destination vector. The elements of the
1603 // vectors are treated as being 8-bit bitfields. There is no distinction
1604 // between data types. The size of the operation can be specified in
1605 // assembler as vext.size. If the value is 16, 32, or 64, the syntax is
1606 // a pseudo-instruction for a VEXT instruction specifying the equivalent
1607 // number of bytes.
1608 //
1609 // Variants VEXTd16, VEXTd32, VEXTd8, and VEXTdf are reduced to VEXTd8;
1610 // variants VEXTq16, VEXTq32, VEXTq8, and VEXTqf are reduced to VEXTq8.
1611 if (Name == "VEXTd16" || Name == "VEXTd32" || Name == "VEXTdf" ||
1612 Name == "VEXTq16" || Name == "VEXTq32" || Name == "VEXTqf")
1613 return false;
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001614 } else if (TN == TARGET_THUMB) {
1615 if (!thumbInstruction(Form))
1616 return false;
1617
Johnny Chen1090d772011-03-24 23:21:14 +00001618 // A8.6.189 STM / STMIA / STMEA -- Encoding T1
1619 // There's only STMIA_UPD for Thumb1.
1620 if (Name == "tSTMIA")
1621 return false;
1622
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001623 // On Darwin R9 is call-clobbered. Ignore the non-Darwin counterparts.
1624 if (Name == "tBL" || Name == "tBLXi" || Name == "tBLXr")
1625 return false;
1626
Johnny Chende165082011-04-11 23:33:30 +00001627 // A8.6.25 BX. Use the generic tBX_Rm, ignore tBX_RET and tBX_RET_vararg.
1628 if (Name == "tBX_RET" || Name == "tBX_RET_vararg")
1629 return false;
1630
Jim Grosbachd40963c2010-12-14 22:28:03 +00001631 // Ignore tADR, prefer tADDrPCi.
1632 if (Name == "tADR")
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001633 return false;
1634
Johnny Chenef74e9a2011-03-25 00:17:42 +00001635 // Delegate t2ADR disassembly to the more generic t2ADDri12/t2SUBri12
1636 // instructions.
1637 if (Name == "t2ADR")
1638 return false;
1639
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001640 // Ignore tADDrSP, tADDspr, and tPICADD, prefer the generic tADDhirr.
1641 // Ignore t2SUBrSPs, prefer the t2SUB[S]r[r|s].
1642 // Ignore t2ADDrSPs, prefer the t2ADD[S]r[r|s].
1643 if (Name == "tADDrSP" || Name == "tADDspr" || Name == "tPICADD" ||
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001644 Name == "t2SUBrSPs" || Name == "t2ADDrSPs")
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001645 return false;
1646
Johnny Chen597fa652011-04-22 19:12:43 +00001647 // FIXME: Use ldr.n to work around a Darwin assembler bug.
1648 // Introduce a workaround with tLDRpciDIS opcode.
1649 if (Name == "tLDRpci")
1650 return false;
1651
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001652 // Ignore t2LDRDpci, prefer the generic t2LDRDi8, t2LDRD_PRE, t2LDRD_POST.
1653 if (Name == "t2LDRDpci")
1654 return false;
1655
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001656 // Resolve conflicts:
1657 //
1658 // tBfar conflicts with tBLr9
Jim Grosbach4629d502011-06-30 17:34:04 +00001659 // t2LDMIA_RET conflict with t2LDM (ditto)
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001660 // tMOVCCi conflicts with tMOVi8
1661 // tMOVCCr conflicts with tMOVgpr2gpr
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001662 // tLDRcp conflicts with tLDRspi
Jim Grosbach0e992192010-10-07 22:14:01 +00001663 // t2MOVCCi16 conflicts with tMOVi16
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001664 if (Name == "tBfar" ||
Jim Grosbach4629d502011-06-30 17:34:04 +00001665 Name == "t2LDMIA_RET" ||
Jim Grosbach5ca66692010-11-29 22:37:40 +00001666 Name == "tMOVCCi" || Name == "tMOVCCr" ||
Jim Grosbach74472b42011-06-29 20:26:39 +00001667 Name == "tLDRcp" ||
Jim Grosbachd40963c2010-12-14 22:28:03 +00001668 Name == "t2MOVCCi16")
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001669 return false;
1670 }
1671
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001672 DEBUG({
Bill Wendling6a97ed32010-12-13 01:03:49 +00001673 // Dumps the instruction encoding format.
1674 switch (TargetName) {
1675 case TARGET_ARM:
1676 case TARGET_THUMB:
1677 errs() << Name << " " << stringForARMFormat((ARMFormat)Form);
1678 break;
1679 }
1680
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001681 errs() << " ";
1682
1683 // Dumps the instruction encoding bits.
1684 dumpBits(errs(), Bits);
1685
1686 errs() << '\n';
1687
1688 // Dumps the list of operand info.
Chris Lattnerc240bb02010-11-01 04:03:32 +00001689 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1690 const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001691 const std::string &OperandName = Info.Name;
1692 const Record &OperandDef = *Info.Rec;
1693
1694 errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
1695 }
1696 });
1697
1698 return true;
1699}
1700
1701void ARMDecoderEmitter::ARMDEBackend::populateInstructions() {
1702 getInstructionsByEnumValue(NumberedInstructions);
1703
Owen Andersond8c87882011-02-18 21:51:29 +00001704 unsigned numUIDs = NumberedInstructions.size();
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001705 if (TargetName == TARGET_ARM) {
Owen Andersond8c87882011-02-18 21:51:29 +00001706 for (unsigned uid = 0; uid < numUIDs; uid++) {
1707 // filter out intrinsics
1708 if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM"))
1709 continue;
1710
1711 if (populateInstruction(*NumberedInstructions[uid], TargetName))
1712 Opcodes.push_back(uid);
1713 }
1714
1715 // Special handling for the ARM chip, which supports two modes of execution.
1716 // This branch handles the Thumb opcodes.
1717 for (unsigned uid = 0; uid < numUIDs; uid++) {
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001718 // filter out intrinsics
1719 if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM")
1720 && !NumberedInstructions[uid]->TheDef->isSubClassOf("InstThumb"))
1721 continue;
1722
1723 if (populateInstruction(*NumberedInstructions[uid], TARGET_THUMB))
1724 Opcodes2.push_back(uid);
1725 }
Owen Andersond8c87882011-02-18 21:51:29 +00001726
1727 return;
1728 }
1729
1730 // For other targets.
1731 for (unsigned uid = 0; uid < numUIDs; uid++) {
1732 Record *R = NumberedInstructions[uid]->TheDef;
1733 if (R->getValueAsString("Namespace") == "TargetOpcode")
1734 continue;
1735
1736 if (populateInstruction(*NumberedInstructions[uid], TargetName))
1737 Opcodes.push_back(uid);
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001738 }
1739}
1740
1741// Emits disassembler code for instruction decoding. This delegates to the
1742// FilterChooser instance to do the heavy lifting.
1743void ARMDecoderEmitter::ARMDEBackend::emit(raw_ostream &o) {
1744 switch (TargetName) {
1745 case TARGET_ARM:
1746 Frontend.EmitSourceFileHeader("ARM/Thumb Decoders", o);
1747 break;
1748 default:
1749 assert(0 && "Unreachable code!");
1750 }
1751
Michael J. Spencer1f6efa32010-11-29 18:16:10 +00001752 o << "#include \"llvm/Support/DataTypes.h\"\n";
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001753 o << "#include <assert.h>\n";
1754 o << '\n';
1755 o << "namespace llvm {\n\n";
1756
Owen Andersond8c87882011-02-18 21:51:29 +00001757 ARMFilterChooser::setTargetName(TargetName);
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001758
1759 switch (TargetName) {
1760 case TARGET_ARM: {
1761 // Emit common utility and ARM ISA decoder.
Owen Andersond8c87882011-02-18 21:51:29 +00001762 FC = new ARMFilterChooser(NumberedInstructions, Opcodes);
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001763 // Reset indentation level.
1764 unsigned Indentation = 0;
1765 FC->emitTop(o, Indentation);
1766 delete FC;
1767
1768 // Emit Thumb ISA decoder as well.
Owen Andersond8c87882011-02-18 21:51:29 +00001769 ARMFilterChooser::setTargetName(TARGET_THUMB);
1770 FC = new ARMFilterChooser(NumberedInstructions, Opcodes2);
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001771 // Reset indentation level.
1772 Indentation = 0;
1773 FC->emitBot(o, Indentation);
1774 break;
1775 }
1776 default:
1777 assert(0 && "Unreachable code!");
1778 }
1779
1780 o << "\n} // End llvm namespace \n";
1781}
1782
1783/////////////////////////
1784// Backend interface //
1785/////////////////////////
1786
1787void ARMDecoderEmitter::initBackend()
1788{
Chris Lattner67db8832010-12-13 00:23:57 +00001789 Backend = new ARMDEBackend(*this, Records);
Johnny Chenb68a3ee2010-04-02 22:27:38 +00001790}
1791
1792void ARMDecoderEmitter::run(raw_ostream &o)
1793{
1794 Backend->emit(o);
1795}
1796
1797void ARMDecoderEmitter::shutdownBackend()
1798{
1799 delete Backend;
1800 Backend = NULL;
1801}