CallingConvEmitter.cpp 11 KB

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