• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
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 VTTs (vtable tables).
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenModule.h"
15 #include "CGCXXABI.h"
16 #include "clang/AST/RecordLayout.h"
17 #include "clang/AST/VTTBuilder.h"
18 using namespace clang;
19 using namespace CodeGen;
20 
21 static llvm::Constant *
GetAddrOfVTTVTable(CodeGenVTables & CGVT,const CXXRecordDecl * MostDerivedClass,const VTTVTable & VTable,llvm::GlobalVariable::LinkageTypes Linkage,llvm::DenseMap<BaseSubobject,uint64_t> & AddressPoints)22 GetAddrOfVTTVTable(CodeGenVTables &CGVT, const CXXRecordDecl *MostDerivedClass,
23                    const VTTVTable &VTable,
24                    llvm::GlobalVariable::LinkageTypes Linkage,
25                    llvm::DenseMap<BaseSubobject, uint64_t> &AddressPoints) {
26   if (VTable.getBase() == MostDerivedClass) {
27     assert(VTable.getBaseOffset().isZero() &&
28            "Most derived class vtable must have a zero offset!");
29     // This is a regular vtable.
30     return CGVT.GetAddrOfVTable(MostDerivedClass);
31   }
32 
33   return CGVT.GenerateConstructionVTable(MostDerivedClass,
34                                          VTable.getBaseSubobject(),
35                                          VTable.isVirtual(),
36                                          Linkage,
37                                          AddressPoints);
38 }
39 
40 void
EmitVTTDefinition(llvm::GlobalVariable * VTT,llvm::GlobalVariable::LinkageTypes Linkage,const CXXRecordDecl * RD)41 CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
42                                   llvm::GlobalVariable::LinkageTypes Linkage,
43                                   const CXXRecordDecl *RD) {
44   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
45 
46   llvm::Type *Int8PtrTy = CGM.Int8PtrTy, *Int64Ty = CGM.Int64Ty;
47   llvm::ArrayType *ArrayType =
48     llvm::ArrayType::get(Int8PtrTy, Builder.getVTTComponents().size());
49 
50   SmallVector<llvm::Constant *, 8> VTables;
51   SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
52   for (const VTTVTable *i = Builder.getVTTVTables().begin(),
53                        *e = Builder.getVTTVTables().end(); i != e; ++i) {
54     VTableAddressPoints.push_back(VTableAddressPointsMapTy());
55     VTables.push_back(GetAddrOfVTTVTable(*this, RD, *i, Linkage,
56                                          VTableAddressPoints.back()));
57   }
58 
59   SmallVector<llvm::Constant *, 8> VTTComponents;
60   for (const VTTComponent *i = Builder.getVTTComponents().begin(),
61                           *e = Builder.getVTTComponents().end(); i != e; ++i) {
62     const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
63     llvm::Constant *VTable = VTables[i->VTableIndex];
64     uint64_t AddressPoint;
65     if (VTTVT.getBase() == RD) {
66       // Just get the address point for the regular vtable.
67       AddressPoint = VTContext.getVTableLayout(RD)
68                               .getAddressPoint(i->VTableBase);
69       assert(AddressPoint != 0 && "Did not find vtable address point!");
70     } else {
71       AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
72       assert(AddressPoint != 0 && "Did not find ctor vtable address point!");
73     }
74 
75      llvm::Value *Idxs[] = {
76        llvm::ConstantInt::get(Int64Ty, 0),
77        llvm::ConstantInt::get(Int64Ty, AddressPoint)
78      };
79 
80      llvm::Constant *Init =
81        llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Idxs);
82 
83      Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
84 
85      VTTComponents.push_back(Init);
86   }
87 
88   llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
89 
90   VTT->setInitializer(Init);
91 
92   // Set the correct linkage.
93   VTT->setLinkage(Linkage);
94 
95   // Set the right visibility.
96   CGM.setTypeVisibility(VTT, RD, CodeGenModule::TVK_ForVTT);
97 }
98 
GetAddrOfVTT(const CXXRecordDecl * RD)99 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
100   assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
101 
102   SmallString<256> OutName;
103   llvm::raw_svector_ostream Out(OutName);
104   CGM.getCXXABI().getMangleContext().mangleCXXVTT(RD, Out);
105   Out.flush();
106   StringRef Name = OutName.str();
107 
108   // This will also defer the definition of the VTT.
109   (void) GetAddrOfVTable(RD);
110 
111   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
112 
113   llvm::ArrayType *ArrayType =
114     llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
115 
116   llvm::GlobalVariable *GV =
117     CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
118                                           llvm::GlobalValue::ExternalLinkage);
119   GV->setUnnamedAddr(true);
120   return GV;
121 }
122 
needsVTTParameter(GlobalDecl GD)123 bool CodeGenVTables::needsVTTParameter(GlobalDecl GD) {
124   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
125 
126   // We don't have any virtual bases, just return early.
127   if (!MD->getParent()->getNumVBases())
128     return false;
129 
130   // Check if we have a base constructor.
131   if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
132     return true;
133 
134   // Check if we have a base destructor.
135   if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
136     return true;
137 
138   return false;
139 }
140 
getSubVTTIndex(const CXXRecordDecl * RD,BaseSubobject Base)141 uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
142                                         BaseSubobject Base) {
143   BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
144 
145   SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
146   if (I != SubVTTIndicies.end())
147     return I->second;
148 
149   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
150 
151   for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
152        Builder.getSubVTTIndicies().begin(),
153        E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
154     // Insert all indices.
155     BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
156 
157     SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
158   }
159 
160   I = SubVTTIndicies.find(ClassSubobjectPair);
161   assert(I != SubVTTIndicies.end() && "Did not find index!");
162 
163   return I->second;
164 }
165 
166 uint64_t
getSecondaryVirtualPointerIndex(const CXXRecordDecl * RD,BaseSubobject Base)167 CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
168                                                 BaseSubobject Base) {
169   SecondaryVirtualPointerIndicesMapTy::iterator I =
170     SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
171 
172   if (I != SecondaryVirtualPointerIndices.end())
173     return I->second;
174 
175   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
176 
177   // Insert all secondary vpointer indices.
178   for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
179        Builder.getSecondaryVirtualPointerIndices().begin(),
180        E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
181     std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
182       std::make_pair(RD, I->first);
183 
184     SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
185   }
186 
187   I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
188   assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
189 
190   return I->second;
191 }
192 
193