1 //===- CallingConvEmitter.cpp - Generate calling conventions --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is responsible for emitting descriptions of the calling
11 // conventions supported by this target.
13 //===----------------------------------------------------------------------===//
15 #include "CodeGenTarget.h"
16 #include "llvm/TableGen/Error.h"
17 #include "llvm/TableGen/Record.h"
18 #include "llvm/TableGen/TableGenBackend.h"
23 class CallingConvEmitter {
24 RecordKeeper &Records;
26 explicit CallingConvEmitter(RecordKeeper &R) : Records(R) {}
28 void run(raw_ostream &o);
31 void EmitCallingConv(Record *CC, raw_ostream &O);
32 void EmitAction(Record *Action, unsigned Indent, raw_ostream &O);
35 } // End anonymous namespace
37 void CallingConvEmitter::run(raw_ostream &O) {
38 std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv");
40 // Emit prototypes for all of the non-custom CC's so that they can forward ref
42 for (unsigned i = 0, e = CCs.size(); i != e; ++i) {
43 if (!CCs[i]->getValueAsBit("Custom")) {
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";
53 // Emit each non-custom calling convention description in full.
54 for (unsigned i = 0, e = CCs.size(); i != e; ++i) {
55 if (!CCs[i]->getValueAsBit("Custom"))
56 EmitCallingConv(CCs[i], O);
61 void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
62 ListInit *CCActions = CC->getValueAsListInit("Actions");
65 O << "\n\nstatic bool " << CC->getName()
66 << "(unsigned ValNo, MVT ValVT,\n"
67 << std::string(CC->getName().size()+13, ' ')
68 << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
69 << std::string(CC->getName().size()+13, ' ')
70 << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
71 // Emit all of the actions, in order.
72 for (unsigned i = 0, e = CCActions->getSize(); i != e; ++i) {
74 EmitAction(CCActions->getElementAsRecord(i), 2, O);
77 O << "\n return true; // CC didn't match.\n";
81 void CallingConvEmitter::EmitAction(Record *Action,
82 unsigned Indent, raw_ostream &O) {
83 std::string IndentStr = std::string(Indent, ' ');
85 if (Action->isSubClassOf("CCPredicateAction")) {
86 O << IndentStr << "if (";
88 if (Action->isSubClassOf("CCIfType")) {
89 ListInit *VTs = Action->getValueAsListInit("VTs");
90 for (unsigned i = 0, e = VTs->getSize(); i != e; ++i) {
91 Record *VT = VTs->getElementAsRecord(i);
92 if (i != 0) O << " ||\n " << IndentStr;
93 O << "LocVT == " << getEnumName(getValueType(VT));
96 } else if (Action->isSubClassOf("CCIf")) {
97 O << Action->getValueAsString("Predicate");
100 PrintFatalError("Unknown CCPredicateAction!");
104 EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O);
105 O << IndentStr << "}\n";
107 if (Action->isSubClassOf("CCDelegateTo")) {
108 Record *CC = Action->getValueAsDef("CC");
109 O << IndentStr << "if (!" << CC->getName()
110 << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
111 << IndentStr << " return false;\n";
112 } else if (Action->isSubClassOf("CCAssignToReg")) {
113 ListInit *RegList = Action->getValueAsListInit("RegList");
114 if (RegList->getSize() == 1) {
115 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
116 O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n";
118 O << IndentStr << "static const MCPhysReg RegList" << ++Counter
120 O << IndentStr << " ";
121 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
122 if (i != 0) O << ", ";
123 O << getQualifiedName(RegList->getElementAsRecord(i));
125 O << "\n" << IndentStr << "};\n";
126 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
127 << Counter << ", " << RegList->getSize() << ")) {\n";
129 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
130 << "Reg, LocVT, LocInfo));\n";
131 O << IndentStr << " return false;\n";
132 O << IndentStr << "}\n";
133 } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
134 ListInit *RegList = Action->getValueAsListInit("RegList");
135 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
136 if (ShadowRegList->getSize() >0 &&
137 ShadowRegList->getSize() != RegList->getSize())
138 PrintFatalError("Invalid length of list of shadowed registers");
140 if (RegList->getSize() == 1) {
141 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
142 O << getQualifiedName(RegList->getElementAsRecord(0));
143 O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0));
146 unsigned RegListNumber = ++Counter;
147 unsigned ShadowRegListNumber = ++Counter;
149 O << IndentStr << "static const MCPhysReg RegList" << RegListNumber
151 O << IndentStr << " ";
152 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
153 if (i != 0) O << ", ";
154 O << getQualifiedName(RegList->getElementAsRecord(i));
156 O << "\n" << IndentStr << "};\n";
158 O << IndentStr << "static const MCPhysReg RegList"
159 << ShadowRegListNumber << "[] = {\n";
160 O << IndentStr << " ";
161 for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) {
162 if (i != 0) O << ", ";
163 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
165 O << "\n" << IndentStr << "};\n";
167 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
168 << RegListNumber << ", " << "RegList" << ShadowRegListNumber
169 << ", " << RegList->getSize() << ")) {\n";
171 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
172 << "Reg, LocVT, LocInfo));\n";
173 O << IndentStr << " return false;\n";
174 O << IndentStr << "}\n";
175 } else if (Action->isSubClassOf("CCAssignToStack")) {
176 int Size = Action->getValueAsInt("Size");
177 int Align = Action->getValueAsInt("Align");
179 O << IndentStr << "unsigned Offset" << ++Counter
180 << " = State.AllocateStack(";
184 O << "\n" << IndentStr
185 << " State.getMachineFunction().getSubtarget().getDataLayout()"
186 "->getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())),"
191 O << "\n" << IndentStr
192 << " State.getMachineFunction().getSubtarget().getDataLayout()"
193 "->getABITypeAlignment(EVT(LocVT).getTypeForEVT(State.getContext()"
195 O << ");\n" << IndentStr
196 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
197 << Counter << ", LocVT, LocInfo));\n";
198 O << IndentStr << "return false;\n";
199 } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
200 int Size = Action->getValueAsInt("Size");
201 int Align = Action->getValueAsInt("Align");
202 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
204 unsigned ShadowRegListNumber = ++Counter;
206 O << IndentStr << "static const MCPhysReg ShadowRegList"
207 << ShadowRegListNumber << "[] = {\n";
208 O << IndentStr << " ";
209 for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) {
210 if (i != 0) O << ", ";
211 O << getQualifiedName(ShadowRegList->getElementAsRecord(i));
213 O << "\n" << IndentStr << "};\n";
215 O << IndentStr << "unsigned Offset" << ++Counter
216 << " = State.AllocateStack("
217 << Size << ", " << Align << ", "
218 << "ShadowRegList" << ShadowRegListNumber << ", "
219 << ShadowRegList->getSize() << ");\n";
220 O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
221 << Counter << ", LocVT, LocInfo));\n";
222 O << IndentStr << "return false;\n";
223 } else if (Action->isSubClassOf("CCPromoteToType")) {
224 Record *DestTy = Action->getValueAsDef("DestTy");
225 MVT::SimpleValueType DestVT = getValueType(DestTy);
226 O << IndentStr << "LocVT = " << getEnumName(DestVT) <<";\n";
227 if (MVT(DestVT).isFloatingPoint()) {
228 O << IndentStr << "LocInfo = CCValAssign::FPExt;\n";
230 O << IndentStr << "if (ArgFlags.isSExt())\n"
231 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
232 << IndentStr << "else if (ArgFlags.isZExt())\n"
233 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
234 << IndentStr << "else\n"
235 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
237 } else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) {
238 Record *DestTy = Action->getValueAsDef("DestTy");
239 MVT::SimpleValueType DestVT = getValueType(DestTy);
240 O << IndentStr << "LocVT = " << getEnumName(DestVT) << ";\n";
241 if (MVT(DestVT).isFloatingPoint()) {
242 PrintFatalError("CCPromoteToUpperBitsInType does not handle floating "
245 O << IndentStr << "if (ArgFlags.isSExt())\n"
246 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExtUpper;\n"
247 << IndentStr << "else if (ArgFlags.isZExt())\n"
248 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExtUpper;\n"
249 << IndentStr << "else\n"
250 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExtUpper;\n";
252 } else if (Action->isSubClassOf("CCBitConvertToType")) {
253 Record *DestTy = Action->getValueAsDef("DestTy");
254 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
255 O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
256 } else if (Action->isSubClassOf("CCPassIndirect")) {
257 Record *DestTy = Action->getValueAsDef("DestTy");
258 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
259 O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
260 } else if (Action->isSubClassOf("CCPassByVal")) {
261 int Size = Action->getValueAsInt("Size");
262 int Align = Action->getValueAsInt("Align");
264 << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
265 << Size << ", " << Align << ", ArgFlags);\n";
266 O << IndentStr << "return false;\n";
267 } else if (Action->isSubClassOf("CCCustom")) {
269 << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
270 << "LocVT, LocInfo, ArgFlags, State))\n";
271 O << IndentStr << IndentStr << "return false;\n";
274 PrintFatalError("Unknown CCAction!");
281 void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
282 emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
283 CallingConvEmitter(RK).run(OS);
286 } // End llvm namespace