CallingConvEmitter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. for (unsigned i = 0, e = RegList->size(); i != e; ++i) {
  119. if (i != 0) O << ", ";
  120. O << getQualifiedName(RegList->getElementAsRecord(i));
  121. }
  122. O << "\n" << IndentStr << "};\n";
  123. O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
  124. << Counter << ")) {\n";
  125. }
  126. O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
  127. << "Reg, LocVT, LocInfo));\n";
  128. O << IndentStr << " return false;\n";
  129. O << IndentStr << "}\n";
  130. } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
  131. ListInit *RegList = Action->getValueAsListInit("RegList");
  132. ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
  133. if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size())
  134. PrintFatalError(Action->getLoc(),
  135. "Invalid length of list of shadowed registers");
  136. if (RegList->size() == 1) {
  137. O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
  138. O << getQualifiedName(RegList->getElementAsRecord(0));
  139. O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0));
  140. O << ")) {\n";
  141. } else {
  142. unsigned RegListNumber = ++Counter;
  143. unsigned ShadowRegListNumber = ++Counter;
  144. O << IndentStr << "static const MCPhysReg RegList" << RegListNumber
  145. << "[] = {\n";
  146. O << IndentStr << " ";
  147. for (unsigned i = 0, e = RegList->size(); i != e; ++i) {
  148. if (i != 0) O << ", ";
  149. O << getQualifiedName(RegList->getElementAsRecord(i));
  150. }
  151. O << "\n" << IndentStr << "};\n";
  152. O << IndentStr << "static const MCPhysReg RegList"
  153. << ShadowRegListNumber << "[] = {\n";
  154. O << IndentStr << " ";
  155. for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i) {
  156. if (i != 0) O << ", ";
  157. O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
  158. }
  159. O << "\n" << IndentStr << "};\n";
  160. O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
  161. << RegListNumber << ", " << "RegList" << ShadowRegListNumber
  162. << ")) {\n";
  163. }
  164. O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
  165. << "Reg, LocVT, LocInfo));\n";
  166. O << IndentStr << " return false;\n";
  167. O << IndentStr << "}\n";
  168. } else if (Action->isSubClassOf("CCAssignToStack")) {
  169. int Size = Action->getValueAsInt("Size");
  170. int Align = Action->getValueAsInt("Align");
  171. O << IndentStr << "unsigned Offset" << ++Counter
  172. << " = State.AllocateStack(";
  173. if (Size)
  174. O << Size << ", ";
  175. else
  176. O << "\n" << IndentStr
  177. << " State.getMachineFunction().getDataLayout()."
  178. "getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())),"
  179. " ";
  180. if (Align)
  181. O << "Align(" << Align << ")";
  182. else
  183. O << "\n"
  184. << IndentStr
  185. << " State.getMachineFunction().getDataLayout()."
  186. "getABITypeAlign(EVT(LocVT).getTypeForEVT(State.getContext()"
  187. "))";
  188. O << ");\n" << IndentStr
  189. << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
  190. << Counter << ", LocVT, LocInfo));\n";
  191. O << IndentStr << "return false;\n";
  192. } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
  193. int Size = Action->getValueAsInt("Size");
  194. int Align = Action->getValueAsInt("Align");
  195. ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
  196. unsigned ShadowRegListNumber = ++Counter;
  197. O << IndentStr << "static const MCPhysReg ShadowRegList"
  198. << ShadowRegListNumber << "[] = {\n";
  199. O << IndentStr << " ";
  200. for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i) {
  201. if (i != 0) O << ", ";
  202. O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
  203. }
  204. O << "\n" << IndentStr << "};\n";
  205. O << IndentStr << "unsigned Offset" << ++Counter
  206. << " = State.AllocateStack(" << Size << ", Align(" << Align << "), "
  207. << "ShadowRegList" << ShadowRegListNumber << ");\n";
  208. O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
  209. << Counter << ", LocVT, LocInfo));\n";
  210. O << IndentStr << "return false;\n";
  211. } else if (Action->isSubClassOf("CCPromoteToType")) {
  212. Record *DestTy = Action->getValueAsDef("DestTy");
  213. MVT::SimpleValueType DestVT = getValueType(DestTy);
  214. O << IndentStr << "LocVT = " << getEnumName(DestVT) <<";\n";
  215. if (MVT(DestVT).isFloatingPoint()) {
  216. O << IndentStr << "LocInfo = CCValAssign::FPExt;\n";
  217. } else {
  218. O << IndentStr << "if (ArgFlags.isSExt())\n"
  219. << IndentStr << " LocInfo = CCValAssign::SExt;\n"
  220. << IndentStr << "else if (ArgFlags.isZExt())\n"
  221. << IndentStr << " LocInfo = CCValAssign::ZExt;\n"
  222. << IndentStr << "else\n"
  223. << IndentStr << " LocInfo = CCValAssign::AExt;\n";
  224. }
  225. } else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) {
  226. Record *DestTy = Action->getValueAsDef("DestTy");
  227. MVT::SimpleValueType DestVT = getValueType(DestTy);
  228. O << IndentStr << "LocVT = " << getEnumName(DestVT) << ";\n";
  229. if (MVT(DestVT).isFloatingPoint()) {
  230. PrintFatalError(Action->getLoc(),
  231. "CCPromoteToUpperBitsInType does not handle floating "
  232. "point");
  233. } else {
  234. O << IndentStr << "if (ArgFlags.isSExt())\n"
  235. << IndentStr << " LocInfo = CCValAssign::SExtUpper;\n"
  236. << IndentStr << "else if (ArgFlags.isZExt())\n"
  237. << IndentStr << " LocInfo = CCValAssign::ZExtUpper;\n"
  238. << IndentStr << "else\n"
  239. << IndentStr << " LocInfo = CCValAssign::AExtUpper;\n";
  240. }
  241. } else if (Action->isSubClassOf("CCBitConvertToType")) {
  242. Record *DestTy = Action->getValueAsDef("DestTy");
  243. O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
  244. O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
  245. } else if (Action->isSubClassOf("CCTruncToType")) {
  246. Record *DestTy = Action->getValueAsDef("DestTy");
  247. O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
  248. O << IndentStr << "LocInfo = CCValAssign::Trunc;\n";
  249. } else if (Action->isSubClassOf("CCPassIndirect")) {
  250. Record *DestTy = Action->getValueAsDef("DestTy");
  251. O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
  252. O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
  253. } else if (Action->isSubClassOf("CCPassByVal")) {
  254. int Size = Action->getValueAsInt("Size");
  255. int Align = Action->getValueAsInt("Align");
  256. O << IndentStr << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
  257. << Size << ", Align(" << Align << "), ArgFlags);\n";
  258. O << IndentStr << "return false;\n";
  259. } else if (Action->isSubClassOf("CCCustom")) {
  260. O << IndentStr
  261. << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
  262. << "LocVT, LocInfo, ArgFlags, State))\n";
  263. O << IndentStr << " return false;\n";
  264. } else {
  265. errs() << *Action;
  266. PrintFatalError(Action->getLoc(), "Unknown CCAction!");
  267. }
  268. }
  269. }
  270. namespace llvm {
  271. void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
  272. emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
  273. CallingConvEmitter(RK).run(OS);
  274. }
  275. } // End llvm namespace