2016-06-22 16:37:51 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifdef LLVM_AVAILABLE
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
2017-01-28 15:32:45 +03:00
|
|
|
#include "restore_new.h"
|
2016-06-22 16:37:51 +03:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 0)
|
|
|
|
#endif
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
2017-01-28 15:32:45 +03:00
|
|
|
#include "define_new_memleakdetect.h"
|
2016-06-22 16:37:51 +03:00
|
|
|
|
|
|
|
extern llvm::LLVMContext g_llvm_ctx;
|
|
|
|
|
|
|
|
// Temporary compiler interface
|
|
|
|
class jit_compiler final
|
|
|
|
{
|
|
|
|
// Execution instance
|
|
|
|
std::unique_ptr<llvm::ExecutionEngine> m_engine;
|
|
|
|
|
2017-02-26 18:56:31 +03:00
|
|
|
// Linkage cache
|
2017-06-23 00:52:09 +03:00
|
|
|
std::unordered_map<std::string, u64> m_link;
|
|
|
|
|
|
|
|
// Compiled functions
|
|
|
|
std::unordered_map<std::string, u64> m_map;
|
2017-02-26 18:56:31 +03:00
|
|
|
|
|
|
|
// Arch
|
|
|
|
std::string m_cpu;
|
|
|
|
|
2016-06-22 16:37:51 +03:00
|
|
|
public:
|
2017-06-23 00:52:09 +03:00
|
|
|
jit_compiler(std::unordered_map<std::string, u64>, std::string _cpu);
|
2016-06-22 16:37:51 +03:00
|
|
|
~jit_compiler();
|
|
|
|
|
2017-06-23 00:52:09 +03:00
|
|
|
// Add module
|
|
|
|
void add(std::unique_ptr<llvm::Module> module, const std::string& path);
|
|
|
|
|
|
|
|
// Finalize
|
|
|
|
void fin(const std::string& path);
|
2017-02-26 18:56:31 +03:00
|
|
|
|
2017-06-23 00:52:09 +03:00
|
|
|
// Add functions directly (name -> code)
|
|
|
|
void add(std::unordered_map<std::string, std::string>);
|
2017-02-26 18:56:31 +03:00
|
|
|
|
2016-06-22 16:37:51 +03:00
|
|
|
// Get compiled function address
|
2017-06-23 00:52:09 +03:00
|
|
|
u64 get(const std::string& name) const
|
2016-06-22 16:37:51 +03:00
|
|
|
{
|
|
|
|
const auto found = m_map.find(name);
|
|
|
|
|
|
|
|
if (found != m_map.end())
|
|
|
|
{
|
|
|
|
return found->second;
|
|
|
|
}
|
|
|
|
|
2017-06-23 00:52:09 +03:00
|
|
|
return m_engine->getFunctionAddress(name);
|
2016-06-22 16:37:51 +03:00
|
|
|
}
|
2017-02-26 18:56:31 +03:00
|
|
|
|
|
|
|
// Get CPU info
|
|
|
|
const std::string& cpu() const
|
|
|
|
{
|
|
|
|
return m_cpu;
|
|
|
|
}
|
2016-06-22 16:37:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|