821032df9570d0d1152e755c4cab365abb500f3c
[oota-llvm.git] / utils / TableGen / AsmWriterEmitter.cpp
1 //===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "AsmWriterEmitter.h"
16 #include "CodeGenTarget.h"
17 #include "Record.h"
18 #include <ostream>
19 using namespace llvm;
20
21 static bool isIdentChar(char C) {
22   return (C >= 'a' && C <= 'z') ||
23          (C >= 'A' && C <= 'Z') ||
24          (C >= '0' && C <= '9') ||
25          C == '_';
26 }
27
28 namespace {
29   struct AsmWriterOperand {
30     enum { isLiteralTextOperand, isMachineInstrOperand } OperandType;
31
32     /// Str - For isLiteralTextOperand, this IS the literal text.  For
33     /// isMachineInstrOperand, this is the PrinterMethodName for the operand.
34     std::string Str;
35
36     /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
37     /// machine instruction.
38     unsigned MIOpNo;
39
40     /// OpVT - For isMachineInstrOperand, this is the value type for the
41     /// operand.
42     MVT::ValueType OpVT;
43
44     AsmWriterOperand(const std::string &LitStr)
45       : OperandType(isLiteralTextOperand),  Str(LitStr) {}
46
47     AsmWriterOperand(const std::string &Printer, unsigned OpNo,
48                      MVT::ValueType VT) : OperandType(isMachineInstrOperand),
49                                           Str(Printer), MIOpNo(OpNo), OpVT(VT){}
50
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;
55       return false;
56     }
57     void EmitCode(std::ostream &OS) const;
58   };
59
60   struct AsmWriterInst {
61     std::vector<AsmWriterOperand> Operands;
62     const CodeGenInstruction *CGI;
63     
64     AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
65
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;
70
71   private:
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);
78       else
79         Operands.push_back(AsmWriterOperand(Str));
80     }
81   };
82 }
83
84
85 void AsmWriterOperand::EmitCode(std::ostream &OS) const {
86   if (OperandType == isLiteralTextOperand)
87     OS << "O << \"" << Str << "\"; ";
88   else
89     OS << Str << "(MI, " << MIOpNo << ", MVT::" << getName(OpVT) << "); ";
90 }
91
92
93 /// ParseAsmString - Parse the specified Instruction's AsmString into this
94 /// AsmWriterInst.
95 ///
96 AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
97   this->CGI = &CGI;
98   bool inVariant = false;  // True if we are inside a {.|.|.} region.
99
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();
106
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] == '{') {
114       if (inVariant)
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 + "'!";
126         LastEmitted = NP+1;
127         if (AsmString[NP] == '}') {
128           inVariant = false;        // No text for this variant.
129           break;
130         }
131       }
132     } else if (AsmString[DollarPos] == '|') {
133       if (!inVariant)
134         throw "'|' character found outside of a variant in instruction '"
135           + CGI.Name + "'!";
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 + "'!";
140       LastEmitted = NP+1;
141       inVariant = false;
142     } else if (AsmString[DollarPos] == '}') {
143       if (!inVariant)
144         throw "'}' character found outside of a variant in instruction '"
145           + CGI.Name + "'!";
146       LastEmitted = DollarPos+1;
147       inVariant = false;
148     } else if (DollarPos+1 != AsmString.size() &&
149                AsmString[DollarPos+1] == '$') {
150       AddLiteralString("$");  // "$$" -> $
151       LastEmitted = DollarPos+2;
152     } else {
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]))
157         ++VarEnd;
158       std::string VarName(AsmString.begin()+DollarPos+1,
159                           AsmString.begin()+VarEnd);
160       if (VarName.empty())
161         throw "Stray '$' in '" + CGI.Name + "' asm string, maybe you want $$?";
162
163       unsigned OpNo = CGI.getOperandNamed(VarName);
164       CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
165
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) {
170         if (MIOp == 1)
171           throw "Should refer to operand #0 instead of #1 for two-address"
172             " instruction '" + CGI.Name + "'!";
173         --MIOp;
174       }
175
176       Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName,
177                                           MIOp, OpInfo.Ty));
178       LastEmitted = VarEnd;
179     }
180   }
181
182   AddLiteralString("\\n");
183 }
184
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;
190
191   unsigned MismatchOperand = ~0U;
192   for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
193     if (Operands[i].OperandType != Other.Operands[i].OperandType)
194       return ~0U;
195
196     if (Operands[i] != Other.Operands[i])
197       if (Operands[i].OperandType == AsmWriterOperand::isMachineInstrOperand ||
198           MismatchOperand != ~0U)
199         return ~0U;
200       else 
201         MismatchOperand = i;
202   }
203   return MismatchOperand;
204 }
205
206
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,
210                              std::ostream &O) {
211   AsmWriterInst FirstInst = Insts.back();
212   Insts.pop_back();
213
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);
218     if (DiffOp != ~0U) {
219       if (DifferingOperand == ~0U)  // First match!
220         DifferingOperand = DiffOp;
221
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);
227       }
228     }
229   }
230
231   std::string Namespace = FirstInst.CGI->Namespace;
232
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.
241       O << "    ";
242       FirstInst.Operands[i].EmitCode(O);
243     } else {
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);
250       O << "break;\n";
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);
255         O << "break;\n";
256       }
257       O << "    }";
258     }
259     O << "\n";
260   }
261
262   O << "    break;\n";
263 }
264
265 void AsmWriterEmitter::run(std::ostream &O) {
266   EmitSourceFileHeader("Assembly Writer Source Fragment", O);
267
268   CodeGenTarget Target;
269   Record *AsmWriter = Target.getAsmWriter();
270   std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
271   unsigned Variant = AsmWriter->getValueAsInt("Variant");
272
273   O <<
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";
282
283   std::string Namespace = Target.inst_begin()->second.Namespace;
284
285   std::vector<AsmWriterInst> Instructions;
286
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));
291
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());
295   
296   while (!Instructions.empty())
297     EmitInstructions(Instructions, O);
298
299   O << "  }\n"
300        "  return true;\n"
301        "}\n";
302 }