1 //===- AsmMatcherEmitter.cpp - Generate an assembly matcher ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits a target specifier matcher for converting parsed
11 // assembly operands in the MCInst structures.
13 //===----------------------------------------------------------------------===//
15 #include "AsmMatcherEmitter.h"
16 #include "CodeGenTarget.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/Debug.h"
24 /// FlattenVariants - Flatten an .td file assembly string by selecting the
25 /// variant at index \arg N.
26 static std::string FlattenVariants(const std::string &AsmString,
28 StringRef Cur = AsmString;
32 // Add the prefix until the next '{', and split out the contents in the
34 std::pair<StringRef, StringRef> Inner, Split = Cur.split('{');
37 if (Split.second.empty())
40 Inner = Split.second.split('}');
42 // Select the Nth variant (or empty).
43 StringRef Selection = Inner.first;
44 for (unsigned i = 0; i != N; ++i)
45 Selection = Selection.split('|').second;
46 Res += Selection.split('|').first;
54 /// TokenizeAsmString - Tokenize a simplified assembly string.
55 static void TokenizeAsmString(const std::string &AsmString,
56 SmallVectorImpl<StringRef> &Tokens) {
59 for (unsigned i = 0, e = AsmString.size(); i != e; ++i) {
60 switch (AsmString[i]) {
67 Tokens.push_back(StringRef(&AsmString[Prev], i - Prev));
70 if (AsmString[i] == '*' || AsmString[i] == '!')
71 Tokens.push_back(StringRef(&AsmString[i], 1));
79 if (InTok && Prev != AsmString.size())
80 Tokens.push_back(StringRef(&AsmString[Prev], AsmString.size() - Prev));
83 void AsmMatcherEmitter::run(raw_ostream &OS) {
85 const std::vector<CodeGenRegister> &Registers = Target.getRegisters();
86 Record *AsmParser = Target.getAsmParser();
87 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
89 std::string Namespace = Registers[0].TheDef->getValueAsString("Namespace");
91 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
93 // Emit the function to match a register name to number.
95 OS << "bool " << Target.getName() << ClassName
96 << "::MatchRegisterName(const StringRef &Name, unsigned &RegNo) {\n";
98 // FIXME: TableGen should have a fast string matcher generator.
99 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
100 const CodeGenRegister &Reg = Registers[i];
101 if (Reg.TheDef->getValueAsString("AsmName").empty())
104 OS << " if (Name == \""
105 << Reg.TheDef->getValueAsString("AsmName") << "\")\n"
106 << " return RegNo=" << i + 1 << ", false;\n";
108 OS << " return true;\n";
111 // Emit the function to match instructions.
112 std::vector<const CodeGenInstruction*> NumberedInstructions;
113 Target.getInstructionsByEnumValue(NumberedInstructions);
115 std::list<std::string> MatchFns;
118 const std::map<std::string, CodeGenInstruction> &Instructions =
119 Target.getInstructions();
120 for (std::map<std::string, CodeGenInstruction>::const_iterator
121 it = Instructions.begin(), ie = Instructions.end(); it != ie; ++it) {
122 const CodeGenInstruction &CGI = it->second;
124 // Ignore psuedo ops.
126 // FIXME: This is a hack.
127 if (const RecordVal *Form = CGI.TheDef->getValue("Form"))
128 if (Form->getValue()->getAsString() == "Pseudo")
131 // Ignore instructions with no .s string.
133 // FIXME: What are these?
134 if (CGI.AsmString.empty())
137 // FIXME: Hack; ignore "lock".
138 if (StringRef(CGI.AsmString).startswith("lock"))
143 if (1 && it->first != "SUB8mr")
147 std::string Flattened = FlattenVariants(CGI.AsmString, 0);
148 SmallVector<StringRef, 8> Tokens;
150 TokenizeAsmString(Flattened, Tokens);
153 outs() << it->first << " -- flattened:\""
154 << Flattened << "\", tokens:[";
155 for (unsigned i = 0, e = Tokens.size(); i != e; ++i) {
162 for (unsigned i = 0, e = CGI.OperandList.size(); i != e; ++i) {
163 const CodeGenInstruction::OperandInfo &OI = CGI.OperandList[i];
164 outs() << " op[" << i << "] = " << OI.Name
165 << " " << OI.Rec->getName()
166 << " (" << OI.MIOperandNo << ", " << OI.MINumOperands << ")\n";
170 // FIXME: Ignore non-literal tokens.
171 if (std::find(Tokens[0].begin(), Tokens[0].end(), '$') != Tokens[0].end())
174 std::string FnName = "Match_" + Target.getName() + "_Inst_" + it->first;
175 MatchFns.push_back(FnName);
177 OS << "static bool " << FnName
178 << "(const StringRef &Name,"
179 << " SmallVectorImpl<X86Operand> &Operands,"
180 << " MCInst &Inst) {\n\n";
182 OS << " // Match name.\n";
183 OS << " if (Name != \"" << Tokens[0] << "\")\n";
184 OS << " return true;\n\n";
186 OS << " // Match number of operands.\n";
187 OS << " if (Operands.size() != " << Tokens.size() - 1 << ")\n";
188 OS << " return true;\n\n";
190 // Compute the total number of MCOperands.
192 // FIXME: Isn't this somewhere else?
193 unsigned NumMIOperands = 0;
194 for (unsigned i = 0, e = CGI.OperandList.size(); i != e; ++i) {
195 const CodeGenInstruction::OperandInfo &OI = CGI.OperandList[i];
196 NumMIOperands = std::max(NumMIOperands,
197 OI.MIOperandNo + OI.MINumOperands);
200 std::set<unsigned> MatchedOperands;
201 // This the list of operands we need to fill in.
203 OS << " MCOperand Ops[" << NumMIOperands << "];\n\n";
205 unsigned ParsedOpIdx = 0;
206 for (unsigned i = 1, e = Tokens.size(); i < e; ++i) {
207 // FIXME: Can only match simple operands.
208 if (Tokens[i][0] != '$') {
209 OS << " // FIXME: unable to match token: '" << Tokens[i] << "'!\n";
210 OS << " return true;\n\n";
214 // Map this token to an operand. FIXME: Move elsewhere.
218 Idx = CGI.getOperandNamed(Tokens[i].substr(1));
220 OS << " // FIXME: unable to find operand: '" << Tokens[i] << "'!\n";
221 OS << " return true;\n\n";
225 // FIXME: Each match routine should always end up filling the same number
226 // of operands, we should just check that the number matches what the
227 // match routine expects here instead of passing it. We can do this once
228 // we start generating the class match functions.
229 const CodeGenInstruction::OperandInfo &OI = CGI.OperandList[Idx];
231 // Track that we have matched these operands.
233 // FIXME: Verify that we don't parse something to the same operand twice.
234 for (unsigned j = 0; j != OI.MINumOperands; ++j)
235 MatchedOperands.insert(OI.MIOperandNo + j);
237 OS << " // Match '" << Tokens[i] << "' (parsed operand " << ParsedOpIdx
238 << ") to machine operands [" << OI.MIOperandNo << ", "
239 << OI.MIOperandNo + OI.MINumOperands << ").\n";
240 OS << " if (Match_" << Target.getName()
241 << "_Op_" << OI.Rec->getName() << "("
242 << "Operands[" << ParsedOpIdx << "], "
243 << "&Ops[" << OI.MIOperandNo << "], "
244 << OI.MINumOperands << "))\n";
245 OS << " return true;\n\n";
250 // Generate code to construct the MCInst.
252 OS << " // Construct MCInst.\n";
253 OS << " Inst.setOpcode(" << Target.getName() << "::"
254 << it->first << ");\n";
255 for (unsigned i = 0, e = NumMIOperands; i != e; ++i) {
256 // FIXME: Oops! Ignore this for now, the instruction should print ok. If
257 // we need to evaluate the constraints.
258 if (!MatchedOperands.count(i)) {
260 OS << " // FIXME: Nothing matched Ops[" << i << "]!\n";
261 OS << " Ops[" << i << "].MakeReg(0);\n";
265 OS << " Inst.addOperand(Ops[" << i << "]);\n";
268 OS << " return false;\n";
272 // Generate the top level match function.
274 OS << "bool " << Target.getName() << ClassName
275 << "::MatchInstruction(const StringRef &Name, "
276 << "SmallVectorImpl<" << Target.getName() << "Operand> &Operands, "
277 << "MCInst &Inst) {\n";
278 for (std::list<std::string>::iterator it = MatchFns.begin(),
279 ie = MatchFns.end(); it != ie; ++it) {
280 OS << " if (!" << *it << "(Name, Operands, Inst))\n";
281 OS << " return false;\n\n";
284 OS << " return true;\n";