Get rid of runTimeHack and instead add proper argument info to the frontend

declatation.
This commit is contained in:
Christian Kamm 2008-07-22 19:24:40 +02:00
parent 78c900831a
commit 566eac93fe
6 changed files with 97 additions and 89 deletions

View file

@ -626,8 +626,9 @@ struct FuncDeclaration : Declaration
const char *kind(); const char *kind();
void toDocBuffer(OutBuffer *buf); void toDocBuffer(OutBuffer *buf);
static FuncDeclaration *genCfunc(Type *treturn, char *name); // LLVMDC: give argument types to runtime functions
static FuncDeclaration *genCfunc(Type *treturn, Identifier *id); static FuncDeclaration *genCfunc(Arguments *args, Type *treturn, char *name);
static FuncDeclaration *genCfunc(Arguments *args, Type *treturn, Identifier *id);
Symbol *toSymbol(); Symbol *toSymbol();
Symbol *toThunkSymbol(int offset); // thunk version Symbol *toThunkSymbol(int offset); // thunk version
@ -637,7 +638,6 @@ struct FuncDeclaration : Declaration
FuncDeclaration *isFuncDeclaration() { return this; } FuncDeclaration *isFuncDeclaration() { return this; }
// llvmdc stuff // llvmdc stuff
bool runTimeHack;
std::set<VarDeclaration*> nestedVars; std::set<VarDeclaration*> nestedVars;
// we keep our own table of label statements as LabelDsymbolS // we keep our own table of label statements as LabelDsymbolS

View file

@ -74,8 +74,6 @@ FuncDeclaration::FuncDeclaration(Loc loc, Loc endloc, Identifier *id, enum STC s
nrvo_can = 1; nrvo_can = 1;
nrvo_var = NULL; nrvo_var = NULL;
shidden = NULL; shidden = NULL;
// llvmdc
runTimeHack = false;
} }
Dsymbol *FuncDeclaration::syntaxCopy(Dsymbol *s) Dsymbol *FuncDeclaration::syntaxCopy(Dsymbol *s)
@ -2009,12 +2007,16 @@ int FuncDeclaration::addPostInvariant()
* Generate a FuncDeclaration for a runtime library function. * 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; FuncDeclaration *fd;
TypeFunction *tf; TypeFunction *tf;
@ -2036,7 +2038,7 @@ FuncDeclaration *FuncDeclaration::genCfunc(Type *treturn, Identifier *id)
} }
else else
{ {
tf = new TypeFunction(NULL, treturn, 0, LINKc); tf = new TypeFunction(args, treturn, 0, LINKc);
fd = new FuncDeclaration(0, 0, id, STCstatic, tf); fd = new FuncDeclaration(0, 0, id, STCstatic, tf);
fd->protection = PROTpublic; fd->protection = PROTpublic;
fd->linkage = LINKc; fd->linkage = LINKc;

View file

@ -1536,8 +1536,12 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident)
static char *name[2] = { "_adReverseChar", "_adReverseWchar" }; static char *name[2] = { "_adReverseChar", "_adReverseWchar" };
nm = name[n->ty == Twchar]; nm = name[n->ty == Twchar];
fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), nm); //LLVMDC: Build arguments.
fd->runTimeHack = true; 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); ec = new VarExp(0, fd);
e = e->castTo(sc, n->arrayOf()); // convert to dynamic array e = e->castTo(sc, n->arrayOf()); // convert to dynamic array
arguments = new Expressions(); arguments = new Expressions();
@ -1554,8 +1558,12 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident)
static char *name[2] = { "_adSortChar", "_adSortWchar" }; static char *name[2] = { "_adSortChar", "_adSortWchar" };
nm = name[n->ty == Twchar]; nm = name[n->ty == Twchar];
fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), nm); //LLVMDC: Build arguments.
fd->runTimeHack = true; 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); ec = new VarExp(0, fd);
e = e->castTo(sc, n->arrayOf()); // convert to dynamic array e = e->castTo(sc, n->arrayOf()); // convert to dynamic array
arguments = new Expressions(); arguments = new Expressions();
@ -1573,8 +1581,16 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident)
assert(size); assert(size);
dup = (ident == Id::dup); dup = (ident == Id::dup);
fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), dup ? Id::adDup : Id::adReverse); //LLVMDC: Build arguments.
fd->runTimeHack = true; 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); ec = new VarExp(0, fd);
e = e->castTo(sc, n->arrayOf()); // convert to dynamic array e = e->castTo(sc, n->arrayOf()); // convert to dynamic array
arguments = new Expressions(); arguments = new Expressions();
@ -1592,9 +1608,12 @@ Expression *TypeArray::dotExp(Scope *sc, Expression *e, Identifier *ident)
FuncDeclaration *fd; FuncDeclaration *fd;
Expressions *arguments; 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")); (char*)(n->ty == Tbit ? "_adSortBit" : "_adSort"));
fd->runTimeHack = true;
ec = new VarExp(0, fd); ec = new VarExp(0, fd);
e = e->castTo(sc, n->arrayOf()); // convert to dynamic array e = e->castTo(sc, n->arrayOf()); // convert to dynamic array
arguments = new Expressions(); arguments = new Expressions();
@ -2268,8 +2287,10 @@ Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident)
FuncDeclaration *fd; FuncDeclaration *fd;
Expressions *arguments; Expressions *arguments;
fd = FuncDeclaration::genCfunc(Type::tsize_t, Id::aaLen); //LLVMDC: Build arguments.
fd->runTimeHack = true; 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); ec = new VarExp(0, fd);
arguments = new Expressions(); arguments = new Expressions();
arguments->push(e); arguments->push(e);
@ -2284,8 +2305,11 @@ Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident)
int size = key->size(e->loc); int size = key->size(e->loc);
assert(size); assert(size);
fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), Id::aaKeys); //LLVMDC: Build arguments.
fd->runTimeHack = true; 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); ec = new VarExp(0, fd);
arguments = new Expressions(); arguments = new Expressions();
arguments->push(e); arguments->push(e);
@ -2299,8 +2323,12 @@ Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident)
FuncDeclaration *fd; FuncDeclaration *fd;
Expressions *arguments; Expressions *arguments;
fd = FuncDeclaration::genCfunc(Type::tvoid->arrayOf(), Id::aaValues); //LLVMDC: Build arguments.
fd->runTimeHack = true; 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); ec = new VarExp(0, fd);
arguments = new Expressions(); arguments = new Expressions();
arguments->push(e); arguments->push(e);
@ -2317,8 +2345,11 @@ Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident)
FuncDeclaration *fd; FuncDeclaration *fd;
Expressions *arguments; Expressions *arguments;
fd = FuncDeclaration::genCfunc(Type::tvoid->pointerTo(), Id::aaRehash); //LLVMDC: Build arguments.
fd->runTimeHack = true; 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); ec = new VarExp(0, fd);
arguments = new Expressions(); arguments = new Expressions();
arguments->push(e->addressOf(sc)); arguments->push(e->addressOf(sc));

View file

@ -1598,11 +1598,24 @@ Statement *ForeachStatement::semantic(Scope *sc)
/* Call: /* Call:
* _aaApply(aggr, keysize, flde) * _aaApply(aggr, keysize, flde)
*/ */
if (dim == 2) //LLVMDC: Build arguments.
fdapply = FuncDeclaration::genCfunc(Type::tindex, "_aaApply2"); Arguments* args = new Arguments;
else args->push(new Argument(STCin, Type::tvoidptr, NULL, NULL));
fdapply = FuncDeclaration::genCfunc(Type::tindex, "_aaApply"); args->push(new Argument(STCin, Type::tsize_t, NULL, NULL));
fdapply->runTimeHack = true; 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); ec = new VarExp(0, fdapply);
Expressions *exps = new Expressions(); Expressions *exps = new Expressions();
exps->push(aggr); exps->push(aggr);
@ -1643,8 +1656,23 @@ Statement *ForeachStatement::semantic(Scope *sc)
const char *r = (op == TOKforeach_reverse) ? "R" : ""; const char *r = (op == TOKforeach_reverse) ? "R" : "";
int j = sprintf(fdname, "_aApply%s%.*s%d", r, 2, fntab[flag], dim); int j = sprintf(fdname, "_aApply%s%.*s%d", r, 2, fntab[flag], dim);
assert(j < sizeof(fdname)); assert(j < sizeof(fdname));
fdapply = FuncDeclaration::genCfunc(Type::tindex, fdname); //LLVMDC: Build arguments.
fdapply->runTimeHack = true; 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); ec = new VarExp(0, fdapply);
Expressions *exps = new Expressions(); Expressions *exps = new Expressions();

View file

@ -287,13 +287,6 @@ void DtoResolveFunction(FuncDeclaration* fdecl)
Logger::println("DtoResolveFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars()); Logger::println("DtoResolveFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars());
LOG_SCOPE; 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 (fdecl->parent)
if (TemplateInstance* tinst = fdecl->parent->isTemplateInstance()) if (TemplateInstance* tinst = fdecl->parent->isTemplateInstance())
{ {
@ -391,25 +384,16 @@ void DtoDeclareFunction(FuncDeclaration* fdecl)
Type* t = DtoDType(fdecl->type); Type* t = DtoDType(fdecl->type);
TypeFunction* f = (TypeFunction*)t; 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 declareOnly = false;
bool templInst = fdecl->parent && DtoIsTemplateInstance(fdecl->parent); bool templInst = fdecl->parent && DtoIsTemplateInstance(fdecl->parent);
if (!templInst && fdecl->getModule() != gIR->dmodule) if (!templInst && fdecl->getModule() != gIR->dmodule)
{ {
Logger::println("not template instance, and not in this module. declare only!"); Logger::println("not template instance, and not in this module. declare only!");
Logger::println("current module: %s", gIR->dmodule->ident->toChars()); 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; declareOnly = true;
} }
else if (fdecl->llvmInternal == LLVMva_start) else if (fdecl->llvmInternal == LLVMva_start)

View file

@ -1001,16 +1001,6 @@ DValue* CallExp::toElem(IRState* p)
// TODO: use sret param attr // TODO: use sret param attr
if (retinptr) { if (retinptr) {
llargs[j] = new llvm::AllocaInst(argiter->get()->getContainedType(0),"rettmp",p->topallocapoint()); 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; ++j;
++argiter; ++argiter;
} }
@ -1164,24 +1154,6 @@ DValue* CallExp::toElem(IRState* p)
if (fnarg && fnarg->llvmByVal) if (fnarg && fnarg->llvmByVal)
palist = palist.addAttr(j+1, llvm::ParamAttr::ByVal); 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(); 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 // set calling convention
if (dfn && dfn->func) { if (dfn && dfn->func) {
int li = dfn->func->llvmInternal; int li = dfn->func->llvmInternal;