Chris Lattner | c9670ef | 2003-07-31 04:43:49 +0000 | [diff] [blame] | 1 | //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | c9670ef | 2003-07-31 04:43:49 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 4e4f863 | 2004-08-04 22:07:54 +0000 | [diff] [blame] | 10 | // CodeEmitterGen uses the descriptions of instructions and their fields to |
| 11 | // construct an automated code emitter: a function that, given a MachineInstr, |
| 12 | // returns the (currently, 32-bit unsigned) value of the instruction. |
Chris Lattner | c9670ef | 2003-07-31 04:43:49 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 16 | #include "CodeEmitterGen.h" |
Misha Brukman | d7a5b28 | 2004-08-09 19:10:43 +0000 | [diff] [blame] | 17 | #include "CodeGenTarget.h" |
Chris Lattner | c9670ef | 2003-07-31 04:43:49 +0000 | [diff] [blame] | 18 | #include "Record.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 22 | void CodeEmitterGen::emitInstrOpBits(std::ostream &o, |
| 23 | const std::vector<RecordVal> &Vals, |
| 24 | std::map<std::string, unsigned> &OpOrder, |
| 25 | std::map<std::string, bool> &OpContinuous) |
| 26 | { |
| 27 | for (unsigned f = 0, e = Vals.size(); f != e; ++f) { |
| 28 | if (Vals[f].getPrefix()) { |
| 29 | BitsInit *FieldInitializer = (BitsInit*)Vals[f].getValue(); |
| 30 | |
| 31 | // Scan through the field looking for bit initializers of the current |
| 32 | // variable... |
| 33 | for (int i = FieldInitializer->getNumBits()-1; i >= 0; --i) { |
| 34 | Init *I = FieldInitializer->getBit(i); |
| 35 | if (BitInit *BI = dynamic_cast<BitInit*>(I)) { |
| 36 | DEBUG(o << " // bit init: f: " << f << ", i: " << i << "\n"); |
| 37 | } else if (UnsetInit *UI = dynamic_cast<UnsetInit*>(I)) { |
| 38 | DEBUG(o << " // unset init: f: " << f << ", i: " << i << "\n"); |
| 39 | } else if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(I)) { |
| 40 | TypedInit *TI = VBI->getVariable(); |
| 41 | if (VarInit *VI = dynamic_cast<VarInit*>(TI)) { |
| 42 | // If the bits of the field are laid out consecutively in the |
| 43 | // instruction, then instead of separately ORing in bits, just |
| 44 | // mask and shift the entire field for efficiency. |
| 45 | if (OpContinuous[VI->getName()]) { |
| 46 | // already taken care of in the loop above, thus there is no |
| 47 | // need to individually OR in the bits |
| 48 | |
| 49 | // for debugging, output the regular version anyway, commented |
| 50 | DEBUG(o << " // Value |= getValueBit(op" |
| 51 | << OpOrder[VI->getName()] << ", " << VBI->getBitNum() |
| 52 | << ")" << " << " << i << ";\n"); |
| 53 | } else { |
| 54 | o << " Value |= getValueBit(op" << OpOrder[VI->getName()] |
| 55 | << ", " << VBI->getBitNum() |
| 56 | << ")" << " << " << i << ";\n"; |
| 57 | } |
| 58 | } else if (FieldInit *FI = dynamic_cast<FieldInit*>(TI)) { |
| 59 | // FIXME: implement this! |
| 60 | std::cerr << "Error: FieldInit not implemented!\n"; |
| 61 | abort(); |
| 62 | } else { |
| 63 | std::cerr << "Error: unimplemented case in " |
| 64 | << "CodeEmitterGen::emitInstrOpBits()\n"; |
| 65 | abort(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
Chris Lattner | 048c00d | 2003-08-01 04:38:18 +0000 | [diff] [blame] | 74 | void CodeEmitterGen::run(std::ostream &o) { |
Misha Brukman | e2ba778 | 2004-08-10 18:31:01 +0000 | [diff] [blame] | 75 | CodeGenTarget Target; |
Chris Lattner | 048c00d | 2003-08-01 04:38:18 +0000 | [diff] [blame] | 76 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 0e5e49e | 2003-08-06 04:36:35 +0000 | [diff] [blame] | 78 | EmitSourceFileHeader("Machine Code Emitter", o); |
Misha Brukman | e2ba778 | 2004-08-10 18:31:01 +0000 | [diff] [blame] | 79 | std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::"; |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame^] | 80 | |
| 81 | std::vector<const CodeGenInstruction*> NumberedInstructions; |
| 82 | Target.getInstructionsByEnumValue(NumberedInstructions); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 83 | |
Misha Brukman | ad346ad | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 84 | // Emit function declaration |
Misha Brukman | e2ba778 | 2004-08-10 18:31:01 +0000 | [diff] [blame] | 85 | o << "unsigned " << Target.getName() << "CodeEmitter::" |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame^] | 86 | << "getBinaryCodeForInstr(MachineInstr &MI) {\n"; |
Misha Brukman | ad346ad | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 87 | |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame^] | 88 | // Emit instruction base values |
| 89 | o << " static const unsigned InstBits[] = {\n"; |
| 90 | for (std::vector<const CodeGenInstruction*>::iterator |
| 91 | IN = NumberedInstructions.begin(), |
| 92 | EN = NumberedInstructions.end(); |
| 93 | IN != EN; ++IN) { |
| 94 | const CodeGenInstruction *CGI = *IN; |
| 95 | Record *R = CGI->TheDef; |
Chris Lattner | 188454a | 2006-01-27 01:39:38 +0000 | [diff] [blame] | 96 | |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame^] | 97 | if (IN != NumberedInstructions.begin()) o << ",\n"; |
| 98 | |
| 99 | if (R->getName() == "PHI" || R->getName() == "INLINEASM") { |
| 100 | o << " 0U"; |
| 101 | continue; |
| 102 | } |
| 103 | |
Chris Lattner | 6f334ad | 2003-08-01 04:46:24 +0000 | [diff] [blame] | 104 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 105 | |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 106 | // For little-endian instruction bit encodings, reverse the bit order |
| 107 | if (Target.isLittleEndianEncoding()) { |
| 108 | unsigned numBits = BI->getNumBits(); |
| 109 | BitsInit *NewBI = new BitsInit(numBits); |
| 110 | for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) { |
| 111 | unsigned bitSwapIdx = numBits - bit - 1; |
| 112 | Init *OrigBit = BI->getBit(bit); |
| 113 | Init *BitSwap = BI->getBit(bitSwapIdx); |
| 114 | NewBI->setBit(bit, BitSwap); |
| 115 | NewBI->setBit(bitSwapIdx, OrigBit); |
| 116 | } |
| 117 | if (numBits % 2) { |
| 118 | unsigned middle = (numBits + 1) / 2; |
| 119 | NewBI->setBit(middle, BI->getBit(middle)); |
| 120 | } |
| 121 | BI = NewBI; |
Chris Lattner | 485c00f | 2006-03-18 00:40:36 +0000 | [diff] [blame] | 122 | |
| 123 | // Update the bits in reversed order so that emitInstrOpBits will get the |
| 124 | // correct endianness. |
| 125 | R->getValue("Inst")->setValue(NewBI); |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 126 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 127 | |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 128 | unsigned Value = 0; |
| 129 | const std::vector<RecordVal> &Vals = R->getValues(); |
| 130 | |
| 131 | // Start by filling in fixed values... |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 132 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) { |
| 133 | if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) { |
| 134 | Value |= B->getValue() << (e-i-1); |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame^] | 137 | o << " " << Value << "U"; |
| 138 | } |
| 139 | o << "\n };\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 140 | |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame^] | 141 | // Emit initial function code |
| 142 | o << " const unsigned opcode = MI.getOpcode();\n" |
| 143 | << " unsigned Value = InstBits[opcode];\n" |
| 144 | << " switch (opcode) {\n"; |
| 145 | |
| 146 | // Emit a case statement for each opcode |
| 147 | for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end(); |
| 148 | I != E; ++I) { |
| 149 | Record *R = *I; |
| 150 | if (R->getName() == "PHI" || R->getName() == "INLINEASM") continue; |
| 151 | |
| 152 | o << " case " << Namespace << R->getName() << ": {\n"; |
| 153 | |
| 154 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
| 155 | const std::vector<RecordVal> &Vals = R->getValues(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 156 | |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 157 | // Loop over all of the fields in the instruction, determining which are the |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 158 | // operands to the instruction. |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 159 | unsigned op = 0; |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 160 | std::map<std::string, unsigned> OpOrder; |
| 161 | std::map<std::string, bool> OpContinuous; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 162 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 163 | if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) { |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 164 | // Is the operand continuous? If so, we can just mask and OR it in |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 165 | // instead of doing it bit-by-bit, saving a lot in runtime cost. |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 166 | BitsInit *InstInit = BI; |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 167 | int beginBitInVar = -1, endBitInVar = -1; |
| 168 | int beginBitInInst = -1, endBitInInst = -1; |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 169 | bool continuous = true; |
| 170 | |
| 171 | for (int bit = InstInit->getNumBits()-1; bit >= 0; --bit) { |
| 172 | if (VarBitInit *VBI = |
| 173 | dynamic_cast<VarBitInit*>(InstInit->getBit(bit))) { |
| 174 | TypedInit *TI = VBI->getVariable(); |
| 175 | if (VarInit *VI = dynamic_cast<VarInit*>(TI)) { |
| 176 | // only process the current variable |
| 177 | if (VI->getName() != Vals[i].getName()) |
| 178 | continue; |
| 179 | |
| 180 | if (beginBitInVar == -1) |
| 181 | beginBitInVar = VBI->getBitNum(); |
| 182 | |
| 183 | if (endBitInVar == -1) |
| 184 | endBitInVar = VBI->getBitNum(); |
| 185 | else { |
| 186 | if (endBitInVar == (int)VBI->getBitNum() + 1) |
| 187 | endBitInVar = VBI->getBitNum(); |
| 188 | else { |
| 189 | continuous = false; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (beginBitInInst == -1) |
| 195 | beginBitInInst = bit; |
| 196 | if (endBitInInst == -1) |
| 197 | endBitInInst = bit; |
| 198 | else { |
| 199 | if (endBitInInst == bit + 1) |
| 200 | endBitInInst = bit; |
| 201 | else { |
| 202 | continuous = false; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // maintain same distance between bits in field and bits in |
| 208 | // instruction. if the relative distances stay the same |
| 209 | // throughout, |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 210 | if (beginBitInVar - (int)VBI->getBitNum() != |
| 211 | beginBitInInst - bit) { |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 212 | continuous = false; |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Chris Lattner | ffaee37 | 2003-08-05 03:59:01 +0000 | [diff] [blame] | 219 | // If we have found no bit in "Inst" which comes from this field, then |
| 220 | // this is not an operand!! |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 221 | if (beginBitInInst != -1) { |
| 222 | o << " // op" << op << ": " << Vals[i].getName() << "\n" |
Chris Lattner | fcd60a0 | 2005-08-19 01:04:33 +0000 | [diff] [blame] | 223 | << " int op" << op |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 224 | <<" = getMachineOpValue(MI, MI.getOperand("<<op<<"));\n"; |
| 225 | //<< " MachineOperand &op" << op <<" = MI.getOperand("<<op<<");\n"; |
| 226 | OpOrder[Vals[i].getName()] = op++; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 227 | |
| 228 | DEBUG(o << " // Var: begin = " << beginBitInVar |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 229 | << ", end = " << endBitInVar |
| 230 | << "; Inst: begin = " << beginBitInInst |
| 231 | << ", end = " << endBitInInst << "\n"); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 232 | |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 233 | if (continuous) { |
| 234 | DEBUG(o << " // continuous: op" << OpOrder[Vals[i].getName()] |
| 235 | << "\n"); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 236 | |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 237 | // Mask off the right bits |
| 238 | // Low mask (ie. shift, if necessary) |
Misha Brukman | dfd414a | 2003-08-06 16:28:49 +0000 | [diff] [blame] | 239 | assert(endBitInVar >= 0 && "Negative shift amount in masking!"); |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 240 | if (endBitInVar != 0) { |
| 241 | o << " op" << OpOrder[Vals[i].getName()] |
| 242 | << " >>= " << endBitInVar << ";\n"; |
| 243 | beginBitInVar -= endBitInVar; |
| 244 | endBitInVar = 0; |
| 245 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 246 | |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 247 | // High mask |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 248 | o << " op" << OpOrder[Vals[i].getName()] |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 249 | << " &= (1<<" << beginBitInVar+1 << ") - 1;\n"; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 250 | |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 251 | // Shift the value to the correct place (according to place in inst) |
Misha Brukman | 4e4f863 | 2004-08-04 22:07:54 +0000 | [diff] [blame] | 252 | assert(endBitInInst >= 0 && "Negative shift amount!"); |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 253 | if (endBitInInst != 0) |
| 254 | o << " op" << OpOrder[Vals[i].getName()] |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 255 | << " <<= " << endBitInInst << ";\n"; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 256 | |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 257 | // Just OR in the result |
| 258 | o << " Value |= op" << OpOrder[Vals[i].getName()] << ";\n"; |
| 259 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 260 | |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 261 | // otherwise, will be taken care of in the loop below using this |
| 262 | // value: |
| 263 | OpContinuous[Vals[i].getName()] = continuous; |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 264 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 268 | emitInstrOpBits(o, Vals, OpOrder, OpContinuous); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 269 | |
| 270 | o << " break;\n" |
| 271 | << " }\n"; |
| 272 | } |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 273 | |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 274 | // Default case: unhandled opcode |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 275 | o << " default:\n" |
Misha Brukman | 0bb806b | 2003-09-17 18:21:48 +0000 | [diff] [blame] | 276 | << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n" |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 277 | << " abort();\n" |
| 278 | << " }\n" |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 279 | << " return Value;\n" |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 280 | << "}\n\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 281 | } |