blob: 50615689d66167e0e7c026eb1a85b6aeaa53fb34 [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//
Xinliang David Lib439c7a2016-02-23 19:18:21 +000010// This tablegen backend emits an assembly printer for the current target.
Chris Lattner1c4ae852004-08-01 05:59:33 +000011// 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"
Daniel Sandersca89f3a2016-11-19 12:21:34 +000018#include "Types.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000019#include "llvm/ADT/SmallString.h"
Craig Topperb6350132012-07-27 06:44:02 +000020#include "llvm/ADT/StringExtras.h"
Owen Andersona84be6c2011-06-27 21:06:21 +000021#include "llvm/ADT/Twine.h"
Chris Lattner692374c2006-07-18 17:18:03 +000022#include "llvm/Support/Debug.h"
Benjamin Kramer17c17bc2013-09-11 15:42:16 +000023#include "llvm/Support/Format.h"
Chris Lattner692374c2006-07-18 17:18:03 +000024#include "llvm/Support/MathExtras.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000025#include "llvm/TableGen/Error.h"
26#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000027#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenda636b32005-01-22 18:50:10 +000028#include <algorithm>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000029#include <cassert>
30#include <map>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000031#include <utility>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000032#include <vector>
Chris Lattner1c4ae852004-08-01 05:59:33 +000033using namespace llvm;
34
Chandler Carruthe96dd892014-04-21 22:55:11 +000035#define DEBUG_TYPE "asm-writer-emitter"
36
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000037namespace {
38class AsmWriterEmitter {
39 RecordKeeper &Records;
Ahmed Bougachabd214002013-10-28 18:07:17 +000040 CodeGenTarget Target;
Craig Topperf9265322016-01-17 20:38:14 +000041 ArrayRef<const CodeGenInstruction *> NumberedInstructions;
Ahmed Bougachabd214002013-10-28 18:07:17 +000042 std::vector<AsmWriterInst> Instructions;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000043public:
Ahmed Bougachabd214002013-10-28 18:07:17 +000044 AsmWriterEmitter(RecordKeeper &R);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000045
46 void run(raw_ostream &o);
47
48private:
49 void EmitPrintInstruction(raw_ostream &o);
50 void EmitGetRegisterName(raw_ostream &o);
51 void EmitPrintAliasInstruction(raw_ostream &O);
52
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000053 void FindUniqueOperandCommands(std::vector<std::string> &UOC,
Craig Topper5dd7a2c2016-01-24 07:13:28 +000054 std::vector<std::vector<unsigned>> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +000055 std::vector<unsigned> &InstOpsUsed,
56 bool PassSubtarget) const;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000057};
58} // end anonymous namespace
59
Chris Lattner59a7f5c2005-01-22 20:31:17 +000060static void PrintCases(std::vector<std::pair<std::string,
Craig Topperc24a4012016-01-14 06:15:07 +000061 AsmWriterOperand> > &OpsToPrint, raw_ostream &O,
62 bool PassSubtarget) {
Craig Topper0b271ad2016-01-13 07:20:13 +000063 O << " case " << OpsToPrint.back().first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000064 AsmWriterOperand TheOp = OpsToPrint.back().second;
65 OpsToPrint.pop_back();
66
67 // Check to see if any other operands are identical in this list, and if so,
68 // emit a case label for them.
69 for (unsigned i = OpsToPrint.size(); i != 0; --i)
70 if (OpsToPrint[i-1].second == TheOp) {
Craig Topper0b271ad2016-01-13 07:20:13 +000071 O << "\n case " << OpsToPrint[i-1].first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000072 OpsToPrint.erase(OpsToPrint.begin()+i-1);
73 }
74
75 // Finally, emit the code.
Craig Topperc24a4012016-01-14 06:15:07 +000076 O << "\n " << TheOp.getCode(PassSubtarget);
Craig Topper0b271ad2016-01-13 07:20:13 +000077 O << "\n break;\n";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000078}
79
Chris Lattner9ceb7c82005-01-22 18:38:13 +000080
81/// EmitInstructions - Emit the last instruction in the vector and any other
82/// instructions that are suitably similar to it.
83static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Craig Topperc24a4012016-01-14 06:15:07 +000084 raw_ostream &O, bool PassSubtarget) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000085 AsmWriterInst FirstInst = Insts.back();
86 Insts.pop_back();
87
88 std::vector<AsmWriterInst> SimilarInsts;
89 unsigned DifferingOperand = ~0;
90 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattner92275bb2005-01-22 19:22:23 +000091 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
92 if (DiffOp != ~1U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000093 if (DifferingOperand == ~0U) // First match!
94 DifferingOperand = DiffOp;
95
96 // If this differs in the same operand as the rest of the instructions in
97 // this class, move it to the SimilarInsts list.
Chris Lattner92275bb2005-01-22 19:22:23 +000098 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000099 SimilarInsts.push_back(Insts[i-1]);
100 Insts.erase(Insts.begin()+i-1);
101 }
102 }
103 }
104
Chris Lattner017b93d2006-05-01 17:01:17 +0000105 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000106 << FirstInst.CGI->TheDef->getName() << ":\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000107 for (const AsmWriterInst &AWI : SimilarInsts)
108 O << " case " << AWI.CGI->Namespace << "::"
109 << AWI.CGI->TheDef->getName() << ":\n";
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000110 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
111 if (i != DifferingOperand) {
112 // If the operand is the same for all instructions, just print it.
Craig Topperc24a4012016-01-14 06:15:07 +0000113 O << " " << FirstInst.Operands[i].getCode(PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000114 } else {
115 // If this is the operand that varies between all of the instructions,
116 // emit a switch for just this operand now.
117 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000118 O << " default: llvm_unreachable(\"Unexpected opcode.\");\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
Craig Topper190ecd52016-01-08 07:06:32 +0000124 for (const AsmWriterInst &AWI : SimilarInsts) {
Chris Lattner017b93d2006-05-01 17:01:17 +0000125 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000126 AWI.CGI->TheDef->getName(),
127 AWI.Operands[i]));
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000128 }
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000129 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
130 while (!OpsToPrint.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000131 PrintCases(OpsToPrint, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000132 O << " }";
133 }
134 O << "\n";
135 }
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000136 O << " break;\n";
137}
Chris Lattner0c23ba52005-01-22 17:32:42 +0000138
Chris Lattner692374c2006-07-18 17:18:03 +0000139void AsmWriterEmitter::
Jim Grosbacha5497342010-09-29 22:32:50 +0000140FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000141 std::vector<std::vector<unsigned>> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000142 std::vector<unsigned> &InstOpsUsed,
143 bool PassSubtarget) const {
Jim Grosbacha5497342010-09-29 22:32:50 +0000144
Chris Lattner692374c2006-07-18 17:18:03 +0000145 // This vector parallels UniqueOperandCommands, keeping track of which
146 // instructions each case are used for. It is a comma separated string of
147 // enums.
148 std::vector<std::string> InstrsForCase;
149 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattneredee5252006-07-18 18:28:27 +0000150 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbacha5497342010-09-29 22:32:50 +0000151
Craig Topper9e9ae602016-01-17 08:05:33 +0000152 for (size_t i = 0, e = Instructions.size(); i != e; ++i) {
153 const AsmWriterInst &Inst = Instructions[i];
154 if (Inst.Operands.empty())
Chris Lattner692374c2006-07-18 17:18:03 +0000155 continue; // Instruction already done.
Chris Lattner9d250692006-07-18 17:50:22 +0000156
Craig Topper9e9ae602016-01-17 08:05:33 +0000157 std::string Command = " "+Inst.Operands[0].getCode(PassSubtarget)+"\n";
Chris Lattner9d250692006-07-18 17:50:22 +0000158
Chris Lattner692374c2006-07-18 17:18:03 +0000159 // Check to see if we already have 'Command' in UniqueOperandCommands.
160 // If not, add it.
David Majnemer42531262016-08-12 03:55:06 +0000161 auto I = find(UniqueOperandCommands, Command);
Craig Toppera99859d2016-01-17 08:05:30 +0000162 if (I != UniqueOperandCommands.end()) {
163 size_t idx = I - UniqueOperandCommands.begin();
Craig Toppera99859d2016-01-17 08:05:30 +0000164 InstrsForCase[idx] += ", ";
Craig Topper9e9ae602016-01-17 08:05:33 +0000165 InstrsForCase[idx] += Inst.CGI->TheDef->getName();
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000166 InstIdxs[idx].push_back(i);
Craig Toppera99859d2016-01-17 08:05:30 +0000167 } else {
Craig Topper1993e3b2016-01-08 07:06:29 +0000168 UniqueOperandCommands.push_back(std::move(Command));
Craig Topper9e9ae602016-01-17 08:05:33 +0000169 InstrsForCase.push_back(Inst.CGI->TheDef->getName());
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000170 InstIdxs.emplace_back();
171 InstIdxs.back().push_back(i);
Chris Lattneredee5252006-07-18 18:28:27 +0000172
173 // This command matches one operand so far.
174 InstOpsUsed.push_back(1);
175 }
176 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000177
Chris Lattneredee5252006-07-18 18:28:27 +0000178 // For each entry of UniqueOperandCommands, there is a set of instructions
179 // that uses it. If the next command of all instructions in the set are
180 // identical, fold it into the command.
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000181 for (size_t CommandIdx = 0, e = UniqueOperandCommands.size();
Chris Lattneredee5252006-07-18 18:28:27 +0000182 CommandIdx != e; ++CommandIdx) {
Jim Grosbacha5497342010-09-29 22:32:50 +0000183
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000184 const auto &Idxs = InstIdxs[CommandIdx];
Chris Lattneredee5252006-07-18 18:28:27 +0000185
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000186 for (unsigned Op = 1; ; ++Op) {
187 // Find the first instruction in the set.
188 const AsmWriterInst &FirstInst = Instructions[Idxs.front()];
Chris Lattneredee5252006-07-18 18:28:27 +0000189 // If this instruction has no more operands, we isn't anything to merge
190 // into this command.
Craig Topper9e9ae602016-01-17 08:05:33 +0000191 if (FirstInst.Operands.size() == Op)
Chris Lattneredee5252006-07-18 18:28:27 +0000192 break;
193
194 // Otherwise, scan to see if all of the other instructions in this command
195 // set share the operand.
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000196 if (std::any_of(Idxs.begin()+1, Idxs.end(),
197 [&](unsigned Idx) {
198 const AsmWriterInst &OtherInst = Instructions[Idx];
199 return OtherInst.Operands.size() == Op ||
200 OtherInst.Operands[Op] != FirstInst.Operands[Op];
201 }))
202 break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000203
Chris Lattneredee5252006-07-18 18:28:27 +0000204 // Okay, everything in this command set has the same next operand. Add it
205 // to UniqueOperandCommands and remember that it was consumed.
Craig Topperc24a4012016-01-14 06:15:07 +0000206 std::string Command = " " +
Craig Topper9e9ae602016-01-17 08:05:33 +0000207 FirstInst.Operands[Op].getCode(PassSubtarget) + "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000208
Chris Lattneredee5252006-07-18 18:28:27 +0000209 UniqueOperandCommands[CommandIdx] += Command;
210 InstOpsUsed[CommandIdx]++;
Chris Lattner692374c2006-07-18 17:18:03 +0000211 }
212 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000213
Chris Lattner692374c2006-07-18 17:18:03 +0000214 // Prepend some of the instructions each case is used for onto the case val.
215 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
216 std::string Instrs = InstrsForCase[i];
217 if (Instrs.size() > 70) {
218 Instrs.erase(Instrs.begin()+70, Instrs.end());
219 Instrs += "...";
220 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000221
Chris Lattner692374c2006-07-18 17:18:03 +0000222 if (!Instrs.empty())
Jim Grosbacha5497342010-09-29 22:32:50 +0000223 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattner692374c2006-07-18 17:18:03 +0000224 UniqueOperandCommands[i];
225 }
226}
227
228
Daniel Dunbar04f049f2009-10-17 20:43:42 +0000229static void UnescapeString(std::string &Str) {
230 for (unsigned i = 0; i != Str.size(); ++i) {
231 if (Str[i] == '\\' && i != Str.size()-1) {
232 switch (Str[i+1]) {
233 default: continue; // Don't execute the code after the switch.
234 case 'a': Str[i] = '\a'; break;
235 case 'b': Str[i] = '\b'; break;
236 case 'e': Str[i] = 27; break;
237 case 'f': Str[i] = '\f'; break;
238 case 'n': Str[i] = '\n'; break;
239 case 'r': Str[i] = '\r'; break;
240 case 't': Str[i] = '\t'; break;
241 case 'v': Str[i] = '\v'; break;
242 case '"': Str[i] = '\"'; break;
243 case '\'': Str[i] = '\''; break;
244 case '\\': Str[i] = '\\'; break;
245 }
246 // Nuke the second character.
247 Str.erase(Str.begin()+i+1);
248 }
249 }
250}
251
Chris Lattner06c5eed2009-09-13 20:08:00 +0000252/// EmitPrintInstruction - Generate the code for the "printInstruction" method
Ahmed Bougachabd214002013-10-28 18:07:17 +0000253/// implementation. Destroys all instances of AsmWriterInst information, by
254/// clearing the Instructions vector.
Chris Lattner06c5eed2009-09-13 20:08:00 +0000255void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner6ffa5012004-08-14 22:50:53 +0000256 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner72770f52004-10-03 20:19:02 +0000257 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Craig Topperc24a4012016-01-14 06:15:07 +0000258 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
Jim Grosbacha5497342010-09-29 22:32:50 +0000259
Chris Lattner1c4ae852004-08-01 05:59:33 +0000260 O <<
261 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner06c5eed2009-09-13 20:08:00 +0000262 "/// from the instruction set description.\n"
Chris Lattnerb94284b2009-08-08 01:32:19 +0000263 "void " << Target.getName() << ClassName
Akira Hatanakab46d0232015-03-27 20:36:02 +0000264 << "::printInstruction(const MCInst *MI, "
265 << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
266 << "raw_ostream &O) {\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000267
Chris Lattnere32982c2006-07-14 22:59:11 +0000268 // Build an aggregate string, and build a table of offsets into it.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000269 SequenceToOffsetTable<std::string> StringTable;
Jim Grosbacha5497342010-09-29 22:32:50 +0000270
Chris Lattner5d751b42006-09-27 16:44:09 +0000271 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000272 /// chunk of the output as well as indices used for operand printing.
Craig Topperf9265322016-01-17 20:38:14 +0000273 std::vector<uint64_t> OpcodeInfo(NumberedInstructions.size());
Craig Topperd4f87a32016-01-13 07:20:12 +0000274 const unsigned OpcodeInfoBits = 64;
Jim Grosbacha5497342010-09-29 22:32:50 +0000275
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000276 // Add all strings to the string table upfront so it can generate an optimized
277 // representation.
Craig Topper9e9ae602016-01-17 08:05:33 +0000278 for (AsmWriterInst &AWI : Instructions) {
279 if (AWI.Operands[0].OperandType ==
Jim Grosbachf4e67082012-04-18 18:56:33 +0000280 AsmWriterOperand::isLiteralTextOperand &&
Craig Topper9e9ae602016-01-17 08:05:33 +0000281 !AWI.Operands[0].Str.empty()) {
282 std::string Str = AWI.Operands[0].Str;
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000283 UnescapeString(Str);
284 StringTable.add(Str);
285 }
286 }
287
288 StringTable.layout();
289
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000290 unsigned MaxStringIdx = 0;
Craig Topper9e9ae602016-01-17 08:05:33 +0000291 for (AsmWriterInst &AWI : Instructions) {
Chris Lattnere32982c2006-07-14 22:59:11 +0000292 unsigned Idx;
Craig Topper9e9ae602016-01-17 08:05:33 +0000293 if (AWI.Operands[0].OperandType != AsmWriterOperand::isLiteralTextOperand ||
294 AWI.Operands[0].Str.empty()) {
Chris Lattner36504652006-07-19 01:39:06 +0000295 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000296 Idx = StringTable.get("");
Chris Lattnere32982c2006-07-14 22:59:11 +0000297 } else {
Craig Topper9e9ae602016-01-17 08:05:33 +0000298 std::string Str = AWI.Operands[0].Str;
Chris Lattnerb47ed612009-09-14 01:16:36 +0000299 UnescapeString(Str);
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000300 Idx = StringTable.get(Str);
Chris Lattnerb47ed612009-09-14 01:16:36 +0000301 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbacha5497342010-09-29 22:32:50 +0000302
Chris Lattnere32982c2006-07-14 22:59:11 +0000303 // Nuke the string from the operand list. It is now handled!
Craig Topper9e9ae602016-01-17 08:05:33 +0000304 AWI.Operands.erase(AWI.Operands.begin());
Chris Lattner92275bb2005-01-22 19:22:23 +0000305 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000306
Chris Lattnerb47ed612009-09-14 01:16:36 +0000307 // Bias offset by one since we want 0 as a sentinel.
Craig Topper9e9ae602016-01-17 08:05:33 +0000308 OpcodeInfo[AWI.CGIIndex] = Idx+1;
Chris Lattner92275bb2005-01-22 19:22:23 +0000309 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000310
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000311 // Figure out how many bits we used for the string index.
Chris Lattnerb47ed612009-09-14 01:16:36 +0000312 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbacha5497342010-09-29 22:32:50 +0000313
Chris Lattner692374c2006-07-18 17:18:03 +0000314 // To reduce code size, we compactify common instructions into a few bits
315 // in the opcode-indexed table.
Craig Topperd4f87a32016-01-13 07:20:12 +0000316 unsigned BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000317
Craig Topper1f387362014-11-25 20:11:25 +0000318 std::vector<std::vector<std::string>> TableDrivenOperandPrinters;
Jim Grosbacha5497342010-09-29 22:32:50 +0000319
Chris Lattnercb0c8482006-07-18 17:56:07 +0000320 while (1) {
Chris Lattner692374c2006-07-18 17:18:03 +0000321 std::vector<std::string> UniqueOperandCommands;
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000322 std::vector<std::vector<unsigned>> InstIdxs;
Chris Lattneredee5252006-07-18 18:28:27 +0000323 std::vector<unsigned> NumInstOpsHandled;
324 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000325 NumInstOpsHandled, PassSubtarget);
Jim Grosbacha5497342010-09-29 22:32:50 +0000326
Chris Lattner692374c2006-07-18 17:18:03 +0000327 // If we ran out of operands to print, we're done.
328 if (UniqueOperandCommands.empty()) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000329
Chris Lattner692374c2006-07-18 17:18:03 +0000330 // Compute the number of bits we need to represent these cases, this is
331 // ceil(log2(numentries)).
332 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbacha5497342010-09-29 22:32:50 +0000333
Chris Lattner692374c2006-07-18 17:18:03 +0000334 // If we don't have enough bits for this operand, don't include it.
335 if (NumBits > BitsLeft) {
Chris Lattner34822f62009-08-23 04:44:11 +0000336 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
337 << " more bits\n");
Chris Lattner692374c2006-07-18 17:18:03 +0000338 break;
339 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000340
Chris Lattner692374c2006-07-18 17:18:03 +0000341 // Otherwise, we can include this in the initial lookup table. Add it in.
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000342 for (size_t i = 0, e = InstIdxs.size(); i != e; ++i) {
343 unsigned NumOps = NumInstOpsHandled[i];
344 for (unsigned Idx : InstIdxs[i]) {
345 OpcodeInfo[Instructions[Idx].CGIIndex] |=
346 (uint64_t)i << (OpcodeInfoBits-BitsLeft);
347 // Remove the info about this operand from the instruction.
348 AsmWriterInst &Inst = Instructions[Idx];
349 if (!Inst.Operands.empty()) {
350 assert(NumOps <= Inst.Operands.size() &&
351 "Can't remove this many ops!");
352 Inst.Operands.erase(Inst.Operands.begin(),
353 Inst.Operands.begin()+NumOps);
354 }
Craig Topper9e9ae602016-01-17 08:05:33 +0000355 }
Chris Lattnercb0c8482006-07-18 17:56:07 +0000356 }
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000357 BitsLeft -= NumBits;
Jim Grosbacha5497342010-09-29 22:32:50 +0000358
Chris Lattnercb0c8482006-07-18 17:56:07 +0000359 // Remember the handlers for this set of operands.
Craig Topper1f387362014-11-25 20:11:25 +0000360 TableDrivenOperandPrinters.push_back(std::move(UniqueOperandCommands));
Chris Lattner692374c2006-07-18 17:18:03 +0000361 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000362
Craig Topper14d91732016-01-11 05:13:41 +0000363 // Emit the string table itself.
Reid Kleckner132c40f2014-07-17 19:43:40 +0000364 O << " static const char AsmStrs[] = {\n";
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000365 StringTable.emit(O, printChar);
366 O << " };\n\n";
Chris Lattnere32982c2006-07-14 22:59:11 +0000367
Craig Topper14d91732016-01-11 05:13:41 +0000368 // Emit the lookup tables in pieces to minimize wasted bytes.
Craig Topperd4f87a32016-01-13 07:20:12 +0000369 unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
Craig Topper14d91732016-01-11 05:13:41 +0000370 unsigned Table = 0, Shift = 0;
371 SmallString<128> BitsString;
372 raw_svector_ostream BitsOS(BitsString);
373 // If the total bits is more than 32-bits we need to use a 64-bit type.
Craig Topperd4f87a32016-01-13 07:20:12 +0000374 BitsOS << " uint" << ((BitsLeft < (OpcodeInfoBits - 32)) ? 64 : 32)
375 << "_t Bits = 0;\n";
Craig Topper14d91732016-01-11 05:13:41 +0000376 while (BytesNeeded != 0) {
377 // Figure out how big this table section needs to be, but no bigger than 4.
378 unsigned TableSize = std::min(1 << Log2_32(BytesNeeded), 4);
379 BytesNeeded -= TableSize;
380 TableSize *= 8; // Convert to bits;
381 uint64_t Mask = (1ULL << TableSize) - 1;
382 O << " static const uint" << TableSize << "_t OpInfo" << Table
383 << "[] = {\n";
Craig Topperf9265322016-01-17 20:38:14 +0000384 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Craig Topper14d91732016-01-11 05:13:41 +0000385 O << " " << ((OpcodeInfo[i] >> Shift) & Mask) << "U,\t// "
Craig Topperf9265322016-01-17 20:38:14 +0000386 << NumberedInstructions[i]->TheDef->getName() << "\n";
Craig Topper14d91732016-01-11 05:13:41 +0000387 }
388 O << " };\n\n";
389 // Emit string to combine the individual table lookups.
390 BitsOS << " Bits |= ";
391 // If the total bits is more than 32-bits we need to use a 64-bit type.
Craig Topperd4f87a32016-01-13 07:20:12 +0000392 if (BitsLeft < (OpcodeInfoBits - 32))
Craig Topper14d91732016-01-11 05:13:41 +0000393 BitsOS << "(uint64_t)";
394 BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
395 // Prepare the shift for the next iteration and increment the table count.
396 Shift += TableSize;
397 ++Table;
398 }
399
400 // Emit the initial tab character.
Evan Cheng32e53472008-02-02 08:39:46 +0000401 O << " O << \"\\t\";\n\n";
402
Craig Topper06cec4c2012-09-14 08:33:11 +0000403 O << " // Emit the opcode for the instruction.\n";
Craig Topper14d91732016-01-11 05:13:41 +0000404 O << BitsString;
405
406 // Emit the starting string.
Manman Ren68cf9fc2012-09-13 17:43:46 +0000407 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattnerb47ed612009-09-14 01:16:36 +0000408 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenefdd25192009-08-05 21:00:52 +0000409
Chris Lattner692374c2006-07-18 17:18:03 +0000410 // Output the table driven operand information.
Craig Topperd4f87a32016-01-13 07:20:12 +0000411 BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000412 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
413 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
414
415 // Compute the number of bits we need to represent these cases, this is
416 // ceil(log2(numentries)).
417 unsigned NumBits = Log2_32_Ceil(Commands.size());
418 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbacha5497342010-09-29 22:32:50 +0000419
Chris Lattner692374c2006-07-18 17:18:03 +0000420 // Emit code to extract this field from Bits.
Chris Lattner692374c2006-07-18 17:18:03 +0000421 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000422 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000423
Chris Lattneredee5252006-07-18 18:28:27 +0000424 if (Commands.size() == 2) {
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000425 // Emit two possibilitys with if/else.
Craig Topper06cec4c2012-09-14 08:33:11 +0000426 O << " if ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000427 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000428 << ((1 << NumBits)-1) << ") {\n"
429 << Commands[1]
430 << " } else {\n"
431 << Commands[0]
432 << " }\n\n";
Eric Christophera573d192010-09-18 18:50:27 +0000433 } else if (Commands.size() == 1) {
434 // Emit a single possibility.
435 O << Commands[0] << "\n\n";
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000436 } else {
Craig Topper06cec4c2012-09-14 08:33:11 +0000437 O << " switch ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000438 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000439 << ((1 << NumBits)-1) << ") {\n"
Craig Toppera4ff6ae2014-11-24 14:09:52 +0000440 << " default: llvm_unreachable(\"Invalid command number.\");\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000441
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000442 // Print out all the cases.
Craig Topper190ecd52016-01-08 07:06:32 +0000443 for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
444 O << " case " << j << ":\n";
445 O << Commands[j];
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000446 O << " break;\n";
447 }
448 O << " }\n\n";
Chris Lattner692374c2006-07-18 17:18:03 +0000449 }
Craig Topper06cec4c2012-09-14 08:33:11 +0000450 BitsLeft -= NumBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000451 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000452
Chris Lattnercb0c8482006-07-18 17:56:07 +0000453 // Okay, delete instructions with no operand info left.
David Majnemerc7004902016-08-12 04:32:37 +0000454 auto I = remove_if(Instructions,
455 [](AsmWriterInst &Inst) { return Inst.Operands.empty(); });
Craig Topper4f1f1152016-01-13 07:20:10 +0000456 Instructions.erase(I, Instructions.end());
Chris Lattner692374c2006-07-18 17:18:03 +0000457
Jim Grosbacha5497342010-09-29 22:32:50 +0000458
Chris Lattner692374c2006-07-18 17:18:03 +0000459 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000460 // elements in the vector.
461 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbacha5497342010-09-29 22:32:50 +0000462
463
Craig Topperdf390602016-01-13 07:20:07 +0000464 // Now that we've emitted all of the operand info that fit into 64 bits, emit
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000465 // information for those instructions that are left. This is a less dense
Craig Topperdf390602016-01-13 07:20:07 +0000466 // encoding, but we expect the main 64-bit table to handle the majority of
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000467 // instructions.
Chris Lattner66e288b2006-07-18 17:38:46 +0000468 if (!Instructions.empty()) {
469 // Find the opcode # of inline asm.
470 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000471 O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
Chris Lattner66e288b2006-07-18 17:38:46 +0000472 while (!Instructions.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000473 EmitInstructions(Instructions, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000474
Chris Lattner66e288b2006-07-18 17:38:46 +0000475 O << " }\n";
476 }
David Greene5b4bc262009-07-29 20:10:24 +0000477
Chris Lattner6e172082006-07-18 19:06:01 +0000478 O << "}\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000479}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000480
Owen Andersona84be6c2011-06-27 21:06:21 +0000481static void
482emitRegisterNameString(raw_ostream &O, StringRef AltName,
David Blaikie9b613db2014-11-29 18:13:39 +0000483 const std::deque<CodeGenRegister> &Registers) {
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000484 SequenceToOffsetTable<std::string> StringTable;
485 SmallVector<std::string, 4> AsmNames(Registers.size());
David Blaikie9b613db2014-11-29 18:13:39 +0000486 unsigned i = 0;
487 for (const auto &Reg : Registers) {
488 std::string &AsmName = AsmNames[i++];
Owen Andersona84be6c2011-06-27 21:06:21 +0000489
Owen Andersona84be6c2011-06-27 21:06:21 +0000490 // "NoRegAltName" is special. We don't need to do a lookup for that,
491 // as it's just a reference to the default register name.
492 if (AltName == "" || AltName == "NoRegAltName") {
493 AsmName = Reg.TheDef->getValueAsString("AsmName");
494 if (AsmName.empty())
495 AsmName = Reg.getName();
496 } else {
497 // Make sure the register has an alternate name for this index.
498 std::vector<Record*> AltNameList =
499 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
500 unsigned Idx = 0, e;
501 for (e = AltNameList.size();
502 Idx < e && (AltNameList[Idx]->getName() != AltName);
503 ++Idx)
504 ;
505 // If the register has an alternate name for this index, use it.
506 // Otherwise, leave it empty as an error flag.
507 if (Idx < e) {
508 std::vector<std::string> AltNames =
509 Reg.TheDef->getValueAsListOfStrings("AltNames");
510 if (AltNames.size() <= Idx)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000511 PrintFatalError(Reg.TheDef->getLoc(),
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000512 "Register definition missing alt name for '" +
513 AltName + "'.");
Owen Andersona84be6c2011-06-27 21:06:21 +0000514 AsmName = AltNames[Idx];
515 }
516 }
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000517 StringTable.add(AsmName);
518 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000519
Craig Topperf8f0a232012-09-15 01:22:42 +0000520 StringTable.layout();
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000521 O << " static const char AsmStrs" << AltName << "[] = {\n";
522 StringTable.emit(O, printChar);
523 O << " };\n\n";
524
Daniel Sandersca89f3a2016-11-19 12:21:34 +0000525 O << " static const " << getMinimalTypeForRange(StringTable.size() - 1, 32)
Craig Topperba6d83e2014-11-24 02:08:35 +0000526 << " RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000527 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Topper7a2cea12012-04-02 00:47:39 +0000528 if ((i % 14) == 0)
529 O << "\n ";
530 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersona84be6c2011-06-27 21:06:21 +0000531 }
Craig Topper9c252eb2012-04-03 06:52:47 +0000532 O << "\n };\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000533 << "\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000534}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000535
536void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +0000537 Record *AsmWriter = Target.getAsmWriter();
538 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
David Blaikie9b613db2014-11-29 18:13:39 +0000539 const auto &Registers = Target.getRegBank().getRegisters();
Craig Topper83421ec2016-01-17 20:38:21 +0000540 const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
Owen Andersona84be6c2011-06-27 21:06:21 +0000541 bool hasAltNames = AltNameIndices.size() > 1;
Hal Finkelcd5f9842015-12-11 17:31:27 +0000542 std::string Namespace =
543 Registers.front().TheDef->getValueAsString("Namespace");
Jim Grosbacha5497342010-09-29 22:32:50 +0000544
Chris Lattner06c5eed2009-09-13 20:08:00 +0000545 O <<
546 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
547 "/// from the register set description. This returns the assembler name\n"
548 "/// for the specified register.\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000549 "const char *" << Target.getName() << ClassName << "::";
550 if (hasAltNames)
551 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
552 else
553 O << "getRegisterName(unsigned RegNo) {\n";
554 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
555 << " && \"Invalid register number!\");\n"
Chris Lattnera7e8ae42009-09-14 01:26:18 +0000556 << "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000557
Owen Andersona84be6c2011-06-27 21:06:21 +0000558 if (hasAltNames) {
Craig Topper190ecd52016-01-08 07:06:32 +0000559 for (const Record *R : AltNameIndices)
560 emitRegisterNameString(O, R->getName(), Registers);
Owen Andersona84be6c2011-06-27 21:06:21 +0000561 } else
562 emitRegisterNameString(O, "", Registers);
Jim Grosbacha5497342010-09-29 22:32:50 +0000563
Owen Andersona84be6c2011-06-27 21:06:21 +0000564 if (hasAltNames) {
Craig Topperba6d83e2014-11-24 02:08:35 +0000565 O << " switch(AltIdx) {\n"
Craig Topperc4965bc2012-02-05 07:21:30 +0000566 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000567 for (const Record *R : AltNameIndices) {
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +0000568 const std::string &AltName = R->getName();
Hal Finkelcd5f9842015-12-11 17:31:27 +0000569 std::string Prefix = !Namespace.empty() ? Namespace + "::" : "";
570 O << " case " << Prefix << AltName << ":\n"
Craig Topperba6d83e2014-11-24 02:08:35 +0000571 << " assert(*(AsmStrs" << AltName << "+RegAsmOffset"
572 << AltName << "[RegNo-1]) &&\n"
573 << " \"Invalid alt name index for register!\");\n"
574 << " return AsmStrs" << AltName << "+RegAsmOffset"
575 << AltName << "[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000576 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000577 O << " }\n";
578 } else {
579 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
580 << " \"Invalid alt name index for register!\");\n"
581 << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000582 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000583 O << "}\n";
Chris Lattner06c5eed2009-09-13 20:08:00 +0000584}
585
Bill Wendling7e5771d2011-03-21 08:31:53 +0000586namespace {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000587// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
588// they both have the same conditionals. In which case, we cannot print out the
589// alias for that pattern.
590class IAPrinter {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000591 std::vector<std::string> Conds;
Tim Northoveree20caa2014-05-12 18:04:06 +0000592 std::map<StringRef, std::pair<int, int>> OpMap;
Tim Northoveree20caa2014-05-12 18:04:06 +0000593
Bill Wendling5d3174c2011-03-21 08:40:31 +0000594 std::string Result;
595 std::string AsmString;
Bill Wendling5d3174c2011-03-21 08:40:31 +0000596public:
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000597 IAPrinter(std::string R, std::string AS)
598 : Result(std::move(R)), AsmString(std::move(AS)) {}
Bill Wendling5d3174c2011-03-21 08:40:31 +0000599
600 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000601
Tim Northoveree20caa2014-05-12 18:04:06 +0000602 void addOperand(StringRef Op, int OpIdx, int PrintMethodIdx = -1) {
603 assert(OpIdx >= 0 && OpIdx < 0xFE && "Idx out of range");
Tim Northover0ee9e7e2014-05-13 09:37:41 +0000604 assert(PrintMethodIdx >= -1 && PrintMethodIdx < 0xFF &&
Jay Foadb3590512014-05-13 08:26:53 +0000605 "Idx out of range");
Tim Northoveree20caa2014-05-12 18:04:06 +0000606 OpMap[Op] = std::make_pair(OpIdx, PrintMethodIdx);
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000607 }
Tim Northoveree20caa2014-05-12 18:04:06 +0000608
Bill Wendling5d3174c2011-03-21 08:40:31 +0000609 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
Tim Northoveree20caa2014-05-12 18:04:06 +0000610 int getOpIndex(StringRef Op) { return OpMap[Op].first; }
611 std::pair<int, int> &getOpData(StringRef Op) { return OpMap[Op]; }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000612
Tim Northoverd8d65a62014-05-15 11:16:32 +0000613 std::pair<StringRef, StringRef::iterator> parseName(StringRef::iterator Start,
614 StringRef::iterator End) {
615 StringRef::iterator I = Start;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000616 StringRef::iterator Next;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000617 if (*I == '{') {
618 // ${some_name}
619 Start = ++I;
620 while (I != End && *I != '}')
621 ++I;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000622 Next = I;
623 // eat the final '}'
624 if (Next != End)
625 ++Next;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000626 } else {
627 // $name, just eat the usual suspects.
628 while (I != End &&
629 ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') ||
630 (*I >= '0' && *I <= '9') || *I == '_'))
631 ++I;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000632 Next = I;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000633 }
634
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000635 return std::make_pair(StringRef(Start, I - Start), Next);
Tim Northoverd8d65a62014-05-15 11:16:32 +0000636 }
637
Evan Cheng4d806e22011-07-06 02:02:33 +0000638 void print(raw_ostream &O) {
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000639 if (Conds.empty()) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000640 O.indent(6) << "return true;\n";
Evan Cheng4d806e22011-07-06 02:02:33 +0000641 return;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000642 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000643
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000644 O << "if (";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000645
646 for (std::vector<std::string>::iterator
647 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
648 if (I != Conds.begin()) {
649 O << " &&\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000650 O.indent(8);
Bill Wendling5d3174c2011-03-21 08:40:31 +0000651 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000652
Bill Wendling5d3174c2011-03-21 08:40:31 +0000653 O << *I;
654 }
655
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000656 O << ") {\n";
657 O.indent(6) << "// " << Result << "\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000658
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000659 // Directly mangle mapped operands into the string. Each operand is
660 // identified by a '$' sign followed by a byte identifying the number of the
661 // operand. We add one to the index to avoid zero bytes.
Tim Northoverd8d65a62014-05-15 11:16:32 +0000662 StringRef ASM(AsmString);
663 SmallString<128> OutString;
664 raw_svector_ostream OS(OutString);
665 for (StringRef::iterator I = ASM.begin(), E = ASM.end(); I != E;) {
666 OS << *I;
667 if (*I == '$') {
668 StringRef Name;
669 std::tie(Name, I) = parseName(++I, E);
670 assert(isOpMapped(Name) && "Unmapped operand!");
Tim Northoveree20caa2014-05-12 18:04:06 +0000671
Tim Northoverd8d65a62014-05-15 11:16:32 +0000672 int OpIndex, PrintIndex;
673 std::tie(OpIndex, PrintIndex) = getOpData(Name);
674 if (PrintIndex == -1) {
675 // Can use the default printOperand route.
676 OS << format("\\x%02X", (unsigned char)OpIndex + 1);
677 } else
678 // 3 bytes if a PrintMethod is needed: 0xFF, the MCInst operand
679 // number, and which of our pre-detected Methods to call.
680 OS << format("\\xFF\\x%02X\\x%02X", OpIndex + 1, PrintIndex + 1);
681 } else {
682 ++I;
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000683 }
684 }
685
686 // Emit the string.
Yaron Keren09fb7c62015-03-10 07:33:23 +0000687 O.indent(6) << "AsmString = \"" << OutString << "\";\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000688
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000689 O.indent(6) << "break;\n";
690 O.indent(4) << '}';
Bill Wendling5d3174c2011-03-21 08:40:31 +0000691 }
692
David Blaikie4ab57cd2015-08-06 19:23:33 +0000693 bool operator==(const IAPrinter &RHS) const {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000694 if (Conds.size() != RHS.Conds.size())
695 return false;
696
697 unsigned Idx = 0;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000698 for (const auto &str : Conds)
699 if (str != RHS.Conds[Idx++])
Bill Wendling5d3174c2011-03-21 08:40:31 +0000700 return false;
701
702 return true;
703 }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000704};
705
Bill Wendling7e5771d2011-03-21 08:31:53 +0000706} // end anonymous namespace
707
Tim Northover5896b062014-05-16 09:42:04 +0000708static unsigned CountNumOperands(StringRef AsmString, unsigned Variant) {
709 std::string FlatAsmString =
710 CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant);
711 AsmString = FlatAsmString;
Bill Wendlinge7124492011-06-14 03:17:20 +0000712
Tim Northover5896b062014-05-16 09:42:04 +0000713 return AsmString.count(' ') + AsmString.count('\t');
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000714}
Bill Wendlinge7124492011-06-14 03:17:20 +0000715
Tim Northover9a24f882014-05-20 09:17:16 +0000716namespace {
717struct AliasPriorityComparator {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000718 typedef std::pair<CodeGenInstAlias, int> ValueType;
Tim Northover9a24f882014-05-20 09:17:16 +0000719 bool operator()(const ValueType &LHS, const ValueType &RHS) {
720 if (LHS.second == RHS.second) {
721 // We don't actually care about the order, but for consistency it
722 // shouldn't depend on pointer comparisons.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000723 return LHS.first.TheDef->getName() < RHS.first.TheDef->getName();
Tim Northover9a24f882014-05-20 09:17:16 +0000724 }
725
726 // Aliases with larger priorities should be considered first.
727 return LHS.second > RHS.second;
728 }
729};
730}
731
732
Bill Wendling7e5771d2011-03-21 08:31:53 +0000733void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
Bill Wendling7e5771d2011-03-21 08:31:53 +0000734 Record *AsmWriter = Target.getAsmWriter();
735
736 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
737 O << "#undef PRINT_ALIAS_INSTR\n\n";
738
Tim Northoveree20caa2014-05-12 18:04:06 +0000739 //////////////////////////////
740 // Gather information about aliases we need to print
741 //////////////////////////////
742
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000743 // Emit the method that prints the alias instruction.
744 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Tim Northover9a24f882014-05-20 09:17:16 +0000745 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Craig Topperc24a4012016-01-14 06:15:07 +0000746 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000747
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000748 std::vector<Record*> AllInstAliases =
749 Records.getAllDerivedDefinitions("InstAlias");
750
751 // Create a map from the qualified name to a list of potential matches.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000752 typedef std::set<std::pair<CodeGenInstAlias, int>, AliasPriorityComparator>
Tim Northover9a24f882014-05-20 09:17:16 +0000753 AliasWithPriority;
754 std::map<std::string, AliasWithPriority> AliasMap;
Craig Topper190ecd52016-01-08 07:06:32 +0000755 for (Record *R : AllInstAliases) {
Tim Northover9a24f882014-05-20 09:17:16 +0000756 int Priority = R->getValueAsInt("EmitPriority");
757 if (Priority < 1)
758 continue; // Aliases with priority 0 are never emitted.
759
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000760 const DagInit *DI = R->getValueAsDag("ResultInst");
Sean Silva88eb8dd2012-10-10 20:24:47 +0000761 const DefInit *Op = cast<DefInit>(DI->getOperator());
David Blaikie4ab57cd2015-08-06 19:23:33 +0000762 AliasMap[getQualifiedName(Op->getDef())].insert(
Craig Topper190ecd52016-01-08 07:06:32 +0000763 std::make_pair(CodeGenInstAlias(R, Variant, Target), Priority));
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000764 }
765
Bill Wendling7e570b52011-03-21 08:59:17 +0000766 // A map of which conditions need to be met for each instruction operand
767 // before it can be matched to the mnemonic.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000768 std::map<std::string, std::vector<IAPrinter>> IAPrinterMap;
Bill Wendling7e570b52011-03-21 08:59:17 +0000769
Craig Topper674d2382016-01-22 05:59:43 +0000770 std::vector<std::string> PrintMethods;
771
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000772 // A list of MCOperandPredicates for all operands in use, and the reverse map
773 std::vector<const Record*> MCOpPredicates;
774 DenseMap<const Record*, unsigned> MCOpPredicateMap;
775
Tim Northover9a24f882014-05-20 09:17:16 +0000776 for (auto &Aliases : AliasMap) {
777 for (auto &Alias : Aliases.second) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000778 const CodeGenInstAlias &CGA = Alias.first;
779 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000780 unsigned NumResultOps =
David Blaikie4ab57cd2015-08-06 19:23:33 +0000781 CountNumOperands(CGA.ResultInst->AsmString, Variant);
Bill Wendlinge7124492011-06-14 03:17:20 +0000782
783 // Don't emit the alias if it has more operands than what it's aliasing.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000784 if (NumResultOps < CountNumOperands(CGA.AsmString, Variant))
Bill Wendlinge7124492011-06-14 03:17:20 +0000785 continue;
786
David Blaikie4ab57cd2015-08-06 19:23:33 +0000787 IAPrinter IAP(CGA.Result->getAsString(), CGA.AsmString);
Bill Wendling7e570b52011-03-21 08:59:17 +0000788
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000789 std::string Namespace = Target.getName();
790 std::vector<Record *> ReqFeatures;
791 if (PassSubtarget) {
792 // We only consider ReqFeatures predicates if PassSubtarget
793 std::vector<Record *> RF =
794 CGA.TheDef->getValueAsListOfDefs("Predicates");
795 std::copy_if(RF.begin(), RF.end(), std::back_inserter(ReqFeatures),
796 [](Record *R) {
797 return R->getValueAsBit("AssemblerMatcherPredicate");
798 });
799 }
800
Tim Northover60091cf2014-05-15 13:36:01 +0000801 unsigned NumMIOps = 0;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000802 for (auto &Operand : CGA.ResultOperands)
Tim Northover60091cf2014-05-15 13:36:01 +0000803 NumMIOps += Operand.getMINumOperands();
804
Bill Wendling7e570b52011-03-21 08:59:17 +0000805 std::string Cond;
Tim Northover60091cf2014-05-15 13:36:01 +0000806 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(NumMIOps);
David Blaikie4ab57cd2015-08-06 19:23:33 +0000807 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000808
Bill Wendling7e570b52011-03-21 08:59:17 +0000809 bool CantHandle = false;
810
Tim Northover60091cf2014-05-15 13:36:01 +0000811 unsigned MIOpNum = 0;
Bill Wendling7e570b52011-03-21 08:59:17 +0000812 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000813 std::string Op = "MI->getOperand(" + llvm::utostr(MIOpNum) + ")";
814
David Blaikie4ab57cd2015-08-06 19:23:33 +0000815 const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
Bill Wendling7e570b52011-03-21 08:59:17 +0000816
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();
Tim Northoveree20caa2014-05-12 18:04:06 +0000821 int PrintMethodIdx = -1;
Bill Wendling7e570b52011-03-21 08:59:17 +0000822
Tim Northoveree20caa2014-05-12 18:04:06 +0000823 // These two may have a PrintMethod, which we want to record (if it's
824 // the first time we've seen it) and provide an index for the aliasing
825 // code to use.
826 if (Rec->isSubClassOf("RegisterOperand") ||
827 Rec->isSubClassOf("Operand")) {
828 std::string PrintMethod = Rec->getValueAsString("PrintMethod");
829 if (PrintMethod != "" && PrintMethod != "printOperand") {
David Majnemer42531262016-08-12 03:55:06 +0000830 PrintMethodIdx =
831 find(PrintMethods, PrintMethod) - PrintMethods.begin();
Tim Northoveree20caa2014-05-12 18:04:06 +0000832 if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
833 PrintMethods.push_back(PrintMethod);
834 }
835 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000836
837 if (Rec->isSubClassOf("RegisterOperand"))
838 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling7e570b52011-03-21 08:59:17 +0000839 if (Rec->isSubClassOf("RegisterClass")) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000840 IAP.addCond(Op + ".isReg()");
Bill Wendling7e570b52011-03-21 08:59:17 +0000841
David Blaikie4ab57cd2015-08-06 19:23:33 +0000842 if (!IAP.isOpMapped(ROName)) {
843 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
844 Record *R = CGA.ResultOperands[i].getRecord();
Jack Carter9c1a0272013-02-05 08:32:10 +0000845 if (R->isSubClassOf("RegisterOperand"))
846 R = R->getValueAsDef("RegClass");
Benjamin Kramer682de392012-03-30 23:13:40 +0000847 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
Tim Northover60091cf2014-05-15 13:36:01 +0000848 R->getName() + "RegClassID)"
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000849 ".contains(" + Op + ".getReg())";
Bill Wendling7e570b52011-03-21 08:59:17 +0000850 } else {
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000851 Cond = Op + ".getReg() == MI->getOperand(" +
David Blaikie4ab57cd2015-08-06 19:23:33 +0000852 llvm::utostr(IAP.getOpIndex(ROName)) + ").getReg()";
Bill Wendling7e570b52011-03-21 08:59:17 +0000853 }
854 } else {
Tim Northoveree20caa2014-05-12 18:04:06 +0000855 // Assume all printable operands are desired for now. This can be
Alp Tokerbeaca192014-05-15 01:52:21 +0000856 // overridden in the InstAlias instantiation if necessary.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000857 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
Bill Wendling7e570b52011-03-21 08:59:17 +0000858
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000859 // There might be an additional predicate on the MCOperand
860 unsigned Entry = MCOpPredicateMap[Rec];
861 if (!Entry) {
862 if (!Rec->isValueUnset("MCOperandPredicate")) {
863 MCOpPredicates.push_back(Rec);
864 Entry = MCOpPredicates.size();
865 MCOpPredicateMap[Rec] = Entry;
866 } else
867 break; // No conditions on this operand at all
868 }
869 Cond = Target.getName() + ClassName + "ValidateMCOperand(" +
Oliver Stannarda34e4702015-12-01 10:48:51 +0000870 Op + ", STI, " + llvm::utostr(Entry) + ")";
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000871 }
872 // for all subcases of ResultOperand::K_Record:
David Blaikie4ab57cd2015-08-06 19:23:33 +0000873 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000874 break;
875 }
Tim Northoverab7689e2013-01-09 13:32:04 +0000876 case CodeGenInstAlias::ResultOperand::K_Imm: {
Tim Northoverab7689e2013-01-09 13:32:04 +0000877 // Just because the alias has an immediate result, doesn't mean the
878 // MCInst will. An MCExpr could be present, for example.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000879 IAP.addCond(Op + ".isImm()");
Tim Northoverab7689e2013-01-09 13:32:04 +0000880
David Blaikie4ab57cd2015-08-06 19:23:33 +0000881 Cond = Op + ".getImm() == " +
882 llvm::utostr(CGA.ResultOperands[i].getImm());
883 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000884 break;
Tim Northoverab7689e2013-01-09 13:32:04 +0000885 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000886 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000887 // If this is zero_reg, something's playing tricks we're not
888 // equipped to handle.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000889 if (!CGA.ResultOperands[i].getRegister()) {
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000890 CantHandle = true;
891 break;
892 }
893
David Blaikie4ab57cd2015-08-06 19:23:33 +0000894 Cond = Op + ".getReg() == " + Target.getName() + "::" +
895 CGA.ResultOperands[i].getRegister()->getName();
896 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000897 break;
898 }
899
Tim Northover60091cf2014-05-15 13:36:01 +0000900 MIOpNum += RO.getMINumOperands();
Bill Wendling7e570b52011-03-21 08:59:17 +0000901 }
902
903 if (CantHandle) continue;
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000904
905 for (auto I = ReqFeatures.cbegin(); I != ReqFeatures.cend(); I++) {
906 Record *R = *I;
907 std::string AsmCondString = R->getValueAsString("AssemblerCondString");
908
909 // AsmCondString has syntax [!]F(,[!]F)*
910 SmallVector<StringRef, 4> Ops;
911 SplitString(AsmCondString, Ops, ",");
912 assert(!Ops.empty() && "AssemblerCondString cannot be empty");
913
914 for (auto &Op : Ops) {
915 assert(!Op.empty() && "Empty operator");
916 if (Op[0] == '!')
917 Cond = "!STI.getFeatureBits()[" + Namespace + "::" +
918 Op.substr(1).str() + "]";
919 else
920 Cond = "STI.getFeatureBits()[" + Namespace + "::" + Op.str() + "]";
921 IAP.addCond(Cond);
922 }
923 }
924
David Blaikie4ab57cd2015-08-06 19:23:33 +0000925 IAPrinterMap[Aliases.first].push_back(std::move(IAP));
Bill Wendling7e570b52011-03-21 08:59:17 +0000926 }
927 }
928
Tim Northoveree20caa2014-05-12 18:04:06 +0000929 //////////////////////////////
930 // Write out the printAliasInstr function
931 //////////////////////////////
932
Bill Wendlingf5199de2011-05-23 00:18:33 +0000933 std::string Header;
934 raw_string_ostream HeaderO(Header);
935
936 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendlinge7124492011-06-14 03:17:20 +0000937 << "::printAliasInstr(const MCInst"
Akira Hatanakab46d0232015-03-27 20:36:02 +0000938 << " *MI, " << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
939 << "raw_ostream &OS) {\n";
Bill Wendling7e570b52011-03-21 08:59:17 +0000940
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000941 std::string Cases;
942 raw_string_ostream CasesO(Cases);
943
David Blaikie4ab57cd2015-08-06 19:23:33 +0000944 for (auto &Entry : IAPrinterMap) {
945 std::vector<IAPrinter> &IAPs = Entry.second;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000946 std::vector<IAPrinter*> UniqueIAPs;
947
David Blaikie4ab57cd2015-08-06 19:23:33 +0000948 for (auto &LHS : IAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000949 bool IsDup = false;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000950 for (const auto &RHS : IAPs) {
951 if (&LHS != &RHS && LHS == RHS) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000952 IsDup = true;
953 break;
954 }
955 }
956
David Blaikie4ab57cd2015-08-06 19:23:33 +0000957 if (!IsDup)
958 UniqueIAPs.push_back(&LHS);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000959 }
960
961 if (UniqueIAPs.empty()) continue;
962
David Blaikie4ab57cd2015-08-06 19:23:33 +0000963 CasesO.indent(2) << "case " << Entry.first << ":\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000964
Craig Topper190ecd52016-01-08 07:06:32 +0000965 for (IAPrinter *IAP : UniqueIAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000966 CasesO.indent(4);
Evan Cheng4d806e22011-07-06 02:02:33 +0000967 IAP->print(CasesO);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000968 CasesO << '\n';
969 }
970
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000971 CasesO.indent(4) << "return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000972 }
973
Bill Wendlinge7124492011-06-14 03:17:20 +0000974 if (CasesO.str().empty()) {
Bill Wendlingf5199de2011-05-23 00:18:33 +0000975 O << HeaderO.str();
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000976 O << " return false;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000977 O << "}\n\n";
978 O << "#endif // PRINT_ALIAS_INSTR\n";
979 return;
980 }
981
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000982 if (!MCOpPredicates.empty())
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000983 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +0000984 << "ValidateMCOperand(const MCOperand &MCOp,\n"
985 << " const MCSubtargetInfo &STI,\n"
986 << " unsigned PredicateIndex);\n";
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000987
Bill Wendlingf5199de2011-05-23 00:18:33 +0000988 O << HeaderO.str();
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000989 O.indent(2) << "const char *AsmString;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000990 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000991 O.indent(2) << "default: return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000992 O << CasesO.str();
993 O.indent(2) << "}\n\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000994
995 // Code that prints the alias, replacing the operands with the ones from the
996 // MCInst.
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000997 O << " unsigned I = 0;\n";
Sjoerd Meijer3c2f7852016-06-03 13:17:37 +0000998 O << " while (AsmString[I] != ' ' && AsmString[I] != '\\t' &&\n";
999 O << " AsmString[I] != '$' && AsmString[I] != '\\0')\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001000 O << " ++I;\n";
1001 O << " OS << '\\t' << StringRef(AsmString, I);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001002
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001003 O << " if (AsmString[I] != '\\0') {\n";
Sjoerd Meijer3c2f7852016-06-03 13:17:37 +00001004 O << " if (AsmString[I] == ' ' || AsmString[I] == '\\t')";
1005 O << " OS << '\\t';\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001006 O << " do {\n";
1007 O << " if (AsmString[I] == '$') {\n";
1008 O << " ++I;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001009 O << " if (AsmString[I] == (char)0xff) {\n";
1010 O << " ++I;\n";
1011 O << " int OpIdx = AsmString[I++] - 1;\n";
1012 O << " int PrintMethodIdx = AsmString[I++] - 1;\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001013 O << " printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, ";
1014 O << (PassSubtarget ? "STI, " : "");
1015 O << "OS);\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001016 O << " } else\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001017 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, ";
1018 O << (PassSubtarget ? "STI, " : "");
1019 O << "OS);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001020 O << " } else {\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001021 O << " OS << AsmString[I++];\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001022 O << " }\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001023 O << " } while (AsmString[I] != '\\0');\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001024 O << " }\n\n";
Jim Grosbachf4e67082012-04-18 18:56:33 +00001025
Eric Christopher2e3fbaa2011-04-18 21:28:11 +00001026 O << " return true;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001027 O << "}\n\n";
1028
Tim Northoveree20caa2014-05-12 18:04:06 +00001029 //////////////////////////////
1030 // Write out the printCustomAliasOperand function
1031 //////////////////////////////
1032
1033 O << "void " << Target.getName() << ClassName << "::"
1034 << "printCustomAliasOperand(\n"
1035 << " const MCInst *MI, unsigned OpIdx,\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001036 << " unsigned PrintMethodIdx,\n"
1037 << (PassSubtarget ? " const MCSubtargetInfo &STI,\n" : "")
1038 << " raw_ostream &OS) {\n";
Aaron Ballmane58a5702014-05-13 12:52:35 +00001039 if (PrintMethods.empty())
1040 O << " llvm_unreachable(\"Unknown PrintMethod kind\");\n";
1041 else {
1042 O << " switch (PrintMethodIdx) {\n"
1043 << " default:\n"
1044 << " llvm_unreachable(\"Unknown PrintMethod kind\");\n"
Tim Northoveree20caa2014-05-12 18:04:06 +00001045 << " break;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001046
Aaron Ballmane58a5702014-05-13 12:52:35 +00001047 for (unsigned i = 0; i < PrintMethods.size(); ++i) {
1048 O << " case " << i << ":\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001049 << " " << PrintMethods[i] << "(MI, OpIdx, "
1050 << (PassSubtarget ? "STI, " : "") << "OS);\n"
Aaron Ballmane58a5702014-05-13 12:52:35 +00001051 << " break;\n";
1052 }
1053 O << " }\n";
1054 }
1055 O << "}\n\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001056
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001057 if (!MCOpPredicates.empty()) {
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001058 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +00001059 << "ValidateMCOperand(const MCOperand &MCOp,\n"
1060 << " const MCSubtargetInfo &STI,\n"
1061 << " unsigned PredicateIndex) {\n"
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001062 << " switch (PredicateIndex) {\n"
1063 << " default:\n"
1064 << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
1065 << " break;\n";
1066
1067 for (unsigned i = 0; i < MCOpPredicates.size(); ++i) {
1068 Init *MCOpPred = MCOpPredicates[i]->getValueInit("MCOperandPredicate");
Tim Northover88403d72016-07-05 21:22:55 +00001069 if (CodeInit *SI = dyn_cast<CodeInit>(MCOpPred)) {
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001070 O << " case " << i + 1 << ": {\n"
1071 << SI->getValue() << "\n"
1072 << " }\n";
1073 } else
1074 llvm_unreachable("Unexpected MCOperandPredicate field!");
1075 }
1076 O << " }\n"
1077 << "}\n\n";
1078 }
1079
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001080 O << "#endif // PRINT_ALIAS_INSTR\n";
1081}
Chris Lattner06c5eed2009-09-13 20:08:00 +00001082
Ahmed Bougachabd214002013-10-28 18:07:17 +00001083AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1084 Record *AsmWriter = Target.getAsmWriter();
Craig Topper0bd58742016-01-13 07:20:05 +00001085 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Ahmed Bougachabd214002013-10-28 18:07:17 +00001086
1087 // Get the instruction numbering.
Craig Topperf9265322016-01-17 20:38:14 +00001088 NumberedInstructions = Target.getInstructionsByEnumValue();
Ahmed Bougachabd214002013-10-28 18:07:17 +00001089
Craig Topperf9265322016-01-17 20:38:14 +00001090 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
1091 const CodeGenInstruction *I = NumberedInstructions[i];
Craig Topper9e9ae602016-01-17 08:05:33 +00001092 if (!I->AsmString.empty() && I->TheDef->getName() != "PHI")
1093 Instructions.emplace_back(*I, i, Variant);
1094 }
Ahmed Bougachabd214002013-10-28 18:07:17 +00001095}
1096
Chris Lattner06c5eed2009-09-13 20:08:00 +00001097void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +00001098 EmitPrintInstruction(O);
1099 EmitGetRegisterName(O);
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001100 EmitPrintAliasInstruction(O);
Chris Lattner06c5eed2009-09-13 20:08:00 +00001101}
1102
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001103
1104namespace llvm {
1105
1106void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1107 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1108 AsmWriterEmitter(RK).run(OS);
1109}
1110
1111} // End llvm namespace