]> granicus.if.org Git - clang/commitdiff
More layout for virtual tables for virtual bases. Still in progress.
authorMike Stump <mrs@apple.com>
Thu, 6 Aug 2009 15:50:11 +0000 (15:50 +0000)
committerMike Stump <mrs@apple.com>
Thu, 6 Aug 2009 15:50:11 +0000 (15:50 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78314 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGCXX.cpp
lib/CodeGen/CodeGenFunction.h
test/CodeGenCXX/virt.cpp

index eb8e87d372ef4b9860cb74a7fb0b42befae0d574..7cf397fc07f2f41cacaf5a5ac9354982a9015c48 100644 (file)
@@ -536,6 +536,22 @@ llvm::Constant *CodeGenFunction::GenerateRtti(const CXXRecordDecl *RD) {
   return Rtti;
 }
 
+void CodeGenFunction::GenerateVtableForBase(const CXXRecordDecl *RD,
+                                       std::vector<llvm::Constant *> &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<CXXRecordDecl>(i->getType()->getAs<RecordType>()->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));
index 6a887d1aa9db5bec9ffe94f23e53346f98ed231d..1f0fad609f546d68f07a0e6e59f7eeaa03d7eba4 100644 (file)
@@ -359,6 +359,8 @@ public:
   void FinishFunction(SourceLocation EndLoc=SourceLocation());
 
   llvm::Constant *GenerateRtti(const CXXRecordDecl *RD);
+  void GenerateVtableForBase(const CXXRecordDecl *RD,
+                             std::vector<llvm::Constant *> &methods);
   llvm::Value *GenerateVtable(const CXXRecordDecl *RD);
 
   void EmitCtorPrologue(const CXXConstructorDecl *CD);
index d2f5e226d45a45648003ba93f47576f207687131..f462b0e693e4e634f8260ea86293d6f652fe6973 100644 (file)
@@ -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
+