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