Browse Source

Using llvm::ExecutionEngine::getFunctionAddress() instead of getPointerToFunction(). Cleanups.

cl-refactor
Paweł Bylica 10 years ago
parent
commit
a4927609d7
  1. 5
      libevmjit/Cache.h
  2. 33
      libevmjit/ExecutionEngine.cpp

5
libevmjit/Cache.h

@ -16,12 +16,11 @@ class ExecBundle
{ {
public: public:
std::unique_ptr<llvm::ExecutionEngine> engine; std::unique_ptr<llvm::ExecutionEngine> engine;
llvm::Function* entryFunc = nullptr;
ExecBundle() = default; ExecBundle() = default;
ExecBundle(ExecBundle&& _other): ExecBundle(ExecBundle&& _other):
engine(std::move(_other.engine)), engine(std::move(_other.engine))
entryFunc(_other.entryFunc) {} {}
ExecBundle(ExecBundle const&) = delete; ExecBundle(ExecBundle const&) = delete;
void operator=(ExecBundle) = delete; void operator=(ExecBundle) = delete;

33
libevmjit/ExecutionEngine.cpp

@ -53,47 +53,37 @@ ReturnCode ExecutionEngine::run(std::unique_ptr<llvm::Module> _module, RuntimeDa
static const auto program = "EVM JIT"; static const auto program = "EVM JIT";
llvm::PrettyStackTraceProgram X(1, &program); llvm::PrettyStackTraceProgram X(1, &program);
auto&& context = llvm::getGlobalContext();
llvm::InitializeNativeTarget(); llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter(); llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser(); llvm::InitializeNativeTargetAsmParser();
std::string errorMsg;
llvm::EngineBuilder builder(module); llvm::EngineBuilder builder(module);
//builder.setMArch(MArch);
//builder.setMCPU(MCPU);
//builder.setMAttrs(MAttrs);
//builder.setRelocationModel(RelocModel);
//builder.setCodeModel(CMModel);
builder.setErrorStr(&errorMsg);
builder.setEngineKind(llvm::EngineKind::JIT); builder.setEngineKind(llvm::EngineKind::JIT);
builder.setUseMCJIT(true); builder.setUseMCJIT(true);
builder.setMCJITMemoryManager(new llvm::SectionMemoryManager()); std::unique_ptr<llvm::SectionMemoryManager> memoryManager(new llvm::SectionMemoryManager);
builder.setMCJITMemoryManager(memoryManager.get());
builder.setOptLevel(llvm::CodeGenOpt::None); builder.setOptLevel(llvm::CodeGenOpt::None);
auto triple = llvm::Triple(llvm::sys::getProcessTriple()); auto triple = llvm::Triple(llvm::sys::getProcessTriple());
if (triple.getOS() == llvm::Triple::OSType::Win32) if (triple.getOS() == llvm::Triple::OSType::Win32)
triple.setObjectFormat(llvm::Triple::ObjectFormatType::ELF); // MCJIT does not support COFF format triple.setObjectFormat(llvm::Triple::ObjectFormatType::ELF); // MCJIT does not support COFF format
module->setTargetTriple(triple.str()); module->setTargetTriple(triple.str());
ExecBundle exec; ExecBundle exec;
exec.engine.reset(builder.create()); exec.engine.reset(builder.create());
if (!exec.engine) if (!exec.engine)
return ReturnCode::LLVMConfigError; return ReturnCode::LLVMConfigError;
_module.release(); // Successfully created llvm::ExecutionEngine takes ownership of the module _module.release(); // Successfully created llvm::ExecutionEngine takes ownership of the module
memoryManager.release(); // and memory manager
auto finalizationStartTime = std::chrono::high_resolution_clock::now(); // TODO: Finalization not needed when llvm::ExecutionEngine::getFunctionAddress used
exec.engine->finalizeObject(); //auto finalizationStartTime = std::chrono::high_resolution_clock::now();
auto finalizationEndTime = std::chrono::high_resolution_clock::now(); //exec.engine->finalizeObject();
clog(JIT) << " + " << std::chrono::duration_cast<std::chrono::milliseconds>(finalizationEndTime - finalizationStartTime).count(); //auto finalizationEndTime = std::chrono::high_resolution_clock::now();
//clog(JIT) << " + " << std::chrono::duration_cast<std::chrono::milliseconds>(finalizationEndTime - finalizationStartTime).count();
auto executionStartTime = std::chrono::high_resolution_clock::now(); auto executionStartTime = std::chrono::high_resolution_clock::now();
exec.entryFunc = module->getFunction("main");
if (!exec.entryFunc)
return ReturnCode::LLVMLinkError;
std::string key{reinterpret_cast<char const*>(_code.data()), _code.size()}; std::string key{reinterpret_cast<char const*>(_code.data()), _code.size()};
//auto& cachedExec = Cache::registerExec(key, std::move(exec)); //auto& cachedExec = Cache::registerExec(key, std::move(exec));
auto returnCode = run(exec, _data, _env); auto returnCode = run(exec, _data, _env);
@ -120,8 +110,7 @@ ReturnCode runEntryFunc(ExecBundle const& _exec, Runtime* _runtime)
// to allow function to be executed. // to allow function to be executed.
// That might be related to memory manager. Can we share one? // That might be related to memory manager. Can we share one?
typedef ReturnCode(*EntryFuncPtr)(Runtime*); typedef ReturnCode(*EntryFuncPtr)(Runtime*);
auto entryFuncVoidPtr = _exec.engine->getPointerToFunction(_exec.entryFunc); auto entryFuncPtr = (EntryFuncPtr)_exec.engine->getFunctionAddress("main");
auto entryFuncPtr = static_cast<EntryFuncPtr>(entryFuncVoidPtr);
ReturnCode returnCode{}; ReturnCode returnCode{};
auto sj = setjmp(_runtime->getJmpBuf()); auto sj = setjmp(_runtime->getJmpBuf());

Loading…
Cancel
Save