1 //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
5 //===----------------------------------------------------------------------===//
7 #include "CodeEmitterGen.h"
9 #include "Support/Statistic.h"
11 bool CodeEmitterGen::run(std::ostream &o) {
12 std::vector<Record*> Insts;
14 const std::map<std::string, Record*> &Defs = Records.getDefs();
15 Record *Inst = Records.getClass("Instruction");
16 assert(Inst && "Couldn't find Instruction class!");
18 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
19 E = Defs.end(); I != E; ++I)
20 if (I->second->isSubClassOf(Inst))
21 Insts.push_back(I->second);
23 std::string Namespace = "V9::";
24 std::string ClassName = "SparcV9CodeEmitter::";
26 //const std::string &Namespace = Inst->getValue("Namespace")->getName();
27 o << "unsigned " << ClassName
28 << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
29 << " unsigned Value = 0;\n"
30 << " DEBUG(std::cerr << MI);\n"
31 << " switch (MI.getOpcode()) {\n";
32 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
36 o << " case " << Namespace << R->getName() << ": {\n"
37 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
39 const RecordVal *InstVal = R->getValue("Inst");
41 std::cerr << "No 'Inst' record found in target description file!\n";
45 Init *InitVal = InstVal->getValue();
46 assert(dynamic_cast<BitsInit*>(InitVal) &&
47 "Can only handle undefined bits<> types!");
48 BitsInit *BI = (BitsInit*)InitVal;
51 const std::vector<RecordVal> &Vals = R->getValues();
53 DEBUG(o << " // prefilling: ");
54 // Start by filling in fixed values...
55 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
56 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
57 Value |= B->getValue() << (e-i-1);
58 DEBUG(o << B->getValue());
65 DEBUG(o << " // " << *InstVal << "\n");
66 o << " Value = " << Value << "U;\n\n";
68 // Loop over all of the fields in the instruction adding in any
69 // contributions to this value (due to bit references).
72 std::map<const std::string,unsigned> OpOrder;
73 std::map<const std::string,bool> OpContinuous;
74 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
75 if (Vals[i].getName() != "Inst" &&
76 !Vals[i].getValue()->isComplete() &&
77 /* ignore annul and predict bits since no one sets them yet */
78 Vals[i].getName() != "annul" &&
79 Vals[i].getName() != "predict")
81 o << " // op" << op << ": " << Vals[i].getName() << "\n"
82 << " int64_t op" << op
83 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
84 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
85 OpOrder[Vals[i].getName()] = op++;
87 // Is the operand continuous? If so, we can just mask and OR it in
88 // instead of doing it bit-by-bit, saving a lot in runtime cost.
89 const BitsInit *InstInit = BI;
90 int beginBitInVar = -1, endBitInVar = -1,
91 beginBitInInst = -1, endBitInInst = -1;
92 bool continuous = true;
94 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
96 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
97 TypedInit *TI = VBI->getVariable();
98 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
99 // only process the current variable
100 if (VI->getName() != Vals[i].getName())
103 if (beginBitInVar == -1)
104 beginBitInVar = VBI->getBitNum();
106 if (endBitInVar == -1)
107 endBitInVar = VBI->getBitNum();
109 if (endBitInVar == (int)VBI->getBitNum() + 1)
110 endBitInVar = VBI->getBitNum();
117 if (beginBitInInst == -1)
118 beginBitInInst = bit;
119 if (endBitInInst == -1)
122 if (endBitInInst == bit + 1)
130 // maintain same distance between bits in field and bits in
131 // instruction. if the relative distances stay the same
133 if ((beginBitInVar - (int)VBI->getBitNum()) !=
134 (beginBitInInst - bit))
143 DEBUG(o << " // Var: begin = " << beginBitInVar
144 << ", end = " << endBitInVar
145 << "; Inst: begin = " << beginBitInInst
146 << ", end = " << endBitInInst << "\n");
149 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
152 // Mask off the right bits
153 // Low mask (ie. shift, if necessary)
154 if (endBitInVar != 0) {
155 o << " op" << OpOrder[Vals[i].getName()]
156 << " >>= " << endBitInVar << ";\n";
157 beginBitInVar -= endBitInVar;
162 o << " op" << OpOrder[Vals[i].getName()]
163 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
165 // Shift the value to the correct place (according to place in instr)
166 if (endBitInInst != 0)
167 o << " op" << OpOrder[Vals[i].getName()]
168 << " <<= " << endBitInInst << ";\n";
170 // Just OR in the result
171 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
174 // otherwise, will be taken care of in the loop below using this value:
175 OpContinuous[Vals[i].getName()] = continuous;
179 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
180 if (Vals[f].getPrefix()) {
181 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
183 // Scan through the field looking for bit initializers of the current
185 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
186 if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
188 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
189 } else if (UnsetInit *UI =
190 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
191 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
192 } else if (VarBitInit *VBI =
193 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
194 TypedInit *TI = VBI->getVariable();
195 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
196 // If the bits of the field are laid out consecutively in the
197 // instruction, then instead of separately ORing in bits, just
198 // mask and shift the entire field for efficiency.
199 if (OpContinuous[VI->getName()]) {
200 // already taken care of in the loop above, thus there is no
201 // need to individually OR in the bits
203 // for debugging, output the regular version anyway, commented
204 DEBUG(o << " // Value |= getValueBit(op"
205 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
206 << ")" << " << " << i << ";\n");
208 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
209 << ", " << VBI->getBitNum()
210 << ")" << " << " << i << ";\n";
212 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
213 // FIXME: implement this!
214 o << "FIELD INIT not implemented yet!\n";
216 o << "Error: UNIMPLEMENTED\n";
221 // ignore annul and predict bits since no one sets them yet
222 if (Vals[f].getName() == "annul" || Vals[f].getName() == "predict") {
223 o << " // found " << Vals[f].getName() << "\n";
233 << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n"
236 << " return Value;\n"