diff --git a/dmd/dsymbol.h b/dmd/dsymbol.h index fc341eaefd..f8e1652445 100644 --- a/dmd/dsymbol.h +++ b/dmd/dsymbol.h @@ -82,10 +82,9 @@ typedef union tree_node TYPE; struct TYPE; #endif -// llvm #if IN_LLVM -struct Ir; -struct IrSymbol; +class Ir; +class IrSymbol; namespace llvm { class Value; diff --git a/dmd/mars.h b/dmd/mars.h index 8d45ff40ba..adbacfa1e0 100644 --- a/dmd/mars.h +++ b/dmd/mars.h @@ -219,6 +219,7 @@ struct Param OUTPUTFLAG output_o; bool llvmAnnotate; bool useInlineAsm; + bool verbose_cg; // target stuff const char* llvmArch; diff --git a/dmd/module.h b/dmd/module.h index a60036a501..354fc69b3f 100644 --- a/dmd/module.h +++ b/dmd/module.h @@ -28,7 +28,7 @@ struct Library; // Back end #if IN_LLVM -struct Ir; +class Ir; struct DValue; typedef DValue elem; namespace llvm { class Module; } diff --git a/dmd/mtype.h b/dmd/mtype.h index d1671b3232..46b46a5025 100644 --- a/dmd/mtype.h +++ b/dmd/mtype.h @@ -22,12 +22,9 @@ #include "expression.h" #if IN_LLVM -// llvm -#include "../ir/irdtype.h" #include "../ir/irfuncty.h" namespace llvm { class Type; } -struct Ir; - +class Ir; class IrType; #endif @@ -284,10 +281,7 @@ struct Type : Object virtual TypeBasic *isTypeBasic(); #if IN_LLVM - // LDC - IrDType ir; static Ir* sir; - IrType* irtype; #endif }; diff --git a/gen/cl_options.cpp b/gen/cl_options.cpp index b8318ad983..7e7873300b 100644 --- a/gen/cl_options.cpp +++ b/gen/cl_options.cpp @@ -44,6 +44,11 @@ static cl::opt verbose("v", cl::ZeroOrMore, cl::location(global.params.verbose)); +static cl::opt verbose_cg("v-cg", + cl::desc("Verbose codegen"), + cl::ZeroOrMore, + cl::location(global.params.verbose_cg)); + static cl::opt warnings("w", cl::desc("Enable warnings"), cl::ZeroOrMore, diff --git a/gen/functions.cpp b/gen/functions.cpp index bc2251c620..a006a6ff74 100644 --- a/gen/functions.cpp +++ b/gen/functions.cpp @@ -27,15 +27,10 @@ using namespace llvm::Attribute; const llvm::FunctionType* DtoFunctionType(Type* type, Type* thistype, Type* nesttype, bool ismain) { - // already built ? - if (type->ir.type != NULL) { - //assert(f->fty != NULL); - return llvm::cast(type->ir.type->get()); - } - if (Logger::enabled()) Logger::println("DtoFunctionType(%s)", type->toChars()); LOG_SCOPE + // sanity check assert(type->ty == Tfunction); TypeFunction* f = (TypeFunction*)type; @@ -165,27 +160,6 @@ const llvm::FunctionType* DtoFunctionType(Type* type, Type* thistype, Type* nest lidx++; } - // If the function type was forward referenced by one of the parameter types, - // it has now been set. - if (f->ir.type) { - // Notify ABI that we won't be needing it for this function type anymore. - abi->doneWithFunctionType(); - - // Some cleanup of memory we won't use - delete fty.ret; - delete fty.arg_sret; - delete fty.arg_this; - delete fty.arg_nest; - delete fty.arg_arguments; - delete fty.arg_argptr; - for (IrFuncTy::ArgIter It = fty.args.begin(), E = fty.args.end(); It != E; ++It) { - delete *It; - } - - Logger::cout() << "Final function type: " << **f->ir.type << '\n'; - return llvm::cast(*f->ir.type); - } - // Now we can modify f->fty safely. f->fty = fty; @@ -219,7 +193,6 @@ const llvm::FunctionType* DtoFunctionType(Type* type, Type* thistype, Type* nest } llvm::FunctionType* functype = llvm::FunctionType::get(f->fty.ret->ltype, argtypes, f->fty.c_vararg); - f->ir.type = new llvm::PATypeHolder(functype); #if 0 Logger::cout() << "Final function type: " << *functype << "\n"; @@ -232,11 +205,6 @@ const llvm::FunctionType* DtoFunctionType(Type* type, Type* thistype, Type* nest static const llvm::FunctionType* DtoVaFunctionType(FuncDeclaration* fdecl) { - // type has already been resolved - if (fdecl->type->ir.type != 0) { - return llvm::cast(fdecl->type->ir.type->get()); - } - TypeFunction* f = (TypeFunction*)fdecl->type; const llvm::FunctionType* fty = 0; @@ -256,7 +224,6 @@ static const llvm::FunctionType* DtoVaFunctionType(FuncDeclaration* fdecl) fty = GET_INTRINSIC_DECL(vaend)->getFunctionType(); assert(fty); - f->ir.type = new llvm::PATypeHolder(fty); return fty; } @@ -264,10 +231,6 @@ static const llvm::FunctionType* DtoVaFunctionType(FuncDeclaration* fdecl) const llvm::FunctionType* DtoFunctionType(FuncDeclaration* fdecl) { - // type has already been resolved - if (fdecl->type->ir.type != 0) - return llvm::cast(fdecl->type->ir.type->get()); - // handle for C vararg intrinsics if (fdecl->isVaIntrinsic()) return DtoVaFunctionType(fdecl); @@ -280,7 +243,7 @@ const llvm::FunctionType* DtoFunctionType(FuncDeclaration* fdecl) dthis = ad->type; const LLType* thisty = DtoType(dthis); //Logger::cout() << "this llvm type: " << *thisty << '\n'; - if (isaStruct(thisty) || (!gIR->structs.empty() && thisty == gIR->topstruct()->type->ir.type->get())) + if (ad->isStructDeclaration()) thisty = getPtrToType(thisty); } else { @@ -326,6 +289,9 @@ void DtoResolveFunction(FuncDeclaration* fdecl) return; // ignore declaration completely } + if (fdecl->ir.resolved) return; + fdecl->ir.resolved = true; + //printf("resolve function: %s\n", fdecl->toPrettyChars()); if (fdecl->parent) @@ -373,10 +339,7 @@ void DtoResolveFunction(FuncDeclaration* fdecl) } } - DtoFunctionType(fdecl); - - if (fdecl->ir.resolved) return; - fdecl->ir.resolved = true; + DtoType(fdecl->type); Logger::println("DtoResolveFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars()); LOG_SCOPE; @@ -515,7 +478,6 @@ void DtoDeclareFunction(FuncDeclaration* fdecl) func->setCallingConv(llvm::CallingConv::C); fdecl->ir.irFunc->func = func; - assert(llvm::isa(f->ir.type->get())); // parameter attributes if (!fdecl->isIntrinsic()) { @@ -646,7 +608,7 @@ void DtoDefineFunction(FuncDeclaration* fd) Type* t = fd->type->toBasetype(); TypeFunction* f = (TypeFunction*)t; - assert(f->ir.type); + assert(f->irtype); llvm::Function* func = fd->ir.irFunc->func; const llvm::FunctionType* functype = func->getFunctionType(); diff --git a/gen/functions.h b/gen/functions.h index bf389a1dc5..427f1e308e 100644 --- a/gen/functions.h +++ b/gen/functions.h @@ -1,6 +1,18 @@ #ifndef LDC_GEN_FUNCTIONS_H #define LDC_GEN_FUNCTIONS_H +#include "mars.h" + +struct FuncDeclaration; +struct Type; + +struct IRAsmBlock; + +namespace llvm +{ + class Value; +} + const llvm::FunctionType* DtoFunctionType(Type* t, Type* thistype, Type* nesttype, bool ismain = false); const llvm::FunctionType* DtoFunctionType(FuncDeclaration* fdecl); @@ -14,6 +26,6 @@ void DtoDefineNakedFunction(FuncDeclaration* fd); void emitABIReturnAsmStmt(IRAsmBlock* asmblock, Loc loc, FuncDeclaration* fdecl); DValue* DtoArgument(Argument* fnarg, Expression* argexp); -void DtoVariadicArgument(Expression* argexp, LLValue* dst); +void DtoVariadicArgument(Expression* argexp, llvm::Value* dst); #endif diff --git a/gen/tocall.cpp b/gen/tocall.cpp index 222d5c4700..0a0a502915 100644 --- a/gen/tocall.cpp +++ b/gen/tocall.cpp @@ -260,9 +260,8 @@ DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions* // the callee D type Type* calleeType = fnval->getType(); - // if the type has not yet been processed, do so now - if (calleeType->ir.type == NULL) - DtoType(calleeType); + // make sure the callee type has been processed + DtoType(calleeType); // get func value if any DFuncValue* dfnval = fnval->isFunc(); diff --git a/gen/tollvm.cpp b/gen/tollvm.cpp index 3653cdd1c9..a6598a8f72 100644 --- a/gen/tollvm.cpp +++ b/gen/tollvm.cpp @@ -24,6 +24,7 @@ #include "ir/irtype.h" #include "ir/irtypeclass.h" +#include "ir/irtypefunction.h" bool DtoIsPassedByRef(Type* type) { @@ -131,27 +132,15 @@ const LLType* DtoType(Type* t) // functions case Tfunction: { - if (!t->ir.type || *t->ir.type == NULL) { - TypeFunction* tf = (TypeFunction*)t; - if (tf->funcdecl) - return DtoFunctionType(tf->funcdecl); - else - return DtoFunctionType(tf,NULL,NULL); - } - else { - return t->ir.type->get(); - } + t->irtype = new IrTypeFunction(t); + return t->irtype->buildType(); } // delegates case Tdelegate: { - if (!t->ir.type || *t->ir.type == NULL) { - return DtoDelegateType(t); - } - else { - return t->ir.type->get(); - } + t->irtype = new IrTypeDelegate(t); + return t->irtype->buildType(); } // typedefs @@ -220,19 +209,6 @@ const LLType* DtoTypeNotVoid(Type* t) ////////////////////////////////////////////////////////////////////////////////////////// -const LLStructType* DtoDelegateType(Type* t) -{ - assert(t->ty == Tdelegate); - const LLType* i8ptr = getVoidPtrType(); - const LLType* func = DtoFunctionType(t->nextOf(), NULL, Type::tvoid->pointerTo()); - const LLType* funcptr = getPtrToType(func); - const LLStructType* dgtype = LLStructType::get(i8ptr, funcptr, NULL); - gIR->module->addTypeName(t->toChars(), dgtype); - return dgtype; -} - -////////////////////////////////////////////////////////////////////////////////////////// - LLValue* DtoDelegateEquals(TOK op, LLValue* lhs, LLValue* rhs) { Logger::println("Doing delegate equality"); @@ -799,7 +775,7 @@ const LLStructType* DtoInterfaceInfoType() // ClassInfo classinfo ClassDeclaration* cd2 = ClassDeclaration::classinfo; DtoResolveClass(cd2); - types.push_back(getPtrToType(cd2->type->ir.type->get())); + types.push_back(DtoType(cd2->type)); // void*[] vtbl std::vector vtbltypes; vtbltypes.push_back(DtoSize_t()); diff --git a/gen/tollvm.h b/gen/tollvm.h index 2d80905fc0..9b1a4c5a2d 100644 --- a/gen/tollvm.h +++ b/gen/tollvm.h @@ -26,7 +26,6 @@ unsigned DtoShouldExtend(Type* type); //const LLType* DtoStructTypeFromArguments(Arguments* arguments); // delegate helpers -const LLStructType* DtoDelegateType(Type* t); LLValue* DtoDelegateEquals(TOK op, LLValue* lhs, LLValue* rhs); // return linkage type for symbol using the current ir state for context diff --git a/gen/toobj.cpp b/gen/toobj.cpp index c55544d6a7..e4fdef1d4b 100644 --- a/gen/toobj.cpp +++ b/gen/toobj.cpp @@ -54,6 +54,7 @@ #include "ir/irvar.h" #include "ir/irmodule.h" +#include "ir/irtype.h" ////////////////////////////////////////////////////////////////////////////////////////// @@ -80,7 +81,8 @@ llvm::Module* Module::genLLVMModule(Ir* sir) Logger::println("Generating module: %s\n", (md ? md->toChars() : toChars())); LOG_SCOPE; - //printf("codegen: %s\n", srcfile->toChars()); + if (global.params.verbose_cg) + printf("codegen: %s (%s)\n", toPrettyChars(), srcfile->toChars()); assert(!global.errors); @@ -609,9 +611,9 @@ void Module::genmoduleinfo() } // moduleinfo llvm struct type - const llvm::StructType* moduleinfoTy = isaStruct(moduleinfo->type->ir.type->get()); + const llvm::StructType* moduleinfoTy = isaStruct(moduleinfo->type->irtype->getPA()); // classinfo llvm struct type - const llvm::StructType* classinfoTy = isaStruct(ClassDeclaration::classinfo->type->ir.type->get()); + const llvm::StructType* classinfoTy = isaStruct(ClassDeclaration::classinfo->type->irtype->getPA()); // initializer vector std::vector initVec; diff --git a/gen/typinf.cpp b/gen/typinf.cpp index bd8b72b78c..cc5115f425 100644 --- a/gen/typinf.cpp +++ b/gen/typinf.cpp @@ -43,6 +43,7 @@ #include "gen/metadata.h" #include "ir/irvar.h" +#include "ir/irtype.h" /******************************************* * Get a canonicalized form of the TypeInfo for use with the internal @@ -331,7 +332,7 @@ void DtoDeclareTypeInfo(TypeInfoDeclaration* tid) // this is a declaration of a builtin __initZ var if (tid->tinfo->builtinTypeInfo()) { // fixup the global - const llvm::Type* rty = Type::typeinfo->type->ir.type->get(); + const llvm::Type* rty = Type::typeinfo->type->irtype->getPA().get(); llvm::cast(irg->type.get())->refineAbstractTypeTo(rty); LLGlobalVariable* g = isaGlobalVar(irg->value); g->setLinkage(llvm::GlobalValue::ExternalLinkage); @@ -544,7 +545,7 @@ void TypeInfoStaticArrayDeclaration::llvmDefine() base->codegen(Type::sir); // get type of typeinfo class - const LLStructType* stype = isaStruct(base->type->ir.type->get()); + const LLStructType* stype = isaStruct(base->type->irtype->getPA().get()); // initializer vector std::vector sinits; @@ -665,7 +666,7 @@ void TypeInfoStructDeclaration::llvmDefine() ClassDeclaration* base = Type::typeinfostruct; base->codegen(Type::sir); - const LLStructType* stype = isaStruct(base->type->ir.type->get()); + const LLStructType* stype = isaStruct(base->type->irtype->getPA()); // vtbl std::vector sinits; @@ -691,7 +692,7 @@ void TypeInfoStructDeclaration::llvmDefine() else #endif { - size_t cisize = getTypeStoreSize(tc->ir.type->get()); + size_t cisize = getTypeStoreSize(tc->irtype->getPA().get()); LLConstant* cicast = llvm::ConstantExpr::getBitCast(sd->ir.irStruct->getInitSymbol(), initpt); sinits.push_back(DtoConstSlice(DtoConstSize_t(cisize), cicast)); } @@ -903,7 +904,7 @@ void TypeInfoInterfaceDeclaration::llvmDefine() base->codegen(Type::sir); // get type of typeinfo class - const LLStructType* stype = isaStruct(base->type->ir.type->get()); + const LLStructType* stype = isaStruct(base->type->irtype->getPA()); // initializer vector std::vector sinits; @@ -939,7 +940,7 @@ void TypeInfoTupleDeclaration::llvmDefine() base->codegen(Type::sir); // get type of typeinfo class - const LLStructType* stype = isaStruct(base->type->ir.type->get()); + const LLStructType* stype = isaStruct(base->type->irtype->getPA()); // initializer vector std::vector sinits; @@ -956,7 +957,7 @@ void TypeInfoTupleDeclaration::llvmDefine() size_t dim = tu->arguments->dim; std::vector arrInits; - const LLType* tiTy = Type::typeinfo->type->ir.type->get(); + const LLType* tiTy = Type::typeinfo->type->irtype->getPA(); tiTy = getPtrToType(tiTy); for (size_t i = 0; i < dim; i++) diff --git a/ir/ir.h b/ir/ir.h index c6aef5fe0a..3f60780c12 100644 --- a/ir/ir.h +++ b/ir/ir.h @@ -16,8 +16,9 @@ struct IrBase : Object virtual ~IrBase() {} }; -struct Ir +class Ir { +public: Ir(); void setState(IRState* p) { irs = p; } diff --git a/ir/irdtype.cpp b/ir/irdtype.cpp deleted file mode 100644 index 373915f11c..0000000000 --- a/ir/irdtype.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "gen/llvm.h" -#include "ir/ir.h" -#include "ir/irdtype.h" - -IrDType::IrDType() -{ - type = NULL; -} diff --git a/ir/irdtype.h b/ir/irdtype.h deleted file mode 100644 index 8751ebea13..0000000000 --- a/ir/irdtype.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef LDC_IR_IRDTYPE_H -#define LDC_IR_IRDTYPE_H - -#include - -namespace llvm { - class PATypeHolder; -} - -struct IrDType -{ - IrDType(); - llvm::PATypeHolder* type; -}; - -#endif diff --git a/ir/irsymbol.h b/ir/irsymbol.h index 635c8da539..ffa609fab2 100644 --- a/ir/irsymbol.h +++ b/ir/irsymbol.h @@ -4,8 +4,9 @@ #include "ir/ir.h" /// Base class for all symbols. -struct IrSymbol +class IrSymbol { +public: /// IrSymbol(Ir* ir) : ir(ir) {} diff --git a/ir/irtype.cpp b/ir/irtype.cpp index c07ecc20b9..998d6a8422 100644 --- a/ir/irtype.cpp +++ b/ir/irtype.cpp @@ -18,8 +18,7 @@ IrType::IrType(Type* dt, const llvm::Type* lt) { assert(dt && "null D Type"); assert(lt && "null LLVM Type"); - assert(dt->ir.type == NULL && "llvm type (old one) already set"); - dt->ir.type = &pa; + assert(dt->irtype == NULL && "already has IrType"); } ////////////////////////////////////////////////////////////////////////////// diff --git a/ir/irtype.h b/ir/irtype.h index ee8d6e19d3..864ac68e96 100644 --- a/ir/irtype.h +++ b/ir/irtype.h @@ -13,6 +13,7 @@ class IrTypeAggr; class IrTypeArray; class IrTypeBasic; class IrTypeClass; +class IrTypeFunction; class IrTypePointer; class IrTypeSArray; class IrTypeStruct; @@ -35,6 +36,8 @@ public: /// virtual IrTypeClass* isClass() { return NULL; } /// + virtual IrTypeFunction* isFunction(){ return NULL; } + /// virtual IrTypePointer* isPointer() { return NULL; } /// virtual IrTypeSArray* isSArray() { return NULL; } diff --git a/ir/irtypefunction.cpp b/ir/irtypefunction.cpp new file mode 100644 index 0000000000..a6a9d1dc01 --- /dev/null +++ b/ir/irtypefunction.cpp @@ -0,0 +1,47 @@ +#include "llvm/DerivedTypes.h" +#include "mtype.h" + +#include "gen/irstate.h" +#include "gen/tollvm.h" +#include "gen/functions.h" + +#include "ir/irtypefunction.h" + +IrTypeFunction::IrTypeFunction(Type * dt) +: IrType(dt, llvm::OpaqueType::get()) +{ + irfty = NULL; +} + +const llvm::Type * IrTypeFunction::buildType() +{ + const llvm::Type* T; + TypeFunction* tf = (TypeFunction*)dtype; + if (tf->funcdecl) + T = DtoFunctionType(tf->funcdecl); + else + T = DtoFunctionType(tf,NULL,NULL); + + llvm::cast(pa.get())->refineAbstractTypeTo(T); + return pa.get(); +} + +////////////////////////////////////////////////////////////////////////////// + +IrTypeDelegate::IrTypeDelegate(Type * dt) +: IrType(dt, llvm::OpaqueType::get()) +{ +} + +const llvm::Type * IrTypeDelegate::buildType() +{ + assert(dtype->ty == Tdelegate); + const LLType* i8ptr = getVoidPtrType(); + const LLType* func = DtoFunctionType(dtype->nextOf(), NULL, Type::tvoid->pointerTo()); + const LLType* funcptr = getPtrToType(func); + const LLStructType* dgtype = LLStructType::get(i8ptr, funcptr, NULL); + gIR->module->addTypeName(dtype->toChars(), dgtype); + + llvm::cast(pa.get())->refineAbstractTypeTo(dgtype); + return pa.get(); +} diff --git a/ir/irtypefunction.h b/ir/irtypefunction.h new file mode 100644 index 0000000000..d14f4af9b8 --- /dev/null +++ b/ir/irtypefunction.h @@ -0,0 +1,42 @@ +#ifndef __LDC_IR_IRTYPEFUNCTION_H__ +#define __LDC_IR_IRTYPEFUNCTION_H__ + +#include "ir/irtype.h" + +class IrFuncTy; + +/// +class IrTypeFunction : public IrType +{ +public: + /// + IrTypeFunction(Type* dt); + + /// + IrTypeFunction* isFunction() { return this; } + + /// + const llvm::Type* buildType(); + + IrFuncTy* fty() { return irfty; } + +protected: + /// + IrFuncTy* irfty; +}; + +/// +class IrTypeDelegate : public IrType +{ +public: + /// + IrTypeDelegate(Type* dt); + + /// + IrTypeDelegate* isDelegate() { return this; } + + /// + const llvm::Type* buildType(); +}; + +#endif