Reduce bloat in target libraries by removing per machine instruction assertion
[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     << "  switch (MI.getOpcode()) {\n";
86
87   // Emit a case statement for each opcode
88   for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
89        I != E; ++I) {
90     Record *R = *I;
91     if (R->getName() == "PHI" || R->getName() == "INLINEASM") continue;
92     
93     o << "    case " << Namespace << R->getName() << ": {\n";
94
95     BitsInit *BI = R->getValueAsBitsInit("Inst");
96
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);
107       }
108       if (numBits % 2) {
109         unsigned middle = (numBits + 1) / 2;
110         NewBI->setBit(middle, BI->getBit(middle));
111       }
112       BI = NewBI;
113       
114       // Update the bits in reversed order so that emitInstrOpBits will get the
115       // correct endianness.
116       R->getValue("Inst")->setValue(NewBI);
117     }
118
119     unsigned Value = 0;
120     const std::vector<RecordVal> &Vals = R->getValues();
121
122     DEBUG(o << "      // prefilling: ");
123     // Start by filling in fixed values...
124     for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
125       if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
126         Value |= B->getValue() << (e-i-1);
127         DEBUG(o << B->getValue());
128       } else {
129         DEBUG(o << "0");
130       }
131     }
132     DEBUG(o << "\n");
133
134     DEBUG(o << "      // " << *R->getValue("Inst") << "\n");
135     o << "      Value = " << Value << "U;\n\n";
136
137     // Loop over all of the fields in the instruction, determining which are the
138     // operands to the instruction.
139     unsigned op = 0;
140     std::map<std::string, unsigned> OpOrder;
141     std::map<std::string, bool> OpContinuous;
142     for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
143       if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
144         // Is the operand continuous? If so, we can just mask and OR it in
145         // instead of doing it bit-by-bit, saving a lot in runtime cost.
146         BitsInit *InstInit = BI;
147         int beginBitInVar = -1, endBitInVar = -1;
148         int beginBitInInst = -1, endBitInInst = -1;
149         bool continuous = true;
150
151         for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
152           if (VarBitInit *VBI =
153               dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
154             TypedInit *TI = VBI->getVariable();
155             if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
156               // only process the current variable
157               if (VI->getName() != Vals[i].getName())
158                 continue;
159
160               if (beginBitInVar == -1)
161                 beginBitInVar = VBI->getBitNum();
162
163               if (endBitInVar == -1)
164                 endBitInVar = VBI->getBitNum();
165               else {
166                 if (endBitInVar == (int)VBI->getBitNum() + 1)
167                   endBitInVar = VBI->getBitNum();
168                 else {
169                   continuous = false;
170                   break;
171                 }
172               }
173
174               if (beginBitInInst == -1)
175                 beginBitInInst = bit;
176               if (endBitInInst == -1)
177                 endBitInInst = bit;
178               else {
179                 if (endBitInInst == bit + 1)
180                   endBitInInst = bit;
181                 else {
182                   continuous = false;
183                   break;
184                 }
185               }
186
187               // maintain same distance between bits in field and bits in
188               // instruction. if the relative distances stay the same
189               // throughout,
190               if (beginBitInVar - (int)VBI->getBitNum() !=
191                   beginBitInInst - bit) {
192                 continuous = false;
193                 break;
194               }
195             }
196           }
197         }
198
199         // If we have found no bit in "Inst" which comes from this field, then
200         // this is not an operand!!
201         if (beginBitInInst != -1) {
202           o << "      // op" << op << ": " << Vals[i].getName() << "\n"
203             << "      int op" << op
204             <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
205           //<< "   MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
206           OpOrder[Vals[i].getName()] = op++;
207
208           DEBUG(o << "      // Var: begin = " << beginBitInVar
209                   << ", end = " << endBitInVar
210                   << "; Inst: begin = " << beginBitInInst
211                   << ", end = " << endBitInInst << "\n");
212
213           if (continuous) {
214             DEBUG(o << "      // continuous: op" << OpOrder[Vals[i].getName()]
215                     << "\n");
216
217             // Mask off the right bits
218             // Low mask (ie. shift, if necessary)
219             assert(endBitInVar >= 0 && "Negative shift amount in masking!");
220             if (endBitInVar != 0) {
221               o << "      op" << OpOrder[Vals[i].getName()]
222                 << " >>= " << endBitInVar << ";\n";
223               beginBitInVar -= endBitInVar;
224               endBitInVar = 0;
225             }
226
227             // High mask
228             o << "      op" << OpOrder[Vals[i].getName()]
229               << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
230
231             // Shift the value to the correct place (according to place in inst)
232             assert(endBitInInst >= 0 && "Negative shift amount!");
233             if (endBitInInst != 0)
234               o << "      op" << OpOrder[Vals[i].getName()]
235               << " <<= " << endBitInInst << ";\n";
236
237             // Just OR in the result
238             o << "      Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
239           }
240
241           // otherwise, will be taken care of in the loop below using this
242           // value:
243           OpContinuous[Vals[i].getName()] = continuous;
244         }
245       }
246     }
247
248     emitInstrOpBits(o, Vals, OpOrder, OpContinuous);
249
250     o << "      break;\n"
251       << "    }\n";
252   }
253
254   // Default case: unhandled opcode
255   o << "  default:\n"
256     << "    std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
257     << "    abort();\n"
258     << "  }\n"
259     << "  return Value;\n"
260     << "}\n\n";
261 }