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