blob: 94f3c6518ca05c9bd9fddcbfd69ee69da0cdb4c9 [file] [log] [blame]
Chris Lattner88ee2a12007-02-27 22:05:51 +00001//===- CallingConvEmitter.cpp - Generate calling conventions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-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 Lattner88ee2a12007-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 Lattner88ee2a12007-02-27 22:05:51 +000015#include "CodeGenTarget.h"
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000016#include "llvm/TableGen/Error.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000017#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000018#include "llvm/TableGen/TableGenBackend.h"
19#include <cassert>
Chris Lattner88ee2a12007-02-27 22:05:51 +000020using namespace llvm;
21
Jakob Stoklund Olesen6f36fa92012-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 Dunbar1a551802009-07-03 00:10:29 +000037void CallingConvEmitter::run(raw_ostream &O) {
Chris Lattner88ee2a12007-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 Sands1e96bab2010-11-04 10:49:57 +000045 << "(unsigned ValNo, MVT ValVT,\n"
Chris Lattner88ee2a12007-02-27 22:05:51 +000046 << std::string(CCs[i]->getName().size()+13, ' ')
Duncan Sands1440e8b2010-11-03 11:35:31 +000047 << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
Chris Lattner2092c8a2007-02-28 04:43:48 +000048 << std::string(CCs[i]->getName().size()+13, ' ')
Duncan Sands276dcbd2008-03-21 09:14:45 +000049 << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n";
Chris Lattner88ee2a12007-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 Dunbar1a551802009-07-03 00:10:29 +000058void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
David Greene05bce0b2011-07-29 22:43:06 +000059 ListInit *CCActions = CC->getValueAsListInit("Actions");
Chris Lattner88ee2a12007-02-27 22:05:51 +000060 Counter = 0;
61
62 O << "\n\nstatic bool " << CC->getName()
Duncan Sands1e96bab2010-11-04 10:49:57 +000063 << "(unsigned ValNo, MVT ValVT,\n"
Chris Lattner88ee2a12007-02-27 22:05:51 +000064 << std::string(CC->getName().size()+13, ' ')
Duncan Sands1440e8b2010-11-03 11:35:31 +000065 << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
Chris Lattner2092c8a2007-02-28 04:43:48 +000066 << std::string(CC->getName().size()+13, ' ')
Duncan Sands276dcbd2008-03-21 09:14:45 +000067 << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
Chris Lattner88ee2a12007-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 Dunbar1a551802009-07-03 00:10:29 +000079 unsigned Indent, raw_ostream &O) {
Chris Lattner88ee2a12007-02-27 22:05:51 +000080 std::string IndentStr = std::string(Indent, ' ');
81
82 if (Action->isSubClassOf("CCPredicateAction")) {
83 O << IndentStr << "if (";
84
Chris Lattnere3bab802007-02-28 05:29:06 +000085 if (Action->isSubClassOf("CCIfType")) {
David Greene05bce0b2011-07-29 22:43:06 +000086 ListInit *VTs = Action->getValueAsListInit("VTs");
Chris Lattner88ee2a12007-02-27 22:05:51 +000087 for (unsigned i = 0, e = VTs->getSize(); i != e; ++i) {
88 Record *VT = VTs->getElementAsRecord(i);
Chris Lattner2092c8a2007-02-28 04:43:48 +000089 if (i != 0) O << " ||\n " << IndentStr;
Chris Lattner88ee2a12007-02-27 22:05:51 +000090 O << "LocVT == " << getEnumName(getValueType(VT));
91 }
92
Chris Lattnere3bab802007-02-28 05:29:06 +000093 } else if (Action->isSubClassOf("CCIf")) {
Chris Lattner88ee2a12007-02-27 22:05:51 +000094 O << Action->getValueAsString("Predicate");
95 } else {
96 Action->dump();
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000097 PrintFatalError("Unknown CCPredicateAction!");
Chris Lattner88ee2a12007-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 Greene05bce0b2011-07-29 22:43:06 +0000110 ListInit *RegList = Action->getValueAsListInit("RegList");
Chris Lattner88ee2a12007-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 Topperc5eaae42012-03-11 07:57:25 +0000115 O << IndentStr << "static const uint16_t RegList" << ++Counter
Chris Lattner88ee2a12007-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 Korobeynikov67073f12008-04-02 05:23:57 +0000130 } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
David Greene05bce0b2011-07-29 22:43:06 +0000131 ListInit *RegList = Action->getValueAsListInit("RegList");
132 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
Anton Korobeynikov67073f12008-04-02 05:23:57 +0000133 if (ShadowRegList->getSize() >0 &&
134 ShadowRegList->getSize() != RegList->getSize())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000135 PrintFatalError("Invalid length of list of shadowed registers");
Anton Korobeynikov67073f12008-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 Topperc5eaae42012-03-11 07:57:25 +0000146 O << IndentStr << "static const uint16_t RegList" << RegListNumber
Anton Korobeynikov67073f12008-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 Topperc5eaae42012-03-11 07:57:25 +0000155 O << IndentStr << "static const uint16_t RegList"
Anton Korobeynikov67073f12008-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 Lattner88ee2a12007-02-27 22:05:51 +0000172 } else if (Action->isSubClassOf("CCAssignToStack")) {
173 int Size = Action->getValueAsInt("Size");
174 int Align = Action->getValueAsInt("Align");
Duncan Sands87b665d2007-11-14 08:29:13 +0000175
Evan Cheng5daafa92008-01-15 03:10:35 +0000176 O << IndentStr << "unsigned Offset" << ++Counter
177 << " = State.AllocateStack(";
Duncan Sands87b665d2007-11-14 08:29:13 +0000178 if (Size)
Evan Cheng5daafa92008-01-15 03:10:35 +0000179 O << Size << ", ";
Duncan Sands87b665d2007-11-14 08:29:13 +0000180 else
Micah Villmow791cfc22012-10-08 16:39:34 +0000181 O << "\n" << IndentStr << " State.getTarget().getDataLayout()"
Duncan Sands1440e8b2010-11-03 11:35:31 +0000182 "->getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())), ";
Duncan Sands87b665d2007-11-14 08:29:13 +0000183 if (Align)
184 O << Align;
185 else
Micah Villmow791cfc22012-10-08 16:39:34 +0000186 O << "\n" << IndentStr << " State.getTarget().getDataLayout()"
Duncan Sands1440e8b2010-11-03 11:35:31 +0000187 "->getABITypeAlignment(EVT(LocVT).getTypeForEVT(State.getContext()))";
Rafael Espindola55e95872010-08-06 15:35:32 +0000188 if (Action->isSubClassOf("CCAssignToStackWithShadow"))
189 O << ", " << getQualifiedName(Action->getValueAsDef("ShadowReg"));
Evan Cheng5daafa92008-01-15 03:10:35 +0000190 O << ");\n" << IndentStr
Duncan Sands87b665d2007-11-14 08:29:13 +0000191 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
Dale Johannesene3ef7442007-11-10 22:07:15 +0000192 << Counter << ", LocVT, LocInfo));\n";
193 O << IndentStr << "return false;\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +0000194 } else if (Action->isSubClassOf("CCPromoteToType")) {
Chris Lattner2092c8a2007-02-28 04:43:48 +0000195 Record *DestTy = Action->getValueAsDef("DestTy");
196 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
Duncan Sands276dcbd2008-03-21 09:14:45 +0000197 O << IndentStr << "if (ArgFlags.isSExt())\n"
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000198 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
Duncan Sands276dcbd2008-03-21 09:14:45 +0000199 << IndentStr << "else if (ArgFlags.isZExt())\n"
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000200 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
201 << IndentStr << "else\n"
202 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
Bob Wilson1f595bb2009-04-17 19:07:39 +0000203 } else if (Action->isSubClassOf("CCBitConvertToType")) {
204 Record *DestTy = Action->getValueAsDef("DestTy");
205 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
206 O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
Anton Korobeynikov4ab15532009-08-03 08:13:56 +0000207 } else if (Action->isSubClassOf("CCPassIndirect")) {
208 Record *DestTy = Action->getValueAsDef("DestTy");
209 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
210 O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
Evan Cheng6bfa8a12008-01-15 03:34:58 +0000211 } else if (Action->isSubClassOf("CCPassByVal")) {
212 int Size = Action->getValueAsInt("Size");
213 int Align = Action->getValueAsInt("Align");
214 O << IndentStr
215 << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
216 << Size << ", " << Align << ", ArgFlags);\n";
Rafael Espindola594d37e2007-08-10 14:44:42 +0000217 O << IndentStr << "return false;\n";
Bob Wilson1f595bb2009-04-17 19:07:39 +0000218 } else if (Action->isSubClassOf("CCCustom")) {
219 O << IndentStr
220 << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
221 << "LocVT, LocInfo, ArgFlags, State))\n";
222 O << IndentStr << IndentStr << "return false;\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +0000223 } else {
224 Action->dump();
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000225 PrintFatalError("Unknown CCAction!");
Chris Lattner88ee2a12007-02-27 22:05:51 +0000226 }
227 }
Chris Lattner50d45652007-02-27 22:08:27 +0000228}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000229
230namespace llvm {
231
232void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
233 emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
234 CallingConvEmitter(RK).run(OS);
235}
236
237} // End llvm namespace