blob: 0c973995978cec56b86c3dc4d4ad00786af39e7f [file] [log] [blame]
Chris Lattner2e1f51b2004-08-01 05:59:33 +00001//===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Chris Lattner2e1f51b2004-08-01 05:59:33 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Chris Lattner2e1f51b2004-08-01 05:59:33 +00008//===----------------------------------------------------------------------===//
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
Chris Lattnerb0b55e72005-01-22 17:32:42 +000041 AsmWriterOperand(const std::string &LitStr)
Nate Begeman391c5d22005-11-30 18:54:35 +000042 : OperandType(isLiteralTextOperand), Str(LitStr) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000043
Nate Begeman391c5d22005-11-30 18:54:35 +000044 AsmWriterOperand(const std::string &Printer, unsigned OpNo)
45 : OperandType(isMachineInstrOperand), Str(Printer), MIOpNo(OpNo) {}
Chris Lattnerb0b55e72005-01-22 17:32:42 +000046
Chris Lattner870c0162005-01-22 18:38:13 +000047 bool operator!=(const AsmWriterOperand &Other) const {
48 if (OperandType != Other.OperandType || Str != Other.Str) return true;
49 if (OperandType == isMachineInstrOperand)
Nate Begeman391c5d22005-11-30 18:54:35 +000050 return MIOpNo != Other.MIOpNo;
Chris Lattner870c0162005-01-22 18:38:13 +000051 return false;
52 }
Chris Lattner38c07512005-01-22 20:31:17 +000053 bool operator==(const AsmWriterOperand &Other) const {
54 return !operator!=(Other);
55 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +000056 void EmitCode(std::ostream &OS) const;
57 };
58
59 struct AsmWriterInst {
60 std::vector<AsmWriterOperand> Operands;
Chris Lattner5765dba2005-01-22 17:40:38 +000061 const CodeGenInstruction *CGI;
Misha Brukman3da94ae2005-04-22 00:00:37 +000062
Chris Lattner5765dba2005-01-22 17:40:38 +000063 AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
Chris Lattner870c0162005-01-22 18:38:13 +000064
Chris Lattnerf8766682005-01-22 19:22:23 +000065 /// MatchesAllButOneOp - If this instruction is exactly identical to the
66 /// specified instruction except for one differing operand, return the
67 /// differing operand number. Otherwise return ~0.
68 unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
Chris Lattner870c0162005-01-22 18:38:13 +000069
Chris Lattnerb0b55e72005-01-22 17:32:42 +000070 private:
71 void AddLiteralString(const std::string &Str) {
72 // If the last operand was already a literal text string, append this to
73 // it, otherwise add a new operand.
74 if (!Operands.empty() &&
75 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
76 Operands.back().Str.append(Str);
77 else
78 Operands.push_back(AsmWriterOperand(Str));
79 }
80 };
81}
82
83
84void AsmWriterOperand::EmitCode(std::ostream &OS) const {
85 if (OperandType == isLiteralTextOperand)
86 OS << "O << \"" << Str << "\"; ";
87 else
Nate Begeman391c5d22005-11-30 18:54:35 +000088 OS << Str << "(MI, " << MIOpNo << "); ";
Chris Lattnerb0b55e72005-01-22 17:32:42 +000089}
90
91
92/// ParseAsmString - Parse the specified Instruction's AsmString into this
93/// AsmWriterInst.
94///
Chris Lattner5765dba2005-01-22 17:40:38 +000095AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
96 this->CGI = &CGI;
Chris Lattnerb03b0802006-02-06 22:43:28 +000097 unsigned CurVariant = ~0U; // ~0 if we are outside a {.|.|.} region, other #.
Chris Lattnerb0b55e72005-01-22 17:32:42 +000098
Chris Lattner1cf9d962006-02-01 19:12:23 +000099 // NOTE: Any extensions to this code need to be mirrored in the
100 // AsmPrinter::printInlineAsm code that executes as compile time (assuming
101 // that inline asm strings should also get the new feature)!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000102 const std::string &AsmString = CGI.AsmString;
103 std::string::size_type LastEmitted = 0;
104 while (LastEmitted != AsmString.size()) {
105 std::string::size_type DollarPos =
106 AsmString.find_first_of("${|}", LastEmitted);
107 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
108
109 // Emit a constant string fragment.
110 if (DollarPos != LastEmitted) {
111 // TODO: this should eventually handle escaping.
Chris Lattnerb03b0802006-02-06 22:43:28 +0000112 if (CurVariant == Variant || CurVariant == ~0U)
113 AddLiteralString(std::string(AsmString.begin()+LastEmitted,
114 AsmString.begin()+DollarPos));
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000115 LastEmitted = DollarPos;
116 } else if (AsmString[DollarPos] == '{') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000117 if (CurVariant != ~0U)
Jeff Cohen00b168892005-07-27 06:12:32 +0000118 throw "Nested variants found for instruction '" +
Chris Lattner3e3def92005-07-15 22:43:04 +0000119 CGI.TheDef->getName() + "'!";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000120 LastEmitted = DollarPos+1;
Chris Lattnerb03b0802006-02-06 22:43:28 +0000121 CurVariant = 0; // We are now inside of the variant!
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000122 } else if (AsmString[DollarPos] == '|') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000123 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000124 throw "'|' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000125 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000126 ++CurVariant;
127 ++LastEmitted;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000128 } else if (AsmString[DollarPos] == '}') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000129 if (CurVariant == ~0U)
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000130 throw "'}' character found outside of a variant in instruction '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000131 + CGI.TheDef->getName() + "'!";
Chris Lattnerb03b0802006-02-06 22:43:28 +0000132 ++LastEmitted;
133 CurVariant = ~0U;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000134 } else if (DollarPos+1 != AsmString.size() &&
135 AsmString[DollarPos+1] == '$') {
Chris Lattnerb03b0802006-02-06 22:43:28 +0000136 if (CurVariant == Variant || CurVariant == ~0U)
137 AddLiteralString("$"); // "$$" -> $
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000138 LastEmitted = DollarPos+2;
139 } else {
140 // Get the name of the variable.
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000141 std::string::size_type VarEnd = DollarPos+1;
Nate Begemanafc54562005-07-14 22:50:30 +0000142
143 // handle ${foo}bar as $foo by detecting whether the character following
144 // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
145 // so the variable name does not contain the leading curly brace.
146 bool hasCurlyBraces = false;
147 if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
148 hasCurlyBraces = true;
149 ++DollarPos;
150 ++VarEnd;
151 }
152
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000153 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
154 ++VarEnd;
155 std::string VarName(AsmString.begin()+DollarPos+1,
156 AsmString.begin()+VarEnd);
Nate Begemanafc54562005-07-14 22:50:30 +0000157
158 // In order to avoid starting the next string at the terminating curly
159 // brace, advance the end position past it if we found an opening curly
160 // brace.
161 if (hasCurlyBraces) {
162 if (VarEnd >= AsmString.size())
163 throw "Reached end of string before terminating curly brace in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000164 + CGI.TheDef->getName() + "'";
Nate Begemanafc54562005-07-14 22:50:30 +0000165 if (AsmString[VarEnd] != '}')
Chris Lattnerb03b0802006-02-06 22:43:28 +0000166 throw "Variable name beginning with '{' did not end with '}' in '"
Chris Lattner3e3def92005-07-15 22:43:04 +0000167 + CGI.TheDef->getName() + "'";
Nate Begemanafc54562005-07-14 22:50:30 +0000168 ++VarEnd;
169 }
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000170 if (VarName.empty())
Jeff Cohen00b168892005-07-27 06:12:32 +0000171 throw "Stray '$' in '" + CGI.TheDef->getName() +
Chris Lattner3e3def92005-07-15 22:43:04 +0000172 "' asm string, maybe you want $$?";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000173
174 unsigned OpNo = CGI.getOperandNamed(VarName);
Chris Lattner5765dba2005-01-22 17:40:38 +0000175 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000176
177 // If this is a two-address instruction and we are not accessing the
178 // 0th operand, remove an operand.
Chris Lattner5765dba2005-01-22 17:40:38 +0000179 unsigned MIOp = OpInfo.MIOperandNo;
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000180 if (CGI.isTwoAddress && MIOp != 0) {
181 if (MIOp == 1)
182 throw "Should refer to operand #0 instead of #1 for two-address"
Chris Lattner3e3def92005-07-15 22:43:04 +0000183 " instruction '" + CGI.TheDef->getName() + "'!";
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000184 --MIOp;
185 }
186
Chris Lattnerb03b0802006-02-06 22:43:28 +0000187 if (CurVariant == Variant || CurVariant == ~0U)
188 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, MIOp));
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000189 LastEmitted = VarEnd;
190 }
191 }
192
193 AddLiteralString("\\n");
194}
195
Chris Lattnerf8766682005-01-22 19:22:23 +0000196/// MatchesAllButOneOp - If this instruction is exactly identical to the
197/// specified instruction except for one differing operand, return the differing
198/// operand number. If more than one operand mismatches, return ~1, otherwise
199/// if the instructions are identical return ~0.
200unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
201 if (Operands.size() != Other.Operands.size()) return ~1;
Chris Lattner870c0162005-01-22 18:38:13 +0000202
203 unsigned MismatchOperand = ~0U;
204 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattner870c0162005-01-22 18:38:13 +0000205 if (Operands[i] != Other.Operands[i])
Chris Lattnerf8766682005-01-22 19:22:23 +0000206 if (MismatchOperand != ~0U) // Already have one mismatch?
207 return ~1U;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000208 else
Chris Lattner870c0162005-01-22 18:38:13 +0000209 MismatchOperand = i;
210 }
211 return MismatchOperand;
212}
213
Chris Lattner38c07512005-01-22 20:31:17 +0000214static void PrintCases(std::vector<std::pair<std::string,
215 AsmWriterOperand> > &OpsToPrint, std::ostream &O) {
216 O << " case " << OpsToPrint.back().first << ": ";
217 AsmWriterOperand TheOp = OpsToPrint.back().second;
218 OpsToPrint.pop_back();
219
220 // Check to see if any other operands are identical in this list, and if so,
221 // emit a case label for them.
222 for (unsigned i = OpsToPrint.size(); i != 0; --i)
223 if (OpsToPrint[i-1].second == TheOp) {
224 O << "\n case " << OpsToPrint[i-1].first << ": ";
225 OpsToPrint.erase(OpsToPrint.begin()+i-1);
226 }
227
228 // Finally, emit the code.
229 TheOp.EmitCode(O);
230 O << "break;\n";
231}
232
Chris Lattner870c0162005-01-22 18:38:13 +0000233
234/// EmitInstructions - Emit the last instruction in the vector and any other
235/// instructions that are suitably similar to it.
236static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
237 std::ostream &O) {
238 AsmWriterInst FirstInst = Insts.back();
239 Insts.pop_back();
240
241 std::vector<AsmWriterInst> SimilarInsts;
242 unsigned DifferingOperand = ~0;
243 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +0000244 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
245 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000246 if (DifferingOperand == ~0U) // First match!
247 DifferingOperand = DiffOp;
248
249 // If this differs in the same operand as the rest of the instructions in
250 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +0000251 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +0000252 SimilarInsts.push_back(Insts[i-1]);
253 Insts.erase(Insts.begin()+i-1);
254 }
255 }
256 }
257
258 std::string Namespace = FirstInst.CGI->Namespace;
259
260 O << " case " << Namespace << "::"
261 << FirstInst.CGI->TheDef->getName() << ":\n";
262 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
263 O << " case " << Namespace << "::"
264 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
265 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
266 if (i != DifferingOperand) {
267 // If the operand is the same for all instructions, just print it.
268 O << " ";
269 FirstInst.Operands[i].EmitCode(O);
270 } else {
271 // If this is the operand that varies between all of the instructions,
272 // emit a switch for just this operand now.
273 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +0000274 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
275 OpsToPrint.push_back(std::make_pair(Namespace+"::"+
276 FirstInst.CGI->TheDef->getName(),
277 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000278
Chris Lattner870c0162005-01-22 18:38:13 +0000279 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +0000280 AsmWriterInst &AWI = SimilarInsts[si];
281 OpsToPrint.push_back(std::make_pair(Namespace+"::"+
282 AWI.CGI->TheDef->getName(),
283 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +0000284 }
Chris Lattner38c07512005-01-22 20:31:17 +0000285 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
286 while (!OpsToPrint.empty())
287 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000288 O << " }";
289 }
290 O << "\n";
291 }
292
293 O << " break;\n";
294}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000295
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000296void AsmWriterEmitter::run(std::ostream &O) {
297 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
298
299 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +0000300 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000301 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
302 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Chris Lattner175580c2004-08-14 22:50:53 +0000303
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000304 O <<
305 "/// printInstruction - This method is automatically generated by tablegen\n"
306 "/// from the instruction set description. This method returns true if the\n"
307 "/// machine instruction was sufficiently described to print it, otherwise\n"
308 "/// it returns false.\n"
Chris Lattner953c6fe2004-10-03 20:19:02 +0000309 "bool " << Target.getName() << ClassName
Chris Lattner175580c2004-08-14 22:50:53 +0000310 << "::printInstruction(const MachineInstr *MI) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000311
312 std::string Namespace = Target.inst_begin()->second.Namespace;
313
Chris Lattner5765dba2005-01-22 17:40:38 +0000314 std::vector<AsmWriterInst> Instructions;
315
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000316 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
317 E = Target.inst_end(); I != E; ++I)
Chris Lattner5765dba2005-01-22 17:40:38 +0000318 if (!I->second.AsmString.empty())
319 Instructions.push_back(AsmWriterInst(I->second, Variant));
Chris Lattner076efa72004-08-01 07:43:02 +0000320
Chris Lattnerf8766682005-01-22 19:22:23 +0000321 // If all of the instructions start with a constant string (a very very common
322 // occurance), emit all of the constant strings as a big table lookup instead
Misha Brukman3da94ae2005-04-22 00:00:37 +0000323 // of requiring a switch for them.
Chris Lattnerf8766682005-01-22 19:22:23 +0000324 bool AllStartWithString = true;
325
326 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
327 if (Instructions[i].Operands.empty() ||
328 Instructions[i].Operands[0].OperandType !=
329 AsmWriterOperand::isLiteralTextOperand) {
330 AllStartWithString = false;
331 break;
332 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000333
Chris Lattner0cfcc1e2006-01-27 02:10:50 +0000334 std::vector<const CodeGenInstruction*> NumberedInstructions;
335 Target.getInstructionsByEnumValue(NumberedInstructions);
336
Chris Lattnerf8766682005-01-22 19:22:23 +0000337 if (AllStartWithString) {
338 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
339 // all machine instructions are necessarily being printed, so there may be
340 // target instructions not in this map.
341 std::map<const CodeGenInstruction*, AsmWriterInst*> CGIAWIMap;
342 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
343 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
344
345 // Emit a table of constant strings.
Chris Lattnerf8766682005-01-22 19:22:23 +0000346 O << " static const char * const OpStrs[] = {\n";
347 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
348 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
349 if (AWI == 0) {
350 // Something not handled by the asmwriter printer.
351 O << " 0,\t// ";
352 } else {
353 O << " \"" << AWI->Operands[0].Str << "\",\t// ";
354 // Nuke the string from the operand list. It is now handled!
355 AWI->Operands.erase(AWI->Operands.begin());
356 }
357 O << NumberedInstructions[i]->TheDef->getName() << "\n";
358 }
359 O << " };\n\n"
360 << " // Emit the opcode for the instruction.\n"
361 << " if (const char *AsmStr = OpStrs[MI->getOpcode()])\n"
362 << " O << AsmStr;\n\n";
363 }
364
Chris Lattner870c0162005-01-22 18:38:13 +0000365 // Because this is a vector we want to emit from the end. Reverse all of the
366 // elements in the vector.
367 std::reverse(Instructions.begin(), Instructions.end());
Chris Lattnerf8766682005-01-22 19:22:23 +0000368
Chris Lattner0cfcc1e2006-01-27 02:10:50 +0000369 // Find the opcode # of inline asm
Chris Lattnerf8766682005-01-22 19:22:23 +0000370 O << " switch (MI->getOpcode()) {\n"
Chris Lattner0cfcc1e2006-01-27 02:10:50 +0000371 " default: return false;\n"
372 " case " << NumberedInstructions.back()->Namespace
373 << "::INLINEASM: printInlineAsm(MI); break;\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000374
Chris Lattner870c0162005-01-22 18:38:13 +0000375 while (!Instructions.empty())
376 EmitInstructions(Instructions, O);
377
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000378 O << " }\n"
379 " return true;\n"
380 "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000381}