blob: 681012974ce8083b003f4ec9277c119e6effea90 [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;
Tim Northoveree20caa2014-05-12 18:04:06 +000041 std::vector<std::string> PrintMethods;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000042public:
Ahmed Bougachabd214002013-10-28 18:07:17 +000043 AsmWriterEmitter(RecordKeeper &R);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000044
45 void run(raw_ostream &o);
46
47private:
48 void EmitPrintInstruction(raw_ostream &o);
49 void EmitGetRegisterName(raw_ostream &o);
50 void EmitPrintAliasInstruction(raw_ostream &O);
51
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000052 void FindUniqueOperandCommands(std::vector<std::string> &UOC,
53 std::vector<unsigned> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +000054 std::vector<unsigned> &InstOpsUsed,
55 bool PassSubtarget) const;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000056};
57} // end anonymous namespace
58
Chris Lattner59a7f5c2005-01-22 20:31:17 +000059static void PrintCases(std::vector<std::pair<std::string,
Craig Topperc24a4012016-01-14 06:15:07 +000060 AsmWriterOperand> > &OpsToPrint, raw_ostream &O,
61 bool PassSubtarget) {
Craig Topper0b271ad2016-01-13 07:20:13 +000062 O << " case " << OpsToPrint.back().first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000063 AsmWriterOperand TheOp = OpsToPrint.back().second;
64 OpsToPrint.pop_back();
65
66 // Check to see if any other operands are identical in this list, and if so,
67 // emit a case label for them.
68 for (unsigned i = OpsToPrint.size(); i != 0; --i)
69 if (OpsToPrint[i-1].second == TheOp) {
Craig Topper0b271ad2016-01-13 07:20:13 +000070 O << "\n case " << OpsToPrint[i-1].first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000071 OpsToPrint.erase(OpsToPrint.begin()+i-1);
72 }
73
74 // Finally, emit the code.
Craig Topperc24a4012016-01-14 06:15:07 +000075 O << "\n " << TheOp.getCode(PassSubtarget);
Craig Topper0b271ad2016-01-13 07:20:13 +000076 O << "\n break;\n";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000077}
78
Chris Lattner9ceb7c82005-01-22 18:38:13 +000079
80/// EmitInstructions - Emit the last instruction in the vector and any other
81/// instructions that are suitably similar to it.
82static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Craig Topperc24a4012016-01-14 06:15:07 +000083 raw_ostream &O, bool PassSubtarget) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000084 AsmWriterInst FirstInst = Insts.back();
85 Insts.pop_back();
86
87 std::vector<AsmWriterInst> SimilarInsts;
88 unsigned DifferingOperand = ~0;
89 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattner92275bb2005-01-22 19:22:23 +000090 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
91 if (DiffOp != ~1U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000092 if (DifferingOperand == ~0U) // First match!
93 DifferingOperand = DiffOp;
94
95 // If this differs in the same operand as the rest of the instructions in
96 // this class, move it to the SimilarInsts list.
Chris Lattner92275bb2005-01-22 19:22:23 +000097 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000098 SimilarInsts.push_back(Insts[i-1]);
99 Insts.erase(Insts.begin()+i-1);
100 }
101 }
102 }
103
Chris Lattner017b93d2006-05-01 17:01:17 +0000104 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000105 << FirstInst.CGI->TheDef->getName() << ":\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000106 for (const AsmWriterInst &AWI : SimilarInsts)
107 O << " case " << AWI.CGI->Namespace << "::"
108 << AWI.CGI->TheDef->getName() << ":\n";
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000109 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
110 if (i != DifferingOperand) {
111 // If the operand is the same for all instructions, just print it.
Craig Topperc24a4012016-01-14 06:15:07 +0000112 O << " " << FirstInst.Operands[i].getCode(PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000113 } else {
114 // If this is the operand that varies between all of the instructions,
115 // emit a switch for just this operand now.
116 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000117 O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000118 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattner017b93d2006-05-01 17:01:17 +0000119 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000120 FirstInst.CGI->TheDef->getName(),
121 FirstInst.Operands[i]));
Misha Brukman650ba8e2005-04-22 00:00:37 +0000122
Craig Topper190ecd52016-01-08 07:06:32 +0000123 for (const AsmWriterInst &AWI : SimilarInsts) {
Chris Lattner017b93d2006-05-01 17:01:17 +0000124 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000125 AWI.CGI->TheDef->getName(),
126 AWI.Operands[i]));
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000127 }
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000128 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
129 while (!OpsToPrint.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000130 PrintCases(OpsToPrint, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000131 O << " }";
132 }
133 O << "\n";
134 }
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000135 O << " break;\n";
136}
Chris Lattner0c23ba52005-01-22 17:32:42 +0000137
Chris Lattner692374c2006-07-18 17:18:03 +0000138void AsmWriterEmitter::
Jim Grosbacha5497342010-09-29 22:32:50 +0000139FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattneredee5252006-07-18 18:28:27 +0000140 std::vector<unsigned> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000141 std::vector<unsigned> &InstOpsUsed,
142 bool PassSubtarget) const {
Craig Topper9e9ae602016-01-17 08:05:33 +0000143 InstIdxs.assign(Instructions.size(), ~0U);
Jim Grosbacha5497342010-09-29 22:32:50 +0000144
Chris Lattner692374c2006-07-18 17:18:03 +0000145 // This vector parallels UniqueOperandCommands, keeping track of which
146 // instructions each case are used for. It is a comma separated string of
147 // enums.
148 std::vector<std::string> InstrsForCase;
149 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattneredee5252006-07-18 18:28:27 +0000150 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbacha5497342010-09-29 22:32:50 +0000151
Craig Topper9e9ae602016-01-17 08:05:33 +0000152 for (size_t i = 0, e = Instructions.size(); i != e; ++i) {
153 const AsmWriterInst &Inst = Instructions[i];
154 if (Inst.Operands.empty())
Chris Lattner692374c2006-07-18 17:18:03 +0000155 continue; // Instruction already done.
Chris Lattner9d250692006-07-18 17:50:22 +0000156
Craig Topper9e9ae602016-01-17 08:05:33 +0000157 std::string Command = " "+Inst.Operands[0].getCode(PassSubtarget)+"\n";
Chris Lattner9d250692006-07-18 17:50:22 +0000158
Chris Lattner692374c2006-07-18 17:18:03 +0000159 // Check to see if we already have 'Command' in UniqueOperandCommands.
160 // If not, add it.
Craig Toppera99859d2016-01-17 08:05:30 +0000161 auto I = std::find(UniqueOperandCommands.begin(),
162 UniqueOperandCommands.end(), Command);
163 if (I != UniqueOperandCommands.end()) {
164 size_t idx = I - UniqueOperandCommands.begin();
165 InstIdxs[i] = idx;
166 InstrsForCase[idx] += ", ";
Craig Topper9e9ae602016-01-17 08:05:33 +0000167 InstrsForCase[idx] += Inst.CGI->TheDef->getName();
Craig Toppera99859d2016-01-17 08:05:30 +0000168 } else {
Chris Lattner692374c2006-07-18 17:18:03 +0000169 InstIdxs[i] = UniqueOperandCommands.size();
Craig Topper1993e3b2016-01-08 07:06:29 +0000170 UniqueOperandCommands.push_back(std::move(Command));
Craig Topper9e9ae602016-01-17 08:05:33 +0000171 InstrsForCase.push_back(Inst.CGI->TheDef->getName());
Chris Lattneredee5252006-07-18 18:28:27 +0000172
173 // This command matches one operand so far.
174 InstOpsUsed.push_back(1);
175 }
176 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000177
Chris Lattneredee5252006-07-18 18:28:27 +0000178 // For each entry of UniqueOperandCommands, there is a set of instructions
179 // that uses it. If the next command of all instructions in the set are
180 // identical, fold it into the command.
181 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
182 CommandIdx != e; ++CommandIdx) {
Jim Grosbacha5497342010-09-29 22:32:50 +0000183
Chris Lattneredee5252006-07-18 18:28:27 +0000184 for (unsigned Op = 1; ; ++Op) {
185 // Scan for the first instruction in the set.
Craig Topper9e9ae602016-01-17 08:05:33 +0000186 auto NIT = std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
Chris Lattneredee5252006-07-18 18:28:27 +0000187 if (NIT == InstIdxs.end()) break; // No commonality.
188
189 // If this instruction has no more operands, we isn't anything to merge
190 // into this command.
Craig Topper9e9ae602016-01-17 08:05:33 +0000191 const AsmWriterInst &FirstInst = Instructions[NIT-InstIdxs.begin()];
192 if (FirstInst.Operands.size() == Op)
Chris Lattneredee5252006-07-18 18:28:27 +0000193 break;
194
195 // Otherwise, scan to see if all of the other instructions in this command
196 // set share the operand.
197 bool AllSame = true;
David Greene5b4bc262009-07-29 20:10:24 +0000198
Chris Lattneredee5252006-07-18 18:28:27 +0000199 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
200 NIT != InstIdxs.end();
201 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
202 // Okay, found another instruction in this command set. If the operand
203 // matches, we're ok, otherwise bail out.
Craig Topper9e9ae602016-01-17 08:05:33 +0000204 const AsmWriterInst &OtherInst = Instructions[NIT-InstIdxs.begin()];
David Greene5b4bc262009-07-29 20:10:24 +0000205
Craig Topper9e9ae602016-01-17 08:05:33 +0000206 if (OtherInst.Operands.size() == Op ||
207 OtherInst.Operands[Op] != FirstInst.Operands[Op]) {
Chris Lattneredee5252006-07-18 18:28:27 +0000208 AllSame = false;
209 break;
210 }
211 }
212 if (!AllSame) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000213
Chris Lattneredee5252006-07-18 18:28:27 +0000214 // Okay, everything in this command set has the same next operand. Add it
215 // to UniqueOperandCommands and remember that it was consumed.
Craig Topperc24a4012016-01-14 06:15:07 +0000216 std::string Command = " " +
Craig Topper9e9ae602016-01-17 08:05:33 +0000217 FirstInst.Operands[Op].getCode(PassSubtarget) + "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000218
Chris Lattneredee5252006-07-18 18:28:27 +0000219 UniqueOperandCommands[CommandIdx] += Command;
220 InstOpsUsed[CommandIdx]++;
Chris Lattner692374c2006-07-18 17:18:03 +0000221 }
222 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000223
Chris Lattner692374c2006-07-18 17:18:03 +0000224 // Prepend some of the instructions each case is used for onto the case val.
225 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
226 std::string Instrs = InstrsForCase[i];
227 if (Instrs.size() > 70) {
228 Instrs.erase(Instrs.begin()+70, Instrs.end());
229 Instrs += "...";
230 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000231
Chris Lattner692374c2006-07-18 17:18:03 +0000232 if (!Instrs.empty())
Jim Grosbacha5497342010-09-29 22:32:50 +0000233 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattner692374c2006-07-18 17:18:03 +0000234 UniqueOperandCommands[i];
235 }
236}
237
238
Daniel Dunbar04f049f2009-10-17 20:43:42 +0000239static void UnescapeString(std::string &Str) {
240 for (unsigned i = 0; i != Str.size(); ++i) {
241 if (Str[i] == '\\' && i != Str.size()-1) {
242 switch (Str[i+1]) {
243 default: continue; // Don't execute the code after the switch.
244 case 'a': Str[i] = '\a'; break;
245 case 'b': Str[i] = '\b'; break;
246 case 'e': Str[i] = 27; break;
247 case 'f': Str[i] = '\f'; break;
248 case 'n': Str[i] = '\n'; break;
249 case 'r': Str[i] = '\r'; break;
250 case 't': Str[i] = '\t'; break;
251 case 'v': Str[i] = '\v'; break;
252 case '"': Str[i] = '\"'; break;
253 case '\'': Str[i] = '\''; break;
254 case '\\': Str[i] = '\\'; break;
255 }
256 // Nuke the second character.
257 Str.erase(Str.begin()+i+1);
258 }
259 }
260}
261
Chris Lattner06c5eed2009-09-13 20:08:00 +0000262/// EmitPrintInstruction - Generate the code for the "printInstruction" method
Ahmed Bougachabd214002013-10-28 18:07:17 +0000263/// implementation. Destroys all instances of AsmWriterInst information, by
264/// clearing the Instructions vector.
Chris Lattner06c5eed2009-09-13 20:08:00 +0000265void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner6ffa5012004-08-14 22:50:53 +0000266 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner72770f52004-10-03 20:19:02 +0000267 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Craig Topperc24a4012016-01-14 06:15:07 +0000268 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
Jim Grosbacha5497342010-09-29 22:32:50 +0000269
Chris Lattner1c4ae852004-08-01 05:59:33 +0000270 O <<
271 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner06c5eed2009-09-13 20:08:00 +0000272 "/// from the instruction set description.\n"
Chris Lattnerb94284b2009-08-08 01:32:19 +0000273 "void " << Target.getName() << ClassName
Akira Hatanakab46d0232015-03-27 20:36:02 +0000274 << "::printInstruction(const MCInst *MI, "
275 << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
276 << "raw_ostream &O) {\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000277
Chris Lattnere32982c2006-07-14 22:59:11 +0000278 // Build an aggregate string, and build a table of offsets into it.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000279 SequenceToOffsetTable<std::string> StringTable;
Jim Grosbacha5497342010-09-29 22:32:50 +0000280
Chris Lattner5d751b42006-09-27 16:44:09 +0000281 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000282 /// chunk of the output as well as indices used for operand printing.
Craig Topperf9265322016-01-17 20:38:14 +0000283 std::vector<uint64_t> OpcodeInfo(NumberedInstructions.size());
Craig Topperd4f87a32016-01-13 07:20:12 +0000284 const unsigned OpcodeInfoBits = 64;
Jim Grosbacha5497342010-09-29 22:32:50 +0000285
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000286 // Add all strings to the string table upfront so it can generate an optimized
287 // representation.
Craig Topper9e9ae602016-01-17 08:05:33 +0000288 for (AsmWriterInst &AWI : Instructions) {
289 if (AWI.Operands[0].OperandType ==
Jim Grosbachf4e67082012-04-18 18:56:33 +0000290 AsmWriterOperand::isLiteralTextOperand &&
Craig Topper9e9ae602016-01-17 08:05:33 +0000291 !AWI.Operands[0].Str.empty()) {
292 std::string Str = AWI.Operands[0].Str;
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000293 UnescapeString(Str);
294 StringTable.add(Str);
295 }
296 }
297
298 StringTable.layout();
299
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000300 unsigned MaxStringIdx = 0;
Craig Topper9e9ae602016-01-17 08:05:33 +0000301 for (AsmWriterInst &AWI : Instructions) {
Chris Lattnere32982c2006-07-14 22:59:11 +0000302 unsigned Idx;
Craig Topper9e9ae602016-01-17 08:05:33 +0000303 if (AWI.Operands[0].OperandType != AsmWriterOperand::isLiteralTextOperand ||
304 AWI.Operands[0].Str.empty()) {
Chris Lattner36504652006-07-19 01:39:06 +0000305 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000306 Idx = StringTable.get("");
Chris Lattnere32982c2006-07-14 22:59:11 +0000307 } else {
Craig Topper9e9ae602016-01-17 08:05:33 +0000308 std::string Str = AWI.Operands[0].Str;
Chris Lattnerb47ed612009-09-14 01:16:36 +0000309 UnescapeString(Str);
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000310 Idx = StringTable.get(Str);
Chris Lattnerb47ed612009-09-14 01:16:36 +0000311 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbacha5497342010-09-29 22:32:50 +0000312
Chris Lattnere32982c2006-07-14 22:59:11 +0000313 // Nuke the string from the operand list. It is now handled!
Craig Topper9e9ae602016-01-17 08:05:33 +0000314 AWI.Operands.erase(AWI.Operands.begin());
Chris Lattner92275bb2005-01-22 19:22:23 +0000315 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000316
Chris Lattnerb47ed612009-09-14 01:16:36 +0000317 // Bias offset by one since we want 0 as a sentinel.
Craig Topper9e9ae602016-01-17 08:05:33 +0000318 OpcodeInfo[AWI.CGIIndex] = Idx+1;
Chris Lattner92275bb2005-01-22 19:22:23 +0000319 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000320
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000321 // Figure out how many bits we used for the string index.
Chris Lattnerb47ed612009-09-14 01:16:36 +0000322 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbacha5497342010-09-29 22:32:50 +0000323
Chris Lattner692374c2006-07-18 17:18:03 +0000324 // To reduce code size, we compactify common instructions into a few bits
325 // in the opcode-indexed table.
Craig Topperd4f87a32016-01-13 07:20:12 +0000326 unsigned BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000327
Craig Topper1f387362014-11-25 20:11:25 +0000328 std::vector<std::vector<std::string>> TableDrivenOperandPrinters;
Jim Grosbacha5497342010-09-29 22:32:50 +0000329
Chris Lattnercb0c8482006-07-18 17:56:07 +0000330 while (1) {
Chris Lattner692374c2006-07-18 17:18:03 +0000331 std::vector<std::string> UniqueOperandCommands;
Chris Lattner692374c2006-07-18 17:18:03 +0000332 std::vector<unsigned> InstIdxs;
Chris Lattneredee5252006-07-18 18:28:27 +0000333 std::vector<unsigned> NumInstOpsHandled;
334 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000335 NumInstOpsHandled, PassSubtarget);
Jim Grosbacha5497342010-09-29 22:32:50 +0000336
Chris Lattner692374c2006-07-18 17:18:03 +0000337 // If we ran out of operands to print, we're done.
338 if (UniqueOperandCommands.empty()) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000339
Chris Lattner692374c2006-07-18 17:18:03 +0000340 // Compute the number of bits we need to represent these cases, this is
341 // ceil(log2(numentries)).
342 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbacha5497342010-09-29 22:32:50 +0000343
Chris Lattner692374c2006-07-18 17:18:03 +0000344 // If we don't have enough bits for this operand, don't include it.
345 if (NumBits > BitsLeft) {
Chris Lattner34822f62009-08-23 04:44:11 +0000346 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
347 << " more bits\n");
Chris Lattner692374c2006-07-18 17:18:03 +0000348 break;
349 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000350
Chris Lattner692374c2006-07-18 17:18:03 +0000351 // Otherwise, we can include this in the initial lookup table. Add it in.
Craig Topper9e9ae602016-01-17 08:05:33 +0000352 for (size_t i = 0, e = Instructions.size(); i != e; ++i)
353 if (InstIdxs[i] != ~0U)
354 OpcodeInfo[Instructions[i].CGIIndex] |=
355 (uint64_t)InstIdxs[i] << (OpcodeInfoBits-BitsLeft);
Craig Topper06cec4c2012-09-14 08:33:11 +0000356 BitsLeft -= NumBits;
Jim Grosbacha5497342010-09-29 22:32:50 +0000357
Chris Lattnercb0c8482006-07-18 17:56:07 +0000358 // Remove the info about this operand.
Craig Topper9e9ae602016-01-17 08:05:33 +0000359 for (size_t i = 0, e = Instructions.size(); i != e; ++i) {
360 AsmWriterInst &Inst = Instructions[i];
361 if (!Inst.Operands.empty()) {
362 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
363 assert(NumOps <= Inst.Operands.size() &&
364 "Can't remove this many ops!");
365 Inst.Operands.erase(Inst.Operands.begin(),
366 Inst.Operands.begin()+NumOps);
367 }
Chris Lattnercb0c8482006-07-18 17:56:07 +0000368 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000369
Chris Lattnercb0c8482006-07-18 17:56:07 +0000370 // Remember the handlers for this set of operands.
Craig Topper1f387362014-11-25 20:11:25 +0000371 TableDrivenOperandPrinters.push_back(std::move(UniqueOperandCommands));
Chris Lattner692374c2006-07-18 17:18:03 +0000372 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000373
Craig Topper14d91732016-01-11 05:13:41 +0000374 // Emit the string table itself.
Reid Kleckner132c40f2014-07-17 19:43:40 +0000375 O << " static const char AsmStrs[] = {\n";
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000376 StringTable.emit(O, printChar);
377 O << " };\n\n";
Chris Lattnere32982c2006-07-14 22:59:11 +0000378
Craig Topper14d91732016-01-11 05:13:41 +0000379 // Emit the lookup tables in pieces to minimize wasted bytes.
Craig Topperd4f87a32016-01-13 07:20:12 +0000380 unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
Craig Topper14d91732016-01-11 05:13:41 +0000381 unsigned Table = 0, Shift = 0;
382 SmallString<128> BitsString;
383 raw_svector_ostream BitsOS(BitsString);
384 // If the total bits is more than 32-bits we need to use a 64-bit type.
Craig Topperd4f87a32016-01-13 07:20:12 +0000385 BitsOS << " uint" << ((BitsLeft < (OpcodeInfoBits - 32)) ? 64 : 32)
386 << "_t Bits = 0;\n";
Craig Topper14d91732016-01-11 05:13:41 +0000387 while (BytesNeeded != 0) {
388 // Figure out how big this table section needs to be, but no bigger than 4.
389 unsigned TableSize = std::min(1 << Log2_32(BytesNeeded), 4);
390 BytesNeeded -= TableSize;
391 TableSize *= 8; // Convert to bits;
392 uint64_t Mask = (1ULL << TableSize) - 1;
393 O << " static const uint" << TableSize << "_t OpInfo" << Table
394 << "[] = {\n";
Craig Topperf9265322016-01-17 20:38:14 +0000395 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Craig Topper14d91732016-01-11 05:13:41 +0000396 O << " " << ((OpcodeInfo[i] >> Shift) & Mask) << "U,\t// "
Craig Topperf9265322016-01-17 20:38:14 +0000397 << NumberedInstructions[i]->TheDef->getName() << "\n";
Craig Topper14d91732016-01-11 05:13:41 +0000398 }
399 O << " };\n\n";
400 // Emit string to combine the individual table lookups.
401 BitsOS << " Bits |= ";
402 // If the total bits is more than 32-bits we need to use a 64-bit type.
Craig Topperd4f87a32016-01-13 07:20:12 +0000403 if (BitsLeft < (OpcodeInfoBits - 32))
Craig Topper14d91732016-01-11 05:13:41 +0000404 BitsOS << "(uint64_t)";
405 BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
406 // Prepare the shift for the next iteration and increment the table count.
407 Shift += TableSize;
408 ++Table;
409 }
410
411 // Emit the initial tab character.
Evan Cheng32e53472008-02-02 08:39:46 +0000412 O << " O << \"\\t\";\n\n";
413
Craig Topper06cec4c2012-09-14 08:33:11 +0000414 O << " // Emit the opcode for the instruction.\n";
Craig Topper14d91732016-01-11 05:13:41 +0000415 O << BitsString;
416
417 // Emit the starting string.
Manman Ren68cf9fc2012-09-13 17:43:46 +0000418 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattnerb47ed612009-09-14 01:16:36 +0000419 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenefdd25192009-08-05 21:00:52 +0000420
Chris Lattner692374c2006-07-18 17:18:03 +0000421 // Output the table driven operand information.
Craig Topperd4f87a32016-01-13 07:20:12 +0000422 BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000423 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
424 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
425
426 // Compute the number of bits we need to represent these cases, this is
427 // ceil(log2(numentries)).
428 unsigned NumBits = Log2_32_Ceil(Commands.size());
429 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbacha5497342010-09-29 22:32:50 +0000430
Chris Lattner692374c2006-07-18 17:18:03 +0000431 // Emit code to extract this field from Bits.
Chris Lattner692374c2006-07-18 17:18:03 +0000432 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000433 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000434
Chris Lattneredee5252006-07-18 18:28:27 +0000435 if (Commands.size() == 2) {
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000436 // Emit two possibilitys with if/else.
Craig Topper06cec4c2012-09-14 08:33:11 +0000437 O << " if ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000438 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000439 << ((1 << NumBits)-1) << ") {\n"
440 << Commands[1]
441 << " } else {\n"
442 << Commands[0]
443 << " }\n\n";
Eric Christophera573d192010-09-18 18:50:27 +0000444 } else if (Commands.size() == 1) {
445 // Emit a single possibility.
446 O << Commands[0] << "\n\n";
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000447 } else {
Craig Topper06cec4c2012-09-14 08:33:11 +0000448 O << " switch ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000449 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000450 << ((1 << NumBits)-1) << ") {\n"
Craig Toppera4ff6ae2014-11-24 14:09:52 +0000451 << " default: llvm_unreachable(\"Invalid command number.\");\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000452
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000453 // Print out all the cases.
Craig Topper190ecd52016-01-08 07:06:32 +0000454 for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
455 O << " case " << j << ":\n";
456 O << Commands[j];
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000457 O << " break;\n";
458 }
459 O << " }\n\n";
Chris Lattner692374c2006-07-18 17:18:03 +0000460 }
Craig Topper06cec4c2012-09-14 08:33:11 +0000461 BitsLeft -= NumBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000462 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000463
Chris Lattnercb0c8482006-07-18 17:56:07 +0000464 // Okay, delete instructions with no operand info left.
Craig Topper4f1f1152016-01-13 07:20:10 +0000465 auto I = std::remove_if(Instructions.begin(), Instructions.end(),
466 [](AsmWriterInst &Inst) {
467 return Inst.Operands.empty();
468 });
469 Instructions.erase(I, Instructions.end());
Chris Lattner692374c2006-07-18 17:18:03 +0000470
Jim Grosbacha5497342010-09-29 22:32:50 +0000471
Chris Lattner692374c2006-07-18 17:18:03 +0000472 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000473 // elements in the vector.
474 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbacha5497342010-09-29 22:32:50 +0000475
476
Craig Topperdf390602016-01-13 07:20:07 +0000477 // Now that we've emitted all of the operand info that fit into 64 bits, emit
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000478 // information for those instructions that are left. This is a less dense
Craig Topperdf390602016-01-13 07:20:07 +0000479 // encoding, but we expect the main 64-bit table to handle the majority of
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000480 // instructions.
Chris Lattner66e288b2006-07-18 17:38:46 +0000481 if (!Instructions.empty()) {
482 // Find the opcode # of inline asm.
483 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000484 O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
Chris Lattner66e288b2006-07-18 17:38:46 +0000485 while (!Instructions.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000486 EmitInstructions(Instructions, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000487
Chris Lattner66e288b2006-07-18 17:38:46 +0000488 O << " }\n";
489 }
David Greene5b4bc262009-07-29 20:10:24 +0000490
Chris Lattner6e172082006-07-18 19:06:01 +0000491 O << "}\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000492}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000493
Craig Topperba6d83e2014-11-24 02:08:35 +0000494static const char *getMinimalTypeForRange(uint64_t Range) {
495 assert(Range < 0xFFFFFFFFULL && "Enum too large");
496 if (Range > 0xFFFF)
497 return "uint32_t";
498 if (Range > 0xFF)
499 return "uint16_t";
500 return "uint8_t";
501}
502
Owen Andersona84be6c2011-06-27 21:06:21 +0000503static void
504emitRegisterNameString(raw_ostream &O, StringRef AltName,
David Blaikie9b613db2014-11-29 18:13:39 +0000505 const std::deque<CodeGenRegister> &Registers) {
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000506 SequenceToOffsetTable<std::string> StringTable;
507 SmallVector<std::string, 4> AsmNames(Registers.size());
David Blaikie9b613db2014-11-29 18:13:39 +0000508 unsigned i = 0;
509 for (const auto &Reg : Registers) {
510 std::string &AsmName = AsmNames[i++];
Owen Andersona84be6c2011-06-27 21:06:21 +0000511
Owen Andersona84be6c2011-06-27 21:06:21 +0000512 // "NoRegAltName" is special. We don't need to do a lookup for that,
513 // as it's just a reference to the default register name.
514 if (AltName == "" || AltName == "NoRegAltName") {
515 AsmName = Reg.TheDef->getValueAsString("AsmName");
516 if (AsmName.empty())
517 AsmName = Reg.getName();
518 } else {
519 // Make sure the register has an alternate name for this index.
520 std::vector<Record*> AltNameList =
521 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
522 unsigned Idx = 0, e;
523 for (e = AltNameList.size();
524 Idx < e && (AltNameList[Idx]->getName() != AltName);
525 ++Idx)
526 ;
527 // If the register has an alternate name for this index, use it.
528 // Otherwise, leave it empty as an error flag.
529 if (Idx < e) {
530 std::vector<std::string> AltNames =
531 Reg.TheDef->getValueAsListOfStrings("AltNames");
532 if (AltNames.size() <= Idx)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000533 PrintFatalError(Reg.TheDef->getLoc(),
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000534 "Register definition missing alt name for '" +
535 AltName + "'.");
Owen Andersona84be6c2011-06-27 21:06:21 +0000536 AsmName = AltNames[Idx];
537 }
538 }
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000539 StringTable.add(AsmName);
540 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000541
Craig Topperf8f0a232012-09-15 01:22:42 +0000542 StringTable.layout();
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000543 O << " static const char AsmStrs" << AltName << "[] = {\n";
544 StringTable.emit(O, printChar);
545 O << " };\n\n";
546
Craig Topperba6d83e2014-11-24 02:08:35 +0000547 O << " static const " << getMinimalTypeForRange(StringTable.size()-1)
548 << " RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000549 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Topper7a2cea12012-04-02 00:47:39 +0000550 if ((i % 14) == 0)
551 O << "\n ";
552 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersona84be6c2011-06-27 21:06:21 +0000553 }
Craig Topper9c252eb2012-04-03 06:52:47 +0000554 O << "\n };\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000555 << "\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000556}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000557
558void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +0000559 Record *AsmWriter = Target.getAsmWriter();
560 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
David Blaikie9b613db2014-11-29 18:13:39 +0000561 const auto &Registers = Target.getRegBank().getRegisters();
Craig Topper83421ec2016-01-17 20:38:21 +0000562 const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
Owen Andersona84be6c2011-06-27 21:06:21 +0000563 bool hasAltNames = AltNameIndices.size() > 1;
Hal Finkelcd5f9842015-12-11 17:31:27 +0000564 std::string Namespace =
565 Registers.front().TheDef->getValueAsString("Namespace");
Jim Grosbacha5497342010-09-29 22:32:50 +0000566
Chris Lattner06c5eed2009-09-13 20:08:00 +0000567 O <<
568 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
569 "/// from the register set description. This returns the assembler name\n"
570 "/// for the specified register.\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000571 "const char *" << Target.getName() << ClassName << "::";
572 if (hasAltNames)
573 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
574 else
575 O << "getRegisterName(unsigned RegNo) {\n";
576 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
577 << " && \"Invalid register number!\");\n"
Chris Lattnera7e8ae42009-09-14 01:26:18 +0000578 << "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000579
Owen Andersona84be6c2011-06-27 21:06:21 +0000580 if (hasAltNames) {
Craig Topper190ecd52016-01-08 07:06:32 +0000581 for (const Record *R : AltNameIndices)
582 emitRegisterNameString(O, R->getName(), Registers);
Owen Andersona84be6c2011-06-27 21:06:21 +0000583 } else
584 emitRegisterNameString(O, "", Registers);
Jim Grosbacha5497342010-09-29 22:32:50 +0000585
Owen Andersona84be6c2011-06-27 21:06:21 +0000586 if (hasAltNames) {
Craig Topperba6d83e2014-11-24 02:08:35 +0000587 O << " switch(AltIdx) {\n"
Craig Topperc4965bc2012-02-05 07:21:30 +0000588 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000589 for (const Record *R : AltNameIndices) {
590 std::string AltName(R->getName());
Hal Finkelcd5f9842015-12-11 17:31:27 +0000591 std::string Prefix = !Namespace.empty() ? Namespace + "::" : "";
592 O << " case " << Prefix << AltName << ":\n"
Craig Topperba6d83e2014-11-24 02:08:35 +0000593 << " assert(*(AsmStrs" << AltName << "+RegAsmOffset"
594 << AltName << "[RegNo-1]) &&\n"
595 << " \"Invalid alt name index for register!\");\n"
596 << " return AsmStrs" << AltName << "+RegAsmOffset"
597 << AltName << "[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000598 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000599 O << " }\n";
600 } else {
601 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
602 << " \"Invalid alt name index for register!\");\n"
603 << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000604 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000605 O << "}\n";
Chris Lattner06c5eed2009-09-13 20:08:00 +0000606}
607
Bill Wendling7e5771d2011-03-21 08:31:53 +0000608namespace {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000609// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
610// they both have the same conditionals. In which case, we cannot print out the
611// alias for that pattern.
612class IAPrinter {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000613 std::vector<std::string> Conds;
Tim Northoveree20caa2014-05-12 18:04:06 +0000614 std::map<StringRef, std::pair<int, int>> OpMap;
615 SmallVector<Record*, 4> ReqFeatures;
616
Bill Wendling5d3174c2011-03-21 08:40:31 +0000617 std::string Result;
618 std::string AsmString;
Bill Wendling5d3174c2011-03-21 08:40:31 +0000619public:
Tim Northoveree20caa2014-05-12 18:04:06 +0000620 IAPrinter(std::string R, std::string AS) : Result(R), AsmString(AS) {}
Bill Wendling5d3174c2011-03-21 08:40:31 +0000621
622 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000623
Tim Northoveree20caa2014-05-12 18:04:06 +0000624 void addOperand(StringRef Op, int OpIdx, int PrintMethodIdx = -1) {
625 assert(OpIdx >= 0 && OpIdx < 0xFE && "Idx out of range");
Tim Northover0ee9e7e2014-05-13 09:37:41 +0000626 assert(PrintMethodIdx >= -1 && PrintMethodIdx < 0xFF &&
Jay Foadb3590512014-05-13 08:26:53 +0000627 "Idx out of range");
Tim Northoveree20caa2014-05-12 18:04:06 +0000628 OpMap[Op] = std::make_pair(OpIdx, PrintMethodIdx);
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000629 }
Tim Northoveree20caa2014-05-12 18:04:06 +0000630
Bill Wendling5d3174c2011-03-21 08:40:31 +0000631 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
Tim Northoveree20caa2014-05-12 18:04:06 +0000632 int getOpIndex(StringRef Op) { return OpMap[Op].first; }
633 std::pair<int, int> &getOpData(StringRef Op) { return OpMap[Op]; }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000634
Tim Northoverd8d65a62014-05-15 11:16:32 +0000635 std::pair<StringRef, StringRef::iterator> parseName(StringRef::iterator Start,
636 StringRef::iterator End) {
637 StringRef::iterator I = Start;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000638 StringRef::iterator Next;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000639 if (*I == '{') {
640 // ${some_name}
641 Start = ++I;
642 while (I != End && *I != '}')
643 ++I;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000644 Next = I;
645 // eat the final '}'
646 if (Next != End)
647 ++Next;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000648 } else {
649 // $name, just eat the usual suspects.
650 while (I != End &&
651 ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') ||
652 (*I >= '0' && *I <= '9') || *I == '_'))
653 ++I;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000654 Next = I;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000655 }
656
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000657 return std::make_pair(StringRef(Start, I - Start), Next);
Tim Northoverd8d65a62014-05-15 11:16:32 +0000658 }
659
Evan Cheng4d806e22011-07-06 02:02:33 +0000660 void print(raw_ostream &O) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000661 if (Conds.empty() && ReqFeatures.empty()) {
662 O.indent(6) << "return true;\n";
Evan Cheng4d806e22011-07-06 02:02:33 +0000663 return;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000664 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000665
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000666 O << "if (";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000667
668 for (std::vector<std::string>::iterator
669 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
670 if (I != Conds.begin()) {
671 O << " &&\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000672 O.indent(8);
Bill Wendling5d3174c2011-03-21 08:40:31 +0000673 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000674
Bill Wendling5d3174c2011-03-21 08:40:31 +0000675 O << *I;
676 }
677
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000678 O << ") {\n";
679 O.indent(6) << "// " << Result << "\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000680
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000681 // Directly mangle mapped operands into the string. Each operand is
682 // identified by a '$' sign followed by a byte identifying the number of the
683 // operand. We add one to the index to avoid zero bytes.
Tim Northoverd8d65a62014-05-15 11:16:32 +0000684 StringRef ASM(AsmString);
685 SmallString<128> OutString;
686 raw_svector_ostream OS(OutString);
687 for (StringRef::iterator I = ASM.begin(), E = ASM.end(); I != E;) {
688 OS << *I;
689 if (*I == '$') {
690 StringRef Name;
691 std::tie(Name, I) = parseName(++I, E);
692 assert(isOpMapped(Name) && "Unmapped operand!");
Tim Northoveree20caa2014-05-12 18:04:06 +0000693
Tim Northoverd8d65a62014-05-15 11:16:32 +0000694 int OpIndex, PrintIndex;
695 std::tie(OpIndex, PrintIndex) = getOpData(Name);
696 if (PrintIndex == -1) {
697 // Can use the default printOperand route.
698 OS << format("\\x%02X", (unsigned char)OpIndex + 1);
699 } else
700 // 3 bytes if a PrintMethod is needed: 0xFF, the MCInst operand
701 // number, and which of our pre-detected Methods to call.
702 OS << format("\\xFF\\x%02X\\x%02X", OpIndex + 1, PrintIndex + 1);
703 } else {
704 ++I;
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000705 }
706 }
707
708 // Emit the string.
Yaron Keren09fb7c62015-03-10 07:33:23 +0000709 O.indent(6) << "AsmString = \"" << OutString << "\";\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000710
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000711 O.indent(6) << "break;\n";
712 O.indent(4) << '}';
Bill Wendling5d3174c2011-03-21 08:40:31 +0000713 }
714
David Blaikie4ab57cd2015-08-06 19:23:33 +0000715 bool operator==(const IAPrinter &RHS) const {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000716 if (Conds.size() != RHS.Conds.size())
717 return false;
718
719 unsigned Idx = 0;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000720 for (const auto &str : Conds)
721 if (str != RHS.Conds[Idx++])
Bill Wendling5d3174c2011-03-21 08:40:31 +0000722 return false;
723
724 return true;
725 }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000726};
727
Bill Wendling7e5771d2011-03-21 08:31:53 +0000728} // end anonymous namespace
729
Tim Northover5896b062014-05-16 09:42:04 +0000730static unsigned CountNumOperands(StringRef AsmString, unsigned Variant) {
731 std::string FlatAsmString =
732 CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant);
733 AsmString = FlatAsmString;
Bill Wendlinge7124492011-06-14 03:17:20 +0000734
Tim Northover5896b062014-05-16 09:42:04 +0000735 return AsmString.count(' ') + AsmString.count('\t');
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000736}
Bill Wendlinge7124492011-06-14 03:17:20 +0000737
Tim Northover9a24f882014-05-20 09:17:16 +0000738namespace {
739struct AliasPriorityComparator {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000740 typedef std::pair<CodeGenInstAlias, int> ValueType;
Tim Northover9a24f882014-05-20 09:17:16 +0000741 bool operator()(const ValueType &LHS, const ValueType &RHS) {
742 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.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000745 return LHS.first.TheDef->getName() < RHS.first.TheDef->getName();
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};
752}
753
754
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.
766 std::string 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
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000792 // A list of MCOperandPredicates for all operands in use, and the reverse map
793 std::vector<const Record*> MCOpPredicates;
794 DenseMap<const Record*, unsigned> MCOpPredicateMap;
795
Tim Northover9a24f882014-05-20 09:17:16 +0000796 for (auto &Aliases : AliasMap) {
797 for (auto &Alias : Aliases.second) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000798 const CodeGenInstAlias &CGA = Alias.first;
799 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000800 unsigned NumResultOps =
David Blaikie4ab57cd2015-08-06 19:23:33 +0000801 CountNumOperands(CGA.ResultInst->AsmString, Variant);
Bill Wendlinge7124492011-06-14 03:17:20 +0000802
803 // Don't emit the alias if it has more operands than what it's aliasing.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000804 if (NumResultOps < CountNumOperands(CGA.AsmString, Variant))
Bill Wendlinge7124492011-06-14 03:17:20 +0000805 continue;
806
David Blaikie4ab57cd2015-08-06 19:23:33 +0000807 IAPrinter IAP(CGA.Result->getAsString(), CGA.AsmString);
Bill Wendling7e570b52011-03-21 08:59:17 +0000808
Tim Northover60091cf2014-05-15 13:36:01 +0000809 unsigned NumMIOps = 0;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000810 for (auto &Operand : CGA.ResultOperands)
Tim Northover60091cf2014-05-15 13:36:01 +0000811 NumMIOps += Operand.getMINumOperands();
812
Bill Wendling7e570b52011-03-21 08:59:17 +0000813 std::string Cond;
Tim Northover60091cf2014-05-15 13:36:01 +0000814 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(NumMIOps);
David Blaikie4ab57cd2015-08-06 19:23:33 +0000815 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000816
Bill Wendling7e570b52011-03-21 08:59:17 +0000817 bool CantHandle = false;
818
Tim Northover60091cf2014-05-15 13:36:01 +0000819 unsigned MIOpNum = 0;
Bill Wendling7e570b52011-03-21 08:59:17 +0000820 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000821 std::string Op = "MI->getOperand(" + llvm::utostr(MIOpNum) + ")";
822
David Blaikie4ab57cd2015-08-06 19:23:33 +0000823 const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
Bill Wendling7e570b52011-03-21 08:59:17 +0000824
825 switch (RO.Kind) {
Bill Wendling7e570b52011-03-21 08:59:17 +0000826 case CodeGenInstAlias::ResultOperand::K_Record: {
827 const Record *Rec = RO.getRecord();
828 StringRef ROName = RO.getName();
Tim Northoveree20caa2014-05-12 18:04:06 +0000829 int PrintMethodIdx = -1;
Bill Wendling7e570b52011-03-21 08:59:17 +0000830
Tim Northoveree20caa2014-05-12 18:04:06 +0000831 // These two may have a PrintMethod, which we want to record (if it's
832 // the first time we've seen it) and provide an index for the aliasing
833 // code to use.
834 if (Rec->isSubClassOf("RegisterOperand") ||
835 Rec->isSubClassOf("Operand")) {
836 std::string PrintMethod = Rec->getValueAsString("PrintMethod");
837 if (PrintMethod != "" && PrintMethod != "printOperand") {
838 PrintMethodIdx = std::find(PrintMethods.begin(),
839 PrintMethods.end(), PrintMethod) -
840 PrintMethods.begin();
841 if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
842 PrintMethods.push_back(PrintMethod);
843 }
844 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000845
846 if (Rec->isSubClassOf("RegisterOperand"))
847 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling7e570b52011-03-21 08:59:17 +0000848 if (Rec->isSubClassOf("RegisterClass")) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000849 IAP.addCond(Op + ".isReg()");
Bill Wendling7e570b52011-03-21 08:59:17 +0000850
David Blaikie4ab57cd2015-08-06 19:23:33 +0000851 if (!IAP.isOpMapped(ROName)) {
852 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
853 Record *R = CGA.ResultOperands[i].getRecord();
Jack Carter9c1a0272013-02-05 08:32:10 +0000854 if (R->isSubClassOf("RegisterOperand"))
855 R = R->getValueAsDef("RegClass");
Benjamin Kramer682de392012-03-30 23:13:40 +0000856 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
Tim Northover60091cf2014-05-15 13:36:01 +0000857 R->getName() + "RegClassID)"
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000858 ".contains(" + Op + ".getReg())";
Bill Wendling7e570b52011-03-21 08:59:17 +0000859 } else {
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000860 Cond = Op + ".getReg() == MI->getOperand(" +
David Blaikie4ab57cd2015-08-06 19:23:33 +0000861 llvm::utostr(IAP.getOpIndex(ROName)) + ").getReg()";
Bill Wendling7e570b52011-03-21 08:59:17 +0000862 }
863 } else {
Tim Northoveree20caa2014-05-12 18:04:06 +0000864 // Assume all printable operands are desired for now. This can be
Alp Tokerbeaca192014-05-15 01:52:21 +0000865 // overridden in the InstAlias instantiation if necessary.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000866 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
Bill Wendling7e570b52011-03-21 08:59:17 +0000867
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000868 // There might be an additional predicate on the MCOperand
869 unsigned Entry = MCOpPredicateMap[Rec];
870 if (!Entry) {
871 if (!Rec->isValueUnset("MCOperandPredicate")) {
872 MCOpPredicates.push_back(Rec);
873 Entry = MCOpPredicates.size();
874 MCOpPredicateMap[Rec] = Entry;
875 } else
876 break; // No conditions on this operand at all
877 }
878 Cond = Target.getName() + ClassName + "ValidateMCOperand(" +
Oliver Stannarda34e4702015-12-01 10:48:51 +0000879 Op + ", STI, " + llvm::utostr(Entry) + ")";
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000880 }
881 // for all subcases of ResultOperand::K_Record:
David Blaikie4ab57cd2015-08-06 19:23:33 +0000882 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000883 break;
884 }
Tim Northoverab7689e2013-01-09 13:32:04 +0000885 case CodeGenInstAlias::ResultOperand::K_Imm: {
Tim Northoverab7689e2013-01-09 13:32:04 +0000886 // Just because the alias has an immediate result, doesn't mean the
887 // MCInst will. An MCExpr could be present, for example.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000888 IAP.addCond(Op + ".isImm()");
Tim Northoverab7689e2013-01-09 13:32:04 +0000889
David Blaikie4ab57cd2015-08-06 19:23:33 +0000890 Cond = Op + ".getImm() == " +
891 llvm::utostr(CGA.ResultOperands[i].getImm());
892 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000893 break;
Tim Northoverab7689e2013-01-09 13:32:04 +0000894 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000895 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000896 // If this is zero_reg, something's playing tricks we're not
897 // equipped to handle.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000898 if (!CGA.ResultOperands[i].getRegister()) {
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000899 CantHandle = true;
900 break;
901 }
902
David Blaikie4ab57cd2015-08-06 19:23:33 +0000903 Cond = Op + ".getReg() == " + Target.getName() + "::" +
904 CGA.ResultOperands[i].getRegister()->getName();
905 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000906 break;
907 }
908
Tim Northover60091cf2014-05-15 13:36:01 +0000909 MIOpNum += RO.getMINumOperands();
Bill Wendling7e570b52011-03-21 08:59:17 +0000910 }
911
912 if (CantHandle) continue;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000913 IAPrinterMap[Aliases.first].push_back(std::move(IAP));
Bill Wendling7e570b52011-03-21 08:59:17 +0000914 }
915 }
916
Tim Northoveree20caa2014-05-12 18:04:06 +0000917 //////////////////////////////
918 // Write out the printAliasInstr function
919 //////////////////////////////
920
Bill Wendlingf5199de2011-05-23 00:18:33 +0000921 std::string Header;
922 raw_string_ostream HeaderO(Header);
923
924 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendlinge7124492011-06-14 03:17:20 +0000925 << "::printAliasInstr(const MCInst"
Akira Hatanakab46d0232015-03-27 20:36:02 +0000926 << " *MI, " << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
927 << "raw_ostream &OS) {\n";
Bill Wendling7e570b52011-03-21 08:59:17 +0000928
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000929 std::string Cases;
930 raw_string_ostream CasesO(Cases);
931
David Blaikie4ab57cd2015-08-06 19:23:33 +0000932 for (auto &Entry : IAPrinterMap) {
933 std::vector<IAPrinter> &IAPs = Entry.second;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000934 std::vector<IAPrinter*> UniqueIAPs;
935
David Blaikie4ab57cd2015-08-06 19:23:33 +0000936 for (auto &LHS : IAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000937 bool IsDup = false;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000938 for (const auto &RHS : IAPs) {
939 if (&LHS != &RHS && LHS == RHS) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000940 IsDup = true;
941 break;
942 }
943 }
944
David Blaikie4ab57cd2015-08-06 19:23:33 +0000945 if (!IsDup)
946 UniqueIAPs.push_back(&LHS);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000947 }
948
949 if (UniqueIAPs.empty()) continue;
950
David Blaikie4ab57cd2015-08-06 19:23:33 +0000951 CasesO.indent(2) << "case " << Entry.first << ":\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000952
Craig Topper190ecd52016-01-08 07:06:32 +0000953 for (IAPrinter *IAP : UniqueIAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000954 CasesO.indent(4);
Evan Cheng4d806e22011-07-06 02:02:33 +0000955 IAP->print(CasesO);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000956 CasesO << '\n';
957 }
958
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000959 CasesO.indent(4) << "return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000960 }
961
Bill Wendlinge7124492011-06-14 03:17:20 +0000962 if (CasesO.str().empty()) {
Bill Wendlingf5199de2011-05-23 00:18:33 +0000963 O << HeaderO.str();
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000964 O << " return false;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000965 O << "}\n\n";
966 O << "#endif // PRINT_ALIAS_INSTR\n";
967 return;
968 }
969
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000970 if (!MCOpPredicates.empty())
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000971 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +0000972 << "ValidateMCOperand(const MCOperand &MCOp,\n"
973 << " const MCSubtargetInfo &STI,\n"
974 << " unsigned PredicateIndex);\n";
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000975
Bill Wendlingf5199de2011-05-23 00:18:33 +0000976 O << HeaderO.str();
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000977 O.indent(2) << "const char *AsmString;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000978 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000979 O.indent(2) << "default: return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000980 O << CasesO.str();
981 O.indent(2) << "}\n\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000982
983 // Code that prints the alias, replacing the operands with the ones from the
984 // MCInst.
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000985 O << " unsigned I = 0;\n";
Tim Northoverd8d65a62014-05-15 11:16:32 +0000986 O << " while (AsmString[I] != ' ' && AsmString[I] != '\t' &&\n";
987 O << " AsmString[I] != '\\0')\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000988 O << " ++I;\n";
989 O << " OS << '\\t' << StringRef(AsmString, I);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000990
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000991 O << " if (AsmString[I] != '\\0') {\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000992 O << " OS << '\\t';\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000993 O << " do {\n";
994 O << " if (AsmString[I] == '$') {\n";
995 O << " ++I;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +0000996 O << " if (AsmString[I] == (char)0xff) {\n";
997 O << " ++I;\n";
998 O << " int OpIdx = AsmString[I++] - 1;\n";
999 O << " int PrintMethodIdx = AsmString[I++] - 1;\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001000 O << " printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, ";
1001 O << (PassSubtarget ? "STI, " : "");
1002 O << "OS);\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001003 O << " } else\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001004 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, ";
1005 O << (PassSubtarget ? "STI, " : "");
1006 O << "OS);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001007 O << " } else {\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001008 O << " OS << AsmString[I++];\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001009 O << " }\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001010 O << " } while (AsmString[I] != '\\0');\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001011 O << " }\n\n";
Jim Grosbachf4e67082012-04-18 18:56:33 +00001012
Eric Christopher2e3fbaa2011-04-18 21:28:11 +00001013 O << " return true;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001014 O << "}\n\n";
1015
Tim Northoveree20caa2014-05-12 18:04:06 +00001016 //////////////////////////////
1017 // Write out the printCustomAliasOperand function
1018 //////////////////////////////
1019
1020 O << "void " << Target.getName() << ClassName << "::"
1021 << "printCustomAliasOperand(\n"
1022 << " const MCInst *MI, unsigned OpIdx,\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001023 << " unsigned PrintMethodIdx,\n"
1024 << (PassSubtarget ? " const MCSubtargetInfo &STI,\n" : "")
1025 << " raw_ostream &OS) {\n";
Aaron Ballmane58a5702014-05-13 12:52:35 +00001026 if (PrintMethods.empty())
1027 O << " llvm_unreachable(\"Unknown PrintMethod kind\");\n";
1028 else {
1029 O << " switch (PrintMethodIdx) {\n"
1030 << " default:\n"
1031 << " llvm_unreachable(\"Unknown PrintMethod kind\");\n"
Tim Northoveree20caa2014-05-12 18:04:06 +00001032 << " break;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001033
Aaron Ballmane58a5702014-05-13 12:52:35 +00001034 for (unsigned i = 0; i < PrintMethods.size(); ++i) {
1035 O << " case " << i << ":\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001036 << " " << PrintMethods[i] << "(MI, OpIdx, "
1037 << (PassSubtarget ? "STI, " : "") << "OS);\n"
Aaron Ballmane58a5702014-05-13 12:52:35 +00001038 << " break;\n";
1039 }
1040 O << " }\n";
1041 }
1042 O << "}\n\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001043
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001044 if (!MCOpPredicates.empty()) {
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001045 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +00001046 << "ValidateMCOperand(const MCOperand &MCOp,\n"
1047 << " const MCSubtargetInfo &STI,\n"
1048 << " unsigned PredicateIndex) {\n"
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001049 << " switch (PredicateIndex) {\n"
1050 << " default:\n"
1051 << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
1052 << " break;\n";
1053
1054 for (unsigned i = 0; i < MCOpPredicates.size(); ++i) {
1055 Init *MCOpPred = MCOpPredicates[i]->getValueInit("MCOperandPredicate");
1056 if (StringInit *SI = dyn_cast<StringInit>(MCOpPred)) {
1057 O << " case " << i + 1 << ": {\n"
1058 << SI->getValue() << "\n"
1059 << " }\n";
1060 } else
1061 llvm_unreachable("Unexpected MCOperandPredicate field!");
1062 }
1063 O << " }\n"
1064 << "}\n\n";
1065 }
1066
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001067 O << "#endif // PRINT_ALIAS_INSTR\n";
1068}
Chris Lattner06c5eed2009-09-13 20:08:00 +00001069
Ahmed Bougachabd214002013-10-28 18:07:17 +00001070AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1071 Record *AsmWriter = Target.getAsmWriter();
Craig Topper0bd58742016-01-13 07:20:05 +00001072 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Ahmed Bougachabd214002013-10-28 18:07:17 +00001073
1074 // Get the instruction numbering.
Craig Topperf9265322016-01-17 20:38:14 +00001075 NumberedInstructions = Target.getInstructionsByEnumValue();
Ahmed Bougachabd214002013-10-28 18:07:17 +00001076
Craig Topperf9265322016-01-17 20:38:14 +00001077 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
1078 const CodeGenInstruction *I = NumberedInstructions[i];
Craig Topper9e9ae602016-01-17 08:05:33 +00001079 if (!I->AsmString.empty() && I->TheDef->getName() != "PHI")
1080 Instructions.emplace_back(*I, i, Variant);
1081 }
Ahmed Bougachabd214002013-10-28 18:07:17 +00001082}
1083
Chris Lattner06c5eed2009-09-13 20:08:00 +00001084void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +00001085 EmitPrintInstruction(O);
1086 EmitGetRegisterName(O);
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001087 EmitPrintAliasInstruction(O);
Chris Lattner06c5eed2009-09-13 20:08:00 +00001088}
1089
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001090
1091namespace llvm {
1092
1093void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1094 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1095 AsmWriterEmitter(RK).run(OS);
1096}
1097
1098} // End llvm namespace