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 EmitSourceFileHeader("Machine Code Emitter", o);
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();
28 o << " case " << Namespace << R->getName() << ": {\n"
29 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
31 BitsInit *BI = R->getValueAsBitsInit("Inst");
34 const std::vector<RecordVal> &Vals = R->getValues();
36 DEBUG(o << " // prefilling: ");
37 // Start by filling in fixed values...
38 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
39 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
40 Value |= B->getValue() << (e-i-1);
41 DEBUG(o << B->getValue());
48 DEBUG(o << " // " << *R->getValue("Inst") << "\n");
49 o << " Value = " << Value << "U;\n\n";
51 // Loop over all of the fields in the instruction determining which are the
52 // operands to the instruction.
55 std::map<std::string, unsigned> OpOrder;
56 std::map<std::string, bool> OpContinuous;
57 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
58 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
59 // Is the operand continuous? If so, we can just mask and OR it in
60 // instead of doing it bit-by-bit, saving a lot in runtime cost.
61 const BitsInit *InstInit = BI;
62 int beginBitInVar = -1, endBitInVar = -1;
63 int beginBitInInst = -1, endBitInInst = -1;
64 bool continuous = true;
66 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
68 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
69 TypedInit *TI = VBI->getVariable();
70 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
71 // only process the current variable
72 if (VI->getName() != Vals[i].getName())
75 if (beginBitInVar == -1)
76 beginBitInVar = VBI->getBitNum();
78 if (endBitInVar == -1)
79 endBitInVar = VBI->getBitNum();
81 if (endBitInVar == (int)VBI->getBitNum() + 1)
82 endBitInVar = VBI->getBitNum();
89 if (beginBitInInst == -1)
91 if (endBitInInst == -1)
94 if (endBitInInst == bit + 1)
102 // maintain same distance between bits in field and bits in
103 // instruction. if the relative distances stay the same
105 if (beginBitInVar - (int)VBI->getBitNum() !=
106 beginBitInInst - bit) {
114 // If we have found no bit in "Inst" which comes from this field, then
115 // this is not an operand!!
116 if (beginBitInInst != -1) {
117 o << " // op" << op << ": " << Vals[i].getName() << "\n"
118 << " int64_t op" << op
119 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
120 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
121 OpOrder[Vals[i].getName()] = op++;
123 DEBUG(o << " // Var: begin = " << beginBitInVar
124 << ", end = " << endBitInVar
125 << "; Inst: begin = " << beginBitInInst
126 << ", end = " << endBitInInst << "\n");
129 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
132 // Mask off the right bits
133 // Low mask (ie. shift, if necessary)
134 assert(endBitInVar >= 0 && "Negative shift amount in masking!");
135 if (endBitInVar != 0) {
136 o << " op" << OpOrder[Vals[i].getName()]
137 << " >>= " << endBitInVar << ";\n";
138 beginBitInVar -= endBitInVar;
143 o << " op" << OpOrder[Vals[i].getName()]
144 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
146 // Shift the value to the correct place (according to place in inst)
147 assert(endBitInInst >= 0 && "Negative shift amount in inst position!");
148 if (endBitInInst != 0)
149 o << " op" << OpOrder[Vals[i].getName()]
150 << " <<= " << endBitInInst << ";\n";
152 // Just OR in the result
153 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
156 // otherwise, will be taken care of in the loop below using this
158 OpContinuous[Vals[i].getName()] = continuous;
163 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
164 if (Vals[f].getPrefix()) {
165 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
167 // Scan through the field looking for bit initializers of the current
169 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
170 if (BitInit *BI = dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
172 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
173 } else if (UnsetInit *UI =
174 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
175 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
176 } else if (VarBitInit *VBI =
177 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
178 TypedInit *TI = VBI->getVariable();
179 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
180 // If the bits of the field are laid out consecutively in the
181 // instruction, then instead of separately ORing in bits, just
182 // mask and shift the entire field for efficiency.
183 if (OpContinuous[VI->getName()]) {
184 // already taken care of in the loop above, thus there is no
185 // need to individually OR in the bits
187 // for debugging, output the regular version anyway, commented
188 DEBUG(o << " // Value |= getValueBit(op"
189 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
190 << ")" << " << " << i << ";\n");
192 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
193 << ", " << VBI->getBitNum()
194 << ")" << " << " << i << ";\n";
196 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
197 // FIXME: implement this!
198 o << "FIELD INIT not implemented yet!\n";
200 o << "Error: UNIMPLEMENTED\n";
212 << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n"
215 << " return Value;\n"