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 * 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 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 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 123 uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD, 124 BaseSubobject Base) { 125 BaseSubobjectPairTy ClassSubobjectPair(RD, Base); 126 127 SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair); 128 if (I != SubVTTIndicies.end()) 129 return I->second; 130 131 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false); 132 133 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I = 134 Builder.getSubVTTIndicies().begin(), 135 E = Builder.getSubVTTIndicies().end(); I != E; ++I) { 136 // Insert all indices. 137 BaseSubobjectPairTy ClassSubobjectPair(RD, I->first); 138 139 SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second)); 140 } 141 142 I = SubVTTIndicies.find(ClassSubobjectPair); 143 assert(I != SubVTTIndicies.end() && "Did not find index!"); 144 145 return I->second; 146 } 147 148 uint64_t 149 CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, 150 BaseSubobject Base) { 151 SecondaryVirtualPointerIndicesMapTy::iterator I = 152 SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base)); 153 154 if (I != SecondaryVirtualPointerIndices.end()) 155 return I->second; 156 157 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false); 158 159 // Insert all secondary vpointer indices. 160 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I = 161 Builder.getSecondaryVirtualPointerIndices().begin(), 162 E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) { 163 std::pair<const CXXRecordDecl *, BaseSubobject> Pair = 164 std::make_pair(RD, I->first); 165 166 SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second)); 167 } 168 169 I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base)); 170 assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!"); 171 172 return I->second; 173 } 174 175