2 #include "CodeEmitterGen.h"
5 void CodeEmitterGen::createEmitter(std::ostream &o) {
6 std::vector<Record*> Insts;
8 const std::map<std::string, Record*> &Defs = Records.getDefs();
9 Record *Inst = Records.getClass("Instruction");
10 assert(Inst && "Couldn't find Instruction class!");
12 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
13 E = Defs.end(); I != E; ++I)
14 if (I->second->isSubClassOf(Inst))
15 Insts.push_back(I->second);
17 std::string Namespace = "V9::";
18 std::string ClassName = "SparcV9CodeEmitter::";
20 //const std::string &Namespace = Inst->getValue("Namespace")->getName();
21 o << "static unsigned " << ClassName
22 << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
23 << " unsigned Value = 0;\n"
24 << " std::cerr << MI;\n"
25 << " switch (MI.getOpcode()) {\n";
26 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
30 o << " case " << Namespace << R->getName() << ": {\n";
32 const RecordVal *InstVal = R->getValue("Inst");
33 Init *InitVal = InstVal->getValue();
35 assert(dynamic_cast<BitsInit*>(InitVal) &&
36 "Can only handle undefined bits<> types!");
37 BitsInit *BI = (BitsInit*)InitVal;
40 const std::vector<RecordVal> &Vals = R->getValues();
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);
54 o << " // " << *InstVal << "\n\n";
55 o << " Value = " << Value << "U;\n\n";
57 // Loop over all of the fields in the instruction adding in any
58 // contributions to this value (due to bit references).
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 Vals[i].getName() != "annul" &&
66 Vals[i].getName() != "cc" &&
67 Vals[i].getName() != "predict")
69 o << " // op" << op << ": " << Vals[i].getName() << "\n"
70 << " int64_t op" << op
71 <<" = getMachineOpValue(MI.getOperand("<<op<<"));\n";
72 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
73 OpOrder[Vals[i].getName()] = op++;
78 for (int f = Vals.size()-1; f >= 0; --f) {
79 if (Vals[f].getPrefix()) {
80 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
82 // Scan through the field looking for bit initializers of the current
84 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
85 if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i))){
87 } else if (UnsetInit *UI =
88 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
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";
98 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
99 // FIXME: implement this!
100 o << "FIELD INIT not implemented yet!\n";
102 o << "Error: UNIMPLEMENTED\n";
107 if (Vals[f].getName() == "annul" || Vals[f].getName() == "cc" ||
108 Vals[f].getName() == "predict")
117 << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
120 << " return Value;\n"