From: Mike Stump Date: Fri, 31 Jul 2009 21:43:43 +0000 (+0000) Subject: And now we can generate a simple vtable. Still a work in progress... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=82b56961dcb813674dbda3c5f5aaee703d55741c;p=clang And now we can generate a simple vtable. Still a work in progress... git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77737 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DeclCXX.h b/include/clang/AST/DeclCXX.h index 5a4923c869..18ff6a3b81 100644 --- a/include/clang/AST/DeclCXX.h +++ b/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/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp index 6c0c0d90f1..62dabf77d6 100644 --- a/lib/CodeGen/CGCXX.cpp +++ b/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; }