From dedf75dd30f0112cb85845c2a5a9ef99f819fac8 Mon Sep 17 00:00:00 2001 From: Dmitry Patsura Date: Sun, 4 Nov 2018 16:26:10 +0900 Subject: [PATCH 1/4] Feature: ExecutionEngine - initial Signed-off-by: Dmitry Patsura --- CMakeLists.txt | 2 +- llvm-node.d.ts | 3 +++ .../execution-engine-module.h | 12 ++++++++++ src/execution-engine/execution-engine.cc | 23 +++++++++++++++++++ src/execution-engine/execution-engine.h | 23 +++++++++++++++++++ src/llvm-node.cc | 3 ++- 6 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/execution-engine/execution-engine-module.h create mode 100644 src/execution-engine/execution-engine.cc create mode 100644 src/execution-engine/execution-engine.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ede663a..2ab094a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ file(GLOB SOURCE_FILES add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES}) set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") -llvm_map_components_to_libnames(llvm_libs ${LLVM_TARGETS_TO_BUILD} bitwriter codegen core support tablegen target) +llvm_map_components_to_libnames(llvm_libs ${LLVM_TARGETS_TO_BUILD} bitwriter codegen core support tablegen target executionengine) # Link against LLVM libraries target_link_libraries(${PROJECT_NAME} ${llvm_libs} ${CMAKE_JS_LIB} ) \ No newline at end of file diff --git a/llvm-node.d.ts b/llvm-node.d.ts index 8cca699..50b072d 100644 --- a/llvm-node.d.ts +++ b/llvm-node.d.ts @@ -683,6 +683,9 @@ declare namespace llvm { getTypeByName(name: string): StructType | null; } + class ExecutionEngine { + } + // support class TargetRegistry { private constructor(); diff --git a/src/execution-engine/execution-engine-module.h b/src/execution-engine/execution-engine-module.h new file mode 100644 index 0000000..cd605c8 --- /dev/null +++ b/src/execution-engine/execution-engine-module.h @@ -0,0 +1,12 @@ + +#ifndef LLVM_NODE_EXECUTION_ENGINE_MODULE_H +#define LLVM_NODE_EXECUTION_ENGINE_MODULE_H + +#include +#include "execution-engine.h" + +NAN_MODULE_INIT(InitExecutionEngine) { + ExecutionEngineWrapper::Init(target); +} + +#endif //LLVM_NODE_EXECUTION_ENGINE_MODULE_H diff --git a/src/execution-engine/execution-engine.cc b/src/execution-engine/execution-engine.cc new file mode 100644 index 0000000..06e32be --- /dev/null +++ b/src/execution-engine/execution-engine.cc @@ -0,0 +1,23 @@ + +#include "execution-engine.h" +#include "../ir/data-layout.h" + +Nan::Persistent ExecutionEngineWrapper::executionEngineTemplate {}; + +NAN_MODULE_INIT(ExecutionEngineWrapper::Init) { + auto objectTemplate = Nan::New(); + objectTemplate->SetInternalFieldCount(1); + + executionEngineTemplate.Reset(objectTemplate); +} + +v8::Local ExecutionEngineWrapper::of(const llvm::ExecutionEngine *executionEnginePtr) { + Nan::EscapableHandleScope escapeScope {}; + v8::Local tpl = Nan::New(executionEngineTemplate); + + auto object = Nan::NewInstance(tpl).ToLocalChecked(); + auto* wrapper = new ExecutionEngineWrapper { executionEnginePtr }; + wrapper->Wrap(object); + + return escapeScope.Escape(object); +} diff --git a/src/execution-engine/execution-engine.h b/src/execution-engine/execution-engine.h new file mode 100644 index 0000000..4b2af03 --- /dev/null +++ b/src/execution-engine/execution-engine.h @@ -0,0 +1,23 @@ + +#ifndef LLVM_NODE_EXECUTION_ENGINE_H +#define LLVM_NODE_EXECUTION_ENGINE_H + +#include +#include +#include "../util/from-value-mixin.h" + +class ExecutionEngineWrapper: public Nan::ObjectWrap, public FromValueMixin { +public: + static NAN_MODULE_INIT(Init); + static v8::Local of(const llvm::ExecutionEngine *ptr); + +private: + const llvm::ExecutionEngine* executionEngine; + static Nan::Persistent executionEngineTemplate; + + explicit ExecutionEngineWrapper(const llvm::ExecutionEngine* executionEngine): executionEngine { executionEngine } { + assert(executionEngine && "No execute engine passed"); + } +}; + +#endif //LLVM_NODE_EXECUTION_ENGINE_H diff --git a/src/llvm-node.cc b/src/llvm-node.cc index bc7c36c..1d97a7c 100644 --- a/src/llvm-node.cc +++ b/src/llvm-node.cc @@ -5,13 +5,14 @@ #include "config/config.h" #include "support/support.h" #include "target/target.h" +#include "execution-engine/execution-engine-module.h" NAN_MODULE_INIT(InitAll) { InitBitCode(target); InitConfig(target); InitIR(target); InitSupport(target); - InitTarget(target); + InitExecutionEngine(target); } NODE_MODULE(llvm, InitAll) \ No newline at end of file From 46912c7745191643a2c6aba7c41b1287a3db63b9 Mon Sep 17 00:00:00 2001 From: Dmitry Patsura Date: Sun, 4 Nov 2018 17:03:53 +0900 Subject: [PATCH 2/4] Feature: ExecutionEngine.addModule - add Signed-off-by: Dmitry Patsura --- llvm-node.d.ts | 5 +++++ src/execution-engine/execution-engine.cc | 15 ++++++++++++++- src/execution-engine/execution-engine.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/llvm-node.d.ts b/llvm-node.d.ts index 50b072d..138d0a8 100644 --- a/llvm-node.d.ts +++ b/llvm-node.d.ts @@ -684,6 +684,11 @@ declare namespace llvm { } class ExecutionEngine { + /** + * Add a Module to the list of modules that we can JIT from. + * @param module + */ + addModule(module: Module); } // support diff --git a/src/execution-engine/execution-engine.cc b/src/execution-engine/execution-engine.cc index 06e32be..5a09873 100644 --- a/src/execution-engine/execution-engine.cc +++ b/src/execution-engine/execution-engine.cc @@ -1,6 +1,6 @@ #include "execution-engine.h" -#include "../ir/data-layout.h" +#include "../ir/module.h" Nan::Persistent ExecutionEngineWrapper::executionEngineTemplate {}; @@ -8,9 +8,22 @@ NAN_MODULE_INIT(ExecutionEngineWrapper::Init) { auto objectTemplate = Nan::New(); objectTemplate->SetInternalFieldCount(1); + Nan::SetMethod(objectTemplate, "addModule", ExecutionEngineWrapper::addModule); + executionEngineTemplate.Reset(objectTemplate); } +NAN_METHOD(ExecutionEngineWrapper::addModule) { + if (info.Length() != 1 || !ModuleWrapper::isInstance(info[0])) { + return Nan::ThrowTypeError("addModule needs to be called with: mod: Module"); + } + + auto* module = ModuleWrapper::FromValue(info[0])->getModule(); + + auto* executionEngine = ExecutionEngineWrapper::FromValue(info.Holder())->executionEngine; + executionEngine->addModule(std::make_unique(module)); +} + v8::Local ExecutionEngineWrapper::of(const llvm::ExecutionEngine *executionEnginePtr) { Nan::EscapableHandleScope escapeScope {}; v8::Local tpl = Nan::New(executionEngineTemplate); diff --git a/src/execution-engine/execution-engine.h b/src/execution-engine/execution-engine.h index 4b2af03..478da58 100644 --- a/src/execution-engine/execution-engine.h +++ b/src/execution-engine/execution-engine.h @@ -18,6 +18,8 @@ class ExecutionEngineWrapper: public Nan::ObjectWrap, public FromValueMixin Date: Sat, 8 Dec 2018 15:04:52 +0900 Subject: [PATCH 3/4] Fix: Compilation issues Signed-off-by: Dmitry Patsura --- src/execution-engine/execution-engine.cc | 4 ++-- src/execution-engine/execution-engine.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/execution-engine/execution-engine.cc b/src/execution-engine/execution-engine.cc index 5a09873..d4c3b40 100644 --- a/src/execution-engine/execution-engine.cc +++ b/src/execution-engine/execution-engine.cc @@ -21,10 +21,10 @@ NAN_METHOD(ExecutionEngineWrapper::addModule) { auto* module = ModuleWrapper::FromValue(info[0])->getModule(); auto* executionEngine = ExecutionEngineWrapper::FromValue(info.Holder())->executionEngine; - executionEngine->addModule(std::make_unique(module)); + executionEngine->addModule(std::unique_ptr(module)); } -v8::Local ExecutionEngineWrapper::of(const llvm::ExecutionEngine *executionEnginePtr) { +v8::Local ExecutionEngineWrapper::of(llvm::ExecutionEngine *executionEnginePtr) { Nan::EscapableHandleScope escapeScope {}; v8::Local tpl = Nan::New(executionEngineTemplate); diff --git a/src/execution-engine/execution-engine.h b/src/execution-engine/execution-engine.h index 478da58..98187c1 100644 --- a/src/execution-engine/execution-engine.h +++ b/src/execution-engine/execution-engine.h @@ -9,13 +9,13 @@ class ExecutionEngineWrapper: public Nan::ObjectWrap, public FromValueMixin { public: static NAN_MODULE_INIT(Init); - static v8::Local of(const llvm::ExecutionEngine *ptr); + static v8::Local of(llvm::ExecutionEngine *ptr); private: - const llvm::ExecutionEngine* executionEngine; + llvm::ExecutionEngine* executionEngine; static Nan::Persistent executionEngineTemplate; - explicit ExecutionEngineWrapper(const llvm::ExecutionEngine* executionEngine): executionEngine { executionEngine } { + explicit ExecutionEngineWrapper(llvm::ExecutionEngine* executionEngine): executionEngine { executionEngine } { assert(executionEngine && "No execute engine passed"); } From 132b59e36c862c7190bbb472cfc4a3fb4ea97ecf Mon Sep 17 00:00:00 2001 From: Dmitry Patsura Date: Wed, 2 Jan 2019 01:32:19 +0900 Subject: [PATCH 4/4] Fix: llvm-node.d.ts - issue --- llvm-node.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm-node.d.ts b/llvm-node.d.ts index 138d0a8..8e19afd 100644 --- a/llvm-node.d.ts +++ b/llvm-node.d.ts @@ -688,7 +688,7 @@ declare namespace llvm { * Add a Module to the list of modules that we can JIT from. * @param module */ - addModule(module: Module); + addModule(module: Module): void; } // support