1 //===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is emits an assembly printer for the current target.
11 // Note that this is currently fairly skeletal, but will grow over time.
13 //===----------------------------------------------------------------------===//
15 #include "AsmWriterEmitter.h"
16 #include "CodeGenTarget.h"
21 static bool isIdentChar(char C) {
22 return (C >= 'a' && C <= 'z') ||
23 (C >= 'A' && C <= 'Z') ||
24 (C >= '0' && C <= '9') ||
29 struct AsmWriterOperand {
30 enum { isLiteralTextOperand, isMachineInstrOperand } OperandType;
32 /// Str - For isLiteralTextOperand, this IS the literal text. For
33 /// isMachineInstrOperand, this is the PrinterMethodName for the operand.
36 /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
37 /// machine instruction.
40 /// OpVT - For isMachineInstrOperand, this is the value type for the
44 AsmWriterOperand(const std::string &LitStr)
45 : OperandType(isLiteralTextOperand), Str(LitStr) {}
47 AsmWriterOperand(const std::string &Printer, unsigned OpNo,
48 MVT::ValueType VT) : OperandType(isMachineInstrOperand),
49 Str(Printer), MIOpNo(OpNo), OpVT(VT){}
51 bool operator!=(const AsmWriterOperand &Other) const {
52 if (OperandType != Other.OperandType || Str != Other.Str) return true;
53 if (OperandType == isMachineInstrOperand)
54 return MIOpNo != Other.MIOpNo || OpVT != Other.OpVT;
57 void EmitCode(std::ostream &OS) const;
60 struct AsmWriterInst {
61 std::vector<AsmWriterOperand> Operands;
62 const CodeGenInstruction *CGI;
64 AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
66 /// MatchesAllButOneString - If this instruction is exactly identical to the
67 /// specified instruction except for one differing literal string, return
68 /// the operand number of the literal string. Otherwise return ~0.
69 unsigned MatchesAllButOneString(const AsmWriterInst &Other) const;
72 void AddLiteralString(const std::string &Str) {
73 // If the last operand was already a literal text string, append this to
74 // it, otherwise add a new operand.
75 if (!Operands.empty() &&
76 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
77 Operands.back().Str.append(Str);
79 Operands.push_back(AsmWriterOperand(Str));
85 void AsmWriterOperand::EmitCode(std::ostream &OS) const {
86 if (OperandType == isLiteralTextOperand)
87 OS << "O << \"" << Str << "\"; ";
89 OS << Str << "(MI, " << MIOpNo << ", MVT::" << getName(OpVT) << "); ";
93 /// ParseAsmString - Parse the specified Instruction's AsmString into this
96 AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
98 bool inVariant = false; // True if we are inside a {.|.|.} region.
100 const std::string &AsmString = CGI.AsmString;
101 std::string::size_type LastEmitted = 0;
102 while (LastEmitted != AsmString.size()) {
103 std::string::size_type DollarPos =
104 AsmString.find_first_of("${|}", LastEmitted);
105 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
107 // Emit a constant string fragment.
108 if (DollarPos != LastEmitted) {
109 // TODO: this should eventually handle escaping.
110 AddLiteralString(std::string(AsmString.begin()+LastEmitted,
111 AsmString.begin()+DollarPos));
112 LastEmitted = DollarPos;
113 } else if (AsmString[DollarPos] == '{') {
115 throw "Nested variants found for instruction '" + CGI.Name + "'!";
116 LastEmitted = DollarPos+1;
117 inVariant = true; // We are now inside of the variant!
118 for (unsigned i = 0; i != Variant; ++i) {
119 // Skip over all of the text for an irrelevant variant here. The
120 // next variant starts at |, or there may not be text for this
121 // variant if we see a }.
122 std::string::size_type NP =
123 AsmString.find_first_of("|}", LastEmitted);
124 if (NP == std::string::npos)
125 throw "Incomplete variant for instruction '" + CGI.Name + "'!";
127 if (AsmString[NP] == '}') {
128 inVariant = false; // No text for this variant.
132 } else if (AsmString[DollarPos] == '|') {
134 throw "'|' character found outside of a variant in instruction '"
136 // Move to the end of variant list.
137 std::string::size_type NP = AsmString.find('}', LastEmitted);
138 if (NP == std::string::npos)
139 throw "Incomplete variant for instruction '" + CGI.Name + "'!";
142 } else if (AsmString[DollarPos] == '}') {
144 throw "'}' character found outside of a variant in instruction '"
146 LastEmitted = DollarPos+1;
148 } else if (DollarPos+1 != AsmString.size() &&
149 AsmString[DollarPos+1] == '$') {
150 AddLiteralString("$"); // "$$" -> $
151 LastEmitted = DollarPos+2;
153 // Get the name of the variable.
154 // TODO: should eventually handle ${foo}bar as $foo
155 std::string::size_type VarEnd = DollarPos+1;
156 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
158 std::string VarName(AsmString.begin()+DollarPos+1,
159 AsmString.begin()+VarEnd);
161 throw "Stray '$' in '" + CGI.Name + "' asm string, maybe you want $$?";
163 unsigned OpNo = CGI.getOperandNamed(VarName);
164 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
166 // If this is a two-address instruction and we are not accessing the
167 // 0th operand, remove an operand.
168 unsigned MIOp = OpInfo.MIOperandNo;
169 if (CGI.isTwoAddress && MIOp != 0) {
171 throw "Should refer to operand #0 instead of #1 for two-address"
172 " instruction '" + CGI.Name + "'!";
176 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName,
178 LastEmitted = VarEnd;
182 AddLiteralString("\\n");
185 /// MatchesAllButOneString - If this instruction is exactly identical to the
186 /// specified instruction except for one differing literal string, return
187 /// the operand number of the literal string. Otherwise return ~0.
188 unsigned AsmWriterInst::MatchesAllButOneString(const AsmWriterInst &Other)const{
189 if (Operands.size() != Other.Operands.size()) return ~0;
191 unsigned MismatchOperand = ~0U;
192 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
193 if (Operands[i].OperandType != Other.Operands[i].OperandType)
196 if (Operands[i] != Other.Operands[i])
197 if (Operands[i].OperandType == AsmWriterOperand::isMachineInstrOperand ||
198 MismatchOperand != ~0U)
203 return MismatchOperand;
207 /// EmitInstructions - Emit the last instruction in the vector and any other
208 /// instructions that are suitably similar to it.
209 static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
211 AsmWriterInst FirstInst = Insts.back();
214 std::vector<AsmWriterInst> SimilarInsts;
215 unsigned DifferingOperand = ~0;
216 for (unsigned i = Insts.size(); i != 0; --i) {
217 unsigned DiffOp = Insts[i-1].MatchesAllButOneString(FirstInst);
219 if (DifferingOperand == ~0U) // First match!
220 DifferingOperand = DiffOp;
222 // If this differs in the same operand as the rest of the instructions in
223 // this class, move it to the SimilarInsts list.
224 if (DifferingOperand == DiffOp) {
225 SimilarInsts.push_back(Insts[i-1]);
226 Insts.erase(Insts.begin()+i-1);
231 std::string Namespace = FirstInst.CGI->Namespace;
233 O << " case " << Namespace << "::"
234 << FirstInst.CGI->TheDef->getName() << ":\n";
235 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
236 O << " case " << Namespace << "::"
237 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
238 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
239 if (i != DifferingOperand) {
240 // If the operand is the same for all instructions, just print it.
242 FirstInst.Operands[i].EmitCode(O);
244 // If this is the operand that varies between all of the instructions,
245 // emit a switch for just this operand now.
246 O << " switch (MI->getOpcode()) {\n";
247 O << " case " << Namespace << "::"
248 << FirstInst.CGI->TheDef->getName() << ": ";
249 FirstInst.Operands[i].EmitCode(O);
251 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
252 O << " case " << Namespace << "::"
253 << SimilarInsts[si].CGI->TheDef->getName() << ": ";
254 SimilarInsts[si].Operands[i].EmitCode(O);
265 void AsmWriterEmitter::run(std::ostream &O) {
266 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
268 CodeGenTarget Target;
269 Record *AsmWriter = Target.getAsmWriter();
270 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
271 unsigned Variant = AsmWriter->getValueAsInt("Variant");
274 "/// printInstruction - This method is automatically generated by tablegen\n"
275 "/// from the instruction set description. This method returns true if the\n"
276 "/// machine instruction was sufficiently described to print it, otherwise\n"
277 "/// it returns false.\n"
278 "bool " << Target.getName() << ClassName
279 << "::printInstruction(const MachineInstr *MI) {\n";
280 O << " switch (MI->getOpcode()) {\n"
281 " default: return false;\n";
283 std::string Namespace = Target.inst_begin()->second.Namespace;
285 std::vector<AsmWriterInst> Instructions;
287 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
288 E = Target.inst_end(); I != E; ++I)
289 if (!I->second.AsmString.empty())
290 Instructions.push_back(AsmWriterInst(I->second, Variant));
292 // Because this is a vector we want to emit from the end. Reverse all of the
293 // elements in the vector.
294 std::reverse(Instructions.begin(), Instructions.end());
296 while (!Instructions.empty())
297 EmitInstructions(Instructions, O);