blob: f3dfb4eb15c8fe34c8912bf710969b233f1ef41f [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 Lattner67db8832010-12-13 00:23:57 +0000243 CodeGenTarget Target(Records);
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 Grosbachca96a862010-09-30 01:29:54 +0000246 bool isMC = AsmWriter->getValueAsBit("isMCAsmWriter");
247 const char *MachineInstrClassName = isMC ? "MCInst" : "MachineInstr";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000248
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000249 O <<
250 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000251 "/// from the instruction set description.\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000252 "void " << Target.getName() << ClassName
Jim Grosbachca96a862010-09-30 01:29:54 +0000253 << "::printInstruction(const " << MachineInstrClassName
254 << " *MI, raw_ostream &O) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000255
Chris Lattner5765dba2005-01-22 17:40:38 +0000256 std::vector<AsmWriterInst> Instructions;
257
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000258 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
259 E = Target.inst_end(); I != E; ++I)
Chris Lattner6a91b182010-03-19 01:00:55 +0000260 if (!(*I)->AsmString.empty() &&
261 (*I)->TheDef->getName() != "PHI")
Sean Callanand0bc7f02010-02-09 23:06:35 +0000262 Instructions.push_back(
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000263 AsmWriterInst(**I,
Sean Callanand0bc7f02010-02-09 23:06:35 +0000264 AsmWriter->getValueAsInt("Variant"),
265 AsmWriter->getValueAsInt("FirstOperandColumn"),
266 AsmWriter->getValueAsInt("OperandSpacing")));
Chris Lattner076efa72004-08-01 07:43:02 +0000267
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000268 // Get the instruction numbering.
Chris Lattnerf6502782010-03-19 00:34:35 +0000269 NumberedInstructions = Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000270
Chris Lattner6af022f2006-07-14 22:59:11 +0000271 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
272 // all machine instructions are necessarily being printed, so there may be
273 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000274 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
275 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000276
Chris Lattner6af022f2006-07-14 22:59:11 +0000277 // Build an aggregate string, and build a table of offsets into it.
Chris Lattner3200fc92009-09-14 01:16:36 +0000278 StringToOffsetTable StringTable;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000279
Chris Lattner259bda42006-09-27 16:44:09 +0000280 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000281 /// chunk of the output as well as indices used for operand printing.
282 std::vector<unsigned> OpcodeInfo;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000283
Chris Lattner55616402006-07-18 17:32:27 +0000284 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000285 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
286 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
287 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000288 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000289 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000290 Idx = ~0U;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000291 } else if (AWI->Operands[0].OperandType !=
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000292 AsmWriterOperand::isLiteralTextOperand ||
293 AWI->Operands[0].Str.empty()) {
294 // Something handled by the asmwriter printer, but with no leading string.
Chris Lattner3200fc92009-09-14 01:16:36 +0000295 Idx = StringTable.GetOrAddStringOffset("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000296 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000297 std::string Str = AWI->Operands[0].Str;
298 UnescapeString(Str);
299 Idx = StringTable.GetOrAddStringOffset(Str);
300 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000301
Chris Lattner6af022f2006-07-14 22:59:11 +0000302 // Nuke the string from the operand list. It is now handled!
303 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000304 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000305
Chris Lattner3200fc92009-09-14 01:16:36 +0000306 // Bias offset by one since we want 0 as a sentinel.
307 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000308 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000309
Chris Lattner55616402006-07-18 17:32:27 +0000310 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000311 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000312
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000313 // To reduce code size, we compactify common instructions into a few bits
314 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000315 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000316
317 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000318
Chris Lattnerb8462862006-07-18 17:56:07 +0000319 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000320 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000321 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000322 std::vector<unsigned> NumInstOpsHandled;
323 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
324 NumInstOpsHandled);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000325
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000326 // If we ran out of operands to print, we're done.
327 if (UniqueOperandCommands.empty()) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000328
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000329 // Compute the number of bits we need to represent these cases, this is
330 // ceil(log2(numentries)).
331 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000332
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000333 // If we don't have enough bits for this operand, don't include it.
334 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000335 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
336 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000337 break;
338 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000339
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000340 // Otherwise, we can include this in the initial lookup table. Add it in.
341 BitsLeft -= NumBits;
342 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000343 if (InstIdxs[i] != ~0U)
344 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000345
Chris Lattnerb8462862006-07-18 17:56:07 +0000346 // Remove the info about this operand.
347 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
348 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000349 if (!Inst->Operands.empty()) {
350 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000351 assert(NumOps <= Inst->Operands.size() &&
352 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000353 Inst->Operands.erase(Inst->Operands.begin(),
354 Inst->Operands.begin()+NumOps);
355 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000356 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000357
Chris Lattnerb8462862006-07-18 17:56:07 +0000358 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000359 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
360 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000361
362
363
Chris Lattner55616402006-07-18 17:32:27 +0000364 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000365 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000366 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000367 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000368 }
369 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000370 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000371 O << " };\n\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000372
Chris Lattner6af022f2006-07-14 22:59:11 +0000373 // Emit the string itself.
Chris Lattner3200fc92009-09-14 01:16:36 +0000374 O << " const char *AsmStrs = \n";
375 StringTable.EmitString(O);
376 O << ";\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000377
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000378 O << " O << \"\\t\";\n\n";
379
Chris Lattner6af022f2006-07-14 22:59:11 +0000380 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000381 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000382 << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000383 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000384
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000385 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000386 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000387 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
388 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
389
390 // Compute the number of bits we need to represent these cases, this is
391 // ceil(log2(numentries)).
392 unsigned NumBits = Log2_32_Ceil(Commands.size());
393 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000394
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000395 // Emit code to extract this field from Bits.
396 BitsLeft -= NumBits;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000397
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000398 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000399 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000400
Chris Lattner96c1ade2006-07-18 18:28:27 +0000401 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000402 // Emit two possibilitys with if/else.
403 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
404 << ((1 << NumBits)-1) << ") {\n"
405 << Commands[1]
406 << " } else {\n"
407 << Commands[0]
408 << " }\n\n";
Eric Christopher16870502010-09-18 18:50:27 +0000409 } else if (Commands.size() == 1) {
410 // Emit a single possibility.
411 O << Commands[0] << "\n\n";
Chris Lattnere7a589d2006-07-18 17:43:54 +0000412 } else {
413 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
414 << ((1 << NumBits)-1) << ") {\n"
415 << " default: // unreachable.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000416
Chris Lattnere7a589d2006-07-18 17:43:54 +0000417 // Print out all the cases.
418 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
419 O << " case " << i << ":\n";
420 O << Commands[i];
421 O << " break;\n";
422 }
423 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000424 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000425 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000426
Chris Lattnerb8462862006-07-18 17:56:07 +0000427 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000428 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
429 // Entire instruction has been emitted?
430 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000431 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000432 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000433 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000434 }
435 }
436
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000437
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000438 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000439 // elements in the vector.
440 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000441
442
Chris Lattner70067602009-09-18 18:10:19 +0000443 // Now that we've emitted all of the operand info that fit into 32 bits, emit
444 // information for those instructions that are left. This is a less dense
445 // encoding, but we expect the main 32-bit table to handle the majority of
446 // instructions.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000447 if (!Instructions.empty()) {
448 // Find the opcode # of inline asm.
449 O << " switch (MI->getOpcode()) {\n";
450 while (!Instructions.empty())
451 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000452
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000453 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000454 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000455 }
David Greenec8d06052009-07-29 20:10:24 +0000456
Chris Lattner0a012122006-07-18 19:06:01 +0000457 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000458}
Chris Lattner05af2612009-09-13 20:08:00 +0000459
460
461void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000462 CodeGenTarget Target(Records);
Chris Lattner05af2612009-09-13 20:08:00 +0000463 Record *AsmWriter = Target.getAsmWriter();
464 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000465 const std::vector<CodeGenRegister*> &Registers =
466 Target.getRegBank().getRegisters();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000467
Chris Lattnerf6761be2009-09-14 01:26:18 +0000468 StringToOffsetTable StringTable;
Chris Lattner05af2612009-09-13 20:08:00 +0000469 O <<
470 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
471 "/// from the register set description. This returns the assembler name\n"
472 "/// for the specified register.\n"
473 "const char *" << Target.getName() << ClassName
Chris Lattnerd95148f2009-09-13 20:19:22 +0000474 << "::getRegisterName(unsigned RegNo) {\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000475 << " assert(RegNo && RegNo < " << (Registers.size()+1)
476 << " && \"Invalid register number!\");\n"
477 << "\n"
Chris Lattnerf96271a2009-09-14 01:27:50 +0000478 << " static const unsigned RegAsmOffset[] = {";
Chris Lattner05af2612009-09-13 20:08:00 +0000479 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000480 const CodeGenRegister &Reg = *Registers[i];
Chris Lattner05af2612009-09-13 20:08:00 +0000481
482 std::string AsmName = Reg.TheDef->getValueAsString("AsmName");
483 if (AsmName.empty())
484 AsmName = Reg.getName();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000485
486
Chris Lattnerf96271a2009-09-14 01:27:50 +0000487 if ((i % 14) == 0)
Chris Lattnerf6761be2009-09-14 01:26:18 +0000488 O << "\n ";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000489
Chris Lattnerf6761be2009-09-14 01:26:18 +0000490 O << StringTable.GetOrAddStringOffset(AsmName) << ", ";
Chris Lattner05af2612009-09-13 20:08:00 +0000491 }
Chris Lattnerf6761be2009-09-14 01:26:18 +0000492 O << "0\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000493 << " };\n"
Chris Lattnerf6761be2009-09-14 01:26:18 +0000494 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000495
Chris Lattnerf6761be2009-09-14 01:26:18 +0000496 O << " const char *AsmStrs =\n";
497 StringTable.EmitString(O);
498 O << ";\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000499
Chris Lattnerf6761be2009-09-14 01:26:18 +0000500 O << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000501 << "}\n";
502}
503
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000504void AsmWriterEmitter::EmitGetInstructionName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000505 CodeGenTarget Target(Records);
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000506 Record *AsmWriter = Target.getAsmWriter();
507 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
508
Chris Lattnerf6502782010-03-19 00:34:35 +0000509 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
510 Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000511
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000512 StringToOffsetTable StringTable;
513 O <<
514"\n\n#ifdef GET_INSTRUCTION_NAME\n"
515"#undef GET_INSTRUCTION_NAME\n\n"
516"/// getInstructionName: This method is automatically generated by tblgen\n"
517"/// from the instruction set description. This returns the enum name of the\n"
518"/// specified instruction.\n"
519 "const char *" << Target.getName() << ClassName
520 << "::getInstructionName(unsigned Opcode) {\n"
521 << " assert(Opcode < " << NumberedInstructions.size()
522 << " && \"Invalid instruction number!\");\n"
523 << "\n"
524 << " static const unsigned InstAsmOffset[] = {";
525 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
526 const CodeGenInstruction &Inst = *NumberedInstructions[i];
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000527
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000528 std::string AsmName = Inst.TheDef->getName();
529 if ((i % 14) == 0)
530 O << "\n ";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000531
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000532 O << StringTable.GetOrAddStringOffset(AsmName) << ", ";
533 }
534 O << "0\n"
535 << " };\n"
536 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000537
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000538 O << " const char *Strs =\n";
539 StringTable.EmitString(O);
540 O << ";\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000541
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000542 O << " return Strs+InstAsmOffset[Opcode];\n"
543 << "}\n\n#endif\n";
544}
545
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000546namespace {
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000547
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000548/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
549/// feature which participates in instruction matching.
550struct SubtargetFeatureInfo {
551 /// \brief The predicate record for this feature.
552 const Record *TheDef;
553
554 /// \brief An unique index assigned to represent this feature.
555 unsigned Index;
556
557 SubtargetFeatureInfo(const Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
558
559 /// \brief The name of the enumerated constant identifying this feature.
560 std::string getEnumName() const {
561 return "Feature_" + TheDef->getName();
562 }
563};
564
565struct AsmWriterInfo {
566 /// Map of Predicate records to their subtarget information.
567 std::map<const Record*, SubtargetFeatureInfo*> SubtargetFeatures;
568
569 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
570 /// given operand.
571 SubtargetFeatureInfo *getSubtargetFeature(const Record *Def) const {
572 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
573 std::map<const Record*, SubtargetFeatureInfo*>::const_iterator I =
574 SubtargetFeatures.find(Def);
575 return I == SubtargetFeatures.end() ? 0 : I->second;
576 }
577
578 void addReqFeatures(const std::vector<Record*> &Features) {
579 for (std::vector<Record*>::const_iterator
580 I = Features.begin(), E = Features.end(); I != E; ++I) {
581 const Record *Pred = *I;
582
583 // Ignore predicates that are not intended for the assembler.
584 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
585 continue;
586
587 if (Pred->getName().empty())
588 throw TGError(Pred->getLoc(), "Predicate has no name!");
589
590 // Don't add the predicate again.
591 if (getSubtargetFeature(Pred))
592 continue;
593
594 unsigned FeatureNo = SubtargetFeatures.size();
595 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
596 assert(FeatureNo < 32 && "Too many subtarget features!");
597 }
598 }
599
600 const SubtargetFeatureInfo *getFeatureInfo(const Record *R) {
601 return SubtargetFeatures[R];
602 }
603};
604
Bill Wendling4962e612011-03-21 08:40:31 +0000605// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
606// they both have the same conditionals. In which case, we cannot print out the
607// alias for that pattern.
608class IAPrinter {
609 AsmWriterInfo &AWI;
610 std::vector<std::string> Conds;
611 std::map<StringRef, unsigned> OpMap;
612 std::string Result;
613 std::string AsmString;
614 std::vector<Record*> ReqFeatures;
615public:
616 IAPrinter(AsmWriterInfo &Info, std::string R, std::string AS)
617 : AWI(Info), Result(R), AsmString(AS) {}
618
619 void addCond(const std::string &C) { Conds.push_back(C); }
620 void addReqFeatures(const std::vector<Record*> &Features) {
621 AWI.addReqFeatures(Features);
622 ReqFeatures = Features;
623 }
624
625 void addOperand(StringRef Op, unsigned Idx) { OpMap[Op] = Idx; }
626 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
627 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
628
Bill Wendlingdd099e12011-04-08 04:08:57 +0000629 bool print(raw_ostream &O) {
Bill Wendling44dcfd32011-04-07 21:20:06 +0000630 if (Conds.empty() && ReqFeatures.empty()) {
631 O.indent(6) << "return true;\n";
Bill Wendlingdd099e12011-04-08 04:08:57 +0000632 return false;
Bill Wendling44dcfd32011-04-07 21:20:06 +0000633 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000634
Bill Wendling44dcfd32011-04-07 21:20:06 +0000635 O << "if (";
Bill Wendling4962e612011-03-21 08:40:31 +0000636
637 for (std::vector<std::string>::iterator
638 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
639 if (I != Conds.begin()) {
640 O << " &&\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000641 O.indent(8);
Bill Wendling4962e612011-03-21 08:40:31 +0000642 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000643
Bill Wendling4962e612011-03-21 08:40:31 +0000644 O << *I;
645 }
646
Bill Wendling4962e612011-03-21 08:40:31 +0000647 if (!ReqFeatures.empty()) {
Bill Wendling44dcfd32011-04-07 21:20:06 +0000648 if (Conds.begin() != Conds.end()) {
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000649 O << " &&\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000650 O.indent(8);
651 } else {
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000652 O << "if (";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000653 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000654
Bill Wendling4962e612011-03-21 08:40:31 +0000655 std::string Req;
656 raw_string_ostream ReqO(Req);
657
658 for (std::vector<Record*>::iterator
659 I = ReqFeatures.begin(), E = ReqFeatures.end(); I != E; ++I) {
660 if (I != ReqFeatures.begin()) ReqO << " | ";
661 ReqO << AWI.getFeatureInfo(*I)->getEnumName();
662 }
663
Bill Wendling4962e612011-03-21 08:40:31 +0000664 O << "(AvailableFeatures & (" << ReqO.str() << ")) == ("
665 << ReqO.str() << ')';
666 }
667
Bill Wendling44dcfd32011-04-07 21:20:06 +0000668 O << ") {\n";
669 O.indent(6) << "// " << Result << "\n";
670 O.indent(6) << "AsmString = \"" << AsmString << "\";\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000671
672 for (std::map<StringRef, unsigned>::iterator
673 I = OpMap.begin(), E = OpMap.end(); I != E; ++I)
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000674 O.indent(6) << "OpMap.push_back(std::make_pair(\"" << I->first << "\", "
675 << I->second << "));\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000676
Bill Wendling44dcfd32011-04-07 21:20:06 +0000677 O.indent(6) << "break;\n";
678 O.indent(4) << '}';
Bill Wendlingdd099e12011-04-08 04:08:57 +0000679 return !ReqFeatures.empty();
Bill Wendling4962e612011-03-21 08:40:31 +0000680 }
681
682 bool operator==(const IAPrinter &RHS) {
683 if (Conds.size() != RHS.Conds.size())
684 return false;
685
686 unsigned Idx = 0;
687 for (std::vector<std::string>::iterator
688 I = Conds.begin(), E = Conds.end(); I != E; ++I)
689 if (*I != RHS.Conds[Idx++])
690 return false;
691
692 return true;
693 }
694
695 bool operator()(const IAPrinter &RHS) {
696 if (Conds.size() < RHS.Conds.size())
697 return true;
698
699 unsigned Idx = 0;
700 for (std::vector<std::string>::iterator
701 I = Conds.begin(), E = Conds.end(); I != E; ++I)
702 if (*I != RHS.Conds[Idx++])
703 return *I < RHS.Conds[Idx++];
704
705 return false;
706 }
707};
708
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000709} // end anonymous namespace
710
711/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
712/// definitions.
713static void EmitSubtargetFeatureFlagEnumeration(AsmWriterInfo &Info,
714 raw_ostream &O) {
715 O << "namespace {\n\n";
716 O << "// Flags for subtarget features that participate in "
717 << "alias instruction matching.\n";
718 O << "enum SubtargetFeatureFlag {\n";
719
720 for (std::map<const Record*, SubtargetFeatureInfo*>::const_iterator
721 I = Info.SubtargetFeatures.begin(),
722 E = Info.SubtargetFeatures.end(); I != E; ++I) {
723 SubtargetFeatureInfo &SFI = *I->second;
724 O << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
725 }
726
727 O << " Feature_None = 0\n";
728 O << "};\n\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000729 O << "} // end anonymous namespace\n\n";
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000730}
731
732/// EmitComputeAvailableFeatures - Emit the function to compute the list of
733/// available features given a subtarget.
734static void EmitComputeAvailableFeatures(AsmWriterInfo &Info,
735 Record *AsmWriter,
736 CodeGenTarget &Target,
737 raw_ostream &O) {
738 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
739
740 O << "unsigned " << Target.getName() << ClassName << "::\n"
741 << "ComputeAvailableFeatures(const " << Target.getName()
742 << "Subtarget *Subtarget) const {\n";
743 O << " unsigned Features = 0;\n";
744
745 for (std::map<const Record*, SubtargetFeatureInfo*>::const_iterator
746 I = Info.SubtargetFeatures.begin(),
747 E = Info.SubtargetFeatures.end(); I != E; ++I) {
748 SubtargetFeatureInfo &SFI = *I->second;
749 O << " if (" << SFI.TheDef->getValueAsString("CondString")
750 << ")\n";
751 O << " Features |= " << SFI.getEnumName() << ";\n";
752 }
753
754 O << " return Features;\n";
755 O << "}\n\n";
756}
757
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000758static void EmitGetMapOperandNumber(raw_ostream &O) {
759 O << "static unsigned getMapOperandNumber("
760 << "const SmallVectorImpl<std::pair<StringRef, unsigned> > &OpMap,\n";
761 O << " StringRef Name) {\n";
762 O << " for (SmallVectorImpl<std::pair<StringRef, unsigned> >::"
763 << "const_iterator\n";
764 O << " I = OpMap.begin(), E = OpMap.end(); I != E; ++I)\n";
765 O << " if (I->first == Name)\n";
766 O << " return I->second;\n";
767 O << " assert(false && \"Operand not in map!\");\n";
768 O << " return 0;\n";
769 O << "}\n\n";
770}
771
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000772void AsmWriterEmitter::EmitRegIsInRegClass(raw_ostream &O) {
773 CodeGenTarget Target(Records);
Bill Wendling7520e3a2011-02-26 03:09:12 +0000774
775 // Enumerate the register classes.
776 const std::vector<CodeGenRegisterClass> &RegisterClasses =
777 Target.getRegisterClasses();
778
779 O << "namespace { // Register classes\n";
780 O << " enum RegClass {\n";
781
782 // Emit the register enum value for each RegisterClass.
783 for (unsigned I = 0, E = RegisterClasses.size(); I != E; ++I) {
784 if (I != 0) O << ",\n";
785 O << " RC_" << RegisterClasses[I].TheDef->getName();
786 }
787
788 O << "\n };\n";
789 O << "} // end anonymous namespace\n\n";
790
791 // Emit a function that returns 'true' if a regsiter is part of a particular
792 // register class. I.e., RAX is part of GR64 on X86.
793 O << "static bool regIsInRegisterClass"
794 << "(unsigned RegClass, unsigned Reg) {\n";
795
796 // Emit the switch that checks if a register belongs to a particular register
797 // class.
798 O << " switch (RegClass) {\n";
799 O << " default: break;\n";
800
801 for (unsigned I = 0, E = RegisterClasses.size(); I != E; ++I) {
802 const CodeGenRegisterClass &RC = RegisterClasses[I];
803
804 // Give the register class a legal C name if it's anonymous.
805 std::string Name = RC.TheDef->getName();
806 O << " case RC_" << Name << ":\n";
807
808 // Emit the register list now.
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000809 unsigned IE = RC.getOrder().size();
Bill Wendling7520e3a2011-02-26 03:09:12 +0000810 if (IE == 1) {
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000811 O << " if (Reg == " << getQualifiedName(RC.getOrder()[0]) << ")\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000812 O << " return true;\n";
813 } else {
814 O << " switch (Reg) {\n";
815 O << " default: break;\n";
816
817 for (unsigned II = 0; II != IE; ++II) {
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000818 Record *Reg = RC.getOrder()[II];
Bill Wendling7520e3a2011-02-26 03:09:12 +0000819 O << " case " << getQualifiedName(Reg) << ":\n";
820 }
821
822 O << " return true;\n";
823 O << " }\n";
824 }
825
826 O << " break;\n";
827 }
828
829 O << " }\n\n";
830 O << " return false;\n";
831 O << "}\n\n";
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000832}
833
Bill Wendling740e5b32011-06-14 03:17:20 +0000834static unsigned CountNumOperands(StringRef AsmString) {
835 unsigned NumOps = 0;
836 std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
837
838 while (!ASM.second.empty()) {
839 ++NumOps;
840 ASM = ASM.second.split(' ');
841 }
842
843 return NumOps;
844}
845
Bill Wendling393c4042011-06-15 04:31:19 +0000846static unsigned CountResultNumOperands(StringRef AsmString) {
847 unsigned NumOps = 0;
848 std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
849
850 if (!ASM.second.empty()) {
851 size_t I = ASM.second.find('{');
852 StringRef Str = ASM.second;
853 if (I != StringRef::npos)
854 Str = ASM.second.substr(I, ASM.second.find('|', I));
855
856 ASM = Str.split(' ');
857
858 do {
859 ++NumOps;
860 ASM = ASM.second.split(' ');
861 } while (!ASM.second.empty());
862 }
863
864 return NumOps;
865}
Bill Wendling740e5b32011-06-14 03:17:20 +0000866
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000867void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
868 CodeGenTarget Target(Records);
869 Record *AsmWriter = Target.getAsmWriter();
870
Bill Wendling740e5b32011-06-14 03:17:20 +0000871 if (!AsmWriter->getValueAsBit("isMCAsmWriter"))
872 return;
873
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000874 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
875 O << "#undef PRINT_ALIAS_INSTR\n\n";
876
877 EmitRegIsInRegClass(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +0000878
879 // Emit the method that prints the alias instruction.
880 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
881
Bill Wendling7520e3a2011-02-26 03:09:12 +0000882 std::vector<Record*> AllInstAliases =
883 Records.getAllDerivedDefinitions("InstAlias");
884
885 // Create a map from the qualified name to a list of potential matches.
886 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
887 for (std::vector<Record*>::iterator
888 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
889 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
890 const Record *R = *I;
Bill Wendlingeef965f2011-04-13 23:36:21 +0000891 if (!R->getValueAsBit("EmitAlias"))
892 continue; // We were told not to emit the alias, but to emit the aliasee.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000893 const DagInit *DI = R->getValueAsDag("ResultInst");
894 const DefInit *Op = dynamic_cast<const DefInit*>(DI->getOperator());
895 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
896 }
897
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000898 // A map of which conditions need to be met for each instruction operand
899 // before it can be matched to the mnemonic.
900 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
901 AsmWriterInfo AWI;
902
903 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
904 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
905 std::vector<CodeGenInstAlias*> &Aliases = I->second;
906
907 for (std::vector<CodeGenInstAlias*>::iterator
908 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
909 const CodeGenInstAlias *CGA = *II;
Bill Wendling740e5b32011-06-14 03:17:20 +0000910 unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
Bill Wendling393c4042011-06-15 04:31:19 +0000911 unsigned NumResultOps =
912 CountResultNumOperands(CGA->ResultInst->AsmString);
Bill Wendling740e5b32011-06-14 03:17:20 +0000913
914 // Don't emit the alias if it has more operands than what it's aliasing.
Bill Wendling393c4042011-06-15 04:31:19 +0000915 if (NumResultOps < CountNumOperands(CGA->AsmString))
Bill Wendling740e5b32011-06-14 03:17:20 +0000916 continue;
917
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000918 IAPrinter *IAP = new IAPrinter(AWI, CGA->Result->getAsString(),
919 CGA->AsmString);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000920 IAP->addReqFeatures(CGA->TheDef->getValueAsListOfDefs("Predicates"));
921
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000922 std::string Cond;
923 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
924 IAP->addCond(Cond);
925
926 std::map<StringRef, unsigned> OpMap;
927 bool CantHandle = false;
928
929 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
930 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
931
932 switch (RO.Kind) {
933 default: assert(0 && "unexpected InstAlias operand kind");
934 case CodeGenInstAlias::ResultOperand::K_Record: {
935 const Record *Rec = RO.getRecord();
936 StringRef ROName = RO.getName();
937
938 if (Rec->isSubClassOf("RegisterClass")) {
939 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
940 IAP->addCond(Cond);
941
942 if (!IAP->isOpMapped(ROName)) {
943 IAP->addOperand(ROName, i);
944 Cond = std::string("regIsInRegisterClass(RC_") +
945 CGA->ResultOperands[i].getRecord()->getName() +
946 ", MI->getOperand(" + llvm::utostr(i) + ").getReg())";
947 IAP->addCond(Cond);
948 } else {
949 Cond = std::string("MI->getOperand(") +
950 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
951 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
952 IAP->addCond(Cond);
953 }
954 } else {
955 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
Bill Wendling740e5b32011-06-14 03:17:20 +0000956 // FIXME: We may need to handle these situations.
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000957 delete IAP;
958 IAP = 0;
959 CantHandle = true;
960 break;
961 }
962
963 break;
964 }
965 case CodeGenInstAlias::ResultOperand::K_Imm:
966 Cond = std::string("MI->getOperand(") +
967 llvm::utostr(i) + ").getImm() == " +
968 llvm::utostr(CGA->ResultOperands[i].getImm());
969 IAP->addCond(Cond);
970 break;
971 case CodeGenInstAlias::ResultOperand::K_Reg:
972 Cond = std::string("MI->getOperand(") +
973 llvm::utostr(i) + ").getReg() == " + Target.getName() +
974 "::" + CGA->ResultOperands[i].getRegister()->getName();
975 IAP->addCond(Cond);
976 break;
977 }
978
979 if (!IAP) break;
980 }
981
982 if (CantHandle) continue;
983 IAPrinterMap[I->first].push_back(IAP);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000984 }
985 }
986
987 EmitSubtargetFeatureFlagEnumeration(AWI, O);
988 EmitComputeAvailableFeatures(AWI, AsmWriter, Target, O);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000989
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000990 std::string Header;
991 raw_string_ostream HeaderO(Header);
992
993 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendling740e5b32011-06-14 03:17:20 +0000994 << "::printAliasInstr(const MCInst"
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000995 << " *MI, raw_ostream &OS) {\n";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000996
Bill Wendling44dcfd32011-04-07 21:20:06 +0000997 std::string Cases;
998 raw_string_ostream CasesO(Cases);
Bill Wendlingdd099e12011-04-08 04:08:57 +0000999 bool NeedAvailableFeatures = false;
Bill Wendling44dcfd32011-04-07 21:20:06 +00001000
1001 for (std::map<std::string, std::vector<IAPrinter*> >::iterator
1002 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
1003 std::vector<IAPrinter*> &IAPs = I->second;
1004 std::vector<IAPrinter*> UniqueIAPs;
1005
1006 for (std::vector<IAPrinter*>::iterator
1007 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
1008 IAPrinter *LHS = *II;
1009 bool IsDup = false;
1010 for (std::vector<IAPrinter*>::iterator
1011 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
1012 IAPrinter *RHS = *III;
1013 if (LHS != RHS && *LHS == *RHS) {
1014 IsDup = true;
1015 break;
1016 }
1017 }
1018
1019 if (!IsDup) UniqueIAPs.push_back(LHS);
1020 }
1021
1022 if (UniqueIAPs.empty()) continue;
1023
1024 CasesO.indent(2) << "case " << I->first << ":\n";
1025
1026 for (std::vector<IAPrinter*>::iterator
1027 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
1028 IAPrinter *IAP = *II;
1029 CasesO.indent(4);
Bill Wendlingdd099e12011-04-08 04:08:57 +00001030 NeedAvailableFeatures |= IAP->print(CasesO);
Bill Wendling44dcfd32011-04-07 21:20:06 +00001031 CasesO << '\n';
1032 }
1033
Eric Christopher721ef662011-04-18 21:28:11 +00001034 CasesO.indent(4) << "return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +00001035 }
1036
Bill Wendling740e5b32011-06-14 03:17:20 +00001037 if (CasesO.str().empty()) {
Bill Wendlingf415d8b2011-05-23 00:18:33 +00001038 O << HeaderO.str();
Eric Christopher721ef662011-04-18 21:28:11 +00001039 O << " return false;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001040 O << "}\n\n";
1041 O << "#endif // PRINT_ALIAS_INSTR\n";
1042 return;
1043 }
1044
Bill Wendlingf415d8b2011-05-23 00:18:33 +00001045 EmitGetMapOperandNumber(O);
1046
1047 O << HeaderO.str();
Bill Wendling44dcfd32011-04-07 21:20:06 +00001048 O.indent(2) << "StringRef AsmString;\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +00001049 O.indent(2) << "SmallVector<std::pair<StringRef, unsigned>, 4> OpMap;\n";
Bill Wendlingdd099e12011-04-08 04:08:57 +00001050 if (NeedAvailableFeatures)
1051 O.indent(2) << "unsigned AvailableFeatures = getAvailableFeatures();\n\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +00001052 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher721ef662011-04-18 21:28:11 +00001053 O.indent(2) << "default: return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +00001054 O << CasesO.str();
1055 O.indent(2) << "}\n\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001056
1057 // Code that prints the alias, replacing the operands with the ones from the
1058 // MCInst.
Bill Wendling7520e3a2011-02-26 03:09:12 +00001059 O << " std::pair<StringRef, StringRef> ASM = AsmString.split(' ');\n";
1060 O << " OS << '\\t' << ASM.first;\n";
1061
1062 O << " if (!ASM.second.empty()) {\n";
1063 O << " OS << '\\t';\n";
1064 O << " for (StringRef::iterator\n";
1065 O << " I = ASM.second.begin(), E = ASM.second.end(); I != E; ) {\n";
1066 O << " if (*I == '$') {\n";
1067 O << " StringRef::iterator Start = ++I;\n";
1068 O << " while (I != E &&\n";
1069 O << " ((*I >= 'a' && *I <= 'z') ||\n";
1070 O << " (*I >= 'A' && *I <= 'Z') ||\n";
1071 O << " (*I >= '0' && *I <= '9') ||\n";
1072 O << " *I == '_'))\n";
1073 O << " ++I;\n";
1074 O << " StringRef Name(Start, I - Start);\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +00001075 O << " printOperand(MI, getMapOperandNumber(OpMap, Name), OS);\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001076 O << " } else {\n";
1077 O << " OS << *I++;\n";
1078 O << " }\n";
1079 O << " }\n";
1080 O << " }\n\n";
1081
Eric Christopher721ef662011-04-18 21:28:11 +00001082 O << " return true;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001083 O << "}\n\n";
1084
1085 O << "#endif // PRINT_ALIAS_INSTR\n";
1086}
Chris Lattner05af2612009-09-13 20:08:00 +00001087
1088void AsmWriterEmitter::run(raw_ostream &O) {
1089 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
Jim Grosbach9255b8d2010-09-29 22:32:50 +00001090
Chris Lattner05af2612009-09-13 20:08:00 +00001091 EmitPrintInstruction(O);
1092 EmitGetRegisterName(O);
Chris Lattner0d7b0aa2010-02-11 22:57:32 +00001093 EmitGetInstructionName(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +00001094 EmitPrintAliasInstruction(O);
Chris Lattner05af2612009-09-13 20:08:00 +00001095}
1096