diff options
Diffstat (limited to 'llvm.cpp')
-rw-r--r-- | llvm.cpp | 95 |
1 files changed, 31 insertions, 64 deletions
@@ -56,19 +56,33 @@ llType(const AType* t) ***************************************************************************/ struct LLVMEngine { - LLVMEngine() : module(new Module("tuplr")), engine(ExecutionEngine::create(module)) + LLVMEngine() + : module(new Module("tuplr")) + , engine(ExecutionEngine::create(module)) + , emp(module) + , opt(&emp) { - // Host provided allocation primitive prototype + // Set up optimiser pipeline + const TargetData* target = engine->getTargetData(); + opt.add(new TargetData(*target)); // Register target arch + opt.add(createInstructionCombiningPass()); // Simple optimizations + opt.add(createReassociatePass()); // Reassociate expressions + opt.add(createGVNPass()); // Eliminate Common Subexpressions + opt.add(createCFGSimplificationPass()); // Simplify control flow + + // Declare host provided allocation primitive std::vector<const Type*> argsT(1, Type::Int32Ty); // unsigned size argsT.push_back(Type::Int8Ty); // char tag FunctionType* funcT = FunctionType::get(PointerType::get(Type::Int8Ty, 0), argsT, false); alloc = Function::Create(funcT, Function::ExternalLinkage, "tuplr_gc_allocate", module); } - Module* module; - ExecutionEngine* engine; - IRBuilder<> builder; - CFunction alloc; + Module* module; + ExecutionEngine* engine; + IRBuilder<> builder; + CFunction alloc; + ExistingModuleProvider emp; + FunctionPassManager opt; }; static LLVMEngine* @@ -77,63 +91,6 @@ llEngine(CEnv& cenv) return reinterpret_cast<LLVMEngine*>(cenv.engine()); } -struct CEnv::PImpl { - PImpl(LLVMEngine* e) : engine(e), emp(e->module), opt(&emp) - { - // Set up the optimizer pipeline: - const TargetData* target = engine->engine->getTargetData(); - opt.add(new TargetData(*target)); // Register target arch - opt.add(createInstructionCombiningPass()); // Simple optimizations - opt.add(createReassociatePass()); // Reassociate expressions - opt.add(createGVNPass()); // Eliminate Common Subexpressions - opt.add(createCFGSimplificationPass()); // Simplify control flow - } - - LLVMEngine* engine; - ExistingModuleProvider emp; - FunctionPassManager opt; -}; - -CEnv::CEnv(PEnv& p, TEnv& t, CEngine e, ostream& os, ostream& es) - : out(os), err(es), penv(p), tenv(t), symID(0), _pimpl(new PImpl((LLVMEngine*)e)) -{ -} - -CEnv::~CEnv() -{ - Object::pool.collect(GC::Roots()); - delete _pimpl; -} - -CEngine -CEnv::engine() -{ - return _pimpl->engine; -} - -CValue -CEnv::compile(AST* obj) -{ - CValue* v = vals.ref(obj); - return (v && *v) ? *v : vals.def(obj, obj->compile(*this)); -} - -void -CEnv::optimise(CFunction f) -{ - if (args.find("-g") != args.end()) - return; - verifyFunction(*static_cast<Function*>(f)); - _pimpl->opt.run(*static_cast<Function*>(f)); -} - -void -CEnv::write(std::ostream& os) -{ - AssemblyAnnotationWriter writer; - _pimpl->engine->module->print(os, &writer); -} - #define LITERAL(CT, NAME, COMPILED) \ template<> CValue \ ALiteral<CT>::compile(CEnv& cenv) { return (COMPILED); } \ @@ -185,7 +142,10 @@ finishFunction(CEnv& cenv, CFunction f, CValue ret) { Value* retVal = llVal(ret); llEngine(cenv)->builder.CreateRet(retVal); - cenv.optimise(llFunc(f)); + + verifyFunction(*static_cast<Function*>(f)); + if (cenv.args.find("-g") == cenv.args.end()) + llEngine(cenv)->opt.run(*static_cast<Function*>(f)); } void @@ -195,6 +155,13 @@ eraseFunction(CEnv& cenv, CFunction f) llFunc(f)->eraseFromParent(); } +void +writeModule(CEnv& cenv, std::ostream& os) +{ + AssemblyAnnotationWriter writer; + llEngine(cenv)->module->print(os, &writer); +} + const string call(CEnv& cenv, CFunction f, AType* retT) { |