Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1 | //===------------ ARMDecoderEmitter.cpp - Decoder Generator ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is part of the ARM Disassembler. |
| 11 | // It contains the tablegen backend that emits the decoder functions for ARM and |
| 12 | // Thumb. The disassembler core includes the auto-generated file, invokes the |
| 13 | // decoder functions, and builds up the MCInst based on the decoded Opcode. |
| 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 | |
| 30 | using 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 Wilson | 9a1c189 | 2010-08-11 00:01:18 +0000 | [diff] [blame] | 52 | ENTRY(ARM_FORMAT_SATFRM, 13) \ |
| 53 | ENTRY(ARM_FORMAT_EXTFRM, 14) \ |
| 54 | ENTRY(ARM_FORMAT_VFPUNARYFRM, 15) \ |
| 55 | ENTRY(ARM_FORMAT_VFPBINARYFRM, 16) \ |
| 56 | ENTRY(ARM_FORMAT_VFPCONV1FRM, 17) \ |
| 57 | ENTRY(ARM_FORMAT_VFPCONV2FRM, 18) \ |
| 58 | ENTRY(ARM_FORMAT_VFPCONV3FRM, 19) \ |
| 59 | ENTRY(ARM_FORMAT_VFPCONV4FRM, 20) \ |
| 60 | ENTRY(ARM_FORMAT_VFPCONV5FRM, 21) \ |
| 61 | ENTRY(ARM_FORMAT_VFPLDSTFRM, 22) \ |
| 62 | ENTRY(ARM_FORMAT_VFPLDSTMULFRM, 23) \ |
| 63 | ENTRY(ARM_FORMAT_VFPMISCFRM, 24) \ |
| 64 | ENTRY(ARM_FORMAT_THUMBFRM, 25) \ |
| 65 | ENTRY(ARM_FORMAT_MISCFRM, 26) \ |
| 66 | ENTRY(ARM_FORMAT_NEONGETLNFRM, 27) \ |
| 67 | ENTRY(ARM_FORMAT_NEONSETLNFRM, 28) \ |
| 68 | ENTRY(ARM_FORMAT_NEONDUPFRM, 29) \ |
| 69 | ENTRY(ARM_FORMAT_NLdSt, 30) \ |
| 70 | ENTRY(ARM_FORMAT_N1RegModImm, 31) \ |
| 71 | ENTRY(ARM_FORMAT_N2Reg, 32) \ |
| 72 | ENTRY(ARM_FORMAT_NVCVT, 33) \ |
| 73 | ENTRY(ARM_FORMAT_NVecDupLn, 34) \ |
| 74 | ENTRY(ARM_FORMAT_N2RegVecShL, 35) \ |
| 75 | ENTRY(ARM_FORMAT_N2RegVecShR, 36) \ |
| 76 | ENTRY(ARM_FORMAT_N3Reg, 37) \ |
| 77 | ENTRY(ARM_FORMAT_N3RegVecSh, 38) \ |
| 78 | ENTRY(ARM_FORMAT_NVecExtract, 39) \ |
| 79 | ENTRY(ARM_FORMAT_NVecMulScalar, 40) \ |
| 80 | ENTRY(ARM_FORMAT_NVTBL, 41) |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 81 | |
| 82 | // ARM instruction format specifies the encoding used by the instruction. |
| 83 | #define ENTRY(n, v) n = v, |
| 84 | typedef enum { |
| 85 | ARM_FORMATS |
| 86 | ARM_FORMAT_NA |
| 87 | } ARMFormat; |
| 88 | #undef ENTRY |
| 89 | |
| 90 | // Converts enum to const char*. |
| 91 | static const char *stringForARMFormat(ARMFormat form) { |
| 92 | #define ENTRY(n, v) case n: return #n; |
| 93 | switch(form) { |
| 94 | ARM_FORMATS |
| 95 | case ARM_FORMAT_NA: |
| 96 | default: |
| 97 | return ""; |
| 98 | } |
| 99 | #undef ENTRY |
| 100 | } |
| 101 | |
Chandler Carruth | 1e86e3f | 2010-04-03 04:45:24 +0000 | [diff] [blame] | 102 | enum { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 103 | 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(). |
| 117 | static 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 | |
| 137 | static uint8_t getByteField(const Record &def, const char *str) { |
| 138 | BitsInit *bits = def.getValueAsBitsInit(str); |
| 139 | return byteFromBitsInit(*bits); |
| 140 | } |
| 141 | |
| 142 | static 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. |
| 149 | static |
| 150 | bool 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. |
| 161 | static 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. |
| 170 | typedef enum { |
| 171 | BIT_TRUE, // '1' |
| 172 | BIT_FALSE, // '0' |
| 173 | BIT_UNSET, // '?' |
| 174 | BIT_UNFILTERED // unfiltered |
| 175 | } bit_value_t; |
| 176 | |
| 177 | static bool ValueSet(bit_value_t V) { |
| 178 | return (V == BIT_TRUE || V == BIT_FALSE); |
| 179 | } |
| 180 | static bool ValueNotSet(bit_value_t V) { |
| 181 | return (V == BIT_UNSET); |
| 182 | } |
| 183 | static int Value(bit_value_t V) { |
| 184 | return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1); |
| 185 | } |
| 186 | static bit_value_t bitFromBits(BitsInit &bits, unsigned index) { |
| 187 | if (BitInit *bit = dynamic_cast<BitInit*>(bits.getBit(index))) |
| 188 | return bit->getValue() ? BIT_TRUE : BIT_FALSE; |
| 189 | |
| 190 | // The bit is uninitialized. |
| 191 | return BIT_UNSET; |
| 192 | } |
| 193 | // Prints the bit value for each position. |
| 194 | static 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. |
| 215 | typedef 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 Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 224 | class ARMFilterChooser; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 225 | |
| 226 | // Representation of the instruction to work on. |
| 227 | typedef 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 Lewycky | 366b1e1 | 2010-10-07 21:55:16 +0000 | [diff] [blame] | 243 | /// An example of a conflict is |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 244 | /// |
| 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 Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 265 | class ARMFilter { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 266 | protected: |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 267 | ARMFilterChooser *Owner; // points to the FilterChooser who owns this filter |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 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. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 279 | std::map<unsigned, ARMFilterChooser*> FilterChooserMap; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 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 | |
| 290 | public: |
| 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 Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 299 | ARMFilterChooser &getVariableFC() { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 300 | assert(NumFiltered == 1); |
| 301 | assert(FilterChooserMap.size() == 1); |
Johnny Chen | 493a441 | 2010-04-02 22:51:04 +0000 | [diff] [blame] | 302 | return *(FilterChooserMap.find((unsigned)-1)->second); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 305 | ARMFilter(const ARMFilter &f); |
| 306 | ARMFilter(ARMFilterChooser &owner, unsigned startBit, unsigned numBits, |
| 307 | bool mixed); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 308 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 309 | ~ARMFilter(); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 310 | |
| 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. |
| 329 | typedef enum { |
| 330 | ATTR_NONE, |
| 331 | ATTR_FILTERED, |
| 332 | ATTR_ALL_SET, |
| 333 | ATTR_ALL_UNSET, |
| 334 | ATTR_MIXED |
| 335 | } bitAttr_t; |
| 336 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 337 | /// ARMFilterChooser - FilterChooser chooses the best filter among a set of Filters |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 338 | /// 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 Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 352 | class ARMFilterChooser { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 353 | static TARGET_NAME_t TargetName; |
| 354 | |
| 355 | protected: |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 356 | friend class ARMFilter; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 357 | |
| 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 Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 365 | std::vector<ARMFilter> Filters; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 366 | |
| 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 Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 372 | ARMFilterChooser *Parent; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 373 | |
| 374 | // Index of the best filter from Filters. |
| 375 | int BestIndex; |
| 376 | |
| 377 | public: |
| 378 | static void setTargetName(TARGET_NAME_t tn) { TargetName = tn; } |
| 379 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 380 | ARMFilterChooser(const ARMFilterChooser &FC) : |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 381 | 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 Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 386 | ARMFilterChooser(const std::vector<const CodeGenInstruction*> &Insts, |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 387 | const std::vector<unsigned> &IDs) : |
| 388 | AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(NULL), |
| 389 | BestIndex(-1) { |
| 390 | for (unsigned i = 0; i < BIT_WIDTH; ++i) |
| 391 | FilterBitValues[i] = BIT_UNFILTERED; |
| 392 | |
| 393 | doFilter(); |
| 394 | } |
| 395 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 396 | ARMFilterChooser(const std::vector<const CodeGenInstruction*> &Insts, |
| 397 | const std::vector<unsigned> &IDs, |
| 398 | bit_value_t (&ParentFilterBitValues)[BIT_WIDTH], |
| 399 | ARMFilterChooser &parent) : |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 400 | AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(&parent), |
| 401 | BestIndex(-1) { |
| 402 | for (unsigned i = 0; i < BIT_WIDTH; ++i) |
| 403 | FilterBitValues[i] = ParentFilterBitValues[i]; |
| 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 | |
| 421 | protected: |
| 422 | // Populates the insn given the uid. |
| 423 | void insnWithID(insn_t &Insn, unsigned Opcode) const { |
Jim Grosbach | 806fcc0 | 2011-07-06 21:33:38 +0000 | [diff] [blame^] | 424 | if (AllInstructions[Opcode]->isPseudo) |
| 425 | return; |
| 426 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 427 | BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst"); |
| 428 | |
| 429 | for (unsigned i = 0; i < BIT_WIDTH; ++i) |
| 430 | Insn[i] = bitFromBits(Bits, i); |
| 431 | |
| 432 | // Set Inst{21} to 1 (wback) when IndexModeBits == IndexModeUpd. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 433 | Record *R = AllInstructions[Opcode]->TheDef; |
| 434 | if (R->getValue("IndexModeBits") && |
| 435 | getByteField(*R, "IndexModeBits") == IndexModeUpd) |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 436 | Insn[21] = BIT_TRUE; |
| 437 | } |
| 438 | |
| 439 | // Returns the record name. |
| 440 | const std::string &nameWithID(unsigned Opcode) const { |
| 441 | return AllInstructions[Opcode]->TheDef->getName(); |
| 442 | } |
| 443 | |
| 444 | // Populates the field of the insn given the start position and the number of |
| 445 | // consecutive bits to scan for. |
| 446 | // |
| 447 | // Returns false if there exists any uninitialized bit value in the range. |
| 448 | // Returns true, otherwise. |
| 449 | bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit, |
| 450 | unsigned NumBits) const; |
| 451 | |
| 452 | /// dumpFilterArray - dumpFilterArray prints out debugging info for the given |
| 453 | /// filter array as a series of chars. |
| 454 | void dumpFilterArray(raw_ostream &o, bit_value_t (&filter)[BIT_WIDTH]); |
| 455 | |
| 456 | /// dumpStack - dumpStack traverses the filter chooser chain and calls |
| 457 | /// dumpFilterArray on each filter chooser up to the top level one. |
| 458 | void dumpStack(raw_ostream &o, const char *prefix); |
| 459 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 460 | ARMFilter &bestFilter() { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 461 | assert(BestIndex != -1 && "BestIndex not set"); |
| 462 | return Filters[BestIndex]; |
| 463 | } |
| 464 | |
| 465 | // Called from Filter::recurse() when singleton exists. For debug purpose. |
| 466 | void SingletonExists(unsigned Opc); |
| 467 | |
| 468 | bool PositionFiltered(unsigned i) { |
| 469 | return ValueSet(FilterBitValues[i]); |
| 470 | } |
| 471 | |
| 472 | // Calculates the island(s) needed to decode the instruction. |
| 473 | // This returns a lit of undecoded bits of an instructions, for example, |
| 474 | // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be |
| 475 | // decoded bits in order to verify that the instruction matches the Opcode. |
| 476 | unsigned getIslands(std::vector<unsigned> &StartBits, |
| 477 | std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals, |
| 478 | insn_t &Insn); |
| 479 | |
| 480 | // The purpose of this function is for the API client to detect possible |
| 481 | // Load/Store Coprocessor instructions. If the coprocessor number is of |
| 482 | // the instruction is either 10 or 11, the decoder should not report the |
| 483 | // instruction as LDC/LDC2/STC/STC2, but should match against Advanced SIMD or |
| 484 | // VFP instructions. |
| 485 | bool LdStCopEncoding1(unsigned Opc) { |
| 486 | const std::string &Name = nameWithID(Opc); |
| 487 | if (Name == "LDC_OFFSET" || Name == "LDC_OPTION" || |
| 488 | Name == "LDC_POST" || Name == "LDC_PRE" || |
| 489 | Name == "LDCL_OFFSET" || Name == "LDCL_OPTION" || |
| 490 | Name == "LDCL_POST" || Name == "LDCL_PRE" || |
| 491 | Name == "STC_OFFSET" || Name == "STC_OPTION" || |
| 492 | Name == "STC_POST" || Name == "STC_PRE" || |
| 493 | Name == "STCL_OFFSET" || Name == "STCL_OPTION" || |
| 494 | Name == "STCL_POST" || Name == "STCL_PRE") |
| 495 | return true; |
| 496 | else |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | // Emits code to decode the singleton. Return true if we have matched all the |
| 501 | // well-known bits. |
| 502 | bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc); |
| 503 | |
| 504 | // Emits code to decode the singleton, and then to decode the rest. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 505 | void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation, |
| 506 | ARMFilter &Best); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 507 | |
| 508 | // Assign a single filter and run with it. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 509 | void runSingleFilter(ARMFilterChooser &owner, unsigned startBit, |
| 510 | unsigned numBit, bool mixed); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 511 | |
| 512 | // reportRegion is a helper function for filterProcessor to mark a region as |
| 513 | // eligible for use as a filter region. |
| 514 | void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex, |
| 515 | bool AllowMixed); |
| 516 | |
| 517 | // FilterProcessor scans the well-known encoding bits of the instructions and |
| 518 | // builds up a list of candidate filters. It chooses the best filter and |
| 519 | // recursively descends down the decoding tree. |
| 520 | bool filterProcessor(bool AllowMixed, bool Greedy = true); |
| 521 | |
| 522 | // Decides on the best configuration of filter(s) to use in order to decode |
| 523 | // the instructions. A conflict of instructions may occur, in which case we |
| 524 | // dump the conflict set to the standard error. |
| 525 | void doFilter(); |
| 526 | |
| 527 | // Emits code to decode our share of instructions. Returns true if the |
| 528 | // emitted code causes a return, which occurs if we know how to decode |
| 529 | // the instruction at this level or the instruction is not decodeable. |
| 530 | bool emit(raw_ostream &o, unsigned &Indentation); |
| 531 | }; |
| 532 | |
| 533 | /////////////////////////// |
| 534 | // // |
| 535 | // Filter Implmenetation // |
| 536 | // // |
| 537 | /////////////////////////// |
| 538 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 539 | ARMFilter::ARMFilter(const ARMFilter &f) : |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 540 | Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed), |
| 541 | FilteredInstructions(f.FilteredInstructions), |
| 542 | VariableInstructions(f.VariableInstructions), |
| 543 | FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered), |
| 544 | LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) { |
| 545 | } |
| 546 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 547 | ARMFilter::ARMFilter(ARMFilterChooser &owner, unsigned startBit, unsigned numBits, |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 548 | bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits), |
| 549 | Mixed(mixed) { |
| 550 | assert(StartBit + NumBits - 1 < BIT_WIDTH); |
| 551 | |
| 552 | NumFiltered = 0; |
| 553 | LastOpcFiltered = 0; |
| 554 | NumVariable = 0; |
| 555 | |
| 556 | for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) { |
| 557 | insn_t Insn; |
| 558 | |
| 559 | // Populates the insn given the uid. |
| 560 | Owner->insnWithID(Insn, Owner->Opcodes[i]); |
| 561 | |
| 562 | uint64_t Field; |
| 563 | // Scans the segment for possibly well-specified encoding bits. |
| 564 | bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits); |
| 565 | |
| 566 | if (ok) { |
| 567 | // The encoding bits are well-known. Lets add the uid of the |
| 568 | // instruction into the bucket keyed off the constant field value. |
| 569 | LastOpcFiltered = Owner->Opcodes[i]; |
| 570 | FilteredInstructions[Field].push_back(LastOpcFiltered); |
| 571 | ++NumFiltered; |
| 572 | } else { |
| 573 | // Some of the encoding bit(s) are unspecfied. This contributes to |
| 574 | // one additional member of "Variable" instructions. |
| 575 | VariableInstructions.push_back(Owner->Opcodes[i]); |
| 576 | ++NumVariable; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | assert((FilteredInstructions.size() + VariableInstructions.size() > 0) |
| 581 | && "Filter returns no instruction categories"); |
| 582 | } |
| 583 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 584 | ARMFilter::~ARMFilter() { |
| 585 | std::map<unsigned, ARMFilterChooser*>::iterator filterIterator; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 586 | for (filterIterator = FilterChooserMap.begin(); |
| 587 | filterIterator != FilterChooserMap.end(); |
| 588 | filterIterator++) { |
| 589 | delete filterIterator->second; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | // Divides the decoding task into sub tasks and delegates them to the |
| 594 | // inferior FilterChooser's. |
| 595 | // |
| 596 | // A special case arises when there's only one entry in the filtered |
| 597 | // instructions. In order to unambiguously decode the singleton, we need to |
| 598 | // match the remaining undecoded encoding bits against the singleton. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 599 | void ARMFilter::recurse() { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 600 | std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator; |
| 601 | |
| 602 | bit_value_t BitValueArray[BIT_WIDTH]; |
| 603 | // Starts by inheriting our parent filter chooser's filter bit values. |
Johnny Chen | 2d16a67 | 2010-04-08 21:23:54 +0000 | [diff] [blame] | 604 | memcpy(BitValueArray, Owner->FilterBitValues, sizeof(BitValueArray)); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 605 | |
| 606 | unsigned bitIndex; |
| 607 | |
| 608 | if (VariableInstructions.size()) { |
| 609 | // Conservatively marks each segment position as BIT_UNSET. |
| 610 | for (bitIndex = 0; bitIndex < NumBits; bitIndex++) |
| 611 | BitValueArray[StartBit + bitIndex] = BIT_UNSET; |
| 612 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 613 | // Delegates to an inferior filter chooser for further processing on this |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 614 | // group of instructions whose segment values are variable. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 615 | FilterChooserMap.insert(std::pair<unsigned, ARMFilterChooser*>( |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 616 | (unsigned)-1, |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 617 | new ARMFilterChooser(Owner->AllInstructions, |
| 618 | VariableInstructions, |
| 619 | BitValueArray, |
| 620 | *Owner) |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 621 | )); |
| 622 | } |
| 623 | |
| 624 | // No need to recurse for a singleton filtered instruction. |
| 625 | // See also Filter::emit(). |
| 626 | if (getNumFiltered() == 1) { |
| 627 | //Owner->SingletonExists(LastOpcFiltered); |
| 628 | assert(FilterChooserMap.size() == 1); |
| 629 | return; |
| 630 | } |
Johnny Chen | 3c500e6 | 2010-04-07 20:53:12 +0000 | [diff] [blame] | 631 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 632 | // Otherwise, create sub choosers. |
| 633 | for (mapIterator = FilteredInstructions.begin(); |
| 634 | mapIterator != FilteredInstructions.end(); |
| 635 | mapIterator++) { |
| 636 | |
| 637 | // Marks all the segment positions with either BIT_TRUE or BIT_FALSE. |
| 638 | for (bitIndex = 0; bitIndex < NumBits; bitIndex++) { |
Benjamin Kramer | 454c4ce | 2010-04-08 15:25:57 +0000 | [diff] [blame] | 639 | if (mapIterator->first & (1ULL << bitIndex)) |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 640 | BitValueArray[StartBit + bitIndex] = BIT_TRUE; |
| 641 | else |
| 642 | BitValueArray[StartBit + bitIndex] = BIT_FALSE; |
| 643 | } |
| 644 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 645 | // Delegates to an inferior filter chooser for further processing on this |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 646 | // category of instructions. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 647 | FilterChooserMap.insert(std::pair<unsigned, ARMFilterChooser*>( |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 648 | mapIterator->first, |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 649 | new ARMFilterChooser(Owner->AllInstructions, |
| 650 | mapIterator->second, |
| 651 | BitValueArray, |
| 652 | *Owner) |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 653 | )); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // Emit code to decode instructions given a segment or segments of bits. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 658 | void ARMFilter::emit(raw_ostream &o, unsigned &Indentation) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 659 | o.indent(Indentation) << "// Check Inst{"; |
| 660 | |
| 661 | if (NumBits > 1) |
| 662 | o << (StartBit + NumBits - 1) << '-'; |
| 663 | |
| 664 | o << StartBit << "} ...\n"; |
| 665 | |
| 666 | o.indent(Indentation) << "switch (fieldFromInstruction(insn, " |
| 667 | << StartBit << ", " << NumBits << ")) {\n"; |
| 668 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 669 | std::map<unsigned, ARMFilterChooser*>::iterator filterIterator; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 670 | |
| 671 | bool DefaultCase = false; |
| 672 | for (filterIterator = FilterChooserMap.begin(); |
| 673 | filterIterator != FilterChooserMap.end(); |
| 674 | filterIterator++) { |
| 675 | |
| 676 | // Field value -1 implies a non-empty set of variable instructions. |
| 677 | // See also recurse(). |
| 678 | if (filterIterator->first == (unsigned)-1) { |
| 679 | DefaultCase = true; |
| 680 | |
| 681 | o.indent(Indentation) << "default:\n"; |
| 682 | o.indent(Indentation) << " break; // fallthrough\n"; |
| 683 | |
| 684 | // Closing curly brace for the switch statement. |
| 685 | // This is unconventional because we want the default processing to be |
| 686 | // performed for the fallthrough cases as well, i.e., when the "cases" |
| 687 | // did not prove a decoded instruction. |
| 688 | o.indent(Indentation) << "}\n"; |
| 689 | |
| 690 | } else |
| 691 | o.indent(Indentation) << "case " << filterIterator->first << ":\n"; |
| 692 | |
| 693 | // We arrive at a category of instructions with the same segment value. |
| 694 | // Now delegate to the sub filter chooser for further decodings. |
| 695 | // The case may fallthrough, which happens if the remaining well-known |
| 696 | // encoding bits do not match exactly. |
| 697 | if (!DefaultCase) { ++Indentation; ++Indentation; } |
| 698 | |
| 699 | bool finished = filterIterator->second->emit(o, Indentation); |
| 700 | // For top level default case, there's no need for a break statement. |
| 701 | if (Owner->isTopLevel() && DefaultCase) |
| 702 | break; |
| 703 | if (!finished) |
| 704 | o.indent(Indentation) << "break;\n"; |
| 705 | |
| 706 | if (!DefaultCase) { --Indentation; --Indentation; } |
| 707 | } |
| 708 | |
| 709 | // If there is no default case, we still need to supply a closing brace. |
| 710 | if (!DefaultCase) { |
| 711 | // Closing curly brace for the switch statement. |
| 712 | o.indent(Indentation) << "}\n"; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | // Returns the number of fanout produced by the filter. More fanout implies |
| 717 | // the filter distinguishes more categories of instructions. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 718 | unsigned ARMFilter::usefulness() const { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 719 | if (VariableInstructions.size()) |
| 720 | return FilteredInstructions.size(); |
| 721 | else |
| 722 | return FilteredInstructions.size() + 1; |
| 723 | } |
| 724 | |
| 725 | ////////////////////////////////// |
| 726 | // // |
| 727 | // Filterchooser Implementation // |
| 728 | // // |
| 729 | ////////////////////////////////// |
| 730 | |
| 731 | // Define the symbol here. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 732 | TARGET_NAME_t ARMFilterChooser::TargetName; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 733 | |
| 734 | // This provides an opportunity for target specific code emission. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 735 | void ARMFilterChooser::emitTopHook(raw_ostream &o) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 736 | if (TargetName == TARGET_ARM) { |
| 737 | // Emit code that references the ARMFormat data type. |
| 738 | o << "static const ARMFormat ARMFormats[] = {\n"; |
| 739 | for (unsigned i = 0, e = AllInstructions.size(); i != e; ++i) { |
| 740 | const Record &Def = *(AllInstructions[i]->TheDef); |
| 741 | const std::string &Name = Def.getName(); |
| 742 | if (Def.isSubClassOf("InstARM") || Def.isSubClassOf("InstThumb")) |
| 743 | o.indent(2) << |
| 744 | stringForARMFormat((ARMFormat)getByteField(Def, "Form")); |
| 745 | else |
| 746 | o << " ARM_FORMAT_NA"; |
| 747 | |
| 748 | o << ",\t// Inst #" << i << " = " << Name << '\n'; |
| 749 | } |
| 750 | o << " ARM_FORMAT_NA\t// Unreachable.\n"; |
| 751 | o << "};\n\n"; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | // Emit the top level typedef and decodeInstruction() function. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 756 | void ARMFilterChooser::emitTop(raw_ostream &o, unsigned &Indentation) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 757 | // Run the target specific emit hook. |
| 758 | emitTopHook(o); |
| 759 | |
| 760 | switch (BIT_WIDTH) { |
| 761 | case 8: |
| 762 | o.indent(Indentation) << "typedef uint8_t field_t;\n"; |
| 763 | break; |
| 764 | case 16: |
| 765 | o.indent(Indentation) << "typedef uint16_t field_t;\n"; |
| 766 | break; |
| 767 | case 32: |
| 768 | o.indent(Indentation) << "typedef uint32_t field_t;\n"; |
| 769 | break; |
| 770 | case 64: |
| 771 | o.indent(Indentation) << "typedef uint64_t field_t;\n"; |
| 772 | break; |
| 773 | default: |
| 774 | assert(0 && "Unexpected instruction size!"); |
| 775 | } |
| 776 | |
| 777 | o << '\n'; |
| 778 | |
| 779 | o.indent(Indentation) << "static field_t " << |
| 780 | "fieldFromInstruction(field_t insn, unsigned startBit, unsigned numBits)\n"; |
| 781 | |
| 782 | o.indent(Indentation) << "{\n"; |
| 783 | |
| 784 | ++Indentation; ++Indentation; |
| 785 | o.indent(Indentation) << "assert(startBit + numBits <= " << BIT_WIDTH |
| 786 | << " && \"Instruction field out of bounds!\");\n"; |
| 787 | o << '\n'; |
| 788 | o.indent(Indentation) << "field_t fieldMask;\n"; |
| 789 | o << '\n'; |
| 790 | o.indent(Indentation) << "if (numBits == " << BIT_WIDTH << ")\n"; |
| 791 | |
| 792 | ++Indentation; ++Indentation; |
| 793 | o.indent(Indentation) << "fieldMask = (field_t)-1;\n"; |
| 794 | --Indentation; --Indentation; |
| 795 | |
| 796 | o.indent(Indentation) << "else\n"; |
| 797 | |
| 798 | ++Indentation; ++Indentation; |
| 799 | o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n"; |
| 800 | --Indentation; --Indentation; |
| 801 | |
| 802 | o << '\n'; |
| 803 | o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n"; |
| 804 | --Indentation; --Indentation; |
| 805 | |
| 806 | o.indent(Indentation) << "}\n"; |
| 807 | |
| 808 | o << '\n'; |
| 809 | |
Jim Grosbach | bb16824 | 2010-10-08 18:13:57 +0000 | [diff] [blame] | 810 | o.indent(Indentation) <<"static uint16_t decodeInstruction(field_t insn) {\n"; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 811 | |
| 812 | ++Indentation; ++Indentation; |
| 813 | // Emits code to decode the instructions. |
| 814 | emit(o, Indentation); |
| 815 | |
| 816 | o << '\n'; |
| 817 | o.indent(Indentation) << "return 0;\n"; |
| 818 | --Indentation; --Indentation; |
| 819 | |
| 820 | o.indent(Indentation) << "}\n"; |
| 821 | |
| 822 | o << '\n'; |
| 823 | } |
| 824 | |
| 825 | // This provides an opportunity for target specific code emission after |
| 826 | // emitTop(). |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 827 | void ARMFilterChooser::emitBot(raw_ostream &o, unsigned &Indentation) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 828 | if (TargetName != TARGET_THUMB) return; |
| 829 | |
| 830 | // Emit code that decodes the Thumb ISA. |
| 831 | o.indent(Indentation) |
| 832 | << "static uint16_t decodeThumbInstruction(field_t insn) {\n"; |
| 833 | |
| 834 | ++Indentation; ++Indentation; |
| 835 | |
| 836 | // Emits code to decode the instructions. |
| 837 | emit(o, Indentation); |
| 838 | |
| 839 | o << '\n'; |
| 840 | o.indent(Indentation) << "return 0;\n"; |
| 841 | |
| 842 | --Indentation; --Indentation; |
| 843 | |
| 844 | o.indent(Indentation) << "}\n"; |
| 845 | } |
| 846 | |
| 847 | // Populates the field of the insn given the start position and the number of |
| 848 | // consecutive bits to scan for. |
| 849 | // |
| 850 | // Returns false if and on the first uninitialized bit value encountered. |
| 851 | // Returns true, otherwise. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 852 | bool ARMFilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn, |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 853 | unsigned StartBit, unsigned NumBits) const { |
| 854 | Field = 0; |
| 855 | |
| 856 | for (unsigned i = 0; i < NumBits; ++i) { |
| 857 | if (Insn[StartBit + i] == BIT_UNSET) |
| 858 | return false; |
| 859 | |
| 860 | if (Insn[StartBit + i] == BIT_TRUE) |
Benjamin Kramer | 454c4ce | 2010-04-08 15:25:57 +0000 | [diff] [blame] | 861 | Field = Field | (1ULL << i); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | /// dumpFilterArray - dumpFilterArray prints out debugging info for the given |
| 868 | /// filter array as a series of chars. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 869 | void ARMFilterChooser::dumpFilterArray(raw_ostream &o, |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 870 | bit_value_t (&filter)[BIT_WIDTH]) { |
| 871 | unsigned bitIndex; |
| 872 | |
| 873 | for (bitIndex = BIT_WIDTH; bitIndex > 0; bitIndex--) { |
| 874 | switch (filter[bitIndex - 1]) { |
| 875 | case BIT_UNFILTERED: |
| 876 | o << "."; |
| 877 | break; |
| 878 | case BIT_UNSET: |
| 879 | o << "_"; |
| 880 | break; |
| 881 | case BIT_TRUE: |
| 882 | o << "1"; |
| 883 | break; |
| 884 | case BIT_FALSE: |
| 885 | o << "0"; |
| 886 | break; |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | /// dumpStack - dumpStack traverses the filter chooser chain and calls |
| 892 | /// dumpFilterArray on each filter chooser up to the top level one. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 893 | void ARMFilterChooser::dumpStack(raw_ostream &o, const char *prefix) { |
| 894 | ARMFilterChooser *current = this; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 895 | |
| 896 | while (current) { |
| 897 | o << prefix; |
| 898 | dumpFilterArray(o, current->FilterBitValues); |
| 899 | o << '\n'; |
| 900 | current = current->Parent; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | // Called from Filter::recurse() when singleton exists. For debug purpose. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 905 | void ARMFilterChooser::SingletonExists(unsigned Opc) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 906 | insn_t Insn0; |
| 907 | insnWithID(Insn0, Opc); |
| 908 | |
| 909 | errs() << "Singleton exists: " << nameWithID(Opc) |
| 910 | << " with its decoding dominating "; |
| 911 | for (unsigned i = 0; i < Opcodes.size(); ++i) { |
| 912 | if (Opcodes[i] == Opc) continue; |
| 913 | errs() << nameWithID(Opcodes[i]) << ' '; |
| 914 | } |
| 915 | errs() << '\n'; |
| 916 | |
| 917 | dumpStack(errs(), "\t\t"); |
| 918 | for (unsigned i = 0; i < Opcodes.size(); i++) { |
| 919 | const std::string &Name = nameWithID(Opcodes[i]); |
| 920 | |
| 921 | errs() << '\t' << Name << " "; |
| 922 | dumpBits(errs(), |
| 923 | getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst")); |
| 924 | errs() << '\n'; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | // Calculates the island(s) needed to decode the instruction. |
| 929 | // This returns a list of undecoded bits of an instructions, for example, |
| 930 | // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be |
| 931 | // decoded bits in order to verify that the instruction matches the Opcode. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 932 | unsigned ARMFilterChooser::getIslands(std::vector<unsigned> &StartBits, |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 933 | std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals, |
| 934 | insn_t &Insn) { |
| 935 | unsigned Num, BitNo; |
| 936 | Num = BitNo = 0; |
| 937 | |
| 938 | uint64_t FieldVal = 0; |
| 939 | |
| 940 | // 0: Init |
| 941 | // 1: Water (the bit value does not affect decoding) |
| 942 | // 2: Island (well-known bit value needed for decoding) |
| 943 | int State = 0; |
| 944 | int Val = -1; |
| 945 | |
| 946 | for (unsigned i = 0; i < BIT_WIDTH; ++i) { |
| 947 | Val = Value(Insn[i]); |
| 948 | bool Filtered = PositionFiltered(i); |
| 949 | switch (State) { |
| 950 | default: |
| 951 | assert(0 && "Unreachable code!"); |
| 952 | break; |
| 953 | case 0: |
| 954 | case 1: |
| 955 | if (Filtered || Val == -1) |
| 956 | State = 1; // Still in Water |
| 957 | else { |
| 958 | State = 2; // Into the Island |
| 959 | BitNo = 0; |
| 960 | StartBits.push_back(i); |
| 961 | FieldVal = Val; |
| 962 | } |
| 963 | break; |
| 964 | case 2: |
| 965 | if (Filtered || Val == -1) { |
| 966 | State = 1; // Into the Water |
| 967 | EndBits.push_back(i - 1); |
| 968 | FieldVals.push_back(FieldVal); |
| 969 | ++Num; |
| 970 | } else { |
| 971 | State = 2; // Still in Island |
| 972 | ++BitNo; |
| 973 | FieldVal = FieldVal | Val << BitNo; |
| 974 | } |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | // If we are still in Island after the loop, do some housekeeping. |
| 979 | if (State == 2) { |
| 980 | EndBits.push_back(BIT_WIDTH - 1); |
| 981 | FieldVals.push_back(FieldVal); |
| 982 | ++Num; |
| 983 | } |
| 984 | |
| 985 | assert(StartBits.size() == Num && EndBits.size() == Num && |
| 986 | FieldVals.size() == Num); |
| 987 | return Num; |
| 988 | } |
| 989 | |
| 990 | // Emits code to decode the singleton. Return true if we have matched all the |
| 991 | // well-known bits. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 992 | bool ARMFilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation, |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 993 | unsigned Opc) { |
| 994 | std::vector<unsigned> StartBits; |
| 995 | std::vector<unsigned> EndBits; |
| 996 | std::vector<uint64_t> FieldVals; |
| 997 | insn_t Insn; |
| 998 | insnWithID(Insn, Opc); |
| 999 | |
| 1000 | // This provides a good opportunity to check for possible Ld/St Coprocessor |
| 1001 | // Opcode and escapes if the coproc # is either 10 or 11. It is a NEON/VFP |
| 1002 | // instruction is disguise. |
| 1003 | if (TargetName == TARGET_ARM && LdStCopEncoding1(Opc)) { |
| 1004 | o.indent(Indentation); |
| 1005 | // A8.6.51 & A8.6.188 |
| 1006 | // If coproc = 0b101?, i.e, slice(insn, 11, 8) = 10 or 11, escape. |
| 1007 | o << "if (fieldFromInstruction(insn, 9, 3) == 5) break; // fallthrough\n"; |
| 1008 | } |
| 1009 | |
| 1010 | // Look for islands of undecoded bits of the singleton. |
| 1011 | getIslands(StartBits, EndBits, FieldVals, Insn); |
| 1012 | |
| 1013 | unsigned Size = StartBits.size(); |
| 1014 | unsigned I, NumBits; |
| 1015 | |
| 1016 | // If we have matched all the well-known bits, just issue a return. |
| 1017 | if (Size == 0) { |
| 1018 | o.indent(Indentation) << "return " << Opc << "; // " << nameWithID(Opc) |
| 1019 | << '\n'; |
| 1020 | return true; |
| 1021 | } |
| 1022 | |
| 1023 | // Otherwise, there are more decodings to be done! |
| 1024 | |
| 1025 | // Emit code to match the island(s) for the singleton. |
| 1026 | o.indent(Indentation) << "// Check "; |
| 1027 | |
| 1028 | for (I = Size; I != 0; --I) { |
| 1029 | o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} "; |
| 1030 | if (I > 1) |
| 1031 | o << "&& "; |
| 1032 | else |
| 1033 | o << "for singleton decoding...\n"; |
| 1034 | } |
| 1035 | |
| 1036 | o.indent(Indentation) << "if ("; |
| 1037 | |
| 1038 | for (I = Size; I != 0; --I) { |
| 1039 | NumBits = EndBits[I-1] - StartBits[I-1] + 1; |
| 1040 | o << "fieldFromInstruction(insn, " << StartBits[I-1] << ", " << NumBits |
| 1041 | << ") == " << FieldVals[I-1]; |
| 1042 | if (I > 1) |
| 1043 | o << " && "; |
| 1044 | else |
| 1045 | o << ")\n"; |
| 1046 | } |
| 1047 | |
| 1048 | o.indent(Indentation) << " return " << Opc << "; // " << nameWithID(Opc) |
| 1049 | << '\n'; |
| 1050 | |
| 1051 | return false; |
| 1052 | } |
| 1053 | |
| 1054 | // Emits code to decode the singleton, and then to decode the rest. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1055 | void ARMFilterChooser::emitSingletonDecoder(raw_ostream &o, |
| 1056 | unsigned &Indentation, |
| 1057 | ARMFilter &Best) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1058 | |
| 1059 | unsigned Opc = Best.getSingletonOpc(); |
| 1060 | |
| 1061 | emitSingletonDecoder(o, Indentation, Opc); |
| 1062 | |
| 1063 | // Emit code for the rest. |
| 1064 | o.indent(Indentation) << "else\n"; |
| 1065 | |
| 1066 | Indentation += 2; |
| 1067 | Best.getVariableFC().emit(o, Indentation); |
| 1068 | Indentation -= 2; |
| 1069 | } |
| 1070 | |
| 1071 | // Assign a single filter and run with it. Top level API client can initialize |
| 1072 | // with a single filter to start the filtering process. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1073 | void ARMFilterChooser::runSingleFilter(ARMFilterChooser &owner, |
| 1074 | unsigned startBit, |
| 1075 | unsigned numBit, bool mixed) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1076 | Filters.clear(); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1077 | ARMFilter F(*this, startBit, numBit, true); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1078 | Filters.push_back(F); |
| 1079 | BestIndex = 0; // Sole Filter instance to choose from. |
| 1080 | bestFilter().recurse(); |
| 1081 | } |
| 1082 | |
| 1083 | // reportRegion is a helper function for filterProcessor to mark a region as |
| 1084 | // eligible for use as a filter region. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1085 | void ARMFilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit, |
| 1086 | unsigned BitIndex, bool AllowMixed) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1087 | if (RA == ATTR_MIXED && AllowMixed) |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1088 | Filters.push_back(ARMFilter(*this, StartBit, BitIndex - StartBit, true)); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1089 | else if (RA == ATTR_ALL_SET && !AllowMixed) |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1090 | Filters.push_back(ARMFilter(*this, StartBit, BitIndex - StartBit, false)); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | // FilterProcessor scans the well-known encoding bits of the instructions and |
| 1094 | // builds up a list of candidate filters. It chooses the best filter and |
| 1095 | // recursively descends down the decoding tree. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1096 | bool ARMFilterChooser::filterProcessor(bool AllowMixed, bool Greedy) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1097 | Filters.clear(); |
| 1098 | BestIndex = -1; |
| 1099 | unsigned numInstructions = Opcodes.size(); |
| 1100 | |
| 1101 | assert(numInstructions && "Filter created with no instructions"); |
| 1102 | |
| 1103 | // No further filtering is necessary. |
| 1104 | if (numInstructions == 1) |
| 1105 | return true; |
| 1106 | |
| 1107 | // Heuristics. See also doFilter()'s "Heuristics" comment when num of |
| 1108 | // instructions is 3. |
| 1109 | if (AllowMixed && !Greedy) { |
| 1110 | assert(numInstructions == 3); |
| 1111 | |
| 1112 | for (unsigned i = 0; i < Opcodes.size(); ++i) { |
| 1113 | std::vector<unsigned> StartBits; |
| 1114 | std::vector<unsigned> EndBits; |
| 1115 | std::vector<uint64_t> FieldVals; |
| 1116 | insn_t Insn; |
| 1117 | |
| 1118 | insnWithID(Insn, Opcodes[i]); |
| 1119 | |
| 1120 | // Look for islands of undecoded bits of any instruction. |
| 1121 | if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) { |
| 1122 | // Found an instruction with island(s). Now just assign a filter. |
| 1123 | runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1, |
| 1124 | true); |
| 1125 | return true; |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | unsigned BitIndex, InsnIndex; |
| 1131 | |
| 1132 | // We maintain BIT_WIDTH copies of the bitAttrs automaton. |
| 1133 | // The automaton consumes the corresponding bit from each |
| 1134 | // instruction. |
| 1135 | // |
| 1136 | // Input symbols: 0, 1, and _ (unset). |
| 1137 | // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED. |
| 1138 | // Initial state: NONE. |
| 1139 | // |
| 1140 | // (NONE) ------- [01] -> (ALL_SET) |
| 1141 | // (NONE) ------- _ ----> (ALL_UNSET) |
| 1142 | // (ALL_SET) ---- [01] -> (ALL_SET) |
| 1143 | // (ALL_SET) ---- _ ----> (MIXED) |
| 1144 | // (ALL_UNSET) -- [01] -> (MIXED) |
| 1145 | // (ALL_UNSET) -- _ ----> (ALL_UNSET) |
| 1146 | // (MIXED) ------ . ----> (MIXED) |
| 1147 | // (FILTERED)---- . ----> (FILTERED) |
| 1148 | |
| 1149 | bitAttr_t bitAttrs[BIT_WIDTH]; |
| 1150 | |
| 1151 | // FILTERED bit positions provide no entropy and are not worthy of pursuing. |
| 1152 | // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position. |
| 1153 | for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex) |
| 1154 | if (FilterBitValues[BitIndex] == BIT_TRUE || |
| 1155 | FilterBitValues[BitIndex] == BIT_FALSE) |
| 1156 | bitAttrs[BitIndex] = ATTR_FILTERED; |
| 1157 | else |
| 1158 | bitAttrs[BitIndex] = ATTR_NONE; |
| 1159 | |
| 1160 | for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) { |
| 1161 | insn_t insn; |
| 1162 | |
| 1163 | insnWithID(insn, Opcodes[InsnIndex]); |
| 1164 | |
| 1165 | for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex) { |
| 1166 | switch (bitAttrs[BitIndex]) { |
| 1167 | case ATTR_NONE: |
| 1168 | if (insn[BitIndex] == BIT_UNSET) |
| 1169 | bitAttrs[BitIndex] = ATTR_ALL_UNSET; |
| 1170 | else |
| 1171 | bitAttrs[BitIndex] = ATTR_ALL_SET; |
| 1172 | break; |
| 1173 | case ATTR_ALL_SET: |
| 1174 | if (insn[BitIndex] == BIT_UNSET) |
| 1175 | bitAttrs[BitIndex] = ATTR_MIXED; |
| 1176 | break; |
| 1177 | case ATTR_ALL_UNSET: |
| 1178 | if (insn[BitIndex] != BIT_UNSET) |
| 1179 | bitAttrs[BitIndex] = ATTR_MIXED; |
| 1180 | break; |
| 1181 | case ATTR_MIXED: |
| 1182 | case ATTR_FILTERED: |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | // The regionAttr automaton consumes the bitAttrs automatons' state, |
| 1189 | // lowest-to-highest. |
| 1190 | // |
| 1191 | // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed) |
| 1192 | // States: NONE, ALL_SET, MIXED |
| 1193 | // Initial state: NONE |
| 1194 | // |
| 1195 | // (NONE) ----- F --> (NONE) |
| 1196 | // (NONE) ----- S --> (ALL_SET) ; and set region start |
| 1197 | // (NONE) ----- U --> (NONE) |
| 1198 | // (NONE) ----- M --> (MIXED) ; and set region start |
| 1199 | // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region |
| 1200 | // (ALL_SET) -- S --> (ALL_SET) |
| 1201 | // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region |
| 1202 | // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region |
| 1203 | // (MIXED) ---- F --> (NONE) ; and report a MIXED region |
| 1204 | // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region |
| 1205 | // (MIXED) ---- U --> (NONE) ; and report a MIXED region |
| 1206 | // (MIXED) ---- M --> (MIXED) |
| 1207 | |
| 1208 | bitAttr_t RA = ATTR_NONE; |
| 1209 | unsigned StartBit = 0; |
| 1210 | |
| 1211 | for (BitIndex = 0; BitIndex < BIT_WIDTH; BitIndex++) { |
| 1212 | bitAttr_t bitAttr = bitAttrs[BitIndex]; |
| 1213 | |
| 1214 | assert(bitAttr != ATTR_NONE && "Bit without attributes"); |
| 1215 | |
| 1216 | switch (RA) { |
| 1217 | case ATTR_NONE: |
| 1218 | switch (bitAttr) { |
| 1219 | case ATTR_FILTERED: |
| 1220 | break; |
| 1221 | case ATTR_ALL_SET: |
| 1222 | StartBit = BitIndex; |
| 1223 | RA = ATTR_ALL_SET; |
| 1224 | break; |
| 1225 | case ATTR_ALL_UNSET: |
| 1226 | break; |
| 1227 | case ATTR_MIXED: |
| 1228 | StartBit = BitIndex; |
| 1229 | RA = ATTR_MIXED; |
| 1230 | break; |
| 1231 | default: |
| 1232 | assert(0 && "Unexpected bitAttr!"); |
| 1233 | } |
| 1234 | break; |
| 1235 | case ATTR_ALL_SET: |
| 1236 | switch (bitAttr) { |
| 1237 | case ATTR_FILTERED: |
| 1238 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1239 | RA = ATTR_NONE; |
| 1240 | break; |
| 1241 | case ATTR_ALL_SET: |
| 1242 | break; |
| 1243 | case ATTR_ALL_UNSET: |
| 1244 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1245 | RA = ATTR_NONE; |
| 1246 | break; |
| 1247 | case ATTR_MIXED: |
| 1248 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1249 | StartBit = BitIndex; |
| 1250 | RA = ATTR_MIXED; |
| 1251 | break; |
| 1252 | default: |
| 1253 | assert(0 && "Unexpected bitAttr!"); |
| 1254 | } |
| 1255 | break; |
| 1256 | case ATTR_MIXED: |
| 1257 | switch (bitAttr) { |
| 1258 | case ATTR_FILTERED: |
| 1259 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1260 | StartBit = BitIndex; |
| 1261 | RA = ATTR_NONE; |
| 1262 | break; |
| 1263 | case ATTR_ALL_SET: |
| 1264 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1265 | StartBit = BitIndex; |
| 1266 | RA = ATTR_ALL_SET; |
| 1267 | break; |
| 1268 | case ATTR_ALL_UNSET: |
| 1269 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1270 | RA = ATTR_NONE; |
| 1271 | break; |
| 1272 | case ATTR_MIXED: |
| 1273 | break; |
| 1274 | default: |
| 1275 | assert(0 && "Unexpected bitAttr!"); |
| 1276 | } |
| 1277 | break; |
| 1278 | case ATTR_ALL_UNSET: |
| 1279 | assert(0 && "regionAttr state machine has no ATTR_UNSET state"); |
| 1280 | case ATTR_FILTERED: |
| 1281 | assert(0 && "regionAttr state machine has no ATTR_FILTERED state"); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | // At the end, if we're still in ALL_SET or MIXED states, report a region |
| 1286 | switch (RA) { |
| 1287 | case ATTR_NONE: |
| 1288 | break; |
| 1289 | case ATTR_FILTERED: |
| 1290 | break; |
| 1291 | case ATTR_ALL_SET: |
| 1292 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1293 | break; |
| 1294 | case ATTR_ALL_UNSET: |
| 1295 | break; |
| 1296 | case ATTR_MIXED: |
| 1297 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1298 | break; |
| 1299 | } |
| 1300 | |
| 1301 | // We have finished with the filter processings. Now it's time to choose |
| 1302 | // the best performing filter. |
| 1303 | BestIndex = 0; |
| 1304 | bool AllUseless = true; |
| 1305 | unsigned BestScore = 0; |
| 1306 | |
| 1307 | for (unsigned i = 0, e = Filters.size(); i != e; ++i) { |
| 1308 | unsigned Usefulness = Filters[i].usefulness(); |
| 1309 | |
| 1310 | if (Usefulness) |
| 1311 | AllUseless = false; |
| 1312 | |
| 1313 | if (Usefulness > BestScore) { |
| 1314 | BestIndex = i; |
| 1315 | BestScore = Usefulness; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | if (!AllUseless) |
| 1320 | bestFilter().recurse(); |
| 1321 | |
| 1322 | return !AllUseless; |
| 1323 | } // end of FilterChooser::filterProcessor(bool) |
| 1324 | |
| 1325 | // Decides on the best configuration of filter(s) to use in order to decode |
| 1326 | // the instructions. A conflict of instructions may occur, in which case we |
| 1327 | // dump the conflict set to the standard error. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1328 | void ARMFilterChooser::doFilter() { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1329 | unsigned Num = Opcodes.size(); |
| 1330 | assert(Num && "FilterChooser created with no instructions"); |
| 1331 | |
| 1332 | // Heuristics: Use Inst{31-28} as the top level filter for ARM ISA. |
| 1333 | if (TargetName == TARGET_ARM && Parent == NULL) { |
| 1334 | runSingleFilter(*this, 28, 4, false); |
| 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | // Try regions of consecutive known bit values first. |
| 1339 | if (filterProcessor(false)) |
| 1340 | return; |
| 1341 | |
| 1342 | // Then regions of mixed bits (both known and unitialized bit values allowed). |
| 1343 | if (filterProcessor(true)) |
| 1344 | return; |
| 1345 | |
| 1346 | // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where |
| 1347 | // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a |
| 1348 | // well-known encoding pattern. In such case, we backtrack and scan for the |
| 1349 | // the very first consecutive ATTR_ALL_SET region and assign a filter to it. |
| 1350 | if (Num == 3 && filterProcessor(true, false)) |
| 1351 | return; |
| 1352 | |
| 1353 | // If we come to here, the instruction decoding has failed. |
Johnny Chen | e0c74fb | 2010-04-09 19:31:33 +0000 | [diff] [blame] | 1354 | // Set the BestIndex to -1 to indicate so. |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1355 | BestIndex = -1; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | // Emits code to decode our share of instructions. Returns true if the |
| 1359 | // emitted code causes a return, which occurs if we know how to decode |
| 1360 | // the instruction at this level or the instruction is not decodeable. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1361 | bool ARMFilterChooser::emit(raw_ostream &o, unsigned &Indentation) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1362 | if (Opcodes.size() == 1) |
| 1363 | // There is only one instruction in the set, which is great! |
| 1364 | // Call emitSingletonDecoder() to see whether there are any remaining |
| 1365 | // encodings bits. |
| 1366 | return emitSingletonDecoder(o, Indentation, Opcodes[0]); |
| 1367 | |
| 1368 | // Choose the best filter to do the decodings! |
| 1369 | if (BestIndex != -1) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1370 | ARMFilter &Best = bestFilter(); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1371 | if (Best.getNumFiltered() == 1) |
| 1372 | emitSingletonDecoder(o, Indentation, Best); |
| 1373 | else |
| 1374 | bestFilter().emit(o, Indentation); |
| 1375 | return false; |
| 1376 | } |
| 1377 | |
| 1378 | // If we reach here, there is a conflict in decoding. Let's resolve the known |
| 1379 | // conflicts! |
| 1380 | if ((TargetName == TARGET_ARM || TargetName == TARGET_THUMB) && |
| 1381 | Opcodes.size() == 2) { |
| 1382 | // Resolve the known conflict sets: |
| 1383 | // |
| 1384 | // 1. source registers are identical => VMOVDneon; otherwise => VORRd |
| 1385 | // 2. source registers are identical => VMOVQ; otherwise => VORRq |
| 1386 | // 3. LDR, LDRcp => return LDR for now. |
| 1387 | // FIXME: How can we distinguish between LDR and LDRcp? Do we need to? |
Johnny Chen | 8c13335 | 2011-03-24 23:42:31 +0000 | [diff] [blame] | 1388 | // 4. tLDMIA, tLDMIA_UPD => Rn = Inst{10-8}, reglist = Inst{7-0}, |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1389 | // wback = registers<Rn> = 0 |
| 1390 | // NOTE: (tLDM, tLDM_UPD) resolution must come before Advanced SIMD |
| 1391 | // addressing mode resolution!!! |
| 1392 | // 5. VLD[234]LN*/VST[234]LN* vs. VLD[234]LN*_UPD/VST[234]LN*_UPD conflicts |
| 1393 | // are resolved returning the non-UPD versions of the instructions if the |
| 1394 | // Rm field, i.e., Inst{3-0} is 0b1111. This is specified in A7.7.1 |
| 1395 | // Advanced SIMD addressing mode. |
| 1396 | const std::string &name1 = nameWithID(Opcodes[0]); |
| 1397 | const std::string &name2 = nameWithID(Opcodes[1]); |
| 1398 | if ((name1 == "VMOVDneon" && name2 == "VORRd") || |
| 1399 | (name1 == "VMOVQ" && name2 == "VORRq")) { |
| 1400 | // Inserting the opening curly brace for this case block. |
| 1401 | --Indentation; --Indentation; |
| 1402 | o.indent(Indentation) << "{\n"; |
| 1403 | ++Indentation; ++Indentation; |
| 1404 | |
| 1405 | o.indent(Indentation) |
| 1406 | << "field_t N = fieldFromInstruction(insn, 7, 1), " |
| 1407 | << "M = fieldFromInstruction(insn, 5, 1);\n"; |
| 1408 | o.indent(Indentation) |
| 1409 | << "field_t Vn = fieldFromInstruction(insn, 16, 4), " |
| 1410 | << "Vm = fieldFromInstruction(insn, 0, 4);\n"; |
| 1411 | o.indent(Indentation) |
| 1412 | << "return (N == M && Vn == Vm) ? " |
| 1413 | << Opcodes[0] << " /* " << name1 << " */ : " |
| 1414 | << Opcodes[1] << " /* " << name2 << " */ ;\n"; |
| 1415 | |
| 1416 | // Inserting the closing curly brace for this case block. |
| 1417 | --Indentation; --Indentation; |
| 1418 | o.indent(Indentation) << "}\n"; |
| 1419 | ++Indentation; ++Indentation; |
| 1420 | |
| 1421 | return true; |
| 1422 | } |
| 1423 | if (name1 == "LDR" && name2 == "LDRcp") { |
| 1424 | o.indent(Indentation) |
| 1425 | << "return " << Opcodes[0] |
| 1426 | << "; // Returning LDR for {LDR, LDRcp}\n"; |
| 1427 | return true; |
| 1428 | } |
Johnny Chen | 8c13335 | 2011-03-24 23:42:31 +0000 | [diff] [blame] | 1429 | if (name1 == "tLDMIA" && name2 == "tLDMIA_UPD") { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1430 | // Inserting the opening curly brace for this case block. |
| 1431 | --Indentation; --Indentation; |
| 1432 | o.indent(Indentation) << "{\n"; |
| 1433 | ++Indentation; ++Indentation; |
| 1434 | |
| 1435 | o.indent(Indentation) |
| 1436 | << "unsigned Rn = fieldFromInstruction(insn, 8, 3), " |
| 1437 | << "list = fieldFromInstruction(insn, 0, 8);\n"; |
| 1438 | o.indent(Indentation) |
| 1439 | << "return ((list >> Rn) & 1) == 0 ? " |
| 1440 | << Opcodes[1] << " /* " << name2 << " */ : " |
| 1441 | << Opcodes[0] << " /* " << name1 << " */ ;\n"; |
| 1442 | |
| 1443 | // Inserting the closing curly brace for this case block. |
| 1444 | --Indentation; --Indentation; |
| 1445 | o.indent(Indentation) << "}\n"; |
| 1446 | ++Indentation; ++Indentation; |
| 1447 | |
| 1448 | return true; |
| 1449 | } |
| 1450 | if (sameStringExceptSuffix(name1, name2, "_UPD")) { |
| 1451 | o.indent(Indentation) |
| 1452 | << "return fieldFromInstruction(insn, 0, 4) == 15 ? " << Opcodes[0] |
| 1453 | << " /* " << name1 << " */ : " << Opcodes[1] << "/* " << name2 |
| 1454 | << " */ ; // Advanced SIMD addressing mode\n"; |
| 1455 | return true; |
| 1456 | } |
| 1457 | |
| 1458 | // Otherwise, it does not belong to the known conflict sets. |
| 1459 | } |
Johnny Chen | e0c74fb | 2010-04-09 19:31:33 +0000 | [diff] [blame] | 1460 | |
| 1461 | // We don't know how to decode these instructions! Return 0 and dump the |
| 1462 | // conflict set! |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1463 | o.indent(Indentation) << "return 0;" << " // Conflict set: "; |
| 1464 | for (int i = 0, N = Opcodes.size(); i < N; ++i) { |
| 1465 | o << nameWithID(Opcodes[i]); |
| 1466 | if (i < (N - 1)) |
| 1467 | o << ", "; |
| 1468 | else |
| 1469 | o << '\n'; |
| 1470 | } |
Johnny Chen | e0c74fb | 2010-04-09 19:31:33 +0000 | [diff] [blame] | 1471 | |
| 1472 | // Print out useful conflict information for postmortem analysis. |
| 1473 | errs() << "Decoding Conflict:\n"; |
| 1474 | |
| 1475 | dumpStack(errs(), "\t\t"); |
| 1476 | |
| 1477 | for (unsigned i = 0; i < Opcodes.size(); i++) { |
| 1478 | const std::string &Name = nameWithID(Opcodes[i]); |
| 1479 | |
| 1480 | errs() << '\t' << Name << " "; |
| 1481 | dumpBits(errs(), |
| 1482 | getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst")); |
| 1483 | errs() << '\n'; |
| 1484 | } |
| 1485 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1486 | return true; |
| 1487 | } |
| 1488 | |
| 1489 | |
| 1490 | //////////////////////////////////////////// |
| 1491 | // // |
| 1492 | // ARMDEBackend // |
| 1493 | // (Helper class for ARMDecoderEmitter) // |
| 1494 | // // |
| 1495 | //////////////////////////////////////////// |
| 1496 | |
| 1497 | class ARMDecoderEmitter::ARMDEBackend { |
| 1498 | public: |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1499 | ARMDEBackend(ARMDecoderEmitter &frontend, RecordKeeper &Records) : |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1500 | NumberedInstructions(), |
| 1501 | Opcodes(), |
| 1502 | Frontend(frontend), |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1503 | Target(Records), |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1504 | FC(NULL) |
| 1505 | { |
| 1506 | if (Target.getName() == "ARM") |
| 1507 | TargetName = TARGET_ARM; |
| 1508 | else { |
| 1509 | errs() << "Target name " << Target.getName() << " not recognized\n"; |
| 1510 | assert(0 && "Unknown target"); |
| 1511 | } |
| 1512 | |
| 1513 | // Populate the instructions for our TargetName. |
| 1514 | populateInstructions(); |
| 1515 | } |
| 1516 | |
| 1517 | ~ARMDEBackend() { |
| 1518 | if (FC) { |
| 1519 | delete FC; |
| 1520 | FC = NULL; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | void getInstructionsByEnumValue(std::vector<const CodeGenInstruction*> |
| 1525 | &NumberedInstructions) { |
| 1526 | // We must emit the PHI opcode first... |
| 1527 | std::string Namespace = Target.getInstNamespace(); |
| 1528 | assert(!Namespace.empty() && "No instructions defined."); |
| 1529 | |
| 1530 | NumberedInstructions = Target.getInstructionsByEnumValue(); |
| 1531 | } |
| 1532 | |
| 1533 | bool populateInstruction(const CodeGenInstruction &CGI, TARGET_NAME_t TN); |
| 1534 | |
| 1535 | void populateInstructions(); |
| 1536 | |
| 1537 | // Emits disassembler code for instruction decoding. This delegates to the |
| 1538 | // FilterChooser instance to do the heavy lifting. |
| 1539 | void emit(raw_ostream &o); |
| 1540 | |
| 1541 | protected: |
| 1542 | std::vector<const CodeGenInstruction*> NumberedInstructions; |
| 1543 | std::vector<unsigned> Opcodes; |
| 1544 | // Special case for the ARM chip, which supports ARM and Thumb ISAs. |
| 1545 | // Opcodes2 will be populated with the Thumb opcodes. |
| 1546 | std::vector<unsigned> Opcodes2; |
| 1547 | ARMDecoderEmitter &Frontend; |
| 1548 | CodeGenTarget Target; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1549 | ARMFilterChooser *FC; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1550 | |
| 1551 | TARGET_NAME_t TargetName; |
| 1552 | }; |
| 1553 | |
Jim Grosbach | 37cc2f1 | 2010-11-30 19:00:13 +0000 | [diff] [blame] | 1554 | bool ARMDecoderEmitter:: |
| 1555 | ARMDEBackend::populateInstruction(const CodeGenInstruction &CGI, |
| 1556 | TARGET_NAME_t TN) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1557 | const Record &Def = *CGI.TheDef; |
| 1558 | const StringRef Name = Def.getName(); |
| 1559 | uint8_t Form = getByteField(Def, "Form"); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1560 | |
Johnny Chen | 1808e4d | 2010-04-09 21:01:02 +0000 | [diff] [blame] | 1561 | BitsInit &Bits = getBitsField(Def, "Inst"); |
| 1562 | |
| 1563 | // If all the bit positions are not specified; do not decode this instruction. |
| 1564 | // We are bound to fail! For proper disassembly, the well-known encoding bits |
| 1565 | // of the instruction must be fully specified. |
| 1566 | // |
| 1567 | // This also removes pseudo instructions from considerations of disassembly, |
| 1568 | // which is a better design and less fragile than the name matchings. |
| 1569 | if (Bits.allInComplete()) return false; |
| 1570 | |
Bruno Cardoso Lopes | a461d42 | 2011-01-18 20:45:56 +0000 | [diff] [blame] | 1571 | // Ignore "asm parser only" instructions. |
| 1572 | if (Def.getValueAsBit("isAsmParserOnly")) |
| 1573 | return false; |
| 1574 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1575 | if (TN == TARGET_ARM) { |
Jim Grosbach | 5e97338 | 2011-03-10 19:05:48 +0000 | [diff] [blame] | 1576 | if (Form == ARM_FORMAT_PSEUDO) |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1577 | return false; |
| 1578 | if (thumbInstruction(Form)) |
| 1579 | return false; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1580 | |
Dale Johannesen | 51e28e6 | 2010-06-03 21:09:53 +0000 | [diff] [blame] | 1581 | // Tail calls are other patterns that generate existing instructions. |
| 1582 | if (Name == "TCRETURNdi" || Name == "TCRETURNdiND" || |
| 1583 | Name == "TCRETURNri" || Name == "TCRETURNriND" || |
Dale Johannesen | 7835f1f | 2010-07-08 01:18:23 +0000 | [diff] [blame] | 1584 | Name == "TAILJMPd" || Name == "TAILJMPdt" || |
| 1585 | Name == "TAILJMPdND" || Name == "TAILJMPdNDt" || |
Dale Johannesen | 6470a11 | 2010-06-15 22:08:33 +0000 | [diff] [blame] | 1586 | Name == "TAILJMPr" || Name == "TAILJMPrND" || |
| 1587 | Name == "MOVr_TC") |
Dale Johannesen | 51e28e6 | 2010-06-03 21:09:53 +0000 | [diff] [blame] | 1588 | return false; |
| 1589 | |
Johnny Chen | e6d69e7 | 2011-03-24 20:42:48 +0000 | [diff] [blame] | 1590 | // Delegate ADR disassembly to the more generic ADDri/SUBri instructions. |
| 1591 | if (Name == "ADR") |
| 1592 | return false; |
| 1593 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1594 | // |
| 1595 | // The following special cases are for conflict resolutions. |
| 1596 | // |
| 1597 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1598 | // A8-598: VEXT |
| 1599 | // Vector Extract extracts elements from the bottom end of the second |
| 1600 | // operand vector and the top end of the first, concatenates them and |
| 1601 | // places the result in the destination vector. The elements of the |
| 1602 | // vectors are treated as being 8-bit bitfields. There is no distinction |
| 1603 | // between data types. The size of the operation can be specified in |
| 1604 | // assembler as vext.size. If the value is 16, 32, or 64, the syntax is |
| 1605 | // a pseudo-instruction for a VEXT instruction specifying the equivalent |
| 1606 | // number of bytes. |
| 1607 | // |
| 1608 | // Variants VEXTd16, VEXTd32, VEXTd8, and VEXTdf are reduced to VEXTd8; |
| 1609 | // variants VEXTq16, VEXTq32, VEXTq8, and VEXTqf are reduced to VEXTq8. |
| 1610 | if (Name == "VEXTd16" || Name == "VEXTd32" || Name == "VEXTdf" || |
| 1611 | Name == "VEXTq16" || Name == "VEXTq32" || Name == "VEXTqf") |
| 1612 | return false; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1613 | } else if (TN == TARGET_THUMB) { |
| 1614 | if (!thumbInstruction(Form)) |
| 1615 | return false; |
| 1616 | |
Johnny Chen | 1090d77 | 2011-03-24 23:21:14 +0000 | [diff] [blame] | 1617 | // A8.6.189 STM / STMIA / STMEA -- Encoding T1 |
| 1618 | // There's only STMIA_UPD for Thumb1. |
| 1619 | if (Name == "tSTMIA") |
| 1620 | return false; |
| 1621 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1622 | // On Darwin R9 is call-clobbered. Ignore the non-Darwin counterparts. |
| 1623 | if (Name == "tBL" || Name == "tBLXi" || Name == "tBLXr") |
| 1624 | return false; |
| 1625 | |
Johnny Chen | de16508 | 2011-04-11 23:33:30 +0000 | [diff] [blame] | 1626 | // A8.6.25 BX. Use the generic tBX_Rm, ignore tBX_RET and tBX_RET_vararg. |
| 1627 | if (Name == "tBX_RET" || Name == "tBX_RET_vararg") |
| 1628 | return false; |
| 1629 | |
Jim Grosbach | d40963c | 2010-12-14 22:28:03 +0000 | [diff] [blame] | 1630 | // Ignore tADR, prefer tADDrPCi. |
| 1631 | if (Name == "tADR") |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1632 | return false; |
| 1633 | |
Johnny Chen | ef74e9a | 2011-03-25 00:17:42 +0000 | [diff] [blame] | 1634 | // Delegate t2ADR disassembly to the more generic t2ADDri12/t2SUBri12 |
| 1635 | // instructions. |
| 1636 | if (Name == "t2ADR") |
| 1637 | return false; |
| 1638 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1639 | // Ignore tADDrSP, tADDspr, and tPICADD, prefer the generic tADDhirr. |
| 1640 | // Ignore t2SUBrSPs, prefer the t2SUB[S]r[r|s]. |
| 1641 | // Ignore t2ADDrSPs, prefer the t2ADD[S]r[r|s]. |
| 1642 | if (Name == "tADDrSP" || Name == "tADDspr" || Name == "tPICADD" || |
Jim Grosbach | f6fd909 | 2011-06-29 23:25:04 +0000 | [diff] [blame] | 1643 | Name == "t2SUBrSPs" || Name == "t2ADDrSPs") |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1644 | return false; |
| 1645 | |
Johnny Chen | 597fa65 | 2011-04-22 19:12:43 +0000 | [diff] [blame] | 1646 | // FIXME: Use ldr.n to work around a Darwin assembler bug. |
| 1647 | // Introduce a workaround with tLDRpciDIS opcode. |
| 1648 | if (Name == "tLDRpci") |
| 1649 | return false; |
| 1650 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1651 | // Ignore t2LDRDpci, prefer the generic t2LDRDi8, t2LDRD_PRE, t2LDRD_POST. |
| 1652 | if (Name == "t2LDRDpci") |
| 1653 | return false; |
| 1654 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1655 | // Resolve conflicts: |
| 1656 | // |
| 1657 | // tBfar conflicts with tBLr9 |
Jim Grosbach | 4629d50 | 2011-06-30 17:34:04 +0000 | [diff] [blame] | 1658 | // t2LDMIA_RET conflict with t2LDM (ditto) |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1659 | // tMOVCCi conflicts with tMOVi8 |
| 1660 | // tMOVCCr conflicts with tMOVgpr2gpr |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1661 | // tLDRcp conflicts with tLDRspi |
Jim Grosbach | 0e99219 | 2010-10-07 22:14:01 +0000 | [diff] [blame] | 1662 | // t2MOVCCi16 conflicts with tMOVi16 |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1663 | if (Name == "tBfar" || |
Jim Grosbach | 4629d50 | 2011-06-30 17:34:04 +0000 | [diff] [blame] | 1664 | Name == "t2LDMIA_RET" || |
Jim Grosbach | 5ca6669 | 2010-11-29 22:37:40 +0000 | [diff] [blame] | 1665 | Name == "tMOVCCi" || Name == "tMOVCCr" || |
Jim Grosbach | 74472b4 | 2011-06-29 20:26:39 +0000 | [diff] [blame] | 1666 | Name == "tLDRcp" || |
Jim Grosbach | d40963c | 2010-12-14 22:28:03 +0000 | [diff] [blame] | 1667 | Name == "t2MOVCCi16") |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1668 | return false; |
| 1669 | } |
| 1670 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1671 | DEBUG({ |
Bill Wendling | 6a97ed3 | 2010-12-13 01:03:49 +0000 | [diff] [blame] | 1672 | // Dumps the instruction encoding format. |
| 1673 | switch (TargetName) { |
| 1674 | case TARGET_ARM: |
| 1675 | case TARGET_THUMB: |
| 1676 | errs() << Name << " " << stringForARMFormat((ARMFormat)Form); |
| 1677 | break; |
| 1678 | } |
| 1679 | |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1680 | errs() << " "; |
| 1681 | |
| 1682 | // Dumps the instruction encoding bits. |
| 1683 | dumpBits(errs(), Bits); |
| 1684 | |
| 1685 | errs() << '\n'; |
| 1686 | |
| 1687 | // Dumps the list of operand info. |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 1688 | for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) { |
| 1689 | const CGIOperandList::OperandInfo &Info = CGI.Operands[i]; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1690 | const std::string &OperandName = Info.Name; |
| 1691 | const Record &OperandDef = *Info.Rec; |
| 1692 | |
| 1693 | errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n"; |
| 1694 | } |
| 1695 | }); |
| 1696 | |
| 1697 | return true; |
| 1698 | } |
| 1699 | |
| 1700 | void ARMDecoderEmitter::ARMDEBackend::populateInstructions() { |
| 1701 | getInstructionsByEnumValue(NumberedInstructions); |
| 1702 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1703 | unsigned numUIDs = NumberedInstructions.size(); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1704 | if (TargetName == TARGET_ARM) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1705 | for (unsigned uid = 0; uid < numUIDs; uid++) { |
| 1706 | // filter out intrinsics |
| 1707 | if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM")) |
| 1708 | continue; |
| 1709 | |
| 1710 | if (populateInstruction(*NumberedInstructions[uid], TargetName)) |
| 1711 | Opcodes.push_back(uid); |
| 1712 | } |
| 1713 | |
| 1714 | // Special handling for the ARM chip, which supports two modes of execution. |
| 1715 | // This branch handles the Thumb opcodes. |
| 1716 | for (unsigned uid = 0; uid < numUIDs; uid++) { |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1717 | // filter out intrinsics |
| 1718 | if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM") |
| 1719 | && !NumberedInstructions[uid]->TheDef->isSubClassOf("InstThumb")) |
| 1720 | continue; |
| 1721 | |
| 1722 | if (populateInstruction(*NumberedInstructions[uid], TARGET_THUMB)) |
| 1723 | Opcodes2.push_back(uid); |
| 1724 | } |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1725 | |
| 1726 | return; |
| 1727 | } |
| 1728 | |
| 1729 | // For other targets. |
| 1730 | for (unsigned uid = 0; uid < numUIDs; uid++) { |
| 1731 | Record *R = NumberedInstructions[uid]->TheDef; |
| 1732 | if (R->getValueAsString("Namespace") == "TargetOpcode") |
| 1733 | continue; |
| 1734 | |
| 1735 | if (populateInstruction(*NumberedInstructions[uid], TargetName)) |
| 1736 | Opcodes.push_back(uid); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | // Emits disassembler code for instruction decoding. This delegates to the |
| 1741 | // FilterChooser instance to do the heavy lifting. |
| 1742 | void ARMDecoderEmitter::ARMDEBackend::emit(raw_ostream &o) { |
| 1743 | switch (TargetName) { |
| 1744 | case TARGET_ARM: |
| 1745 | Frontend.EmitSourceFileHeader("ARM/Thumb Decoders", o); |
| 1746 | break; |
| 1747 | default: |
| 1748 | assert(0 && "Unreachable code!"); |
| 1749 | } |
| 1750 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 1751 | o << "#include \"llvm/Support/DataTypes.h\"\n"; |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1752 | o << "#include <assert.h>\n"; |
| 1753 | o << '\n'; |
| 1754 | o << "namespace llvm {\n\n"; |
| 1755 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1756 | ARMFilterChooser::setTargetName(TargetName); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1757 | |
| 1758 | switch (TargetName) { |
| 1759 | case TARGET_ARM: { |
| 1760 | // Emit common utility and ARM ISA decoder. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1761 | FC = new ARMFilterChooser(NumberedInstructions, Opcodes); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1762 | // Reset indentation level. |
| 1763 | unsigned Indentation = 0; |
| 1764 | FC->emitTop(o, Indentation); |
| 1765 | delete FC; |
| 1766 | |
| 1767 | // Emit Thumb ISA decoder as well. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1768 | ARMFilterChooser::setTargetName(TARGET_THUMB); |
| 1769 | FC = new ARMFilterChooser(NumberedInstructions, Opcodes2); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1770 | // Reset indentation level. |
| 1771 | Indentation = 0; |
| 1772 | FC->emitBot(o, Indentation); |
| 1773 | break; |
| 1774 | } |
| 1775 | default: |
| 1776 | assert(0 && "Unreachable code!"); |
| 1777 | } |
| 1778 | |
| 1779 | o << "\n} // End llvm namespace \n"; |
| 1780 | } |
| 1781 | |
| 1782 | ///////////////////////// |
| 1783 | // Backend interface // |
| 1784 | ///////////////////////// |
| 1785 | |
| 1786 | void ARMDecoderEmitter::initBackend() |
| 1787 | { |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1788 | Backend = new ARMDEBackend(*this, Records); |
Johnny Chen | b68a3ee | 2010-04-02 22:27:38 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | void ARMDecoderEmitter::run(raw_ostream &o) |
| 1792 | { |
| 1793 | Backend->emit(o); |
| 1794 | } |
| 1795 | |
| 1796 | void ARMDecoderEmitter::shutdownBackend() |
| 1797 | { |
| 1798 | delete Backend; |
| 1799 | Backend = NULL; |
| 1800 | } |