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