blob: 066e03d8c71bf3d1778a8b9e86ac968060db734a [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"
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000017#include "Error.h"
Chris Lattner2e1f51b2004-08-01 05:59:33 +000018#include "CodeGenTarget.h"
Chris Lattner175580c2004-08-14 22:50:53 +000019#include "Record.h"
Chris Lattner44da5fb2009-09-14 01:19:16 +000020#include "StringToOffsetTable.h"
Chris Lattnerbdff5f92006-07-18 17:18:03 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/MathExtras.h"
Jeff Cohen615ed992005-01-22 18:50:10 +000023#include <algorithm>
Chris Lattner2e1f51b2004-08-01 05:59:33 +000024using namespace llvm;
25
Chris Lattner38c07512005-01-22 20:31:17 +000026static void PrintCases(std::vector<std::pair<std::string,
Daniel Dunbar1a551802009-07-03 00:10:29 +000027 AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
Chris Lattner38c07512005-01-22 20:31:17 +000028 O << " case " << OpsToPrint.back().first << ": ";
29 AsmWriterOperand TheOp = OpsToPrint.back().second;
30 OpsToPrint.pop_back();
31
32 // Check to see if any other operands are identical in this list, and if so,
33 // emit a case label for them.
34 for (unsigned i = OpsToPrint.size(); i != 0; --i)
35 if (OpsToPrint[i-1].second == TheOp) {
36 O << "\n case " << OpsToPrint[i-1].first << ": ";
37 OpsToPrint.erase(OpsToPrint.begin()+i-1);
38 }
39
40 // Finally, emit the code.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000041 O << TheOp.getCode();
Chris Lattner38c07512005-01-22 20:31:17 +000042 O << "break;\n";
43}
44
Chris Lattner870c0162005-01-22 18:38:13 +000045
46/// EmitInstructions - Emit the last instruction in the vector and any other
47/// instructions that are suitably similar to it.
48static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
Daniel Dunbar1a551802009-07-03 00:10:29 +000049 raw_ostream &O) {
Chris Lattner870c0162005-01-22 18:38:13 +000050 AsmWriterInst FirstInst = Insts.back();
51 Insts.pop_back();
52
53 std::vector<AsmWriterInst> SimilarInsts;
54 unsigned DifferingOperand = ~0;
55 for (unsigned i = Insts.size(); i != 0; --i) {
Chris Lattnerf8766682005-01-22 19:22:23 +000056 unsigned DiffOp = Insts[i-1].MatchesAllButOneOp(FirstInst);
57 if (DiffOp != ~1U) {
Chris Lattner870c0162005-01-22 18:38:13 +000058 if (DifferingOperand == ~0U) // First match!
59 DifferingOperand = DiffOp;
60
61 // If this differs in the same operand as the rest of the instructions in
62 // this class, move it to the SimilarInsts list.
Chris Lattnerf8766682005-01-22 19:22:23 +000063 if (DifferingOperand == DiffOp || DiffOp == ~0U) {
Chris Lattner870c0162005-01-22 18:38:13 +000064 SimilarInsts.push_back(Insts[i-1]);
65 Insts.erase(Insts.begin()+i-1);
66 }
67 }
68 }
69
Chris Lattnera1e8a802006-05-01 17:01:17 +000070 O << " case " << FirstInst.CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +000071 << FirstInst.CGI->TheDef->getName() << ":\n";
72 for (unsigned i = 0, e = SimilarInsts.size(); i != e; ++i)
Chris Lattnera1e8a802006-05-01 17:01:17 +000073 O << " case " << SimilarInsts[i].CGI->Namespace << "::"
Chris Lattner870c0162005-01-22 18:38:13 +000074 << SimilarInsts[i].CGI->TheDef->getName() << ":\n";
75 for (unsigned i = 0, e = FirstInst.Operands.size(); i != e; ++i) {
76 if (i != DifferingOperand) {
77 // If the operand is the same for all instructions, just print it.
Chris Lattnerbdff5f92006-07-18 17:18:03 +000078 O << " " << FirstInst.Operands[i].getCode();
Chris Lattner870c0162005-01-22 18:38:13 +000079 } else {
80 // If this is the operand that varies between all of the instructions,
81 // emit a switch for just this operand now.
82 O << " switch (MI->getOpcode()) {\n";
Chris Lattner38c07512005-01-22 20:31:17 +000083 std::vector<std::pair<std::string, AsmWriterOperand> > OpsToPrint;
Chris Lattnera1e8a802006-05-01 17:01:17 +000084 OpsToPrint.push_back(std::make_pair(FirstInst.CGI->Namespace + "::" +
Chris Lattner38c07512005-01-22 20:31:17 +000085 FirstInst.CGI->TheDef->getName(),
86 FirstInst.Operands[i]));
Misha Brukman3da94ae2005-04-22 00:00:37 +000087
Chris Lattner870c0162005-01-22 18:38:13 +000088 for (unsigned si = 0, e = SimilarInsts.size(); si != e; ++si) {
Chris Lattner38c07512005-01-22 20:31:17 +000089 AsmWriterInst &AWI = SimilarInsts[si];
Chris Lattnera1e8a802006-05-01 17:01:17 +000090 OpsToPrint.push_back(std::make_pair(AWI.CGI->Namespace+"::"+
Chris Lattner38c07512005-01-22 20:31:17 +000091 AWI.CGI->TheDef->getName(),
92 AWI.Operands[i]));
Chris Lattner870c0162005-01-22 18:38:13 +000093 }
Chris Lattner38c07512005-01-22 20:31:17 +000094 std::reverse(OpsToPrint.begin(), OpsToPrint.end());
95 while (!OpsToPrint.empty())
96 PrintCases(OpsToPrint, O);
Chris Lattner870c0162005-01-22 18:38:13 +000097 O << " }";
98 }
99 O << "\n";
100 }
Chris Lattner870c0162005-01-22 18:38:13 +0000101 O << " break;\n";
102}
Chris Lattnerb0b55e72005-01-22 17:32:42 +0000103
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000104void AsmWriterEmitter::
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000105FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
Chris Lattner96c1ade2006-07-18 18:28:27 +0000106 std::vector<unsigned> &InstIdxs,
107 std::vector<unsigned> &InstOpsUsed) const {
Chris Lattner195bb4a2006-07-18 19:27:30 +0000108 InstIdxs.assign(NumberedInstructions.size(), ~0U);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000109
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000110 // This vector parallels UniqueOperandCommands, keeping track of which
111 // instructions each case are used for. It is a comma separated string of
112 // enums.
113 std::vector<std::string> InstrsForCase;
114 InstrsForCase.resize(UniqueOperandCommands.size());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000115 InstOpsUsed.assign(UniqueOperandCommands.size(), 0);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000116
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000117 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
118 const AsmWriterInst *Inst = getAsmWriterInstByID(i);
Bill Wendlingb9449d62010-07-16 23:10:00 +0000119 if (Inst == 0) continue; // PHI, INLINEASM, PROLOG_LABEL, etc.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000120
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000121 std::string Command;
Chris Lattnerb8462862006-07-18 17:56:07 +0000122 if (Inst->Operands.empty())
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000123 continue; // Instruction already done.
Chris Lattner191dd1f2006-07-18 17:50:22 +0000124
Chris Lattnerb8462862006-07-18 17:56:07 +0000125 Command = " " + Inst->Operands[0].getCode() + "\n";
Chris Lattner191dd1f2006-07-18 17:50:22 +0000126
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000127 // Check to see if we already have 'Command' in UniqueOperandCommands.
128 // If not, add it.
129 bool FoundIt = false;
130 for (unsigned idx = 0, e = UniqueOperandCommands.size(); idx != e; ++idx)
131 if (UniqueOperandCommands[idx] == Command) {
132 InstIdxs[i] = idx;
133 InstrsForCase[idx] += ", ";
134 InstrsForCase[idx] += Inst->CGI->TheDef->getName();
135 FoundIt = true;
136 break;
137 }
138 if (!FoundIt) {
139 InstIdxs[i] = UniqueOperandCommands.size();
140 UniqueOperandCommands.push_back(Command);
141 InstrsForCase.push_back(Inst->CGI->TheDef->getName());
Chris Lattner96c1ade2006-07-18 18:28:27 +0000142
143 // This command matches one operand so far.
144 InstOpsUsed.push_back(1);
145 }
146 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000147
Chris Lattner96c1ade2006-07-18 18:28:27 +0000148 // For each entry of UniqueOperandCommands, there is a set of instructions
149 // that uses it. If the next command of all instructions in the set are
150 // identical, fold it into the command.
151 for (unsigned CommandIdx = 0, e = UniqueOperandCommands.size();
152 CommandIdx != e; ++CommandIdx) {
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000153
Chris Lattner96c1ade2006-07-18 18:28:27 +0000154 for (unsigned Op = 1; ; ++Op) {
155 // Scan for the first instruction in the set.
156 std::vector<unsigned>::iterator NIT =
157 std::find(InstIdxs.begin(), InstIdxs.end(), CommandIdx);
158 if (NIT == InstIdxs.end()) break; // No commonality.
159
160 // If this instruction has no more operands, we isn't anything to merge
161 // into this command.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000162 const AsmWriterInst *FirstInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000163 getAsmWriterInstByID(NIT-InstIdxs.begin());
164 if (!FirstInst || FirstInst->Operands.size() == Op)
165 break;
166
167 // Otherwise, scan to see if all of the other instructions in this command
168 // set share the operand.
169 bool AllSame = true;
David Greenec8d06052009-07-29 20:10:24 +0000170 // Keep track of the maximum, number of operands or any
171 // instruction we see in the group.
172 size_t MaxSize = FirstInst->Operands.size();
173
Chris Lattner96c1ade2006-07-18 18:28:27 +0000174 for (NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx);
175 NIT != InstIdxs.end();
176 NIT = std::find(NIT+1, InstIdxs.end(), CommandIdx)) {
177 // Okay, found another instruction in this command set. If the operand
178 // matches, we're ok, otherwise bail out.
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000179 const AsmWriterInst *OtherInst =
Chris Lattner96c1ade2006-07-18 18:28:27 +0000180 getAsmWriterInstByID(NIT-InstIdxs.begin());
David Greenec8d06052009-07-29 20:10:24 +0000181
182 if (OtherInst &&
183 OtherInst->Operands.size() > FirstInst->Operands.size())
184 MaxSize = std::max(MaxSize, OtherInst->Operands.size());
185
Chris Lattner96c1ade2006-07-18 18:28:27 +0000186 if (!OtherInst || OtherInst->Operands.size() == Op ||
187 OtherInst->Operands[Op] != FirstInst->Operands[Op]) {
188 AllSame = false;
189 break;
190 }
191 }
192 if (!AllSame) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000193
Chris Lattner96c1ade2006-07-18 18:28:27 +0000194 // Okay, everything in this command set has the same next operand. Add it
195 // to UniqueOperandCommands and remember that it was consumed.
196 std::string Command = " " + FirstInst->Operands[Op].getCode() + "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000197
Chris Lattner96c1ade2006-07-18 18:28:27 +0000198 UniqueOperandCommands[CommandIdx] += Command;
199 InstOpsUsed[CommandIdx]++;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000200 }
201 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000202
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000203 // Prepend some of the instructions each case is used for onto the case val.
204 for (unsigned i = 0, e = InstrsForCase.size(); i != e; ++i) {
205 std::string Instrs = InstrsForCase[i];
206 if (Instrs.size() > 70) {
207 Instrs.erase(Instrs.begin()+70, Instrs.end());
208 Instrs += "...";
209 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000210
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000211 if (!Instrs.empty())
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000212 UniqueOperandCommands[i] = " // " + Instrs + "\n" +
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000213 UniqueOperandCommands[i];
214 }
215}
216
217
Daniel Dunbar9bd34602009-10-17 20:43:42 +0000218static void UnescapeString(std::string &Str) {
219 for (unsigned i = 0; i != Str.size(); ++i) {
220 if (Str[i] == '\\' && i != Str.size()-1) {
221 switch (Str[i+1]) {
222 default: continue; // Don't execute the code after the switch.
223 case 'a': Str[i] = '\a'; break;
224 case 'b': Str[i] = '\b'; break;
225 case 'e': Str[i] = 27; break;
226 case 'f': Str[i] = '\f'; break;
227 case 'n': Str[i] = '\n'; break;
228 case 'r': Str[i] = '\r'; break;
229 case 't': Str[i] = '\t'; break;
230 case 'v': Str[i] = '\v'; break;
231 case '"': Str[i] = '\"'; break;
232 case '\'': Str[i] = '\''; break;
233 case '\\': Str[i] = '\\'; break;
234 }
235 // Nuke the second character.
236 Str.erase(Str.begin()+i+1);
237 }
238 }
239}
240
Chris Lattner05af2612009-09-13 20:08:00 +0000241/// EmitPrintInstruction - Generate the code for the "printInstruction" method
242/// implementation.
243void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000244 CodeGenTarget Target(Records);
Chris Lattner175580c2004-08-14 22:50:53 +0000245 Record *AsmWriter = Target.getAsmWriter();
Chris Lattner953c6fe2004-10-03 20:19:02 +0000246 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jim Grosbachca96a862010-09-30 01:29:54 +0000247 bool isMC = AsmWriter->getValueAsBit("isMCAsmWriter");
248 const char *MachineInstrClassName = isMC ? "MCInst" : "MachineInstr";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000249
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000250 O <<
251 "/// printInstruction - This method is automatically generated by tablegen\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000252 "/// from the instruction set description.\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000253 "void " << Target.getName() << ClassName
Jim Grosbachca96a862010-09-30 01:29:54 +0000254 << "::printInstruction(const " << MachineInstrClassName
255 << " *MI, raw_ostream &O) {\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000256
Chris Lattner5765dba2005-01-22 17:40:38 +0000257 std::vector<AsmWriterInst> Instructions;
258
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000259 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
260 E = Target.inst_end(); I != E; ++I)
Chris Lattner6a91b182010-03-19 01:00:55 +0000261 if (!(*I)->AsmString.empty() &&
262 (*I)->TheDef->getName() != "PHI")
Sean Callanand0bc7f02010-02-09 23:06:35 +0000263 Instructions.push_back(
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000264 AsmWriterInst(**I,
Sean Callanand0bc7f02010-02-09 23:06:35 +0000265 AsmWriter->getValueAsInt("Variant"),
266 AsmWriter->getValueAsInt("FirstOperandColumn"),
267 AsmWriter->getValueAsInt("OperandSpacing")));
Chris Lattner076efa72004-08-01 07:43:02 +0000268
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000269 // Get the instruction numbering.
Chris Lattnerf6502782010-03-19 00:34:35 +0000270 NumberedInstructions = Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000271
Chris Lattner6af022f2006-07-14 22:59:11 +0000272 // Compute the CodeGenInstruction -> AsmWriterInst mapping. Note that not
273 // all machine instructions are necessarily being printed, so there may be
274 // target instructions not in this map.
Chris Lattner6af022f2006-07-14 22:59:11 +0000275 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
276 CGIAWIMap.insert(std::make_pair(Instructions[i].CGI, &Instructions[i]));
Chris Lattnerf8766682005-01-22 19:22:23 +0000277
Chris Lattner6af022f2006-07-14 22:59:11 +0000278 // Build an aggregate string, and build a table of offsets into it.
Chris Lattner3200fc92009-09-14 01:16:36 +0000279 StringToOffsetTable StringTable;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000280
Chris Lattner259bda42006-09-27 16:44:09 +0000281 /// OpcodeInfo - This encodes the index of the string to use for the first
Chris Lattner55616402006-07-18 17:32:27 +0000282 /// chunk of the output as well as indices used for operand printing.
283 std::vector<unsigned> OpcodeInfo;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000284
Chris Lattner55616402006-07-18 17:32:27 +0000285 unsigned MaxStringIdx = 0;
Chris Lattner6af022f2006-07-14 22:59:11 +0000286 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
287 AsmWriterInst *AWI = CGIAWIMap[NumberedInstructions[i]];
288 unsigned Idx;
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000289 if (AWI == 0) {
Chris Lattner6af022f2006-07-14 22:59:11 +0000290 // Something not handled by the asmwriter printer.
Chris Lattner3200fc92009-09-14 01:16:36 +0000291 Idx = ~0U;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000292 } else if (AWI->Operands[0].OperandType !=
Chris Lattnera6dc9fb2006-07-19 01:39:06 +0000293 AsmWriterOperand::isLiteralTextOperand ||
294 AWI->Operands[0].Str.empty()) {
295 // Something handled by the asmwriter printer, but with no leading string.
Chris Lattner3200fc92009-09-14 01:16:36 +0000296 Idx = StringTable.GetOrAddStringOffset("");
Chris Lattner6af022f2006-07-14 22:59:11 +0000297 } else {
Chris Lattner3200fc92009-09-14 01:16:36 +0000298 std::string Str = AWI->Operands[0].Str;
299 UnescapeString(Str);
300 Idx = StringTable.GetOrAddStringOffset(Str);
301 MaxStringIdx = std::max(MaxStringIdx, Idx);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000302
Chris Lattner6af022f2006-07-14 22:59:11 +0000303 // Nuke the string from the operand list. It is now handled!
304 AWI->Operands.erase(AWI->Operands.begin());
Chris Lattnerf8766682005-01-22 19:22:23 +0000305 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000306
Chris Lattner3200fc92009-09-14 01:16:36 +0000307 // Bias offset by one since we want 0 as a sentinel.
308 OpcodeInfo.push_back(Idx+1);
Chris Lattnerf8766682005-01-22 19:22:23 +0000309 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000310
Chris Lattner55616402006-07-18 17:32:27 +0000311 // Figure out how many bits we used for the string index.
Chris Lattner3200fc92009-09-14 01:16:36 +0000312 unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000313
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000314 // To reduce code size, we compactify common instructions into a few bits
315 // in the opcode-indexed table.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000316 unsigned BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000317
318 std::vector<std::vector<std::string> > TableDrivenOperandPrinters;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000319
Chris Lattnerb8462862006-07-18 17:56:07 +0000320 while (1) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000321 std::vector<std::string> UniqueOperandCommands;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000322 std::vector<unsigned> InstIdxs;
Chris Lattner96c1ade2006-07-18 18:28:27 +0000323 std::vector<unsigned> NumInstOpsHandled;
324 FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
325 NumInstOpsHandled);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000326
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000327 // If we ran out of operands to print, we're done.
328 if (UniqueOperandCommands.empty()) break;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000329
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000330 // Compute the number of bits we need to represent these cases, this is
331 // ceil(log2(numentries)).
332 unsigned NumBits = Log2_32_Ceil(UniqueOperandCommands.size());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000333
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000334 // If we don't have enough bits for this operand, don't include it.
335 if (NumBits > BitsLeft) {
Chris Lattner569f1212009-08-23 04:44:11 +0000336 DEBUG(errs() << "Not enough bits to densely encode " << NumBits
337 << " more bits\n");
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000338 break;
339 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000340
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000341 // Otherwise, we can include this in the initial lookup table. Add it in.
342 BitsLeft -= NumBits;
343 for (unsigned i = 0, e = InstIdxs.size(); i != e; ++i)
Chris Lattner195bb4a2006-07-18 19:27:30 +0000344 if (InstIdxs[i] != ~0U)
345 OpcodeInfo[i] |= InstIdxs[i] << (BitsLeft+AsmStrBits);
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000346
Chris Lattnerb8462862006-07-18 17:56:07 +0000347 // Remove the info about this operand.
348 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
349 if (AsmWriterInst *Inst = getAsmWriterInstByID(i))
Chris Lattner96c1ade2006-07-18 18:28:27 +0000350 if (!Inst->Operands.empty()) {
351 unsigned NumOps = NumInstOpsHandled[InstIdxs[i]];
Chris Lattner0a012122006-07-18 19:06:01 +0000352 assert(NumOps <= Inst->Operands.size() &&
353 "Can't remove this many ops!");
Chris Lattner96c1ade2006-07-18 18:28:27 +0000354 Inst->Operands.erase(Inst->Operands.begin(),
355 Inst->Operands.begin()+NumOps);
356 }
Chris Lattnerb8462862006-07-18 17:56:07 +0000357 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000358
Chris Lattnerb8462862006-07-18 17:56:07 +0000359 // Remember the handlers for this set of operands.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000360 TableDrivenOperandPrinters.push_back(UniqueOperandCommands);
361 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000362
363
364
Chris Lattner55616402006-07-18 17:32:27 +0000365 O<<" static const unsigned OpInfo[] = {\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000366 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000367 O << " " << OpcodeInfo[i] << "U,\t// "
Chris Lattner55616402006-07-18 17:32:27 +0000368 << NumberedInstructions[i]->TheDef->getName() << "\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000369 }
370 // Add a dummy entry so the array init doesn't end with a comma.
Chris Lattner55616402006-07-18 17:32:27 +0000371 O << " 0U\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000372 O << " };\n\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000373
Chris Lattner6af022f2006-07-14 22:59:11 +0000374 // Emit the string itself.
Chris Lattner3200fc92009-09-14 01:16:36 +0000375 O << " const char *AsmStrs = \n";
376 StringTable.EmitString(O);
377 O << ";\n\n";
Chris Lattner6af022f2006-07-14 22:59:11 +0000378
Evan Cheng4eecdeb2008-02-02 08:39:46 +0000379 O << " O << \"\\t\";\n\n";
380
Chris Lattner6af022f2006-07-14 22:59:11 +0000381 O << " // Emit the opcode for the instruction.\n"
Chris Lattner55616402006-07-18 17:32:27 +0000382 << " unsigned Bits = OpInfo[MI->getOpcode()];\n"
Chris Lattner41aefdc2009-08-08 01:32:19 +0000383 << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
Chris Lattner3200fc92009-09-14 01:16:36 +0000384 << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
David Greenea5bb59f2009-08-05 21:00:52 +0000385
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000386 // Output the table driven operand information.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000387 BitsLeft = 32-AsmStrBits;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000388 for (unsigned i = 0, e = TableDrivenOperandPrinters.size(); i != e; ++i) {
389 std::vector<std::string> &Commands = TableDrivenOperandPrinters[i];
390
391 // Compute the number of bits we need to represent these cases, this is
392 // ceil(log2(numentries)).
393 unsigned NumBits = Log2_32_Ceil(Commands.size());
394 assert(NumBits <= BitsLeft && "consistency error");
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000395
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000396 // Emit code to extract this field from Bits.
397 BitsLeft -= NumBits;
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000398
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000399 O << "\n // Fragment " << i << " encoded into " << NumBits
Chris Lattnere7a589d2006-07-18 17:43:54 +0000400 << " bits for " << Commands.size() << " unique commands.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000401
Chris Lattner96c1ade2006-07-18 18:28:27 +0000402 if (Commands.size() == 2) {
Chris Lattnere7a589d2006-07-18 17:43:54 +0000403 // Emit two possibilitys with if/else.
404 O << " if ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
405 << ((1 << NumBits)-1) << ") {\n"
406 << Commands[1]
407 << " } else {\n"
408 << Commands[0]
409 << " }\n\n";
Eric Christopher16870502010-09-18 18:50:27 +0000410 } else if (Commands.size() == 1) {
411 // Emit a single possibility.
412 O << Commands[0] << "\n\n";
Chris Lattnere7a589d2006-07-18 17:43:54 +0000413 } else {
414 O << " switch ((Bits >> " << (BitsLeft+AsmStrBits) << ") & "
415 << ((1 << NumBits)-1) << ") {\n"
416 << " default: // unreachable.\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000417
Chris Lattnere7a589d2006-07-18 17:43:54 +0000418 // Print out all the cases.
419 for (unsigned i = 0, e = Commands.size(); i != e; ++i) {
420 O << " case " << i << ":\n";
421 O << Commands[i];
422 O << " break;\n";
423 }
424 O << " }\n\n";
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000425 }
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000426 }
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000427
Chris Lattnerb8462862006-07-18 17:56:07 +0000428 // Okay, delete instructions with no operand info left.
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000429 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
430 // Entire instruction has been emitted?
431 AsmWriterInst &Inst = Instructions[i];
Chris Lattnerb8462862006-07-18 17:56:07 +0000432 if (Inst.Operands.empty()) {
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000433 Instructions.erase(Instructions.begin()+i);
Chris Lattnerb8462862006-07-18 17:56:07 +0000434 --i; --e;
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000435 }
436 }
437
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000438
Chris Lattnerbdff5f92006-07-18 17:18:03 +0000439 // Because this is a vector, we want to emit from the end. Reverse all of the
Chris Lattner870c0162005-01-22 18:38:13 +0000440 // elements in the vector.
441 std::reverse(Instructions.begin(), Instructions.end());
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000442
443
Chris Lattner70067602009-09-18 18:10:19 +0000444 // Now that we've emitted all of the operand info that fit into 32 bits, emit
445 // information for those instructions that are left. This is a less dense
446 // encoding, but we expect the main 32-bit table to handle the majority of
447 // instructions.
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000448 if (!Instructions.empty()) {
449 // Find the opcode # of inline asm.
450 O << " switch (MI->getOpcode()) {\n";
451 while (!Instructions.empty())
452 EmitInstructions(Instructions, O);
Chris Lattner870c0162005-01-22 18:38:13 +0000453
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000454 O << " }\n";
Chris Lattner41aefdc2009-08-08 01:32:19 +0000455 O << " return;\n";
Chris Lattnerb51ecd42006-07-18 17:38:46 +0000456 }
David Greenec8d06052009-07-29 20:10:24 +0000457
Chris Lattner0a012122006-07-18 19:06:01 +0000458 O << "}\n";
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000459}
Chris Lattner05af2612009-09-13 20:08:00 +0000460
461
462void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000463 CodeGenTarget Target(Records);
Chris Lattner05af2612009-09-13 20:08:00 +0000464 Record *AsmWriter = Target.getAsmWriter();
465 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000466 const std::vector<CodeGenRegister*> &Registers =
467 Target.getRegBank().getRegisters();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000468
Chris Lattnerf6761be2009-09-14 01:26:18 +0000469 StringToOffsetTable StringTable;
Chris Lattner05af2612009-09-13 20:08:00 +0000470 O <<
471 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
472 "/// from the register set description. This returns the assembler name\n"
473 "/// for the specified register.\n"
474 "const char *" << Target.getName() << ClassName
Chris Lattnerd95148f2009-09-13 20:19:22 +0000475 << "::getRegisterName(unsigned RegNo) {\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000476 << " assert(RegNo && RegNo < " << (Registers.size()+1)
477 << " && \"Invalid register number!\");\n"
478 << "\n"
Chris Lattnerf96271a2009-09-14 01:27:50 +0000479 << " static const unsigned RegAsmOffset[] = {";
Chris Lattner05af2612009-09-13 20:08:00 +0000480 for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000481 const CodeGenRegister &Reg = *Registers[i];
Chris Lattner05af2612009-09-13 20:08:00 +0000482
483 std::string AsmName = Reg.TheDef->getValueAsString("AsmName");
484 if (AsmName.empty())
485 AsmName = Reg.getName();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000486
487
Chris Lattnerf96271a2009-09-14 01:27:50 +0000488 if ((i % 14) == 0)
Chris Lattnerf6761be2009-09-14 01:26:18 +0000489 O << "\n ";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000490
Chris Lattnerf6761be2009-09-14 01:26:18 +0000491 O << StringTable.GetOrAddStringOffset(AsmName) << ", ";
Chris Lattner05af2612009-09-13 20:08:00 +0000492 }
Chris Lattnerf6761be2009-09-14 01:26:18 +0000493 O << "0\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000494 << " };\n"
Chris Lattnerf6761be2009-09-14 01:26:18 +0000495 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000496
Chris Lattnerf6761be2009-09-14 01:26:18 +0000497 O << " const char *AsmStrs =\n";
498 StringTable.EmitString(O);
499 O << ";\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000500
Chris Lattnerf6761be2009-09-14 01:26:18 +0000501 O << " return AsmStrs+RegAsmOffset[RegNo-1];\n"
Chris Lattner05af2612009-09-13 20:08:00 +0000502 << "}\n";
503}
504
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000505void AsmWriterEmitter::EmitGetInstructionName(raw_ostream &O) {
Chris Lattner67db8832010-12-13 00:23:57 +0000506 CodeGenTarget Target(Records);
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000507 Record *AsmWriter = Target.getAsmWriter();
508 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
509
Chris Lattnerf6502782010-03-19 00:34:35 +0000510 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
511 Target.getInstructionsByEnumValue();
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000512
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000513 StringToOffsetTable StringTable;
514 O <<
515"\n\n#ifdef GET_INSTRUCTION_NAME\n"
516"#undef GET_INSTRUCTION_NAME\n\n"
517"/// getInstructionName: This method is automatically generated by tblgen\n"
518"/// from the instruction set description. This returns the enum name of the\n"
519"/// specified instruction.\n"
520 "const char *" << Target.getName() << ClassName
521 << "::getInstructionName(unsigned Opcode) {\n"
522 << " assert(Opcode < " << NumberedInstructions.size()
523 << " && \"Invalid instruction number!\");\n"
524 << "\n"
525 << " static const unsigned InstAsmOffset[] = {";
526 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
527 const CodeGenInstruction &Inst = *NumberedInstructions[i];
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000528
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000529 std::string AsmName = Inst.TheDef->getName();
530 if ((i % 14) == 0)
531 O << "\n ";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000532
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000533 O << StringTable.GetOrAddStringOffset(AsmName) << ", ";
534 }
535 O << "0\n"
536 << " };\n"
537 << "\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000538
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000539 O << " const char *Strs =\n";
540 StringTable.EmitString(O);
541 O << ";\n";
Jim Grosbach9255b8d2010-09-29 22:32:50 +0000542
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000543 O << " return Strs+InstAsmOffset[Opcode];\n"
544 << "}\n\n#endif\n";
545}
546
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000547namespace {
Chris Lattner0d7b0aa2010-02-11 22:57:32 +0000548
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000549/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
550/// feature which participates in instruction matching.
551struct SubtargetFeatureInfo {
552 /// \brief The predicate record for this feature.
553 const Record *TheDef;
554
555 /// \brief An unique index assigned to represent this feature.
556 unsigned Index;
557
558 SubtargetFeatureInfo(const Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
559
560 /// \brief The name of the enumerated constant identifying this feature.
561 std::string getEnumName() const {
562 return "Feature_" + TheDef->getName();
563 }
564};
565
566struct AsmWriterInfo {
567 /// Map of Predicate records to their subtarget information.
568 std::map<const Record*, SubtargetFeatureInfo*> SubtargetFeatures;
569
570 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
571 /// given operand.
572 SubtargetFeatureInfo *getSubtargetFeature(const Record *Def) const {
573 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
574 std::map<const Record*, SubtargetFeatureInfo*>::const_iterator I =
575 SubtargetFeatures.find(Def);
576 return I == SubtargetFeatures.end() ? 0 : I->second;
577 }
578
579 void addReqFeatures(const std::vector<Record*> &Features) {
580 for (std::vector<Record*>::const_iterator
581 I = Features.begin(), E = Features.end(); I != E; ++I) {
582 const Record *Pred = *I;
583
584 // Ignore predicates that are not intended for the assembler.
585 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
586 continue;
587
588 if (Pred->getName().empty())
589 throw TGError(Pred->getLoc(), "Predicate has no name!");
590
591 // Don't add the predicate again.
592 if (getSubtargetFeature(Pred))
593 continue;
594
595 unsigned FeatureNo = SubtargetFeatures.size();
596 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
597 assert(FeatureNo < 32 && "Too many subtarget features!");
598 }
599 }
600
601 const SubtargetFeatureInfo *getFeatureInfo(const Record *R) {
602 return SubtargetFeatures[R];
603 }
604};
605
Bill Wendling4962e612011-03-21 08:40:31 +0000606// IAPrinter - Holds information about an InstAlias. Two InstAliases match if
607// they both have the same conditionals. In which case, we cannot print out the
608// alias for that pattern.
609class IAPrinter {
610 AsmWriterInfo &AWI;
611 std::vector<std::string> Conds;
612 std::map<StringRef, unsigned> OpMap;
613 std::string Result;
614 std::string AsmString;
615 std::vector<Record*> ReqFeatures;
616public:
617 IAPrinter(AsmWriterInfo &Info, std::string R, std::string AS)
618 : AWI(Info), Result(R), AsmString(AS) {}
619
620 void addCond(const std::string &C) { Conds.push_back(C); }
621 void addReqFeatures(const std::vector<Record*> &Features) {
622 AWI.addReqFeatures(Features);
623 ReqFeatures = Features;
624 }
625
626 void addOperand(StringRef Op, unsigned Idx) { OpMap[Op] = Idx; }
627 unsigned getOpIndex(StringRef Op) { return OpMap[Op]; }
628 bool isOpMapped(StringRef Op) { return OpMap.find(Op) != OpMap.end(); }
629
Bill Wendlingdd099e12011-04-08 04:08:57 +0000630 bool print(raw_ostream &O) {
Bill Wendling44dcfd32011-04-07 21:20:06 +0000631 if (Conds.empty() && ReqFeatures.empty()) {
632 O.indent(6) << "return true;\n";
Bill Wendlingdd099e12011-04-08 04:08:57 +0000633 return false;
Bill Wendling44dcfd32011-04-07 21:20:06 +0000634 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000635
Bill Wendling44dcfd32011-04-07 21:20:06 +0000636 O << "if (";
Bill Wendling4962e612011-03-21 08:40:31 +0000637
638 for (std::vector<std::string>::iterator
639 I = Conds.begin(), E = Conds.end(); I != E; ++I) {
640 if (I != Conds.begin()) {
641 O << " &&\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000642 O.indent(8);
Bill Wendling4962e612011-03-21 08:40:31 +0000643 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000644
Bill Wendling4962e612011-03-21 08:40:31 +0000645 O << *I;
646 }
647
Bill Wendling4962e612011-03-21 08:40:31 +0000648 if (!ReqFeatures.empty()) {
Bill Wendling44dcfd32011-04-07 21:20:06 +0000649 if (Conds.begin() != Conds.end()) {
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000650 O << " &&\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000651 O.indent(8);
652 } else {
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000653 O << "if (";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000654 }
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000655
Bill Wendling4962e612011-03-21 08:40:31 +0000656 std::string Req;
657 raw_string_ostream ReqO(Req);
658
659 for (std::vector<Record*>::iterator
660 I = ReqFeatures.begin(), E = ReqFeatures.end(); I != E; ++I) {
661 if (I != ReqFeatures.begin()) ReqO << " | ";
662 ReqO << AWI.getFeatureInfo(*I)->getEnumName();
663 }
664
Bill Wendling4962e612011-03-21 08:40:31 +0000665 O << "(AvailableFeatures & (" << ReqO.str() << ")) == ("
666 << ReqO.str() << ')';
667 }
668
Bill Wendling44dcfd32011-04-07 21:20:06 +0000669 O << ") {\n";
670 O.indent(6) << "// " << Result << "\n";
671 O.indent(6) << "AsmString = \"" << AsmString << "\";\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000672
673 for (std::map<StringRef, unsigned>::iterator
674 I = OpMap.begin(), E = OpMap.end(); I != E; ++I)
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000675 O.indent(6) << "OpMap.push_back(std::make_pair(\"" << I->first << "\", "
676 << I->second << "));\n";
Bill Wendling4962e612011-03-21 08:40:31 +0000677
Bill Wendling44dcfd32011-04-07 21:20:06 +0000678 O.indent(6) << "break;\n";
679 O.indent(4) << '}';
Bill Wendlingdd099e12011-04-08 04:08:57 +0000680 return !ReqFeatures.empty();
Bill Wendling4962e612011-03-21 08:40:31 +0000681 }
682
683 bool operator==(const IAPrinter &RHS) {
684 if (Conds.size() != RHS.Conds.size())
685 return false;
686
687 unsigned Idx = 0;
688 for (std::vector<std::string>::iterator
689 I = Conds.begin(), E = Conds.end(); I != E; ++I)
690 if (*I != RHS.Conds[Idx++])
691 return false;
692
693 return true;
694 }
695
696 bool operator()(const IAPrinter &RHS) {
697 if (Conds.size() < RHS.Conds.size())
698 return true;
699
700 unsigned Idx = 0;
701 for (std::vector<std::string>::iterator
702 I = Conds.begin(), E = Conds.end(); I != E; ++I)
703 if (*I != RHS.Conds[Idx++])
704 return *I < RHS.Conds[Idx++];
705
706 return false;
707 }
708};
709
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000710} // end anonymous namespace
711
712/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
713/// definitions.
714static void EmitSubtargetFeatureFlagEnumeration(AsmWriterInfo &Info,
715 raw_ostream &O) {
716 O << "namespace {\n\n";
717 O << "// Flags for subtarget features that participate in "
718 << "alias instruction matching.\n";
719 O << "enum SubtargetFeatureFlag {\n";
720
721 for (std::map<const Record*, SubtargetFeatureInfo*>::const_iterator
722 I = Info.SubtargetFeatures.begin(),
723 E = Info.SubtargetFeatures.end(); I != E; ++I) {
724 SubtargetFeatureInfo &SFI = *I->second;
725 O << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
726 }
727
728 O << " Feature_None = 0\n";
729 O << "};\n\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +0000730 O << "} // end anonymous namespace\n\n";
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000731}
732
733/// EmitComputeAvailableFeatures - Emit the function to compute the list of
734/// available features given a subtarget.
735static void EmitComputeAvailableFeatures(AsmWriterInfo &Info,
736 Record *AsmWriter,
737 CodeGenTarget &Target,
738 raw_ostream &O) {
739 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
740
741 O << "unsigned " << Target.getName() << ClassName << "::\n"
742 << "ComputeAvailableFeatures(const " << Target.getName()
743 << "Subtarget *Subtarget) const {\n";
744 O << " unsigned Features = 0;\n";
745
746 for (std::map<const Record*, SubtargetFeatureInfo*>::const_iterator
747 I = Info.SubtargetFeatures.begin(),
748 E = Info.SubtargetFeatures.end(); I != E; ++I) {
749 SubtargetFeatureInfo &SFI = *I->second;
750 O << " if (" << SFI.TheDef->getValueAsString("CondString")
751 << ")\n";
752 O << " Features |= " << SFI.getEnumName() << ";\n";
753 }
754
755 O << " return Features;\n";
756 O << "}\n\n";
757}
758
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000759static void EmitGetMapOperandNumber(raw_ostream &O) {
760 O << "static unsigned getMapOperandNumber("
761 << "const SmallVectorImpl<std::pair<StringRef, unsigned> > &OpMap,\n";
762 O << " StringRef Name) {\n";
763 O << " for (SmallVectorImpl<std::pair<StringRef, unsigned> >::"
764 << "const_iterator\n";
765 O << " I = OpMap.begin(), E = OpMap.end(); I != E; ++I)\n";
766 O << " if (I->first == Name)\n";
767 O << " return I->second;\n";
768 O << " assert(false && \"Operand not in map!\");\n";
769 O << " return 0;\n";
770 O << "}\n\n";
771}
772
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000773void AsmWriterEmitter::EmitRegIsInRegClass(raw_ostream &O) {
774 CodeGenTarget Target(Records);
Bill Wendling7520e3a2011-02-26 03:09:12 +0000775
776 // Enumerate the register classes.
777 const std::vector<CodeGenRegisterClass> &RegisterClasses =
778 Target.getRegisterClasses();
779
780 O << "namespace { // Register classes\n";
781 O << " enum RegClass {\n";
782
783 // Emit the register enum value for each RegisterClass.
784 for (unsigned I = 0, E = RegisterClasses.size(); I != E; ++I) {
785 if (I != 0) O << ",\n";
786 O << " RC_" << RegisterClasses[I].TheDef->getName();
787 }
788
789 O << "\n };\n";
790 O << "} // end anonymous namespace\n\n";
791
792 // Emit a function that returns 'true' if a regsiter is part of a particular
793 // register class. I.e., RAX is part of GR64 on X86.
794 O << "static bool regIsInRegisterClass"
795 << "(unsigned RegClass, unsigned Reg) {\n";
796
797 // Emit the switch that checks if a register belongs to a particular register
798 // class.
799 O << " switch (RegClass) {\n";
800 O << " default: break;\n";
801
802 for (unsigned I = 0, E = RegisterClasses.size(); I != E; ++I) {
803 const CodeGenRegisterClass &RC = RegisterClasses[I];
804
805 // Give the register class a legal C name if it's anonymous.
806 std::string Name = RC.TheDef->getName();
807 O << " case RC_" << Name << ":\n";
808
809 // Emit the register list now.
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000810 unsigned IE = RC.getOrder().size();
Bill Wendling7520e3a2011-02-26 03:09:12 +0000811 if (IE == 1) {
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000812 O << " if (Reg == " << getQualifiedName(RC.getOrder()[0]) << ")\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +0000813 O << " return true;\n";
814 } else {
815 O << " switch (Reg) {\n";
816 O << " default: break;\n";
817
818 for (unsigned II = 0; II != IE; ++II) {
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000819 Record *Reg = RC.getOrder()[II];
Bill Wendling7520e3a2011-02-26 03:09:12 +0000820 O << " case " << getQualifiedName(Reg) << ":\n";
821 }
822
823 O << " return true;\n";
824 O << " }\n";
825 }
826
827 O << " break;\n";
828 }
829
830 O << " }\n\n";
831 O << " return false;\n";
832 O << "}\n\n";
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000833}
834
Bill Wendling740e5b32011-06-14 03:17:20 +0000835static unsigned CountNumOperands(StringRef AsmString) {
836 unsigned NumOps = 0;
837 std::pair<StringRef, StringRef> ASM = AsmString.split(' ');
838
839 while (!ASM.second.empty()) {
840 ++NumOps;
841 ASM = ASM.second.split(' ');
842 }
843
844 return NumOps;
845}
846
Bill Wendling393c4042011-06-15 04:31:19 +0000847static unsigned CountResultNumOperands(StringRef AsmString) {
848 unsigned NumOps = 0;
849 std::pair<StringRef, StringRef> ASM = AsmString.split('\t');
850
851 if (!ASM.second.empty()) {
852 size_t I = ASM.second.find('{');
853 StringRef Str = ASM.second;
854 if (I != StringRef::npos)
855 Str = ASM.second.substr(I, ASM.second.find('|', I));
856
857 ASM = Str.split(' ');
858
859 do {
860 ++NumOps;
861 ASM = ASM.second.split(' ');
862 } while (!ASM.second.empty());
863 }
864
865 return NumOps;
866}
Bill Wendling740e5b32011-06-14 03:17:20 +0000867
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000868void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
869 CodeGenTarget Target(Records);
870 Record *AsmWriter = Target.getAsmWriter();
871
Bill Wendling740e5b32011-06-14 03:17:20 +0000872 if (!AsmWriter->getValueAsBit("isMCAsmWriter"))
873 return;
874
Bill Wendling2cf6fc62011-03-21 08:31:53 +0000875 O << "\n#ifdef PRINT_ALIAS_INSTR\n";
876 O << "#undef PRINT_ALIAS_INSTR\n\n";
877
878 EmitRegIsInRegClass(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +0000879
880 // Emit the method that prints the alias instruction.
881 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
882
Bill Wendling7520e3a2011-02-26 03:09:12 +0000883 std::vector<Record*> AllInstAliases =
884 Records.getAllDerivedDefinitions("InstAlias");
885
886 // Create a map from the qualified name to a list of potential matches.
887 std::map<std::string, std::vector<CodeGenInstAlias*> > AliasMap;
888 for (std::vector<Record*>::iterator
889 I = AllInstAliases.begin(), E = AllInstAliases.end(); I != E; ++I) {
890 CodeGenInstAlias *Alias = new CodeGenInstAlias(*I, Target);
891 const Record *R = *I;
Bill Wendlingeef965f2011-04-13 23:36:21 +0000892 if (!R->getValueAsBit("EmitAlias"))
893 continue; // We were told not to emit the alias, but to emit the aliasee.
Bill Wendling7520e3a2011-02-26 03:09:12 +0000894 const DagInit *DI = R->getValueAsDag("ResultInst");
895 const DefInit *Op = dynamic_cast<const DefInit*>(DI->getOperator());
896 AliasMap[getQualifiedName(Op->getDef())].push_back(Alias);
897 }
898
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000899 // A map of which conditions need to be met for each instruction operand
900 // before it can be matched to the mnemonic.
901 std::map<std::string, std::vector<IAPrinter*> > IAPrinterMap;
902 AsmWriterInfo AWI;
903
904 for (std::map<std::string, std::vector<CodeGenInstAlias*> >::iterator
905 I = AliasMap.begin(), E = AliasMap.end(); I != E; ++I) {
906 std::vector<CodeGenInstAlias*> &Aliases = I->second;
907
908 for (std::vector<CodeGenInstAlias*>::iterator
909 II = Aliases.begin(), IE = Aliases.end(); II != IE; ++II) {
910 const CodeGenInstAlias *CGA = *II;
Bill Wendling740e5b32011-06-14 03:17:20 +0000911 unsigned LastOpNo = CGA->ResultInstOperandIndex.size();
Bill Wendling393c4042011-06-15 04:31:19 +0000912 unsigned NumResultOps =
913 CountResultNumOperands(CGA->ResultInst->AsmString);
Bill Wendling740e5b32011-06-14 03:17:20 +0000914
915 // Don't emit the alias if it has more operands than what it's aliasing.
Bill Wendling393c4042011-06-15 04:31:19 +0000916 if (NumResultOps < CountNumOperands(CGA->AsmString))
Bill Wendling740e5b32011-06-14 03:17:20 +0000917 continue;
918
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000919 IAPrinter *IAP = new IAPrinter(AWI, CGA->Result->getAsString(),
920 CGA->AsmString);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000921 IAP->addReqFeatures(CGA->TheDef->getValueAsListOfDefs("Predicates"));
922
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000923 std::string Cond;
924 Cond = std::string("MI->getNumOperands() == ") + llvm::utostr(LastOpNo);
925 IAP->addCond(Cond);
926
927 std::map<StringRef, unsigned> OpMap;
928 bool CantHandle = false;
929
930 for (unsigned i = 0, e = LastOpNo; i != e; ++i) {
931 const CodeGenInstAlias::ResultOperand &RO = CGA->ResultOperands[i];
932
933 switch (RO.Kind) {
934 default: assert(0 && "unexpected InstAlias operand kind");
935 case CodeGenInstAlias::ResultOperand::K_Record: {
936 const Record *Rec = RO.getRecord();
937 StringRef ROName = RO.getName();
938
939 if (Rec->isSubClassOf("RegisterClass")) {
940 Cond = std::string("MI->getOperand(")+llvm::utostr(i)+").isReg()";
941 IAP->addCond(Cond);
942
943 if (!IAP->isOpMapped(ROName)) {
944 IAP->addOperand(ROName, i);
945 Cond = std::string("regIsInRegisterClass(RC_") +
946 CGA->ResultOperands[i].getRecord()->getName() +
947 ", MI->getOperand(" + llvm::utostr(i) + ").getReg())";
948 IAP->addCond(Cond);
949 } else {
950 Cond = std::string("MI->getOperand(") +
951 llvm::utostr(i) + ").getReg() == MI->getOperand(" +
952 llvm::utostr(IAP->getOpIndex(ROName)) + ").getReg()";
953 IAP->addCond(Cond);
954 }
955 } else {
956 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
Bill Wendling740e5b32011-06-14 03:17:20 +0000957 // FIXME: We may need to handle these situations.
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000958 delete IAP;
959 IAP = 0;
960 CantHandle = true;
961 break;
962 }
963
964 break;
965 }
966 case CodeGenInstAlias::ResultOperand::K_Imm:
967 Cond = std::string("MI->getOperand(") +
968 llvm::utostr(i) + ").getImm() == " +
969 llvm::utostr(CGA->ResultOperands[i].getImm());
970 IAP->addCond(Cond);
971 break;
972 case CodeGenInstAlias::ResultOperand::K_Reg:
973 Cond = std::string("MI->getOperand(") +
974 llvm::utostr(i) + ").getReg() == " + Target.getName() +
975 "::" + CGA->ResultOperands[i].getRegister()->getName();
976 IAP->addCond(Cond);
977 break;
978 }
979
980 if (!IAP) break;
981 }
982
983 if (CantHandle) continue;
984 IAPrinterMap[I->first].push_back(IAP);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000985 }
986 }
987
988 EmitSubtargetFeatureFlagEnumeration(AWI, O);
989 EmitComputeAvailableFeatures(AWI, AsmWriter, Target, O);
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000990
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000991 std::string Header;
992 raw_string_ostream HeaderO(Header);
993
994 HeaderO << "bool " << Target.getName() << ClassName
Bill Wendling740e5b32011-06-14 03:17:20 +0000995 << "::printAliasInstr(const MCInst"
Bill Wendlingf415d8b2011-05-23 00:18:33 +0000996 << " *MI, raw_ostream &OS) {\n";
Bill Wendling3ce1b7d2011-03-21 08:59:17 +0000997
Bill Wendling44dcfd32011-04-07 21:20:06 +0000998 std::string Cases;
999 raw_string_ostream CasesO(Cases);
Bill Wendlingdd099e12011-04-08 04:08:57 +00001000 bool NeedAvailableFeatures = false;
Bill Wendling44dcfd32011-04-07 21:20:06 +00001001
1002 for (std::map<std::string, std::vector<IAPrinter*> >::iterator
1003 I = IAPrinterMap.begin(), E = IAPrinterMap.end(); I != E; ++I) {
1004 std::vector<IAPrinter*> &IAPs = I->second;
1005 std::vector<IAPrinter*> UniqueIAPs;
1006
1007 for (std::vector<IAPrinter*>::iterator
1008 II = IAPs.begin(), IE = IAPs.end(); II != IE; ++II) {
1009 IAPrinter *LHS = *II;
1010 bool IsDup = false;
1011 for (std::vector<IAPrinter*>::iterator
1012 III = IAPs.begin(), IIE = IAPs.end(); III != IIE; ++III) {
1013 IAPrinter *RHS = *III;
1014 if (LHS != RHS && *LHS == *RHS) {
1015 IsDup = true;
1016 break;
1017 }
1018 }
1019
1020 if (!IsDup) UniqueIAPs.push_back(LHS);
1021 }
1022
1023 if (UniqueIAPs.empty()) continue;
1024
1025 CasesO.indent(2) << "case " << I->first << ":\n";
1026
1027 for (std::vector<IAPrinter*>::iterator
1028 II = UniqueIAPs.begin(), IE = UniqueIAPs.end(); II != IE; ++II) {
1029 IAPrinter *IAP = *II;
1030 CasesO.indent(4);
Bill Wendlingdd099e12011-04-08 04:08:57 +00001031 NeedAvailableFeatures |= IAP->print(CasesO);
Bill Wendling44dcfd32011-04-07 21:20:06 +00001032 CasesO << '\n';
1033 }
1034
Eric Christopher721ef662011-04-18 21:28:11 +00001035 CasesO.indent(4) << "return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +00001036 }
1037
Bill Wendling740e5b32011-06-14 03:17:20 +00001038 if (CasesO.str().empty()) {
Bill Wendlingf415d8b2011-05-23 00:18:33 +00001039 O << HeaderO.str();
Eric Christopher721ef662011-04-18 21:28:11 +00001040 O << " return false;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001041 O << "}\n\n";
1042 O << "#endif // PRINT_ALIAS_INSTR\n";
1043 return;
1044 }
1045
Bill Wendlingf415d8b2011-05-23 00:18:33 +00001046 EmitGetMapOperandNumber(O);
1047
1048 O << HeaderO.str();
Bill Wendling44dcfd32011-04-07 21:20:06 +00001049 O.indent(2) << "StringRef AsmString;\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +00001050 O.indent(2) << "SmallVector<std::pair<StringRef, unsigned>, 4> OpMap;\n";
Bill Wendlingdd099e12011-04-08 04:08:57 +00001051 if (NeedAvailableFeatures)
1052 O.indent(2) << "unsigned AvailableFeatures = getAvailableFeatures();\n\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +00001053 O.indent(2) << "switch (MI->getOpcode()) {\n";
Eric Christopher721ef662011-04-18 21:28:11 +00001054 O.indent(2) << "default: return false;\n";
Bill Wendling44dcfd32011-04-07 21:20:06 +00001055 O << CasesO.str();
1056 O.indent(2) << "}\n\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001057
1058 // Code that prints the alias, replacing the operands with the ones from the
1059 // MCInst.
Bill Wendling7520e3a2011-02-26 03:09:12 +00001060 O << " std::pair<StringRef, StringRef> ASM = AsmString.split(' ');\n";
1061 O << " OS << '\\t' << ASM.first;\n";
1062
1063 O << " if (!ASM.second.empty()) {\n";
1064 O << " OS << '\\t';\n";
1065 O << " for (StringRef::iterator\n";
1066 O << " I = ASM.second.begin(), E = ASM.second.end(); I != E; ) {\n";
1067 O << " if (*I == '$') {\n";
1068 O << " StringRef::iterator Start = ++I;\n";
1069 O << " while (I != E &&\n";
1070 O << " ((*I >= 'a' && *I <= 'z') ||\n";
1071 O << " (*I >= 'A' && *I <= 'Z') ||\n";
1072 O << " (*I >= '0' && *I <= '9') ||\n";
1073 O << " *I == '_'))\n";
1074 O << " ++I;\n";
1075 O << " StringRef Name(Start, I - Start);\n";
Bill Wendlingf415d8b2011-05-23 00:18:33 +00001076 O << " printOperand(MI, getMapOperandNumber(OpMap, Name), OS);\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001077 O << " } else {\n";
1078 O << " OS << *I++;\n";
1079 O << " }\n";
1080 O << " }\n";
1081 O << " }\n\n";
1082
Eric Christopher721ef662011-04-18 21:28:11 +00001083 O << " return true;\n";
Bill Wendling7520e3a2011-02-26 03:09:12 +00001084 O << "}\n\n";
1085
1086 O << "#endif // PRINT_ALIAS_INSTR\n";
1087}
Chris Lattner05af2612009-09-13 20:08:00 +00001088
1089void AsmWriterEmitter::run(raw_ostream &O) {
1090 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
Jim Grosbach9255b8d2010-09-29 22:32:50 +00001091
Chris Lattner05af2612009-09-13 20:08:00 +00001092 EmitPrintInstruction(O);
1093 EmitGetRegisterName(O);
Chris Lattner0d7b0aa2010-02-11 22:57:32 +00001094 EmitGetInstructionName(O);
Bill Wendling7520e3a2011-02-26 03:09:12 +00001095 EmitPrintAliasInstruction(O);
Chris Lattner05af2612009-09-13 20:08:00 +00001096}
1097