From b4dba561084b7ce60a8b1cfdb4e3b9de87de8d35 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 22 Aug 2010 19:16:28 +0000 Subject: Simplify Engine::startFunction. git-svn-id: http://svn.drobilla.net/resp/resp@267 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/c.cpp | 15 +++++---------- src/compile.cpp | 11 ++++++----- src/llvm.cpp | 14 ++++---------- src/repl.cpp | 4 ++-- src/resp.hpp | 4 ++-- 5 files changed, 19 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/c.cpp b/src/c.cpp index 66ab971..77c5fbe 100644 --- a/src/c.cpp +++ b/src/c.cpp @@ -101,8 +101,7 @@ struct CEngine : public Engine { } CFunc startFunction(CEnv& cenv, - const std::string& name, const AType* retT, const ATuple& argsT, - const vector argNames) + const std::string& name, const ATuple* args, const AType* retT, const ATuple& argsT) { vector cprot; FOREACH(ATuple::const_iterator, i, argsT) { @@ -119,12 +118,12 @@ struct CEngine : public Engine { f->returnType = *llType(retT); f->name = name; f->text += f->returnType + "\n" + f->name + "("; - ATuple::const_iterator ai = argsT.begin(); - vector::const_iterator ni = argNames.begin(); + ATuple::const_iterator ai = argsT.begin(); + ATuple::const_iterator ni = args->begin(); for (; ai != argsT.end(); ++ai, ++ni) { if (ai != argsT.begin()) f->text += ", "; - f->text += *llType((*ai)->as()) + " " + *ni; + f->text += *llType((*ai)->as()) + " " + (*ni)->as()->cppstr; } f->text += ")\n{\n"; @@ -216,13 +215,9 @@ CEngine::compileFunction(CEnv& cenv, const AFn* fn, const AType* type) const AType* retT = type->last()->as(); Subst argsSubst = cenv.tenv.buildSubst(type, *argsT); - vector argNames; - for (ATuple::const_iterator i = fn->prot()->begin(); i != fn->prot()->end(); ++i) - argNames.push_back((*i)->str()); - // Write function declaration const string name = (fn->name == "") ? cenv.penv.gensymstr("_fn") : fn->name; - Function* f = llFunc(cenv.engine()->startFunction(cenv, name, retT, *argsT, argNames)); + Function* f = llFunc(cenv.engine()->startFunction(cenv, name, fn->prot(), retT, *argsT)); cenv.push(); Subst oldSubst = cenv.tsubst; diff --git a/src/compile.cpp b/src/compile.cpp index 7a875f3..66c3abe 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -64,11 +64,12 @@ AFn::compile(CEnv& cenv) const throw() { const AType* type = cenv.type(this); CFunc f = cenv.findImpl(this, type); - if (!f) { - f = cenv.engine()->compileFunction(cenv, this, type); - cenv.vals.def(cenv.penv.sym(name), f); - cenv.addImpl(this, f); - } + if (f) + return f; + + f = cenv.engine()->compileFunction(cenv, this, type); + cenv.vals.def(cenv.penv.sym(name), f); + cenv.addImpl(this, f); return f; } diff --git a/src/llvm.cpp b/src/llvm.cpp index 8e3ea50..7ba3d54 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -128,8 +128,7 @@ struct LLVMEngine : public Engine { } CFunc startFunction(CEnv& cenv, - const std::string& name, const AType* retT, const ATuple& argsT, - const vector argNames) + const std::string& name, const ATuple* args, const AType* retT, const ATuple& argsT) { Function::LinkageTypes linkage = Function::ExternalLinkage; @@ -152,9 +151,8 @@ struct LLVMEngine : public Engine { // Set argument names in generated code Function::arg_iterator a = f->arg_begin(); - if (!argNames.empty()) - for (vector::const_iterator i = argNames.begin(); i != argNames.end(); ++a, ++i) - a->setName(*i); + for (ATuple::const_iterator i = args->begin(); i != args->end(); ++a, ++i) + a->setName((*i)->as()->cppstr); BasicBlock* bb = BasicBlock::Create(context, "entry", f); builder.SetInsertPoint(bb); @@ -330,13 +328,9 @@ LLVMEngine::compileFunction(CEnv& cenv, const AFn* fn, const AType* type) const AType* argsT = type->prot()->as(); const AType* retT = type->last()->as(); - vector argNames; - for (ATuple::const_iterator i = fn->prot()->begin(); i != fn->prot()->end(); ++i) - argNames.push_back((*i)->str()); - // Write function declaration const string name = (fn->name == "") ? cenv.penv.gensymstr("_fn") : fn->name; - Function* f = llFunc(cenv.engine()->startFunction(cenv, name, retT, *argsT, argNames)); + Function* f = llFunc(cenv.engine()->startFunction(cenv, name, fn->prot(), retT, *argsT)); cenv.push(); diff --git a/src/repl.cpp b/src/repl.cpp index 65f04ef..d2b8270 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -149,7 +149,7 @@ eval(CEnv& cenv, const string& name, istream& is, bool execute) const AType* type = cenv.type(exprs.back()); // Create function for top-level of program - f = cenv.engine()->startFunction(cenv, "main", type, ATuple(cursor)); + f = cenv.engine()->startFunction(cenv, "main", new ATuple(cursor), type, ATuple(cursor)); // Compile expressions (other than function definitions) into it for (list::const_iterator i = exprs.begin(); i != exprs.end(); ++i) @@ -191,7 +191,7 @@ repl(CEnv& cenv) CFunc f = NULL; try { // Create function for this repl loop - f = cenv.engine()->startFunction(cenv, replFnName, type, ATuple(cursor)); + f = cenv.engine()->startFunction(cenv, replFnName, new ATuple(cursor), type, ATuple(cursor)); cenv.engine()->finishFunction(cenv, f, ast->compile(cenv)); callPrintCollect(cenv, f, ast, type, true); if (cenv.args.find("-d") != cenv.args.end()) diff --git a/src/resp.hpp b/src/resp.hpp index 1432b0f..5d3a93c 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -657,9 +657,9 @@ struct Engine { virtual CFunc startFunction( CEnv& cenv, const std::string& name, + const ATuple* args, const AType* retT, - const ATuple& argsT, - const vector argNames=vector()) = 0; + const ATuple& argsT) = 0; typedef const vector ValVec; -- cgit v1.2.1