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