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