[cfe-commits] r77737 - in /cfe/trunk: include/clang/AST/DeclCXX.h lib/CodeGen/CGCXX.cpp

Mike Stump mrs at apple.com
Fri Jul 31 14:43:43 PDT 2009


Author: mrs
Date: Fri Jul 31 16:43:43 2009
New Revision: 77737

URL: http://llvm.org/viewvc/llvm-project?rev=77737&view=rev
Log:
And now we can generate a simple vtable.  Still a work in progress...

Modified:
    cfe/trunk/include/clang/AST/DeclCXX.h
    cfe/trunk/lib/CodeGen/CGCXX.cpp

Modified: cfe/trunk/include/clang/AST/DeclCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclCXX.h?rev=77737&r1=77736&r2=77737&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclCXX.h (original)
+++ cfe/trunk/include/clang/AST/DeclCXX.h Fri Jul 31 16:43:43 2009
@@ -425,9 +425,12 @@
   /// special methods, etc.
   typedef specific_decl_iterator<CXXMethodDecl> method_iterator;
   
+  /// method_begin - Method begin iterator.  Iterates in the order the methods
+  /// were declared.
   method_iterator method_begin() const {
     return method_iterator(decls_begin());
   }
+  /// method_end - Method end iterator.
   method_iterator method_end() const {
     return method_iterator(decls_end());
   }

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXX.cpp Fri Jul 31 16:43:43 2009
@@ -494,7 +494,6 @@
   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;
@@ -503,23 +502,36 @@
   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;
+  llvm::GlobalVariable::LinkageTypes linktype;
+  linktype = llvm::GlobalValue::WeakAnyLinkage;
+  std::vector<llvm::Constant *> methods;
   typedef CXXRecordDecl::method_iterator meth_iter;
+  llvm::Constant *m;
+  llvm::Type *Ptr8Ty;
+  Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
+  m = llvm::Constant::getNullValue(Ptr8Ty);
+  int64_t offset = 0;
+  methods.push_back(m); offset += LLVMPointerWidth;
+  methods.push_back(m); offset += LLVMPointerWidth;
   for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
        ++mi) {
-    if (mi->isVirtual())
-      methods.push_back(*mi);
+    if (mi->isVirtual()) {
+      m = CGM.GetAddrOfFunction(GlobalDecl(*mi));
+      m = llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
+      methods.push_back(m);
+    }
   }
-
-  llvm::Type *Ptr8Ty;
-  Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
+  llvm::Constant *C;
+  llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
+  C = llvm::ConstantArray::get(type, methods);
+  llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
+                                                 linktype, C, Name);
+  // CGM.CreateRuntimeFunction(FTy, Name);
   vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
-  // FIXME: finish layout for virtual bases and fix for 32-bit
-  int64_t offset = 16;
+  // FIXME: finish layout for virtual bases
   vtable = Builder.CreateGEP(vtable,
                              llvm::ConstantInt::get(llvm::Type::Int64Ty,
-                                                    offset));
+                                                    offset/8));
   return vtable;
 }
 





More information about the cfe-commits mailing list