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