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" |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 23 | void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) { |
| 24 | for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end(); |
| 25 | I != E; ++I) { |
| 26 | Record *R = *I; |
| 27 | if (R->getName() == "PHI" || R->getName() == "INLINEASM") continue; |
| 28 | |
| 29 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 30 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 31 | unsigned numBits = BI->getNumBits(); |
| 32 | BitsInit *NewBI = new BitsInit(numBits); |
| 33 | for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) { |
| 34 | unsigned bitSwapIdx = numBits - bit - 1; |
| 35 | Init *OrigBit = BI->getBit(bit); |
| 36 | Init *BitSwap = BI->getBit(bitSwapIdx); |
| 37 | NewBI->setBit(bit, BitSwap); |
| 38 | NewBI->setBit(bitSwapIdx, OrigBit); |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 39 | } |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 40 | if (numBits % 2) { |
| 41 | unsigned middle = (numBits + 1) / 2; |
| 42 | NewBI->setBit(middle, BI->getBit(middle)); |
| 43 | } |
| 44 | |
| 45 | // Update the bits in reversed order so that emitInstrOpBits will get the |
| 46 | // correct endianness. |
| 47 | R->getValue("Inst")->setValue(NewBI); |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | |
Jim Laskey | cb12903 | 2006-07-13 22:17:08 +0000 | [diff] [blame^] | 52 | // If the VarBitInit at position 'bit' matches the specified variable then |
| 53 | // return the variable bit position. Otherwise return -1. |
| 54 | int CodeEmitterGen::getVariableBit(const std::string &VarName, |
| 55 | BitsInit *BI, int bit) { |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 56 | if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) { |
| 57 | TypedInit *TI = VBI->getVariable(); |
| 58 | |
| 59 | if (VarInit *VI = dynamic_cast<VarInit*>(TI)) { |
| 60 | if (VI->getName() == VarName) return VBI->getBitNum(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | |
Chris Lattner | 048c00d | 2003-08-01 04:38:18 +0000 | [diff] [blame] | 68 | void CodeEmitterGen::run(std::ostream &o) { |
Misha Brukman | e2ba778 | 2004-08-10 18:31:01 +0000 | [diff] [blame] | 69 | CodeGenTarget Target; |
Chris Lattner | 048c00d | 2003-08-01 04:38:18 +0000 | [diff] [blame] | 70 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 71 | |
| 72 | // For little-endian instruction bit encodings, reverse the bit order |
| 73 | if (Target.isLittleEndianEncoding()) reverseBits(Insts); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 0e5e49e | 2003-08-06 04:36:35 +0000 | [diff] [blame] | 75 | EmitSourceFileHeader("Machine Code Emitter", o); |
Misha Brukman | e2ba778 | 2004-08-10 18:31:01 +0000 | [diff] [blame] | 76 | std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::"; |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 77 | |
| 78 | std::vector<const CodeGenInstruction*> NumberedInstructions; |
| 79 | Target.getInstructionsByEnumValue(NumberedInstructions); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 80 | |
Misha Brukman | ad346ad | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 81 | // Emit function declaration |
Misha Brukman | e2ba778 | 2004-08-10 18:31:01 +0000 | [diff] [blame] | 82 | o << "unsigned " << Target.getName() << "CodeEmitter::" |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 83 | << "getBinaryCodeForInstr(MachineInstr &MI) {\n"; |
Misha Brukman | ad346ad | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 84 | |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 85 | // Emit instruction base values |
| 86 | o << " static const unsigned InstBits[] = {\n"; |
| 87 | for (std::vector<const CodeGenInstruction*>::iterator |
| 88 | IN = NumberedInstructions.begin(), |
| 89 | EN = NumberedInstructions.end(); |
| 90 | IN != EN; ++IN) { |
| 91 | const CodeGenInstruction *CGI = *IN; |
| 92 | Record *R = CGI->TheDef; |
Chris Lattner | 188454a | 2006-01-27 01:39:38 +0000 | [diff] [blame] | 93 | |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 94 | if (IN != NumberedInstructions.begin()) o << ",\n"; |
| 95 | |
| 96 | if (R->getName() == "PHI" || R->getName() == "INLINEASM") { |
| 97 | o << " 0U"; |
| 98 | continue; |
| 99 | } |
| 100 | |
Chris Lattner | 6f334ad | 2003-08-01 04:46:24 +0000 | [diff] [blame] | 101 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 102 | |
| 103 | unsigned Value = 0; |
| 104 | const std::vector<RecordVal> &Vals = R->getValues(); |
| 105 | |
| 106 | // Start by filling in fixed values... |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 107 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) { |
| 108 | if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) { |
| 109 | Value |= B->getValue() << (e-i-1); |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 112 | o << " " << Value << "U"; |
| 113 | } |
| 114 | o << "\n };\n"; |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 115 | |
| 116 | // Map to accumulate all the cases. |
| 117 | std::map<std::string, std::vector<std::string> > CaseMap; |
| 118 | |
| 119 | // Construct all cases statement for each opcode |
| 120 | for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end(); |
| 121 | IC != EC; ++IC) { |
| 122 | Record *R = *IC; |
| 123 | const std::string &InstName = R->getName(); |
| 124 | std::string Case(""); |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 125 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 126 | if (InstName == "PHI" || InstName == "INLINEASM") continue; |
| 127 | |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 128 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
| 129 | const std::vector<RecordVal> &Vals = R->getValues(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 130 | |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 131 | // 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] | 132 | // operands to the instruction. |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 133 | unsigned op = 0; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 134 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 135 | if (!Vals[i].getPrefix() && !Vals[i].getValue()->isComplete()) { |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 136 | // 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] | 137 | // instead of doing it bit-by-bit, saving a lot in runtime cost. |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 138 | const std::string &VarName = Vals[i].getName(); |
| 139 | bool gotOp = false; |
| 140 | |
| 141 | for (int bit = BI->getNumBits()-1; bit >= 0; ) { |
| 142 | int varBit = getVariableBit(VarName, BI, bit); |
| 143 | |
| 144 | if (varBit == -1) { |
| 145 | --bit; |
| 146 | } else { |
| 147 | int beginInstBit = bit; |
| 148 | int beginVarBit = varBit; |
| 149 | int N = 1; |
| 150 | |
| 151 | for (--bit; bit >= 0;) { |
| 152 | varBit = getVariableBit(VarName, BI, bit); |
| 153 | if (varBit == -1 || varBit != (beginVarBit - N)) break; |
| 154 | ++N; |
| 155 | --bit; |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 156 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 157 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 158 | if (!gotOp) { |
| 159 | Case += " // op: " + VarName + "\n" |
| 160 | + " op = getMachineOpValue(MI, MI.getOperand(" |
| 161 | + utostr(op++) |
| 162 | + "));\n"; |
| 163 | gotOp = true; |
| 164 | } |
| 165 | |
| 166 | unsigned opMask = (1 << N) - 1; |
| 167 | int opShift = beginVarBit - N + 1; |
| 168 | opMask <<= opShift; |
| 169 | opShift = beginInstBit - beginVarBit; |
| 170 | |
| 171 | if (opShift > 0) { |
| 172 | Case += " Value |= (op & " + utostr(opMask) + "U) << " |
| 173 | + itostr(opShift) + ";\n"; |
| 174 | } else if (opShift < 0) { |
| 175 | Case += " Value |= (op & " + utostr(opMask) + "U) >> " |
| 176 | + itostr(-opShift) + ";\n"; |
| 177 | } else { |
| 178 | Case += " Value |= op & " + utostr(opMask) + "U;\n"; |
| 179 | } |
Chris Lattner | d7efef9 | 2003-08-05 03:53:04 +0000 | [diff] [blame] | 180 | } |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 181 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 185 | std::vector<std::string> &InstList = CaseMap[Case]; |
| 186 | InstList.push_back(InstName); |
| 187 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 188 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 189 | |
| 190 | // Emit initial function code |
| 191 | o << " const unsigned opcode = MI.getOpcode();\n" |
| 192 | << " unsigned Value = InstBits[opcode];\n" |
| 193 | << " unsigned op;\n" |
| 194 | << " switch (opcode) {\n"; |
| 195 | |
| 196 | // Emit each case statement |
| 197 | std::map<std::string, std::vector<std::string> >::iterator IE, EE; |
| 198 | for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) { |
| 199 | const std::string &Case = IE->first; |
| 200 | std::vector<std::string> &InstList = IE->second; |
| 201 | |
| 202 | for (int i = 0, N = InstList.size(); i < N; i++) { |
| 203 | if (i) o << "\n"; |
| 204 | o << " case " << Namespace << InstList[i] << ":"; |
| 205 | } |
| 206 | o << " {\n"; |
| 207 | o << Case; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 208 | o << " break;\n" |
| 209 | << " }\n"; |
| 210 | } |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 211 | |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 212 | // Default case: unhandled opcode |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 213 | o << " default:\n" |
Misha Brukman | 0bb806b | 2003-09-17 18:21:48 +0000 | [diff] [blame] | 214 | << " std::cerr << \"Not supported instr: \" << MI << \"\\n\";\n" |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 215 | << " abort();\n" |
| 216 | << " }\n" |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 217 | << " return Value;\n" |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 218 | << "}\n\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 219 | } |