Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1 | //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | d303203 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 8adcd9f | 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 | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | d303203 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 8e5492e | 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 | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Misha Brukman | 920ae95 | 2004-08-09 19:10:43 +0000 | [diff] [blame] | 16 | #include "CodeGenTarget.h" |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Jim Grosbach | 0b7fda2 | 2010-11-02 17:35:25 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 91d19d8 | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 20 | #include "llvm/TableGen/Record.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 21 | #include "llvm/TableGen/TableGenBackend.h" |
Bill Wendling | 5343836 | 2010-12-13 01:05:54 +0000 | [diff] [blame] | 22 | #include <map> |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
Chris Lattner | 6847866 | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Jim Grosbach | e4e6bf4 | 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 | 0b7fda2 | 2010-11-02 17:35:25 +0000 | [diff] [blame] | 30 | static cl::opt<bool> |
Jim Grosbach | e4e6bf4 | 2010-11-03 23:38:14 +0000 | [diff] [blame] | 31 | MCEmitter("mc-emitter", |
Jim Grosbach | 0b7fda2 | 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 | e6aed13 | 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); |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 46 | int getVariableBit(const std::string &VarName, BitsInit *BI, int bit); |
| 47 | std::string getInstructionCase(Record *R, CodeGenTarget &Target); |
| 48 | void AddCodeToMergeInOperand(Record *R, BitsInit *BI, |
| 49 | const std::string &VarName, |
| 50 | unsigned &NumberedOp, |
| 51 | std::string &Case, CodeGenTarget &Target); |
| 52 | |
| 53 | }; |
| 54 | |
Jim Laskey | 10d4b04 | 2006-07-13 22:17:08 +0000 | [diff] [blame] | 55 | // If the VarBitInit at position 'bit' matches the specified variable then |
| 56 | // return the variable bit position. Otherwise return -1. |
Dan Gohman | c87c16b | 2009-12-15 20:21:44 +0000 | [diff] [blame] | 57 | int CodeEmitterGen::getVariableBit(const std::string &VarName, |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 58 | BitsInit *BI, int bit) { |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 59 | if (VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit(bit))) { |
| 60 | if (VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar())) |
Chris Lattner | 42c4ac4 | 2010-11-15 06:42:13 +0000 | [diff] [blame] | 61 | if (VI->getName() == VarName) |
| 62 | return VBI->getBitNum(); |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 63 | } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit(bit))) { |
Owen Anderson | 715973f | 2011-04-28 17:51:45 +0000 | [diff] [blame] | 64 | if (VI->getName() == VarName) |
| 65 | return 0; |
| 66 | } |
Jim Grosbach | daab660 | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 67 | |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 68 | return -1; |
Jim Grosbach | daab660 | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 69 | } |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 70 | |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 71 | void CodeEmitterGen:: |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 72 | AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName, |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 73 | unsigned &NumberedOp, |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 74 | std::string &Case, CodeGenTarget &Target) { |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 75 | CodeGenInstruction &CGI = Target.getInstruction(R); |
| 76 | |
Chris Lattner | 578c765 | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 77 | // Determine if VarName actually contributes to the Inst encoding. |
| 78 | int bit = BI->getNumBits()-1; |
| 79 | |
| 80 | // Scan for a bit that this contributed to. |
| 81 | for (; bit >= 0; ) { |
| 82 | if (getVariableBit(VarName, BI, bit) != -1) |
| 83 | break; |
| 84 | |
| 85 | --bit; |
| 86 | } |
| 87 | |
| 88 | // If we found no bits, ignore this value, otherwise emit the call to get the |
| 89 | // operand encoding. |
| 90 | if (bit < 0) return; |
| 91 | |
| 92 | // If the operand matches by name, reference according to that |
| 93 | // operand number. Non-matching operands are assumed to be in |
| 94 | // order. |
| 95 | unsigned OpIdx; |
| 96 | if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) { |
| 97 | // Get the machine operand number for the indicated operand. |
| 98 | OpIdx = CGI.Operands[OpIdx].MIOperandNo; |
| 99 | assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) && |
| 100 | "Explicitly used operand also marked as not emitted!"); |
| 101 | } else { |
Evandro Menezes | 567698a | 2012-11-09 20:29:37 +0000 | [diff] [blame] | 102 | unsigned NumberOps = CGI.Operands.size(); |
Chris Lattner | 578c765 | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 103 | /// If this operand is not supposed to be emitted by the |
| 104 | /// generated emitter, skip it. |
Evandro Menezes | 567698a | 2012-11-09 20:29:37 +0000 | [diff] [blame] | 105 | while (NumberedOp < NumberOps && |
| 106 | CGI.Operands.isFlatOperandNotEmitted(NumberedOp)) |
Chris Lattner | 578c765 | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 107 | ++NumberedOp; |
Evandro Menezes | 03789a9 | 2012-11-09 21:27:03 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 578c765 | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 109 | OpIdx = NumberedOp++; |
| 110 | } |
| 111 | |
| 112 | std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx); |
| 113 | std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName; |
| 114 | |
| 115 | // If the source operand has a custom encoder, use it. This will |
| 116 | // get the encoding for all of the suboperands. |
| 117 | if (!EncoderMethodName.empty()) { |
| 118 | // A custom encoder has all of the information for the |
| 119 | // sub-operands, if there are more than one, so only |
| 120 | // query the encoder once per source operand. |
| 121 | if (SO.second == 0) { |
| 122 | Case += " // op: " + VarName + "\n" + |
| 123 | " op = " + EncoderMethodName + "(MI, " + utostr(OpIdx); |
| 124 | if (MCEmitter) |
| 125 | Case += ", Fixups"; |
| 126 | Case += ");\n"; |
| 127 | } |
| 128 | } else { |
| 129 | Case += " // op: " + VarName + "\n" + |
| 130 | " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")"; |
| 131 | if (MCEmitter) |
| 132 | Case += ", Fixups"; |
| 133 | Case += ");\n"; |
| 134 | } |
| 135 | |
| 136 | for (; bit >= 0; ) { |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 137 | int varBit = getVariableBit(VarName, BI, bit); |
| 138 | |
| 139 | // If this bit isn't from a variable, skip it. |
| 140 | if (varBit == -1) { |
| 141 | --bit; |
| 142 | continue; |
| 143 | } |
| 144 | |
Bob Wilson | f9bab3a | 2011-01-27 23:08:52 +0000 | [diff] [blame] | 145 | // Figure out the consecutive range of bits covered by this operand, in |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 146 | // order to generate better encoding code. |
| 147 | int beginInstBit = bit; |
| 148 | int beginVarBit = varBit; |
| 149 | int N = 1; |
| 150 | for (--bit; bit >= 0;) { |
| 151 | varBit = getVariableBit(VarName, BI, bit); |
| 152 | if (varBit == -1 || varBit != (beginVarBit - N)) break; |
| 153 | ++N; |
| 154 | --bit; |
| 155 | } |
Chris Lattner | 578c765 | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 156 | |
NAKAMURA Takumi | c72fdf4 | 2012-03-09 14:52:44 +0000 | [diff] [blame] | 157 | uint64_t opMask = ~(uint64_t)0 >> (64-N); |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 158 | int opShift = beginVarBit - N + 1; |
| 159 | opMask <<= opShift; |
| 160 | opShift = beginInstBit - beginVarBit; |
| 161 | |
| 162 | if (opShift > 0) { |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 163 | Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) << " + |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 164 | itostr(opShift) + ";\n"; |
| 165 | } else if (opShift < 0) { |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 166 | Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) >> " + |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 167 | itostr(-opShift) + ";\n"; |
| 168 | } else { |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 169 | Case += " Value |= op & UINT64_C(" + utostr(opMask) + ");\n"; |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | std::string CodeEmitterGen::getInstructionCase(Record *R, |
| 176 | CodeGenTarget &Target) { |
| 177 | std::string Case; |
| 178 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 179 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 180 | const std::vector<RecordVal> &Vals = R->getValues(); |
| 181 | unsigned NumberedOp = 0; |
| 182 | |
| 183 | // Loop over all of the fields in the instruction, determining which are the |
| 184 | // operands to the instruction. |
| 185 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 186 | // Ignore fixed fields in the record, we're looking for values like: |
| 187 | // bits<5> RST = { ?, ?, ?, ?, ? }; |
| 188 | if (Vals[i].getPrefix() || Vals[i].getValue()->isComplete()) |
| 189 | continue; |
| 190 | |
| 191 | AddCodeToMergeInOperand(R, BI, Vals[i].getName(), NumberedOp, Case, Target); |
| 192 | } |
| 193 | |
| 194 | std::string PostEmitter = R->getValueAsString("PostEncoderMethod"); |
| 195 | if (!PostEmitter.empty()) |
| 196 | Case += " Value = " + PostEmitter + "(MI, Value);\n"; |
| 197 | |
| 198 | return Case; |
| 199 | } |
| 200 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 201 | void CodeEmitterGen::run(raw_ostream &o) { |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 202 | CodeGenTarget Target(Records); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 203 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
Jim Grosbach | daab660 | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 204 | |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 205 | // For little-endian instruction bit encodings, reverse the bit order |
Hal Finkel | 81e6fcc | 2013-12-17 22:37:50 +0000 | [diff] [blame^] | 206 | Target.reverseBitsForLittleEndianEncoding(); |
Jim Grosbach | daab660 | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 918be52 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 208 | const std::vector<const CodeGenInstruction*> &NumberedInstructions = |
| 209 | Target.getInstructionsByEnumValue(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 210 | |
Misha Brukman | 422d0fa | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 211 | // Emit function declaration |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 212 | o << "uint64_t " << Target.getName(); |
Jim Grosbach | e4e6bf4 | 2010-11-03 23:38:14 +0000 | [diff] [blame] | 213 | if (MCEmitter) |
| 214 | o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n" |
| 215 | << " SmallVectorImpl<MCFixup> &Fixups) const {\n"; |
| 216 | else |
| 217 | o << "CodeEmitter::getBinaryCodeForInstr(const MachineInstr &MI) const {\n"; |
Misha Brukman | 422d0fa | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 218 | |
Jim Laskey | 23bd480 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 219 | // Emit instruction base values |
Owen Anderson | 773642d | 2012-03-06 21:48:32 +0000 | [diff] [blame] | 220 | o << " static const uint64_t InstBits[] = {\n"; |
Chris Lattner | 918be52 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 221 | for (std::vector<const CodeGenInstruction*>::const_iterator |
Jim Laskey | 23bd480 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 222 | IN = NumberedInstructions.begin(), |
| 223 | EN = NumberedInstructions.end(); |
| 224 | IN != EN; ++IN) { |
| 225 | const CodeGenInstruction *CGI = *IN; |
| 226 | Record *R = CGI->TheDef; |
Jim Grosbach | daab660 | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 227 | |
Jim Grosbach | f3fd36e | 2011-07-06 21:33:38 +0000 | [diff] [blame] | 228 | if (R->getValueAsString("Namespace") == "TargetOpcode" || |
| 229 | R->getValueAsBit("isPseudo")) { |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 230 | o << " UINT64_C(0),\n"; |
Jim Laskey | 23bd480 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 231 | continue; |
| 232 | } |
Jim Grosbach | daab660 | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 233 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 234 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 235 | |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 236 | // Start by filling in fixed values. |
Owen Anderson | 773642d | 2012-03-06 21:48:32 +0000 | [diff] [blame] | 237 | uint64_t Value = 0; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 238 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) { |
Sean Silva | fb509ed | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 239 | if (BitInit *B = dyn_cast<BitInit>(BI->getBit(e-i-1))) |
Owen Anderson | 773642d | 2012-03-06 21:48:32 +0000 | [diff] [blame] | 240 | Value |= (uint64_t)B->getValue() << (e-i-1); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 241 | } |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 242 | o << " UINT64_C(" << Value << ")," << '\t' << "// " << R->getName() << "\n"; |
Jim Laskey | 23bd480 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 243 | } |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 244 | o << " UINT64_C(0)\n };\n"; |
Jim Grosbach | daab660 | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 245 | |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 246 | // Map to accumulate all the cases. |
| 247 | std::map<std::string, std::vector<std::string> > CaseMap; |
Jim Grosbach | daab660 | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 248 | |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 249 | // Construct all cases statement for each opcode |
| 250 | for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end(); |
| 251 | IC != EC; ++IC) { |
| 252 | Record *R = *IC; |
Jim Grosbach | f3fd36e | 2011-07-06 21:33:38 +0000 | [diff] [blame] | 253 | if (R->getValueAsString("Namespace") == "TargetOpcode" || |
| 254 | R->getValueAsBit("isPseudo")) |
Jakob Stoklund Olesen | 3b1657b | 2010-07-02 21:44:22 +0000 | [diff] [blame] | 255 | continue; |
Jim Grosbach | cd25b86 | 2011-02-03 23:26:36 +0000 | [diff] [blame] | 256 | const std::string &InstName = R->getValueAsString("Namespace") + "::" |
| 257 | + R->getName(); |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 258 | std::string Case = getInstructionCase(R, Target); |
Dan Gohman | 60a446a | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 259 | |
Chris Lattner | c19d510 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 260 | CaseMap[Case].push_back(InstName); |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 262 | |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 263 | // Emit initial function code |
| 264 | o << " const unsigned opcode = MI.getOpcode();\n" |
Owen Anderson | 773642d | 2012-03-06 21:48:32 +0000 | [diff] [blame] | 265 | << " uint64_t Value = InstBits[opcode];\n" |
| 266 | << " uint64_t op = 0;\n" |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 267 | << " (void)op; // suppress warning\n" |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 268 | << " switch (opcode) {\n"; |
| 269 | |
| 270 | // Emit each case statement |
| 271 | std::map<std::string, std::vector<std::string> >::iterator IE, EE; |
| 272 | for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) { |
| 273 | const std::string &Case = IE->first; |
| 274 | std::vector<std::string> &InstList = IE->second; |
| 275 | |
| 276 | for (int i = 0, N = InstList.size(); i < N; i++) { |
| 277 | if (i) o << "\n"; |
Jim Grosbach | cd25b86 | 2011-02-03 23:26:36 +0000 | [diff] [blame] | 278 | o << " case " << InstList[i] << ":"; |
Jim Laskey | a44f626 | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 279 | } |
| 280 | o << " {\n"; |
| 281 | o << Case; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 282 | o << " break;\n" |
| 283 | << " }\n"; |
| 284 | } |
| 285 | |
Misha Brukman | 8393c15 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 286 | // Default case: unhandled opcode |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 287 | o << " default:\n" |
Torok Edwin | fa04002 | 2009-07-08 19:04:27 +0000 | [diff] [blame] | 288 | << " std::string msg;\n" |
| 289 | << " raw_string_ostream Msg(msg);\n" |
| 290 | << " Msg << \"Not supported instr: \" << MI;\n" |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 291 | << " report_fatal_error(Msg.str());\n" |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 292 | << " }\n" |
| 293 | << " return Value;\n" |
Misha Brukman | 8393c15 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 294 | << "}\n\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 295 | } |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 296 | |
| 297 | } // End anonymous namespace |
| 298 | |
| 299 | namespace llvm { |
| 300 | |
| 301 | void EmitCodeEmitter(RecordKeeper &RK, raw_ostream &OS) { |
| 302 | emitSourceFileHeader("Machine Code Emitter", OS); |
| 303 | CodeEmitterGen(RK).run(OS); |
| 304 | } |
| 305 | |
| 306 | } // End llvm namespace |