diff options
Diffstat (limited to 'src/llvm.cpp')
-rw-r--r-- | src/llvm.cpp | 78 |
1 files changed, 42 insertions, 36 deletions
diff --git a/src/llvm.cpp b/src/llvm.cpp index 038beb1..88d937a 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -23,16 +23,17 @@ #define __STDC_CONSTANT_MACROS 1 #include <map> +#include <memory> #include <sstream> +#include <stack> #include <string> #include <vector> -#include <stack> #include <boost/format.hpp> #include "llvm/ExecutionEngine/ExecutionEngine.h" -#include "llvm/ExecutionEngine/JIT.h" -#include "llvm/ExecutionEngine/JITMemoryManager.h" +#include "llvm/ExecutionEngine/MCJIT.h" +// #include "llvm/ExecutionEngine/JITMemoryManager.h" #include "llvm/IR/AssemblyAnnotationWriter.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/DerivedTypes.h" @@ -42,10 +43,10 @@ #include "llvm/IR/Module.h" #include "llvm/IR/Value.h" #include "llvm/IR/Verifier.h" -#include "llvm/PassManager.h" +#include "llvm/IR/PassManager.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_os_ostream.h" -#include "llvm/Transforms/IPO/PassManagerBuilder.h" +// #include "llvm/Transforms/IPO/PassManagerBuilder.h" #include "llvm/Transforms/Scalar.h" #include "resp.hpp" @@ -58,8 +59,8 @@ struct IfRecord { IfRecord(LLVMContext& context, unsigned labelIndex, Type* t) : type(t) , mergeBB(BasicBlock::Create(context, "endif")) - , thenBB(BasicBlock::Create(context, (format("then%1%") % labelIndex).str())) - , elseBB(BasicBlock::Create(context, (format("else%1%") % labelIndex).str())) + , thenBB(BasicBlock::Create(context, (boost::format("then%1%") % labelIndex).str())) + , elseBB(BasicBlock::Create(context, (boost::format("else%1%") % labelIndex).str())) , thenV(NULL) , elseV(NULL) , nMergeBranches(0) @@ -119,12 +120,12 @@ private: Type* llType(const AST* t, const char* name=NULL); LLVMContext context; - Module* module; - ExecutionEngine* engine; + std::unique_ptr<Module> module; + ExecutionEngine* eengine; IRBuilder<> builder; Function* alloc; FunctionPassManager* fnOpt; - PassManager* modOpt; + ModulePassManager* modOpt; CType objectT; StructType* opaqueT; @@ -148,23 +149,23 @@ LLVMEngine::LLVMEngine() , labelIndex(1) { InitializeNativeTarget(); - module = new Module("resp", context); - engine = EngineBuilder(module).create(); - fnOpt = new FunctionPassManager(module); - modOpt = new PassManager(); + module = llvm::make_unique<Module>("resp", context); + fnOpt = new FunctionPassManager(module.get()); + // engine = EngineBuilder(module).create(); + modOpt = new ModulePassManager(); // Set up optimisers - PassManagerBuilder pmb; - pmb.OptLevel = 3; - pmb.populateFunctionPassManager(*fnOpt); - pmb.populateModulePassManager(*modOpt); + // PassManagerBuilder pmb; + // pmb.OptLevel = 3; + // pmb.populateFunctionPassManager(*fnOpt); + // pmb.populateModulePassManager(*modOpt); // Declare host provided allocation primitive std::vector<Type*> argsT(1, Type::getInt32Ty(context)); // unsigned size FunctionType* funcT = FunctionType::get( PointerType::get(Type::getInt8Ty(context), 0), argsT, false); alloc = Function::Create( - funcT, Function::ExternalLinkage, "__resp_alloc", module); + funcT, Function::ExternalLinkage, "__resp_alloc", module.get()); // Build Object type (tag only, binary compatible with any constructed thing) vector<Type*> ctypes; @@ -175,7 +176,7 @@ LLVMEngine::LLVMEngine() LLVMEngine::~LLVMEngine() { - delete engine; + delete eengine; delete fnOpt; delete modOpt; } @@ -313,11 +314,11 @@ CVal LLVMEngine::compileCons(CEnv& cenv, const char* tname, const ATuple* type, CVal rtti, const vector<CVal>& fields) { // Find size of memory required - size_t s = engine->getDataLayout()->getTypeSizeInBits( + size_t s = module->getDataLayout().getTypeSizeInBits( PointerType::get(Type::getInt8Ty(context), 0)); assert(type->begin() != type->end()); for (ATuple::const_iterator i = type->iter_at(1); i != type->end(); ++i) - s += engine->getDataLayout()->getTypeSizeInBits( + s += module->getDataLayout().getTypeSizeInBits( (Type*)compileType(cenv, (*i)->str(), *i)); // Allocate struct @@ -341,7 +342,7 @@ LLVMEngine::compileCons(CEnv& cenv, const char* tname, const ATuple* type, CVal for (vector<CVal>::const_iterator f = fields.begin(); f != fields.end(); ++f, ++i, ++t) { Value* val = llVal(*f); Value* field = builder.CreateStructGEP( - structPtr, i, (format("tup%1%") % i).str().c_str()); + structPtr, i, (boost::format("tup%1%") % i).str().c_str()); if ((*t)->to_tuple()) val = builder.CreateBitCast(val, llType(*t), "objPtr"); @@ -370,7 +371,7 @@ LLVMEngine::compileLiteral(CEnv& cenv, const AST* lit) case T_INT32: return ConstantInt::get(Type::getInt32Ty(context), ((const ALiteral<int32_t>*)lit)->val, true); default: - throw Error(lit->loc, "Unknown literal type"); + throw RespError(lit->loc, "Unknown literal type"); } } @@ -424,12 +425,12 @@ LLVMEngine::compileProt( } THROW_IF(!llType(retT), Cursor(), - (format("return has non-concrete type `%1%'") % retT->str()).str()); + (boost::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, llName, module); + Function* f = Function::Create(fT, linkage, llName, module.get()); // Note f->getName() may be different from llName // however LLVM chooses to mangle is fine, we keep a pointer @@ -483,8 +484,8 @@ LLVMEngine::pushFnArgs(CEnv& cenv, const ATuple* prot, const ATuple* type, CFunc // Bind argument values in CEnv ATuple::const_iterator p = prot->begin(); ATuple::const_iterator pT = argsT->begin(); - assert(prot->size() == argsT->size()); - assert(prot->size() == f->num_args()); + assert(prot->list_len() == argsT->list_len()); + // assert(prot->list_len() == f->num_args()); for (Function::arg_iterator a = f->arg_begin(); a != f->arg_end(); ++a, ++p, ++pT) { const AST* t = *pT;//cenv.resolveType(*pT); // THROW_IF(!llType(t), (*p)->loc, "untyped parameter\n"); @@ -504,11 +505,13 @@ LLVMEngine::finishFn(CEnv& cenv, CVal ret, const AST* retT) llvm::raw_os_ostream os(std::cerr); if (verifyFunction(*static_cast<Function*>(f), &os)) { - module->dump(); - throw Error(Cursor(), "Broken module"); + // module->dump(); + throw RespError(Cursor(), "Broken module"); + } + llvm::AnalysisManager<Function> amanager; + if (cenv.args.find("-g") == cenv.args.end()) { + fnOpt->run(*static_cast<Function*>(f), amanager); } - if (cenv.args.find("-g") == cenv.args.end()) - fnOpt->run(*static_cast<Function*>(f)); currentFn = NULL; } @@ -644,7 +647,7 @@ LLVMEngine::compilePrimitive(CEnv& cenv, const ATuple* prim) return engine->builder.CreateICmp(pred, a, b); } - throw Error(prim->loc, "unknown primitive"); + throw RespError(prim->loc, "unknown primitive"); } CVal @@ -670,8 +673,10 @@ LLVMEngine::compileGlobalGet(CEnv& cenv, const string& sym, CVal val) void LLVMEngine::writeModule(CEnv& cenv, std::ostream& os) { - if (cenv.args.find("-g") == cenv.args.end()) - modOpt->run(*module); + if (cenv.args.find("-g") == cenv.args.end()) { + llvm::AnalysisManager<Module> amanager; + modOpt->run(*module, amanager); + } AssemblyAnnotationWriter writer; llvm::raw_os_ostream raw_stream(os); @@ -681,7 +686,7 @@ LLVMEngine::writeModule(CEnv& cenv, std::ostream& os) const string LLVMEngine::call(CEnv& cenv, CFunc f, const AST* retT) { - void* fp = engine->getPointerToFunction(llFunc(f)); + void* fp = eengine->getPointerToFunction(llFunc(f)); const Type* t = llType(retT); THROW_IF(!fp, Cursor(), "unable to get function pointer"); THROW_IF(!t, Cursor(), "function with non-concrete return type called"); @@ -701,6 +706,7 @@ LLVMEngine::call(CEnv& cenv, CFunc f, const AST* retT) case '\"': case '\\': ss << '\\'; + // fallthru default: ss << c; break; |