1 #include "Support/Statistic.h"
3 #include "CodeEmitterGen.h"
5 bool CodeEmitterGen::run(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");
35 std::cerr << "No 'Inst' record found in target description file!\n";
39 Init *InitVal = InstVal->getValue();
40 assert(dynamic_cast<BitsInit*>(InitVal) &&
41 "Can only handle undefined bits<> types!");
42 BitsInit *BI = (BitsInit*)InitVal;
45 const std::vector<RecordVal> &Vals = R->getValues();
47 DEBUG(o << " // prefilling: ");
48 // Start by filling in fixed values...
49 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
50 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
51 Value |= B->getValue() << (e-i-1);
52 DEBUG(o << B->getValue());
59 DEBUG(o << " // " << *InstVal << "\n");
60 o << " Value = " << Value << "U;\n\n";
62 // Loop over all of the fields in the instruction adding in any
63 // contributions to this value (due to bit references).
66 std::map<const std::string,unsigned> OpOrder;
67 std::map<const std::string,bool> OpContinuous;
68 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
69 if (Vals[i].getName() != "Inst" &&
70 !Vals[i].getValue()->isComplete() &&
71 /* ignore annul and predict bits since no one sets them yet */
72 Vals[i].getName() != "annul" &&
73 Vals[i].getName() != "predict")
75 o << " // op" << op << ": " << Vals[i].getName() << "\n"
76 << " int64_t op" << op
77 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
78 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
79 OpOrder[Vals[i].getName()] = op++;
81 // Is the operand continuous? If so, we can just mask and OR it in
82 // instead of doing it bit-by-bit, saving a lot in runtime cost.
83 const BitsInit *InstInit = BI;
84 int beginBitInVar = -1, endBitInVar = -1,
85 beginBitInInst = -1, endBitInInst = -1;
86 bool continuous = true;
88 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
90 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
91 TypedInit *TI = VBI->getVariable();
92 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
93 // only process the current variable
94 if (VI->getName() != Vals[i].getName())
97 if (beginBitInVar == -1)
98 beginBitInVar = VBI->getBitNum();
100 if (endBitInVar == -1)
101 endBitInVar = VBI->getBitNum();
103 if (endBitInVar == (int)VBI->getBitNum() + 1)
104 endBitInVar = VBI->getBitNum();
111 if (beginBitInInst == -1)
112 beginBitInInst = bit;
113 if (endBitInInst == -1)
116 if (endBitInInst == bit + 1)
124 // maintain same distance between bits in field and bits in
125 // instruction. if the relative distances stay the same
127 if ((beginBitInVar - (int)VBI->getBitNum()) !=
128 (beginBitInInst - bit))
137 DEBUG(o << " // Var: begin = " << beginBitInVar
138 << ", end = " << endBitInVar
139 << "; Inst: begin = " << beginBitInInst
140 << ", end = " << endBitInInst << "\n");
143 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
146 // Mask off the right bits
147 // Low mask (ie. shift, if necessary)
148 if (endBitInVar != 0) {
149 o << " op" << OpOrder[Vals[i].getName()]
150 << " >>= " << endBitInVar << ";\n";
151 beginBitInVar -= endBitInVar;
156 o << " op" << OpOrder[Vals[i].getName()]
157 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
159 // Shift the value to the correct place (according to place in instr)
160 if (endBitInInst != 0)
161 o << " op" << OpOrder[Vals[i].getName()]
162 << " <<= " << endBitInInst << ";\n";
164 // Just OR in the result
165 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
168 // otherwise, will be taken care of in the loop below using this value:
169 OpContinuous[Vals[i].getName()] = continuous;
173 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
174 if (Vals[f].getPrefix()) {
175 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
177 // Scan through the field looking for bit initializers of the current
179 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
180 if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
182 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
183 } else if (UnsetInit *UI =
184 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
185 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
186 } else if (VarBitInit *VBI =
187 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
188 TypedInit *TI = VBI->getVariable();
189 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
190 // If the bits of the field are laid out consecutively in the
191 // instruction, then instead of separately ORing in bits, just
192 // mask and shift the entire field for efficiency.
193 if (OpContinuous[VI->getName()]) {
194 // already taken care of in the loop above, thus there is no
195 // need to individually OR in the bits
197 // for debugging, output the regular version anyway, commented
198 DEBUG(o << " // Value |= getValueBit(op"
199 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
200 << ")" << " << " << i << ";\n");
202 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
203 << ", " << VBI->getBitNum()
204 << ")" << " << " << i << ";\n";
206 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
207 // FIXME: implement this!
208 o << "FIELD INIT not implemented yet!\n";
210 o << "Error: UNIMPLEMENTED\n";
215 // ignore annul and predict bits since no one sets them yet
216 if (Vals[f].getName() == "annul" || Vals[f].getName() == "predict") {
217 o << " // found " << Vals[f].getName() << "\n";
227 << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n"
230 << " return Value;\n"