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. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 143 | static void TokenizeAsmString(const 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 | |
| 202 | default: |
| 203 | InTok = true; |
| 204 | } |
| 205 | } |
| 206 | if (InTok && Prev != AsmString.size()) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 207 | Tokens.push_back(AsmString.substr(Prev)); |
| 208 | } |
| 209 | |
| 210 | static bool IsAssemblerInstruction(const StringRef &Name, |
| 211 | const CodeGenInstruction &CGI, |
| 212 | const SmallVectorImpl<StringRef> &Tokens) { |
Daniel Dunbar | 7417b76 | 2009-08-11 22:17:52 +0000 | [diff] [blame] | 213 | // Ignore "codegen only" instructions. |
| 214 | if (CGI.TheDef->getValueAsBit("isCodeGenOnly")) |
| 215 | return false; |
| 216 | |
| 217 | // Ignore pseudo ops. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 218 | // |
Daniel Dunbar | 7417b76 | 2009-08-11 22:17:52 +0000 | [diff] [blame] | 219 | // FIXME: This is a hack; can we convert these instructions to set the |
| 220 | // "codegen only" bit instead? |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 221 | if (const RecordVal *Form = CGI.TheDef->getValue("Form")) |
| 222 | if (Form->getValue()->getAsString() == "Pseudo") |
| 223 | return false; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 224 | |
Daniel Dunbar | 72fa87f | 2009-08-09 08:19:00 +0000 | [diff] [blame] | 225 | // Ignore "Int_*" and "*_Int" instructions, which are internal aliases. |
| 226 | // |
| 227 | // FIXME: This is a total hack. |
| 228 | if (StringRef(Name).startswith("Int_") || StringRef(Name).endswith("_Int")) |
| 229 | return false; |
| 230 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 231 | // Ignore instructions with no .s string. |
| 232 | // |
| 233 | // FIXME: What are these? |
| 234 | if (CGI.AsmString.empty()) |
| 235 | return false; |
| 236 | |
| 237 | // FIXME: Hack; ignore any instructions with a newline in them. |
| 238 | if (std::find(CGI.AsmString.begin(), |
| 239 | CGI.AsmString.end(), '\n') != CGI.AsmString.end()) |
| 240 | return false; |
| 241 | |
| 242 | // Ignore instructions with attributes, these are always fake instructions for |
| 243 | // simplifying codegen. |
| 244 | // |
| 245 | // FIXME: Is this true? |
| 246 | // |
Daniel Dunbar | 7417b76 | 2009-08-11 22:17:52 +0000 | [diff] [blame] | 247 | // Also, check for instructions which reference the operand multiple times; |
| 248 | // this implies a constraint we would not honor. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 249 | std::set<std::string> OperandNames; |
| 250 | for (unsigned i = 1, e = Tokens.size(); i < e; ++i) { |
| 251 | if (Tokens[i][0] == '$' && |
| 252 | std::find(Tokens[i].begin(), |
| 253 | Tokens[i].end(), ':') != Tokens[i].end()) { |
| 254 | DEBUG({ |
| 255 | errs() << "warning: '" << Name << "': " |
| 256 | << "ignoring instruction; operand with attribute '" |
Daniel Dunbar | 7417b76 | 2009-08-11 22:17:52 +0000 | [diff] [blame] | 257 | << Tokens[i] << "'\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 258 | }); |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | if (Tokens[i][0] == '$' && !OperandNames.insert(Tokens[i]).second) { |
Daniel Dunbar | 7417b76 | 2009-08-11 22:17:52 +0000 | [diff] [blame] | 263 | std::string Err = "'" + Name.str() + "': " + |
| 264 | "invalid assembler instruction; tied operand '" + Tokens[i].str() + "'"; |
| 265 | throw TGError(CGI.TheDef->getLoc(), Err); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | namespace { |
| 273 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 274 | /// ClassInfo - Helper class for storing the information about a particular |
| 275 | /// class of operands which can be matched. |
| 276 | struct ClassInfo { |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 277 | enum ClassInfoKind { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 278 | /// Invalid kind, for use as a sentinel value. |
| 279 | Invalid = 0, |
| 280 | |
| 281 | /// The class for a particular token. |
| 282 | Token, |
| 283 | |
| 284 | /// The (first) register class, subsequent register classes are |
| 285 | /// RegisterClass0+1, and so on. |
| 286 | RegisterClass0, |
| 287 | |
| 288 | /// The (first) user defined class, subsequent user defined classes are |
| 289 | /// UserClass0+1, and so on. |
| 290 | UserClass0 = 1<<16 |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | /// Kind - The class kind, which is either a predefined kind, or (UserClass0 + |
| 294 | /// N) for the Nth user defined class. |
| 295 | unsigned Kind; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 296 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 297 | /// SuperClasses - The super classes of this class. Note that for simplicities |
| 298 | /// sake user operands only record their immediate super class, while register |
| 299 | /// operands include all superclasses. |
| 300 | std::vector<ClassInfo*> SuperClasses; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 301 | |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 302 | /// Name - The full class name, suitable for use in an enum. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 303 | std::string Name; |
| 304 | |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 305 | /// ClassName - The unadorned generic name for this class (e.g., Token). |
| 306 | std::string ClassName; |
| 307 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 308 | /// ValueName - The name of the value this class represents; for a token this |
| 309 | /// is the literal token string, for an operand it is the TableGen class (or |
| 310 | /// empty if this is a derived class). |
| 311 | std::string ValueName; |
| 312 | |
| 313 | /// PredicateMethod - The name of the operand method to test whether the |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 314 | /// 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] | 315 | std::string PredicateMethod; |
| 316 | |
| 317 | /// 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] | 318 | /// MCInst; this is not valid for Token or register kinds. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 319 | std::string RenderMethod; |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 320 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 321 | /// For register classes, the records for all the registers in this class. |
| 322 | std::set<Record*> Registers; |
| 323 | |
| 324 | public: |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 325 | /// isRegisterClass() - Check if this is a register class. |
| 326 | bool isRegisterClass() const { |
| 327 | return Kind >= RegisterClass0 && Kind < UserClass0; |
| 328 | } |
| 329 | |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 330 | /// isUserClass() - Check if this is a user defined class. |
| 331 | bool isUserClass() const { |
| 332 | return Kind >= UserClass0; |
| 333 | } |
| 334 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 335 | /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes |
| 336 | /// are related if they are in the same class hierarchy. |
| 337 | bool isRelatedTo(const ClassInfo &RHS) const { |
| 338 | // Tokens are only related to tokens. |
| 339 | if (Kind == Token || RHS.Kind == Token) |
| 340 | return Kind == Token && RHS.Kind == Token; |
| 341 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 342 | // Registers classes are only related to registers classes, and only if |
| 343 | // their intersection is non-empty. |
| 344 | if (isRegisterClass() || RHS.isRegisterClass()) { |
| 345 | if (!isRegisterClass() || !RHS.isRegisterClass()) |
| 346 | return false; |
| 347 | |
| 348 | std::set<Record*> Tmp; |
| 349 | std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin()); |
| 350 | std::set_intersection(Registers.begin(), Registers.end(), |
| 351 | RHS.Registers.begin(), RHS.Registers.end(), |
| 352 | II); |
| 353 | |
| 354 | return !Tmp.empty(); |
| 355 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 356 | |
| 357 | // Otherwise we have two users operands; they are related if they are in the |
| 358 | // same class hierarchy. |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 359 | // |
| 360 | // FIXME: This is an oversimplification, they should only be related if they |
| 361 | // intersect, however we don't have that information. |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 362 | assert(isUserClass() && RHS.isUserClass() && "Unexpected class!"); |
| 363 | const ClassInfo *Root = this; |
| 364 | while (!Root->SuperClasses.empty()) |
| 365 | Root = Root->SuperClasses.front(); |
| 366 | |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 367 | const ClassInfo *RHSRoot = &RHS; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 368 | while (!RHSRoot->SuperClasses.empty()) |
| 369 | RHSRoot = RHSRoot->SuperClasses.front(); |
| 370 | |
| 371 | return Root == RHSRoot; |
| 372 | } |
| 373 | |
| 374 | /// isSubsetOf - Test whether this class is a subset of \arg RHS; |
| 375 | bool isSubsetOf(const ClassInfo &RHS) const { |
| 376 | // This is a subset of RHS if it is the same class... |
| 377 | if (this == &RHS) |
| 378 | return true; |
| 379 | |
| 380 | // ... or if any of its super classes are a subset of RHS. |
| 381 | for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(), |
| 382 | ie = SuperClasses.end(); it != ie; ++it) |
| 383 | if ((*it)->isSubsetOf(RHS)) |
| 384 | return true; |
| 385 | |
| 386 | return false; |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 389 | /// operator< - Compare two classes. |
| 390 | bool operator<(const ClassInfo &RHS) const { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 391 | // Unrelated classes can be ordered by kind. |
| 392 | if (!isRelatedTo(RHS)) |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 393 | return Kind < RHS.Kind; |
| 394 | |
| 395 | switch (Kind) { |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 396 | case Invalid: |
| 397 | assert(0 && "Invalid kind!"); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 398 | case Token: |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 399 | // Tokens are comparable by value. |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 400 | // |
| 401 | // FIXME: Compare by enum value. |
| 402 | return ValueName < RHS.ValueName; |
| 403 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 404 | default: |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 405 | // This class preceeds the RHS if it is a proper subset of the RHS. |
| 406 | return this != &RHS && isSubsetOf(RHS); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 411 | /// InstructionInfo - Helper class for storing the necessary information for an |
| 412 | /// instruction which is capable of being matched. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 413 | struct InstructionInfo { |
| 414 | struct Operand { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 415 | /// The unique class instance this operand should match. |
| 416 | ClassInfo *Class; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 417 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 418 | /// The original operand this corresponds to, if any. |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 419 | const CodeGenInstruction::OperandInfo *OperandInfo; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | /// InstrName - The target name for this instruction. |
| 423 | std::string InstrName; |
| 424 | |
| 425 | /// Instr - The instruction this matches. |
| 426 | const CodeGenInstruction *Instr; |
| 427 | |
| 428 | /// AsmString - The assembly string for this instruction (with variants |
| 429 | /// removed). |
| 430 | std::string AsmString; |
| 431 | |
| 432 | /// Tokens - The tokenized assembly pattern that this instruction matches. |
| 433 | SmallVector<StringRef, 4> Tokens; |
| 434 | |
| 435 | /// Operands - The operands that this instruction matches. |
| 436 | SmallVector<Operand, 4> Operands; |
| 437 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 438 | /// ConversionFnKind - The enum value which is passed to the generated |
| 439 | /// ConvertToMCInst to convert parsed operands into an MCInst for this |
| 440 | /// function. |
| 441 | std::string ConversionFnKind; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 442 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 443 | /// operator< - Compare two instructions. |
| 444 | bool operator<(const InstructionInfo &RHS) const { |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 445 | if (Operands.size() != RHS.Operands.size()) |
| 446 | return Operands.size() < RHS.Operands.size(); |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 447 | |
Daniel Dunbar | db2ddb5 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 448 | // Compare lexicographically by operand. The matcher validates that other |
| 449 | // orderings wouldn't be ambiguous using \see CouldMatchAmiguouslyWith(). |
| 450 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 451 | if (*Operands[i].Class < *RHS.Operands[i].Class) |
| 452 | return true; |
Daniel Dunbar | db2ddb5 | 2009-08-09 08:23:23 +0000 | [diff] [blame] | 453 | if (*RHS.Operands[i].Class < *Operands[i].Class) |
| 454 | return false; |
| 455 | } |
| 456 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 460 | /// CouldMatchAmiguouslyWith - Check whether this instruction could |
| 461 | /// ambiguously match the same set of operands as \arg RHS (without being a |
| 462 | /// strictly superior match). |
| 463 | bool CouldMatchAmiguouslyWith(const InstructionInfo &RHS) { |
| 464 | // The number of operands is unambiguous. |
| 465 | if (Operands.size() != RHS.Operands.size()) |
| 466 | return false; |
| 467 | |
| 468 | // Tokens and operand kinds are unambiguous (assuming a correct target |
| 469 | // specific parser). |
| 470 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 471 | if (Operands[i].Class->Kind != RHS.Operands[i].Class->Kind || |
| 472 | Operands[i].Class->Kind == ClassInfo::Token) |
| 473 | if (*Operands[i].Class < *RHS.Operands[i].Class || |
| 474 | *RHS.Operands[i].Class < *Operands[i].Class) |
| 475 | return false; |
| 476 | |
| 477 | // Otherwise, this operand could commute if all operands are equivalent, or |
| 478 | // there is a pair of operands that compare less than and a pair that |
| 479 | // compare greater than. |
| 480 | bool HasLT = false, HasGT = false; |
| 481 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 482 | if (*Operands[i].Class < *RHS.Operands[i].Class) |
| 483 | HasLT = true; |
| 484 | if (*RHS.Operands[i].Class < *Operands[i].Class) |
| 485 | HasGT = true; |
| 486 | } |
| 487 | |
| 488 | return !(HasLT ^ HasGT); |
| 489 | } |
| 490 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 491 | public: |
| 492 | void dump(); |
| 493 | }; |
| 494 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 495 | class AsmMatcherInfo { |
| 496 | public: |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 497 | /// The tablegen AsmParser record. |
| 498 | Record *AsmParser; |
| 499 | |
| 500 | /// The AsmParser "CommentDelimiter" value. |
| 501 | std::string CommentDelimiter; |
| 502 | |
| 503 | /// The AsmParser "RegisterPrefix" value. |
| 504 | std::string RegisterPrefix; |
| 505 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 506 | /// The classes which are needed for matching. |
| 507 | std::vector<ClassInfo*> Classes; |
| 508 | |
| 509 | /// The information on the instruction to match. |
| 510 | std::vector<InstructionInfo*> Instructions; |
| 511 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 512 | /// Map of Register records to their class information. |
| 513 | std::map<Record*, ClassInfo*> RegisterClasses; |
| 514 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 515 | private: |
| 516 | /// Map of token to class information which has already been constructed. |
| 517 | std::map<std::string, ClassInfo*> TokenClasses; |
| 518 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 519 | /// Map of RegisterClass records to their class information. |
| 520 | std::map<Record*, ClassInfo*> RegisterClassClasses; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 521 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 522 | /// Map of AsmOperandClass records to their class information. |
| 523 | std::map<Record*, ClassInfo*> AsmOperandClasses; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 524 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 525 | private: |
| 526 | /// getTokenClass - Lookup or create the class for the given token. |
| 527 | ClassInfo *getTokenClass(const StringRef &Token); |
| 528 | |
| 529 | /// getOperandClass - Lookup or create the class for the given operand. |
| 530 | ClassInfo *getOperandClass(const StringRef &Token, |
| 531 | const CodeGenInstruction::OperandInfo &OI); |
| 532 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 533 | /// BuildRegisterClasses - Build the ClassInfo* instances for register |
| 534 | /// classes. |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 535 | void BuildRegisterClasses(CodeGenTarget &Target, |
| 536 | std::set<std::string> &SingletonRegisterNames); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 537 | |
| 538 | /// BuildOperandClasses - Build the ClassInfo* instances for user defined |
| 539 | /// operand classes. |
| 540 | void BuildOperandClasses(CodeGenTarget &Target); |
| 541 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 542 | public: |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 543 | AsmMatcherInfo(Record *_AsmParser); |
| 544 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 545 | /// BuildInfo - Construct the various tables used during matching. |
| 546 | void BuildInfo(CodeGenTarget &Target); |
| 547 | }; |
| 548 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | void InstructionInfo::dump() { |
| 552 | errs() << InstrName << " -- " << "flattened:\"" << AsmString << '\"' |
| 553 | << ", tokens:["; |
| 554 | for (unsigned i = 0, e = Tokens.size(); i != e; ++i) { |
| 555 | errs() << Tokens[i]; |
| 556 | if (i + 1 != e) |
| 557 | errs() << ", "; |
| 558 | } |
| 559 | errs() << "]\n"; |
| 560 | |
| 561 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 562 | Operand &Op = Operands[i]; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 563 | errs() << " op[" << i << "] = " << Op.Class->ClassName << " - "; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 564 | if (Op.Class->Kind == ClassInfo::Token) { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 565 | errs() << '\"' << Tokens[i] << "\"\n"; |
| 566 | continue; |
| 567 | } |
| 568 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 569 | if (!Op.OperandInfo) { |
| 570 | errs() << "(singleton register)\n"; |
| 571 | continue; |
| 572 | } |
| 573 | |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 574 | const CodeGenInstruction::OperandInfo &OI = *Op.OperandInfo; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 575 | errs() << OI.Name << " " << OI.Rec->getName() |
| 576 | << " (" << OI.MIOperandNo << ", " << OI.MINumOperands << ")\n"; |
| 577 | } |
| 578 | } |
| 579 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 580 | static std::string getEnumNameForToken(const StringRef &Str) { |
| 581 | std::string Res; |
| 582 | |
| 583 | for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) { |
| 584 | switch (*it) { |
| 585 | case '*': Res += "_STAR_"; break; |
| 586 | case '%': Res += "_PCT_"; break; |
| 587 | case ':': Res += "_COLON_"; break; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 588 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 589 | default: |
| 590 | if (isalnum(*it)) { |
| 591 | Res += *it; |
| 592 | } else { |
| 593 | Res += "_" + utostr((unsigned) *it) + "_"; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | return Res; |
| 599 | } |
| 600 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 601 | /// getRegisterRecord - Get the register record for \arg name, or 0. |
| 602 | static Record *getRegisterRecord(CodeGenTarget &Target, const StringRef &Name) { |
| 603 | for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) { |
| 604 | const CodeGenRegister &Reg = Target.getRegisters()[i]; |
| 605 | if (Name == Reg.TheDef->getValueAsString("AsmName")) |
| 606 | return Reg.TheDef; |
| 607 | } |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 612 | ClassInfo *AsmMatcherInfo::getTokenClass(const StringRef &Token) { |
| 613 | ClassInfo *&Entry = TokenClasses[Token]; |
| 614 | |
| 615 | if (!Entry) { |
| 616 | Entry = new ClassInfo(); |
| 617 | Entry->Kind = ClassInfo::Token; |
Daniel Dunbar | 6745d42 | 2009-08-09 05:18:30 +0000 | [diff] [blame] | 618 | Entry->ClassName = "Token"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 619 | Entry->Name = "MCK_" + getEnumNameForToken(Token); |
| 620 | Entry->ValueName = Token; |
| 621 | Entry->PredicateMethod = "<invalid>"; |
| 622 | Entry->RenderMethod = "<invalid>"; |
| 623 | Classes.push_back(Entry); |
| 624 | } |
| 625 | |
| 626 | return Entry; |
| 627 | } |
| 628 | |
| 629 | ClassInfo * |
| 630 | AsmMatcherInfo::getOperandClass(const StringRef &Token, |
| 631 | const CodeGenInstruction::OperandInfo &OI) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 632 | if (OI.Rec->isSubClassOf("RegisterClass")) { |
| 633 | ClassInfo *CI = RegisterClassClasses[OI.Rec]; |
| 634 | |
| 635 | if (!CI) { |
| 636 | PrintError(OI.Rec->getLoc(), "register class has no class info!"); |
| 637 | throw std::string("ERROR: Missing register class!"); |
| 638 | } |
| 639 | |
| 640 | return CI; |
| 641 | } |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 642 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 643 | assert(OI.Rec->isSubClassOf("Operand") && "Unexpected operand!"); |
| 644 | Record *MatchClass = OI.Rec->getValueAsDef("ParserMatchClass"); |
| 645 | ClassInfo *CI = AsmOperandClasses[MatchClass]; |
| 646 | |
| 647 | if (!CI) { |
| 648 | PrintError(OI.Rec->getLoc(), "operand has no match class!"); |
| 649 | throw std::string("ERROR: Missing match class!"); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 652 | return CI; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 655 | void AsmMatcherInfo::BuildRegisterClasses(CodeGenTarget &Target, |
| 656 | std::set<std::string> |
| 657 | &SingletonRegisterNames) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 658 | std::vector<CodeGenRegisterClass> RegisterClasses; |
| 659 | std::vector<CodeGenRegister> Registers; |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 660 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 661 | RegisterClasses = Target.getRegisterClasses(); |
| 662 | Registers = Target.getRegisters(); |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 663 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 664 | // The register sets used for matching. |
| 665 | std::set< std::set<Record*> > RegisterSets; |
| 666 | |
| 667 | // Gather the defined sets. |
| 668 | for (std::vector<CodeGenRegisterClass>::iterator it = RegisterClasses.begin(), |
| 669 | ie = RegisterClasses.end(); it != ie; ++it) |
| 670 | RegisterSets.insert(std::set<Record*>(it->Elements.begin(), |
| 671 | it->Elements.end())); |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 672 | |
| 673 | // Add any required singleton sets. |
| 674 | for (std::set<std::string>::iterator it = SingletonRegisterNames.begin(), |
| 675 | ie = SingletonRegisterNames.end(); it != ie; ++it) |
| 676 | if (Record *Rec = getRegisterRecord(Target, *it)) |
| 677 | RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1)); |
| 678 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 679 | // Introduce derived sets where necessary (when a register does not determine |
| 680 | // a unique register set class), and build the mapping of registers to the set |
| 681 | // they should classify to. |
| 682 | std::map<Record*, std::set<Record*> > RegisterMap; |
| 683 | for (std::vector<CodeGenRegister>::iterator it = Registers.begin(), |
| 684 | ie = Registers.end(); it != ie; ++it) { |
| 685 | CodeGenRegister &CGR = *it; |
| 686 | // Compute the intersection of all sets containing this register. |
| 687 | std::set<Record*> ContainingSet; |
| 688 | |
| 689 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 690 | ie = RegisterSets.end(); it != ie; ++it) { |
| 691 | if (!it->count(CGR.TheDef)) |
| 692 | continue; |
| 693 | |
| 694 | if (ContainingSet.empty()) { |
| 695 | ContainingSet = *it; |
| 696 | } else { |
| 697 | std::set<Record*> Tmp; |
| 698 | std::swap(Tmp, ContainingSet); |
| 699 | std::insert_iterator< std::set<Record*> > II(ContainingSet, |
| 700 | ContainingSet.begin()); |
| 701 | std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), |
| 702 | II); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | if (!ContainingSet.empty()) { |
| 707 | RegisterSets.insert(ContainingSet); |
| 708 | RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet)); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // Construct the register classes. |
| 713 | std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses; |
| 714 | unsigned Index = 0; |
| 715 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 716 | ie = RegisterSets.end(); it != ie; ++it, ++Index) { |
| 717 | ClassInfo *CI = new ClassInfo(); |
| 718 | CI->Kind = ClassInfo::RegisterClass0 + Index; |
| 719 | CI->ClassName = "Reg" + utostr(Index); |
| 720 | CI->Name = "MCK_Reg" + utostr(Index); |
| 721 | CI->ValueName = ""; |
| 722 | CI->PredicateMethod = ""; // unused |
| 723 | CI->RenderMethod = "addRegOperands"; |
Daniel Dunbar | 8409bfb | 2009-08-11 20:10:07 +0000 | [diff] [blame] | 724 | CI->Registers = *it; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 725 | Classes.push_back(CI); |
| 726 | RegisterSetClasses.insert(std::make_pair(*it, CI)); |
| 727 | } |
| 728 | |
| 729 | // Find the superclasses; we could compute only the subgroup lattice edges, |
| 730 | // but there isn't really a point. |
| 731 | for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(), |
| 732 | ie = RegisterSets.end(); it != ie; ++it) { |
| 733 | ClassInfo *CI = RegisterSetClasses[*it]; |
| 734 | for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(), |
| 735 | ie2 = RegisterSets.end(); it2 != ie2; ++it2) |
| 736 | if (*it != *it2 && |
| 737 | std::includes(it2->begin(), it2->end(), it->begin(), it->end())) |
| 738 | CI->SuperClasses.push_back(RegisterSetClasses[*it2]); |
| 739 | } |
| 740 | |
| 741 | // Name the register classes which correspond to a user defined RegisterClass. |
| 742 | for (std::vector<CodeGenRegisterClass>::iterator it = RegisterClasses.begin(), |
| 743 | ie = RegisterClasses.end(); it != ie; ++it) { |
| 744 | ClassInfo *CI = RegisterSetClasses[std::set<Record*>(it->Elements.begin(), |
| 745 | it->Elements.end())]; |
| 746 | if (CI->ValueName.empty()) { |
| 747 | CI->ClassName = it->getName(); |
| 748 | CI->Name = "MCK_" + it->getName(); |
| 749 | CI->ValueName = it->getName(); |
| 750 | } else |
| 751 | CI->ValueName = CI->ValueName + "," + it->getName(); |
| 752 | |
| 753 | RegisterClassClasses.insert(std::make_pair(it->TheDef, CI)); |
| 754 | } |
| 755 | |
| 756 | // Populate the map for individual registers. |
| 757 | for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(), |
| 758 | ie = RegisterMap.end(); it != ie; ++it) |
| 759 | this->RegisterClasses[it->first] = RegisterSetClasses[it->second]; |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 760 | |
| 761 | // Name the register classes which correspond to singleton registers. |
| 762 | for (std::set<std::string>::iterator it = SingletonRegisterNames.begin(), |
| 763 | ie = SingletonRegisterNames.end(); it != ie; ++it) { |
| 764 | if (Record *Rec = getRegisterRecord(Target, *it)) { |
| 765 | ClassInfo *CI = this->RegisterClasses[Rec]; |
| 766 | assert(CI && "Missing singleton register class info!"); |
| 767 | |
| 768 | if (CI->ValueName.empty()) { |
| 769 | CI->ClassName = Rec->getName(); |
| 770 | CI->Name = "MCK_" + Rec->getName(); |
| 771 | CI->ValueName = Rec->getName(); |
| 772 | } else |
| 773 | CI->ValueName = CI->ValueName + "," + Rec->getName(); |
| 774 | } |
| 775 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | void AsmMatcherInfo::BuildOperandClasses(CodeGenTarget &Target) { |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 779 | std::vector<Record*> AsmOperands; |
| 780 | AsmOperands = Records.getAllDerivedDefinitions("AsmOperandClass"); |
| 781 | unsigned Index = 0; |
| 782 | for (std::vector<Record*>::iterator it = AsmOperands.begin(), |
| 783 | ie = AsmOperands.end(); it != ie; ++it, ++Index) { |
| 784 | ClassInfo *CI = new ClassInfo(); |
| 785 | CI->Kind = ClassInfo::UserClass0 + Index; |
| 786 | |
| 787 | Init *Super = (*it)->getValueInit("SuperClass"); |
| 788 | if (DefInit *DI = dynamic_cast<DefInit*>(Super)) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 789 | ClassInfo *SC = AsmOperandClasses[DI->getDef()]; |
| 790 | if (!SC) |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 791 | PrintError((*it)->getLoc(), "Invalid super class reference!"); |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 792 | else |
| 793 | CI->SuperClasses.push_back(SC); |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 794 | } else { |
| 795 | assert(dynamic_cast<UnsetInit*>(Super) && "Unexpected SuperClass field!"); |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 796 | } |
| 797 | CI->ClassName = (*it)->getValueAsString("Name"); |
| 798 | CI->Name = "MCK_" + CI->ClassName; |
| 799 | CI->ValueName = (*it)->getName(); |
Daniel Dunbar | 5c468e3 | 2009-08-10 21:00:45 +0000 | [diff] [blame] | 800 | |
| 801 | // Get or construct the predicate method name. |
| 802 | Init *PMName = (*it)->getValueInit("PredicateMethod"); |
| 803 | if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) { |
| 804 | CI->PredicateMethod = SI->getValue(); |
| 805 | } else { |
| 806 | assert(dynamic_cast<UnsetInit*>(PMName) && |
| 807 | "Unexpected PredicateMethod field!"); |
| 808 | CI->PredicateMethod = "is" + CI->ClassName; |
| 809 | } |
| 810 | |
| 811 | // Get or construct the render method name. |
| 812 | Init *RMName = (*it)->getValueInit("RenderMethod"); |
| 813 | if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) { |
| 814 | CI->RenderMethod = SI->getValue(); |
| 815 | } else { |
| 816 | assert(dynamic_cast<UnsetInit*>(RMName) && |
| 817 | "Unexpected RenderMethod field!"); |
| 818 | CI->RenderMethod = "add" + CI->ClassName + "Operands"; |
| 819 | } |
| 820 | |
Daniel Dunbar | 338825c | 2009-08-10 18:41:10 +0000 | [diff] [blame] | 821 | AsmOperandClasses[*it] = CI; |
| 822 | Classes.push_back(CI); |
| 823 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 826 | AsmMatcherInfo::AsmMatcherInfo(Record *_AsmParser) |
| 827 | : AsmParser(_AsmParser), |
| 828 | CommentDelimiter(AsmParser->getValueAsString("CommentDelimiter")), |
| 829 | RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")) |
| 830 | { |
| 831 | } |
| 832 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 833 | void AsmMatcherInfo::BuildInfo(CodeGenTarget &Target) { |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 834 | // Parse the instructions; we need to do this first so that we can gather the |
| 835 | // singleton register classes. |
| 836 | std::set<std::string> SingletonRegisterNames; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 837 | for (std::map<std::string, CodeGenInstruction>::const_iterator |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 838 | it = Target.getInstructions().begin(), |
| 839 | ie = Target.getInstructions().end(); |
| 840 | it != ie; ++it) { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 841 | const CodeGenInstruction &CGI = it->second; |
| 842 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 843 | if (!StringRef(it->first).startswith(MatchPrefix)) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 844 | continue; |
| 845 | |
| 846 | OwningPtr<InstructionInfo> II(new InstructionInfo); |
| 847 | |
| 848 | II->InstrName = it->first; |
| 849 | II->Instr = &it->second; |
| 850 | II->AsmString = FlattenVariants(CGI.AsmString, 0); |
| 851 | |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 852 | // Remove comments from the asm string. |
| 853 | if (!CommentDelimiter.empty()) { |
| 854 | size_t Idx = StringRef(II->AsmString).find(CommentDelimiter); |
| 855 | if (Idx != StringRef::npos) |
| 856 | II->AsmString = II->AsmString.substr(0, Idx); |
| 857 | } |
| 858 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 859 | TokenizeAsmString(II->AsmString, II->Tokens); |
| 860 | |
| 861 | // Ignore instructions which shouldn't be matched. |
| 862 | if (!IsAssemblerInstruction(it->first, CGI, II->Tokens)) |
| 863 | continue; |
| 864 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 865 | // Collect singleton registers, if used. |
| 866 | if (!RegisterPrefix.empty()) { |
| 867 | for (unsigned i = 0, e = II->Tokens.size(); i != e; ++i) { |
| 868 | if (II->Tokens[i].startswith(RegisterPrefix)) { |
| 869 | StringRef RegName = II->Tokens[i].substr(RegisterPrefix.size()); |
| 870 | Record *Rec = getRegisterRecord(Target, RegName); |
| 871 | |
| 872 | if (!Rec) { |
| 873 | std::string Err = "unable to find register for '" + RegName.str() + |
| 874 | "' (which matches register prefix)"; |
| 875 | throw TGError(CGI.TheDef->getLoc(), Err); |
| 876 | } |
| 877 | |
| 878 | SingletonRegisterNames.insert(RegName); |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | Instructions.push_back(II.take()); |
| 884 | } |
| 885 | |
| 886 | // Build info for the register classes. |
| 887 | BuildRegisterClasses(Target, SingletonRegisterNames); |
| 888 | |
| 889 | // Build info for the user defined assembly operand classes. |
| 890 | BuildOperandClasses(Target); |
| 891 | |
| 892 | // Build the instruction information. |
| 893 | for (std::vector<InstructionInfo*>::iterator it = Instructions.begin(), |
| 894 | ie = Instructions.end(); it != ie; ++it) { |
| 895 | InstructionInfo *II = *it; |
| 896 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 897 | for (unsigned i = 0, e = II->Tokens.size(); i != e; ++i) { |
| 898 | StringRef Token = II->Tokens[i]; |
| 899 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 900 | // Check for singleton registers. |
| 901 | if (!RegisterPrefix.empty() && Token.startswith(RegisterPrefix)) { |
| 902 | StringRef RegName = II->Tokens[i].substr(RegisterPrefix.size()); |
| 903 | InstructionInfo::Operand Op; |
| 904 | Op.Class = RegisterClasses[getRegisterRecord(Target, RegName)]; |
| 905 | Op.OperandInfo = 0; |
| 906 | assert(Op.Class && Op.Class->Registers.size() == 1 && |
| 907 | "Unexpected class for singleton register"); |
| 908 | II->Operands.push_back(Op); |
| 909 | continue; |
| 910 | } |
| 911 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 912 | // Check for simple tokens. |
| 913 | if (Token[0] != '$') { |
| 914 | InstructionInfo::Operand Op; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 915 | Op.Class = getTokenClass(Token); |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 916 | Op.OperandInfo = 0; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 917 | II->Operands.push_back(Op); |
| 918 | continue; |
| 919 | } |
| 920 | |
| 921 | // Otherwise this is an operand reference. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 922 | StringRef OperandName; |
| 923 | if (Token[1] == '{') |
| 924 | OperandName = Token.substr(2, Token.size() - 3); |
| 925 | else |
| 926 | OperandName = Token.substr(1); |
| 927 | |
| 928 | // Map this token to an operand. FIXME: Move elsewhere. |
| 929 | unsigned Idx; |
| 930 | try { |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 931 | Idx = II->Instr->getOperandNamed(OperandName); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 932 | } catch(...) { |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 933 | throw std::string("error: unable to find operand: '" + |
| 934 | OperandName.str() + "'"); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 937 | const CodeGenInstruction::OperandInfo &OI = II->Instr->OperandList[Idx]; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 938 | InstructionInfo::Operand Op; |
| 939 | Op.Class = getOperandClass(Token, OI); |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 940 | Op.OperandInfo = &OI; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 941 | II->Operands.push_back(Op); |
| 942 | } |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 943 | } |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 944 | |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 945 | // Reorder classes so that classes preceed super classes. |
| 946 | std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>()); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 949 | static void EmitConvertToMCInst(CodeGenTarget &Target, |
| 950 | std::vector<InstructionInfo*> &Infos, |
| 951 | raw_ostream &OS) { |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 952 | // Write the convert function to a separate stream, so we can drop it after |
| 953 | // the enum. |
| 954 | std::string ConvertFnBody; |
| 955 | raw_string_ostream CvtOS(ConvertFnBody); |
| 956 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 957 | // Function we have already generated. |
| 958 | std::set<std::string> GeneratedFns; |
| 959 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 960 | // Start the unified conversion function. |
| 961 | |
| 962 | CvtOS << "static bool ConvertToMCInst(ConversionKind Kind, MCInst &Inst, " |
| 963 | << "unsigned Opcode,\n" |
| 964 | << " SmallVectorImpl<" |
| 965 | << Target.getName() << "Operand> &Operands) {\n"; |
| 966 | CvtOS << " Inst.setOpcode(Opcode);\n"; |
| 967 | CvtOS << " switch (Kind) {\n"; |
| 968 | CvtOS << " default:\n"; |
| 969 | |
| 970 | // Start the enum, which we will generate inline. |
| 971 | |
| 972 | OS << "// Unified function for converting operants to MCInst instances.\n\n"; |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 973 | OS << "enum ConversionKind {\n"; |
| 974 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 975 | for (std::vector<InstructionInfo*>::const_iterator it = Infos.begin(), |
| 976 | ie = Infos.end(); it != ie; ++it) { |
| 977 | InstructionInfo &II = **it; |
| 978 | |
| 979 | // Order the (class) operands by the order to convert them into an MCInst. |
| 980 | SmallVector<std::pair<unsigned, unsigned>, 4> MIOperandList; |
| 981 | for (unsigned i = 0, e = II.Operands.size(); i != e; ++i) { |
| 982 | InstructionInfo::Operand &Op = II.Operands[i]; |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 983 | if (Op.OperandInfo) |
| 984 | MIOperandList.push_back(std::make_pair(Op.OperandInfo->MIOperandNo, i)); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 985 | } |
| 986 | std::sort(MIOperandList.begin(), MIOperandList.end()); |
| 987 | |
| 988 | // Compute the total number of operands. |
| 989 | unsigned NumMIOperands = 0; |
| 990 | for (unsigned i = 0, e = II.Instr->OperandList.size(); i != e; ++i) { |
| 991 | const CodeGenInstruction::OperandInfo &OI = II.Instr->OperandList[i]; |
| 992 | NumMIOperands = std::max(NumMIOperands, |
| 993 | OI.MIOperandNo + OI.MINumOperands); |
| 994 | } |
| 995 | |
| 996 | // Build the conversion function signature. |
| 997 | std::string Signature = "Convert"; |
| 998 | unsigned CurIndex = 0; |
| 999 | for (unsigned i = 0, e = MIOperandList.size(); i != e; ++i) { |
| 1000 | InstructionInfo::Operand &Op = II.Operands[MIOperandList[i].second]; |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1001 | assert(CurIndex <= Op.OperandInfo->MIOperandNo && |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1002 | "Duplicate match for instruction operand!"); |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1003 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1004 | Signature += "_"; |
| 1005 | |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1006 | // Skip operands which weren't matched by anything, this occurs when the |
| 1007 | // .td file encodes "implicit" operands as explicit ones. |
| 1008 | // |
| 1009 | // FIXME: This should be removed from the MCInst structure. |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1010 | for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1011 | Signature += "Imp"; |
| 1012 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1013 | // Registers are always converted the same, don't duplicate the conversion |
| 1014 | // function based on them. |
| 1015 | // |
| 1016 | // FIXME: We could generalize this based on the render method, if it |
| 1017 | // mattered. |
| 1018 | if (Op.Class->isRegisterClass()) |
| 1019 | Signature += "Reg"; |
| 1020 | else |
| 1021 | Signature += Op.Class->ClassName; |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1022 | Signature += utostr(Op.OperandInfo->MINumOperands); |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1023 | Signature += "_" + utostr(MIOperandList[i].second); |
| 1024 | |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1025 | CurIndex += Op.OperandInfo->MINumOperands; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | // Add any trailing implicit operands. |
| 1029 | for (; CurIndex != NumMIOperands; ++CurIndex) |
| 1030 | Signature += "Imp"; |
| 1031 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1032 | II.ConversionFnKind = Signature; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1033 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1034 | // Check if we have already generated this signature. |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1035 | if (!GeneratedFns.insert(Signature).second) |
| 1036 | continue; |
| 1037 | |
| 1038 | // If not, emit it now. |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1039 | |
| 1040 | // Add to the enum list. |
| 1041 | OS << " " << Signature << ",\n"; |
| 1042 | |
| 1043 | // And to the convert function. |
| 1044 | CvtOS << " case " << Signature << ":\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1045 | CurIndex = 0; |
| 1046 | for (unsigned i = 0, e = MIOperandList.size(); i != e; ++i) { |
| 1047 | InstructionInfo::Operand &Op = II.Operands[MIOperandList[i].second]; |
| 1048 | |
| 1049 | // Add the implicit operands. |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1050 | for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex) |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1051 | CvtOS << " Inst.addOperand(MCOperand::CreateReg(0));\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1052 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1053 | CvtOS << " Operands[" << MIOperandList[i].second |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1054 | << "]." << Op.Class->RenderMethod |
Benjamin Kramer | fa1165a | 2009-08-08 10:06:30 +0000 | [diff] [blame] | 1055 | << "(Inst, " << Op.OperandInfo->MINumOperands << ");\n"; |
| 1056 | CurIndex += Op.OperandInfo->MINumOperands; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | // And add trailing implicit operands. |
| 1060 | for (; CurIndex != NumMIOperands; ++CurIndex) |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1061 | CvtOS << " Inst.addOperand(MCOperand::CreateReg(0));\n"; |
| 1062 | CvtOS << " break;\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1063 | } |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1064 | |
| 1065 | // Finish the convert function. |
| 1066 | |
| 1067 | CvtOS << " }\n"; |
| 1068 | CvtOS << " return false;\n"; |
| 1069 | CvtOS << "}\n\n"; |
| 1070 | |
| 1071 | // Finish the enum, and drop the convert function after it. |
| 1072 | |
| 1073 | OS << " NumConversionVariants\n"; |
| 1074 | OS << "};\n\n"; |
| 1075 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1076 | OS << CvtOS.str(); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1079 | /// EmitMatchClassEnumeration - Emit the enumeration for match class kinds. |
| 1080 | static void EmitMatchClassEnumeration(CodeGenTarget &Target, |
| 1081 | std::vector<ClassInfo*> &Infos, |
| 1082 | raw_ostream &OS) { |
| 1083 | OS << "namespace {\n\n"; |
| 1084 | |
| 1085 | OS << "/// MatchClassKind - The kinds of classes which participate in\n" |
| 1086 | << "/// instruction matching.\n"; |
| 1087 | OS << "enum MatchClassKind {\n"; |
| 1088 | OS << " InvalidMatchClass = 0,\n"; |
| 1089 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
| 1090 | ie = Infos.end(); it != ie; ++it) { |
| 1091 | ClassInfo &CI = **it; |
| 1092 | OS << " " << CI.Name << ", // "; |
| 1093 | if (CI.Kind == ClassInfo::Token) { |
| 1094 | OS << "'" << CI.ValueName << "'\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1095 | } else if (CI.isRegisterClass()) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1096 | if (!CI.ValueName.empty()) |
| 1097 | OS << "register class '" << CI.ValueName << "'\n"; |
| 1098 | else |
| 1099 | OS << "derived register class\n"; |
| 1100 | } else { |
| 1101 | OS << "user defined class '" << CI.ValueName << "'\n"; |
| 1102 | } |
| 1103 | } |
| 1104 | OS << " NumMatchClassKinds\n"; |
| 1105 | OS << "};\n\n"; |
| 1106 | |
| 1107 | OS << "}\n\n"; |
| 1108 | } |
| 1109 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1110 | /// EmitClassifyOperand - Emit the function to classify an operand. |
| 1111 | static void EmitClassifyOperand(CodeGenTarget &Target, |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1112 | AsmMatcherInfo &Info, |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1113 | raw_ostream &OS) { |
| 1114 | OS << "static MatchClassKind ClassifyOperand(" |
| 1115 | << Target.getName() << "Operand &Operand) {\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1116 | |
| 1117 | // Classify tokens. |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1118 | OS << " if (Operand.isToken())\n"; |
| 1119 | OS << " return MatchTokenString(Operand.getToken());\n\n"; |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1120 | |
| 1121 | // Classify registers. |
| 1122 | // |
| 1123 | // FIXME: Don't hardcode isReg, getReg. |
| 1124 | OS << " if (Operand.isReg()) {\n"; |
| 1125 | OS << " switch (Operand.getReg()) {\n"; |
| 1126 | OS << " default: return InvalidMatchClass;\n"; |
| 1127 | for (std::map<Record*, ClassInfo*>::iterator |
| 1128 | it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end(); |
| 1129 | it != ie; ++it) |
| 1130 | OS << " case " << Target.getName() << "::" |
| 1131 | << it->first->getName() << ": return " << it->second->Name << ";\n"; |
| 1132 | OS << " }\n"; |
| 1133 | OS << " }\n\n"; |
| 1134 | |
| 1135 | // Classify user defined operands. |
| 1136 | for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(), |
| 1137 | ie = Info.Classes.end(); it != ie; ++it) { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1138 | ClassInfo &CI = **it; |
| 1139 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1140 | if (!CI.isUserClass()) |
| 1141 | continue; |
| 1142 | |
| 1143 | OS << " // '" << CI.ClassName << "' class"; |
| 1144 | if (!CI.SuperClasses.empty()) { |
| 1145 | OS << ", subclass of "; |
| 1146 | for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) { |
| 1147 | if (i) OS << ", "; |
| 1148 | OS << "'" << CI.SuperClasses[i]->ClassName << "'"; |
| 1149 | assert(CI < *CI.SuperClasses[i] && "Invalid class relation!"); |
Daniel Dunbar | 5fe6338 | 2009-08-09 07:20:21 +0000 | [diff] [blame] | 1150 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1151 | } |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1152 | OS << "\n"; |
| 1153 | |
| 1154 | OS << " if (Operand." << CI.PredicateMethod << "()) {\n"; |
| 1155 | |
| 1156 | // Validate subclass relationships. |
| 1157 | if (!CI.SuperClasses.empty()) { |
| 1158 | for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) |
| 1159 | OS << " assert(Operand." << CI.SuperClasses[i]->PredicateMethod |
| 1160 | << "() && \"Invalid class relationship!\");\n"; |
| 1161 | } |
| 1162 | |
| 1163 | OS << " return " << CI.Name << ";\n"; |
| 1164 | OS << " }\n\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1165 | } |
| 1166 | OS << " return InvalidMatchClass;\n"; |
| 1167 | OS << "}\n\n"; |
| 1168 | } |
| 1169 | |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1170 | /// EmitIsSubclass - Emit the subclass predicate function. |
| 1171 | static void EmitIsSubclass(CodeGenTarget &Target, |
| 1172 | std::vector<ClassInfo*> &Infos, |
| 1173 | raw_ostream &OS) { |
| 1174 | OS << "/// IsSubclass - Compute whether \\arg A is a subclass of \\arg B.\n"; |
| 1175 | OS << "static bool IsSubclass(MatchClassKind A, MatchClassKind B) {\n"; |
| 1176 | OS << " if (A == B)\n"; |
| 1177 | OS << " return true;\n\n"; |
| 1178 | |
| 1179 | OS << " switch (A) {\n"; |
| 1180 | OS << " default:\n"; |
| 1181 | OS << " return false;\n"; |
| 1182 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
| 1183 | ie = Infos.end(); it != ie; ++it) { |
| 1184 | ClassInfo &A = **it; |
| 1185 | |
| 1186 | if (A.Kind != ClassInfo::Token) { |
| 1187 | std::vector<StringRef> SuperClasses; |
| 1188 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
| 1189 | ie = Infos.end(); it != ie; ++it) { |
| 1190 | ClassInfo &B = **it; |
| 1191 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1192 | if (&A != &B && A.isSubsetOf(B)) |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1193 | SuperClasses.push_back(B.Name); |
| 1194 | } |
| 1195 | |
| 1196 | if (SuperClasses.empty()) |
| 1197 | continue; |
| 1198 | |
| 1199 | OS << "\n case " << A.Name << ":\n"; |
| 1200 | |
| 1201 | if (SuperClasses.size() == 1) { |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1202 | OS << " return B == " << SuperClasses.back() << ";\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1203 | continue; |
| 1204 | } |
| 1205 | |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1206 | OS << " switch (B) {\n"; |
| 1207 | OS << " default: return false;\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1208 | for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1209 | OS << " case " << SuperClasses[i] << ": return true;\n"; |
| 1210 | OS << " }\n"; |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | OS << " }\n"; |
| 1214 | OS << "}\n\n"; |
| 1215 | } |
| 1216 | |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1217 | typedef std::pair<std::string, std::string> StringPair; |
| 1218 | |
| 1219 | /// FindFirstNonCommonLetter - Find the first character in the keys of the |
| 1220 | /// string pairs that is not shared across the whole set of strings. All |
| 1221 | /// strings are assumed to have the same length. |
| 1222 | static unsigned |
| 1223 | FindFirstNonCommonLetter(const std::vector<const StringPair*> &Matches) { |
| 1224 | assert(!Matches.empty()); |
| 1225 | for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) { |
| 1226 | // Check to see if letter i is the same across the set. |
| 1227 | char Letter = Matches[0]->first[i]; |
| 1228 | |
| 1229 | for (unsigned str = 0, e = Matches.size(); str != e; ++str) |
| 1230 | if (Matches[str]->first[i] != Letter) |
| 1231 | return i; |
| 1232 | } |
| 1233 | |
| 1234 | return Matches[0]->first.size(); |
| 1235 | } |
| 1236 | |
| 1237 | /// EmitStringMatcherForChar - Given a set of strings that are known to be the |
| 1238 | /// same length and whose characters leading up to CharNo are the same, emit |
| 1239 | /// code to verify that CharNo and later are the same. |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1240 | /// |
| 1241 | /// \return - True if control can leave the emitted code fragment. |
| 1242 | static bool EmitStringMatcherForChar(const std::string &StrVariableName, |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1243 | const std::vector<const StringPair*> &Matches, |
| 1244 | unsigned CharNo, unsigned IndentCount, |
| 1245 | raw_ostream &OS) { |
| 1246 | assert(!Matches.empty() && "Must have at least one string to match!"); |
| 1247 | std::string Indent(IndentCount*2+4, ' '); |
| 1248 | |
| 1249 | // If we have verified that the entire string matches, we're done: output the |
| 1250 | // matching code. |
| 1251 | if (CharNo == Matches[0]->first.size()) { |
| 1252 | assert(Matches.size() == 1 && "Had duplicate keys to match on"); |
| 1253 | |
| 1254 | // FIXME: If Matches[0].first has embeded \n, this will be bad. |
| 1255 | OS << Indent << Matches[0]->second << "\t // \"" << Matches[0]->first |
| 1256 | << "\"\n"; |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1257 | return false; |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | // Bucket the matches by the character we are comparing. |
| 1261 | std::map<char, std::vector<const StringPair*> > MatchesByLetter; |
| 1262 | |
| 1263 | for (unsigned i = 0, e = Matches.size(); i != e; ++i) |
| 1264 | MatchesByLetter[Matches[i]->first[CharNo]].push_back(Matches[i]); |
| 1265 | |
| 1266 | |
| 1267 | // If we have exactly one bucket to match, see how many characters are common |
| 1268 | // across the whole set and match all of them at once. |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1269 | if (MatchesByLetter.size() == 1) { |
| 1270 | unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches); |
| 1271 | unsigned NumChars = FirstNonCommonLetter-CharNo; |
| 1272 | |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1273 | // Emit code to break out if the prefix doesn't match. |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1274 | if (NumChars == 1) { |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1275 | // Do the comparison with if (Str[1] != 'f') |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1276 | // FIXME: Need to escape general characters. |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1277 | OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '" |
| 1278 | << Matches[0]->first[CharNo] << "')\n"; |
| 1279 | OS << Indent << " break;\n"; |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1280 | } else { |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1281 | // Do the comparison with if (Str.substr(1,3) != "foo"). |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1282 | // FIXME: Need to escape general strings. |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1283 | OS << Indent << "if (" << StrVariableName << ".substr(" << CharNo << "," |
| 1284 | << NumChars << ") != \""; |
| 1285 | OS << Matches[0]->first.substr(CharNo, NumChars) << "\")\n"; |
Daniel Dunbar | af3e9d4 | 2009-08-08 23:43:16 +0000 | [diff] [blame] | 1286 | OS << Indent << " break;\n"; |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1289 | return EmitStringMatcherForChar(StrVariableName, Matches, |
| 1290 | FirstNonCommonLetter, IndentCount, OS); |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | // Otherwise, we have multiple possible things, emit a switch on the |
| 1294 | // character. |
| 1295 | OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n"; |
| 1296 | OS << Indent << "default: break;\n"; |
| 1297 | |
| 1298 | for (std::map<char, std::vector<const StringPair*> >::iterator LI = |
| 1299 | MatchesByLetter.begin(), E = MatchesByLetter.end(); LI != E; ++LI) { |
| 1300 | // TODO: escape hard stuff (like \n) if we ever care about it. |
| 1301 | OS << Indent << "case '" << LI->first << "':\t // " |
| 1302 | << LI->second.size() << " strings to match.\n"; |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1303 | if (EmitStringMatcherForChar(StrVariableName, LI->second, CharNo+1, |
| 1304 | IndentCount+1, OS)) |
| 1305 | OS << Indent << " break;\n"; |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | OS << Indent << "}\n"; |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1309 | return true; |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | /// EmitStringMatcher - Given a list of strings and code to execute when they |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1314 | /// match, output a simple switch tree to classify the input string. |
| 1315 | /// |
| 1316 | /// If a match is found, the code in Vals[i].second is executed; control must |
| 1317 | /// not exit this code fragment. If nothing matches, execution falls through. |
| 1318 | /// |
| 1319 | /// \param StrVariableName - The name of the variable to test. |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1320 | static void EmitStringMatcher(const std::string &StrVariableName, |
| 1321 | const std::vector<StringPair> &Matches, |
| 1322 | raw_ostream &OS) { |
| 1323 | // First level categorization: group strings by length. |
| 1324 | std::map<unsigned, std::vector<const StringPair*> > MatchesByLength; |
| 1325 | |
| 1326 | for (unsigned i = 0, e = Matches.size(); i != e; ++i) |
| 1327 | MatchesByLength[Matches[i].first.size()].push_back(&Matches[i]); |
| 1328 | |
| 1329 | // Output a switch statement on length and categorize the elements within each |
| 1330 | // bin. |
| 1331 | OS << " switch (" << StrVariableName << ".size()) {\n"; |
| 1332 | OS << " default: break;\n"; |
| 1333 | |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1334 | for (std::map<unsigned, std::vector<const StringPair*> >::iterator LI = |
| 1335 | MatchesByLength.begin(), E = MatchesByLength.end(); LI != E; ++LI) { |
| 1336 | OS << " case " << LI->first << ":\t // " << LI->second.size() |
| 1337 | << " strings to match.\n"; |
Daniel Dunbar | 72ffae9 | 2009-08-08 22:57:25 +0000 | [diff] [blame] | 1338 | if (EmitStringMatcherForChar(StrVariableName, LI->second, 0, 0, OS)) |
| 1339 | OS << " break;\n"; |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1342 | OS << " }\n"; |
| 1343 | } |
| 1344 | |
| 1345 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1346 | /// EmitMatchTokenString - Emit the function to match a token string to the |
| 1347 | /// appropriate match class value. |
| 1348 | static void EmitMatchTokenString(CodeGenTarget &Target, |
| 1349 | std::vector<ClassInfo*> &Infos, |
| 1350 | raw_ostream &OS) { |
| 1351 | // Construct the match list. |
| 1352 | std::vector<StringPair> Matches; |
| 1353 | for (std::vector<ClassInfo*>::iterator it = Infos.begin(), |
| 1354 | ie = Infos.end(); it != ie; ++it) { |
| 1355 | ClassInfo &CI = **it; |
| 1356 | |
| 1357 | if (CI.Kind == ClassInfo::Token) |
| 1358 | Matches.push_back(StringPair(CI.ValueName, "return " + CI.Name + ";")); |
| 1359 | } |
| 1360 | |
| 1361 | OS << "static MatchClassKind MatchTokenString(const StringRef &Name) {\n"; |
| 1362 | |
| 1363 | EmitStringMatcher("Name", Matches, OS); |
| 1364 | |
| 1365 | OS << " return InvalidMatchClass;\n"; |
| 1366 | OS << "}\n\n"; |
| 1367 | } |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1368 | |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1369 | /// EmitMatchRegisterName - Emit the function to match a string to the target |
| 1370 | /// specific register enum. |
| 1371 | static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser, |
| 1372 | raw_ostream &OS) { |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1373 | // Construct the match list. |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1374 | std::vector<StringPair> Matches; |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1375 | for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) { |
| 1376 | const CodeGenRegister &Reg = Target.getRegisters()[i]; |
Daniel Dunbar | 22be522 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 1377 | if (Reg.TheDef->getValueAsString("AsmName").empty()) |
| 1378 | continue; |
| 1379 | |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1380 | Matches.push_back(StringPair(Reg.TheDef->getValueAsString("AsmName"), |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1381 | "return " + utostr(i + 1) + ";")); |
Daniel Dunbar | 22be522 | 2009-07-17 18:51:11 +0000 | [diff] [blame] | 1382 | } |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1383 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1384 | OS << "unsigned " << Target.getName() |
| 1385 | << AsmParser->getValueAsString("AsmParserClassName") |
| 1386 | << "::MatchRegisterName(const StringRef &Name) {\n"; |
| 1387 | |
Chris Lattner | 70add88 | 2009-08-08 20:02:57 +0000 | [diff] [blame] | 1388 | EmitStringMatcher("Name", Matches, OS); |
| 1389 | |
Daniel Dunbar | 245f058 | 2009-08-08 21:22:41 +0000 | [diff] [blame] | 1390 | OS << " return 0;\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1391 | OS << "}\n\n"; |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1392 | } |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1393 | |
Daniel Dunbar | 2234e5e | 2009-08-07 21:01:44 +0000 | [diff] [blame] | 1394 | void AsmMatcherEmitter::run(raw_ostream &OS) { |
| 1395 | CodeGenTarget Target; |
| 1396 | Record *AsmParser = Target.getAsmParser(); |
| 1397 | std::string ClassName = AsmParser->getValueAsString("AsmParserClassName"); |
| 1398 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1399 | // Compute the information on the instructions to match. |
Daniel Dunbar | 59fc42d | 2009-08-11 20:59:47 +0000 | [diff] [blame] | 1400 | AsmMatcherInfo Info(AsmParser); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1401 | Info.BuildInfo(Target); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1402 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1403 | // Sort the instruction table using the partial order on classes. |
| 1404 | std::sort(Info.Instructions.begin(), Info.Instructions.end(), |
| 1405 | less_ptr<InstructionInfo>()); |
| 1406 | |
Daniel Dunbar | b7479c0 | 2009-08-08 05:24:34 +0000 | [diff] [blame] | 1407 | DEBUG_WITH_TYPE("instruction_info", { |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1408 | for (std::vector<InstructionInfo*>::iterator |
| 1409 | it = Info.Instructions.begin(), ie = Info.Instructions.end(); |
| 1410 | it != ie; ++it) |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1411 | (*it)->dump(); |
| 1412 | }); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1413 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1414 | // Check for ambiguous instructions. |
| 1415 | unsigned NumAmbiguous = 0; |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 1416 | for (unsigned i = 0, e = Info.Instructions.size(); i != e; ++i) { |
| 1417 | for (unsigned j = i + 1; j != e; ++j) { |
| 1418 | InstructionInfo &A = *Info.Instructions[i]; |
| 1419 | InstructionInfo &B = *Info.Instructions[j]; |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1420 | |
Daniel Dunbar | 2b54481 | 2009-08-09 06:05:33 +0000 | [diff] [blame] | 1421 | if (A.CouldMatchAmiguouslyWith(B)) { |
| 1422 | DEBUG_WITH_TYPE("ambiguous_instrs", { |
| 1423 | errs() << "warning: ambiguous instruction match:\n"; |
| 1424 | A.dump(); |
| 1425 | errs() << "\nis incomparable with:\n"; |
| 1426 | B.dump(); |
| 1427 | errs() << "\n\n"; |
| 1428 | }); |
| 1429 | ++NumAmbiguous; |
| 1430 | } |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | if (NumAmbiguous) |
| 1434 | DEBUG_WITH_TYPE("ambiguous_instrs", { |
| 1435 | errs() << "warning: " << NumAmbiguous |
| 1436 | << " ambiguous instructions!\n"; |
| 1437 | }); |
| 1438 | |
Daniel Dunbar | 1095f2a | 2009-08-11 23:23:44 +0000 | [diff] [blame] | 1439 | // Write the output. |
| 1440 | |
| 1441 | EmitSourceFileHeader("Assembly Matcher Source Fragment", OS); |
| 1442 | |
| 1443 | // Emit the function to match a register name to number. |
| 1444 | EmitMatchRegisterName(Target, AsmParser, OS); |
| 1445 | |
Daniel Dunbar | 606e8ad | 2009-08-09 04:00:06 +0000 | [diff] [blame] | 1446 | // Generate the unified function to convert operands into an MCInst. |
| 1447 | EmitConvertToMCInst(Target, Info.Instructions, OS); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1448 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1449 | // Emit the enumeration for classes which participate in matching. |
| 1450 | EmitMatchClassEnumeration(Target, Info.Classes, OS); |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1451 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1452 | // Emit the routine to match token strings to their match class. |
| 1453 | EmitMatchTokenString(Target, Info.Classes, OS); |
| 1454 | |
| 1455 | // Emit the routine to classify an operand. |
Daniel Dunbar | ea6408f | 2009-08-11 02:59:53 +0000 | [diff] [blame] | 1456 | EmitClassifyOperand(Target, Info, OS); |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1457 | |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1458 | // Emit the subclass predicate routine. |
| 1459 | EmitIsSubclass(Target, Info.Classes, OS); |
| 1460 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1461 | // Finally, build the match function. |
| 1462 | |
| 1463 | size_t MaxNumOperands = 0; |
| 1464 | for (std::vector<InstructionInfo*>::const_iterator it = |
| 1465 | Info.Instructions.begin(), ie = Info.Instructions.end(); |
| 1466 | it != ie; ++it) |
| 1467 | MaxNumOperands = std::max(MaxNumOperands, (*it)->Operands.size()); |
| 1468 | |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1469 | OS << "bool " << Target.getName() << ClassName |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1470 | << "::MatchInstruction(" |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1471 | << "SmallVectorImpl<" << Target.getName() << "Operand> &Operands, " |
| 1472 | << "MCInst &Inst) {\n"; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1473 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1474 | // Emit the static match table; unused classes get initalized to 0 which is |
| 1475 | // guaranteed to be InvalidMatchClass. |
| 1476 | // |
| 1477 | // FIXME: We can reduce the size of this table very easily. First, we change |
| 1478 | // it so that store the kinds in separate bit-fields for each index, which |
| 1479 | // only needs to be the max width used for classes at that index (we also need |
| 1480 | // to reject based on this during classification). If we then make sure to |
| 1481 | // order the match kinds appropriately (putting mnemonics last), then we |
| 1482 | // should only end up using a few bits for each class, especially the ones |
| 1483 | // following the mnemonic. |
Chris Lattner | c604953 | 2009-08-08 19:15:25 +0000 | [diff] [blame] | 1484 | OS << " static const struct MatchEntry {\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1485 | OS << " unsigned Opcode;\n"; |
| 1486 | OS << " ConversionKind ConvertFn;\n"; |
| 1487 | OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n"; |
| 1488 | OS << " } MatchTable[" << Info.Instructions.size() << "] = {\n"; |
| 1489 | |
| 1490 | for (std::vector<InstructionInfo*>::const_iterator it = |
| 1491 | Info.Instructions.begin(), ie = Info.Instructions.end(); |
| 1492 | it != ie; ++it) { |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1493 | InstructionInfo &II = **it; |
| 1494 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1495 | OS << " { " << Target.getName() << "::" << II.InstrName |
| 1496 | << ", " << II.ConversionFnKind << ", { "; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1497 | for (unsigned i = 0, e = II.Operands.size(); i != e; ++i) { |
| 1498 | InstructionInfo::Operand &Op = II.Operands[i]; |
| 1499 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1500 | if (i) OS << ", "; |
| 1501 | OS << Op.Class->Name; |
Daniel Dunbar | 20927f2 | 2009-08-07 08:26:05 +0000 | [diff] [blame] | 1502 | } |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1503 | OS << " } },\n"; |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1506 | OS << " };\n\n"; |
| 1507 | |
| 1508 | // Emit code to compute the class list for this operand vector. |
| 1509 | OS << " // Eliminate obvious mismatches.\n"; |
| 1510 | OS << " if (Operands.size() > " << MaxNumOperands << ")\n"; |
| 1511 | OS << " return true;\n\n"; |
| 1512 | |
| 1513 | OS << " // Compute the class list for this operand vector.\n"; |
| 1514 | OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n"; |
| 1515 | OS << " for (unsigned i = 0, e = Operands.size(); i != e; ++i) {\n"; |
| 1516 | OS << " Classes[i] = ClassifyOperand(Operands[i]);\n\n"; |
| 1517 | |
| 1518 | OS << " // Check for invalid operands before matching.\n"; |
| 1519 | OS << " if (Classes[i] == InvalidMatchClass)\n"; |
| 1520 | OS << " return true;\n"; |
| 1521 | OS << " }\n\n"; |
| 1522 | |
| 1523 | OS << " // Mark unused classes.\n"; |
| 1524 | OS << " for (unsigned i = Operands.size(), e = " << MaxNumOperands << "; " |
| 1525 | << "i != e; ++i)\n"; |
| 1526 | OS << " Classes[i] = InvalidMatchClass;\n\n"; |
| 1527 | |
| 1528 | // Emit code to search the table. |
| 1529 | OS << " // Search the table.\n"; |
Chris Lattner | d39bd3a | 2009-08-08 19:16:05 +0000 | [diff] [blame] | 1530 | OS << " for (const MatchEntry *it = MatchTable, " |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1531 | << "*ie = MatchTable + " << Info.Instructions.size() |
| 1532 | << "; it != ie; ++it) {\n"; |
| 1533 | for (unsigned i = 0; i != MaxNumOperands; ++i) { |
Daniel Dunbar | fdb1f49 | 2009-08-10 16:05:47 +0000 | [diff] [blame] | 1534 | OS << " if (!IsSubclass(Classes[" |
| 1535 | << i << "], it->Classes[" << i << "]))\n"; |
Daniel Dunbar | a3741fa | 2009-08-08 07:50:56 +0000 | [diff] [blame] | 1536 | OS << " continue;\n"; |
| 1537 | } |
| 1538 | OS << "\n"; |
| 1539 | OS << " return ConvertToMCInst(it->ConvertFn, Inst, " |
| 1540 | << "it->Opcode, Operands);\n"; |
| 1541 | OS << " }\n\n"; |
| 1542 | |
Daniel Dunbar | a027d22 | 2009-07-31 02:32:59 +0000 | [diff] [blame] | 1543 | OS << " return true;\n"; |
| 1544 | OS << "}\n\n"; |
Daniel Dunbar | d51ffcf | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 1545 | } |