blob: a4e993f80ec9bbf5d66ad29513464c513067452d [file] [log] [blame]
Chris Lattnerbf5f3942007-02-27 22:05:51 +00001//===- CallingConvEmitter.cpp - Generate calling conventions --------------===//
2//
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
Chris Lattnerbf5f3942007-02-27 22:05:51 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This tablegen backend is responsible for emitting descriptions of the calling
10// conventions supported by this target.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbf5f3942007-02-27 22:05:51 +000014#include "CodeGenTarget.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000015#include "llvm/TableGen/Error.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000016#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000017#include "llvm/TableGen/TableGenBackend.h"
18#include <cassert>
Chris Lattnerbf5f3942007-02-27 22:05:51 +000019using namespace llvm;
20
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000021namespace {
22class CallingConvEmitter {
23 RecordKeeper &Records;
24public:
25 explicit CallingConvEmitter(RecordKeeper &R) : Records(R) {}
26
27 void run(raw_ostream &o);
28
29private:
30 void EmitCallingConv(Record *CC, raw_ostream &O);
31 void EmitAction(Record *Action, unsigned Indent, raw_ostream &O);
32 unsigned Counter;
33};
34} // End anonymous namespace
35
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000036void CallingConvEmitter::run(raw_ostream &O) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +000037 std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv");
Daniel Sandersca80f1a2014-11-01 17:38:22 +000038
39 // Emit prototypes for all of the non-custom CC's so that they can forward ref
40 // each other.
Javed Absardfd8e2f2017-10-16 14:52:26 +000041 for (Record *CC : CCs) {
42 if (!CC->getValueAsBit("Custom")) {
Reid Kleckner38f99002019-01-19 00:33:02 +000043 unsigned Pad = CC->getName().size();
44 if (CC->getValueAsBit("Entry")) {
45 O << "bool llvm::";
46 Pad += 12;
47 } else {
48 O << "static bool ";
49 Pad += 13;
50 }
51 O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n"
52 << std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
53 << std::string(Pad, ' ')
Daniel Sandersca80f1a2014-11-01 17:38:22 +000054 << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n";
55 }
Chris Lattnerbf5f3942007-02-27 22:05:51 +000056 }
Daniel Sandersca80f1a2014-11-01 17:38:22 +000057
58 // Emit each non-custom calling convention description in full.
Javed Absardfd8e2f2017-10-16 14:52:26 +000059 for (Record *CC : CCs) {
60 if (!CC->getValueAsBit("Custom"))
61 EmitCallingConv(CC, O);
Daniel Sandersca80f1a2014-11-01 17:38:22 +000062 }
Chris Lattnerbf5f3942007-02-27 22:05:51 +000063}
64
65
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000066void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
David Greeneaf8ee2c2011-07-29 22:43:06 +000067 ListInit *CCActions = CC->getValueAsListInit("Actions");
Chris Lattnerbf5f3942007-02-27 22:05:51 +000068 Counter = 0;
69
Reid Kleckner38f99002019-01-19 00:33:02 +000070 O << "\n\n";
71 unsigned Pad = CC->getName().size();
72 if (CC->getValueAsBit("Entry")) {
73 O << "bool llvm::";
74 Pad += 12;
75 } else {
76 O << "static bool ";
77 Pad += 13;
78 }
79 O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n"
80 << std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
81 << std::string(Pad, ' ') << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +000082 // Emit all of the actions, in order.
Craig Topper664f6a02015-06-02 04:15:57 +000083 for (unsigned i = 0, e = CCActions->size(); i != e; ++i) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +000084 O << "\n";
85 EmitAction(CCActions->getElementAsRecord(i), 2, O);
86 }
87
88 O << "\n return true; // CC didn't match.\n";
89 O << "}\n";
90}
91
92void CallingConvEmitter::EmitAction(Record *Action,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000093 unsigned Indent, raw_ostream &O) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +000094 std::string IndentStr = std::string(Indent, ' ');
95
96 if (Action->isSubClassOf("CCPredicateAction")) {
97 O << IndentStr << "if (";
98
Chris Lattner7fb08232007-02-28 05:29:06 +000099 if (Action->isSubClassOf("CCIfType")) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000100 ListInit *VTs = Action->getValueAsListInit("VTs");
Craig Topper664f6a02015-06-02 04:15:57 +0000101 for (unsigned i = 0, e = VTs->size(); i != e; ++i) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000102 Record *VT = VTs->getElementAsRecord(i);
Chris Lattner22778a32007-02-28 04:43:48 +0000103 if (i != 0) O << " ||\n " << IndentStr;
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000104 O << "LocVT == " << getEnumName(getValueType(VT));
105 }
106
Chris Lattner7fb08232007-02-28 05:29:06 +0000107 } else if (Action->isSubClassOf("CCIf")) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000108 O << Action->getValueAsString("Predicate");
109 } else {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000110 errs() << *Action;
Daniel Sandersdff673b2019-02-12 17:36:57 +0000111 PrintFatalError(Action->getLoc(), "Unknown CCPredicateAction!");
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000112 }
113
114 O << ") {\n";
115 EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O);
116 O << IndentStr << "}\n";
117 } else {
118 if (Action->isSubClassOf("CCDelegateTo")) {
119 Record *CC = Action->getValueAsDef("CC");
120 O << IndentStr << "if (!" << CC->getName()
121 << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
122 << IndentStr << " return false;\n";
123 } else if (Action->isSubClassOf("CCAssignToReg")) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000124 ListInit *RegList = Action->getValueAsListInit("RegList");
Craig Topper664f6a02015-06-02 04:15:57 +0000125 if (RegList->size() == 1) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000126 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
127 O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n";
128 } else {
Craig Topper840beec2014-04-04 05:16:06 +0000129 O << IndentStr << "static const MCPhysReg RegList" << ++Counter
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000130 << "[] = {\n";
131 O << IndentStr << " ";
Craig Topper664f6a02015-06-02 04:15:57 +0000132 for (unsigned i = 0, e = RegList->size(); i != e; ++i) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000133 if (i != 0) O << ", ";
134 O << getQualifiedName(RegList->getElementAsRecord(i));
135 }
136 O << "\n" << IndentStr << "};\n";
137 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000138 << Counter << ")) {\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000139 }
140 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
141 << "Reg, LocVT, LocInfo));\n";
142 O << IndentStr << " return false;\n";
143 O << IndentStr << "}\n";
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000144 } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000145 ListInit *RegList = Action->getValueAsListInit("RegList");
146 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
Craig Topper664f6a02015-06-02 04:15:57 +0000147 if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size())
Daniel Sandersdff673b2019-02-12 17:36:57 +0000148 PrintFatalError(Action->getLoc(),
149 "Invalid length of list of shadowed registers");
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000150
Craig Topper664f6a02015-06-02 04:15:57 +0000151 if (RegList->size() == 1) {
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000152 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
153 O << getQualifiedName(RegList->getElementAsRecord(0));
154 O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0));
155 O << ")) {\n";
156 } else {
157 unsigned RegListNumber = ++Counter;
158 unsigned ShadowRegListNumber = ++Counter;
159
Craig Topper840beec2014-04-04 05:16:06 +0000160 O << IndentStr << "static const MCPhysReg RegList" << RegListNumber
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000161 << "[] = {\n";
162 O << IndentStr << " ";
Craig Topper664f6a02015-06-02 04:15:57 +0000163 for (unsigned i = 0, e = RegList->size(); i != e; ++i) {
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000164 if (i != 0) O << ", ";
165 O << getQualifiedName(RegList->getElementAsRecord(i));
166 }
167 O << "\n" << IndentStr << "};\n";
168
Craig Topper840beec2014-04-04 05:16:06 +0000169 O << IndentStr << "static const MCPhysReg RegList"
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000170 << ShadowRegListNumber << "[] = {\n";
171 O << IndentStr << " ";
Craig Topper664f6a02015-06-02 04:15:57 +0000172 for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i) {
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000173 if (i != 0) O << ", ";
174 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
175 }
176 O << "\n" << IndentStr << "};\n";
177
178 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
179 << RegListNumber << ", " << "RegList" << ShadowRegListNumber
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000180 << ")) {\n";
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000181 }
182 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
183 << "Reg, LocVT, LocInfo));\n";
184 O << IndentStr << " return false;\n";
185 O << IndentStr << "}\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000186 } else if (Action->isSubClassOf("CCAssignToStack")) {
187 int Size = Action->getValueAsInt("Size");
188 int Align = Action->getValueAsInt("Align");
Duncan Sandse2287ed2007-11-14 08:29:13 +0000189
Evan Chenga9ecdf62008-01-15 03:10:35 +0000190 O << IndentStr << "unsigned Offset" << ++Counter
191 << " = State.AllocateStack(";
Duncan Sandse2287ed2007-11-14 08:29:13 +0000192 if (Size)
Evan Chenga9ecdf62008-01-15 03:10:35 +0000193 O << Size << ", ";
Duncan Sandse2287ed2007-11-14 08:29:13 +0000194 else
Eric Christopherd9134482014-08-04 21:25:23 +0000195 O << "\n" << IndentStr
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000196 << " State.getMachineFunction().getDataLayout()."
197 "getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())),"
Eric Christopherd9134482014-08-04 21:25:23 +0000198 " ";
Duncan Sandse2287ed2007-11-14 08:29:13 +0000199 if (Align)
Guillaume Chatelet80845db2020-06-04 21:56:01 +0000200 O << "Align(" << Align << ")";
Duncan Sandse2287ed2007-11-14 08:29:13 +0000201 else
Guillaume Chatelet2aa48302020-06-05 15:58:20 +0000202 O << "\n"
203 << IndentStr
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000204 << " State.getMachineFunction().getDataLayout()."
Guillaume Chatelet2aa48302020-06-05 15:58:20 +0000205 "getABITypeAlign(EVT(LocVT).getTypeForEVT(State.getContext()"
Eric Christopherd9134482014-08-04 21:25:23 +0000206 "))";
Evan Chenga9ecdf62008-01-15 03:10:35 +0000207 O << ");\n" << IndentStr
Duncan Sandse2287ed2007-11-14 08:29:13 +0000208 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
Dale Johannesenb988e7e2007-11-10 22:07:15 +0000209 << Counter << ", LocVT, LocInfo));\n";
210 O << IndentStr << "return false;\n";
Oliver Stannard1dc10342014-02-07 11:19:53 +0000211 } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
212 int Size = Action->getValueAsInt("Size");
213 int Align = Action->getValueAsInt("Align");
214 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
215
216 unsigned ShadowRegListNumber = ++Counter;
217
Craig Topper840beec2014-04-04 05:16:06 +0000218 O << IndentStr << "static const MCPhysReg ShadowRegList"
Oliver Stannard1dc10342014-02-07 11:19:53 +0000219 << ShadowRegListNumber << "[] = {\n";
220 O << IndentStr << " ";
Craig Topper664f6a02015-06-02 04:15:57 +0000221 for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i) {
Oliver Stannard1dc10342014-02-07 11:19:53 +0000222 if (i != 0) O << ", ";
223 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
224 }
225 O << "\n" << IndentStr << "};\n";
226
227 O << IndentStr << "unsigned Offset" << ++Counter
Guillaume Chatelet80845db2020-06-04 21:56:01 +0000228 << " = State.AllocateStack(" << Size << ", Align(" << Align << "), "
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000229 << "ShadowRegList" << ShadowRegListNumber << ");\n";
Oliver Stannard1dc10342014-02-07 11:19:53 +0000230 O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
231 << Counter << ", LocVT, LocInfo));\n";
232 O << IndentStr << "return false;\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000233 } else if (Action->isSubClassOf("CCPromoteToType")) {
Chris Lattner22778a32007-02-28 04:43:48 +0000234 Record *DestTy = Action->getValueAsDef("DestTy");
Lang Hames06234ec2014-01-14 19:56:36 +0000235 MVT::SimpleValueType DestVT = getValueType(DestTy);
236 O << IndentStr << "LocVT = " << getEnumName(DestVT) <<";\n";
237 if (MVT(DestVT).isFloatingPoint()) {
238 O << IndentStr << "LocInfo = CCValAssign::FPExt;\n";
239 } else {
240 O << IndentStr << "if (ArgFlags.isSExt())\n"
241 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
242 << IndentStr << "else if (ArgFlags.isZExt())\n"
243 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
244 << IndentStr << "else\n"
245 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
246 }
Daniel Sandersae275e32014-09-25 12:15:05 +0000247 } else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) {
248 Record *DestTy = Action->getValueAsDef("DestTy");
249 MVT::SimpleValueType DestVT = getValueType(DestTy);
250 O << IndentStr << "LocVT = " << getEnumName(DestVT) << ";\n";
251 if (MVT(DestVT).isFloatingPoint()) {
Daniel Sandersdff673b2019-02-12 17:36:57 +0000252 PrintFatalError(Action->getLoc(),
253 "CCPromoteToUpperBitsInType does not handle floating "
Daniel Sandersae275e32014-09-25 12:15:05 +0000254 "point");
255 } else {
256 O << IndentStr << "if (ArgFlags.isSExt())\n"
257 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExtUpper;\n"
258 << IndentStr << "else if (ArgFlags.isZExt())\n"
259 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExtUpper;\n"
260 << IndentStr << "else\n"
261 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExtUpper;\n";
262 }
Bob Wilsona4c22902009-04-17 19:07:39 +0000263 } else if (Action->isSubClassOf("CCBitConvertToType")) {
264 Record *DestTy = Action->getValueAsDef("DestTy");
265 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
266 O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
Tim Northoverf1c28922019-09-12 10:22:23 +0000267 } else if (Action->isSubClassOf("CCTruncToType")) {
268 Record *DestTy = Action->getValueAsDef("DestTy");
269 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
270 O << IndentStr << "LocInfo = CCValAssign::Trunc;\n";
Anton Korobeynikov442beab2009-08-03 08:13:56 +0000271 } else if (Action->isSubClassOf("CCPassIndirect")) {
272 Record *DestTy = Action->getValueAsDef("DestTy");
273 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
274 O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
Evan Cheng4d70ba32008-01-15 03:34:58 +0000275 } else if (Action->isSubClassOf("CCPassByVal")) {
276 int Size = Action->getValueAsInt("Size");
277 int Align = Action->getValueAsInt("Align");
Guillaume Chatelet94b0c322020-06-08 08:48:49 +0000278 O << IndentStr << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
279 << Size << ", Align(" << Align << "), ArgFlags);\n";
Rafael Espindola66011c12007-08-10 14:44:42 +0000280 O << IndentStr << "return false;\n";
Bob Wilsona4c22902009-04-17 19:07:39 +0000281 } else if (Action->isSubClassOf("CCCustom")) {
282 O << IndentStr
283 << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
284 << "LocVT, LocInfo, ArgFlags, State))\n";
285 O << IndentStr << IndentStr << "return false;\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000286 } else {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000287 errs() << *Action;
Daniel Sandersdff673b2019-02-12 17:36:57 +0000288 PrintFatalError(Action->getLoc(), "Unknown CCAction!");
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000289 }
290 }
Chris Lattnercbebe462007-02-27 22:08:27 +0000291}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000292
293namespace llvm {
294
295void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
296 emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
297 CallingConvEmitter(RK).run(OS);
298}
299
300} // End llvm namespace