xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/CallingConvEmitter.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
10b57cec5SDimitry Andric //===- CallingConvEmitter.cpp - Generate calling conventions --------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This tablegen backend is responsible for emitting descriptions of the calling
100b57cec5SDimitry Andric // conventions supported by this target.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "CodeGenTarget.h"
150b57cec5SDimitry Andric #include "llvm/TableGen/Error.h"
160b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
170b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
180b57cec5SDimitry Andric #include <cassert>
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric namespace {
220b57cec5SDimitry Andric class CallingConvEmitter {
230b57cec5SDimitry Andric   RecordKeeper &Records;
240b57cec5SDimitry Andric public:
250b57cec5SDimitry Andric   explicit CallingConvEmitter(RecordKeeper &R) : Records(R) {}
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric   void run(raw_ostream &o);
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric private:
300b57cec5SDimitry Andric   void EmitCallingConv(Record *CC, raw_ostream &O);
310b57cec5SDimitry Andric   void EmitAction(Record *Action, unsigned Indent, raw_ostream &O);
320b57cec5SDimitry Andric   unsigned Counter;
330b57cec5SDimitry Andric };
340b57cec5SDimitry Andric } // End anonymous namespace
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric void CallingConvEmitter::run(raw_ostream &O) {
370b57cec5SDimitry Andric   std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv");
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   // Emit prototypes for all of the non-custom CC's so that they can forward ref
400b57cec5SDimitry Andric   // each other.
41e8d8bef9SDimitry Andric   Records.startTimer("Emit prototypes");
420b57cec5SDimitry Andric   for (Record *CC : CCs) {
430b57cec5SDimitry Andric     if (!CC->getValueAsBit("Custom")) {
440b57cec5SDimitry Andric       unsigned Pad = CC->getName().size();
450b57cec5SDimitry Andric       if (CC->getValueAsBit("Entry")) {
460b57cec5SDimitry Andric         O << "bool llvm::";
470b57cec5SDimitry Andric         Pad += 12;
480b57cec5SDimitry Andric       } else {
490b57cec5SDimitry Andric         O << "static bool ";
500b57cec5SDimitry Andric         Pad += 13;
510b57cec5SDimitry Andric       }
520b57cec5SDimitry Andric       O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n"
530b57cec5SDimitry Andric         << std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
540b57cec5SDimitry Andric         << std::string(Pad, ' ')
550b57cec5SDimitry Andric         << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n";
560b57cec5SDimitry Andric     }
570b57cec5SDimitry Andric   }
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   // Emit each non-custom calling convention description in full.
60e8d8bef9SDimitry Andric   Records.startTimer("Emit full descriptions");
610b57cec5SDimitry Andric   for (Record *CC : CCs) {
620b57cec5SDimitry Andric     if (!CC->getValueAsBit("Custom"))
630b57cec5SDimitry Andric       EmitCallingConv(CC, O);
640b57cec5SDimitry Andric   }
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
690b57cec5SDimitry Andric   ListInit *CCActions = CC->getValueAsListInit("Actions");
700b57cec5SDimitry Andric   Counter = 0;
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   O << "\n\n";
730b57cec5SDimitry Andric   unsigned Pad = CC->getName().size();
740b57cec5SDimitry Andric   if (CC->getValueAsBit("Entry")) {
750b57cec5SDimitry Andric     O << "bool llvm::";
760b57cec5SDimitry Andric     Pad += 12;
770b57cec5SDimitry Andric   } else {
780b57cec5SDimitry Andric     O << "static bool ";
790b57cec5SDimitry Andric     Pad += 13;
800b57cec5SDimitry Andric   }
810b57cec5SDimitry Andric   O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n"
820b57cec5SDimitry Andric     << std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
830b57cec5SDimitry Andric     << std::string(Pad, ' ') << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
840b57cec5SDimitry Andric   // Emit all of the actions, in order.
850b57cec5SDimitry Andric   for (unsigned i = 0, e = CCActions->size(); i != e; ++i) {
860b57cec5SDimitry Andric     O << "\n";
870b57cec5SDimitry Andric     EmitAction(CCActions->getElementAsRecord(i), 2, O);
880b57cec5SDimitry Andric   }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   O << "\n  return true; // CC didn't match.\n";
910b57cec5SDimitry Andric   O << "}\n";
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric void CallingConvEmitter::EmitAction(Record *Action,
950b57cec5SDimitry Andric                                     unsigned Indent, raw_ostream &O) {
960b57cec5SDimitry Andric   std::string IndentStr = std::string(Indent, ' ');
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   if (Action->isSubClassOf("CCPredicateAction")) {
990b57cec5SDimitry Andric     O << IndentStr << "if (";
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric     if (Action->isSubClassOf("CCIfType")) {
1020b57cec5SDimitry Andric       ListInit *VTs = Action->getValueAsListInit("VTs");
1030b57cec5SDimitry Andric       for (unsigned i = 0, e = VTs->size(); i != e; ++i) {
1040b57cec5SDimitry Andric         Record *VT = VTs->getElementAsRecord(i);
1050b57cec5SDimitry Andric         if (i != 0) O << " ||\n    " << IndentStr;
1060b57cec5SDimitry Andric         O << "LocVT == " << getEnumName(getValueType(VT));
1070b57cec5SDimitry Andric       }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCIf")) {
1100b57cec5SDimitry Andric       O << Action->getValueAsString("Predicate");
1110b57cec5SDimitry Andric     } else {
1120b57cec5SDimitry Andric       errs() << *Action;
1130b57cec5SDimitry Andric       PrintFatalError(Action->getLoc(), "Unknown CCPredicateAction!");
1140b57cec5SDimitry Andric     }
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric     O << ") {\n";
1170b57cec5SDimitry Andric     EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O);
1180b57cec5SDimitry Andric     O << IndentStr << "}\n";
1190b57cec5SDimitry Andric   } else {
1200b57cec5SDimitry Andric     if (Action->isSubClassOf("CCDelegateTo")) {
1210b57cec5SDimitry Andric       Record *CC = Action->getValueAsDef("CC");
1220b57cec5SDimitry Andric       O << IndentStr << "if (!" << CC->getName()
1230b57cec5SDimitry Andric         << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
1240b57cec5SDimitry Andric         << IndentStr << "  return false;\n";
1250b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCAssignToReg")) {
1260b57cec5SDimitry Andric       ListInit *RegList = Action->getValueAsListInit("RegList");
1270b57cec5SDimitry Andric       if (RegList->size() == 1) {
1280b57cec5SDimitry Andric         O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
1290b57cec5SDimitry Andric         O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n";
1300b57cec5SDimitry Andric       } else {
1310b57cec5SDimitry Andric         O << IndentStr << "static const MCPhysReg RegList" << ++Counter
1320b57cec5SDimitry Andric           << "[] = {\n";
1330b57cec5SDimitry Andric         O << IndentStr << "  ";
134*fe6060f1SDimitry Andric         ListSeparator LS;
135*fe6060f1SDimitry Andric         for (unsigned i = 0, e = RegList->size(); i != e; ++i)
136*fe6060f1SDimitry Andric           O << LS << getQualifiedName(RegList->getElementAsRecord(i));
1370b57cec5SDimitry Andric         O << "\n" << IndentStr << "};\n";
1380b57cec5SDimitry Andric         O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
1390b57cec5SDimitry Andric           << Counter << ")) {\n";
1400b57cec5SDimitry Andric       }
1410b57cec5SDimitry Andric       O << IndentStr << "  State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
1420b57cec5SDimitry Andric         << "Reg, LocVT, LocInfo));\n";
1430b57cec5SDimitry Andric       O << IndentStr << "  return false;\n";
1440b57cec5SDimitry Andric       O << IndentStr << "}\n";
1450b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
1460b57cec5SDimitry Andric       ListInit *RegList = Action->getValueAsListInit("RegList");
1470b57cec5SDimitry Andric       ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
1480b57cec5SDimitry Andric       if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size())
1490b57cec5SDimitry Andric         PrintFatalError(Action->getLoc(),
1500b57cec5SDimitry Andric                         "Invalid length of list of shadowed registers");
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric       if (RegList->size() == 1) {
1530b57cec5SDimitry Andric         O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
1540b57cec5SDimitry Andric         O << getQualifiedName(RegList->getElementAsRecord(0));
1550b57cec5SDimitry Andric         O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0));
1560b57cec5SDimitry Andric         O << ")) {\n";
1570b57cec5SDimitry Andric       } else {
1580b57cec5SDimitry Andric         unsigned RegListNumber = ++Counter;
1590b57cec5SDimitry Andric         unsigned ShadowRegListNumber = ++Counter;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric         O << IndentStr << "static const MCPhysReg RegList" << RegListNumber
1620b57cec5SDimitry Andric           << "[] = {\n";
1630b57cec5SDimitry Andric         O << IndentStr << "  ";
164*fe6060f1SDimitry Andric         ListSeparator LS;
165*fe6060f1SDimitry Andric         for (unsigned i = 0, e = RegList->size(); i != e; ++i)
166*fe6060f1SDimitry Andric           O << LS << getQualifiedName(RegList->getElementAsRecord(i));
1670b57cec5SDimitry Andric         O << "\n" << IndentStr << "};\n";
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric         O << IndentStr << "static const MCPhysReg RegList"
1700b57cec5SDimitry Andric           << ShadowRegListNumber << "[] = {\n";
1710b57cec5SDimitry Andric         O << IndentStr << "  ";
172*fe6060f1SDimitry Andric         ListSeparator LSS;
173*fe6060f1SDimitry Andric         for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i)
174*fe6060f1SDimitry Andric           O << LSS << getQualifiedName(ShadowRegList->getElementAsRecord(i));
1750b57cec5SDimitry Andric         O << "\n" << IndentStr << "};\n";
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric         O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
1780b57cec5SDimitry Andric           << RegListNumber << ", " << "RegList" << ShadowRegListNumber
1790b57cec5SDimitry Andric           << ")) {\n";
1800b57cec5SDimitry Andric       }
1810b57cec5SDimitry Andric       O << IndentStr << "  State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
1820b57cec5SDimitry Andric         << "Reg, LocVT, LocInfo));\n";
1830b57cec5SDimitry Andric       O << IndentStr << "  return false;\n";
1840b57cec5SDimitry Andric       O << IndentStr << "}\n";
1850b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCAssignToStack")) {
1860b57cec5SDimitry Andric       int Size = Action->getValueAsInt("Size");
1870b57cec5SDimitry Andric       int Align = Action->getValueAsInt("Align");
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric       O << IndentStr << "unsigned Offset" << ++Counter
1900b57cec5SDimitry Andric         << " = State.AllocateStack(";
1910b57cec5SDimitry Andric       if (Size)
1920b57cec5SDimitry Andric         O << Size << ", ";
1930b57cec5SDimitry Andric       else
1940b57cec5SDimitry Andric         O << "\n" << IndentStr
1950b57cec5SDimitry Andric           << "  State.getMachineFunction().getDataLayout()."
1960b57cec5SDimitry Andric              "getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())),"
1970b57cec5SDimitry Andric              " ";
1980b57cec5SDimitry Andric       if (Align)
1995ffd83dbSDimitry Andric         O << "Align(" << Align << ")";
2000b57cec5SDimitry Andric       else
2015ffd83dbSDimitry Andric         O << "\n"
2025ffd83dbSDimitry Andric           << IndentStr
2030b57cec5SDimitry Andric           << "  State.getMachineFunction().getDataLayout()."
2045ffd83dbSDimitry Andric              "getABITypeAlign(EVT(LocVT).getTypeForEVT(State.getContext()"
2050b57cec5SDimitry Andric              "))";
2060b57cec5SDimitry Andric       O << ");\n" << IndentStr
2070b57cec5SDimitry Andric         << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
2080b57cec5SDimitry Andric         << Counter << ", LocVT, LocInfo));\n";
2090b57cec5SDimitry Andric       O << IndentStr << "return false;\n";
2100b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
2110b57cec5SDimitry Andric       int Size = Action->getValueAsInt("Size");
2120b57cec5SDimitry Andric       int Align = Action->getValueAsInt("Align");
2130b57cec5SDimitry Andric       ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric       unsigned ShadowRegListNumber = ++Counter;
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric       O << IndentStr << "static const MCPhysReg ShadowRegList"
2180b57cec5SDimitry Andric           << ShadowRegListNumber << "[] = {\n";
2190b57cec5SDimitry Andric       O << IndentStr << "  ";
220*fe6060f1SDimitry Andric       ListSeparator LS;
221*fe6060f1SDimitry Andric       for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i)
222*fe6060f1SDimitry Andric         O << LS << getQualifiedName(ShadowRegList->getElementAsRecord(i));
2230b57cec5SDimitry Andric       O << "\n" << IndentStr << "};\n";
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric       O << IndentStr << "unsigned Offset" << ++Counter
2265ffd83dbSDimitry Andric         << " = State.AllocateStack(" << Size << ", Align(" << Align << "), "
2270b57cec5SDimitry Andric         << "ShadowRegList" << ShadowRegListNumber << ");\n";
2280b57cec5SDimitry Andric       O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
2290b57cec5SDimitry Andric         << Counter << ", LocVT, LocInfo));\n";
2300b57cec5SDimitry Andric       O << IndentStr << "return false;\n";
2310b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCPromoteToType")) {
2320b57cec5SDimitry Andric       Record *DestTy = Action->getValueAsDef("DestTy");
2330b57cec5SDimitry Andric       MVT::SimpleValueType DestVT = getValueType(DestTy);
2340b57cec5SDimitry Andric       O << IndentStr << "LocVT = " << getEnumName(DestVT) <<";\n";
2350b57cec5SDimitry Andric       if (MVT(DestVT).isFloatingPoint()) {
2360b57cec5SDimitry Andric         O << IndentStr << "LocInfo = CCValAssign::FPExt;\n";
2370b57cec5SDimitry Andric       } else {
2380b57cec5SDimitry Andric         O << IndentStr << "if (ArgFlags.isSExt())\n"
239e8d8bef9SDimitry Andric           << IndentStr << "  LocInfo = CCValAssign::SExt;\n"
2400b57cec5SDimitry Andric           << IndentStr << "else if (ArgFlags.isZExt())\n"
241e8d8bef9SDimitry Andric           << IndentStr << "  LocInfo = CCValAssign::ZExt;\n"
2420b57cec5SDimitry Andric           << IndentStr << "else\n"
243e8d8bef9SDimitry Andric           << IndentStr << "  LocInfo = CCValAssign::AExt;\n";
2440b57cec5SDimitry Andric       }
2450b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) {
2460b57cec5SDimitry Andric       Record *DestTy = Action->getValueAsDef("DestTy");
2470b57cec5SDimitry Andric       MVT::SimpleValueType DestVT = getValueType(DestTy);
2480b57cec5SDimitry Andric       O << IndentStr << "LocVT = " << getEnumName(DestVT) << ";\n";
2490b57cec5SDimitry Andric       if (MVT(DestVT).isFloatingPoint()) {
2500b57cec5SDimitry Andric         PrintFatalError(Action->getLoc(),
2510b57cec5SDimitry Andric                         "CCPromoteToUpperBitsInType does not handle floating "
2520b57cec5SDimitry Andric                         "point");
2530b57cec5SDimitry Andric       } else {
2540b57cec5SDimitry Andric         O << IndentStr << "if (ArgFlags.isSExt())\n"
255e8d8bef9SDimitry Andric           << IndentStr << "  LocInfo = CCValAssign::SExtUpper;\n"
2560b57cec5SDimitry Andric           << IndentStr << "else if (ArgFlags.isZExt())\n"
257e8d8bef9SDimitry Andric           << IndentStr << "  LocInfo = CCValAssign::ZExtUpper;\n"
2580b57cec5SDimitry Andric           << IndentStr << "else\n"
259e8d8bef9SDimitry Andric           << IndentStr << "  LocInfo = CCValAssign::AExtUpper;\n";
2600b57cec5SDimitry Andric       }
2610b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCBitConvertToType")) {
2620b57cec5SDimitry Andric       Record *DestTy = Action->getValueAsDef("DestTy");
2630b57cec5SDimitry Andric       O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
2640b57cec5SDimitry Andric       O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
2658bcb0991SDimitry Andric     } else if (Action->isSubClassOf("CCTruncToType")) {
2668bcb0991SDimitry Andric       Record *DestTy = Action->getValueAsDef("DestTy");
2678bcb0991SDimitry Andric       O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
2688bcb0991SDimitry Andric       O << IndentStr << "LocInfo = CCValAssign::Trunc;\n";
2690b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCPassIndirect")) {
2700b57cec5SDimitry Andric       Record *DestTy = Action->getValueAsDef("DestTy");
2710b57cec5SDimitry Andric       O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
2720b57cec5SDimitry Andric       O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
2730b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCPassByVal")) {
2740b57cec5SDimitry Andric       int Size = Action->getValueAsInt("Size");
2750b57cec5SDimitry Andric       int Align = Action->getValueAsInt("Align");
2765ffd83dbSDimitry Andric       O << IndentStr << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
2775ffd83dbSDimitry Andric         << Size << ", Align(" << Align << "), ArgFlags);\n";
2780b57cec5SDimitry Andric       O << IndentStr << "return false;\n";
2790b57cec5SDimitry Andric     } else if (Action->isSubClassOf("CCCustom")) {
2800b57cec5SDimitry Andric       O << IndentStr
2810b57cec5SDimitry Andric         << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
2820b57cec5SDimitry Andric         << "LocVT, LocInfo, ArgFlags, State))\n";
283e8d8bef9SDimitry Andric       O << IndentStr << "  return false;\n";
2840b57cec5SDimitry Andric     } else {
2850b57cec5SDimitry Andric       errs() << *Action;
2860b57cec5SDimitry Andric       PrintFatalError(Action->getLoc(), "Unknown CCAction!");
2870b57cec5SDimitry Andric     }
2880b57cec5SDimitry Andric   }
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric namespace llvm {
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
2940b57cec5SDimitry Andric   emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
2950b57cec5SDimitry Andric   CallingConvEmitter(RK).run(OS);
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric } // End llvm namespace
299