blob: 75b9bc6cca40128b05160d348f0a1ef7774b4292 [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"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000016#include "CodeGenInstruction.h"
17#include "CodeGenRegisters.h"
Chris Lattner1c4ae852004-08-01 05:59:33 +000018#include "CodeGenTarget.h"
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +000019#include "SequenceToOffsetTable.h"
Daniel Sandersca89f3a2016-11-19 12:21:34 +000020#include "Types.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000021#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/DenseMap.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000023#include "llvm/ADT/SmallString.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000024#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/STLExtras.h"
Craig Topperb6350132012-07-27 06:44:02 +000026#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000027#include "llvm/ADT/StringRef.h"
Owen Andersona84be6c2011-06-27 21:06:21 +000028#include "llvm/ADT/Twine.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000029#include "llvm/Support/Casting.h"
Chris Lattner692374c2006-07-18 17:18:03 +000030#include "llvm/Support/Debug.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000031#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer17c17bc2013-09-11 15:42:16 +000032#include "llvm/Support/Format.h"
Chris Lattner692374c2006-07-18 17:18:03 +000033#include "llvm/Support/MathExtras.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000034#include "llvm/Support/raw_ostream.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000035#include "llvm/TableGen/Error.h"
36#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000037#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenda636b32005-01-22 18:50:10 +000038#include <algorithm>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000039#include <cassert>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000040#include <cstddef>
41#include <cstdint>
42#include <deque>
43#include <iterator>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000044#include <map>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000045#include <set>
46#include <string>
47#include <tuple>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000048#include <utility>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000049#include <vector>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000050
Chris Lattner1c4ae852004-08-01 05:59:33 +000051using namespace llvm;
52
Chandler Carruthe96dd892014-04-21 22:55:11 +000053#define DEBUG_TYPE "asm-writer-emitter"
54
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000055namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000056
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000057class AsmWriterEmitter {
58 RecordKeeper &Records;
Ahmed Bougachabd214002013-10-28 18:07:17 +000059 CodeGenTarget Target;
Craig Topperf9265322016-01-17 20:38:14 +000060 ArrayRef<const CodeGenInstruction *> NumberedInstructions;
Ahmed Bougachabd214002013-10-28 18:07:17 +000061 std::vector<AsmWriterInst> Instructions;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000062
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000063public:
Ahmed Bougachabd214002013-10-28 18:07:17 +000064 AsmWriterEmitter(RecordKeeper &R);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000065
66 void run(raw_ostream &o);
67
68private:
69 void EmitPrintInstruction(raw_ostream &o);
70 void EmitGetRegisterName(raw_ostream &o);
71 void EmitPrintAliasInstruction(raw_ostream &O);
72
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000073 void FindUniqueOperandCommands(std::vector<std::string> &UOC,
Craig Topper5dd7a2c2016-01-24 07:13:28 +000074 std::vector<std::vector<unsigned>> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +000075 std::vector<unsigned> &InstOpsUsed,
76 bool PassSubtarget) const;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000077};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000078
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000079} // end anonymous namespace
80
Chris Lattner59a7f5c2005-01-22 20:31:17 +000081static void PrintCases(std::vector<std::pair<std::string,
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000082 AsmWriterOperand>> &OpsToPrint, raw_ostream &O,
Craig Topperc24a4012016-01-14 06:15:07 +000083 bool PassSubtarget) {
Craig Topper0b271ad2016-01-13 07:20:13 +000084 O << " case " << OpsToPrint.back().first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000085 AsmWriterOperand TheOp = OpsToPrint.back().second;
86 OpsToPrint.pop_back();
87
88 // Check to see if any other operands are identical in this list, and if so,
89 // emit a case label for them.
90 for (unsigned i = OpsToPrint.size(); i != 0; --i)
91 if (OpsToPrint[i-1].second == TheOp) {
Craig Topper0b271ad2016-01-13 07:20:13 +000092 O << "\n case " << OpsToPrint[i-1].first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000093 OpsToPrint.erase(OpsToPrint.begin()+i-1);
94 }
95
96 // Finally, emit the code.
Craig Topperc24a4012016-01-14 06:15:07 +000097 O << "\n " << TheOp.getCode(PassSubtarget);
Craig Topper0b271ad2016-01-13 07:20:13 +000098 O << "\n break;\n";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000099}
100
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000101/// EmitInstructions - Emit the last instruction in the vector and any other
102/// instructions that are suitably similar to it.
103static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Craig Topperc24a4012016-01-14 06:15:07 +0000104 raw_ostream &O, bool PassSubtarget) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000105 AsmWriterInst FirstInst = Insts.back();
106 Insts.pop_back();
107
108 std::vector<AsmWriterInst> SimilarInsts;
109 unsigned DifferingOperand = ~0;
110 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattner92275bb2005-01-22 19:22:23 +0000111 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
112 if (DiffOp != ~1U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000113 if (DifferingOperand == ~0U) // First match!
114 DifferingOperand = DiffOp;
115
116 // If this differs in the same operand as the rest of the instructions in
117 // this class, move it to the SimilarInsts list.
Chris Lattner92275bb2005-01-22 19:22:23 +0000118 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000119 SimilarInsts.push_back(Insts[i-1]);
120 Insts.erase(Insts.begin()+i-1);
121 }
122 }
123 }
124
Chris Lattner017b93d2006-05-01 17:01:17 +0000125 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000126 << FirstInst.CGI->TheDef->getName() << ":\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000127 for (const AsmWriterInst &AWI : SimilarInsts)
128 O << " case " << AWI.CGI->Namespace << "::"
129 << AWI.CGI->TheDef->getName() << ":\n";
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000130 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
131 if (i != DifferingOperand) {
132 // If the operand is the same for all instructions, just print it.
Craig Topperc24a4012016-01-14 06:15:07 +0000133 O << " " << FirstInst.Operands[i].getCode(PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000134 } else {
135 // If this is the operand that varies between all of the instructions,
136 // emit a switch for just this operand now.
137 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000138 O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000139 std::vector<std::pair<std::string, AsmWriterOperand>> OpsToPrint;
Craig Topper86a9aee2017-07-07 06:22:35 +0000140 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace.str() + "::" +
Matthias Braun4a86d452016-12-04 05:48:16 +0000141 FirstInst.CGI->TheDef->getName().str(),
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000142 FirstInst.Operands[i]));
Misha Brukman650ba8e2005-04-22 00:00:37 +0000143
Craig Topper190ecd52016-01-08 07:06:32 +0000144 for (const AsmWriterInst &AWI : SimilarInsts) {
Craig Topper86a9aee2017-07-07 06:22:35 +0000145 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace.str()+"::" +
Matthias Braun4a86d452016-12-04 05:48:16 +0000146 AWI.CGI->TheDef->getName().str(),
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000147 AWI.Operands[i]));
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000148 }
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000149 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
150 while (!OpsToPrint.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000151 PrintCases(OpsToPrint, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000152 O << " }";
153 }
154 O << "\n";
155 }
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000156 O << " break;\n";
157}
Chris Lattner0c23ba52005-01-22 17:32:42 +0000158
Chris Lattner692374c2006-07-18 17:18:03 +0000159void AsmWriterEmitter::
Jim Grosbacha5497342010-09-29 22:32:50 +0000160FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000161 std::vector<std::vector<unsigned>> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000162 std::vector<unsigned> &InstOpsUsed,
163 bool PassSubtarget) const {
Chris Lattner692374c2006-07-18 17:18:03 +0000164 // This vector parallels UniqueOperandCommands, keeping track of which
165 // instructions each case are used for. It is a comma separated string of
166 // enums.
167 std::vector<std::string> InstrsForCase;
168 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattneredee5252006-07-18 18:28:27 +0000169 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbacha5497342010-09-29 22:32:50 +0000170
Craig Topper9e9ae602016-01-17 08:05:33 +0000171 for (size_t i = 0, e = Instructions.size(); i != e; ++i) {
172 const AsmWriterInst &Inst = Instructions[i];
173 if (Inst.Operands.empty())
Chris Lattner692374c2006-07-18 17:18:03 +0000174 continue; // Instruction already done.
Chris Lattner9d250692006-07-18 17:50:22 +0000175
Craig Topper9e9ae602016-01-17 08:05:33 +0000176 std::string Command = " "+Inst.Operands[0].getCode(PassSubtarget)+"\n";
Chris Lattner9d250692006-07-18 17:50:22 +0000177
Chris Lattner692374c2006-07-18 17:18:03 +0000178 // Check to see if we already have 'Command' in UniqueOperandCommands.
179 // If not, add it.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000180 auto I = llvm::find(UniqueOperandCommands, Command);
Craig Toppera99859d2016-01-17 08:05:30 +0000181 if (I != UniqueOperandCommands.end()) {
182 size_t idx = I - UniqueOperandCommands.begin();
Craig Toppera99859d2016-01-17 08:05:30 +0000183 InstrsForCase[idx] += ", ";
Craig Topper9e9ae602016-01-17 08:05:33 +0000184 InstrsForCase[idx] += Inst.CGI->TheDef->getName();
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000185 InstIdxs[idx].push_back(i);
Craig Toppera99859d2016-01-17 08:05:30 +0000186 } else {
Craig Topper1993e3b2016-01-08 07:06:29 +0000187 UniqueOperandCommands.push_back(std::move(Command));
Craig Topper9e9ae602016-01-17 08:05:33 +0000188 InstrsForCase.push_back(Inst.CGI->TheDef->getName());
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000189 InstIdxs.emplace_back();
190 InstIdxs.back().push_back(i);
Chris Lattneredee5252006-07-18 18:28:27 +0000191
192 // This command matches one operand so far.
193 InstOpsUsed.push_back(1);
194 }
195 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000196
Chris Lattneredee5252006-07-18 18:28:27 +0000197 // For each entry of UniqueOperandCommands, there is a set of instructions
198 // that uses it. If the next command of all instructions in the set are
199 // identical, fold it into the command.
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000200 for (size_t CommandIdx = 0, e = UniqueOperandCommands.size();
Chris Lattneredee5252006-07-18 18:28:27 +0000201 CommandIdx != e; ++CommandIdx) {
Jim Grosbacha5497342010-09-29 22:32:50 +0000202
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000203 const auto &Idxs = InstIdxs[CommandIdx];
Chris Lattneredee5252006-07-18 18:28:27 +0000204
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000205 for (unsigned Op = 1; ; ++Op) {
206 // Find the first instruction in the set.
207 const AsmWriterInst &FirstInst = Instructions[Idxs.front()];
Chris Lattneredee5252006-07-18 18:28:27 +0000208 // If this instruction has no more operands, we isn't anything to merge
209 // into this command.
Craig Topper9e9ae602016-01-17 08:05:33 +0000210 if (FirstInst.Operands.size() == Op)
Chris Lattneredee5252006-07-18 18:28:27 +0000211 break;
212
213 // Otherwise, scan to see if all of the other instructions in this command
214 // set share the operand.
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000215 if (std::any_of(Idxs.begin()+1, Idxs.end(),
216 [&](unsigned Idx) {
217 const AsmWriterInst &OtherInst = Instructions[Idx];
218 return OtherInst.Operands.size() == Op ||
219 OtherInst.Operands[Op] != FirstInst.Operands[Op];
220 }))
221 break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000222
Chris Lattneredee5252006-07-18 18:28:27 +0000223 // Okay, everything in this command set has the same next operand. Add it
224 // to UniqueOperandCommands and remember that it was consumed.
Craig Topperc24a4012016-01-14 06:15:07 +0000225 std::string Command = " " +
Craig Topper9e9ae602016-01-17 08:05:33 +0000226 FirstInst.Operands[Op].getCode(PassSubtarget) + "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000227
Chris Lattneredee5252006-07-18 18:28:27 +0000228 UniqueOperandCommands[CommandIdx] += Command;
229 InstOpsUsed[CommandIdx]++;
Chris Lattner692374c2006-07-18 17:18:03 +0000230 }
231 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000232
Chris Lattner692374c2006-07-18 17:18:03 +0000233 // Prepend some of the instructions each case is used for onto the case val.
234 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
235 std::string Instrs = InstrsForCase[i];
236 if (Instrs.size() > 70) {
237 Instrs.erase(Instrs.begin()+70, Instrs.end());
238 Instrs += "...";
239 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000240
Chris Lattner692374c2006-07-18 17:18:03 +0000241 if (!Instrs.empty())
Jim Grosbacha5497342010-09-29 22:32:50 +0000242 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattner692374c2006-07-18 17:18:03 +0000243 UniqueOperandCommands[i];
244 }
245}
246
Daniel Dunbar04f049f2009-10-17 20:43:42 +0000247static void UnescapeString(std::string &Str) {
248 for (unsigned i = 0; i != Str.size(); ++i) {
249 if (Str[i] == '\\' && i != Str.size()-1) {
250 switch (Str[i+1]) {
251 default: continue; // Don't execute the code after the switch.
252 case 'a': Str[i] = '\a'; break;
253 case 'b': Str[i] = '\b'; break;
254 case 'e': Str[i] = 27; break;
255 case 'f': Str[i] = '\f'; break;
256 case 'n': Str[i] = '\n'; break;
257 case 'r': Str[i] = '\r'; break;
258 case 't': Str[i] = '\t'; break;
259 case 'v': Str[i] = '\v'; break;
260 case '"': Str[i] = '\"'; break;
261 case '\'': Str[i] = '\''; break;
262 case '\\': Str[i] = '\\'; break;
263 }
264 // Nuke the second character.
265 Str.erase(Str.begin()+i+1);
266 }
267 }
268}
269
Chris Lattner06c5eed2009-09-13 20:08:00 +0000270/// EmitPrintInstruction - Generate the code for the "printInstruction" method
Ahmed Bougachabd214002013-10-28 18:07:17 +0000271/// implementation. Destroys all instances of AsmWriterInst information, by
272/// clearing the Instructions vector.
Chris Lattner06c5eed2009-09-13 20:08:00 +0000273void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner6ffa5012004-08-14 22:50:53 +0000274 Record *AsmWriter = Target.getAsmWriter();
Craig Topperbcd3c372017-05-31 21:12:46 +0000275 StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Craig Topperc24a4012016-01-14 06:15:07 +0000276 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
Jim Grosbacha5497342010-09-29 22:32:50 +0000277
Chris Lattner1c4ae852004-08-01 05:59:33 +0000278 O <<
279 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner06c5eed2009-09-13 20:08:00 +0000280 "/// from the instruction set description.\n"
Chris Lattnerb94284b2009-08-08 01:32:19 +0000281 "void " << Target.getName() << ClassName
Akira Hatanakab46d0232015-03-27 20:36:02 +0000282 << "::printInstruction(const MCInst *MI, "
283 << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
284 << "raw_ostream &O) {\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000285
Chris Lattnere32982c2006-07-14 22:59:11 +0000286 // Build an aggregate string, and build a table of offsets into it.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000287 SequenceToOffsetTable<std::string> StringTable;
Jim Grosbacha5497342010-09-29 22:32:50 +0000288
Chris Lattner5d751b42006-09-27 16:44:09 +0000289 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000290 /// chunk of the output as well as indices used for operand printing.
Craig Topperf9265322016-01-17 20:38:14 +0000291 std::vector<uint64_t> OpcodeInfo(NumberedInstructions.size());
Craig Topperd4f87a32016-01-13 07:20:12 +0000292 const unsigned OpcodeInfoBits = 64;
Jim Grosbacha5497342010-09-29 22:32:50 +0000293
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000294 // Add all strings to the string table upfront so it can generate an optimized
295 // representation.
Craig Topper9e9ae602016-01-17 08:05:33 +0000296 for (AsmWriterInst &AWI : Instructions) {
297 if (AWI.Operands[0].OperandType ==
Jim Grosbachf4e67082012-04-18 18:56:33 +0000298 AsmWriterOperand::isLiteralTextOperand &&
Craig Topper9e9ae602016-01-17 08:05:33 +0000299 !AWI.Operands[0].Str.empty()) {
300 std::string Str = AWI.Operands[0].Str;
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000301 UnescapeString(Str);
302 StringTable.add(Str);
303 }
304 }
305
306 StringTable.layout();
307
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000308 unsigned MaxStringIdx = 0;
Craig Topper9e9ae602016-01-17 08:05:33 +0000309 for (AsmWriterInst &AWI : Instructions) {
Chris Lattnere32982c2006-07-14 22:59:11 +0000310 unsigned Idx;
Craig Topper9e9ae602016-01-17 08:05:33 +0000311 if (AWI.Operands[0].OperandType != AsmWriterOperand::isLiteralTextOperand ||
312 AWI.Operands[0].Str.empty()) {
Chris Lattner36504652006-07-19 01:39:06 +0000313 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000314 Idx = StringTable.get("");
Chris Lattnere32982c2006-07-14 22:59:11 +0000315 } else {
Craig Topper9e9ae602016-01-17 08:05:33 +0000316 std::string Str = AWI.Operands[0].Str;
Chris Lattnerb47ed612009-09-14 01:16:36 +0000317 UnescapeString(Str);
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000318 Idx = StringTable.get(Str);
Chris Lattnerb47ed612009-09-14 01:16:36 +0000319 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbacha5497342010-09-29 22:32:50 +0000320
Chris Lattnere32982c2006-07-14 22:59:11 +0000321 // Nuke the string from the operand list. It is now handled!
Craig Topper9e9ae602016-01-17 08:05:33 +0000322 AWI.Operands.erase(AWI.Operands.begin());
Chris Lattner92275bb2005-01-22 19:22:23 +0000323 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000324
Chris Lattnerb47ed612009-09-14 01:16:36 +0000325 // Bias offset by one since we want 0 as a sentinel.
Craig Topper9e9ae602016-01-17 08:05:33 +0000326 OpcodeInfo[AWI.CGIIndex] = Idx+1;
Chris Lattner92275bb2005-01-22 19:22:23 +0000327 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000328
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000329 // Figure out how many bits we used for the string index.
Chris Lattnerb47ed612009-09-14 01:16:36 +0000330 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbacha5497342010-09-29 22:32:50 +0000331
Chris Lattner692374c2006-07-18 17:18:03 +0000332 // To reduce code size, we compactify common instructions into a few bits
333 // in the opcode-indexed table.
Craig Topperd4f87a32016-01-13 07:20:12 +0000334 unsigned BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000335
Craig Topper1f387362014-11-25 20:11:25 +0000336 std::vector<std::vector<std::string>> TableDrivenOperandPrinters;
Jim Grosbacha5497342010-09-29 22:32:50 +0000337
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000338 while (true) {
Chris Lattner692374c2006-07-18 17:18:03 +0000339 std::vector<std::string> UniqueOperandCommands;
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000340 std::vector<std::vector<unsigned>> InstIdxs;
Chris Lattneredee5252006-07-18 18:28:27 +0000341 std::vector<unsigned> NumInstOpsHandled;
342 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000343 NumInstOpsHandled, PassSubtarget);
Jim Grosbacha5497342010-09-29 22:32:50 +0000344
Chris Lattner692374c2006-07-18 17:18:03 +0000345 // If we ran out of operands to print, we're done.
346 if (UniqueOperandCommands.empty()) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000347
Chris Lattner692374c2006-07-18 17:18:03 +0000348 // Compute the number of bits we need to represent these cases, this is
349 // ceil(log2(numentries)).
350 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbacha5497342010-09-29 22:32:50 +0000351
Chris Lattner692374c2006-07-18 17:18:03 +0000352 // If we don't have enough bits for this operand, don't include it.
353 if (NumBits > BitsLeft) {
Chris Lattner34822f62009-08-23 04:44:11 +0000354 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
355 << " more bits\n");
Chris Lattner692374c2006-07-18 17:18:03 +0000356 break;
357 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000358
Chris Lattner692374c2006-07-18 17:18:03 +0000359 // Otherwise, we can include this in the initial lookup table. Add it in.
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000360 for (size_t i = 0, e = InstIdxs.size(); i != e; ++i) {
361 unsigned NumOps = NumInstOpsHandled[i];
362 for (unsigned Idx : InstIdxs[i]) {
363 OpcodeInfo[Instructions[Idx].CGIIndex] |=
364 (uint64_t)i << (OpcodeInfoBits-BitsLeft);
365 // Remove the info about this operand from the instruction.
366 AsmWriterInst &Inst = Instructions[Idx];
367 if (!Inst.Operands.empty()) {
368 assert(NumOps <= Inst.Operands.size() &&
369 "Can't remove this many ops!");
370 Inst.Operands.erase(Inst.Operands.begin(),
371 Inst.Operands.begin()+NumOps);
372 }
Craig Topper9e9ae602016-01-17 08:05:33 +0000373 }
Chris Lattnercb0c8482006-07-18 17:56:07 +0000374 }
Craig Topper5dd7a2c2016-01-24 07:13:28 +0000375 BitsLeft -= NumBits;
Jim Grosbacha5497342010-09-29 22:32:50 +0000376
Chris Lattnercb0c8482006-07-18 17:56:07 +0000377 // Remember the handlers for this set of operands.
Craig Topper1f387362014-11-25 20:11:25 +0000378 TableDrivenOperandPrinters.push_back(std::move(UniqueOperandCommands));
Chris Lattner692374c2006-07-18 17:18:03 +0000379 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000380
Craig Topper14d91732016-01-11 05:13:41 +0000381 // Emit the string table itself.
Reid Kleckner132c40f2014-07-17 19:43:40 +0000382 O << " static const char AsmStrs[] = {\n";
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000383 StringTable.emit(O, printChar);
384 O << " };\n\n";
Chris Lattnere32982c2006-07-14 22:59:11 +0000385
Craig Topper14d91732016-01-11 05:13:41 +0000386 // Emit the lookup tables in pieces to minimize wasted bytes.
Craig Topperd4f87a32016-01-13 07:20:12 +0000387 unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
Craig Topper14d91732016-01-11 05:13:41 +0000388 unsigned Table = 0, Shift = 0;
389 SmallString<128> BitsString;
390 raw_svector_ostream BitsOS(BitsString);
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 BitsOS << " uint" << ((BitsLeft < (OpcodeInfoBits - 32)) ? 64 : 32)
393 << "_t Bits = 0;\n";
Craig Topper14d91732016-01-11 05:13:41 +0000394 while (BytesNeeded != 0) {
395 // Figure out how big this table section needs to be, but no bigger than 4.
396 unsigned TableSize = std::min(1 << Log2_32(BytesNeeded), 4);
397 BytesNeeded -= TableSize;
398 TableSize *= 8; // Convert to bits;
399 uint64_t Mask = (1ULL << TableSize) - 1;
400 O << " static const uint" << TableSize << "_t OpInfo" << Table
401 << "[] = {\n";
Craig Topperf9265322016-01-17 20:38:14 +0000402 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Craig Topper14d91732016-01-11 05:13:41 +0000403 O << " " << ((OpcodeInfo[i] >> Shift) & Mask) << "U,\t// "
Craig Topperf9265322016-01-17 20:38:14 +0000404 << NumberedInstructions[i]->TheDef->getName() << "\n";
Craig Topper14d91732016-01-11 05:13:41 +0000405 }
406 O << " };\n\n";
407 // Emit string to combine the individual table lookups.
408 BitsOS << " Bits |= ";
409 // If the total bits is more than 32-bits we need to use a 64-bit type.
Craig Topperd4f87a32016-01-13 07:20:12 +0000410 if (BitsLeft < (OpcodeInfoBits - 32))
Craig Topper14d91732016-01-11 05:13:41 +0000411 BitsOS << "(uint64_t)";
412 BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
413 // Prepare the shift for the next iteration and increment the table count.
414 Shift += TableSize;
415 ++Table;
416 }
417
418 // Emit the initial tab character.
Evan Cheng32e53472008-02-02 08:39:46 +0000419 O << " O << \"\\t\";\n\n";
420
Craig Topper06cec4c2012-09-14 08:33:11 +0000421 O << " // Emit the opcode for the instruction.\n";
Craig Topper14d91732016-01-11 05:13:41 +0000422 O << BitsString;
423
424 // Emit the starting string.
Manman Ren68cf9fc2012-09-13 17:43:46 +0000425 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattnerb47ed612009-09-14 01:16:36 +0000426 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenefdd25192009-08-05 21:00:52 +0000427
Chris Lattner692374c2006-07-18 17:18:03 +0000428 // Output the table driven operand information.
Craig Topperd4f87a32016-01-13 07:20:12 +0000429 BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000430 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
431 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
432
433 // Compute the number of bits we need to represent these cases, this is
434 // ceil(log2(numentries)).
435 unsigned NumBits = Log2_32_Ceil(Commands.size());
436 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbacha5497342010-09-29 22:32:50 +0000437
Chris Lattner692374c2006-07-18 17:18:03 +0000438 // Emit code to extract this field from Bits.
Chris Lattner692374c2006-07-18 17:18:03 +0000439 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000440 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000441
Chris Lattneredee5252006-07-18 18:28:27 +0000442 if (Commands.size() == 2) {
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000443 // Emit two possibilitys with if/else.
Craig Topper06cec4c2012-09-14 08:33:11 +0000444 O << " if ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000445 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000446 << ((1 << NumBits)-1) << ") {\n"
447 << Commands[1]
448 << " } else {\n"
449 << Commands[0]
450 << " }\n\n";
Eric Christophera573d192010-09-18 18:50:27 +0000451 } else if (Commands.size() == 1) {
452 // Emit a single possibility.
453 O << Commands[0] << "\n\n";
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000454 } else {
Craig Topper06cec4c2012-09-14 08:33:11 +0000455 O << " switch ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000456 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000457 << ((1 << NumBits)-1) << ") {\n"
Craig Toppera4ff6ae2014-11-24 14:09:52 +0000458 << " default: llvm_unreachable(\"Invalid command number.\");\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000459
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000460 // Print out all the cases.
Craig Topper190ecd52016-01-08 07:06:32 +0000461 for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
462 O << " case " << j << ":\n";
463 O << Commands[j];
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000464 O << " break;\n";
465 }
466 O << " }\n\n";
Chris Lattner692374c2006-07-18 17:18:03 +0000467 }
Craig Topper06cec4c2012-09-14 08:33:11 +0000468 BitsLeft -= NumBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000469 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000470
Chris Lattnercb0c8482006-07-18 17:56:07 +0000471 // Okay, delete instructions with no operand info left.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000472 auto I = llvm::remove_if(Instructions,
David Majnemerc7004902016-08-12 04:32:37 +0000473 [](AsmWriterInst &Inst) { return Inst.Operands.empty(); });
Craig Topper4f1f1152016-01-13 07:20:10 +0000474 Instructions.erase(I, Instructions.end());
Chris Lattner692374c2006-07-18 17:18:03 +0000475
Jim Grosbacha5497342010-09-29 22:32:50 +0000476
Chris Lattner692374c2006-07-18 17:18:03 +0000477 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000478 // elements in the vector.
479 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbacha5497342010-09-29 22:32:50 +0000480
481
Craig Topperdf390602016-01-13 07:20:07 +0000482 // Now that we've emitted all of the operand info that fit into 64 bits, emit
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000483 // information for those instructions that are left. This is a less dense
Craig Topperdf390602016-01-13 07:20:07 +0000484 // encoding, but we expect the main 64-bit table to handle the majority of
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000485 // instructions.
Chris Lattner66e288b2006-07-18 17:38:46 +0000486 if (!Instructions.empty()) {
487 // Find the opcode # of inline asm.
488 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000489 O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
Chris Lattner66e288b2006-07-18 17:38:46 +0000490 while (!Instructions.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000491 EmitInstructions(Instructions, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000492
Chris Lattner66e288b2006-07-18 17:38:46 +0000493 O << " }\n";
494 }
David Greene5b4bc262009-07-29 20:10:24 +0000495
Chris Lattner6e172082006-07-18 19:06:01 +0000496 O << "}\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000497}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000498
Owen Andersona84be6c2011-06-27 21:06:21 +0000499static void
500emitRegisterNameString(raw_ostream &O, StringRef AltName,
David Blaikie9b613db2014-11-29 18:13:39 +0000501 const std::deque<CodeGenRegister> &Registers) {
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000502 SequenceToOffsetTable<std::string> StringTable;
503 SmallVector<std::string, 4> AsmNames(Registers.size());
David Blaikie9b613db2014-11-29 18:13:39 +0000504 unsigned i = 0;
505 for (const auto &Reg : Registers) {
506 std::string &AsmName = AsmNames[i++];
Owen Andersona84be6c2011-06-27 21:06:21 +0000507
Owen Andersona84be6c2011-06-27 21:06:21 +0000508 // "NoRegAltName" is special. We don't need to do a lookup for that,
509 // as it's just a reference to the default register name.
510 if (AltName == "" || AltName == "NoRegAltName") {
511 AsmName = Reg.TheDef->getValueAsString("AsmName");
512 if (AsmName.empty())
513 AsmName = Reg.getName();
514 } else {
515 // Make sure the register has an alternate name for this index.
516 std::vector<Record*> AltNameList =
517 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
518 unsigned Idx = 0, e;
519 for (e = AltNameList.size();
520 Idx < e && (AltNameList[Idx]->getName() != AltName);
521 ++Idx)
522 ;
523 // If the register has an alternate name for this index, use it.
524 // Otherwise, leave it empty as an error flag.
525 if (Idx < e) {
Craig Topper2b8419a2017-05-31 19:01:11 +0000526 std::vector<StringRef> AltNames =
Owen Andersona84be6c2011-06-27 21:06:21 +0000527 Reg.TheDef->getValueAsListOfStrings("AltNames");
528 if (AltNames.size() <= Idx)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000529 PrintFatalError(Reg.TheDef->getLoc(),
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000530 "Register definition missing alt name for '" +
531 AltName + "'.");
Owen Andersona84be6c2011-06-27 21:06:21 +0000532 AsmName = AltNames[Idx];
533 }
534 }
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000535 StringTable.add(AsmName);
536 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000537
Craig Topperf8f0a232012-09-15 01:22:42 +0000538 StringTable.layout();
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000539 O << " static const char AsmStrs" << AltName << "[] = {\n";
540 StringTable.emit(O, printChar);
541 O << " };\n\n";
542
Daniel Sandersca89f3a2016-11-19 12:21:34 +0000543 O << " static const " << getMinimalTypeForRange(StringTable.size() - 1, 32)
Craig Topperba6d83e2014-11-24 02:08:35 +0000544 << " RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000545 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Topper7a2cea12012-04-02 00:47:39 +0000546 if ((i % 14) == 0)
547 O << "\n ";
548 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersona84be6c2011-06-27 21:06:21 +0000549 }
Craig Topper9c252eb2012-04-03 06:52:47 +0000550 O << "\n };\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000551 << "\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000552}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000553
554void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +0000555 Record *AsmWriter = Target.getAsmWriter();
Craig Topperbcd3c372017-05-31 21:12:46 +0000556 StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
David Blaikie9b613db2014-11-29 18:13:39 +0000557 const auto &Registers = Target.getRegBank().getRegisters();
Craig Topper83421ec2016-01-17 20:38:21 +0000558 const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
Owen Andersona84be6c2011-06-27 21:06:21 +0000559 bool hasAltNames = AltNameIndices.size() > 1;
Craig Topperbcd3c372017-05-31 21:12:46 +0000560 StringRef Namespace = Registers.front().TheDef->getValueAsString("Namespace");
Jim Grosbacha5497342010-09-29 22:32:50 +0000561
Chris Lattner06c5eed2009-09-13 20:08:00 +0000562 O <<
563 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
564 "/// from the register set description. This returns the assembler name\n"
565 "/// for the specified register.\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000566 "const char *" << Target.getName() << ClassName << "::";
567 if (hasAltNames)
568 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
569 else
570 O << "getRegisterName(unsigned RegNo) {\n";
571 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
572 << " && \"Invalid register number!\");\n"
Chris Lattnera7e8ae42009-09-14 01:26:18 +0000573 << "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000574
Owen Andersona84be6c2011-06-27 21:06:21 +0000575 if (hasAltNames) {
Craig Topper190ecd52016-01-08 07:06:32 +0000576 for (const Record *R : AltNameIndices)
577 emitRegisterNameString(O, R->getName(), Registers);
Owen Andersona84be6c2011-06-27 21:06:21 +0000578 } else
579 emitRegisterNameString(O, "", Registers);
Jim Grosbacha5497342010-09-29 22:32:50 +0000580
Owen Andersona84be6c2011-06-27 21:06:21 +0000581 if (hasAltNames) {
Craig Topperba6d83e2014-11-24 02:08:35 +0000582 O << " switch(AltIdx) {\n"
Craig Topperc4965bc2012-02-05 07:21:30 +0000583 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000584 for (const Record *R : AltNameIndices) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000585 StringRef AltName = R->getName();
586 O << " case ";
587 if (!Namespace.empty())
588 O << Namespace << "::";
589 O << AltName << ":\n"
590 << " assert(*(AsmStrs" << AltName << "+RegAsmOffset" << AltName
591 << "[RegNo-1]) &&\n"
Craig Topperba6d83e2014-11-24 02:08:35 +0000592 << " \"Invalid alt name index for register!\");\n"
Craig Topperbcd3c372017-05-31 21:12:46 +0000593 << " return AsmStrs" << AltName << "+RegAsmOffset" << AltName
594 << "[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000595 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000596 O << " }\n";
597 } else {
598 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
599 << " \"Invalid alt name index for register!\");\n"
600 << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000601 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000602 O << "}\n";
Chris Lattner06c5eed2009-09-13 20:08:00 +0000603}
604
Bill Wendling7e5771d2011-03-21 08:31:53 +0000605namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000606
Bill Wendling5d3174c2011-03-21 08:40:31 +0000607// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
608// they both have the same conditionals. In which case, we cannot print out the
609// alias for that pattern.
610class IAPrinter {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000611 std::vector<std::string> Conds;
Tim Northoveree20caa2014-05-12 18:04:06 +0000612 std::map<StringRef, std::pair<int, int>> OpMap;
Tim Northoveree20caa2014-05-12 18:04:06 +0000613
Bill Wendling5d3174c2011-03-21 08:40:31 +0000614 std::string Result;
615 std::string AsmString;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000616
Bill Wendling5d3174c2011-03-21 08:40:31 +0000617public:
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000618 IAPrinter(std::string R, std::string AS)
619 : Result(std::move(R)), AsmString(std::move(AS)) {}
Bill Wendling5d3174c2011-03-21 08:40:31 +0000620
621 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000622
Tim Northoveree20caa2014-05-12 18:04:06 +0000623 void addOperand(StringRef Op, int OpIdx, int PrintMethodIdx = -1) {
624 assert(OpIdx >= 0 && OpIdx < 0xFE && "Idx out of range");
Tim Northover0ee9e7e2014-05-13 09:37:41 +0000625 assert(PrintMethodIdx >= -1 && PrintMethodIdx < 0xFF &&
Jay Foadb3590512014-05-13 08:26:53 +0000626 "Idx out of range");
Tim Northoveree20caa2014-05-12 18:04:06 +0000627 OpMap[Op] = std::make_pair(OpIdx, PrintMethodIdx);
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000628 }
Tim Northoveree20caa2014-05-12 18:04:06 +0000629
Bill Wendling5d3174c2011-03-21 08:40:31 +0000630 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
Tim Northoveree20caa2014-05-12 18:04:06 +0000631 int getOpIndex(StringRef Op) { return OpMap[Op].first; }
632 std::pair<int, int> &getOpData(StringRef Op) { return OpMap[Op]; }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000633
Tim Northoverd8d65a62014-05-15 11:16:32 +0000634 std::pair<StringRef, StringRef::iterator> parseName(StringRef::iterator Start,
635 StringRef::iterator End) {
636 StringRef::iterator I = Start;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000637 StringRef::iterator Next;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000638 if (*I == '{') {
639 // ${some_name}
640 Start = ++I;
641 while (I != End && *I != '}')
642 ++I;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000643 Next = I;
644 // eat the final '}'
645 if (Next != End)
646 ++Next;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000647 } else {
648 // $name, just eat the usual suspects.
649 while (I != End &&
650 ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') ||
651 (*I >= '0' && *I <= '9') || *I == '_'))
652 ++I;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000653 Next = I;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000654 }
655
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000656 return std::make_pair(StringRef(Start, I - Start), Next);
Tim Northoverd8d65a62014-05-15 11:16:32 +0000657 }
658
Evan Cheng4d806e22011-07-06 02:02:33 +0000659 void print(raw_ostream &O) {
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000660 if (Conds.empty()) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000661 O.indent(6) << "return true;\n";
Evan Cheng4d806e22011-07-06 02:02:33 +0000662 return;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000663 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000664
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000665 O << "if (";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000666
667 for (std::vector<std::string>::iterator
668 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
669 if (I != Conds.begin()) {
670 O << " &&\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000671 O.indent(8);
Bill Wendling5d3174c2011-03-21 08:40:31 +0000672 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000673
Bill Wendling5d3174c2011-03-21 08:40:31 +0000674 O << *I;
675 }
676
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000677 O << ") {\n";
678 O.indent(6) << "// " << Result << "\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000679
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000680 // Directly mangle mapped operands into the string. Each operand is
681 // identified by a '$' sign followed by a byte identifying the number of the
682 // operand. We add one to the index to avoid zero bytes.
Tim Northoverd8d65a62014-05-15 11:16:32 +0000683 StringRef ASM(AsmString);
684 SmallString<128> OutString;
685 raw_svector_ostream OS(OutString);
686 for (StringRef::iterator I = ASM.begin(), E = ASM.end(); I != E;) {
687 OS << *I;
688 if (*I == '$') {
689 StringRef Name;
690 std::tie(Name, I) = parseName(++I, E);
691 assert(isOpMapped(Name) && "Unmapped operand!");
Tim Northoveree20caa2014-05-12 18:04:06 +0000692
Tim Northoverd8d65a62014-05-15 11:16:32 +0000693 int OpIndex, PrintIndex;
694 std::tie(OpIndex, PrintIndex) = getOpData(Name);
695 if (PrintIndex == -1) {
696 // Can use the default printOperand route.
697 OS << format("\\x%02X", (unsigned char)OpIndex + 1);
698 } else
699 // 3 bytes if a PrintMethod is needed: 0xFF, the MCInst operand
700 // number, and which of our pre-detected Methods to call.
701 OS << format("\\xFF\\x%02X\\x%02X", OpIndex + 1, PrintIndex + 1);
702 } else {
703 ++I;
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000704 }
705 }
706
707 // Emit the string.
Yaron Keren09fb7c62015-03-10 07:33:23 +0000708 O.indent(6) << "AsmString = \"" << OutString << "\";\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000709
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000710 O.indent(6) << "break;\n";
711 O.indent(4) << '}';
Bill Wendling5d3174c2011-03-21 08:40:31 +0000712 }
713
David Blaikie4ab57cd2015-08-06 19:23:33 +0000714 bool operator==(const IAPrinter &RHS) const {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000715 if (Conds.size() != RHS.Conds.size())
716 return false;
717
718 unsigned Idx = 0;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000719 for (const auto &str : Conds)
720 if (str != RHS.Conds[Idx++])
Bill Wendling5d3174c2011-03-21 08:40:31 +0000721 return false;
722
723 return true;
724 }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000725};
726
Bill Wendling7e5771d2011-03-21 08:31:53 +0000727} // end anonymous namespace
728
Tim Northover5896b062014-05-16 09:42:04 +0000729static unsigned CountNumOperands(StringRef AsmString, unsigned Variant) {
730 std::string FlatAsmString =
731 CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant);
732 AsmString = FlatAsmString;
Bill Wendlinge7124492011-06-14 03:17:20 +0000733
Tim Northover5896b062014-05-16 09:42:04 +0000734 return AsmString.count(' ') + AsmString.count('\t');
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000735}
Bill Wendlinge7124492011-06-14 03:17:20 +0000736
Tim Northover9a24f882014-05-20 09:17:16 +0000737namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000738
Tim Northover9a24f882014-05-20 09:17:16 +0000739struct AliasPriorityComparator {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000740 typedef std::pair<CodeGenInstAlias, int> ValueType;
Eric Fiselieraa54e502016-12-27 23:15:58 +0000741 bool operator()(const ValueType &LHS, const ValueType &RHS) const {
Tim Northover9a24f882014-05-20 09:17:16 +0000742 if (LHS.second == RHS.second) {
743 // We don't actually care about the order, but for consistency it
744 // shouldn't depend on pointer comparisons.
Quentin Colombet21136c02017-02-10 02:43:09 +0000745 return LessRecordByID()(LHS.first.TheDef, RHS.first.TheDef);
Tim Northover9a24f882014-05-20 09:17:16 +0000746 }
747
748 // Aliases with larger priorities should be considered first.
749 return LHS.second > RHS.second;
750 }
751};
Tim Northover9a24f882014-05-20 09:17:16 +0000752
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000753} // end anonymous namespace
Tim Northover9a24f882014-05-20 09:17:16 +0000754
Bill Wendling7e5771d2011-03-21 08:31:53 +0000755void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
Bill Wendling7e5771d2011-03-21 08:31:53 +0000756 Record *AsmWriter = Target.getAsmWriter();
757
758 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
759 O << "#undef PRINT_ALIAS_INSTR\n\n";
760
Tim Northoveree20caa2014-05-12 18:04:06 +0000761 //////////////////////////////
762 // Gather information about aliases we need to print
763 //////////////////////////////
764
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000765 // Emit the method that prints the alias instruction.
Craig Topperbcd3c372017-05-31 21:12:46 +0000766 StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Tim Northover9a24f882014-05-20 09:17:16 +0000767 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Craig Topperc24a4012016-01-14 06:15:07 +0000768 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000769
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.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000774 typedef std::set<std::pair<CodeGenInstAlias, int>, AliasPriorityComparator>
Tim Northover9a24f882014-05-20 09:17:16 +0000775 AliasWithPriority;
776 std::map<std::string, AliasWithPriority> AliasMap;
Craig Topper190ecd52016-01-08 07:06:32 +0000777 for (Record *R : AllInstAliases) {
Tim Northover9a24f882014-05-20 09:17:16 +0000778 int Priority = R->getValueAsInt("EmitPriority");
779 if (Priority < 1)
780 continue; // Aliases with priority 0 are never emitted.
781
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000782 const DagInit *DI = R->getValueAsDag("ResultInst");
Sean Silva88eb8dd2012-10-10 20:24:47 +0000783 const DefInit *Op = cast<DefInit>(DI->getOperator());
David Blaikie4ab57cd2015-08-06 19:23:33 +0000784 AliasMap[getQualifiedName(Op->getDef())].insert(
Craig Topper190ecd52016-01-08 07:06:32 +0000785 std::make_pair(CodeGenInstAlias(R, Variant, Target), Priority));
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000786 }
787
Bill Wendling7e570b52011-03-21 08:59:17 +0000788 // A map of which conditions need to be met for each instruction operand
789 // before it can be matched to the mnemonic.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000790 std::map<std::string, std::vector<IAPrinter>> IAPrinterMap;
Bill Wendling7e570b52011-03-21 08:59:17 +0000791
Craig Topper674d2382016-01-22 05:59:43 +0000792 std::vector<std::string> PrintMethods;
793
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000794 // A list of MCOperandPredicates for all operands in use, and the reverse map
795 std::vector<const Record*> MCOpPredicates;
796 DenseMap<const Record*, unsigned> MCOpPredicateMap;
797
Tim Northover9a24f882014-05-20 09:17:16 +0000798 for (auto &Aliases : AliasMap) {
799 for (auto &Alias : Aliases.second) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000800 const CodeGenInstAlias &CGA = Alias.first;
801 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000802 unsigned NumResultOps =
David Blaikie4ab57cd2015-08-06 19:23:33 +0000803 CountNumOperands(CGA.ResultInst->AsmString, Variant);
Bill Wendlinge7124492011-06-14 03:17:20 +0000804
805 // Don't emit the alias if it has more operands than what it's aliasing.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000806 if (NumResultOps < CountNumOperands(CGA.AsmString, Variant))
Bill Wendlinge7124492011-06-14 03:17:20 +0000807 continue;
808
David Blaikie4ab57cd2015-08-06 19:23:33 +0000809 IAPrinter IAP(CGA.Result->getAsString(), CGA.AsmString);
Bill Wendling7e570b52011-03-21 08:59:17 +0000810
Craig Topperbcd3c372017-05-31 21:12:46 +0000811 StringRef Namespace = Target.getName();
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000812 std::vector<Record *> ReqFeatures;
813 if (PassSubtarget) {
814 // We only consider ReqFeatures predicates if PassSubtarget
815 std::vector<Record *> RF =
816 CGA.TheDef->getValueAsListOfDefs("Predicates");
Sanjoy Das90208722017-02-21 00:38:44 +0000817 copy_if(RF, std::back_inserter(ReqFeatures), [](Record *R) {
818 return R->getValueAsBit("AssemblerMatcherPredicate");
819 });
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000820 }
821
Tim Northover60091cf2014-05-15 13:36:01 +0000822 unsigned NumMIOps = 0;
Amara Emersonc3a4b282017-02-08 21:41:16 +0000823 for (auto &Operand : CGA.ResultOperands)
824 NumMIOps += Operand.getMINumOperands();
Tim Northover60091cf2014-05-15 13:36:01 +0000825
Bill Wendling7e570b52011-03-21 08:59:17 +0000826 std::string Cond;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000827 Cond = std::string("MI->getNumOperands() == ") + utostr(NumMIOps);
David Blaikie4ab57cd2015-08-06 19:23:33 +0000828 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000829
Bill Wendling7e570b52011-03-21 08:59:17 +0000830 bool CantHandle = false;
831
Tim Northover60091cf2014-05-15 13:36:01 +0000832 unsigned MIOpNum = 0;
Bill Wendling7e570b52011-03-21 08:59:17 +0000833 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000834 std::string Op = "MI->getOperand(" + utostr(MIOpNum) + ")";
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000835
David Blaikie4ab57cd2015-08-06 19:23:33 +0000836 const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
Bill Wendling7e570b52011-03-21 08:59:17 +0000837
838 switch (RO.Kind) {
Bill Wendling7e570b52011-03-21 08:59:17 +0000839 case CodeGenInstAlias::ResultOperand::K_Record: {
840 const Record *Rec = RO.getRecord();
841 StringRef ROName = RO.getName();
Tim Northoveree20caa2014-05-12 18:04:06 +0000842 int PrintMethodIdx = -1;
Bill Wendling7e570b52011-03-21 08:59:17 +0000843
Tim Northoveree20caa2014-05-12 18:04:06 +0000844 // These two may have a PrintMethod, which we want to record (if it's
845 // the first time we've seen it) and provide an index for the aliasing
846 // code to use.
847 if (Rec->isSubClassOf("RegisterOperand") ||
848 Rec->isSubClassOf("Operand")) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000849 StringRef PrintMethod = Rec->getValueAsString("PrintMethod");
Tim Northoveree20caa2014-05-12 18:04:06 +0000850 if (PrintMethod != "" && PrintMethod != "printOperand") {
David Majnemer42531262016-08-12 03:55:06 +0000851 PrintMethodIdx =
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000852 llvm::find(PrintMethods, PrintMethod) - PrintMethods.begin();
Tim Northoveree20caa2014-05-12 18:04:06 +0000853 if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
854 PrintMethods.push_back(PrintMethod);
855 }
856 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000857
858 if (Rec->isSubClassOf("RegisterOperand"))
859 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling7e570b52011-03-21 08:59:17 +0000860 if (Rec->isSubClassOf("RegisterClass")) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000861 IAP.addCond(Op + ".isReg()");
Bill Wendling7e570b52011-03-21 08:59:17 +0000862
David Blaikie4ab57cd2015-08-06 19:23:33 +0000863 if (!IAP.isOpMapped(ROName)) {
864 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
865 Record *R = CGA.ResultOperands[i].getRecord();
Jack Carter9c1a0272013-02-05 08:32:10 +0000866 if (R->isSubClassOf("RegisterOperand"))
867 R = R->getValueAsDef("RegClass");
Matthias Braun4a86d452016-12-04 05:48:16 +0000868 Cond = std::string("MRI.getRegClass(") + Target.getName().str() +
869 "::" + R->getName().str() + "RegClassID).contains(" + Op +
870 ".getReg())";
Bill Wendling7e570b52011-03-21 08:59:17 +0000871 } else {
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000872 Cond = Op + ".getReg() == MI->getOperand(" +
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000873 utostr(IAP.getOpIndex(ROName)) + ").getReg()";
Bill Wendling7e570b52011-03-21 08:59:17 +0000874 }
875 } else {
Tim Northoveree20caa2014-05-12 18:04:06 +0000876 // Assume all printable operands are desired for now. This can be
Alp Tokerbeaca192014-05-15 01:52:21 +0000877 // overridden in the InstAlias instantiation if necessary.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000878 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
Bill Wendling7e570b52011-03-21 08:59:17 +0000879
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000880 // There might be an additional predicate on the MCOperand
881 unsigned Entry = MCOpPredicateMap[Rec];
882 if (!Entry) {
883 if (!Rec->isValueUnset("MCOperandPredicate")) {
884 MCOpPredicates.push_back(Rec);
885 Entry = MCOpPredicates.size();
886 MCOpPredicateMap[Rec] = Entry;
887 } else
888 break; // No conditions on this operand at all
889 }
Craig Topperbcd3c372017-05-31 21:12:46 +0000890 Cond = (Target.getName() + ClassName + "ValidateMCOperand(" + Op +
891 ", STI, " + utostr(Entry) + ")")
892 .str();
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000893 }
894 // for all subcases of ResultOperand::K_Record:
David Blaikie4ab57cd2015-08-06 19:23:33 +0000895 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000896 break;
897 }
Tim Northoverab7689e2013-01-09 13:32:04 +0000898 case CodeGenInstAlias::ResultOperand::K_Imm: {
Tim Northoverab7689e2013-01-09 13:32:04 +0000899 // Just because the alias has an immediate result, doesn't mean the
900 // MCInst will. An MCExpr could be present, for example.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000901 IAP.addCond(Op + ".isImm()");
Tim Northoverab7689e2013-01-09 13:32:04 +0000902
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000903 Cond = Op + ".getImm() == " + itostr(CGA.ResultOperands[i].getImm());
David Blaikie4ab57cd2015-08-06 19:23:33 +0000904 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000905 break;
Tim Northoverab7689e2013-01-09 13:32:04 +0000906 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000907 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000908 // If this is zero_reg, something's playing tricks we're not
909 // equipped to handle.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000910 if (!CGA.ResultOperands[i].getRegister()) {
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000911 CantHandle = true;
912 break;
913 }
914
Matthias Braun4a86d452016-12-04 05:48:16 +0000915 Cond = Op + ".getReg() == " + Target.getName().str() + "::" +
916 CGA.ResultOperands[i].getRegister()->getName().str();
David Blaikie4ab57cd2015-08-06 19:23:33 +0000917 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000918 break;
919 }
920
Tim Northover60091cf2014-05-15 13:36:01 +0000921 MIOpNum += RO.getMINumOperands();
Bill Wendling7e570b52011-03-21 08:59:17 +0000922 }
923
924 if (CantHandle) continue;
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000925
926 for (auto I = ReqFeatures.cbegin(); I != ReqFeatures.cend(); I++) {
927 Record *R = *I;
Craig Topperbcd3c372017-05-31 21:12:46 +0000928 StringRef AsmCondString = R->getValueAsString("AssemblerCondString");
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000929
930 // AsmCondString has syntax [!]F(,[!]F)*
931 SmallVector<StringRef, 4> Ops;
932 SplitString(AsmCondString, Ops, ",");
933 assert(!Ops.empty() && "AssemblerCondString cannot be empty");
934
935 for (auto &Op : Ops) {
936 assert(!Op.empty() && "Empty operator");
937 if (Op[0] == '!')
Craig Topperbcd3c372017-05-31 21:12:46 +0000938 Cond = ("!STI.getFeatureBits()[" + Namespace + "::" + Op.substr(1) +
939 "]")
940 .str();
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000941 else
Craig Topperbcd3c372017-05-31 21:12:46 +0000942 Cond =
943 ("STI.getFeatureBits()[" + Namespace + "::" + Op + "]").str();
Sjoerd Meijer84e2f692016-06-03 13:14:19 +0000944 IAP.addCond(Cond);
945 }
946 }
947
David Blaikie4ab57cd2015-08-06 19:23:33 +0000948 IAPrinterMap[Aliases.first].push_back(std::move(IAP));
Bill Wendling7e570b52011-03-21 08:59:17 +0000949 }
950 }
951
Tim Northoveree20caa2014-05-12 18:04:06 +0000952 //////////////////////////////
953 // Write out the printAliasInstr function
954 //////////////////////////////
955
Bill Wendlingf5199de2011-05-23 00:18:33 +0000956 std::string Header;
957 raw_string_ostream HeaderO(Header);
958
959 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendlinge7124492011-06-14 03:17:20 +0000960 << "::printAliasInstr(const MCInst"
Akira Hatanakab46d0232015-03-27 20:36:02 +0000961 << " *MI, " << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
962 << "raw_ostream &OS) {\n";
Bill Wendling7e570b52011-03-21 08:59:17 +0000963
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000964 std::string Cases;
965 raw_string_ostream CasesO(Cases);
966
David Blaikie4ab57cd2015-08-06 19:23:33 +0000967 for (auto &Entry : IAPrinterMap) {
968 std::vector<IAPrinter> &IAPs = Entry.second;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000969 std::vector<IAPrinter*> UniqueIAPs;
970
David Blaikie4ab57cd2015-08-06 19:23:33 +0000971 for (auto &LHS : IAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000972 bool IsDup = false;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000973 for (const auto &RHS : IAPs) {
974 if (&LHS != &RHS && LHS == RHS) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000975 IsDup = true;
976 break;
977 }
978 }
979
David Blaikie4ab57cd2015-08-06 19:23:33 +0000980 if (!IsDup)
981 UniqueIAPs.push_back(&LHS);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000982 }
983
984 if (UniqueIAPs.empty()) continue;
985
David Blaikie4ab57cd2015-08-06 19:23:33 +0000986 CasesO.indent(2) << "case " << Entry.first << ":\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000987
Craig Topper190ecd52016-01-08 07:06:32 +0000988 for (IAPrinter *IAP : UniqueIAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000989 CasesO.indent(4);
Evan Cheng4d806e22011-07-06 02:02:33 +0000990 IAP->print(CasesO);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000991 CasesO << '\n';
992 }
993
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000994 CasesO.indent(4) << "return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000995 }
996
Bill Wendlinge7124492011-06-14 03:17:20 +0000997 if (CasesO.str().empty()) {
Bill Wendlingf5199de2011-05-23 00:18:33 +0000998 O << HeaderO.str();
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000999 O << " return false;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001000 O << "}\n\n";
1001 O << "#endif // PRINT_ALIAS_INSTR\n";
1002 return;
1003 }
1004
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001005 if (!MCOpPredicates.empty())
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001006 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +00001007 << "ValidateMCOperand(const MCOperand &MCOp,\n"
1008 << " const MCSubtargetInfo &STI,\n"
1009 << " unsigned PredicateIndex);\n";
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001010
Bill Wendlingf5199de2011-05-23 00:18:33 +00001011 O << HeaderO.str();
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001012 O.indent(2) << "const char *AsmString;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +00001013 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher2e3fbaa2011-04-18 21:28:11 +00001014 O.indent(2) << "default: return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +00001015 O << CasesO.str();
1016 O.indent(2) << "}\n\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001017
1018 // Code that prints the alias, replacing the operands with the ones from the
1019 // MCInst.
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001020 O << " unsigned I = 0;\n";
Sjoerd Meijer3c2f7852016-06-03 13:17:37 +00001021 O << " while (AsmString[I] != ' ' && AsmString[I] != '\\t' &&\n";
1022 O << " AsmString[I] != '$' && AsmString[I] != '\\0')\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001023 O << " ++I;\n";
1024 O << " OS << '\\t' << StringRef(AsmString, I);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001025
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001026 O << " if (AsmString[I] != '\\0') {\n";
Sjoerd Meijer3c2f7852016-06-03 13:17:37 +00001027 O << " if (AsmString[I] == ' ' || AsmString[I] == '\\t')";
1028 O << " OS << '\\t';\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001029 O << " do {\n";
1030 O << " if (AsmString[I] == '$') {\n";
1031 O << " ++I;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001032 O << " if (AsmString[I] == (char)0xff) {\n";
1033 O << " ++I;\n";
1034 O << " int OpIdx = AsmString[I++] - 1;\n";
1035 O << " int PrintMethodIdx = AsmString[I++] - 1;\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001036 O << " printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, ";
1037 O << (PassSubtarget ? "STI, " : "");
1038 O << "OS);\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001039 O << " } else\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001040 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, ";
1041 O << (PassSubtarget ? "STI, " : "");
1042 O << "OS);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001043 O << " } else {\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001044 O << " OS << AsmString[I++];\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001045 O << " }\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001046 O << " } while (AsmString[I] != '\\0');\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001047 O << " }\n\n";
Jim Grosbachf4e67082012-04-18 18:56:33 +00001048
Eric Christopher2e3fbaa2011-04-18 21:28:11 +00001049 O << " return true;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001050 O << "}\n\n";
1051
Tim Northoveree20caa2014-05-12 18:04:06 +00001052 //////////////////////////////
1053 // Write out the printCustomAliasOperand function
1054 //////////////////////////////
1055
1056 O << "void " << Target.getName() << ClassName << "::"
1057 << "printCustomAliasOperand(\n"
1058 << " const MCInst *MI, unsigned OpIdx,\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001059 << " unsigned PrintMethodIdx,\n"
1060 << (PassSubtarget ? " const MCSubtargetInfo &STI,\n" : "")
1061 << " raw_ostream &OS) {\n";
Aaron Ballmane58a5702014-05-13 12:52:35 +00001062 if (PrintMethods.empty())
1063 O << " llvm_unreachable(\"Unknown PrintMethod kind\");\n";
1064 else {
1065 O << " switch (PrintMethodIdx) {\n"
1066 << " default:\n"
1067 << " llvm_unreachable(\"Unknown PrintMethod kind\");\n"
Tim Northoveree20caa2014-05-12 18:04:06 +00001068 << " break;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001069
Aaron Ballmane58a5702014-05-13 12:52:35 +00001070 for (unsigned i = 0; i < PrintMethods.size(); ++i) {
1071 O << " case " << i << ":\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001072 << " " << PrintMethods[i] << "(MI, OpIdx, "
1073 << (PassSubtarget ? "STI, " : "") << "OS);\n"
Aaron Ballmane58a5702014-05-13 12:52:35 +00001074 << " break;\n";
1075 }
1076 O << " }\n";
1077 }
1078 O << "}\n\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001079
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001080 if (!MCOpPredicates.empty()) {
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001081 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +00001082 << "ValidateMCOperand(const MCOperand &MCOp,\n"
1083 << " const MCSubtargetInfo &STI,\n"
1084 << " unsigned PredicateIndex) {\n"
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001085 << " switch (PredicateIndex) {\n"
1086 << " default:\n"
1087 << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
1088 << " break;\n";
1089
1090 for (unsigned i = 0; i < MCOpPredicates.size(); ++i) {
1091 Init *MCOpPred = MCOpPredicates[i]->getValueInit("MCOperandPredicate");
Tim Northover88403d72016-07-05 21:22:55 +00001092 if (CodeInit *SI = dyn_cast<CodeInit>(MCOpPred)) {
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001093 O << " case " << i + 1 << ": {\n"
1094 << SI->getValue() << "\n"
1095 << " }\n";
1096 } else
1097 llvm_unreachable("Unexpected MCOperandPredicate field!");
1098 }
1099 O << " }\n"
1100 << "}\n\n";
1101 }
1102
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001103 O << "#endif // PRINT_ALIAS_INSTR\n";
1104}
Chris Lattner06c5eed2009-09-13 20:08:00 +00001105
Ahmed Bougachabd214002013-10-28 18:07:17 +00001106AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1107 Record *AsmWriter = Target.getAsmWriter();
Craig Topper0bd58742016-01-13 07:20:05 +00001108 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Ahmed Bougachabd214002013-10-28 18:07:17 +00001109
1110 // Get the instruction numbering.
Craig Topperf9265322016-01-17 20:38:14 +00001111 NumberedInstructions = Target.getInstructionsByEnumValue();
Ahmed Bougachabd214002013-10-28 18:07:17 +00001112
Craig Topperf9265322016-01-17 20:38:14 +00001113 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
1114 const CodeGenInstruction *I = NumberedInstructions[i];
Craig Topper9e9ae602016-01-17 08:05:33 +00001115 if (!I->AsmString.empty() && I->TheDef->getName() != "PHI")
1116 Instructions.emplace_back(*I, i, Variant);
1117 }
Ahmed Bougachabd214002013-10-28 18:07:17 +00001118}
1119
Chris Lattner06c5eed2009-09-13 20:08:00 +00001120void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +00001121 EmitPrintInstruction(O);
1122 EmitGetRegisterName(O);
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001123 EmitPrintAliasInstruction(O);
Chris Lattner06c5eed2009-09-13 20:08:00 +00001124}
1125
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001126namespace llvm {
1127
1128void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1129 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1130 AsmWriterEmitter(RK).run(OS);
1131}
1132
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +00001133} // end namespace llvm