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