blob: c4812dcae6d5cf4ca55d9a3b27ab8d1c8fcc988e [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"
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"
Jeff Cohen615ed992005-01-22 18:50:10 +000024#include <algorithm>
Chris Lattner2e1f51b2004-08-01 05:59:33 +000025using namespace llvm;
26
Chris Lattner38c07512005-01-22 20:31:17 +000027static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +000028 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +000029 O << " case " << OpsToPrint.back().first << ": ";
30 AsmWriterOperand TheOp = OpsToPrint.back().second;
31 OpsToPrint.pop_back();
32
33 // Check to see if any other operands are identical in this list, and if so,
34 // emit a case label for them.
35 for (unsigned i = OpsToPrint.size(); i != 0; --i)
36 if (OpsToPrint[i-1].second == TheOp) {
37 O << "\n case " << OpsToPrint[i-1].first << ": ";
38 OpsToPrint.erase(OpsToPrint.begin()+i-1);
39 }
40
41 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000042 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +000043 O << "break;\n";
44}
45
Chris Lattner870c0162005-01-22 18:38:13 +000046
47/// EmitInstructions - Emit the last instruction in the vector and any other
48/// instructions that are suitably similar to it.
49static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +000050 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +000051 AsmWriterInst FirstInst = Insts.back();
52 Insts.pop_back();
53
54 std::vector<AsmWriterInst> SimilarInsts;
55 unsigned DifferingOperand = ~0;
56 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +000057 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
58 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +000059 if (DifferingOperand == ~0U) // First match!
60 DifferingOperand = DiffOp;
61
62 // If this differs in the same operand as the rest of the instructions in
63 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +000064 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +000065 SimilarInsts.push_back(Insts[i-1]);
66 Insts.erase(Insts.begin()+i-1);
67 }
68 }
69 }
70
Chris Lattnera1e8a802006-05-01 17:01:17 +000071 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +000072 << FirstInst.CGI->TheDef->getName() << ":\n";
73 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +000074 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +000075 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
76 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
77 if (i != DifferingOperand) {
78 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000079 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +000080 } else {
81 // If this is the operand that varies between all of the instructions,
82 // emit a switch for just this operand now.
83 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +000084 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +000085 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +000086 FirstInst.CGI->TheDef->getName(),
87 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +000088
Chris Lattner870c0162005-01-22 18:38:13 +000089 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +000090 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +000091 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +000092 AWI.CGI->TheDef->getName(),
93 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +000094 }
Chris Lattner38c07512005-01-22 20:31:17 +000095 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
96 while (!OpsToPrint.empty())
97 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +000098 O << " }";
99 }
100 O << "\n";
101 }
Chris Lattner870c0162005-01-22 18:38:13 +0000102 O << " break;\n";
103}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000104
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000105void AsmWriterEmitter::
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000106FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000107 std::vector<unsigned> &InstIdxs,
108 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000109 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000110
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000111 // This vector parallels UniqueOperandCommands, keeping track of which
112 // instructions each case are used for. It is a comma separated string of
113 // enums.
114 std::vector<std::string> InstrsForCase;
115 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000116 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000117
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000118 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
119 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Bill Wendlingb9449d62010-07-16 23:10:00 +0000120 if (Inst == 0) continue; // PHI, INLINEASM, PROLOG_LABEL, etc.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000121
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000122 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000123 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000124 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000125
Chris Lattnerb8462862006-07-18 17:56:07 +0000126 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000127
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000128 // Check to see if we already have 'Command' in UniqueOperandCommands.
129 // If not, add it.
130 bool FoundIt = false;
131 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
132 if (UniqueOperandCommands[idx] == Command) {
133 InstIdxs[i] = idx;
134 InstrsForCase[idx] += ", ";
135 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
136 FoundIt = true;
137 break;
138 }
139 if (!FoundIt) {
140 InstIdxs[i] = UniqueOperandCommands.size();
141 UniqueOperandCommands.push_back(Command);
142 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000143
144 // This command matches one operand so far.
145 InstOpsUsed.push_back(1);
146 }
147 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000148
Chris Lattner96c1ade2006-07-18 18:28:27 +0000149 // For each entry of UniqueOperandCommands, there is a set of instructions
150 // that uses it. If the next command of all instructions in the set are
151 // identical, fold it into the command.
152 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
153 CommandIdx != e; ++CommandIdx) {
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000154
Chris Lattner96c1ade2006-07-18 18:28:27 +0000155 for (unsigned Op = 1; ; ++Op) {
156 // Scan for the first instruction in the set.
157 std::vector<unsigned>::iterator NIT =
158 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
159 if (NIT == InstIdxs.end()) break; // No commonality.
160
161 // If this instruction has no more operands, we isn't anything to merge
162 // into this command.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000163 const AsmWriterInst *FirstInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000164 getAsmWriterInstByID(NIT-InstIdxs.begin());
165 if (!FirstInst || FirstInst->Operands.size() == Op)
166 break;
167
168 // Otherwise, scan to see if all of the other instructions in this command
169 // set share the operand.
170 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000171 // Keep track of the maximum, number of operands or any
172 // instruction we see in the group.
173 size_t MaxSize = FirstInst->Operands.size();
174
Chris Lattner96c1ade2006-07-18 18:28:27 +0000175 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
176 NIT != InstIdxs.end();
177 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
178 // Okay, found another instruction in this command set. If the operand
179 // matches, we're ok, otherwise bail out.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000180 const AsmWriterInst *OtherInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000181 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000182
183 if (OtherInst &&
184 OtherInst->Operands.size() > FirstInst->Operands.size())
185 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
186
Chris Lattner96c1ade2006-07-18 18:28:27 +0000187 if (!OtherInst || OtherInst->Operands.size() == Op ||
188 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
189 AllSame = false;
190 break;
191 }
192 }
193 if (!AllSame) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000194
Chris Lattner96c1ade2006-07-18 18:28:27 +0000195 // Okay, everything in this command set has the same next operand. Add it
196 // to UniqueOperandCommands and remember that it was consumed.
197 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000198
Chris Lattner96c1ade2006-07-18 18:28:27 +0000199 UniqueOperandCommands[CommandIdx] += Command;
200 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000201 }
202 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000203
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000204 // Prepend some of the instructions each case is used for onto the case val.
205 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
206 std::string Instrs = InstrsForCase[i];
207 if (Instrs.size() > 70) {
208 Instrs.erase(Instrs.begin()+70, Instrs.end());
209 Instrs += "...";
210 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000211
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000212 if (!Instrs.empty())
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000213 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000214 UniqueOperandCommands[i];
215 }
216}
217
218
Daniel Dunbar9bd34602009-10-17 20:43:42 +0000219static void UnescapeString(std::string &Str) {
220 for (unsigned i = 0; i != Str.size(); ++i) {
221 if (Str[i] == '\\' && i != Str.size()-1) {
222 switch (Str[i+1]) {
223 default: continue; // Don't execute the code after the switch.
224 case 'a': Str[i] = '\a'; break;
225 case 'b': Str[i] = '\b'; break;
226 case 'e': Str[i] = 27; break;
227 case 'f': Str[i] = '\f'; break;
228 case 'n': Str[i] = '\n'; break;
229 case 'r': Str[i] = '\r'; break;
230 case 't': Str[i] = '\t'; break;
231 case 'v': Str[i] = '\v'; break;
232 case '"': Str[i] = '\"'; break;
233 case '\'': Str[i] = '\''; break;
234 case '\\': Str[i] = '\\'; break;
235 }
236 // Nuke the second character.
237 Str.erase(Str.begin()+i+1);
238 }
239 }
240}
241
Chris Lattner05af2612009-09-13 20:08:00 +0000242/// EmitPrintInstruction - Generate the code for the "printInstruction" method
243/// implementation.
244void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000245 CodeGenTarget Target(Records);
Chris Lattner175580c2004-08-14 22:50:53 +0000246 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000247 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jim Grosbachca96a862010-09-30 01:29:54 +0000248 bool isMC = AsmWriter->getValueAsBit("isMCAsmWriter");
249 const char *MachineInstrClassName = isMC ? "MCInst" : "MachineInstr";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000250
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000251 O <<
252 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000253 "/// from the instruction set description.\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000254 "void " << Target.getName() << ClassName
Jim Grosbachca96a862010-09-30 01:29:54 +0000255 << "::printInstruction(const " << MachineInstrClassName
256 << " *MI, raw_ostream &O) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000257
Chris Lattner5765dba2005-01-22 17:40:38 +0000258 std::vector<AsmWriterInst> Instructions;
259
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000260 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
261 E = Target.inst_end(); I != E; ++I)
Chris Lattner6a91b182010-03-19 01:00:55 +0000262 if (!(*I)->AsmString.empty() &&
263 (*I)->TheDef->getName() != "PHI")
Sean Callanand0bc7f02010-02-09 23:06:35 +0000264 Instructions.push_back(
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000265 AsmWriterInst(**I,
Sean Callanand0bc7f02010-02-09 23:06:35 +0000266 AsmWriter->getValueAsInt("Variant"),
267 AsmWriter->getValueAsInt("FirstOperandColumn"),
268 AsmWriter->getValueAsInt("OperandSpacing")));
Chris Lattner076efa72004-08-01 07:43:02 +0000269
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000270 // Get the instruction numbering.
Chris Lattnerf6502782010-03-19 00:34:35 +0000271 NumberedInstructions = Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000272
Chris Lattner6af022f2006-07-14 22:59:11 +0000273 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
274 // all machine instructions are necessarily being printed, so there may be
275 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000276 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
277 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000278
Chris Lattner6af022f2006-07-14 22:59:11 +0000279 // Build an aggregate string, and build a table of offsets into it.
Chris Lattner3200fc92009-09-14 01:16:36 +0000280 StringToOffsetTable StringTable;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000281
Chris Lattner259bda42006-09-27 16:44:09 +0000282 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000283 /// chunk of the output as well as indices used for operand printing.
284 std::vector<unsigned> OpcodeInfo;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000285
Chris Lattner55616402006-07-18 17:32:27 +0000286 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000287 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
288 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
289 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000290 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000291 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000292 Idx = ~0U;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000293 } else if (AWI->Operands[0].OperandType !=
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000294 AsmWriterOperand::isLiteralTextOperand ||
295 AWI->Operands[0].Str.empty()) {
296 // Something handled by the asmwriter printer, but with no leading string.
Chris Lattner3200fc92009-09-14 01:16:36 +0000297 Idx = StringTable.GetOrAddStringOffset("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000298 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000299 std::string Str = AWI->Operands[0].Str;
300 UnescapeString(Str);
301 Idx = StringTable.GetOrAddStringOffset(Str);
302 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000303
Chris Lattner6af022f2006-07-14 22:59:11 +0000304 // Nuke the string from the operand list. It is now handled!
305 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000306 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000307
Chris Lattner3200fc92009-09-14 01:16:36 +0000308 // Bias offset by one since we want 0 as a sentinel.
309 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000310 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000311
Chris Lattner55616402006-07-18 17:32:27 +0000312 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000313 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000314
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000315 // To reduce code size, we compactify common instructions into a few bits
316 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000317 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000318
319 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000320
Chris Lattnerb8462862006-07-18 17:56:07 +0000321 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000322 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000323 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000324 std::vector<unsigned> NumInstOpsHandled;
325 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
326 NumInstOpsHandled);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000327
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000328 // If we ran out of operands to print, we're done.
329 if (UniqueOperandCommands.empty()) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000330
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000331 // Compute the number of bits we need to represent these cases, this is
332 // ceil(log2(numentries)).
333 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000334
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000335 // If we don't have enough bits for this operand, don't include it.
336 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000337 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
338 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000339 break;
340 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000341
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000342 // Otherwise, we can include this in the initial lookup table. Add it in.
343 BitsLeft -= NumBits;
344 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000345 if (InstIdxs[i] != ~0U)
346 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000347
Chris Lattnerb8462862006-07-18 17:56:07 +0000348 // Remove the info about this operand.
349 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
350 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000351 if (!Inst->Operands.empty()) {
352 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000353 assert(NumOps <= Inst->Operands.size() &&
354 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000355 Inst->Operands.erase(Inst->Operands.begin(),
356 Inst->Operands.begin()+NumOps);
357 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000358 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000359
Chris Lattnerb8462862006-07-18 17:56:07 +0000360 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000361 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
362 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000363
364
365
Chris Lattner55616402006-07-18 17:32:27 +0000366 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000367 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000368 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000369 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000370 }
371 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000372 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000373 O << " };\n\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000374
Chris Lattner6af022f2006-07-14 22:59:11 +0000375 // Emit the string itself.
Jakob Stoklund Olesenbcfa9822012-03-15 18:05:57 +0000376 O << " const char *AsmStrs = \n";
Chris Lattner3200fc92009-09-14 01:16:36 +0000377 StringTable.EmitString(O);
378 O << ";\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000379
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000380 O << " O << \"\\t\";\n\n";
381
Chris Lattner6af022f2006-07-14 22:59:11 +0000382 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000383 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000384 << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000385 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000386
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000387 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000388 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000389 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
390 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
391
392 // Compute the number of bits we need to represent these cases, this is
393 // ceil(log2(numentries)).
394 unsigned NumBits = Log2_32_Ceil(Commands.size());
395 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000396
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000397 // Emit code to extract this field from Bits.
398 BitsLeft -= NumBits;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000399
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000400 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000401 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000402
Chris Lattner96c1ade2006-07-18 18:28:27 +0000403 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000404 // Emit two possibilitys with if/else.
405 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
406 << ((1 << NumBits)-1) << ") {\n"
407 << Commands[1]
408 << " } else {\n"
409 << Commands[0]
410 << " }\n\n";
Eric Christopher16870502010-09-18 18:50:27 +0000411 } else if (Commands.size() == 1) {
412 // Emit a single possibility.
413 O << Commands[0] << "\n\n";
Chris Lattnere7a589d2006-07-18 17:43:54 +0000414 } else {
415 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
416 << ((1 << NumBits)-1) << ") {\n"
417 << " default: // unreachable.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000418
Chris Lattnere7a589d2006-07-18 17:43:54 +0000419 // Print out all the cases.
420 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
421 O << " case " << i << ":\n";
422 O << Commands[i];
423 O << " break;\n";
424 }
425 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000426 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000427 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000428
Chris Lattnerb8462862006-07-18 17:56:07 +0000429 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000430 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
431 // Entire instruction has been emitted?
432 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000433 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000434 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000435 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000436 }
437 }
438
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000439
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000440 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000441 // elements in the vector.
442 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000443
444
Chris Lattner70067602009-09-18 18:10:19 +0000445 // Now that we've emitted all of the operand info that fit into 32 bits, emit
446 // information for those instructions that are left. This is a less dense
447 // encoding, but we expect the main 32-bit table to handle the majority of
448 // instructions.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000449 if (!Instructions.empty()) {
450 // Find the opcode # of inline asm.
451 O << " switch (MI->getOpcode()) {\n";
452 while (!Instructions.empty())
453 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000454
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000455 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000456 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000457 }
David Greenec8d06052009-07-29 20:10:24 +0000458
Chris Lattner0a012122006-07-18 19:06:01 +0000459 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000460}
Chris Lattner05af2612009-09-13 20:08:00 +0000461
Owen Andersonbea6f612011-06-27 21:06:21 +0000462static void
463emitRegisterNameString(raw_ostream &O, StringRef AltName,
464 const std::vector<CodeGenRegister*> &Registers) {
465 StringToOffsetTable StringTable;
Jakob Stoklund Olesend66b9a22012-03-15 18:05:54 +0000466 O << " static const unsigned RegAsmOffset" << AltName << "[] = {\n ";
Owen Andersonbea6f612011-06-27 21:06:21 +0000467 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
468 const CodeGenRegister &Reg = *Registers[i];
469
Francois Pichet2e10b082011-06-29 11:25:34 +0000470 std::string AsmName;
Owen Andersonbea6f612011-06-27 21:06:21 +0000471 // "NoRegAltName" is special. We don't need to do a lookup for that,
472 // as it's just a reference to the default register name.
473 if (AltName == "" || AltName == "NoRegAltName") {
474 AsmName = Reg.TheDef->getValueAsString("AsmName");
475 if (AsmName.empty())
476 AsmName = Reg.getName();
477 } else {
478 // Make sure the register has an alternate name for this index.
479 std::vector<Record*> AltNameList =
480 Reg.TheDef->getValueAsListOfDefs("RegAltNameIndices");
481 unsigned Idx = 0, e;
482 for (e = AltNameList.size();
483 Idx < e && (AltNameList[Idx]->getName() != AltName);
484 ++Idx)
485 ;
486 // If the register has an alternate name for this index, use it.
487 // Otherwise, leave it empty as an error flag.
488 if (Idx < e) {
489 std::vector<std::string> AltNames =
490 Reg.TheDef->getValueAsListOfStrings("AltNames");
491 if (AltNames.size() <= Idx)
492 throw TGError(Reg.TheDef->getLoc(),
493 (Twine("Register definition missing alt name for '") +
494 AltName + "'.").str());
495 AsmName = AltNames[Idx];
496 }
497 }
498
Jakob Stoklund Olesenbcfa9822012-03-15 18:05:57 +0000499 O << StringTable.GetOrAddStringOffset(AsmName);
Owen Andersonbea6f612011-06-27 21:06:21 +0000500 if (((i + 1) % 14) == 0)
501 O << ",\n ";
502 else
503 O << ", ";
504
505 }
506 O << "0\n"
507 << " };\n"
508 << "\n";
509
Jakob Stoklund Olesend66b9a22012-03-15 18:05:54 +0000510 O << " const char *AsmStrs" << AltName << " =\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000511 StringTable.EmitString(O);
512 O << ";\n";
513}
Chris Lattner05af2612009-09-13 20:08:00 +0000514
515void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000516 CodeGenTarget Target(Records);
Chris Lattner05af2612009-09-13 20:08:00 +0000517 Record *AsmWriter = Target.getAsmWriter();
518 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000519 const std::vector<CodeGenRegister*> &Registers =
520 Target.getRegBank().getRegisters();
Owen Andersonbea6f612011-06-27 21:06:21 +0000521 std::vector<Record*> AltNameIndices = Target.getRegAltNameIndices();
522 bool hasAltNames = AltNameIndices.size() > 1;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000523
Chris Lattner05af2612009-09-13 20:08:00 +0000524 O <<
525 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
526 "/// from the register set description. This returns the assembler name\n"
527 "/// for the specified register.\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000528 "const char *" << Target.getName() << ClassName << "::";
529 if (hasAltNames)
530 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
531 else
532 O << "getRegisterName(unsigned RegNo) {\n";
533 O << " assert(RegNo && RegNo < " << (Registers.size()+1)
534 << " && \"Invalid register number!\");\n"
Chris Lattnerf6761be2009-09-14 01:26:18 +0000535 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000536
Owen Andersonbea6f612011-06-27 21:06:21 +0000537 if (hasAltNames) {
538 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i)
539 emitRegisterNameString(O, AltNameIndices[i]->getName(), Registers);
540 } else
541 emitRegisterNameString(O, "", Registers);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000542
Owen Andersonbea6f612011-06-27 21:06:21 +0000543 if (hasAltNames) {
Jakob Stoklund Olesend66b9a22012-03-15 18:05:54 +0000544 O << " const unsigned *RegAsmOffset;\n"
Owen Andersonbea6f612011-06-27 21:06:21 +0000545 << " const char *AsmStrs;\n"
546 << " switch(AltIdx) {\n"
Craig Topper655b8de2012-02-05 07:21:30 +0000547 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
Owen Andersonbea6f612011-06-27 21:06:21 +0000548 for (unsigned i = 0, e = AltNameIndices.size(); i < e; ++i) {
549 StringRef Namespace = AltNameIndices[1]->getValueAsString("Namespace");
550 StringRef AltName(AltNameIndices[i]->getName());
551 O << " case " << Namespace << "::" << AltName
552 << ":\n"
553 << " AsmStrs = AsmStrs" << AltName << ";\n"
554 << " RegAsmOffset = RegAsmOffset" << AltName << ";\n"
555 << " break;\n";
556 }
557 O << "}\n";
558 }
559
560 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
561 << " \"Invalid alt name index for register!\");\n"
562 << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000563 << "}\n";
564}
565
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000566void AsmWriterEmitter::EmitGetInstructionName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000567 CodeGenTarget Target(Records);
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000568 Record *AsmWriter = Target.getAsmWriter();
569 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
570
Chris Lattnerf6502782010-03-19 00:34:35 +0000571 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
572 Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000573
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000574 StringToOffsetTable StringTable;
575 O <<
576"\n\n#ifdef GET_INSTRUCTION_NAME\n"
577"#undef GET_INSTRUCTION_NAME\n\n"
578"/// getInstructionName: This method is automatically generated by tblgen\n"
579"/// from the instruction set description. This returns the enum name of the\n"
580"/// specified instruction.\n"
581 "const char *" << Target.getName() << ClassName
582 << "::getInstructionName(unsigned Opcode) {\n"
583 << " assert(Opcode < " << NumberedInstructions.size()
584 << " && \"Invalid instruction number!\");\n"
585 << "\n"
Jakob Stoklund Olesend66b9a22012-03-15 18:05:54 +0000586 << " static const unsigned InstAsmOffset[] = {";
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000587 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
588 const CodeGenInstruction &Inst = *NumberedInstructions[i];
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000589
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000590 std::string AsmName = Inst.TheDef->getName();
591 if ((i % 14) == 0)
592 O << "\n ";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000593
Jakob Stoklund Olesenbcfa9822012-03-15 18:05:57 +0000594 O << StringTable.GetOrAddStringOffset(AsmName) << ", ";
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000595 }
596 O << "0\n"
597 << " };\n"
598 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000599
Jakob Stoklund Olesend66b9a22012-03-15 18:05:54 +0000600 O << " const char *Strs =\n";
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000601 StringTable.EmitString(O);
602 O << ";\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000603
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000604 O << " return Strs+InstAsmOffset[Opcode];\n"
605 << "}\n\n#endif\n";
606}
607
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000608namespace {
Bill Wendling4962e612011-03-21 08:40:31 +0000609// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
610// they both have the same conditionals. In which case, we cannot print out the
611// alias for that pattern.
612class IAPrinter {
Bill Wendling4962e612011-03-21 08:40:31 +0000613 std::vector<std::string> Conds;
614 std::map<StringRef, unsigned> OpMap;
615 std::string Result;
616 std::string AsmString;
617 std::vector<Record*> ReqFeatures;
618public:
Evan Cheng68ae5b42011-07-06 02:02:33 +0000619 IAPrinter(std::string R, std::string AS)
620 : Result(R), AsmString(AS) {}
Bill Wendling4962e612011-03-21 08:40:31 +0000621
622 void addCond(const std::string &C) { Conds.push_back(C); }
Bill Wendling4962e612011-03-21 08:40:31 +0000623
624 void addOperand(StringRef Op, unsigned Idx) { OpMap[Op] = Idx; }
625 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
626 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
627
Evan Cheng68ae5b42011-07-06 02:02:33 +0000628 void print(raw_ostream &O) {
Bill Wendling44dcfd32011-04-07 21:20:06 +0000629 if (Conds.empty() && ReqFeatures.empty()) {
630 O.indent(6) << "return true;\n";
Evan Cheng68ae5b42011-07-06 02:02:33 +0000631 return;
Bill Wendling44dcfd32011-04-07 21:20:06 +0000632 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000633
Bill Wendling44dcfd32011-04-07 21:20:06 +0000634 O << "if (";
Bill Wendling4962e612011-03-21 08:40:31 +0000635
636 for (std::vector<std::string>::iterator
637 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
638 if (I != Conds.begin()) {
639 O << " &&\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000640 O.indent(8);
Bill Wendling4962e612011-03-21 08:40:31 +0000641 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000642
Bill Wendling4962e612011-03-21 08:40:31 +0000643 O << *I;
644 }
645
Bill Wendling44dcfd32011-04-07 21:20:06 +0000646 O << ") {\n";
647 O.indent(6) << "// " << Result << "\n";
648 O.indent(6) << "AsmString = \"" << AsmString << "\";\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000649
650 for (std::map<StringRef, unsigned>::iterator
651 I = OpMap.begin(), E = OpMap.end(); I != E; ++I)
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000652 O.indent(6) << "OpMap.push_back(std::make_pair(\"" << I->first << "\", "
653 << I->second << "));\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000654
Bill Wendling44dcfd32011-04-07 21:20:06 +0000655 O.indent(6) << "break;\n";
656 O.indent(4) << '}';
Bill Wendling4962e612011-03-21 08:40:31 +0000657 }
658
659 bool operator==(const IAPrinter &RHS) {
660 if (Conds.size() != RHS.Conds.size())
661 return false;
662
663 unsigned Idx = 0;
664 for (std::vector<std::string>::iterator
665 I = Conds.begin(), E = Conds.end(); I != E; ++I)
666 if (*I != RHS.Conds[Idx++])
667 return false;
668
669 return true;
670 }
671
672 bool operator()(const IAPrinter &RHS) {
673 if (Conds.size() < RHS.Conds.size())
674 return true;
675
676 unsigned Idx = 0;
677 for (std::vector<std::string>::iterator
678 I = Conds.begin(), E = Conds.end(); I != E; ++I)
679 if (*I != RHS.Conds[Idx++])
680 return *I < RHS.Conds[Idx++];
681
682 return false;
683 }
684};
685
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000686} // end anonymous namespace
687
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000688static void EmitGetMapOperandNumber(raw_ostream &O) {
689 O << "static unsigned getMapOperandNumber("
690 << "const SmallVectorImpl<std::pair<StringRef, unsigned> > &OpMap,\n";
691 O << " StringRef Name) {\n";
692 O << " for (SmallVectorImpl<std::pair<StringRef, unsigned> >::"
693 << "const_iterator\n";
694 O << " I = OpMap.begin(), E = OpMap.end(); I != E; ++I)\n";
695 O << " if (I->first == Name)\n";
696 O << " return I->second;\n";
697 O << " assert(false && \"Operand not in map!\");\n";
698 O << " return 0;\n";
699 O << "}\n\n";
700}
701
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000702void AsmWriterEmitter::EmitRegIsInRegClass(raw_ostream &O) {
703 CodeGenTarget Target(Records);
Bill Wendling7520e3a2011-02-26 03:09:12 +0000704
705 // Enumerate the register classes.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000706 ArrayRef<CodeGenRegisterClass*> RegisterClasses =
707 Target.getRegBank().getRegClasses();
Bill Wendling7520e3a2011-02-26 03:09:12 +0000708
709 O << "namespace { // Register classes\n";
710 O << " enum RegClass {\n";
711
712 // Emit the register enum value for each RegisterClass.
713 for (unsigned I = 0, E = RegisterClasses.size(); I != E; ++I) {
714 if (I != 0) O << ",\n";
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +0000715 O << " RC_" << RegisterClasses[I]->getName();
Bill Wendling7520e3a2011-02-26 03:09:12 +0000716 }
717
718 O << "\n };\n";
719 O << "} // end anonymous namespace\n\n";
720
721 // Emit a function that returns 'true' if a regsiter is part of a particular
722 // register class. I.e., RAX is part of GR64 on X86.
723 O << "static bool regIsInRegisterClass"
724 << "(unsigned RegClass, unsigned Reg) {\n";
725
726 // Emit the switch that checks if a register belongs to a particular register
727 // class.
728 O << " switch (RegClass) {\n";
729 O << " default: break;\n";
730
731 for (unsigned I = 0, E = RegisterClasses.size(); I != E; ++I) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000732 const CodeGenRegisterClass &RC = *RegisterClasses[I];
Bill Wendling7520e3a2011-02-26 03:09:12 +0000733
734 // Give the register class a legal C name if it's anonymous.
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +0000735 std::string Name = RC.getName();
Bill Wendling7520e3a2011-02-26 03:09:12 +0000736 O << " case RC_" << Name << ":\n";
737
738 // Emit the register list now.
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000739 unsigned IE = RC.getOrder().size();
Bill Wendling7520e3a2011-02-26 03:09:12 +0000740 if (IE == 1) {
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000741 O << " if (Reg == " << getQualifiedName(RC.getOrder()[0]) << ")\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000742 O << " return true;\n";
743 } else {
744 O << " switch (Reg) {\n";
745 O << " default: break;\n";
746
747 for (unsigned II = 0; II != IE; ++II) {
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000748 Record *Reg = RC.getOrder()[II];
Bill Wendling7520e3a2011-02-26 03:09:12 +0000749 O << " case " << getQualifiedName(Reg) << ":\n";
750 }
751
752 O << " return true;\n";
753 O << " }\n";
754 }
755
756 O << " break;\n";
757 }
758
759 O << " }\n\n";
760 O << " return false;\n";
761 O << "}\n\n";
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000762}
763
Bill Wendling740e5b32011-06-14 03:17:20 +0000764static unsigned CountNumOperands(StringRef AsmString) {
765 unsigned NumOps = 0;
766 std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
767
768 while (!ASM.second.empty()) {
769 ++NumOps;
770 ASM = ASM.second.split(' ');
771 }
772
773 return NumOps;
774}
775
Bill Wendling393c4042011-06-15 04:31:19 +0000776static unsigned CountResultNumOperands(StringRef AsmString) {
777 unsigned NumOps = 0;
778 std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
779
780 if (!ASM.second.empty()) {
781 size_t I = ASM.second.find('{');
782 StringRef Str = ASM.second;
783 if (I != StringRef::npos)
784 Str = ASM.second.substr(I, ASM.second.find('|', I));
785
786 ASM = Str.split(' ');
787
788 do {
789 ++NumOps;
790 ASM = ASM.second.split(' ');
791 } while (!ASM.second.empty());
792 }
793
794 return NumOps;
795}
Bill Wendling740e5b32011-06-14 03:17:20 +0000796
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000797void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
798 CodeGenTarget Target(Records);
799 Record *AsmWriter = Target.getAsmWriter();
800
Bill Wendling740e5b32011-06-14 03:17:20 +0000801 if (!AsmWriter->getValueAsBit("isMCAsmWriter"))
802 return;
803
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000804 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
805 O << "#undef PRINT_ALIAS_INSTR\n\n";
806
807 EmitRegIsInRegClass(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +0000808
809 // Emit the method that prints the alias instruction.
810 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
811
Bill Wendling7520e3a2011-02-26 03:09:12 +0000812 std::vector<Record*> AllInstAliases =
813 Records.getAllDerivedDefinitions("InstAlias");
814
815 // Create a map from the qualified name to a list of potential matches.
816 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
817 for (std::vector<Record*>::iterator
818 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
819 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
820 const Record *R = *I;
Bill Wendlingeef965f2011-04-13 23:36:21 +0000821 if (!R->getValueAsBit("EmitAlias"))
822 continue; // We were told not to emit the alias, but to emit the aliasee.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000823 const DagInit *DI = R->getValueAsDag("ResultInst");
824 const DefInit *Op = dynamic_cast<const DefInit*>(DI->getOperator());
825 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
826 }
827
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000828 // A map of which conditions need to be met for each instruction operand
829 // before it can be matched to the mnemonic.
830 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000831
832 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
833 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
834 std::vector<CodeGenInstAlias*> &Aliases = I->second;
835
836 for (std::vector<CodeGenInstAlias*>::iterator
837 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
838 const CodeGenInstAlias *CGA = *II;
Bill Wendling740e5b32011-06-14 03:17:20 +0000839 unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
Bill Wendling393c4042011-06-15 04:31:19 +0000840 unsigned NumResultOps =
841 CountResultNumOperands(CGA->ResultInst->AsmString);
Bill Wendling740e5b32011-06-14 03:17:20 +0000842
843 // Don't emit the alias if it has more operands than what it's aliasing.
Bill Wendling393c4042011-06-15 04:31:19 +0000844 if (NumResultOps < CountNumOperands(CGA->AsmString))
Bill Wendling740e5b32011-06-14 03:17:20 +0000845 continue;
846
Evan Cheng68ae5b42011-07-06 02:02:33 +0000847 IAPrinter *IAP = new IAPrinter(CGA->Result->getAsString(),
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000848 CGA->AsmString);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000849
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000850 std::string Cond;
851 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
852 IAP->addCond(Cond);
853
854 std::map<StringRef, unsigned> OpMap;
855 bool CantHandle = false;
856
857 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
858 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
859
860 switch (RO.Kind) {
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000861 case CodeGenInstAlias::ResultOperand::K_Record: {
862 const Record *Rec = RO.getRecord();
863 StringRef ROName = RO.getName();
864
Owen Andersonbea6f612011-06-27 21:06:21 +0000865
866 if (Rec->isSubClassOf("RegisterOperand"))
867 Rec = Rec->getValueAsDef("RegClass");
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000868 if (Rec->isSubClassOf("RegisterClass")) {
869 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
870 IAP->addCond(Cond);
871
872 if (!IAP->isOpMapped(ROName)) {
873 IAP->addOperand(ROName, i);
874 Cond = std::string("regIsInRegisterClass(RC_") +
875 CGA->ResultOperands[i].getRecord()->getName() +
876 ", MI->getOperand(" + llvm::utostr(i) + ").getReg())";
877 IAP->addCond(Cond);
878 } else {
879 Cond = std::string("MI->getOperand(") +
880 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
881 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
882 IAP->addCond(Cond);
883 }
884 } else {
885 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
Bill Wendling740e5b32011-06-14 03:17:20 +0000886 // FIXME: We may need to handle these situations.
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000887 delete IAP;
888 IAP = 0;
889 CantHandle = true;
890 break;
891 }
892
893 break;
894 }
895 case CodeGenInstAlias::ResultOperand::K_Imm:
896 Cond = std::string("MI->getOperand(") +
897 llvm::utostr(i) + ").getImm() == " +
898 llvm::utostr(CGA->ResultOperands[i].getImm());
899 IAP->addCond(Cond);
900 break;
901 case CodeGenInstAlias::ResultOperand::K_Reg:
Jim Grosbachbfc94292011-11-15 01:46:57 +0000902 // If this is zero_reg, something's playing tricks we're not
903 // equipped to handle.
904 if (!CGA->ResultOperands[i].getRegister()) {
905 CantHandle = true;
906 break;
907 }
908
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000909 Cond = std::string("MI->getOperand(") +
910 llvm::utostr(i) + ").getReg() == " + Target.getName() +
911 "::" + CGA->ResultOperands[i].getRegister()->getName();
912 IAP->addCond(Cond);
913 break;
914 }
915
916 if (!IAP) break;
917 }
918
919 if (CantHandle) continue;
920 IAPrinterMap[I->first].push_back(IAP);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000921 }
922 }
923
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000924 std::string Header;
925 raw_string_ostream HeaderO(Header);
926
927 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendling740e5b32011-06-14 03:17:20 +0000928 << "::printAliasInstr(const MCInst"
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000929 << " *MI, raw_ostream &OS) {\n";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000930
Bill Wendling44dcfd32011-04-07 21:20:06 +0000931 std::string Cases;
932 raw_string_ostream CasesO(Cases);
933
934 for (std::map<std::string, std::vector<IAPrinter*> >::iterator
935 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
936 std::vector<IAPrinter*> &IAPs = I->second;
937 std::vector<IAPrinter*> UniqueIAPs;
938
939 for (std::vector<IAPrinter*>::iterator
940 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
941 IAPrinter *LHS = *II;
942 bool IsDup = false;
943 for (std::vector<IAPrinter*>::iterator
944 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
945 IAPrinter *RHS = *III;
946 if (LHS != RHS && *LHS == *RHS) {
947 IsDup = true;
948 break;
949 }
950 }
951
952 if (!IsDup) UniqueIAPs.push_back(LHS);
953 }
954
955 if (UniqueIAPs.empty()) continue;
956
957 CasesO.indent(2) << "case " << I->first << ":\n";
958
959 for (std::vector<IAPrinter*>::iterator
960 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
961 IAPrinter *IAP = *II;
962 CasesO.indent(4);
Evan Cheng68ae5b42011-07-06 02:02:33 +0000963 IAP->print(CasesO);
Bill Wendling44dcfd32011-04-07 21:20:06 +0000964 CasesO << '\n';
965 }
966
Eric Christopher721ef662011-04-18 21:28:11 +0000967 CasesO.indent(4) << "return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000968 }
969
Bill Wendling740e5b32011-06-14 03:17:20 +0000970 if (CasesO.str().empty()) {
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000971 O << HeaderO.str();
Eric Christopher721ef662011-04-18 21:28:11 +0000972 O << " return false;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000973 O << "}\n\n";
974 O << "#endif // PRINT_ALIAS_INSTR\n";
975 return;
976 }
977
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000978 EmitGetMapOperandNumber(O);
979
980 O << HeaderO.str();
Bill Wendling44dcfd32011-04-07 21:20:06 +0000981 O.indent(2) << "StringRef AsmString;\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000982 O.indent(2) << "SmallVector<std::pair<StringRef, unsigned>, 4> OpMap;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000983 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher721ef662011-04-18 21:28:11 +0000984 O.indent(2) << "default: return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000985 O << CasesO.str();
986 O.indent(2) << "}\n\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000987
988 // Code that prints the alias, replacing the operands with the ones from the
989 // MCInst.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000990 O << " std::pair<StringRef, StringRef> ASM = AsmString.split(' ');\n";
991 O << " OS << '\\t' << ASM.first;\n";
992
993 O << " if (!ASM.second.empty()) {\n";
994 O << " OS << '\\t';\n";
995 O << " for (StringRef::iterator\n";
996 O << " I = ASM.second.begin(), E = ASM.second.end(); I != E; ) {\n";
997 O << " if (*I == '$') {\n";
998 O << " StringRef::iterator Start = ++I;\n";
999 O << " while (I != E &&\n";
1000 O << " ((*I >= 'a' && *I <= 'z') ||\n";
1001 O << " (*I >= 'A' && *I <= 'Z') ||\n";
1002 O << " (*I >= '0' && *I <= '9') ||\n";
1003 O << " *I == '_'))\n";
1004 O << " ++I;\n";
1005 O << " StringRef Name(Start, I - Start);\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +00001006 O << " printOperand(MI, getMapOperandNumber(OpMap, Name), OS);\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001007 O << " } else {\n";
1008 O << " OS << *I++;\n";
1009 O << " }\n";
1010 O << " }\n";
1011 O << " }\n\n";
1012
Eric Christopher721ef662011-04-18 21:28:11 +00001013 O << " return true;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001014 O << "}\n\n";
1015
1016 O << "#endif // PRINT_ALIAS_INSTR\n";
1017}
Chris Lattner05af2612009-09-13 20:08:00 +00001018
1019void AsmWriterEmitter::run(raw_ostream &O) {
1020 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
Jim Grosbach9255b8d2010-09-29 22:32:50 +00001021
Chris Lattner05af2612009-09-13 20:08:00 +00001022 EmitPrintInstruction(O);
1023 EmitGetRegisterName(O);
Chris Lattner0d7b0aa2010-02-11 22:57:32 +00001024 EmitGetInstructionName(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +00001025 EmitPrintAliasInstruction(O);
Chris Lattner05af2612009-09-13 20:08:00 +00001026}
1027