907a90c76df9a938d3910e343e864579294e8fdb
[oota-llvm.git] / utils / TableGen / CallingConvEmitter.cpp
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>
20 using namespace llvm;
21
22 namespace {
23 class CallingConvEmitter {
24   RecordKeeper &Records;
25 public:
26   explicit CallingConvEmitter(RecordKeeper &R) : Records(R) {}
27
28   void run(raw_ostream &o);
29
30 private:
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
37 void 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
58 void 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
78 void 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
182           << "  State.getMachineFunction().getSubtarget().getDataLayout()"
183              "->getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())),"
184              " ";
185       if (Align)
186         O << Align;
187       else
188         O << "\n" << IndentStr
189           << "  State.getMachineFunction().getSubtarget().getDataLayout()"
190              "->getABITypeAlignment(EVT(LocVT).getTypeForEVT(State.getContext()"
191              "))";
192       O << ");\n" << IndentStr
193         << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
194         << Counter << ", LocVT, LocInfo));\n";
195       O << IndentStr << "return false;\n";
196     } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
197       int Size = Action->getValueAsInt("Size");
198       int Align = Action->getValueAsInt("Align");
199       ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
200
201       unsigned ShadowRegListNumber = ++Counter;
202
203       O << IndentStr << "static const MCPhysReg ShadowRegList"
204           << ShadowRegListNumber << "[] = {\n";
205       O << IndentStr << "  ";
206       for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) {
207         if (i != 0) O << ", ";
208         O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
209       }
210       O << "\n" << IndentStr << "};\n";
211
212       O << IndentStr << "unsigned Offset" << ++Counter
213         << " = State.AllocateStack("
214         << Size << ", " << Align << ", "
215         << "ShadowRegList" << ShadowRegListNumber << ", "
216         << ShadowRegList->getSize() << ");\n";
217       O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
218         << Counter << ", LocVT, LocInfo));\n";
219       O << IndentStr << "return false;\n";
220     } else if (Action->isSubClassOf("CCPromoteToType")) {
221       Record *DestTy = Action->getValueAsDef("DestTy");
222       MVT::SimpleValueType DestVT = getValueType(DestTy);
223       O << IndentStr << "LocVT = " << getEnumName(DestVT) <<";\n";
224       if (MVT(DestVT).isFloatingPoint()) {
225         O << IndentStr << "LocInfo = CCValAssign::FPExt;\n";
226       } else {
227         O << IndentStr << "if (ArgFlags.isSExt())\n"
228           << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
229           << IndentStr << "else if (ArgFlags.isZExt())\n"
230           << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
231           << IndentStr << "else\n"
232           << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
233       }
234     } else if (Action->isSubClassOf("CCBitConvertToType")) {
235       Record *DestTy = Action->getValueAsDef("DestTy");
236       O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
237       O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
238     } else if (Action->isSubClassOf("CCPassIndirect")) {
239       Record *DestTy = Action->getValueAsDef("DestTy");
240       O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
241       O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
242     } else if (Action->isSubClassOf("CCPassByVal")) {
243       int Size = Action->getValueAsInt("Size");
244       int Align = Action->getValueAsInt("Align");
245       O << IndentStr
246         << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
247         << Size << ", " << Align << ", ArgFlags);\n";
248       O << IndentStr << "return false;\n";
249     } else if (Action->isSubClassOf("CCCustom")) {
250       O << IndentStr
251         << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
252         << "LocVT, LocInfo, ArgFlags, State))\n";
253       O << IndentStr << IndentStr << "return false;\n";
254     } else {
255       Action->dump();
256       PrintFatalError("Unknown CCAction!");
257     }
258   }
259 }
260
261 namespace llvm {
262
263 void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
264   emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
265   CallingConvEmitter(RK).run(OS);
266 }
267
268 } // End llvm namespace