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