blob: b12a48ad94bf01b804246d5d222058380925383c [file] [log] [blame]
Anders Carlssondbd920c2009-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;
Mike Stumpacfd1e52009-11-13 01:54:23 +000032 /// LayoutClass - The most derived class used for virtual base layout
33 /// information.
34 const CXXRecordDecl *LayoutClass;
Mike Stump4cde6262009-11-13 02:13:54 +000035 /// LayoutOffset - The offset for Class in LayoutClass.
36 uint64_t LayoutOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +000037 /// BLayout - Layout for the most derived class that this vtable is being
38 /// built for.
39 const ASTRecordLayout &BLayout;
40 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
41 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
42 llvm::Constant *rtti;
43 llvm::LLVMContext &VMContext;
44 CodeGenModule &CGM; // Per-module state.
45 /// Index - Maps a method decl into a vtable index. Useful for virtual
46 /// dispatch codegen.
47 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
48 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
49 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump9e7e3c62009-11-06 23:27:42 +000050 // This is the offset to the nearest virtual base
51 llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +000052 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump94aff932009-10-27 23:46:47 +000053
54 typedef llvm::DenseMap<const CXXMethodDecl *, int> Pures_t;
55 Pures_t Pures;
Anders Carlssondbd920c2009-10-11 22:13:54 +000056 typedef std::pair<Index_t, Index_t> CallOffset;
57 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
58 Thunks_t Thunks;
59 typedef llvm::DenseMap<const CXXMethodDecl *,
Mike Stumpd9878a12009-10-13 10:55:21 +000060 std::pair<std::pair<CallOffset, CallOffset>,
61 CanQualType> > CovariantThunks_t;
Anders Carlssondbd920c2009-10-11 22:13:54 +000062 CovariantThunks_t CovariantThunks;
63 std::vector<Index_t> VCalls;
Mike Stump9840c702009-11-12 20:47:57 +000064
65 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
66 // CtorVtable - Used to hold the AddressPoints (offsets) into the built vtable
67 // for use in computing the initializers for the VTT.
68 llvm::DenseMap<CtorVtable_t, int64_t> &AddressPoints;
69
Anders Carlssondbd920c2009-10-11 22:13:54 +000070 typedef CXXRecordDecl::method_iterator method_iter;
71 // FIXME: Linkage should follow vtable
72 const bool Extern;
73 const uint32_t LLVMPointerWidth;
74 Index_t extra;
Mike Stump11dea942009-10-15 02:04:03 +000075 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stump94aff932009-10-27 23:46:47 +000076 llvm::Constant *cxa_pure;
Anders Carlssondbd920c2009-10-11 22:13:54 +000077public:
Mike Stump4cde6262009-11-13 02:13:54 +000078 VtableBuilder(std::vector<llvm::Constant *> &meth, const CXXRecordDecl *c,
79 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm)
80 : methods(meth), Class(c), LayoutClass(l), LayoutOffset(lo),
Mike Stumpacfd1e52009-11-13 01:54:23 +000081 BLayout(cgm.getContext().getASTRecordLayout(l)),
Anders Carlssondbd920c2009-10-11 22:13:54 +000082 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump9840c702009-11-12 20:47:57 +000083 CGM(cgm), AddressPoints(*new llvm::DenseMap<CtorVtable_t, int64_t>),
84 Extern(true),
Mike Stump9e7e3c62009-11-06 23:27:42 +000085 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Anders Carlssondbd920c2009-10-11 22:13:54 +000086 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump7809e0d2009-10-28 00:35:46 +000087
88 // Calculate pointer for ___cxa_pure_virtual.
89 const llvm::FunctionType *FTy;
90 std::vector<const llvm::Type*> ArgTys;
91 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
92 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
93 cxa_pure = wrap(CGM.CreateRuntimeFunction(FTy, "__cxa_pure_virtual"));
Anders Carlssondbd920c2009-10-11 22:13:54 +000094 }
95
96 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
97 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
98 { return VBIndex; }
99
Mike Stump9840c702009-11-12 20:47:57 +0000100 llvm::DenseMap<CtorVtable_t, int64_t> *getAddressPoints()
101 { return &AddressPoints; }
102
Anders Carlssondbd920c2009-10-11 22:13:54 +0000103 llvm::Constant *wrap(Index_t i) {
104 llvm::Constant *m;
105 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
106 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
107 }
108
109 llvm::Constant *wrap(llvm::Constant *m) {
110 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
111 }
112
Mike Stump80ac2352009-11-12 23:36:21 +0000113//#define D1(x)
114#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump6a9612f2009-10-31 20:06:59 +0000115
116 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stumpab28c132009-10-13 22:54:56 +0000117 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000118 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
119 e = RD->bases_end(); i != e; ++i) {
120 const CXXRecordDecl *Base =
121 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stumpab28c132009-10-13 22:54:56 +0000122 Index_t next_vbindex = current_vbindex;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000123 if (i->isVirtual() && !SeenVBase.count(Base)) {
124 SeenVBase.insert(Base);
Mike Stumpab28c132009-10-13 22:54:56 +0000125 if (updateVBIndex) {
Mike Stump6a9612f2009-10-31 20:06:59 +0000126 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stumpab28c132009-10-13 22:54:56 +0000127 - 3*LLVMPointerWidth/8);
128 VBIndex[Base] = next_vbindex;
129 }
Mike Stump6a9612f2009-10-31 20:06:59 +0000130 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
131 VCalls.push_back((0?700:0) + BaseOffset);
132 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
133 Base->getNameAsCString(),
134 (int)-VCalls.size()-3, (int)BaseOffset,
135 Class->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000136 }
Mike Stumpab28c132009-10-13 22:54:56 +0000137 // We also record offsets for non-virtual bases to closest enclosing
138 // virtual base. We do this so that we don't have to search
139 // for the nearst virtual base class when generating thunks.
140 if (updateVBIndex && VBIndex.count(Base) == 0)
141 VBIndex[Base] = next_vbindex;
Mike Stump6a9612f2009-10-31 20:06:59 +0000142 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000143 }
144 }
145
146 void StartNewTable() {
147 SeenVBase.clear();
148 }
149
150 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
151
Mike Stump3425b972009-10-15 09:30:16 +0000152 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
153 Index_t Offset = 0) {
154
155 if (B == D)
156 return Offset;
157
158 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
159 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
160 e = D->bases_end(); i != e; ++i) {
161 const CXXRecordDecl *Base =
162 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
163 int64_t BaseOffset = 0;
164 if (!i->isVirtual())
165 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
166 int64_t o = getNVOffset_1(Base, B, BaseOffset);
167 if (o >= 0)
168 return o;
169 }
170
171 return -1;
172 }
173
174 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
175 /// derived class D.
176 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump9c212892009-11-03 19:03:17 +0000177 qD = qD->getPointeeType();
178 qB = qB->getPointeeType();
Mike Stump3425b972009-10-15 09:30:16 +0000179 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
180 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
181 int64_t o = getNVOffset_1(D, B);
182 if (o >= 0)
183 return o;
184
185 assert(false && "FIXME: non-virtual base not found");
186 return 0;
187 }
188
Anders Carlssondbd920c2009-10-11 22:13:54 +0000189 /// getVbaseOffset - Returns the index into the vtable for the virtual base
190 /// offset for the given (B) virtual base of the derived class D.
191 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump9c212892009-11-03 19:03:17 +0000192 qD = qD->getPointeeType();
193 qB = qB->getPointeeType();
Anders Carlssondbd920c2009-10-11 22:13:54 +0000194 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
195 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
196 if (D != Class)
197 return VBlookup(D, B);
198 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
199 i = VBIndex.find(B);
200 if (i != VBIndex.end())
201 return i->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000202
Mike Stumpab28c132009-10-13 22:54:56 +0000203 assert(false && "FIXME: Base not found");
Anders Carlssondbd920c2009-10-11 22:13:54 +0000204 return 0;
205 }
206
207 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stump3425b972009-10-15 09:30:16 +0000208 bool MorallyVirtual, Index_t OverrideOffset,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000209 Index_t Offset, int64_t CurrentVBaseOffset) {
Mike Stump7809e0d2009-10-28 00:35:46 +0000210 const bool isPure = MD->isPure();
Anders Carlssondbd920c2009-10-11 22:13:54 +0000211 typedef CXXMethodDecl::method_iterator meth_iter;
Mike Stump3425b972009-10-15 09:30:16 +0000212 // FIXME: Should OverrideOffset's be Offset?
Anders Carlssondbd920c2009-10-11 22:13:54 +0000213
214 // FIXME: Don't like the nested loops. For very large inheritance
215 // heirarchies we could have a table on the side with the final overridder
216 // and just replace each instance of an overridden method once. Would be
217 // nice to measure the cost/benefit on real code.
218
219 for (meth_iter mi = MD->begin_overridden_methods(),
220 e = MD->end_overridden_methods();
221 mi != e; ++mi) {
222 const CXXMethodDecl *OMD = *mi;
223 llvm::Constant *om;
224 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
225 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
226
227 for (Index_t i = 0, e = submethods.size();
228 i != e; ++i) {
229 // FIXME: begin_overridden_methods might be too lax, covariance */
230 if (submethods[i] != om)
231 continue;
232 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
233 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
234 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
235 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
236 CallOffset ReturnOffset = std::make_pair(0, 0);
237 if (oret != ret) {
238 // FIXME: calculate offsets for covariance
Mike Stumpd9878a12009-10-13 10:55:21 +0000239 if (CovariantThunks.count(OMD)) {
240 oret = CovariantThunks[OMD].second;
241 CovariantThunks.erase(OMD);
242 }
Mike Stump3425b972009-10-15 09:30:16 +0000243 // FIXME: Double check oret
244 Index_t nv = getNVOffset(oret, ret)/8;
Mike Stumpd9878a12009-10-13 10:55:21 +0000245 ReturnOffset = std::make_pair(nv, getVbaseOffset(oret, ret));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000246 }
247 Index[MD] = i;
248 submethods[i] = m;
Mike Stump7809e0d2009-10-28 00:35:46 +0000249 if (isPure)
250 Pures[MD] = 1;
251 Pures.erase(OMD);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000252 Thunks.erase(OMD);
Mike Stump9e7e3c62009-11-06 23:27:42 +0000253 if (MorallyVirtual || VCall.count(OMD)) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000254 Index_t &idx = VCall[OMD];
255 if (idx == 0) {
Mike Stump9e7e3c62009-11-06 23:27:42 +0000256 NonVirtualOffset[MD] = -OverrideOffset/8 + CurrentVBaseOffset/8;
Mike Stump3425b972009-10-15 09:30:16 +0000257 VCallOffset[MD] = OverrideOffset/8;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000258 idx = VCalls.size()+1;
259 VCalls.push_back(0);
Mike Stump6a9612f2009-10-31 20:06:59 +0000260 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
Mike Stump33c530e2009-11-06 02:38:24 +0000261 MD->getNameAsCString(), (int)-idx-3, (int)VCalls[idx-1],
Mike Stump6a9612f2009-10-31 20:06:59 +0000262 Class->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000263 } else {
Mike Stump9e7e3c62009-11-06 23:27:42 +0000264 NonVirtualOffset[MD] = NonVirtualOffset[OMD];
Anders Carlssondbd920c2009-10-11 22:13:54 +0000265 VCallOffset[MD] = VCallOffset[OMD];
Mike Stump3425b972009-10-15 09:30:16 +0000266 VCalls[idx-1] = -VCallOffset[OMD] + OverrideOffset/8;
Mike Stump6a9612f2009-10-31 20:06:59 +0000267 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
Mike Stump33c530e2009-11-06 02:38:24 +0000268 MD->getNameAsCString(), (int)-idx-3, (int)VCalls[idx-1],
Mike Stump6a9612f2009-10-31 20:06:59 +0000269 Class->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000270 }
271 VCall[MD] = idx;
Mike Stump9e7e3c62009-11-06 23:27:42 +0000272 int64_t O = NonVirtualOffset[MD];
273 int v = -((idx+extra+2)*LLVMPointerWidth/8);
274 // Optimize out virtual adjustments of 0.
275 if (VCalls[idx-1] == 0)
276 v = 0;
277 CallOffset ThisOffset = std::make_pair(O, v);
Mike Stump11dea942009-10-15 02:04:03 +0000278 // FIXME: Do we always have to build a covariant thunk to save oret,
279 // which is the containing virtual base class?
Anders Carlssondbd920c2009-10-11 22:13:54 +0000280 if (ReturnOffset.first || ReturnOffset.second)
Mike Stumpd9878a12009-10-13 10:55:21 +0000281 CovariantThunks[MD] = std::make_pair(std::make_pair(ThisOffset,
282 ReturnOffset),
283 oret);
Mike Stump9e7e3c62009-11-06 23:27:42 +0000284 else if (!isPure && (ThisOffset.first || ThisOffset.second))
Anders Carlssondbd920c2009-10-11 22:13:54 +0000285 Thunks[MD] = ThisOffset;
286 return true;
287 }
Mike Stumpab28c132009-10-13 22:54:56 +0000288
Anders Carlssondbd920c2009-10-11 22:13:54 +0000289 // FIXME: finish off
Mike Stump3425b972009-10-15 09:30:16 +0000290 int64_t O = VCallOffset[OMD] - OverrideOffset/8;
Mike Stump33c530e2009-11-06 02:38:24 +0000291
Mike Stumpab28c132009-10-13 22:54:56 +0000292 if (O || ReturnOffset.first || ReturnOffset.second) {
293 CallOffset ThisOffset = std::make_pair(O, 0);
294
295 if (ReturnOffset.first || ReturnOffset.second)
296 CovariantThunks[MD] = std::make_pair(std::make_pair(ThisOffset,
297 ReturnOffset),
298 oret);
Mike Stump7809e0d2009-10-28 00:35:46 +0000299 else if (!isPure)
Mike Stumpab28c132009-10-13 22:54:56 +0000300 Thunks[MD] = ThisOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000301 }
Anders Carlssondbd920c2009-10-11 22:13:54 +0000302 return true;
303 }
304 }
305
306 return false;
307 }
308
309 void InstallThunks() {
310 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
311 i != e; ++i) {
312 const CXXMethodDecl *MD = i->first;
Mike Stump7809e0d2009-10-28 00:35:46 +0000313 assert(!MD->isPure() && "Trying to thunk a pure");
Anders Carlssondbd920c2009-10-11 22:13:54 +0000314 Index_t idx = Index[MD];
315 Index_t nv_O = i->second.first;
316 Index_t v_O = i->second.second;
317 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
318 }
319 Thunks.clear();
320 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
321 e = CovariantThunks.end();
322 i != e; ++i) {
323 const CXXMethodDecl *MD = i->first;
Mike Stump7809e0d2009-10-28 00:35:46 +0000324 if (MD->isPure())
325 continue;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000326 Index_t idx = Index[MD];
Mike Stumpd9878a12009-10-13 10:55:21 +0000327 Index_t nv_t = i->second.first.first.first;
328 Index_t v_t = i->second.first.first.second;
329 Index_t nv_r = i->second.first.second.first;
330 Index_t v_r = i->second.first.second.second;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000331 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
332 v_r);
333 }
334 CovariantThunks.clear();
Mike Stump94aff932009-10-27 23:46:47 +0000335 for (Pures_t::iterator i = Pures.begin(), e = Pures.end();
336 i != e; ++i) {
337 const CXXMethodDecl *MD = i->first;
338 Index_t idx = Index[MD];
339 submethods[idx] = cxa_pure;
340 }
341 Pures.clear();
Anders Carlssondbd920c2009-10-11 22:13:54 +0000342 }
343
Mike Stump1ae31782009-10-27 23:36:26 +0000344 llvm::Constant *WrapAddrOf(const CXXMethodDecl *MD) {
345 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
346 return wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
347
348 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
349 const llvm::Type *Ty =
350 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
351 FPT->isVariadic());
352
353 return wrap(CGM.GetAddrOfFunction(MD, Ty));
354 }
355
Mike Stump9e7e3c62009-11-06 23:27:42 +0000356 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
357 int64_t CurrentVBaseOffset) {
Mike Stump11dea942009-10-15 02:04:03 +0000358 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlssondbd920c2009-10-11 22:13:54 +0000359 e = Path->rend(); i != e; ++i) {
360 const CXXRecordDecl *RD = i->first;
Mike Stump3425b972009-10-15 09:30:16 +0000361 int64_t OverrideOffset = i->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000362 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
363 ++mi) {
364 if (!mi->isVirtual())
365 continue;
366
367 const CXXMethodDecl *MD = *mi;
Mike Stump1ae31782009-10-27 23:36:26 +0000368 llvm::Constant *m = WrapAddrOf(MD);
Mike Stump9e7e3c62009-11-06 23:27:42 +0000369 OverrideMethod(MD, m, MorallyVirtual, OverrideOffset, Offset,
370 CurrentVBaseOffset);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000371 }
372 }
373 }
374
Mike Stump6a9612f2009-10-31 20:06:59 +0000375 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000376 bool ForVirtualBase, int64_t CurrentVBaseOffset) {
Mike Stump1ae31782009-10-27 23:36:26 +0000377 llvm::Constant *m = WrapAddrOf(MD);
378
Anders Carlssondbd920c2009-10-11 22:13:54 +0000379 // If we can find a previously allocated slot for this, reuse it.
Mike Stump9e7e3c62009-11-06 23:27:42 +0000380 if (OverrideMethod(MD, m, MorallyVirtual, Offset, Offset,
381 CurrentVBaseOffset))
Anders Carlssondbd920c2009-10-11 22:13:54 +0000382 return;
383
384 // else allocate a new slot.
385 Index[MD] = submethods.size();
386 submethods.push_back(m);
Mike Stump6a9612f2009-10-31 20:06:59 +0000387 D1(printf(" vfn for %s at %d\n", MD->getNameAsCString(), (int)Index[MD]));
Mike Stump7809e0d2009-10-28 00:35:46 +0000388 if (MD->isPure())
389 Pures[MD] = 1;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000390 if (MorallyVirtual) {
391 VCallOffset[MD] = Offset/8;
392 Index_t &idx = VCall[MD];
393 // Allocate the first one, after that, we reuse the previous one.
394 if (idx == 0) {
Mike Stump9e7e3c62009-11-06 23:27:42 +0000395 NonVirtualOffset[MD] = CurrentVBaseOffset/8 - Offset/8;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000396 idx = VCalls.size()+1;
397 VCalls.push_back(0);
Mike Stump6a9612f2009-10-31 20:06:59 +0000398 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stump33c530e2009-11-06 02:38:24 +0000399 MD->getNameAsCString(), (int)-VCalls.size()-3, 0));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000400 }
401 }
402 }
403
404 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000405 Index_t Offset, bool RDisVirtualBase,
406 int64_t CurrentVBaseOffset) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000407 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
408 ++mi)
409 if (mi->isVirtual())
Mike Stump9e7e3c62009-11-06 23:27:42 +0000410 AddMethod(*mi, MorallyVirtual, Offset, RDisVirtualBase,
411 CurrentVBaseOffset);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000412 }
413
414 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
415 const CXXRecordDecl *PrimaryBase,
416 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000417 int64_t Offset, int64_t CurrentVBaseOffset,
418 Path_t *Path) {
Mike Stump11dea942009-10-15 02:04:03 +0000419 Path->push_back(std::make_pair(RD, Offset));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000420 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
421 e = RD->bases_end(); i != e; ++i) {
422 if (i->isVirtual())
423 continue;
424 const CXXRecordDecl *Base =
425 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
426 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
427 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
428 StartNewTable();
Mike Stump4cde6262009-11-13 02:13:54 +0000429 GenerateVtableForBase(Base, o, MorallyVirtual, false,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000430 CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000431 }
432 }
Mike Stump11dea942009-10-15 02:04:03 +0000433 Path->pop_back();
Anders Carlssondbd920c2009-10-11 22:13:54 +0000434 }
435
Mike Stump0ca42792009-10-14 18:14:51 +0000436// #define D(X) do { X; } while (0)
437#define D(X)
438
439 void insertVCalls(int InsertionPoint) {
440 llvm::Constant *e = 0;
Mike Stump6a9612f2009-10-31 20:06:59 +0000441 D1(printf("============= combining vbase/vcall\n"));
Mike Stump0ca42792009-10-14 18:14:51 +0000442 D(VCalls.insert(VCalls.begin(), 673));
443 D(VCalls.push_back(672));
Mike Stump3425b972009-10-15 09:30:16 +0000444 methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
Mike Stump0ca42792009-10-14 18:14:51 +0000445 // The vcalls come first...
446 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
447 e = VCalls.rend();
448 i != e; ++i)
449 methods[InsertionPoint++] = wrap((0?600:0) + *i);
450 VCalls.clear();
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000451 VCall.clear();
Mike Stump0ca42792009-10-14 18:14:51 +0000452 }
453
Mike Stump6a9612f2009-10-31 20:06:59 +0000454 Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
455 const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual,
456 bool MorallyVirtual, int64_t Offset, bool ForVirtualBase,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000457 int64_t CurrentVBaseOffset,
Mike Stump6a9612f2009-10-31 20:06:59 +0000458 Path_t *Path) {
Mike Stump11dea942009-10-15 02:04:03 +0000459 bool alloc = false;
460 if (Path == 0) {
461 alloc = true;
462 Path = new Path_t;
463 }
464
Anders Carlssondbd920c2009-10-11 22:13:54 +0000465 StartNewTable();
466 extra = 0;
Mike Stump0ca42792009-10-14 18:14:51 +0000467 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
468 int VCallInsertionPoint = methods.size();
469 if (!DeferVCalls) {
470 insertVCalls(VCallInsertionPoint);
Mike Stump3425b972009-10-15 09:30:16 +0000471 } else
472 // FIXME: just for extra, or for all uses of VCalls.size post this?
473 extra = -VCalls.size();
Anders Carlssondbd920c2009-10-11 22:13:54 +0000474
Mike Stump4cde6262009-11-13 02:13:54 +0000475 methods.push_back(wrap(-((Offset-LayoutOffset)/8)));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000476 methods.push_back(rtti);
477 Index_t AddressPoint = methods.size();
478
479 InstallThunks();
Mike Stump6a9612f2009-10-31 20:06:59 +0000480 D1(printf("============= combining methods\n"));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000481 methods.insert(methods.end(), submethods.begin(), submethods.end());
482 submethods.clear();
483
484 // and then the non-virtual bases.
485 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000486 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stump0ca42792009-10-14 18:14:51 +0000487
488 if (ForVirtualBase) {
Mike Stump9840c702009-11-12 20:47:57 +0000489 // FIXME: We're adding to VCalls in callers, we need to do the overrides
490 // in the inner part, so that we know the complete set of vcalls during
491 // the build and don't have to insert into methods. Saving out the
492 // AddressPoint here, would need to be fixed, if we didn't do that. Also
493 // retroactively adding vcalls for overrides later wind up in the wrong
494 // place, the vcall slot has to be alloted during the walk of the base
495 // when the function is first introduces.
Mike Stump0ca42792009-10-14 18:14:51 +0000496 AddressPoint += VCalls.size();
Mike Stump9840c702009-11-12 20:47:57 +0000497 insertVCalls(VCallInsertionPoint);
Mike Stump0ca42792009-10-14 18:14:51 +0000498 }
499
Mike Stumpacfd1e52009-11-13 01:54:23 +0000500 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
501 RD->getNameAsCString(), Class->getNameAsCString(),
502 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
503 AddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump9840c702009-11-12 20:47:57 +0000504
Mike Stump11dea942009-10-15 02:04:03 +0000505 if (alloc) {
506 delete Path;
507 }
Anders Carlssondbd920c2009-10-11 22:13:54 +0000508 return AddressPoint;
509 }
510
Mike Stump6a9612f2009-10-31 20:06:59 +0000511 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
512 bool updateVBIndex, Index_t current_vbindex,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000513 bool RDisVirtualBase, int64_t CurrentVBaseOffset) {
Mike Stump6a9612f2009-10-31 20:06:59 +0000514 if (!RD->isDynamicClass())
515 return;
516
517 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
518 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
519 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
520
521 // vtables are composed from the chain of primaries.
522 if (PrimaryBase) {
523 D1(printf(" doing primaries for %s most derived %s\n",
524 RD->getNameAsCString(), Class->getNameAsCString()));
525
Mike Stump9e7e3c62009-11-06 23:27:42 +0000526 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
527 if (PrimaryBaseWasVirtual)
528 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
529
Mike Stump6a9612f2009-10-31 20:06:59 +0000530 if (!PrimaryBaseWasVirtual)
531 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000532 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
533 BaseCurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +0000534 }
535
536 D1(printf(" doing vcall entries for %s most derived %s\n",
537 RD->getNameAsCString(), Class->getNameAsCString()));
538
539 // And add the virtuals for the class to the primary vtable.
Mike Stump9e7e3c62009-11-06 23:27:42 +0000540 AddMethods(RD, MorallyVirtual, Offset, RDisVirtualBase, CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +0000541 }
542
543 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
544 bool updateVBIndex, Index_t current_vbindex,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000545 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
546 bool bottom=false) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000547 if (!RD->isDynamicClass())
548 return;
549
550 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
551 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
552 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
553
554 // vtables are composed from the chain of primaries.
555 if (PrimaryBase) {
Mike Stump9e7e3c62009-11-06 23:27:42 +0000556 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
557 if (PrimaryBaseWasVirtual) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000558 IndirectPrimary.insert(PrimaryBase);
Mike Stump9e7e3c62009-11-06 23:27:42 +0000559 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
560 }
Mike Stump6a9612f2009-10-31 20:06:59 +0000561
562 D1(printf(" doing primaries for %s most derived %s\n",
563 RD->getNameAsCString(), Class->getNameAsCString()));
564
565 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000566 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
567 BaseCurrentVBaseOffset);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000568 }
569
Mike Stump6a9612f2009-10-31 20:06:59 +0000570 D1(printf(" doing vbase entries for %s most derived %s\n",
571 RD->getNameAsCString(), Class->getNameAsCString()));
572 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
573
574 if (RDisVirtualBase || bottom) {
575 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000576 RDisVirtualBase, CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +0000577 }
Anders Carlssondbd920c2009-10-11 22:13:54 +0000578 }
579
Mike Stump4cde6262009-11-13 02:13:54 +0000580 int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
581 bool MorallyVirtual = false,
Anders Carlssondbd920c2009-10-11 22:13:54 +0000582 bool ForVirtualBase = false,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000583 int CurrentVBaseOffset = 0,
Mike Stump11dea942009-10-15 02:04:03 +0000584 Path_t *Path = 0) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000585 if (!RD->isDynamicClass())
586 return 0;
587
Mike Stump92774d12009-11-13 02:35:38 +0000588 // Construction vtable don't need parts that have no virtual bases and
589 // aren't morally virtual.
590 if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual)
591 return 0;
592
Anders Carlssondbd920c2009-10-11 22:13:54 +0000593 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
594 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
595 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
596
Anders Carlssondbd920c2009-10-11 22:13:54 +0000597 extra = 0;
Mike Stump6a9612f2009-10-31 20:06:59 +0000598 D1(printf("building entries for base %s most derived %s\n",
599 RD->getNameAsCString(), Class->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000600
Mike Stump6a9612f2009-10-31 20:06:59 +0000601 if (ForVirtualBase)
602 extra = VCalls.size();
603
604 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000605 CurrentVBaseOffset, true);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000606
607 if (Path)
Mike Stump9e7e3c62009-11-06 23:27:42 +0000608 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000609
Mike Stump6a9612f2009-10-31 20:06:59 +0000610 return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000611 Offset, ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000612 }
613
614 void GenerateVtableForVBases(const CXXRecordDecl *RD,
615 int64_t Offset = 0,
Mike Stump11dea942009-10-15 02:04:03 +0000616 Path_t *Path = 0) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000617 bool alloc = false;
618 if (Path == 0) {
619 alloc = true;
Mike Stump11dea942009-10-15 02:04:03 +0000620 Path = new Path_t;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000621 }
622 // FIXME: We also need to override using all paths to a virtual base,
623 // right now, we just process the first path
624 Path->push_back(std::make_pair(RD, Offset));
625 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
626 e = RD->bases_end(); i != e; ++i) {
627 const CXXRecordDecl *Base =
628 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
629 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
630 // Mark it so we don't output it twice.
631 IndirectPrimary.insert(Base);
632 StartNewTable();
Mike Stump0ca42792009-10-14 18:14:51 +0000633 VCall.clear();
Anders Carlssondbd920c2009-10-11 22:13:54 +0000634 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump9e7e3c62009-11-06 23:27:42 +0000635 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump6a9612f2009-10-31 20:06:59 +0000636 D1(printf("vtable %s virtual base %s\n",
637 Class->getNameAsCString(), Base->getNameAsCString()));
Mike Stump4cde6262009-11-13 02:13:54 +0000638 GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset,
Mike Stump9e7e3c62009-11-06 23:27:42 +0000639 Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000640 }
Mike Stump9840c702009-11-12 20:47:57 +0000641 int64_t BaseOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000642 if (i->isVirtual())
643 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump9840c702009-11-12 20:47:57 +0000644 else {
645 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
646 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
647 }
648
Mike Stump11dea942009-10-15 02:04:03 +0000649 if (Base->getNumVBases()) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000650 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump11dea942009-10-15 02:04:03 +0000651 }
Anders Carlssondbd920c2009-10-11 22:13:54 +0000652 }
653 Path->pop_back();
654 if (alloc)
655 delete Path;
656 }
657};
658
659
660VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
661 CXXRecordDecl *B) {
662 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
663}
664
665int64_t CGVtableInfo::getMethodVtableIndex(const CXXMethodDecl *MD) {
666 MD = MD->getCanonicalDecl();
667
668 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(MD);
669 if (I != MethodVtableIndices.end())
670 return I->second;
671
672 const CXXRecordDecl *RD = MD->getParent();
673
674 std::vector<llvm::Constant *> methods;
675 // FIXME: This seems expensive. Can we do a partial job to get
676 // just this data.
Mike Stump4cde6262009-11-13 02:13:54 +0000677 VtableBuilder b(methods, RD, RD, 0, CGM);
Mike Stump6a9612f2009-10-31 20:06:59 +0000678 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000679 b.GenerateVtableForBase(RD);
680 b.GenerateVtableForVBases(RD);
681
682 MethodVtableIndices.insert(b.getIndex().begin(),
683 b.getIndex().end());
684
685 I = MethodVtableIndices.find(MD);
686 assert(I != MethodVtableIndices.end() && "Did not find index!");
687 return I->second;
688}
689
690int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
691 const CXXRecordDecl *VBase) {
692 ClassPairTy ClassPair(RD, VBase);
693
694 VirtualBaseClassIndiciesTy::iterator I =
695 VirtualBaseClassIndicies.find(ClassPair);
696 if (I != VirtualBaseClassIndicies.end())
697 return I->second;
698
699 std::vector<llvm::Constant *> methods;
700 // FIXME: This seems expensive. Can we do a partial job to get
701 // just this data.
Mike Stump4cde6262009-11-13 02:13:54 +0000702 VtableBuilder b(methods, RD, RD, 0, CGM);
Mike Stump6a9612f2009-10-31 20:06:59 +0000703 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000704 b.GenerateVtableForBase(RD);
705 b.GenerateVtableForVBases(RD);
706
707 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
708 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
709 // Insert all types.
710 ClassPairTy ClassPair(RD, I->first);
711
712 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
713 }
714
715 I = VirtualBaseClassIndicies.find(ClassPair);
716 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
717
718 return I->second;
719}
720
Mike Stump9840c702009-11-12 20:47:57 +0000721llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass,
722 const CXXRecordDecl *RD,
Mike Stump8cfcb522009-11-11 20:26:26 +0000723 uint64_t Offset) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000724 llvm::SmallString<256> OutName;
725 llvm::raw_svector_ostream Out(OutName);
Mike Stump9840c702009-11-12 20:47:57 +0000726 if (LayoutClass != RD)
727 mangleCXXCtorVtable(getMangleContext(), LayoutClass, Offset/8, RD, Out);
Mike Stump8cfcb522009-11-11 20:26:26 +0000728 else
729 mangleCXXVtable(getMangleContext(), RD, Out);
Benjamin Kramer7a9474e2009-10-11 22:57:54 +0000730
Anders Carlssondbd920c2009-10-11 22:13:54 +0000731 llvm::GlobalVariable::LinkageTypes linktype;
Chandler Carruth6ade6212009-10-26 17:14:14 +0000732 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlssondbd920c2009-10-11 22:13:54 +0000733 std::vector<llvm::Constant *> methods;
734 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
735 int64_t AddressPoint;
736
Mike Stump4cde6262009-11-13 02:13:54 +0000737 VtableBuilder b(methods, RD, LayoutClass, Offset, *this);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000738
Mike Stump6a9612f2009-10-31 20:06:59 +0000739 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +0000740 // First comes the vtables for all the non-virtual bases...
Mike Stump4cde6262009-11-13 02:13:54 +0000741 AddressPoint = b.GenerateVtableForBase(RD, Offset);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000742
743 // then the vtables for all the virtual bases.
Mike Stump4cde6262009-11-13 02:13:54 +0000744 b.GenerateVtableForVBases(RD, Offset);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000745
Mike Stumpacfd1e52009-11-13 01:54:23 +0000746 CodeGenModule::AddrMap_t *&ref = AddressPoints[LayoutClass];
747 if (ref == 0)
748 ref = new CodeGenModule::AddrMap_t;
749
750 (*ref)[RD] = b.getAddressPoints();
Mike Stump9840c702009-11-12 20:47:57 +0000751
Anders Carlssondbd920c2009-10-11 22:13:54 +0000752 llvm::Constant *C;
753 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
754 C = llvm::ConstantArray::get(type, methods);
Mike Stump380dd752009-11-10 07:44:33 +0000755 llvm::Constant *vtable = new llvm::GlobalVariable(getModule(), type,
756 true, linktype, C,
757 Out.str());
758 vtable = llvm::ConstantExpr::getBitCast(vtable, Ptr8Ty);
759 llvm::Constant *AddressPointC;
760 uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0);
761 AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
762 AddressPoint*LLVMPointerWidth/8);
Mike Stump9840c702009-11-12 20:47:57 +0000763 vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC,
764 1);
Mike Stump380dd752009-11-10 07:44:33 +0000765
Anders Carlssondbd920c2009-10-11 22:13:54 +0000766 return vtable;
767}
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000768
769class VTTBuilder {
770 /// Inits - The list of values built for the VTT.
771 std::vector<llvm::Constant *> &Inits;
772 /// Class - The most derived class that this vtable is being built for.
773 const CXXRecordDecl *Class;
774 CodeGenModule &CGM; // Per-module state.
Mike Stump971977f2009-11-11 00:35:07 +0000775 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stumpaee8de32009-11-11 03:08:24 +0000776 /// BLayout - Layout for the most derived class that this vtable is being
777 /// built for.
778 const ASTRecordLayout &BLayout;
Mike Stumpacfd1e52009-11-13 01:54:23 +0000779 CodeGenModule::AddrMap_t &AddressPoints;
Mike Stump9840c702009-11-12 20:47:57 +0000780 // vtbl - A pointer to the vtable for Class.
781 llvm::Constant *ClassVtbl;
782 llvm::LLVMContext &VMContext;
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000783
Mike Stump28f7ce12009-11-12 22:56:32 +0000784 /// BuildVtablePtr - Build up a referene to the given secondary vtable
Mike Stumpacfd1e52009-11-13 01:54:23 +0000785 llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl,
786 const CXXRecordDecl *VtblClass,
787 const CXXRecordDecl *RD,
Mike Stump28f7ce12009-11-12 22:56:32 +0000788 uint64_t Offset) {
789 int64_t AddressPoint;
Mike Stumpacfd1e52009-11-13 01:54:23 +0000790 AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)];
Mike Stump80ac2352009-11-12 23:36:21 +0000791 // FIXME: We can never have 0 address point. Do this for now so gepping
792 // retains the same structure.
793 if (AddressPoint == 0)
794 AddressPoint = 1;
Mike Stumpacfd1e52009-11-13 01:54:23 +0000795 D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n",
796 RD->getNameAsCString(), VtblClass->getNameAsCString(),
797 Class->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump28f7ce12009-11-12 22:56:32 +0000798 uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0);
799 llvm::Constant *init;
800 init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
801 AddressPoint*LLVMPointerWidth/8);
802 init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1);
803 return init;
804 }
805
Mike Stump9840c702009-11-12 20:47:57 +0000806 /// Secondary - Add the secondary vtable pointers to Inits. Offset is the
807 /// current offset in bits to the object we're working on.
Mike Stump28f7ce12009-11-12 22:56:32 +0000808 void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl,
Mike Stumpacfd1e52009-11-13 01:54:23 +0000809 const CXXRecordDecl *VtblClass, uint64_t Offset=0,
810 bool MorallyVirtual=false) {
Mike Stump971977f2009-11-11 00:35:07 +0000811 if (RD->getNumVBases() == 0 && ! MorallyVirtual)
812 return;
813
814 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
815 e = RD->bases_end(); i != e; ++i) {
816 const CXXRecordDecl *Base =
817 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
818 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
819 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
820 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
821 bool NonVirtualPrimaryBase;
822 NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
823 bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
Mike Stumpaee8de32009-11-11 03:08:24 +0000824 uint64_t BaseOffset;
825 if (!i->isVirtual()) {
826 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
827 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
828 } else
829 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump80ac2352009-11-12 23:36:21 +0000830 llvm::Constant *subvtbl = vtbl;
Mike Stumpacfd1e52009-11-13 01:54:23 +0000831 const CXXRecordDecl *subVtblClass = VtblClass;
Mike Stump971977f2009-11-11 00:35:07 +0000832 if ((Base->getNumVBases() || BaseMorallyVirtual)
833 && !NonVirtualPrimaryBase) {
834 // FIXME: Slightly too many of these for __ZTT8test8_B2
Mike Stump28f7ce12009-11-12 22:56:32 +0000835 llvm::Constant *init;
Mike Stump80ac2352009-11-12 23:36:21 +0000836 if (BaseMorallyVirtual)
Mike Stumpacfd1e52009-11-13 01:54:23 +0000837 init = BuildVtablePtr(vtbl, VtblClass, RD, Offset);
Mike Stump80ac2352009-11-12 23:36:21 +0000838 else {
Mike Stump28f7ce12009-11-12 22:56:32 +0000839 init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset);
Mike Stump80ac2352009-11-12 23:36:21 +0000840 subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stumpacfd1e52009-11-13 01:54:23 +0000841 subVtblClass = Base;
Mike Stump80ac2352009-11-12 23:36:21 +0000842 }
Mike Stump28f7ce12009-11-12 22:56:32 +0000843 Inits.push_back(init);
Mike Stump971977f2009-11-11 00:35:07 +0000844 }
Mike Stumpacfd1e52009-11-13 01:54:23 +0000845 Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual);
Mike Stump971977f2009-11-11 00:35:07 +0000846 }
847 }
848
Mike Stump9840c702009-11-12 20:47:57 +0000849 /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the
850 /// currnet object we're working on.
851 void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) {
Mike Stump971977f2009-11-11 00:35:07 +0000852 if (RD->getNumVBases() == 0 && !MorallyVirtual)
853 return;
854
Mike Stump9840c702009-11-12 20:47:57 +0000855 llvm::Constant *init;
Mike Stumpacfd1e52009-11-13 01:54:23 +0000856 const CXXRecordDecl *VtblClass;
857
Mike Stump971977f2009-11-11 00:35:07 +0000858 // First comes the primary virtual table pointer...
Mike Stumpacfd1e52009-11-13 01:54:23 +0000859 if (MorallyVirtual) {
860 init = BuildVtablePtr(ClassVtbl, Class, RD, Offset);
861 VtblClass = Class;
862 } else {
Mike Stump9840c702009-11-12 20:47:57 +0000863 init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset);
Mike Stumpacfd1e52009-11-13 01:54:23 +0000864 VtblClass = RD;
865 }
Mike Stump28f7ce12009-11-12 22:56:32 +0000866 llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump9840c702009-11-12 20:47:57 +0000867 Inits.push_back(init);
Mike Stump971977f2009-11-11 00:35:07 +0000868
869 // then the secondary VTTs....
Mike Stump9840c702009-11-12 20:47:57 +0000870 SecondaryVTTs(RD, Offset, MorallyVirtual);
Mike Stump971977f2009-11-11 00:35:07 +0000871
872 // and last the secondary vtable pointers.
Mike Stumpacfd1e52009-11-13 01:54:23 +0000873 Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual);
Mike Stump971977f2009-11-11 00:35:07 +0000874 }
875
876 /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
877 /// built from each direct non-virtual proper base that requires a VTT in
878 /// declaration order.
Mike Stump9840c702009-11-12 20:47:57 +0000879 void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0,
880 bool MorallyVirtual=false) {
Mike Stump971977f2009-11-11 00:35:07 +0000881 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
882 e = RD->bases_end(); i != e; ++i) {
883 const CXXRecordDecl *Base =
884 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
885 if (i->isVirtual())
886 continue;
Mike Stump9840c702009-11-12 20:47:57 +0000887 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
888 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base);
889 BuildVTT(Base, BaseOffset, MorallyVirtual);
Mike Stump971977f2009-11-11 00:35:07 +0000890 }
891 }
892
893 /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
894 /// graph preorder.
895 void VirtualVTTs(const CXXRecordDecl *RD) {
896 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
897 e = RD->bases_end(); i != e; ++i) {
898 const CXXRecordDecl *Base =
899 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
900 if (i->isVirtual() && !SeenVBase.count(Base)) {
901 SeenVBase.insert(Base);
Mike Stump9840c702009-11-12 20:47:57 +0000902 uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
903 BuildVTT(Base, BaseOffset, true);
Mike Stump971977f2009-11-11 00:35:07 +0000904 }
905 VirtualVTTs(Base);
906 }
907 }
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000908public:
909 VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
Mike Stumpaee8de32009-11-11 03:08:24 +0000910 CodeGenModule &cgm)
911 : Inits(inits), Class(c), CGM(cgm),
Mike Stump9840c702009-11-12 20:47:57 +0000912 BLayout(cgm.getContext().getASTRecordLayout(c)),
Mike Stumpacfd1e52009-11-13 01:54:23 +0000913 AddressPoints(*cgm.AddressPoints[c]),
Mike Stump9840c702009-11-12 20:47:57 +0000914 VMContext(cgm.getModule().getContext()) {
Mike Stump380dd752009-11-10 07:44:33 +0000915
Mike Stump971977f2009-11-11 00:35:07 +0000916 // First comes the primary virtual table pointer for the complete class...
Mike Stump9840c702009-11-12 20:47:57 +0000917 ClassVtbl = CGM.getVtableInfo().getVtable(Class);
918 Inits.push_back(ClassVtbl);
919 ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0));
920
Mike Stump971977f2009-11-11 00:35:07 +0000921 // then the secondary VTTs...
922 SecondaryVTTs(Class);
923
924 // then the secondary vtable pointers...
Mike Stumpacfd1e52009-11-13 01:54:23 +0000925 Secondary(Class, ClassVtbl, Class);
Mike Stump971977f2009-11-11 00:35:07 +0000926
927 // and last, the virtual VTTs.
928 VirtualVTTs(Class);
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000929 }
930};
931
Mike Stump380dd752009-11-10 07:44:33 +0000932llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) {
Mike Stumpf1c03332009-11-10 19:13:04 +0000933 // Only classes that have virtual bases need a VTT.
934 if (RD->getNumVBases() == 0)
935 return 0;
936
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000937 llvm::SmallString<256> OutName;
938 llvm::raw_svector_ostream Out(OutName);
Mike Stump380dd752009-11-10 07:44:33 +0000939 mangleCXXVTT(getMangleContext(), RD, Out);
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000940
941 llvm::GlobalVariable::LinkageTypes linktype;
942 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
943 std::vector<llvm::Constant *> inits;
944 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
945
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000946 D1(printf("vtt %s\n", RD->getNameAsCString()));
947
Mike Stump971977f2009-11-11 00:35:07 +0000948 VTTBuilder b(inits, RD, *this);
949
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000950 llvm::Constant *C;
951 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size());
952 C = llvm::ConstantArray::get(type, inits);
Mike Stump380dd752009-11-10 07:44:33 +0000953 llvm::Constant *vtt = new llvm::GlobalVariable(getModule(), type, true,
954 linktype, C, Out.str());
955 vtt = llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty);
Mike Stumpfbfb52d2009-11-10 02:30:51 +0000956 return vtt;
957}
Mike Stump380dd752009-11-10 07:44:33 +0000958
Mike Stump8cfcb522009-11-11 20:26:26 +0000959llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Mike Stump380dd752009-11-10 07:44:33 +0000960 llvm::Constant *&vtbl = Vtables[RD];
961 if (vtbl)
962 return vtbl;
Mike Stump9840c702009-11-12 20:47:57 +0000963 vtbl = CGM.GenerateVtable(RD, RD);
Mike Stump380dd752009-11-10 07:44:33 +0000964 CGM.GenerateVTT(RD);
965 return vtbl;
966}
Mike Stump8cfcb522009-11-11 20:26:26 +0000967
Mike Stump9840c702009-11-12 20:47:57 +0000968llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass,
969 const CXXRecordDecl *RD,
Mike Stump8cfcb522009-11-11 20:26:26 +0000970 uint64_t Offset) {
Mike Stump9840c702009-11-12 20:47:57 +0000971 return CGM.GenerateVtable(LayoutClass, RD, Offset);
Mike Stump8cfcb522009-11-11 20:26:26 +0000972}