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