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