blob: a6d1daf91a485f39d70025fd941df9eff00a2c0c [file] [log] [blame]
Chris Lattner2e1f51b2004-08-01 05:59:33 +00001//===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Chris Lattner2e1f51b2004-08-01 05:59:33 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
Chris Lattner2e1f51b2004-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 Callanand32c02f2010-02-09 21:50:41 +000015#include "AsmWriterInst.h"
Chris Lattner2e1f51b2004-08-01 05:59:33 +000016#include "CodeGenTarget.h"
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +000017#include "SequenceToOffsetTable.h"
Craig Topperf5535872012-07-27 06:44:02 +000018#include "llvm/ADT/StringExtras.h"
Owen Andersonbea6f612011-06-27 21:06:21 +000019#include "llvm/ADT/Twine.h"
Chris Lattnerbdff5f92006-07-18 17:18:03 +000020#include "llvm/Support/Debug.h"
Benjamin Kramer209a8c82013-09-11 15:42:16 +000021#include "llvm/Support/Format.h"
Chris Lattnerbdff5f92006-07-18 17:18:03 +000022#include "llvm/Support/MathExtras.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000023#include "llvm/TableGen/Error.h"
24#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000025#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohen615ed992005-01-22 18:50:10 +000026#include <algorithm>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000027#include <cassert>
28#include <map>
29#include <vector>
Chris Lattner2e1f51b2004-08-01 05:59:33 +000030using namespace llvm;
31
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000032namespace {
33class AsmWriterEmitter {
34 RecordKeeper &Records;
35 std::map<const CodeGenInstruction*, AsmWriterInst*> CGIAWIMap;
36 std::vector<const CodeGenInstruction*> NumberedInstructions;
37public:
38 AsmWriterEmitter(RecordKeeper &R) : Records(R) {}
39
40 void run(raw_ostream &o);
41
42private:
43 void EmitPrintInstruction(raw_ostream &o);
44 void EmitGetRegisterName(raw_ostream &o);
45 void EmitPrintAliasInstruction(raw_ostream &O);
46
47 AsmWriterInst *getAsmWriterInstByID(unsigned ID) const {
48 assert(ID < NumberedInstructions.size());
49 std::map<const CodeGenInstruction*, AsmWriterInst*>::const_iterator I =
50 CGIAWIMap.find(NumberedInstructions[ID]);
51 assert(I != CGIAWIMap.end() && "Didn't find inst!");
52 return I->second;
53 }
54 void FindUniqueOperandCommands(std::vector<std::string> &UOC,
55 std::vector<unsigned> &InstIdxs,
56 std::vector<unsigned> &InstOpsUsed) const;
57};
58} // end anonymous namespace
59
Chris Lattner38c07512005-01-22 20:31:17 +000060static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +000061 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +000062 O << " case " << OpsToPrint.back().first << ": ";
63 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) {
70 O << "\n case " << OpsToPrint[i-1].first << ": ";
71 OpsToPrint.erase(OpsToPrint.begin()+i-1);
72 }
73
74 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000075 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +000076 O << "break;\n";
77}
78
Chris Lattner870c0162005-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,
Daniel Dunbar1a551802009-07-03 00:10:29 +000083 raw_ostream &O) {
Chris Lattner870c0162005-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 Lattnerf8766682005-01-22 19:22:23 +000090 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
91 if (DiffOp != ~1U) {
Chris Lattner870c0162005-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 Lattnerf8766682005-01-22 19:22:23 +000097 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +000098 SimilarInsts.push_back(Insts[i-1]);
99 Insts.erase(Insts.begin()+i-1);
100 }
101 }
102 }
103
Chris Lattnera1e8a802006-05-01 17:01:17 +0000104 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000105 << FirstInst.CGI->TheDef->getName() << ":\n";
106 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +0000107 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000108 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
109 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.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000112 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-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";
Chris Lattner38c07512005-01-22 20:31:17 +0000117 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +0000118 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +0000119 FirstInst.CGI->TheDef->getName(),
120 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000121
Chris Lattner870c0162005-01-22 18:38:13 +0000122 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +0000123 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +0000124 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +0000125 AWI.CGI->TheDef->getName(),
126 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +0000127 }
Chris Lattner38c07512005-01-22 20:31:17 +0000128 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
129 while (!OpsToPrint.empty())
130 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000131 O << " }";
132 }
133 O << "\n";
134 }
Chris Lattner870c0162005-01-22 18:38:13 +0000135 O << " break;\n";
136}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000137
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000138void AsmWriterEmitter::
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000139FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000140 std::vector<unsigned> &InstIdxs,
141 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000142 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000143
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000144 // This vector parallels UniqueOperandCommands, keeping track of which
145 // instructions each case are used for. It is a comma separated string of
146 // enums.
147 std::vector<std::string> InstrsForCase;
148 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000149 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000150
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000151 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
152 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Bill Wendlingb9449d62010-07-16 23:10:00 +0000153 if (Inst == 0) continue; // PHI, INLINEASM, PROLOG_LABEL, etc.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000154
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000155 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000156 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000157 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000158
Chris Lattnerb8462862006-07-18 17:56:07 +0000159 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000160
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000161 // Check to see if we already have 'Command' in UniqueOperandCommands.
162 // If not, add it.
163 bool FoundIt = false;
164 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
165 if (UniqueOperandCommands[idx] == Command) {
166 InstIdxs[i] = idx;
167 InstrsForCase[idx] += ", ";
168 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
169 FoundIt = true;
170 break;
171 }
172 if (!FoundIt) {
173 InstIdxs[i] = UniqueOperandCommands.size();
174 UniqueOperandCommands.push_back(Command);
175 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000176
177 // This command matches one operand so far.
178 InstOpsUsed.push_back(1);
179 }
180 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000181
Chris Lattner96c1ade2006-07-18 18:28:27 +0000182 // For each entry of UniqueOperandCommands, there is a set of instructions
183 // that uses it. If the next command of all instructions in the set are
184 // identical, fold it into the command.
185 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
186 CommandIdx != e; ++CommandIdx) {
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000187
Chris Lattner96c1ade2006-07-18 18:28:27 +0000188 for (unsigned Op = 1; ; ++Op) {
189 // Scan for the first instruction in the set.
190 std::vector<unsigned>::iterator NIT =
191 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
192 if (NIT == InstIdxs.end()) break; // No commonality.
193
194 // If this instruction has no more operands, we isn't anything to merge
195 // into this command.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000196 const AsmWriterInst *FirstInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000197 getAsmWriterInstByID(NIT-InstIdxs.begin());
198 if (!FirstInst || FirstInst->Operands.size() == Op)
199 break;
200
201 // Otherwise, scan to see if all of the other instructions in this command
202 // set share the operand.
203 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000204 // Keep track of the maximum, number of operands or any
205 // instruction we see in the group.
206 size_t MaxSize = FirstInst->Operands.size();
207
Chris Lattner96c1ade2006-07-18 18:28:27 +0000208 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
209 NIT != InstIdxs.end();
210 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
211 // Okay, found another instruction in this command set. If the operand
212 // matches, we're ok, otherwise bail out.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000213 const AsmWriterInst *OtherInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000214 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000215
216 if (OtherInst &&
217 OtherInst->Operands.size() > FirstInst->Operands.size())
218 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
219
Chris Lattner96c1ade2006-07-18 18:28:27 +0000220 if (!OtherInst || OtherInst->Operands.size() == Op ||
221 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
222 AllSame = false;
223 break;
224 }
225 }
226 if (!AllSame) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000227
Chris Lattner96c1ade2006-07-18 18:28:27 +0000228 // Okay, everything in this command set has the same next operand. Add it
229 // to UniqueOperandCommands and remember that it was consumed.
230 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000231
Chris Lattner96c1ade2006-07-18 18:28:27 +0000232 UniqueOperandCommands[CommandIdx] += Command;
233 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000234 }
235 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000236
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000237 // Prepend some of the instructions each case is used for onto the case val.
238 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
239 std::string Instrs = InstrsForCase[i];
240 if (Instrs.size() > 70) {
241 Instrs.erase(Instrs.begin()+70, Instrs.end());
242 Instrs += "...";
243 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000244
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000245 if (!Instrs.empty())
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000246 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000247 UniqueOperandCommands[i];
248 }
249}
250
251
Daniel Dunbar9bd34602009-10-17 20:43:42 +0000252static void UnescapeString(std::string &Str) {
253 for (unsigned i = 0; i != Str.size(); ++i) {
254 if (Str[i] == '\\' && i != Str.size()-1) {
255 switch (Str[i+1]) {
256 default: continue; // Don't execute the code after the switch.
257 case 'a': Str[i] = '\a'; break;
258 case 'b': Str[i] = '\b'; break;
259 case 'e': Str[i] = 27; break;
260 case 'f': Str[i] = '\f'; break;
261 case 'n': Str[i] = '\n'; break;
262 case 'r': Str[i] = '\r'; break;
263 case 't': Str[i] = '\t'; break;
264 case 'v': Str[i] = '\v'; break;
265 case '"': Str[i] = '\"'; break;
266 case '\'': Str[i] = '\''; break;
267 case '\\': Str[i] = '\\'; break;
268 }
269 // Nuke the second character.
270 Str.erase(Str.begin()+i+1);
271 }
272 }
273}
274
Chris Lattner05af2612009-09-13 20:08:00 +0000275/// EmitPrintInstruction - Generate the code for the "printInstruction" method
276/// implementation.
277void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000278 CodeGenTarget Target(Records);
Chris Lattner175580c2004-08-14 22:50:53 +0000279 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000280 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jim Grosbachca96a862010-09-30 01:29:54 +0000281 bool isMC = AsmWriter->getValueAsBit("isMCAsmWriter");
282 const char *MachineInstrClassName = isMC ? "MCInst" : "MachineInstr";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000283
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000284 O <<
285 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000286 "/// from the instruction set description.\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000287 "void " << Target.getName() << ClassName
Jim Grosbachca96a862010-09-30 01:29:54 +0000288 << "::printInstruction(const " << MachineInstrClassName
289 << " *MI, raw_ostream &O) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000290
Chris Lattner5765dba2005-01-22 17:40:38 +0000291 std::vector<AsmWriterInst> Instructions;
292
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000293 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
294 E = Target.inst_end(); I != E; ++I)
Chris Lattner6a91b182010-03-19 01:00:55 +0000295 if (!(*I)->AsmString.empty() &&
296 (*I)->TheDef->getName() != "PHI")
Sean Callanand0bc7f02010-02-09 23:06:35 +0000297 Instructions.push_back(
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000298 AsmWriterInst(**I,
Sean Callanand0bc7f02010-02-09 23:06:35 +0000299 AsmWriter->getValueAsInt("Variant"),
300 AsmWriter->getValueAsInt("FirstOperandColumn"),
301 AsmWriter->getValueAsInt("OperandSpacing")));
Chris Lattner076efa72004-08-01 07:43:02 +0000302
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000303 // Get the instruction numbering.
Chris Lattnerf6502782010-03-19 00:34:35 +0000304 NumberedInstructions = Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000305
Chris Lattner6af022f2006-07-14 22:59:11 +0000306 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
307 // all machine instructions are necessarily being printed, so there may be
308 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000309 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
310 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000311
Chris Lattner6af022f2006-07-14 22:59:11 +0000312 // Build an aggregate string, and build a table of offsets into it.
Benjamin Kramer94338592012-04-02 09:13:46 +0000313 SequenceToOffsetTable<std::string> StringTable;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000314
Chris Lattner259bda42006-09-27 16:44:09 +0000315 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000316 /// chunk of the output as well as indices used for operand printing.
Manman Ren6579cf82012-09-13 17:43:46 +0000317 /// To reduce the number of unhandled cases, we expand the size from 32-bit
318 /// to 32+16 = 48-bit.
Craig Topperf4d78242012-09-14 08:33:11 +0000319 std::vector<uint64_t> OpcodeInfo;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000320
Benjamin Kramer94338592012-04-02 09:13:46 +0000321 // Add all strings to the string table upfront so it can generate an optimized
322 // representation.
323 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
324 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
325 if (AWI != 0 &&
Jim Grosbach016c6792012-04-18 18:56:33 +0000326 AWI->Operands[0].OperandType ==
327 AsmWriterOperand::isLiteralTextOperand &&
Benjamin Kramer94338592012-04-02 09:13:46 +0000328 !AWI->Operands[0].Str.empty()) {
329 std::string Str = AWI->Operands[0].Str;
330 UnescapeString(Str);
331 StringTable.add(Str);
332 }
333 }
334
335 StringTable.layout();
336
Chris Lattner55616402006-07-18 17:32:27 +0000337 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000338 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
339 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
340 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000341 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000342 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000343 Idx = ~0U;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000344 } else if (AWI->Operands[0].OperandType !=
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000345 AsmWriterOperand::isLiteralTextOperand ||
346 AWI->Operands[0].Str.empty()) {
347 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer94338592012-04-02 09:13:46 +0000348 Idx = StringTable.get("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000349 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000350 std::string Str = AWI->Operands[0].Str;
351 UnescapeString(Str);
Benjamin Kramer94338592012-04-02 09:13:46 +0000352 Idx = StringTable.get(Str);
Chris Lattner3200fc92009-09-14 01:16:36 +0000353 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000354
Chris Lattner6af022f2006-07-14 22:59:11 +0000355 // Nuke the string from the operand list. It is now handled!
356 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000357 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000358
Chris Lattner3200fc92009-09-14 01:16:36 +0000359 // Bias offset by one since we want 0 as a sentinel.
Craig Topperf4d78242012-09-14 08:33:11 +0000360 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000361 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000362
Chris Lattner55616402006-07-18 17:32:27 +0000363 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000364 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000365
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000366 // To reduce code size, we compactify common instructions into a few bits
367 // in the opcode-indexed table.
Craig Topperf4d78242012-09-14 08:33:11 +0000368 unsigned BitsLeft = 64-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000369
370 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000371
Chris Lattnerb8462862006-07-18 17:56:07 +0000372 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000373 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000374 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000375 std::vector<unsigned> NumInstOpsHandled;
376 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
377 NumInstOpsHandled);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000378
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000379 // If we ran out of operands to print, we're done.
380 if (UniqueOperandCommands.empty()) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000381
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000382 // Compute the number of bits we need to represent these cases, this is
383 // ceil(log2(numentries)).
384 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000385
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000386 // If we don't have enough bits for this operand, don't include it.
387 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000388 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
389 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000390 break;
391 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000392
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000393 // Otherwise, we can include this in the initial lookup table. Add it in.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000394 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Manman Ren6579cf82012-09-13 17:43:46 +0000395 if (InstIdxs[i] != ~0U) {
Craig Topperf4d78242012-09-14 08:33:11 +0000396 OpcodeInfo[i] |= (uint64_t)InstIdxs[i] << (64-BitsLeft);
Manman Ren6579cf82012-09-13 17:43:46 +0000397 }
Craig Topperf4d78242012-09-14 08:33:11 +0000398 BitsLeft -= NumBits;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000399
Chris Lattnerb8462862006-07-18 17:56:07 +0000400 // Remove the info about this operand.
401 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
402 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000403 if (!Inst->Operands.empty()) {
404 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000405 assert(NumOps <= Inst->Operands.size() &&
406 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000407 Inst->Operands.erase(Inst->Operands.begin(),
408 Inst->Operands.begin()+NumOps);
409 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000410 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000411
Chris Lattnerb8462862006-07-18 17:56:07 +0000412 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000413 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
414 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000415
416
Craig Topperf4d78242012-09-14 08:33:11 +0000417 // We always emit at least one 32-bit table. A second table is emitted if
418 // more bits are needed.
419 O<<" static const uint32_t OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000420 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Craig Topperf4d78242012-09-14 08:33:11 +0000421 O << " " << (OpcodeInfo[i] & 0xffffffff) << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000422 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000423 }
424 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000425 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000426 O << " };\n\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000427
Craig Topperf4d78242012-09-14 08:33:11 +0000428 if (BitsLeft < 32) {
Manman Ren6579cf82012-09-13 17:43:46 +0000429 // Add a second OpInfo table only when it is necessary.
Craig Topperf4d78242012-09-14 08:33:11 +0000430 // Adjust the type of the second table based on the number of bits needed.
431 O << " static const uint"
432 << ((BitsLeft < 16) ? "32" : (BitsLeft < 24) ? "16" : "8")
433 << "_t OpInfo2[] = {\n";
Manman Ren6579cf82012-09-13 17:43:46 +0000434 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Craig Topperf4d78242012-09-14 08:33:11 +0000435 O << " " << (OpcodeInfo[i] >> 32) << "U,\t// "
Manman Ren6579cf82012-09-13 17:43:46 +0000436 << NumberedInstructions[i]->TheDef->getName() << "\n";
437 }
438 // Add a dummy entry so the array init doesn't end with a comma.
439 O << " 0U\n";
440 O << " };\n\n";
441 }
442
Chris Lattner6af022f2006-07-14 22:59:11 +0000443 // Emit the string itself.
Benjamin Kramer94338592012-04-02 09:13:46 +0000444 O << " const char AsmStrs[] = {\n";
445 StringTable.emit(O, printChar);
446 O << " };\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000447
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000448 O << " O << \"\\t\";\n\n";
449
Craig Topperf4d78242012-09-14 08:33:11 +0000450 O << " // Emit the opcode for the instruction.\n";
451 if (BitsLeft < 32) {
452 // If we have two tables then we need to perform two lookups and combine
453 // the results into a single 64-bit value.
454 O << " uint64_t Bits1 = OpInfo[MI->getOpcode()];\n"
455 << " uint64_t Bits2 = OpInfo2[MI->getOpcode()];\n"
456 << " uint64_t Bits = (Bits2 << 32) | Bits1;\n";
457 } else {
458 // If only one table is used we just need to perform a single lookup.
459 O << " uint32_t Bits = OpInfo[MI->getOpcode()];\n";
460 }
Manman Ren6579cf82012-09-13 17:43:46 +0000461 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000462 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000463
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000464 // Output the table driven operand information.
Craig Topperf4d78242012-09-14 08:33:11 +0000465 BitsLeft = 64-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000466 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
467 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
468
469 // Compute the number of bits we need to represent these cases, this is
470 // ceil(log2(numentries)).
471 unsigned NumBits = Log2_32_Ceil(Commands.size());
472 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000473
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000474 // Emit code to extract this field from Bits.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000475 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000476 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000477
Chris Lattner96c1ade2006-07-18 18:28:27 +0000478 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000479 // Emit two possibilitys with if/else.
Craig Topperf4d78242012-09-14 08:33:11 +0000480 O << " if ((Bits >> "
481 << (64-BitsLeft) << ") & "
Chris Lattnere7a589d2006-07-18 17:43:54 +0000482 << ((1 << NumBits)-1) << ") {\n"
483 << Commands[1]
484 << " } else {\n"
485 << Commands[0]
486 << " }\n\n";
Eric Christopher16870502010-09-18 18:50:27 +0000487 } else if (Commands.size() == 1) {
488 // Emit a single possibility.
489 O << Commands[0] << "\n\n";
Chris Lattnere7a589d2006-07-18 17:43:54 +0000490 } else {
Craig Topperf4d78242012-09-14 08:33:11 +0000491 O << " switch ((Bits >> "
492 << (64-BitsLeft) << ") & "
Chris Lattnere7a589d2006-07-18 17:43:54 +0000493 << ((1 << NumBits)-1) << ") {\n"
494 << " default: // unreachable.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000495
Chris Lattnere7a589d2006-07-18 17:43:54 +0000496 // Print out all the cases.
497 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
498 O << " case " << i << ":\n";
499 O << Commands[i];
500 O << " break;\n";
501 }
502 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000503 }
Craig Topperf4d78242012-09-14 08:33:11 +0000504 BitsLeft -= NumBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000505 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000506
Chris Lattnerb8462862006-07-18 17:56:07 +0000507 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000508 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
509 // Entire instruction has been emitted?
510 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000511 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000512 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000513 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000514 }
515 }
516
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000517
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000518 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000519 // elements in the vector.
520 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000521
522
Chris Lattner70067602009-09-18 18:10:19 +0000523 // Now that we've emitted all of the operand info that fit into 32 bits, emit
524 // information for those instructions that are left. This is a less dense
525 // encoding, but we expect the main 32-bit table to handle the majority of
526 // instructions.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000527 if (!Instructions.empty()) {
528 // Find the opcode # of inline asm.
529 O << " switch (MI->getOpcode()) {\n";
530 while (!Instructions.empty())
531 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000532
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000533 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000534 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000535 }
David Greenec8d06052009-07-29 20:10:24 +0000536
Chris Lattner0a012122006-07-18 19:06:01 +0000537 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000538}
Chris Lattner05af2612009-09-13 20:08:00 +0000539
Owen Andersonbea6f612011-06-27 21:06:21 +0000540static void
541emitRegisterNameString(raw_ostream &O, StringRef AltName,
Craig Topper9b1b25f2012-04-03 06:52:47 +0000542 const std::vector<CodeGenRegister*> &Registers) {
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000543 SequenceToOffsetTable<std::string> StringTable;
544 SmallVector<std::string, 4> AsmNames(Registers.size());
Owen Andersonbea6f612011-06-27 21:06:21 +0000545 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
546 const CodeGenRegister &Reg = *Registers[i];
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000547 std::string &AsmName = AsmNames[i];
Owen Andersonbea6f612011-06-27 21:06:21 +0000548
Owen Andersonbea6f612011-06-27 21:06:21 +0000549 // "NoRegAltName" is special. We don't need to do a lookup for that,
550 // as it's just a reference to the default register name.
551 if (AltName == "" || AltName == "NoRegAltName") {
552 AsmName = Reg.TheDef->getValueAsString("AsmName");
553 if (AsmName.empty())
554 AsmName = Reg.getName();
555 } else {
556 // Make sure the register has an alternate name for this index.
557 std::vector<Record*> AltNameList =
558 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
559 unsigned Idx = 0, e;
560 for (e = AltNameList.size();
561 Idx < e && (AltNameList[Idx]->getName() != AltName);
562 ++Idx)
563 ;
564 // If the register has an alternate name for this index, use it.
565 // Otherwise, leave it empty as an error flag.
566 if (Idx < e) {
567 std::vector<std::string> AltNames =
568 Reg.TheDef->getValueAsListOfStrings("AltNames");
569 if (AltNames.size() <= Idx)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000570 PrintFatalError(Reg.TheDef->getLoc(),
571 (Twine("Register definition missing alt name for '") +
572 AltName + "'.").str());
Owen Andersonbea6f612011-06-27 21:06:21 +0000573 AsmName = AltNames[Idx];
574 }
575 }
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000576 StringTable.add(AsmName);
577 }
Owen Andersonbea6f612011-06-27 21:06:21 +0000578
Craig Topper5974c312012-09-15 01:22:42 +0000579 StringTable.layout();
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000580 O << " static const char AsmStrs" << AltName << "[] = {\n";
581 StringTable.emit(O, printChar);
582 O << " };\n\n";
583
Craig Topper5974c312012-09-15 01:22:42 +0000584 O << " static const uint32_t RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000585 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Toppera4bd58b2012-04-02 00:47:39 +0000586 if ((i % 14) == 0)
587 O << "\n ";
588 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersonbea6f612011-06-27 21:06:21 +0000589 }
Craig Topper9b1b25f2012-04-03 06:52:47 +0000590 O << "\n };\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000591 << "\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000592}
Chris Lattner05af2612009-09-13 20:08:00 +0000593
594void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000595 CodeGenTarget Target(Records);
Chris Lattner05af2612009-09-13 20:08:00 +0000596 Record *AsmWriter = Target.getAsmWriter();
597 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000598 const std::vector<CodeGenRegister*> &Registers =
599 Target.getRegBank().getRegisters();
Owen Andersonbea6f612011-06-27 21:06:21 +0000600 std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices();
601 bool hasAltNames = AltNameIndices.size() > 1;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000602
Chris Lattner05af2612009-09-13 20:08:00 +0000603 O <<
604 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
605 "/// from the register set description. This returns the assembler name\n"
606 "/// for the specified register.\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000607 "const char *" << Target.getName() << ClassName << "::";
608 if (hasAltNames)
609 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
610 else
611 O << "getRegisterName(unsigned RegNo) {\n";
612 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
613 << " && \"Invalid register number!\");\n"
Chris Lattnerf6761be2009-09-14 01:26:18 +0000614 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000615
Owen Andersonbea6f612011-06-27 21:06:21 +0000616 if (hasAltNames) {
617 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i)
618 emitRegisterNameString(O, AltNameIndices[i]->getName(), Registers);
619 } else
620 emitRegisterNameString(O, "", Registers);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000621
Owen Andersonbea6f612011-06-27 21:06:21 +0000622 if (hasAltNames) {
Craig Topper5974c312012-09-15 01:22:42 +0000623 O << " const uint32_t *RegAsmOffset;\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000624 << " const char *AsmStrs;\n"
625 << " switch(AltIdx) {\n"
Craig Topper655b8de2012-02-05 07:21:30 +0000626 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000627 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) {
628 StringRef Namespace = AltNameIndices[1]->getValueAsString("Namespace");
629 StringRef AltName(AltNameIndices[i]->getName());
630 O << " case " << Namespace << "::" << AltName
631 << ":\n"
632 << " AsmStrs = AsmStrs" << AltName << ";\n"
633 << " RegAsmOffset = RegAsmOffset" << AltName << ";\n"
634 << " break;\n";
635 }
636 O << "}\n";
637 }
638
639 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
640 << " \"Invalid alt name index for register!\");\n"
641 << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000642 << "}\n";
643}
644
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000645namespace {
Bill Wendling4962e612011-03-21 08:40:31 +0000646// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
647// they both have the same conditionals. In which case, we cannot print out the
648// alias for that pattern.
649class IAPrinter {
Bill Wendling4962e612011-03-21 08:40:31 +0000650 std::vector<std::string> Conds;
651 std::map<StringRef, unsigned> OpMap;
652 std::string Result;
653 std::string AsmString;
Jim Grosbacha5b06852012-04-18 19:02:43 +0000654 SmallVector<Record*, 4> ReqFeatures;
Bill Wendling4962e612011-03-21 08:40:31 +0000655public:
Evan Cheng68ae5b42011-07-06 02:02:33 +0000656 IAPrinter(std::string R, std::string AS)
657 : Result(R), AsmString(AS) {}
Bill Wendling4962e612011-03-21 08:40:31 +0000658
659 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling4962e612011-03-21 08:40:31 +0000660
Benjamin Kramer209a8c82013-09-11 15:42:16 +0000661 void addOperand(StringRef Op, unsigned Idx) {
662 assert(Idx < 0xFF && "Index too large!");
663 OpMap[Op] = Idx;
664 }
Bill Wendling4962e612011-03-21 08:40:31 +0000665 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
666 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
667
Evan Cheng68ae5b42011-07-06 02:02:33 +0000668 void print(raw_ostream &O) {
Bill Wendling44dcfd32011-04-07 21:20:06 +0000669 if (Conds.empty() && ReqFeatures.empty()) {
670 O.indent(6) << "return true;\n";
Evan Cheng68ae5b42011-07-06 02:02:33 +0000671 return;
Bill Wendling44dcfd32011-04-07 21:20:06 +0000672 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000673
Bill Wendling44dcfd32011-04-07 21:20:06 +0000674 O << "if (";
Bill Wendling4962e612011-03-21 08:40:31 +0000675
676 for (std::vector<std::string>::iterator
677 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
678 if (I != Conds.begin()) {
679 O << " &&\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000680 O.indent(8);
Bill Wendling4962e612011-03-21 08:40:31 +0000681 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000682
Bill Wendling4962e612011-03-21 08:40:31 +0000683 O << *I;
684 }
685
Bill Wendling44dcfd32011-04-07 21:20:06 +0000686 O << ") {\n";
687 O.indent(6) << "// " << Result << "\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000688
Benjamin Kramer209a8c82013-09-11 15:42:16 +0000689 // Directly mangle mapped operands into the string. Each operand is
690 // identified by a '$' sign followed by a byte identifying the number of the
691 // operand. We add one to the index to avoid zero bytes.
692 std::pair<StringRef, StringRef> ASM = StringRef(AsmString).split(' ');
693 SmallString<128> OutString = ASM.first;
694 if (!ASM.second.empty()) {
695 raw_svector_ostream OS(OutString);
696 OS << ' ';
697 for (StringRef::iterator I = ASM.second.begin(), E = ASM.second.end();
698 I != E;) {
699 OS << *I;
700 if (*I == '$') {
701 StringRef::iterator Start = ++I;
702 while (I != E &&
703 ((*I >= 'a' && *I <= 'z') || (*I >= 'A' && *I <= 'Z') ||
704 (*I >= '0' && *I <= '9') || *I == '_'))
705 ++I;
706 StringRef Name(Start, I - Start);
707 assert(isOpMapped(Name) && "Unmapped operand!");
708 OS << format("\\x%02X", (unsigned char)getOpIndex(Name) + 1);
709 } else {
710 ++I;
711 }
712 }
713 }
714
715 // Emit the string.
716 O.indent(6) << "AsmString = \"" << OutString.str() << "\";\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000717
Bill Wendling44dcfd32011-04-07 21:20:06 +0000718 O.indent(6) << "break;\n";
719 O.indent(4) << '}';
Bill Wendling4962e612011-03-21 08:40:31 +0000720 }
721
722 bool operator==(const IAPrinter &RHS) {
723 if (Conds.size() != RHS.Conds.size())
724 return false;
725
726 unsigned Idx = 0;
727 for (std::vector<std::string>::iterator
728 I = Conds.begin(), E = Conds.end(); I != E; ++I)
729 if (*I != RHS.Conds[Idx++])
730 return false;
731
732 return true;
733 }
734
735 bool operator()(const IAPrinter &RHS) {
736 if (Conds.size() < RHS.Conds.size())
737 return true;
738
739 unsigned Idx = 0;
740 for (std::vector<std::string>::iterator
741 I = Conds.begin(), E = Conds.end(); I != E; ++I)
742 if (*I != RHS.Conds[Idx++])
743 return *I < RHS.Conds[Idx++];
744
745 return false;
746 }
747};
748
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000749} // end anonymous namespace
750
Bill Wendling740e5b32011-06-14 03:17:20 +0000751static unsigned CountNumOperands(StringRef AsmString) {
752 unsigned NumOps = 0;
753 std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
754
755 while (!ASM.second.empty()) {
756 ++NumOps;
757 ASM = ASM.second.split(' ');
758 }
759
760 return NumOps;
761}
762
Bill Wendling393c4042011-06-15 04:31:19 +0000763static unsigned CountResultNumOperands(StringRef AsmString) {
764 unsigned NumOps = 0;
765 std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
766
767 if (!ASM.second.empty()) {
768 size_t I = ASM.second.find('{');
769 StringRef Str = ASM.second;
770 if (I != StringRef::npos)
771 Str = ASM.second.substr(I, ASM.second.find('|', I));
772
773 ASM = Str.split(' ');
774
775 do {
776 ++NumOps;
777 ASM = ASM.second.split(' ');
778 } while (!ASM.second.empty());
779 }
780
781 return NumOps;
782}
Bill Wendling740e5b32011-06-14 03:17:20 +0000783
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000784void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
785 CodeGenTarget Target(Records);
786 Record *AsmWriter = Target.getAsmWriter();
787
Bill Wendling740e5b32011-06-14 03:17:20 +0000788 if (!AsmWriter->getValueAsBit("isMCAsmWriter"))
789 return;
790
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000791 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
792 O << "#undef PRINT_ALIAS_INSTR\n\n";
793
Bill Wendling7520e3a2011-02-26 03:09:12 +0000794 // Emit the method that prints the alias instruction.
795 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
796
Bill Wendling7520e3a2011-02-26 03:09:12 +0000797 std::vector<Record*> AllInstAliases =
798 Records.getAllDerivedDefinitions("InstAlias");
799
800 // Create a map from the qualified name to a list of potential matches.
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000801 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
Bill Wendling7520e3a2011-02-26 03:09:12 +0000802 for (std::vector<Record*>::iterator
803 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
804 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
805 const Record *R = *I;
Bill Wendlingeef965f2011-04-13 23:36:21 +0000806 if (!R->getValueAsBit("EmitAlias"))
807 continue; // We were told not to emit the alias, but to emit the aliasee.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000808 const DagInit *DI = R->getValueAsDag("ResultInst");
Sean Silva3f7b7f82012-10-10 20:24:47 +0000809 const DefInit *Op = cast<DefInit>(DI->getOperator());
Bill Wendling7520e3a2011-02-26 03:09:12 +0000810 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
811 }
812
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000813 // A map of which conditions need to be met for each instruction operand
814 // before it can be matched to the mnemonic.
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000815 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000816
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000817 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000818 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
819 std::vector<CodeGenInstAlias*> &Aliases = I->second;
820
821 for (std::vector<CodeGenInstAlias*>::iterator
822 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
823 const CodeGenInstAlias *CGA = *II;
Bill Wendling740e5b32011-06-14 03:17:20 +0000824 unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
Bill Wendling393c4042011-06-15 04:31:19 +0000825 unsigned NumResultOps =
826 CountResultNumOperands(CGA->ResultInst->AsmString);
Bill Wendling740e5b32011-06-14 03:17:20 +0000827
828 // Don't emit the alias if it has more operands than what it's aliasing.
Bill Wendling393c4042011-06-15 04:31:19 +0000829 if (NumResultOps < CountNumOperands(CGA->AsmString))
Bill Wendling740e5b32011-06-14 03:17:20 +0000830 continue;
831
Evan Cheng68ae5b42011-07-06 02:02:33 +0000832 IAPrinter *IAP = new IAPrinter(CGA->Result->getAsString(),
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000833 CGA->AsmString);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000834
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000835 std::string Cond;
836 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
837 IAP->addCond(Cond);
838
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000839 bool CantHandle = false;
840
841 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
842 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
843
844 switch (RO.Kind) {
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000845 case CodeGenInstAlias::ResultOperand::K_Record: {
846 const Record *Rec = RO.getRecord();
847 StringRef ROName = RO.getName();
848
Owen Andersonbea6f612011-06-27 21:06:21 +0000849
850 if (Rec->isSubClassOf("RegisterOperand"))
851 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000852 if (Rec->isSubClassOf("RegisterClass")) {
853 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
854 IAP->addCond(Cond);
855
856 if (!IAP->isOpMapped(ROName)) {
857 IAP->addOperand(ROName, i);
Jack Carter37ef65b2013-02-05 08:32:10 +0000858 Record *R = CGA->ResultOperands[i].getRecord();
859 if (R->isSubClassOf("RegisterOperand"))
860 R = R->getValueAsDef("RegClass");
Benjamin Kramercef670a2012-03-30 23:13:40 +0000861 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
Jack Carter37ef65b2013-02-05 08:32:10 +0000862 R->getName() + "RegClassID)"
Benjamin Kramercef670a2012-03-30 23:13:40 +0000863 ".contains(MI->getOperand(" + llvm::utostr(i) + ").getReg())";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000864 IAP->addCond(Cond);
865 } else {
866 Cond = std::string("MI->getOperand(") +
867 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
868 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
869 IAP->addCond(Cond);
870 }
871 } else {
872 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
Bill Wendling740e5b32011-06-14 03:17:20 +0000873 // FIXME: We may need to handle these situations.
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000874 delete IAP;
875 IAP = 0;
876 CantHandle = true;
877 break;
878 }
879
880 break;
881 }
Tim Northover7bf2e1b2013-01-09 13:32:04 +0000882 case CodeGenInstAlias::ResultOperand::K_Imm: {
883 std::string Op = "MI->getOperand(" + llvm::utostr(i) + ")";
884
885 // Just because the alias has an immediate result, doesn't mean the
886 // MCInst will. An MCExpr could be present, for example.
887 IAP->addCond(Op + ".isImm()");
888
889 Cond = Op + ".getImm() == "
890 + llvm::utostr(CGA->ResultOperands[i].getImm());
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000891 IAP->addCond(Cond);
892 break;
Tim Northover7bf2e1b2013-01-09 13:32:04 +0000893 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000894 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbachbfc94292011-11-15 01:46:57 +0000895 // If this is zero_reg, something's playing tricks we're not
896 // equipped to handle.
897 if (!CGA->ResultOperands[i].getRegister()) {
898 CantHandle = true;
899 break;
900 }
901
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000902 Cond = std::string("MI->getOperand(") +
903 llvm::utostr(i) + ").getReg() == " + Target.getName() +
904 "::" + CGA->ResultOperands[i].getRegister()->getName();
905 IAP->addCond(Cond);
906 break;
907 }
908
909 if (!IAP) break;
910 }
911
912 if (CantHandle) continue;
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000913 IAPrinterMap[I->first].push_back(IAP);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000914 }
915 }
916
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000917 std::string Header;
918 raw_string_ostream HeaderO(Header);
919
920 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendling740e5b32011-06-14 03:17:20 +0000921 << "::printAliasInstr(const MCInst"
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000922 << " *MI, raw_ostream &OS) {\n";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000923
Bill Wendling44dcfd32011-04-07 21:20:06 +0000924 std::string Cases;
925 raw_string_ostream CasesO(Cases);
926
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000927 for (std::map<std::string, std::vector<IAPrinter*> >::iterator
Bill Wendling44dcfd32011-04-07 21:20:06 +0000928 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
929 std::vector<IAPrinter*> &IAPs = I->second;
930 std::vector<IAPrinter*> UniqueIAPs;
931
932 for (std::vector<IAPrinter*>::iterator
933 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
934 IAPrinter *LHS = *II;
935 bool IsDup = false;
936 for (std::vector<IAPrinter*>::iterator
937 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
938 IAPrinter *RHS = *III;
939 if (LHS != RHS && *LHS == *RHS) {
940 IsDup = true;
941 break;
942 }
943 }
944
945 if (!IsDup) UniqueIAPs.push_back(LHS);
946 }
947
948 if (UniqueIAPs.empty()) continue;
949
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000950 CasesO.indent(2) << "case " << I->first << ":\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000951
952 for (std::vector<IAPrinter*>::iterator
953 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
954 IAPrinter *IAP = *II;
955 CasesO.indent(4);
Evan Cheng68ae5b42011-07-06 02:02:33 +0000956 IAP->print(CasesO);
Bill Wendling44dcfd32011-04-07 21:20:06 +0000957 CasesO << '\n';
958 }
959
Eric Christopher721ef662011-04-18 21:28:11 +0000960 CasesO.indent(4) << "return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000961 }
962
Bill Wendling740e5b32011-06-14 03:17:20 +0000963 if (CasesO.str().empty()) {
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000964 O << HeaderO.str();
Eric Christopher721ef662011-04-18 21:28:11 +0000965 O << " return false;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000966 O << "}\n\n";
967 O << "#endif // PRINT_ALIAS_INSTR\n";
968 return;
969 }
970
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000971 O << HeaderO.str();
Benjamin Kramer209a8c82013-09-11 15:42:16 +0000972 O.indent(2) << "const char *AsmString;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000973 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher721ef662011-04-18 21:28:11 +0000974 O.indent(2) << "default: return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000975 O << CasesO.str();
976 O.indent(2) << "}\n\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000977
978 // Code that prints the alias, replacing the operands with the ones from the
979 // MCInst.
Benjamin Kramer209a8c82013-09-11 15:42:16 +0000980 O << " unsigned I = 0;\n";
981 O << " while (AsmString[I] != ' ' && AsmString[I] != '\\0')\n";
982 O << " ++I;\n";
983 O << " OS << '\\t' << StringRef(AsmString, I);\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000984
Benjamin Kramer209a8c82013-09-11 15:42:16 +0000985 O << " if (AsmString[I] != '\\0') {\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000986 O << " OS << '\\t';\n";
Benjamin Kramer209a8c82013-09-11 15:42:16 +0000987 O << " do {\n";
988 O << " if (AsmString[I] == '$') {\n";
989 O << " ++I;\n";
990 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, OS);\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000991 O << " } else {\n";
Benjamin Kramer209a8c82013-09-11 15:42:16 +0000992 O << " OS << AsmString[I++];\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000993 O << " }\n";
Benjamin Kramer209a8c82013-09-11 15:42:16 +0000994 O << " } while (AsmString[I] != '\\0');\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000995 O << " }\n\n";
Jim Grosbach016c6792012-04-18 18:56:33 +0000996
Eric Christopher721ef662011-04-18 21:28:11 +0000997 O << " return true;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000998 O << "}\n\n";
999
1000 O << "#endif // PRINT_ALIAS_INSTR\n";
1001}
Chris Lattner05af2612009-09-13 20:08:00 +00001002
1003void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner05af2612009-09-13 20:08:00 +00001004 EmitPrintInstruction(O);
1005 EmitGetRegisterName(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +00001006 EmitPrintAliasInstruction(O);
Chris Lattner05af2612009-09-13 20:08:00 +00001007}
1008
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001009
1010namespace llvm {
1011
1012void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1013 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1014 AsmWriterEmitter(RK).run(OS);
1015}
1016
1017} // End llvm namespace