From da593c99dc01b393ab082baadce0e6de34df49ac Mon Sep 17 00:00:00 2001 From: Frits van Bommel Date: Sat, 30 May 2009 13:04:49 +0200 Subject: [PATCH] Remove code duplication for vtable loads and improve instruction naming to make bitcode with virtual calls easier to read. --- gen/classes.cpp | 10 ++++++++-- gen/classes.h | 2 +- gen/toir.cpp | 20 +++----------------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/gen/classes.cpp b/gen/classes.cpp index 2ac7be9f..ec20de4d 100644 --- a/gen/classes.cpp +++ b/gen/classes.cpp @@ -514,7 +514,7 @@ LLValue* DtoIndexClass(LLValue* src, ClassDeclaration* cd, VarDeclaration* vd) ////////////////////////////////////////////////////////////////////////////////////////// -LLValue* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl) +LLValue* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl, char* name) { // sanity checks assert(fdecl->isVirtual()); @@ -533,7 +533,9 @@ LLValue* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl) // load vtbl ptr funcval = DtoLoad(funcval); // index vtbl - funcval = DtoGEPi(funcval, 0, fdecl->vtblIndex, fdecl->toChars()); + std::string vtblname = name; + vtblname.append("@vtbl"); + funcval = DtoGEPi(funcval, 0, fdecl->vtblIndex, vtblname.c_str()); // load funcptr funcval = DtoAlignedLoad(funcval); @@ -542,6 +544,10 @@ LLValue* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl) // cast to final funcptr type funcval = DtoBitCast(funcval, getPtrToType(DtoType(fdecl->type))); + + // postpone naming until after casting to get the name in call instructions + funcval->setName(name); + if (Logger::enabled()) Logger::cout() << "funcval casted: " << *funcval << '\n'; diff --git a/gen/classes.h b/gen/classes.h index 558fe9f5..4c65084f 100644 --- a/gen/classes.h +++ b/gen/classes.h @@ -32,6 +32,6 @@ DValue* DtoDynamicCastInterface(DValue* val, Type* to); LLValue* DtoIndexClass(LLValue* src, ClassDeclaration* sd, VarDeclaration* vd); -LLValue* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl); +LLValue* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl, char* name); #endif diff --git a/gen/toir.cpp b/gen/toir.cpp index 06cdb226..be72c50b 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -1134,22 +1134,8 @@ DValue* DotVarExp::toElem(IRState* p) assert(funcval); } else { - assert(fdecl->vtblIndex > 0); - assert(e1type->ty == Tclass); - - LLValue* zero = DtoConstUint(0); - size_t vtblidx = fdecl->vtblIndex; - if (Logger::enabled()) - Logger::cout() << "vthis: " << *vthis << '\n'; - funcval = DtoGEP(vthis, zero, zero); - funcval = DtoLoad(funcval); - Logger::println("vtblidx = %lu", vtblidx); - funcval = DtoGEP(funcval, zero, DtoConstUint(vtblidx), toChars()); - funcval = DtoLoad(funcval); - - funcval = DtoBitCast(funcval, getPtrToType(DtoType(fdecl->type))); - if (Logger::enabled()) - Logger::cout() << "funcval casted: " << *funcval << '\n'; + DImValue vthis3(e1type, vthis); + funcval = DtoVirtualFunctionPointer(&vthis3, fdecl, toChars()); } return new DFuncValue(fdecl, funcval, vthis2); @@ -2031,7 +2017,7 @@ DValue* DelegateExp::toElem(IRState* p) LLValue* castfptr; if (func->isVirtual() && !func->isFinal()) - castfptr = DtoVirtualFunctionPointer(u, func); + castfptr = DtoVirtualFunctionPointer(u, func, toChars()); else if (func->isAbstract()) assert(0 && "TODO delegate to abstract method"); else if (func->toParent()->isInterfaceDeclaration())