CallingConvEmitter.cpp 12 KB

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