blob: 4d0c0ca8e7019a82e2ade70369492d44d5023b22 [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"
Jim Laskeya44f6262006-07-13 21:02:53 +000017#include "llvm/ADT/StringExtras.h"
Jim Grosbach0b7fda22010-11-02 17:35:25 +000018#include "llvm/Support/CommandLine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/Support/Debug.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000020#include "llvm/TableGen/Record.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:
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000044 int getVariableBit(const std::string &VarName, BitsInit *BI, int bit);
45 std::string getInstructionCase(Record *R, CodeGenTarget &Target);
46 void AddCodeToMergeInOperand(Record *R, BitsInit *BI,
47 const std::string &VarName,
48 unsigned &NumberedOp,
Hal Finkel5457bd02014-03-13 07:57:54 +000049 std::set<unsigned> &NamedOpIndices,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000050 std::string &Case, CodeGenTarget &Target);
51
52};
53
Jim Laskey10d4b042006-07-13 22:17:08 +000054// If the VarBitInit at position 'bit' matches the specified variable then
55// return the variable bit position. Otherwise return -1.
Dan Gohmanc87c16b2009-12-15 20:21:44 +000056int CodeEmitterGen::getVariableBit(const std::string &VarName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000057 BitsInit *BI, int bit) {
Sean Silvafb509ed2012-10-10 20:24:43 +000058 if (VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit(bit))) {
59 if (VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar()))
Chris Lattner42c4ac42010-11-15 06:42:13 +000060 if (VI->getName() == VarName)
61 return VBI->getBitNum();
Sean Silvafb509ed2012-10-10 20:24:43 +000062 } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit(bit))) {
Owen Anderson715973f2011-04-28 17:51:45 +000063 if (VI->getName() == VarName)
64 return 0;
65 }
Jim Grosbachdaab6602010-10-07 16:56:28 +000066
Jim Laskeya44f6262006-07-13 21:02:53 +000067 return -1;
Jim Grosbachdaab6602010-10-07 16:56:28 +000068}
Jim Laskeya44f6262006-07-13 21:02:53 +000069
Chris Lattnerc19d5102010-11-15 06:59:17 +000070void CodeEmitterGen::
David Greeneaf8ee2c2011-07-29 22:43:06 +000071AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName,
Eric Christopher71520a82011-07-11 23:06:52 +000072 unsigned &NumberedOp,
Hal Finkel5457bd02014-03-13 07:57:54 +000073 std::set<unsigned> &NamedOpIndices,
Chris Lattnerc19d5102010-11-15 06:59:17 +000074 std::string &Case, CodeGenTarget &Target) {
Chris Lattnerc19d5102010-11-15 06:59:17 +000075 CodeGenInstruction &CGI = Target.getInstruction(R);
76
Chris Lattner578c7652010-11-15 07:09:28 +000077 // Determine if VarName actually contributes to the Inst encoding.
78 int bit = BI->getNumBits()-1;
79
80 // Scan for a bit that this contributed to.
81 for (; bit >= 0; ) {
82 if (getVariableBit(VarName, BI, bit) != -1)
83 break;
84
85 --bit;
86 }
87
88 // If we found no bits, ignore this value, otherwise emit the call to get the
89 // operand encoding.
90 if (bit < 0) return;
91
92 // If the operand matches by name, reference according to that
93 // operand number. Non-matching operands are assumed to be in
94 // order.
95 unsigned OpIdx;
96 if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
97 // Get the machine operand number for the indicated operand.
98 OpIdx = CGI.Operands[OpIdx].MIOperandNo;
99 assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) &&
100 "Explicitly used operand also marked as not emitted!");
101 } else {
Evandro Menezes567698a2012-11-09 20:29:37 +0000102 unsigned NumberOps = CGI.Operands.size();
Chris Lattner578c7652010-11-15 07:09:28 +0000103 /// If this operand is not supposed to be emitted by the
104 /// generated emitter, skip it.
Evandro Menezes567698a2012-11-09 20:29:37 +0000105 while (NumberedOp < NumberOps &&
Hal Finkel5457bd02014-03-13 07:57:54 +0000106 (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
107 (NamedOpIndices.size() && NamedOpIndices.count(
Hal Finkel68e03bd2014-03-22 11:33:32 +0000108 CGI.Operands.getSubOperandNumber(NumberedOp).first)))) {
Chris Lattner578c7652010-11-15 07:09:28 +0000109 ++NumberedOp;
Evandro Menezes03789a92012-11-09 21:27:03 +0000110
Hal Finkel68e03bd2014-03-22 11:33:32 +0000111 if (NumberedOp >= CGI.Operands.back().MIOperandNo +
112 CGI.Operands.back().MINumOperands) {
113 errs() << "Too few operands in record " << R->getName() <<
114 " (no match for variable " << VarName << "):\n";
115 errs() << *R;
116 errs() << '\n';
117
118 return;
119 }
120 }
121
Chris Lattner578c7652010-11-15 07:09:28 +0000122 OpIdx = NumberedOp++;
123 }
124
125 std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx);
126 std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName;
127
128 // If the source operand has a custom encoder, use it. This will
129 // get the encoding for all of the suboperands.
130 if (!EncoderMethodName.empty()) {
131 // A custom encoder has all of the information for the
132 // sub-operands, if there are more than one, so only
133 // query the encoder once per source operand.
134 if (SO.second == 0) {
135 Case += " // op: " + VarName + "\n" +
136 " op = " + EncoderMethodName + "(MI, " + utostr(OpIdx);
137 if (MCEmitter)
David Woodhouse3fa98a62014-01-28 23:13:18 +0000138 Case += ", Fixups, STI";
Chris Lattner578c7652010-11-15 07:09:28 +0000139 Case += ");\n";
140 }
141 } else {
142 Case += " // op: " + VarName + "\n" +
143 " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
144 if (MCEmitter)
David Woodhouse3fa98a62014-01-28 23:13:18 +0000145 Case += ", Fixups, STI";
Chris Lattner578c7652010-11-15 07:09:28 +0000146 Case += ");\n";
147 }
148
149 for (; bit >= 0; ) {
Chris Lattnerc19d5102010-11-15 06:59:17 +0000150 int varBit = getVariableBit(VarName, BI, bit);
151
152 // If this bit isn't from a variable, skip it.
153 if (varBit == -1) {
154 --bit;
155 continue;
156 }
157
Bob Wilsonf9bab3a2011-01-27 23:08:52 +0000158 // Figure out the consecutive range of bits covered by this operand, in
Chris Lattnerc19d5102010-11-15 06:59:17 +0000159 // order to generate better encoding code.
160 int beginInstBit = bit;
161 int beginVarBit = varBit;
162 int N = 1;
163 for (--bit; bit >= 0;) {
164 varBit = getVariableBit(VarName, BI, bit);
165 if (varBit == -1 || varBit != (beginVarBit - N)) break;
166 ++N;
167 --bit;
168 }
Chris Lattner578c7652010-11-15 07:09:28 +0000169
NAKAMURA Takumic72fdf42012-03-09 14:52:44 +0000170 uint64_t opMask = ~(uint64_t)0 >> (64-N);
Chris Lattnerc19d5102010-11-15 06:59:17 +0000171 int opShift = beginVarBit - N + 1;
172 opMask <<= opShift;
173 opShift = beginInstBit - beginVarBit;
174
175 if (opShift > 0) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000176 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) << " +
Chris Lattnerc19d5102010-11-15 06:59:17 +0000177 itostr(opShift) + ";\n";
178 } else if (opShift < 0) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000179 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) >> " +
Chris Lattnerc19d5102010-11-15 06:59:17 +0000180 itostr(-opShift) + ";\n";
181 } else {
Owen Andersond845d9d2012-01-24 18:37:29 +0000182 Case += " Value |= op & UINT64_C(" + utostr(opMask) + ");\n";
Chris Lattnerc19d5102010-11-15 06:59:17 +0000183 }
184 }
185}
186
187
188std::string CodeEmitterGen::getInstructionCase(Record *R,
189 CodeGenTarget &Target) {
190 std::string Case;
191
David Greeneaf8ee2c2011-07-29 22:43:06 +0000192 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattnerc19d5102010-11-15 06:59:17 +0000193 const std::vector<RecordVal> &Vals = R->getValues();
194 unsigned NumberedOp = 0;
195
Hal Finkel5457bd02014-03-13 07:57:54 +0000196 std::set<unsigned> NamedOpIndices;
197 // Collect the set of operand indices that might correspond to named
198 // operand, and skip these when assigning operands based on position.
199 if (Target.getInstructionSet()->
200 getValueAsBit("noNamedPositionallyEncodedOperands")) {
201 CodeGenInstruction &CGI = Target.getInstruction(R);
202 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
203 unsigned OpIdx;
204 if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
205 continue;
206
207 NamedOpIndices.insert(OpIdx);
208 }
209 }
210
Chris Lattnerc19d5102010-11-15 06:59:17 +0000211 // Loop over all of the fields in the instruction, determining which are the
212 // operands to the instruction.
213 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
214 // Ignore fixed fields in the record, we're looking for values like:
215 // bits<5> RST = { ?, ?, ?, ?, ? };
216 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
217 continue;
218
Hal Finkel5457bd02014-03-13 07:57:54 +0000219 AddCodeToMergeInOperand(R, BI, Vals[i].getName(), NumberedOp,
220 NamedOpIndices, Case, Target);
Chris Lattnerc19d5102010-11-15 06:59:17 +0000221 }
222
223 std::string PostEmitter = R->getValueAsString("PostEncoderMethod");
David Woodhouse3fa98a62014-01-28 23:13:18 +0000224 if (!PostEmitter.empty()) {
225 Case += " Value = " + PostEmitter + "(MI, Value";
226 if (MCEmitter)
227 Case += ", STI";
228 Case += ");\n";
229 }
Chris Lattnerc19d5102010-11-15 06:59:17 +0000230
231 return Case;
232}
233
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000234void CodeEmitterGen::run(raw_ostream &o) {
Chris Lattner77d369c2010-12-13 00:23:57 +0000235 CodeGenTarget Target(Records);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000236 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbachdaab6602010-10-07 16:56:28 +0000237
Jim Laskeya44f6262006-07-13 21:02:53 +0000238 // For little-endian instruction bit encodings, reverse the bit order
Hal Finkel81e6fcc2013-12-17 22:37:50 +0000239 Target.reverseBitsForLittleEndianEncoding();
Jim Grosbachdaab6602010-10-07 16:56:28 +0000240
Chris Lattner918be522010-03-19 00:34:35 +0000241 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
242 Target.getInstructionsByEnumValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000243
Misha Brukman422d0fa2004-08-10 20:54:58 +0000244 // Emit function declaration
Owen Andersond845d9d2012-01-24 18:37:29 +0000245 o << "uint64_t " << Target.getName();
Jim Grosbache4e6bf42010-11-03 23:38:14 +0000246 if (MCEmitter)
247 o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
David Woodhouse3fa98a62014-01-28 23:13:18 +0000248 << " SmallVectorImpl<MCFixup> &Fixups,\n"
249 << " const MCSubtargetInfo &STI) const {\n";
Jim Grosbache4e6bf42010-11-03 23:38:14 +0000250 else
251 o << "CodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const {\n";
Misha Brukman422d0fa2004-08-10 20:54:58 +0000252
Jim Laskey23bd4802006-07-12 19:15:43 +0000253 // Emit instruction base values
Owen Anderson773642d2012-03-06 21:48:32 +0000254 o << " static const uint64_t InstBits[] = {\n";
Chris Lattner918be522010-03-19 00:34:35 +0000255 for (std::vector<const CodeGenInstruction*>::const_iterator
Jim Laskey23bd4802006-07-12 19:15:43 +0000256 IN = NumberedInstructions.begin(),
257 EN = NumberedInstructions.end();
258 IN != EN; ++IN) {
259 const CodeGenInstruction *CGI = *IN;
260 Record *R = CGI->TheDef;
Jim Grosbachdaab6602010-10-07 16:56:28 +0000261
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000262 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
263 R->getValueAsBit("isPseudo")) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000264 o << " UINT64_C(0),\n";
Jim Laskey23bd4802006-07-12 19:15:43 +0000265 continue;
266 }
Jim Grosbachdaab6602010-10-07 16:56:28 +0000267
David Greeneaf8ee2c2011-07-29 22:43:06 +0000268 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000269
Chris Lattnerc19d5102010-11-15 06:59:17 +0000270 // Start by filling in fixed values.
Owen Anderson773642d2012-03-06 21:48:32 +0000271 uint64_t Value = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000272 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000273 if (BitInit *B = dyn_cast<BitInit>(BI->getBit(e-i-1)))
Owen Anderson773642d2012-03-06 21:48:32 +0000274 Value |= (uint64_t)B->getValue() << (e-i-1);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000275 }
Owen Andersond845d9d2012-01-24 18:37:29 +0000276 o << " UINT64_C(" << Value << ")," << '\t' << "// " << R->getName() << "\n";
Jim Laskey23bd4802006-07-12 19:15:43 +0000277 }
Owen Andersond845d9d2012-01-24 18:37:29 +0000278 o << " UINT64_C(0)\n };\n";
Jim Grosbachdaab6602010-10-07 16:56:28 +0000279
Jim Laskeya44f6262006-07-13 21:02:53 +0000280 // Map to accumulate all the cases.
281 std::map<std::string, std::vector<std::string> > CaseMap;
Jim Grosbachdaab6602010-10-07 16:56:28 +0000282
Jim Laskeya44f6262006-07-13 21:02:53 +0000283 // Construct all cases statement for each opcode
284 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
285 IC != EC; ++IC) {
286 Record *R = *IC;
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000287 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
288 R->getValueAsBit("isPseudo"))
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000289 continue;
Jim Grosbachcd25b862011-02-03 23:26:36 +0000290 const std::string &InstName = R->getValueAsString("Namespace") + "::"
291 + R->getName();
Chris Lattnerc19d5102010-11-15 06:59:17 +0000292 std::string Case = getInstructionCase(R, Target);
Dan Gohman60a446a2009-04-13 15:38:05 +0000293
Chris Lattnerc19d5102010-11-15 06:59:17 +0000294 CaseMap[Case].push_back(InstName);
Jim Laskeya44f6262006-07-13 21:02:53 +0000295 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000296
Jim Laskeya44f6262006-07-13 21:02:53 +0000297 // Emit initial function code
298 o << " const unsigned opcode = MI.getOpcode();\n"
Owen Anderson773642d2012-03-06 21:48:32 +0000299 << " uint64_t Value = InstBits[opcode];\n"
300 << " uint64_t op = 0;\n"
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000301 << " (void)op; // suppress warning\n"
Jim Laskeya44f6262006-07-13 21:02:53 +0000302 << " switch (opcode) {\n";
303
304 // Emit each case statement
305 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
306 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
307 const std::string &Case = IE->first;
308 std::vector<std::string> &InstList = IE->second;
309
310 for (int i = 0, N = InstList.size(); i < N; i++) {
311 if (i) o << "\n";
Jim Grosbachcd25b862011-02-03 23:26:36 +0000312 o << " case " << InstList[i] << ":";
Jim Laskeya44f6262006-07-13 21:02:53 +0000313 }
314 o << " {\n";
315 o << Case;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000316 o << " break;\n"
317 << " }\n";
318 }
319
Misha Brukman8393c152004-10-14 05:53:01 +0000320 // Default case: unhandled opcode
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000321 o << " default:\n"
Alp Tokere69170a2014-06-26 22:52:05 +0000322 << " std::string msg;\n"
323 << " raw_string_ostream Msg(msg);\n"
Torok Edwinfa040022009-07-08 19:04:27 +0000324 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner2104b8d2010-04-07 22:58:41 +0000325 << " report_fatal_error(Msg.str());\n"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000326 << " }\n"
327 << " return Value;\n"
Misha Brukman8393c152004-10-14 05:53:01 +0000328 << "}\n\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000329}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000330
331} // End anonymous namespace
332
333namespace llvm {
334
335void EmitCodeEmitter(RecordKeeper &RK, raw_ostream &OS) {
336 emitSourceFileHeader("Machine Code Emitter", OS);
337 CodeEmitterGen(RK).run(OS);
338}
339
340} // End llvm namespace