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