blob: 34012cafe5f37fae67208384f5560b0e4e461c75 [file] [log] [blame]
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001//===--- CGVtable.cpp - Emit LLVM Code for C++ vtables --------------------===//
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 contains code dealing with C++ code generation of virtual tables.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenModule.h"
15#include "CodeGenFunction.h"
16
17#include "clang/AST/RecordLayout.h"
18
19using namespace clang;
20using namespace CodeGen;
21
22class VtableBuilder {
23public:
24 /// Index_t - Vtable index type.
25 typedef uint64_t Index_t;
26private:
27 std::vector<llvm::Constant *> &methods;
28 std::vector<llvm::Constant *> submethods;
29 llvm::Type *Ptr8Ty;
30 /// Class - The most derived class that this vtable is being built for.
31 const CXXRecordDecl *Class;
32 /// BLayout - Layout for the most derived class that this vtable is being
33 /// built for.
34 const ASTRecordLayout &BLayout;
35 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
36 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
37 llvm::Constant *rtti;
38 llvm::LLVMContext &VMContext;
39 CodeGenModule &CGM; // Per-module state.
40 /// Index - Maps a method decl into a vtable index. Useful for virtual
41 /// dispatch codegen.
42 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
43 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
44 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
45 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stumpbb9ff052009-10-27 23:46:47 +000046
47 typedef llvm::DenseMap<const CXXMethodDecl *, int> Pures_t;
48 Pures_t Pures;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000049 typedef std::pair<Index_t, Index_t> CallOffset;
50 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
51 Thunks_t Thunks;
52 typedef llvm::DenseMap<const CXXMethodDecl *,
Mike Stump87876a02009-10-13 10:55:21 +000053 std::pair<std::pair<CallOffset, CallOffset>,
54 CanQualType> > CovariantThunks_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000055 CovariantThunks_t CovariantThunks;
56 std::vector<Index_t> VCalls;
57 typedef CXXRecordDecl::method_iterator method_iter;
58 // FIXME: Linkage should follow vtable
59 const bool Extern;
60 const uint32_t LLVMPointerWidth;
61 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +000062 int CurrentVBaseOffset;
63 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stumpbb9ff052009-10-27 23:46:47 +000064 llvm::Constant *cxa_pure;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000065public:
66 VtableBuilder(std::vector<llvm::Constant *> &meth,
67 const CXXRecordDecl *c,
68 CodeGenModule &cgm)
69 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
70 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
71 CGM(cgm), Extern(true),
Mike Stump37dbe962009-10-15 02:04:03 +000072 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)),
73 CurrentVBaseOffset(0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000074 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump375faa82009-10-28 00:35:46 +000075
76 // Calculate pointer for ___cxa_pure_virtual.
77 const llvm::FunctionType *FTy;
78 std::vector<const llvm::Type*> ArgTys;
79 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
80 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
81 cxa_pure = wrap(CGM.CreateRuntimeFunction(FTy, "__cxa_pure_virtual"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +000082 }
83
84 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
85 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
86 { return VBIndex; }
87
88 llvm::Constant *wrap(Index_t i) {
89 llvm::Constant *m;
90 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
91 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
92 }
93
94 llvm::Constant *wrap(llvm::Constant *m) {
95 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
96 }
97
Mike Stump75ce5732009-10-31 20:06:59 +000098#define D1(x)
99//#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
100
101 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +0000102 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000103 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
104 e = RD->bases_end(); i != e; ++i) {
105 const CXXRecordDecl *Base =
106 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump28431212009-10-13 22:54:56 +0000107 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000108 if (i->isVirtual() && !SeenVBase.count(Base)) {
109 SeenVBase.insert(Base);
Mike Stump28431212009-10-13 22:54:56 +0000110 if (updateVBIndex) {
Mike Stump75ce5732009-10-31 20:06:59 +0000111 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stump28431212009-10-13 22:54:56 +0000112 - 3*LLVMPointerWidth/8);
113 VBIndex[Base] = next_vbindex;
114 }
Mike Stump75ce5732009-10-31 20:06:59 +0000115 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
116 VCalls.push_back((0?700:0) + BaseOffset);
117 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
118 Base->getNameAsCString(),
119 (int)-VCalls.size()-3, (int)BaseOffset,
120 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000121 }
Mike Stump28431212009-10-13 22:54:56 +0000122 // We also record offsets for non-virtual bases to closest enclosing
123 // virtual base. We do this so that we don't have to search
124 // for the nearst virtual base class when generating thunks.
125 if (updateVBIndex && VBIndex.count(Base) == 0)
126 VBIndex[Base] = next_vbindex;
Mike Stump75ce5732009-10-31 20:06:59 +0000127 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000128 }
129 }
130
131 void StartNewTable() {
132 SeenVBase.clear();
133 }
134
135 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
136
Mike Stump8bccbfd2009-10-15 09:30:16 +0000137 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
138 Index_t Offset = 0) {
139
140 if (B == D)
141 return Offset;
142
143 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
144 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
145 e = D->bases_end(); i != e; ++i) {
146 const CXXRecordDecl *Base =
147 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
148 int64_t BaseOffset = 0;
149 if (!i->isVirtual())
150 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
151 int64_t o = getNVOffset_1(Base, B, BaseOffset);
152 if (o >= 0)
153 return o;
154 }
155
156 return -1;
157 }
158
159 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
160 /// derived class D.
161 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000162 qD = qD->getPointeeType();
163 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000164 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
165 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
166 int64_t o = getNVOffset_1(D, B);
167 if (o >= 0)
168 return o;
169
170 assert(false && "FIXME: non-virtual base not found");
171 return 0;
172 }
173
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000174 /// getVbaseOffset - Returns the index into the vtable for the virtual base
175 /// offset for the given (B) virtual base of the derived class D.
176 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000177 qD = qD->getPointeeType();
178 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000179 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
180 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
181 if (D != Class)
182 return VBlookup(D, B);
183 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
184 i = VBIndex.find(B);
185 if (i != VBIndex.end())
186 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000187
Mike Stump28431212009-10-13 22:54:56 +0000188 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000189 return 0;
190 }
191
192 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stump8bccbfd2009-10-15 09:30:16 +0000193 bool MorallyVirtual, Index_t OverrideOffset,
194 Index_t Offset) {
Mike Stump375faa82009-10-28 00:35:46 +0000195 const bool isPure = MD->isPure();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000196 typedef CXXMethodDecl::method_iterator meth_iter;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000197 // FIXME: Should OverrideOffset's be Offset?
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000198
199 // FIXME: Don't like the nested loops. For very large inheritance
200 // heirarchies we could have a table on the side with the final overridder
201 // and just replace each instance of an overridden method once. Would be
202 // nice to measure the cost/benefit on real code.
203
204 for (meth_iter mi = MD->begin_overridden_methods(),
205 e = MD->end_overridden_methods();
206 mi != e; ++mi) {
207 const CXXMethodDecl *OMD = *mi;
208 llvm::Constant *om;
209 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
210 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
211
212 for (Index_t i = 0, e = submethods.size();
213 i != e; ++i) {
214 // FIXME: begin_overridden_methods might be too lax, covariance */
215 if (submethods[i] != om)
216 continue;
217 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
218 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
219 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
220 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
221 CallOffset ReturnOffset = std::make_pair(0, 0);
222 if (oret != ret) {
223 // FIXME: calculate offsets for covariance
Mike Stump87876a02009-10-13 10:55:21 +0000224 if (CovariantThunks.count(OMD)) {
225 oret = CovariantThunks[OMD].second;
226 CovariantThunks.erase(OMD);
227 }
Mike Stump8bccbfd2009-10-15 09:30:16 +0000228 // FIXME: Double check oret
229 Index_t nv = getNVOffset(oret, ret)/8;
Mike Stump87876a02009-10-13 10:55:21 +0000230 ReturnOffset = std::make_pair(nv, getVbaseOffset(oret, ret));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000231 }
232 Index[MD] = i;
233 submethods[i] = m;
Mike Stump375faa82009-10-28 00:35:46 +0000234 if (isPure)
235 Pures[MD] = 1;
236 Pures.erase(OMD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000237 Thunks.erase(OMD);
238 if (MorallyVirtual) {
239 Index_t &idx = VCall[OMD];
240 if (idx == 0) {
Mike Stump8bccbfd2009-10-15 09:30:16 +0000241 VCallOffset[MD] = OverrideOffset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000242 idx = VCalls.size()+1;
243 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000244 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
Mike Stump72431bd2009-11-06 02:38:24 +0000245 MD->getNameAsCString(), (int)-idx-3, (int)VCalls[idx-1],
Mike Stump75ce5732009-10-31 20:06:59 +0000246 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000247 } else {
248 VCallOffset[MD] = VCallOffset[OMD];
Mike Stump8bccbfd2009-10-15 09:30:16 +0000249 VCalls[idx-1] = -VCallOffset[OMD] + OverrideOffset/8;
Mike Stump75ce5732009-10-31 20:06:59 +0000250 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
Mike Stump72431bd2009-11-06 02:38:24 +0000251 MD->getNameAsCString(), (int)-idx-3, (int)VCalls[idx-1],
Mike Stump75ce5732009-10-31 20:06:59 +0000252 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000253 }
254 VCall[MD] = idx;
255 CallOffset ThisOffset;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000256 ThisOffset = std::make_pair(CurrentVBaseOffset/8 - Offset/8,
Mike Stump37dbe962009-10-15 02:04:03 +0000257 -((idx+extra+2)*LLVMPointerWidth/8));
258 // FIXME: Do we always have to build a covariant thunk to save oret,
259 // which is the containing virtual base class?
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000260 if (ReturnOffset.first || ReturnOffset.second)
Mike Stump87876a02009-10-13 10:55:21 +0000261 CovariantThunks[MD] = std::make_pair(std::make_pair(ThisOffset,
262 ReturnOffset),
263 oret);
Mike Stump375faa82009-10-28 00:35:46 +0000264 else if (!isPure)
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000265 Thunks[MD] = ThisOffset;
266 return true;
267 }
Mike Stump28431212009-10-13 22:54:56 +0000268
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000269 // FIXME: finish off
Mike Stump8bccbfd2009-10-15 09:30:16 +0000270 int64_t O = VCallOffset[OMD] - OverrideOffset/8;
271 // int64_t O = CurrentVBaseOffset/8 - OverrideOffset/8;
Mike Stump72431bd2009-11-06 02:38:24 +0000272
273 if (VCall.count(OMD)) {
274 VCallOffset[MD] = VCallOffset[OMD];
275 Index_t idx = VCall[OMD];
276 VCalls[idx-1] = -VCallOffset[OMD] + OverrideOffset/8;
277 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
278 MD->getNameAsCString(), (int)-idx-3, (int)VCalls[idx-1],
279 Class->getNameAsCString()));
280 VCall[MD] = idx;
281 }
Mike Stump28431212009-10-13 22:54:56 +0000282 if (O || ReturnOffset.first || ReturnOffset.second) {
283 CallOffset ThisOffset = std::make_pair(O, 0);
284
285 if (ReturnOffset.first || ReturnOffset.second)
286 CovariantThunks[MD] = std::make_pair(std::make_pair(ThisOffset,
287 ReturnOffset),
288 oret);
Mike Stump375faa82009-10-28 00:35:46 +0000289 else if (!isPure)
Mike Stump28431212009-10-13 22:54:56 +0000290 Thunks[MD] = ThisOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000291 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000292 return true;
293 }
294 }
295
296 return false;
297 }
298
299 void InstallThunks() {
300 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
301 i != e; ++i) {
302 const CXXMethodDecl *MD = i->first;
Mike Stump375faa82009-10-28 00:35:46 +0000303 assert(!MD->isPure() && "Trying to thunk a pure");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000304 Index_t idx = Index[MD];
305 Index_t nv_O = i->second.first;
306 Index_t v_O = i->second.second;
307 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
308 }
309 Thunks.clear();
310 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
311 e = CovariantThunks.end();
312 i != e; ++i) {
313 const CXXMethodDecl *MD = i->first;
Mike Stump375faa82009-10-28 00:35:46 +0000314 if (MD->isPure())
315 continue;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000316 Index_t idx = Index[MD];
Mike Stump87876a02009-10-13 10:55:21 +0000317 Index_t nv_t = i->second.first.first.first;
318 Index_t v_t = i->second.first.first.second;
319 Index_t nv_r = i->second.first.second.first;
320 Index_t v_r = i->second.first.second.second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000321 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
322 v_r);
323 }
324 CovariantThunks.clear();
Mike Stumpbb9ff052009-10-27 23:46:47 +0000325 for (Pures_t::iterator i = Pures.begin(), e = Pures.end();
326 i != e; ++i) {
327 const CXXMethodDecl *MD = i->first;
328 Index_t idx = Index[MD];
329 submethods[idx] = cxa_pure;
330 }
331 Pures.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000332 }
333
Mike Stump18e8b472009-10-27 23:36:26 +0000334 llvm::Constant *WrapAddrOf(const CXXMethodDecl *MD) {
335 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
336 return wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
337
338 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
339 const llvm::Type *Ty =
340 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
341 FPT->isVariadic());
342
343 return wrap(CGM.GetAddrOfFunction(MD, Ty));
344 }
345
Mike Stump8bccbfd2009-10-15 09:30:16 +0000346 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000347 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000348 e = Path->rend(); i != e; ++i) {
349 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000350 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000351 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
352 ++mi) {
353 if (!mi->isVirtual())
354 continue;
355
356 const CXXMethodDecl *MD = *mi;
Mike Stump18e8b472009-10-27 23:36:26 +0000357 llvm::Constant *m = WrapAddrOf(MD);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000358 OverrideMethod(MD, m, MorallyVirtual, OverrideOffset, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000359 }
360 }
361 }
362
Mike Stump75ce5732009-10-31 20:06:59 +0000363 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset,
364 bool ForVirtualBase) {
Mike Stump18e8b472009-10-27 23:36:26 +0000365 llvm::Constant *m = WrapAddrOf(MD);
366
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000367 // If we can find a previously allocated slot for this, reuse it.
Mike Stump8bccbfd2009-10-15 09:30:16 +0000368 if (OverrideMethod(MD, m, MorallyVirtual, Offset, Offset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000369 return;
370
371 // else allocate a new slot.
372 Index[MD] = submethods.size();
373 submethods.push_back(m);
Mike Stump75ce5732009-10-31 20:06:59 +0000374 D1(printf(" vfn for %s at %d\n", MD->getNameAsCString(), (int)Index[MD]));
Mike Stump375faa82009-10-28 00:35:46 +0000375 if (MD->isPure())
376 Pures[MD] = 1;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000377 if (MorallyVirtual) {
378 VCallOffset[MD] = Offset/8;
379 Index_t &idx = VCall[MD];
380 // Allocate the first one, after that, we reuse the previous one.
381 if (idx == 0) {
382 idx = VCalls.size()+1;
383 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000384 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stump72431bd2009-11-06 02:38:24 +0000385 MD->getNameAsCString(), (int)-VCalls.size()-3, 0));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000386 }
387 }
388 }
389
390 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Mike Stump75ce5732009-10-31 20:06:59 +0000391 Index_t Offset, bool RDisVirtualBase) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000392 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
393 ++mi)
394 if (mi->isVirtual())
Mike Stump75ce5732009-10-31 20:06:59 +0000395 AddMethod(*mi, MorallyVirtual, Offset, RDisVirtualBase);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000396 }
397
398 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
399 const CXXRecordDecl *PrimaryBase,
400 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stump37dbe962009-10-15 02:04:03 +0000401 int64_t Offset, Path_t *Path) {
402 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000403 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
404 e = RD->bases_end(); i != e; ++i) {
405 if (i->isVirtual())
406 continue;
407 const CXXRecordDecl *Base =
408 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
409 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
410 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
411 StartNewTable();
Mike Stump37dbe962009-10-15 02:04:03 +0000412 CurrentVBaseOffset = Offset;
413 GenerateVtableForBase(Base, MorallyVirtual, o, false, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000414 }
415 }
Mike Stump37dbe962009-10-15 02:04:03 +0000416 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000417 }
418
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000419// #define D(X) do { X; } while (0)
420#define D(X)
421
422 void insertVCalls(int InsertionPoint) {
423 llvm::Constant *e = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000424 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000425 D(VCalls.insert(VCalls.begin(), 673));
426 D(VCalls.push_back(672));
Mike Stump8bccbfd2009-10-15 09:30:16 +0000427 methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000428 // The vcalls come first...
429 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
430 e = VCalls.rend();
431 i != e; ++i)
432 methods[InsertionPoint++] = wrap((0?600:0) + *i);
433 VCalls.clear();
434 }
435
Mike Stump75ce5732009-10-31 20:06:59 +0000436 Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
437 const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual,
438 bool MorallyVirtual, int64_t Offset, bool ForVirtualBase,
439 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000440 bool alloc = false;
441 if (Path == 0) {
442 alloc = true;
443 Path = new Path_t;
444 }
445
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000446 StartNewTable();
447 extra = 0;
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000448 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
449 int VCallInsertionPoint = methods.size();
450 if (!DeferVCalls) {
451 insertVCalls(VCallInsertionPoint);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000452 } else
453 // FIXME: just for extra, or for all uses of VCalls.size post this?
454 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000455
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000456 methods.push_back(wrap(-(Offset/8)));
457 methods.push_back(rtti);
458 Index_t AddressPoint = methods.size();
459
460 InstallThunks();
Mike Stump75ce5732009-10-31 20:06:59 +0000461 D1(printf("============= combining methods\n"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000462 methods.insert(methods.end(), submethods.begin(), submethods.end());
463 submethods.clear();
464
465 // and then the non-virtual bases.
466 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stump37dbe962009-10-15 02:04:03 +0000467 MorallyVirtual, Offset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000468
469 if (ForVirtualBase) {
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000470 insertVCalls(VCallInsertionPoint);
471 AddressPoint += VCalls.size();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000472 }
473
Mike Stump37dbe962009-10-15 02:04:03 +0000474 if (alloc) {
475 delete Path;
476 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000477 return AddressPoint;
478 }
479
Mike Stump75ce5732009-10-31 20:06:59 +0000480 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
481 bool updateVBIndex, Index_t current_vbindex,
482 bool RDisVirtualBase) {
483 if (!RD->isDynamicClass())
484 return;
485
486 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
487 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
488 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
489
490 // vtables are composed from the chain of primaries.
491 if (PrimaryBase) {
492 D1(printf(" doing primaries for %s most derived %s\n",
493 RD->getNameAsCString(), Class->getNameAsCString()));
494
495 if (!PrimaryBaseWasVirtual)
496 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
497 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual);
498 }
499
500 D1(printf(" doing vcall entries for %s most derived %s\n",
501 RD->getNameAsCString(), Class->getNameAsCString()));
502
503 // And add the virtuals for the class to the primary vtable.
504 AddMethods(RD, MorallyVirtual, Offset, RDisVirtualBase);
505 }
506
507 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
508 bool updateVBIndex, Index_t current_vbindex,
509 bool RDisVirtualBase, bool bottom=false) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000510 if (!RD->isDynamicClass())
511 return;
512
513 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
514 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
515 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
516
517 // vtables are composed from the chain of primaries.
518 if (PrimaryBase) {
519 if (PrimaryBaseWasVirtual)
520 IndirectPrimary.insert(PrimaryBase);
Mike Stump75ce5732009-10-31 20:06:59 +0000521
522 D1(printf(" doing primaries for %s most derived %s\n",
523 RD->getNameAsCString(), Class->getNameAsCString()));
524
525 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
526 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000527 }
528
Mike Stump75ce5732009-10-31 20:06:59 +0000529 D1(printf(" doing vbase entries for %s most derived %s\n",
530 RD->getNameAsCString(), Class->getNameAsCString()));
531 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
532
533 if (RDisVirtualBase || bottom) {
534 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
535 RDisVirtualBase);
536 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000537 }
538
539 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
540 bool MorallyVirtual = false, int64_t Offset = 0,
541 bool ForVirtualBase = false,
Mike Stump37dbe962009-10-15 02:04:03 +0000542 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000543 if (!RD->isDynamicClass())
544 return 0;
545
546 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
547 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
548 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
549
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000550 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000551 D1(printf("building entries for base %s most derived %s\n",
552 RD->getNameAsCString(), Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000553
Mike Stump75ce5732009-10-31 20:06:59 +0000554 if (ForVirtualBase)
555 extra = VCalls.size();
556
557 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
558 true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000559
560 if (Path)
Mike Stump8bccbfd2009-10-15 09:30:16 +0000561 OverrideMethods(Path, MorallyVirtual, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000562
Mike Stump75ce5732009-10-31 20:06:59 +0000563 return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual,
564 Offset, ForVirtualBase, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000565 }
566
567 void GenerateVtableForVBases(const CXXRecordDecl *RD,
568 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000569 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000570 bool alloc = false;
571 if (Path == 0) {
572 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000573 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000574 }
575 // FIXME: We also need to override using all paths to a virtual base,
576 // right now, we just process the first path
577 Path->push_back(std::make_pair(RD, Offset));
578 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
579 e = RD->bases_end(); i != e; ++i) {
580 const CXXRecordDecl *Base =
581 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
582 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
583 // Mark it so we don't output it twice.
584 IndirectPrimary.insert(Base);
585 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000586 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000587 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump37dbe962009-10-15 02:04:03 +0000588 CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +0000589 D1(printf("vtable %s virtual base %s\n",
590 Class->getNameAsCString(), Base->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000591 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
592 }
593 int64_t BaseOffset = Offset;
594 if (i->isVirtual())
595 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump37dbe962009-10-15 02:04:03 +0000596 if (Base->getNumVBases()) {
597 CurrentVBaseOffset = BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000598 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000599 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000600 }
601 Path->pop_back();
602 if (alloc)
603 delete Path;
604 }
605};
606
607
608VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
609 CXXRecordDecl *B) {
610 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
611}
612
613int64_t CGVtableInfo::getMethodVtableIndex(const CXXMethodDecl *MD) {
614 MD = MD->getCanonicalDecl();
615
616 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(MD);
617 if (I != MethodVtableIndices.end())
618 return I->second;
619
620 const CXXRecordDecl *RD = MD->getParent();
621
622 std::vector<llvm::Constant *> methods;
623 // FIXME: This seems expensive. Can we do a partial job to get
624 // just this data.
625 VtableBuilder b(methods, RD, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +0000626 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000627 b.GenerateVtableForBase(RD);
628 b.GenerateVtableForVBases(RD);
629
630 MethodVtableIndices.insert(b.getIndex().begin(),
631 b.getIndex().end());
632
633 I = MethodVtableIndices.find(MD);
634 assert(I != MethodVtableIndices.end() && "Did not find index!");
635 return I->second;
636}
637
638int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
639 const CXXRecordDecl *VBase) {
640 ClassPairTy ClassPair(RD, VBase);
641
642 VirtualBaseClassIndiciesTy::iterator I =
643 VirtualBaseClassIndicies.find(ClassPair);
644 if (I != VirtualBaseClassIndicies.end())
645 return I->second;
646
647 std::vector<llvm::Constant *> methods;
648 // FIXME: This seems expensive. Can we do a partial job to get
649 // just this data.
650 VtableBuilder b(methods, RD, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +0000651 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000652 b.GenerateVtableForBase(RD);
653 b.GenerateVtableForVBases(RD);
654
655 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
656 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
657 // Insert all types.
658 ClassPairTy ClassPair(RD, I->first);
659
660 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
661 }
662
663 I = VirtualBaseClassIndicies.find(ClassPair);
664 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
665
666 return I->second;
667}
668
669llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
670 llvm::SmallString<256> OutName;
671 llvm::raw_svector_ostream Out(OutName);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000672 mangleCXXVtable(CGM.getMangleContext(), RD, Out);
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +0000673
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000674 llvm::GlobalVariable::LinkageTypes linktype;
Chandler Carruth6e0df532009-10-26 17:14:14 +0000675 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000676 std::vector<llvm::Constant *> methods;
677 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
678 int64_t AddressPoint;
679
680 VtableBuilder b(methods, RD, CGM);
681
Mike Stump75ce5732009-10-31 20:06:59 +0000682 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000683 // First comes the vtables for all the non-virtual bases...
684 AddressPoint = b.GenerateVtableForBase(RD);
685
686 // then the vtables for all the virtual bases.
687 b.GenerateVtableForVBases(RD);
688
689 llvm::Constant *C;
690 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
691 C = llvm::ConstantArray::get(type, methods);
692 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
693 linktype, C, Out.str());
694 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
695 vtable = Builder.CreateGEP(vtable,
696 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
697 AddressPoint*LLVMPointerWidth/8));
698 return vtable;
699}