blob: b75e960dc3677ee1408a7cd4ca0d380df7584b0f [file] [log] [blame]
Chris Lattner1c4ae852004-08-01 05:59:33 +00001//===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Chris Lattner1c4ae852004-08-01 05:59:33 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
Chris Lattner1c4ae852004-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
Sean Callananb7e8f4a2010-02-09 21:50:41 +000015#include "AsmWriterInst.h"
Chris Lattner1c4ae852004-08-01 05:59:33 +000016#include "CodeGenTarget.h"
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +000017#include "SequenceToOffsetTable.h"
Craig Topperb6350132012-07-27 06:44:02 +000018#include "llvm/ADT/StringExtras.h"
Owen Andersona84be6c2011-06-27 21:06:21 +000019#include "llvm/ADT/Twine.h"
Chris Lattner692374c2006-07-18 17:18:03 +000020#include "llvm/Support/Debug.h"
Benjamin Kramer17c17bc2013-09-11 15:42:16 +000021#include "llvm/Support/Format.h"
Chris Lattner692374c2006-07-18 17:18:03 +000022#include "llvm/Support/MathExtras.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000023#include "llvm/TableGen/Error.h"
24#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000025#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenda636b32005-01-22 18:50:10 +000026#include <algorithm>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000027#include <cassert>
28#include <map>
29#include <vector>
Chris Lattner1c4ae852004-08-01 05:59:33 +000030using namespace llvm;
31
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000032namespace {
33class AsmWriterEmitter {
34 RecordKeeper &Records;
Ahmed Bougachabd214002013-10-28 18:07:17 +000035 CodeGenTarget Target;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000036 std::map<const CodeGenInstruction*, AsmWriterInst*> CGIAWIMap;
Craig Topper4c6129a2014-02-05 07:56:49 +000037 const std::vector<const CodeGenInstruction*> *NumberedInstructions;
Ahmed Bougachabd214002013-10-28 18:07:17 +000038 std::vector<AsmWriterInst> Instructions;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000039public:
Ahmed Bougachabd214002013-10-28 18:07:17 +000040 AsmWriterEmitter(RecordKeeper &R);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000041
42 void run(raw_ostream &o);
43
44private:
45 void EmitPrintInstruction(raw_ostream &o);
46 void EmitGetRegisterName(raw_ostream &o);
47 void EmitPrintAliasInstruction(raw_ostream &O);
48
49 AsmWriterInst *getAsmWriterInstByID(unsigned ID) const {
Craig Topper4c6129a2014-02-05 07:56:49 +000050 assert(ID < NumberedInstructions->size());
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000051 std::map<const CodeGenInstruction*, AsmWriterInst*>::const_iterator I =
Craig Topper4c6129a2014-02-05 07:56:49 +000052 CGIAWIMap.find(NumberedInstructions->at(ID));
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000053 assert(I != CGIAWIMap.end() && "Didn't find inst!");
54 return I->second;
55 }
56 void FindUniqueOperandCommands(std::vector<std::string> &UOC,
57 std::vector<unsigned> &InstIdxs,
58 std::vector<unsigned> &InstOpsUsed) const;
59};
60} // end anonymous namespace
61
Chris Lattner59a7f5c2005-01-22 20:31:17 +000062static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000063 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner59a7f5c2005-01-22 20:31:17 +000064 O << " case " << OpsToPrint.back().first << ": ";
65 AsmWriterOperand TheOp = OpsToPrint.back().second;
66 OpsToPrint.pop_back();
67
68 // Check to see if any other operands are identical in this list, and if so,
69 // emit a case label for them.
70 for (unsigned i = OpsToPrint.size(); i != 0; --i)
71 if (OpsToPrint[i-1].second == TheOp) {
72 O << "\n case " << OpsToPrint[i-1].first << ": ";
73 OpsToPrint.erase(OpsToPrint.begin()+i-1);
74 }
75
76 // Finally, emit the code.
Chris Lattner692374c2006-07-18 17:18:03 +000077 O << TheOp.getCode();
Chris Lattner59a7f5c2005-01-22 20:31:17 +000078 O << "break;\n";
79}
80
Chris Lattner9ceb7c82005-01-22 18:38:13 +000081
82/// EmitInstructions - Emit the last instruction in the vector and any other
83/// instructions that are suitably similar to it.
84static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000085 raw_ostream &O) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000086 AsmWriterInst FirstInst = Insts.back();
87 Insts.pop_back();
88
89 std::vector<AsmWriterInst> SimilarInsts;
90 unsigned DifferingOperand = ~0;
91 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattner92275bb2005-01-22 19:22:23 +000092 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
93 if (DiffOp != ~1U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000094 if (DifferingOperand == ~0U) // First match!
95 DifferingOperand = DiffOp;
96
97 // If this differs in the same operand as the rest of the instructions in
98 // this class, move it to the SimilarInsts list.
Chris Lattner92275bb2005-01-22 19:22:23 +000099 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000100 SimilarInsts.push_back(Insts[i-1]);
101 Insts.erase(Insts.begin()+i-1);
102 }
103 }
104 }
105
Chris Lattner017b93d2006-05-01 17:01:17 +0000106 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000107 << FirstInst.CGI->TheDef->getName() << ":\n";
108 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattner017b93d2006-05-01 17:01:17 +0000109 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000110 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
111 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
112 if (i != DifferingOperand) {
113 // If the operand is the same for all instructions, just print it.
Chris Lattner692374c2006-07-18 17:18:03 +0000114 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000115 } else {
116 // If this is the operand that varies between all of the instructions,
117 // emit a switch for just this operand now.
118 O << " switch (MI->getOpcode()) {\n";
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000119 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattner017b93d2006-05-01 17:01:17 +0000120 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000121 FirstInst.CGI->TheDef->getName(),
122 FirstInst.Operands[i]));
Misha Brukman650ba8e2005-04-22 00:00:37 +0000123
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000124 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000125 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattner017b93d2006-05-01 17:01:17 +0000126 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000127 AWI.CGI->TheDef->getName(),
128 AWI.Operands[i]));
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000129 }
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000130 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
131 while (!OpsToPrint.empty())
132 PrintCases(OpsToPrint, O);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000133 O << " }";
134 }
135 O << "\n";
136 }
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000137 O << " break;\n";
138}
Chris Lattner0c23ba52005-01-22 17:32:42 +0000139
Chris Lattner692374c2006-07-18 17:18:03 +0000140void AsmWriterEmitter::
Jim Grosbacha5497342010-09-29 22:32:50 +0000141FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattneredee5252006-07-18 18:28:27 +0000142 std::vector<unsigned> &InstIdxs,
143 std::vector<unsigned> &InstOpsUsed) const {
Craig Topper4c6129a2014-02-05 07:56:49 +0000144 InstIdxs.assign(NumberedInstructions->size(), ~0U);
Jim Grosbacha5497342010-09-29 22:32:50 +0000145
Chris Lattner692374c2006-07-18 17:18:03 +0000146 // This vector parallels UniqueOperandCommands, keeping track of which
147 // instructions each case are used for. It is a comma separated string of
148 // enums.
149 std::vector<std::string> InstrsForCase;
150 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattneredee5252006-07-18 18:28:27 +0000151 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbacha5497342010-09-29 22:32:50 +0000152
Craig Topper4c6129a2014-02-05 07:56:49 +0000153 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
Chris Lattner692374c2006-07-18 17:18:03 +0000154 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Bill Wendling004d7722010-07-16 23:10:00 +0000155 if (Inst == 0) continue; // PHI, INLINEASM, PROLOG_LABEL, etc.
Jim Grosbacha5497342010-09-29 22:32:50 +0000156
Chris Lattner692374c2006-07-18 17:18:03 +0000157 std::string Command;
Chris Lattnercb0c8482006-07-18 17:56:07 +0000158 if (Inst->Operands.empty())
Chris Lattner692374c2006-07-18 17:18:03 +0000159 continue; // Instruction already done.
Chris Lattner9d250692006-07-18 17:50:22 +0000160
Chris Lattnercb0c8482006-07-18 17:56:07 +0000161 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner9d250692006-07-18 17:50:22 +0000162
Chris Lattner692374c2006-07-18 17:18:03 +0000163 // Check to see if we already have 'Command' in UniqueOperandCommands.
164 // If not, add it.
165 bool FoundIt = false;
166 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
167 if (UniqueOperandCommands[idx] == Command) {
168 InstIdxs[i] = idx;
169 InstrsForCase[idx] += ", ";
170 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
171 FoundIt = true;
172 break;
173 }
174 if (!FoundIt) {
175 InstIdxs[i] = UniqueOperandCommands.size();
176 UniqueOperandCommands.push_back(Command);
177 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattneredee5252006-07-18 18:28:27 +0000178
179 // This command matches one operand so far.
180 InstOpsUsed.push_back(1);
181 }
182 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000183
Chris Lattneredee5252006-07-18 18:28:27 +0000184 // For each entry of UniqueOperandCommands, there is a set of instructions
185 // that uses it. If the next command of all instructions in the set are
186 // identical, fold it into the command.
187 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
188 CommandIdx != e; ++CommandIdx) {
Jim Grosbacha5497342010-09-29 22:32:50 +0000189
Chris Lattneredee5252006-07-18 18:28:27 +0000190 for (unsigned Op = 1; ; ++Op) {
191 // Scan for the first instruction in the set.
192 std::vector<unsigned>::iterator NIT =
193 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
194 if (NIT == InstIdxs.end()) break; // No commonality.
195
196 // If this instruction has no more operands, we isn't anything to merge
197 // into this command.
Jim Grosbacha5497342010-09-29 22:32:50 +0000198 const AsmWriterInst *FirstInst =
Chris Lattneredee5252006-07-18 18:28:27 +0000199 getAsmWriterInstByID(NIT-InstIdxs.begin());
200 if (!FirstInst || FirstInst->Operands.size() == Op)
201 break;
202
203 // Otherwise, scan to see if all of the other instructions in this command
204 // set share the operand.
205 bool AllSame = true;
David Greene5b4bc262009-07-29 20:10:24 +0000206 // Keep track of the maximum, number of operands or any
207 // instruction we see in the group.
208 size_t MaxSize = FirstInst->Operands.size();
209
Chris Lattneredee5252006-07-18 18:28:27 +0000210 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
211 NIT != InstIdxs.end();
212 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
213 // Okay, found another instruction in this command set. If the operand
214 // matches, we're ok, otherwise bail out.
Jim Grosbacha5497342010-09-29 22:32:50 +0000215 const AsmWriterInst *OtherInst =
Chris Lattneredee5252006-07-18 18:28:27 +0000216 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greene5b4bc262009-07-29 20:10:24 +0000217
218 if (OtherInst &&
219 OtherInst->Operands.size() > FirstInst->Operands.size())
220 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
221
Chris Lattneredee5252006-07-18 18:28:27 +0000222 if (!OtherInst || OtherInst->Operands.size() == Op ||
223 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
224 AllSame = false;
225 break;
226 }
227 }
228 if (!AllSame) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000229
Chris Lattneredee5252006-07-18 18:28:27 +0000230 // Okay, everything in this command set has the same next operand. Add it
231 // to UniqueOperandCommands and remember that it was consumed.
232 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000233
Chris Lattneredee5252006-07-18 18:28:27 +0000234 UniqueOperandCommands[CommandIdx] += Command;
235 InstOpsUsed[CommandIdx]++;
Chris Lattner692374c2006-07-18 17:18:03 +0000236 }
237 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000238
Chris Lattner692374c2006-07-18 17:18:03 +0000239 // Prepend some of the instructions each case is used for onto the case val.
240 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
241 std::string Instrs = InstrsForCase[i];
242 if (Instrs.size() > 70) {
243 Instrs.erase(Instrs.begin()+70, Instrs.end());
244 Instrs += "...";
245 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000246
Chris Lattner692374c2006-07-18 17:18:03 +0000247 if (!Instrs.empty())
Jim Grosbacha5497342010-09-29 22:32:50 +0000248 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattner692374c2006-07-18 17:18:03 +0000249 UniqueOperandCommands[i];
250 }
251}
252
253
Daniel Dunbar04f049f2009-10-17 20:43:42 +0000254static void UnescapeString(std::string &Str) {
255 for (unsigned i = 0; i != Str.size(); ++i) {
256 if (Str[i] == '\\' && i != Str.size()-1) {
257 switch (Str[i+1]) {
258 default: continue; // Don't execute the code after the switch.
259 case 'a': Str[i] = '\a'; break;
260 case 'b': Str[i] = '\b'; break;
261 case 'e': Str[i] = 27; break;
262 case 'f': Str[i] = '\f'; break;
263 case 'n': Str[i] = '\n'; break;
264 case 'r': Str[i] = '\r'; break;
265 case 't': Str[i] = '\t'; break;
266 case 'v': Str[i] = '\v'; break;
267 case '"': Str[i] = '\"'; break;
268 case '\'': Str[i] = '\''; break;
269 case '\\': Str[i] = '\\'; break;
270 }
271 // Nuke the second character.
272 Str.erase(Str.begin()+i+1);
273 }
274 }
275}
276
Chris Lattner06c5eed2009-09-13 20:08:00 +0000277/// EmitPrintInstruction - Generate the code for the "printInstruction" method
Ahmed Bougachabd214002013-10-28 18:07:17 +0000278/// implementation. Destroys all instances of AsmWriterInst information, by
279/// clearing the Instructions vector.
Chris Lattner06c5eed2009-09-13 20:08:00 +0000280void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner6ffa5012004-08-14 22:50:53 +0000281 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner72770f52004-10-03 20:19:02 +0000282 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jim Grosbacha5497342010-09-29 22:32:50 +0000283
Chris Lattner1c4ae852004-08-01 05:59:33 +0000284 O <<
285 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner06c5eed2009-09-13 20:08:00 +0000286 "/// from the instruction set description.\n"
Chris Lattnerb94284b2009-08-08 01:32:19 +0000287 "void " << Target.getName() << ClassName
Roman Divacky9dc6df52014-01-10 22:59:49 +0000288 << "::printInstruction(const MCInst *MI, raw_ostream &O) {\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000289
Chris Lattnere32982c2006-07-14 22:59:11 +0000290 // Build an aggregate string, and build a table of offsets into it.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000291 SequenceToOffsetTable<std::string> StringTable;
Jim Grosbacha5497342010-09-29 22:32:50 +0000292
Chris Lattner5d751b42006-09-27 16:44:09 +0000293 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000294 /// chunk of the output as well as indices used for operand printing.
Manman Ren68cf9fc2012-09-13 17:43:46 +0000295 /// To reduce the number of unhandled cases, we expand the size from 32-bit
296 /// to 32+16 = 48-bit.
Craig Topper06cec4c2012-09-14 08:33:11 +0000297 std::vector<uint64_t> OpcodeInfo;
Jim Grosbacha5497342010-09-29 22:32:50 +0000298
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000299 // Add all strings to the string table upfront so it can generate an optimized
300 // representation.
Craig Topper4c6129a2014-02-05 07:56:49 +0000301 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
302 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions->at(i)];
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000303 if (AWI != 0 &&
Jim Grosbachf4e67082012-04-18 18:56:33 +0000304 AWI->Operands[0].OperandType ==
305 AsmWriterOperand::isLiteralTextOperand &&
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000306 !AWI->Operands[0].Str.empty()) {
307 std::string Str = AWI->Operands[0].Str;
308 UnescapeString(Str);
309 StringTable.add(Str);
310 }
311 }
312
313 StringTable.layout();
314
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000315 unsigned MaxStringIdx = 0;
Craig Topper4c6129a2014-02-05 07:56:49 +0000316 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
317 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions->at(i)];
Chris Lattnere32982c2006-07-14 22:59:11 +0000318 unsigned Idx;
Chris Lattner36504652006-07-19 01:39:06 +0000319 if (AWI == 0) {
Chris Lattnere32982c2006-07-14 22:59:11 +0000320 // Something not handled by the asmwriter printer.
Chris Lattnerb47ed612009-09-14 01:16:36 +0000321 Idx = ~0U;
Jim Grosbacha5497342010-09-29 22:32:50 +0000322 } else if (AWI->Operands[0].OperandType !=
Chris Lattner36504652006-07-19 01:39:06 +0000323 AsmWriterOperand::isLiteralTextOperand ||
324 AWI->Operands[0].Str.empty()) {
325 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000326 Idx = StringTable.get("");
Chris Lattnere32982c2006-07-14 22:59:11 +0000327 } else {
Chris Lattnerb47ed612009-09-14 01:16:36 +0000328 std::string Str = AWI->Operands[0].Str;
329 UnescapeString(Str);
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000330 Idx = StringTable.get(Str);
Chris Lattnerb47ed612009-09-14 01:16:36 +0000331 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbacha5497342010-09-29 22:32:50 +0000332
Chris Lattnere32982c2006-07-14 22:59:11 +0000333 // Nuke the string from the operand list. It is now handled!
334 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattner92275bb2005-01-22 19:22:23 +0000335 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000336
Chris Lattnerb47ed612009-09-14 01:16:36 +0000337 // Bias offset by one since we want 0 as a sentinel.
Craig Topper06cec4c2012-09-14 08:33:11 +0000338 OpcodeInfo.push_back(Idx+1);
Chris Lattner92275bb2005-01-22 19:22:23 +0000339 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000340
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000341 // Figure out how many bits we used for the string index.
Chris Lattnerb47ed612009-09-14 01:16:36 +0000342 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbacha5497342010-09-29 22:32:50 +0000343
Chris Lattner692374c2006-07-18 17:18:03 +0000344 // To reduce code size, we compactify common instructions into a few bits
345 // in the opcode-indexed table.
Craig Topper06cec4c2012-09-14 08:33:11 +0000346 unsigned BitsLeft = 64-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000347
348 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
Jim Grosbacha5497342010-09-29 22:32:50 +0000349
Chris Lattnercb0c8482006-07-18 17:56:07 +0000350 while (1) {
Chris Lattner692374c2006-07-18 17:18:03 +0000351 std::vector<std::string> UniqueOperandCommands;
Chris Lattner692374c2006-07-18 17:18:03 +0000352 std::vector<unsigned> InstIdxs;
Chris Lattneredee5252006-07-18 18:28:27 +0000353 std::vector<unsigned> NumInstOpsHandled;
354 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
355 NumInstOpsHandled);
Jim Grosbacha5497342010-09-29 22:32:50 +0000356
Chris Lattner692374c2006-07-18 17:18:03 +0000357 // If we ran out of operands to print, we're done.
358 if (UniqueOperandCommands.empty()) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000359
Chris Lattner692374c2006-07-18 17:18:03 +0000360 // Compute the number of bits we need to represent these cases, this is
361 // ceil(log2(numentries)).
362 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbacha5497342010-09-29 22:32:50 +0000363
Chris Lattner692374c2006-07-18 17:18:03 +0000364 // If we don't have enough bits for this operand, don't include it.
365 if (NumBits > BitsLeft) {
Chris Lattner34822f62009-08-23 04:44:11 +0000366 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
367 << " more bits\n");
Chris Lattner692374c2006-07-18 17:18:03 +0000368 break;
369 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000370
Chris Lattner692374c2006-07-18 17:18:03 +0000371 // Otherwise, we can include this in the initial lookup table. Add it in.
Chris Lattner692374c2006-07-18 17:18:03 +0000372 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Manman Ren68cf9fc2012-09-13 17:43:46 +0000373 if (InstIdxs[i] != ~0U) {
Craig Topper06cec4c2012-09-14 08:33:11 +0000374 OpcodeInfo[i] |= (uint64_t)InstIdxs[i] << (64-BitsLeft);
Manman Ren68cf9fc2012-09-13 17:43:46 +0000375 }
Craig Topper06cec4c2012-09-14 08:33:11 +0000376 BitsLeft -= NumBits;
Jim Grosbacha5497342010-09-29 22:32:50 +0000377
Chris Lattnercb0c8482006-07-18 17:56:07 +0000378 // Remove the info about this operand.
Craig Topper4c6129a2014-02-05 07:56:49 +0000379 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
Chris Lattnercb0c8482006-07-18 17:56:07 +0000380 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattneredee5252006-07-18 18:28:27 +0000381 if (!Inst->Operands.empty()) {
382 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner6e172082006-07-18 19:06:01 +0000383 assert(NumOps <= Inst->Operands.size() &&
384 "Can't remove this many ops!");
Chris Lattneredee5252006-07-18 18:28:27 +0000385 Inst->Operands.erase(Inst->Operands.begin(),
386 Inst->Operands.begin()+NumOps);
387 }
Chris Lattnercb0c8482006-07-18 17:56:07 +0000388 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000389
Chris Lattnercb0c8482006-07-18 17:56:07 +0000390 // Remember the handlers for this set of operands.
Chris Lattner692374c2006-07-18 17:18:03 +0000391 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
392 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000393
394
Craig Topper06cec4c2012-09-14 08:33:11 +0000395 // We always emit at least one 32-bit table. A second table is emitted if
396 // more bits are needed.
397 O<<" static const uint32_t OpInfo[] = {\n";
Craig Topper4c6129a2014-02-05 07:56:49 +0000398 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
Craig Topper06cec4c2012-09-14 08:33:11 +0000399 O << " " << (OpcodeInfo[i] & 0xffffffff) << "U,\t// "
Craig Topper4c6129a2014-02-05 07:56:49 +0000400 << NumberedInstructions->at(i)->TheDef->getName() << "\n";
Chris Lattner692374c2006-07-18 17:18:03 +0000401 }
402 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000403 O << " 0U\n";
Chris Lattnere32982c2006-07-14 22:59:11 +0000404 O << " };\n\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000405
Craig Topper06cec4c2012-09-14 08:33:11 +0000406 if (BitsLeft < 32) {
Manman Ren68cf9fc2012-09-13 17:43:46 +0000407 // Add a second OpInfo table only when it is necessary.
Craig Topper06cec4c2012-09-14 08:33:11 +0000408 // Adjust the type of the second table based on the number of bits needed.
409 O << " static const uint"
410 << ((BitsLeft < 16) ? "32" : (BitsLeft < 24) ? "16" : "8")
411 << "_t OpInfo2[] = {\n";
Craig Topper4c6129a2014-02-05 07:56:49 +0000412 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
Craig Topper06cec4c2012-09-14 08:33:11 +0000413 O << " " << (OpcodeInfo[i] >> 32) << "U,\t// "
Craig Topper4c6129a2014-02-05 07:56:49 +0000414 << NumberedInstructions->at(i)->TheDef->getName() << "\n";
Manman Ren68cf9fc2012-09-13 17:43:46 +0000415 }
416 // Add a dummy entry so the array init doesn't end with a comma.
417 O << " 0U\n";
418 O << " };\n\n";
419 }
420
Chris Lattnere32982c2006-07-14 22:59:11 +0000421 // Emit the string itself.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000422 O << " const char AsmStrs[] = {\n";
423 StringTable.emit(O, printChar);
424 O << " };\n\n";
Chris Lattnere32982c2006-07-14 22:59:11 +0000425
Evan Cheng32e53472008-02-02 08:39:46 +0000426 O << " O << \"\\t\";\n\n";
427
Craig Topper06cec4c2012-09-14 08:33:11 +0000428 O << " // Emit the opcode for the instruction.\n";
429 if (BitsLeft < 32) {
430 // If we have two tables then we need to perform two lookups and combine
431 // the results into a single 64-bit value.
432 O << " uint64_t Bits1 = OpInfo[MI->getOpcode()];\n"
433 << " uint64_t Bits2 = OpInfo2[MI->getOpcode()];\n"
434 << " uint64_t Bits = (Bits2 << 32) | Bits1;\n";
435 } else {
436 // If only one table is used we just need to perform a single lookup.
437 O << " uint32_t Bits = OpInfo[MI->getOpcode()];\n";
438 }
Manman Ren68cf9fc2012-09-13 17:43:46 +0000439 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattnerb47ed612009-09-14 01:16:36 +0000440 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenefdd25192009-08-05 21:00:52 +0000441
Chris Lattner692374c2006-07-18 17:18:03 +0000442 // Output the table driven operand information.
Craig Topper06cec4c2012-09-14 08:33:11 +0000443 BitsLeft = 64-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000444 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
445 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
446
447 // Compute the number of bits we need to represent these cases, this is
448 // ceil(log2(numentries)).
449 unsigned NumBits = Log2_32_Ceil(Commands.size());
450 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbacha5497342010-09-29 22:32:50 +0000451
Chris Lattner692374c2006-07-18 17:18:03 +0000452 // Emit code to extract this field from Bits.
Chris Lattner692374c2006-07-18 17:18:03 +0000453 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000454 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000455
Chris Lattneredee5252006-07-18 18:28:27 +0000456 if (Commands.size() == 2) {
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000457 // Emit two possibilitys with if/else.
Craig Topper06cec4c2012-09-14 08:33:11 +0000458 O << " if ((Bits >> "
459 << (64-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000460 << ((1 << NumBits)-1) << ") {\n"
461 << Commands[1]
462 << " } else {\n"
463 << Commands[0]
464 << " }\n\n";
Eric Christophera573d192010-09-18 18:50:27 +0000465 } else if (Commands.size() == 1) {
466 // Emit a single possibility.
467 O << Commands[0] << "\n\n";
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000468 } else {
Craig Topper06cec4c2012-09-14 08:33:11 +0000469 O << " switch ((Bits >> "
470 << (64-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000471 << ((1 << NumBits)-1) << ") {\n"
472 << " default: // unreachable.\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000473
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000474 // Print out all the cases.
475 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
476 O << " case " << i << ":\n";
477 O << Commands[i];
478 O << " break;\n";
479 }
480 O << " }\n\n";
Chris Lattner692374c2006-07-18 17:18:03 +0000481 }
Craig Topper06cec4c2012-09-14 08:33:11 +0000482 BitsLeft -= NumBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000483 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000484
Chris Lattnercb0c8482006-07-18 17:56:07 +0000485 // Okay, delete instructions with no operand info left.
Chris Lattner692374c2006-07-18 17:18:03 +0000486 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
487 // Entire instruction has been emitted?
488 AsmWriterInst &Inst = Instructions[i];
Chris Lattnercb0c8482006-07-18 17:56:07 +0000489 if (Inst.Operands.empty()) {
Chris Lattner692374c2006-07-18 17:18:03 +0000490 Instructions.erase(Instructions.begin()+i);
Chris Lattnercb0c8482006-07-18 17:56:07 +0000491 --i; --e;
Chris Lattner692374c2006-07-18 17:18:03 +0000492 }
493 }
494
Jim Grosbacha5497342010-09-29 22:32:50 +0000495
Chris Lattner692374c2006-07-18 17:18:03 +0000496 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000497 // elements in the vector.
498 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbacha5497342010-09-29 22:32:50 +0000499
500
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000501 // Now that we've emitted all of the operand info that fit into 32 bits, emit
502 // information for those instructions that are left. This is a less dense
503 // encoding, but we expect the main 32-bit table to handle the majority of
504 // instructions.
Chris Lattner66e288b2006-07-18 17:38:46 +0000505 if (!Instructions.empty()) {
506 // Find the opcode # of inline asm.
507 O << " switch (MI->getOpcode()) {\n";
508 while (!Instructions.empty())
509 EmitInstructions(Instructions, O);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000510
Chris Lattner66e288b2006-07-18 17:38:46 +0000511 O << " }\n";
Chris Lattnerb94284b2009-08-08 01:32:19 +0000512 O << " return;\n";
Chris Lattner66e288b2006-07-18 17:38:46 +0000513 }
David Greene5b4bc262009-07-29 20:10:24 +0000514
Chris Lattner6e172082006-07-18 19:06:01 +0000515 O << "}\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000516}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000517
Owen Andersona84be6c2011-06-27 21:06:21 +0000518static void
519emitRegisterNameString(raw_ostream &O, StringRef AltName,
Craig Topper9c252eb2012-04-03 06:52:47 +0000520 const std::vector<CodeGenRegister*> &Registers) {
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000521 SequenceToOffsetTable<std::string> StringTable;
522 SmallVector<std::string, 4> AsmNames(Registers.size());
Owen Andersona84be6c2011-06-27 21:06:21 +0000523 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
524 const CodeGenRegister &Reg = *Registers[i];
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000525 std::string &AsmName = AsmNames[i];
Owen Andersona84be6c2011-06-27 21:06:21 +0000526
Owen Andersona84be6c2011-06-27 21:06:21 +0000527 // "NoRegAltName" is special. We don't need to do a lookup for that,
528 // as it's just a reference to the default register name.
529 if (AltName == "" || AltName == "NoRegAltName") {
530 AsmName = Reg.TheDef->getValueAsString("AsmName");
531 if (AsmName.empty())
532 AsmName = Reg.getName();
533 } else {
534 // Make sure the register has an alternate name for this index.
535 std::vector<Record*> AltNameList =
536 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
537 unsigned Idx = 0, e;
538 for (e = AltNameList.size();
539 Idx < e && (AltNameList[Idx]->getName() != AltName);
540 ++Idx)
541 ;
542 // If the register has an alternate name for this index, use it.
543 // Otherwise, leave it empty as an error flag.
544 if (Idx < e) {
545 std::vector<std::string> AltNames =
546 Reg.TheDef->getValueAsListOfStrings("AltNames");
547 if (AltNames.size() <= Idx)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000548 PrintFatalError(Reg.TheDef->getLoc(),
549 (Twine("Register definition missing alt name for '") +
550 AltName + "'.").str());
Owen Andersona84be6c2011-06-27 21:06:21 +0000551 AsmName = AltNames[Idx];
552 }
553 }
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000554 StringTable.add(AsmName);
555 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000556
Craig Topperf8f0a232012-09-15 01:22:42 +0000557 StringTable.layout();
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000558 O << " static const char AsmStrs" << AltName << "[] = {\n";
559 StringTable.emit(O, printChar);
560 O << " };\n\n";
561
Craig Topperf8f0a232012-09-15 01:22:42 +0000562 O << " static const uint32_t RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000563 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Topper7a2cea12012-04-02 00:47:39 +0000564 if ((i % 14) == 0)
565 O << "\n ";
566 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersona84be6c2011-06-27 21:06:21 +0000567 }
Craig Topper9c252eb2012-04-03 06:52:47 +0000568 O << "\n };\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000569 << "\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000570}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000571
572void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +0000573 Record *AsmWriter = Target.getAsmWriter();
574 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jakob Stoklund Olesen8e188be2011-06-18 04:26:06 +0000575 const std::vector<CodeGenRegister*> &Registers =
576 Target.getRegBank().getRegisters();
Owen Andersona84be6c2011-06-27 21:06:21 +0000577 std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices();
578 bool hasAltNames = AltNameIndices.size() > 1;
Jim Grosbacha5497342010-09-29 22:32:50 +0000579
Chris Lattner06c5eed2009-09-13 20:08:00 +0000580 O <<
581 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
582 "/// from the register set description. This returns the assembler name\n"
583 "/// for the specified register.\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000584 "const char *" << Target.getName() << ClassName << "::";
585 if (hasAltNames)
586 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
587 else
588 O << "getRegisterName(unsigned RegNo) {\n";
589 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
590 << " && \"Invalid register number!\");\n"
Chris Lattnera7e8ae42009-09-14 01:26:18 +0000591 << "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000592
Owen Andersona84be6c2011-06-27 21:06:21 +0000593 if (hasAltNames) {
594 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i)
595 emitRegisterNameString(O, AltNameIndices[i]->getName(), Registers);
596 } else
597 emitRegisterNameString(O, "", Registers);
Jim Grosbacha5497342010-09-29 22:32:50 +0000598
Owen Andersona84be6c2011-06-27 21:06:21 +0000599 if (hasAltNames) {
Craig Topperf8f0a232012-09-15 01:22:42 +0000600 O << " const uint32_t *RegAsmOffset;\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000601 << " const char *AsmStrs;\n"
602 << " switch(AltIdx) {\n"
Craig Topperc4965bc2012-02-05 07:21:30 +0000603 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000604 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) {
605 StringRef Namespace = AltNameIndices[1]->getValueAsString("Namespace");
606 StringRef AltName(AltNameIndices[i]->getName());
607 O << " case " << Namespace << "::" << AltName
608 << ":\n"
609 << " AsmStrs = AsmStrs" << AltName << ";\n"
610 << " RegAsmOffset = RegAsmOffset" << AltName << ";\n"
611 << " break;\n";
612 }
613 O << "}\n";
614 }
615
616 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
617 << " \"Invalid alt name index for register!\");\n"
618 << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
Chris Lattner06c5eed2009-09-13 20:08:00 +0000619 << "}\n";
620}
621
Bill Wendling7e5771d2011-03-21 08:31:53 +0000622namespace {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000623// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
624// they both have the same conditionals. In which case, we cannot print out the
625// alias for that pattern.
626class IAPrinter {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000627 std::vector<std::string> Conds;
628 std::map<StringRef, unsigned> OpMap;
629 std::string Result;
630 std::string AsmString;
Jim Grosbachdba3f5b2012-04-18 19:02:43 +0000631 SmallVector<Record*, 4> ReqFeatures;
Bill Wendling5d3174c2011-03-21 08:40:31 +0000632public:
Evan Cheng4d806e22011-07-06 02:02:33 +0000633 IAPrinter(std::string R, std::string AS)
634 : Result(R), AsmString(AS) {}
Bill Wendling5d3174c2011-03-21 08:40:31 +0000635
636 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000637
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000638 void addOperand(StringRef Op, unsigned Idx) {
639 assert(Idx < 0xFF && "Index too large!");
640 OpMap[Op] = Idx;
641 }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000642 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
643 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
644
Evan Cheng4d806e22011-07-06 02:02:33 +0000645 void print(raw_ostream &O) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000646 if (Conds.empty() && ReqFeatures.empty()) {
647 O.indent(6) << "return true;\n";
Evan Cheng4d806e22011-07-06 02:02:33 +0000648 return;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000649 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000650
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000651 O << "if (";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000652
653 for (std::vector<std::string>::iterator
654 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
655 if (I != Conds.begin()) {
656 O << " &&\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000657 O.indent(8);
Bill Wendling5d3174c2011-03-21 08:40:31 +0000658 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000659
Bill Wendling5d3174c2011-03-21 08:40:31 +0000660 O << *I;
661 }
662
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000663 O << ") {\n";
664 O.indent(6) << "// " << Result << "\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000665
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000666 // Directly mangle mapped operands into the string. Each operand is
667 // identified by a '$' sign followed by a byte identifying the number of the
668 // operand. We add one to the index to avoid zero bytes.
669 std::pair<StringRef, StringRef> ASM = StringRef(AsmString).split(' ');
670 SmallString<128> OutString = ASM.first;
671 if (!ASM.second.empty()) {
672 raw_svector_ostream OS(OutString);
673 OS << ' ';
674 for (StringRef::iterator I = ASM.second.begin(), E = ASM.second.end();
675 I != E;) {
676 OS << *I;
677 if (*I == '$') {
678 StringRef::iterator Start = ++I;
679 while (I != E &&
680 ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') ||
681 (*I >= '0' && *I <= '9') || *I == '_'))
682 ++I;
683 StringRef Name(Start, I - Start);
684 assert(isOpMapped(Name) && "Unmapped operand!");
685 OS << format("\\x%02X", (unsigned char)getOpIndex(Name) + 1);
686 } else {
687 ++I;
688 }
689 }
690 }
691
692 // Emit the string.
693 O.indent(6) << "AsmString = \"" << OutString.str() << "\";\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000694
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000695 O.indent(6) << "break;\n";
696 O.indent(4) << '}';
Bill Wendling5d3174c2011-03-21 08:40:31 +0000697 }
698
699 bool operator==(const IAPrinter &RHS) {
700 if (Conds.size() != RHS.Conds.size())
701 return false;
702
703 unsigned Idx = 0;
704 for (std::vector<std::string>::iterator
705 I = Conds.begin(), E = Conds.end(); I != E; ++I)
706 if (*I != RHS.Conds[Idx++])
707 return false;
708
709 return true;
710 }
711
712 bool operator()(const IAPrinter &RHS) {
713 if (Conds.size() < RHS.Conds.size())
714 return true;
715
716 unsigned Idx = 0;
717 for (std::vector<std::string>::iterator
718 I = Conds.begin(), E = Conds.end(); I != E; ++I)
719 if (*I != RHS.Conds[Idx++])
720 return *I < RHS.Conds[Idx++];
721
722 return false;
723 }
724};
725
Bill Wendling7e5771d2011-03-21 08:31:53 +0000726} // end anonymous namespace
727
Bill Wendlinge7124492011-06-14 03:17:20 +0000728static unsigned CountNumOperands(StringRef AsmString) {
729 unsigned NumOps = 0;
730 std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
731
732 while (!ASM.second.empty()) {
733 ++NumOps;
734 ASM = ASM.second.split(' ');
735 }
736
737 return NumOps;
738}
739
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000740static unsigned CountResultNumOperands(StringRef AsmString) {
741 unsigned NumOps = 0;
742 std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
743
744 if (!ASM.second.empty()) {
745 size_t I = ASM.second.find('{');
746 StringRef Str = ASM.second;
747 if (I != StringRef::npos)
748 Str = ASM.second.substr(I, ASM.second.find('|', I));
749
750 ASM = Str.split(' ');
751
752 do {
753 ++NumOps;
754 ASM = ASM.second.split(' ');
755 } while (!ASM.second.empty());
756 }
757
758 return NumOps;
759}
Bill Wendlinge7124492011-06-14 03:17:20 +0000760
Bill Wendling7e5771d2011-03-21 08:31:53 +0000761void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
Bill Wendling7e5771d2011-03-21 08:31:53 +0000762 Record *AsmWriter = Target.getAsmWriter();
763
764 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
765 O << "#undef PRINT_ALIAS_INSTR\n\n";
766
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000767 // Emit the method that prints the alias instruction.
768 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
769
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000770 std::vector<Record*> AllInstAliases =
771 Records.getAllDerivedDefinitions("InstAlias");
772
773 // Create a map from the qualified name to a list of potential matches.
Jim Grosbachefe653f2012-04-18 20:24:49 +0000774 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000775 for (std::vector<Record*>::iterator
776 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
777 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
778 const Record *R = *I;
Bill Wendling6dd69d92011-04-13 23:36:21 +0000779 if (!R->getValueAsBit("EmitAlias"))
780 continue; // We were told not to emit the alias, but to emit the aliasee.
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000781 const DagInit *DI = R->getValueAsDag("ResultInst");
Sean Silva88eb8dd2012-10-10 20:24:47 +0000782 const DefInit *Op = cast<DefInit>(DI->getOperator());
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000783 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
784 }
785
Bill Wendling7e570b52011-03-21 08:59:17 +0000786 // A map of which conditions need to be met for each instruction operand
787 // before it can be matched to the mnemonic.
Jim Grosbachefe653f2012-04-18 20:24:49 +0000788 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
Bill Wendling7e570b52011-03-21 08:59:17 +0000789
Jim Grosbachefe653f2012-04-18 20:24:49 +0000790 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
Bill Wendling7e570b52011-03-21 08:59:17 +0000791 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
792 std::vector<CodeGenInstAlias*> &Aliases = I->second;
793
794 for (std::vector<CodeGenInstAlias*>::iterator
795 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
796 const CodeGenInstAlias *CGA = *II;
Bill Wendlinge7124492011-06-14 03:17:20 +0000797 unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000798 unsigned NumResultOps =
799 CountResultNumOperands(CGA->ResultInst->AsmString);
Bill Wendlinge7124492011-06-14 03:17:20 +0000800
801 // Don't emit the alias if it has more operands than what it's aliasing.
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000802 if (NumResultOps < CountNumOperands(CGA->AsmString))
Bill Wendlinge7124492011-06-14 03:17:20 +0000803 continue;
804
Evan Cheng4d806e22011-07-06 02:02:33 +0000805 IAPrinter *IAP = new IAPrinter(CGA->Result->getAsString(),
Bill Wendling7e570b52011-03-21 08:59:17 +0000806 CGA->AsmString);
Bill Wendling7e570b52011-03-21 08:59:17 +0000807
Bill Wendling7e570b52011-03-21 08:59:17 +0000808 std::string Cond;
809 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
810 IAP->addCond(Cond);
811
Bill Wendling7e570b52011-03-21 08:59:17 +0000812 bool CantHandle = false;
813
814 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
815 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
816
817 switch (RO.Kind) {
Bill Wendling7e570b52011-03-21 08:59:17 +0000818 case CodeGenInstAlias::ResultOperand::K_Record: {
819 const Record *Rec = RO.getRecord();
820 StringRef ROName = RO.getName();
821
Owen Andersona84be6c2011-06-27 21:06:21 +0000822
823 if (Rec->isSubClassOf("RegisterOperand"))
824 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling7e570b52011-03-21 08:59:17 +0000825 if (Rec->isSubClassOf("RegisterClass")) {
826 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
827 IAP->addCond(Cond);
828
829 if (!IAP->isOpMapped(ROName)) {
830 IAP->addOperand(ROName, i);
Jack Carter9c1a0272013-02-05 08:32:10 +0000831 Record *R = CGA->ResultOperands[i].getRecord();
832 if (R->isSubClassOf("RegisterOperand"))
833 R = R->getValueAsDef("RegClass");
Benjamin Kramer682de392012-03-30 23:13:40 +0000834 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
Jack Carter9c1a0272013-02-05 08:32:10 +0000835 R->getName() + "RegClassID)"
Benjamin Kramer682de392012-03-30 23:13:40 +0000836 ".contains(MI->getOperand(" + llvm::utostr(i) + ").getReg())";
Bill Wendling7e570b52011-03-21 08:59:17 +0000837 IAP->addCond(Cond);
838 } else {
839 Cond = std::string("MI->getOperand(") +
840 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
841 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
842 IAP->addCond(Cond);
843 }
844 } else {
845 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
Bill Wendlinge7124492011-06-14 03:17:20 +0000846 // FIXME: We may need to handle these situations.
Bill Wendling7e570b52011-03-21 08:59:17 +0000847 delete IAP;
848 IAP = 0;
849 CantHandle = true;
850 break;
851 }
852
853 break;
854 }
Tim Northoverab7689e2013-01-09 13:32:04 +0000855 case CodeGenInstAlias::ResultOperand::K_Imm: {
856 std::string Op = "MI->getOperand(" + llvm::utostr(i) + ")";
857
858 // Just because the alias has an immediate result, doesn't mean the
859 // MCInst will. An MCExpr could be present, for example.
860 IAP->addCond(Op + ".isImm()");
861
862 Cond = Op + ".getImm() == "
863 + llvm::utostr(CGA->ResultOperands[i].getImm());
Bill Wendling7e570b52011-03-21 08:59:17 +0000864 IAP->addCond(Cond);
865 break;
Tim Northoverab7689e2013-01-09 13:32:04 +0000866 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000867 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000868 // If this is zero_reg, something's playing tricks we're not
869 // equipped to handle.
870 if (!CGA->ResultOperands[i].getRegister()) {
871 CantHandle = true;
872 break;
873 }
874
Bill Wendling7e570b52011-03-21 08:59:17 +0000875 Cond = std::string("MI->getOperand(") +
876 llvm::utostr(i) + ").getReg() == " + Target.getName() +
877 "::" + CGA->ResultOperands[i].getRegister()->getName();
878 IAP->addCond(Cond);
879 break;
880 }
881
882 if (!IAP) break;
883 }
884
885 if (CantHandle) continue;
Jim Grosbachefe653f2012-04-18 20:24:49 +0000886 IAPrinterMap[I->first].push_back(IAP);
Bill Wendling7e570b52011-03-21 08:59:17 +0000887 }
888 }
889
Bill Wendlingf5199de2011-05-23 00:18:33 +0000890 std::string Header;
891 raw_string_ostream HeaderO(Header);
892
893 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendlinge7124492011-06-14 03:17:20 +0000894 << "::printAliasInstr(const MCInst"
Bill Wendlingf5199de2011-05-23 00:18:33 +0000895 << " *MI, raw_ostream &OS) {\n";
Bill Wendling7e570b52011-03-21 08:59:17 +0000896
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000897 std::string Cases;
898 raw_string_ostream CasesO(Cases);
899
Jim Grosbachefe653f2012-04-18 20:24:49 +0000900 for (std::map<std::string, std::vector<IAPrinter*> >::iterator
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000901 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
902 std::vector<IAPrinter*> &IAPs = I->second;
903 std::vector<IAPrinter*> UniqueIAPs;
904
905 for (std::vector<IAPrinter*>::iterator
906 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
907 IAPrinter *LHS = *II;
908 bool IsDup = false;
909 for (std::vector<IAPrinter*>::iterator
910 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
911 IAPrinter *RHS = *III;
912 if (LHS != RHS && *LHS == *RHS) {
913 IsDup = true;
914 break;
915 }
916 }
917
918 if (!IsDup) UniqueIAPs.push_back(LHS);
919 }
920
921 if (UniqueIAPs.empty()) continue;
922
Jim Grosbachefe653f2012-04-18 20:24:49 +0000923 CasesO.indent(2) << "case " << I->first << ":\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000924
925 for (std::vector<IAPrinter*>::iterator
926 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
927 IAPrinter *IAP = *II;
928 CasesO.indent(4);
Evan Cheng4d806e22011-07-06 02:02:33 +0000929 IAP->print(CasesO);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000930 CasesO << '\n';
931 }
932
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000933 CasesO.indent(4) << "return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000934 }
935
Bill Wendlinge7124492011-06-14 03:17:20 +0000936 if (CasesO.str().empty()) {
Bill Wendlingf5199de2011-05-23 00:18:33 +0000937 O << HeaderO.str();
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000938 O << " return false;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000939 O << "}\n\n";
940 O << "#endif // PRINT_ALIAS_INSTR\n";
941 return;
942 }
943
Bill Wendlingf5199de2011-05-23 00:18:33 +0000944 O << HeaderO.str();
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000945 O.indent(2) << "const char *AsmString;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000946 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000947 O.indent(2) << "default: return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000948 O << CasesO.str();
949 O.indent(2) << "}\n\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000950
951 // Code that prints the alias, replacing the operands with the ones from the
952 // MCInst.
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000953 O << " unsigned I = 0;\n";
954 O << " while (AsmString[I] != ' ' && AsmString[I] != '\\0')\n";
955 O << " ++I;\n";
956 O << " OS << '\\t' << StringRef(AsmString, I);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000957
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000958 O << " if (AsmString[I] != '\\0') {\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000959 O << " OS << '\\t';\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000960 O << " do {\n";
961 O << " if (AsmString[I] == '$') {\n";
962 O << " ++I;\n";
963 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, OS);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000964 O << " } else {\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000965 O << " OS << AsmString[I++];\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000966 O << " }\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000967 O << " } while (AsmString[I] != '\\0');\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000968 O << " }\n\n";
Jim Grosbachf4e67082012-04-18 18:56:33 +0000969
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000970 O << " return true;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000971 O << "}\n\n";
972
973 O << "#endif // PRINT_ALIAS_INSTR\n";
974}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000975
Ahmed Bougachabd214002013-10-28 18:07:17 +0000976AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
977 Record *AsmWriter = Target.getAsmWriter();
978 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
979 E = Target.inst_end();
980 I != E; ++I)
981 if (!(*I)->AsmString.empty() && (*I)->TheDef->getName() != "PHI")
982 Instructions.push_back(
983 AsmWriterInst(**I, AsmWriter->getValueAsInt("Variant"),
Ahmed Bougachabd214002013-10-28 18:07:17 +0000984 AsmWriter->getValueAsInt("OperandSpacing")));
985
986 // Get the instruction numbering.
Craig Topper4c6129a2014-02-05 07:56:49 +0000987 NumberedInstructions = &Target.getInstructionsByEnumValue();
Ahmed Bougachabd214002013-10-28 18:07:17 +0000988
989 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
990 // all machine instructions are necessarily being printed, so there may be
991 // target instructions not in this map.
992 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
993 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
994}
995
Chris Lattner06c5eed2009-09-13 20:08:00 +0000996void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +0000997 EmitPrintInstruction(O);
998 EmitGetRegisterName(O);
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000999 EmitPrintAliasInstruction(O);
Chris Lattner06c5eed2009-09-13 20:08:00 +00001000}
1001
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001002
1003namespace llvm {
1004
1005void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1006 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1007 AsmWriterEmitter(RK).run(OS);
1008}
1009
1010} // End llvm namespace