blob: 907a90c76df9a938d3910e343e864579294e8fdb [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 Topper840beec2014-04-04 05:16:06 +0000115 O << IndentStr << "static const MCPhysReg 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 Topper840beec2014-04-04 05:16:06 +0000146 O << IndentStr << "static const MCPhysReg 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 Topper840beec2014-04-04 05:16:06 +0000155 O << IndentStr << "static const MCPhysReg 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
Eric Christopherd9134482014-08-04 21:25:23 +0000181 O << "\n" << IndentStr
Eric Christopherb5217502014-08-06 18:45:26 +0000182 << " State.getMachineFunction().getSubtarget().getDataLayout()"
Eric Christopherd9134482014-08-04 21:25:23 +0000183 "->getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())),"
184 " ";
Duncan Sandse2287ed2007-11-14 08:29:13 +0000185 if (Align)
186 O << Align;
187 else
Eric Christopherd9134482014-08-04 21:25:23 +0000188 O << "\n" << IndentStr
Eric Christopherb5217502014-08-06 18:45:26 +0000189 << " State.getMachineFunction().getSubtarget().getDataLayout()"
Eric Christopherd9134482014-08-04 21:25:23 +0000190 "->getABITypeAlignment(EVT(LocVT).getTypeForEVT(State.getContext()"
191 "))";
Evan Chenga9ecdf62008-01-15 03:10:35 +0000192 O << ");\n" << IndentStr
Duncan Sandse2287ed2007-11-14 08:29:13 +0000193 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
Dale Johannesenb988e7e2007-11-10 22:07:15 +0000194 << Counter << ", LocVT, LocInfo));\n";
195 O << IndentStr << "return false;\n";
Oliver Stannard1dc10342014-02-07 11:19:53 +0000196 } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
197 int Size = Action->getValueAsInt("Size");
198 int Align = Action->getValueAsInt("Align");
199 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
200
201 unsigned ShadowRegListNumber = ++Counter;
202
Craig Topper840beec2014-04-04 05:16:06 +0000203 O << IndentStr << "static const MCPhysReg ShadowRegList"
Oliver Stannard1dc10342014-02-07 11:19:53 +0000204 << ShadowRegListNumber << "[] = {\n";
205 O << IndentStr << " ";
206 for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) {
207 if (i != 0) O << ", ";
208 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
209 }
210 O << "\n" << IndentStr << "};\n";
211
212 O << IndentStr << "unsigned Offset" << ++Counter
213 << " = State.AllocateStack("
214 << Size << ", " << Align << ", "
215 << "ShadowRegList" << ShadowRegListNumber << ", "
216 << ShadowRegList->getSize() << ");\n";
217 O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
218 << Counter << ", LocVT, LocInfo));\n";
219 O << IndentStr << "return false;\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000220 } else if (Action->isSubClassOf("CCPromoteToType")) {
Chris Lattner22778a32007-02-28 04:43:48 +0000221 Record *DestTy = Action->getValueAsDef("DestTy");
Lang Hames06234ec2014-01-14 19:56:36 +0000222 MVT::SimpleValueType DestVT = getValueType(DestTy);
223 O << IndentStr << "LocVT = " << getEnumName(DestVT) <<";\n";
224 if (MVT(DestVT).isFloatingPoint()) {
225 O << IndentStr << "LocInfo = CCValAssign::FPExt;\n";
226 } else {
227 O << IndentStr << "if (ArgFlags.isSExt())\n"
228 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
229 << IndentStr << "else if (ArgFlags.isZExt())\n"
230 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
231 << IndentStr << "else\n"
232 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
233 }
Bob Wilsona4c22902009-04-17 19:07:39 +0000234 } else if (Action->isSubClassOf("CCBitConvertToType")) {
235 Record *DestTy = Action->getValueAsDef("DestTy");
236 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
237 O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
Anton Korobeynikov442beab2009-08-03 08:13:56 +0000238 } else if (Action->isSubClassOf("CCPassIndirect")) {
239 Record *DestTy = Action->getValueAsDef("DestTy");
240 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
241 O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
Evan Cheng4d70ba32008-01-15 03:34:58 +0000242 } else if (Action->isSubClassOf("CCPassByVal")) {
243 int Size = Action->getValueAsInt("Size");
244 int Align = Action->getValueAsInt("Align");
245 O << IndentStr
246 << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
247 << Size << ", " << Align << ", ArgFlags);\n";
Rafael Espindola66011c12007-08-10 14:44:42 +0000248 O << IndentStr << "return false;\n";
Bob Wilsona4c22902009-04-17 19:07:39 +0000249 } else if (Action->isSubClassOf("CCCustom")) {
250 O << IndentStr
251 << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
252 << "LocVT, LocInfo, ArgFlags, State))\n";
253 O << IndentStr << IndentStr << "return false;\n";
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000254 } else {
255 Action->dump();
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000256 PrintFatalError("Unknown CCAction!");
Chris Lattnerbf5f3942007-02-27 22:05:51 +0000257 }
258 }
Chris Lattnercbebe462007-02-27 22:08:27 +0000259}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000260
261namespace llvm {
262
263void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
264 emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
265 CallingConvEmitter(RK).run(OS);
266}
267
268} // End llvm namespace