Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1 | //===------------ FixedLenDecoderEmitter.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 | // It contains the tablegen backend that emits the decoder functions for |
| 11 | // targets with fixed length instruction set. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "decoder-emitter" |
| 16 | |
| 17 | #include "FixedLenDecoderEmitter.h" |
| 18 | #include "CodeGenTarget.h" |
| 19 | #include "Record.h" |
| 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | #include <vector> |
| 25 | #include <map> |
| 26 | #include <string> |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system |
| 31 | // for a bit value. |
| 32 | // |
| 33 | // BIT_UNFILTERED is used as the init value for a filter position. It is used |
| 34 | // only for filter processings. |
| 35 | typedef enum { |
| 36 | BIT_TRUE, // '1' |
| 37 | BIT_FALSE, // '0' |
| 38 | BIT_UNSET, // '?' |
| 39 | BIT_UNFILTERED // unfiltered |
| 40 | } bit_value_t; |
| 41 | |
| 42 | static bool ValueSet(bit_value_t V) { |
| 43 | return (V == BIT_TRUE || V == BIT_FALSE); |
| 44 | } |
| 45 | static bool ValueNotSet(bit_value_t V) { |
| 46 | return (V == BIT_UNSET); |
| 47 | } |
| 48 | static int Value(bit_value_t V) { |
| 49 | return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1); |
| 50 | } |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 51 | static bit_value_t bitFromBits(BitsInit &bits, unsigned index) { |
| 52 | if (BitInit *bit = dynamic_cast<BitInit*>(bits.getBit(index))) |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 53 | return bit->getValue() ? BIT_TRUE : BIT_FALSE; |
| 54 | |
| 55 | // The bit is uninitialized. |
| 56 | return BIT_UNSET; |
| 57 | } |
| 58 | // Prints the bit value for each position. |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 59 | static void dumpBits(raw_ostream &o, BitsInit &bits) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 60 | unsigned index; |
| 61 | |
| 62 | for (index = bits.getNumBits(); index > 0; index--) { |
| 63 | switch (bitFromBits(bits, index - 1)) { |
| 64 | case BIT_TRUE: |
| 65 | o << "1"; |
| 66 | break; |
| 67 | case BIT_FALSE: |
| 68 | o << "0"; |
| 69 | break; |
| 70 | case BIT_UNSET: |
| 71 | o << "_"; |
| 72 | break; |
| 73 | default: |
| 74 | assert(0 && "unexpected return value from bitFromBits"); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 79 | static BitsInit &getBitsField(const Record &def, const char *str) { |
| 80 | BitsInit *bits = def.getValueAsBitsInit(str); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 81 | return *bits; |
| 82 | } |
| 83 | |
| 84 | // Forward declaration. |
| 85 | class FilterChooser; |
| 86 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 87 | // Representation of the instruction to work on. |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 88 | typedef std::vector<bit_value_t> insn_t; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 89 | |
| 90 | /// Filter - Filter works with FilterChooser to produce the decoding tree for |
| 91 | /// the ISA. |
| 92 | /// |
| 93 | /// It is useful to think of a Filter as governing the switch stmts of the |
| 94 | /// decoding tree in a certain level. Each case stmt delegates to an inferior |
| 95 | /// FilterChooser to decide what further decoding logic to employ, or in another |
| 96 | /// words, what other remaining bits to look at. The FilterChooser eventually |
| 97 | /// chooses a best Filter to do its job. |
| 98 | /// |
| 99 | /// This recursive scheme ends when the number of Opcodes assigned to the |
| 100 | /// FilterChooser becomes 1 or if there is a conflict. A conflict happens when |
| 101 | /// the Filter/FilterChooser combo does not know how to distinguish among the |
| 102 | /// Opcodes assigned. |
| 103 | /// |
| 104 | /// An example of a conflict is |
| 105 | /// |
| 106 | /// Conflict: |
| 107 | /// 111101000.00........00010000.... |
| 108 | /// 111101000.00........0001........ |
| 109 | /// 1111010...00........0001........ |
| 110 | /// 1111010...00.................... |
| 111 | /// 1111010......................... |
| 112 | /// 1111............................ |
| 113 | /// ................................ |
| 114 | /// VST4q8a 111101000_00________00010000____ |
| 115 | /// VST4q8b 111101000_00________00010000____ |
| 116 | /// |
| 117 | /// The Debug output shows the path that the decoding tree follows to reach the |
| 118 | /// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced |
| 119 | /// even registers, while VST4q8b is a vst4 to double-spaced odd regsisters. |
| 120 | /// |
| 121 | /// The encoding info in the .td files does not specify this meta information, |
| 122 | /// which could have been used by the decoder to resolve the conflict. The |
| 123 | /// decoder could try to decode the even/odd register numbering and assign to |
| 124 | /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a" |
| 125 | /// version and return the Opcode since the two have the same Asm format string. |
| 126 | class Filter { |
| 127 | protected: |
| 128 | FilterChooser *Owner; // points to the FilterChooser who owns this filter |
| 129 | unsigned StartBit; // the starting bit position |
| 130 | unsigned NumBits; // number of bits to filter |
| 131 | bool Mixed; // a mixed region contains both set and unset bits |
| 132 | |
| 133 | // Map of well-known segment value to the set of uid's with that value. |
| 134 | std::map<uint64_t, std::vector<unsigned> > FilteredInstructions; |
| 135 | |
| 136 | // Set of uid's with non-constant segment values. |
| 137 | std::vector<unsigned> VariableInstructions; |
| 138 | |
| 139 | // Map of well-known segment value to its delegate. |
| 140 | std::map<unsigned, FilterChooser*> FilterChooserMap; |
| 141 | |
| 142 | // Number of instructions which fall under FilteredInstructions category. |
| 143 | unsigned NumFiltered; |
| 144 | |
| 145 | // Keeps track of the last opcode in the filtered bucket. |
| 146 | unsigned LastOpcFiltered; |
| 147 | |
| 148 | // Number of instructions which fall under VariableInstructions category. |
| 149 | unsigned NumVariable; |
| 150 | |
| 151 | public: |
| 152 | unsigned getNumFiltered() { return NumFiltered; } |
| 153 | unsigned getNumVariable() { return NumVariable; } |
| 154 | unsigned getSingletonOpc() { |
| 155 | assert(NumFiltered == 1); |
| 156 | return LastOpcFiltered; |
| 157 | } |
| 158 | // Return the filter chooser for the group of instructions without constant |
| 159 | // segment values. |
| 160 | FilterChooser &getVariableFC() { |
| 161 | assert(NumFiltered == 1); |
| 162 | assert(FilterChooserMap.size() == 1); |
| 163 | return *(FilterChooserMap.find((unsigned)-1)->second); |
| 164 | } |
| 165 | |
| 166 | Filter(const Filter &f); |
| 167 | Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed); |
| 168 | |
| 169 | ~Filter(); |
| 170 | |
| 171 | // Divides the decoding task into sub tasks and delegates them to the |
| 172 | // inferior FilterChooser's. |
| 173 | // |
| 174 | // A special case arises when there's only one entry in the filtered |
| 175 | // instructions. In order to unambiguously decode the singleton, we need to |
| 176 | // match the remaining undecoded encoding bits against the singleton. |
| 177 | void recurse(); |
| 178 | |
| 179 | // Emit code to decode instructions given a segment or segments of bits. |
| 180 | void emit(raw_ostream &o, unsigned &Indentation); |
| 181 | |
| 182 | // Returns the number of fanout produced by the filter. More fanout implies |
| 183 | // the filter distinguishes more categories of instructions. |
| 184 | unsigned usefulness() const; |
| 185 | }; // End of class Filter |
| 186 | |
| 187 | // These are states of our finite state machines used in FilterChooser's |
| 188 | // filterProcessor() which produces the filter candidates to use. |
| 189 | typedef enum { |
| 190 | ATTR_NONE, |
| 191 | ATTR_FILTERED, |
| 192 | ATTR_ALL_SET, |
| 193 | ATTR_ALL_UNSET, |
| 194 | ATTR_MIXED |
| 195 | } bitAttr_t; |
| 196 | |
| 197 | /// FilterChooser - FilterChooser chooses the best filter among a set of Filters |
| 198 | /// in order to perform the decoding of instructions at the current level. |
| 199 | /// |
| 200 | /// Decoding proceeds from the top down. Based on the well-known encoding bits |
| 201 | /// of instructions available, FilterChooser builds up the possible Filters that |
| 202 | /// can further the task of decoding by distinguishing among the remaining |
| 203 | /// candidate instructions. |
| 204 | /// |
| 205 | /// Once a filter has been chosen, it is called upon to divide the decoding task |
| 206 | /// into sub-tasks and delegates them to its inferior FilterChoosers for further |
| 207 | /// processings. |
| 208 | /// |
| 209 | /// It is useful to think of a Filter as governing the switch stmts of the |
| 210 | /// decoding tree. And each case is delegated to an inferior FilterChooser to |
| 211 | /// decide what further remaining bits to look at. |
| 212 | class FilterChooser { |
| 213 | protected: |
| 214 | friend class Filter; |
| 215 | |
| 216 | // Vector of codegen instructions to choose our filter. |
| 217 | const std::vector<const CodeGenInstruction*> &AllInstructions; |
| 218 | |
| 219 | // Vector of uid's for this filter chooser to work on. |
| 220 | const std::vector<unsigned> Opcodes; |
| 221 | |
| 222 | // Lookup table for the operand decoding of instructions. |
| 223 | std::map<unsigned, std::vector<OperandInfo> > &Operands; |
| 224 | |
| 225 | // Vector of candidate filters. |
| 226 | std::vector<Filter> Filters; |
| 227 | |
| 228 | // Array of bit values passed down from our parent. |
| 229 | // Set to all BIT_UNFILTERED's for Parent == NULL. |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 230 | std::vector<bit_value_t> FilterBitValues; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 231 | |
| 232 | // Links to the FilterChooser above us in the decoding tree. |
| 233 | FilterChooser *Parent; |
| 234 | |
| 235 | // Index of the best filter from Filters. |
| 236 | int BestIndex; |
| 237 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 238 | // Width of instructions |
| 239 | unsigned BitWidth; |
| 240 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 241 | public: |
| 242 | FilterChooser(const FilterChooser &FC) : |
| 243 | AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes), |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 244 | Operands(FC.Operands), Filters(FC.Filters), |
| 245 | FilterBitValues(FC.FilterBitValues), Parent(FC.Parent), |
| 246 | BestIndex(FC.BestIndex), BitWidth(FC.BitWidth) { } |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 247 | |
| 248 | FilterChooser(const std::vector<const CodeGenInstruction*> &Insts, |
| 249 | const std::vector<unsigned> &IDs, |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 250 | std::map<unsigned, std::vector<OperandInfo> > &Ops, |
| 251 | unsigned BW) : |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 252 | AllInstructions(Insts), Opcodes(IDs), Operands(Ops), Filters(), |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 253 | Parent(NULL), BestIndex(-1), BitWidth(BW) { |
| 254 | for (unsigned i = 0; i < BitWidth; ++i) |
| 255 | FilterBitValues.push_back(BIT_UNFILTERED); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 256 | |
| 257 | doFilter(); |
| 258 | } |
| 259 | |
| 260 | FilterChooser(const std::vector<const CodeGenInstruction*> &Insts, |
| 261 | const std::vector<unsigned> &IDs, |
| 262 | std::map<unsigned, std::vector<OperandInfo> > &Ops, |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 263 | std::vector<bit_value_t> &ParentFilterBitValues, |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 264 | FilterChooser &parent) : |
| 265 | AllInstructions(Insts), Opcodes(IDs), Operands(Ops), |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 266 | Filters(), FilterBitValues(ParentFilterBitValues), |
| 267 | Parent(&parent), BestIndex(-1), BitWidth(parent.BitWidth) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 268 | doFilter(); |
| 269 | } |
| 270 | |
| 271 | // The top level filter chooser has NULL as its parent. |
| 272 | bool isTopLevel() { return Parent == NULL; } |
| 273 | |
| 274 | // Emit the top level typedef and decodeInstruction() function. |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 275 | void emitTop(raw_ostream &o, unsigned Indentation, std::string Namespace); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 276 | |
| 277 | protected: |
| 278 | // Populates the insn given the uid. |
| 279 | void insnWithID(insn_t &Insn, unsigned Opcode) const { |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 280 | BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst"); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 281 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 282 | for (unsigned i = 0; i < BitWidth; ++i) |
| 283 | Insn.push_back(bitFromBits(Bits, i)); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Returns the record name. |
| 287 | const std::string &nameWithID(unsigned Opcode) const { |
| 288 | return AllInstructions[Opcode]->TheDef->getName(); |
| 289 | } |
| 290 | |
| 291 | // Populates the field of the insn given the start position and the number of |
| 292 | // consecutive bits to scan for. |
| 293 | // |
| 294 | // Returns false if there exists any uninitialized bit value in the range. |
| 295 | // Returns true, otherwise. |
| 296 | bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit, |
| 297 | unsigned NumBits) const; |
| 298 | |
| 299 | /// dumpFilterArray - dumpFilterArray prints out debugging info for the given |
| 300 | /// filter array as a series of chars. |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 301 | void dumpFilterArray(raw_ostream &o, std::vector<bit_value_t> & filter); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 302 | |
| 303 | /// dumpStack - dumpStack traverses the filter chooser chain and calls |
| 304 | /// dumpFilterArray on each filter chooser up to the top level one. |
| 305 | void dumpStack(raw_ostream &o, const char *prefix); |
| 306 | |
| 307 | Filter &bestFilter() { |
| 308 | assert(BestIndex != -1 && "BestIndex not set"); |
| 309 | return Filters[BestIndex]; |
| 310 | } |
| 311 | |
| 312 | // Called from Filter::recurse() when singleton exists. For debug purpose. |
| 313 | void SingletonExists(unsigned Opc); |
| 314 | |
| 315 | bool PositionFiltered(unsigned i) { |
| 316 | return ValueSet(FilterBitValues[i]); |
| 317 | } |
| 318 | |
| 319 | // Calculates the island(s) needed to decode the instruction. |
| 320 | // This returns a lit of undecoded bits of an instructions, for example, |
| 321 | // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be |
| 322 | // decoded bits in order to verify that the instruction matches the Opcode. |
| 323 | unsigned getIslands(std::vector<unsigned> &StartBits, |
| 324 | std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals, |
| 325 | insn_t &Insn); |
| 326 | |
| 327 | // Emits code to decode the singleton. Return true if we have matched all the |
| 328 | // well-known bits. |
| 329 | bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc); |
| 330 | |
| 331 | // Emits code to decode the singleton, and then to decode the rest. |
| 332 | void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,Filter &Best); |
| 333 | |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 334 | void emitBinaryParser(raw_ostream &o , unsigned &Indentation, |
| 335 | OperandInfo &OpInfo); |
| 336 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 337 | // Assign a single filter and run with it. |
| 338 | void runSingleFilter(FilterChooser &owner, unsigned startBit, unsigned numBit, |
| 339 | bool mixed); |
| 340 | |
| 341 | // reportRegion is a helper function for filterProcessor to mark a region as |
| 342 | // eligible for use as a filter region. |
| 343 | void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex, |
| 344 | bool AllowMixed); |
| 345 | |
| 346 | // FilterProcessor scans the well-known encoding bits of the instructions and |
| 347 | // builds up a list of candidate filters. It chooses the best filter and |
| 348 | // recursively descends down the decoding tree. |
| 349 | bool filterProcessor(bool AllowMixed, bool Greedy = true); |
| 350 | |
| 351 | // Decides on the best configuration of filter(s) to use in order to decode |
| 352 | // the instructions. A conflict of instructions may occur, in which case we |
| 353 | // dump the conflict set to the standard error. |
| 354 | void doFilter(); |
| 355 | |
| 356 | // Emits code to decode our share of instructions. Returns true if the |
| 357 | // emitted code causes a return, which occurs if we know how to decode |
| 358 | // the instruction at this level or the instruction is not decodeable. |
| 359 | bool emit(raw_ostream &o, unsigned &Indentation); |
| 360 | }; |
| 361 | |
| 362 | /////////////////////////// |
| 363 | // // |
| 364 | // Filter Implmenetation // |
| 365 | // // |
| 366 | /////////////////////////// |
| 367 | |
| 368 | Filter::Filter(const Filter &f) : |
| 369 | Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed), |
| 370 | FilteredInstructions(f.FilteredInstructions), |
| 371 | VariableInstructions(f.VariableInstructions), |
| 372 | FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered), |
| 373 | LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) { |
| 374 | } |
| 375 | |
| 376 | Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, |
| 377 | bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits), |
| 378 | Mixed(mixed) { |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 379 | assert(StartBit + NumBits - 1 < Owner->BitWidth); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 380 | |
| 381 | NumFiltered = 0; |
| 382 | LastOpcFiltered = 0; |
| 383 | NumVariable = 0; |
| 384 | |
| 385 | for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) { |
| 386 | insn_t Insn; |
| 387 | |
| 388 | // Populates the insn given the uid. |
| 389 | Owner->insnWithID(Insn, Owner->Opcodes[i]); |
| 390 | |
| 391 | uint64_t Field; |
| 392 | // Scans the segment for possibly well-specified encoding bits. |
| 393 | bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits); |
| 394 | |
| 395 | if (ok) { |
| 396 | // The encoding bits are well-known. Lets add the uid of the |
| 397 | // instruction into the bucket keyed off the constant field value. |
| 398 | LastOpcFiltered = Owner->Opcodes[i]; |
| 399 | FilteredInstructions[Field].push_back(LastOpcFiltered); |
| 400 | ++NumFiltered; |
| 401 | } else { |
| 402 | // Some of the encoding bit(s) are unspecfied. This contributes to |
| 403 | // one additional member of "Variable" instructions. |
| 404 | VariableInstructions.push_back(Owner->Opcodes[i]); |
| 405 | ++NumVariable; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | assert((FilteredInstructions.size() + VariableInstructions.size() > 0) |
| 410 | && "Filter returns no instruction categories"); |
| 411 | } |
| 412 | |
| 413 | Filter::~Filter() { |
| 414 | std::map<unsigned, FilterChooser*>::iterator filterIterator; |
| 415 | for (filterIterator = FilterChooserMap.begin(); |
| 416 | filterIterator != FilterChooserMap.end(); |
| 417 | filterIterator++) { |
| 418 | delete filterIterator->second; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // Divides the decoding task into sub tasks and delegates them to the |
| 423 | // inferior FilterChooser's. |
| 424 | // |
| 425 | // A special case arises when there's only one entry in the filtered |
| 426 | // instructions. In order to unambiguously decode the singleton, we need to |
| 427 | // match the remaining undecoded encoding bits against the singleton. |
| 428 | void Filter::recurse() { |
| 429 | std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator; |
| 430 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 431 | // Starts by inheriting our parent filter chooser's filter bit values. |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 432 | std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 433 | |
| 434 | unsigned bitIndex; |
| 435 | |
| 436 | if (VariableInstructions.size()) { |
| 437 | // Conservatively marks each segment position as BIT_UNSET. |
| 438 | for (bitIndex = 0; bitIndex < NumBits; bitIndex++) |
| 439 | BitValueArray[StartBit + bitIndex] = BIT_UNSET; |
| 440 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 441 | // Delegates to an inferior filter chooser for further processing on this |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 442 | // group of instructions whose segment values are variable. |
| 443 | FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>( |
| 444 | (unsigned)-1, |
| 445 | new FilterChooser(Owner->AllInstructions, |
| 446 | VariableInstructions, |
| 447 | Owner->Operands, |
| 448 | BitValueArray, |
| 449 | *Owner) |
| 450 | )); |
| 451 | } |
| 452 | |
| 453 | // No need to recurse for a singleton filtered instruction. |
| 454 | // See also Filter::emit(). |
| 455 | if (getNumFiltered() == 1) { |
| 456 | //Owner->SingletonExists(LastOpcFiltered); |
| 457 | assert(FilterChooserMap.size() == 1); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | // Otherwise, create sub choosers. |
| 462 | for (mapIterator = FilteredInstructions.begin(); |
| 463 | mapIterator != FilteredInstructions.end(); |
| 464 | mapIterator++) { |
| 465 | |
| 466 | // Marks all the segment positions with either BIT_TRUE or BIT_FALSE. |
| 467 | for (bitIndex = 0; bitIndex < NumBits; bitIndex++) { |
| 468 | if (mapIterator->first & (1ULL << bitIndex)) |
| 469 | BitValueArray[StartBit + bitIndex] = BIT_TRUE; |
| 470 | else |
| 471 | BitValueArray[StartBit + bitIndex] = BIT_FALSE; |
| 472 | } |
| 473 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 474 | // Delegates to an inferior filter chooser for further processing on this |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 475 | // category of instructions. |
| 476 | FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>( |
| 477 | mapIterator->first, |
| 478 | new FilterChooser(Owner->AllInstructions, |
| 479 | mapIterator->second, |
| 480 | Owner->Operands, |
| 481 | BitValueArray, |
| 482 | *Owner) |
| 483 | )); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // Emit code to decode instructions given a segment or segments of bits. |
| 488 | void Filter::emit(raw_ostream &o, unsigned &Indentation) { |
| 489 | o.indent(Indentation) << "// Check Inst{"; |
| 490 | |
| 491 | if (NumBits > 1) |
| 492 | o << (StartBit + NumBits - 1) << '-'; |
| 493 | |
| 494 | o << StartBit << "} ...\n"; |
| 495 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 496 | o.indent(Indentation) << "switch (fieldFromInstruction" << Owner->BitWidth |
| 497 | << "(insn, " << StartBit << ", " |
| 498 | << NumBits << ")) {\n"; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 499 | |
| 500 | std::map<unsigned, FilterChooser*>::iterator filterIterator; |
| 501 | |
| 502 | bool DefaultCase = false; |
| 503 | for (filterIterator = FilterChooserMap.begin(); |
| 504 | filterIterator != FilterChooserMap.end(); |
| 505 | filterIterator++) { |
| 506 | |
| 507 | // Field value -1 implies a non-empty set of variable instructions. |
| 508 | // See also recurse(). |
| 509 | if (filterIterator->first == (unsigned)-1) { |
| 510 | DefaultCase = true; |
| 511 | |
| 512 | o.indent(Indentation) << "default:\n"; |
| 513 | o.indent(Indentation) << " break; // fallthrough\n"; |
| 514 | |
| 515 | // Closing curly brace for the switch statement. |
| 516 | // This is unconventional because we want the default processing to be |
| 517 | // performed for the fallthrough cases as well, i.e., when the "cases" |
| 518 | // did not prove a decoded instruction. |
| 519 | o.indent(Indentation) << "}\n"; |
| 520 | |
| 521 | } else |
| 522 | o.indent(Indentation) << "case " << filterIterator->first << ":\n"; |
| 523 | |
| 524 | // We arrive at a category of instructions with the same segment value. |
| 525 | // Now delegate to the sub filter chooser for further decodings. |
| 526 | // The case may fallthrough, which happens if the remaining well-known |
| 527 | // encoding bits do not match exactly. |
| 528 | if (!DefaultCase) { ++Indentation; ++Indentation; } |
| 529 | |
| 530 | bool finished = filterIterator->second->emit(o, Indentation); |
| 531 | // For top level default case, there's no need for a break statement. |
| 532 | if (Owner->isTopLevel() && DefaultCase) |
| 533 | break; |
| 534 | if (!finished) |
| 535 | o.indent(Indentation) << "break;\n"; |
| 536 | |
| 537 | if (!DefaultCase) { --Indentation; --Indentation; } |
| 538 | } |
| 539 | |
| 540 | // If there is no default case, we still need to supply a closing brace. |
| 541 | if (!DefaultCase) { |
| 542 | // Closing curly brace for the switch statement. |
| 543 | o.indent(Indentation) << "}\n"; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | // Returns the number of fanout produced by the filter. More fanout implies |
| 548 | // the filter distinguishes more categories of instructions. |
| 549 | unsigned Filter::usefulness() const { |
| 550 | if (VariableInstructions.size()) |
| 551 | return FilteredInstructions.size(); |
| 552 | else |
| 553 | return FilteredInstructions.size() + 1; |
| 554 | } |
| 555 | |
| 556 | ////////////////////////////////// |
| 557 | // // |
| 558 | // Filterchooser Implementation // |
| 559 | // // |
| 560 | ////////////////////////////////// |
| 561 | |
| 562 | // Emit the top level typedef and decodeInstruction() function. |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 563 | void FilterChooser::emitTop(raw_ostream &o, unsigned Indentation, |
| 564 | std::string Namespace) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 565 | o.indent(Indentation) << |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 566 | "static bool decode" << Namespace << "Instruction" << BitWidth |
| 567 | << "(MCInst &MI, uint" << BitWidth << "_t insn, uint64_t Address, " |
| 568 | << "const void *Decoder) {\n"; |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 569 | o.indent(Indentation) << " unsigned tmp = 0;\n(void)tmp;\n"; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 570 | |
| 571 | ++Indentation; ++Indentation; |
| 572 | // Emits code to decode the instructions. |
| 573 | emit(o, Indentation); |
| 574 | |
| 575 | o << '\n'; |
| 576 | o.indent(Indentation) << "return false;\n"; |
| 577 | --Indentation; --Indentation; |
| 578 | |
| 579 | o.indent(Indentation) << "}\n"; |
| 580 | |
| 581 | o << '\n'; |
| 582 | } |
| 583 | |
| 584 | // Populates the field of the insn given the start position and the number of |
| 585 | // consecutive bits to scan for. |
| 586 | // |
| 587 | // Returns false if and on the first uninitialized bit value encountered. |
| 588 | // Returns true, otherwise. |
| 589 | bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn, |
| 590 | unsigned StartBit, unsigned NumBits) const { |
| 591 | Field = 0; |
| 592 | |
| 593 | for (unsigned i = 0; i < NumBits; ++i) { |
| 594 | if (Insn[StartBit + i] == BIT_UNSET) |
| 595 | return false; |
| 596 | |
| 597 | if (Insn[StartBit + i] == BIT_TRUE) |
| 598 | Field = Field | (1ULL << i); |
| 599 | } |
| 600 | |
| 601 | return true; |
| 602 | } |
| 603 | |
| 604 | /// dumpFilterArray - dumpFilterArray prints out debugging info for the given |
| 605 | /// filter array as a series of chars. |
| 606 | void FilterChooser::dumpFilterArray(raw_ostream &o, |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 607 | std::vector<bit_value_t> &filter) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 608 | unsigned bitIndex; |
| 609 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 610 | for (bitIndex = BitWidth; bitIndex > 0; bitIndex--) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 611 | switch (filter[bitIndex - 1]) { |
| 612 | case BIT_UNFILTERED: |
| 613 | o << "."; |
| 614 | break; |
| 615 | case BIT_UNSET: |
| 616 | o << "_"; |
| 617 | break; |
| 618 | case BIT_TRUE: |
| 619 | o << "1"; |
| 620 | break; |
| 621 | case BIT_FALSE: |
| 622 | o << "0"; |
| 623 | break; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /// dumpStack - dumpStack traverses the filter chooser chain and calls |
| 629 | /// dumpFilterArray on each filter chooser up to the top level one. |
| 630 | void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) { |
| 631 | FilterChooser *current = this; |
| 632 | |
| 633 | while (current) { |
| 634 | o << prefix; |
| 635 | dumpFilterArray(o, current->FilterBitValues); |
| 636 | o << '\n'; |
| 637 | current = current->Parent; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | // Called from Filter::recurse() when singleton exists. For debug purpose. |
| 642 | void FilterChooser::SingletonExists(unsigned Opc) { |
| 643 | insn_t Insn0; |
| 644 | insnWithID(Insn0, Opc); |
| 645 | |
| 646 | errs() << "Singleton exists: " << nameWithID(Opc) |
| 647 | << " with its decoding dominating "; |
| 648 | for (unsigned i = 0; i < Opcodes.size(); ++i) { |
| 649 | if (Opcodes[i] == Opc) continue; |
| 650 | errs() << nameWithID(Opcodes[i]) << ' '; |
| 651 | } |
| 652 | errs() << '\n'; |
| 653 | |
| 654 | dumpStack(errs(), "\t\t"); |
| 655 | for (unsigned i = 0; i < Opcodes.size(); i++) { |
| 656 | const std::string &Name = nameWithID(Opcodes[i]); |
| 657 | |
| 658 | errs() << '\t' << Name << " "; |
| 659 | dumpBits(errs(), |
| 660 | getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst")); |
| 661 | errs() << '\n'; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // Calculates the island(s) needed to decode the instruction. |
| 666 | // This returns a list of undecoded bits of an instructions, for example, |
| 667 | // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be |
| 668 | // decoded bits in order to verify that the instruction matches the Opcode. |
| 669 | unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits, |
| 670 | std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals, |
| 671 | insn_t &Insn) { |
| 672 | unsigned Num, BitNo; |
| 673 | Num = BitNo = 0; |
| 674 | |
| 675 | uint64_t FieldVal = 0; |
| 676 | |
| 677 | // 0: Init |
| 678 | // 1: Water (the bit value does not affect decoding) |
| 679 | // 2: Island (well-known bit value needed for decoding) |
| 680 | int State = 0; |
| 681 | int Val = -1; |
| 682 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 683 | for (unsigned i = 0; i < BitWidth; ++i) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 684 | Val = Value(Insn[i]); |
| 685 | bool Filtered = PositionFiltered(i); |
| 686 | switch (State) { |
| 687 | default: |
| 688 | assert(0 && "Unreachable code!"); |
| 689 | break; |
| 690 | case 0: |
| 691 | case 1: |
| 692 | if (Filtered || Val == -1) |
| 693 | State = 1; // Still in Water |
| 694 | else { |
| 695 | State = 2; // Into the Island |
| 696 | BitNo = 0; |
| 697 | StartBits.push_back(i); |
| 698 | FieldVal = Val; |
| 699 | } |
| 700 | break; |
| 701 | case 2: |
| 702 | if (Filtered || Val == -1) { |
| 703 | State = 1; // Into the Water |
| 704 | EndBits.push_back(i - 1); |
| 705 | FieldVals.push_back(FieldVal); |
| 706 | ++Num; |
| 707 | } else { |
| 708 | State = 2; // Still in Island |
| 709 | ++BitNo; |
| 710 | FieldVal = FieldVal | Val << BitNo; |
| 711 | } |
| 712 | break; |
| 713 | } |
| 714 | } |
| 715 | // If we are still in Island after the loop, do some housekeeping. |
| 716 | if (State == 2) { |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 717 | EndBits.push_back(BitWidth - 1); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 718 | FieldVals.push_back(FieldVal); |
| 719 | ++Num; |
| 720 | } |
| 721 | |
| 722 | assert(StartBits.size() == Num && EndBits.size() == Num && |
| 723 | FieldVals.size() == Num); |
| 724 | return Num; |
| 725 | } |
| 726 | |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 727 | void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation, |
| 728 | OperandInfo &OpInfo) { |
| 729 | std::string &Decoder = OpInfo.Decoder; |
| 730 | |
| 731 | if (OpInfo.numFields() == 1) { |
| 732 | OperandInfo::iterator OI = OpInfo.begin(); |
| 733 | o.indent(Indentation) << " tmp = fieldFromInstruction" << BitWidth |
| 734 | << "(insn, " << OI->Base << ", " << OI->Width |
| 735 | << ");\n"; |
| 736 | } else { |
| 737 | o.indent(Indentation) << " tmp = 0;\n"; |
| 738 | for (OperandInfo::iterator OI = OpInfo.begin(), OE = OpInfo.end(); |
| 739 | OI != OE; ++OI) { |
| 740 | o.indent(Indentation) << " tmp |= (fieldFromInstruction" << BitWidth |
| 741 | << "(insn, " << OI->Base << ", " << OI->Width |
| 742 | << ") << " << OI->Offset << ");\n"; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (Decoder != "") |
| 747 | o.indent(Indentation) << " " << Decoder |
| 748 | << "(MI, tmp, Address, Decoder);\n"; |
| 749 | else |
| 750 | o.indent(Indentation) << " MI.addOperand(MCOperand::CreateImm(tmp));\n"; |
| 751 | |
| 752 | } |
| 753 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 754 | // Emits code to decode the singleton. Return true if we have matched all the |
| 755 | // well-known bits. |
| 756 | bool FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation, |
| 757 | unsigned Opc) { |
| 758 | std::vector<unsigned> StartBits; |
| 759 | std::vector<unsigned> EndBits; |
| 760 | std::vector<uint64_t> FieldVals; |
| 761 | insn_t Insn; |
| 762 | insnWithID(Insn, Opc); |
| 763 | |
| 764 | // Look for islands of undecoded bits of the singleton. |
| 765 | getIslands(StartBits, EndBits, FieldVals, Insn); |
| 766 | |
| 767 | unsigned Size = StartBits.size(); |
| 768 | unsigned I, NumBits; |
| 769 | |
| 770 | // If we have matched all the well-known bits, just issue a return. |
| 771 | if (Size == 0) { |
| 772 | o.indent(Indentation) << "{\n"; |
| 773 | o.indent(Indentation) << " MI.setOpcode(" << Opc << ");\n"; |
| 774 | std::vector<OperandInfo>& InsnOperands = Operands[Opc]; |
| 775 | for (std::vector<OperandInfo>::iterator |
| 776 | I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) { |
| 777 | // If a custom instruction decoder was specified, use that. |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 778 | if (I->numFields() == 0 && I->Decoder.size()) { |
Owen Anderson | 5755715 | 2011-04-18 18:42:26 +0000 | [diff] [blame] | 779 | o.indent(Indentation) << " " << I->Decoder |
| 780 | << "(MI, insn, Address, Decoder);\n"; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 784 | emitBinaryParser(o, Indentation, *I); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | o.indent(Indentation) << " return true; // " << nameWithID(Opc) |
| 788 | << '\n'; |
| 789 | o.indent(Indentation) << "}\n"; |
| 790 | return true; |
| 791 | } |
| 792 | |
| 793 | // Otherwise, there are more decodings to be done! |
| 794 | |
| 795 | // Emit code to match the island(s) for the singleton. |
| 796 | o.indent(Indentation) << "// Check "; |
| 797 | |
| 798 | for (I = Size; I != 0; --I) { |
| 799 | o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} "; |
| 800 | if (I > 1) |
| 801 | o << "&& "; |
| 802 | else |
| 803 | o << "for singleton decoding...\n"; |
| 804 | } |
| 805 | |
| 806 | o.indent(Indentation) << "if ("; |
| 807 | |
| 808 | for (I = Size; I != 0; --I) { |
| 809 | NumBits = EndBits[I-1] - StartBits[I-1] + 1; |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 810 | o << "fieldFromInstruction" << BitWidth << "(insn, " |
| 811 | << StartBits[I-1] << ", " << NumBits |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 812 | << ") == " << FieldVals[I-1]; |
| 813 | if (I > 1) |
| 814 | o << " && "; |
| 815 | else |
| 816 | o << ") {\n"; |
| 817 | } |
| 818 | o.indent(Indentation) << " MI.setOpcode(" << Opc << ");\n"; |
| 819 | std::vector<OperandInfo>& InsnOperands = Operands[Opc]; |
| 820 | for (std::vector<OperandInfo>::iterator |
| 821 | I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) { |
| 822 | // If a custom instruction decoder was specified, use that. |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 823 | if (I->numFields() == 0 && I->Decoder.size()) { |
Owen Anderson | 5755715 | 2011-04-18 18:42:26 +0000 | [diff] [blame] | 824 | o.indent(Indentation) << " " << I->Decoder |
| 825 | << "(MI, insn, Address, Decoder);\n"; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 826 | break; |
| 827 | } |
| 828 | |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 829 | emitBinaryParser(o, Indentation, *I); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 830 | } |
| 831 | o.indent(Indentation) << " return true; // " << nameWithID(Opc) |
| 832 | << '\n'; |
| 833 | o.indent(Indentation) << "}\n"; |
| 834 | |
| 835 | return false; |
| 836 | } |
| 837 | |
| 838 | // Emits code to decode the singleton, and then to decode the rest. |
| 839 | void FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation, |
| 840 | Filter &Best) { |
| 841 | |
| 842 | unsigned Opc = Best.getSingletonOpc(); |
| 843 | |
| 844 | emitSingletonDecoder(o, Indentation, Opc); |
| 845 | |
| 846 | // Emit code for the rest. |
| 847 | o.indent(Indentation) << "else\n"; |
| 848 | |
| 849 | Indentation += 2; |
| 850 | Best.getVariableFC().emit(o, Indentation); |
| 851 | Indentation -= 2; |
| 852 | } |
| 853 | |
| 854 | // Assign a single filter and run with it. Top level API client can initialize |
| 855 | // with a single filter to start the filtering process. |
| 856 | void FilterChooser::runSingleFilter(FilterChooser &owner, unsigned startBit, |
| 857 | unsigned numBit, bool mixed) { |
| 858 | Filters.clear(); |
| 859 | Filter F(*this, startBit, numBit, true); |
| 860 | Filters.push_back(F); |
| 861 | BestIndex = 0; // Sole Filter instance to choose from. |
| 862 | bestFilter().recurse(); |
| 863 | } |
| 864 | |
| 865 | // reportRegion is a helper function for filterProcessor to mark a region as |
| 866 | // eligible for use as a filter region. |
| 867 | void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit, |
| 868 | unsigned BitIndex, bool AllowMixed) { |
| 869 | if (RA == ATTR_MIXED && AllowMixed) |
| 870 | Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true)); |
| 871 | else if (RA == ATTR_ALL_SET && !AllowMixed) |
| 872 | Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false)); |
| 873 | } |
| 874 | |
| 875 | // FilterProcessor scans the well-known encoding bits of the instructions and |
| 876 | // builds up a list of candidate filters. It chooses the best filter and |
| 877 | // recursively descends down the decoding tree. |
| 878 | bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) { |
| 879 | Filters.clear(); |
| 880 | BestIndex = -1; |
| 881 | unsigned numInstructions = Opcodes.size(); |
| 882 | |
| 883 | assert(numInstructions && "Filter created with no instructions"); |
| 884 | |
| 885 | // No further filtering is necessary. |
| 886 | if (numInstructions == 1) |
| 887 | return true; |
| 888 | |
| 889 | // Heuristics. See also doFilter()'s "Heuristics" comment when num of |
| 890 | // instructions is 3. |
| 891 | if (AllowMixed && !Greedy) { |
| 892 | assert(numInstructions == 3); |
| 893 | |
| 894 | for (unsigned i = 0; i < Opcodes.size(); ++i) { |
| 895 | std::vector<unsigned> StartBits; |
| 896 | std::vector<unsigned> EndBits; |
| 897 | std::vector<uint64_t> FieldVals; |
| 898 | insn_t Insn; |
| 899 | |
| 900 | insnWithID(Insn, Opcodes[i]); |
| 901 | |
| 902 | // Look for islands of undecoded bits of any instruction. |
| 903 | if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) { |
| 904 | // Found an instruction with island(s). Now just assign a filter. |
| 905 | runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1, |
| 906 | true); |
| 907 | return true; |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | unsigned BitIndex, InsnIndex; |
| 913 | |
| 914 | // We maintain BIT_WIDTH copies of the bitAttrs automaton. |
| 915 | // The automaton consumes the corresponding bit from each |
| 916 | // instruction. |
| 917 | // |
| 918 | // Input symbols: 0, 1, and _ (unset). |
| 919 | // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED. |
| 920 | // Initial state: NONE. |
| 921 | // |
| 922 | // (NONE) ------- [01] -> (ALL_SET) |
| 923 | // (NONE) ------- _ ----> (ALL_UNSET) |
| 924 | // (ALL_SET) ---- [01] -> (ALL_SET) |
| 925 | // (ALL_SET) ---- _ ----> (MIXED) |
| 926 | // (ALL_UNSET) -- [01] -> (MIXED) |
| 927 | // (ALL_UNSET) -- _ ----> (ALL_UNSET) |
| 928 | // (MIXED) ------ . ----> (MIXED) |
| 929 | // (FILTERED)---- . ----> (FILTERED) |
| 930 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 931 | std::vector<bitAttr_t> bitAttrs; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 932 | |
| 933 | // FILTERED bit positions provide no entropy and are not worthy of pursuing. |
| 934 | // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position. |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 935 | for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 936 | if (FilterBitValues[BitIndex] == BIT_TRUE || |
| 937 | FilterBitValues[BitIndex] == BIT_FALSE) |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 938 | bitAttrs.push_back(ATTR_FILTERED); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 939 | else |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 940 | bitAttrs.push_back(ATTR_NONE); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 941 | |
| 942 | for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) { |
| 943 | insn_t insn; |
| 944 | |
| 945 | insnWithID(insn, Opcodes[InsnIndex]); |
| 946 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 947 | for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 948 | switch (bitAttrs[BitIndex]) { |
| 949 | case ATTR_NONE: |
| 950 | if (insn[BitIndex] == BIT_UNSET) |
| 951 | bitAttrs[BitIndex] = ATTR_ALL_UNSET; |
| 952 | else |
| 953 | bitAttrs[BitIndex] = ATTR_ALL_SET; |
| 954 | break; |
| 955 | case ATTR_ALL_SET: |
| 956 | if (insn[BitIndex] == BIT_UNSET) |
| 957 | bitAttrs[BitIndex] = ATTR_MIXED; |
| 958 | break; |
| 959 | case ATTR_ALL_UNSET: |
| 960 | if (insn[BitIndex] != BIT_UNSET) |
| 961 | bitAttrs[BitIndex] = ATTR_MIXED; |
| 962 | break; |
| 963 | case ATTR_MIXED: |
| 964 | case ATTR_FILTERED: |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | // The regionAttr automaton consumes the bitAttrs automatons' state, |
| 971 | // lowest-to-highest. |
| 972 | // |
| 973 | // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed) |
| 974 | // States: NONE, ALL_SET, MIXED |
| 975 | // Initial state: NONE |
| 976 | // |
| 977 | // (NONE) ----- F --> (NONE) |
| 978 | // (NONE) ----- S --> (ALL_SET) ; and set region start |
| 979 | // (NONE) ----- U --> (NONE) |
| 980 | // (NONE) ----- M --> (MIXED) ; and set region start |
| 981 | // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region |
| 982 | // (ALL_SET) -- S --> (ALL_SET) |
| 983 | // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region |
| 984 | // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region |
| 985 | // (MIXED) ---- F --> (NONE) ; and report a MIXED region |
| 986 | // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region |
| 987 | // (MIXED) ---- U --> (NONE) ; and report a MIXED region |
| 988 | // (MIXED) ---- M --> (MIXED) |
| 989 | |
| 990 | bitAttr_t RA = ATTR_NONE; |
| 991 | unsigned StartBit = 0; |
| 992 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 993 | for (BitIndex = 0; BitIndex < BitWidth; BitIndex++) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 994 | bitAttr_t bitAttr = bitAttrs[BitIndex]; |
| 995 | |
| 996 | assert(bitAttr != ATTR_NONE && "Bit without attributes"); |
| 997 | |
| 998 | switch (RA) { |
| 999 | case ATTR_NONE: |
| 1000 | switch (bitAttr) { |
| 1001 | case ATTR_FILTERED: |
| 1002 | break; |
| 1003 | case ATTR_ALL_SET: |
| 1004 | StartBit = BitIndex; |
| 1005 | RA = ATTR_ALL_SET; |
| 1006 | break; |
| 1007 | case ATTR_ALL_UNSET: |
| 1008 | break; |
| 1009 | case ATTR_MIXED: |
| 1010 | StartBit = BitIndex; |
| 1011 | RA = ATTR_MIXED; |
| 1012 | break; |
| 1013 | default: |
| 1014 | assert(0 && "Unexpected bitAttr!"); |
| 1015 | } |
| 1016 | break; |
| 1017 | case ATTR_ALL_SET: |
| 1018 | switch (bitAttr) { |
| 1019 | case ATTR_FILTERED: |
| 1020 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1021 | RA = ATTR_NONE; |
| 1022 | break; |
| 1023 | case ATTR_ALL_SET: |
| 1024 | break; |
| 1025 | case ATTR_ALL_UNSET: |
| 1026 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1027 | RA = ATTR_NONE; |
| 1028 | break; |
| 1029 | case ATTR_MIXED: |
| 1030 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1031 | StartBit = BitIndex; |
| 1032 | RA = ATTR_MIXED; |
| 1033 | break; |
| 1034 | default: |
| 1035 | assert(0 && "Unexpected bitAttr!"); |
| 1036 | } |
| 1037 | break; |
| 1038 | case ATTR_MIXED: |
| 1039 | switch (bitAttr) { |
| 1040 | case ATTR_FILTERED: |
| 1041 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1042 | StartBit = BitIndex; |
| 1043 | RA = ATTR_NONE; |
| 1044 | break; |
| 1045 | case ATTR_ALL_SET: |
| 1046 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1047 | StartBit = BitIndex; |
| 1048 | RA = ATTR_ALL_SET; |
| 1049 | break; |
| 1050 | case ATTR_ALL_UNSET: |
| 1051 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1052 | RA = ATTR_NONE; |
| 1053 | break; |
| 1054 | case ATTR_MIXED: |
| 1055 | break; |
| 1056 | default: |
| 1057 | assert(0 && "Unexpected bitAttr!"); |
| 1058 | } |
| 1059 | break; |
| 1060 | case ATTR_ALL_UNSET: |
| 1061 | assert(0 && "regionAttr state machine has no ATTR_UNSET state"); |
| 1062 | case ATTR_FILTERED: |
| 1063 | assert(0 && "regionAttr state machine has no ATTR_FILTERED state"); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | // At the end, if we're still in ALL_SET or MIXED states, report a region |
| 1068 | switch (RA) { |
| 1069 | case ATTR_NONE: |
| 1070 | break; |
| 1071 | case ATTR_FILTERED: |
| 1072 | break; |
| 1073 | case ATTR_ALL_SET: |
| 1074 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1075 | break; |
| 1076 | case ATTR_ALL_UNSET: |
| 1077 | break; |
| 1078 | case ATTR_MIXED: |
| 1079 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1080 | break; |
| 1081 | } |
| 1082 | |
| 1083 | // We have finished with the filter processings. Now it's time to choose |
| 1084 | // the best performing filter. |
| 1085 | BestIndex = 0; |
| 1086 | bool AllUseless = true; |
| 1087 | unsigned BestScore = 0; |
| 1088 | |
| 1089 | for (unsigned i = 0, e = Filters.size(); i != e; ++i) { |
| 1090 | unsigned Usefulness = Filters[i].usefulness(); |
| 1091 | |
| 1092 | if (Usefulness) |
| 1093 | AllUseless = false; |
| 1094 | |
| 1095 | if (Usefulness > BestScore) { |
| 1096 | BestIndex = i; |
| 1097 | BestScore = Usefulness; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | if (!AllUseless) |
| 1102 | bestFilter().recurse(); |
| 1103 | |
| 1104 | return !AllUseless; |
| 1105 | } // end of FilterChooser::filterProcessor(bool) |
| 1106 | |
| 1107 | // Decides on the best configuration of filter(s) to use in order to decode |
| 1108 | // the instructions. A conflict of instructions may occur, in which case we |
| 1109 | // dump the conflict set to the standard error. |
| 1110 | void FilterChooser::doFilter() { |
| 1111 | unsigned Num = Opcodes.size(); |
| 1112 | assert(Num && "FilterChooser created with no instructions"); |
| 1113 | |
| 1114 | // Try regions of consecutive known bit values first. |
| 1115 | if (filterProcessor(false)) |
| 1116 | return; |
| 1117 | |
| 1118 | // Then regions of mixed bits (both known and unitialized bit values allowed). |
| 1119 | if (filterProcessor(true)) |
| 1120 | return; |
| 1121 | |
| 1122 | // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where |
| 1123 | // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a |
| 1124 | // well-known encoding pattern. In such case, we backtrack and scan for the |
| 1125 | // the very first consecutive ATTR_ALL_SET region and assign a filter to it. |
| 1126 | if (Num == 3 && filterProcessor(true, false)) |
| 1127 | return; |
| 1128 | |
| 1129 | // If we come to here, the instruction decoding has failed. |
| 1130 | // Set the BestIndex to -1 to indicate so. |
| 1131 | BestIndex = -1; |
| 1132 | } |
| 1133 | |
| 1134 | // Emits code to decode our share of instructions. Returns true if the |
| 1135 | // emitted code causes a return, which occurs if we know how to decode |
| 1136 | // the instruction at this level or the instruction is not decodeable. |
| 1137 | bool FilterChooser::emit(raw_ostream &o, unsigned &Indentation) { |
| 1138 | if (Opcodes.size() == 1) |
| 1139 | // There is only one instruction in the set, which is great! |
| 1140 | // Call emitSingletonDecoder() to see whether there are any remaining |
| 1141 | // encodings bits. |
| 1142 | return emitSingletonDecoder(o, Indentation, Opcodes[0]); |
| 1143 | |
| 1144 | // Choose the best filter to do the decodings! |
| 1145 | if (BestIndex != -1) { |
| 1146 | Filter &Best = bestFilter(); |
| 1147 | if (Best.getNumFiltered() == 1) |
| 1148 | emitSingletonDecoder(o, Indentation, Best); |
| 1149 | else |
| 1150 | bestFilter().emit(o, Indentation); |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | // We don't know how to decode these instructions! Return 0 and dump the |
| 1155 | // conflict set! |
| 1156 | o.indent(Indentation) << "return 0;" << " // Conflict set: "; |
| 1157 | for (int i = 0, N = Opcodes.size(); i < N; ++i) { |
| 1158 | o << nameWithID(Opcodes[i]); |
| 1159 | if (i < (N - 1)) |
| 1160 | o << ", "; |
| 1161 | else |
| 1162 | o << '\n'; |
| 1163 | } |
| 1164 | |
| 1165 | // Print out useful conflict information for postmortem analysis. |
| 1166 | errs() << "Decoding Conflict:\n"; |
| 1167 | |
| 1168 | dumpStack(errs(), "\t\t"); |
| 1169 | |
| 1170 | for (unsigned i = 0; i < Opcodes.size(); i++) { |
| 1171 | const std::string &Name = nameWithID(Opcodes[i]); |
| 1172 | |
| 1173 | errs() << '\t' << Name << " "; |
| 1174 | dumpBits(errs(), |
| 1175 | getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst")); |
| 1176 | errs() << '\n'; |
| 1177 | } |
| 1178 | |
| 1179 | return true; |
| 1180 | } |
| 1181 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1182 | static bool populateInstruction(const CodeGenInstruction &CGI, |
| 1183 | unsigned Opc, |
| 1184 | std::map<unsigned, std::vector<OperandInfo> >& Operands){ |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1185 | const Record &Def = *CGI.TheDef; |
| 1186 | // If all the bit positions are not specified; do not decode this instruction. |
| 1187 | // We are bound to fail! For proper disassembly, the well-known encoding bits |
| 1188 | // of the instruction must be fully specified. |
| 1189 | // |
| 1190 | // This also removes pseudo instructions from considerations of disassembly, |
| 1191 | // which is a better design and less fragile than the name matchings. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1192 | // Ignore "asm parser only" instructions. |
Owen Anderson | 4dd27eb | 2011-03-14 20:58:49 +0000 | [diff] [blame] | 1193 | if (Def.getValueAsBit("isAsmParserOnly") || |
| 1194 | Def.getValueAsBit("isCodeGenOnly")) |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1195 | return false; |
| 1196 | |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1197 | BitsInit &Bits = getBitsField(Def, "Inst"); |
Jim Grosbach | 806fcc0 | 2011-07-06 21:33:38 +0000 | [diff] [blame] | 1198 | if (Bits.allInComplete()) return false; |
| 1199 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1200 | std::vector<OperandInfo> InsnOperands; |
| 1201 | |
| 1202 | // If the instruction has specified a custom decoding hook, use that instead |
| 1203 | // of trying to auto-generate the decoder. |
| 1204 | std::string InstDecoder = Def.getValueAsString("DecoderMethod"); |
| 1205 | if (InstDecoder != "") { |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1206 | InsnOperands.push_back(OperandInfo(InstDecoder)); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1207 | Operands[Opc] = InsnOperands; |
| 1208 | return true; |
| 1209 | } |
| 1210 | |
| 1211 | // Generate a description of the operand of the instruction that we know |
| 1212 | // how to decode automatically. |
| 1213 | // FIXME: We'll need to have a way to manually override this as needed. |
| 1214 | |
| 1215 | // Gather the outputs/inputs of the instruction, so we can find their |
| 1216 | // positions in the encoding. This assumes for now that they appear in the |
| 1217 | // MCInst in the order that they're listed. |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1218 | std::vector<std::pair<Init*, std::string> > InOutOperands; |
| 1219 | DagInit *Out = Def.getValueAsDag("OutOperandList"); |
| 1220 | DagInit *In = Def.getValueAsDag("InOperandList"); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1221 | for (unsigned i = 0; i < Out->getNumArgs(); ++i) |
| 1222 | InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i))); |
| 1223 | for (unsigned i = 0; i < In->getNumArgs(); ++i) |
| 1224 | InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i))); |
| 1225 | |
Owen Anderson | 00ef6e3 | 2011-07-28 23:56:20 +0000 | [diff] [blame] | 1226 | // Search for tied operands, so that we can correctly instantiate |
| 1227 | // operands that are not explicitly represented in the encoding. |
| 1228 | std::map<Init*, std::string> TiedNames; |
| 1229 | for (unsigned i = 0; i < CGI.Operands.size(); ++i) { |
| 1230 | int tiedTo = CGI.Operands[i].getTiedRegister(); |
Owen Anderson | d217da1 | 2011-07-29 17:32:03 +0000 | [diff] [blame] | 1231 | if (tiedTo != -1) |
Owen Anderson | 00ef6e3 | 2011-07-28 23:56:20 +0000 | [diff] [blame] | 1232 | TiedNames[InOutOperands[i].first] = InOutOperands[tiedTo].second; |
Owen Anderson | 00ef6e3 | 2011-07-28 23:56:20 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1235 | // For each operand, see if we can figure out where it is encoded. |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1236 | for (std::vector<std::pair<Init*, std::string> >::iterator |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1237 | NI = InOutOperands.begin(), NE = InOutOperands.end(); NI != NE; ++NI) { |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1238 | std::string Decoder = ""; |
| 1239 | |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1240 | // At this point, we can locate the field, but we need to know how to |
| 1241 | // interpret it. As a first step, require the target to provide callbacks |
| 1242 | // for decoding register classes. |
| 1243 | // FIXME: This need to be extended to handle instructions with custom |
| 1244 | // decoder methods, and operands with (simple) MIOperandInfo's. |
| 1245 | TypedInit *TI = dynamic_cast<TypedInit*>(NI->first); |
| 1246 | RecordRecTy *Type = dynamic_cast<RecordRecTy*>(TI->getType()); |
| 1247 | Record *TypeRecord = Type->getRecord(); |
| 1248 | bool isReg = false; |
| 1249 | if (TypeRecord->isSubClassOf("RegisterOperand")) |
| 1250 | TypeRecord = TypeRecord->getValueAsDef("RegClass"); |
| 1251 | if (TypeRecord->isSubClassOf("RegisterClass")) { |
| 1252 | Decoder = "Decode" + TypeRecord->getName() + "RegisterClass"; |
| 1253 | isReg = true; |
| 1254 | } |
| 1255 | |
| 1256 | RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod"); |
| 1257 | StringInit *String = DecoderString ? |
| 1258 | dynamic_cast<StringInit*>(DecoderString->getValue()) : 0; |
| 1259 | if (!isReg && String && String->getValue() != "") |
| 1260 | Decoder = String->getValue(); |
| 1261 | |
| 1262 | OperandInfo OpInfo(Decoder); |
| 1263 | unsigned Base = ~0U; |
| 1264 | unsigned Width = 0; |
| 1265 | unsigned Offset = 0; |
| 1266 | |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1267 | for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) { |
Owen Anderson | d217da1 | 2011-07-29 17:32:03 +0000 | [diff] [blame] | 1268 | std::string name = NI->second; |
| 1269 | std::string altname = TiedNames[NI->first]; |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1270 | VarBitInit *BI = dynamic_cast<VarBitInit*>(Bits.getBit(bi)); |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1271 | if (!BI) { |
| 1272 | if (Base != ~0U) { |
| 1273 | OpInfo.addField(Base, Width, Offset); |
| 1274 | Base = ~0U; |
| 1275 | Width = 0; |
| 1276 | Offset = 0; |
| 1277 | } |
| 1278 | continue; |
| 1279 | } |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1280 | |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1281 | VarInit *Var = dynamic_cast<VarInit*>(BI->getVariable()); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1282 | assert(Var); |
Owen Anderson | 00ef6e3 | 2011-07-28 23:56:20 +0000 | [diff] [blame] | 1283 | if (Var->getName() != NI->second && |
| 1284 | Var->getName() != TiedNames[NI->first]) { |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1285 | if (Base != ~0U) { |
| 1286 | OpInfo.addField(Base, Width, Offset); |
| 1287 | Base = ~0U; |
| 1288 | Width = 0; |
| 1289 | Offset = 0; |
| 1290 | } |
| 1291 | continue; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1294 | if (Base == ~0U) { |
| 1295 | Base = bi; |
| 1296 | Width = 1; |
| 1297 | Offset = BI->getBitNum(); |
| 1298 | } else { |
| 1299 | ++Width; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1300 | } |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Owen Anderson | d1e38df | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1303 | if (Base != ~0U) |
| 1304 | OpInfo.addField(Base, Width, Offset); |
| 1305 | |
| 1306 | if (OpInfo.numFields() > 0) |
| 1307 | InsnOperands.push_back(OpInfo); |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | Operands[Opc] = InsnOperands; |
| 1311 | |
| 1312 | |
| 1313 | #if 0 |
| 1314 | DEBUG({ |
| 1315 | // Dumps the instruction encoding bits. |
| 1316 | dumpBits(errs(), Bits); |
| 1317 | |
| 1318 | errs() << '\n'; |
| 1319 | |
| 1320 | // Dumps the list of operand info. |
| 1321 | for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) { |
| 1322 | const CGIOperandList::OperandInfo &Info = CGI.Operands[i]; |
| 1323 | const std::string &OperandName = Info.Name; |
| 1324 | const Record &OperandDef = *Info.Rec; |
| 1325 | |
| 1326 | errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n"; |
| 1327 | } |
| 1328 | }); |
| 1329 | #endif |
| 1330 | |
| 1331 | return true; |
| 1332 | } |
| 1333 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1334 | static void emitHelper(llvm::raw_ostream &o, unsigned BitWidth) { |
| 1335 | unsigned Indentation = 0; |
| 1336 | std::string WidthStr = "uint" + utostr(BitWidth) + "_t"; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1337 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1338 | o << '\n'; |
| 1339 | |
| 1340 | o.indent(Indentation) << "static " << WidthStr << |
| 1341 | " fieldFromInstruction" << BitWidth << |
| 1342 | "(" << WidthStr <<" insn, unsigned startBit, unsigned numBits)\n"; |
| 1343 | |
| 1344 | o.indent(Indentation) << "{\n"; |
| 1345 | |
| 1346 | ++Indentation; ++Indentation; |
| 1347 | o.indent(Indentation) << "assert(startBit + numBits <= " << BitWidth |
| 1348 | << " && \"Instruction field out of bounds!\");\n"; |
| 1349 | o << '\n'; |
| 1350 | o.indent(Indentation) << WidthStr << " fieldMask;\n"; |
| 1351 | o << '\n'; |
| 1352 | o.indent(Indentation) << "if (numBits == " << BitWidth << ")\n"; |
| 1353 | |
| 1354 | ++Indentation; ++Indentation; |
| 1355 | o.indent(Indentation) << "fieldMask = (" << WidthStr << ")-1;\n"; |
| 1356 | --Indentation; --Indentation; |
| 1357 | |
| 1358 | o.indent(Indentation) << "else\n"; |
| 1359 | |
| 1360 | ++Indentation; ++Indentation; |
| 1361 | o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n"; |
| 1362 | --Indentation; --Indentation; |
| 1363 | |
| 1364 | o << '\n'; |
| 1365 | o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n"; |
| 1366 | --Indentation; --Indentation; |
| 1367 | |
| 1368 | o.indent(Indentation) << "}\n"; |
| 1369 | |
| 1370 | o << '\n'; |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | // Emits disassembler code for instruction decoding. |
| 1374 | void FixedLenDecoderEmitter::run(raw_ostream &o) |
| 1375 | { |
| 1376 | o << "#include \"llvm/MC/MCInst.h\"\n"; |
| 1377 | o << "#include \"llvm/Support/DataTypes.h\"\n"; |
| 1378 | o << "#include <assert.h>\n"; |
| 1379 | o << '\n'; |
| 1380 | o << "namespace llvm {\n\n"; |
| 1381 | |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1382 | // Parameterize the decoders based on namespace and instruction width. |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1383 | NumberedInstructions = Target.getInstructionsByEnumValue(); |
Owen Anderson | f1a0090 | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1384 | std::map<std::pair<std::string, unsigned>, |
| 1385 | std::vector<unsigned> > OpcMap; |
| 1386 | std::map<unsigned, std::vector<OperandInfo> > Operands; |
| 1387 | |
| 1388 | for (unsigned i = 0; i < NumberedInstructions.size(); ++i) { |
| 1389 | const CodeGenInstruction *Inst = NumberedInstructions[i]; |
| 1390 | Record *Def = Inst->TheDef; |
| 1391 | unsigned Size = Def->getValueAsInt("Size"); |
| 1392 | if (Def->getValueAsString("Namespace") == "TargetOpcode" || |
| 1393 | Def->getValueAsBit("isPseudo") || |
| 1394 | Def->getValueAsBit("isAsmParserOnly") || |
| 1395 | Def->getValueAsBit("isCodeGenOnly")) |
| 1396 | continue; |
| 1397 | |
| 1398 | std::string DecoderNamespace = Def->getValueAsString("DecoderNamespace"); |
| 1399 | |
| 1400 | if (Size) { |
| 1401 | if (populateInstruction(*Inst, i, Operands)) { |
| 1402 | OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i); |
| 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | std::set<unsigned> Sizes; |
| 1408 | for (std::map<std::pair<std::string, unsigned>, |
| 1409 | std::vector<unsigned> >::iterator |
| 1410 | I = OpcMap.begin(), E = OpcMap.end(); I != E; ++I) { |
| 1411 | // If we haven't visited this instruction width before, emit the |
| 1412 | // helper method to extract fields. |
| 1413 | if (!Sizes.count(I->first.second)) { |
| 1414 | emitHelper(o, 8*I->first.second); |
| 1415 | Sizes.insert(I->first.second); |
| 1416 | } |
| 1417 | |
| 1418 | // Emit the decoder for this namespace+width combination. |
| 1419 | FilterChooser FC(NumberedInstructions, I->second, Operands, |
| 1420 | 8*I->first.second); |
| 1421 | FC.emitTop(o, 0, I->first.first); |
| 1422 | } |
Owen Anderson | d8c8788 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1423 | |
| 1424 | o << "\n} // End llvm namespace \n"; |
| 1425 | } |