2 #include "CodeEmitterGen.h"
4 void CodeEmitterGen::createEmitter(std::ostream &o) {
5 std::vector<Record*> Insts;
7 const std::map<std::string, Record*> &Defs = Records.getDefs();
8 Record *Inst = Records.getClass("Instruction");
9 assert(Inst && "Couldn't find Instruction class!");
11 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
12 E = Defs.end(); I != E; ++I)
13 if (I->second->isSubClassOf(Inst))
14 Insts.push_back(I->second);
16 std::string Namespace = "V9::";
17 std::string ClassName = "SparcV9CodeEmitter::";
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();
29 o << " case " << Namespace << R->getName() << ": {\n"
30 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
32 const RecordVal *InstVal = R->getValue("Inst");
33 Init *InitVal = InstVal->getValue();
35 assert(dynamic_cast<BitsInit*>(InitVal) &&
36 "Can only handle undefined bits<> types!");
37 BitsInit *BI = (BitsInit*)InitVal;
40 const std::vector<RecordVal> &Vals = R->getValues();
42 o << " // prefilling: ";
43 // Start by filling in fixed values...
44 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
45 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
46 Value |= B->getValue() << (e-i-1);
54 o << " // " << *InstVal << "\n";
55 o << " Value = " << Value << "U;\n\n";
57 // Loop over all of the fields in the instruction adding in any
58 // contributions to this value (due to bit references).
61 std::map<const std::string,unsigned> OpOrder;
62 std::map<const std::string,bool> OpContinuous;
63 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
64 if (Vals[i].getName() != "Inst" &&
65 !Vals[i].getValue()->isComplete() &&
66 /* ignore annul and predict bits since no one sets them yet */
67 Vals[i].getName() != "annul" &&
68 Vals[i].getName() != "predict")
70 o << " // op" << op << ": " << Vals[i].getName() << "\n"
71 << " int64_t op" << op
72 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
73 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
74 OpOrder[Vals[i].getName()] = op++;
76 // Is the operand continuous? If so, we can just mask and OR it in
77 // instead of doing it bit-by-bit, saving a lot in runtime cost.
78 const BitsInit *InstInit = BI;
79 int beginBitInVar = -1, endBitInVar = -1,
80 beginBitInInst = -1, endBitInInst = -1;
81 bool continuous = true;
83 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
85 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
86 TypedInit *TI = VBI->getVariable();
87 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
88 // only process the current variable
89 if (VI->getName() != Vals[i].getName())
92 if (beginBitInVar == -1)
93 beginBitInVar = VBI->getBitNum();
95 if (endBitInVar == -1)
96 endBitInVar = VBI->getBitNum();
98 if (endBitInVar == (int)VBI->getBitNum() + 1)
99 endBitInVar = VBI->getBitNum();
106 if (beginBitInInst == -1)
107 beginBitInInst = bit;
108 if (endBitInInst == -1)
111 if (endBitInInst == bit + 1)
119 // maintain same distance between bits in field and bits in
120 // instruction. if the relative distances stay the same
122 if ((beginBitInVar - (int)VBI->getBitNum()) !=
123 (beginBitInInst - bit))
133 o << " // continuous: op" << OpOrder[Vals[i].getName()] << "\n";
135 // Mask off the right bits
136 // Low mask (ie. shift, if necessary)
137 if (endBitInVar != 0) {
138 o << " op" << OpOrder[Vals[i].getName()]
139 << " >>= endBitInVar;\n";
140 beginBitInVar -= endBitInVar;
145 o << " op" << OpOrder[Vals[i].getName()]
146 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
148 // Shift the value to the correct place (according to place in instr)
149 if (endBitInInst != 0)
150 o << " op" << OpOrder[Vals[i].getName()]
151 << " <<= " << endBitInInst << ";\n";
153 // Just OR in the result
154 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
157 // otherwise, will be taken care of in the loop below using this value:
158 OpContinuous[Vals[i].getName()] = continuous;
162 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
163 if (Vals[f].getPrefix()) {
164 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
166 // Scan through the field looking for bit initializers of the current
168 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
169 if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
171 o << " // bit init: f: " << f << ", i: " << i << "\n";
172 } else if (UnsetInit *UI =
173 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
174 o << " // unset init: f: " << f << ", i: " << i << "\n";
175 } else if (VarBitInit *VBI =
176 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
177 TypedInit *TI = VBI->getVariable();
178 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
179 // If the bits of the field are laid out consecutively in the
180 // instruction, then instead of separately ORing in bits, just
181 // mask and shift the entire field for efficiency.
182 if (OpContinuous[VI->getName()]) {
183 // already taken care of in the loop above, thus there is no
184 // need to individually OR in the bits
186 // for debugging, output the regular version anyway, commented
187 o << " // Value |= getValueBit(op"
188 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
189 << ")" << " << " << i << ";\n";
191 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
192 << ", " << VBI->getBitNum()
193 << ")" << " << " << i << ";\n";
195 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
196 // FIXME: implement this!
197 o << "FIELD INIT not implemented yet!\n";
199 o << "Error: UNIMPLEMENTED\n";
204 // ignore annul and predict bits since no one sets them yet
205 if (Vals[f].getName() == "annul" || Vals[f].getName() == "predict") {
206 o << " // found " << Vals[f].getName() << "\n";
216 << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n"
219 << " return Value;\n"