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