blob: bd153a855ce9cf114602c3cf9dbbf89ffff7c1d2 [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"
Chris Lattner44da5fb2009-09-14 01:19:16 +000017#include "StringToOffsetTable.h"
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +000018#include "SequenceToOffsetTable.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.
316 std::vector<unsigned> OpcodeInfo;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000317
Benjamin Kramer94338592012-04-02 09:13:46 +0000318 // Add all strings to the string table upfront so it can generate an optimized
319 // representation.
320 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
321 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
322 if (AWI != 0 &&
Jim Grosbach016c6792012-04-18 18:56:33 +0000323 AWI->Operands[0].OperandType ==
324 AsmWriterOperand::isLiteralTextOperand &&
Benjamin Kramer94338592012-04-02 09:13:46 +0000325 !AWI->Operands[0].Str.empty()) {
326 std::string Str = AWI->Operands[0].Str;
327 UnescapeString(Str);
328 StringTable.add(Str);
329 }
330 }
331
332 StringTable.layout();
333
Chris Lattner55616402006-07-18 17:32:27 +0000334 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000335 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
336 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
337 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000338 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000339 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000340 Idx = ~0U;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000341 } else if (AWI->Operands[0].OperandType !=
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000342 AsmWriterOperand::isLiteralTextOperand ||
343 AWI->Operands[0].Str.empty()) {
344 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer94338592012-04-02 09:13:46 +0000345 Idx = StringTable.get("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000346 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000347 std::string Str = AWI->Operands[0].Str;
348 UnescapeString(Str);
Benjamin Kramer94338592012-04-02 09:13:46 +0000349 Idx = StringTable.get(Str);
Chris Lattner3200fc92009-09-14 01:16:36 +0000350 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000351
Chris Lattner6af022f2006-07-14 22:59:11 +0000352 // Nuke the string from the operand list. It is now handled!
353 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000354 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000355
Chris Lattner3200fc92009-09-14 01:16:36 +0000356 // Bias offset by one since we want 0 as a sentinel.
357 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000358 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000359
Chris Lattner55616402006-07-18 17:32:27 +0000360 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000361 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000362
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000363 // To reduce code size, we compactify common instructions into a few bits
364 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000365 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000366
367 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000368
Chris Lattnerb8462862006-07-18 17:56:07 +0000369 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000370 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000371 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000372 std::vector<unsigned> NumInstOpsHandled;
373 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
374 NumInstOpsHandled);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000375
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000376 // If we ran out of operands to print, we're done.
377 if (UniqueOperandCommands.empty()) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000378
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000379 // Compute the number of bits we need to represent these cases, this is
380 // ceil(log2(numentries)).
381 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000382
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000383 // If we don't have enough bits for this operand, don't include it.
384 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000385 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
386 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000387 break;
388 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000389
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000390 // Otherwise, we can include this in the initial lookup table. Add it in.
391 BitsLeft -= NumBits;
392 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000393 if (InstIdxs[i] != ~0U)
394 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000395
Chris Lattnerb8462862006-07-18 17:56:07 +0000396 // Remove the info about this operand.
397 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
398 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000399 if (!Inst->Operands.empty()) {
400 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000401 assert(NumOps <= Inst->Operands.size() &&
402 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000403 Inst->Operands.erase(Inst->Operands.begin(),
404 Inst->Operands.begin()+NumOps);
405 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000406 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000407
Chris Lattnerb8462862006-07-18 17:56:07 +0000408 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000409 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
410 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000411
412
413
Chris Lattner55616402006-07-18 17:32:27 +0000414 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000415 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000416 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000417 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000418 }
419 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000420 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000421 O << " };\n\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000422
Chris Lattner6af022f2006-07-14 22:59:11 +0000423 // Emit the string itself.
Benjamin Kramer94338592012-04-02 09:13:46 +0000424 O << " const char AsmStrs[] = {\n";
425 StringTable.emit(O, printChar);
426 O << " };\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000427
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000428 O << " O << \"\\t\";\n\n";
429
Chris Lattner6af022f2006-07-14 22:59:11 +0000430 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000431 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000432 << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000433 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000434
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000435 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000436 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000437 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
438 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
439
440 // Compute the number of bits we need to represent these cases, this is
441 // ceil(log2(numentries)).
442 unsigned NumBits = Log2_32_Ceil(Commands.size());
443 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000444
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000445 // Emit code to extract this field from Bits.
446 BitsLeft -= NumBits;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000447
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000448 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000449 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000450
Chris Lattner96c1ade2006-07-18 18:28:27 +0000451 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000452 // Emit two possibilitys with if/else.
453 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
454 << ((1 << NumBits)-1) << ") {\n"
455 << Commands[1]
456 << " } else {\n"
457 << Commands[0]
458 << " }\n\n";
Eric Christopher16870502010-09-18 18:50:27 +0000459 } else if (Commands.size() == 1) {
460 // Emit a single possibility.
461 O << Commands[0] << "\n\n";
Chris Lattnere7a589d2006-07-18 17:43:54 +0000462 } else {
463 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
464 << ((1 << NumBits)-1) << ") {\n"
465 << " default: // unreachable.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000466
Chris Lattnere7a589d2006-07-18 17:43:54 +0000467 // Print out all the cases.
468 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
469 O << " case " << i << ":\n";
470 O << Commands[i];
471 O << " break;\n";
472 }
473 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000474 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000475 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000476
Chris Lattnerb8462862006-07-18 17:56:07 +0000477 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000478 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
479 // Entire instruction has been emitted?
480 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000481 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000482 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000483 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000484 }
485 }
486
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000487
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000488 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000489 // elements in the vector.
490 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000491
492
Chris Lattner70067602009-09-18 18:10:19 +0000493 // Now that we've emitted all of the operand info that fit into 32 bits, emit
494 // information for those instructions that are left. This is a less dense
495 // encoding, but we expect the main 32-bit table to handle the majority of
496 // instructions.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000497 if (!Instructions.empty()) {
498 // Find the opcode # of inline asm.
499 O << " switch (MI->getOpcode()) {\n";
500 while (!Instructions.empty())
501 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000502
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000503 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000504 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000505 }
David Greenec8d06052009-07-29 20:10:24 +0000506
Chris Lattner0a012122006-07-18 19:06:01 +0000507 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000508}
Chris Lattner05af2612009-09-13 20:08:00 +0000509
Owen Andersonbea6f612011-06-27 21:06:21 +0000510static void
511emitRegisterNameString(raw_ostream &O, StringRef AltName,
Craig Topper9b1b25f2012-04-03 06:52:47 +0000512 const std::vector<CodeGenRegister*> &Registers) {
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000513 SequenceToOffsetTable<std::string> StringTable;
514 SmallVector<std::string, 4> AsmNames(Registers.size());
Owen Andersonbea6f612011-06-27 21:06:21 +0000515 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
516 const CodeGenRegister &Reg = *Registers[i];
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000517 std::string &AsmName = AsmNames[i];
Owen Andersonbea6f612011-06-27 21:06:21 +0000518
Owen Andersonbea6f612011-06-27 21:06:21 +0000519 // "NoRegAltName" is special. We don't need to do a lookup for that,
520 // as it's just a reference to the default register name.
521 if (AltName == "" || AltName == "NoRegAltName") {
522 AsmName = Reg.TheDef->getValueAsString("AsmName");
523 if (AsmName.empty())
524 AsmName = Reg.getName();
525 } else {
526 // Make sure the register has an alternate name for this index.
527 std::vector<Record*> AltNameList =
528 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
529 unsigned Idx = 0, e;
530 for (e = AltNameList.size();
531 Idx < e && (AltNameList[Idx]->getName() != AltName);
532 ++Idx)
533 ;
534 // If the register has an alternate name for this index, use it.
535 // Otherwise, leave it empty as an error flag.
536 if (Idx < e) {
537 std::vector<std::string> AltNames =
538 Reg.TheDef->getValueAsListOfStrings("AltNames");
539 if (AltNames.size() <= Idx)
540 throw TGError(Reg.TheDef->getLoc(),
541 (Twine("Register definition missing alt name for '") +
542 AltName + "'.").str());
543 AsmName = AltNames[Idx];
544 }
545 }
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000546 StringTable.add(AsmName);
547 }
Owen Andersonbea6f612011-06-27 21:06:21 +0000548
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000549 StringTable.layout();
550 O << " static const char AsmStrs" << AltName << "[] = {\n";
551 StringTable.emit(O, printChar);
552 O << " };\n\n";
553
Craig Toppera4bd58b2012-04-02 00:47:39 +0000554 O << " static const unsigned RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000555 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Toppera4bd58b2012-04-02 00:47:39 +0000556 if ((i % 14) == 0)
557 O << "\n ";
558 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersonbea6f612011-06-27 21:06:21 +0000559 }
Craig Topper9b1b25f2012-04-03 06:52:47 +0000560 O << "\n };\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000561 << "\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000562}
Chris Lattner05af2612009-09-13 20:08:00 +0000563
564void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000565 CodeGenTarget Target(Records);
Chris Lattner05af2612009-09-13 20:08:00 +0000566 Record *AsmWriter = Target.getAsmWriter();
567 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000568 const std::vector<CodeGenRegister*> &Registers =
569 Target.getRegBank().getRegisters();
Owen Andersonbea6f612011-06-27 21:06:21 +0000570 std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices();
571 bool hasAltNames = AltNameIndices.size() > 1;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000572
Chris Lattner05af2612009-09-13 20:08:00 +0000573 O <<
574 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
575 "/// from the register set description. This returns the assembler name\n"
576 "/// for the specified register.\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000577 "const char *" << Target.getName() << ClassName << "::";
578 if (hasAltNames)
579 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
580 else
581 O << "getRegisterName(unsigned RegNo) {\n";
582 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
583 << " && \"Invalid register number!\");\n"
Chris Lattnerf6761be2009-09-14 01:26:18 +0000584 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000585
Owen Andersonbea6f612011-06-27 21:06:21 +0000586 if (hasAltNames) {
587 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i)
588 emitRegisterNameString(O, AltNameIndices[i]->getName(), Registers);
589 } else
590 emitRegisterNameString(O, "", Registers);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000591
Owen Andersonbea6f612011-06-27 21:06:21 +0000592 if (hasAltNames) {
Jakob Stoklund Olesend66b9a22012-03-15 18:05:54 +0000593 O << " const unsigned *RegAsmOffset;\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000594 << " const char *AsmStrs;\n"
595 << " switch(AltIdx) {\n"
Craig Topper655b8de2012-02-05 07:21:30 +0000596 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000597 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) {
598 StringRef Namespace = AltNameIndices[1]->getValueAsString("Namespace");
599 StringRef AltName(AltNameIndices[i]->getName());
600 O << " case " << Namespace << "::" << AltName
601 << ":\n"
602 << " AsmStrs = AsmStrs" << AltName << ";\n"
603 << " RegAsmOffset = RegAsmOffset" << AltName << ";\n"
604 << " break;\n";
605 }
606 O << "}\n";
607 }
608
609 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
610 << " \"Invalid alt name index for register!\");\n"
611 << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000612 << "}\n";
613}
614
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000615namespace {
Bill Wendling4962e612011-03-21 08:40:31 +0000616// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
617// they both have the same conditionals. In which case, we cannot print out the
618// alias for that pattern.
619class IAPrinter {
Bill Wendling4962e612011-03-21 08:40:31 +0000620 std::vector<std::string> Conds;
621 std::map<StringRef, unsigned> OpMap;
622 std::string Result;
623 std::string AsmString;
Jim Grosbacha5b06852012-04-18 19:02:43 +0000624 SmallVector<Record*, 4> ReqFeatures;
Bill Wendling4962e612011-03-21 08:40:31 +0000625public:
Evan Cheng68ae5b42011-07-06 02:02:33 +0000626 IAPrinter(std::string R, std::string AS)
627 : Result(R), AsmString(AS) {}
Bill Wendling4962e612011-03-21 08:40:31 +0000628
629 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling4962e612011-03-21 08:40:31 +0000630
631 void addOperand(StringRef Op, unsigned Idx) { OpMap[Op] = Idx; }
632 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
633 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
634
Evan Cheng68ae5b42011-07-06 02:02:33 +0000635 void print(raw_ostream &O) {
Bill Wendling44dcfd32011-04-07 21:20:06 +0000636 if (Conds.empty() && ReqFeatures.empty()) {
637 O.indent(6) << "return true;\n";
Evan Cheng68ae5b42011-07-06 02:02:33 +0000638 return;
Bill Wendling44dcfd32011-04-07 21:20:06 +0000639 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000640
Bill Wendling44dcfd32011-04-07 21:20:06 +0000641 O << "if (";
Bill Wendling4962e612011-03-21 08:40:31 +0000642
643 for (std::vector<std::string>::iterator
644 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
645 if (I != Conds.begin()) {
646 O << " &&\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000647 O.indent(8);
Bill Wendling4962e612011-03-21 08:40:31 +0000648 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000649
Bill Wendling4962e612011-03-21 08:40:31 +0000650 O << *I;
651 }
652
Bill Wendling44dcfd32011-04-07 21:20:06 +0000653 O << ") {\n";
654 O.indent(6) << "// " << Result << "\n";
655 O.indent(6) << "AsmString = \"" << AsmString << "\";\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000656
657 for (std::map<StringRef, unsigned>::iterator
658 I = OpMap.begin(), E = OpMap.end(); I != E; ++I)
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000659 O.indent(6) << "OpMap.push_back(std::make_pair(\"" << I->first << "\", "
660 << I->second << "));\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000661
Bill Wendling44dcfd32011-04-07 21:20:06 +0000662 O.indent(6) << "break;\n";
663 O.indent(4) << '}';
Bill Wendling4962e612011-03-21 08:40:31 +0000664 }
665
666 bool operator==(const IAPrinter &RHS) {
667 if (Conds.size() != RHS.Conds.size())
668 return false;
669
670 unsigned Idx = 0;
671 for (std::vector<std::string>::iterator
672 I = Conds.begin(), E = Conds.end(); I != E; ++I)
673 if (*I != RHS.Conds[Idx++])
674 return false;
675
676 return true;
677 }
678
679 bool operator()(const IAPrinter &RHS) {
680 if (Conds.size() < RHS.Conds.size())
681 return true;
682
683 unsigned Idx = 0;
684 for (std::vector<std::string>::iterator
685 I = Conds.begin(), E = Conds.end(); I != E; ++I)
686 if (*I != RHS.Conds[Idx++])
687 return *I < RHS.Conds[Idx++];
688
689 return false;
690 }
691};
692
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000693} // end anonymous namespace
694
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000695static void EmitGetMapOperandNumber(raw_ostream &O) {
696 O << "static unsigned getMapOperandNumber("
697 << "const SmallVectorImpl<std::pair<StringRef, unsigned> > &OpMap,\n";
698 O << " StringRef Name) {\n";
699 O << " for (SmallVectorImpl<std::pair<StringRef, unsigned> >::"
700 << "const_iterator\n";
701 O << " I = OpMap.begin(), E = OpMap.end(); I != E; ++I)\n";
702 O << " if (I->first == Name)\n";
703 O << " return I->second;\n";
Craig Topper58609b72012-04-04 04:55:46 +0000704 O << " llvm_unreachable(\"Operand not in map!\");\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000705 O << "}\n\n";
706}
707
Bill Wendling740e5b32011-06-14 03:17:20 +0000708static unsigned CountNumOperands(StringRef AsmString) {
709 unsigned NumOps = 0;
710 std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
711
712 while (!ASM.second.empty()) {
713 ++NumOps;
714 ASM = ASM.second.split(' ');
715 }
716
717 return NumOps;
718}
719
Bill Wendling393c4042011-06-15 04:31:19 +0000720static unsigned CountResultNumOperands(StringRef AsmString) {
721 unsigned NumOps = 0;
722 std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
723
724 if (!ASM.second.empty()) {
725 size_t I = ASM.second.find('{');
726 StringRef Str = ASM.second;
727 if (I != StringRef::npos)
728 Str = ASM.second.substr(I, ASM.second.find('|', I));
729
730 ASM = Str.split(' ');
731
732 do {
733 ++NumOps;
734 ASM = ASM.second.split(' ');
735 } while (!ASM.second.empty());
736 }
737
738 return NumOps;
739}
Bill Wendling740e5b32011-06-14 03:17:20 +0000740
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000741void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
742 CodeGenTarget Target(Records);
743 Record *AsmWriter = Target.getAsmWriter();
744
Bill Wendling740e5b32011-06-14 03:17:20 +0000745 if (!AsmWriter->getValueAsBit("isMCAsmWriter"))
746 return;
747
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000748 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
749 O << "#undef PRINT_ALIAS_INSTR\n\n";
750
Bill Wendling7520e3a2011-02-26 03:09:12 +0000751 // Emit the method that prints the alias instruction.
752 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
753
Bill Wendling7520e3a2011-02-26 03:09:12 +0000754 std::vector<Record*> AllInstAliases =
755 Records.getAllDerivedDefinitions("InstAlias");
756
757 // Create a map from the qualified name to a list of potential matches.
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000758 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
Bill Wendling7520e3a2011-02-26 03:09:12 +0000759 for (std::vector<Record*>::iterator
760 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
761 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
762 const Record *R = *I;
Bill Wendlingeef965f2011-04-13 23:36:21 +0000763 if (!R->getValueAsBit("EmitAlias"))
764 continue; // We were told not to emit the alias, but to emit the aliasee.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000765 const DagInit *DI = R->getValueAsDag("ResultInst");
766 const DefInit *Op = dynamic_cast<const DefInit*>(DI->getOperator());
767 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
768 }
769
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000770 // A map of which conditions need to be met for each instruction operand
771 // before it can be matched to the mnemonic.
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000772 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000773
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000774 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000775 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
776 std::vector<CodeGenInstAlias*> &Aliases = I->second;
777
778 for (std::vector<CodeGenInstAlias*>::iterator
779 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
780 const CodeGenInstAlias *CGA = *II;
Bill Wendling740e5b32011-06-14 03:17:20 +0000781 unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
Bill Wendling393c4042011-06-15 04:31:19 +0000782 unsigned NumResultOps =
783 CountResultNumOperands(CGA->ResultInst->AsmString);
Bill Wendling740e5b32011-06-14 03:17:20 +0000784
785 // Don't emit the alias if it has more operands than what it's aliasing.
Bill Wendling393c4042011-06-15 04:31:19 +0000786 if (NumResultOps < CountNumOperands(CGA->AsmString))
Bill Wendling740e5b32011-06-14 03:17:20 +0000787 continue;
788
Evan Cheng68ae5b42011-07-06 02:02:33 +0000789 IAPrinter *IAP = new IAPrinter(CGA->Result->getAsString(),
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000790 CGA->AsmString);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000791
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000792 std::string Cond;
793 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
794 IAP->addCond(Cond);
795
796 std::map<StringRef, unsigned> OpMap;
797 bool CantHandle = false;
798
799 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
800 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
801
802 switch (RO.Kind) {
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000803 case CodeGenInstAlias::ResultOperand::K_Record: {
804 const Record *Rec = RO.getRecord();
805 StringRef ROName = RO.getName();
806
Owen Andersonbea6f612011-06-27 21:06:21 +0000807
808 if (Rec->isSubClassOf("RegisterOperand"))
809 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000810 if (Rec->isSubClassOf("RegisterClass")) {
811 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
812 IAP->addCond(Cond);
813
814 if (!IAP->isOpMapped(ROName)) {
815 IAP->addOperand(ROName, i);
Benjamin Kramercef670a2012-03-30 23:13:40 +0000816 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
817 CGA->ResultOperands[i].getRecord()->getName() + "RegClassID)"
818 ".contains(MI->getOperand(" + llvm::utostr(i) + ").getReg())";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000819 IAP->addCond(Cond);
820 } else {
821 Cond = std::string("MI->getOperand(") +
822 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
823 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
824 IAP->addCond(Cond);
825 }
826 } else {
827 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
Bill Wendling740e5b32011-06-14 03:17:20 +0000828 // FIXME: We may need to handle these situations.
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000829 delete IAP;
830 IAP = 0;
831 CantHandle = true;
832 break;
833 }
834
835 break;
836 }
837 case CodeGenInstAlias::ResultOperand::K_Imm:
838 Cond = std::string("MI->getOperand(") +
839 llvm::utostr(i) + ").getImm() == " +
840 llvm::utostr(CGA->ResultOperands[i].getImm());
841 IAP->addCond(Cond);
842 break;
843 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbachbfc94292011-11-15 01:46:57 +0000844 // If this is zero_reg, something's playing tricks we're not
845 // equipped to handle.
846 if (!CGA->ResultOperands[i].getRegister()) {
847 CantHandle = true;
848 break;
849 }
850
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000851 Cond = std::string("MI->getOperand(") +
852 llvm::utostr(i) + ").getReg() == " + Target.getName() +
853 "::" + CGA->ResultOperands[i].getRegister()->getName();
854 IAP->addCond(Cond);
855 break;
856 }
857
858 if (!IAP) break;
859 }
860
861 if (CantHandle) continue;
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000862 IAPrinterMap[I->first].push_back(IAP);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000863 }
864 }
865
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000866 std::string Header;
867 raw_string_ostream HeaderO(Header);
868
869 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendling740e5b32011-06-14 03:17:20 +0000870 << "::printAliasInstr(const MCInst"
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000871 << " *MI, raw_ostream &OS) {\n";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000872
Bill Wendling44dcfd32011-04-07 21:20:06 +0000873 std::string Cases;
874 raw_string_ostream CasesO(Cases);
875
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000876 for (std::map<std::string, std::vector<IAPrinter*> >::iterator
Bill Wendling44dcfd32011-04-07 21:20:06 +0000877 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
878 std::vector<IAPrinter*> &IAPs = I->second;
879 std::vector<IAPrinter*> UniqueIAPs;
880
881 for (std::vector<IAPrinter*>::iterator
882 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
883 IAPrinter *LHS = *II;
884 bool IsDup = false;
885 for (std::vector<IAPrinter*>::iterator
886 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
887 IAPrinter *RHS = *III;
888 if (LHS != RHS && *LHS == *RHS) {
889 IsDup = true;
890 break;
891 }
892 }
893
894 if (!IsDup) UniqueIAPs.push_back(LHS);
895 }
896
897 if (UniqueIAPs.empty()) continue;
898
Jim Grosbachb4b26f82012-04-18 20:24:49 +0000899 CasesO.indent(2) << "case " << I->first << ":\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000900
901 for (std::vector<IAPrinter*>::iterator
902 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
903 IAPrinter *IAP = *II;
904 CasesO.indent(4);
Evan Cheng68ae5b42011-07-06 02:02:33 +0000905 IAP->print(CasesO);
Bill Wendling44dcfd32011-04-07 21:20:06 +0000906 CasesO << '\n';
907 }
908
Eric Christopher721ef662011-04-18 21:28:11 +0000909 CasesO.indent(4) << "return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000910 }
911
Bill Wendling740e5b32011-06-14 03:17:20 +0000912 if (CasesO.str().empty()) {
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000913 O << HeaderO.str();
Eric Christopher721ef662011-04-18 21:28:11 +0000914 O << " return false;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000915 O << "}\n\n";
916 O << "#endif // PRINT_ALIAS_INSTR\n";
917 return;
918 }
919
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000920 EmitGetMapOperandNumber(O);
921
922 O << HeaderO.str();
Bill Wendling44dcfd32011-04-07 21:20:06 +0000923 O.indent(2) << "StringRef AsmString;\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000924 O.indent(2) << "SmallVector<std::pair<StringRef, unsigned>, 4> OpMap;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000925 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher721ef662011-04-18 21:28:11 +0000926 O.indent(2) << "default: return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000927 O << CasesO.str();
928 O.indent(2) << "}\n\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000929
930 // Code that prints the alias, replacing the operands with the ones from the
931 // MCInst.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000932 O << " std::pair<StringRef, StringRef> ASM = AsmString.split(' ');\n";
933 O << " OS << '\\t' << ASM.first;\n";
934
935 O << " if (!ASM.second.empty()) {\n";
936 O << " OS << '\\t';\n";
937 O << " for (StringRef::iterator\n";
938 O << " I = ASM.second.begin(), E = ASM.second.end(); I != E; ) {\n";
939 O << " if (*I == '$') {\n";
940 O << " StringRef::iterator Start = ++I;\n";
941 O << " while (I != E &&\n";
942 O << " ((*I >= 'a' && *I <= 'z') ||\n";
943 O << " (*I >= 'A' && *I <= 'Z') ||\n";
944 O << " (*I >= '0' && *I <= '9') ||\n";
945 O << " *I == '_'))\n";
946 O << " ++I;\n";
947 O << " StringRef Name(Start, I - Start);\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000948 O << " printOperand(MI, getMapOperandNumber(OpMap, Name), OS);\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000949 O << " } else {\n";
950 O << " OS << *I++;\n";
951 O << " }\n";
952 O << " }\n";
953 O << " }\n\n";
Jim Grosbach016c6792012-04-18 18:56:33 +0000954
Eric Christopher721ef662011-04-18 21:28:11 +0000955 O << " return true;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000956 O << "}\n\n";
957
958 O << "#endif // PRINT_ALIAS_INSTR\n";
959}
Chris Lattner05af2612009-09-13 20:08:00 +0000960
961void AsmWriterEmitter::run(raw_ostream &O) {
Chris Lattner05af2612009-09-13 20:08:00 +0000962 EmitPrintInstruction(O);
963 EmitGetRegisterName(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +0000964 EmitPrintAliasInstruction(O);
Chris Lattner05af2612009-09-13 20:08:00 +0000965}
966
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000967
968namespace llvm {
969
970void EmitAsmWriter(RecordKeeper &RK, raw_ostream &OS) {
971 emitSourceFileHeader("Assembly Writer Source Fragment", OS);
972 AsmWriterEmitter(RK).run(OS);
973}
974
975} // End llvm namespace