blob: dd617a65d1aa28b150ef06891517a8f40701ba5c [file] [log] [blame]
Chris Lattner1c4ae852004-08-01 05:59:33 +00001//===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Chris Lattner1c4ae852004-08-01 05:59:33 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
Chris Lattner1c4ae852004-08-01 05:59:33 +00008//===----------------------------------------------------------------------===//
9//
10// This tablegen backend is emits an assembly printer for the current target.
11// Note that this is currently fairly skeletal, but will grow over time.
12//
13//===----------------------------------------------------------------------===//
14
Sean Callananb7e8f4a2010-02-09 21:50:41 +000015#include "AsmWriterInst.h"
Chris Lattner1c4ae852004-08-01 05:59:33 +000016#include "CodeGenTarget.h"
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +000017#include "SequenceToOffsetTable.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000018#include "llvm/ADT/SmallString.h"
Craig Topperb6350132012-07-27 06:44:02 +000019#include "llvm/ADT/StringExtras.h"
Owen Andersona84be6c2011-06-27 21:06:21 +000020#include "llvm/ADT/Twine.h"
Chris Lattner692374c2006-07-18 17:18:03 +000021#include "llvm/Support/Debug.h"
Benjamin Kramer17c17bc2013-09-11 15:42:16 +000022#include "llvm/Support/Format.h"
Chris Lattner692374c2006-07-18 17:18:03 +000023#include "llvm/Support/MathExtras.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000024#include "llvm/TableGen/Error.h"
25#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000026#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenda636b32005-01-22 18:50:10 +000027#include <algorithm>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000028#include <cassert>
29#include <map>
30#include <vector>
Chris Lattner1c4ae852004-08-01 05:59:33 +000031using namespace llvm;
32
Chandler Carruthe96dd892014-04-21 22:55:11 +000033#define DEBUG_TYPE "asm-writer-emitter"
34
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000035namespace {
36class AsmWriterEmitter {
37 RecordKeeper &Records;
Ahmed Bougachabd214002013-10-28 18:07:17 +000038 CodeGenTarget Target;
Craig Topperf9265322016-01-17 20:38:14 +000039 ArrayRef<const CodeGenInstruction *> NumberedInstructions;
Ahmed Bougachabd214002013-10-28 18:07:17 +000040 std::vector<AsmWriterInst> Instructions;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000041public:
Ahmed Bougachabd214002013-10-28 18:07:17 +000042 AsmWriterEmitter(RecordKeeper &R);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000043
44 void run(raw_ostream &o);
45
46private:
47 void EmitPrintInstruction(raw_ostream &o);
48 void EmitGetRegisterName(raw_ostream &o);
49 void EmitPrintAliasInstruction(raw_ostream &O);
50
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000051 void FindUniqueOperandCommands(std::vector<std::string> &UOC,
52 std::vector<unsigned> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +000053 std::vector<unsigned> &InstOpsUsed,
54 bool PassSubtarget) const;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000055};
56} // end anonymous namespace
57
Chris Lattner59a7f5c2005-01-22 20:31:17 +000058static void PrintCases(std::vector<std::pair<std::string,
Craig Topperc24a4012016-01-14 06:15:07 +000059 AsmWriterOperand> > &OpsToPrint, raw_ostream &O,
60 bool PassSubtarget) {
Craig Topper0b271ad2016-01-13 07:20:13 +000061 O << " case " << OpsToPrint.back().first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000062 AsmWriterOperand TheOp = OpsToPrint.back().second;
63 OpsToPrint.pop_back();
64
65 // Check to see if any other operands are identical in this list, and if so,
66 // emit a case label for them.
67 for (unsigned i = OpsToPrint.size(); i != 0; --i)
68 if (OpsToPrint[i-1].second == TheOp) {
Craig Topper0b271ad2016-01-13 07:20:13 +000069 O << "\n case " << OpsToPrint[i-1].first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000070 OpsToPrint.erase(OpsToPrint.begin()+i-1);
71 }
72
73 // Finally, emit the code.
Craig Topperc24a4012016-01-14 06:15:07 +000074 O << "\n " << TheOp.getCode(PassSubtarget);
Craig Topper0b271ad2016-01-13 07:20:13 +000075 O << "\n break;\n";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000076}
77
Chris Lattner9ceb7c82005-01-22 18:38:13 +000078
79/// EmitInstructions - Emit the last instruction in the vector and any other
80/// instructions that are suitably similar to it.
81static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Craig Topperc24a4012016-01-14 06:15:07 +000082 raw_ostream &O, bool PassSubtarget) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000083 AsmWriterInst FirstInst = Insts.back();
84 Insts.pop_back();
85
86 std::vector<AsmWriterInst> SimilarInsts;
87 unsigned DifferingOperand = ~0;
88 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattner92275bb2005-01-22 19:22:23 +000089 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
90 if (DiffOp != ~1U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000091 if (DifferingOperand == ~0U) // First match!
92 DifferingOperand = DiffOp;
93
94 // If this differs in the same operand as the rest of the instructions in
95 // this class, move it to the SimilarInsts list.
Chris Lattner92275bb2005-01-22 19:22:23 +000096 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000097 SimilarInsts.push_back(Insts[i-1]);
98 Insts.erase(Insts.begin()+i-1);
99 }
100 }
101 }
102
Chris Lattner017b93d2006-05-01 17:01:17 +0000103 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000104 << FirstInst.CGI->TheDef->getName() << ":\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000105 for (const AsmWriterInst &AWI : SimilarInsts)
106 O << " case " << AWI.CGI->Namespace << "::"
107 << AWI.CGI->TheDef->getName() << ":\n";
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000108 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
109 if (i != DifferingOperand) {
110 // If the operand is the same for all instructions, just print it.
Craig Topperc24a4012016-01-14 06:15:07 +0000111 O << " " << FirstInst.Operands[i].getCode(PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000112 } else {
113 // If this is the operand that varies between all of the instructions,
114 // emit a switch for just this operand now.
115 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000116 O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000117 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattner017b93d2006-05-01 17:01:17 +0000118 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000119 FirstInst.CGI->TheDef->getName(),
120 FirstInst.Operands[i]));
Misha Brukman650ba8e2005-04-22 00:00:37 +0000121
Craig Topper190ecd52016-01-08 07:06:32 +0000122 for (const AsmWriterInst &AWI : SimilarInsts) {
Chris Lattner017b93d2006-05-01 17:01:17 +0000123 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000124 AWI.CGI->TheDef->getName(),
125 AWI.Operands[i]));
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000126 }
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000127 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
128 while (!OpsToPrint.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000129 PrintCases(OpsToPrint, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000130 O << " }";
131 }
132 O << "\n";
133 }
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000134 O << " break;\n";
135}
Chris Lattner0c23ba52005-01-22 17:32:42 +0000136
Chris Lattner692374c2006-07-18 17:18:03 +0000137void AsmWriterEmitter::
Jim Grosbacha5497342010-09-29 22:32:50 +0000138FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattneredee5252006-07-18 18:28:27 +0000139 std::vector<unsigned> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000140 std::vector<unsigned> &InstOpsUsed,
141 bool PassSubtarget) const {
Craig Topper9e9ae602016-01-17 08:05:33 +0000142 InstIdxs.assign(Instructions.size(), ~0U);
Jim Grosbacha5497342010-09-29 22:32:50 +0000143
Chris Lattner692374c2006-07-18 17:18:03 +0000144 // This vector parallels UniqueOperandCommands, keeping track of which
145 // instructions each case are used for. It is a comma separated string of
146 // enums.
147 std::vector<std::string> InstrsForCase;
148 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattneredee5252006-07-18 18:28:27 +0000149 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbacha5497342010-09-29 22:32:50 +0000150
Craig Topper9e9ae602016-01-17 08:05:33 +0000151 for (size_t i = 0, e = Instructions.size(); i != e; ++i) {
152 const AsmWriterInst &Inst = Instructions[i];
153 if (Inst.Operands.empty())
Chris Lattner692374c2006-07-18 17:18:03 +0000154 continue; // Instruction already done.
Chris Lattner9d250692006-07-18 17:50:22 +0000155
Craig Topper9e9ae602016-01-17 08:05:33 +0000156 std::string Command = " "+Inst.Operands[0].getCode(PassSubtarget)+"\n";
Chris Lattner9d250692006-07-18 17:50:22 +0000157
Chris Lattner692374c2006-07-18 17:18:03 +0000158 // Check to see if we already have 'Command' in UniqueOperandCommands.
159 // If not, add it.
Craig Toppera99859d2016-01-17 08:05:30 +0000160 auto I = std::find(UniqueOperandCommands.begin(),
161 UniqueOperandCommands.end(), Command);
162 if (I != UniqueOperandCommands.end()) {
163 size_t idx = I - UniqueOperandCommands.begin();
164 InstIdxs[i] = idx;
165 InstrsForCase[idx] += ", ";
Craig Topper9e9ae602016-01-17 08:05:33 +0000166 InstrsForCase[idx] += Inst.CGI->TheDef->getName();
Craig Toppera99859d2016-01-17 08:05:30 +0000167 } else {
Chris Lattner692374c2006-07-18 17:18:03 +0000168 InstIdxs[i] = UniqueOperandCommands.size();
Craig Topper1993e3b2016-01-08 07:06:29 +0000169 UniqueOperandCommands.push_back(std::move(Command));
Craig Topper9e9ae602016-01-17 08:05:33 +0000170 InstrsForCase.push_back(Inst.CGI->TheDef->getName());
Chris Lattneredee5252006-07-18 18:28:27 +0000171
172 // This command matches one operand so far.
173 InstOpsUsed.push_back(1);
174 }
175 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000176
Chris Lattneredee5252006-07-18 18:28:27 +0000177 // For each entry of UniqueOperandCommands, there is a set of instructions
178 // that uses it. If the next command of all instructions in the set are
179 // identical, fold it into the command.
180 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
181 CommandIdx != e; ++CommandIdx) {
Jim Grosbacha5497342010-09-29 22:32:50 +0000182
Chris Lattneredee5252006-07-18 18:28:27 +0000183 for (unsigned Op = 1; ; ++Op) {
184 // Scan for the first instruction in the set.
Craig Topper9e9ae602016-01-17 08:05:33 +0000185 auto NIT = std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
Chris Lattneredee5252006-07-18 18:28:27 +0000186 if (NIT == InstIdxs.end()) break; // No commonality.
187
188 // If this instruction has no more operands, we isn't anything to merge
189 // into this command.
Craig Topper9e9ae602016-01-17 08:05:33 +0000190 const AsmWriterInst &FirstInst = Instructions[NIT-InstIdxs.begin()];
191 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.
196 bool AllSame = true;
David Greene5b4bc262009-07-29 20:10:24 +0000197
Chris Lattneredee5252006-07-18 18:28:27 +0000198 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
199 NIT != InstIdxs.end();
200 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
201 // Okay, found another instruction in this command set. If the operand
202 // matches, we're ok, otherwise bail out.
Craig Topper9e9ae602016-01-17 08:05:33 +0000203 const AsmWriterInst &OtherInst = Instructions[NIT-InstIdxs.begin()];
David Greene5b4bc262009-07-29 20:10:24 +0000204
Craig Topper9e9ae602016-01-17 08:05:33 +0000205 if (OtherInst.Operands.size() == Op ||
206 OtherInst.Operands[Op] != FirstInst.Operands[Op]) {
Chris Lattneredee5252006-07-18 18:28:27 +0000207 AllSame = false;
208 break;
209 }
210 }
211 if (!AllSame) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000212
Chris Lattneredee5252006-07-18 18:28:27 +0000213 // Okay, everything in this command set has the same next operand. Add it
214 // to UniqueOperandCommands and remember that it was consumed.
Craig Topperc24a4012016-01-14 06:15:07 +0000215 std::string Command = " " +
Craig Topper9e9ae602016-01-17 08:05:33 +0000216 FirstInst.Operands[Op].getCode(PassSubtarget) + "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000217
Chris Lattneredee5252006-07-18 18:28:27 +0000218 UniqueOperandCommands[CommandIdx] += Command;
219 InstOpsUsed[CommandIdx]++;
Chris Lattner692374c2006-07-18 17:18:03 +0000220 }
221 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000222
Chris Lattner692374c2006-07-18 17:18:03 +0000223 // Prepend some of the instructions each case is used for onto the case val.
224 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
225 std::string Instrs = InstrsForCase[i];
226 if (Instrs.size() > 70) {
227 Instrs.erase(Instrs.begin()+70, Instrs.end());
228 Instrs += "...";
229 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000230
Chris Lattner692374c2006-07-18 17:18:03 +0000231 if (!Instrs.empty())
Jim Grosbacha5497342010-09-29 22:32:50 +0000232 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattner692374c2006-07-18 17:18:03 +0000233 UniqueOperandCommands[i];
234 }
235}
236
237
Daniel Dunbar04f049f2009-10-17 20:43:42 +0000238static void UnescapeString(std::string &Str) {
239 for (unsigned i = 0; i != Str.size(); ++i) {
240 if (Str[i] == '\\' && i != Str.size()-1) {
241 switch (Str[i+1]) {
242 default: continue; // Don't execute the code after the switch.
243 case 'a': Str[i] = '\a'; break;
244 case 'b': Str[i] = '\b'; break;
245 case 'e': Str[i] = 27; break;
246 case 'f': Str[i] = '\f'; break;
247 case 'n': Str[i] = '\n'; break;
248 case 'r': Str[i] = '\r'; break;
249 case 't': Str[i] = '\t'; break;
250 case 'v': Str[i] = '\v'; break;
251 case '"': Str[i] = '\"'; break;
252 case '\'': Str[i] = '\''; break;
253 case '\\': Str[i] = '\\'; break;
254 }
255 // Nuke the second character.
256 Str.erase(Str.begin()+i+1);
257 }
258 }
259}
260
Chris Lattner06c5eed2009-09-13 20:08:00 +0000261/// EmitPrintInstruction - Generate the code for the "printInstruction" method
Ahmed Bougachabd214002013-10-28 18:07:17 +0000262/// implementation. Destroys all instances of AsmWriterInst information, by
263/// clearing the Instructions vector.
Chris Lattner06c5eed2009-09-13 20:08:00 +0000264void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner6ffa5012004-08-14 22:50:53 +0000265 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner72770f52004-10-03 20:19:02 +0000266 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Craig Topperc24a4012016-01-14 06:15:07 +0000267 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
Jim Grosbacha5497342010-09-29 22:32:50 +0000268
Chris Lattner1c4ae852004-08-01 05:59:33 +0000269 O <<
270 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner06c5eed2009-09-13 20:08:00 +0000271 "/// from the instruction set description.\n"
Chris Lattnerb94284b2009-08-08 01:32:19 +0000272 "void " << Target.getName() << ClassName
Akira Hatanakab46d0232015-03-27 20:36:02 +0000273 << "::printInstruction(const MCInst *MI, "
274 << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
275 << "raw_ostream &O) {\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000276
Chris Lattnere32982c2006-07-14 22:59:11 +0000277 // Build an aggregate string, and build a table of offsets into it.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000278 SequenceToOffsetTable<std::string> StringTable;
Jim Grosbacha5497342010-09-29 22:32:50 +0000279
Chris Lattner5d751b42006-09-27 16:44:09 +0000280 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000281 /// chunk of the output as well as indices used for operand printing.
Craig Topperf9265322016-01-17 20:38:14 +0000282 std::vector<uint64_t> OpcodeInfo(NumberedInstructions.size());
Craig Topperd4f87a32016-01-13 07:20:12 +0000283 const unsigned OpcodeInfoBits = 64;
Jim Grosbacha5497342010-09-29 22:32:50 +0000284
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000285 // Add all strings to the string table upfront so it can generate an optimized
286 // representation.
Craig Topper9e9ae602016-01-17 08:05:33 +0000287 for (AsmWriterInst &AWI : Instructions) {
288 if (AWI.Operands[0].OperandType ==
Jim Grosbachf4e67082012-04-18 18:56:33 +0000289 AsmWriterOperand::isLiteralTextOperand &&
Craig Topper9e9ae602016-01-17 08:05:33 +0000290 !AWI.Operands[0].Str.empty()) {
291 std::string Str = AWI.Operands[0].Str;
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000292 UnescapeString(Str);
293 StringTable.add(Str);
294 }
295 }
296
297 StringTable.layout();
298
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000299 unsigned MaxStringIdx = 0;
Craig Topper9e9ae602016-01-17 08:05:33 +0000300 for (AsmWriterInst &AWI : Instructions) {
Chris Lattnere32982c2006-07-14 22:59:11 +0000301 unsigned Idx;
Craig Topper9e9ae602016-01-17 08:05:33 +0000302 if (AWI.Operands[0].OperandType != AsmWriterOperand::isLiteralTextOperand ||
303 AWI.Operands[0].Str.empty()) {
Chris Lattner36504652006-07-19 01:39:06 +0000304 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000305 Idx = StringTable.get("");
Chris Lattnere32982c2006-07-14 22:59:11 +0000306 } else {
Craig Topper9e9ae602016-01-17 08:05:33 +0000307 std::string Str = AWI.Operands[0].Str;
Chris Lattnerb47ed612009-09-14 01:16:36 +0000308 UnescapeString(Str);
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000309 Idx = StringTable.get(Str);
Chris Lattnerb47ed612009-09-14 01:16:36 +0000310 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbacha5497342010-09-29 22:32:50 +0000311
Chris Lattnere32982c2006-07-14 22:59:11 +0000312 // Nuke the string from the operand list. It is now handled!
Craig Topper9e9ae602016-01-17 08:05:33 +0000313 AWI.Operands.erase(AWI.Operands.begin());
Chris Lattner92275bb2005-01-22 19:22:23 +0000314 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000315
Chris Lattnerb47ed612009-09-14 01:16:36 +0000316 // Bias offset by one since we want 0 as a sentinel.
Craig Topper9e9ae602016-01-17 08:05:33 +0000317 OpcodeInfo[AWI.CGIIndex] = Idx+1;
Chris Lattner92275bb2005-01-22 19:22:23 +0000318 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000319
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000320 // Figure out how many bits we used for the string index.
Chris Lattnerb47ed612009-09-14 01:16:36 +0000321 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbacha5497342010-09-29 22:32:50 +0000322
Chris Lattner692374c2006-07-18 17:18:03 +0000323 // To reduce code size, we compactify common instructions into a few bits
324 // in the opcode-indexed table.
Craig Topperd4f87a32016-01-13 07:20:12 +0000325 unsigned BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000326
Craig Topper1f387362014-11-25 20:11:25 +0000327 std::vector<std::vector<std::string>> TableDrivenOperandPrinters;
Jim Grosbacha5497342010-09-29 22:32:50 +0000328
Chris Lattnercb0c8482006-07-18 17:56:07 +0000329 while (1) {
Chris Lattner692374c2006-07-18 17:18:03 +0000330 std::vector<std::string> UniqueOperandCommands;
Chris Lattner692374c2006-07-18 17:18:03 +0000331 std::vector<unsigned> InstIdxs;
Chris Lattneredee5252006-07-18 18:28:27 +0000332 std::vector<unsigned> NumInstOpsHandled;
333 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000334 NumInstOpsHandled, PassSubtarget);
Jim Grosbacha5497342010-09-29 22:32:50 +0000335
Chris Lattner692374c2006-07-18 17:18:03 +0000336 // If we ran out of operands to print, we're done.
337 if (UniqueOperandCommands.empty()) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000338
Chris Lattner692374c2006-07-18 17:18:03 +0000339 // Compute the number of bits we need to represent these cases, this is
340 // ceil(log2(numentries)).
341 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbacha5497342010-09-29 22:32:50 +0000342
Chris Lattner692374c2006-07-18 17:18:03 +0000343 // If we don't have enough bits for this operand, don't include it.
344 if (NumBits > BitsLeft) {
Chris Lattner34822f62009-08-23 04:44:11 +0000345 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
346 << " more bits\n");
Chris Lattner692374c2006-07-18 17:18:03 +0000347 break;
348 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000349
Chris Lattner692374c2006-07-18 17:18:03 +0000350 // Otherwise, we can include this in the initial lookup table. Add it in.
Craig Topper9e9ae602016-01-17 08:05:33 +0000351 for (size_t i = 0, e = Instructions.size(); i != e; ++i)
352 if (InstIdxs[i] != ~0U)
353 OpcodeInfo[Instructions[i].CGIIndex] |=
354 (uint64_t)InstIdxs[i] << (OpcodeInfoBits-BitsLeft);
Craig Topper06cec4c2012-09-14 08:33:11 +0000355 BitsLeft -= NumBits;
Jim Grosbacha5497342010-09-29 22:32:50 +0000356
Chris Lattnercb0c8482006-07-18 17:56:07 +0000357 // Remove the info about this operand.
Craig Topper9e9ae602016-01-17 08:05:33 +0000358 for (size_t i = 0, e = Instructions.size(); i != e; ++i) {
359 AsmWriterInst &Inst = Instructions[i];
360 if (!Inst.Operands.empty()) {
361 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
362 assert(NumOps <= Inst.Operands.size() &&
363 "Can't remove this many ops!");
364 Inst.Operands.erase(Inst.Operands.begin(),
365 Inst.Operands.begin()+NumOps);
366 }
Chris Lattnercb0c8482006-07-18 17:56:07 +0000367 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000368
Chris Lattnercb0c8482006-07-18 17:56:07 +0000369 // Remember the handlers for this set of operands.
Craig Topper1f387362014-11-25 20:11:25 +0000370 TableDrivenOperandPrinters.push_back(std::move(UniqueOperandCommands));
Chris Lattner692374c2006-07-18 17:18:03 +0000371 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000372
Craig Topper14d91732016-01-11 05:13:41 +0000373 // Emit the string table itself.
Reid Kleckner132c40f2014-07-17 19:43:40 +0000374 O << " static const char AsmStrs[] = {\n";
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000375 StringTable.emit(O, printChar);
376 O << " };\n\n";
Chris Lattnere32982c2006-07-14 22:59:11 +0000377
Craig Topper14d91732016-01-11 05:13:41 +0000378 // Emit the lookup tables in pieces to minimize wasted bytes.
Craig Topperd4f87a32016-01-13 07:20:12 +0000379 unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
Craig Topper14d91732016-01-11 05:13:41 +0000380 unsigned Table = 0, Shift = 0;
381 SmallString<128> BitsString;
382 raw_svector_ostream BitsOS(BitsString);
383 // If the total bits is more than 32-bits we need to use a 64-bit type.
Craig Topperd4f87a32016-01-13 07:20:12 +0000384 BitsOS << " uint" << ((BitsLeft < (OpcodeInfoBits - 32)) ? 64 : 32)
385 << "_t Bits = 0;\n";
Craig Topper14d91732016-01-11 05:13:41 +0000386 while (BytesNeeded != 0) {
387 // Figure out how big this table section needs to be, but no bigger than 4.
388 unsigned TableSize = std::min(1 << Log2_32(BytesNeeded), 4);
389 BytesNeeded -= TableSize;
390 TableSize *= 8; // Convert to bits;
391 uint64_t Mask = (1ULL << TableSize) - 1;
392 O << " static const uint" << TableSize << "_t OpInfo" << Table
393 << "[] = {\n";
Craig Topperf9265322016-01-17 20:38:14 +0000394 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Craig Topper14d91732016-01-11 05:13:41 +0000395 O << " " << ((OpcodeInfo[i] >> Shift) & Mask) << "U,\t// "
Craig Topperf9265322016-01-17 20:38:14 +0000396 << NumberedInstructions[i]->TheDef->getName() << "\n";
Craig Topper14d91732016-01-11 05:13:41 +0000397 }
398 O << " };\n\n";
399 // Emit string to combine the individual table lookups.
400 BitsOS << " Bits |= ";
401 // If the total bits is more than 32-bits we need to use a 64-bit type.
Craig Topperd4f87a32016-01-13 07:20:12 +0000402 if (BitsLeft < (OpcodeInfoBits - 32))
Craig Topper14d91732016-01-11 05:13:41 +0000403 BitsOS << "(uint64_t)";
404 BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
405 // Prepare the shift for the next iteration and increment the table count.
406 Shift += TableSize;
407 ++Table;
408 }
409
410 // Emit the initial tab character.
Evan Cheng32e53472008-02-02 08:39:46 +0000411 O << " O << \"\\t\";\n\n";
412
Craig Topper06cec4c2012-09-14 08:33:11 +0000413 O << " // Emit the opcode for the instruction.\n";
Craig Topper14d91732016-01-11 05:13:41 +0000414 O << BitsString;
415
416 // Emit the starting string.
Manman Ren68cf9fc2012-09-13 17:43:46 +0000417 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattnerb47ed612009-09-14 01:16:36 +0000418 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenefdd25192009-08-05 21:00:52 +0000419
Chris Lattner692374c2006-07-18 17:18:03 +0000420 // Output the table driven operand information.
Craig Topperd4f87a32016-01-13 07:20:12 +0000421 BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000422 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
423 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
424
425 // Compute the number of bits we need to represent these cases, this is
426 // ceil(log2(numentries)).
427 unsigned NumBits = Log2_32_Ceil(Commands.size());
428 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbacha5497342010-09-29 22:32:50 +0000429
Chris Lattner692374c2006-07-18 17:18:03 +0000430 // Emit code to extract this field from Bits.
Chris Lattner692374c2006-07-18 17:18:03 +0000431 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000432 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000433
Chris Lattneredee5252006-07-18 18:28:27 +0000434 if (Commands.size() == 2) {
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000435 // Emit two possibilitys with if/else.
Craig Topper06cec4c2012-09-14 08:33:11 +0000436 O << " if ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000437 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000438 << ((1 << NumBits)-1) << ") {\n"
439 << Commands[1]
440 << " } else {\n"
441 << Commands[0]
442 << " }\n\n";
Eric Christophera573d192010-09-18 18:50:27 +0000443 } else if (Commands.size() == 1) {
444 // Emit a single possibility.
445 O << Commands[0] << "\n\n";
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000446 } else {
Craig Topper06cec4c2012-09-14 08:33:11 +0000447 O << " switch ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000448 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000449 << ((1 << NumBits)-1) << ") {\n"
Craig Toppera4ff6ae2014-11-24 14:09:52 +0000450 << " default: llvm_unreachable(\"Invalid command number.\");\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000451
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000452 // Print out all the cases.
Craig Topper190ecd52016-01-08 07:06:32 +0000453 for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
454 O << " case " << j << ":\n";
455 O << Commands[j];
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000456 O << " break;\n";
457 }
458 O << " }\n\n";
Chris Lattner692374c2006-07-18 17:18:03 +0000459 }
Craig Topper06cec4c2012-09-14 08:33:11 +0000460 BitsLeft -= NumBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000461 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000462
Chris Lattnercb0c8482006-07-18 17:56:07 +0000463 // Okay, delete instructions with no operand info left.
Craig Topper4f1f1152016-01-13 07:20:10 +0000464 auto I = std::remove_if(Instructions.begin(), Instructions.end(),
465 [](AsmWriterInst &Inst) {
466 return Inst.Operands.empty();
467 });
468 Instructions.erase(I, Instructions.end());
Chris Lattner692374c2006-07-18 17:18:03 +0000469
Jim Grosbacha5497342010-09-29 22:32:50 +0000470
Chris Lattner692374c2006-07-18 17:18:03 +0000471 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000472 // elements in the vector.
473 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbacha5497342010-09-29 22:32:50 +0000474
475
Craig Topperdf390602016-01-13 07:20:07 +0000476 // Now that we've emitted all of the operand info that fit into 64 bits, emit
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000477 // information for those instructions that are left. This is a less dense
Craig Topperdf390602016-01-13 07:20:07 +0000478 // encoding, but we expect the main 64-bit table to handle the majority of
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000479 // instructions.
Chris Lattner66e288b2006-07-18 17:38:46 +0000480 if (!Instructions.empty()) {
481 // Find the opcode # of inline asm.
482 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000483 O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
Chris Lattner66e288b2006-07-18 17:38:46 +0000484 while (!Instructions.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000485 EmitInstructions(Instructions, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000486
Chris Lattner66e288b2006-07-18 17:38:46 +0000487 O << " }\n";
488 }
David Greene5b4bc262009-07-29 20:10:24 +0000489
Chris Lattner6e172082006-07-18 19:06:01 +0000490 O << "}\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000491}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000492
Craig Topperba6d83e2014-11-24 02:08:35 +0000493static const char *getMinimalTypeForRange(uint64_t Range) {
494 assert(Range < 0xFFFFFFFFULL && "Enum too large");
495 if (Range > 0xFFFF)
496 return "uint32_t";
497 if (Range > 0xFF)
498 return "uint16_t";
499 return "uint8_t";
500}
501
Owen Andersona84be6c2011-06-27 21:06:21 +0000502static void
503emitRegisterNameString(raw_ostream &O, StringRef AltName,
David Blaikie9b613db2014-11-29 18:13:39 +0000504 const std::deque<CodeGenRegister> &Registers) {
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000505 SequenceToOffsetTable<std::string> StringTable;
506 SmallVector<std::string, 4> AsmNames(Registers.size());
David Blaikie9b613db2014-11-29 18:13:39 +0000507 unsigned i = 0;
508 for (const auto &Reg : Registers) {
509 std::string &AsmName = AsmNames[i++];
Owen Andersona84be6c2011-06-27 21:06:21 +0000510
Owen Andersona84be6c2011-06-27 21:06:21 +0000511 // "NoRegAltName" is special. We don't need to do a lookup for that,
512 // as it's just a reference to the default register name.
513 if (AltName == "" || AltName == "NoRegAltName") {
514 AsmName = Reg.TheDef->getValueAsString("AsmName");
515 if (AsmName.empty())
516 AsmName = Reg.getName();
517 } else {
518 // Make sure the register has an alternate name for this index.
519 std::vector<Record*> AltNameList =
520 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
521 unsigned Idx = 0, e;
522 for (e = AltNameList.size();
523 Idx < e && (AltNameList[Idx]->getName() != AltName);
524 ++Idx)
525 ;
526 // If the register has an alternate name for this index, use it.
527 // Otherwise, leave it empty as an error flag.
528 if (Idx < e) {
529 std::vector<std::string> AltNames =
530 Reg.TheDef->getValueAsListOfStrings("AltNames");
531 if (AltNames.size() <= Idx)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000532 PrintFatalError(Reg.TheDef->getLoc(),
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000533 "Register definition missing alt name for '" +
534 AltName + "'.");
Owen Andersona84be6c2011-06-27 21:06:21 +0000535 AsmName = AltNames[Idx];
536 }
537 }
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000538 StringTable.add(AsmName);
539 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000540
Craig Topperf8f0a232012-09-15 01:22:42 +0000541 StringTable.layout();
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000542 O << " static const char AsmStrs" << AltName << "[] = {\n";
543 StringTable.emit(O, printChar);
544 O << " };\n\n";
545
Craig Topperba6d83e2014-11-24 02:08:35 +0000546 O << " static const " << getMinimalTypeForRange(StringTable.size()-1)
547 << " RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000548 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Topper7a2cea12012-04-02 00:47:39 +0000549 if ((i % 14) == 0)
550 O << "\n ";
551 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersona84be6c2011-06-27 21:06:21 +0000552 }
Craig Topper9c252eb2012-04-03 06:52:47 +0000553 O << "\n };\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000554 << "\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000555}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000556
557void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +0000558 Record *AsmWriter = Target.getAsmWriter();
559 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
David Blaikie9b613db2014-11-29 18:13:39 +0000560 const auto &Registers = Target.getRegBank().getRegisters();
Craig Topper83421ec2016-01-17 20:38:21 +0000561 const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
Owen Andersona84be6c2011-06-27 21:06:21 +0000562 bool hasAltNames = AltNameIndices.size() > 1;
Hal Finkelcd5f9842015-12-11 17:31:27 +0000563 std::string Namespace =
564 Registers.front().TheDef->getValueAsString("Namespace");
Jim Grosbacha5497342010-09-29 22:32:50 +0000565
Chris Lattner06c5eed2009-09-13 20:08:00 +0000566 O <<
567 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
568 "/// from the register set description. This returns the assembler name\n"
569 "/// for the specified register.\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000570 "const char *" << Target.getName() << ClassName << "::";
571 if (hasAltNames)
572 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
573 else
574 O << "getRegisterName(unsigned RegNo) {\n";
575 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
576 << " && \"Invalid register number!\");\n"
Chris Lattnera7e8ae42009-09-14 01:26:18 +0000577 << "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000578
Owen Andersona84be6c2011-06-27 21:06:21 +0000579 if (hasAltNames) {
Craig Topper190ecd52016-01-08 07:06:32 +0000580 for (const Record *R : AltNameIndices)
581 emitRegisterNameString(O, R->getName(), Registers);
Owen Andersona84be6c2011-06-27 21:06:21 +0000582 } else
583 emitRegisterNameString(O, "", Registers);
Jim Grosbacha5497342010-09-29 22:32:50 +0000584
Owen Andersona84be6c2011-06-27 21:06:21 +0000585 if (hasAltNames) {
Craig Topperba6d83e2014-11-24 02:08:35 +0000586 O << " switch(AltIdx) {\n"
Craig Topperc4965bc2012-02-05 07:21:30 +0000587 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000588 for (const Record *R : AltNameIndices) {
589 std::string AltName(R->getName());
Hal Finkelcd5f9842015-12-11 17:31:27 +0000590 std::string Prefix = !Namespace.empty() ? Namespace + "::" : "";
591 O << " case " << Prefix << AltName << ":\n"
Craig Topperba6d83e2014-11-24 02:08:35 +0000592 << " assert(*(AsmStrs" << AltName << "+RegAsmOffset"
593 << AltName << "[RegNo-1]) &&\n"
594 << " \"Invalid alt name index for register!\");\n"
595 << " return AsmStrs" << AltName << "+RegAsmOffset"
596 << AltName << "[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000597 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000598 O << " }\n";
599 } else {
600 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
601 << " \"Invalid alt name index for register!\");\n"
602 << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000603 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000604 O << "}\n";
Chris Lattner06c5eed2009-09-13 20:08:00 +0000605}
606
Bill Wendling7e5771d2011-03-21 08:31:53 +0000607namespace {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000608// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
609// they both have the same conditionals. In which case, we cannot print out the
610// alias for that pattern.
611class IAPrinter {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000612 std::vector<std::string> Conds;
Tim Northoveree20caa2014-05-12 18:04:06 +0000613 std::map<StringRef, std::pair<int, int>> OpMap;
614 SmallVector<Record*, 4> ReqFeatures;
615
Bill Wendling5d3174c2011-03-21 08:40:31 +0000616 std::string Result;
617 std::string AsmString;
Bill Wendling5d3174c2011-03-21 08:40:31 +0000618public:
Tim Northoveree20caa2014-05-12 18:04:06 +0000619 IAPrinter(std::string R, std::string AS) : Result(R), AsmString(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) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000660 if (Conds.empty() && ReqFeatures.empty()) {
661 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 {
738struct AliasPriorityComparator {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000739 typedef std::pair<CodeGenInstAlias, int> ValueType;
Tim Northover9a24f882014-05-20 09:17:16 +0000740 bool operator()(const ValueType &LHS, const ValueType &RHS) {
741 if (LHS.second == RHS.second) {
742 // We don't actually care about the order, but for consistency it
743 // shouldn't depend on pointer comparisons.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000744 return LHS.first.TheDef->getName() < RHS.first.TheDef->getName();
Tim Northover9a24f882014-05-20 09:17:16 +0000745 }
746
747 // Aliases with larger priorities should be considered first.
748 return LHS.second > RHS.second;
749 }
750};
751}
752
753
Bill Wendling7e5771d2011-03-21 08:31:53 +0000754void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
Bill Wendling7e5771d2011-03-21 08:31:53 +0000755 Record *AsmWriter = Target.getAsmWriter();
756
757 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
758 O << "#undef PRINT_ALIAS_INSTR\n\n";
759
Tim Northoveree20caa2014-05-12 18:04:06 +0000760 //////////////////////////////
761 // Gather information about aliases we need to print
762 //////////////////////////////
763
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000764 // Emit the method that prints the alias instruction.
765 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Tim Northover9a24f882014-05-20 09:17:16 +0000766 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Craig Topperc24a4012016-01-14 06:15:07 +0000767 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000768
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000769 std::vector<Record*> AllInstAliases =
770 Records.getAllDerivedDefinitions("InstAlias");
771
772 // Create a map from the qualified name to a list of potential matches.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000773 typedef std::set<std::pair<CodeGenInstAlias, int>, AliasPriorityComparator>
Tim Northover9a24f882014-05-20 09:17:16 +0000774 AliasWithPriority;
775 std::map<std::string, AliasWithPriority> AliasMap;
Craig Topper190ecd52016-01-08 07:06:32 +0000776 for (Record *R : AllInstAliases) {
Tim Northover9a24f882014-05-20 09:17:16 +0000777 int Priority = R->getValueAsInt("EmitPriority");
778 if (Priority < 1)
779 continue; // Aliases with priority 0 are never emitted.
780
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000781 const DagInit *DI = R->getValueAsDag("ResultInst");
Sean Silva88eb8dd2012-10-10 20:24:47 +0000782 const DefInit *Op = cast<DefInit>(DI->getOperator());
David Blaikie4ab57cd2015-08-06 19:23:33 +0000783 AliasMap[getQualifiedName(Op->getDef())].insert(
Craig Topper190ecd52016-01-08 07:06:32 +0000784 std::make_pair(CodeGenInstAlias(R, Variant, Target), Priority));
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000785 }
786
Bill Wendling7e570b52011-03-21 08:59:17 +0000787 // A map of which conditions need to be met for each instruction operand
788 // before it can be matched to the mnemonic.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000789 std::map<std::string, std::vector<IAPrinter>> IAPrinterMap;
Bill Wendling7e570b52011-03-21 08:59:17 +0000790
Craig Topper674d2382016-01-22 05:59:43 +0000791 std::vector<std::string> PrintMethods;
792
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000793 // A list of MCOperandPredicates for all operands in use, and the reverse map
794 std::vector<const Record*> MCOpPredicates;
795 DenseMap<const Record*, unsigned> MCOpPredicateMap;
796
Tim Northover9a24f882014-05-20 09:17:16 +0000797 for (auto &Aliases : AliasMap) {
798 for (auto &Alias : Aliases.second) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000799 const CodeGenInstAlias &CGA = Alias.first;
800 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000801 unsigned NumResultOps =
David Blaikie4ab57cd2015-08-06 19:23:33 +0000802 CountNumOperands(CGA.ResultInst->AsmString, Variant);
Bill Wendlinge7124492011-06-14 03:17:20 +0000803
804 // Don't emit the alias if it has more operands than what it's aliasing.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000805 if (NumResultOps < CountNumOperands(CGA.AsmString, Variant))
Bill Wendlinge7124492011-06-14 03:17:20 +0000806 continue;
807
David Blaikie4ab57cd2015-08-06 19:23:33 +0000808 IAPrinter IAP(CGA.Result->getAsString(), CGA.AsmString);
Bill Wendling7e570b52011-03-21 08:59:17 +0000809
Tim Northover60091cf2014-05-15 13:36:01 +0000810 unsigned NumMIOps = 0;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000811 for (auto &Operand : CGA.ResultOperands)
Tim Northover60091cf2014-05-15 13:36:01 +0000812 NumMIOps += Operand.getMINumOperands();
813
Bill Wendling7e570b52011-03-21 08:59:17 +0000814 std::string Cond;
Tim Northover60091cf2014-05-15 13:36:01 +0000815 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(NumMIOps);
David Blaikie4ab57cd2015-08-06 19:23:33 +0000816 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000817
Bill Wendling7e570b52011-03-21 08:59:17 +0000818 bool CantHandle = false;
819
Tim Northover60091cf2014-05-15 13:36:01 +0000820 unsigned MIOpNum = 0;
Bill Wendling7e570b52011-03-21 08:59:17 +0000821 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000822 std::string Op = "MI->getOperand(" + llvm::utostr(MIOpNum) + ")";
823
David Blaikie4ab57cd2015-08-06 19:23:33 +0000824 const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
Bill Wendling7e570b52011-03-21 08:59:17 +0000825
826 switch (RO.Kind) {
Bill Wendling7e570b52011-03-21 08:59:17 +0000827 case CodeGenInstAlias::ResultOperand::K_Record: {
828 const Record *Rec = RO.getRecord();
829 StringRef ROName = RO.getName();
Tim Northoveree20caa2014-05-12 18:04:06 +0000830 int PrintMethodIdx = -1;
Bill Wendling7e570b52011-03-21 08:59:17 +0000831
Tim Northoveree20caa2014-05-12 18:04:06 +0000832 // These two may have a PrintMethod, which we want to record (if it's
833 // the first time we've seen it) and provide an index for the aliasing
834 // code to use.
835 if (Rec->isSubClassOf("RegisterOperand") ||
836 Rec->isSubClassOf("Operand")) {
837 std::string PrintMethod = Rec->getValueAsString("PrintMethod");
838 if (PrintMethod != "" && PrintMethod != "printOperand") {
839 PrintMethodIdx = std::find(PrintMethods.begin(),
840 PrintMethods.end(), PrintMethod) -
841 PrintMethods.begin();
842 if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
843 PrintMethods.push_back(PrintMethod);
844 }
845 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000846
847 if (Rec->isSubClassOf("RegisterOperand"))
848 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling7e570b52011-03-21 08:59:17 +0000849 if (Rec->isSubClassOf("RegisterClass")) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000850 IAP.addCond(Op + ".isReg()");
Bill Wendling7e570b52011-03-21 08:59:17 +0000851
David Blaikie4ab57cd2015-08-06 19:23:33 +0000852 if (!IAP.isOpMapped(ROName)) {
853 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
854 Record *R = CGA.ResultOperands[i].getRecord();
Jack Carter9c1a0272013-02-05 08:32:10 +0000855 if (R->isSubClassOf("RegisterOperand"))
856 R = R->getValueAsDef("RegClass");
Benjamin Kramer682de392012-03-30 23:13:40 +0000857 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
Tim Northover60091cf2014-05-15 13:36:01 +0000858 R->getName() + "RegClassID)"
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000859 ".contains(" + Op + ".getReg())";
Bill Wendling7e570b52011-03-21 08:59:17 +0000860 } else {
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000861 Cond = Op + ".getReg() == MI->getOperand(" +
David Blaikie4ab57cd2015-08-06 19:23:33 +0000862 llvm::utostr(IAP.getOpIndex(ROName)) + ").getReg()";
Bill Wendling7e570b52011-03-21 08:59:17 +0000863 }
864 } else {
Tim Northoveree20caa2014-05-12 18:04:06 +0000865 // Assume all printable operands are desired for now. This can be
Alp Tokerbeaca192014-05-15 01:52:21 +0000866 // overridden in the InstAlias instantiation if necessary.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000867 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
Bill Wendling7e570b52011-03-21 08:59:17 +0000868
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000869 // There might be an additional predicate on the MCOperand
870 unsigned Entry = MCOpPredicateMap[Rec];
871 if (!Entry) {
872 if (!Rec->isValueUnset("MCOperandPredicate")) {
873 MCOpPredicates.push_back(Rec);
874 Entry = MCOpPredicates.size();
875 MCOpPredicateMap[Rec] = Entry;
876 } else
877 break; // No conditions on this operand at all
878 }
879 Cond = Target.getName() + ClassName + "ValidateMCOperand(" +
Oliver Stannarda34e4702015-12-01 10:48:51 +0000880 Op + ", STI, " + llvm::utostr(Entry) + ")";
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000881 }
882 // for all subcases of ResultOperand::K_Record:
David Blaikie4ab57cd2015-08-06 19:23:33 +0000883 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000884 break;
885 }
Tim Northoverab7689e2013-01-09 13:32:04 +0000886 case CodeGenInstAlias::ResultOperand::K_Imm: {
Tim Northoverab7689e2013-01-09 13:32:04 +0000887 // Just because the alias has an immediate result, doesn't mean the
888 // MCInst will. An MCExpr could be present, for example.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000889 IAP.addCond(Op + ".isImm()");
Tim Northoverab7689e2013-01-09 13:32:04 +0000890
David Blaikie4ab57cd2015-08-06 19:23:33 +0000891 Cond = Op + ".getImm() == " +
892 llvm::utostr(CGA.ResultOperands[i].getImm());
893 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000894 break;
Tim Northoverab7689e2013-01-09 13:32:04 +0000895 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000896 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000897 // If this is zero_reg, something's playing tricks we're not
898 // equipped to handle.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000899 if (!CGA.ResultOperands[i].getRegister()) {
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000900 CantHandle = true;
901 break;
902 }
903
David Blaikie4ab57cd2015-08-06 19:23:33 +0000904 Cond = Op + ".getReg() == " + Target.getName() + "::" +
905 CGA.ResultOperands[i].getRegister()->getName();
906 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000907 break;
908 }
909
Tim Northover60091cf2014-05-15 13:36:01 +0000910 MIOpNum += RO.getMINumOperands();
Bill Wendling7e570b52011-03-21 08:59:17 +0000911 }
912
913 if (CantHandle) continue;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000914 IAPrinterMap[Aliases.first].push_back(std::move(IAP));
Bill Wendling7e570b52011-03-21 08:59:17 +0000915 }
916 }
917
Tim Northoveree20caa2014-05-12 18:04:06 +0000918 //////////////////////////////
919 // Write out the printAliasInstr function
920 //////////////////////////////
921
Bill Wendlingf5199de2011-05-23 00:18:33 +0000922 std::string Header;
923 raw_string_ostream HeaderO(Header);
924
925 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendlinge7124492011-06-14 03:17:20 +0000926 << "::printAliasInstr(const MCInst"
Akira Hatanakab46d0232015-03-27 20:36:02 +0000927 << " *MI, " << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
928 << "raw_ostream &OS) {\n";
Bill Wendling7e570b52011-03-21 08:59:17 +0000929
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000930 std::string Cases;
931 raw_string_ostream CasesO(Cases);
932
David Blaikie4ab57cd2015-08-06 19:23:33 +0000933 for (auto &Entry : IAPrinterMap) {
934 std::vector<IAPrinter> &IAPs = Entry.second;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000935 std::vector<IAPrinter*> UniqueIAPs;
936
David Blaikie4ab57cd2015-08-06 19:23:33 +0000937 for (auto &LHS : IAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000938 bool IsDup = false;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000939 for (const auto &RHS : IAPs) {
940 if (&LHS != &RHS && LHS == RHS) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000941 IsDup = true;
942 break;
943 }
944 }
945
David Blaikie4ab57cd2015-08-06 19:23:33 +0000946 if (!IsDup)
947 UniqueIAPs.push_back(&LHS);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000948 }
949
950 if (UniqueIAPs.empty()) continue;
951
David Blaikie4ab57cd2015-08-06 19:23:33 +0000952 CasesO.indent(2) << "case " << Entry.first << ":\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000953
Craig Topper190ecd52016-01-08 07:06:32 +0000954 for (IAPrinter *IAP : UniqueIAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000955 CasesO.indent(4);
Evan Cheng4d806e22011-07-06 02:02:33 +0000956 IAP->print(CasesO);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000957 CasesO << '\n';
958 }
959
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000960 CasesO.indent(4) << "return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000961 }
962
Bill Wendlinge7124492011-06-14 03:17:20 +0000963 if (CasesO.str().empty()) {
Bill Wendlingf5199de2011-05-23 00:18:33 +0000964 O << HeaderO.str();
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000965 O << " return false;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000966 O << "}\n\n";
967 O << "#endif // PRINT_ALIAS_INSTR\n";
968 return;
969 }
970
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000971 if (!MCOpPredicates.empty())
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000972 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +0000973 << "ValidateMCOperand(const MCOperand &MCOp,\n"
974 << " const MCSubtargetInfo &STI,\n"
975 << " unsigned PredicateIndex);\n";
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000976
Bill Wendlingf5199de2011-05-23 00:18:33 +0000977 O << HeaderO.str();
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000978 O.indent(2) << "const char *AsmString;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000979 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000980 O.indent(2) << "default: return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000981 O << CasesO.str();
982 O.indent(2) << "}\n\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000983
984 // Code that prints the alias, replacing the operands with the ones from the
985 // MCInst.
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000986 O << " unsigned I = 0;\n";
Tim Northoverd8d65a62014-05-15 11:16:32 +0000987 O << " while (AsmString[I] != ' ' && AsmString[I] != '\t' &&\n";
988 O << " AsmString[I] != '\\0')\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000989 O << " ++I;\n";
990 O << " OS << '\\t' << StringRef(AsmString, I);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000991
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000992 O << " if (AsmString[I] != '\\0') {\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000993 O << " OS << '\\t';\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000994 O << " do {\n";
995 O << " if (AsmString[I] == '$') {\n";
996 O << " ++I;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +0000997 O << " if (AsmString[I] == (char)0xff) {\n";
998 O << " ++I;\n";
999 O << " int OpIdx = AsmString[I++] - 1;\n";
1000 O << " int PrintMethodIdx = AsmString[I++] - 1;\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001001 O << " printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, ";
1002 O << (PassSubtarget ? "STI, " : "");
1003 O << "OS);\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001004 O << " } else\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001005 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, ";
1006 O << (PassSubtarget ? "STI, " : "");
1007 O << "OS);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001008 O << " } else {\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001009 O << " OS << AsmString[I++];\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001010 O << " }\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001011 O << " } while (AsmString[I] != '\\0');\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001012 O << " }\n\n";
Jim Grosbachf4e67082012-04-18 18:56:33 +00001013
Eric Christopher2e3fbaa2011-04-18 21:28:11 +00001014 O << " return true;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001015 O << "}\n\n";
1016
Tim Northoveree20caa2014-05-12 18:04:06 +00001017 //////////////////////////////
1018 // Write out the printCustomAliasOperand function
1019 //////////////////////////////
1020
1021 O << "void " << Target.getName() << ClassName << "::"
1022 << "printCustomAliasOperand(\n"
1023 << " const MCInst *MI, unsigned OpIdx,\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001024 << " unsigned PrintMethodIdx,\n"
1025 << (PassSubtarget ? " const MCSubtargetInfo &STI,\n" : "")
1026 << " raw_ostream &OS) {\n";
Aaron Ballmane58a5702014-05-13 12:52:35 +00001027 if (PrintMethods.empty())
1028 O << " llvm_unreachable(\"Unknown PrintMethod kind\");\n";
1029 else {
1030 O << " switch (PrintMethodIdx) {\n"
1031 << " default:\n"
1032 << " llvm_unreachable(\"Unknown PrintMethod kind\");\n"
Tim Northoveree20caa2014-05-12 18:04:06 +00001033 << " break;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001034
Aaron Ballmane58a5702014-05-13 12:52:35 +00001035 for (unsigned i = 0; i < PrintMethods.size(); ++i) {
1036 O << " case " << i << ":\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001037 << " " << PrintMethods[i] << "(MI, OpIdx, "
1038 << (PassSubtarget ? "STI, " : "") << "OS);\n"
Aaron Ballmane58a5702014-05-13 12:52:35 +00001039 << " break;\n";
1040 }
1041 O << " }\n";
1042 }
1043 O << "}\n\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001044
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001045 if (!MCOpPredicates.empty()) {
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001046 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +00001047 << "ValidateMCOperand(const MCOperand &MCOp,\n"
1048 << " const MCSubtargetInfo &STI,\n"
1049 << " unsigned PredicateIndex) {\n"
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001050 << " switch (PredicateIndex) {\n"
1051 << " default:\n"
1052 << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
1053 << " break;\n";
1054
1055 for (unsigned i = 0; i < MCOpPredicates.size(); ++i) {
1056 Init *MCOpPred = MCOpPredicates[i]->getValueInit("MCOperandPredicate");
1057 if (StringInit *SI = dyn_cast<StringInit>(MCOpPred)) {
1058 O << " case " << i + 1 << ": {\n"
1059 << SI->getValue() << "\n"
1060 << " }\n";
1061 } else
1062 llvm_unreachable("Unexpected MCOperandPredicate field!");
1063 }
1064 O << " }\n"
1065 << "}\n\n";
1066 }
1067
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001068 O << "#endif // PRINT_ALIAS_INSTR\n";
1069}
Chris Lattner06c5eed2009-09-13 20:08:00 +00001070
Ahmed Bougachabd214002013-10-28 18:07:17 +00001071AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1072 Record *AsmWriter = Target.getAsmWriter();
Craig Topper0bd58742016-01-13 07:20:05 +00001073 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Ahmed Bougachabd214002013-10-28 18:07:17 +00001074
1075 // Get the instruction numbering.
Craig Topperf9265322016-01-17 20:38:14 +00001076 NumberedInstructions = Target.getInstructionsByEnumValue();
Ahmed Bougachabd214002013-10-28 18:07:17 +00001077
Craig Topperf9265322016-01-17 20:38:14 +00001078 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
1079 const CodeGenInstruction *I = NumberedInstructions[i];
Craig Topper9e9ae602016-01-17 08:05:33 +00001080 if (!I->AsmString.empty() && I->TheDef->getName() != "PHI")
1081 Instructions.emplace_back(*I, i, Variant);
1082 }
Ahmed Bougachabd214002013-10-28 18:07:17 +00001083}
1084
Chris Lattner06c5eed2009-09-13 20:08:00 +00001085void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +00001086 EmitPrintInstruction(O);
1087 EmitGetRegisterName(O);
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001088 EmitPrintAliasInstruction(O);
Chris Lattner06c5eed2009-09-13 20:08:00 +00001089}
1090
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001091
1092namespace llvm {
1093
1094void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1095 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1096 AsmWriterEmitter(RK).run(OS);
1097}
1098
1099} // End llvm namespace