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