[cfe-commits] r77699 - in /cfe/trunk/lib: AST/DeclCXX.cpp AST/RecordLayoutBuilder.cpp CodeGen/CGCXX.cpp CodeGen/CodeGenFunction.h CodeGen/CodeGenModule.h CodeGen/Mangle.cpp CodeGen/Mangle.h

Mike Stump mrs at apple.com
Fri Jul 31 11:25:34 PDT 2009


Author: mrs
Date: Fri Jul 31 13:25:34 2009
New Revision: 77699

URL: http://llvm.org/viewvc/llvm-project?rev=77699&view=rev
Log:
Add code to setup the vtable pointer in the constructor.  Work in progress.

Modified:
    cfe/trunk/lib/AST/DeclCXX.cpp
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
    cfe/trunk/lib/CodeGen/CGCXX.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h
    cfe/trunk/lib/CodeGen/CodeGenModule.h
    cfe/trunk/lib/CodeGen/Mangle.cpp
    cfe/trunk/lib/CodeGen/Mangle.h

Modified: cfe/trunk/lib/AST/DeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclCXX.cpp?rev=77699&r1=77698&r2=77699&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclCXX.cpp (original)
+++ cfe/trunk/lib/AST/DeclCXX.cpp Fri Jul 31 13:25:34 2009
@@ -377,6 +377,8 @@
   if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
     ClassTy = TD->getInjectedClassNameType(C);
   else
+    // FIXME: What is the design on getTagDeclType when it requires casting
+    // away const?  mutable?
     ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
   ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
   return C.getPointerType(ClassTy);
@@ -652,7 +654,7 @@
       AllToInit.push_back(Member);
     } 
   }
-  
+
   NumInitializers = AllToInit.size();
   if (NumInitializers > 0) {
     NumBaseOrMemberInitializers = NumInitializers;

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=77699&r1=77698&r2=77699&view=diff

==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Fri Jul 31 13:25:34 2009
@@ -27,7 +27,7 @@
 void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD) {
   if (RD->isPolymorphic() || RD->getNumVBases())
     {
-      assert (RD->getNumBases() == 0 && "no polymorphic inheritance yet");
+      // assert (RD->getNumBases() == 0 && "no polymorphic inheritance yet");
       int AS = 0;
       UpdateAlignment(Ctx.Target.getPointerAlign(AS));
       Size += Ctx.Target.getPointerWidth(AS);

Modified: cfe/trunk/lib/CodeGen/CGCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCXX.cpp?rev=77699&r1=77698&r2=77699&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXX.cpp Fri Jul 31 13:25:34 2009
@@ -107,8 +107,7 @@
 
   const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
 
-  // FIXME: It isn't just virtual as written, but all virtual functions.
-  if (MD->isVirtualAsWritten()) {
+  if (MD->isVirtual()) {
     ErrorUnsupported(CE, "virtual dispatch");
   }
 
@@ -491,21 +490,54 @@
   return UniqueMangledName(Name.begin(), Name.end());
 }
 
+llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
+  const llvm::FunctionType *FTy;
+  FTy = llvm::FunctionType::get(llvm::Type::VoidTy,
+                                std::vector<const llvm::Type*>(), false);
+
+  llvm::SmallString<256> OutName;
+  llvm::raw_svector_ostream Out(OutName);
+  QualType ClassTy;
+  // FIXME: What is the design on getTagDeclType when it requires casting
+  // away const?  mutable?
+  ClassTy = getContext().getTagDeclType(const_cast<CXXRecordDecl*>(RD));
+  mangleCXXVtable(ClassTy, getContext(), Out);
+  const char *Name = OutName.c_str();
+  llvm::Value *vtable = CGM.CreateRuntimeFunction(FTy, Name);
+  llvm::SmallVector<CXXMethodDecl *,32> methods;
+  typedef CXXRecordDecl::method_iterator meth_iter;
+  for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
+       ++mi) {
+    if (mi->isVirtual())
+      methods.push_back(*mi);
+  }
+
+  llvm::Type *Ptr8Ty;
+  Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
+  vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
+  // FIXME: finish layout for virtual bases and fix for 32-bit
+  int64_t offset = 16;
+  vtable = Builder.CreateGEP(vtable,
+                             llvm::ConstantInt::get(llvm::Type::Int64Ty,
+                                                    offset));
+  return vtable;
+}
+
 /// EmitCtorPrologue - This routine generates necessary code to initialize
 /// base classes and non-static data members belonging to this constructor.
 void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) {
   const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
-  assert(!ClassDecl->isPolymorphic()
-         && "FIXME: virtual table initialization unsupported");
   assert(ClassDecl->getNumVBases() == 0
          && "FIXME: virtual base initialization unsupported");
+  llvm::Value *LoadOfThis = 0;
+
   
   for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
        E = CD->init_end();
        B != E; ++B) {
     CXXBaseOrMemberInitializer *Member = (*B);
     if (Member->isBaseInitializer()) {
-      llvm::Value *LoadOfThis = LoadCXXThis();
+      LoadOfThis = LoadCXXThis();
       Type *BaseType = Member->getBaseClass();
       CXXRecordDecl *BaseClassDecl = 
         cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
@@ -522,7 +554,7 @@
       assert(!getContext().getAsArrayType(FieldType) 
              && "FIXME. Field arrays initialization unsupported");
 
-      llvm::Value *LoadOfThis = LoadCXXThis();
+      LoadOfThis = LoadCXXThis();
       LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
       if (FieldType->getAs<RecordType>()) {
         
@@ -544,6 +576,19 @@
         EmitStoreThroughLValue(RValue::get(RHS), LHS, FieldType);
     }
   }
+
+  // Initialize the vtable pointer
+  if (ClassDecl->isPolymorphic() || ClassDecl->getNumVBases()) {
+    if (!LoadOfThis)
+      LoadOfThis = LoadCXXThis();
+    llvm::Value *VtableField;
+    llvm::Type *Ptr8Ty, *PtrPtr8Ty;
+    Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
+    PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
+    VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
+    llvm::Value *vtable = GenerateVtable(ClassDecl);
+    Builder.CreateStore(vtable, VtableField);
+  }
 }
 
 /// EmitDtorEpilogue - Emit all code that comes at the end of class's

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=77699&r1=77698&r2=77699&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Fri Jul 31 13:25:34 2009
@@ -358,6 +358,8 @@
   /// legal to call this function even if there is no current insertion point.
   void FinishFunction(SourceLocation EndLoc=SourceLocation());
 
+  llvm::Value *GenerateVtable(const CXXRecordDecl *RD);
+
   void EmitCtorPrologue(const CXXConstructorDecl *CD);
   
   /// EmitDtorEpilogue - Emit all code that comes at the end of class's

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=77699&r1=77698&r2=77699&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Fri Jul 31 13:25:34 2009
@@ -373,7 +373,7 @@
                                     CXXDtorType Type);
 
   void EmitTentativeDefinition(const VarDecl *D);
-  
+
   enum GVALinkage {
     GVA_Internal,
     GVA_C99Inline,

Modified: cfe/trunk/lib/CodeGen/Mangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/Mangle.cpp?rev=77699&r1=77698&r2=77699&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.cpp (original)
+++ cfe/trunk/lib/CodeGen/Mangle.cpp Fri Jul 31 13:25:34 2009
@@ -41,6 +41,7 @@
     bool mangle(const NamedDecl *D);
     void mangleGuardVariable(const VarDecl *D);
     
+    void mangleCXXVtable(QualType Type);
     void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type);
     void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type);
 
@@ -158,10 +159,16 @@
   mangle(D);
 }
 
+void CXXNameMangler::mangleCXXVtable(QualType T) {
+  // <special-name> ::= TV <type>  # virtual table
+  Out << "_ZTV";
+  mangleType(T);
+}
+
 void CXXNameMangler::mangleGuardVariable(const VarDecl *D)
 {
   //  <special-name> ::= GV <object name>	# Guard variable for one-time 
-  //                                      # initialization
+  //                                            # initialization
 
   Out << "_ZGV";
   mangleName(D);
@@ -807,7 +814,12 @@
     
     os.flush();
   }
-  
-  
-}
 
+  void mangleCXXVtable(QualType Type, ASTContext &Context,
+                       llvm::raw_ostream &os) {
+    CXXNameMangler Mangler(Context, os);
+    Mangler.mangleCXXVtable(Type);
+
+    os.flush();
+  }
+}

Modified: cfe/trunk/lib/CodeGen/Mangle.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/Mangle.h?rev=77699&r1=77698&r2=77699&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.h (original)
+++ cfe/trunk/lib/CodeGen/Mangle.h Fri Jul 31 13:25:34 2009
@@ -19,6 +19,7 @@
 #define LLVM_CLANG_CODEGEN_MANGLE_H
 
 #include "CGCXX.h"
+#include "clang/AST/Type.h"
 
 namespace llvm {
   class raw_ostream;
@@ -35,6 +36,7 @@
                   llvm::raw_ostream &os);
   void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
                            llvm::raw_ostream &os);
+  void mangleCXXVtable(QualType T, ASTContext &Context, llvm::raw_ostream &os);
   void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
                      ASTContext &Context, llvm::raw_ostream &os);
   void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,





More information about the cfe-commits mailing list