1 //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "CodeEmitterGen.h"
17 #include "CodeGenTarget.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/Debug.h"
23 void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) {
24 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
27 if (R->getName() == "PHI" ||
28 R->getName() == "INLINEASM" ||
29 R->getName() == "LABEL" ||
30 R->getName() == "EXTRACT_SUBREG" ||
31 R->getName() == "INSERT_SUBREG") continue;
33 BitsInit *BI = R->getValueAsBitsInit("Inst");
35 unsigned numBits = BI->getNumBits();
36 BitsInit *NewBI = new BitsInit(numBits);
37 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
38 unsigned bitSwapIdx = numBits - bit - 1;
39 Init *OrigBit = BI->getBit(bit);
40 Init *BitSwap = BI->getBit(bitSwapIdx);
41 NewBI->setBit(bit, BitSwap);
42 NewBI->setBit(bitSwapIdx, OrigBit);
45 unsigned middle = (numBits + 1) / 2;
46 NewBI->setBit(middle, BI->getBit(middle));
49 // Update the bits in reversed order so that emitInstrOpBits will get the
50 // correct endianness.
51 R->getValue("Inst")->setValue(NewBI);
56 // If the VarBitInit at position 'bit' matches the specified variable then
57 // return the variable bit position. Otherwise return -1.
58 int CodeEmitterGen::getVariableBit(const std::string &VarName,
59 BitsInit *BI, int bit) {
60 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
61 TypedInit *TI = VBI->getVariable();
63 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
64 if (VI->getName() == VarName) return VBI->getBitNum();
72 void CodeEmitterGen::run(std::ostream &o) {
74 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
76 // For little-endian instruction bit encodings, reverse the bit order
77 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
79 EmitSourceFileHeader("Machine Code Emitter", o);
80 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
82 std::vector<const CodeGenInstruction*> NumberedInstructions;
83 Target.getInstructionsByEnumValue(NumberedInstructions);
85 // Emit function declaration
86 o << "unsigned " << Target.getName() << "CodeEmitter::"
87 << "getBinaryCodeForInstr(MachineInstr &MI) {\n";
89 // Emit instruction base values
90 o << " static const unsigned InstBits[] = {\n";
91 for (std::vector<const CodeGenInstruction*>::iterator
92 IN = NumberedInstructions.begin(),
93 EN = NumberedInstructions.end();
95 const CodeGenInstruction *CGI = *IN;
96 Record *R = CGI->TheDef;
98 if (IN != NumberedInstructions.begin()) o << ",\n";
100 if (R->getName() == "PHI" ||
101 R->getName() == "INLINEASM" ||
102 R->getName() == "LABEL" ||
103 R->getName() == "EXTRACT_SUBREG" ||
104 R->getName() == "INSERT_SUBREG") {
109 BitsInit *BI = R->getValueAsBitsInit("Inst");
111 // Start by filling in fixed values...
113 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
114 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
115 Value |= B->getValue() << (e-i-1);
118 o << " " << Value << "U";
122 // Map to accumulate all the cases.
123 std::map<std::string, std::vector<std::string> > CaseMap;
125 // Construct all cases statement for each opcode
126 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
129 const std::string &InstName = R->getName();
130 std::string Case("");
132 if (InstName == "PHI" ||
133 InstName == "INLINEASM" ||
134 InstName == "LABEL"||
135 InstName == "EXTRACT_SUBREG" ||
136 InstName == "INSERT_SUBREG") continue;
138 BitsInit *BI = R->getValueAsBitsInit("Inst");
139 const std::vector<RecordVal> &Vals = R->getValues();
140 CodeGenInstruction &CGI = Target.getInstruction(InstName);
142 // Loop over all of the fields in the instruction, determining which are the
143 // operands to the instruction.
145 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
146 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
147 // Is the operand continuous? If so, we can just mask and OR it in
148 // instead of doing it bit-by-bit, saving a lot in runtime cost.
149 const std::string &VarName = Vals[i].getName();
152 for (int bit = BI->getNumBits()-1; bit >= 0; ) {
153 int varBit = getVariableBit(VarName, BI, bit);
158 int beginInstBit = bit;
159 int beginVarBit = varBit;
162 for (--bit; bit >= 0;) {
163 varBit = getVariableBit(VarName, BI, bit);
164 if (varBit == -1 || varBit != (beginVarBit - N)) break;
170 /// If this operand is not supposed to be emitted by the generated
171 /// emitter, skip it.
172 while (CGI.isFlatOperandNotEmitted(op))
175 Case += " // op: " + VarName + "\n"
176 + " op = getMachineOpValue(MI, MI.getOperand("
177 + utostr(op++) + "));\n";
181 unsigned opMask = (1 << N) - 1;
182 int opShift = beginVarBit - N + 1;
184 opShift = beginInstBit - beginVarBit;
187 Case += " Value |= (op & " + utostr(opMask) + "U) << "
188 + itostr(opShift) + ";\n";
189 } else if (opShift < 0) {
190 Case += " Value |= (op & " + utostr(opMask) + "U) >> "
191 + itostr(-opShift) + ";\n";
193 Case += " Value |= op & " + utostr(opMask) + "U;\n";
200 std::vector<std::string> &InstList = CaseMap[Case];
201 InstList.push_back(InstName);
205 // Emit initial function code
206 o << " const unsigned opcode = MI.getOpcode();\n"
207 << " unsigned Value = InstBits[opcode];\n"
209 << " switch (opcode) {\n";
211 // Emit each case statement
212 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
213 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
214 const std::string &Case = IE->first;
215 std::vector<std::string> &InstList = IE->second;
217 for (int i = 0, N = InstList.size(); i < N; i++) {
219 o << " case " << Namespace << InstList[i] << ":";
227 // Default case: unhandled opcode
229 << " cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
232 << " return Value;\n"