blob: ecd09630e1ef8dd3af3d6c62b6a984e569e1d1a9 [file] [log] [blame]
Chris Lattner2e1f51b2004-08-01 05:59:33 +00001//===- 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"
Chris Lattner175580c2004-08-14 22:50:53 +000017#include "Record.h"
Jeff Cohen615ed992005-01-22 18:50:10 +000018#include <algorithm>
Chris Lattner2e1f51b2004-08-01 05:59:33 +000019#include <ostream>
20using namespace llvm;
21
Chris Lattner076efa72004-08-01 07:43:02 +000022static bool isIdentChar(char C) {
23 return (C >= 'a' && C <= 'z') ||
24 (C >= 'A' && C <= 'Z') ||
25 (C >= '0' && C <= '9') ||
26 C == '_';
27}
28
Chris Lattnerb0b55e72005-01-22 17:32:42 +000029namespace {
30 struct AsmWriterOperand {
31 enum { isLiteralTextOperand, isMachineInstrOperand } OperandType;
32
33 /// Str - For isLiteralTextOperand, this IS the literal text. For
34 /// isMachineInstrOperand, this is the PrinterMethodName for the operand.
35 std::string Str;
36
37 /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
38 /// machine instruction.
39 unsigned MIOpNo;
40
41 /// OpVT - For isMachineInstrOperand, this is the value type for the
42 /// operand.
43 MVT::ValueType OpVT;
44
45 AsmWriterOperand(const std::string &LitStr)
46 : OperandType(isLiteralTextOperand), Str(LitStr) {}
47
48 AsmWriterOperand(const std::string &Printer, unsigned OpNo,
49 MVT::ValueType VT) : OperandType(isMachineInstrOperand),
50 Str(Printer), MIOpNo(OpNo), OpVT(VT){}
51
Chris Lattner870c0162005-01-22 18:38:13 +000052 bool operator!=(const AsmWriterOperand &Other) const {
53 if (OperandType != Other.OperandType || Str != Other.Str) return true;
54 if (OperandType == isMachineInstrOperand)
55 return MIOpNo != Other.MIOpNo || OpVT != Other.OpVT;
56 return false;
57 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +000058 void EmitCode(std::ostream &OS) const;
59 };
60
61 struct AsmWriterInst {
62 std::vector<AsmWriterOperand> Operands;
Chris Lattner5765dba2005-01-22 17:40:38 +000063 const CodeGenInstruction *CGI;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000064
Chris Lattner5765dba2005-01-22 17:40:38 +000065 AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
Chris Lattner870c0162005-01-22 18:38:13 +000066
67 /// MatchesAllButOneString - If this instruction is exactly identical to the
68 /// specified instruction except for one differing literal string, return
69 /// the operand number of the literal string. Otherwise return ~0.
70 unsigned MatchesAllButOneString(const AsmWriterInst &Other) const;
71
Chris Lattnerb0b55e72005-01-22 17:32:42 +000072 private:
73 void AddLiteralString(const std::string &Str) {
74 // If the last operand was already a literal text string, append this to
75 // it, otherwise add a new operand.
76 if (!Operands.empty() &&
77 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
78 Operands.back().Str.append(Str);
79 else
80 Operands.push_back(AsmWriterOperand(Str));
81 }
82 };
83}
84
85
86void AsmWriterOperand::EmitCode(std::ostream &OS) const {
87 if (OperandType == isLiteralTextOperand)
88 OS << "O << \"" << Str << "\"; ";
89 else
90 OS << Str << "(MI, " << MIOpNo << ", MVT::" << getName(OpVT) << "); ";
91}
92
93
94/// ParseAsmString - Parse the specified Instruction's AsmString into this
95/// AsmWriterInst.
96///
Chris Lattner5765dba2005-01-22 17:40:38 +000097AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
98 this->CGI = &CGI;
Chris Lattnerb0b55e72005-01-22 17:32:42 +000099 bool inVariant = false; // True if we are inside a {.|.|.} region.
100
101 const std::string &AsmString = CGI.AsmString;
102 std::string::size_type LastEmitted = 0;
103 while (LastEmitted != AsmString.size()) {
104 std::string::size_type DollarPos =
105 AsmString.find_first_of("${|}", LastEmitted);
106 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
107
108 // Emit a constant string fragment.
109 if (DollarPos != LastEmitted) {
110 // TODO: this should eventually handle escaping.
111 AddLiteralString(std::string(AsmString.begin()+LastEmitted,
112 AsmString.begin()+DollarPos));
113 LastEmitted = DollarPos;
114 } else if (AsmString[DollarPos] == '{') {
115 if (inVariant)
116 throw "Nested variants found for instruction '" + CGI.Name + "'!";
117 LastEmitted = DollarPos+1;
118 inVariant = true; // We are now inside of the variant!
119 for (unsigned i = 0; i != Variant; ++i) {
120 // Skip over all of the text for an irrelevant variant here. The
121 // next variant starts at |, or there may not be text for this
122 // variant if we see a }.
123 std::string::size_type NP =
124 AsmString.find_first_of("|}", LastEmitted);
125 if (NP == std::string::npos)
126 throw "Incomplete variant for instruction '" + CGI.Name + "'!";
127 LastEmitted = NP+1;
128 if (AsmString[NP] == '}') {
129 inVariant = false; // No text for this variant.
130 break;
131 }
132 }
133 } else if (AsmString[DollarPos] == '|') {
134 if (!inVariant)
135 throw "'|' character found outside of a variant in instruction '"
136 + CGI.Name + "'!";
137 // Move to the end of variant list.
138 std::string::size_type NP = AsmString.find('}', LastEmitted);
139 if (NP == std::string::npos)
140 throw "Incomplete variant for instruction '" + CGI.Name + "'!";
141 LastEmitted = NP+1;
142 inVariant = false;
143 } else if (AsmString[DollarPos] == '}') {
144 if (!inVariant)
145 throw "'}' character found outside of a variant in instruction '"
146 + CGI.Name + "'!";
147 LastEmitted = DollarPos+1;
148 inVariant = false;
149 } else if (DollarPos+1 != AsmString.size() &&
150 AsmString[DollarPos+1] == '$') {
151 AddLiteralString("$"); // "$$" -> $
152 LastEmitted = DollarPos+2;
153 } else {
154 // Get the name of the variable.
155 // TODO: should eventually handle ${foo}bar as $foo
156 std::string::size_type VarEnd = DollarPos+1;
157 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
158 ++VarEnd;
159 std::string VarName(AsmString.begin()+DollarPos+1,
160 AsmString.begin()+VarEnd);
161 if (VarName.empty())
162 throw "Stray '$' in '" + CGI.Name + "' asm string, maybe you want $$?";
163
164 unsigned OpNo = CGI.getOperandNamed(VarName);
Chris Lattner5765dba2005-01-22 17:40:38 +0000165 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000166
167 // If this is a two-address instruction and we are not accessing the
168 // 0th operand, remove an operand.
Chris Lattner5765dba2005-01-22 17:40:38 +0000169 unsigned MIOp = OpInfo.MIOperandNo;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000170 if (CGI.isTwoAddress && MIOp != 0) {
171 if (MIOp == 1)
172 throw "Should refer to operand #0 instead of #1 for two-address"
173 " instruction '" + CGI.Name + "'!";
174 --MIOp;
175 }
176
Chris Lattner5765dba2005-01-22 17:40:38 +0000177 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName,
178 MIOp, OpInfo.Ty));
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000179 LastEmitted = VarEnd;
180 }
181 }
182
183 AddLiteralString("\\n");
184}
185
Chris Lattner870c0162005-01-22 18:38:13 +0000186/// MatchesAllButOneString - If this instruction is exactly identical to the
187/// specified instruction except for one differing literal string, return
188/// the operand number of the literal string. Otherwise return ~0.
189unsigned AsmWriterInst::MatchesAllButOneString(const AsmWriterInst &Other)const{
190 if (Operands.size() != Other.Operands.size()) return ~0;
191
192 unsigned MismatchOperand = ~0U;
193 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
194 if (Operands[i].OperandType != Other.Operands[i].OperandType)
195 return ~0U;
196
197 if (Operands[i] != Other.Operands[i])
198 if (Operands[i].OperandType == AsmWriterOperand::isMachineInstrOperand ||
199 MismatchOperand != ~0U)
200 return ~0U;
201 else
202 MismatchOperand = i;
203 }
204 return MismatchOperand;
205}
206
207
208/// EmitInstructions - Emit the last instruction in the vector and any other
209/// instructions that are suitably similar to it.
210static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
211 std::ostream &O) {
212 AsmWriterInst FirstInst = Insts.back();
213 Insts.pop_back();
214
215 std::vector<AsmWriterInst> SimilarInsts;
216 unsigned DifferingOperand = ~0;
217 for (unsigned i = Insts.size(); i != 0; --i) {
218 unsigned DiffOp = Insts[i-1].MatchesAllButOneString(FirstInst);
219 if (DiffOp != ~0U) {
220 if (DifferingOperand == ~0U) // First match!
221 DifferingOperand = DiffOp;
222
223 // If this differs in the same operand as the rest of the instructions in
224 // this class, move it to the SimilarInsts list.
225 if (DifferingOperand == DiffOp) {
226 SimilarInsts.push_back(Insts[i-1]);
227 Insts.erase(Insts.begin()+i-1);
228 }
229 }
230 }
231
232 std::string Namespace = FirstInst.CGI->Namespace;
233
234 O << " case " << Namespace << "::"
235 << FirstInst.CGI->TheDef->getName() << ":\n";
236 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
237 O << " case " << Namespace << "::"
238 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
239 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
240 if (i != DifferingOperand) {
241 // If the operand is the same for all instructions, just print it.
242 O << " ";
243 FirstInst.Operands[i].EmitCode(O);
244 } else {
245 // If this is the operand that varies between all of the instructions,
246 // emit a switch for just this operand now.
247 O << " switch (MI->getOpcode()) {\n";
248 O << " case " << Namespace << "::"
249 << FirstInst.CGI->TheDef->getName() << ": ";
250 FirstInst.Operands[i].EmitCode(O);
251 O << "break;\n";
252 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
253 O << " case " << Namespace << "::"
254 << SimilarInsts[si].CGI->TheDef->getName() << ": ";
255 SimilarInsts[si].Operands[i].EmitCode(O);
256 O << "break;\n";
257 }
258 O << " }";
259 }
260 O << "\n";
261 }
262
263 O << " break;\n";
264}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000265
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000266void AsmWriterEmitter::run(std::ostream &O) {
267 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
268
269 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +0000270 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000271 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
272 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Chris Lattner175580c2004-08-14 22:50:53 +0000273
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000274 O <<
275 "/// printInstruction - This method is automatically generated by tablegen\n"
276 "/// from the instruction set description. This method returns true if the\n"
277 "/// machine instruction was sufficiently described to print it, otherwise\n"
278 "/// it returns false.\n"
Chris Lattner953c6fe2004-10-03 20:19:02 +0000279 "bool " << Target.getName() << ClassName
Chris Lattner175580c2004-08-14 22:50:53 +0000280 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000281 O << " switch (MI->getOpcode()) {\n"
282 " default: return false;\n";
283
284 std::string Namespace = Target.inst_begin()->second.Namespace;
285
Chris Lattner5765dba2005-01-22 17:40:38 +0000286 std::vector<AsmWriterInst> Instructions;
287
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000288 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
289 E = Target.inst_end(); I != E; ++I)
Chris Lattner5765dba2005-01-22 17:40:38 +0000290 if (!I->second.AsmString.empty())
291 Instructions.push_back(AsmWriterInst(I->second, Variant));
Chris Lattner076efa72004-08-01 07:43:02 +0000292
Chris Lattner870c0162005-01-22 18:38:13 +0000293 // Because this is a vector we want to emit from the end. Reverse all of the
294 // elements in the vector.
295 std::reverse(Instructions.begin(), Instructions.end());
Chris Lattner5765dba2005-01-22 17:40:38 +0000296
Chris Lattner870c0162005-01-22 18:38:13 +0000297 while (!Instructions.empty())
298 EmitInstructions(Instructions, O);
299
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000300 O << " }\n"
301 " return true;\n"
302 "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000303}