1 //===- CallingConvEmitter.cpp - Generate calling conventions --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend is responsible for emitting descriptions of the calling
11 // conventions supported by this target.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "CodeGenTarget.h"
16 #include "llvm/TableGen/Error.h"
17 #include "llvm/TableGen/Record.h"
18 #include "llvm/TableGen/TableGenBackend.h"
19 #include <cassert>
20 using namespace llvm;
21
22 namespace {
23 class CallingConvEmitter {
24 RecordKeeper &Records;
25 public:
CallingConvEmitter(RecordKeeper & R)26 explicit CallingConvEmitter(RecordKeeper &R) : Records(R) {}
27
28 void run(raw_ostream &o);
29
30 private:
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
run(raw_ostream & O)37 void CallingConvEmitter::run(raw_ostream &O) {
38 std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv");
39
40 // Emit prototypes for all of the non-custom CC's so that they can forward ref
41 // each other.
42 for (unsigned i = 0, e = CCs.size(); i != e; ++i) {
43 if (!CCs[i]->getValueAsBit("Custom")) {
44 O << "static bool " << CCs[i]->getName()
45 << "(unsigned ValNo, MVT ValVT,\n"
46 << std::string(CCs[i]->getName().size() + 13, ' ')
47 << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
48 << std::string(CCs[i]->getName().size() + 13, ' ')
49 << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n";
50 }
51 }
52
53 // Emit each non-custom calling convention description in full.
54 for (unsigned i = 0, e = CCs.size(); i != e; ++i) {
55 if (!CCs[i]->getValueAsBit("Custom"))
56 EmitCallingConv(CCs[i], O);
57 }
58 }
59
60
EmitCallingConv(Record * CC,raw_ostream & O)61 void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
62 ListInit *CCActions = CC->getValueAsListInit("Actions");
63 Counter = 0;
64
65 O << "\n\nstatic bool " << CC->getName()
66 << "(unsigned ValNo, MVT ValVT,\n"
67 << std::string(CC->getName().size()+13, ' ')
68 << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
69 << std::string(CC->getName().size()+13, ' ')
70 << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
71 // Emit all of the actions, in order.
72 for (unsigned i = 0, e = CCActions->size(); i != e; ++i) {
73 O << "\n";
74 EmitAction(CCActions->getElementAsRecord(i), 2, O);
75 }
76
77 O << "\n return true; // CC didn't match.\n";
78 O << "}\n";
79 }
80
EmitAction(Record * Action,unsigned Indent,raw_ostream & O)81 void CallingConvEmitter::EmitAction(Record *Action,
82 unsigned Indent, raw_ostream &O) {
83 std::string IndentStr = std::string(Indent, ' ');
84
85 if (Action->isSubClassOf("CCPredicateAction")) {
86 O << IndentStr << "if (";
87
88 if (Action->isSubClassOf("CCIfType")) {
89 ListInit *VTs = Action->getValueAsListInit("VTs");
90 for (unsigned i = 0, e = VTs->size(); i != e; ++i) {
91 Record *VT = VTs->getElementAsRecord(i);
92 if (i != 0) O << " ||\n " << IndentStr;
93 O << "LocVT == " << getEnumName(getValueType(VT));
94 }
95
96 } else if (Action->isSubClassOf("CCIf")) {
97 O << Action->getValueAsString("Predicate");
98 } else {
99 Action->dump();
100 PrintFatalError("Unknown CCPredicateAction!");
101 }
102
103 O << ") {\n";
104 EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O);
105 O << IndentStr << "}\n";
106 } else {
107 if (Action->isSubClassOf("CCDelegateTo")) {
108 Record *CC = Action->getValueAsDef("CC");
109 O << IndentStr << "if (!" << CC->getName()
110 << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
111 << IndentStr << " return false;\n";
112 } else if (Action->isSubClassOf("CCAssignToReg")) {
113 ListInit *RegList = Action->getValueAsListInit("RegList");
114 if (RegList->size() == 1) {
115 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
116 O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n";
117 } else {
118 O << IndentStr << "static const MCPhysReg RegList" << ++Counter
119 << "[] = {\n";
120 O << IndentStr << " ";
121 for (unsigned i = 0, e = RegList->size(); i != e; ++i) {
122 if (i != 0) O << ", ";
123 O << getQualifiedName(RegList->getElementAsRecord(i));
124 }
125 O << "\n" << IndentStr << "};\n";
126 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
127 << Counter << ")) {\n";
128 }
129 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
130 << "Reg, LocVT, LocInfo));\n";
131 O << IndentStr << " return false;\n";
132 O << IndentStr << "}\n";
133 } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
134 ListInit *RegList = Action->getValueAsListInit("RegList");
135 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
136 if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size())
137 PrintFatalError("Invalid length of list of shadowed registers");
138
139 if (RegList->size() == 1) {
140 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
141 O << getQualifiedName(RegList->getElementAsRecord(0));
142 O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0));
143 O << ")) {\n";
144 } else {
145 unsigned RegListNumber = ++Counter;
146 unsigned ShadowRegListNumber = ++Counter;
147
148 O << IndentStr << "static const MCPhysReg RegList" << RegListNumber
149 << "[] = {\n";
150 O << IndentStr << " ";
151 for (unsigned i = 0, e = RegList->size(); i != e; ++i) {
152 if (i != 0) O << ", ";
153 O << getQualifiedName(RegList->getElementAsRecord(i));
154 }
155 O << "\n" << IndentStr << "};\n";
156
157 O << IndentStr << "static const MCPhysReg RegList"
158 << ShadowRegListNumber << "[] = {\n";
159 O << IndentStr << " ";
160 for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i) {
161 if (i != 0) O << ", ";
162 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
163 }
164 O << "\n" << IndentStr << "};\n";
165
166 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
167 << RegListNumber << ", " << "RegList" << ShadowRegListNumber
168 << ")) {\n";
169 }
170 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
171 << "Reg, LocVT, LocInfo));\n";
172 O << IndentStr << " return false;\n";
173 O << IndentStr << "}\n";
174 } else if (Action->isSubClassOf("CCAssignToStack")) {
175 int Size = Action->getValueAsInt("Size");
176 int Align = Action->getValueAsInt("Align");
177
178 O << IndentStr << "unsigned Offset" << ++Counter
179 << " = State.AllocateStack(";
180 if (Size)
181 O << Size << ", ";
182 else
183 O << "\n" << IndentStr
184 << " State.getMachineFunction().getDataLayout()."
185 "getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())),"
186 " ";
187 if (Align)
188 O << Align;
189 else
190 O << "\n" << IndentStr
191 << " State.getMachineFunction().getDataLayout()."
192 "getABITypeAlignment(EVT(LocVT).getTypeForEVT(State.getContext()"
193 "))";
194 O << ");\n" << IndentStr
195 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
196 << Counter << ", LocVT, LocInfo));\n";
197 O << IndentStr << "return false;\n";
198 } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
199 int Size = Action->getValueAsInt("Size");
200 int Align = Action->getValueAsInt("Align");
201 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
202
203 unsigned ShadowRegListNumber = ++Counter;
204
205 O << IndentStr << "static const MCPhysReg ShadowRegList"
206 << ShadowRegListNumber << "[] = {\n";
207 O << IndentStr << " ";
208 for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i) {
209 if (i != 0) O << ", ";
210 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
211 }
212 O << "\n" << IndentStr << "};\n";
213
214 O << IndentStr << "unsigned Offset" << ++Counter
215 << " = State.AllocateStack("
216 << Size << ", " << Align << ", "
217 << "ShadowRegList" << ShadowRegListNumber << ");\n";
218 O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
219 << Counter << ", LocVT, LocInfo));\n";
220 O << IndentStr << "return false;\n";
221 } else if (Action->isSubClassOf("CCPromoteToType")) {
222 Record *DestTy = Action->getValueAsDef("DestTy");
223 MVT::SimpleValueType DestVT = getValueType(DestTy);
224 O << IndentStr << "LocVT = " << getEnumName(DestVT) <<";\n";
225 if (MVT(DestVT).isFloatingPoint()) {
226 O << IndentStr << "LocInfo = CCValAssign::FPExt;\n";
227 } else {
228 O << IndentStr << "if (ArgFlags.isSExt())\n"
229 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
230 << IndentStr << "else if (ArgFlags.isZExt())\n"
231 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
232 << IndentStr << "else\n"
233 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
234 }
235 } else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) {
236 Record *DestTy = Action->getValueAsDef("DestTy");
237 MVT::SimpleValueType DestVT = getValueType(DestTy);
238 O << IndentStr << "LocVT = " << getEnumName(DestVT) << ";\n";
239 if (MVT(DestVT).isFloatingPoint()) {
240 PrintFatalError("CCPromoteToUpperBitsInType does not handle floating "
241 "point");
242 } else {
243 O << IndentStr << "if (ArgFlags.isSExt())\n"
244 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExtUpper;\n"
245 << IndentStr << "else if (ArgFlags.isZExt())\n"
246 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExtUpper;\n"
247 << IndentStr << "else\n"
248 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExtUpper;\n";
249 }
250 } else if (Action->isSubClassOf("CCBitConvertToType")) {
251 Record *DestTy = Action->getValueAsDef("DestTy");
252 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
253 O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
254 } else if (Action->isSubClassOf("CCPassIndirect")) {
255 Record *DestTy = Action->getValueAsDef("DestTy");
256 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
257 O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
258 } else if (Action->isSubClassOf("CCPassByVal")) {
259 int Size = Action->getValueAsInt("Size");
260 int Align = Action->getValueAsInt("Align");
261 O << IndentStr
262 << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
263 << Size << ", " << Align << ", ArgFlags);\n";
264 O << IndentStr << "return false;\n";
265 } else if (Action->isSubClassOf("CCCustom")) {
266 O << IndentStr
267 << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
268 << "LocVT, LocInfo, ArgFlags, State))\n";
269 O << IndentStr << IndentStr << "return false;\n";
270 } else {
271 Action->dump();
272 PrintFatalError("Unknown CCAction!");
273 }
274 }
275 }
276
277 namespace llvm {
278
EmitCallingConv(RecordKeeper & RK,raw_ostream & OS)279 void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
280 emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
281 CallingConvEmitter(RK).run(OS);
282 }
283
284 } // End llvm namespace
285