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) {
39 std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv");
41 // Emit prototypes for all of the CC's so that they can forward ref each
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";
52 // Emit each calling convention description in full.
53 for (unsigned i = 0, e = CCs.size(); i != e; ++i)
54 EmitCallingConv(CCs[i], O);
58 void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
59 ListInit *CCActions = CC->getValueAsListInit("Actions");
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) {
71 EmitAction(CCActions->getElementAsRecord(i), 2, O);
74 O << "\n return true; // CC didn't match.\n";
78 void CallingConvEmitter::EmitAction(Record *Action,
79 unsigned Indent, raw_ostream &O) {
80 std::string IndentStr = std::string(Indent, ' ');
82 if (Action->isSubClassOf("CCPredicateAction")) {
83 O << IndentStr << "if (";
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));
93 } else if (Action->isSubClassOf("CCIf")) {
94 O << Action->getValueAsString("Predicate");
97 PrintFatalError("Unknown CCPredicateAction!");
101 EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O);
102 O << IndentStr << "}\n";
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";
115 O << IndentStr << "static const uint16_t RegList" << ++Counter
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));
122 O << "\n" << IndentStr << "};\n";
123 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
124 << Counter << ", " << RegList->getSize() << ")) {\n";
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");
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));
143 unsigned RegListNumber = ++Counter;
144 unsigned ShadowRegListNumber = ++Counter;
146 O << IndentStr << "static const uint16_t RegList" << RegListNumber
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));
153 O << "\n" << IndentStr << "};\n";
155 O << IndentStr << "static const uint16_t 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));
162 O << "\n" << IndentStr << "};\n";
164 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
165 << RegListNumber << ", " << "RegList" << ShadowRegListNumber
166 << ", " << RegList->getSize() << ")) {\n";
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");
176 O << IndentStr << "unsigned Offset" << ++Counter
177 << " = State.AllocateStack(";
181 O << "\n" << IndentStr << " State.getTarget().getDataLayout()"
182 "->getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext())), ";
186 O << "\n" << IndentStr << " State.getTarget().getDataLayout()"
187 "->getABITypeAlignment(EVT(LocVT).getTypeForEVT(State.getContext()))";
188 if (Action->isSubClassOf("CCAssignToStackWithShadow"))
189 O << ", " << getQualifiedName(Action->getValueAsDef("ShadowReg"));
190 O << ");\n" << IndentStr
191 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
192 << Counter << ", LocVT, LocInfo));\n";
193 O << IndentStr << "return false;\n";
194 } else if (Action->isSubClassOf("CCPromoteToType")) {
195 Record *DestTy = Action->getValueAsDef("DestTy");
196 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
197 O << IndentStr << "if (ArgFlags.isSExt())\n"
198 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
199 << IndentStr << "else if (ArgFlags.isZExt())\n"
200 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
201 << IndentStr << "else\n"
202 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
203 } else if (Action->isSubClassOf("CCBitConvertToType")) {
204 Record *DestTy = Action->getValueAsDef("DestTy");
205 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
206 O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
207 } else if (Action->isSubClassOf("CCPassIndirect")) {
208 Record *DestTy = Action->getValueAsDef("DestTy");
209 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
210 O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";
211 } else if (Action->isSubClassOf("CCPassByVal")) {
212 int Size = Action->getValueAsInt("Size");
213 int Align = Action->getValueAsInt("Align");
215 << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, "
216 << Size << ", " << Align << ", ArgFlags);\n";
217 O << IndentStr << "return false;\n";
218 } else if (Action->isSubClassOf("CCCustom")) {
220 << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, "
221 << "LocVT, LocInfo, ArgFlags, State))\n";
222 O << IndentStr << IndentStr << "return false;\n";
225 PrintFatalError("Unknown CCAction!");
232 void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) {
233 emitSourceFileHeader("Calling Convention Implementation Fragment", OS);
234 CallingConvEmitter(RK).run(OS);
237 } // End llvm namespace