From d28e4f7ad0d0d8d32d65a098a16f65036e5d318c Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 15 Mar 2009 06:13:50 +0000 Subject: Remove duplicated code. git-svn-id: http://svn.drobilla.net/resp/tuplr@100 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- llvm.cpp | 75 ++++++++++++++++++++++------------------------------------------ 1 file changed, 25 insertions(+), 50 deletions(-) diff --git a/llvm.cpp b/llvm.cpp index 8417163..e00b55d 100644 --- a/llvm.cpp +++ b/llvm.cpp @@ -195,44 +195,15 @@ void AClosure::lift(CEnv& cenv) { AType* type = cenv.type(this); - if (!type->concrete() || funcs.find(type)) + if (funcs.find(type) || !type->concrete()) return; - - // Write function declaration - string name = this->name == "" ? cenv.gensym("_fn") : this->name; - ATuple* protT = dynamic_cast(type->at(1)); - assert(protT); - Function* f = compileFunction(cenv, name, - lltype(dynamic_cast(type->at(type->size() - 1))), - *protT); - cenv.push(); - Subst oldSubst = cenv.tsubst; - cenv.tsubst = Subst::compose(cenv.tsubst, *subst); - - // Bind argument values in CEnv - vector args; - const_iterator p = prot()->begin(); - size_t i = 0; - for (Function::arg_iterator a = f->arg_begin(); a != f->arg_end(); ++a, ++p) - cenv.def((*p)->as(), *p, protT->at(i++)->as(), &*a); + ATuple* protT = type->at(1)->as(); + vector argsT; + for (size_t i = 0; i < protT->size(); ++i) + argsT.push_back(protT->at(i)->as()); - // Write function body - try { - // Define value first for recursion - cenv.precompile(this, f); - funcs.push_back(make_pair(type, f)); - - CValue retVal = cenv.compile(at(2)); - llengine(cenv)->builder.CreateRet(LLVal(retVal)); // Finish function - cenv.optimise(LLFunc(f)); - } catch (Error& e) { - f->eraseFromParent(); // Error reading body, remove function - cenv.pop(); - throw e; - } - cenv.tsubst = oldSubst; - cenv.pop(); + liftCall(cenv, argsT); } void @@ -242,23 +213,28 @@ AClosure::liftCall(CEnv& cenv, const vector& argsT) assert(gt != cenv.tenv.genericTypes.end()); AType* genericType = new AType(*gt->second); - // Find type and build substitution - assert(argsT.size() == prot()->size()); - Subst argsSubst; - ATuple* genericProtT = dynamic_cast(genericType->at(1)); - assert(genericProtT); - for (size_t i = 0; i < argsT.size(); ++i) - argsSubst[dynamic_cast(genericProtT->at(i))] = dynamic_cast(argsT.at(i)); - - AType* thisType = argsSubst.apply(genericType)->as(); + AType* thisType = NULL; + Subst argsSubst; + if (!genericType->concrete()) { + // Find type and build substitution + assert(argsT.size() == prot()->size()); + ATuple* genericProtT = dynamic_cast(genericType->at(1)); + assert(genericProtT); + for (size_t i = 0; i < argsT.size(); ++i) + argsSubst[dynamic_cast(genericProtT->at(i))] = dynamic_cast(argsT.at(i)); + + thisType = argsSubst.apply(genericType)->as(); + + if (!thisType->concrete()) + throw Error("unable to resolve concrete type for function", loc); + } else { + thisType = cenv.type(this); + } - if (!thisType->concrete()) - throw Error("unable to resolve concrete type for function", loc); - if (funcs.find(thisType)) return; - - ATuple* protT = dynamic_cast(thisType->at(1)); + + ATuple* protT = thisType->at(1)->as(); // Write function declaration string name = this->name == "" ? cenv.gensym("_fn") : this->name; @@ -286,7 +262,6 @@ AClosure::liftCall(CEnv& cenv, const vector& argsT) CValue retVal = cenv.compile(at(2)); llengine(cenv)->builder.CreateRet(LLVal(retVal)); // Finish function cenv.optimise(LLFunc(f)); - } catch (Error& e) { f->eraseFromParent(); // Error reading body, remove function cenv.pop(); -- cgit v1.2.1