blob: 9d0f3fdbae97d7e0779c2a88169adb9bc25fabef [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;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +000037 if (R->getValueAsString("Namespace") == "TargetOpcode")
38 continue;
Dan Gohmanf8c73942009-04-13 15:38:05 +000039
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000040 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman28eefa52004-10-14 05:53:01 +000041
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000042 unsigned numBits = BI->getNumBits();
43 BitsInit *NewBI = new BitsInit(numBits);
44 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
45 unsigned bitSwapIdx = numBits - bit - 1;
46 Init *OrigBit = BI->getBit(bit);
47 Init *BitSwap = BI->getBit(bitSwapIdx);
48 NewBI->setBit(bit, BitSwap);
49 NewBI->setBit(bitSwapIdx, OrigBit);
Misha Brukman28eefa52004-10-14 05:53:01 +000050 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000051 if (numBits % 2) {
52 unsigned middle = (numBits + 1) / 2;
53 NewBI->setBit(middle, BI->getBit(middle));
54 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000055
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000056 // Update the bits in reversed order so that emitInstrOpBits will get the
57 // correct endianness.
58 R->getValue("Inst")->setValue(NewBI);
Misha Brukman28eefa52004-10-14 05:53:01 +000059 }
60}
61
Jim Laskeycb129032006-07-13 22:17:08 +000062// If the VarBitInit at position 'bit' matches the specified variable then
63// return the variable bit position. Otherwise return -1.
Dan Gohman9b03da62009-12-15 20:21:44 +000064int CodeEmitterGen::getVariableBit(const std::string &VarName,
Chris Lattner16201172010-11-15 06:59:17 +000065 BitsInit *BI, int bit) {
Chris Lattner98e969a2010-11-15 06:42:13 +000066 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit)))
67 if (VarInit *VI = dynamic_cast<VarInit*>(VBI->getVariable()))
68 if (VI->getName() == VarName)
69 return VBI->getBitNum();
Jim Grosbach8b892ae2010-10-07 16:56:28 +000070
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000071 return -1;
Jim Grosbach8b892ae2010-10-07 16:56:28 +000072}
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000073
Chris Lattner16201172010-11-15 06:59:17 +000074void CodeEmitterGen::
75AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName,
76 unsigned &NumberedOp,
77 std::string &Case, CodeGenTarget &Target) {
Chris Lattner16201172010-11-15 06:59:17 +000078 CodeGenInstruction &CGI = Target.getInstruction(R);
79
Chris Lattner8ae082b2010-11-15 07:09:28 +000080 // Determine if VarName actually contributes to the Inst encoding.
81 int bit = BI->getNumBits()-1;
82
83 // Scan for a bit that this contributed to.
84 for (; bit >= 0; ) {
85 if (getVariableBit(VarName, BI, bit) != -1)
86 break;
87
88 --bit;
89 }
90
91 // If we found no bits, ignore this value, otherwise emit the call to get the
92 // operand encoding.
93 if (bit < 0) return;
94
95 // If the operand matches by name, reference according to that
96 // operand number. Non-matching operands are assumed to be in
97 // order.
98 unsigned OpIdx;
99 if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
100 // Get the machine operand number for the indicated operand.
101 OpIdx = CGI.Operands[OpIdx].MIOperandNo;
102 assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) &&
103 "Explicitly used operand also marked as not emitted!");
104 } else {
105 /// If this operand is not supposed to be emitted by the
106 /// generated emitter, skip it.
107 while (CGI.Operands.isFlatOperandNotEmitted(NumberedOp))
108 ++NumberedOp;
109 OpIdx = NumberedOp++;
110 }
111
112 std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx);
113 std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName;
114
115 // If the source operand has a custom encoder, use it. This will
116 // get the encoding for all of the suboperands.
117 if (!EncoderMethodName.empty()) {
118 // A custom encoder has all of the information for the
119 // sub-operands, if there are more than one, so only
120 // query the encoder once per source operand.
121 if (SO.second == 0) {
122 Case += " // op: " + VarName + "\n" +
123 " op = " + EncoderMethodName + "(MI, " + utostr(OpIdx);
124 if (MCEmitter)
125 Case += ", Fixups";
126 Case += ");\n";
127 }
128 } else {
129 Case += " // op: " + VarName + "\n" +
130 " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
131 if (MCEmitter)
132 Case += ", Fixups";
133 Case += ");\n";
134 }
135
136 for (; bit >= 0; ) {
Chris Lattner16201172010-11-15 06:59:17 +0000137 int varBit = getVariableBit(VarName, BI, bit);
138
139 // If this bit isn't from a variable, skip it.
140 if (varBit == -1) {
141 --bit;
142 continue;
143 }
144
145 // Figure out the consequtive range of bits covered by this operand, in
146 // order to generate better encoding code.
147 int beginInstBit = bit;
148 int beginVarBit = varBit;
149 int N = 1;
150 for (--bit; bit >= 0;) {
151 varBit = getVariableBit(VarName, BI, bit);
152 if (varBit == -1 || varBit != (beginVarBit - N)) break;
153 ++N;
154 --bit;
155 }
Chris Lattner8ae082b2010-11-15 07:09:28 +0000156
Chris Lattner16201172010-11-15 06:59:17 +0000157 unsigned opMask = ~0U >> (32-N);
158 int opShift = beginVarBit - N + 1;
159 opMask <<= opShift;
160 opShift = beginInstBit - beginVarBit;
161
162 if (opShift > 0) {
163 Case += " Value |= (op & " + utostr(opMask) + "U) << " +
164 itostr(opShift) + ";\n";
165 } else if (opShift < 0) {
166 Case += " Value |= (op & " + utostr(opMask) + "U) >> " +
167 itostr(-opShift) + ";\n";
168 } else {
169 Case += " Value |= op & " + utostr(opMask) + "U;\n";
170 }
171 }
172}
173
174
175std::string CodeEmitterGen::getInstructionCase(Record *R,
176 CodeGenTarget &Target) {
177 std::string Case;
178
179 BitsInit *BI = R->getValueAsBitsInit("Inst");
180 const std::vector<RecordVal> &Vals = R->getValues();
181 unsigned NumberedOp = 0;
182
183 // Loop over all of the fields in the instruction, determining which are the
184 // operands to the instruction.
185 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
186 // Ignore fixed fields in the record, we're looking for values like:
187 // bits<5> RST = { ?, ?, ?, ?, ? };
188 if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete())
189 continue;
190
191 AddCodeToMergeInOperand(R, BI, Vals[i].getName(), NumberedOp, Case, Target);
192 }
193
194 std::string PostEmitter = R->getValueAsString("PostEncoderMethod");
195 if (!PostEmitter.empty())
196 Case += " Value = " + PostEmitter + "(MI, Value);\n";
197
198 return Case;
199}
200
Daniel Dunbar1a551802009-07-03 00:10:29 +0000201void CodeEmitterGen::run(raw_ostream &o) {
Chris Lattner67db8832010-12-13 00:23:57 +0000202 CodeGenTarget Target(Records);
Chris Lattner048c00d2003-08-01 04:38:18 +0000203 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000204
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000205 // For little-endian instruction bit encodings, reverse the bit order
206 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
Misha Brukman9fff7e12003-05-24 00:15:53 +0000207
Chris Lattner0e5e49e2003-08-06 04:36:35 +0000208 EmitSourceFileHeader("Machine Code Emitter", o);
Misha Brukmane2ba7782004-08-10 18:31:01 +0000209 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000210
Chris Lattnerf6502782010-03-19 00:34:35 +0000211 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
212 Target.getInstructionsByEnumValue();
Misha Brukman9fff7e12003-05-24 00:15:53 +0000213
Misha Brukmanad346ad2004-08-10 20:54:58 +0000214 // Emit function declaration
Jim Grosbach60aaa762010-11-03 23:38:14 +0000215 o << "unsigned " << Target.getName();
216 if (MCEmitter)
217 o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
218 << " SmallVectorImpl<MCFixup> &Fixups) const {\n";
219 else
220 o << "CodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const {\n";
Misha Brukmanad346ad2004-08-10 20:54:58 +0000221
Jim Laskeyed393432006-07-12 19:15:43 +0000222 // Emit instruction base values
223 o << " static const unsigned InstBits[] = {\n";
Chris Lattnerf6502782010-03-19 00:34:35 +0000224 for (std::vector<const CodeGenInstruction*>::const_iterator
Jim Laskeyed393432006-07-12 19:15:43 +0000225 IN = NumberedInstructions.begin(),
226 EN = NumberedInstructions.end();
227 IN != EN; ++IN) {
228 const CodeGenInstruction *CGI = *IN;
229 Record *R = CGI->TheDef;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000230
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000231 if (R->getValueAsString("Namespace") == "TargetOpcode") {
Evan Chengbc95b232008-09-17 06:29:52 +0000232 o << " 0U,\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000233 continue;
234 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000235
Chris Lattner6f334ad2003-08-01 04:46:24 +0000236 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000237
Chris Lattner16201172010-11-15 06:59:17 +0000238 // Start by filling in fixed values.
Reid Spencerfa3e3b92006-11-03 01:48:30 +0000239 unsigned Value = 0;
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000240 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
Chris Lattner16201172010-11-15 06:59:17 +0000241 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1)))
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000242 Value |= B->getValue() << (e-i-1);
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000243 }
Evan Chengbc95b232008-09-17 06:29:52 +0000244 o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000245 }
Evan Chengbc95b232008-09-17 06:29:52 +0000246 o << " 0U\n };\n";
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000247
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000248 // Map to accumulate all the cases.
249 std::map<std::string, std::vector<std::string> > CaseMap;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000250
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000251 // Construct all cases statement for each opcode
252 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
253 IC != EC; ++IC) {
254 Record *R = *IC;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000255 if (R->getValueAsString("Namespace") == "TargetOpcode")
256 continue;
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000257 const std::string &InstName = R->getName();
Chris Lattner16201172010-11-15 06:59:17 +0000258 std::string Case = getInstructionCase(R, Target);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000259
Chris Lattner16201172010-11-15 06:59:17 +0000260 CaseMap[Case].push_back(InstName);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000261 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000262
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000263 // Emit initial function code
264 o << " const unsigned opcode = MI.getOpcode();\n"
265 << " unsigned Value = InstBits[opcode];\n"
Evan Chenge3e36262008-09-07 09:00:57 +0000266 << " unsigned op = 0;\n"
Evan Chengacff3392008-09-02 06:51:36 +0000267 << " op = op; // suppress warning\n"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000268 << " switch (opcode) {\n";
269
270 // Emit each case statement
271 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
272 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
273 const std::string &Case = IE->first;
274 std::vector<std::string> &InstList = IE->second;
275
276 for (int i = 0, N = InstList.size(); i < N; i++) {
277 if (i) o << "\n";
278 o << " case " << Namespace << InstList[i] << ":";
279 }
280 o << " {\n";
281 o << Case;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000282 o << " break;\n"
283 << " }\n";
284 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000285
Misha Brukman28eefa52004-10-14 05:53:01 +0000286 // Default case: unhandled opcode
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000287 o << " default:\n"
Torok Edwin804e0fe2009-07-08 19:04:27 +0000288 << " std::string msg;\n"
289 << " raw_string_ostream Msg(msg);\n"
290 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner75361b62010-04-07 22:58:41 +0000291 << " report_fatal_error(Msg.str());\n"
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000292 << " }\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +0000293 << " return Value;\n"
Misha Brukman28eefa52004-10-14 05:53:01 +0000294 << "}\n\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000295}