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