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