blob: 74268a3c3e7a3efa7849423f7b5eaf3c49e6545d [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" ||
Evan Cheng2e28d622008-02-02 04:07:54 +000032 R->getName() == "DECLARE" ||
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" ||
36 R->getName() == "SUBREG_TO_REG") continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037
38 BitsInit *BI = R->getValueAsBitsInit("Inst");
39
40 unsigned numBits = BI->getNumBits();
41 BitsInit *NewBI = new BitsInit(numBits);
42 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
43 unsigned bitSwapIdx = numBits - bit - 1;
44 Init *OrigBit = BI->getBit(bit);
45 Init *BitSwap = BI->getBit(bitSwapIdx);
46 NewBI->setBit(bit, BitSwap);
47 NewBI->setBit(bitSwapIdx, OrigBit);
48 }
49 if (numBits % 2) {
50 unsigned middle = (numBits + 1) / 2;
51 NewBI->setBit(middle, BI->getBit(middle));
52 }
53
54 // Update the bits in reversed order so that emitInstrOpBits will get the
55 // correct endianness.
56 R->getValue("Inst")->setValue(NewBI);
57 }
58}
59
60
61// If the VarBitInit at position 'bit' matches the specified variable then
62// return the variable bit position. Otherwise return -1.
63int CodeEmitterGen::getVariableBit(const std::string &VarName,
64 BitsInit *BI, int bit) {
65 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
66 TypedInit *TI = VBI->getVariable();
67
68 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
69 if (VI->getName() == VarName) return VBI->getBitNum();
70 }
71 }
72
73 return -1;
74}
75
76
77void CodeEmitterGen::run(std::ostream &o) {
78 CodeGenTarget Target;
79 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
80
81 // For little-endian instruction bit encodings, reverse the bit order
82 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
83
84 EmitSourceFileHeader("Machine Code Emitter", o);
85 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
86
87 std::vector<const CodeGenInstruction*> NumberedInstructions;
88 Target.getInstructionsByEnumValue(NumberedInstructions);
89
90 // Emit function declaration
91 o << "unsigned " << Target.getName() << "CodeEmitter::"
Evan Cheng3ca89372008-09-02 06:51:36 +000092 << "getBinaryCodeForInstr(const MachineInstr &MI) {\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
94 // Emit instruction base values
95 o << " static const unsigned InstBits[] = {\n";
96 for (std::vector<const CodeGenInstruction*>::iterator
97 IN = NumberedInstructions.begin(),
98 EN = NumberedInstructions.end();
99 IN != EN; ++IN) {
100 const CodeGenInstruction *CGI = *IN;
101 Record *R = CGI->TheDef;
102
103 if (IN != NumberedInstructions.begin()) o << ",\n";
104
105 if (R->getName() == "PHI" ||
106 R->getName() == "INLINEASM" ||
Dan Gohmanfa607c92008-07-01 00:05:16 +0000107 R->getName() == "DBG_LABEL" ||
108 R->getName() == "EH_LABEL" ||
109 R->getName() == "GC_LABEL" ||
Evan Cheng2e28d622008-02-02 04:07:54 +0000110 R->getName() == "DECLARE" ||
Christopher Lamb071a2a72007-07-26 07:48:21 +0000111 R->getName() == "EXTRACT_SUBREG" ||
Evan Cheng3c0eda52008-03-15 00:03:38 +0000112 R->getName() == "INSERT_SUBREG" ||
Christopher Lamb76d72da2008-03-16 03:12:01 +0000113 R->getName() == "IMPLICIT_DEF" ||
114 R->getName() == "SUBREG_TO_REG") {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 o << " 0U";
116 continue;
117 }
118
119 BitsInit *BI = R->getValueAsBitsInit("Inst");
120
121 // Start by filling in fixed values...
122 unsigned Value = 0;
123 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
124 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
125 Value |= B->getValue() << (e-i-1);
126 }
127 }
128 o << " " << Value << "U";
129 }
130 o << "\n };\n";
131
132 // Map to accumulate all the cases.
133 std::map<std::string, std::vector<std::string> > CaseMap;
134
135 // Construct all cases statement for each opcode
136 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
137 IC != EC; ++IC) {
138 Record *R = *IC;
139 const std::string &InstName = R->getName();
140 std::string Case("");
141
142 if (InstName == "PHI" ||
143 InstName == "INLINEASM" ||
Dan Gohmanfa607c92008-07-01 00:05:16 +0000144 InstName == "DBG_LABEL"||
145 InstName == "EH_LABEL"||
146 InstName == "GC_LABEL"||
Evan Cheng2e28d622008-02-02 04:07:54 +0000147 InstName == "DECLARE"||
Christopher Lamb071a2a72007-07-26 07:48:21 +0000148 InstName == "EXTRACT_SUBREG" ||
Evan Cheng3c0eda52008-03-15 00:03:38 +0000149 InstName == "INSERT_SUBREG" ||
Christopher Lamb76d72da2008-03-16 03:12:01 +0000150 InstName == "IMPLICIT_DEF" ||
151 InstName == "SUBREG_TO_REG") continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152
153 BitsInit *BI = R->getValueAsBitsInit("Inst");
154 const std::vector<RecordVal> &Vals = R->getValues();
155 CodeGenInstruction &CGI = Target.getInstruction(InstName);
156
157 // Loop over all of the fields in the instruction, determining which are the
158 // operands to the instruction.
159 unsigned op = 0;
160 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
161 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
162 // Is the operand continuous? If so, we can just mask and OR it in
163 // instead of doing it bit-by-bit, saving a lot in runtime cost.
164 const std::string &VarName = Vals[i].getName();
165 bool gotOp = false;
166
167 for (int bit = BI->getNumBits()-1; bit >= 0; ) {
168 int varBit = getVariableBit(VarName, BI, bit);
169
170 if (varBit == -1) {
171 --bit;
172 } else {
173 int beginInstBit = bit;
174 int beginVarBit = varBit;
175 int N = 1;
176
177 for (--bit; bit >= 0;) {
178 varBit = getVariableBit(VarName, BI, bit);
179 if (varBit == -1 || varBit != (beginVarBit - N)) break;
180 ++N;
181 --bit;
182 }
183
184 if (!gotOp) {
185 /// If this operand is not supposed to be emitted by the generated
186 /// emitter, skip it.
187 while (CGI.isFlatOperandNotEmitted(op))
188 ++op;
189
190 Case += " // op: " + VarName + "\n"
191 + " op = getMachineOpValue(MI, MI.getOperand("
192 + utostr(op++) + "));\n";
193 gotOp = true;
194 }
195
196 unsigned opMask = (1 << N) - 1;
197 int opShift = beginVarBit - N + 1;
198 opMask <<= opShift;
199 opShift = beginInstBit - beginVarBit;
200
201 if (opShift > 0) {
202 Case += " Value |= (op & " + utostr(opMask) + "U) << "
203 + itostr(opShift) + ";\n";
204 } else if (opShift < 0) {
205 Case += " Value |= (op & " + utostr(opMask) + "U) >> "
206 + itostr(-opShift) + ";\n";
207 } else {
208 Case += " Value |= op & " + utostr(opMask) + "U;\n";
209 }
210 }
211 }
212 }
213 }
214
215 std::vector<std::string> &InstList = CaseMap[Case];
216 InstList.push_back(InstName);
217 }
218
219
220 // Emit initial function code
221 o << " const unsigned opcode = MI.getOpcode();\n"
222 << " unsigned Value = InstBits[opcode];\n"
223 << " unsigned op;\n"
Evan Cheng3ca89372008-09-02 06:51:36 +0000224 << " op = op; // suppress warning\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 << " switch (opcode) {\n";
226
227 // Emit each case statement
228 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
229 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
230 const std::string &Case = IE->first;
231 std::vector<std::string> &InstList = IE->second;
232
233 for (int i = 0, N = InstList.size(); i < N; i++) {
234 if (i) o << "\n";
235 o << " case " << Namespace << InstList[i] << ":";
236 }
237 o << " {\n";
238 o << Case;
239 o << " break;\n"
240 << " }\n";
241 }
242
243 // Default case: unhandled opcode
244 o << " default:\n"
245 << " cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
246 << " abort();\n"
247 << " }\n"
248 << " return Value;\n"
249 << "}\n\n";
250}