blob: ac8d896d36472cd2b02ed198182daba88e40d446 [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"
21#include "llvm/Support/MathExtras.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000022#include "llvm/TableGen/Error.h"
23#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000024#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohen615ed992005-01-22 18:50:10 +000025#include <algorithm>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000026#include <cassert>
27#include <map>
28#include <vector>
Chris Lattner2e1f51b2004-08-01 05:59:33 +000029using namespace llvm;
30
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000031namespace {
32class AsmWriterEmitter {
33 RecordKeeper &Records;
34 std::map<const CodeGenInstruction*, AsmWriterInst*> CGIAWIMap;
35 std::vector<const CodeGenInstruction*> NumberedInstructions;
36public:
37 AsmWriterEmitter(RecordKeeper &R) : Records(R) {}
38
39 void run(raw_ostream &o);
40
41private:
42 void EmitPrintInstruction(raw_ostream &o);
43 void EmitGetRegisterName(raw_ostream &o);
44 void EmitPrintAliasInstruction(raw_ostream &O);
45
46 AsmWriterInst *getAsmWriterInstByID(unsigned ID) const {
47 assert(ID < NumberedInstructions.size());
48 std::map<const CodeGenInstruction*, AsmWriterInst*>::const_iterator I =
49 CGIAWIMap.find(NumberedInstructions[ID]);
50 assert(I != CGIAWIMap.end() && "Didn't find inst!");
51 return I->second;
52 }
53 void FindUniqueOperandCommands(std::vector<std::string> &UOC,
54 std::vector<unsigned> &InstIdxs,
55 std::vector<unsigned> &InstOpsUsed) const;
56};
57} // end anonymous namespace
58
Chris Lattner38c07512005-01-22 20:31:17 +000059static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +000060 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +000061 O << " case " << OpsToPrint.back().first << ": ";
62 AsmWriterOperand TheOp = OpsToPrint.back().second;
63 OpsToPrint.pop_back();
64
65 // Check to see if any other operands are identical in this list, and if so,
66 // emit a case label for them.
67 for (unsigned i = OpsToPrint.size(); i != 0; --i)
68 if (OpsToPrint[i-1].second == TheOp) {
69 O << "\n case " << OpsToPrint[i-1].first << ": ";
70 OpsToPrint.erase(OpsToPrint.begin()+i-1);
71 }
72
73 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000074 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +000075 O << "break;\n";
76}
77
Chris Lattner870c0162005-01-22 18:38:13 +000078
79/// EmitInstructions - Emit the last instruction in the vector and any other
80/// instructions that are suitably similar to it.
81static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +000082 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +000083 AsmWriterInst FirstInst = Insts.back();
84 Insts.pop_back();
85
86 std::vector<AsmWriterInst> SimilarInsts;
87 unsigned DifferingOperand = ~0;
88 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +000089 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
90 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +000091 if (DifferingOperand == ~0U) // First match!
92 DifferingOperand = DiffOp;
93
94 // If this differs in the same operand as the rest of the instructions in
95 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +000096 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +000097 SimilarInsts.push_back(Insts[i-1]);
98 Insts.erase(Insts.begin()+i-1);
99 }
100 }
101 }
102
Chris Lattnera1e8a802006-05-01 17:01:17 +0000103 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000104 << FirstInst.CGI->TheDef->getName() << ":\n";
105 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +0000106 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +0000107 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
108 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
109 if (i != DifferingOperand) {
110 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000111 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +0000112 } else {
113 // If this is the operand that varies between all of the instructions,
114 // emit a switch for just this operand now.
115 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +0000116 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +0000117 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +0000118 FirstInst.CGI->TheDef->getName(),
119 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000120
Chris Lattner870c0162005-01-22 18:38:13 +0000121 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +0000122 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +0000123 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +0000124 AWI.CGI->TheDef->getName(),
125 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +0000126 }
Chris Lattner38c07512005-01-22 20:31:17 +0000127 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
128 while (!OpsToPrint.empty())
129 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000130 O << " }";
131 }
132 O << "\n";
133 }
Chris Lattner870c0162005-01-22 18:38:13 +0000134 O << " break;\n";
135}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000136
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000137void AsmWriterEmitter::
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000138FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000139 std::vector<unsigned> &InstIdxs,
140 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000141 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000142
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000143 // This vector parallels UniqueOperandCommands, keeping track of which
144 // instructions each case are used for. It is a comma separated string of
145 // enums.
146 std::vector<std::string> InstrsForCase;
147 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000148 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000149
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000150 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
151 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Bill Wendlingb9449d62010-07-16 23:10:00 +0000152 if (Inst == 0) continue; // PHI, INLINEASM, PROLOG_LABEL, etc.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000153
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000154 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000155 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000156 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000157
Chris Lattnerb8462862006-07-18 17:56:07 +0000158 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000159
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000160 // Check to see if we already have 'Command' in UniqueOperandCommands.
161 // If not, add it.
162 bool FoundIt = false;
163 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
164 if (UniqueOperandCommands[idx] == Command) {
165 InstIdxs[i] = idx;
166 InstrsForCase[idx] += ", ";
167 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
168 FoundIt = true;
169 break;
170 }
171 if (!FoundIt) {
172 InstIdxs[i] = UniqueOperandCommands.size();
173 UniqueOperandCommands.push_back(Command);
174 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000175
176 // This command matches one operand so far.
177 InstOpsUsed.push_back(1);
178 }
179 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000180
Chris Lattner96c1ade2006-07-18 18:28:27 +0000181 // For each entry of UniqueOperandCommands, there is a set of instructions
182 // that uses it. If the next command of all instructions in the set are
183 // identical, fold it into the command.
184 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
185 CommandIdx != e; ++CommandIdx) {
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000186
Chris Lattner96c1ade2006-07-18 18:28:27 +0000187 for (unsigned Op = 1; ; ++Op) {
188 // Scan for the first instruction in the set.
189 std::vector<unsigned>::iterator NIT =
190 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
191 if (NIT == InstIdxs.end()) break; // No commonality.
192
193 // If this instruction has no more operands, we isn't anything to merge
194 // into this command.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000195 const AsmWriterInst *FirstInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000196 getAsmWriterInstByID(NIT-InstIdxs.begin());
197 if (!FirstInst || FirstInst->Operands.size() == Op)
198 break;
199
200 // Otherwise, scan to see if all of the other instructions in this command
201 // set share the operand.
202 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000203 // Keep track of the maximum, number of operands or any
204 // instruction we see in the group.
205 size_t MaxSize = FirstInst->Operands.size();
206
Chris Lattner96c1ade2006-07-18 18:28:27 +0000207 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
208 NIT != InstIdxs.end();
209 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
210 // Okay, found another instruction in this command set. If the operand
211 // matches, we're ok, otherwise bail out.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000212 const AsmWriterInst *OtherInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000213 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000214
215 if (OtherInst &&
216 OtherInst->Operands.size() > FirstInst->Operands.size())
217 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
218
Chris Lattner96c1ade2006-07-18 18:28:27 +0000219 if (!OtherInst || OtherInst->Operands.size() == Op ||
220 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
221 AllSame = false;
222 break;
223 }
224 }
225 if (!AllSame) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000226
Chris Lattner96c1ade2006-07-18 18:28:27 +0000227 // Okay, everything in this command set has the same next operand. Add it
228 // to UniqueOperandCommands and remember that it was consumed.
229 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000230
Chris Lattner96c1ade2006-07-18 18:28:27 +0000231 UniqueOperandCommands[CommandIdx] += Command;
232 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000233 }
234 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000235
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000236 // Prepend some of the instructions each case is used for onto the case val.
237 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
238 std::string Instrs = InstrsForCase[i];
239 if (Instrs.size() > 70) {
240 Instrs.erase(Instrs.begin()+70, Instrs.end());
241 Instrs += "...";
242 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000243
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000244 if (!Instrs.empty())
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000245 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000246 UniqueOperandCommands[i];
247 }
248}
249
250
Daniel Dunbar9bd34602009-10-17 20:43:42 +0000251static void UnescapeString(std::string &Str) {
252 for (unsigned i = 0; i != Str.size(); ++i) {
253 if (Str[i] == '\\' && i != Str.size()-1) {
254 switch (Str[i+1]) {
255 default: continue; // Don't execute the code after the switch.
256 case 'a': Str[i] = '\a'; break;
257 case 'b': Str[i] = '\b'; break;
258 case 'e': Str[i] = 27; break;
259 case 'f': Str[i] = '\f'; break;
260 case 'n': Str[i] = '\n'; break;
261 case 'r': Str[i] = '\r'; break;
262 case 't': Str[i] = '\t'; break;
263 case 'v': Str[i] = '\v'; break;
264 case '"': Str[i] = '\"'; break;
265 case '\'': Str[i] = '\''; break;
266 case '\\': Str[i] = '\\'; break;
267 }
268 // Nuke the second character.
269 Str.erase(Str.begin()+i+1);
270 }
271 }
272}
273
Chris Lattner05af2612009-09-13 20:08:00 +0000274/// EmitPrintInstruction - Generate the code for the "printInstruction" method
275/// implementation.
276void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000277 CodeGenTarget Target(Records);
Chris Lattner175580c2004-08-14 22:50:53 +0000278 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000279 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jim Grosbachca96a862010-09-30 01:29:54 +0000280 bool isMC = AsmWriter->getValueAsBit("isMCAsmWriter");
281 const char *MachineInstrClassName = isMC ? "MCInst" : "MachineInstr";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000282
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000283 O <<
284 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000285 "/// from the instruction set description.\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000286 "void " << Target.getName() << ClassName
Jim Grosbachca96a862010-09-30 01:29:54 +0000287 << "::printInstruction(const " << MachineInstrClassName
288 << " *MI, raw_ostream &O) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000289
Chris Lattner5765dba2005-01-22 17:40:38 +0000290 std::vector<AsmWriterInst> Instructions;
291
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000292 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
293 E = Target.inst_end(); I != E; ++I)
Chris Lattner6a91b182010-03-19 01:00:55 +0000294 if (!(*I)->AsmString.empty() &&
295 (*I)->TheDef->getName() != "PHI")
Sean Callanand0bc7f02010-02-09 23:06:35 +0000296 Instructions.push_back(
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000297 AsmWriterInst(**I,
Sean Callanand0bc7f02010-02-09 23:06:35 +0000298 AsmWriter->getValueAsInt("Variant"),
299 AsmWriter->getValueAsInt("FirstOperandColumn"),
300 AsmWriter->getValueAsInt("OperandSpacing")));
Chris Lattner076efa72004-08-01 07:43:02 +0000301
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000302 // Get the instruction numbering.
Chris Lattnerf6502782010-03-19 00:34:35 +0000303 NumberedInstructions = Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000304
Chris Lattner6af022f2006-07-14 22:59:11 +0000305 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
306 // all machine instructions are necessarily being printed, so there may be
307 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000308 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
309 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000310
Chris Lattner6af022f2006-07-14 22:59:11 +0000311 // Build an aggregate string, and build a table of offsets into it.
Benjamin Kramer94338592012-04-02 09:13:46 +0000312 SequenceToOffsetTable<std::string> StringTable;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000313
Chris Lattner259bda42006-09-27 16:44:09 +0000314 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000315 /// chunk of the output as well as indices used for operand printing.
Manman Ren6579cf82012-09-13 17:43:46 +0000316 /// To reduce the number of unhandled cases, we expand the size from 32-bit
317 /// to 32+16 = 48-bit.
Craig Topperf4d78242012-09-14 08:33:11 +0000318 std::vector<uint64_t> OpcodeInfo;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000319
Benjamin Kramer94338592012-04-02 09:13:46 +0000320 // Add all strings to the string table upfront so it can generate an optimized
321 // representation.
322 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
323 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
324 if (AWI != 0 &&
Jim Grosbach016c6792012-04-18 18:56:33 +0000325 AWI->Operands[0].OperandType ==
326 AsmWriterOperand::isLiteralTextOperand &&
Benjamin Kramer94338592012-04-02 09:13:46 +0000327 !AWI->Operands[0].Str.empty()) {
328 std::string Str = AWI->Operands[0].Str;
329 UnescapeString(Str);
330 StringTable.add(Str);
331 }
332 }
333
334 StringTable.layout();
335
Chris Lattner55616402006-07-18 17:32:27 +0000336 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000337 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
338 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
339 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000340 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000341 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000342 Idx = ~0U;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000343 } else if (AWI->Operands[0].OperandType !=
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000344 AsmWriterOperand::isLiteralTextOperand ||
345 AWI->Operands[0].Str.empty()) {
346 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer94338592012-04-02 09:13:46 +0000347 Idx = StringTable.get("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000348 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000349 std::string Str = AWI->Operands[0].Str;
350 UnescapeString(Str);
Benjamin Kramer94338592012-04-02 09:13:46 +0000351 Idx = StringTable.get(Str);
Chris Lattner3200fc92009-09-14 01:16:36 +0000352 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000353
Chris Lattner6af022f2006-07-14 22:59:11 +0000354 // Nuke the string from the operand list. It is now handled!
355 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000356 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000357
Chris Lattner3200fc92009-09-14 01:16:36 +0000358 // Bias offset by one since we want 0 as a sentinel.
Craig Topperf4d78242012-09-14 08:33:11 +0000359 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000360 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000361
Chris Lattner55616402006-07-18 17:32:27 +0000362 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000363 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000364
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000365 // To reduce code size, we compactify common instructions into a few bits
366 // in the opcode-indexed table.
Craig Topperf4d78242012-09-14 08:33:11 +0000367 unsigned BitsLeft = 64-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000368
369 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000370
Chris Lattnerb8462862006-07-18 17:56:07 +0000371 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000372 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000373 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000374 std::vector<unsigned> NumInstOpsHandled;
375 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
376 NumInstOpsHandled);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000377
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000378 // If we ran out of operands to print, we're done.
379 if (UniqueOperandCommands.empty()) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000380
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000381 // Compute the number of bits we need to represent these cases, this is
382 // ceil(log2(numentries)).
383 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000384
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000385 // If we don't have enough bits for this operand, don't include it.
386 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000387 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
388 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000389 break;
390 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000391
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000392 // Otherwise, we can include this in the initial lookup table. Add it in.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000393 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Manman Ren6579cf82012-09-13 17:43:46 +0000394 if (InstIdxs[i] != ~0U) {
Craig Topperf4d78242012-09-14 08:33:11 +0000395 OpcodeInfo[i] |= (uint64_t)InstIdxs[i] << (64-BitsLeft);
Manman Ren6579cf82012-09-13 17:43:46 +0000396 }
Craig Topperf4d78242012-09-14 08:33:11 +0000397 BitsLeft -= NumBits;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000398
Chris Lattnerb8462862006-07-18 17:56:07 +0000399 // Remove the info about this operand.
400 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
401 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000402 if (!Inst->Operands.empty()) {
403 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000404 assert(NumOps <= Inst->Operands.size() &&
405 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000406 Inst->Operands.erase(Inst->Operands.begin(),
407 Inst->Operands.begin()+NumOps);
408 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000409 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000410
Chris Lattnerb8462862006-07-18 17:56:07 +0000411 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000412 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
413 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000414
415
Craig Topperf4d78242012-09-14 08:33:11 +0000416 // We always emit at least one 32-bit table. A second table is emitted if
417 // more bits are needed.
418 O<<" static const uint32_t OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000419 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Craig Topperf4d78242012-09-14 08:33:11 +0000420 O << " " << (OpcodeInfo[i] & 0xffffffff) << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000421 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000422 }
423 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000424 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000425 O << " };\n\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000426
Craig Topperf4d78242012-09-14 08:33:11 +0000427 if (BitsLeft < 32) {
Manman Ren6579cf82012-09-13 17:43:46 +0000428 // Add a second OpInfo table only when it is necessary.
Craig Topperf4d78242012-09-14 08:33:11 +0000429 // Adjust the type of the second table based on the number of bits needed.
430 O << " static const uint"
431 << ((BitsLeft < 16) ? "32" : (BitsLeft < 24) ? "16" : "8")
432 << "_t OpInfo2[] = {\n";
Manman Ren6579cf82012-09-13 17:43:46 +0000433 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Craig Topperf4d78242012-09-14 08:33:11 +0000434 O << " " << (OpcodeInfo[i] >> 32) << "U,\t// "
Manman Ren6579cf82012-09-13 17:43:46 +0000435 << NumberedInstructions[i]->TheDef->getName() << "\n";
436 }
437 // Add a dummy entry so the array init doesn't end with a comma.
438 O << " 0U\n";
439 O << " };\n\n";
440 }
441
Chris Lattner6af022f2006-07-14 22:59:11 +0000442 // Emit the string itself.
Benjamin Kramer94338592012-04-02 09:13:46 +0000443 O << " const char AsmStrs[] = {\n";
444 StringTable.emit(O, printChar);
445 O << " };\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000446
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000447 O << " O << \"\\t\";\n\n";
448
Craig Topperf4d78242012-09-14 08:33:11 +0000449 O << " // Emit the opcode for the instruction.\n";
450 if (BitsLeft < 32) {
451 // If we have two tables then we need to perform two lookups and combine
452 // the results into a single 64-bit value.
453 O << " uint64_t Bits1 = OpInfo[MI->getOpcode()];\n"
454 << " uint64_t Bits2 = OpInfo2[MI->getOpcode()];\n"
455 << " uint64_t Bits = (Bits2 << 32) | Bits1;\n";
456 } else {
457 // If only one table is used we just need to perform a single lookup.
458 O << " uint32_t Bits = OpInfo[MI->getOpcode()];\n";
459 }
Manman Ren6579cf82012-09-13 17:43:46 +0000460 O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000461 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000462
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000463 // Output the table driven operand information.
Craig Topperf4d78242012-09-14 08:33:11 +0000464 BitsLeft = 64-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000465 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
466 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
467
468 // Compute the number of bits we need to represent these cases, this is
469 // ceil(log2(numentries)).
470 unsigned NumBits = Log2_32_Ceil(Commands.size());
471 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000472
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000473 // Emit code to extract this field from Bits.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000474 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000475 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000476
Chris Lattner96c1ade2006-07-18 18:28:27 +0000477 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000478 // Emit two possibilitys with if/else.
Craig Topperf4d78242012-09-14 08:33:11 +0000479 O << " if ((Bits >> "
480 << (64-BitsLeft) << ") & "
Chris Lattnere7a589d2006-07-18 17:43:54 +0000481 << ((1 << NumBits)-1) << ") {\n"
482 << Commands[1]
483 << " } else {\n"
484 << Commands[0]
485 << " }\n\n";
Eric Christopher16870502010-09-18 18:50:27 +0000486 } else if (Commands.size() == 1) {
487 // Emit a single possibility.
488 O << Commands[0] << "\n\n";
Chris Lattnere7a589d2006-07-18 17:43:54 +0000489 } else {
Craig Topperf4d78242012-09-14 08:33:11 +0000490 O << " switch ((Bits >> "
491 << (64-BitsLeft) << ") & "
Chris Lattnere7a589d2006-07-18 17:43:54 +0000492 << ((1 << NumBits)-1) << ") {\n"
493 << " default: // unreachable.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000494
Chris Lattnere7a589d2006-07-18 17:43:54 +0000495 // Print out all the cases.
496 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
497 O << " case " << i << ":\n";
498 O << Commands[i];
499 O << " break;\n";
500 }
501 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000502 }
Craig Topperf4d78242012-09-14 08:33:11 +0000503 BitsLeft -= NumBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000504 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000505
Chris Lattnerb8462862006-07-18 17:56:07 +0000506 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000507 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
508 // Entire instruction has been emitted?
509 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000510 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000511 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000512 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000513 }
514 }
515
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000516
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000517 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000518 // elements in the vector.
519 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000520
521
Chris Lattner70067602009-09-18 18:10:19 +0000522 // Now that we've emitted all of the operand info that fit into 32 bits, emit
523 // information for those instructions that are left. This is a less dense
524 // encoding, but we expect the main 32-bit table to handle the majority of
525 // instructions.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000526 if (!Instructions.empty()) {
527 // Find the opcode # of inline asm.
528 O << " switch (MI->getOpcode()) {\n";
529 while (!Instructions.empty())
530 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000531
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000532 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000533 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000534 }
David Greenec8d06052009-07-29 20:10:24 +0000535
Chris Lattner0a012122006-07-18 19:06:01 +0000536 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000537}
Chris Lattner05af2612009-09-13 20:08:00 +0000538
Owen Andersonbea6f612011-06-27 21:06:21 +0000539static void
540emitRegisterNameString(raw_ostream &O, StringRef AltName,
Craig Topper9b1b25f2012-04-03 06:52:47 +0000541 const std::vector<CodeGenRegister*> &Registers) {
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000542 SequenceToOffsetTable<std::string> StringTable;
543 SmallVector<std::string, 4> AsmNames(Registers.size());
Owen Andersonbea6f612011-06-27 21:06:21 +0000544 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
545 const CodeGenRegister &Reg = *Registers[i];
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000546 std::string &AsmName = AsmNames[i];
Owen Andersonbea6f612011-06-27 21:06:21 +0000547
Owen Andersonbea6f612011-06-27 21:06:21 +0000548 // "NoRegAltName" is special. We don't need to do a lookup for that,
549 // as it's just a reference to the default register name.
550 if (AltName == "" || AltName == "NoRegAltName") {
551 AsmName = Reg.TheDef->getValueAsString("AsmName");
552 if (AsmName.empty())
553 AsmName = Reg.getName();
554 } else {
555 // Make sure the register has an alternate name for this index.
556 std::vector<Record*> AltNameList =
557 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
558 unsigned Idx = 0, e;
559 for (e = AltNameList.size();
560 Idx < e && (AltNameList[Idx]->getName() != AltName);
561 ++Idx)
562 ;
563 // If the register has an alternate name for this index, use it.
564 // Otherwise, leave it empty as an error flag.
565 if (Idx < e) {
566 std::vector<std::string> AltNames =
567 Reg.TheDef->getValueAsListOfStrings("AltNames");
568 if (AltNames.size() <= Idx)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000569 PrintFatalError(Reg.TheDef->getLoc(),
570 (Twine("Register definition missing alt name for '") +
571 AltName + "'.").str());
Owen Andersonbea6f612011-06-27 21:06:21 +0000572 AsmName = AltNames[Idx];
573 }
574 }
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000575 StringTable.add(AsmName);
576 }
Owen Andersonbea6f612011-06-27 21:06:21 +0000577
Craig Topper5974c312012-09-15 01:22:42 +0000578 StringTable.layout();
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000579 O << " static const char AsmStrs" << AltName << "[] = {\n";
580 StringTable.emit(O, printChar);
581 O << " };\n\n";
582
Craig Topper5974c312012-09-15 01:22:42 +0000583 O << " static const uint32_t RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000584 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Toppera4bd58b2012-04-02 00:47:39 +0000585 if ((i % 14) == 0)
586 O << "\n ";
587 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersonbea6f612011-06-27 21:06:21 +0000588 }
Craig Topper9b1b25f2012-04-03 06:52:47 +0000589 O << "\n };\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000590 << "\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000591}
Chris Lattner05af2612009-09-13 20:08:00 +0000592
593void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000594 CodeGenTarget Target(Records);
Chris Lattner05af2612009-09-13 20:08:00 +0000595 Record *AsmWriter = Target.getAsmWriter();
596 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000597 const std::vector<CodeGenRegister*> &Registers =
598 Target.getRegBank().getRegisters();
Owen Andersonbea6f612011-06-27 21:06:21 +0000599 std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices();
600 bool hasAltNames = AltNameIndices.size() > 1;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000601
Chris Lattner05af2612009-09-13 20:08:00 +0000602 O <<
603 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
604 "/// from the register set description. This returns the assembler name\n"
605 "/// for the specified register.\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000606 "const char *" << Target.getName() << ClassName << "::";
607 if (hasAltNames)
608 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
609 else
610 O << "getRegisterName(unsigned RegNo) {\n";
611 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
612 << " && \"Invalid register number!\");\n"
Chris Lattnerf6761be2009-09-14 01:26:18 +0000613 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000614
Owen Andersonbea6f612011-06-27 21:06:21 +0000615 if (hasAltNames) {
616 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i)
617 emitRegisterNameString(O, AltNameIndices[i]->getName(), Registers);
618 } else
619 emitRegisterNameString(O, "", Registers);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000620
Owen Andersonbea6f612011-06-27 21:06:21 +0000621 if (hasAltNames) {
Craig Topper5974c312012-09-15 01:22:42 +0000622 O << " const uint32_t *RegAsmOffset;\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000623 << " const char *AsmStrs;\n"
624 << " switch(AltIdx) {\n"
Craig Topper655b8de2012-02-05 07:21:30 +0000625 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000626 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) {
627 StringRef Namespace = AltNameIndices[1]->getValueAsString("Namespace");
628 StringRef AltName(AltNameIndices[i]->getName());
629 O << " case " << Namespace << "::" << AltName
630 << ":\n"
631 << " AsmStrs = AsmStrs" << AltName << ";\n"
632 << " RegAsmOffset = RegAsmOffset" << AltName << ";\n"
633 << " break;\n";
634 }
635 O << "}\n";
636 }
637
638 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
639 << " \"Invalid alt name index for register!\");\n"
640 << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000641 << "}\n";
642}
643
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000644namespace {
Bill Wendling4962e612011-03-21 08:40:31 +0000645// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
646// they both have the same conditionals. In which case, we cannot print out the
647// alias for that pattern.
648class IAPrinter {
Bill Wendling4962e612011-03-21 08:40:31 +0000649 std::vector<std::string> Conds;
650 std::map<StringRef, unsigned> OpMap;
651 std::string Result;
652 std::string AsmString;
Jim Grosbacha5b06852012-04-18 19:02:43 +0000653 SmallVector<Record*, 4> ReqFeatures;
Bill Wendling4962e612011-03-21 08:40:31 +0000654public:
Evan Cheng68ae5b42011-07-06 02:02:33 +0000655 IAPrinter(std::string R, std::string AS)
656 : Result(R), AsmString(AS) {}
Bill Wendling4962e612011-03-21 08:40:31 +0000657
658 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling4962e612011-03-21 08:40:31 +0000659
660 void addOperand(StringRef Op, unsigned Idx) { OpMap[Op] = Idx; }
661 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
662 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
663
Evan Cheng68ae5b42011-07-06 02:02:33 +0000664 void print(raw_ostream &O) {
Bill Wendling44dcfd32011-04-07 21:20:06 +0000665 if (Conds.empty() && ReqFeatures.empty()) {
666 O.indent(6) << "return true;\n";
Evan Cheng68ae5b42011-07-06 02:02:33 +0000667 return;
Bill Wendling44dcfd32011-04-07 21:20:06 +0000668 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000669
Bill Wendling44dcfd32011-04-07 21:20:06 +0000670 O << "if (";
Bill Wendling4962e612011-03-21 08:40:31 +0000671
672 for (std::vector<std::string>::iterator
673 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
674 if (I != Conds.begin()) {
675 O << " &&\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000676 O.indent(8);
Bill Wendling4962e612011-03-21 08:40:31 +0000677 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000678
Bill Wendling4962e612011-03-21 08:40:31 +0000679 O << *I;
680 }
681
Bill Wendling44dcfd32011-04-07 21:20:06 +0000682 O << ") {\n";
683 O.indent(6) << "// " << Result << "\n";
684 O.indent(6) << "AsmString = \"" << AsmString << "\";\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000685
686 for (std::map<StringRef, unsigned>::iterator
687 I = OpMap.begin(), E = OpMap.end(); I != E; ++I)
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000688 O.indent(6) << "OpMap.push_back(std::make_pair(\"" << I->first << "\", "
689 << I->second << "));\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000690
Bill Wendling44dcfd32011-04-07 21:20:06 +0000691 O.indent(6) << "break;\n";
692 O.indent(4) << '}';
Bill Wendling4962e612011-03-21 08:40:31 +0000693 }
694
695 bool operator==(const IAPrinter &RHS) {
696 if (Conds.size() != RHS.Conds.size())
697 return false;
698
699 unsigned Idx = 0;
700 for (std::vector<std::string>::iterator
701 I = Conds.begin(), E = Conds.end(); I != E; ++I)
702 if (*I != RHS.Conds[Idx++])
703 return false;
704
705 return true;
706 }
707
708 bool operator()(const IAPrinter &RHS) {
709 if (Conds.size() < RHS.Conds.size())
710 return true;
711
712 unsigned Idx = 0;
713 for (std::vector<std::string>::iterator
714 I = Conds.begin(), E = Conds.end(); I != E; ++I)
715 if (*I != RHS.Conds[Idx++])
716 return *I < RHS.Conds[Idx++];
717
718 return false;
719 }
720};
721
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000722} // end anonymous namespace
723
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000724static void EmitGetMapOperandNumber(raw_ostream &O) {
725 O << "static unsigned getMapOperandNumber("
726 << "const SmallVectorImpl<std::pair<StringRef, unsigned> > &OpMap,\n";
727 O << " StringRef Name) {\n";
728 O << " for (SmallVectorImpl<std::pair<StringRef, unsigned> >::"
729 << "const_iterator\n";
730 O << " I = OpMap.begin(), E = OpMap.end(); I != E; ++I)\n";
731 O << " if (I->first == Name)\n";
732 O << " return I->second;\n";
Craig Topper58609b72012-04-04 04:55:46 +0000733 O << " llvm_unreachable(\"Operand not in map!\");\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000734 O << "}\n\n";
735}
736
Bill Wendling740e5b32011-06-14 03:17:20 +0000737static unsigned CountNumOperands(StringRef AsmString) {
738 unsigned NumOps = 0;
739 std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
740
741 while (!ASM.second.empty()) {
742 ++NumOps;
743 ASM = ASM.second.split(' ');
744 }
745
746 return NumOps;
747}
748
Bill Wendling393c4042011-06-15 04:31:19 +0000749static unsigned CountResultNumOperands(StringRef AsmString) {
750 unsigned NumOps = 0;
751 std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
752
753 if (!ASM.second.empty()) {
754 size_t I = ASM.second.find('{');
755 StringRef Str = ASM.second;
756 if (I != StringRef::npos)
757 Str = ASM.second.substr(I, ASM.second.find('|', I));
758
759 ASM = Str.split(' ');
760
761 do {
762 ++NumOps;
763 ASM = ASM.second.split(' ');
764 } while (!ASM.second.empty());
765 }
766
767 return NumOps;
768}
Bill Wendling740e5b32011-06-14 03:17:20 +0000769
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000770void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
771 CodeGenTarget Target(Records);
772 Record *AsmWriter = Target.getAsmWriter();
773
Bill Wendling740e5b32011-06-14 03:17:20 +0000774 if (!AsmWriter->getValueAsBit("isMCAsmWriter"))
775 return;
776
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000777 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
778 O << "#undef PRINT_ALIAS_INSTR\n\n";
779
Bill Wendling7520e3a2011-02-26 03:09:12 +0000780 // Emit the method that prints the alias instruction.
781 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
782
Bill Wendling7520e3a2011-02-26 03:09:12 +0000783 std::vector<Record*> AllInstAliases =
784 Records.getAllDerivedDefinitions("InstAlias");
785
786 // Create a map from the qualified name to a list of potential matches.
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000787 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
Bill Wendling7520e3a2011-02-26 03:09:12 +0000788 for (std::vector<Record*>::iterator
789 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
790 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
791 const Record *R = *I;
Bill Wendlingeef965f2011-04-13 23:36:21 +0000792 if (!R->getValueAsBit("EmitAlias"))
793 continue; // We were told not to emit the alias, but to emit the aliasee.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000794 const DagInit *DI = R->getValueAsDag("ResultInst");
Sean Silva3f7b7f82012-10-10 20:24:47 +0000795 const DefInit *Op = cast<DefInit>(DI->getOperator());
Bill Wendling7520e3a2011-02-26 03:09:12 +0000796 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
797 }
798
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000799 // A map of which conditions need to be met for each instruction operand
800 // before it can be matched to the mnemonic.
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000801 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000802
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000803 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000804 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
805 std::vector<CodeGenInstAlias*> &Aliases = I->second;
806
807 for (std::vector<CodeGenInstAlias*>::iterator
808 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
809 const CodeGenInstAlias *CGA = *II;
Bill Wendling740e5b32011-06-14 03:17:20 +0000810 unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
Bill Wendling393c4042011-06-15 04:31:19 +0000811 unsigned NumResultOps =
812 CountResultNumOperands(CGA->ResultInst->AsmString);
Bill Wendling740e5b32011-06-14 03:17:20 +0000813
814 // Don't emit the alias if it has more operands than what it's aliasing.
Bill Wendling393c4042011-06-15 04:31:19 +0000815 if (NumResultOps < CountNumOperands(CGA->AsmString))
Bill Wendling740e5b32011-06-14 03:17:20 +0000816 continue;
817
Evan Cheng68ae5b42011-07-06 02:02:33 +0000818 IAPrinter *IAP = new IAPrinter(CGA->Result->getAsString(),
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000819 CGA->AsmString);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000820
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000821 std::string Cond;
822 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
823 IAP->addCond(Cond);
824
825 std::map<StringRef, unsigned> OpMap;
826 bool CantHandle = false;
827
828 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
829 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
830
831 switch (RO.Kind) {
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000832 case CodeGenInstAlias::ResultOperand::K_Record: {
833 const Record *Rec = RO.getRecord();
834 StringRef ROName = RO.getName();
835
Owen Andersonbea6f612011-06-27 21:06:21 +0000836
837 if (Rec->isSubClassOf("RegisterOperand"))
838 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000839 if (Rec->isSubClassOf("RegisterClass")) {
840 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
841 IAP->addCond(Cond);
842
843 if (!IAP->isOpMapped(ROName)) {
844 IAP->addOperand(ROName, i);
Jack Carter37ef65b2013-02-05 08:32:10 +0000845 Record *R = CGA->ResultOperands[i].getRecord();
846 if (R->isSubClassOf("RegisterOperand"))
847 R = R->getValueAsDef("RegClass");
Benjamin Kramercef670a2012-03-30 23:13:40 +0000848 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
Jack Carter37ef65b2013-02-05 08:32:10 +0000849 R->getName() + "RegClassID)"
Benjamin Kramercef670a2012-03-30 23:13:40 +0000850 ".contains(MI->getOperand(" + llvm::utostr(i) + ").getReg())";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000851 IAP->addCond(Cond);
852 } else {
853 Cond = std::string("MI->getOperand(") +
854 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
855 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
856 IAP->addCond(Cond);
857 }
858 } else {
859 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
Bill Wendling740e5b32011-06-14 03:17:20 +0000860 // FIXME: We may need to handle these situations.
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000861 delete IAP;
862 IAP = 0;
863 CantHandle = true;
864 break;
865 }
866
867 break;
868 }
Tim Northover7bf2e1b2013-01-09 13:32:04 +0000869 case CodeGenInstAlias::ResultOperand::K_Imm: {
870 std::string Op = "MI->getOperand(" + llvm::utostr(i) + ")";
871
872 // Just because the alias has an immediate result, doesn't mean the
873 // MCInst will. An MCExpr could be present, for example.
874 IAP->addCond(Op + ".isImm()");
875
876 Cond = Op + ".getImm() == "
877 + llvm::utostr(CGA->ResultOperands[i].getImm());
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000878 IAP->addCond(Cond);
879 break;
Tim Northover7bf2e1b2013-01-09 13:32:04 +0000880 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000881 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbachbfc94292011-11-15 01:46:57 +0000882 // If this is zero_reg, something's playing tricks we're not
883 // equipped to handle.
884 if (!CGA->ResultOperands[i].getRegister()) {
885 CantHandle = true;
886 break;
887 }
888
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000889 Cond = std::string("MI->getOperand(") +
890 llvm::utostr(i) + ").getReg() == " + Target.getName() +
891 "::" + CGA->ResultOperands[i].getRegister()->getName();
892 IAP->addCond(Cond);
893 break;
894 }
895
896 if (!IAP) break;
897 }
898
899 if (CantHandle) continue;
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000900 IAPrinterMap[I->first].push_back(IAP);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000901 }
902 }
903
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000904 std::string Header;
905 raw_string_ostream HeaderO(Header);
906
907 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendling740e5b32011-06-14 03:17:20 +0000908 << "::printAliasInstr(const MCInst"
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000909 << " *MI, raw_ostream &OS) {\n";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000910
Bill Wendling44dcfd32011-04-07 21:20:06 +0000911 std::string Cases;
912 raw_string_ostream CasesO(Cases);
913
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000914 for (std::map<std::string, std::vector<IAPrinter*> >::iterator
Bill Wendling44dcfd32011-04-07 21:20:06 +0000915 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
916 std::vector<IAPrinter*> &IAPs = I->second;
917 std::vector<IAPrinter*> UniqueIAPs;
918
919 for (std::vector<IAPrinter*>::iterator
920 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
921 IAPrinter *LHS = *II;
922 bool IsDup = false;
923 for (std::vector<IAPrinter*>::iterator
924 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
925 IAPrinter *RHS = *III;
926 if (LHS != RHS && *LHS == *RHS) {
927 IsDup = true;
928 break;
929 }
930 }
931
932 if (!IsDup) UniqueIAPs.push_back(LHS);
933 }
934
935 if (UniqueIAPs.empty()) continue;
936
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000937 CasesO.indent(2) << "case " << I->first << ":\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000938
939 for (std::vector<IAPrinter*>::iterator
940 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
941 IAPrinter *IAP = *II;
942 CasesO.indent(4);
Evan Cheng68ae5b42011-07-06 02:02:33 +0000943 IAP->print(CasesO);
Bill Wendling44dcfd32011-04-07 21:20:06 +0000944 CasesO << '\n';
945 }
946
Eric Christopher721ef662011-04-18 21:28:11 +0000947 CasesO.indent(4) << "return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000948 }
949
Bill Wendling740e5b32011-06-14 03:17:20 +0000950 if (CasesO.str().empty()) {
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000951 O << HeaderO.str();
Eric Christopher721ef662011-04-18 21:28:11 +0000952 O << " return false;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000953 O << "}\n\n";
954 O << "#endif // PRINT_ALIAS_INSTR\n";
955 return;
956 }
957
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000958 EmitGetMapOperandNumber(O);
959
960 O << HeaderO.str();
Bill Wendling44dcfd32011-04-07 21:20:06 +0000961 O.indent(2) << "StringRef AsmString;\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000962 O.indent(2) << "SmallVector<std::pair<StringRef, unsigned>, 4> OpMap;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000963 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher721ef662011-04-18 21:28:11 +0000964 O.indent(2) << "default: return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000965 O << CasesO.str();
966 O.indent(2) << "}\n\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000967
968 // Code that prints the alias, replacing the operands with the ones from the
969 // MCInst.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000970 O << " std::pair<StringRef, StringRef> ASM = AsmString.split(' ');\n";
971 O << " OS << '\\t' << ASM.first;\n";
972
973 O << " if (!ASM.second.empty()) {\n";
974 O << " OS << '\\t';\n";
975 O << " for (StringRef::iterator\n";
976 O << " I = ASM.second.begin(), E = ASM.second.end(); I != E; ) {\n";
977 O << " if (*I == '$') {\n";
978 O << " StringRef::iterator Start = ++I;\n";
979 O << " while (I != E &&\n";
980 O << " ((*I >= 'a' && *I <= 'z') ||\n";
981 O << " (*I >= 'A' && *I <= 'Z') ||\n";
982 O << " (*I >= '0' && *I <= '9') ||\n";
983 O << " *I == '_'))\n";
984 O << " ++I;\n";
985 O << " StringRef Name(Start, I - Start);\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000986 O << " printOperand(MI, getMapOperandNumber(OpMap, Name), OS);\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000987 O << " } else {\n";
988 O << " OS << *I++;\n";
989 O << " }\n";
990 O << " }\n";
991 O << " }\n\n";
Jim Grosbach016c6792012-04-18 18:56:33 +0000992
Eric Christopher721ef662011-04-18 21:28:11 +0000993 O << " return true;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000994 O << "}\n\n";
995
996 O << "#endif // PRINT_ALIAS_INSTR\n";
997}
Chris Lattner05af2612009-09-13 20:08:00 +0000998
999void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner05af2612009-09-13 20:08:00 +00001000 EmitPrintInstruction(O);
1001 EmitGetRegisterName(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +00001002 EmitPrintAliasInstruction(O);
Chris Lattner05af2612009-09-13 20:08:00 +00001003}
1004
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00001005
1006namespace llvm {
1007
1008void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
1009 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
1010 AsmWriterEmitter(RK).run(OS);
1011}
1012
1013} // End llvm namespace