blob: 6c8ebfb5d217e9d29c2f796b25b50b71f0a20db5 [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;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000039 std::map<const CodeGenInstruction*, AsmWriterInst*> CGIAWIMap;
Craig Topper4c6129a2014-02-05 07:56:49 +000040 const std::vector<const CodeGenInstruction*> *NumberedInstructions;
Ahmed Bougachabd214002013-10-28 18:07:17 +000041 std::vector<AsmWriterInst> Instructions;
Tim Northoveree20caa2014-05-12 18:04:06 +000042 std::vector<std::string> PrintMethods;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000043public:
Ahmed Bougachabd214002013-10-28 18:07:17 +000044 AsmWriterEmitter(RecordKeeper &R);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000045
46 void run(raw_ostream &o);
47
48private:
49 void EmitPrintInstruction(raw_ostream &o);
50 void EmitGetRegisterName(raw_ostream &o);
51 void EmitPrintAliasInstruction(raw_ostream &O);
52
53 AsmWriterInst *getAsmWriterInstByID(unsigned ID) const {
Craig Topper4c6129a2014-02-05 07:56:49 +000054 assert(ID < NumberedInstructions->size());
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000055 std::map<const CodeGenInstruction*, AsmWriterInst*>::const_iterator I =
Craig Topper4c6129a2014-02-05 07:56:49 +000056 CGIAWIMap.find(NumberedInstructions->at(ID));
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000057 assert(I != CGIAWIMap.end() && "Didn't find inst!");
58 return I->second;
59 }
60 void FindUniqueOperandCommands(std::vector<std::string> &UOC,
61 std::vector<unsigned> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +000062 std::vector<unsigned> &InstOpsUsed,
63 bool PassSubtarget) const;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000064};
65} // end anonymous namespace
66
Chris Lattner59a7f5c2005-01-22 20:31:17 +000067static void PrintCases(std::vector<std::pair<std::string,
Craig Topperc24a4012016-01-14 06:15:07 +000068 AsmWriterOperand> > &OpsToPrint, raw_ostream &O,
69 bool PassSubtarget) {
Craig Topper0b271ad2016-01-13 07:20:13 +000070 O << " case " << OpsToPrint.back().first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000071 AsmWriterOperand TheOp = OpsToPrint.back().second;
72 OpsToPrint.pop_back();
73
74 // Check to see if any other operands are identical in this list, and if so,
75 // emit a case label for them.
76 for (unsigned i = OpsToPrint.size(); i != 0; --i)
77 if (OpsToPrint[i-1].second == TheOp) {
Craig Topper0b271ad2016-01-13 07:20:13 +000078 O << "\n case " << OpsToPrint[i-1].first << ":";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000079 OpsToPrint.erase(OpsToPrint.begin()+i-1);
80 }
81
82 // Finally, emit the code.
Craig Topperc24a4012016-01-14 06:15:07 +000083 O << "\n " << TheOp.getCode(PassSubtarget);
Craig Topper0b271ad2016-01-13 07:20:13 +000084 O << "\n break;\n";
Chris Lattner59a7f5c2005-01-22 20:31:17 +000085}
86
Chris Lattner9ceb7c82005-01-22 18:38:13 +000087
88/// EmitInstructions - Emit the last instruction in the vector and any other
89/// instructions that are suitably similar to it.
90static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Craig Topperc24a4012016-01-14 06:15:07 +000091 raw_ostream &O, bool PassSubtarget) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +000092 AsmWriterInst FirstInst = Insts.back();
93 Insts.pop_back();
94
95 std::vector<AsmWriterInst> SimilarInsts;
96 unsigned DifferingOperand = ~0;
97 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattner92275bb2005-01-22 19:22:23 +000098 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
99 if (DiffOp != ~1U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000100 if (DifferingOperand == ~0U) // First match!
101 DifferingOperand = DiffOp;
102
103 // If this differs in the same operand as the rest of the instructions in
104 // this class, move it to the SimilarInsts list.
Chris Lattner92275bb2005-01-22 19:22:23 +0000105 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000106 SimilarInsts.push_back(Insts[i-1]);
107 Insts.erase(Insts.begin()+i-1);
108 }
109 }
110 }
111
Chris Lattner017b93d2006-05-01 17:01:17 +0000112 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000113 << FirstInst.CGI->TheDef->getName() << ":\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000114 for (const AsmWriterInst &AWI : SimilarInsts)
115 O << " case " << AWI.CGI->Namespace << "::"
116 << AWI.CGI->TheDef->getName() << ":\n";
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000117 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
118 if (i != DifferingOperand) {
119 // If the operand is the same for all instructions, just print it.
Craig Topperc24a4012016-01-14 06:15:07 +0000120 O << " " << FirstInst.Operands[i].getCode(PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000121 } else {
122 // If this is the operand that varies between all of the instructions,
123 // emit a switch for just this operand now.
124 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000125 O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000126 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattner017b93d2006-05-01 17:01:17 +0000127 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000128 FirstInst.CGI->TheDef->getName(),
129 FirstInst.Operands[i]));
Misha Brukman650ba8e2005-04-22 00:00:37 +0000130
Craig Topper190ecd52016-01-08 07:06:32 +0000131 for (const AsmWriterInst &AWI : SimilarInsts) {
Chris Lattner017b93d2006-05-01 17:01:17 +0000132 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000133 AWI.CGI->TheDef->getName(),
134 AWI.Operands[i]));
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000135 }
Chris Lattner59a7f5c2005-01-22 20:31:17 +0000136 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
137 while (!OpsToPrint.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000138 PrintCases(OpsToPrint, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000139 O << " }";
140 }
141 O << "\n";
142 }
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000143 O << " break;\n";
144}
Chris Lattner0c23ba52005-01-22 17:32:42 +0000145
Chris Lattner692374c2006-07-18 17:18:03 +0000146void AsmWriterEmitter::
Jim Grosbacha5497342010-09-29 22:32:50 +0000147FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattneredee5252006-07-18 18:28:27 +0000148 std::vector<unsigned> &InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000149 std::vector<unsigned> &InstOpsUsed,
150 bool PassSubtarget) const {
Craig Topper4c6129a2014-02-05 07:56:49 +0000151 InstIdxs.assign(NumberedInstructions->size(), ~0U);
Jim Grosbacha5497342010-09-29 22:32:50 +0000152
Chris Lattner692374c2006-07-18 17:18:03 +0000153 // This vector parallels UniqueOperandCommands, keeping track of which
154 // instructions each case are used for. It is a comma separated string of
155 // enums.
156 std::vector<std::string> InstrsForCase;
157 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattneredee5252006-07-18 18:28:27 +0000158 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbacha5497342010-09-29 22:32:50 +0000159
Craig Topper4c6129a2014-02-05 07:56:49 +0000160 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
Chris Lattner692374c2006-07-18 17:18:03 +0000161 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Craig Topper24064772014-04-15 07:20:03 +0000162 if (!Inst)
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000163 continue; // PHI, INLINEASM, CFI_INSTRUCTION, etc.
Jim Grosbacha5497342010-09-29 22:32:50 +0000164
Chris Lattnercb0c8482006-07-18 17:56:07 +0000165 if (Inst->Operands.empty())
Chris Lattner692374c2006-07-18 17:18:03 +0000166 continue; // Instruction already done.
Chris Lattner9d250692006-07-18 17:50:22 +0000167
Craig Topperc24a4012016-01-14 06:15:07 +0000168 std::string Command = " "+Inst->Operands[0].getCode(PassSubtarget)+"\n";
Chris Lattner9d250692006-07-18 17:50:22 +0000169
Chris Lattner692374c2006-07-18 17:18:03 +0000170 // Check to see if we already have 'Command' in UniqueOperandCommands.
171 // If not, add it.
172 bool FoundIt = false;
173 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
174 if (UniqueOperandCommands[idx] == Command) {
175 InstIdxs[i] = idx;
176 InstrsForCase[idx] += ", ";
177 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
178 FoundIt = true;
179 break;
180 }
181 if (!FoundIt) {
182 InstIdxs[i] = UniqueOperandCommands.size();
Craig Topper1993e3b2016-01-08 07:06:29 +0000183 UniqueOperandCommands.push_back(std::move(Command));
Chris Lattner692374c2006-07-18 17:18:03 +0000184 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattneredee5252006-07-18 18:28:27 +0000185
186 // This command matches one operand so far.
187 InstOpsUsed.push_back(1);
188 }
189 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000190
Chris Lattneredee5252006-07-18 18:28:27 +0000191 // For each entry of UniqueOperandCommands, there is a set of instructions
192 // that uses it. If the next command of all instructions in the set are
193 // identical, fold it into the command.
194 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
195 CommandIdx != e; ++CommandIdx) {
Jim Grosbacha5497342010-09-29 22:32:50 +0000196
Chris Lattneredee5252006-07-18 18:28:27 +0000197 for (unsigned Op = 1; ; ++Op) {
198 // Scan for the first instruction in the set.
199 std::vector<unsigned>::iterator NIT =
200 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
201 if (NIT == InstIdxs.end()) break; // No commonality.
202
203 // If this instruction has no more operands, we isn't anything to merge
204 // into this command.
Jim Grosbacha5497342010-09-29 22:32:50 +0000205 const AsmWriterInst *FirstInst =
Chris Lattneredee5252006-07-18 18:28:27 +0000206 getAsmWriterInstByID(NIT-InstIdxs.begin());
207 if (!FirstInst || FirstInst->Operands.size() == Op)
208 break;
209
210 // Otherwise, scan to see if all of the other instructions in this command
211 // set share the operand.
212 bool AllSame = true;
David Greene5b4bc262009-07-29 20:10:24 +0000213
Chris Lattneredee5252006-07-18 18:28:27 +0000214 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
215 NIT != InstIdxs.end();
216 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
217 // Okay, found another instruction in this command set. If the operand
218 // matches, we're ok, otherwise bail out.
Jim Grosbacha5497342010-09-29 22:32:50 +0000219 const AsmWriterInst *OtherInst =
Chris Lattneredee5252006-07-18 18:28:27 +0000220 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greene5b4bc262009-07-29 20:10:24 +0000221
Chris Lattneredee5252006-07-18 18:28:27 +0000222 if (!OtherInst || OtherInst->Operands.size() == Op ||
223 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
224 AllSame = false;
225 break;
226 }
227 }
228 if (!AllSame) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000229
Chris Lattneredee5252006-07-18 18:28:27 +0000230 // Okay, everything in this command set has the same next operand. Add it
231 // to UniqueOperandCommands and remember that it was consumed.
Craig Topperc24a4012016-01-14 06:15:07 +0000232 std::string Command = " " +
233 FirstInst->Operands[Op].getCode(PassSubtarget) + "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000234
Chris Lattneredee5252006-07-18 18:28:27 +0000235 UniqueOperandCommands[CommandIdx] += Command;
236 InstOpsUsed[CommandIdx]++;
Chris Lattner692374c2006-07-18 17:18:03 +0000237 }
238 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000239
Chris Lattner692374c2006-07-18 17:18:03 +0000240 // Prepend some of the instructions each case is used for onto the case val.
241 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
242 std::string Instrs = InstrsForCase[i];
243 if (Instrs.size() > 70) {
244 Instrs.erase(Instrs.begin()+70, Instrs.end());
245 Instrs += "...";
246 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000247
Chris Lattner692374c2006-07-18 17:18:03 +0000248 if (!Instrs.empty())
Jim Grosbacha5497342010-09-29 22:32:50 +0000249 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattner692374c2006-07-18 17:18:03 +0000250 UniqueOperandCommands[i];
251 }
252}
253
254
Daniel Dunbar04f049f2009-10-17 20:43:42 +0000255static void UnescapeString(std::string &Str) {
256 for (unsigned i = 0; i != Str.size(); ++i) {
257 if (Str[i] == '\\' && i != Str.size()-1) {
258 switch (Str[i+1]) {
259 default: continue; // Don't execute the code after the switch.
260 case 'a': Str[i] = '\a'; break;
261 case 'b': Str[i] = '\b'; break;
262 case 'e': Str[i] = 27; break;
263 case 'f': Str[i] = '\f'; break;
264 case 'n': Str[i] = '\n'; break;
265 case 'r': Str[i] = '\r'; break;
266 case 't': Str[i] = '\t'; break;
267 case 'v': Str[i] = '\v'; break;
268 case '"': Str[i] = '\"'; break;
269 case '\'': Str[i] = '\''; break;
270 case '\\': Str[i] = '\\'; break;
271 }
272 // Nuke the second character.
273 Str.erase(Str.begin()+i+1);
274 }
275 }
276}
277
Chris Lattner06c5eed2009-09-13 20:08:00 +0000278/// EmitPrintInstruction - Generate the code for the "printInstruction" method
Ahmed Bougachabd214002013-10-28 18:07:17 +0000279/// implementation. Destroys all instances of AsmWriterInst information, by
280/// clearing the Instructions vector.
Chris Lattner06c5eed2009-09-13 20:08:00 +0000281void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner6ffa5012004-08-14 22:50:53 +0000282 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner72770f52004-10-03 20:19:02 +0000283 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Craig Topperc24a4012016-01-14 06:15:07 +0000284 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
Jim Grosbacha5497342010-09-29 22:32:50 +0000285
Chris Lattner1c4ae852004-08-01 05:59:33 +0000286 O <<
287 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner06c5eed2009-09-13 20:08:00 +0000288 "/// from the instruction set description.\n"
Chris Lattnerb94284b2009-08-08 01:32:19 +0000289 "void " << Target.getName() << ClassName
Akira Hatanakab46d0232015-03-27 20:36:02 +0000290 << "::printInstruction(const MCInst *MI, "
291 << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
292 << "raw_ostream &O) {\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000293
Chris Lattnere32982c2006-07-14 22:59:11 +0000294 // Build an aggregate string, and build a table of offsets into it.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000295 SequenceToOffsetTable<std::string> StringTable;
Jim Grosbacha5497342010-09-29 22:32:50 +0000296
Chris Lattner5d751b42006-09-27 16:44:09 +0000297 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000298 /// chunk of the output as well as indices used for operand printing.
Craig Topper06cec4c2012-09-14 08:33:11 +0000299 std::vector<uint64_t> OpcodeInfo;
Craig Topperd4f87a32016-01-13 07:20:12 +0000300 const unsigned OpcodeInfoBits = 64;
Jim Grosbacha5497342010-09-29 22:32:50 +0000301
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000302 // Add all strings to the string table upfront so it can generate an optimized
303 // representation.
Craig Topper190ecd52016-01-08 07:06:32 +0000304 for (const CodeGenInstruction *Inst : *NumberedInstructions) {
305 AsmWriterInst *AWI = CGIAWIMap[Inst];
Craig Topper24064772014-04-15 07:20:03 +0000306 if (AWI &&
Jim Grosbachf4e67082012-04-18 18:56:33 +0000307 AWI->Operands[0].OperandType ==
308 AsmWriterOperand::isLiteralTextOperand &&
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000309 !AWI->Operands[0].Str.empty()) {
310 std::string Str = AWI->Operands[0].Str;
311 UnescapeString(Str);
312 StringTable.add(Str);
313 }
314 }
315
316 StringTable.layout();
317
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000318 unsigned MaxStringIdx = 0;
Craig Topper190ecd52016-01-08 07:06:32 +0000319 for (const CodeGenInstruction *Inst : *NumberedInstructions) {
320 AsmWriterInst *AWI = CGIAWIMap[Inst];
Chris Lattnere32982c2006-07-14 22:59:11 +0000321 unsigned Idx;
Craig Topper24064772014-04-15 07:20:03 +0000322 if (!AWI) {
Chris Lattnere32982c2006-07-14 22:59:11 +0000323 // Something not handled by the asmwriter printer.
Chris Lattnerb47ed612009-09-14 01:16:36 +0000324 Idx = ~0U;
Jim Grosbacha5497342010-09-29 22:32:50 +0000325 } else if (AWI->Operands[0].OperandType !=
Chris Lattner36504652006-07-19 01:39:06 +0000326 AsmWriterOperand::isLiteralTextOperand ||
327 AWI->Operands[0].Str.empty()) {
328 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000329 Idx = StringTable.get("");
Chris Lattnere32982c2006-07-14 22:59:11 +0000330 } else {
Chris Lattnerb47ed612009-09-14 01:16:36 +0000331 std::string Str = AWI->Operands[0].Str;
332 UnescapeString(Str);
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000333 Idx = StringTable.get(Str);
Chris Lattnerb47ed612009-09-14 01:16:36 +0000334 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbacha5497342010-09-29 22:32:50 +0000335
Chris Lattnere32982c2006-07-14 22:59:11 +0000336 // Nuke the string from the operand list. It is now handled!
337 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattner92275bb2005-01-22 19:22:23 +0000338 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000339
Chris Lattnerb47ed612009-09-14 01:16:36 +0000340 // Bias offset by one since we want 0 as a sentinel.
Craig Topper06cec4c2012-09-14 08:33:11 +0000341 OpcodeInfo.push_back(Idx+1);
Chris Lattner92275bb2005-01-22 19:22:23 +0000342 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000343
Chris Lattner1ac0eb72006-07-18 17:32:27 +0000344 // Figure out how many bits we used for the string index.
Chris Lattnerb47ed612009-09-14 01:16:36 +0000345 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbacha5497342010-09-29 22:32:50 +0000346
Chris Lattner692374c2006-07-18 17:18:03 +0000347 // To reduce code size, we compactify common instructions into a few bits
348 // in the opcode-indexed table.
Craig Topperd4f87a32016-01-13 07:20:12 +0000349 unsigned BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000350
Craig Topper1f387362014-11-25 20:11:25 +0000351 std::vector<std::vector<std::string>> TableDrivenOperandPrinters;
Jim Grosbacha5497342010-09-29 22:32:50 +0000352
Chris Lattnercb0c8482006-07-18 17:56:07 +0000353 while (1) {
Chris Lattner692374c2006-07-18 17:18:03 +0000354 std::vector<std::string> UniqueOperandCommands;
Chris Lattner692374c2006-07-18 17:18:03 +0000355 std::vector<unsigned> InstIdxs;
Chris Lattneredee5252006-07-18 18:28:27 +0000356 std::vector<unsigned> NumInstOpsHandled;
357 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
Craig Topperc24a4012016-01-14 06:15:07 +0000358 NumInstOpsHandled, PassSubtarget);
Jim Grosbacha5497342010-09-29 22:32:50 +0000359
Chris Lattner692374c2006-07-18 17:18:03 +0000360 // If we ran out of operands to print, we're done.
361 if (UniqueOperandCommands.empty()) break;
Jim Grosbacha5497342010-09-29 22:32:50 +0000362
Chris Lattner692374c2006-07-18 17:18:03 +0000363 // Compute the number of bits we need to represent these cases, this is
364 // ceil(log2(numentries)).
365 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbacha5497342010-09-29 22:32:50 +0000366
Chris Lattner692374c2006-07-18 17:18:03 +0000367 // If we don't have enough bits for this operand, don't include it.
368 if (NumBits > BitsLeft) {
Chris Lattner34822f62009-08-23 04:44:11 +0000369 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
370 << " more bits\n");
Chris Lattner692374c2006-07-18 17:18:03 +0000371 break;
372 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000373
Chris Lattner692374c2006-07-18 17:18:03 +0000374 // Otherwise, we can include this in the initial lookup table. Add it in.
Chris Lattner692374c2006-07-18 17:18:03 +0000375 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Manman Ren68cf9fc2012-09-13 17:43:46 +0000376 if (InstIdxs[i] != ~0U) {
Craig Topperd4f87a32016-01-13 07:20:12 +0000377 OpcodeInfo[i] |= (uint64_t)InstIdxs[i] << (OpcodeInfoBits-BitsLeft);
Manman Ren68cf9fc2012-09-13 17:43:46 +0000378 }
Craig Topper06cec4c2012-09-14 08:33:11 +0000379 BitsLeft -= NumBits;
Jim Grosbacha5497342010-09-29 22:32:50 +0000380
Chris Lattnercb0c8482006-07-18 17:56:07 +0000381 // Remove the info about this operand.
Craig Topper4c6129a2014-02-05 07:56:49 +0000382 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
Chris Lattnercb0c8482006-07-18 17:56:07 +0000383 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattneredee5252006-07-18 18:28:27 +0000384 if (!Inst->Operands.empty()) {
385 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner6e172082006-07-18 19:06:01 +0000386 assert(NumOps <= Inst->Operands.size() &&
387 "Can't remove this many ops!");
Chris Lattneredee5252006-07-18 18:28:27 +0000388 Inst->Operands.erase(Inst->Operands.begin(),
389 Inst->Operands.begin()+NumOps);
390 }
Chris Lattnercb0c8482006-07-18 17:56:07 +0000391 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000392
Chris Lattnercb0c8482006-07-18 17:56:07 +0000393 // Remember the handlers for this set of operands.
Craig Topper1f387362014-11-25 20:11:25 +0000394 TableDrivenOperandPrinters.push_back(std::move(UniqueOperandCommands));
Chris Lattner692374c2006-07-18 17:18:03 +0000395 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000396
Craig Topper14d91732016-01-11 05:13:41 +0000397 // Emit the string table itself.
Reid Kleckner132c40f2014-07-17 19:43:40 +0000398 O << " static const char AsmStrs[] = {\n";
Benjamin Kramer22d093e2012-04-02 09:13:46 +0000399 StringTable.emit(O, printChar);
400 O << " };\n\n";
Chris Lattnere32982c2006-07-14 22:59:11 +0000401
Craig Topper14d91732016-01-11 05:13:41 +0000402 // Emit the lookup tables in pieces to minimize wasted bytes.
Craig Topperd4f87a32016-01-13 07:20:12 +0000403 unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
Craig Topper14d91732016-01-11 05:13:41 +0000404 unsigned Table = 0, Shift = 0;
405 SmallString<128> BitsString;
406 raw_svector_ostream BitsOS(BitsString);
407 // If the total bits is more than 32-bits we need to use a 64-bit type.
Craig Topperd4f87a32016-01-13 07:20:12 +0000408 BitsOS << " uint" << ((BitsLeft < (OpcodeInfoBits - 32)) ? 64 : 32)
409 << "_t Bits = 0;\n";
Craig Topper14d91732016-01-11 05:13:41 +0000410 while (BytesNeeded != 0) {
411 // Figure out how big this table section needs to be, but no bigger than 4.
412 unsigned TableSize = std::min(1 << Log2_32(BytesNeeded), 4);
413 BytesNeeded -= TableSize;
414 TableSize *= 8; // Convert to bits;
415 uint64_t Mask = (1ULL << TableSize) - 1;
416 O << " static const uint" << TableSize << "_t OpInfo" << Table
417 << "[] = {\n";
418 for (unsigned i = 0, e = NumberedInstructions->size(); i != e; ++i) {
419 O << " " << ((OpcodeInfo[i] >> Shift) & Mask) << "U,\t// "
420 << NumberedInstructions->at(i)->TheDef->getName() << "\n";
421 }
422 O << " };\n\n";
423 // Emit string to combine the individual table lookups.
424 BitsOS << " Bits |= ";
425 // If the total bits is more than 32-bits we need to use a 64-bit type.
Craig Topperd4f87a32016-01-13 07:20:12 +0000426 if (BitsLeft < (OpcodeInfoBits - 32))
Craig Topper14d91732016-01-11 05:13:41 +0000427 BitsOS << "(uint64_t)";
428 BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
429 // Prepare the shift for the next iteration and increment the table count.
430 Shift += TableSize;
431 ++Table;
432 }
433
434 // Emit the initial tab character.
Evan Cheng32e53472008-02-02 08:39:46 +0000435 O << " O << \"\\t\";\n\n";
436
Craig Topper06cec4c2012-09-14 08:33:11 +0000437 O << " // Emit the opcode for the instruction.\n";
Craig Topper14d91732016-01-11 05:13:41 +0000438 O << BitsString;
439
440 // Emit the starting string.
Manman Ren68cf9fc2012-09-13 17:43:46 +0000441 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattnerb47ed612009-09-14 01:16:36 +0000442 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenefdd25192009-08-05 21:00:52 +0000443
Chris Lattner692374c2006-07-18 17:18:03 +0000444 // Output the table driven operand information.
Craig Topperd4f87a32016-01-13 07:20:12 +0000445 BitsLeft = OpcodeInfoBits-AsmStrBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000446 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
447 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
448
449 // Compute the number of bits we need to represent these cases, this is
450 // ceil(log2(numentries)).
451 unsigned NumBits = Log2_32_Ceil(Commands.size());
452 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbacha5497342010-09-29 22:32:50 +0000453
Chris Lattner692374c2006-07-18 17:18:03 +0000454 // Emit code to extract this field from Bits.
Chris Lattner692374c2006-07-18 17:18:03 +0000455 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000456 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000457
Chris Lattneredee5252006-07-18 18:28:27 +0000458 if (Commands.size() == 2) {
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000459 // Emit two possibilitys with if/else.
Craig Topper06cec4c2012-09-14 08:33:11 +0000460 O << " if ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000461 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000462 << ((1 << NumBits)-1) << ") {\n"
463 << Commands[1]
464 << " } else {\n"
465 << Commands[0]
466 << " }\n\n";
Eric Christophera573d192010-09-18 18:50:27 +0000467 } else if (Commands.size() == 1) {
468 // Emit a single possibility.
469 O << Commands[0] << "\n\n";
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000470 } else {
Craig Topper06cec4c2012-09-14 08:33:11 +0000471 O << " switch ((Bits >> "
Craig Topperd4f87a32016-01-13 07:20:12 +0000472 << (OpcodeInfoBits-BitsLeft) << ") & "
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000473 << ((1 << NumBits)-1) << ") {\n"
Craig Toppera4ff6ae2014-11-24 14:09:52 +0000474 << " default: llvm_unreachable(\"Invalid command number.\");\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000475
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000476 // Print out all the cases.
Craig Topper190ecd52016-01-08 07:06:32 +0000477 for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
478 O << " case " << j << ":\n";
479 O << Commands[j];
Chris Lattner75dcf6c2006-07-18 17:43:54 +0000480 O << " break;\n";
481 }
482 O << " }\n\n";
Chris Lattner692374c2006-07-18 17:18:03 +0000483 }
Craig Topper06cec4c2012-09-14 08:33:11 +0000484 BitsLeft -= NumBits;
Chris Lattner692374c2006-07-18 17:18:03 +0000485 }
Jim Grosbacha5497342010-09-29 22:32:50 +0000486
Chris Lattnercb0c8482006-07-18 17:56:07 +0000487 // Okay, delete instructions with no operand info left.
Craig Topper4f1f1152016-01-13 07:20:10 +0000488 auto I = std::remove_if(Instructions.begin(), Instructions.end(),
489 [](AsmWriterInst &Inst) {
490 return Inst.Operands.empty();
491 });
492 Instructions.erase(I, Instructions.end());
Chris Lattner692374c2006-07-18 17:18:03 +0000493
Jim Grosbacha5497342010-09-29 22:32:50 +0000494
Chris Lattner692374c2006-07-18 17:18:03 +0000495 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000496 // elements in the vector.
497 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbacha5497342010-09-29 22:32:50 +0000498
499
Craig Topperdf390602016-01-13 07:20:07 +0000500 // Now that we've emitted all of the operand info that fit into 64 bits, emit
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000501 // information for those instructions that are left. This is a less dense
Craig Topperdf390602016-01-13 07:20:07 +0000502 // encoding, but we expect the main 64-bit table to handle the majority of
Chris Lattnerbf1a7692009-09-18 18:10:19 +0000503 // instructions.
Chris Lattner66e288b2006-07-18 17:38:46 +0000504 if (!Instructions.empty()) {
505 // Find the opcode # of inline asm.
506 O << " switch (MI->getOpcode()) {\n";
Craig Topper0b271ad2016-01-13 07:20:13 +0000507 O << " default: llvm_unreachable(\"Unexpected opcode.\");\n";
Chris Lattner66e288b2006-07-18 17:38:46 +0000508 while (!Instructions.empty())
Craig Topperc24a4012016-01-14 06:15:07 +0000509 EmitInstructions(Instructions, O, PassSubtarget);
Chris Lattner9ceb7c82005-01-22 18:38:13 +0000510
Chris Lattner66e288b2006-07-18 17:38:46 +0000511 O << " }\n";
512 }
David Greene5b4bc262009-07-29 20:10:24 +0000513
Chris Lattner6e172082006-07-18 19:06:01 +0000514 O << "}\n";
Chris Lattner1c4ae852004-08-01 05:59:33 +0000515}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000516
Craig Topperba6d83e2014-11-24 02:08:35 +0000517static const char *getMinimalTypeForRange(uint64_t Range) {
518 assert(Range < 0xFFFFFFFFULL && "Enum too large");
519 if (Range > 0xFFFF)
520 return "uint32_t";
521 if (Range > 0xFF)
522 return "uint16_t";
523 return "uint8_t";
524}
525
Owen Andersona84be6c2011-06-27 21:06:21 +0000526static void
527emitRegisterNameString(raw_ostream &O, StringRef AltName,
David Blaikie9b613db2014-11-29 18:13:39 +0000528 const std::deque<CodeGenRegister> &Registers) {
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000529 SequenceToOffsetTable<std::string> StringTable;
530 SmallVector<std::string, 4> AsmNames(Registers.size());
David Blaikie9b613db2014-11-29 18:13:39 +0000531 unsigned i = 0;
532 for (const auto &Reg : Registers) {
533 std::string &AsmName = AsmNames[i++];
Owen Andersona84be6c2011-06-27 21:06:21 +0000534
Owen Andersona84be6c2011-06-27 21:06:21 +0000535 // "NoRegAltName" is special. We don't need to do a lookup for that,
536 // as it's just a reference to the default register name.
537 if (AltName == "" || AltName == "NoRegAltName") {
538 AsmName = Reg.TheDef->getValueAsString("AsmName");
539 if (AsmName.empty())
540 AsmName = Reg.getName();
541 } else {
542 // Make sure the register has an alternate name for this index.
543 std::vector<Record*> AltNameList =
544 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
545 unsigned Idx = 0, e;
546 for (e = AltNameList.size();
547 Idx < e && (AltNameList[Idx]->getName() != AltName);
548 ++Idx)
549 ;
550 // If the register has an alternate name for this index, use it.
551 // Otherwise, leave it empty as an error flag.
552 if (Idx < e) {
553 std::vector<std::string> AltNames =
554 Reg.TheDef->getValueAsListOfStrings("AltNames");
555 if (AltNames.size() <= Idx)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000556 PrintFatalError(Reg.TheDef->getLoc(),
Benjamin Kramer48e7e852014-03-29 17:17:15 +0000557 "Register definition missing alt name for '" +
558 AltName + "'.");
Owen Andersona84be6c2011-06-27 21:06:21 +0000559 AsmName = AltNames[Idx];
560 }
561 }
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000562 StringTable.add(AsmName);
563 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000564
Craig Topperf8f0a232012-09-15 01:22:42 +0000565 StringTable.layout();
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000566 O << " static const char AsmStrs" << AltName << "[] = {\n";
567 StringTable.emit(O, printChar);
568 O << " };\n\n";
569
Craig Topperba6d83e2014-11-24 02:08:35 +0000570 O << " static const " << getMinimalTypeForRange(StringTable.size()-1)
571 << " RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesen892f4802012-03-30 21:12:52 +0000572 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Topper7a2cea12012-04-02 00:47:39 +0000573 if ((i % 14) == 0)
574 O << "\n ";
575 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersona84be6c2011-06-27 21:06:21 +0000576 }
Craig Topper9c252eb2012-04-03 06:52:47 +0000577 O << "\n };\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000578 << "\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000579}
Chris Lattner06c5eed2009-09-13 20:08:00 +0000580
581void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +0000582 Record *AsmWriter = Target.getAsmWriter();
583 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
David Blaikie9b613db2014-11-29 18:13:39 +0000584 const auto &Registers = Target.getRegBank().getRegisters();
Owen Andersona84be6c2011-06-27 21:06:21 +0000585 std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices();
586 bool hasAltNames = AltNameIndices.size() > 1;
Hal Finkelcd5f9842015-12-11 17:31:27 +0000587 std::string Namespace =
588 Registers.front().TheDef->getValueAsString("Namespace");
Jim Grosbacha5497342010-09-29 22:32:50 +0000589
Chris Lattner06c5eed2009-09-13 20:08:00 +0000590 O <<
591 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
592 "/// from the register set description. This returns the assembler name\n"
593 "/// for the specified register.\n"
Owen Andersona84be6c2011-06-27 21:06:21 +0000594 "const char *" << Target.getName() << ClassName << "::";
595 if (hasAltNames)
596 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
597 else
598 O << "getRegisterName(unsigned RegNo) {\n";
599 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
600 << " && \"Invalid register number!\");\n"
Chris Lattnera7e8ae42009-09-14 01:26:18 +0000601 << "\n";
Jim Grosbacha5497342010-09-29 22:32:50 +0000602
Owen Andersona84be6c2011-06-27 21:06:21 +0000603 if (hasAltNames) {
Craig Topper190ecd52016-01-08 07:06:32 +0000604 for (const Record *R : AltNameIndices)
605 emitRegisterNameString(O, R->getName(), Registers);
Owen Andersona84be6c2011-06-27 21:06:21 +0000606 } else
607 emitRegisterNameString(O, "", Registers);
Jim Grosbacha5497342010-09-29 22:32:50 +0000608
Owen Andersona84be6c2011-06-27 21:06:21 +0000609 if (hasAltNames) {
Craig Topperba6d83e2014-11-24 02:08:35 +0000610 O << " switch(AltIdx) {\n"
Craig Topperc4965bc2012-02-05 07:21:30 +0000611 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Craig Topper190ecd52016-01-08 07:06:32 +0000612 for (const Record *R : AltNameIndices) {
613 std::string AltName(R->getName());
Hal Finkelcd5f9842015-12-11 17:31:27 +0000614 std::string Prefix = !Namespace.empty() ? Namespace + "::" : "";
615 O << " case " << Prefix << AltName << ":\n"
Craig Topperba6d83e2014-11-24 02:08:35 +0000616 << " assert(*(AsmStrs" << AltName << "+RegAsmOffset"
617 << AltName << "[RegNo-1]) &&\n"
618 << " \"Invalid alt name index for register!\");\n"
619 << " return AsmStrs" << AltName << "+RegAsmOffset"
620 << AltName << "[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000621 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000622 O << " }\n";
623 } else {
624 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
625 << " \"Invalid alt name index for register!\");\n"
626 << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
Owen Andersona84be6c2011-06-27 21:06:21 +0000627 }
Craig Topperba6d83e2014-11-24 02:08:35 +0000628 O << "}\n";
Chris Lattner06c5eed2009-09-13 20:08:00 +0000629}
630
Bill Wendling7e5771d2011-03-21 08:31:53 +0000631namespace {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000632// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
633// they both have the same conditionals. In which case, we cannot print out the
634// alias for that pattern.
635class IAPrinter {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000636 std::vector<std::string> Conds;
Tim Northoveree20caa2014-05-12 18:04:06 +0000637 std::map<StringRef, std::pair<int, int>> OpMap;
638 SmallVector<Record*, 4> ReqFeatures;
639
Bill Wendling5d3174c2011-03-21 08:40:31 +0000640 std::string Result;
641 std::string AsmString;
Bill Wendling5d3174c2011-03-21 08:40:31 +0000642public:
Tim Northoveree20caa2014-05-12 18:04:06 +0000643 IAPrinter(std::string R, std::string AS) : Result(R), AsmString(AS) {}
Bill Wendling5d3174c2011-03-21 08:40:31 +0000644
645 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000646
Tim Northoveree20caa2014-05-12 18:04:06 +0000647 void addOperand(StringRef Op, int OpIdx, int PrintMethodIdx = -1) {
648 assert(OpIdx >= 0 && OpIdx < 0xFE && "Idx out of range");
Tim Northover0ee9e7e2014-05-13 09:37:41 +0000649 assert(PrintMethodIdx >= -1 && PrintMethodIdx < 0xFF &&
Jay Foadb3590512014-05-13 08:26:53 +0000650 "Idx out of range");
Tim Northoveree20caa2014-05-12 18:04:06 +0000651 OpMap[Op] = std::make_pair(OpIdx, PrintMethodIdx);
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000652 }
Tim Northoveree20caa2014-05-12 18:04:06 +0000653
Bill Wendling5d3174c2011-03-21 08:40:31 +0000654 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
Tim Northoveree20caa2014-05-12 18:04:06 +0000655 int getOpIndex(StringRef Op) { return OpMap[Op].first; }
656 std::pair<int, int> &getOpData(StringRef Op) { return OpMap[Op]; }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000657
Tim Northoverd8d65a62014-05-15 11:16:32 +0000658 std::pair<StringRef, StringRef::iterator> parseName(StringRef::iterator Start,
659 StringRef::iterator End) {
660 StringRef::iterator I = Start;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000661 StringRef::iterator Next;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000662 if (*I == '{') {
663 // ${some_name}
664 Start = ++I;
665 while (I != End && *I != '}')
666 ++I;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000667 Next = I;
668 // eat the final '}'
669 if (Next != End)
670 ++Next;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000671 } else {
672 // $name, just eat the usual suspects.
673 while (I != End &&
674 ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') ||
675 (*I >= '0' && *I <= '9') || *I == '_'))
676 ++I;
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000677 Next = I;
Tim Northoverd8d65a62014-05-15 11:16:32 +0000678 }
679
Evgeny Astigeevichb42003d2014-12-16 18:16:17 +0000680 return std::make_pair(StringRef(Start, I - Start), Next);
Tim Northoverd8d65a62014-05-15 11:16:32 +0000681 }
682
Evan Cheng4d806e22011-07-06 02:02:33 +0000683 void print(raw_ostream &O) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000684 if (Conds.empty() && ReqFeatures.empty()) {
685 O.indent(6) << "return true;\n";
Evan Cheng4d806e22011-07-06 02:02:33 +0000686 return;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000687 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000688
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000689 O << "if (";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000690
691 for (std::vector<std::string>::iterator
692 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
693 if (I != Conds.begin()) {
694 O << " &&\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000695 O.indent(8);
Bill Wendling5d3174c2011-03-21 08:40:31 +0000696 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000697
Bill Wendling5d3174c2011-03-21 08:40:31 +0000698 O << *I;
699 }
700
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000701 O << ") {\n";
702 O.indent(6) << "// " << Result << "\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000703
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000704 // Directly mangle mapped operands into the string. Each operand is
705 // identified by a '$' sign followed by a byte identifying the number of the
706 // operand. We add one to the index to avoid zero bytes.
Tim Northoverd8d65a62014-05-15 11:16:32 +0000707 StringRef ASM(AsmString);
708 SmallString<128> OutString;
709 raw_svector_ostream OS(OutString);
710 for (StringRef::iterator I = ASM.begin(), E = ASM.end(); I != E;) {
711 OS << *I;
712 if (*I == '$') {
713 StringRef Name;
714 std::tie(Name, I) = parseName(++I, E);
715 assert(isOpMapped(Name) && "Unmapped operand!");
Tim Northoveree20caa2014-05-12 18:04:06 +0000716
Tim Northoverd8d65a62014-05-15 11:16:32 +0000717 int OpIndex, PrintIndex;
718 std::tie(OpIndex, PrintIndex) = getOpData(Name);
719 if (PrintIndex == -1) {
720 // Can use the default printOperand route.
721 OS << format("\\x%02X", (unsigned char)OpIndex + 1);
722 } else
723 // 3 bytes if a PrintMethod is needed: 0xFF, the MCInst operand
724 // number, and which of our pre-detected Methods to call.
725 OS << format("\\xFF\\x%02X\\x%02X", OpIndex + 1, PrintIndex + 1);
726 } else {
727 ++I;
Benjamin Kramer17c17bc2013-09-11 15:42:16 +0000728 }
729 }
730
731 // Emit the string.
Yaron Keren09fb7c62015-03-10 07:33:23 +0000732 O.indent(6) << "AsmString = \"" << OutString << "\";\n";
Bill Wendling5d3174c2011-03-21 08:40:31 +0000733
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000734 O.indent(6) << "break;\n";
735 O.indent(4) << '}';
Bill Wendling5d3174c2011-03-21 08:40:31 +0000736 }
737
David Blaikie4ab57cd2015-08-06 19:23:33 +0000738 bool operator==(const IAPrinter &RHS) const {
Bill Wendling5d3174c2011-03-21 08:40:31 +0000739 if (Conds.size() != RHS.Conds.size())
740 return false;
741
742 unsigned Idx = 0;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000743 for (const auto &str : Conds)
744 if (str != RHS.Conds[Idx++])
Bill Wendling5d3174c2011-03-21 08:40:31 +0000745 return false;
746
747 return true;
748 }
Bill Wendling5d3174c2011-03-21 08:40:31 +0000749};
750
Bill Wendling7e5771d2011-03-21 08:31:53 +0000751} // end anonymous namespace
752
Tim Northover5896b062014-05-16 09:42:04 +0000753static unsigned CountNumOperands(StringRef AsmString, unsigned Variant) {
754 std::string FlatAsmString =
755 CodeGenInstruction::FlattenAsmStringVariants(AsmString, Variant);
756 AsmString = FlatAsmString;
Bill Wendlinge7124492011-06-14 03:17:20 +0000757
Tim Northover5896b062014-05-16 09:42:04 +0000758 return AsmString.count(' ') + AsmString.count('\t');
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000759}
Bill Wendlinge7124492011-06-14 03:17:20 +0000760
Tim Northover9a24f882014-05-20 09:17:16 +0000761namespace {
762struct AliasPriorityComparator {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000763 typedef std::pair<CodeGenInstAlias, int> ValueType;
Tim Northover9a24f882014-05-20 09:17:16 +0000764 bool operator()(const ValueType &LHS, const ValueType &RHS) {
765 if (LHS.second == RHS.second) {
766 // We don't actually care about the order, but for consistency it
767 // shouldn't depend on pointer comparisons.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000768 return LHS.first.TheDef->getName() < RHS.first.TheDef->getName();
Tim Northover9a24f882014-05-20 09:17:16 +0000769 }
770
771 // Aliases with larger priorities should be considered first.
772 return LHS.second > RHS.second;
773 }
774};
775}
776
777
Bill Wendling7e5771d2011-03-21 08:31:53 +0000778void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
Bill Wendling7e5771d2011-03-21 08:31:53 +0000779 Record *AsmWriter = Target.getAsmWriter();
780
781 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
782 O << "#undef PRINT_ALIAS_INSTR\n\n";
783
Tim Northoveree20caa2014-05-12 18:04:06 +0000784 //////////////////////////////
785 // Gather information about aliases we need to print
786 //////////////////////////////
787
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000788 // Emit the method that prints the alias instruction.
789 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Tim Northover9a24f882014-05-20 09:17:16 +0000790 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Craig Topperc24a4012016-01-14 06:15:07 +0000791 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000792
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000793 std::vector<Record*> AllInstAliases =
794 Records.getAllDerivedDefinitions("InstAlias");
795
796 // Create a map from the qualified name to a list of potential matches.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000797 typedef std::set<std::pair<CodeGenInstAlias, int>, AliasPriorityComparator>
Tim Northover9a24f882014-05-20 09:17:16 +0000798 AliasWithPriority;
799 std::map<std::string, AliasWithPriority> AliasMap;
Craig Topper190ecd52016-01-08 07:06:32 +0000800 for (Record *R : AllInstAliases) {
Tim Northover9a24f882014-05-20 09:17:16 +0000801 int Priority = R->getValueAsInt("EmitPriority");
802 if (Priority < 1)
803 continue; // Aliases with priority 0 are never emitted.
804
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000805 const DagInit *DI = R->getValueAsDag("ResultInst");
Sean Silva88eb8dd2012-10-10 20:24:47 +0000806 const DefInit *Op = cast<DefInit>(DI->getOperator());
David Blaikie4ab57cd2015-08-06 19:23:33 +0000807 AliasMap[getQualifiedName(Op->getDef())].insert(
Craig Topper190ecd52016-01-08 07:06:32 +0000808 std::make_pair(CodeGenInstAlias(R, Variant, Target), Priority));
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000809 }
810
Bill Wendling7e570b52011-03-21 08:59:17 +0000811 // A map of which conditions need to be met for each instruction operand
812 // before it can be matched to the mnemonic.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000813 std::map<std::string, std::vector<IAPrinter>> IAPrinterMap;
Bill Wendling7e570b52011-03-21 08:59:17 +0000814
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000815 // A list of MCOperandPredicates for all operands in use, and the reverse map
816 std::vector<const Record*> MCOpPredicates;
817 DenseMap<const Record*, unsigned> MCOpPredicateMap;
818
Tim Northover9a24f882014-05-20 09:17:16 +0000819 for (auto &Aliases : AliasMap) {
820 for (auto &Alias : Aliases.second) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000821 const CodeGenInstAlias &CGA = Alias.first;
822 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Bill Wendling36c0c6d2011-06-15 04:31:19 +0000823 unsigned NumResultOps =
David Blaikie4ab57cd2015-08-06 19:23:33 +0000824 CountNumOperands(CGA.ResultInst->AsmString, Variant);
Bill Wendlinge7124492011-06-14 03:17:20 +0000825
826 // Don't emit the alias if it has more operands than what it's aliasing.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000827 if (NumResultOps < CountNumOperands(CGA.AsmString, Variant))
Bill Wendlinge7124492011-06-14 03:17:20 +0000828 continue;
829
David Blaikie4ab57cd2015-08-06 19:23:33 +0000830 IAPrinter IAP(CGA.Result->getAsString(), CGA.AsmString);
Bill Wendling7e570b52011-03-21 08:59:17 +0000831
Tim Northover60091cf2014-05-15 13:36:01 +0000832 unsigned NumMIOps = 0;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000833 for (auto &Operand : CGA.ResultOperands)
Tim Northover60091cf2014-05-15 13:36:01 +0000834 NumMIOps += Operand.getMINumOperands();
835
Bill Wendling7e570b52011-03-21 08:59:17 +0000836 std::string Cond;
Tim Northover60091cf2014-05-15 13:36:01 +0000837 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(NumMIOps);
David Blaikie4ab57cd2015-08-06 19:23:33 +0000838 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000839
Bill Wendling7e570b52011-03-21 08:59:17 +0000840 bool CantHandle = false;
841
Tim Northover60091cf2014-05-15 13:36:01 +0000842 unsigned MIOpNum = 0;
Bill Wendling7e570b52011-03-21 08:59:17 +0000843 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000844 std::string Op = "MI->getOperand(" + llvm::utostr(MIOpNum) + ")";
845
David Blaikie4ab57cd2015-08-06 19:23:33 +0000846 const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
Bill Wendling7e570b52011-03-21 08:59:17 +0000847
848 switch (RO.Kind) {
Bill Wendling7e570b52011-03-21 08:59:17 +0000849 case CodeGenInstAlias::ResultOperand::K_Record: {
850 const Record *Rec = RO.getRecord();
851 StringRef ROName = RO.getName();
Tim Northoveree20caa2014-05-12 18:04:06 +0000852 int PrintMethodIdx = -1;
Bill Wendling7e570b52011-03-21 08:59:17 +0000853
Tim Northoveree20caa2014-05-12 18:04:06 +0000854 // These two may have a PrintMethod, which we want to record (if it's
855 // the first time we've seen it) and provide an index for the aliasing
856 // code to use.
857 if (Rec->isSubClassOf("RegisterOperand") ||
858 Rec->isSubClassOf("Operand")) {
859 std::string PrintMethod = Rec->getValueAsString("PrintMethod");
860 if (PrintMethod != "" && PrintMethod != "printOperand") {
861 PrintMethodIdx = std::find(PrintMethods.begin(),
862 PrintMethods.end(), PrintMethod) -
863 PrintMethods.begin();
864 if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
865 PrintMethods.push_back(PrintMethod);
866 }
867 }
Owen Andersona84be6c2011-06-27 21:06:21 +0000868
869 if (Rec->isSubClassOf("RegisterOperand"))
870 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling7e570b52011-03-21 08:59:17 +0000871 if (Rec->isSubClassOf("RegisterClass")) {
David Blaikie4ab57cd2015-08-06 19:23:33 +0000872 IAP.addCond(Op + ".isReg()");
Bill Wendling7e570b52011-03-21 08:59:17 +0000873
David Blaikie4ab57cd2015-08-06 19:23:33 +0000874 if (!IAP.isOpMapped(ROName)) {
875 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
876 Record *R = CGA.ResultOperands[i].getRecord();
Jack Carter9c1a0272013-02-05 08:32:10 +0000877 if (R->isSubClassOf("RegisterOperand"))
878 R = R->getValueAsDef("RegClass");
Benjamin Kramer682de392012-03-30 23:13:40 +0000879 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
Tim Northover60091cf2014-05-15 13:36:01 +0000880 R->getName() + "RegClassID)"
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000881 ".contains(" + Op + ".getReg())";
Bill Wendling7e570b52011-03-21 08:59:17 +0000882 } else {
Artyom Skrobovaf3c20f2014-06-10 12:47:23 +0000883 Cond = Op + ".getReg() == MI->getOperand(" +
David Blaikie4ab57cd2015-08-06 19:23:33 +0000884 llvm::utostr(IAP.getOpIndex(ROName)) + ").getReg()";
Bill Wendling7e570b52011-03-21 08:59:17 +0000885 }
886 } else {
Tim Northoveree20caa2014-05-12 18:04:06 +0000887 // Assume all printable operands are desired for now. This can be
Alp Tokerbeaca192014-05-15 01:52:21 +0000888 // overridden in the InstAlias instantiation if necessary.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000889 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
Bill Wendling7e570b52011-03-21 08:59:17 +0000890
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000891 // There might be an additional predicate on the MCOperand
892 unsigned Entry = MCOpPredicateMap[Rec];
893 if (!Entry) {
894 if (!Rec->isValueUnset("MCOperandPredicate")) {
895 MCOpPredicates.push_back(Rec);
896 Entry = MCOpPredicates.size();
897 MCOpPredicateMap[Rec] = Entry;
898 } else
899 break; // No conditions on this operand at all
900 }
901 Cond = Target.getName() + ClassName + "ValidateMCOperand(" +
Oliver Stannarda34e4702015-12-01 10:48:51 +0000902 Op + ", STI, " + llvm::utostr(Entry) + ")";
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000903 }
904 // for all subcases of ResultOperand::K_Record:
David Blaikie4ab57cd2015-08-06 19:23:33 +0000905 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000906 break;
907 }
Tim Northoverab7689e2013-01-09 13:32:04 +0000908 case CodeGenInstAlias::ResultOperand::K_Imm: {
Tim Northoverab7689e2013-01-09 13:32:04 +0000909 // Just because the alias has an immediate result, doesn't mean the
910 // MCInst will. An MCExpr could be present, for example.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000911 IAP.addCond(Op + ".isImm()");
Tim Northoverab7689e2013-01-09 13:32:04 +0000912
David Blaikie4ab57cd2015-08-06 19:23:33 +0000913 Cond = Op + ".getImm() == " +
914 llvm::utostr(CGA.ResultOperands[i].getImm());
915 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000916 break;
Tim Northoverab7689e2013-01-09 13:32:04 +0000917 }
Bill Wendling7e570b52011-03-21 08:59:17 +0000918 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000919 // If this is zero_reg, something's playing tricks we're not
920 // equipped to handle.
David Blaikie4ab57cd2015-08-06 19:23:33 +0000921 if (!CGA.ResultOperands[i].getRegister()) {
Jim Grosbach29cdcda2011-11-15 01:46:57 +0000922 CantHandle = true;
923 break;
924 }
925
David Blaikie4ab57cd2015-08-06 19:23:33 +0000926 Cond = Op + ".getReg() == " + Target.getName() + "::" +
927 CGA.ResultOperands[i].getRegister()->getName();
928 IAP.addCond(Cond);
Bill Wendling7e570b52011-03-21 08:59:17 +0000929 break;
930 }
931
Tim Northover60091cf2014-05-15 13:36:01 +0000932 MIOpNum += RO.getMINumOperands();
Bill Wendling7e570b52011-03-21 08:59:17 +0000933 }
934
935 if (CantHandle) continue;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000936 IAPrinterMap[Aliases.first].push_back(std::move(IAP));
Bill Wendling7e570b52011-03-21 08:59:17 +0000937 }
938 }
939
Tim Northoveree20caa2014-05-12 18:04:06 +0000940 //////////////////////////////
941 // Write out the printAliasInstr function
942 //////////////////////////////
943
Bill Wendlingf5199de2011-05-23 00:18:33 +0000944 std::string Header;
945 raw_string_ostream HeaderO(Header);
946
947 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendlinge7124492011-06-14 03:17:20 +0000948 << "::printAliasInstr(const MCInst"
Akira Hatanakab46d0232015-03-27 20:36:02 +0000949 << " *MI, " << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
950 << "raw_ostream &OS) {\n";
Bill Wendling7e570b52011-03-21 08:59:17 +0000951
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000952 std::string Cases;
953 raw_string_ostream CasesO(Cases);
954
David Blaikie4ab57cd2015-08-06 19:23:33 +0000955 for (auto &Entry : IAPrinterMap) {
956 std::vector<IAPrinter> &IAPs = Entry.second;
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000957 std::vector<IAPrinter*> UniqueIAPs;
958
David Blaikie4ab57cd2015-08-06 19:23:33 +0000959 for (auto &LHS : IAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000960 bool IsDup = false;
David Blaikie4ab57cd2015-08-06 19:23:33 +0000961 for (const auto &RHS : IAPs) {
962 if (&LHS != &RHS && LHS == RHS) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000963 IsDup = true;
964 break;
965 }
966 }
967
David Blaikie4ab57cd2015-08-06 19:23:33 +0000968 if (!IsDup)
969 UniqueIAPs.push_back(&LHS);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000970 }
971
972 if (UniqueIAPs.empty()) continue;
973
David Blaikie4ab57cd2015-08-06 19:23:33 +0000974 CasesO.indent(2) << "case " << Entry.first << ":\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000975
Craig Topper190ecd52016-01-08 07:06:32 +0000976 for (IAPrinter *IAP : UniqueIAPs) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000977 CasesO.indent(4);
Evan Cheng4d806e22011-07-06 02:02:33 +0000978 IAP->print(CasesO);
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000979 CasesO << '\n';
980 }
981
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000982 CasesO.indent(4) << "return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +0000983 }
984
Bill Wendlinge7124492011-06-14 03:17:20 +0000985 if (CasesO.str().empty()) {
Bill Wendlingf5199de2011-05-23 00:18:33 +0000986 O << HeaderO.str();
Eric Christopher2e3fbaa2011-04-18 21:28:11 +0000987 O << " return false;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +0000988 O << "}\n\n";
989 O << "#endif // PRINT_ALIAS_INSTR\n";
990 return;
991 }
992
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000993 if (!MCOpPredicates.empty())
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000994 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +0000995 << "ValidateMCOperand(const MCOperand &MCOp,\n"
996 << " const MCSubtargetInfo &STI,\n"
997 << " unsigned PredicateIndex);\n";
Artyom Skrobov6c8682e2014-06-10 13:11:35 +0000998
Bill Wendlingf5199de2011-05-23 00:18:33 +0000999 O << HeaderO.str();
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001000 O.indent(2) << "const char *AsmString;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +00001001 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher2e3fbaa2011-04-18 21:28:11 +00001002 O.indent(2) << "default: return false;\n";
Bill Wendlingbc3f7902011-04-07 21:20:06 +00001003 O << CasesO.str();
1004 O.indent(2) << "}\n\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001005
1006 // Code that prints the alias, replacing the operands with the ones from the
1007 // MCInst.
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001008 O << " unsigned I = 0;\n";
Tim Northoverd8d65a62014-05-15 11:16:32 +00001009 O << " while (AsmString[I] != ' ' && AsmString[I] != '\t' &&\n";
1010 O << " AsmString[I] != '\\0')\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001011 O << " ++I;\n";
1012 O << " OS << '\\t' << StringRef(AsmString, I);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001013
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001014 O << " if (AsmString[I] != '\\0') {\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001015 O << " OS << '\\t';\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001016 O << " do {\n";
1017 O << " if (AsmString[I] == '$') {\n";
1018 O << " ++I;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001019 O << " if (AsmString[I] == (char)0xff) {\n";
1020 O << " ++I;\n";
1021 O << " int OpIdx = AsmString[I++] - 1;\n";
1022 O << " int PrintMethodIdx = AsmString[I++] - 1;\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001023 O << " printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, ";
1024 O << (PassSubtarget ? "STI, " : "");
1025 O << "OS);\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001026 O << " } else\n";
Akira Hatanakab46d0232015-03-27 20:36:02 +00001027 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, ";
1028 O << (PassSubtarget ? "STI, " : "");
1029 O << "OS);\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001030 O << " } else {\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001031 O << " OS << AsmString[I++];\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001032 O << " }\n";
Benjamin Kramer17c17bc2013-09-11 15:42:16 +00001033 O << " } while (AsmString[I] != '\\0');\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001034 O << " }\n\n";
Jim Grosbachf4e67082012-04-18 18:56:33 +00001035
Eric Christopher2e3fbaa2011-04-18 21:28:11 +00001036 O << " return true;\n";
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001037 O << "}\n\n";
1038
Tim Northoveree20caa2014-05-12 18:04:06 +00001039 //////////////////////////////
1040 // Write out the printCustomAliasOperand function
1041 //////////////////////////////
1042
1043 O << "void " << Target.getName() << ClassName << "::"
1044 << "printCustomAliasOperand(\n"
1045 << " const MCInst *MI, unsigned OpIdx,\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001046 << " unsigned PrintMethodIdx,\n"
1047 << (PassSubtarget ? " const MCSubtargetInfo &STI,\n" : "")
1048 << " raw_ostream &OS) {\n";
Aaron Ballmane58a5702014-05-13 12:52:35 +00001049 if (PrintMethods.empty())
1050 O << " llvm_unreachable(\"Unknown PrintMethod kind\");\n";
1051 else {
1052 O << " switch (PrintMethodIdx) {\n"
1053 << " default:\n"
1054 << " llvm_unreachable(\"Unknown PrintMethod kind\");\n"
Tim Northoveree20caa2014-05-12 18:04:06 +00001055 << " break;\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001056
Aaron Ballmane58a5702014-05-13 12:52:35 +00001057 for (unsigned i = 0; i < PrintMethods.size(); ++i) {
1058 O << " case " << i << ":\n"
Akira Hatanakab46d0232015-03-27 20:36:02 +00001059 << " " << PrintMethods[i] << "(MI, OpIdx, "
1060 << (PassSubtarget ? "STI, " : "") << "OS);\n"
Aaron Ballmane58a5702014-05-13 12:52:35 +00001061 << " break;\n";
1062 }
1063 O << " }\n";
1064 }
1065 O << "}\n\n";
Tim Northoveree20caa2014-05-12 18:04:06 +00001066
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00001067 if (!MCOpPredicates.empty()) {
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001068 O << "static bool " << Target.getName() << ClassName
Oliver Stannarda34e4702015-12-01 10:48:51 +00001069 << "ValidateMCOperand(const MCOperand &MCOp,\n"
1070 << " const MCSubtargetInfo &STI,\n"
1071 << " unsigned PredicateIndex) {\n"
Artyom Skrobov6c8682e2014-06-10 13:11:35 +00001072 << " switch (PredicateIndex) {\n"
1073 << " default:\n"
1074 << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
1075 << " break;\n";
1076
1077 for (unsigned i = 0; i < MCOpPredicates.size(); ++i) {
1078 Init *MCOpPred = MCOpPredicates[i]->getValueInit("MCOperandPredicate");
1079 if (StringInit *SI = dyn_cast<StringInit>(MCOpPred)) {
1080 O << " case " << i + 1 << ": {\n"
1081 << SI->getValue() << "\n"
1082 << " }\n";
1083 } else
1084 llvm_unreachable("Unexpected MCOperandPredicate field!");
1085 }
1086 O << " }\n"
1087 << "}\n\n";
1088 }
1089
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001090 O << "#endif // PRINT_ALIAS_INSTR\n";
1091}
Chris Lattner06c5eed2009-09-13 20:08:00 +00001092
Ahmed Bougachabd214002013-10-28 18:07:17 +00001093AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1094 Record *AsmWriter = Target.getAsmWriter();
Craig Topper0bd58742016-01-13 07:20:05 +00001095 unsigned Variant = AsmWriter->getValueAsInt("Variant");
Craig Topper03ec8012014-11-25 20:11:31 +00001096 for (const CodeGenInstruction *I : Target.instructions())
1097 if (!I->AsmString.empty() && I->TheDef->getName() != "PHI")
Craig Topperc24a4012016-01-14 06:15:07 +00001098 Instructions.emplace_back(*I, Variant);
Ahmed Bougachabd214002013-10-28 18:07:17 +00001099
1100 // Get the instruction numbering.
Craig Topper4c6129a2014-02-05 07:56:49 +00001101 NumberedInstructions = &Target.getInstructionsByEnumValue();
Ahmed Bougachabd214002013-10-28 18:07:17 +00001102
1103 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
1104 // all machine instructions are necessarily being printed, so there may be
1105 // target instructions not in this map.
Craig Topper190ecd52016-01-08 07:06:32 +00001106 for (AsmWriterInst &AWI : Instructions)
1107 CGIAWIMap.insert(std::make_pair(AWI.CGI, &AWI));
Ahmed Bougachabd214002013-10-28 18:07:17 +00001108}
1109
Chris Lattner06c5eed2009-09-13 20:08:00 +00001110void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner06c5eed2009-09-13 20:08:00 +00001111 EmitPrintInstruction(O);
1112 EmitGetRegisterName(O);
Bill Wendling31ca7ef2011-02-26 03:09:12 +00001113 EmitPrintAliasInstruction(O);
Chris Lattner06c5eed2009-09-13 20:08:00 +00001114}
1115
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +00001116
1117namespace llvm {
1118
1119void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1120 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1121 AsmWriterEmitter(RK).run(OS);
1122}
1123
1124} // End llvm namespace