Owen Anderson | 4e81890 | 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 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 15 | #include "CodeGenTarget.h" |
James Molloy | d9ba4fd | 2012-02-09 10:56:31 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/APInt.h" |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/ADT/Twine.h" |
| 21 | #include "llvm/MC/MCFixedLenDisassembler.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 22 | #include "llvm/Support/DataTypes.h" |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FormattedStream.h" |
| 25 | #include "llvm/Support/LEB128.h" |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 91d19d8 | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 27 | #include "llvm/TableGen/Error.h" |
| 28 | #include "llvm/TableGen/Record.h" |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 29 | #include <map> |
| 30 | #include <string> |
Chandler Carruth | 91d19d8 | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 31 | #include <vector> |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
Chandler Carruth | 97acce2 | 2014-04-22 03:06:00 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "decoder-emitter" |
| 36 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | struct EncodingField { |
| 39 | unsigned Base, Width, Offset; |
| 40 | EncodingField(unsigned B, unsigned W, unsigned O) |
| 41 | : Base(B), Width(W), Offset(O) { } |
| 42 | }; |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 43 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 44 | struct OperandInfo { |
| 45 | std::vector<EncodingField> Fields; |
| 46 | std::string Decoder; |
| 47 | |
| 48 | OperandInfo(std::string D) |
| 49 | : Decoder(D) { } |
| 50 | |
| 51 | void addField(unsigned Base, unsigned Width, unsigned Offset) { |
| 52 | Fields.push_back(EncodingField(Base, Width, Offset)); |
| 53 | } |
| 54 | |
| 55 | unsigned numFields() const { return Fields.size(); } |
| 56 | |
| 57 | typedef std::vector<EncodingField>::const_iterator const_iterator; |
| 58 | |
| 59 | const_iterator begin() const { return Fields.begin(); } |
| 60 | const_iterator end() const { return Fields.end(); } |
| 61 | }; |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 62 | |
| 63 | typedef std::vector<uint8_t> DecoderTable; |
| 64 | typedef uint32_t DecoderFixup; |
| 65 | typedef std::vector<DecoderFixup> FixupList; |
| 66 | typedef std::vector<FixupList> FixupScopeList; |
| 67 | typedef SetVector<std::string> PredicateSet; |
| 68 | typedef SetVector<std::string> DecoderSet; |
| 69 | struct DecoderTableInfo { |
| 70 | DecoderTable Table; |
| 71 | FixupScopeList FixupStack; |
| 72 | PredicateSet Predicates; |
| 73 | DecoderSet Decoders; |
| 74 | }; |
| 75 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 76 | } // End anonymous namespace |
| 77 | |
| 78 | namespace { |
| 79 | class FixedLenDecoderEmitter { |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 80 | const std::vector<const CodeGenInstruction*> *NumberedInstructions; |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 81 | public: |
| 82 | |
| 83 | // Defaults preserved here for documentation, even though they aren't |
| 84 | // strictly necessary given the way that this is currently being called. |
| 85 | FixedLenDecoderEmitter(RecordKeeper &R, |
| 86 | std::string PredicateNamespace, |
| 87 | std::string GPrefix = "if (", |
| 88 | std::string GPostfix = " == MCDisassembler::Fail)" |
| 89 | " return MCDisassembler::Fail;", |
| 90 | std::string ROK = "MCDisassembler::Success", |
| 91 | std::string RFail = "MCDisassembler::Fail", |
| 92 | std::string L = "") : |
| 93 | Target(R), |
| 94 | PredicateNamespace(PredicateNamespace), |
| 95 | GuardPrefix(GPrefix), GuardPostfix(GPostfix), |
| 96 | ReturnOK(ROK), ReturnFail(RFail), Locals(L) {} |
| 97 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 98 | // Emit the decoder state machine table. |
| 99 | void emitTable(formatted_raw_ostream &o, DecoderTable &Table, |
| 100 | unsigned Indentation, unsigned BitWidth, |
| 101 | StringRef Namespace) const; |
| 102 | void emitPredicateFunction(formatted_raw_ostream &OS, |
| 103 | PredicateSet &Predicates, |
| 104 | unsigned Indentation) const; |
| 105 | void emitDecoderFunction(formatted_raw_ostream &OS, |
| 106 | DecoderSet &Decoders, |
| 107 | unsigned Indentation) const; |
| 108 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 109 | // run - Output the code emitter |
| 110 | void run(raw_ostream &o); |
| 111 | |
| 112 | private: |
| 113 | CodeGenTarget Target; |
| 114 | public: |
| 115 | std::string PredicateNamespace; |
| 116 | std::string GuardPrefix, GuardPostfix; |
| 117 | std::string ReturnOK, ReturnFail; |
| 118 | std::string Locals; |
| 119 | }; |
| 120 | } // End anonymous namespace |
| 121 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 122 | // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system |
| 123 | // for a bit value. |
| 124 | // |
| 125 | // BIT_UNFILTERED is used as the init value for a filter position. It is used |
| 126 | // only for filter processings. |
| 127 | typedef enum { |
| 128 | BIT_TRUE, // '1' |
| 129 | BIT_FALSE, // '0' |
| 130 | BIT_UNSET, // '?' |
| 131 | BIT_UNFILTERED // unfiltered |
| 132 | } bit_value_t; |
| 133 | |
| 134 | static bool ValueSet(bit_value_t V) { |
| 135 | return (V == BIT_TRUE || V == BIT_FALSE); |
| 136 | } |
| 137 | static bool ValueNotSet(bit_value_t V) { |
| 138 | return (V == BIT_UNSET); |
| 139 | } |
| 140 | static int Value(bit_value_t V) { |
| 141 | return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1); |
| 142 | } |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 143 | static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) { |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 144 | if (BitInit *bit = dyn_cast<BitInit>(bits.getBit(index))) |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 145 | return bit->getValue() ? BIT_TRUE : BIT_FALSE; |
| 146 | |
| 147 | // The bit is uninitialized. |
| 148 | return BIT_UNSET; |
| 149 | } |
| 150 | // Prints the bit value for each position. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 151 | static void dumpBits(raw_ostream &o, const BitsInit &bits) { |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 152 | for (unsigned index = bits.getNumBits(); index > 0; --index) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 153 | switch (bitFromBits(bits, index - 1)) { |
| 154 | case BIT_TRUE: |
| 155 | o << "1"; |
| 156 | break; |
| 157 | case BIT_FALSE: |
| 158 | o << "0"; |
| 159 | break; |
| 160 | case BIT_UNSET: |
| 161 | o << "_"; |
| 162 | break; |
| 163 | default: |
Craig Topper | c4965bc | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 164 | llvm_unreachable("unexpected return value from bitFromBits"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 169 | static BitsInit &getBitsField(const Record &def, const char *str) { |
| 170 | BitsInit *bits = def.getValueAsBitsInit(str); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 171 | return *bits; |
| 172 | } |
| 173 | |
| 174 | // Forward declaration. |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 175 | namespace { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 176 | class FilterChooser; |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 177 | } // End anonymous namespace |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 178 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 179 | // Representation of the instruction to work on. |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 180 | typedef std::vector<bit_value_t> insn_t; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 181 | |
| 182 | /// Filter - Filter works with FilterChooser to produce the decoding tree for |
| 183 | /// the ISA. |
| 184 | /// |
| 185 | /// It is useful to think of a Filter as governing the switch stmts of the |
| 186 | /// decoding tree in a certain level. Each case stmt delegates to an inferior |
| 187 | /// FilterChooser to decide what further decoding logic to employ, or in another |
| 188 | /// words, what other remaining bits to look at. The FilterChooser eventually |
| 189 | /// chooses a best Filter to do its job. |
| 190 | /// |
| 191 | /// This recursive scheme ends when the number of Opcodes assigned to the |
| 192 | /// FilterChooser becomes 1 or if there is a conflict. A conflict happens when |
| 193 | /// the Filter/FilterChooser combo does not know how to distinguish among the |
| 194 | /// Opcodes assigned. |
| 195 | /// |
| 196 | /// An example of a conflict is |
| 197 | /// |
| 198 | /// Conflict: |
| 199 | /// 111101000.00........00010000.... |
| 200 | /// 111101000.00........0001........ |
| 201 | /// 1111010...00........0001........ |
| 202 | /// 1111010...00.................... |
| 203 | /// 1111010......................... |
| 204 | /// 1111............................ |
| 205 | /// ................................ |
| 206 | /// VST4q8a 111101000_00________00010000____ |
| 207 | /// VST4q8b 111101000_00________00010000____ |
| 208 | /// |
| 209 | /// The Debug output shows the path that the decoding tree follows to reach the |
| 210 | /// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced |
| 211 | /// even registers, while VST4q8b is a vst4 to double-spaced odd regsisters. |
| 212 | /// |
| 213 | /// The encoding info in the .td files does not specify this meta information, |
| 214 | /// which could have been used by the decoder to resolve the conflict. The |
| 215 | /// decoder could try to decode the even/odd register numbering and assign to |
| 216 | /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a" |
| 217 | /// version and return the Opcode since the two have the same Asm format string. |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 218 | namespace { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 219 | class Filter { |
| 220 | protected: |
Craig Topper | 501d95c | 2012-03-16 06:52:56 +0000 | [diff] [blame] | 221 | const FilterChooser *Owner;// points to the FilterChooser who owns this filter |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 222 | unsigned StartBit; // the starting bit position |
| 223 | unsigned NumBits; // number of bits to filter |
| 224 | bool Mixed; // a mixed region contains both set and unset bits |
| 225 | |
| 226 | // Map of well-known segment value to the set of uid's with that value. |
| 227 | std::map<uint64_t, std::vector<unsigned> > FilteredInstructions; |
| 228 | |
| 229 | // Set of uid's with non-constant segment values. |
| 230 | std::vector<unsigned> VariableInstructions; |
| 231 | |
| 232 | // Map of well-known segment value to its delegate. |
Craig Topper | cf05f91 | 2014-09-03 06:07:54 +0000 | [diff] [blame] | 233 | std::map<unsigned, std::unique_ptr<const FilterChooser>> FilterChooserMap; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 234 | |
| 235 | // Number of instructions which fall under FilteredInstructions category. |
| 236 | unsigned NumFiltered; |
| 237 | |
| 238 | // Keeps track of the last opcode in the filtered bucket. |
| 239 | unsigned LastOpcFiltered; |
| 240 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 241 | public: |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 242 | unsigned getNumFiltered() const { return NumFiltered; } |
| 243 | unsigned getSingletonOpc() const { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 244 | assert(NumFiltered == 1); |
| 245 | return LastOpcFiltered; |
| 246 | } |
| 247 | // Return the filter chooser for the group of instructions without constant |
| 248 | // segment values. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 249 | const FilterChooser &getVariableFC() const { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 250 | assert(NumFiltered == 1); |
| 251 | assert(FilterChooserMap.size() == 1); |
| 252 | return *(FilterChooserMap.find((unsigned)-1)->second); |
| 253 | } |
| 254 | |
Craig Topper | 5c2b4ac | 2014-09-03 05:49:07 +0000 | [diff] [blame] | 255 | Filter(Filter &&f); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 256 | Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed); |
| 257 | |
| 258 | ~Filter(); |
| 259 | |
| 260 | // Divides the decoding task into sub tasks and delegates them to the |
| 261 | // inferior FilterChooser's. |
| 262 | // |
| 263 | // A special case arises when there's only one entry in the filtered |
| 264 | // instructions. In order to unambiguously decode the singleton, we need to |
| 265 | // match the remaining undecoded encoding bits against the singleton. |
| 266 | void recurse(); |
| 267 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 268 | // Emit table entries to decode instructions given a segment or segments of |
| 269 | // bits. |
| 270 | void emitTableEntry(DecoderTableInfo &TableInfo) const; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 271 | |
| 272 | // Returns the number of fanout produced by the filter. More fanout implies |
| 273 | // the filter distinguishes more categories of instructions. |
| 274 | unsigned usefulness() const; |
| 275 | }; // End of class Filter |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 276 | } // End anonymous namespace |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 277 | |
| 278 | // These are states of our finite state machines used in FilterChooser's |
| 279 | // filterProcessor() which produces the filter candidates to use. |
| 280 | typedef enum { |
| 281 | ATTR_NONE, |
| 282 | ATTR_FILTERED, |
| 283 | ATTR_ALL_SET, |
| 284 | ATTR_ALL_UNSET, |
| 285 | ATTR_MIXED |
| 286 | } bitAttr_t; |
| 287 | |
| 288 | /// FilterChooser - FilterChooser chooses the best filter among a set of Filters |
| 289 | /// in order to perform the decoding of instructions at the current level. |
| 290 | /// |
| 291 | /// Decoding proceeds from the top down. Based on the well-known encoding bits |
| 292 | /// of instructions available, FilterChooser builds up the possible Filters that |
| 293 | /// can further the task of decoding by distinguishing among the remaining |
| 294 | /// candidate instructions. |
| 295 | /// |
| 296 | /// Once a filter has been chosen, it is called upon to divide the decoding task |
| 297 | /// into sub-tasks and delegates them to its inferior FilterChoosers for further |
| 298 | /// processings. |
| 299 | /// |
| 300 | /// It is useful to think of a Filter as governing the switch stmts of the |
| 301 | /// decoding tree. And each case is delegated to an inferior FilterChooser to |
| 302 | /// decide what further remaining bits to look at. |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 303 | namespace { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 304 | class FilterChooser { |
| 305 | protected: |
| 306 | friend class Filter; |
| 307 | |
| 308 | // Vector of codegen instructions to choose our filter. |
| 309 | const std::vector<const CodeGenInstruction*> &AllInstructions; |
| 310 | |
| 311 | // Vector of uid's for this filter chooser to work on. |
Craig Topper | 501d95c | 2012-03-16 06:52:56 +0000 | [diff] [blame] | 312 | const std::vector<unsigned> &Opcodes; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 313 | |
| 314 | // Lookup table for the operand decoding of instructions. |
Craig Topper | 501d95c | 2012-03-16 06:52:56 +0000 | [diff] [blame] | 315 | const std::map<unsigned, std::vector<OperandInfo> > &Operands; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 316 | |
| 317 | // Vector of candidate filters. |
| 318 | std::vector<Filter> Filters; |
| 319 | |
| 320 | // Array of bit values passed down from our parent. |
| 321 | // Set to all BIT_UNFILTERED's for Parent == NULL. |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 322 | std::vector<bit_value_t> FilterBitValues; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 323 | |
| 324 | // Links to the FilterChooser above us in the decoding tree. |
Craig Topper | 501d95c | 2012-03-16 06:52:56 +0000 | [diff] [blame] | 325 | const FilterChooser *Parent; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 326 | |
| 327 | // Index of the best filter from Filters. |
| 328 | int BestIndex; |
| 329 | |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 330 | // Width of instructions |
| 331 | unsigned BitWidth; |
| 332 | |
Owen Anderson | a4043c4 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 333 | // Parent emitter |
| 334 | const FixedLenDecoderEmitter *Emitter; |
| 335 | |
Craig Topper | 5c2b4ac | 2014-09-03 05:49:07 +0000 | [diff] [blame] | 336 | FilterChooser(const FilterChooser &) LLVM_DELETED_FUNCTION; |
| 337 | void operator=(const FilterChooser &) LLVM_DELETED_FUNCTION; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 338 | public: |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 339 | |
| 340 | FilterChooser(const std::vector<const CodeGenInstruction*> &Insts, |
| 341 | const std::vector<unsigned> &IDs, |
Craig Topper | 501d95c | 2012-03-16 06:52:56 +0000 | [diff] [blame] | 342 | const std::map<unsigned, std::vector<OperandInfo> > &Ops, |
Owen Anderson | a4043c4 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 343 | unsigned BW, |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 344 | const FixedLenDecoderEmitter *E) |
| 345 | : AllInstructions(Insts), Opcodes(IDs), Operands(Ops), Filters(), |
Craig Topper | 1ddc288 | 2014-09-04 04:49:03 +0000 | [diff] [blame] | 346 | FilterBitValues(BW, BIT_UNFILTERED), Parent(nullptr), BestIndex(-1), |
| 347 | BitWidth(BW), Emitter(E) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 348 | doFilter(); |
| 349 | } |
| 350 | |
| 351 | FilterChooser(const std::vector<const CodeGenInstruction*> &Insts, |
| 352 | const std::vector<unsigned> &IDs, |
Craig Topper | 501d95c | 2012-03-16 06:52:56 +0000 | [diff] [blame] | 353 | const std::map<unsigned, std::vector<OperandInfo> > &Ops, |
| 354 | const std::vector<bit_value_t> &ParentFilterBitValues, |
| 355 | const FilterChooser &parent) |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 356 | : AllInstructions(Insts), Opcodes(IDs), Operands(Ops), |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 357 | Filters(), FilterBitValues(ParentFilterBitValues), |
Owen Anderson | a4043c4 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 358 | Parent(&parent), BestIndex(-1), BitWidth(parent.BitWidth), |
| 359 | Emitter(parent.Emitter) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 360 | doFilter(); |
| 361 | } |
| 362 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 363 | unsigned getBitWidth() const { return BitWidth; } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 364 | |
| 365 | protected: |
| 366 | // Populates the insn given the uid. |
| 367 | void insnWithID(insn_t &Insn, unsigned Opcode) const { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 368 | BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 369 | |
James Molloy | d9ba4fd | 2012-02-09 10:56:31 +0000 | [diff] [blame] | 370 | // We may have a SoftFail bitmask, which specifies a mask where an encoding |
| 371 | // may differ from the value in "Inst" and yet still be valid, but the |
| 372 | // disassembler should return SoftFail instead of Success. |
| 373 | // |
| 374 | // This is used for marking UNPREDICTABLE instructions in the ARM world. |
Jim Grosbach | 3f4b239 | 2012-02-29 22:07:56 +0000 | [diff] [blame] | 375 | BitsInit *SFBits = |
| 376 | AllInstructions[Opcode]->TheDef->getValueAsBitsInit("SoftFail"); |
James Molloy | d9ba4fd | 2012-02-09 10:56:31 +0000 | [diff] [blame] | 377 | |
| 378 | for (unsigned i = 0; i < BitWidth; ++i) { |
| 379 | if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE) |
| 380 | Insn.push_back(BIT_UNSET); |
| 381 | else |
| 382 | Insn.push_back(bitFromBits(Bits, i)); |
| 383 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // Returns the record name. |
| 387 | const std::string &nameWithID(unsigned Opcode) const { |
| 388 | return AllInstructions[Opcode]->TheDef->getName(); |
| 389 | } |
| 390 | |
| 391 | // Populates the field of the insn given the start position and the number of |
| 392 | // consecutive bits to scan for. |
| 393 | // |
| 394 | // Returns false if there exists any uninitialized bit value in the range. |
| 395 | // Returns true, otherwise. |
| 396 | bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit, |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 397 | unsigned NumBits) const; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 398 | |
| 399 | /// dumpFilterArray - dumpFilterArray prints out debugging info for the given |
| 400 | /// filter array as a series of chars. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 401 | void dumpFilterArray(raw_ostream &o, |
| 402 | const std::vector<bit_value_t> & filter) const; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 403 | |
| 404 | /// dumpStack - dumpStack traverses the filter chooser chain and calls |
| 405 | /// dumpFilterArray on each filter chooser up to the top level one. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 406 | void dumpStack(raw_ostream &o, const char *prefix) const; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 407 | |
| 408 | Filter &bestFilter() { |
| 409 | assert(BestIndex != -1 && "BestIndex not set"); |
| 410 | return Filters[BestIndex]; |
| 411 | } |
| 412 | |
| 413 | // Called from Filter::recurse() when singleton exists. For debug purpose. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 414 | void SingletonExists(unsigned Opc) const; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 415 | |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 416 | bool PositionFiltered(unsigned i) const { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 417 | return ValueSet(FilterBitValues[i]); |
| 418 | } |
| 419 | |
| 420 | // Calculates the island(s) needed to decode the instruction. |
| 421 | // This returns a lit of undecoded bits of an instructions, for example, |
| 422 | // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be |
| 423 | // decoded bits in order to verify that the instruction matches the Opcode. |
| 424 | unsigned getIslands(std::vector<unsigned> &StartBits, |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 425 | std::vector<unsigned> &EndBits, |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 426 | std::vector<uint64_t> &FieldVals, |
| 427 | const insn_t &Insn) const; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 428 | |
James Molloy | 8067df9 | 2011-09-07 19:42:28 +0000 | [diff] [blame] | 429 | // Emits code to check the Predicates member of an instruction are true. |
| 430 | // Returns true if predicate matches were emitted, false otherwise. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 431 | bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation, |
| 432 | unsigned Opc) const; |
James Molloy | 8067df9 | 2011-09-07 19:42:28 +0000 | [diff] [blame] | 433 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 434 | bool doesOpcodeNeedPredicate(unsigned Opc) const; |
| 435 | unsigned getPredicateIndex(DecoderTableInfo &TableInfo, StringRef P) const; |
| 436 | void emitPredicateTableEntry(DecoderTableInfo &TableInfo, |
| 437 | unsigned Opc) const; |
James Molloy | d9ba4fd | 2012-02-09 10:56:31 +0000 | [diff] [blame] | 438 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 439 | void emitSoftFailTableEntry(DecoderTableInfo &TableInfo, |
| 440 | unsigned Opc) const; |
| 441 | |
| 442 | // Emits table entries to decode the singleton. |
| 443 | void emitSingletonTableEntry(DecoderTableInfo &TableInfo, |
| 444 | unsigned Opc) const; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 445 | |
| 446 | // Emits code to decode the singleton, and then to decode the rest. |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 447 | void emitSingletonTableEntry(DecoderTableInfo &TableInfo, |
| 448 | const Filter &Best) const; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 449 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 450 | void emitBinaryParser(raw_ostream &o, unsigned &Indentation, |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 451 | const OperandInfo &OpInfo) const; |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 452 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 453 | void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc) const; |
| 454 | unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc) const; |
| 455 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 456 | // Assign a single filter and run with it. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 457 | void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 458 | |
| 459 | // reportRegion is a helper function for filterProcessor to mark a region as |
| 460 | // eligible for use as a filter region. |
| 461 | void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex, |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 462 | bool AllowMixed); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 463 | |
| 464 | // FilterProcessor scans the well-known encoding bits of the instructions and |
| 465 | // builds up a list of candidate filters. It chooses the best filter and |
| 466 | // recursively descends down the decoding tree. |
| 467 | bool filterProcessor(bool AllowMixed, bool Greedy = true); |
| 468 | |
| 469 | // Decides on the best configuration of filter(s) to use in order to decode |
| 470 | // the instructions. A conflict of instructions may occur, in which case we |
| 471 | // dump the conflict set to the standard error. |
| 472 | void doFilter(); |
| 473 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 474 | public: |
| 475 | // emitTableEntries - Emit state machine entries to decode our share of |
| 476 | // instructions. |
| 477 | void emitTableEntries(DecoderTableInfo &TableInfo) const; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 478 | }; |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 479 | } // End anonymous namespace |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 480 | |
| 481 | /////////////////////////// |
| 482 | // // |
Craig Topper | 93e6434 | 2012-03-16 00:56:01 +0000 | [diff] [blame] | 483 | // Filter Implementation // |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 484 | // // |
| 485 | /////////////////////////// |
| 486 | |
Craig Topper | 5c2b4ac | 2014-09-03 05:49:07 +0000 | [diff] [blame] | 487 | Filter::Filter(Filter &&f) |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 488 | : Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed), |
Craig Topper | 5c2b4ac | 2014-09-03 05:49:07 +0000 | [diff] [blame] | 489 | FilteredInstructions(std::move(f.FilteredInstructions)), |
| 490 | VariableInstructions(std::move(f.VariableInstructions)), |
| 491 | FilterChooserMap(std::move(f.FilterChooserMap)), NumFiltered(f.NumFiltered), |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 492 | LastOpcFiltered(f.LastOpcFiltered) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 496 | bool mixed) |
| 497 | : Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) { |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 498 | assert(StartBit + NumBits - 1 < Owner->BitWidth); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 499 | |
| 500 | NumFiltered = 0; |
| 501 | LastOpcFiltered = 0; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 502 | |
| 503 | for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) { |
| 504 | insn_t Insn; |
| 505 | |
| 506 | // Populates the insn given the uid. |
| 507 | Owner->insnWithID(Insn, Owner->Opcodes[i]); |
| 508 | |
| 509 | uint64_t Field; |
| 510 | // Scans the segment for possibly well-specified encoding bits. |
| 511 | bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits); |
| 512 | |
| 513 | if (ok) { |
| 514 | // The encoding bits are well-known. Lets add the uid of the |
| 515 | // instruction into the bucket keyed off the constant field value. |
| 516 | LastOpcFiltered = Owner->Opcodes[i]; |
| 517 | FilteredInstructions[Field].push_back(LastOpcFiltered); |
| 518 | ++NumFiltered; |
| 519 | } else { |
Craig Topper | 93e6434 | 2012-03-16 00:56:01 +0000 | [diff] [blame] | 520 | // Some of the encoding bit(s) are unspecified. This contributes to |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 521 | // one additional member of "Variable" instructions. |
| 522 | VariableInstructions.push_back(Owner->Opcodes[i]); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | assert((FilteredInstructions.size() + VariableInstructions.size() > 0) |
| 527 | && "Filter returns no instruction categories"); |
| 528 | } |
| 529 | |
| 530 | Filter::~Filter() { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // Divides the decoding task into sub tasks and delegates them to the |
| 534 | // inferior FilterChooser's. |
| 535 | // |
| 536 | // A special case arises when there's only one entry in the filtered |
| 537 | // instructions. In order to unambiguously decode the singleton, we need to |
| 538 | // match the remaining undecoded encoding bits against the singleton. |
| 539 | void Filter::recurse() { |
| 540 | std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator; |
| 541 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 542 | // Starts by inheriting our parent filter chooser's filter bit values. |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 543 | std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 544 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 545 | if (VariableInstructions.size()) { |
| 546 | // Conservatively marks each segment position as BIT_UNSET. |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 547 | for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 548 | BitValueArray[StartBit + bitIndex] = BIT_UNSET; |
| 549 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 550 | // Delegates to an inferior filter chooser for further processing on this |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 551 | // group of instructions whose segment values are variable. |
Yaron Keren | e499db0 | 2014-09-03 08:22:30 +0000 | [diff] [blame] | 552 | FilterChooserMap.insert( |
| 553 | std::make_pair(-1U, llvm::make_unique<FilterChooser>( |
| 554 | Owner->AllInstructions, VariableInstructions, |
| 555 | Owner->Operands, BitValueArray, *Owner))); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // No need to recurse for a singleton filtered instruction. |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 559 | // See also Filter::emit*(). |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 560 | if (getNumFiltered() == 1) { |
| 561 | //Owner->SingletonExists(LastOpcFiltered); |
| 562 | assert(FilterChooserMap.size() == 1); |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | // Otherwise, create sub choosers. |
| 567 | for (mapIterator = FilteredInstructions.begin(); |
| 568 | mapIterator != FilteredInstructions.end(); |
| 569 | mapIterator++) { |
| 570 | |
| 571 | // Marks all the segment positions with either BIT_TRUE or BIT_FALSE. |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 572 | for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 573 | if (mapIterator->first & (1ULL << bitIndex)) |
| 574 | BitValueArray[StartBit + bitIndex] = BIT_TRUE; |
| 575 | else |
| 576 | BitValueArray[StartBit + bitIndex] = BIT_FALSE; |
| 577 | } |
| 578 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 579 | // Delegates to an inferior filter chooser for further processing on this |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 580 | // category of instructions. |
Craig Topper | cf05f91 | 2014-09-03 06:07:54 +0000 | [diff] [blame] | 581 | FilterChooserMap.insert(std::make_pair( |
Yaron Keren | e499db0 | 2014-09-03 08:22:30 +0000 | [diff] [blame] | 582 | mapIterator->first, llvm::make_unique<FilterChooser>( |
| 583 | Owner->AllInstructions, mapIterator->second, |
| 584 | Owner->Operands, BitValueArray, *Owner))); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 588 | static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups, |
| 589 | uint32_t DestIdx) { |
| 590 | // Any NumToSkip fixups in the current scope can resolve to the |
| 591 | // current location. |
| 592 | for (FixupList::const_reverse_iterator I = Fixups.rbegin(), |
| 593 | E = Fixups.rend(); |
| 594 | I != E; ++I) { |
| 595 | // Calculate the distance from the byte following the fixup entry byte |
| 596 | // to the destination. The Target is calculated from after the 16-bit |
| 597 | // NumToSkip entry itself, so subtract two from the displacement here |
| 598 | // to account for that. |
| 599 | uint32_t FixupIdx = *I; |
| 600 | uint32_t Delta = DestIdx - FixupIdx - 2; |
| 601 | // Our NumToSkip entries are 16-bits. Make sure our table isn't too |
| 602 | // big. |
| 603 | assert(Delta < 65536U && "disassembler decoding table too large!"); |
| 604 | Table[FixupIdx] = (uint8_t)Delta; |
| 605 | Table[FixupIdx + 1] = (uint8_t)(Delta >> 8); |
| 606 | } |
| 607 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 608 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 609 | // Emit table entries to decode instructions given a segment or segments |
| 610 | // of bits. |
| 611 | void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const { |
| 612 | TableInfo.Table.push_back(MCD::OPC_ExtractField); |
| 613 | TableInfo.Table.push_back(StartBit); |
| 614 | TableInfo.Table.push_back(NumBits); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 615 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 616 | // A new filter entry begins a new scope for fixup resolution. |
| 617 | TableInfo.FixupStack.push_back(FixupList()); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 618 | |
Craig Topper | cf05f91 | 2014-09-03 06:07:54 +0000 | [diff] [blame] | 619 | std::map<unsigned, |
| 620 | std::unique_ptr<const FilterChooser>>::const_iterator filterIterator; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 621 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 622 | DecoderTable &Table = TableInfo.Table; |
| 623 | |
| 624 | size_t PrevFilter = 0; |
| 625 | bool HasFallthrough = false; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 626 | for (filterIterator = FilterChooserMap.begin(); |
| 627 | filterIterator != FilterChooserMap.end(); |
| 628 | filterIterator++) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 629 | // Field value -1 implies a non-empty set of variable instructions. |
| 630 | // See also recurse(). |
| 631 | if (filterIterator->first == (unsigned)-1) { |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 632 | HasFallthrough = true; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 633 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 634 | // Each scope should always have at least one filter value to check |
| 635 | // for. |
| 636 | assert(PrevFilter != 0 && "empty filter set!"); |
| 637 | FixupList &CurScope = TableInfo.FixupStack.back(); |
| 638 | // Resolve any NumToSkip fixups in the current scope. |
| 639 | resolveTableFixups(Table, CurScope, Table.size()); |
| 640 | CurScope.clear(); |
| 641 | PrevFilter = 0; // Don't re-process the filter's fallthrough. |
| 642 | } else { |
| 643 | Table.push_back(MCD::OPC_FilterValue); |
| 644 | // Encode and emit the value to filter against. |
| 645 | uint8_t Buffer[8]; |
| 646 | unsigned Len = encodeULEB128(filterIterator->first, Buffer); |
| 647 | Table.insert(Table.end(), Buffer, Buffer + Len); |
| 648 | // Reserve space for the NumToSkip entry. We'll backpatch the value |
| 649 | // later. |
| 650 | PrevFilter = Table.size(); |
| 651 | Table.push_back(0); |
| 652 | Table.push_back(0); |
| 653 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 654 | |
| 655 | // We arrive at a category of instructions with the same segment value. |
| 656 | // Now delegate to the sub filter chooser for further decodings. |
| 657 | // The case may fallthrough, which happens if the remaining well-known |
| 658 | // encoding bits do not match exactly. |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 659 | filterIterator->second->emitTableEntries(TableInfo); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 660 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 661 | // Now that we've emitted the body of the handler, update the NumToSkip |
| 662 | // of the filter itself to be able to skip forward when false. Subtract |
| 663 | // two as to account for the width of the NumToSkip field itself. |
| 664 | if (PrevFilter) { |
| 665 | uint32_t NumToSkip = Table.size() - PrevFilter - 2; |
| 666 | assert(NumToSkip < 65536U && "disassembler decoding table too large!"); |
| 667 | Table[PrevFilter] = (uint8_t)NumToSkip; |
| 668 | Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8); |
| 669 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 672 | // Any remaining unresolved fixups bubble up to the parent fixup scope. |
| 673 | assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!"); |
| 674 | FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1; |
| 675 | FixupScopeList::iterator Dest = Source - 1; |
| 676 | Dest->insert(Dest->end(), Source->begin(), Source->end()); |
| 677 | TableInfo.FixupStack.pop_back(); |
| 678 | |
| 679 | // If there is no fallthrough, then the final filter should get fixed |
| 680 | // up according to the enclosing scope rather than the current position. |
| 681 | if (!HasFallthrough) |
| 682 | TableInfo.FixupStack.back().push_back(PrevFilter); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | // Returns the number of fanout produced by the filter. More fanout implies |
| 686 | // the filter distinguishes more categories of instructions. |
| 687 | unsigned Filter::usefulness() const { |
| 688 | if (VariableInstructions.size()) |
| 689 | return FilteredInstructions.size(); |
| 690 | else |
| 691 | return FilteredInstructions.size() + 1; |
| 692 | } |
| 693 | |
| 694 | ////////////////////////////////// |
| 695 | // // |
| 696 | // Filterchooser Implementation // |
| 697 | // // |
| 698 | ////////////////////////////////// |
| 699 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 700 | // Emit the decoder state machine table. |
| 701 | void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS, |
| 702 | DecoderTable &Table, |
| 703 | unsigned Indentation, |
| 704 | unsigned BitWidth, |
| 705 | StringRef Namespace) const { |
| 706 | OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace |
| 707 | << BitWidth << "[] = {\n"; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 708 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 709 | Indentation += 2; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 710 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 711 | // FIXME: We may be able to use the NumToSkip values to recover |
| 712 | // appropriate indentation levels. |
| 713 | DecoderTable::const_iterator I = Table.begin(); |
| 714 | DecoderTable::const_iterator E = Table.end(); |
| 715 | while (I != E) { |
| 716 | assert (I < E && "incomplete decode table entry!"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 717 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 718 | uint64_t Pos = I - Table.begin(); |
| 719 | OS << "/* " << Pos << " */"; |
| 720 | OS.PadToColumn(12); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 721 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 722 | switch (*I) { |
| 723 | default: |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 724 | PrintFatalError("invalid decode table opcode"); |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 725 | case MCD::OPC_ExtractField: { |
| 726 | ++I; |
| 727 | unsigned Start = *I++; |
| 728 | unsigned Len = *I++; |
| 729 | OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", " |
| 730 | << Len << ", // Inst{"; |
| 731 | if (Len > 1) |
| 732 | OS << (Start + Len - 1) << "-"; |
| 733 | OS << Start << "} ...\n"; |
| 734 | break; |
| 735 | } |
| 736 | case MCD::OPC_FilterValue: { |
| 737 | ++I; |
| 738 | OS.indent(Indentation) << "MCD::OPC_FilterValue, "; |
| 739 | // The filter value is ULEB128 encoded. |
| 740 | while (*I >= 128) |
| 741 | OS << utostr(*I++) << ", "; |
| 742 | OS << utostr(*I++) << ", "; |
| 743 | |
| 744 | // 16-bit numtoskip value. |
| 745 | uint8_t Byte = *I++; |
| 746 | uint32_t NumToSkip = Byte; |
| 747 | OS << utostr(Byte) << ", "; |
| 748 | Byte = *I++; |
| 749 | OS << utostr(Byte) << ", "; |
| 750 | NumToSkip |= Byte << 8; |
| 751 | OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n"; |
| 752 | break; |
| 753 | } |
| 754 | case MCD::OPC_CheckField: { |
| 755 | ++I; |
| 756 | unsigned Start = *I++; |
| 757 | unsigned Len = *I++; |
| 758 | OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", " |
| 759 | << Len << ", ";// << Val << ", " << NumToSkip << ",\n"; |
| 760 | // ULEB128 encoded field value. |
| 761 | for (; *I >= 128; ++I) |
| 762 | OS << utostr(*I) << ", "; |
| 763 | OS << utostr(*I++) << ", "; |
| 764 | // 16-bit numtoskip value. |
| 765 | uint8_t Byte = *I++; |
| 766 | uint32_t NumToSkip = Byte; |
| 767 | OS << utostr(Byte) << ", "; |
| 768 | Byte = *I++; |
| 769 | OS << utostr(Byte) << ", "; |
| 770 | NumToSkip |= Byte << 8; |
| 771 | OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n"; |
| 772 | break; |
| 773 | } |
| 774 | case MCD::OPC_CheckPredicate: { |
| 775 | ++I; |
| 776 | OS.indent(Indentation) << "MCD::OPC_CheckPredicate, "; |
| 777 | for (; *I >= 128; ++I) |
| 778 | OS << utostr(*I) << ", "; |
| 779 | OS << utostr(*I++) << ", "; |
| 780 | |
| 781 | // 16-bit numtoskip value. |
| 782 | uint8_t Byte = *I++; |
| 783 | uint32_t NumToSkip = Byte; |
| 784 | OS << utostr(Byte) << ", "; |
| 785 | Byte = *I++; |
| 786 | OS << utostr(Byte) << ", "; |
| 787 | NumToSkip |= Byte << 8; |
| 788 | OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n"; |
| 789 | break; |
| 790 | } |
| 791 | case MCD::OPC_Decode: { |
| 792 | ++I; |
| 793 | // Extract the ULEB128 encoded Opcode to a buffer. |
| 794 | uint8_t Buffer[8], *p = Buffer; |
| 795 | while ((*p++ = *I++) >= 128) |
| 796 | assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer) |
| 797 | && "ULEB128 value too large!"); |
| 798 | // Decode the Opcode value. |
| 799 | unsigned Opc = decodeULEB128(Buffer); |
| 800 | OS.indent(Indentation) << "MCD::OPC_Decode, "; |
| 801 | for (p = Buffer; *p >= 128; ++p) |
| 802 | OS << utostr(*p) << ", "; |
| 803 | OS << utostr(*p) << ", "; |
| 804 | |
| 805 | // Decoder index. |
| 806 | for (; *I >= 128; ++I) |
| 807 | OS << utostr(*I) << ", "; |
| 808 | OS << utostr(*I++) << ", "; |
| 809 | |
| 810 | OS << "// Opcode: " |
| 811 | << NumberedInstructions->at(Opc)->TheDef->getName() << "\n"; |
| 812 | break; |
| 813 | } |
| 814 | case MCD::OPC_SoftFail: { |
| 815 | ++I; |
| 816 | OS.indent(Indentation) << "MCD::OPC_SoftFail"; |
| 817 | // Positive mask |
| 818 | uint64_t Value = 0; |
| 819 | unsigned Shift = 0; |
| 820 | do { |
| 821 | OS << ", " << utostr(*I); |
| 822 | Value += (*I & 0x7f) << Shift; |
| 823 | Shift += 7; |
| 824 | } while (*I++ >= 128); |
| 825 | if (Value > 127) |
| 826 | OS << " /* 0x" << utohexstr(Value) << " */"; |
| 827 | // Negative mask |
| 828 | Value = 0; |
| 829 | Shift = 0; |
| 830 | do { |
| 831 | OS << ", " << utostr(*I); |
| 832 | Value += (*I & 0x7f) << Shift; |
| 833 | Shift += 7; |
| 834 | } while (*I++ >= 128); |
| 835 | if (Value > 127) |
| 836 | OS << " /* 0x" << utohexstr(Value) << " */"; |
| 837 | OS << ",\n"; |
| 838 | break; |
| 839 | } |
| 840 | case MCD::OPC_Fail: { |
| 841 | ++I; |
| 842 | OS.indent(Indentation) << "MCD::OPC_Fail,\n"; |
| 843 | break; |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | OS.indent(Indentation) << "0\n"; |
| 848 | |
| 849 | Indentation -= 2; |
| 850 | |
| 851 | OS.indent(Indentation) << "};\n\n"; |
| 852 | } |
| 853 | |
| 854 | void FixedLenDecoderEmitter:: |
| 855 | emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates, |
| 856 | unsigned Indentation) const { |
| 857 | // The predicate function is just a big switch statement based on the |
| 858 | // input predicate index. |
| 859 | OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, " |
| 860 | << "uint64_t Bits) {\n"; |
| 861 | Indentation += 2; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 862 | if (!Predicates.empty()) { |
| 863 | OS.indent(Indentation) << "switch (Idx) {\n"; |
| 864 | OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n"; |
| 865 | unsigned Index = 0; |
| 866 | for (PredicateSet::const_iterator I = Predicates.begin(), E = Predicates.end(); |
| 867 | I != E; ++I, ++Index) { |
| 868 | OS.indent(Indentation) << "case " << Index << ":\n"; |
| 869 | OS.indent(Indentation+2) << "return (" << *I << ");\n"; |
| 870 | } |
| 871 | OS.indent(Indentation) << "}\n"; |
| 872 | } else { |
| 873 | // No case statement to emit |
| 874 | OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n"; |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 875 | } |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 876 | Indentation -= 2; |
| 877 | OS.indent(Indentation) << "}\n\n"; |
| 878 | } |
| 879 | |
| 880 | void FixedLenDecoderEmitter:: |
| 881 | emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders, |
| 882 | unsigned Indentation) const { |
| 883 | // The decoder function is just a big switch statement based on the |
| 884 | // input decoder index. |
| 885 | OS.indent(Indentation) << "template<typename InsnType>\n"; |
| 886 | OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S," |
| 887 | << " unsigned Idx, InsnType insn, MCInst &MI,\n"; |
| 888 | OS.indent(Indentation) << " uint64_t " |
Benjamin Kramer | 26b568d | 2012-08-15 10:26:44 +0000 | [diff] [blame] | 889 | << "Address, const void *Decoder) {\n"; |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 890 | Indentation += 2; |
| 891 | OS.indent(Indentation) << "InsnType tmp;\n"; |
| 892 | OS.indent(Indentation) << "switch (Idx) {\n"; |
| 893 | OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n"; |
| 894 | unsigned Index = 0; |
| 895 | for (DecoderSet::const_iterator I = Decoders.begin(), E = Decoders.end(); |
| 896 | I != E; ++I, ++Index) { |
| 897 | OS.indent(Indentation) << "case " << Index << ":\n"; |
Craig Topper | ebc3aa2 | 2012-08-17 05:16:15 +0000 | [diff] [blame] | 898 | OS << *I; |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 899 | OS.indent(Indentation+2) << "return S;\n"; |
| 900 | } |
| 901 | OS.indent(Indentation) << "}\n"; |
| 902 | Indentation -= 2; |
| 903 | OS.indent(Indentation) << "}\n\n"; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | // Populates the field of the insn given the start position and the number of |
| 907 | // consecutive bits to scan for. |
| 908 | // |
| 909 | // Returns false if and on the first uninitialized bit value encountered. |
| 910 | // Returns true, otherwise. |
| 911 | bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn, |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 912 | unsigned StartBit, unsigned NumBits) const { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 913 | Field = 0; |
| 914 | |
| 915 | for (unsigned i = 0; i < NumBits; ++i) { |
| 916 | if (Insn[StartBit + i] == BIT_UNSET) |
| 917 | return false; |
| 918 | |
| 919 | if (Insn[StartBit + i] == BIT_TRUE) |
| 920 | Field = Field | (1ULL << i); |
| 921 | } |
| 922 | |
| 923 | return true; |
| 924 | } |
| 925 | |
| 926 | /// dumpFilterArray - dumpFilterArray prints out debugging info for the given |
| 927 | /// filter array as a series of chars. |
| 928 | void FilterChooser::dumpFilterArray(raw_ostream &o, |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 929 | const std::vector<bit_value_t> &filter) const { |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 930 | for (unsigned bitIndex = BitWidth; bitIndex > 0; bitIndex--) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 931 | switch (filter[bitIndex - 1]) { |
| 932 | case BIT_UNFILTERED: |
| 933 | o << "."; |
| 934 | break; |
| 935 | case BIT_UNSET: |
| 936 | o << "_"; |
| 937 | break; |
| 938 | case BIT_TRUE: |
| 939 | o << "1"; |
| 940 | break; |
| 941 | case BIT_FALSE: |
| 942 | o << "0"; |
| 943 | break; |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | /// dumpStack - dumpStack traverses the filter chooser chain and calls |
| 949 | /// dumpFilterArray on each filter chooser up to the top level one. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 950 | void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const { |
| 951 | const FilterChooser *current = this; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 952 | |
| 953 | while (current) { |
| 954 | o << prefix; |
| 955 | dumpFilterArray(o, current->FilterBitValues); |
| 956 | o << '\n'; |
| 957 | current = current->Parent; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | // Called from Filter::recurse() when singleton exists. For debug purpose. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 962 | void FilterChooser::SingletonExists(unsigned Opc) const { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 963 | insn_t Insn0; |
| 964 | insnWithID(Insn0, Opc); |
| 965 | |
| 966 | errs() << "Singleton exists: " << nameWithID(Opc) |
| 967 | << " with its decoding dominating "; |
| 968 | for (unsigned i = 0; i < Opcodes.size(); ++i) { |
| 969 | if (Opcodes[i] == Opc) continue; |
| 970 | errs() << nameWithID(Opcodes[i]) << ' '; |
| 971 | } |
| 972 | errs() << '\n'; |
| 973 | |
| 974 | dumpStack(errs(), "\t\t"); |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 975 | for (unsigned i = 0; i < Opcodes.size(); ++i) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 976 | const std::string &Name = nameWithID(Opcodes[i]); |
| 977 | |
| 978 | errs() << '\t' << Name << " "; |
| 979 | dumpBits(errs(), |
| 980 | getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst")); |
| 981 | errs() << '\n'; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | // Calculates the island(s) needed to decode the instruction. |
| 986 | // This returns a list of undecoded bits of an instructions, for example, |
| 987 | // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be |
| 988 | // decoded bits in order to verify that the instruction matches the Opcode. |
| 989 | unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits, |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 990 | std::vector<unsigned> &EndBits, |
| 991 | std::vector<uint64_t> &FieldVals, |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 992 | const insn_t &Insn) const { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 993 | unsigned Num, BitNo; |
| 994 | Num = BitNo = 0; |
| 995 | |
| 996 | uint64_t FieldVal = 0; |
| 997 | |
| 998 | // 0: Init |
| 999 | // 1: Water (the bit value does not affect decoding) |
| 1000 | // 2: Island (well-known bit value needed for decoding) |
| 1001 | int State = 0; |
| 1002 | int Val = -1; |
| 1003 | |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1004 | for (unsigned i = 0; i < BitWidth; ++i) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1005 | Val = Value(Insn[i]); |
| 1006 | bool Filtered = PositionFiltered(i); |
| 1007 | switch (State) { |
Craig Topper | c4965bc | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 1008 | default: llvm_unreachable("Unreachable code!"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1009 | case 0: |
| 1010 | case 1: |
| 1011 | if (Filtered || Val == -1) |
| 1012 | State = 1; // Still in Water |
| 1013 | else { |
| 1014 | State = 2; // Into the Island |
| 1015 | BitNo = 0; |
| 1016 | StartBits.push_back(i); |
| 1017 | FieldVal = Val; |
| 1018 | } |
| 1019 | break; |
| 1020 | case 2: |
| 1021 | if (Filtered || Val == -1) { |
| 1022 | State = 1; // Into the Water |
| 1023 | EndBits.push_back(i - 1); |
| 1024 | FieldVals.push_back(FieldVal); |
| 1025 | ++Num; |
| 1026 | } else { |
| 1027 | State = 2; // Still in Island |
| 1028 | ++BitNo; |
| 1029 | FieldVal = FieldVal | Val << BitNo; |
| 1030 | } |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | // If we are still in Island after the loop, do some housekeeping. |
| 1035 | if (State == 2) { |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1036 | EndBits.push_back(BitWidth - 1); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1037 | FieldVals.push_back(FieldVal); |
| 1038 | ++Num; |
| 1039 | } |
| 1040 | |
| 1041 | assert(StartBits.size() == Num && EndBits.size() == Num && |
| 1042 | FieldVals.size() == Num); |
| 1043 | return Num; |
| 1044 | } |
| 1045 | |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1046 | void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation, |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 1047 | const OperandInfo &OpInfo) const { |
| 1048 | const std::string &Decoder = OpInfo.Decoder; |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1049 | |
| 1050 | if (OpInfo.numFields() == 1) { |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 1051 | OperandInfo::const_iterator OI = OpInfo.begin(); |
Craig Topper | ebc3aa2 | 2012-08-17 05:16:15 +0000 | [diff] [blame] | 1052 | o.indent(Indentation) << "tmp = fieldFromInstruction" |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1053 | << "(insn, " << OI->Base << ", " << OI->Width |
Craig Topper | 5996da2 | 2014-09-27 04:38:02 +0000 | [diff] [blame] | 1054 | << ")"; |
| 1055 | if (OI->Offset) |
| 1056 | o << " << " << OI->Offset; |
| 1057 | o << ";\n"; |
| 1058 | |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1059 | } else { |
Craig Topper | ebc3aa2 | 2012-08-17 05:16:15 +0000 | [diff] [blame] | 1060 | o.indent(Indentation) << "tmp = 0;\n"; |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 1061 | for (OperandInfo::const_iterator OI = OpInfo.begin(), OE = OpInfo.end(); |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1062 | OI != OE; ++OI) { |
Craig Topper | ebc3aa2 | 2012-08-17 05:16:15 +0000 | [diff] [blame] | 1063 | o.indent(Indentation) << "tmp |= (fieldFromInstruction" |
Andrew Trick | 61abca6 | 2011-09-08 05:23:14 +0000 | [diff] [blame] | 1064 | << "(insn, " << OI->Base << ", " << OI->Width |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1065 | << ") << " << OI->Offset << ");\n"; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | if (Decoder != "") |
Craig Topper | ebc3aa2 | 2012-08-17 05:16:15 +0000 | [diff] [blame] | 1070 | o.indent(Indentation) << Emitter->GuardPrefix << Decoder |
Jim Grosbach | 3f4b239 | 2012-02-29 22:07:56 +0000 | [diff] [blame] | 1071 | << "(MI, tmp, Address, Decoder)" |
| 1072 | << Emitter->GuardPostfix << "\n"; |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1073 | else |
Craig Topper | ebc3aa2 | 2012-08-17 05:16:15 +0000 | [diff] [blame] | 1074 | o.indent(Indentation) << "MI.addOperand(MCOperand::CreateImm(tmp));\n"; |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1075 | |
| 1076 | } |
| 1077 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1078 | void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation, |
| 1079 | unsigned Opc) const { |
| 1080 | std::map<unsigned, std::vector<OperandInfo> >::const_iterator OpIter = |
| 1081 | Operands.find(Opc); |
| 1082 | const std::vector<OperandInfo>& InsnOperands = OpIter->second; |
| 1083 | for (std::vector<OperandInfo>::const_iterator |
| 1084 | I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) { |
| 1085 | // If a custom instruction decoder was specified, use that. |
| 1086 | if (I->numFields() == 0 && I->Decoder.size()) { |
Craig Topper | ebc3aa2 | 2012-08-17 05:16:15 +0000 | [diff] [blame] | 1087 | OS.indent(Indentation) << Emitter->GuardPrefix << I->Decoder |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1088 | << "(MI, insn, Address, Decoder)" |
| 1089 | << Emitter->GuardPostfix << "\n"; |
| 1090 | break; |
| 1091 | } |
| 1092 | |
| 1093 | emitBinaryParser(OS, Indentation, *I); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders, |
| 1098 | unsigned Opc) const { |
| 1099 | // Build up the predicate string. |
| 1100 | SmallString<256> Decoder; |
| 1101 | // FIXME: emitDecoder() function can take a buffer directly rather than |
| 1102 | // a stream. |
| 1103 | raw_svector_ostream S(Decoder); |
Craig Topper | ebc3aa2 | 2012-08-17 05:16:15 +0000 | [diff] [blame] | 1104 | unsigned I = 4; |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1105 | emitDecoder(S, I, Opc); |
| 1106 | S.flush(); |
| 1107 | |
| 1108 | // Using the full decoder string as the key value here is a bit |
| 1109 | // heavyweight, but is effective. If the string comparisons become a |
| 1110 | // performance concern, we can implement a mangling of the predicate |
| 1111 | // data easilly enough with a map back to the actual string. That's |
| 1112 | // overkill for now, though. |
| 1113 | |
| 1114 | // Make sure the predicate is in the table. |
| 1115 | Decoders.insert(Decoder.str()); |
| 1116 | // Now figure out the index for when we write out the table. |
| 1117 | DecoderSet::const_iterator P = std::find(Decoders.begin(), |
| 1118 | Decoders.end(), |
| 1119 | Decoder.str()); |
| 1120 | return (unsigned)(P - Decoders.begin()); |
| 1121 | } |
| 1122 | |
James Molloy | 8067df9 | 2011-09-07 19:42:28 +0000 | [diff] [blame] | 1123 | static void emitSinglePredicateMatch(raw_ostream &o, StringRef str, |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 1124 | const std::string &PredicateNamespace) { |
Andrew Trick | 43674ad | 2011-09-08 05:25:49 +0000 | [diff] [blame] | 1125 | if (str[0] == '!') |
| 1126 | o << "!(Bits & " << PredicateNamespace << "::" |
| 1127 | << str.slice(1,str.size()) << ")"; |
James Molloy | 8067df9 | 2011-09-07 19:42:28 +0000 | [diff] [blame] | 1128 | else |
Andrew Trick | 43674ad | 2011-09-08 05:25:49 +0000 | [diff] [blame] | 1129 | o << "(Bits & " << PredicateNamespace << "::" << str << ")"; |
James Molloy | 8067df9 | 2011-09-07 19:42:28 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation, |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 1133 | unsigned Opc) const { |
Jim Grosbach | 3f4b239 | 2012-02-29 22:07:56 +0000 | [diff] [blame] | 1134 | ListInit *Predicates = |
| 1135 | AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates"); |
James Molloy | 8067df9 | 2011-09-07 19:42:28 +0000 | [diff] [blame] | 1136 | for (unsigned i = 0; i < Predicates->getSize(); ++i) { |
| 1137 | Record *Pred = Predicates->getElementAsRecord(i); |
| 1138 | if (!Pred->getValue("AssemblerMatcherPredicate")) |
| 1139 | continue; |
| 1140 | |
| 1141 | std::string P = Pred->getValueAsString("AssemblerCondString"); |
| 1142 | |
| 1143 | if (!P.length()) |
| 1144 | continue; |
| 1145 | |
| 1146 | if (i != 0) |
| 1147 | o << " && "; |
| 1148 | |
| 1149 | StringRef SR(P); |
| 1150 | std::pair<StringRef, StringRef> pairs = SR.split(','); |
| 1151 | while (pairs.second.size()) { |
| 1152 | emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace); |
| 1153 | o << " && "; |
| 1154 | pairs = pairs.second.split(','); |
| 1155 | } |
| 1156 | emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace); |
| 1157 | } |
| 1158 | return Predicates->getSize() > 0; |
Andrew Trick | 61abca6 | 2011-09-08 05:23:14 +0000 | [diff] [blame] | 1159 | } |
James Molloy | 8067df9 | 2011-09-07 19:42:28 +0000 | [diff] [blame] | 1160 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1161 | bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const { |
| 1162 | ListInit *Predicates = |
| 1163 | AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates"); |
| 1164 | for (unsigned i = 0; i < Predicates->getSize(); ++i) { |
| 1165 | Record *Pred = Predicates->getElementAsRecord(i); |
| 1166 | if (!Pred->getValue("AssemblerMatcherPredicate")) |
| 1167 | continue; |
| 1168 | |
| 1169 | std::string P = Pred->getValueAsString("AssemblerCondString"); |
| 1170 | |
| 1171 | if (!P.length()) |
| 1172 | continue; |
| 1173 | |
| 1174 | return true; |
| 1175 | } |
| 1176 | return false; |
| 1177 | } |
| 1178 | |
| 1179 | unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo, |
| 1180 | StringRef Predicate) const { |
| 1181 | // Using the full predicate string as the key value here is a bit |
| 1182 | // heavyweight, but is effective. If the string comparisons become a |
| 1183 | // performance concern, we can implement a mangling of the predicate |
| 1184 | // data easilly enough with a map back to the actual string. That's |
| 1185 | // overkill for now, though. |
| 1186 | |
| 1187 | // Make sure the predicate is in the table. |
| 1188 | TableInfo.Predicates.insert(Predicate.str()); |
| 1189 | // Now figure out the index for when we write out the table. |
| 1190 | PredicateSet::const_iterator P = std::find(TableInfo.Predicates.begin(), |
| 1191 | TableInfo.Predicates.end(), |
| 1192 | Predicate.str()); |
| 1193 | return (unsigned)(P - TableInfo.Predicates.begin()); |
| 1194 | } |
| 1195 | |
| 1196 | void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo, |
| 1197 | unsigned Opc) const { |
| 1198 | if (!doesOpcodeNeedPredicate(Opc)) |
| 1199 | return; |
| 1200 | |
| 1201 | // Build up the predicate string. |
| 1202 | SmallString<256> Predicate; |
| 1203 | // FIXME: emitPredicateMatch() functions can take a buffer directly rather |
| 1204 | // than a stream. |
| 1205 | raw_svector_ostream PS(Predicate); |
| 1206 | unsigned I = 0; |
| 1207 | emitPredicateMatch(PS, I, Opc); |
| 1208 | |
| 1209 | // Figure out the index into the predicate table for the predicate just |
| 1210 | // computed. |
| 1211 | unsigned PIdx = getPredicateIndex(TableInfo, PS.str()); |
| 1212 | SmallString<16> PBytes; |
| 1213 | raw_svector_ostream S(PBytes); |
| 1214 | encodeULEB128(PIdx, S); |
| 1215 | S.flush(); |
| 1216 | |
| 1217 | TableInfo.Table.push_back(MCD::OPC_CheckPredicate); |
| 1218 | // Predicate index |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 1219 | for (unsigned i = 0, e = PBytes.size(); i != e; ++i) |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1220 | TableInfo.Table.push_back(PBytes[i]); |
| 1221 | // Push location for NumToSkip backpatching. |
| 1222 | TableInfo.FixupStack.back().push_back(TableInfo.Table.size()); |
| 1223 | TableInfo.Table.push_back(0); |
| 1224 | TableInfo.Table.push_back(0); |
| 1225 | } |
| 1226 | |
| 1227 | void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo, |
| 1228 | unsigned Opc) const { |
Jim Grosbach | 3f4b239 | 2012-02-29 22:07:56 +0000 | [diff] [blame] | 1229 | BitsInit *SFBits = |
| 1230 | AllInstructions[Opc]->TheDef->getValueAsBitsInit("SoftFail"); |
James Molloy | d9ba4fd | 2012-02-09 10:56:31 +0000 | [diff] [blame] | 1231 | if (!SFBits) return; |
| 1232 | BitsInit *InstBits = AllInstructions[Opc]->TheDef->getValueAsBitsInit("Inst"); |
| 1233 | |
| 1234 | APInt PositiveMask(BitWidth, 0ULL); |
| 1235 | APInt NegativeMask(BitWidth, 0ULL); |
| 1236 | for (unsigned i = 0; i < BitWidth; ++i) { |
| 1237 | bit_value_t B = bitFromBits(*SFBits, i); |
| 1238 | bit_value_t IB = bitFromBits(*InstBits, i); |
| 1239 | |
| 1240 | if (B != BIT_TRUE) continue; |
| 1241 | |
| 1242 | switch (IB) { |
| 1243 | case BIT_FALSE: |
| 1244 | // The bit is meant to be false, so emit a check to see if it is true. |
| 1245 | PositiveMask.setBit(i); |
| 1246 | break; |
| 1247 | case BIT_TRUE: |
| 1248 | // The bit is meant to be true, so emit a check to see if it is false. |
| 1249 | NegativeMask.setBit(i); |
| 1250 | break; |
| 1251 | default: |
| 1252 | // The bit is not set; this must be an error! |
| 1253 | StringRef Name = AllInstructions[Opc]->TheDef->getName(); |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1254 | errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in " << Name |
| 1255 | << " is set but Inst{" << i << "} is unset!\n" |
James Molloy | d9ba4fd | 2012-02-09 10:56:31 +0000 | [diff] [blame] | 1256 | << " - You can only mark a bit as SoftFail if it is fully defined" |
| 1257 | << " (1/0 - not '?') in Inst\n"; |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1258 | return; |
James Molloy | d9ba4fd | 2012-02-09 10:56:31 +0000 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | bool NeedPositiveMask = PositiveMask.getBoolValue(); |
| 1263 | bool NeedNegativeMask = NegativeMask.getBoolValue(); |
| 1264 | |
| 1265 | if (!NeedPositiveMask && !NeedNegativeMask) |
| 1266 | return; |
| 1267 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1268 | TableInfo.Table.push_back(MCD::OPC_SoftFail); |
James Molloy | d9ba4fd | 2012-02-09 10:56:31 +0000 | [diff] [blame] | 1269 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1270 | SmallString<16> MaskBytes; |
| 1271 | raw_svector_ostream S(MaskBytes); |
| 1272 | if (NeedPositiveMask) { |
| 1273 | encodeULEB128(PositiveMask.getZExtValue(), S); |
| 1274 | S.flush(); |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 1275 | for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i) |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1276 | TableInfo.Table.push_back(MaskBytes[i]); |
| 1277 | } else |
| 1278 | TableInfo.Table.push_back(0); |
| 1279 | if (NeedNegativeMask) { |
| 1280 | MaskBytes.clear(); |
| 1281 | S.resync(); |
| 1282 | encodeULEB128(NegativeMask.getZExtValue(), S); |
| 1283 | S.flush(); |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 1284 | for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i) |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1285 | TableInfo.Table.push_back(MaskBytes[i]); |
| 1286 | } else |
| 1287 | TableInfo.Table.push_back(0); |
James Molloy | d9ba4fd | 2012-02-09 10:56:31 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1290 | // Emits table entries to decode the singleton. |
| 1291 | void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo, |
| 1292 | unsigned Opc) const { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1293 | std::vector<unsigned> StartBits; |
| 1294 | std::vector<unsigned> EndBits; |
| 1295 | std::vector<uint64_t> FieldVals; |
| 1296 | insn_t Insn; |
| 1297 | insnWithID(Insn, Opc); |
| 1298 | |
| 1299 | // Look for islands of undecoded bits of the singleton. |
| 1300 | getIslands(StartBits, EndBits, FieldVals, Insn); |
| 1301 | |
| 1302 | unsigned Size = StartBits.size(); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1303 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1304 | // Emit the predicate table entry if one is needed. |
| 1305 | emitPredicateTableEntry(TableInfo, Opc); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1306 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1307 | // Check any additional encoding fields needed. |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 1308 | for (unsigned I = Size; I != 0; --I) { |
| 1309 | unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1; |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1310 | TableInfo.Table.push_back(MCD::OPC_CheckField); |
| 1311 | TableInfo.Table.push_back(StartBits[I-1]); |
| 1312 | TableInfo.Table.push_back(NumBits); |
| 1313 | uint8_t Buffer[8], *p; |
| 1314 | encodeULEB128(FieldVals[I-1], Buffer); |
| 1315 | for (p = Buffer; *p >= 128 ; ++p) |
| 1316 | TableInfo.Table.push_back(*p); |
| 1317 | TableInfo.Table.push_back(*p); |
| 1318 | // Push location for NumToSkip backpatching. |
| 1319 | TableInfo.FixupStack.back().push_back(TableInfo.Table.size()); |
| 1320 | // The fixup is always 16-bits, so go ahead and allocate the space |
| 1321 | // in the table so all our relative position calculations work OK even |
| 1322 | // before we fully resolve the real value here. |
| 1323 | TableInfo.Table.push_back(0); |
| 1324 | TableInfo.Table.push_back(0); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1325 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1326 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1327 | // Check for soft failure of the match. |
| 1328 | emitSoftFailTableEntry(TableInfo, Opc); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1329 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1330 | TableInfo.Table.push_back(MCD::OPC_Decode); |
| 1331 | uint8_t Buffer[8], *p; |
| 1332 | encodeULEB128(Opc, Buffer); |
| 1333 | for (p = Buffer; *p >= 128 ; ++p) |
| 1334 | TableInfo.Table.push_back(*p); |
| 1335 | TableInfo.Table.push_back(*p); |
| 1336 | |
| 1337 | unsigned DIdx = getDecoderIndex(TableInfo.Decoders, Opc); |
| 1338 | SmallString<16> Bytes; |
| 1339 | raw_svector_ostream S(Bytes); |
| 1340 | encodeULEB128(DIdx, S); |
| 1341 | S.flush(); |
| 1342 | |
| 1343 | // Decoder index |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 1344 | for (unsigned i = 0, e = Bytes.size(); i != e; ++i) |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1345 | TableInfo.Table.push_back(Bytes[i]); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1348 | // Emits table entries to decode the singleton, and then to decode the rest. |
| 1349 | void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo, |
| 1350 | const Filter &Best) const { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1351 | unsigned Opc = Best.getSingletonOpc(); |
| 1352 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1353 | // complex singletons need predicate checks from the first singleton |
| 1354 | // to refer forward to the variable filterchooser that follows. |
| 1355 | TableInfo.FixupStack.push_back(FixupList()); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1356 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1357 | emitSingletonTableEntry(TableInfo, Opc); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1358 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1359 | resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(), |
| 1360 | TableInfo.Table.size()); |
| 1361 | TableInfo.FixupStack.pop_back(); |
| 1362 | |
| 1363 | Best.getVariableFC().emitTableEntries(TableInfo); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1366 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1367 | // Assign a single filter and run with it. Top level API client can initialize |
| 1368 | // with a single filter to start the filtering process. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 1369 | void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit, |
| 1370 | bool mixed) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1371 | Filters.clear(); |
Craig Topper | 5c2b4ac | 2014-09-03 05:49:07 +0000 | [diff] [blame] | 1372 | Filters.push_back(Filter(*this, startBit, numBit, true)); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1373 | BestIndex = 0; // Sole Filter instance to choose from. |
| 1374 | bestFilter().recurse(); |
| 1375 | } |
| 1376 | |
| 1377 | // reportRegion is a helper function for filterProcessor to mark a region as |
| 1378 | // eligible for use as a filter region. |
| 1379 | void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit, |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 1380 | unsigned BitIndex, bool AllowMixed) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1381 | if (RA == ATTR_MIXED && AllowMixed) |
| 1382 | Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true)); |
| 1383 | else if (RA == ATTR_ALL_SET && !AllowMixed) |
| 1384 | Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false)); |
| 1385 | } |
| 1386 | |
| 1387 | // FilterProcessor scans the well-known encoding bits of the instructions and |
| 1388 | // builds up a list of candidate filters. It chooses the best filter and |
| 1389 | // recursively descends down the decoding tree. |
| 1390 | bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) { |
| 1391 | Filters.clear(); |
| 1392 | BestIndex = -1; |
| 1393 | unsigned numInstructions = Opcodes.size(); |
| 1394 | |
| 1395 | assert(numInstructions && "Filter created with no instructions"); |
| 1396 | |
| 1397 | // No further filtering is necessary. |
| 1398 | if (numInstructions == 1) |
| 1399 | return true; |
| 1400 | |
| 1401 | // Heuristics. See also doFilter()'s "Heuristics" comment when num of |
| 1402 | // instructions is 3. |
| 1403 | if (AllowMixed && !Greedy) { |
| 1404 | assert(numInstructions == 3); |
| 1405 | |
| 1406 | for (unsigned i = 0; i < Opcodes.size(); ++i) { |
| 1407 | std::vector<unsigned> StartBits; |
| 1408 | std::vector<unsigned> EndBits; |
| 1409 | std::vector<uint64_t> FieldVals; |
| 1410 | insn_t Insn; |
| 1411 | |
| 1412 | insnWithID(Insn, Opcodes[i]); |
| 1413 | |
| 1414 | // Look for islands of undecoded bits of any instruction. |
| 1415 | if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) { |
| 1416 | // Found an instruction with island(s). Now just assign a filter. |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 1417 | runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1418 | return true; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 1423 | unsigned BitIndex; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1424 | |
| 1425 | // We maintain BIT_WIDTH copies of the bitAttrs automaton. |
| 1426 | // The automaton consumes the corresponding bit from each |
| 1427 | // instruction. |
| 1428 | // |
| 1429 | // Input symbols: 0, 1, and _ (unset). |
| 1430 | // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED. |
| 1431 | // Initial state: NONE. |
| 1432 | // |
| 1433 | // (NONE) ------- [01] -> (ALL_SET) |
| 1434 | // (NONE) ------- _ ----> (ALL_UNSET) |
| 1435 | // (ALL_SET) ---- [01] -> (ALL_SET) |
| 1436 | // (ALL_SET) ---- _ ----> (MIXED) |
| 1437 | // (ALL_UNSET) -- [01] -> (MIXED) |
| 1438 | // (ALL_UNSET) -- _ ----> (ALL_UNSET) |
| 1439 | // (MIXED) ------ . ----> (MIXED) |
| 1440 | // (FILTERED)---- . ----> (FILTERED) |
| 1441 | |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1442 | std::vector<bitAttr_t> bitAttrs; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1443 | |
| 1444 | // FILTERED bit positions provide no entropy and are not worthy of pursuing. |
| 1445 | // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position. |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1446 | for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1447 | if (FilterBitValues[BitIndex] == BIT_TRUE || |
| 1448 | FilterBitValues[BitIndex] == BIT_FALSE) |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1449 | bitAttrs.push_back(ATTR_FILTERED); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1450 | else |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1451 | bitAttrs.push_back(ATTR_NONE); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1452 | |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 1453 | for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1454 | insn_t insn; |
| 1455 | |
| 1456 | insnWithID(insn, Opcodes[InsnIndex]); |
| 1457 | |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 1458 | for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1459 | switch (bitAttrs[BitIndex]) { |
| 1460 | case ATTR_NONE: |
| 1461 | if (insn[BitIndex] == BIT_UNSET) |
| 1462 | bitAttrs[BitIndex] = ATTR_ALL_UNSET; |
| 1463 | else |
| 1464 | bitAttrs[BitIndex] = ATTR_ALL_SET; |
| 1465 | break; |
| 1466 | case ATTR_ALL_SET: |
| 1467 | if (insn[BitIndex] == BIT_UNSET) |
| 1468 | bitAttrs[BitIndex] = ATTR_MIXED; |
| 1469 | break; |
| 1470 | case ATTR_ALL_UNSET: |
| 1471 | if (insn[BitIndex] != BIT_UNSET) |
| 1472 | bitAttrs[BitIndex] = ATTR_MIXED; |
| 1473 | break; |
| 1474 | case ATTR_MIXED: |
| 1475 | case ATTR_FILTERED: |
| 1476 | break; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | // The regionAttr automaton consumes the bitAttrs automatons' state, |
| 1482 | // lowest-to-highest. |
| 1483 | // |
| 1484 | // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed) |
| 1485 | // States: NONE, ALL_SET, MIXED |
| 1486 | // Initial state: NONE |
| 1487 | // |
| 1488 | // (NONE) ----- F --> (NONE) |
| 1489 | // (NONE) ----- S --> (ALL_SET) ; and set region start |
| 1490 | // (NONE) ----- U --> (NONE) |
| 1491 | // (NONE) ----- M --> (MIXED) ; and set region start |
| 1492 | // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region |
| 1493 | // (ALL_SET) -- S --> (ALL_SET) |
| 1494 | // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region |
| 1495 | // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region |
| 1496 | // (MIXED) ---- F --> (NONE) ; and report a MIXED region |
| 1497 | // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region |
| 1498 | // (MIXED) ---- U --> (NONE) ; and report a MIXED region |
| 1499 | // (MIXED) ---- M --> (MIXED) |
| 1500 | |
| 1501 | bitAttr_t RA = ATTR_NONE; |
| 1502 | unsigned StartBit = 0; |
| 1503 | |
Craig Topper | 29688ab | 2012-08-17 05:42:16 +0000 | [diff] [blame] | 1504 | for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1505 | bitAttr_t bitAttr = bitAttrs[BitIndex]; |
| 1506 | |
| 1507 | assert(bitAttr != ATTR_NONE && "Bit without attributes"); |
| 1508 | |
| 1509 | switch (RA) { |
| 1510 | case ATTR_NONE: |
| 1511 | switch (bitAttr) { |
| 1512 | case ATTR_FILTERED: |
| 1513 | break; |
| 1514 | case ATTR_ALL_SET: |
| 1515 | StartBit = BitIndex; |
| 1516 | RA = ATTR_ALL_SET; |
| 1517 | break; |
| 1518 | case ATTR_ALL_UNSET: |
| 1519 | break; |
| 1520 | case ATTR_MIXED: |
| 1521 | StartBit = BitIndex; |
| 1522 | RA = ATTR_MIXED; |
| 1523 | break; |
| 1524 | default: |
Craig Topper | c4965bc | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 1525 | llvm_unreachable("Unexpected bitAttr!"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1526 | } |
| 1527 | break; |
| 1528 | case ATTR_ALL_SET: |
| 1529 | switch (bitAttr) { |
| 1530 | case ATTR_FILTERED: |
| 1531 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1532 | RA = ATTR_NONE; |
| 1533 | break; |
| 1534 | case ATTR_ALL_SET: |
| 1535 | break; |
| 1536 | case ATTR_ALL_UNSET: |
| 1537 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1538 | RA = ATTR_NONE; |
| 1539 | break; |
| 1540 | case ATTR_MIXED: |
| 1541 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1542 | StartBit = BitIndex; |
| 1543 | RA = ATTR_MIXED; |
| 1544 | break; |
| 1545 | default: |
Craig Topper | c4965bc | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 1546 | llvm_unreachable("Unexpected bitAttr!"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1547 | } |
| 1548 | break; |
| 1549 | case ATTR_MIXED: |
| 1550 | switch (bitAttr) { |
| 1551 | case ATTR_FILTERED: |
| 1552 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1553 | StartBit = BitIndex; |
| 1554 | RA = ATTR_NONE; |
| 1555 | break; |
| 1556 | case ATTR_ALL_SET: |
| 1557 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1558 | StartBit = BitIndex; |
| 1559 | RA = ATTR_ALL_SET; |
| 1560 | break; |
| 1561 | case ATTR_ALL_UNSET: |
| 1562 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1563 | RA = ATTR_NONE; |
| 1564 | break; |
| 1565 | case ATTR_MIXED: |
| 1566 | break; |
| 1567 | default: |
Craig Topper | c4965bc | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 1568 | llvm_unreachable("Unexpected bitAttr!"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1569 | } |
| 1570 | break; |
| 1571 | case ATTR_ALL_UNSET: |
Craig Topper | c4965bc | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 1572 | llvm_unreachable("regionAttr state machine has no ATTR_UNSET state"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1573 | case ATTR_FILTERED: |
Craig Topper | c4965bc | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 1574 | llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | // At the end, if we're still in ALL_SET or MIXED states, report a region |
| 1579 | switch (RA) { |
| 1580 | case ATTR_NONE: |
| 1581 | break; |
| 1582 | case ATTR_FILTERED: |
| 1583 | break; |
| 1584 | case ATTR_ALL_SET: |
| 1585 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1586 | break; |
| 1587 | case ATTR_ALL_UNSET: |
| 1588 | break; |
| 1589 | case ATTR_MIXED: |
| 1590 | reportRegion(RA, StartBit, BitIndex, AllowMixed); |
| 1591 | break; |
| 1592 | } |
| 1593 | |
| 1594 | // We have finished with the filter processings. Now it's time to choose |
| 1595 | // the best performing filter. |
| 1596 | BestIndex = 0; |
| 1597 | bool AllUseless = true; |
| 1598 | unsigned BestScore = 0; |
| 1599 | |
| 1600 | for (unsigned i = 0, e = Filters.size(); i != e; ++i) { |
| 1601 | unsigned Usefulness = Filters[i].usefulness(); |
| 1602 | |
| 1603 | if (Usefulness) |
| 1604 | AllUseless = false; |
| 1605 | |
| 1606 | if (Usefulness > BestScore) { |
| 1607 | BestIndex = i; |
| 1608 | BestScore = Usefulness; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (!AllUseless) |
| 1613 | bestFilter().recurse(); |
| 1614 | |
| 1615 | return !AllUseless; |
| 1616 | } // end of FilterChooser::filterProcessor(bool) |
| 1617 | |
| 1618 | // Decides on the best configuration of filter(s) to use in order to decode |
| 1619 | // the instructions. A conflict of instructions may occur, in which case we |
| 1620 | // dump the conflict set to the standard error. |
| 1621 | void FilterChooser::doFilter() { |
| 1622 | unsigned Num = Opcodes.size(); |
| 1623 | assert(Num && "FilterChooser created with no instructions"); |
| 1624 | |
| 1625 | // Try regions of consecutive known bit values first. |
| 1626 | if (filterProcessor(false)) |
| 1627 | return; |
| 1628 | |
| 1629 | // Then regions of mixed bits (both known and unitialized bit values allowed). |
| 1630 | if (filterProcessor(true)) |
| 1631 | return; |
| 1632 | |
| 1633 | // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where |
| 1634 | // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a |
| 1635 | // well-known encoding pattern. In such case, we backtrack and scan for the |
| 1636 | // the very first consecutive ATTR_ALL_SET region and assign a filter to it. |
| 1637 | if (Num == 3 && filterProcessor(true, false)) |
| 1638 | return; |
| 1639 | |
| 1640 | // If we come to here, the instruction decoding has failed. |
| 1641 | // Set the BestIndex to -1 to indicate so. |
| 1642 | BestIndex = -1; |
| 1643 | } |
| 1644 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1645 | // emitTableEntries - Emit state machine entries to decode our share of |
| 1646 | // instructions. |
| 1647 | void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const { |
| 1648 | if (Opcodes.size() == 1) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1649 | // There is only one instruction in the set, which is great! |
| 1650 | // Call emitSingletonDecoder() to see whether there are any remaining |
| 1651 | // encodings bits. |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1652 | emitSingletonTableEntry(TableInfo, Opcodes[0]); |
| 1653 | return; |
| 1654 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1655 | |
| 1656 | // Choose the best filter to do the decodings! |
| 1657 | if (BestIndex != -1) { |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 1658 | const Filter &Best = Filters[BestIndex]; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1659 | if (Best.getNumFiltered() == 1) |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1660 | emitSingletonTableEntry(TableInfo, Best); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1661 | else |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1662 | Best.emitTableEntry(TableInfo); |
| 1663 | return; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1666 | // We don't know how to decode these instructions! Dump the |
| 1667 | // conflict set and bail. |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1668 | |
| 1669 | // Print out useful conflict information for postmortem analysis. |
| 1670 | errs() << "Decoding Conflict:\n"; |
| 1671 | |
| 1672 | dumpStack(errs(), "\t\t"); |
| 1673 | |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 1674 | for (unsigned i = 0; i < Opcodes.size(); ++i) { |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1675 | const std::string &Name = nameWithID(Opcodes[i]); |
| 1676 | |
| 1677 | errs() << '\t' << Name << " "; |
| 1678 | dumpBits(errs(), |
| 1679 | getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst")); |
| 1680 | errs() << '\n'; |
| 1681 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 1684 | static bool populateInstruction(CodeGenTarget &Target, |
| 1685 | const CodeGenInstruction &CGI, unsigned Opc, |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 1686 | std::map<unsigned, std::vector<OperandInfo> > &Operands){ |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1687 | const Record &Def = *CGI.TheDef; |
| 1688 | // If all the bit positions are not specified; do not decode this instruction. |
| 1689 | // We are bound to fail! For proper disassembly, the well-known encoding bits |
| 1690 | // of the instruction must be fully specified. |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1691 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1692 | BitsInit &Bits = getBitsField(Def, "Inst"); |
Jim Grosbach | f3fd36e | 2011-07-06 21:33:38 +0000 | [diff] [blame] | 1693 | if (Bits.allInComplete()) return false; |
| 1694 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1695 | std::vector<OperandInfo> InsnOperands; |
| 1696 | |
| 1697 | // If the instruction has specified a custom decoding hook, use that instead |
| 1698 | // of trying to auto-generate the decoder. |
| 1699 | std::string InstDecoder = Def.getValueAsString("DecoderMethod"); |
| 1700 | if (InstDecoder != "") { |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1701 | InsnOperands.push_back(OperandInfo(InstDecoder)); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1702 | Operands[Opc] = InsnOperands; |
| 1703 | return true; |
| 1704 | } |
| 1705 | |
| 1706 | // Generate a description of the operand of the instruction that we know |
| 1707 | // how to decode automatically. |
| 1708 | // FIXME: We'll need to have a way to manually override this as needed. |
| 1709 | |
| 1710 | // Gather the outputs/inputs of the instruction, so we can find their |
| 1711 | // positions in the encoding. This assumes for now that they appear in the |
| 1712 | // MCInst in the order that they're listed. |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1713 | std::vector<std::pair<Init*, std::string> > InOutOperands; |
| 1714 | DagInit *Out = Def.getValueAsDag("OutOperandList"); |
| 1715 | DagInit *In = Def.getValueAsDag("InOperandList"); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1716 | for (unsigned i = 0; i < Out->getNumArgs(); ++i) |
| 1717 | InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i))); |
| 1718 | for (unsigned i = 0; i < In->getNumArgs(); ++i) |
| 1719 | InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i))); |
| 1720 | |
Owen Anderson | 53562d0 | 2011-07-28 23:56:20 +0000 | [diff] [blame] | 1721 | // Search for tied operands, so that we can correctly instantiate |
| 1722 | // operands that are not explicitly represented in the encoding. |
Owen Anderson | cb32ce2 | 2011-07-29 18:28:52 +0000 | [diff] [blame] | 1723 | std::map<std::string, std::string> TiedNames; |
Owen Anderson | 53562d0 | 2011-07-28 23:56:20 +0000 | [diff] [blame] | 1724 | for (unsigned i = 0; i < CGI.Operands.size(); ++i) { |
| 1725 | int tiedTo = CGI.Operands[i].getTiedRegister(); |
Owen Anderson | cb32ce2 | 2011-07-29 18:28:52 +0000 | [diff] [blame] | 1726 | if (tiedTo != -1) { |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 1727 | std::pair<unsigned, unsigned> SO = |
| 1728 | CGI.Operands.getSubOperandNumber(tiedTo); |
| 1729 | TiedNames[InOutOperands[i].second] = InOutOperands[SO.first].second; |
| 1730 | TiedNames[InOutOperands[SO.first].second] = InOutOperands[i].second; |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | std::map<std::string, std::vector<OperandInfo> > NumberedInsnOperands; |
| 1735 | std::set<std::string> NumberedInsnOperandsNoTie; |
| 1736 | if (Target.getInstructionSet()-> |
| 1737 | getValueAsBit("decodePositionallyEncodedOperands")) { |
| 1738 | const std::vector<RecordVal> &Vals = Def.getValues(); |
| 1739 | unsigned NumberedOp = 0; |
| 1740 | |
Hal Finkel | 5457bd0 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 1741 | std::set<unsigned> NamedOpIndices; |
| 1742 | if (Target.getInstructionSet()-> |
| 1743 | getValueAsBit("noNamedPositionallyEncodedOperands")) |
| 1744 | // Collect the set of operand indices that might correspond to named |
| 1745 | // operand, and skip these when assigning operands based on position. |
| 1746 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 1747 | unsigned OpIdx; |
| 1748 | if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx)) |
| 1749 | continue; |
| 1750 | |
| 1751 | NamedOpIndices.insert(OpIdx); |
| 1752 | } |
| 1753 | |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 1754 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 1755 | // Ignore fixed fields in the record, we're looking for values like: |
| 1756 | // bits<5> RST = { ?, ?, ?, ?, ? }; |
| 1757 | if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete()) |
| 1758 | continue; |
| 1759 | |
| 1760 | // Determine if Vals[i] actually contributes to the Inst encoding. |
| 1761 | unsigned bi = 0; |
| 1762 | for (; bi < Bits.getNumBits(); ++bi) { |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 1763 | VarInit *Var = nullptr; |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 1764 | VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi)); |
| 1765 | if (BI) |
| 1766 | Var = dyn_cast<VarInit>(BI->getBitVar()); |
| 1767 | else |
| 1768 | Var = dyn_cast<VarInit>(Bits.getBit(bi)); |
| 1769 | |
| 1770 | if (Var && Var->getName() == Vals[i].getName()) |
| 1771 | break; |
| 1772 | } |
| 1773 | |
| 1774 | if (bi == Bits.getNumBits()) |
| 1775 | continue; |
| 1776 | |
| 1777 | // Skip variables that correspond to explicitly-named operands. |
| 1778 | unsigned OpIdx; |
| 1779 | if (CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx)) |
| 1780 | continue; |
| 1781 | |
| 1782 | // Get the bit range for this operand: |
| 1783 | unsigned bitStart = bi++, bitWidth = 1; |
| 1784 | for (; bi < Bits.getNumBits(); ++bi) { |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 1785 | VarInit *Var = nullptr; |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 1786 | VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi)); |
| 1787 | if (BI) |
| 1788 | Var = dyn_cast<VarInit>(BI->getBitVar()); |
| 1789 | else |
| 1790 | Var = dyn_cast<VarInit>(Bits.getBit(bi)); |
| 1791 | |
| 1792 | if (!Var) |
| 1793 | break; |
| 1794 | |
| 1795 | if (Var->getName() != Vals[i].getName()) |
| 1796 | break; |
| 1797 | |
| 1798 | ++bitWidth; |
| 1799 | } |
| 1800 | |
| 1801 | unsigned NumberOps = CGI.Operands.size(); |
| 1802 | while (NumberedOp < NumberOps && |
Hal Finkel | 5457bd0 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 1803 | (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) || |
| 1804 | (NamedOpIndices.size() && NamedOpIndices.count( |
| 1805 | CGI.Operands.getSubOperandNumber(NumberedOp).first)))) |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 1806 | ++NumberedOp; |
| 1807 | |
| 1808 | OpIdx = NumberedOp++; |
| 1809 | |
| 1810 | // OpIdx now holds the ordered operand number of Vals[i]. |
| 1811 | std::pair<unsigned, unsigned> SO = |
| 1812 | CGI.Operands.getSubOperandNumber(OpIdx); |
| 1813 | const std::string &Name = CGI.Operands[SO.first].Name; |
| 1814 | |
| 1815 | DEBUG(dbgs() << "Numbered operand mapping for " << Def.getName() << ": " << |
| 1816 | Name << "(" << SO.first << ", " << SO.second << ") => " << |
| 1817 | Vals[i].getName() << "\n"); |
| 1818 | |
| 1819 | std::string Decoder = ""; |
| 1820 | Record *TypeRecord = CGI.Operands[SO.first].Rec; |
| 1821 | |
| 1822 | RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod"); |
| 1823 | StringInit *String = DecoderString ? |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 1824 | dyn_cast<StringInit>(DecoderString->getValue()) : nullptr; |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 1825 | if (String && String->getValue() != "") |
| 1826 | Decoder = String->getValue(); |
| 1827 | |
| 1828 | if (Decoder == "" && |
| 1829 | CGI.Operands[SO.first].MIOperandInfo && |
| 1830 | CGI.Operands[SO.first].MIOperandInfo->getNumArgs()) { |
| 1831 | Init *Arg = CGI.Operands[SO.first].MIOperandInfo-> |
| 1832 | getArg(SO.second); |
| 1833 | if (TypedInit *TI = cast<TypedInit>(Arg)) { |
| 1834 | RecordRecTy *Type = cast<RecordRecTy>(TI->getType()); |
| 1835 | TypeRecord = Type->getRecord(); |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | bool isReg = false; |
| 1840 | if (TypeRecord->isSubClassOf("RegisterOperand")) |
| 1841 | TypeRecord = TypeRecord->getValueAsDef("RegClass"); |
| 1842 | if (TypeRecord->isSubClassOf("RegisterClass")) { |
| 1843 | Decoder = "Decode" + TypeRecord->getName() + "RegisterClass"; |
| 1844 | isReg = true; |
| 1845 | } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) { |
| 1846 | Decoder = "DecodePointerLikeRegClass" + |
| 1847 | utostr(TypeRecord->getValueAsInt("RegClassKind")); |
| 1848 | isReg = true; |
| 1849 | } |
| 1850 | |
| 1851 | DecoderString = TypeRecord->getValue("DecoderMethod"); |
| 1852 | String = DecoderString ? |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 1853 | dyn_cast<StringInit>(DecoderString->getValue()) : nullptr; |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 1854 | if (!isReg && String && String->getValue() != "") |
| 1855 | Decoder = String->getValue(); |
| 1856 | |
| 1857 | OperandInfo OpInfo(Decoder); |
| 1858 | OpInfo.addField(bitStart, bitWidth, 0); |
| 1859 | |
| 1860 | NumberedInsnOperands[Name].push_back(OpInfo); |
| 1861 | |
| 1862 | // FIXME: For complex operands with custom decoders we can't handle tied |
| 1863 | // sub-operands automatically. Skip those here and assume that this is |
| 1864 | // fixed up elsewhere. |
| 1865 | if (CGI.Operands[SO.first].MIOperandInfo && |
| 1866 | CGI.Operands[SO.first].MIOperandInfo->getNumArgs() > 1 && |
| 1867 | String && String->getValue() != "") |
| 1868 | NumberedInsnOperandsNoTie.insert(Name); |
Owen Anderson | cb32ce2 | 2011-07-29 18:28:52 +0000 | [diff] [blame] | 1869 | } |
Owen Anderson | 53562d0 | 2011-07-28 23:56:20 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1872 | // For each operand, see if we can figure out where it is encoded. |
Craig Topper | 501d95c | 2012-03-16 06:52:56 +0000 | [diff] [blame] | 1873 | for (std::vector<std::pair<Init*, std::string> >::const_iterator |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1874 | NI = InOutOperands.begin(), NE = InOutOperands.end(); NI != NE; ++NI) { |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 1875 | if (!NumberedInsnOperands[NI->second].empty()) { |
| 1876 | InsnOperands.insert(InsnOperands.end(), |
| 1877 | NumberedInsnOperands[NI->second].begin(), |
| 1878 | NumberedInsnOperands[NI->second].end()); |
| 1879 | continue; |
| 1880 | } else if (!NumberedInsnOperands[TiedNames[NI->second]].empty()) { |
| 1881 | if (!NumberedInsnOperandsNoTie.count(TiedNames[NI->second])) { |
| 1882 | // Figure out to which (sub)operand we're tied. |
| 1883 | unsigned i = CGI.Operands.getOperandNamed(TiedNames[NI->second]); |
| 1884 | int tiedTo = CGI.Operands[i].getTiedRegister(); |
| 1885 | if (tiedTo == -1) { |
| 1886 | i = CGI.Operands.getOperandNamed(NI->second); |
| 1887 | tiedTo = CGI.Operands[i].getTiedRegister(); |
| 1888 | } |
| 1889 | |
| 1890 | if (tiedTo != -1) { |
| 1891 | std::pair<unsigned, unsigned> SO = |
| 1892 | CGI.Operands.getSubOperandNumber(tiedTo); |
| 1893 | |
| 1894 | InsnOperands.push_back(NumberedInsnOperands[TiedNames[NI->second]] |
| 1895 | [SO.second]); |
| 1896 | } |
| 1897 | } |
| 1898 | continue; |
| 1899 | } |
| 1900 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1901 | std::string Decoder = ""; |
| 1902 | |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1903 | // At this point, we can locate the field, but we need to know how to |
| 1904 | // interpret it. As a first step, require the target to provide callbacks |
| 1905 | // for decoding register classes. |
| 1906 | // FIXME: This need to be extended to handle instructions with custom |
| 1907 | // decoder methods, and operands with (simple) MIOperandInfo's. |
Sean Silva | 88eb8dd | 2012-10-10 20:24:47 +0000 | [diff] [blame] | 1908 | TypedInit *TI = cast<TypedInit>(NI->first); |
| 1909 | RecordRecTy *Type = cast<RecordRecTy>(TI->getType()); |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1910 | Record *TypeRecord = Type->getRecord(); |
| 1911 | bool isReg = false; |
| 1912 | if (TypeRecord->isSubClassOf("RegisterOperand")) |
| 1913 | TypeRecord = TypeRecord->getValueAsDef("RegClass"); |
| 1914 | if (TypeRecord->isSubClassOf("RegisterClass")) { |
| 1915 | Decoder = "Decode" + TypeRecord->getName() + "RegisterClass"; |
| 1916 | isReg = true; |
Hal Finkel | 9d95e8d | 2013-12-19 14:58:22 +0000 | [diff] [blame] | 1917 | } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) { |
| 1918 | Decoder = "DecodePointerLikeRegClass" + |
| 1919 | utostr(TypeRecord->getValueAsInt("RegClassKind")); |
| 1920 | isReg = true; |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod"); |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1924 | StringInit *String = DecoderString ? |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 1925 | dyn_cast<StringInit>(DecoderString->getValue()) : nullptr; |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1926 | if (!isReg && String && String->getValue() != "") |
| 1927 | Decoder = String->getValue(); |
| 1928 | |
| 1929 | OperandInfo OpInfo(Decoder); |
| 1930 | unsigned Base = ~0U; |
| 1931 | unsigned Width = 0; |
| 1932 | unsigned Offset = 0; |
| 1933 | |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1934 | for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) { |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 1935 | VarInit *Var = nullptr; |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1936 | VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi)); |
Owen Anderson | 3022d67 | 2011-08-01 22:45:43 +0000 | [diff] [blame] | 1937 | if (BI) |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1938 | Var = dyn_cast<VarInit>(BI->getBitVar()); |
Owen Anderson | 3022d67 | 2011-08-01 22:45:43 +0000 | [diff] [blame] | 1939 | else |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1940 | Var = dyn_cast<VarInit>(Bits.getBit(bi)); |
Owen Anderson | 3022d67 | 2011-08-01 22:45:43 +0000 | [diff] [blame] | 1941 | |
| 1942 | if (!Var) { |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1943 | if (Base != ~0U) { |
| 1944 | OpInfo.addField(Base, Width, Offset); |
| 1945 | Base = ~0U; |
| 1946 | Width = 0; |
| 1947 | Offset = 0; |
| 1948 | } |
| 1949 | continue; |
| 1950 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1951 | |
Owen Anderson | 53562d0 | 2011-07-28 23:56:20 +0000 | [diff] [blame] | 1952 | if (Var->getName() != NI->second && |
Owen Anderson | cb32ce2 | 2011-07-29 18:28:52 +0000 | [diff] [blame] | 1953 | Var->getName() != TiedNames[NI->second]) { |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1954 | if (Base != ~0U) { |
| 1955 | OpInfo.addField(Base, Width, Offset); |
| 1956 | Base = ~0U; |
| 1957 | Width = 0; |
| 1958 | Offset = 0; |
| 1959 | } |
| 1960 | continue; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1963 | if (Base == ~0U) { |
| 1964 | Base = bi; |
| 1965 | Width = 1; |
Owen Anderson | 3022d67 | 2011-08-01 22:45:43 +0000 | [diff] [blame] | 1966 | Offset = BI ? BI->getBitNum() : 0; |
| 1967 | } else if (BI && BI->getBitNum() != Offset + Width) { |
Owen Anderson | e08f5b5 | 2011-07-29 23:01:18 +0000 | [diff] [blame] | 1968 | OpInfo.addField(Base, Width, Offset); |
| 1969 | Base = bi; |
| 1970 | Width = 1; |
| 1971 | Offset = BI->getBitNum(); |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1972 | } else { |
| 1973 | ++Width; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1974 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Owen Anderson | e359165 | 2011-07-28 21:54:31 +0000 | [diff] [blame] | 1977 | if (Base != ~0U) |
| 1978 | OpInfo.addField(Base, Width, Offset); |
| 1979 | |
| 1980 | if (OpInfo.numFields() > 0) |
| 1981 | InsnOperands.push_back(OpInfo); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | Operands[Opc] = InsnOperands; |
| 1985 | |
| 1986 | |
| 1987 | #if 0 |
| 1988 | DEBUG({ |
| 1989 | // Dumps the instruction encoding bits. |
| 1990 | dumpBits(errs(), Bits); |
| 1991 | |
| 1992 | errs() << '\n'; |
| 1993 | |
| 1994 | // Dumps the list of operand info. |
| 1995 | for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) { |
| 1996 | const CGIOperandList::OperandInfo &Info = CGI.Operands[i]; |
| 1997 | const std::string &OperandName = Info.Name; |
| 1998 | const Record &OperandDef = *Info.Rec; |
| 1999 | |
| 2000 | errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n"; |
| 2001 | } |
| 2002 | }); |
| 2003 | #endif |
| 2004 | |
| 2005 | return true; |
| 2006 | } |
| 2007 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2008 | // emitFieldFromInstruction - Emit the templated helper function |
| 2009 | // fieldFromInstruction(). |
| 2010 | static void emitFieldFromInstruction(formatted_raw_ostream &OS) { |
| 2011 | OS << "// Helper function for extracting fields from encoded instructions.\n" |
| 2012 | << "template<typename InsnType>\n" |
| 2013 | << "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n" |
| 2014 | << " unsigned numBits) {\n" |
| 2015 | << " assert(startBit + numBits <= (sizeof(InsnType)*8) &&\n" |
| 2016 | << " \"Instruction field out of bounds!\");\n" |
| 2017 | << " InsnType fieldMask;\n" |
| 2018 | << " if (numBits == sizeof(InsnType)*8)\n" |
| 2019 | << " fieldMask = (InsnType)(-1LL);\n" |
| 2020 | << " else\n" |
NAKAMURA Takumi | bf99a42 | 2012-12-26 06:43:14 +0000 | [diff] [blame] | 2021 | << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n" |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2022 | << " return (insn & fieldMask) >> startBit;\n" |
| 2023 | << "}\n\n"; |
| 2024 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 2025 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2026 | // emitDecodeInstruction - Emit the templated helper function |
| 2027 | // decodeInstruction(). |
| 2028 | static void emitDecodeInstruction(formatted_raw_ostream &OS) { |
| 2029 | OS << "template<typename InsnType>\n" |
| 2030 | << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,\n" |
| 2031 | << " InsnType insn, uint64_t Address,\n" |
| 2032 | << " const void *DisAsm,\n" |
| 2033 | << " const MCSubtargetInfo &STI) {\n" |
| 2034 | << " uint64_t Bits = STI.getFeatureBits();\n" |
| 2035 | << "\n" |
| 2036 | << " const uint8_t *Ptr = DecodeTable;\n" |
Jim Grosbach | 4c36349 | 2012-09-17 18:00:53 +0000 | [diff] [blame] | 2037 | << " uint32_t CurFieldValue = 0;\n" |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2038 | << " DecodeStatus S = MCDisassembler::Success;\n" |
| 2039 | << " for (;;) {\n" |
| 2040 | << " ptrdiff_t Loc = Ptr - DecodeTable;\n" |
| 2041 | << " switch (*Ptr) {\n" |
| 2042 | << " default:\n" |
| 2043 | << " errs() << Loc << \": Unexpected decode table opcode!\\n\";\n" |
| 2044 | << " return MCDisassembler::Fail;\n" |
| 2045 | << " case MCD::OPC_ExtractField: {\n" |
| 2046 | << " unsigned Start = *++Ptr;\n" |
| 2047 | << " unsigned Len = *++Ptr;\n" |
| 2048 | << " ++Ptr;\n" |
| 2049 | << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\n" |
| 2050 | << " DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << \", \"\n" |
| 2051 | << " << Len << \"): \" << CurFieldValue << \"\\n\");\n" |
| 2052 | << " break;\n" |
| 2053 | << " }\n" |
| 2054 | << " case MCD::OPC_FilterValue: {\n" |
| 2055 | << " // Decode the field value.\n" |
| 2056 | << " unsigned Len;\n" |
| 2057 | << " InsnType Val = decodeULEB128(++Ptr, &Len);\n" |
| 2058 | << " Ptr += Len;\n" |
| 2059 | << " // NumToSkip is a plain 16-bit integer.\n" |
| 2060 | << " unsigned NumToSkip = *Ptr++;\n" |
| 2061 | << " NumToSkip |= (*Ptr++) << 8;\n" |
| 2062 | << "\n" |
| 2063 | << " // Perform the filter operation.\n" |
| 2064 | << " if (Val != CurFieldValue)\n" |
| 2065 | << " Ptr += NumToSkip;\n" |
| 2066 | << " DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << \", \" << NumToSkip\n" |
| 2067 | << " << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" : \"PASS:\")\n" |
| 2068 | << " << \" continuing at \" << (Ptr - DecodeTable) << \"\\n\");\n" |
| 2069 | << "\n" |
| 2070 | << " break;\n" |
| 2071 | << " }\n" |
| 2072 | << " case MCD::OPC_CheckField: {\n" |
| 2073 | << " unsigned Start = *++Ptr;\n" |
| 2074 | << " unsigned Len = *++Ptr;\n" |
| 2075 | << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\n" |
| 2076 | << " // Decode the field value.\n" |
| 2077 | << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\n" |
| 2078 | << " Ptr += Len;\n" |
| 2079 | << " // NumToSkip is a plain 16-bit integer.\n" |
| 2080 | << " unsigned NumToSkip = *Ptr++;\n" |
| 2081 | << " NumToSkip |= (*Ptr++) << 8;\n" |
| 2082 | << "\n" |
| 2083 | << " // If the actual and expected values don't match, skip.\n" |
| 2084 | << " if (ExpectedValue != FieldValue)\n" |
| 2085 | << " Ptr += NumToSkip;\n" |
| 2086 | << " DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << \", \"\n" |
| 2087 | << " << Len << \", \" << ExpectedValue << \", \" << NumToSkip\n" |
| 2088 | << " << \"): FieldValue = \" << FieldValue << \", ExpectedValue = \"\n" |
| 2089 | << " << ExpectedValue << \": \"\n" |
| 2090 | << " << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : \"FAIL\\n\"));\n" |
| 2091 | << " break;\n" |
| 2092 | << " }\n" |
| 2093 | << " case MCD::OPC_CheckPredicate: {\n" |
| 2094 | << " unsigned Len;\n" |
| 2095 | << " // Decode the Predicate Index value.\n" |
| 2096 | << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\n" |
| 2097 | << " Ptr += Len;\n" |
| 2098 | << " // NumToSkip is a plain 16-bit integer.\n" |
| 2099 | << " unsigned NumToSkip = *Ptr++;\n" |
| 2100 | << " NumToSkip |= (*Ptr++) << 8;\n" |
| 2101 | << " // Check the predicate.\n" |
| 2102 | << " bool Pred;\n" |
| 2103 | << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n" |
| 2104 | << " Ptr += NumToSkip;\n" |
| 2105 | << " (void)Pred;\n" |
| 2106 | << " DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx << \"): \"\n" |
| 2107 | << " << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n" |
| 2108 | << "\n" |
| 2109 | << " break;\n" |
| 2110 | << " }\n" |
| 2111 | << " case MCD::OPC_Decode: {\n" |
| 2112 | << " unsigned Len;\n" |
| 2113 | << " // Decode the Opcode value.\n" |
| 2114 | << " unsigned Opc = decodeULEB128(++Ptr, &Len);\n" |
| 2115 | << " Ptr += Len;\n" |
| 2116 | << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n" |
| 2117 | << " Ptr += Len;\n" |
| 2118 | << " DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n" |
| 2119 | << " << \", using decoder \" << DecodeIdx << \"\\n\" );\n" |
| 2120 | << " DEBUG(dbgs() << \"----- DECODE SUCCESSFUL -----\\n\");\n" |
| 2121 | << "\n" |
| 2122 | << " MI.setOpcode(Opc);\n" |
Benjamin Kramer | 26b568d | 2012-08-15 10:26:44 +0000 | [diff] [blame] | 2123 | << " return decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm);\n" |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2124 | << " }\n" |
| 2125 | << " case MCD::OPC_SoftFail: {\n" |
| 2126 | << " // Decode the mask values.\n" |
| 2127 | << " unsigned Len;\n" |
| 2128 | << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\n" |
| 2129 | << " Ptr += Len;\n" |
| 2130 | << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\n" |
| 2131 | << " Ptr += Len;\n" |
| 2132 | << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\n" |
| 2133 | << " if (Fail)\n" |
| 2134 | << " S = MCDisassembler::SoftFail;\n" |
| 2135 | << " DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? \"FAIL\\n\":\"PASS\\n\"));\n" |
| 2136 | << " break;\n" |
| 2137 | << " }\n" |
| 2138 | << " case MCD::OPC_Fail: {\n" |
| 2139 | << " DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n" |
| 2140 | << " return MCDisassembler::Fail;\n" |
| 2141 | << " }\n" |
| 2142 | << " }\n" |
| 2143 | << " }\n" |
| 2144 | << " llvm_unreachable(\"bogosity detected in disassembler state machine!\");\n" |
| 2145 | << "}\n\n"; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | // Emits disassembler code for instruction decoding. |
Craig Topper | 82d0d5f | 2012-03-16 01:19:24 +0000 | [diff] [blame] | 2149 | void FixedLenDecoderEmitter::run(raw_ostream &o) { |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2150 | formatted_raw_ostream OS(o); |
| 2151 | OS << "#include \"llvm/MC/MCInst.h\"\n"; |
| 2152 | OS << "#include \"llvm/Support/Debug.h\"\n"; |
| 2153 | OS << "#include \"llvm/Support/DataTypes.h\"\n"; |
| 2154 | OS << "#include \"llvm/Support/LEB128.h\"\n"; |
| 2155 | OS << "#include \"llvm/Support/raw_ostream.h\"\n"; |
| 2156 | OS << "#include <assert.h>\n"; |
| 2157 | OS << '\n'; |
| 2158 | OS << "namespace llvm {\n\n"; |
| 2159 | |
| 2160 | emitFieldFromInstruction(OS); |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 2161 | |
Hal Finkel | 81e6fcc | 2013-12-17 22:37:50 +0000 | [diff] [blame] | 2162 | Target.reverseBitsForLittleEndianEncoding(); |
| 2163 | |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 2164 | // Parameterize the decoders based on namespace and instruction width. |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2165 | NumberedInstructions = &Target.getInstructionsByEnumValue(); |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 2166 | std::map<std::pair<std::string, unsigned>, |
| 2167 | std::vector<unsigned> > OpcMap; |
| 2168 | std::map<unsigned, std::vector<OperandInfo> > Operands; |
| 2169 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2170 | for (unsigned i = 0; i < NumberedInstructions->size(); ++i) { |
| 2171 | const CodeGenInstruction *Inst = NumberedInstructions->at(i); |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 2172 | const Record *Def = Inst->TheDef; |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 2173 | unsigned Size = Def->getValueAsInt("Size"); |
| 2174 | if (Def->getValueAsString("Namespace") == "TargetOpcode" || |
| 2175 | Def->getValueAsBit("isPseudo") || |
| 2176 | Def->getValueAsBit("isAsmParserOnly") || |
| 2177 | Def->getValueAsBit("isCodeGenOnly")) |
| 2178 | continue; |
| 2179 | |
| 2180 | std::string DecoderNamespace = Def->getValueAsString("DecoderNamespace"); |
| 2181 | |
| 2182 | if (Size) { |
Hal Finkel | 71b2e20 | 2013-12-19 16:12:53 +0000 | [diff] [blame] | 2183 | if (populateInstruction(Target, *Inst, i, Operands)) { |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 2184 | OpcMap[std::make_pair(DecoderNamespace, Size)].push_back(i); |
| 2185 | } |
| 2186 | } |
| 2187 | } |
| 2188 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2189 | DecoderTableInfo TableInfo; |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 2190 | for (std::map<std::pair<std::string, unsigned>, |
Craig Topper | 48c112b | 2012-03-16 05:58:09 +0000 | [diff] [blame] | 2191 | std::vector<unsigned> >::const_iterator |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 2192 | I = OpcMap.begin(), E = OpcMap.end(); I != E; ++I) { |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 2193 | // Emit the decoder for this namespace+width combination. |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2194 | FilterChooser FC(*NumberedInstructions, I->second, Operands, |
Owen Anderson | a4043c4 | 2011-08-17 17:44:15 +0000 | [diff] [blame] | 2195 | 8*I->first.second, this); |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2196 | |
| 2197 | // The decode table is cleared for each top level decoder function. The |
| 2198 | // predicates and decoders themselves, however, are shared across all |
| 2199 | // decoders to give more opportunities for uniqueing. |
| 2200 | TableInfo.Table.clear(); |
| 2201 | TableInfo.FixupStack.clear(); |
| 2202 | TableInfo.Table.reserve(16384); |
| 2203 | TableInfo.FixupStack.push_back(FixupList()); |
| 2204 | FC.emitTableEntries(TableInfo); |
| 2205 | // Any NumToSkip fixups in the top level scope can resolve to the |
| 2206 | // OPC_Fail at the end of the table. |
| 2207 | assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!"); |
| 2208 | // Resolve any NumToSkip fixups in the current scope. |
| 2209 | resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(), |
| 2210 | TableInfo.Table.size()); |
| 2211 | TableInfo.FixupStack.clear(); |
| 2212 | |
| 2213 | TableInfo.Table.push_back(MCD::OPC_Fail); |
| 2214 | |
| 2215 | // Print the table to the output stream. |
| 2216 | emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), I->first.first); |
| 2217 | OS.flush(); |
Owen Anderson | c78e03c | 2011-07-19 21:06:00 +0000 | [diff] [blame] | 2218 | } |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 2219 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 2220 | // Emit the predicate function. |
| 2221 | emitPredicateFunction(OS, TableInfo.Predicates, 0); |
| 2222 | |
| 2223 | // Emit the decoder function. |
| 2224 | emitDecoderFunction(OS, TableInfo.Decoders, 0); |
| 2225 | |
| 2226 | // Emit the main entry point for the decoder, decodeInstruction(). |
| 2227 | emitDecodeInstruction(OS); |
| 2228 | |
| 2229 | OS << "\n} // End llvm namespace\n"; |
Owen Anderson | 4e81890 | 2011-02-18 21:51:29 +0000 | [diff] [blame] | 2230 | } |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 2231 | |
| 2232 | namespace llvm { |
| 2233 | |
| 2234 | void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS, |
| 2235 | std::string PredicateNamespace, |
| 2236 | std::string GPrefix, |
| 2237 | std::string GPostfix, |
| 2238 | std::string ROK, |
| 2239 | std::string RFail, |
| 2240 | std::string L) { |
| 2241 | FixedLenDecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix, |
| 2242 | ROK, RFail, L).run(OS); |
| 2243 | } |
| 2244 | |
| 2245 | } // End llvm namespace |