Don't emit JIT code for these instructions
[oota-llvm.git] / utils / TableGen / CodeEmitterGen.cpp
1 //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
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/Support/Debug.h"
20 using namespace llvm;
21
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)
26 {
27   for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
28     if (Vals[f].getPrefix()) {
29       BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
30
31       // Scan through the field looking for bit initializers of the current
32       // variable...
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
48
49               // for debugging, output the regular version anyway, commented
50               DEBUG(o << "      // Value |= getValueBit(op"
51                       << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
52                       << ")" << " << " << i << ";\n");
53             } else {
54               o << "      Value |= getValueBit(op" << OpOrder[VI->getName()]
55                 << ", " << VBI->getBitNum()
56                 << ")" << " << " << i << ";\n";
57             }
58           } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
59             // FIXME: implement this!
60             std::cerr << "Error: FieldInit not implemented!\n";
61             abort();
62           } else {
63             std::cerr << "Error: unimplemented case in "
64                       << "CodeEmitterGen::emitInstrOpBits()\n";
65             abort();
66           }
67         }
68       }
69     }
70   }
71 }
72
73
74 void CodeEmitterGen::run(std::ostream &o) {
75   CodeGenTarget Target;
76   std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
77
78   EmitSourceFileHeader("Machine Code Emitter", o);
79   std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::";
80
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";
87
88   // Emit a case statement for each opcode
89   for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
90        I != E; ++I) {
91     Record *R = *I;
92     if (R->getName() == "PHI" || R->getName() == "INLINEASM") continue;
93     
94     o << "    case " << Namespace << R->getName() << ": {\n"
95       << "      DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
96
97     BitsInit *BI = R->getValueAsBitsInit("Inst");
98
99     // For little-endian instruction bit encodings, reverse the bit order
100     if (Target.isLittleEndianEncoding()) {
101       unsigned numBits = BI->getNumBits();
102       BitsInit *NewBI = new BitsInit(numBits);
103       for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
104         unsigned bitSwapIdx = numBits - bit - 1;
105         Init *OrigBit = BI->getBit(bit);
106         Init *BitSwap = BI->getBit(bitSwapIdx);
107         NewBI->setBit(bit, BitSwap);
108         NewBI->setBit(bitSwapIdx, OrigBit);
109       }
110       if (numBits % 2) {
111         unsigned middle = (numBits + 1) / 2;
112         NewBI->setBit(middle, BI->getBit(middle));
113       }
114       BI = NewBI;
115     }
116
117     unsigned Value = 0;
118     const std::vector<RecordVal> &Vals = R->getValues();
119
120     DEBUG(o << "      // prefilling: ");
121     // Start by filling in fixed values...
122     for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
123       if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
124         Value |= B->getValue() << (e-i-1);
125         DEBUG(o << B->getValue());
126       } else {
127         DEBUG(o << "0");
128       }
129     }
130     DEBUG(o << "\n");
131
132     DEBUG(o << "      // " << *R->getValue("Inst") << "\n");
133     o << "      Value = " << Value << "U;\n\n";
134
135     // Loop over all of the fields in the instruction, determining which are the
136     // operands to the instruction.
137     unsigned op = 0;
138     std::map<std::string, unsigned> OpOrder;
139     std::map<std::string, bool> OpContinuous;
140     for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
141       if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
142         // Is the operand continuous? If so, we can just mask and OR it in
143         // instead of doing it bit-by-bit, saving a lot in runtime cost.
144         BitsInit *InstInit = BI;
145         int beginBitInVar = -1, endBitInVar = -1;
146         int beginBitInInst = -1, endBitInInst = -1;
147         bool continuous = true;
148
149         for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
150           if (VarBitInit *VBI =
151               dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
152             TypedInit *TI = VBI->getVariable();
153             if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
154               // only process the current variable
155               if (VI->getName() != Vals[i].getName())
156                 continue;
157
158               if (beginBitInVar == -1)
159                 beginBitInVar = VBI->getBitNum();
160
161               if (endBitInVar == -1)
162                 endBitInVar = VBI->getBitNum();
163               else {
164                 if (endBitInVar == (int)VBI->getBitNum() + 1)
165                   endBitInVar = VBI->getBitNum();
166                 else {
167                   continuous = false;
168                   break;
169                 }
170               }
171
172               if (beginBitInInst == -1)
173                 beginBitInInst = bit;
174               if (endBitInInst == -1)
175                 endBitInInst = bit;
176               else {
177                 if (endBitInInst == bit + 1)
178                   endBitInInst = bit;
179                 else {
180                   continuous = false;
181                   break;
182                 }
183               }
184
185               // maintain same distance between bits in field and bits in
186               // instruction. if the relative distances stay the same
187               // throughout,
188               if (beginBitInVar - (int)VBI->getBitNum() !=
189                   beginBitInInst - bit) {
190                 continuous = false;
191                 break;
192               }
193             }
194           }
195         }
196
197         // If we have found no bit in "Inst" which comes from this field, then
198         // this is not an operand!!
199         if (beginBitInInst != -1) {
200           o << "      // op" << op << ": " << Vals[i].getName() << "\n"
201             << "      int op" << op
202             <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
203           //<< "   MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
204           OpOrder[Vals[i].getName()] = op++;
205
206           DEBUG(o << "      // Var: begin = " << beginBitInVar
207                   << ", end = " << endBitInVar
208                   << "; Inst: begin = " << beginBitInInst
209                   << ", end = " << endBitInInst << "\n");
210
211           if (continuous) {
212             DEBUG(o << "      // continuous: op" << OpOrder[Vals[i].getName()]
213                     << "\n");
214
215             // Mask off the right bits
216             // Low mask (ie. shift, if necessary)
217             assert(endBitInVar >= 0 && "Negative shift amount in masking!");
218             if (endBitInVar != 0) {
219               o << "      op" << OpOrder[Vals[i].getName()]
220                 << " >>= " << endBitInVar << ";\n";
221               beginBitInVar -= endBitInVar;
222               endBitInVar = 0;
223             }
224
225             // High mask
226             o << "      op" << OpOrder[Vals[i].getName()]
227               << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
228
229             // Shift the value to the correct place (according to place in inst)
230             assert(endBitInInst >= 0 && "Negative shift amount!");
231             if (endBitInInst != 0)
232               o << "      op" << OpOrder[Vals[i].getName()]
233               << " <<= " << endBitInInst << ";\n";
234
235             // Just OR in the result
236             o << "      Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
237           }
238
239           // otherwise, will be taken care of in the loop below using this
240           // value:
241           OpContinuous[Vals[i].getName()] = continuous;
242         }
243       }
244     }
245
246     emitInstrOpBits(o, Vals, OpOrder, OpContinuous);
247
248     o << "      break;\n"
249       << "    }\n";
250   }
251
252   // Default case: unhandled opcode
253   o << "  default:\n"
254     << "    std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
255     << "    abort();\n"
256     << "  }\n"
257     << "  return Value;\n"
258     << "}\n\n";
259 }