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