blob: 6ae0b39c8ddea911e6319c9d1b5b6d3eda324e7e [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 Lattner175580c2004-08-14 22:50:53 +000018#include "Record.h"
Chris Lattner44da5fb2009-09-14 01:19:16 +000019#include "StringToOffsetTable.h"
Chris Lattnerbdff5f92006-07-18 17:18:03 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/MathExtras.h"
Jeff Cohen615ed992005-01-22 18:50:10 +000022#include <algorithm>
Chris Lattner2e1f51b2004-08-01 05:59:33 +000023using namespace llvm;
24
Chris Lattner38c07512005-01-22 20:31:17 +000025static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +000026 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +000027 O << " case " << OpsToPrint.back().first << ": ";
28 AsmWriterOperand TheOp = OpsToPrint.back().second;
29 OpsToPrint.pop_back();
30
31 // Check to see if any other operands are identical in this list, and if so,
32 // emit a case label for them.
33 for (unsigned i = OpsToPrint.size(); i != 0; --i)
34 if (OpsToPrint[i-1].second == TheOp) {
35 O << "\n case " << OpsToPrint[i-1].first << ": ";
36 OpsToPrint.erase(OpsToPrint.begin()+i-1);
37 }
38
39 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000040 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +000041 O << "break;\n";
42}
43
Chris Lattner870c0162005-01-22 18:38:13 +000044
45/// EmitInstructions - Emit the last instruction in the vector and any other
46/// instructions that are suitably similar to it.
47static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +000048 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +000049 AsmWriterInst FirstInst = Insts.back();
50 Insts.pop_back();
51
52 std::vector<AsmWriterInst> SimilarInsts;
53 unsigned DifferingOperand = ~0;
54 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +000055 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
56 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +000057 if (DifferingOperand == ~0U) // First match!
58 DifferingOperand = DiffOp;
59
60 // If this differs in the same operand as the rest of the instructions in
61 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +000062 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +000063 SimilarInsts.push_back(Insts[i-1]);
64 Insts.erase(Insts.begin()+i-1);
65 }
66 }
67 }
68
Chris Lattnera1e8a802006-05-01 17:01:17 +000069 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +000070 << FirstInst.CGI->TheDef->getName() << ":\n";
71 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +000072 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +000073 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
74 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
75 if (i != DifferingOperand) {
76 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000077 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +000078 } else {
79 // If this is the operand that varies between all of the instructions,
80 // emit a switch for just this operand now.
81 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +000082 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +000083 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +000084 FirstInst.CGI->TheDef->getName(),
85 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +000086
Chris Lattner870c0162005-01-22 18:38:13 +000087 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +000088 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +000089 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +000090 AWI.CGI->TheDef->getName(),
91 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +000092 }
Chris Lattner38c07512005-01-22 20:31:17 +000093 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
94 while (!OpsToPrint.empty())
95 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +000096 O << " }";
97 }
98 O << "\n";
99 }
Chris Lattner870c0162005-01-22 18:38:13 +0000100 O << " break;\n";
101}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000102
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000103void AsmWriterEmitter::
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000104FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000105 std::vector<unsigned> &InstIdxs,
106 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000107 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000108
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000109 // This vector parallels UniqueOperandCommands, keeping track of which
110 // instructions each case are used for. It is a comma separated string of
111 // enums.
112 std::vector<std::string> InstrsForCase;
113 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000114 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000115
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000116 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
117 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Bill Wendlingb9449d62010-07-16 23:10:00 +0000118 if (Inst == 0) continue; // PHI, INLINEASM, PROLOG_LABEL, etc.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000119
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000120 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000121 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000122 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000123
Chris Lattnerb8462862006-07-18 17:56:07 +0000124 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000125
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000126 // Check to see if we already have 'Command' in UniqueOperandCommands.
127 // If not, add it.
128 bool FoundIt = false;
129 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
130 if (UniqueOperandCommands[idx] == Command) {
131 InstIdxs[i] = idx;
132 InstrsForCase[idx] += ", ";
133 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
134 FoundIt = true;
135 break;
136 }
137 if (!FoundIt) {
138 InstIdxs[i] = UniqueOperandCommands.size();
139 UniqueOperandCommands.push_back(Command);
140 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000141
142 // This command matches one operand so far.
143 InstOpsUsed.push_back(1);
144 }
145 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000146
Chris Lattner96c1ade2006-07-18 18:28:27 +0000147 // For each entry of UniqueOperandCommands, there is a set of instructions
148 // that uses it. If the next command of all instructions in the set are
149 // identical, fold it into the command.
150 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
151 CommandIdx != e; ++CommandIdx) {
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000152
Chris Lattner96c1ade2006-07-18 18:28:27 +0000153 for (unsigned Op = 1; ; ++Op) {
154 // Scan for the first instruction in the set.
155 std::vector<unsigned>::iterator NIT =
156 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
157 if (NIT == InstIdxs.end()) break; // No commonality.
158
159 // If this instruction has no more operands, we isn't anything to merge
160 // into this command.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000161 const AsmWriterInst *FirstInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000162 getAsmWriterInstByID(NIT-InstIdxs.begin());
163 if (!FirstInst || FirstInst->Operands.size() == Op)
164 break;
165
166 // Otherwise, scan to see if all of the other instructions in this command
167 // set share the operand.
168 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000169 // Keep track of the maximum, number of operands or any
170 // instruction we see in the group.
171 size_t MaxSize = FirstInst->Operands.size();
172
Chris Lattner96c1ade2006-07-18 18:28:27 +0000173 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
174 NIT != InstIdxs.end();
175 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
176 // Okay, found another instruction in this command set. If the operand
177 // matches, we're ok, otherwise bail out.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000178 const AsmWriterInst *OtherInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000179 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000180
181 if (OtherInst &&
182 OtherInst->Operands.size() > FirstInst->Operands.size())
183 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
184
Chris Lattner96c1ade2006-07-18 18:28:27 +0000185 if (!OtherInst || OtherInst->Operands.size() == Op ||
186 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
187 AllSame = false;
188 break;
189 }
190 }
191 if (!AllSame) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000192
Chris Lattner96c1ade2006-07-18 18:28:27 +0000193 // Okay, everything in this command set has the same next operand. Add it
194 // to UniqueOperandCommands and remember that it was consumed.
195 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000196
Chris Lattner96c1ade2006-07-18 18:28:27 +0000197 UniqueOperandCommands[CommandIdx] += Command;
198 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000199 }
200 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000201
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000202 // Prepend some of the instructions each case is used for onto the case val.
203 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
204 std::string Instrs = InstrsForCase[i];
205 if (Instrs.size() > 70) {
206 Instrs.erase(Instrs.begin()+70, Instrs.end());
207 Instrs += "...";
208 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000209
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000210 if (!Instrs.empty())
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000211 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000212 UniqueOperandCommands[i];
213 }
214}
215
216
Daniel Dunbar9bd34602009-10-17 20:43:42 +0000217static void UnescapeString(std::string &Str) {
218 for (unsigned i = 0; i != Str.size(); ++i) {
219 if (Str[i] == '\\' && i != Str.size()-1) {
220 switch (Str[i+1]) {
221 default: continue; // Don't execute the code after the switch.
222 case 'a': Str[i] = '\a'; break;
223 case 'b': Str[i] = '\b'; break;
224 case 'e': Str[i] = 27; break;
225 case 'f': Str[i] = '\f'; break;
226 case 'n': Str[i] = '\n'; break;
227 case 'r': Str[i] = '\r'; break;
228 case 't': Str[i] = '\t'; break;
229 case 'v': Str[i] = '\v'; break;
230 case '"': Str[i] = '\"'; break;
231 case '\'': Str[i] = '\''; break;
232 case '\\': Str[i] = '\\'; break;
233 }
234 // Nuke the second character.
235 Str.erase(Str.begin()+i+1);
236 }
237 }
238}
239
Chris Lattner05af2612009-09-13 20:08:00 +0000240/// EmitPrintInstruction - Generate the code for the "printInstruction" method
241/// implementation.
242void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000243 CodeGenTarget Target;
Chris Lattner175580c2004-08-14 22:50:53 +0000244 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000245 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000246
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000247 O <<
248 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000249 "/// from the instruction set description.\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000250 "void " << Target.getName() << ClassName
Chris Lattner35c33bd2010-04-04 04:47:45 +0000251 << "::printInstruction(const MachineInstr *MI, raw_ostream &O) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000252
Chris Lattner5765dba2005-01-22 17:40:38 +0000253 std::vector<AsmWriterInst> Instructions;
254
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000255 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
256 E = Target.inst_end(); I != E; ++I)
Chris Lattner6a91b182010-03-19 01:00:55 +0000257 if (!(*I)->AsmString.empty() &&
258 (*I)->TheDef->getName() != "PHI")
Sean Callanand0bc7f02010-02-09 23:06:35 +0000259 Instructions.push_back(
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000260 AsmWriterInst(**I,
Sean Callanand0bc7f02010-02-09 23:06:35 +0000261 AsmWriter->getValueAsInt("Variant"),
262 AsmWriter->getValueAsInt("FirstOperandColumn"),
263 AsmWriter->getValueAsInt("OperandSpacing")));
Chris Lattner076efa72004-08-01 07:43:02 +0000264
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000265 // Get the instruction numbering.
Chris Lattnerf6502782010-03-19 00:34:35 +0000266 NumberedInstructions = Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000267
Chris Lattner6af022f2006-07-14 22:59:11 +0000268 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
269 // all machine instructions are necessarily being printed, so there may be
270 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000271 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
272 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000273
Chris Lattner6af022f2006-07-14 22:59:11 +0000274 // Build an aggregate string, and build a table of offsets into it.
Chris Lattner3200fc92009-09-14 01:16:36 +0000275 StringToOffsetTable StringTable;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000276
Chris Lattner259bda42006-09-27 16:44:09 +0000277 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000278 /// chunk of the output as well as indices used for operand printing.
279 std::vector<unsigned> OpcodeInfo;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000280
Chris Lattner55616402006-07-18 17:32:27 +0000281 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000282 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
283 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
284 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000285 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000286 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000287 Idx = ~0U;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000288 } else if (AWI->Operands[0].OperandType !=
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000289 AsmWriterOperand::isLiteralTextOperand ||
290 AWI->Operands[0].Str.empty()) {
291 // Something handled by the asmwriter printer, but with no leading string.
Chris Lattner3200fc92009-09-14 01:16:36 +0000292 Idx = StringTable.GetOrAddStringOffset("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000293 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000294 std::string Str = AWI->Operands[0].Str;
295 UnescapeString(Str);
296 Idx = StringTable.GetOrAddStringOffset(Str);
297 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000298
Chris Lattner6af022f2006-07-14 22:59:11 +0000299 // Nuke the string from the operand list. It is now handled!
300 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000301 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000302
Chris Lattner3200fc92009-09-14 01:16:36 +0000303 // Bias offset by one since we want 0 as a sentinel.
304 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000305 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000306
Chris Lattner55616402006-07-18 17:32:27 +0000307 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000308 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000309
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000310 // To reduce code size, we compactify common instructions into a few bits
311 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000312 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000313
314 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000315
Chris Lattnerb8462862006-07-18 17:56:07 +0000316 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000317 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000318 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000319 std::vector<unsigned> NumInstOpsHandled;
320 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
321 NumInstOpsHandled);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000322
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000323 // If we ran out of operands to print, we're done.
324 if (UniqueOperandCommands.empty()) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000325
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000326 // Compute the number of bits we need to represent these cases, this is
327 // ceil(log2(numentries)).
328 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000329
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000330 // If we don't have enough bits for this operand, don't include it.
331 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000332 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
333 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000334 break;
335 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000336
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000337 // Otherwise, we can include this in the initial lookup table. Add it in.
338 BitsLeft -= NumBits;
339 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000340 if (InstIdxs[i] != ~0U)
341 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000342
Chris Lattnerb8462862006-07-18 17:56:07 +0000343 // Remove the info about this operand.
344 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
345 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000346 if (!Inst->Operands.empty()) {
347 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000348 assert(NumOps <= Inst->Operands.size() &&
349 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000350 Inst->Operands.erase(Inst->Operands.begin(),
351 Inst->Operands.begin()+NumOps);
352 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000353 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000354
Chris Lattnerb8462862006-07-18 17:56:07 +0000355 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000356 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
357 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000358
359
360
Chris Lattner55616402006-07-18 17:32:27 +0000361 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000362 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000363 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000364 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000365 }
366 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000367 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000368 O << " };\n\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000369
Chris Lattner6af022f2006-07-14 22:59:11 +0000370 // Emit the string itself.
Chris Lattner3200fc92009-09-14 01:16:36 +0000371 O << " const char *AsmStrs = \n";
372 StringTable.EmitString(O);
373 O << ";\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000374
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000375 O << " O << \"\\t\";\n\n";
376
Chris Lattner6af022f2006-07-14 22:59:11 +0000377 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000378 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000379 << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000380 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000381
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000382 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000383 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000384 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
385 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
386
387 // Compute the number of bits we need to represent these cases, this is
388 // ceil(log2(numentries)).
389 unsigned NumBits = Log2_32_Ceil(Commands.size());
390 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000391
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000392 // Emit code to extract this field from Bits.
393 BitsLeft -= NumBits;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000394
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000395 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000396 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000397
Chris Lattner96c1ade2006-07-18 18:28:27 +0000398 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000399 // Emit two possibilitys with if/else.
400 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
401 << ((1 << NumBits)-1) << ") {\n"
402 << Commands[1]
403 << " } else {\n"
404 << Commands[0]
405 << " }\n\n";
Eric Christopher16870502010-09-18 18:50:27 +0000406 } else if (Commands.size() == 1) {
407 // Emit a single possibility.
408 O << Commands[0] << "\n\n";
Chris Lattnere7a589d2006-07-18 17:43:54 +0000409 } else {
410 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
411 << ((1 << NumBits)-1) << ") {\n"
412 << " default: // unreachable.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000413
Chris Lattnere7a589d2006-07-18 17:43:54 +0000414 // Print out all the cases.
415 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
416 O << " case " << i << ":\n";
417 O << Commands[i];
418 O << " break;\n";
419 }
420 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000421 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000422 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000423
Chris Lattnerb8462862006-07-18 17:56:07 +0000424 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000425 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
426 // Entire instruction has been emitted?
427 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000428 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000429 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000430 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000431 }
432 }
433
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000434
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000435 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000436 // elements in the vector.
437 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000438
439
Chris Lattner70067602009-09-18 18:10:19 +0000440 // Now that we've emitted all of the operand info that fit into 32 bits, emit
441 // information for those instructions that are left. This is a less dense
442 // encoding, but we expect the main 32-bit table to handle the majority of
443 // instructions.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000444 if (!Instructions.empty()) {
445 // Find the opcode # of inline asm.
446 O << " switch (MI->getOpcode()) {\n";
447 while (!Instructions.empty())
448 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000449
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000450 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000451 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000452 }
David Greenec8d06052009-07-29 20:10:24 +0000453
Chris Lattner0a012122006-07-18 19:06:01 +0000454 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000455}
Chris Lattner05af2612009-09-13 20:08:00 +0000456
457
458void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
459 CodeGenTarget Target;
460 Record *AsmWriter = Target.getAsmWriter();
461 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
462 const std::vector<CodeGenRegister> &Registers = Target.getRegisters();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000463
Chris Lattnerf6761be2009-09-14 01:26:18 +0000464 StringToOffsetTable StringTable;
Chris Lattner05af2612009-09-13 20:08:00 +0000465 O <<
466 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
467 "/// from the register set description. This returns the assembler name\n"
468 "/// for the specified register.\n"
469 "const char *" << Target.getName() << ClassName
Chris Lattnerd95148f2009-09-13 20:19:22 +0000470 << "::getRegisterName(unsigned RegNo) {\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000471 << " assert(RegNo && RegNo < " << (Registers.size()+1)
472 << " && \"Invalid register number!\");\n"
473 << "\n"
Chris Lattnerf96271a2009-09-14 01:27:50 +0000474 << " static const unsigned RegAsmOffset[] = {";
Chris Lattner05af2612009-09-13 20:08:00 +0000475 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
476 const CodeGenRegister &Reg = Registers[i];
477
478 std::string AsmName = Reg.TheDef->getValueAsString("AsmName");
479 if (AsmName.empty())
480 AsmName = Reg.getName();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000481
482
Chris Lattnerf96271a2009-09-14 01:27:50 +0000483 if ((i % 14) == 0)
Chris Lattnerf6761be2009-09-14 01:26:18 +0000484 O << "\n ";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000485
Chris Lattnerf6761be2009-09-14 01:26:18 +0000486 O << StringTable.GetOrAddStringOffset(AsmName) << ", ";
Chris Lattner05af2612009-09-13 20:08:00 +0000487 }
Chris Lattnerf6761be2009-09-14 01:26:18 +0000488 O << "0\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000489 << " };\n"
Chris Lattnerf6761be2009-09-14 01:26:18 +0000490 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000491
Chris Lattnerf6761be2009-09-14 01:26:18 +0000492 O << " const char *AsmStrs =\n";
493 StringTable.EmitString(O);
494 O << ";\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000495
Chris Lattnerf6761be2009-09-14 01:26:18 +0000496 O << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000497 << "}\n";
498}
499
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000500void AsmWriterEmitter::EmitGetInstructionName(raw_ostream &O) {
501 CodeGenTarget Target;
502 Record *AsmWriter = Target.getAsmWriter();
503 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
504
Chris Lattnerf6502782010-03-19 00:34:35 +0000505 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
506 Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000507
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000508 StringToOffsetTable StringTable;
509 O <<
510"\n\n#ifdef GET_INSTRUCTION_NAME\n"
511"#undef GET_INSTRUCTION_NAME\n\n"
512"/// getInstructionName: This method is automatically generated by tblgen\n"
513"/// from the instruction set description. This returns the enum name of the\n"
514"/// specified instruction.\n"
515 "const char *" << Target.getName() << ClassName
516 << "::getInstructionName(unsigned Opcode) {\n"
517 << " assert(Opcode < " << NumberedInstructions.size()
518 << " && \"Invalid instruction number!\");\n"
519 << "\n"
520 << " static const unsigned InstAsmOffset[] = {";
521 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
522 const CodeGenInstruction &Inst = *NumberedInstructions[i];
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000523
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000524 std::string AsmName = Inst.TheDef->getName();
525 if ((i % 14) == 0)
526 O << "\n ";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000527
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000528 O << StringTable.GetOrAddStringOffset(AsmName) << ", ";
529 }
530 O << "0\n"
531 << " };\n"
532 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000533
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000534 O << " const char *Strs =\n";
535 StringTable.EmitString(O);
536 O << ";\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000537
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000538 O << " return Strs+InstAsmOffset[Opcode];\n"
539 << "}\n\n#endif\n";
540}
541
542
Chris Lattner05af2612009-09-13 20:08:00 +0000543
544void AsmWriterEmitter::run(raw_ostream &O) {
545 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000546
Chris Lattner05af2612009-09-13 20:08:00 +0000547 EmitPrintInstruction(O);
548 EmitGetRegisterName(O);
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000549 EmitGetInstructionName(O);
Chris Lattner05af2612009-09-13 20:08:00 +0000550}
551