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