blob: 631075263e4681ddd6fb568972a4a1ab3760fe2b [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/Debug.h"
Chris Lattner2082ebe2004-08-01 03:55:39 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000023void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) {
24 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
25 I != E; ++I) {
26 Record *R = *I;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +000027 if (R->getValueAsString("Namespace") == "TargetOpcode")
28 continue;
Dan Gohmanf8c73942009-04-13 15:38:05 +000029
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000030 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman28eefa52004-10-14 05:53:01 +000031
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000032 unsigned numBits = BI->getNumBits();
33 BitsInit *NewBI = new BitsInit(numBits);
34 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
35 unsigned bitSwapIdx = numBits - bit - 1;
36 Init *OrigBit = BI->getBit(bit);
37 Init *BitSwap = BI->getBit(bitSwapIdx);
38 NewBI->setBit(bit, BitSwap);
39 NewBI->setBit(bitSwapIdx, OrigBit);
Misha Brukman28eefa52004-10-14 05:53:01 +000040 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000041 if (numBits % 2) {
42 unsigned middle = (numBits + 1) / 2;
43 NewBI->setBit(middle, BI->getBit(middle));
44 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000045
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000046 // Update the bits in reversed order so that emitInstrOpBits will get the
47 // correct endianness.
48 R->getValue("Inst")->setValue(NewBI);
Misha Brukman28eefa52004-10-14 05:53:01 +000049 }
50}
51
52
Jim Laskeycb129032006-07-13 22:17:08 +000053// If the VarBitInit at position 'bit' matches the specified variable then
54// return the variable bit position. Otherwise return -1.
Dan Gohman9b03da62009-12-15 20:21:44 +000055int CodeEmitterGen::getVariableBit(const std::string &VarName,
Jim Laskeycb129032006-07-13 22:17:08 +000056 BitsInit *BI, int bit) {
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000057 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
58 TypedInit *TI = VBI->getVariable();
Jim Grosbach8b892ae2010-10-07 16:56:28 +000059
Dan Gohman9b03da62009-12-15 20:21:44 +000060 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
61 if (VI->getName() == VarName) return VBI->getBitNum();
62 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000063 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000064
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000065 return -1;
Jim Grosbach8b892ae2010-10-07 16:56:28 +000066}
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000067
68
Daniel Dunbar1a551802009-07-03 00:10:29 +000069void CodeEmitterGen::run(raw_ostream &o) {
Misha Brukmane2ba7782004-08-10 18:31:01 +000070 CodeGenTarget Target;
Chris Lattner048c00d2003-08-01 04:38:18 +000071 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbach8b892ae2010-10-07 16:56:28 +000072
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000073 // For little-endian instruction bit encodings, reverse the bit order
74 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
Misha Brukman9fff7e12003-05-24 00:15:53 +000075
Chris Lattner0e5e49e2003-08-06 04:36:35 +000076 EmitSourceFileHeader("Machine Code Emitter", o);
Misha Brukmane2ba7782004-08-10 18:31:01 +000077 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
Jim Grosbach8b892ae2010-10-07 16:56:28 +000078
Chris Lattnerf6502782010-03-19 00:34:35 +000079 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
80 Target.getInstructionsByEnumValue();
Misha Brukman9fff7e12003-05-24 00:15:53 +000081
Misha Brukmanad346ad2004-08-10 20:54:58 +000082 // Emit function declaration
Misha Brukmane2ba7782004-08-10 18:31:01 +000083 o << "unsigned " << Target.getName() << "CodeEmitter::"
Jim Grosbachbade37b2010-10-08 00:21:28 +000084 << "getBinaryCodeForInstr(const MachineInstr &MI) const {\n";
Misha Brukmanad346ad2004-08-10 20:54:58 +000085
Jim Laskeyed393432006-07-12 19:15:43 +000086 // Emit instruction base values
87 o << " static const unsigned InstBits[] = {\n";
Chris Lattnerf6502782010-03-19 00:34:35 +000088 for (std::vector<const CodeGenInstruction*>::const_iterator
Jim Laskeyed393432006-07-12 19:15:43 +000089 IN = NumberedInstructions.begin(),
90 EN = NumberedInstructions.end();
91 IN != EN; ++IN) {
92 const CodeGenInstruction *CGI = *IN;
93 Record *R = CGI->TheDef;
Jim Grosbach8b892ae2010-10-07 16:56:28 +000094
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +000095 if (R->getValueAsString("Namespace") == "TargetOpcode") {
Evan Chengbc95b232008-09-17 06:29:52 +000096 o << " 0U,\n";
Jim Laskeyed393432006-07-12 19:15:43 +000097 continue;
98 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +000099
Chris Lattner6f334ad2003-08-01 04:46:24 +0000100 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000101
Misha Brukman9fff7e12003-05-24 00:15:53 +0000102 // Start by filling in fixed values...
Reid Spencerfa3e3b92006-11-03 01:48:30 +0000103 unsigned Value = 0;
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000104 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
105 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
106 Value |= B->getValue() << (e-i-1);
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000107 }
108 }
Evan Chengbc95b232008-09-17 06:29:52 +0000109 o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000110 }
Evan Chengbc95b232008-09-17 06:29:52 +0000111 o << " 0U\n };\n";
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000112
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000113 // Map to accumulate all the cases.
114 std::map<std::string, std::vector<std::string> > CaseMap;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000115
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000116 // Construct all cases statement for each opcode
117 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
118 IC != EC; ++IC) {
119 Record *R = *IC;
Jakob Stoklund Olesen65766ce2010-07-02 21:44:22 +0000120 if (R->getValueAsString("Namespace") == "TargetOpcode")
121 continue;
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000122 const std::string &InstName = R->getName();
123 std::string Case("");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000124
Jim Laskeyed393432006-07-12 19:15:43 +0000125 BitsInit *BI = R->getValueAsBitsInit("Inst");
126 const std::vector<RecordVal> &Vals = R->getValues();
Chris Lattnerf30187a2010-03-19 00:07:20 +0000127 CodeGenInstruction &CGI = Target.getInstruction(R);
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000128
Misha Brukman28eefa52004-10-14 05:53:01 +0000129 // Loop over all of the fields in the instruction, determining which are the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000130 // operands to the instruction.
Jim Grosbach01855072010-10-11 18:25:51 +0000131 unsigned NumberedOp = 0;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000132 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Misha Brukman28eefa52004-10-14 05:53:01 +0000133 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
Misha Brukman7eac4762003-07-15 21:00:32 +0000134 // Is the operand continuous? If so, we can just mask and OR it in
Misha Brukman3da94ae2005-04-22 00:00:37 +0000135 // instead of doing it bit-by-bit, saving a lot in runtime cost.
Dan Gohman9b03da62009-12-15 20:21:44 +0000136 const std::string &VarName = Vals[i].getName();
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000137 bool gotOp = false;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000138
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000139 for (int bit = BI->getNumBits()-1; bit >= 0; ) {
Dan Gohman9b03da62009-12-15 20:21:44 +0000140 int varBit = getVariableBit(VarName, BI, bit);
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000141
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000142 if (varBit == -1) {
143 --bit;
144 } else {
145 int beginInstBit = bit;
146 int beginVarBit = varBit;
147 int N = 1;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000148
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000149 for (--bit; bit >= 0;) {
Dan Gohman9b03da62009-12-15 20:21:44 +0000150 varBit = getVariableBit(VarName, BI, bit);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000151 if (varBit == -1 || varBit != (beginVarBit - N)) break;
152 ++N;
153 --bit;
Chris Lattnerd7efef92003-08-05 03:53:04 +0000154 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000155
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000156 if (!gotOp) {
Jim Grosbach01855072010-10-11 18:25:51 +0000157 // If the operand matches by name, reference according to that
158 // operand number. Non-matching operands are assumed to be in
159 // order.
160 unsigned OpIdx;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000161 if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
Jim Grosbach1a7233f2010-10-11 21:31:22 +0000162 // Get the machine operand number for the indicated operand.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000163 OpIdx = CGI.Operands[OpIdx].MIOperandNo;
164 assert (!CGI.Operands.isFlatOperandNotEmitted(OpIdx) &&
Jim Grosbacha1e21942010-10-11 21:41:31 +0000165 "Explicitly used operand also marked as not emitted!");
Jim Grosbach01855072010-10-11 18:25:51 +0000166 } else {
167 /// If this operand is not supposed to be emitted by the
168 /// generated emitter, skip it.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000169 while (CGI.Operands.isFlatOperandNotEmitted(NumberedOp))
Jim Grosbach01855072010-10-11 18:25:51 +0000170 ++NumberedOp;
171 OpIdx = NumberedOp++;
172 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000173 std::pair<unsigned, unsigned> SO =
174 CGI.Operands.getSubOperandNumber(OpIdx);
Jim Grosbach5013f742010-10-12 22:21:57 +0000175 std::string &EncoderMethodName =
Chris Lattnerc240bb02010-11-01 04:03:32 +0000176 CGI.Operands[SO.first].EncoderMethodName;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000177
Jim Grosbach5013f742010-10-12 22:21:57 +0000178 // If the source operand has a custom encoder, use it. This will
179 // get the encoding for all of the suboperands.
180 if (!EncoderMethodName.empty()) {
181 // A custom encoder has all of the information for the
182 // sub-operands, if there are more than one, so only
183 // query the encoder once per source operand.
184 if (SO.second == 0) {
185 Case += " // op: " + VarName + "\n"
186 + " op = " + EncoderMethodName + "(MI, "
187 + utostr(OpIdx) + ");\n";
188 }
189 } else {
190 Case += " // op: " + VarName + "\n"
191 + " op = getMachineOpValue(MI, MI.getOperand("
192 + utostr(OpIdx) + "));\n";
193 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000194 gotOp = true;
195 }
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000196
Chris Lattner1cfa0772008-10-05 18:31:58 +0000197 unsigned opMask = ~0U >> (32-N);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000198 int opShift = beginVarBit - N + 1;
199 opMask <<= opShift;
200 opShift = beginInstBit - beginVarBit;
Jim Grosbach8b892ae2010-10-07 16:56:28 +0000201
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000202 if (opShift > 0) {
203 Case += " Value |= (op & " + utostr(opMask) + "U) << "
204 + itostr(opShift) + ";\n";
205 } else if (opShift < 0) {
206 Case += " Value |= (op & " + utostr(opMask) + "U) >> "
207 + itostr(-opShift) + ";\n";
208 } else {
209 Case += " Value |= op & " + utostr(opMask) + "U;\n";
210 }
Chris Lattnerd7efef92003-08-05 03:53:04 +0000211 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000212 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000213 }
214 }
215
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000216 std::vector<std::string> &InstList = CaseMap[Case];
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000217 InstList.push_back(InstName);
218 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000219
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000220
221 // Emit initial function code
222 o << " const unsigned opcode = MI.getOpcode();\n"
223 << " unsigned Value = InstBits[opcode];\n"
Evan Chenge3e36262008-09-07 09:00:57 +0000224 << " unsigned op = 0;\n"
Evan Chengacff3392008-09-02 06:51:36 +0000225 << " op = op; // suppress warning\n"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000226 << " switch (opcode) {\n";
227
228 // Emit each case statement
229 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
230 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
231 const std::string &Case = IE->first;
232 std::vector<std::string> &InstList = IE->second;
233
234 for (int i = 0, N = InstList.size(); i < N; i++) {
235 if (i) o << "\n";
236 o << " case " << Namespace << InstList[i] << ":";
237 }
238 o << " {\n";
239 o << Case;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000240 o << " break;\n"
241 << " }\n";
242 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000243
Misha Brukman28eefa52004-10-14 05:53:01 +0000244 // Default case: unhandled opcode
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000245 o << " default:\n"
Torok Edwin804e0fe2009-07-08 19:04:27 +0000246 << " std::string msg;\n"
247 << " raw_string_ostream Msg(msg);\n"
248 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner75361b62010-04-07 22:58:41 +0000249 << " report_fatal_error(Msg.str());\n"
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000250 << " }\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +0000251 << " return Value;\n"
Misha Brukman28eefa52004-10-14 05:53:01 +0000252 << "}\n\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000253}