blob: 23d42de33d179addd6963529be977179ac3cecdf [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// 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.
13//
14//===----------------------------------------------------------------------===//
15
16#include "CodeEmitterGen.h"
17#include "CodeGenTarget.h"
18#include "Record.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/Support/Debug.h"
21using namespace llvm;
22
23void 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;
27 if (R->getName() == "PHI" ||
28 R->getName() == "INLINEASM" ||
Dan Gohmanfa607c92008-07-01 00:05:16 +000029 R->getName() == "DBG_LABEL" ||
30 R->getName() == "EH_LABEL" ||
31 R->getName() == "GC_LABEL" ||
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +000032 R->getName() == "KILL" ||
Christopher Lamb071a2a72007-07-26 07:48:21 +000033 R->getName() == "EXTRACT_SUBREG" ||
Evan Cheng3c0eda52008-03-15 00:03:38 +000034 R->getName() == "INSERT_SUBREG" ||
Christopher Lamb76d72da2008-03-16 03:12:01 +000035 R->getName() == "IMPLICIT_DEF" ||
Dan Gohman1aeb5db2009-04-13 15:38:05 +000036 R->getName() == "SUBREG_TO_REG" ||
Dale Johannesen4b04d8a2010-01-08 23:51:25 +000037 R->getName() == "COPY_TO_REGCLASS" ||
Dale Johannesen982759b2010-01-09 01:24:25 +000038 R->getName() == "DEBUG_VALUE" ||
39 R->getName() == "DEBUG_DECLARE") continue;
Dan Gohman1aeb5db2009-04-13 15:38:05 +000040
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 BitsInit *BI = R->getValueAsBitsInit("Inst");
42
43 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);
51 }
52 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);
60 }
61}
62
63
64// If the VarBitInit at position 'bit' matches the specified variable then
65// return the variable bit position. Otherwise return -1.
Dan Gohman8f4f9942009-12-15 20:21:44 +000066int CodeEmitterGen::getVariableBit(const std::string &VarName,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 BitsInit *BI, int bit) {
68 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
69 TypedInit *TI = VBI->getVariable();
Dan Gohman8f4f9942009-12-15 20:21:44 +000070
71 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
72 if (VI->getName() == VarName) return VBI->getBitNum();
73 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 }
75
76 return -1;
77}
78
79
Daniel Dunbard4287062009-07-03 00:10:29 +000080void CodeEmitterGen::run(raw_ostream &o) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 CodeGenTarget Target;
82 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
83
84 // For little-endian instruction bit encodings, reverse the bit order
85 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
86
87 EmitSourceFileHeader("Machine Code Emitter", o);
88 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
89
90 std::vector<const CodeGenInstruction*> NumberedInstructions;
91 Target.getInstructionsByEnumValue(NumberedInstructions);
92
93 // Emit function declaration
94 o << "unsigned " << Target.getName() << "CodeEmitter::"
Evan Cheng3ca89372008-09-02 06:51:36 +000095 << "getBinaryCodeForInstr(const MachineInstr &MI) {\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 // Emit instruction base values
98 o << " static const unsigned InstBits[] = {\n";
99 for (std::vector<const CodeGenInstruction*>::iterator
100 IN = NumberedInstructions.begin(),
101 EN = NumberedInstructions.end();
102 IN != EN; ++IN) {
103 const CodeGenInstruction *CGI = *IN;
104 Record *R = CGI->TheDef;
105
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 if (R->getName() == "PHI" ||
107 R->getName() == "INLINEASM" ||
Dan Gohmanfa607c92008-07-01 00:05:16 +0000108 R->getName() == "DBG_LABEL" ||
109 R->getName() == "EH_LABEL" ||
110 R->getName() == "GC_LABEL" ||
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +0000111 R->getName() == "KILL" ||
Christopher Lamb071a2a72007-07-26 07:48:21 +0000112 R->getName() == "EXTRACT_SUBREG" ||
Evan Cheng3c0eda52008-03-15 00:03:38 +0000113 R->getName() == "INSERT_SUBREG" ||
Christopher Lamb76d72da2008-03-16 03:12:01 +0000114 R->getName() == "IMPLICIT_DEF" ||
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000115 R->getName() == "SUBREG_TO_REG" ||
Dale Johannesen4b04d8a2010-01-08 23:51:25 +0000116 R->getName() == "COPY_TO_REGCLASS" ||
Dale Johannesen982759b2010-01-09 01:24:25 +0000117 R->getName() == "DEBUG_VALUE" ||
118 R->getName() == "DEBUG_DECLARE") {
Evan Cheng15345892008-09-17 06:29:52 +0000119 o << " 0U,\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 continue;
121 }
122
123 BitsInit *BI = R->getValueAsBitsInit("Inst");
124
125 // Start by filling in fixed values...
126 unsigned Value = 0;
127 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);
130 }
131 }
Evan Cheng15345892008-09-17 06:29:52 +0000132 o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 }
Evan Cheng15345892008-09-17 06:29:52 +0000134 o << " 0U\n };\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +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("");
145
146 if (InstName == "PHI" ||
147 InstName == "INLINEASM" ||
Dan Gohmanfa607c92008-07-01 00:05:16 +0000148 InstName == "DBG_LABEL"||
149 InstName == "EH_LABEL"||
150 InstName == "GC_LABEL"||
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +0000151 InstName == "KILL"||
Christopher Lamb071a2a72007-07-26 07:48:21 +0000152 InstName == "EXTRACT_SUBREG" ||
Evan Cheng3c0eda52008-03-15 00:03:38 +0000153 InstName == "INSERT_SUBREG" ||
Christopher Lamb76d72da2008-03-16 03:12:01 +0000154 InstName == "IMPLICIT_DEF" ||
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000155 InstName == "SUBREG_TO_REG" ||
Dale Johannesen4b04d8a2010-01-08 23:51:25 +0000156 InstName == "COPY_TO_REGCLASS" ||
Dale Johannesen982759b2010-01-09 01:24:25 +0000157 InstName == "DEBUG_VALUE" ||
158 InstName == "DEBUG_DECLARE") continue;
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000159
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 BitsInit *BI = R->getValueAsBitsInit("Inst");
161 const std::vector<RecordVal> &Vals = R->getValues();
162 CodeGenInstruction &CGI = Target.getInstruction(InstName);
163
164 // Loop over all of the fields in the instruction, determining which are the
165 // operands to the instruction.
166 unsigned op = 0;
167 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
168 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
169 // Is the operand continuous? If so, we can just mask and OR it in
170 // instead of doing it bit-by-bit, saving a lot in runtime cost.
Dan Gohman8f4f9942009-12-15 20:21:44 +0000171 const std::string &VarName = Vals[i].getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 bool gotOp = false;
173
174 for (int bit = BI->getNumBits()-1; bit >= 0; ) {
Dan Gohman8f4f9942009-12-15 20:21:44 +0000175 int varBit = getVariableBit(VarName, BI, bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohman8f4f9942009-12-15 20:21:44 +0000185 varBit = getVariableBit(VarName, BI, bit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 if (varBit == -1 || varBit != (beginVarBit - N)) break;
187 ++N;
188 --bit;
189 }
190
191 if (!gotOp) {
192 /// 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 Gohman8f4f9942009-12-15 20:21:44 +0000197 Case += " // op: " + VarName + "\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 + " op = getMachineOpValue(MI, MI.getOperand("
199 + utostr(op++) + "));\n";
200 gotOp = true;
201 }
202
Chris Lattnerdeffab32008-10-05 18:31:58 +0000203 unsigned opMask = ~0U >> (32-N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
217 }
218 }
219 }
220 }
221
222 std::vector<std::string> &InstList = CaseMap[Case];
223 InstList.push_back(InstName);
224 }
225
226
227 // Emit initial function code
228 o << " const unsigned opcode = MI.getOpcode();\n"
229 << " unsigned Value = InstBits[opcode];\n"
Evan Cheng54eb0292008-09-07 09:00:57 +0000230 << " unsigned op = 0;\n"
Evan Cheng3ca89372008-09-02 06:51:36 +0000231 << " op = op; // suppress warning\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
246 o << " break;\n"
247 << " }\n";
248 }
249
250 // Default case: unhandled opcode
251 o << " default:\n"
Edwin Török2b331342009-07-08 19:04:27 +0000252 << " std::string msg;\n"
253 << " raw_string_ostream Msg(msg);\n"
254 << " Msg << \"Not supported instr: \" << MI;\n"
255 << " llvm_report_error(Msg.str());\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 << " }\n"
257 << " return Value;\n"
258 << "}\n\n";
259}