blob: c94d384901f0c21d02081448c1df5c9f3fe30a23 [file] [log] [blame]
Chris Lattnerc9670ef2003-07-31 04:43:49 +00001//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +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//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerc9670ef2003-07-31 04:43:49 +00009//
Misha Brukman4e4f8632004-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 Lattnerc9670ef2003-07-31 04:43:49 +000013//
14//===----------------------------------------------------------------------===//
15
Misha Brukmand7a5b282004-08-09 19:10:43 +000016#include "CodeGenTarget.h"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000017#include "llvm/ADT/StringExtras.h"
Jim Grosbachab3d00e2010-11-02 17:35:25 +000018#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/Debug.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000020#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000021#include "llvm/TableGen/TableGenBackend.h"
Bill Wendlingeac8f352010-12-13 01:05:54 +000022#include <map>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000023#include <string>
24#include <vector>
Chris Lattner2082ebe2004-08-01 03:55:39 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Jim Grosbach60aaa762010-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 Grosbachab3d00e2010-11-02 17:35:25 +000030static cl::opt<bool>
Jim Grosbach60aaa762010-11-03 23:38:14 +000031MCEmitter("mc-emitter",
Jim Grosbachab3d00e2010-11-02 17:35:25 +000032 cl::desc("Generate CodeEmitter for use with the MC library."),
33 cl::init(false));
34
Jakob Stoklund Olesen6f36fa92012-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 Laskeyf1b05bf2006-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 Grosbach806fcc02011-07-06 21:33:38 +000060 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
61 R->getValueAsBit("isPseudo"))
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +000062 continue;
Dan Gohmanf8c73942009-04-13 15:38:05 +000063
David Greene05bce0b2011-07-29 22:43:06 +000064 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman28eefa52004-10-14 05:53:01 +000065
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000066 unsigned numBits = BI->getNumBits();
David Greeneca7fd3d2011-07-29 19:07:00 +000067
David Greene05bce0b2011-07-29 22:43:06 +000068 SmallVector<Init *, 16> NewBits(numBits);
David Greeneca7fd3d2011-07-29 19:07:00 +000069
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000070 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
71 unsigned bitSwapIdx = numBits - bit - 1;
David Greene05bce0b2011-07-29 22:43:06 +000072 Init *OrigBit = BI->getBit(bit);
73 Init *BitSwap = BI->getBit(bitSwapIdx);
David Greeneca7fd3d2011-07-29 19:07:00 +000074 NewBits[bit] = BitSwap;
75 NewBits[bitSwapIdx] = OrigBit;
Misha Brukman28eefa52004-10-14 05:53:01 +000076 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000077 if (numBits % 2) {
78 unsigned middle = (numBits + 1) / 2;
David Greeneca7fd3d2011-07-29 19:07:00 +000079 NewBits[middle] = BI->getBit(middle);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000080 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000081
David Greene05bce0b2011-07-29 22:43:06 +000082 BitsInit *NewBI = BitsInit::get(NewBits);
David Greeneca7fd3d2011-07-29 19:07:00 +000083
Jim Laskeyf1b05bf2006-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 Brukman28eefa52004-10-14 05:53:01 +000087 }
88}
89
Jim Laskeycb129032006-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 Gohman9b03da62009-12-15 20:21:44 +000092int CodeEmitterGen::getVariableBit(const std::string &VarName,
David Greene05bce0b2011-07-29 22:43:06 +000093 BitsInit *BI, int bit) {
Sean Silva6cfc8062012-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 Lattner98e969a2010-11-15 06:42:13 +000096 if (VI->getName() == VarName)
97 return VBI->getBitNum();
Sean Silva6cfc8062012-10-10 20:24:43 +000098 } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit(bit))) {
Owen Anderson4cdcb472011-04-28 17:51:45 +000099 if (VI->getName() == VarName)
100 return 0;
101 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000102
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000103 return -1;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000104}
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000105
Chris Lattner16201172010-11-15 06:59:17 +0000106void CodeEmitterGen::
David Greene05bce0b2011-07-29 22:43:06 +0000107AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName,
Eric Christopherd568b3f2011-07-11 23:06:52 +0000108 unsigned &NumberedOp,
Chris Lattner16201172010-11-15 06:59:17 +0000109 std::string &Case, CodeGenTarget &Target) {
Chris Lattner16201172010-11-15 06:59:17 +0000110 CodeGenInstruction &CGI = Target.getInstruction(R);
111
Chris Lattner8ae082b2010-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 Menezesf1bb4212012-11-09 20:29:37 +0000137 unsigned NumberOps = CGI.Operands.size();
Chris Lattner8ae082b2010-11-15 07:09:28 +0000138 /// If this operand is not supposed to be emitted by the
139 /// generated emitter, skip it.
Evandro Menezesf1bb4212012-11-09 20:29:37 +0000140 while (NumberedOp < NumberOps &&
141 CGI.Operands.isFlatOperandNotEmitted(NumberedOp))
Chris Lattner8ae082b2010-11-15 07:09:28 +0000142 ++NumberedOp;
Evandro Menezesf1adbfe2012-11-09 21:27:03 +0000143
Chris Lattner8ae082b2010-11-15 07:09:28 +0000144 OpIdx = NumberedOp++;
145 }
146
147 std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx);
148 std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName;
149
150 // If the source operand has a custom encoder, use it. This will
151 // get the encoding for all of the suboperands.
152 if (!EncoderMethodName.empty()) {
153 // A custom encoder has all of the information for the
154 // sub-operands, if there are more than one, so only
155 // query the encoder once per source operand.
156 if (SO.second == 0) {
157 Case += " // op: " + VarName + "\n" +
158 " op = " + EncoderMethodName + "(MI, " + utostr(OpIdx);
159 if (MCEmitter)
160 Case += ", Fixups";
161 Case += ");\n";
162 }
163 } else {
164 Case += " // op: " + VarName + "\n" +
165 " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
166 if (MCEmitter)
167 Case += ", Fixups";
168 Case += ");\n";
169 }
170
171 for (; bit >= 0; ) {
Chris Lattner16201172010-11-15 06:59:17 +0000172 int varBit = getVariableBit(VarName, BI, bit);
173
174 // If this bit isn't from a variable, skip it.
175 if (varBit == -1) {
176 --bit;
177 continue;
178 }
179
Bob Wilson9b8c3532011-01-27 23:08:52 +0000180 // Figure out the consecutive range of bits covered by this operand, in
Chris Lattner16201172010-11-15 06:59:17 +0000181 // order to generate better encoding code.
182 int beginInstBit = bit;
183 int beginVarBit = varBit;
184 int N = 1;
185 for (--bit; bit >= 0;) {
186 varBit = getVariableBit(VarName, BI, bit);
187 if (varBit == -1 || varBit != (beginVarBit - N)) break;
188 ++N;
189 --bit;
190 }
Chris Lattner8ae082b2010-11-15 07:09:28 +0000191
NAKAMURA Takumi89d81392012-03-09 14:52:44 +0000192 uint64_t opMask = ~(uint64_t)0 >> (64-N);
Chris Lattner16201172010-11-15 06:59:17 +0000193 int opShift = beginVarBit - N + 1;
194 opMask <<= opShift;
195 opShift = beginInstBit - beginVarBit;
196
197 if (opShift > 0) {
Owen Anderson4f8dc7b2012-01-24 18:37:29 +0000198 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) << " +
Chris Lattner16201172010-11-15 06:59:17 +0000199 itostr(opShift) + ";\n";
200 } else if (opShift < 0) {
Owen Anderson4f8dc7b2012-01-24 18:37:29 +0000201 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) >> " +
Chris Lattner16201172010-11-15 06:59:17 +0000202 itostr(-opShift) + ";\n";
203 } else {
Owen Anderson4f8dc7b2012-01-24 18:37:29 +0000204 Case += " Value |= op & UINT64_C(" + utostr(opMask) + ");\n";
Chris Lattner16201172010-11-15 06:59:17 +0000205 }
206 }
207}
208
209
210std::string CodeEmitterGen::getInstructionCase(Record *R,
211 CodeGenTarget &Target) {
212 std::string Case;
213
David Greene05bce0b2011-07-29 22:43:06 +0000214 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattner16201172010-11-15 06:59:17 +0000215 const std::vector<RecordVal> &Vals = R->getValues();
216 unsigned NumberedOp = 0;
217
218 // Loop over all of the fields in the instruction, determining which are the
219 // operands to the instruction.
220 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
221 // Ignore fixed fields in the record, we're looking for values like:
222 // bits<5> RST = { ?, ?, ?, ?, ? };
223 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
224 continue;
225
226 AddCodeToMergeInOperand(R, BI, Vals[i].getName(), NumberedOp, Case, Target);
227 }
228
229 std::string PostEmitter = R->getValueAsString("PostEncoderMethod");
230 if (!PostEmitter.empty())
231 Case += " Value = " + PostEmitter + "(MI, Value);\n";
232
233 return Case;
234}
235
Daniel Dunbar1a551802009-07-03 00:10:29 +0000236void CodeEmitterGen::run(raw_ostream &o) {
Chris Lattner67db8832010-12-13 00:23:57 +0000237 CodeGenTarget Target(Records);
Chris Lattner048c00d2003-08-01 04:38:18 +0000238 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000239
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000240 // For little-endian instruction bit encodings, reverse the bit order
241 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
Misha Brukman9fff7e12003-05-24 00:15:53 +0000242
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000243
Chris Lattnerf6502782010-03-19 00:34:35 +0000244 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
245 Target.getInstructionsByEnumValue();
Misha Brukman9fff7e12003-05-24 00:15:53 +0000246
Misha Brukmanad346ad2004-08-10 20:54:58 +0000247 // Emit function declaration
Owen Anderson4f8dc7b2012-01-24 18:37:29 +0000248 o << "uint64_t " << Target.getName();
Jim Grosbach60aaa762010-11-03 23:38:14 +0000249 if (MCEmitter)
250 o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
251 << " SmallVectorImpl<MCFixup> &Fixups) const {\n";
252 else
253 o << "CodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const {\n";
Misha Brukmanad346ad2004-08-10 20:54:58 +0000254
Jim Laskeyed393432006-07-12 19:15:43 +0000255 // Emit instruction base values
Owen Anderson40530ad2012-03-06 21:48:32 +0000256 o << " static const uint64_t InstBits[] = {\n";
Chris Lattnerf6502782010-03-19 00:34:35 +0000257 for (std::vector<const CodeGenInstruction*>::const_iterator
Jim Laskeyed393432006-07-12 19:15:43 +0000258 IN = NumberedInstructions.begin(),
259 EN = NumberedInstructions.end();
260 IN != EN; ++IN) {
261 const CodeGenInstruction *CGI = *IN;
262 Record *R = CGI->TheDef;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000263
Jim Grosbach806fcc02011-07-06 21:33:38 +0000264 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
265 R->getValueAsBit("isPseudo")) {
Owen Anderson4f8dc7b2012-01-24 18:37:29 +0000266 o << " UINT64_C(0),\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000267 continue;
268 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000269
David Greene05bce0b2011-07-29 22:43:06 +0000270 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000271
Chris Lattner16201172010-11-15 06:59:17 +0000272 // Start by filling in fixed values.
Owen Anderson40530ad2012-03-06 21:48:32 +0000273 uint64_t Value = 0;
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000274 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +0000275 if (BitInit *B = dyn_cast<BitInit>(BI->getBit(e-i-1)))
Owen Anderson40530ad2012-03-06 21:48:32 +0000276 Value |= (uint64_t)B->getValue() << (e-i-1);
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000277 }
Owen Anderson4f8dc7b2012-01-24 18:37:29 +0000278 o << " UINT64_C(" << Value << ")," << '\t' << "// " << R->getName() << "\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000279 }
Owen Anderson4f8dc7b2012-01-24 18:37:29 +0000280 o << " UINT64_C(0)\n };\n";
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000281
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000282 // Map to accumulate all the cases.
283 std::map<std::string, std::vector<std::string> > CaseMap;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000284
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000285 // Construct all cases statement for each opcode
286 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
287 IC != EC; ++IC) {
288 Record *R = *IC;
Jim Grosbach806fcc02011-07-06 21:33:38 +0000289 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
290 R->getValueAsBit("isPseudo"))
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000291 continue;
Jim Grosbach0ed92f22011-02-03 23:26:36 +0000292 const std::string &InstName = R->getValueAsString("Namespace") + "::"
293 + R->getName();
Chris Lattner16201172010-11-15 06:59:17 +0000294 std::string Case = getInstructionCase(R, Target);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000295
Chris Lattner16201172010-11-15 06:59:17 +0000296 CaseMap[Case].push_back(InstName);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000297 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000298
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000299 // Emit initial function code
300 o << " const unsigned opcode = MI.getOpcode();\n"
Owen Anderson40530ad2012-03-06 21:48:32 +0000301 << " uint64_t Value = InstBits[opcode];\n"
302 << " uint64_t op = 0;\n"
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000303 << " (void)op; // suppress warning\n"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000304 << " switch (opcode) {\n";
305
306 // Emit each case statement
307 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
308 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
309 const std::string &Case = IE->first;
310 std::vector<std::string> &InstList = IE->second;
311
312 for (int i = 0, N = InstList.size(); i < N; i++) {
313 if (i) o << "\n";
Jim Grosbach0ed92f22011-02-03 23:26:36 +0000314 o << " case " << InstList[i] << ":";
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000315 }
316 o << " {\n";
317 o << Case;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000318 o << " break;\n"
319 << " }\n";
320 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000321
Misha Brukman28eefa52004-10-14 05:53:01 +0000322 // Default case: unhandled opcode
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000323 o << " default:\n"
Torok Edwin804e0fe2009-07-08 19:04:27 +0000324 << " std::string msg;\n"
325 << " raw_string_ostream Msg(msg);\n"
326 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner75361b62010-04-07 22:58:41 +0000327 << " report_fatal_error(Msg.str());\n"
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000328 << " }\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +0000329 << " return Value;\n"
Misha Brukman28eefa52004-10-14 05:53:01 +0000330 << "}\n\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000331}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000332
333} // End anonymous namespace
334
335namespace llvm {
336
337void EmitCodeEmitter(RecordKeeper &RK, raw_ostream &OS) {
338 emitSourceFileHeader("Machine Code Emitter", OS);
339 CodeEmitterGen(RK).run(OS);
340}
341
342} // End llvm namespace