changeset 378:d8234836b40f

Get rid of runTimeHack and instead add proper argument info to the frontend declatation.
author Christian Kamm <kamm incasoftware de>
date Tue, 22 Jul 2008 19:24:40 +0200
parents 4731f3eed1f0
children d632801b15f0
files dmd/declaration.h dmd/func.c dmd/mtype.c dmd/statement.c gen/functions.cpp gen/toir.cpp
diffstat 6 files changed, 97 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/dmd/declaration.h	Mon Jul 21 20:36:03 2008 +0200
+++ b/dmd/declaration.h	Tue Jul 22 19:24:40 2008 +0200
@@ -626,8 +626,9 @@
     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 @@
     FuncDeclaration *isFuncDeclaration() { return this; }
 
     // llvmdc stuff
-    bool runTimeHack;
     std::set<VarDeclaration*> nestedVars;
 
     // we keep our own table of label statements as LabelDsymbolS
--- a/dmd/func.c	Mon Jul 21 20:36:03 2008 +0200
+++ b/dmd/func.c	Tue Jul 22 19:24:40 2008 +0200
@@ -74,8 +74,6 @@
     nrvo_can = 1;
     nrvo_var = NULL;
     shidden = NULL;
-    // llvmdc
-    runTimeHack = false;
 }
 
 Dsymbol *FuncDeclaration::syntaxCopy(Dsymbol *s)
@@ -2009,12 +2007,16 @@
  * 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 @@
     }
     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;
--- a/dmd/mtype.c	Mon Jul 21 20:36:03 2008 +0200
+++ b/dmd/mtype.c	Tue Jul 22 19:24:40 2008 +0200
@@ -1536,8 +1536,12 @@
 	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 @@
 	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 @@
 
 	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 @@
 	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 @@
 	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 @@
 	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 @@
 	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 @@
 	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));
--- a/dmd/statement.c	Mon Jul 21 20:36:03 2008 +0200
+++ b/dmd/statement.c	Tue Jul 22 19:24:40 2008 +0200
@@ -1598,11 +1598,24 @@
 		/* 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 @@
 		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();
--- a/gen/functions.cpp	Mon Jul 21 20:36:03 2008 +0200
+++ b/gen/functions.cpp	Tue Jul 22 19:24:40 2008 +0200
@@ -287,13 +287,6 @@
     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 @@
     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)
--- a/gen/toir.cpp	Mon Jul 21 20:36:03 2008 +0200
+++ b/gen/toir.cpp	Tue Jul 22 19:24:40 2008 +0200
@@ -1001,16 +1001,6 @@
     // 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 @@
 
             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 @@
 
     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;