CodeEmitterGen.cpp revision c86516f35ccc64ad33acdd79c89aa4c4e33c6e70
1#include "Support/Statistic.h"
2#include "Record.h"
3#include "CodeEmitterGen.h"
4
5void 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          DEBUG(o << "      // continuous: op" << OpOrder[Vals[i].getName()]
140                  << "\n");
141
142          // Mask off the right bits
143          // Low mask (ie. shift, if necessary)
144          if (endBitInVar != 0) {
145            o << "      op" << OpOrder[Vals[i].getName()]
146              << " >>= " << endBitInVar << ";\n";
147            beginBitInVar -= endBitInVar;
148            endBitInVar = 0;
149          }
150
151          // High mask
152          o << "      op" << OpOrder[Vals[i].getName()]
153            << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
154
155          // Shift the value to the correct place (according to place in instr)
156          if (endBitInInst != 0)
157            o << "      op" << OpOrder[Vals[i].getName()]
158              << " <<= " << endBitInInst << ";\n";
159
160          // Just OR in the result
161          o << "      Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
162        }
163
164        // otherwise, will be taken care of in the loop below using this value:
165        OpContinuous[Vals[i].getName()] = continuous;
166      }
167    }
168
169    for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
170      if (Vals[f].getPrefix()) {
171        BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
172
173        // Scan through the field looking for bit initializers of the current
174        // variable...
175        for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
176          if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
177          {
178            DEBUG(o << "      // bit init: f: " << f << ", i: " << i << "\n");
179          } else if (UnsetInit *UI =
180                     dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
181            DEBUG(o << "      // unset init: f: " << f << ", i: " << i << "\n");
182          } else if (VarBitInit *VBI =
183                     dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
184            TypedInit *TI = VBI->getVariable();
185            if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
186              // If the bits of the field are laid out consecutively in the
187              // instruction, then instead of separately ORing in bits, just
188              // mask and shift the entire field for efficiency.
189              if (OpContinuous[VI->getName()]) {
190                // already taken care of in the loop above, thus there is no
191                // need to individually OR in the bits
192
193                // for debugging, output the regular version anyway, commented
194                DEBUG(o << "      // Value |= getValueBit(op"
195                        << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
196                        << ")" << " << " << i << ";\n");
197              } else {
198                o << "      Value |= getValueBit(op" << OpOrder[VI->getName()]
199                  << ", " << VBI->getBitNum()
200                  << ")" << " << " << i << ";\n";
201              }
202            } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
203              // FIXME: implement this!
204              o << "FIELD INIT not implemented yet!\n";
205            } else {
206              o << "Error: UNIMPLEMENTED\n";
207            }
208          }
209        }
210      } else {
211        // ignore annul and predict bits since no one sets them yet
212        if (Vals[f].getName() == "annul" || Vals[f].getName() == "predict") {
213          o << "      // found " << Vals[f].getName() << "\n";
214        }
215      }
216    }
217
218    o << "      break;\n"
219      << "    }\n";
220  }
221
222  o << "  default:\n"
223    << "    DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n"
224    << "    abort();\n"
225    << "  }\n"
226    << "  return Value;\n"
227    << "}\n";
228}
229