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