CodeEmitterGen.cpp revision 7eac4766b156ad2965125a7ee3f1f4834dfbb00b
1#include "Record.h" 2#include "CodeEmitterGen.h" 3 4void CodeEmitterGen::createEmitter(std::ostream &o) { 5 std::vector<Record*> Insts; 6 7 const std::map<std::string, Record*> &Defs = Records.getDefs(); 8 Record *Inst = Records.getClass("Instruction"); 9 assert(Inst && "Couldn't find Instruction class!"); 10 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); 15 16 std::string Namespace = "V9::"; 17 std::string ClassName = "SparcV9CodeEmitter::"; 18 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(); 26 I != E; ++I) 27 { 28 Record *R = *I; 29 o << " case " << Namespace << R->getName() << ": {\n" 30 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n"; 31 32 const RecordVal *InstVal = R->getValue("Inst"); 33 Init *InitVal = InstVal->getValue(); 34 35 assert(dynamic_cast<BitsInit*>(InitVal) && 36 "Can only handle undefined bits<> types!"); 37 BitsInit *BI = (BitsInit*)InitVal; 38 39 unsigned Value = 0; 40 const std::vector<RecordVal> &Vals = R->getValues(); 41 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); 47 o << B->getValue(); 48 } else { 49 o << "0"; 50 } 51 } 52 o << "\n"; 53 54 o << " // " << *InstVal << "\n"; 55 o << " Value = " << Value << "U;\n\n"; 56 57 // Loop over all of the fields in the instruction adding in any 58 // contributions to this value (due to bit references). 59 // 60 unsigned op = 0; 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") 69 { 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++; 75 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; 82 83 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) { 84 if (VarBitInit *VBI = 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()) 90 continue; 91 92 if (beginBitInVar == -1) 93 beginBitInVar = VBI->getBitNum(); 94 95 if (endBitInVar == -1) 96 endBitInVar = VBI->getBitNum(); 97 else { 98 if (endBitInVar == (int)VBI->getBitNum() + 1) 99 endBitInVar = VBI->getBitNum(); 100 else { 101 continuous = false; 102 break; 103 } 104 } 105 106 if (beginBitInInst == -1) 107 beginBitInInst = bit; 108 if (endBitInInst == -1) 109 endBitInInst = bit; 110 else { 111 if (endBitInInst == bit + 1) 112 endBitInInst = bit; 113 else { 114 continuous = false; 115 break; 116 } 117 } 118 119 // maintain same distance between bits in field and bits in 120 // instruction. if the relative distances stay the same 121 // throughout, 122 if ((beginBitInVar - (int)VBI->getBitNum()) != 123 (beginBitInInst - bit)) 124 { 125 continuous = false; 126 break; 127 } 128 } 129 } 130 } 131 132 if (continuous) { 133 o << " // continuous: op" << OpOrder[Vals[i].getName()] << "\n"; 134 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; 141 endBitInVar = 0; 142 } 143 144 // High mask 145 o << " op" << OpOrder[Vals[i].getName()] 146 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n"; 147 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"; 152 153 // Just OR in the result 154 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n"; 155 } 156 157 // otherwise, will be taken care of in the loop below using this value: 158 OpContinuous[Vals[i].getName()] = continuous; 159 } 160 } 161 162 for (unsigned f = 0, e = Vals.size(); f != e; ++f) { 163 if (Vals[f].getPrefix()) { 164 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue(); 165 166 // Scan through the field looking for bit initializers of the current 167 // variable... 168 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) { 169 if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i))) 170 { 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 185 186 // for debugging, output the regular version anyway, commented 187 o << " // Value |= getValueBit(op" 188 << OpOrder[VI->getName()] << ", " << VBI->getBitNum() 189 << ")" << " << " << i << ";\n"; 190 } else { 191 o << " Value |= getValueBit(op" << OpOrder[VI->getName()] 192 << ", " << VBI->getBitNum() 193 << ")" << " << " << i << ";\n"; 194 } 195 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) { 196 // FIXME: implement this! 197 o << "FIELD INIT not implemented yet!\n"; 198 } else { 199 o << "Error: UNIMPLEMENTED\n"; 200 } 201 } 202 } 203 } else { 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"; 207 } 208 } 209 } 210 211 o << " break;\n" 212 << " }\n"; 213 } 214 215 o << " default:\n" 216 << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n" 217 << " abort();\n" 218 << " }\n" 219 << " return Value;\n" 220 << "}\n"; 221} 222