More minor cleanups of the interface
[oota-llvm.git] / support / tools / TableGen / CodeEmitterGen.cpp
1 //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
2 //
3 // FIXME: Document.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "CodeEmitterGen.h"
8 #include "Record.h"
9 #include "Support/Statistic.h"
10
11 bool CodeEmitterGen::run(std::ostream &o) {
12   std::vector<Record*> Insts;
13
14   const std::map<std::string, Record*> &Defs = Records.getDefs();
15   Record *Inst = Records.getClass("Instruction");
16   assert(Inst && "Couldn't find Instruction class!");
17
18   for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
19          E = Defs.end(); I != E; ++I)
20     if (I->second->isSubClassOf(Inst))
21       Insts.push_back(I->second);
22
23   std::string Namespace = "V9::";
24   std::string ClassName = "SparcV9CodeEmitter::";
25
26   //const std::string &Namespace = Inst->getValue("Namespace")->getName();
27   o << "unsigned " << ClassName
28     << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
29     << "  unsigned Value = 0;\n"
30     << "  DEBUG(std::cerr << MI);\n"
31     << "  switch (MI.getOpcode()) {\n";
32   for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
33        I != E; ++I)
34   {
35     Record *R = *I;
36     o << "    case " << Namespace << R->getName() << ": {\n"
37       << "      DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
38
39     const RecordVal *InstVal = R->getValue("Inst");
40     if (!InstVal) {
41       std::cerr << "No 'Inst' record found in target description file!\n";
42       return true;
43     }
44
45     Init *InitVal = InstVal->getValue();
46     assert(dynamic_cast<BitsInit*>(InitVal) &&
47            "Can only handle undefined bits<> types!");
48     BitsInit *BI = (BitsInit*)InitVal;
49
50     unsigned Value = 0;
51     const std::vector<RecordVal> &Vals = R->getValues();
52
53     DEBUG(o << "      // prefilling: ");
54     // Start by filling in fixed values...
55     for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
56       if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
57         Value |= B->getValue() << (e-i-1);
58         DEBUG(o << B->getValue());
59       } else {
60         DEBUG(o << "0");
61       }
62     }
63     DEBUG(o << "\n");
64
65     DEBUG(o << "      // " << *InstVal << "\n");
66     o << "      Value = " << Value << "U;\n\n";
67     
68     // Loop over all of the fields in the instruction adding in any
69     // contributions to this value (due to bit references).
70     //
71     unsigned op = 0;
72     std::map<const std::string,unsigned> OpOrder;
73     std::map<const std::string,bool> OpContinuous;
74     for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
75       if (Vals[i].getName() != "Inst" && 
76           !Vals[i].getValue()->isComplete() &&
77           /* ignore annul and predict bits since no one sets them yet */
78           Vals[i].getName() != "annul" && 
79           Vals[i].getName() != "predict")
80       {
81         o << "      // op" << op << ": " << Vals[i].getName() << "\n"
82           << "      int64_t op" << op 
83           <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
84         //<< "      MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
85         OpOrder[Vals[i].getName()] = op++;
86
87         // Is the operand continuous? If so, we can just mask and OR it in
88         // instead of doing it bit-by-bit, saving a lot in runtime cost.        
89         const BitsInit *InstInit = BI;
90         int beginBitInVar = -1, endBitInVar = -1,
91           beginBitInInst = -1, endBitInInst = -1;
92         bool continuous = true;
93
94         for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
95           if (VarBitInit *VBI =
96               dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
97             TypedInit *TI = VBI->getVariable();
98             if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
99               // only process the current variable
100               if (VI->getName() != Vals[i].getName())
101                 continue;
102
103               if (beginBitInVar == -1)
104                 beginBitInVar = VBI->getBitNum();
105
106               if (endBitInVar == -1)
107                 endBitInVar = VBI->getBitNum();
108               else {
109                 if (endBitInVar == (int)VBI->getBitNum() + 1)
110                   endBitInVar = VBI->getBitNum();
111                 else {
112                   continuous = false;
113                   break;
114                 }
115               }
116
117               if (beginBitInInst == -1)
118                 beginBitInInst = bit;
119               if (endBitInInst == -1)
120                 endBitInInst = bit;
121               else {
122                 if (endBitInInst == bit + 1)
123                   endBitInInst = bit;
124                 else {
125                   continuous = false;
126                   break;
127                 }
128               }
129
130               // maintain same distance between bits in field and bits in
131               // instruction. if the relative distances stay the same
132               // throughout,
133               if ((beginBitInVar - (int)VBI->getBitNum()) !=
134                   (beginBitInInst - bit))
135               {
136                 continuous = false;
137                 break;
138               }
139             }
140           }
141         }
142
143         DEBUG(o << "      // Var: begin = " << beginBitInVar 
144                 << ", end = " << endBitInVar
145                 << "; Inst: begin = " << beginBitInInst
146                 << ", end = " << endBitInInst << "\n");
147
148         if (continuous) {
149           DEBUG(o << "      // continuous: op" << OpOrder[Vals[i].getName()]
150                   << "\n");
151           
152           // Mask off the right bits
153           // Low mask (ie. shift, if necessary)
154           if (endBitInVar != 0) {
155             o << "      op" << OpOrder[Vals[i].getName()]
156               << " >>= " << endBitInVar << ";\n";
157             beginBitInVar -= endBitInVar;
158             endBitInVar = 0;
159           }
160
161           // High mask
162           o << "      op" << OpOrder[Vals[i].getName()]
163             << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
164
165           // Shift the value to the correct place (according to place in instr)
166           if (endBitInInst != 0)
167             o << "      op" << OpOrder[Vals[i].getName()]
168               << " <<= " << endBitInInst << ";\n";
169
170           // Just OR in the result
171           o << "      Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
172         }
173
174         // otherwise, will be taken care of in the loop below using this value:
175         OpContinuous[Vals[i].getName()] = continuous;
176       }
177     }
178
179     for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
180       if (Vals[f].getPrefix()) {
181         BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
182
183         // Scan through the field looking for bit initializers of the current
184         // variable...
185         for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
186           if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
187           {
188             DEBUG(o << "      // bit init: f: " << f << ", i: " << i << "\n");
189           } else if (UnsetInit *UI =
190                      dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
191             DEBUG(o << "      // unset init: f: " << f << ", i: " << i << "\n");
192           } else if (VarBitInit *VBI =
193                      dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
194             TypedInit *TI = VBI->getVariable();
195             if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
196               // If the bits of the field are laid out consecutively in the
197               // instruction, then instead of separately ORing in bits, just
198               // mask and shift the entire field for efficiency.
199               if (OpContinuous[VI->getName()]) {
200                 // already taken care of in the loop above, thus there is no
201                 // need to individually OR in the bits
202
203                 // for debugging, output the regular version anyway, commented
204                 DEBUG(o << "      // Value |= getValueBit(op"
205                         << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
206                         << ")" << " << " << i << ";\n");
207               } else {
208                 o << "      Value |= getValueBit(op" << OpOrder[VI->getName()]
209                   << ", " << VBI->getBitNum()
210                   << ")" << " << " << i << ";\n";
211               }
212             } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
213               // FIXME: implement this!
214               o << "FIELD INIT not implemented yet!\n";
215             } else {
216               o << "Error: UNIMPLEMENTED\n";
217             }
218           }
219         }
220       } else {
221         // ignore annul and predict bits since no one sets them yet
222         if (Vals[f].getName() == "annul" || Vals[f].getName() == "predict") {
223           o << "      // found " << Vals[f].getName() << "\n";
224         }
225       }
226     }
227
228     o << "      break;\n"
229       << "    }\n";
230   }
231
232   o << "  default:\n"
233     << "    DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n"
234     << "    abort();\n"
235     << "  }\n"
236     << "  return Value;\n"
237     << "}\n";
238   return false;
239 }