Daniel Dunbar | 3085b57 | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 1 | //===- AsmMatcherEmitter.cpp - Generate an assembly matcher ---------------===// |
| 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 |
Daniel Dunbar | 3085b57 | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This tablegen backend emits a target specifier matcher for converting parsed |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 10 | // assembly operands in the MCInst structures. It also emits a matcher for |
| 11 | // custom operand parsing. |
| 12 | // |
| 13 | // Converting assembly operands into MCInst structures |
| 14 | // --------------------------------------------------- |
Daniel Dunbar | 3085b57 | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 15 | // |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 16 | // The input to the target specific matcher is a list of literal tokens and |
| 17 | // operands. The target specific parser should generally eliminate any syntax |
| 18 | // which is not relevant for matching; for example, comma tokens should have |
| 19 | // already been consumed and eliminated by the parser. Most instructions will |
| 20 | // end up with a single literal token (the instruction name) and some number of |
| 21 | // operands. |
| 22 | // |
| 23 | // Some example inputs, for X86: |
| 24 | // 'addl' (immediate ...) (register ...) |
| 25 | // 'add' (immediate ...) (memory ...) |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 26 | // 'call' '*' %epc |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 27 | // |
| 28 | // The assembly matcher is responsible for converting this input into a precise |
| 29 | // machine instruction (i.e., an instruction with a well defined encoding). This |
| 30 | // mapping has several properties which complicate matching: |
| 31 | // |
| 32 | // - It may be ambiguous; many architectures can legally encode particular |
| 33 | // variants of an instruction in different ways (for example, using a smaller |
| 34 | // encoding for small immediates). Such ambiguities should never be |
| 35 | // arbitrarily resolved by the assembler, the assembler is always responsible |
| 36 | // for choosing the "best" available instruction. |
| 37 | // |
| 38 | // - It may depend on the subtarget or the assembler context. Instructions |
| 39 | // which are invalid for the current mode, but otherwise unambiguous (e.g., |
| 40 | // an SSE instruction in a file being assembled for i486) should be accepted |
| 41 | // and rejected by the assembler front end. However, if the proper encoding |
| 42 | // for an instruction is dependent on the assembler context then the matcher |
| 43 | // is responsible for selecting the correct machine instruction for the |
| 44 | // current mode. |
| 45 | // |
| 46 | // The core matching algorithm attempts to exploit the regularity in most |
| 47 | // instruction sets to quickly determine the set of possibly matching |
| 48 | // instructions, and the simplify the generated code. Additionally, this helps |
| 49 | // to ensure that the ambiguities are intentionally resolved by the user. |
| 50 | // |
| 51 | // The matching is divided into two distinct phases: |
| 52 | // |
| 53 | // 1. Classification: Each operand is mapped to the unique set which (a) |
| 54 | // contains it, and (b) is the largest such subset for which a single |
| 55 | // instruction could match all members. |
| 56 | // |
| 57 | // For register classes, we can generate these subgroups automatically. For |
| 58 | // arbitrary operands, we expect the user to define the classes and their |
| 59 | // relations to one another (for example, 8-bit signed immediates as a |
| 60 | // subset of 32-bit immediates). |
| 61 | // |
| 62 | // By partitioning the operands in this way, we guarantee that for any |
| 63 | // tuple of classes, any single instruction must match either all or none |
| 64 | // of the sets of operands which could classify to that tuple. |
| 65 | // |
| 66 | // In addition, the subset relation amongst classes induces a partial order |
| 67 | // on such tuples, which we use to resolve ambiguities. |
| 68 | // |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 69 | // 2. The input can now be treated as a tuple of classes (static tokens are |
| 70 | // simple singleton sets). Each such tuple should generally map to a single |
| 71 | // instruction (we currently ignore cases where this isn't true, whee!!!), |
| 72 | // which we can emit a simple matcher for. |
| 73 | // |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 74 | // Custom Operand Parsing |
| 75 | // ---------------------- |
| 76 | // |
| 77 | // Some targets need a custom way to parse operands, some specific instructions |
| 78 | // can contain arguments that can represent processor flags and other kinds of |
Craig Topper | aae8fb8 | 2012-09-18 01:13:36 +0000 | [diff] [blame] | 79 | // identifiers that need to be mapped to specific values in the final encoded |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 80 | // instructions. The target specific custom operand parsing works in the |
| 81 | // following way: |
| 82 | // |
| 83 | // 1. A operand match table is built, each entry contains a mnemonic, an |
| 84 | // operand class, a mask for all operand positions for that same |
| 85 | // class/mnemonic and target features to be checked while trying to match. |
| 86 | // |
| 87 | // 2. The operand matcher will try every possible entry with the same |
| 88 | // mnemonic and will check if the target feature for this mnemonic also |
| 89 | // matches. After that, if the operand to be matched has its index |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 90 | // present in the mask, a successful match occurs. Otherwise, fallback |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 91 | // to the regular operand parsing. |
| 92 | // |
| 93 | // 3. For a match success, each operand class that has a 'ParserMethod' |
| 94 | // becomes part of a switch from where the custom method is called. |
| 95 | // |
Daniel Dunbar | 3085b57 | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 96 | //===----------------------------------------------------------------------===// |
| 97 | |
Daniel Dunbar | 3085b57 | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 98 | #include "CodeGenTarget.h" |
Daniel Sanders | ea6ef3d | 2016-11-15 09:51:02 +0000 | [diff] [blame] | 99 | #include "SubtargetFeatureInfo.h" |
Daniel Sanders | ca89f3a | 2016-11-19 12:21:34 +0000 | [diff] [blame] | 100 | #include "Types.h" |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 101 | #include "llvm/ADT/CachedHashString.h" |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 102 | #include "llvm/ADT/PointerUnion.h" |
Chandler Carruth | 91d19d8 | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 103 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | f7a01e9 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 104 | #include "llvm/ADT/SmallPtrSet.h" |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 105 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 106 | #include "llvm/ADT/StringExtras.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 107 | #include "llvm/Config/llvm-config.h" |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 108 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 109 | #include "llvm/Support/Debug.h" |
Craig Topper | c4965bc | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 110 | #include "llvm/Support/ErrorHandling.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 111 | #include "llvm/TableGen/Error.h" |
| 112 | #include "llvm/TableGen/Record.h" |
Douglas Gregor | 12c1cd3 | 2012-05-02 17:32:48 +0000 | [diff] [blame] | 113 | #include "llvm/TableGen/StringMatcher.h" |
Craig Topper | 3e1d5da | 2013-08-29 05:09:55 +0000 | [diff] [blame] | 114 | #include "llvm/TableGen/StringToOffsetTable.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 115 | #include "llvm/TableGen/TableGenBackend.h" |
| 116 | #include <cassert> |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 117 | #include <cctype> |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 118 | #include <forward_list> |
Daniel Dunbar | 7133028 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 119 | #include <map> |
| 120 | #include <set> |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | 3085b57 | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 122 | using namespace llvm; |
| 123 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 124 | #define DEBUG_TYPE "asm-matcher-emitter" |
| 125 | |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 126 | cl::OptionCategory AsmMatcherEmitterCat("Options for -gen-asm-matcher"); |
| 127 | |
Daniel Dunbar | 15b8037 | 2009-08-07 20:33:39 +0000 | [diff] [blame] | 128 | static cl::opt<std::string> |
Daniel Sanders | 0848b23 | 2017-03-27 13:15:13 +0000 | [diff] [blame] | 129 | MatchPrefix("match-prefix", cl::init(""), |
| 130 | cl::desc("Only match instructions with the given prefix"), |
| 131 | cl::cat(AsmMatcherEmitterCat)); |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 132 | |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 133 | namespace { |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 134 | class AsmMatcherInfo; |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 135 | |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 136 | // Register sets are used as keys in some second-order sets TableGen creates |
| 137 | // when generating its data structures. This means that the order of two |
| 138 | // RegisterSets can be seen in the outputted AsmMatcher tables occasionally, and |
| 139 | // can even affect compiler output (at least seen in diagnostics produced when |
| 140 | // all matches fail). So we use a type that sorts them consistently. |
| 141 | typedef std::set<Record*, LessRecordByID> RegisterSet; |
| 142 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 143 | class AsmMatcherEmitter { |
| 144 | RecordKeeper &Records; |
| 145 | public: |
| 146 | AsmMatcherEmitter(RecordKeeper &R) : Records(R) {} |
| 147 | |
| 148 | void run(raw_ostream &o); |
| 149 | }; |
| 150 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 151 | /// ClassInfo - Helper class for storing the information about a particular |
| 152 | /// class of operands which can be matched. |
| 153 | struct ClassInfo { |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 154 | enum ClassInfoKind { |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 155 | /// Invalid kind, for use as a sentinel value. |
| 156 | Invalid = 0, |
| 157 | |
| 158 | /// The class for a particular token. |
| 159 | Token, |
| 160 | |
| 161 | /// The (first) register class, subsequent register classes are |
| 162 | /// RegisterClass0+1, and so on. |
| 163 | RegisterClass0, |
| 164 | |
| 165 | /// The (first) user defined class, subsequent user defined classes are |
| 166 | /// UserClass0+1, and so on. |
| 167 | UserClass0 = 1<<16 |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | /// Kind - The class kind, which is either a predefined kind, or (UserClass0 + |
| 171 | /// N) for the Nth user defined class. |
| 172 | unsigned Kind; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 173 | |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 174 | /// SuperClasses - The super classes of this class. Note that for simplicities |
| 175 | /// sake user operands only record their immediate super class, while register |
| 176 | /// operands include all superclasses. |
| 177 | std::vector<ClassInfo*> SuperClasses; |
Daniel Dunbar | 8e33cb2 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 178 | |
Daniel Dunbar | c32aa06 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 179 | /// Name - The full class name, suitable for use in an enum. |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 180 | std::string Name; |
| 181 | |
Daniel Dunbar | c32aa06 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 182 | /// ClassName - The unadorned generic name for this class (e.g., Token). |
| 183 | std::string ClassName; |
| 184 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 185 | /// ValueName - The name of the value this class represents; for a token this |
| 186 | /// is the literal token string, for an operand it is the TableGen class (or |
| 187 | /// empty if this is a derived class). |
| 188 | std::string ValueName; |
| 189 | |
| 190 | /// PredicateMethod - The name of the operand method to test whether the |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 191 | /// operand matches this class; this is not valid for Token or register kinds. |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 192 | std::string PredicateMethod; |
| 193 | |
| 194 | /// RenderMethod - The name of the operand method to add this operand to an |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 195 | /// MCInst; this is not valid for Token or register kinds. |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 196 | std::string RenderMethod; |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 197 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 198 | /// ParserMethod - The name of the operand method to do a target specific |
| 199 | /// parsing on the operand. |
| 200 | std::string ParserMethod; |
| 201 | |
Eric Christopher | 650c8f2 | 2014-05-20 17:11:11 +0000 | [diff] [blame] | 202 | /// For register classes: the records for all the registers in this class. |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 203 | RegisterSet Registers; |
Daniel Dunbar | 34c8791 | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 204 | |
Eric Christopher | 650c8f2 | 2014-05-20 17:11:11 +0000 | [diff] [blame] | 205 | /// For custom match classes: the diagnostic kind for when the predicate fails. |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 206 | std::string DiagnosticType; |
Tom Stellard | b9f235e | 2016-02-05 19:59:33 +0000 | [diff] [blame] | 207 | |
Oliver Stannard | 41dfac3 | 2017-10-03 14:34:57 +0000 | [diff] [blame] | 208 | /// For custom match classes: the diagnostic string for when the predicate fails. |
| 209 | std::string DiagnosticString; |
| 210 | |
Tom Stellard | b9f235e | 2016-02-05 19:59:33 +0000 | [diff] [blame] | 211 | /// Is this operand optional and not always required. |
| 212 | bool IsOptional; |
| 213 | |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 214 | /// DefaultMethod - The name of the method that returns the default operand |
| 215 | /// for optional operand |
| 216 | std::string DefaultMethod; |
| 217 | |
Daniel Dunbar | 34c8791 | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 218 | public: |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 219 | /// isRegisterClass() - Check if this is a register class. |
| 220 | bool isRegisterClass() const { |
| 221 | return Kind >= RegisterClass0 && Kind < UserClass0; |
| 222 | } |
| 223 | |
Daniel Dunbar | 8e33cb2 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 224 | /// isUserClass() - Check if this is a user defined class. |
| 225 | bool isUserClass() const { |
| 226 | return Kind >= UserClass0; |
| 227 | } |
| 228 | |
Dmitri Gribenko | 8d30240 | 2012-09-15 20:22:05 +0000 | [diff] [blame] | 229 | /// isRelatedTo - Check whether this class is "related" to \p RHS. Classes |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 230 | /// are related if they are in the same class hierarchy. |
| 231 | bool isRelatedTo(const ClassInfo &RHS) const { |
| 232 | // Tokens are only related to tokens. |
| 233 | if (Kind == Token || RHS.Kind == Token) |
| 234 | return Kind == Token && RHS.Kind == Token; |
| 235 | |
Daniel Dunbar | 34c8791 | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 236 | // Registers classes are only related to registers classes, and only if |
| 237 | // their intersection is non-empty. |
| 238 | if (isRegisterClass() || RHS.isRegisterClass()) { |
| 239 | if (!isRegisterClass() || !RHS.isRegisterClass()) |
| 240 | return false; |
| 241 | |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 242 | RegisterSet Tmp; |
| 243 | std::insert_iterator<RegisterSet> II(Tmp, Tmp.begin()); |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 244 | std::set_intersection(Registers.begin(), Registers.end(), |
Daniel Dunbar | 34c8791 | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 245 | RHS.Registers.begin(), RHS.Registers.end(), |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 246 | II, LessRecordByID()); |
Daniel Dunbar | 34c8791 | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 247 | |
| 248 | return !Tmp.empty(); |
| 249 | } |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 250 | |
| 251 | // Otherwise we have two users operands; they are related if they are in the |
| 252 | // same class hierarchy. |
Daniel Dunbar | 34c8791 | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 253 | // |
| 254 | // FIXME: This is an oversimplification, they should only be related if they |
| 255 | // intersect, however we don't have that information. |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 256 | assert(isUserClass() && RHS.isUserClass() && "Unexpected class!"); |
| 257 | const ClassInfo *Root = this; |
| 258 | while (!Root->SuperClasses.empty()) |
| 259 | Root = Root->SuperClasses.front(); |
| 260 | |
Daniel Dunbar | 34c8791 | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 261 | const ClassInfo *RHSRoot = &RHS; |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 262 | while (!RHSRoot->SuperClasses.empty()) |
| 263 | RHSRoot = RHSRoot->SuperClasses.front(); |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 264 | |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 265 | return Root == RHSRoot; |
| 266 | } |
| 267 | |
Dmitri Gribenko | 8d30240 | 2012-09-15 20:22:05 +0000 | [diff] [blame] | 268 | /// isSubsetOf - Test whether this class is a subset of \p RHS. |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 269 | bool isSubsetOf(const ClassInfo &RHS) const { |
| 270 | // This is a subset of RHS if it is the same class... |
| 271 | if (this == &RHS) |
| 272 | return true; |
| 273 | |
| 274 | // ... or if any of its super classes are a subset of RHS. |
Marcello Maggioni | 218b6a2 | 2018-07-13 16:36:14 +0000 | [diff] [blame] | 275 | SmallVector<const ClassInfo *, 16> Worklist(SuperClasses.begin(), |
| 276 | SuperClasses.end()); |
| 277 | SmallPtrSet<const ClassInfo *, 16> Visited; |
| 278 | while (!Worklist.empty()) { |
| 279 | auto *CI = Worklist.pop_back_val(); |
| 280 | if (CI == &RHS) |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 281 | return true; |
Marcello Maggioni | 218b6a2 | 2018-07-13 16:36:14 +0000 | [diff] [blame] | 282 | for (auto *Super : CI->SuperClasses) |
| 283 | if (Visited.insert(Super).second) |
| 284 | Worklist.push_back(Super); |
| 285 | } |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 286 | |
| 287 | return false; |
Daniel Dunbar | 8e33cb2 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Oliver Stannard | 7772f02 | 2016-01-25 10:20:19 +0000 | [diff] [blame] | 290 | int getTreeDepth() const { |
| 291 | int Depth = 0; |
| 292 | const ClassInfo *Root = this; |
| 293 | while (!Root->SuperClasses.empty()) { |
| 294 | Depth++; |
| 295 | Root = Root->SuperClasses.front(); |
| 296 | } |
| 297 | return Depth; |
| 298 | } |
| 299 | |
| 300 | const ClassInfo *findRoot() const { |
| 301 | const ClassInfo *Root = this; |
| 302 | while (!Root->SuperClasses.empty()) |
| 303 | Root = Root->SuperClasses.front(); |
| 304 | return Root; |
| 305 | } |
| 306 | |
| 307 | /// Compare two classes. This does not produce a total ordering, but does |
| 308 | /// guarantee that subclasses are sorted before their parents, and that the |
| 309 | /// ordering is transitive. |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 310 | bool operator<(const ClassInfo &RHS) const { |
Daniel Dunbar | 97ac3af | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 311 | if (this == &RHS) |
| 312 | return false; |
| 313 | |
Oliver Stannard | 7772f02 | 2016-01-25 10:20:19 +0000 | [diff] [blame] | 314 | // First, enforce the ordering between the three different types of class. |
| 315 | // Tokens sort before registers, which sort before user classes. |
| 316 | if (Kind == Token) { |
| 317 | if (RHS.Kind != Token) |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 318 | return true; |
Oliver Stannard | 7772f02 | 2016-01-25 10:20:19 +0000 | [diff] [blame] | 319 | assert(RHS.Kind == Token); |
| 320 | } else if (isRegisterClass()) { |
| 321 | if (RHS.Kind == Token) |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 322 | return false; |
Oliver Stannard | 7772f02 | 2016-01-25 10:20:19 +0000 | [diff] [blame] | 323 | else if (RHS.isUserClass()) |
| 324 | return true; |
| 325 | assert(RHS.isRegisterClass()); |
| 326 | } else if (isUserClass()) { |
| 327 | if (!RHS.isUserClass()) |
| 328 | return false; |
| 329 | assert(RHS.isUserClass()); |
| 330 | } else { |
| 331 | llvm_unreachable("Unknown ClassInfoKind"); |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 332 | } |
Oliver Stannard | 7772f02 | 2016-01-25 10:20:19 +0000 | [diff] [blame] | 333 | |
| 334 | if (Kind == Token || isUserClass()) { |
| 335 | // Related tokens and user classes get sorted by depth in the inheritence |
| 336 | // tree (so that subclasses are before their parents). |
| 337 | if (isRelatedTo(RHS)) { |
| 338 | if (getTreeDepth() > RHS.getTreeDepth()) |
| 339 | return true; |
| 340 | if (getTreeDepth() < RHS.getTreeDepth()) |
| 341 | return false; |
| 342 | } else { |
| 343 | // Unrelated tokens and user classes are ordered by the name of their |
| 344 | // root nodes, so that there is a consistent ordering between |
| 345 | // unconnected trees. |
| 346 | return findRoot()->ValueName < RHS.findRoot()->ValueName; |
| 347 | } |
| 348 | } else if (isRegisterClass()) { |
| 349 | // For register sets, sort by number of registers. This guarantees that |
| 350 | // a set will always sort before all of it's strict supersets. |
| 351 | if (Registers.size() != RHS.Registers.size()) |
| 352 | return Registers.size() < RHS.Registers.size(); |
| 353 | } else { |
| 354 | llvm_unreachable("Unknown ClassInfoKind"); |
| 355 | } |
| 356 | |
| 357 | // FIXME: We should be able to just return false here, as we only need a |
| 358 | // partial order (we use stable sorts, so this is deterministic) and the |
| 359 | // name of a class shouldn't be significant. However, some of the backends |
| 360 | // accidentally rely on this behaviour, so it will have to stay like this |
| 361 | // until they are fixed. |
| 362 | return ValueName < RHS.ValueName; |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 363 | } |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 364 | }; |
| 365 | |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 366 | class AsmVariantInfo { |
| 367 | public: |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 368 | StringRef RegisterPrefix; |
| 369 | StringRef TokenizingCharacters; |
| 370 | StringRef SeparatorCharacters; |
| 371 | StringRef BreakCharacters; |
| 372 | StringRef Name; |
Craig Topper | c8b5b25 | 2015-12-30 06:00:18 +0000 | [diff] [blame] | 373 | int AsmVariantNo; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 374 | }; |
| 375 | |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 376 | /// MatchableInfo - Helper class for storing the necessary information for an |
| 377 | /// instruction or alias which is capable of being matched. |
| 378 | struct MatchableInfo { |
Chris Lattner | 896cf04 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 379 | struct AsmOperand { |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 380 | /// Token - This is the token that the operand came from. |
| 381 | StringRef Token; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 382 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 383 | /// The unique class instance this operand should match. |
| 384 | ClassInfo *Class; |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 7108dad | 2010-11-04 01:42:59 +0000 | [diff] [blame] | 386 | /// The operand name this is, if anything. |
| 387 | StringRef SrcOpName; |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 388 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 389 | /// The operand name this is, before renaming for tied operands. |
| 390 | StringRef OrigSrcOpName; |
| 391 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 392 | /// The suboperand index within SrcOpName, or -1 for the entire operand. |
| 393 | int SubOpIdx; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 394 | |
Ahmed Bougacha | eb4dbd8 | 2015-05-29 01:03:37 +0000 | [diff] [blame] | 395 | /// Whether the token is "isolated", i.e., it is preceded and followed |
| 396 | /// by separators. |
| 397 | bool IsIsolatedToken; |
| 398 | |
Devang Patel | 6d676e4 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 399 | /// Register record if this token is singleton register. |
| 400 | Record *SingletonReg; |
| 401 | |
Ahmed Bougacha | eb4dbd8 | 2015-05-29 01:03:37 +0000 | [diff] [blame] | 402 | explicit AsmOperand(bool IsIsolatedToken, StringRef T) |
| 403 | : Token(T), Class(nullptr), SubOpIdx(-1), |
| 404 | IsIsolatedToken(IsIsolatedToken), SingletonReg(nullptr) {} |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 405 | }; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 407 | /// ResOperand - This represents a single operand in the result instruction |
| 408 | /// generated by the match. In cases (like addressing modes) where a single |
| 409 | /// assembler operand expands to multiple MCOperands, this represents the |
| 410 | /// single assembler operand, not the MCOperand. |
| 411 | struct ResOperand { |
| 412 | enum { |
| 413 | /// RenderAsmOperand - This represents an operand result that is |
| 414 | /// generated by calling the render method on the assembly operand. The |
| 415 | /// corresponding AsmOperand is specified by AsmOperandNum. |
| 416 | RenderAsmOperand, |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 418 | /// TiedOperand - This represents a result operand that is a duplicate of |
| 419 | /// a previous result operand. |
Chris Lattner | b6f8e82 | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 420 | TiedOperand, |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 421 | |
Chris Lattner | b6f8e82 | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 422 | /// ImmOperand - This represents an immediate value that is dumped into |
| 423 | /// the operand. |
Chris Lattner | 4869d34 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 424 | ImmOperand, |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 4869d34 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 426 | /// RegOperand - This represents a fixed register that is dumped in. |
| 427 | RegOperand |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 428 | } Kind; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 429 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 430 | /// Tuple containing the index of the (earlier) result operand that should |
| 431 | /// be copied from, as well as the indices of the corresponding (parsed) |
| 432 | /// operands in the asm string. |
| 433 | struct TiedOperandsTuple { |
| 434 | unsigned ResOpnd; |
| 435 | unsigned SrcOpnd1Idx; |
| 436 | unsigned SrcOpnd2Idx; |
| 437 | }; |
| 438 | |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 439 | union { |
| 440 | /// This is the operand # in the AsmOperands list that this should be |
| 441 | /// copied from. |
| 442 | unsigned AsmOperandNum; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 443 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 444 | /// Description of tied operands. |
| 445 | TiedOperandsTuple TiedOperands; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 446 | |
Chris Lattner | b6f8e82 | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 447 | /// ImmVal - This is the immediate value added to the instruction. |
| 448 | int64_t ImmVal; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 4869d34 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 450 | /// Register - This is the register record. |
| 451 | Record *Register; |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 452 | }; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 453 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 454 | /// MINumOperands - The number of MCInst operands populated by this |
| 455 | /// operand. |
| 456 | unsigned MINumOperands; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 457 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 458 | static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) { |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 459 | ResOperand X; |
| 460 | X.Kind = RenderAsmOperand; |
| 461 | X.AsmOperandNum = AsmOpNum; |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 462 | X.MINumOperands = NumOperands; |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 463 | return X; |
| 464 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 465 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 466 | static ResOperand getTiedOp(unsigned TiedOperandNum, unsigned SrcOperand1, |
| 467 | unsigned SrcOperand2) { |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 468 | ResOperand X; |
| 469 | X.Kind = TiedOperand; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 470 | X.TiedOperands = { TiedOperandNum, SrcOperand1, SrcOperand2 }; |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 471 | X.MINumOperands = 1; |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 472 | return X; |
| 473 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 474 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 475 | static ResOperand getImmOp(int64_t Val) { |
Chris Lattner | b6f8e82 | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 476 | ResOperand X; |
| 477 | X.Kind = ImmOperand; |
| 478 | X.ImmVal = Val; |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 479 | X.MINumOperands = 1; |
Chris Lattner | b6f8e82 | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 480 | return X; |
| 481 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 482 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 483 | static ResOperand getRegOp(Record *Reg) { |
Chris Lattner | 4869d34 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 484 | ResOperand X; |
| 485 | X.Kind = RegOperand; |
| 486 | X.Register = Reg; |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 487 | X.MINumOperands = 1; |
Chris Lattner | 4869d34 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 488 | return X; |
| 489 | } |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 490 | }; |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 491 | |
Devang Patel | 9bdc505 | 2012-01-10 17:50:43 +0000 | [diff] [blame] | 492 | /// AsmVariantID - Target's assembly syntax variant no. |
| 493 | int AsmVariantID; |
| 494 | |
David Blaikie | ba4e00f | 2014-12-22 21:26:26 +0000 | [diff] [blame] | 495 | /// AsmString - The assembly string for this instruction (with variants |
| 496 | /// removed), e.g. "movsx $src, $dst". |
| 497 | std::string AsmString; |
| 498 | |
Chris Lattner | a7a903e | 2010-11-02 17:34:28 +0000 | [diff] [blame] | 499 | /// TheDef - This is the definition of the instruction or InstAlias that this |
| 500 | /// matchable came from. |
Chris Lattner | 39bc53b | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 501 | Record *const TheDef; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 503 | /// DefRec - This is the definition that it came from. |
| 504 | PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 505 | |
Chris Lattner | fecdad6 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 506 | const CodeGenInstruction *getResultInst() const { |
| 507 | if (DefRec.is<const CodeGenInstruction*>()) |
| 508 | return DefRec.get<const CodeGenInstruction*>(); |
| 509 | return DefRec.get<const CodeGenInstAlias*>()->ResultInst; |
| 510 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 511 | |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 512 | /// ResOperands - This is the operand list that should be built for the result |
| 513 | /// MCInst. |
Jim Grosbach | a37e229 | 2012-04-19 17:52:34 +0000 | [diff] [blame] | 514 | SmallVector<ResOperand, 8> ResOperands; |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 516 | /// Mnemonic - This is the first token of the matched instruction, its |
| 517 | /// mnemonic. |
| 518 | StringRef Mnemonic; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 519 | |
Chris Lattner | d64b7c0 | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 520 | /// AsmOperands - The textual operands that this instruction matches, |
Chris Lattner | a7a903e | 2010-11-02 17:34:28 +0000 | [diff] [blame] | 521 | /// annotated with a class and where in the OperandList they were defined. |
| 522 | /// This directly corresponds to the tokenized AsmString after the mnemonic is |
| 523 | /// removed. |
Jim Grosbach | a37e229 | 2012-04-19 17:52:34 +0000 | [diff] [blame] | 524 | SmallVector<AsmOperand, 8> AsmOperands; |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 525 | |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 526 | /// Predicates - The required subtarget features to match this instruction. |
David Blaikie | 9a9da99 | 2014-11-28 22:15:06 +0000 | [diff] [blame] | 527 | SmallVector<const SubtargetFeatureInfo *, 4> RequiredFeatures; |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 528 | |
Daniel Dunbar | 7133028 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 529 | /// ConversionFnKind - The enum value which is passed to the generated |
Chad Rosier | ba284b9 | 2012-09-05 01:02:38 +0000 | [diff] [blame] | 530 | /// convertToMCInst to convert parsed operands into an MCInst for this |
Daniel Dunbar | 7133028 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 531 | /// function. |
| 532 | std::string ConversionFnKind; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 533 | |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 534 | /// If this instruction is deprecated in some form. |
| 535 | bool HasDeprecation; |
| 536 | |
Tom Stellard | 74c87c8 | 2015-05-26 15:55:50 +0000 | [diff] [blame] | 537 | /// If this is an alias, this is use to determine whether or not to using |
| 538 | /// the conversion function defined by the instruction's AsmMatchConverter |
| 539 | /// or to use the function generated by the alias. |
| 540 | bool UseInstAsmMatchConverter; |
| 541 | |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 542 | MatchableInfo(const CodeGenInstruction &CGI) |
Tom Stellard | 74c87c8 | 2015-05-26 15:55:50 +0000 | [diff] [blame] | 543 | : AsmVariantID(0), AsmString(CGI.AsmString), TheDef(CGI.TheDef), DefRec(&CGI), |
| 544 | UseInstAsmMatchConverter(true) { |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 545 | } |
Chris Lattner | 39bc53b | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 546 | |
David Blaikie | ba4e00f | 2014-12-22 21:26:26 +0000 | [diff] [blame] | 547 | MatchableInfo(std::unique_ptr<const CodeGenInstAlias> Alias) |
Tom Stellard | 74c87c8 | 2015-05-26 15:55:50 +0000 | [diff] [blame] | 548 | : AsmVariantID(0), AsmString(Alias->AsmString), TheDef(Alias->TheDef), |
| 549 | DefRec(Alias.release()), |
| 550 | UseInstAsmMatchConverter( |
| 551 | TheDef->getValueAsBit("UseInstAsmMatchConverter")) { |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 552 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 553 | |
David Blaikie | 6e48a81 | 2015-08-01 01:08:30 +0000 | [diff] [blame] | 554 | // Could remove this and the dtor if PointerUnion supported unique_ptr |
| 555 | // elements with a dynamic failure/assertion (like the one below) in the case |
| 556 | // where it was copied while being in an owning state. |
| 557 | MatchableInfo(const MatchableInfo &RHS) |
| 558 | : AsmVariantID(RHS.AsmVariantID), AsmString(RHS.AsmString), |
| 559 | TheDef(RHS.TheDef), DefRec(RHS.DefRec), ResOperands(RHS.ResOperands), |
| 560 | Mnemonic(RHS.Mnemonic), AsmOperands(RHS.AsmOperands), |
| 561 | RequiredFeatures(RHS.RequiredFeatures), |
| 562 | ConversionFnKind(RHS.ConversionFnKind), |
| 563 | HasDeprecation(RHS.HasDeprecation), |
| 564 | UseInstAsmMatchConverter(RHS.UseInstAsmMatchConverter) { |
| 565 | assert(!DefRec.is<const CodeGenInstAlias *>()); |
| 566 | } |
| 567 | |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 568 | ~MatchableInfo() { |
David Blaikie | ba4e00f | 2014-12-22 21:26:26 +0000 | [diff] [blame] | 569 | delete DefRec.dyn_cast<const CodeGenInstAlias*>(); |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 570 | } |
Craig Topper | ce27489 | 2014-11-28 05:01:21 +0000 | [diff] [blame] | 571 | |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 572 | // Two-operand aliases clone from the main matchable, but mark the second |
| 573 | // operand as a tied operand of the first for purposes of the assembler. |
| 574 | void formTwoOperandAlias(StringRef Constraint); |
| 575 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 576 | void initialize(const AsmMatcherInfo &Info, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 577 | SmallPtrSetImpl<Record*> &SingletonRegisters, |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 578 | AsmVariantInfo const &Variant, |
| 579 | bool HasMnemonicFirst); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 580 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 581 | /// validate - Return true if this matchable is a valid thing to match against |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 582 | /// and perform a bunch of validity checking. |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 583 | bool validate(StringRef CommentDelimiter, bool IsAlias) const; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 584 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 585 | /// findAsmOperand - Find the AsmOperand with the specified name and |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 586 | /// suboperand index. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 587 | int findAsmOperand(StringRef N, int SubOpIdx) const { |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 588 | auto I = find_if(AsmOperands, [&](const AsmOperand &Op) { |
| 589 | return Op.SrcOpName == N && Op.SubOpIdx == SubOpIdx; |
| 590 | }); |
Craig Topper | 58a0e7a | 2016-01-03 07:33:36 +0000 | [diff] [blame] | 591 | return (I != AsmOperands.end()) ? I - AsmOperands.begin() : -1; |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 592 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 593 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 594 | /// findAsmOperandNamed - Find the first AsmOperand with the specified name. |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 595 | /// This does not check the suboperand index. |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 596 | int findAsmOperandNamed(StringRef N, int LastIdx = -1) const { |
| 597 | auto I = std::find_if(AsmOperands.begin() + LastIdx + 1, AsmOperands.end(), |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 598 | [&](const AsmOperand &Op) { return Op.SrcOpName == N; }); |
Craig Topper | 58a0e7a | 2016-01-03 07:33:36 +0000 | [diff] [blame] | 599 | return (I != AsmOperands.end()) ? I - AsmOperands.begin() : -1; |
Chris Lattner | 897a140 | 2010-11-04 01:55:23 +0000 | [diff] [blame] | 600 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 601 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 602 | int findAsmOperandOriginallyNamed(StringRef N) const { |
| 603 | auto I = |
| 604 | find_if(AsmOperands, |
| 605 | [&](const AsmOperand &Op) { return Op.OrigSrcOpName == N; }); |
| 606 | return (I != AsmOperands.end()) ? I - AsmOperands.begin() : -1; |
| 607 | } |
| 608 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 609 | void buildInstructionResultOperands(); |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 610 | void buildAliasResultOperands(bool AliasConstraintsAreChecked); |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 611 | |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 612 | /// operator< - Compare two matchables. |
| 613 | bool operator<(const MatchableInfo &RHS) const { |
Chris Lattner | 82d88ce | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 614 | // The primary comparator is the instruction mnemonic. |
Ahmed Bougacha | ef3358d | 2016-06-23 17:09:49 +0000 | [diff] [blame] | 615 | if (int Cmp = Mnemonic.compare(RHS.Mnemonic)) |
| 616 | return Cmp == -1; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 617 | |
Chris Lattner | d64b7c0 | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 618 | if (AsmOperands.size() != RHS.AsmOperands.size()) |
| 619 | return AsmOperands.size() < RHS.AsmOperands.size(); |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 620 | |
Daniel Dunbar | d963191 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 621 | // Compare lexicographically by operand. The matcher validates that other |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 622 | // orderings wouldn't be ambiguous using \see couldMatchAmbiguouslyWith(). |
Chris Lattner | d64b7c0 | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 623 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 624 | if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class) |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 625 | return true; |
Chris Lattner | d64b7c0 | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 626 | if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class) |
Daniel Dunbar | d963191 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 627 | return false; |
| 628 | } |
| 629 | |
Andrew Trick | 818f5ac | 2012-08-29 03:52:57 +0000 | [diff] [blame] | 630 | // Give matches that require more features higher precedence. This is useful |
| 631 | // because we cannot define AssemblerPredicates with the negation of |
| 632 | // processor features. For example, ARM v6 "nop" may be either a HINT or |
| 633 | // MOV. With v6, we want to match HINT. The assembler has no way to |
| 634 | // predicate MOV under "NoV6", but HINT will always match first because it |
| 635 | // requires V6 while MOV does not. |
| 636 | if (RequiredFeatures.size() != RHS.RequiredFeatures.size()) |
| 637 | return RequiredFeatures.size() > RHS.RequiredFeatures.size(); |
| 638 | |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 639 | return false; |
| 640 | } |
| 641 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 642 | /// couldMatchAmbiguouslyWith - Check whether this matchable could |
Dmitri Gribenko | 8d30240 | 2012-09-15 20:22:05 +0000 | [diff] [blame] | 643 | /// ambiguously match the same set of operands as \p RHS (without being a |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 644 | /// strictly superior match). |
Craig Topper | 42bd819 | 2014-11-28 03:53:00 +0000 | [diff] [blame] | 645 | bool couldMatchAmbiguouslyWith(const MatchableInfo &RHS) const { |
Chris Lattner | e3c48de | 2010-11-01 23:57:23 +0000 | [diff] [blame] | 646 | // The primary comparator is the instruction mnemonic. |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 647 | if (Mnemonic != RHS.Mnemonic) |
Chris Lattner | e3c48de | 2010-11-01 23:57:23 +0000 | [diff] [blame] | 648 | return false; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 649 | |
Craig Topper | ad89541 | 2018-01-06 19:20:32 +0000 | [diff] [blame] | 650 | // Different variants can't conflict. |
| 651 | if (AsmVariantID != RHS.AsmVariantID) |
| 652 | return false; |
| 653 | |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 654 | // The number of operands is unambiguous. |
Chris Lattner | d64b7c0 | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 655 | if (AsmOperands.size() != RHS.AsmOperands.size()) |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 656 | return false; |
| 657 | |
Daniel Dunbar | e197409 | 2010-01-23 00:26:16 +0000 | [diff] [blame] | 658 | // Otherwise, make sure the ordering of the two instructions is unambiguous |
| 659 | // by checking that either (a) a token or operand kind discriminates them, |
| 660 | // or (b) the ordering among equivalent kinds is consistent. |
| 661 | |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 662 | // Tokens and operand kinds are unambiguous (assuming a correct target |
| 663 | // specific parser). |
Chris Lattner | d64b7c0 | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 664 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) |
| 665 | if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind || |
| 666 | AsmOperands[i].Class->Kind == ClassInfo::Token) |
| 667 | if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class || |
| 668 | *RHS.AsmOperands[i].Class < *AsmOperands[i].Class) |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 669 | return false; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 670 | |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 671 | // Otherwise, this operand could commute if all operands are equivalent, or |
| 672 | // there is a pair of operands that compare less than and a pair that |
| 673 | // compare greater than. |
| 674 | bool HasLT = false, HasGT = false; |
Chris Lattner | d64b7c0 | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 675 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 676 | if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class) |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 677 | HasLT = true; |
Chris Lattner | d64b7c0 | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 678 | if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class) |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 679 | HasGT = true; |
| 680 | } |
| 681 | |
Craig Topper | 322b67f | 2016-01-03 07:33:39 +0000 | [diff] [blame] | 682 | return HasLT == HasGT; |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Craig Topper | 42bd819 | 2014-11-28 03:53:00 +0000 | [diff] [blame] | 685 | void dump() const; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 686 | |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 687 | private: |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 688 | void tokenizeAsmString(AsmMatcherInfo const &Info, |
| 689 | AsmVariantInfo const &Variant); |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 690 | void addAsmOperand(StringRef Token, bool IsIsolatedToken = false); |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 691 | }; |
| 692 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 693 | struct OperandMatchEntry { |
| 694 | unsigned OperandMask; |
Craig Topper | 42bd819 | 2014-11-28 03:53:00 +0000 | [diff] [blame] | 695 | const MatchableInfo* MI; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 696 | ClassInfo *CI; |
| 697 | |
Craig Topper | 42bd819 | 2014-11-28 03:53:00 +0000 | [diff] [blame] | 698 | static OperandMatchEntry create(const MatchableInfo *mi, ClassInfo *ci, |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 699 | unsigned opMask) { |
| 700 | OperandMatchEntry X; |
| 701 | X.OperandMask = opMask; |
| 702 | X.CI = ci; |
| 703 | X.MI = mi; |
| 704 | return X; |
| 705 | } |
| 706 | }; |
| 707 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 708 | class AsmMatcherInfo { |
| 709 | public: |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 710 | /// Tracked Records |
Chris Lattner | 89dcb68 | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 711 | RecordKeeper &Records; |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 712 | |
Daniel Dunbar | e431871 | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 713 | /// The tablegen AsmParser record. |
| 714 | Record *AsmParser; |
| 715 | |
Chris Lattner | b80ab36 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 716 | /// Target - The target information. |
| 717 | CodeGenTarget &Target; |
| 718 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 719 | /// The classes which are needed for matching. |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 720 | std::forward_list<ClassInfo> Classes; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 721 | |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 722 | /// The information on the matchables to match. |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 723 | std::vector<std::unique_ptr<MatchableInfo>> Matchables; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 724 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 725 | /// Info for custom matching operands by user defined methods. |
| 726 | std::vector<OperandMatchEntry> OperandMatchInfo; |
| 727 | |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 728 | /// Map of Register records to their class information. |
Sean Silva | c8f5657 | 2012-09-19 01:47:01 +0000 | [diff] [blame] | 729 | typedef std::map<Record*, ClassInfo*, LessRecordByID> RegisterClassesTy; |
| 730 | RegisterClassesTy RegisterClasses; |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 731 | |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 732 | /// Map of Predicate records to their subtarget information. |
David Blaikie | 9a9da99 | 2014-11-28 22:15:06 +0000 | [diff] [blame] | 733 | std::map<Record *, SubtargetFeatureInfo, LessRecordByID> SubtargetFeatures; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 734 | |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 735 | /// Map of AsmOperandClass records to their class information. |
| 736 | std::map<Record*, ClassInfo*> AsmOperandClasses; |
| 737 | |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 738 | /// Map of RegisterClass records to their class information. |
| 739 | std::map<Record*, ClassInfo*> RegisterClassClasses; |
| 740 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 741 | private: |
| 742 | /// Map of token to class information which has already been constructed. |
| 743 | std::map<std::string, ClassInfo*> TokenClasses; |
| 744 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 745 | private: |
| 746 | /// getTokenClass - Lookup or create the class for the given token. |
Chris Lattner | 60db0a6 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 747 | ClassInfo *getTokenClass(StringRef Token); |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 748 | |
| 749 | /// getOperandClass - Lookup or create the class for the given operand. |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 750 | ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI, |
Jim Grosbach | d1f1b79 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 751 | int SubOpIdx); |
| 752 | ClassInfo *getOperandClass(Record *Rec, int SubOpIdx); |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 753 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 754 | /// buildRegisterClasses - Build the ClassInfo* instances for register |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 755 | /// classes. |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 756 | void buildRegisterClasses(SmallPtrSetImpl<Record*> &SingletonRegisters); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 757 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 758 | /// buildOperandClasses - Build the ClassInfo* instances for user defined |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 759 | /// operand classes. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 760 | void buildOperandClasses(); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 761 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 762 | void buildInstructionOperandReference(MatchableInfo *II, StringRef OpName, |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 763 | unsigned AsmOpIdx); |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 764 | void buildAliasOperandReference(MatchableInfo *II, StringRef OpName, |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 765 | MatchableInfo::AsmOperand &Op); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 766 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 767 | public: |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 768 | AsmMatcherInfo(Record *AsmParser, |
| 769 | CodeGenTarget &Target, |
Chris Lattner | 89dcb68 | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 770 | RecordKeeper &Records); |
Daniel Dunbar | e431871 | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 771 | |
Daniel Sanders | ea6ef3d | 2016-11-15 09:51:02 +0000 | [diff] [blame] | 772 | /// Construct the various tables used during matching. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 773 | void buildInfo(); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 774 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 775 | /// buildOperandMatchInfo - Build the necessary information to handle user |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 776 | /// defined operand parsing methods. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 777 | void buildOperandMatchInfo(); |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 4369007 | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 779 | /// getSubtargetFeature - Lookup or create the subtarget feature info for the |
| 780 | /// given operand. |
David Blaikie | 9a9da99 | 2014-11-28 22:15:06 +0000 | [diff] [blame] | 781 | const SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const { |
Chris Lattner | 4369007 | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 782 | assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!"); |
Craig Topper | 42bd819 | 2014-11-28 03:53:00 +0000 | [diff] [blame] | 783 | const auto &I = SubtargetFeatures.find(Def); |
David Blaikie | 9a9da99 | 2014-11-28 22:15:06 +0000 | [diff] [blame] | 784 | return I == SubtargetFeatures.end() ? nullptr : &I->second; |
Chris Lattner | 4369007 | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 785 | } |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 786 | |
Chris Lattner | 89dcb68 | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 787 | RecordKeeper &getRecords() const { |
| 788 | return Records; |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 789 | } |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 790 | |
| 791 | bool hasOptionalOperands() const { |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 792 | return find_if(Classes, [](const ClassInfo &Class) { |
| 793 | return Class.IsOptional; |
| 794 | }) != Classes.end(); |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 795 | } |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 796 | }; |
| 797 | |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 798 | } // end anonymous namespace |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 799 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 800 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Galina Kistanova | 98d4bd5 | 2017-05-17 02:20:05 +0000 | [diff] [blame] | 801 | LLVM_DUMP_METHOD void MatchableInfo::dump() const { |
Chris Lattner | 9f09381 | 2010-11-06 06:43:11 +0000 | [diff] [blame] | 802 | errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n"; |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 803 | |
Craig Topper | ad89541 | 2018-01-06 19:20:32 +0000 | [diff] [blame] | 804 | errs() << " variant: " << AsmVariantID << "\n"; |
| 805 | |
Chris Lattner | d64b7c0 | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 806 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
Craig Topper | 42bd819 | 2014-11-28 03:53:00 +0000 | [diff] [blame] | 807 | const AsmOperand &Op = AsmOperands[i]; |
Daniel Dunbar | c32aa06 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 808 | errs() << " op[" << i << "] = " << Op.Class->ClassName << " - "; |
Chris Lattner | 4779e3e9 | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 809 | errs() << '\"' << Op.Token << "\"\n"; |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 810 | } |
| 811 | } |
Galina Kistanova | 98d4bd5 | 2017-05-17 02:20:05 +0000 | [diff] [blame] | 812 | #endif |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 813 | |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 814 | static std::pair<StringRef, StringRef> |
Jakob Stoklund Olesen | d7b6696 | 2012-08-22 23:33:58 +0000 | [diff] [blame] | 815 | parseTwoOperandConstraint(StringRef S, ArrayRef<SMLoc> Loc) { |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 816 | // Split via the '='. |
| 817 | std::pair<StringRef, StringRef> Ops = S.split('='); |
| 818 | if (Ops.second == "") |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 819 | PrintFatalError(Loc, "missing '=' in two-operand alias constraint"); |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 820 | // Trim whitespace and the leading '$' on the operand names. |
| 821 | size_t start = Ops.first.find_first_of('$'); |
| 822 | if (start == std::string::npos) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 823 | PrintFatalError(Loc, "expected '$' prefix on asm operand name"); |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 824 | Ops.first = Ops.first.slice(start + 1, std::string::npos); |
| 825 | size_t end = Ops.first.find_last_of(" \t"); |
| 826 | Ops.first = Ops.first.slice(0, end); |
| 827 | // Now the second operand. |
| 828 | start = Ops.second.find_first_of('$'); |
| 829 | if (start == std::string::npos) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 830 | PrintFatalError(Loc, "expected '$' prefix on asm operand name"); |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 831 | Ops.second = Ops.second.slice(start + 1, std::string::npos); |
| 832 | end = Ops.second.find_last_of(" \t"); |
| 833 | Ops.first = Ops.first.slice(0, end); |
| 834 | return Ops; |
| 835 | } |
| 836 | |
| 837 | void MatchableInfo::formTwoOperandAlias(StringRef Constraint) { |
| 838 | // Figure out which operands are aliased and mark them as tied. |
| 839 | std::pair<StringRef, StringRef> Ops = |
| 840 | parseTwoOperandConstraint(Constraint, TheDef->getLoc()); |
| 841 | |
| 842 | // Find the AsmOperands that refer to the operands we're aliasing. |
| 843 | int SrcAsmOperand = findAsmOperandNamed(Ops.first); |
| 844 | int DstAsmOperand = findAsmOperandNamed(Ops.second); |
| 845 | if (SrcAsmOperand == -1) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 846 | PrintFatalError(TheDef->getLoc(), |
Benjamin Kramer | 48e7e85 | 2014-03-29 17:17:15 +0000 | [diff] [blame] | 847 | "unknown source two-operand alias operand '" + Ops.first + |
| 848 | "'."); |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 849 | if (DstAsmOperand == -1) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 850 | PrintFatalError(TheDef->getLoc(), |
Benjamin Kramer | 48e7e85 | 2014-03-29 17:17:15 +0000 | [diff] [blame] | 851 | "unknown destination two-operand alias operand '" + |
| 852 | Ops.second + "'."); |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 853 | |
| 854 | // Find the ResOperand that refers to the operand we're aliasing away |
| 855 | // and update it to refer to the combined operand instead. |
Craig Topper | e4e7415 | 2015-12-29 07:03:23 +0000 | [diff] [blame] | 856 | for (ResOperand &Op : ResOperands) { |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 857 | if (Op.Kind == ResOperand::RenderAsmOperand && |
| 858 | Op.AsmOperandNum == (unsigned)SrcAsmOperand) { |
| 859 | Op.AsmOperandNum = DstAsmOperand; |
| 860 | break; |
| 861 | } |
| 862 | } |
| 863 | // Remove the AsmOperand for the alias operand. |
| 864 | AsmOperands.erase(AsmOperands.begin() + SrcAsmOperand); |
| 865 | // Adjust the ResOperand references to any AsmOperands that followed |
| 866 | // the one we just deleted. |
Craig Topper | e4e7415 | 2015-12-29 07:03:23 +0000 | [diff] [blame] | 867 | for (ResOperand &Op : ResOperands) { |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 868 | switch(Op.Kind) { |
| 869 | default: |
| 870 | // Nothing to do for operands that don't reference AsmOperands. |
| 871 | break; |
| 872 | case ResOperand::RenderAsmOperand: |
| 873 | if (Op.AsmOperandNum > (unsigned)SrcAsmOperand) |
| 874 | --Op.AsmOperandNum; |
| 875 | break; |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
Craig Topper | 22fa45f | 2015-09-13 18:01:25 +0000 | [diff] [blame] | 880 | /// extractSingletonRegisterForAsmOperand - Extract singleton register, |
| 881 | /// if present, from specified token. |
| 882 | static void |
| 883 | extractSingletonRegisterForAsmOperand(MatchableInfo::AsmOperand &Op, |
| 884 | const AsmMatcherInfo &Info, |
| 885 | StringRef RegisterPrefix) { |
| 886 | StringRef Tok = Op.Token; |
| 887 | |
| 888 | // If this token is not an isolated token, i.e., it isn't separated from |
| 889 | // other tokens (e.g. with whitespace), don't interpret it as a register name. |
| 890 | if (!Op.IsIsolatedToken) |
| 891 | return; |
| 892 | |
| 893 | if (RegisterPrefix.empty()) { |
| 894 | std::string LoweredTok = Tok.lower(); |
| 895 | if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(LoweredTok)) |
| 896 | Op.SingletonReg = Reg->TheDef; |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | if (!Tok.startswith(RegisterPrefix)) |
| 901 | return; |
| 902 | |
| 903 | StringRef RegName = Tok.substr(RegisterPrefix.size()); |
| 904 | if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName)) |
| 905 | Op.SingletonReg = Reg->TheDef; |
| 906 | |
| 907 | // If there is no register prefix (i.e. "%" in "%eax"), then this may |
| 908 | // be some random non-register token, just ignore it. |
Craig Topper | 22fa45f | 2015-09-13 18:01:25 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 911 | void MatchableInfo::initialize(const AsmMatcherInfo &Info, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 912 | SmallPtrSetImpl<Record*> &SingletonRegisters, |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 913 | AsmVariantInfo const &Variant, |
| 914 | bool HasMnemonicFirst) { |
Craig Topper | c8b5b25 | 2015-12-30 06:00:18 +0000 | [diff] [blame] | 915 | AsmVariantID = Variant.AsmVariantNo; |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 916 | AsmString = |
Craig Topper | c8b5b25 | 2015-12-30 06:00:18 +0000 | [diff] [blame] | 917 | CodeGenInstruction::FlattenAsmStringVariants(AsmString, |
| 918 | Variant.AsmVariantNo); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 919 | |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 920 | tokenizeAsmString(Info, Variant); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 921 | |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 922 | // The first token of the instruction is the mnemonic, which must be a |
| 923 | // simple string, not a $foo variable or a singleton register. |
| 924 | if (AsmOperands.empty()) |
| 925 | PrintFatalError(TheDef->getLoc(), |
| 926 | "Instruction '" + TheDef->getName() + "' has no tokens"); |
| 927 | |
| 928 | assert(!AsmOperands[0].Token.empty()); |
| 929 | if (HasMnemonicFirst) { |
| 930 | Mnemonic = AsmOperands[0].Token; |
| 931 | if (Mnemonic[0] == '$') |
| 932 | PrintFatalError(TheDef->getLoc(), |
| 933 | "Invalid instruction mnemonic '" + Mnemonic + "'!"); |
| 934 | |
| 935 | // Remove the first operand, it is tracked in the mnemonic field. |
| 936 | AsmOperands.erase(AsmOperands.begin()); |
| 937 | } else if (AsmOperands[0].Token[0] != '$') |
| 938 | Mnemonic = AsmOperands[0].Token; |
| 939 | |
Chris Lattner | ba465f9 | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 940 | // Compute the require features. |
Craig Topper | 22fa45f | 2015-09-13 18:01:25 +0000 | [diff] [blame] | 941 | for (Record *Predicate : TheDef->getValueAsListOfDefs("Predicates")) |
David Blaikie | 9a9da99 | 2014-11-28 22:15:06 +0000 | [diff] [blame] | 942 | if (const SubtargetFeatureInfo *Feature = |
Craig Topper | 22fa45f | 2015-09-13 18:01:25 +0000 | [diff] [blame] | 943 | Info.getSubtargetFeature(Predicate)) |
Chris Lattner | ba465f9 | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 944 | RequiredFeatures.push_back(Feature); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 945 | |
Chris Lattner | ba465f9 | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 946 | // Collect singleton registers, if used. |
Craig Topper | 22fa45f | 2015-09-13 18:01:25 +0000 | [diff] [blame] | 947 | for (MatchableInfo::AsmOperand &Op : AsmOperands) { |
Craig Topper | c8b5b25 | 2015-12-30 06:00:18 +0000 | [diff] [blame] | 948 | extractSingletonRegisterForAsmOperand(Op, Info, Variant.RegisterPrefix); |
Craig Topper | 22fa45f | 2015-09-13 18:01:25 +0000 | [diff] [blame] | 949 | if (Record *Reg = Op.SingletonReg) |
Chris Lattner | ba465f9 | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 950 | SingletonRegisters.insert(Reg); |
| 951 | } |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 952 | |
| 953 | const RecordVal *DepMask = TheDef->getValue("DeprecatedFeatureMask"); |
| 954 | if (!DepMask) |
| 955 | DepMask = TheDef->getValue("ComplexDeprecationPredicate"); |
| 956 | |
| 957 | HasDeprecation = |
| 958 | DepMask ? !DepMask->getValue()->getAsUnquotedString().empty() : false; |
Chris Lattner | ba465f9 | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Ahmed Bougacha | d8dc2ac | 2015-05-29 00:55:55 +0000 | [diff] [blame] | 961 | /// Append an AsmOperand for the given substring of AsmString. |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 962 | void MatchableInfo::addAsmOperand(StringRef Token, bool IsIsolatedToken) { |
| 963 | AsmOperands.push_back(AsmOperand(IsIsolatedToken, Token)); |
Ahmed Bougacha | d8dc2ac | 2015-05-29 00:55:55 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 966 | /// tokenizeAsmString - Tokenize a simplified assembly string. |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 967 | void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info, |
| 968 | AsmVariantInfo const &Variant) { |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 969 | StringRef String = AsmString; |
Craig Topper | ba61432 | 2015-12-30 06:00:15 +0000 | [diff] [blame] | 970 | size_t Prev = 0; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 971 | bool InTok = false; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 972 | bool IsIsolatedToken = true; |
Craig Topper | ba61432 | 2015-12-30 06:00:15 +0000 | [diff] [blame] | 973 | for (size_t i = 0, e = String.size(); i != e; ++i) { |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 974 | char Char = String[i]; |
| 975 | if (Variant.BreakCharacters.find(Char) != std::string::npos) { |
| 976 | if (InTok) { |
| 977 | addAsmOperand(String.slice(Prev, i), false); |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 978 | Prev = i; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 979 | IsIsolatedToken = false; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 980 | } |
| 981 | InTok = true; |
| 982 | continue; |
| 983 | } |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 984 | if (Variant.TokenizingCharacters.find(Char) != std::string::npos) { |
| 985 | if (InTok) { |
| 986 | addAsmOperand(String.slice(Prev, i), IsIsolatedToken); |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 987 | InTok = false; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 988 | IsIsolatedToken = false; |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 989 | } |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 990 | addAsmOperand(String.slice(i, i + 1), IsIsolatedToken); |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 991 | Prev = i + 1; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 992 | IsIsolatedToken = true; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 993 | continue; |
| 994 | } |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 995 | if (Variant.SeparatorCharacters.find(Char) != std::string::npos) { |
| 996 | if (InTok) { |
| 997 | addAsmOperand(String.slice(Prev, i), IsIsolatedToken); |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 998 | InTok = false; |
| 999 | } |
| 1000 | Prev = i + 1; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1001 | IsIsolatedToken = true; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 1002 | continue; |
| 1003 | } |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1004 | |
| 1005 | switch (Char) { |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1006 | case '\\': |
| 1007 | if (InTok) { |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1008 | addAsmOperand(String.slice(Prev, i), false); |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1009 | InTok = false; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1010 | IsIsolatedToken = false; |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1011 | } |
| 1012 | ++i; |
| 1013 | assert(i != String.size() && "Invalid quoted character"); |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1014 | addAsmOperand(String.slice(i, i + 1), IsIsolatedToken); |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1015 | Prev = i + 1; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1016 | IsIsolatedToken = false; |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1017 | break; |
| 1018 | |
| 1019 | case '$': { |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1020 | if (InTok) { |
| 1021 | addAsmOperand(String.slice(Prev, i), false); |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1022 | InTok = false; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1023 | IsIsolatedToken = false; |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1024 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1025 | |
Colin LeMahieu | 3d90574 | 2015-08-10 19:58:06 +0000 | [diff] [blame] | 1026 | // If this isn't "${", start new identifier looking like "$xxx" |
Chris Lattner | d6746d5 | 2010-11-06 22:06:03 +0000 | [diff] [blame] | 1027 | if (i + 1 == String.size() || String[i + 1] != '{') { |
| 1028 | Prev = i; |
| 1029 | break; |
| 1030 | } |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1031 | |
Craig Topper | ba61432 | 2015-12-30 06:00:15 +0000 | [diff] [blame] | 1032 | size_t EndPos = String.find('}', i); |
| 1033 | assert(EndPos != StringRef::npos && |
| 1034 | "Missing brace in operand reference!"); |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1035 | addAsmOperand(String.slice(i, EndPos+1), IsIsolatedToken); |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1036 | Prev = EndPos + 1; |
| 1037 | i = EndPos; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1038 | IsIsolatedToken = false; |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1039 | break; |
| 1040 | } |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1041 | |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1042 | default: |
| 1043 | InTok = true; |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1044 | break; |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | if (InTok && Prev != String.size()) |
Craig Topper | bc22e26 | 2015-12-31 05:01:45 +0000 | [diff] [blame] | 1048 | addAsmOperand(String.substr(Prev), IsIsolatedToken); |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1051 | bool MatchableInfo::validate(StringRef CommentDelimiter, bool IsAlias) const { |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1052 | // Reject matchables with no .s string. |
Chris Lattner | 39bc53b | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1053 | if (AsmString.empty()) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1054 | PrintFatalError(TheDef->getLoc(), "instruction with empty asm string"); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1056 | // Reject any matchables with a newline in them, they should be marked |
Chris Lattner | 39bc53b | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1057 | // isCodeGenOnly if they are pseudo instructions. |
| 1058 | if (AsmString.find('\n') != std::string::npos) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1059 | PrintFatalError(TheDef->getLoc(), |
Chris Lattner | 39bc53b | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1060 | "multiline instruction is not valid for the asmparser, " |
| 1061 | "mark it isCodeGenOnly"); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1062 | |
Chris Lattner | 178f4bb | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 1063 | // Remove comments from the asm string. We know that the asmstring only |
| 1064 | // has one line. |
| 1065 | if (!CommentDelimiter.empty() && |
| 1066 | StringRef(AsmString).find(CommentDelimiter) != StringRef::npos) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1067 | PrintFatalError(TheDef->getLoc(), |
Chris Lattner | 178f4bb | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 1068 | "asmstring for instruction has comment character in it, " |
| 1069 | "mark it isCodeGenOnly"); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1071 | // Reject matchables with operand modifiers, these aren't something we can |
Bob Wilson | 266d2ba | 2011-01-20 18:38:07 +0000 | [diff] [blame] | 1072 | // handle, the target should be refactored to use operands instead of |
| 1073 | // modifiers. |
Chris Lattner | 39bc53b | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1074 | // |
| 1075 | // Also, check for instructions which reference the operand multiple times; |
| 1076 | // this implies a constraint we would not honor. |
| 1077 | std::set<std::string> OperandNames; |
Craig Topper | 77bd2b7 | 2015-12-30 06:00:20 +0000 | [diff] [blame] | 1078 | for (const AsmOperand &Op : AsmOperands) { |
| 1079 | StringRef Tok = Op.Token; |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1080 | if (Tok[0] == '$' && Tok.find(':') != StringRef::npos) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1081 | PrintFatalError(TheDef->getLoc(), |
Benjamin Kramer | 48e7e85 | 2014-03-29 17:17:15 +0000 | [diff] [blame] | 1082 | "matchable with operand modifier '" + Tok + |
| 1083 | "' not supported by asm matcher. Mark isCodeGenOnly!"); |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1084 | // Verify that any operand is only mentioned once. |
Chris Lattner | 4d23eb2 | 2010-11-02 23:18:43 +0000 | [diff] [blame] | 1085 | // We reject aliases and ignore instructions for now. |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1086 | if (!IsAlias && Tok[0] == '$' && !OperandNames.insert(Tok).second) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1087 | LLVM_DEBUG({ |
Chris Lattner | 9f09381 | 2010-11-06 06:43:11 +0000 | [diff] [blame] | 1088 | errs() << "warning: '" << TheDef->getName() << "': " |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1089 | << "ignoring instruction with tied operand '" |
Benjamin Kramer | 48e7e85 | 2014-03-29 17:17:15 +0000 | [diff] [blame] | 1090 | << Tok << "'\n"; |
Chris Lattner | 39bc53b | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1091 | }); |
| 1092 | return false; |
| 1093 | } |
| 1094 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1095 | |
Chris Lattner | 39bc53b | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1096 | return true; |
| 1097 | } |
| 1098 | |
Chris Lattner | 60db0a6 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 1099 | static std::string getEnumNameForToken(StringRef Str) { |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1100 | std::string Res; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1101 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1102 | for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) { |
| 1103 | switch (*it) { |
| 1104 | case '*': Res += "_STAR_"; break; |
| 1105 | case '%': Res += "_PCT_"; break; |
| 1106 | case ':': Res += "_COLON_"; break; |
Bill Wendling | 4a08e56 | 2010-11-18 23:36:54 +0000 | [diff] [blame] | 1107 | case '!': Res += "_EXCLAIM_"; break; |
Bill Wendling | a01ea89 | 2011-01-22 09:44:32 +0000 | [diff] [blame] | 1108 | case '.': Res += "_DOT_"; break; |
Tim Northover | b3cfb28 | 2013-01-10 16:47:31 +0000 | [diff] [blame] | 1109 | case '<': Res += "_LT_"; break; |
| 1110 | case '>': Res += "_GT_"; break; |
Hal Finkel | f909072 | 2015-01-15 01:33:00 +0000 | [diff] [blame] | 1111 | case '-': Res += "_MINUS_"; break; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1112 | default: |
Tim Northover | b3cfb28 | 2013-01-10 16:47:31 +0000 | [diff] [blame] | 1113 | if ((*it >= 'A' && *it <= 'Z') || |
| 1114 | (*it >= 'a' && *it <= 'z') || |
| 1115 | (*it >= '0' && *it <= '9')) |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1116 | Res += *it; |
Chris Lattner | 33fc3e0 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 1117 | else |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1118 | Res += "_" + utostr((unsigned) *it) + "_"; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | return Res; |
| 1123 | } |
| 1124 | |
Chris Lattner | 60db0a6 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 1125 | ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) { |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1126 | ClassInfo *&Entry = TokenClasses[Token]; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1127 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1128 | if (!Entry) { |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 1129 | Classes.emplace_front(); |
| 1130 | Entry = &Classes.front(); |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1131 | Entry->Kind = ClassInfo::Token; |
Daniel Dunbar | c32aa06 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 1132 | Entry->ClassName = "Token"; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1133 | Entry->Name = "MCK_" + getEnumNameForToken(Token); |
| 1134 | Entry->ValueName = Token; |
| 1135 | Entry->PredicateMethod = "<invalid>"; |
| 1136 | Entry->RenderMethod = "<invalid>"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1137 | Entry->ParserMethod = ""; |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 1138 | Entry->DiagnosticType = ""; |
Tom Stellard | b9f235e | 2016-02-05 19:59:33 +0000 | [diff] [blame] | 1139 | Entry->IsOptional = false; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 1140 | Entry->DefaultMethod = "<invalid>"; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | return Entry; |
| 1144 | } |
| 1145 | |
| 1146 | ClassInfo * |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1147 | AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI, |
| 1148 | int SubOpIdx) { |
| 1149 | Record *Rec = OI.Rec; |
| 1150 | if (SubOpIdx != -1) |
Sean Silva | 88eb8dd | 2012-10-10 20:24:47 +0000 | [diff] [blame] | 1151 | Rec = cast<DefInit>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef(); |
Jim Grosbach | d1f1b79 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 1152 | return getOperandClass(Rec, SubOpIdx); |
| 1153 | } |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1154 | |
Jim Grosbach | d1f1b79 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 1155 | ClassInfo * |
| 1156 | AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) { |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1157 | if (Rec->isSubClassOf("RegisterOperand")) { |
| 1158 | // RegisterOperand may have an associated ParserMatchClass. If it does, |
| 1159 | // use it, else just fall back to the underlying register class. |
| 1160 | const RecordVal *R = Rec->getValue("ParserMatchClass"); |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 1161 | if (!R || !R->getValue()) |
Daniel Sanders | dff673b | 2019-02-12 17:36:57 +0000 | [diff] [blame] | 1162 | PrintFatalError(Rec->getLoc(), |
| 1163 | "Record `" + Rec->getName() + |
| 1164 | "' does not have a ParserMatchClass!\n"); |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1165 | |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1166 | if (DefInit *DI= dyn_cast<DefInit>(R->getValue())) { |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1167 | Record *MatchClass = DI->getDef(); |
| 1168 | if (ClassInfo *CI = AsmOperandClasses[MatchClass]) |
| 1169 | return CI; |
| 1170 | } |
| 1171 | |
| 1172 | // No custom match class. Just use the register class. |
| 1173 | Record *ClassRec = Rec->getValueAsDef("RegClass"); |
| 1174 | if (!ClassRec) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1175 | PrintFatalError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() + |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1176 | "' has no associated register class!\n"); |
| 1177 | if (ClassInfo *CI = RegisterClassClasses[ClassRec]) |
| 1178 | return CI; |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1179 | PrintFatalError(Rec->getLoc(), "register class has no class info!"); |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1182 | if (Rec->isSubClassOf("RegisterClass")) { |
| 1183 | if (ClassInfo *CI = RegisterClassClasses[Rec]) |
Chris Lattner | 77d3ead | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 1184 | return CI; |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1185 | PrintFatalError(Rec->getLoc(), "register class has no class info!"); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1186 | } |
Daniel Dunbar | 8e33cb2 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1187 | |
Jim Grosbach | f6cb1ee | 2012-09-12 17:40:25 +0000 | [diff] [blame] | 1188 | if (!Rec->isSubClassOf("Operand")) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1189 | PrintFatalError(Rec->getLoc(), "Operand `" + Rec->getName() + |
Jim Grosbach | f6cb1ee | 2012-09-12 17:40:25 +0000 | [diff] [blame] | 1190 | "' does not derive from class Operand!\n"); |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1191 | Record *MatchClass = Rec->getValueAsDef("ParserMatchClass"); |
Chris Lattner | 77d3ead | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 1192 | if (ClassInfo *CI = AsmOperandClasses[MatchClass]) |
| 1193 | return CI; |
Daniel Dunbar | 17410a4 | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1194 | |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1195 | PrintFatalError(Rec->getLoc(), "operand has no match class!"); |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1198 | struct LessRegisterSet { |
Tim Northover | 9c30f7a | 2013-09-16 17:33:40 +0000 | [diff] [blame] | 1199 | bool operator() (const RegisterSet &LHS, const RegisterSet & RHS) const { |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1200 | // std::set<T> defines its own compariso "operator<", but it |
| 1201 | // performs a lexicographical comparison by T's innate comparison |
| 1202 | // for some reason. We don't want non-deterministic pointer |
| 1203 | // comparisons so use this instead. |
| 1204 | return std::lexicographical_compare(LHS.begin(), LHS.end(), |
| 1205 | RHS.begin(), RHS.end(), |
| 1206 | LessRecordByID()); |
| 1207 | } |
| 1208 | }; |
| 1209 | |
Chris Lattner | f7a01e9 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1210 | void AsmMatcherInfo:: |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 1211 | buildRegisterClasses(SmallPtrSetImpl<Record*> &SingletonRegisters) { |
David Blaikie | 9b613db | 2014-11-29 18:13:39 +0000 | [diff] [blame] | 1212 | const auto &Registers = Target.getRegBank().getRegisters(); |
David Blaikie | c0bb5ca | 2014-12-03 19:58:41 +0000 | [diff] [blame] | 1213 | auto &RegClassList = Target.getRegBank().getRegClasses(); |
Daniel Dunbar | 17410a4 | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1214 | |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1215 | typedef std::set<RegisterSet, LessRegisterSet> RegisterSetSet; |
| 1216 | |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1217 | // The register sets used for matching. |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1218 | RegisterSetSet RegisterSets; |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1219 | |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1220 | // Gather the defined sets. |
David Blaikie | dacea4b | 2014-12-03 19:58:45 +0000 | [diff] [blame] | 1221 | for (const CodeGenRegisterClass &RC : RegClassList) |
| 1222 | RegisterSets.insert( |
| 1223 | RegisterSet(RC.getOrder().begin(), RC.getOrder().end())); |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1224 | |
| 1225 | // Add any required singleton sets. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1226 | for (Record *Rec : SingletonRegisters) { |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1227 | RegisterSets.insert(RegisterSet(&Rec, &Rec + 1)); |
Chris Lattner | f7a01e9 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1228 | } |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1229 | |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1230 | // Introduce derived sets where necessary (when a register does not determine |
| 1231 | // a unique register set class), and build the mapping of registers to the set |
| 1232 | // they should classify to. |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1233 | std::map<Record*, RegisterSet> RegisterMap; |
David Blaikie | 9b613db | 2014-11-29 18:13:39 +0000 | [diff] [blame] | 1234 | for (const CodeGenRegister &CGR : Registers) { |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1235 | // Compute the intersection of all sets containing this register. |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1236 | RegisterSet ContainingSet; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1237 | |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1238 | for (const RegisterSet &RS : RegisterSets) { |
David Blaikie | 9b613db | 2014-11-29 18:13:39 +0000 | [diff] [blame] | 1239 | if (!RS.count(CGR.TheDef)) |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1240 | continue; |
| 1241 | |
| 1242 | if (ContainingSet.empty()) { |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1243 | ContainingSet = RS; |
Chris Lattner | 77d3ead | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 1244 | continue; |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1245 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1246 | |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1247 | RegisterSet Tmp; |
Chris Lattner | 77d3ead | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 1248 | std::swap(Tmp, ContainingSet); |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1249 | std::insert_iterator<RegisterSet> II(ContainingSet, |
| 1250 | ContainingSet.begin()); |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1251 | std::set_intersection(Tmp.begin(), Tmp.end(), RS.begin(), RS.end(), II, |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1252 | LessRecordByID()); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | if (!ContainingSet.empty()) { |
| 1256 | RegisterSets.insert(ContainingSet); |
David Blaikie | 9b613db | 2014-11-29 18:13:39 +0000 | [diff] [blame] | 1257 | RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet)); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | // Construct the register classes. |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1262 | std::map<RegisterSet, ClassInfo*, LessRegisterSet> RegisterSetClasses; |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1263 | unsigned Index = 0; |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1264 | for (const RegisterSet &RS : RegisterSets) { |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 1265 | Classes.emplace_front(); |
| 1266 | ClassInfo *CI = &Classes.front(); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1267 | CI->Kind = ClassInfo::RegisterClass0 + Index; |
| 1268 | CI->ClassName = "Reg" + utostr(Index); |
| 1269 | CI->Name = "MCK_Reg" + utostr(Index); |
| 1270 | CI->ValueName = ""; |
| 1271 | CI->PredicateMethod = ""; // unused |
| 1272 | CI->RenderMethod = "addRegOperands"; |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1273 | CI->Registers = RS; |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 1274 | // FIXME: diagnostic type. |
| 1275 | CI->DiagnosticType = ""; |
Tom Stellard | b9f235e | 2016-02-05 19:59:33 +0000 | [diff] [blame] | 1276 | CI->IsOptional = false; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 1277 | CI->DefaultMethod = ""; // unused |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1278 | RegisterSetClasses.insert(std::make_pair(RS, CI)); |
| 1279 | ++Index; |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | // Find the superclasses; we could compute only the subgroup lattice edges, |
| 1283 | // but there isn't really a point. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1284 | for (const RegisterSet &RS : RegisterSets) { |
| 1285 | ClassInfo *CI = RegisterSetClasses[RS]; |
| 1286 | for (const RegisterSet &RS2 : RegisterSets) |
| 1287 | if (RS != RS2 && |
| 1288 | std::includes(RS2.begin(), RS2.end(), RS.begin(), RS.end(), |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1289 | LessRecordByID())) |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1290 | CI->SuperClasses.push_back(RegisterSetClasses[RS2]); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | // Name the register classes which correspond to a user defined RegisterClass. |
David Blaikie | dacea4b | 2014-12-03 19:58:45 +0000 | [diff] [blame] | 1294 | for (const CodeGenRegisterClass &RC : RegClassList) { |
Jakob Stoklund Olesen | bd92dc6 | 2011-10-04 15:28:08 +0000 | [diff] [blame] | 1295 | // Def will be NULL for non-user defined register classes. |
David Blaikie | dacea4b | 2014-12-03 19:58:45 +0000 | [diff] [blame] | 1296 | Record *Def = RC.getDef(); |
Jakob Stoklund Olesen | bd92dc6 | 2011-10-04 15:28:08 +0000 | [diff] [blame] | 1297 | if (!Def) |
| 1298 | continue; |
David Blaikie | dacea4b | 2014-12-03 19:58:45 +0000 | [diff] [blame] | 1299 | ClassInfo *CI = RegisterSetClasses[RegisterSet(RC.getOrder().begin(), |
| 1300 | RC.getOrder().end())]; |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1301 | if (CI->ValueName.empty()) { |
David Blaikie | dacea4b | 2014-12-03 19:58:45 +0000 | [diff] [blame] | 1302 | CI->ClassName = RC.getName(); |
| 1303 | CI->Name = "MCK_" + RC.getName(); |
| 1304 | CI->ValueName = RC.getName(); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1305 | } else |
David Blaikie | dacea4b | 2014-12-03 19:58:45 +0000 | [diff] [blame] | 1306 | CI->ValueName = CI->ValueName + "," + RC.getName(); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1307 | |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 1308 | Init *DiagnosticType = Def->getValueInit("DiagnosticType"); |
| 1309 | if (StringInit *SI = dyn_cast<StringInit>(DiagnosticType)) |
| 1310 | CI->DiagnosticType = SI->getValue(); |
| 1311 | |
| 1312 | Init *DiagnosticString = Def->getValueInit("DiagnosticString"); |
| 1313 | if (StringInit *SI = dyn_cast<StringInit>(DiagnosticString)) |
| 1314 | CI->DiagnosticString = SI->getValue(); |
| 1315 | |
| 1316 | // If we have a diagnostic string but the diagnostic type is not specified |
| 1317 | // explicitly, create an anonymous diagnostic type. |
| 1318 | if (!CI->DiagnosticString.empty() && CI->DiagnosticType.empty()) |
| 1319 | CI->DiagnosticType = RC.getName(); |
| 1320 | |
Jakob Stoklund Olesen | bd92dc6 | 2011-10-04 15:28:08 +0000 | [diff] [blame] | 1321 | RegisterClassClasses.insert(std::make_pair(Def, CI)); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | // Populate the map for individual registers. |
Tim Northover | c74e691 | 2013-09-16 16:43:19 +0000 | [diff] [blame] | 1325 | for (std::map<Record*, RegisterSet>::iterator it = RegisterMap.begin(), |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1326 | ie = RegisterMap.end(); it != ie; ++it) |
Chris Lattner | 77d3ead | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 1327 | RegisterClasses[it->first] = RegisterSetClasses[it->second]; |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1328 | |
| 1329 | // Name the register classes which correspond to singleton registers. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1330 | for (Record *Rec : SingletonRegisters) { |
Chris Lattner | 77d3ead | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 1331 | ClassInfo *CI = RegisterClasses[Rec]; |
Chris Lattner | f7a01e9 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1332 | assert(CI && "Missing singleton register class info!"); |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1333 | |
Chris Lattner | f7a01e9 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1334 | if (CI->ValueName.empty()) { |
| 1335 | CI->ClassName = Rec->getName(); |
Matthias Braun | 4a86d45 | 2016-12-04 05:48:16 +0000 | [diff] [blame] | 1336 | CI->Name = "MCK_" + Rec->getName().str(); |
Chris Lattner | f7a01e9 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1337 | CI->ValueName = Rec->getName(); |
| 1338 | } else |
Matthias Braun | 4a86d45 | 2016-12-04 05:48:16 +0000 | [diff] [blame] | 1339 | CI->ValueName = CI->ValueName + "," + Rec->getName().str(); |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1340 | } |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1343 | void AsmMatcherInfo::buildOperandClasses() { |
Chris Lattner | e3c48de | 2010-11-01 23:57:23 +0000 | [diff] [blame] | 1344 | std::vector<Record*> AsmOperands = |
| 1345 | Records.getAllDerivedDefinitions("AsmOperandClass"); |
Daniel Dunbar | cf18153 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 1346 | |
| 1347 | // Pre-populate AsmOperandClasses map. |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 1348 | for (Record *Rec : AsmOperands) { |
| 1349 | Classes.emplace_front(); |
| 1350 | AsmOperandClasses[Rec] = &Classes.front(); |
| 1351 | } |
Daniel Dunbar | cf18153 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 1352 | |
Daniel Dunbar | 17410a4 | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1353 | unsigned Index = 0; |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1354 | for (Record *Rec : AsmOperands) { |
| 1355 | ClassInfo *CI = AsmOperandClasses[Rec]; |
Daniel Dunbar | 17410a4 | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1356 | CI->Kind = ClassInfo::UserClass0 + Index; |
| 1357 | |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1358 | ListInit *Supers = Rec->getValueAsListInit("SuperClasses"); |
Craig Topper | ef0578a | 2015-06-02 04:15:51 +0000 | [diff] [blame] | 1359 | for (Init *I : Supers->getValues()) { |
| 1360 | DefInit *DI = dyn_cast<DefInit>(I); |
Daniel Dunbar | 346782c | 2010-05-22 21:02:29 +0000 | [diff] [blame] | 1361 | if (!DI) { |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1362 | PrintError(Rec->getLoc(), "Invalid super class reference!"); |
Daniel Dunbar | 346782c | 2010-05-22 21:02:29 +0000 | [diff] [blame] | 1363 | continue; |
| 1364 | } |
| 1365 | |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1366 | ClassInfo *SC = AsmOperandClasses[DI->getDef()]; |
| 1367 | if (!SC) |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1368 | PrintError(Rec->getLoc(), "Invalid super class reference!"); |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1369 | else |
| 1370 | CI->SuperClasses.push_back(SC); |
Daniel Dunbar | 17410a4 | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1371 | } |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1372 | CI->ClassName = Rec->getValueAsString("Name"); |
Daniel Dunbar | 17410a4 | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1373 | CI->Name = "MCK_" + CI->ClassName; |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1374 | CI->ValueName = Rec->getName(); |
Daniel Dunbar | aeb1feb | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1375 | |
| 1376 | // Get or construct the predicate method name. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1377 | Init *PMName = Rec->getValueInit("PredicateMethod"); |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1378 | if (StringInit *SI = dyn_cast<StringInit>(PMName)) { |
Daniel Dunbar | aeb1feb | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1379 | CI->PredicateMethod = SI->getValue(); |
| 1380 | } else { |
Sean Silva | 88eb8dd | 2012-10-10 20:24:47 +0000 | [diff] [blame] | 1381 | assert(isa<UnsetInit>(PMName) && "Unexpected PredicateMethod field!"); |
Daniel Dunbar | aeb1feb | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1382 | CI->PredicateMethod = "is" + CI->ClassName; |
| 1383 | } |
| 1384 | |
| 1385 | // Get or construct the render method name. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1386 | Init *RMName = Rec->getValueInit("RenderMethod"); |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1387 | if (StringInit *SI = dyn_cast<StringInit>(RMName)) { |
Daniel Dunbar | aeb1feb | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1388 | CI->RenderMethod = SI->getValue(); |
| 1389 | } else { |
Sean Silva | 88eb8dd | 2012-10-10 20:24:47 +0000 | [diff] [blame] | 1390 | assert(isa<UnsetInit>(RMName) && "Unexpected RenderMethod field!"); |
Daniel Dunbar | aeb1feb | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1391 | CI->RenderMethod = "add" + CI->ClassName + "Operands"; |
| 1392 | } |
| 1393 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1394 | // Get the parse method name or leave it as empty. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1395 | Init *PRMName = Rec->getValueInit("ParserMethod"); |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1396 | if (StringInit *SI = dyn_cast<StringInit>(PRMName)) |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1397 | CI->ParserMethod = SI->getValue(); |
| 1398 | |
Oliver Stannard | 41dfac3 | 2017-10-03 14:34:57 +0000 | [diff] [blame] | 1399 | // Get the diagnostic type and string or leave them as empty. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1400 | Init *DiagnosticType = Rec->getValueInit("DiagnosticType"); |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 1401 | if (StringInit *SI = dyn_cast<StringInit>(DiagnosticType)) |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 1402 | CI->DiagnosticType = SI->getValue(); |
Oliver Stannard | 41dfac3 | 2017-10-03 14:34:57 +0000 | [diff] [blame] | 1403 | Init *DiagnosticString = Rec->getValueInit("DiagnosticString"); |
| 1404 | if (StringInit *SI = dyn_cast<StringInit>(DiagnosticString)) |
| 1405 | CI->DiagnosticString = SI->getValue(); |
| 1406 | // If we have a DiagnosticString, we need a DiagnosticType for use within |
| 1407 | // the matcher. |
| 1408 | if (!CI->DiagnosticString.empty() && CI->DiagnosticType.empty()) |
| 1409 | CI->DiagnosticType = CI->ClassName; |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 1410 | |
Tom Stellard | b9f235e | 2016-02-05 19:59:33 +0000 | [diff] [blame] | 1411 | Init *IsOptional = Rec->getValueInit("IsOptional"); |
| 1412 | if (BitInit *BI = dyn_cast<BitInit>(IsOptional)) |
| 1413 | CI->IsOptional = BI->getValue(); |
| 1414 | |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 1415 | // Get or construct the default method name. |
| 1416 | Init *DMName = Rec->getValueInit("DefaultMethod"); |
| 1417 | if (StringInit *SI = dyn_cast<StringInit>(DMName)) { |
| 1418 | CI->DefaultMethod = SI->getValue(); |
| 1419 | } else { |
| 1420 | assert(isa<UnsetInit>(DMName) && "Unexpected DefaultMethod field!"); |
| 1421 | CI->DefaultMethod = "default" + CI->ClassName + "Operands"; |
| 1422 | } |
| 1423 | |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1424 | ++Index; |
Daniel Dunbar | 17410a4 | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1425 | } |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1428 | AsmMatcherInfo::AsmMatcherInfo(Record *asmParser, |
| 1429 | CodeGenTarget &target, |
Chris Lattner | 89dcb68 | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 1430 | RecordKeeper &records) |
Devang Patel | 6d676e4 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 1431 | : Records(records), AsmParser(asmParser), Target(target) { |
Daniel Dunbar | e431871 | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1434 | /// buildOperandMatchInfo - Build the necessary information to handle user |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1435 | /// defined operand parsing methods. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1436 | void AsmMatcherInfo::buildOperandMatchInfo() { |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1437 | |
Jim Grosbach | 925a6d0 | 2012-04-18 23:46:25 +0000 | [diff] [blame] | 1438 | /// Map containing a mask with all operands indices that can be found for |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1439 | /// that class inside a instruction. |
Benjamin Kramer | 3a377bc | 2014-03-01 11:47:00 +0000 | [diff] [blame] | 1440 | typedef std::map<ClassInfo *, unsigned, less_ptr<ClassInfo>> OpClassMaskTy; |
Sean Silva | 835139b | 2012-09-19 01:47:03 +0000 | [diff] [blame] | 1441 | OpClassMaskTy OpClassMask; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1442 | |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 1443 | for (const auto &MI : Matchables) { |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1444 | OpClassMask.clear(); |
| 1445 | |
| 1446 | // Keep track of all operands of this instructions which belong to the |
| 1447 | // same class. |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1448 | for (unsigned i = 0, e = MI->AsmOperands.size(); i != e; ++i) { |
| 1449 | const MatchableInfo::AsmOperand &Op = MI->AsmOperands[i]; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1450 | if (Op.Class->ParserMethod.empty()) |
| 1451 | continue; |
| 1452 | unsigned &OperandMask = OpClassMask[Op.Class]; |
| 1453 | OperandMask |= (1 << i); |
| 1454 | } |
| 1455 | |
| 1456 | // Generate operand match info for each mnemonic/operand class pair. |
Craig Topper | 42bd819 | 2014-11-28 03:53:00 +0000 | [diff] [blame] | 1457 | for (const auto &OCM : OpClassMask) { |
| 1458 | unsigned OpMask = OCM.second; |
| 1459 | ClassInfo *CI = OCM.first; |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1460 | OperandMatchInfo.push_back(OperandMatchEntry::create(MI.get(), CI, |
| 1461 | OpMask)); |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1466 | void AsmMatcherInfo::buildInfo() { |
Chris Lattner | a0e8719 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1467 | // Build information about all of the AssemblerPredicates. |
Daniel Sanders | ea6ef3d | 2016-11-15 09:51:02 +0000 | [diff] [blame] | 1468 | const std::vector<std::pair<Record *, SubtargetFeatureInfo>> |
| 1469 | &SubtargetFeaturePairs = SubtargetFeatureInfo::getAll(Records); |
| 1470 | SubtargetFeatures.insert(SubtargetFeaturePairs.begin(), |
| 1471 | SubtargetFeaturePairs.end()); |
Daniel Sanders | a3e1125 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 1472 | #ifndef NDEBUG |
Daniel Sanders | ea6ef3d | 2016-11-15 09:51:02 +0000 | [diff] [blame] | 1473 | for (const auto &Pair : SubtargetFeatures) |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1474 | LLVM_DEBUG(Pair.second.dump()); |
Daniel Sanders | a3e1125 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 1475 | #endif // NDEBUG |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1476 | |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 1477 | bool HasMnemonicFirst = AsmParser->getValueAsBit("HasMnemonicFirst"); |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1478 | bool ReportMultipleNearMisses = |
| 1479 | AsmParser->getValueAsBit("ReportMultipleNearMisses"); |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 1480 | |
Chris Lattner | 33fc3e0 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 1481 | // Parse the instructions; we need to do this first so that we can gather the |
| 1482 | // singleton register classes. |
Chris Lattner | f7a01e9 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1483 | SmallPtrSet<Record*, 16> SingletonRegisters; |
Devang Patel | 85d684a | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1484 | unsigned VariantCount = Target.getAsmParserVariantCount(); |
| 1485 | for (unsigned VC = 0; VC != VariantCount; ++VC) { |
| 1486 | Record *AsmVariant = Target.getAsmParserVariant(VC); |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 1487 | StringRef CommentDelimiter = |
| 1488 | AsmVariant->getValueAsString("CommentDelimiter"); |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 1489 | AsmVariantInfo Variant; |
Craig Topper | c8b5b25 | 2015-12-30 06:00:18 +0000 | [diff] [blame] | 1490 | Variant.RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix"); |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 1491 | Variant.TokenizingCharacters = |
| 1492 | AsmVariant->getValueAsString("TokenizingCharacters"); |
| 1493 | Variant.SeparatorCharacters = |
| 1494 | AsmVariant->getValueAsString("SeparatorCharacters"); |
| 1495 | Variant.BreakCharacters = |
| 1496 | AsmVariant->getValueAsString("BreakCharacters"); |
Sam Kolton | 1b746d1 | 2016-09-08 15:50:52 +0000 | [diff] [blame] | 1497 | Variant.Name = AsmVariant->getValueAsString("Name"); |
Craig Topper | c8b5b25 | 2015-12-30 06:00:18 +0000 | [diff] [blame] | 1498 | Variant.AsmVariantNo = AsmVariant->getValueAsInt("Variant"); |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1499 | |
Craig Topper | 8cc904d | 2016-01-17 20:38:18 +0000 | [diff] [blame] | 1500 | for (const CodeGenInstruction *CGI : Target.getInstructionsByEnumValue()) { |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1501 | |
Devang Patel | 85d684a | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1502 | // If the tblgen -match-prefix option is specified (for tblgen hackers), |
| 1503 | // filter the set of instructions we consider. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1504 | if (!StringRef(CGI->TheDef->getName()).startswith(MatchPrefix)) |
Jim Grosbach | 3263a07 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1505 | continue; |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1506 | |
Devang Patel | 85d684a | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1507 | // Ignore "codegen only" instructions. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 1508 | if (CGI->TheDef->getValueAsBit("isCodeGenOnly")) |
Jim Grosbach | 3263a07 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1509 | continue; |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1510 | |
Sam Kolton | 1b746d1 | 2016-09-08 15:50:52 +0000 | [diff] [blame] | 1511 | // Ignore instructions for different instructions |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 1512 | StringRef V = CGI->TheDef->getValueAsString("AsmVariantName"); |
Sam Kolton | 1b746d1 | 2016-09-08 15:50:52 +0000 | [diff] [blame] | 1513 | if (!V.empty() && V != Variant.Name) |
| 1514 | continue; |
| 1515 | |
Craig Topper | 1c8fbd2 | 2015-09-06 03:44:50 +0000 | [diff] [blame] | 1516 | auto II = llvm::make_unique<MatchableInfo>(*CGI); |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1517 | |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 1518 | II->initialize(*this, SingletonRegisters, Variant, HasMnemonicFirst); |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1519 | |
Devang Patel | 85d684a | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1520 | // Ignore instructions which shouldn't be matched and diagnose invalid |
| 1521 | // instruction definitions with an error. |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1522 | if (!II->validate(CommentDelimiter, false)) |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1523 | continue; |
| 1524 | |
| 1525 | Matchables.push_back(std::move(II)); |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1526 | } |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1527 | |
Devang Patel | 85d684a | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1528 | // Parse all of the InstAlias definitions and stick them in the list of |
| 1529 | // matchables. |
| 1530 | std::vector<Record*> AllInstAliases = |
| 1531 | Records.getAllDerivedDefinitions("InstAlias"); |
| 1532 | for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) { |
David Blaikie | ba4e00f | 2014-12-22 21:26:26 +0000 | [diff] [blame] | 1533 | auto Alias = llvm::make_unique<CodeGenInstAlias>(AllInstAliases[i], |
Craig Topper | c8b5b25 | 2015-12-30 06:00:18 +0000 | [diff] [blame] | 1534 | Target); |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1535 | |
Devang Patel | 85d684a | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1536 | // If the tblgen -match-prefix option is specified (for tblgen hackers), |
| 1537 | // filter the set of instruction aliases we consider, based on the target |
| 1538 | // instruction. |
Jim Grosbach | 56e6326 | 2012-04-17 00:01:04 +0000 | [diff] [blame] | 1539 | if (!StringRef(Alias->ResultInst->TheDef->getName()) |
| 1540 | .startswith( MatchPrefix)) |
Jim Grosbach | 3263a07 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1541 | continue; |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1542 | |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 1543 | StringRef V = Alias->TheDef->getValueAsString("AsmVariantName"); |
Sam Kolton | 1b746d1 | 2016-09-08 15:50:52 +0000 | [diff] [blame] | 1544 | if (!V.empty() && V != Variant.Name) |
| 1545 | continue; |
| 1546 | |
Craig Topper | 1c8fbd2 | 2015-09-06 03:44:50 +0000 | [diff] [blame] | 1547 | auto II = llvm::make_unique<MatchableInfo>(std::move(Alias)); |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1548 | |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 1549 | II->initialize(*this, SingletonRegisters, Variant, HasMnemonicFirst); |
Jim Grosbach | 0bba00d | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1550 | |
Devang Patel | 85d684a | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1551 | // Validate the alias definitions. |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1552 | II->validate(CommentDelimiter, true); |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1553 | |
| 1554 | Matchables.push_back(std::move(II)); |
Devang Patel | 85d684a | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1555 | } |
Chris Lattner | 488c201 | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 1556 | } |
Chris Lattner | d8adec7 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 1557 | |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1558 | // Build info for the register classes. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1559 | buildRegisterClasses(SingletonRegisters); |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1560 | |
| 1561 | // Build info for the user defined assembly operand classes. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1562 | buildOperandClasses(); |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | 4779e3e9 | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1564 | // Build the information about matchables, now that we have fully formed |
| 1565 | // classes. |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1566 | std::vector<std::unique_ptr<MatchableInfo>> NewMatchables; |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 1567 | for (auto &II : Matchables) { |
Chris Lattner | 82d88ce | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 1568 | // Parse the tokens after the mnemonic. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1569 | // Note: buildInstructionOperandReference may insert new AsmOperands, so |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1570 | // don't precompute the loop bound. |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1571 | for (unsigned i = 0; i != II->AsmOperands.size(); ++i) { |
| 1572 | MatchableInfo::AsmOperand &Op = II->AsmOperands[i]; |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1573 | StringRef Token = Op.Token; |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1574 | |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1575 | // Check for singleton registers. |
Craig Topper | e4e7415 | 2015-12-29 07:03:23 +0000 | [diff] [blame] | 1576 | if (Record *RegRecord = Op.SingletonReg) { |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1577 | Op.Class = RegisterClasses[RegRecord]; |
Chris Lattner | b80ab36 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1578 | assert(Op.Class && Op.Class->Registers.size() == 1 && |
| 1579 | "Unexpected class for singleton register"); |
Chris Lattner | b80ab36 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1580 | continue; |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1583 | // Check for simple tokens. |
| 1584 | if (Token[0] != '$') { |
Chris Lattner | 28ea9b1 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1585 | Op.Class = getTokenClass(Token); |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1586 | continue; |
| 1587 | } |
| 1588 | |
Chris Lattner | d6746d5 | 2010-11-06 22:06:03 +0000 | [diff] [blame] | 1589 | if (Token.size() > 1 && isdigit(Token[1])) { |
| 1590 | Op.Class = getTokenClass(Token); |
| 1591 | continue; |
| 1592 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1593 | |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1594 | // Otherwise this is an operand reference. |
Chris Lattner | ccde463 | 2010-11-04 01:58:23 +0000 | [diff] [blame] | 1595 | StringRef OperandName; |
| 1596 | if (Token[1] == '{') |
| 1597 | OperandName = Token.substr(2, Token.size() - 3); |
| 1598 | else |
| 1599 | OperandName = Token.substr(1); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1600 | |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1601 | if (II->DefRec.is<const CodeGenInstruction*>()) |
| 1602 | buildInstructionOperandReference(II.get(), OperandName, i); |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1603 | else |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1604 | buildAliasOperandReference(II.get(), OperandName, Op); |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1605 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1606 | |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1607 | if (II->DefRec.is<const CodeGenInstruction*>()) { |
| 1608 | II->buildInstructionResultOperands(); |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 1609 | // If the instruction has a two-operand alias, build up the |
| 1610 | // matchable here. We'll add them in bulk at the end to avoid |
| 1611 | // confusing this loop. |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 1612 | StringRef Constraint = |
| 1613 | II->TheDef->getValueAsString("TwoOperandAliasConstraint"); |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 1614 | if (Constraint != "") { |
| 1615 | // Start by making a copy of the original matchable. |
Craig Topper | 1c8fbd2 | 2015-09-06 03:44:50 +0000 | [diff] [blame] | 1616 | auto AliasII = llvm::make_unique<MatchableInfo>(*II); |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 1617 | |
| 1618 | // Adjust it to be a two-operand alias. |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1619 | AliasII->formTwoOperandAlias(Constraint); |
| 1620 | |
| 1621 | // Add the alias to the matchables list. |
| 1622 | NewMatchables.push_back(std::move(AliasII)); |
Jim Grosbach | 31c2d3f | 2012-04-19 23:59:23 +0000 | [diff] [blame] | 1623 | } |
| 1624 | } else |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1625 | // FIXME: The tied operands checking is not yet integrated with the |
| 1626 | // framework for reporting multiple near misses. To prevent invalid |
| 1627 | // formats from being matched with an alias if a tied-operands check |
| 1628 | // would otherwise have disallowed it, we just disallow such constructs |
| 1629 | // in TableGen completely. |
| 1630 | II->buildAliasResultOperands(!ReportMultipleNearMisses); |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1631 | } |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 1632 | if (!NewMatchables.empty()) |
Benjamin Kramer | 4f6ac16 | 2015-02-28 10:11:12 +0000 | [diff] [blame] | 1633 | Matchables.insert(Matchables.end(), |
| 1634 | std::make_move_iterator(NewMatchables.begin()), |
| 1635 | std::make_move_iterator(NewMatchables.end())); |
Daniel Dunbar | 8e33cb2 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1636 | |
Jim Grosbach | ba39592 | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 1637 | // Process token alias definitions and set up the associated superclass |
| 1638 | // information. |
| 1639 | std::vector<Record*> AllTokenAliases = |
| 1640 | Records.getAllDerivedDefinitions("TokenAlias"); |
Craig Topper | e4e7415 | 2015-12-29 07:03:23 +0000 | [diff] [blame] | 1641 | for (Record *Rec : AllTokenAliases) { |
Jim Grosbach | ba39592 | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 1642 | ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken")); |
| 1643 | ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken")); |
Jim Grosbach | 37f6dcb3 | 2012-04-17 21:23:52 +0000 | [diff] [blame] | 1644 | if (FromClass == ToClass) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1645 | PrintFatalError(Rec->getLoc(), |
Jim Grosbach | 37f6dcb3 | 2012-04-17 21:23:52 +0000 | [diff] [blame] | 1646 | "error: Destination value identical to source value."); |
Jim Grosbach | ba39592 | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 1647 | FromClass->SuperClasses.push_back(ToClass); |
| 1648 | } |
| 1649 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1650 | // Reorder classes so that classes precede super classes. |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 1651 | Classes.sort(); |
Oliver Stannard | 7772f02 | 2016-01-25 10:20:19 +0000 | [diff] [blame] | 1652 | |
Matthias Braun | a8eed31 | 2016-12-05 19:44:31 +0000 | [diff] [blame] | 1653 | #ifdef EXPENSIVE_CHECKS |
| 1654 | // Verify that the table is sorted and operator < works transitively. |
Oliver Stannard | 7772f02 | 2016-01-25 10:20:19 +0000 | [diff] [blame] | 1655 | for (auto I = Classes.begin(), E = Classes.end(); I != E; ++I) { |
| 1656 | for (auto J = I; J != E; ++J) { |
| 1657 | assert(!(*J < *I)); |
| 1658 | assert(I == J || !J->isSubsetOf(*I)); |
| 1659 | } |
| 1660 | } |
Matthias Braun | a8eed31 | 2016-12-05 19:44:31 +0000 | [diff] [blame] | 1661 | #endif |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1664 | /// buildInstructionOperandReference - The specified operand is a reference to a |
Chris Lattner | 4779e3e9 | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1665 | /// named operand such as $src. Resolve the Class and OperandInfo pointers. |
| 1666 | void AsmMatcherInfo:: |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1667 | buildInstructionOperandReference(MatchableInfo *II, |
Chris Lattner | ccde463 | 2010-11-04 01:58:23 +0000 | [diff] [blame] | 1668 | StringRef OperandName, |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1669 | unsigned AsmOpIdx) { |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1670 | const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>(); |
| 1671 | const CGIOperandList &Operands = CGI.Operands; |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1672 | MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx]; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1673 | |
Chris Lattner | fecdad6 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 1674 | // Map this token to an operand. |
Chris Lattner | 4779e3e9 | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1675 | unsigned Idx; |
| 1676 | if (!Operands.hasOperandNamed(OperandName, Idx)) |
Benjamin Kramer | 48e7e85 | 2014-03-29 17:17:15 +0000 | [diff] [blame] | 1677 | PrintFatalError(II->TheDef->getLoc(), |
| 1678 | "error: unable to find operand: '" + OperandName + "'"); |
Chris Lattner | 897a140 | 2010-11-04 01:55:23 +0000 | [diff] [blame] | 1679 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1680 | // If the instruction operand has multiple suboperands, but the parser |
| 1681 | // match class for the asm operand is still the default "ImmAsmOperand", |
| 1682 | // then handle each suboperand separately. |
| 1683 | if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) { |
| 1684 | Record *Rec = Operands[Idx].Rec; |
| 1685 | assert(Rec->isSubClassOf("Operand") && "Unexpected operand!"); |
| 1686 | Record *MatchClass = Rec->getValueAsDef("ParserMatchClass"); |
| 1687 | if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") { |
| 1688 | // Insert remaining suboperands after AsmOpIdx in II->AsmOperands. |
| 1689 | StringRef Token = Op->Token; // save this in case Op gets moved |
| 1690 | for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) { |
Ahmed Bougacha | eb4dbd8 | 2015-05-29 01:03:37 +0000 | [diff] [blame] | 1691 | MatchableInfo::AsmOperand NewAsmOp(/*IsIsolatedToken=*/true, Token); |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1692 | NewAsmOp.SubOpIdx = SI; |
| 1693 | II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp); |
| 1694 | } |
| 1695 | // Replace Op with first suboperand. |
| 1696 | Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved |
| 1697 | Op->SubOpIdx = 0; |
| 1698 | } |
| 1699 | } |
| 1700 | |
Chris Lattner | 897a140 | 2010-11-04 01:55:23 +0000 | [diff] [blame] | 1701 | // Set up the operand class. |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1702 | Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx); |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1703 | Op->OrigSrcOpName = OperandName; |
Chris Lattner | 897a140 | 2010-11-04 01:55:23 +0000 | [diff] [blame] | 1704 | |
| 1705 | // If the named operand is tied, canonicalize it to the untied operand. |
| 1706 | // For example, something like: |
| 1707 | // (outs GPR:$dst), (ins GPR:$src) |
| 1708 | // with an asmstring of |
| 1709 | // "inc $src" |
| 1710 | // we want to canonicalize to: |
| 1711 | // "inc $dst" |
| 1712 | // so that we know how to provide the $dst operand when filling in the result. |
Ulrich Weigand | e037a49 | 2013-04-27 18:48:23 +0000 | [diff] [blame] | 1713 | int OITied = -1; |
| 1714 | if (Operands[Idx].MINumOperands == 1) |
| 1715 | OITied = Operands[Idx].getTiedRegister(); |
Chris Lattner | 4779e3e9 | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1716 | if (OITied != -1) { |
| 1717 | // The tied operand index is an MIOperand index, find the operand that |
| 1718 | // contains it. |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1719 | std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied); |
| 1720 | OperandName = Operands[Idx.first].Name; |
| 1721 | Op->SubOpIdx = Idx.second; |
Chris Lattner | 4779e3e9 | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1722 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1723 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1724 | Op->SrcOpName = OperandName; |
Chris Lattner | 4779e3e9 | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1727 | /// buildAliasOperandReference - When parsing an operand reference out of the |
Chris Lattner | b625dd2 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 1728 | /// matching string (e.g. "movsx $src, $dst"), determine what the class of the |
| 1729 | /// operand reference is by looking it up in the result pattern definition. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1730 | void AsmMatcherInfo::buildAliasOperandReference(MatchableInfo *II, |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1731 | StringRef OperandName, |
| 1732 | MatchableInfo::AsmOperand &Op) { |
| 1733 | const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>(); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1734 | |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1735 | // Set up the operand class. |
Chris Lattner | b625dd2 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 1736 | for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i) |
Chris Lattner | b6f8e82 | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 1737 | if (CGA.ResultOperands[i].isRecord() && |
| 1738 | CGA.ResultOperands[i].getName() == OperandName) { |
Chris Lattner | fecdad6 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 1739 | // It's safe to go with the first one we find, because CodeGenInstAlias |
| 1740 | // validates that all operands with the same name have the same record. |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1741 | Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second; |
Jim Grosbach | d1f1b79 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 1742 | // Use the match class from the Alias definition, not the |
| 1743 | // destination instruction, as we may have an immediate that's |
| 1744 | // being munged by the match class. |
| 1745 | Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(), |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1746 | Op.SubOpIdx); |
Chris Lattner | b625dd2 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 1747 | Op.SrcOpName = OperandName; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1748 | Op.OrigSrcOpName = OperandName; |
Chris Lattner | b625dd2 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 1749 | return; |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1750 | } |
Chris Lattner | b625dd2 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 1751 | |
Benjamin Kramer | 48e7e85 | 2014-03-29 17:17:15 +0000 | [diff] [blame] | 1752 | PrintFatalError(II->TheDef->getLoc(), |
| 1753 | "error: unable to find operand: '" + OperandName + "'"); |
Chris Lattner | 4efe13d | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1756 | void MatchableInfo::buildInstructionResultOperands() { |
Chris Lattner | fecdad6 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 1757 | const CodeGenInstruction *ResultInst = getResultInst(); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1758 | |
Chris Lattner | fecdad6 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 1759 | // Loop over all operands of the result instruction, determining how to |
| 1760 | // populate them. |
Craig Topper | e4e7415 | 2015-12-29 07:03:23 +0000 | [diff] [blame] | 1761 | for (const CGIOperandList::OperandInfo &OpInfo : ResultInst->Operands) { |
Chris Lattner | 7108dad | 2010-11-04 01:42:59 +0000 | [diff] [blame] | 1762 | // If this is a tied operand, just copy from the previously handled operand. |
Ulrich Weigand | e037a49 | 2013-04-27 18:48:23 +0000 | [diff] [blame] | 1763 | int TiedOp = -1; |
| 1764 | if (OpInfo.MINumOperands == 1) |
| 1765 | TiedOp = OpInfo.getTiedRegister(); |
Chris Lattner | 7108dad | 2010-11-04 01:42:59 +0000 | [diff] [blame] | 1766 | if (TiedOp != -1) { |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1767 | int TiedSrcOperand = findAsmOperandOriginallyNamed(OpInfo.Name); |
| 1768 | if (TiedSrcOperand != -1 && |
| 1769 | ResOperands[TiedOp].Kind == ResOperand::RenderAsmOperand) |
| 1770 | ResOperands.push_back(ResOperand::getTiedOp( |
| 1771 | TiedOp, ResOperands[TiedOp].AsmOperandNum, TiedSrcOperand)); |
| 1772 | else |
| 1773 | ResOperands.push_back(ResOperand::getTiedOp(TiedOp, 0, 0)); |
Chris Lattner | 7108dad | 2010-11-04 01:42:59 +0000 | [diff] [blame] | 1774 | continue; |
| 1775 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1776 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1777 | int SrcOperand = findAsmOperandNamed(OpInfo.Name); |
Ulrich Weigand | e037a49 | 2013-04-27 18:48:23 +0000 | [diff] [blame] | 1778 | if (OpInfo.Name.empty() || SrcOperand == -1) { |
| 1779 | // This may happen for operands that are tied to a suboperand of a |
| 1780 | // complex operand. Simply use a dummy value here; nobody should |
| 1781 | // use this operand slot. |
| 1782 | // FIXME: The long term goal is for the MCOperand list to not contain |
| 1783 | // tied operands at all. |
| 1784 | ResOperands.push_back(ResOperand::getImmOp(0)); |
| 1785 | continue; |
| 1786 | } |
Chris Lattner | 7108dad | 2010-11-04 01:42:59 +0000 | [diff] [blame] | 1787 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1788 | // Check if the one AsmOperand populates the entire operand. |
| 1789 | unsigned NumOperands = OpInfo.MINumOperands; |
| 1790 | if (AsmOperands[SrcOperand].SubOpIdx == -1) { |
| 1791 | ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands)); |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1792 | continue; |
| 1793 | } |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1794 | |
| 1795 | // Add a separate ResOperand for each suboperand. |
| 1796 | for (unsigned AI = 0; AI < NumOperands; ++AI) { |
| 1797 | assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI && |
| 1798 | AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name && |
| 1799 | "unexpected AsmOperands for suboperands"); |
| 1800 | ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1)); |
| 1801 | } |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1802 | } |
| 1803 | } |
| 1804 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1805 | void MatchableInfo::buildAliasResultOperands(bool AliasConstraintsAreChecked) { |
Chris Lattner | 8188fb2 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1806 | const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>(); |
| 1807 | const CodeGenInstruction *ResultInst = getResultInst(); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1808 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1809 | // Map of: $reg -> #lastref |
| 1810 | // where $reg is the name of the operand in the asm string |
| 1811 | // where #lastref is the last processed index where $reg was referenced in |
| 1812 | // the asm string. |
| 1813 | SmallDenseMap<StringRef, int> OperandRefs; |
| 1814 | |
Chris Lattner | 8188fb2 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1815 | // Loop over all operands of the result instruction, determining how to |
| 1816 | // populate them. |
| 1817 | unsigned AliasOpNo = 0; |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1818 | unsigned LastOpNo = CGA.ResultInstOperandIndex.size(); |
Chris Lattner | 8188fb2 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1819 | for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) { |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1820 | const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i]; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1821 | |
Chris Lattner | 8188fb2 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1822 | // If this is a tied operand, just copy from the previously handled operand. |
Ulrich Weigand | e037a49 | 2013-04-27 18:48:23 +0000 | [diff] [blame] | 1823 | int TiedOp = -1; |
| 1824 | if (OpInfo->MINumOperands == 1) |
| 1825 | TiedOp = OpInfo->getTiedRegister(); |
Chris Lattner | 8188fb2 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1826 | if (TiedOp != -1) { |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1827 | unsigned SrcOp1 = 0; |
| 1828 | unsigned SrcOp2 = 0; |
| 1829 | |
| 1830 | // If an operand has been specified twice in the asm string, |
| 1831 | // add the two source operand's indices to the TiedOp so that |
| 1832 | // at runtime the 'tied' constraint is checked. |
| 1833 | if (ResOperands[TiedOp].Kind == ResOperand::RenderAsmOperand) { |
| 1834 | SrcOp1 = ResOperands[TiedOp].AsmOperandNum; |
| 1835 | |
| 1836 | // Find the next operand (similarly named operand) in the string. |
| 1837 | StringRef Name = AsmOperands[SrcOp1].SrcOpName; |
| 1838 | auto Insert = OperandRefs.try_emplace(Name, SrcOp1); |
| 1839 | SrcOp2 = findAsmOperandNamed(Name, Insert.first->second); |
| 1840 | |
| 1841 | // Not updating the record in OperandRefs will cause TableGen |
| 1842 | // to fail with an error at the end of this function. |
| 1843 | if (AliasConstraintsAreChecked) |
| 1844 | Insert.first->second = SrcOp2; |
| 1845 | |
| 1846 | // In case it only has one reference in the asm string, |
| 1847 | // it doesn't need to be checked for tied constraints. |
| 1848 | SrcOp2 = (SrcOp2 == (unsigned)-1) ? SrcOp1 : SrcOp2; |
| 1849 | } |
| 1850 | |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 1851 | // If the alias operand is of a different operand class, we only want |
| 1852 | // to benefit from the tied-operands check and just match the operand |
| 1853 | // as a normal, but not copy the original (TiedOp) to the result |
| 1854 | // instruction. We do this by passing -1 as the tied operand to copy. |
| 1855 | if (ResultInst->Operands[i].Rec->getName() != |
| 1856 | ResultInst->Operands[TiedOp].Rec->getName()) { |
| 1857 | SrcOp1 = ResOperands[TiedOp].AsmOperandNum; |
| 1858 | int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second; |
| 1859 | StringRef Name = CGA.ResultOperands[AliasOpNo].getName(); |
| 1860 | SrcOp2 = findAsmOperand(Name, SubIdx); |
| 1861 | ResOperands.push_back( |
| 1862 | ResOperand::getTiedOp((unsigned)-1, SrcOp1, SrcOp2)); |
| 1863 | } else { |
| 1864 | ResOperands.push_back(ResOperand::getTiedOp(TiedOp, SrcOp1, SrcOp2)); |
| 1865 | continue; |
| 1866 | } |
Chris Lattner | 4869d34 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1869 | // Handle all the suboperands for this operand. |
| 1870 | const std::string &OpName = OpInfo->Name; |
| 1871 | for ( ; AliasOpNo < LastOpNo && |
| 1872 | CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) { |
| 1873 | int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second; |
| 1874 | |
| 1875 | // Find out what operand from the asmparser that this MCInst operand |
| 1876 | // comes from. |
| 1877 | switch (CGA.ResultOperands[AliasOpNo].Kind) { |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1878 | case CodeGenInstAlias::ResultOperand::K_Record: { |
| 1879 | StringRef Name = CGA.ResultOperands[AliasOpNo].getName(); |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 1880 | int SrcOperand = findAsmOperand(Name, SubIdx); |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1881 | if (SrcOperand == -1) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 1882 | PrintFatalError(TheDef->getLoc(), "Instruction '" + |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1883 | TheDef->getName() + "' has operand '" + OpName + |
| 1884 | "' that doesn't appear in asm string!"); |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1885 | |
| 1886 | // Add it to the operand references. If it is added a second time, the |
| 1887 | // record won't be updated and it will fail later on. |
| 1888 | OperandRefs.try_emplace(Name, SrcOperand); |
| 1889 | |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1890 | unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1); |
| 1891 | ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, |
| 1892 | NumOperands)); |
| 1893 | break; |
| 1894 | } |
| 1895 | case CodeGenInstAlias::ResultOperand::K_Imm: { |
| 1896 | int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm(); |
| 1897 | ResOperands.push_back(ResOperand::getImmOp(ImmVal)); |
| 1898 | break; |
| 1899 | } |
| 1900 | case CodeGenInstAlias::ResultOperand::K_Reg: { |
| 1901 | Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister(); |
| 1902 | ResOperands.push_back(ResOperand::getRegOp(Reg)); |
| 1903 | break; |
| 1904 | } |
| 1905 | } |
Chris Lattner | 4869d34 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 1906 | } |
Chris Lattner | 8188fb2 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1907 | } |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Check that operands are not repeated more times than is supported. |
| 1910 | for (auto &T : OperandRefs) { |
| 1911 | if (T.second != -1 && findAsmOperandNamed(T.first, T.second) != -1) |
| 1912 | PrintFatalError(TheDef->getLoc(), |
| 1913 | "Operand '" + T.first + "' can never be matched"); |
| 1914 | } |
Chris Lattner | 8188fb2 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1915 | } |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1916 | |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 1917 | static unsigned |
| 1918 | getConverterOperandID(const std::string &Name, |
| 1919 | SmallSetVector<CachedHashString, 16> &Table, |
| 1920 | bool &IsNew) { |
| 1921 | IsNew = Table.insert(CachedHashString(Name)); |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 1922 | |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 1923 | unsigned ID = IsNew ? Table.size() - 1 : find(Table, Name) - Table.begin(); |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 1924 | |
| 1925 | assert(ID < Table.size()); |
| 1926 | |
| 1927 | return ID; |
| 1928 | } |
| 1929 | |
Craig Topper | b64f915 | 2019-04-02 20:52:04 +0000 | [diff] [blame^] | 1930 | static unsigned |
| 1931 | emitConvertFuncs(CodeGenTarget &Target, StringRef ClassName, |
| 1932 | std::vector<std::unique_ptr<MatchableInfo>> &Infos, |
| 1933 | bool HasMnemonicFirst, bool HasOptionalOperands, |
| 1934 | raw_ostream &OS) { |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 1935 | SmallSetVector<CachedHashString, 16> OperandConversionKinds; |
| 1936 | SmallSetVector<CachedHashString, 16> InstructionConversionKinds; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 1937 | std::vector<std::vector<uint8_t> > ConversionTable; |
| 1938 | size_t MaxRowLength = 2; // minimum is custom converter plus terminator. |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1939 | |
Chris Lattner | f29c0b6 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1940 | // TargetOperandClass - This is the target's operand class, like X86Operand. |
Matthias Braun | 4a86d45 | 2016-12-04 05:48:16 +0000 | [diff] [blame] | 1941 | std::string TargetOperandClass = Target.getName().str() + "Operand"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1942 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 1943 | // Write the convert function to a separate stream, so we can drop it after |
| 1944 | // the enum. We'll build up the conversion handlers for the individual |
| 1945 | // operand types opportunistically as we encounter them. |
| 1946 | std::string ConvertFnBody; |
| 1947 | raw_string_ostream CvtOS(ConvertFnBody); |
| 1948 | // Start the unified conversion function. |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 1949 | if (HasOptionalOperands) { |
| 1950 | CvtOS << "void " << Target.getName() << ClassName << "::\n" |
| 1951 | << "convertToMCInst(unsigned Kind, MCInst &Inst, " |
| 1952 | << "unsigned Opcode,\n" |
| 1953 | << " const OperandVector &Operands,\n" |
| 1954 | << " const SmallBitVector &OptionalOperandsMask) {\n"; |
| 1955 | } else { |
| 1956 | CvtOS << "void " << Target.getName() << ClassName << "::\n" |
| 1957 | << "convertToMCInst(unsigned Kind, MCInst &Inst, " |
| 1958 | << "unsigned Opcode,\n" |
| 1959 | << " const OperandVector &Operands) {\n"; |
| 1960 | } |
| 1961 | CvtOS << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n"; |
| 1962 | CvtOS << " const uint8_t *Converter = ConversionTable[Kind];\n"; |
| 1963 | if (HasOptionalOperands) { |
Nirav Dave | b2f3fad | 2017-08-07 13:55:27 +0000 | [diff] [blame] | 1964 | size_t MaxNumOperands = 0; |
| 1965 | for (const auto &MI : Infos) { |
| 1966 | MaxNumOperands = std::max(MaxNumOperands, MI->AsmOperands.size()); |
| 1967 | } |
| 1968 | CvtOS << " unsigned DefaultsOffset[" << (MaxNumOperands + 1) |
| 1969 | << "] = { 0 };\n"; |
| 1970 | CvtOS << " assert(OptionalOperandsMask.size() == " << (MaxNumOperands) |
| 1971 | << ");\n"; |
| 1972 | CvtOS << " for (unsigned i = 0, NumDefaults = 0; i < " << (MaxNumOperands) |
| 1973 | << "; ++i) {\n"; |
| 1974 | CvtOS << " DefaultsOffset[i + 1] = NumDefaults;\n"; |
| 1975 | CvtOS << " NumDefaults += (OptionalOperandsMask[i] ? 1 : 0);\n"; |
| 1976 | CvtOS << " }\n"; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 1977 | } |
| 1978 | CvtOS << " unsigned OpIdx;\n"; |
| 1979 | CvtOS << " Inst.setOpcode(Opcode);\n"; |
| 1980 | CvtOS << " for (const uint8_t *p = Converter; *p; p+= 2) {\n"; |
| 1981 | if (HasOptionalOperands) { |
Nirav Dave | b2f3fad | 2017-08-07 13:55:27 +0000 | [diff] [blame] | 1982 | CvtOS << " OpIdx = *(p + 1) - DefaultsOffset[*(p + 1)];\n"; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 1983 | } else { |
| 1984 | CvtOS << " OpIdx = *(p + 1);\n"; |
| 1985 | } |
| 1986 | CvtOS << " switch (*p) {\n"; |
| 1987 | CvtOS << " default: llvm_unreachable(\"invalid conversion entry!\");\n"; |
| 1988 | CvtOS << " case CVT_Reg:\n"; |
| 1989 | CvtOS << " static_cast<" << TargetOperandClass |
| 1990 | << "&>(*Operands[OpIdx]).addRegOperands(Inst, 1);\n"; |
| 1991 | CvtOS << " break;\n"; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1992 | CvtOS << " case CVT_Tied: {\n"; |
Simon Pilgrim | e4d40f9 | 2018-02-17 12:29:47 +0000 | [diff] [blame] | 1993 | CvtOS << " assert(OpIdx < (size_t)(std::end(TiedAsmOperandTable) -\n"; |
| 1994 | CvtOS << " std::begin(TiedAsmOperandTable)) &&\n"; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 1995 | CvtOS << " \"Tied operand not found\");\n"; |
| 1996 | CvtOS << " unsigned TiedResOpnd = TiedAsmOperandTable[OpIdx][0];\n"; |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 1997 | CvtOS << " if (TiedResOpnd != (uint8_t) -1)\n"; |
| 1998 | CvtOS << " Inst.addOperand(Inst.getOperand(TiedResOpnd));\n"; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 1999 | CvtOS << " break;\n"; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2000 | CvtOS << " }\n"; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2001 | |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2002 | std::string OperandFnBody; |
| 2003 | raw_string_ostream OpOS(OperandFnBody); |
| 2004 | // Start the operand number lookup function. |
Chad Rosier | f4e35dc | 2012-10-01 23:45:51 +0000 | [diff] [blame] | 2005 | OpOS << "void " << Target.getName() << ClassName << "::\n" |
| 2006 | << "convertToMapAndConstraints(unsigned Kind,\n"; |
Chad Rosier | 380a74a | 2012-10-02 00:25:57 +0000 | [diff] [blame] | 2007 | OpOS.indent(27); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 2008 | OpOS << "const OperandVector &Operands) {\n" |
Chad Rosier | 98cfa10 | 2012-08-31 00:03:31 +0000 | [diff] [blame] | 2009 | << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n" |
Chad Rosier | f4e35dc | 2012-10-01 23:45:51 +0000 | [diff] [blame] | 2010 | << " unsigned NumMCOperands = 0;\n" |
Craig Topper | 9150610 | 2012-09-18 01:41:49 +0000 | [diff] [blame] | 2011 | << " const uint8_t *Converter = ConversionTable[Kind];\n" |
| 2012 | << " for (const uint8_t *p = Converter; *p; p+= 2) {\n" |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2013 | << " switch (*p) {\n" |
| 2014 | << " default: llvm_unreachable(\"invalid conversion entry!\");\n" |
| 2015 | << " case CVT_Reg:\n" |
Chad Rosier | 2f480a8 | 2012-10-12 22:53:36 +0000 | [diff] [blame] | 2016 | << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n" |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 2017 | << " Operands[*(p + 1)]->setConstraint(\"r\");\n" |
Chad Rosier | 2f480a8 | 2012-10-12 22:53:36 +0000 | [diff] [blame] | 2018 | << " ++NumMCOperands;\n" |
| 2019 | << " break;\n" |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2020 | << " case CVT_Tied:\n" |
Chad Rosier | f4e35dc | 2012-10-01 23:45:51 +0000 | [diff] [blame] | 2021 | << " ++NumMCOperands;\n" |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2022 | << " break;\n"; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2023 | |
| 2024 | // Pre-populate the operand conversion kinds with the standard always |
| 2025 | // available entries. |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 2026 | OperandConversionKinds.insert(CachedHashString("CVT_Done")); |
| 2027 | OperandConversionKinds.insert(CachedHashString("CVT_Reg")); |
| 2028 | OperandConversionKinds.insert(CachedHashString("CVT_Tied")); |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2029 | enum { CVT_Done, CVT_Reg, CVT_Tied }; |
| 2030 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2031 | // Map of e.g. <0, 2, 3> -> "Tie_0_2_3" enum label. |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 2032 | std::map<std::tuple<uint8_t, uint8_t, uint8_t>, std::string> |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2033 | TiedOperandsEnumMap; |
| 2034 | |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 2035 | for (auto &II : Infos) { |
Daniel Dunbar | 77b7c3f | 2011-02-04 17:12:15 +0000 | [diff] [blame] | 2036 | // Check if we have a custom match function. |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 2037 | StringRef AsmMatchConverter = |
| 2038 | II->getResultInst()->TheDef->getValueAsString("AsmMatchConverter"); |
Tom Stellard | 74c87c8 | 2015-05-26 15:55:50 +0000 | [diff] [blame] | 2039 | if (!AsmMatchConverter.empty() && II->UseInstAsmMatchConverter) { |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 2040 | std::string Signature = ("ConvertCustom_" + AsmMatchConverter).str(); |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 2041 | II->ConversionFnKind = Signature; |
Daniel Dunbar | 77b7c3f | 2011-02-04 17:12:15 +0000 | [diff] [blame] | 2042 | |
| 2043 | // Check if we have already generated this signature. |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 2044 | if (!InstructionConversionKinds.insert(CachedHashString(Signature))) |
Daniel Dunbar | 77b7c3f | 2011-02-04 17:12:15 +0000 | [diff] [blame] | 2045 | continue; |
| 2046 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2047 | // Remember this converter for the kind enum. |
| 2048 | unsigned KindID = OperandConversionKinds.size(); |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 2049 | OperandConversionKinds.insert( |
| 2050 | CachedHashString("CVT_" + getEnumNameForToken(AsmMatchConverter))); |
Daniel Dunbar | 77b7c3f | 2011-02-04 17:12:15 +0000 | [diff] [blame] | 2051 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2052 | // Add the converter row for this instruction. |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2053 | ConversionTable.emplace_back(); |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2054 | ConversionTable.back().push_back(KindID); |
| 2055 | ConversionTable.back().push_back(CVT_Done); |
| 2056 | |
| 2057 | // Add the handler to the conversion driver function. |
Tim Northover | b3cfb28 | 2013-01-10 16:47:31 +0000 | [diff] [blame] | 2058 | CvtOS << " case CVT_" |
| 2059 | << getEnumNameForToken(AsmMatchConverter) << ":\n" |
Chad Rosier | 451ef13 | 2012-08-31 22:12:31 +0000 | [diff] [blame] | 2060 | << " " << AsmMatchConverter << "(Inst, Operands);\n" |
Chad Rosier | 98cfa10 | 2012-08-31 00:03:31 +0000 | [diff] [blame] | 2061 | << " break;\n"; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2062 | |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2063 | // FIXME: Handle the operand number lookup for custom match functions. |
Daniel Dunbar | 77b7c3f | 2011-02-04 17:12:15 +0000 | [diff] [blame] | 2064 | continue; |
| 2065 | } |
| 2066 | |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 2067 | // Build the conversion function signature. |
| 2068 | std::string Signature = "Convert"; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2069 | |
| 2070 | std::vector<uint8_t> ConversionRow; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2071 | |
Chris Lattner | 5cf8a4a | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 2072 | // Compute the convert enum and the case body. |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 2073 | MaxRowLength = std::max(MaxRowLength, II->ResOperands.size()*2 + 1 ); |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2074 | |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 2075 | for (unsigned i = 0, e = II->ResOperands.size(); i != e; ++i) { |
| 2076 | const MatchableInfo::ResOperand &OpInfo = II->ResOperands[i]; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2077 | |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 2078 | // Generate code to populate each result operand. |
| 2079 | switch (OpInfo.Kind) { |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 2080 | case MatchableInfo::ResOperand::RenderAsmOperand: { |
| 2081 | // This comes from something we parsed. |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 2082 | const MatchableInfo::AsmOperand &Op = |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 2083 | II->AsmOperands[OpInfo.AsmOperandNum]; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2084 | |
Chris Lattner | e032dbf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 2085 | // Registers are always converted the same, don't duplicate the |
| 2086 | // conversion function based on them. |
Chris Lattner | e032dbf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 2087 | Signature += "__"; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2088 | std::string Class; |
| 2089 | Class = Op.Class->isRegisterClass() ? "Reg" : Op.Class->ClassName; |
| 2090 | Signature += Class; |
Bob Wilson | b9b2422 | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 2091 | Signature += utostr(OpInfo.MINumOperands); |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 2092 | Signature += "_" + itostr(OpInfo.AsmOperandNum); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2093 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2094 | // Add the conversion kind, if necessary, and get the associated ID |
| 2095 | // the index of its entry in the vector). |
| 2096 | std::string Name = "CVT_" + (Op.Class->isRegisterClass() ? "Reg" : |
| 2097 | Op.Class->RenderMethod); |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 2098 | if (Op.Class->IsOptional) { |
| 2099 | // For optional operands we must also care about DefaultMethod |
| 2100 | assert(HasOptionalOperands); |
| 2101 | Name += "_" + Op.Class->DefaultMethod; |
| 2102 | } |
Tim Northover | b3cfb28 | 2013-01-10 16:47:31 +0000 | [diff] [blame] | 2103 | Name = getEnumNameForToken(Name); |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2104 | |
| 2105 | bool IsNewConverter = false; |
| 2106 | unsigned ID = getConverterOperandID(Name, OperandConversionKinds, |
| 2107 | IsNewConverter); |
| 2108 | |
| 2109 | // Add the operand entry to the instruction kind conversion row. |
| 2110 | ConversionRow.push_back(ID); |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 2111 | ConversionRow.push_back(OpInfo.AsmOperandNum + HasMnemonicFirst); |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2112 | |
| 2113 | if (!IsNewConverter) |
| 2114 | break; |
| 2115 | |
| 2116 | // This is a new operand kind. Add a handler for it to the |
| 2117 | // converter driver. |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 2118 | CvtOS << " case " << Name << ":\n"; |
| 2119 | if (Op.Class->IsOptional) { |
| 2120 | // If optional operand is not present in actual instruction then we |
| 2121 | // should call its DefaultMethod before RenderMethod |
| 2122 | assert(HasOptionalOperands); |
| 2123 | CvtOS << " if (OptionalOperandsMask[*(p + 1) - 1]) {\n" |
| 2124 | << " " << Op.Class->DefaultMethod << "()" |
| 2125 | << "->" << Op.Class->RenderMethod << "(Inst, " |
| 2126 | << OpInfo.MINumOperands << ");\n" |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 2127 | << " } else {\n" |
| 2128 | << " static_cast<" << TargetOperandClass |
| 2129 | << "&>(*Operands[OpIdx])." << Op.Class->RenderMethod |
| 2130 | << "(Inst, " << OpInfo.MINumOperands << ");\n" |
| 2131 | << " }\n"; |
| 2132 | } else { |
| 2133 | CvtOS << " static_cast<" << TargetOperandClass |
| 2134 | << "&>(*Operands[OpIdx])." << Op.Class->RenderMethod |
| 2135 | << "(Inst, " << OpInfo.MINumOperands << ");\n"; |
| 2136 | } |
| 2137 | CvtOS << " break;\n"; |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2138 | |
| 2139 | // Add a handler for the operand number lookup. |
| 2140 | OpOS << " case " << Name << ":\n" |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 2141 | << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"; |
| 2142 | |
| 2143 | if (Op.Class->isRegisterClass()) |
| 2144 | OpOS << " Operands[*(p + 1)]->setConstraint(\"r\");\n"; |
| 2145 | else |
| 2146 | OpOS << " Operands[*(p + 1)]->setConstraint(\"m\");\n"; |
| 2147 | OpOS << " NumMCOperands += " << OpInfo.MINumOperands << ";\n" |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2148 | << " break;\n"; |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 2149 | break; |
Daniel Dunbar | f22553a | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 2150 | } |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 2151 | case MatchableInfo::ResOperand::TiedOperand: { |
| 2152 | // If this operand is tied to a previous one, just copy the MCInst |
| 2153 | // operand from the earlier one.We can only tie single MCOperand values. |
Ulrich Weigand | e037a49 | 2013-04-27 18:48:23 +0000 | [diff] [blame] | 2154 | assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand"); |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 2155 | uint8_t TiedOp = OpInfo.TiedOperands.ResOpnd; |
| 2156 | uint8_t SrcOp1 = |
| 2157 | OpInfo.TiedOperands.SrcOpnd1Idx + HasMnemonicFirst; |
| 2158 | uint8_t SrcOp2 = |
| 2159 | OpInfo.TiedOperands.SrcOpnd2Idx + HasMnemonicFirst; |
| 2160 | assert((i > TiedOp || TiedOp == (uint8_t)-1) && |
| 2161 | "Tied operand precedes its target!"); |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2162 | auto TiedTupleName = std::string("Tie") + utostr(TiedOp) + '_' + |
| 2163 | utostr(SrcOp1) + '_' + utostr(SrcOp2); |
| 2164 | Signature += "__" + TiedTupleName; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2165 | ConversionRow.push_back(CVT_Tied); |
| 2166 | ConversionRow.push_back(TiedOp); |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2167 | ConversionRow.push_back(SrcOp1); |
| 2168 | ConversionRow.push_back(SrcOp2); |
| 2169 | |
| 2170 | // Also create an 'enum' for this combination of tied operands. |
| 2171 | auto Key = std::make_tuple(TiedOp, SrcOp1, SrcOp2); |
| 2172 | TiedOperandsEnumMap.emplace(Key, TiedTupleName); |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 2173 | break; |
| 2174 | } |
Chris Lattner | b6f8e82 | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 2175 | case MatchableInfo::ResOperand::ImmOperand: { |
| 2176 | int64_t Val = OpInfo.ImmVal; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2177 | std::string Ty = "imm_" + itostr(Val); |
Hal Finkel | f909072 | 2015-01-15 01:33:00 +0000 | [diff] [blame] | 2178 | Ty = getEnumNameForToken(Ty); |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2179 | Signature += "__" + Ty; |
| 2180 | |
| 2181 | std::string Name = "CVT_" + Ty; |
| 2182 | bool IsNewConverter = false; |
| 2183 | unsigned ID = getConverterOperandID(Name, OperandConversionKinds, |
| 2184 | IsNewConverter); |
| 2185 | // Add the operand entry to the instruction kind conversion row. |
| 2186 | ConversionRow.push_back(ID); |
| 2187 | ConversionRow.push_back(0); |
| 2188 | |
| 2189 | if (!IsNewConverter) |
| 2190 | break; |
| 2191 | |
| 2192 | CvtOS << " case " << Name << ":\n" |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 2193 | << " Inst.addOperand(MCOperand::createImm(" << Val << "));\n" |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2194 | << " break;\n"; |
| 2195 | |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2196 | OpOS << " case " << Name << ":\n" |
Chad Rosier | 2f480a8 | 2012-10-12 22:53:36 +0000 | [diff] [blame] | 2197 | << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n" |
| 2198 | << " Operands[*(p + 1)]->setConstraint(\"\");\n" |
Chad Rosier | f4e35dc | 2012-10-01 23:45:51 +0000 | [diff] [blame] | 2199 | << " ++NumMCOperands;\n" |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2200 | << " break;\n"; |
Chris Lattner | b6f8e82 | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 2201 | break; |
| 2202 | } |
Chris Lattner | 4869d34 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 2203 | case MatchableInfo::ResOperand::RegOperand: { |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2204 | std::string Reg, Name; |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 2205 | if (!OpInfo.Register) { |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2206 | Name = "reg0"; |
| 2207 | Reg = "0"; |
Bob Wilson | 03912ab | 2011-01-14 22:58:09 +0000 | [diff] [blame] | 2208 | } else { |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2209 | Reg = getQualifiedName(OpInfo.Register); |
Matthias Braun | 4a86d45 | 2016-12-04 05:48:16 +0000 | [diff] [blame] | 2210 | Name = "reg" + OpInfo.Register->getName().str(); |
Bob Wilson | 03912ab | 2011-01-14 22:58:09 +0000 | [diff] [blame] | 2211 | } |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2212 | Signature += "__" + Name; |
| 2213 | Name = "CVT_" + Name; |
| 2214 | bool IsNewConverter = false; |
| 2215 | unsigned ID = getConverterOperandID(Name, OperandConversionKinds, |
| 2216 | IsNewConverter); |
| 2217 | // Add the operand entry to the instruction kind conversion row. |
| 2218 | ConversionRow.push_back(ID); |
| 2219 | ConversionRow.push_back(0); |
| 2220 | |
| 2221 | if (!IsNewConverter) |
| 2222 | break; |
| 2223 | CvtOS << " case " << Name << ":\n" |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 2224 | << " Inst.addOperand(MCOperand::createReg(" << Reg << "));\n" |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2225 | << " break;\n"; |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2226 | |
| 2227 | OpOS << " case " << Name << ":\n" |
Chad Rosier | 2f480a8 | 2012-10-12 22:53:36 +0000 | [diff] [blame] | 2228 | << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n" |
| 2229 | << " Operands[*(p + 1)]->setConstraint(\"m\");\n" |
Chad Rosier | f4e35dc | 2012-10-01 23:45:51 +0000 | [diff] [blame] | 2230 | << " ++NumMCOperands;\n" |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2231 | << " break;\n"; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2232 | } |
Chris Lattner | 743081d | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 2233 | } |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 2234 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2235 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2236 | // If there were no operands, add to the signature to that effect |
| 2237 | if (Signature == "Convert") |
| 2238 | Signature += "_NoOperands"; |
| 2239 | |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 2240 | II->ConversionFnKind = Signature; |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 2241 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2242 | // Save the signature. If we already have it, don't add a new row |
| 2243 | // to the table. |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 2244 | if (!InstructionConversionKinds.insert(CachedHashString(Signature))) |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 2245 | continue; |
| 2246 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2247 | // Add the row to the table. |
Craig Topper | c4de7ee | 2015-08-16 21:27:08 +0000 | [diff] [blame] | 2248 | ConversionTable.push_back(std::move(ConversionRow)); |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 2249 | } |
Daniel Dunbar | 7133028 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 2250 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2251 | // Finish up the converter driver function. |
Chad Rosier | c38826c | 2012-09-03 17:39:57 +0000 | [diff] [blame] | 2252 | CvtOS << " }\n }\n}\n\n"; |
Daniel Dunbar | 7133028 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 2253 | |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2254 | // Finish up the operand number lookup function. |
Chad Rosier | f4e35dc | 2012-10-01 23:45:51 +0000 | [diff] [blame] | 2255 | OpOS << " }\n }\n}\n\n"; |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2256 | |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2257 | // Output a static table for tied operands. |
| 2258 | if (TiedOperandsEnumMap.size()) { |
| 2259 | // The number of tied operand combinations will be small in practice, |
| 2260 | // but just add the assert to be sure. |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 2261 | assert(TiedOperandsEnumMap.size() <= 254 && |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2262 | "Too many tied-operand combinations to reference with " |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 2263 | "an 8bit offset from the conversion table, where index " |
| 2264 | "'255' is reserved as operand not to be copied."); |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2265 | |
| 2266 | OS << "enum {\n"; |
| 2267 | for (auto &KV : TiedOperandsEnumMap) { |
| 2268 | OS << " " << KV.second << ",\n"; |
| 2269 | } |
| 2270 | OS << "};\n\n"; |
| 2271 | |
Craig Topper | 88c142b | 2018-06-18 16:17:46 +0000 | [diff] [blame] | 2272 | OS << "static const uint8_t TiedAsmOperandTable[][3] = {\n"; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2273 | for (auto &KV : TiedOperandsEnumMap) { |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 2274 | OS << " /* " << KV.second << " */ { " |
| 2275 | << utostr(std::get<0>(KV.first)) << ", " |
| 2276 | << utostr(std::get<1>(KV.first)) << ", " |
| 2277 | << utostr(std::get<2>(KV.first)) << " },\n"; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2278 | } |
| 2279 | OS << "};\n\n"; |
| 2280 | } else |
Craig Topper | 88c142b | 2018-06-18 16:17:46 +0000 | [diff] [blame] | 2281 | OS << "static const uint8_t TiedAsmOperandTable[][3] = " |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 2282 | "{ /* empty */ {0, 0, 0} };\n\n"; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2283 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2284 | OS << "namespace {\n"; |
Daniel Dunbar | 7133028 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 2285 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2286 | // Output the operand conversion kind enum. |
| 2287 | OS << "enum OperatorConversionKind {\n"; |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 2288 | for (const auto &Converter : OperandConversionKinds) |
Craig Topper | 6e526f1 | 2016-01-03 07:33:30 +0000 | [diff] [blame] | 2289 | OS << " " << Converter << ",\n"; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2290 | OS << " CVT_NUM_CONVERTERS\n"; |
Daniel Dunbar | 7133028 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 2291 | OS << "};\n\n"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2292 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2293 | // Output the instruction conversion kind enum. |
| 2294 | OS << "enum InstructionConversionKind {\n"; |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 2295 | for (const auto &Signature : InstructionConversionKinds) |
Craig Topper | 802d3d3 | 2015-08-16 21:27:10 +0000 | [diff] [blame] | 2296 | OS << " " << Signature << ",\n"; |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2297 | OS << " CVT_NUM_SIGNATURES\n"; |
| 2298 | OS << "};\n\n"; |
| 2299 | |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2300 | OS << "} // end anonymous namespace\n\n"; |
| 2301 | |
| 2302 | // Output the conversion table. |
Craig Topper | 9150610 | 2012-09-18 01:41:49 +0000 | [diff] [blame] | 2303 | OS << "static const uint8_t ConversionTable[CVT_NUM_SIGNATURES][" |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2304 | << MaxRowLength << "] = {\n"; |
| 2305 | |
| 2306 | for (unsigned Row = 0, ERow = ConversionTable.size(); Row != ERow; ++Row) { |
| 2307 | assert(ConversionTable[Row].size() % 2 == 0 && "bad conversion row!"); |
| 2308 | OS << " // " << InstructionConversionKinds[Row] << "\n"; |
| 2309 | OS << " { "; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2310 | for (unsigned i = 0, e = ConversionTable[Row].size(); i != e; i += 2) { |
| 2311 | OS << OperandConversionKinds[ConversionTable[Row][i]] << ", "; |
| 2312 | if (OperandConversionKinds[ConversionTable[Row][i]] != |
| 2313 | CachedHashString("CVT_Tied")) { |
| 2314 | OS << (unsigned)(ConversionTable[Row][i + 1]) << ", "; |
| 2315 | continue; |
| 2316 | } |
| 2317 | |
| 2318 | // For a tied operand, emit a reference to the TiedAsmOperandTable |
| 2319 | // that contains the operand to copy, and the parsed operands to |
| 2320 | // check for their tied constraints. |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 2321 | auto Key = std::make_tuple((uint8_t)ConversionTable[Row][i + 1], |
| 2322 | (uint8_t)ConversionTable[Row][i + 2], |
| 2323 | (uint8_t)ConversionTable[Row][i + 3]); |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 2324 | auto TiedOpndEnum = TiedOperandsEnumMap.find(Key); |
| 2325 | assert(TiedOpndEnum != TiedOperandsEnumMap.end() && |
| 2326 | "No record for tied operand pair"); |
| 2327 | OS << TiedOpndEnum->second << ", "; |
| 2328 | i += 2; |
| 2329 | } |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2330 | OS << "CVT_Done },\n"; |
| 2331 | } |
| 2332 | |
| 2333 | OS << "};\n\n"; |
| 2334 | |
| 2335 | // Spit out the conversion driver function. |
Daniel Dunbar | 7133028 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 2336 | OS << CvtOS.str(); |
Jim Grosbach | c93f6c7 | 2012-08-22 01:06:23 +0000 | [diff] [blame] | 2337 | |
Chad Rosier | 738ea25 | 2012-08-30 17:59:25 +0000 | [diff] [blame] | 2338 | // Spit out the operand number lookup function. |
| 2339 | OS << OpOS.str(); |
Craig Topper | b64f915 | 2019-04-02 20:52:04 +0000 | [diff] [blame^] | 2340 | |
| 2341 | return ConversionTable.size(); |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 2344 | /// emitMatchClassEnumeration - Emit the enumeration for match class kinds. |
| 2345 | static void emitMatchClassEnumeration(CodeGenTarget &Target, |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2346 | std::forward_list<ClassInfo> &Infos, |
| 2347 | raw_ostream &OS) { |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2348 | OS << "namespace {\n\n"; |
| 2349 | |
| 2350 | OS << "/// MatchClassKind - The kinds of classes which participate in\n" |
| 2351 | << "/// instruction matching.\n"; |
| 2352 | OS << "enum MatchClassKind {\n"; |
| 2353 | OS << " InvalidMatchClass = 0,\n"; |
Tom Stellard | b9f235e | 2016-02-05 19:59:33 +0000 | [diff] [blame] | 2354 | OS << " OptionalMatchClass = 1,\n"; |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 2355 | ClassInfo::ClassInfoKind LastKind = ClassInfo::Token; |
| 2356 | StringRef LastName = "OptionalMatchClass"; |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 2357 | for (const auto &CI : Infos) { |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 2358 | if (LastKind == ClassInfo::Token && CI.Kind != ClassInfo::Token) { |
| 2359 | OS << " MCK_LAST_TOKEN = " << LastName << ",\n"; |
| 2360 | } else if (LastKind < ClassInfo::UserClass0 && |
| 2361 | CI.Kind >= ClassInfo::UserClass0) { |
| 2362 | OS << " MCK_LAST_REGISTER = " << LastName << ",\n"; |
| 2363 | } |
| 2364 | LastKind = (ClassInfo::ClassInfoKind)CI.Kind; |
| 2365 | LastName = CI.Name; |
| 2366 | |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2367 | OS << " " << CI.Name << ", // "; |
| 2368 | if (CI.Kind == ClassInfo::Token) { |
| 2369 | OS << "'" << CI.ValueName << "'\n"; |
| 2370 | } else if (CI.isRegisterClass()) { |
| 2371 | if (!CI.ValueName.empty()) |
| 2372 | OS << "register class '" << CI.ValueName << "'\n"; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2373 | else |
| 2374 | OS << "derived register class\n"; |
| 2375 | } else { |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2376 | OS << "user defined class '" << CI.ValueName << "'\n"; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2377 | } |
| 2378 | } |
| 2379 | OS << " NumMatchClassKinds\n"; |
| 2380 | OS << "};\n\n"; |
| 2381 | |
| 2382 | OS << "}\n\n"; |
| 2383 | } |
| 2384 | |
Oliver Stannard | 41dfac3 | 2017-10-03 14:34:57 +0000 | [diff] [blame] | 2385 | /// emitMatchClassDiagStrings - Emit a function to get the diagnostic text to be |
| 2386 | /// used when an assembly operand does not match the expected operand class. |
| 2387 | static void emitOperandMatchErrorDiagStrings(AsmMatcherInfo &Info, raw_ostream &OS) { |
| 2388 | // If the target does not use DiagnosticString for any operands, don't emit |
| 2389 | // an unused function. |
| 2390 | if (std::all_of( |
| 2391 | Info.Classes.begin(), Info.Classes.end(), |
| 2392 | [](const ClassInfo &CI) { return CI.DiagnosticString.empty(); })) |
| 2393 | return; |
| 2394 | |
| 2395 | OS << "static const char *getMatchKindDiag(" << Info.Target.getName() |
| 2396 | << "AsmParser::" << Info.Target.getName() |
| 2397 | << "MatchResultTy MatchResult) {\n"; |
| 2398 | OS << " switch (MatchResult) {\n"; |
| 2399 | |
| 2400 | for (const auto &CI: Info.Classes) { |
| 2401 | if (!CI.DiagnosticString.empty()) { |
| 2402 | assert(!CI.DiagnosticType.empty() && |
| 2403 | "DiagnosticString set without DiagnosticType"); |
| 2404 | OS << " case " << Info.Target.getName() |
| 2405 | << "AsmParser::Match_" << CI.DiagnosticType << ":\n"; |
| 2406 | OS << " return \"" << CI.DiagnosticString << "\";\n"; |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | OS << " default:\n"; |
| 2411 | OS << " return nullptr;\n"; |
| 2412 | |
| 2413 | OS << " }\n"; |
| 2414 | OS << "}\n\n"; |
| 2415 | } |
| 2416 | |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 2417 | static void emitRegisterMatchErrorFunc(AsmMatcherInfo &Info, raw_ostream &OS) { |
| 2418 | OS << "static unsigned getDiagKindFromRegisterClass(MatchClassKind " |
| 2419 | "RegisterClass) {\n"; |
Fangrui Song | 2e83b2e | 2018-10-19 06:12:02 +0000 | [diff] [blame] | 2420 | if (none_of(Info.Classes, [](const ClassInfo &CI) { |
| 2421 | return CI.isRegisterClass() && !CI.DiagnosticType.empty(); |
| 2422 | })) { |
Oliver Stannard | dab5212 | 2017-10-12 09:28:23 +0000 | [diff] [blame] | 2423 | OS << " return MCTargetAsmParser::Match_InvalidOperand;\n"; |
| 2424 | } else { |
| 2425 | OS << " switch (RegisterClass) {\n"; |
| 2426 | for (const auto &CI: Info.Classes) { |
| 2427 | if (CI.isRegisterClass() && !CI.DiagnosticType.empty()) { |
| 2428 | OS << " case " << CI.Name << ":\n"; |
| 2429 | OS << " return " << Info.Target.getName() << "AsmParser::Match_" |
| 2430 | << CI.DiagnosticType << ";\n"; |
| 2431 | } |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 2432 | } |
Oliver Stannard | dab5212 | 2017-10-12 09:28:23 +0000 | [diff] [blame] | 2433 | |
| 2434 | OS << " default:\n"; |
| 2435 | OS << " return MCTargetAsmParser::Match_InvalidOperand;\n"; |
| 2436 | |
| 2437 | OS << " }\n"; |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 2438 | } |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 2439 | OS << "}\n\n"; |
| 2440 | } |
| 2441 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 2442 | /// emitValidateOperandClass - Emit the function to validate an operand class. |
| 2443 | static void emitValidateOperandClass(AsmMatcherInfo &Info, |
Jim Grosbach | 6e2e29b | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 2444 | raw_ostream &OS) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 2445 | OS << "static unsigned validateOperandClass(MCParsedAsmOperand &GOp, " |
Jim Grosbach | 6e2e29b | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 2446 | << "MatchClassKind Kind) {\n"; |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 2447 | OS << " " << Info.Target.getName() << "Operand &Operand = (" |
| 2448 | << Info.Target.getName() << "Operand&)GOp;\n"; |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 2449 | |
Kevin Enderby | 1b87c80 | 2011-07-15 18:30:43 +0000 | [diff] [blame] | 2450 | // The InvalidMatchClass is not to match any operand. |
| 2451 | OS << " if (Kind == InvalidMatchClass)\n"; |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 2452 | OS << " return MCTargetAsmParser::Match_InvalidOperand;\n\n"; |
Kevin Enderby | 1b87c80 | 2011-07-15 18:30:43 +0000 | [diff] [blame] | 2453 | |
Jim Grosbach | 6e2e29b | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 2454 | // Check for Token operands first. |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 2455 | // FIXME: Use a more specific diagnostic type. |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 2456 | OS << " if (Operand.isToken() && Kind <= MCK_LAST_TOKEN)\n"; |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 2457 | OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind) ?\n" |
| 2458 | << " MCTargetAsmParser::Match_Success :\n" |
| 2459 | << " MCTargetAsmParser::Match_InvalidOperand;\n\n"; |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 2460 | |
Jim Grosbach | 6e2e29b | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 2461 | // Check the user classes. We don't care what order since we're only |
| 2462 | // actually matching against one of them. |
Valery Pykhtin | 020c29e | 2016-04-05 16:18:16 +0000 | [diff] [blame] | 2463 | OS << " switch (Kind) {\n" |
| 2464 | " default: break;\n"; |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 2465 | for (const auto &CI : Info.Classes) { |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2466 | if (!CI.isUserClass()) |
Daniel Dunbar | bb98db2 | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 2467 | continue; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2468 | |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2469 | OS << " // '" << CI.ClassName << "' class\n"; |
Sander de Smalen | a2fb1d1 | 2018-04-26 09:24:45 +0000 | [diff] [blame] | 2470 | OS << " case " << CI.Name << ": {\n"; |
| 2471 | OS << " DiagnosticPredicate DP(Operand." << CI.PredicateMethod |
| 2472 | << "());\n"; |
| 2473 | OS << " if (DP.isMatch())\n"; |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 2474 | OS << " return MCTargetAsmParser::Match_Success;\n"; |
Sander de Smalen | a2fb1d1 | 2018-04-26 09:24:45 +0000 | [diff] [blame] | 2475 | if (!CI.DiagnosticType.empty()) { |
| 2476 | OS << " if (DP.isNearMatch())\n"; |
| 2477 | OS << " return " << Info.Target.getName() << "AsmParser::Match_" |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2478 | << CI.DiagnosticType << ";\n"; |
Sander de Smalen | a2fb1d1 | 2018-04-26 09:24:45 +0000 | [diff] [blame] | 2479 | OS << " break;\n"; |
| 2480 | } |
Valery Pykhtin | 020c29e | 2016-04-05 16:18:16 +0000 | [diff] [blame] | 2481 | else |
| 2482 | OS << " break;\n"; |
Sander de Smalen | a2fb1d1 | 2018-04-26 09:24:45 +0000 | [diff] [blame] | 2483 | OS << " }\n"; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2484 | } |
Valery Pykhtin | 020c29e | 2016-04-05 16:18:16 +0000 | [diff] [blame] | 2485 | OS << " } // end switch (Kind)\n\n"; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2486 | |
Owen Anderson | 8a503f2 | 2012-07-16 23:20:09 +0000 | [diff] [blame] | 2487 | // Check for register operands, including sub-classes. |
| 2488 | OS << " if (Operand.isReg()) {\n"; |
| 2489 | OS << " MatchClassKind OpKind;\n"; |
| 2490 | OS << " switch (Operand.getReg()) {\n"; |
| 2491 | OS << " default: OpKind = InvalidMatchClass; break;\n"; |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 2492 | for (const auto &RC : Info.RegisterClasses) |
Craig Topper | 2b347eb | 2017-07-07 05:19:25 +0000 | [diff] [blame] | 2493 | OS << " case " << RC.first->getValueAsString("Namespace") << "::" |
Craig Topper | 03ec801 | 2014-11-25 20:11:31 +0000 | [diff] [blame] | 2494 | << RC.first->getName() << ": OpKind = " << RC.second->Name |
Owen Anderson | 8a503f2 | 2012-07-16 23:20:09 +0000 | [diff] [blame] | 2495 | << "; break;\n"; |
| 2496 | OS << " }\n"; |
| 2497 | OS << " return isSubclass(OpKind, Kind) ? " |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 2498 | << "(unsigned)MCTargetAsmParser::Match_Success :\n " |
| 2499 | << " getDiagKindFromRegisterClass(Kind);\n }\n\n"; |
| 2500 | |
| 2501 | // Expected operand is a register, but actual is not. |
| 2502 | OS << " if (Kind > MCK_LAST_TOKEN && Kind <= MCK_LAST_REGISTER)\n"; |
| 2503 | OS << " return getDiagKindFromRegisterClass(Kind);\n\n"; |
Owen Anderson | 8a503f2 | 2012-07-16 23:20:09 +0000 | [diff] [blame] | 2504 | |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 2505 | // Generic fallthrough match failure case for operands that don't have |
| 2506 | // specialized diagnostic types. |
| 2507 | OS << " return MCTargetAsmParser::Match_InvalidOperand;\n"; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2508 | OS << "}\n\n"; |
| 2509 | } |
| 2510 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 2511 | /// emitIsSubclass - Emit the subclass predicate function. |
| 2512 | static void emitIsSubclass(CodeGenTarget &Target, |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2513 | std::forward_list<ClassInfo> &Infos, |
Daniel Dunbar | 2587b61 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 2514 | raw_ostream &OS) { |
Dmitri Gribenko | 8d30240 | 2012-09-15 20:22:05 +0000 | [diff] [blame] | 2515 | OS << "/// isSubclass - Compute whether \\p A is a subclass of \\p B.\n"; |
Jim Grosbach | 1f5c5aa | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 2516 | OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n"; |
Daniel Dunbar | 2587b61 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 2517 | OS << " if (A == B)\n"; |
| 2518 | OS << " return true;\n\n"; |
| 2519 | |
Craig Topper | 39311c7 | 2015-12-30 06:00:22 +0000 | [diff] [blame] | 2520 | bool EmittedSwitch = false; |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 2521 | for (const auto &A : Infos) { |
Jim Grosbach | ba39592 | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 2522 | std::vector<StringRef> SuperClasses; |
Tom Stellard | b9f235e | 2016-02-05 19:59:33 +0000 | [diff] [blame] | 2523 | if (A.IsOptional) |
| 2524 | SuperClasses.push_back("OptionalMatchClass"); |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 2525 | for (const auto &B : Infos) { |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2526 | if (&A != &B && A.isSubsetOf(B)) |
| 2527 | SuperClasses.push_back(B.Name); |
Daniel Dunbar | 2587b61 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 2528 | } |
Jim Grosbach | ba39592 | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 2529 | |
| 2530 | if (SuperClasses.empty()) |
| 2531 | continue; |
| 2532 | |
Craig Topper | 39311c7 | 2015-12-30 06:00:22 +0000 | [diff] [blame] | 2533 | // If this is the first SuperClass, emit the switch header. |
| 2534 | if (!EmittedSwitch) { |
Craig Topper | 13b2a4e | 2015-12-30 06:00:24 +0000 | [diff] [blame] | 2535 | OS << " switch (A) {\n"; |
Craig Topper | 39311c7 | 2015-12-30 06:00:22 +0000 | [diff] [blame] | 2536 | OS << " default:\n"; |
| 2537 | OS << " return false;\n"; |
| 2538 | EmittedSwitch = true; |
| 2539 | } |
| 2540 | |
| 2541 | OS << "\n case " << A.Name << ":\n"; |
Jim Grosbach | ba39592 | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 2542 | |
| 2543 | if (SuperClasses.size() == 1) { |
Craig Topper | 13b2a4e | 2015-12-30 06:00:24 +0000 | [diff] [blame] | 2544 | OS << " return B == " << SuperClasses.back() << ";\n"; |
Jim Grosbach | ba39592 | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 2545 | continue; |
| 2546 | } |
| 2547 | |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 2548 | if (!SuperClasses.empty()) { |
Craig Topper | 39311c7 | 2015-12-30 06:00:22 +0000 | [diff] [blame] | 2549 | OS << " switch (B) {\n"; |
| 2550 | OS << " default: return false;\n"; |
Craig Topper | 77bd2b7 | 2015-12-30 06:00:20 +0000 | [diff] [blame] | 2551 | for (StringRef SC : SuperClasses) |
Craig Topper | 39311c7 | 2015-12-30 06:00:22 +0000 | [diff] [blame] | 2552 | OS << " case " << SC << ": return true;\n"; |
| 2553 | OS << " }\n"; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 2554 | } else { |
| 2555 | // No case statement to emit |
Craig Topper | 39311c7 | 2015-12-30 06:00:22 +0000 | [diff] [blame] | 2556 | OS << " return false;\n"; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 2557 | } |
Daniel Dunbar | 2587b61 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 2558 | } |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 2559 | |
Craig Topper | 39311c7 | 2015-12-30 06:00:22 +0000 | [diff] [blame] | 2560 | // If there were case statements emitted into the string stream write the |
| 2561 | // default. |
Craig Topper | f58323e | 2016-01-03 07:33:34 +0000 | [diff] [blame] | 2562 | if (EmittedSwitch) |
| 2563 | OS << " }\n"; |
| 2564 | else |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 2565 | OS << " return false;\n"; |
| 2566 | |
Daniel Dunbar | 2587b61 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 2567 | OS << "}\n\n"; |
| 2568 | } |
| 2569 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 2570 | /// emitMatchTokenString - Emit the function to match a token string to the |
Daniel Dunbar | 66f4f54 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 2571 | /// appropriate match class value. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 2572 | static void emitMatchTokenString(CodeGenTarget &Target, |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2573 | std::forward_list<ClassInfo> &Infos, |
Daniel Dunbar | 66f4f54 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 2574 | raw_ostream &OS) { |
| 2575 | // Construct the match list. |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 2576 | std::vector<StringMatcher::StringPair> Matches; |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 2577 | for (const auto &CI : Infos) { |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2578 | if (CI.Kind == ClassInfo::Token) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2579 | Matches.emplace_back(CI.ValueName, "return " + CI.Name + ";"); |
Daniel Dunbar | 66f4f54 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Jim Grosbach | 1f5c5aa | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 2582 | OS << "static MatchClassKind matchTokenString(StringRef Name) {\n"; |
Daniel Dunbar | 66f4f54 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 2583 | |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 2584 | StringMatcher("Name", Matches, OS).Emit(); |
Daniel Dunbar | 66f4f54 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 2585 | |
| 2586 | OS << " return InvalidMatchClass;\n"; |
| 2587 | OS << "}\n\n"; |
| 2588 | } |
Chris Lattner | 00e2e74 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 2589 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 2590 | /// emitMatchRegisterName - Emit the function to match a string to the target |
Daniel Dunbar | d0470d7 | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 2591 | /// specific register enum. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 2592 | static void emitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser, |
Daniel Dunbar | d0470d7 | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 2593 | raw_ostream &OS) { |
Daniel Dunbar | 66f4f54 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 2594 | // Construct the match list. |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 2595 | std::vector<StringMatcher::StringPair> Matches; |
David Blaikie | 9b613db | 2014-11-29 18:13:39 +0000 | [diff] [blame] | 2596 | const auto &Regs = Target.getRegBank().getRegisters(); |
| 2597 | for (const CodeGenRegister &Reg : Regs) { |
| 2598 | if (Reg.TheDef->getValueAsString("AsmName").empty()) |
Daniel Dunbar | e2eec05 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 2599 | continue; |
| 2600 | |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2601 | Matches.emplace_back(Reg.TheDef->getValueAsString("AsmName"), |
| 2602 | "return " + utostr(Reg.EnumValue) + ";"); |
Daniel Dunbar | e2eec05 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 2603 | } |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2604 | |
Chris Lattner | 60db0a6 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 2605 | OS << "static unsigned MatchRegisterName(StringRef Name) {\n"; |
Daniel Dunbar | 66f4f54 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 2606 | |
Alex Bradbury | d590c857 | 2017-12-07 09:51:55 +0000 | [diff] [blame] | 2607 | bool IgnoreDuplicates = |
| 2608 | AsmParser->getValueAsBit("AllowDuplicateRegisterNames"); |
| 2609 | StringMatcher("Name", Matches, OS).Emit(0, IgnoreDuplicates); |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2610 | |
Daniel Dunbar | 66f4f54 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 2611 | OS << " return 0;\n"; |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 2612 | OS << "}\n\n"; |
Daniel Dunbar | d0470d7 | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 2613 | } |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 2614 | |
Dylan McKay | bff960a | 2016-02-03 10:30:16 +0000 | [diff] [blame] | 2615 | /// Emit the function to match a string to the target |
| 2616 | /// specific register enum. |
| 2617 | static void emitMatchRegisterAltName(CodeGenTarget &Target, Record *AsmParser, |
| 2618 | raw_ostream &OS) { |
| 2619 | // Construct the match list. |
| 2620 | std::vector<StringMatcher::StringPair> Matches; |
| 2621 | const auto &Regs = Target.getRegBank().getRegisters(); |
| 2622 | for (const CodeGenRegister &Reg : Regs) { |
| 2623 | |
| 2624 | auto AltNames = Reg.TheDef->getValueAsListOfStrings("AltNames"); |
| 2625 | |
| 2626 | for (auto AltName : AltNames) { |
| 2627 | AltName = StringRef(AltName).trim(); |
| 2628 | |
| 2629 | // don't handle empty alternative names |
| 2630 | if (AltName.empty()) |
| 2631 | continue; |
| 2632 | |
| 2633 | Matches.emplace_back(AltName, |
| 2634 | "return " + utostr(Reg.EnumValue) + ";"); |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | OS << "static unsigned MatchRegisterAltName(StringRef Name) {\n"; |
| 2639 | |
Alex Bradbury | d590c857 | 2017-12-07 09:51:55 +0000 | [diff] [blame] | 2640 | bool IgnoreDuplicates = |
| 2641 | AsmParser->getValueAsBit("AllowDuplicateRegisterNames"); |
| 2642 | StringMatcher("Name", Matches, OS).Emit(0, IgnoreDuplicates); |
Dylan McKay | bff960a | 2016-02-03 10:30:16 +0000 | [diff] [blame] | 2643 | |
| 2644 | OS << " return 0;\n"; |
| 2645 | OS << "}\n\n"; |
| 2646 | } |
| 2647 | |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 2648 | /// emitOperandDiagnosticTypes - Emit the operand matching diagnostic types. |
| 2649 | static void emitOperandDiagnosticTypes(AsmMatcherInfo &Info, raw_ostream &OS) { |
| 2650 | // Get the set of diagnostic types from all of the operand classes. |
| 2651 | std::set<StringRef> Types; |
Craig Topper | 6e526f1 | 2016-01-03 07:33:30 +0000 | [diff] [blame] | 2652 | for (const auto &OpClassEntry : Info.AsmOperandClasses) { |
| 2653 | if (!OpClassEntry.second->DiagnosticType.empty()) |
| 2654 | Types.insert(OpClassEntry.second->DiagnosticType); |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 2655 | } |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 2656 | for (const auto &OpClassEntry : Info.RegisterClassClasses) { |
| 2657 | if (!OpClassEntry.second->DiagnosticType.empty()) |
| 2658 | Types.insert(OpClassEntry.second->DiagnosticType); |
| 2659 | } |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 2660 | |
| 2661 | if (Types.empty()) return; |
| 2662 | |
| 2663 | // Now emit the enum entries. |
Craig Topper | 6e526f1 | 2016-01-03 07:33:30 +0000 | [diff] [blame] | 2664 | for (StringRef Type : Types) |
| 2665 | OS << " Match_" << Type << ",\n"; |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 2666 | OS << " END_OPERAND_DIAGNOSTIC_TYPES\n"; |
| 2667 | } |
| 2668 | |
Jim Grosbach | 5117ef7 | 2012-04-24 22:40:08 +0000 | [diff] [blame] | 2669 | /// emitGetSubtargetFeatureName - Emit the helper function to get the |
| 2670 | /// user-level name for a subtarget feature. |
| 2671 | static void emitGetSubtargetFeatureName(AsmMatcherInfo &Info, raw_ostream &OS) { |
| 2672 | OS << "// User-level names for subtarget features that participate in\n" |
| 2673 | << "// instruction matching.\n" |
Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 2674 | << "static const char *getSubtargetFeatureName(uint64_t Val) {\n"; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 2675 | if (!Info.SubtargetFeatures.empty()) { |
Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 2676 | OS << " switch(Val) {\n"; |
Craig Topper | 42bd819 | 2014-11-28 03:53:00 +0000 | [diff] [blame] | 2677 | for (const auto &SF : Info.SubtargetFeatures) { |
David Blaikie | 9a9da99 | 2014-11-28 22:15:06 +0000 | [diff] [blame] | 2678 | const SubtargetFeatureInfo &SFI = SF.second; |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 2679 | // FIXME: Totally just a placeholder name to get the algorithm working. |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2680 | OS << " case " << SFI.getEnumBitName() << ": return \"" |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 2681 | << SFI.TheDef->getValueAsString("PredicateName") << "\";\n"; |
| 2682 | } |
| 2683 | OS << " default: return \"(unknown)\";\n"; |
| 2684 | OS << " }\n"; |
| 2685 | } else { |
| 2686 | // Nothing to emit, so skip the switch |
| 2687 | OS << " return \"(unknown)\";\n"; |
Jim Grosbach | 5117ef7 | 2012-04-24 22:40:08 +0000 | [diff] [blame] | 2688 | } |
Aaron Ballman | e59e358 | 2013-07-15 16:53:32 +0000 | [diff] [blame] | 2689 | OS << "}\n\n"; |
Jim Grosbach | 5117ef7 | 2012-04-24 22:40:08 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Chris Lattner | 4369007 | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 2692 | static std::string GetAliasRequiredFeatures(Record *R, |
| 2693 | const AsmMatcherInfo &Info) { |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2694 | std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates"); |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2695 | std::string Result; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2696 | |
| 2697 | if (ReqFeatures.empty()) |
| 2698 | return Result; |
| 2699 | |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2700 | for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) { |
David Blaikie | 9a9da99 | 2014-11-28 22:15:06 +0000 | [diff] [blame] | 2701 | const SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2702 | |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 2703 | if (!F) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 2704 | PrintFatalError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() + |
Chris Lattner | 517dc95 | 2010-11-01 02:09:21 +0000 | [diff] [blame] | 2705 | "' is not marked as an AssemblerPredicate!"); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2706 | |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2707 | if (i) |
| 2708 | Result += " && "; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2709 | |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2710 | Result += "Features.test(" + F->getEnumBitName() + ')'; |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2711 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2712 | |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2713 | return Result; |
| 2714 | } |
| 2715 | |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 2716 | static void emitMnemonicAliasVariant(raw_ostream &OS,const AsmMatcherInfo &Info, |
| 2717 | std::vector<Record*> &Aliases, |
| 2718 | unsigned Indent = 0, |
| 2719 | StringRef AsmParserVariantName = StringRef()){ |
Chris Lattner | cf9b6e3 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 2720 | // Keep track of all the aliases from a mnemonic. Use an std::map so that the |
| 2721 | // iteration order of the map is stable. |
| 2722 | std::map<std::string, std::vector<Record*> > AliasesFromMnemonic; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2723 | |
Craig Topper | 6e526f1 | 2016-01-03 07:33:30 +0000 | [diff] [blame] | 2724 | for (Record *R : Aliases) { |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 2725 | // FIXME: Allow AssemblerVariantName to be a comma separated list. |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 2726 | StringRef AsmVariantName = R->getValueAsString("AsmVariantName"); |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 2727 | if (AsmVariantName != AsmParserVariantName) |
| 2728 | continue; |
Chris Lattner | cf9b6e3 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 2729 | AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R); |
Chris Lattner | ba7b4fe | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 2730 | } |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 2731 | if (AliasesFromMnemonic.empty()) |
| 2732 | return; |
Vladimir Medic | 75429ad | 2013-07-16 09:22:38 +0000 | [diff] [blame] | 2733 | |
Chris Lattner | cf9b6e3 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 2734 | // Process each alias a "from" mnemonic at a time, building the code executed |
| 2735 | // by the string remapper. |
| 2736 | std::vector<StringMatcher::StringPair> Cases; |
Craig Topper | 6e526f1 | 2016-01-03 07:33:30 +0000 | [diff] [blame] | 2737 | for (const auto &AliasEntry : AliasesFromMnemonic) { |
| 2738 | const std::vector<Record*> &ToVec = AliasEntry.second; |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2739 | |
| 2740 | // Loop through each alias and emit code that handles each case. If there |
| 2741 | // are two instructions without predicates, emit an error. If there is one, |
| 2742 | // emit it last. |
| 2743 | std::string MatchCode; |
| 2744 | int AliasWithNoPredicate = -1; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2745 | |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2746 | for (unsigned i = 0, e = ToVec.size(); i != e; ++i) { |
| 2747 | Record *R = ToVec[i]; |
Chris Lattner | 4369007 | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 2748 | std::string FeatureMask = GetAliasRequiredFeatures(R, Info); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2749 | |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2750 | // If this unconditionally matches, remember it for later and diagnose |
| 2751 | // duplicates. |
| 2752 | if (FeatureMask.empty()) { |
| 2753 | if (AliasWithNoPredicate != -1) { |
| 2754 | // We can't have two aliases from the same mnemonic with no predicate. |
| 2755 | PrintError(ToVec[AliasWithNoPredicate]->getLoc(), |
| 2756 | "two MnemonicAliases with the same 'from' mnemonic!"); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 2757 | PrintFatalError(R->getLoc(), "this is the other MnemonicAlias."); |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2758 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2759 | |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2760 | AliasWithNoPredicate = i; |
| 2761 | continue; |
| 2762 | } |
Craig Topper | 6e526f1 | 2016-01-03 07:33:30 +0000 | [diff] [blame] | 2763 | if (R->getValueAsString("ToMnemonic") == AliasEntry.first) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 2764 | PrintFatalError(R->getLoc(), "MnemonicAlias to the same string"); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2765 | |
Chris Lattner | f9ec2fb | 2010-10-30 19:47:49 +0000 | [diff] [blame] | 2766 | if (!MatchCode.empty()) |
| 2767 | MatchCode += "else "; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2768 | MatchCode += "if (" + FeatureMask + ")\n"; |
Craig Topper | 2b8419a | 2017-05-31 19:01:11 +0000 | [diff] [blame] | 2769 | MatchCode += " Mnemonic = \""; |
| 2770 | MatchCode += R->getValueAsString("ToMnemonic"); |
| 2771 | MatchCode += "\";\n"; |
Chris Lattner | cf9b6e3 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 2772 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2773 | |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2774 | if (AliasWithNoPredicate != -1) { |
| 2775 | Record *R = ToVec[AliasWithNoPredicate]; |
Chris Lattner | f9ec2fb | 2010-10-30 19:47:49 +0000 | [diff] [blame] | 2776 | if (!MatchCode.empty()) |
| 2777 | MatchCode += "else\n "; |
Craig Topper | 2b8419a | 2017-05-31 19:01:11 +0000 | [diff] [blame] | 2778 | MatchCode += "Mnemonic = \""; |
| 2779 | MatchCode += R->getValueAsString("ToMnemonic"); |
| 2780 | MatchCode += "\";\n"; |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2781 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2782 | |
Chris Lattner | 2cb092d | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2783 | MatchCode += "return;"; |
| 2784 | |
Craig Topper | 6e526f1 | 2016-01-03 07:33:30 +0000 | [diff] [blame] | 2785 | Cases.push_back(std::make_pair(AliasEntry.first, MatchCode)); |
Chris Lattner | cf9b6e3 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 2786 | } |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 2787 | StringMatcher("Mnemonic", Cases, OS).Emit(Indent); |
| 2788 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2789 | |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 2790 | /// emitMnemonicAliases - If the target has any MnemonicAlias<> definitions, |
| 2791 | /// emit a function for them and return true, otherwise return false. |
| 2792 | static bool emitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info, |
| 2793 | CodeGenTarget &Target) { |
| 2794 | // Ignore aliases when match-prefix is set. |
| 2795 | if (!MatchPrefix.empty()) |
| 2796 | return false; |
| 2797 | |
| 2798 | std::vector<Record*> Aliases = |
| 2799 | Info.getRecords().getAllDerivedDefinitions("MnemonicAlias"); |
| 2800 | if (Aliases.empty()) return false; |
| 2801 | |
| 2802 | OS << "static void applyMnemonicAliases(StringRef &Mnemonic, " |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2803 | "const FeatureBitset &Features, unsigned VariantID) {\n"; |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 2804 | OS << " switch (VariantID) {\n"; |
| 2805 | unsigned VariantCount = Target.getAsmParserVariantCount(); |
| 2806 | for (unsigned VC = 0; VC != VariantCount; ++VC) { |
| 2807 | Record *AsmVariant = Target.getAsmParserVariant(VC); |
| 2808 | int AsmParserVariantNo = AsmVariant->getValueAsInt("Variant"); |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 2809 | StringRef AsmParserVariantName = AsmVariant->getValueAsString("Name"); |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 2810 | OS << " case " << AsmParserVariantNo << ":\n"; |
| 2811 | emitMnemonicAliasVariant(OS, Info, Aliases, /*Indent=*/2, |
| 2812 | AsmParserVariantName); |
| 2813 | OS << " break;\n"; |
| 2814 | } |
| 2815 | OS << " }\n"; |
| 2816 | |
| 2817 | // Emit aliases that apply to all variants. |
| 2818 | emitMnemonicAliasVariant(OS, Info, Aliases); |
| 2819 | |
Daniel Dunbar | e46bc4c | 2011-01-18 01:59:30 +0000 | [diff] [blame] | 2820 | OS << "}\n\n"; |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2821 | |
Chris Lattner | 477fba4f | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 2822 | return true; |
Chris Lattner | ba7b4fe | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 2825 | static void emitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target, |
Craig Topper | 7ecfa6d | 2012-09-18 07:02:21 +0000 | [diff] [blame] | 2826 | const AsmMatcherInfo &Info, StringRef ClassName, |
| 2827 | StringToOffsetTable &StringTable, |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2828 | unsigned MaxMnemonicIndex, |
| 2829 | unsigned MaxFeaturesIndex, |
| 2830 | bool HasMnemonicFirst) { |
Craig Topper | 7ecfa6d | 2012-09-18 07:02:21 +0000 | [diff] [blame] | 2831 | unsigned MaxMask = 0; |
Craig Topper | 869cd5f | 2015-12-31 08:18:20 +0000 | [diff] [blame] | 2832 | for (const OperandMatchEntry &OMI : Info.OperandMatchInfo) { |
| 2833 | MaxMask |= OMI.OperandMask; |
Craig Topper | 7ecfa6d | 2012-09-18 07:02:21 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2836 | // Emit the static custom operand parsing table; |
| 2837 | OS << "namespace {\n"; |
| 2838 | OS << " struct OperandMatchEntry {\n"; |
Craig Topper | 7ecfa6d | 2012-09-18 07:02:21 +0000 | [diff] [blame] | 2839 | OS << " " << getMinimalTypeForRange(MaxMnemonicIndex) |
| 2840 | << " Mnemonic;\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2841 | OS << " " << getMinimalTypeForRange(MaxMask) |
| 2842 | << " OperandMask;\n"; |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2843 | OS << " " << getMinimalTypeForRange(std::distance( |
| 2844 | Info.Classes.begin(), Info.Classes.end())) << " Class;\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2845 | OS << " " << getMinimalTypeForRange(MaxFeaturesIndex) |
| 2846 | << " RequiredFeaturesIdx;\n\n"; |
Benjamin Kramer | 0764a3f | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2847 | OS << " StringRef getMnemonic() const {\n"; |
| 2848 | OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n"; |
| 2849 | OS << " MnemonicTable[Mnemonic]);\n"; |
| 2850 | OS << " }\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2851 | OS << " };\n\n"; |
| 2852 | |
| 2853 | OS << " // Predicate for searching for an opcode.\n"; |
| 2854 | OS << " struct LessOpcodeOperand {\n"; |
| 2855 | OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n"; |
Benjamin Kramer | 0764a3f | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2856 | OS << " return LHS.getMnemonic() < RHS;\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2857 | OS << " }\n"; |
| 2858 | OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n"; |
Benjamin Kramer | 0764a3f | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2859 | OS << " return LHS < RHS.getMnemonic();\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2860 | OS << " }\n"; |
| 2861 | OS << " bool operator()(const OperandMatchEntry &LHS,"; |
| 2862 | OS << " const OperandMatchEntry &RHS) {\n"; |
Benjamin Kramer | 0764a3f | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2863 | OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2864 | OS << " }\n"; |
| 2865 | OS << " };\n"; |
| 2866 | |
| 2867 | OS << "} // end anonymous namespace.\n\n"; |
| 2868 | |
| 2869 | OS << "static const OperandMatchEntry OperandMatchTable[" |
| 2870 | << Info.OperandMatchInfo.size() << "] = {\n"; |
| 2871 | |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2872 | OS << " /* Operand List Mnemonic, Mask, Operand Class, Features */\n"; |
Craig Topper | 869cd5f | 2015-12-31 08:18:20 +0000 | [diff] [blame] | 2873 | for (const OperandMatchEntry &OMI : Info.OperandMatchInfo) { |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2874 | const MatchableInfo &II = *OMI.MI; |
| 2875 | |
Craig Topper | 7ecfa6d | 2012-09-18 07:02:21 +0000 | [diff] [blame] | 2876 | OS << " { "; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2877 | |
Craig Topper | 7ecfa6d | 2012-09-18 07:02:21 +0000 | [diff] [blame] | 2878 | // Store a pascal-style length byte in the mnemonic. |
| 2879 | std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str(); |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2880 | OS << StringTable.GetOrAddStringOffset(LenMnemonic, false) |
Craig Topper | 7ecfa6d | 2012-09-18 07:02:21 +0000 | [diff] [blame] | 2881 | << " /* " << II.Mnemonic << " */, "; |
| 2882 | |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2883 | OS << OMI.OperandMask; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2884 | OS << " /* "; |
| 2885 | bool printComma = false; |
| 2886 | for (int i = 0, e = 31; i !=e; ++i) |
| 2887 | if (OMI.OperandMask & (1 << i)) { |
| 2888 | if (printComma) |
| 2889 | OS << ", "; |
| 2890 | OS << i; |
| 2891 | printComma = true; |
| 2892 | } |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2893 | OS << " */, "; |
| 2894 | |
| 2895 | OS << OMI.CI->Name; |
| 2896 | |
| 2897 | // Write the required features mask. |
| 2898 | OS << ", AMFBS"; |
| 2899 | if (II.RequiredFeatures.empty()) |
| 2900 | OS << "_None"; |
| 2901 | else |
| 2902 | for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) |
| 2903 | OS << '_' << II.RequiredFeatures[i]->TheDef->getName(); |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2904 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2905 | OS << " },\n"; |
| 2906 | } |
| 2907 | OS << "};\n\n"; |
| 2908 | |
| 2909 | // Emit the operand class switch to call the correct custom parser for |
| 2910 | // the found operand class. |
Alex Bradbury | 58eba09 | 2016-11-01 16:32:05 +0000 | [diff] [blame] | 2911 | OS << "OperandMatchResultTy " << Target.getName() << ClassName << "::\n" |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 2912 | << "tryCustomParseOperand(OperandVector" |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2913 | << " &Operands,\n unsigned MCK) {\n\n" |
| 2914 | << " switch(MCK) {\n"; |
| 2915 | |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 2916 | for (const auto &CI : Info.Classes) { |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2917 | if (CI.ParserMethod.empty()) |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2918 | continue; |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 2919 | OS << " case " << CI.Name << ":\n" |
| 2920 | << " return " << CI.ParserMethod << "(Operands);\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | OS << " default:\n"; |
Jim Grosbach | 861e49c | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2924 | OS << " return MatchOperand_NoMatch;\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2925 | OS << " }\n"; |
Jim Grosbach | 861e49c | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2926 | OS << " return MatchOperand_NoMatch;\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2927 | OS << "}\n\n"; |
| 2928 | |
| 2929 | // Emit the static custom operand parser. This code is very similar with |
| 2930 | // the other matcher. Also use MatchResultTy here just in case we go for |
| 2931 | // a better error handling. |
Alex Bradbury | 58eba09 | 2016-11-01 16:32:05 +0000 | [diff] [blame] | 2932 | OS << "OperandMatchResultTy " << Target.getName() << ClassName << "::\n" |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 2933 | << "MatchOperandParserImpl(OperandVector" |
Sander de Smalen | cd6be96 | 2017-12-20 11:02:42 +0000 | [diff] [blame] | 2934 | << " &Operands,\n StringRef Mnemonic,\n" |
| 2935 | << " bool ParseForAllFeatures) {\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2936 | |
| 2937 | // Emit code to get the available features. |
| 2938 | OS << " // Get the current feature set.\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2939 | OS << " const FeatureBitset &AvailableFeatures = getAvailableFeatures();\n\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2940 | |
| 2941 | OS << " // Get the next operand index.\n"; |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 2942 | OS << " unsigned NextOpNum = Operands.size()" |
| 2943 | << (HasMnemonicFirst ? " - 1" : "") << ";\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2944 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2945 | // Emit code to search the table. |
| 2946 | OS << " // Search the table.\n"; |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 2947 | if (HasMnemonicFirst) { |
| 2948 | OS << " auto MnemonicRange =\n"; |
| 2949 | OS << " std::equal_range(std::begin(OperandMatchTable), " |
| 2950 | "std::end(OperandMatchTable),\n"; |
| 2951 | OS << " Mnemonic, LessOpcodeOperand());\n\n"; |
| 2952 | } else { |
| 2953 | OS << " auto MnemonicRange = std::make_pair(std::begin(OperandMatchTable)," |
| 2954 | " std::end(OperandMatchTable));\n"; |
| 2955 | OS << " if (!Mnemonic.empty())\n"; |
| 2956 | OS << " MnemonicRange =\n"; |
| 2957 | OS << " std::equal_range(std::begin(OperandMatchTable), " |
| 2958 | "std::end(OperandMatchTable),\n"; |
| 2959 | OS << " Mnemonic, LessOpcodeOperand());\n\n"; |
| 2960 | } |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2961 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2962 | OS << " if (MnemonicRange.first == MnemonicRange.second)\n"; |
Jim Grosbach | 861e49c | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2963 | OS << " return MatchOperand_NoMatch;\n\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2964 | |
| 2965 | OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n" |
| 2966 | << " *ie = MnemonicRange.second; it != ie; ++it) {\n"; |
| 2967 | |
| 2968 | OS << " // equal_range guarantees that instruction mnemonic matches.\n"; |
Benjamin Kramer | 0764a3f | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2969 | OS << " assert(Mnemonic == it->getMnemonic());\n\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2970 | |
| 2971 | // Emit check that the required features are available. |
| 2972 | OS << " // check if the available features match\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2973 | OS << " const FeatureBitset &RequiredFeatures = " |
| 2974 | "FeatureBitsets[it->RequiredFeaturesIdx];\n"; |
Sander de Smalen | cd6be96 | 2017-12-20 11:02:42 +0000 | [diff] [blame] | 2975 | OS << " if (!ParseForAllFeatures && (AvailableFeatures & " |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 2976 | "RequiredFeatures) != RequiredFeatures)\n"; |
Sander de Smalen | cd6be96 | 2017-12-20 11:02:42 +0000 | [diff] [blame] | 2977 | OS << " continue;\n\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2978 | |
| 2979 | // Emit check to ensure the operand number matches. |
| 2980 | OS << " // check if the operand in question has a custom parser.\n"; |
| 2981 | OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n"; |
| 2982 | OS << " continue;\n\n"; |
| 2983 | |
| 2984 | // Emit call to the custom parser method |
| 2985 | OS << " // call custom parse method to handle the operand\n"; |
Jim Grosbach | 861e49c | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2986 | OS << " OperandMatchResultTy Result = "; |
Jim Grosbach | 1f5c5aa | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 2987 | OS << "tryCustomParseOperand(Operands, it->Class);\n"; |
Jim Grosbach | 861e49c | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2988 | OS << " if (Result != MatchOperand_NoMatch)\n"; |
| 2989 | OS << " return Result;\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2990 | OS << " }\n\n"; |
| 2991 | |
Jim Grosbach | 861e49c | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2992 | OS << " // Okay, we had no match.\n"; |
| 2993 | OS << " return MatchOperand_NoMatch;\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2994 | OS << "}\n\n"; |
| 2995 | } |
| 2996 | |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 2997 | static void emitAsmTiedOperandConstraints(CodeGenTarget &Target, |
| 2998 | AsmMatcherInfo &Info, |
| 2999 | raw_ostream &OS) { |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 3000 | std::string AsmParserName = |
| 3001 | Info.AsmParser->getValueAsString("AsmParserClassName"); |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 3002 | OS << "static bool "; |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 3003 | OS << "checkAsmTiedOperandConstraints(const " << Target.getName() |
| 3004 | << AsmParserName << "&AsmParser,\n"; |
| 3005 | OS << " unsigned Kind,\n"; |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 3006 | OS << " const OperandVector &Operands,\n"; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 3007 | OS << " uint64_t &ErrorInfo) {\n"; |
| 3008 | OS << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n"; |
| 3009 | OS << " const uint8_t *Converter = ConversionTable[Kind];\n"; |
| 3010 | OS << " for (const uint8_t *p = Converter; *p; p+= 2) {\n"; |
| 3011 | OS << " switch (*p) {\n"; |
| 3012 | OS << " case CVT_Tied: {\n"; |
| 3013 | OS << " unsigned OpIdx = *(p+1);\n"; |
Simon Pilgrim | e4d40f9 | 2018-02-17 12:29:47 +0000 | [diff] [blame] | 3014 | OS << " assert(OpIdx < (size_t)(std::end(TiedAsmOperandTable) -\n"; |
| 3015 | OS << " std::begin(TiedAsmOperandTable)) &&\n"; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 3016 | OS << " \"Tied operand not found\");\n"; |
| 3017 | OS << " unsigned OpndNum1 = TiedAsmOperandTable[OpIdx][1];\n"; |
| 3018 | OS << " unsigned OpndNum2 = TiedAsmOperandTable[OpIdx][2];\n"; |
| 3019 | OS << " if (OpndNum1 != OpndNum2) {\n"; |
| 3020 | OS << " auto &SrcOp1 = Operands[OpndNum1];\n"; |
| 3021 | OS << " auto &SrcOp2 = Operands[OpndNum2];\n"; |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 3022 | OS << " if (SrcOp1->isReg() && SrcOp2->isReg()) {\n"; |
| 3023 | OS << " if (!AsmParser.regsEqual(*SrcOp1, *SrcOp2)) {\n"; |
| 3024 | OS << " ErrorInfo = OpndNum2;\n"; |
| 3025 | OS << " return false;\n"; |
| 3026 | OS << " }\n"; |
Sander de Smalen | 5b691a1 | 2018-02-04 16:24:17 +0000 | [diff] [blame] | 3027 | OS << " }\n"; |
| 3028 | OS << " }\n"; |
| 3029 | OS << " break;\n"; |
| 3030 | OS << " }\n"; |
| 3031 | OS << " default:\n"; |
| 3032 | OS << " break;\n"; |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 3033 | OS << " }\n"; |
| 3034 | OS << " }\n"; |
| 3035 | OS << " return true;\n"; |
| 3036 | OS << "}\n\n"; |
| 3037 | } |
| 3038 | |
Sjoerd Meijer | 6d14fdf | 2017-07-05 12:39:13 +0000 | [diff] [blame] | 3039 | static void emitMnemonicSpellChecker(raw_ostream &OS, CodeGenTarget &Target, |
| 3040 | unsigned VariantCount) { |
Craig Topper | 2a06028 | 2017-10-26 06:46:40 +0000 | [diff] [blame] | 3041 | OS << "static std::string " << Target.getName() |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3042 | << "MnemonicSpellCheck(StringRef S, const FeatureBitset &FBS," |
| 3043 | << " unsigned VariantID) {\n"; |
Sjoerd Meijer | 6d14fdf | 2017-07-05 12:39:13 +0000 | [diff] [blame] | 3044 | if (!VariantCount) |
| 3045 | OS << " return \"\";"; |
| 3046 | else { |
| 3047 | OS << " const unsigned MaxEditDist = 2;\n"; |
| 3048 | OS << " std::vector<StringRef> Candidates;\n"; |
Craig Topper | 0551556 | 2017-10-26 06:46:41 +0000 | [diff] [blame] | 3049 | OS << " StringRef Prev = \"\";\n\n"; |
| 3050 | |
| 3051 | OS << " // Find the appropriate table for this asm variant.\n"; |
| 3052 | OS << " const MatchEntry *Start, *End;\n"; |
| 3053 | OS << " switch (VariantID) {\n"; |
| 3054 | OS << " default: llvm_unreachable(\"invalid variant!\");\n"; |
| 3055 | for (unsigned VC = 0; VC != VariantCount; ++VC) { |
| 3056 | Record *AsmVariant = Target.getAsmParserVariant(VC); |
| 3057 | int AsmVariantNo = AsmVariant->getValueAsInt("Variant"); |
| 3058 | OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC |
| 3059 | << "); End = std::end(MatchTable" << VC << "); break;\n"; |
| 3060 | } |
| 3061 | OS << " }\n\n"; |
| 3062 | OS << " for (auto I = Start; I < End; I++) {\n"; |
Sjoerd Meijer | 6d14fdf | 2017-07-05 12:39:13 +0000 | [diff] [blame] | 3063 | OS << " // Ignore unsupported instructions.\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3064 | OS << " const FeatureBitset &RequiredFeatures = " |
| 3065 | "FeatureBitsets[I->RequiredFeaturesIdx];\n"; |
| 3066 | OS << " if ((FBS & RequiredFeatures) != RequiredFeatures)\n"; |
Sjoerd Meijer | 6d14fdf | 2017-07-05 12:39:13 +0000 | [diff] [blame] | 3067 | OS << " continue;\n"; |
| 3068 | OS << "\n"; |
| 3069 | OS << " StringRef T = I->getMnemonic();\n"; |
| 3070 | OS << " // Avoid recomputing the edit distance for the same string.\n"; |
| 3071 | OS << " if (T.equals(Prev))\n"; |
| 3072 | OS << " continue;\n"; |
| 3073 | OS << "\n"; |
| 3074 | OS << " Prev = T;\n"; |
| 3075 | OS << " unsigned Dist = S.edit_distance(T, false, MaxEditDist);\n"; |
| 3076 | OS << " if (Dist <= MaxEditDist)\n"; |
| 3077 | OS << " Candidates.push_back(T);\n"; |
| 3078 | OS << " }\n"; |
| 3079 | OS << "\n"; |
| 3080 | OS << " if (Candidates.empty())\n"; |
| 3081 | OS << " return \"\";\n"; |
| 3082 | OS << "\n"; |
| 3083 | OS << " std::string Res = \", did you mean: \";\n"; |
| 3084 | OS << " unsigned i = 0;\n"; |
| 3085 | OS << " for( ; i < Candidates.size() - 1; i++)\n"; |
| 3086 | OS << " Res += Candidates[i].str() + \", \";\n"; |
| 3087 | OS << " return Res + Candidates[i].str() + \"?\";\n"; |
| 3088 | } |
| 3089 | OS << "}\n"; |
| 3090 | OS << "\n"; |
| 3091 | } |
| 3092 | |
| 3093 | |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3094 | // Emit a function mapping match classes to strings, for debugging. |
| 3095 | static void emitMatchClassKindNames(std::forward_list<ClassInfo> &Infos, |
| 3096 | raw_ostream &OS) { |
| 3097 | OS << "#ifndef NDEBUG\n"; |
| 3098 | OS << "const char *getMatchClassName(MatchClassKind Kind) {\n"; |
| 3099 | OS << " switch (Kind) {\n"; |
| 3100 | |
| 3101 | OS << " case InvalidMatchClass: return \"InvalidMatchClass\";\n"; |
| 3102 | OS << " case OptionalMatchClass: return \"OptionalMatchClass\";\n"; |
| 3103 | for (const auto &CI : Infos) { |
| 3104 | OS << " case " << CI.Name << ": return \"" << CI.Name << "\";\n"; |
| 3105 | } |
| 3106 | OS << " case NumMatchClassKinds: return \"NumMatchClassKinds\";\n"; |
| 3107 | |
| 3108 | OS << " }\n"; |
| 3109 | OS << " llvm_unreachable(\"unhandled MatchClassKind!\");\n"; |
| 3110 | OS << "}\n\n"; |
| 3111 | OS << "#endif // NDEBUG\n"; |
| 3112 | } |
| 3113 | |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3114 | static std::string |
| 3115 | getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) { |
| 3116 | std::string Name = "AMFBS"; |
| 3117 | for (const auto &Feature : FeatureBitset) |
| 3118 | Name += ("_" + Feature->getName()).str(); |
| 3119 | return Name; |
| 3120 | } |
| 3121 | |
Daniel Dunbar | d0470d7 | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 3122 | void AsmMatcherEmitter::run(raw_ostream &OS) { |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 3123 | CodeGenTarget Target(Records); |
Daniel Dunbar | d0470d7 | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 3124 | Record *AsmParser = Target.getAsmParser(); |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 3125 | StringRef ClassName = AsmParser->getValueAsString("AsmParserClassName"); |
Daniel Dunbar | d0470d7 | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 3126 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3127 | // Compute the information on the instructions to match. |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 3128 | AsmMatcherInfo Info(AsmParser, Target, Records); |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 3129 | Info.buildInfo(); |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 3130 | |
Daniel Dunbar | 3b8a466 | 2010-02-02 23:46:36 +0000 | [diff] [blame] | 3131 | // Sort the instruction table using the partial order on classes. We use |
| 3132 | // stable_sort to ensure that ambiguous instructions are still |
| 3133 | // deterministically ordered. |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3134 | std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(), |
| 3135 | [](const std::unique_ptr<MatchableInfo> &a, |
| 3136 | const std::unique_ptr<MatchableInfo> &b){ |
| 3137 | return *a < *b;}); |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3138 | |
Matthias Braun | a8eed31 | 2016-12-05 19:44:31 +0000 | [diff] [blame] | 3139 | #ifdef EXPENSIVE_CHECKS |
| 3140 | // Verify that the table is sorted and operator < works transitively. |
| 3141 | for (auto I = Info.Matchables.begin(), E = Info.Matchables.end(); I != E; |
| 3142 | ++I) { |
| 3143 | for (auto J = I; J != E; ++J) { |
| 3144 | assert(!(**J < **I)); |
| 3145 | } |
| 3146 | } |
| 3147 | #endif |
| 3148 | |
Daniel Dunbar | 7133028 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 3149 | DEBUG_WITH_TYPE("instruction_info", { |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 3150 | for (const auto &MI : Info.Matchables) |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3151 | MI->dump(); |
Daniel Dunbar | e10787e | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 3152 | }); |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 3153 | |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 3154 | // Check for ambiguous matchables. |
Chris Lattner | c0658cb | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 3155 | DEBUG_WITH_TYPE("ambiguous_instrs", { |
| 3156 | unsigned NumAmbiguous = 0; |
David Blaikie | 9a6f283 | 2014-12-22 21:26:38 +0000 | [diff] [blame] | 3157 | for (auto I = Info.Matchables.begin(), E = Info.Matchables.end(); I != E; |
| 3158 | ++I) { |
| 3159 | for (auto J = std::next(I); J != E; ++J) { |
| 3160 | const MatchableInfo &A = **I; |
| 3161 | const MatchableInfo &B = **J; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3162 | |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 3163 | if (A.couldMatchAmbiguouslyWith(B)) { |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 3164 | errs() << "warning: ambiguous matchables:\n"; |
Chris Lattner | c0658cb | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 3165 | A.dump(); |
| 3166 | errs() << "\nis incomparable with:\n"; |
| 3167 | B.dump(); |
| 3168 | errs() << "\n\n"; |
Chris Lattner | fdb7dec | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 3169 | ++NumAmbiguous; |
| 3170 | } |
Daniel Dunbar | f573b56 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 3171 | } |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 3172 | } |
Chris Lattner | fdb7dec | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 3173 | if (NumAmbiguous) |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3174 | errs() << "warning: " << NumAmbiguous |
Chris Lattner | ad77681 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 3175 | << " ambiguous matchables!\n"; |
Chris Lattner | c0658cb | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 3176 | }); |
Daniel Dunbar | 3239f02 | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 3177 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 3178 | // Compute the information on the custom operand parsing. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 3179 | Info.buildOperandMatchInfo(); |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 3180 | |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 3181 | bool HasMnemonicFirst = AsmParser->getValueAsBit("HasMnemonicFirst"); |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 3182 | bool HasOptionalOperands = Info.hasOptionalOperands(); |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3183 | bool ReportMultipleNearMisses = |
| 3184 | AsmParser->getValueAsBit("ReportMultipleNearMisses"); |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 3185 | |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 3186 | // Write the output. |
| 3187 | |
Chris Lattner | 3e4582a | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 3188 | // Information for the class declaration. |
| 3189 | OS << "\n#ifdef GET_ASSEMBLER_HEADER\n"; |
| 3190 | OS << "#undef GET_ASSEMBLER_HEADER\n"; |
Jim Grosbach | 860a84d | 2011-02-11 21:31:55 +0000 | [diff] [blame] | 3191 | OS << " // This should be included into the middle of the declaration of\n"; |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 3192 | OS << " // your subclasses implementation of MCTargetAsmParser.\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3193 | OS << " FeatureBitset ComputeAvailableFeatures(const FeatureBitset& FB) const;\n"; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 3194 | if (HasOptionalOperands) { |
| 3195 | OS << " void convertToMCInst(unsigned Kind, MCInst &Inst, " |
| 3196 | << "unsigned Opcode,\n" |
| 3197 | << " const OperandVector &Operands,\n" |
| 3198 | << " const SmallBitVector &OptionalOperandsMask);\n"; |
| 3199 | } else { |
| 3200 | OS << " void convertToMCInst(unsigned Kind, MCInst &Inst, " |
| 3201 | << "unsigned Opcode,\n" |
| 3202 | << " const OperandVector &Operands);\n"; |
| 3203 | } |
Chad Rosier | 380a74a | 2012-10-02 00:25:57 +0000 | [diff] [blame] | 3204 | OS << " void convertToMapAndConstraints(unsigned Kind,\n "; |
Peter Collingbourne | 0da8630 | 2016-10-10 22:49:37 +0000 | [diff] [blame] | 3205 | OS << " const OperandVector &Operands) override;\n"; |
Craig Topper | a5754e6 | 2015-01-03 08:16:29 +0000 | [diff] [blame] | 3206 | OS << " unsigned MatchInstructionImpl(const OperandVector &Operands,\n" |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3207 | << " MCInst &Inst,\n"; |
| 3208 | if (ReportMultipleNearMisses) |
| 3209 | OS << " SmallVectorImpl<NearMissInfo> *NearMisses,\n"; |
| 3210 | else |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3211 | OS << " uint64_t &ErrorInfo,\n" |
| 3212 | << " FeatureBitset &MissingFeatures,\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3213 | OS << " bool matchingInlineAsm,\n" |
Chad Rosier | 380a74a | 2012-10-02 00:25:57 +0000 | [diff] [blame] | 3214 | << " unsigned VariantID = 0);\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3215 | if (!ReportMultipleNearMisses) |
| 3216 | OS << " unsigned MatchInstructionImpl(const OperandVector &Operands,\n" |
| 3217 | << " MCInst &Inst,\n" |
| 3218 | << " uint64_t &ErrorInfo,\n" |
| 3219 | << " bool matchingInlineAsm,\n" |
| 3220 | << " unsigned VariantID = 0) {\n" |
| 3221 | << " FeatureBitset MissingFeatures;\n" |
| 3222 | << " return MatchInstructionImpl(Operands, Inst, ErrorInfo, MissingFeatures,\n" |
| 3223 | << " matchingInlineAsm, VariantID);\n" |
| 3224 | << " }\n\n"; |
| 3225 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 3226 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 3227 | if (!Info.OperandMatchInfo.empty()) { |
Jim Grosbach | 861e49c | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 3228 | OS << " OperandMatchResultTy MatchOperandParserImpl(\n"; |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 3229 | OS << " OperandVector &Operands,\n"; |
Sander de Smalen | cd6be96 | 2017-12-20 11:02:42 +0000 | [diff] [blame] | 3230 | OS << " StringRef Mnemonic,\n"; |
| 3231 | OS << " bool ParseForAllFeatures = false);\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 3232 | |
Jim Grosbach | 1f5c5aa | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 3233 | OS << " OperandMatchResultTy tryCustomParseOperand(\n"; |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 3234 | OS << " OperandVector &Operands,\n"; |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 3235 | OS << " unsigned MCK);\n\n"; |
| 3236 | } |
| 3237 | |
Chris Lattner | 3e4582a | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 3238 | OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n"; |
| 3239 | |
Jim Grosbach | 3a8a0fa | 2012-06-22 23:56:44 +0000 | [diff] [blame] | 3240 | // Emit the operand match diagnostic enum names. |
| 3241 | OS << "\n#ifdef GET_OPERAND_DIAGNOSTIC_TYPES\n"; |
| 3242 | OS << "#undef GET_OPERAND_DIAGNOSTIC_TYPES\n\n"; |
| 3243 | emitOperandDiagnosticTypes(Info, OS); |
| 3244 | OS << "#endif // GET_OPERAND_DIAGNOSTIC_TYPES\n\n"; |
| 3245 | |
Chris Lattner | 3e4582a | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 3246 | OS << "\n#ifdef GET_REGISTER_MATCHER\n"; |
| 3247 | OS << "#undef GET_REGISTER_MATCHER\n\n"; |
| 3248 | |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 3249 | // Emit the subtarget feature enumeration. |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3250 | SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration( |
Daniel Sanders | 72db2a3 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 3251 | Info.SubtargetFeatures, OS); |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 3252 | |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 3253 | // Emit the function to match a register name to number. |
Akira Hatanaka | 7605630c | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 3254 | // This should be omitted for Mips target |
| 3255 | if (AsmParser->getValueAsBit("ShouldEmitMatchRegisterName")) |
| 3256 | emitMatchRegisterName(Target, AsmParser, OS); |
Chris Lattner | 3e4582a | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 3257 | |
Dylan McKay | bff960a | 2016-02-03 10:30:16 +0000 | [diff] [blame] | 3258 | if (AsmParser->getValueAsBit("ShouldEmitMatchRegisterAltName")) |
| 3259 | emitMatchRegisterAltName(Target, AsmParser, OS); |
| 3260 | |
Chris Lattner | 3e4582a | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 3261 | OS << "#endif // GET_REGISTER_MATCHER\n\n"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3262 | |
Craig Topper | 3ec7c2a | 2012-04-25 06:56:34 +0000 | [diff] [blame] | 3263 | OS << "\n#ifdef GET_SUBTARGET_FEATURE_NAME\n"; |
| 3264 | OS << "#undef GET_SUBTARGET_FEATURE_NAME\n\n"; |
Daniel Dunbar | 3fb754a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 3265 | |
Jim Grosbach | 5117ef7 | 2012-04-24 22:40:08 +0000 | [diff] [blame] | 3266 | // Generate the helper function to get the names for subtarget features. |
| 3267 | emitGetSubtargetFeatureName(Info, OS); |
| 3268 | |
Craig Topper | 3ec7c2a | 2012-04-25 06:56:34 +0000 | [diff] [blame] | 3269 | OS << "#endif // GET_SUBTARGET_FEATURE_NAME\n\n"; |
| 3270 | |
| 3271 | OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n"; |
| 3272 | OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n"; |
| 3273 | |
Chris Lattner | 477fba4f | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 3274 | // Generate the function that remaps for mnemonic aliases. |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 3275 | bool HasMnemonicAliases = emitMnemonicAliases(OS, Info, Target); |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 3276 | |
Chad Rosier | f4e35dc | 2012-10-01 23:45:51 +0000 | [diff] [blame] | 3277 | // Generate the convertToMCInst function to convert operands into an MCInst. |
| 3278 | // Also, generate the convertToMapAndConstraints function for MS-style inline |
| 3279 | // assembly. The latter doesn't actually generate a MCInst. |
Craig Topper | b64f915 | 2019-04-02 20:52:04 +0000 | [diff] [blame^] | 3280 | unsigned NumConverters = emitConvertFuncs(Target, ClassName, Info.Matchables, |
| 3281 | HasMnemonicFirst, |
| 3282 | HasOptionalOperands, OS); |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 3283 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3284 | // Emit the enumeration for classes which participate in matching. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 3285 | emitMatchClassEnumeration(Target, Info.Classes, OS); |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 3286 | |
Oliver Stannard | 41dfac3 | 2017-10-03 14:34:57 +0000 | [diff] [blame] | 3287 | // Emit a function to get the user-visible string to describe an operand |
| 3288 | // match failure in diagnostics. |
| 3289 | emitOperandMatchErrorDiagStrings(Info, OS); |
| 3290 | |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 3291 | // Emit a function to map register classes to operand match failure codes. |
| 3292 | emitRegisterMatchErrorFunc(Info, OS); |
| 3293 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3294 | // Emit the routine to match token strings to their match class. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 3295 | emitMatchTokenString(Target, Info.Classes, OS); |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3296 | |
Daniel Dunbar | 2587b61 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 3297 | // Emit the subclass predicate routine. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 3298 | emitIsSubclass(Target, Info.Classes, OS); |
Daniel Dunbar | 2587b61 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 3299 | |
Jim Grosbach | 6e2e29b | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 3300 | // Emit the routine to validate an operand against a match class. |
Jim Grosbach | 8c2beaa | 2012-04-19 17:52:32 +0000 | [diff] [blame] | 3301 | emitValidateOperandClass(Info, OS); |
Jim Grosbach | 6e2e29b | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 3302 | |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3303 | emitMatchClassKindNames(Info.Classes, OS); |
| 3304 | |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 3305 | // Emit the available features compute function. |
Daniel Sanders | e7b0d66 | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 3306 | SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures( |
Daniel Sanders | 72db2a3 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 3307 | Info.Target.getName(), ClassName, "ComputeAvailableFeatures", |
| 3308 | Info.SubtargetFeatures, OS); |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 3309 | |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 3310 | if (!ReportMultipleNearMisses) |
| 3311 | emitAsmTiedOperandConstraints(Target, Info, OS); |
| 3312 | |
Craig Topper | e2cfeb3 | 2012-09-18 06:10:45 +0000 | [diff] [blame] | 3313 | StringToOffsetTable StringTable; |
| 3314 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3315 | size_t MaxNumOperands = 0; |
Craig Topper | e2cfeb3 | 2012-09-18 06:10:45 +0000 | [diff] [blame] | 3316 | unsigned MaxMnemonicIndex = 0; |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 3317 | bool HasDeprecation = false; |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 3318 | for (const auto &MI : Info.Matchables) { |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3319 | MaxNumOperands = std::max(MaxNumOperands, MI->AsmOperands.size()); |
| 3320 | HasDeprecation |= MI->HasDeprecation; |
Craig Topper | e2cfeb3 | 2012-09-18 06:10:45 +0000 | [diff] [blame] | 3321 | |
| 3322 | // Store a pascal-style length byte in the mnemonic. |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3323 | std::string LenMnemonic = char(MI->Mnemonic.size()) + MI->Mnemonic.str(); |
Craig Topper | e2cfeb3 | 2012-09-18 06:10:45 +0000 | [diff] [blame] | 3324 | MaxMnemonicIndex = std::max(MaxMnemonicIndex, |
| 3325 | StringTable.GetOrAddStringOffset(LenMnemonic, false)); |
| 3326 | } |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3327 | |
Craig Topper | 7ecfa6d | 2012-09-18 07:02:21 +0000 | [diff] [blame] | 3328 | OS << "static const char *const MnemonicTable =\n"; |
| 3329 | StringTable.EmitString(OS); |
| 3330 | OS << ";\n\n"; |
| 3331 | |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3332 | std::vector<std::vector<Record *>> FeatureBitsets; |
| 3333 | for (const auto &MI : Info.Matchables) { |
| 3334 | if (MI->RequiredFeatures.empty()) |
| 3335 | continue; |
| 3336 | FeatureBitsets.emplace_back(); |
| 3337 | for (unsigned I = 0, E = MI->RequiredFeatures.size(); I != E; ++I) |
| 3338 | FeatureBitsets.back().push_back(MI->RequiredFeatures[I]->TheDef); |
| 3339 | } |
| 3340 | |
| 3341 | llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A, |
| 3342 | const std::vector<Record *> &B) { |
| 3343 | if (A.size() < B.size()) |
| 3344 | return true; |
| 3345 | if (A.size() > B.size()) |
| 3346 | return false; |
| 3347 | for (const auto &Pair : zip(A, B)) { |
| 3348 | if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName()) |
| 3349 | return true; |
| 3350 | if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName()) |
| 3351 | return false; |
| 3352 | } |
| 3353 | return false; |
| 3354 | }); |
| 3355 | FeatureBitsets.erase( |
| 3356 | std::unique(FeatureBitsets.begin(), FeatureBitsets.end()), |
| 3357 | FeatureBitsets.end()); |
| 3358 | OS << "// Feature bitsets.\n" |
| 3359 | << "enum : " << getMinimalTypeForRange(FeatureBitsets.size()) << " {\n" |
| 3360 | << " AMFBS_None,\n"; |
| 3361 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 3362 | if (FeatureBitset.empty()) |
| 3363 | continue; |
| 3364 | OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n"; |
| 3365 | } |
| 3366 | OS << "};\n\n" |
| 3367 | << "const static FeatureBitset FeatureBitsets[] {\n" |
| 3368 | << " {}, // AMFBS_None\n"; |
| 3369 | for (const auto &FeatureBitset : FeatureBitsets) { |
| 3370 | if (FeatureBitset.empty()) |
| 3371 | continue; |
| 3372 | OS << " {"; |
| 3373 | for (const auto &Feature : FeatureBitset) { |
| 3374 | const auto &I = Info.SubtargetFeatures.find(Feature); |
| 3375 | assert(I != Info.SubtargetFeatures.end() && "Didn't import predicate?"); |
| 3376 | OS << I->second.getEnumBitName() << ", "; |
| 3377 | } |
| 3378 | OS << "},\n"; |
| 3379 | } |
| 3380 | OS << "};\n\n"; |
| 3381 | |
Simon Pilgrim | 6bdc755 | 2017-03-31 10:59:37 +0000 | [diff] [blame] | 3382 | // Emit the static match table; unused classes get initialized to 0 which is |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3383 | // guaranteed to be InvalidMatchClass. |
| 3384 | // |
| 3385 | // FIXME: We can reduce the size of this table very easily. First, we change |
| 3386 | // it so that store the kinds in separate bit-fields for each index, which |
| 3387 | // only needs to be the max width used for classes at that index (we also need |
| 3388 | // to reject based on this during classification). If we then make sure to |
| 3389 | // order the match kinds appropriately (putting mnemonics last), then we |
| 3390 | // should only end up using a few bits for each class, especially the ones |
| 3391 | // following the mnemonic. |
Chris Lattner | 6b6f3dd | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 3392 | OS << "namespace {\n"; |
| 3393 | OS << " struct MatchEntry {\n"; |
Craig Topper | e2cfeb3 | 2012-09-18 06:10:45 +0000 | [diff] [blame] | 3394 | OS << " " << getMinimalTypeForRange(MaxMnemonicIndex) |
| 3395 | << " Mnemonic;\n"; |
Benjamin Kramer | 5aeee5f | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 3396 | OS << " uint16_t Opcode;\n"; |
Craig Topper | b64f915 | 2019-04-02 20:52:04 +0000 | [diff] [blame^] | 3397 | OS << " " << getMinimalTypeForRange(NumConverters) |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3398 | << " ConvertFn;\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3399 | OS << " " << getMinimalTypeForRange(FeatureBitsets.size()) |
| 3400 | << " RequiredFeaturesIdx;\n"; |
David Blaikie | d749e34 | 2014-11-28 20:35:57 +0000 | [diff] [blame] | 3401 | OS << " " << getMinimalTypeForRange( |
| 3402 | std::distance(Info.Classes.begin(), Info.Classes.end())) |
| 3403 | << " Classes[" << MaxNumOperands << "];\n"; |
Benjamin Kramer | 5aeee5f | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 3404 | OS << " StringRef getMnemonic() const {\n"; |
| 3405 | OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n"; |
| 3406 | OS << " MnemonicTable[Mnemonic]);\n"; |
| 3407 | OS << " }\n"; |
Chris Lattner | 8130197 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 3408 | OS << " };\n\n"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3409 | |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 3410 | OS << " // Predicate for searching for an opcode.\n"; |
Chris Lattner | 8130197 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 3411 | OS << " struct LessOpcode {\n"; |
| 3412 | OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n"; |
Benjamin Kramer | 5aeee5f | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 3413 | OS << " return LHS.getMnemonic() < RHS;\n"; |
Chris Lattner | 8130197 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 3414 | OS << " }\n"; |
| 3415 | OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n"; |
Benjamin Kramer | 5aeee5f | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 3416 | OS << " return LHS < RHS.getMnemonic();\n"; |
Chris Lattner | 8130197 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 3417 | OS << " }\n"; |
Chris Lattner | 6282336 | 2010-09-07 06:10:48 +0000 | [diff] [blame] | 3418 | OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n"; |
Benjamin Kramer | 5aeee5f | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 3419 | OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n"; |
Chris Lattner | 6282336 | 2010-09-07 06:10:48 +0000 | [diff] [blame] | 3420 | OS << " }\n"; |
Chris Lattner | 6b6f3dd | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 3421 | OS << " };\n"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3422 | |
Chris Lattner | 6b6f3dd | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 3423 | OS << "} // end anonymous namespace.\n\n"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3424 | |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3425 | unsigned VariantCount = Target.getAsmParserVariantCount(); |
| 3426 | for (unsigned VC = 0; VC != VariantCount; ++VC) { |
| 3427 | Record *AsmVariant = Target.getAsmParserVariant(VC); |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3428 | int AsmVariantNo = AsmVariant->getValueAsInt("Variant"); |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3429 | |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3430 | OS << "static const MatchEntry MatchTable" << VC << "[] = {\n"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3431 | |
Craig Topper | f34dad9 | 2014-11-28 03:53:02 +0000 | [diff] [blame] | 3432 | for (const auto &MI : Info.Matchables) { |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3433 | if (MI->AsmVariantID != AsmVariantNo) |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3434 | continue; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3435 | |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3436 | // Store a pascal-style length byte in the mnemonic. |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3437 | std::string LenMnemonic = char(MI->Mnemonic.size()) + MI->Mnemonic.str(); |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3438 | OS << " { " << StringTable.GetOrAddStringOffset(LenMnemonic, false) |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3439 | << " /* " << MI->Mnemonic << " */, " |
Craig Topper | 2b347eb | 2017-07-07 05:19:25 +0000 | [diff] [blame] | 3440 | << Target.getInstNamespace() << "::" |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3441 | << MI->getResultInst()->TheDef->getName() << ", " |
| 3442 | << MI->ConversionFnKind << ", "; |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3443 | |
| 3444 | // Write the required features mask. |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3445 | OS << "AMFBS"; |
| 3446 | if (MI->RequiredFeatures.empty()) |
| 3447 | OS << "_None"; |
| 3448 | else |
| 3449 | for (unsigned i = 0, e = MI->RequiredFeatures.size(); i != e; ++i) |
| 3450 | OS << '_' << MI->RequiredFeatures[i]->TheDef->getName(); |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3451 | |
| 3452 | OS << ", { "; |
Duncan P. N. Exon Smith | 5a48cafb | 2014-11-28 23:00:22 +0000 | [diff] [blame] | 3453 | for (unsigned i = 0, e = MI->AsmOperands.size(); i != e; ++i) { |
| 3454 | const MatchableInfo::AsmOperand &Op = MI->AsmOperands[i]; |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3455 | |
| 3456 | if (i) OS << ", "; |
| 3457 | OS << Op.Class->Name; |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 3458 | } |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3459 | OS << " }, },\n"; |
Craig Topper | 4de7373 | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 3460 | } |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3461 | |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3462 | OS << "};\n\n"; |
| 3463 | } |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 3464 | |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3465 | OS << "#include \"llvm/Support/Debug.h\"\n"; |
| 3466 | OS << "#include \"llvm/Support/Format.h\"\n\n"; |
| 3467 | |
Chris Lattner | 6b6f3dd | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 3468 | // Finally, build the match function. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 3469 | OS << "unsigned " << Target.getName() << ClassName << "::\n" |
Craig Topper | a5754e6 | 2015-01-03 08:16:29 +0000 | [diff] [blame] | 3470 | << "MatchInstructionImpl(const OperandVector &Operands,\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3471 | OS << " MCInst &Inst,\n"; |
| 3472 | if (ReportMultipleNearMisses) |
| 3473 | OS << " SmallVectorImpl<NearMissInfo> *NearMisses,\n"; |
| 3474 | else |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3475 | OS << " uint64_t &ErrorInfo,\n" |
| 3476 | << " FeatureBitset &MissingFeatures,\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3477 | OS << " bool matchingInlineAsm, unsigned VariantID) {\n"; |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 3478 | |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3479 | if (!ReportMultipleNearMisses) { |
| 3480 | OS << " // Eliminate obvious mismatches.\n"; |
| 3481 | OS << " if (Operands.size() > " |
| 3482 | << (MaxNumOperands + HasMnemonicFirst) << ") {\n"; |
| 3483 | OS << " ErrorInfo = " |
| 3484 | << (MaxNumOperands + HasMnemonicFirst) << ";\n"; |
| 3485 | OS << " return Match_InvalidOperand;\n"; |
| 3486 | OS << " }\n\n"; |
| 3487 | } |
Chad Rosier | eac13a3 | 2012-08-30 21:43:05 +0000 | [diff] [blame] | 3488 | |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 3489 | // Emit code to get the available features. |
| 3490 | OS << " // Get the current feature set.\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3491 | OS << " const FeatureBitset &AvailableFeatures = getAvailableFeatures();\n\n"; |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 3492 | |
Chris Lattner | ba7b4fe | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 3493 | OS << " // Get the instruction mnemonic, which is the first token.\n"; |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 3494 | if (HasMnemonicFirst) { |
| 3495 | OS << " StringRef Mnemonic = ((" << Target.getName() |
| 3496 | << "Operand&)*Operands[0]).getToken();\n\n"; |
| 3497 | } else { |
| 3498 | OS << " StringRef Mnemonic;\n"; |
| 3499 | OS << " if (Operands[0]->isToken())\n"; |
| 3500 | OS << " Mnemonic = ((" << Target.getName() |
| 3501 | << "Operand&)*Operands[0]).getToken();\n\n"; |
| 3502 | } |
Chris Lattner | ba7b4fe | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 3503 | |
Chris Lattner | 477fba4f | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 3504 | if (HasMnemonicAliases) { |
| 3505 | OS << " // Process all MnemonicAliases to remap the mnemonic.\n"; |
Chad Rosier | 9f7a221 | 2013-04-18 22:35:36 +0000 | [diff] [blame] | 3506 | OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures, VariantID);\n\n"; |
Chris Lattner | 477fba4f | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 3507 | } |
Bob Wilson | f4ee9e5 | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 3508 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3509 | // Emit code to compute the class list for this operand vector. |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3510 | if (!ReportMultipleNearMisses) { |
| 3511 | OS << " // Some state to try to produce better error messages.\n"; |
| 3512 | OS << " bool HadMatchOtherThanFeatures = false;\n"; |
| 3513 | OS << " bool HadMatchOtherThanPredicate = false;\n"; |
| 3514 | OS << " unsigned RetCode = Match_InvalidOperand;\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3515 | OS << " MissingFeatures.set();\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3516 | OS << " // Set ErrorInfo to the operand that mismatches if it is\n"; |
| 3517 | OS << " // wrong for all instances of the instruction.\n"; |
| 3518 | OS << " ErrorInfo = ~0ULL;\n"; |
| 3519 | } |
| 3520 | |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 3521 | if (HasOptionalOperands) { |
| 3522 | OS << " SmallBitVector OptionalOperandsMask(" << MaxNumOperands << ");\n"; |
| 3523 | } |
Chris Lattner | 8130197 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 3524 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3525 | // Emit code to search the table. |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3526 | OS << " // Find the appropriate table for this asm variant.\n"; |
| 3527 | OS << " const MatchEntry *Start, *End;\n"; |
| 3528 | OS << " switch (VariantID) {\n"; |
Craig Topper | 8c714d1 | 2015-01-03 08:16:14 +0000 | [diff] [blame] | 3529 | OS << " default: llvm_unreachable(\"invalid variant!\");\n"; |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3530 | for (unsigned VC = 0; VC != VariantCount; ++VC) { |
| 3531 | Record *AsmVariant = Target.getAsmParserVariant(VC); |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3532 | int AsmVariantNo = AsmVariant->getValueAsInt("Variant"); |
Benjamin Kramer | 502b9e1 | 2014-04-12 16:15:53 +0000 | [diff] [blame] | 3533 | OS << " case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC |
| 3534 | << "); End = std::end(MatchTable" << VC << "); break;\n"; |
Craig Topper | 690d8ea | 2013-07-24 07:33:14 +0000 | [diff] [blame] | 3535 | } |
| 3536 | OS << " }\n"; |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 3537 | |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3538 | OS << " // Search the table.\n"; |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 3539 | if (HasMnemonicFirst) { |
| 3540 | OS << " auto MnemonicRange = " |
| 3541 | "std::equal_range(Start, End, Mnemonic, LessOpcode());\n\n"; |
| 3542 | } else { |
| 3543 | OS << " auto MnemonicRange = std::make_pair(Start, End);\n"; |
| 3544 | OS << " unsigned SIndex = Mnemonic.empty() ? 0 : 1;\n"; |
| 3545 | OS << " if (!Mnemonic.empty())\n"; |
| 3546 | OS << " MnemonicRange = " |
| 3547 | "std::equal_range(Start, End, Mnemonic.lower(), LessOpcode());\n\n"; |
| 3548 | } |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3549 | |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3550 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"AsmMatcher: found \" <<\n" |
| 3551 | << " std::distance(MnemonicRange.first, MnemonicRange.second) << \n" |
| 3552 | << " \" encodings with mnemonic '\" << Mnemonic << \"'\\n\");\n\n"; |
| 3553 | |
Chris Lattner | 628fbec | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 3554 | OS << " // Return a more specific error code if no mnemonics match.\n"; |
| 3555 | OS << " if (MnemonicRange.first == MnemonicRange.second)\n"; |
| 3556 | OS << " return Match_MnemonicFail;\n\n"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3557 | |
Chris Lattner | 8130197 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 3558 | OS << " for (const MatchEntry *it = MnemonicRange.first, " |
Chris Lattner | 9026ac0 | 2010-09-06 21:23:43 +0000 | [diff] [blame] | 3559 | << "*ie = MnemonicRange.second;\n"; |
Chris Lattner | 8130197 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 3560 | OS << " it != ie; ++it) {\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3561 | OS << " const FeatureBitset &RequiredFeatures = " |
| 3562 | "FeatureBitsets[it->RequiredFeaturesIdx];\n"; |
Sander de Smalen | cd6be96 | 2017-12-20 11:02:42 +0000 | [diff] [blame] | 3563 | OS << " bool HasRequiredFeatures =\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3564 | OS << " (AvailableFeatures & RequiredFeatures) == RequiredFeatures;\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3565 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Trying to match opcode \"\n"; |
| 3566 | OS << " << MII.getName(it->Opcode) << \"\\n\");\n"; |
| 3567 | |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3568 | if (ReportMultipleNearMisses) { |
| 3569 | OS << " // Some state to record ways in which this instruction did not match.\n"; |
| 3570 | OS << " NearMissInfo OperandNearMiss = NearMissInfo::getSuccess();\n"; |
| 3571 | OS << " NearMissInfo FeaturesNearMiss = NearMissInfo::getSuccess();\n"; |
| 3572 | OS << " NearMissInfo EarlyPredicateNearMiss = NearMissInfo::getSuccess();\n"; |
| 3573 | OS << " NearMissInfo LatePredicateNearMiss = NearMissInfo::getSuccess();\n"; |
Oliver Stannard | 7ab6060 | 2017-12-04 13:42:22 +0000 | [diff] [blame] | 3574 | OS << " bool MultipleInvalidOperands = false;\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
Craig Topper | fd2c6a3 | 2015-12-31 08:18:23 +0000 | [diff] [blame] | 3577 | if (HasMnemonicFirst) { |
| 3578 | OS << " // equal_range guarantees that instruction mnemonic matches.\n"; |
| 3579 | OS << " assert(Mnemonic == it->getMnemonic());\n"; |
| 3580 | } |
| 3581 | |
Daniel Dunbar | eefe861 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 3582 | // Emit check that the subclasses match. |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3583 | if (!ReportMultipleNearMisses) |
| 3584 | OS << " bool OperandsValid = true;\n"; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 3585 | if (HasOptionalOperands) { |
| 3586 | OS << " OptionalOperandsMask.reset(0, " << MaxNumOperands << ");\n"; |
| 3587 | } |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3588 | OS << " for (unsigned FormalIdx = " << (HasMnemonicFirst ? "0" : "SIndex") |
| 3589 | << ", ActualIdx = " << (HasMnemonicFirst ? "1" : "SIndex") |
| 3590 | << "; FormalIdx != " << MaxNumOperands << "; ++FormalIdx) {\n"; |
| 3591 | OS << " auto Formal = " |
| 3592 | << "static_cast<MatchClassKind>(it->Classes[FormalIdx]);\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3593 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\",\n"; |
| 3594 | OS << " dbgs() << \" Matching formal operand class \" << getMatchClassName(Formal)\n"; |
| 3595 | OS << " << \" against actual operand at index \" << ActualIdx);\n"; |
| 3596 | OS << " if (ActualIdx < Operands.size())\n"; |
| 3597 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \" (\";\n"; |
| 3598 | OS << " Operands[ActualIdx]->print(dbgs()); dbgs() << \"): \");\n"; |
| 3599 | OS << " else\n"; |
| 3600 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \": \");\n"; |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3601 | OS << " if (ActualIdx >= Operands.size()) {\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3602 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"actual operand index out of range \");\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3603 | if (ReportMultipleNearMisses) { |
| 3604 | OS << " bool ThisOperandValid = (Formal == " <<"InvalidMatchClass) || " |
| 3605 | "isSubclass(Formal, OptionalMatchClass);\n"; |
| 3606 | OS << " if (!ThisOperandValid) {\n"; |
| 3607 | OS << " if (!OperandNearMiss) {\n"; |
| 3608 | OS << " // Record info about match failure for later use.\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3609 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"recording too-few-operands near miss\\n\");\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3610 | OS << " OperandNearMiss =\n"; |
| 3611 | OS << " NearMissInfo::getTooFewOperands(Formal, it->Opcode);\n"; |
Oliver Stannard | 1e73e95 | 2017-11-21 15:16:50 +0000 | [diff] [blame] | 3612 | OS << " } else if (OperandNearMiss.getKind() != NearMissInfo::NearMissTooFewOperands) {\n"; |
Oliver Stannard | 7ab6060 | 2017-12-04 13:42:22 +0000 | [diff] [blame] | 3613 | OS << " // If more than one operand is invalid, give up on this match entry.\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3614 | OS << " DEBUG_WITH_TYPE(\n"; |
| 3615 | OS << " \"asm-matcher\",\n"; |
| 3616 | OS << " dbgs() << \"second invalid operand, giving up on this opcode\\n\");\n"; |
Oliver Stannard | 7ab6060 | 2017-12-04 13:42:22 +0000 | [diff] [blame] | 3617 | OS << " MultipleInvalidOperands = true;\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3618 | OS << " break;\n"; |
| 3619 | OS << " }\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3620 | OS << " } else {\n"; |
| 3621 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"but formal operand not required\\n\");\n"; |
Oliver Stannard | 6e94331 | 2017-11-21 15:12:05 +0000 | [diff] [blame] | 3622 | OS << " break;\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3623 | OS << " }\n"; |
| 3624 | OS << " continue;\n"; |
| 3625 | } else { |
| 3626 | OS << " OperandsValid = (Formal == InvalidMatchClass) || isSubclass(Formal, OptionalMatchClass);\n"; |
| 3627 | OS << " if (!OperandsValid) ErrorInfo = ActualIdx;\n"; |
| 3628 | if (HasOptionalOperands) { |
| 3629 | OS << " OptionalOperandsMask.set(FormalIdx, " << MaxNumOperands |
| 3630 | << ");\n"; |
| 3631 | } |
| 3632 | OS << " break;\n"; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 3633 | } |
Jim Grosbach | 6e2e29b | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 3634 | OS << " }\n"; |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3635 | OS << " MCParsedAsmOperand &Actual = *Operands[ActualIdx];\n"; |
Colin LeMahieu | 23403c2 | 2015-11-09 00:46:46 +0000 | [diff] [blame] | 3636 | OS << " unsigned Diag = validateOperandClass(Actual, Formal);\n"; |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3637 | OS << " if (Diag == Match_Success) {\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3638 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\",\n"; |
| 3639 | OS << " dbgs() << \"match success using generic matcher\\n\");\n"; |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3640 | OS << " ++ActualIdx;\n"; |
Chris Lattner | 339cc7b | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 3641 | OS << " continue;\n"; |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3642 | OS << " }\n"; |
Jim Grosbach | 86c652a | 2013-02-06 06:00:06 +0000 | [diff] [blame] | 3643 | OS << " // If the generic handler indicates an invalid operand\n"; |
| 3644 | OS << " // failure, check for a special case.\n"; |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 3645 | OS << " if (Diag != Match_Success) {\n"; |
| 3646 | OS << " unsigned TargetDiag = validateTargetOperandClass(Actual, Formal);\n"; |
| 3647 | OS << " if (TargetDiag == Match_Success) {\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3648 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\",\n"; |
| 3649 | OS << " dbgs() << \"match success using target matcher\\n\");\n"; |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3650 | OS << " ++ActualIdx;\n"; |
Jim Grosbach | 86c652a | 2013-02-06 06:00:06 +0000 | [diff] [blame] | 3651 | OS << " continue;\n"; |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3652 | OS << " }\n"; |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 3653 | OS << " // If the target matcher returned a specific error code use\n"; |
| 3654 | OS << " // that, else use the one from the generic matcher.\n"; |
Sander de Smalen | cd6be96 | 2017-12-20 11:02:42 +0000 | [diff] [blame] | 3655 | OS << " if (TargetDiag != Match_InvalidOperand && " |
| 3656 | "HasRequiredFeatures)\n"; |
Oliver Stannard | 29ffd3f | 2017-10-10 11:00:40 +0000 | [diff] [blame] | 3657 | OS << " Diag = TargetDiag;\n"; |
Jim Grosbach | 86c652a | 2013-02-06 06:00:06 +0000 | [diff] [blame] | 3658 | OS << " }\n"; |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3659 | OS << " // If current formal operand wasn't matched and it is optional\n" |
| 3660 | << " // then try to match next formal operand\n"; |
| 3661 | OS << " if (Diag == Match_InvalidOperand " |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 3662 | << "&& isSubclass(Formal, OptionalMatchClass)) {\n"; |
| 3663 | if (HasOptionalOperands) { |
| 3664 | OS << " OptionalOperandsMask.set(FormalIdx);\n"; |
| 3665 | } |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3666 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"ignoring optional operand\\n\");\n"; |
Nikolay Haustov | ea8febd | 2016-03-01 08:34:43 +0000 | [diff] [blame] | 3667 | OS << " continue;\n"; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 3668 | OS << " }\n"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3669 | |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3670 | if (ReportMultipleNearMisses) { |
| 3671 | OS << " if (!OperandNearMiss) {\n"; |
| 3672 | OS << " // If this is the first invalid operand we have seen, record some\n"; |
| 3673 | OS << " // information about it.\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3674 | OS << " DEBUG_WITH_TYPE(\n"; |
| 3675 | OS << " \"asm-matcher\",\n"; |
| 3676 | OS << " dbgs()\n"; |
| 3677 | OS << " << \"operand match failed, recording near-miss with diag code \"\n"; |
| 3678 | OS << " << Diag << \"\\n\");\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3679 | OS << " OperandNearMiss =\n"; |
| 3680 | OS << " NearMissInfo::getMissedOperand(Diag, Formal, it->Opcode, ActualIdx);\n"; |
| 3681 | OS << " ++ActualIdx;\n"; |
| 3682 | OS << " } else {\n"; |
| 3683 | OS << " // If more than one operand is invalid, give up on this match entry.\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3684 | OS << " DEBUG_WITH_TYPE(\n"; |
| 3685 | OS << " \"asm-matcher\",\n"; |
| 3686 | OS << " dbgs() << \"second operand mismatch, skipping this opcode\\n\");\n"; |
Oliver Stannard | 7ab6060 | 2017-12-04 13:42:22 +0000 | [diff] [blame] | 3687 | OS << " MultipleInvalidOperands = true;\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3688 | OS << " break;\n"; |
| 3689 | OS << " }\n"; |
| 3690 | OS << " }\n\n"; |
| 3691 | } else { |
| 3692 | OS << " // If this operand is broken for all of the instances of this\n"; |
| 3693 | OS << " // mnemonic, keep track of it so we can report loc info.\n"; |
| 3694 | OS << " // If we already had a match that only failed due to a\n"; |
| 3695 | OS << " // target predicate, that diagnostic is preferred.\n"; |
| 3696 | OS << " if (!HadMatchOtherThanPredicate &&\n"; |
| 3697 | OS << " (it == MnemonicRange.first || ErrorInfo <= ActualIdx)) {\n"; |
Sander de Smalen | cd6be96 | 2017-12-20 11:02:42 +0000 | [diff] [blame] | 3698 | OS << " if (HasRequiredFeatures && (ErrorInfo != ActualIdx || Diag " |
| 3699 | "!= Match_InvalidOperand))\n"; |
Sander de Smalen | 4acd57e | 2017-11-21 15:07:43 +0000 | [diff] [blame] | 3700 | OS << " RetCode = Diag;\n"; |
Sander de Smalen | 14e36ee | 2017-12-14 16:09:48 +0000 | [diff] [blame] | 3701 | OS << " ErrorInfo = ActualIdx;\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3702 | OS << " }\n"; |
| 3703 | OS << " // Otherwise, just reject this instance of the mnemonic.\n"; |
| 3704 | OS << " OperandsValid = false;\n"; |
| 3705 | OS << " break;\n"; |
| 3706 | OS << " }\n\n"; |
| 3707 | } |
| 3708 | |
Oliver Stannard | 7ab6060 | 2017-12-04 13:42:22 +0000 | [diff] [blame] | 3709 | if (ReportMultipleNearMisses) |
| 3710 | OS << " if (MultipleInvalidOperands) {\n"; |
| 3711 | else |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3712 | OS << " if (!OperandsValid) {\n"; |
Oliver Stannard | 7ab6060 | 2017-12-04 13:42:22 +0000 | [diff] [blame] | 3713 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: multiple \"\n"; |
| 3714 | OS << " \"operand mismatches, ignoring \"\n"; |
| 3715 | OS << " \"this opcode\\n\");\n"; |
| 3716 | OS << " continue;\n"; |
| 3717 | OS << " }\n"; |
Chris Lattner | b4be28f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 3718 | |
| 3719 | // Emit check that the required features are available. |
Sander de Smalen | cd6be96 | 2017-12-20 11:02:42 +0000 | [diff] [blame] | 3720 | OS << " if (!HasRequiredFeatures) {\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3721 | if (!ReportMultipleNearMisses) |
| 3722 | OS << " HadMatchOtherThanFeatures = true;\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3723 | OS << " FeatureBitset NewMissingFeatures = RequiredFeatures & " |
Jim Grosbach | 9ec06a15 | 2012-06-18 19:45:46 +0000 | [diff] [blame] | 3724 | "~AvailableFeatures;\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3725 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Missing target features:\";\n"; |
| 3726 | OS << " for (unsigned I = 0, E = NewMissingFeatures.size(); I != E; ++I)\n"; |
| 3727 | OS << " if (NewMissingFeatures[I])\n"; |
| 3728 | OS << " dbgs() << ' ' << I;\n"; |
| 3729 | OS << " dbgs() << \"\\n\");\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3730 | if (ReportMultipleNearMisses) { |
| 3731 | OS << " FeaturesNearMiss = NearMissInfo::getMissedFeature(NewMissingFeatures);\n"; |
| 3732 | } else { |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3733 | OS << " if (NewMissingFeatures.count() <=\n" |
| 3734 | " MissingFeatures.count())\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3735 | OS << " MissingFeatures = NewMissingFeatures;\n"; |
| 3736 | OS << " continue;\n"; |
| 3737 | } |
Chris Lattner | b4be28f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 3738 | OS << " }\n"; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3739 | OS << "\n"; |
Ahmed Bougacha | 0dc1979 | 2014-12-16 18:05:28 +0000 | [diff] [blame] | 3740 | OS << " Inst.clear();\n\n"; |
Daniel Sanders | c553742 | 2016-07-27 13:49:44 +0000 | [diff] [blame] | 3741 | OS << " Inst.setOpcode(it->Opcode);\n"; |
| 3742 | // Verify the instruction with the target-specific match predicate function. |
| 3743 | OS << " // We have a potential match but have not rendered the operands.\n" |
| 3744 | << " // Check the target predicate to handle any context sensitive\n" |
| 3745 | " // constraints.\n" |
| 3746 | << " // For example, Ties that are referenced multiple times must be\n" |
| 3747 | " // checked here to ensure the input is the same for each match\n" |
| 3748 | " // constraints. If we leave it any later the ties will have been\n" |
| 3749 | " // canonicalized\n" |
| 3750 | << " unsigned MatchResult;\n" |
| 3751 | << " if ((MatchResult = checkEarlyTargetMatchPredicate(Inst, " |
| 3752 | "Operands)) != Match_Success) {\n" |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3753 | << " Inst.clear();\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3754 | OS << " DEBUG_WITH_TYPE(\n"; |
| 3755 | OS << " \"asm-matcher\",\n"; |
| 3756 | OS << " dbgs() << \"Early target match predicate failed with diag code \"\n"; |
| 3757 | OS << " << MatchResult << \"\\n\");\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3758 | if (ReportMultipleNearMisses) { |
| 3759 | OS << " EarlyPredicateNearMiss = NearMissInfo::getMissedPredicate(MatchResult);\n"; |
| 3760 | } else { |
| 3761 | OS << " RetCode = MatchResult;\n" |
| 3762 | << " HadMatchOtherThanPredicate = true;\n" |
| 3763 | << " continue;\n"; |
| 3764 | } |
| 3765 | OS << " }\n\n"; |
| 3766 | |
| 3767 | if (ReportMultipleNearMisses) { |
| 3768 | OS << " // If we did not successfully match the operands, then we can't convert to\n"; |
| 3769 | OS << " // an MCInst, so bail out on this instruction variant now.\n"; |
| 3770 | OS << " if (OperandNearMiss) {\n"; |
| 3771 | OS << " // If the operand mismatch was the only problem, reprrt it as a near-miss.\n"; |
| 3772 | OS << " if (NearMisses && !FeaturesNearMiss && !EarlyPredicateNearMiss) {\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3773 | OS << " DEBUG_WITH_TYPE(\n"; |
| 3774 | OS << " \"asm-matcher\",\n"; |
| 3775 | OS << " dbgs()\n"; |
| 3776 | OS << " << \"Opcode result: one mismatched operand, adding near-miss\\n\");\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3777 | OS << " NearMisses->push_back(OperandNearMiss);\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3778 | OS << " } else {\n"; |
| 3779 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: multiple \"\n"; |
| 3780 | OS << " \"types of mismatch, so not \"\n"; |
| 3781 | OS << " \"reporting near-miss\\n\");\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3782 | OS << " }\n"; |
| 3783 | OS << " continue;\n"; |
| 3784 | OS << " }\n\n"; |
| 3785 | } |
| 3786 | |
Chad Rosier | f4e35dc | 2012-10-01 23:45:51 +0000 | [diff] [blame] | 3787 | OS << " if (matchingInlineAsm) {\n"; |
Chad Rosier | 2f480a8 | 2012-10-12 22:53:36 +0000 | [diff] [blame] | 3788 | OS << " convertToMapAndConstraints(it->ConvertFn, Operands);\n"; |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 3789 | if (!ReportMultipleNearMisses) { |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 3790 | OS << " if (!checkAsmTiedOperandConstraints(*this, it->ConvertFn, " |
| 3791 | "Operands, ErrorInfo))\n"; |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 3792 | OS << " return Match_InvalidTiedOperand;\n"; |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 3793 | OS << "\n"; |
| 3794 | } |
Chad Rosier | f4e35dc | 2012-10-01 23:45:51 +0000 | [diff] [blame] | 3795 | OS << " return Match_Success;\n"; |
| 3796 | OS << " }\n\n"; |
Daniel Dunbar | 6619340 | 2011-02-04 17:12:23 +0000 | [diff] [blame] | 3797 | OS << " // We have selected a definite instruction, convert the parsed\n" |
| 3798 | << " // operands into the appropriate MCInst.\n"; |
Sam Kolton | 5f10a13 | 2016-05-06 11:31:17 +0000 | [diff] [blame] | 3799 | if (HasOptionalOperands) { |
| 3800 | OS << " convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands,\n" |
| 3801 | << " OptionalOperandsMask);\n"; |
| 3802 | } else { |
| 3803 | OS << " convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n"; |
| 3804 | } |
Daniel Dunbar | 6619340 | 2011-02-04 17:12:23 +0000 | [diff] [blame] | 3805 | OS << "\n"; |
Daniel Dunbar | 451a435 | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 3806 | |
Jim Grosbach | 120a96a | 2011-08-15 23:03:29 +0000 | [diff] [blame] | 3807 | // Verify the instruction with the target-specific match predicate function. |
| 3808 | OS << " // We have a potential match. Check the target predicate to\n" |
| 3809 | << " // handle any context sensitive constraints.\n" |
Jim Grosbach | 120a96a | 2011-08-15 23:03:29 +0000 | [diff] [blame] | 3810 | << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !=" |
| 3811 | << " Match_Success) {\n" |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3812 | << " DEBUG_WITH_TYPE(\"asm-matcher\",\n" |
| 3813 | << " dbgs() << \"Target match predicate failed with diag code \"\n" |
| 3814 | << " << MatchResult << \"\\n\");\n" |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3815 | << " Inst.clear();\n"; |
| 3816 | if (ReportMultipleNearMisses) { |
| 3817 | OS << " LatePredicateNearMiss = NearMissInfo::getMissedPredicate(MatchResult);\n"; |
| 3818 | } else { |
| 3819 | OS << " RetCode = MatchResult;\n" |
| 3820 | << " HadMatchOtherThanPredicate = true;\n" |
| 3821 | << " continue;\n"; |
| 3822 | } |
| 3823 | OS << " }\n\n"; |
| 3824 | |
| 3825 | if (ReportMultipleNearMisses) { |
| 3826 | OS << " int NumNearMisses = ((int)(bool)OperandNearMiss +\n"; |
| 3827 | OS << " (int)(bool)FeaturesNearMiss +\n"; |
| 3828 | OS << " (int)(bool)EarlyPredicateNearMiss +\n"; |
| 3829 | OS << " (int)(bool)LatePredicateNearMiss);\n"; |
| 3830 | OS << " if (NumNearMisses == 1) {\n"; |
| 3831 | OS << " // We had exactly one type of near-miss, so add that to the list.\n"; |
| 3832 | OS << " assert(!OperandNearMiss && \"OperandNearMiss was handled earlier\");\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3833 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: found one type of \"\n"; |
| 3834 | OS << " \"mismatch, so reporting a \"\n"; |
| 3835 | OS << " \"near-miss\\n\");\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3836 | OS << " if (NearMisses && FeaturesNearMiss)\n"; |
| 3837 | OS << " NearMisses->push_back(FeaturesNearMiss);\n"; |
| 3838 | OS << " else if (NearMisses && EarlyPredicateNearMiss)\n"; |
| 3839 | OS << " NearMisses->push_back(EarlyPredicateNearMiss);\n"; |
| 3840 | OS << " else if (NearMisses && LatePredicateNearMiss)\n"; |
| 3841 | OS << " NearMisses->push_back(LatePredicateNearMiss);\n"; |
| 3842 | OS << "\n"; |
| 3843 | OS << " continue;\n"; |
| 3844 | OS << " } else if (NumNearMisses > 1) {\n"; |
| 3845 | OS << " // This instruction missed in more than one way, so ignore it.\n"; |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3846 | OS << " DEBUG_WITH_TYPE(\"asm-matcher\", dbgs() << \"Opcode result: multiple \"\n"; |
| 3847 | OS << " \"types of mismatch, so not \"\n"; |
| 3848 | OS << " \"reporting near-miss\\n\");\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3849 | OS << " continue;\n"; |
| 3850 | OS << " }\n"; |
| 3851 | } |
Jim Grosbach | 120a96a | 2011-08-15 23:03:29 +0000 | [diff] [blame] | 3852 | |
Daniel Dunbar | 451a435 | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 3853 | // Call the post-processing function, if used. |
Craig Topper | bcd3c37 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 3854 | StringRef InsnCleanupFn = AsmParser->getValueAsString("AsmParserInstCleanup"); |
Daniel Dunbar | 451a435 | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 3855 | if (!InsnCleanupFn.empty()) |
| 3856 | OS << " " << InsnCleanupFn << "(Inst);\n"; |
| 3857 | |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 3858 | if (HasDeprecation) { |
| 3859 | OS << " std::string Info;\n"; |
Weiming Zhao | b38cfce | 2016-12-05 23:55:13 +0000 | [diff] [blame] | 3860 | OS << " if (!getParser().getTargetParser().\n"; |
| 3861 | OS << " getTargetOptions().MCNoDeprecatedWarn &&\n"; |
| 3862 | OS << " MII.get(Inst.getOpcode()).getDeprecatedInfo(Inst, getSTI(), Info)) {\n"; |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 3863 | OS << " SMLoc Loc = ((" << Target.getName() |
| 3864 | << "Operand&)*Operands[0]).getStartLoc();\n"; |
Rafael Espindola | 961d469 | 2014-11-11 05:18:41 +0000 | [diff] [blame] | 3865 | OS << " getParser().Warning(Loc, Info, None);\n"; |
Joey Gouly | 0e76fa7 | 2013-09-12 10:28:05 +0000 | [diff] [blame] | 3866 | OS << " }\n"; |
| 3867 | } |
| 3868 | |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 3869 | if (!ReportMultipleNearMisses) { |
Sander de Smalen | 118099a | 2018-06-18 13:39:29 +0000 | [diff] [blame] | 3870 | OS << " if (!checkAsmTiedOperandConstraints(*this, it->ConvertFn, " |
| 3871 | "Operands, ErrorInfo))\n"; |
Craig Topper | 773ead2 | 2018-04-25 06:24:51 +0000 | [diff] [blame] | 3872 | OS << " return Match_InvalidTiedOperand;\n"; |
Sander de Smalen | 886510f | 2018-01-10 10:10:56 +0000 | [diff] [blame] | 3873 | OS << "\n"; |
| 3874 | } |
| 3875 | |
Oliver Stannard | 4191b9e | 2017-10-11 09:17:43 +0000 | [diff] [blame] | 3876 | OS << " DEBUG_WITH_TYPE(\n"; |
| 3877 | OS << " \"asm-matcher\",\n"; |
| 3878 | OS << " dbgs() << \"Opcode result: complete match, selecting this opcode\\n\");\n"; |
Chris Lattner | a22a368 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 3879 | OS << " return Match_Success;\n"; |
Daniel Dunbar | 541efcc | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 3880 | OS << " }\n\n"; |
| 3881 | |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3882 | if (ReportMultipleNearMisses) { |
| 3883 | OS << " // No instruction variants matched exactly.\n"; |
| 3884 | OS << " return Match_NearMisses;\n"; |
| 3885 | } else { |
| 3886 | OS << " // Okay, we had no match. Try to return a useful error code.\n"; |
| 3887 | OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)\n"; |
| 3888 | OS << " return RetCode;\n\n"; |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3889 | OS << " ErrorInfo = 0;\n"; |
Oliver Stannard | 65f7bc5 | 2017-10-03 09:33:12 +0000 | [diff] [blame] | 3890 | OS << " return Match_MissingFeature;\n"; |
| 3891 | } |
Daniel Dunbar | b6d6aa2 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 3892 | OS << "}\n\n"; |
Jim Grosbach | 0eccfc2 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 3893 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 3894 | if (!Info.OperandMatchInfo.empty()) |
Craig Topper | 7ecfa6d | 2012-09-18 07:02:21 +0000 | [diff] [blame] | 3895 | emitCustomOperandParsing(OS, Target, Info, ClassName, StringTable, |
Stanislav Mekhanoshin | e98944e | 2019-03-11 17:04:35 +0000 | [diff] [blame] | 3896 | MaxMnemonicIndex, FeatureBitsets.size(), |
| 3897 | HasMnemonicFirst); |
Bruno Cardoso Lopes | 2315beb | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 3898 | |
Chris Lattner | 3e4582a | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 3899 | OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n"; |
Craig Topper | 2a06028 | 2017-10-26 06:46:40 +0000 | [diff] [blame] | 3900 | |
| 3901 | OS << "\n#ifdef GET_MNEMONIC_SPELL_CHECKER\n"; |
| 3902 | OS << "#undef GET_MNEMONIC_SPELL_CHECKER\n\n"; |
| 3903 | |
| 3904 | emitMnemonicSpellChecker(OS, Target, VariantCount); |
| 3905 | |
| 3906 | OS << "#endif // GET_MNEMONIC_SPELL_CHECKER\n\n"; |
Daniel Dunbar | 3085b57 | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 3907 | } |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 3908 | |
| 3909 | namespace llvm { |
| 3910 | |
| 3911 | void EmitAsmMatcher(RecordKeeper &RK, raw_ostream &OS) { |
| 3912 | emitSourceFileHeader("Assembly Matcher Source Fragment", OS); |
| 3913 | AsmMatcherEmitter(RK).run(OS); |
| 3914 | } |
| 3915 | |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 3916 | } // end namespace llvm |