blob: 96bd3361ffce8e2258a6879cd37f5abf30fda961 [file] [log] [blame]
Chris Lattnerbf5f3942007-02-27 22:05:51 +00001//===- CallingConvEmitter.cpp - Generate calling conventions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerbf5f3942007-02-27 22:05:51 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend is responsible for emitting descriptions of the calling
11// conventions supported by this target.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerbf5f3942007-02-27 22:05:51 +000015#include "CodeGenTarget.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000016#include "llvm/TableGen/Error.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000017#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000018#include "llvm/TableGen/TableGenBackend.h"
19#include <cassert>
Chris Lattnerbf5f3942007-02-27 22:05:51 +000020using namespace llvm;
21
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000022namespace {
23class CallingConvEmitter {
24 RecordKeeper &Records;
25public:
26 explicit CallingConvEmitter(RecordKeeper &R) : Records(R) {}
27
28 void run(raw_ostream &o);
29
30private:
31 void EmitCallingConv(Record *CC, raw_ostream &O);
32 void EmitAction(Record *Action, unsigned Indent, raw_ostream &O);
33 unsigned Counter;
34};
35} // End anonymous namespace
36
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000037void CallingConvEmitter::run(raw_ostream &O) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +000038
39 std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv");
40
41 // Emit prototypes for all of the CC's so that they can forward ref each
42 // other.
43 for (unsigned i = 0, e = CCs.size(); i != e; ++i) {
44 O << "static bool " << CCs[i]->getName()
Duncan Sands71049f72010-11-04 10:49:57 +000045 << "(unsigned ValNo, MVT ValVT,\n"
Chris Lattnerbf5f3942007-02-27 22:05:51 +000046 << std::string(CCs[i]->getName().size()+13, ' ')
Duncan Sandsf5dda012010-11-03 11:35:31 +000047 << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
Chris Lattner22778a32007-02-28 04:43:48 +000048 << std::string(CCs[i]->getName().size()+13, ' ')
Duncan Sandsd97eea32008-03-21 09:14:45 +000049 << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +000050 }
51
52 // Emit each calling convention description in full.
53 for (unsigned i = 0, e = CCs.size(); i != e; ++i)
54 EmitCallingConv(CCs[i], O);
55}
56
57
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000058void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
David Greeneaf8ee2c2011-07-29 22:43:06 +000059 ListInit *CCActions = CC->getValueAsListInit("Actions");
Chris Lattnerbf5f3942007-02-27 22:05:51 +000060 Counter = 0;
61
62 O << "\n\nstatic bool " << CC->getName()
Duncan Sands71049f72010-11-04 10:49:57 +000063 << "(unsigned ValNo, MVT ValVT,\n"
Chris Lattnerbf5f3942007-02-27 22:05:51 +000064 << std::string(CC->getName().size()+13, ' ')
Duncan Sandsf5dda012010-11-03 11:35:31 +000065 << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
Chris Lattner22778a32007-02-28 04:43:48 +000066 << std::string(CC->getName().size()+13, ' ')
Duncan Sandsd97eea32008-03-21 09:14:45 +000067 << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +000068 // Emit all of the actions, in order.
69 for (unsigned i = 0, e = CCActions->getSize(); i != e; ++i) {
70 O << "\n";
71 EmitAction(CCActions->getElementAsRecord(i), 2, O);
72 }
73
74 O << "\n return true; // CC didn't match.\n";
75 O << "}\n";
76}
77
78void CallingConvEmitter::EmitAction(Record *Action,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000079 unsigned Indent, raw_ostream &O) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +000080 std::string IndentStr = std::string(Indent, ' ');
81
82 if (Action->isSubClassOf("CCPredicateAction")) {
83 O << IndentStr << "if (";
84
Chris Lattner7fb08232007-02-28 05:29:06 +000085 if (Action->isSubClassOf("CCIfType")) {
David Greeneaf8ee2c2011-07-29 22:43:06 +000086 ListInit *VTs = Action->getValueAsListInit("VTs");
Chris Lattnerbf5f3942007-02-27 22:05:51 +000087 for (unsigned i = 0, e = VTs->getSize(); i != e; ++i) {
88 Record *VT = VTs->getElementAsRecord(i);
Chris Lattner22778a32007-02-28 04:43:48 +000089 if (i != 0) O << " ||\n " << IndentStr;
Chris Lattnerbf5f3942007-02-27 22:05:51 +000090 O << "LocVT == " << getEnumName(getValueType(VT));
91 }
92
Chris Lattner7fb08232007-02-28 05:29:06 +000093 } else if (Action->isSubClassOf("CCIf")) {
Chris Lattnerbf5f3942007-02-27 22:05:51 +000094 O << Action->getValueAsString("Predicate");
95 } else {
96 Action->dump();
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000097 PrintFatalError("Unknown CCPredicateAction!");
Chris Lattnerbf5f3942007-02-27 22:05:51 +000098 }
99
100 O << ") {\n";
101 EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O);
102 O << IndentStr << "}\n";
103 } else {
104 if (Action->isSubClassOf("CCDelegateTo")) {
105 Record *CC = Action->getValueAsDef("CC");
106 O << IndentStr << "if (!" << CC->getName()
107 << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
108 << IndentStr << " return false;\n";
109 } else if (Action->isSubClassOf("CCAssignToReg")) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000110 ListInit *RegList = Action->getValueAsListInit("RegList");
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000111 if (RegList->getSize() == 1) {
112 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
113 O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n";
114 } else {
Craig Topperbef78fc2012-03-11 07:57:25 +0000115 O << IndentStr << "static const uint16_t RegList" << ++Counter
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000116 << "[] = {\n";
117 O << IndentStr << " ";
118 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
119 if (i != 0) O << ", ";
120 O << getQualifiedName(RegList->getElementAsRecord(i));
121 }
122 O << "\n" << IndentStr << "};\n";
123 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
124 << Counter << ", " << RegList->getSize() << ")) {\n";
125 }
126 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
127 << "Reg, LocVT, LocInfo));\n";
128 O << IndentStr << " return false;\n";
129 O << IndentStr << "}\n";
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000130 } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000131 ListInit *RegList = Action->getValueAsListInit("RegList");
132 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000133 if (ShadowRegList->getSize() >0 &&
134 ShadowRegList->getSize() != RegList->getSize())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000135 PrintFatalError("Invalid length of list of shadowed registers");
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000136
137 if (RegList->getSize() == 1) {
138 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
139 O << getQualifiedName(RegList->getElementAsRecord(0));
140 O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0));
141 O << ")) {\n";
142 } else {
143 unsigned RegListNumber = ++Counter;
144 unsigned ShadowRegListNumber = ++Counter;
145
Craig Topperbef78fc2012-03-11 07:57:25 +0000146 O << IndentStr << "static const uint16_t RegList" << RegListNumber
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000147 << "[] = {\n";
148 O << IndentStr << " ";
149 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
150 if (i != 0) O << ", ";
151 O << getQualifiedName(RegList->getElementAsRecord(i));
152 }
153 O << "\n" << IndentStr << "};\n";
154
Craig Topperbef78fc2012-03-11 07:57:25 +0000155 O << IndentStr << "static const uint16_t RegList"
Anton Korobeynikov20c9e4c2008-04-02 05:23:57 +0000156 << ShadowRegListNumber << "[] = {\n";
157 O << IndentStr << " ";
158 for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) {
159 if (i != 0) O << ", ";
160 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
161 }
162 O << "\n" << IndentStr << "};\n";
163
164 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
165 << RegListNumber << ", " << "RegList" << ShadowRegListNumber
166 << ", " << RegList->getSize() << ")) {\n";
167 }
168 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
169 << "Reg, LocVT, LocInfo));\n";
170 O << IndentStr << " return false;\n";
171 O << IndentStr << "}\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000172 } else if (Action->isSubClassOf("CCAssignToStack")) {
173 int Size = Action->getValueAsInt("Size");
174 int Align = Action->getValueAsInt("Align");
Duncan Sandse2287ed2007-11-14 08:29:13 +0000175
Evan Chenga9ecdf62008-01-15 03:10:35 +0000176 O << IndentStr << "unsigned Offset" << ++Counter
177 << " = State.AllocateStack(";
Duncan Sandse2287ed2007-11-14 08:29:13 +0000178 if (Size)
Evan Chenga9ecdf62008-01-15 03:10:35 +0000179 O << Size << ", ";
Duncan Sandse2287ed2007-11-14 08:29:13 +0000180 else
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000181 O << "\n" << IndentStr << " State.getTarget().getDataLayout()"
Duncan Sandsf5dda012010-11-03 11:35:31 +0000182 "->getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())), ";
Duncan Sandse2287ed2007-11-14 08:29:13 +0000183 if (Align)
184 O << Align;
185 else
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000186 O << "\n" << IndentStr << " State.getTarget().getDataLayout()"
Duncan Sandsf5dda012010-11-03 11:35:31 +0000187 "->getABITypeAlignment(EVT(LocVT).getTypeForEVT(State.getContext()))";
Evan Chenga9ecdf62008-01-15 03:10:35 +0000188 O << ");\n" << IndentStr
Duncan Sandse2287ed2007-11-14 08:29:13 +0000189 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
Dale Johannesenb988e7e2007-11-10 22:07:15 +0000190 << Counter << ", LocVT, LocInfo));\n";
191 O << IndentStr << "return false;\n";
Oliver Stannard1dc10342014-02-07 11:19:53 +0000192 } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
193 int Size = Action->getValueAsInt("Size");
194 int Align = Action->getValueAsInt("Align");
195 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
196
197 unsigned ShadowRegListNumber = ++Counter;
198
199 O << IndentStr << "static const uint16_t ShadowRegList"
200 << ShadowRegListNumber << "[] = {\n";
201 O << IndentStr << " ";
202 for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) {
203 if (i != 0) O << ", ";
204 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
205 }
206 O << "\n" << IndentStr << "};\n";
207
208 O << IndentStr << "unsigned Offset" << ++Counter
209 << " = State.AllocateStack("
210 << Size << ", " << Align << ", "
211 << "ShadowRegList" << ShadowRegListNumber << ", "
212 << ShadowRegList->getSize() << ");\n";
213 O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
214 << Counter << ", LocVT, LocInfo));\n";
215 O << IndentStr << "return false;\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000216 } else if (Action->isSubClassOf("CCPromoteToType")) {
Chris Lattner22778a32007-02-28 04:43:48 +0000217 Record *DestTy = Action->getValueAsDef("DestTy");
Lang Hames06234ec2014-01-14 19:56:36 +0000218 MVT::SimpleValueType DestVT = getValueType(DestTy);
219 O << IndentStr << "LocVT = " << getEnumName(DestVT) <<";\n";
220 if (MVT(DestVT).isFloatingPoint()) {
221 O << IndentStr << "LocInfo = CCValAssign::FPExt;\n";
222 } else {
223 O << IndentStr << "if (ArgFlags.isSExt())\n"
224 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
225 << IndentStr << "else if (ArgFlags.isZExt())\n"
226 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
227 << IndentStr << "else\n"
228 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
229 }
Bob Wilsona4c22902009-04-17 19:07:39 +0000230 } else if (Action->isSubClassOf("CCBitConvertToType")) {
231 Record *DestTy = Action->getValueAsDef("DestTy");
232 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
233 O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
Anton Korobeynikov442beab2009-08-03 08:13:56 +0000234 } else if (Action->isSubClassOf("CCPassIndirect")) {
235 Record *DestTy = Action->getValueAsDef("DestTy");
236 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
237 O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
Evan Cheng4d70ba32008-01-15 03:34:58 +0000238 } else if (Action->isSubClassOf("CCPassByVal")) {
239 int Size = Action->getValueAsInt("Size");
240 int Align = Action->getValueAsInt("Align");
241 O << IndentStr
242 << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
243 << Size << ", " << Align << ", ArgFlags);\n";
Rafael Espindola66011c12007-08-10 14:44:42 +0000244 O << IndentStr << "return false;\n";
Bob Wilsona4c22902009-04-17 19:07:39 +0000245 } else if (Action->isSubClassOf("CCCustom")) {
246 O << IndentStr
247 << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
248 << "LocVT, LocInfo, ArgFlags, State))\n";
249 O << IndentStr << IndentStr << "return false;\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000250 } else {
251 Action->dump();
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000252 PrintFatalError("Unknown CCAction!");
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000253 }
254 }
Chris Lattnercbebe462007-02-27 22:08:27 +0000255}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000256
257namespace llvm {
258
259void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
260 emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
261 CallingConvEmitter(RK).run(OS);
262}
263
264} // End llvm namespace