blob: e9c4bd30f9143590efcdcbc26cf93fd17f3b8826 [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"
Peter Collingbourne7c788882011-10-01 16:41:13 +000016#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000017#include "llvm/TableGen/TableGenBackend.h"
18#include <cassert>
Chris Lattner88ee2a12007-02-27 22:05:51 +000019using namespace llvm;
20
Jakob Stoklund Olesen6f36fa92012-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 Dunbar1a551802009-07-03 00:10:29 +000036void CallingConvEmitter::run(raw_ostream &O) {
Chris Lattner88ee2a12007-02-27 22:05:51 +000037
38 std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv");
39
40 // Emit prototypes for all of the CC's so that they can forward ref each
41 // other.
42 for (unsigned i = 0, e = CCs.size(); i != e; ++i) {
43 O << "static bool " << CCs[i]->getName()
Duncan Sands1e96bab2010-11-04 10:49:57 +000044 << "(unsigned ValNo, MVT ValVT,\n"
Chris Lattner88ee2a12007-02-27 22:05:51 +000045 << std::string(CCs[i]->getName().size()+13, ' ')
Duncan Sands1440e8b2010-11-03 11:35:31 +000046 << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
Chris Lattner2092c8a2007-02-28 04:43:48 +000047 << std::string(CCs[i]->getName().size()+13, ' ')
Duncan Sands276dcbd2008-03-21 09:14:45 +000048 << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +000049 }
50
51 // Emit each calling convention description in full.
52 for (unsigned i = 0, e = CCs.size(); i != e; ++i)
53 EmitCallingConv(CCs[i], O);
54}
55
56
Daniel Dunbar1a551802009-07-03 00:10:29 +000057void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
David Greene05bce0b2011-07-29 22:43:06 +000058 ListInit *CCActions = CC->getValueAsListInit("Actions");
Chris Lattner88ee2a12007-02-27 22:05:51 +000059 Counter = 0;
60
61 O << "\n\nstatic bool " << CC->getName()
Duncan Sands1e96bab2010-11-04 10:49:57 +000062 << "(unsigned ValNo, MVT ValVT,\n"
Chris Lattner88ee2a12007-02-27 22:05:51 +000063 << std::string(CC->getName().size()+13, ' ')
Duncan Sands1440e8b2010-11-03 11:35:31 +000064 << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
Chris Lattner2092c8a2007-02-28 04:43:48 +000065 << std::string(CC->getName().size()+13, ' ')
Duncan Sands276dcbd2008-03-21 09:14:45 +000066 << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +000067 // Emit all of the actions, in order.
68 for (unsigned i = 0, e = CCActions->getSize(); i != e; ++i) {
69 O << "\n";
70 EmitAction(CCActions->getElementAsRecord(i), 2, O);
71 }
72
73 O << "\n return true; // CC didn't match.\n";
74 O << "}\n";
75}
76
77void CallingConvEmitter::EmitAction(Record *Action,
Daniel Dunbar1a551802009-07-03 00:10:29 +000078 unsigned Indent, raw_ostream &O) {
Chris Lattner88ee2a12007-02-27 22:05:51 +000079 std::string IndentStr = std::string(Indent, ' ');
80
81 if (Action->isSubClassOf("CCPredicateAction")) {
82 O << IndentStr << "if (";
83
Chris Lattnere3bab802007-02-28 05:29:06 +000084 if (Action->isSubClassOf("CCIfType")) {
David Greene05bce0b2011-07-29 22:43:06 +000085 ListInit *VTs = Action->getValueAsListInit("VTs");
Chris Lattner88ee2a12007-02-27 22:05:51 +000086 for (unsigned i = 0, e = VTs->getSize(); i != e; ++i) {
87 Record *VT = VTs->getElementAsRecord(i);
Chris Lattner2092c8a2007-02-28 04:43:48 +000088 if (i != 0) O << " ||\n " << IndentStr;
Chris Lattner88ee2a12007-02-27 22:05:51 +000089 O << "LocVT == " << getEnumName(getValueType(VT));
90 }
91
Chris Lattnere3bab802007-02-28 05:29:06 +000092 } else if (Action->isSubClassOf("CCIf")) {
Chris Lattner88ee2a12007-02-27 22:05:51 +000093 O << Action->getValueAsString("Predicate");
94 } else {
95 Action->dump();
96 throw "Unknown CCPredicateAction!";
97 }
98
99 O << ") {\n";
100 EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O);
101 O << IndentStr << "}\n";
102 } else {
103 if (Action->isSubClassOf("CCDelegateTo")) {
104 Record *CC = Action->getValueAsDef("CC");
105 O << IndentStr << "if (!" << CC->getName()
106 << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
107 << IndentStr << " return false;\n";
108 } else if (Action->isSubClassOf("CCAssignToReg")) {
David Greene05bce0b2011-07-29 22:43:06 +0000109 ListInit *RegList = Action->getValueAsListInit("RegList");
Chris Lattner88ee2a12007-02-27 22:05:51 +0000110 if (RegList->getSize() == 1) {
111 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
112 O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n";
113 } else {
Craig Topperc5eaae42012-03-11 07:57:25 +0000114 O << IndentStr << "static const uint16_t RegList" << ++Counter
Chris Lattner88ee2a12007-02-27 22:05:51 +0000115 << "[] = {\n";
116 O << IndentStr << " ";
117 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
118 if (i != 0) O << ", ";
119 O << getQualifiedName(RegList->getElementAsRecord(i));
120 }
121 O << "\n" << IndentStr << "};\n";
122 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
123 << Counter << ", " << RegList->getSize() << ")) {\n";
124 }
125 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
126 << "Reg, LocVT, LocInfo));\n";
127 O << IndentStr << " return false;\n";
128 O << IndentStr << "}\n";
Anton Korobeynikov67073f12008-04-02 05:23:57 +0000129 } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
David Greene05bce0b2011-07-29 22:43:06 +0000130 ListInit *RegList = Action->getValueAsListInit("RegList");
131 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
Anton Korobeynikov67073f12008-04-02 05:23:57 +0000132 if (ShadowRegList->getSize() >0 &&
133 ShadowRegList->getSize() != RegList->getSize())
134 throw "Invalid length of list of shadowed registers";
135
136 if (RegList->getSize() == 1) {
137 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
138 O << getQualifiedName(RegList->getElementAsRecord(0));
139 O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0));
140 O << ")) {\n";
141 } else {
142 unsigned RegListNumber = ++Counter;
143 unsigned ShadowRegListNumber = ++Counter;
144
Craig Topperc5eaae42012-03-11 07:57:25 +0000145 O << IndentStr << "static const uint16_t RegList" << RegListNumber
Anton Korobeynikov67073f12008-04-02 05:23:57 +0000146 << "[] = {\n";
147 O << IndentStr << " ";
148 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
149 if (i != 0) O << ", ";
150 O << getQualifiedName(RegList->getElementAsRecord(i));
151 }
152 O << "\n" << IndentStr << "};\n";
153
Craig Topperc5eaae42012-03-11 07:57:25 +0000154 O << IndentStr << "static const uint16_t RegList"
Anton Korobeynikov67073f12008-04-02 05:23:57 +0000155 << ShadowRegListNumber << "[] = {\n";
156 O << IndentStr << " ";
157 for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) {
158 if (i != 0) O << ", ";
159 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
160 }
161 O << "\n" << IndentStr << "};\n";
162
163 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
164 << RegListNumber << ", " << "RegList" << ShadowRegListNumber
165 << ", " << RegList->getSize() << ")) {\n";
166 }
167 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
168 << "Reg, LocVT, LocInfo));\n";
169 O << IndentStr << " return false;\n";
170 O << IndentStr << "}\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +0000171 } else if (Action->isSubClassOf("CCAssignToStack")) {
172 int Size = Action->getValueAsInt("Size");
173 int Align = Action->getValueAsInt("Align");
Duncan Sands87b665d2007-11-14 08:29:13 +0000174
Evan Cheng5daafa92008-01-15 03:10:35 +0000175 O << IndentStr << "unsigned Offset" << ++Counter
176 << " = State.AllocateStack(";
Duncan Sands87b665d2007-11-14 08:29:13 +0000177 if (Size)
Evan Cheng5daafa92008-01-15 03:10:35 +0000178 O << Size << ", ";
Duncan Sands87b665d2007-11-14 08:29:13 +0000179 else
Evan Cheng5daafa92008-01-15 03:10:35 +0000180 O << "\n" << IndentStr << " State.getTarget().getTargetData()"
Duncan Sands1440e8b2010-11-03 11:35:31 +0000181 "->getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())), ";
Duncan Sands87b665d2007-11-14 08:29:13 +0000182 if (Align)
183 O << Align;
184 else
Evan Cheng5daafa92008-01-15 03:10:35 +0000185 O << "\n" << IndentStr << " State.getTarget().getTargetData()"
Duncan Sands1440e8b2010-11-03 11:35:31 +0000186 "->getABITypeAlignment(EVT(LocVT).getTypeForEVT(State.getContext()))";
Rafael Espindola55e95872010-08-06 15:35:32 +0000187 if (Action->isSubClassOf("CCAssignToStackWithShadow"))
188 O << ", " << getQualifiedName(Action->getValueAsDef("ShadowReg"));
Evan Cheng5daafa92008-01-15 03:10:35 +0000189 O << ");\n" << IndentStr
Duncan Sands87b665d2007-11-14 08:29:13 +0000190 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
Dale Johannesene3ef7442007-11-10 22:07:15 +0000191 << Counter << ", LocVT, LocInfo));\n";
192 O << IndentStr << "return false;\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +0000193 } else if (Action->isSubClassOf("CCPromoteToType")) {
Chris Lattner2092c8a2007-02-28 04:43:48 +0000194 Record *DestTy = Action->getValueAsDef("DestTy");
195 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
Duncan Sands276dcbd2008-03-21 09:14:45 +0000196 O << IndentStr << "if (ArgFlags.isSExt())\n"
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000197 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
Duncan Sands276dcbd2008-03-21 09:14:45 +0000198 << IndentStr << "else if (ArgFlags.isZExt())\n"
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000199 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
200 << IndentStr << "else\n"
201 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
Bob Wilson1f595bb2009-04-17 19:07:39 +0000202 } else if (Action->isSubClassOf("CCBitConvertToType")) {
203 Record *DestTy = Action->getValueAsDef("DestTy");
204 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
205 O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
Anton Korobeynikov4ab15532009-08-03 08:13:56 +0000206 } else if (Action->isSubClassOf("CCPassIndirect")) {
207 Record *DestTy = Action->getValueAsDef("DestTy");
208 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
209 O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
Evan Cheng6bfa8a12008-01-15 03:34:58 +0000210 } else if (Action->isSubClassOf("CCPassByVal")) {
211 int Size = Action->getValueAsInt("Size");
212 int Align = Action->getValueAsInt("Align");
213 O << IndentStr
214 << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
215 << Size << ", " << Align << ", ArgFlags);\n";
Rafael Espindola594d37e2007-08-10 14:44:42 +0000216 O << IndentStr << "return false;\n";
Bob Wilson1f595bb2009-04-17 19:07:39 +0000217 } else if (Action->isSubClassOf("CCCustom")) {
218 O << IndentStr
219 << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
220 << "LocVT, LocInfo, ArgFlags, State))\n";
221 O << IndentStr << IndentStr << "return false;\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +0000222 } else {
223 Action->dump();
224 throw "Unknown CCAction!";
225 }
226 }
Chris Lattner50d45652007-02-27 22:08:27 +0000227}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000228
229namespace llvm {
230
231void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
232 emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
233 CallingConvEmitter(RK).run(OS);
234}
235
236} // End llvm namespace