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 |
| 80 | // identifiers that need to be mapped to specific valeus in the final encoded |
| 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 | |
| 99 | #include "AsmMatcherEmitter.h" |
| 100 | #include "CodeGenTarget.h" |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 101 | #include "StringMatcher.h" |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 102 | #include "StringToOffsetTable.h" |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 103 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 104 | #include "llvm/ADT/PointerUnion.h" |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 105 | #include "llvm/ADT/SmallPtrSet.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 106 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 107 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 108 | #include "llvm/ADT/StringExtras.h" |
| 109 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 110 | #include "llvm/Support/Debug.h" |
Craig Topper | 655b8de | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 111 | #include "llvm/Support/ErrorHandling.h" |
Peter Collingbourne | 7c78888 | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 112 | #include "llvm/TableGen/Error.h" |
| 113 | #include "llvm/TableGen/Record.h" |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 114 | #include <map> |
| 115 | #include <set> |
Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 116 | using namespace llvm; |
| 117 | |
Daniel Dunbar | 2724915 | 2009-08-07 20:33:39 +0000 | [diff] [blame] | 118 | static cl::opt<std::string> |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 119 | MatchPrefix("match-prefix", cl::init(""), |
| 120 | cl::desc("Only match instructions with the given prefix")); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 122 | namespace { |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 123 | class AsmMatcherInfo; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 124 | struct SubtargetFeatureInfo; |
| 125 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 126 | /// ClassInfo - Helper class for storing the information about a particular |
| 127 | /// class of operands which can be matched. |
| 128 | struct ClassInfo { |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 129 | enum ClassInfoKind { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 130 | /// Invalid kind, for use as a sentinel value. |
| 131 | Invalid = 0, |
| 132 | |
| 133 | /// The class for a particular token. |
| 134 | Token, |
| 135 | |
| 136 | /// The (first) register class, subsequent register classes are |
| 137 | /// RegisterClass0+1, and so on. |
| 138 | RegisterClass0, |
| 139 | |
| 140 | /// The (first) user defined class, subsequent user defined classes are |
| 141 | /// UserClass0+1, and so on. |
| 142 | UserClass0 = 1<<16 |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /// Kind - The class kind, which is either a predefined kind, or (UserClass0 + |
| 146 | /// N) for the Nth user defined class. |
| 147 | unsigned Kind; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 148 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 149 | /// SuperClasses - The super classes of this class. Note that for simplicities |
| 150 | /// sake user operands only record their immediate super class, while register |
| 151 | /// operands include all superclasses. |
| 152 | std::vector<ClassInfo*> SuperClasses; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 153 | |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 154 | /// Name - The full class name, suitable for use in an enum. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 155 | std::string Name; |
| 156 | |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 157 | /// ClassName - The unadorned generic name for this class (e.g., Token). |
| 158 | std::string ClassName; |
| 159 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 160 | /// ValueName - The name of the value this class represents; for a token this |
| 161 | /// is the literal token string, for an operand it is the TableGen class (or |
| 162 | /// empty if this is a derived class). |
| 163 | std::string ValueName; |
| 164 | |
| 165 | /// PredicateMethod - The name of the operand method to test whether the |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 166 | /// 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] | 167 | std::string PredicateMethod; |
| 168 | |
| 169 | /// 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] | 170 | /// MCInst; this is not valid for Token or register kinds. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 171 | std::string RenderMethod; |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 172 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 173 | /// ParserMethod - The name of the operand method to do a target specific |
| 174 | /// parsing on the operand. |
| 175 | std::string ParserMethod; |
| 176 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 177 | /// For register classes, the records for all the registers in this class. |
| 178 | std::set<Record*> Registers; |
| 179 | |
| 180 | public: |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 181 | /// isRegisterClass() - Check if this is a register class. |
| 182 | bool isRegisterClass() const { |
| 183 | return Kind >= RegisterClass0 && Kind < UserClass0; |
| 184 | } |
| 185 | |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 186 | /// isUserClass() - Check if this is a user defined class. |
| 187 | bool isUserClass() const { |
| 188 | return Kind >= UserClass0; |
| 189 | } |
| 190 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 191 | /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes |
| 192 | /// are related if they are in the same class hierarchy. |
| 193 | bool isRelatedTo(const ClassInfo &RHS) const { |
| 194 | // Tokens are only related to tokens. |
| 195 | if (Kind == Token || RHS.Kind == Token) |
| 196 | return Kind == Token && RHS.Kind == Token; |
| 197 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 198 | // Registers classes are only related to registers classes, and only if |
| 199 | // their intersection is non-empty. |
| 200 | if (isRegisterClass() || RHS.isRegisterClass()) { |
| 201 | if (!isRegisterClass() || !RHS.isRegisterClass()) |
| 202 | return false; |
| 203 | |
| 204 | std::set<Record*> Tmp; |
| 205 | std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin()); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 206 | std::set_intersection(Registers.begin(), Registers.end(), |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 207 | RHS.Registers.begin(), RHS.Registers.end(), |
| 208 | II); |
| 209 | |
| 210 | return !Tmp.empty(); |
| 211 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 212 | |
| 213 | // Otherwise we have two users operands; they are related if they are in the |
| 214 | // same class hierarchy. |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 215 | // |
| 216 | // FIXME: This is an oversimplification, they should only be related if they |
| 217 | // intersect, however we don't have that information. |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 218 | assert(isUserClass() && RHS.isUserClass() && "Unexpected class!"); |
| 219 | const ClassInfo *Root = this; |
| 220 | while (!Root->SuperClasses.empty()) |
| 221 | Root = Root->SuperClasses.front(); |
| 222 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 223 | const ClassInfo *RHSRoot = &RHS; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 224 | while (!RHSRoot->SuperClasses.empty()) |
| 225 | RHSRoot = RHSRoot->SuperClasses.front(); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 226 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 227 | return Root == RHSRoot; |
| 228 | } |
| 229 | |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 230 | /// isSubsetOf - Test whether this class is a subset of \arg RHS; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 231 | bool isSubsetOf(const ClassInfo &RHS) const { |
| 232 | // This is a subset of RHS if it is the same class... |
| 233 | if (this == &RHS) |
| 234 | return true; |
| 235 | |
| 236 | // ... or if any of its super classes are a subset of RHS. |
| 237 | for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(), |
| 238 | ie = SuperClasses.end(); it != ie; ++it) |
| 239 | if ((*it)->isSubsetOf(RHS)) |
| 240 | return true; |
| 241 | |
| 242 | return false; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 245 | /// operator< - Compare two classes. |
| 246 | bool operator<(const ClassInfo &RHS) const { |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 247 | if (this == &RHS) |
| 248 | return false; |
| 249 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 250 | // Unrelated classes can be ordered by kind. |
| 251 | if (!isRelatedTo(RHS)) |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 252 | return Kind < RHS.Kind; |
| 253 | |
| 254 | switch (Kind) { |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 255 | case Invalid: |
Craig Topper | 655b8de | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 256 | llvm_unreachable("Invalid kind!"); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 257 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 258 | default: |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 259 | // 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] | 260 | if (isSubsetOf(RHS)) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 261 | return true; |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 262 | if (RHS.isSubsetOf(*this)) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 263 | return false; |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 264 | |
| 265 | // Otherwise, order by name to ensure we have a total ordering. |
| 266 | return ValueName < RHS.ValueName; |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 271 | /// MatchableInfo - Helper class for storing the necessary information for an |
| 272 | /// instruction or alias which is capable of being matched. |
| 273 | struct MatchableInfo { |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 274 | struct AsmOperand { |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 275 | /// Token - This is the token that the operand came from. |
| 276 | StringRef Token; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 277 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 278 | /// The unique class instance this operand should match. |
| 279 | ClassInfo *Class; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 567820c | 2010-11-04 01:42:59 +0000 | [diff] [blame] | 281 | /// The operand name this is, if anything. |
| 282 | StringRef SrcOpName; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 283 | |
| 284 | /// The suboperand index within SrcOpName, or -1 for the entire operand. |
| 285 | int SubOpIdx; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 286 | |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 287 | /// Register record if this token is singleton register. |
| 288 | Record *SingletonReg; |
| 289 | |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 290 | explicit AsmOperand(StringRef T) : Token(T), Class(0), SubOpIdx(-1), |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 291 | SingletonReg(0) {} |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 292 | }; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 294 | /// ResOperand - This represents a single operand in the result instruction |
| 295 | /// generated by the match. In cases (like addressing modes) where a single |
| 296 | /// assembler operand expands to multiple MCOperands, this represents the |
| 297 | /// single assembler operand, not the MCOperand. |
| 298 | struct ResOperand { |
| 299 | enum { |
| 300 | /// RenderAsmOperand - This represents an operand result that is |
| 301 | /// generated by calling the render method on the assembly operand. The |
| 302 | /// corresponding AsmOperand is specified by AsmOperandNum. |
| 303 | RenderAsmOperand, |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 305 | /// TiedOperand - This represents a result operand that is a duplicate of |
| 306 | /// a previous result operand. |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 307 | TiedOperand, |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 309 | /// ImmOperand - This represents an immediate value that is dumped into |
| 310 | /// the operand. |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 311 | ImmOperand, |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 313 | /// RegOperand - This represents a fixed register that is dumped in. |
| 314 | RegOperand |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 315 | } Kind; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 317 | union { |
| 318 | /// This is the operand # in the AsmOperands list that this should be |
| 319 | /// copied from. |
| 320 | unsigned AsmOperandNum; |
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 | /// TiedOperandNum - This is the (earlier) result operand that should be |
| 323 | /// copied from. |
| 324 | unsigned TiedOperandNum; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 326 | /// ImmVal - This is the immediate value added to the instruction. |
| 327 | int64_t ImmVal; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 329 | /// Register - This is the register record. |
| 330 | Record *Register; |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 331 | }; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 332 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 333 | /// MINumOperands - The number of MCInst operands populated by this |
| 334 | /// operand. |
| 335 | unsigned MINumOperands; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 336 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 337 | static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) { |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 338 | ResOperand X; |
| 339 | X.Kind = RenderAsmOperand; |
| 340 | X.AsmOperandNum = AsmOpNum; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 341 | X.MINumOperands = NumOperands; |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 342 | return X; |
| 343 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 344 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 345 | static ResOperand getTiedOp(unsigned TiedOperandNum) { |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 346 | ResOperand X; |
| 347 | X.Kind = TiedOperand; |
| 348 | X.TiedOperandNum = TiedOperandNum; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 349 | X.MINumOperands = 1; |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 350 | return X; |
| 351 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 352 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 353 | static ResOperand getImmOp(int64_t Val) { |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 354 | ResOperand X; |
| 355 | X.Kind = ImmOperand; |
| 356 | X.ImmVal = Val; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 357 | X.MINumOperands = 1; |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 358 | return X; |
| 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 | static ResOperand getRegOp(Record *Reg) { |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 362 | ResOperand X; |
| 363 | X.Kind = RegOperand; |
| 364 | X.Register = Reg; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 365 | X.MINumOperands = 1; |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 366 | return X; |
| 367 | } |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 368 | }; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 369 | |
Devang Patel | 56315d3 | 2012-01-10 17:50:43 +0000 | [diff] [blame] | 370 | /// AsmVariantID - Target's assembly syntax variant no. |
| 371 | int AsmVariantID; |
| 372 | |
Chris Lattner | 3b5aec6 | 2010-11-02 17:34:28 +0000 | [diff] [blame] | 373 | /// TheDef - This is the definition of the instruction or InstAlias that this |
| 374 | /// matchable came from. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 375 | Record *const TheDef; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 376 | |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 377 | /// DefRec - This is the definition that it came from. |
| 378 | PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 662e5a3 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 380 | const CodeGenInstruction *getResultInst() const { |
| 381 | if (DefRec.is<const CodeGenInstruction*>()) |
| 382 | return DefRec.get<const CodeGenInstruction*>(); |
| 383 | return DefRec.get<const CodeGenInstAlias*>()->ResultInst; |
| 384 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 386 | /// ResOperands - This is the operand list that should be built for the result |
| 387 | /// MCInst. |
| 388 | std::vector<ResOperand> ResOperands; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 389 | |
| 390 | /// AsmString - The assembly string for this instruction (with variants |
Chris Lattner | 3b5aec6 | 2010-11-02 17:34:28 +0000 | [diff] [blame] | 391 | /// removed), e.g. "movsx $src, $dst". |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 392 | std::string AsmString; |
| 393 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 394 | /// Mnemonic - This is the first token of the matched instruction, its |
| 395 | /// mnemonic. |
| 396 | StringRef Mnemonic; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 398 | /// AsmOperands - The textual operands that this instruction matches, |
Chris Lattner | 3b5aec6 | 2010-11-02 17:34:28 +0000 | [diff] [blame] | 399 | /// annotated with a class and where in the OperandList they were defined. |
| 400 | /// This directly corresponds to the tokenized AsmString after the mnemonic is |
| 401 | /// removed. |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 402 | SmallVector<AsmOperand, 4> AsmOperands; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 403 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 404 | /// Predicates - The required subtarget features to match this instruction. |
| 405 | SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures; |
| 406 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 407 | /// ConversionFnKind - The enum value which is passed to the generated |
| 408 | /// ConvertToMCInst to convert parsed operands into an MCInst for this |
| 409 | /// function. |
| 410 | std::string ConversionFnKind; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 412 | MatchableInfo(const CodeGenInstruction &CGI) |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 413 | : AsmVariantID(0), TheDef(CGI.TheDef), DefRec(&CGI), |
Devang Patel | 56315d3 | 2012-01-10 17:50:43 +0000 | [diff] [blame] | 414 | AsmString(CGI.AsmString) { |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 417 | MatchableInfo(const CodeGenInstAlias *Alias) |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 418 | : AsmVariantID(0), TheDef(Alias->TheDef), DefRec(Alias), |
Devang Patel | 56315d3 | 2012-01-10 17:50:43 +0000 | [diff] [blame] | 419 | AsmString(Alias->AsmString) { |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 420 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 421 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 422 | void Initialize(const AsmMatcherInfo &Info, |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 423 | SmallPtrSet<Record*, 16> &SingletonRegisters, |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 424 | int AsmVariantNo, std::string &RegisterPrefix); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 426 | /// Validate - Return true if this matchable is a valid thing to match against |
| 427 | /// and perform a bunch of validity checking. |
| 428 | bool Validate(StringRef CommentDelimiter, bool Hack) const; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 429 | |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 430 | /// extractSingletonRegisterForAsmOperand - Extract singleton register, |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 431 | /// if present, from specified token. |
| 432 | void |
| 433 | extractSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info, |
| 434 | std::string &RegisterPrefix); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 435 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 436 | /// FindAsmOperand - Find the AsmOperand with the specified name and |
| 437 | /// suboperand index. |
| 438 | int FindAsmOperand(StringRef N, int SubOpIdx) const { |
| 439 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) |
| 440 | if (N == AsmOperands[i].SrcOpName && |
| 441 | SubOpIdx == AsmOperands[i].SubOpIdx) |
| 442 | return i; |
| 443 | return -1; |
| 444 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 445 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 446 | /// FindAsmOperandNamed - Find the first AsmOperand with the specified name. |
| 447 | /// This does not check the suboperand index. |
Chris Lattner | ba3b5b6 | 2010-11-04 01:55:23 +0000 | [diff] [blame] | 448 | int FindAsmOperandNamed(StringRef N) const { |
| 449 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) |
| 450 | if (N == AsmOperands[i].SrcOpName) |
| 451 | return i; |
| 452 | return -1; |
| 453 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 454 | |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 455 | void BuildInstructionResultOperands(); |
| 456 | void BuildAliasResultOperands(); |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 458 | /// operator< - Compare two matchables. |
| 459 | bool operator<(const MatchableInfo &RHS) const { |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 460 | // The primary comparator is the instruction mnemonic. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 461 | if (Mnemonic != RHS.Mnemonic) |
| 462 | return Mnemonic < RHS.Mnemonic; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 464 | if (AsmOperands.size() != RHS.AsmOperands.size()) |
| 465 | return AsmOperands.size() < RHS.AsmOperands.size(); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 466 | |
Daniel Dunbar | db2ddb5 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 467 | // Compare lexicographically by operand. The matcher validates that other |
Bob Wilson | 1f64ac4 | 2011-01-26 21:26:21 +0000 | [diff] [blame] | 468 | // orderings wouldn't be ambiguous using \see CouldMatchAmbiguouslyWith(). |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 469 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 470 | if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class) |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 471 | return true; |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 472 | if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class) |
Daniel Dunbar | db2ddb5 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | |
Bob Wilson | 1f64ac4 | 2011-01-26 21:26:21 +0000 | [diff] [blame] | 479 | /// CouldMatchAmbiguouslyWith - Check whether this matchable could |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 480 | /// ambiguously match the same set of operands as \arg RHS (without being a |
| 481 | /// strictly superior match). |
Bob Wilson | 1f64ac4 | 2011-01-26 21:26:21 +0000 | [diff] [blame] | 482 | bool CouldMatchAmbiguouslyWith(const MatchableInfo &RHS) { |
Chris Lattner | e66b7eb | 2010-11-01 23:57:23 +0000 | [diff] [blame] | 483 | // The primary comparator is the instruction mnemonic. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 484 | if (Mnemonic != RHS.Mnemonic) |
Chris Lattner | e66b7eb | 2010-11-01 23:57:23 +0000 | [diff] [blame] | 485 | return false; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 486 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 487 | // The number of operands is unambiguous. |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 488 | if (AsmOperands.size() != RHS.AsmOperands.size()) |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 489 | return false; |
| 490 | |
Daniel Dunbar | 1402f0b | 2010-01-23 00:26:16 +0000 | [diff] [blame] | 491 | // Otherwise, make sure the ordering of the two instructions is unambiguous |
| 492 | // by checking that either (a) a token or operand kind discriminates them, |
| 493 | // or (b) the ordering among equivalent kinds is consistent. |
| 494 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 495 | // Tokens and operand kinds are unambiguous (assuming a correct target |
| 496 | // specific parser). |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 497 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) |
| 498 | if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind || |
| 499 | AsmOperands[i].Class->Kind == ClassInfo::Token) |
| 500 | if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class || |
| 501 | *RHS.AsmOperands[i].Class < *AsmOperands[i].Class) |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 502 | return false; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 503 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 504 | // Otherwise, this operand could commute if all operands are equivalent, or |
| 505 | // there is a pair of operands that compare less than and a pair that |
| 506 | // compare greater than. |
| 507 | bool HasLT = false, HasGT = false; |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 508 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 509 | if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class) |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 510 | HasLT = true; |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 511 | if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class) |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 512 | HasGT = true; |
| 513 | } |
| 514 | |
| 515 | return !(HasLT ^ HasGT); |
| 516 | } |
| 517 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 518 | void dump(); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 519 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 520 | private: |
| 521 | void TokenizeAsmString(const AsmMatcherInfo &Info); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 522 | }; |
| 523 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 524 | /// SubtargetFeatureInfo - Helper class for storing information on a subtarget |
| 525 | /// feature which participates in instruction matching. |
| 526 | struct SubtargetFeatureInfo { |
| 527 | /// \brief The predicate record for this feature. |
| 528 | Record *TheDef; |
| 529 | |
| 530 | /// \brief An unique index assigned to represent this feature. |
| 531 | unsigned Index; |
| 532 | |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 533 | SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {} |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 534 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 535 | /// \brief The name of the enumerated constant identifying this feature. |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 536 | std::string getEnumName() const { |
| 537 | return "Feature_" + TheDef->getName(); |
| 538 | } |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 539 | }; |
| 540 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 541 | struct OperandMatchEntry { |
| 542 | unsigned OperandMask; |
| 543 | MatchableInfo* MI; |
| 544 | ClassInfo *CI; |
| 545 | |
| 546 | static OperandMatchEntry Create(MatchableInfo* mi, ClassInfo *ci, |
| 547 | unsigned opMask) { |
| 548 | OperandMatchEntry X; |
| 549 | X.OperandMask = opMask; |
| 550 | X.CI = ci; |
| 551 | X.MI = mi; |
| 552 | return X; |
| 553 | } |
| 554 | }; |
| 555 | |
| 556 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 557 | class AsmMatcherInfo { |
| 558 | public: |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 559 | /// Tracked Records |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 560 | RecordKeeper &Records; |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 561 | |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 562 | /// The tablegen AsmParser record. |
| 563 | Record *AsmParser; |
| 564 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 565 | /// Target - The target information. |
| 566 | CodeGenTarget &Target; |
| 567 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 568 | /// The classes which are needed for matching. |
| 569 | std::vector<ClassInfo*> Classes; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 570 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 571 | /// The information on the matchables to match. |
| 572 | std::vector<MatchableInfo*> Matchables; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 573 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 574 | /// Info for custom matching operands by user defined methods. |
| 575 | std::vector<OperandMatchEntry> OperandMatchInfo; |
| 576 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 577 | /// Map of Register records to their class information. |
| 578 | std::map<Record*, ClassInfo*> RegisterClasses; |
| 579 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 580 | /// Map of Predicate records to their subtarget information. |
| 581 | std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 582 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 583 | private: |
| 584 | /// Map of token to class information which has already been constructed. |
| 585 | std::map<std::string, ClassInfo*> TokenClasses; |
| 586 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 587 | /// Map of RegisterClass records to their class information. |
| 588 | std::map<Record*, ClassInfo*> RegisterClassClasses; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 589 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 590 | /// Map of AsmOperandClass records to their class information. |
| 591 | std::map<Record*, ClassInfo*> AsmOperandClasses; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 592 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 593 | private: |
| 594 | /// getTokenClass - Lookup or create the class for the given token. |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 595 | ClassInfo *getTokenClass(StringRef Token); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 596 | |
| 597 | /// getOperandClass - Lookup or create the class for the given operand. |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 598 | ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI, |
Jim Grosbach | 48c1f84 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 599 | int SubOpIdx); |
| 600 | ClassInfo *getOperandClass(Record *Rec, int SubOpIdx); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 601 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 602 | /// BuildRegisterClasses - Build the ClassInfo* instances for register |
| 603 | /// classes. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 604 | void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 605 | |
| 606 | /// BuildOperandClasses - Build the ClassInfo* instances for user defined |
| 607 | /// operand classes. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 608 | void BuildOperandClasses(); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 609 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 610 | void BuildInstructionOperandReference(MatchableInfo *II, StringRef OpName, |
| 611 | unsigned AsmOpIdx); |
| 612 | void BuildAliasOperandReference(MatchableInfo *II, StringRef OpName, |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 613 | MatchableInfo::AsmOperand &Op); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 614 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 615 | public: |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 616 | AsmMatcherInfo(Record *AsmParser, |
| 617 | CodeGenTarget &Target, |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 618 | RecordKeeper &Records); |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 619 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 620 | /// BuildInfo - Construct the various tables used during matching. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 621 | void BuildInfo(); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 622 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 623 | /// BuildOperandMatchInfo - Build the necessary information to handle user |
| 624 | /// defined operand parsing methods. |
| 625 | void BuildOperandMatchInfo(); |
| 626 | |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 627 | /// getSubtargetFeature - Lookup or create the subtarget feature info for the |
| 628 | /// given operand. |
| 629 | SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const { |
| 630 | assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!"); |
| 631 | std::map<Record*, SubtargetFeatureInfo*>::const_iterator I = |
| 632 | SubtargetFeatures.find(Def); |
| 633 | return I == SubtargetFeatures.end() ? 0 : I->second; |
| 634 | } |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 636 | RecordKeeper &getRecords() const { |
| 637 | return Records; |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 638 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 639 | }; |
| 640 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 643 | void MatchableInfo::dump() { |
Chris Lattner | 5abd1eb | 2010-11-06 06:43:11 +0000 | [diff] [blame] | 644 | errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 646 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 647 | AsmOperand &Op = AsmOperands[i]; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 648 | errs() << " op[" << i << "] = " << Op.Class->ClassName << " - "; |
Chris Lattner | 0bb780c | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 649 | errs() << '\"' << Op.Token << "\"\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 653 | void MatchableInfo::Initialize(const AsmMatcherInfo &Info, |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 654 | SmallPtrSet<Record*, 16> &SingletonRegisters, |
| 655 | int AsmVariantNo, std::string &RegisterPrefix) { |
Devang Patel | 56315d3 | 2012-01-10 17:50:43 +0000 | [diff] [blame] | 656 | AsmVariantID = AsmVariantNo; |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 657 | AsmString = |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 658 | CodeGenInstruction::FlattenAsmStringVariants(AsmString, AsmVariantNo); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 659 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 660 | TokenizeAsmString(Info); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 661 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 662 | // Compute the require features. |
| 663 | std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates"); |
| 664 | for (unsigned i = 0, e = Predicates.size(); i != e; ++i) |
| 665 | if (SubtargetFeatureInfo *Feature = |
| 666 | Info.getSubtargetFeature(Predicates[i])) |
| 667 | RequiredFeatures.push_back(Feature); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 668 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 669 | // Collect singleton registers, if used. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 670 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 671 | extractSingletonRegisterForAsmOperand(i, Info, RegisterPrefix); |
| 672 | if (Record *Reg = AsmOperands[i].SingletonReg) |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 673 | SingletonRegisters.insert(Reg); |
| 674 | } |
| 675 | } |
| 676 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 677 | /// TokenizeAsmString - Tokenize a simplified assembly string. |
| 678 | void MatchableInfo::TokenizeAsmString(const AsmMatcherInfo &Info) { |
| 679 | StringRef String = AsmString; |
| 680 | unsigned Prev = 0; |
| 681 | bool InTok = true; |
| 682 | for (unsigned i = 0, e = String.size(); i != e; ++i) { |
| 683 | switch (String[i]) { |
| 684 | case '[': |
| 685 | case ']': |
| 686 | case '*': |
| 687 | case '!': |
| 688 | case ' ': |
| 689 | case '\t': |
| 690 | case ',': |
| 691 | if (InTok) { |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 692 | AsmOperands.push_back(AsmOperand(String.slice(Prev, i))); |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 693 | InTok = false; |
| 694 | } |
| 695 | if (!isspace(String[i]) && String[i] != ',') |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 696 | AsmOperands.push_back(AsmOperand(String.substr(i, 1))); |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 697 | Prev = i + 1; |
| 698 | break; |
| 699 | |
| 700 | case '\\': |
| 701 | if (InTok) { |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 702 | AsmOperands.push_back(AsmOperand(String.slice(Prev, i))); |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 703 | InTok = false; |
| 704 | } |
| 705 | ++i; |
| 706 | assert(i != String.size() && "Invalid quoted character"); |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 707 | AsmOperands.push_back(AsmOperand(String.substr(i, 1))); |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 708 | Prev = i + 1; |
| 709 | break; |
| 710 | |
| 711 | case '$': { |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 712 | if (InTok) { |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 713 | AsmOperands.push_back(AsmOperand(String.slice(Prev, i))); |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 714 | InTok = false; |
| 715 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 7ad3147 | 2010-11-06 22:06:03 +0000 | [diff] [blame] | 717 | // If this isn't "${", treat like a normal token. |
| 718 | if (i + 1 == String.size() || String[i + 1] != '{') { |
| 719 | Prev = i; |
| 720 | break; |
| 721 | } |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 722 | |
| 723 | StringRef::iterator End = std::find(String.begin() + i, String.end(),'}'); |
| 724 | assert(End != String.end() && "Missing brace in operand reference!"); |
| 725 | size_t EndPos = End - String.begin(); |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 726 | AsmOperands.push_back(AsmOperand(String.slice(i, EndPos+1))); |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 727 | Prev = EndPos + 1; |
| 728 | i = EndPos; |
| 729 | break; |
| 730 | } |
| 731 | |
| 732 | case '.': |
| 733 | if (InTok) |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 734 | AsmOperands.push_back(AsmOperand(String.slice(Prev, i))); |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 735 | Prev = i; |
| 736 | InTok = true; |
| 737 | break; |
| 738 | |
| 739 | default: |
| 740 | InTok = true; |
| 741 | } |
| 742 | } |
| 743 | if (InTok && Prev != String.size()) |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 744 | AsmOperands.push_back(AsmOperand(String.substr(Prev))); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 745 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 746 | // The first token of the instruction is the mnemonic, which must be a |
| 747 | // simple string, not a $foo variable or a singleton register. |
Jim Grosbach | 4a2242c | 2011-11-30 23:16:25 +0000 | [diff] [blame] | 748 | if (AsmOperands.empty()) |
| 749 | throw TGError(TheDef->getLoc(), |
| 750 | "Instruction '" + TheDef->getName() + "' has no tokens"); |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 751 | Mnemonic = AsmOperands[0].Token; |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 752 | // FIXME : Check and raise an error if it is a register. |
Devang Patel | b78307f | 2012-01-07 01:22:23 +0000 | [diff] [blame] | 753 | if (Mnemonic[0] == '$') |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 754 | throw TGError(TheDef->getLoc(), |
| 755 | "Invalid instruction mnemonic '" + Mnemonic.str() + "'!"); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 756 | |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 757 | // Remove the first operand, it is tracked in the mnemonic field. |
| 758 | AsmOperands.erase(AsmOperands.begin()); |
| 759 | } |
| 760 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 761 | bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const { |
| 762 | // Reject matchables with no .s string. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 763 | if (AsmString.empty()) |
| 764 | throw TGError(TheDef->getLoc(), "instruction with empty asm string"); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 765 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 766 | // Reject any matchables with a newline in them, they should be marked |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 767 | // isCodeGenOnly if they are pseudo instructions. |
| 768 | if (AsmString.find('\n') != std::string::npos) |
| 769 | throw TGError(TheDef->getLoc(), |
| 770 | "multiline instruction is not valid for the asmparser, " |
| 771 | "mark it isCodeGenOnly"); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 773 | // Remove comments from the asm string. We know that the asmstring only |
| 774 | // has one line. |
| 775 | if (!CommentDelimiter.empty() && |
| 776 | StringRef(AsmString).find(CommentDelimiter) != StringRef::npos) |
| 777 | throw TGError(TheDef->getLoc(), |
| 778 | "asmstring for instruction has comment character in it, " |
| 779 | "mark it isCodeGenOnly"); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 780 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 781 | // Reject matchables with operand modifiers, these aren't something we can |
Bob Wilson | 906bc36 | 2011-01-20 18:38:07 +0000 | [diff] [blame] | 782 | // handle, the target should be refactored to use operands instead of |
| 783 | // modifiers. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 784 | // |
| 785 | // Also, check for instructions which reference the operand multiple times; |
| 786 | // this implies a constraint we would not honor. |
| 787 | std::set<std::string> OperandNames; |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 788 | for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) { |
| 789 | StringRef Tok = AsmOperands[i].Token; |
| 790 | if (Tok[0] == '$' && Tok.find(':') != StringRef::npos) |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 791 | throw TGError(TheDef->getLoc(), |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 792 | "matchable with operand modifier '" + Tok.str() + |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 793 | "' not supported by asm matcher. Mark isCodeGenOnly!"); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 794 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 795 | // Verify that any operand is only mentioned once. |
Chris Lattner | d51257a | 2010-11-02 23:18:43 +0000 | [diff] [blame] | 796 | // We reject aliases and ignore instructions for now. |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 797 | if (Tok[0] == '$' && !OperandNames.insert(Tok).second) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 798 | if (!Hack) |
| 799 | throw TGError(TheDef->getLoc(), |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 800 | "ERROR: matchable with tied operand '" + Tok.str() + |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 801 | "' can never be matched!"); |
| 802 | // FIXME: Should reject these. The ARM backend hits this with $lane in a |
| 803 | // bunch of instructions. It is unclear what the right answer is. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 804 | DEBUG({ |
Chris Lattner | 5abd1eb | 2010-11-06 06:43:11 +0000 | [diff] [blame] | 805 | errs() << "warning: '" << TheDef->getName() << "': " |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 806 | << "ignoring instruction with tied operand '" |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 807 | << Tok.str() << "'\n"; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 808 | }); |
| 809 | return false; |
| 810 | } |
| 811 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 812 | |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 813 | return true; |
| 814 | } |
| 815 | |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 816 | /// extractSingletonRegisterForAsmOperand - Extract singleton register, |
Devang Patel | d06b01c | 2012-01-09 21:30:46 +0000 | [diff] [blame] | 817 | /// if present, from specified token. |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 818 | void MatchableInfo:: |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 819 | extractSingletonRegisterForAsmOperand(unsigned OperandNo, |
Devang Patel | d06b01c | 2012-01-09 21:30:46 +0000 | [diff] [blame] | 820 | const AsmMatcherInfo &Info, |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 821 | std::string &RegisterPrefix) { |
Devang Patel | d06b01c | 2012-01-09 21:30:46 +0000 | [diff] [blame] | 822 | StringRef Tok = AsmOperands[OperandNo].Token; |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 823 | if (RegisterPrefix.empty()) { |
Devang Patel | d06b01c | 2012-01-09 21:30:46 +0000 | [diff] [blame] | 824 | std::string LoweredTok = Tok.lower(); |
| 825 | if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(LoweredTok)) |
| 826 | AsmOperands[OperandNo].SingletonReg = Reg->TheDef; |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 827 | return; |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 828 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 829 | |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 830 | if (!Tok.startswith(RegisterPrefix)) |
| 831 | return; |
| 832 | |
| 833 | StringRef RegName = Tok.substr(RegisterPrefix.size()); |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 834 | if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName)) |
Devang Patel | d06b01c | 2012-01-09 21:30:46 +0000 | [diff] [blame] | 835 | AsmOperands[OperandNo].SingletonReg = Reg->TheDef; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 836 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 837 | // If there is no register prefix (i.e. "%" in "%eax"), then this may |
| 838 | // be some random non-register token, just ignore it. |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 839 | return; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 842 | static std::string getEnumNameForToken(StringRef Str) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 843 | std::string Res; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 844 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 845 | for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) { |
| 846 | switch (*it) { |
| 847 | case '*': Res += "_STAR_"; break; |
| 848 | case '%': Res += "_PCT_"; break; |
| 849 | case ':': Res += "_COLON_"; break; |
Bill Wendling | bd9c77b | 2010-11-18 23:36:54 +0000 | [diff] [blame] | 850 | case '!': Res += "_EXCLAIM_"; break; |
Bill Wendling | 0ef755d | 2011-01-22 09:44:32 +0000 | [diff] [blame] | 851 | case '.': Res += "_DOT_"; break; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 852 | default: |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 853 | if (isalnum(*it)) |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 854 | Res += *it; |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 855 | else |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 856 | Res += "_" + utostr((unsigned) *it) + "_"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | return Res; |
| 861 | } |
| 862 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 863 | ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 864 | ClassInfo *&Entry = TokenClasses[Token]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 865 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 866 | if (!Entry) { |
| 867 | Entry = new ClassInfo(); |
| 868 | Entry->Kind = ClassInfo::Token; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 869 | Entry->ClassName = "Token"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 870 | Entry->Name = "MCK_" + getEnumNameForToken(Token); |
| 871 | Entry->ValueName = Token; |
| 872 | Entry->PredicateMethod = "<invalid>"; |
| 873 | Entry->RenderMethod = "<invalid>"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 874 | Entry->ParserMethod = ""; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 875 | Classes.push_back(Entry); |
| 876 | } |
| 877 | |
| 878 | return Entry; |
| 879 | } |
| 880 | |
| 881 | ClassInfo * |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 882 | AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI, |
| 883 | int SubOpIdx) { |
| 884 | Record *Rec = OI.Rec; |
| 885 | if (SubOpIdx != -1) |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 886 | Rec = dynamic_cast<DefInit*>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef(); |
Jim Grosbach | 48c1f84 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 887 | return getOperandClass(Rec, SubOpIdx); |
| 888 | } |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 889 | |
Jim Grosbach | 48c1f84 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 890 | ClassInfo * |
| 891 | AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) { |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 892 | if (Rec->isSubClassOf("RegisterOperand")) { |
| 893 | // RegisterOperand may have an associated ParserMatchClass. If it does, |
| 894 | // use it, else just fall back to the underlying register class. |
| 895 | const RecordVal *R = Rec->getValue("ParserMatchClass"); |
| 896 | if (R == 0 || R->getValue() == 0) |
| 897 | throw "Record `" + Rec->getName() + |
| 898 | "' does not have a ParserMatchClass!\n"; |
| 899 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 900 | if (DefInit *DI= dynamic_cast<DefInit*>(R->getValue())) { |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 901 | Record *MatchClass = DI->getDef(); |
| 902 | if (ClassInfo *CI = AsmOperandClasses[MatchClass]) |
| 903 | return CI; |
| 904 | } |
| 905 | |
| 906 | // No custom match class. Just use the register class. |
| 907 | Record *ClassRec = Rec->getValueAsDef("RegClass"); |
| 908 | if (!ClassRec) |
| 909 | throw TGError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() + |
| 910 | "' has no associated register class!\n"); |
| 911 | if (ClassInfo *CI = RegisterClassClasses[ClassRec]) |
| 912 | return CI; |
| 913 | throw TGError(Rec->getLoc(), "register class has no class info!"); |
| 914 | } |
| 915 | |
| 916 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 917 | if (Rec->isSubClassOf("RegisterClass")) { |
| 918 | if (ClassInfo *CI = RegisterClassClasses[Rec]) |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 919 | return CI; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 920 | throw TGError(Rec->getLoc(), "register class has no class info!"); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 921 | } |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 922 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 923 | assert(Rec->isSubClassOf("Operand") && "Unexpected operand!"); |
| 924 | Record *MatchClass = Rec->getValueAsDef("ParserMatchClass"); |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 925 | if (ClassInfo *CI = AsmOperandClasses[MatchClass]) |
| 926 | return CI; |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 927 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 928 | throw TGError(Rec->getLoc(), "operand has no match class!"); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 931 | void AsmMatcherInfo:: |
| 932 | BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) { |
Jakob Stoklund Olesen | abdbc84 | 2011-06-18 04:26:06 +0000 | [diff] [blame] | 933 | const std::vector<CodeGenRegister*> &Registers = |
| 934 | Target.getRegBank().getRegisters(); |
Jakob Stoklund Olesen | 29f018c | 2011-09-29 22:28:37 +0000 | [diff] [blame] | 935 | ArrayRef<CodeGenRegisterClass*> RegClassList = |
| 936 | Target.getRegBank().getRegClasses(); |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 937 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 938 | // The register sets used for matching. |
| 939 | std::set< std::set<Record*> > RegisterSets; |
| 940 | |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 941 | // Gather the defined sets. |
Jakob Stoklund Olesen | 29f018c | 2011-09-29 22:28:37 +0000 | [diff] [blame] | 942 | for (ArrayRef<CodeGenRegisterClass*>::const_iterator it = |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 943 | RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) |
Jakob Stoklund Olesen | 29f018c | 2011-09-29 22:28:37 +0000 | [diff] [blame] | 944 | RegisterSets.insert(std::set<Record*>( |
| 945 | (*it)->getOrder().begin(), (*it)->getOrder().end())); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 946 | |
| 947 | // Add any required singleton sets. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 948 | for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(), |
| 949 | ie = SingletonRegisters.end(); it != ie; ++it) { |
| 950 | Record *Rec = *it; |
| 951 | RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1)); |
| 952 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 953 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 954 | // Introduce derived sets where necessary (when a register does not determine |
| 955 | // a unique register set class), and build the mapping of registers to the set |
| 956 | // they should classify to. |
| 957 | std::map<Record*, std::set<Record*> > RegisterMap; |
Jakob Stoklund Olesen | abdbc84 | 2011-06-18 04:26:06 +0000 | [diff] [blame] | 958 | for (std::vector<CodeGenRegister*>::const_iterator it = Registers.begin(), |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 959 | ie = Registers.end(); it != ie; ++it) { |
Jakob Stoklund Olesen | abdbc84 | 2011-06-18 04:26:06 +0000 | [diff] [blame] | 960 | const CodeGenRegister &CGR = **it; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 961 | // Compute the intersection of all sets containing this register. |
| 962 | std::set<Record*> ContainingSet; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 963 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 964 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 965 | ie = RegisterSets.end(); it != ie; ++it) { |
| 966 | if (!it->count(CGR.TheDef)) |
| 967 | continue; |
| 968 | |
| 969 | if (ContainingSet.empty()) { |
| 970 | ContainingSet = *it; |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 971 | continue; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 972 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 973 | |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 974 | std::set<Record*> Tmp; |
| 975 | std::swap(Tmp, ContainingSet); |
| 976 | std::insert_iterator< std::set<Record*> > II(ContainingSet, |
| 977 | ContainingSet.begin()); |
| 978 | std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | if (!ContainingSet.empty()) { |
| 982 | RegisterSets.insert(ContainingSet); |
| 983 | RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet)); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // Construct the register classes. |
| 988 | std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses; |
| 989 | unsigned Index = 0; |
| 990 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 991 | ie = RegisterSets.end(); it != ie; ++it, ++Index) { |
| 992 | ClassInfo *CI = new ClassInfo(); |
| 993 | CI->Kind = ClassInfo::RegisterClass0 + Index; |
| 994 | CI->ClassName = "Reg" + utostr(Index); |
| 995 | CI->Name = "MCK_Reg" + utostr(Index); |
| 996 | CI->ValueName = ""; |
| 997 | CI->PredicateMethod = ""; // unused |
| 998 | CI->RenderMethod = "addRegOperands"; |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 999 | CI->Registers = *it; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1000 | Classes.push_back(CI); |
| 1001 | RegisterSetClasses.insert(std::make_pair(*it, CI)); |
| 1002 | } |
| 1003 | |
| 1004 | // Find the superclasses; we could compute only the subgroup lattice edges, |
| 1005 | // but there isn't really a point. |
| 1006 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 1007 | ie = RegisterSets.end(); it != ie; ++it) { |
| 1008 | ClassInfo *CI = RegisterSetClasses[*it]; |
| 1009 | for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(), |
| 1010 | ie2 = RegisterSets.end(); it2 != ie2; ++it2) |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1011 | if (*it != *it2 && |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1012 | std::includes(it2->begin(), it2->end(), it->begin(), it->end())) |
| 1013 | CI->SuperClasses.push_back(RegisterSetClasses[*it2]); |
| 1014 | } |
| 1015 | |
| 1016 | // Name the register classes which correspond to a user defined RegisterClass. |
Jakob Stoklund Olesen | 29f018c | 2011-09-29 22:28:37 +0000 | [diff] [blame] | 1017 | for (ArrayRef<CodeGenRegisterClass*>::const_iterator |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 1018 | it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) { |
Jakob Stoklund Olesen | 29f018c | 2011-09-29 22:28:37 +0000 | [diff] [blame] | 1019 | const CodeGenRegisterClass &RC = **it; |
Jakob Stoklund Olesen | 6fea31e | 2011-10-04 15:28:08 +0000 | [diff] [blame] | 1020 | // Def will be NULL for non-user defined register classes. |
| 1021 | Record *Def = RC.getDef(); |
| 1022 | if (!Def) |
| 1023 | continue; |
Jakob Stoklund Olesen | 29f018c | 2011-09-29 22:28:37 +0000 | [diff] [blame] | 1024 | ClassInfo *CI = RegisterSetClasses[std::set<Record*>(RC.getOrder().begin(), |
| 1025 | RC.getOrder().end())]; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1026 | if (CI->ValueName.empty()) { |
Jakob Stoklund Olesen | 29f018c | 2011-09-29 22:28:37 +0000 | [diff] [blame] | 1027 | CI->ClassName = RC.getName(); |
| 1028 | CI->Name = "MCK_" + RC.getName(); |
| 1029 | CI->ValueName = RC.getName(); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1030 | } else |
Jakob Stoklund Olesen | 29f018c | 2011-09-29 22:28:37 +0000 | [diff] [blame] | 1031 | CI->ValueName = CI->ValueName + "," + RC.getName(); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1032 | |
Jakob Stoklund Olesen | 6fea31e | 2011-10-04 15:28:08 +0000 | [diff] [blame] | 1033 | RegisterClassClasses.insert(std::make_pair(Def, CI)); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | // Populate the map for individual registers. |
| 1037 | for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(), |
| 1038 | ie = RegisterMap.end(); it != ie; ++it) |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 1039 | RegisterClasses[it->first] = RegisterSetClasses[it->second]; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1040 | |
| 1041 | // Name the register classes which correspond to singleton registers. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1042 | for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(), |
| 1043 | ie = SingletonRegisters.end(); it != ie; ++it) { |
| 1044 | Record *Rec = *it; |
Chris Lattner | ec6f096 | 2010-11-02 18:10:06 +0000 | [diff] [blame] | 1045 | ClassInfo *CI = RegisterClasses[Rec]; |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1046 | assert(CI && "Missing singleton register class info!"); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1048 | if (CI->ValueName.empty()) { |
| 1049 | CI->ClassName = Rec->getName(); |
| 1050 | CI->Name = "MCK_" + Rec->getName(); |
| 1051 | CI->ValueName = Rec->getName(); |
| 1052 | } else |
| 1053 | CI->ValueName = CI->ValueName + "," + Rec->getName(); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1054 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1057 | void AsmMatcherInfo::BuildOperandClasses() { |
Chris Lattner | e66b7eb | 2010-11-01 23:57:23 +0000 | [diff] [blame] | 1058 | std::vector<Record*> AsmOperands = |
| 1059 | Records.getAllDerivedDefinitions("AsmOperandClass"); |
Daniel Dunbar | a2f5e00 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 1060 | |
| 1061 | // Pre-populate AsmOperandClasses map. |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1062 | for (std::vector<Record*>::iterator it = AsmOperands.begin(), |
Daniel Dunbar | a2f5e00 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 1063 | ie = AsmOperands.end(); it != ie; ++it) |
| 1064 | AsmOperandClasses[*it] = new ClassInfo(); |
| 1065 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1066 | unsigned Index = 0; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1067 | for (std::vector<Record*>::iterator it = AsmOperands.begin(), |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1068 | ie = AsmOperands.end(); it != ie; ++it, ++Index) { |
Daniel Dunbar | a2f5e00 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 1069 | ClassInfo *CI = AsmOperandClasses[*it]; |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1070 | CI->Kind = ClassInfo::UserClass0 + Index; |
| 1071 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1072 | ListInit *Supers = (*it)->getValueAsListInit("SuperClasses"); |
Daniel Dunbar | 54ddf3d | 2010-05-22 21:02:29 +0000 | [diff] [blame] | 1073 | for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1074 | DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i)); |
Daniel Dunbar | 54ddf3d | 2010-05-22 21:02:29 +0000 | [diff] [blame] | 1075 | if (!DI) { |
| 1076 | PrintError((*it)->getLoc(), "Invalid super class reference!"); |
| 1077 | continue; |
| 1078 | } |
| 1079 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1080 | ClassInfo *SC = AsmOperandClasses[DI->getDef()]; |
| 1081 | if (!SC) |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1082 | PrintError((*it)->getLoc(), "Invalid super class reference!"); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1083 | else |
| 1084 | CI->SuperClasses.push_back(SC); |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1085 | } |
| 1086 | CI->ClassName = (*it)->getValueAsString("Name"); |
| 1087 | CI->Name = "MCK_" + CI->ClassName; |
| 1088 | CI->ValueName = (*it)->getName(); |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1089 | |
| 1090 | // Get or construct the predicate method name. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1091 | Init *PMName = (*it)->getValueInit("PredicateMethod"); |
| 1092 | if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) { |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1093 | CI->PredicateMethod = SI->getValue(); |
| 1094 | } else { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1095 | assert(dynamic_cast<UnsetInit*>(PMName) && |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1096 | "Unexpected PredicateMethod field!"); |
| 1097 | CI->PredicateMethod = "is" + CI->ClassName; |
| 1098 | } |
| 1099 | |
| 1100 | // Get or construct the render method name. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1101 | Init *RMName = (*it)->getValueInit("RenderMethod"); |
| 1102 | if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) { |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1103 | CI->RenderMethod = SI->getValue(); |
| 1104 | } else { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1105 | assert(dynamic_cast<UnsetInit*>(RMName) && |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 1106 | "Unexpected RenderMethod field!"); |
| 1107 | CI->RenderMethod = "add" + CI->ClassName + "Operands"; |
| 1108 | } |
| 1109 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1110 | // Get the parse method name or leave it as empty. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1111 | Init *PRMName = (*it)->getValueInit("ParserMethod"); |
| 1112 | if (StringInit *SI = dynamic_cast<StringInit*>(PRMName)) |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1113 | CI->ParserMethod = SI->getValue(); |
| 1114 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 1115 | AsmOperandClasses[*it] = CI; |
| 1116 | Classes.push_back(CI); |
| 1117 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1120 | AsmMatcherInfo::AsmMatcherInfo(Record *asmParser, |
| 1121 | CodeGenTarget &target, |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 1122 | RecordKeeper &records) |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 1123 | : Records(records), AsmParser(asmParser), Target(target) { |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 1126 | /// BuildOperandMatchInfo - Build the necessary information to handle user |
| 1127 | /// defined operand parsing methods. |
| 1128 | void AsmMatcherInfo::BuildOperandMatchInfo() { |
| 1129 | |
Jim Grosbach | d4824fc | 2012-04-18 23:46:25 +0000 | [diff] [blame^] | 1130 | /// 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] | 1131 | /// that class inside a instruction. |
| 1132 | std::map<ClassInfo*, unsigned> OpClassMask; |
| 1133 | |
| 1134 | for (std::vector<MatchableInfo*>::const_iterator it = |
| 1135 | Matchables.begin(), ie = Matchables.end(); |
| 1136 | it != ie; ++it) { |
| 1137 | MatchableInfo &II = **it; |
| 1138 | OpClassMask.clear(); |
| 1139 | |
| 1140 | // Keep track of all operands of this instructions which belong to the |
| 1141 | // same class. |
| 1142 | for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) { |
| 1143 | MatchableInfo::AsmOperand &Op = II.AsmOperands[i]; |
| 1144 | if (Op.Class->ParserMethod.empty()) |
| 1145 | continue; |
| 1146 | unsigned &OperandMask = OpClassMask[Op.Class]; |
| 1147 | OperandMask |= (1 << i); |
| 1148 | } |
| 1149 | |
| 1150 | // Generate operand match info for each mnemonic/operand class pair. |
| 1151 | for (std::map<ClassInfo*, unsigned>::iterator iit = OpClassMask.begin(), |
| 1152 | iie = OpClassMask.end(); iit != iie; ++iit) { |
| 1153 | unsigned OpMask = iit->second; |
| 1154 | ClassInfo *CI = iit->first; |
| 1155 | OperandMatchInfo.push_back(OperandMatchEntry::Create(&II, CI, OpMask)); |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1160 | void AsmMatcherInfo::BuildInfo() { |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1161 | // Build information about all of the AssemblerPredicates. |
| 1162 | std::vector<Record*> AllPredicates = |
| 1163 | Records.getAllDerivedDefinitions("Predicate"); |
| 1164 | for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) { |
| 1165 | Record *Pred = AllPredicates[i]; |
| 1166 | // Ignore predicates that are not intended for the assembler. |
| 1167 | if (!Pred->getValueAsBit("AssemblerMatcherPredicate")) |
| 1168 | continue; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1169 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 1170 | if (Pred->getName().empty()) |
| 1171 | throw TGError(Pred->getLoc(), "Predicate has no name!"); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1172 | |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1173 | unsigned FeatureNo = SubtargetFeatures.size(); |
| 1174 | SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo); |
| 1175 | assert(FeatureNo < 32 && "Too many subtarget features!"); |
| 1176 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 1178 | // Parse the instructions; we need to do this first so that we can gather the |
| 1179 | // singleton register classes. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1180 | SmallPtrSet<Record*, 16> SingletonRegisters; |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1181 | unsigned VariantCount = Target.getAsmParserVariantCount(); |
| 1182 | for (unsigned VC = 0; VC != VariantCount; ++VC) { |
| 1183 | Record *AsmVariant = Target.getAsmParserVariant(VC); |
Jim Grosbach | 65da6fc | 2012-04-17 00:01:04 +0000 | [diff] [blame] | 1184 | std::string CommentDelimiter = |
| 1185 | AsmVariant->getValueAsString("CommentDelimiter"); |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1186 | std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix"); |
| 1187 | int AsmVariantNo = AsmVariant->getValueAsInt("Variant"); |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1188 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1189 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1190 | E = Target.inst_end(); I != E; ++I) { |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1191 | const CodeGenInstruction &CGI = **I; |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1192 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1193 | // If the tblgen -match-prefix option is specified (for tblgen hackers), |
| 1194 | // filter the set of instructions we consider. |
| 1195 | if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix)) |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1196 | continue; |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1197 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1198 | // Ignore "codegen only" instructions. |
| 1199 | if (CGI.TheDef->getValueAsBit("isCodeGenOnly")) |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1200 | continue; |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1201 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1202 | // Validate the operand list to ensure we can handle this instruction. |
| 1203 | for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) { |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1204 | const CGIOperandList::OperandInfo &OI = CGI.Operands[i]; |
| 1205 | |
| 1206 | // Validate tied operands. |
| 1207 | if (OI.getTiedRegister() != -1) { |
| 1208 | // If we have a tied operand that consists of multiple MCOperands, |
| 1209 | // reject it. We reject aliases and ignore instructions for now. |
| 1210 | if (OI.MINumOperands != 1) { |
| 1211 | // FIXME: Should reject these. The ARM backend hits this with $lane |
Jim Grosbach | 65da6fc | 2012-04-17 00:01:04 +0000 | [diff] [blame] | 1212 | // in a bunch of instructions. The right answer is unclear. |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1213 | DEBUG({ |
| 1214 | errs() << "warning: '" << CGI.TheDef->getName() << "': " |
Jim Grosbach | 65da6fc | 2012-04-17 00:01:04 +0000 | [diff] [blame] | 1215 | << "ignoring instruction with multi-operand tied operand '" |
| 1216 | << OI.Name << "'\n"; |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1217 | }); |
| 1218 | continue; |
| 1219 | } |
| 1220 | } |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1221 | } |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1222 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1223 | OwningPtr<MatchableInfo> II(new MatchableInfo(CGI)); |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1224 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1225 | II->Initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix); |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1226 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1227 | // Ignore instructions which shouldn't be matched and diagnose invalid |
| 1228 | // instruction definitions with an error. |
| 1229 | if (!II->Validate(CommentDelimiter, true)) |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1230 | continue; |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1231 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1232 | // Ignore "Int_*" and "*_Int" instructions, which are internal aliases. |
| 1233 | // |
| 1234 | // FIXME: This is a total hack. |
| 1235 | if (StringRef(II->TheDef->getName()).startswith("Int_") || |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1236 | StringRef(II->TheDef->getName()).endswith("_Int")) |
| 1237 | continue; |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1238 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1239 | Matchables.push_back(II.take()); |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1240 | } |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1241 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1242 | // Parse all of the InstAlias definitions and stick them in the list of |
| 1243 | // matchables. |
| 1244 | std::vector<Record*> AllInstAliases = |
| 1245 | Records.getAllDerivedDefinitions("InstAlias"); |
| 1246 | for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) { |
| 1247 | CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i], Target); |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1248 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1249 | // If the tblgen -match-prefix option is specified (for tblgen hackers), |
| 1250 | // filter the set of instruction aliases we consider, based on the target |
| 1251 | // instruction. |
Jim Grosbach | 65da6fc | 2012-04-17 00:01:04 +0000 | [diff] [blame] | 1252 | if (!StringRef(Alias->ResultInst->TheDef->getName()) |
| 1253 | .startswith( MatchPrefix)) |
Jim Grosbach | 11fc646 | 2012-04-11 21:02:33 +0000 | [diff] [blame] | 1254 | continue; |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1255 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1256 | OwningPtr<MatchableInfo> II(new MatchableInfo(Alias)); |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1257 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1258 | II->Initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix); |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1259 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1260 | // Validate the alias definitions. |
| 1261 | II->Validate(CommentDelimiter, false); |
Jim Grosbach | f35307c | 2012-01-24 21:06:59 +0000 | [diff] [blame] | 1262 | |
Devang Patel | 0dbcada | 2012-01-09 19:13:28 +0000 | [diff] [blame] | 1263 | Matchables.push_back(II.take()); |
| 1264 | } |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 1265 | } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 1266 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1267 | // Build info for the register classes. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1268 | BuildRegisterClasses(SingletonRegisters); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1269 | |
| 1270 | // Build info for the user defined assembly operand classes. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1271 | BuildOperandClasses(); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1272 | |
Chris Lattner | 0bb780c | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1273 | // Build the information about matchables, now that we have fully formed |
| 1274 | // classes. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1275 | for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(), |
| 1276 | ie = Matchables.end(); it != ie; ++it) { |
| 1277 | MatchableInfo *II = *it; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 1279 | // Parse the tokens after the mnemonic. |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1280 | // Note: BuildInstructionOperandReference may insert new AsmOperands, so |
| 1281 | // don't precompute the loop bound. |
| 1282 | for (unsigned i = 0; i != II->AsmOperands.size(); ++i) { |
Chris Lattner | c0b14a2 | 2010-11-03 19:47:34 +0000 | [diff] [blame] | 1283 | MatchableInfo::AsmOperand &Op = II->AsmOperands[i]; |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1284 | StringRef Token = Op.Token; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1285 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1286 | // Check for singleton registers. |
Devang Patel | 63faf82 | 2012-01-07 01:33:34 +0000 | [diff] [blame] | 1287 | if (Record *RegRecord = II->AsmOperands[i].SingletonReg) { |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1288 | Op.Class = RegisterClasses[RegRecord]; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1289 | assert(Op.Class && Op.Class->Registers.size() == 1 && |
| 1290 | "Unexpected class for singleton register"); |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1291 | continue; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1294 | // Check for simple tokens. |
| 1295 | if (Token[0] != '$') { |
Chris Lattner | d19ec05 | 2010-11-02 17:30:52 +0000 | [diff] [blame] | 1296 | Op.Class = getTokenClass(Token); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1297 | continue; |
| 1298 | } |
| 1299 | |
Chris Lattner | 7ad3147 | 2010-11-06 22:06:03 +0000 | [diff] [blame] | 1300 | if (Token.size() > 1 && isdigit(Token[1])) { |
| 1301 | Op.Class = getTokenClass(Token); |
| 1302 | continue; |
| 1303 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1305 | // Otherwise this is an operand reference. |
Chris Lattner | 5f4280c | 2010-11-04 01:58:23 +0000 | [diff] [blame] | 1306 | StringRef OperandName; |
| 1307 | if (Token[1] == '{') |
| 1308 | OperandName = Token.substr(2, Token.size() - 3); |
| 1309 | else |
| 1310 | OperandName = Token.substr(1); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1312 | if (II->DefRec.is<const CodeGenInstruction*>()) |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1313 | BuildInstructionOperandReference(II, OperandName, i); |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1314 | else |
Chris Lattner | 225549f | 2010-11-06 06:39:47 +0000 | [diff] [blame] | 1315 | BuildAliasOperandReference(II, OperandName, Op); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1316 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1317 | |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1318 | if (II->DefRec.is<const CodeGenInstruction*>()) |
| 1319 | II->BuildInstructionResultOperands(); |
| 1320 | else |
| 1321 | II->BuildAliasResultOperands(); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1322 | } |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1323 | |
Jim Grosbach | a66512e | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 1324 | // Process token alias definitions and set up the associated superclass |
| 1325 | // information. |
| 1326 | std::vector<Record*> AllTokenAliases = |
| 1327 | Records.getAllDerivedDefinitions("TokenAlias"); |
| 1328 | for (unsigned i = 0, e = AllTokenAliases.size(); i != e; ++i) { |
| 1329 | Record *Rec = AllTokenAliases[i]; |
| 1330 | ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken")); |
| 1331 | ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken")); |
Jim Grosbach | 67cd20d | 2012-04-17 21:23:52 +0000 | [diff] [blame] | 1332 | if (FromClass == ToClass) |
| 1333 | throw TGError(Rec->getLoc(), |
| 1334 | "error: Destination value identical to source value."); |
Jim Grosbach | a66512e | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 1335 | FromClass->SuperClasses.push_back(ToClass); |
| 1336 | } |
| 1337 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1338 | // Reorder classes so that classes precede super classes. |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1339 | std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>()); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Chris Lattner | 0bb780c | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1342 | /// BuildInstructionOperandReference - The specified operand is a reference to a |
| 1343 | /// named operand such as $src. Resolve the Class and OperandInfo pointers. |
| 1344 | void AsmMatcherInfo:: |
| 1345 | BuildInstructionOperandReference(MatchableInfo *II, |
Chris Lattner | 5f4280c | 2010-11-04 01:58:23 +0000 | [diff] [blame] | 1346 | StringRef OperandName, |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1347 | unsigned AsmOpIdx) { |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1348 | const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>(); |
| 1349 | const CGIOperandList &Operands = CGI.Operands; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1350 | MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx]; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | 662e5a3 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 1352 | // Map this token to an operand. |
Chris Lattner | 0bb780c | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1353 | unsigned Idx; |
| 1354 | if (!Operands.hasOperandNamed(OperandName, Idx)) |
| 1355 | throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" + |
| 1356 | OperandName.str() + "'"); |
Chris Lattner | ba3b5b6 | 2010-11-04 01:55:23 +0000 | [diff] [blame] | 1357 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1358 | // If the instruction operand has multiple suboperands, but the parser |
| 1359 | // match class for the asm operand is still the default "ImmAsmOperand", |
| 1360 | // then handle each suboperand separately. |
| 1361 | if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) { |
| 1362 | Record *Rec = Operands[Idx].Rec; |
| 1363 | assert(Rec->isSubClassOf("Operand") && "Unexpected operand!"); |
| 1364 | Record *MatchClass = Rec->getValueAsDef("ParserMatchClass"); |
| 1365 | if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") { |
| 1366 | // Insert remaining suboperands after AsmOpIdx in II->AsmOperands. |
| 1367 | StringRef Token = Op->Token; // save this in case Op gets moved |
| 1368 | for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) { |
| 1369 | MatchableInfo::AsmOperand NewAsmOp(Token); |
| 1370 | NewAsmOp.SubOpIdx = SI; |
| 1371 | II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp); |
| 1372 | } |
| 1373 | // Replace Op with first suboperand. |
| 1374 | Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved |
| 1375 | Op->SubOpIdx = 0; |
| 1376 | } |
| 1377 | } |
| 1378 | |
Chris Lattner | ba3b5b6 | 2010-11-04 01:55:23 +0000 | [diff] [blame] | 1379 | // Set up the operand class. |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1380 | Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx); |
Chris Lattner | ba3b5b6 | 2010-11-04 01:55:23 +0000 | [diff] [blame] | 1381 | |
| 1382 | // If the named operand is tied, canonicalize it to the untied operand. |
| 1383 | // For example, something like: |
| 1384 | // (outs GPR:$dst), (ins GPR:$src) |
| 1385 | // with an asmstring of |
| 1386 | // "inc $src" |
| 1387 | // we want to canonicalize to: |
| 1388 | // "inc $dst" |
| 1389 | // so that we know how to provide the $dst operand when filling in the result. |
| 1390 | int OITied = Operands[Idx].getTiedRegister(); |
Chris Lattner | 0bb780c | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1391 | if (OITied != -1) { |
| 1392 | // The tied operand index is an MIOperand index, find the operand that |
| 1393 | // contains it. |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1394 | std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied); |
| 1395 | OperandName = Operands[Idx.first].Name; |
| 1396 | Op->SubOpIdx = Idx.second; |
Chris Lattner | 0bb780c | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1397 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1398 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1399 | Op->SrcOpName = OperandName; |
Chris Lattner | 0bb780c | 2010-11-04 00:57:06 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 1402 | /// BuildAliasOperandReference - When parsing an operand reference out of the |
| 1403 | /// matching string (e.g. "movsx $src, $dst"), determine what the class of the |
| 1404 | /// operand reference is by looking it up in the result pattern definition. |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1405 | void AsmMatcherInfo::BuildAliasOperandReference(MatchableInfo *II, |
| 1406 | StringRef OperandName, |
| 1407 | MatchableInfo::AsmOperand &Op) { |
| 1408 | const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>(); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1409 | |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1410 | // Set up the operand class. |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 1411 | for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i) |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 1412 | if (CGA.ResultOperands[i].isRecord() && |
| 1413 | CGA.ResultOperands[i].getName() == OperandName) { |
Chris Lattner | 662e5a3 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 1414 | // It's safe to go with the first one we find, because CodeGenInstAlias |
| 1415 | // validates that all operands with the same name have the same record. |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1416 | Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second; |
Jim Grosbach | 48c1f84 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 1417 | // Use the match class from the Alias definition, not the |
| 1418 | // destination instruction, as we may have an immediate that's |
| 1419 | // being munged by the match class. |
| 1420 | Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(), |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1421 | Op.SubOpIdx); |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 1422 | Op.SrcOpName = OperandName; |
| 1423 | return; |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1424 | } |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 1425 | |
| 1426 | throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" + |
| 1427 | OperandName.str() + "'"); |
Chris Lattner | c07bd40 | 2010-11-04 02:11:18 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1430 | void MatchableInfo::BuildInstructionResultOperands() { |
Chris Lattner | 662e5a3 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 1431 | const CodeGenInstruction *ResultInst = getResultInst(); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | 662e5a3 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 1433 | // Loop over all operands of the result instruction, determining how to |
| 1434 | // populate them. |
| 1435 | for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) { |
| 1436 | const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i]; |
Chris Lattner | 567820c | 2010-11-04 01:42:59 +0000 | [diff] [blame] | 1437 | |
| 1438 | // If this is a tied operand, just copy from the previously handled operand. |
| 1439 | int TiedOp = OpInfo.getTiedRegister(); |
| 1440 | if (TiedOp != -1) { |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1441 | ResOperands.push_back(ResOperand::getTiedOp(TiedOp)); |
Chris Lattner | 567820c | 2010-11-04 01:42:59 +0000 | [diff] [blame] | 1442 | continue; |
| 1443 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1444 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1445 | // Find out what operand from the asmparser this MCInst operand comes from. |
Chris Lattner | ba3b5b6 | 2010-11-04 01:55:23 +0000 | [diff] [blame] | 1446 | int SrcOperand = FindAsmOperandNamed(OpInfo.Name); |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1447 | if (OpInfo.Name.empty() || SrcOperand == -1) |
| 1448 | throw TGError(TheDef->getLoc(), "Instruction '" + |
| 1449 | TheDef->getName() + "' has operand '" + OpInfo.Name + |
| 1450 | "' that doesn't appear in asm string!"); |
Chris Lattner | 567820c | 2010-11-04 01:42:59 +0000 | [diff] [blame] | 1451 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1452 | // Check if the one AsmOperand populates the entire operand. |
| 1453 | unsigned NumOperands = OpInfo.MINumOperands; |
| 1454 | if (AsmOperands[SrcOperand].SubOpIdx == -1) { |
| 1455 | ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands)); |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1456 | continue; |
| 1457 | } |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1458 | |
| 1459 | // Add a separate ResOperand for each suboperand. |
| 1460 | for (unsigned AI = 0; AI < NumOperands; ++AI) { |
| 1461 | assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI && |
| 1462 | AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name && |
| 1463 | "unexpected AsmOperands for suboperands"); |
| 1464 | ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1)); |
| 1465 | } |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1469 | void MatchableInfo::BuildAliasResultOperands() { |
| 1470 | const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>(); |
| 1471 | const CodeGenInstruction *ResultInst = getResultInst(); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1472 | |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1473 | // Loop over all operands of the result instruction, determining how to |
| 1474 | // populate them. |
| 1475 | unsigned AliasOpNo = 0; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1476 | unsigned LastOpNo = CGA.ResultInstOperandIndex.size(); |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1477 | for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) { |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1478 | const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i]; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1479 | |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1480 | // If this is a tied operand, just copy from the previously handled operand. |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1481 | int TiedOp = OpInfo->getTiedRegister(); |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1482 | if (TiedOp != -1) { |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1483 | ResOperands.push_back(ResOperand::getTiedOp(TiedOp)); |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 1484 | continue; |
| 1485 | } |
| 1486 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1487 | // Handle all the suboperands for this operand. |
| 1488 | const std::string &OpName = OpInfo->Name; |
| 1489 | for ( ; AliasOpNo < LastOpNo && |
| 1490 | CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) { |
| 1491 | int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second; |
| 1492 | |
| 1493 | // Find out what operand from the asmparser that this MCInst operand |
| 1494 | // comes from. |
| 1495 | switch (CGA.ResultOperands[AliasOpNo].Kind) { |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1496 | case CodeGenInstAlias::ResultOperand::K_Record: { |
| 1497 | StringRef Name = CGA.ResultOperands[AliasOpNo].getName(); |
| 1498 | int SrcOperand = FindAsmOperand(Name, SubIdx); |
| 1499 | if (SrcOperand == -1) |
| 1500 | throw TGError(TheDef->getLoc(), "Instruction '" + |
| 1501 | TheDef->getName() + "' has operand '" + OpName + |
| 1502 | "' that doesn't appear in asm string!"); |
| 1503 | unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1); |
| 1504 | ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, |
| 1505 | NumOperands)); |
| 1506 | break; |
| 1507 | } |
| 1508 | case CodeGenInstAlias::ResultOperand::K_Imm: { |
| 1509 | int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm(); |
| 1510 | ResOperands.push_back(ResOperand::getImmOp(ImmVal)); |
| 1511 | break; |
| 1512 | } |
| 1513 | case CodeGenInstAlias::ResultOperand::K_Reg: { |
| 1514 | Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister(); |
| 1515 | ResOperands.push_back(ResOperand::getRegOp(Reg)); |
| 1516 | break; |
| 1517 | } |
| 1518 | } |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 1519 | } |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 1520 | } |
| 1521 | } |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1522 | |
Daniel Dunbar | 5c228a9 | 2011-02-04 23:17:40 +0000 | [diff] [blame] | 1523 | static void EmitConvertToMCInst(CodeGenTarget &Target, StringRef ClassName, |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1524 | std::vector<MatchableInfo*> &Infos, |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1525 | raw_ostream &OS) { |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1526 | // Write the convert function to a separate stream, so we can drop it after |
| 1527 | // the enum. |
| 1528 | std::string ConvertFnBody; |
| 1529 | raw_string_ostream CvtOS(ConvertFnBody); |
| 1530 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1531 | // Function we have already generated. |
| 1532 | std::set<std::string> GeneratedFns; |
| 1533 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1534 | // Start the unified conversion function. |
Daniel Dunbar | 5c228a9 | 2011-02-04 23:17:40 +0000 | [diff] [blame] | 1535 | CvtOS << "bool " << Target.getName() << ClassName << "::\n"; |
| 1536 | CvtOS << "ConvertToMCInst(unsigned Kind, MCInst &Inst, " |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1537 | << "unsigned Opcode,\n" |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1538 | << " const SmallVectorImpl<MCParsedAsmOperand*" |
| 1539 | << "> &Operands) {\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1540 | CvtOS << " Inst.setOpcode(Opcode);\n"; |
| 1541 | CvtOS << " switch (Kind) {\n"; |
| 1542 | CvtOS << " default:\n"; |
| 1543 | |
| 1544 | // Start the enum, which we will generate inline. |
| 1545 | |
Chris Lattner | d51257a | 2010-11-02 23:18:43 +0000 | [diff] [blame] | 1546 | OS << "// Unified function for converting operands to MCInst instances.\n\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1547 | OS << "enum ConversionKind {\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1549 | // TargetOperandClass - This is the target's operand class, like X86Operand. |
| 1550 | std::string TargetOperandClass = Target.getName() + "Operand"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1551 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1552 | for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(), |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1553 | ie = Infos.end(); it != ie; ++it) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 1554 | MatchableInfo &II = **it; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1555 | |
Daniel Dunbar | cf12067 | 2011-02-04 17:12:15 +0000 | [diff] [blame] | 1556 | // Check if we have a custom match function. |
Daniel Dunbar | 27b83d4 | 2011-04-01 20:23:52 +0000 | [diff] [blame] | 1557 | std::string AsmMatchConverter = |
| 1558 | II.getResultInst()->TheDef->getValueAsString("AsmMatchConverter"); |
Daniel Dunbar | cf12067 | 2011-02-04 17:12:15 +0000 | [diff] [blame] | 1559 | if (!AsmMatchConverter.empty()) { |
Daniel Dunbar | 27b83d4 | 2011-04-01 20:23:52 +0000 | [diff] [blame] | 1560 | std::string Signature = "ConvertCustom_" + AsmMatchConverter; |
Daniel Dunbar | cf12067 | 2011-02-04 17:12:15 +0000 | [diff] [blame] | 1561 | II.ConversionFnKind = Signature; |
| 1562 | |
| 1563 | // Check if we have already generated this signature. |
| 1564 | if (!GeneratedFns.insert(Signature).second) |
| 1565 | continue; |
| 1566 | |
| 1567 | // If not, emit it now. Add to the enum list. |
| 1568 | OS << " " << Signature << ",\n"; |
| 1569 | |
| 1570 | CvtOS << " case " << Signature << ":\n"; |
Daniel Dunbar | b412915 | 2011-02-04 17:12:23 +0000 | [diff] [blame] | 1571 | CvtOS << " return " << AsmMatchConverter |
| 1572 | << "(Inst, Opcode, Operands);\n"; |
Daniel Dunbar | cf12067 | 2011-02-04 17:12:15 +0000 | [diff] [blame] | 1573 | continue; |
| 1574 | } |
| 1575 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1576 | // Build the conversion function signature. |
| 1577 | std::string Signature = "Convert"; |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1578 | std::string CaseBody; |
| 1579 | raw_string_ostream CaseOS(CaseBody); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1580 | |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1581 | // Compute the convert enum and the case body. |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1582 | for (unsigned i = 0, e = II.ResOperands.size(); i != e; ++i) { |
| 1583 | const MatchableInfo::ResOperand &OpInfo = II.ResOperands[i]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1585 | // Generate code to populate each result operand. |
| 1586 | switch (OpInfo.Kind) { |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1587 | case MatchableInfo::ResOperand::RenderAsmOperand: { |
| 1588 | // This comes from something we parsed. |
| 1589 | MatchableInfo::AsmOperand &Op = II.AsmOperands[OpInfo.AsmOperandNum]; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1590 | |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1591 | // Registers are always converted the same, don't duplicate the |
| 1592 | // conversion function based on them. |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1593 | Signature += "__"; |
| 1594 | if (Op.Class->isRegisterClass()) |
| 1595 | Signature += "Reg"; |
| 1596 | else |
| 1597 | Signature += Op.Class->ClassName; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1598 | Signature += utostr(OpInfo.MINumOperands); |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1599 | Signature += "_" + itostr(OpInfo.AsmOperandNum); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1600 | |
Chris Lattner | 9b0d4bf | 2010-11-02 22:55:03 +0000 | [diff] [blame] | 1601 | CaseOS << " ((" << TargetOperandClass << "*)Operands[" |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1602 | << (OpInfo.AsmOperandNum+1) << "])->" << Op.Class->RenderMethod |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1603 | << "(Inst, " << OpInfo.MINumOperands << ");\n"; |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1604 | break; |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1605 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1606 | |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1607 | case MatchableInfo::ResOperand::TiedOperand: { |
| 1608 | // If this operand is tied to a previous one, just copy the MCInst |
| 1609 | // operand from the earlier one.We can only tie single MCOperand values. |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 1610 | //assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand"); |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1611 | unsigned TiedOp = OpInfo.TiedOperandNum; |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1612 | assert(i > TiedOp && "Tied operand precedes its target!"); |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1613 | CaseOS << " Inst.addOperand(Inst.getOperand(" << TiedOp << "));\n"; |
| 1614 | Signature += "__Tie" + utostr(TiedOp); |
| 1615 | break; |
| 1616 | } |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 1617 | case MatchableInfo::ResOperand::ImmOperand: { |
| 1618 | int64_t Val = OpInfo.ImmVal; |
| 1619 | CaseOS << " Inst.addOperand(MCOperand::CreateImm(" << Val << "));\n"; |
| 1620 | Signature += "__imm" + itostr(Val); |
| 1621 | break; |
| 1622 | } |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 1623 | case MatchableInfo::ResOperand::RegOperand: { |
Bob Wilson | dc1a2bd | 2011-01-14 22:58:09 +0000 | [diff] [blame] | 1624 | if (OpInfo.Register == 0) { |
| 1625 | CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n"; |
| 1626 | Signature += "__reg0"; |
| 1627 | } else { |
| 1628 | std::string N = getQualifiedName(OpInfo.Register); |
| 1629 | CaseOS << " Inst.addOperand(MCOperand::CreateReg(" << N << "));\n"; |
| 1630 | Signature += "__reg" + OpInfo.Register->getName(); |
| 1631 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1632 | } |
Chris Lattner | 1d13bda | 2010-11-04 00:43:46 +0000 | [diff] [blame] | 1633 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1634 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1635 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1636 | II.ConversionFnKind = Signature; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1637 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1638 | // Check if we have already generated this signature. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1639 | if (!GeneratedFns.insert(Signature).second) |
| 1640 | continue; |
| 1641 | |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1642 | // If not, emit it now. Add to the enum list. |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1643 | OS << " " << Signature << ",\n"; |
| 1644 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1645 | CvtOS << " case " << Signature << ":\n"; |
Chris Lattner | dda855d | 2010-11-02 21:49:44 +0000 | [diff] [blame] | 1646 | CvtOS << CaseOS.str(); |
Daniel Dunbar | b412915 | 2011-02-04 17:12:23 +0000 | [diff] [blame] | 1647 | CvtOS << " return true;\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1648 | } |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1649 | |
| 1650 | // Finish the convert function. |
| 1651 | |
| 1652 | CvtOS << " }\n"; |
Daniel Dunbar | b412915 | 2011-02-04 17:12:23 +0000 | [diff] [blame] | 1653 | CvtOS << " return false;\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1654 | CvtOS << "}\n\n"; |
| 1655 | |
| 1656 | // Finish the enum, and drop the convert function after it. |
| 1657 | |
| 1658 | OS << " NumConversionVariants\n"; |
| 1659 | OS << "};\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1660 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1661 | OS << CvtOS.str(); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1664 | /// EmitMatchClassEnumeration - Emit the enumeration for match class kinds. |
| 1665 | static void EmitMatchClassEnumeration(CodeGenTarget &Target, |
| 1666 | std::vector<ClassInfo*> &Infos, |
| 1667 | raw_ostream &OS) { |
| 1668 | OS << "namespace {\n\n"; |
| 1669 | |
| 1670 | OS << "/// MatchClassKind - The kinds of classes which participate in\n" |
| 1671 | << "/// instruction matching.\n"; |
| 1672 | OS << "enum MatchClassKind {\n"; |
| 1673 | OS << " InvalidMatchClass = 0,\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1674 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1675 | ie = Infos.end(); it != ie; ++it) { |
| 1676 | ClassInfo &CI = **it; |
| 1677 | OS << " " << CI.Name << ", // "; |
| 1678 | if (CI.Kind == ClassInfo::Token) { |
| 1679 | OS << "'" << CI.ValueName << "'\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1680 | } else if (CI.isRegisterClass()) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1681 | if (!CI.ValueName.empty()) |
| 1682 | OS << "register class '" << CI.ValueName << "'\n"; |
| 1683 | else |
| 1684 | OS << "derived register class\n"; |
| 1685 | } else { |
| 1686 | OS << "user defined class '" << CI.ValueName << "'\n"; |
| 1687 | } |
| 1688 | } |
| 1689 | OS << " NumMatchClassKinds\n"; |
| 1690 | OS << "};\n\n"; |
| 1691 | |
| 1692 | OS << "}\n\n"; |
| 1693 | } |
| 1694 | |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1695 | /// EmitValidateOperandClass - Emit the function to validate an operand class. |
| 1696 | static void EmitValidateOperandClass(AsmMatcherInfo &Info, |
| 1697 | raw_ostream &OS) { |
Jim Grosbach | 3d5d8f6 | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 1698 | OS << "static bool validateOperandClass(MCParsedAsmOperand *GOp, " |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1699 | << "MatchClassKind Kind) {\n"; |
| 1700 | OS << " " << Info.Target.getName() << "Operand &Operand = *(" |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1701 | << Info.Target.getName() << "Operand*)GOp;\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1702 | |
Kevin Enderby | 8938183 | 2011-07-15 18:30:43 +0000 | [diff] [blame] | 1703 | // The InvalidMatchClass is not to match any operand. |
| 1704 | OS << " if (Kind == InvalidMatchClass)\n"; |
| 1705 | OS << " return false;\n\n"; |
| 1706 | |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1707 | // Check for Token operands first. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1708 | OS << " if (Operand.isToken())\n"; |
Jim Grosbach | a66512e | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 1709 | OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind);" |
| 1710 | << "\n\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1711 | |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1712 | // Check for register operands, including sub-classes. |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1713 | OS << " if (Operand.isReg()) {\n"; |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1714 | OS << " MatchClassKind OpKind;\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1715 | OS << " switch (Operand.getReg()) {\n"; |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1716 | OS << " default: OpKind = InvalidMatchClass; break;\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1717 | for (std::map<Record*, ClassInfo*>::iterator |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1718 | it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end(); |
| 1719 | it != ie; ++it) |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1720 | OS << " case " << Info.Target.getName() << "::" |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1721 | << it->first->getName() << ": OpKind = " << it->second->Name |
| 1722 | << "; break;\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1723 | OS << " }\n"; |
Jim Grosbach | 3d5d8f6 | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 1724 | OS << " return isSubclass(OpKind, Kind);\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1725 | OS << " }\n\n"; |
| 1726 | |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1727 | // Check the user classes. We don't care what order since we're only |
| 1728 | // actually matching against one of them. |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1729 | for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(), |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1730 | ie = Info.Classes.end(); it != ie; ++it) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1731 | ClassInfo &CI = **it; |
| 1732 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1733 | if (!CI.isUserClass()) |
| 1734 | continue; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1735 | |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1736 | OS << " // '" << CI.ClassName << "' class\n"; |
| 1737 | OS << " if (Kind == " << CI.Name |
| 1738 | << " && Operand." << CI.PredicateMethod << "()) {\n"; |
| 1739 | OS << " return true;\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1740 | OS << " }\n\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1741 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1742 | |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 1743 | OS << " return false;\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1744 | OS << "}\n\n"; |
| 1745 | } |
| 1746 | |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1747 | /// EmitIsSubclass - Emit the subclass predicate function. |
| 1748 | static void EmitIsSubclass(CodeGenTarget &Target, |
| 1749 | std::vector<ClassInfo*> &Infos, |
| 1750 | raw_ostream &OS) { |
Jim Grosbach | 3d5d8f6 | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 1751 | OS << "/// isSubclass - Compute whether \\arg A is a subclass of \\arg B.\n"; |
| 1752 | OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1753 | OS << " if (A == B)\n"; |
| 1754 | OS << " return true;\n\n"; |
| 1755 | |
| 1756 | OS << " switch (A) {\n"; |
| 1757 | OS << " default:\n"; |
| 1758 | OS << " return false;\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1759 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1760 | ie = Infos.end(); it != ie; ++it) { |
| 1761 | ClassInfo &A = **it; |
| 1762 | |
Jim Grosbach | a66512e | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 1763 | std::vector<StringRef> SuperClasses; |
| 1764 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
| 1765 | ie = Infos.end(); it != ie; ++it) { |
| 1766 | ClassInfo &B = **it; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1767 | |
Jim Grosbach | a66512e | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 1768 | if (&A != &B && A.isSubsetOf(B)) |
| 1769 | SuperClasses.push_back(B.Name); |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1770 | } |
Jim Grosbach | a66512e | 2011-12-06 23:43:54 +0000 | [diff] [blame] | 1771 | |
| 1772 | if (SuperClasses.empty()) |
| 1773 | continue; |
| 1774 | |
| 1775 | OS << "\n case " << A.Name << ":\n"; |
| 1776 | |
| 1777 | if (SuperClasses.size() == 1) { |
| 1778 | OS << " return B == " << SuperClasses.back() << ";\n"; |
| 1779 | continue; |
| 1780 | } |
| 1781 | |
| 1782 | OS << " switch (B) {\n"; |
| 1783 | OS << " default: return false;\n"; |
| 1784 | for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) |
| 1785 | OS << " case " << SuperClasses[i] << ": return true;\n"; |
| 1786 | OS << " }\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1787 | } |
| 1788 | OS << " }\n"; |
| 1789 | OS << "}\n\n"; |
| 1790 | } |
| 1791 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1792 | /// EmitMatchTokenString - Emit the function to match a token string to the |
| 1793 | /// appropriate match class value. |
| 1794 | static void EmitMatchTokenString(CodeGenTarget &Target, |
| 1795 | std::vector<ClassInfo*> &Infos, |
| 1796 | raw_ostream &OS) { |
| 1797 | // Construct the match list. |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1798 | std::vector<StringMatcher::StringPair> Matches; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1799 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1800 | ie = Infos.end(); it != ie; ++it) { |
| 1801 | ClassInfo &CI = **it; |
| 1802 | |
| 1803 | if (CI.Kind == ClassInfo::Token) |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1804 | Matches.push_back(StringMatcher::StringPair(CI.ValueName, |
| 1805 | "return " + CI.Name + ";")); |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Jim Grosbach | 3d5d8f6 | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 1808 | OS << "static MatchClassKind matchTokenString(StringRef Name) {\n"; |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1810 | StringMatcher("Name", Matches, OS).Emit(); |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1811 | |
| 1812 | OS << " return InvalidMatchClass;\n"; |
| 1813 | OS << "}\n\n"; |
| 1814 | } |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1815 | |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1816 | /// EmitMatchRegisterName - Emit the function to match a string to the target |
| 1817 | /// specific register enum. |
| 1818 | static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser, |
| 1819 | raw_ostream &OS) { |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1820 | // Construct the match list. |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1821 | std::vector<StringMatcher::StringPair> Matches; |
Jakob Stoklund Olesen | abdbc84 | 2011-06-18 04:26:06 +0000 | [diff] [blame] | 1822 | const std::vector<CodeGenRegister*> &Regs = |
| 1823 | Target.getRegBank().getRegisters(); |
| 1824 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 1825 | const CodeGenRegister *Reg = Regs[i]; |
| 1826 | if (Reg->TheDef->getValueAsString("AsmName").empty()) |
Daniel Dunbar | 22be522 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 1827 | continue; |
| 1828 | |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1829 | Matches.push_back(StringMatcher::StringPair( |
Jakob Stoklund Olesen | abdbc84 | 2011-06-18 04:26:06 +0000 | [diff] [blame] | 1830 | Reg->TheDef->getValueAsString("AsmName"), |
| 1831 | "return " + utostr(Reg->EnumValue) + ";")); |
Daniel Dunbar | 22be522 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 1832 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1833 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 1834 | OS << "static unsigned MatchRegisterName(StringRef Name) {\n"; |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1835 | |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1836 | StringMatcher("Name", Matches, OS).Emit(); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1837 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1838 | OS << " return 0;\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1839 | OS << "}\n\n"; |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1840 | } |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1841 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1842 | /// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag |
| 1843 | /// definitions. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1844 | static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info, |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1845 | raw_ostream &OS) { |
| 1846 | OS << "// Flags for subtarget features that participate in " |
| 1847 | << "instruction matching.\n"; |
| 1848 | OS << "enum SubtargetFeatureFlag {\n"; |
| 1849 | for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator |
| 1850 | it = Info.SubtargetFeatures.begin(), |
| 1851 | ie = Info.SubtargetFeatures.end(); it != ie; ++it) { |
| 1852 | SubtargetFeatureInfo &SFI = *it->second; |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1853 | OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1854 | } |
| 1855 | OS << " Feature_None = 0\n"; |
| 1856 | OS << "};\n\n"; |
| 1857 | } |
| 1858 | |
| 1859 | /// EmitComputeAvailableFeatures - Emit the function to compute the list of |
| 1860 | /// available features given a subtarget. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1861 | static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info, |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1862 | raw_ostream &OS) { |
| 1863 | std::string ClassName = |
| 1864 | Info.AsmParser->getValueAsString("AsmParserClassName"); |
| 1865 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1866 | OS << "unsigned " << Info.Target.getName() << ClassName << "::\n" |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 1867 | << "ComputeAvailableFeatures(uint64_t FB) const {\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1868 | OS << " unsigned Features = 0;\n"; |
| 1869 | for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator |
| 1870 | it = Info.SubtargetFeatures.begin(), |
| 1871 | ie = Info.SubtargetFeatures.end(); it != ie; ++it) { |
| 1872 | SubtargetFeatureInfo &SFI = *it->second; |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 1873 | |
| 1874 | OS << " if ("; |
Jim Grosbach | 65da6fc | 2012-04-17 00:01:04 +0000 | [diff] [blame] | 1875 | std::string CondStorage = |
| 1876 | SFI.TheDef->getValueAsString("AssemblerCondString"); |
Evan Cheng | fbc38d2 | 2011-07-08 18:04:22 +0000 | [diff] [blame] | 1877 | StringRef Conds = CondStorage; |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 1878 | std::pair<StringRef,StringRef> Comma = Conds.split(','); |
| 1879 | bool First = true; |
| 1880 | do { |
| 1881 | if (!First) |
| 1882 | OS << " && "; |
| 1883 | |
| 1884 | bool Neg = false; |
| 1885 | StringRef Cond = Comma.first; |
| 1886 | if (Cond[0] == '!') { |
| 1887 | Neg = true; |
| 1888 | Cond = Cond.substr(1); |
| 1889 | } |
| 1890 | |
| 1891 | OS << "((FB & " << Info.Target.getName() << "::" << Cond << ")"; |
| 1892 | if (Neg) |
| 1893 | OS << " == 0"; |
| 1894 | else |
| 1895 | OS << " != 0"; |
| 1896 | OS << ")"; |
| 1897 | |
| 1898 | if (Comma.second.empty()) |
| 1899 | break; |
| 1900 | |
| 1901 | First = false; |
| 1902 | Comma = Comma.second.split(','); |
| 1903 | } while (true); |
| 1904 | |
| 1905 | OS << ")\n"; |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1906 | OS << " Features |= " << SFI.getEnumName() << ";\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1907 | } |
| 1908 | OS << " return Features;\n"; |
| 1909 | OS << "}\n\n"; |
| 1910 | } |
| 1911 | |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 1912 | static std::string GetAliasRequiredFeatures(Record *R, |
| 1913 | const AsmMatcherInfo &Info) { |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1914 | std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates"); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1915 | std::string Result; |
| 1916 | unsigned NumFeatures = 0; |
| 1917 | for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) { |
Chris Lattner | 4a74ee7 | 2010-11-01 02:09:21 +0000 | [diff] [blame] | 1918 | SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1919 | |
Chris Lattner | 4a74ee7 | 2010-11-01 02:09:21 +0000 | [diff] [blame] | 1920 | if (F == 0) |
| 1921 | throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() + |
| 1922 | "' is not marked as an AssemblerPredicate!"); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1923 | |
Chris Lattner | 4a74ee7 | 2010-11-01 02:09:21 +0000 | [diff] [blame] | 1924 | if (NumFeatures) |
| 1925 | Result += '|'; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1926 | |
Chris Lattner | 4a74ee7 | 2010-11-01 02:09:21 +0000 | [diff] [blame] | 1927 | Result += F->getEnumName(); |
| 1928 | ++NumFeatures; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1929 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1930 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1931 | if (NumFeatures > 1) |
| 1932 | Result = '(' + Result + ')'; |
| 1933 | return Result; |
| 1934 | } |
| 1935 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1936 | /// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions, |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1937 | /// emit a function for them and return true, otherwise return false. |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1938 | static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) { |
Daniel Dunbar | c0a7007 | 2011-01-24 23:26:31 +0000 | [diff] [blame] | 1939 | // Ignore aliases when match-prefix is set. |
| 1940 | if (!MatchPrefix.empty()) |
| 1941 | return false; |
| 1942 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1943 | std::vector<Record*> Aliases = |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1944 | Info.getRecords().getAllDerivedDefinitions("MnemonicAlias"); |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1945 | if (Aliases.empty()) return false; |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1946 | |
Jim Grosbach | 3d5d8f6 | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 1947 | OS << "static void applyMnemonicAliases(StringRef &Mnemonic, " |
Chris Lattner | 8cc0a6b | 2010-10-30 18:57:07 +0000 | [diff] [blame] | 1948 | "unsigned Features) {\n"; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1949 | |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1950 | // Keep track of all the aliases from a mnemonic. Use an std::map so that the |
| 1951 | // iteration order of the map is stable. |
| 1952 | std::map<std::string, std::vector<Record*> > AliasesFromMnemonic; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1953 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1954 | for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { |
| 1955 | Record *R = Aliases[i]; |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1956 | AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R); |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1957 | } |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1958 | |
| 1959 | // Process each alias a "from" mnemonic at a time, building the code executed |
| 1960 | // by the string remapper. |
| 1961 | std::vector<StringMatcher::StringPair> Cases; |
| 1962 | for (std::map<std::string, std::vector<Record*> >::iterator |
| 1963 | I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end(); |
| 1964 | I != E; ++I) { |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1965 | const std::vector<Record*> &ToVec = I->second; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1966 | |
| 1967 | // Loop through each alias and emit code that handles each case. If there |
| 1968 | // are two instructions without predicates, emit an error. If there is one, |
| 1969 | // emit it last. |
| 1970 | std::string MatchCode; |
| 1971 | int AliasWithNoPredicate = -1; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1972 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1973 | for (unsigned i = 0, e = ToVec.size(); i != e; ++i) { |
| 1974 | Record *R = ToVec[i]; |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 1975 | std::string FeatureMask = GetAliasRequiredFeatures(R, Info); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1976 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1977 | // If this unconditionally matches, remember it for later and diagnose |
| 1978 | // duplicates. |
| 1979 | if (FeatureMask.empty()) { |
| 1980 | if (AliasWithNoPredicate != -1) { |
| 1981 | // We can't have two aliases from the same mnemonic with no predicate. |
| 1982 | PrintError(ToVec[AliasWithNoPredicate]->getLoc(), |
| 1983 | "two MnemonicAliases with the same 'from' mnemonic!"); |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 1984 | throw TGError(R->getLoc(), "this is the other MnemonicAlias."); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1985 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1986 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1987 | AliasWithNoPredicate = i; |
| 1988 | continue; |
| 1989 | } |
Joerg Sonnenberger | 6ef6ced | 2011-02-17 23:22:19 +0000 | [diff] [blame] | 1990 | if (R->getValueAsString("ToMnemonic") == I->first) |
| 1991 | throw TGError(R->getLoc(), "MnemonicAlias to the same string"); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | 8cf8bcc | 2010-10-30 19:47:49 +0000 | [diff] [blame] | 1993 | if (!MatchCode.empty()) |
| 1994 | MatchCode += "else "; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1995 | MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n"; |
| 1996 | MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n"; |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1997 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 1998 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1999 | if (AliasWithNoPredicate != -1) { |
| 2000 | Record *R = ToVec[AliasWithNoPredicate]; |
Chris Lattner | 8cf8bcc | 2010-10-30 19:47:49 +0000 | [diff] [blame] | 2001 | if (!MatchCode.empty()) |
| 2002 | MatchCode += "else\n "; |
| 2003 | MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n"; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2004 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2005 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 2006 | MatchCode += "return;"; |
| 2007 | |
| 2008 | Cases.push_back(std::make_pair(I->first, MatchCode)); |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 2009 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2010 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 2011 | StringMatcher("Mnemonic", Cases, OS).Emit(); |
Daniel Dunbar | 55b5e85 | 2011-01-18 01:59:30 +0000 | [diff] [blame] | 2012 | OS << "}\n\n"; |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2013 | |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 2014 | return true; |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Jim Grosbach | 194f3fa | 2012-03-01 17:30:35 +0000 | [diff] [blame] | 2017 | static const char *getMinimalTypeForRange(uint64_t Range) { |
| 2018 | assert(Range < 0xFFFFFFFFULL && "Enum too large"); |
| 2019 | if (Range > 0xFFFF) |
| 2020 | return "uint32_t"; |
| 2021 | if (Range > 0xFF) |
| 2022 | return "uint16_t"; |
| 2023 | return "uint8_t"; |
| 2024 | } |
| 2025 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2026 | static void EmitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target, |
| 2027 | const AsmMatcherInfo &Info, StringRef ClassName) { |
| 2028 | // Emit the static custom operand parsing table; |
| 2029 | OS << "namespace {\n"; |
| 2030 | OS << " struct OperandMatchEntry {\n"; |
Jakob Stoklund Olesen | 7044cce | 2012-03-15 21:22:53 +0000 | [diff] [blame] | 2031 | OS << " static const char *const MnemonicTable;\n"; |
Craig Topper | fab3f7e | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 2032 | OS << " uint32_t OperandMask;\n"; |
| 2033 | OS << " uint32_t Mnemonic;\n"; |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2034 | OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size()) |
Craig Topper | fab3f7e | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 2035 | << " RequiredFeatures;\n"; |
| 2036 | OS << " " << getMinimalTypeForRange(Info.Classes.size()) |
| 2037 | << " Class;\n\n"; |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2038 | OS << " StringRef getMnemonic() const {\n"; |
| 2039 | OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n"; |
| 2040 | OS << " MnemonicTable[Mnemonic]);\n"; |
| 2041 | OS << " }\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2042 | OS << " };\n\n"; |
| 2043 | |
| 2044 | OS << " // Predicate for searching for an opcode.\n"; |
| 2045 | OS << " struct LessOpcodeOperand {\n"; |
| 2046 | OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n"; |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2047 | OS << " return LHS.getMnemonic() < RHS;\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2048 | OS << " }\n"; |
| 2049 | OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n"; |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2050 | OS << " return LHS < RHS.getMnemonic();\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2051 | OS << " }\n"; |
| 2052 | OS << " bool operator()(const OperandMatchEntry &LHS,"; |
| 2053 | OS << " const OperandMatchEntry &RHS) {\n"; |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2054 | OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2055 | OS << " }\n"; |
| 2056 | OS << " };\n"; |
| 2057 | |
| 2058 | OS << "} // end anonymous namespace.\n\n"; |
| 2059 | |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2060 | StringToOffsetTable StringTable; |
| 2061 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2062 | OS << "static const OperandMatchEntry OperandMatchTable[" |
| 2063 | << Info.OperandMatchInfo.size() << "] = {\n"; |
| 2064 | |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2065 | OS << " /* Operand List Mask, Mnemonic, Operand Class, Features */\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2066 | for (std::vector<OperandMatchEntry>::const_iterator it = |
| 2067 | Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end(); |
| 2068 | it != ie; ++it) { |
| 2069 | const OperandMatchEntry &OMI = *it; |
| 2070 | const MatchableInfo &II = *OMI.MI; |
| 2071 | |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2072 | OS << " { " << OMI.OperandMask; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2073 | |
| 2074 | OS << " /* "; |
| 2075 | bool printComma = false; |
| 2076 | for (int i = 0, e = 31; i !=e; ++i) |
| 2077 | if (OMI.OperandMask & (1 << i)) { |
| 2078 | if (printComma) |
| 2079 | OS << ", "; |
| 2080 | OS << i; |
| 2081 | printComma = true; |
| 2082 | } |
| 2083 | OS << " */"; |
| 2084 | |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2085 | // Store a pascal-style length byte in the mnemonic. |
| 2086 | std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str(); |
Jakob Stoklund Olesen | bcfa982 | 2012-03-15 18:05:57 +0000 | [diff] [blame] | 2087 | OS << ", " << StringTable.GetOrAddStringOffset(LenMnemonic, false) |
Craig Topper | fab3f7e | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 2088 | << " /* " << II.Mnemonic << " */, "; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2089 | |
| 2090 | // Write the required features mask. |
| 2091 | if (!II.RequiredFeatures.empty()) { |
| 2092 | for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) { |
| 2093 | if (i) OS << "|"; |
| 2094 | OS << II.RequiredFeatures[i]->getEnumName(); |
| 2095 | } |
| 2096 | } else |
| 2097 | OS << "0"; |
Craig Topper | fab3f7e | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 2098 | |
| 2099 | OS << ", " << OMI.CI->Name; |
| 2100 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2101 | OS << " },\n"; |
| 2102 | } |
| 2103 | OS << "};\n\n"; |
| 2104 | |
Jakob Stoklund Olesen | 7044cce | 2012-03-15 21:22:53 +0000 | [diff] [blame] | 2105 | OS << "const char *const OperandMatchEntry::MnemonicTable =\n"; |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2106 | StringTable.EmitString(OS); |
| 2107 | OS << ";\n\n"; |
| 2108 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2109 | // Emit the operand class switch to call the correct custom parser for |
| 2110 | // the found operand class. |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2111 | OS << Target.getName() << ClassName << "::OperandMatchResultTy " |
| 2112 | << Target.getName() << ClassName << "::\n" |
Jim Grosbach | 3d5d8f6 | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 2113 | << "tryCustomParseOperand(SmallVectorImpl<MCParsedAsmOperand*>" |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2114 | << " &Operands,\n unsigned MCK) {\n\n" |
| 2115 | << " switch(MCK) {\n"; |
| 2116 | |
| 2117 | for (std::vector<ClassInfo*>::const_iterator it = Info.Classes.begin(), |
| 2118 | ie = Info.Classes.end(); it != ie; ++it) { |
| 2119 | ClassInfo *CI = *it; |
| 2120 | if (CI->ParserMethod.empty()) |
| 2121 | continue; |
| 2122 | OS << " case " << CI->Name << ":\n" |
| 2123 | << " return " << CI->ParserMethod << "(Operands);\n"; |
| 2124 | } |
| 2125 | |
| 2126 | OS << " default:\n"; |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2127 | OS << " return MatchOperand_NoMatch;\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2128 | OS << " }\n"; |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2129 | OS << " return MatchOperand_NoMatch;\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2130 | OS << "}\n\n"; |
| 2131 | |
| 2132 | // Emit the static custom operand parser. This code is very similar with |
| 2133 | // the other matcher. Also use MatchResultTy here just in case we go for |
| 2134 | // a better error handling. |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2135 | OS << Target.getName() << ClassName << "::OperandMatchResultTy " |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2136 | << Target.getName() << ClassName << "::\n" |
| 2137 | << "MatchOperandParserImpl(SmallVectorImpl<MCParsedAsmOperand*>" |
| 2138 | << " &Operands,\n StringRef Mnemonic) {\n"; |
| 2139 | |
| 2140 | // Emit code to get the available features. |
| 2141 | OS << " // Get the current feature set.\n"; |
| 2142 | OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n"; |
| 2143 | |
| 2144 | OS << " // Get the next operand index.\n"; |
| 2145 | OS << " unsigned NextOpNum = Operands.size()-1;\n"; |
| 2146 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2147 | // Emit code to search the table. |
| 2148 | OS << " // Search the table.\n"; |
| 2149 | OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>"; |
| 2150 | OS << " MnemonicRange =\n"; |
| 2151 | OS << " std::equal_range(OperandMatchTable, OperandMatchTable+" |
| 2152 | << Info.OperandMatchInfo.size() << ", Mnemonic,\n" |
| 2153 | << " LessOpcodeOperand());\n\n"; |
| 2154 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2155 | OS << " if (MnemonicRange.first == MnemonicRange.second)\n"; |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2156 | OS << " return MatchOperand_NoMatch;\n\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2157 | |
| 2158 | OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n" |
| 2159 | << " *ie = MnemonicRange.second; it != ie; ++it) {\n"; |
| 2160 | |
| 2161 | OS << " // equal_range guarantees that instruction mnemonic matches.\n"; |
Benjamin Kramer | b08bb34 | 2012-03-03 20:44:43 +0000 | [diff] [blame] | 2162 | OS << " assert(Mnemonic == it->getMnemonic());\n\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2163 | |
| 2164 | // Emit check that the required features are available. |
| 2165 | OS << " // check if the available features match\n"; |
| 2166 | OS << " if ((AvailableFeatures & it->RequiredFeatures) " |
| 2167 | << "!= it->RequiredFeatures) {\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2168 | OS << " continue;\n"; |
| 2169 | OS << " }\n\n"; |
| 2170 | |
| 2171 | // Emit check to ensure the operand number matches. |
| 2172 | OS << " // check if the operand in question has a custom parser.\n"; |
| 2173 | OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n"; |
| 2174 | OS << " continue;\n\n"; |
| 2175 | |
| 2176 | // Emit call to the custom parser method |
| 2177 | OS << " // call custom parse method to handle the operand\n"; |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2178 | OS << " OperandMatchResultTy Result = "; |
Jim Grosbach | 3d5d8f6 | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 2179 | OS << "tryCustomParseOperand(Operands, it->Class);\n"; |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2180 | OS << " if (Result != MatchOperand_NoMatch)\n"; |
| 2181 | OS << " return Result;\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2182 | OS << " }\n\n"; |
| 2183 | |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2184 | OS << " // Okay, we had no match.\n"; |
| 2185 | OS << " return MatchOperand_NoMatch;\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2186 | OS << "}\n\n"; |
| 2187 | } |
| 2188 | |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 2189 | void AsmMatcherEmitter::run(raw_ostream &OS) { |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 2190 | CodeGenTarget Target(Records); |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 2191 | Record *AsmParser = Target.getAsmParser(); |
| 2192 | std::string ClassName = AsmParser->getValueAsString("AsmParserClassName"); |
| 2193 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2194 | // Compute the information on the instructions to match. |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 2195 | AsmMatcherInfo Info(AsmParser, Target, Records); |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 2196 | Info.BuildInfo(); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 2197 | |
Daniel Dunbar | e1f6de3 | 2010-02-02 23:46:36 +0000 | [diff] [blame] | 2198 | // Sort the instruction table using the partial order on classes. We use |
| 2199 | // stable_sort to ensure that ambiguous instructions are still |
| 2200 | // deterministically ordered. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2201 | std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(), |
| 2202 | less_ptr<MatchableInfo>()); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2203 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 2204 | DEBUG_WITH_TYPE("instruction_info", { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2205 | for (std::vector<MatchableInfo*>::iterator |
| 2206 | it = Info.Matchables.begin(), ie = Info.Matchables.end(); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2207 | it != ie; ++it) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 2208 | (*it)->dump(); |
| 2209 | }); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 2210 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2211 | // Check for ambiguous matchables. |
Chris Lattner | fa0d74d | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 2212 | DEBUG_WITH_TYPE("ambiguous_instrs", { |
| 2213 | unsigned NumAmbiguous = 0; |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2214 | for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) { |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 2215 | for (unsigned j = i + 1; j != e; ++j) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2216 | MatchableInfo &A = *Info.Matchables[i]; |
| 2217 | MatchableInfo &B = *Info.Matchables[j]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2218 | |
Bob Wilson | 1f64ac4 | 2011-01-26 21:26:21 +0000 | [diff] [blame] | 2219 | if (A.CouldMatchAmbiguouslyWith(B)) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2220 | errs() << "warning: ambiguous matchables:\n"; |
Chris Lattner | fa0d74d | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 2221 | A.dump(); |
| 2222 | errs() << "\nis incomparable with:\n"; |
| 2223 | B.dump(); |
| 2224 | errs() << "\n\n"; |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 2225 | ++NumAmbiguous; |
| 2226 | } |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 2227 | } |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 2228 | } |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 2229 | if (NumAmbiguous) |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2230 | errs() << "warning: " << NumAmbiguous |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2231 | << " ambiguous matchables!\n"; |
Chris Lattner | fa0d74d | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 2232 | }); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 2233 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2234 | // Compute the information on the custom operand parsing. |
| 2235 | Info.BuildOperandMatchInfo(); |
| 2236 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 2237 | // Write the output. |
| 2238 | |
| 2239 | EmitSourceFileHeader("Assembly Matcher Source Fragment", OS); |
| 2240 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 2241 | // Information for the class declaration. |
| 2242 | OS << "\n#ifdef GET_ASSEMBLER_HEADER\n"; |
| 2243 | OS << "#undef GET_ASSEMBLER_HEADER\n"; |
Jim Grosbach | 84cb033 | 2011-02-11 21:31:55 +0000 | [diff] [blame] | 2244 | 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] | 2245 | OS << " // your subclasses implementation of MCTargetAsmParser.\n"; |
Evan Cheng | ebdeeab | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 2246 | OS << " unsigned ComputeAvailableFeatures(uint64_t FeatureBits) const;\n"; |
Daniel Dunbar | 5c228a9 | 2011-02-04 23:17:40 +0000 | [diff] [blame] | 2247 | OS << " bool ConvertToMCInst(unsigned Kind, MCInst &Inst, " |
| 2248 | << "unsigned Opcode,\n" |
| 2249 | << " const SmallVectorImpl<MCParsedAsmOperand*> " |
| 2250 | << "&Operands);\n"; |
Bob Wilson | 1fe3aa1 | 2011-01-26 21:43:46 +0000 | [diff] [blame] | 2251 | OS << " bool MnemonicIsValid(StringRef Mnemonic);\n"; |
Jim Grosbach | 19cb7f4 | 2011-08-15 23:03:29 +0000 | [diff] [blame] | 2252 | OS << " unsigned MatchInstructionImpl(\n"; |
Daniel Dunbar | 083203d | 2011-01-10 15:26:11 +0000 | [diff] [blame] | 2253 | OS << " const SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n"; |
Devang Patel | 56315d3 | 2012-01-10 17:50:43 +0000 | [diff] [blame] | 2254 | OS << " MCInst &Inst, unsigned &ErrorInfo, unsigned VariantID = 0);\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2255 | |
| 2256 | if (Info.OperandMatchInfo.size()) { |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 2257 | OS << "\n enum OperandMatchResultTy {\n"; |
| 2258 | OS << " MatchOperand_Success, // operand matched successfully\n"; |
| 2259 | OS << " MatchOperand_NoMatch, // operand did not match\n"; |
| 2260 | OS << " MatchOperand_ParseFail // operand matched but had errors\n"; |
| 2261 | OS << " };\n"; |
| 2262 | OS << " OperandMatchResultTy MatchOperandParserImpl(\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2263 | OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n"; |
| 2264 | OS << " StringRef Mnemonic);\n"; |
| 2265 | |
Jim Grosbach | 3d5d8f6 | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 2266 | OS << " OperandMatchResultTy tryCustomParseOperand(\n"; |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2267 | OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n"; |
| 2268 | OS << " unsigned MCK);\n\n"; |
| 2269 | } |
| 2270 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 2271 | OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n"; |
| 2272 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 2273 | OS << "\n#ifdef GET_REGISTER_MATCHER\n"; |
| 2274 | OS << "#undef GET_REGISTER_MATCHER\n\n"; |
| 2275 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2276 | // Emit the subtarget feature enumeration. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 2277 | EmitSubtargetFeatureFlagEnumeration(Info, OS); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2278 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 2279 | // Emit the function to match a register name to number. |
| 2280 | EmitMatchRegisterName(Target, AsmParser, OS); |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 2281 | |
| 2282 | OS << "#endif // GET_REGISTER_MATCHER\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2283 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 2284 | |
| 2285 | OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n"; |
| 2286 | OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n"; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 2287 | |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 2288 | // Generate the function that remaps for mnemonic aliases. |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 2289 | bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info); |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2290 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 2291 | // Generate the unified function to convert operands into an MCInst. |
Daniel Dunbar | 5c228a9 | 2011-02-04 23:17:40 +0000 | [diff] [blame] | 2292 | EmitConvertToMCInst(Target, ClassName, Info.Matchables, OS); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 2293 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2294 | // Emit the enumeration for classes which participate in matching. |
| 2295 | EmitMatchClassEnumeration(Target, Info.Classes, OS); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 2296 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2297 | // Emit the routine to match token strings to their match class. |
| 2298 | EmitMatchTokenString(Target, Info.Classes, OS); |
| 2299 | |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 2300 | // Emit the subclass predicate routine. |
| 2301 | EmitIsSubclass(Target, Info.Classes, OS); |
| 2302 | |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 2303 | // Emit the routine to validate an operand against a match class. |
| 2304 | EmitValidateOperandClass(Info, OS); |
| 2305 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2306 | // Emit the available features compute function. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 2307 | EmitComputeAvailableFeatures(Info, OS); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2308 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2309 | |
| 2310 | size_t MaxNumOperands = 0; |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2311 | for (std::vector<MatchableInfo*>::const_iterator it = |
| 2312 | Info.Matchables.begin(), ie = Info.Matchables.end(); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2313 | it != ie; ++it) |
Chris Lattner | 3116fef | 2010-11-02 01:03:43 +0000 | [diff] [blame] | 2314 | MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size()); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2315 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2316 | // Emit the static match table; unused classes get initalized to 0 which is |
| 2317 | // guaranteed to be InvalidMatchClass. |
| 2318 | // |
| 2319 | // FIXME: We can reduce the size of this table very easily. First, we change |
| 2320 | // it so that store the kinds in separate bit-fields for each index, which |
| 2321 | // only needs to be the max width used for classes at that index (we also need |
| 2322 | // to reject based on this during classification). If we then make sure to |
| 2323 | // order the match kinds appropriately (putting mnemonics last), then we |
| 2324 | // should only end up using a few bits for each class, especially the ones |
| 2325 | // following the mnemonic. |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 2326 | OS << "namespace {\n"; |
| 2327 | OS << " struct MatchEntry {\n"; |
Jakob Stoklund Olesen | 7044cce | 2012-03-15 21:22:53 +0000 | [diff] [blame] | 2328 | OS << " static const char *const MnemonicTable;\n"; |
Craig Topper | fab3f7e | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 2329 | OS << " uint32_t Mnemonic;\n"; |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2330 | OS << " uint16_t Opcode;\n"; |
Benjamin Kramer | af482cf | 2011-10-17 16:18:09 +0000 | [diff] [blame] | 2331 | OS << " " << getMinimalTypeForRange(Info.Matchables.size()) |
| 2332 | << " ConvertFn;\n"; |
Benjamin Kramer | af482cf | 2011-10-17 16:18:09 +0000 | [diff] [blame] | 2333 | OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size()) |
| 2334 | << " RequiredFeatures;\n"; |
Craig Topper | fab3f7e | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 2335 | OS << " " << getMinimalTypeForRange(Info.Classes.size()) |
| 2336 | << " Classes[" << MaxNumOperands << "];\n"; |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2337 | OS << " uint8_t AsmVariantID;\n\n"; |
| 2338 | OS << " StringRef getMnemonic() const {\n"; |
| 2339 | OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n"; |
| 2340 | OS << " MnemonicTable[Mnemonic]);\n"; |
| 2341 | OS << " }\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 2342 | OS << " };\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2343 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2344 | OS << " // Predicate for searching for an opcode.\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 2345 | OS << " struct LessOpcode {\n"; |
| 2346 | OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n"; |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2347 | OS << " return LHS.getMnemonic() < RHS;\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 2348 | OS << " }\n"; |
| 2349 | OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n"; |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2350 | OS << " return LHS < RHS.getMnemonic();\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 2351 | OS << " }\n"; |
Chris Lattner | 32c685c | 2010-09-07 06:10:48 +0000 | [diff] [blame] | 2352 | OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n"; |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2353 | OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n"; |
Chris Lattner | 32c685c | 2010-09-07 06:10:48 +0000 | [diff] [blame] | 2354 | OS << " }\n"; |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 2355 | OS << " };\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2356 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 2357 | OS << "} // end anonymous namespace.\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2358 | |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2359 | StringToOffsetTable StringTable; |
| 2360 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 2361 | OS << "static const MatchEntry MatchTable[" |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2362 | << Info.Matchables.size() << "] = {\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2364 | for (std::vector<MatchableInfo*>::const_iterator it = |
| 2365 | Info.Matchables.begin(), ie = Info.Matchables.end(); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2366 | it != ie; ++it) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2367 | MatchableInfo &II = **it; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2368 | |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2369 | // Store a pascal-style length byte in the mnemonic. |
| 2370 | std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str(); |
Craig Topper | fab3f7e | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 2371 | OS << " { " << StringTable.GetOrAddStringOffset(LenMnemonic, false) |
| 2372 | << " /* " << II.Mnemonic << " */, " |
| 2373 | << Target.getName() << "::" |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2374 | << II.getResultInst()->TheDef->getName() << ", " |
Craig Topper | fab3f7e | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 2375 | << II.ConversionFnKind << ", "; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2376 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2377 | // Write the required features mask. |
| 2378 | if (!II.RequiredFeatures.empty()) { |
| 2379 | for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) { |
| 2380 | if (i) OS << "|"; |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 2381 | OS << II.RequiredFeatures[i]->getEnumName(); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2382 | } |
| 2383 | } else |
| 2384 | OS << "0"; |
Craig Topper | fab3f7e | 2012-04-02 07:48:39 +0000 | [diff] [blame] | 2385 | |
| 2386 | OS << ", { "; |
| 2387 | for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) { |
| 2388 | MatchableInfo::AsmOperand &Op = II.AsmOperands[i]; |
| 2389 | |
| 2390 | if (i) OS << ", "; |
| 2391 | OS << Op.Class->Name; |
| 2392 | } |
| 2393 | OS << " }, " << II.AsmVariantID; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2394 | OS << "},\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 2395 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2396 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 2397 | OS << "};\n\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 2398 | |
Jakob Stoklund Olesen | 7044cce | 2012-03-15 21:22:53 +0000 | [diff] [blame] | 2399 | OS << "const char *const MatchEntry::MnemonicTable =\n"; |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2400 | StringTable.EmitString(OS); |
| 2401 | OS << ";\n\n"; |
| 2402 | |
Bob Wilson | 1fe3aa1 | 2011-01-26 21:43:46 +0000 | [diff] [blame] | 2403 | // A method to determine if a mnemonic is in the list. |
| 2404 | OS << "bool " << Target.getName() << ClassName << "::\n" |
| 2405 | << "MnemonicIsValid(StringRef Mnemonic) {\n"; |
| 2406 | OS << " // Search the table.\n"; |
| 2407 | OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n"; |
| 2408 | OS << " std::equal_range(MatchTable, MatchTable+" |
| 2409 | << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n"; |
| 2410 | OS << " return MnemonicRange.first != MnemonicRange.second;\n"; |
| 2411 | OS << "}\n\n"; |
| 2412 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 2413 | // Finally, build the match function. |
Jim Grosbach | 19cb7f4 | 2011-08-15 23:03:29 +0000 | [diff] [blame] | 2414 | OS << "unsigned " |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 2415 | << Target.getName() << ClassName << "::\n" |
| 2416 | << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>" |
| 2417 | << " &Operands,\n"; |
Devang Patel | 56315d3 | 2012-01-10 17:50:43 +0000 | [diff] [blame] | 2418 | OS << " MCInst &Inst, unsigned &ErrorInfo,\n"; |
| 2419 | OS << " unsigned VariantID) {\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2420 | |
| 2421 | // Emit code to get the available features. |
| 2422 | OS << " // Get the current feature set.\n"; |
| 2423 | OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n"; |
| 2424 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 2425 | OS << " // Get the instruction mnemonic, which is the first token.\n"; |
| 2426 | OS << " StringRef Mnemonic = ((" << Target.getName() |
| 2427 | << "Operand*)Operands[0])->getToken();\n\n"; |
| 2428 | |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 2429 | if (HasMnemonicAliases) { |
| 2430 | OS << " // Process all MnemonicAliases to remap the mnemonic.\n"; |
Devang Patel | 40bced0 | 2012-01-17 18:30:45 +0000 | [diff] [blame] | 2431 | OS << " // FIXME : Add an entry in AsmParserVariant to check this.\n"; |
| 2432 | OS << " if (!VariantID)\n"; |
| 2433 | OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n"; |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 2434 | } |
Bob Wilson | 828295b | 2011-01-26 21:26:19 +0000 | [diff] [blame] | 2435 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2436 | // Emit code to compute the class list for this operand vector. |
| 2437 | OS << " // Eliminate obvious mismatches.\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 2438 | OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n"; |
| 2439 | OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n"; |
| 2440 | OS << " return Match_InvalidOperand;\n"; |
| 2441 | OS << " }\n\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2442 | |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 2443 | OS << " // Some state to try to produce better error messages.\n"; |
Jim Grosbach | 19cb7f4 | 2011-08-15 23:03:29 +0000 | [diff] [blame] | 2444 | OS << " bool HadMatchOtherThanFeatures = false;\n"; |
Jim Grosbach | 578071a | 2011-08-16 20:12:35 +0000 | [diff] [blame] | 2445 | OS << " bool HadMatchOtherThanPredicate = false;\n"; |
Jim Grosbach | 19cb7f4 | 2011-08-15 23:03:29 +0000 | [diff] [blame] | 2446 | OS << " unsigned RetCode = Match_InvalidOperand;\n"; |
Jim Grosbach | 84cb033 | 2011-02-11 21:31:55 +0000 | [diff] [blame] | 2447 | OS << " // Set ErrorInfo to the operand that mismatches if it is\n"; |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 2448 | OS << " // wrong for all instances of the instruction.\n"; |
| 2449 | OS << " ErrorInfo = ~0U;\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 2450 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2451 | // Emit code to search the table. |
| 2452 | OS << " // Search the table.\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 2453 | OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n"; |
| 2454 | OS << " std::equal_range(MatchTable, MatchTable+" |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame] | 2455 | << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2456 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 2457 | OS << " // Return a more specific error code if no mnemonics match.\n"; |
| 2458 | OS << " if (MnemonicRange.first == MnemonicRange.second)\n"; |
| 2459 | OS << " return Match_MnemonicFail;\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2460 | |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 2461 | OS << " for (const MatchEntry *it = MnemonicRange.first, " |
Chris Lattner | 80db4e5 | 2010-09-06 21:23:43 +0000 | [diff] [blame] | 2462 | << "*ie = MnemonicRange.second;\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 2463 | OS << " it != ie; ++it) {\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2464 | |
Gabor Greif | e53ee3b | 2010-09-07 06:06:06 +0000 | [diff] [blame] | 2465 | OS << " // equal_range guarantees that instruction mnemonic matches.\n"; |
Benjamin Kramer | a4c5ecf | 2012-03-03 19:13:26 +0000 | [diff] [blame] | 2466 | OS << " assert(Mnemonic == it->getMnemonic());\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2467 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 2468 | // Emit check that the subclasses match. |
Devang Patel | 56315d3 | 2012-01-10 17:50:43 +0000 | [diff] [blame] | 2469 | OS << " if (VariantID != it->AsmVariantID) continue;\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 2470 | OS << " bool OperandsValid = true;\n"; |
| 2471 | OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n"; |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 2472 | OS << " if (i + 1 >= Operands.size()) {\n"; |
| 2473 | OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n"; |
Jim Grosbach | b9d5af0 | 2011-05-03 19:09:56 +0000 | [diff] [blame] | 2474 | OS << " break;\n"; |
Jim Grosbach | b9db0c5 | 2011-02-10 00:08:28 +0000 | [diff] [blame] | 2475 | OS << " }\n"; |
Jim Grosbach | 3d5d8f6 | 2011-12-06 22:07:02 +0000 | [diff] [blame] | 2476 | OS << " if (validateOperandClass(Operands[i+1], " |
Benjamin Kramer | af482cf | 2011-10-17 16:18:09 +0000 | [diff] [blame] | 2477 | "(MatchClassKind)it->Classes[i]))\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 2478 | OS << " continue;\n"; |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 2479 | OS << " // If this operand is broken for all of the instances of this\n"; |
| 2480 | OS << " // mnemonic, keep track of it so we can report loc info.\n"; |
Kevin Enderby | 79fcb6d | 2011-02-02 18:20:55 +0000 | [diff] [blame] | 2481 | OS << " if (it == MnemonicRange.first || ErrorInfo <= i+1)\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 2482 | OS << " ErrorInfo = i+1;\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 2483 | OS << " // Otherwise, just reject this instance of the mnemonic.\n"; |
| 2484 | OS << " OperandsValid = false;\n"; |
| 2485 | OS << " break;\n"; |
| 2486 | OS << " }\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2487 | |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 2488 | OS << " if (!OperandsValid) continue;\n"; |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 2489 | |
| 2490 | // Emit check that the required features are available. |
| 2491 | OS << " if ((AvailableFeatures & it->RequiredFeatures) " |
| 2492 | << "!= it->RequiredFeatures) {\n"; |
| 2493 | OS << " HadMatchOtherThanFeatures = true;\n"; |
| 2494 | OS << " continue;\n"; |
| 2495 | OS << " }\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2496 | OS << "\n"; |
Daniel Dunbar | b412915 | 2011-02-04 17:12:23 +0000 | [diff] [blame] | 2497 | OS << " // We have selected a definite instruction, convert the parsed\n" |
| 2498 | << " // operands into the appropriate MCInst.\n"; |
| 2499 | OS << " if (!ConvertToMCInst(it->ConvertFn, Inst,\n" |
| 2500 | << " it->Opcode, Operands))\n"; |
| 2501 | OS << " return Match_ConversionFail;\n"; |
| 2502 | OS << "\n"; |
Daniel Dunbar | 8cc9c0c | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 2503 | |
Jim Grosbach | 19cb7f4 | 2011-08-15 23:03:29 +0000 | [diff] [blame] | 2504 | // Verify the instruction with the target-specific match predicate function. |
| 2505 | OS << " // We have a potential match. Check the target predicate to\n" |
| 2506 | << " // handle any context sensitive constraints.\n" |
| 2507 | << " unsigned MatchResult;\n" |
| 2508 | << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !=" |
| 2509 | << " Match_Success) {\n" |
| 2510 | << " Inst.clear();\n" |
| 2511 | << " RetCode = MatchResult;\n" |
Jim Grosbach | 578071a | 2011-08-16 20:12:35 +0000 | [diff] [blame] | 2512 | << " HadMatchOtherThanPredicate = true;\n" |
Jim Grosbach | 19cb7f4 | 2011-08-15 23:03:29 +0000 | [diff] [blame] | 2513 | << " continue;\n" |
| 2514 | << " }\n\n"; |
| 2515 | |
Daniel Dunbar | 8cc9c0c | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 2516 | // Call the post-processing function, if used. |
| 2517 | std::string InsnCleanupFn = |
| 2518 | AsmParser->getValueAsString("AsmParserInstCleanup"); |
| 2519 | if (!InsnCleanupFn.empty()) |
| 2520 | OS << " " << InsnCleanupFn << "(Inst);\n"; |
| 2521 | |
Chris Lattner | 79ed3f7 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 2522 | OS << " return Match_Success;\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 2523 | OS << " }\n\n"; |
| 2524 | |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 2525 | OS << " // Okay, we had no match. Try to return a useful error code.\n"; |
Jim Grosbach | 578071a | 2011-08-16 20:12:35 +0000 | [diff] [blame] | 2526 | OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)"; |
| 2527 | OS << " return RetCode;\n"; |
| 2528 | OS << " return Match_MissingFeature;\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 2529 | OS << "}\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 2530 | |
Bruno Cardoso Lopes | e7a5452 | 2011-02-07 19:38:32 +0000 | [diff] [blame] | 2531 | if (Info.OperandMatchInfo.size()) |
| 2532 | EmitCustomOperandParsing(OS, Target, Info, ClassName); |
| 2533 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 2534 | OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n"; |
Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 2535 | } |