diff options
author | David Robillard <d@drobilla.net> | 2010-08-22 20:20:38 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2010-08-22 20:20:38 +0000 |
commit | 8338b52abc0e5a1c0893526bde2d347f1891773e (patch) | |
tree | ffea8eedbb766ff08dd6b30636b9060232d1ebb8 /src/llvm.cpp | |
parent | b4dba561084b7ce60a8b1cfdb4e3b9de87de8d35 (diff) | |
download | resp-8338b52abc0e5a1c0893526bde2d347f1891773e.tar.gz resp-8338b52abc0e5a1c0893526bde2d347f1891773e.tar.bz2 resp-8338b52abc0e5a1c0893526bde2d347f1891773e.zip |
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
Diffstat (limited to 'src/llvm.cpp')
-rw-r--r-- | src/llvm.cpp | 51 |
1 files changed, 15 insertions, 36 deletions
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*>(); + const AType* retT = type->last()->as<const AType*>(); + Function::LinkageTypes linkage = Function::ExternalLinkage; vector<const Type*> cprot; - FOREACH(ATuple::const_iterator, i, argsT) { + FOREACHP(ATuple::const_iterator, i, argsT) { AType* at = (*i)->as<AType*>(); 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<CVal>& 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*>(); - const AType* retT = type->last()->as<const AType*>(); - - // 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<Value*> 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 AType*>(); const Type* lt = llType(t); THROW_IF(!lt, fn->loc, "untyped parameter\n"); cenv.def((*p)->as<ASymbol*>(), *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 |