Move TargetData to DataLayout.
[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/Record.h"
17 #include "llvm/TableGen/TableGenBackend.h"
18 #include <cassert>
19 using namespace llvm;
20
21 namespace {
22 class CallingConvEmitter {
23   RecordKeeper &Records;
24 public:
25   explicit CallingConvEmitter(RecordKeeper &R) : Records(R) {}
26
27   void run(raw_ostream &o);
28
29 private:
30   void EmitCallingConv(Record *CC, raw_ostream &O);
31   void EmitAction(Record *Action, unsigned Indent, raw_ostream &O);
32   unsigned Counter;
33 };
34 } // End anonymous namespace
35
36 void CallingConvEmitter::run(raw_ostream &O) {
37
38   std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv");
39   
40   // Emit prototypes for all of the CC's so that they can forward ref each
41   // other.
42   for (unsigned i = 0, e = CCs.size(); i != e; ++i) {
43     O << "static bool " << CCs[i]->getName()
44       << "(unsigned ValNo, MVT ValVT,\n"
45       << std::string(CCs[i]->getName().size()+13, ' ')
46       << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
47       << std::string(CCs[i]->getName().size()+13, ' ')
48       << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n";
49   }
50   
51   // Emit each calling convention description in full.
52   for (unsigned i = 0, e = CCs.size(); i != e; ++i)
53     EmitCallingConv(CCs[i], O);
54 }
55
56
57 void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
58   ListInit *CCActions = CC->getValueAsListInit("Actions");
59   Counter = 0;
60
61   O << "\n\nstatic bool " << CC->getName()
62     << "(unsigned ValNo, MVT ValVT,\n"
63     << std::string(CC->getName().size()+13, ' ')
64     << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
65     << std::string(CC->getName().size()+13, ' ')
66     << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
67   // Emit all of the actions, in order.
68   for (unsigned i = 0, e = CCActions->getSize(); i != e; ++i) {
69     O << "\n";
70     EmitAction(CCActions->getElementAsRecord(i), 2, O);
71   }
72   
73   O << "\n  return true;  // CC didn't match.\n";
74   O << "}\n";
75 }
76
77 void CallingConvEmitter::EmitAction(Record *Action,
78                                     unsigned Indent, raw_ostream &O) {
79   std::string IndentStr = std::string(Indent, ' ');
80   
81   if (Action->isSubClassOf("CCPredicateAction")) {
82     O << IndentStr << "if (";
83     
84     if (Action->isSubClassOf("CCIfType")) {
85       ListInit *VTs = Action->getValueAsListInit("VTs");
86       for (unsigned i = 0, e = VTs->getSize(); i != e; ++i) {
87         Record *VT = VTs->getElementAsRecord(i);
88         if (i != 0) O << " ||\n    " << IndentStr;
89         O << "LocVT == " << getEnumName(getValueType(VT));
90       }
91
92     } else if (Action->isSubClassOf("CCIf")) {
93       O << Action->getValueAsString("Predicate");
94     } else {
95       Action->dump();
96       throw "Unknown CCPredicateAction!";
97     }
98     
99     O << ") {\n";
100     EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O);
101     O << IndentStr << "}\n";
102   } else {
103     if (Action->isSubClassOf("CCDelegateTo")) {
104       Record *CC = Action->getValueAsDef("CC");
105       O << IndentStr << "if (!" << CC->getName()
106         << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
107         << IndentStr << "  return false;\n";
108     } else if (Action->isSubClassOf("CCAssignToReg")) {
109       ListInit *RegList = Action->getValueAsListInit("RegList");
110       if (RegList->getSize() == 1) {
111         O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
112         O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n";
113       } else {
114         O << IndentStr << "static const uint16_t RegList" << ++Counter
115           << "[] = {\n";
116         O << IndentStr << "  ";
117         for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
118           if (i != 0) O << ", ";
119           O << getQualifiedName(RegList->getElementAsRecord(i));
120         }
121         O << "\n" << IndentStr << "};\n";
122         O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
123           << Counter << ", " << RegList->getSize() << ")) {\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->getSize() >0 &&
133           ShadowRegList->getSize() != RegList->getSize())
134         throw "Invalid length of list of shadowed registers";
135
136       if (RegList->getSize() == 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
145         O << IndentStr << "static const uint16_t RegList" << RegListNumber
146           << "[] = {\n";
147         O << IndentStr << "  ";
148         for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
149           if (i != 0) O << ", ";
150           O << getQualifiedName(RegList->getElementAsRecord(i));
151         }
152         O << "\n" << IndentStr << "};\n";
153
154         O << IndentStr << "static const uint16_t RegList"
155           << ShadowRegListNumber << "[] = {\n";
156         O << IndentStr << "  ";
157         for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) {
158           if (i != 0) O << ", ";
159           O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
160         }
161         O << "\n" << IndentStr << "};\n";
162
163         O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
164           << RegListNumber << ", " << "RegList" << ShadowRegListNumber
165           << ", " << RegList->getSize() << ")) {\n";
166       }
167       O << IndentStr << "  State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
168         << "Reg, LocVT, LocInfo));\n";
169       O << IndentStr << "  return false;\n";
170       O << IndentStr << "}\n";
171     } else if (Action->isSubClassOf("CCAssignToStack")) {
172       int Size = Action->getValueAsInt("Size");
173       int Align = Action->getValueAsInt("Align");
174
175       O << IndentStr << "unsigned Offset" << ++Counter
176         << " = State.AllocateStack(";
177       if (Size)
178         O << Size << ", ";
179       else
180         O << "\n" << IndentStr << "  State.getTarget().getDataLayout()"
181           "->getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())), ";
182       if (Align)
183         O << Align;
184       else
185         O << "\n" << IndentStr << "  State.getTarget().getDataLayout()"
186           "->getABITypeAlignment(EVT(LocVT).getTypeForEVT(State.getContext()))";
187       if (Action->isSubClassOf("CCAssignToStackWithShadow"))
188         O << ", " << getQualifiedName(Action->getValueAsDef("ShadowReg"));
189       O << ");\n" << IndentStr
190         << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
191         << Counter << ", LocVT, LocInfo));\n";
192       O << IndentStr << "return false;\n";
193     } else if (Action->isSubClassOf("CCPromoteToType")) {
194       Record *DestTy = Action->getValueAsDef("DestTy");
195       O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
196       O << IndentStr << "if (ArgFlags.isSExt())\n"
197         << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
198         << IndentStr << "else if (ArgFlags.isZExt())\n"
199         << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
200         << IndentStr << "else\n"
201         << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
202     } else if (Action->isSubClassOf("CCBitConvertToType")) {
203       Record *DestTy = Action->getValueAsDef("DestTy");
204       O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
205       O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
206     } else if (Action->isSubClassOf("CCPassIndirect")) {
207       Record *DestTy = Action->getValueAsDef("DestTy");
208       O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
209       O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
210     } else if (Action->isSubClassOf("CCPassByVal")) {
211       int Size = Action->getValueAsInt("Size");
212       int Align = Action->getValueAsInt("Align");
213       O << IndentStr
214         << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
215         << Size << ", " << Align << ", ArgFlags);\n";
216       O << IndentStr << "return false;\n";
217     } else if (Action->isSubClassOf("CCCustom")) {
218       O << IndentStr
219         << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
220         << "LocVT, LocInfo, ArgFlags, State))\n";
221       O << IndentStr << IndentStr << "return false;\n";
222     } else {
223       Action->dump();
224       throw "Unknown CCAction!";
225     }
226   }
227 }
228
229 namespace llvm {
230
231 void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
232   emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
233   CallingConvEmitter(RK).run(OS);
234 }
235
236 } // End llvm namespace