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