diff --git a/dmd/declaration.h b/dmd/declaration.h index 51e28422..28425de0 100644 --- a/dmd/declaration.h +++ b/dmd/declaration.h @@ -626,8 +626,9 @@ struct FuncDeclaration : Declaration const char *kind(); void toDocBuffer(OutBuffer *buf); - static FuncDeclaration *genCfunc(Type *treturn, char *name); - static FuncDeclaration *genCfunc(Type *treturn, Identifier *id); +// LLVMDC: give argument types to runtime functions + static FuncDeclaration *genCfunc(Arguments *args, Type *treturn, char *name); + static FuncDeclaration *genCfunc(Arguments *args, Type *treturn, Identifier *id); Symbol *toSymbol(); Symbol *toThunkSymbol(int offset); // thunk version @@ -637,7 +638,6 @@ struct FuncDeclaration : Declaration FuncDeclaration *isFuncDeclaration() { return this; } // llvmdc stuff - bool runTimeHack; std::set nestedVars; // we keep our own table of label statements as LabelDsymbolS diff --git a/dmd/func.c b/dmd/func.c index 8676753e..d523a798 100644 --- a/dmd/func.c +++ b/dmd/func.c @@ -74,8 +74,6 @@ FuncDeclaration::FuncDeclaration(Loc loc, Loc endloc, Identifier *id, enum STC s nrvo_can = 1; nrvo_var = NULL; shidden = NULL; - // llvmdc - runTimeHack = false; } Dsymbol *FuncDeclaration::syntaxCopy(Dsymbol *s) @@ -2009,12 +2007,16 @@ int FuncDeclaration::addPostInvariant() * Generate a FuncDeclaration for a runtime library function. */ -FuncDeclaration *FuncDeclaration::genCfunc(Type *treturn, char *name) +// +// LLVMDC: Adjusted to give argument info to the runtime function decl. +// + +FuncDeclaration *FuncDeclaration::genCfunc(Arguments *args, Type *treturn, char *name) { - return genCfunc(treturn, Lexer::idPool(name)); + return genCfunc(args, treturn, Lexer::idPool(name)); } -FuncDeclaration *FuncDeclaration::genCfunc(Type *treturn, Identifier *id) +FuncDeclaration *FuncDeclaration::genCfunc(Arguments *args, Type *treturn, Identifier *id) { FuncDeclaration *fd; TypeFunction *tf; @@ -2036,7 +2038,7 @@ FuncDeclaration *FuncDeclaration::genCfunc(Type *treturn, Identifier *id) } else { - tf = new TypeFunction(NULL, treturn, 0, LINKc); + tf = new TypeFunction(args, treturn, 0, LINKc); fd = new FuncDeclaration(0, 0, id, STCstatic, tf); fd->protection = PROTpublic; fd->linkage = LINKc; diff --git a/dmd/mtype.c b/dmd/mtype.c index 4899041d..10994620 100644 --- a/dmd/mtype.c +++ b/dmd/mtype.c @@ -1536,8 +1536,12 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident) static char *name[2] = { "_adReverseChar", "_adReverseWchar" }; nm = name[n->ty == Twchar]; - fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), nm); - fd->runTimeHack = true; + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + Type* arrty = n->ty == Twchar ? Type::tchar->arrayOf() : Type::twchar->arrayOf(); + args->push(new Argument(STCin, arrty, NULL, NULL)); + fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), nm); + ec = new VarExp(0, fd); e = e->castTo(sc, n->arrayOf()); // convert to dynamic array arguments = new Expressions(); @@ -1554,8 +1558,12 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident) static char *name[2] = { "_adSortChar", "_adSortWchar" }; nm = name[n->ty == Twchar]; - fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), nm); - fd->runTimeHack = true; + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + Type* arrty = n->ty == Twchar ? Type::tchar->arrayOf() : Type::twchar->arrayOf(); + args->push(new Argument(STCin, arrty, NULL, NULL)); + fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), nm); + ec = new VarExp(0, fd); e = e->castTo(sc, n->arrayOf()); // convert to dynamic array arguments = new Expressions(); @@ -1573,8 +1581,16 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident) assert(size); dup = (ident == Id::dup); - fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), dup ? Id::adDup : Id::adReverse); - fd->runTimeHack = true; + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + if(dup) { + args->push(new Argument(STCin, Type::typeinfo->type, NULL, NULL)); + args->push(new Argument(STCin, Type::tvoid->arrayOf(), NULL, NULL)); + } else { + args->push(new Argument(STCin, Type::tvoid->arrayOf(), NULL, NULL)); + args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); + } + fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), dup ? Id::adDup : Id::adReverse); ec = new VarExp(0, fd); e = e->castTo(sc, n->arrayOf()); // convert to dynamic array arguments = new Expressions(); @@ -1592,9 +1608,12 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident) FuncDeclaration *fd; Expressions *arguments; - fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + args->push(new Argument(STCin, Type::tvoid->arrayOf(), NULL, NULL)); + args->push(new Argument(STCin, Type::typeinfo->type, NULL, NULL)); + fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), (char*)(n->ty == Tbit ? "_adSortBit" : "_adSort")); - fd->runTimeHack = true; ec = new VarExp(0, fd); e = e->castTo(sc, n->arrayOf()); // convert to dynamic array arguments = new Expressions(); @@ -2268,8 +2287,10 @@ Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident) FuncDeclaration *fd; Expressions *arguments; - fd = FuncDeclaration::genCfunc(Type::tsize_t, Id::aaLen); - fd->runTimeHack = true; + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + args->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + fd = FuncDeclaration::genCfunc(args, Type::tsize_t, Id::aaLen); ec = new VarExp(0, fd); arguments = new Expressions(); arguments->push(e); @@ -2284,8 +2305,11 @@ Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident) int size = key->size(e->loc); assert(size); - fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), Id::aaKeys); - fd->runTimeHack = true; + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + args->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); + fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::aaKeys); ec = new VarExp(0, fd); arguments = new Expressions(); arguments->push(e); @@ -2299,8 +2323,12 @@ Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident) FuncDeclaration *fd; Expressions *arguments; - fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), Id::aaValues); - fd->runTimeHack = true; + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + args->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); + args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); + fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::aaValues); ec = new VarExp(0, fd); arguments = new Expressions(); arguments->push(e); @@ -2317,8 +2345,11 @@ Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident) FuncDeclaration *fd; Expressions *arguments; - fd = FuncDeclaration::genCfunc(Type::tvoid->pointerTo(), Id::aaRehash); - fd->runTimeHack = true; + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + args->push(new Argument(STCin, Type::tvoidptr->pointerTo(), NULL, NULL)); + args->push(new Argument(STCin, Type::typeinfo->type, NULL, NULL)); + fd = FuncDeclaration::genCfunc(args, Type::tvoid->pointerTo(), Id::aaRehash); ec = new VarExp(0, fd); arguments = new Expressions(); arguments->push(e->addressOf(sc)); diff --git a/dmd/statement.c b/dmd/statement.c index 0f08a134..0875f94f 100644 --- a/dmd/statement.c +++ b/dmd/statement.c @@ -1598,11 +1598,24 @@ Statement *ForeachStatement::semantic(Scope *sc) /* Call: * _aaApply(aggr, keysize, flde) */ - if (dim == 2) - fdapply = FuncDeclaration::genCfunc(Type::tindex, "_aaApply2"); - else - fdapply = FuncDeclaration::genCfunc(Type::tindex, "_aaApply"); - fdapply->runTimeHack = true; + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + args->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + args->push(new Argument(STCin, Type::tsize_t, NULL, NULL)); + if (dim == 2) { + Arguments* dgargs = new Arguments; + dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + TypeDelegate* dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); + args->push(new Argument(STCin, dgty, NULL, NULL)); + fdapply = FuncDeclaration::genCfunc(args, Type::tindex, "_aaApply2"); + } else { + Arguments* dgargs = new Arguments; + dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + TypeDelegate* dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); + args->push(new Argument(STCin, dgty, NULL, NULL)); + fdapply = FuncDeclaration::genCfunc(args, Type::tindex, "_aaApply"); + } ec = new VarExp(0, fdapply); Expressions *exps = new Expressions(); exps->push(aggr); @@ -1643,8 +1656,23 @@ Statement *ForeachStatement::semantic(Scope *sc) const char *r = (op == TOKforeach_reverse) ? "R" : ""; int j = sprintf(fdname, "_aApply%s%.*s%d", r, 2, fntab[flag], dim); assert(j < sizeof(fdname)); - fdapply = FuncDeclaration::genCfunc(Type::tindex, fdname); - fdapply->runTimeHack = true; + //LLVMDC: Build arguments. + Arguments* args = new Arguments; + args->push(new Argument(STCin, Type::tvoid->arrayOf(), NULL, NULL)); + if (dim == 2) { + Arguments* dgargs = new Arguments; + dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + TypeDelegate* dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); + args->push(new Argument(STCin, dgty, NULL, NULL)); + fdapply = FuncDeclaration::genCfunc(args, Type::tindex, fdname); + } else { + Arguments* dgargs = new Arguments; + dgargs->push(new Argument(STCin, Type::tvoidptr, NULL, NULL)); + TypeDelegate* dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tindex, 0, LINKd)); + args->push(new Argument(STCin, dgty, NULL, NULL)); + fdapply = FuncDeclaration::genCfunc(args, Type::tindex, fdname); + } ec = new VarExp(0, fdapply); Expressions *exps = new Expressions(); diff --git a/gen/functions.cpp b/gen/functions.cpp index f13432c0..3d9a8d99 100644 --- a/gen/functions.cpp +++ b/gen/functions.cpp @@ -287,13 +287,6 @@ void DtoResolveFunction(FuncDeclaration* fdecl) Logger::println("DtoResolveFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars()); LOG_SCOPE; - if (fdecl->runTimeHack) { - gIR->declareList.push_back(fdecl); - TypeFunction* tf = (TypeFunction*)fdecl->type; - tf->llvmRetInPtr = DtoIsPassedByRef(tf->next); - return; - } - if (fdecl->parent) if (TemplateInstance* tinst = fdecl->parent->isTemplateInstance()) { @@ -391,25 +384,16 @@ void DtoDeclareFunction(FuncDeclaration* fdecl) Type* t = DtoDType(fdecl->type); TypeFunction* f = (TypeFunction*)t; - // runtime function special handling - if (fdecl->runTimeHack) { - Logger::println("runtime hack func chars: %s", fdecl->toChars()); - if (!fdecl->ir.irFunc) { - IrFunction* irfunc = new IrFunction(fdecl); - llvm::Function* llfunc = LLVM_D_GetRuntimeFunction(gIR->module, fdecl->toChars()); - fdecl->ir.irFunc = irfunc; - fdecl->ir.irFunc->func = llfunc; - } - return; - } - bool declareOnly = false; bool templInst = fdecl->parent && DtoIsTemplateInstance(fdecl->parent); if (!templInst && fdecl->getModule() != gIR->dmodule) { Logger::println("not template instance, and not in this module. declare only!"); Logger::println("current module: %s", gIR->dmodule->ident->toChars()); - Logger::println("func module: %s", fdecl->getModule()->ident->toChars()); + if(fdecl->getModule()) + Logger::println("func module: %s", fdecl->getModule()->ident->toChars()); + else + Logger::println("func not in a module, probably runtime"); declareOnly = true; } else if (fdecl->llvmInternal == LLVMva_start) diff --git a/gen/toir.cpp b/gen/toir.cpp index fb40ee63..23db8f01 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -1001,16 +1001,6 @@ DValue* CallExp::toElem(IRState* p) // TODO: use sret param attr if (retinptr) { llargs[j] = new llvm::AllocaInst(argiter->get()->getContainedType(0),"rettmp",p->topallocapoint()); - - if (dfn && dfn->func && dfn->func->runTimeHack) { - const LLType* rettype = getPtrToType(DtoType(type)); - if (llargs[j]->getType() != llfnty->getParamType(j)) { - Logger::println("llvmRunTimeHack==true - force casting return value param"); - Logger::cout() << "casting: " << *llargs[j] << " to type: " << *llfnty->getParamType(j) << '\n'; - llargs[j] = DtoBitCast(llargs[j], llfnty->getParamType(j)); - } - } - ++j; ++argiter; } @@ -1164,24 +1154,6 @@ DValue* CallExp::toElem(IRState* p) if (fnarg && fnarg->llvmByVal) palist = palist.addAttr(j+1, llvm::ParamAttr::ByVal); - - // this hack is necessary :/ - // thing is DMD doesn't create correct signatures for the DMD generated calls to the runtime. - // only the return type is right, no arguments (parameters==NULL) ... - if (dfn && dfn->func && dfn->func->runTimeHack) { - llvm::Function* fn = dfn->func->ir.irFunc->func; - assert(fn); - if (fn->getParamAttrs().paramHasAttr(j+1, llvm::ParamAttr::ByVal)) - palist = palist.addAttr(j+1, llvm::ParamAttr::ByVal); - - if (llfnty->getParamType(j) != NULL) { - if (llargs[j]->getType() != llfnty->getParamType(j)) { - Logger::println("llvmRunTimeHack==true - force casting argument"); - Logger::cout() << "casting: " << *llargs[j] << " to type: " << *llfnty->getParamType(j) << '\n'; - llargs[j] = DtoBitCast(llargs[j], llfnty->getParamType(j)); - } - } - } } } @@ -1205,15 +1177,6 @@ DValue* CallExp::toElem(IRState* p) LLValue* retllval = (retinptr) ? llargs[0] : call->get(); - if (retinptr && dfn && dfn->func && dfn->func->runTimeHack) { - const LLType* rettype = getPtrToType(DtoType(type)); - if (retllval->getType() != rettype) { - Logger::println("llvmRunTimeHack==true - force casting return value"); - Logger::cout() << "from: " << *retllval->getType() << " to: " << *rettype << '\n'; - retllval = DtoBitCast(retllval, rettype); - } - } - // set calling convention if (dfn && dfn->func) { int li = dfn->func->llvmInternal;