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 // Emit function declaration
82 o << "unsigned " << Target.getName() << "CodeEmitter::"
83 << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
84 << " unsigned Value = 0;\n"
85 << " DEBUG(std::cerr << MI);\n"
86 << " switch (MI.getOpcode()) {\n";
88 // Emit a case statement for each opcode
89 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
92 o << " case " << Namespace << R->getName() << ": {\n"
93 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
95 BitsInit *BI = R->getValueAsBitsInit("Inst");
97 // For little-endian instruction bit encodings, reverse the bit order
98 if (Target.isLittleEndianEncoding()) {
99 unsigned numBits = BI->getNumBits();
100 BitsInit *NewBI = new BitsInit(numBits);
101 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
102 unsigned bitSwapIdx = numBits - bit - 1;
103 Init *OrigBit = BI->getBit(bit);
104 Init *BitSwap = BI->getBit(bitSwapIdx);
105 NewBI->setBit(bit, BitSwap);
106 NewBI->setBit(bitSwapIdx, OrigBit);
109 unsigned middle = (numBits + 1) / 2;
110 NewBI->setBit(middle, BI->getBit(middle));
116 const std::vector<RecordVal> &Vals = R->getValues();
118 DEBUG(o << " // prefilling: ");
119 // Start by filling in fixed values...
120 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
121 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
122 Value |= B->getValue() << (e-i-1);
123 DEBUG(o << B->getValue());
130 DEBUG(o << " // " << *R->getValue("Inst") << "\n");
131 o << " Value = " << Value << "U;\n\n";
133 // Loop over all of the fields in the instruction, determining which are the
134 // operands to the instruction.
136 std::map<std::string, unsigned> OpOrder;
137 std::map<std::string, bool> OpContinuous;
138 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
139 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
140 // Is the operand continuous? If so, we can just mask and OR it in
141 // instead of doing it bit-by-bit, saving a lot in runtime cost.
142 BitsInit *InstInit = BI;
143 int beginBitInVar = -1, endBitInVar = -1;
144 int beginBitInInst = -1, endBitInInst = -1;
145 bool continuous = true;
147 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
148 if (VarBitInit *VBI =
149 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
150 TypedInit *TI = VBI->getVariable();
151 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
152 // only process the current variable
153 if (VI->getName() != Vals[i].getName())
156 if (beginBitInVar == -1)
157 beginBitInVar = VBI->getBitNum();
159 if (endBitInVar == -1)
160 endBitInVar = VBI->getBitNum();
162 if (endBitInVar == (int)VBI->getBitNum() + 1)
163 endBitInVar = VBI->getBitNum();
170 if (beginBitInInst == -1)
171 beginBitInInst = bit;
172 if (endBitInInst == -1)
175 if (endBitInInst == bit + 1)
183 // maintain same distance between bits in field and bits in
184 // instruction. if the relative distances stay the same
186 if (beginBitInVar - (int)VBI->getBitNum() !=
187 beginBitInInst - bit) {
195 // If we have found no bit in "Inst" which comes from this field, then
196 // this is not an operand!!
197 if (beginBitInInst != -1) {
198 o << " // op" << op << ": " << Vals[i].getName() << "\n"
200 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
201 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
202 OpOrder[Vals[i].getName()] = op++;
204 DEBUG(o << " // Var: begin = " << beginBitInVar
205 << ", end = " << endBitInVar
206 << "; Inst: begin = " << beginBitInInst
207 << ", end = " << endBitInInst << "\n");
210 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
213 // Mask off the right bits
214 // Low mask (ie. shift, if necessary)
215 assert(endBitInVar >= 0 && "Negative shift amount in masking!");
216 if (endBitInVar != 0) {
217 o << " op" << OpOrder[Vals[i].getName()]
218 << " >>= " << endBitInVar << ";\n";
219 beginBitInVar -= endBitInVar;
224 o << " op" << OpOrder[Vals[i].getName()]
225 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
227 // Shift the value to the correct place (according to place in inst)
228 assert(endBitInInst >= 0 && "Negative shift amount!");
229 if (endBitInInst != 0)
230 o << " op" << OpOrder[Vals[i].getName()]
231 << " <<= " << endBitInInst << ";\n";
233 // Just OR in the result
234 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
237 // otherwise, will be taken care of in the loop below using this
239 OpContinuous[Vals[i].getName()] = continuous;
244 emitInstrOpBits(o, Vals, OpOrder, OpContinuous);
250 // Default case: unhandled opcode
252 << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
255 << " return Value;\n"