1 //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Support/Debug.h"
22 void CodeEmitterGen::emitInstrOpBits(std::ostream &o,
23 const std::vector<RecordVal> &Vals,
24 std::map<std::string, unsigned> &OpOrder,
25 std::map<std::string, bool> &OpContinuous)
27 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
28 if (Vals[f].getPrefix()) {
29 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
31 // Scan through the field looking for bit initializers of the current
33 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
34 Init *I = FieldInitializer->getBit(i);
35 if (BitInit *BI = dynamic_cast<BitInit*>(I)) {
36 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
37 } else if (UnsetInit *UI = dynamic_cast<UnsetInit*>(I)) {
38 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
39 } else if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(I)) {
40 TypedInit *TI = VBI->getVariable();
41 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
42 // If the bits of the field are laid out consecutively in the
43 // instruction, then instead of separately ORing in bits, just
44 // mask and shift the entire field for efficiency.
45 if (OpContinuous[VI->getName()]) {
46 // already taken care of in the loop above, thus there is no
47 // need to individually OR in the bits
49 // for debugging, output the regular version anyway, commented
50 DEBUG(o << " // Value |= getValueBit(op"
51 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
52 << ")" << " << " << i << ";\n");
54 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
55 << ", " << VBI->getBitNum()
56 << ")" << " << " << i << ";\n";
58 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
59 // FIXME: implement this!
60 std::cerr << "Error: FieldInit not implemented!\n";
63 std::cerr << "Error: unimplemented case in "
64 << "CodeEmitterGen::emitInstrOpBits()\n";
74 void CodeEmitterGen::run(std::ostream &o) {
76 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
78 EmitSourceFileHeader("Machine Code Emitter", o);
79 std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
81 std::vector<const CodeGenInstruction*> NumberedInstructions;
82 Target.getInstructionsByEnumValue(NumberedInstructions);
84 // Emit function declaration
85 o << "unsigned " << Target.getName() << "CodeEmitter::"
86 << "getBinaryCodeForInstr(MachineInstr &MI) {\n";
88 // Emit instruction base values
89 o << " static const unsigned InstBits[] = {\n";
90 for (std::vector<const CodeGenInstruction*>::iterator
91 IN = NumberedInstructions.begin(),
92 EN = NumberedInstructions.end();
94 const CodeGenInstruction *CGI = *IN;
95 Record *R = CGI->TheDef;
97 if (IN != NumberedInstructions.begin()) o << ",\n";
99 if (R->getName() == "PHI" || R->getName() == "INLINEASM") {
104 BitsInit *BI = R->getValueAsBitsInit("Inst");
106 // For little-endian instruction bit encodings, reverse the bit order
107 if (Target.isLittleEndianEncoding()) {
108 unsigned numBits = BI->getNumBits();
109 BitsInit *NewBI = new BitsInit(numBits);
110 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
111 unsigned bitSwapIdx = numBits - bit - 1;
112 Init *OrigBit = BI->getBit(bit);
113 Init *BitSwap = BI->getBit(bitSwapIdx);
114 NewBI->setBit(bit, BitSwap);
115 NewBI->setBit(bitSwapIdx, OrigBit);
118 unsigned middle = (numBits + 1) / 2;
119 NewBI->setBit(middle, BI->getBit(middle));
123 // Update the bits in reversed order so that emitInstrOpBits will get the
124 // correct endianness.
125 R->getValue("Inst")->setValue(NewBI);
129 const std::vector<RecordVal> &Vals = R->getValues();
131 // Start by filling in fixed values...
132 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
133 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
134 Value |= B->getValue() << (e-i-1);
137 o << " " << Value << "U";
141 // Emit initial function code
142 o << " const unsigned opcode = MI.getOpcode();\n"
143 << " unsigned Value = InstBits[opcode];\n"
144 << " switch (opcode) {\n";
146 // Emit a case statement for each opcode
147 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
150 if (R->getName() == "PHI" || R->getName() == "INLINEASM") continue;
152 o << " case " << Namespace << R->getName() << ": {\n";
154 BitsInit *BI = R->getValueAsBitsInit("Inst");
155 const std::vector<RecordVal> &Vals = R->getValues();
157 // Loop over all of the fields in the instruction, determining which are the
158 // operands to the instruction.
160 std::map<std::string, unsigned> OpOrder;
161 std::map<std::string, bool> OpContinuous;
162 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
163 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
164 // Is the operand continuous? If so, we can just mask and OR it in
165 // instead of doing it bit-by-bit, saving a lot in runtime cost.
166 BitsInit *InstInit = BI;
167 int beginBitInVar = -1, endBitInVar = -1;
168 int beginBitInInst = -1, endBitInInst = -1;
169 bool continuous = true;
171 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
172 if (VarBitInit *VBI =
173 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
174 TypedInit *TI = VBI->getVariable();
175 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
176 // only process the current variable
177 if (VI->getName() != Vals[i].getName())
180 if (beginBitInVar == -1)
181 beginBitInVar = VBI->getBitNum();
183 if (endBitInVar == -1)
184 endBitInVar = VBI->getBitNum();
186 if (endBitInVar == (int)VBI->getBitNum() + 1)
187 endBitInVar = VBI->getBitNum();
194 if (beginBitInInst == -1)
195 beginBitInInst = bit;
196 if (endBitInInst == -1)
199 if (endBitInInst == bit + 1)
207 // maintain same distance between bits in field and bits in
208 // instruction. if the relative distances stay the same
210 if (beginBitInVar - (int)VBI->getBitNum() !=
211 beginBitInInst - bit) {
219 // If we have found no bit in "Inst" which comes from this field, then
220 // this is not an operand!!
221 if (beginBitInInst != -1) {
222 o << " // op" << op << ": " << Vals[i].getName() << "\n"
224 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
225 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
226 OpOrder[Vals[i].getName()] = op++;
228 DEBUG(o << " // Var: begin = " << beginBitInVar
229 << ", end = " << endBitInVar
230 << "; Inst: begin = " << beginBitInInst
231 << ", end = " << endBitInInst << "\n");
234 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
237 // Mask off the right bits
238 // Low mask (ie. shift, if necessary)
239 assert(endBitInVar >= 0 && "Negative shift amount in masking!");
240 if (endBitInVar != 0) {
241 o << " op" << OpOrder[Vals[i].getName()]
242 << " >>= " << endBitInVar << ";\n";
243 beginBitInVar -= endBitInVar;
248 o << " op" << OpOrder[Vals[i].getName()]
249 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
251 // Shift the value to the correct place (according to place in inst)
252 assert(endBitInInst >= 0 && "Negative shift amount!");
253 if (endBitInInst != 0)
254 o << " op" << OpOrder[Vals[i].getName()]
255 << " <<= " << endBitInInst << ";\n";
257 // Just OR in the result
258 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
261 // otherwise, will be taken care of in the loop below using this
263 OpContinuous[Vals[i].getName()] = continuous;
268 emitInstrOpBits(o, Vals, OpOrder, OpContinuous);
274 // Default case: unhandled opcode
276 << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
279 << " return Value;\n"