1 //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
5 //===----------------------------------------------------------------------===//
7 #include "CodeEmitterGen.h"
9 #include "Support/Statistic.h"
11 void CodeEmitterGen::run(std::ostream &o) {
12 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
14 std::string Namespace = "V9::";
15 std::string ClassName = "SparcV9CodeEmitter::";
17 //const std::string &Namespace = Inst->getValue("Namespace")->getName();
18 o << "unsigned " << ClassName
19 << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
20 << " unsigned Value = 0;\n"
21 << " DEBUG(std::cerr << MI);\n"
22 << " switch (MI.getOpcode()) {\n";
23 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
26 o << " case " << Namespace << R->getName() << ": {\n"
27 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
29 const RecordVal *InstVal = R->getValue("Inst");
31 throw std::string("No 'Inst' record found in target description file!");
33 Init *InitVal = InstVal->getValue();
34 assert(dynamic_cast<BitsInit*>(InitVal) &&
35 "Can only handle undefined bits<> types!");
36 BitsInit *BI = (BitsInit*)InitVal;
39 const std::vector<RecordVal> &Vals = R->getValues();
41 DEBUG(o << " // prefilling: ");
42 // Start by filling in fixed values...
43 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
44 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
45 Value |= B->getValue() << (e-i-1);
46 DEBUG(o << B->getValue());
53 DEBUG(o << " // " << *InstVal << "\n");
54 o << " Value = " << Value << "U;\n\n";
56 // Loop over all of the fields in the instruction adding in any
57 // contributions to this value (due to bit references).
60 std::map<const std::string,unsigned> OpOrder;
61 std::map<const std::string,bool> OpContinuous;
62 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
63 if (Vals[i].getName() != "Inst" &&
64 !Vals[i].getValue()->isComplete() &&
65 /* ignore annul and predict bits since no one sets them yet */
66 Vals[i].getName() != "annul" &&
67 Vals[i].getName() != "predict")
69 o << " // op" << op << ": " << Vals[i].getName() << "\n"
70 << " int64_t op" << op
71 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
72 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
73 OpOrder[Vals[i].getName()] = op++;
75 // Is the operand continuous? If so, we can just mask and OR it in
76 // instead of doing it bit-by-bit, saving a lot in runtime cost.
77 const BitsInit *InstInit = BI;
78 int beginBitInVar = -1, endBitInVar = -1,
79 beginBitInInst = -1, endBitInInst = -1;
80 bool continuous = true;
82 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
84 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
85 TypedInit *TI = VBI->getVariable();
86 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
87 // only process the current variable
88 if (VI->getName() != Vals[i].getName())
91 if (beginBitInVar == -1)
92 beginBitInVar = VBI->getBitNum();
94 if (endBitInVar == -1)
95 endBitInVar = VBI->getBitNum();
97 if (endBitInVar == (int)VBI->getBitNum() + 1)
98 endBitInVar = VBI->getBitNum();
105 if (beginBitInInst == -1)
106 beginBitInInst = bit;
107 if (endBitInInst == -1)
110 if (endBitInInst == bit + 1)
118 // maintain same distance between bits in field and bits in
119 // instruction. if the relative distances stay the same
121 if ((beginBitInVar - (int)VBI->getBitNum()) !=
122 (beginBitInInst - bit))
131 DEBUG(o << " // Var: begin = " << beginBitInVar
132 << ", end = " << endBitInVar
133 << "; Inst: begin = " << beginBitInInst
134 << ", end = " << endBitInInst << "\n");
137 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
140 // Mask off the right bits
141 // Low mask (ie. shift, if necessary)
142 if (endBitInVar != 0) {
143 o << " op" << OpOrder[Vals[i].getName()]
144 << " >>= " << endBitInVar << ";\n";
145 beginBitInVar -= endBitInVar;
150 o << " op" << OpOrder[Vals[i].getName()]
151 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
153 // Shift the value to the correct place (according to place in instr)
154 if (endBitInInst != 0)
155 o << " op" << OpOrder[Vals[i].getName()]
156 << " <<= " << endBitInInst << ";\n";
158 // Just OR in the result
159 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
162 // otherwise, will be taken care of in the loop below using this value:
163 OpContinuous[Vals[i].getName()] = continuous;
167 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
168 if (Vals[f].getPrefix()) {
169 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
171 // Scan through the field looking for bit initializers of the current
173 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
174 if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
176 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
177 } else if (UnsetInit *UI =
178 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
179 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
180 } else if (VarBitInit *VBI =
181 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
182 TypedInit *TI = VBI->getVariable();
183 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
184 // If the bits of the field are laid out consecutively in the
185 // instruction, then instead of separately ORing in bits, just
186 // mask and shift the entire field for efficiency.
187 if (OpContinuous[VI->getName()]) {
188 // already taken care of in the loop above, thus there is no
189 // need to individually OR in the bits
191 // for debugging, output the regular version anyway, commented
192 DEBUG(o << " // Value |= getValueBit(op"
193 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
194 << ")" << " << " << i << ";\n");
196 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
197 << ", " << VBI->getBitNum()
198 << ")" << " << " << i << ";\n";
200 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
201 // FIXME: implement this!
202 o << "FIELD INIT not implemented yet!\n";
204 o << "Error: UNIMPLEMENTED\n";
209 // ignore annul and predict bits since no one sets them yet
210 if (Vals[f].getName() == "annul" || Vals[f].getName() == "predict") {
211 o << " // found " << Vals[f].getName() << "\n";
221 << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n"
224 << " return Value;\n"