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 | |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 16 | #include "CodeGenInstruction.h" |
Misha Brukman | d7a5b28 | 2004-08-09 19:10:43 +0000 | [diff] [blame] | 17 | #include "CodeGenTarget.h" |
Daniel Sanders | b313c74 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 18 | #include "SubtargetFeatureInfo.h" |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/ArrayRef.h" |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Casting.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 4ffd89f | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 23 | #include "llvm/TableGen/Record.h" |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 24 | #include "llvm/TableGen/TableGenBackend.h" |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 25 | #include <cassert> |
| 26 | #include <cstdint> |
Bill Wendling | eac8f35 | 2010-12-13 01:05:54 +0000 | [diff] [blame] | 27 | #include <map> |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 28 | #include <set> |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 29 | #include <string> |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 30 | #include <utility> |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 31 | #include <vector> |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | class CodeEmitterGen { |
| 38 | RecordKeeper &Records; |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 39 | |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 40 | public: |
| 41 | CodeEmitterGen(RecordKeeper &R) : Records(R) {} |
| 42 | |
| 43 | void run(raw_ostream &o); |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 44 | |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 45 | private: |
Jakob Stoklund Olesen | 6f36fa9 | 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, |
Hal Finkel | 79c15b2 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 51 | std::set<unsigned> &NamedOpIndices, |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 52 | std::string &Case, CodeGenTarget &Target); |
| 53 | |
| 54 | }; |
| 55 | |
Jim Laskey | cb12903 | 2006-07-13 22:17:08 +0000 | [diff] [blame] | 56 | // If the VarBitInit at position 'bit' matches the specified variable then |
| 57 | // return the variable bit position. Otherwise return -1. |
Dan Gohman | 9b03da6 | 2009-12-15 20:21:44 +0000 | [diff] [blame] | 58 | int CodeEmitterGen::getVariableBit(const std::string &VarName, |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 59 | BitsInit *BI, int bit) { |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 60 | if (VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit(bit))) { |
| 61 | if (VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar())) |
Chris Lattner | 98e969a | 2010-11-15 06:42:13 +0000 | [diff] [blame] | 62 | if (VI->getName() == VarName) |
| 63 | return VBI->getBitNum(); |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 64 | } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit(bit))) { |
Owen Anderson | 4cdcb47 | 2011-04-28 17:51:45 +0000 | [diff] [blame] | 65 | if (VI->getName() == VarName) |
| 66 | return 0; |
| 67 | } |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 68 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 69 | return -1; |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 70 | } |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 72 | void CodeEmitterGen:: |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 73 | AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName, |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 74 | unsigned &NumberedOp, |
Hal Finkel | 79c15b2 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 75 | std::set<unsigned> &NamedOpIndices, |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 76 | std::string &Case, CodeGenTarget &Target) { |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 77 | CodeGenInstruction &CGI = Target.getInstruction(R); |
| 78 | |
Chris Lattner | 8ae082b | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 79 | // Determine if VarName actually contributes to the Inst encoding. |
| 80 | int bit = BI->getNumBits()-1; |
| 81 | |
| 82 | // Scan for a bit that this contributed to. |
| 83 | for (; bit >= 0; ) { |
| 84 | if (getVariableBit(VarName, BI, bit) != -1) |
| 85 | break; |
| 86 | |
| 87 | --bit; |
| 88 | } |
| 89 | |
| 90 | // If we found no bits, ignore this value, otherwise emit the call to get the |
| 91 | // operand encoding. |
| 92 | if (bit < 0) return; |
| 93 | |
| 94 | // If the operand matches by name, reference according to that |
| 95 | // operand number. Non-matching operands are assumed to be in |
| 96 | // order. |
| 97 | unsigned OpIdx; |
| 98 | if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) { |
| 99 | // Get the machine operand number for the indicated operand. |
| 100 | OpIdx = CGI.Operands[OpIdx].MIOperandNo; |
| 101 | assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) && |
| 102 | "Explicitly used operand also marked as not emitted!"); |
| 103 | } else { |
Evandro Menezes | f1bb421 | 2012-11-09 20:29:37 +0000 | [diff] [blame] | 104 | unsigned NumberOps = CGI.Operands.size(); |
Chris Lattner | 8ae082b | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 105 | /// If this operand is not supposed to be emitted by the |
| 106 | /// generated emitter, skip it. |
Evandro Menezes | f1bb421 | 2012-11-09 20:29:37 +0000 | [diff] [blame] | 107 | while (NumberedOp < NumberOps && |
Hal Finkel | 79c15b2 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 108 | (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) || |
Alexander Kornienko | b4c6267 | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 109 | (!NamedOpIndices.empty() && NamedOpIndices.count( |
Hal Finkel | f20a219 | 2014-03-22 11:33:32 +0000 | [diff] [blame] | 110 | CGI.Operands.getSubOperandNumber(NumberedOp).first)))) { |
Chris Lattner | 8ae082b | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 111 | ++NumberedOp; |
Evandro Menezes | f1adbfe | 2012-11-09 21:27:03 +0000 | [diff] [blame] | 112 | |
Hal Finkel | f20a219 | 2014-03-22 11:33:32 +0000 | [diff] [blame] | 113 | if (NumberedOp >= CGI.Operands.back().MIOperandNo + |
| 114 | CGI.Operands.back().MINumOperands) { |
| 115 | errs() << "Too few operands in record " << R->getName() << |
| 116 | " (no match for variable " << VarName << "):\n"; |
| 117 | errs() << *R; |
| 118 | errs() << '\n'; |
| 119 | |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | |
Chris Lattner | 8ae082b | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 124 | OpIdx = NumberedOp++; |
| 125 | } |
| 126 | |
| 127 | std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx); |
| 128 | std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName; |
| 129 | |
| 130 | // If the source operand has a custom encoder, use it. This will |
| 131 | // get the encoding for all of the suboperands. |
| 132 | if (!EncoderMethodName.empty()) { |
| 133 | // A custom encoder has all of the information for the |
| 134 | // sub-operands, if there are more than one, so only |
| 135 | // query the encoder once per source operand. |
| 136 | if (SO.second == 0) { |
| 137 | Case += " // op: " + VarName + "\n" + |
| 138 | " op = " + EncoderMethodName + "(MI, " + utostr(OpIdx); |
Eric Christopher | d5dd8ce | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 139 | Case += ", Fixups, STI"; |
Chris Lattner | 8ae082b | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 140 | Case += ");\n"; |
| 141 | } |
| 142 | } else { |
| 143 | Case += " // op: " + VarName + "\n" + |
| 144 | " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")"; |
Eric Christopher | d5dd8ce | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 145 | Case += ", Fixups, STI"; |
Chris Lattner | 8ae082b | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 146 | Case += ");\n"; |
| 147 | } |
| 148 | |
| 149 | for (; bit >= 0; ) { |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 150 | int varBit = getVariableBit(VarName, BI, bit); |
| 151 | |
| 152 | // If this bit isn't from a variable, skip it. |
| 153 | if (varBit == -1) { |
| 154 | --bit; |
| 155 | continue; |
| 156 | } |
| 157 | |
Bob Wilson | 9b8c353 | 2011-01-27 23:08:52 +0000 | [diff] [blame] | 158 | // Figure out the consecutive range of bits covered by this operand, in |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 159 | // order to generate better encoding code. |
| 160 | int beginInstBit = bit; |
| 161 | int beginVarBit = varBit; |
| 162 | int N = 1; |
| 163 | for (--bit; bit >= 0;) { |
| 164 | varBit = getVariableBit(VarName, BI, bit); |
| 165 | if (varBit == -1 || varBit != (beginVarBit - N)) break; |
| 166 | ++N; |
| 167 | --bit; |
| 168 | } |
Chris Lattner | 8ae082b | 2010-11-15 07:09:28 +0000 | [diff] [blame] | 169 | |
NAKAMURA Takumi | 89d8139 | 2012-03-09 14:52:44 +0000 | [diff] [blame] | 170 | uint64_t opMask = ~(uint64_t)0 >> (64-N); |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 171 | int opShift = beginVarBit - N + 1; |
| 172 | opMask <<= opShift; |
| 173 | opShift = beginInstBit - beginVarBit; |
| 174 | |
| 175 | if (opShift > 0) { |
Owen Anderson | 4f8dc7b | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 176 | Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) << " + |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 177 | itostr(opShift) + ";\n"; |
| 178 | } else if (opShift < 0) { |
Owen Anderson | 4f8dc7b | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 179 | Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) >> " + |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 180 | itostr(-opShift) + ";\n"; |
| 181 | } else { |
Owen Anderson | 4f8dc7b | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 182 | Case += " Value |= op & UINT64_C(" + utostr(opMask) + ");\n"; |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 187 | std::string CodeEmitterGen::getInstructionCase(Record *R, |
| 188 | CodeGenTarget &Target) { |
| 189 | std::string Case; |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 190 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 191 | unsigned NumberedOp = 0; |
Hal Finkel | 79c15b2 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 192 | std::set<unsigned> NamedOpIndices; |
Alexander Shaposhnikov | d6059bd | 2017-07-04 06:16:53 +0000 | [diff] [blame] | 193 | |
Hal Finkel | 79c15b2 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 194 | // Collect the set of operand indices that might correspond to named |
| 195 | // operand, and skip these when assigning operands based on position. |
| 196 | if (Target.getInstructionSet()-> |
| 197 | getValueAsBit("noNamedPositionallyEncodedOperands")) { |
| 198 | CodeGenInstruction &CGI = Target.getInstruction(R); |
Alexander Shaposhnikov | d6059bd | 2017-07-04 06:16:53 +0000 | [diff] [blame] | 199 | for (const RecordVal &RV : R->getValues()) { |
Hal Finkel | 79c15b2 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 200 | unsigned OpIdx; |
Alexander Shaposhnikov | d6059bd | 2017-07-04 06:16:53 +0000 | [diff] [blame] | 201 | if (!CGI.Operands.hasOperandNamed(RV.getName(), OpIdx)) |
Hal Finkel | 79c15b2 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 202 | continue; |
| 203 | |
| 204 | NamedOpIndices.insert(OpIdx); |
| 205 | } |
| 206 | } |
| 207 | |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 208 | // Loop over all of the fields in the instruction, determining which are the |
| 209 | // operands to the instruction. |
Alexander Shaposhnikov | d6059bd | 2017-07-04 06:16:53 +0000 | [diff] [blame] | 210 | for (const RecordVal &RV : R->getValues()) { |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 211 | // Ignore fixed fields in the record, we're looking for values like: |
| 212 | // bits<5> RST = { ?, ?, ?, ?, ? }; |
Alexander Shaposhnikov | d6059bd | 2017-07-04 06:16:53 +0000 | [diff] [blame] | 213 | if (RV.getPrefix() || RV.getValue()->isComplete()) |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 214 | continue; |
| 215 | |
Alexander Shaposhnikov | d6059bd | 2017-07-04 06:16:53 +0000 | [diff] [blame] | 216 | AddCodeToMergeInOperand(R, BI, RV.getName(), NumberedOp, |
Hal Finkel | 79c15b2 | 2014-03-13 07:57:54 +0000 | [diff] [blame] | 217 | NamedOpIndices, Case, Target); |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 218 | } |
Craig Topper | 2a12987 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 219 | |
| 220 | StringRef PostEmitter = R->getValueAsString("PostEncoderMethod"); |
David Woodhouse | 4faa861 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 221 | if (!PostEmitter.empty()) { |
Craig Topper | 2a12987 | 2017-05-31 21:12:46 +0000 | [diff] [blame] | 222 | Case += " Value = "; |
| 223 | Case += PostEmitter; |
| 224 | Case += "(MI, Value"; |
Eric Christopher | d5dd8ce | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 225 | Case += ", STI"; |
David Woodhouse | 4faa861 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 226 | Case += ");\n"; |
| 227 | } |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 228 | |
| 229 | return Case; |
| 230 | } |
| 231 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 232 | void CodeEmitterGen::run(raw_ostream &o) { |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 233 | CodeGenTarget Target(Records); |
Chris Lattner | 048c00d | 2003-08-01 04:38:18 +0000 | [diff] [blame] | 234 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 235 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 236 | // For little-endian instruction bit encodings, reverse the bit order |
Hal Finkel | af73dfe | 2013-12-17 22:37:50 +0000 | [diff] [blame] | 237 | Target.reverseBitsForLittleEndianEncoding(); |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 238 | |
Craig Topper | 3f0462d | 2016-02-01 01:33:42 +0000 | [diff] [blame] | 239 | ArrayRef<const CodeGenInstruction*> NumberedInstructions = |
Chris Lattner | f650278 | 2010-03-19 00:34:35 +0000 | [diff] [blame] | 240 | Target.getInstructionsByEnumValue(); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 241 | |
Misha Brukman | ad346ad | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 242 | // Emit function declaration |
Owen Anderson | 4f8dc7b | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 243 | o << "uint64_t " << Target.getName(); |
Eric Christopher | d5dd8ce | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 244 | o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n" |
| 245 | << " SmallVectorImpl<MCFixup> &Fixups,\n" |
| 246 | << " const MCSubtargetInfo &STI) const {\n"; |
Misha Brukman | ad346ad | 2004-08-10 20:54:58 +0000 | [diff] [blame] | 247 | |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 248 | // Emit instruction base values |
Owen Anderson | 40530ad | 2012-03-06 21:48:32 +0000 | [diff] [blame] | 249 | o << " static const uint64_t InstBits[] = {\n"; |
Craig Topper | 3f0462d | 2016-02-01 01:33:42 +0000 | [diff] [blame] | 250 | for (const CodeGenInstruction *CGI : NumberedInstructions) { |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 251 | Record *R = CGI->TheDef; |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 252 | |
Jim Grosbach | 806fcc0 | 2011-07-06 21:33:38 +0000 | [diff] [blame] | 253 | if (R->getValueAsString("Namespace") == "TargetOpcode" || |
| 254 | R->getValueAsBit("isPseudo")) { |
Owen Anderson | 4f8dc7b | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 255 | o << " UINT64_C(0),\n"; |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 256 | continue; |
| 257 | } |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 258 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 259 | BitsInit *BI = R->getValueAsBitsInit("Inst"); |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 261 | // Start by filling in fixed values. |
Owen Anderson | 40530ad | 2012-03-06 21:48:32 +0000 | [diff] [blame] | 262 | uint64_t Value = 0; |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 263 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) { |
Sean Silva | 6cfc806 | 2012-10-10 20:24:43 +0000 | [diff] [blame] | 264 | if (BitInit *B = dyn_cast<BitInit>(BI->getBit(e-i-1))) |
Owen Anderson | 40530ad | 2012-03-06 21:48:32 +0000 | [diff] [blame] | 265 | Value |= (uint64_t)B->getValue() << (e-i-1); |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 266 | } |
Owen Anderson | 4f8dc7b | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 267 | o << " UINT64_C(" << Value << ")," << '\t' << "// " << R->getName() << "\n"; |
Jim Laskey | ed39343 | 2006-07-12 19:15:43 +0000 | [diff] [blame] | 268 | } |
Owen Anderson | 4f8dc7b | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 269 | o << " UINT64_C(0)\n };\n"; |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 270 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 271 | // Map to accumulate all the cases. |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 272 | std::map<std::string, std::vector<std::string>> CaseMap; |
Jim Grosbach | 8b892ae | 2010-10-07 16:56:28 +0000 | [diff] [blame] | 273 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 274 | // Construct all cases statement for each opcode |
| 275 | for (std::vector<Record*>::iterator IC = Insts.begin(), EC = Insts.end(); |
| 276 | IC != EC; ++IC) { |
| 277 | Record *R = *IC; |
Jim Grosbach | 806fcc0 | 2011-07-06 21:33:38 +0000 | [diff] [blame] | 278 | if (R->getValueAsString("Namespace") == "TargetOpcode" || |
| 279 | R->getValueAsBit("isPseudo")) |
Jakob Stoklund Olesen | 65766ce | 2010-07-02 21:44:22 +0000 | [diff] [blame] | 280 | continue; |
Craig Topper | c469be3 | 2017-05-31 19:01:11 +0000 | [diff] [blame] | 281 | std::string InstName = |
| 282 | (R->getValueAsString("Namespace") + "::" + R->getName()).str(); |
Chris Lattner | 1620117 | 2010-11-15 06:59:17 +0000 | [diff] [blame] | 283 | std::string Case = getInstructionCase(R, Target); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 284 | |
Craig Topper | c469be3 | 2017-05-31 19:01:11 +0000 | [diff] [blame] | 285 | CaseMap[Case].push_back(std::move(InstName)); |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 286 | } |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 287 | |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 288 | // Emit initial function code |
| 289 | o << " const unsigned opcode = MI.getOpcode();\n" |
Owen Anderson | 40530ad | 2012-03-06 21:48:32 +0000 | [diff] [blame] | 290 | << " uint64_t Value = InstBits[opcode];\n" |
| 291 | << " uint64_t op = 0;\n" |
Jeffrey Yasskin | 8e68c38 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 292 | << " (void)op; // suppress warning\n" |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 293 | << " switch (opcode) {\n"; |
| 294 | |
| 295 | // Emit each case statement |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 296 | std::map<std::string, std::vector<std::string>>::iterator IE, EE; |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 297 | for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) { |
| 298 | const std::string &Case = IE->first; |
| 299 | std::vector<std::string> &InstList = IE->second; |
| 300 | |
| 301 | for (int i = 0, N = InstList.size(); i < N; i++) { |
| 302 | if (i) o << "\n"; |
Jim Grosbach | 0ed92f2 | 2011-02-03 23:26:36 +0000 | [diff] [blame] | 303 | o << " case " << InstList[i] << ":"; |
Jim Laskey | f1b05bf | 2006-07-13 21:02:53 +0000 | [diff] [blame] | 304 | } |
| 305 | o << " {\n"; |
| 306 | o << Case; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 307 | o << " break;\n" |
| 308 | << " }\n"; |
| 309 | } |
Misha Brukman | 7eac476 | 2003-07-15 21:00:32 +0000 | [diff] [blame] | 310 | |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 311 | // Default case: unhandled opcode |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 312 | o << " default:\n" |
Alp Toker | 8dd8d5c | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 313 | << " std::string msg;\n" |
| 314 | << " raw_string_ostream Msg(msg);\n" |
Torok Edwin | 804e0fe | 2009-07-08 19:04:27 +0000 | [diff] [blame] | 315 | << " Msg << \"Not supported instr: \" << MI;\n" |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 316 | << " report_fatal_error(Msg.str());\n" |
Misha Brukman | cbfde0a | 2003-05-27 22:19:58 +0000 | [diff] [blame] | 317 | << " }\n" |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 318 | << " return Value;\n" |
Misha Brukman | 28eefa5 | 2004-10-14 05:53:01 +0000 | [diff] [blame] | 319 | << "}\n\n"; |
Daniel Sanders | b313c74 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 320 | |
| 321 | const auto &All = SubtargetFeatureInfo::getAll(Records); |
| 322 | std::map<Record *, SubtargetFeatureInfo, LessRecordByID> SubtargetFeatures; |
| 323 | SubtargetFeatures.insert(All.begin(), All.end()); |
| 324 | |
| 325 | o << "#ifdef ENABLE_INSTR_PREDICATE_VERIFIER\n" |
| 326 | << "#undef ENABLE_INSTR_PREDICATE_VERIFIER\n" |
| 327 | << "#include <sstream>\n\n"; |
| 328 | |
| 329 | // Emit the subtarget feature enumeration. |
| 330 | SubtargetFeatureInfo::emitSubtargetFeatureFlagEnumeration(SubtargetFeatures, |
| 331 | o); |
| 332 | |
| 333 | // Emit the name table for error messages. |
| 334 | o << "#ifndef NDEBUG\n"; |
| 335 | SubtargetFeatureInfo::emitNameTable(SubtargetFeatures, o); |
| 336 | o << "#endif // NDEBUG\n"; |
| 337 | |
| 338 | // Emit the available features compute function. |
Daniel Sanders | e8660ea | 2017-04-21 15:59:56 +0000 | [diff] [blame] | 339 | SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures( |
Daniel Sanders | b313c74 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 340 | Target.getName(), "MCCodeEmitter", "computeAvailableFeatures", |
| 341 | SubtargetFeatures, o); |
| 342 | |
| 343 | // Emit the predicate verifier. |
| 344 | o << "void " << Target.getName() |
| 345 | << "MCCodeEmitter::verifyInstructionPredicates(\n" |
| 346 | << " const MCInst &Inst, uint64_t AvailableFeatures) const {\n" |
| 347 | << "#ifndef NDEBUG\n" |
| 348 | << " static uint64_t RequiredFeatures[] = {\n"; |
| 349 | unsigned InstIdx = 0; |
| 350 | for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) { |
| 351 | o << " "; |
| 352 | for (Record *Predicate : Inst->TheDef->getValueAsListOfDefs("Predicates")) { |
| 353 | const auto &I = SubtargetFeatures.find(Predicate); |
| 354 | if (I != SubtargetFeatures.end()) |
| 355 | o << I->second.getEnumName() << " | "; |
| 356 | } |
| 357 | o << "0, // " << Inst->TheDef->getName() << " = " << InstIdx << "\n"; |
| 358 | InstIdx++; |
| 359 | } |
| 360 | o << " };\n\n"; |
| 361 | o << " assert(Inst.getOpcode() < " << InstIdx << ");\n"; |
| 362 | o << " uint64_t MissingFeatures =\n" |
| 363 | << " (AvailableFeatures & RequiredFeatures[Inst.getOpcode()]) ^\n" |
| 364 | << " RequiredFeatures[Inst.getOpcode()];\n" |
| 365 | << " if (MissingFeatures) {\n" |
| 366 | << " std::ostringstream Msg;\n" |
Daniel Sanders | c170429 | 2016-11-19 14:47:41 +0000 | [diff] [blame] | 367 | << " Msg << \"Attempting to emit \" << " |
| 368 | "MCII.getName(Inst.getOpcode()).str()\n" |
Daniel Sanders | b313c74 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 369 | << " << \" instruction but the \";\n" |
| 370 | << " for (unsigned i = 0; i < 8 * sizeof(MissingFeatures); ++i)\n" |
| 371 | << " if (MissingFeatures & (1ULL << i))\n" |
| 372 | << " Msg << SubtargetFeatureNames[i] << \" \";\n" |
| 373 | << " Msg << \"predicate(s) are not met\";\n" |
| 374 | << " report_fatal_error(Msg.str());\n" |
| 375 | << " }\n" |
Daniel Sanders | c170429 | 2016-11-19 14:47:41 +0000 | [diff] [blame] | 376 | << "#else\n" |
| 377 | << "// Silence unused variable warning on targets that don't use MCII for " |
| 378 | "other purposes (e.g. BPF).\n" |
| 379 | << "(void)MCII;\n" |
Daniel Sanders | b313c74 | 2016-11-19 13:05:44 +0000 | [diff] [blame] | 380 | << "#endif // NDEBUG\n"; |
| 381 | o << "}\n"; |
| 382 | o << "#endif\n"; |
Misha Brukman | 9fff7e1 | 2003-05-24 00:15:53 +0000 | [diff] [blame] | 383 | } |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 384 | |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 385 | } // end anonymous namespace |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 386 | |
| 387 | namespace llvm { |
| 388 | |
| 389 | void EmitCodeEmitter(RecordKeeper &RK, raw_ostream &OS) { |
| 390 | emitSourceFileHeader("Machine Code Emitter", OS); |
| 391 | CodeEmitterGen(RK).run(OS); |
| 392 | } |
| 393 | |
Eugene Zelenko | c02caf5 | 2016-11-30 17:48:10 +0000 | [diff] [blame] | 394 | } // end namespace llvm |