1 //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
14 #include "CodeEmitterGen.h"
16 #include "Support/Debug.h"
19 void CodeEmitterGen::run(std::ostream &o) {
20 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
22 EmitSourceFileHeader("Machine Code Emitter", o);
24 std::string Namespace = "V9::";
25 std::string ClassName = "SparcV9CodeEmitter::";
27 //const std::string &Namespace = Inst->getValue("Namespace")->getName();
28 o << "unsigned " << ClassName
29 << "getBinaryCodeForInstr(MachineInstr &MI) {\n"
30 << " unsigned Value = 0;\n"
31 << " DEBUG(std::cerr << MI);\n"
32 << " switch (MI.getOpcode()) {\n";
33 for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end();
36 o << " case " << Namespace << R->getName() << ": {\n"
37 << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n";
39 BitsInit *BI = R->getValueAsBitsInit("Inst");
42 const std::vector<RecordVal> &Vals = R->getValues();
44 DEBUG(o << " // prefilling: ");
45 // Start by filling in fixed values...
46 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
47 if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) {
48 Value |= B->getValue() << (e-i-1);
49 DEBUG(o << B->getValue());
56 DEBUG(o << " // " << *R->getValue("Inst") << "\n");
57 o << " Value = " << Value << "U;\n\n";
59 // Loop over all of the fields in the instruction determining which are the
60 // operands to the instruction.
63 std::map<std::string, unsigned> OpOrder;
64 std::map<std::string, bool> OpContinuous;
65 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
66 if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) {
67 // Is the operand continuous? If so, we can just mask and OR it in
68 // instead of doing it bit-by-bit, saving a lot in runtime cost.
69 const BitsInit *InstInit = BI;
70 int beginBitInVar = -1, endBitInVar = -1;
71 int beginBitInInst = -1, endBitInInst = -1;
72 bool continuous = true;
74 for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) {
76 dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) {
77 TypedInit *TI = VBI->getVariable();
78 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
79 // only process the current variable
80 if (VI->getName() != Vals[i].getName())
83 if (beginBitInVar == -1)
84 beginBitInVar = VBI->getBitNum();
86 if (endBitInVar == -1)
87 endBitInVar = VBI->getBitNum();
89 if (endBitInVar == (int)VBI->getBitNum() + 1)
90 endBitInVar = VBI->getBitNum();
97 if (beginBitInInst == -1)
99 if (endBitInInst == -1)
102 if (endBitInInst == bit + 1)
110 // maintain same distance between bits in field and bits in
111 // instruction. if the relative distances stay the same
113 if (beginBitInVar - (int)VBI->getBitNum() !=
114 beginBitInInst - bit) {
122 // If we have found no bit in "Inst" which comes from this field, then
123 // this is not an operand!!
124 if (beginBitInInst != -1) {
125 o << " // op" << op << ": " << Vals[i].getName() << "\n"
126 << " int64_t op" << op
127 <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n";
128 //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n";
129 OpOrder[Vals[i].getName()] = op++;
131 DEBUG(o << " // Var: begin = " << beginBitInVar
132 << ", end = " << endBitInVar
133 << "; Inst: begin = " << beginBitInInst
134 << ", end = " << endBitInInst << "\n");
137 DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()]
140 // Mask off the right bits
141 // Low mask (ie. shift, if necessary)
142 assert(endBitInVar >= 0 && "Negative shift amount in masking!");
143 if (endBitInVar != 0) {
144 o << " op" << OpOrder[Vals[i].getName()]
145 << " >>= " << endBitInVar << ";\n";
146 beginBitInVar -= endBitInVar;
151 o << " op" << OpOrder[Vals[i].getName()]
152 << " &= (1<<" << beginBitInVar+1 << ") - 1;\n";
154 // Shift the value to the correct place (according to place in inst)
155 assert(endBitInInst >= 0 && "Negative shift amount in inst position!");
156 if (endBitInInst != 0)
157 o << " op" << OpOrder[Vals[i].getName()]
158 << " <<= " << endBitInInst << ";\n";
160 // Just OR in the result
161 o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n";
164 // otherwise, will be taken care of in the loop below using this
166 OpContinuous[Vals[i].getName()] = continuous;
171 for (unsigned f = 0, e = Vals.size(); f != e; ++f) {
172 if (Vals[f].getPrefix()) {
173 BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue();
175 // Scan through the field looking for bit initializers of the current
177 for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) {
178 if (BitInit *BI = dynamic_cast<BitInit*>(FieldInitializer->getBit(i)))
180 DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n");
181 } else if (UnsetInit *UI =
182 dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) {
183 DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n");
184 } else if (VarBitInit *VBI =
185 dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) {
186 TypedInit *TI = VBI->getVariable();
187 if (VarInit *VI = dynamic_cast<VarInit*>(TI)) {
188 // If the bits of the field are laid out consecutively in the
189 // instruction, then instead of separately ORing in bits, just
190 // mask and shift the entire field for efficiency.
191 if (OpContinuous[VI->getName()]) {
192 // already taken care of in the loop above, thus there is no
193 // need to individually OR in the bits
195 // for debugging, output the regular version anyway, commented
196 DEBUG(o << " // Value |= getValueBit(op"
197 << OpOrder[VI->getName()] << ", " << VBI->getBitNum()
198 << ")" << " << " << i << ";\n");
200 o << " Value |= getValueBit(op" << OpOrder[VI->getName()]
201 << ", " << VBI->getBitNum()
202 << ")" << " << " << i << ";\n";
204 } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) {
205 // FIXME: implement this!
206 o << "FIELD INIT not implemented yet!\n";
208 o << "Error: UNIMPLEMENTED\n";
220 << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n"
223 << " return Value;\n"
226 EmitSourceFileTail(o);