From: Mike Stump Date: Thu, 6 Aug 2009 15:50:11 +0000 (+0000) Subject: More layout for virtual tables for virtual bases. Still in progress. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8a12b564923a72224730a467007e61b5701e4aa7;p=clang More layout for virtual tables for virtual bases. Still in progress. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78314 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp index eb8e87d372..7cf397fc07 100644 --- a/lib/CodeGen/CGCXX.cpp +++ b/lib/CodeGen/CGCXX.cpp @@ -536,6 +536,22 @@ llvm::Constant *CodeGenFunction::GenerateRtti(const CXXRecordDecl *RD) { return Rtti; } +void CodeGenFunction::GenerateVtableForBase(const CXXRecordDecl *RD, + std::vector &methods) { + typedef CXXRecordDecl::method_iterator meth_iter; + llvm::Type *Ptr8Ty; + Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0); + for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; + ++mi) { + if (mi->isVirtual()) { + llvm::Constant *m; + m = CGM.GetAddrOfFunction(GlobalDecl(*mi)); + m = llvm::ConstantExpr::getBitCast(m, Ptr8Ty); + methods.push_back(m); + } + } +} + llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { llvm::SmallString<256> OutName; llvm::raw_svector_ostream Out(OutName); @@ -576,14 +592,7 @@ llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { m = llvm::Constant::getNullValue(Ptr8Ty); methods.push_back(m); } - for (meth_iter mi = Base->method_begin(), me = Base->method_end(); mi != me; - ++mi) { - if (mi->isVirtual()) { - m = CGM.GetAddrOfFunction(GlobalDecl(*mi)); - m = llvm::ConstantExpr::getBitCast(m, Ptr8Ty); - methods.push_back(m); - } - } + GenerateVtableForBase(Base, methods); if (PrimaryBase == Base) { for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; ++mi) { @@ -605,6 +614,15 @@ llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { } } } + // FIXME: finish layout for virtual bases + // FIXME: audit indirect virtual bases + for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(), + e = RD->vbases_end(); i != e; ++i) { + const CXXRecordDecl *Base = + cast(i->getType()->getAs()->getDecl()); + if (Base != PrimaryBase) + GenerateVtableForBase(Base, methods); + } llvm::Constant *C; llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size()); @@ -612,7 +630,6 @@ llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true, linktype, C, Name); vtable = Builder.CreateBitCast(vtable, Ptr8Ty); - // FIXME: finish layout for virtual bases vtable = Builder.CreateGEP(vtable, llvm::ConstantInt::get(llvm::Type::Int64Ty, Offset/8)); diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 6a887d1aa9..1f0fad609f 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -359,6 +359,8 @@ public: void FinishFunction(SourceLocation EndLoc=SourceLocation()); llvm::Constant *GenerateRtti(const CXXRecordDecl *RD); + void GenerateVtableForBase(const CXXRecordDecl *RD, + std::vector &methods); llvm::Value *GenerateVtable(const CXXRecordDecl *RD); void EmitCtorPrologue(const CXXConstructorDecl *CD); diff --git a/test/CodeGenCXX/virt.cpp b/test/CodeGenCXX/virt.cpp index d2f5e226d4..f462b0e693 100644 --- a/test/CodeGenCXX/virt.cpp +++ b/test/CodeGenCXX/virt.cpp @@ -47,7 +47,7 @@ void test2() { // CHECK-LP32: movl $4, __ZZ5test2vE2sz // CHECK-LP64: movl $8, __ZZ5test2vE2sz(%rip) } - + static_assert(sizeof(F) == sizeof(void*)*4, "invalid vbase size"); struct E { @@ -116,3 +116,30 @@ int main() { // CHECK-LP32: .space 4 // CHECK-LP32: .long __ZN1C4bee1Ev // CHECK-LP32: .long __ZN1C4bee2Ev + +// CHECK-LP32: __ZTV1F: +// CHECK-LP32 .space 4 +// CHECK-LP32 .long 8 +// CHECK-LP32 .space 4 +// CHECK-LP32: .space 4 +// CHECK-LP32: .space 4 +// CHECK-LP32 .long __ZN1D3booEv +// CHECK-LP32 .long __ZN1F3fooEv +// CHECK-LP32 .space 4 +// CHECK-LP32 .long -8 +// CHECK-LP32 .space 4 +// CHECK-LP32: .long __ZN2D13barEv + +// CHECK-LP64: __ZTV1F: +// CHECK-LP64 .space 8 +// CHECK-LP64 .quad 16 +// CHECK-LP64 .space 8 +// CHECK-LP64: .space 8 +// CHECK-LP64: .space 8 +// CHECK-LP64 .quad __ZN1D3booEv +// CHECK-LP64 .quad __ZN1F3fooEv +// CHECK-LP64 .space 8 +// CHECK-LP64 .space -16 +// CHECK-LP64 .space 8 +// CHECK-LP64: .quad __ZN2D13barEv +