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 |
| 11 | // assembly operands in the MCInst structures. |
| 12 | // |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 13 | // The input to the target specific matcher is a list of literal tokens and |
| 14 | // operands. The target specific parser should generally eliminate any syntax |
| 15 | // which is not relevant for matching; for example, comma tokens should have |
| 16 | // already been consumed and eliminated by the parser. Most instructions will |
| 17 | // end up with a single literal token (the instruction name) and some number of |
| 18 | // operands. |
| 19 | // |
| 20 | // Some example inputs, for X86: |
| 21 | // 'addl' (immediate ...) (register ...) |
| 22 | // 'add' (immediate ...) (memory ...) |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 23 | // 'call' '*' %epc |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 24 | // |
| 25 | // The assembly matcher is responsible for converting this input into a precise |
| 26 | // machine instruction (i.e., an instruction with a well defined encoding). This |
| 27 | // mapping has several properties which complicate matching: |
| 28 | // |
| 29 | // - It may be ambiguous; many architectures can legally encode particular |
| 30 | // variants of an instruction in different ways (for example, using a smaller |
| 31 | // encoding for small immediates). Such ambiguities should never be |
| 32 | // arbitrarily resolved by the assembler, the assembler is always responsible |
| 33 | // for choosing the "best" available instruction. |
| 34 | // |
| 35 | // - It may depend on the subtarget or the assembler context. Instructions |
| 36 | // which are invalid for the current mode, but otherwise unambiguous (e.g., |
| 37 | // an SSE instruction in a file being assembled for i486) should be accepted |
| 38 | // and rejected by the assembler front end. However, if the proper encoding |
| 39 | // for an instruction is dependent on the assembler context then the matcher |
| 40 | // is responsible for selecting the correct machine instruction for the |
| 41 | // current mode. |
| 42 | // |
| 43 | // The core matching algorithm attempts to exploit the regularity in most |
| 44 | // instruction sets to quickly determine the set of possibly matching |
| 45 | // instructions, and the simplify the generated code. Additionally, this helps |
| 46 | // to ensure that the ambiguities are intentionally resolved by the user. |
| 47 | // |
| 48 | // The matching is divided into two distinct phases: |
| 49 | // |
| 50 | // 1. Classification: Each operand is mapped to the unique set which (a) |
| 51 | // contains it, and (b) is the largest such subset for which a single |
| 52 | // instruction could match all members. |
| 53 | // |
| 54 | // For register classes, we can generate these subgroups automatically. For |
| 55 | // arbitrary operands, we expect the user to define the classes and their |
| 56 | // relations to one another (for example, 8-bit signed immediates as a |
| 57 | // subset of 32-bit immediates). |
| 58 | // |
| 59 | // By partitioning the operands in this way, we guarantee that for any |
| 60 | // tuple of classes, any single instruction must match either all or none |
| 61 | // of the sets of operands which could classify to that tuple. |
| 62 | // |
| 63 | // In addition, the subset relation amongst classes induces a partial order |
| 64 | // on such tuples, which we use to resolve ambiguities. |
| 65 | // |
| 66 | // FIXME: What do we do if a crazy case shows up where this is the wrong |
| 67 | // resolution? |
| 68 | // |
| 69 | // 2. The input can now be treated as a tuple of classes (static tokens are |
| 70 | // simple singleton sets). Each such tuple should generally map to a single |
| 71 | // instruction (we currently ignore cases where this isn't true, whee!!!), |
| 72 | // which we can emit a simple matcher for. |
| 73 | // |
Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | |
| 76 | #include "AsmMatcherEmitter.h" |
| 77 | #include "CodeGenTarget.h" |
| 78 | #include "Record.h" |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 79 | #include "StringMatcher.h" |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 80 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 81 | #include "llvm/ADT/SmallPtrSet.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 82 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 83 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 84 | #include "llvm/ADT/StringExtras.h" |
| 85 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 86 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 87 | #include <list> |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 88 | #include <map> |
| 89 | #include <set> |
Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 90 | using namespace llvm; |
| 91 | |
Daniel Dunbar | 2724915 | 2009-08-07 20:33:39 +0000 | [diff] [blame] | 92 | static cl::opt<std::string> |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 93 | MatchPrefix("match-prefix", cl::init(""), |
| 94 | cl::desc("Only match instructions with the given prefix")); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 95 | |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 96 | /// TokenizeAsmString - Tokenize a simplified assembly string. |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 97 | static void TokenizeAsmString(StringRef AsmString, |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 98 | SmallVectorImpl<StringRef> &Tokens) { |
| 99 | unsigned Prev = 0; |
| 100 | bool InTok = true; |
| 101 | for (unsigned i = 0, e = AsmString.size(); i != e; ++i) { |
| 102 | switch (AsmString[i]) { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 103 | case '[': |
| 104 | case ']': |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 105 | case '*': |
| 106 | case '!': |
| 107 | case ' ': |
| 108 | case '\t': |
| 109 | case ',': |
| 110 | if (InTok) { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 111 | Tokens.push_back(AsmString.slice(Prev, i)); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 112 | InTok = false; |
| 113 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 114 | if (!isspace(AsmString[i]) && AsmString[i] != ',') |
| 115 | Tokens.push_back(AsmString.substr(i, 1)); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 116 | Prev = i + 1; |
| 117 | break; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 118 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 119 | case '\\': |
| 120 | if (InTok) { |
| 121 | Tokens.push_back(AsmString.slice(Prev, i)); |
| 122 | InTok = false; |
| 123 | } |
| 124 | ++i; |
| 125 | assert(i != AsmString.size() && "Invalid quoted character"); |
| 126 | Tokens.push_back(AsmString.substr(i, 1)); |
| 127 | Prev = i + 1; |
| 128 | break; |
| 129 | |
| 130 | case '$': { |
| 131 | // If this isn't "${", treat like a normal token. |
| 132 | if (i + 1 == AsmString.size() || AsmString[i + 1] != '{') { |
| 133 | if (InTok) { |
| 134 | Tokens.push_back(AsmString.slice(Prev, i)); |
| 135 | InTok = false; |
| 136 | } |
| 137 | Prev = i; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | if (InTok) { |
| 142 | Tokens.push_back(AsmString.slice(Prev, i)); |
| 143 | InTok = false; |
| 144 | } |
| 145 | |
| 146 | StringRef::iterator End = |
| 147 | std::find(AsmString.begin() + i, AsmString.end(), '}'); |
| 148 | assert(End != AsmString.end() && "Missing brace in operand reference!"); |
| 149 | size_t EndPos = End - AsmString.begin(); |
| 150 | Tokens.push_back(AsmString.slice(i, EndPos+1)); |
| 151 | Prev = EndPos + 1; |
| 152 | i = EndPos; |
| 153 | break; |
| 154 | } |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 155 | |
Daniel Dunbar | 4d39b67 | 2010-08-11 06:36:59 +0000 | [diff] [blame] | 156 | case '.': |
| 157 | if (InTok) { |
| 158 | Tokens.push_back(AsmString.slice(Prev, i)); |
| 159 | } |
| 160 | Prev = i; |
| 161 | InTok = true; |
| 162 | break; |
| 163 | |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 164 | default: |
| 165 | InTok = true; |
| 166 | } |
| 167 | } |
| 168 | if (InTok && Prev != AsmString.size()) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 169 | Tokens.push_back(AsmString.substr(Prev)); |
| 170 | } |
| 171 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 172 | |
| 173 | namespace { |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 174 | class AsmMatcherInfo; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 175 | struct SubtargetFeatureInfo; |
| 176 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 177 | /// ClassInfo - Helper class for storing the information about a particular |
| 178 | /// class of operands which can be matched. |
| 179 | struct ClassInfo { |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 180 | enum ClassInfoKind { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 181 | /// Invalid kind, for use as a sentinel value. |
| 182 | Invalid = 0, |
| 183 | |
| 184 | /// The class for a particular token. |
| 185 | Token, |
| 186 | |
| 187 | /// The (first) register class, subsequent register classes are |
| 188 | /// RegisterClass0+1, and so on. |
| 189 | RegisterClass0, |
| 190 | |
| 191 | /// The (first) user defined class, subsequent user defined classes are |
| 192 | /// UserClass0+1, and so on. |
| 193 | UserClass0 = 1<<16 |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | /// Kind - The class kind, which is either a predefined kind, or (UserClass0 + |
| 197 | /// N) for the Nth user defined class. |
| 198 | unsigned Kind; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 199 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 200 | /// SuperClasses - The super classes of this class. Note that for simplicities |
| 201 | /// sake user operands only record their immediate super class, while register |
| 202 | /// operands include all superclasses. |
| 203 | std::vector<ClassInfo*> SuperClasses; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 204 | |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 205 | /// Name - The full class name, suitable for use in an enum. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 206 | std::string Name; |
| 207 | |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 208 | /// ClassName - The unadorned generic name for this class (e.g., Token). |
| 209 | std::string ClassName; |
| 210 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 211 | /// ValueName - The name of the value this class represents; for a token this |
| 212 | /// is the literal token string, for an operand it is the TableGen class (or |
| 213 | /// empty if this is a derived class). |
| 214 | std::string ValueName; |
| 215 | |
| 216 | /// PredicateMethod - The name of the operand method to test whether the |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 217 | /// 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] | 218 | std::string PredicateMethod; |
| 219 | |
| 220 | /// 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] | 221 | /// MCInst; this is not valid for Token or register kinds. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 222 | std::string RenderMethod; |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 223 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 224 | /// For register classes, the records for all the registers in this class. |
| 225 | std::set<Record*> Registers; |
| 226 | |
| 227 | public: |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 228 | /// isRegisterClass() - Check if this is a register class. |
| 229 | bool isRegisterClass() const { |
| 230 | return Kind >= RegisterClass0 && Kind < UserClass0; |
| 231 | } |
| 232 | |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 233 | /// isUserClass() - Check if this is a user defined class. |
| 234 | bool isUserClass() const { |
| 235 | return Kind >= UserClass0; |
| 236 | } |
| 237 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 238 | /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes |
| 239 | /// are related if they are in the same class hierarchy. |
| 240 | bool isRelatedTo(const ClassInfo &RHS) const { |
| 241 | // Tokens are only related to tokens. |
| 242 | if (Kind == Token || RHS.Kind == Token) |
| 243 | return Kind == Token && RHS.Kind == Token; |
| 244 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 245 | // Registers classes are only related to registers classes, and only if |
| 246 | // their intersection is non-empty. |
| 247 | if (isRegisterClass() || RHS.isRegisterClass()) { |
| 248 | if (!isRegisterClass() || !RHS.isRegisterClass()) |
| 249 | return false; |
| 250 | |
| 251 | std::set<Record*> Tmp; |
| 252 | std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin()); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 253 | std::set_intersection(Registers.begin(), Registers.end(), |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 254 | RHS.Registers.begin(), RHS.Registers.end(), |
| 255 | II); |
| 256 | |
| 257 | return !Tmp.empty(); |
| 258 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 259 | |
| 260 | // Otherwise we have two users operands; they are related if they are in the |
| 261 | // same class hierarchy. |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 262 | // |
| 263 | // FIXME: This is an oversimplification, they should only be related if they |
| 264 | // intersect, however we don't have that information. |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 265 | assert(isUserClass() && RHS.isUserClass() && "Unexpected class!"); |
| 266 | const ClassInfo *Root = this; |
| 267 | while (!Root->SuperClasses.empty()) |
| 268 | Root = Root->SuperClasses.front(); |
| 269 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 270 | const ClassInfo *RHSRoot = &RHS; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 271 | while (!RHSRoot->SuperClasses.empty()) |
| 272 | RHSRoot = RHSRoot->SuperClasses.front(); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 273 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 274 | return Root == RHSRoot; |
| 275 | } |
| 276 | |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 277 | /// isSubsetOf - Test whether this class is a subset of \arg RHS; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 278 | bool isSubsetOf(const ClassInfo &RHS) const { |
| 279 | // This is a subset of RHS if it is the same class... |
| 280 | if (this == &RHS) |
| 281 | return true; |
| 282 | |
| 283 | // ... or if any of its super classes are a subset of RHS. |
| 284 | for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(), |
| 285 | ie = SuperClasses.end(); it != ie; ++it) |
| 286 | if ((*it)->isSubsetOf(RHS)) |
| 287 | return true; |
| 288 | |
| 289 | return false; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 292 | /// operator< - Compare two classes. |
| 293 | bool operator<(const ClassInfo &RHS) const { |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 294 | if (this == &RHS) |
| 295 | return false; |
| 296 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 297 | // Unrelated classes can be ordered by kind. |
| 298 | if (!isRelatedTo(RHS)) |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 299 | return Kind < RHS.Kind; |
| 300 | |
| 301 | switch (Kind) { |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 302 | case Invalid: |
| 303 | assert(0 && "Invalid kind!"); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 304 | case Token: |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 305 | // Tokens are comparable by value. |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 306 | // |
| 307 | // FIXME: Compare by enum value. |
| 308 | return ValueName < RHS.ValueName; |
| 309 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 310 | default: |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 311 | // This class preceeds the RHS if it is a proper subset of the RHS. |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 312 | if (isSubsetOf(RHS)) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 313 | return true; |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 314 | if (RHS.isSubsetOf(*this)) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 315 | return false; |
Daniel Dunbar | 368a456 | 2010-05-27 05:31:32 +0000 | [diff] [blame] | 316 | |
| 317 | // Otherwise, order by name to ensure we have a total ordering. |
| 318 | return ValueName < RHS.ValueName; |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 321 | }; |
| 322 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 323 | /// MatchableInfo - Helper class for storing the necessary information for an |
| 324 | /// instruction or alias which is capable of being matched. |
| 325 | struct MatchableInfo { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 326 | struct Operand { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 327 | /// The unique class instance this operand should match. |
| 328 | ClassInfo *Class; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 329 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 330 | /// The original operand this corresponds to, if any. |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 331 | const CGIOperandList::OperandInfo *OperandInfo; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | /// InstrName - The target name for this instruction. |
| 335 | std::string InstrName; |
| 336 | |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 337 | Record *const TheDef; |
| 338 | const CGIOperandList &OperandList; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 339 | |
| 340 | /// AsmString - The assembly string for this instruction (with variants |
| 341 | /// removed). |
| 342 | std::string AsmString; |
| 343 | |
| 344 | /// Tokens - The tokenized assembly pattern that this instruction matches. |
| 345 | SmallVector<StringRef, 4> Tokens; |
| 346 | |
| 347 | /// Operands - The operands that this instruction matches. |
| 348 | SmallVector<Operand, 4> Operands; |
| 349 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 350 | /// Predicates - The required subtarget features to match this instruction. |
| 351 | SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures; |
| 352 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 353 | /// ConversionFnKind - The enum value which is passed to the generated |
| 354 | /// ConvertToMCInst to convert parsed operands into an MCInst for this |
| 355 | /// function. |
| 356 | std::string ConversionFnKind; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 358 | MatchableInfo(const CodeGenInstruction &CGI) |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 359 | : TheDef(CGI.TheDef), OperandList(CGI.Operands), AsmString(CGI.AsmString) { |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 362 | MatchableInfo(const CodeGenInstAlias *Alias) |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 363 | : TheDef(Alias->TheDef), OperandList(Alias->Operands), |
| 364 | AsmString(Alias->AsmString) { |
| 365 | |
| 366 | } |
| 367 | |
| 368 | void Initialize(const AsmMatcherInfo &Info, |
| 369 | SmallPtrSet<Record*, 16> &SingletonRegisters); |
| 370 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 371 | /// Validate - Return true if this matchable is a valid thing to match against |
| 372 | /// and perform a bunch of validity checking. |
| 373 | bool Validate(StringRef CommentDelimiter, bool Hack) const; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 375 | /// getSingletonRegisterForToken - If the specified token is a singleton |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 376 | /// register, return the Record for it, otherwise return null. |
| 377 | Record *getSingletonRegisterForToken(unsigned i, |
| 378 | const AsmMatcherInfo &Info) const; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 380 | /// operator< - Compare two matchables. |
| 381 | bool operator<(const MatchableInfo &RHS) const { |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 382 | // The primary comparator is the instruction mnemonic. |
| 383 | if (Tokens[0] != RHS.Tokens[0]) |
| 384 | return Tokens[0] < RHS.Tokens[0]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 385 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 386 | if (Operands.size() != RHS.Operands.size()) |
| 387 | return Operands.size() < RHS.Operands.size(); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | db2ddb5 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 389 | // Compare lexicographically by operand. The matcher validates that other |
| 390 | // orderings wouldn't be ambiguous using \see CouldMatchAmiguouslyWith(). |
| 391 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 392 | if (*Operands[i].Class < *RHS.Operands[i].Class) |
| 393 | return true; |
Daniel Dunbar | db2ddb5 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 394 | if (*RHS.Operands[i].Class < *Operands[i].Class) |
| 395 | return false; |
| 396 | } |
| 397 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 398 | return false; |
| 399 | } |
| 400 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 401 | /// CouldMatchAmiguouslyWith - Check whether this matchable could |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 402 | /// ambiguously match the same set of operands as \arg RHS (without being a |
| 403 | /// strictly superior match). |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 404 | bool CouldMatchAmiguouslyWith(const MatchableInfo &RHS) { |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 405 | // The number of operands is unambiguous. |
| 406 | if (Operands.size() != RHS.Operands.size()) |
| 407 | return false; |
| 408 | |
Daniel Dunbar | 1402f0b | 2010-01-23 00:26:16 +0000 | [diff] [blame] | 409 | // Otherwise, make sure the ordering of the two instructions is unambiguous |
| 410 | // by checking that either (a) a token or operand kind discriminates them, |
| 411 | // or (b) the ordering among equivalent kinds is consistent. |
| 412 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 413 | // Tokens and operand kinds are unambiguous (assuming a correct target |
| 414 | // specific parser). |
| 415 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 416 | if (Operands[i].Class->Kind != RHS.Operands[i].Class->Kind || |
| 417 | Operands[i].Class->Kind == ClassInfo::Token) |
| 418 | if (*Operands[i].Class < *RHS.Operands[i].Class || |
| 419 | *RHS.Operands[i].Class < *Operands[i].Class) |
| 420 | return false; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 421 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 422 | // Otherwise, this operand could commute if all operands are equivalent, or |
| 423 | // there is a pair of operands that compare less than and a pair that |
| 424 | // compare greater than. |
| 425 | bool HasLT = false, HasGT = false; |
| 426 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 427 | if (*Operands[i].Class < *RHS.Operands[i].Class) |
| 428 | HasLT = true; |
| 429 | if (*RHS.Operands[i].Class < *Operands[i].Class) |
| 430 | HasGT = true; |
| 431 | } |
| 432 | |
| 433 | return !(HasLT ^ HasGT); |
| 434 | } |
| 435 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 436 | void dump(); |
| 437 | }; |
| 438 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 439 | /// SubtargetFeatureInfo - Helper class for storing information on a subtarget |
| 440 | /// feature which participates in instruction matching. |
| 441 | struct SubtargetFeatureInfo { |
| 442 | /// \brief The predicate record for this feature. |
| 443 | Record *TheDef; |
| 444 | |
| 445 | /// \brief An unique index assigned to represent this feature. |
| 446 | unsigned Index; |
| 447 | |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 448 | SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {} |
| 449 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 450 | /// \brief The name of the enumerated constant identifying this feature. |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 451 | std::string getEnumName() const { |
| 452 | return "Feature_" + TheDef->getName(); |
| 453 | } |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 456 | class AsmMatcherInfo { |
| 457 | public: |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 458 | /// The tablegen AsmParser record. |
| 459 | Record *AsmParser; |
| 460 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 461 | /// Target - The target information. |
| 462 | CodeGenTarget &Target; |
| 463 | |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 464 | /// The AsmParser "RegisterPrefix" value. |
| 465 | std::string RegisterPrefix; |
| 466 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 467 | /// The classes which are needed for matching. |
| 468 | std::vector<ClassInfo*> Classes; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 470 | /// The information on the matchables to match. |
| 471 | std::vector<MatchableInfo*> Matchables; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 472 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 473 | /// Map of Register records to their class information. |
| 474 | std::map<Record*, ClassInfo*> RegisterClasses; |
| 475 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 476 | /// Map of Predicate records to their subtarget information. |
| 477 | std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures; |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 478 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 479 | private: |
| 480 | /// Map of token to class information which has already been constructed. |
| 481 | std::map<std::string, ClassInfo*> TokenClasses; |
| 482 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 483 | /// Map of RegisterClass records to their class information. |
| 484 | std::map<Record*, ClassInfo*> RegisterClassClasses; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 485 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 486 | /// Map of AsmOperandClass records to their class information. |
| 487 | std::map<Record*, ClassInfo*> AsmOperandClasses; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 488 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 489 | private: |
| 490 | /// getTokenClass - Lookup or create the class for the given token. |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 491 | ClassInfo *getTokenClass(StringRef Token); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 492 | |
| 493 | /// getOperandClass - Lookup or create the class for the given operand. |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 494 | ClassInfo *getOperandClass(StringRef Token, |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 495 | const CGIOperandList::OperandInfo &OI); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 496 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 497 | /// BuildRegisterClasses - Build the ClassInfo* instances for register |
| 498 | /// classes. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 499 | void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 500 | |
| 501 | /// BuildOperandClasses - Build the ClassInfo* instances for user defined |
| 502 | /// operand classes. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 503 | void BuildOperandClasses(); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 504 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 505 | public: |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 506 | AsmMatcherInfo(Record *AsmParser, CodeGenTarget &Target); |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 507 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 508 | /// BuildInfo - Construct the various tables used during matching. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 509 | void BuildInfo(); |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 510 | |
| 511 | /// getSubtargetFeature - Lookup or create the subtarget feature info for the |
| 512 | /// given operand. |
| 513 | SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const { |
| 514 | assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!"); |
| 515 | std::map<Record*, SubtargetFeatureInfo*>::const_iterator I = |
| 516 | SubtargetFeatures.find(Def); |
| 517 | return I == SubtargetFeatures.end() ? 0 : I->second; |
| 518 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 519 | }; |
| 520 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 523 | void MatchableInfo::dump() { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 524 | errs() << InstrName << " -- " << "flattened:\"" << AsmString << '\"' |
| 525 | << ", tokens:["; |
| 526 | for (unsigned i = 0, e = Tokens.size(); i != e; ++i) { |
| 527 | errs() << Tokens[i]; |
| 528 | if (i + 1 != e) |
| 529 | errs() << ", "; |
| 530 | } |
| 531 | errs() << "]\n"; |
| 532 | |
| 533 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 534 | Operand &Op = Operands[i]; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 535 | errs() << " op[" << i << "] = " << Op.Class->ClassName << " - "; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 536 | if (Op.Class->Kind == ClassInfo::Token) { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 537 | errs() << '\"' << Tokens[i] << "\"\n"; |
| 538 | continue; |
| 539 | } |
| 540 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 541 | if (!Op.OperandInfo) { |
| 542 | errs() << "(singleton register)\n"; |
| 543 | continue; |
| 544 | } |
| 545 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 546 | const CGIOperandList::OperandInfo &OI = *Op.OperandInfo; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 547 | errs() << OI.Name << " " << OI.Rec->getName() |
| 548 | << " (" << OI.MIOperandNo << ", " << OI.MINumOperands << ")\n"; |
| 549 | } |
| 550 | } |
| 551 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 552 | void MatchableInfo::Initialize(const AsmMatcherInfo &Info, |
| 553 | SmallPtrSet<Record*, 16> &SingletonRegisters) { |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 554 | InstrName = TheDef->getName(); |
| 555 | |
| 556 | // TODO: Eventually support asmparser for Variant != 0. |
| 557 | AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, 0); |
| 558 | |
| 559 | TokenizeAsmString(AsmString, Tokens); |
| 560 | |
| 561 | // Compute the require features. |
| 562 | std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates"); |
| 563 | for (unsigned i = 0, e = Predicates.size(); i != e; ++i) |
| 564 | if (SubtargetFeatureInfo *Feature = |
| 565 | Info.getSubtargetFeature(Predicates[i])) |
| 566 | RequiredFeatures.push_back(Feature); |
| 567 | |
| 568 | // Collect singleton registers, if used. |
| 569 | for (unsigned i = 0, e = Tokens.size(); i != e; ++i) { |
| 570 | if (Record *Reg = getSingletonRegisterForToken(i, Info)) |
| 571 | SingletonRegisters.insert(Reg); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 576 | /// getRegisterRecord - Get the register record for \arg name, or 0. |
| 577 | static Record *getRegisterRecord(CodeGenTarget &Target, StringRef Name) { |
| 578 | for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) { |
| 579 | const CodeGenRegister &Reg = Target.getRegisters()[i]; |
| 580 | if (Name == Reg.TheDef->getValueAsString("AsmName")) |
| 581 | return Reg.TheDef; |
| 582 | } |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 587 | bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const { |
| 588 | // Reject matchables with no .s string. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 589 | if (AsmString.empty()) |
| 590 | throw TGError(TheDef->getLoc(), "instruction with empty asm string"); |
| 591 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 592 | // Reject any matchables with a newline in them, they should be marked |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 593 | // isCodeGenOnly if they are pseudo instructions. |
| 594 | if (AsmString.find('\n') != std::string::npos) |
| 595 | throw TGError(TheDef->getLoc(), |
| 596 | "multiline instruction is not valid for the asmparser, " |
| 597 | "mark it isCodeGenOnly"); |
| 598 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 599 | // Remove comments from the asm string. We know that the asmstring only |
| 600 | // has one line. |
| 601 | if (!CommentDelimiter.empty() && |
| 602 | StringRef(AsmString).find(CommentDelimiter) != StringRef::npos) |
| 603 | throw TGError(TheDef->getLoc(), |
| 604 | "asmstring for instruction has comment character in it, " |
| 605 | "mark it isCodeGenOnly"); |
| 606 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 607 | // Reject matchables with operand modifiers, these aren't something we can |
| 608 | /// handle, the target should be refactored to use operands instead of |
| 609 | /// modifiers. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 610 | // |
| 611 | // Also, check for instructions which reference the operand multiple times; |
| 612 | // this implies a constraint we would not honor. |
| 613 | std::set<std::string> OperandNames; |
| 614 | for (unsigned i = 1, e = Tokens.size(); i < e; ++i) { |
| 615 | if (Tokens[i][0] == '$' && Tokens[i].find(':') != StringRef::npos) |
| 616 | throw TGError(TheDef->getLoc(), |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 617 | "matchable with operand modifier '" + Tokens[i].str() + |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 618 | "' not supported by asm matcher. Mark isCodeGenOnly!"); |
| 619 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 620 | // Verify that any operand is only mentioned once. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 621 | if (Tokens[i][0] == '$' && !OperandNames.insert(Tokens[i]).second) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 622 | if (!Hack) |
| 623 | throw TGError(TheDef->getLoc(), |
| 624 | "ERROR: matchable with tied operand '" + Tokens[i].str() + |
| 625 | "' can never be matched!"); |
| 626 | // FIXME: Should reject these. The ARM backend hits this with $lane in a |
| 627 | // bunch of instructions. It is unclear what the right answer is. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 628 | DEBUG({ |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 629 | errs() << "warning: '" << InstrName << "': " |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 630 | << "ignoring instruction with tied operand '" |
| 631 | << Tokens[i].str() << "'\n"; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 632 | }); |
| 633 | return false; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | return true; |
| 638 | } |
| 639 | |
| 640 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 641 | /// getSingletonRegisterForToken - If the specified token is a singleton |
| 642 | /// register, return the register name, otherwise return a null StringRef. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 643 | Record *MatchableInfo:: |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 644 | getSingletonRegisterForToken(unsigned i, const AsmMatcherInfo &Info) const { |
| 645 | StringRef Tok = Tokens[i]; |
| 646 | if (!Tok.startswith(Info.RegisterPrefix)) |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 647 | return 0; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 648 | |
| 649 | StringRef RegName = Tok.substr(Info.RegisterPrefix.size()); |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 650 | if (Record *Rec = getRegisterRecord(Info.Target, RegName)) |
| 651 | return Rec; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 652 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 653 | // If there is no register prefix (i.e. "%" in "%eax"), then this may |
| 654 | // be some random non-register token, just ignore it. |
| 655 | if (Info.RegisterPrefix.empty()) |
| 656 | return 0; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 658 | std::string Err = "unable to find register for '" + RegName.str() + |
| 659 | "' (which matches register prefix)"; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 660 | throw TGError(TheDef->getLoc(), Err); |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 664 | static std::string getEnumNameForToken(StringRef Str) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 665 | std::string Res; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 666 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 667 | for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) { |
| 668 | switch (*it) { |
| 669 | case '*': Res += "_STAR_"; break; |
| 670 | case '%': Res += "_PCT_"; break; |
| 671 | case ':': Res += "_COLON_"; break; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 672 | default: |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 673 | if (isalnum(*it)) |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 674 | Res += *it; |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 675 | else |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 676 | Res += "_" + utostr((unsigned) *it) + "_"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
| 680 | return Res; |
| 681 | } |
| 682 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 683 | ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 684 | ClassInfo *&Entry = TokenClasses[Token]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 685 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 686 | if (!Entry) { |
| 687 | Entry = new ClassInfo(); |
| 688 | Entry->Kind = ClassInfo::Token; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 689 | Entry->ClassName = "Token"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 690 | Entry->Name = "MCK_" + getEnumNameForToken(Token); |
| 691 | Entry->ValueName = Token; |
| 692 | Entry->PredicateMethod = "<invalid>"; |
| 693 | Entry->RenderMethod = "<invalid>"; |
| 694 | Classes.push_back(Entry); |
| 695 | } |
| 696 | |
| 697 | return Entry; |
| 698 | } |
| 699 | |
| 700 | ClassInfo * |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 701 | AsmMatcherInfo::getOperandClass(StringRef Token, |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 702 | const CGIOperandList::OperandInfo &OI) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 703 | if (OI.Rec->isSubClassOf("RegisterClass")) { |
| 704 | ClassInfo *CI = RegisterClassClasses[OI.Rec]; |
| 705 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 706 | if (!CI) |
| 707 | throw TGError(OI.Rec->getLoc(), "register class has no class info!"); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 708 | |
| 709 | return CI; |
| 710 | } |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 711 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 712 | assert(OI.Rec->isSubClassOf("Operand") && "Unexpected operand!"); |
| 713 | Record *MatchClass = OI.Rec->getValueAsDef("ParserMatchClass"); |
| 714 | ClassInfo *CI = AsmOperandClasses[MatchClass]; |
| 715 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 716 | if (!CI) |
| 717 | throw TGError(OI.Rec->getLoc(), "operand has no match class!"); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 718 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 719 | return CI; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 722 | void AsmMatcherInfo:: |
| 723 | BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 724 | std::vector<CodeGenRegisterClass> RegisterClasses; |
| 725 | std::vector<CodeGenRegister> Registers; |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 726 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 727 | RegisterClasses = Target.getRegisterClasses(); |
| 728 | Registers = Target.getRegisters(); |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 729 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 730 | // The register sets used for matching. |
| 731 | std::set< std::set<Record*> > RegisterSets; |
| 732 | |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 733 | // Gather the defined sets. |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 734 | for (std::vector<CodeGenRegisterClass>::iterator it = RegisterClasses.begin(), |
| 735 | ie = RegisterClasses.end(); it != ie; ++it) |
| 736 | RegisterSets.insert(std::set<Record*>(it->Elements.begin(), |
| 737 | it->Elements.end())); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 738 | |
| 739 | // Add any required singleton sets. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 740 | for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(), |
| 741 | ie = SingletonRegisters.end(); it != ie; ++it) { |
| 742 | Record *Rec = *it; |
| 743 | RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1)); |
| 744 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 745 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 746 | // Introduce derived sets where necessary (when a register does not determine |
| 747 | // a unique register set class), and build the mapping of registers to the set |
| 748 | // they should classify to. |
| 749 | std::map<Record*, std::set<Record*> > RegisterMap; |
| 750 | for (std::vector<CodeGenRegister>::iterator it = Registers.begin(), |
| 751 | ie = Registers.end(); it != ie; ++it) { |
| 752 | CodeGenRegister &CGR = *it; |
| 753 | // Compute the intersection of all sets containing this register. |
| 754 | std::set<Record*> ContainingSet; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 755 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 756 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 757 | ie = RegisterSets.end(); it != ie; ++it) { |
| 758 | if (!it->count(CGR.TheDef)) |
| 759 | continue; |
| 760 | |
| 761 | if (ContainingSet.empty()) { |
| 762 | ContainingSet = *it; |
| 763 | } else { |
| 764 | std::set<Record*> Tmp; |
| 765 | std::swap(Tmp, ContainingSet); |
| 766 | std::insert_iterator< std::set<Record*> > II(ContainingSet, |
| 767 | ContainingSet.begin()); |
| 768 | std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), |
| 769 | II); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if (!ContainingSet.empty()) { |
| 774 | RegisterSets.insert(ContainingSet); |
| 775 | RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet)); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | // Construct the register classes. |
| 780 | std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses; |
| 781 | unsigned Index = 0; |
| 782 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 783 | ie = RegisterSets.end(); it != ie; ++it, ++Index) { |
| 784 | ClassInfo *CI = new ClassInfo(); |
| 785 | CI->Kind = ClassInfo::RegisterClass0 + Index; |
| 786 | CI->ClassName = "Reg" + utostr(Index); |
| 787 | CI->Name = "MCK_Reg" + utostr(Index); |
| 788 | CI->ValueName = ""; |
| 789 | CI->PredicateMethod = ""; // unused |
| 790 | CI->RenderMethod = "addRegOperands"; |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 791 | CI->Registers = *it; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 792 | Classes.push_back(CI); |
| 793 | RegisterSetClasses.insert(std::make_pair(*it, CI)); |
| 794 | } |
| 795 | |
| 796 | // Find the superclasses; we could compute only the subgroup lattice edges, |
| 797 | // but there isn't really a point. |
| 798 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 799 | ie = RegisterSets.end(); it != ie; ++it) { |
| 800 | ClassInfo *CI = RegisterSetClasses[*it]; |
| 801 | for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(), |
| 802 | ie2 = RegisterSets.end(); it2 != ie2; ++it2) |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 803 | if (*it != *it2 && |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 804 | std::includes(it2->begin(), it2->end(), it->begin(), it->end())) |
| 805 | CI->SuperClasses.push_back(RegisterSetClasses[*it2]); |
| 806 | } |
| 807 | |
| 808 | // Name the register classes which correspond to a user defined RegisterClass. |
| 809 | for (std::vector<CodeGenRegisterClass>::iterator it = RegisterClasses.begin(), |
| 810 | ie = RegisterClasses.end(); it != ie; ++it) { |
| 811 | ClassInfo *CI = RegisterSetClasses[std::set<Record*>(it->Elements.begin(), |
| 812 | it->Elements.end())]; |
| 813 | if (CI->ValueName.empty()) { |
| 814 | CI->ClassName = it->getName(); |
| 815 | CI->Name = "MCK_" + it->getName(); |
| 816 | CI->ValueName = it->getName(); |
| 817 | } else |
| 818 | CI->ValueName = CI->ValueName + "," + it->getName(); |
| 819 | |
| 820 | RegisterClassClasses.insert(std::make_pair(it->TheDef, CI)); |
| 821 | } |
| 822 | |
| 823 | // Populate the map for individual registers. |
| 824 | for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(), |
| 825 | ie = RegisterMap.end(); it != ie; ++it) |
| 826 | this->RegisterClasses[it->first] = RegisterSetClasses[it->second]; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 827 | |
| 828 | // Name the register classes which correspond to singleton registers. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 829 | for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(), |
| 830 | ie = SingletonRegisters.end(); it != ie; ++it) { |
| 831 | Record *Rec = *it; |
| 832 | ClassInfo *CI = this->RegisterClasses[Rec]; |
| 833 | assert(CI && "Missing singleton register class info!"); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 835 | if (CI->ValueName.empty()) { |
| 836 | CI->ClassName = Rec->getName(); |
| 837 | CI->Name = "MCK_" + Rec->getName(); |
| 838 | CI->ValueName = Rec->getName(); |
| 839 | } else |
| 840 | CI->ValueName = CI->ValueName + "," + Rec->getName(); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 841 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 844 | void AsmMatcherInfo::BuildOperandClasses() { |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 845 | std::vector<Record*> AsmOperands; |
| 846 | AsmOperands = Records.getAllDerivedDefinitions("AsmOperandClass"); |
Daniel Dunbar | a2f5e00 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 847 | |
| 848 | // Pre-populate AsmOperandClasses map. |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 849 | for (std::vector<Record*>::iterator it = AsmOperands.begin(), |
Daniel Dunbar | a2f5e00 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 850 | ie = AsmOperands.end(); it != ie; ++it) |
| 851 | AsmOperandClasses[*it] = new ClassInfo(); |
| 852 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 853 | unsigned Index = 0; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 854 | for (std::vector<Record*>::iterator it = AsmOperands.begin(), |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 855 | ie = AsmOperands.end(); it != ie; ++it, ++Index) { |
Daniel Dunbar | a2f5e00 | 2010-01-30 01:02:37 +0000 | [diff] [blame] | 856 | ClassInfo *CI = AsmOperandClasses[*it]; |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 857 | CI->Kind = ClassInfo::UserClass0 + Index; |
| 858 | |
Daniel Dunbar | 54ddf3d | 2010-05-22 21:02:29 +0000 | [diff] [blame] | 859 | ListInit *Supers = (*it)->getValueAsListInit("SuperClasses"); |
| 860 | for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) { |
| 861 | DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i)); |
| 862 | if (!DI) { |
| 863 | PrintError((*it)->getLoc(), "Invalid super class reference!"); |
| 864 | continue; |
| 865 | } |
| 866 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 867 | ClassInfo *SC = AsmOperandClasses[DI->getDef()]; |
| 868 | if (!SC) |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 869 | PrintError((*it)->getLoc(), "Invalid super class reference!"); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 870 | else |
| 871 | CI->SuperClasses.push_back(SC); |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 872 | } |
| 873 | CI->ClassName = (*it)->getValueAsString("Name"); |
| 874 | CI->Name = "MCK_" + CI->ClassName; |
| 875 | CI->ValueName = (*it)->getName(); |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 876 | |
| 877 | // Get or construct the predicate method name. |
| 878 | Init *PMName = (*it)->getValueInit("PredicateMethod"); |
| 879 | if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) { |
| 880 | CI->PredicateMethod = SI->getValue(); |
| 881 | } else { |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 882 | assert(dynamic_cast<UnsetInit*>(PMName) && |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 883 | "Unexpected PredicateMethod field!"); |
| 884 | CI->PredicateMethod = "is" + CI->ClassName; |
| 885 | } |
| 886 | |
| 887 | // Get or construct the render method name. |
| 888 | Init *RMName = (*it)->getValueInit("RenderMethod"); |
| 889 | if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) { |
| 890 | CI->RenderMethod = SI->getValue(); |
| 891 | } else { |
| 892 | assert(dynamic_cast<UnsetInit*>(RMName) && |
| 893 | "Unexpected RenderMethod field!"); |
| 894 | CI->RenderMethod = "add" + CI->ClassName + "Operands"; |
| 895 | } |
| 896 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 897 | AsmOperandClasses[*it] = CI; |
| 898 | Classes.push_back(CI); |
| 899 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 902 | AsmMatcherInfo::AsmMatcherInfo(Record *asmParser, CodeGenTarget &target) |
| 903 | : AsmParser(asmParser), Target(target), |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 904 | RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")) { |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 907 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 908 | void AsmMatcherInfo::BuildInfo() { |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 909 | // Build information about all of the AssemblerPredicates. |
| 910 | std::vector<Record*> AllPredicates = |
| 911 | Records.getAllDerivedDefinitions("Predicate"); |
| 912 | for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) { |
| 913 | Record *Pred = AllPredicates[i]; |
| 914 | // Ignore predicates that are not intended for the assembler. |
| 915 | if (!Pred->getValueAsBit("AssemblerMatcherPredicate")) |
| 916 | continue; |
| 917 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 918 | if (Pred->getName().empty()) |
| 919 | throw TGError(Pred->getLoc(), "Predicate has no name!"); |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 920 | |
| 921 | unsigned FeatureNo = SubtargetFeatures.size(); |
| 922 | SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo); |
| 923 | assert(FeatureNo < 32 && "Too many subtarget features!"); |
| 924 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 925 | |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 926 | StringRef CommentDelimiter = AsmParser->getValueAsString("CommentDelimiter"); |
| 927 | |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 928 | // Parse the instructions; we need to do this first so that we can gather the |
| 929 | // singleton register classes. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 930 | SmallPtrSet<Record*, 16> SingletonRegisters; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 931 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 932 | E = Target.inst_end(); I != E; ++I) { |
| 933 | const CodeGenInstruction &CGI = **I; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 934 | |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 935 | // If the tblgen -match-prefix option is specified (for tblgen hackers), |
| 936 | // filter the set of instructions we consider. |
Chris Lattner | b61e09d | 2010-03-19 00:18:23 +0000 | [diff] [blame] | 937 | if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix)) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 938 | continue; |
| 939 | |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 940 | // Ignore "codegen only" instructions. |
| 941 | if (CGI.TheDef->getValueAsBit("isCodeGenOnly")) |
| 942 | continue; |
| 943 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 944 | OwningPtr<MatchableInfo> II(new MatchableInfo(CGI)); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 945 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 946 | II->Initialize(*this, SingletonRegisters); |
| 947 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 948 | // Ignore instructions which shouldn't be matched and diagnose invalid |
| 949 | // instruction definitions with an error. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 950 | if (!II->Validate(CommentDelimiter, true)) |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 951 | continue; |
| 952 | |
| 953 | // Ignore "Int_*" and "*_Int" instructions, which are internal aliases. |
| 954 | // |
| 955 | // FIXME: This is a total hack. |
| 956 | if (StringRef(II->InstrName).startswith("Int_") || |
| 957 | StringRef(II->InstrName).endswith("_Int")) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 958 | continue; |
Chris Lattner | 39ee036 | 2010-10-31 19:10:56 +0000 | [diff] [blame] | 959 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 960 | Matchables.push_back(II.take()); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 961 | } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 962 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 963 | // Parse all of the InstAlias definitions and stick them in the list of |
| 964 | // matchables. |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 965 | std::vector<Record*> AllInstAliases = |
| 966 | Records.getAllDerivedDefinitions("InstAlias"); |
| 967 | for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) { |
| 968 | CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i]); |
| 969 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 970 | OwningPtr<MatchableInfo> II(new MatchableInfo(Alias)); |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 971 | |
Chris Lattner | c2d67bb | 2010-11-01 04:53:48 +0000 | [diff] [blame] | 972 | II->Initialize(*this, SingletonRegisters); |
| 973 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 974 | // Validate the alias definitions. |
| 975 | II->Validate(CommentDelimiter, false); |
| 976 | |
| 977 | //Matchables.push_back(II.take()); |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 978 | } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 979 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 980 | // Build info for the register classes. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 981 | BuildRegisterClasses(SingletonRegisters); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 982 | |
| 983 | // Build info for the user defined assembly operand classes. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 984 | BuildOperandClasses(); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 986 | // Build the information about matchables. |
| 987 | for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(), |
| 988 | ie = Matchables.end(); it != ie; ++it) { |
| 989 | MatchableInfo *II = *it; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 990 | |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 991 | // The first token of the instruction is the mnemonic, which must be a |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 992 | // simple string, not a $foo variable or a singleton register. |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 993 | assert(!II->Tokens.empty() && "Instruction has no tokens?"); |
| 994 | StringRef Mnemonic = II->Tokens[0]; |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 995 | if (Mnemonic[0] == '$' || II->getSingletonRegisterForToken(0, *this)) |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 996 | throw TGError(II->TheDef->getLoc(), |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 997 | "Invalid instruction mnemonic '" + Mnemonic.str() + "'!"); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 998 | |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 999 | // Parse the tokens after the mnemonic. |
| 1000 | for (unsigned i = 1, e = II->Tokens.size(); i != e; ++i) { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1001 | StringRef Token = II->Tokens[i]; |
| 1002 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1003 | // Check for singleton registers. |
Chris Lattner | 1de8823 | 2010-11-01 01:47:07 +0000 | [diff] [blame] | 1004 | if (Record *RegRecord = II->getSingletonRegisterForToken(i, *this)) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1005 | MatchableInfo::Operand Op; |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1006 | Op.Class = RegisterClasses[RegRecord]; |
| 1007 | Op.OperandInfo = 0; |
| 1008 | assert(Op.Class && Op.Class->Registers.size() == 1 && |
| 1009 | "Unexpected class for singleton register"); |
| 1010 | II->Operands.push_back(Op); |
| 1011 | continue; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1014 | // Check for simple tokens. |
| 1015 | if (Token[0] != '$') { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1016 | MatchableInfo::Operand Op; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1017 | Op.Class = getTokenClass(Token); |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1018 | Op.OperandInfo = 0; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1019 | II->Operands.push_back(Op); |
| 1020 | continue; |
| 1021 | } |
| 1022 | |
| 1023 | // Otherwise this is an operand reference. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1024 | StringRef OperandName; |
| 1025 | if (Token[1] == '{') |
| 1026 | OperandName = Token.substr(2, Token.size() - 3); |
| 1027 | else |
| 1028 | OperandName = Token.substr(1); |
| 1029 | |
| 1030 | // Map this token to an operand. FIXME: Move elsewhere. |
| 1031 | unsigned Idx; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1032 | if (!II->OperandList.hasOperandNamed(OperandName, Idx)) |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 1033 | throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" + |
| 1034 | OperandName.str() + "'"); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1035 | |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1036 | // FIXME: This is annoying, the named operand may be tied (e.g., |
| 1037 | // XCHG8rm). What we want is the untied operand, which we now have to |
| 1038 | // grovel for. Only worry about this for single entry operands, we have to |
| 1039 | // clean this up anyway. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1040 | const CGIOperandList::OperandInfo *OI = &II->OperandList[Idx]; |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1041 | if (OI->Constraints[0].isTied()) { |
| 1042 | unsigned TiedOp = OI->Constraints[0].getTiedOperand(); |
| 1043 | |
| 1044 | // The tied operand index is an MIOperand index, find the operand that |
| 1045 | // contains it. |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1046 | for (unsigned i = 0, e = II->OperandList.size(); i != e; ++i) { |
| 1047 | if (II->OperandList[i].MIOperandNo == TiedOp) { |
| 1048 | OI = &II->OperandList[i]; |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | assert(OI && "Unable to find tied operand target!"); |
| 1054 | } |
| 1055 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1056 | MatchableInfo::Operand Op; |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1057 | Op.Class = getOperandClass(Token, *OI); |
| 1058 | Op.OperandInfo = OI; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1059 | II->Operands.push_back(Op); |
| 1060 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1061 | } |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1062 | |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1063 | // Reorder classes so that classes preceed super classes. |
| 1064 | std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>()); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Daniel Dunbar | 3b6910d | 2010-02-12 01:46:54 +0000 | [diff] [blame] | 1067 | static std::pair<unsigned, unsigned> * |
| 1068 | GetTiedOperandAtIndex(SmallVectorImpl<std::pair<unsigned, unsigned> > &List, |
| 1069 | unsigned Index) { |
| 1070 | for (unsigned i = 0, e = List.size(); i != e; ++i) |
| 1071 | if (Index == List[i].first) |
| 1072 | return &List[i]; |
| 1073 | |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1077 | static void EmitConvertToMCInst(CodeGenTarget &Target, |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1078 | std::vector<MatchableInfo*> &Infos, |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1079 | raw_ostream &OS) { |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1080 | // Write the convert function to a separate stream, so we can drop it after |
| 1081 | // the enum. |
| 1082 | std::string ConvertFnBody; |
| 1083 | raw_string_ostream CvtOS(ConvertFnBody); |
| 1084 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1085 | // Function we have already generated. |
| 1086 | std::set<std::string> GeneratedFns; |
| 1087 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1088 | // Start the unified conversion function. |
| 1089 | |
Daniel Dunbar | 8cc9c0c | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 1090 | CvtOS << "static void ConvertToMCInst(ConversionKind Kind, MCInst &Inst, " |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1091 | << "unsigned Opcode,\n" |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1092 | << " const SmallVectorImpl<MCParsedAsmOperand*" |
| 1093 | << "> &Operands) {\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1094 | CvtOS << " Inst.setOpcode(Opcode);\n"; |
| 1095 | CvtOS << " switch (Kind) {\n"; |
| 1096 | CvtOS << " default:\n"; |
| 1097 | |
| 1098 | // Start the enum, which we will generate inline. |
| 1099 | |
| 1100 | OS << "// Unified function for converting operants to MCInst instances.\n\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1101 | OS << "enum ConversionKind {\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1102 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1103 | // TargetOperandClass - This is the target's operand class, like X86Operand. |
| 1104 | std::string TargetOperandClass = Target.getName() + "Operand"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1106 | for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(), |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1107 | ie = Infos.end(); it != ie; ++it) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1108 | MatchableInfo &II = **it; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Order the (class) operands by the order to convert them into an MCInst. |
| 1111 | SmallVector<std::pair<unsigned, unsigned>, 4> MIOperandList; |
| 1112 | for (unsigned i = 0, e = II.Operands.size(); i != e; ++i) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1113 | MatchableInfo::Operand &Op = II.Operands[i]; |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1114 | if (Op.OperandInfo) |
| 1115 | MIOperandList.push_back(std::make_pair(Op.OperandInfo->MIOperandNo, i)); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1116 | } |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1117 | |
| 1118 | // Find any tied operands. |
| 1119 | SmallVector<std::pair<unsigned, unsigned>, 4> TiedOperands; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1120 | for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) { |
| 1121 | const CGIOperandList::OperandInfo &OpInfo = II.OperandList[i]; |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1122 | for (unsigned j = 0, e = OpInfo.Constraints.size(); j != e; ++j) { |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 1123 | const CGIOperandList::ConstraintInfo &CI = OpInfo.Constraints[j]; |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1124 | if (CI.isTied()) |
| 1125 | TiedOperands.push_back(std::make_pair(OpInfo.MIOperandNo + j, |
| 1126 | CI.getTiedOperand())); |
| 1127 | } |
| 1128 | } |
| 1129 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1130 | std::sort(MIOperandList.begin(), MIOperandList.end()); |
| 1131 | |
| 1132 | // Compute the total number of operands. |
| 1133 | unsigned NumMIOperands = 0; |
Chris Lattner | 5bc9387 | 2010-11-01 04:34:44 +0000 | [diff] [blame] | 1134 | for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) { |
| 1135 | const CGIOperandList::OperandInfo &OI = II.OperandList[i]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1136 | NumMIOperands = std::max(NumMIOperands, |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1137 | OI.MIOperandNo + OI.MINumOperands); |
| 1138 | } |
| 1139 | |
| 1140 | // Build the conversion function signature. |
| 1141 | std::string Signature = "Convert"; |
| 1142 | unsigned CurIndex = 0; |
| 1143 | for (unsigned i = 0, e = MIOperandList.size(); i != e; ++i) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1144 | MatchableInfo::Operand &Op = II.Operands[MIOperandList[i].second]; |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1145 | assert(CurIndex <= Op.OperandInfo->MIOperandNo && |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1146 | "Duplicate match for instruction operand!"); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1147 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1148 | // Skip operands which weren't matched by anything, this occurs when the |
| 1149 | // .td file encodes "implicit" operands as explicit ones. |
| 1150 | // |
| 1151 | // FIXME: This should be removed from the MCInst structure. |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1152 | for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex) { |
Daniel Dunbar | 3b6910d | 2010-02-12 01:46:54 +0000 | [diff] [blame] | 1153 | std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands, |
| 1154 | CurIndex); |
| 1155 | if (!Tie) |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1156 | Signature += "__Imp"; |
| 1157 | else |
Daniel Dunbar | 3b6910d | 2010-02-12 01:46:54 +0000 | [diff] [blame] | 1158 | Signature += "__Tie" + utostr(Tie->second); |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | Signature += "__"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1162 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1163 | // Registers are always converted the same, don't duplicate the conversion |
| 1164 | // function based on them. |
| 1165 | // |
| 1166 | // FIXME: We could generalize this based on the render method, if it |
| 1167 | // mattered. |
| 1168 | if (Op.Class->isRegisterClass()) |
| 1169 | Signature += "Reg"; |
| 1170 | else |
| 1171 | Signature += Op.Class->ClassName; |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1172 | Signature += utostr(Op.OperandInfo->MINumOperands); |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1173 | Signature += "_" + utostr(MIOperandList[i].second); |
| 1174 | |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1175 | CurIndex += Op.OperandInfo->MINumOperands; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | // Add any trailing implicit operands. |
Daniel Dunbar | 3b6910d | 2010-02-12 01:46:54 +0000 | [diff] [blame] | 1179 | for (; CurIndex != NumMIOperands; ++CurIndex) { |
| 1180 | std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands, |
| 1181 | CurIndex); |
| 1182 | if (!Tie) |
| 1183 | Signature += "__Imp"; |
| 1184 | else |
| 1185 | Signature += "__Tie" + utostr(Tie->second); |
| 1186 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1187 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1188 | II.ConversionFnKind = Signature; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1189 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1190 | // Check if we have already generated this signature. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1191 | if (!GeneratedFns.insert(Signature).second) |
| 1192 | continue; |
| 1193 | |
| 1194 | // If not, emit it now. |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1195 | |
| 1196 | // Add to the enum list. |
| 1197 | OS << " " << Signature << ",\n"; |
| 1198 | |
| 1199 | // And to the convert function. |
| 1200 | CvtOS << " case " << Signature << ":\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1201 | CurIndex = 0; |
| 1202 | for (unsigned i = 0, e = MIOperandList.size(); i != e; ++i) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1203 | MatchableInfo::Operand &Op = II.Operands[MIOperandList[i].second]; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Add the implicit operands. |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1206 | for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex) { |
| 1207 | // See if this is a tied operand. |
Daniel Dunbar | 3b6910d | 2010-02-12 01:46:54 +0000 | [diff] [blame] | 1208 | std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands, |
| 1209 | CurIndex); |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1210 | |
Daniel Dunbar | 3b6910d | 2010-02-12 01:46:54 +0000 | [diff] [blame] | 1211 | if (!Tie) { |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1212 | // If not, this is some implicit operand. Just assume it is a register |
| 1213 | // for now. |
| 1214 | CvtOS << " Inst.addOperand(MCOperand::CreateReg(0));\n"; |
| 1215 | } else { |
| 1216 | // Copy the tied operand. |
Daniel Dunbar | 3b6910d | 2010-02-12 01:46:54 +0000 | [diff] [blame] | 1217 | assert(Tie->first>Tie->second && "Tied operand preceeds its target!"); |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1218 | CvtOS << " Inst.addOperand(Inst.getOperand(" |
Daniel Dunbar | 3b6910d | 2010-02-12 01:46:54 +0000 | [diff] [blame] | 1219 | << Tie->second << "));\n"; |
Daniel Dunbar | af61681 | 2010-02-10 08:15:48 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1222 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1223 | CvtOS << " ((" << TargetOperandClass << "*)Operands[" |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1224 | << MIOperandList[i].second |
| 1225 | << "+1])->" << Op.Class->RenderMethod |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1226 | << "(Inst, " << Op.OperandInfo->MINumOperands << ");\n"; |
| 1227 | CurIndex += Op.OperandInfo->MINumOperands; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1228 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1229 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1230 | // And add trailing implicit operands. |
Daniel Dunbar | 3b6910d | 2010-02-12 01:46:54 +0000 | [diff] [blame] | 1231 | for (; CurIndex != NumMIOperands; ++CurIndex) { |
| 1232 | std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands, |
| 1233 | CurIndex); |
| 1234 | |
| 1235 | if (!Tie) { |
| 1236 | // If not, this is some implicit operand. Just assume it is a register |
| 1237 | // for now. |
| 1238 | CvtOS << " Inst.addOperand(MCOperand::CreateReg(0));\n"; |
| 1239 | } else { |
| 1240 | // Copy the tied operand. |
| 1241 | assert(Tie->first>Tie->second && "Tied operand preceeds its target!"); |
| 1242 | CvtOS << " Inst.addOperand(Inst.getOperand(" |
| 1243 | << Tie->second << "));\n"; |
| 1244 | } |
| 1245 | } |
| 1246 | |
Daniel Dunbar | 8cc9c0c | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 1247 | CvtOS << " return;\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1248 | } |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1249 | |
| 1250 | // Finish the convert function. |
| 1251 | |
| 1252 | CvtOS << " }\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1253 | CvtOS << "}\n\n"; |
| 1254 | |
| 1255 | // Finish the enum, and drop the convert function after it. |
| 1256 | |
| 1257 | OS << " NumConversionVariants\n"; |
| 1258 | OS << "};\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1259 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1260 | OS << CvtOS.str(); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1263 | /// EmitMatchClassEnumeration - Emit the enumeration for match class kinds. |
| 1264 | static void EmitMatchClassEnumeration(CodeGenTarget &Target, |
| 1265 | std::vector<ClassInfo*> &Infos, |
| 1266 | raw_ostream &OS) { |
| 1267 | OS << "namespace {\n\n"; |
| 1268 | |
| 1269 | OS << "/// MatchClassKind - The kinds of classes which participate in\n" |
| 1270 | << "/// instruction matching.\n"; |
| 1271 | OS << "enum MatchClassKind {\n"; |
| 1272 | OS << " InvalidMatchClass = 0,\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1273 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1274 | ie = Infos.end(); it != ie; ++it) { |
| 1275 | ClassInfo &CI = **it; |
| 1276 | OS << " " << CI.Name << ", // "; |
| 1277 | if (CI.Kind == ClassInfo::Token) { |
| 1278 | OS << "'" << CI.ValueName << "'\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1279 | } else if (CI.isRegisterClass()) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1280 | if (!CI.ValueName.empty()) |
| 1281 | OS << "register class '" << CI.ValueName << "'\n"; |
| 1282 | else |
| 1283 | OS << "derived register class\n"; |
| 1284 | } else { |
| 1285 | OS << "user defined class '" << CI.ValueName << "'\n"; |
| 1286 | } |
| 1287 | } |
| 1288 | OS << " NumMatchClassKinds\n"; |
| 1289 | OS << "};\n\n"; |
| 1290 | |
| 1291 | OS << "}\n\n"; |
| 1292 | } |
| 1293 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1294 | /// EmitClassifyOperand - Emit the function to classify an operand. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1295 | static void EmitClassifyOperand(AsmMatcherInfo &Info, |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1296 | raw_ostream &OS) { |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1297 | OS << "static MatchClassKind ClassifyOperand(MCParsedAsmOperand *GOp) {\n" |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1298 | << " " << Info.Target.getName() << "Operand &Operand = *(" |
| 1299 | << Info.Target.getName() << "Operand*)GOp;\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1300 | |
| 1301 | // Classify tokens. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1302 | OS << " if (Operand.isToken())\n"; |
| 1303 | OS << " return MatchTokenString(Operand.getToken());\n\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1304 | |
| 1305 | // Classify registers. |
| 1306 | // |
| 1307 | // FIXME: Don't hardcode isReg, getReg. |
| 1308 | OS << " if (Operand.isReg()) {\n"; |
| 1309 | OS << " switch (Operand.getReg()) {\n"; |
| 1310 | OS << " default: return InvalidMatchClass;\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1311 | for (std::map<Record*, ClassInfo*>::iterator |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1312 | it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end(); |
| 1313 | it != ie; ++it) |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1314 | OS << " case " << Info.Target.getName() << "::" |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1315 | << it->first->getName() << ": return " << it->second->Name << ";\n"; |
| 1316 | OS << " }\n"; |
| 1317 | OS << " }\n\n"; |
| 1318 | |
| 1319 | // Classify user defined operands. |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1320 | for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(), |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1321 | ie = Info.Classes.end(); it != ie; ++it) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1322 | ClassInfo &CI = **it; |
| 1323 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1324 | if (!CI.isUserClass()) |
| 1325 | continue; |
| 1326 | |
| 1327 | OS << " // '" << CI.ClassName << "' class"; |
| 1328 | if (!CI.SuperClasses.empty()) { |
| 1329 | OS << ", subclass of "; |
| 1330 | for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) { |
| 1331 | if (i) OS << ", "; |
| 1332 | OS << "'" << CI.SuperClasses[i]->ClassName << "'"; |
| 1333 | assert(CI < *CI.SuperClasses[i] && "Invalid class relation!"); |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1334 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1335 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1336 | OS << "\n"; |
| 1337 | |
| 1338 | OS << " if (Operand." << CI.PredicateMethod << "()) {\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1339 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1340 | // Validate subclass relationships. |
| 1341 | if (!CI.SuperClasses.empty()) { |
| 1342 | for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) |
| 1343 | OS << " assert(Operand." << CI.SuperClasses[i]->PredicateMethod |
| 1344 | << "() && \"Invalid class relationship!\");\n"; |
| 1345 | } |
| 1346 | |
| 1347 | OS << " return " << CI.Name << ";\n"; |
| 1348 | OS << " }\n\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1349 | } |
| 1350 | OS << " return InvalidMatchClass;\n"; |
| 1351 | OS << "}\n\n"; |
| 1352 | } |
| 1353 | |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1354 | /// EmitIsSubclass - Emit the subclass predicate function. |
| 1355 | static void EmitIsSubclass(CodeGenTarget &Target, |
| 1356 | std::vector<ClassInfo*> &Infos, |
| 1357 | raw_ostream &OS) { |
| 1358 | OS << "/// IsSubclass - Compute whether \\arg A is a subclass of \\arg B.\n"; |
| 1359 | OS << "static bool IsSubclass(MatchClassKind A, MatchClassKind B) {\n"; |
| 1360 | OS << " if (A == B)\n"; |
| 1361 | OS << " return true;\n\n"; |
| 1362 | |
| 1363 | OS << " switch (A) {\n"; |
| 1364 | OS << " default:\n"; |
| 1365 | OS << " return false;\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1366 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1367 | ie = Infos.end(); it != ie; ++it) { |
| 1368 | ClassInfo &A = **it; |
| 1369 | |
| 1370 | if (A.Kind != ClassInfo::Token) { |
| 1371 | std::vector<StringRef> SuperClasses; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1372 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1373 | ie = Infos.end(); it != ie; ++it) { |
| 1374 | ClassInfo &B = **it; |
| 1375 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1376 | if (&A != &B && A.isSubsetOf(B)) |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1377 | SuperClasses.push_back(B.Name); |
| 1378 | } |
| 1379 | |
| 1380 | if (SuperClasses.empty()) |
| 1381 | continue; |
| 1382 | |
| 1383 | OS << "\n case " << A.Name << ":\n"; |
| 1384 | |
| 1385 | if (SuperClasses.size() == 1) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1386 | OS << " return B == " << SuperClasses.back() << ";\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1387 | continue; |
| 1388 | } |
| 1389 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1390 | OS << " switch (B) {\n"; |
| 1391 | OS << " default: return false;\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1392 | for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1393 | OS << " case " << SuperClasses[i] << ": return true;\n"; |
| 1394 | OS << " }\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | OS << " }\n"; |
| 1398 | OS << "}\n\n"; |
| 1399 | } |
| 1400 | |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1401 | |
| 1402 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1403 | /// EmitMatchTokenString - Emit the function to match a token string to the |
| 1404 | /// appropriate match class value. |
| 1405 | static void EmitMatchTokenString(CodeGenTarget &Target, |
| 1406 | std::vector<ClassInfo*> &Infos, |
| 1407 | raw_ostream &OS) { |
| 1408 | // Construct the match list. |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1409 | std::vector<StringMatcher::StringPair> Matches; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1410 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1411 | ie = Infos.end(); it != ie; ++it) { |
| 1412 | ClassInfo &CI = **it; |
| 1413 | |
| 1414 | if (CI.Kind == ClassInfo::Token) |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1415 | Matches.push_back(StringMatcher::StringPair(CI.ValueName, |
| 1416 | "return " + CI.Name + ";")); |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 1419 | OS << "static MatchClassKind MatchTokenString(StringRef Name) {\n"; |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1420 | |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1421 | StringMatcher("Name", Matches, OS).Emit(); |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1422 | |
| 1423 | OS << " return InvalidMatchClass;\n"; |
| 1424 | OS << "}\n\n"; |
| 1425 | } |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1426 | |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1427 | /// EmitMatchRegisterName - Emit the function to match a string to the target |
| 1428 | /// specific register enum. |
| 1429 | static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser, |
| 1430 | raw_ostream &OS) { |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1431 | // Construct the match list. |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1432 | std::vector<StringMatcher::StringPair> Matches; |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1433 | for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) { |
| 1434 | const CodeGenRegister &Reg = Target.getRegisters()[i]; |
Daniel Dunbar | 22be522 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 1435 | if (Reg.TheDef->getValueAsString("AsmName").empty()) |
| 1436 | continue; |
| 1437 | |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1438 | Matches.push_back(StringMatcher::StringPair( |
| 1439 | Reg.TheDef->getValueAsString("AsmName"), |
| 1440 | "return " + utostr(i + 1) + ";")); |
Daniel Dunbar | 22be522 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 1441 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1442 | |
Chris Lattner | b8d6e98 | 2010-02-09 00:34:28 +0000 | [diff] [blame] | 1443 | OS << "static unsigned MatchRegisterName(StringRef Name) {\n"; |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1444 | |
Chris Lattner | 5845e5c | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 1445 | StringMatcher("Name", Matches, OS).Emit(); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1446 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1447 | OS << " return 0;\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1448 | OS << "}\n\n"; |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1449 | } |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1450 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1451 | /// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag |
| 1452 | /// definitions. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1453 | static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info, |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1454 | raw_ostream &OS) { |
| 1455 | OS << "// Flags for subtarget features that participate in " |
| 1456 | << "instruction matching.\n"; |
| 1457 | OS << "enum SubtargetFeatureFlag {\n"; |
| 1458 | for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator |
| 1459 | it = Info.SubtargetFeatures.begin(), |
| 1460 | ie = Info.SubtargetFeatures.end(); it != ie; ++it) { |
| 1461 | SubtargetFeatureInfo &SFI = *it->second; |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1462 | OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1463 | } |
| 1464 | OS << " Feature_None = 0\n"; |
| 1465 | OS << "};\n\n"; |
| 1466 | } |
| 1467 | |
| 1468 | /// EmitComputeAvailableFeatures - Emit the function to compute the list of |
| 1469 | /// available features given a subtarget. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1470 | static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info, |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1471 | raw_ostream &OS) { |
| 1472 | std::string ClassName = |
| 1473 | Info.AsmParser->getValueAsString("AsmParserClassName"); |
| 1474 | |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1475 | OS << "unsigned " << Info.Target.getName() << ClassName << "::\n" |
| 1476 | << "ComputeAvailableFeatures(const " << Info.Target.getName() |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1477 | << "Subtarget *Subtarget) const {\n"; |
| 1478 | OS << " unsigned Features = 0;\n"; |
| 1479 | for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator |
| 1480 | it = Info.SubtargetFeatures.begin(), |
| 1481 | ie = Info.SubtargetFeatures.end(); it != ie; ++it) { |
| 1482 | SubtargetFeatureInfo &SFI = *it->second; |
| 1483 | OS << " if (" << SFI.TheDef->getValueAsString("CondString") |
| 1484 | << ")\n"; |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1485 | OS << " Features |= " << SFI.getEnumName() << ";\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1486 | } |
| 1487 | OS << " return Features;\n"; |
| 1488 | OS << "}\n\n"; |
| 1489 | } |
| 1490 | |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 1491 | static std::string GetAliasRequiredFeatures(Record *R, |
| 1492 | const AsmMatcherInfo &Info) { |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1493 | std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates"); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1494 | std::string Result; |
| 1495 | unsigned NumFeatures = 0; |
| 1496 | for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) { |
Chris Lattner | 4a74ee7 | 2010-11-01 02:09:21 +0000 | [diff] [blame] | 1497 | SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1498 | |
Chris Lattner | 4a74ee7 | 2010-11-01 02:09:21 +0000 | [diff] [blame] | 1499 | if (F == 0) |
| 1500 | throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() + |
| 1501 | "' is not marked as an AssemblerPredicate!"); |
| 1502 | |
| 1503 | if (NumFeatures) |
| 1504 | Result += '|'; |
| 1505 | |
| 1506 | Result += F->getEnumName(); |
| 1507 | ++NumFeatures; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | if (NumFeatures > 1) |
| 1511 | Result = '(' + Result + ')'; |
| 1512 | return Result; |
| 1513 | } |
| 1514 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1515 | /// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions, |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1516 | /// emit a function for them and return true, otherwise return false. |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1517 | static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) { |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1518 | std::vector<Record*> Aliases = |
| 1519 | Records.getAllDerivedDefinitions("MnemonicAlias"); |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1520 | if (Aliases.empty()) return false; |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1521 | |
Chris Lattner | 8cc0a6b | 2010-10-30 18:57:07 +0000 | [diff] [blame] | 1522 | OS << "static void ApplyMnemonicAliases(StringRef &Mnemonic, " |
| 1523 | "unsigned Features) {\n"; |
| 1524 | |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1525 | // Keep track of all the aliases from a mnemonic. Use an std::map so that the |
| 1526 | // iteration order of the map is stable. |
| 1527 | std::map<std::string, std::vector<Record*> > AliasesFromMnemonic; |
| 1528 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1529 | for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { |
| 1530 | Record *R = Aliases[i]; |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1531 | AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R); |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1532 | } |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1533 | |
| 1534 | // Process each alias a "from" mnemonic at a time, building the code executed |
| 1535 | // by the string remapper. |
| 1536 | std::vector<StringMatcher::StringPair> Cases; |
| 1537 | for (std::map<std::string, std::vector<Record*> >::iterator |
| 1538 | I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end(); |
| 1539 | I != E; ++I) { |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1540 | const std::vector<Record*> &ToVec = I->second; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1541 | |
| 1542 | // Loop through each alias and emit code that handles each case. If there |
| 1543 | // are two instructions without predicates, emit an error. If there is one, |
| 1544 | // emit it last. |
| 1545 | std::string MatchCode; |
| 1546 | int AliasWithNoPredicate = -1; |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1547 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1548 | for (unsigned i = 0, e = ToVec.size(); i != e; ++i) { |
| 1549 | Record *R = ToVec[i]; |
Chris Lattner | 6fa152c | 2010-10-30 20:15:02 +0000 | [diff] [blame] | 1550 | std::string FeatureMask = GetAliasRequiredFeatures(R, Info); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1551 | |
| 1552 | // If this unconditionally matches, remember it for later and diagnose |
| 1553 | // duplicates. |
| 1554 | if (FeatureMask.empty()) { |
| 1555 | if (AliasWithNoPredicate != -1) { |
| 1556 | // We can't have two aliases from the same mnemonic with no predicate. |
| 1557 | PrintError(ToVec[AliasWithNoPredicate]->getLoc(), |
| 1558 | "two MnemonicAliases with the same 'from' mnemonic!"); |
Chris Lattner | 4164f6b | 2010-11-01 04:44:29 +0000 | [diff] [blame] | 1559 | throw TGError(R->getLoc(), "this is the other MnemonicAlias."); |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | AliasWithNoPredicate = i; |
| 1563 | continue; |
| 1564 | } |
| 1565 | |
Chris Lattner | 8cf8bcc | 2010-10-30 19:47:49 +0000 | [diff] [blame] | 1566 | if (!MatchCode.empty()) |
| 1567 | MatchCode += "else "; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1568 | MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n"; |
| 1569 | MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n"; |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1572 | if (AliasWithNoPredicate != -1) { |
| 1573 | Record *R = ToVec[AliasWithNoPredicate]; |
Chris Lattner | 8cf8bcc | 2010-10-30 19:47:49 +0000 | [diff] [blame] | 1574 | if (!MatchCode.empty()) |
| 1575 | MatchCode += "else\n "; |
| 1576 | MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n"; |
Chris Lattner | 693173f | 2010-10-30 19:23:13 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | MatchCode += "return;"; |
| 1580 | |
| 1581 | Cases.push_back(std::make_pair(I->first, MatchCode)); |
Chris Lattner | 4fd32c6 | 2010-10-30 18:56:12 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1584 | |
| 1585 | StringMatcher("Mnemonic", Cases, OS).Emit(); |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1586 | OS << "}\n"; |
| 1587 | |
| 1588 | return true; |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1591 | void AsmMatcherEmitter::run(raw_ostream &OS) { |
| 1592 | CodeGenTarget Target; |
| 1593 | Record *AsmParser = Target.getAsmParser(); |
| 1594 | std::string ClassName = AsmParser->getValueAsString("AsmParserClassName"); |
| 1595 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1596 | // Compute the information on the instructions to match. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1597 | AsmMatcherInfo Info(AsmParser, Target); |
| 1598 | Info.BuildInfo(); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1599 | |
Daniel Dunbar | e1f6de3 | 2010-02-02 23:46:36 +0000 | [diff] [blame] | 1600 | // Sort the instruction table using the partial order on classes. We use |
| 1601 | // stable_sort to ensure that ambiguous instructions are still |
| 1602 | // deterministically ordered. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1603 | std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(), |
| 1604 | less_ptr<MatchableInfo>()); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1605 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1606 | DEBUG_WITH_TYPE("instruction_info", { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1607 | for (std::vector<MatchableInfo*>::iterator |
| 1608 | it = Info.Matchables.begin(), ie = Info.Matchables.end(); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1609 | it != ie; ++it) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1610 | (*it)->dump(); |
| 1611 | }); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1612 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1613 | // Check for ambiguous matchables. |
Chris Lattner | fa0d74d | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 1614 | DEBUG_WITH_TYPE("ambiguous_instrs", { |
| 1615 | unsigned NumAmbiguous = 0; |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1616 | for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) { |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 1617 | for (unsigned j = i + 1; j != e; ++j) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1618 | MatchableInfo &A = *Info.Matchables[i]; |
| 1619 | MatchableInfo &B = *Info.Matchables[j]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1620 | |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 1621 | if (A.CouldMatchAmiguouslyWith(B)) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1622 | errs() << "warning: ambiguous matchables:\n"; |
Chris Lattner | fa0d74d | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 1623 | A.dump(); |
| 1624 | errs() << "\nis incomparable with:\n"; |
| 1625 | B.dump(); |
| 1626 | errs() << "\n\n"; |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 1627 | ++NumAmbiguous; |
| 1628 | } |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 1629 | } |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1630 | } |
Chris Lattner | 8741036 | 2010-09-06 20:21:47 +0000 | [diff] [blame] | 1631 | if (NumAmbiguous) |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1632 | errs() << "warning: " << NumAmbiguous |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1633 | << " ambiguous matchables!\n"; |
Chris Lattner | fa0d74d | 2010-09-06 21:28:52 +0000 | [diff] [blame] | 1634 | }); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1635 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1636 | // Write the output. |
| 1637 | |
| 1638 | EmitSourceFileHeader("Assembly Matcher Source Fragment", OS); |
| 1639 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1640 | // Information for the class declaration. |
| 1641 | OS << "\n#ifdef GET_ASSEMBLER_HEADER\n"; |
| 1642 | OS << "#undef GET_ASSEMBLER_HEADER\n"; |
Chris Lattner | 79ed3f7 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 1643 | OS << " // This should be included into the middle of the declaration of \n"; |
| 1644 | OS << " // your subclasses implementation of TargetAsmParser.\n"; |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1645 | OS << " unsigned ComputeAvailableFeatures(const " << |
| 1646 | Target.getName() << "Subtarget *Subtarget) const;\n"; |
Chris Lattner | 79ed3f7 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 1647 | OS << " enum MatchResultTy {\n"; |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1648 | OS << " Match_Success, Match_MnemonicFail, Match_InvalidOperand,\n"; |
| 1649 | OS << " Match_MissingFeature\n"; |
Chris Lattner | 79ed3f7 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 1650 | OS << " };\n"; |
Jim Grosbach | bb16824 | 2010-10-08 18:13:57 +0000 | [diff] [blame] | 1651 | OS << " MatchResultTy MatchInstructionImpl(const " |
| 1652 | << "SmallVectorImpl<MCParsedAsmOperand*>" |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1653 | << " &Operands, MCInst &Inst, unsigned &ErrorInfo);\n\n"; |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1654 | OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n"; |
| 1655 | |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1656 | |
| 1657 | |
| 1658 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1659 | OS << "\n#ifdef GET_REGISTER_MATCHER\n"; |
| 1660 | OS << "#undef GET_REGISTER_MATCHER\n\n"; |
| 1661 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1662 | // Emit the subtarget feature enumeration. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1663 | EmitSubtargetFeatureFlagEnumeration(Info, OS); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1664 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1665 | // Emit the function to match a register name to number. |
| 1666 | EmitMatchRegisterName(Target, AsmParser, OS); |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1667 | |
| 1668 | OS << "#endif // GET_REGISTER_MATCHER\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1670 | |
| 1671 | OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n"; |
| 1672 | OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n"; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1673 | |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1674 | // Generate the function that remaps for mnemonic aliases. |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1675 | bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info); |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1676 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1677 | // Generate the unified function to convert operands into an MCInst. |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1678 | EmitConvertToMCInst(Target, Info.Matchables, OS); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1679 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1680 | // Emit the enumeration for classes which participate in matching. |
| 1681 | EmitMatchClassEnumeration(Target, Info.Classes, OS); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1682 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1683 | // Emit the routine to match token strings to their match class. |
| 1684 | EmitMatchTokenString(Target, Info.Classes, OS); |
| 1685 | |
| 1686 | // Emit the routine to classify an operand. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1687 | EmitClassifyOperand(Info, OS); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1688 | |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1689 | // Emit the subclass predicate routine. |
| 1690 | EmitIsSubclass(Target, Info.Classes, OS); |
| 1691 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1692 | // Emit the available features compute function. |
Chris Lattner | 02bcbc9 | 2010-11-01 01:37:30 +0000 | [diff] [blame] | 1693 | EmitComputeAvailableFeatures(Info, OS); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1694 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1695 | |
| 1696 | size_t MaxNumOperands = 0; |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1697 | for (std::vector<MatchableInfo*>::const_iterator it = |
| 1698 | Info.Matchables.begin(), ie = Info.Matchables.end(); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1699 | it != ie; ++it) |
| 1700 | MaxNumOperands = std::max(MaxNumOperands, (*it)->Operands.size()); |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1701 | |
| 1702 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1703 | // Emit the static match table; unused classes get initalized to 0 which is |
| 1704 | // guaranteed to be InvalidMatchClass. |
| 1705 | // |
| 1706 | // FIXME: We can reduce the size of this table very easily. First, we change |
| 1707 | // it so that store the kinds in separate bit-fields for each index, which |
| 1708 | // only needs to be the max width used for classes at that index (we also need |
| 1709 | // to reject based on this during classification). If we then make sure to |
| 1710 | // order the match kinds appropriately (putting mnemonics last), then we |
| 1711 | // should only end up using a few bits for each class, especially the ones |
| 1712 | // following the mnemonic. |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1713 | OS << "namespace {\n"; |
| 1714 | OS << " struct MatchEntry {\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1715 | OS << " unsigned Opcode;\n"; |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 1716 | OS << " const char *Mnemonic;\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1717 | OS << " ConversionKind ConvertFn;\n"; |
| 1718 | OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1719 | OS << " unsigned RequiredFeatures;\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1720 | OS << " };\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1721 | |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1722 | OS << "// Predicate for searching for an opcode.\n"; |
| 1723 | OS << " struct LessOpcode {\n"; |
| 1724 | OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n"; |
| 1725 | OS << " return StringRef(LHS.Mnemonic) < RHS;\n"; |
| 1726 | OS << " }\n"; |
| 1727 | OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n"; |
| 1728 | OS << " return LHS < StringRef(RHS.Mnemonic);\n"; |
| 1729 | OS << " }\n"; |
Chris Lattner | 32c685c | 2010-09-07 06:10:48 +0000 | [diff] [blame] | 1730 | OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n"; |
| 1731 | OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n"; |
| 1732 | OS << " }\n"; |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1733 | OS << " };\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1734 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1735 | OS << "} // end anonymous namespace.\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1736 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1737 | OS << "static const MatchEntry MatchTable[" |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1738 | << Info.Matchables.size() << "] = {\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1739 | |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1740 | for (std::vector<MatchableInfo*>::const_iterator it = |
| 1741 | Info.Matchables.begin(), ie = Info.Matchables.end(); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1742 | it != ie; ++it) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1743 | MatchableInfo &II = **it; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1744 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1745 | OS << " { " << Target.getName() << "::" << II.InstrName |
| 1746 | << ", \"" << II.Tokens[0] << "\"" |
| 1747 | << ", " << II.ConversionFnKind << ", { "; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1748 | for (unsigned i = 0, e = II.Operands.size(); i != e; ++i) { |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1749 | MatchableInfo::Operand &Op = II.Operands[i]; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1750 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1751 | if (i) OS << ", "; |
| 1752 | OS << Op.Class->Name; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1753 | } |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1754 | OS << " }, "; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1755 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1756 | // Write the required features mask. |
| 1757 | if (!II.RequiredFeatures.empty()) { |
| 1758 | for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) { |
| 1759 | if (i) OS << "|"; |
Chris Lattner | 0aed1e7 | 2010-10-30 20:07:57 +0000 | [diff] [blame] | 1760 | OS << II.RequiredFeatures[i]->getEnumName(); |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1761 | } |
| 1762 | } else |
| 1763 | OS << "0"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1764 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1765 | OS << "},\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1766 | } |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1767 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1768 | OS << "};\n\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1769 | |
Chris Lattner | 96352e5 | 2010-09-06 21:08:38 +0000 | [diff] [blame] | 1770 | // Finally, build the match function. |
| 1771 | OS << Target.getName() << ClassName << "::MatchResultTy " |
| 1772 | << Target.getName() << ClassName << "::\n" |
| 1773 | << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>" |
| 1774 | << " &Operands,\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1775 | OS << " MCInst &Inst, unsigned &ErrorInfo) {\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1776 | |
| 1777 | // Emit code to get the available features. |
| 1778 | OS << " // Get the current feature set.\n"; |
| 1779 | OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n"; |
| 1780 | |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1781 | OS << " // Get the instruction mnemonic, which is the first token.\n"; |
| 1782 | OS << " StringRef Mnemonic = ((" << Target.getName() |
| 1783 | << "Operand*)Operands[0])->getToken();\n\n"; |
| 1784 | |
Chris Lattner | 7fd4489 | 2010-10-30 18:48:18 +0000 | [diff] [blame] | 1785 | if (HasMnemonicAliases) { |
| 1786 | OS << " // Process all MnemonicAliases to remap the mnemonic.\n"; |
| 1787 | OS << " ApplyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n"; |
| 1788 | } |
Chris Lattner | 674c1dc | 2010-10-30 17:36:36 +0000 | [diff] [blame] | 1789 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1790 | // Emit code to compute the class list for this operand vector. |
| 1791 | OS << " // Eliminate obvious mismatches.\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1792 | OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n"; |
| 1793 | OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n"; |
| 1794 | OS << " return Match_InvalidOperand;\n"; |
| 1795 | OS << " }\n\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1796 | |
| 1797 | OS << " // Compute the class list for this operand vector.\n"; |
| 1798 | OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n"; |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 1799 | OS << " for (unsigned i = 1, e = Operands.size(); i != e; ++i) {\n"; |
| 1800 | OS << " Classes[i-1] = ClassifyOperand(Operands[i]);\n\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1801 | |
| 1802 | OS << " // Check for invalid operands before matching.\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1803 | OS << " if (Classes[i-1] == InvalidMatchClass) {\n"; |
| 1804 | OS << " ErrorInfo = i;\n"; |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1805 | OS << " return Match_InvalidOperand;\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1806 | OS << " }\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1807 | OS << " }\n\n"; |
| 1808 | |
| 1809 | OS << " // Mark unused classes.\n"; |
Chris Lattner | e206fcf | 2010-09-06 21:01:37 +0000 | [diff] [blame] | 1810 | OS << " for (unsigned i = Operands.size()-1, e = " << MaxNumOperands << "; " |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1811 | << "i != e; ++i)\n"; |
| 1812 | OS << " Classes[i] = InvalidMatchClass;\n\n"; |
| 1813 | |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 1814 | OS << " // Some state to try to produce better error messages.\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1815 | OS << " bool HadMatchOtherThanFeatures = false;\n\n"; |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 1816 | OS << " // Set ErrorInfo to the operand that mismatches if it is \n"; |
| 1817 | OS << " // wrong for all instances of the instruction.\n"; |
| 1818 | OS << " ErrorInfo = ~0U;\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1819 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1820 | // Emit code to search the table. |
| 1821 | OS << " // Search the table.\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1822 | OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n"; |
| 1823 | OS << " std::equal_range(MatchTable, MatchTable+" |
Chris Lattner | 22bc5c4 | 2010-11-01 05:06:45 +0000 | [diff] [blame^] | 1824 | << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1826 | OS << " // Return a more specific error code if no mnemonics match.\n"; |
| 1827 | OS << " if (MnemonicRange.first == MnemonicRange.second)\n"; |
| 1828 | OS << " return Match_MnemonicFail;\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1829 | |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1830 | OS << " for (const MatchEntry *it = MnemonicRange.first, " |
Chris Lattner | 80db4e5 | 2010-09-06 21:23:43 +0000 | [diff] [blame] | 1831 | << "*ie = MnemonicRange.second;\n"; |
Chris Lattner | 2b1f943 | 2010-09-06 21:22:45 +0000 | [diff] [blame] | 1832 | OS << " it != ie; ++it) {\n"; |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1833 | |
Gabor Greif | e53ee3b | 2010-09-07 06:06:06 +0000 | [diff] [blame] | 1834 | OS << " // equal_range guarantees that instruction mnemonic matches.\n"; |
Chris Lattner | 44b0daa | 2010-09-06 21:25:43 +0000 | [diff] [blame] | 1835 | OS << " assert(Mnemonic == it->Mnemonic);\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1836 | |
Daniel Dunbar | 54074b5 | 2010-07-19 05:44:09 +0000 | [diff] [blame] | 1837 | // Emit check that the subclasses match. |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1838 | OS << " bool OperandsValid = true;\n"; |
| 1839 | OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n"; |
| 1840 | OS << " if (IsSubclass(Classes[i], it->Classes[i]))\n"; |
| 1841 | OS << " continue;\n"; |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 1842 | OS << " // If this operand is broken for all of the instances of this\n"; |
| 1843 | OS << " // mnemonic, keep track of it so we can report loc info.\n"; |
| 1844 | OS << " if (it == MnemonicRange.first || ErrorInfo == i+1)\n"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1845 | OS << " ErrorInfo = i+1;\n"; |
Chris Lattner | 9bb9fa1 | 2010-09-06 23:37:39 +0000 | [diff] [blame] | 1846 | OS << " else\n"; |
| 1847 | OS << " ErrorInfo = ~0U;"; |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1848 | OS << " // Otherwise, just reject this instance of the mnemonic.\n"; |
| 1849 | OS << " OperandsValid = false;\n"; |
| 1850 | OS << " break;\n"; |
| 1851 | OS << " }\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | ce4a335 | 2010-09-06 22:11:18 +0000 | [diff] [blame] | 1853 | OS << " if (!OperandsValid) continue;\n"; |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1854 | |
| 1855 | // Emit check that the required features are available. |
| 1856 | OS << " if ((AvailableFeatures & it->RequiredFeatures) " |
| 1857 | << "!= it->RequiredFeatures) {\n"; |
| 1858 | OS << " HadMatchOtherThanFeatures = true;\n"; |
| 1859 | OS << " continue;\n"; |
| 1860 | OS << " }\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1861 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1862 | OS << "\n"; |
Daniel Dunbar | 8cc9c0c | 2010-03-18 20:05:56 +0000 | [diff] [blame] | 1863 | OS << " ConvertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n"; |
| 1864 | |
| 1865 | // Call the post-processing function, if used. |
| 1866 | std::string InsnCleanupFn = |
| 1867 | AsmParser->getValueAsString("AsmParserInstCleanup"); |
| 1868 | if (!InsnCleanupFn.empty()) |
| 1869 | OS << " " << InsnCleanupFn << "(Inst);\n"; |
| 1870 | |
Chris Lattner | 79ed3f7 | 2010-09-06 19:22:17 +0000 | [diff] [blame] | 1871 | OS << " return Match_Success;\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1872 | OS << " }\n\n"; |
| 1873 | |
Chris Lattner | ec6789f | 2010-09-06 20:08:02 +0000 | [diff] [blame] | 1874 | OS << " // Okay, we had no match. Try to return a useful error code.\n"; |
| 1875 | OS << " if (HadMatchOtherThanFeatures) return Match_MissingFeature;\n"; |
Chris Lattner | a008e8a | 2010-09-06 21:54:15 +0000 | [diff] [blame] | 1876 | OS << " return Match_InvalidOperand;\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1877 | OS << "}\n\n"; |
Jim Grosbach | a7c7822 | 2010-10-29 22:13:48 +0000 | [diff] [blame] | 1878 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1879 | OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n"; |
Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 1880 | } |