blob: da65763905a8f7e76411714687a6fdb7d916885e [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman650ba8e2005-04-22 00:00:37 +00006//
John Criswelld3032032003-10-20 20:20:30 +00007//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00008//
Misha Brukman8e5492e2004-08-04 22:07:54 +00009// CodeEmitterGen uses the descriptions of instructions and their fields to
10// construct an automated code emitter: a function that, given a MachineInstr,
11// returns the (currently, 32-bit unsigned) value of the instruction.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000012//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000015#include "CodeGenInstruction.h"
Misha Brukman920ae952004-08-09 19:10:43 +000016#include "CodeGenTarget.h"
Daniel Sanders72db2a32016-11-19 13:05:44 +000017#include "SubtargetFeatureInfo.h"
Stanislav Mekhanoshine98944e2019-03-11 17:04:35 +000018#include "Types.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000019#include "llvm/ADT/ArrayRef.h"
Jim Laskeya44f6262006-07-13 21:02:53 +000020#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000021#include "llvm/Support/Casting.h"
22#include "llvm/Support/raw_ostream.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000023#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000024#include "llvm/TableGen/TableGenBackend.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000025#include <cassert>
26#include <cstdint>
Bill Wendling53438362010-12-13 01:05:54 +000027#include <map>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000028#include <set>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000029#include <string>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000030#include <utility>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000031#include <vector>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000032
Chris Lattner68478662004-08-01 03:55:39 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000035namespace {
36
37class CodeEmitterGen {
38 RecordKeeper &Records;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000039
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000040public:
41 CodeEmitterGen(RecordKeeper &R) : Records(R) {}
42
43 void run(raw_ostream &o);
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000044
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000045private:
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000046 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 Finkel5457bd02014-03-13 07:57:54 +000051 std::set<unsigned> &NamedOpIndices,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000052 std::string &Case, CodeGenTarget &Target);
53
54};
55
Jim Laskey10d4b042006-07-13 22:17:08 +000056// If the VarBitInit at position 'bit' matches the specified variable then
57// return the variable bit position. Otherwise return -1.
Dan Gohmanc87c16b2009-12-15 20:21:44 +000058int CodeEmitterGen::getVariableBit(const std::string &VarName,
David Greeneaf8ee2c2011-07-29 22:43:06 +000059 BitsInit *BI, int bit) {
Sean Silvafb509ed2012-10-10 20:24:43 +000060 if (VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit(bit))) {
61 if (VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar()))
Chris Lattner42c4ac42010-11-15 06:42:13 +000062 if (VI->getName() == VarName)
63 return VBI->getBitNum();
Sean Silvafb509ed2012-10-10 20:24:43 +000064 } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit(bit))) {
Owen Anderson715973f2011-04-28 17:51:45 +000065 if (VI->getName() == VarName)
66 return 0;
67 }
Jim Grosbachdaab6602010-10-07 16:56:28 +000068
Jim Laskeya44f6262006-07-13 21:02:53 +000069 return -1;
Jim Grosbachdaab6602010-10-07 16:56:28 +000070}
Jim Laskeya44f6262006-07-13 21:02:53 +000071
Chris Lattnerc19d5102010-11-15 06:59:17 +000072void CodeEmitterGen::
David Greeneaf8ee2c2011-07-29 22:43:06 +000073AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName,
Eric Christopher71520a82011-07-11 23:06:52 +000074 unsigned &NumberedOp,
Hal Finkel5457bd02014-03-13 07:57:54 +000075 std::set<unsigned> &NamedOpIndices,
Chris Lattnerc19d5102010-11-15 06:59:17 +000076 std::string &Case, CodeGenTarget &Target) {
Chris Lattnerc19d5102010-11-15 06:59:17 +000077 CodeGenInstruction &CGI = Target.getInstruction(R);
78
Chris Lattner578c7652010-11-15 07:09:28 +000079 // 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 Menezes567698a2012-11-09 20:29:37 +0000104 unsigned NumberOps = CGI.Operands.size();
Chris Lattner578c7652010-11-15 07:09:28 +0000105 /// If this operand is not supposed to be emitted by the
106 /// generated emitter, skip it.
Evandro Menezes567698a2012-11-09 20:29:37 +0000107 while (NumberedOp < NumberOps &&
Hal Finkel5457bd02014-03-13 07:57:54 +0000108 (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000109 (!NamedOpIndices.empty() && NamedOpIndices.count(
Hal Finkel68e03bd2014-03-22 11:33:32 +0000110 CGI.Operands.getSubOperandNumber(NumberedOp).first)))) {
Chris Lattner578c7652010-11-15 07:09:28 +0000111 ++NumberedOp;
Evandro Menezes03789a92012-11-09 21:27:03 +0000112
Hal Finkel68e03bd2014-03-22 11:33:32 +0000113 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 Lattner578c7652010-11-15 07:09:28 +0000124 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 Christopher79cc1e32014-09-02 22:28:02 +0000139 Case += ", Fixups, STI";
Chris Lattner578c7652010-11-15 07:09:28 +0000140 Case += ");\n";
141 }
142 } else {
143 Case += " // op: " + VarName + "\n" +
144 " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
Eric Christopher79cc1e32014-09-02 22:28:02 +0000145 Case += ", Fixups, STI";
Chris Lattner578c7652010-11-15 07:09:28 +0000146 Case += ");\n";
147 }
148
149 for (; bit >= 0; ) {
Chris Lattnerc19d5102010-11-15 06:59:17 +0000150 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 Wilsonf9bab3a2011-01-27 23:08:52 +0000158 // Figure out the consecutive range of bits covered by this operand, in
Chris Lattnerc19d5102010-11-15 06:59:17 +0000159 // 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 Lattner578c7652010-11-15 07:09:28 +0000169
NAKAMURA Takumic72fdf42012-03-09 14:52:44 +0000170 uint64_t opMask = ~(uint64_t)0 >> (64-N);
Chris Lattnerc19d5102010-11-15 06:59:17 +0000171 int opShift = beginVarBit - N + 1;
172 opMask <<= opShift;
173 opShift = beginInstBit - beginVarBit;
174
175 if (opShift > 0) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000176 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) << " +
Chris Lattnerc19d5102010-11-15 06:59:17 +0000177 itostr(opShift) + ";\n";
178 } else if (opShift < 0) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000179 Case += " Value |= (op & UINT64_C(" + utostr(opMask) + ")) >> " +
Chris Lattnerc19d5102010-11-15 06:59:17 +0000180 itostr(-opShift) + ";\n";
181 } else {
Owen Andersond845d9d2012-01-24 18:37:29 +0000182 Case += " Value |= op & UINT64_C(" + utostr(opMask) + ");\n";
Chris Lattnerc19d5102010-11-15 06:59:17 +0000183 }
184 }
185}
186
Chris Lattnerc19d5102010-11-15 06:59:17 +0000187std::string CodeEmitterGen::getInstructionCase(Record *R,
188 CodeGenTarget &Target) {
189 std::string Case;
David Greeneaf8ee2c2011-07-29 22:43:06 +0000190 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattnerc19d5102010-11-15 06:59:17 +0000191 unsigned NumberedOp = 0;
Hal Finkel5457bd02014-03-13 07:57:54 +0000192 std::set<unsigned> NamedOpIndices;
Alexander Shaposhnikov8b5f0c92017-07-04 06:16:53 +0000193
Hal Finkel5457bd02014-03-13 07:57:54 +0000194 // 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 Shaposhnikov8b5f0c92017-07-04 06:16:53 +0000199 for (const RecordVal &RV : R->getValues()) {
Hal Finkel5457bd02014-03-13 07:57:54 +0000200 unsigned OpIdx;
Alexander Shaposhnikov8b5f0c92017-07-04 06:16:53 +0000201 if (!CGI.Operands.hasOperandNamed(RV.getName(), OpIdx))
Hal Finkel5457bd02014-03-13 07:57:54 +0000202 continue;
203
204 NamedOpIndices.insert(OpIdx);
205 }
206 }
207
Chris Lattnerc19d5102010-11-15 06:59:17 +0000208 // Loop over all of the fields in the instruction, determining which are the
209 // operands to the instruction.
Alexander Shaposhnikov8b5f0c92017-07-04 06:16:53 +0000210 for (const RecordVal &RV : R->getValues()) {
Chris Lattnerc19d5102010-11-15 06:59:17 +0000211 // Ignore fixed fields in the record, we're looking for values like:
212 // bits<5> RST = { ?, ?, ?, ?, ? };
Alexander Shaposhnikov8b5f0c92017-07-04 06:16:53 +0000213 if (RV.getPrefix() || RV.getValue()->isComplete())
Chris Lattnerc19d5102010-11-15 06:59:17 +0000214 continue;
215
Alexander Shaposhnikov8b5f0c92017-07-04 06:16:53 +0000216 AddCodeToMergeInOperand(R, BI, RV.getName(), NumberedOp,
Hal Finkel5457bd02014-03-13 07:57:54 +0000217 NamedOpIndices, Case, Target);
Chris Lattnerc19d5102010-11-15 06:59:17 +0000218 }
Craig Topperbcd3c372017-05-31 21:12:46 +0000219
220 StringRef PostEmitter = R->getValueAsString("PostEncoderMethod");
David Woodhouse3fa98a62014-01-28 23:13:18 +0000221 if (!PostEmitter.empty()) {
Craig Topperbcd3c372017-05-31 21:12:46 +0000222 Case += " Value = ";
223 Case += PostEmitter;
224 Case += "(MI, Value";
Eric Christopher79cc1e32014-09-02 22:28:02 +0000225 Case += ", STI";
David Woodhouse3fa98a62014-01-28 23:13:18 +0000226 Case += ");\n";
227 }
Chris Lattnerc19d5102010-11-15 06:59:17 +0000228
229 return Case;
230}
231
Stanislav Mekhanoshine98944e2019-03-11 17:04:35 +0000232static std::string
233getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) {
234 std::string Name = "CEFBS";
235 for (const auto &Feature : FeatureBitset)
236 Name += ("_" + Feature->getName()).str();
237 return Name;
238}
239
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000240void CodeEmitterGen::run(raw_ostream &o) {
Chris Lattner77d369c2010-12-13 00:23:57 +0000241 CodeGenTarget Target(Records);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000242 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Jim Grosbachdaab6602010-10-07 16:56:28 +0000243
Jim Laskeya44f6262006-07-13 21:02:53 +0000244 // For little-endian instruction bit encodings, reverse the bit order
Hal Finkel81e6fcc2013-12-17 22:37:50 +0000245 Target.reverseBitsForLittleEndianEncoding();
Jim Grosbachdaab6602010-10-07 16:56:28 +0000246
Craig Topper28851b62016-02-01 01:33:42 +0000247 ArrayRef<const CodeGenInstruction*> NumberedInstructions =
Chris Lattner918be522010-03-19 00:34:35 +0000248 Target.getInstructionsByEnumValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000249
Misha Brukman422d0fa2004-08-10 20:54:58 +0000250 // Emit function declaration
Owen Andersond845d9d2012-01-24 18:37:29 +0000251 o << "uint64_t " << Target.getName();
Eric Christopher79cc1e32014-09-02 22:28:02 +0000252 o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
253 << " SmallVectorImpl<MCFixup> &Fixups,\n"
254 << " const MCSubtargetInfo &STI) const {\n";
Misha Brukman422d0fa2004-08-10 20:54:58 +0000255
Jim Laskey23bd4802006-07-12 19:15:43 +0000256 // Emit instruction base values
Owen Anderson773642d2012-03-06 21:48:32 +0000257 o << " static const uint64_t InstBits[] = {\n";
Craig Topper28851b62016-02-01 01:33:42 +0000258 for (const CodeGenInstruction *CGI : NumberedInstructions) {
Jim Laskey23bd4802006-07-12 19:15:43 +0000259 Record *R = CGI->TheDef;
Jim Grosbachdaab6602010-10-07 16:56:28 +0000260
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000261 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
262 R->getValueAsBit("isPseudo")) {
Owen Andersond845d9d2012-01-24 18:37:29 +0000263 o << " UINT64_C(0),\n";
Jim Laskey23bd4802006-07-12 19:15:43 +0000264 continue;
265 }
Jim Grosbachdaab6602010-10-07 16:56:28 +0000266
David Greeneaf8ee2c2011-07-29 22:43:06 +0000267 BitsInit *BI = R->getValueAsBitsInit("Inst");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000268
Chris Lattnerc19d5102010-11-15 06:59:17 +0000269 // Start by filling in fixed values.
Owen Anderson773642d2012-03-06 21:48:32 +0000270 uint64_t Value = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000271 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000272 if (BitInit *B = dyn_cast<BitInit>(BI->getBit(e-i-1)))
Owen Anderson773642d2012-03-06 21:48:32 +0000273 Value |= (uint64_t)B->getValue() << (e-i-1);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000274 }
Owen Andersond845d9d2012-01-24 18:37:29 +0000275 o << " UINT64_C(" << Value << ")," << '\t' << "// " << R->getName() << "\n";
Jim Laskey23bd4802006-07-12 19:15:43 +0000276 }
Owen Andersond845d9d2012-01-24 18:37:29 +0000277 o << " UINT64_C(0)\n };\n";
Jim Grosbachdaab6602010-10-07 16:56:28 +0000278
Jim Laskeya44f6262006-07-13 21:02:53 +0000279 // Map to accumulate all the cases.
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000280 std::map<std::string, std::vector<std::string>> CaseMap;
Jim Grosbachdaab6602010-10-07 16:56:28 +0000281
Jim Laskeya44f6262006-07-13 21:02:53 +0000282 // 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 Grosbachf3fd36e2011-07-06 21:33:38 +0000286 if (R->getValueAsString("Namespace") == "TargetOpcode" ||
287 R->getValueAsBit("isPseudo"))
Jakob Stoklund Olesen3b1657b2010-07-02 21:44:22 +0000288 continue;
Craig Topper2b8419a2017-05-31 19:01:11 +0000289 std::string InstName =
290 (R->getValueAsString("Namespace") + "::" + R->getName()).str();
Chris Lattnerc19d5102010-11-15 06:59:17 +0000291 std::string Case = getInstructionCase(R, Target);
Dan Gohman60a446a2009-04-13 15:38:05 +0000292
Craig Topper2b8419a2017-05-31 19:01:11 +0000293 CaseMap[Case].push_back(std::move(InstName));
Jim Laskeya44f6262006-07-13 21:02:53 +0000294 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000295
Jim Laskeya44f6262006-07-13 21:02:53 +0000296 // Emit initial function code
297 o << " const unsigned opcode = MI.getOpcode();\n"
Owen Anderson773642d2012-03-06 21:48:32 +0000298 << " uint64_t Value = InstBits[opcode];\n"
299 << " uint64_t op = 0;\n"
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000300 << " (void)op; // suppress warning\n"
Jim Laskeya44f6262006-07-13 21:02:53 +0000301 << " switch (opcode) {\n";
302
303 // Emit each case statement
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000304 std::map<std::string, std::vector<std::string>>::iterator IE, EE;
Jim Laskeya44f6262006-07-13 21:02:53 +0000305 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 Grosbachcd25b862011-02-03 23:26:36 +0000311 o << " case " << InstList[i] << ":";
Jim Laskeya44f6262006-07-13 21:02:53 +0000312 }
313 o << " {\n";
314 o << Case;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000315 o << " break;\n"
316 << " }\n";
317 }
318
Misha Brukman8393c152004-10-14 05:53:01 +0000319 // Default case: unhandled opcode
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000320 o << " default:\n"
Alp Tokere69170a2014-06-26 22:52:05 +0000321 << " std::string msg;\n"
322 << " raw_string_ostream Msg(msg);\n"
Torok Edwinfa040022009-07-08 19:04:27 +0000323 << " Msg << \"Not supported instr: \" << MI;\n"
Chris Lattner2104b8d2010-04-07 22:58:41 +0000324 << " report_fatal_error(Msg.str());\n"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000325 << " }\n"
326 << " return Value;\n"
Misha Brukman8393c152004-10-14 05:53:01 +0000327 << "}\n\n";
Daniel Sanders72db2a32016-11-19 13:05:44 +0000328
329 const auto &All = SubtargetFeatureInfo::getAll(Records);
330 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> SubtargetFeatures;
331 SubtargetFeatures.insert(All.begin(), All.end());
332
333 o << "#ifdef ENABLE_INSTR_PREDICATE_VERIFIER\n"
334 << "#undef ENABLE_INSTR_PREDICATE_VERIFIER\n"
335 << "#include <sstream>\n\n";
336
337 // Emit the subtarget feature enumeration.
Stanislav Mekhanoshine98944e2019-03-11 17:04:35 +0000338 SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures,
339 o);
Daniel Sanders72db2a32016-11-19 13:05:44 +0000340
341 // Emit the name table for error messages.
342 o << "#ifndef NDEBUG\n";
343 SubtargetFeatureInfo::emitNameTable(SubtargetFeatures, o);
344 o << "#endif // NDEBUG\n";
345
346 // Emit the available features compute function.
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000347 SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
Daniel Sanders72db2a32016-11-19 13:05:44 +0000348 Target.getName(), "MCCodeEmitter", "computeAvailableFeatures",
349 SubtargetFeatures, o);
350
Stanislav Mekhanoshine98944e2019-03-11 17:04:35 +0000351 std::vector<std::vector<Record *>> FeatureBitsets;
Daniel Sanders72db2a32016-11-19 13:05:44 +0000352 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
Stanislav Mekhanoshine98944e2019-03-11 17:04:35 +0000353 FeatureBitsets.emplace_back();
Daniel Sanders72db2a32016-11-19 13:05:44 +0000354 for (Record *Predicate : Inst->TheDef->getValueAsListOfDefs("Predicates")) {
355 const auto &I = SubtargetFeatures.find(Predicate);
356 if (I != SubtargetFeatures.end())
Stanislav Mekhanoshine98944e2019-03-11 17:04:35 +0000357 FeatureBitsets.back().push_back(I->second.TheDef);
Daniel Sanders72db2a32016-11-19 13:05:44 +0000358 }
Stanislav Mekhanoshine98944e2019-03-11 17:04:35 +0000359 }
360
361 llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A,
362 const std::vector<Record *> &B) {
363 if (A.size() < B.size())
364 return true;
365 if (A.size() > B.size())
366 return false;
367 for (const auto &Pair : zip(A, B)) {
368 if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName())
369 return true;
370 if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName())
371 return false;
372 }
373 return false;
374 });
375 FeatureBitsets.erase(
376 std::unique(FeatureBitsets.begin(), FeatureBitsets.end()),
377 FeatureBitsets.end());
378 o << "#ifndef NDEBUG\n"
379 << "// Feature bitsets.\n"
380 << "enum : " << getMinimalTypeForRange(FeatureBitsets.size()) << " {\n"
381 << " CEFBS_None,\n";
382 for (const auto &FeatureBitset : FeatureBitsets) {
383 if (FeatureBitset.empty())
384 continue;
385 o << " " << getNameForFeatureBitset(FeatureBitset) << ",\n";
386 }
387 o << "};\n\n"
388 << "const static FeatureBitset FeatureBitsets[] {\n"
389 << " {}, // CEFBS_None\n";
390 for (const auto &FeatureBitset : FeatureBitsets) {
391 if (FeatureBitset.empty())
392 continue;
393 o << " {";
394 for (const auto &Feature : FeatureBitset) {
395 const auto &I = SubtargetFeatures.find(Feature);
396 assert(I != SubtargetFeatures.end() && "Didn't import predicate?");
397 o << I->second.getEnumBitName() << ", ";
398 }
399 o << "},\n";
400 }
401 o << "};\n"
402 << "#endif // NDEBUG\n\n";
403
404
405 // Emit the predicate verifier.
406 o << "void " << Target.getName()
407 << "MCCodeEmitter::verifyInstructionPredicates(\n"
408 << " const MCInst &Inst, const FeatureBitset &AvailableFeatures) const {\n"
409 << "#ifndef NDEBUG\n"
410 << " static " << getMinimalTypeForRange(FeatureBitsets.size())
411 << " RequiredFeaturesRefs[] = {\n";
412 unsigned InstIdx = 0;
413 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
414 o << " CEFBS";
415 unsigned NumPredicates = 0;
416 for (Record *Predicate : Inst->TheDef->getValueAsListOfDefs("Predicates")) {
417 const auto &I = SubtargetFeatures.find(Predicate);
418 if (I != SubtargetFeatures.end()) {
419 o << '_' << I->second.TheDef->getName();
420 NumPredicates++;
421 }
422 }
423 if (!NumPredicates)
424 o << "_None";
425 o << ", // " << Inst->TheDef->getName() << " = " << InstIdx << "\n";
Daniel Sanders72db2a32016-11-19 13:05:44 +0000426 InstIdx++;
427 }
428 o << " };\n\n";
429 o << " assert(Inst.getOpcode() < " << InstIdx << ");\n";
Stanislav Mekhanoshine98944e2019-03-11 17:04:35 +0000430 o << " const FeatureBitset &RequiredFeatures = "
431 "FeatureBitsets[RequiredFeaturesRefs[Inst.getOpcode()]];\n";
432 o << " FeatureBitset MissingFeatures =\n"
433 << " (AvailableFeatures & RequiredFeatures) ^\n"
434 << " RequiredFeatures;\n"
435 << " if (MissingFeatures.any()) {\n"
Daniel Sanders72db2a32016-11-19 13:05:44 +0000436 << " std::ostringstream Msg;\n"
Daniel Sandersc95590b2016-11-19 14:47:41 +0000437 << " Msg << \"Attempting to emit \" << "
438 "MCII.getName(Inst.getOpcode()).str()\n"
Daniel Sanders72db2a32016-11-19 13:05:44 +0000439 << " << \" instruction but the \";\n"
Stanislav Mekhanoshine98944e2019-03-11 17:04:35 +0000440 << " for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i)\n"
441 << " if (MissingFeatures.test(i))\n"
Daniel Sanders72db2a32016-11-19 13:05:44 +0000442 << " Msg << SubtargetFeatureNames[i] << \" \";\n"
443 << " Msg << \"predicate(s) are not met\";\n"
444 << " report_fatal_error(Msg.str());\n"
445 << " }\n"
Daniel Sandersc95590b2016-11-19 14:47:41 +0000446 << "#else\n"
447 << "// Silence unused variable warning on targets that don't use MCII for "
448 "other purposes (e.g. BPF).\n"
449 << "(void)MCII;\n"
Daniel Sanders72db2a32016-11-19 13:05:44 +0000450 << "#endif // NDEBUG\n";
451 o << "}\n";
452 o << "#endif\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000453}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000454
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000455} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000456
457namespace llvm {
458
459void EmitCodeEmitter(RecordKeeper &RK, raw_ostream &OS) {
460 emitSourceFileHeader("Machine Code Emitter", OS);
461 CodeEmitterGen(RK).run(OS);
462}
463
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000464} // end namespace llvm