blob: 0d6d0c805e4b40bd54532b3060f8565f5450be26 [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
15#include "AsmWriterEmitter.h"
Sean Callanand32c02f2010-02-09 21:50:41 +000016#include "AsmWriterInst.h"
Chris Lattner2e1f51b2004-08-01 05:59:33 +000017#include "CodeGenTarget.h"
Chris Lattner44da5fb2009-09-14 01:19:16 +000018#include "StringToOffsetTable.h"
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +000019#include "SequenceToOffsetTable.h"
Owen Andersonbea6f612011-06-27 21:06:21 +000020#include "llvm/ADT/Twine.h"
Chris Lattnerbdff5f92006-07-18 17:18:03 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/MathExtras.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000023#include "llvm/TableGen/Error.h"
24#include "llvm/TableGen/Record.h"
Jeff Cohen615ed992005-01-22 18:50:10 +000025#include <algorithm>
Chris Lattner2e1f51b2004-08-01 05:59:33 +000026using namespace llvm;
27
Chris Lattner38c07512005-01-22 20:31:17 +000028static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +000029 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +000030 O << " case " << OpsToPrint.back().first << ": ";
31 AsmWriterOperand TheOp = OpsToPrint.back().second;
32 OpsToPrint.pop_back();
33
34 // Check to see if any other operands are identical in this list, and if so,
35 // emit a case label for them.
36 for (unsigned i = OpsToPrint.size(); i != 0; --i)
37 if (OpsToPrint[i-1].second == TheOp) {
38 O << "\n case " << OpsToPrint[i-1].first << ": ";
39 OpsToPrint.erase(OpsToPrint.begin()+i-1);
40 }
41
42 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000043 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +000044 O << "break;\n";
45}
46
Chris Lattner870c0162005-01-22 18:38:13 +000047
48/// EmitInstructions - Emit the last instruction in the vector and any other
49/// instructions that are suitably similar to it.
50static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +000051 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +000052 AsmWriterInst FirstInst = Insts.back();
53 Insts.pop_back();
54
55 std::vector<AsmWriterInst> SimilarInsts;
56 unsigned DifferingOperand = ~0;
57 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +000058 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
59 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +000060 if (DifferingOperand == ~0U) // First match!
61 DifferingOperand = DiffOp;
62
63 // If this differs in the same operand as the rest of the instructions in
64 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +000065 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +000066 SimilarInsts.push_back(Insts[i-1]);
67 Insts.erase(Insts.begin()+i-1);
68 }
69 }
70 }
71
Chris Lattnera1e8a802006-05-01 17:01:17 +000072 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +000073 << FirstInst.CGI->TheDef->getName() << ":\n";
74 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +000075 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +000076 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
77 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
78 if (i != DifferingOperand) {
79 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000080 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +000081 } else {
82 // If this is the operand that varies between all of the instructions,
83 // emit a switch for just this operand now.
84 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +000085 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +000086 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +000087 FirstInst.CGI->TheDef->getName(),
88 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +000089
Chris Lattner870c0162005-01-22 18:38:13 +000090 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +000091 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +000092 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +000093 AWI.CGI->TheDef->getName(),
94 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +000095 }
Chris Lattner38c07512005-01-22 20:31:17 +000096 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
97 while (!OpsToPrint.empty())
98 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +000099 O << " }";
100 }
101 O << "\n";
102 }
Chris Lattner870c0162005-01-22 18:38:13 +0000103 O << " break;\n";
104}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000105
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000106void AsmWriterEmitter::
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000107FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000108 std::vector<unsigned> &InstIdxs,
109 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000110 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000111
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000112 // This vector parallels UniqueOperandCommands, keeping track of which
113 // instructions each case are used for. It is a comma separated string of
114 // enums.
115 std::vector<std::string> InstrsForCase;
116 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000117 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000118
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000119 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
120 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Bill Wendlingb9449d62010-07-16 23:10:00 +0000121 if (Inst == 0) continue; // PHI, INLINEASM, PROLOG_LABEL, etc.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000122
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000123 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000124 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000125 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000126
Chris Lattnerb8462862006-07-18 17:56:07 +0000127 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000128
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000129 // Check to see if we already have 'Command' in UniqueOperandCommands.
130 // If not, add it.
131 bool FoundIt = false;
132 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
133 if (UniqueOperandCommands[idx] == Command) {
134 InstIdxs[i] = idx;
135 InstrsForCase[idx] += ", ";
136 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
137 FoundIt = true;
138 break;
139 }
140 if (!FoundIt) {
141 InstIdxs[i] = UniqueOperandCommands.size();
142 UniqueOperandCommands.push_back(Command);
143 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000144
145 // This command matches one operand so far.
146 InstOpsUsed.push_back(1);
147 }
148 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000149
Chris Lattner96c1ade2006-07-18 18:28:27 +0000150 // For each entry of UniqueOperandCommands, there is a set of instructions
151 // that uses it. If the next command of all instructions in the set are
152 // identical, fold it into the command.
153 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
154 CommandIdx != e; ++CommandIdx) {
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000155
Chris Lattner96c1ade2006-07-18 18:28:27 +0000156 for (unsigned Op = 1; ; ++Op) {
157 // Scan for the first instruction in the set.
158 std::vector<unsigned>::iterator NIT =
159 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
160 if (NIT == InstIdxs.end()) break; // No commonality.
161
162 // If this instruction has no more operands, we isn't anything to merge
163 // into this command.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000164 const AsmWriterInst *FirstInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000165 getAsmWriterInstByID(NIT-InstIdxs.begin());
166 if (!FirstInst || FirstInst->Operands.size() == Op)
167 break;
168
169 // Otherwise, scan to see if all of the other instructions in this command
170 // set share the operand.
171 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000172 // Keep track of the maximum, number of operands or any
173 // instruction we see in the group.
174 size_t MaxSize = FirstInst->Operands.size();
175
Chris Lattner96c1ade2006-07-18 18:28:27 +0000176 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
177 NIT != InstIdxs.end();
178 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
179 // Okay, found another instruction in this command set. If the operand
180 // matches, we're ok, otherwise bail out.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000181 const AsmWriterInst *OtherInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000182 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000183
184 if (OtherInst &&
185 OtherInst->Operands.size() > FirstInst->Operands.size())
186 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
187
Chris Lattner96c1ade2006-07-18 18:28:27 +0000188 if (!OtherInst || OtherInst->Operands.size() == Op ||
189 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
190 AllSame = false;
191 break;
192 }
193 }
194 if (!AllSame) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000195
Chris Lattner96c1ade2006-07-18 18:28:27 +0000196 // Okay, everything in this command set has the same next operand. Add it
197 // to UniqueOperandCommands and remember that it was consumed.
198 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000199
Chris Lattner96c1ade2006-07-18 18:28:27 +0000200 UniqueOperandCommands[CommandIdx] += Command;
201 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000202 }
203 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000204
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000205 // Prepend some of the instructions each case is used for onto the case val.
206 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
207 std::string Instrs = InstrsForCase[i];
208 if (Instrs.size() > 70) {
209 Instrs.erase(Instrs.begin()+70, Instrs.end());
210 Instrs += "...";
211 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000212
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000213 if (!Instrs.empty())
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000214 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000215 UniqueOperandCommands[i];
216 }
217}
218
219
Daniel Dunbar9bd34602009-10-17 20:43:42 +0000220static void UnescapeString(std::string &Str) {
221 for (unsigned i = 0; i != Str.size(); ++i) {
222 if (Str[i] == '\\' && i != Str.size()-1) {
223 switch (Str[i+1]) {
224 default: continue; // Don't execute the code after the switch.
225 case 'a': Str[i] = '\a'; break;
226 case 'b': Str[i] = '\b'; break;
227 case 'e': Str[i] = 27; break;
228 case 'f': Str[i] = '\f'; break;
229 case 'n': Str[i] = '\n'; break;
230 case 'r': Str[i] = '\r'; break;
231 case 't': Str[i] = '\t'; break;
232 case 'v': Str[i] = '\v'; break;
233 case '"': Str[i] = '\"'; break;
234 case '\'': Str[i] = '\''; break;
235 case '\\': Str[i] = '\\'; break;
236 }
237 // Nuke the second character.
238 Str.erase(Str.begin()+i+1);
239 }
240 }
241}
242
Chris Lattner05af2612009-09-13 20:08:00 +0000243/// EmitPrintInstruction - Generate the code for the "printInstruction" method
244/// implementation.
245void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000246 CodeGenTarget Target(Records);
Chris Lattner175580c2004-08-14 22:50:53 +0000247 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000248 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jim Grosbachca96a862010-09-30 01:29:54 +0000249 bool isMC = AsmWriter->getValueAsBit("isMCAsmWriter");
250 const char *MachineInstrClassName = isMC ? "MCInst" : "MachineInstr";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000251
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000252 O <<
253 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000254 "/// from the instruction set description.\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000255 "void " << Target.getName() << ClassName
Jim Grosbachca96a862010-09-30 01:29:54 +0000256 << "::printInstruction(const " << MachineInstrClassName
257 << " *MI, raw_ostream &O) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000258
Chris Lattner5765dba2005-01-22 17:40:38 +0000259 std::vector<AsmWriterInst> Instructions;
260
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000261 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
262 E = Target.inst_end(); I != E; ++I)
Chris Lattner6a91b182010-03-19 01:00:55 +0000263 if (!(*I)->AsmString.empty() &&
264 (*I)->TheDef->getName() != "PHI")
Sean Callanand0bc7f02010-02-09 23:06:35 +0000265 Instructions.push_back(
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000266 AsmWriterInst(**I,
Sean Callanand0bc7f02010-02-09 23:06:35 +0000267 AsmWriter->getValueAsInt("Variant"),
268 AsmWriter->getValueAsInt("FirstOperandColumn"),
269 AsmWriter->getValueAsInt("OperandSpacing")));
Chris Lattner076efa72004-08-01 07:43:02 +0000270
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000271 // Get the instruction numbering.
Chris Lattnerf6502782010-03-19 00:34:35 +0000272 NumberedInstructions = Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000273
Chris Lattner6af022f2006-07-14 22:59:11 +0000274 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
275 // all machine instructions are necessarily being printed, so there may be
276 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000277 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
278 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000279
Chris Lattner6af022f2006-07-14 22:59:11 +0000280 // Build an aggregate string, and build a table of offsets into it.
Benjamin Kramer94338592012-04-02 09:13:46 +0000281 SequenceToOffsetTable<std::string> StringTable;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000282
Chris Lattner259bda42006-09-27 16:44:09 +0000283 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000284 /// chunk of the output as well as indices used for operand printing.
285 std::vector<unsigned> OpcodeInfo;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000286
Benjamin Kramer94338592012-04-02 09:13:46 +0000287 // Add all strings to the string table upfront so it can generate an optimized
288 // representation.
289 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
290 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
291 if (AWI != 0 &&
Jim Grosbach016c6792012-04-18 18:56:33 +0000292 AWI->Operands[0].OperandType ==
293 AsmWriterOperand::isLiteralTextOperand &&
Benjamin Kramer94338592012-04-02 09:13:46 +0000294 !AWI->Operands[0].Str.empty()) {
295 std::string Str = AWI->Operands[0].Str;
296 UnescapeString(Str);
297 StringTable.add(Str);
298 }
299 }
300
301 StringTable.layout();
302
Chris Lattner55616402006-07-18 17:32:27 +0000303 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000304 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
305 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
306 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000307 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000308 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000309 Idx = ~0U;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000310 } else if (AWI->Operands[0].OperandType !=
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000311 AsmWriterOperand::isLiteralTextOperand ||
312 AWI->Operands[0].Str.empty()) {
313 // Something handled by the asmwriter printer, but with no leading string.
Benjamin Kramer94338592012-04-02 09:13:46 +0000314 Idx = StringTable.get("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000315 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000316 std::string Str = AWI->Operands[0].Str;
317 UnescapeString(Str);
Benjamin Kramer94338592012-04-02 09:13:46 +0000318 Idx = StringTable.get(Str);
Chris Lattner3200fc92009-09-14 01:16:36 +0000319 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000320
Chris Lattner6af022f2006-07-14 22:59:11 +0000321 // Nuke the string from the operand list. It is now handled!
322 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000323 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000324
Chris Lattner3200fc92009-09-14 01:16:36 +0000325 // Bias offset by one since we want 0 as a sentinel.
326 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000327 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000328
Chris Lattner55616402006-07-18 17:32:27 +0000329 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000330 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000331
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000332 // To reduce code size, we compactify common instructions into a few bits
333 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000334 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000335
336 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000337
Chris Lattnerb8462862006-07-18 17:56:07 +0000338 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000339 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000340 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000341 std::vector<unsigned> NumInstOpsHandled;
342 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
343 NumInstOpsHandled);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000344
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000345 // If we ran out of operands to print, we're done.
346 if (UniqueOperandCommands.empty()) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000347
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000348 // Compute the number of bits we need to represent these cases, this is
349 // ceil(log2(numentries)).
350 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000351
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000352 // If we don't have enough bits for this operand, don't include it.
353 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000354 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
355 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000356 break;
357 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000358
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000359 // Otherwise, we can include this in the initial lookup table. Add it in.
360 BitsLeft -= NumBits;
361 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000362 if (InstIdxs[i] != ~0U)
363 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000364
Chris Lattnerb8462862006-07-18 17:56:07 +0000365 // Remove the info about this operand.
366 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
367 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000368 if (!Inst->Operands.empty()) {
369 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000370 assert(NumOps <= Inst->Operands.size() &&
371 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000372 Inst->Operands.erase(Inst->Operands.begin(),
373 Inst->Operands.begin()+NumOps);
374 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000375 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000376
Chris Lattnerb8462862006-07-18 17:56:07 +0000377 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000378 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
379 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000380
381
382
Chris Lattner55616402006-07-18 17:32:27 +0000383 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000384 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000385 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000386 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000387 }
388 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000389 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000390 O << " };\n\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000391
Chris Lattner6af022f2006-07-14 22:59:11 +0000392 // Emit the string itself.
Benjamin Kramer94338592012-04-02 09:13:46 +0000393 O << " const char AsmStrs[] = {\n";
394 StringTable.emit(O, printChar);
395 O << " };\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000396
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000397 O << " O << \"\\t\";\n\n";
398
Chris Lattner6af022f2006-07-14 22:59:11 +0000399 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000400 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000401 << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000402 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000403
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000404 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000405 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000406 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
407 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
408
409 // Compute the number of bits we need to represent these cases, this is
410 // ceil(log2(numentries)).
411 unsigned NumBits = Log2_32_Ceil(Commands.size());
412 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000413
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000414 // Emit code to extract this field from Bits.
415 BitsLeft -= NumBits;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000416
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000417 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000418 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000419
Chris Lattner96c1ade2006-07-18 18:28:27 +0000420 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000421 // Emit two possibilitys with if/else.
422 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
423 << ((1 << NumBits)-1) << ") {\n"
424 << Commands[1]
425 << " } else {\n"
426 << Commands[0]
427 << " }\n\n";
Eric Christopher16870502010-09-18 18:50:27 +0000428 } else if (Commands.size() == 1) {
429 // Emit a single possibility.
430 O << Commands[0] << "\n\n";
Chris Lattnere7a589d2006-07-18 17:43:54 +0000431 } else {
432 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
433 << ((1 << NumBits)-1) << ") {\n"
434 << " default: // unreachable.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000435
Chris Lattnere7a589d2006-07-18 17:43:54 +0000436 // Print out all the cases.
437 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
438 O << " case " << i << ":\n";
439 O << Commands[i];
440 O << " break;\n";
441 }
442 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000443 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000444 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000445
Chris Lattnerb8462862006-07-18 17:56:07 +0000446 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000447 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
448 // Entire instruction has been emitted?
449 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000450 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000451 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000452 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000453 }
454 }
455
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000456
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000457 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000458 // elements in the vector.
459 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000460
461
Chris Lattner70067602009-09-18 18:10:19 +0000462 // Now that we've emitted all of the operand info that fit into 32 bits, emit
463 // information for those instructions that are left. This is a less dense
464 // encoding, but we expect the main 32-bit table to handle the majority of
465 // instructions.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000466 if (!Instructions.empty()) {
467 // Find the opcode # of inline asm.
468 O << " switch (MI->getOpcode()) {\n";
469 while (!Instructions.empty())
470 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000471
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000472 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000473 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000474 }
David Greenec8d06052009-07-29 20:10:24 +0000475
Chris Lattner0a012122006-07-18 19:06:01 +0000476 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000477}
Chris Lattner05af2612009-09-13 20:08:00 +0000478
Owen Andersonbea6f612011-06-27 21:06:21 +0000479static void
480emitRegisterNameString(raw_ostream &O, StringRef AltName,
Craig Topper9b1b25f2012-04-03 06:52:47 +0000481 const std::vector<CodeGenRegister*> &Registers) {
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000482 SequenceToOffsetTable<std::string> StringTable;
483 SmallVector<std::string, 4> AsmNames(Registers.size());
Owen Andersonbea6f612011-06-27 21:06:21 +0000484 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
485 const CodeGenRegister &Reg = *Registers[i];
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000486 std::string &AsmName = AsmNames[i];
Owen Andersonbea6f612011-06-27 21:06:21 +0000487
Owen Andersonbea6f612011-06-27 21:06:21 +0000488 // "NoRegAltName" is special. We don't need to do a lookup for that,
489 // as it's just a reference to the default register name.
490 if (AltName == "" || AltName == "NoRegAltName") {
491 AsmName = Reg.TheDef->getValueAsString("AsmName");
492 if (AsmName.empty())
493 AsmName = Reg.getName();
494 } else {
495 // Make sure the register has an alternate name for this index.
496 std::vector<Record*> AltNameList =
497 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
498 unsigned Idx = 0, e;
499 for (e = AltNameList.size();
500 Idx < e && (AltNameList[Idx]->getName() != AltName);
501 ++Idx)
502 ;
503 // If the register has an alternate name for this index, use it.
504 // Otherwise, leave it empty as an error flag.
505 if (Idx < e) {
506 std::vector<std::string> AltNames =
507 Reg.TheDef->getValueAsListOfStrings("AltNames");
508 if (AltNames.size() <= Idx)
509 throw TGError(Reg.TheDef->getLoc(),
510 (Twine("Register definition missing alt name for '") +
511 AltName + "'.").str());
512 AsmName = AltNames[Idx];
513 }
514 }
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000515 StringTable.add(AsmName);
516 }
Owen Andersonbea6f612011-06-27 21:06:21 +0000517
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000518 StringTable.layout();
519 O << " static const char AsmStrs" << AltName << "[] = {\n";
520 StringTable.emit(O, printChar);
521 O << " };\n\n";
522
Craig Toppera4bd58b2012-04-02 00:47:39 +0000523 O << " static const unsigned RegAsmOffset" << AltName << "[] = {";
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000524 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Craig Toppera4bd58b2012-04-02 00:47:39 +0000525 if ((i % 14) == 0)
526 O << "\n ";
527 O << StringTable.get(AsmNames[i]) << ", ";
Owen Andersonbea6f612011-06-27 21:06:21 +0000528 }
Craig Topper9b1b25f2012-04-03 06:52:47 +0000529 O << "\n };\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000530 << "\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000531}
Chris Lattner05af2612009-09-13 20:08:00 +0000532
533void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000534 CodeGenTarget Target(Records);
Chris Lattner05af2612009-09-13 20:08:00 +0000535 Record *AsmWriter = Target.getAsmWriter();
536 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000537 const std::vector<CodeGenRegister*> &Registers =
538 Target.getRegBank().getRegisters();
Owen Andersonbea6f612011-06-27 21:06:21 +0000539 std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices();
540 bool hasAltNames = AltNameIndices.size() > 1;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000541
Chris Lattner05af2612009-09-13 20:08:00 +0000542 O <<
543 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
544 "/// from the register set description. This returns the assembler name\n"
545 "/// for the specified register.\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000546 "const char *" << Target.getName() << ClassName << "::";
547 if (hasAltNames)
548 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
549 else
550 O << "getRegisterName(unsigned RegNo) {\n";
551 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
552 << " && \"Invalid register number!\");\n"
Chris Lattnerf6761be2009-09-14 01:26:18 +0000553 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000554
Owen Andersonbea6f612011-06-27 21:06:21 +0000555 if (hasAltNames) {
556 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i)
557 emitRegisterNameString(O, AltNameIndices[i]->getName(), Registers);
558 } else
559 emitRegisterNameString(O, "", Registers);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000560
Owen Andersonbea6f612011-06-27 21:06:21 +0000561 if (hasAltNames) {
Jakob Stoklund Olesend66b9a22012-03-15 18:05:54 +0000562 O << " const unsigned *RegAsmOffset;\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000563 << " const char *AsmStrs;\n"
564 << " switch(AltIdx) {\n"
Craig Topper655b8de2012-02-05 07:21:30 +0000565 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000566 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) {
567 StringRef Namespace = AltNameIndices[1]->getValueAsString("Namespace");
568 StringRef AltName(AltNameIndices[i]->getName());
569 O << " case " << Namespace << "::" << AltName
570 << ":\n"
571 << " AsmStrs = AsmStrs" << AltName << ";\n"
572 << " RegAsmOffset = RegAsmOffset" << AltName << ";\n"
573 << " break;\n";
574 }
575 O << "}\n";
576 }
577
578 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
579 << " \"Invalid alt name index for register!\");\n"
580 << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000581 << "}\n";
582}
583
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000584namespace {
Bill Wendling4962e612011-03-21 08:40:31 +0000585// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
586// they both have the same conditionals. In which case, we cannot print out the
587// alias for that pattern.
588class IAPrinter {
Bill Wendling4962e612011-03-21 08:40:31 +0000589 std::vector<std::string> Conds;
590 std::map<StringRef, unsigned> OpMap;
591 std::string Result;
592 std::string AsmString;
593 std::vector<Record*> ReqFeatures;
594public:
Evan Cheng68ae5b42011-07-06 02:02:33 +0000595 IAPrinter(std::string R, std::string AS)
596 : Result(R), AsmString(AS) {}
Bill Wendling4962e612011-03-21 08:40:31 +0000597
598 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling4962e612011-03-21 08:40:31 +0000599
600 void addOperand(StringRef Op, unsigned Idx) { OpMap[Op] = Idx; }
601 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
602 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
603
Evan Cheng68ae5b42011-07-06 02:02:33 +0000604 void print(raw_ostream &O) {
Bill Wendling44dcfd32011-04-07 21:20:06 +0000605 if (Conds.empty() && ReqFeatures.empty()) {
606 O.indent(6) << "return true;\n";
Evan Cheng68ae5b42011-07-06 02:02:33 +0000607 return;
Bill Wendling44dcfd32011-04-07 21:20:06 +0000608 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000609
Bill Wendling44dcfd32011-04-07 21:20:06 +0000610 O << "if (";
Bill Wendling4962e612011-03-21 08:40:31 +0000611
612 for (std::vector<std::string>::iterator
613 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
614 if (I != Conds.begin()) {
615 O << " &&\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000616 O.indent(8);
Bill Wendling4962e612011-03-21 08:40:31 +0000617 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000618
Bill Wendling4962e612011-03-21 08:40:31 +0000619 O << *I;
620 }
621
Bill Wendling44dcfd32011-04-07 21:20:06 +0000622 O << ") {\n";
623 O.indent(6) << "// " << Result << "\n";
624 O.indent(6) << "AsmString = \"" << AsmString << "\";\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000625
626 for (std::map<StringRef, unsigned>::iterator
627 I = OpMap.begin(), E = OpMap.end(); I != E; ++I)
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000628 O.indent(6) << "OpMap.push_back(std::make_pair(\"" << I->first << "\", "
629 << I->second << "));\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000630
Bill Wendling44dcfd32011-04-07 21:20:06 +0000631 O.indent(6) << "break;\n";
632 O.indent(4) << '}';
Bill Wendling4962e612011-03-21 08:40:31 +0000633 }
634
635 bool operator==(const IAPrinter &RHS) {
636 if (Conds.size() != RHS.Conds.size())
637 return false;
638
639 unsigned Idx = 0;
640 for (std::vector<std::string>::iterator
641 I = Conds.begin(), E = Conds.end(); I != E; ++I)
642 if (*I != RHS.Conds[Idx++])
643 return false;
644
645 return true;
646 }
647
648 bool operator()(const IAPrinter &RHS) {
649 if (Conds.size() < RHS.Conds.size())
650 return true;
651
652 unsigned Idx = 0;
653 for (std::vector<std::string>::iterator
654 I = Conds.begin(), E = Conds.end(); I != E; ++I)
655 if (*I != RHS.Conds[Idx++])
656 return *I < RHS.Conds[Idx++];
657
658 return false;
659 }
660};
661
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000662} // end anonymous namespace
663
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000664static void EmitGetMapOperandNumber(raw_ostream &O) {
665 O << "static unsigned getMapOperandNumber("
666 << "const SmallVectorImpl<std::pair<StringRef, unsigned> > &OpMap,\n";
667 O << " StringRef Name) {\n";
668 O << " for (SmallVectorImpl<std::pair<StringRef, unsigned> >::"
669 << "const_iterator\n";
670 O << " I = OpMap.begin(), E = OpMap.end(); I != E; ++I)\n";
671 O << " if (I->first == Name)\n";
672 O << " return I->second;\n";
Craig Topper58609b72012-04-04 04:55:46 +0000673 O << " llvm_unreachable(\"Operand not in map!\");\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000674 O << "}\n\n";
675}
676
Bill Wendling740e5b32011-06-14 03:17:20 +0000677static unsigned CountNumOperands(StringRef AsmString) {
678 unsigned NumOps = 0;
679 std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
680
681 while (!ASM.second.empty()) {
682 ++NumOps;
683 ASM = ASM.second.split(' ');
684 }
685
686 return NumOps;
687}
688
Bill Wendling393c4042011-06-15 04:31:19 +0000689static unsigned CountResultNumOperands(StringRef AsmString) {
690 unsigned NumOps = 0;
691 std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
692
693 if (!ASM.second.empty()) {
694 size_t I = ASM.second.find('{');
695 StringRef Str = ASM.second;
696 if (I != StringRef::npos)
697 Str = ASM.second.substr(I, ASM.second.find('|', I));
698
699 ASM = Str.split(' ');
700
701 do {
702 ++NumOps;
703 ASM = ASM.second.split(' ');
704 } while (!ASM.second.empty());
705 }
706
707 return NumOps;
708}
Bill Wendling740e5b32011-06-14 03:17:20 +0000709
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000710void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
711 CodeGenTarget Target(Records);
712 Record *AsmWriter = Target.getAsmWriter();
713
Bill Wendling740e5b32011-06-14 03:17:20 +0000714 if (!AsmWriter->getValueAsBit("isMCAsmWriter"))
715 return;
716
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000717 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
718 O << "#undef PRINT_ALIAS_INSTR\n\n";
719
Bill Wendling7520e3a2011-02-26 03:09:12 +0000720 // Emit the method that prints the alias instruction.
721 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
722
Bill Wendling7520e3a2011-02-26 03:09:12 +0000723 std::vector<Record*> AllInstAliases =
724 Records.getAllDerivedDefinitions("InstAlias");
725
726 // Create a map from the qualified name to a list of potential matches.
727 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
728 for (std::vector<Record*>::iterator
729 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
730 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
731 const Record *R = *I;
Bill Wendlingeef965f2011-04-13 23:36:21 +0000732 if (!R->getValueAsBit("EmitAlias"))
733 continue; // We were told not to emit the alias, but to emit the aliasee.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000734 const DagInit *DI = R->getValueAsDag("ResultInst");
735 const DefInit *Op = dynamic_cast<const DefInit*>(DI->getOperator());
736 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
737 }
738
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000739 // A map of which conditions need to be met for each instruction operand
740 // before it can be matched to the mnemonic.
741 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000742
743 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
744 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
745 std::vector<CodeGenInstAlias*> &Aliases = I->second;
746
747 for (std::vector<CodeGenInstAlias*>::iterator
748 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
749 const CodeGenInstAlias *CGA = *II;
Bill Wendling740e5b32011-06-14 03:17:20 +0000750 unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
Bill Wendling393c4042011-06-15 04:31:19 +0000751 unsigned NumResultOps =
752 CountResultNumOperands(CGA->ResultInst->AsmString);
Bill Wendling740e5b32011-06-14 03:17:20 +0000753
754 // Don't emit the alias if it has more operands than what it's aliasing.
Bill Wendling393c4042011-06-15 04:31:19 +0000755 if (NumResultOps < CountNumOperands(CGA->AsmString))
Bill Wendling740e5b32011-06-14 03:17:20 +0000756 continue;
757
Evan Cheng68ae5b42011-07-06 02:02:33 +0000758 IAPrinter *IAP = new IAPrinter(CGA->Result->getAsString(),
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000759 CGA->AsmString);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000760
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000761 std::string Cond;
762 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
763 IAP->addCond(Cond);
764
765 std::map<StringRef, unsigned> OpMap;
766 bool CantHandle = false;
767
768 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
769 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
770
771 switch (RO.Kind) {
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000772 case CodeGenInstAlias::ResultOperand::K_Record: {
773 const Record *Rec = RO.getRecord();
774 StringRef ROName = RO.getName();
775
Owen Andersonbea6f612011-06-27 21:06:21 +0000776
777 if (Rec->isSubClassOf("RegisterOperand"))
778 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000779 if (Rec->isSubClassOf("RegisterClass")) {
780 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
781 IAP->addCond(Cond);
782
783 if (!IAP->isOpMapped(ROName)) {
784 IAP->addOperand(ROName, i);
Benjamin Kramercef670a2012-03-30 23:13:40 +0000785 Cond = std::string("MRI.getRegClass(") + Target.getName() + "::" +
786 CGA->ResultOperands[i].getRecord()->getName() + "RegClassID)"
787 ".contains(MI->getOperand(" + llvm::utostr(i) + ").getReg())";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000788 IAP->addCond(Cond);
789 } else {
790 Cond = std::string("MI->getOperand(") +
791 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
792 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
793 IAP->addCond(Cond);
794 }
795 } else {
796 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
Bill Wendling740e5b32011-06-14 03:17:20 +0000797 // FIXME: We may need to handle these situations.
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000798 delete IAP;
799 IAP = 0;
800 CantHandle = true;
801 break;
802 }
803
804 break;
805 }
806 case CodeGenInstAlias::ResultOperand::K_Imm:
807 Cond = std::string("MI->getOperand(") +
808 llvm::utostr(i) + ").getImm() == " +
809 llvm::utostr(CGA->ResultOperands[i].getImm());
810 IAP->addCond(Cond);
811 break;
812 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbachbfc94292011-11-15 01:46:57 +0000813 // If this is zero_reg, something's playing tricks we're not
814 // equipped to handle.
815 if (!CGA->ResultOperands[i].getRegister()) {
816 CantHandle = true;
817 break;
818 }
819
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000820 Cond = std::string("MI->getOperand(") +
821 llvm::utostr(i) + ").getReg() == " + Target.getName() +
822 "::" + CGA->ResultOperands[i].getRegister()->getName();
823 IAP->addCond(Cond);
824 break;
825 }
826
827 if (!IAP) break;
828 }
829
830 if (CantHandle) continue;
831 IAPrinterMap[I->first].push_back(IAP);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000832 }
833 }
834
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000835 std::string Header;
836 raw_string_ostream HeaderO(Header);
837
838 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendling740e5b32011-06-14 03:17:20 +0000839 << "::printAliasInstr(const MCInst"
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000840 << " *MI, raw_ostream &OS) {\n";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000841
Bill Wendling44dcfd32011-04-07 21:20:06 +0000842 std::string Cases;
843 raw_string_ostream CasesO(Cases);
844
845 for (std::map<std::string, std::vector<IAPrinter*> >::iterator
846 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
847 std::vector<IAPrinter*> &IAPs = I->second;
848 std::vector<IAPrinter*> UniqueIAPs;
849
850 for (std::vector<IAPrinter*>::iterator
851 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
852 IAPrinter *LHS = *II;
853 bool IsDup = false;
854 for (std::vector<IAPrinter*>::iterator
855 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
856 IAPrinter *RHS = *III;
857 if (LHS != RHS && *LHS == *RHS) {
858 IsDup = true;
859 break;
860 }
861 }
862
863 if (!IsDup) UniqueIAPs.push_back(LHS);
864 }
865
866 if (UniqueIAPs.empty()) continue;
867
868 CasesO.indent(2) << "case " << I->first << ":\n";
869
870 for (std::vector<IAPrinter*>::iterator
871 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
872 IAPrinter *IAP = *II;
873 CasesO.indent(4);
Evan Cheng68ae5b42011-07-06 02:02:33 +0000874 IAP->print(CasesO);
Bill Wendling44dcfd32011-04-07 21:20:06 +0000875 CasesO << '\n';
876 }
877
Eric Christopher721ef662011-04-18 21:28:11 +0000878 CasesO.indent(4) << "return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000879 }
880
Bill Wendling740e5b32011-06-14 03:17:20 +0000881 if (CasesO.str().empty()) {
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000882 O << HeaderO.str();
Eric Christopher721ef662011-04-18 21:28:11 +0000883 O << " return false;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000884 O << "}\n\n";
885 O << "#endif // PRINT_ALIAS_INSTR\n";
886 return;
887 }
888
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000889 EmitGetMapOperandNumber(O);
890
891 O << HeaderO.str();
Bill Wendling44dcfd32011-04-07 21:20:06 +0000892 O.indent(2) << "StringRef AsmString;\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000893 O.indent(2) << "SmallVector<std::pair<StringRef, unsigned>, 4> OpMap;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000894 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher721ef662011-04-18 21:28:11 +0000895 O.indent(2) << "default: return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000896 O << CasesO.str();
897 O.indent(2) << "}\n\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000898
899 // Code that prints the alias, replacing the operands with the ones from the
900 // MCInst.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000901 O << " std::pair<StringRef, StringRef> ASM = AsmString.split(' ');\n";
902 O << " OS << '\\t' << ASM.first;\n";
903
904 O << " if (!ASM.second.empty()) {\n";
905 O << " OS << '\\t';\n";
906 O << " for (StringRef::iterator\n";
907 O << " I = ASM.second.begin(), E = ASM.second.end(); I != E; ) {\n";
908 O << " if (*I == '$') {\n";
909 O << " StringRef::iterator Start = ++I;\n";
910 O << " while (I != E &&\n";
911 O << " ((*I >= 'a' && *I <= 'z') ||\n";
912 O << " (*I >= 'A' && *I <= 'Z') ||\n";
913 O << " (*I >= '0' && *I <= '9') ||\n";
914 O << " *I == '_'))\n";
915 O << " ++I;\n";
916 O << " StringRef Name(Start, I - Start);\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000917 O << " printOperand(MI, getMapOperandNumber(OpMap, Name), OS);\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000918 O << " } else {\n";
919 O << " OS << *I++;\n";
920 O << " }\n";
921 O << " }\n";
922 O << " }\n\n";
Jim Grosbach016c6792012-04-18 18:56:33 +0000923
Eric Christopher721ef662011-04-18 21:28:11 +0000924 O << " return true;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000925 O << "}\n\n";
926
927 O << "#endif // PRINT_ALIAS_INSTR\n";
928}
Chris Lattner05af2612009-09-13 20:08:00 +0000929
930void AsmWriterEmitter::run(raw_ostream &O) {
931 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000932
Chris Lattner05af2612009-09-13 20:08:00 +0000933 EmitPrintInstruction(O);
934 EmitGetRegisterName(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +0000935 EmitPrintAliasInstruction(O);
Chris Lattner05af2612009-09-13 20:08:00 +0000936}
937