2010-01-22 00:50:45 +08:00
|
|
|
//===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ code generation of VTTs (vtable tables).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenModule.h"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2010-01-22 00:50:45 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2011-09-26 09:56:24 +08:00
|
|
|
#include "clang/AST/VTTBuilder.h"
|
2010-01-22 00:50:45 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2015-04-03 02:55:21 +08:00
|
|
|
static llvm::GlobalVariable *
|
2013-09-27 22:48:01 +08:00
|
|
|
GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM,
|
|
|
|
const CXXRecordDecl *MostDerivedClass,
|
2011-10-23 03:16:39 +08:00
|
|
|
const VTTVTable &VTable,
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage,
|
2016-12-14 04:40:39 +08:00
|
|
|
VTableLayout::AddressPointsMapTy &AddressPoints) {
|
2011-09-26 09:56:06 +08:00
|
|
|
if (VTable.getBase() == MostDerivedClass) {
|
|
|
|
assert(VTable.getBaseOffset().isZero() &&
|
2010-03-26 11:56:54 +08:00
|
|
|
"Most derived class vtable must have a zero offset!");
|
|
|
|
// This is a regular vtable.
|
2013-09-27 22:48:01 +08:00
|
|
|
return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
|
2010-03-26 11:56:54 +08:00
|
|
|
}
|
|
|
|
|
2011-09-26 09:56:06 +08:00
|
|
|
return CGVT.GenerateConstructionVTable(MostDerivedClass,
|
|
|
|
VTable.getBaseSubobject(),
|
|
|
|
VTable.isVirtual(),
|
|
|
|
Linkage,
|
|
|
|
AddressPoints);
|
2010-03-26 11:56:54 +08:00
|
|
|
}
|
|
|
|
|
2011-01-30 03:16:51 +08:00
|
|
|
void
|
|
|
|
CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage,
|
|
|
|
const CXXRecordDecl *RD) {
|
2011-09-26 09:56:10 +08:00
|
|
|
VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
|
2010-01-22 00:50:45 +08:00
|
|
|
|
2016-03-15 03:07:10 +08:00
|
|
|
llvm::Type *Int8PtrTy = CGM.Int8PtrTy, *Int32Ty = CGM.Int32Ty;
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::ArrayType *ArrayType =
|
2011-01-30 03:16:51 +08:00
|
|
|
llvm::ArrayType::get(Int8PtrTy, Builder.getVTTComponents().size());
|
2015-04-03 02:55:21 +08:00
|
|
|
|
|
|
|
SmallVector<llvm::GlobalVariable *, 8> VTables;
|
2011-09-26 09:56:06 +08:00
|
|
|
SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
|
|
|
|
for (const VTTVTable *i = Builder.getVTTVTables().begin(),
|
|
|
|
*e = Builder.getVTTVTables().end(); i != e; ++i) {
|
|
|
|
VTableAddressPoints.push_back(VTableAddressPointsMapTy());
|
2013-09-27 22:48:01 +08:00
|
|
|
VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
|
2011-09-26 09:56:06 +08:00
|
|
|
VTableAddressPoints.back()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<llvm::Constant *, 8> VTTComponents;
|
|
|
|
for (const VTTComponent *i = Builder.getVTTComponents().begin(),
|
|
|
|
*e = Builder.getVTTComponents().end(); i != e; ++i) {
|
|
|
|
const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
|
2015-04-03 02:55:21 +08:00
|
|
|
llvm::GlobalVariable *VTable = VTables[i->VTableIndex];
|
2016-12-14 04:40:39 +08:00
|
|
|
VTableLayout::AddressPointLocation AddressPoint;
|
2011-09-26 09:56:06 +08:00
|
|
|
if (VTTVT.getBase() == RD) {
|
|
|
|
// Just get the address point for the regular vtable.
|
2013-11-05 23:54:58 +08:00
|
|
|
AddressPoint =
|
2013-12-21 07:58:52 +08:00
|
|
|
getItaniumVTableContext().getVTableLayout(RD).getAddressPoint(
|
|
|
|
i->VTableBase);
|
2011-09-26 09:56:06 +08:00
|
|
|
} else {
|
|
|
|
AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
|
2016-12-14 04:40:39 +08:00
|
|
|
assert(AddressPoint.AddressPointIndex != 0 &&
|
|
|
|
"Did not find ctor vtable address point!");
|
2011-09-26 09:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *Idxs[] = {
|
2016-03-15 03:07:10 +08:00
|
|
|
llvm::ConstantInt::get(Int32Ty, 0),
|
2016-12-14 04:40:39 +08:00
|
|
|
llvm::ConstantInt::get(Int32Ty, AddressPoint.VTableIndex),
|
|
|
|
llvm::ConstantInt::get(Int32Ty, AddressPoint.AddressPointIndex),
|
2011-09-26 09:56:06 +08:00
|
|
|
};
|
|
|
|
|
2016-12-14 04:50:44 +08:00
|
|
|
llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
|
|
|
|
VTable->getValueType(), VTable, Idxs, /*InBounds=*/true,
|
|
|
|
/*InRangeIndex=*/1);
|
2011-09-26 09:56:06 +08:00
|
|
|
|
|
|
|
Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
|
|
|
|
|
|
|
|
VTTComponents.push_back(Init);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
|
2011-01-30 03:16:51 +08:00
|
|
|
|
|
|
|
VTT->setInitializer(Init);
|
2010-01-22 00:50:45 +08:00
|
|
|
|
2011-01-30 03:16:51 +08:00
|
|
|
// Set the correct linkage.
|
|
|
|
VTT->setLinkage(Linkage);
|
2011-01-30 03:34:19 +08:00
|
|
|
|
2015-05-10 05:10:07 +08:00
|
|
|
if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
|
|
|
|
VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
|
2015-01-16 07:18:01 +08:00
|
|
|
|
2011-01-30 03:34:19 +08:00
|
|
|
// Set the right visibility.
|
2014-02-08 08:41:16 +08:00
|
|
|
CGM.setGlobalVisibility(VTT, RD);
|
2011-01-30 03:16:51 +08:00
|
|
|
}
|
2010-01-22 00:50:45 +08:00
|
|
|
|
2011-01-30 03:16:51 +08:00
|
|
|
llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
|
|
|
|
assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
|
2010-01-22 00:50:45 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> OutName;
|
2011-02-11 10:52:17 +08:00
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
2013-10-03 14:26:13 +08:00
|
|
|
cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
|
|
|
|
.mangleCXXVTT(RD, Out);
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name = OutName.str();
|
2010-03-26 12:10:39 +08:00
|
|
|
|
2011-09-26 09:56:36 +08:00
|
|
|
// This will also defer the definition of the VTT.
|
2013-09-27 22:48:01 +08:00
|
|
|
(void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
|
2011-05-16 12:08:36 +08:00
|
|
|
|
2011-09-26 09:56:10 +08:00
|
|
|
VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
|
2010-01-22 00:50:45 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::ArrayType *ArrayType =
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
|
2010-01-22 00:50:45 +08:00
|
|
|
|
2011-01-30 03:16:51 +08:00
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
|
|
|
|
llvm::GlobalValue::ExternalLinkage);
|
2016-06-15 05:02:05 +08:00
|
|
|
GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
2010-01-22 00:50:45 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2010-03-23 12:11:45 +08:00
|
|
|
uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
|
2010-05-03 07:53:25 +08:00
|
|
|
BaseSubobject Base) {
|
2010-05-03 08:55:11 +08:00
|
|
|
BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
|
2010-01-22 00:50:45 +08:00
|
|
|
|
2010-05-03 08:55:11 +08:00
|
|
|
SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
|
2010-01-22 00:50:45 +08:00
|
|
|
if (I != SubVTTIndicies.end())
|
|
|
|
return I->second;
|
|
|
|
|
2011-09-26 09:56:10 +08:00
|
|
|
VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
|
2010-01-22 00:50:45 +08:00
|
|
|
|
2010-05-03 08:55:11 +08:00
|
|
|
for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
|
2010-01-22 00:50:45 +08:00
|
|
|
Builder.getSubVTTIndicies().begin(),
|
|
|
|
E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
|
|
|
|
// Insert all indices.
|
2010-05-03 08:55:11 +08:00
|
|
|
BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
|
2010-01-22 00:50:45 +08:00
|
|
|
|
2010-05-03 08:55:11 +08:00
|
|
|
SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
|
2010-01-22 00:50:45 +08:00
|
|
|
}
|
|
|
|
|
2010-05-03 08:55:11 +08:00
|
|
|
I = SubVTTIndicies.find(ClassSubobjectPair);
|
2010-01-22 00:50:45 +08:00
|
|
|
assert(I != SubVTTIndicies.end() && "Did not find index!");
|
|
|
|
|
|
|
|
return I->second;
|
|
|
|
}
|
2010-03-26 12:23:58 +08:00
|
|
|
|
|
|
|
uint64_t
|
|
|
|
CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
|
|
|
|
BaseSubobject Base) {
|
|
|
|
SecondaryVirtualPointerIndicesMapTy::iterator I =
|
|
|
|
SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
|
|
|
|
|
|
|
|
if (I != SecondaryVirtualPointerIndices.end())
|
|
|
|
return I->second;
|
|
|
|
|
2011-09-26 09:56:10 +08:00
|
|
|
VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
|
2010-03-26 12:23:58 +08:00
|
|
|
|
|
|
|
// Insert all secondary vpointer indices.
|
|
|
|
for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
|
|
|
|
Builder.getSecondaryVirtualPointerIndices().begin(),
|
|
|
|
E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
|
|
|
|
std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
|
|
|
|
std::make_pair(RD, I->first);
|
|
|
|
|
|
|
|
SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
|
|
|
|
}
|
|
|
|
|
|
|
|
I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
|
|
|
|
assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
|
|
|
|
|
|
|
|
return I->second;
|
|
|
|
}
|