From 67829f64938cc2057795a3e9c41df512bfa5724a Mon Sep 17 00:00:00 2001 From: Mike Stump Date: Fri, 31 Jul 2009 21:43:43 +0000 Subject: [PATCH] And now we can generate a simple vtable. Still a work in progress... llvm-svn: 77737 --- clang/include/clang/AST/DeclCXX.h | 3 +++ clang/lib/CodeGen/CGCXX.cpp | 34 +++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h index 5a4923c..18ff6a3 100644 --- a/clang/include/clang/AST/DeclCXX.h +++ b/clang/include/clang/AST/DeclCXX.h @@ -425,9 +425,12 @@ public: /// special methods, etc. typedef specific_decl_iterator 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()); } diff --git a/clang/lib/CodeGen/CGCXX.cpp b/clang/lib/CodeGen/CGCXX.cpp index 6c0c0d9..62dabf7 100644 --- a/clang/lib/CodeGen/CGCXX.cpp +++ b/clang/lib/CodeGen/CGCXX.cpp @@ -494,7 +494,6 @@ llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { const llvm::FunctionType *FTy; FTy = llvm::FunctionType::get(llvm::Type::VoidTy, std::vector(), false); - llvm::SmallString<256> OutName; llvm::raw_svector_ostream Out(OutName); QualType ClassTy; @@ -503,23 +502,36 @@ llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { ClassTy = getContext().getTagDeclType(const_cast(RD)); mangleCXXVtable(ClassTy, getContext(), Out); const char *Name = OutName.c_str(); - llvm::Value *vtable = CGM.CreateRuntimeFunction(FTy, Name); - llvm::SmallVector methods; + llvm::GlobalVariable::LinkageTypes linktype; + linktype = llvm::GlobalValue::WeakAnyLinkage; + std::vector 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; } -- 2.7.4