1 //===- CallingConvEmitter.cpp - Generate calling conventions --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This tablegen backend is responsible for emitting descriptions of the calling
10 // conventions supported by this target.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenTarget.h"
15 #include "llvm/TableGen/Error.h"
16 #include "llvm/TableGen/Record.h"
17 #include "llvm/TableGen/TableGenBackend.h"
23 class CallingConvEmitter
{
24 RecordKeeper
&Records
;
25 unsigned Counter
= 0u;
26 std::string CurrentAction
;
27 bool SwiftAction
= false;
29 std::map
<std::string
, std::set
<std::string
>> AssignedRegsMap
;
30 std::map
<std::string
, std::set
<std::string
>> AssignedSwiftRegsMap
;
31 std::map
<std::string
, std::set
<std::string
>> DelegateToMap
;
34 explicit CallingConvEmitter(RecordKeeper
&R
) : Records(R
) {}
36 void run(raw_ostream
&o
);
39 void EmitCallingConv(Record
*CC
, raw_ostream
&O
);
40 void EmitAction(Record
*Action
, unsigned Indent
, raw_ostream
&O
);
41 void EmitArgRegisterLists(raw_ostream
&O
);
43 } // End anonymous namespace
45 void CallingConvEmitter::run(raw_ostream
&O
) {
46 emitSourceFileHeader("Calling Convention Implementation Fragment", O
);
48 std::vector
<Record
*> CCs
= Records
.getAllDerivedDefinitions("CallingConv");
50 // Emit prototypes for all of the non-custom CC's so that they can forward ref
52 Records
.startTimer("Emit prototypes");
53 O
<< "#ifndef GET_CC_REGISTER_LISTS\n\n";
54 for (Record
*CC
: CCs
) {
55 if (!CC
->getValueAsBit("Custom")) {
56 unsigned Pad
= CC
->getName().size();
57 if (CC
->getValueAsBit("Entry")) {
64 O
<< CC
->getName() << "(unsigned ValNo, MVT ValVT,\n"
65 << std::string(Pad
, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
66 << std::string(Pad
, ' ')
67 << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n";
71 // Emit each non-custom calling convention description in full.
72 Records
.startTimer("Emit full descriptions");
73 for (Record
*CC
: CCs
) {
74 if (!CC
->getValueAsBit("Custom")) {
75 EmitCallingConv(CC
, O
);
79 EmitArgRegisterLists(O
);
81 O
<< "\n#endif // CC_REGISTER_LIST\n";
84 void CallingConvEmitter::EmitCallingConv(Record
*CC
, raw_ostream
&O
) {
85 ListInit
*CCActions
= CC
->getValueAsListInit("Actions");
88 CurrentAction
= CC
->getName().str();
89 // Call upon the creation of a map entry from the void!
90 // We want an entry in AssignedRegsMap for every action, even if that
92 AssignedRegsMap
[CurrentAction
] = {};
95 unsigned Pad
= CurrentAction
.size();
96 if (CC
->getValueAsBit("Entry")) {
103 O
<< CurrentAction
<< "(unsigned ValNo, MVT ValVT,\n"
104 << std::string(Pad
, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
105 << std::string(Pad
, ' ') << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
106 // Emit all of the actions, in order.
107 for (unsigned i
= 0, e
= CCActions
->size(); i
!= e
; ++i
) {
108 Record
*Action
= CCActions
->getElementAsRecord(i
);
109 SwiftAction
= llvm::any_of(Action
->getSuperClasses(),
110 [](const std::pair
<Record
*, SMRange
> &Class
) {
112 Class
.first
->getNameInitAsString();
113 return StringRef(Name
).startswith("CCIfSwift");
117 EmitAction(Action
, 2, O
);
120 O
<< "\n return true; // CC didn't match.\n";
124 void CallingConvEmitter::EmitAction(Record
*Action
,
125 unsigned Indent
, raw_ostream
&O
) {
126 std::string IndentStr
= std::string(Indent
, ' ');
128 if (Action
->isSubClassOf("CCPredicateAction")) {
129 O
<< IndentStr
<< "if (";
131 if (Action
->isSubClassOf("CCIfType")) {
132 ListInit
*VTs
= Action
->getValueAsListInit("VTs");
133 for (unsigned i
= 0, e
= VTs
->size(); i
!= e
; ++i
) {
134 Record
*VT
= VTs
->getElementAsRecord(i
);
135 if (i
!= 0) O
<< " ||\n " << IndentStr
;
136 O
<< "LocVT == " << getEnumName(getValueType(VT
));
139 } else if (Action
->isSubClassOf("CCIf")) {
140 O
<< Action
->getValueAsString("Predicate");
143 PrintFatalError(Action
->getLoc(), "Unknown CCPredicateAction!");
147 EmitAction(Action
->getValueAsDef("SubAction"), Indent
+2, O
);
148 O
<< IndentStr
<< "}\n";
150 if (Action
->isSubClassOf("CCDelegateTo")) {
151 Record
*CC
= Action
->getValueAsDef("CC");
152 O
<< IndentStr
<< "if (!" << CC
->getName()
153 << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
154 << IndentStr
<< " return false;\n";
155 DelegateToMap
[CurrentAction
].insert(CC
->getName().str());
156 } else if (Action
->isSubClassOf("CCAssignToReg") ||
157 Action
->isSubClassOf("CCAssignToRegAndStack")) {
158 ListInit
*RegList
= Action
->getValueAsListInit("RegList");
159 if (RegList
->size() == 1) {
160 std::string Name
= getQualifiedName(RegList
->getElementAsRecord(0));
161 O
<< IndentStr
<< "if (unsigned Reg = State.AllocateReg(" << Name
164 AssignedSwiftRegsMap
[CurrentAction
].insert(Name
);
166 AssignedRegsMap
[CurrentAction
].insert(Name
);
168 O
<< IndentStr
<< "static const MCPhysReg RegList" << ++Counter
170 O
<< IndentStr
<< " ";
172 for (unsigned i
= 0, e
= RegList
->size(); i
!= e
; ++i
) {
173 std::string Name
= getQualifiedName(RegList
->getElementAsRecord(i
));
175 AssignedSwiftRegsMap
[CurrentAction
].insert(Name
);
177 AssignedRegsMap
[CurrentAction
].insert(Name
);
180 O
<< "\n" << IndentStr
<< "};\n";
181 O
<< IndentStr
<< "if (unsigned Reg = State.AllocateReg(RegList"
182 << Counter
<< ")) {\n";
184 O
<< IndentStr
<< " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
185 << "Reg, LocVT, LocInfo));\n";
186 if (Action
->isSubClassOf("CCAssignToRegAndStack")) {
187 int Size
= Action
->getValueAsInt("Size");
188 int Align
= Action
->getValueAsInt("Align");
189 O
<< IndentStr
<< " (void)State.AllocateStack(";
195 << " State.getMachineFunction().getDataLayout()."
196 "getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())),"
199 O
<< "Align(" << Align
<< ")";
203 << " State.getMachineFunction().getDataLayout()."
204 "getABITypeAlign(EVT(LocVT).getTypeForEVT(State.getContext()"
208 O
<< IndentStr
<< " return false;\n";
209 O
<< IndentStr
<< "}\n";
210 } else if (Action
->isSubClassOf("CCAssignToRegWithShadow")) {
211 ListInit
*RegList
= Action
->getValueAsListInit("RegList");
212 ListInit
*ShadowRegList
= Action
->getValueAsListInit("ShadowRegList");
213 if (!ShadowRegList
->empty() && ShadowRegList
->size() != RegList
->size())
214 PrintFatalError(Action
->getLoc(),
215 "Invalid length of list of shadowed registers");
217 if (RegList
->size() == 1) {
218 O
<< IndentStr
<< "if (unsigned Reg = State.AllocateReg(";
219 O
<< getQualifiedName(RegList
->getElementAsRecord(0));
220 O
<< ", " << getQualifiedName(ShadowRegList
->getElementAsRecord(0));
223 unsigned RegListNumber
= ++Counter
;
224 unsigned ShadowRegListNumber
= ++Counter
;
226 O
<< IndentStr
<< "static const MCPhysReg RegList" << RegListNumber
228 O
<< IndentStr
<< " ";
230 for (unsigned i
= 0, e
= RegList
->size(); i
!= e
; ++i
)
231 O
<< LS
<< getQualifiedName(RegList
->getElementAsRecord(i
));
232 O
<< "\n" << IndentStr
<< "};\n";
234 O
<< IndentStr
<< "static const MCPhysReg RegList"
235 << ShadowRegListNumber
<< "[] = {\n";
236 O
<< IndentStr
<< " ";
238 for (unsigned i
= 0, e
= ShadowRegList
->size(); i
!= e
; ++i
)
239 O
<< LSS
<< getQualifiedName(ShadowRegList
->getElementAsRecord(i
));
240 O
<< "\n" << IndentStr
<< "};\n";
242 O
<< IndentStr
<< "if (unsigned Reg = State.AllocateReg(RegList"
243 << RegListNumber
<< ", " << "RegList" << ShadowRegListNumber
246 O
<< IndentStr
<< " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
247 << "Reg, LocVT, LocInfo));\n";
248 O
<< IndentStr
<< " return false;\n";
249 O
<< IndentStr
<< "}\n";
250 } else if (Action
->isSubClassOf("CCAssignToStack")) {
251 int Size
= Action
->getValueAsInt("Size");
252 int Align
= Action
->getValueAsInt("Align");
254 O
<< IndentStr
<< "int64_t Offset" << ++Counter
255 << " = State.AllocateStack(";
259 O
<< "\n" << IndentStr
260 << " State.getMachineFunction().getDataLayout()."
261 "getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())),"
264 O
<< "Align(" << Align
<< ")";
268 << " State.getMachineFunction().getDataLayout()."
269 "getABITypeAlign(EVT(LocVT).getTypeForEVT(State.getContext()"
271 O
<< ");\n" << IndentStr
272 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
273 << Counter
<< ", LocVT, LocInfo));\n";
274 O
<< IndentStr
<< "return false;\n";
275 } else if (Action
->isSubClassOf("CCAssignToStackWithShadow")) {
276 int Size
= Action
->getValueAsInt("Size");
277 int Align
= Action
->getValueAsInt("Align");
278 ListInit
*ShadowRegList
= Action
->getValueAsListInit("ShadowRegList");
280 unsigned ShadowRegListNumber
= ++Counter
;
282 O
<< IndentStr
<< "static const MCPhysReg ShadowRegList"
283 << ShadowRegListNumber
<< "[] = {\n";
284 O
<< IndentStr
<< " ";
286 for (unsigned i
= 0, e
= ShadowRegList
->size(); i
!= e
; ++i
)
287 O
<< LS
<< getQualifiedName(ShadowRegList
->getElementAsRecord(i
));
288 O
<< "\n" << IndentStr
<< "};\n";
290 O
<< IndentStr
<< "int64_t Offset" << ++Counter
291 << " = State.AllocateStack(" << Size
<< ", Align(" << Align
<< "), "
292 << "ShadowRegList" << ShadowRegListNumber
<< ");\n";
293 O
<< IndentStr
<< "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
294 << Counter
<< ", LocVT, LocInfo));\n";
295 O
<< IndentStr
<< "return false;\n";
296 } else if (Action
->isSubClassOf("CCPromoteToType")) {
297 Record
*DestTy
= Action
->getValueAsDef("DestTy");
298 MVT::SimpleValueType DestVT
= getValueType(DestTy
);
299 O
<< IndentStr
<< "LocVT = " << getEnumName(DestVT
) <<";\n";
300 if (MVT(DestVT
).isFloatingPoint()) {
301 O
<< IndentStr
<< "LocInfo = CCValAssign::FPExt;\n";
303 O
<< IndentStr
<< "if (ArgFlags.isSExt())\n"
304 << IndentStr
<< " LocInfo = CCValAssign::SExt;\n"
305 << IndentStr
<< "else if (ArgFlags.isZExt())\n"
306 << IndentStr
<< " LocInfo = CCValAssign::ZExt;\n"
307 << IndentStr
<< "else\n"
308 << IndentStr
<< " LocInfo = CCValAssign::AExt;\n";
310 } else if (Action
->isSubClassOf("CCPromoteToUpperBitsInType")) {
311 Record
*DestTy
= Action
->getValueAsDef("DestTy");
312 MVT::SimpleValueType DestVT
= getValueType(DestTy
);
313 O
<< IndentStr
<< "LocVT = " << getEnumName(DestVT
) << ";\n";
314 if (MVT(DestVT
).isFloatingPoint()) {
315 PrintFatalError(Action
->getLoc(),
316 "CCPromoteToUpperBitsInType does not handle floating "
319 O
<< IndentStr
<< "if (ArgFlags.isSExt())\n"
320 << IndentStr
<< " LocInfo = CCValAssign::SExtUpper;\n"
321 << IndentStr
<< "else if (ArgFlags.isZExt())\n"
322 << IndentStr
<< " LocInfo = CCValAssign::ZExtUpper;\n"
323 << IndentStr
<< "else\n"
324 << IndentStr
<< " LocInfo = CCValAssign::AExtUpper;\n";
326 } else if (Action
->isSubClassOf("CCBitConvertToType")) {
327 Record
*DestTy
= Action
->getValueAsDef("DestTy");
328 O
<< IndentStr
<< "LocVT = " << getEnumName(getValueType(DestTy
)) <<";\n";
329 O
<< IndentStr
<< "LocInfo = CCValAssign::BCvt;\n";
330 } else if (Action
->isSubClassOf("CCTruncToType")) {
331 Record
*DestTy
= Action
->getValueAsDef("DestTy");
332 O
<< IndentStr
<< "LocVT = " << getEnumName(getValueType(DestTy
)) <<";\n";
333 O
<< IndentStr
<< "LocInfo = CCValAssign::Trunc;\n";
334 } else if (Action
->isSubClassOf("CCPassIndirect")) {
335 Record
*DestTy
= Action
->getValueAsDef("DestTy");
336 O
<< IndentStr
<< "LocVT = " << getEnumName(getValueType(DestTy
)) <<";\n";
337 O
<< IndentStr
<< "LocInfo = CCValAssign::Indirect;\n";
338 } else if (Action
->isSubClassOf("CCPassByVal")) {
339 int Size
= Action
->getValueAsInt("Size");
340 int Align
= Action
->getValueAsInt("Align");
341 O
<< IndentStr
<< "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
342 << Size
<< ", Align(" << Align
<< "), ArgFlags);\n";
343 O
<< IndentStr
<< "return false;\n";
344 } else if (Action
->isSubClassOf("CCCustom")) {
346 << "if (" << Action
->getValueAsString("FuncName") << "(ValNo, ValVT, "
347 << "LocVT, LocInfo, ArgFlags, State))\n";
348 O
<< IndentStr
<< " return false;\n";
351 PrintFatalError(Action
->getLoc(), "Unknown CCAction!");
356 void CallingConvEmitter::EmitArgRegisterLists(raw_ostream
&O
) {
357 // Transitively merge all delegated CCs into AssignedRegsMap.
358 using EntryTy
= std::pair
<std::string
, std::set
<std::string
>>;
362 std::deque
<EntryTy
> Worklist(DelegateToMap
.begin(), DelegateToMap
.end());
364 while (!Worklist
.empty()) {
365 EntryTy Entry
= Worklist
.front();
366 Worklist
.pop_front();
368 const std::string
&CCName
= Entry
.first
;
369 std::set
<std::string
> &Registers
= Entry
.second
;
370 if (!Registers
.empty())
373 for (auto &InnerEntry
: Worklist
) {
374 const std::string
&InnerCCName
= InnerEntry
.first
;
375 std::set
<std::string
> &InnerRegisters
= InnerEntry
.second
;
377 if (InnerRegisters
.find(CCName
) != InnerRegisters
.end()) {
378 AssignedRegsMap
[InnerCCName
].insert(
379 AssignedRegsMap
[CCName
].begin(),
380 AssignedRegsMap
[CCName
].end());
381 InnerRegisters
.erase(CCName
);
385 DelegateToMap
.erase(CCName
);
390 if (AssignedRegsMap
.empty())
395 for (auto &Entry
: AssignedRegsMap
) {
396 const std::string
&RegName
= Entry
.first
;
397 std::set
<std::string
> &Registers
= Entry
.second
;
402 O
<< "const MCRegister " << Entry
.first
<< "_ArgRegs[] = { ";
404 if (Registers
.empty()) {
408 for (const std::string
&Reg
: Registers
)
415 if (AssignedSwiftRegsMap
.empty())
418 O
<< "\n// Registers used by Swift.\n";
419 for (auto &Entry
: AssignedSwiftRegsMap
) {
420 const std::string
&RegName
= Entry
.first
;
421 std::set
<std::string
> &Registers
= Entry
.second
;
423 O
<< "const MCRegister " << RegName
<< "_Swift_ArgRegs[] = { ";
426 for (const std::string
&Reg
: Registers
)
433 static TableGen::Emitter::OptClass
<CallingConvEmitter
>
434 X("gen-callingconv", "Generate calling convention descriptions");