blob: d828dfc25db7b9096f63c086a49c80ae329fb469 [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 Brukman9fff7e12003-05-24 00:15:53 +000016#include "CodeEmitterGen.h"
Misha Brukmand7a5b282004-08-09 19:10:43 +000017#include "CodeGenTarget.h"
Chris Lattnerc9670ef2003-07-31 04:43:49 +000018#include "Record.h"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000019#include "llvm/ADT/StringExtras.h"
Jim Grosbachab3d00e2010-11-02 17:35:25 +000020#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/Debug.h"
Bill Wendlingeac8f352010-12-13 01:05:54 +000022#include <map>
Chris Lattner2082ebe2004-08-01 03:55:39 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Jim Grosbach60aaa762010-11-03 23:38:14 +000025// FIXME: Somewhat hackish to use a command line option for this. There should
26// be a CodeEmitter class in the Target.td that controls this sort of thing
27// instead.
Jim Grosbachab3d00e2010-11-02 17:35:25 +000028static cl::opt<bool>
Jim Grosbach60aaa762010-11-03 23:38:14 +000029MCEmitter("mc-emitter",
Jim Grosbachab3d00e2010-11-02 17:35:25 +000030 cl::desc("Generate CodeEmitter for use with the MC library."),
31 cl::init(false));
32
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000033void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) {
34 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
35 I != E; ++I) {
36 Record *R = *I;
Jim Grosbach806fcc02011-07-06 21:33:38 +000037 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
38 R->getValueAsBit("isPseudo"))
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +000039 continue;
Dan Gohmanf8c73942009-04-13 15:38:05 +000040
Eric Christopherd568b3f2011-07-11 23:06:52 +000041 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman28eefa52004-10-14 05:53:01 +000042
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000043 unsigned numBits = BI->getNumBits();
Eric Christopherd568b3f2011-07-11 23:06:52 +000044 BitsInit *NewBI = new BitsInit(numBits);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000045 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
46 unsigned bitSwapIdx = numBits - bit - 1;
Eric Christopherd568b3f2011-07-11 23:06:52 +000047 Init *OrigBit = BI->getBit(bit);
48 Init *BitSwap = BI->getBit(bitSwapIdx);
49 NewBI->setBit(bit, BitSwap);
50 NewBI->setBit(bitSwapIdx, OrigBit);
Misha Brukman28eefa52004-10-14 05:53:01 +000051 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000052 if (numBits % 2) {
53 unsigned middle = (numBits + 1) / 2;
Eric Christopherd568b3f2011-07-11 23:06:52 +000054 NewBI->setBit(middle, BI->getBit(middle));
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000055 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000056
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000057 // Update the bits in reversed order so that emitInstrOpBits will get the
58 // correct endianness.
59 R->getValue("Inst")->setValue(NewBI);
Misha Brukman28eefa52004-10-14 05:53:01 +000060 }
61}
62
Jim Laskeycb129032006-07-13 22:17:08 +000063// If the VarBitInit at position 'bit' matches the specified variable then
64// return the variable bit position. Otherwise return -1.
Dan Gohman9b03da62009-12-15 20:21:44 +000065int CodeEmitterGen::getVariableBit(const std::string &VarName,
Eric Christopherd568b3f2011-07-11 23:06:52 +000066 BitsInit *BI, int bit) {
67 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
68 if (VarInit *VI = dynamic_cast<VarInit*>(VBI->getVariable()))
Chris Lattner98e969a2010-11-15 06:42:13 +000069 if (VI->getName() == VarName)
70 return VBI->getBitNum();
Eric Christopherd568b3f2011-07-11 23:06:52 +000071 } else if (VarInit *VI = dynamic_cast<VarInit*>(BI->getBit(bit))) {
Owen Anderson4cdcb472011-04-28 17:51:45 +000072 if (VI->getName() == VarName)
73 return 0;
74 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000075
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000076 return -1;
Jim Grosbach8b892ae2010-10-07 16:56:28 +000077}
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000078
Chris Lattner16201172010-11-15 06:59:17 +000079void CodeEmitterGen::
Eric Christopherd568b3f2011-07-11 23:06:52 +000080AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName,
81 unsigned &NumberedOp,
Chris Lattner16201172010-11-15 06:59:17 +000082 std::string &Case, CodeGenTarget &Target) {
Chris Lattner16201172010-11-15 06:59:17 +000083 CodeGenInstruction &CGI = Target.getInstruction(R);
84
Chris Lattner8ae082b2010-11-15 07:09:28 +000085 // Determine if VarName actually contributes to the Inst encoding.
86 int bit = BI->getNumBits()-1;
87
88 // Scan for a bit that this contributed to.
89 for (; bit >= 0; ) {
90 if (getVariableBit(VarName, BI, bit) != -1)
91 break;
92
93 --bit;
94 }
95
96 // If we found no bits, ignore this value, otherwise emit the call to get the
97 // operand encoding.
98 if (bit < 0) return;
99
100 // If the operand matches by name, reference according to that
101 // operand number. Non-matching operands are assumed to be in
102 // order.
103 unsigned OpIdx;
104 if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
105 // Get the machine operand number for the indicated operand.
106 OpIdx = CGI.Operands[OpIdx].MIOperandNo;
107 assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) &&
108 "Explicitly used operand also marked as not emitted!");
109 } else {
110 /// If this operand is not supposed to be emitted by the
111 /// generated emitter, skip it.
112 while (CGI.Operands.isFlatOperandNotEmitted(NumberedOp))
113 ++NumberedOp;
114 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);
129 if (MCEmitter)
130 Case += ", Fixups";
131 Case += ");\n";
132 }
133 } else {
134 Case += " // op: " + VarName + "\n" +
135 " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
136 if (MCEmitter)
137 Case += ", Fixups";
138 Case += ");\n";
139 }
140
141 for (; bit >= 0; ) {
Chris Lattner16201172010-11-15 06:59:17 +0000142 int varBit = getVariableBit(VarName, BI, bit);
143
144 // If this bit isn't from a variable, skip it.
145 if (varBit == -1) {
146 --bit;
147 continue;
148 }
149
Bob Wilson9b8c3532011-01-27 23:08:52 +0000150 // Figure out the consecutive range of bits covered by this operand, in
Chris Lattner16201172010-11-15 06:59:17 +0000151 // order to generate better encoding code.
152 int beginInstBit = bit;
153 int beginVarBit = varBit;
154 int N = 1;
155 for (--bit; bit >= 0;) {
156 varBit = getVariableBit(VarName, BI, bit);
157 if (varBit == -1 || varBit != (beginVarBit - N)) break;
158 ++N;
159 --bit;
160 }
Chris Lattner8ae082b2010-11-15 07:09:28 +0000161
Chris Lattner16201172010-11-15 06:59:17 +0000162 unsigned opMask = ~0U >> (32-N);
163 int opShift = beginVarBit - N + 1;
164 opMask <<= opShift;
165 opShift = beginInstBit - beginVarBit;
166
167 if (opShift > 0) {
168 Case += " Value |= (op & " + utostr(opMask) + "U) << " +
169 itostr(opShift) + ";\n";
170 } else if (opShift < 0) {
171 Case += " Value |= (op & " + utostr(opMask) + "U) >> " +
172 itostr(-opShift) + ";\n";
173 } else {
174 Case += " Value |= op & " + utostr(opMask) + "U;\n";
175 }
176 }
177}
178
179
180std::string CodeEmitterGen::getInstructionCase(Record *R,
181 CodeGenTarget &Target) {
182 std::string Case;
183
Eric Christopherd568b3f2011-07-11 23:06:52 +0000184 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattner16201172010-11-15 06:59:17 +0000185 const std::vector<RecordVal> &Vals = R->getValues();
186 unsigned NumberedOp = 0;
187
188 // Loop over all of the fields in the instruction, determining which are the
189 // operands to the instruction.
190 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
191 // Ignore fixed fields in the record, we're looking for values like:
192 // bits<5> RST = { ?, ?, ?, ?, ? };
193 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
194 continue;
195
196 AddCodeToMergeInOperand(R, BI, Vals[i].getName(), NumberedOp, Case, Target);
197 }
198
199 std::string PostEmitter = R->getValueAsString("PostEncoderMethod");
200 if (!PostEmitter.empty())
201 Case += " Value = " + PostEmitter + "(MI, Value);\n";
202
203 return Case;
204}
205
Daniel Dunbar1a551802009-07-03 00:10:29 +0000206void CodeEmitterGen::run(raw_ostream &o) {
Chris Lattner67db8832010-12-13 00:23:57 +0000207 CodeGenTarget Target(Records);
Chris Lattner048c00d2003-08-01 04:38:18 +0000208 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000209
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000210 // For little-endian instruction bit encodings, reverse the bit order
211 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
Misha Brukman9fff7e12003-05-24 00:15:53 +0000212
Chris Lattner0e5e49e2003-08-06 04:36:35 +0000213 EmitSourceFileHeader("Machine Code Emitter", o);
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000214
Chris Lattnerf6502782010-03-19 00:34:35 +0000215 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
216 Target.getInstructionsByEnumValue();
Misha Brukman9fff7e12003-05-24 00:15:53 +0000217
Misha Brukmanad346ad2004-08-10 20:54:58 +0000218 // Emit function declaration
Jim Grosbach60aaa762010-11-03 23:38:14 +0000219 o << "unsigned " << Target.getName();
220 if (MCEmitter)
221 o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
222 << " SmallVectorImpl<MCFixup> &Fixups) const {\n";
223 else
224 o << "CodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const {\n";
Misha Brukmanad346ad2004-08-10 20:54:58 +0000225
Jim Laskeyed393432006-07-12 19:15:43 +0000226 // Emit instruction base values
227 o << " static const unsigned InstBits[] = {\n";
Chris Lattnerf6502782010-03-19 00:34:35 +0000228 for (std::vector<const CodeGenInstruction*>::const_iterator
Jim Laskeyed393432006-07-12 19:15:43 +0000229 IN = NumberedInstructions.begin(),
230 EN = NumberedInstructions.end();
231 IN != EN; ++IN) {
232 const CodeGenInstruction *CGI = *IN;
233 Record *R = CGI->TheDef;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000234
Jim Grosbach806fcc02011-07-06 21:33:38 +0000235 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
236 R->getValueAsBit("isPseudo")) {
Evan Chengbc95b232008-09-17 06:29:52 +0000237 o << " 0U,\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000238 continue;
239 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000240
Eric Christopherd568b3f2011-07-11 23:06:52 +0000241 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000242
Chris Lattner16201172010-11-15 06:59:17 +0000243 // Start by filling in fixed values.
Reid Spencerfa3e3b92006-11-03 01:48:30 +0000244 unsigned Value = 0;
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000245 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
Eric Christopherd568b3f2011-07-11 23:06:52 +0000246 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1)))
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000247 Value |= B->getValue() << (e-i-1);
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000248 }
Evan Chengbc95b232008-09-17 06:29:52 +0000249 o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000250 }
Evan Chengbc95b232008-09-17 06:29:52 +0000251 o << " 0U\n };\n";
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000252
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000253 // Map to accumulate all the cases.
254 std::map<std::string, std::vector<std::string> > CaseMap;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000255
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000256 // Construct all cases statement for each opcode
257 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
258 IC != EC; ++IC) {
259 Record *R = *IC;
Jim Grosbach806fcc02011-07-06 21:33:38 +0000260 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
261 R->getValueAsBit("isPseudo"))
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000262 continue;
Jim Grosbach0ed92f22011-02-03 23:26:36 +0000263 const std::string &InstName = R->getValueAsString("Namespace") + "::"
264 + R->getName();
Chris Lattner16201172010-11-15 06:59:17 +0000265 std::string Case = getInstructionCase(R, Target);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000266
Chris Lattner16201172010-11-15 06:59:17 +0000267 CaseMap[Case].push_back(InstName);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000268 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000269
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000270 // Emit initial function code
271 o << " const unsigned opcode = MI.getOpcode();\n"
272 << " unsigned Value = InstBits[opcode];\n"
Evan Chenge3e36262008-09-07 09:00:57 +0000273 << " unsigned op = 0;\n"
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000274 << " (void)op; // suppress warning\n"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000275 << " switch (opcode) {\n";
276
277 // Emit each case statement
278 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
279 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
280 const std::string &Case = IE->first;
281 std::vector<std::string> &InstList = IE->second;
282
283 for (int i = 0, N = InstList.size(); i < N; i++) {
284 if (i) o << "\n";
Jim Grosbach0ed92f22011-02-03 23:26:36 +0000285 o << " case " << InstList[i] << ":";
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000286 }
287 o << " {\n";
288 o << Case;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000289 o << " break;\n"
290 << " }\n";
291 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000292
Misha Brukman28eefa52004-10-14 05:53:01 +0000293 // Default case: unhandled opcode
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000294 o << " default:\n"
Torok Edwin804e0fe2009-07-08 19:04:27 +0000295 << " std::string msg;\n"
296 << " raw_string_ostream Msg(msg);\n"
297 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner75361b62010-04-07 22:58:41 +0000298 << " report_fatal_error(Msg.str());\n"
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000299 << " }\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +0000300 << " return Value;\n"
Misha Brukman28eefa52004-10-14 05:53:01 +0000301 << "}\n\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000302}