Chris Lattner | c9670ef | 2003-07-31 04:43:49 +0000 | [diff] [blame] | 1 | //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===// |
| 2 | // |
| 3 | // FIXME: Document. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 7 | #include "CodeEmitterGen.h" |
Chris Lattner | c9670ef | 2003-07-31 04:43:49 +0000 | [diff] [blame] | 8 | #include "Record.h" |
| 9 | #include "Support/Statistic.h" |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 10 | |
Chris Lattner | 048c00d | 2003-08-01 04:38:18 +0000 | [diff] [blame] | 11 | void CodeEmitterGen::run(std::ostream &o) { |
| 12 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 13 | |
| 14 | std::string Namespace = "V9::"; |
| 15 | std::string ClassName = "SparcV9CodeEmitter::"; |
| 16 | |
| 17 | //const std::string &Namespace = Inst->getValue("Namespace")->getName(); |
Misha Brukman | b9dd815 | 2003-05-27 22:29:02 +0000 | [diff] [blame] | 18 | o << "unsigned " << ClassName |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 19 | << "getBinaryCodeForInstr(MachineInstr &MI) {\n" |
| 20 | << " unsigned Value = 0;\n" |
Misha Brukman | f4ef4c8 | 2003-06-06 00:27:02 +0000 | [diff] [blame] | 21 | << " DEBUG(std::cerr << MI);\n" |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 22 | << " switch (MI.getOpcode()) {\n"; |
| 23 | for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end(); |
Chris Lattner | cf1b585 | 2003-08-01 04:15:25 +0000 | [diff] [blame] | 24 | I != E; ++I) { |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 25 | Record *R = *I; |
Misha Brukman | ecc7fd3 | 2003-05-28 18:29:10 +0000 | [diff] [blame] | 26 | o << " case " << Namespace << R->getName() << ": {\n" |
Misha Brukman | f4ef4c8 | 2003-06-06 00:27:02 +0000 | [diff] [blame] | 27 | << " DEBUG(std::cerr << \"Emitting " << R->getName() << "\\n\");\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 6f334ad | 2003-08-01 04:46:24 +0000 | [diff] [blame] | 29 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 30 | |
| 31 | unsigned Value = 0; |
| 32 | const std::vector<RecordVal> &Vals = R->getValues(); |
| 33 | |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 34 | DEBUG(o << " // prefilling: "); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 35 | // Start by filling in fixed values... |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 36 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) { |
| 37 | if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) { |
| 38 | Value |= B->getValue() << (e-i-1); |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 39 | DEBUG(o << B->getValue()); |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 40 | } else { |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 41 | DEBUG(o << "0"); |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 44 | DEBUG(o << "\n"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 6f334ad | 2003-08-01 04:46:24 +0000 | [diff] [blame] | 46 | DEBUG(o << " // " << *R->getValue("Inst") << "\n"); |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 47 | o << " Value = " << Value << "U;\n\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 48 | |
| 49 | // Loop over all of the fields in the instruction adding in any |
| 50 | // contributions to this value (due to bit references). |
| 51 | // |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 52 | unsigned op = 0; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 53 | std::map<const std::string,unsigned> OpOrder; |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 54 | std::map<const std::string,bool> OpContinuous; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 55 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 56 | if (Vals[i].getName() != "Inst" && |
| 57 | !Vals[i].getValue()->isComplete() && |
Misha Brukman | 3d194ac | 2003-06-05 23:15:25 +0000 | [diff] [blame] | 58 | /* ignore annul and predict bits since no one sets them yet */ |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 59 | Vals[i].getName() != "annul" && |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 60 | Vals[i].getName() != "predict") |
| 61 | { |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 62 | o << " // op" << op << ": " << Vals[i].getName() << "\n" |
| 63 | << " int64_t op" << op |
Misha Brukman | e7800b5 | 2003-05-30 20:32:01 +0000 | [diff] [blame] | 64 | <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n"; |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 65 | //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n"; |
| 66 | OpOrder[Vals[i].getName()] = op++; |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 67 | |
| 68 | // Is the operand continuous? If so, we can just mask and OR it in |
| 69 | // instead of doing it bit-by-bit, saving a lot in runtime cost. |
| 70 | const BitsInit *InstInit = BI; |
| 71 | int beginBitInVar = -1, endBitInVar = -1, |
| 72 | beginBitInInst = -1, endBitInInst = -1; |
| 73 | bool continuous = true; |
| 74 | |
| 75 | for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) { |
| 76 | if (VarBitInit *VBI = |
| 77 | dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) { |
| 78 | TypedInit *TI = VBI->getVariable(); |
| 79 | if (VarInit *VI = dynamic_cast<VarInit*>(TI)) { |
| 80 | // only process the current variable |
| 81 | if (VI->getName() != Vals[i].getName()) |
| 82 | continue; |
| 83 | |
| 84 | if (beginBitInVar == -1) |
| 85 | beginBitInVar = VBI->getBitNum(); |
| 86 | |
| 87 | if (endBitInVar == -1) |
| 88 | endBitInVar = VBI->getBitNum(); |
| 89 | else { |
| 90 | if (endBitInVar == (int)VBI->getBitNum() + 1) |
| 91 | endBitInVar = VBI->getBitNum(); |
| 92 | else { |
| 93 | continuous = false; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (beginBitInInst == -1) |
| 99 | beginBitInInst = bit; |
| 100 | if (endBitInInst == -1) |
| 101 | endBitInInst = bit; |
| 102 | else { |
| 103 | if (endBitInInst == bit + 1) |
| 104 | endBitInInst = bit; |
| 105 | else { |
| 106 | continuous = false; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // maintain same distance between bits in field and bits in |
| 112 | // instruction. if the relative distances stay the same |
| 113 | // throughout, |
| 114 | if ((beginBitInVar - (int)VBI->getBitNum()) != |
| 115 | (beginBitInInst - bit)) |
| 116 | { |
| 117 | continuous = false; |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 124 | DEBUG(o << " // Var: begin = " << beginBitInVar |
| 125 | << ", end = " << endBitInVar |
| 126 | << "; Inst: begin = " << beginBitInInst |
| 127 | << ", end = " << endBitInInst << "\n"); |
| 128 | |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 129 | if (continuous) { |
Misha Brukman | c86516f | 2003-07-18 18:03:45 +0000 | [diff] [blame] | 130 | DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()] |
| 131 | << "\n"); |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 132 | |
| 133 | // Mask off the right bits |
| 134 | // Low mask (ie. shift, if necessary) |
| 135 | if (endBitInVar != 0) { |
| 136 | o << " op" << OpOrder[Vals[i].getName()] |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 137 | << " >>= " << endBitInVar << ";\n"; |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 138 | beginBitInVar -= endBitInVar; |
| 139 | endBitInVar = 0; |
| 140 | } |
| 141 | |
| 142 | // High mask |
| 143 | o << " op" << OpOrder[Vals[i].getName()] |
| 144 | << " &= (1<<" << beginBitInVar+1 << ") - 1;\n"; |
| 145 | |
| 146 | // Shift the value to the correct place (according to place in instr) |
| 147 | if (endBitInInst != 0) |
| 148 | o << " op" << OpOrder[Vals[i].getName()] |
| 149 | << " <<= " << endBitInInst << ";\n"; |
| 150 | |
| 151 | // Just OR in the result |
| 152 | o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n"; |
| 153 | } |
| 154 | |
| 155 | // otherwise, will be taken care of in the loop below using this value: |
| 156 | OpContinuous[Vals[i].getName()] = continuous; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Misha Brukman | 48aa824 | 2003-07-07 22:30:44 +0000 | [diff] [blame] | 160 | for (unsigned f = 0, e = Vals.size(); f != e; ++f) { |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 161 | if (Vals[f].getPrefix()) { |
| 162 | BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue(); |
| 163 | |
| 164 | // Scan through the field looking for bit initializers of the current |
| 165 | // variable... |
| 166 | for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) { |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 167 | if (BitInit *BI=dynamic_cast<BitInit*>(FieldInitializer->getBit(i))) |
| 168 | { |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 169 | DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n"); |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 170 | } else if (UnsetInit *UI = |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 171 | dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) { |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 172 | DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 173 | } else if (VarBitInit *VBI = |
| 174 | dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) { |
| 175 | TypedInit *TI = VBI->getVariable(); |
| 176 | if (VarInit *VI = dynamic_cast<VarInit*>(TI)) { |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 177 | // If the bits of the field are laid out consecutively in the |
| 178 | // instruction, then instead of separately ORing in bits, just |
| 179 | // mask and shift the entire field for efficiency. |
| 180 | if (OpContinuous[VI->getName()]) { |
| 181 | // already taken care of in the loop above, thus there is no |
| 182 | // need to individually OR in the bits |
| 183 | |
| 184 | // for debugging, output the regular version anyway, commented |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 185 | DEBUG(o << " // Value |= getValueBit(op" |
| 186 | << OpOrder[VI->getName()] << ", " << VBI->getBitNum() |
| 187 | << ")" << " << " << i << ";\n"); |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 188 | } else { |
| 189 | o << " Value |= getValueBit(op" << OpOrder[VI->getName()] |
| 190 | << ", " << VBI->getBitNum() |
| 191 | << ")" << " << " << i << ";\n"; |
| 192 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 193 | } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) { |
| 194 | // FIXME: implement this! |
| 195 | o << "FIELD INIT not implemented yet!\n"; |
| 196 | } else { |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 197 | o << "Error: UNIMPLEMENTED\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 200 | } |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 201 | } else { |
Misha Brukman | 3d194ac | 2003-06-05 23:15:25 +0000 | [diff] [blame] | 202 | // ignore annul and predict bits since no one sets them yet |
Misha Brukman | 48aa824 | 2003-07-07 22:30:44 +0000 | [diff] [blame] | 203 | if (Vals[f].getName() == "annul" || Vals[f].getName() == "predict") { |
| 204 | o << " // found " << Vals[f].getName() << "\n"; |
| 205 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | o << " break;\n" |
| 210 | << " }\n"; |
| 211 | } |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 212 | |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 213 | o << " default:\n" |
Misha Brukman | f4ef4c8 | 2003-06-06 00:27:02 +0000 | [diff] [blame] | 214 | << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n" |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 215 | << " abort();\n" |
| 216 | << " }\n" |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 217 | << " return Value;\n" |
| 218 | << "}\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 219 | } |