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 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Jim Grosbach | ab3d00e | 2010-11-02 17:35:25 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Jim Grosbach | 60aaa76 | 2010-11-03 23:38:14 +0000 | [diff] [blame] | 24 | // FIXME: Somewhat hackish to use a command line option for this. There should |
| 25 | // be a CodeEmitter class in the Target.td that controls this sort of thing |
| 26 | // instead. |
Jim Grosbach | ab3d00e | 2010-11-02 17:35:25 +0000 | [diff] [blame] | 27 | static cl::opt<bool> |
Jim Grosbach | 60aaa76 | 2010-11-03 23:38:14 +0000 | [diff] [blame] | 28 | MCEmitter("mc-emitter", |
Jim Grosbach | ab3d00e | 2010-11-02 17:35:25 +0000 | [diff] [blame] | 29 | cl::desc("Generate CodeEmitter for use with the MC library."), |
| 30 | cl::init(false)); |
| 31 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 32 | void CodeEmitterGen::reverseBits(std::vector<Record*> &Insts) { |
| 33 | for (std::vector<Record*>::iterator I = Insts.begin(), E = Insts.end(); |
| 34 | I != E; ++I) { |
| 35 | Record *R = *I; |
Jakob Stoklund Olesen | 65766ce | 2010-07-02 21:44:22 +0000 | [diff] [blame] | 36 | if (R->getValueAsString("Namespace") == "TargetOpcode") |
| 37 | continue; |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 38 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 39 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 40 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 41 | unsigned numBits = BI->getNumBits(); |
| 42 | BitsInit *NewBI = new BitsInit(numBits); |
| 43 | for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) { |
| 44 | unsigned bitSwapIdx = numBits - bit - 1; |
| 45 | Init *OrigBit = BI->getBit(bit); |
| 46 | Init *BitSwap = BI->getBit(bitSwapIdx); |
| 47 | NewBI->setBit(bit, BitSwap); |
| 48 | NewBI->setBit(bitSwapIdx, OrigBit); |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 49 | } |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 50 | if (numBits % 2) { |
| 51 | unsigned middle = (numBits + 1) / 2; |
| 52 | NewBI->setBit(middle, BI->getBit(middle)); |
| 53 | } |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 54 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 55 | // Update the bits in reversed order so that emitInstrOpBits will get the |
| 56 | // correct endianness. |
| 57 | R->getValue("Inst")->setValue(NewBI); |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Jim Laskey | cb12903 | 2006-07-13 22:17:08 +0000 | [diff] [blame] | 61 | // If the VarBitInit at position 'bit' matches the specified variable then |
| 62 | // return the variable bit position. Otherwise return -1. |
Dan Gohman | 9b03da6 | 2009-12-15 20:21:44 +0000 | [diff] [blame] | 63 | int CodeEmitterGen::getVariableBit(const std::string &VarName, |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame^] | 64 | BitsInit *BI, int bit) { |
Chris Lattner | 98e969a | 2010-11-15 06:42:13 +0000 | [diff] [blame] | 65 | if (VarBitInit *VBI = dynamic_cast<VarBitInit*>(BI->getBit(bit))) |
| 66 | if (VarInit *VI = dynamic_cast<VarInit*>(VBI->getVariable())) |
| 67 | if (VI->getName() == VarName) |
| 68 | return VBI->getBitNum(); |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 69 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 70 | return -1; |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 71 | } |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame^] | 73 | void CodeEmitterGen:: |
| 74 | AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName, |
| 75 | unsigned &NumberedOp, |
| 76 | std::string &Case, CodeGenTarget &Target) { |
| 77 | bool gotOp = false; |
| 78 | CodeGenInstruction &CGI = Target.getInstruction(R); |
| 79 | |
| 80 | for (int bit = BI->getNumBits()-1; bit >= 0; ) { |
| 81 | int varBit = getVariableBit(VarName, BI, bit); |
| 82 | |
| 83 | // If this bit isn't from a variable, skip it. |
| 84 | if (varBit == -1) { |
| 85 | --bit; |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | // Figure out the consequtive range of bits covered by this operand, in |
| 90 | // order to generate better encoding code. |
| 91 | int beginInstBit = bit; |
| 92 | int beginVarBit = varBit; |
| 93 | int N = 1; |
| 94 | for (--bit; bit >= 0;) { |
| 95 | varBit = getVariableBit(VarName, BI, bit); |
| 96 | if (varBit == -1 || varBit != (beginVarBit - N)) break; |
| 97 | ++N; |
| 98 | --bit; |
| 99 | } |
| 100 | |
| 101 | if (!gotOp) { |
| 102 | // If the operand matches by name, reference according to that |
| 103 | // operand number. Non-matching operands are assumed to be in |
| 104 | // order. |
| 105 | unsigned OpIdx; |
| 106 | if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) { |
| 107 | // Get the machine operand number for the indicated operand. |
| 108 | OpIdx = CGI.Operands[OpIdx].MIOperandNo; |
| 109 | assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) && |
| 110 | "Explicitly used operand also marked as not emitted!"); |
| 111 | } else { |
| 112 | /// If this operand is not supposed to be emitted by the |
| 113 | /// generated emitter, skip it. |
| 114 | while (CGI.Operands.isFlatOperandNotEmitted(NumberedOp)) |
| 115 | ++NumberedOp; |
| 116 | OpIdx = NumberedOp++; |
| 117 | } |
| 118 | std::pair<unsigned, unsigned> SO =CGI.Operands.getSubOperandNumber(OpIdx); |
| 119 | std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName; |
| 120 | |
| 121 | // If the source operand has a custom encoder, use it. This will |
| 122 | // get the encoding for all of the suboperands. |
| 123 | if (!EncoderMethodName.empty()) { |
| 124 | // A custom encoder has all of the information for the |
| 125 | // sub-operands, if there are more than one, so only |
| 126 | // query the encoder once per source operand. |
| 127 | if (SO.second == 0) { |
| 128 | Case += " // op: " + VarName + "\n" |
| 129 | + " op = " + EncoderMethodName + "(MI, " |
| 130 | + utostr(OpIdx); |
| 131 | if (MCEmitter) |
| 132 | Case += ", Fixups"; |
| 133 | Case += ");\n"; |
| 134 | } |
| 135 | } else { |
| 136 | Case += " // op: " + VarName + "\n" + |
| 137 | " op = getMachineOpValue(MI, MI.getOperand(" + |
| 138 | utostr(OpIdx) + ")"; |
| 139 | if (MCEmitter) |
| 140 | Case += ", Fixups"; |
| 141 | Case += ");\n"; |
| 142 | } |
| 143 | gotOp = true; |
| 144 | } |
| 145 | |
| 146 | unsigned opMask = ~0U >> (32-N); |
| 147 | int opShift = beginVarBit - N + 1; |
| 148 | opMask <<= opShift; |
| 149 | opShift = beginInstBit - beginVarBit; |
| 150 | |
| 151 | if (opShift > 0) { |
| 152 | Case += " Value |= (op & " + utostr(opMask) + "U) << " + |
| 153 | itostr(opShift) + ";\n"; |
| 154 | } else if (opShift < 0) { |
| 155 | Case += " Value |= (op & " + utostr(opMask) + "U) >> " + |
| 156 | itostr(-opShift) + ";\n"; |
| 157 | } else { |
| 158 | Case += " Value |= op & " + utostr(opMask) + "U;\n"; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
| 164 | std::string CodeEmitterGen::getInstructionCase(Record *R, |
| 165 | CodeGenTarget &Target) { |
| 166 | std::string Case; |
| 167 | |
| 168 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
| 169 | const std::vector<RecordVal> &Vals = R->getValues(); |
| 170 | unsigned NumberedOp = 0; |
| 171 | |
| 172 | // Loop over all of the fields in the instruction, determining which are the |
| 173 | // operands to the instruction. |
| 174 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 175 | // Ignore fixed fields in the record, we're looking for values like: |
| 176 | // bits<5> RST = { ?, ?, ?, ?, ? }; |
| 177 | if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete()) |
| 178 | continue; |
| 179 | |
| 180 | AddCodeToMergeInOperand(R, BI, Vals[i].getName(), NumberedOp, Case, Target); |
| 181 | } |
| 182 | |
| 183 | std::string PostEmitter = R->getValueAsString("PostEncoderMethod"); |
| 184 | if (!PostEmitter.empty()) |
| 185 | Case += " Value = " + PostEmitter + "(MI, Value);\n"; |
| 186 | |
| 187 | return Case; |
| 188 | } |
| 189 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 190 | void CodeEmitterGen::run(raw_ostream &o) { |
Misha Brukman | e2ba778 | 2004-08-10 18:31:01 +0000 | [diff] [blame] | 191 | CodeGenTarget Target; |
Chris Lattner | 048c00d | 2003-08-01 04:38:18 +0000 | [diff] [blame] | 192 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 193 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 194 | // For little-endian instruction bit encodings, reverse the bit order |
| 195 | if (Target.isLittleEndianEncoding()) reverseBits(Insts); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 0e5e49e | 2003-08-06 04:36:35 +0000 | [diff] [blame] | 197 | EmitSourceFileHeader("Machine Code Emitter", o); |
Misha Brukman | e2ba778 | 2004-08-10 18:31:01 +0000 | [diff] [blame] | 198 | std::string Namespace = Insts[0]->getValueAsString("Namespace") + "::"; |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 199 | |
Chris Lattner | f650278 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 200 | const std::vector<const CodeGenInstruction*> &NumberedInstructions = |
| 201 | Target.getInstructionsByEnumValue(); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 202 | |
Misha Brukman | ad346ad | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 203 | // Emit function declaration |
Jim Grosbach | 60aaa76 | 2010-11-03 23:38:14 +0000 | [diff] [blame] | 204 | o << "unsigned " << Target.getName(); |
| 205 | if (MCEmitter) |
| 206 | o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n" |
| 207 | << " SmallVectorImpl<MCFixup> &Fixups) const {\n"; |
| 208 | else |
| 209 | o << "CodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const {\n"; |
Misha Brukman | ad346ad | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 210 | |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 211 | // Emit instruction base values |
| 212 | o << " static const unsigned InstBits[] = {\n"; |
Chris Lattner | f650278 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 213 | for (std::vector<const CodeGenInstruction*>::const_iterator |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 214 | IN = NumberedInstructions.begin(), |
| 215 | EN = NumberedInstructions.end(); |
| 216 | IN != EN; ++IN) { |
| 217 | const CodeGenInstruction *CGI = *IN; |
| 218 | Record *R = CGI->TheDef; |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 219 | |
Jakob Stoklund Olesen | 65766ce | 2010-07-02 21:44:22 +0000 | [diff] [blame] | 220 | if (R->getValueAsString("Namespace") == "TargetOpcode") { |
Evan Cheng | bc95b23 | 2008-09-17 06:29:52 +0000 | [diff] [blame] | 221 | o << " 0U,\n"; |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 222 | continue; |
| 223 | } |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 6f334ad | 2003-08-01 04:46:24 +0000 | [diff] [blame] | 225 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame^] | 227 | // Start by filling in fixed values. |
Reid Spencer | fa3e3b9 | 2006-11-03 01:48:30 +0000 | [diff] [blame] | 228 | unsigned Value = 0; |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 229 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) { |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame^] | 230 | if (BitInit *B = dynamic_cast<BitInit*>(BI->getBit(e-i-1))) |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 231 | Value |= B->getValue() << (e-i-1); |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 232 | } |
Evan Cheng | bc95b23 | 2008-09-17 06:29:52 +0000 | [diff] [blame] | 233 | o << " " << Value << "U," << '\t' << "// " << R->getName() << "\n"; |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 234 | } |
Evan Cheng | bc95b23 | 2008-09-17 06:29:52 +0000 | [diff] [blame] | 235 | o << " 0U\n };\n"; |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 236 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 237 | // Map to accumulate all the cases. |
| 238 | std::map<std::string, std::vector<std::string> > CaseMap; |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 239 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 240 | // Construct all cases statement for each opcode |
| 241 | for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end(); |
| 242 | IC != EC; ++IC) { |
| 243 | Record *R = *IC; |
Jakob Stoklund Olesen | 65766ce | 2010-07-02 21:44:22 +0000 | [diff] [blame] | 244 | if (R->getValueAsString("Namespace") == "TargetOpcode") |
| 245 | continue; |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 246 | const std::string &InstName = R->getName(); |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame^] | 247 | std::string Case = getInstructionCase(R, Target); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame^] | 249 | CaseMap[Case].push_back(InstName); |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 250 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 251 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 252 | // Emit initial function code |
| 253 | o << " const unsigned opcode = MI.getOpcode();\n" |
| 254 | << " unsigned Value = InstBits[opcode];\n" |
Evan Cheng | e3e3626 | 2008-09-07 09:00:57 +0000 | [diff] [blame] | 255 | << " unsigned op = 0;\n" |
Evan Cheng | acff339 | 2008-09-02 06:51:36 +0000 | [diff] [blame] | 256 | << " op = op; // suppress warning\n" |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 257 | << " switch (opcode) {\n"; |
| 258 | |
| 259 | // Emit each case statement |
| 260 | std::map<std::string, std::vector<std::string> >::iterator IE, EE; |
| 261 | for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) { |
| 262 | const std::string &Case = IE->first; |
| 263 | std::vector<std::string> &InstList = IE->second; |
| 264 | |
| 265 | for (int i = 0, N = InstList.size(); i < N; i++) { |
| 266 | if (i) o << "\n"; |
| 267 | o << " case " << Namespace << InstList[i] << ":"; |
| 268 | } |
| 269 | o << " {\n"; |
| 270 | o << Case; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 271 | o << " break;\n" |
| 272 | << " }\n"; |
| 273 | } |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 274 | |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 275 | // Default case: unhandled opcode |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 276 | o << " default:\n" |
Torok Edwin | 804e0fe | 2009-07-08 19:04:27 +0000 | [diff] [blame] | 277 | << " std::string msg;\n" |
| 278 | << " raw_string_ostream Msg(msg);\n" |
| 279 | << " Msg << \"Not supported instr: \" << MI;\n" |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 280 | << " report_fatal_error(Msg.str());\n" |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 281 | << " }\n" |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 282 | << " return Value;\n" |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 283 | << "}\n\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 284 | } |