blob: e86c18fb66382c8ef50c20a37299f35af5fe2684 [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"
Chris Lattner2082ebe2004-08-01 03:55:39 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Jim Grosbach60aaa762010-11-03 23:38:14 +000024// FIXME: Somewhat hackish to use a command line option for this. There should
25// be a CodeEmitter class in the Target.td that controls this sort of thing
26// instead.
Jim Grosbachab3d00e2010-11-02 17:35:25 +000027static cl::opt<bool>
Jim Grosbach60aaa762010-11-03 23:38:14 +000028MCEmitter("mc-emitter",
Jim Grosbachab3d00e2010-11-02 17:35:25 +000029 cl::desc("Generate CodeEmitter for use with the MC library."),
30 cl::init(false));
31
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000032void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) {
33 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
34 I != E; ++I) {
35 Record *R = *I;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +000036 if (R->getValueAsString("Namespace") == "TargetOpcode")
37 continue;
Dan Gohmanf8c73942009-04-13 15:38:05 +000038
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000039 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman28eefa52004-10-14 05:53:01 +000040
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000041 unsigned numBits = BI->getNumBits();
42 BitsInit *NewBI = new BitsInit(numBits);
43 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
44 unsigned bitSwapIdx = numBits - bit - 1;
45 Init *OrigBit = BI->getBit(bit);
46 Init *BitSwap = BI->getBit(bitSwapIdx);
47 NewBI->setBit(bit, BitSwap);
48 NewBI->setBit(bitSwapIdx, OrigBit);
Misha Brukman28eefa52004-10-14 05:53:01 +000049 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000050 if (numBits % 2) {
51 unsigned middle = (numBits + 1) / 2;
52 NewBI->setBit(middle, BI->getBit(middle));
53 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000054
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000055 // Update the bits in reversed order so that emitInstrOpBits will get the
56 // correct endianness.
57 R->getValue("Inst")->setValue(NewBI);
Misha Brukman28eefa52004-10-14 05:53:01 +000058 }
59}
60
Jim Laskeycb129032006-07-13 22:17:08 +000061// If the VarBitInit at position 'bit' matches the specified variable then
62// return the variable bit position. Otherwise return -1.
Dan Gohman9b03da62009-12-15 20:21:44 +000063int CodeEmitterGen::getVariableBit(const std::string &VarName,
Jim Laskeycb129032006-07-13 22:17:08 +000064 BitsInit *BI, int bit) {
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000065 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
66 TypedInit *TI = VBI->getVariable();
Jim Grosbach8b892ae2010-10-07 16:56:28 +000067
Dan Gohman9b03da62009-12-15 20:21:44 +000068 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
69 if (VI->getName() == VarName) return VBI->getBitNum();
70 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000071 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000072
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000073 return -1;
Jim Grosbach8b892ae2010-10-07 16:56:28 +000074}
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000075
Daniel Dunbar1a551802009-07-03 00:10:29 +000076void CodeEmitterGen::run(raw_ostream &o) {
Misha Brukmane2ba7782004-08-10 18:31:01 +000077 CodeGenTarget Target;
Chris Lattner048c00d2003-08-01 04:38:18 +000078 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbach8b892ae2010-10-07 16:56:28 +000079
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000080 // For little-endian instruction bit encodings, reverse the bit order
81 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
Misha Brukman9fff7e12003-05-24 00:15:53 +000082
Chris Lattner0e5e49e2003-08-06 04:36:35 +000083 EmitSourceFileHeader("Machine Code Emitter", o);
Misha Brukmane2ba7782004-08-10 18:31:01 +000084 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
Jim Grosbach8b892ae2010-10-07 16:56:28 +000085
Chris Lattnerf6502782010-03-19 00:34:35 +000086 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
87 Target.getInstructionsByEnumValue();
Misha Brukman9fff7e12003-05-24 00:15:53 +000088
Misha Brukmanad346ad2004-08-10 20:54:58 +000089 // Emit function declaration
Jim Grosbach60aaa762010-11-03 23:38:14 +000090 o << "unsigned " << Target.getName();
91 if (MCEmitter)
92 o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
93 << " SmallVectorImpl<MCFixup> &Fixups) const {\n";
94 else
95 o << "CodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const {\n";
Misha Brukmanad346ad2004-08-10 20:54:58 +000096
Jim Laskeyed393432006-07-12 19:15:43 +000097 // Emit instruction base values
98 o << " static const unsigned InstBits[] = {\n";
Chris Lattnerf6502782010-03-19 00:34:35 +000099 for (std::vector<const CodeGenInstruction*>::const_iterator
Jim Laskeyed393432006-07-12 19:15:43 +0000100 IN = NumberedInstructions.begin(),
101 EN = NumberedInstructions.end();
102 IN != EN; ++IN) {
103 const CodeGenInstruction *CGI = *IN;
104 Record *R = CGI->TheDef;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000105
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000106 if (R->getValueAsString("Namespace") == "TargetOpcode") {
Evan Chengbc95b232008-09-17 06:29:52 +0000107 o << " 0U,\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000108 continue;
109 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000110
Chris Lattner6f334ad2003-08-01 04:46:24 +0000111 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000112
Misha Brukman9fff7e12003-05-24 00:15:53 +0000113 // Start by filling in fixed values...
Reid Spencerfa3e3b92006-11-03 01:48:30 +0000114 unsigned Value = 0;
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000115 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
116 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
117 Value |= B->getValue() << (e-i-1);
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000118 }
119 }
Evan Chengbc95b232008-09-17 06:29:52 +0000120 o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000121 }
Evan Chengbc95b232008-09-17 06:29:52 +0000122 o << " 0U\n };\n";
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000123
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000124 // Map to accumulate all the cases.
125 std::map<std::string, std::vector<std::string> > CaseMap;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000126
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000127 // Construct all cases statement for each opcode
128 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
129 IC != EC; ++IC) {
130 Record *R = *IC;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000131 if (R->getValueAsString("Namespace") == "TargetOpcode")
132 continue;
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000133 const std::string &InstName = R->getName();
134 std::string Case("");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000135
Jim Laskeyed393432006-07-12 19:15:43 +0000136 BitsInit *BI = R->getValueAsBitsInit("Inst");
137 const std::vector<RecordVal> &Vals = R->getValues();
Chris Lattnerf30187a2010-03-19 00:07:20 +0000138 CodeGenInstruction &CGI = Target.getInstruction(R);
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000139
Misha Brukman28eefa52004-10-14 05:53:01 +0000140 // Loop over all of the fields in the instruction, determining which are the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000141 // operands to the instruction.
Jim Grosbach01855072010-10-11 18:25:51 +0000142 unsigned NumberedOp = 0;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000143 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Misha Brukman28eefa52004-10-14 05:53:01 +0000144 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
Misha Brukman7eac4762003-07-15 21:00:32 +0000145 // Is the operand continuous? If so, we can just mask and OR it in
Misha Brukman3da94ae2005-04-22 00:00:37 +0000146 // instead of doing it bit-by-bit, saving a lot in runtime cost.
Dan Gohman9b03da62009-12-15 20:21:44 +0000147 const std::string &VarName = Vals[i].getName();
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000148 bool gotOp = false;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000149
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000150 for (int bit = BI->getNumBits()-1; bit >= 0; ) {
Dan Gohman9b03da62009-12-15 20:21:44 +0000151 int varBit = getVariableBit(VarName, BI, bit);
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000152
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000153 if (varBit == -1) {
154 --bit;
155 } else {
156 int beginInstBit = bit;
157 int beginVarBit = varBit;
158 int N = 1;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000159
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000160 for (--bit; bit >= 0;) {
Dan Gohman9b03da62009-12-15 20:21:44 +0000161 varBit = getVariableBit(VarName, BI, bit);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000162 if (varBit == -1 || varBit != (beginVarBit - N)) break;
163 ++N;
164 --bit;
Chris Lattnerd7efef92003-08-05 03:53:04 +0000165 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000166
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000167 if (!gotOp) {
Jim Grosbach01855072010-10-11 18:25:51 +0000168 // If the operand matches by name, reference according to that
169 // operand number. Non-matching operands are assumed to be in
170 // order.
171 unsigned OpIdx;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000172 if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
Jim Grosbach1a7233f2010-10-11 21:31:22 +0000173 // Get the machine operand number for the indicated operand.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000174 OpIdx = CGI.Operands[OpIdx].MIOperandNo;
175 assert (!CGI.Operands.isFlatOperandNotEmitted(OpIdx) &&
Jim Grosbacha1e21942010-10-11 21:41:31 +0000176 "Explicitly used operand also marked as not emitted!");
Jim Grosbach01855072010-10-11 18:25:51 +0000177 } else {
178 /// If this operand is not supposed to be emitted by the
179 /// generated emitter, skip it.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000180 while (CGI.Operands.isFlatOperandNotEmitted(NumberedOp))
Jim Grosbach01855072010-10-11 18:25:51 +0000181 ++NumberedOp;
182 OpIdx = NumberedOp++;
183 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000184 std::pair<unsigned, unsigned> SO =
185 CGI.Operands.getSubOperandNumber(OpIdx);
Jim Grosbach5013f742010-10-12 22:21:57 +0000186 std::string &EncoderMethodName =
Chris Lattnerc240bb02010-11-01 04:03:32 +0000187 CGI.Operands[SO.first].EncoderMethodName;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000188
Jim Grosbach5013f742010-10-12 22:21:57 +0000189 // If the source operand has a custom encoder, use it. This will
190 // get the encoding for all of the suboperands.
191 if (!EncoderMethodName.empty()) {
192 // A custom encoder has all of the information for the
193 // sub-operands, if there are more than one, so only
194 // query the encoder once per source operand.
195 if (SO.second == 0) {
196 Case += " // op: " + VarName + "\n"
197 + " op = " + EncoderMethodName + "(MI, "
Jim Grosbach60aaa762010-11-03 23:38:14 +0000198 + utostr(OpIdx);
199 if (MCEmitter)
200 Case += ", Fixups";
201 Case += ");\n";
Jim Grosbach5013f742010-10-12 22:21:57 +0000202 }
203 } else {
204 Case += " // op: " + VarName + "\n"
205 + " op = getMachineOpValue(MI, MI.getOperand("
Jim Grosbach60aaa762010-11-03 23:38:14 +0000206 + utostr(OpIdx) + ")";
207 if (MCEmitter)
208 Case += ", Fixups";
209 Case += ");\n";
Jim Grosbach5013f742010-10-12 22:21:57 +0000210 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000211 gotOp = true;
212 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000213
Chris Lattner1cfa0772008-10-05 18:31:58 +0000214 unsigned opMask = ~0U >> (32-N);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000215 int opShift = beginVarBit - N + 1;
216 opMask <<= opShift;
217 opShift = beginInstBit - beginVarBit;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000218
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000219 if (opShift > 0) {
220 Case += " Value |= (op & " + utostr(opMask) + "U) << "
221 + itostr(opShift) + ";\n";
222 } else if (opShift < 0) {
223 Case += " Value |= (op & " + utostr(opMask) + "U) >> "
224 + itostr(-opShift) + ";\n";
225 } else {
226 Case += " Value |= op & " + utostr(opMask) + "U;\n";
227 }
Chris Lattnerd7efef92003-08-05 03:53:04 +0000228 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000229 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000230 }
231 }
232
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000233 std::vector<std::string> &InstList = CaseMap[Case];
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000234 InstList.push_back(InstName);
235 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000236
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000237 // Emit initial function code
238 o << " const unsigned opcode = MI.getOpcode();\n"
239 << " unsigned Value = InstBits[opcode];\n"
Evan Chenge3e36262008-09-07 09:00:57 +0000240 << " unsigned op = 0;\n"
Evan Chengacff3392008-09-02 06:51:36 +0000241 << " op = op; // suppress warning\n"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000242 << " switch (opcode) {\n";
243
244 // Emit each case statement
245 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
246 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
247 const std::string &Case = IE->first;
248 std::vector<std::string> &InstList = IE->second;
249
250 for (int i = 0, N = InstList.size(); i < N; i++) {
251 if (i) o << "\n";
252 o << " case " << Namespace << InstList[i] << ":";
253 }
254 o << " {\n";
255 o << Case;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000256 o << " break;\n"
257 << " }\n";
258 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000259
Misha Brukman28eefa52004-10-14 05:53:01 +0000260 // Default case: unhandled opcode
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000261 o << " default:\n"
Torok Edwin804e0fe2009-07-08 19:04:27 +0000262 << " std::string msg;\n"
263 << " raw_string_ostream Msg(msg);\n"
264 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner75361b62010-04-07 22:58:41 +0000265 << " report_fatal_error(Msg.str());\n"
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000266 << " }\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +0000267 << " return Value;\n"
Misha Brukman28eefa52004-10-14 05:53:01 +0000268 << "}\n\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000269}