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" |
Chris Lattner | c648dab | 2003-08-01 22:13:59 +0000 | [diff] [blame] | 9 | #include "Support/Debug.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 | |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 49 | // Loop over all of the fields in the instruction determining which are the |
| 50 | // operands to the instruction. |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 51 | // |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 52 | unsigned op = 0; |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 53 | std::map<std::string, unsigned> OpOrder; |
| 54 | std::map<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) { |
Misha Brukman | d88ba5a | 2003-08-05 14:35:35 +0000 | [diff] [blame^] | 56 | if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) { |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 57 | // Is the operand continuous? If so, we can just mask and OR it in |
| 58 | // instead of doing it bit-by-bit, saving a lot in runtime cost. |
| 59 | const BitsInit *InstInit = BI; |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 60 | int beginBitInVar = -1, endBitInVar = -1; |
| 61 | int beginBitInInst = -1, endBitInInst = -1; |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 62 | bool continuous = true; |
| 63 | |
| 64 | for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) { |
| 65 | if (VarBitInit *VBI = |
| 66 | dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) { |
| 67 | TypedInit *TI = VBI->getVariable(); |
| 68 | if (VarInit *VI = dynamic_cast<VarInit*>(TI)) { |
| 69 | // only process the current variable |
| 70 | if (VI->getName() != Vals[i].getName()) |
| 71 | continue; |
| 72 | |
| 73 | if (beginBitInVar == -1) |
| 74 | beginBitInVar = VBI->getBitNum(); |
| 75 | |
| 76 | if (endBitInVar == -1) |
| 77 | endBitInVar = VBI->getBitNum(); |
| 78 | else { |
| 79 | if (endBitInVar == (int)VBI->getBitNum() + 1) |
| 80 | endBitInVar = VBI->getBitNum(); |
| 81 | else { |
| 82 | continuous = false; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (beginBitInInst == -1) |
| 88 | beginBitInInst = bit; |
| 89 | if (endBitInInst == -1) |
| 90 | endBitInInst = bit; |
| 91 | else { |
| 92 | if (endBitInInst == bit + 1) |
| 93 | endBitInInst = bit; |
| 94 | else { |
| 95 | continuous = false; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // maintain same distance between bits in field and bits in |
| 101 | // instruction. if the relative distances stay the same |
| 102 | // throughout, |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 103 | if (beginBitInVar - (int)VBI->getBitNum() != |
| 104 | beginBitInInst - bit) { |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 105 | continuous = false; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Chris Lattner | ffaee37 | 2003-08-05 03:59:01 +0000 | [diff] [blame] | 112 | // If we have found no bit in "Inst" which comes from this field, then |
| 113 | // this is not an operand!! |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 114 | if (beginBitInInst != -1) { |
| 115 | o << " // op" << op << ": " << Vals[i].getName() << "\n" |
| 116 | << " int64_t op" << op |
| 117 | <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n"; |
| 118 | //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n"; |
| 119 | OpOrder[Vals[i].getName()] = op++; |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 120 | |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 121 | DEBUG(o << " // Var: begin = " << beginBitInVar |
| 122 | << ", end = " << endBitInVar |
| 123 | << "; Inst: begin = " << beginBitInInst |
| 124 | << ", end = " << endBitInInst << "\n"); |
| 125 | |
| 126 | if (continuous) { |
| 127 | DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()] |
| 128 | << "\n"); |
| 129 | |
| 130 | // Mask off the right bits |
| 131 | // Low mask (ie. shift, if necessary) |
| 132 | if (endBitInVar != 0) { |
| 133 | o << " op" << OpOrder[Vals[i].getName()] |
| 134 | << " >>= " << endBitInVar << ";\n"; |
| 135 | beginBitInVar -= endBitInVar; |
| 136 | endBitInVar = 0; |
| 137 | } |
| 138 | |
| 139 | // High mask |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 140 | o << " op" << OpOrder[Vals[i].getName()] |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 141 | << " &= (1<<" << beginBitInVar+1 << ") - 1;\n"; |
| 142 | |
| 143 | // Shift the value to the correct place (according to place in inst) |
| 144 | if (endBitInInst != 0) |
| 145 | o << " op" << OpOrder[Vals[i].getName()] |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 146 | << " <<= " << endBitInInst << ";\n"; |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 147 | |
| 148 | // Just OR in the result |
| 149 | o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n"; |
| 150 | } |
| 151 | |
| 152 | // otherwise, will be taken care of in the loop below using this |
| 153 | // value: |
| 154 | OpContinuous[Vals[i].getName()] = continuous; |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 155 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Misha Brukman | 48aa824 | 2003-07-07 22:30:44 +0000 | [diff] [blame] | 159 | for (unsigned f = 0, e = Vals.size(); f != e; ++f) { |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 160 | if (Vals[f].getPrefix()) { |
| 161 | BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue(); |
| 162 | |
| 163 | // Scan through the field looking for bit initializers of the current |
| 164 | // variable... |
| 165 | for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) { |
Chris Lattner | ffaee37 | 2003-08-05 03:59:01 +0000 | [diff] [blame] | 166 | if (BitInit *BI = dynamic_cast<BitInit*>(FieldInitializer->getBit(i))) |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 167 | { |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 168 | DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n"); |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 169 | } else if (UnsetInit *UI = |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 170 | dynamic_cast<UnsetInit*>(FieldInitializer->getBit(i))) { |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 171 | DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 172 | } else if (VarBitInit *VBI = |
| 173 | dynamic_cast<VarBitInit*>(FieldInitializer->getBit(i))) { |
| 174 | TypedInit *TI = VBI->getVariable(); |
| 175 | if (VarInit *VI = dynamic_cast<VarInit*>(TI)) { |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 176 | // If the bits of the field are laid out consecutively in the |
| 177 | // instruction, then instead of separately ORing in bits, just |
| 178 | // mask and shift the entire field for efficiency. |
| 179 | if (OpContinuous[VI->getName()]) { |
| 180 | // already taken care of in the loop above, thus there is no |
| 181 | // need to individually OR in the bits |
| 182 | |
| 183 | // for debugging, output the regular version anyway, commented |
Misha Brukman | f6e5217 | 2003-07-15 21:26:09 +0000 | [diff] [blame] | 184 | DEBUG(o << " // Value |= getValueBit(op" |
| 185 | << OpOrder[VI->getName()] << ", " << VBI->getBitNum() |
| 186 | << ")" << " << " << i << ";\n"); |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 187 | } else { |
| 188 | o << " Value |= getValueBit(op" << OpOrder[VI->getName()] |
| 189 | << ", " << VBI->getBitNum() |
| 190 | << ")" << " << " << i << ";\n"; |
| 191 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 192 | } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) { |
| 193 | // FIXME: implement this! |
| 194 | o << "FIELD INIT not implemented yet!\n"; |
| 195 | } else { |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 196 | o << "Error: UNIMPLEMENTED\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 199 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | o << " break;\n" |
| 204 | << " }\n"; |
| 205 | } |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 206 | |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 207 | o << " default:\n" |
Misha Brukman | f4ef4c8 | 2003-06-06 00:27:02 +0000 | [diff] [blame] | 208 | << " DEBUG(std::cerr << \"Not supported instr: \" << MI << \"\\n\");\n" |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 209 | << " abort();\n" |
| 210 | << " }\n" |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 211 | << " return Value;\n" |
| 212 | << "}\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 213 | } |