CodeEmitterGen.cpp revision 1cb65f2d6202bbfcea0a764ab1356cb574ef4d20
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 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") 68 { 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++; 74 } 75 } 76 77 unsigned Offset = 31; 78 for (int f = Vals.size()-1; f >= 0; --f) { 79 if (Vals[f].getPrefix()) { 80 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue(); 81 82 // Scan through the field looking for bit initializers of the current 83 // variable... 84 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) { 85 if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i))){ 86 --Offset; 87 } else if (UnsetInit *UI = 88 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) { 89 --Offset; 90 } else if (VarBitInit *VBI = 91 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) { 92 TypedInit *TI = VBI->getVariable(); 93 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) { 94 o << " Value |= getValueBit(op" << OpOrder[VI->getName()] 95 << ", " << VBI->getBitNum() 96 << ")" << " << " << Offset << ";\n"; 97 --Offset; 98 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) { 99 // FIXME: implement this! 100 o << "FIELD INIT not implemented yet!\n"; 101 } else { 102 o << "Error: UNIMPLEMENTED\n"; 103 } 104 } 105 } 106 } else { 107 // ignore annul and predict bits since no one sets them yet 108 if (Vals[f].getName() == "annul" || Vals[f].getName() == "predict") 109 --Offset; 110 } 111 } 112 113 o << " break;\n" 114 << " }\n"; 115 } 116 o << " default:\n" 117 << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n" 118 << " abort();\n" 119 << " }\n" 120 << " return Value;\n" 121 << "}\n"; 122} 123