|
|
@ -3,6 +3,7 @@ |
|
|
|
#include <cassert> |
|
|
|
#include <iostream> |
|
|
|
#include <llvm/IR/Module.h> |
|
|
|
#include <llvm/IR/LLVMContext.h> |
|
|
|
#include <llvm/Support/Path.h> |
|
|
|
#include <llvm/Support/FileSystem.h> |
|
|
|
#include <llvm/Support/raw_os_ostream.h> |
|
|
@ -23,6 +24,32 @@ ObjectCache* Cache::getObjectCache() |
|
|
|
return &objectCache; |
|
|
|
} |
|
|
|
|
|
|
|
namespace |
|
|
|
{ |
|
|
|
llvm::MemoryBuffer* lastObject; |
|
|
|
} |
|
|
|
|
|
|
|
std::unique_ptr<llvm::Module> Cache::getObject(std::string const& id) |
|
|
|
{ |
|
|
|
assert(!lastObject); |
|
|
|
llvm::SmallString<256> cachePath; |
|
|
|
llvm::sys::path::system_temp_directory(false, cachePath); |
|
|
|
llvm::sys::path::append(cachePath, "evm_objs", id); |
|
|
|
|
|
|
|
if (auto r = llvm::MemoryBuffer::getFile(cachePath.str(), -1, false)) |
|
|
|
lastObject = llvm::MemoryBuffer::getMemBufferCopy(r.get()->getBuffer()); |
|
|
|
else if (r.getError() != std::make_error_code(std::errc::no_such_file_or_directory)) |
|
|
|
std::cerr << r.getError().message(); // TODO: Add log
|
|
|
|
|
|
|
|
if (lastObject) // if object found create fake module
|
|
|
|
{ |
|
|
|
auto module = std::unique_ptr<llvm::Module>(new llvm::Module(id, llvm::getGlobalContext())); |
|
|
|
auto mainFuncType = llvm::FunctionType::get(llvm::IntegerType::get(llvm::getGlobalContext(), 32), {}, false); |
|
|
|
auto func = llvm::Function::Create(mainFuncType, llvm::Function::ExternalLinkage, id, module.get()); |
|
|
|
} |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::MemoryBuffer const* _object) |
|
|
|
{ |
|
|
@ -43,16 +70,9 @@ void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::Memory |
|
|
|
|
|
|
|
llvm::MemoryBuffer* ObjectCache::getObject(llvm::Module const* _module) |
|
|
|
{ |
|
|
|
auto&& id = _module->getModuleIdentifier(); |
|
|
|
llvm::SmallString<256> cachePath; |
|
|
|
llvm::sys::path::system_temp_directory(false, cachePath); |
|
|
|
llvm::sys::path::append(cachePath, "evm_objs", id); |
|
|
|
|
|
|
|
if (auto r = llvm::MemoryBuffer::getFile(cachePath.str(), -1, false)) |
|
|
|
return llvm::MemoryBuffer::getMemBufferCopy(r.get()->getBuffer()); |
|
|
|
else if (r.getError() != std::make_error_code(std::errc::no_such_file_or_directory)) |
|
|
|
std::cerr << r.getError().message(); // TODO: Add log
|
|
|
|
return nullptr; |
|
|
|
auto o = lastObject; |
|
|
|
lastObject = nullptr; |
|
|
|
return o; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|