xref: /minix3/external/bsd/llvm/dist/clang/lib/CodeGen/CGVTT.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This contains code dealing with C++ code generation of VTTs (vtable tables).
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "CodeGenModule.h"
15f4a2713aSLionel Sambuc #include "CGCXXABI.h"
16f4a2713aSLionel Sambuc #include "clang/AST/RecordLayout.h"
17f4a2713aSLionel Sambuc #include "clang/AST/VTTBuilder.h"
18f4a2713aSLionel Sambuc using namespace clang;
19f4a2713aSLionel Sambuc using namespace CodeGen;
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc static llvm::Constant *
GetAddrOfVTTVTable(CodeGenVTables & CGVT,CodeGenModule & CGM,const CXXRecordDecl * MostDerivedClass,const VTTVTable & VTable,llvm::GlobalVariable::LinkageTypes Linkage,llvm::DenseMap<BaseSubobject,uint64_t> & AddressPoints)22f4a2713aSLionel Sambuc GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM,
23f4a2713aSLionel Sambuc                    const CXXRecordDecl *MostDerivedClass,
24f4a2713aSLionel Sambuc                    const VTTVTable &VTable,
25f4a2713aSLionel Sambuc                    llvm::GlobalVariable::LinkageTypes Linkage,
26f4a2713aSLionel Sambuc                    llvm::DenseMap<BaseSubobject, uint64_t> &AddressPoints) {
27f4a2713aSLionel Sambuc   if (VTable.getBase() == MostDerivedClass) {
28f4a2713aSLionel Sambuc     assert(VTable.getBaseOffset().isZero() &&
29f4a2713aSLionel Sambuc            "Most derived class vtable must have a zero offset!");
30f4a2713aSLionel Sambuc     // This is a regular vtable.
31f4a2713aSLionel Sambuc     return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
32f4a2713aSLionel Sambuc   }
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc   return CGVT.GenerateConstructionVTable(MostDerivedClass,
35f4a2713aSLionel Sambuc                                          VTable.getBaseSubobject(),
36f4a2713aSLionel Sambuc                                          VTable.isVirtual(),
37f4a2713aSLionel Sambuc                                          Linkage,
38f4a2713aSLionel Sambuc                                          AddressPoints);
39f4a2713aSLionel Sambuc }
40f4a2713aSLionel Sambuc 
41f4a2713aSLionel Sambuc void
EmitVTTDefinition(llvm::GlobalVariable * VTT,llvm::GlobalVariable::LinkageTypes Linkage,const CXXRecordDecl * RD)42f4a2713aSLionel Sambuc CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
43f4a2713aSLionel Sambuc                                   llvm::GlobalVariable::LinkageTypes Linkage,
44f4a2713aSLionel Sambuc                                   const CXXRecordDecl *RD) {
45f4a2713aSLionel Sambuc   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc   llvm::Type *Int8PtrTy = CGM.Int8PtrTy, *Int64Ty = CGM.Int64Ty;
48f4a2713aSLionel Sambuc   llvm::ArrayType *ArrayType =
49f4a2713aSLionel Sambuc     llvm::ArrayType::get(Int8PtrTy, Builder.getVTTComponents().size());
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc   SmallVector<llvm::Constant *, 8> VTables;
52f4a2713aSLionel Sambuc   SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
53f4a2713aSLionel Sambuc   for (const VTTVTable *i = Builder.getVTTVTables().begin(),
54f4a2713aSLionel Sambuc                        *e = Builder.getVTTVTables().end(); i != e; ++i) {
55f4a2713aSLionel Sambuc     VTableAddressPoints.push_back(VTableAddressPointsMapTy());
56f4a2713aSLionel Sambuc     VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
57f4a2713aSLionel Sambuc                                          VTableAddressPoints.back()));
58f4a2713aSLionel Sambuc   }
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc   SmallVector<llvm::Constant *, 8> VTTComponents;
61f4a2713aSLionel Sambuc   for (const VTTComponent *i = Builder.getVTTComponents().begin(),
62f4a2713aSLionel Sambuc                           *e = Builder.getVTTComponents().end(); i != e; ++i) {
63f4a2713aSLionel Sambuc     const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
64f4a2713aSLionel Sambuc     llvm::Constant *VTable = VTables[i->VTableIndex];
65f4a2713aSLionel Sambuc     uint64_t AddressPoint;
66f4a2713aSLionel Sambuc     if (VTTVT.getBase() == RD) {
67f4a2713aSLionel Sambuc       // Just get the address point for the regular vtable.
68f4a2713aSLionel Sambuc       AddressPoint =
69*0a6a1f1dSLionel Sambuc           getItaniumVTableContext().getVTableLayout(RD).getAddressPoint(
70*0a6a1f1dSLionel Sambuc               i->VTableBase);
71f4a2713aSLionel Sambuc       assert(AddressPoint != 0 && "Did not find vtable address point!");
72f4a2713aSLionel Sambuc     } else {
73f4a2713aSLionel Sambuc       AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
74f4a2713aSLionel Sambuc       assert(AddressPoint != 0 && "Did not find ctor vtable address point!");
75f4a2713aSLionel Sambuc     }
76f4a2713aSLionel Sambuc 
77f4a2713aSLionel Sambuc      llvm::Value *Idxs[] = {
78f4a2713aSLionel Sambuc        llvm::ConstantInt::get(Int64Ty, 0),
79f4a2713aSLionel Sambuc        llvm::ConstantInt::get(Int64Ty, AddressPoint)
80f4a2713aSLionel Sambuc      };
81f4a2713aSLionel Sambuc 
82f4a2713aSLionel Sambuc      llvm::Constant *Init =
83f4a2713aSLionel Sambuc        llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Idxs);
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc      Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc      VTTComponents.push_back(Init);
88f4a2713aSLionel Sambuc   }
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc   llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc   VTT->setInitializer(Init);
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc   // Set the correct linkage.
95f4a2713aSLionel Sambuc   VTT->setLinkage(Linkage);
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc   // Set the right visibility.
98*0a6a1f1dSLionel Sambuc   CGM.setGlobalVisibility(VTT, RD);
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc 
GetAddrOfVTT(const CXXRecordDecl * RD)101f4a2713aSLionel Sambuc llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
102f4a2713aSLionel Sambuc   assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc   SmallString<256> OutName;
105f4a2713aSLionel Sambuc   llvm::raw_svector_ostream Out(OutName);
106f4a2713aSLionel Sambuc   cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
107f4a2713aSLionel Sambuc       .mangleCXXVTT(RD, Out);
108f4a2713aSLionel Sambuc   Out.flush();
109f4a2713aSLionel Sambuc   StringRef Name = OutName.str();
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc   // This will also defer the definition of the VTT.
112f4a2713aSLionel Sambuc   (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc   llvm::ArrayType *ArrayType =
117f4a2713aSLionel Sambuc     llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc   llvm::GlobalVariable *GV =
120f4a2713aSLionel Sambuc     CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
121f4a2713aSLionel Sambuc                                           llvm::GlobalValue::ExternalLinkage);
122f4a2713aSLionel Sambuc   GV->setUnnamedAddr(true);
123f4a2713aSLionel Sambuc   return GV;
124f4a2713aSLionel Sambuc }
125f4a2713aSLionel Sambuc 
getSubVTTIndex(const CXXRecordDecl * RD,BaseSubobject Base)126f4a2713aSLionel Sambuc uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
127f4a2713aSLionel Sambuc                                         BaseSubobject Base) {
128f4a2713aSLionel Sambuc   BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
129f4a2713aSLionel Sambuc 
130f4a2713aSLionel Sambuc   SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
131f4a2713aSLionel Sambuc   if (I != SubVTTIndicies.end())
132f4a2713aSLionel Sambuc     return I->second;
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc   for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
137f4a2713aSLionel Sambuc        Builder.getSubVTTIndicies().begin(),
138f4a2713aSLionel Sambuc        E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
139f4a2713aSLionel Sambuc     // Insert all indices.
140f4a2713aSLionel Sambuc     BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc     SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
143f4a2713aSLionel Sambuc   }
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc   I = SubVTTIndicies.find(ClassSubobjectPair);
146f4a2713aSLionel Sambuc   assert(I != SubVTTIndicies.end() && "Did not find index!");
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc   return I->second;
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc uint64_t
getSecondaryVirtualPointerIndex(const CXXRecordDecl * RD,BaseSubobject Base)152f4a2713aSLionel Sambuc CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
153f4a2713aSLionel Sambuc                                                 BaseSubobject Base) {
154f4a2713aSLionel Sambuc   SecondaryVirtualPointerIndicesMapTy::iterator I =
155f4a2713aSLionel Sambuc     SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc   if (I != SecondaryVirtualPointerIndices.end())
158f4a2713aSLionel Sambuc     return I->second;
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
161f4a2713aSLionel Sambuc 
162f4a2713aSLionel Sambuc   // Insert all secondary vpointer indices.
163f4a2713aSLionel Sambuc   for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
164f4a2713aSLionel Sambuc        Builder.getSecondaryVirtualPointerIndices().begin(),
165f4a2713aSLionel Sambuc        E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
166f4a2713aSLionel Sambuc     std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
167f4a2713aSLionel Sambuc       std::make_pair(RD, I->first);
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc     SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
170f4a2713aSLionel Sambuc   }
171f4a2713aSLionel Sambuc 
172f4a2713aSLionel Sambuc   I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
173f4a2713aSLionel Sambuc   assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
174f4a2713aSLionel Sambuc 
175f4a2713aSLionel Sambuc   return I->second;
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc 
178