diff --git a/gen/arrays.cpp b/gen/arrays.cpp index 5afa5f1e..ddf9a7b4 100644 --- a/gen/arrays.cpp +++ b/gen/arrays.cpp @@ -934,10 +934,9 @@ DValue* DtoCastArray(DValue* u, Type* to) Logger::cout() << "from array or sarray" << '\n'; if (totype->ty == Tpointer) { Logger::cout() << "to pointer" << '\n'; - assert(fromtype->next == totype->next || totype->next->ty == Tvoid); rval = DtoArrayPtr(u); - if (fromtype->next != totype->next) - rval = gIR->ir->CreateBitCast(rval, getPtrToType(llvm::Type::Int8Ty), "tmp"); + if (rval->getType() != tolltype) + rval = gIR->ir->CreateBitCast(rval, tolltype, "tmp"); } else if (totype->ty == Tarray) { Logger::cout() << "to array" << '\n'; diff --git a/gen/classes.cpp b/gen/classes.cpp index 3ac98e8e..6fe8e923 100644 --- a/gen/classes.cpp +++ b/gen/classes.cpp @@ -124,18 +124,20 @@ void DtoResolveClass(ClassDeclaration* cd) } }*/ + // push state gIR->structs.push_back(irstruct); gIR->classes.push_back(cd); + // vector holding the field types + std::vector fieldtypes; + // add vtable ts->ir.vtblType = new llvm::PATypeHolder(llvm::OpaqueType::get()); const llvm::Type* vtabty = getPtrToType(ts->ir.vtblType->get()); - - std::vector fieldtypes; fieldtypes.push_back(vtabty); // add monitor - fieldtypes.push_back(getPtrToType(llvm::Type::Int8Ty)); + fieldtypes.push_back(getVoidPtrType()); // add base class data fields first LLVM_AddBaseClassData(&cd->baseclasses); @@ -241,7 +243,11 @@ void DtoResolveClass(ClassDeclaration* cd) fieldtypes.push_back(ivtblTy); // fix the interface vtable type + #if OPAQUE_VTBLS + iri->vtblTy = isaArray(itc->ir.vtblType->get()); + #else iri->vtblTy = isaStruct(itc->ir.vtblType->get()); + #endif // set index iri->index = interIdx++; @@ -273,6 +279,12 @@ void DtoResolveClass(ClassDeclaration* cd) // create vtable type llvm::GlobalVariable* svtblVar = 0; +#if OPAQUE_VTBLS + // void*[vtbl.dim] + const llvm::ArrayType* svtbl_ty + = llvm::ArrayType::get(getVoidPtrType(), cd->vtbl.dim); + +#else std::vector sinits_ty; for (int k=0; k < cd->vtbl.dim; k++) @@ -310,19 +322,29 @@ void DtoResolveClass(ClassDeclaration* cd) assert(0); } + // get type assert(!sinits_ty.empty()); const llvm::StructType* svtbl_ty = llvm::StructType::get(sinits_ty); - - std::string styname(cd->mangle()); - styname.append("__vtblType"); - gIR->module->addTypeName(styname, svtbl_ty); +#endif // refine for final vtable type llvm::cast(ts->ir.vtblType->get())->refineAbstractTypeTo(svtbl_ty); +#if !OPAQUE_VTBLS + // name vtbl type + std::string styname(cd->mangle()); + styname.append("__vtblType"); + gIR->module->addTypeName(styname, svtbl_ty); +#endif + + // log + Logger::cout() << "final class type: " << *ts->ir.type->get() << '\n'; + + // pop state gIR->classes.pop_back(); gIR->structs.pop_back(); + // queue declare gIR->declareList.push_back(cd); } @@ -359,9 +381,7 @@ void DtoDeclareClass(ClassDeclaration* cd) std::string varname("_D"); varname.append(cd->mangle()); varname.append("6__vtblZ"); - - const llvm::StructType* svtbl_ty = isaStruct(ts->ir.vtblType->get()); - cd->ir.irStruct->vtbl = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); + cd->ir.irStruct->vtbl = new llvm::GlobalVariable(ts->ir.vtblType->get(), true, _linkage, 0, varname, gIR->module); } // get interface info type @@ -445,7 +465,11 @@ void DtoConstInitClass(ClassDeclaration* cd) assert(cd->type->ty == Tclass); TypeClass* ts = (TypeClass*)cd->type; const llvm::StructType* structtype = isaStruct(ts->ir.type->get()); +#if OPAQUE_VTBLS + const llvm::ArrayType* vtbltype = isaArray(ts->ir.vtblType->get()); +#else const llvm::StructType* vtbltype = isaStruct(ts->ir.vtblType->get()); +#endif // make sure each offset knows its default initializer for (IrStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) @@ -543,37 +567,40 @@ void DtoConstInitClass(ClassDeclaration* cd) assert(dsym); //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; + #if OPAQUE_VTBLS + const llvm::Type* targetTy = getVoidPtrType(); + #else + const llvm::Type* targetTy = vtbltype->getElementType(k); + #endif + + llvm::Constant* c = NULL; + // virtual method if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { DtoForceDeclareDsymbol(fd); assert(fd->ir.irFunc->func); - llvm::Constant* c = llvm::cast(fd->ir.irFunc->func); - // cast if necessary (overridden method) - if (c->getType() != vtbltype->getElementType(k)) - c = llvm::ConstantExpr::getBitCast(c, vtbltype->getElementType(k)); - sinits.push_back(c); + c = llvm::cast(fd->ir.irFunc->func); } + // classinfo else if (ClassDeclaration* cd2 = dsym->isClassDeclaration()) { assert(cd->ir.irStruct->classInfo); - llvm::Constant* c = cd->ir.irStruct->classInfo; - sinits.push_back(c); + c = cd->ir.irStruct->classInfo; } - else - assert(0); - } + assert(c != NULL); + // cast if necessary (overridden method) + if (c->getType() != targetTy) + c = llvm::ConstantExpr::getBitCast(c, targetTy); + sinits.push_back(c); + } + #if OPAQUE_VTBLS + const llvm::ArrayType* svtbl_ty = isaArray(ts->ir.vtblType->get()); + llvm::Constant* cvtblInit = llvm::ConstantArray::get(svtbl_ty, sinits); + cd->ir.irStruct->constVtbl = llvm::cast(cvtblInit); + #else const llvm::StructType* svtbl_ty = isaStruct(ts->ir.vtblType->get()); - -#if 0 - for (size_t i=0; i< sinits.size(); ++i) - { - Logger::cout() << "field[" << i << "] = " << *svtbl_ty->getElementType(i) << '\n'; - Logger::cout() << "init [" << i << "] = " << *sinits[i]->getType() << '\n'; - assert(svtbl_ty->getElementType(i) == sinits[i]->getType()); - } -#endif - llvm::Constant* cvtblInit = llvm::ConstantStruct::get(svtbl_ty, sinits); cd->ir.irStruct->constVtbl = llvm::cast(cvtblInit); + #endif // create interface vtable const initalizers for (IrStruct::InterfaceVectorIter i=irstruct->interfaceVec.begin(); i!=irstruct->interfaceVec.end(); ++i) @@ -585,7 +612,11 @@ void DtoConstInitClass(ClassDeclaration* cd) assert(id->type->ty == Tclass); TypeClass* its = (TypeClass*)id->type; + #if OPAQUE_VTBLS + const llvm::ArrayType* ivtbl_ty = isaArray(its->ir.vtblType->get()); + #else const llvm::StructType* ivtbl_ty = isaStruct(its->ir.vtblType->get()); + #endif // generate interface info initializer std::vector infoInits; @@ -613,7 +644,12 @@ void DtoConstInitClass(ClassDeclaration* cd) std::vector iinits; // add interface info + #if OPAQUE_VTBLS + const llvm::Type* targetTy = getVoidPtrType(); + iinits.push_back(llvm::ConstantExpr::getBitCast(iri->info, targetTy)); + #else iinits.push_back(iri->info); + #endif for (int k=1; k < b->vtbl.dim; k++) { @@ -626,22 +662,24 @@ void DtoConstInitClass(ClassDeclaration* cd) assert(fd->ir.irFunc->func); llvm::Constant* c = llvm::cast(fd->ir.irFunc->func); + #if !OPAQUE_VTBLS + const llvm::Type* targetTy = iri->vtblTy->getContainedType(k); + #endif + // we have to bitcast, as the type created in ResolveClass expects a different this type - c = llvm::ConstantExpr::getBitCast(c, iri->vtblTy->getContainedType(k)); + c = llvm::ConstantExpr::getBitCast(c, targetTy); iinits.push_back(c); + Logger::cout() << "c: " << *c << '\n'; } - #if 0 - for (size_t x=0; x< iinits.size(); ++x) - { - Logger::cout() << "field[" << x << "] = " << *ivtbl_ty->getElementType(x) << "\n\n"; - Logger::cout() << "init [" << x << "] = " << *iinits[x] << "\n\n"; - assert(ivtbl_ty->getElementType(x) == iinits[x]->getType()); - } - #endif - + #if OPAQUE_VTBLS + Logger::cout() << "n: " << iinits.size() << " ivtbl_ty: " << *ivtbl_ty << '\n'; + llvm::Constant* civtblInit = llvm::ConstantArray::get(ivtbl_ty, iinits); + iri->vtblInit = llvm::cast(civtblInit); + #else llvm::Constant* civtblInit = llvm::ConstantStruct::get(ivtbl_ty, iinits); iri->vtblInit = llvm::cast(civtblInit); + #endif } } // we always generate interfaceinfos as best we can @@ -1171,7 +1209,7 @@ llvm::Value* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl) assert(DtoDType(inst->getType())->ty == Tclass); llvm::Value* vthis = inst->getRVal(); - //Logger::cout() << "vthis: " << *vthis << '\n'; + Logger::cout() << "vthis: " << *vthis << '\n'; llvm::Value* funcval; funcval = DtoGEPi(vthis, 0, 0, "tmp"); @@ -1179,6 +1217,13 @@ llvm::Value* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl) funcval = DtoGEPi(funcval, 0, fdecl->vtblIndex, fdecl->toPrettyChars()); funcval = DtoLoad(funcval); + Logger::cout() << "funcval: " << *funcval << '\n'; + +#if OPAQUE_VTBLS + funcval = DtoBitCast(funcval, getPtrToType(DtoType(fdecl->type))); + Logger::cout() << "funcval casted: " << *funcval << '\n'; +#endif + //assert(funcval->getType() == DtoType(fdecl->type)); //cc = DtoCallingConv(fdecl->linkage); diff --git a/gen/toir.cpp b/gen/toir.cpp index 0e2eb6e3..13ffd01d 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -1474,6 +1474,10 @@ DValue* DotVarExp::toElem(IRState* p) funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); funcval = DtoGEP(funcval, zero, vtblidx, toChars(), p->scopebb()); funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); + #if OPAQUE_VTBLS + funcval = DtoBitCast(funcval, getPtrToType(DtoType(fdecl->type))); + Logger::cout() << "funcval casted: " << *funcval << '\n'; + #endif //assert(funcval->getType() == DtoType(fdecl->type)); //cc = DtoCallingConv(fdecl->linkage); } diff --git a/gen/tollvm.cpp b/gen/tollvm.cpp index 98ac51f6..8ccd6715 100644 --- a/gen/tollvm.cpp +++ b/gen/tollvm.cpp @@ -1428,6 +1428,11 @@ const llvm::PointerType* getPtrToType(const llvm::Type* t) return llvm::PointerType::get(t, 0); } +const llvm::PointerType* getVoidPtrType() +{ + return getPtrToType(llvm::Type::Int8Ty); +} + llvm::ConstantPointerNull* getNullPtr(const llvm::Type* t) { const llvm::PointerType* pt = llvm::cast(t); diff --git a/gen/tollvm.h b/gen/tollvm.h index 9dd68146..b3c17880 100644 --- a/gen/tollvm.h +++ b/gen/tollvm.h @@ -123,6 +123,7 @@ llvm::GlobalVariable* isaGlobalVar(llvm::Value* v); // llvm::T::get(...) wrappers const llvm::PointerType* getPtrToType(const llvm::Type* t); +const llvm::PointerType* getVoidPtrType(); llvm::ConstantPointerNull* getNullPtr(const llvm::Type* t); // type sizes diff --git a/ir/irstruct.cpp b/ir/irstruct.cpp index 926aaba3..a5394421 100644 --- a/ir/irstruct.cpp +++ b/ir/irstruct.cpp @@ -4,7 +4,11 @@ #include "ir/irstruct.h" #include "gen/irstate.h" +#if OPAQUE_VTBLS +IrInterface::IrInterface(BaseClass* b, const llvm::ArrayType* vt) +#else IrInterface::IrInterface(BaseClass* b, const llvm::StructType* vt) +#endif { base = b; decl = b->base; diff --git a/ir/irstruct.h b/ir/irstruct.h index c280358f..9402b107 100644 --- a/ir/irstruct.h +++ b/ir/irstruct.h @@ -11,8 +11,13 @@ struct IrInterface : IrBase BaseClass* base; ClassDeclaration* decl; +#if OPAQUE_VTBLS + const llvm::ArrayType* vtblTy; + llvm::ConstantArray* vtblInit; +#else const llvm::StructType* vtblTy; llvm::ConstantStruct* vtblInit; +#endif llvm::GlobalVariable* vtbl; const llvm::StructType* infoTy; @@ -21,7 +26,11 @@ struct IrInterface : IrBase int index; +#if OPAQUE_VTBLS + IrInterface(BaseClass* b, const llvm::ArrayType* vt); +#else IrInterface(BaseClass* b, const llvm::StructType* vt); +#endif ~IrInterface(); }; @@ -67,7 +76,11 @@ public: bool constinited; llvm::GlobalVariable* vtbl; +#if OPAQUE_VTBLS + llvm::ConstantArray* constVtbl; +#else llvm::ConstantStruct* constVtbl; +#endif llvm::GlobalVariable* init; llvm::Constant* constInit; llvm::GlobalVariable* classInfo; diff --git a/ir/irtype.cpp b/ir/irtype.cpp index 12dcc998..78f7e301 100644 --- a/ir/irtype.cpp +++ b/ir/irtype.cpp @@ -21,7 +21,9 @@ IrType::IrType(const IrType& s) { assert(list.insert(this).second); type = s.type; +#if OPAQUE_VTBLS vtblType = s.type; +#endif } IrType::~IrType() @@ -32,5 +34,7 @@ IrType::~IrType() void IrType::reset() { type = NULL; +#if OPAQUE_VTBLS vtblType = NULL; +#endif } diff --git a/ir/irtype.h b/ir/irtype.h index c48db020..6f8f7ded 100644 --- a/ir/irtype.h +++ b/ir/irtype.h @@ -21,7 +21,9 @@ struct IrType void reset(); llvm::PATypeHolder* type; +#if !OPAQUE_VTBLS llvm::PATypeHolder* vtblType; +#endif }; #endif diff --git a/premake.lua b/premake.lua index 9063c1c9..543037b2 100644 --- a/premake.lua +++ b/premake.lua @@ -27,9 +27,13 @@ package.buildoptions = { "-x c++", "`llvm-config --cxxflags`" } package.linkoptions = { -- long but it's faster than just 'all' "`llvm-config --libs core asmparser bitreader bitwriter support target transformutils scalaropts ipo instrumentation x86 powerpc`", - "`llvm-config --ldflags`" + "`llvm-config --ldflags`", +} +package.defines = { + "IN_LLVM", + "_DH", + "OPAQUE_VTBLS=1", } -package.defines = { "IN_LLVM", "_DH" } package.config.Release.defines = { "LLVMD_NO_LOGGER" } package.config.Debug.buildoptions = { "-g -O0" } --package.targetprefix = "llvm" diff --git a/tango/lib/llvmdc-posix.mak b/tango/lib/llvmdc-posix.mak index 0b5fcdef..ff5de2bc 100644 --- a/tango/lib/llvmdc-posix.mak +++ b/tango/lib/llvmdc-posix.mak @@ -31,8 +31,8 @@ DC=llvmdc ADD_CFLAGS= ADD_DFLAGS= -targets : lib clib doc -all : lib clib doc +targets : lib doc +all : lib doc ######################################################