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