blob: fbe9947b096ebe965ed2da7c5f5eb4dc3a8a3a83 [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 Grosbachab3d00e2010-11-02 17:35:25 +000024static cl::opt<bool>
25MCEmitter("mc-code-emitter",
26 cl::desc("Generate CodeEmitter for use with the MC library."),
27 cl::init(false));
28
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000029void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) {
30 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
31 I != E; ++I) {
32 Record *R = *I;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +000033 if (R->getValueAsString("Namespace") == "TargetOpcode")
34 continue;
Dan Gohmanf8c73942009-04-13 15:38:05 +000035
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000036 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman28eefa52004-10-14 05:53:01 +000037
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000038 unsigned numBits = BI->getNumBits();
39 BitsInit *NewBI = new BitsInit(numBits);
40 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
41 unsigned bitSwapIdx = numBits - bit - 1;
42 Init *OrigBit = BI->getBit(bit);
43 Init *BitSwap = BI->getBit(bitSwapIdx);
44 NewBI->setBit(bit, BitSwap);
45 NewBI->setBit(bitSwapIdx, OrigBit);
Misha Brukman28eefa52004-10-14 05:53:01 +000046 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000047 if (numBits % 2) {
48 unsigned middle = (numBits + 1) / 2;
49 NewBI->setBit(middle, BI->getBit(middle));
50 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000051
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000052 // Update the bits in reversed order so that emitInstrOpBits will get the
53 // correct endianness.
54 R->getValue("Inst")->setValue(NewBI);
Misha Brukman28eefa52004-10-14 05:53:01 +000055 }
56}
57
Jim Laskeycb129032006-07-13 22:17:08 +000058// If the VarBitInit at position 'bit' matches the specified variable then
59// return the variable bit position. Otherwise return -1.
Dan Gohman9b03da62009-12-15 20:21:44 +000060int CodeEmitterGen::getVariableBit(const std::string &VarName,
Jim Laskeycb129032006-07-13 22:17:08 +000061 BitsInit *BI, int bit) {
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000062 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
63 TypedInit *TI = VBI->getVariable();
Jim Grosbach8b892ae2010-10-07 16:56:28 +000064
Dan Gohman9b03da62009-12-15 20:21:44 +000065 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
66 if (VI->getName() == VarName) return VBI->getBitNum();
67 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000068 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000069
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000070 return -1;
Jim Grosbach8b892ae2010-10-07 16:56:28 +000071}
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000072
Daniel Dunbar1a551802009-07-03 00:10:29 +000073void CodeEmitterGen::run(raw_ostream &o) {
Misha Brukmane2ba7782004-08-10 18:31:01 +000074 CodeGenTarget Target;
Chris Lattner048c00d2003-08-01 04:38:18 +000075 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbach8b892ae2010-10-07 16:56:28 +000076
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000077 // For little-endian instruction bit encodings, reverse the bit order
78 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
Misha Brukman9fff7e12003-05-24 00:15:53 +000079
Chris Lattner0e5e49e2003-08-06 04:36:35 +000080 EmitSourceFileHeader("Machine Code Emitter", o);
Misha Brukmane2ba7782004-08-10 18:31:01 +000081 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
Jim Grosbach8b892ae2010-10-07 16:56:28 +000082
Chris Lattnerf6502782010-03-19 00:34:35 +000083 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
84 Target.getInstructionsByEnumValue();
Misha Brukman9fff7e12003-05-24 00:15:53 +000085
Misha Brukmanad346ad2004-08-10 20:54:58 +000086 // Emit function declaration
Misha Brukmane2ba7782004-08-10 18:31:01 +000087 o << "unsigned " << Target.getName() << "CodeEmitter::"
Jim Grosbachbade37b2010-10-08 00:21:28 +000088 << "getBinaryCodeForInstr(const MachineInstr &MI) const {\n";
Misha Brukmanad346ad2004-08-10 20:54:58 +000089
Jim Laskeyed393432006-07-12 19:15:43 +000090 // Emit instruction base values
91 o << " static const unsigned InstBits[] = {\n";
Chris Lattnerf6502782010-03-19 00:34:35 +000092 for (std::vector<const CodeGenInstruction*>::const_iterator
Jim Laskeyed393432006-07-12 19:15:43 +000093 IN = NumberedInstructions.begin(),
94 EN = NumberedInstructions.end();
95 IN != EN; ++IN) {
96 const CodeGenInstruction *CGI = *IN;
97 Record *R = CGI->TheDef;
Jim Grosbach8b892ae2010-10-07 16:56:28 +000098
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +000099 if (R->getValueAsString("Namespace") == "TargetOpcode") {
Evan Chengbc95b232008-09-17 06:29:52 +0000100 o << " 0U,\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000101 continue;
102 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000103
Chris Lattner6f334ad2003-08-01 04:46:24 +0000104 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000105
Misha Brukman9fff7e12003-05-24 00:15:53 +0000106 // Start by filling in fixed values...
Reid Spencerfa3e3b92006-11-03 01:48:30 +0000107 unsigned Value = 0;
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000108 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
109 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
110 Value |= B->getValue() << (e-i-1);
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000111 }
112 }
Evan Chengbc95b232008-09-17 06:29:52 +0000113 o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000114 }
Evan Chengbc95b232008-09-17 06:29:52 +0000115 o << " 0U\n };\n";
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000116
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000117 // Map to accumulate all the cases.
118 std::map<std::string, std::vector<std::string> > CaseMap;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000119
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000120 // Construct all cases statement for each opcode
121 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
122 IC != EC; ++IC) {
123 Record *R = *IC;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000124 if (R->getValueAsString("Namespace") == "TargetOpcode")
125 continue;
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000126 const std::string &InstName = R->getName();
127 std::string Case("");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000128
Jim Laskeyed393432006-07-12 19:15:43 +0000129 BitsInit *BI = R->getValueAsBitsInit("Inst");
130 const std::vector<RecordVal> &Vals = R->getValues();
Chris Lattnerf30187a2010-03-19 00:07:20 +0000131 CodeGenInstruction &CGI = Target.getInstruction(R);
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000132
Misha Brukman28eefa52004-10-14 05:53:01 +0000133 // Loop over all of the fields in the instruction, determining which are the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000134 // operands to the instruction.
Jim Grosbach01855072010-10-11 18:25:51 +0000135 unsigned NumberedOp = 0;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000136 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Misha Brukman28eefa52004-10-14 05:53:01 +0000137 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
Misha Brukman7eac4762003-07-15 21:00:32 +0000138 // Is the operand continuous? If so, we can just mask and OR it in
Misha Brukman3da94ae2005-04-22 00:00:37 +0000139 // instead of doing it bit-by-bit, saving a lot in runtime cost.
Dan Gohman9b03da62009-12-15 20:21:44 +0000140 const std::string &VarName = Vals[i].getName();
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000141 bool gotOp = false;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000142
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000143 for (int bit = BI->getNumBits()-1; bit >= 0; ) {
Dan Gohman9b03da62009-12-15 20:21:44 +0000144 int varBit = getVariableBit(VarName, BI, bit);
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000145
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000146 if (varBit == -1) {
147 --bit;
148 } else {
149 int beginInstBit = bit;
150 int beginVarBit = varBit;
151 int N = 1;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000152
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000153 for (--bit; bit >= 0;) {
Dan Gohman9b03da62009-12-15 20:21:44 +0000154 varBit = getVariableBit(VarName, BI, bit);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000155 if (varBit == -1 || varBit != (beginVarBit - N)) break;
156 ++N;
157 --bit;
Chris Lattnerd7efef92003-08-05 03:53:04 +0000158 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000159
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000160 if (!gotOp) {
Jim Grosbach01855072010-10-11 18:25:51 +0000161 // If the operand matches by name, reference according to that
162 // operand number. Non-matching operands are assumed to be in
163 // order.
164 unsigned OpIdx;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000165 if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
Jim Grosbach1a7233f2010-10-11 21:31:22 +0000166 // Get the machine operand number for the indicated operand.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000167 OpIdx = CGI.Operands[OpIdx].MIOperandNo;
168 assert (!CGI.Operands.isFlatOperandNotEmitted(OpIdx) &&
Jim Grosbacha1e21942010-10-11 21:41:31 +0000169 "Explicitly used operand also marked as not emitted!");
Jim Grosbach01855072010-10-11 18:25:51 +0000170 } else {
171 /// If this operand is not supposed to be emitted by the
172 /// generated emitter, skip it.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000173 while (CGI.Operands.isFlatOperandNotEmitted(NumberedOp))
Jim Grosbach01855072010-10-11 18:25:51 +0000174 ++NumberedOp;
175 OpIdx = NumberedOp++;
176 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000177 std::pair<unsigned, unsigned> SO =
178 CGI.Operands.getSubOperandNumber(OpIdx);
Jim Grosbach5013f742010-10-12 22:21:57 +0000179 std::string &EncoderMethodName =
Chris Lattnerc240bb02010-11-01 04:03:32 +0000180 CGI.Operands[SO.first].EncoderMethodName;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000181
Jim Grosbach5013f742010-10-12 22:21:57 +0000182 // If the source operand has a custom encoder, use it. This will
183 // get the encoding for all of the suboperands.
184 if (!EncoderMethodName.empty()) {
185 // A custom encoder has all of the information for the
186 // sub-operands, if there are more than one, so only
187 // query the encoder once per source operand.
188 if (SO.second == 0) {
189 Case += " // op: " + VarName + "\n"
190 + " op = " + EncoderMethodName + "(MI, "
191 + utostr(OpIdx) + ");\n";
192 }
193 } else {
194 Case += " // op: " + VarName + "\n"
195 + " op = getMachineOpValue(MI, MI.getOperand("
196 + utostr(OpIdx) + "));\n";
197 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000198 gotOp = true;
199 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000200
Chris Lattner1cfa0772008-10-05 18:31:58 +0000201 unsigned opMask = ~0U >> (32-N);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000202 int opShift = beginVarBit - N + 1;
203 opMask <<= opShift;
204 opShift = beginInstBit - beginVarBit;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000205
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000206 if (opShift > 0) {
207 Case += " Value |= (op & " + utostr(opMask) + "U) << "
208 + itostr(opShift) + ";\n";
209 } else if (opShift < 0) {
210 Case += " Value |= (op & " + utostr(opMask) + "U) >> "
211 + itostr(-opShift) + ";\n";
212 } else {
213 Case += " Value |= op & " + utostr(opMask) + "U;\n";
214 }
Chris Lattnerd7efef92003-08-05 03:53:04 +0000215 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000216 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000217 }
218 }
219
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000220 std::vector<std::string> &InstList = CaseMap[Case];
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000221 InstList.push_back(InstName);
222 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000223
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000224 // Emit initial function code
225 o << " const unsigned opcode = MI.getOpcode();\n"
226 << " unsigned Value = InstBits[opcode];\n"
Evan Chenge3e36262008-09-07 09:00:57 +0000227 << " unsigned op = 0;\n"
Evan Chengacff3392008-09-02 06:51:36 +0000228 << " op = op; // suppress warning\n"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000229 << " switch (opcode) {\n";
230
231 // Emit each case statement
232 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
233 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
234 const std::string &Case = IE->first;
235 std::vector<std::string> &InstList = IE->second;
236
237 for (int i = 0, N = InstList.size(); i < N; i++) {
238 if (i) o << "\n";
239 o << " case " << Namespace << InstList[i] << ":";
240 }
241 o << " {\n";
242 o << Case;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000243 o << " break;\n"
244 << " }\n";
245 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000246
Misha Brukman28eefa52004-10-14 05:53:01 +0000247 // Default case: unhandled opcode
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000248 o << " default:\n"
Torok Edwin804e0fe2009-07-08 19:04:27 +0000249 << " std::string msg;\n"
250 << " raw_string_ostream Msg(msg);\n"
251 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner75361b62010-04-07 22:58:41 +0000252 << " report_fatal_error(Msg.str());\n"
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000253 << " }\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +0000254 << " return Value;\n"
Misha Brukman28eefa52004-10-14 05:53:01 +0000255 << "}\n\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000256}