blob: bcca15cd57e0292f5a651531bba2d81e53a43c5c [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
Misha Brukman8e5492e2004-08-04 22:07:54 +000010// CodeEmitterGen uses the descriptions of instructions and their fields to
11// construct an automated code emitter: a function that, given a MachineInstr,
12// returns the (currently, 32-bit unsigned) value of the instruction.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000013//
14//===----------------------------------------------------------------------===//
15
Misha Brukman920ae952004-08-09 19:10:43 +000016#include "CodeGenTarget.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000017#include "llvm/TableGen/Record.h"
Jim Laskeya44f6262006-07-13 21:02:53 +000018#include "llvm/ADT/StringExtras.h"
Jim Grosbach0b7fda22010-11-02 17:35:25 +000019#include "llvm/Support/CommandLine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/Debug.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000021#include "llvm/TableGen/TableGenBackend.h"
Bill Wendling53438362010-12-13 01:05:54 +000022#include <map>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000023#include <string>
24#include <vector>
Chris Lattner68478662004-08-01 03:55:39 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Jim Grosbache4e6bf42010-11-03 23:38:14 +000027// FIXME: Somewhat hackish to use a command line option for this. There should
28// be a CodeEmitter class in the Target.td that controls this sort of thing
29// instead.
Jim Grosbach0b7fda22010-11-02 17:35:25 +000030static cl::opt<bool>
Jim Grosbache4e6bf42010-11-03 23:38:14 +000031MCEmitter("mc-emitter",
Jim Grosbach0b7fda22010-11-02 17:35:25 +000032 cl::desc("Generate CodeEmitter for use with the MC library."),
33 cl::init(false));
34
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000035namespace {
36
37class CodeEmitterGen {
38 RecordKeeper &Records;
39public:
40 CodeEmitterGen(RecordKeeper &R) : Records(R) {}
41
42 void run(raw_ostream &o);
43private:
44 void emitMachineOpEmitter(raw_ostream &o, const std::string &Namespace);
45 void emitGetValueBit(raw_ostream &o, const std::string &Namespace);
46 void reverseBits(std::vector<Record*> &Insts);
47 int getVariableBit(const std::string &VarName, BitsInit *BI, int bit);
48 std::string getInstructionCase(Record *R, CodeGenTarget &Target);
49 void AddCodeToMergeInOperand(Record *R, BitsInit *BI,
50 const std::string &VarName,
51 unsigned &NumberedOp,
52 std::string &Case, CodeGenTarget &Target);
53
54};
55
Jim Laskeya44f6262006-07-13 21:02:53 +000056void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) {
57 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
58 I != E; ++I) {
59 Record *R = *I;
Jim Grosbachf3fd36e2011-07-06 21:33:38 +000060 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
61 R->getValueAsBit("isPseudo"))
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +000062 continue;
Dan Gohman60a446a2009-04-13 15:38:05 +000063
David Greeneaf8ee2c2011-07-29 22:43:06 +000064 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman8393c152004-10-14 05:53:01 +000065
Jim Laskeya44f6262006-07-13 21:02:53 +000066 unsigned numBits = BI->getNumBits();
David Greeneb3da8122011-07-29 19:07:00 +000067
David Greeneaf8ee2c2011-07-29 22:43:06 +000068 SmallVector<Init *, 16> NewBits(numBits);
David Greeneb3da8122011-07-29 19:07:00 +000069
Jim Laskeya44f6262006-07-13 21:02:53 +000070 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
71 unsigned bitSwapIdx = numBits - bit - 1;
David Greeneaf8ee2c2011-07-29 22:43:06 +000072 Init *OrigBit = BI->getBit(bit);
73 Init *BitSwap = BI->getBit(bitSwapIdx);
David Greeneb3da8122011-07-29 19:07:00 +000074 NewBits[bit] = BitSwap;
75 NewBits[bitSwapIdx] = OrigBit;
Misha Brukman8393c152004-10-14 05:53:01 +000076 }
Jim Laskeya44f6262006-07-13 21:02:53 +000077 if (numBits % 2) {
78 unsigned middle = (numBits + 1) / 2;
David Greeneb3da8122011-07-29 19:07:00 +000079 NewBits[middle] = BI->getBit(middle);
Jim Laskeya44f6262006-07-13 21:02:53 +000080 }
Jim Grosbachdaab6602010-10-07 16:56:28 +000081
David Greeneaf8ee2c2011-07-29 22:43:06 +000082 BitsInit *NewBI = BitsInit::get(NewBits);
David Greeneb3da8122011-07-29 19:07:00 +000083
Jim Laskeya44f6262006-07-13 21:02:53 +000084 // Update the bits in reversed order so that emitInstrOpBits will get the
85 // correct endianness.
86 R->getValue("Inst")->setValue(NewBI);
Misha Brukman8393c152004-10-14 05:53:01 +000087 }
88}
89
Jim Laskey10d4b042006-07-13 22:17:08 +000090// If the VarBitInit at position 'bit' matches the specified variable then
91// return the variable bit position. Otherwise return -1.
Dan Gohmanc87c16b2009-12-15 20:21:44 +000092int CodeEmitterGen::getVariableBit(const std::string &VarName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000093 BitsInit *BI, int bit) {
Sean Silvafb509ed2012-10-10 20:24:43 +000094 if (VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit(bit))) {
95 if (VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar()))
Chris Lattner42c4ac42010-11-15 06:42:13 +000096 if (VI->getName() == VarName)
97 return VBI->getBitNum();
Sean Silvafb509ed2012-10-10 20:24:43 +000098 } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit(bit))) {
Owen Anderson715973f2011-04-28 17:51:45 +000099 if (VI->getName() == VarName)
100 return 0;
101 }
Jim Grosbachdaab6602010-10-07 16:56:28 +0000102
Jim Laskeya44f6262006-07-13 21:02:53 +0000103 return -1;
Jim Grosbachdaab6602010-10-07 16:56:28 +0000104}
Jim Laskeya44f6262006-07-13 21:02:53 +0000105
Chris Lattnerc19d5102010-11-15 06:59:17 +0000106void CodeEmitterGen::
David Greeneaf8ee2c2011-07-29 22:43:06 +0000107AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName,
Eric Christopher71520a82011-07-11 23:06:52 +0000108 unsigned &NumberedOp,
Chris Lattnerc19d5102010-11-15 06:59:17 +0000109 std::string &Case, CodeGenTarget &Target) {
Chris Lattnerc19d5102010-11-15 06:59:17 +0000110 CodeGenInstruction &CGI = Target.getInstruction(R);
111
Chris Lattner578c7652010-11-15 07:09:28 +0000112 // Determine if VarName actually contributes to the Inst encoding.
113 int bit = BI->getNumBits()-1;
114
115 // Scan for a bit that this contributed to.
116 for (; bit >= 0; ) {
117 if (getVariableBit(VarName, BI, bit) != -1)
118 break;
119
120 --bit;
121 }
122
123 // If we found no bits, ignore this value, otherwise emit the call to get the
124 // operand encoding.
125 if (bit < 0) return;
126
127 // If the operand matches by name, reference according to that
128 // operand number. Non-matching operands are assumed to be in
129 // order.
130 unsigned OpIdx;
131 if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
132 // Get the machine operand number for the indicated operand.
133 OpIdx = CGI.Operands[OpIdx].MIOperandNo;
134 assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) &&
135 "Explicitly used operand also marked as not emitted!");
136 } else {
Evandro Menezes567698a2012-11-09 20:29:37 +0000137 unsigned NumberOps = CGI.Operands.size();
Chris Lattner578c7652010-11-15 07:09:28 +0000138 /// If this operand is not supposed to be emitted by the
139 /// generated emitter, skip it.
Evandro Menezes567698a2012-11-09 20:29:37 +0000140 while (NumberedOp < NumberOps &&
141 CGI.Operands.isFlatOperandNotEmitted(NumberedOp))
Chris Lattner578c7652010-11-15 07:09:28 +0000142 ++NumberedOp;
Evandro Menezes567698a2012-11-09 20:29:37 +0000143 // If this operand has not been found, ignore it.
144 if (NumberedOp >= NumberOps)
145 return;
Chris Lattner578c7652010-11-15 07:09:28 +0000146 OpIdx = NumberedOp++;
147 }
148
149 std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx);
150 std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName;
151
152 // If the source operand has a custom encoder, use it. This will
153 // get the encoding for all of the suboperands.
154 if (!EncoderMethodName.empty()) {
155 // A custom encoder has all of the information for the
156 // sub-operands, if there are more than one, so only
157 // query the encoder once per source operand.
158 if (SO.second == 0) {
159 Case += " // op: " + VarName + "\n" +
160 " op = " + EncoderMethodName + "(MI, " + utostr(OpIdx);
161 if (MCEmitter)
162 Case += ", Fixups";
163 Case += ");\n";
164 }
165 } else {
166 Case += " // op: " + VarName + "\n" +
167 " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
168 if (MCEmitter)
169 Case += ", Fixups";
170 Case += ");\n";
171 }
172
173 for (; bit >= 0; ) {
Chris Lattnerc19d5102010-11-15 06:59:17 +0000174 int varBit = getVariableBit(VarName, BI, bit);
175
176 // If this bit isn't from a variable, skip it.
177 if (varBit == -1) {
178 --bit;
179 continue;
180 }
181
Bob Wilsonf9bab3a2011-01-27 23:08:52 +0000182 // Figure out the consecutive range of bits covered by this operand, in
Chris Lattnerc19d5102010-11-15 06:59:17 +0000183 // order to generate better encoding code.
184 int beginInstBit = bit;
185 int beginVarBit = varBit;
186 int N = 1;
187 for (--bit; bit >= 0;) {
188 varBit = getVariableBit(VarName, BI, bit);
189 if (varBit == -1 || varBit != (beginVarBit - N)) break;
190 ++N;
191 --bit;
192 }
Chris Lattner578c7652010-11-15 07:09:28 +0000193
NAKAMURA Takumic72fdf42012-03-09 14:52:44 +0000194 uint64_t opMask = ~(uint64_t)0 >> (64-N);
Chris Lattnerc19d5102010-11-15 06:59:17 +0000195 int opShift = beginVarBit - N + 1;
196 opMask <<= opShift;
197 opShift = beginInstBit - beginVarBit;
198
199 if (opShift > 0) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000200 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) << " +
Chris Lattnerc19d5102010-11-15 06:59:17 +0000201 itostr(opShift) + ";\n";
202 } else if (opShift < 0) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000203 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) >> " +
Chris Lattnerc19d5102010-11-15 06:59:17 +0000204 itostr(-opShift) + ";\n";
205 } else {
Owen Andersond845d9d2012-01-24 18:37:29 +0000206 Case += " Value |= op & UINT64_C(" + utostr(opMask) + ");\n";
Chris Lattnerc19d5102010-11-15 06:59:17 +0000207 }
208 }
209}
210
211
212std::string CodeEmitterGen::getInstructionCase(Record *R,
213 CodeGenTarget &Target) {
214 std::string Case;
215
David Greeneaf8ee2c2011-07-29 22:43:06 +0000216 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattnerc19d5102010-11-15 06:59:17 +0000217 const std::vector<RecordVal> &Vals = R->getValues();
218 unsigned NumberedOp = 0;
219
220 // Loop over all of the fields in the instruction, determining which are the
221 // operands to the instruction.
222 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
223 // Ignore fixed fields in the record, we're looking for values like:
224 // bits<5> RST = { ?, ?, ?, ?, ? };
225 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
226 continue;
227
228 AddCodeToMergeInOperand(R, BI, Vals[i].getName(), NumberedOp, Case, Target);
229 }
230
231 std::string PostEmitter = R->getValueAsString("PostEncoderMethod");
232 if (!PostEmitter.empty())
233 Case += " Value = " + PostEmitter + "(MI, Value);\n";
234
235 return Case;
236}
237
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000238void CodeEmitterGen::run(raw_ostream &o) {
Chris Lattner77d369c2010-12-13 00:23:57 +0000239 CodeGenTarget Target(Records);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000240 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbachdaab6602010-10-07 16:56:28 +0000241
Jim Laskeya44f6262006-07-13 21:02:53 +0000242 // For little-endian instruction bit encodings, reverse the bit order
243 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000244
Jim Grosbachdaab6602010-10-07 16:56:28 +0000245
Chris Lattner918be522010-03-19 00:34:35 +0000246 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
247 Target.getInstructionsByEnumValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000248
Misha Brukman422d0fa2004-08-10 20:54:58 +0000249 // Emit function declaration
Owen Andersond845d9d2012-01-24 18:37:29 +0000250 o << "uint64_t " << Target.getName();
Jim Grosbache4e6bf42010-11-03 23:38:14 +0000251 if (MCEmitter)
252 o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
253 << " SmallVectorImpl<MCFixup> &Fixups) const {\n";
254 else
255 o << "CodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const {\n";
Misha Brukman422d0fa2004-08-10 20:54:58 +0000256
Jim Laskey23bd4802006-07-12 19:15:43 +0000257 // Emit instruction base values
Owen Anderson773642d2012-03-06 21:48:32 +0000258 o << " static const uint64_t InstBits[] = {\n";
Chris Lattner918be522010-03-19 00:34:35 +0000259 for (std::vector<const CodeGenInstruction*>::const_iterator
Jim Laskey23bd4802006-07-12 19:15:43 +0000260 IN = NumberedInstructions.begin(),
261 EN = NumberedInstructions.end();
262 IN != EN; ++IN) {
263 const CodeGenInstruction *CGI = *IN;
264 Record *R = CGI->TheDef;
Jim Grosbachdaab6602010-10-07 16:56:28 +0000265
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000266 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
267 R->getValueAsBit("isPseudo")) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000268 o << " UINT64_C(0),\n";
Jim Laskey23bd4802006-07-12 19:15:43 +0000269 continue;
270 }
Jim Grosbachdaab6602010-10-07 16:56:28 +0000271
David Greeneaf8ee2c2011-07-29 22:43:06 +0000272 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000273
Chris Lattnerc19d5102010-11-15 06:59:17 +0000274 // Start by filling in fixed values.
Owen Anderson773642d2012-03-06 21:48:32 +0000275 uint64_t Value = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000276 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000277 if (BitInit *B = dyn_cast<BitInit>(BI->getBit(e-i-1)))
Owen Anderson773642d2012-03-06 21:48:32 +0000278 Value |= (uint64_t)B->getValue() << (e-i-1);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000279 }
Owen Andersond845d9d2012-01-24 18:37:29 +0000280 o << " UINT64_C(" << Value << ")," << '\t' << "// " << R->getName() << "\n";
Jim Laskey23bd4802006-07-12 19:15:43 +0000281 }
Owen Andersond845d9d2012-01-24 18:37:29 +0000282 o << " UINT64_C(0)\n };\n";
Jim Grosbachdaab6602010-10-07 16:56:28 +0000283
Jim Laskeya44f6262006-07-13 21:02:53 +0000284 // Map to accumulate all the cases.
285 std::map<std::string, std::vector<std::string> > CaseMap;
Jim Grosbachdaab6602010-10-07 16:56:28 +0000286
Jim Laskeya44f6262006-07-13 21:02:53 +0000287 // Construct all cases statement for each opcode
288 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
289 IC != EC; ++IC) {
290 Record *R = *IC;
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000291 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
292 R->getValueAsBit("isPseudo"))
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000293 continue;
Jim Grosbachcd25b862011-02-03 23:26:36 +0000294 const std::string &InstName = R->getValueAsString("Namespace") + "::"
295 + R->getName();
Chris Lattnerc19d5102010-11-15 06:59:17 +0000296 std::string Case = getInstructionCase(R, Target);
Dan Gohman60a446a2009-04-13 15:38:05 +0000297
Chris Lattnerc19d5102010-11-15 06:59:17 +0000298 CaseMap[Case].push_back(InstName);
Jim Laskeya44f6262006-07-13 21:02:53 +0000299 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000300
Jim Laskeya44f6262006-07-13 21:02:53 +0000301 // Emit initial function code
302 o << " const unsigned opcode = MI.getOpcode();\n"
Owen Anderson773642d2012-03-06 21:48:32 +0000303 << " uint64_t Value = InstBits[opcode];\n"
304 << " uint64_t op = 0;\n"
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000305 << " (void)op; // suppress warning\n"
Jim Laskeya44f6262006-07-13 21:02:53 +0000306 << " switch (opcode) {\n";
307
308 // Emit each case statement
309 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
310 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
311 const std::string &Case = IE->first;
312 std::vector<std::string> &InstList = IE->second;
313
314 for (int i = 0, N = InstList.size(); i < N; i++) {
315 if (i) o << "\n";
Jim Grosbachcd25b862011-02-03 23:26:36 +0000316 o << " case " << InstList[i] << ":";
Jim Laskeya44f6262006-07-13 21:02:53 +0000317 }
318 o << " {\n";
319 o << Case;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000320 o << " break;\n"
321 << " }\n";
322 }
323
Misha Brukman8393c152004-10-14 05:53:01 +0000324 // Default case: unhandled opcode
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000325 o << " default:\n"
Torok Edwinfa040022009-07-08 19:04:27 +0000326 << " std::string msg;\n"
327 << " raw_string_ostream Msg(msg);\n"
328 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner2104b8d2010-04-07 22:58:41 +0000329 << " report_fatal_error(Msg.str());\n"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000330 << " }\n"
331 << " return Value;\n"
Misha Brukman8393c152004-10-14 05:53:01 +0000332 << "}\n\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000333}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000334
335} // End anonymous namespace
336
337namespace llvm {
338
339void EmitCodeEmitter(RecordKeeper &RK, raw_ostream &OS) {
340 emitSourceFileHeader("Machine Code Emitter", OS);
341 CodeEmitterGen(RK).run(OS);
342}
343
344} // End llvm namespace