blob: 82013c94c9624730fb3806c7b16c5a4865ba52b4 [file] [log] [blame]
Chris Lattner88ee2a12007-02-27 22:05:51 +00001//===- CallingConvEmitter.cpp - Generate calling conventions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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 "CallingConvEmitter.h"
16#include "Record.h"
17#include "CodeGenTarget.h"
18using namespace llvm;
19
20void CallingConvEmitter::run(std::ostream &O) {
21 EmitSourceFileHeader("Calling Convention Implementation Fragment", O);
22
23 std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv");
24
25 // Emit prototypes for all of the CC's so that they can forward ref each
26 // other.
27 for (unsigned i = 0, e = CCs.size(); i != e; ++i) {
28 O << "static bool " << CCs[i]->getName()
Chris Lattner2092c8a2007-02-28 04:43:48 +000029 << "(unsigned ValNo, MVT::ValueType ValVT,\n"
Chris Lattner88ee2a12007-02-27 22:05:51 +000030 << std::string(CCs[i]->getName().size()+13, ' ')
Chris Lattner2092c8a2007-02-28 04:43:48 +000031 << "MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,\n"
32 << std::string(CCs[i]->getName().size()+13, ' ')
33 << "unsigned ArgFlags, CCState &State);\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +000034 }
35
36 // Emit each calling convention description in full.
37 for (unsigned i = 0, e = CCs.size(); i != e; ++i)
38 EmitCallingConv(CCs[i], O);
39}
40
41
42void CallingConvEmitter::EmitCallingConv(Record *CC, std::ostream &O) {
43 ListInit *CCActions = CC->getValueAsListInit("Actions");
44 Counter = 0;
45
46 O << "\n\nstatic bool " << CC->getName()
Chris Lattner2092c8a2007-02-28 04:43:48 +000047 << "(unsigned ValNo, MVT::ValueType ValVT,\n"
Chris Lattner88ee2a12007-02-27 22:05:51 +000048 << std::string(CC->getName().size()+13, ' ')
Chris Lattner2092c8a2007-02-28 04:43:48 +000049 << "MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,\n"
50 << std::string(CC->getName().size()+13, ' ')
Chris Lattner88ee2a12007-02-27 22:05:51 +000051 << "unsigned ArgFlags, CCState &State) {\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +000052 // Emit all of the actions, in order.
53 for (unsigned i = 0, e = CCActions->getSize(); i != e; ++i) {
54 O << "\n";
55 EmitAction(CCActions->getElementAsRecord(i), 2, O);
56 }
57
58 O << "\n return true; // CC didn't match.\n";
59 O << "}\n";
60}
61
62void CallingConvEmitter::EmitAction(Record *Action,
63 unsigned Indent, std::ostream &O) {
64 std::string IndentStr = std::string(Indent, ' ');
65
66 if (Action->isSubClassOf("CCPredicateAction")) {
67 O << IndentStr << "if (";
68
Chris Lattnere3bab802007-02-28 05:29:06 +000069 if (Action->isSubClassOf("CCIfType")) {
Chris Lattner88ee2a12007-02-27 22:05:51 +000070 ListInit *VTs = Action->getValueAsListInit("VTs");
71 for (unsigned i = 0, e = VTs->getSize(); i != e; ++i) {
72 Record *VT = VTs->getElementAsRecord(i);
Chris Lattner2092c8a2007-02-28 04:43:48 +000073 if (i != 0) O << " ||\n " << IndentStr;
Chris Lattner88ee2a12007-02-27 22:05:51 +000074 O << "LocVT == " << getEnumName(getValueType(VT));
75 }
76
Chris Lattnere3bab802007-02-28 05:29:06 +000077 } else if (Action->isSubClassOf("CCIf")) {
Chris Lattner88ee2a12007-02-27 22:05:51 +000078 O << Action->getValueAsString("Predicate");
79 } else {
80 Action->dump();
81 throw "Unknown CCPredicateAction!";
82 }
83
84 O << ") {\n";
85 EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O);
86 O << IndentStr << "}\n";
87 } else {
88 if (Action->isSubClassOf("CCDelegateTo")) {
89 Record *CC = Action->getValueAsDef("CC");
90 O << IndentStr << "if (!" << CC->getName()
91 << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
92 << IndentStr << " return false;\n";
93 } else if (Action->isSubClassOf("CCAssignToReg")) {
94 ListInit *RegList = Action->getValueAsListInit("RegList");
95 if (RegList->getSize() == 1) {
96 O << IndentStr << "if (unsigned Reg = State.AllocateReg(";
97 O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n";
98 } else {
99 O << IndentStr << "static const unsigned RegList" << ++Counter
100 << "[] = {\n";
101 O << IndentStr << " ";
102 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
103 if (i != 0) O << ", ";
104 O << getQualifiedName(RegList->getElementAsRecord(i));
105 }
106 O << "\n" << IndentStr << "};\n";
107 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList"
108 << Counter << ", " << RegList->getSize() << ")) {\n";
109 }
110 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, "
111 << "Reg, LocVT, LocInfo));\n";
112 O << IndentStr << " return false;\n";
113 O << IndentStr << "}\n";
114 } else if (Action->isSubClassOf("CCAssignToStack")) {
115 int Size = Action->getValueAsInt("Size");
116 int Align = Action->getValueAsInt("Align");
Duncan Sands87b665d2007-11-14 08:29:13 +0000117
Chris Lattner88ee2a12007-02-27 22:05:51 +0000118 O << IndentStr << "unsigned Offset" << ++Counter
Duncan Sands87b665d2007-11-14 08:29:13 +0000119 << " = State.AllocateStack(";
120 if (Size)
121 O << Size << ", ";
122 else
123 O << "\n" << IndentStr << " State.getTarget().getTargetData()"
124 "->getABITypeSize(MVT::getTypeForValueType(LocVT)), ";
125 if (Align)
126 O << Align;
127 else
128 O << "\n" << IndentStr << " State.getTarget().getTargetData()"
129 "->getABITypeAlignment(MVT::getTypeForValueType(LocVT))";
130 O << ");\n" << IndentStr
131 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
Dale Johannesene3ef7442007-11-10 22:07:15 +0000132 << Counter << ", LocVT, LocInfo));\n";
133 O << IndentStr << "return false;\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +0000134 } else if (Action->isSubClassOf("CCPromoteToType")) {
Chris Lattner2092c8a2007-02-28 04:43:48 +0000135 Record *DestTy = Action->getValueAsDef("DestTy");
136 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n";
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000137 O << IndentStr << "if (ArgFlags & ISD::ParamFlags::SExt)\n"
138 << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n"
139 << IndentStr << "else if (ArgFlags & ISD::ParamFlags::ZExt)\n"
140 << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n"
141 << IndentStr << "else\n"
142 << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n";
Rafael Espindola1aa7efb2007-07-06 10:57:03 +0000143 } else if (Action->isSubClassOf("CCStructAssign")) {
Rafael Espindola594d37e2007-08-10 14:44:42 +0000144 O << IndentStr <<
145 "State.HandleStruct(ValNo, ValVT, LocVT, LocInfo, ArgFlags);\n";
146 O << IndentStr << "return false;\n";
Chris Lattner88ee2a12007-02-27 22:05:51 +0000147 } else {
148 Action->dump();
149 throw "Unknown CCAction!";
150 }
151 }
Chris Lattner50d45652007-02-27 22:08:27 +0000152}