blob: bed15132de8555c0ef439bbc762cf3152475c1a5 [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;
46 typedef std::pair<Index_t, Index_t> CallOffset;
47 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
48 Thunks_t Thunks;
49 typedef llvm::DenseMap<const CXXMethodDecl *,
Mike Stump87876a02009-10-13 10:55:21 +000050 std::pair<std::pair<CallOffset, CallOffset>,
51 CanQualType> > CovariantThunks_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000052 CovariantThunks_t CovariantThunks;
53 std::vector<Index_t> VCalls;
54 typedef CXXRecordDecl::method_iterator method_iter;
55 // FIXME: Linkage should follow vtable
56 const bool Extern;
57 const uint32_t LLVMPointerWidth;
58 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +000059 int CurrentVBaseOffset;
60 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000061public:
62 VtableBuilder(std::vector<llvm::Constant *> &meth,
63 const CXXRecordDecl *c,
64 CodeGenModule &cgm)
65 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
66 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
67 CGM(cgm), Extern(true),
Mike Stump37dbe962009-10-15 02:04:03 +000068 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)),
69 CurrentVBaseOffset(0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000070 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
71 }
72
73 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
74 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
75 { return VBIndex; }
76
77 llvm::Constant *wrap(Index_t i) {
78 llvm::Constant *m;
79 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
80 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
81 }
82
83 llvm::Constant *wrap(llvm::Constant *m) {
84 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
85 }
86
87 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stump87876a02009-10-13 10:55:21 +000088 const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +000089 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000090 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
91 e = RD->bases_end(); i != e; ++i) {
92 const CXXRecordDecl *Base =
93 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump28431212009-10-13 22:54:56 +000094 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000095 if (i->isVirtual() && !SeenVBase.count(Base)) {
96 SeenVBase.insert(Base);
97 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
98 llvm::Constant *m = wrap(BaseOffset);
99 m = wrap((0?700:0) + BaseOffset);
Mike Stump28431212009-10-13 22:54:56 +0000100 if (updateVBIndex) {
101 next_vbindex = (ssize_t)(-(offsets.size()*LLVMPointerWidth/8)
102 - 3*LLVMPointerWidth/8);
103 VBIndex[Base] = next_vbindex;
104 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000105 offsets.push_back(m);
106 }
Mike Stump28431212009-10-13 22:54:56 +0000107 // We also record offsets for non-virtual bases to closest enclosing
108 // virtual base. We do this so that we don't have to search
109 // for the nearst virtual base class when generating thunks.
110 if (updateVBIndex && VBIndex.count(Base) == 0)
111 VBIndex[Base] = next_vbindex;
112 GenerateVBaseOffsets(offsets, Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000113 }
114 }
115
116 void StartNewTable() {
117 SeenVBase.clear();
118 }
119
120 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
121
Mike Stump8bccbfd2009-10-15 09:30:16 +0000122 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
123 Index_t Offset = 0) {
124
125 if (B == D)
126 return Offset;
127
128 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
129 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
130 e = D->bases_end(); i != e; ++i) {
131 const CXXRecordDecl *Base =
132 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
133 int64_t BaseOffset = 0;
134 if (!i->isVirtual())
135 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
136 int64_t o = getNVOffset_1(Base, B, BaseOffset);
137 if (o >= 0)
138 return o;
139 }
140
141 return -1;
142 }
143
144 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
145 /// derived class D.
146 Index_t getNVOffset(QualType qB, QualType qD) {
147 qD = qD->getAs<PointerType>()->getPointeeType();
148 qB = qB->getAs<PointerType>()->getPointeeType();
149 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
150 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
151 int64_t o = getNVOffset_1(D, B);
152 if (o >= 0)
153 return o;
154
155 assert(false && "FIXME: non-virtual base not found");
156 return 0;
157 }
158
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000159 /// getVbaseOffset - Returns the index into the vtable for the virtual base
160 /// offset for the given (B) virtual base of the derived class D.
161 Index_t getVbaseOffset(QualType qB, QualType qD) {
162 qD = qD->getAs<PointerType>()->getPointeeType();
163 qB = qB->getAs<PointerType>()->getPointeeType();
164 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
165 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
166 if (D != Class)
167 return VBlookup(D, B);
168 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
169 i = VBIndex.find(B);
170 if (i != VBIndex.end())
171 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000172
Mike Stump28431212009-10-13 22:54:56 +0000173 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000174 return 0;
175 }
176
177 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stump8bccbfd2009-10-15 09:30:16 +0000178 bool MorallyVirtual, Index_t OverrideOffset,
179 Index_t Offset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000180 typedef CXXMethodDecl::method_iterator meth_iter;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000181 // FIXME: Should OverrideOffset's be Offset?
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000182
183 // FIXME: Don't like the nested loops. For very large inheritance
184 // heirarchies we could have a table on the side with the final overridder
185 // and just replace each instance of an overridden method once. Would be
186 // nice to measure the cost/benefit on real code.
187
188 for (meth_iter mi = MD->begin_overridden_methods(),
189 e = MD->end_overridden_methods();
190 mi != e; ++mi) {
191 const CXXMethodDecl *OMD = *mi;
192 llvm::Constant *om;
193 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
194 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
195
196 for (Index_t i = 0, e = submethods.size();
197 i != e; ++i) {
198 // FIXME: begin_overridden_methods might be too lax, covariance */
199 if (submethods[i] != om)
200 continue;
201 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
202 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
203 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
204 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
205 CallOffset ReturnOffset = std::make_pair(0, 0);
206 if (oret != ret) {
207 // FIXME: calculate offsets for covariance
Mike Stump87876a02009-10-13 10:55:21 +0000208 if (CovariantThunks.count(OMD)) {
209 oret = CovariantThunks[OMD].second;
210 CovariantThunks.erase(OMD);
211 }
Mike Stump8bccbfd2009-10-15 09:30:16 +0000212 // FIXME: Double check oret
213 Index_t nv = getNVOffset(oret, ret)/8;
Mike Stump87876a02009-10-13 10:55:21 +0000214 ReturnOffset = std::make_pair(nv, getVbaseOffset(oret, ret));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000215 }
216 Index[MD] = i;
217 submethods[i] = m;
218
219 Thunks.erase(OMD);
220 if (MorallyVirtual) {
221 Index_t &idx = VCall[OMD];
222 if (idx == 0) {
Mike Stump8bccbfd2009-10-15 09:30:16 +0000223 VCallOffset[MD] = OverrideOffset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000224 idx = VCalls.size()+1;
225 VCalls.push_back(0);
226 } else {
227 VCallOffset[MD] = VCallOffset[OMD];
Mike Stump8bccbfd2009-10-15 09:30:16 +0000228 VCalls[idx-1] = -VCallOffset[OMD] + OverrideOffset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000229 }
230 VCall[MD] = idx;
231 CallOffset ThisOffset;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000232 ThisOffset = std::make_pair(CurrentVBaseOffset/8 - Offset/8,
Mike Stump37dbe962009-10-15 02:04:03 +0000233 -((idx+extra+2)*LLVMPointerWidth/8));
234 // FIXME: Do we always have to build a covariant thunk to save oret,
235 // which is the containing virtual base class?
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000236 if (ReturnOffset.first || ReturnOffset.second)
Mike Stump87876a02009-10-13 10:55:21 +0000237 CovariantThunks[MD] = std::make_pair(std::make_pair(ThisOffset,
238 ReturnOffset),
239 oret);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000240 else
241 Thunks[MD] = ThisOffset;
242 return true;
243 }
Mike Stump28431212009-10-13 22:54:56 +0000244
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000245 // FIXME: finish off
Mike Stump8bccbfd2009-10-15 09:30:16 +0000246 int64_t O = VCallOffset[OMD] - OverrideOffset/8;
247 // int64_t O = CurrentVBaseOffset/8 - OverrideOffset/8;
Mike Stump28431212009-10-13 22:54:56 +0000248 if (O || ReturnOffset.first || ReturnOffset.second) {
249 CallOffset ThisOffset = std::make_pair(O, 0);
250
251 if (ReturnOffset.first || ReturnOffset.second)
252 CovariantThunks[MD] = std::make_pair(std::make_pair(ThisOffset,
253 ReturnOffset),
254 oret);
255 else
256 Thunks[MD] = ThisOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000257 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000258 return true;
259 }
260 }
261
262 return false;
263 }
264
265 void InstallThunks() {
266 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
267 i != e; ++i) {
268 const CXXMethodDecl *MD = i->first;
269 Index_t idx = Index[MD];
270 Index_t nv_O = i->second.first;
271 Index_t v_O = i->second.second;
272 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
273 }
274 Thunks.clear();
275 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
276 e = CovariantThunks.end();
277 i != e; ++i) {
278 const CXXMethodDecl *MD = i->first;
279 Index_t idx = Index[MD];
Mike Stump87876a02009-10-13 10:55:21 +0000280 Index_t nv_t = i->second.first.first.first;
281 Index_t v_t = i->second.first.first.second;
282 Index_t nv_r = i->second.first.second.first;
283 Index_t v_r = i->second.first.second.second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000284 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
285 v_r);
286 }
287 CovariantThunks.clear();
288 }
289
Mike Stump18e8b472009-10-27 23:36:26 +0000290 llvm::Constant *WrapAddrOf(const CXXMethodDecl *MD) {
291 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
292 return wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
293
294 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
295 const llvm::Type *Ty =
296 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
297 FPT->isVariadic());
298
299 return wrap(CGM.GetAddrOfFunction(MD, Ty));
300 }
301
Mike Stump8bccbfd2009-10-15 09:30:16 +0000302 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000303 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000304 e = Path->rend(); i != e; ++i) {
305 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000306 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000307 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
308 ++mi) {
309 if (!mi->isVirtual())
310 continue;
311
312 const CXXMethodDecl *MD = *mi;
Mike Stump18e8b472009-10-27 23:36:26 +0000313 llvm::Constant *m = WrapAddrOf(MD);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000314 OverrideMethod(MD, m, MorallyVirtual, OverrideOffset, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000315 }
316 }
317 }
318
319 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Mike Stump18e8b472009-10-27 23:36:26 +0000320 llvm::Constant *m = WrapAddrOf(MD);
321
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000322 // If we can find a previously allocated slot for this, reuse it.
Mike Stump8bccbfd2009-10-15 09:30:16 +0000323 if (OverrideMethod(MD, m, MorallyVirtual, Offset, Offset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000324 return;
325
326 // else allocate a new slot.
327 Index[MD] = submethods.size();
328 submethods.push_back(m);
329 if (MorallyVirtual) {
330 VCallOffset[MD] = Offset/8;
331 Index_t &idx = VCall[MD];
332 // Allocate the first one, after that, we reuse the previous one.
333 if (idx == 0) {
334 idx = VCalls.size()+1;
335 VCalls.push_back(0);
336 }
337 }
338 }
339
340 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
341 Index_t Offset) {
342 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
343 ++mi)
344 if (mi->isVirtual())
345 AddMethod(*mi, MorallyVirtual, Offset);
346 }
347
348 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
349 const CXXRecordDecl *PrimaryBase,
350 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stump37dbe962009-10-15 02:04:03 +0000351 int64_t Offset, Path_t *Path) {
352 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000353 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
354 e = RD->bases_end(); i != e; ++i) {
355 if (i->isVirtual())
356 continue;
357 const CXXRecordDecl *Base =
358 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
359 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
360 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
361 StartNewTable();
Mike Stump37dbe962009-10-15 02:04:03 +0000362 CurrentVBaseOffset = Offset;
363 GenerateVtableForBase(Base, MorallyVirtual, o, false, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000364 }
365 }
Mike Stump37dbe962009-10-15 02:04:03 +0000366 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000367 }
368
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000369// #define D(X) do { X; } while (0)
370#define D(X)
371
372 void insertVCalls(int InsertionPoint) {
373 llvm::Constant *e = 0;
374 D(VCalls.insert(VCalls.begin(), 673));
375 D(VCalls.push_back(672));
Mike Stump8bccbfd2009-10-15 09:30:16 +0000376 methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000377 // The vcalls come first...
378 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
379 e = VCalls.rend();
380 i != e; ++i)
381 methods[InsertionPoint++] = wrap((0?600:0) + *i);
382 VCalls.clear();
383 }
384
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000385 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
386 const ASTRecordLayout &Layout,
387 const CXXRecordDecl *PrimaryBase,
388 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stump37dbe962009-10-15 02:04:03 +0000389 int64_t Offset, bool ForVirtualBase, Path_t *Path) {
390 bool alloc = false;
391 if (Path == 0) {
392 alloc = true;
393 Path = new Path_t;
394 }
395
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000396 StartNewTable();
397 extra = 0;
398 // FIXME: Cleanup.
399 if (!ForVirtualBase) {
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000400 D(methods.push_back(wrap(666)));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000401 // then virtual base offsets...
402 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
403 e = offsets.rend(); i != e; ++i)
404 methods.push_back(*i);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000405 D(methods.push_back(wrap(667)));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000406 }
407
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000408 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
409 int VCallInsertionPoint = methods.size();
410 if (!DeferVCalls) {
411 insertVCalls(VCallInsertionPoint);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000412 } else
413 // FIXME: just for extra, or for all uses of VCalls.size post this?
414 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000415
416 if (ForVirtualBase) {
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000417 D(methods.push_back(wrap(668)));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000418 // then virtual base offsets...
419 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
420 e = offsets.rend(); i != e; ++i)
421 methods.push_back(*i);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000422 D(methods.push_back(wrap(669)));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000423 }
424
425 methods.push_back(wrap(-(Offset/8)));
426 methods.push_back(rtti);
427 Index_t AddressPoint = methods.size();
428
429 InstallThunks();
430 methods.insert(methods.end(), submethods.begin(), submethods.end());
431 submethods.clear();
432
433 // and then the non-virtual bases.
434 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stump37dbe962009-10-15 02:04:03 +0000435 MorallyVirtual, Offset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000436
437 if (ForVirtualBase) {
438 D(methods.push_back(wrap(670)));
439 insertVCalls(VCallInsertionPoint);
440 AddressPoint += VCalls.size();
441 D(methods.push_back(wrap(671)));
442 }
443
Mike Stump37dbe962009-10-15 02:04:03 +0000444 if (alloc) {
445 delete Path;
446 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000447 return AddressPoint;
448 }
449
450 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
451 if (!RD->isDynamicClass())
452 return;
453
454 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
455 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
456 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
457
458 // vtables are composed from the chain of primaries.
459 if (PrimaryBase) {
460 if (PrimaryBaseWasVirtual)
461 IndirectPrimary.insert(PrimaryBase);
462 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
463 }
464
465 // And add the virtuals for the class to the primary vtable.
466 AddMethods(RD, MorallyVirtual, Offset);
467 }
468
469 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
470 bool MorallyVirtual = false, int64_t Offset = 0,
471 bool ForVirtualBase = false,
Mike Stump37dbe962009-10-15 02:04:03 +0000472 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000473 if (!RD->isDynamicClass())
474 return 0;
475
476 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
477 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
478 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
479
480 std::vector<llvm::Constant *> offsets;
481 extra = 0;
Mike Stump28431212009-10-13 22:54:56 +0000482 GenerateVBaseOffsets(offsets, RD, Offset, !ForVirtualBase, 0);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000483 if (ForVirtualBase)
484 extra = offsets.size();
485
486 // vtables are composed from the chain of primaries.
487 if (PrimaryBase) {
488 if (PrimaryBaseWasVirtual)
489 IndirectPrimary.insert(PrimaryBase);
490 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
491 }
492
493 // And add the virtuals for the class to the primary vtable.
494 AddMethods(RD, MorallyVirtual, Offset);
495
496 if (Path)
Mike Stump8bccbfd2009-10-15 09:30:16 +0000497 OverrideMethods(Path, MorallyVirtual, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000498
499 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stump37dbe962009-10-15 02:04:03 +0000500 MorallyVirtual, Offset, ForVirtualBase, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000501 }
502
503 void GenerateVtableForVBases(const CXXRecordDecl *RD,
504 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000505 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000506 bool alloc = false;
507 if (Path == 0) {
508 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000509 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000510 }
511 // FIXME: We also need to override using all paths to a virtual base,
512 // right now, we just process the first path
513 Path->push_back(std::make_pair(RD, Offset));
514 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
515 e = RD->bases_end(); i != e; ++i) {
516 const CXXRecordDecl *Base =
517 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
518 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
519 // Mark it so we don't output it twice.
520 IndirectPrimary.insert(Base);
521 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000522 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000523 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump37dbe962009-10-15 02:04:03 +0000524 CurrentVBaseOffset = BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000525 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
526 }
527 int64_t BaseOffset = Offset;
528 if (i->isVirtual())
529 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump37dbe962009-10-15 02:04:03 +0000530 if (Base->getNumVBases()) {
531 CurrentVBaseOffset = BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000532 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000533 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000534 }
535 Path->pop_back();
536 if (alloc)
537 delete Path;
538 }
539};
540
541
542VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
543 CXXRecordDecl *B) {
544 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
545}
546
547int64_t CGVtableInfo::getMethodVtableIndex(const CXXMethodDecl *MD) {
548 MD = MD->getCanonicalDecl();
549
550 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(MD);
551 if (I != MethodVtableIndices.end())
552 return I->second;
553
554 const CXXRecordDecl *RD = MD->getParent();
555
556 std::vector<llvm::Constant *> methods;
557 // FIXME: This seems expensive. Can we do a partial job to get
558 // just this data.
559 VtableBuilder b(methods, RD, CGM);
560 b.GenerateVtableForBase(RD);
561 b.GenerateVtableForVBases(RD);
562
563 MethodVtableIndices.insert(b.getIndex().begin(),
564 b.getIndex().end());
565
566 I = MethodVtableIndices.find(MD);
567 assert(I != MethodVtableIndices.end() && "Did not find index!");
568 return I->second;
569}
570
571int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
572 const CXXRecordDecl *VBase) {
573 ClassPairTy ClassPair(RD, VBase);
574
575 VirtualBaseClassIndiciesTy::iterator I =
576 VirtualBaseClassIndicies.find(ClassPair);
577 if (I != VirtualBaseClassIndicies.end())
578 return I->second;
579
580 std::vector<llvm::Constant *> methods;
581 // FIXME: This seems expensive. Can we do a partial job to get
582 // just this data.
583 VtableBuilder b(methods, RD, CGM);
584 b.GenerateVtableForBase(RD);
585 b.GenerateVtableForVBases(RD);
586
587 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
588 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
589 // Insert all types.
590 ClassPairTy ClassPair(RD, I->first);
591
592 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
593 }
594
595 I = VirtualBaseClassIndicies.find(ClassPair);
596 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
597
598 return I->second;
599}
600
601llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
602 llvm::SmallString<256> OutName;
603 llvm::raw_svector_ostream Out(OutName);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000604 mangleCXXVtable(CGM.getMangleContext(), RD, Out);
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +0000605
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000606 llvm::GlobalVariable::LinkageTypes linktype;
Chandler Carruth6e0df532009-10-26 17:14:14 +0000607 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000608 std::vector<llvm::Constant *> methods;
609 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
610 int64_t AddressPoint;
611
612 VtableBuilder b(methods, RD, CGM);
613
614 // First comes the vtables for all the non-virtual bases...
615 AddressPoint = b.GenerateVtableForBase(RD);
616
617 // then the vtables for all the virtual bases.
618 b.GenerateVtableForVBases(RD);
619
620 llvm::Constant *C;
621 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
622 C = llvm::ConstantArray::get(type, methods);
623 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
624 linktype, C, Out.str());
625 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
626 vtable = Builder.CreateGEP(vtable,
627 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
628 AddressPoint*LLVMPointerWidth/8));
629 return vtable;
630}