1 #include "Support/Statistic.h"
3 #include "CodeEmitterGen.h"
5 void CodeEmitterGen::createEmitter(std::ostream &o) {
6 std::vector<Record*> Insts;
8 const std::map<std::string, Record*> &Defs = Records.getDefs();
9 Record *Inst = Records.getClass("Instruction");
10 assert(Inst && "Couldn't find Instruction class!");
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);
17 std::string Namespace = "V9::";
18 std::string ClassName = "SparcV9CodeEmitter::";
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();
30 o << " case " << Namespace << R->getName() << ": {\n"
31 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
33 const RecordVal *InstVal = R->getValue("Inst");
34 Init *InitVal = InstVal->getValue();
36 assert(dynamic_cast<BitsInit*>(InitVal) &&
37 "Can only handle undefined bits<> types!");
38 BitsInit *BI = (BitsInit*)InitVal;
41 const std::vector<RecordVal> &Vals = R->getValues();
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());
55 DEBUG(o << " // " << *InstVal << "\n");
56 o << " Value = " << Value << "U;\n\n";
58 // Loop over all of the fields in the instruction adding in any
59 // contributions to this value (due to bit references).
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")
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++;
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;
84 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
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())
93 if (beginBitInVar == -1)
94 beginBitInVar = VBI->getBitNum();
96 if (endBitInVar == -1)
97 endBitInVar = VBI->getBitNum();
99 if (endBitInVar == (int)VBI->getBitNum() + 1)
100 endBitInVar = VBI->getBitNum();
107 if (beginBitInInst == -1)
108 beginBitInInst = bit;
109 if (endBitInInst == -1)
112 if (endBitInInst == bit + 1)
120 // maintain same distance between bits in field and bits in
121 // instruction. if the relative distances stay the same
123 if ((beginBitInVar - (int)VBI->getBitNum()) !=
124 (beginBitInInst - bit))
133 DEBUG(o << " // Var: begin = " << beginBitInVar
134 << ", end = " << endBitInVar
135 << "; Inst: begin = " << beginBitInInst
136 << ", end = " << endBitInInst << "\n");
139 o << " // continuous: op" << OpOrder[Vals[i].getName()] << "\n";
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;
151 o << " op" << OpOrder[Vals[i].getName()]
152 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
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";
159 // Just OR in the result
160 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
163 // otherwise, will be taken care of in the loop below using this value:
164 OpContinuous[Vals[i].getName()] = continuous;
168 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
169 if (Vals[f].getPrefix()) {
170 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
172 // Scan through the field looking for bit initializers of the current
174 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
175 if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
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
192 // for debugging, output the regular version anyway, commented
193 DEBUG(o << " // Value |= getValueBit(op"
194 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
195 << ")" << " << " << i << ";\n");
197 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
198 << ", " << VBI->getBitNum()
199 << ")" << " << " << i << ";\n";
201 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
202 // FIXME: implement this!
203 o << "FIELD INIT not implemented yet!\n";
205 o << "Error: UNIMPLEMENTED\n";
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";
222 << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n"
225 << " return Value;\n"