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