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