From 8338b52abc0e5a1c0893526bde2d347f1891773e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 22 Aug 2010 20:20:38 +0000 Subject: Simplify Engine function compilation interface. Removes duplicated code in various backends and reduces Engine code knowledge of AFn specifics (which belongs in compile.cpp). git-svn-id: http://svn.drobilla.net/resp/resp@268 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/llvm.cpp | 51 +++++++++++++++------------------------------------ 1 file changed, 15 insertions(+), 36 deletions(-) (limited to 'src/llvm.cpp') diff --git a/src/llvm.cpp b/src/llvm.cpp index 7ba3d54..8b4deaa 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -128,12 +128,15 @@ struct LLVMEngine : public Engine { } CFunc startFunction(CEnv& cenv, - const std::string& name, const ATuple* args, const AType* retT, const ATuple& argsT) + const std::string& name, const ATuple* args, const AType* type) { + const AType* argsT = type->prot()->as(); + const AType* retT = type->last()->as(); + Function::LinkageTypes linkage = Function::ExternalLinkage; vector cprot; - FOREACH(ATuple::const_iterator, i, argsT) { + FOREACHP(ATuple::const_iterator, i, argsT) { AType* at = (*i)->as(); THROW_IF(!llType(at), Cursor(), string("non-concrete parameter :: ") + at->str()) @@ -143,10 +146,12 @@ struct LLVMEngine : public Engine { THROW_IF(!llType(retT), Cursor(), (format("return has non-concrete type `%1%'") % retT->str()).str()); + const string llName = (name == "") ? cenv.penv.gensymstr("_fn") : name; + FunctionType* fT = FunctionType::get(llType(retT), cprot, false); - Function* f = Function::Create(fT, linkage, name, module); + Function* f = Function::Create(fT, linkage, llName, module); - // Note f->getName() may be different from name + // Note f->getName() may be different from llName // however LLVM chooses to mangle is fine, we keep a pointer // Set argument names in generated code @@ -159,6 +164,8 @@ struct LLVMEngine : public Engine { return f; } + + void pushFunctionArgs(CEnv& cenv, const AFn* fn, const AType* type, CFunc f); void finishFunction(CEnv& cenv, CFunc f, CVal ret) { builder.CreateRet(llVal(ret)); @@ -184,8 +191,6 @@ struct LLVMEngine : public Engine { return builder.CreateCall(llFunc(f), llArgs.begin(), llArgs.end()); } - CFunc compileFunction(CEnv& cenv, const AFn* fn, const AType* type); - CVal compileTup(CEnv& cenv, const AType* type, const vector& fields); CVal compileDot(CEnv& cenv, CVal tup, int32_t index); CVal compileLiteral(CEnv& cenv, const AST* lit); @@ -320,19 +325,12 @@ LLVMEngine::compileString(CEnv& cenv, const char* str) return builder.CreateGlobalStringPtr(str); } -CFunc -LLVMEngine::compileFunction(CEnv& cenv, const AFn* fn, const AType* type) +void +LLVMEngine::pushFunctionArgs(CEnv& cenv, const AFn* fn, const AType* type, CFunc f) { - assert(type->concrete()); + cenv.push(); const AType* argsT = type->prot()->as(); - const AType* retT = type->last()->as(); - - // Write function declaration - const string name = (fn->name == "") ? cenv.penv.gensymstr("_fn") : fn->name; - Function* f = llFunc(cenv.engine()->startFunction(cenv, name, fn->prot(), retT, *argsT)); - - cenv.push(); // Bind argument values in CEnv vector args; @@ -340,31 +338,12 @@ LLVMEngine::compileFunction(CEnv& cenv, const AFn* fn, const AType* type) ATuple::const_iterator pT = argsT->begin(); assert(fn->prot()->size() == argsT->size()); assert(fn->prot()->size() == f->num_args()); - for (Function::arg_iterator a = f->arg_begin(); a != f->arg_end(); ++a, ++p, ++pT) { + for (Function::arg_iterator a = llFunc(f)->arg_begin(); a != llFunc(f)->arg_end(); ++a, ++p, ++pT) { const AType* t = (*pT)->as(); const Type* lt = llType(t); THROW_IF(!lt, fn->loc, "untyped parameter\n"); cenv.def((*p)->as(), *p, t, &*a); } - - assert(!cenv.currentFn); - - // Write function body - try { - cenv.currentFn = f; - CVal retVal = NULL; - for (AFn::const_iterator i = fn->begin() + 2; i != fn->end(); ++i) - retVal = (*i)->compile(cenv); - cenv.engine()->finishFunction(cenv, f, retVal); - } catch (Error& e) { - f->eraseFromParent(); // Error reading body, remove function - cenv.pop(); - cenv.currentFn = NULL; - throw e; - } - cenv.pop(); - cenv.currentFn = NULL; - return f; } CVal -- cgit v1.2.1