From fb31047dcd5d9c495838200b3659f489f982aba0 Mon Sep 17 00:00:00 2001 From: Alexey Prokhin Date: Mon, 29 Jul 2013 16:29:23 +0400 Subject: [PATCH] Prettify our C functions' signatures patch to have a chance to be included into dmd --- dmd2/arrayop.c | 2 +- dmd2/mtype.c | 131 ++++++++++++++++++++--------------------------- dmd2/statement.c | 96 +++++++++++++--------------------- 3 files changed, 93 insertions(+), 136 deletions(-) diff --git a/dmd2/arrayop.c b/dmd2/arrayop.c index d0bad81a..3f9f9a48 100644 --- a/dmd2/arrayop.c +++ b/dmd2/arrayop.c @@ -251,7 +251,7 @@ int isDruntimeArrayOp(Identifier *ident) ArrayOp *buildArrayOp(Identifier *ident, BinExp *exp, Scope *sc, Loc loc) { ArrayOp *op = new ArrayOp; -#if IN_LLVM +#if IN_LLVM // LDC: Build parameters. Parameters *fparams = new Parameters(); Expression *loopbody = exp->buildArrayLoop(fparams); if (isDruntimeArrayOp(ident)) diff --git a/dmd2/mtype.c b/dmd2/mtype.c index e7747c40..1baaa9b0 100644 --- a/dmd2/mtype.c +++ b/dmd2/mtype.c @@ -3624,37 +3624,24 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident, int f if (ident == Id::reverse && (n->ty == Tchar || n->ty == Twchar)) { - -#if IN_LLVM - Expression *ec; - - //LDC: Build arguments. - static FuncDeclaration *adReverseChar_fd = NULL; - if(!adReverseChar_fd) { - Parameters* args = new Parameters; - Type* arrty = Type::tchar->arrayOf(); - args->push(new Parameter(STCin, arrty, NULL, NULL)); - adReverseChar_fd = FuncDeclaration::genCfunc(args, arrty, "_adReverseChar"); - } - static FuncDeclaration *adReverseWchar_fd = NULL; - if(!adReverseWchar_fd) { - Parameters* args = new Parameters; - Type* arrty = Type::twchar->arrayOf(); - args->push(new Parameter(STCin, arrty, NULL, NULL)); - adReverseWchar_fd = FuncDeclaration::genCfunc(args, arrty, "_adReverseWchar"); - } - - if(n->ty == Twchar) - ec = new VarExp(Loc(), adReverseWchar_fd); - else - ec = new VarExp(Loc(), adReverseChar_fd); -#else static const char *name[2] = { "_adReverseChar", "_adReverseWchar" }; +#if IN_LLVM // LDC: Build parameters. + static FuncDeclaration *funcs[2] = { 0, 0 }; + int i = n->ty == Twchar; + if (!funcs[i]) { + Parameters *args = new Parameters; + Type *next = n->ty == Twchar ? Type::twchar : Type::tchar; + Type *arrty = next->arrayOf(); + args->push(new Parameter(STCin, arrty, NULL, NULL)); + funcs[i] = FuncDeclaration::genCfunc(args, arrty, name[i]); + } + FuncDeclaration *fd = funcs[i]; +#else const char *nm = name[n->ty == Twchar]; FuncDeclaration *fd = FuncDeclaration::genCfunc(Type::tindex, nm); - Expression *ec = new VarExp(Loc(), fd); #endif + Expression *ec = new VarExp(Loc(), fd); e = e->castTo(sc, n->arrayOf()); // convert to dynamic array Expressions *arguments = new Expressions(); arguments->push(e); @@ -3664,34 +3651,28 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident, int f else if (ident == Id::sort && (n->ty == Tchar || n->ty == Twchar)) { Expression *ec; + FuncDeclaration *fd; Expressions *arguments; + const char *nm; + static const char *name[2] = { "_adSortChar", "_adSortWchar" }; -#if IN_LLVM - //LDC: Build arguments. - static FuncDeclaration *adSortChar_fd = NULL; - if(!adSortChar_fd) { - Parameters* args = new Parameters; - Type* arrty = Type::tchar->arrayOf(); - args->push(new Parameter(STCin, arrty, NULL, NULL)); - adSortChar_fd = FuncDeclaration::genCfunc(args, arrty, "_adSortChar"); +#if IN_LLVM // LDC: Build parameters. + static FuncDeclaration *funcs[2] = { 0, 0 }; + int i = n->ty == Twchar; + if (!funcs[i]) { + Parameters *args = new Parameters; + Type *next = n->ty == Twchar ? Type::twchar : Type::tchar; + Type *arrty = next->arrayOf(); + args->push(new Parameter(STCin, arrty, NULL, NULL)); + funcs[i] = FuncDeclaration::genCfunc(args, arrty, name[i]); } - static FuncDeclaration *adSortWchar_fd = NULL; - if(!adSortWchar_fd) { - Parameters* args = new Parameters; - Type* arrty = Type::twchar->arrayOf(); - args->push(new Parameter(STCin, arrty, NULL, NULL)); - adSortWchar_fd = FuncDeclaration::genCfunc(args, arrty, "_adSortWchar"); - } - - if(n->ty == Twchar) - ec = new VarExp(Loc(), adSortWchar_fd); - else - ec = new VarExp(Loc(), adSortChar_fd); + fd = funcs[i]; #else nm = name[n->ty == Twchar]; fd = FuncDeclaration::genCfunc(Type::tindex, nm); - ec = new VarExp(Loc(), fd); #endif + ec = new VarExp(Loc(), fd); + e = e->castTo(sc, n->arrayOf()); // convert to dynamic array arguments = new Expressions(); arguments->push(e); @@ -3701,6 +3682,7 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident, int f else if (ident == Id::reverse || ident == Id::dup || ident == Id::idup) { Expression *ec; + FuncDeclaration *fd; Expressions *arguments; int size = next->size(e->loc); int dup; @@ -3708,31 +3690,31 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident, int f Expression *olde = e; assert(size); dup = (ident == Id::dup || ident == Id::idup); -#if IN_LLVM - //LDC: Build arguments. - static FuncDeclaration *adDup_fd = NULL; - if(!adDup_fd) { - Parameters* args = new Parameters; - args->push(new Parameter(STCin, Type::typeinfo->type, NULL, NULL)); - args->push(new Parameter(STCin, Type::tvoid->arrayOf(), NULL, NULL)); - adDup_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::adDup); +#if IN_LLVM // LDC: Build parameters. + Parameters *args = new Parameters; + if (dup) { + static FuncDeclaration *adDup_fd = 0; + if (!adDup_fd) { + Parameters* args = new Parameters; + args->push(new Parameter(STCin, Type::typeinfo->type, NULL, NULL)); + args->push(new Parameter(STCin, Type::tvoid->arrayOf(), NULL, NULL)); + adDup_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::adDup); + } + fd = adDup_fd; + } else { + static FuncDeclaration *adReverse_fd = 0; + if (!adReverse_fd) { + Parameters* args = new Parameters; + args->push(new Parameter(STCin, Type::tvoid->arrayOf(), NULL, NULL)); + args->push(new Parameter(STCin, Type::tsize_t, NULL, NULL)); + adReverse_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::adReverse); + } + fd = adReverse_fd; } - static FuncDeclaration *adReverse_fd = NULL; - if(!adReverse_fd) { - Parameters* args = new Parameters; - args->push(new Parameter(STCin, Type::tvoid->arrayOf(), NULL, NULL)); - args->push(new Parameter(STCin, Type::tsize_t, NULL, NULL)); - adReverse_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), Id::adReverse); - } - - if(dup) - ec = new VarExp(Loc(), adDup_fd); - else - ec = new VarExp(Loc(), adReverse_fd); #else fd = FuncDeclaration::genCfunc(Type::tindex, dup ? Id::adDup : Id::adReverse); - ec = new VarExp(Loc(), fd); #endif + ec = new VarExp(Loc(), fd); e = e->castTo(sc, n->arrayOf()); // convert to dynamic array arguments = new Expressions(); if (dup) @@ -3768,21 +3750,18 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident, int f Expression *ec; Expressions *arguments; -#if IN_LLVM - //LDC: Build arguments. - static FuncDeclaration *adSort_fd = NULL; - if(!adSort_fd) { +#if IN_LLVM // LDC: Build parameters. + static FuncDeclaration *fd = NULL; + if (!fd) { Parameters* args = new Parameters; args->push(new Parameter(STCin, Type::tvoid->arrayOf(), NULL, NULL)); args->push(new Parameter(STCin, Type::typeinfo->type, NULL, NULL)); - adSort_fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), "_adSort"); + fd = FuncDeclaration::genCfunc(args, Type::tvoid->arrayOf(), "_adSort"); } - - ec = new VarExp(Loc(), adSort_fd); #else fd = FuncDeclaration::genCfunc(tint32->arrayOf(), "_adSort"); - ec = new VarExp(Loc(), fd); #endif + ec = new VarExp(Loc(), fd); e = e->castTo(sc, n->arrayOf()); // convert to dynamic array arguments = new Expressions(); arguments->push(e); diff --git a/dmd2/statement.c b/dmd2/statement.c index 9d9c5454..28f4d77d 100644 --- a/dmd2/statement.c +++ b/dmd2/statement.c @@ -1989,11 +1989,6 @@ Lagain: { Expression *ec; Expression *e; -#if IN_LLVM - FuncDeclaration *fdapply; - TypeDelegate* dgty; - TypeDelegate* fldeTy; -#endif if (!checkForArgTypes()) { body = body->semanticNoScope(sc); @@ -2135,46 +2130,36 @@ Lagain: /* Call: * _aaApply(aggr, keysize, flde) */ -#if !IN_LLVM +#if IN_LLVM // LDC: Build parameters. + static const char *names[2] = { "_aaApply", "_aaApply2" }; + static FuncDeclaration *funcs[2] = { NULL, NULL }; + static TypeDelegate *dgs[2] = { NULL, NULL }; + FuncDeclaration *fdapply; - if (dim == 2) - fdapply = FuncDeclaration::genCfunc(Type::tindex, "_aaApply2"); - else - fdapply = FuncDeclaration::genCfunc(Type::tindex, "_aaApply"); -#else - //LDC: Build arguments. - static FuncDeclaration *aaApply2_fd = NULL; - static TypeDelegate* aaApply2_dg; - if(!aaApply2_fd) { + TypeDelegate *fldeTy; + + unsigned char i = dim == 2; + if (!funcs[i]) { Parameters* args = new Parameters; args->push(new Parameter(STCin, Type::tvoid->pointerTo(), NULL, NULL)); args->push(new Parameter(STCin, Type::tsize_t, NULL, NULL)); Parameters* dgargs = new Parameters; dgargs->push(new Parameter(STCin, Type::tvoidptr, NULL, NULL)); - dgargs->push(new Parameter(STCin, Type::tvoidptr, NULL, NULL)); - aaApply2_dg = new TypeDelegate(new TypeFunction(dgargs, Type::tint32, 0, LINKd)); - args->push(new Parameter(STCin, aaApply2_dg, NULL, NULL)); - aaApply2_fd = FuncDeclaration::genCfunc(args, Type::tint32, "_aaApply2"); - } - static FuncDeclaration *aaApply_fd = NULL; - static TypeDelegate* aaApply_dg; - if(!aaApply_fd) { - Parameters* args = new Parameters; - args->push(new Parameter(STCin, Type::tvoid->pointerTo(), NULL, NULL)); // FIXME: Real parameter type is AA. - args->push(new Parameter(STCin, Type::tsize_t, NULL, NULL)); - Parameters* dgargs = new Parameters; - dgargs->push(new Parameter(STCin, Type::tvoidptr, NULL, NULL)); - aaApply_dg = new TypeDelegate(new TypeFunction(dgargs, Type::tint32, 0, LINKd)); - args->push(new Parameter(STCin, aaApply_dg, NULL, NULL)); - aaApply_fd = FuncDeclaration::genCfunc(args, Type::tint32, "_aaApply"); - } - if (dim == 2) { - fdapply = aaApply2_fd; - fldeTy = aaApply2_dg; - } else { - fdapply = aaApply_fd; - fldeTy = aaApply_dg; + if (dim == 2) + dgargs->push(new Parameter(STCin, Type::tvoidptr, NULL, NULL)); + dgs[i] = new TypeDelegate(new TypeFunction(dgargs, Type::tint32, 0, LINKd)); + args->push(new Parameter(STCin, dgs[i], NULL, NULL)); + funcs[i] = FuncDeclaration::genCfunc(args, Type::tint32, names[i]); } + fdapply = funcs[i]; + fldeTy = dgs[i]; + +#else + FuncDeclaration *fdapply; + if (dim == 2) + fdapply = FuncDeclaration::genCfunc(Type::tindex, "_aaApply2"); + else + fdapply = FuncDeclaration::genCfunc(Type::tindex, "_aaApply"); #endif ec = new VarExp(Loc(), fdapply); Expressions *exps = new Expressions(); @@ -2229,24 +2214,19 @@ Lagain: const char *r = (op == TOKforeach_reverse) ? "R" : ""; int j = sprintf(fdname, "_aApply%s%.*s%llu", r, 2, fntab[flag], (ulonglong)dim); assert(j < sizeof(fdname) / sizeof(fdname[0])); -#if IN_LLVM - //LDC: Build arguments. +#if IN_LLVM // LDC: Build parameters. + FuncDeclaration *fdapply; + TypeDelegate *dgty; Parameters* args = new Parameters; args->push(new Parameter(STCin, tn->arrayOf(), NULL, NULL)); - if (dim == 2) { - Parameters* dgargs = new Parameters; + Parameters* dgargs = new Parameters; + dgargs->push(new Parameter(STCin, Type::tvoidptr, NULL, NULL)); + if (dim == 2) dgargs->push(new Parameter(STCin, Type::tvoidptr, NULL, NULL)); - dgargs->push(new Parameter(STCin, Type::tvoidptr, NULL, NULL)); - dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tint32, 0, LINKd)); - args->push(new Parameter(STCin, dgty, NULL, NULL)); - fdapply = FuncDeclaration::genCfunc(args, Type::tint32, fdname); - } else { - Parameters* dgargs = new Parameters; - dgargs->push(new Parameter(STCin, Type::tvoidptr, NULL, NULL)); - dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tint32, 0, LINKd)); - args->push(new Parameter(STCin, dgty, NULL, NULL)); - fdapply = FuncDeclaration::genCfunc(args, Type::tint32, fdname); - } + dgty = new TypeDelegate(new TypeFunction(dgargs, Type::tint32, 0, LINKd)); + args->push(new Parameter(STCin, dgty, NULL, NULL)); + fdapply = FuncDeclaration::genCfunc(args, Type::tint32, fdname); + #else FuncDeclaration *fdapply = FuncDeclaration::genCfunc(Type::tindex, fdname); #endif @@ -4419,8 +4399,7 @@ Statement *SynchronizedStatement::semantic(Scope *sc) Statements *cs = new Statements(); cs->push(new ExpStatement(loc, tmp)); -#if IN_LLVM - // LDC: Build args +#if IN_LLVM // LDC: Build parameters. Parameters* args = new Parameters; args->push(new Parameter(STCin, ClassDeclaration::object->type, NULL, NULL)); FuncDeclaration *fdenter = FuncDeclaration::genCfunc(args, Type::tvoid, Id::monitorenter); @@ -4431,7 +4410,7 @@ Statement *SynchronizedStatement::semantic(Scope *sc) e->type = Type::tvoid; // do not run semantic on e cs->push(new ExpStatement(loc, e)); -#if IN_LLVM +#if IN_LLVM // LDC: Build parameters. FuncDeclaration *fdexit = FuncDeclaration::genCfunc(args, Type::tvoid, Id::monitorexit); #else FuncDeclaration *fdexit = FuncDeclaration::genCfunc(Type::tvoid, Id::monitorexit); @@ -4465,8 +4444,7 @@ Statement *SynchronizedStatement::semantic(Scope *sc) Statements *cs = new Statements(); cs->push(new ExpStatement(loc, tmp)); -#if IN_LLVM - // LDC: Build args +#if IN_LLVM // LDC: Build parameters. Parameters* args = new Parameters; args->push(new Parameter(STCin, t->pointerTo(), NULL, NULL)); @@ -4480,7 +4458,7 @@ Statement *SynchronizedStatement::semantic(Scope *sc) e->type = Type::tvoid; // do not run semantic on e cs->push(new ExpStatement(loc, e)); -#if IN_LLVM +#if IN_LLVM // LDC: Build parameters. FuncDeclaration *fdexit = FuncDeclaration::genCfunc(args, Type::tvoid, Id::criticalexit); #else FuncDeclaration *fdexit = FuncDeclaration::genCfunc(Type::tvoid, Id::criticalexit);