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