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