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