Skip to content

WIP: Feature: ExecutionEngine - initial #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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} )
8 changes: 8 additions & 0 deletions llvm-node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,14 @@ declare namespace llvm {
getTypeByName(name: string): StructType | null;
}

class ExecutionEngine {
/**
* Add a Module to the list of modules that we can JIT from.
* @param module
*/
addModule(module: Module): void;
}

// support
class TargetRegistry {
private constructor();
Expand Down
12 changes: 12 additions & 0 deletions src/execution-engine/execution-engine-module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#ifndef LLVM_NODE_EXECUTION_ENGINE_MODULE_H
#define LLVM_NODE_EXECUTION_ENGINE_MODULE_H

#include <nan.h>
#include "execution-engine.h"

NAN_MODULE_INIT(InitExecutionEngine) {
ExecutionEngineWrapper::Init(target);
}

#endif //LLVM_NODE_EXECUTION_ENGINE_MODULE_H
36 changes: 36 additions & 0 deletions src/execution-engine/execution-engine.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include "execution-engine.h"
#include "../ir/module.h"

Nan::Persistent<v8::ObjectTemplate> ExecutionEngineWrapper::executionEngineTemplate {};

NAN_MODULE_INIT(ExecutionEngineWrapper::Init) {
auto objectTemplate = Nan::New<v8::ObjectTemplate>();
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::unique_ptr<llvm::Module>(module));
}

v8::Local<v8::Object> ExecutionEngineWrapper::of(llvm::ExecutionEngine *executionEnginePtr) {
Nan::EscapableHandleScope escapeScope {};
v8::Local<v8::ObjectTemplate> tpl = Nan::New(executionEngineTemplate);

auto object = Nan::NewInstance(tpl).ToLocalChecked();
auto* wrapper = new ExecutionEngineWrapper { executionEnginePtr };
wrapper->Wrap(object);

return escapeScope.Escape(object);
}
25 changes: 25 additions & 0 deletions src/execution-engine/execution-engine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#ifndef LLVM_NODE_EXECUTION_ENGINE_H
#define LLVM_NODE_EXECUTION_ENGINE_H

#include <nan.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include "../util/from-value-mixin.h"

class ExecutionEngineWrapper: public Nan::ObjectWrap, public FromValueMixin<ExecutionEngineWrapper> {
public:
static NAN_MODULE_INIT(Init);
static v8::Local<v8::Object> of(llvm::ExecutionEngine *ptr);

private:
llvm::ExecutionEngine* executionEngine;
static Nan::Persistent<v8::ObjectTemplate> executionEngineTemplate;

explicit ExecutionEngineWrapper(llvm::ExecutionEngine* executionEngine): executionEngine { executionEngine } {
assert(executionEngine && "No execute engine passed");
}

static NAN_METHOD(addModule);
};

#endif //LLVM_NODE_EXECUTION_ENGINE_H
3 changes: 2 additions & 1 deletion src/llvm-node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)