1 //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
5 //===----------------------------------------------------------------------===//
7 #include "CodeEmitterGen.h"
9 #include "Support/Debug.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 BitsInit *BI = R->getValueAsBitsInit("Inst");
32 const std::vector<RecordVal> &Vals = R->getValues();
34 DEBUG(o << " // prefilling: ");
35 // Start by filling in fixed values...
36 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
37 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
38 Value |= B->getValue() << (e-i-1);
39 DEBUG(o << B->getValue());
46 DEBUG(o << " // " << *R->getValue("Inst") << "\n");
47 o << " Value = " << Value << "U;\n\n";
49 // Loop over all of the fields in the instruction determining which are the
50 // operands to the instruction.
53 std::map<std::string, unsigned> OpOrder;
54 std::map<std::string, bool> OpContinuous;
55 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
56 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete() &&
57 /* ignore annul and predict bits since no one sets them yet */
58 Vals[i].getName() != "annul" && Vals[i].getName() != "predict")
60 // Is the operand continuous? If so, we can just mask and OR it in
61 // instead of doing it bit-by-bit, saving a lot in runtime cost.
62 const BitsInit *InstInit = BI;
63 int beginBitInVar = -1, endBitInVar = -1;
64 int beginBitInInst = -1, endBitInInst = -1;
65 bool continuous = true;
67 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
69 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
70 TypedInit *TI = VBI->getVariable();
71 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
72 // only process the current variable
73 if (VI->getName() != Vals[i].getName())
76 if (beginBitInVar == -1)
77 beginBitInVar = VBI->getBitNum();
79 if (endBitInVar == -1)
80 endBitInVar = VBI->getBitNum();
82 if (endBitInVar == (int)VBI->getBitNum() + 1)
83 endBitInVar = VBI->getBitNum();
90 if (beginBitInInst == -1)
92 if (endBitInInst == -1)
95 if (endBitInInst == bit + 1)
103 // maintain same distance between bits in field and bits in
104 // instruction. if the relative distances stay the same
106 if (beginBitInVar - (int)VBI->getBitNum() !=
107 beginBitInInst - bit) {
115 if (beginBitInInst != -1) {
116 o << " // op" << op << ": " << Vals[i].getName() << "\n"
117 << " int64_t op" << op
118 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
119 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
120 OpOrder[Vals[i].getName()] = op++;
122 DEBUG(o << " // Var: begin = " << beginBitInVar
123 << ", end = " << endBitInVar
124 << "; Inst: begin = " << beginBitInInst
125 << ", end = " << endBitInInst << "\n");
128 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
131 // Mask off the right bits
132 // Low mask (ie. shift, if necessary)
133 if (endBitInVar != 0) {
134 o << " op" << OpOrder[Vals[i].getName()]
135 << " >>= " << endBitInVar << ";\n";
136 beginBitInVar -= endBitInVar;
141 o << " op" << OpOrder[Vals[i].getName()]
142 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
144 // Shift the value to the correct place (according to place in inst)
145 if (endBitInInst != 0)
146 o << " op" << OpOrder[Vals[i].getName()]
147 << " <<= " << endBitInInst << ";\n";
149 // Just OR in the result
150 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
153 // otherwise, will be taken care of in the loop below using this
155 OpContinuous[Vals[i].getName()] = continuous;
160 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
161 if (Vals[f].getPrefix()) {
162 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
164 // Scan through the field looking for bit initializers of the current
166 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
167 if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
169 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
170 } else if (UnsetInit *UI =
171 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
172 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
173 } else if (VarBitInit *VBI =
174 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
175 TypedInit *TI = VBI->getVariable();
176 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
177 // If the bits of the field are laid out consecutively in the
178 // instruction, then instead of separately ORing in bits, just
179 // mask and shift the entire field for efficiency.
180 if (OpContinuous[VI->getName()]) {
181 // already taken care of in the loop above, thus there is no
182 // need to individually OR in the bits
184 // for debugging, output the regular version anyway, commented
185 DEBUG(o << " // Value |= getValueBit(op"
186 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
187 << ")" << " << " << i << ";\n");
189 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
190 << ", " << VBI->getBitNum()
191 << ")" << " << " << i << ";\n";
193 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
194 // FIXME: implement this!
195 o << "FIELD INIT not implemented yet!\n";
197 o << "Error: UNIMPLEMENTED\n";
202 // ignore annul and predict bits since no one sets them yet
203 if (Vals[f].getName() == "annul" || Vals[f].getName() == "predict") {
204 o << " // found " << Vals[f].getName() << "\n";
214 << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n"
217 << " return Value;\n"