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->getValueAsString("Namespace") == "TargetOpcode")
30 BitsInit *BI = R->getValueAsBitsInit("Inst");
32 unsigned numBits = BI->getNumBits();
33 BitsInit *NewBI = new BitsInit(numBits);
34 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
35 unsigned bitSwapIdx = numBits - bit - 1;
36 Init *OrigBit = BI->getBit(bit);
37 Init *BitSwap = BI->getBit(bitSwapIdx);
38 NewBI->setBit(bit, BitSwap);
39 NewBI->setBit(bitSwapIdx, OrigBit);
42 unsigned middle = (numBits + 1) / 2;
43 NewBI->setBit(middle, BI->getBit(middle));
46 // Update the bits in reversed order so that emitInstrOpBits will get the
47 // correct endianness.
48 R->getValue("Inst")->setValue(NewBI);
53 // If the VarBitInit at position 'bit' matches the specified variable then
54 // return the variable bit position. Otherwise return -1.
55 int CodeEmitterGen::getVariableBit(const std::string &VarName,
56 BitsInit *BI, int bit) {
57 if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) {
58 TypedInit *TI = VBI->getVariable();
60 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
61 if (VI->getName() == VarName) return VBI->getBitNum();
69 void CodeEmitterGen::run(raw_ostream &o) {
71 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
73 // For little-endian instruction bit encodings, reverse the bit order
74 if (Target.isLittleEndianEncoding()) reverseBits(Insts);
76 EmitSourceFileHeader("Machine Code Emitter", o);
77 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
79 const std::vector<const CodeGenInstruction*> &NumberedInstructions =
80 Target.getInstructionsByEnumValue();
82 // Emit function declaration
83 o << "unsigned " << Target.getName() << "CodeEmitter::"
84 << "getBinaryCodeForInstr(const MachineInstr &MI) const {\n";
86 // Emit instruction base values
87 o << " static const unsigned InstBits[] = {\n";
88 for (std::vector<const CodeGenInstruction*>::const_iterator
89 IN = NumberedInstructions.begin(),
90 EN = NumberedInstructions.end();
92 const CodeGenInstruction *CGI = *IN;
93 Record *R = CGI->TheDef;
95 if (R->getValueAsString("Namespace") == "TargetOpcode") {
100 BitsInit *BI = R->getValueAsBitsInit("Inst");
102 // Start by filling in fixed values...
104 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
105 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
106 Value |= B->getValue() << (e-i-1);
109 o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n";
113 // Map to accumulate all the cases.
114 std::map<std::string, std::vector<std::string> > CaseMap;
116 // Construct all cases statement for each opcode
117 for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end();
120 if (R->getValueAsString("Namespace") == "TargetOpcode")
122 const std::string &InstName = R->getName();
123 std::string Case("");
125 BitsInit *BI = R->getValueAsBitsInit("Inst");
126 const std::vector<RecordVal> &Vals = R->getValues();
127 CodeGenInstruction &CGI = Target.getInstruction(R);
129 // Loop over all of the fields in the instruction, determining which are the
130 // operands to the instruction.
131 unsigned NumberedOp = 0;
132 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
133 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
134 // Is the operand continuous? If so, we can just mask and OR it in
135 // instead of doing it bit-by-bit, saving a lot in runtime cost.
136 const std::string &VarName = Vals[i].getName();
139 for (int bit = BI->getNumBits()-1; bit >= 0; ) {
140 int varBit = getVariableBit(VarName, BI, bit);
145 int beginInstBit = bit;
146 int beginVarBit = varBit;
149 for (--bit; bit >= 0;) {
150 varBit = getVariableBit(VarName, BI, bit);
151 if (varBit == -1 || varBit != (beginVarBit - N)) break;
158 // If the operand matches by name, reference according to that
159 // operand number. Non-matching operands are assumed to be in
162 if (CGI.hasOperandNamed(VarName, OpIdx)) {
163 // Get the machine operand number for the indicated operand.
164 OpIdx = CGI.OperandList[OpIdx].MIOperandNo;
165 assert (!CGI.isFlatOperandNotEmitted(OpIdx) &&
166 "Explicitly used operand also marked as not emitted!");
168 /// If this operand is not supposed to be emitted by the
169 /// generated emitter, skip it.
170 while (CGI.isFlatOperandNotEmitted(NumberedOp))
172 OpIdx = NumberedOp++;
175 Case += " // op: " + VarName + "\n"
176 + " op = getMachineOpValue(MI, MI.getOperand("
177 + utostr(OpIdx) + "));\n";
181 unsigned opMask = ~0U >> (32-N);
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"
208 << " unsigned op = 0;\n"
209 << " op = op; // suppress warning\n"
210 << " switch (opcode) {\n";
212 // Emit each case statement
213 std::map<std::string, std::vector<std::string> >::iterator IE, EE;
214 for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
215 const std::string &Case = IE->first;
216 std::vector<std::string> &InstList = IE->second;
218 for (int i = 0, N = InstList.size(); i < N; i++) {
220 o << " case " << Namespace << InstList[i] << ":";
228 // Default case: unhandled opcode
230 << " std::string msg;\n"
231 << " raw_string_ostream Msg(msg);\n"
232 << " Msg << \"Not supported instr: \" << MI;\n"
233 << " report_fatal_error(Msg.str());\n"
235 << " return Value;\n"