blob: 2a2a4ef63e16600c3818e2bc89e35470dab417d1 [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;
Jim Laskeya683f9b2007-01-26 17:29:20 +000027 if (R->getName() == "PHI" ||
28 R->getName() == "INLINEASM" ||
Dan Gohman44066042008-07-01 00:05:16 +000029 R->getName() == "DBG_LABEL" ||
30 R->getName() == "EH_LABEL" ||
31 R->getName() == "GC_LABEL" ||
Jakob Stoklund Olesen26207e52009-09-28 20:32:26 +000032 R->getName() == "KILL" ||
Christopher Lamb08d52072007-07-26 07:48:21 +000033 R->getName() == "EXTRACT_SUBREG" ||
Evan Chengda47e6e2008-03-15 00:03:38 +000034 R->getName() == "INSERT_SUBREG" ||
Christopher Lambc9298232008-03-16 03:12:01 +000035 R->getName() == "IMPLICIT_DEF" ||
Dan Gohmanf8c73942009-04-13 15:38:05 +000036 R->getName() == "SUBREG_TO_REG" ||
Dale Johannesen87563b32010-01-08 23:51:25 +000037 R->getName() == "COPY_TO_REGCLASS" ||
Evan Chengb55c8be2010-05-01 00:28:44 +000038 R->getName() == "DBG_VALUE" ||
39 R->getName() == "REG_SEQUENCE") continue;
Dan Gohmanf8c73942009-04-13 15:38:05 +000040
Jim Laskeyf1b05bf2006-07-13 21:02:53 +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();
44 BitsInit *NewBI = new BitsInit(numBits);
45 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
46 unsigned bitSwapIdx = numBits - bit - 1;
47 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;
54 NewBI->setBit(middle, BI->getBit(middle));
55 }
56
57 // 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
63
Jim Laskeycb129032006-07-13 22:17:08 +000064// If the VarBitInit at position 'bit' matches the specified variable then
65// return the variable bit position. Otherwise return -1.
Dan Gohman9b03da62009-12-15 20:21:44 +000066int CodeEmitterGen::getVariableBit(const std::string &VarName,
Jim Laskeycb129032006-07-13 22:17:08 +000067 BitsInit *BI, int bit) {
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000068 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
69 TypedInit *TI = VBI->getVariable();
Dan Gohman9b03da62009-12-15 20:21:44 +000070
71 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
72 if (VI->getName() == VarName) return VBI->getBitNum();
73 }
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000074 }
75
76 return -1;
77}
78
79
Daniel Dunbar1a551802009-07-03 00:10:29 +000080void CodeEmitterGen::run(raw_ostream &o) {
Misha Brukmane2ba7782004-08-10 18:31:01 +000081 CodeGenTarget Target;
Chris Lattner048c00d2003-08-01 04:38:18 +000082 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Laskeyf1b05bf2006-07-13 21:02:53 +000083
84 // For little-endian instruction bit encodings, reverse the bit order
85 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
Misha Brukman9fff7e12003-05-24 00:15:53 +000086
Chris Lattner0e5e49e2003-08-06 04:36:35 +000087 EmitSourceFileHeader("Machine Code Emitter", o);
Misha Brukmane2ba7782004-08-10 18:31:01 +000088 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
Jim Laskeyed393432006-07-12 19:15:43 +000089
Chris Lattnerf6502782010-03-19 00:34:35 +000090 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
91 Target.getInstructionsByEnumValue();
Misha Brukman9fff7e12003-05-24 00:15:53 +000092
Misha Brukmanad346ad2004-08-10 20:54:58 +000093 // Emit function declaration
Misha Brukmane2ba7782004-08-10 18:31:01 +000094 o << "unsigned " << Target.getName() << "CodeEmitter::"
Evan Chengacff3392008-09-02 06:51:36 +000095 << "getBinaryCodeForInstr(const MachineInstr &MI) {\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;
Chris Lattner188454a2006-01-27 01:39:38 +0000105
Jim Laskeya683f9b2007-01-26 17:29:20 +0000106 if (R->getName() == "PHI" ||
107 R->getName() == "INLINEASM" ||
Dan Gohman44066042008-07-01 00:05:16 +0000108 R->getName() == "DBG_LABEL" ||
109 R->getName() == "EH_LABEL" ||
110 R->getName() == "GC_LABEL" ||
Jakob Stoklund Olesen26207e52009-09-28 20:32:26 +0000111 R->getName() == "KILL" ||
Christopher Lamb08d52072007-07-26 07:48:21 +0000112 R->getName() == "EXTRACT_SUBREG" ||
Evan Chengda47e6e2008-03-15 00:03:38 +0000113 R->getName() == "INSERT_SUBREG" ||
Christopher Lambc9298232008-03-16 03:12:01 +0000114 R->getName() == "IMPLICIT_DEF" ||
Dan Gohmanf8c73942009-04-13 15:38:05 +0000115 R->getName() == "SUBREG_TO_REG" ||
Dale Johannesen87563b32010-01-08 23:51:25 +0000116 R->getName() == "COPY_TO_REGCLASS" ||
Evan Chengb55c8be2010-05-01 00:28:44 +0000117 R->getName() == "DBG_VALUE" ||
118 R->getName() == "REG_SEQUENCE") {
Evan Chengbc95b232008-09-17 06:29:52 +0000119 o << " 0U,\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000120 continue;
121 }
122
Chris Lattner6f334ad2003-08-01 04:46:24 +0000123 BitsInit *BI = R->getValueAsBitsInit("Inst");
Misha Brukman9fff7e12003-05-24 00:15:53 +0000124
Misha Brukman9fff7e12003-05-24 00:15:53 +0000125 // Start by filling in fixed values...
Reid Spencerfa3e3b92006-11-03 01:48:30 +0000126 unsigned Value = 0;
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000127 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
128 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
129 Value |= B->getValue() << (e-i-1);
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000130 }
131 }
Evan Chengbc95b232008-09-17 06:29:52 +0000132 o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n";
Jim Laskeyed393432006-07-12 19:15:43 +0000133 }
Evan Chengbc95b232008-09-17 06:29:52 +0000134 o << " 0U\n };\n";
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000135
136 // Map to accumulate all the cases.
137 std::map<std::string, std::vector<std::string> > CaseMap;
138
139 // Construct all cases statement for each opcode
140 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
141 IC != EC; ++IC) {
142 Record *R = *IC;
143 const std::string &InstName = R->getName();
144 std::string Case("");
Jim Laskeyed393432006-07-12 19:15:43 +0000145
Jim Laskeya683f9b2007-01-26 17:29:20 +0000146 if (InstName == "PHI" ||
147 InstName == "INLINEASM" ||
Dan Gohman44066042008-07-01 00:05:16 +0000148 InstName == "DBG_LABEL"||
149 InstName == "EH_LABEL"||
150 InstName == "GC_LABEL"||
Jakob Stoklund Olesen26207e52009-09-28 20:32:26 +0000151 InstName == "KILL"||
Christopher Lamb08d52072007-07-26 07:48:21 +0000152 InstName == "EXTRACT_SUBREG" ||
Evan Chengda47e6e2008-03-15 00:03:38 +0000153 InstName == "INSERT_SUBREG" ||
Christopher Lambc9298232008-03-16 03:12:01 +0000154 InstName == "IMPLICIT_DEF" ||
Dan Gohmanf8c73942009-04-13 15:38:05 +0000155 InstName == "SUBREG_TO_REG" ||
Dale Johannesen87563b32010-01-08 23:51:25 +0000156 InstName == "COPY_TO_REGCLASS" ||
Evan Chengb55c8be2010-05-01 00:28:44 +0000157 InstName == "DBG_VALUE" ||
158 InstName == "REG_SEQUENCE") continue;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000159
Jim Laskeyed393432006-07-12 19:15:43 +0000160 BitsInit *BI = R->getValueAsBitsInit("Inst");
161 const std::vector<RecordVal> &Vals = R->getValues();
Chris Lattnerf30187a2010-03-19 00:07:20 +0000162 CodeGenInstruction &CGI = Target.getInstruction(R);
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000163
Misha Brukman28eefa52004-10-14 05:53:01 +0000164 // Loop over all of the fields in the instruction, determining which are the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000165 // operands to the instruction.
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000166 unsigned op = 0;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000167 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Misha Brukman28eefa52004-10-14 05:53:01 +0000168 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
Misha Brukman7eac4762003-07-15 21:00:32 +0000169 // Is the operand continuous? If so, we can just mask and OR it in
Misha Brukman3da94ae2005-04-22 00:00:37 +0000170 // instead of doing it bit-by-bit, saving a lot in runtime cost.
Dan Gohman9b03da62009-12-15 20:21:44 +0000171 const std::string &VarName = Vals[i].getName();
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000172 bool gotOp = false;
173
174 for (int bit = BI->getNumBits()-1; bit >= 0; ) {
Dan Gohman9b03da62009-12-15 20:21:44 +0000175 int varBit = getVariableBit(VarName, BI, bit);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000176
177 if (varBit == -1) {
178 --bit;
179 } else {
180 int beginInstBit = bit;
181 int beginVarBit = varBit;
182 int N = 1;
183
184 for (--bit; bit >= 0;) {
Dan Gohman9b03da62009-12-15 20:21:44 +0000185 varBit = getVariableBit(VarName, BI, bit);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000186 if (varBit == -1 || varBit != (beginVarBit - N)) break;
187 ++N;
188 --bit;
Chris Lattnerd7efef92003-08-05 03:53:04 +0000189 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000190
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000191 if (!gotOp) {
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000192 /// If this operand is not supposed to be emitted by the generated
193 /// emitter, skip it.
194 while (CGI.isFlatOperandNotEmitted(op))
195 ++op;
196
Dan Gohman9b03da62009-12-15 20:21:44 +0000197 Case += " // op: " + VarName + "\n"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000198 + " op = getMachineOpValue(MI, MI.getOperand("
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000199 + utostr(op++) + "));\n";
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000200 gotOp = true;
201 }
202
Chris Lattner1cfa0772008-10-05 18:31:58 +0000203 unsigned opMask = ~0U >> (32-N);
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000204 int opShift = beginVarBit - N + 1;
205 opMask <<= opShift;
206 opShift = beginInstBit - beginVarBit;
207
208 if (opShift > 0) {
209 Case += " Value |= (op & " + utostr(opMask) + "U) << "
210 + itostr(opShift) + ";\n";
211 } else if (opShift < 0) {
212 Case += " Value |= (op & " + utostr(opMask) + "U) >> "
213 + itostr(-opShift) + ";\n";
214 } else {
215 Case += " Value |= op & " + utostr(opMask) + "U;\n";
216 }
Chris Lattnerd7efef92003-08-05 03:53:04 +0000217 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000218 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000219 }
220 }
221
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000222 std::vector<std::string> &InstList = CaseMap[Case];
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000223 InstList.push_back(InstName);
224 }
Misha Brukman9fff7e12003-05-24 00:15:53 +0000225
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000226
227 // Emit initial function code
228 o << " const unsigned opcode = MI.getOpcode();\n"
229 << " unsigned Value = InstBits[opcode];\n"
Evan Chenge3e36262008-09-07 09:00:57 +0000230 << " unsigned op = 0;\n"
Evan Chengacff3392008-09-02 06:51:36 +0000231 << " op = op; // suppress warning\n"
Jim Laskeyf1b05bf2006-07-13 21:02:53 +0000232 << " switch (opcode) {\n";
233
234 // Emit each case statement
235 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
236 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
237 const std::string &Case = IE->first;
238 std::vector<std::string> &InstList = IE->second;
239
240 for (int i = 0, N = InstList.size(); i < N; i++) {
241 if (i) o << "\n";
242 o << " case " << Namespace << InstList[i] << ":";
243 }
244 o << " {\n";
245 o << Case;
Misha Brukman9fff7e12003-05-24 00:15:53 +0000246 o << " break;\n"
247 << " }\n";
248 }
Misha Brukman7eac4762003-07-15 21:00:32 +0000249
Misha Brukman28eefa52004-10-14 05:53:01 +0000250 // Default case: unhandled opcode
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000251 o << " default:\n"
Torok Edwin804e0fe2009-07-08 19:04:27 +0000252 << " std::string msg;\n"
253 << " raw_string_ostream Msg(msg);\n"
254 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner75361b62010-04-07 22:58:41 +0000255 << " report_fatal_error(Msg.str());\n"
Misha Brukmancbfde0a2003-05-27 22:19:58 +0000256 << " }\n"
Misha Brukman9fff7e12003-05-24 00:15:53 +0000257 << " return Value;\n"
Misha Brukman28eefa52004-10-14 05:53:01 +0000258 << "}\n\n";
Misha Brukman9fff7e12003-05-24 00:15:53 +0000259}