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