blob: 46fcdf5e96ffdcc48a4d17a94094ac94cbd64fdf [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
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000027namespace {
28
29class CodeEmitterGen {
30 RecordKeeper &Records;
31public:
32 CodeEmitterGen(RecordKeeper &R) : Records(R) {}
33
34 void run(raw_ostream &o);
35private:
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000036 int getVariableBit(const std::string &VarName, BitsInit *BI, int bit);
37 std::string getInstructionCase(Record *R, CodeGenTarget &Target);
38 void AddCodeToMergeInOperand(Record *R, BitsInit *BI,
39 const std::string &VarName,
40 unsigned &NumberedOp,
Hal Finkel5457bd02014-03-13 07:57:54 +000041 std::set<unsigned> &NamedOpIndices,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000042 std::string &Case, CodeGenTarget &Target);
43
44};
45
Jim Laskey10d4b042006-07-13 22:17:08 +000046// If the VarBitInit at position 'bit' matches the specified variable then
47// return the variable bit position. Otherwise return -1.
Dan Gohmanc87c16b2009-12-15 20:21:44 +000048int CodeEmitterGen::getVariableBit(const std::string &VarName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000049 BitsInit *BI, int bit) {
Sean Silvafb509ed2012-10-10 20:24:43 +000050 if (VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit(bit))) {
51 if (VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar()))
Chris Lattner42c4ac42010-11-15 06:42:13 +000052 if (VI->getName() == VarName)
53 return VBI->getBitNum();
Sean Silvafb509ed2012-10-10 20:24:43 +000054 } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit(bit))) {
Owen Anderson715973f2011-04-28 17:51:45 +000055 if (VI->getName() == VarName)
56 return 0;
57 }
Jim Grosbachdaab6602010-10-07 16:56:28 +000058
Jim Laskeya44f6262006-07-13 21:02:53 +000059 return -1;
Jim Grosbachdaab6602010-10-07 16:56:28 +000060}
Jim Laskeya44f6262006-07-13 21:02:53 +000061
Chris Lattnerc19d5102010-11-15 06:59:17 +000062void CodeEmitterGen::
David Greeneaf8ee2c2011-07-29 22:43:06 +000063AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName,
Eric Christopher71520a82011-07-11 23:06:52 +000064 unsigned &NumberedOp,
Hal Finkel5457bd02014-03-13 07:57:54 +000065 std::set<unsigned> &NamedOpIndices,
Chris Lattnerc19d5102010-11-15 06:59:17 +000066 std::string &Case, CodeGenTarget &Target) {
Chris Lattnerc19d5102010-11-15 06:59:17 +000067 CodeGenInstruction &CGI = Target.getInstruction(R);
68
Chris Lattner578c7652010-11-15 07:09:28 +000069 // Determine if VarName actually contributes to the Inst encoding.
70 int bit = BI->getNumBits()-1;
71
72 // Scan for a bit that this contributed to.
73 for (; bit >= 0; ) {
74 if (getVariableBit(VarName, BI, bit) != -1)
75 break;
76
77 --bit;
78 }
79
80 // If we found no bits, ignore this value, otherwise emit the call to get the
81 // operand encoding.
82 if (bit < 0) return;
83
84 // If the operand matches by name, reference according to that
85 // operand number. Non-matching operands are assumed to be in
86 // order.
87 unsigned OpIdx;
88 if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
89 // Get the machine operand number for the indicated operand.
90 OpIdx = CGI.Operands[OpIdx].MIOperandNo;
91 assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) &&
92 "Explicitly used operand also marked as not emitted!");
93 } else {
Evandro Menezes567698a2012-11-09 20:29:37 +000094 unsigned NumberOps = CGI.Operands.size();
Chris Lattner578c7652010-11-15 07:09:28 +000095 /// If this operand is not supposed to be emitted by the
96 /// generated emitter, skip it.
Evandro Menezes567698a2012-11-09 20:29:37 +000097 while (NumberedOp < NumberOps &&
Hal Finkel5457bd02014-03-13 07:57:54 +000098 (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
Alexander Kornienko8c0809c2015-01-15 11:41:30 +000099 (!NamedOpIndices.empty() && NamedOpIndices.count(
Hal Finkel68e03bd2014-03-22 11:33:32 +0000100 CGI.Operands.getSubOperandNumber(NumberedOp).first)))) {
Chris Lattner578c7652010-11-15 07:09:28 +0000101 ++NumberedOp;
Evandro Menezes03789a92012-11-09 21:27:03 +0000102
Hal Finkel68e03bd2014-03-22 11:33:32 +0000103 if (NumberedOp >= CGI.Operands.back().MIOperandNo +
104 CGI.Operands.back().MINumOperands) {
105 errs() << "Too few operands in record " << R->getName() <<
106 " (no match for variable " << VarName << "):\n";
107 errs() << *R;
108 errs() << '\n';
109
110 return;
111 }
112 }
113
Chris Lattner578c7652010-11-15 07:09:28 +0000114 OpIdx = NumberedOp++;
115 }
116
117 std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx);
118 std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName;
119
120 // If the source operand has a custom encoder, use it. This will
121 // get the encoding for all of the suboperands.
122 if (!EncoderMethodName.empty()) {
123 // A custom encoder has all of the information for the
124 // sub-operands, if there are more than one, so only
125 // query the encoder once per source operand.
126 if (SO.second == 0) {
127 Case += " // op: " + VarName + "\n" +
128 " op = " + EncoderMethodName + "(MI, " + utostr(OpIdx);
Eric Christopher79cc1e32014-09-02 22:28:02 +0000129 Case += ", Fixups, STI";
Chris Lattner578c7652010-11-15 07:09:28 +0000130 Case += ");\n";
131 }
132 } else {
133 Case += " // op: " + VarName + "\n" +
134 " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
Eric Christopher79cc1e32014-09-02 22:28:02 +0000135 Case += ", Fixups, STI";
Chris Lattner578c7652010-11-15 07:09:28 +0000136 Case += ");\n";
137 }
138
139 for (; bit >= 0; ) {
Chris Lattnerc19d5102010-11-15 06:59:17 +0000140 int varBit = getVariableBit(VarName, BI, bit);
141
142 // If this bit isn't from a variable, skip it.
143 if (varBit == -1) {
144 --bit;
145 continue;
146 }
147
Bob Wilsonf9bab3a2011-01-27 23:08:52 +0000148 // Figure out the consecutive range of bits covered by this operand, in
Chris Lattnerc19d5102010-11-15 06:59:17 +0000149 // order to generate better encoding code.
150 int beginInstBit = bit;
151 int beginVarBit = varBit;
152 int N = 1;
153 for (--bit; bit >= 0;) {
154 varBit = getVariableBit(VarName, BI, bit);
155 if (varBit == -1 || varBit != (beginVarBit - N)) break;
156 ++N;
157 --bit;
158 }
Chris Lattner578c7652010-11-15 07:09:28 +0000159
NAKAMURA Takumic72fdf42012-03-09 14:52:44 +0000160 uint64_t opMask = ~(uint64_t)0 >> (64-N);
Chris Lattnerc19d5102010-11-15 06:59:17 +0000161 int opShift = beginVarBit - N + 1;
162 opMask <<= opShift;
163 opShift = beginInstBit - beginVarBit;
164
165 if (opShift > 0) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000166 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) << " +
Chris Lattnerc19d5102010-11-15 06:59:17 +0000167 itostr(opShift) + ";\n";
168 } else if (opShift < 0) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000169 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) >> " +
Chris Lattnerc19d5102010-11-15 06:59:17 +0000170 itostr(-opShift) + ";\n";
171 } else {
Owen Andersond845d9d2012-01-24 18:37:29 +0000172 Case += " Value |= op & UINT64_C(" + utostr(opMask) + ");\n";
Chris Lattnerc19d5102010-11-15 06:59:17 +0000173 }
174 }
175}
176
177
178std::string CodeEmitterGen::getInstructionCase(Record *R,
179 CodeGenTarget &Target) {
180 std::string Case;
181
David Greeneaf8ee2c2011-07-29 22:43:06 +0000182 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattnerc19d5102010-11-15 06:59:17 +0000183 const std::vector<RecordVal> &Vals = R->getValues();
184 unsigned NumberedOp = 0;
185
Hal Finkel5457bd02014-03-13 07:57:54 +0000186 std::set<unsigned> NamedOpIndices;
187 // Collect the set of operand indices that might correspond to named
188 // operand, and skip these when assigning operands based on position.
189 if (Target.getInstructionSet()->
190 getValueAsBit("noNamedPositionallyEncodedOperands")) {
191 CodeGenInstruction &CGI = Target.getInstruction(R);
192 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
193 unsigned OpIdx;
194 if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
195 continue;
196
197 NamedOpIndices.insert(OpIdx);
198 }
199 }
200
Chris Lattnerc19d5102010-11-15 06:59:17 +0000201 // Loop over all of the fields in the instruction, determining which are the
202 // operands to the instruction.
203 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
204 // Ignore fixed fields in the record, we're looking for values like:
205 // bits<5> RST = { ?, ?, ?, ?, ? };
206 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
207 continue;
208
Hal Finkel5457bd02014-03-13 07:57:54 +0000209 AddCodeToMergeInOperand(R, BI, Vals[i].getName(), NumberedOp,
210 NamedOpIndices, Case, Target);
Chris Lattnerc19d5102010-11-15 06:59:17 +0000211 }
212
213 std::string PostEmitter = R->getValueAsString("PostEncoderMethod");
David Woodhouse3fa98a62014-01-28 23:13:18 +0000214 if (!PostEmitter.empty()) {
215 Case += " Value = " + PostEmitter + "(MI, Value";
Eric Christopher79cc1e32014-09-02 22:28:02 +0000216 Case += ", STI";
David Woodhouse3fa98a62014-01-28 23:13:18 +0000217 Case += ");\n";
218 }
Chris Lattnerc19d5102010-11-15 06:59:17 +0000219
220 return Case;
221}
222
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000223void CodeEmitterGen::run(raw_ostream &o) {
Chris Lattner77d369c2010-12-13 00:23:57 +0000224 CodeGenTarget Target(Records);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000225 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbachdaab6602010-10-07 16:56:28 +0000226
Jim Laskeya44f6262006-07-13 21:02:53 +0000227 // For little-endian instruction bit encodings, reverse the bit order
Hal Finkel81e6fcc2013-12-17 22:37:50 +0000228 Target.reverseBitsForLittleEndianEncoding();
Jim Grosbachdaab6602010-10-07 16:56:28 +0000229
Chris Lattner918be522010-03-19 00:34:35 +0000230 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
231 Target.getInstructionsByEnumValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000232
Misha Brukman422d0fa2004-08-10 20:54:58 +0000233 // Emit function declaration
Owen Andersond845d9d2012-01-24 18:37:29 +0000234 o << "uint64_t " << Target.getName();
Eric Christopher79cc1e32014-09-02 22:28:02 +0000235 o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
236 << " SmallVectorImpl<MCFixup> &Fixups,\n"
237 << " const MCSubtargetInfo &STI) const {\n";
Misha Brukman422d0fa2004-08-10 20:54:58 +0000238
Jim Laskey23bd4802006-07-12 19:15:43 +0000239 // Emit instruction base values
Owen Anderson773642d2012-03-06 21:48:32 +0000240 o << " static const uint64_t InstBits[] = {\n";
Chris Lattner918be522010-03-19 00:34:35 +0000241 for (std::vector<const CodeGenInstruction*>::const_iterator
Jim Laskey23bd4802006-07-12 19:15:43 +0000242 IN = NumberedInstructions.begin(),
243 EN = NumberedInstructions.end();
244 IN != EN; ++IN) {
245 const CodeGenInstruction *CGI = *IN;
246 Record *R = CGI->TheDef;
Jim Grosbachdaab6602010-10-07 16:56:28 +0000247
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000248 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
249 R->getValueAsBit("isPseudo")) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000250 o << " UINT64_C(0),\n";
Jim Laskey23bd4802006-07-12 19:15:43 +0000251 continue;
252 }
Jim Grosbachdaab6602010-10-07 16:56:28 +0000253
David Greeneaf8ee2c2011-07-29 22:43:06 +0000254 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000255
Chris Lattnerc19d5102010-11-15 06:59:17 +0000256 // Start by filling in fixed values.
Owen Anderson773642d2012-03-06 21:48:32 +0000257 uint64_t Value = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000258 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000259 if (BitInit *B = dyn_cast<BitInit>(BI->getBit(e-i-1)))
Owen Anderson773642d2012-03-06 21:48:32 +0000260 Value |= (uint64_t)B->getValue() << (e-i-1);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000261 }
Owen Andersond845d9d2012-01-24 18:37:29 +0000262 o << " UINT64_C(" << Value << ")," << '\t' << "// " << R->getName() << "\n";
Jim Laskey23bd4802006-07-12 19:15:43 +0000263 }
Owen Andersond845d9d2012-01-24 18:37:29 +0000264 o << " UINT64_C(0)\n };\n";
Jim Grosbachdaab6602010-10-07 16:56:28 +0000265
Jim Laskeya44f6262006-07-13 21:02:53 +0000266 // Map to accumulate all the cases.
267 std::map<std::string, std::vector<std::string> > CaseMap;
Jim Grosbachdaab6602010-10-07 16:56:28 +0000268
Jim Laskeya44f6262006-07-13 21:02:53 +0000269 // Construct all cases statement for each opcode
270 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
271 IC != EC; ++IC) {
272 Record *R = *IC;
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000273 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
274 R->getValueAsBit("isPseudo"))
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000275 continue;
Jim Grosbachcd25b862011-02-03 23:26:36 +0000276 const std::string &InstName = R->getValueAsString("Namespace") + "::"
277 + R->getName();
Chris Lattnerc19d5102010-11-15 06:59:17 +0000278 std::string Case = getInstructionCase(R, Target);
Dan Gohman60a446a2009-04-13 15:38:05 +0000279
Chris Lattnerc19d5102010-11-15 06:59:17 +0000280 CaseMap[Case].push_back(InstName);
Jim Laskeya44f6262006-07-13 21:02:53 +0000281 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000282
Jim Laskeya44f6262006-07-13 21:02:53 +0000283 // Emit initial function code
284 o << " const unsigned opcode = MI.getOpcode();\n"
Owen Anderson773642d2012-03-06 21:48:32 +0000285 << " uint64_t Value = InstBits[opcode];\n"
286 << " uint64_t op = 0;\n"
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000287 << " (void)op; // suppress warning\n"
Jim Laskeya44f6262006-07-13 21:02:53 +0000288 << " switch (opcode) {\n";
289
290 // Emit each case statement
291 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
292 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
293 const std::string &Case = IE->first;
294 std::vector<std::string> &InstList = IE->second;
295
296 for (int i = 0, N = InstList.size(); i < N; i++) {
297 if (i) o << "\n";
Jim Grosbachcd25b862011-02-03 23:26:36 +0000298 o << " case " << InstList[i] << ":";
Jim Laskeya44f6262006-07-13 21:02:53 +0000299 }
300 o << " {\n";
301 o << Case;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000302 o << " break;\n"
303 << " }\n";
304 }
305
Misha Brukman8393c152004-10-14 05:53:01 +0000306 // Default case: unhandled opcode
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000307 o << " default:\n"
Alp Tokere69170a2014-06-26 22:52:05 +0000308 << " std::string msg;\n"
309 << " raw_string_ostream Msg(msg);\n"
Torok Edwinfa040022009-07-08 19:04:27 +0000310 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner2104b8d2010-04-07 22:58:41 +0000311 << " report_fatal_error(Msg.str());\n"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000312 << " }\n"
313 << " return Value;\n"
Misha Brukman8393c152004-10-14 05:53:01 +0000314 << "}\n\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000315}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000316
317} // End anonymous namespace
318
319namespace llvm {
320
321void EmitCodeEmitter(RecordKeeper &RK, raw_ostream &OS) {
322 emitSourceFileHeader("Machine Code Emitter", OS);
323 CodeEmitterGen(RK).run(OS);
324}
325
326} // End llvm namespace