Skip to content

Commit

Permalink
move plugin loading code to plugin.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-kenzel committed Aug 9, 2023
1 parent 6de7224 commit 35d6164
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 61 deletions.
1 change: 1 addition & 0 deletions src/thorin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(THORIN_SOURCES
type.h
world.cpp
world.h
plugin.cpp
plugin.h
plugin_helpers.h
analyses/cfg.cpp
Expand Down
67 changes: 67 additions & 0 deletions src/thorin/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "thorin/world.h"

#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <execinfo.h>
#include <dlfcn.h>
#endif

namespace thorin {
bool World::load_plugin(const char* plugin_name) {
#ifdef _WIN32
return false;
#else
void *handle = dlopen(plugin_name, RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
ELOG("Error loading plugin {}: {}", plugin_name, dlerror());
ELOG("Is plugin contained in LD_LIBRARY_PATH?");
return false;
}
dlerror();

char *error;
auto initfunc = reinterpret_cast<plugin_init_func_t*>(dlsym(handle, "init"));
if ((error = dlerror()) != NULL) {
ILOG("Plugin {} did not supply an init function", plugin_name);
} else {
initfunc();
}

data_.plugin_modules_.push_back(handle);
return true;
#endif
}

template <typename T>
static T* lookup_plugin_function(void* plugin_module, const char* function_name) {
#ifdef _WIN32
#else
if (void* func = dlsym(plugin_module, function_name)) {
return reinterpret_cast<T*>(func);
}
#endif
return nullptr;
}

unique_plugin_intrinsic World::load_plugin_intrinsic(const char* function_name) const {
for (auto plugin_module : data_.plugin_modules_) {
if (auto create_intrinsic = lookup_plugin_function<plugin_intrinsic_create_func_t>(plugin_module, function_name))
return unique_plugin_intrinsic(create_intrinsic());
}

return nullptr;
}

void World::link_plugin_intrinsic(Continuation* cont) {
auto intrinsic = load_plugin_intrinsic(cont->name().c_str());

if (!intrinsic)
error(cont->loc(), "failed to link plugin intrinsic {}", cont->name());

cont->attributes().intrinsic = Intrinsic::Plugin;
data_.plugin_intrinsics_.push_back({ cont, std::move(intrinsic) });
}
}
61 changes: 0 additions & 61 deletions src/thorin/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@

#include <cmath>
#include <algorithm>
#ifdef _MSC_VER
#include <windows.h>
#else
#include <execinfo.h>
#include <dlfcn.h>
#endif

#include "thorin/def.h"
#include "thorin/primop.h"
Expand Down Expand Up @@ -1315,59 +1309,4 @@ void World::opt() {
RUN_PASS(cleanup())
RUN_PASS(codegen_prepare(*this))
}

bool World::load_plugin(const char* plugin_name) {
#ifdef _MSC_VER
return false;
#else // _MSC_VER
void *handle = dlopen(plugin_name, RTLD_LAZY | RTLD_GLOBAL);
if (!handle) {
ELOG("Error loading plugin {}: {}", plugin_name, dlerror());
ELOG("Is plugin contained in LD_LIBRARY_PATH?");
return false;
}
dlerror();

char *error;
auto initfunc = reinterpret_cast<plugin_init_func_t*>(dlsym(handle, "init"));
if ((error = dlerror()) != NULL) {
ILOG("Plugin {} did not supply an init function", plugin_name);
} else {
initfunc();
}

data_.plugin_modules_.push_back(handle);
return true;
#endif // _MSC_VER
}

template <typename T>
static T* lookup_plugin_function(void* plugin_module, const char* function_name) {
#ifdef _MSC_VER
#else // _MSC_VER
if (void* func = dlsym(plugin_module, function_name)) {
return reinterpret_cast<T*>(func);
}
#endif // _MSC_VER
return nullptr;
}

unique_plugin_intrinsic World::load_plugin_intrinsic(const char* function_name) const {
for (auto plugin_module : data_.plugin_modules_) {
if (auto create_intrinsic = lookup_plugin_function<plugin_intrinsic_create_func_t>(plugin_module, function_name))
return unique_plugin_intrinsic(create_intrinsic());
}

return nullptr;
}

void World::link_plugin_intrinsic(Continuation* cont) {
auto intrinsic = load_plugin_intrinsic(cont->name().c_str());

if (!intrinsic)
error(cont->loc(), "failed to link plugin intrinsic {}", cont->name());

cont->attributes().intrinsic = Intrinsic::Plugin;
data_.plugin_intrinsics_.push_back({ cont, std::move(intrinsic) });
}
}

0 comments on commit 35d6164

Please sign in to comment.