Daniel Dunbar | 3f6e3ff | 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 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "AsmMatcherEmitter.h" |
| 16 | #include "CodeGenTarget.h" |
| 17 | #include "Record.h" |
| 18 | using namespace llvm; |
| 19 | |
Daniel Dunbar | 2f9876b | 2009-07-17 18:51:11 +0000 | [diff] [blame^] | 20 | void AsmMatcherEmitter::run(raw_ostream &OS) { |
Daniel Dunbar | 3f6e3ff | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 21 | CodeGenTarget Target; |
Daniel Dunbar | 2f9876b | 2009-07-17 18:51:11 +0000 | [diff] [blame^] | 22 | const std::vector<CodeGenRegister> &Registers = Target.getRegisters(); |
| 23 | |
| 24 | std::string Namespace = Registers[0].TheDef->getValueAsString("Namespace"); |
| 25 | |
| 26 | EmitSourceFileHeader("Assembly Matcher Source Fragment", OS); |
| 27 | OS << "namespace llvm {\n\n"; |
| 28 | |
| 29 | // Emit the function to match a register name to number. |
| 30 | |
| 31 | if (!Namespace.empty()) |
| 32 | OS << "namespace " << Namespace << " {\n"; |
| 33 | OS << "bool MatchRegisterName(const std::string &Name, unsigned &RegNo) {\n"; |
| 34 | |
| 35 | // FIXME: TableGen should have a fast string matcher generator. |
| 36 | for (unsigned i = 0, e = Registers.size(); i != e; ++i) { |
| 37 | const CodeGenRegister &Reg = Registers[i]; |
| 38 | if (Reg.TheDef->getValueAsString("AsmName").empty()) |
| 39 | continue; |
| 40 | |
| 41 | OS << " if (Name == \"" |
| 42 | << Reg.TheDef->getValueAsString("AsmName") << "\")\n" |
| 43 | << " return RegNo=" << i + 1 << ", false;\n"; |
| 44 | } |
| 45 | OS << " return true;\n"; |
| 46 | OS << "}\n"; |
| 47 | |
| 48 | if (!Namespace.empty()) |
| 49 | OS << "}\n"; |
| 50 | OS << "} // End llvm namespace \n"; |
Daniel Dunbar | 3f6e3ff | 2009-07-11 19:39:44 +0000 | [diff] [blame] | 51 | } |