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